• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.h>
14 
15 #include <uapi/linux/io_uring.h>
16 
17 #include "io_uring.h"
18 #include "opdef.h"
19 #include "kbuf.h"
20 #include "rsrc.h"
21 #include "rw.h"
22 
23 struct io_rw {
24 	/* NOTE: kiocb has the file as the first member, so don't do it here */
25 	struct kiocb			kiocb;
26 	u64				addr;
27 	u32				len;
28 	rwf_t				flags;
29 };
30 
io_file_supports_nowait(struct io_kiocb * req)31 static inline bool io_file_supports_nowait(struct io_kiocb *req)
32 {
33 	return req->flags & REQ_F_SUPPORT_NOWAIT;
34 }
35 
36 #ifdef CONFIG_COMPAT
io_iov_compat_buffer_select_prep(struct io_rw * rw)37 static int io_iov_compat_buffer_select_prep(struct io_rw *rw)
38 {
39 	struct compat_iovec __user *uiov;
40 	compat_ssize_t clen;
41 
42 	uiov = u64_to_user_ptr(rw->addr);
43 	if (!access_ok(uiov, sizeof(*uiov)))
44 		return -EFAULT;
45 	if (__get_user(clen, &uiov->iov_len))
46 		return -EFAULT;
47 	if (clen < 0)
48 		return -EINVAL;
49 
50 	rw->len = clen;
51 	return 0;
52 }
53 #endif
54 
io_iov_buffer_select_prep(struct io_kiocb * req)55 static int io_iov_buffer_select_prep(struct io_kiocb *req)
56 {
57 	struct iovec __user *uiov;
58 	struct iovec iov;
59 	struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw);
60 
61 	if (rw->len != 1)
62 		return -EINVAL;
63 
64 #ifdef CONFIG_COMPAT
65 	if (req->ctx->compat)
66 		return io_iov_compat_buffer_select_prep(rw);
67 #endif
68 
69 	uiov = u64_to_user_ptr(rw->addr);
70 	if (copy_from_user(&iov, uiov, sizeof(*uiov)))
71 		return -EFAULT;
72 	rw->len = iov.iov_len;
73 	return 0;
74 }
75 
io_prep_rw(struct io_kiocb * req,const struct io_uring_sqe * sqe)76 int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe)
77 {
78 	struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw);
79 	unsigned ioprio;
80 	int ret;
81 
82 	rw->kiocb.ki_pos = READ_ONCE(sqe->off);
83 	/* used for fixed read/write too - just read unconditionally */
84 	req->buf_index = READ_ONCE(sqe->buf_index);
85 
86 	if (req->opcode == IORING_OP_READ_FIXED ||
87 	    req->opcode == IORING_OP_WRITE_FIXED) {
88 		struct io_ring_ctx *ctx = req->ctx;
89 		u16 index;
90 
91 		if (unlikely(req->buf_index >= ctx->nr_user_bufs))
92 			return -EFAULT;
93 		index = array_index_nospec(req->buf_index, ctx->nr_user_bufs);
94 		req->imu = ctx->user_bufs[index];
95 		io_req_set_rsrc_node(req, ctx, 0);
96 	}
97 
98 	ioprio = READ_ONCE(sqe->ioprio);
99 	if (ioprio) {
100 		ret = ioprio_check_cap(ioprio);
101 		if (ret)
102 			return ret;
103 
104 		rw->kiocb.ki_ioprio = ioprio;
105 	} else {
106 		rw->kiocb.ki_ioprio = get_current_ioprio();
107 	}
108 
109 	rw->addr = READ_ONCE(sqe->addr);
110 	rw->len = READ_ONCE(sqe->len);
111 	rw->flags = READ_ONCE(sqe->rw_flags);
112 
113 	/* Have to do this validation here, as this is in io_read() rw->len might
114 	 * have chanaged due to buffer selection
115 	 */
116 	if (req->opcode == IORING_OP_READV && req->flags & REQ_F_BUFFER_SELECT) {
117 		ret = io_iov_buffer_select_prep(req);
118 		if (ret)
119 			return ret;
120 	}
121 
122 	return 0;
123 }
124 
io_readv_writev_cleanup(struct io_kiocb * req)125 void io_readv_writev_cleanup(struct io_kiocb *req)
126 {
127 	struct io_async_rw *io = req->async_data;
128 
129 	kfree(io->free_iovec);
130 }
131 
io_rw_done(struct kiocb * kiocb,ssize_t ret)132 static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
133 {
134 	switch (ret) {
135 	case -EIOCBQUEUED:
136 		break;
137 	case -ERESTARTSYS:
138 	case -ERESTARTNOINTR:
139 	case -ERESTARTNOHAND:
140 	case -ERESTART_RESTARTBLOCK:
141 		/*
142 		 * We can't just restart the syscall, since previously
143 		 * submitted sqes may already be in progress. Just fail this
144 		 * IO with EINTR.
145 		 */
146 		ret = -EINTR;
147 		fallthrough;
148 	default:
149 		kiocb->ki_complete(kiocb, ret);
150 	}
151 }
152 
io_kiocb_update_pos(struct io_kiocb * req)153 static inline loff_t *io_kiocb_update_pos(struct io_kiocb *req)
154 {
155 	struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw);
156 
157 	if (rw->kiocb.ki_pos != -1)
158 		return &rw->kiocb.ki_pos;
159 
160 	if (!(req->file->f_mode & FMODE_STREAM)) {
161 		req->flags |= REQ_F_CUR_POS;
162 		rw->kiocb.ki_pos = req->file->f_pos;
163 		return &rw->kiocb.ki_pos;
164 	}
165 
166 	rw->kiocb.ki_pos = 0;
167 	return NULL;
168 }
169 
io_req_task_queue_reissue(struct io_kiocb * req)170 static void io_req_task_queue_reissue(struct io_kiocb *req)
171 {
172 	req->io_task_work.func = io_queue_iowq;
173 	io_req_task_work_add(req);
174 }
175 
176 #ifdef CONFIG_BLOCK
io_resubmit_prep(struct io_kiocb * req)177 static bool io_resubmit_prep(struct io_kiocb *req)
178 {
179 	struct io_async_rw *io = req->async_data;
180 
181 	if (!req_has_async_data(req))
182 		return !io_req_prep_async(req);
183 	iov_iter_restore(&io->s.iter, &io->s.iter_state);
184 	return true;
185 }
186 
io_rw_should_reissue(struct io_kiocb * req)187 static bool io_rw_should_reissue(struct io_kiocb *req)
188 {
189 	umode_t mode = file_inode(req->file)->i_mode;
190 	struct io_ring_ctx *ctx = req->ctx;
191 
192 	if (!S_ISBLK(mode) && !S_ISREG(mode))
193 		return false;
194 	if ((req->flags & REQ_F_NOWAIT) || (io_wq_current_is_worker() &&
195 	    !(ctx->flags & IORING_SETUP_IOPOLL)))
196 		return false;
197 	/*
198 	 * If ref is dying, we might be running poll reap from the exit work.
199 	 * Don't attempt to reissue from that path, just let it fail with
200 	 * -EAGAIN.
201 	 */
202 	if (percpu_ref_is_dying(&ctx->refs))
203 		return false;
204 	/*
205 	 * Play it safe and assume not safe to re-import and reissue if we're
206 	 * not in the original thread group (or in task context).
207 	 */
208 	if (!same_thread_group(req->task, current) || !in_task())
209 		return false;
210 	return true;
211 }
212 #else
io_resubmit_prep(struct io_kiocb * req)213 static bool io_resubmit_prep(struct io_kiocb *req)
214 {
215 	return false;
216 }
io_rw_should_reissue(struct io_kiocb * req)217 static bool io_rw_should_reissue(struct io_kiocb *req)
218 {
219 	return false;
220 }
221 #endif
222 
kiocb_end_write(struct io_kiocb * req)223 static void kiocb_end_write(struct io_kiocb *req)
224 {
225 	/*
226 	 * Tell lockdep we inherited freeze protection from submission
227 	 * thread.
228 	 */
229 	if (req->flags & REQ_F_ISREG) {
230 		struct super_block *sb = file_inode(req->file)->i_sb;
231 
232 		__sb_writers_acquired(sb, SB_FREEZE_WRITE);
233 		sb_end_write(sb);
234 	}
235 }
236 
237 /*
238  * Trigger the notifications after having done some IO, and finish the write
239  * accounting, if any.
240  */
io_req_io_end(struct io_kiocb * req)241 static void io_req_io_end(struct io_kiocb *req)
242 {
243 	struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw);
244 
245 	if (rw->kiocb.ki_flags & IOCB_WRITE) {
246 		kiocb_end_write(req);
247 		fsnotify_modify(req->file);
248 	} else {
249 		fsnotify_access(req->file);
250 	}
251 }
252 
__io_complete_rw_common(struct io_kiocb * req,long res)253 static bool __io_complete_rw_common(struct io_kiocb *req, long res)
254 {
255 	if (unlikely(res != req->cqe.res)) {
256 		if ((res == -EAGAIN || res == -EOPNOTSUPP) &&
257 		    io_rw_should_reissue(req)) {
258 			/*
259 			 * Reissue will start accounting again, finish the
260 			 * current cycle.
261 			 */
262 			io_req_io_end(req);
263 			req->flags |= REQ_F_REISSUE | REQ_F_PARTIAL_IO;
264 			return true;
265 		}
266 		req_set_fail(req);
267 		req->cqe.res = res;
268 	}
269 	return false;
270 }
271 
io_fixup_rw_res(struct io_kiocb * req,long res)272 static inline int io_fixup_rw_res(struct io_kiocb *req, long res)
273 {
274 	struct io_async_rw *io = req->async_data;
275 
276 	/* add previously done IO, if any */
277 	if (req_has_async_data(req) && io->bytes_done > 0) {
278 		if (res < 0)
279 			res = io->bytes_done;
280 		else
281 			res += io->bytes_done;
282 	}
283 	return res;
284 }
285 
io_req_rw_complete(struct io_kiocb * req,bool * locked)286 static void io_req_rw_complete(struct io_kiocb *req, bool *locked)
287 {
288 	io_req_io_end(req);
289 	io_req_task_complete(req, locked);
290 }
291 
io_complete_rw(struct kiocb * kiocb,long res)292 static void io_complete_rw(struct kiocb *kiocb, long res)
293 {
294 	struct io_rw *rw = container_of(kiocb, struct io_rw, kiocb);
295 	struct io_kiocb *req = cmd_to_io_kiocb(rw);
296 
297 	if (__io_complete_rw_common(req, res))
298 		return;
299 	io_req_set_res(req, io_fixup_rw_res(req, res), 0);
300 	req->io_task_work.func = io_req_rw_complete;
301 	io_req_task_work_add(req);
302 }
303 
io_complete_rw_iopoll(struct kiocb * kiocb,long res)304 static void io_complete_rw_iopoll(struct kiocb *kiocb, long res)
305 {
306 	struct io_rw *rw = container_of(kiocb, struct io_rw, kiocb);
307 	struct io_kiocb *req = cmd_to_io_kiocb(rw);
308 
309 	if (kiocb->ki_flags & IOCB_WRITE)
310 		kiocb_end_write(req);
311 	if (unlikely(res != req->cqe.res)) {
312 		if (res == -EAGAIN && io_rw_should_reissue(req)) {
313 			req->flags |= REQ_F_REISSUE | REQ_F_PARTIAL_IO;
314 			return;
315 		}
316 		req->cqe.res = res;
317 	}
318 
319 	/* order with io_iopoll_complete() checking ->iopoll_completed */
320 	smp_store_release(&req->iopoll_completed, 1);
321 }
322 
kiocb_done(struct io_kiocb * req,ssize_t ret,unsigned int issue_flags)323 static int kiocb_done(struct io_kiocb *req, ssize_t ret,
324 		       unsigned int issue_flags)
325 {
326 	struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw);
327 	unsigned final_ret = io_fixup_rw_res(req, ret);
328 
329 	if (ret >= 0 && req->flags & REQ_F_CUR_POS)
330 		req->file->f_pos = rw->kiocb.ki_pos;
331 	if (ret >= 0 && (rw->kiocb.ki_complete == io_complete_rw)) {
332 		if (!__io_complete_rw_common(req, ret)) {
333 			/*
334 			 * Safe to call io_end from here as we're inline
335 			 * from the submission path.
336 			 */
337 			io_req_io_end(req);
338 			io_req_set_res(req, final_ret,
339 				       io_put_kbuf(req, issue_flags));
340 			return IOU_OK;
341 		}
342 	} else {
343 		io_rw_done(&rw->kiocb, ret);
344 	}
345 
346 	if (req->flags & REQ_F_REISSUE) {
347 		req->flags &= ~REQ_F_REISSUE;
348 		if (io_resubmit_prep(req))
349 			io_req_task_queue_reissue(req);
350 		else
351 			io_req_task_queue_fail(req, final_ret);
352 	}
353 	return IOU_ISSUE_SKIP_COMPLETE;
354 }
355 
__io_import_iovec(int ddir,struct io_kiocb * req,struct io_rw_state * s,unsigned int issue_flags)356 static struct iovec *__io_import_iovec(int ddir, struct io_kiocb *req,
357 				       struct io_rw_state *s,
358 				       unsigned int issue_flags)
359 {
360 	struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw);
361 	struct iov_iter *iter = &s->iter;
362 	u8 opcode = req->opcode;
363 	struct iovec *iovec;
364 	void __user *buf;
365 	size_t sqe_len;
366 	ssize_t ret;
367 
368 	if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
369 		ret = io_import_fixed(ddir, iter, req->imu, rw->addr, rw->len);
370 		if (ret)
371 			return ERR_PTR(ret);
372 		return NULL;
373 	}
374 
375 	buf = u64_to_user_ptr(rw->addr);
376 	sqe_len = rw->len;
377 
378 	if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE ||
379 	    (req->flags & REQ_F_BUFFER_SELECT)) {
380 		if (io_do_buffer_select(req)) {
381 			buf = io_buffer_select(req, &sqe_len, issue_flags);
382 			if (!buf)
383 				return ERR_PTR(-ENOBUFS);
384 			rw->addr = (unsigned long) buf;
385 			rw->len = sqe_len;
386 		}
387 
388 		ret = import_single_range(ddir, buf, sqe_len, s->fast_iov, iter);
389 		if (ret)
390 			return ERR_PTR(ret);
391 		return NULL;
392 	}
393 
394 	iovec = s->fast_iov;
395 	ret = __import_iovec(ddir, buf, sqe_len, UIO_FASTIOV, &iovec, iter,
396 			      req->ctx->compat);
397 	if (unlikely(ret < 0))
398 		return ERR_PTR(ret);
399 	return iovec;
400 }
401 
io_import_iovec(int rw,struct io_kiocb * req,struct iovec ** iovec,struct io_rw_state * s,unsigned int issue_flags)402 static inline int io_import_iovec(int rw, struct io_kiocb *req,
403 				  struct iovec **iovec, struct io_rw_state *s,
404 				  unsigned int issue_flags)
405 {
406 	*iovec = __io_import_iovec(rw, req, s, issue_flags);
407 	if (unlikely(IS_ERR(*iovec)))
408 		return PTR_ERR(*iovec);
409 
410 	iov_iter_save_state(&s->iter, &s->iter_state);
411 	return 0;
412 }
413 
io_kiocb_ppos(struct kiocb * kiocb)414 static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb)
415 {
416 	return (kiocb->ki_filp->f_mode & FMODE_STREAM) ? NULL : &kiocb->ki_pos;
417 }
418 
419 /*
420  * For files that don't have ->read_iter() and ->write_iter(), handle them
421  * by looping over ->read() or ->write() manually.
422  */
loop_rw_iter(int ddir,struct io_rw * rw,struct iov_iter * iter)423 static ssize_t loop_rw_iter(int ddir, struct io_rw *rw, struct iov_iter *iter)
424 {
425 	struct kiocb *kiocb = &rw->kiocb;
426 	struct file *file = kiocb->ki_filp;
427 	ssize_t ret = 0;
428 	loff_t *ppos;
429 
430 	/*
431 	 * Don't support polled IO through this interface, and we can't
432 	 * support non-blocking either. For the latter, this just causes
433 	 * the kiocb to be handled from an async context.
434 	 */
435 	if (kiocb->ki_flags & IOCB_HIPRI)
436 		return -EOPNOTSUPP;
437 	if ((kiocb->ki_flags & IOCB_NOWAIT) &&
438 	    !(kiocb->ki_filp->f_flags & O_NONBLOCK))
439 		return -EAGAIN;
440 
441 	ppos = io_kiocb_ppos(kiocb);
442 
443 	while (iov_iter_count(iter)) {
444 		struct iovec iovec;
445 		ssize_t nr;
446 
447 		if (!iov_iter_is_bvec(iter)) {
448 			iovec = iov_iter_iovec(iter);
449 		} else {
450 			iovec.iov_base = u64_to_user_ptr(rw->addr);
451 			iovec.iov_len = rw->len;
452 		}
453 
454 		if (ddir == READ) {
455 			nr = file->f_op->read(file, iovec.iov_base,
456 					      iovec.iov_len, ppos);
457 		} else {
458 			nr = file->f_op->write(file, iovec.iov_base,
459 					       iovec.iov_len, ppos);
460 		}
461 
462 		if (nr < 0) {
463 			if (!ret)
464 				ret = nr;
465 			break;
466 		}
467 		ret += nr;
468 		if (!iov_iter_is_bvec(iter)) {
469 			iov_iter_advance(iter, nr);
470 		} else {
471 			rw->addr += nr;
472 			rw->len -= nr;
473 			if (!rw->len)
474 				break;
475 		}
476 		if (nr != iovec.iov_len)
477 			break;
478 	}
479 
480 	return ret;
481 }
482 
io_req_map_rw(struct io_kiocb * req,const struct iovec * iovec,const struct iovec * fast_iov,struct iov_iter * iter)483 static void io_req_map_rw(struct io_kiocb *req, const struct iovec *iovec,
484 			  const struct iovec *fast_iov, struct iov_iter *iter)
485 {
486 	struct io_async_rw *io = req->async_data;
487 
488 	memcpy(&io->s.iter, iter, sizeof(*iter));
489 	io->free_iovec = iovec;
490 	io->bytes_done = 0;
491 	/* can only be fixed buffers, no need to do anything */
492 	if (iov_iter_is_bvec(iter))
493 		return;
494 	if (!iovec) {
495 		unsigned iov_off = 0;
496 
497 		io->s.iter.iov = io->s.fast_iov;
498 		if (iter->iov != fast_iov) {
499 			iov_off = iter->iov - fast_iov;
500 			io->s.iter.iov += iov_off;
501 		}
502 		if (io->s.fast_iov != fast_iov)
503 			memcpy(io->s.fast_iov + iov_off, fast_iov + iov_off,
504 			       sizeof(struct iovec) * iter->nr_segs);
505 	} else {
506 		req->flags |= REQ_F_NEED_CLEANUP;
507 	}
508 }
509 
io_setup_async_rw(struct io_kiocb * req,const struct iovec * iovec,struct io_rw_state * s,bool force)510 static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec,
511 			     struct io_rw_state *s, bool force)
512 {
513 	if (!force && !io_op_defs[req->opcode].prep_async)
514 		return 0;
515 	if (!req_has_async_data(req)) {
516 		struct io_async_rw *iorw;
517 
518 		if (io_alloc_async_data(req)) {
519 			kfree(iovec);
520 			return -ENOMEM;
521 		}
522 
523 		io_req_map_rw(req, iovec, s->fast_iov, &s->iter);
524 		iorw = req->async_data;
525 		/* we've copied and mapped the iter, ensure state is saved */
526 		iov_iter_save_state(&iorw->s.iter, &iorw->s.iter_state);
527 	}
528 	return 0;
529 }
530 
io_rw_prep_async(struct io_kiocb * req,int rw)531 static inline int io_rw_prep_async(struct io_kiocb *req, int rw)
532 {
533 	struct io_async_rw *iorw = req->async_data;
534 	struct iovec *iov;
535 	int ret;
536 
537 	iorw->bytes_done = 0;
538 	iorw->free_iovec = NULL;
539 
540 	/* submission path, ->uring_lock should already be taken */
541 	ret = io_import_iovec(rw, req, &iov, &iorw->s, 0);
542 	if (unlikely(ret < 0))
543 		return ret;
544 
545 	if (iov) {
546 		iorw->free_iovec = iov;
547 		req->flags |= REQ_F_NEED_CLEANUP;
548 	}
549 
550 	return 0;
551 }
552 
io_readv_prep_async(struct io_kiocb * req)553 int io_readv_prep_async(struct io_kiocb *req)
554 {
555 	return io_rw_prep_async(req, ITER_DEST);
556 }
557 
io_writev_prep_async(struct io_kiocb * req)558 int io_writev_prep_async(struct io_kiocb *req)
559 {
560 	return io_rw_prep_async(req, ITER_SOURCE);
561 }
562 
563 /*
564  * This is our waitqueue callback handler, registered through __folio_lock_async()
565  * when we initially tried to do the IO with the iocb armed our waitqueue.
566  * This gets called when the page is unlocked, and we generally expect that to
567  * happen when the page IO is completed and the page is now uptodate. This will
568  * queue a task_work based retry of the operation, attempting to copy the data
569  * again. If the latter fails because the page was NOT uptodate, then we will
570  * do a thread based blocking retry of the operation. That's the unexpected
571  * slow path.
572  */
io_async_buf_func(struct wait_queue_entry * wait,unsigned mode,int sync,void * arg)573 static int io_async_buf_func(struct wait_queue_entry *wait, unsigned mode,
574 			     int sync, void *arg)
575 {
576 	struct wait_page_queue *wpq;
577 	struct io_kiocb *req = wait->private;
578 	struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw);
579 	struct wait_page_key *key = arg;
580 
581 	wpq = container_of(wait, struct wait_page_queue, wait);
582 
583 	if (!wake_page_match(wpq, key))
584 		return 0;
585 
586 	rw->kiocb.ki_flags &= ~IOCB_WAITQ;
587 	list_del_init(&wait->entry);
588 	io_req_task_queue(req);
589 	return 1;
590 }
591 
592 /*
593  * This controls whether a given IO request should be armed for async page
594  * based retry. If we return false here, the request is handed to the async
595  * worker threads for retry. If we're doing buffered reads on a regular file,
596  * we prepare a private wait_page_queue entry and retry the operation. This
597  * will either succeed because the page is now uptodate and unlocked, or it
598  * will register a callback when the page is unlocked at IO completion. Through
599  * that callback, io_uring uses task_work to setup a retry of the operation.
600  * That retry will attempt the buffered read again. The retry will generally
601  * succeed, or in rare cases where it fails, we then fall back to using the
602  * async worker threads for a blocking retry.
603  */
io_rw_should_retry(struct io_kiocb * req)604 static bool io_rw_should_retry(struct io_kiocb *req)
605 {
606 	struct io_async_rw *io = req->async_data;
607 	struct wait_page_queue *wait = &io->wpq;
608 	struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw);
609 	struct kiocb *kiocb = &rw->kiocb;
610 
611 	/* never retry for NOWAIT, we just complete with -EAGAIN */
612 	if (req->flags & REQ_F_NOWAIT)
613 		return false;
614 
615 	/* Only for buffered IO */
616 	if (kiocb->ki_flags & (IOCB_DIRECT | IOCB_HIPRI))
617 		return false;
618 
619 	/*
620 	 * just use poll if we can, and don't attempt if the fs doesn't
621 	 * support callback based unlocks
622 	 */
623 	if (file_can_poll(req->file) || !(req->file->f_mode & FMODE_BUF_RASYNC))
624 		return false;
625 
626 	wait->wait.func = io_async_buf_func;
627 	wait->wait.private = req;
628 	wait->wait.flags = 0;
629 	INIT_LIST_HEAD(&wait->wait.entry);
630 	kiocb->ki_flags |= IOCB_WAITQ;
631 	kiocb->ki_flags &= ~IOCB_NOWAIT;
632 	kiocb->ki_waitq = wait;
633 	return true;
634 }
635 
io_iter_do_read(struct io_rw * rw,struct iov_iter * iter)636 static inline int io_iter_do_read(struct io_rw *rw, struct iov_iter *iter)
637 {
638 	struct file *file = rw->kiocb.ki_filp;
639 
640 	if (likely(file->f_op->read_iter))
641 		return call_read_iter(file, &rw->kiocb, iter);
642 	else if (file->f_op->read)
643 		return loop_rw_iter(READ, rw, iter);
644 	else
645 		return -EINVAL;
646 }
647 
need_complete_io(struct io_kiocb * req)648 static bool need_complete_io(struct io_kiocb *req)
649 {
650 	return req->flags & REQ_F_ISREG ||
651 		S_ISBLK(file_inode(req->file)->i_mode);
652 }
653 
io_rw_init_file(struct io_kiocb * req,fmode_t mode)654 static int io_rw_init_file(struct io_kiocb *req, fmode_t mode)
655 {
656 	struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw);
657 	struct kiocb *kiocb = &rw->kiocb;
658 	struct io_ring_ctx *ctx = req->ctx;
659 	struct file *file = req->file;
660 	int ret;
661 
662 	if (unlikely(!file || !(file->f_mode & mode)))
663 		return -EBADF;
664 
665 	if (!io_req_ffs_set(req))
666 		req->flags |= io_file_get_flags(file) << REQ_F_SUPPORT_NOWAIT_BIT;
667 
668 	kiocb->ki_flags = file->f_iocb_flags;
669 	ret = kiocb_set_rw_flags(kiocb, rw->flags);
670 	if (unlikely(ret))
671 		return ret;
672 
673 	/*
674 	 * If the file is marked O_NONBLOCK, still allow retry for it if it
675 	 * supports async. Otherwise it's impossible to use O_NONBLOCK files
676 	 * reliably. If not, or it IOCB_NOWAIT is set, don't retry.
677 	 */
678 	if ((kiocb->ki_flags & IOCB_NOWAIT) ||
679 	    ((file->f_flags & O_NONBLOCK) && !io_file_supports_nowait(req)))
680 		req->flags |= REQ_F_NOWAIT;
681 
682 	if (ctx->flags & IORING_SETUP_IOPOLL) {
683 		if (!(kiocb->ki_flags & IOCB_DIRECT) || !file->f_op->iopoll)
684 			return -EOPNOTSUPP;
685 
686 		kiocb->private = NULL;
687 		kiocb->ki_flags |= IOCB_HIPRI | IOCB_ALLOC_CACHE;
688 		kiocb->ki_complete = io_complete_rw_iopoll;
689 		req->iopoll_completed = 0;
690 	} else {
691 		if (kiocb->ki_flags & IOCB_HIPRI)
692 			return -EINVAL;
693 		kiocb->ki_complete = io_complete_rw;
694 	}
695 
696 	return 0;
697 }
698 
io_read(struct io_kiocb * req,unsigned int issue_flags)699 int io_read(struct io_kiocb *req, unsigned int issue_flags)
700 {
701 	struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw);
702 	struct io_rw_state __s, *s = &__s;
703 	struct iovec *iovec;
704 	struct kiocb *kiocb = &rw->kiocb;
705 	bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
706 	struct io_async_rw *io;
707 	ssize_t ret, ret2;
708 	loff_t *ppos;
709 
710 	if (!req_has_async_data(req)) {
711 		ret = io_import_iovec(ITER_DEST, req, &iovec, s, issue_flags);
712 		if (unlikely(ret < 0))
713 			return ret;
714 	} else {
715 		io = req->async_data;
716 		s = &io->s;
717 
718 		/*
719 		 * Safe and required to re-import if we're using provided
720 		 * buffers, as we dropped the selected one before retry.
721 		 */
722 		if (io_do_buffer_select(req)) {
723 			ret = io_import_iovec(ITER_DEST, req, &iovec, s, issue_flags);
724 			if (unlikely(ret < 0))
725 				return ret;
726 		}
727 
728 		/*
729 		 * We come here from an earlier attempt, restore our state to
730 		 * match in case it doesn't. It's cheap enough that we don't
731 		 * need to make this conditional.
732 		 */
733 		iov_iter_restore(&s->iter, &s->iter_state);
734 		iovec = NULL;
735 	}
736 	ret = io_rw_init_file(req, FMODE_READ);
737 	if (unlikely(ret)) {
738 		kfree(iovec);
739 		return ret;
740 	}
741 	req->cqe.res = iov_iter_count(&s->iter);
742 
743 	if (force_nonblock) {
744 		/* If the file doesn't support async, just async punt */
745 		if (unlikely(!io_file_supports_nowait(req))) {
746 			ret = io_setup_async_rw(req, iovec, s, true);
747 			return ret ?: -EAGAIN;
748 		}
749 		kiocb->ki_flags |= IOCB_NOWAIT;
750 	} else {
751 		/* Ensure we clear previously set non-block flag */
752 		kiocb->ki_flags &= ~IOCB_NOWAIT;
753 	}
754 
755 	ppos = io_kiocb_update_pos(req);
756 
757 	ret = rw_verify_area(READ, req->file, ppos, req->cqe.res);
758 	if (unlikely(ret)) {
759 		kfree(iovec);
760 		return ret;
761 	}
762 
763 	ret = io_iter_do_read(rw, &s->iter);
764 
765 	if (ret == -EAGAIN || (req->flags & REQ_F_REISSUE)) {
766 		req->flags &= ~REQ_F_REISSUE;
767 		/* if we can poll, just do that */
768 		if (req->opcode == IORING_OP_READ && file_can_poll(req->file))
769 			return -EAGAIN;
770 		/* IOPOLL retry should happen for io-wq threads */
771 		if (!force_nonblock && !(req->ctx->flags & IORING_SETUP_IOPOLL))
772 			goto done;
773 		/* no retry on NONBLOCK nor RWF_NOWAIT */
774 		if (req->flags & REQ_F_NOWAIT)
775 			goto done;
776 		ret = 0;
777 	} else if (ret == -EIOCBQUEUED) {
778 		if (iovec)
779 			kfree(iovec);
780 		return IOU_ISSUE_SKIP_COMPLETE;
781 	} else if (ret == req->cqe.res || ret <= 0 || !force_nonblock ||
782 		   (req->flags & REQ_F_NOWAIT) || !need_complete_io(req)) {
783 		/* read all, failed, already did sync or don't want to retry */
784 		goto done;
785 	}
786 
787 	/*
788 	 * Don't depend on the iter state matching what was consumed, or being
789 	 * untouched in case of error. Restore it and we'll advance it
790 	 * manually if we need to.
791 	 */
792 	iov_iter_restore(&s->iter, &s->iter_state);
793 
794 	ret2 = io_setup_async_rw(req, iovec, s, true);
795 	iovec = NULL;
796 	if (ret2) {
797 		ret = ret > 0 ? ret : ret2;
798 		goto done;
799 	}
800 
801 	io = req->async_data;
802 	s = &io->s;
803 	/*
804 	 * Now use our persistent iterator and state, if we aren't already.
805 	 * We've restored and mapped the iter to match.
806 	 */
807 
808 	do {
809 		/*
810 		 * We end up here because of a partial read, either from
811 		 * above or inside this loop. Advance the iter by the bytes
812 		 * that were consumed.
813 		 */
814 		iov_iter_advance(&s->iter, ret);
815 		if (!iov_iter_count(&s->iter))
816 			break;
817 		io->bytes_done += ret;
818 		iov_iter_save_state(&s->iter, &s->iter_state);
819 
820 		/* if we can retry, do so with the callbacks armed */
821 		if (!io_rw_should_retry(req)) {
822 			kiocb->ki_flags &= ~IOCB_WAITQ;
823 			return -EAGAIN;
824 		}
825 
826 		req->cqe.res = iov_iter_count(&s->iter);
827 		/*
828 		 * Now retry read with the IOCB_WAITQ parts set in the iocb. If
829 		 * we get -EIOCBQUEUED, then we'll get a notification when the
830 		 * desired page gets unlocked. We can also get a partial read
831 		 * here, and if we do, then just retry at the new offset.
832 		 */
833 		ret = io_iter_do_read(rw, &s->iter);
834 		if (ret == -EIOCBQUEUED)
835 			return IOU_ISSUE_SKIP_COMPLETE;
836 		/* we got some bytes, but not all. retry. */
837 		kiocb->ki_flags &= ~IOCB_WAITQ;
838 		iov_iter_restore(&s->iter, &s->iter_state);
839 	} while (ret > 0);
840 done:
841 	/* it's faster to check here then delegate to kfree */
842 	if (iovec)
843 		kfree(iovec);
844 	return kiocb_done(req, ret, issue_flags);
845 }
846 
io_write(struct io_kiocb * req,unsigned int issue_flags)847 int io_write(struct io_kiocb *req, unsigned int issue_flags)
848 {
849 	struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw);
850 	struct io_rw_state __s, *s = &__s;
851 	struct iovec *iovec;
852 	struct kiocb *kiocb = &rw->kiocb;
853 	bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
854 	ssize_t ret, ret2;
855 	loff_t *ppos;
856 
857 	if (!req_has_async_data(req)) {
858 		ret = io_import_iovec(ITER_SOURCE, req, &iovec, s, issue_flags);
859 		if (unlikely(ret < 0))
860 			return ret;
861 	} else {
862 		struct io_async_rw *io = req->async_data;
863 
864 		s = &io->s;
865 		iov_iter_restore(&s->iter, &s->iter_state);
866 		iovec = NULL;
867 	}
868 	ret = io_rw_init_file(req, FMODE_WRITE);
869 	if (unlikely(ret)) {
870 		kfree(iovec);
871 		return ret;
872 	}
873 	req->cqe.res = iov_iter_count(&s->iter);
874 
875 	if (force_nonblock) {
876 		/* If the file doesn't support async, just async punt */
877 		if (unlikely(!io_file_supports_nowait(req)))
878 			goto copy_iov;
879 
880 		/* File path supports NOWAIT for non-direct_IO only for block devices. */
881 		if (!(kiocb->ki_flags & IOCB_DIRECT) &&
882 			!(kiocb->ki_filp->f_mode & FMODE_BUF_WASYNC) &&
883 			(req->flags & REQ_F_ISREG))
884 			goto copy_iov;
885 
886 		kiocb->ki_flags |= IOCB_NOWAIT;
887 	} else {
888 		/* Ensure we clear previously set non-block flag */
889 		kiocb->ki_flags &= ~IOCB_NOWAIT;
890 	}
891 
892 	ppos = io_kiocb_update_pos(req);
893 
894 	ret = rw_verify_area(WRITE, req->file, ppos, req->cqe.res);
895 	if (unlikely(ret)) {
896 		kfree(iovec);
897 		return ret;
898 	}
899 
900 	/*
901 	 * Open-code file_start_write here to grab freeze protection,
902 	 * which will be released by another thread in
903 	 * io_complete_rw().  Fool lockdep by telling it the lock got
904 	 * released so that it doesn't complain about the held lock when
905 	 * we return to userspace.
906 	 */
907 	if (req->flags & REQ_F_ISREG) {
908 		sb_start_write(file_inode(req->file)->i_sb);
909 		__sb_writers_release(file_inode(req->file)->i_sb,
910 					SB_FREEZE_WRITE);
911 	}
912 	kiocb->ki_flags |= IOCB_WRITE;
913 
914 	if (likely(req->file->f_op->write_iter))
915 		ret2 = call_write_iter(req->file, kiocb, &s->iter);
916 	else if (req->file->f_op->write)
917 		ret2 = loop_rw_iter(WRITE, rw, &s->iter);
918 	else
919 		ret2 = -EINVAL;
920 
921 	if (req->flags & REQ_F_REISSUE) {
922 		req->flags &= ~REQ_F_REISSUE;
923 		ret2 = -EAGAIN;
924 	}
925 
926 	/*
927 	 * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just
928 	 * retry them without IOCB_NOWAIT.
929 	 */
930 	if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
931 		ret2 = -EAGAIN;
932 	/* no retry on NONBLOCK nor RWF_NOWAIT */
933 	if (ret2 == -EAGAIN && (req->flags & REQ_F_NOWAIT))
934 		goto done;
935 	if (!force_nonblock || ret2 != -EAGAIN) {
936 		/* IOPOLL retry should happen for io-wq threads */
937 		if (ret2 == -EAGAIN && (req->ctx->flags & IORING_SETUP_IOPOLL))
938 			goto copy_iov;
939 
940 		if (ret2 != req->cqe.res && ret2 >= 0 && need_complete_io(req)) {
941 			struct io_async_rw *io;
942 
943 			trace_io_uring_short_write(req->ctx, kiocb->ki_pos - ret2,
944 						req->cqe.res, ret2);
945 
946 			/* This is a partial write. The file pos has already been
947 			 * updated, setup the async struct to complete the request
948 			 * in the worker. Also update bytes_done to account for
949 			 * the bytes already written.
950 			 */
951 			iov_iter_save_state(&s->iter, &s->iter_state);
952 			ret = io_setup_async_rw(req, iovec, s, true);
953 
954 			io = req->async_data;
955 			if (io)
956 				io->bytes_done += ret2;
957 
958 			if (kiocb->ki_flags & IOCB_WRITE)
959 				kiocb_end_write(req);
960 			return ret ? ret : -EAGAIN;
961 		}
962 done:
963 		ret = kiocb_done(req, ret2, issue_flags);
964 	} else {
965 copy_iov:
966 		iov_iter_restore(&s->iter, &s->iter_state);
967 		ret = io_setup_async_rw(req, iovec, s, false);
968 		if (!ret) {
969 			if (kiocb->ki_flags & IOCB_WRITE)
970 				kiocb_end_write(req);
971 			return -EAGAIN;
972 		}
973 		return ret;
974 	}
975 	/* it's reportedly faster than delegating the null check to kfree() */
976 	if (iovec)
977 		kfree(iovec);
978 	return ret;
979 }
980 
io_cqring_ev_posted_iopoll(struct io_ring_ctx * ctx)981 static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx)
982 {
983 	io_commit_cqring_flush(ctx);
984 	if (ctx->flags & IORING_SETUP_SQPOLL)
985 		io_cqring_wake(ctx);
986 }
987 
io_rw_fail(struct io_kiocb * req)988 void io_rw_fail(struct io_kiocb *req)
989 {
990 	int res;
991 
992 	res = io_fixup_rw_res(req, req->cqe.res);
993 	io_req_set_res(req, res, req->cqe.flags);
994 }
995 
io_do_iopoll(struct io_ring_ctx * ctx,bool force_nonspin)996 int io_do_iopoll(struct io_ring_ctx *ctx, bool force_nonspin)
997 {
998 	struct io_wq_work_node *pos, *start, *prev;
999 	unsigned int poll_flags = BLK_POLL_NOSLEEP;
1000 	DEFINE_IO_COMP_BATCH(iob);
1001 	int nr_events = 0;
1002 
1003 	/*
1004 	 * Only spin for completions if we don't have multiple devices hanging
1005 	 * off our complete list.
1006 	 */
1007 	if (ctx->poll_multi_queue || force_nonspin)
1008 		poll_flags |= BLK_POLL_ONESHOT;
1009 
1010 	wq_list_for_each(pos, start, &ctx->iopoll_list) {
1011 		struct io_kiocb *req = container_of(pos, struct io_kiocb, comp_list);
1012 		struct file *file = req->file;
1013 		int ret;
1014 
1015 		/*
1016 		 * Move completed and retryable entries to our local lists.
1017 		 * If we find a request that requires polling, break out
1018 		 * and complete those lists first, if we have entries there.
1019 		 */
1020 		if (READ_ONCE(req->iopoll_completed))
1021 			break;
1022 
1023 		if (req->opcode == IORING_OP_URING_CMD) {
1024 			struct io_uring_cmd *ioucmd;
1025 
1026 			ioucmd = io_kiocb_to_cmd(req, struct io_uring_cmd);
1027 			ret = file->f_op->uring_cmd_iopoll(ioucmd, &iob,
1028 								poll_flags);
1029 		} else {
1030 			struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw);
1031 
1032 			ret = file->f_op->iopoll(&rw->kiocb, &iob, poll_flags);
1033 		}
1034 		if (unlikely(ret < 0))
1035 			return ret;
1036 		else if (ret)
1037 			poll_flags |= BLK_POLL_ONESHOT;
1038 
1039 		/* iopoll may have completed current req */
1040 		if (!rq_list_empty(iob.req_list) ||
1041 		    READ_ONCE(req->iopoll_completed))
1042 			break;
1043 	}
1044 
1045 	if (!rq_list_empty(iob.req_list))
1046 		iob.complete(&iob);
1047 	else if (!pos)
1048 		return 0;
1049 
1050 	prev = start;
1051 	wq_list_for_each_resume(pos, prev) {
1052 		struct io_kiocb *req = container_of(pos, struct io_kiocb, comp_list);
1053 
1054 		/* order with io_complete_rw_iopoll(), e.g. ->result updates */
1055 		if (!smp_load_acquire(&req->iopoll_completed))
1056 			break;
1057 		nr_events++;
1058 		if (unlikely(req->flags & REQ_F_CQE_SKIP))
1059 			continue;
1060 
1061 		req->cqe.flags = io_put_kbuf(req, 0);
1062 		if (unlikely(!__io_fill_cqe_req(ctx, req))) {
1063 			spin_lock(&ctx->completion_lock);
1064 			io_req_cqe_overflow(req);
1065 			spin_unlock(&ctx->completion_lock);
1066 		}
1067 	}
1068 
1069 	if (unlikely(!nr_events))
1070 		return 0;
1071 
1072 	io_commit_cqring(ctx);
1073 	io_cqring_ev_posted_iopoll(ctx);
1074 	pos = start ? start->next : ctx->iopoll_list.first;
1075 	wq_list_cut(&ctx->iopoll_list, prev, start);
1076 	io_free_batch_list(ctx, pos);
1077 	return nr_events;
1078 }
1079