blob: e54bd469d53ad1bd44a7b12ae0b7691201e18058 [file] [log] [blame]
Jens Axboe2b188cc2019-01-07 10:46:33 -07001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Shared application/kernel submission and completion ring pairs, for
4 * supporting fast/efficient IO.
5 *
6 * A note on the read/write ordering memory barriers that are matched between
Stefan Bühler1e84b972019-04-24 23:54:16 +02007 * the application and kernel side.
8 *
9 * After the application reads the CQ ring tail, it must use an
10 * appropriate smp_rmb() to pair with the smp_wmb() the kernel uses
11 * before writing the tail (using smp_load_acquire to read the tail will
12 * do). It also needs a smp_mb() before updating CQ head (ordering the
13 * entry load(s) with the head store), pairing with an implicit barrier
14 * through a control-dependency in io_get_cqring (smp_store_release to
15 * store head will do). Failure to do so could lead to reading invalid
16 * CQ entries.
17 *
18 * Likewise, the application must use an appropriate smp_wmb() before
19 * writing the SQ tail (ordering SQ entry stores with the tail store),
20 * which pairs with smp_load_acquire in io_get_sqring (smp_store_release
21 * to store the tail will do). And it needs a barrier ordering the SQ
22 * head load before writing new SQ entries (smp_load_acquire to read
23 * head will do).
24 *
25 * When using the SQ poll thread (IORING_SETUP_SQPOLL), the application
26 * needs to check the SQ flags for IORING_SQ_NEED_WAKEUP *after*
27 * updating the SQ tail; a full memory barrier smp_mb() is needed
28 * between.
Jens Axboe2b188cc2019-01-07 10:46:33 -070029 *
30 * Also see the examples in the liburing library:
31 *
32 * git://git.kernel.dk/liburing
33 *
34 * io_uring also uses READ/WRITE_ONCE() for _any_ store or load that happens
35 * from data shared between the kernel and application. This is done both
36 * for ordering purposes, but also to ensure that once a value is loaded from
37 * data that the application could potentially modify, it remains stable.
38 *
39 * Copyright (C) 2018-2019 Jens Axboe
Christoph Hellwigc992fe22019-01-11 09:43:02 -070040 * Copyright (c) 2018-2019 Christoph Hellwig
Jens Axboe2b188cc2019-01-07 10:46:33 -070041 */
42#include <linux/kernel.h>
43#include <linux/init.h>
44#include <linux/errno.h>
45#include <linux/syscalls.h>
46#include <linux/compat.h>
47#include <linux/refcount.h>
48#include <linux/uio.h>
49
50#include <linux/sched/signal.h>
51#include <linux/fs.h>
52#include <linux/file.h>
53#include <linux/fdtable.h>
54#include <linux/mm.h>
55#include <linux/mman.h>
56#include <linux/mmu_context.h>
57#include <linux/percpu.h>
58#include <linux/slab.h>
Jens Axboe6c271ce2019-01-10 11:22:30 -070059#include <linux/kthread.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070060#include <linux/blkdev.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070061#include <linux/bvec.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070062#include <linux/net.h>
63#include <net/sock.h>
64#include <net/af_unix.h>
Jens Axboe6b063142019-01-10 22:13:58 -070065#include <net/scm.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070066#include <linux/anon_inodes.h>
67#include <linux/sched/mm.h>
68#include <linux/uaccess.h>
69#include <linux/nospec.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070070#include <linux/sizes.h>
71#include <linux/hugetlb.h>
Jens Axboeaa4c3962019-11-29 10:14:00 -070072#include <linux/highmem.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070073
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020074#define CREATE_TRACE_POINTS
75#include <trace/events/io_uring.h>
76
Jens Axboe2b188cc2019-01-07 10:46:33 -070077#include <uapi/linux/io_uring.h>
78
79#include "internal.h"
Jens Axboe561fb042019-10-24 07:25:42 -060080#include "io-wq.h"
Jens Axboe2b188cc2019-01-07 10:46:33 -070081
Daniel Xu5277dea2019-09-14 14:23:45 -070082#define IORING_MAX_ENTRIES 32768
Jens Axboe33a107f2019-10-04 12:10:03 -060083#define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES)
Jens Axboe65e19f52019-10-26 07:20:21 -060084
85/*
86 * Shift of 9 is 512 entries, or exactly one page on 64-bit archs
87 */
88#define IORING_FILE_TABLE_SHIFT 9
89#define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT)
90#define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1)
91#define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE)
Jens Axboe2b188cc2019-01-07 10:46:33 -070092
93struct io_uring {
94 u32 head ____cacheline_aligned_in_smp;
95 u32 tail ____cacheline_aligned_in_smp;
96};
97
Stefan Bühler1e84b972019-04-24 23:54:16 +020098/*
Hristo Venev75b28af2019-08-26 17:23:46 +000099 * This data is shared with the application through the mmap at offsets
100 * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200101 *
102 * The offsets to the member fields are published through struct
103 * io_sqring_offsets when calling io_uring_setup.
104 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000105struct io_rings {
Stefan Bühler1e84b972019-04-24 23:54:16 +0200106 /*
107 * Head and tail offsets into the ring; the offsets need to be
108 * masked to get valid indices.
109 *
Hristo Venev75b28af2019-08-26 17:23:46 +0000110 * The kernel controls head of the sq ring and the tail of the cq ring,
111 * and the application controls tail of the sq ring and the head of the
112 * cq ring.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200113 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000114 struct io_uring sq, cq;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200115 /*
Hristo Venev75b28af2019-08-26 17:23:46 +0000116 * Bitmasks to apply to head and tail offsets (constant, equals
Stefan Bühler1e84b972019-04-24 23:54:16 +0200117 * ring_entries - 1)
118 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000119 u32 sq_ring_mask, cq_ring_mask;
120 /* Ring sizes (constant, power of 2) */
121 u32 sq_ring_entries, cq_ring_entries;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200122 /*
123 * Number of invalid entries dropped by the kernel due to
124 * invalid index stored in array
125 *
126 * Written by the kernel, shouldn't be modified by the
127 * application (i.e. get number of "new events" by comparing to
128 * cached value).
129 *
130 * After a new SQ head value was read by the application this
131 * counter includes all submissions that were dropped reaching
132 * the new SQ head (and possibly more).
133 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000134 u32 sq_dropped;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200135 /*
136 * Runtime flags
137 *
138 * Written by the kernel, shouldn't be modified by the
139 * application.
140 *
141 * The application needs a full memory barrier before checking
142 * for IORING_SQ_NEED_WAKEUP after updating the sq tail.
143 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000144 u32 sq_flags;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200145 /*
146 * Number of completion events lost because the queue was full;
147 * this should be avoided by the application by making sure
LimingWu0b4295b2019-12-05 20:18:18 +0800148 * there are not more requests pending than there is space in
Stefan Bühler1e84b972019-04-24 23:54:16 +0200149 * the completion queue.
150 *
151 * Written by the kernel, shouldn't be modified by the
152 * application (i.e. get number of "new events" by comparing to
153 * cached value).
154 *
155 * As completion events come in out of order this counter is not
156 * ordered with any other data.
157 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000158 u32 cq_overflow;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200159 /*
160 * Ring buffer of completion events.
161 *
162 * The kernel writes completion events fresh every time they are
163 * produced, so the application is allowed to modify pending
164 * entries.
165 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000166 struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700167};
168
Jens Axboeedafcce2019-01-09 09:16:05 -0700169struct io_mapped_ubuf {
170 u64 ubuf;
171 size_t len;
172 struct bio_vec *bvec;
173 unsigned int nr_bvecs;
174};
175
Jens Axboe65e19f52019-10-26 07:20:21 -0600176struct fixed_file_table {
177 struct file **files;
Jens Axboe31b51512019-01-18 22:56:34 -0700178};
179
Jens Axboe2b188cc2019-01-07 10:46:33 -0700180struct io_ring_ctx {
181 struct {
182 struct percpu_ref refs;
183 } ____cacheline_aligned_in_smp;
184
185 struct {
186 unsigned int flags;
187 bool compat;
188 bool account_mem;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700189 bool cq_overflow_flushed;
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +0300190 bool drain_next;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700191
Hristo Venev75b28af2019-08-26 17:23:46 +0000192 /*
193 * Ring buffer of indices into array of io_uring_sqe, which is
194 * mmapped by the application using the IORING_OFF_SQES offset.
195 *
196 * This indirection could e.g. be used to assign fixed
197 * io_uring_sqe entries to operations and only submit them to
198 * the queue when needed.
199 *
200 * The kernel modifies neither the indices array nor the entries
201 * array.
202 */
203 u32 *sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700204 unsigned cached_sq_head;
205 unsigned sq_entries;
206 unsigned sq_mask;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700207 unsigned sq_thread_idle;
Jens Axboe498ccd92019-10-25 10:04:25 -0600208 unsigned cached_sq_dropped;
Jens Axboe206aefd2019-11-07 18:27:42 -0700209 atomic_t cached_cq_overflow;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700210 struct io_uring_sqe *sq_sqes;
Jens Axboede0617e2019-04-06 21:51:27 -0600211
212 struct list_head defer_list;
Jens Axboe5262f562019-09-17 12:26:57 -0600213 struct list_head timeout_list;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700214 struct list_head cq_overflow_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700215
Jens Axboefcb323c2019-10-24 12:39:47 -0600216 wait_queue_head_t inflight_wait;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700217 } ____cacheline_aligned_in_smp;
218
Hristo Venev75b28af2019-08-26 17:23:46 +0000219 struct io_rings *rings;
220
Jens Axboe2b188cc2019-01-07 10:46:33 -0700221 /* IO offload */
Jens Axboe561fb042019-10-24 07:25:42 -0600222 struct io_wq *io_wq;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700223 struct task_struct *sqo_thread; /* if using sq thread polling */
224 struct mm_struct *sqo_mm;
225 wait_queue_head_t sqo_wait;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700226
Jens Axboe6b063142019-01-10 22:13:58 -0700227 /*
228 * If used, fixed file set. Writers must ensure that ->refs is dead,
229 * readers must ensure that ->refs is alive as long as the file* is
230 * used. Only updated through io_uring_register(2).
231 */
Jens Axboe65e19f52019-10-26 07:20:21 -0600232 struct fixed_file_table *file_table;
Jens Axboe6b063142019-01-10 22:13:58 -0700233 unsigned nr_user_files;
234
Jens Axboeedafcce2019-01-09 09:16:05 -0700235 /* if used, fixed mapped user buffers */
236 unsigned nr_user_bufs;
237 struct io_mapped_ubuf *user_bufs;
238
Jens Axboe2b188cc2019-01-07 10:46:33 -0700239 struct user_struct *user;
240
Jens Axboe0b8c0ec2019-12-02 08:50:00 -0700241 const struct cred *creds;
Jens Axboe181e4482019-11-25 08:52:30 -0700242
Jens Axboe206aefd2019-11-07 18:27:42 -0700243 /* 0 is for ctx quiesce/reinit/free, 1 is for sqo_thread started */
244 struct completion *completions;
245
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700246 /* if all else fails... */
247 struct io_kiocb *fallback_req;
248
Jens Axboe206aefd2019-11-07 18:27:42 -0700249#if defined(CONFIG_UNIX)
250 struct socket *ring_sock;
251#endif
252
253 struct {
254 unsigned cached_cq_tail;
255 unsigned cq_entries;
256 unsigned cq_mask;
257 atomic_t cq_timeouts;
258 struct wait_queue_head cq_wait;
259 struct fasync_struct *cq_fasync;
260 struct eventfd_ctx *cq_ev_fd;
261 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700262
263 struct {
264 struct mutex uring_lock;
265 wait_queue_head_t wait;
266 } ____cacheline_aligned_in_smp;
267
268 struct {
269 spinlock_t completion_lock;
Jens Axboedef596e2019-01-09 08:59:42 -0700270 bool poll_multi_file;
271 /*
272 * ->poll_list is protected by the ctx->uring_lock for
273 * io_uring instances that don't use IORING_SETUP_SQPOLL.
274 * For SQPOLL, only the single threaded io_sq_thread() will
275 * manipulate the list, hence no extra locking is needed there.
276 */
277 struct list_head poll_list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700278 struct hlist_head *cancel_hash;
279 unsigned cancel_hash_bits;
Jens Axboefcb323c2019-10-24 12:39:47 -0600280
281 spinlock_t inflight_lock;
282 struct list_head inflight_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700283 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700284};
285
Jens Axboe09bb8392019-03-13 12:39:28 -0600286/*
287 * First field must be the file pointer in all the
288 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
289 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700290struct io_poll_iocb {
291 struct file *file;
292 struct wait_queue_head *head;
293 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600294 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700295 bool canceled;
Jens Axboe392edb42019-12-09 17:52:20 -0700296 struct wait_queue_entry wait;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700297};
298
Jens Axboead8a48a2019-11-15 08:49:11 -0700299struct io_timeout_data {
300 struct io_kiocb *req;
301 struct hrtimer timer;
302 struct timespec64 ts;
303 enum hrtimer_mode mode;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +0300304 u32 seq_offset;
Jens Axboead8a48a2019-11-15 08:49:11 -0700305};
306
Jens Axboef499a022019-12-02 16:28:46 -0700307struct io_async_connect {
308 struct sockaddr_storage address;
309};
310
Jens Axboe03b12302019-12-02 18:50:25 -0700311struct io_async_msghdr {
312 struct iovec fast_iov[UIO_FASTIOV];
313 struct iovec *iov;
314 struct sockaddr __user *uaddr;
315 struct msghdr msg;
316};
317
Jens Axboef67676d2019-12-02 11:03:47 -0700318struct io_async_rw {
319 struct iovec fast_iov[UIO_FASTIOV];
320 struct iovec *iov;
321 ssize_t nr_segs;
322 ssize_t size;
323};
324
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700325struct io_async_ctx {
326 struct io_uring_sqe sqe;
Jens Axboef67676d2019-12-02 11:03:47 -0700327 union {
328 struct io_async_rw rw;
Jens Axboe03b12302019-12-02 18:50:25 -0700329 struct io_async_msghdr msg;
Jens Axboef499a022019-12-02 16:28:46 -0700330 struct io_async_connect connect;
Jens Axboe2d283902019-12-04 11:08:05 -0700331 struct io_timeout_data timeout;
Jens Axboef67676d2019-12-02 11:03:47 -0700332 };
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700333};
334
Jens Axboe09bb8392019-03-13 12:39:28 -0600335/*
336 * NOTE! Each of the iocb union members has the file pointer
337 * as the first entry in their struct definition. So you can
338 * access the file pointer through any of the sub-structs,
339 * or directly as just 'ki_filp' in this struct.
340 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700341struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700342 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600343 struct file *file;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700344 struct kiocb rw;
345 struct io_poll_iocb poll;
346 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700347
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300348 const struct io_uring_sqe *sqe;
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700349 struct io_async_ctx *io;
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300350 struct file *ring_file;
351 int ring_fd;
352 bool has_user;
353 bool in_async;
354 bool needs_fixed_file;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700355
356 struct io_ring_ctx *ctx;
Jens Axboeeac406c2019-11-14 12:09:58 -0700357 union {
358 struct list_head list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700359 struct hlist_node hash_node;
Jens Axboeeac406c2019-11-14 12:09:58 -0700360 };
Jens Axboe9e645e112019-05-10 16:07:28 -0600361 struct list_head link_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700362 unsigned int flags;
Jens Axboec16361c2019-01-17 08:39:48 -0700363 refcount_t refs;
Stefan Bühler8449eed2019-04-27 20:34:19 +0200364#define REQ_F_NOWAIT 1 /* must not punt to workers */
Jens Axboedef596e2019-01-09 08:59:42 -0700365#define REQ_F_IOPOLL_COMPLETED 2 /* polled IO has completed */
Jens Axboe6b063142019-01-10 22:13:58 -0700366#define REQ_F_FIXED_FILE 4 /* ctx owns file */
Jens Axboe4d7dd462019-11-20 13:03:52 -0700367#define REQ_F_LINK_NEXT 8 /* already grabbed next link */
Stefan Bühlere2033e32019-05-11 19:08:01 +0200368#define REQ_F_IO_DRAIN 16 /* drain existing IO first */
369#define REQ_F_IO_DRAINED 32 /* drain done */
Jens Axboe9e645e112019-05-10 16:07:28 -0600370#define REQ_F_LINK 64 /* linked sqes */
Jens Axboe2665abf2019-11-05 12:40:47 -0700371#define REQ_F_LINK_TIMEOUT 128 /* has linked timeout */
Zhengyuan Liuf7b76ac2019-07-16 23:26:14 +0800372#define REQ_F_FAIL_LINK 256 /* fail rest of links */
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +0300373#define REQ_F_DRAIN_LINK 512 /* link should be fully drained */
Jens Axboe5262f562019-09-17 12:26:57 -0600374#define REQ_F_TIMEOUT 1024 /* timeout request */
Jens Axboe491381ce2019-10-17 09:20:46 -0600375#define REQ_F_ISREG 2048 /* regular file */
376#define REQ_F_MUST_PUNT 4096 /* must be punted even for NONBLOCK */
Jens Axboe93bd25b2019-11-11 23:34:31 -0700377#define REQ_F_TIMEOUT_NOSEQ 8192 /* no timeout sequence */
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800378#define REQ_F_INFLIGHT 16384 /* on inflight list */
379#define REQ_F_COMP_LOCKED 32768 /* completion under lock */
Jens Axboe4e88d6e2019-12-07 20:59:47 -0700380#define REQ_F_HARDLINK 65536 /* doesn't sever on completion < 0 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700381 u64 user_data;
Jens Axboe9e645e112019-05-10 16:07:28 -0600382 u32 result;
Jens Axboede0617e2019-04-06 21:51:27 -0600383 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700384
Jens Axboefcb323c2019-10-24 12:39:47 -0600385 struct list_head inflight_entry;
386
Jens Axboe561fb042019-10-24 07:25:42 -0600387 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700388};
389
390#define IO_PLUG_THRESHOLD 2
Jens Axboedef596e2019-01-09 08:59:42 -0700391#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700392
Jens Axboe9a56a232019-01-09 09:06:50 -0700393struct io_submit_state {
394 struct blk_plug plug;
395
396 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700397 * io_kiocb alloc cache
398 */
399 void *reqs[IO_IOPOLL_BATCH];
400 unsigned int free_reqs;
401 unsigned int cur_req;
402
403 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700404 * File reference cache
405 */
406 struct file *file;
407 unsigned int fd;
408 unsigned int has_refs;
409 unsigned int used_refs;
410 unsigned int ios_left;
411};
412
Jens Axboe561fb042019-10-24 07:25:42 -0600413static void io_wq_submit_work(struct io_wq_work **workptr);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700414static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liu4fe2c962019-09-09 20:50:40 +0800415static void __io_free_req(struct io_kiocb *req);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800416static void io_put_req(struct io_kiocb *req);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700417static void io_double_put_req(struct io_kiocb *req);
Jens Axboe978db572019-11-14 22:39:04 -0700418static void __io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700419static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
420static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboede0617e2019-04-06 21:51:27 -0600421
Jens Axboe2b188cc2019-01-07 10:46:33 -0700422static struct kmem_cache *req_cachep;
423
424static const struct file_operations io_uring_fops;
425
426struct sock *io_uring_get_socket(struct file *file)
427{
428#if defined(CONFIG_UNIX)
429 if (file->f_op == &io_uring_fops) {
430 struct io_ring_ctx *ctx = file->private_data;
431
432 return ctx->ring_sock->sk;
433 }
434#endif
435 return NULL;
436}
437EXPORT_SYMBOL(io_uring_get_socket);
438
439static void io_ring_ctx_ref_free(struct percpu_ref *ref)
440{
441 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
442
Jens Axboe206aefd2019-11-07 18:27:42 -0700443 complete(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700444}
445
446static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
447{
448 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -0700449 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700450
451 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
452 if (!ctx)
453 return NULL;
454
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700455 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
456 if (!ctx->fallback_req)
457 goto err;
458
Jens Axboe206aefd2019-11-07 18:27:42 -0700459 ctx->completions = kmalloc(2 * sizeof(struct completion), GFP_KERNEL);
460 if (!ctx->completions)
461 goto err;
462
Jens Axboe78076bb2019-12-04 19:56:40 -0700463 /*
464 * Use 5 bits less than the max cq entries, that should give us around
465 * 32 entries per hash list if totally full and uniformly spread.
466 */
467 hash_bits = ilog2(p->cq_entries);
468 hash_bits -= 5;
469 if (hash_bits <= 0)
470 hash_bits = 1;
471 ctx->cancel_hash_bits = hash_bits;
472 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
473 GFP_KERNEL);
474 if (!ctx->cancel_hash)
475 goto err;
476 __hash_init(ctx->cancel_hash, 1U << hash_bits);
477
Roman Gushchin21482892019-05-07 10:01:48 -0700478 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -0700479 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
480 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700481
482 ctx->flags = p->flags;
483 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700484 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe206aefd2019-11-07 18:27:42 -0700485 init_completion(&ctx->completions[0]);
486 init_completion(&ctx->completions[1]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700487 mutex_init(&ctx->uring_lock);
488 init_waitqueue_head(&ctx->wait);
489 spin_lock_init(&ctx->completion_lock);
Jens Axboedef596e2019-01-09 08:59:42 -0700490 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboede0617e2019-04-06 21:51:27 -0600491 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -0600492 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -0600493 init_waitqueue_head(&ctx->inflight_wait);
494 spin_lock_init(&ctx->inflight_lock);
495 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700496 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -0700497err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700498 if (ctx->fallback_req)
499 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe206aefd2019-11-07 18:27:42 -0700500 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -0700501 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -0700502 kfree(ctx);
503 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700504}
505
Bob Liu9d858b22019-11-13 18:06:25 +0800506static inline bool __req_need_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -0600507{
Jackie Liua197f662019-11-08 08:09:12 -0700508 struct io_ring_ctx *ctx = req->ctx;
509
Jens Axboe498ccd92019-10-25 10:04:25 -0600510 return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped
511 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboede0617e2019-04-06 21:51:27 -0600512}
513
Bob Liu9d858b22019-11-13 18:06:25 +0800514static inline bool req_need_defer(struct io_kiocb *req)
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600515{
Bob Liu9d858b22019-11-13 18:06:25 +0800516 if ((req->flags & (REQ_F_IO_DRAIN|REQ_F_IO_DRAINED)) == REQ_F_IO_DRAIN)
517 return __req_need_defer(req);
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600518
Bob Liu9d858b22019-11-13 18:06:25 +0800519 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600520}
521
522static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -0600523{
524 struct io_kiocb *req;
525
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600526 req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list);
Bob Liu9d858b22019-11-13 18:06:25 +0800527 if (req && !req_need_defer(req)) {
Jens Axboede0617e2019-04-06 21:51:27 -0600528 list_del_init(&req->list);
529 return req;
530 }
531
532 return NULL;
533}
534
Jens Axboe5262f562019-09-17 12:26:57 -0600535static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx)
536{
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600537 struct io_kiocb *req;
538
539 req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list);
Jens Axboe93bd25b2019-11-11 23:34:31 -0700540 if (req) {
541 if (req->flags & REQ_F_TIMEOUT_NOSEQ)
542 return NULL;
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800543 if (!__req_need_defer(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -0700544 list_del_init(&req->list);
545 return req;
546 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600547 }
548
549 return NULL;
Jens Axboe5262f562019-09-17 12:26:57 -0600550}
551
Jens Axboede0617e2019-04-06 21:51:27 -0600552static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700553{
Hristo Venev75b28af2019-08-26 17:23:46 +0000554 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700555
Hristo Venev75b28af2019-08-26 17:23:46 +0000556 if (ctx->cached_cq_tail != READ_ONCE(rings->cq.tail)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700557 /* order cqe stores with ring update */
Hristo Venev75b28af2019-08-26 17:23:46 +0000558 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700559
Jens Axboe2b188cc2019-01-07 10:46:33 -0700560 if (wq_has_sleeper(&ctx->cq_wait)) {
561 wake_up_interruptible(&ctx->cq_wait);
562 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
563 }
564 }
565}
566
Jens Axboe561fb042019-10-24 07:25:42 -0600567static inline bool io_sqe_needs_user(const struct io_uring_sqe *sqe)
Jens Axboe18d9be12019-09-10 09:13:05 -0600568{
Jens Axboe561fb042019-10-24 07:25:42 -0600569 u8 opcode = READ_ONCE(sqe->opcode);
570
571 return !(opcode == IORING_OP_READ_FIXED ||
572 opcode == IORING_OP_WRITE_FIXED);
573}
574
Jens Axboe94ae5e72019-11-14 19:39:52 -0700575static inline bool io_prep_async_work(struct io_kiocb *req,
576 struct io_kiocb **link)
Jens Axboe561fb042019-10-24 07:25:42 -0600577{
578 bool do_hashed = false;
Jens Axboe54a91f32019-09-10 09:15:04 -0600579
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300580 if (req->sqe) {
581 switch (req->sqe->opcode) {
Jens Axboe6cc47d12019-09-18 11:18:23 -0600582 case IORING_OP_WRITEV:
583 case IORING_OP_WRITE_FIXED:
Jens Axboe53108d42019-12-09 20:12:38 -0700584 /* only regular files should be hashed for writes */
585 if (req->flags & REQ_F_ISREG)
586 do_hashed = true;
Jens Axboe5f8fd2d32019-11-07 10:57:36 -0700587 /* fall-through */
588 case IORING_OP_READV:
589 case IORING_OP_READ_FIXED:
590 case IORING_OP_SENDMSG:
591 case IORING_OP_RECVMSG:
592 case IORING_OP_ACCEPT:
593 case IORING_OP_POLL_ADD:
Jens Axboef8e85cf2019-11-23 14:24:24 -0700594 case IORING_OP_CONNECT:
Jens Axboe5f8fd2d32019-11-07 10:57:36 -0700595 /*
596 * We know REQ_F_ISREG is not set on some of these
597 * opcodes, but this enables us to keep the check in
598 * just one place.
599 */
600 if (!(req->flags & REQ_F_ISREG))
601 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe6cc47d12019-09-18 11:18:23 -0600602 break;
603 }
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300604 if (io_sqe_needs_user(req->sqe))
Jens Axboe561fb042019-10-24 07:25:42 -0600605 req->work.flags |= IO_WQ_WORK_NEEDS_USER;
Jens Axboe54a91f32019-09-10 09:15:04 -0600606 }
607
Jens Axboe94ae5e72019-11-14 19:39:52 -0700608 *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -0600609 return do_hashed;
610}
611
Jackie Liua197f662019-11-08 08:09:12 -0700612static inline void io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -0600613{
Jackie Liua197f662019-11-08 08:09:12 -0700614 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe94ae5e72019-11-14 19:39:52 -0700615 struct io_kiocb *link;
616 bool do_hashed;
617
618 do_hashed = io_prep_async_work(req, &link);
Jens Axboe561fb042019-10-24 07:25:42 -0600619
620 trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work,
621 req->flags);
622 if (!do_hashed) {
623 io_wq_enqueue(ctx->io_wq, &req->work);
624 } else {
625 io_wq_enqueue_hashed(ctx->io_wq, &req->work,
626 file_inode(req->file));
627 }
Jens Axboe94ae5e72019-11-14 19:39:52 -0700628
629 if (link)
630 io_queue_linked_timeout(link);
Jens Axboe18d9be12019-09-10 09:13:05 -0600631}
632
Jens Axboe5262f562019-09-17 12:26:57 -0600633static void io_kill_timeout(struct io_kiocb *req)
634{
635 int ret;
636
Jens Axboe2d283902019-12-04 11:08:05 -0700637 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe5262f562019-09-17 12:26:57 -0600638 if (ret != -1) {
639 atomic_inc(&req->ctx->cq_timeouts);
Jens Axboe842f9612019-10-29 12:34:10 -0600640 list_del_init(&req->list);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700641 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800642 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -0600643 }
644}
645
646static void io_kill_timeouts(struct io_ring_ctx *ctx)
647{
648 struct io_kiocb *req, *tmp;
649
650 spin_lock_irq(&ctx->completion_lock);
651 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
652 io_kill_timeout(req);
653 spin_unlock_irq(&ctx->completion_lock);
654}
655
Jens Axboede0617e2019-04-06 21:51:27 -0600656static void io_commit_cqring(struct io_ring_ctx *ctx)
657{
658 struct io_kiocb *req;
659
Jens Axboe5262f562019-09-17 12:26:57 -0600660 while ((req = io_get_timeout_req(ctx)) != NULL)
661 io_kill_timeout(req);
662
Jens Axboede0617e2019-04-06 21:51:27 -0600663 __io_commit_cqring(ctx);
664
665 while ((req = io_get_deferred_req(ctx)) != NULL) {
666 req->flags |= REQ_F_IO_DRAINED;
Jackie Liua197f662019-11-08 08:09:12 -0700667 io_queue_async_work(req);
Jens Axboede0617e2019-04-06 21:51:27 -0600668 }
669}
670
Jens Axboe2b188cc2019-01-07 10:46:33 -0700671static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
672{
Hristo Venev75b28af2019-08-26 17:23:46 +0000673 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700674 unsigned tail;
675
676 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +0200677 /*
678 * writes to the cq entry need to come after reading head; the
679 * control dependency is enough as we're using WRITE_ONCE to
680 * fill the cq entry
681 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000682 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700683 return NULL;
684
685 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +0000686 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -0700687}
688
Jens Axboe8c838782019-03-12 15:48:16 -0600689static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
690{
691 if (waitqueue_active(&ctx->wait))
692 wake_up(&ctx->wait);
693 if (waitqueue_active(&ctx->sqo_wait))
694 wake_up(&ctx->sqo_wait);
Jens Axboe9b402842019-04-11 11:45:41 -0600695 if (ctx->cq_ev_fd)
696 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -0600697}
698
Jens Axboec4a2ed72019-11-21 21:01:26 -0700699/* Returns true if there are no backlogged entries after the flush */
700static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700701{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700702 struct io_rings *rings = ctx->rings;
703 struct io_uring_cqe *cqe;
704 struct io_kiocb *req;
705 unsigned long flags;
706 LIST_HEAD(list);
707
708 if (!force) {
709 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -0700710 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700711 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
712 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -0700713 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700714 }
715
716 spin_lock_irqsave(&ctx->completion_lock, flags);
717
718 /* if force is set, the ring is going away. always drop after that */
719 if (force)
720 ctx->cq_overflow_flushed = true;
721
Jens Axboec4a2ed72019-11-21 21:01:26 -0700722 cqe = NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700723 while (!list_empty(&ctx->cq_overflow_list)) {
724 cqe = io_get_cqring(ctx);
725 if (!cqe && !force)
726 break;
727
728 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
729 list);
730 list_move(&req->list, &list);
731 if (cqe) {
732 WRITE_ONCE(cqe->user_data, req->user_data);
733 WRITE_ONCE(cqe->res, req->result);
734 WRITE_ONCE(cqe->flags, 0);
735 } else {
736 WRITE_ONCE(ctx->rings->cq_overflow,
737 atomic_inc_return(&ctx->cached_cq_overflow));
738 }
739 }
740
741 io_commit_cqring(ctx);
742 spin_unlock_irqrestore(&ctx->completion_lock, flags);
743 io_cqring_ev_posted(ctx);
744
745 while (!list_empty(&list)) {
746 req = list_first_entry(&list, struct io_kiocb, list);
747 list_del(&req->list);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800748 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700749 }
Jens Axboec4a2ed72019-11-21 21:01:26 -0700750
751 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700752}
753
Jens Axboe78e19bb2019-11-06 15:21:34 -0700754static void io_cqring_fill_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700755{
Jens Axboe78e19bb2019-11-06 15:21:34 -0700756 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700757 struct io_uring_cqe *cqe;
758
Jens Axboe78e19bb2019-11-06 15:21:34 -0700759 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -0700760
Jens Axboe2b188cc2019-01-07 10:46:33 -0700761 /*
762 * If we can't get a cq entry, userspace overflowed the
763 * submission (by quite a lot). Increment the overflow count in
764 * the ring.
765 */
766 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700767 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -0700768 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700769 WRITE_ONCE(cqe->res, res);
770 WRITE_ONCE(cqe->flags, 0);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700771 } else if (ctx->cq_overflow_flushed) {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700772 WRITE_ONCE(ctx->rings->cq_overflow,
773 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700774 } else {
775 refcount_inc(&req->refs);
776 req->result = res;
777 list_add_tail(&req->list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700778 }
779}
780
Jens Axboe78e19bb2019-11-06 15:21:34 -0700781static void io_cqring_add_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700782{
Jens Axboe78e19bb2019-11-06 15:21:34 -0700783 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700784 unsigned long flags;
785
786 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700787 io_cqring_fill_event(req, res);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700788 io_commit_cqring(ctx);
789 spin_unlock_irqrestore(&ctx->completion_lock, flags);
790
Jens Axboe8c838782019-03-12 15:48:16 -0600791 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700792}
793
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700794static inline bool io_is_fallback_req(struct io_kiocb *req)
795{
796 return req == (struct io_kiocb *)
797 ((unsigned long) req->ctx->fallback_req & ~1UL);
798}
799
800static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
801{
802 struct io_kiocb *req;
803
804 req = ctx->fallback_req;
805 if (!test_and_set_bit_lock(0, (unsigned long *) ctx->fallback_req))
806 return req;
807
808 return NULL;
809}
810
Jens Axboe2579f912019-01-09 09:10:43 -0700811static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
812 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700813{
Jens Axboefd6fab22019-03-14 16:30:06 -0600814 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700815 struct io_kiocb *req;
816
817 if (!percpu_ref_tryget(&ctx->refs))
818 return NULL;
819
Jens Axboe2579f912019-01-09 09:10:43 -0700820 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -0600821 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -0700822 if (unlikely(!req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700823 goto fallback;
Jens Axboe2579f912019-01-09 09:10:43 -0700824 } else if (!state->free_reqs) {
825 size_t sz;
826 int ret;
827
828 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -0600829 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
830
831 /*
832 * Bulk alloc is all-or-nothing. If we fail to get a batch,
833 * retry single alloc to be on the safe side.
834 */
835 if (unlikely(ret <= 0)) {
836 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
837 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700838 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -0600839 ret = 1;
840 }
Jens Axboe2579f912019-01-09 09:10:43 -0700841 state->free_reqs = ret - 1;
842 state->cur_req = 1;
843 req = state->reqs[0];
844 } else {
845 req = state->reqs[state->cur_req];
846 state->free_reqs--;
847 state->cur_req++;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700848 }
849
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700850got_it:
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700851 req->io = NULL;
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300852 req->ring_file = NULL;
Jens Axboe60c112b2019-06-21 10:20:18 -0600853 req->file = NULL;
Jens Axboe2579f912019-01-09 09:10:43 -0700854 req->ctx = ctx;
855 req->flags = 0;
Jens Axboee65ef562019-03-12 10:16:44 -0600856 /* one is dropped after submission, the other at completion */
857 refcount_set(&req->refs, 2);
Jens Axboe9e645e112019-05-10 16:07:28 -0600858 req->result = 0;
Jens Axboe561fb042019-10-24 07:25:42 -0600859 INIT_IO_WORK(&req->work, io_wq_submit_work);
Jens Axboe2579f912019-01-09 09:10:43 -0700860 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700861fallback:
862 req = io_get_fallback_req(ctx);
863 if (req)
864 goto got_it;
Pavel Begunkov6805b322019-10-08 02:18:42 +0300865 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700866 return NULL;
867}
868
Jens Axboedef596e2019-01-09 08:59:42 -0700869static void io_free_req_many(struct io_ring_ctx *ctx, void **reqs, int *nr)
870{
871 if (*nr) {
872 kmem_cache_free_bulk(req_cachep, *nr, reqs);
Pavel Begunkov6805b322019-10-08 02:18:42 +0300873 percpu_ref_put_many(&ctx->refs, *nr);
Jens Axboedef596e2019-01-09 08:59:42 -0700874 *nr = 0;
875 }
876}
877
Jens Axboe9e645e112019-05-10 16:07:28 -0600878static void __io_free_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700879{
Jens Axboefcb323c2019-10-24 12:39:47 -0600880 struct io_ring_ctx *ctx = req->ctx;
881
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700882 if (req->io)
883 kfree(req->io);
Jens Axboe09bb8392019-03-13 12:39:28 -0600884 if (req->file && !(req->flags & REQ_F_FIXED_FILE))
885 fput(req->file);
Jens Axboefcb323c2019-10-24 12:39:47 -0600886 if (req->flags & REQ_F_INFLIGHT) {
887 unsigned long flags;
888
889 spin_lock_irqsave(&ctx->inflight_lock, flags);
890 list_del(&req->inflight_entry);
891 if (waitqueue_active(&ctx->inflight_wait))
892 wake_up(&ctx->inflight_wait);
893 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
894 }
895 percpu_ref_put(&ctx->refs);
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700896 if (likely(!io_is_fallback_req(req)))
897 kmem_cache_free(req_cachep, req);
898 else
899 clear_bit_unlock(0, (unsigned long *) ctx->fallback_req);
Jens Axboee65ef562019-03-12 10:16:44 -0600900}
901
Jackie Liua197f662019-11-08 08:09:12 -0700902static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -0600903{
Jackie Liua197f662019-11-08 08:09:12 -0700904 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -0700905 int ret;
906
Jens Axboe2d283902019-12-04 11:08:05 -0700907 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe2665abf2019-11-05 12:40:47 -0700908 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -0700909 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -0700910 io_commit_cqring(ctx);
911 req->flags &= ~REQ_F_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +0800912 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -0700913 return true;
914 }
915
916 return false;
917}
918
Jens Axboeba816ad2019-09-28 11:36:45 -0600919static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -0600920{
Jens Axboe2665abf2019-11-05 12:40:47 -0700921 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -0700922 bool wake_ev = false;
Jens Axboe9e645e112019-05-10 16:07:28 -0600923
Jens Axboe4d7dd462019-11-20 13:03:52 -0700924 /* Already got next link */
925 if (req->flags & REQ_F_LINK_NEXT)
926 return;
927
Jens Axboe9e645e112019-05-10 16:07:28 -0600928 /*
929 * The list should never be empty when we are called here. But could
930 * potentially happen if the chain is messed up, check to be on the
931 * safe side.
932 */
Pavel Begunkov44932332019-12-05 16:16:35 +0300933 while (!list_empty(&req->link_list)) {
934 struct io_kiocb *nxt = list_first_entry(&req->link_list,
935 struct io_kiocb, link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700936
Pavel Begunkov44932332019-12-05 16:16:35 +0300937 if (unlikely((req->flags & REQ_F_LINK_TIMEOUT) &&
938 (nxt->flags & REQ_F_TIMEOUT))) {
939 list_del_init(&nxt->link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700940 wake_ev |= io_link_cancel_timeout(nxt);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700941 req->flags &= ~REQ_F_LINK_TIMEOUT;
942 continue;
943 }
Jens Axboe9e645e112019-05-10 16:07:28 -0600944
Pavel Begunkov44932332019-12-05 16:16:35 +0300945 list_del_init(&req->link_list);
946 if (!list_empty(&nxt->link_list))
947 nxt->flags |= REQ_F_LINK;
Pavel Begunkovb18fdf72019-11-21 23:21:02 +0300948 *nxtptr = nxt;
Jens Axboe94ae5e72019-11-14 19:39:52 -0700949 break;
Jens Axboe9e645e112019-05-10 16:07:28 -0600950 }
Jens Axboe2665abf2019-11-05 12:40:47 -0700951
Jens Axboe4d7dd462019-11-20 13:03:52 -0700952 req->flags |= REQ_F_LINK_NEXT;
Jens Axboe2665abf2019-11-05 12:40:47 -0700953 if (wake_ev)
954 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -0600955}
956
957/*
958 * Called if REQ_F_LINK is set, and we fail the head request
959 */
960static void io_fail_links(struct io_kiocb *req)
961{
Jens Axboe2665abf2019-11-05 12:40:47 -0700962 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -0700963 unsigned long flags;
964
965 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -0600966
967 while (!list_empty(&req->link_list)) {
Pavel Begunkov44932332019-12-05 16:16:35 +0300968 struct io_kiocb *link = list_first_entry(&req->link_list,
969 struct io_kiocb, link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -0600970
Pavel Begunkov44932332019-12-05 16:16:35 +0300971 list_del_init(&link->link_list);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +0200972 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -0700973
974 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300975 link->sqe->opcode == IORING_OP_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -0700976 io_link_cancel_timeout(link);
Jens Axboe2665abf2019-11-05 12:40:47 -0700977 } else {
Jens Axboe78e19bb2019-11-06 15:21:34 -0700978 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe978db572019-11-14 22:39:04 -0700979 __io_double_put_req(link);
Jens Axboe2665abf2019-11-05 12:40:47 -0700980 }
Jens Axboe5d960722019-11-19 15:31:28 -0700981 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -0600982 }
Jens Axboe2665abf2019-11-05 12:40:47 -0700983
984 io_commit_cqring(ctx);
985 spin_unlock_irqrestore(&ctx->completion_lock, flags);
986 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -0600987}
988
Jens Axboe4d7dd462019-11-20 13:03:52 -0700989static void io_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -0600990{
Jens Axboe4d7dd462019-11-20 13:03:52 -0700991 if (likely(!(req->flags & REQ_F_LINK)))
Jens Axboe2665abf2019-11-05 12:40:47 -0700992 return;
Jens Axboe2665abf2019-11-05 12:40:47 -0700993
Jens Axboe9e645e112019-05-10 16:07:28 -0600994 /*
995 * If LINK is set, we have dependent requests in this chain. If we
996 * didn't fail this request, queue the first one up, moving any other
997 * dependencies to the next request. In case of failure, fail the rest
998 * of the chain.
999 */
Jens Axboe2665abf2019-11-05 12:40:47 -07001000 if (req->flags & REQ_F_FAIL_LINK) {
1001 io_fail_links(req);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001002 } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) ==
1003 REQ_F_LINK_TIMEOUT) {
Jens Axboe2665abf2019-11-05 12:40:47 -07001004 struct io_ring_ctx *ctx = req->ctx;
1005 unsigned long flags;
1006
1007 /*
1008 * If this is a timeout link, we could be racing with the
1009 * timeout timer. Grab the completion lock for this case to
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001010 * protect against that.
Jens Axboe2665abf2019-11-05 12:40:47 -07001011 */
1012 spin_lock_irqsave(&ctx->completion_lock, flags);
1013 io_req_link_next(req, nxt);
1014 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1015 } else {
1016 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -06001017 }
Jens Axboe4d7dd462019-11-20 13:03:52 -07001018}
Jens Axboe9e645e112019-05-10 16:07:28 -06001019
Jackie Liuc69f8db2019-11-09 11:00:08 +08001020static void io_free_req(struct io_kiocb *req)
1021{
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001022 struct io_kiocb *nxt = NULL;
1023
1024 io_req_find_next(req, &nxt);
Pavel Begunkov70cf9f32019-11-21 23:21:00 +03001025 __io_free_req(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001026
1027 if (nxt)
1028 io_queue_async_work(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08001029}
1030
Jens Axboeba816ad2019-09-28 11:36:45 -06001031/*
1032 * Drop reference to request, return next in chain (if there is one) if this
1033 * was the last reference to this request.
1034 */
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001035__attribute__((nonnull))
Jackie Liuec9c02a2019-11-08 23:50:36 +08001036static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboee65ef562019-03-12 10:16:44 -06001037{
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001038 io_req_find_next(req, nxtptr);
Jens Axboe4d7dd462019-11-20 13:03:52 -07001039
Jens Axboee65ef562019-03-12 10:16:44 -06001040 if (refcount_dec_and_test(&req->refs))
Jens Axboe4d7dd462019-11-20 13:03:52 -07001041 __io_free_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001042}
1043
Jens Axboe2b188cc2019-01-07 10:46:33 -07001044static void io_put_req(struct io_kiocb *req)
1045{
Jens Axboedef596e2019-01-09 08:59:42 -07001046 if (refcount_dec_and_test(&req->refs))
1047 io_free_req(req);
1048}
1049
Jens Axboe978db572019-11-14 22:39:04 -07001050/*
1051 * Must only be used if we don't need to care about links, usually from
1052 * within the completion handling itself.
1053 */
1054static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06001055{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001056 /* drop both submit and complete references */
1057 if (refcount_sub_and_test(2, &req->refs))
1058 __io_free_req(req);
1059}
1060
Jens Axboe978db572019-11-14 22:39:04 -07001061static void io_double_put_req(struct io_kiocb *req)
1062{
1063 /* drop both submit and complete references */
1064 if (refcount_sub_and_test(2, &req->refs))
1065 io_free_req(req);
1066}
1067
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001068static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06001069{
Jens Axboe84f97dc2019-11-06 11:27:53 -07001070 struct io_rings *rings = ctx->rings;
1071
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001072 /*
1073 * noflush == true is from the waitqueue handler, just ensure we wake
1074 * up the task, and the next invocation will flush the entries. We
1075 * cannot safely to it from here.
1076 */
1077 if (noflush && !list_empty(&ctx->cq_overflow_list))
1078 return -1U;
1079
1080 io_cqring_overflow_flush(ctx, false);
1081
Jens Axboea3a0e432019-08-20 11:03:11 -06001082 /* See comment at the top of this file */
1083 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00001084 return READ_ONCE(rings->cq.tail) - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06001085}
1086
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03001087static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1088{
1089 struct io_rings *rings = ctx->rings;
1090
1091 /* make sure SQ entry isn't read before tail */
1092 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1093}
1094
Jens Axboedef596e2019-01-09 08:59:42 -07001095/*
1096 * Find and free completed poll iocbs
1097 */
1098static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
1099 struct list_head *done)
1100{
1101 void *reqs[IO_IOPOLL_BATCH];
1102 struct io_kiocb *req;
Jens Axboe09bb8392019-03-13 12:39:28 -06001103 int to_free;
Jens Axboedef596e2019-01-09 08:59:42 -07001104
Jens Axboe09bb8392019-03-13 12:39:28 -06001105 to_free = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001106 while (!list_empty(done)) {
1107 req = list_first_entry(done, struct io_kiocb, list);
1108 list_del(&req->list);
1109
Jens Axboe78e19bb2019-11-06 15:21:34 -07001110 io_cqring_fill_event(req, req->result);
Jens Axboedef596e2019-01-09 08:59:42 -07001111 (*nr_events)++;
1112
Jens Axboe09bb8392019-03-13 12:39:28 -06001113 if (refcount_dec_and_test(&req->refs)) {
1114 /* If we're not using fixed files, we have to pair the
1115 * completion part with the file put. Use regular
1116 * completions for those, only batch free for fixed
Jens Axboe9e645e112019-05-10 16:07:28 -06001117 * file and non-linked commands.
Jens Axboe09bb8392019-03-13 12:39:28 -06001118 */
Jens Axboe1a6b74f2019-12-02 10:33:15 -07001119 if (((req->flags & (REQ_F_FIXED_FILE|REQ_F_LINK)) ==
1120 REQ_F_FIXED_FILE) && !io_is_fallback_req(req) &&
1121 !req->io) {
Jens Axboe09bb8392019-03-13 12:39:28 -06001122 reqs[to_free++] = req;
1123 if (to_free == ARRAY_SIZE(reqs))
1124 io_free_req_many(ctx, reqs, &to_free);
Jens Axboe6b063142019-01-10 22:13:58 -07001125 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001126 io_free_req(req);
Jens Axboe6b063142019-01-10 22:13:58 -07001127 }
Jens Axboe9a56a232019-01-09 09:06:50 -07001128 }
Jens Axboedef596e2019-01-09 08:59:42 -07001129 }
Jens Axboedef596e2019-01-09 08:59:42 -07001130
Jens Axboe09bb8392019-03-13 12:39:28 -06001131 io_commit_cqring(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07001132 io_free_req_many(ctx, reqs, &to_free);
1133}
1134
1135static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1136 long min)
1137{
1138 struct io_kiocb *req, *tmp;
1139 LIST_HEAD(done);
1140 bool spin;
1141 int ret;
1142
1143 /*
1144 * Only spin for completions if we don't have multiple devices hanging
1145 * off our complete list, and we're under the requested amount.
1146 */
1147 spin = !ctx->poll_multi_file && *nr_events < min;
1148
1149 ret = 0;
1150 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
1151 struct kiocb *kiocb = &req->rw;
1152
1153 /*
1154 * Move completed entries to our local list. If we find a
1155 * request that requires polling, break out and complete
1156 * the done list first, if we have entries there.
1157 */
1158 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
1159 list_move_tail(&req->list, &done);
1160 continue;
1161 }
1162 if (!list_empty(&done))
1163 break;
1164
1165 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1166 if (ret < 0)
1167 break;
1168
1169 if (ret && spin)
1170 spin = false;
1171 ret = 0;
1172 }
1173
1174 if (!list_empty(&done))
1175 io_iopoll_complete(ctx, nr_events, &done);
1176
1177 return ret;
1178}
1179
1180/*
1181 * Poll for a mininum of 'min' events. Note that if min == 0 we consider that a
1182 * non-spinning poll check - we'll still enter the driver poll loop, but only
1183 * as a non-spinning completion check.
1184 */
1185static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
1186 long min)
1187{
Jens Axboe08f54392019-08-21 22:19:11 -06001188 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07001189 int ret;
1190
1191 ret = io_do_iopoll(ctx, nr_events, min);
1192 if (ret < 0)
1193 return ret;
1194 if (!min || *nr_events >= min)
1195 return 0;
1196 }
1197
1198 return 1;
1199}
1200
1201/*
1202 * We can't just wait for polled events to come to us, we have to actively
1203 * find and complete them.
1204 */
1205static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
1206{
1207 if (!(ctx->flags & IORING_SETUP_IOPOLL))
1208 return;
1209
1210 mutex_lock(&ctx->uring_lock);
1211 while (!list_empty(&ctx->poll_list)) {
1212 unsigned int nr_events = 0;
1213
1214 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -06001215
1216 /*
1217 * Ensure we allow local-to-the-cpu processing to take place,
1218 * in this case we need to ensure that we reap all events.
1219 */
1220 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -07001221 }
1222 mutex_unlock(&ctx->uring_lock);
1223}
1224
Jens Axboe2b2ed972019-10-25 10:06:15 -06001225static int __io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1226 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07001227{
Jens Axboe2b2ed972019-10-25 10:06:15 -06001228 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001229
1230 do {
1231 int tmin = 0;
1232
Jens Axboe500f9fb2019-08-19 12:15:59 -06001233 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06001234 * Don't enter poll loop if we already have events pending.
1235 * If we do, we can potentially be spinning for commands that
1236 * already triggered a CQE (eg in error).
1237 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001238 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06001239 break;
1240
1241 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06001242 * If a submit got punted to a workqueue, we can have the
1243 * application entering polling for a command before it gets
1244 * issued. That app will hold the uring_lock for the duration
1245 * of the poll right here, so we need to take a breather every
1246 * now and then to ensure that the issue has a chance to add
1247 * the poll to the issued list. Otherwise we can spin here
1248 * forever, while the workqueue is stuck trying to acquire the
1249 * very same mutex.
1250 */
1251 if (!(++iters & 7)) {
1252 mutex_unlock(&ctx->uring_lock);
1253 mutex_lock(&ctx->uring_lock);
1254 }
1255
Jens Axboedef596e2019-01-09 08:59:42 -07001256 if (*nr_events < min)
1257 tmin = min - *nr_events;
1258
1259 ret = io_iopoll_getevents(ctx, nr_events, tmin);
1260 if (ret <= 0)
1261 break;
1262 ret = 0;
1263 } while (min && !*nr_events && !need_resched());
1264
Jens Axboe2b2ed972019-10-25 10:06:15 -06001265 return ret;
1266}
1267
1268static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1269 long min)
1270{
1271 int ret;
1272
1273 /*
1274 * We disallow the app entering submit/complete with polling, but we
1275 * still need to lock the ring to prevent racing with polled issue
1276 * that got punted to a workqueue.
1277 */
1278 mutex_lock(&ctx->uring_lock);
1279 ret = __io_iopoll_check(ctx, nr_events, min);
Jens Axboe500f9fb2019-08-19 12:15:59 -06001280 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001281 return ret;
1282}
1283
Jens Axboe491381ce2019-10-17 09:20:46 -06001284static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001285{
Jens Axboe491381ce2019-10-17 09:20:46 -06001286 /*
1287 * Tell lockdep we inherited freeze protection from submission
1288 * thread.
1289 */
1290 if (req->flags & REQ_F_ISREG) {
1291 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001292
Jens Axboe491381ce2019-10-17 09:20:46 -06001293 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001294 }
Jens Axboe491381ce2019-10-17 09:20:46 -06001295 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001296}
1297
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001298static inline void req_set_fail_links(struct io_kiocb *req)
1299{
1300 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1301 req->flags |= REQ_F_FAIL_LINK;
1302}
1303
Jens Axboeba816ad2019-09-28 11:36:45 -06001304static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001305{
1306 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1307
Jens Axboe491381ce2019-10-17 09:20:46 -06001308 if (kiocb->ki_flags & IOCB_WRITE)
1309 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001310
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001311 if (res != req->result)
1312 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001313 io_cqring_add_event(req, res);
Jens Axboeba816ad2019-09-28 11:36:45 -06001314}
1315
1316static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1317{
1318 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1319
1320 io_complete_rw_common(kiocb, res);
Jens Axboee65ef562019-03-12 10:16:44 -06001321 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001322}
1323
Jens Axboeba816ad2019-09-28 11:36:45 -06001324static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res)
1325{
1326 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001327 struct io_kiocb *nxt = NULL;
Jens Axboeba816ad2019-09-28 11:36:45 -06001328
1329 io_complete_rw_common(kiocb, res);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001330 io_put_req_find_next(req, &nxt);
1331
1332 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001333}
1334
Jens Axboedef596e2019-01-09 08:59:42 -07001335static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1336{
1337 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1338
Jens Axboe491381ce2019-10-17 09:20:46 -06001339 if (kiocb->ki_flags & IOCB_WRITE)
1340 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001341
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001342 if (res != req->result)
1343 req_set_fail_links(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06001344 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -07001345 if (res != -EAGAIN)
1346 req->flags |= REQ_F_IOPOLL_COMPLETED;
1347}
1348
1349/*
1350 * After the iocb has been issued, it's safe to be found on the poll list.
1351 * Adding the kiocb to the list AFTER submission ensures that we don't
1352 * find it from a io_iopoll_getevents() thread before the issuer is done
1353 * accessing the kiocb cookie.
1354 */
1355static void io_iopoll_req_issued(struct io_kiocb *req)
1356{
1357 struct io_ring_ctx *ctx = req->ctx;
1358
1359 /*
1360 * Track whether we have multiple files in our lists. This will impact
1361 * how we do polling eventually, not spinning if we're on potentially
1362 * different devices.
1363 */
1364 if (list_empty(&ctx->poll_list)) {
1365 ctx->poll_multi_file = false;
1366 } else if (!ctx->poll_multi_file) {
1367 struct io_kiocb *list_req;
1368
1369 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1370 list);
1371 if (list_req->rw.ki_filp != req->rw.ki_filp)
1372 ctx->poll_multi_file = true;
1373 }
1374
1375 /*
1376 * For fast devices, IO may have already completed. If it has, add
1377 * it to the front so we find it first.
1378 */
1379 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1380 list_add(&req->list, &ctx->poll_list);
1381 else
1382 list_add_tail(&req->list, &ctx->poll_list);
1383}
1384
Jens Axboe3d6770f2019-04-13 11:50:54 -06001385static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07001386{
Jens Axboe3d6770f2019-04-13 11:50:54 -06001387 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -07001388 int diff = state->has_refs - state->used_refs;
1389
1390 if (diff)
1391 fput_many(state->file, diff);
1392 state->file = NULL;
1393 }
1394}
1395
1396/*
1397 * Get as many references to a file as we have IOs left in this submission,
1398 * assuming most submissions are for one file, or at least that each file
1399 * has more than one submission.
1400 */
1401static struct file *io_file_get(struct io_submit_state *state, int fd)
1402{
1403 if (!state)
1404 return fget(fd);
1405
1406 if (state->file) {
1407 if (state->fd == fd) {
1408 state->used_refs++;
1409 state->ios_left--;
1410 return state->file;
1411 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06001412 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07001413 }
1414 state->file = fget_many(fd, state->ios_left);
1415 if (!state->file)
1416 return NULL;
1417
1418 state->fd = fd;
1419 state->has_refs = state->ios_left;
1420 state->used_refs = 1;
1421 state->ios_left--;
1422 return state->file;
1423}
1424
Jens Axboe2b188cc2019-01-07 10:46:33 -07001425/*
1426 * If we tracked the file through the SCM inflight mechanism, we could support
1427 * any file. For now, just ensure that anything potentially problematic is done
1428 * inline.
1429 */
1430static bool io_file_supports_async(struct file *file)
1431{
1432 umode_t mode = file_inode(file)->i_mode;
1433
1434 if (S_ISBLK(mode) || S_ISCHR(mode))
1435 return true;
1436 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
1437 return true;
1438
1439 return false;
1440}
1441
Pavel Begunkov267bc902019-11-07 01:41:08 +03001442static int io_prep_rw(struct io_kiocb *req, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001443{
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001444 const struct io_uring_sqe *sqe = req->sqe;
Jens Axboedef596e2019-01-09 08:59:42 -07001445 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001446 struct kiocb *kiocb = &req->rw;
Jens Axboe09bb8392019-03-13 12:39:28 -06001447 unsigned ioprio;
1448 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001449
Jens Axboe09bb8392019-03-13 12:39:28 -06001450 if (!req->file)
1451 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001452
Jens Axboe491381ce2019-10-17 09:20:46 -06001453 if (S_ISREG(file_inode(req->file)->i_mode))
1454 req->flags |= REQ_F_ISREG;
1455
Jens Axboe2b188cc2019-01-07 10:46:33 -07001456 kiocb->ki_pos = READ_ONCE(sqe->off);
1457 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
1458 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
1459
1460 ioprio = READ_ONCE(sqe->ioprio);
1461 if (ioprio) {
1462 ret = ioprio_check_cap(ioprio);
1463 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06001464 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001465
1466 kiocb->ki_ioprio = ioprio;
1467 } else
1468 kiocb->ki_ioprio = get_current_ioprio();
1469
1470 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
1471 if (unlikely(ret))
Jens Axboe09bb8392019-03-13 12:39:28 -06001472 return ret;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001473
1474 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06001475 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
1476 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02001477 req->flags |= REQ_F_NOWAIT;
1478
1479 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001480 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001481
Jens Axboedef596e2019-01-09 08:59:42 -07001482 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07001483 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
1484 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06001485 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001486
Jens Axboedef596e2019-01-09 08:59:42 -07001487 kiocb->ki_flags |= IOCB_HIPRI;
1488 kiocb->ki_complete = io_complete_rw_iopoll;
Jens Axboe6873e0b2019-10-30 13:53:09 -06001489 req->result = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001490 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001491 if (kiocb->ki_flags & IOCB_HIPRI)
1492 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07001493 kiocb->ki_complete = io_complete_rw;
1494 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001495 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001496}
1497
1498static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
1499{
1500 switch (ret) {
1501 case -EIOCBQUEUED:
1502 break;
1503 case -ERESTARTSYS:
1504 case -ERESTARTNOINTR:
1505 case -ERESTARTNOHAND:
1506 case -ERESTART_RESTARTBLOCK:
1507 /*
1508 * We can't just restart the syscall, since previously
1509 * submitted sqes may already be in progress. Just fail this
1510 * IO with EINTR.
1511 */
1512 ret = -EINTR;
1513 /* fall through */
1514 default:
1515 kiocb->ki_complete(kiocb, ret, 0);
1516 }
1517}
1518
Jens Axboeba816ad2019-09-28 11:36:45 -06001519static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt,
1520 bool in_async)
1521{
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001522 if (in_async && ret >= 0 && kiocb->ki_complete == io_complete_rw)
Jens Axboeba816ad2019-09-28 11:36:45 -06001523 *nxt = __io_complete_rw(kiocb, ret);
1524 else
1525 io_rw_done(kiocb, ret);
1526}
1527
Pavel Begunkov7d009162019-11-25 23:14:40 +03001528static ssize_t io_import_fixed(struct io_ring_ctx *ctx, int rw,
1529 const struct io_uring_sqe *sqe,
1530 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07001531{
1532 size_t len = READ_ONCE(sqe->len);
1533 struct io_mapped_ubuf *imu;
1534 unsigned index, buf_index;
1535 size_t offset;
1536 u64 buf_addr;
1537
1538 /* attempt to use fixed buffers without having provided iovecs */
1539 if (unlikely(!ctx->user_bufs))
1540 return -EFAULT;
1541
1542 buf_index = READ_ONCE(sqe->buf_index);
1543 if (unlikely(buf_index >= ctx->nr_user_bufs))
1544 return -EFAULT;
1545
1546 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
1547 imu = &ctx->user_bufs[index];
1548 buf_addr = READ_ONCE(sqe->addr);
1549
1550 /* overflow */
1551 if (buf_addr + len < buf_addr)
1552 return -EFAULT;
1553 /* not inside the mapped region */
1554 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
1555 return -EFAULT;
1556
1557 /*
1558 * May not be a start of buffer, set size appropriately
1559 * and advance us to the beginning.
1560 */
1561 offset = buf_addr - imu->ubuf;
1562 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06001563
1564 if (offset) {
1565 /*
1566 * Don't use iov_iter_advance() here, as it's really slow for
1567 * using the latter parts of a big fixed buffer - it iterates
1568 * over each segment manually. We can cheat a bit here, because
1569 * we know that:
1570 *
1571 * 1) it's a BVEC iter, we set it up
1572 * 2) all bvecs are PAGE_SIZE in size, except potentially the
1573 * first and last bvec
1574 *
1575 * So just find our index, and adjust the iterator afterwards.
1576 * If the offset is within the first bvec (or the whole first
1577 * bvec, just use iov_iter_advance(). This makes it easier
1578 * since we can just skip the first segment, which may not
1579 * be PAGE_SIZE aligned.
1580 */
1581 const struct bio_vec *bvec = imu->bvec;
1582
1583 if (offset <= bvec->bv_len) {
1584 iov_iter_advance(iter, offset);
1585 } else {
1586 unsigned long seg_skip;
1587
1588 /* skip first vec */
1589 offset -= bvec->bv_len;
1590 seg_skip = 1 + (offset >> PAGE_SHIFT);
1591
1592 iter->bvec = bvec + seg_skip;
1593 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02001594 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001595 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001596 }
1597 }
1598
Jens Axboe5e559562019-11-13 16:12:46 -07001599 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001600}
1601
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001602static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
1603 struct iovec **iovec, struct iov_iter *iter)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001604{
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001605 const struct io_uring_sqe *sqe = req->sqe;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001606 void __user *buf = u64_to_user_ptr(READ_ONCE(sqe->addr));
1607 size_t sqe_len = READ_ONCE(sqe->len);
Jens Axboeedafcce2019-01-09 09:16:05 -07001608 u8 opcode;
1609
1610 /*
1611 * We're reading ->opcode for the second time, but the first read
1612 * doesn't care whether it's _FIXED or not, so it doesn't matter
1613 * whether ->opcode changes concurrently. The first read does care
1614 * about whether it is a READ or a WRITE, so we don't trust this read
1615 * for that purpose and instead let the caller pass in the read/write
1616 * flag.
1617 */
1618 opcode = READ_ONCE(sqe->opcode);
Pavel Begunkov7d009162019-11-25 23:14:40 +03001619 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07001620 *iovec = NULL;
Pavel Begunkov7d009162019-11-25 23:14:40 +03001621 return io_import_fixed(req->ctx, rw, sqe, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07001622 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001623
Jens Axboef67676d2019-12-02 11:03:47 -07001624 if (req->io) {
1625 struct io_async_rw *iorw = &req->io->rw;
1626
1627 *iovec = iorw->iov;
1628 iov_iter_init(iter, rw, *iovec, iorw->nr_segs, iorw->size);
1629 if (iorw->iov == iorw->fast_iov)
1630 *iovec = NULL;
1631 return iorw->size;
1632 }
1633
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001634 if (!req->has_user)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001635 return -EFAULT;
1636
1637#ifdef CONFIG_COMPAT
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001638 if (req->ctx->compat)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001639 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
1640 iovec, iter);
1641#endif
1642
1643 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
1644}
1645
Jens Axboe32960612019-09-23 11:05:34 -06001646/*
1647 * For files that don't have ->read_iter() and ->write_iter(), handle them
1648 * by looping over ->read() or ->write() manually.
1649 */
1650static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
1651 struct iov_iter *iter)
1652{
1653 ssize_t ret = 0;
1654
1655 /*
1656 * Don't support polled IO through this interface, and we can't
1657 * support non-blocking either. For the latter, this just causes
1658 * the kiocb to be handled from an async context.
1659 */
1660 if (kiocb->ki_flags & IOCB_HIPRI)
1661 return -EOPNOTSUPP;
1662 if (kiocb->ki_flags & IOCB_NOWAIT)
1663 return -EAGAIN;
1664
1665 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03001666 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06001667 ssize_t nr;
1668
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03001669 if (!iov_iter_is_bvec(iter)) {
1670 iovec = iov_iter_iovec(iter);
1671 } else {
1672 /* fixed buffers import bvec */
1673 iovec.iov_base = kmap(iter->bvec->bv_page)
1674 + iter->iov_offset;
1675 iovec.iov_len = min(iter->count,
1676 iter->bvec->bv_len - iter->iov_offset);
1677 }
1678
Jens Axboe32960612019-09-23 11:05:34 -06001679 if (rw == READ) {
1680 nr = file->f_op->read(file, iovec.iov_base,
1681 iovec.iov_len, &kiocb->ki_pos);
1682 } else {
1683 nr = file->f_op->write(file, iovec.iov_base,
1684 iovec.iov_len, &kiocb->ki_pos);
1685 }
1686
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03001687 if (iov_iter_is_bvec(iter))
1688 kunmap(iter->bvec->bv_page);
1689
Jens Axboe32960612019-09-23 11:05:34 -06001690 if (nr < 0) {
1691 if (!ret)
1692 ret = nr;
1693 break;
1694 }
1695 ret += nr;
1696 if (nr != iovec.iov_len)
1697 break;
1698 iov_iter_advance(iter, nr);
1699 }
1700
1701 return ret;
1702}
1703
Jens Axboef67676d2019-12-02 11:03:47 -07001704static void io_req_map_io(struct io_kiocb *req, ssize_t io_size,
1705 struct iovec *iovec, struct iovec *fast_iov,
1706 struct iov_iter *iter)
1707{
1708 req->io->rw.nr_segs = iter->nr_segs;
1709 req->io->rw.size = io_size;
1710 req->io->rw.iov = iovec;
1711 if (!req->io->rw.iov) {
1712 req->io->rw.iov = req->io->rw.fast_iov;
1713 memcpy(req->io->rw.iov, fast_iov,
1714 sizeof(struct iovec) * iter->nr_segs);
1715 }
1716}
1717
1718static int io_setup_async_io(struct io_kiocb *req, ssize_t io_size,
1719 struct iovec *iovec, struct iovec *fast_iov,
1720 struct iov_iter *iter)
1721{
1722 req->io = kmalloc(sizeof(*req->io), GFP_KERNEL);
1723 if (req->io) {
1724 io_req_map_io(req, io_size, iovec, fast_iov, iter);
1725 memcpy(&req->io->sqe, req->sqe, sizeof(req->io->sqe));
1726 req->sqe = &req->io->sqe;
1727 return 0;
1728 }
1729
1730 return -ENOMEM;
1731}
1732
1733static int io_read_prep(struct io_kiocb *req, struct iovec **iovec,
1734 struct iov_iter *iter, bool force_nonblock)
1735{
1736 ssize_t ret;
1737
1738 ret = io_prep_rw(req, force_nonblock);
1739 if (ret)
1740 return ret;
1741
1742 if (unlikely(!(req->file->f_mode & FMODE_READ)))
1743 return -EBADF;
1744
1745 return io_import_iovec(READ, req, iovec, iter);
1746}
1747
Pavel Begunkov267bc902019-11-07 01:41:08 +03001748static int io_read(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001749 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001750{
1751 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1752 struct kiocb *kiocb = &req->rw;
1753 struct iov_iter iter;
1754 struct file *file;
Jens Axboe31b51512019-01-18 22:56:34 -07001755 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07001756 ssize_t io_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001757
Jens Axboef67676d2019-12-02 11:03:47 -07001758 if (!req->io) {
1759 ret = io_read_prep(req, &iovec, &iter, force_nonblock);
1760 if (ret < 0)
1761 return ret;
1762 } else {
1763 ret = io_import_iovec(READ, req, &iovec, &iter);
1764 if (ret < 0)
1765 return ret;
1766 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001767
Jens Axboef67676d2019-12-02 11:03:47 -07001768 file = req->file;
1769 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06001770 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07001771 req->result = io_size;
1772
1773 /*
1774 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
1775 * we know to async punt it even if it was opened O_NONBLOCK
1776 */
1777 if (force_nonblock && !io_file_supports_async(file)) {
1778 req->flags |= REQ_F_MUST_PUNT;
1779 goto copy_iov;
1780 }
Jens Axboe9e645e112019-05-10 16:07:28 -06001781
Jens Axboe31b51512019-01-18 22:56:34 -07001782 iov_count = iov_iter_count(&iter);
1783 ret = rw_verify_area(READ, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001784 if (!ret) {
1785 ssize_t ret2;
1786
Jens Axboe32960612019-09-23 11:05:34 -06001787 if (file->f_op->read_iter)
1788 ret2 = call_read_iter(file, kiocb, &iter);
1789 else
1790 ret2 = loop_rw_iter(READ, file, kiocb, &iter);
1791
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001792 /*
1793 * In case of a short read, punt to async. This can happen
1794 * if we have data partially cached. Alternatively we can
1795 * return the short read, in which case the application will
1796 * need to issue another SQE and wait for it. That SQE will
1797 * need async punt anyway, so it's more efficient to do it
1798 * here.
1799 */
Jens Axboe491381ce2019-10-17 09:20:46 -06001800 if (force_nonblock && !(req->flags & REQ_F_NOWAIT) &&
1801 (req->flags & REQ_F_ISREG) &&
Jens Axboef67676d2019-12-02 11:03:47 -07001802 ret2 > 0 && ret2 < io_size)
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001803 ret2 = -EAGAIN;
1804 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboef67676d2019-12-02 11:03:47 -07001805 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001806 kiocb_done(kiocb, ret2, nxt, req->in_async);
Jens Axboef67676d2019-12-02 11:03:47 -07001807 } else {
1808copy_iov:
1809 ret = io_setup_async_io(req, io_size, iovec,
1810 inline_vecs, &iter);
1811 if (ret)
1812 goto out_free;
1813 return -EAGAIN;
1814 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001815 }
Jens Axboef67676d2019-12-02 11:03:47 -07001816out_free:
Jens Axboe2b188cc2019-01-07 10:46:33 -07001817 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001818 return ret;
1819}
1820
Jens Axboef67676d2019-12-02 11:03:47 -07001821static int io_write_prep(struct io_kiocb *req, struct iovec **iovec,
1822 struct iov_iter *iter, bool force_nonblock)
1823{
1824 ssize_t ret;
1825
1826 ret = io_prep_rw(req, force_nonblock);
1827 if (ret)
1828 return ret;
1829
1830 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
1831 return -EBADF;
1832
1833 return io_import_iovec(WRITE, req, iovec, iter);
1834}
1835
Pavel Begunkov267bc902019-11-07 01:41:08 +03001836static int io_write(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001837 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001838{
1839 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1840 struct kiocb *kiocb = &req->rw;
1841 struct iov_iter iter;
1842 struct file *file;
Jens Axboe31b51512019-01-18 22:56:34 -07001843 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07001844 ssize_t ret, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001845
Jens Axboef67676d2019-12-02 11:03:47 -07001846 if (!req->io) {
1847 ret = io_write_prep(req, &iovec, &iter, force_nonblock);
1848 if (ret < 0)
1849 return ret;
1850 } else {
1851 ret = io_import_iovec(WRITE, req, &iovec, &iter);
1852 if (ret < 0)
1853 return ret;
1854 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001855
Jens Axboe2b188cc2019-01-07 10:46:33 -07001856 file = kiocb->ki_filp;
Jens Axboef67676d2019-12-02 11:03:47 -07001857 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06001858 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07001859 req->result = io_size;
1860
1861 /*
1862 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
1863 * we know to async punt it even if it was opened O_NONBLOCK
1864 */
1865 if (force_nonblock && !io_file_supports_async(req->file)) {
1866 req->flags |= REQ_F_MUST_PUNT;
1867 goto copy_iov;
1868 }
1869
1870 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT))
1871 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06001872
Jens Axboe31b51512019-01-18 22:56:34 -07001873 iov_count = iov_iter_count(&iter);
Jens Axboe31b51512019-01-18 22:56:34 -07001874 ret = rw_verify_area(WRITE, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001875 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01001876 ssize_t ret2;
1877
Jens Axboe2b188cc2019-01-07 10:46:33 -07001878 /*
1879 * Open-code file_start_write here to grab freeze protection,
1880 * which will be released by another thread in
1881 * io_complete_rw(). Fool lockdep by telling it the lock got
1882 * released so that it doesn't complain about the held lock when
1883 * we return to userspace.
1884 */
Jens Axboe491381ce2019-10-17 09:20:46 -06001885 if (req->flags & REQ_F_ISREG) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001886 __sb_start_write(file_inode(file)->i_sb,
1887 SB_FREEZE_WRITE, true);
1888 __sb_writers_release(file_inode(file)->i_sb,
1889 SB_FREEZE_WRITE);
1890 }
1891 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01001892
Jens Axboe32960612019-09-23 11:05:34 -06001893 if (file->f_op->write_iter)
1894 ret2 = call_write_iter(file, kiocb, &iter);
1895 else
1896 ret2 = loop_rw_iter(WRITE, file, kiocb, &iter);
Jens Axboef67676d2019-12-02 11:03:47 -07001897 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001898 kiocb_done(kiocb, ret2, nxt, req->in_async);
Jens Axboef67676d2019-12-02 11:03:47 -07001899 } else {
1900copy_iov:
1901 ret = io_setup_async_io(req, io_size, iovec,
1902 inline_vecs, &iter);
1903 if (ret)
1904 goto out_free;
1905 return -EAGAIN;
1906 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001907 }
Jens Axboe31b51512019-01-18 22:56:34 -07001908out_free:
Jens Axboe2b188cc2019-01-07 10:46:33 -07001909 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001910 return ret;
1911}
1912
1913/*
1914 * IORING_OP_NOP just posts a completion event, nothing else.
1915 */
Jens Axboe78e19bb2019-11-06 15:21:34 -07001916static int io_nop(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001917{
1918 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001919
Jens Axboedef596e2019-01-09 08:59:42 -07001920 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1921 return -EINVAL;
1922
Jens Axboe78e19bb2019-11-06 15:21:34 -07001923 io_cqring_add_event(req, 0);
Jens Axboee65ef562019-03-12 10:16:44 -06001924 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001925 return 0;
1926}
1927
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001928static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1929{
Jens Axboe6b063142019-01-10 22:13:58 -07001930 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001931
Jens Axboe09bb8392019-03-13 12:39:28 -06001932 if (!req->file)
1933 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001934
Jens Axboe6b063142019-01-10 22:13:58 -07001935 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07001936 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07001937 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001938 return -EINVAL;
1939
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001940 return 0;
1941}
1942
1943static int io_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001944 struct io_kiocb **nxt, bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001945{
1946 loff_t sqe_off = READ_ONCE(sqe->off);
1947 loff_t sqe_len = READ_ONCE(sqe->len);
1948 loff_t end = sqe_off + sqe_len;
1949 unsigned fsync_flags;
1950 int ret;
1951
1952 fsync_flags = READ_ONCE(sqe->fsync_flags);
1953 if (unlikely(fsync_flags & ~IORING_FSYNC_DATASYNC))
1954 return -EINVAL;
1955
1956 ret = io_prep_fsync(req, sqe);
1957 if (ret)
1958 return ret;
1959
1960 /* fsync always requires a blocking context */
1961 if (force_nonblock)
1962 return -EAGAIN;
1963
1964 ret = vfs_fsync_range(req->rw.ki_filp, sqe_off,
1965 end > 0 ? end : LLONG_MAX,
1966 fsync_flags & IORING_FSYNC_DATASYNC);
1967
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001968 if (ret < 0)
1969 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001970 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001971 io_put_req_find_next(req, nxt);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001972 return 0;
1973}
1974
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001975static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1976{
1977 struct io_ring_ctx *ctx = req->ctx;
1978 int ret = 0;
1979
1980 if (!req->file)
1981 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001982
1983 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1984 return -EINVAL;
1985 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
1986 return -EINVAL;
1987
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001988 return ret;
1989}
1990
1991static int io_sync_file_range(struct io_kiocb *req,
1992 const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001993 struct io_kiocb **nxt,
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001994 bool force_nonblock)
1995{
1996 loff_t sqe_off;
1997 loff_t sqe_len;
1998 unsigned flags;
1999 int ret;
2000
2001 ret = io_prep_sfr(req, sqe);
2002 if (ret)
2003 return ret;
2004
2005 /* sync_file_range always requires a blocking context */
2006 if (force_nonblock)
2007 return -EAGAIN;
2008
2009 sqe_off = READ_ONCE(sqe->off);
2010 sqe_len = READ_ONCE(sqe->len);
2011 flags = READ_ONCE(sqe->sync_range_flags);
2012
2013 ret = sync_file_range(req->rw.ki_filp, sqe_off, sqe_len, flags);
2014
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002015 if (ret < 0)
2016 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07002017 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08002018 io_put_req_find_next(req, nxt);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002019 return 0;
2020}
2021
Jens Axboe03b12302019-12-02 18:50:25 -07002022static int io_sendmsg_prep(struct io_kiocb *req, struct io_async_ctx *io)
Jens Axboeaa1fa282019-04-19 13:38:09 -06002023{
Jens Axboe03b12302019-12-02 18:50:25 -07002024#if defined(CONFIG_NET)
2025 const struct io_uring_sqe *sqe = req->sqe;
2026 struct user_msghdr __user *msg;
2027 unsigned flags;
2028
2029 flags = READ_ONCE(sqe->msg_flags);
2030 msg = (struct user_msghdr __user *)(unsigned long) READ_ONCE(sqe->addr);
Jens Axboed9688562019-12-09 19:35:20 -07002031 io->msg.iov = io->msg.fast_iov;
Jens Axboe03b12302019-12-02 18:50:25 -07002032 return sendmsg_copy_msghdr(&io->msg.msg, msg, flags, &io->msg.iov);
2033#else
2034 return 0;
2035#endif
2036}
2037
2038static int io_sendmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2039 struct io_kiocb **nxt, bool force_nonblock)
2040{
2041#if defined(CONFIG_NET)
2042 struct socket *sock;
2043 int ret;
2044
2045 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2046 return -EINVAL;
2047
2048 sock = sock_from_file(req->file, &ret);
2049 if (sock) {
2050 struct io_async_ctx io, *copy;
2051 struct sockaddr_storage addr;
2052 struct msghdr *kmsg;
2053 unsigned flags;
2054
2055 flags = READ_ONCE(sqe->msg_flags);
2056 if (flags & MSG_DONTWAIT)
2057 req->flags |= REQ_F_NOWAIT;
2058 else if (force_nonblock)
2059 flags |= MSG_DONTWAIT;
2060
2061 if (req->io) {
2062 kmsg = &req->io->msg.msg;
2063 kmsg->msg_name = &addr;
2064 } else {
2065 kmsg = &io.msg.msg;
2066 kmsg->msg_name = &addr;
Jens Axboe03b12302019-12-02 18:50:25 -07002067 ret = io_sendmsg_prep(req, &io);
2068 if (ret)
2069 goto out;
2070 }
2071
2072 ret = __sys_sendmsg_sock(sock, kmsg, flags);
2073 if (force_nonblock && ret == -EAGAIN) {
2074 copy = kmalloc(sizeof(*copy), GFP_KERNEL);
2075 if (!copy) {
2076 ret = -ENOMEM;
2077 goto out;
2078 }
2079 memcpy(&copy->msg, &io.msg, sizeof(copy->msg));
2080 req->io = copy;
2081 memcpy(&req->io->sqe, req->sqe, sizeof(*req->sqe));
2082 req->sqe = &req->io->sqe;
2083 return ret;
2084 }
2085 if (ret == -ERESTARTSYS)
2086 ret = -EINTR;
2087 }
2088
2089out:
2090 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002091 if (ret < 0)
2092 req_set_fail_links(req);
Jens Axboe03b12302019-12-02 18:50:25 -07002093 io_put_req_find_next(req, nxt);
2094 return 0;
2095#else
2096 return -EOPNOTSUPP;
2097#endif
2098}
2099
2100static int io_recvmsg_prep(struct io_kiocb *req, struct io_async_ctx *io)
2101{
2102#if defined(CONFIG_NET)
2103 const struct io_uring_sqe *sqe = req->sqe;
2104 struct user_msghdr __user *msg;
2105 unsigned flags;
2106
2107 flags = READ_ONCE(sqe->msg_flags);
2108 msg = (struct user_msghdr __user *)(unsigned long) READ_ONCE(sqe->addr);
Jens Axboed9688562019-12-09 19:35:20 -07002109 io->msg.iov = io->msg.fast_iov;
Jens Axboe03b12302019-12-02 18:50:25 -07002110 return recvmsg_copy_msghdr(&io->msg.msg, msg, flags, &io->msg.uaddr,
2111 &io->msg.iov);
2112#else
2113 return 0;
2114#endif
2115}
2116
2117static int io_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2118 struct io_kiocb **nxt, bool force_nonblock)
2119{
2120#if defined(CONFIG_NET)
Jens Axboe0fa03c62019-04-19 13:34:07 -06002121 struct socket *sock;
2122 int ret;
2123
2124 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2125 return -EINVAL;
2126
2127 sock = sock_from_file(req->file, &ret);
2128 if (sock) {
2129 struct user_msghdr __user *msg;
Jens Axboe03b12302019-12-02 18:50:25 -07002130 struct io_async_ctx io, *copy;
2131 struct sockaddr_storage addr;
2132 struct msghdr *kmsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002133 unsigned flags;
2134
2135 flags = READ_ONCE(sqe->msg_flags);
2136 if (flags & MSG_DONTWAIT)
2137 req->flags |= REQ_F_NOWAIT;
2138 else if (force_nonblock)
2139 flags |= MSG_DONTWAIT;
2140
2141 msg = (struct user_msghdr __user *) (unsigned long)
2142 READ_ONCE(sqe->addr);
Jens Axboe03b12302019-12-02 18:50:25 -07002143 if (req->io) {
2144 kmsg = &req->io->msg.msg;
2145 kmsg->msg_name = &addr;
2146 } else {
2147 kmsg = &io.msg.msg;
2148 kmsg->msg_name = &addr;
Jens Axboe03b12302019-12-02 18:50:25 -07002149 ret = io_recvmsg_prep(req, &io);
2150 if (ret)
2151 goto out;
2152 }
Jens Axboe0fa03c62019-04-19 13:34:07 -06002153
Jens Axboe03b12302019-12-02 18:50:25 -07002154 ret = __sys_recvmsg_sock(sock, kmsg, msg, io.msg.uaddr, flags);
2155 if (force_nonblock && ret == -EAGAIN) {
2156 copy = kmalloc(sizeof(*copy), GFP_KERNEL);
2157 if (!copy) {
2158 ret = -ENOMEM;
2159 goto out;
2160 }
2161 memcpy(copy, &io, sizeof(*copy));
2162 req->io = copy;
2163 memcpy(&req->io->sqe, req->sqe, sizeof(*req->sqe));
2164 req->sqe = &req->io->sqe;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002165 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07002166 }
Jens Axboe441cdbd2019-12-02 18:49:10 -07002167 if (ret == -ERESTARTSYS)
2168 ret = -EINTR;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002169 }
2170
Jens Axboe03b12302019-12-02 18:50:25 -07002171out:
Jens Axboe78e19bb2019-11-06 15:21:34 -07002172 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002173 if (ret < 0)
2174 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08002175 io_put_req_find_next(req, nxt);
Jens Axboe0fa03c62019-04-19 13:34:07 -06002176 return 0;
2177#else
2178 return -EOPNOTSUPP;
2179#endif
2180}
2181
Jens Axboe17f2fe32019-10-17 14:42:58 -06002182static int io_accept(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2183 struct io_kiocb **nxt, bool force_nonblock)
2184{
2185#if defined(CONFIG_NET)
2186 struct sockaddr __user *addr;
2187 int __user *addr_len;
2188 unsigned file_flags;
2189 int flags, ret;
2190
2191 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
2192 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05002193 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06002194 return -EINVAL;
2195
2196 addr = (struct sockaddr __user *) (unsigned long) READ_ONCE(sqe->addr);
2197 addr_len = (int __user *) (unsigned long) READ_ONCE(sqe->addr2);
2198 flags = READ_ONCE(sqe->accept_flags);
2199 file_flags = force_nonblock ? O_NONBLOCK : 0;
2200
2201 ret = __sys_accept4_file(req->file, file_flags, addr, addr_len, flags);
2202 if (ret == -EAGAIN && force_nonblock) {
2203 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
2204 return -EAGAIN;
2205 }
Jens Axboe8e3cca12019-11-09 19:52:33 -07002206 if (ret == -ERESTARTSYS)
2207 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002208 if (ret < 0)
2209 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07002210 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08002211 io_put_req_find_next(req, nxt);
Jens Axboe17f2fe32019-10-17 14:42:58 -06002212 return 0;
2213#else
2214 return -EOPNOTSUPP;
2215#endif
2216}
2217
Jens Axboef499a022019-12-02 16:28:46 -07002218static int io_connect_prep(struct io_kiocb *req, struct io_async_ctx *io)
2219{
2220#if defined(CONFIG_NET)
2221 const struct io_uring_sqe *sqe = req->sqe;
2222 struct sockaddr __user *addr;
2223 int addr_len;
2224
2225 addr = (struct sockaddr __user *) (unsigned long) READ_ONCE(sqe->addr);
2226 addr_len = READ_ONCE(sqe->addr2);
2227 return move_addr_to_kernel(addr, addr_len, &io->connect.address);
2228#else
2229 return 0;
2230#endif
2231}
2232
Jens Axboef8e85cf2019-11-23 14:24:24 -07002233static int io_connect(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2234 struct io_kiocb **nxt, bool force_nonblock)
2235{
2236#if defined(CONFIG_NET)
Jens Axboef499a022019-12-02 16:28:46 -07002237 struct io_async_ctx __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07002238 unsigned file_flags;
2239 int addr_len, ret;
2240
2241 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
2242 return -EINVAL;
2243 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
2244 return -EINVAL;
2245
Jens Axboef8e85cf2019-11-23 14:24:24 -07002246 addr_len = READ_ONCE(sqe->addr2);
2247 file_flags = force_nonblock ? O_NONBLOCK : 0;
2248
Jens Axboef499a022019-12-02 16:28:46 -07002249 if (req->io) {
2250 io = req->io;
2251 } else {
2252 ret = io_connect_prep(req, &__io);
2253 if (ret)
2254 goto out;
2255 io = &__io;
2256 }
2257
2258 ret = __sys_connect_file(req->file, &io->connect.address, addr_len,
2259 file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07002260 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboef499a022019-12-02 16:28:46 -07002261 io = kmalloc(sizeof(*io), GFP_KERNEL);
2262 if (!io) {
2263 ret = -ENOMEM;
2264 goto out;
2265 }
2266 memcpy(&io->connect, &__io.connect, sizeof(io->connect));
2267 req->io = io;
2268 memcpy(&io->sqe, req->sqe, sizeof(*req->sqe));
2269 req->sqe = &io->sqe;
Jens Axboef8e85cf2019-11-23 14:24:24 -07002270 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07002271 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07002272 if (ret == -ERESTARTSYS)
2273 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07002274out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002275 if (ret < 0)
2276 req_set_fail_links(req);
Jens Axboef8e85cf2019-11-23 14:24:24 -07002277 io_cqring_add_event(req, ret);
2278 io_put_req_find_next(req, nxt);
2279 return 0;
2280#else
2281 return -EOPNOTSUPP;
2282#endif
2283}
2284
Jens Axboe221c5eb2019-01-17 09:41:58 -07002285static void io_poll_remove_one(struct io_kiocb *req)
2286{
2287 struct io_poll_iocb *poll = &req->poll;
2288
2289 spin_lock(&poll->head->lock);
2290 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07002291 if (!list_empty(&poll->wait.entry)) {
2292 list_del_init(&poll->wait.entry);
Jackie Liua197f662019-11-08 08:09:12 -07002293 io_queue_async_work(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002294 }
2295 spin_unlock(&poll->head->lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07002296 hash_del(&req->hash_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002297}
2298
2299static void io_poll_remove_all(struct io_ring_ctx *ctx)
2300{
Jens Axboe78076bb2019-12-04 19:56:40 -07002301 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002302 struct io_kiocb *req;
Jens Axboe78076bb2019-12-04 19:56:40 -07002303 int i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002304
2305 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07002306 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
2307 struct hlist_head *list;
2308
2309 list = &ctx->cancel_hash[i];
2310 hlist_for_each_entry_safe(req, tmp, list, hash_node)
2311 io_poll_remove_one(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002312 }
2313 spin_unlock_irq(&ctx->completion_lock);
2314}
2315
Jens Axboe47f46762019-11-09 17:43:02 -07002316static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
2317{
Jens Axboe78076bb2019-12-04 19:56:40 -07002318 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07002319 struct io_kiocb *req;
2320
Jens Axboe78076bb2019-12-04 19:56:40 -07002321 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
2322 hlist_for_each_entry(req, list, hash_node) {
2323 if (sqe_addr == req->user_data) {
Jens Axboeeac406c2019-11-14 12:09:58 -07002324 io_poll_remove_one(req);
2325 return 0;
2326 }
Jens Axboe47f46762019-11-09 17:43:02 -07002327 }
2328
2329 return -ENOENT;
2330}
2331
Jens Axboe221c5eb2019-01-17 09:41:58 -07002332/*
2333 * Find a running poll command that matches one specified in sqe->addr,
2334 * and remove it if found.
2335 */
2336static int io_poll_remove(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2337{
2338 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07002339 int ret;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002340
2341 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2342 return -EINVAL;
2343 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
2344 sqe->poll_events)
2345 return -EINVAL;
2346
2347 spin_lock_irq(&ctx->completion_lock);
Jens Axboe47f46762019-11-09 17:43:02 -07002348 ret = io_poll_cancel(ctx, READ_ONCE(sqe->addr));
Jens Axboe221c5eb2019-01-17 09:41:58 -07002349 spin_unlock_irq(&ctx->completion_lock);
2350
Jens Axboe78e19bb2019-11-06 15:21:34 -07002351 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002352 if (ret < 0)
2353 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06002354 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002355 return 0;
2356}
2357
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002358static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002359{
Jackie Liua197f662019-11-08 08:09:12 -07002360 struct io_ring_ctx *ctx = req->ctx;
2361
Jens Axboe8c838782019-03-12 15:48:16 -06002362 req->poll.done = true;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002363 if (error)
2364 io_cqring_fill_event(req, error);
2365 else
2366 io_cqring_fill_event(req, mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06002367 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002368}
2369
Jens Axboe561fb042019-10-24 07:25:42 -06002370static void io_poll_complete_work(struct io_wq_work **workptr)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002371{
Jens Axboe561fb042019-10-24 07:25:42 -06002372 struct io_wq_work *work = *workptr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002373 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
2374 struct io_poll_iocb *poll = &req->poll;
2375 struct poll_table_struct pt = { ._key = poll->events };
2376 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe89723d02019-11-05 15:32:58 -07002377 struct io_kiocb *nxt = NULL;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002378 __poll_t mask = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002379 int ret = 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002380
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002381 if (work->flags & IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06002382 WRITE_ONCE(poll->canceled, true);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002383 ret = -ECANCELED;
2384 } else if (READ_ONCE(poll->canceled)) {
2385 ret = -ECANCELED;
2386 }
Jens Axboe561fb042019-10-24 07:25:42 -06002387
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002388 if (ret != -ECANCELED)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002389 mask = vfs_poll(poll->file, &pt) & poll->events;
2390
2391 /*
2392 * Note that ->ki_cancel callers also delete iocb from active_reqs after
2393 * calling ->ki_cancel. We need the ctx_lock roundtrip here to
2394 * synchronize with them. In the cancellation case the list_del_init
2395 * itself is not actually needed, but harmless so we keep it in to
2396 * avoid further branches in the fast path.
2397 */
2398 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002399 if (!mask && ret != -ECANCELED) {
Jens Axboe392edb42019-12-09 17:52:20 -07002400 add_wait_queue(poll->head, &poll->wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002401 spin_unlock_irq(&ctx->completion_lock);
2402 return;
2403 }
Jens Axboe78076bb2019-12-04 19:56:40 -07002404 hash_del(&req->hash_node);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002405 io_poll_complete(req, mask, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002406 spin_unlock_irq(&ctx->completion_lock);
2407
Jens Axboe8c838782019-03-12 15:48:16 -06002408 io_cqring_ev_posted(ctx);
Jens Axboe89723d02019-11-05 15:32:58 -07002409
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002410 if (ret < 0)
2411 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08002412 io_put_req_find_next(req, &nxt);
Jens Axboe89723d02019-11-05 15:32:58 -07002413 if (nxt)
2414 *workptr = &nxt->work;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002415}
2416
2417static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
2418 void *key)
2419{
Jens Axboee9444752019-11-26 15:02:04 -07002420 struct io_poll_iocb *poll = wait->private;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002421 struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
2422 struct io_ring_ctx *ctx = req->ctx;
2423 __poll_t mask = key_to_poll(key);
Jens Axboe8c838782019-03-12 15:48:16 -06002424 unsigned long flags;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002425
2426 /* for instances that support it check for an event match first: */
Jens Axboe8c838782019-03-12 15:48:16 -06002427 if (mask && !(mask & poll->events))
2428 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002429
Jens Axboe392edb42019-12-09 17:52:20 -07002430 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06002431
Jens Axboe7c9e7f02019-11-12 08:15:53 -07002432 /*
2433 * Run completion inline if we can. We're using trylock here because
2434 * we are violating the completion_lock -> poll wq lock ordering.
2435 * If we have a link timeout we're going to need the completion_lock
2436 * for finalizing the request, mark us as having grabbed that already.
2437 */
Jens Axboe8c838782019-03-12 15:48:16 -06002438 if (mask && spin_trylock_irqsave(&ctx->completion_lock, flags)) {
Jens Axboe78076bb2019-12-04 19:56:40 -07002439 hash_del(&req->hash_node);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002440 io_poll_complete(req, mask, 0);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07002441 req->flags |= REQ_F_COMP_LOCKED;
2442 io_put_req(req);
Jens Axboe8c838782019-03-12 15:48:16 -06002443 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2444
2445 io_cqring_ev_posted(ctx);
Jens Axboe8c838782019-03-12 15:48:16 -06002446 } else {
Jackie Liua197f662019-11-08 08:09:12 -07002447 io_queue_async_work(req);
Jens Axboe8c838782019-03-12 15:48:16 -06002448 }
2449
Jens Axboe221c5eb2019-01-17 09:41:58 -07002450 return 1;
2451}
2452
2453struct io_poll_table {
2454 struct poll_table_struct pt;
2455 struct io_kiocb *req;
2456 int error;
2457};
2458
2459static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
2460 struct poll_table_struct *p)
2461{
2462 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
2463
2464 if (unlikely(pt->req->poll.head)) {
2465 pt->error = -EINVAL;
2466 return;
2467 }
2468
2469 pt->error = 0;
2470 pt->req->poll.head = head;
Jens Axboe392edb42019-12-09 17:52:20 -07002471 add_wait_queue(head, &pt->req->poll.wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002472}
2473
Jens Axboeeac406c2019-11-14 12:09:58 -07002474static void io_poll_req_insert(struct io_kiocb *req)
2475{
2476 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07002477 struct hlist_head *list;
Jens Axboeeac406c2019-11-14 12:09:58 -07002478
Jens Axboe78076bb2019-12-04 19:56:40 -07002479 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
2480 hlist_add_head(&req->hash_node, list);
Jens Axboeeac406c2019-11-14 12:09:58 -07002481}
2482
Jens Axboe89723d02019-11-05 15:32:58 -07002483static int io_poll_add(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2484 struct io_kiocb **nxt)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002485{
2486 struct io_poll_iocb *poll = &req->poll;
2487 struct io_ring_ctx *ctx = req->ctx;
2488 struct io_poll_table ipt;
Jens Axboe8c838782019-03-12 15:48:16 -06002489 bool cancel = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002490 __poll_t mask;
2491 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002492
2493 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2494 return -EINVAL;
2495 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
2496 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06002497 if (!poll->file)
2498 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002499
Jens Axboe1a6b74f2019-12-02 10:33:15 -07002500 req->io = NULL;
Jens Axboe561fb042019-10-24 07:25:42 -06002501 INIT_IO_WORK(&req->work, io_poll_complete_work);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002502 events = READ_ONCE(sqe->poll_events);
2503 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
Jens Axboe78076bb2019-12-04 19:56:40 -07002504 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002505
Jens Axboe221c5eb2019-01-17 09:41:58 -07002506 poll->head = NULL;
Jens Axboe8c838782019-03-12 15:48:16 -06002507 poll->done = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002508 poll->canceled = false;
2509
2510 ipt.pt._qproc = io_poll_queue_proc;
2511 ipt.pt._key = poll->events;
2512 ipt.req = req;
2513 ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
2514
2515 /* initialized the list so that we can do list_empty checks */
Jens Axboe392edb42019-12-09 17:52:20 -07002516 INIT_LIST_HEAD(&poll->wait.entry);
2517 init_waitqueue_func_entry(&poll->wait, io_poll_wake);
2518 poll->wait.private = poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002519
Jens Axboe36703242019-07-25 10:20:18 -06002520 INIT_LIST_HEAD(&req->list);
2521
Jens Axboe221c5eb2019-01-17 09:41:58 -07002522 mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002523
2524 spin_lock_irq(&ctx->completion_lock);
Jens Axboe8c838782019-03-12 15:48:16 -06002525 if (likely(poll->head)) {
2526 spin_lock(&poll->head->lock);
Jens Axboe392edb42019-12-09 17:52:20 -07002527 if (unlikely(list_empty(&poll->wait.entry))) {
Jens Axboe8c838782019-03-12 15:48:16 -06002528 if (ipt.error)
2529 cancel = true;
2530 ipt.error = 0;
2531 mask = 0;
2532 }
2533 if (mask || ipt.error)
Jens Axboe392edb42019-12-09 17:52:20 -07002534 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06002535 else if (cancel)
2536 WRITE_ONCE(poll->canceled, true);
2537 else if (!poll->done) /* actually waiting for an event */
Jens Axboeeac406c2019-11-14 12:09:58 -07002538 io_poll_req_insert(req);
Jens Axboe8c838782019-03-12 15:48:16 -06002539 spin_unlock(&poll->head->lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002540 }
Jens Axboe8c838782019-03-12 15:48:16 -06002541 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06002542 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002543 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06002544 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07002545 spin_unlock_irq(&ctx->completion_lock);
2546
Jens Axboe8c838782019-03-12 15:48:16 -06002547 if (mask) {
2548 io_cqring_ev_posted(ctx);
Jackie Liuec9c02a2019-11-08 23:50:36 +08002549 io_put_req_find_next(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002550 }
Jens Axboe8c838782019-03-12 15:48:16 -06002551 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002552}
2553
Jens Axboe5262f562019-09-17 12:26:57 -06002554static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
2555{
Jens Axboead8a48a2019-11-15 08:49:11 -07002556 struct io_timeout_data *data = container_of(timer,
2557 struct io_timeout_data, timer);
2558 struct io_kiocb *req = data->req;
2559 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06002560 unsigned long flags;
2561
Jens Axboe5262f562019-09-17 12:26:57 -06002562 atomic_inc(&ctx->cq_timeouts);
2563
2564 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08002565 /*
Jens Axboe11365042019-10-16 09:08:32 -06002566 * We could be racing with timeout deletion. If the list is empty,
2567 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08002568 */
Jens Axboe842f9612019-10-29 12:34:10 -06002569 if (!list_empty(&req->list)) {
Jens Axboe11365042019-10-16 09:08:32 -06002570 struct io_kiocb *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06002571
Jens Axboe11365042019-10-16 09:08:32 -06002572 /*
2573 * Adjust the reqs sequence before the current one because it
2574 * will consume a slot in the cq_ring and the the cq_tail
2575 * pointer will be increased, otherwise other timeout reqs may
2576 * return in advance without waiting for enough wait_nr.
2577 */
2578 prev = req;
2579 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
2580 prev->sequence++;
Jens Axboe11365042019-10-16 09:08:32 -06002581 list_del_init(&req->list);
Jens Axboe11365042019-10-16 09:08:32 -06002582 }
Jens Axboe842f9612019-10-29 12:34:10 -06002583
Jens Axboe78e19bb2019-11-06 15:21:34 -07002584 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06002585 io_commit_cqring(ctx);
2586 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2587
2588 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002589 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06002590 io_put_req(req);
2591 return HRTIMER_NORESTART;
2592}
2593
Jens Axboe47f46762019-11-09 17:43:02 -07002594static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
2595{
2596 struct io_kiocb *req;
2597 int ret = -ENOENT;
2598
2599 list_for_each_entry(req, &ctx->timeout_list, list) {
2600 if (user_data == req->user_data) {
2601 list_del_init(&req->list);
2602 ret = 0;
2603 break;
2604 }
2605 }
2606
2607 if (ret == -ENOENT)
2608 return ret;
2609
Jens Axboe2d283902019-12-04 11:08:05 -07002610 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe47f46762019-11-09 17:43:02 -07002611 if (ret == -1)
2612 return -EALREADY;
2613
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002614 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07002615 io_cqring_fill_event(req, -ECANCELED);
2616 io_put_req(req);
2617 return 0;
2618}
2619
Jens Axboe11365042019-10-16 09:08:32 -06002620/*
2621 * Remove or update an existing timeout command
2622 */
2623static int io_timeout_remove(struct io_kiocb *req,
2624 const struct io_uring_sqe *sqe)
2625{
2626 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe11365042019-10-16 09:08:32 -06002627 unsigned flags;
Jens Axboe47f46762019-11-09 17:43:02 -07002628 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06002629
2630 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2631 return -EINVAL;
2632 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
2633 return -EINVAL;
2634 flags = READ_ONCE(sqe->timeout_flags);
2635 if (flags)
2636 return -EINVAL;
2637
Jens Axboe11365042019-10-16 09:08:32 -06002638 spin_lock_irq(&ctx->completion_lock);
Jens Axboe47f46762019-11-09 17:43:02 -07002639 ret = io_timeout_cancel(ctx, READ_ONCE(sqe->addr));
Jens Axboe11365042019-10-16 09:08:32 -06002640
Jens Axboe47f46762019-11-09 17:43:02 -07002641 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06002642 io_commit_cqring(ctx);
2643 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06002644 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002645 if (ret < 0)
2646 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08002647 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06002648 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06002649}
2650
Jens Axboe2d283902019-12-04 11:08:05 -07002651static int io_timeout_prep(struct io_kiocb *req, struct io_async_ctx *io,
2652 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06002653{
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002654 const struct io_uring_sqe *sqe = req->sqe;
Jens Axboead8a48a2019-11-15 08:49:11 -07002655 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06002656 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06002657
Jens Axboead8a48a2019-11-15 08:49:11 -07002658 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06002659 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07002660 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06002661 return -EINVAL;
Jens Axboe2d283902019-12-04 11:08:05 -07002662 if (sqe->off && is_timeout_link)
2663 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06002664 flags = READ_ONCE(sqe->timeout_flags);
2665 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06002666 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06002667
Jens Axboe2d283902019-12-04 11:08:05 -07002668 data = &io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07002669 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07002670 req->flags |= REQ_F_TIMEOUT;
2671
2672 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06002673 return -EFAULT;
2674
Jens Axboe11365042019-10-16 09:08:32 -06002675 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07002676 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06002677 else
Jens Axboead8a48a2019-11-15 08:49:11 -07002678 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06002679
Jens Axboead8a48a2019-11-15 08:49:11 -07002680 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
Jens Axboe2d283902019-12-04 11:08:05 -07002681 req->io = io;
Jens Axboead8a48a2019-11-15 08:49:11 -07002682 return 0;
2683}
2684
2685static int io_timeout(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2686{
2687 unsigned count;
2688 struct io_ring_ctx *ctx = req->ctx;
2689 struct io_timeout_data *data;
Jens Axboe2d283902019-12-04 11:08:05 -07002690 struct io_async_ctx *io;
Jens Axboead8a48a2019-11-15 08:49:11 -07002691 struct list_head *entry;
2692 unsigned span = 0;
Jens Axboead8a48a2019-11-15 08:49:11 -07002693
Jens Axboe2d283902019-12-04 11:08:05 -07002694 io = req->io;
2695 if (!io) {
2696 int ret;
2697
2698 io = kmalloc(sizeof(*io), GFP_KERNEL);
2699 if (!io)
2700 return -ENOMEM;
2701 ret = io_timeout_prep(req, io, false);
2702 if (ret) {
2703 kfree(io);
2704 return ret;
2705 }
2706 }
2707 data = &req->io->timeout;
Jens Axboe93bd25b2019-11-11 23:34:31 -07002708
Jens Axboe5262f562019-09-17 12:26:57 -06002709 /*
2710 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07002711 * timeout event to be satisfied. If it isn't set, then this is
2712 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06002713 */
2714 count = READ_ONCE(sqe->off);
Jens Axboe93bd25b2019-11-11 23:34:31 -07002715 if (!count) {
2716 req->flags |= REQ_F_TIMEOUT_NOSEQ;
2717 spin_lock_irq(&ctx->completion_lock);
2718 entry = ctx->timeout_list.prev;
2719 goto add;
2720 }
Jens Axboe5262f562019-09-17 12:26:57 -06002721
2722 req->sequence = ctx->cached_sq_head + count - 1;
Jens Axboe2d283902019-12-04 11:08:05 -07002723 data->seq_offset = count;
Jens Axboe5262f562019-09-17 12:26:57 -06002724
2725 /*
2726 * Insertion sort, ensuring the first entry in the list is always
2727 * the one we need first.
2728 */
Jens Axboe5262f562019-09-17 12:26:57 -06002729 spin_lock_irq(&ctx->completion_lock);
2730 list_for_each_prev(entry, &ctx->timeout_list) {
2731 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
yangerkun5da0fb12019-10-15 21:59:29 +08002732 unsigned nxt_sq_head;
2733 long long tmp, tmp_nxt;
Jens Axboe2d283902019-12-04 11:08:05 -07002734 u32 nxt_offset = nxt->io->timeout.seq_offset;
Jens Axboe5262f562019-09-17 12:26:57 -06002735
Jens Axboe93bd25b2019-11-11 23:34:31 -07002736 if (nxt->flags & REQ_F_TIMEOUT_NOSEQ)
2737 continue;
2738
yangerkun5da0fb12019-10-15 21:59:29 +08002739 /*
2740 * Since cached_sq_head + count - 1 can overflow, use type long
2741 * long to store it.
2742 */
2743 tmp = (long long)ctx->cached_sq_head + count - 1;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +03002744 nxt_sq_head = nxt->sequence - nxt_offset + 1;
2745 tmp_nxt = (long long)nxt_sq_head + nxt_offset - 1;
yangerkun5da0fb12019-10-15 21:59:29 +08002746
2747 /*
2748 * cached_sq_head may overflow, and it will never overflow twice
2749 * once there is some timeout req still be valid.
2750 */
2751 if (ctx->cached_sq_head < nxt_sq_head)
yangerkun8b07a652019-10-17 12:12:35 +08002752 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08002753
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002754 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06002755 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002756
2757 /*
2758 * Sequence of reqs after the insert one and itself should
2759 * be adjusted because each timeout req consumes a slot.
2760 */
2761 span++;
2762 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06002763 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002764 req->sequence -= span;
Jens Axboe93bd25b2019-11-11 23:34:31 -07002765add:
Jens Axboe5262f562019-09-17 12:26:57 -06002766 list_add(&req->list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07002767 data->timer.function = io_timeout_fn;
2768 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06002769 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06002770 return 0;
2771}
2772
Jens Axboe62755e32019-10-28 21:49:21 -06002773static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06002774{
Jens Axboe62755e32019-10-28 21:49:21 -06002775 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06002776
Jens Axboe62755e32019-10-28 21:49:21 -06002777 return req->user_data == (unsigned long) data;
2778}
2779
Jens Axboee977d6d2019-11-05 12:39:45 -07002780static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06002781{
Jens Axboe62755e32019-10-28 21:49:21 -06002782 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06002783 int ret = 0;
2784
Jens Axboe62755e32019-10-28 21:49:21 -06002785 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
2786 switch (cancel_ret) {
2787 case IO_WQ_CANCEL_OK:
2788 ret = 0;
2789 break;
2790 case IO_WQ_CANCEL_RUNNING:
2791 ret = -EALREADY;
2792 break;
2793 case IO_WQ_CANCEL_NOTFOUND:
2794 ret = -ENOENT;
2795 break;
2796 }
2797
Jens Axboee977d6d2019-11-05 12:39:45 -07002798 return ret;
2799}
2800
Jens Axboe47f46762019-11-09 17:43:02 -07002801static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
2802 struct io_kiocb *req, __u64 sqe_addr,
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002803 struct io_kiocb **nxt, int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07002804{
2805 unsigned long flags;
2806 int ret;
2807
2808 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
2809 if (ret != -ENOENT) {
2810 spin_lock_irqsave(&ctx->completion_lock, flags);
2811 goto done;
2812 }
2813
2814 spin_lock_irqsave(&ctx->completion_lock, flags);
2815 ret = io_timeout_cancel(ctx, sqe_addr);
2816 if (ret != -ENOENT)
2817 goto done;
2818 ret = io_poll_cancel(ctx, sqe_addr);
2819done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002820 if (!ret)
2821 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07002822 io_cqring_fill_event(req, ret);
2823 io_commit_cqring(ctx);
2824 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2825 io_cqring_ev_posted(ctx);
2826
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002827 if (ret < 0)
2828 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07002829 io_put_req_find_next(req, nxt);
2830}
2831
Jens Axboee977d6d2019-11-05 12:39:45 -07002832static int io_async_cancel(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2833 struct io_kiocb **nxt)
2834{
2835 struct io_ring_ctx *ctx = req->ctx;
Jens Axboee977d6d2019-11-05 12:39:45 -07002836
2837 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2838 return -EINVAL;
2839 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
2840 sqe->cancel_flags)
2841 return -EINVAL;
2842
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002843 io_async_find_and_cancel(ctx, req, READ_ONCE(sqe->addr), nxt, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06002844 return 0;
2845}
2846
Jens Axboef67676d2019-12-02 11:03:47 -07002847static int io_req_defer_prep(struct io_kiocb *req, struct io_async_ctx *io)
2848{
2849 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
2850 struct iov_iter iter;
2851 ssize_t ret;
2852
2853 memcpy(&io->sqe, req->sqe, sizeof(io->sqe));
2854 req->sqe = &io->sqe;
2855
2856 switch (io->sqe.opcode) {
2857 case IORING_OP_READV:
2858 case IORING_OP_READ_FIXED:
2859 ret = io_read_prep(req, &iovec, &iter, true);
2860 break;
2861 case IORING_OP_WRITEV:
2862 case IORING_OP_WRITE_FIXED:
2863 ret = io_write_prep(req, &iovec, &iter, true);
2864 break;
Jens Axboe03b12302019-12-02 18:50:25 -07002865 case IORING_OP_SENDMSG:
2866 ret = io_sendmsg_prep(req, io);
2867 break;
2868 case IORING_OP_RECVMSG:
2869 ret = io_recvmsg_prep(req, io);
2870 break;
Jens Axboef499a022019-12-02 16:28:46 -07002871 case IORING_OP_CONNECT:
2872 ret = io_connect_prep(req, io);
2873 break;
Jens Axboe2d283902019-12-04 11:08:05 -07002874 case IORING_OP_TIMEOUT:
2875 return io_timeout_prep(req, io, false);
2876 case IORING_OP_LINK_TIMEOUT:
2877 return io_timeout_prep(req, io, true);
Jens Axboef67676d2019-12-02 11:03:47 -07002878 default:
2879 req->io = io;
2880 return 0;
2881 }
2882
2883 if (ret < 0)
2884 return ret;
2885
2886 req->io = io;
2887 io_req_map_io(req, ret, iovec, inline_vecs, &iter);
2888 return 0;
2889}
2890
Jackie Liua197f662019-11-08 08:09:12 -07002891static int io_req_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -06002892{
Jackie Liua197f662019-11-08 08:09:12 -07002893 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe1a6b74f2019-12-02 10:33:15 -07002894 struct io_async_ctx *io;
Jens Axboef67676d2019-12-02 11:03:47 -07002895 int ret;
Jens Axboede0617e2019-04-06 21:51:27 -06002896
Bob Liu9d858b22019-11-13 18:06:25 +08002897 /* Still need defer if there is pending req in defer list. */
2898 if (!req_need_defer(req) && list_empty(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06002899 return 0;
2900
Jens Axboe1a6b74f2019-12-02 10:33:15 -07002901 io = kmalloc(sizeof(*io), GFP_KERNEL);
2902 if (!io)
Jens Axboede0617e2019-04-06 21:51:27 -06002903 return -EAGAIN;
2904
Jens Axboe2d283902019-12-04 11:08:05 -07002905 ret = io_req_defer_prep(req, io);
2906 if (ret < 0) {
2907 kfree(io);
2908 return ret;
2909 }
2910
Jens Axboede0617e2019-04-06 21:51:27 -06002911 spin_lock_irq(&ctx->completion_lock);
Bob Liu9d858b22019-11-13 18:06:25 +08002912 if (!req_need_defer(req) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06002913 spin_unlock_irq(&ctx->completion_lock);
Jens Axboede0617e2019-04-06 21:51:27 -06002914 return 0;
2915 }
2916
Jens Axboe915967f2019-11-21 09:01:20 -07002917 trace_io_uring_defer(ctx, req, req->user_data);
Jens Axboede0617e2019-04-06 21:51:27 -06002918 list_add_tail(&req->list, &ctx->defer_list);
2919 spin_unlock_irq(&ctx->completion_lock);
2920 return -EIOCBQUEUED;
2921}
2922
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03002923__attribute__((nonnull))
Pavel Begunkovd7324472019-11-21 21:24:36 +03002924static int io_issue_sqe(struct io_kiocb *req, struct io_kiocb **nxt,
2925 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002926{
Jens Axboee0c5c572019-03-12 10:18:47 -06002927 int ret, opcode;
Jackie Liua197f662019-11-08 08:09:12 -07002928 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002929
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002930 opcode = READ_ONCE(req->sqe->opcode);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002931 switch (opcode) {
2932 case IORING_OP_NOP:
Jens Axboe78e19bb2019-11-06 15:21:34 -07002933 ret = io_nop(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002934 break;
2935 case IORING_OP_READV:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002936 if (unlikely(req->sqe->buf_index))
Jens Axboeedafcce2019-01-09 09:16:05 -07002937 return -EINVAL;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002938 ret = io_read(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002939 break;
2940 case IORING_OP_WRITEV:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002941 if (unlikely(req->sqe->buf_index))
Jens Axboeedafcce2019-01-09 09:16:05 -07002942 return -EINVAL;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002943 ret = io_write(req, nxt, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07002944 break;
2945 case IORING_OP_READ_FIXED:
Pavel Begunkov267bc902019-11-07 01:41:08 +03002946 ret = io_read(req, nxt, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07002947 break;
2948 case IORING_OP_WRITE_FIXED:
Pavel Begunkov267bc902019-11-07 01:41:08 +03002949 ret = io_write(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002950 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002951 case IORING_OP_FSYNC:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002952 ret = io_fsync(req, req->sqe, nxt, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002953 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002954 case IORING_OP_POLL_ADD:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002955 ret = io_poll_add(req, req->sqe, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002956 break;
2957 case IORING_OP_POLL_REMOVE:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002958 ret = io_poll_remove(req, req->sqe);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002959 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002960 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002961 ret = io_sync_file_range(req, req->sqe, nxt, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002962 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002963 case IORING_OP_SENDMSG:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002964 ret = io_sendmsg(req, req->sqe, nxt, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06002965 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06002966 case IORING_OP_RECVMSG:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002967 ret = io_recvmsg(req, req->sqe, nxt, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06002968 break;
Jens Axboe5262f562019-09-17 12:26:57 -06002969 case IORING_OP_TIMEOUT:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002970 ret = io_timeout(req, req->sqe);
Jens Axboe5262f562019-09-17 12:26:57 -06002971 break;
Jens Axboe11365042019-10-16 09:08:32 -06002972 case IORING_OP_TIMEOUT_REMOVE:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002973 ret = io_timeout_remove(req, req->sqe);
Jens Axboe11365042019-10-16 09:08:32 -06002974 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06002975 case IORING_OP_ACCEPT:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002976 ret = io_accept(req, req->sqe, nxt, force_nonblock);
Jens Axboe17f2fe32019-10-17 14:42:58 -06002977 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07002978 case IORING_OP_CONNECT:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002979 ret = io_connect(req, req->sqe, nxt, force_nonblock);
Jens Axboef8e85cf2019-11-23 14:24:24 -07002980 break;
Jens Axboe62755e32019-10-28 21:49:21 -06002981 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002982 ret = io_async_cancel(req, req->sqe, nxt);
Jens Axboe62755e32019-10-28 21:49:21 -06002983 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002984 default:
2985 ret = -EINVAL;
2986 break;
2987 }
2988
Jens Axboedef596e2019-01-09 08:59:42 -07002989 if (ret)
2990 return ret;
2991
2992 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe9e645e112019-05-10 16:07:28 -06002993 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07002994 return -EAGAIN;
2995
Jens Axboedef596e2019-01-09 08:59:42 -07002996 io_iopoll_req_issued(req);
Jens Axboedef596e2019-01-09 08:59:42 -07002997 }
2998
2999 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003000}
3001
Jens Axboeb76da702019-11-20 13:05:32 -07003002static void io_link_work_cb(struct io_wq_work **workptr)
3003{
3004 struct io_wq_work *work = *workptr;
3005 struct io_kiocb *link = work->data;
3006
3007 io_queue_linked_timeout(link);
3008 work->func = io_wq_submit_work;
3009}
3010
Jens Axboe561fb042019-10-24 07:25:42 -06003011static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboe31b51512019-01-18 22:56:34 -07003012{
Jens Axboe561fb042019-10-24 07:25:42 -06003013 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003014 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe561fb042019-10-24 07:25:42 -06003015 struct io_kiocb *nxt = NULL;
3016 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003017
Jens Axboe561fb042019-10-24 07:25:42 -06003018 /* Ensure we clear previously set non-block flag */
3019 req->rw.ki_flags &= ~IOCB_NOWAIT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003020
Jens Axboe561fb042019-10-24 07:25:42 -06003021 if (work->flags & IO_WQ_WORK_CANCEL)
3022 ret = -ECANCELED;
Jens Axboe31b51512019-01-18 22:56:34 -07003023
Jens Axboe561fb042019-10-24 07:25:42 -06003024 if (!ret) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003025 req->has_user = (work->flags & IO_WQ_WORK_HAS_MM) != 0;
3026 req->in_async = true;
Jens Axboe561fb042019-10-24 07:25:42 -06003027 do {
Pavel Begunkovd7324472019-11-21 21:24:36 +03003028 ret = io_issue_sqe(req, &nxt, false);
Jens Axboe561fb042019-10-24 07:25:42 -06003029 /*
3030 * We can get EAGAIN for polled IO even though we're
3031 * forcing a sync submission from here, since we can't
3032 * wait for request slots on the block side.
3033 */
3034 if (ret != -EAGAIN)
3035 break;
3036 cond_resched();
3037 } while (1);
3038 }
Jens Axboe31b51512019-01-18 22:56:34 -07003039
Jens Axboe561fb042019-10-24 07:25:42 -06003040 /* drop submission reference */
Jackie Liuec9c02a2019-11-08 23:50:36 +08003041 io_put_req(req);
Jens Axboe817869d2019-04-30 14:44:05 -06003042
Jens Axboe561fb042019-10-24 07:25:42 -06003043 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003044 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07003045 io_cqring_add_event(req, ret);
Jens Axboe817869d2019-04-30 14:44:05 -06003046 io_put_req(req);
Jens Axboeedafcce2019-01-09 09:16:05 -07003047 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003048
Jens Axboe561fb042019-10-24 07:25:42 -06003049 /* if a dependent link is ready, pass it back */
3050 if (!ret && nxt) {
Jens Axboe94ae5e72019-11-14 19:39:52 -07003051 struct io_kiocb *link;
3052
3053 io_prep_async_work(nxt, &link);
Jens Axboe561fb042019-10-24 07:25:42 -06003054 *workptr = &nxt->work;
Jens Axboeb76da702019-11-20 13:05:32 -07003055 if (link) {
3056 nxt->work.flags |= IO_WQ_WORK_CB;
3057 nxt->work.func = io_link_work_cb;
3058 nxt->work.data = link;
3059 }
Jens Axboeedafcce2019-01-09 09:16:05 -07003060 }
Jens Axboe31b51512019-01-18 22:56:34 -07003061}
Jens Axboe2b188cc2019-01-07 10:46:33 -07003062
Jens Axboe09bb8392019-03-13 12:39:28 -06003063static bool io_op_needs_file(const struct io_uring_sqe *sqe)
3064{
3065 int op = READ_ONCE(sqe->opcode);
3066
3067 switch (op) {
3068 case IORING_OP_NOP:
3069 case IORING_OP_POLL_REMOVE:
Pavel Begunkov5683e542019-11-14 00:59:19 +03003070 case IORING_OP_TIMEOUT:
Pavel Begunkova320e9f2019-11-14 00:11:01 +03003071 case IORING_OP_TIMEOUT_REMOVE:
3072 case IORING_OP_ASYNC_CANCEL:
3073 case IORING_OP_LINK_TIMEOUT:
Jens Axboe09bb8392019-03-13 12:39:28 -06003074 return false;
3075 default:
3076 return true;
3077 }
3078}
3079
Jens Axboe65e19f52019-10-26 07:20:21 -06003080static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
3081 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06003082{
Jens Axboe65e19f52019-10-26 07:20:21 -06003083 struct fixed_file_table *table;
3084
3085 table = &ctx->file_table[index >> IORING_FILE_TABLE_SHIFT];
3086 return table->files[index & IORING_FILE_TABLE_MASK];
3087}
3088
Jackie Liua197f662019-11-08 08:09:12 -07003089static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req)
Jens Axboe09bb8392019-03-13 12:39:28 -06003090{
Jackie Liua197f662019-11-08 08:09:12 -07003091 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe09bb8392019-03-13 12:39:28 -06003092 unsigned flags;
3093 int fd;
3094
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003095 flags = READ_ONCE(req->sqe->flags);
3096 fd = READ_ONCE(req->sqe->fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06003097
Jackie Liu4fe2c962019-09-09 20:50:40 +08003098 if (flags & IOSQE_IO_DRAIN)
Jens Axboede0617e2019-04-06 21:51:27 -06003099 req->flags |= REQ_F_IO_DRAIN;
Jens Axboede0617e2019-04-06 21:51:27 -06003100
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003101 if (!io_op_needs_file(req->sqe))
Jens Axboe09bb8392019-03-13 12:39:28 -06003102 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06003103
3104 if (flags & IOSQE_FIXED_FILE) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003105 if (unlikely(!ctx->file_table ||
Jens Axboe09bb8392019-03-13 12:39:28 -06003106 (unsigned) fd >= ctx->nr_user_files))
3107 return -EBADF;
Jens Axboeb7620122019-10-26 07:22:55 -06003108 fd = array_index_nospec(fd, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06003109 req->file = io_file_from_index(ctx, fd);
3110 if (!req->file)
Jens Axboe08a45172019-10-03 08:11:03 -06003111 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06003112 req->flags |= REQ_F_FIXED_FILE;
3113 } else {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003114 if (req->needs_fixed_file)
Jens Axboe09bb8392019-03-13 12:39:28 -06003115 return -EBADF;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02003116 trace_io_uring_file_get(ctx, fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06003117 req->file = io_file_get(state, fd);
3118 if (unlikely(!req->file))
3119 return -EBADF;
3120 }
3121
3122 return 0;
3123}
3124
Jackie Liua197f662019-11-08 08:09:12 -07003125static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003126{
Jens Axboefcb323c2019-10-24 12:39:47 -06003127 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07003128 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06003129
3130 rcu_read_lock();
3131 spin_lock_irq(&ctx->inflight_lock);
3132 /*
3133 * We use the f_ops->flush() handler to ensure that we can flush
3134 * out work accessing these files if the fd is closed. Check if
3135 * the fd has changed since we started down this path, and disallow
3136 * this operation if it has.
3137 */
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003138 if (fcheck(req->ring_fd) == req->ring_file) {
Jens Axboefcb323c2019-10-24 12:39:47 -06003139 list_add(&req->inflight_entry, &ctx->inflight_list);
3140 req->flags |= REQ_F_INFLIGHT;
3141 req->work.files = current->files;
3142 ret = 0;
3143 }
3144 spin_unlock_irq(&ctx->inflight_lock);
3145 rcu_read_unlock();
3146
3147 return ret;
3148}
3149
Jens Axboe2665abf2019-11-05 12:40:47 -07003150static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
3151{
Jens Axboead8a48a2019-11-15 08:49:11 -07003152 struct io_timeout_data *data = container_of(timer,
3153 struct io_timeout_data, timer);
3154 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07003155 struct io_ring_ctx *ctx = req->ctx;
3156 struct io_kiocb *prev = NULL;
3157 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07003158
3159 spin_lock_irqsave(&ctx->completion_lock, flags);
3160
3161 /*
3162 * We don't expect the list to be empty, that will only happen if we
3163 * race with the completion of the linked work.
3164 */
Pavel Begunkov44932332019-12-05 16:16:35 +03003165 if (!list_empty(&req->link_list)) {
3166 prev = list_entry(req->link_list.prev, struct io_kiocb,
3167 link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07003168 if (refcount_inc_not_zero(&prev->refs)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03003169 list_del_init(&req->link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07003170 prev->flags &= ~REQ_F_LINK_TIMEOUT;
3171 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07003172 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07003173 }
3174
3175 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3176
3177 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003178 req_set_fail_links(prev);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003179 io_async_find_and_cancel(ctx, req, prev->user_data, NULL,
3180 -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07003181 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07003182 } else {
3183 io_cqring_add_event(req, -ETIME);
3184 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07003185 }
Jens Axboe2665abf2019-11-05 12:40:47 -07003186 return HRTIMER_NORESTART;
3187}
3188
Jens Axboead8a48a2019-11-15 08:49:11 -07003189static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07003190{
Jens Axboe76a46e02019-11-10 23:34:16 -07003191 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07003192
Jens Axboe76a46e02019-11-10 23:34:16 -07003193 /*
3194 * If the list is now empty, then our linked request finished before
3195 * we got a chance to setup the timer
3196 */
3197 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov44932332019-12-05 16:16:35 +03003198 if (!list_empty(&req->link_list)) {
Jens Axboe2d283902019-12-04 11:08:05 -07003199 struct io_timeout_data *data = &req->io->timeout;
Jens Axboe94ae5e72019-11-14 19:39:52 -07003200
Jens Axboead8a48a2019-11-15 08:49:11 -07003201 data->timer.function = io_link_timeout_fn;
3202 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
3203 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07003204 }
Jens Axboe76a46e02019-11-10 23:34:16 -07003205 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07003206
Jens Axboe2665abf2019-11-05 12:40:47 -07003207 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07003208 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07003209}
3210
Jens Axboead8a48a2019-11-15 08:49:11 -07003211static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07003212{
3213 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003214
Jens Axboe2665abf2019-11-05 12:40:47 -07003215 if (!(req->flags & REQ_F_LINK))
3216 return NULL;
3217
Pavel Begunkov44932332019-12-05 16:16:35 +03003218 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
3219 link_list);
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003220 if (!nxt || nxt->sqe->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07003221 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07003222
Jens Axboe76a46e02019-11-10 23:34:16 -07003223 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07003224 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07003225}
3226
Jens Axboe0e0702d2019-11-14 21:42:10 -07003227static void __io_queue_sqe(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003228{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07003229 struct io_kiocb *linked_timeout;
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03003230 struct io_kiocb *nxt = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003231 int ret;
3232
Jens Axboe4a0a7a12019-12-09 20:01:01 -07003233again:
3234 linked_timeout = io_prep_linked_timeout(req);
3235
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03003236 ret = io_issue_sqe(req, &nxt, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06003237
3238 /*
3239 * We async punt it if the file wasn't marked NOWAIT, or if the file
3240 * doesn't support non-blocking read/write attempts
3241 */
3242 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
3243 (req->flags & REQ_F_MUST_PUNT))) {
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03003244 if (req->work.flags & IO_WQ_WORK_NEEDS_FILES) {
3245 ret = io_grab_files(req);
3246 if (ret)
3247 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003248 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03003249
3250 /*
3251 * Queued up for async execution, worker will release
3252 * submit reference when the iocb is actually submitted.
3253 */
3254 io_queue_async_work(req);
Jens Axboe4a0a7a12019-12-09 20:01:01 -07003255 goto done_req;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003256 }
Jens Axboee65ef562019-03-12 10:16:44 -06003257
Jens Axboefcb323c2019-10-24 12:39:47 -06003258err:
Jens Axboee65ef562019-03-12 10:16:44 -06003259 /* drop submission reference */
3260 io_put_req(req);
3261
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03003262 if (linked_timeout) {
Jens Axboe76a46e02019-11-10 23:34:16 -07003263 if (!ret)
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03003264 io_queue_linked_timeout(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07003265 else
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03003266 io_put_req(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07003267 }
3268
Jens Axboee65ef562019-03-12 10:16:44 -06003269 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06003270 if (ret) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07003271 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003272 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06003273 io_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06003274 }
Jens Axboe4a0a7a12019-12-09 20:01:01 -07003275done_req:
3276 if (nxt) {
3277 req = nxt;
3278 nxt = NULL;
3279 goto again;
3280 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003281}
3282
Jens Axboe0e0702d2019-11-14 21:42:10 -07003283static void io_queue_sqe(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08003284{
3285 int ret;
3286
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003287 if (unlikely(req->ctx->drain_next)) {
3288 req->flags |= REQ_F_IO_DRAIN;
3289 req->ctx->drain_next = false;
3290 }
3291 req->ctx->drain_next = (req->flags & REQ_F_DRAIN_LINK);
3292
Jackie Liua197f662019-11-08 08:09:12 -07003293 ret = io_req_defer(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08003294 if (ret) {
3295 if (ret != -EIOCBQUEUED) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07003296 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003297 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07003298 io_double_put_req(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08003299 }
Jens Axboe0e0702d2019-11-14 21:42:10 -07003300 } else
3301 __io_queue_sqe(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08003302}
3303
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003304static inline void io_queue_link_head(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08003305{
Jens Axboe94ae5e72019-11-14 19:39:52 -07003306 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003307 io_cqring_add_event(req, -ECANCELED);
3308 io_double_put_req(req);
3309 } else
Jens Axboe0e0702d2019-11-14 21:42:10 -07003310 io_queue_sqe(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08003311}
3312
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003313
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003314#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
3315 IOSQE_IO_HARDLINK)
Jens Axboe9e645e112019-05-10 16:07:28 -06003316
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03003317static bool io_submit_sqe(struct io_kiocb *req, struct io_submit_state *state,
Jackie Liua197f662019-11-08 08:09:12 -07003318 struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06003319{
Jackie Liua197f662019-11-08 08:09:12 -07003320 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -06003321 int ret;
3322
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003323 req->user_data = req->sqe->user_data;
Jens Axboe78e19bb2019-11-06 15:21:34 -07003324
Jens Axboe9e645e112019-05-10 16:07:28 -06003325 /* enforce forwards compatibility on users */
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003326 if (unlikely(req->sqe->flags & ~SQE_VALID_FLAGS)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06003327 ret = -EINVAL;
Pavel Begunkov196be952019-11-07 01:41:06 +03003328 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06003329 }
3330
Jackie Liua197f662019-11-08 08:09:12 -07003331 ret = io_req_set_file(state, req);
Jens Axboe9e645e112019-05-10 16:07:28 -06003332 if (unlikely(ret)) {
3333err_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07003334 io_cqring_add_event(req, ret);
3335 io_double_put_req(req);
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03003336 return false;
Jens Axboe9e645e112019-05-10 16:07:28 -06003337 }
3338
Jens Axboe9e645e112019-05-10 16:07:28 -06003339 /*
3340 * If we already have a head request, queue this one for async
3341 * submittal once the head completes. If we don't have a head but
3342 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
3343 * submitted sync once the chain is complete. If none of those
3344 * conditions are true (normal request), then just queue it.
3345 */
3346 if (*link) {
3347 struct io_kiocb *prev = *link;
Jens Axboe1a6b74f2019-12-02 10:33:15 -07003348 struct io_async_ctx *io;
Jens Axboe9e645e112019-05-10 16:07:28 -06003349
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003350 if (req->sqe->flags & IOSQE_IO_DRAIN)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003351 (*link)->flags |= REQ_F_DRAIN_LINK | REQ_F_IO_DRAIN;
3352
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003353 if (req->sqe->flags & IOSQE_IO_HARDLINK)
3354 req->flags |= REQ_F_HARDLINK;
3355
Jens Axboe1a6b74f2019-12-02 10:33:15 -07003356 io = kmalloc(sizeof(*io), GFP_KERNEL);
3357 if (!io) {
Jens Axboe9e645e112019-05-10 16:07:28 -06003358 ret = -EAGAIN;
3359 goto err_req;
3360 }
3361
Jens Axboef67676d2019-12-02 11:03:47 -07003362 ret = io_req_defer_prep(req, io);
Jens Axboe2d283902019-12-04 11:08:05 -07003363 if (ret) {
3364 kfree(io);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003365 /* fail even hard links since we don't submit */
Jens Axboe2d283902019-12-04 11:08:05 -07003366 prev->flags |= REQ_F_FAIL_LINK;
Jens Axboef67676d2019-12-02 11:03:47 -07003367 goto err_req;
Jens Axboe2d283902019-12-04 11:08:05 -07003368 }
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02003369 trace_io_uring_link(ctx, req, prev);
Pavel Begunkov44932332019-12-05 16:16:35 +03003370 list_add_tail(&req->link_list, &prev->link_list);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003371 } else if (req->sqe->flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06003372 req->flags |= REQ_F_LINK;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003373 if (req->sqe->flags & IOSQE_IO_HARDLINK)
3374 req->flags |= REQ_F_HARDLINK;
Jens Axboe9e645e112019-05-10 16:07:28 -06003375
Jens Axboe9e645e112019-05-10 16:07:28 -06003376 INIT_LIST_HEAD(&req->link_list);
3377 *link = req;
3378 } else {
Jackie Liua197f662019-11-08 08:09:12 -07003379 io_queue_sqe(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06003380 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03003381
3382 return true;
Jens Axboe9e645e112019-05-10 16:07:28 -06003383}
3384
Jens Axboe9a56a232019-01-09 09:06:50 -07003385/*
3386 * Batched submission is done, ensure local IO is flushed out.
3387 */
3388static void io_submit_state_end(struct io_submit_state *state)
3389{
3390 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06003391 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07003392 if (state->free_reqs)
3393 kmem_cache_free_bulk(req_cachep, state->free_reqs,
3394 &state->reqs[state->cur_req]);
Jens Axboe9a56a232019-01-09 09:06:50 -07003395}
3396
3397/*
3398 * Start submission side cache.
3399 */
3400static void io_submit_state_start(struct io_submit_state *state,
Jackie Liu22efde52019-12-02 17:14:52 +08003401 unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07003402{
3403 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07003404 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07003405 state->file = NULL;
3406 state->ios_left = max_ios;
3407}
3408
Jens Axboe2b188cc2019-01-07 10:46:33 -07003409static void io_commit_sqring(struct io_ring_ctx *ctx)
3410{
Hristo Venev75b28af2019-08-26 17:23:46 +00003411 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003412
Hristo Venev75b28af2019-08-26 17:23:46 +00003413 if (ctx->cached_sq_head != READ_ONCE(rings->sq.head)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07003414 /*
3415 * Ensure any loads from the SQEs are done at this point,
3416 * since once we write the new head, the application could
3417 * write new data to them.
3418 */
Hristo Venev75b28af2019-08-26 17:23:46 +00003419 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003420 }
3421}
3422
3423/*
Jens Axboe2b188cc2019-01-07 10:46:33 -07003424 * Fetch an sqe, if one is available. Note that s->sqe will point to memory
3425 * that is mapped by userspace. This means that care needs to be taken to
3426 * ensure that reads are stable, as we cannot rely on userspace always
3427 * being a good citizen. If members of the sqe are validated and then later
3428 * used, it's important that those reads are done through READ_ONCE() to
3429 * prevent a re-load down the line.
3430 */
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003431static bool io_get_sqring(struct io_ring_ctx *ctx, struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003432{
Hristo Venev75b28af2019-08-26 17:23:46 +00003433 struct io_rings *rings = ctx->rings;
3434 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003435 unsigned head;
3436
3437 /*
3438 * The cached sq head (or cq tail) serves two purposes:
3439 *
3440 * 1) allows us to batch the cost of updating the user visible
3441 * head updates.
3442 * 2) allows the kernel side to track the head on its own, even
3443 * though the application is the one updating it.
3444 */
3445 head = ctx->cached_sq_head;
Stefan Bühlere523a292019-04-19 11:57:44 +02003446 /* make sure SQ entry isn't read before tail */
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03003447 if (unlikely(head == smp_load_acquire(&rings->sq.tail)))
Jens Axboe2b188cc2019-01-07 10:46:33 -07003448 return false;
3449
Hristo Venev75b28af2019-08-26 17:23:46 +00003450 head = READ_ONCE(sq_array[head & ctx->sq_mask]);
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03003451 if (likely(head < ctx->sq_entries)) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003452 /*
3453 * All io need record the previous position, if LINK vs DARIN,
3454 * it can be used to mark the position of the first IO in the
3455 * link list.
3456 */
3457 req->sequence = ctx->cached_sq_head;
3458 req->sqe = &ctx->sq_sqes[head];
Jens Axboe2b188cc2019-01-07 10:46:33 -07003459 ctx->cached_sq_head++;
3460 return true;
3461 }
3462
3463 /* drop invalid entries */
3464 ctx->cached_sq_head++;
Jens Axboe498ccd92019-10-25 10:04:25 -06003465 ctx->cached_sq_dropped++;
3466 WRITE_ONCE(rings->sq_dropped, ctx->cached_sq_dropped);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003467 return false;
3468}
3469
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003470static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkovae9428c2019-11-06 00:22:14 +03003471 struct file *ring_file, int ring_fd,
3472 struct mm_struct **mm, bool async)
Jens Axboe6c271ce2019-01-10 11:22:30 -07003473{
3474 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06003475 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06003476 int i, submitted = 0;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03003477 bool mm_fault = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003478
Jens Axboec4a2ed72019-11-21 21:01:26 -07003479 /* if we have a backlog and couldn't flush it all, return BUSY */
3480 if (!list_empty(&ctx->cq_overflow_list) &&
3481 !io_cqring_overflow_flush(ctx, false))
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003482 return -EBUSY;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003483
3484 if (nr > IO_PLUG_THRESHOLD) {
Jackie Liu22efde52019-12-02 17:14:52 +08003485 io_submit_state_start(&state, nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003486 statep = &state;
3487 }
3488
3489 for (i = 0; i < nr; i++) {
Pavel Begunkov196be952019-11-07 01:41:06 +03003490 struct io_kiocb *req;
Pavel Begunkov50585b92019-11-07 01:41:07 +03003491 unsigned int sqe_flags;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003492
Pavel Begunkov196be952019-11-07 01:41:06 +03003493 req = io_get_req(ctx, statep);
3494 if (unlikely(!req)) {
3495 if (!submitted)
3496 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003497 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06003498 }
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003499 if (!io_get_sqring(ctx, req)) {
Pavel Begunkov196be952019-11-07 01:41:06 +03003500 __io_free_req(req);
3501 break;
3502 }
Jens Axboe9e645e112019-05-10 16:07:28 -06003503
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003504 if (io_sqe_needs_user(req->sqe) && !*mm) {
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03003505 mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm);
3506 if (!mm_fault) {
3507 use_mm(ctx->sqo_mm);
3508 *mm = ctx->sqo_mm;
3509 }
3510 }
3511
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03003512 submitted++;
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003513 sqe_flags = req->sqe->flags;
Pavel Begunkov50585b92019-11-07 01:41:07 +03003514
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003515 req->ring_file = ring_file;
3516 req->ring_fd = ring_fd;
3517 req->has_user = *mm != NULL;
3518 req->in_async = async;
3519 req->needs_fixed_file = async;
3520 trace_io_uring_submit_sqe(ctx, req->sqe->user_data,
Pavel Begunkov50585b92019-11-07 01:41:07 +03003521 true, async);
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03003522 if (!io_submit_sqe(req, statep, &link))
3523 break;
Pavel Begunkove5eb6362019-11-06 00:22:15 +03003524 /*
3525 * If previous wasn't linked and we have a linked command,
3526 * that's the end of the chain. Submit the previous link.
3527 */
Pavel Begunkov50585b92019-11-07 01:41:07 +03003528 if (!(sqe_flags & IOSQE_IO_LINK) && link) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003529 io_queue_link_head(link);
Pavel Begunkove5eb6362019-11-06 00:22:15 +03003530 link = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003531 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07003532 }
3533
Jens Axboe9e645e112019-05-10 16:07:28 -06003534 if (link)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003535 io_queue_link_head(link);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003536 if (statep)
3537 io_submit_state_end(&state);
3538
Pavel Begunkovae9428c2019-11-06 00:22:14 +03003539 /* Commit SQ ring head once we've consumed and submitted all SQEs */
3540 io_commit_sqring(ctx);
3541
Jens Axboe6c271ce2019-01-10 11:22:30 -07003542 return submitted;
3543}
3544
3545static int io_sq_thread(void *data)
3546{
Jens Axboe6c271ce2019-01-10 11:22:30 -07003547 struct io_ring_ctx *ctx = data;
3548 struct mm_struct *cur_mm = NULL;
Jens Axboe181e4482019-11-25 08:52:30 -07003549 const struct cred *old_cred;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003550 mm_segment_t old_fs;
3551 DEFINE_WAIT(wait);
3552 unsigned inflight;
3553 unsigned long timeout;
Jens Axboec1edbf52019-11-10 16:56:04 -07003554 int ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003555
Jens Axboe206aefd2019-11-07 18:27:42 -07003556 complete(&ctx->completions[1]);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08003557
Jens Axboe6c271ce2019-01-10 11:22:30 -07003558 old_fs = get_fs();
3559 set_fs(USER_DS);
Jens Axboe181e4482019-11-25 08:52:30 -07003560 old_cred = override_creds(ctx->creds);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003561
Jens Axboec1edbf52019-11-10 16:56:04 -07003562 ret = timeout = inflight = 0;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003563 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003564 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003565
3566 if (inflight) {
3567 unsigned nr_events = 0;
3568
3569 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe2b2ed972019-10-25 10:06:15 -06003570 /*
3571 * inflight is the count of the maximum possible
3572 * entries we submitted, but it can be smaller
3573 * if we dropped some of them. If we don't have
3574 * poll entries available, then we know that we
3575 * have nothing left to poll for. Reset the
3576 * inflight count to zero in that case.
3577 */
3578 mutex_lock(&ctx->uring_lock);
3579 if (!list_empty(&ctx->poll_list))
3580 __io_iopoll_check(ctx, &nr_events, 0);
3581 else
3582 inflight = 0;
3583 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003584 } else {
3585 /*
3586 * Normal IO, just pretend everything completed.
3587 * We don't have to poll completions for that.
3588 */
3589 nr_events = inflight;
3590 }
3591
3592 inflight -= nr_events;
3593 if (!inflight)
3594 timeout = jiffies + ctx->sq_thread_idle;
3595 }
3596
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003597 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07003598
3599 /*
3600 * If submit got -EBUSY, flag us as needing the application
3601 * to enter the kernel to reap and flush events.
3602 */
3603 if (!to_submit || ret == -EBUSY) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07003604 /*
3605 * We're polling. If we're within the defined idle
3606 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07003607 * to sleep. The exception is if we got EBUSY doing
3608 * more IO, we should wait for the application to
3609 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07003610 */
Jens Axboec1edbf52019-11-10 16:56:04 -07003611 if (inflight ||
3612 (!time_after(jiffies, timeout) && ret != -EBUSY)) {
Jens Axboe9831a902019-09-19 09:48:55 -06003613 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07003614 continue;
3615 }
3616
3617 /*
3618 * Drop cur_mm before scheduling, we can't hold it for
3619 * long periods (or over schedule()). Do this before
3620 * adding ourselves to the waitqueue, as the unuse/drop
3621 * may sleep.
3622 */
3623 if (cur_mm) {
3624 unuse_mm(cur_mm);
3625 mmput(cur_mm);
3626 cur_mm = NULL;
3627 }
3628
3629 prepare_to_wait(&ctx->sqo_wait, &wait,
3630 TASK_INTERRUPTIBLE);
3631
3632 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00003633 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02003634 /* make sure to read SQ tail after writing flags */
3635 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07003636
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003637 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07003638 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003639 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07003640 finish_wait(&ctx->sqo_wait, &wait);
3641 break;
3642 }
3643 if (signal_pending(current))
3644 flush_signals(current);
3645 schedule();
3646 finish_wait(&ctx->sqo_wait, &wait);
3647
Hristo Venev75b28af2019-08-26 17:23:46 +00003648 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003649 continue;
3650 }
3651 finish_wait(&ctx->sqo_wait, &wait);
3652
Hristo Venev75b28af2019-08-26 17:23:46 +00003653 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003654 }
3655
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003656 to_submit = min(to_submit, ctx->sq_entries);
Jens Axboe8a4955f2019-12-09 14:52:35 -07003657 mutex_lock(&ctx->uring_lock);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003658 ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true);
Jens Axboe8a4955f2019-12-09 14:52:35 -07003659 mutex_unlock(&ctx->uring_lock);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003660 if (ret > 0)
3661 inflight += ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003662 }
3663
3664 set_fs(old_fs);
3665 if (cur_mm) {
3666 unuse_mm(cur_mm);
3667 mmput(cur_mm);
3668 }
Jens Axboe181e4482019-11-25 08:52:30 -07003669 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06003670
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003671 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06003672
Jens Axboe6c271ce2019-01-10 11:22:30 -07003673 return 0;
3674}
3675
Jens Axboebda52162019-09-24 13:47:15 -06003676struct io_wait_queue {
3677 struct wait_queue_entry wq;
3678 struct io_ring_ctx *ctx;
3679 unsigned to_wait;
3680 unsigned nr_timeouts;
3681};
3682
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003683static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06003684{
3685 struct io_ring_ctx *ctx = iowq->ctx;
3686
3687 /*
3688 * Wake up if we have enough events, or if a timeout occured since we
3689 * started waiting. For timeouts, we always want to return to userspace,
3690 * regardless of event count.
3691 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003692 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06003693 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
3694}
3695
3696static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
3697 int wake_flags, void *key)
3698{
3699 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
3700 wq);
3701
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003702 /* use noflush == true, as we can't safely rely on locking context */
3703 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06003704 return -1;
3705
3706 return autoremove_wake_function(curr, mode, wake_flags, key);
3707}
3708
Jens Axboe2b188cc2019-01-07 10:46:33 -07003709/*
3710 * Wait until events become available, if we don't already have some. The
3711 * application must reap them itself, as they reside on the shared cq ring.
3712 */
3713static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
3714 const sigset_t __user *sig, size_t sigsz)
3715{
Jens Axboebda52162019-09-24 13:47:15 -06003716 struct io_wait_queue iowq = {
3717 .wq = {
3718 .private = current,
3719 .func = io_wake_function,
3720 .entry = LIST_HEAD_INIT(iowq.wq.entry),
3721 },
3722 .ctx = ctx,
3723 .to_wait = min_events,
3724 };
Hristo Venev75b28af2019-08-26 17:23:46 +00003725 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08003726 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003727
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003728 if (io_cqring_events(ctx, false) >= min_events)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003729 return 0;
3730
3731 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01003732#ifdef CONFIG_COMPAT
3733 if (in_compat_syscall())
3734 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07003735 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01003736 else
3737#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07003738 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01003739
Jens Axboe2b188cc2019-01-07 10:46:33 -07003740 if (ret)
3741 return ret;
3742 }
3743
Jens Axboebda52162019-09-24 13:47:15 -06003744 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02003745 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06003746 do {
3747 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
3748 TASK_INTERRUPTIBLE);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003749 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06003750 break;
3751 schedule();
3752 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08003753 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06003754 break;
3755 }
3756 } while (1);
3757 finish_wait(&ctx->wait, &iowq.wq);
3758
Jackie Liue9ffa5c2019-10-29 11:16:42 +08003759 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003760
Hristo Venev75b28af2019-08-26 17:23:46 +00003761 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003762}
3763
Jens Axboe6b063142019-01-10 22:13:58 -07003764static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
3765{
3766#if defined(CONFIG_UNIX)
3767 if (ctx->ring_sock) {
3768 struct sock *sock = ctx->ring_sock->sk;
3769 struct sk_buff *skb;
3770
3771 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
3772 kfree_skb(skb);
3773 }
3774#else
3775 int i;
3776
Jens Axboe65e19f52019-10-26 07:20:21 -06003777 for (i = 0; i < ctx->nr_user_files; i++) {
3778 struct file *file;
3779
3780 file = io_file_from_index(ctx, i);
3781 if (file)
3782 fput(file);
3783 }
Jens Axboe6b063142019-01-10 22:13:58 -07003784#endif
3785}
3786
3787static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
3788{
Jens Axboe65e19f52019-10-26 07:20:21 -06003789 unsigned nr_tables, i;
3790
3791 if (!ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003792 return -ENXIO;
3793
3794 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06003795 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
3796 for (i = 0; i < nr_tables; i++)
3797 kfree(ctx->file_table[i].files);
3798 kfree(ctx->file_table);
3799 ctx->file_table = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003800 ctx->nr_user_files = 0;
3801 return 0;
3802}
3803
Jens Axboe6c271ce2019-01-10 11:22:30 -07003804static void io_sq_thread_stop(struct io_ring_ctx *ctx)
3805{
3806 if (ctx->sqo_thread) {
Jens Axboe206aefd2019-11-07 18:27:42 -07003807 wait_for_completion(&ctx->completions[1]);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003808 /*
3809 * The park is a bit of a work-around, without it we get
3810 * warning spews on shutdown with SQPOLL set and affinity
3811 * set to a single CPU.
3812 */
Jens Axboe06058632019-04-13 09:26:03 -06003813 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003814 kthread_stop(ctx->sqo_thread);
3815 ctx->sqo_thread = NULL;
3816 }
3817}
3818
Jens Axboe6b063142019-01-10 22:13:58 -07003819static void io_finish_async(struct io_ring_ctx *ctx)
3820{
Jens Axboe6c271ce2019-01-10 11:22:30 -07003821 io_sq_thread_stop(ctx);
3822
Jens Axboe561fb042019-10-24 07:25:42 -06003823 if (ctx->io_wq) {
3824 io_wq_destroy(ctx->io_wq);
3825 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003826 }
3827}
3828
3829#if defined(CONFIG_UNIX)
3830static void io_destruct_skb(struct sk_buff *skb)
3831{
3832 struct io_ring_ctx *ctx = skb->sk->sk_user_data;
3833
Jens Axboe561fb042019-10-24 07:25:42 -06003834 if (ctx->io_wq)
3835 io_wq_flush(ctx->io_wq);
Jens Axboe8a997342019-10-09 14:40:13 -06003836
Jens Axboe6b063142019-01-10 22:13:58 -07003837 unix_destruct_scm(skb);
3838}
3839
3840/*
3841 * Ensure the UNIX gc is aware of our file set, so we are certain that
3842 * the io_uring can be safely unregistered on process exit, even if we have
3843 * loops in the file referencing.
3844 */
3845static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
3846{
3847 struct sock *sk = ctx->ring_sock->sk;
3848 struct scm_fp_list *fpl;
3849 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06003850 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07003851
3852 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
3853 unsigned long inflight = ctx->user->unix_inflight + nr;
3854
3855 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
3856 return -EMFILE;
3857 }
3858
3859 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
3860 if (!fpl)
3861 return -ENOMEM;
3862
3863 skb = alloc_skb(0, GFP_KERNEL);
3864 if (!skb) {
3865 kfree(fpl);
3866 return -ENOMEM;
3867 }
3868
3869 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07003870
Jens Axboe08a45172019-10-03 08:11:03 -06003871 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07003872 fpl->user = get_uid(ctx->user);
3873 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003874 struct file *file = io_file_from_index(ctx, i + offset);
3875
3876 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06003877 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06003878 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06003879 unix_inflight(fpl->user, fpl->fp[nr_files]);
3880 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07003881 }
3882
Jens Axboe08a45172019-10-03 08:11:03 -06003883 if (nr_files) {
3884 fpl->max = SCM_MAX_FD;
3885 fpl->count = nr_files;
3886 UNIXCB(skb).fp = fpl;
3887 skb->destructor = io_destruct_skb;
3888 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
3889 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07003890
Jens Axboe08a45172019-10-03 08:11:03 -06003891 for (i = 0; i < nr_files; i++)
3892 fput(fpl->fp[i]);
3893 } else {
3894 kfree_skb(skb);
3895 kfree(fpl);
3896 }
Jens Axboe6b063142019-01-10 22:13:58 -07003897
3898 return 0;
3899}
3900
3901/*
3902 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
3903 * causes regular reference counting to break down. We rely on the UNIX
3904 * garbage collection to take care of this problem for us.
3905 */
3906static int io_sqe_files_scm(struct io_ring_ctx *ctx)
3907{
3908 unsigned left, total;
3909 int ret = 0;
3910
3911 total = 0;
3912 left = ctx->nr_user_files;
3913 while (left) {
3914 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07003915
3916 ret = __io_sqe_files_scm(ctx, this_files, total);
3917 if (ret)
3918 break;
3919 left -= this_files;
3920 total += this_files;
3921 }
3922
3923 if (!ret)
3924 return 0;
3925
3926 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003927 struct file *file = io_file_from_index(ctx, total);
3928
3929 if (file)
3930 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07003931 total++;
3932 }
3933
3934 return ret;
3935}
3936#else
3937static int io_sqe_files_scm(struct io_ring_ctx *ctx)
3938{
3939 return 0;
3940}
3941#endif
3942
Jens Axboe65e19f52019-10-26 07:20:21 -06003943static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
3944 unsigned nr_files)
3945{
3946 int i;
3947
3948 for (i = 0; i < nr_tables; i++) {
3949 struct fixed_file_table *table = &ctx->file_table[i];
3950 unsigned this_files;
3951
3952 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
3953 table->files = kcalloc(this_files, sizeof(struct file *),
3954 GFP_KERNEL);
3955 if (!table->files)
3956 break;
3957 nr_files -= this_files;
3958 }
3959
3960 if (i == nr_tables)
3961 return 0;
3962
3963 for (i = 0; i < nr_tables; i++) {
3964 struct fixed_file_table *table = &ctx->file_table[i];
3965 kfree(table->files);
3966 }
3967 return 1;
3968}
3969
Jens Axboe6b063142019-01-10 22:13:58 -07003970static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
3971 unsigned nr_args)
3972{
3973 __s32 __user *fds = (__s32 __user *) arg;
Jens Axboe65e19f52019-10-26 07:20:21 -06003974 unsigned nr_tables;
Jens Axboe6b063142019-01-10 22:13:58 -07003975 int fd, ret = 0;
3976 unsigned i;
3977
Jens Axboe65e19f52019-10-26 07:20:21 -06003978 if (ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003979 return -EBUSY;
3980 if (!nr_args)
3981 return -EINVAL;
3982 if (nr_args > IORING_MAX_FIXED_FILES)
3983 return -EMFILE;
3984
Jens Axboe65e19f52019-10-26 07:20:21 -06003985 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
3986 ctx->file_table = kcalloc(nr_tables, sizeof(struct fixed_file_table),
3987 GFP_KERNEL);
3988 if (!ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003989 return -ENOMEM;
3990
Jens Axboe65e19f52019-10-26 07:20:21 -06003991 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
3992 kfree(ctx->file_table);
Jens Axboe46568e92019-11-10 08:40:53 -07003993 ctx->file_table = NULL;
Jens Axboe65e19f52019-10-26 07:20:21 -06003994 return -ENOMEM;
3995 }
3996
Jens Axboe08a45172019-10-03 08:11:03 -06003997 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003998 struct fixed_file_table *table;
3999 unsigned index;
4000
Jens Axboe6b063142019-01-10 22:13:58 -07004001 ret = -EFAULT;
4002 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
4003 break;
Jens Axboe08a45172019-10-03 08:11:03 -06004004 /* allow sparse sets */
4005 if (fd == -1) {
4006 ret = 0;
4007 continue;
4008 }
Jens Axboe6b063142019-01-10 22:13:58 -07004009
Jens Axboe65e19f52019-10-26 07:20:21 -06004010 table = &ctx->file_table[i >> IORING_FILE_TABLE_SHIFT];
4011 index = i & IORING_FILE_TABLE_MASK;
4012 table->files[index] = fget(fd);
Jens Axboe6b063142019-01-10 22:13:58 -07004013
4014 ret = -EBADF;
Jens Axboe65e19f52019-10-26 07:20:21 -06004015 if (!table->files[index])
Jens Axboe6b063142019-01-10 22:13:58 -07004016 break;
4017 /*
4018 * Don't allow io_uring instances to be registered. If UNIX
4019 * isn't enabled, then this causes a reference cycle and this
4020 * instance can never get freed. If UNIX is enabled we'll
4021 * handle it just fine, but there's still no point in allowing
4022 * a ring fd as it doesn't support regular read/write anyway.
4023 */
Jens Axboe65e19f52019-10-26 07:20:21 -06004024 if (table->files[index]->f_op == &io_uring_fops) {
4025 fput(table->files[index]);
Jens Axboe6b063142019-01-10 22:13:58 -07004026 break;
4027 }
Jens Axboe6b063142019-01-10 22:13:58 -07004028 ret = 0;
4029 }
4030
4031 if (ret) {
Jens Axboe65e19f52019-10-26 07:20:21 -06004032 for (i = 0; i < ctx->nr_user_files; i++) {
4033 struct file *file;
Jens Axboe6b063142019-01-10 22:13:58 -07004034
Jens Axboe65e19f52019-10-26 07:20:21 -06004035 file = io_file_from_index(ctx, i);
4036 if (file)
4037 fput(file);
4038 }
4039 for (i = 0; i < nr_tables; i++)
4040 kfree(ctx->file_table[i].files);
4041
4042 kfree(ctx->file_table);
4043 ctx->file_table = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07004044 ctx->nr_user_files = 0;
4045 return ret;
4046 }
4047
4048 ret = io_sqe_files_scm(ctx);
4049 if (ret)
4050 io_sqe_files_unregister(ctx);
4051
4052 return ret;
4053}
4054
Jens Axboec3a31e62019-10-03 13:59:56 -06004055static void io_sqe_file_unregister(struct io_ring_ctx *ctx, int index)
4056{
4057#if defined(CONFIG_UNIX)
Jens Axboe65e19f52019-10-26 07:20:21 -06004058 struct file *file = io_file_from_index(ctx, index);
Jens Axboec3a31e62019-10-03 13:59:56 -06004059 struct sock *sock = ctx->ring_sock->sk;
4060 struct sk_buff_head list, *head = &sock->sk_receive_queue;
4061 struct sk_buff *skb;
4062 int i;
4063
4064 __skb_queue_head_init(&list);
4065
4066 /*
4067 * Find the skb that holds this file in its SCM_RIGHTS. When found,
4068 * remove this entry and rearrange the file array.
4069 */
4070 skb = skb_dequeue(head);
4071 while (skb) {
4072 struct scm_fp_list *fp;
4073
4074 fp = UNIXCB(skb).fp;
4075 for (i = 0; i < fp->count; i++) {
4076 int left;
4077
4078 if (fp->fp[i] != file)
4079 continue;
4080
4081 unix_notinflight(fp->user, fp->fp[i]);
4082 left = fp->count - 1 - i;
4083 if (left) {
4084 memmove(&fp->fp[i], &fp->fp[i + 1],
4085 left * sizeof(struct file *));
4086 }
4087 fp->count--;
4088 if (!fp->count) {
4089 kfree_skb(skb);
4090 skb = NULL;
4091 } else {
4092 __skb_queue_tail(&list, skb);
4093 }
4094 fput(file);
4095 file = NULL;
4096 break;
4097 }
4098
4099 if (!file)
4100 break;
4101
4102 __skb_queue_tail(&list, skb);
4103
4104 skb = skb_dequeue(head);
4105 }
4106
4107 if (skb_peek(&list)) {
4108 spin_lock_irq(&head->lock);
4109 while ((skb = __skb_dequeue(&list)) != NULL)
4110 __skb_queue_tail(head, skb);
4111 spin_unlock_irq(&head->lock);
4112 }
4113#else
Jens Axboe65e19f52019-10-26 07:20:21 -06004114 fput(io_file_from_index(ctx, index));
Jens Axboec3a31e62019-10-03 13:59:56 -06004115#endif
4116}
4117
4118static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
4119 int index)
4120{
4121#if defined(CONFIG_UNIX)
4122 struct sock *sock = ctx->ring_sock->sk;
4123 struct sk_buff_head *head = &sock->sk_receive_queue;
4124 struct sk_buff *skb;
4125
4126 /*
4127 * See if we can merge this file into an existing skb SCM_RIGHTS
4128 * file set. If there's no room, fall back to allocating a new skb
4129 * and filling it in.
4130 */
4131 spin_lock_irq(&head->lock);
4132 skb = skb_peek(head);
4133 if (skb) {
4134 struct scm_fp_list *fpl = UNIXCB(skb).fp;
4135
4136 if (fpl->count < SCM_MAX_FD) {
4137 __skb_unlink(skb, head);
4138 spin_unlock_irq(&head->lock);
4139 fpl->fp[fpl->count] = get_file(file);
4140 unix_inflight(fpl->user, fpl->fp[fpl->count]);
4141 fpl->count++;
4142 spin_lock_irq(&head->lock);
4143 __skb_queue_head(head, skb);
4144 } else {
4145 skb = NULL;
4146 }
4147 }
4148 spin_unlock_irq(&head->lock);
4149
4150 if (skb) {
4151 fput(file);
4152 return 0;
4153 }
4154
4155 return __io_sqe_files_scm(ctx, 1, index);
4156#else
4157 return 0;
4158#endif
4159}
4160
4161static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
4162 unsigned nr_args)
4163{
4164 struct io_uring_files_update up;
4165 __s32 __user *fds;
4166 int fd, i, err;
4167 __u32 done;
4168
Jens Axboe65e19f52019-10-26 07:20:21 -06004169 if (!ctx->file_table)
Jens Axboec3a31e62019-10-03 13:59:56 -06004170 return -ENXIO;
4171 if (!nr_args)
4172 return -EINVAL;
4173 if (copy_from_user(&up, arg, sizeof(up)))
4174 return -EFAULT;
4175 if (check_add_overflow(up.offset, nr_args, &done))
4176 return -EOVERFLOW;
4177 if (done > ctx->nr_user_files)
4178 return -EINVAL;
4179
4180 done = 0;
4181 fds = (__s32 __user *) up.fds;
4182 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06004183 struct fixed_file_table *table;
4184 unsigned index;
4185
Jens Axboec3a31e62019-10-03 13:59:56 -06004186 err = 0;
4187 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
4188 err = -EFAULT;
4189 break;
4190 }
4191 i = array_index_nospec(up.offset, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06004192 table = &ctx->file_table[i >> IORING_FILE_TABLE_SHIFT];
4193 index = i & IORING_FILE_TABLE_MASK;
4194 if (table->files[index]) {
Jens Axboec3a31e62019-10-03 13:59:56 -06004195 io_sqe_file_unregister(ctx, i);
Jens Axboe65e19f52019-10-26 07:20:21 -06004196 table->files[index] = NULL;
Jens Axboec3a31e62019-10-03 13:59:56 -06004197 }
4198 if (fd != -1) {
4199 struct file *file;
4200
4201 file = fget(fd);
4202 if (!file) {
4203 err = -EBADF;
4204 break;
4205 }
4206 /*
4207 * Don't allow io_uring instances to be registered. If
4208 * UNIX isn't enabled, then this causes a reference
4209 * cycle and this instance can never get freed. If UNIX
4210 * is enabled we'll handle it just fine, but there's
4211 * still no point in allowing a ring fd as it doesn't
4212 * support regular read/write anyway.
4213 */
4214 if (file->f_op == &io_uring_fops) {
4215 fput(file);
4216 err = -EBADF;
4217 break;
4218 }
Jens Axboe65e19f52019-10-26 07:20:21 -06004219 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06004220 err = io_sqe_file_register(ctx, file, i);
4221 if (err)
4222 break;
4223 }
4224 nr_args--;
4225 done++;
4226 up.offset++;
4227 }
4228
4229 return done ? done : err;
4230}
4231
Jens Axboe7d723062019-11-12 22:31:31 -07004232static void io_put_work(struct io_wq_work *work)
4233{
4234 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
4235
4236 io_put_req(req);
4237}
4238
4239static void io_get_work(struct io_wq_work *work)
4240{
4241 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
4242
4243 refcount_inc(&req->refs);
4244}
4245
Jens Axboe6c271ce2019-01-10 11:22:30 -07004246static int io_sq_offload_start(struct io_ring_ctx *ctx,
4247 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004248{
Jens Axboe576a3472019-11-25 08:49:20 -07004249 struct io_wq_data data;
Jens Axboe561fb042019-10-24 07:25:42 -06004250 unsigned concurrency;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004251 int ret;
4252
Jens Axboe6c271ce2019-01-10 11:22:30 -07004253 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004254 mmgrab(current->mm);
4255 ctx->sqo_mm = current->mm;
4256
Jens Axboe6c271ce2019-01-10 11:22:30 -07004257 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06004258 ret = -EPERM;
4259 if (!capable(CAP_SYS_ADMIN))
4260 goto err;
4261
Jens Axboe917257d2019-04-13 09:28:55 -06004262 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
4263 if (!ctx->sq_thread_idle)
4264 ctx->sq_thread_idle = HZ;
4265
Jens Axboe6c271ce2019-01-10 11:22:30 -07004266 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06004267 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004268
Jens Axboe917257d2019-04-13 09:28:55 -06004269 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06004270 if (cpu >= nr_cpu_ids)
4271 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08004272 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06004273 goto err;
4274
Jens Axboe6c271ce2019-01-10 11:22:30 -07004275 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
4276 ctx, cpu,
4277 "io_uring-sq");
4278 } else {
4279 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
4280 "io_uring-sq");
4281 }
4282 if (IS_ERR(ctx->sqo_thread)) {
4283 ret = PTR_ERR(ctx->sqo_thread);
4284 ctx->sqo_thread = NULL;
4285 goto err;
4286 }
4287 wake_up_process(ctx->sqo_thread);
4288 } else if (p->flags & IORING_SETUP_SQ_AFF) {
4289 /* Can't have SQ_AFF without SQPOLL */
4290 ret = -EINVAL;
4291 goto err;
4292 }
4293
Jens Axboe576a3472019-11-25 08:49:20 -07004294 data.mm = ctx->sqo_mm;
4295 data.user = ctx->user;
Jens Axboe181e4482019-11-25 08:52:30 -07004296 data.creds = ctx->creds;
Jens Axboe576a3472019-11-25 08:49:20 -07004297 data.get_work = io_get_work;
4298 data.put_work = io_put_work;
4299
Jens Axboe561fb042019-10-24 07:25:42 -06004300 /* Do QD, or 4 * CPUS, whatever is smallest */
4301 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
Jens Axboe576a3472019-11-25 08:49:20 -07004302 ctx->io_wq = io_wq_create(concurrency, &data);
Jens Axboe975c99a52019-10-30 08:42:56 -06004303 if (IS_ERR(ctx->io_wq)) {
4304 ret = PTR_ERR(ctx->io_wq);
4305 ctx->io_wq = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004306 goto err;
4307 }
4308
4309 return 0;
4310err:
Jens Axboe54a91f32019-09-10 09:15:04 -06004311 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004312 mmdrop(ctx->sqo_mm);
4313 ctx->sqo_mm = NULL;
4314 return ret;
4315}
4316
4317static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
4318{
4319 atomic_long_sub(nr_pages, &user->locked_vm);
4320}
4321
4322static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
4323{
4324 unsigned long page_limit, cur_pages, new_pages;
4325
4326 /* Don't allow more pages than we can safely lock */
4327 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
4328
4329 do {
4330 cur_pages = atomic_long_read(&user->locked_vm);
4331 new_pages = cur_pages + nr_pages;
4332 if (new_pages > page_limit)
4333 return -ENOMEM;
4334 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
4335 new_pages) != cur_pages);
4336
4337 return 0;
4338}
4339
4340static void io_mem_free(void *ptr)
4341{
Mark Rutland52e04ef2019-04-30 17:30:21 +01004342 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004343
Mark Rutland52e04ef2019-04-30 17:30:21 +01004344 if (!ptr)
4345 return;
4346
4347 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004348 if (put_page_testzero(page))
4349 free_compound_page(page);
4350}
4351
4352static void *io_mem_alloc(size_t size)
4353{
4354 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
4355 __GFP_NORETRY;
4356
4357 return (void *) __get_free_pages(gfp_flags, get_order(size));
4358}
4359
Hristo Venev75b28af2019-08-26 17:23:46 +00004360static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
4361 size_t *sq_offset)
4362{
4363 struct io_rings *rings;
4364 size_t off, sq_array_size;
4365
4366 off = struct_size(rings, cqes, cq_entries);
4367 if (off == SIZE_MAX)
4368 return SIZE_MAX;
4369
4370#ifdef CONFIG_SMP
4371 off = ALIGN(off, SMP_CACHE_BYTES);
4372 if (off == 0)
4373 return SIZE_MAX;
4374#endif
4375
4376 sq_array_size = array_size(sizeof(u32), sq_entries);
4377 if (sq_array_size == SIZE_MAX)
4378 return SIZE_MAX;
4379
4380 if (check_add_overflow(off, sq_array_size, &off))
4381 return SIZE_MAX;
4382
4383 if (sq_offset)
4384 *sq_offset = off;
4385
4386 return off;
4387}
4388
Jens Axboe2b188cc2019-01-07 10:46:33 -07004389static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
4390{
Hristo Venev75b28af2019-08-26 17:23:46 +00004391 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004392
Hristo Venev75b28af2019-08-26 17:23:46 +00004393 pages = (size_t)1 << get_order(
4394 rings_size(sq_entries, cq_entries, NULL));
4395 pages += (size_t)1 << get_order(
4396 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07004397
Hristo Venev75b28af2019-08-26 17:23:46 +00004398 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004399}
4400
Jens Axboeedafcce2019-01-09 09:16:05 -07004401static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
4402{
4403 int i, j;
4404
4405 if (!ctx->user_bufs)
4406 return -ENXIO;
4407
4408 for (i = 0; i < ctx->nr_user_bufs; i++) {
4409 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
4410
4411 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbard27c4d3a2019-08-04 19:32:06 -07004412 put_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07004413
4414 if (ctx->account_mem)
4415 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004416 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07004417 imu->nr_bvecs = 0;
4418 }
4419
4420 kfree(ctx->user_bufs);
4421 ctx->user_bufs = NULL;
4422 ctx->nr_user_bufs = 0;
4423 return 0;
4424}
4425
4426static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
4427 void __user *arg, unsigned index)
4428{
4429 struct iovec __user *src;
4430
4431#ifdef CONFIG_COMPAT
4432 if (ctx->compat) {
4433 struct compat_iovec __user *ciovs;
4434 struct compat_iovec ciov;
4435
4436 ciovs = (struct compat_iovec __user *) arg;
4437 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
4438 return -EFAULT;
4439
4440 dst->iov_base = (void __user *) (unsigned long) ciov.iov_base;
4441 dst->iov_len = ciov.iov_len;
4442 return 0;
4443 }
4444#endif
4445 src = (struct iovec __user *) arg;
4446 if (copy_from_user(dst, &src[index], sizeof(*dst)))
4447 return -EFAULT;
4448 return 0;
4449}
4450
4451static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
4452 unsigned nr_args)
4453{
4454 struct vm_area_struct **vmas = NULL;
4455 struct page **pages = NULL;
4456 int i, j, got_pages = 0;
4457 int ret = -EINVAL;
4458
4459 if (ctx->user_bufs)
4460 return -EBUSY;
4461 if (!nr_args || nr_args > UIO_MAXIOV)
4462 return -EINVAL;
4463
4464 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
4465 GFP_KERNEL);
4466 if (!ctx->user_bufs)
4467 return -ENOMEM;
4468
4469 for (i = 0; i < nr_args; i++) {
4470 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
4471 unsigned long off, start, end, ubuf;
4472 int pret, nr_pages;
4473 struct iovec iov;
4474 size_t size;
4475
4476 ret = io_copy_iov(ctx, &iov, arg, i);
4477 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03004478 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07004479
4480 /*
4481 * Don't impose further limits on the size and buffer
4482 * constraints here, we'll -EINVAL later when IO is
4483 * submitted if they are wrong.
4484 */
4485 ret = -EFAULT;
4486 if (!iov.iov_base || !iov.iov_len)
4487 goto err;
4488
4489 /* arbitrary limit, but we need something */
4490 if (iov.iov_len > SZ_1G)
4491 goto err;
4492
4493 ubuf = (unsigned long) iov.iov_base;
4494 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
4495 start = ubuf >> PAGE_SHIFT;
4496 nr_pages = end - start;
4497
4498 if (ctx->account_mem) {
4499 ret = io_account_mem(ctx->user, nr_pages);
4500 if (ret)
4501 goto err;
4502 }
4503
4504 ret = 0;
4505 if (!pages || nr_pages > got_pages) {
4506 kfree(vmas);
4507 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004508 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07004509 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004510 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07004511 sizeof(struct vm_area_struct *),
4512 GFP_KERNEL);
4513 if (!pages || !vmas) {
4514 ret = -ENOMEM;
4515 if (ctx->account_mem)
4516 io_unaccount_mem(ctx->user, nr_pages);
4517 goto err;
4518 }
4519 got_pages = nr_pages;
4520 }
4521
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004522 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07004523 GFP_KERNEL);
4524 ret = -ENOMEM;
4525 if (!imu->bvec) {
4526 if (ctx->account_mem)
4527 io_unaccount_mem(ctx->user, nr_pages);
4528 goto err;
4529 }
4530
4531 ret = 0;
4532 down_read(&current->mm->mmap_sem);
Ira Weiny932f4a62019-05-13 17:17:03 -07004533 pret = get_user_pages(ubuf, nr_pages,
4534 FOLL_WRITE | FOLL_LONGTERM,
4535 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07004536 if (pret == nr_pages) {
4537 /* don't support file backed memory */
4538 for (j = 0; j < nr_pages; j++) {
4539 struct vm_area_struct *vma = vmas[j];
4540
4541 if (vma->vm_file &&
4542 !is_file_hugepages(vma->vm_file)) {
4543 ret = -EOPNOTSUPP;
4544 break;
4545 }
4546 }
4547 } else {
4548 ret = pret < 0 ? pret : -EFAULT;
4549 }
4550 up_read(&current->mm->mmap_sem);
4551 if (ret) {
4552 /*
4553 * if we did partial map, or found file backed vmas,
4554 * release any pages we did get
4555 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07004556 if (pret > 0)
4557 put_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07004558 if (ctx->account_mem)
4559 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004560 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07004561 goto err;
4562 }
4563
4564 off = ubuf & ~PAGE_MASK;
4565 size = iov.iov_len;
4566 for (j = 0; j < nr_pages; j++) {
4567 size_t vec_len;
4568
4569 vec_len = min_t(size_t, size, PAGE_SIZE - off);
4570 imu->bvec[j].bv_page = pages[j];
4571 imu->bvec[j].bv_len = vec_len;
4572 imu->bvec[j].bv_offset = off;
4573 off = 0;
4574 size -= vec_len;
4575 }
4576 /* store original address for later verification */
4577 imu->ubuf = ubuf;
4578 imu->len = iov.iov_len;
4579 imu->nr_bvecs = nr_pages;
4580
4581 ctx->nr_user_bufs++;
4582 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004583 kvfree(pages);
4584 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07004585 return 0;
4586err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004587 kvfree(pages);
4588 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07004589 io_sqe_buffer_unregister(ctx);
4590 return ret;
4591}
4592
Jens Axboe9b402842019-04-11 11:45:41 -06004593static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
4594{
4595 __s32 __user *fds = arg;
4596 int fd;
4597
4598 if (ctx->cq_ev_fd)
4599 return -EBUSY;
4600
4601 if (copy_from_user(&fd, fds, sizeof(*fds)))
4602 return -EFAULT;
4603
4604 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
4605 if (IS_ERR(ctx->cq_ev_fd)) {
4606 int ret = PTR_ERR(ctx->cq_ev_fd);
4607 ctx->cq_ev_fd = NULL;
4608 return ret;
4609 }
4610
4611 return 0;
4612}
4613
4614static int io_eventfd_unregister(struct io_ring_ctx *ctx)
4615{
4616 if (ctx->cq_ev_fd) {
4617 eventfd_ctx_put(ctx->cq_ev_fd);
4618 ctx->cq_ev_fd = NULL;
4619 return 0;
4620 }
4621
4622 return -ENXIO;
4623}
4624
Jens Axboe2b188cc2019-01-07 10:46:33 -07004625static void io_ring_ctx_free(struct io_ring_ctx *ctx)
4626{
Jens Axboe6b063142019-01-10 22:13:58 -07004627 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004628 if (ctx->sqo_mm)
4629 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07004630
4631 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07004632 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07004633 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06004634 io_eventfd_unregister(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07004635
Jens Axboe2b188cc2019-01-07 10:46:33 -07004636#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07004637 if (ctx->ring_sock) {
4638 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004639 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07004640 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004641#endif
4642
Hristo Venev75b28af2019-08-26 17:23:46 +00004643 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004644 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004645
4646 percpu_ref_exit(&ctx->refs);
4647 if (ctx->account_mem)
4648 io_unaccount_mem(ctx->user,
4649 ring_pages(ctx->sq_entries, ctx->cq_entries));
4650 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07004651 put_cred(ctx->creds);
Jens Axboe206aefd2019-11-07 18:27:42 -07004652 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -07004653 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07004654 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004655 kfree(ctx);
4656}
4657
4658static __poll_t io_uring_poll(struct file *file, poll_table *wait)
4659{
4660 struct io_ring_ctx *ctx = file->private_data;
4661 __poll_t mask = 0;
4662
4663 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02004664 /*
4665 * synchronizes with barrier from wq_has_sleeper call in
4666 * io_commit_cqring
4667 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004668 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00004669 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
4670 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004671 mask |= EPOLLOUT | EPOLLWRNORM;
yangerkundaa5de52019-09-24 20:53:34 +08004672 if (READ_ONCE(ctx->rings->cq.head) != ctx->cached_cq_tail)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004673 mask |= EPOLLIN | EPOLLRDNORM;
4674
4675 return mask;
4676}
4677
4678static int io_uring_fasync(int fd, struct file *file, int on)
4679{
4680 struct io_ring_ctx *ctx = file->private_data;
4681
4682 return fasync_helper(fd, file, on, &ctx->cq_fasync);
4683}
4684
4685static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
4686{
4687 mutex_lock(&ctx->uring_lock);
4688 percpu_ref_kill(&ctx->refs);
4689 mutex_unlock(&ctx->uring_lock);
4690
Jens Axboe5262f562019-09-17 12:26:57 -06004691 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004692 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06004693
4694 if (ctx->io_wq)
4695 io_wq_cancel_all(ctx->io_wq);
4696
Jens Axboedef596e2019-01-09 08:59:42 -07004697 io_iopoll_reap_events(ctx);
Jens Axboe15dff282019-11-13 09:09:23 -07004698 /* if we failed setting up the ctx, we might not have any rings */
4699 if (ctx->rings)
4700 io_cqring_overflow_flush(ctx, true);
Jens Axboe206aefd2019-11-07 18:27:42 -07004701 wait_for_completion(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004702 io_ring_ctx_free(ctx);
4703}
4704
4705static int io_uring_release(struct inode *inode, struct file *file)
4706{
4707 struct io_ring_ctx *ctx = file->private_data;
4708
4709 file->private_data = NULL;
4710 io_ring_ctx_wait_and_kill(ctx);
4711 return 0;
4712}
4713
Jens Axboefcb323c2019-10-24 12:39:47 -06004714static void io_uring_cancel_files(struct io_ring_ctx *ctx,
4715 struct files_struct *files)
4716{
4717 struct io_kiocb *req;
4718 DEFINE_WAIT(wait);
4719
4720 while (!list_empty_careful(&ctx->inflight_list)) {
Jens Axboe768134d2019-11-10 20:30:53 -07004721 struct io_kiocb *cancel_req = NULL;
Jens Axboefcb323c2019-10-24 12:39:47 -06004722
4723 spin_lock_irq(&ctx->inflight_lock);
4724 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07004725 if (req->work.files != files)
4726 continue;
4727 /* req is being completed, ignore */
4728 if (!refcount_inc_not_zero(&req->refs))
4729 continue;
4730 cancel_req = req;
4731 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06004732 }
Jens Axboe768134d2019-11-10 20:30:53 -07004733 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06004734 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07004735 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06004736 spin_unlock_irq(&ctx->inflight_lock);
4737
Jens Axboe768134d2019-11-10 20:30:53 -07004738 /* We need to keep going until we don't find a matching req */
4739 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06004740 break;
Bob Liu2f6d9b92019-11-13 18:06:24 +08004741
4742 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
4743 io_put_req(cancel_req);
Jens Axboefcb323c2019-10-24 12:39:47 -06004744 schedule();
4745 }
Jens Axboe768134d2019-11-10 20:30:53 -07004746 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06004747}
4748
4749static int io_uring_flush(struct file *file, void *data)
4750{
4751 struct io_ring_ctx *ctx = file->private_data;
4752
4753 io_uring_cancel_files(ctx, data);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004754 if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) {
4755 io_cqring_overflow_flush(ctx, true);
Jens Axboefcb323c2019-10-24 12:39:47 -06004756 io_wq_cancel_all(ctx->io_wq);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004757 }
Jens Axboefcb323c2019-10-24 12:39:47 -06004758 return 0;
4759}
4760
Roman Penyaev6c5c2402019-11-28 12:53:22 +01004761static void *io_uring_validate_mmap_request(struct file *file,
4762 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004763{
Jens Axboe2b188cc2019-01-07 10:46:33 -07004764 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01004765 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004766 struct page *page;
4767 void *ptr;
4768
4769 switch (offset) {
4770 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00004771 case IORING_OFF_CQ_RING:
4772 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004773 break;
4774 case IORING_OFF_SQES:
4775 ptr = ctx->sq_sqes;
4776 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004777 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01004778 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004779 }
4780
4781 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07004782 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01004783 return ERR_PTR(-EINVAL);
4784
4785 return ptr;
4786}
4787
4788#ifdef CONFIG_MMU
4789
4790static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
4791{
4792 size_t sz = vma->vm_end - vma->vm_start;
4793 unsigned long pfn;
4794 void *ptr;
4795
4796 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
4797 if (IS_ERR(ptr))
4798 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004799
4800 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
4801 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
4802}
4803
Roman Penyaev6c5c2402019-11-28 12:53:22 +01004804#else /* !CONFIG_MMU */
4805
4806static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
4807{
4808 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
4809}
4810
4811static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
4812{
4813 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
4814}
4815
4816static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
4817 unsigned long addr, unsigned long len,
4818 unsigned long pgoff, unsigned long flags)
4819{
4820 void *ptr;
4821
4822 ptr = io_uring_validate_mmap_request(file, pgoff, len);
4823 if (IS_ERR(ptr))
4824 return PTR_ERR(ptr);
4825
4826 return (unsigned long) ptr;
4827}
4828
4829#endif /* !CONFIG_MMU */
4830
Jens Axboe2b188cc2019-01-07 10:46:33 -07004831SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
4832 u32, min_complete, u32, flags, const sigset_t __user *, sig,
4833 size_t, sigsz)
4834{
4835 struct io_ring_ctx *ctx;
4836 long ret = -EBADF;
4837 int submitted = 0;
4838 struct fd f;
4839
Jens Axboe6c271ce2019-01-10 11:22:30 -07004840 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07004841 return -EINVAL;
4842
4843 f = fdget(fd);
4844 if (!f.file)
4845 return -EBADF;
4846
4847 ret = -EOPNOTSUPP;
4848 if (f.file->f_op != &io_uring_fops)
4849 goto out_fput;
4850
4851 ret = -ENXIO;
4852 ctx = f.file->private_data;
4853 if (!percpu_ref_tryget(&ctx->refs))
4854 goto out_fput;
4855
Jens Axboe6c271ce2019-01-10 11:22:30 -07004856 /*
4857 * For SQ polling, the thread will do all submissions and completions.
4858 * Just return the requested submit count, and wake the thread if
4859 * we were asked to.
4860 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06004861 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004862 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07004863 if (!list_empty_careful(&ctx->cq_overflow_list))
4864 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004865 if (flags & IORING_ENTER_SQ_WAKEUP)
4866 wake_up(&ctx->sqo_wait);
4867 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06004868 } else if (to_submit) {
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004869 struct mm_struct *cur_mm;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004870
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004871 to_submit = min(to_submit, ctx->sq_entries);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004872 mutex_lock(&ctx->uring_lock);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004873 /* already have mm, so io_submit_sqes() won't try to grab it */
4874 cur_mm = ctx->sqo_mm;
4875 submitted = io_submit_sqes(ctx, to_submit, f.file, fd,
4876 &cur_mm, false);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004877 mutex_unlock(&ctx->uring_lock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004878 }
4879 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07004880 unsigned nr_events = 0;
4881
Jens Axboe2b188cc2019-01-07 10:46:33 -07004882 min_complete = min(min_complete, ctx->cq_entries);
4883
Jens Axboedef596e2019-01-09 08:59:42 -07004884 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07004885 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07004886 } else {
4887 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
4888 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004889 }
4890
Pavel Begunkov6805b322019-10-08 02:18:42 +03004891 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004892out_fput:
4893 fdput(f);
4894 return submitted ? submitted : ret;
4895}
4896
4897static const struct file_operations io_uring_fops = {
4898 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06004899 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07004900 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01004901#ifndef CONFIG_MMU
4902 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
4903 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
4904#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07004905 .poll = io_uring_poll,
4906 .fasync = io_uring_fasync,
4907};
4908
4909static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
4910 struct io_uring_params *p)
4911{
Hristo Venev75b28af2019-08-26 17:23:46 +00004912 struct io_rings *rings;
4913 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004914
Hristo Venev75b28af2019-08-26 17:23:46 +00004915 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
4916 if (size == SIZE_MAX)
4917 return -EOVERFLOW;
4918
4919 rings = io_mem_alloc(size);
4920 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004921 return -ENOMEM;
4922
Hristo Venev75b28af2019-08-26 17:23:46 +00004923 ctx->rings = rings;
4924 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
4925 rings->sq_ring_mask = p->sq_entries - 1;
4926 rings->cq_ring_mask = p->cq_entries - 1;
4927 rings->sq_ring_entries = p->sq_entries;
4928 rings->cq_ring_entries = p->cq_entries;
4929 ctx->sq_mask = rings->sq_ring_mask;
4930 ctx->cq_mask = rings->cq_ring_mask;
4931 ctx->sq_entries = rings->sq_ring_entries;
4932 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004933
4934 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07004935 if (size == SIZE_MAX) {
4936 io_mem_free(ctx->rings);
4937 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004938 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07004939 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004940
4941 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07004942 if (!ctx->sq_sqes) {
4943 io_mem_free(ctx->rings);
4944 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004945 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07004946 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004947
Jens Axboe2b188cc2019-01-07 10:46:33 -07004948 return 0;
4949}
4950
4951/*
4952 * Allocate an anonymous fd, this is what constitutes the application
4953 * visible backing of an io_uring instance. The application mmaps this
4954 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
4955 * we have to tie this fd to a socket for file garbage collection purposes.
4956 */
4957static int io_uring_get_fd(struct io_ring_ctx *ctx)
4958{
4959 struct file *file;
4960 int ret;
4961
4962#if defined(CONFIG_UNIX)
4963 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
4964 &ctx->ring_sock);
4965 if (ret)
4966 return ret;
4967#endif
4968
4969 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
4970 if (ret < 0)
4971 goto err;
4972
4973 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
4974 O_RDWR | O_CLOEXEC);
4975 if (IS_ERR(file)) {
4976 put_unused_fd(ret);
4977 ret = PTR_ERR(file);
4978 goto err;
4979 }
4980
4981#if defined(CONFIG_UNIX)
4982 ctx->ring_sock->file = file;
Jens Axboe6b063142019-01-10 22:13:58 -07004983 ctx->ring_sock->sk->sk_user_data = ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004984#endif
4985 fd_install(ret, file);
4986 return ret;
4987err:
4988#if defined(CONFIG_UNIX)
4989 sock_release(ctx->ring_sock);
4990 ctx->ring_sock = NULL;
4991#endif
4992 return ret;
4993}
4994
4995static int io_uring_create(unsigned entries, struct io_uring_params *p)
4996{
4997 struct user_struct *user = NULL;
4998 struct io_ring_ctx *ctx;
4999 bool account_mem;
5000 int ret;
5001
5002 if (!entries || entries > IORING_MAX_ENTRIES)
5003 return -EINVAL;
5004
5005 /*
5006 * Use twice as many entries for the CQ ring. It's possible for the
5007 * application to drive a higher depth than the size of the SQ ring,
5008 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06005009 * some flexibility in overcommitting a bit. If the application has
5010 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
5011 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07005012 */
5013 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06005014 if (p->flags & IORING_SETUP_CQSIZE) {
5015 /*
5016 * If IORING_SETUP_CQSIZE is set, we do the same roundup
5017 * to a power-of-two, if it isn't already. We do NOT impose
5018 * any cq vs sq ring sizing.
5019 */
5020 if (p->cq_entries < p->sq_entries || p->cq_entries > IORING_MAX_CQ_ENTRIES)
5021 return -EINVAL;
5022 p->cq_entries = roundup_pow_of_two(p->cq_entries);
5023 } else {
5024 p->cq_entries = 2 * p->sq_entries;
5025 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07005026
5027 user = get_uid(current_user());
5028 account_mem = !capable(CAP_IPC_LOCK);
5029
5030 if (account_mem) {
5031 ret = io_account_mem(user,
5032 ring_pages(p->sq_entries, p->cq_entries));
5033 if (ret) {
5034 free_uid(user);
5035 return ret;
5036 }
5037 }
5038
5039 ctx = io_ring_ctx_alloc(p);
5040 if (!ctx) {
5041 if (account_mem)
5042 io_unaccount_mem(user, ring_pages(p->sq_entries,
5043 p->cq_entries));
5044 free_uid(user);
5045 return -ENOMEM;
5046 }
5047 ctx->compat = in_compat_syscall();
5048 ctx->account_mem = account_mem;
5049 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07005050 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07005051
5052 ret = io_allocate_scq_urings(ctx, p);
5053 if (ret)
5054 goto err;
5055
Jens Axboe6c271ce2019-01-10 11:22:30 -07005056 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005057 if (ret)
5058 goto err;
5059
Jens Axboe2b188cc2019-01-07 10:46:33 -07005060 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00005061 p->sq_off.head = offsetof(struct io_rings, sq.head);
5062 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
5063 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
5064 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
5065 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
5066 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
5067 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005068
5069 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00005070 p->cq_off.head = offsetof(struct io_rings, cq.head);
5071 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
5072 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
5073 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
5074 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
5075 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06005076
Jens Axboe044c1ab2019-10-28 09:15:33 -06005077 /*
5078 * Install ring fd as the very last thing, so we don't risk someone
5079 * having closed it before we finish setup
5080 */
5081 ret = io_uring_get_fd(ctx);
5082 if (ret < 0)
5083 goto err;
5084
Jens Axboeda8c9692019-12-02 18:51:26 -07005085 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
5086 IORING_FEAT_SUBMIT_STABLE;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02005087 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005088 return ret;
5089err:
5090 io_ring_ctx_wait_and_kill(ctx);
5091 return ret;
5092}
5093
5094/*
5095 * Sets up an aio uring context, and returns the fd. Applications asks for a
5096 * ring size, we return the actual sq/cq ring sizes (among other things) in the
5097 * params structure passed in.
5098 */
5099static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
5100{
5101 struct io_uring_params p;
5102 long ret;
5103 int i;
5104
5105 if (copy_from_user(&p, params, sizeof(p)))
5106 return -EFAULT;
5107 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
5108 if (p.resv[i])
5109 return -EINVAL;
5110 }
5111
Jens Axboe6c271ce2019-01-10 11:22:30 -07005112 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe33a107f2019-10-04 12:10:03 -06005113 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE))
Jens Axboe2b188cc2019-01-07 10:46:33 -07005114 return -EINVAL;
5115
5116 ret = io_uring_create(entries, &p);
5117 if (ret < 0)
5118 return ret;
5119
5120 if (copy_to_user(params, &p, sizeof(p)))
5121 return -EFAULT;
5122
5123 return ret;
5124}
5125
5126SYSCALL_DEFINE2(io_uring_setup, u32, entries,
5127 struct io_uring_params __user *, params)
5128{
5129 return io_uring_setup(entries, params);
5130}
5131
Jens Axboeedafcce2019-01-09 09:16:05 -07005132static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
5133 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06005134 __releases(ctx->uring_lock)
5135 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07005136{
5137 int ret;
5138
Jens Axboe35fa71a2019-04-22 10:23:23 -06005139 /*
5140 * We're inside the ring mutex, if the ref is already dying, then
5141 * someone else killed the ctx or is already going through
5142 * io_uring_register().
5143 */
5144 if (percpu_ref_is_dying(&ctx->refs))
5145 return -ENXIO;
5146
Jens Axboeedafcce2019-01-09 09:16:05 -07005147 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06005148
5149 /*
5150 * Drop uring mutex before waiting for references to exit. If another
5151 * thread is currently inside io_uring_enter() it might need to grab
5152 * the uring_lock to make progress. If we hold it here across the drain
5153 * wait, then we can deadlock. It's safe to drop the mutex here, since
5154 * no new references will come in after we've killed the percpu ref.
5155 */
5156 mutex_unlock(&ctx->uring_lock);
Jens Axboe206aefd2019-11-07 18:27:42 -07005157 wait_for_completion(&ctx->completions[0]);
Jens Axboeb19062a2019-04-15 10:49:38 -06005158 mutex_lock(&ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07005159
5160 switch (opcode) {
5161 case IORING_REGISTER_BUFFERS:
5162 ret = io_sqe_buffer_register(ctx, arg, nr_args);
5163 break;
5164 case IORING_UNREGISTER_BUFFERS:
5165 ret = -EINVAL;
5166 if (arg || nr_args)
5167 break;
5168 ret = io_sqe_buffer_unregister(ctx);
5169 break;
Jens Axboe6b063142019-01-10 22:13:58 -07005170 case IORING_REGISTER_FILES:
5171 ret = io_sqe_files_register(ctx, arg, nr_args);
5172 break;
5173 case IORING_UNREGISTER_FILES:
5174 ret = -EINVAL;
5175 if (arg || nr_args)
5176 break;
5177 ret = io_sqe_files_unregister(ctx);
5178 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06005179 case IORING_REGISTER_FILES_UPDATE:
5180 ret = io_sqe_files_update(ctx, arg, nr_args);
5181 break;
Jens Axboe9b402842019-04-11 11:45:41 -06005182 case IORING_REGISTER_EVENTFD:
5183 ret = -EINVAL;
5184 if (nr_args != 1)
5185 break;
5186 ret = io_eventfd_register(ctx, arg);
5187 break;
5188 case IORING_UNREGISTER_EVENTFD:
5189 ret = -EINVAL;
5190 if (arg || nr_args)
5191 break;
5192 ret = io_eventfd_unregister(ctx);
5193 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07005194 default:
5195 ret = -EINVAL;
5196 break;
5197 }
5198
5199 /* bring the ctx back to life */
Jens Axboe206aefd2019-11-07 18:27:42 -07005200 reinit_completion(&ctx->completions[0]);
Jens Axboeedafcce2019-01-09 09:16:05 -07005201 percpu_ref_reinit(&ctx->refs);
5202 return ret;
5203}
5204
5205SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
5206 void __user *, arg, unsigned int, nr_args)
5207{
5208 struct io_ring_ctx *ctx;
5209 long ret = -EBADF;
5210 struct fd f;
5211
5212 f = fdget(fd);
5213 if (!f.file)
5214 return -EBADF;
5215
5216 ret = -EOPNOTSUPP;
5217 if (f.file->f_op != &io_uring_fops)
5218 goto out_fput;
5219
5220 ctx = f.file->private_data;
5221
5222 mutex_lock(&ctx->uring_lock);
5223 ret = __io_uring_register(ctx, opcode, arg, nr_args);
5224 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02005225 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
5226 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07005227out_fput:
5228 fdput(f);
5229 return ret;
5230}
5231
Jens Axboe2b188cc2019-01-07 10:46:33 -07005232static int __init io_uring_init(void)
5233{
5234 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
5235 return 0;
5236};
5237__initcall(io_uring_init);