blob: 02254929231b3543bfbf0dcd52a998115620706f [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 Axboe2b188cc2019-01-07 10:46:33 -070072
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020073#define CREATE_TRACE_POINTS
74#include <trace/events/io_uring.h>
75
Jens Axboe2b188cc2019-01-07 10:46:33 -070076#include <uapi/linux/io_uring.h>
77
78#include "internal.h"
Jens Axboe561fb042019-10-24 07:25:42 -060079#include "io-wq.h"
Jens Axboe2b188cc2019-01-07 10:46:33 -070080
Daniel Xu5277dea2019-09-14 14:23:45 -070081#define IORING_MAX_ENTRIES 32768
Jens Axboe33a107f2019-10-04 12:10:03 -060082#define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES)
Jens Axboe65e19f52019-10-26 07:20:21 -060083
84/*
85 * Shift of 9 is 512 entries, or exactly one page on 64-bit archs
86 */
87#define IORING_FILE_TABLE_SHIFT 9
88#define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT)
89#define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1)
90#define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE)
Jens Axboe2b188cc2019-01-07 10:46:33 -070091
92struct io_uring {
93 u32 head ____cacheline_aligned_in_smp;
94 u32 tail ____cacheline_aligned_in_smp;
95};
96
Stefan Bühler1e84b972019-04-24 23:54:16 +020097/*
Hristo Venev75b28af2019-08-26 17:23:46 +000098 * This data is shared with the application through the mmap at offsets
99 * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200100 *
101 * The offsets to the member fields are published through struct
102 * io_sqring_offsets when calling io_uring_setup.
103 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000104struct io_rings {
Stefan Bühler1e84b972019-04-24 23:54:16 +0200105 /*
106 * Head and tail offsets into the ring; the offsets need to be
107 * masked to get valid indices.
108 *
Hristo Venev75b28af2019-08-26 17:23:46 +0000109 * The kernel controls head of the sq ring and the tail of the cq ring,
110 * and the application controls tail of the sq ring and the head of the
111 * cq ring.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200112 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000113 struct io_uring sq, cq;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200114 /*
Hristo Venev75b28af2019-08-26 17:23:46 +0000115 * Bitmasks to apply to head and tail offsets (constant, equals
Stefan Bühler1e84b972019-04-24 23:54:16 +0200116 * ring_entries - 1)
117 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000118 u32 sq_ring_mask, cq_ring_mask;
119 /* Ring sizes (constant, power of 2) */
120 u32 sq_ring_entries, cq_ring_entries;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200121 /*
122 * Number of invalid entries dropped by the kernel due to
123 * invalid index stored in array
124 *
125 * Written by the kernel, shouldn't be modified by the
126 * application (i.e. get number of "new events" by comparing to
127 * cached value).
128 *
129 * After a new SQ head value was read by the application this
130 * counter includes all submissions that were dropped reaching
131 * the new SQ head (and possibly more).
132 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000133 u32 sq_dropped;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200134 /*
135 * Runtime flags
136 *
137 * Written by the kernel, shouldn't be modified by the
138 * application.
139 *
140 * The application needs a full memory barrier before checking
141 * for IORING_SQ_NEED_WAKEUP after updating the sq tail.
142 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000143 u32 sq_flags;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200144 /*
145 * Number of completion events lost because the queue was full;
146 * this should be avoided by the application by making sure
147 * there are not more requests pending thatn there is space in
148 * the completion queue.
149 *
150 * Written by the kernel, shouldn't be modified by the
151 * application (i.e. get number of "new events" by comparing to
152 * cached value).
153 *
154 * As completion events come in out of order this counter is not
155 * ordered with any other data.
156 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000157 u32 cq_overflow;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200158 /*
159 * Ring buffer of completion events.
160 *
161 * The kernel writes completion events fresh every time they are
162 * produced, so the application is allowed to modify pending
163 * entries.
164 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000165 struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700166};
167
Jens Axboeedafcce2019-01-09 09:16:05 -0700168struct io_mapped_ubuf {
169 u64 ubuf;
170 size_t len;
171 struct bio_vec *bvec;
172 unsigned int nr_bvecs;
173};
174
Jens Axboe65e19f52019-10-26 07:20:21 -0600175struct fixed_file_table {
176 struct file **files;
Jens Axboe31b51512019-01-18 22:56:34 -0700177};
178
Jens Axboe2b188cc2019-01-07 10:46:33 -0700179struct io_ring_ctx {
180 struct {
181 struct percpu_ref refs;
182 } ____cacheline_aligned_in_smp;
183
184 struct {
185 unsigned int flags;
186 bool compat;
187 bool account_mem;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700188 bool cq_overflow_flushed;
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +0300189 bool drain_next;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700190
Hristo Venev75b28af2019-08-26 17:23:46 +0000191 /*
192 * Ring buffer of indices into array of io_uring_sqe, which is
193 * mmapped by the application using the IORING_OFF_SQES offset.
194 *
195 * This indirection could e.g. be used to assign fixed
196 * io_uring_sqe entries to operations and only submit them to
197 * the queue when needed.
198 *
199 * The kernel modifies neither the indices array nor the entries
200 * array.
201 */
202 u32 *sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700203 unsigned cached_sq_head;
204 unsigned sq_entries;
205 unsigned sq_mask;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700206 unsigned sq_thread_idle;
Jens Axboe498ccd92019-10-25 10:04:25 -0600207 unsigned cached_sq_dropped;
Jens Axboe206aefd2019-11-07 18:27:42 -0700208 atomic_t cached_cq_overflow;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700209 struct io_uring_sqe *sq_sqes;
Jens Axboede0617e2019-04-06 21:51:27 -0600210
211 struct list_head defer_list;
Jens Axboe5262f562019-09-17 12:26:57 -0600212 struct list_head timeout_list;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700213 struct list_head cq_overflow_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700214
Jens Axboefcb323c2019-10-24 12:39:47 -0600215 wait_queue_head_t inflight_wait;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700216 } ____cacheline_aligned_in_smp;
217
Hristo Venev75b28af2019-08-26 17:23:46 +0000218 struct io_rings *rings;
219
Jens Axboe2b188cc2019-01-07 10:46:33 -0700220 /* IO offload */
Jens Axboe561fb042019-10-24 07:25:42 -0600221 struct io_wq *io_wq;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700222 struct task_struct *sqo_thread; /* if using sq thread polling */
223 struct mm_struct *sqo_mm;
224 wait_queue_head_t sqo_wait;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700225
Jens Axboe6b063142019-01-10 22:13:58 -0700226 /*
227 * If used, fixed file set. Writers must ensure that ->refs is dead,
228 * readers must ensure that ->refs is alive as long as the file* is
229 * used. Only updated through io_uring_register(2).
230 */
Jens Axboe65e19f52019-10-26 07:20:21 -0600231 struct fixed_file_table *file_table;
Jens Axboe6b063142019-01-10 22:13:58 -0700232 unsigned nr_user_files;
233
Jens Axboeedafcce2019-01-09 09:16:05 -0700234 /* if used, fixed mapped user buffers */
235 unsigned nr_user_bufs;
236 struct io_mapped_ubuf *user_bufs;
237
Jens Axboe2b188cc2019-01-07 10:46:33 -0700238 struct user_struct *user;
239
Jens Axboe206aefd2019-11-07 18:27:42 -0700240 /* 0 is for ctx quiesce/reinit/free, 1 is for sqo_thread started */
241 struct completion *completions;
242
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700243 /* if all else fails... */
244 struct io_kiocb *fallback_req;
245
Jens Axboe206aefd2019-11-07 18:27:42 -0700246#if defined(CONFIG_UNIX)
247 struct socket *ring_sock;
248#endif
249
250 struct {
251 unsigned cached_cq_tail;
252 unsigned cq_entries;
253 unsigned cq_mask;
254 atomic_t cq_timeouts;
255 struct wait_queue_head cq_wait;
256 struct fasync_struct *cq_fasync;
257 struct eventfd_ctx *cq_ev_fd;
258 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700259
260 struct {
261 struct mutex uring_lock;
262 wait_queue_head_t wait;
263 } ____cacheline_aligned_in_smp;
264
265 struct {
266 spinlock_t completion_lock;
Jens Axboedef596e2019-01-09 08:59:42 -0700267 bool poll_multi_file;
268 /*
269 * ->poll_list is protected by the ctx->uring_lock for
270 * io_uring instances that don't use IORING_SETUP_SQPOLL.
271 * For SQPOLL, only the single threaded io_sq_thread() will
272 * manipulate the list, hence no extra locking is needed there.
273 */
274 struct list_head poll_list;
Jens Axboeeac406c2019-11-14 12:09:58 -0700275 struct rb_root cancel_tree;
Jens Axboefcb323c2019-10-24 12:39:47 -0600276
277 spinlock_t inflight_lock;
278 struct list_head inflight_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700279 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700280};
281
282struct sqe_submit {
283 const struct io_uring_sqe *sqe;
Jens Axboefcb323c2019-10-24 12:39:47 -0600284 struct file *ring_file;
285 int ring_fd;
Jackie Liu8776f3f2019-09-09 20:50:39 +0800286 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700287 bool has_user;
Jackie Liuba5290c2019-10-09 09:19:59 +0800288 bool in_async;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700289 bool needs_fixed_file;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700290};
291
Jens Axboe09bb8392019-03-13 12:39:28 -0600292/*
293 * First field must be the file pointer in all the
294 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
295 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700296struct io_poll_iocb {
297 struct file *file;
298 struct wait_queue_head *head;
299 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600300 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700301 bool canceled;
302 struct wait_queue_entry wait;
303};
304
Jens Axboead8a48a2019-11-15 08:49:11 -0700305struct io_timeout_data {
306 struct io_kiocb *req;
307 struct hrtimer timer;
308 struct timespec64 ts;
309 enum hrtimer_mode mode;
310};
311
Jens Axboe5262f562019-09-17 12:26:57 -0600312struct io_timeout {
313 struct file *file;
Jens Axboead8a48a2019-11-15 08:49:11 -0700314 struct io_timeout_data *data;
Jens Axboe5262f562019-09-17 12:26:57 -0600315};
316
Jens Axboe09bb8392019-03-13 12:39:28 -0600317/*
318 * NOTE! Each of the iocb union members has the file pointer
319 * as the first entry in their struct definition. So you can
320 * access the file pointer through any of the sub-structs,
321 * or directly as just 'ki_filp' in this struct.
322 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700323struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700324 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600325 struct file *file;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700326 struct kiocb rw;
327 struct io_poll_iocb poll;
Jens Axboe5262f562019-09-17 12:26:57 -0600328 struct io_timeout timeout;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700329 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700330
331 struct sqe_submit submit;
332
333 struct io_ring_ctx *ctx;
Jens Axboeeac406c2019-11-14 12:09:58 -0700334 union {
335 struct list_head list;
336 struct rb_node rb_node;
337 };
Jens Axboe9e645e112019-05-10 16:07:28 -0600338 struct list_head link_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700339 unsigned int flags;
Jens Axboec16361c2019-01-17 08:39:48 -0700340 refcount_t refs;
Stefan Bühler8449eed2019-04-27 20:34:19 +0200341#define REQ_F_NOWAIT 1 /* must not punt to workers */
Jens Axboedef596e2019-01-09 08:59:42 -0700342#define REQ_F_IOPOLL_COMPLETED 2 /* polled IO has completed */
Jens Axboe6b063142019-01-10 22:13:58 -0700343#define REQ_F_FIXED_FILE 4 /* ctx owns file */
Jens Axboe4d7dd462019-11-20 13:03:52 -0700344#define REQ_F_LINK_NEXT 8 /* already grabbed next link */
Stefan Bühlere2033e32019-05-11 19:08:01 +0200345#define REQ_F_IO_DRAIN 16 /* drain existing IO first */
346#define REQ_F_IO_DRAINED 32 /* drain done */
Jens Axboe9e645e112019-05-10 16:07:28 -0600347#define REQ_F_LINK 64 /* linked sqes */
Jens Axboe2665abf2019-11-05 12:40:47 -0700348#define REQ_F_LINK_TIMEOUT 128 /* has linked timeout */
Zhengyuan Liuf7b76ac2019-07-16 23:26:14 +0800349#define REQ_F_FAIL_LINK 256 /* fail rest of links */
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +0300350#define REQ_F_DRAIN_LINK 512 /* link should be fully drained */
Jens Axboe5262f562019-09-17 12:26:57 -0600351#define REQ_F_TIMEOUT 1024 /* timeout request */
Jens Axboe491381ce2019-10-17 09:20:46 -0600352#define REQ_F_ISREG 2048 /* regular file */
353#define REQ_F_MUST_PUNT 4096 /* must be punted even for NONBLOCK */
Jens Axboe93bd25b2019-11-11 23:34:31 -0700354#define REQ_F_TIMEOUT_NOSEQ 8192 /* no timeout sequence */
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800355#define REQ_F_INFLIGHT 16384 /* on inflight list */
356#define REQ_F_COMP_LOCKED 32768 /* completion under lock */
Jens Axboe94ae5e72019-11-14 19:39:52 -0700357#define REQ_F_FREE_SQE 65536 /* free sqe if not async queued */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700358 u64 user_data;
Jens Axboe9e645e112019-05-10 16:07:28 -0600359 u32 result;
Jens Axboede0617e2019-04-06 21:51:27 -0600360 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700361
Jens Axboefcb323c2019-10-24 12:39:47 -0600362 struct list_head inflight_entry;
363
Jens Axboe561fb042019-10-24 07:25:42 -0600364 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700365};
366
367#define IO_PLUG_THRESHOLD 2
Jens Axboedef596e2019-01-09 08:59:42 -0700368#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700369
Jens Axboe9a56a232019-01-09 09:06:50 -0700370struct io_submit_state {
371 struct blk_plug plug;
372
373 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700374 * io_kiocb alloc cache
375 */
376 void *reqs[IO_IOPOLL_BATCH];
377 unsigned int free_reqs;
378 unsigned int cur_req;
379
380 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700381 * File reference cache
382 */
383 struct file *file;
384 unsigned int fd;
385 unsigned int has_refs;
386 unsigned int used_refs;
387 unsigned int ios_left;
388};
389
Jens Axboe561fb042019-10-24 07:25:42 -0600390static void io_wq_submit_work(struct io_wq_work **workptr);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700391static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liu4fe2c962019-09-09 20:50:40 +0800392static void __io_free_req(struct io_kiocb *req);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800393static void io_put_req(struct io_kiocb *req);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700394static void io_double_put_req(struct io_kiocb *req);
Jens Axboe978db572019-11-14 22:39:04 -0700395static void __io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700396static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
397static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboede0617e2019-04-06 21:51:27 -0600398
Jens Axboe2b188cc2019-01-07 10:46:33 -0700399static struct kmem_cache *req_cachep;
400
401static const struct file_operations io_uring_fops;
402
403struct sock *io_uring_get_socket(struct file *file)
404{
405#if defined(CONFIG_UNIX)
406 if (file->f_op == &io_uring_fops) {
407 struct io_ring_ctx *ctx = file->private_data;
408
409 return ctx->ring_sock->sk;
410 }
411#endif
412 return NULL;
413}
414EXPORT_SYMBOL(io_uring_get_socket);
415
416static void io_ring_ctx_ref_free(struct percpu_ref *ref)
417{
418 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
419
Jens Axboe206aefd2019-11-07 18:27:42 -0700420 complete(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700421}
422
423static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
424{
425 struct io_ring_ctx *ctx;
426
427 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
428 if (!ctx)
429 return NULL;
430
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700431 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
432 if (!ctx->fallback_req)
433 goto err;
434
Jens Axboe206aefd2019-11-07 18:27:42 -0700435 ctx->completions = kmalloc(2 * sizeof(struct completion), GFP_KERNEL);
436 if (!ctx->completions)
437 goto err;
438
Roman Gushchin21482892019-05-07 10:01:48 -0700439 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -0700440 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
441 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700442
443 ctx->flags = p->flags;
444 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700445 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe206aefd2019-11-07 18:27:42 -0700446 init_completion(&ctx->completions[0]);
447 init_completion(&ctx->completions[1]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700448 mutex_init(&ctx->uring_lock);
449 init_waitqueue_head(&ctx->wait);
450 spin_lock_init(&ctx->completion_lock);
Jens Axboedef596e2019-01-09 08:59:42 -0700451 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboeeac406c2019-11-14 12:09:58 -0700452 ctx->cancel_tree = RB_ROOT;
Jens Axboede0617e2019-04-06 21:51:27 -0600453 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -0600454 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -0600455 init_waitqueue_head(&ctx->inflight_wait);
456 spin_lock_init(&ctx->inflight_lock);
457 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700458 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -0700459err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700460 if (ctx->fallback_req)
461 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe206aefd2019-11-07 18:27:42 -0700462 kfree(ctx->completions);
463 kfree(ctx);
464 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700465}
466
Bob Liu9d858b22019-11-13 18:06:25 +0800467static inline bool __req_need_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -0600468{
Jackie Liua197f662019-11-08 08:09:12 -0700469 struct io_ring_ctx *ctx = req->ctx;
470
Jens Axboe498ccd92019-10-25 10:04:25 -0600471 return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped
472 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboede0617e2019-04-06 21:51:27 -0600473}
474
Bob Liu9d858b22019-11-13 18:06:25 +0800475static inline bool req_need_defer(struct io_kiocb *req)
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600476{
Bob Liu9d858b22019-11-13 18:06:25 +0800477 if ((req->flags & (REQ_F_IO_DRAIN|REQ_F_IO_DRAINED)) == REQ_F_IO_DRAIN)
478 return __req_need_defer(req);
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600479
Bob Liu9d858b22019-11-13 18:06:25 +0800480 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600481}
482
483static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -0600484{
485 struct io_kiocb *req;
486
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600487 req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list);
Bob Liu9d858b22019-11-13 18:06:25 +0800488 if (req && !req_need_defer(req)) {
Jens Axboede0617e2019-04-06 21:51:27 -0600489 list_del_init(&req->list);
490 return req;
491 }
492
493 return NULL;
494}
495
Jens Axboe5262f562019-09-17 12:26:57 -0600496static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx)
497{
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600498 struct io_kiocb *req;
499
500 req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list);
Jens Axboe93bd25b2019-11-11 23:34:31 -0700501 if (req) {
502 if (req->flags & REQ_F_TIMEOUT_NOSEQ)
503 return NULL;
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800504 if (!__req_need_defer(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -0700505 list_del_init(&req->list);
506 return req;
507 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600508 }
509
510 return NULL;
Jens Axboe5262f562019-09-17 12:26:57 -0600511}
512
Jens Axboede0617e2019-04-06 21:51:27 -0600513static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700514{
Hristo Venev75b28af2019-08-26 17:23:46 +0000515 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700516
Hristo Venev75b28af2019-08-26 17:23:46 +0000517 if (ctx->cached_cq_tail != READ_ONCE(rings->cq.tail)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700518 /* order cqe stores with ring update */
Hristo Venev75b28af2019-08-26 17:23:46 +0000519 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700520
Jens Axboe2b188cc2019-01-07 10:46:33 -0700521 if (wq_has_sleeper(&ctx->cq_wait)) {
522 wake_up_interruptible(&ctx->cq_wait);
523 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
524 }
525 }
526}
527
Jens Axboe561fb042019-10-24 07:25:42 -0600528static inline bool io_sqe_needs_user(const struct io_uring_sqe *sqe)
Jens Axboe18d9be12019-09-10 09:13:05 -0600529{
Jens Axboe561fb042019-10-24 07:25:42 -0600530 u8 opcode = READ_ONCE(sqe->opcode);
531
532 return !(opcode == IORING_OP_READ_FIXED ||
533 opcode == IORING_OP_WRITE_FIXED);
534}
535
Jens Axboe94ae5e72019-11-14 19:39:52 -0700536static inline bool io_prep_async_work(struct io_kiocb *req,
537 struct io_kiocb **link)
Jens Axboe561fb042019-10-24 07:25:42 -0600538{
539 bool do_hashed = false;
Jens Axboe54a91f32019-09-10 09:15:04 -0600540
Jens Axboe6cc47d12019-09-18 11:18:23 -0600541 if (req->submit.sqe) {
542 switch (req->submit.sqe->opcode) {
543 case IORING_OP_WRITEV:
544 case IORING_OP_WRITE_FIXED:
Jens Axboe561fb042019-10-24 07:25:42 -0600545 do_hashed = true;
Jens Axboe5f8fd2d2019-11-07 10:57:36 -0700546 /* fall-through */
547 case IORING_OP_READV:
548 case IORING_OP_READ_FIXED:
549 case IORING_OP_SENDMSG:
550 case IORING_OP_RECVMSG:
551 case IORING_OP_ACCEPT:
552 case IORING_OP_POLL_ADD:
Jens Axboef8e85cf2019-11-23 14:24:24 -0700553 case IORING_OP_CONNECT:
Jens Axboe5f8fd2d2019-11-07 10:57:36 -0700554 /*
555 * We know REQ_F_ISREG is not set on some of these
556 * opcodes, but this enables us to keep the check in
557 * just one place.
558 */
559 if (!(req->flags & REQ_F_ISREG))
560 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe6cc47d12019-09-18 11:18:23 -0600561 break;
562 }
Jens Axboe561fb042019-10-24 07:25:42 -0600563 if (io_sqe_needs_user(req->submit.sqe))
564 req->work.flags |= IO_WQ_WORK_NEEDS_USER;
Jens Axboe54a91f32019-09-10 09:15:04 -0600565 }
566
Jens Axboe94ae5e72019-11-14 19:39:52 -0700567 *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -0600568 return do_hashed;
569}
570
Jackie Liua197f662019-11-08 08:09:12 -0700571static inline void io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -0600572{
Jackie Liua197f662019-11-08 08:09:12 -0700573 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe94ae5e72019-11-14 19:39:52 -0700574 struct io_kiocb *link;
575 bool do_hashed;
576
577 do_hashed = io_prep_async_work(req, &link);
Jens Axboe561fb042019-10-24 07:25:42 -0600578
579 trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work,
580 req->flags);
581 if (!do_hashed) {
582 io_wq_enqueue(ctx->io_wq, &req->work);
583 } else {
584 io_wq_enqueue_hashed(ctx->io_wq, &req->work,
585 file_inode(req->file));
586 }
Jens Axboe94ae5e72019-11-14 19:39:52 -0700587
588 if (link)
589 io_queue_linked_timeout(link);
Jens Axboe18d9be12019-09-10 09:13:05 -0600590}
591
Jens Axboe5262f562019-09-17 12:26:57 -0600592static void io_kill_timeout(struct io_kiocb *req)
593{
594 int ret;
595
Jens Axboead8a48a2019-11-15 08:49:11 -0700596 ret = hrtimer_try_to_cancel(&req->timeout.data->timer);
Jens Axboe5262f562019-09-17 12:26:57 -0600597 if (ret != -1) {
598 atomic_inc(&req->ctx->cq_timeouts);
Jens Axboe842f9612019-10-29 12:34:10 -0600599 list_del_init(&req->list);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700600 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800601 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -0600602 }
603}
604
605static void io_kill_timeouts(struct io_ring_ctx *ctx)
606{
607 struct io_kiocb *req, *tmp;
608
609 spin_lock_irq(&ctx->completion_lock);
610 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
611 io_kill_timeout(req);
612 spin_unlock_irq(&ctx->completion_lock);
613}
614
Jens Axboede0617e2019-04-06 21:51:27 -0600615static void io_commit_cqring(struct io_ring_ctx *ctx)
616{
617 struct io_kiocb *req;
618
Jens Axboe5262f562019-09-17 12:26:57 -0600619 while ((req = io_get_timeout_req(ctx)) != NULL)
620 io_kill_timeout(req);
621
Jens Axboede0617e2019-04-06 21:51:27 -0600622 __io_commit_cqring(ctx);
623
624 while ((req = io_get_deferred_req(ctx)) != NULL) {
625 req->flags |= REQ_F_IO_DRAINED;
Jackie Liua197f662019-11-08 08:09:12 -0700626 io_queue_async_work(req);
Jens Axboede0617e2019-04-06 21:51:27 -0600627 }
628}
629
Jens Axboe2b188cc2019-01-07 10:46:33 -0700630static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
631{
Hristo Venev75b28af2019-08-26 17:23:46 +0000632 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700633 unsigned tail;
634
635 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +0200636 /*
637 * writes to the cq entry need to come after reading head; the
638 * control dependency is enough as we're using WRITE_ONCE to
639 * fill the cq entry
640 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000641 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700642 return NULL;
643
644 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +0000645 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -0700646}
647
Jens Axboe8c838782019-03-12 15:48:16 -0600648static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
649{
650 if (waitqueue_active(&ctx->wait))
651 wake_up(&ctx->wait);
652 if (waitqueue_active(&ctx->sqo_wait))
653 wake_up(&ctx->sqo_wait);
Jens Axboe9b402842019-04-11 11:45:41 -0600654 if (ctx->cq_ev_fd)
655 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -0600656}
657
Jens Axboec4a2ed72019-11-21 21:01:26 -0700658/* Returns true if there are no backlogged entries after the flush */
659static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700660{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700661 struct io_rings *rings = ctx->rings;
662 struct io_uring_cqe *cqe;
663 struct io_kiocb *req;
664 unsigned long flags;
665 LIST_HEAD(list);
666
667 if (!force) {
668 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -0700669 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700670 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
671 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -0700672 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700673 }
674
675 spin_lock_irqsave(&ctx->completion_lock, flags);
676
677 /* if force is set, the ring is going away. always drop after that */
678 if (force)
679 ctx->cq_overflow_flushed = true;
680
Jens Axboec4a2ed72019-11-21 21:01:26 -0700681 cqe = NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700682 while (!list_empty(&ctx->cq_overflow_list)) {
683 cqe = io_get_cqring(ctx);
684 if (!cqe && !force)
685 break;
686
687 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
688 list);
689 list_move(&req->list, &list);
690 if (cqe) {
691 WRITE_ONCE(cqe->user_data, req->user_data);
692 WRITE_ONCE(cqe->res, req->result);
693 WRITE_ONCE(cqe->flags, 0);
694 } else {
695 WRITE_ONCE(ctx->rings->cq_overflow,
696 atomic_inc_return(&ctx->cached_cq_overflow));
697 }
698 }
699
700 io_commit_cqring(ctx);
701 spin_unlock_irqrestore(&ctx->completion_lock, flags);
702 io_cqring_ev_posted(ctx);
703
704 while (!list_empty(&list)) {
705 req = list_first_entry(&list, struct io_kiocb, list);
706 list_del(&req->list);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800707 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700708 }
Jens Axboec4a2ed72019-11-21 21:01:26 -0700709
710 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700711}
712
Jens Axboe78e19bb2019-11-06 15:21:34 -0700713static void io_cqring_fill_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700714{
Jens Axboe78e19bb2019-11-06 15:21:34 -0700715 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700716 struct io_uring_cqe *cqe;
717
Jens Axboe78e19bb2019-11-06 15:21:34 -0700718 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -0700719
Jens Axboe2b188cc2019-01-07 10:46:33 -0700720 /*
721 * If we can't get a cq entry, userspace overflowed the
722 * submission (by quite a lot). Increment the overflow count in
723 * the ring.
724 */
725 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700726 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -0700727 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700728 WRITE_ONCE(cqe->res, res);
729 WRITE_ONCE(cqe->flags, 0);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700730 } else if (ctx->cq_overflow_flushed) {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700731 WRITE_ONCE(ctx->rings->cq_overflow,
732 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700733 } else {
734 refcount_inc(&req->refs);
735 req->result = res;
736 list_add_tail(&req->list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700737 }
738}
739
Jens Axboe78e19bb2019-11-06 15:21:34 -0700740static void io_cqring_add_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700741{
Jens Axboe78e19bb2019-11-06 15:21:34 -0700742 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700743 unsigned long flags;
744
745 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700746 io_cqring_fill_event(req, res);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700747 io_commit_cqring(ctx);
748 spin_unlock_irqrestore(&ctx->completion_lock, flags);
749
Jens Axboe8c838782019-03-12 15:48:16 -0600750 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700751}
752
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700753static inline bool io_is_fallback_req(struct io_kiocb *req)
754{
755 return req == (struct io_kiocb *)
756 ((unsigned long) req->ctx->fallback_req & ~1UL);
757}
758
759static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
760{
761 struct io_kiocb *req;
762
763 req = ctx->fallback_req;
764 if (!test_and_set_bit_lock(0, (unsigned long *) ctx->fallback_req))
765 return req;
766
767 return NULL;
768}
769
Jens Axboe2579f912019-01-09 09:10:43 -0700770static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
771 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700772{
Jens Axboefd6fab22019-03-14 16:30:06 -0600773 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700774 struct io_kiocb *req;
775
776 if (!percpu_ref_tryget(&ctx->refs))
777 return NULL;
778
Jens Axboe2579f912019-01-09 09:10:43 -0700779 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -0600780 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -0700781 if (unlikely(!req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700782 goto fallback;
Jens Axboe2579f912019-01-09 09:10:43 -0700783 } else if (!state->free_reqs) {
784 size_t sz;
785 int ret;
786
787 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -0600788 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
789
790 /*
791 * Bulk alloc is all-or-nothing. If we fail to get a batch,
792 * retry single alloc to be on the safe side.
793 */
794 if (unlikely(ret <= 0)) {
795 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
796 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700797 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -0600798 ret = 1;
799 }
Jens Axboe2579f912019-01-09 09:10:43 -0700800 state->free_reqs = ret - 1;
801 state->cur_req = 1;
802 req = state->reqs[0];
803 } else {
804 req = state->reqs[state->cur_req];
805 state->free_reqs--;
806 state->cur_req++;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700807 }
808
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700809got_it:
Jens Axboe60c112b2019-06-21 10:20:18 -0600810 req->file = NULL;
Jens Axboe2579f912019-01-09 09:10:43 -0700811 req->ctx = ctx;
812 req->flags = 0;
Jens Axboee65ef562019-03-12 10:16:44 -0600813 /* one is dropped after submission, the other at completion */
814 refcount_set(&req->refs, 2);
Jens Axboe9e645e112019-05-10 16:07:28 -0600815 req->result = 0;
Jens Axboe561fb042019-10-24 07:25:42 -0600816 INIT_IO_WORK(&req->work, io_wq_submit_work);
Jens Axboe2579f912019-01-09 09:10:43 -0700817 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700818fallback:
819 req = io_get_fallback_req(ctx);
820 if (req)
821 goto got_it;
Pavel Begunkov6805b322019-10-08 02:18:42 +0300822 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700823 return NULL;
824}
825
Jens Axboedef596e2019-01-09 08:59:42 -0700826static void io_free_req_many(struct io_ring_ctx *ctx, void **reqs, int *nr)
827{
828 if (*nr) {
829 kmem_cache_free_bulk(req_cachep, *nr, reqs);
Pavel Begunkov6805b322019-10-08 02:18:42 +0300830 percpu_ref_put_many(&ctx->refs, *nr);
Jens Axboedef596e2019-01-09 08:59:42 -0700831 *nr = 0;
832 }
833}
834
Jens Axboe9e645e112019-05-10 16:07:28 -0600835static void __io_free_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700836{
Jens Axboefcb323c2019-10-24 12:39:47 -0600837 struct io_ring_ctx *ctx = req->ctx;
838
Pavel Begunkovbbad27b2019-11-19 23:32:47 +0300839 if (req->flags & REQ_F_FREE_SQE)
840 kfree(req->submit.sqe);
Jens Axboe09bb8392019-03-13 12:39:28 -0600841 if (req->file && !(req->flags & REQ_F_FIXED_FILE))
842 fput(req->file);
Jens Axboefcb323c2019-10-24 12:39:47 -0600843 if (req->flags & REQ_F_INFLIGHT) {
844 unsigned long flags;
845
846 spin_lock_irqsave(&ctx->inflight_lock, flags);
847 list_del(&req->inflight_entry);
848 if (waitqueue_active(&ctx->inflight_wait))
849 wake_up(&ctx->inflight_wait);
850 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
851 }
Jens Axboead8a48a2019-11-15 08:49:11 -0700852 if (req->flags & REQ_F_TIMEOUT)
853 kfree(req->timeout.data);
Jens Axboefcb323c2019-10-24 12:39:47 -0600854 percpu_ref_put(&ctx->refs);
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700855 if (likely(!io_is_fallback_req(req)))
856 kmem_cache_free(req_cachep, req);
857 else
858 clear_bit_unlock(0, (unsigned long *) ctx->fallback_req);
Jens Axboee65ef562019-03-12 10:16:44 -0600859}
860
Jackie Liua197f662019-11-08 08:09:12 -0700861static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -0600862{
Jackie Liua197f662019-11-08 08:09:12 -0700863 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -0700864 int ret;
865
Jens Axboead8a48a2019-11-15 08:49:11 -0700866 ret = hrtimer_try_to_cancel(&req->timeout.data->timer);
Jens Axboe2665abf2019-11-05 12:40:47 -0700867 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -0700868 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -0700869 io_commit_cqring(ctx);
870 req->flags &= ~REQ_F_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +0800871 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -0700872 return true;
873 }
874
875 return false;
876}
877
Jens Axboeba816ad2019-09-28 11:36:45 -0600878static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -0600879{
Jens Axboe2665abf2019-11-05 12:40:47 -0700880 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -0600881 struct io_kiocb *nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -0700882 bool wake_ev = false;
Jens Axboe9e645e112019-05-10 16:07:28 -0600883
Jens Axboe4d7dd462019-11-20 13:03:52 -0700884 /* Already got next link */
885 if (req->flags & REQ_F_LINK_NEXT)
886 return;
887
Jens Axboe9e645e112019-05-10 16:07:28 -0600888 /*
889 * The list should never be empty when we are called here. But could
890 * potentially happen if the chain is messed up, check to be on the
891 * safe side.
892 */
893 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, list);
Jens Axboe2665abf2019-11-05 12:40:47 -0700894 while (nxt) {
Jens Axboe76a46e02019-11-10 23:34:16 -0700895 list_del_init(&nxt->list);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700896
897 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
898 (nxt->flags & REQ_F_TIMEOUT)) {
899 wake_ev |= io_link_cancel_timeout(nxt);
900 nxt = list_first_entry_or_null(&req->link_list,
901 struct io_kiocb, list);
902 req->flags &= ~REQ_F_LINK_TIMEOUT;
903 continue;
904 }
Jens Axboe9e645e112019-05-10 16:07:28 -0600905 if (!list_empty(&req->link_list)) {
906 INIT_LIST_HEAD(&nxt->link_list);
907 list_splice(&req->link_list, &nxt->link_list);
908 nxt->flags |= REQ_F_LINK;
909 }
910
Pavel Begunkovb18fdf72019-11-21 23:21:02 +0300911 *nxtptr = nxt;
Jens Axboe94ae5e72019-11-14 19:39:52 -0700912 break;
Jens Axboe9e645e112019-05-10 16:07:28 -0600913 }
Jens Axboe2665abf2019-11-05 12:40:47 -0700914
Jens Axboe4d7dd462019-11-20 13:03:52 -0700915 req->flags |= REQ_F_LINK_NEXT;
Jens Axboe2665abf2019-11-05 12:40:47 -0700916 if (wake_ev)
917 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -0600918}
919
920/*
921 * Called if REQ_F_LINK is set, and we fail the head request
922 */
923static void io_fail_links(struct io_kiocb *req)
924{
Jens Axboe2665abf2019-11-05 12:40:47 -0700925 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -0600926 struct io_kiocb *link;
Jens Axboe2665abf2019-11-05 12:40:47 -0700927 unsigned long flags;
928
929 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -0600930
931 while (!list_empty(&req->link_list)) {
932 link = list_first_entry(&req->link_list, struct io_kiocb, list);
Jens Axboe2665abf2019-11-05 12:40:47 -0700933 list_del_init(&link->list);
Jens Axboe9e645e112019-05-10 16:07:28 -0600934
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +0200935 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -0700936
937 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
938 link->submit.sqe->opcode == IORING_OP_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -0700939 io_link_cancel_timeout(link);
Jens Axboe2665abf2019-11-05 12:40:47 -0700940 } else {
Jens Axboe78e19bb2019-11-06 15:21:34 -0700941 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe978db572019-11-14 22:39:04 -0700942 __io_double_put_req(link);
Jens Axboe2665abf2019-11-05 12:40:47 -0700943 }
Jens Axboe5d960722019-11-19 15:31:28 -0700944 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -0600945 }
Jens Axboe2665abf2019-11-05 12:40:47 -0700946
947 io_commit_cqring(ctx);
948 spin_unlock_irqrestore(&ctx->completion_lock, flags);
949 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -0600950}
951
Jens Axboe4d7dd462019-11-20 13:03:52 -0700952static void io_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -0600953{
Jens Axboe4d7dd462019-11-20 13:03:52 -0700954 if (likely(!(req->flags & REQ_F_LINK)))
Jens Axboe2665abf2019-11-05 12:40:47 -0700955 return;
Jens Axboe2665abf2019-11-05 12:40:47 -0700956
Jens Axboe9e645e112019-05-10 16:07:28 -0600957 /*
958 * If LINK is set, we have dependent requests in this chain. If we
959 * didn't fail this request, queue the first one up, moving any other
960 * dependencies to the next request. In case of failure, fail the rest
961 * of the chain.
962 */
Jens Axboe2665abf2019-11-05 12:40:47 -0700963 if (req->flags & REQ_F_FAIL_LINK) {
964 io_fail_links(req);
Jens Axboe7c9e7f02019-11-12 08:15:53 -0700965 } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) ==
966 REQ_F_LINK_TIMEOUT) {
Jens Axboe2665abf2019-11-05 12:40:47 -0700967 struct io_ring_ctx *ctx = req->ctx;
968 unsigned long flags;
969
970 /*
971 * If this is a timeout link, we could be racing with the
972 * timeout timer. Grab the completion lock for this case to
Jens Axboe7c9e7f02019-11-12 08:15:53 -0700973 * protect against that.
Jens Axboe2665abf2019-11-05 12:40:47 -0700974 */
975 spin_lock_irqsave(&ctx->completion_lock, flags);
976 io_req_link_next(req, nxt);
977 spin_unlock_irqrestore(&ctx->completion_lock, flags);
978 } else {
979 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -0600980 }
Jens Axboe4d7dd462019-11-20 13:03:52 -0700981}
Jens Axboe9e645e112019-05-10 16:07:28 -0600982
Jackie Liuc69f8db2019-11-09 11:00:08 +0800983static void io_free_req(struct io_kiocb *req)
984{
Pavel Begunkov944e58b2019-11-21 23:21:01 +0300985 struct io_kiocb *nxt = NULL;
986
987 io_req_find_next(req, &nxt);
Pavel Begunkov70cf9f32019-11-21 23:21:00 +0300988 __io_free_req(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +0300989
990 if (nxt)
991 io_queue_async_work(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +0800992}
993
Jens Axboeba816ad2019-09-28 11:36:45 -0600994/*
995 * Drop reference to request, return next in chain (if there is one) if this
996 * was the last reference to this request.
997 */
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +0300998__attribute__((nonnull))
Jackie Liuec9c02a2019-11-08 23:50:36 +0800999static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboee65ef562019-03-12 10:16:44 -06001000{
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001001 io_req_find_next(req, nxtptr);
Jens Axboe4d7dd462019-11-20 13:03:52 -07001002
Jens Axboee65ef562019-03-12 10:16:44 -06001003 if (refcount_dec_and_test(&req->refs))
Jens Axboe4d7dd462019-11-20 13:03:52 -07001004 __io_free_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001005}
1006
Jens Axboe2b188cc2019-01-07 10:46:33 -07001007static void io_put_req(struct io_kiocb *req)
1008{
Jens Axboedef596e2019-01-09 08:59:42 -07001009 if (refcount_dec_and_test(&req->refs))
1010 io_free_req(req);
1011}
1012
Jens Axboe978db572019-11-14 22:39:04 -07001013/*
1014 * Must only be used if we don't need to care about links, usually from
1015 * within the completion handling itself.
1016 */
1017static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06001018{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001019 /* drop both submit and complete references */
1020 if (refcount_sub_and_test(2, &req->refs))
1021 __io_free_req(req);
1022}
1023
Jens Axboe978db572019-11-14 22:39:04 -07001024static void io_double_put_req(struct io_kiocb *req)
1025{
1026 /* drop both submit and complete references */
1027 if (refcount_sub_and_test(2, &req->refs))
1028 io_free_req(req);
1029}
1030
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001031static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06001032{
Jens Axboe84f97dc2019-11-06 11:27:53 -07001033 struct io_rings *rings = ctx->rings;
1034
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001035 /*
1036 * noflush == true is from the waitqueue handler, just ensure we wake
1037 * up the task, and the next invocation will flush the entries. We
1038 * cannot safely to it from here.
1039 */
1040 if (noflush && !list_empty(&ctx->cq_overflow_list))
1041 return -1U;
1042
1043 io_cqring_overflow_flush(ctx, false);
1044
Jens Axboea3a0e432019-08-20 11:03:11 -06001045 /* See comment at the top of this file */
1046 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00001047 return READ_ONCE(rings->cq.tail) - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06001048}
1049
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03001050static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1051{
1052 struct io_rings *rings = ctx->rings;
1053
1054 /* make sure SQ entry isn't read before tail */
1055 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1056}
1057
Jens Axboedef596e2019-01-09 08:59:42 -07001058/*
1059 * Find and free completed poll iocbs
1060 */
1061static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
1062 struct list_head *done)
1063{
1064 void *reqs[IO_IOPOLL_BATCH];
1065 struct io_kiocb *req;
Jens Axboe09bb8392019-03-13 12:39:28 -06001066 int to_free;
Jens Axboedef596e2019-01-09 08:59:42 -07001067
Jens Axboe09bb8392019-03-13 12:39:28 -06001068 to_free = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001069 while (!list_empty(done)) {
1070 req = list_first_entry(done, struct io_kiocb, list);
1071 list_del(&req->list);
1072
Jens Axboe78e19bb2019-11-06 15:21:34 -07001073 io_cqring_fill_event(req, req->result);
Jens Axboedef596e2019-01-09 08:59:42 -07001074 (*nr_events)++;
1075
Jens Axboe09bb8392019-03-13 12:39:28 -06001076 if (refcount_dec_and_test(&req->refs)) {
1077 /* If we're not using fixed files, we have to pair the
1078 * completion part with the file put. Use regular
1079 * completions for those, only batch free for fixed
Jens Axboe9e645e112019-05-10 16:07:28 -06001080 * file and non-linked commands.
Jens Axboe09bb8392019-03-13 12:39:28 -06001081 */
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03001082 if (((req->flags &
1083 (REQ_F_FIXED_FILE|REQ_F_LINK|REQ_F_FREE_SQE)) ==
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001084 REQ_F_FIXED_FILE) && !io_is_fallback_req(req)) {
Jens Axboe09bb8392019-03-13 12:39:28 -06001085 reqs[to_free++] = req;
1086 if (to_free == ARRAY_SIZE(reqs))
1087 io_free_req_many(ctx, reqs, &to_free);
Jens Axboe6b063142019-01-10 22:13:58 -07001088 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001089 io_free_req(req);
Jens Axboe6b063142019-01-10 22:13:58 -07001090 }
Jens Axboe9a56a232019-01-09 09:06:50 -07001091 }
Jens Axboedef596e2019-01-09 08:59:42 -07001092 }
Jens Axboedef596e2019-01-09 08:59:42 -07001093
Jens Axboe09bb8392019-03-13 12:39:28 -06001094 io_commit_cqring(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07001095 io_free_req_many(ctx, reqs, &to_free);
1096}
1097
1098static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1099 long min)
1100{
1101 struct io_kiocb *req, *tmp;
1102 LIST_HEAD(done);
1103 bool spin;
1104 int ret;
1105
1106 /*
1107 * Only spin for completions if we don't have multiple devices hanging
1108 * off our complete list, and we're under the requested amount.
1109 */
1110 spin = !ctx->poll_multi_file && *nr_events < min;
1111
1112 ret = 0;
1113 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
1114 struct kiocb *kiocb = &req->rw;
1115
1116 /*
1117 * Move completed entries to our local list. If we find a
1118 * request that requires polling, break out and complete
1119 * the done list first, if we have entries there.
1120 */
1121 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
1122 list_move_tail(&req->list, &done);
1123 continue;
1124 }
1125 if (!list_empty(&done))
1126 break;
1127
1128 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1129 if (ret < 0)
1130 break;
1131
1132 if (ret && spin)
1133 spin = false;
1134 ret = 0;
1135 }
1136
1137 if (!list_empty(&done))
1138 io_iopoll_complete(ctx, nr_events, &done);
1139
1140 return ret;
1141}
1142
1143/*
1144 * Poll for a mininum of 'min' events. Note that if min == 0 we consider that a
1145 * non-spinning poll check - we'll still enter the driver poll loop, but only
1146 * as a non-spinning completion check.
1147 */
1148static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
1149 long min)
1150{
Jens Axboe08f54392019-08-21 22:19:11 -06001151 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07001152 int ret;
1153
1154 ret = io_do_iopoll(ctx, nr_events, min);
1155 if (ret < 0)
1156 return ret;
1157 if (!min || *nr_events >= min)
1158 return 0;
1159 }
1160
1161 return 1;
1162}
1163
1164/*
1165 * We can't just wait for polled events to come to us, we have to actively
1166 * find and complete them.
1167 */
1168static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
1169{
1170 if (!(ctx->flags & IORING_SETUP_IOPOLL))
1171 return;
1172
1173 mutex_lock(&ctx->uring_lock);
1174 while (!list_empty(&ctx->poll_list)) {
1175 unsigned int nr_events = 0;
1176
1177 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -06001178
1179 /*
1180 * Ensure we allow local-to-the-cpu processing to take place,
1181 * in this case we need to ensure that we reap all events.
1182 */
1183 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -07001184 }
1185 mutex_unlock(&ctx->uring_lock);
1186}
1187
Jens Axboe2b2ed972019-10-25 10:06:15 -06001188static int __io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1189 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07001190{
Jens Axboe2b2ed972019-10-25 10:06:15 -06001191 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001192
1193 do {
1194 int tmin = 0;
1195
Jens Axboe500f9fb2019-08-19 12:15:59 -06001196 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06001197 * Don't enter poll loop if we already have events pending.
1198 * If we do, we can potentially be spinning for commands that
1199 * already triggered a CQE (eg in error).
1200 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001201 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06001202 break;
1203
1204 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06001205 * If a submit got punted to a workqueue, we can have the
1206 * application entering polling for a command before it gets
1207 * issued. That app will hold the uring_lock for the duration
1208 * of the poll right here, so we need to take a breather every
1209 * now and then to ensure that the issue has a chance to add
1210 * the poll to the issued list. Otherwise we can spin here
1211 * forever, while the workqueue is stuck trying to acquire the
1212 * very same mutex.
1213 */
1214 if (!(++iters & 7)) {
1215 mutex_unlock(&ctx->uring_lock);
1216 mutex_lock(&ctx->uring_lock);
1217 }
1218
Jens Axboedef596e2019-01-09 08:59:42 -07001219 if (*nr_events < min)
1220 tmin = min - *nr_events;
1221
1222 ret = io_iopoll_getevents(ctx, nr_events, tmin);
1223 if (ret <= 0)
1224 break;
1225 ret = 0;
1226 } while (min && !*nr_events && !need_resched());
1227
Jens Axboe2b2ed972019-10-25 10:06:15 -06001228 return ret;
1229}
1230
1231static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1232 long min)
1233{
1234 int ret;
1235
1236 /*
1237 * We disallow the app entering submit/complete with polling, but we
1238 * still need to lock the ring to prevent racing with polled issue
1239 * that got punted to a workqueue.
1240 */
1241 mutex_lock(&ctx->uring_lock);
1242 ret = __io_iopoll_check(ctx, nr_events, min);
Jens Axboe500f9fb2019-08-19 12:15:59 -06001243 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001244 return ret;
1245}
1246
Jens Axboe491381ce2019-10-17 09:20:46 -06001247static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001248{
Jens Axboe491381ce2019-10-17 09:20:46 -06001249 /*
1250 * Tell lockdep we inherited freeze protection from submission
1251 * thread.
1252 */
1253 if (req->flags & REQ_F_ISREG) {
1254 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001255
Jens Axboe491381ce2019-10-17 09:20:46 -06001256 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001257 }
Jens Axboe491381ce2019-10-17 09:20:46 -06001258 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001259}
1260
Jens Axboeba816ad2019-09-28 11:36:45 -06001261static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001262{
1263 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1264
Jens Axboe491381ce2019-10-17 09:20:46 -06001265 if (kiocb->ki_flags & IOCB_WRITE)
1266 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001267
Jens Axboe9e645e112019-05-10 16:07:28 -06001268 if ((req->flags & REQ_F_LINK) && res != req->result)
1269 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001270 io_cqring_add_event(req, res);
Jens Axboeba816ad2019-09-28 11:36:45 -06001271}
1272
1273static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1274{
1275 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1276
1277 io_complete_rw_common(kiocb, res);
Jens Axboee65ef562019-03-12 10:16:44 -06001278 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001279}
1280
Jens Axboeba816ad2019-09-28 11:36:45 -06001281static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res)
1282{
1283 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001284 struct io_kiocb *nxt = NULL;
Jens Axboeba816ad2019-09-28 11:36:45 -06001285
1286 io_complete_rw_common(kiocb, res);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001287 io_put_req_find_next(req, &nxt);
1288
1289 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001290}
1291
Jens Axboedef596e2019-01-09 08:59:42 -07001292static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1293{
1294 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1295
Jens Axboe491381ce2019-10-17 09:20:46 -06001296 if (kiocb->ki_flags & IOCB_WRITE)
1297 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001298
Jens Axboe9e645e112019-05-10 16:07:28 -06001299 if ((req->flags & REQ_F_LINK) && res != req->result)
1300 req->flags |= REQ_F_FAIL_LINK;
1301 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -07001302 if (res != -EAGAIN)
1303 req->flags |= REQ_F_IOPOLL_COMPLETED;
1304}
1305
1306/*
1307 * After the iocb has been issued, it's safe to be found on the poll list.
1308 * Adding the kiocb to the list AFTER submission ensures that we don't
1309 * find it from a io_iopoll_getevents() thread before the issuer is done
1310 * accessing the kiocb cookie.
1311 */
1312static void io_iopoll_req_issued(struct io_kiocb *req)
1313{
1314 struct io_ring_ctx *ctx = req->ctx;
1315
1316 /*
1317 * Track whether we have multiple files in our lists. This will impact
1318 * how we do polling eventually, not spinning if we're on potentially
1319 * different devices.
1320 */
1321 if (list_empty(&ctx->poll_list)) {
1322 ctx->poll_multi_file = false;
1323 } else if (!ctx->poll_multi_file) {
1324 struct io_kiocb *list_req;
1325
1326 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1327 list);
1328 if (list_req->rw.ki_filp != req->rw.ki_filp)
1329 ctx->poll_multi_file = true;
1330 }
1331
1332 /*
1333 * For fast devices, IO may have already completed. If it has, add
1334 * it to the front so we find it first.
1335 */
1336 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1337 list_add(&req->list, &ctx->poll_list);
1338 else
1339 list_add_tail(&req->list, &ctx->poll_list);
1340}
1341
Jens Axboe3d6770f2019-04-13 11:50:54 -06001342static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07001343{
Jens Axboe3d6770f2019-04-13 11:50:54 -06001344 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -07001345 int diff = state->has_refs - state->used_refs;
1346
1347 if (diff)
1348 fput_many(state->file, diff);
1349 state->file = NULL;
1350 }
1351}
1352
1353/*
1354 * Get as many references to a file as we have IOs left in this submission,
1355 * assuming most submissions are for one file, or at least that each file
1356 * has more than one submission.
1357 */
1358static struct file *io_file_get(struct io_submit_state *state, int fd)
1359{
1360 if (!state)
1361 return fget(fd);
1362
1363 if (state->file) {
1364 if (state->fd == fd) {
1365 state->used_refs++;
1366 state->ios_left--;
1367 return state->file;
1368 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06001369 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07001370 }
1371 state->file = fget_many(fd, state->ios_left);
1372 if (!state->file)
1373 return NULL;
1374
1375 state->fd = fd;
1376 state->has_refs = state->ios_left;
1377 state->used_refs = 1;
1378 state->ios_left--;
1379 return state->file;
1380}
1381
Jens Axboe2b188cc2019-01-07 10:46:33 -07001382/*
1383 * If we tracked the file through the SCM inflight mechanism, we could support
1384 * any file. For now, just ensure that anything potentially problematic is done
1385 * inline.
1386 */
1387static bool io_file_supports_async(struct file *file)
1388{
1389 umode_t mode = file_inode(file)->i_mode;
1390
1391 if (S_ISBLK(mode) || S_ISCHR(mode))
1392 return true;
1393 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
1394 return true;
1395
1396 return false;
1397}
1398
Pavel Begunkov267bc902019-11-07 01:41:08 +03001399static int io_prep_rw(struct io_kiocb *req, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001400{
Pavel Begunkov267bc902019-11-07 01:41:08 +03001401 const struct io_uring_sqe *sqe = req->submit.sqe;
Jens Axboedef596e2019-01-09 08:59:42 -07001402 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001403 struct kiocb *kiocb = &req->rw;
Jens Axboe09bb8392019-03-13 12:39:28 -06001404 unsigned ioprio;
1405 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001406
Jens Axboe09bb8392019-03-13 12:39:28 -06001407 if (!req->file)
1408 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001409
Jens Axboe491381ce2019-10-17 09:20:46 -06001410 if (S_ISREG(file_inode(req->file)->i_mode))
1411 req->flags |= REQ_F_ISREG;
1412
1413 /*
1414 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
1415 * we know to async punt it even if it was opened O_NONBLOCK
1416 */
1417 if (force_nonblock && !io_file_supports_async(req->file)) {
1418 req->flags |= REQ_F_MUST_PUNT;
1419 return -EAGAIN;
1420 }
Jens Axboe6b063142019-01-10 22:13:58 -07001421
Jens Axboe2b188cc2019-01-07 10:46:33 -07001422 kiocb->ki_pos = READ_ONCE(sqe->off);
1423 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
1424 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
1425
1426 ioprio = READ_ONCE(sqe->ioprio);
1427 if (ioprio) {
1428 ret = ioprio_check_cap(ioprio);
1429 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06001430 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001431
1432 kiocb->ki_ioprio = ioprio;
1433 } else
1434 kiocb->ki_ioprio = get_current_ioprio();
1435
1436 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
1437 if (unlikely(ret))
Jens Axboe09bb8392019-03-13 12:39:28 -06001438 return ret;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001439
1440 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06001441 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
1442 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02001443 req->flags |= REQ_F_NOWAIT;
1444
1445 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001446 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001447
Jens Axboedef596e2019-01-09 08:59:42 -07001448 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07001449 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
1450 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06001451 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001452
Jens Axboedef596e2019-01-09 08:59:42 -07001453 kiocb->ki_flags |= IOCB_HIPRI;
1454 kiocb->ki_complete = io_complete_rw_iopoll;
Jens Axboe6873e0b2019-10-30 13:53:09 -06001455 req->result = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001456 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001457 if (kiocb->ki_flags & IOCB_HIPRI)
1458 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07001459 kiocb->ki_complete = io_complete_rw;
1460 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001461 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001462}
1463
1464static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
1465{
1466 switch (ret) {
1467 case -EIOCBQUEUED:
1468 break;
1469 case -ERESTARTSYS:
1470 case -ERESTARTNOINTR:
1471 case -ERESTARTNOHAND:
1472 case -ERESTART_RESTARTBLOCK:
1473 /*
1474 * We can't just restart the syscall, since previously
1475 * submitted sqes may already be in progress. Just fail this
1476 * IO with EINTR.
1477 */
1478 ret = -EINTR;
1479 /* fall through */
1480 default:
1481 kiocb->ki_complete(kiocb, ret, 0);
1482 }
1483}
1484
Jens Axboeba816ad2019-09-28 11:36:45 -06001485static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt,
1486 bool in_async)
1487{
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001488 if (in_async && ret >= 0 && kiocb->ki_complete == io_complete_rw)
Jens Axboeba816ad2019-09-28 11:36:45 -06001489 *nxt = __io_complete_rw(kiocb, ret);
1490 else
1491 io_rw_done(kiocb, ret);
1492}
1493
Jens Axboeedafcce2019-01-09 09:16:05 -07001494static int io_import_fixed(struct io_ring_ctx *ctx, int rw,
1495 const struct io_uring_sqe *sqe,
1496 struct iov_iter *iter)
1497{
1498 size_t len = READ_ONCE(sqe->len);
1499 struct io_mapped_ubuf *imu;
1500 unsigned index, buf_index;
1501 size_t offset;
1502 u64 buf_addr;
1503
1504 /* attempt to use fixed buffers without having provided iovecs */
1505 if (unlikely(!ctx->user_bufs))
1506 return -EFAULT;
1507
1508 buf_index = READ_ONCE(sqe->buf_index);
1509 if (unlikely(buf_index >= ctx->nr_user_bufs))
1510 return -EFAULT;
1511
1512 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
1513 imu = &ctx->user_bufs[index];
1514 buf_addr = READ_ONCE(sqe->addr);
1515
1516 /* overflow */
1517 if (buf_addr + len < buf_addr)
1518 return -EFAULT;
1519 /* not inside the mapped region */
1520 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
1521 return -EFAULT;
1522
1523 /*
1524 * May not be a start of buffer, set size appropriately
1525 * and advance us to the beginning.
1526 */
1527 offset = buf_addr - imu->ubuf;
1528 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06001529
1530 if (offset) {
1531 /*
1532 * Don't use iov_iter_advance() here, as it's really slow for
1533 * using the latter parts of a big fixed buffer - it iterates
1534 * over each segment manually. We can cheat a bit here, because
1535 * we know that:
1536 *
1537 * 1) it's a BVEC iter, we set it up
1538 * 2) all bvecs are PAGE_SIZE in size, except potentially the
1539 * first and last bvec
1540 *
1541 * So just find our index, and adjust the iterator afterwards.
1542 * If the offset is within the first bvec (or the whole first
1543 * bvec, just use iov_iter_advance(). This makes it easier
1544 * since we can just skip the first segment, which may not
1545 * be PAGE_SIZE aligned.
1546 */
1547 const struct bio_vec *bvec = imu->bvec;
1548
1549 if (offset <= bvec->bv_len) {
1550 iov_iter_advance(iter, offset);
1551 } else {
1552 unsigned long seg_skip;
1553
1554 /* skip first vec */
1555 offset -= bvec->bv_len;
1556 seg_skip = 1 + (offset >> PAGE_SHIFT);
1557
1558 iter->bvec = bvec + seg_skip;
1559 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02001560 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001561 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001562 }
1563 }
1564
Jens Axboe5e559562019-11-13 16:12:46 -07001565 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001566}
1567
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001568static ssize_t io_import_iovec(struct io_ring_ctx *ctx, int rw,
1569 const struct sqe_submit *s, struct iovec **iovec,
1570 struct iov_iter *iter)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001571{
1572 const struct io_uring_sqe *sqe = s->sqe;
1573 void __user *buf = u64_to_user_ptr(READ_ONCE(sqe->addr));
1574 size_t sqe_len = READ_ONCE(sqe->len);
Jens Axboeedafcce2019-01-09 09:16:05 -07001575 u8 opcode;
1576
1577 /*
1578 * We're reading ->opcode for the second time, but the first read
1579 * doesn't care whether it's _FIXED or not, so it doesn't matter
1580 * whether ->opcode changes concurrently. The first read does care
1581 * about whether it is a READ or a WRITE, so we don't trust this read
1582 * for that purpose and instead let the caller pass in the read/write
1583 * flag.
1584 */
1585 opcode = READ_ONCE(sqe->opcode);
1586 if (opcode == IORING_OP_READ_FIXED ||
1587 opcode == IORING_OP_WRITE_FIXED) {
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001588 ssize_t ret = io_import_fixed(ctx, rw, sqe, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07001589 *iovec = NULL;
1590 return ret;
1591 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001592
1593 if (!s->has_user)
1594 return -EFAULT;
1595
1596#ifdef CONFIG_COMPAT
1597 if (ctx->compat)
1598 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
1599 iovec, iter);
1600#endif
1601
1602 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
1603}
1604
Jens Axboe32960612019-09-23 11:05:34 -06001605/*
1606 * For files that don't have ->read_iter() and ->write_iter(), handle them
1607 * by looping over ->read() or ->write() manually.
1608 */
1609static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
1610 struct iov_iter *iter)
1611{
1612 ssize_t ret = 0;
1613
1614 /*
1615 * Don't support polled IO through this interface, and we can't
1616 * support non-blocking either. For the latter, this just causes
1617 * the kiocb to be handled from an async context.
1618 */
1619 if (kiocb->ki_flags & IOCB_HIPRI)
1620 return -EOPNOTSUPP;
1621 if (kiocb->ki_flags & IOCB_NOWAIT)
1622 return -EAGAIN;
1623
1624 while (iov_iter_count(iter)) {
1625 struct iovec iovec = iov_iter_iovec(iter);
1626 ssize_t nr;
1627
1628 if (rw == READ) {
1629 nr = file->f_op->read(file, iovec.iov_base,
1630 iovec.iov_len, &kiocb->ki_pos);
1631 } else {
1632 nr = file->f_op->write(file, iovec.iov_base,
1633 iovec.iov_len, &kiocb->ki_pos);
1634 }
1635
1636 if (nr < 0) {
1637 if (!ret)
1638 ret = nr;
1639 break;
1640 }
1641 ret += nr;
1642 if (nr != iovec.iov_len)
1643 break;
1644 iov_iter_advance(iter, nr);
1645 }
1646
1647 return ret;
1648}
1649
Pavel Begunkov267bc902019-11-07 01:41:08 +03001650static int io_read(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001651 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001652{
1653 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1654 struct kiocb *kiocb = &req->rw;
1655 struct iov_iter iter;
1656 struct file *file;
Jens Axboe31b51512019-01-18 22:56:34 -07001657 size_t iov_count;
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001658 ssize_t read_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001659
Pavel Begunkov267bc902019-11-07 01:41:08 +03001660 ret = io_prep_rw(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001661 if (ret)
1662 return ret;
1663 file = kiocb->ki_filp;
1664
Jens Axboe2b188cc2019-01-07 10:46:33 -07001665 if (unlikely(!(file->f_mode & FMODE_READ)))
Jens Axboe09bb8392019-03-13 12:39:28 -06001666 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001667
Pavel Begunkov267bc902019-11-07 01:41:08 +03001668 ret = io_import_iovec(req->ctx, READ, &req->submit, &iovec, &iter);
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001669 if (ret < 0)
Jens Axboe09bb8392019-03-13 12:39:28 -06001670 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001671
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001672 read_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06001673 if (req->flags & REQ_F_LINK)
1674 req->result = read_size;
1675
Jens Axboe31b51512019-01-18 22:56:34 -07001676 iov_count = iov_iter_count(&iter);
1677 ret = rw_verify_area(READ, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001678 if (!ret) {
1679 ssize_t ret2;
1680
Jens Axboe32960612019-09-23 11:05:34 -06001681 if (file->f_op->read_iter)
1682 ret2 = call_read_iter(file, kiocb, &iter);
1683 else
1684 ret2 = loop_rw_iter(READ, file, kiocb, &iter);
1685
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001686 /*
1687 * In case of a short read, punt to async. This can happen
1688 * if we have data partially cached. Alternatively we can
1689 * return the short read, in which case the application will
1690 * need to issue another SQE and wait for it. That SQE will
1691 * need async punt anyway, so it's more efficient to do it
1692 * here.
1693 */
Jens Axboe491381ce2019-10-17 09:20:46 -06001694 if (force_nonblock && !(req->flags & REQ_F_NOWAIT) &&
1695 (req->flags & REQ_F_ISREG) &&
1696 ret2 > 0 && ret2 < read_size)
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001697 ret2 = -EAGAIN;
1698 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboe561fb042019-10-24 07:25:42 -06001699 if (!force_nonblock || ret2 != -EAGAIN)
Pavel Begunkov267bc902019-11-07 01:41:08 +03001700 kiocb_done(kiocb, ret2, nxt, req->submit.in_async);
Jens Axboe561fb042019-10-24 07:25:42 -06001701 else
Jens Axboe2b188cc2019-01-07 10:46:33 -07001702 ret = -EAGAIN;
1703 }
1704 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001705 return ret;
1706}
1707
Pavel Begunkov267bc902019-11-07 01:41:08 +03001708static int io_write(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001709 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001710{
1711 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1712 struct kiocb *kiocb = &req->rw;
1713 struct iov_iter iter;
1714 struct file *file;
Jens Axboe31b51512019-01-18 22:56:34 -07001715 size_t iov_count;
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001716 ssize_t ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001717
Pavel Begunkov267bc902019-11-07 01:41:08 +03001718 ret = io_prep_rw(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001719 if (ret)
1720 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001721
Jens Axboe2b188cc2019-01-07 10:46:33 -07001722 file = kiocb->ki_filp;
1723 if (unlikely(!(file->f_mode & FMODE_WRITE)))
Jens Axboe09bb8392019-03-13 12:39:28 -06001724 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001725
Pavel Begunkov267bc902019-11-07 01:41:08 +03001726 ret = io_import_iovec(req->ctx, WRITE, &req->submit, &iovec, &iter);
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001727 if (ret < 0)
Jens Axboe09bb8392019-03-13 12:39:28 -06001728 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001729
Jens Axboe9e645e112019-05-10 16:07:28 -06001730 if (req->flags & REQ_F_LINK)
1731 req->result = ret;
1732
Jens Axboe31b51512019-01-18 22:56:34 -07001733 iov_count = iov_iter_count(&iter);
1734
1735 ret = -EAGAIN;
Jens Axboe561fb042019-10-24 07:25:42 -06001736 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT))
Jens Axboe31b51512019-01-18 22:56:34 -07001737 goto out_free;
Jens Axboe31b51512019-01-18 22:56:34 -07001738
1739 ret = rw_verify_area(WRITE, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001740 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01001741 ssize_t ret2;
1742
Jens Axboe2b188cc2019-01-07 10:46:33 -07001743 /*
1744 * Open-code file_start_write here to grab freeze protection,
1745 * which will be released by another thread in
1746 * io_complete_rw(). Fool lockdep by telling it the lock got
1747 * released so that it doesn't complain about the held lock when
1748 * we return to userspace.
1749 */
Jens Axboe491381ce2019-10-17 09:20:46 -06001750 if (req->flags & REQ_F_ISREG) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001751 __sb_start_write(file_inode(file)->i_sb,
1752 SB_FREEZE_WRITE, true);
1753 __sb_writers_release(file_inode(file)->i_sb,
1754 SB_FREEZE_WRITE);
1755 }
1756 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01001757
Jens Axboe32960612019-09-23 11:05:34 -06001758 if (file->f_op->write_iter)
1759 ret2 = call_write_iter(file, kiocb, &iter);
1760 else
1761 ret2 = loop_rw_iter(WRITE, file, kiocb, &iter);
Jens Axboe561fb042019-10-24 07:25:42 -06001762 if (!force_nonblock || ret2 != -EAGAIN)
Pavel Begunkov267bc902019-11-07 01:41:08 +03001763 kiocb_done(kiocb, ret2, nxt, req->submit.in_async);
Jens Axboe561fb042019-10-24 07:25:42 -06001764 else
Roman Penyaev9bf79332019-03-25 20:09:24 +01001765 ret = -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001766 }
Jens Axboe31b51512019-01-18 22:56:34 -07001767out_free:
Jens Axboe2b188cc2019-01-07 10:46:33 -07001768 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001769 return ret;
1770}
1771
1772/*
1773 * IORING_OP_NOP just posts a completion event, nothing else.
1774 */
Jens Axboe78e19bb2019-11-06 15:21:34 -07001775static int io_nop(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001776{
1777 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001778
Jens Axboedef596e2019-01-09 08:59:42 -07001779 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1780 return -EINVAL;
1781
Jens Axboe78e19bb2019-11-06 15:21:34 -07001782 io_cqring_add_event(req, 0);
Jens Axboee65ef562019-03-12 10:16:44 -06001783 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001784 return 0;
1785}
1786
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001787static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1788{
Jens Axboe6b063142019-01-10 22:13:58 -07001789 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001790
Jens Axboe09bb8392019-03-13 12:39:28 -06001791 if (!req->file)
1792 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001793
Jens Axboe6b063142019-01-10 22:13:58 -07001794 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07001795 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07001796 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001797 return -EINVAL;
1798
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001799 return 0;
1800}
1801
1802static int io_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001803 struct io_kiocb **nxt, bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001804{
1805 loff_t sqe_off = READ_ONCE(sqe->off);
1806 loff_t sqe_len = READ_ONCE(sqe->len);
1807 loff_t end = sqe_off + sqe_len;
1808 unsigned fsync_flags;
1809 int ret;
1810
1811 fsync_flags = READ_ONCE(sqe->fsync_flags);
1812 if (unlikely(fsync_flags & ~IORING_FSYNC_DATASYNC))
1813 return -EINVAL;
1814
1815 ret = io_prep_fsync(req, sqe);
1816 if (ret)
1817 return ret;
1818
1819 /* fsync always requires a blocking context */
1820 if (force_nonblock)
1821 return -EAGAIN;
1822
1823 ret = vfs_fsync_range(req->rw.ki_filp, sqe_off,
1824 end > 0 ? end : LLONG_MAX,
1825 fsync_flags & IORING_FSYNC_DATASYNC);
1826
Jens Axboe9e645e112019-05-10 16:07:28 -06001827 if (ret < 0 && (req->flags & REQ_F_LINK))
1828 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001829 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001830 io_put_req_find_next(req, nxt);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001831 return 0;
1832}
1833
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001834static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1835{
1836 struct io_ring_ctx *ctx = req->ctx;
1837 int ret = 0;
1838
1839 if (!req->file)
1840 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001841
1842 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1843 return -EINVAL;
1844 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
1845 return -EINVAL;
1846
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001847 return ret;
1848}
1849
1850static int io_sync_file_range(struct io_kiocb *req,
1851 const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001852 struct io_kiocb **nxt,
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001853 bool force_nonblock)
1854{
1855 loff_t sqe_off;
1856 loff_t sqe_len;
1857 unsigned flags;
1858 int ret;
1859
1860 ret = io_prep_sfr(req, sqe);
1861 if (ret)
1862 return ret;
1863
1864 /* sync_file_range always requires a blocking context */
1865 if (force_nonblock)
1866 return -EAGAIN;
1867
1868 sqe_off = READ_ONCE(sqe->off);
1869 sqe_len = READ_ONCE(sqe->len);
1870 flags = READ_ONCE(sqe->sync_range_flags);
1871
1872 ret = sync_file_range(req->rw.ki_filp, sqe_off, sqe_len, flags);
1873
Jens Axboe9e645e112019-05-10 16:07:28 -06001874 if (ret < 0 && (req->flags & REQ_F_LINK))
1875 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001876 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001877 io_put_req_find_next(req, nxt);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001878 return 0;
1879}
1880
Jens Axboe0fa03c62019-04-19 13:34:07 -06001881#if defined(CONFIG_NET)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001882static int io_send_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001883 struct io_kiocb **nxt, bool force_nonblock,
Jens Axboeaa1fa282019-04-19 13:38:09 -06001884 long (*fn)(struct socket *, struct user_msghdr __user *,
1885 unsigned int))
1886{
Jens Axboe0fa03c62019-04-19 13:34:07 -06001887 struct socket *sock;
1888 int ret;
1889
1890 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1891 return -EINVAL;
1892
1893 sock = sock_from_file(req->file, &ret);
1894 if (sock) {
1895 struct user_msghdr __user *msg;
1896 unsigned flags;
1897
1898 flags = READ_ONCE(sqe->msg_flags);
1899 if (flags & MSG_DONTWAIT)
1900 req->flags |= REQ_F_NOWAIT;
1901 else if (force_nonblock)
1902 flags |= MSG_DONTWAIT;
1903
1904 msg = (struct user_msghdr __user *) (unsigned long)
1905 READ_ONCE(sqe->addr);
1906
Jens Axboeaa1fa282019-04-19 13:38:09 -06001907 ret = fn(sock, msg, flags);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001908 if (force_nonblock && ret == -EAGAIN)
1909 return ret;
1910 }
1911
Jens Axboe78e19bb2019-11-06 15:21:34 -07001912 io_cqring_add_event(req, ret);
Jens Axboef1f40852019-11-05 20:33:16 -07001913 if (ret < 0 && (req->flags & REQ_F_LINK))
1914 req->flags |= REQ_F_FAIL_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001915 io_put_req_find_next(req, nxt);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001916 return 0;
Jens Axboeaa1fa282019-04-19 13:38:09 -06001917}
1918#endif
1919
1920static int io_sendmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001921 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001922{
1923#if defined(CONFIG_NET)
Jens Axboeba816ad2019-09-28 11:36:45 -06001924 return io_send_recvmsg(req, sqe, nxt, force_nonblock,
1925 __sys_sendmsg_sock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06001926#else
1927 return -EOPNOTSUPP;
1928#endif
1929}
1930
1931static int io_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001932 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001933{
1934#if defined(CONFIG_NET)
Jens Axboeba816ad2019-09-28 11:36:45 -06001935 return io_send_recvmsg(req, sqe, nxt, force_nonblock,
1936 __sys_recvmsg_sock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001937#else
1938 return -EOPNOTSUPP;
1939#endif
1940}
1941
Jens Axboe17f2fe32019-10-17 14:42:58 -06001942static int io_accept(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1943 struct io_kiocb **nxt, bool force_nonblock)
1944{
1945#if defined(CONFIG_NET)
1946 struct sockaddr __user *addr;
1947 int __user *addr_len;
1948 unsigned file_flags;
1949 int flags, ret;
1950
1951 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
1952 return -EINVAL;
1953 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
1954 return -EINVAL;
1955
1956 addr = (struct sockaddr __user *) (unsigned long) READ_ONCE(sqe->addr);
1957 addr_len = (int __user *) (unsigned long) READ_ONCE(sqe->addr2);
1958 flags = READ_ONCE(sqe->accept_flags);
1959 file_flags = force_nonblock ? O_NONBLOCK : 0;
1960
1961 ret = __sys_accept4_file(req->file, file_flags, addr, addr_len, flags);
1962 if (ret == -EAGAIN && force_nonblock) {
1963 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
1964 return -EAGAIN;
1965 }
Jens Axboe8e3cca12019-11-09 19:52:33 -07001966 if (ret == -ERESTARTSYS)
1967 ret = -EINTR;
Jens Axboe17f2fe32019-10-17 14:42:58 -06001968 if (ret < 0 && (req->flags & REQ_F_LINK))
1969 req->flags |= REQ_F_FAIL_LINK;
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);
Jens Axboe17f2fe32019-10-17 14:42:58 -06001972 return 0;
1973#else
1974 return -EOPNOTSUPP;
1975#endif
1976}
1977
Jens Axboef8e85cf2019-11-23 14:24:24 -07001978static int io_connect(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1979 struct io_kiocb **nxt, bool force_nonblock)
1980{
1981#if defined(CONFIG_NET)
1982 struct sockaddr __user *addr;
1983 unsigned file_flags;
1984 int addr_len, ret;
1985
1986 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
1987 return -EINVAL;
1988 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
1989 return -EINVAL;
1990
1991 addr = (struct sockaddr __user *) (unsigned long) READ_ONCE(sqe->addr);
1992 addr_len = READ_ONCE(sqe->addr2);
1993 file_flags = force_nonblock ? O_NONBLOCK : 0;
1994
1995 ret = __sys_connect_file(req->file, addr, addr_len, file_flags);
1996 if (ret == -EAGAIN && force_nonblock)
1997 return -EAGAIN;
1998 if (ret == -ERESTARTSYS)
1999 ret = -EINTR;
2000 if (ret < 0 && (req->flags & REQ_F_LINK))
2001 req->flags |= REQ_F_FAIL_LINK;
2002 io_cqring_add_event(req, ret);
2003 io_put_req_find_next(req, nxt);
2004 return 0;
2005#else
2006 return -EOPNOTSUPP;
2007#endif
2008}
2009
Jens Axboeeac406c2019-11-14 12:09:58 -07002010static inline void io_poll_remove_req(struct io_kiocb *req)
2011{
2012 if (!RB_EMPTY_NODE(&req->rb_node)) {
2013 rb_erase(&req->rb_node, &req->ctx->cancel_tree);
2014 RB_CLEAR_NODE(&req->rb_node);
2015 }
2016}
2017
Jens Axboe221c5eb2019-01-17 09:41:58 -07002018static void io_poll_remove_one(struct io_kiocb *req)
2019{
2020 struct io_poll_iocb *poll = &req->poll;
2021
2022 spin_lock(&poll->head->lock);
2023 WRITE_ONCE(poll->canceled, true);
2024 if (!list_empty(&poll->wait.entry)) {
2025 list_del_init(&poll->wait.entry);
Jackie Liua197f662019-11-08 08:09:12 -07002026 io_queue_async_work(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002027 }
2028 spin_unlock(&poll->head->lock);
Jens Axboeeac406c2019-11-14 12:09:58 -07002029 io_poll_remove_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002030}
2031
2032static void io_poll_remove_all(struct io_ring_ctx *ctx)
2033{
Jens Axboeeac406c2019-11-14 12:09:58 -07002034 struct rb_node *node;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002035 struct io_kiocb *req;
2036
2037 spin_lock_irq(&ctx->completion_lock);
Jens Axboeeac406c2019-11-14 12:09:58 -07002038 while ((node = rb_first(&ctx->cancel_tree)) != NULL) {
2039 req = rb_entry(node, struct io_kiocb, rb_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002040 io_poll_remove_one(req);
2041 }
2042 spin_unlock_irq(&ctx->completion_lock);
2043}
2044
Jens Axboe47f46762019-11-09 17:43:02 -07002045static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
2046{
Jens Axboeeac406c2019-11-14 12:09:58 -07002047 struct rb_node *p, *parent = NULL;
Jens Axboe47f46762019-11-09 17:43:02 -07002048 struct io_kiocb *req;
2049
Jens Axboeeac406c2019-11-14 12:09:58 -07002050 p = ctx->cancel_tree.rb_node;
2051 while (p) {
2052 parent = p;
2053 req = rb_entry(parent, struct io_kiocb, rb_node);
2054 if (sqe_addr < req->user_data) {
2055 p = p->rb_left;
2056 } else if (sqe_addr > req->user_data) {
2057 p = p->rb_right;
2058 } else {
2059 io_poll_remove_one(req);
2060 return 0;
2061 }
Jens Axboe47f46762019-11-09 17:43:02 -07002062 }
2063
2064 return -ENOENT;
2065}
2066
Jens Axboe221c5eb2019-01-17 09:41:58 -07002067/*
2068 * Find a running poll command that matches one specified in sqe->addr,
2069 * and remove it if found.
2070 */
2071static int io_poll_remove(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2072{
2073 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07002074 int ret;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002075
2076 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2077 return -EINVAL;
2078 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
2079 sqe->poll_events)
2080 return -EINVAL;
2081
2082 spin_lock_irq(&ctx->completion_lock);
Jens Axboe47f46762019-11-09 17:43:02 -07002083 ret = io_poll_cancel(ctx, READ_ONCE(sqe->addr));
Jens Axboe221c5eb2019-01-17 09:41:58 -07002084 spin_unlock_irq(&ctx->completion_lock);
2085
Jens Axboe78e19bb2019-11-06 15:21:34 -07002086 io_cqring_add_event(req, ret);
Jens Axboef1f40852019-11-05 20:33:16 -07002087 if (ret < 0 && (req->flags & REQ_F_LINK))
2088 req->flags |= REQ_F_FAIL_LINK;
Jens Axboee65ef562019-03-12 10:16:44 -06002089 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002090 return 0;
2091}
2092
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002093static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002094{
Jackie Liua197f662019-11-08 08:09:12 -07002095 struct io_ring_ctx *ctx = req->ctx;
2096
Jens Axboe8c838782019-03-12 15:48:16 -06002097 req->poll.done = true;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002098 if (error)
2099 io_cqring_fill_event(req, error);
2100 else
2101 io_cqring_fill_event(req, mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06002102 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002103}
2104
Jens Axboe561fb042019-10-24 07:25:42 -06002105static void io_poll_complete_work(struct io_wq_work **workptr)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002106{
Jens Axboe561fb042019-10-24 07:25:42 -06002107 struct io_wq_work *work = *workptr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002108 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
2109 struct io_poll_iocb *poll = &req->poll;
2110 struct poll_table_struct pt = { ._key = poll->events };
2111 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe89723d02019-11-05 15:32:58 -07002112 struct io_kiocb *nxt = NULL;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002113 __poll_t mask = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002114 int ret = 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002115
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002116 if (work->flags & IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06002117 WRITE_ONCE(poll->canceled, true);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002118 ret = -ECANCELED;
2119 } else if (READ_ONCE(poll->canceled)) {
2120 ret = -ECANCELED;
2121 }
Jens Axboe561fb042019-10-24 07:25:42 -06002122
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002123 if (ret != -ECANCELED)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002124 mask = vfs_poll(poll->file, &pt) & poll->events;
2125
2126 /*
2127 * Note that ->ki_cancel callers also delete iocb from active_reqs after
2128 * calling ->ki_cancel. We need the ctx_lock roundtrip here to
2129 * synchronize with them. In the cancellation case the list_del_init
2130 * itself is not actually needed, but harmless so we keep it in to
2131 * avoid further branches in the fast path.
2132 */
2133 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002134 if (!mask && ret != -ECANCELED) {
Jens Axboe221c5eb2019-01-17 09:41:58 -07002135 add_wait_queue(poll->head, &poll->wait);
2136 spin_unlock_irq(&ctx->completion_lock);
2137 return;
2138 }
Jens Axboeeac406c2019-11-14 12:09:58 -07002139 io_poll_remove_req(req);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002140 io_poll_complete(req, mask, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002141 spin_unlock_irq(&ctx->completion_lock);
2142
Jens Axboe8c838782019-03-12 15:48:16 -06002143 io_cqring_ev_posted(ctx);
Jens Axboe89723d02019-11-05 15:32:58 -07002144
Jens Axboefba38c22019-11-18 12:27:57 -07002145 if (ret < 0 && req->flags & REQ_F_LINK)
2146 req->flags |= REQ_F_FAIL_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08002147 io_put_req_find_next(req, &nxt);
Jens Axboe89723d02019-11-05 15:32:58 -07002148 if (nxt)
2149 *workptr = &nxt->work;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002150}
2151
2152static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
2153 void *key)
2154{
2155 struct io_poll_iocb *poll = container_of(wait, struct io_poll_iocb,
2156 wait);
2157 struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
2158 struct io_ring_ctx *ctx = req->ctx;
2159 __poll_t mask = key_to_poll(key);
Jens Axboe8c838782019-03-12 15:48:16 -06002160 unsigned long flags;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002161
2162 /* for instances that support it check for an event match first: */
Jens Axboe8c838782019-03-12 15:48:16 -06002163 if (mask && !(mask & poll->events))
2164 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002165
2166 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06002167
Jens Axboe7c9e7f02019-11-12 08:15:53 -07002168 /*
2169 * Run completion inline if we can. We're using trylock here because
2170 * we are violating the completion_lock -> poll wq lock ordering.
2171 * If we have a link timeout we're going to need the completion_lock
2172 * for finalizing the request, mark us as having grabbed that already.
2173 */
Jens Axboe8c838782019-03-12 15:48:16 -06002174 if (mask && spin_trylock_irqsave(&ctx->completion_lock, flags)) {
Jens Axboeeac406c2019-11-14 12:09:58 -07002175 io_poll_remove_req(req);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002176 io_poll_complete(req, mask, 0);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07002177 req->flags |= REQ_F_COMP_LOCKED;
2178 io_put_req(req);
Jens Axboe8c838782019-03-12 15:48:16 -06002179 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2180
2181 io_cqring_ev_posted(ctx);
Jens Axboe8c838782019-03-12 15:48:16 -06002182 } else {
Jackie Liua197f662019-11-08 08:09:12 -07002183 io_queue_async_work(req);
Jens Axboe8c838782019-03-12 15:48:16 -06002184 }
2185
Jens Axboe221c5eb2019-01-17 09:41:58 -07002186 return 1;
2187}
2188
2189struct io_poll_table {
2190 struct poll_table_struct pt;
2191 struct io_kiocb *req;
2192 int error;
2193};
2194
2195static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
2196 struct poll_table_struct *p)
2197{
2198 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
2199
2200 if (unlikely(pt->req->poll.head)) {
2201 pt->error = -EINVAL;
2202 return;
2203 }
2204
2205 pt->error = 0;
2206 pt->req->poll.head = head;
2207 add_wait_queue(head, &pt->req->poll.wait);
2208}
2209
Jens Axboeeac406c2019-11-14 12:09:58 -07002210static void io_poll_req_insert(struct io_kiocb *req)
2211{
2212 struct io_ring_ctx *ctx = req->ctx;
2213 struct rb_node **p = &ctx->cancel_tree.rb_node;
2214 struct rb_node *parent = NULL;
2215 struct io_kiocb *tmp;
2216
2217 while (*p) {
2218 parent = *p;
2219 tmp = rb_entry(parent, struct io_kiocb, rb_node);
2220 if (req->user_data < tmp->user_data)
2221 p = &(*p)->rb_left;
2222 else
2223 p = &(*p)->rb_right;
2224 }
2225 rb_link_node(&req->rb_node, parent, p);
2226 rb_insert_color(&req->rb_node, &ctx->cancel_tree);
2227}
2228
Jens Axboe89723d02019-11-05 15:32:58 -07002229static int io_poll_add(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2230 struct io_kiocb **nxt)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002231{
2232 struct io_poll_iocb *poll = &req->poll;
2233 struct io_ring_ctx *ctx = req->ctx;
2234 struct io_poll_table ipt;
Jens Axboe8c838782019-03-12 15:48:16 -06002235 bool cancel = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002236 __poll_t mask;
2237 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002238
2239 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2240 return -EINVAL;
2241 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
2242 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06002243 if (!poll->file)
2244 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002245
Jens Axboe6cc47d12019-09-18 11:18:23 -06002246 req->submit.sqe = NULL;
Jens Axboe561fb042019-10-24 07:25:42 -06002247 INIT_IO_WORK(&req->work, io_poll_complete_work);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002248 events = READ_ONCE(sqe->poll_events);
2249 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
Jens Axboeeac406c2019-11-14 12:09:58 -07002250 RB_CLEAR_NODE(&req->rb_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002251
Jens Axboe221c5eb2019-01-17 09:41:58 -07002252 poll->head = NULL;
Jens Axboe8c838782019-03-12 15:48:16 -06002253 poll->done = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002254 poll->canceled = false;
2255
2256 ipt.pt._qproc = io_poll_queue_proc;
2257 ipt.pt._key = poll->events;
2258 ipt.req = req;
2259 ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
2260
2261 /* initialized the list so that we can do list_empty checks */
2262 INIT_LIST_HEAD(&poll->wait.entry);
2263 init_waitqueue_func_entry(&poll->wait, io_poll_wake);
2264
Jens Axboe36703242019-07-25 10:20:18 -06002265 INIT_LIST_HEAD(&req->list);
2266
Jens Axboe221c5eb2019-01-17 09:41:58 -07002267 mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002268
2269 spin_lock_irq(&ctx->completion_lock);
Jens Axboe8c838782019-03-12 15:48:16 -06002270 if (likely(poll->head)) {
2271 spin_lock(&poll->head->lock);
2272 if (unlikely(list_empty(&poll->wait.entry))) {
2273 if (ipt.error)
2274 cancel = true;
2275 ipt.error = 0;
2276 mask = 0;
2277 }
2278 if (mask || ipt.error)
2279 list_del_init(&poll->wait.entry);
2280 else if (cancel)
2281 WRITE_ONCE(poll->canceled, true);
2282 else if (!poll->done) /* actually waiting for an event */
Jens Axboeeac406c2019-11-14 12:09:58 -07002283 io_poll_req_insert(req);
Jens Axboe8c838782019-03-12 15:48:16 -06002284 spin_unlock(&poll->head->lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002285 }
Jens Axboe8c838782019-03-12 15:48:16 -06002286 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06002287 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002288 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06002289 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07002290 spin_unlock_irq(&ctx->completion_lock);
2291
Jens Axboe8c838782019-03-12 15:48:16 -06002292 if (mask) {
2293 io_cqring_ev_posted(ctx);
Jackie Liuec9c02a2019-11-08 23:50:36 +08002294 io_put_req_find_next(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002295 }
Jens Axboe8c838782019-03-12 15:48:16 -06002296 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002297}
2298
Jens Axboe5262f562019-09-17 12:26:57 -06002299static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
2300{
Jens Axboead8a48a2019-11-15 08:49:11 -07002301 struct io_timeout_data *data = container_of(timer,
2302 struct io_timeout_data, timer);
2303 struct io_kiocb *req = data->req;
2304 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06002305 unsigned long flags;
2306
Jens Axboe5262f562019-09-17 12:26:57 -06002307 atomic_inc(&ctx->cq_timeouts);
2308
2309 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08002310 /*
Jens Axboe11365042019-10-16 09:08:32 -06002311 * We could be racing with timeout deletion. If the list is empty,
2312 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08002313 */
Jens Axboe842f9612019-10-29 12:34:10 -06002314 if (!list_empty(&req->list)) {
Jens Axboe11365042019-10-16 09:08:32 -06002315 struct io_kiocb *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06002316
Jens Axboe11365042019-10-16 09:08:32 -06002317 /*
2318 * Adjust the reqs sequence before the current one because it
2319 * will consume a slot in the cq_ring and the the cq_tail
2320 * pointer will be increased, otherwise other timeout reqs may
2321 * return in advance without waiting for enough wait_nr.
2322 */
2323 prev = req;
2324 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
2325 prev->sequence++;
Jens Axboe11365042019-10-16 09:08:32 -06002326 list_del_init(&req->list);
Jens Axboe11365042019-10-16 09:08:32 -06002327 }
Jens Axboe842f9612019-10-29 12:34:10 -06002328
Jens Axboe78e19bb2019-11-06 15:21:34 -07002329 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06002330 io_commit_cqring(ctx);
2331 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2332
2333 io_cqring_ev_posted(ctx);
Jens Axboef1f40852019-11-05 20:33:16 -07002334 if (req->flags & REQ_F_LINK)
2335 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe5262f562019-09-17 12:26:57 -06002336 io_put_req(req);
2337 return HRTIMER_NORESTART;
2338}
2339
Jens Axboe47f46762019-11-09 17:43:02 -07002340static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
2341{
2342 struct io_kiocb *req;
2343 int ret = -ENOENT;
2344
2345 list_for_each_entry(req, &ctx->timeout_list, list) {
2346 if (user_data == req->user_data) {
2347 list_del_init(&req->list);
2348 ret = 0;
2349 break;
2350 }
2351 }
2352
2353 if (ret == -ENOENT)
2354 return ret;
2355
Jens Axboead8a48a2019-11-15 08:49:11 -07002356 ret = hrtimer_try_to_cancel(&req->timeout.data->timer);
Jens Axboe47f46762019-11-09 17:43:02 -07002357 if (ret == -1)
2358 return -EALREADY;
2359
Jens Axboefba38c22019-11-18 12:27:57 -07002360 if (req->flags & REQ_F_LINK)
2361 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe47f46762019-11-09 17:43:02 -07002362 io_cqring_fill_event(req, -ECANCELED);
2363 io_put_req(req);
2364 return 0;
2365}
2366
Jens Axboe11365042019-10-16 09:08:32 -06002367/*
2368 * Remove or update an existing timeout command
2369 */
2370static int io_timeout_remove(struct io_kiocb *req,
2371 const struct io_uring_sqe *sqe)
2372{
2373 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe11365042019-10-16 09:08:32 -06002374 unsigned flags;
Jens Axboe47f46762019-11-09 17:43:02 -07002375 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06002376
2377 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2378 return -EINVAL;
2379 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
2380 return -EINVAL;
2381 flags = READ_ONCE(sqe->timeout_flags);
2382 if (flags)
2383 return -EINVAL;
2384
Jens Axboe11365042019-10-16 09:08:32 -06002385 spin_lock_irq(&ctx->completion_lock);
Jens Axboe47f46762019-11-09 17:43:02 -07002386 ret = io_timeout_cancel(ctx, READ_ONCE(sqe->addr));
Jens Axboe11365042019-10-16 09:08:32 -06002387
Jens Axboe47f46762019-11-09 17:43:02 -07002388 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06002389 io_commit_cqring(ctx);
2390 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06002391 io_cqring_ev_posted(ctx);
Jens Axboe47f46762019-11-09 17:43:02 -07002392 if (ret < 0 && req->flags & REQ_F_LINK)
2393 req->flags |= REQ_F_FAIL_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08002394 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06002395 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06002396}
2397
Jens Axboead8a48a2019-11-15 08:49:11 -07002398static int io_timeout_setup(struct io_kiocb *req)
Jens Axboe5262f562019-09-17 12:26:57 -06002399{
Jens Axboead8a48a2019-11-15 08:49:11 -07002400 const struct io_uring_sqe *sqe = req->submit.sqe;
2401 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06002402 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06002403
Jens Axboead8a48a2019-11-15 08:49:11 -07002404 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06002405 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07002406 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06002407 return -EINVAL;
2408 flags = READ_ONCE(sqe->timeout_flags);
2409 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06002410 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06002411
Jens Axboead8a48a2019-11-15 08:49:11 -07002412 data = kzalloc(sizeof(struct io_timeout_data), GFP_KERNEL);
2413 if (!data)
2414 return -ENOMEM;
2415 data->req = req;
2416 req->timeout.data = data;
2417 req->flags |= REQ_F_TIMEOUT;
2418
2419 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06002420 return -EFAULT;
2421
Jens Axboe11365042019-10-16 09:08:32 -06002422 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07002423 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06002424 else
Jens Axboead8a48a2019-11-15 08:49:11 -07002425 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06002426
Jens Axboead8a48a2019-11-15 08:49:11 -07002427 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
2428 return 0;
2429}
2430
2431static int io_timeout(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2432{
2433 unsigned count;
2434 struct io_ring_ctx *ctx = req->ctx;
2435 struct io_timeout_data *data;
2436 struct list_head *entry;
2437 unsigned span = 0;
2438 int ret;
2439
2440 ret = io_timeout_setup(req);
2441 /* common setup allows flags (like links) set, we don't */
2442 if (!ret && sqe->flags)
2443 ret = -EINVAL;
2444 if (ret)
2445 return ret;
Jens Axboe93bd25b2019-11-11 23:34:31 -07002446
Jens Axboe5262f562019-09-17 12:26:57 -06002447 /*
2448 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07002449 * timeout event to be satisfied. If it isn't set, then this is
2450 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06002451 */
2452 count = READ_ONCE(sqe->off);
Jens Axboe93bd25b2019-11-11 23:34:31 -07002453 if (!count) {
2454 req->flags |= REQ_F_TIMEOUT_NOSEQ;
2455 spin_lock_irq(&ctx->completion_lock);
2456 entry = ctx->timeout_list.prev;
2457 goto add;
2458 }
Jens Axboe5262f562019-09-17 12:26:57 -06002459
2460 req->sequence = ctx->cached_sq_head + count - 1;
yangerkun5da0fb12019-10-15 21:59:29 +08002461 /* reuse it to store the count */
2462 req->submit.sequence = count;
Jens Axboe5262f562019-09-17 12:26:57 -06002463
2464 /*
2465 * Insertion sort, ensuring the first entry in the list is always
2466 * the one we need first.
2467 */
Jens Axboe5262f562019-09-17 12:26:57 -06002468 spin_lock_irq(&ctx->completion_lock);
2469 list_for_each_prev(entry, &ctx->timeout_list) {
2470 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
yangerkun5da0fb12019-10-15 21:59:29 +08002471 unsigned nxt_sq_head;
2472 long long tmp, tmp_nxt;
Jens Axboe5262f562019-09-17 12:26:57 -06002473
Jens Axboe93bd25b2019-11-11 23:34:31 -07002474 if (nxt->flags & REQ_F_TIMEOUT_NOSEQ)
2475 continue;
2476
yangerkun5da0fb12019-10-15 21:59:29 +08002477 /*
2478 * Since cached_sq_head + count - 1 can overflow, use type long
2479 * long to store it.
2480 */
2481 tmp = (long long)ctx->cached_sq_head + count - 1;
2482 nxt_sq_head = nxt->sequence - nxt->submit.sequence + 1;
2483 tmp_nxt = (long long)nxt_sq_head + nxt->submit.sequence - 1;
2484
2485 /*
2486 * cached_sq_head may overflow, and it will never overflow twice
2487 * once there is some timeout req still be valid.
2488 */
2489 if (ctx->cached_sq_head < nxt_sq_head)
yangerkun8b07a652019-10-17 12:12:35 +08002490 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08002491
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002492 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06002493 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002494
2495 /*
2496 * Sequence of reqs after the insert one and itself should
2497 * be adjusted because each timeout req consumes a slot.
2498 */
2499 span++;
2500 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06002501 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002502 req->sequence -= span;
Jens Axboe93bd25b2019-11-11 23:34:31 -07002503add:
Jens Axboe5262f562019-09-17 12:26:57 -06002504 list_add(&req->list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07002505 data = req->timeout.data;
2506 data->timer.function = io_timeout_fn;
2507 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06002508 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06002509 return 0;
2510}
2511
Jens Axboe62755e32019-10-28 21:49:21 -06002512static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06002513{
Jens Axboe62755e32019-10-28 21:49:21 -06002514 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06002515
Jens Axboe62755e32019-10-28 21:49:21 -06002516 return req->user_data == (unsigned long) data;
2517}
2518
Jens Axboee977d6d2019-11-05 12:39:45 -07002519static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06002520{
Jens Axboe62755e32019-10-28 21:49:21 -06002521 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06002522 int ret = 0;
2523
Jens Axboe62755e32019-10-28 21:49:21 -06002524 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
2525 switch (cancel_ret) {
2526 case IO_WQ_CANCEL_OK:
2527 ret = 0;
2528 break;
2529 case IO_WQ_CANCEL_RUNNING:
2530 ret = -EALREADY;
2531 break;
2532 case IO_WQ_CANCEL_NOTFOUND:
2533 ret = -ENOENT;
2534 break;
2535 }
2536
Jens Axboee977d6d2019-11-05 12:39:45 -07002537 return ret;
2538}
2539
Jens Axboe47f46762019-11-09 17:43:02 -07002540static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
2541 struct io_kiocb *req, __u64 sqe_addr,
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002542 struct io_kiocb **nxt, int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07002543{
2544 unsigned long flags;
2545 int ret;
2546
2547 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
2548 if (ret != -ENOENT) {
2549 spin_lock_irqsave(&ctx->completion_lock, flags);
2550 goto done;
2551 }
2552
2553 spin_lock_irqsave(&ctx->completion_lock, flags);
2554 ret = io_timeout_cancel(ctx, sqe_addr);
2555 if (ret != -ENOENT)
2556 goto done;
2557 ret = io_poll_cancel(ctx, sqe_addr);
2558done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002559 if (!ret)
2560 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07002561 io_cqring_fill_event(req, ret);
2562 io_commit_cqring(ctx);
2563 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2564 io_cqring_ev_posted(ctx);
2565
2566 if (ret < 0 && (req->flags & REQ_F_LINK))
2567 req->flags |= REQ_F_FAIL_LINK;
2568 io_put_req_find_next(req, nxt);
2569}
2570
Jens Axboee977d6d2019-11-05 12:39:45 -07002571static int io_async_cancel(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2572 struct io_kiocb **nxt)
2573{
2574 struct io_ring_ctx *ctx = req->ctx;
Jens Axboee977d6d2019-11-05 12:39:45 -07002575
2576 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2577 return -EINVAL;
2578 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
2579 sqe->cancel_flags)
2580 return -EINVAL;
2581
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002582 io_async_find_and_cancel(ctx, req, READ_ONCE(sqe->addr), nxt, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06002583 return 0;
2584}
2585
Jackie Liua197f662019-11-08 08:09:12 -07002586static int io_req_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -06002587{
Pavel Begunkov267bc902019-11-07 01:41:08 +03002588 const struct io_uring_sqe *sqe = req->submit.sqe;
Jens Axboede0617e2019-04-06 21:51:27 -06002589 struct io_uring_sqe *sqe_copy;
Jackie Liua197f662019-11-08 08:09:12 -07002590 struct io_ring_ctx *ctx = req->ctx;
Jens Axboede0617e2019-04-06 21:51:27 -06002591
Bob Liu9d858b22019-11-13 18:06:25 +08002592 /* Still need defer if there is pending req in defer list. */
2593 if (!req_need_defer(req) && list_empty(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06002594 return 0;
2595
2596 sqe_copy = kmalloc(sizeof(*sqe_copy), GFP_KERNEL);
2597 if (!sqe_copy)
2598 return -EAGAIN;
2599
2600 spin_lock_irq(&ctx->completion_lock);
Bob Liu9d858b22019-11-13 18:06:25 +08002601 if (!req_need_defer(req) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06002602 spin_unlock_irq(&ctx->completion_lock);
2603 kfree(sqe_copy);
2604 return 0;
2605 }
2606
2607 memcpy(sqe_copy, sqe, sizeof(*sqe_copy));
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03002608 req->flags |= REQ_F_FREE_SQE;
Jens Axboede0617e2019-04-06 21:51:27 -06002609 req->submit.sqe = sqe_copy;
2610
Jens Axboe915967f2019-11-21 09:01:20 -07002611 trace_io_uring_defer(ctx, req, req->user_data);
Jens Axboede0617e2019-04-06 21:51:27 -06002612 list_add_tail(&req->list, &ctx->defer_list);
2613 spin_unlock_irq(&ctx->completion_lock);
2614 return -EIOCBQUEUED;
2615}
2616
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03002617__attribute__((nonnull))
Pavel Begunkovd7324472019-11-21 21:24:36 +03002618static int io_issue_sqe(struct io_kiocb *req, struct io_kiocb **nxt,
2619 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002620{
Jens Axboee0c5c572019-03-12 10:18:47 -06002621 int ret, opcode;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002622 struct sqe_submit *s = &req->submit;
Jackie Liua197f662019-11-08 08:09:12 -07002623 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002624
2625 opcode = READ_ONCE(s->sqe->opcode);
2626 switch (opcode) {
2627 case IORING_OP_NOP:
Jens Axboe78e19bb2019-11-06 15:21:34 -07002628 ret = io_nop(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002629 break;
2630 case IORING_OP_READV:
Jens Axboeedafcce2019-01-09 09:16:05 -07002631 if (unlikely(s->sqe->buf_index))
2632 return -EINVAL;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002633 ret = io_read(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002634 break;
2635 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07002636 if (unlikely(s->sqe->buf_index))
2637 return -EINVAL;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002638 ret = io_write(req, nxt, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07002639 break;
2640 case IORING_OP_READ_FIXED:
Pavel Begunkov267bc902019-11-07 01:41:08 +03002641 ret = io_read(req, nxt, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07002642 break;
2643 case IORING_OP_WRITE_FIXED:
Pavel Begunkov267bc902019-11-07 01:41:08 +03002644 ret = io_write(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002645 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002646 case IORING_OP_FSYNC:
Jens Axboeba816ad2019-09-28 11:36:45 -06002647 ret = io_fsync(req, s->sqe, nxt, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002648 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002649 case IORING_OP_POLL_ADD:
Jens Axboe89723d02019-11-05 15:32:58 -07002650 ret = io_poll_add(req, s->sqe, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002651 break;
2652 case IORING_OP_POLL_REMOVE:
2653 ret = io_poll_remove(req, s->sqe);
2654 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002655 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboeba816ad2019-09-28 11:36:45 -06002656 ret = io_sync_file_range(req, s->sqe, nxt, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002657 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002658 case IORING_OP_SENDMSG:
Jens Axboeba816ad2019-09-28 11:36:45 -06002659 ret = io_sendmsg(req, s->sqe, nxt, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06002660 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06002661 case IORING_OP_RECVMSG:
Jens Axboeba816ad2019-09-28 11:36:45 -06002662 ret = io_recvmsg(req, s->sqe, nxt, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06002663 break;
Jens Axboe5262f562019-09-17 12:26:57 -06002664 case IORING_OP_TIMEOUT:
2665 ret = io_timeout(req, s->sqe);
2666 break;
Jens Axboe11365042019-10-16 09:08:32 -06002667 case IORING_OP_TIMEOUT_REMOVE:
2668 ret = io_timeout_remove(req, s->sqe);
2669 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06002670 case IORING_OP_ACCEPT:
2671 ret = io_accept(req, s->sqe, nxt, force_nonblock);
2672 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07002673 case IORING_OP_CONNECT:
2674 ret = io_connect(req, s->sqe, nxt, force_nonblock);
2675 break;
Jens Axboe62755e32019-10-28 21:49:21 -06002676 case IORING_OP_ASYNC_CANCEL:
2677 ret = io_async_cancel(req, s->sqe, nxt);
2678 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002679 default:
2680 ret = -EINVAL;
2681 break;
2682 }
2683
Jens Axboedef596e2019-01-09 08:59:42 -07002684 if (ret)
2685 return ret;
2686
2687 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe9e645e112019-05-10 16:07:28 -06002688 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07002689 return -EAGAIN;
2690
2691 /* workqueue context doesn't hold uring_lock, grab it now */
Jackie Liuba5290c2019-10-09 09:19:59 +08002692 if (s->in_async)
Jens Axboedef596e2019-01-09 08:59:42 -07002693 mutex_lock(&ctx->uring_lock);
2694 io_iopoll_req_issued(req);
Jackie Liuba5290c2019-10-09 09:19:59 +08002695 if (s->in_async)
Jens Axboedef596e2019-01-09 08:59:42 -07002696 mutex_unlock(&ctx->uring_lock);
2697 }
2698
2699 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002700}
2701
Jens Axboeb76da702019-11-20 13:05:32 -07002702static void io_link_work_cb(struct io_wq_work **workptr)
2703{
2704 struct io_wq_work *work = *workptr;
2705 struct io_kiocb *link = work->data;
2706
2707 io_queue_linked_timeout(link);
2708 work->func = io_wq_submit_work;
2709}
2710
Jens Axboe561fb042019-10-24 07:25:42 -06002711static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboe31b51512019-01-18 22:56:34 -07002712{
Jens Axboe561fb042019-10-24 07:25:42 -06002713 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002714 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe561fb042019-10-24 07:25:42 -06002715 struct sqe_submit *s = &req->submit;
Jens Axboe561fb042019-10-24 07:25:42 -06002716 struct io_kiocb *nxt = NULL;
2717 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002718
Jens Axboe561fb042019-10-24 07:25:42 -06002719 /* Ensure we clear previously set non-block flag */
2720 req->rw.ki_flags &= ~IOCB_NOWAIT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002721
Jens Axboe561fb042019-10-24 07:25:42 -06002722 if (work->flags & IO_WQ_WORK_CANCEL)
2723 ret = -ECANCELED;
Jens Axboe31b51512019-01-18 22:56:34 -07002724
Jens Axboe561fb042019-10-24 07:25:42 -06002725 if (!ret) {
2726 s->has_user = (work->flags & IO_WQ_WORK_HAS_MM) != 0;
2727 s->in_async = true;
2728 do {
Pavel Begunkovd7324472019-11-21 21:24:36 +03002729 ret = io_issue_sqe(req, &nxt, false);
Jens Axboe561fb042019-10-24 07:25:42 -06002730 /*
2731 * We can get EAGAIN for polled IO even though we're
2732 * forcing a sync submission from here, since we can't
2733 * wait for request slots on the block side.
2734 */
2735 if (ret != -EAGAIN)
2736 break;
2737 cond_resched();
2738 } while (1);
2739 }
Jens Axboe31b51512019-01-18 22:56:34 -07002740
Jens Axboe561fb042019-10-24 07:25:42 -06002741 /* drop submission reference */
Jackie Liuec9c02a2019-11-08 23:50:36 +08002742 io_put_req(req);
Jens Axboe817869d2019-04-30 14:44:05 -06002743
Jens Axboe561fb042019-10-24 07:25:42 -06002744 if (ret) {
Jens Axboef1f40852019-11-05 20:33:16 -07002745 if (req->flags & REQ_F_LINK)
2746 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07002747 io_cqring_add_event(req, ret);
Jens Axboe817869d2019-04-30 14:44:05 -06002748 io_put_req(req);
Jens Axboeedafcce2019-01-09 09:16:05 -07002749 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002750
Jens Axboe561fb042019-10-24 07:25:42 -06002751 /* if a dependent link is ready, pass it back */
2752 if (!ret && nxt) {
Jens Axboe94ae5e72019-11-14 19:39:52 -07002753 struct io_kiocb *link;
2754
2755 io_prep_async_work(nxt, &link);
Jens Axboe561fb042019-10-24 07:25:42 -06002756 *workptr = &nxt->work;
Jens Axboeb76da702019-11-20 13:05:32 -07002757 if (link) {
2758 nxt->work.flags |= IO_WQ_WORK_CB;
2759 nxt->work.func = io_link_work_cb;
2760 nxt->work.data = link;
2761 }
Jens Axboeedafcce2019-01-09 09:16:05 -07002762 }
Jens Axboe31b51512019-01-18 22:56:34 -07002763}
Jens Axboe2b188cc2019-01-07 10:46:33 -07002764
Jens Axboe09bb8392019-03-13 12:39:28 -06002765static bool io_op_needs_file(const struct io_uring_sqe *sqe)
2766{
2767 int op = READ_ONCE(sqe->opcode);
2768
2769 switch (op) {
2770 case IORING_OP_NOP:
2771 case IORING_OP_POLL_REMOVE:
Pavel Begunkov5683e542019-11-14 00:59:19 +03002772 case IORING_OP_TIMEOUT:
Pavel Begunkova320e9f2019-11-14 00:11:01 +03002773 case IORING_OP_TIMEOUT_REMOVE:
2774 case IORING_OP_ASYNC_CANCEL:
2775 case IORING_OP_LINK_TIMEOUT:
Jens Axboe09bb8392019-03-13 12:39:28 -06002776 return false;
2777 default:
2778 return true;
2779 }
2780}
2781
Jens Axboe65e19f52019-10-26 07:20:21 -06002782static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
2783 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06002784{
Jens Axboe65e19f52019-10-26 07:20:21 -06002785 struct fixed_file_table *table;
2786
2787 table = &ctx->file_table[index >> IORING_FILE_TABLE_SHIFT];
2788 return table->files[index & IORING_FILE_TABLE_MASK];
2789}
2790
Jackie Liua197f662019-11-08 08:09:12 -07002791static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req)
Jens Axboe09bb8392019-03-13 12:39:28 -06002792{
Pavel Begunkov267bc902019-11-07 01:41:08 +03002793 struct sqe_submit *s = &req->submit;
Jackie Liua197f662019-11-08 08:09:12 -07002794 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe09bb8392019-03-13 12:39:28 -06002795 unsigned flags;
2796 int fd;
2797
2798 flags = READ_ONCE(s->sqe->flags);
2799 fd = READ_ONCE(s->sqe->fd);
2800
Jackie Liu4fe2c962019-09-09 20:50:40 +08002801 if (flags & IOSQE_IO_DRAIN)
Jens Axboede0617e2019-04-06 21:51:27 -06002802 req->flags |= REQ_F_IO_DRAIN;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002803 /*
2804 * All io need record the previous position, if LINK vs DARIN,
2805 * it can be used to mark the position of the first IO in the
2806 * link list.
2807 */
2808 req->sequence = s->sequence;
Jens Axboede0617e2019-04-06 21:51:27 -06002809
Jens Axboe60c112b2019-06-21 10:20:18 -06002810 if (!io_op_needs_file(s->sqe))
Jens Axboe09bb8392019-03-13 12:39:28 -06002811 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06002812
2813 if (flags & IOSQE_FIXED_FILE) {
Jens Axboe65e19f52019-10-26 07:20:21 -06002814 if (unlikely(!ctx->file_table ||
Jens Axboe09bb8392019-03-13 12:39:28 -06002815 (unsigned) fd >= ctx->nr_user_files))
2816 return -EBADF;
Jens Axboeb7620122019-10-26 07:22:55 -06002817 fd = array_index_nospec(fd, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06002818 req->file = io_file_from_index(ctx, fd);
2819 if (!req->file)
Jens Axboe08a45172019-10-03 08:11:03 -06002820 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06002821 req->flags |= REQ_F_FIXED_FILE;
2822 } else {
2823 if (s->needs_fixed_file)
2824 return -EBADF;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002825 trace_io_uring_file_get(ctx, fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06002826 req->file = io_file_get(state, fd);
2827 if (unlikely(!req->file))
2828 return -EBADF;
2829 }
2830
2831 return 0;
2832}
2833
Jackie Liua197f662019-11-08 08:09:12 -07002834static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002835{
Jens Axboefcb323c2019-10-24 12:39:47 -06002836 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07002837 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06002838
2839 rcu_read_lock();
2840 spin_lock_irq(&ctx->inflight_lock);
2841 /*
2842 * We use the f_ops->flush() handler to ensure that we can flush
2843 * out work accessing these files if the fd is closed. Check if
2844 * the fd has changed since we started down this path, and disallow
2845 * this operation if it has.
2846 */
2847 if (fcheck(req->submit.ring_fd) == req->submit.ring_file) {
2848 list_add(&req->inflight_entry, &ctx->inflight_list);
2849 req->flags |= REQ_F_INFLIGHT;
2850 req->work.files = current->files;
2851 ret = 0;
2852 }
2853 spin_unlock_irq(&ctx->inflight_lock);
2854 rcu_read_unlock();
2855
2856 return ret;
2857}
2858
Jens Axboe2665abf2019-11-05 12:40:47 -07002859static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
2860{
Jens Axboead8a48a2019-11-15 08:49:11 -07002861 struct io_timeout_data *data = container_of(timer,
2862 struct io_timeout_data, timer);
2863 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07002864 struct io_ring_ctx *ctx = req->ctx;
2865 struct io_kiocb *prev = NULL;
2866 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07002867
2868 spin_lock_irqsave(&ctx->completion_lock, flags);
2869
2870 /*
2871 * We don't expect the list to be empty, that will only happen if we
2872 * race with the completion of the linked work.
2873 */
2874 if (!list_empty(&req->list)) {
2875 prev = list_entry(req->list.prev, struct io_kiocb, link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07002876 if (refcount_inc_not_zero(&prev->refs)) {
Jens Axboe76a46e02019-11-10 23:34:16 -07002877 list_del_init(&req->list);
Jens Axboe5d960722019-11-19 15:31:28 -07002878 prev->flags &= ~REQ_F_LINK_TIMEOUT;
2879 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07002880 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07002881 }
2882
2883 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2884
2885 if (prev) {
Jens Axboefba38c22019-11-18 12:27:57 -07002886 if (prev->flags & REQ_F_LINK)
2887 prev->flags |= REQ_F_FAIL_LINK;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002888 io_async_find_and_cancel(ctx, req, prev->user_data, NULL,
2889 -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07002890 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07002891 } else {
2892 io_cqring_add_event(req, -ETIME);
2893 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07002894 }
Jens Axboe2665abf2019-11-05 12:40:47 -07002895 return HRTIMER_NORESTART;
2896}
2897
Jens Axboead8a48a2019-11-15 08:49:11 -07002898static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07002899{
Jens Axboe76a46e02019-11-10 23:34:16 -07002900 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07002901
Jens Axboe76a46e02019-11-10 23:34:16 -07002902 /*
2903 * If the list is now empty, then our linked request finished before
2904 * we got a chance to setup the timer
2905 */
2906 spin_lock_irq(&ctx->completion_lock);
2907 if (!list_empty(&req->list)) {
Jens Axboe94ae5e72019-11-14 19:39:52 -07002908 struct io_timeout_data *data = req->timeout.data;
2909
Jens Axboead8a48a2019-11-15 08:49:11 -07002910 data->timer.function = io_link_timeout_fn;
2911 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
2912 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07002913 }
Jens Axboe76a46e02019-11-10 23:34:16 -07002914 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07002915
Jens Axboe2665abf2019-11-05 12:40:47 -07002916 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07002917 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07002918}
2919
Jens Axboead8a48a2019-11-15 08:49:11 -07002920static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07002921{
2922 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002923
Jens Axboe2665abf2019-11-05 12:40:47 -07002924 if (!(req->flags & REQ_F_LINK))
2925 return NULL;
2926
2927 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, list);
Jens Axboe76a46e02019-11-10 23:34:16 -07002928 if (!nxt || nxt->submit.sqe->opcode != IORING_OP_LINK_TIMEOUT)
2929 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07002930
Jens Axboe76a46e02019-11-10 23:34:16 -07002931 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07002932 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07002933}
2934
Jens Axboe0e0702d2019-11-14 21:42:10 -07002935static void __io_queue_sqe(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002936{
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03002937 struct io_kiocb *linked_timeout = io_prep_linked_timeout(req);
2938 struct io_kiocb *nxt = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002939 int ret;
2940
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03002941 ret = io_issue_sqe(req, &nxt, true);
2942 if (nxt)
2943 io_queue_async_work(nxt);
Jens Axboe491381ce2019-10-17 09:20:46 -06002944
2945 /*
2946 * We async punt it if the file wasn't marked NOWAIT, or if the file
2947 * doesn't support non-blocking read/write attempts
2948 */
2949 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
2950 (req->flags & REQ_F_MUST_PUNT))) {
Pavel Begunkov267bc902019-11-07 01:41:08 +03002951 struct sqe_submit *s = &req->submit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002952 struct io_uring_sqe *sqe_copy;
2953
Jackie Liu954dab12019-09-18 10:37:52 +08002954 sqe_copy = kmemdup(s->sqe, sizeof(*sqe_copy), GFP_KERNEL);
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03002955 if (!sqe_copy)
2956 goto err;
Jens Axboee65ef562019-03-12 10:16:44 -06002957
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03002958 s->sqe = sqe_copy;
2959 req->flags |= REQ_F_FREE_SQE;
2960
2961 if (req->work.flags & IO_WQ_WORK_NEEDS_FILES) {
2962 ret = io_grab_files(req);
2963 if (ret)
2964 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002965 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03002966
2967 /*
2968 * Queued up for async execution, worker will release
2969 * submit reference when the iocb is actually submitted.
2970 */
2971 io_queue_async_work(req);
2972 return;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002973 }
Jens Axboee65ef562019-03-12 10:16:44 -06002974
Jens Axboefcb323c2019-10-24 12:39:47 -06002975err:
Jens Axboee65ef562019-03-12 10:16:44 -06002976 /* drop submission reference */
2977 io_put_req(req);
2978
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03002979 if (linked_timeout) {
Jens Axboe76a46e02019-11-10 23:34:16 -07002980 if (!ret)
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03002981 io_queue_linked_timeout(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07002982 else
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03002983 io_put_req(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07002984 }
2985
Jens Axboee65ef562019-03-12 10:16:44 -06002986 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06002987 if (ret) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07002988 io_cqring_add_event(req, ret);
Jens Axboe9e645e112019-05-10 16:07:28 -06002989 if (req->flags & REQ_F_LINK)
2990 req->flags |= REQ_F_FAIL_LINK;
Jens Axboee65ef562019-03-12 10:16:44 -06002991 io_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002992 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002993}
2994
Jens Axboe0e0702d2019-11-14 21:42:10 -07002995static void io_queue_sqe(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08002996{
2997 int ret;
2998
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03002999 if (unlikely(req->ctx->drain_next)) {
3000 req->flags |= REQ_F_IO_DRAIN;
3001 req->ctx->drain_next = false;
3002 }
3003 req->ctx->drain_next = (req->flags & REQ_F_DRAIN_LINK);
3004
Jackie Liua197f662019-11-08 08:09:12 -07003005 ret = io_req_defer(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08003006 if (ret) {
3007 if (ret != -EIOCBQUEUED) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07003008 io_cqring_add_event(req, ret);
Pavel Begunkovd3b357962019-11-19 23:32:48 +03003009 if (req->flags & REQ_F_LINK)
3010 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07003011 io_double_put_req(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08003012 }
Jens Axboe0e0702d2019-11-14 21:42:10 -07003013 } else
3014 __io_queue_sqe(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08003015}
3016
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003017static inline void io_queue_link_head(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08003018{
Jens Axboe94ae5e72019-11-14 19:39:52 -07003019 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003020 io_cqring_add_event(req, -ECANCELED);
3021 io_double_put_req(req);
3022 } else
Jens Axboe0e0702d2019-11-14 21:42:10 -07003023 io_queue_sqe(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08003024}
3025
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003026
Jens Axboe9e645e112019-05-10 16:07:28 -06003027#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK)
3028
Jackie Liua197f662019-11-08 08:09:12 -07003029static void io_submit_sqe(struct io_kiocb *req, struct io_submit_state *state,
3030 struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06003031{
Pavel Begunkov267bc902019-11-07 01:41:08 +03003032 struct sqe_submit *s = &req->submit;
Jackie Liua197f662019-11-08 08:09:12 -07003033 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -06003034 int ret;
3035
Jens Axboe78e19bb2019-11-06 15:21:34 -07003036 req->user_data = s->sqe->user_data;
3037
Jens Axboe9e645e112019-05-10 16:07:28 -06003038 /* enforce forwards compatibility on users */
3039 if (unlikely(s->sqe->flags & ~SQE_VALID_FLAGS)) {
3040 ret = -EINVAL;
Pavel Begunkov196be952019-11-07 01:41:06 +03003041 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06003042 }
3043
Jackie Liua197f662019-11-08 08:09:12 -07003044 ret = io_req_set_file(state, req);
Jens Axboe9e645e112019-05-10 16:07:28 -06003045 if (unlikely(ret)) {
3046err_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07003047 io_cqring_add_event(req, ret);
3048 io_double_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06003049 return;
3050 }
3051
Jens Axboe9e645e112019-05-10 16:07:28 -06003052 /*
3053 * If we already have a head request, queue this one for async
3054 * submittal once the head completes. If we don't have a head but
3055 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
3056 * submitted sync once the chain is complete. If none of those
3057 * conditions are true (normal request), then just queue it.
3058 */
3059 if (*link) {
3060 struct io_kiocb *prev = *link;
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03003061 struct io_uring_sqe *sqe_copy;
Jens Axboe9e645e112019-05-10 16:07:28 -06003062
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003063 if (s->sqe->flags & IOSQE_IO_DRAIN)
3064 (*link)->flags |= REQ_F_DRAIN_LINK | REQ_F_IO_DRAIN;
3065
Jens Axboe94ae5e72019-11-14 19:39:52 -07003066 if (READ_ONCE(s->sqe->opcode) == IORING_OP_LINK_TIMEOUT) {
3067 ret = io_timeout_setup(req);
3068 /* common setup allows offset being set, we don't */
3069 if (!ret && s->sqe->off)
3070 ret = -EINVAL;
3071 if (ret) {
3072 prev->flags |= REQ_F_FAIL_LINK;
3073 goto err_req;
3074 }
3075 }
3076
Jens Axboe9e645e112019-05-10 16:07:28 -06003077 sqe_copy = kmemdup(s->sqe, sizeof(*sqe_copy), GFP_KERNEL);
3078 if (!sqe_copy) {
3079 ret = -EAGAIN;
3080 goto err_req;
3081 }
3082
3083 s->sqe = sqe_copy;
Jens Axboe94ae5e72019-11-14 19:39:52 -07003084 req->flags |= REQ_F_FREE_SQE;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02003085 trace_io_uring_link(ctx, req, prev);
Jens Axboe9e645e112019-05-10 16:07:28 -06003086 list_add_tail(&req->list, &prev->link_list);
3087 } else if (s->sqe->flags & IOSQE_IO_LINK) {
3088 req->flags |= REQ_F_LINK;
3089
Jens Axboe9e645e112019-05-10 16:07:28 -06003090 INIT_LIST_HEAD(&req->link_list);
3091 *link = req;
3092 } else {
Jackie Liua197f662019-11-08 08:09:12 -07003093 io_queue_sqe(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06003094 }
3095}
3096
Jens Axboe9a56a232019-01-09 09:06:50 -07003097/*
3098 * Batched submission is done, ensure local IO is flushed out.
3099 */
3100static void io_submit_state_end(struct io_submit_state *state)
3101{
3102 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06003103 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07003104 if (state->free_reqs)
3105 kmem_cache_free_bulk(req_cachep, state->free_reqs,
3106 &state->reqs[state->cur_req]);
Jens Axboe9a56a232019-01-09 09:06:50 -07003107}
3108
3109/*
3110 * Start submission side cache.
3111 */
3112static void io_submit_state_start(struct io_submit_state *state,
3113 struct io_ring_ctx *ctx, unsigned max_ios)
3114{
3115 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07003116 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07003117 state->file = NULL;
3118 state->ios_left = max_ios;
3119}
3120
Jens Axboe2b188cc2019-01-07 10:46:33 -07003121static void io_commit_sqring(struct io_ring_ctx *ctx)
3122{
Hristo Venev75b28af2019-08-26 17:23:46 +00003123 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003124
Hristo Venev75b28af2019-08-26 17:23:46 +00003125 if (ctx->cached_sq_head != READ_ONCE(rings->sq.head)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07003126 /*
3127 * Ensure any loads from the SQEs are done at this point,
3128 * since once we write the new head, the application could
3129 * write new data to them.
3130 */
Hristo Venev75b28af2019-08-26 17:23:46 +00003131 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003132 }
3133}
3134
3135/*
Jens Axboe2b188cc2019-01-07 10:46:33 -07003136 * Fetch an sqe, if one is available. Note that s->sqe will point to memory
3137 * that is mapped by userspace. This means that care needs to be taken to
3138 * ensure that reads are stable, as we cannot rely on userspace always
3139 * being a good citizen. If members of the sqe are validated and then later
3140 * used, it's important that those reads are done through READ_ONCE() to
3141 * prevent a re-load down the line.
3142 */
3143static bool io_get_sqring(struct io_ring_ctx *ctx, struct sqe_submit *s)
3144{
Hristo Venev75b28af2019-08-26 17:23:46 +00003145 struct io_rings *rings = ctx->rings;
3146 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003147 unsigned head;
3148
3149 /*
3150 * The cached sq head (or cq tail) serves two purposes:
3151 *
3152 * 1) allows us to batch the cost of updating the user visible
3153 * head updates.
3154 * 2) allows the kernel side to track the head on its own, even
3155 * though the application is the one updating it.
3156 */
3157 head = ctx->cached_sq_head;
Stefan Bühlere523a292019-04-19 11:57:44 +02003158 /* make sure SQ entry isn't read before tail */
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03003159 if (unlikely(head == smp_load_acquire(&rings->sq.tail)))
Jens Axboe2b188cc2019-01-07 10:46:33 -07003160 return false;
3161
Hristo Venev75b28af2019-08-26 17:23:46 +00003162 head = READ_ONCE(sq_array[head & ctx->sq_mask]);
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03003163 if (likely(head < ctx->sq_entries)) {
Jens Axboefcb323c2019-10-24 12:39:47 -06003164 s->ring_file = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003165 s->sqe = &ctx->sq_sqes[head];
Jackie Liu8776f3f2019-09-09 20:50:39 +08003166 s->sequence = ctx->cached_sq_head;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003167 ctx->cached_sq_head++;
3168 return true;
3169 }
3170
3171 /* drop invalid entries */
3172 ctx->cached_sq_head++;
Jens Axboe498ccd92019-10-25 10:04:25 -06003173 ctx->cached_sq_dropped++;
3174 WRITE_ONCE(rings->sq_dropped, ctx->cached_sq_dropped);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003175 return false;
3176}
3177
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003178static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkovae9428c2019-11-06 00:22:14 +03003179 struct file *ring_file, int ring_fd,
3180 struct mm_struct **mm, bool async)
Jens Axboe6c271ce2019-01-10 11:22:30 -07003181{
3182 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06003183 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06003184 int i, submitted = 0;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03003185 bool mm_fault = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003186
Jens Axboec4a2ed72019-11-21 21:01:26 -07003187 /* if we have a backlog and couldn't flush it all, return BUSY */
3188 if (!list_empty(&ctx->cq_overflow_list) &&
3189 !io_cqring_overflow_flush(ctx, false))
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003190 return -EBUSY;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003191
3192 if (nr > IO_PLUG_THRESHOLD) {
3193 io_submit_state_start(&state, ctx, nr);
3194 statep = &state;
3195 }
3196
3197 for (i = 0; i < nr; i++) {
Pavel Begunkov196be952019-11-07 01:41:06 +03003198 struct io_kiocb *req;
Pavel Begunkov50585b92019-11-07 01:41:07 +03003199 unsigned int sqe_flags;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003200
Pavel Begunkov196be952019-11-07 01:41:06 +03003201 req = io_get_req(ctx, statep);
3202 if (unlikely(!req)) {
3203 if (!submitted)
3204 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003205 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06003206 }
Pavel Begunkov50585b92019-11-07 01:41:07 +03003207 if (!io_get_sqring(ctx, &req->submit)) {
Pavel Begunkov196be952019-11-07 01:41:06 +03003208 __io_free_req(req);
3209 break;
3210 }
Jens Axboe9e645e112019-05-10 16:07:28 -06003211
Pavel Begunkov50585b92019-11-07 01:41:07 +03003212 if (io_sqe_needs_user(req->submit.sqe) && !*mm) {
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03003213 mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm);
3214 if (!mm_fault) {
3215 use_mm(ctx->sqo_mm);
3216 *mm = ctx->sqo_mm;
3217 }
3218 }
3219
Pavel Begunkov50585b92019-11-07 01:41:07 +03003220 sqe_flags = req->submit.sqe->flags;
3221
Pavel Begunkov50585b92019-11-07 01:41:07 +03003222 req->submit.ring_file = ring_file;
3223 req->submit.ring_fd = ring_fd;
3224 req->submit.has_user = *mm != NULL;
3225 req->submit.in_async = async;
3226 req->submit.needs_fixed_file = async;
3227 trace_io_uring_submit_sqe(ctx, req->submit.sqe->user_data,
3228 true, async);
Jackie Liua197f662019-11-08 08:09:12 -07003229 io_submit_sqe(req, statep, &link);
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03003230 submitted++;
Pavel Begunkove5eb6362019-11-06 00:22:15 +03003231
3232 /*
3233 * If previous wasn't linked and we have a linked command,
3234 * that's the end of the chain. Submit the previous link.
3235 */
Pavel Begunkov50585b92019-11-07 01:41:07 +03003236 if (!(sqe_flags & IOSQE_IO_LINK) && link) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003237 io_queue_link_head(link);
Pavel Begunkove5eb6362019-11-06 00:22:15 +03003238 link = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003239 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07003240 }
3241
Jens Axboe9e645e112019-05-10 16:07:28 -06003242 if (link)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003243 io_queue_link_head(link);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003244 if (statep)
3245 io_submit_state_end(&state);
3246
Pavel Begunkovae9428c2019-11-06 00:22:14 +03003247 /* Commit SQ ring head once we've consumed and submitted all SQEs */
3248 io_commit_sqring(ctx);
3249
Jens Axboe6c271ce2019-01-10 11:22:30 -07003250 return submitted;
3251}
3252
3253static int io_sq_thread(void *data)
3254{
Jens Axboe6c271ce2019-01-10 11:22:30 -07003255 struct io_ring_ctx *ctx = data;
3256 struct mm_struct *cur_mm = NULL;
3257 mm_segment_t old_fs;
3258 DEFINE_WAIT(wait);
3259 unsigned inflight;
3260 unsigned long timeout;
Jens Axboec1edbf52019-11-10 16:56:04 -07003261 int ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003262
Jens Axboe206aefd2019-11-07 18:27:42 -07003263 complete(&ctx->completions[1]);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08003264
Jens Axboe6c271ce2019-01-10 11:22:30 -07003265 old_fs = get_fs();
3266 set_fs(USER_DS);
3267
Jens Axboec1edbf52019-11-10 16:56:04 -07003268 ret = timeout = inflight = 0;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003269 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003270 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003271
3272 if (inflight) {
3273 unsigned nr_events = 0;
3274
3275 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe2b2ed972019-10-25 10:06:15 -06003276 /*
3277 * inflight is the count of the maximum possible
3278 * entries we submitted, but it can be smaller
3279 * if we dropped some of them. If we don't have
3280 * poll entries available, then we know that we
3281 * have nothing left to poll for. Reset the
3282 * inflight count to zero in that case.
3283 */
3284 mutex_lock(&ctx->uring_lock);
3285 if (!list_empty(&ctx->poll_list))
3286 __io_iopoll_check(ctx, &nr_events, 0);
3287 else
3288 inflight = 0;
3289 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003290 } else {
3291 /*
3292 * Normal IO, just pretend everything completed.
3293 * We don't have to poll completions for that.
3294 */
3295 nr_events = inflight;
3296 }
3297
3298 inflight -= nr_events;
3299 if (!inflight)
3300 timeout = jiffies + ctx->sq_thread_idle;
3301 }
3302
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003303 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07003304
3305 /*
3306 * If submit got -EBUSY, flag us as needing the application
3307 * to enter the kernel to reap and flush events.
3308 */
3309 if (!to_submit || ret == -EBUSY) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07003310 /*
3311 * We're polling. If we're within the defined idle
3312 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07003313 * to sleep. The exception is if we got EBUSY doing
3314 * more IO, we should wait for the application to
3315 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07003316 */
Jens Axboec1edbf52019-11-10 16:56:04 -07003317 if (inflight ||
3318 (!time_after(jiffies, timeout) && ret != -EBUSY)) {
Jens Axboe9831a902019-09-19 09:48:55 -06003319 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07003320 continue;
3321 }
3322
3323 /*
3324 * Drop cur_mm before scheduling, we can't hold it for
3325 * long periods (or over schedule()). Do this before
3326 * adding ourselves to the waitqueue, as the unuse/drop
3327 * may sleep.
3328 */
3329 if (cur_mm) {
3330 unuse_mm(cur_mm);
3331 mmput(cur_mm);
3332 cur_mm = NULL;
3333 }
3334
3335 prepare_to_wait(&ctx->sqo_wait, &wait,
3336 TASK_INTERRUPTIBLE);
3337
3338 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00003339 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02003340 /* make sure to read SQ tail after writing flags */
3341 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07003342
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003343 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07003344 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003345 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07003346 finish_wait(&ctx->sqo_wait, &wait);
3347 break;
3348 }
3349 if (signal_pending(current))
3350 flush_signals(current);
3351 schedule();
3352 finish_wait(&ctx->sqo_wait, &wait);
3353
Hristo Venev75b28af2019-08-26 17:23:46 +00003354 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003355 continue;
3356 }
3357 finish_wait(&ctx->sqo_wait, &wait);
3358
Hristo Venev75b28af2019-08-26 17:23:46 +00003359 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003360 }
3361
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003362 to_submit = min(to_submit, ctx->sq_entries);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003363 ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true);
3364 if (ret > 0)
3365 inflight += ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003366 }
3367
3368 set_fs(old_fs);
3369 if (cur_mm) {
3370 unuse_mm(cur_mm);
3371 mmput(cur_mm);
3372 }
Jens Axboe06058632019-04-13 09:26:03 -06003373
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003374 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06003375
Jens Axboe6c271ce2019-01-10 11:22:30 -07003376 return 0;
3377}
3378
Jens Axboebda52162019-09-24 13:47:15 -06003379struct io_wait_queue {
3380 struct wait_queue_entry wq;
3381 struct io_ring_ctx *ctx;
3382 unsigned to_wait;
3383 unsigned nr_timeouts;
3384};
3385
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003386static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06003387{
3388 struct io_ring_ctx *ctx = iowq->ctx;
3389
3390 /*
3391 * Wake up if we have enough events, or if a timeout occured since we
3392 * started waiting. For timeouts, we always want to return to userspace,
3393 * regardless of event count.
3394 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003395 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06003396 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
3397}
3398
3399static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
3400 int wake_flags, void *key)
3401{
3402 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
3403 wq);
3404
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003405 /* use noflush == true, as we can't safely rely on locking context */
3406 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06003407 return -1;
3408
3409 return autoremove_wake_function(curr, mode, wake_flags, key);
3410}
3411
Jens Axboe2b188cc2019-01-07 10:46:33 -07003412/*
3413 * Wait until events become available, if we don't already have some. The
3414 * application must reap them itself, as they reside on the shared cq ring.
3415 */
3416static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
3417 const sigset_t __user *sig, size_t sigsz)
3418{
Jens Axboebda52162019-09-24 13:47:15 -06003419 struct io_wait_queue iowq = {
3420 .wq = {
3421 .private = current,
3422 .func = io_wake_function,
3423 .entry = LIST_HEAD_INIT(iowq.wq.entry),
3424 },
3425 .ctx = ctx,
3426 .to_wait = min_events,
3427 };
Hristo Venev75b28af2019-08-26 17:23:46 +00003428 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08003429 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003430
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003431 if (io_cqring_events(ctx, false) >= min_events)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003432 return 0;
3433
3434 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01003435#ifdef CONFIG_COMPAT
3436 if (in_compat_syscall())
3437 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07003438 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01003439 else
3440#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07003441 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01003442
Jens Axboe2b188cc2019-01-07 10:46:33 -07003443 if (ret)
3444 return ret;
3445 }
3446
Jens Axboebda52162019-09-24 13:47:15 -06003447 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02003448 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06003449 do {
3450 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
3451 TASK_INTERRUPTIBLE);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003452 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06003453 break;
3454 schedule();
3455 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08003456 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06003457 break;
3458 }
3459 } while (1);
3460 finish_wait(&ctx->wait, &iowq.wq);
3461
Jackie Liue9ffa5c2019-10-29 11:16:42 +08003462 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003463
Hristo Venev75b28af2019-08-26 17:23:46 +00003464 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003465}
3466
Jens Axboe6b063142019-01-10 22:13:58 -07003467static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
3468{
3469#if defined(CONFIG_UNIX)
3470 if (ctx->ring_sock) {
3471 struct sock *sock = ctx->ring_sock->sk;
3472 struct sk_buff *skb;
3473
3474 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
3475 kfree_skb(skb);
3476 }
3477#else
3478 int i;
3479
Jens Axboe65e19f52019-10-26 07:20:21 -06003480 for (i = 0; i < ctx->nr_user_files; i++) {
3481 struct file *file;
3482
3483 file = io_file_from_index(ctx, i);
3484 if (file)
3485 fput(file);
3486 }
Jens Axboe6b063142019-01-10 22:13:58 -07003487#endif
3488}
3489
3490static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
3491{
Jens Axboe65e19f52019-10-26 07:20:21 -06003492 unsigned nr_tables, i;
3493
3494 if (!ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003495 return -ENXIO;
3496
3497 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06003498 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
3499 for (i = 0; i < nr_tables; i++)
3500 kfree(ctx->file_table[i].files);
3501 kfree(ctx->file_table);
3502 ctx->file_table = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003503 ctx->nr_user_files = 0;
3504 return 0;
3505}
3506
Jens Axboe6c271ce2019-01-10 11:22:30 -07003507static void io_sq_thread_stop(struct io_ring_ctx *ctx)
3508{
3509 if (ctx->sqo_thread) {
Jens Axboe206aefd2019-11-07 18:27:42 -07003510 wait_for_completion(&ctx->completions[1]);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003511 /*
3512 * The park is a bit of a work-around, without it we get
3513 * warning spews on shutdown with SQPOLL set and affinity
3514 * set to a single CPU.
3515 */
Jens Axboe06058632019-04-13 09:26:03 -06003516 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003517 kthread_stop(ctx->sqo_thread);
3518 ctx->sqo_thread = NULL;
3519 }
3520}
3521
Jens Axboe6b063142019-01-10 22:13:58 -07003522static void io_finish_async(struct io_ring_ctx *ctx)
3523{
Jens Axboe6c271ce2019-01-10 11:22:30 -07003524 io_sq_thread_stop(ctx);
3525
Jens Axboe561fb042019-10-24 07:25:42 -06003526 if (ctx->io_wq) {
3527 io_wq_destroy(ctx->io_wq);
3528 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003529 }
3530}
3531
3532#if defined(CONFIG_UNIX)
3533static void io_destruct_skb(struct sk_buff *skb)
3534{
3535 struct io_ring_ctx *ctx = skb->sk->sk_user_data;
3536
Jens Axboe561fb042019-10-24 07:25:42 -06003537 if (ctx->io_wq)
3538 io_wq_flush(ctx->io_wq);
Jens Axboe8a997342019-10-09 14:40:13 -06003539
Jens Axboe6b063142019-01-10 22:13:58 -07003540 unix_destruct_scm(skb);
3541}
3542
3543/*
3544 * Ensure the UNIX gc is aware of our file set, so we are certain that
3545 * the io_uring can be safely unregistered on process exit, even if we have
3546 * loops in the file referencing.
3547 */
3548static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
3549{
3550 struct sock *sk = ctx->ring_sock->sk;
3551 struct scm_fp_list *fpl;
3552 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06003553 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07003554
3555 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
3556 unsigned long inflight = ctx->user->unix_inflight + nr;
3557
3558 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
3559 return -EMFILE;
3560 }
3561
3562 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
3563 if (!fpl)
3564 return -ENOMEM;
3565
3566 skb = alloc_skb(0, GFP_KERNEL);
3567 if (!skb) {
3568 kfree(fpl);
3569 return -ENOMEM;
3570 }
3571
3572 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07003573
Jens Axboe08a45172019-10-03 08:11:03 -06003574 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07003575 fpl->user = get_uid(ctx->user);
3576 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003577 struct file *file = io_file_from_index(ctx, i + offset);
3578
3579 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06003580 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06003581 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06003582 unix_inflight(fpl->user, fpl->fp[nr_files]);
3583 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07003584 }
3585
Jens Axboe08a45172019-10-03 08:11:03 -06003586 if (nr_files) {
3587 fpl->max = SCM_MAX_FD;
3588 fpl->count = nr_files;
3589 UNIXCB(skb).fp = fpl;
3590 skb->destructor = io_destruct_skb;
3591 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
3592 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07003593
Jens Axboe08a45172019-10-03 08:11:03 -06003594 for (i = 0; i < nr_files; i++)
3595 fput(fpl->fp[i]);
3596 } else {
3597 kfree_skb(skb);
3598 kfree(fpl);
3599 }
Jens Axboe6b063142019-01-10 22:13:58 -07003600
3601 return 0;
3602}
3603
3604/*
3605 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
3606 * causes regular reference counting to break down. We rely on the UNIX
3607 * garbage collection to take care of this problem for us.
3608 */
3609static int io_sqe_files_scm(struct io_ring_ctx *ctx)
3610{
3611 unsigned left, total;
3612 int ret = 0;
3613
3614 total = 0;
3615 left = ctx->nr_user_files;
3616 while (left) {
3617 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07003618
3619 ret = __io_sqe_files_scm(ctx, this_files, total);
3620 if (ret)
3621 break;
3622 left -= this_files;
3623 total += this_files;
3624 }
3625
3626 if (!ret)
3627 return 0;
3628
3629 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003630 struct file *file = io_file_from_index(ctx, total);
3631
3632 if (file)
3633 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07003634 total++;
3635 }
3636
3637 return ret;
3638}
3639#else
3640static int io_sqe_files_scm(struct io_ring_ctx *ctx)
3641{
3642 return 0;
3643}
3644#endif
3645
Jens Axboe65e19f52019-10-26 07:20:21 -06003646static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
3647 unsigned nr_files)
3648{
3649 int i;
3650
3651 for (i = 0; i < nr_tables; i++) {
3652 struct fixed_file_table *table = &ctx->file_table[i];
3653 unsigned this_files;
3654
3655 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
3656 table->files = kcalloc(this_files, sizeof(struct file *),
3657 GFP_KERNEL);
3658 if (!table->files)
3659 break;
3660 nr_files -= this_files;
3661 }
3662
3663 if (i == nr_tables)
3664 return 0;
3665
3666 for (i = 0; i < nr_tables; i++) {
3667 struct fixed_file_table *table = &ctx->file_table[i];
3668 kfree(table->files);
3669 }
3670 return 1;
3671}
3672
Jens Axboe6b063142019-01-10 22:13:58 -07003673static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
3674 unsigned nr_args)
3675{
3676 __s32 __user *fds = (__s32 __user *) arg;
Jens Axboe65e19f52019-10-26 07:20:21 -06003677 unsigned nr_tables;
Jens Axboe6b063142019-01-10 22:13:58 -07003678 int fd, ret = 0;
3679 unsigned i;
3680
Jens Axboe65e19f52019-10-26 07:20:21 -06003681 if (ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003682 return -EBUSY;
3683 if (!nr_args)
3684 return -EINVAL;
3685 if (nr_args > IORING_MAX_FIXED_FILES)
3686 return -EMFILE;
3687
Jens Axboe65e19f52019-10-26 07:20:21 -06003688 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
3689 ctx->file_table = kcalloc(nr_tables, sizeof(struct fixed_file_table),
3690 GFP_KERNEL);
3691 if (!ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003692 return -ENOMEM;
3693
Jens Axboe65e19f52019-10-26 07:20:21 -06003694 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
3695 kfree(ctx->file_table);
Jens Axboe46568e92019-11-10 08:40:53 -07003696 ctx->file_table = NULL;
Jens Axboe65e19f52019-10-26 07:20:21 -06003697 return -ENOMEM;
3698 }
3699
Jens Axboe08a45172019-10-03 08:11:03 -06003700 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003701 struct fixed_file_table *table;
3702 unsigned index;
3703
Jens Axboe6b063142019-01-10 22:13:58 -07003704 ret = -EFAULT;
3705 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
3706 break;
Jens Axboe08a45172019-10-03 08:11:03 -06003707 /* allow sparse sets */
3708 if (fd == -1) {
3709 ret = 0;
3710 continue;
3711 }
Jens Axboe6b063142019-01-10 22:13:58 -07003712
Jens Axboe65e19f52019-10-26 07:20:21 -06003713 table = &ctx->file_table[i >> IORING_FILE_TABLE_SHIFT];
3714 index = i & IORING_FILE_TABLE_MASK;
3715 table->files[index] = fget(fd);
Jens Axboe6b063142019-01-10 22:13:58 -07003716
3717 ret = -EBADF;
Jens Axboe65e19f52019-10-26 07:20:21 -06003718 if (!table->files[index])
Jens Axboe6b063142019-01-10 22:13:58 -07003719 break;
3720 /*
3721 * Don't allow io_uring instances to be registered. If UNIX
3722 * isn't enabled, then this causes a reference cycle and this
3723 * instance can never get freed. If UNIX is enabled we'll
3724 * handle it just fine, but there's still no point in allowing
3725 * a ring fd as it doesn't support regular read/write anyway.
3726 */
Jens Axboe65e19f52019-10-26 07:20:21 -06003727 if (table->files[index]->f_op == &io_uring_fops) {
3728 fput(table->files[index]);
Jens Axboe6b063142019-01-10 22:13:58 -07003729 break;
3730 }
Jens Axboe6b063142019-01-10 22:13:58 -07003731 ret = 0;
3732 }
3733
3734 if (ret) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003735 for (i = 0; i < ctx->nr_user_files; i++) {
3736 struct file *file;
Jens Axboe6b063142019-01-10 22:13:58 -07003737
Jens Axboe65e19f52019-10-26 07:20:21 -06003738 file = io_file_from_index(ctx, i);
3739 if (file)
3740 fput(file);
3741 }
3742 for (i = 0; i < nr_tables; i++)
3743 kfree(ctx->file_table[i].files);
3744
3745 kfree(ctx->file_table);
3746 ctx->file_table = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003747 ctx->nr_user_files = 0;
3748 return ret;
3749 }
3750
3751 ret = io_sqe_files_scm(ctx);
3752 if (ret)
3753 io_sqe_files_unregister(ctx);
3754
3755 return ret;
3756}
3757
Jens Axboec3a31e62019-10-03 13:59:56 -06003758static void io_sqe_file_unregister(struct io_ring_ctx *ctx, int index)
3759{
3760#if defined(CONFIG_UNIX)
Jens Axboe65e19f52019-10-26 07:20:21 -06003761 struct file *file = io_file_from_index(ctx, index);
Jens Axboec3a31e62019-10-03 13:59:56 -06003762 struct sock *sock = ctx->ring_sock->sk;
3763 struct sk_buff_head list, *head = &sock->sk_receive_queue;
3764 struct sk_buff *skb;
3765 int i;
3766
3767 __skb_queue_head_init(&list);
3768
3769 /*
3770 * Find the skb that holds this file in its SCM_RIGHTS. When found,
3771 * remove this entry and rearrange the file array.
3772 */
3773 skb = skb_dequeue(head);
3774 while (skb) {
3775 struct scm_fp_list *fp;
3776
3777 fp = UNIXCB(skb).fp;
3778 for (i = 0; i < fp->count; i++) {
3779 int left;
3780
3781 if (fp->fp[i] != file)
3782 continue;
3783
3784 unix_notinflight(fp->user, fp->fp[i]);
3785 left = fp->count - 1 - i;
3786 if (left) {
3787 memmove(&fp->fp[i], &fp->fp[i + 1],
3788 left * sizeof(struct file *));
3789 }
3790 fp->count--;
3791 if (!fp->count) {
3792 kfree_skb(skb);
3793 skb = NULL;
3794 } else {
3795 __skb_queue_tail(&list, skb);
3796 }
3797 fput(file);
3798 file = NULL;
3799 break;
3800 }
3801
3802 if (!file)
3803 break;
3804
3805 __skb_queue_tail(&list, skb);
3806
3807 skb = skb_dequeue(head);
3808 }
3809
3810 if (skb_peek(&list)) {
3811 spin_lock_irq(&head->lock);
3812 while ((skb = __skb_dequeue(&list)) != NULL)
3813 __skb_queue_tail(head, skb);
3814 spin_unlock_irq(&head->lock);
3815 }
3816#else
Jens Axboe65e19f52019-10-26 07:20:21 -06003817 fput(io_file_from_index(ctx, index));
Jens Axboec3a31e62019-10-03 13:59:56 -06003818#endif
3819}
3820
3821static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
3822 int index)
3823{
3824#if defined(CONFIG_UNIX)
3825 struct sock *sock = ctx->ring_sock->sk;
3826 struct sk_buff_head *head = &sock->sk_receive_queue;
3827 struct sk_buff *skb;
3828
3829 /*
3830 * See if we can merge this file into an existing skb SCM_RIGHTS
3831 * file set. If there's no room, fall back to allocating a new skb
3832 * and filling it in.
3833 */
3834 spin_lock_irq(&head->lock);
3835 skb = skb_peek(head);
3836 if (skb) {
3837 struct scm_fp_list *fpl = UNIXCB(skb).fp;
3838
3839 if (fpl->count < SCM_MAX_FD) {
3840 __skb_unlink(skb, head);
3841 spin_unlock_irq(&head->lock);
3842 fpl->fp[fpl->count] = get_file(file);
3843 unix_inflight(fpl->user, fpl->fp[fpl->count]);
3844 fpl->count++;
3845 spin_lock_irq(&head->lock);
3846 __skb_queue_head(head, skb);
3847 } else {
3848 skb = NULL;
3849 }
3850 }
3851 spin_unlock_irq(&head->lock);
3852
3853 if (skb) {
3854 fput(file);
3855 return 0;
3856 }
3857
3858 return __io_sqe_files_scm(ctx, 1, index);
3859#else
3860 return 0;
3861#endif
3862}
3863
3864static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
3865 unsigned nr_args)
3866{
3867 struct io_uring_files_update up;
3868 __s32 __user *fds;
3869 int fd, i, err;
3870 __u32 done;
3871
Jens Axboe65e19f52019-10-26 07:20:21 -06003872 if (!ctx->file_table)
Jens Axboec3a31e62019-10-03 13:59:56 -06003873 return -ENXIO;
3874 if (!nr_args)
3875 return -EINVAL;
3876 if (copy_from_user(&up, arg, sizeof(up)))
3877 return -EFAULT;
3878 if (check_add_overflow(up.offset, nr_args, &done))
3879 return -EOVERFLOW;
3880 if (done > ctx->nr_user_files)
3881 return -EINVAL;
3882
3883 done = 0;
3884 fds = (__s32 __user *) up.fds;
3885 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003886 struct fixed_file_table *table;
3887 unsigned index;
3888
Jens Axboec3a31e62019-10-03 13:59:56 -06003889 err = 0;
3890 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
3891 err = -EFAULT;
3892 break;
3893 }
3894 i = array_index_nospec(up.offset, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06003895 table = &ctx->file_table[i >> IORING_FILE_TABLE_SHIFT];
3896 index = i & IORING_FILE_TABLE_MASK;
3897 if (table->files[index]) {
Jens Axboec3a31e62019-10-03 13:59:56 -06003898 io_sqe_file_unregister(ctx, i);
Jens Axboe65e19f52019-10-26 07:20:21 -06003899 table->files[index] = NULL;
Jens Axboec3a31e62019-10-03 13:59:56 -06003900 }
3901 if (fd != -1) {
3902 struct file *file;
3903
3904 file = fget(fd);
3905 if (!file) {
3906 err = -EBADF;
3907 break;
3908 }
3909 /*
3910 * Don't allow io_uring instances to be registered. If
3911 * UNIX isn't enabled, then this causes a reference
3912 * cycle and this instance can never get freed. If UNIX
3913 * is enabled we'll handle it just fine, but there's
3914 * still no point in allowing a ring fd as it doesn't
3915 * support regular read/write anyway.
3916 */
3917 if (file->f_op == &io_uring_fops) {
3918 fput(file);
3919 err = -EBADF;
3920 break;
3921 }
Jens Axboe65e19f52019-10-26 07:20:21 -06003922 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06003923 err = io_sqe_file_register(ctx, file, i);
3924 if (err)
3925 break;
3926 }
3927 nr_args--;
3928 done++;
3929 up.offset++;
3930 }
3931
3932 return done ? done : err;
3933}
3934
Jens Axboe7d723062019-11-12 22:31:31 -07003935static void io_put_work(struct io_wq_work *work)
3936{
3937 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
3938
3939 io_put_req(req);
3940}
3941
3942static void io_get_work(struct io_wq_work *work)
3943{
3944 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
3945
3946 refcount_inc(&req->refs);
3947}
3948
Jens Axboe6c271ce2019-01-10 11:22:30 -07003949static int io_sq_offload_start(struct io_ring_ctx *ctx,
3950 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003951{
Jens Axboe561fb042019-10-24 07:25:42 -06003952 unsigned concurrency;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003953 int ret;
3954
Jens Axboe6c271ce2019-01-10 11:22:30 -07003955 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003956 mmgrab(current->mm);
3957 ctx->sqo_mm = current->mm;
3958
Jens Axboe6c271ce2019-01-10 11:22:30 -07003959 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06003960 ret = -EPERM;
3961 if (!capable(CAP_SYS_ADMIN))
3962 goto err;
3963
Jens Axboe917257d2019-04-13 09:28:55 -06003964 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
3965 if (!ctx->sq_thread_idle)
3966 ctx->sq_thread_idle = HZ;
3967
Jens Axboe6c271ce2019-01-10 11:22:30 -07003968 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06003969 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003970
Jens Axboe917257d2019-04-13 09:28:55 -06003971 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06003972 if (cpu >= nr_cpu_ids)
3973 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08003974 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06003975 goto err;
3976
Jens Axboe6c271ce2019-01-10 11:22:30 -07003977 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
3978 ctx, cpu,
3979 "io_uring-sq");
3980 } else {
3981 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
3982 "io_uring-sq");
3983 }
3984 if (IS_ERR(ctx->sqo_thread)) {
3985 ret = PTR_ERR(ctx->sqo_thread);
3986 ctx->sqo_thread = NULL;
3987 goto err;
3988 }
3989 wake_up_process(ctx->sqo_thread);
3990 } else if (p->flags & IORING_SETUP_SQ_AFF) {
3991 /* Can't have SQ_AFF without SQPOLL */
3992 ret = -EINVAL;
3993 goto err;
3994 }
3995
Jens Axboe561fb042019-10-24 07:25:42 -06003996 /* Do QD, or 4 * CPUS, whatever is smallest */
3997 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
Jens Axboe7d723062019-11-12 22:31:31 -07003998 ctx->io_wq = io_wq_create(concurrency, ctx->sqo_mm, ctx->user,
3999 io_get_work, io_put_work);
Jens Axboe975c99a52019-10-30 08:42:56 -06004000 if (IS_ERR(ctx->io_wq)) {
4001 ret = PTR_ERR(ctx->io_wq);
4002 ctx->io_wq = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004003 goto err;
4004 }
4005
4006 return 0;
4007err:
Jens Axboe54a91f32019-09-10 09:15:04 -06004008 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004009 mmdrop(ctx->sqo_mm);
4010 ctx->sqo_mm = NULL;
4011 return ret;
4012}
4013
4014static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
4015{
4016 atomic_long_sub(nr_pages, &user->locked_vm);
4017}
4018
4019static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
4020{
4021 unsigned long page_limit, cur_pages, new_pages;
4022
4023 /* Don't allow more pages than we can safely lock */
4024 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
4025
4026 do {
4027 cur_pages = atomic_long_read(&user->locked_vm);
4028 new_pages = cur_pages + nr_pages;
4029 if (new_pages > page_limit)
4030 return -ENOMEM;
4031 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
4032 new_pages) != cur_pages);
4033
4034 return 0;
4035}
4036
4037static void io_mem_free(void *ptr)
4038{
Mark Rutland52e04ef2019-04-30 17:30:21 +01004039 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004040
Mark Rutland52e04ef2019-04-30 17:30:21 +01004041 if (!ptr)
4042 return;
4043
4044 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004045 if (put_page_testzero(page))
4046 free_compound_page(page);
4047}
4048
4049static void *io_mem_alloc(size_t size)
4050{
4051 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
4052 __GFP_NORETRY;
4053
4054 return (void *) __get_free_pages(gfp_flags, get_order(size));
4055}
4056
Hristo Venev75b28af2019-08-26 17:23:46 +00004057static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
4058 size_t *sq_offset)
4059{
4060 struct io_rings *rings;
4061 size_t off, sq_array_size;
4062
4063 off = struct_size(rings, cqes, cq_entries);
4064 if (off == SIZE_MAX)
4065 return SIZE_MAX;
4066
4067#ifdef CONFIG_SMP
4068 off = ALIGN(off, SMP_CACHE_BYTES);
4069 if (off == 0)
4070 return SIZE_MAX;
4071#endif
4072
4073 sq_array_size = array_size(sizeof(u32), sq_entries);
4074 if (sq_array_size == SIZE_MAX)
4075 return SIZE_MAX;
4076
4077 if (check_add_overflow(off, sq_array_size, &off))
4078 return SIZE_MAX;
4079
4080 if (sq_offset)
4081 *sq_offset = off;
4082
4083 return off;
4084}
4085
Jens Axboe2b188cc2019-01-07 10:46:33 -07004086static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
4087{
Hristo Venev75b28af2019-08-26 17:23:46 +00004088 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004089
Hristo Venev75b28af2019-08-26 17:23:46 +00004090 pages = (size_t)1 << get_order(
4091 rings_size(sq_entries, cq_entries, NULL));
4092 pages += (size_t)1 << get_order(
4093 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07004094
Hristo Venev75b28af2019-08-26 17:23:46 +00004095 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004096}
4097
Jens Axboeedafcce2019-01-09 09:16:05 -07004098static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
4099{
4100 int i, j;
4101
4102 if (!ctx->user_bufs)
4103 return -ENXIO;
4104
4105 for (i = 0; i < ctx->nr_user_bufs; i++) {
4106 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
4107
4108 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbard27c4d3a2019-08-04 19:32:06 -07004109 put_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07004110
4111 if (ctx->account_mem)
4112 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004113 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07004114 imu->nr_bvecs = 0;
4115 }
4116
4117 kfree(ctx->user_bufs);
4118 ctx->user_bufs = NULL;
4119 ctx->nr_user_bufs = 0;
4120 return 0;
4121}
4122
4123static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
4124 void __user *arg, unsigned index)
4125{
4126 struct iovec __user *src;
4127
4128#ifdef CONFIG_COMPAT
4129 if (ctx->compat) {
4130 struct compat_iovec __user *ciovs;
4131 struct compat_iovec ciov;
4132
4133 ciovs = (struct compat_iovec __user *) arg;
4134 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
4135 return -EFAULT;
4136
4137 dst->iov_base = (void __user *) (unsigned long) ciov.iov_base;
4138 dst->iov_len = ciov.iov_len;
4139 return 0;
4140 }
4141#endif
4142 src = (struct iovec __user *) arg;
4143 if (copy_from_user(dst, &src[index], sizeof(*dst)))
4144 return -EFAULT;
4145 return 0;
4146}
4147
4148static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
4149 unsigned nr_args)
4150{
4151 struct vm_area_struct **vmas = NULL;
4152 struct page **pages = NULL;
4153 int i, j, got_pages = 0;
4154 int ret = -EINVAL;
4155
4156 if (ctx->user_bufs)
4157 return -EBUSY;
4158 if (!nr_args || nr_args > UIO_MAXIOV)
4159 return -EINVAL;
4160
4161 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
4162 GFP_KERNEL);
4163 if (!ctx->user_bufs)
4164 return -ENOMEM;
4165
4166 for (i = 0; i < nr_args; i++) {
4167 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
4168 unsigned long off, start, end, ubuf;
4169 int pret, nr_pages;
4170 struct iovec iov;
4171 size_t size;
4172
4173 ret = io_copy_iov(ctx, &iov, arg, i);
4174 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03004175 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07004176
4177 /*
4178 * Don't impose further limits on the size and buffer
4179 * constraints here, we'll -EINVAL later when IO is
4180 * submitted if they are wrong.
4181 */
4182 ret = -EFAULT;
4183 if (!iov.iov_base || !iov.iov_len)
4184 goto err;
4185
4186 /* arbitrary limit, but we need something */
4187 if (iov.iov_len > SZ_1G)
4188 goto err;
4189
4190 ubuf = (unsigned long) iov.iov_base;
4191 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
4192 start = ubuf >> PAGE_SHIFT;
4193 nr_pages = end - start;
4194
4195 if (ctx->account_mem) {
4196 ret = io_account_mem(ctx->user, nr_pages);
4197 if (ret)
4198 goto err;
4199 }
4200
4201 ret = 0;
4202 if (!pages || nr_pages > got_pages) {
4203 kfree(vmas);
4204 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004205 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07004206 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004207 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07004208 sizeof(struct vm_area_struct *),
4209 GFP_KERNEL);
4210 if (!pages || !vmas) {
4211 ret = -ENOMEM;
4212 if (ctx->account_mem)
4213 io_unaccount_mem(ctx->user, nr_pages);
4214 goto err;
4215 }
4216 got_pages = nr_pages;
4217 }
4218
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004219 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07004220 GFP_KERNEL);
4221 ret = -ENOMEM;
4222 if (!imu->bvec) {
4223 if (ctx->account_mem)
4224 io_unaccount_mem(ctx->user, nr_pages);
4225 goto err;
4226 }
4227
4228 ret = 0;
4229 down_read(&current->mm->mmap_sem);
Ira Weiny932f4a62019-05-13 17:17:03 -07004230 pret = get_user_pages(ubuf, nr_pages,
4231 FOLL_WRITE | FOLL_LONGTERM,
4232 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07004233 if (pret == nr_pages) {
4234 /* don't support file backed memory */
4235 for (j = 0; j < nr_pages; j++) {
4236 struct vm_area_struct *vma = vmas[j];
4237
4238 if (vma->vm_file &&
4239 !is_file_hugepages(vma->vm_file)) {
4240 ret = -EOPNOTSUPP;
4241 break;
4242 }
4243 }
4244 } else {
4245 ret = pret < 0 ? pret : -EFAULT;
4246 }
4247 up_read(&current->mm->mmap_sem);
4248 if (ret) {
4249 /*
4250 * if we did partial map, or found file backed vmas,
4251 * release any pages we did get
4252 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07004253 if (pret > 0)
4254 put_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07004255 if (ctx->account_mem)
4256 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004257 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07004258 goto err;
4259 }
4260
4261 off = ubuf & ~PAGE_MASK;
4262 size = iov.iov_len;
4263 for (j = 0; j < nr_pages; j++) {
4264 size_t vec_len;
4265
4266 vec_len = min_t(size_t, size, PAGE_SIZE - off);
4267 imu->bvec[j].bv_page = pages[j];
4268 imu->bvec[j].bv_len = vec_len;
4269 imu->bvec[j].bv_offset = off;
4270 off = 0;
4271 size -= vec_len;
4272 }
4273 /* store original address for later verification */
4274 imu->ubuf = ubuf;
4275 imu->len = iov.iov_len;
4276 imu->nr_bvecs = nr_pages;
4277
4278 ctx->nr_user_bufs++;
4279 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004280 kvfree(pages);
4281 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07004282 return 0;
4283err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004284 kvfree(pages);
4285 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07004286 io_sqe_buffer_unregister(ctx);
4287 return ret;
4288}
4289
Jens Axboe9b402842019-04-11 11:45:41 -06004290static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
4291{
4292 __s32 __user *fds = arg;
4293 int fd;
4294
4295 if (ctx->cq_ev_fd)
4296 return -EBUSY;
4297
4298 if (copy_from_user(&fd, fds, sizeof(*fds)))
4299 return -EFAULT;
4300
4301 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
4302 if (IS_ERR(ctx->cq_ev_fd)) {
4303 int ret = PTR_ERR(ctx->cq_ev_fd);
4304 ctx->cq_ev_fd = NULL;
4305 return ret;
4306 }
4307
4308 return 0;
4309}
4310
4311static int io_eventfd_unregister(struct io_ring_ctx *ctx)
4312{
4313 if (ctx->cq_ev_fd) {
4314 eventfd_ctx_put(ctx->cq_ev_fd);
4315 ctx->cq_ev_fd = NULL;
4316 return 0;
4317 }
4318
4319 return -ENXIO;
4320}
4321
Jens Axboe2b188cc2019-01-07 10:46:33 -07004322static void io_ring_ctx_free(struct io_ring_ctx *ctx)
4323{
Jens Axboe6b063142019-01-10 22:13:58 -07004324 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004325 if (ctx->sqo_mm)
4326 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07004327
4328 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07004329 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07004330 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06004331 io_eventfd_unregister(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07004332
Jens Axboe2b188cc2019-01-07 10:46:33 -07004333#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07004334 if (ctx->ring_sock) {
4335 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004336 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07004337 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004338#endif
4339
Hristo Venev75b28af2019-08-26 17:23:46 +00004340 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004341 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004342
4343 percpu_ref_exit(&ctx->refs);
4344 if (ctx->account_mem)
4345 io_unaccount_mem(ctx->user,
4346 ring_pages(ctx->sq_entries, ctx->cq_entries));
4347 free_uid(ctx->user);
Jens Axboe206aefd2019-11-07 18:27:42 -07004348 kfree(ctx->completions);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07004349 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004350 kfree(ctx);
4351}
4352
4353static __poll_t io_uring_poll(struct file *file, poll_table *wait)
4354{
4355 struct io_ring_ctx *ctx = file->private_data;
4356 __poll_t mask = 0;
4357
4358 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02004359 /*
4360 * synchronizes with barrier from wq_has_sleeper call in
4361 * io_commit_cqring
4362 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004363 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00004364 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
4365 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004366 mask |= EPOLLOUT | EPOLLWRNORM;
yangerkundaa5de52019-09-24 20:53:34 +08004367 if (READ_ONCE(ctx->rings->cq.head) != ctx->cached_cq_tail)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004368 mask |= EPOLLIN | EPOLLRDNORM;
4369
4370 return mask;
4371}
4372
4373static int io_uring_fasync(int fd, struct file *file, int on)
4374{
4375 struct io_ring_ctx *ctx = file->private_data;
4376
4377 return fasync_helper(fd, file, on, &ctx->cq_fasync);
4378}
4379
4380static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
4381{
4382 mutex_lock(&ctx->uring_lock);
4383 percpu_ref_kill(&ctx->refs);
4384 mutex_unlock(&ctx->uring_lock);
4385
Jens Axboe5262f562019-09-17 12:26:57 -06004386 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004387 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06004388
4389 if (ctx->io_wq)
4390 io_wq_cancel_all(ctx->io_wq);
4391
Jens Axboedef596e2019-01-09 08:59:42 -07004392 io_iopoll_reap_events(ctx);
Jens Axboe15dff282019-11-13 09:09:23 -07004393 /* if we failed setting up the ctx, we might not have any rings */
4394 if (ctx->rings)
4395 io_cqring_overflow_flush(ctx, true);
Jens Axboe206aefd2019-11-07 18:27:42 -07004396 wait_for_completion(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004397 io_ring_ctx_free(ctx);
4398}
4399
4400static int io_uring_release(struct inode *inode, struct file *file)
4401{
4402 struct io_ring_ctx *ctx = file->private_data;
4403
4404 file->private_data = NULL;
4405 io_ring_ctx_wait_and_kill(ctx);
4406 return 0;
4407}
4408
Jens Axboefcb323c2019-10-24 12:39:47 -06004409static void io_uring_cancel_files(struct io_ring_ctx *ctx,
4410 struct files_struct *files)
4411{
4412 struct io_kiocb *req;
4413 DEFINE_WAIT(wait);
4414
4415 while (!list_empty_careful(&ctx->inflight_list)) {
Jens Axboe768134d2019-11-10 20:30:53 -07004416 struct io_kiocb *cancel_req = NULL;
Jens Axboefcb323c2019-10-24 12:39:47 -06004417
4418 spin_lock_irq(&ctx->inflight_lock);
4419 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07004420 if (req->work.files != files)
4421 continue;
4422 /* req is being completed, ignore */
4423 if (!refcount_inc_not_zero(&req->refs))
4424 continue;
4425 cancel_req = req;
4426 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06004427 }
Jens Axboe768134d2019-11-10 20:30:53 -07004428 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06004429 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07004430 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06004431 spin_unlock_irq(&ctx->inflight_lock);
4432
Jens Axboe768134d2019-11-10 20:30:53 -07004433 /* We need to keep going until we don't find a matching req */
4434 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06004435 break;
Bob Liu2f6d9b92019-11-13 18:06:24 +08004436
4437 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
4438 io_put_req(cancel_req);
Jens Axboefcb323c2019-10-24 12:39:47 -06004439 schedule();
4440 }
Jens Axboe768134d2019-11-10 20:30:53 -07004441 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06004442}
4443
4444static int io_uring_flush(struct file *file, void *data)
4445{
4446 struct io_ring_ctx *ctx = file->private_data;
4447
4448 io_uring_cancel_files(ctx, data);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004449 if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) {
4450 io_cqring_overflow_flush(ctx, true);
Jens Axboefcb323c2019-10-24 12:39:47 -06004451 io_wq_cancel_all(ctx->io_wq);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004452 }
Jens Axboefcb323c2019-10-24 12:39:47 -06004453 return 0;
4454}
4455
Jens Axboe2b188cc2019-01-07 10:46:33 -07004456static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
4457{
4458 loff_t offset = (loff_t) vma->vm_pgoff << PAGE_SHIFT;
4459 unsigned long sz = vma->vm_end - vma->vm_start;
4460 struct io_ring_ctx *ctx = file->private_data;
4461 unsigned long pfn;
4462 struct page *page;
4463 void *ptr;
4464
4465 switch (offset) {
4466 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00004467 case IORING_OFF_CQ_RING:
4468 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004469 break;
4470 case IORING_OFF_SQES:
4471 ptr = ctx->sq_sqes;
4472 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004473 default:
4474 return -EINVAL;
4475 }
4476
4477 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07004478 if (sz > page_size(page))
Jens Axboe2b188cc2019-01-07 10:46:33 -07004479 return -EINVAL;
4480
4481 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
4482 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
4483}
4484
4485SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
4486 u32, min_complete, u32, flags, const sigset_t __user *, sig,
4487 size_t, sigsz)
4488{
4489 struct io_ring_ctx *ctx;
4490 long ret = -EBADF;
4491 int submitted = 0;
4492 struct fd f;
4493
Jens Axboe6c271ce2019-01-10 11:22:30 -07004494 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07004495 return -EINVAL;
4496
4497 f = fdget(fd);
4498 if (!f.file)
4499 return -EBADF;
4500
4501 ret = -EOPNOTSUPP;
4502 if (f.file->f_op != &io_uring_fops)
4503 goto out_fput;
4504
4505 ret = -ENXIO;
4506 ctx = f.file->private_data;
4507 if (!percpu_ref_tryget(&ctx->refs))
4508 goto out_fput;
4509
Jens Axboe6c271ce2019-01-10 11:22:30 -07004510 /*
4511 * For SQ polling, the thread will do all submissions and completions.
4512 * Just return the requested submit count, and wake the thread if
4513 * we were asked to.
4514 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06004515 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004516 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07004517 if (!list_empty_careful(&ctx->cq_overflow_list))
4518 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004519 if (flags & IORING_ENTER_SQ_WAKEUP)
4520 wake_up(&ctx->sqo_wait);
4521 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06004522 } else if (to_submit) {
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004523 struct mm_struct *cur_mm;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004524
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004525 to_submit = min(to_submit, ctx->sq_entries);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004526 mutex_lock(&ctx->uring_lock);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004527 /* already have mm, so io_submit_sqes() won't try to grab it */
4528 cur_mm = ctx->sqo_mm;
4529 submitted = io_submit_sqes(ctx, to_submit, f.file, fd,
4530 &cur_mm, false);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004531 mutex_unlock(&ctx->uring_lock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004532 }
4533 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07004534 unsigned nr_events = 0;
4535
Jens Axboe2b188cc2019-01-07 10:46:33 -07004536 min_complete = min(min_complete, ctx->cq_entries);
4537
Jens Axboedef596e2019-01-09 08:59:42 -07004538 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07004539 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07004540 } else {
4541 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
4542 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004543 }
4544
Pavel Begunkov6805b322019-10-08 02:18:42 +03004545 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004546out_fput:
4547 fdput(f);
4548 return submitted ? submitted : ret;
4549}
4550
4551static const struct file_operations io_uring_fops = {
4552 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06004553 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07004554 .mmap = io_uring_mmap,
4555 .poll = io_uring_poll,
4556 .fasync = io_uring_fasync,
4557};
4558
4559static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
4560 struct io_uring_params *p)
4561{
Hristo Venev75b28af2019-08-26 17:23:46 +00004562 struct io_rings *rings;
4563 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004564
Hristo Venev75b28af2019-08-26 17:23:46 +00004565 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
4566 if (size == SIZE_MAX)
4567 return -EOVERFLOW;
4568
4569 rings = io_mem_alloc(size);
4570 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004571 return -ENOMEM;
4572
Hristo Venev75b28af2019-08-26 17:23:46 +00004573 ctx->rings = rings;
4574 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
4575 rings->sq_ring_mask = p->sq_entries - 1;
4576 rings->cq_ring_mask = p->cq_entries - 1;
4577 rings->sq_ring_entries = p->sq_entries;
4578 rings->cq_ring_entries = p->cq_entries;
4579 ctx->sq_mask = rings->sq_ring_mask;
4580 ctx->cq_mask = rings->cq_ring_mask;
4581 ctx->sq_entries = rings->sq_ring_entries;
4582 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004583
4584 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07004585 if (size == SIZE_MAX) {
4586 io_mem_free(ctx->rings);
4587 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004588 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07004589 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004590
4591 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07004592 if (!ctx->sq_sqes) {
4593 io_mem_free(ctx->rings);
4594 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004595 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07004596 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004597
Jens Axboe2b188cc2019-01-07 10:46:33 -07004598 return 0;
4599}
4600
4601/*
4602 * Allocate an anonymous fd, this is what constitutes the application
4603 * visible backing of an io_uring instance. The application mmaps this
4604 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
4605 * we have to tie this fd to a socket for file garbage collection purposes.
4606 */
4607static int io_uring_get_fd(struct io_ring_ctx *ctx)
4608{
4609 struct file *file;
4610 int ret;
4611
4612#if defined(CONFIG_UNIX)
4613 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
4614 &ctx->ring_sock);
4615 if (ret)
4616 return ret;
4617#endif
4618
4619 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
4620 if (ret < 0)
4621 goto err;
4622
4623 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
4624 O_RDWR | O_CLOEXEC);
4625 if (IS_ERR(file)) {
4626 put_unused_fd(ret);
4627 ret = PTR_ERR(file);
4628 goto err;
4629 }
4630
4631#if defined(CONFIG_UNIX)
4632 ctx->ring_sock->file = file;
Jens Axboe6b063142019-01-10 22:13:58 -07004633 ctx->ring_sock->sk->sk_user_data = ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004634#endif
4635 fd_install(ret, file);
4636 return ret;
4637err:
4638#if defined(CONFIG_UNIX)
4639 sock_release(ctx->ring_sock);
4640 ctx->ring_sock = NULL;
4641#endif
4642 return ret;
4643}
4644
4645static int io_uring_create(unsigned entries, struct io_uring_params *p)
4646{
4647 struct user_struct *user = NULL;
4648 struct io_ring_ctx *ctx;
4649 bool account_mem;
4650 int ret;
4651
4652 if (!entries || entries > IORING_MAX_ENTRIES)
4653 return -EINVAL;
4654
4655 /*
4656 * Use twice as many entries for the CQ ring. It's possible for the
4657 * application to drive a higher depth than the size of the SQ ring,
4658 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06004659 * some flexibility in overcommitting a bit. If the application has
4660 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
4661 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07004662 */
4663 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06004664 if (p->flags & IORING_SETUP_CQSIZE) {
4665 /*
4666 * If IORING_SETUP_CQSIZE is set, we do the same roundup
4667 * to a power-of-two, if it isn't already. We do NOT impose
4668 * any cq vs sq ring sizing.
4669 */
4670 if (p->cq_entries < p->sq_entries || p->cq_entries > IORING_MAX_CQ_ENTRIES)
4671 return -EINVAL;
4672 p->cq_entries = roundup_pow_of_two(p->cq_entries);
4673 } else {
4674 p->cq_entries = 2 * p->sq_entries;
4675 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004676
4677 user = get_uid(current_user());
4678 account_mem = !capable(CAP_IPC_LOCK);
4679
4680 if (account_mem) {
4681 ret = io_account_mem(user,
4682 ring_pages(p->sq_entries, p->cq_entries));
4683 if (ret) {
4684 free_uid(user);
4685 return ret;
4686 }
4687 }
4688
4689 ctx = io_ring_ctx_alloc(p);
4690 if (!ctx) {
4691 if (account_mem)
4692 io_unaccount_mem(user, ring_pages(p->sq_entries,
4693 p->cq_entries));
4694 free_uid(user);
4695 return -ENOMEM;
4696 }
4697 ctx->compat = in_compat_syscall();
4698 ctx->account_mem = account_mem;
4699 ctx->user = user;
4700
4701 ret = io_allocate_scq_urings(ctx, p);
4702 if (ret)
4703 goto err;
4704
Jens Axboe6c271ce2019-01-10 11:22:30 -07004705 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004706 if (ret)
4707 goto err;
4708
Jens Axboe2b188cc2019-01-07 10:46:33 -07004709 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00004710 p->sq_off.head = offsetof(struct io_rings, sq.head);
4711 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
4712 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
4713 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
4714 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
4715 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
4716 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004717
4718 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00004719 p->cq_off.head = offsetof(struct io_rings, cq.head);
4720 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
4721 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
4722 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
4723 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
4724 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06004725
Jens Axboe044c1ab2019-10-28 09:15:33 -06004726 /*
4727 * Install ring fd as the very last thing, so we don't risk someone
4728 * having closed it before we finish setup
4729 */
4730 ret = io_uring_get_fd(ctx);
4731 if (ret < 0)
4732 goto err;
4733
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004734 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004735 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004736 return ret;
4737err:
4738 io_ring_ctx_wait_and_kill(ctx);
4739 return ret;
4740}
4741
4742/*
4743 * Sets up an aio uring context, and returns the fd. Applications asks for a
4744 * ring size, we return the actual sq/cq ring sizes (among other things) in the
4745 * params structure passed in.
4746 */
4747static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
4748{
4749 struct io_uring_params p;
4750 long ret;
4751 int i;
4752
4753 if (copy_from_user(&p, params, sizeof(p)))
4754 return -EFAULT;
4755 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
4756 if (p.resv[i])
4757 return -EINVAL;
4758 }
4759
Jens Axboe6c271ce2019-01-10 11:22:30 -07004760 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe33a107f2019-10-04 12:10:03 -06004761 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE))
Jens Axboe2b188cc2019-01-07 10:46:33 -07004762 return -EINVAL;
4763
4764 ret = io_uring_create(entries, &p);
4765 if (ret < 0)
4766 return ret;
4767
4768 if (copy_to_user(params, &p, sizeof(p)))
4769 return -EFAULT;
4770
4771 return ret;
4772}
4773
4774SYSCALL_DEFINE2(io_uring_setup, u32, entries,
4775 struct io_uring_params __user *, params)
4776{
4777 return io_uring_setup(entries, params);
4778}
4779
Jens Axboeedafcce2019-01-09 09:16:05 -07004780static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
4781 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06004782 __releases(ctx->uring_lock)
4783 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07004784{
4785 int ret;
4786
Jens Axboe35fa71a2019-04-22 10:23:23 -06004787 /*
4788 * We're inside the ring mutex, if the ref is already dying, then
4789 * someone else killed the ctx or is already going through
4790 * io_uring_register().
4791 */
4792 if (percpu_ref_is_dying(&ctx->refs))
4793 return -ENXIO;
4794
Jens Axboeedafcce2019-01-09 09:16:05 -07004795 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06004796
4797 /*
4798 * Drop uring mutex before waiting for references to exit. If another
4799 * thread is currently inside io_uring_enter() it might need to grab
4800 * the uring_lock to make progress. If we hold it here across the drain
4801 * wait, then we can deadlock. It's safe to drop the mutex here, since
4802 * no new references will come in after we've killed the percpu ref.
4803 */
4804 mutex_unlock(&ctx->uring_lock);
Jens Axboe206aefd2019-11-07 18:27:42 -07004805 wait_for_completion(&ctx->completions[0]);
Jens Axboeb19062a2019-04-15 10:49:38 -06004806 mutex_lock(&ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07004807
4808 switch (opcode) {
4809 case IORING_REGISTER_BUFFERS:
4810 ret = io_sqe_buffer_register(ctx, arg, nr_args);
4811 break;
4812 case IORING_UNREGISTER_BUFFERS:
4813 ret = -EINVAL;
4814 if (arg || nr_args)
4815 break;
4816 ret = io_sqe_buffer_unregister(ctx);
4817 break;
Jens Axboe6b063142019-01-10 22:13:58 -07004818 case IORING_REGISTER_FILES:
4819 ret = io_sqe_files_register(ctx, arg, nr_args);
4820 break;
4821 case IORING_UNREGISTER_FILES:
4822 ret = -EINVAL;
4823 if (arg || nr_args)
4824 break;
4825 ret = io_sqe_files_unregister(ctx);
4826 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06004827 case IORING_REGISTER_FILES_UPDATE:
4828 ret = io_sqe_files_update(ctx, arg, nr_args);
4829 break;
Jens Axboe9b402842019-04-11 11:45:41 -06004830 case IORING_REGISTER_EVENTFD:
4831 ret = -EINVAL;
4832 if (nr_args != 1)
4833 break;
4834 ret = io_eventfd_register(ctx, arg);
4835 break;
4836 case IORING_UNREGISTER_EVENTFD:
4837 ret = -EINVAL;
4838 if (arg || nr_args)
4839 break;
4840 ret = io_eventfd_unregister(ctx);
4841 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07004842 default:
4843 ret = -EINVAL;
4844 break;
4845 }
4846
4847 /* bring the ctx back to life */
Jens Axboe206aefd2019-11-07 18:27:42 -07004848 reinit_completion(&ctx->completions[0]);
Jens Axboeedafcce2019-01-09 09:16:05 -07004849 percpu_ref_reinit(&ctx->refs);
4850 return ret;
4851}
4852
4853SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
4854 void __user *, arg, unsigned int, nr_args)
4855{
4856 struct io_ring_ctx *ctx;
4857 long ret = -EBADF;
4858 struct fd f;
4859
4860 f = fdget(fd);
4861 if (!f.file)
4862 return -EBADF;
4863
4864 ret = -EOPNOTSUPP;
4865 if (f.file->f_op != &io_uring_fops)
4866 goto out_fput;
4867
4868 ctx = f.file->private_data;
4869
4870 mutex_lock(&ctx->uring_lock);
4871 ret = __io_uring_register(ctx, opcode, arg, nr_args);
4872 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004873 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
4874 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07004875out_fput:
4876 fdput(f);
4877 return ret;
4878}
4879
Jens Axboe2b188cc2019-01-07 10:46:33 -07004880static int __init io_uring_init(void)
4881{
4882 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
4883 return 0;
4884};
4885__initcall(io_uring_init);