| // SPDX-License-Identifier: GPL-2.0 |
| /* |
| * Shared application/kernel submission and completion ring pairs, for |
| * supporting fast/efficient IO. |
| * |
| * A note on the read/write ordering memory barriers that are matched between |
| * the application and kernel side. |
| * |
| * After the application reads the CQ ring tail, it must use an |
| * appropriate smp_rmb() to pair with the smp_wmb() the kernel uses |
| * before writing the tail (using smp_load_acquire to read the tail will |
| * do). It also needs a smp_mb() before updating CQ head (ordering the |
| * entry load(s) with the head store), pairing with an implicit barrier |
| * through a control-dependency in io_get_cqring (smp_store_release to |
| * store head will do). Failure to do so could lead to reading invalid |
| * CQ entries. |
| * |
| * Likewise, the application must use an appropriate smp_wmb() before |
| * writing the SQ tail (ordering SQ entry stores with the tail store), |
| * which pairs with smp_load_acquire in io_get_sqring (smp_store_release |
| * to store the tail will do). And it needs a barrier ordering the SQ |
| * head load before writing new SQ entries (smp_load_acquire to read |
| * head will do). |
| * |
| * When using the SQ poll thread (IORING_SETUP_SQPOLL), the application |
| * needs to check the SQ flags for IORING_SQ_NEED_WAKEUP *after* |
| * updating the SQ tail; a full memory barrier smp_mb() is needed |
| * between. |
| * |
| * Also see the examples in the liburing library: |
| * |
| * git://git.kernel.dk/liburing |
| * |
| * io_uring also uses READ/WRITE_ONCE() for _any_ store or load that happens |
| * from data shared between the kernel and application. This is done both |
| * for ordering purposes, but also to ensure that once a value is loaded from |
| * data that the application could potentially modify, it remains stable. |
| * |
| * Copyright (C) 2018-2019 Jens Axboe |
| * Copyright (c) 2018-2019 Christoph Hellwig |
| */ |
| #include <linux/kernel.h> |
| #include <linux/init.h> |
| #include <linux/errno.h> |
| #include <linux/syscalls.h> |
| #include <linux/compat.h> |
| #include <net/compat.h> |
| #include <linux/refcount.h> |
| #include <linux/uio.h> |
| #include <linux/bits.h> |
| |
| #include <linux/sched/signal.h> |
| #include <linux/fs.h> |
| #include <linux/file.h> |
| #include <linux/fdtable.h> |
| #include <linux/mm.h> |
| #include <linux/mman.h> |
| #include <linux/percpu.h> |
| #include <linux/slab.h> |
| #include <linux/kthread.h> |
| #include <linux/blkdev.h> |
| #include <linux/bvec.h> |
| #include <linux/net.h> |
| #include <net/sock.h> |
| #include <net/af_unix.h> |
| #include <net/scm.h> |
| #include <linux/anon_inodes.h> |
| #include <linux/sched/mm.h> |
| #include <linux/uaccess.h> |
| #include <linux/nospec.h> |
| #include <linux/sizes.h> |
| #include <linux/hugetlb.h> |
| #include <linux/highmem.h> |
| #include <linux/namei.h> |
| #include <linux/fsnotify.h> |
| #include <linux/fadvise.h> |
| #include <linux/eventpoll.h> |
| #include <linux/fs_struct.h> |
| #include <linux/splice.h> |
| #include <linux/task_work.h> |
| #include <linux/pagemap.h> |
| #include <linux/io_uring.h> |
| #include <linux/blk-cgroup.h> |
| #include <linux/audit.h> |
| |
| #define CREATE_TRACE_POINTS |
| #include <trace/events/io_uring.h> |
| |
| #include <uapi/linux/io_uring.h> |
| |
| #include "internal.h" |
| #include "io-wq.h" |
| |
| #define IORING_MAX_ENTRIES 32768 |
| #define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES) |
| |
| /* |
| * Shift of 9 is 512 entries, or exactly one page on 64-bit archs |
| */ |
| #define IORING_FILE_TABLE_SHIFT 9 |
| #define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT) |
| #define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1) |
| #define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE) |
| #define IORING_MAX_RESTRICTIONS (IORING_RESTRICTION_LAST + \ |
| IORING_REGISTER_LAST + IORING_OP_LAST) |
| |
| struct io_uring { |
| u32 head ____cacheline_aligned_in_smp; |
| u32 tail ____cacheline_aligned_in_smp; |
| }; |
| |
| /* |
| * This data is shared with the application through the mmap at offsets |
| * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING. |
| * |
| * The offsets to the member fields are published through struct |
| * io_sqring_offsets when calling io_uring_setup. |
| */ |
| struct io_rings { |
| /* |
| * Head and tail offsets into the ring; the offsets need to be |
| * masked to get valid indices. |
| * |
| * The kernel controls head of the sq ring and the tail of the cq ring, |
| * and the application controls tail of the sq ring and the head of the |
| * cq ring. |
| */ |
| struct io_uring sq, cq; |
| /* |
| * Bitmasks to apply to head and tail offsets (constant, equals |
| * ring_entries - 1) |
| */ |
| u32 sq_ring_mask, cq_ring_mask; |
| /* Ring sizes (constant, power of 2) */ |
| u32 sq_ring_entries, cq_ring_entries; |
| /* |
| * Number of invalid entries dropped by the kernel due to |
| * invalid index stored in array |
| * |
| * Written by the kernel, shouldn't be modified by the |
| * application (i.e. get number of "new events" by comparing to |
| * cached value). |
| * |
| * After a new SQ head value was read by the application this |
| * counter includes all submissions that were dropped reaching |
| * the new SQ head (and possibly more). |
| */ |
| u32 sq_dropped; |
| /* |
| * Runtime SQ flags |
| * |
| * Written by the kernel, shouldn't be modified by the |
| * application. |
| * |
| * The application needs a full memory barrier before checking |
| * for IORING_SQ_NEED_WAKEUP after updating the sq tail. |
| */ |
| u32 sq_flags; |
| /* |
| * Runtime CQ flags |
| * |
| * Written by the application, shouldn't be modified by the |
| * kernel. |
| */ |
| u32 cq_flags; |
| /* |
| * Number of completion events lost because the queue was full; |
| * this should be avoided by the application by making sure |
| * there are not more requests pending than there is space in |
| * the completion queue. |
| * |
| * Written by the kernel, shouldn't be modified by the |
| * application (i.e. get number of "new events" by comparing to |
| * cached value). |
| * |
| * As completion events come in out of order this counter is not |
| * ordered with any other data. |
| */ |
| u32 cq_overflow; |
| /* |
| * Ring buffer of completion events. |
| * |
| * The kernel writes completion events fresh every time they are |
| * produced, so the application is allowed to modify pending |
| * entries. |
| */ |
| struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp; |
| }; |
| |
| struct io_mapped_ubuf { |
| u64 ubuf; |
| size_t len; |
| struct bio_vec *bvec; |
| unsigned int nr_bvecs; |
| unsigned long acct_pages; |
| }; |
| |
| struct fixed_file_table { |
| struct file **files; |
| }; |
| |
| struct fixed_file_ref_node { |
| struct percpu_ref refs; |
| struct list_head node; |
| struct list_head file_list; |
| struct fixed_file_data *file_data; |
| struct llist_node llist; |
| bool done; |
| }; |
| |
| struct fixed_file_data { |
| struct fixed_file_table *table; |
| struct io_ring_ctx *ctx; |
| |
| struct fixed_file_ref_node *node; |
| struct percpu_ref refs; |
| struct completion done; |
| struct list_head ref_list; |
| spinlock_t lock; |
| }; |
| |
| struct io_buffer { |
| struct list_head list; |
| __u64 addr; |
| __u32 len; |
| __u16 bid; |
| }; |
| |
| struct io_restriction { |
| DECLARE_BITMAP(register_op, IORING_REGISTER_LAST); |
| DECLARE_BITMAP(sqe_op, IORING_OP_LAST); |
| u8 sqe_flags_allowed; |
| u8 sqe_flags_required; |
| bool registered; |
| }; |
| |
| struct io_sq_data { |
| refcount_t refs; |
| struct mutex lock; |
| |
| /* ctx's that are using this sqd */ |
| struct list_head ctx_list; |
| struct list_head ctx_new_list; |
| struct mutex ctx_lock; |
| |
| struct task_struct *thread; |
| struct wait_queue_head wait; |
| }; |
| |
| struct io_ring_ctx { |
| struct { |
| struct percpu_ref refs; |
| } ____cacheline_aligned_in_smp; |
| |
| struct { |
| unsigned int flags; |
| unsigned int compat: 1; |
| unsigned int limit_mem: 1; |
| unsigned int cq_overflow_flushed: 1; |
| unsigned int drain_next: 1; |
| unsigned int eventfd_async: 1; |
| unsigned int restricted: 1; |
| unsigned int sqo_dead: 1; |
| |
| /* |
| * Ring buffer of indices into array of io_uring_sqe, which is |
| * mmapped by the application using the IORING_OFF_SQES offset. |
| * |
| * This indirection could e.g. be used to assign fixed |
| * io_uring_sqe entries to operations and only submit them to |
| * the queue when needed. |
| * |
| * The kernel modifies neither the indices array nor the entries |
| * array. |
| */ |
| u32 *sq_array; |
| unsigned cached_sq_head; |
| unsigned sq_entries; |
| unsigned sq_mask; |
| unsigned sq_thread_idle; |
| unsigned cached_sq_dropped; |
| unsigned cached_cq_overflow; |
| unsigned long sq_check_overflow; |
| |
| struct list_head defer_list; |
| struct list_head timeout_list; |
| struct list_head cq_overflow_list; |
| |
| struct io_uring_sqe *sq_sqes; |
| } ____cacheline_aligned_in_smp; |
| |
| struct io_rings *rings; |
| |
| /* IO offload */ |
| struct io_wq *io_wq; |
| |
| /* |
| * For SQPOLL usage - we hold a reference to the parent task, so we |
| * have access to the ->files |
| */ |
| struct task_struct *sqo_task; |
| |
| /* Only used for accounting purposes */ |
| struct mm_struct *mm_account; |
| |
| #ifdef CONFIG_BLK_CGROUP |
| struct cgroup_subsys_state *sqo_blkcg_css; |
| #endif |
| |
| struct io_sq_data *sq_data; /* if using sq thread polling */ |
| |
| struct wait_queue_head sqo_sq_wait; |
| struct wait_queue_entry sqo_wait_entry; |
| struct list_head sqd_list; |
| |
| /* |
| * If used, fixed file set. Writers must ensure that ->refs is dead, |
| * readers must ensure that ->refs is alive as long as the file* is |
| * used. Only updated through io_uring_register(2). |
| */ |
| struct fixed_file_data *file_data; |
| unsigned nr_user_files; |
| |
| /* if used, fixed mapped user buffers */ |
| unsigned nr_user_bufs; |
| struct io_mapped_ubuf *user_bufs; |
| |
| struct user_struct *user; |
| |
| const struct cred *creds; |
| |
| #ifdef CONFIG_AUDIT |
| kuid_t loginuid; |
| unsigned int sessionid; |
| #endif |
| |
| struct completion ref_comp; |
| struct completion sq_thread_comp; |
| |
| /* if all else fails... */ |
| struct io_kiocb *fallback_req; |
| |
| #if defined(CONFIG_UNIX) |
| struct socket *ring_sock; |
| #endif |
| |
| struct xarray io_buffers; |
| |
| struct xarray personalities; |
| u32 pers_next; |
| |
| struct { |
| unsigned cached_cq_tail; |
| unsigned cq_entries; |
| unsigned cq_mask; |
| atomic_t cq_timeouts; |
| unsigned cq_last_tm_flush; |
| unsigned long cq_check_overflow; |
| struct wait_queue_head cq_wait; |
| struct fasync_struct *cq_fasync; |
| struct eventfd_ctx *cq_ev_fd; |
| } ____cacheline_aligned_in_smp; |
| |
| struct { |
| struct mutex uring_lock; |
| wait_queue_head_t wait; |
| } ____cacheline_aligned_in_smp; |
| |
| struct { |
| spinlock_t completion_lock; |
| |
| /* |
| * ->iopoll_list is protected by the ctx->uring_lock for |
| * io_uring instances that don't use IORING_SETUP_SQPOLL. |
| * For SQPOLL, only the single threaded io_sq_thread() will |
| * manipulate the list, hence no extra locking is needed there. |
| */ |
| struct list_head iopoll_list; |
| struct hlist_head *cancel_hash; |
| unsigned cancel_hash_bits; |
| bool poll_multi_file; |
| |
| spinlock_t inflight_lock; |
| struct list_head inflight_list; |
| } ____cacheline_aligned_in_smp; |
| |
| struct delayed_work file_put_work; |
| struct llist_head file_put_llist; |
| |
| struct work_struct exit_work; |
| struct io_restriction restrictions; |
| }; |
| |
| /* |
| * First field must be the file pointer in all the |
| * iocb unions! See also 'struct kiocb' in <linux/fs.h> |
| */ |
| struct io_poll_iocb { |
| struct file *file; |
| union { |
| struct wait_queue_head *head; |
| u64 addr; |
| }; |
| __poll_t events; |
| bool done; |
| bool canceled; |
| struct wait_queue_entry wait; |
| }; |
| |
| struct io_close { |
| struct file *file; |
| struct file *put_file; |
| int fd; |
| }; |
| |
| struct io_timeout_data { |
| struct io_kiocb *req; |
| struct hrtimer timer; |
| struct timespec64 ts; |
| enum hrtimer_mode mode; |
| }; |
| |
| struct io_accept { |
| struct file *file; |
| struct sockaddr __user *addr; |
| int __user *addr_len; |
| int flags; |
| unsigned long nofile; |
| }; |
| |
| struct io_sync { |
| struct file *file; |
| loff_t len; |
| loff_t off; |
| int flags; |
| int mode; |
| }; |
| |
| struct io_cancel { |
| struct file *file; |
| u64 addr; |
| }; |
| |
| struct io_timeout { |
| struct file *file; |
| u32 off; |
| u32 target_seq; |
| struct list_head list; |
| }; |
| |
| struct io_timeout_rem { |
| struct file *file; |
| u64 addr; |
| }; |
| |
| struct io_rw { |
| /* NOTE: kiocb has the file as the first member, so don't do it here */ |
| struct kiocb kiocb; |
| u64 addr; |
| u64 len; |
| }; |
| |
| struct io_connect { |
| struct file *file; |
| struct sockaddr __user *addr; |
| int addr_len; |
| }; |
| |
| struct io_sr_msg { |
| struct file *file; |
| union { |
| struct user_msghdr __user *umsg; |
| void __user *buf; |
| }; |
| int msg_flags; |
| int bgid; |
| size_t len; |
| struct io_buffer *kbuf; |
| }; |
| |
| struct io_open { |
| struct file *file; |
| int dfd; |
| bool ignore_nonblock; |
| struct filename *filename; |
| struct open_how how; |
| unsigned long nofile; |
| }; |
| |
| struct io_files_update { |
| struct file *file; |
| u64 arg; |
| u32 nr_args; |
| u32 offset; |
| }; |
| |
| struct io_fadvise { |
| struct file *file; |
| u64 offset; |
| u32 len; |
| u32 advice; |
| }; |
| |
| struct io_madvise { |
| struct file *file; |
| u64 addr; |
| u32 len; |
| u32 advice; |
| }; |
| |
| struct io_epoll { |
| struct file *file; |
| int epfd; |
| int op; |
| int fd; |
| struct epoll_event event; |
| }; |
| |
| struct io_splice { |
| struct file *file_out; |
| struct file *file_in; |
| loff_t off_out; |
| loff_t off_in; |
| u64 len; |
| unsigned int flags; |
| }; |
| |
| struct io_provide_buf { |
| struct file *file; |
| __u64 addr; |
| __u32 len; |
| __u32 bgid; |
| __u16 nbufs; |
| __u16 bid; |
| }; |
| |
| struct io_statx { |
| struct file *file; |
| int dfd; |
| unsigned int mask; |
| unsigned int flags; |
| const char __user *filename; |
| struct statx __user *buffer; |
| }; |
| |
| struct io_completion { |
| struct file *file; |
| struct list_head list; |
| u32 cflags; |
| }; |
| |
| struct io_async_connect { |
| struct sockaddr_storage address; |
| }; |
| |
| struct io_async_msghdr { |
| struct iovec fast_iov[UIO_FASTIOV]; |
| struct iovec *iov; |
| struct sockaddr __user *uaddr; |
| struct msghdr msg; |
| struct sockaddr_storage addr; |
| }; |
| |
| struct io_async_rw { |
| struct iovec fast_iov[UIO_FASTIOV]; |
| const struct iovec *free_iovec; |
| struct iov_iter iter; |
| size_t bytes_done; |
| struct wait_page_queue wpq; |
| }; |
| |
| enum { |
| REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT, |
| REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT, |
| REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT, |
| REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT, |
| REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT, |
| REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT, |
| |
| REQ_F_LINK_HEAD_BIT, |
| REQ_F_FAIL_LINK_BIT, |
| REQ_F_INFLIGHT_BIT, |
| REQ_F_CUR_POS_BIT, |
| REQ_F_NOWAIT_BIT, |
| REQ_F_LINK_TIMEOUT_BIT, |
| REQ_F_ISREG_BIT, |
| REQ_F_NEED_CLEANUP_BIT, |
| REQ_F_POLLED_BIT, |
| REQ_F_BUFFER_SELECTED_BIT, |
| REQ_F_NO_FILE_TABLE_BIT, |
| REQ_F_WORK_INITIALIZED_BIT, |
| REQ_F_LTIMEOUT_ACTIVE_BIT, |
| |
| /* not a real bit, just to check we're not overflowing the space */ |
| __REQ_F_LAST_BIT, |
| }; |
| |
| enum { |
| /* ctx owns file */ |
| REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT), |
| /* drain existing IO first */ |
| REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT), |
| /* linked sqes */ |
| REQ_F_LINK = BIT(REQ_F_LINK_BIT), |
| /* doesn't sever on completion < 0 */ |
| REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT), |
| /* IOSQE_ASYNC */ |
| REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT), |
| /* IOSQE_BUFFER_SELECT */ |
| REQ_F_BUFFER_SELECT = BIT(REQ_F_BUFFER_SELECT_BIT), |
| |
| /* head of a link */ |
| REQ_F_LINK_HEAD = BIT(REQ_F_LINK_HEAD_BIT), |
| /* fail rest of links */ |
| REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT), |
| /* on inflight list */ |
| REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT), |
| /* read/write uses file position */ |
| REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT), |
| /* must not punt to workers */ |
| REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT), |
| /* has or had linked timeout */ |
| REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT), |
| /* regular file */ |
| REQ_F_ISREG = BIT(REQ_F_ISREG_BIT), |
| /* needs cleanup */ |
| REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT), |
| /* already went through poll handler */ |
| REQ_F_POLLED = BIT(REQ_F_POLLED_BIT), |
| /* buffer already selected */ |
| REQ_F_BUFFER_SELECTED = BIT(REQ_F_BUFFER_SELECTED_BIT), |
| /* doesn't need file table for this request */ |
| REQ_F_NO_FILE_TABLE = BIT(REQ_F_NO_FILE_TABLE_BIT), |
| /* io_wq_work is initialized */ |
| REQ_F_WORK_INITIALIZED = BIT(REQ_F_WORK_INITIALIZED_BIT), |
| /* linked timeout is active, i.e. prepared by link's head */ |
| REQ_F_LTIMEOUT_ACTIVE = BIT(REQ_F_LTIMEOUT_ACTIVE_BIT), |
| }; |
| |
| struct async_poll { |
| struct io_poll_iocb poll; |
| struct io_poll_iocb *double_poll; |
| }; |
| |
| /* |
| * NOTE! Each of the iocb union members has the file pointer |
| * as the first entry in their struct definition. So you can |
| * access the file pointer through any of the sub-structs, |
| * or directly as just 'ki_filp' in this struct. |
| */ |
| struct io_kiocb { |
| union { |
| struct file *file; |
| struct io_rw rw; |
| struct io_poll_iocb poll; |
| struct io_accept accept; |
| struct io_sync sync; |
| struct io_cancel cancel; |
| struct io_timeout timeout; |
| struct io_timeout_rem timeout_rem; |
| struct io_connect connect; |
| struct io_sr_msg sr_msg; |
| struct io_open open; |
| struct io_close close; |
| struct io_files_update files_update; |
| struct io_fadvise fadvise; |
| struct io_madvise madvise; |
| struct io_epoll epoll; |
| struct io_splice splice; |
| struct io_provide_buf pbuf; |
| struct io_statx statx; |
| /* use only after cleaning per-op data, see io_clean_op() */ |
| struct io_completion compl; |
| }; |
| |
| /* opcode allocated if it needs to store data for async defer */ |
| void *async_data; |
| u8 opcode; |
| /* polled IO has completed */ |
| u8 iopoll_completed; |
| |
| u16 buf_index; |
| u32 result; |
| |
| struct io_ring_ctx *ctx; |
| unsigned int flags; |
| refcount_t refs; |
| struct task_struct *task; |
| u64 user_data; |
| |
| struct list_head link_list; |
| |
| /* |
| * 1. used with ctx->iopoll_list with reads/writes |
| * 2. to track reqs with ->files (see io_op_def::file_table) |
| */ |
| struct list_head inflight_entry; |
| |
| struct percpu_ref *fixed_file_refs; |
| struct callback_head task_work; |
| /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */ |
| struct hlist_node hash_node; |
| struct async_poll *apoll; |
| struct io_wq_work work; |
| }; |
| |
| struct io_defer_entry { |
| struct list_head list; |
| struct io_kiocb *req; |
| u32 seq; |
| }; |
| |
| #define IO_IOPOLL_BATCH 8 |
| |
| struct io_comp_state { |
| unsigned int nr; |
| struct list_head list; |
| struct io_ring_ctx *ctx; |
| }; |
| |
| struct io_submit_state { |
| struct blk_plug plug; |
| |
| /* |
| * io_kiocb alloc cache |
| */ |
| void *reqs[IO_IOPOLL_BATCH]; |
| unsigned int free_reqs; |
| |
| /* |
| * Batch completion logic |
| */ |
| struct io_comp_state comp; |
| |
| /* |
| * File reference cache |
| */ |
| struct file *file; |
| unsigned int fd; |
| unsigned int has_refs; |
| unsigned int ios_left; |
| }; |
| |
| struct io_op_def { |
| /* needs req->file assigned */ |
| unsigned needs_file : 1; |
| /* don't fail if file grab fails */ |
| unsigned needs_file_no_error : 1; |
| /* hash wq insertion if file is a regular file */ |
| unsigned hash_reg_file : 1; |
| /* unbound wq insertion if file is a non-regular file */ |
| unsigned unbound_nonreg_file : 1; |
| /* opcode is not supported by this kernel */ |
| unsigned not_supported : 1; |
| /* set if opcode supports polled "wait" */ |
| unsigned pollin : 1; |
| unsigned pollout : 1; |
| /* op supports buffer selection */ |
| unsigned buffer_select : 1; |
| /* must always have async data allocated */ |
| unsigned needs_async_data : 1; |
| /* size of async data needed, if any */ |
| unsigned short async_size; |
| unsigned work_flags; |
| }; |
| |
| static const struct io_op_def io_op_defs[] = { |
| [IORING_OP_NOP] = {}, |
| [IORING_OP_READV] = { |
| .needs_file = 1, |
| .unbound_nonreg_file = 1, |
| .pollin = 1, |
| .buffer_select = 1, |
| .needs_async_data = 1, |
| .async_size = sizeof(struct io_async_rw), |
| .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG, |
| }, |
| [IORING_OP_WRITEV] = { |
| .needs_file = 1, |
| .hash_reg_file = 1, |
| .unbound_nonreg_file = 1, |
| .pollout = 1, |
| .needs_async_data = 1, |
| .async_size = sizeof(struct io_async_rw), |
| .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG | |
| IO_WQ_WORK_FSIZE, |
| }, |
| [IORING_OP_FSYNC] = { |
| .needs_file = 1, |
| .work_flags = IO_WQ_WORK_BLKCG, |
| }, |
| [IORING_OP_READ_FIXED] = { |
| .needs_file = 1, |
| .unbound_nonreg_file = 1, |
| .pollin = 1, |
| .async_size = sizeof(struct io_async_rw), |
| .work_flags = IO_WQ_WORK_BLKCG | IO_WQ_WORK_MM, |
| }, |
| [IORING_OP_WRITE_FIXED] = { |
| .needs_file = 1, |
| .hash_reg_file = 1, |
| .unbound_nonreg_file = 1, |
| .pollout = 1, |
| .async_size = sizeof(struct io_async_rw), |
| .work_flags = IO_WQ_WORK_BLKCG | IO_WQ_WORK_FSIZE | |
| IO_WQ_WORK_MM, |
| }, |
| [IORING_OP_POLL_ADD] = { |
| .needs_file = 1, |
| .unbound_nonreg_file = 1, |
| }, |
| [IORING_OP_POLL_REMOVE] = {}, |
| [IORING_OP_SYNC_FILE_RANGE] = { |
| .needs_file = 1, |
| .work_flags = IO_WQ_WORK_BLKCG, |
| }, |
| [IORING_OP_SENDMSG] = { |
| .needs_file = 1, |
| .unbound_nonreg_file = 1, |
| .pollout = 1, |
| .needs_async_data = 1, |
| .async_size = sizeof(struct io_async_msghdr), |
| .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG | |
| IO_WQ_WORK_FS, |
| }, |
| [IORING_OP_RECVMSG] = { |
| .needs_file = 1, |
| .unbound_nonreg_file = 1, |
| .pollin = 1, |
| .buffer_select = 1, |
| .needs_async_data = 1, |
| .async_size = sizeof(struct io_async_msghdr), |
| .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG | |
| IO_WQ_WORK_FS, |
| }, |
| [IORING_OP_TIMEOUT] = { |
| .needs_async_data = 1, |
| .async_size = sizeof(struct io_timeout_data), |
| .work_flags = IO_WQ_WORK_MM, |
| }, |
| [IORING_OP_TIMEOUT_REMOVE] = {}, |
| [IORING_OP_ACCEPT] = { |
| .needs_file = 1, |
| .unbound_nonreg_file = 1, |
| .pollin = 1, |
| .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_FILES, |
| }, |
| [IORING_OP_ASYNC_CANCEL] = {}, |
| [IORING_OP_LINK_TIMEOUT] = { |
| .needs_async_data = 1, |
| .async_size = sizeof(struct io_timeout_data), |
| .work_flags = IO_WQ_WORK_MM, |
| }, |
| [IORING_OP_CONNECT] = { |
| .needs_file = 1, |
| .unbound_nonreg_file = 1, |
| .pollout = 1, |
| .needs_async_data = 1, |
| .async_size = sizeof(struct io_async_connect), |
| .work_flags = IO_WQ_WORK_MM, |
| }, |
| [IORING_OP_FALLOCATE] = { |
| .needs_file = 1, |
| .work_flags = IO_WQ_WORK_BLKCG | IO_WQ_WORK_FSIZE, |
| }, |
| [IORING_OP_OPENAT] = { |
| .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_BLKCG | |
| IO_WQ_WORK_FS, |
| }, |
| [IORING_OP_CLOSE] = { |
| .needs_file = 1, |
| .needs_file_no_error = 1, |
| .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_BLKCG, |
| }, |
| [IORING_OP_FILES_UPDATE] = { |
| .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_MM, |
| }, |
| [IORING_OP_STATX] = { |
| .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_MM | |
| IO_WQ_WORK_FS | IO_WQ_WORK_BLKCG, |
| }, |
| [IORING_OP_READ] = { |
| .needs_file = 1, |
| .unbound_nonreg_file = 1, |
| .pollin = 1, |
| .buffer_select = 1, |
| .async_size = sizeof(struct io_async_rw), |
| .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG, |
| }, |
| [IORING_OP_WRITE] = { |
| .needs_file = 1, |
| .hash_reg_file = 1, |
| .unbound_nonreg_file = 1, |
| .pollout = 1, |
| .async_size = sizeof(struct io_async_rw), |
| .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG | |
| IO_WQ_WORK_FSIZE, |
| }, |
| [IORING_OP_FADVISE] = { |
| .needs_file = 1, |
| .work_flags = IO_WQ_WORK_BLKCG, |
| }, |
| [IORING_OP_MADVISE] = { |
| .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG, |
| }, |
| [IORING_OP_SEND] = { |
| .needs_file = 1, |
| .unbound_nonreg_file = 1, |
| .pollout = 1, |
| .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG, |
| }, |
| [IORING_OP_RECV] = { |
| .needs_file = 1, |
| .unbound_nonreg_file = 1, |
| .pollin = 1, |
| .buffer_select = 1, |
| .work_flags = IO_WQ_WORK_MM | IO_WQ_WORK_BLKCG, |
| }, |
| [IORING_OP_OPENAT2] = { |
| .work_flags = IO_WQ_WORK_FILES | IO_WQ_WORK_FS | |
| IO_WQ_WORK_BLKCG, |
| }, |
| [IORING_OP_EPOLL_CTL] = { |
| .unbound_nonreg_file = 1, |
| .work_flags = IO_WQ_WORK_FILES, |
| }, |
| [IORING_OP_SPLICE] = { |
| .needs_file = 1, |
| .hash_reg_file = 1, |
| .unbound_nonreg_file = 1, |
| .work_flags = IO_WQ_WORK_BLKCG, |
| }, |
| [IORING_OP_PROVIDE_BUFFERS] = {}, |
| [IORING_OP_REMOVE_BUFFERS] = {}, |
| [IORING_OP_TEE] = { |
| .needs_file = 1, |
| .hash_reg_file = 1, |
| .unbound_nonreg_file = 1, |
| }, |
| }; |
| |
| enum io_mem_account { |
| ACCT_LOCKED, |
| ACCT_PINNED, |
| }; |
| |
| static void destroy_fixed_file_ref_node(struct fixed_file_ref_node *ref_node); |
| static struct fixed_file_ref_node *alloc_fixed_file_ref_node( |
| struct io_ring_ctx *ctx); |
| |
| static void __io_complete_rw(struct io_kiocb *req, long res, long res2, |
| struct io_comp_state *cs); |
| static void io_cqring_fill_event(struct io_kiocb *req, long res); |
| static void io_put_req(struct io_kiocb *req); |
| static void io_put_req_deferred(struct io_kiocb *req, int nr); |
| static void io_double_put_req(struct io_kiocb *req); |
| static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req); |
| static void __io_queue_linked_timeout(struct io_kiocb *req); |
| static void io_queue_linked_timeout(struct io_kiocb *req); |
| static int __io_sqe_files_update(struct io_ring_ctx *ctx, |
| struct io_uring_files_update *ip, |
| unsigned nr_args); |
| static void __io_clean_op(struct io_kiocb *req); |
| static struct file *io_file_get(struct io_submit_state *state, |
| struct io_kiocb *req, int fd, bool fixed); |
| static void __io_queue_sqe(struct io_kiocb *req, struct io_comp_state *cs); |
| static void io_file_put_work(struct work_struct *work); |
| |
| static ssize_t io_import_iovec(int rw, struct io_kiocb *req, |
| struct iovec **iovec, struct iov_iter *iter, |
| bool needs_lock); |
| static int io_setup_async_rw(struct io_kiocb *req, const struct iovec *iovec, |
| const struct iovec *fast_iov, |
| struct iov_iter *iter, bool force); |
| static void io_req_drop_files(struct io_kiocb *req); |
| static void io_req_task_queue(struct io_kiocb *req); |
| |
| static struct kmem_cache *req_cachep; |
| |
| static const struct file_operations io_uring_fops; |
| |
| struct sock *io_uring_get_socket(struct file *file) |
| { |
| #if defined(CONFIG_UNIX) |
| if (file->f_op == &io_uring_fops) { |
| struct io_ring_ctx *ctx = file->private_data; |
| |
| return ctx->ring_sock->sk; |
| } |
| #endif |
| return NULL; |
| } |
| EXPORT_SYMBOL(io_uring_get_socket); |
| |
| static inline void io_clean_op(struct io_kiocb *req) |
| { |
| if (req->flags & (REQ_F_NEED_CLEANUP | REQ_F_BUFFER_SELECTED)) |
| __io_clean_op(req); |
| } |
| |
| static inline bool __io_match_files(struct io_kiocb *req, |
| struct files_struct *files) |
| { |
| if (req->file && req->file->f_op == &io_uring_fops) |
| return true; |
| |
| return ((req->flags & REQ_F_WORK_INITIALIZED) && |
| (req->work.flags & IO_WQ_WORK_FILES)) && |
| req->work.identity->files == files; |
| } |
| |
| static void io_refs_resurrect(struct percpu_ref *ref, struct completion *compl) |
| { |
| bool got = percpu_ref_tryget(ref); |
| |
| /* already at zero, wait for ->release() */ |
| if (!got) |
| wait_for_completion(compl); |
| percpu_ref_resurrect(ref); |
| if (got) |
| percpu_ref_put(ref); |
| } |
| |
| static bool io_match_task(struct io_kiocb *head, |
| struct task_struct *task, |
| struct files_struct *files) |
| { |
| struct io_kiocb *link; |
| |
| if (task && head->task != task) { |
| /* in terms of cancelation, always match if req task is dead */ |
| if (head->task->flags & PF_EXITING) |
| return true; |
| return false; |
| } |
| if (!files) |
| return true; |
| if (__io_match_files(head, files)) |
| return true; |
| if (head->flags & REQ_F_LINK_HEAD) { |
| list_for_each_entry(link, &head->link_list, link_list) { |
| if (__io_match_files(link, files)) |
| return true; |
| } |
| } |
| return false; |
| } |
| |
| |
| static void io_sq_thread_drop_mm(void) |
| { |
| struct mm_struct *mm = current->mm; |
| |
| if (mm) { |
| kthread_unuse_mm(mm); |
| mmput(mm); |
| current->mm = NULL; |
| } |
| } |
| |
| static int __io_sq_thread_acquire_mm(struct io_ring_ctx *ctx) |
| { |
| struct mm_struct *mm; |
| |
| if (current->flags & PF_EXITING) |
| return -EFAULT; |
| if (current->mm) |
| return 0; |
| |
| /* Should never happen */ |
| if (unlikely(!(ctx->flags & IORING_SETUP_SQPOLL))) |
| return -EFAULT; |
| |
| task_lock(ctx->sqo_task); |
| mm = ctx->sqo_task->mm; |
| if (unlikely(!mm || !mmget_not_zero(mm))) |
| mm = NULL; |
| task_unlock(ctx->sqo_task); |
| |
| if (mm) { |
| kthread_use_mm(mm); |
| return 0; |
| } |
| |
| return -EFAULT; |
| } |
| |
| static int io_sq_thread_acquire_mm(struct io_ring_ctx *ctx, |
| struct io_kiocb *req) |
| { |
| if (!(io_op_defs[req->opcode].work_flags & IO_WQ_WORK_MM)) |
| return 0; |
| return __io_sq_thread_acquire_mm(ctx); |
| } |
| |
| static void io_sq_thread_associate_blkcg(struct io_ring_ctx *ctx, |
| struct cgroup_subsys_state **cur_css) |
| |
| { |
| #ifdef CONFIG_BLK_CGROUP |
| /* puts the old one when swapping */ |
| if (*cur_css != ctx->sqo_blkcg_css) { |
| kthread_associate_blkcg(ctx->sqo_blkcg_css); |
| *cur_css = ctx->sqo_blkcg_css; |
| } |
| #endif |
| } |
| |
| static void io_sq_thread_unassociate_blkcg(void) |
| { |
| #ifdef CONFIG_BLK_CGROUP |
| kthread_associate_blkcg(NULL); |
| #endif |
| } |
| |
| static inline void req_set_fail_links(struct io_kiocb *req) |
| { |
| if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK) |
| req->flags |= REQ_F_FAIL_LINK; |
| } |
| |
| /* |
| * None of these are dereferenced, they are simply used to check if any of |
| * them have changed. If we're under current and check they are still the |
| * same, we're fine to grab references to them for actual out-of-line use. |
| */ |
| static void io_init_identity(struct io_identity *id) |
| { |
| id->files = current->files; |
| id->mm = current->mm; |
| #ifdef CONFIG_BLK_CGROUP |
| rcu_read_lock(); |
| id->blkcg_css = blkcg_css(); |
| rcu_read_unlock(); |
| #endif |
| id->creds = current_cred(); |
| id->nsproxy = current->nsproxy; |
| id->fs = current->fs; |
| id->fsize = rlimit(RLIMIT_FSIZE); |
| #ifdef CONFIG_AUDIT |
| id->loginuid = current->loginuid; |
| id->sessionid = current->sessionid; |
| #endif |
| refcount_set(&id->count, 1); |
| } |
| |
| static inline void __io_req_init_async(struct io_kiocb *req) |
| { |
| memset(&req->work, 0, sizeof(req->work)); |
| req->flags |= REQ_F_WORK_INITIALIZED; |
| } |
| |
| /* |
| * Note: must call io_req_init_async() for the first time you |
| * touch any members of io_wq_work. |
| */ |
| static inline void io_req_init_async(struct io_kiocb *req) |
| { |
| struct io_uring_task *tctx = current->io_uring; |
| |
| if (req->flags & REQ_F_WORK_INITIALIZED) |
| return; |
| |
| __io_req_init_async(req); |
| |
| /* Grab a ref if this isn't our static identity */ |
| req->work.identity = tctx->identity; |
| if (tctx->identity != &tctx->__identity) |
| refcount_inc(&req->work.identity->count); |
| } |
| |
| static inline bool io_async_submit(struct io_ring_ctx *ctx) |
| { |
| return ctx->flags & IORING_SETUP_SQPOLL; |
| } |
| |
| static void io_ring_ctx_ref_free(struct percpu_ref *ref) |
| { |
| struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs); |
| |
| complete(&ctx->ref_comp); |
| } |
| |
| static inline bool io_is_timeout_noseq(struct io_kiocb *req) |
| { |
| return !req->timeout.off; |
| } |
| |
| static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) |
| { |
| struct io_ring_ctx *ctx; |
| int hash_bits; |
| |
| ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); |
| if (!ctx) |
| return NULL; |
| |
| ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL); |
| if (!ctx->fallback_req) |
| goto err; |
| |
| /* |
| * Use 5 bits less than the max cq entries, that should give us around |
| * 32 entries per hash list if totally full and uniformly spread. |
| */ |
| hash_bits = ilog2(p->cq_entries); |
| hash_bits -= 5; |
| if (hash_bits <= 0) |
| hash_bits = 1; |
| ctx->cancel_hash_bits = hash_bits; |
| ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head), |
| GFP_KERNEL); |
| if (!ctx->cancel_hash) |
| goto err; |
| __hash_init(ctx->cancel_hash, 1U << hash_bits); |
| |
| if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free, |
| PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) |
| goto err; |
| |
| ctx->flags = p->flags; |
| init_waitqueue_head(&ctx->sqo_sq_wait); |
| INIT_LIST_HEAD(&ctx->sqd_list); |
| init_waitqueue_head(&ctx->cq_wait); |
| INIT_LIST_HEAD(&ctx->cq_overflow_list); |
| init_completion(&ctx->ref_comp); |
| init_completion(&ctx->sq_thread_comp); |
| xa_init_flags(&ctx->io_buffers, XA_FLAGS_ALLOC1); |
| xa_init_flags(&ctx->personalities, XA_FLAGS_ALLOC1); |
| mutex_init(&ctx->uring_lock); |
| init_waitqueue_head(&ctx->wait); |
| spin_lock_init(&ctx->completion_lock); |
| INIT_LIST_HEAD(&ctx->iopoll_list); |
| INIT_LIST_HEAD(&ctx->defer_list); |
| INIT_LIST_HEAD(&ctx->timeout_list); |
| spin_lock_init(&ctx->inflight_lock); |
| INIT_LIST_HEAD(&ctx->inflight_list); |
| INIT_DELAYED_WORK(&ctx->file_put_work, io_file_put_work); |
| init_llist_head(&ctx->file_put_llist); |
| return ctx; |
| err: |
| if (ctx->fallback_req) |
| kmem_cache_free(req_cachep, ctx->fallback_req); |
| kfree(ctx->cancel_hash); |
| kfree(ctx); |
| return NULL; |
| } |
| |
| static bool req_need_defer(struct io_kiocb *req, u32 seq) |
| { |
| if (unlikely(req->flags & REQ_F_IO_DRAIN)) { |
| struct io_ring_ctx *ctx = req->ctx; |
| |
| return seq != ctx->cached_cq_tail |
| + READ_ONCE(ctx->cached_cq_overflow); |
| } |
| |
| return false; |
| } |
| |
| static void __io_commit_cqring(struct io_ring_ctx *ctx) |
| { |
| struct io_rings *rings = ctx->rings; |
| |
| /* order cqe stores with ring update */ |
| smp_store_release(&rings->cq.tail, ctx->cached_cq_tail); |
| } |
| |
| static void io_put_identity(struct io_uring_task *tctx, struct io_kiocb *req) |
| { |
| if (req->work.identity == &tctx->__identity) |
| return; |
| if (refcount_dec_and_test(&req->work.identity->count)) |
| kfree(req->work.identity); |
| } |
| |
| static void io_req_clean_work(struct io_kiocb *req) |
| { |
| if (!(req->flags & REQ_F_WORK_INITIALIZED)) |
| return; |
| |
| req->flags &= ~REQ_F_WORK_INITIALIZED; |
| |
| if (req->work.flags & IO_WQ_WORK_MM) { |
| mmdrop(req->work.identity->mm); |
| req->work.flags &= ~IO_WQ_WORK_MM; |
| } |
| #ifdef CONFIG_BLK_CGROUP |
| if (req->work.flags & IO_WQ_WORK_BLKCG) { |
| css_put(req->work.identity->blkcg_css); |
| req->work.flags &= ~IO_WQ_WORK_BLKCG; |
| } |
| #endif |
| if (req->work.flags & IO_WQ_WORK_CREDS) { |
| put_cred(req->work.identity->creds); |
| req->work.flags &= ~IO_WQ_WORK_CREDS; |
| } |
| if (req->work.flags & IO_WQ_WORK_FS) { |
| struct fs_struct *fs = req->work.identity->fs; |
| |
| spin_lock(&req->work.identity->fs->lock); |
| if (--fs->users) |
| fs = NULL; |
| spin_unlock(&req->work.identity->fs->lock); |
| if (fs) |
| free_fs_struct(fs); |
| req->work.flags &= ~IO_WQ_WORK_FS; |
| } |
| if (req->flags & REQ_F_INFLIGHT) |
| io_req_drop_files(req); |
| |
| io_put_identity(req->task->io_uring, req); |
| } |
| |
| /* |
| * Create a private copy of io_identity, since some fields don't match |
| * the current context. |
| */ |
| static bool io_identity_cow(struct io_kiocb *req) |
| { |
| struct io_uring_task *tctx = current->io_uring; |
| const struct cred *creds = NULL; |
| struct io_identity *id; |
| |
| if (req->work.flags & IO_WQ_WORK_CREDS) |
| creds = req->work.identity->creds; |
| |
| id = kmemdup(req->work.identity, sizeof(*id), GFP_KERNEL); |
| if (unlikely(!id)) { |
| req->work.flags |= IO_WQ_WORK_CANCEL; |
| return false; |
| } |
| |
| /* |
| * We can safely just re-init the creds we copied Either the field |
| * matches the current one, or we haven't grabbed it yet. The only |
| * exception is ->creds, through registered personalities, so handle |
| * that one separately. |
| */ |
| io_init_identity(id); |
| if (creds) |
| id->creds = creds; |
| |
| /* add one for this request */ |
| refcount_inc(&id->count); |
| |
| /* drop tctx and req identity references, if needed */ |
| if (tctx->identity != &tctx->__identity && |
| refcount_dec_and_test(&tctx->identity->count)) |
| kfree(tctx->identity); |
| if (req->work.identity != &tctx->__identity && |
| refcount_dec_and_test(&req->work.identity->count)) |
| kfree(req->work.identity); |
| |
| req->work.identity = id; |
| tctx->identity = id; |
| return true; |
| } |
| |
| static bool io_grab_identity(struct io_kiocb *req) |
| { |
| const struct io_op_def *def = &io_op_defs[req->opcode]; |
| struct io_identity *id = req->work.identity; |
| struct io_ring_ctx *ctx = req->ctx; |
| |
| if (def->work_flags & IO_WQ_WORK_FSIZE) { |
| if (id->fsize != rlimit(RLIMIT_FSIZE)) |
| return false; |
| req->work.flags |= IO_WQ_WORK_FSIZE; |
| } |
| #ifdef CONFIG_BLK_CGROUP |
| if (!(req->work.flags & IO_WQ_WORK_BLKCG) && |
| (def->work_flags & IO_WQ_WORK_BLKCG)) { |
| rcu_read_lock(); |
| if (id->blkcg_css != blkcg_css()) { |
| rcu_read_unlock(); |
| return false; |
| } |
| /* |
| * This should be rare, either the cgroup is dying or the task |
| * is moving cgroups. Just punt to root for the handful of ios. |
| */ |
| if (css_tryget_online(id->blkcg_css)) |
| req->work.flags |= IO_WQ_WORK_BLKCG; |
| rcu_read_unlock(); |
| } |
| #endif |
| if (!(req->work.flags & IO_WQ_WORK_CREDS)) { |
| if (id->creds != current_cred()) |
| return false; |
| get_cred(id->creds); |
| req->work.flags |= IO_WQ_WORK_CREDS; |
| } |
| #ifdef CONFIG_AUDIT |
| if (!uid_eq(current->loginuid, id->loginuid) || |
| current->sessionid != id->sessionid) |
| return false; |
| #endif |
| if (!(req->work.flags & IO_WQ_WORK_FS) && |
| (def->work_flags & IO_WQ_WORK_FS)) { |
| if (current->fs != id->fs) |
| return false; |
| spin_lock(&id->fs->lock); |
| if (!id->fs->in_exec) { |
| id->fs->users++; |
| req->work.flags |= IO_WQ_WORK_FS; |
| } else { |
| req->work.flags |= IO_WQ_WORK_CANCEL; |
| } |
| spin_unlock(¤t->fs->lock); |
| } |
| if (!(req->work.flags & IO_WQ_WORK_FILES) && |
| (def->work_flags & IO_WQ_WORK_FILES) && |
| !(req->flags & REQ_F_NO_FILE_TABLE)) { |
| if (id->files != current->files || |
| id->nsproxy != current->nsproxy) |
| return false; |
| atomic_inc(&id->files->count); |
| get_nsproxy(id->nsproxy); |
| |
| if (!(req->flags & REQ_F_INFLIGHT)) { |
| req->flags |= REQ_F_INFLIGHT; |
| |
| spin_lock_irq(&ctx->inflight_lock); |
| list_add(&req->inflight_entry, &ctx->inflight_list); |
| spin_unlock_irq(&ctx->inflight_lock); |
| } |
| req->work.flags |= IO_WQ_WORK_FILES; |
| } |
| if (!(req->work.flags & IO_WQ_WORK_MM) && |
| (def->work_flags & IO_WQ_WORK_MM)) { |
| if (id->mm != current->mm) |
| return false; |
| mmgrab(id->mm); |
| req->work.flags |= IO_WQ_WORK_MM; |
| } |
| |
| return true; |
| } |
| |
| static void io_prep_async_work(struct io_kiocb *req) |
| { |
| const struct io_op_def *def = &io_op_defs[req->opcode]; |
| struct io_ring_ctx *ctx = req->ctx; |
| struct io_identity *id; |
| |
| io_req_init_async(req); |
| id = req->work.identity; |
| |
| if (req->flags & REQ_F_FORCE_ASYNC) |
| req->work.flags |= IO_WQ_WORK_CONCURRENT; |
| |
| if (req->flags & REQ_F_ISREG) { |
| if (def->hash_reg_file || (ctx->flags & IORING_SETUP_IOPOLL)) |
| io_wq_hash_work(&req->work, file_inode(req->file)); |
| } else if (!req->file || !S_ISBLK(file_inode(req->file)->i_mode)) { |
| if (def->unbound_nonreg_file) |
| req->work.flags |= IO_WQ_WORK_UNBOUND; |
| } |
| |
| /* if we fail grabbing identity, we must COW, regrab, and retry */ |
| if (io_grab_identity(req)) |
| return; |
| |
| if (!io_identity_cow(req)) |
| return; |
| |
| /* can't fail at this point */ |
| if (!io_grab_identity(req)) |
| WARN_ON(1); |
| } |
| |
| static void io_prep_async_link(struct io_kiocb *req) |
| { |
| struct io_kiocb *cur; |
| |
| io_prep_async_work(req); |
| if (req->flags & REQ_F_LINK_HEAD) |
| list_for_each_entry(cur, &req->link_list, link_list) |
| io_prep_async_work(cur); |
| } |
| |
| static struct io_kiocb *__io_queue_async_work(struct io_kiocb *req) |
| { |
| struct io_ring_ctx *ctx = req->ctx; |
| struct io_kiocb *link = io_prep_linked_timeout(req); |
| |
| trace_io_uring_queue_async_work(ctx, io_wq_is_hashed(&req->work), req, |
| &req->work, req->flags); |
| io_wq_enqueue(ctx->io_wq, &req->work); |
| return link; |
| } |
| |
| static void io_queue_async_work(struct io_kiocb *req) |
| { |
| struct io_kiocb *link; |
| |
| /* init ->work of the whole link before punting */ |
| io_prep_async_link(req); |
| link = __io_queue_async_work(req); |
| |
| if (link) |
| io_queue_linked_timeout(link); |
| } |
| |
| static void io_kill_timeout(struct io_kiocb *req, int status) |
| { |
| struct io_timeout_data *io = req->async_data; |
| int ret; |
| |
| ret = hrtimer_try_to_cancel(&io->timer); |
| if (ret != -1) { |
| if (status) |
| req_set_fail_links(req); |
| atomic_set(&req->ctx->cq_timeouts, |
| atomic_read(&req->ctx->cq_timeouts) + 1); |
| list_del_init(&req->timeout.list); |
| io_cqring_fill_event(req, status); |
| io_put_req_deferred(req, 1); |
| } |
| } |
| |
| /* |
| * Returns true if we found and killed one or more timeouts |
| */ |
| static bool io_kill_timeouts(struct io_ring_ctx *ctx, struct task_struct *tsk, |
| struct files_struct *files) |
| { |
| struct io_kiocb *req, *tmp; |
| int canceled = 0; |
| |
| spin_lock_irq(&ctx->completion_lock); |
| list_for_each_entry_safe(req, tmp, &ctx->timeout_list, timeout.list) { |
| if (io_match_task(req, tsk, files)) { |
| io_kill_timeout(req, -ECANCELED); |
| canceled++; |
| } |
| } |
| spin_unlock_irq(&ctx->completion_lock); |
| return canceled != 0; |
| } |
| |
| static void __io_queue_deferred(struct io_ring_ctx *ctx) |
| { |
| do { |
| struct io_defer_entry *de = list_first_entry(&ctx->defer_list, |
| struct io_defer_entry, list); |
| |
| if (req_need_defer(de->req, de->seq)) |
| break; |
| list_del_init(&de->list); |
| io_req_task_queue(de->req); |
| kfree(de); |
| } while (!list_empty(&ctx->defer_list)); |
| } |
| |
| static void io_flush_timeouts(struct io_ring_ctx *ctx) |
| { |
| u32 seq; |
| |
| if (list_empty(&ctx->timeout_list)) |
| return; |
| |
| seq = ctx->cached_cq_tail - atomic_read(&ctx->cq_timeouts); |
| |
| do { |
| u32 events_needed, events_got; |
| struct io_kiocb *req = list_first_entry(&ctx->timeout_list, |
| struct io_kiocb, timeout.list); |
| |
| if (io_is_timeout_noseq(req)) |
| break; |
| |
| /* |
| * Since seq can easily wrap around over time, subtract |
| * the last seq at which timeouts were flushed before comparing. |
| * Assuming not more than 2^31-1 events have happened since, |
| * these subtractions won't have wrapped, so we can check if |
| * target is in [last_seq, current_seq] by comparing the two. |
| */ |
| events_needed = req->timeout.target_seq - ctx->cq_last_tm_flush; |
| events_got = seq - ctx->cq_last_tm_flush; |
| if (events_got < events_needed) |
| break; |
| |
| list_del_init(&req->timeout.list); |
| io_kill_timeout(req, 0); |
| } while (!list_empty(&ctx->timeout_list)); |
| |
| ctx->cq_last_tm_flush = seq; |
| } |
| |
| static void io_commit_cqring(struct io_ring_ctx *ctx) |
| { |
| io_flush_timeouts(ctx); |
| __io_commit_cqring(ctx); |
| |
| if (unlikely(!list_empty(&ctx->defer_list))) |
| __io_queue_deferred(ctx); |
| } |
| |
| static inline bool io_sqring_full(struct io_ring_ctx *ctx) |
| { |
| struct io_rings *r = ctx->rings; |
| |
| return READ_ONCE(r->sq.tail) - ctx->cached_sq_head == r->sq_ring_entries; |
| } |
| |
| static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx) |
| { |
| struct io_rings *rings = ctx->rings; |
| unsigned tail; |
| |
| tail = ctx->cached_cq_tail; |
| /* |
| * writes to the cq entry need to come after reading head; the |
| * control dependency is enough as we're using WRITE_ONCE to |
| * fill the cq entry |
| */ |
| if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries) |
| return NULL; |
| |
| ctx->cached_cq_tail++; |
| return &rings->cqes[tail & ctx->cq_mask]; |
| } |
| |
| static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx) |
| { |
| if (!ctx->cq_ev_fd) |
| return false; |
| if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED) |
| return false; |
| if (!ctx->eventfd_async) |
| return true; |
| return io_wq_current_is_worker(); |
| } |
| |
| static void io_cqring_ev_posted(struct io_ring_ctx *ctx) |
| { |
| if (wq_has_sleeper(&ctx->cq_wait)) { |
| wake_up_interruptible(&ctx->cq_wait); |
| kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN); |
| } |
| if (waitqueue_active(&ctx->wait)) |
| wake_up(&ctx->wait); |
| if (ctx->sq_data && waitqueue_active(&ctx->sq_data->wait)) |
| wake_up(&ctx->sq_data->wait); |
| if (io_should_trigger_evfd(ctx)) |
| eventfd_signal(ctx->cq_ev_fd, 1); |
| } |
| |
| static void io_cqring_mark_overflow(struct io_ring_ctx *ctx) |
| { |
| if (list_empty(&ctx->cq_overflow_list)) { |
| clear_bit(0, &ctx->sq_check_overflow); |
| clear_bit(0, &ctx->cq_check_overflow); |
| ctx->rings->sq_flags &= ~IORING_SQ_CQ_OVERFLOW; |
| } |
| } |
| |
| /* Returns true if there are no backlogged entries after the flush */ |
| static bool __io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force, |
| struct task_struct *tsk, |
| struct files_struct *files) |
| { |
| struct io_rings *rings = ctx->rings; |
| struct io_kiocb *req, *tmp; |
| struct io_uring_cqe *cqe; |
| unsigned long flags; |
| LIST_HEAD(list); |
| |
| if (!force) { |
| if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) == |
| rings->cq_ring_entries)) |
| return false; |
| } |
| |
| spin_lock_irqsave(&ctx->completion_lock, flags); |
| |
| cqe = NULL; |
| list_for_each_entry_safe(req, tmp, &ctx->cq_overflow_list, compl.list) { |
| if (!io_match_task(req, tsk, files)) |
| continue; |
| |
| cqe = io_get_cqring(ctx); |
| if (!cqe && !force) |
| break; |
| |
| list_move(&req->compl.list, &list); |
| if (cqe) { |
| WRITE_ONCE(cqe->user_data, req->user_data); |
| WRITE_ONCE(cqe->res, req->result); |
| WRITE_ONCE(cqe->flags, req->compl.cflags); |
| } else { |
| ctx->cached_cq_overflow++; |
| WRITE_ONCE(ctx->rings->cq_overflow, |
| ctx->cached_cq_overflow); |
| } |
| } |
| |
| io_commit_cqring(ctx); |
| io_cqring_mark_overflow(ctx); |
| |
| spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| io_cqring_ev_posted(ctx); |
| |
| while (!list_empty(&list)) { |
| req = list_first_entry(&list, struct io_kiocb, compl.list); |
| list_del(&req->compl.list); |
| io_put_req(req); |
| } |
| |
| return cqe != NULL; |
| } |
| |
| static void io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force, |
| struct task_struct *tsk, |
| struct files_struct *files) |
| { |
| if (test_bit(0, &ctx->cq_check_overflow)) { |
| /* iopoll syncs against uring_lock, not completion_lock */ |
| if (ctx->flags & IORING_SETUP_IOPOLL) |
| mutex_lock(&ctx->uring_lock); |
| __io_cqring_overflow_flush(ctx, force, tsk, files); |
| if (ctx->flags & IORING_SETUP_IOPOLL) |
| mutex_unlock(&ctx->uring_lock); |
| } |
| } |
| |
| static void __io_cqring_fill_event(struct io_kiocb *req, long res, |
| unsigned int cflags) |
| { |
| struct io_ring_ctx *ctx = req->ctx; |
| struct io_uring_cqe *cqe; |
| |
| trace_io_uring_complete(ctx, req->user_data, res); |
| |
| /* |
| * If we can't get a cq entry, userspace overflowed the |
| * submission (by quite a lot). Increment the overflow count in |
| * the ring. |
| */ |
| cqe = io_get_cqring(ctx); |
| if (likely(cqe)) { |
| WRITE_ONCE(cqe->user_data, req->user_data); |
| WRITE_ONCE(cqe->res, res); |
| WRITE_ONCE(cqe->flags, cflags); |
| } else if (ctx->cq_overflow_flushed || |
| atomic_read(&req->task->io_uring->in_idle)) { |
| /* |
| * If we're in ring overflow flush mode, or in task cancel mode, |
| * then we cannot store the request for later flushing, we need |
| * to drop it on the floor. |
| */ |
| ctx->cached_cq_overflow++; |
| WRITE_ONCE(ctx->rings->cq_overflow, ctx->cached_cq_overflow); |
| } else { |
| if (list_empty(&ctx->cq_overflow_list)) { |
| set_bit(0, &ctx->sq_check_overflow); |
| set_bit(0, &ctx->cq_check_overflow); |
| ctx->rings->sq_flags |= IORING_SQ_CQ_OVERFLOW; |
| } |
| io_clean_op(req); |
| req->result = res; |
| req->compl.cflags = cflags; |
| refcount_inc(&req->refs); |
| list_add_tail(&req->compl.list, &ctx->cq_overflow_list); |
| } |
| } |
| |
| static void io_cqring_fill_event(struct io_kiocb *req, long res) |
| { |
| __io_cqring_fill_event(req, res, 0); |
| } |
| |
| static void io_cqring_add_event(struct io_kiocb *req, long res, long cflags) |
| { |
| struct io_ring_ctx *ctx = req->ctx; |
| unsigned long flags; |
| |
| spin_lock_irqsave(&ctx->completion_lock, flags); |
| __io_cqring_fill_event(req, res, cflags); |
| io_commit_cqring(ctx); |
| spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| |
| io_cqring_ev_posted(ctx); |
| } |
| |
| static void io_submit_flush_completions(struct io_comp_state *cs) |
| { |
| struct io_ring_ctx *ctx = cs->ctx; |
| |
| spin_lock_irq(&ctx->completion_lock); |
| while (!list_empty(&cs->list)) { |
| struct io_kiocb *req; |
| |
| req = list_first_entry(&cs->list, struct io_kiocb, compl.list); |
| list_del(&req->compl.list); |
| __io_cqring_fill_event(req, req->result, req->compl.cflags); |
| |
| /* |
| * io_free_req() doesn't care about completion_lock unless one |
| * of these flags is set. REQ_F_WORK_INITIALIZED is in the list |
| * because of a potential deadlock with req->work.fs->lock |
| */ |
| if (req->flags & (REQ_F_FAIL_LINK|REQ_F_LINK_TIMEOUT |
| |REQ_F_WORK_INITIALIZED)) { |
| spin_unlock_irq(&ctx->completion_lock); |
| io_put_req(req); |
| spin_lock_irq(&ctx->completion_lock); |
| } else { |
| io_put_req(req); |
| } |
| } |
| io_commit_cqring(ctx); |
| spin_unlock_irq(&ctx->completion_lock); |
| |
| io_cqring_ev_posted(ctx); |
| cs->nr = 0; |
| } |
| |
| static void __io_req_complete(struct io_kiocb *req, long res, unsigned cflags, |
| struct io_comp_state *cs) |
| { |
| if (!cs) { |
| io_cqring_add_event(req, res, cflags); |
| io_put_req(req); |
| } else { |
| io_clean_op(req); |
| req->result = res; |
| req->compl.cflags = cflags; |
| list_add_tail(&req->compl.list, &cs->list); |
| if (++cs->nr >= 32) |
| io_submit_flush_completions(cs); |
| } |
| } |
| |
| static void io_req_complete(struct io_kiocb *req, long res) |
| { |
| __io_req_complete(req, res, 0, NULL); |
| } |
| |
| static inline bool io_is_fallback_req(struct io_kiocb *req) |
| { |
| return req == (struct io_kiocb *) |
| ((unsigned long) req->ctx->fallback_req & ~1UL); |
| } |
| |
| static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx) |
| { |
| struct io_kiocb *req; |
| |
| req = ctx->fallback_req; |
| if (!test_and_set_bit_lock(0, (unsigned long *) &ctx->fallback_req)) |
| return req; |
| |
| return NULL; |
| } |
| |
| static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx, |
| struct io_submit_state *state) |
| { |
| if (!state->free_reqs) { |
| gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; |
| size_t sz; |
| int ret; |
| |
| sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs)); |
| ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs); |
| |
| /* |
| * Bulk alloc is all-or-nothing. If we fail to get a batch, |
| * retry single alloc to be on the safe side. |
| */ |
| if (unlikely(ret <= 0)) { |
| state->reqs[0] = kmem_cache_alloc(req_cachep, gfp); |
| if (!state->reqs[0]) |
| goto fallback; |
| ret = 1; |
| } |
| state->free_reqs = ret; |
| } |
| |
| state->free_reqs--; |
| return state->reqs[state->free_reqs]; |
| fallback: |
| return io_get_fallback_req(ctx); |
| } |
| |
| static inline void io_put_file(struct io_kiocb *req, struct file *file, |
| bool fixed) |
| { |
| if (fixed) |
| percpu_ref_put(req->fixed_file_refs); |
| else |
| fput(file); |
| } |
| |
| static void io_dismantle_req(struct io_kiocb *req) |
| { |
| io_clean_op(req); |
| |
| if (req->async_data) |
| kfree(req->async_data); |
| if (req->file) |
| io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE)); |
| |
| io_req_clean_work(req); |
| } |
| |
| static void __io_free_req(struct io_kiocb *req) |
| { |
| struct io_uring_task *tctx = req->task->io_uring; |
| struct io_ring_ctx *ctx = req->ctx; |
| |
| io_dismantle_req(req); |
| |
| percpu_counter_dec(&tctx->inflight); |
| if (atomic_read(&tctx->in_idle)) |
| wake_up(&tctx->wait); |
| put_task_struct(req->task); |
| |
| if (likely(!io_is_fallback_req(req))) |
| kmem_cache_free(req_cachep, req); |
| else |
| clear_bit_unlock(0, (unsigned long *) &ctx->fallback_req); |
| percpu_ref_put(&ctx->refs); |
| } |
| |
| static void io_kill_linked_timeout(struct io_kiocb *req) |
| { |
| struct io_ring_ctx *ctx = req->ctx; |
| struct io_kiocb *link; |
| bool cancelled = false; |
| unsigned long flags; |
| |
| spin_lock_irqsave(&ctx->completion_lock, flags); |
| link = list_first_entry_or_null(&req->link_list, struct io_kiocb, |
| link_list); |
| /* |
| * Can happen if a linked timeout fired and link had been like |
| * req -> link t-out -> link t-out [-> ...] |
| */ |
| if (link && (link->flags & REQ_F_LTIMEOUT_ACTIVE)) { |
| struct io_timeout_data *io = link->async_data; |
| int ret; |
| |
| list_del_init(&link->link_list); |
| ret = hrtimer_try_to_cancel(&io->timer); |
| if (ret != -1) { |
| io_cqring_fill_event(link, -ECANCELED); |
| io_commit_cqring(ctx); |
| cancelled = true; |
| } |
| } |
| req->flags &= ~REQ_F_LINK_TIMEOUT; |
| spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| |
| if (cancelled) { |
| io_cqring_ev_posted(ctx); |
| io_put_req(link); |
| } |
| } |
| |
| static struct io_kiocb *io_req_link_next(struct io_kiocb *req) |
| { |
| struct io_kiocb *nxt; |
| |
| /* |
| * The list should never be empty when we are called here. But could |
| * potentially happen if the chain is messed up, check to be on the |
| * safe side. |
| */ |
| if (unlikely(list_empty(&req->link_list))) |
| return NULL; |
| |
| nxt = list_first_entry(&req->link_list, struct io_kiocb, link_list); |
| list_del_init(&req->link_list); |
| if (!list_empty(&nxt->link_list)) |
| nxt->flags |= REQ_F_LINK_HEAD; |
| return nxt; |
| } |
| |
| /* |
| * Called if REQ_F_LINK_HEAD is set, and we fail the head request |
| */ |
| static void io_fail_links(struct io_kiocb *req) |
| { |
| struct io_ring_ctx *ctx = req->ctx; |
| unsigned long flags; |
| |
| spin_lock_irqsave(&ctx->completion_lock, flags); |
| while (!list_empty(&req->link_list)) { |
| struct io_kiocb *link = list_first_entry(&req->link_list, |
| struct io_kiocb, link_list); |
| |
| list_del_init(&link->link_list); |
| trace_io_uring_fail_link(req, link); |
| |
| io_cqring_fill_event(link, -ECANCELED); |
| |
| /* |
| * It's ok to free under spinlock as they're not linked anymore, |
| * but avoid REQ_F_WORK_INITIALIZED because it may deadlock on |
| * work.fs->lock. |
| */ |
| if (link->flags & REQ_F_WORK_INITIALIZED) |
| io_put_req_deferred(link, 2); |
| else |
| io_double_put_req(link); |
| } |
| |
| io_commit_cqring(ctx); |
| spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| |
| io_cqring_ev_posted(ctx); |
| } |
| |
| static struct io_kiocb *__io_req_find_next(struct io_kiocb *req) |
| { |
| req->flags &= ~REQ_F_LINK_HEAD; |
| if (req->flags & REQ_F_LINK_TIMEOUT) |
| io_kill_linked_timeout(req); |
| |
| /* |
| * If LINK is set, we have dependent requests in this chain. If we |
| * didn't fail this request, queue the first one up, moving any other |
| * dependencies to the next request. In case of failure, fail the rest |
| * of the chain. |
| */ |
| if (likely(!(req->flags & REQ_F_FAIL_LINK))) |
| return io_req_link_next(req); |
| io_fail_links(req); |
| return NULL; |
| } |
| |
| static struct io_kiocb *io_req_find_next(struct io_kiocb *req) |
| { |
| if (likely(!(req->flags & REQ_F_LINK_HEAD))) |
| return NULL; |
| return __io_req_find_next(req); |
| } |
| |
| static int io_req_task_work_add(struct io_kiocb *req, bool twa_signal_ok) |
| { |
| struct task_struct *tsk = req->task; |
| struct io_ring_ctx *ctx = req->ctx; |
| enum task_work_notify_mode notify; |
| int ret; |
| |
| if (tsk->flags & PF_EXITING) |
| return -ESRCH; |
| |
| /* |
| * SQPOLL kernel thread doesn't need notification, just a wakeup. For |
| * all other cases, use TWA_SIGNAL unconditionally to ensure we're |
| * processing task_work. There's no reliable way to tell if TWA_RESUME |
| * will do the job. |
| */ |
| notify = TWA_NONE; |
| if (!(ctx->flags & IORING_SETUP_SQPOLL) && twa_signal_ok) |
| notify = TWA_SIGNAL; |
| |
| ret = task_work_add(tsk, &req->task_work, notify); |
| if (!ret) |
| wake_up_process(tsk); |
| |
| return ret; |
| } |
| |
| static void __io_req_task_cancel(struct io_kiocb *req, int error) |
| { |
| struct io_ring_ctx *ctx = req->ctx; |
| |
| spin_lock_irq(&ctx->completion_lock); |
| io_cqring_fill_event(req, error); |
| io_commit_cqring(ctx); |
| spin_unlock_irq(&ctx->completion_lock); |
| |
| io_cqring_ev_posted(ctx); |
| req_set_fail_links(req); |
| io_double_put_req(req); |
| } |
| |
| static void io_req_task_cancel(struct callback_head *cb) |
| { |
| struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
| struct io_ring_ctx *ctx = req->ctx; |
| |
| mutex_lock(&ctx->uring_lock); |
| __io_req_task_cancel(req, -ECANCELED); |
| mutex_unlock(&ctx->uring_lock); |
| percpu_ref_put(&ctx->refs); |
| } |
| |
| static void __io_req_task_submit(struct io_kiocb *req) |
| { |
| struct io_ring_ctx *ctx = req->ctx; |
| |
| mutex_lock(&ctx->uring_lock); |
| if (!ctx->sqo_dead && !__io_sq_thread_acquire_mm(ctx)) |
| __io_queue_sqe(req, NULL); |
| else |
| __io_req_task_cancel(req, -EFAULT); |
| mutex_unlock(&ctx->uring_lock); |
| |
| if (ctx->flags & IORING_SETUP_SQPOLL) |
| io_sq_thread_drop_mm(); |
| } |
| |
| static void io_req_task_submit(struct callback_head *cb) |
| { |
| struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
| struct io_ring_ctx *ctx = req->ctx; |
| |
| __io_req_task_submit(req); |
| percpu_ref_put(&ctx->refs); |
| } |
| |
| static void io_req_task_queue(struct io_kiocb *req) |
| { |
| int ret; |
| |
| init_task_work(&req->task_work, io_req_task_submit); |
| percpu_ref_get(&req->ctx->refs); |
| |
| ret = io_req_task_work_add(req, true); |
| if (unlikely(ret)) { |
| struct task_struct *tsk; |
| |
| init_task_work(&req->task_work, io_req_task_cancel); |
| tsk = io_wq_get_task(req->ctx->io_wq); |
| task_work_add(tsk, &req->task_work, TWA_NONE); |
| wake_up_process(tsk); |
| } |
| } |
| |
| static void io_queue_next(struct io_kiocb *req) |
| { |
| struct io_kiocb *nxt = io_req_find_next(req); |
| |
| if (nxt) |
| io_req_task_queue(nxt); |
| } |
| |
| static void io_free_req(struct io_kiocb *req) |
| { |
| io_queue_next(req); |
| __io_free_req(req); |
| } |
| |
| struct req_batch { |
| void *reqs[IO_IOPOLL_BATCH]; |
| int to_free; |
| |
| struct task_struct *task; |
| int task_refs; |
| }; |
| |
| static inline void io_init_req_batch(struct req_batch *rb) |
| { |
| rb->to_free = 0; |
| rb->task_refs = 0; |
| rb->task = NULL; |
| } |
| |
| static void __io_req_free_batch_flush(struct io_ring_ctx *ctx, |
| struct req_batch *rb) |
| { |
| kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs); |
| percpu_ref_put_many(&ctx->refs, rb->to_free); |
| rb->to_free = 0; |
| } |
| |
| static void io_req_free_batch_finish(struct io_ring_ctx *ctx, |
| struct req_batch *rb) |
| { |
| if (rb->to_free) |
| __io_req_free_batch_flush(ctx, rb); |
| if (rb->task) { |
| struct io_uring_task *tctx = rb->task->io_uring; |
| |
| percpu_counter_sub(&tctx->inflight, rb->task_refs); |
| if (atomic_read(&tctx->in_idle)) |
| wake_up(&tctx->wait); |
| put_task_struct_many(rb->task, rb->task_refs); |
| rb->task = NULL; |
| } |
| } |
| |
| static void io_req_free_batch(struct req_batch *rb, struct io_kiocb *req) |
| { |
| if (unlikely(io_is_fallback_req(req))) { |
| io_free_req(req); |
| return; |
| } |
| if (req->flags & REQ_F_LINK_HEAD) |
| io_queue_next(req); |
| |
| if (req->task != rb->task) { |
| if (rb->task) { |
| struct io_uring_task *tctx = rb->task->io_uring; |
| |
| percpu_counter_sub(&tctx->inflight, rb->task_refs); |
| if (atomic_read(&tctx->in_idle)) |
| wake_up(&tctx->wait); |
| put_task_struct_many(rb->task, rb->task_refs); |
| } |
| rb->task = req->task; |
| rb->task_refs = 0; |
| } |
| rb->task_refs++; |
| |
| io_dismantle_req(req); |
| rb->reqs[rb->to_free++] = req; |
| if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs))) |
| __io_req_free_batch_flush(req->ctx, rb); |
| } |
| |
| /* |
| * Drop reference to request, return next in chain (if there is one) if this |
| * was the last reference to this request. |
| */ |
| static struct io_kiocb *io_put_req_find_next(struct io_kiocb *req) |
| { |
| struct io_kiocb *nxt = NULL; |
| |
| if (refcount_dec_and_test(&req->refs)) { |
| nxt = io_req_find_next(req); |
| __io_free_req(req); |
| } |
| return nxt; |
| } |
| |
| static void io_put_req(struct io_kiocb *req) |
| { |
| if (refcount_dec_and_test(&req->refs)) |
| io_free_req(req); |
| } |
| |
| static void io_put_req_deferred_cb(struct callback_head *cb) |
| { |
| struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work); |
| |
| io_free_req(req); |
| } |
| |
| static void io_free_req_deferred(struct io_kiocb *req) |
| { |
| int ret; |
| |
| init_task_work(&req->task_work, io_put_req_deferred_cb); |
| ret = io_req_task_work_add(req, true); |
| if (unlikely(ret)) { |
| struct task_struct *tsk; |
| |
| tsk = io_wq_get_task(req->ctx->io_wq); |
| task_work_add(tsk, &req->task_work, TWA_NONE); |
| wake_up_process(tsk); |
| } |
| } |
| |
| static inline void io_put_req_deferred(struct io_kiocb *req, int refs) |
| { |
| if (refcount_sub_and_test(refs, &req->refs)) |
| io_free_req_deferred(req); |
| } |
| |
| static struct io_wq_work *io_steal_work(struct io_kiocb *req) |
| { |
| struct io_kiocb *nxt; |
| |
| /* |
| * A ref is owned by io-wq in which context we're. So, if that's the |
| * last one, it's safe to steal next work. False negatives are Ok, |
| * it just will be re-punted async in io_put_work() |
| */ |
| if (refcount_read(&req->refs) != 1) |
| return NULL; |
| |
| nxt = io_req_find_next(req); |
| return nxt ? &nxt->work : NULL; |
| } |
| |
| static void io_double_put_req(struct io_kiocb *req) |
| { |
| /* drop both submit and complete references */ |
| if (refcount_sub_and_test(2, &req->refs)) |
| io_free_req(req); |
| } |
| |
| static unsigned io_cqring_events(struct io_ring_ctx *ctx) |
| { |
| struct io_rings *rings = ctx->rings; |
| |
| /* See comment at the top of this file */ |
| smp_rmb(); |
| return ctx->cached_cq_tail - READ_ONCE(rings->cq.head); |
| } |
| |
| static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx) |
| { |
| struct io_rings *rings = ctx->rings; |
| |
| /* make sure SQ entry isn't read before tail */ |
| return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head; |
| } |
| |
| static unsigned int io_put_kbuf(struct io_kiocb *req, struct io_buffer *kbuf) |
| { |
| unsigned int cflags; |
| |
| cflags = kbuf->bid << IORING_CQE_BUFFER_SHIFT; |
| cflags |= IORING_CQE_F_BUFFER; |
| req->flags &= ~REQ_F_BUFFER_SELECTED; |
| kfree(kbuf); |
| return cflags; |
| } |
| |
| static inline unsigned int io_put_rw_kbuf(struct io_kiocb *req) |
| { |
| struct io_buffer *kbuf; |
| |
| kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; |
| return io_put_kbuf(req, kbuf); |
| } |
| |
| static inline bool io_run_task_work(void) |
| { |
| /* |
| * Not safe to run on exiting task, and the task_work handling will |
| * not add work to such a task. |
| */ |
| if (unlikely(current->flags & PF_EXITING)) |
| return false; |
| if (current->task_works) { |
| __set_current_state(TASK_RUNNING); |
| task_work_run(); |
| return true; |
| } |
| |
| return false; |
| } |
| |
| static void io_iopoll_queue(struct list_head *again) |
| { |
| struct io_kiocb *req; |
| |
| do { |
| req = list_first_entry(again, struct io_kiocb, inflight_entry); |
| list_del(&req->inflight_entry); |
| __io_complete_rw(req, -EAGAIN, 0, NULL); |
| } while (!list_empty(again)); |
| } |
| |
| /* |
| * Find and free completed poll iocbs |
| */ |
| static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| struct list_head *done) |
| { |
| struct req_batch rb; |
| struct io_kiocb *req; |
| LIST_HEAD(again); |
| |
| /* order with ->result store in io_complete_rw_iopoll() */ |
| smp_rmb(); |
| |
| io_init_req_batch(&rb); |
| while (!list_empty(done)) { |
| int cflags = 0; |
| |
| req = list_first_entry(done, struct io_kiocb, inflight_entry); |
| if (READ_ONCE(req->result) == -EAGAIN) { |
| req->result = 0; |
| req->iopoll_completed = 0; |
| list_move_tail(&req->inflight_entry, &again); |
| continue; |
| } |
| list_del(&req->inflight_entry); |
| |
| if (req->flags & REQ_F_BUFFER_SELECTED) |
| cflags = io_put_rw_kbuf(req); |
| |
| __io_cqring_fill_event(req, req->result, cflags); |
| (*nr_events)++; |
| |
| if (refcount_dec_and_test(&req->refs)) |
| io_req_free_batch(&rb, req); |
| } |
| |
| io_commit_cqring(ctx); |
| if (ctx->flags & IORING_SETUP_SQPOLL) |
| io_cqring_ev_posted(ctx); |
| io_req_free_batch_finish(ctx, &rb); |
| |
| if (!list_empty(&again)) |
| io_iopoll_queue(&again); |
| } |
| |
| static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| long min) |
| { |
| struct io_kiocb *req, *tmp; |
| LIST_HEAD(done); |
| bool spin; |
| int ret; |
| |
| /* |
| * Only spin for completions if we don't have multiple devices hanging |
| * off our complete list, and we're under the requested amount. |
| */ |
| spin = !ctx->poll_multi_file && *nr_events < min; |
| |
| ret = 0; |
| list_for_each_entry_safe(req, tmp, &ctx->iopoll_list, inflight_entry) { |
| struct kiocb *kiocb = &req->rw.kiocb; |
| |
| /* |
| * Move completed and retryable entries to our local lists. |
| * If we find a request that requires polling, break out |
| * and complete those lists first, if we have entries there. |
| */ |
| if (READ_ONCE(req->iopoll_completed)) { |
| list_move_tail(&req->inflight_entry, &done); |
| continue; |
| } |
| if (!list_empty(&done)) |
| break; |
| |
| ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin); |
| if (ret < 0) |
| break; |
| |
| /* iopoll may have completed current req */ |
| if (READ_ONCE(req->iopoll_completed)) |
| list_move_tail(&req->inflight_entry, &done); |
| |
| if (ret && spin) |
| spin = false; |
| ret = 0; |
| } |
| |
| if (!list_empty(&done)) |
| io_iopoll_complete(ctx, nr_events, &done); |
| |
| return ret; |
| } |
| |
| /* |
| * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a |
| * non-spinning poll check - we'll still enter the driver poll loop, but only |
| * as a non-spinning completion check. |
| */ |
| static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| long min) |
| { |
| while (!list_empty(&ctx->iopoll_list) && !need_resched()) { |
| int ret; |
| |
| ret = io_do_iopoll(ctx, nr_events, min); |
| if (ret < 0) |
| return ret; |
| if (*nr_events >= min) |
| return 0; |
| } |
| |
| return 1; |
| } |
| |
| /* |
| * We can't just wait for polled events to come to us, we have to actively |
| * find and complete them. |
| */ |
| static void io_iopoll_try_reap_events(struct io_ring_ctx *ctx) |
| { |
| if (!(ctx->flags & IORING_SETUP_IOPOLL)) |
| return; |
| |
| mutex_lock(&ctx->uring_lock); |
| while (!list_empty(&ctx->iopoll_list)) { |
| unsigned int nr_events = 0; |
| |
| io_do_iopoll(ctx, &nr_events, 0); |
| |
| /* let it sleep and repeat later if can't complete a request */ |
| if (nr_events == 0) |
| break; |
| /* |
| * Ensure we allow local-to-the-cpu processing to take place, |
| * in this case we need to ensure that we reap all events. |
| * Also let task_work, etc. to progress by releasing the mutex |
| */ |
| if (need_resched()) { |
| mutex_unlock(&ctx->uring_lock); |
| cond_resched(); |
| mutex_lock(&ctx->uring_lock); |
| } |
| } |
| mutex_unlock(&ctx->uring_lock); |
| } |
| |
| static int io_iopoll_check(struct io_ring_ctx *ctx, long min) |
| { |
| unsigned int nr_events = 0; |
| int iters = 0, ret = 0; |
| |
| /* |
| * We disallow the app entering submit/complete with polling, but we |
| * still need to lock the ring to prevent racing with polled issue |
| * that got punted to a workqueue. |
| */ |
| mutex_lock(&ctx->uring_lock); |
| do { |
| /* |
| * Don't enter poll loop if we already have events pending. |
| * If we do, we can potentially be spinning for commands that |
| * already triggered a CQE (eg in error). |
| */ |
| if (test_bit(0, &ctx->cq_check_overflow)) |
| __io_cqring_overflow_flush(ctx, false, NULL, NULL); |
| if (io_cqring_events(ctx)) |
| break; |
| |
| /* |
| * If a submit got punted to a workqueue, we can have the |
| * application entering polling for a command before it gets |
| * issued. That app will hold the uring_lock for the duration |
| * of the poll right here, so we need to take a breather every |
| * now and then to ensure that the issue has a chance to add |
| * the poll to the issued list. Otherwise we can spin here |
| * forever, while the workqueue is stuck trying to acquire the |
| * very same mutex. |
| */ |
| if (!(++iters & 7)) { |
| mutex_unlock(&ctx->uring_lock); |
| io_run_task_work(); |
| mutex_lock(&ctx->uring_lock); |
| } |
| |
| ret = io_iopoll_getevents(ctx, &nr_events, min); |
| if (ret <= 0) |
| break; |
| ret = 0; |
| } while (min && !nr_events && !need_resched()); |
| |
| mutex_unlock(&ctx->uring_lock); |
| return ret; |
| } |
| |
| static void kiocb_end_write(struct io_kiocb *req) |
| { |
| /* |
| * Tell lockdep we inherited freeze protection from submission |
| * thread. |
| */ |
| if (req->flags & REQ_F_ISREG) { |
| struct inode *inode = file_inode(req->file); |
| |
| __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE); |
| } |
| file_end_write(req->file); |
| } |
| |
| static void io_complete_rw_common(struct kiocb *kiocb, long res, |
| struct io_comp_state *cs) |
| { |
| struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
| int cflags = 0; |
| |
| if (kiocb->ki_flags & IOCB_WRITE) |
| kiocb_end_write(req); |
| |
| if (res != req->result) |
| req_set_fail_links(req); |
| if (req->flags & REQ_F_BUFFER_SELECTED) |
| cflags = io_put_rw_kbuf(req); |
| __io_req_complete(req, res, cflags, cs); |
| } |
| |
| #ifdef CONFIG_BLOCK |
| static bool io_resubmit_prep(struct io_kiocb *req, int error) |
| { |
| struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; |
| ssize_t ret = -ECANCELED; |
| struct iov_iter iter; |
| int rw; |
| |
| if (error) { |
| ret = error; |
| goto end_req; |
| } |
| |
| switch (req->opcode) { |
| case IORING_OP_READV: |
| case IORING_OP_READ_FIXED: |
| case IORING_OP_READ: |
| rw = READ; |
| break; |
| case IORING_OP_WRITEV: |
| case IORING_OP_WRITE_FIXED: |
| case IORING_OP_WRITE: |
| rw = WRITE; |
| break; |
| default: |
| printk_once(KERN_WARNING "io_uring: bad opcode in resubmit %d\n", |
| req->opcode); |
| goto end_req; |
| } |
| |
| if (!req->async_data) { |
| ret = io_import_iovec(rw, req, &iovec, &iter, false); |
| if (ret < 0) |
| goto end_req; |
| ret = io_setup_async_rw(req, iovec, inline_vecs, &iter, false); |
| if (!ret) |
| return true; |
| kfree(iovec); |
| } else { |
| return true; |
| } |
| end_req: |
| req_set_fail_links(req); |
| return false; |
| } |
| #endif |
| |
| static bool io_rw_reissue(struct io_kiocb *req, long res) |
| { |
| #ifdef CONFIG_BLOCK |
| umode_t mode = file_inode(req->file)->i_mode; |
| int ret; |
| |
| if (!S_ISBLK(mode) && !S_ISREG(mode)) |
| return false; |
| if ((res != -EAGAIN && res != -EOPNOTSUPP) || io_wq_current_is_worker()) |
| return false; |
| /* |
| * If ref is dying, we might be running poll reap from the exit work. |
| * Don't attempt to reissue from that path, just let it fail with |
| * -EAGAIN. |
| */ |
| if (percpu_ref_is_dying(&req->ctx->refs)) |
| return false; |
| |
| ret = io_sq_thread_acquire_mm(req->ctx, req); |
| |
| if (io_resubmit_prep(req, ret)) { |
| refcount_inc(&req->refs); |
| io_queue_async_work(req); |
| return true; |
| } |
| |
| #endif |
| return false; |
| } |
| |
| static void __io_complete_rw(struct io_kiocb *req, long res, long res2, |
| struct io_comp_state *cs) |
| { |
| if (!io_rw_reissue(req, res)) |
| io_complete_rw_common(&req->rw.kiocb, res, cs); |
| } |
| |
| static void io_complete_rw(struct kiocb *kiocb, long res, long res2) |
| { |
| struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
| |
| __io_complete_rw(req, res, res2, NULL); |
| } |
| |
| static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2) |
| { |
| struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
| |
| if (kiocb->ki_flags & IOCB_WRITE) |
| kiocb_end_write(req); |
| |
| if (res != -EAGAIN && res != req->result) |
| req_set_fail_links(req); |
| |
| WRITE_ONCE(req->result, res); |
| /* order with io_poll_complete() checking ->result */ |
| smp_wmb(); |
| WRITE_ONCE(req->iopoll_completed, 1); |
| } |
| |
| /* |
| * After the iocb has been issued, it's safe to be found on the poll list. |
| * Adding the kiocb to the list AFTER submission ensures that we don't |
| * find it from a io_iopoll_getevents() thread before the issuer is done |
| * accessing the kiocb cookie. |
| */ |
| static void io_iopoll_req_issued(struct io_kiocb *req) |
| { |
| struct io_ring_ctx *ctx = req->ctx; |
| |
| /* |
| * Track whether we have multiple files in our lists. This will impact |
| * how we do polling eventually, not spinning if we're on potentially |
| * different devices. |
| */ |
| if (list_empty(&ctx->iopoll_list)) { |
| ctx->poll_multi_file = false; |
| } else if (!ctx->poll_multi_file) { |
| struct io_kiocb *list_req; |
| |
| list_req = list_first_entry(&ctx->iopoll_list, struct io_kiocb, |
| inflight_entry); |
| if (list_req->file != req->file) |
| ctx->poll_multi_file = true; |
| } |
| |
| /* |
| * For fast devices, IO may have already completed. If it has, add |
| * it to the front so we find it first. |
| */ |
| if (READ_ONCE(req->iopoll_completed)) |
| list_add(&req->inflight_entry, &ctx->iopoll_list); |
| else |
| list_add_tail(&req->inflight_entry, &ctx->iopoll_list); |
| |
| if ((ctx->flags & IORING_SETUP_SQPOLL) && |
| wq_has_sleeper(&ctx->sq_data->wait)) |
| wake_up(&ctx->sq_data->wait); |
| } |
| |
| static void __io_state_file_put(struct io_submit_state *state) |
| { |
| if (state->has_refs) |
| fput_many(state->file, state->has_refs); |
| state->file = NULL; |
| } |
| |
| static inline void io_state_file_put(struct io_submit_state *state) |
| { |
| if (state->file) |
| __io_state_file_put(state); |
| } |
| |
| /* |
| * Get as many references to a file as we have IOs left in this submission, |
| * assuming most submissions are for one file, or at least that each file |
| * has more than one submission. |
| */ |
| static struct file *__io_file_get(struct io_submit_state *state, int fd) |
| { |
| if (!state) |
| return fget(fd); |
| |
| if (state->file) { |
| if (state->fd == fd) { |
| state->has_refs--; |
| return state->file; |
| } |
| __io_state_file_put(state); |
| } |
| state->file = fget_many(fd, state->ios_left); |
| if (!state->file) |
| return NULL; |
| |
| state->fd = fd; |
| state->has_refs = state->ios_left - 1; |
| return state->file; |
| } |
| |
| static bool io_bdev_nowait(struct block_device *bdev) |
| { |
| #ifdef CONFIG_BLOCK |
| return !bdev || blk_queue_nowait(bdev_get_queue(bdev)); |
| #else |
| return true; |
| #endif |
| } |
| |
| /* |
| * If we tracked the file through the SCM inflight mechanism, we could support |
| * any file. For now, just ensure that anything potentially problematic is done |
| * inline. |
| */ |
| static bool io_file_supports_async(struct file *file, int rw) |
| { |
| umode_t mode = file_inode(file)->i_mode; |
| |
| if (S_ISBLK(mode)) { |
| if (io_bdev_nowait(file->f_inode->i_bdev)) |
| return true; |
| return false; |
| } |
| if (S_ISSOCK(mode)) |
| return true; |
| if (S_ISREG(mode)) { |
| if (io_bdev_nowait(file->f_inode->i_sb->s_bdev) && |
| file->f_op != &io_uring_fops) |
| return true; |
| return false; |
| } |
| |
| /* any ->read/write should understand O_NONBLOCK */ |
| if (file->f_flags & O_NONBLOCK) |
| return true; |
| |
| if (!(file->f_mode & FMODE_NOWAIT)) |
| return false; |
| |
| if (rw == READ) |
| return file->f_op->read_iter != NULL; |
| |
| return file->f_op->write_iter != NULL; |
| } |
| |
| static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| { |
| struct io_ring_ctx *ctx = req->ctx; |
| struct kiocb *kiocb = &req->rw.kiocb; |
| unsigned ioprio; |
| int ret; |
| |
| if (S_ISREG(file_inode(req->file)->i_mode)) |
| req->flags |= REQ_F_ISREG; |
| |
| kiocb->ki_pos = READ_ONCE(sqe->off); |
| if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) { |
| req->flags |= REQ_F_CUR_POS; |
| kiocb->ki_pos = req->file->f_pos; |
| } |
| kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp)); |
| kiocb->ki_flags = iocb_flags(kiocb->ki_filp); |
| ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags)); |
| if (unlikely(ret)) |
| return ret; |
| |
| ioprio = READ_ONCE(sqe->ioprio); |
| if (ioprio) { |
| ret = ioprio_check_cap(ioprio); |
| if (ret) |
| return ret; |
| |
| kiocb->ki_ioprio = ioprio; |
| } else |
| kiocb->ki_ioprio = get_current_ioprio(); |
| |
| /* don't allow async punt if RWF_NOWAIT was requested */ |
| if (kiocb->ki_flags & IOCB_NOWAIT) |
| req->flags |= REQ_F_NOWAIT; |
| |
| if (ctx->flags & IORING_SETUP_IOPOLL) { |
| if (!(kiocb->ki_flags & IOCB_DIRECT) || |
| !kiocb->ki_filp->f_op->iopoll) |
| return -EOPNOTSUPP; |
| |
| kiocb->ki_flags |= IOCB_HIPRI; |
| kiocb->ki_complete = io_complete_rw_iopoll; |
| req->iopoll_completed = 0; |
| } else { |
| if (kiocb->ki_flags & IOCB_HIPRI) |
| return -EINVAL; |
| kiocb->ki_complete = io_complete_rw; |
| } |
| |
| req->rw.addr = READ_ONCE(sqe->addr); |
| req->rw.len = READ_ONCE(sqe->len); |
| req->buf_index = READ_ONCE(sqe->buf_index); |
| return 0; |
| } |
| |
| static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret) |
| { |
| switch (ret) { |
| case -EIOCBQUEUED: |
| break; |
| case -ERESTARTSYS: |
| case -ERESTARTNOINTR: |
| case -ERESTARTNOHAND: |
| case -ERESTART_RESTARTBLOCK: |
| /* |
| * We can't just restart the syscall, since previously |
| * submitted sqes may already be in progress. Just fail this |
| * IO with EINTR. |
| */ |
| ret = -EINTR; |
| fallthrough; |
| default: |
| kiocb->ki_complete(kiocb, ret, 0); |
| } |
| } |
| |
| static void kiocb_done(struct kiocb *kiocb, ssize_t ret, |
| struct io_comp_state *cs) |
| { |
| struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb); |
| struct io_async_rw *io = req->async_data; |
| |
| /* add previously done IO, if any */ |
| if (io && io->bytes_done > 0) { |
| if (ret < 0) |
| ret = io->bytes_done; |
| else |
| ret += io->bytes_done; |
| } |
| |
| if (req->flags & REQ_F_CUR_POS) |
| req->file->f_pos = kiocb->ki_pos; |
| if (ret >= 0 && kiocb->ki_complete == io_complete_rw) |
| __io_complete_rw(req, ret, 0, cs); |
| else |
| io_rw_done(kiocb, ret); |
| } |
| |
| static ssize_t io_import_fixed(struct io_kiocb *req, int rw, |
| struct iov_iter *iter) |
| { |
| struct io_ring_ctx *ctx = req->ctx; |
| size_t len = req->rw.len; |
| struct io_mapped_ubuf *imu; |
| u16 index, buf_index = req->buf_index; |
| size_t offset; |
| u64 buf_addr; |
| |
| if (unlikely(buf_index >= ctx->nr_user_bufs)) |
| return -EFAULT; |
| index = array_index_nospec(buf_index, ctx->nr_user_bufs); |
| imu = &ctx->user_bufs[index]; |
| buf_addr = req->rw.addr; |
| |
| /* overflow */ |
| if (buf_addr + len < buf_addr) |
| return -EFAULT; |
| /* not inside the mapped region */ |
| if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len) |
| return -EFAULT; |
| |
| /* |
| * May not be a start of buffer, set size appropriately |
| * and advance us to the beginning. |
| */ |
| offset = buf_addr - imu->ubuf; |
| iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len); |
| |
| if (offset) { |
| /* |
| * Don't use iov_iter_advance() here, as it's really slow for |
| * using the latter parts of a big fixed buffer - it iterates |
| * over each segment manually. We can cheat a bit here, because |
| * we know that: |
| * |
| * 1) it's a BVEC iter, we set it up |
| * 2) all bvecs are PAGE_SIZE in size, except potentially the |
| * first and last bvec |
| * |
| * So just find our index, and adjust the iterator afterwards. |
| * If the offset is within the first bvec (or the whole first |
| * bvec, just use iov_iter_advance(). This makes it easier |
| * since we can just skip the first segment, which may not |
| * be PAGE_SIZE aligned. |
| */ |
| const struct bio_vec *bvec = imu->bvec; |
| |
| if (offset <= bvec->bv_len) { |
| iov_iter_advance(iter, offset); |
| } else { |
| unsigned long seg_skip; |
| |
| /* skip first vec */ |
| offset -= bvec->bv_len; |
| seg_skip = 1 + (offset >> PAGE_SHIFT); |
| |
| iter->bvec = bvec + seg_skip; |
| iter->nr_segs -= seg_skip; |
| iter->count -= bvec->bv_len + offset; |
| iter->iov_offset = offset & ~PAGE_MASK; |
| } |
| } |
| |
| return len; |
| } |
| |
| static void io_ring_submit_unlock(struct io_ring_ctx *ctx, bool needs_lock) |
| { |
| if (needs_lock) |
| mutex_unlock(&ctx->uring_lock); |
| } |
| |
| static void io_ring_submit_lock(struct io_ring_ctx *ctx, bool needs_lock) |
| { |
| /* |
| * "Normal" inline submissions always hold the uring_lock, since we |
| * grab it from the system call. Same is true for the SQPOLL offload. |
| * The only exception is when we've detached the request and issue it |
| * from an async worker thread, grab the lock for that case. |
| */ |
| if (needs_lock) |
| mutex_lock(&ctx->uring_lock); |
| } |
| |
| static struct io_buffer *io_buffer_select(struct io_kiocb *req, size_t *len, |
| int bgid, struct io_buffer *kbuf, |
| bool needs_lock) |
| { |
| struct io_buffer *head; |
| |
| if (req->flags & REQ_F_BUFFER_SELECTED) |
| return kbuf; |
| |
| io_ring_submit_lock(req->ctx, needs_lock); |
| |
| lockdep_assert_held(&req->ctx->uring_lock); |
| |
| head = xa_load(&req->ctx->io_buffers, bgid); |
| if (head) { |
| if (!list_empty(&head->list)) { |
| kbuf = list_last_entry(&head->list, struct io_buffer, |
| list); |
| list_del(&kbuf->list); |
| } else { |
| kbuf = head; |
| xa_erase(&req->ctx->io_buffers, bgid); |
| } |
| if (*len > kbuf->len) |
| *len = kbuf->len; |
| } else { |
| kbuf = ERR_PTR(-ENOBUFS); |
| } |
| |
| io_ring_submit_unlock(req->ctx, needs_lock); |
| |
| return kbuf; |
| } |
| |
| static void __user *io_rw_buffer_select(struct io_kiocb *req, size_t *len, |
| bool needs_lock) |
| { |
| struct io_buffer *kbuf; |
| u16 bgid; |
| |
| kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; |
| bgid = req->buf_index; |
| kbuf = io_buffer_select(req, len, bgid, kbuf, needs_lock); |
| if (IS_ERR(kbuf)) |
| return kbuf; |
| req->rw.addr = (u64) (unsigned long) kbuf; |
| req->flags |= REQ_F_BUFFER_SELECTED; |
| return u64_to_user_ptr(kbuf->addr); |
| } |
| |
| #ifdef CONFIG_COMPAT |
| static ssize_t io_compat_import(struct io_kiocb *req, struct iovec *iov, |
| bool needs_lock) |
| { |
| struct compat_iovec __user *uiov; |
| compat_ssize_t clen; |
| void __user *buf; |
| ssize_t len; |
| |
| uiov = u64_to_user_ptr(req->rw.addr); |
| if (!access_ok(uiov, sizeof(*uiov))) |
| return -EFAULT; |
| if (__get_user(clen, &uiov->iov_len)) |
| return -EFAULT; |
| if (clen < 0) |
| return -EINVAL; |
| |
| len = clen; |
| buf = io_rw_buffer_select(req, &len, needs_lock); |
| if (IS_ERR(buf)) |
| return PTR_ERR(buf); |
| iov[0].iov_base = buf; |
| iov[0].iov_len = (compat_size_t) len; |
| return 0; |
| } |
| #endif |
| |
| static ssize_t __io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, |
| bool needs_lock) |
| { |
| struct iovec __user *uiov = u64_to_user_ptr(req->rw.addr); |
| void __user *buf; |
| ssize_t len; |
| |
| if (copy_from_user(iov, uiov, sizeof(*uiov))) |
| return -EFAULT; |
| |
| len = iov[0].iov_len; |
| if (len < 0) |
| return -EINVAL; |
| buf = io_rw_buffer_select(req, &len, needs_lock); |
| if (IS_ERR(buf)) |
| return PTR_ERR(buf); |
| iov[0].iov_base = buf; |
| iov[0].iov_len = len; |
| return 0; |
| } |
| |
| static ssize_t io_iov_buffer_select(struct io_kiocb *req, struct iovec *iov, |
| bool needs_lock) |
| { |
| if (req->flags & REQ_F_BUFFER_SELECTED) { |
| struct io_buffer *kbuf; |
| |
| kbuf = (struct io_buffer *) (unsigned long) req->rw.addr; |
| iov[0].iov_base = u64_to_user_ptr(kbuf->addr); |
| iov[0].iov_len = kbuf->len; |
| return 0; |
| } |
| if (req->rw.len != 1) |
| return -EINVAL; |
| |
| #ifdef CONFIG_COMPAT |
| if (req->ctx->compat) |
| return io_compat_import(req, iov, needs_lock); |
| #endif |
| |
| return __io_iov_buffer_select(req, iov, needs_lock); |
| } |
| |
| static ssize_t __io_import_iovec(int rw, struct io_kiocb *req, |
| struct iovec **iovec, struct iov_iter *iter, |
| bool needs_lock) |
| { |
| void __user *buf = u64_to_user_ptr(req->rw.addr); |
| size_t sqe_len = req->rw.len; |
| ssize_t ret; |
| u8 opcode; |
| |
| opcode = req->opcode; |
| if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) { |
| *iovec = NULL; |
| return io_import_fixed(req, rw, iter); |
| } |
| |
| /* buffer index only valid with fixed read/write, or buffer select */ |
| if (req->buf_index && !(req->flags & REQ_F_BUFFER_SELECT)) |
| return -EINVAL; |
| |
| if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) { |
| if (req->flags & REQ_F_BUFFER_SELECT) { |
| buf = io_rw_buffer_select(req, &sqe_len, needs_lock); |
| if (IS_ERR(buf)) |
| return PTR_ERR(buf); |
| req->rw.len = sqe_len; |
| } |
| |
| ret = import_single_range(rw, buf, sqe_len, *iovec, iter); |
| *iovec = NULL; |
| return ret; |
| } |
| |
| if (req->flags & REQ_F_BUFFER_SELECT) { |
| ret = io_iov_buffer_select(req, *iovec, needs_lock); |
| if (!ret) { |
| ret = (*iovec)->iov_len; |
| iov_iter_init(iter, rw, *iovec, 1, ret); |
| } |
| *iovec = NULL; |
| return ret; |
| } |
| |
| return __import_iovec(rw, buf, sqe_len,
|