Linux Audio

Check our new training course

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