blob: fabae84396bc2338d409a1404970a491dcf34e09 [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)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03001625 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06001626 ssize_t nr;
1627
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03001628 if (!iov_iter_is_bvec(iter)) {
1629 iovec = iov_iter_iovec(iter);
1630 } else {
1631 /* fixed buffers import bvec */
1632 iovec.iov_base = kmap(iter->bvec->bv_page)
1633 + iter->iov_offset;
1634 iovec.iov_len = min(iter->count,
1635 iter->bvec->bv_len - iter->iov_offset);
1636 }
1637
Jens Axboe32960612019-09-23 11:05:34 -06001638 if (rw == READ) {
1639 nr = file->f_op->read(file, iovec.iov_base,
1640 iovec.iov_len, &kiocb->ki_pos);
1641 } else {
1642 nr = file->f_op->write(file, iovec.iov_base,
1643 iovec.iov_len, &kiocb->ki_pos);
1644 }
1645
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03001646 if (iov_iter_is_bvec(iter))
1647 kunmap(iter->bvec->bv_page);
1648
Jens Axboe32960612019-09-23 11:05:34 -06001649 if (nr < 0) {
1650 if (!ret)
1651 ret = nr;
1652 break;
1653 }
1654 ret += nr;
1655 if (nr != iovec.iov_len)
1656 break;
1657 iov_iter_advance(iter, nr);
1658 }
1659
1660 return ret;
1661}
1662
Pavel Begunkov267bc902019-11-07 01:41:08 +03001663static int io_read(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001664 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001665{
1666 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1667 struct kiocb *kiocb = &req->rw;
1668 struct iov_iter iter;
1669 struct file *file;
Jens Axboe31b51512019-01-18 22:56:34 -07001670 size_t iov_count;
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001671 ssize_t read_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001672
Pavel Begunkov267bc902019-11-07 01:41:08 +03001673 ret = io_prep_rw(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001674 if (ret)
1675 return ret;
1676 file = kiocb->ki_filp;
1677
Jens Axboe2b188cc2019-01-07 10:46:33 -07001678 if (unlikely(!(file->f_mode & FMODE_READ)))
Jens Axboe09bb8392019-03-13 12:39:28 -06001679 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001680
Pavel Begunkov267bc902019-11-07 01:41:08 +03001681 ret = io_import_iovec(req->ctx, READ, &req->submit, &iovec, &iter);
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001682 if (ret < 0)
Jens Axboe09bb8392019-03-13 12:39:28 -06001683 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001684
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001685 read_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06001686 if (req->flags & REQ_F_LINK)
1687 req->result = read_size;
1688
Jens Axboe31b51512019-01-18 22:56:34 -07001689 iov_count = iov_iter_count(&iter);
1690 ret = rw_verify_area(READ, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001691 if (!ret) {
1692 ssize_t ret2;
1693
Jens Axboe32960612019-09-23 11:05:34 -06001694 if (file->f_op->read_iter)
1695 ret2 = call_read_iter(file, kiocb, &iter);
1696 else
1697 ret2 = loop_rw_iter(READ, file, kiocb, &iter);
1698
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001699 /*
1700 * In case of a short read, punt to async. This can happen
1701 * if we have data partially cached. Alternatively we can
1702 * return the short read, in which case the application will
1703 * need to issue another SQE and wait for it. That SQE will
1704 * need async punt anyway, so it's more efficient to do it
1705 * here.
1706 */
Jens Axboe491381ce2019-10-17 09:20:46 -06001707 if (force_nonblock && !(req->flags & REQ_F_NOWAIT) &&
1708 (req->flags & REQ_F_ISREG) &&
1709 ret2 > 0 && ret2 < read_size)
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001710 ret2 = -EAGAIN;
1711 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboe561fb042019-10-24 07:25:42 -06001712 if (!force_nonblock || ret2 != -EAGAIN)
Pavel Begunkov267bc902019-11-07 01:41:08 +03001713 kiocb_done(kiocb, ret2, nxt, req->submit.in_async);
Jens Axboe561fb042019-10-24 07:25:42 -06001714 else
Jens Axboe2b188cc2019-01-07 10:46:33 -07001715 ret = -EAGAIN;
1716 }
1717 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001718 return ret;
1719}
1720
Pavel Begunkov267bc902019-11-07 01:41:08 +03001721static int io_write(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001722 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001723{
1724 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1725 struct kiocb *kiocb = &req->rw;
1726 struct iov_iter iter;
1727 struct file *file;
Jens Axboe31b51512019-01-18 22:56:34 -07001728 size_t iov_count;
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001729 ssize_t ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001730
Pavel Begunkov267bc902019-11-07 01:41:08 +03001731 ret = io_prep_rw(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001732 if (ret)
1733 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001734
Jens Axboe2b188cc2019-01-07 10:46:33 -07001735 file = kiocb->ki_filp;
1736 if (unlikely(!(file->f_mode & FMODE_WRITE)))
Jens Axboe09bb8392019-03-13 12:39:28 -06001737 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001738
Pavel Begunkov267bc902019-11-07 01:41:08 +03001739 ret = io_import_iovec(req->ctx, WRITE, &req->submit, &iovec, &iter);
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001740 if (ret < 0)
Jens Axboe09bb8392019-03-13 12:39:28 -06001741 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001742
Jens Axboe9e645e112019-05-10 16:07:28 -06001743 if (req->flags & REQ_F_LINK)
1744 req->result = ret;
1745
Jens Axboe31b51512019-01-18 22:56:34 -07001746 iov_count = iov_iter_count(&iter);
1747
1748 ret = -EAGAIN;
Jens Axboe561fb042019-10-24 07:25:42 -06001749 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT))
Jens Axboe31b51512019-01-18 22:56:34 -07001750 goto out_free;
Jens Axboe31b51512019-01-18 22:56:34 -07001751
1752 ret = rw_verify_area(WRITE, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001753 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01001754 ssize_t ret2;
1755
Jens Axboe2b188cc2019-01-07 10:46:33 -07001756 /*
1757 * Open-code file_start_write here to grab freeze protection,
1758 * which will be released by another thread in
1759 * io_complete_rw(). Fool lockdep by telling it the lock got
1760 * released so that it doesn't complain about the held lock when
1761 * we return to userspace.
1762 */
Jens Axboe491381ce2019-10-17 09:20:46 -06001763 if (req->flags & REQ_F_ISREG) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001764 __sb_start_write(file_inode(file)->i_sb,
1765 SB_FREEZE_WRITE, true);
1766 __sb_writers_release(file_inode(file)->i_sb,
1767 SB_FREEZE_WRITE);
1768 }
1769 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01001770
Jens Axboe32960612019-09-23 11:05:34 -06001771 if (file->f_op->write_iter)
1772 ret2 = call_write_iter(file, kiocb, &iter);
1773 else
1774 ret2 = loop_rw_iter(WRITE, file, kiocb, &iter);
Jens Axboe561fb042019-10-24 07:25:42 -06001775 if (!force_nonblock || ret2 != -EAGAIN)
Pavel Begunkov267bc902019-11-07 01:41:08 +03001776 kiocb_done(kiocb, ret2, nxt, req->submit.in_async);
Jens Axboe561fb042019-10-24 07:25:42 -06001777 else
Roman Penyaev9bf79332019-03-25 20:09:24 +01001778 ret = -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001779 }
Jens Axboe31b51512019-01-18 22:56:34 -07001780out_free:
Jens Axboe2b188cc2019-01-07 10:46:33 -07001781 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001782 return ret;
1783}
1784
1785/*
1786 * IORING_OP_NOP just posts a completion event, nothing else.
1787 */
Jens Axboe78e19bb2019-11-06 15:21:34 -07001788static int io_nop(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001789{
1790 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001791
Jens Axboedef596e2019-01-09 08:59:42 -07001792 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1793 return -EINVAL;
1794
Jens Axboe78e19bb2019-11-06 15:21:34 -07001795 io_cqring_add_event(req, 0);
Jens Axboee65ef562019-03-12 10:16:44 -06001796 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001797 return 0;
1798}
1799
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001800static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1801{
Jens Axboe6b063142019-01-10 22:13:58 -07001802 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001803
Jens Axboe09bb8392019-03-13 12:39:28 -06001804 if (!req->file)
1805 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001806
Jens Axboe6b063142019-01-10 22:13:58 -07001807 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07001808 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07001809 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001810 return -EINVAL;
1811
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001812 return 0;
1813}
1814
1815static int io_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001816 struct io_kiocb **nxt, bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001817{
1818 loff_t sqe_off = READ_ONCE(sqe->off);
1819 loff_t sqe_len = READ_ONCE(sqe->len);
1820 loff_t end = sqe_off + sqe_len;
1821 unsigned fsync_flags;
1822 int ret;
1823
1824 fsync_flags = READ_ONCE(sqe->fsync_flags);
1825 if (unlikely(fsync_flags & ~IORING_FSYNC_DATASYNC))
1826 return -EINVAL;
1827
1828 ret = io_prep_fsync(req, sqe);
1829 if (ret)
1830 return ret;
1831
1832 /* fsync always requires a blocking context */
1833 if (force_nonblock)
1834 return -EAGAIN;
1835
1836 ret = vfs_fsync_range(req->rw.ki_filp, sqe_off,
1837 end > 0 ? end : LLONG_MAX,
1838 fsync_flags & IORING_FSYNC_DATASYNC);
1839
Jens Axboe9e645e112019-05-10 16:07:28 -06001840 if (ret < 0 && (req->flags & REQ_F_LINK))
1841 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001842 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001843 io_put_req_find_next(req, nxt);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001844 return 0;
1845}
1846
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001847static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1848{
1849 struct io_ring_ctx *ctx = req->ctx;
1850 int ret = 0;
1851
1852 if (!req->file)
1853 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001854
1855 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1856 return -EINVAL;
1857 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
1858 return -EINVAL;
1859
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001860 return ret;
1861}
1862
1863static int io_sync_file_range(struct io_kiocb *req,
1864 const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001865 struct io_kiocb **nxt,
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001866 bool force_nonblock)
1867{
1868 loff_t sqe_off;
1869 loff_t sqe_len;
1870 unsigned flags;
1871 int ret;
1872
1873 ret = io_prep_sfr(req, sqe);
1874 if (ret)
1875 return ret;
1876
1877 /* sync_file_range always requires a blocking context */
1878 if (force_nonblock)
1879 return -EAGAIN;
1880
1881 sqe_off = READ_ONCE(sqe->off);
1882 sqe_len = READ_ONCE(sqe->len);
1883 flags = READ_ONCE(sqe->sync_range_flags);
1884
1885 ret = sync_file_range(req->rw.ki_filp, sqe_off, sqe_len, flags);
1886
Jens Axboe9e645e112019-05-10 16:07:28 -06001887 if (ret < 0 && (req->flags & REQ_F_LINK))
1888 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001889 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001890 io_put_req_find_next(req, nxt);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001891 return 0;
1892}
1893
Jens Axboe0fa03c62019-04-19 13:34:07 -06001894#if defined(CONFIG_NET)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001895static int io_send_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001896 struct io_kiocb **nxt, bool force_nonblock,
Jens Axboeaa1fa282019-04-19 13:38:09 -06001897 long (*fn)(struct socket *, struct user_msghdr __user *,
1898 unsigned int))
1899{
Jens Axboe0fa03c62019-04-19 13:34:07 -06001900 struct socket *sock;
1901 int ret;
1902
1903 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1904 return -EINVAL;
1905
1906 sock = sock_from_file(req->file, &ret);
1907 if (sock) {
1908 struct user_msghdr __user *msg;
1909 unsigned flags;
1910
1911 flags = READ_ONCE(sqe->msg_flags);
1912 if (flags & MSG_DONTWAIT)
1913 req->flags |= REQ_F_NOWAIT;
1914 else if (force_nonblock)
1915 flags |= MSG_DONTWAIT;
1916
1917 msg = (struct user_msghdr __user *) (unsigned long)
1918 READ_ONCE(sqe->addr);
1919
Jens Axboeaa1fa282019-04-19 13:38:09 -06001920 ret = fn(sock, msg, flags);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001921 if (force_nonblock && ret == -EAGAIN)
1922 return ret;
1923 }
1924
Jens Axboe78e19bb2019-11-06 15:21:34 -07001925 io_cqring_add_event(req, ret);
Jens Axboef1f40852019-11-05 20:33:16 -07001926 if (ret < 0 && (req->flags & REQ_F_LINK))
1927 req->flags |= REQ_F_FAIL_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001928 io_put_req_find_next(req, nxt);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001929 return 0;
Jens Axboeaa1fa282019-04-19 13:38:09 -06001930}
1931#endif
1932
1933static int io_sendmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001934 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001935{
1936#if defined(CONFIG_NET)
Jens Axboeba816ad2019-09-28 11:36:45 -06001937 return io_send_recvmsg(req, sqe, nxt, force_nonblock,
1938 __sys_sendmsg_sock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06001939#else
1940 return -EOPNOTSUPP;
1941#endif
1942}
1943
1944static int io_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001945 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001946{
1947#if defined(CONFIG_NET)
Jens Axboeba816ad2019-09-28 11:36:45 -06001948 return io_send_recvmsg(req, sqe, nxt, force_nonblock,
1949 __sys_recvmsg_sock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001950#else
1951 return -EOPNOTSUPP;
1952#endif
1953}
1954
Jens Axboe17f2fe32019-10-17 14:42:58 -06001955static int io_accept(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1956 struct io_kiocb **nxt, bool force_nonblock)
1957{
1958#if defined(CONFIG_NET)
1959 struct sockaddr __user *addr;
1960 int __user *addr_len;
1961 unsigned file_flags;
1962 int flags, ret;
1963
1964 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
1965 return -EINVAL;
1966 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
1967 return -EINVAL;
1968
1969 addr = (struct sockaddr __user *) (unsigned long) READ_ONCE(sqe->addr);
1970 addr_len = (int __user *) (unsigned long) READ_ONCE(sqe->addr2);
1971 flags = READ_ONCE(sqe->accept_flags);
1972 file_flags = force_nonblock ? O_NONBLOCK : 0;
1973
1974 ret = __sys_accept4_file(req->file, file_flags, addr, addr_len, flags);
1975 if (ret == -EAGAIN && force_nonblock) {
1976 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
1977 return -EAGAIN;
1978 }
Jens Axboe8e3cca12019-11-09 19:52:33 -07001979 if (ret == -ERESTARTSYS)
1980 ret = -EINTR;
Jens Axboe17f2fe32019-10-17 14:42:58 -06001981 if (ret < 0 && (req->flags & REQ_F_LINK))
1982 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001983 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001984 io_put_req_find_next(req, nxt);
Jens Axboe17f2fe32019-10-17 14:42:58 -06001985 return 0;
1986#else
1987 return -EOPNOTSUPP;
1988#endif
1989}
1990
Jens Axboef8e85cf2019-11-23 14:24:24 -07001991static int io_connect(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1992 struct io_kiocb **nxt, bool force_nonblock)
1993{
1994#if defined(CONFIG_NET)
1995 struct sockaddr __user *addr;
1996 unsigned file_flags;
1997 int addr_len, ret;
1998
1999 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
2000 return -EINVAL;
2001 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
2002 return -EINVAL;
2003
2004 addr = (struct sockaddr __user *) (unsigned long) READ_ONCE(sqe->addr);
2005 addr_len = READ_ONCE(sqe->addr2);
2006 file_flags = force_nonblock ? O_NONBLOCK : 0;
2007
2008 ret = __sys_connect_file(req->file, addr, addr_len, file_flags);
2009 if (ret == -EAGAIN && force_nonblock)
2010 return -EAGAIN;
2011 if (ret == -ERESTARTSYS)
2012 ret = -EINTR;
2013 if (ret < 0 && (req->flags & REQ_F_LINK))
2014 req->flags |= REQ_F_FAIL_LINK;
2015 io_cqring_add_event(req, ret);
2016 io_put_req_find_next(req, nxt);
2017 return 0;
2018#else
2019 return -EOPNOTSUPP;
2020#endif
2021}
2022
Jens Axboeeac406c2019-11-14 12:09:58 -07002023static inline void io_poll_remove_req(struct io_kiocb *req)
2024{
2025 if (!RB_EMPTY_NODE(&req->rb_node)) {
2026 rb_erase(&req->rb_node, &req->ctx->cancel_tree);
2027 RB_CLEAR_NODE(&req->rb_node);
2028 }
2029}
2030
Jens Axboe221c5eb2019-01-17 09:41:58 -07002031static void io_poll_remove_one(struct io_kiocb *req)
2032{
2033 struct io_poll_iocb *poll = &req->poll;
2034
2035 spin_lock(&poll->head->lock);
2036 WRITE_ONCE(poll->canceled, true);
2037 if (!list_empty(&poll->wait.entry)) {
2038 list_del_init(&poll->wait.entry);
Jackie Liua197f662019-11-08 08:09:12 -07002039 io_queue_async_work(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002040 }
2041 spin_unlock(&poll->head->lock);
Jens Axboeeac406c2019-11-14 12:09:58 -07002042 io_poll_remove_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002043}
2044
2045static void io_poll_remove_all(struct io_ring_ctx *ctx)
2046{
Jens Axboeeac406c2019-11-14 12:09:58 -07002047 struct rb_node *node;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002048 struct io_kiocb *req;
2049
2050 spin_lock_irq(&ctx->completion_lock);
Jens Axboeeac406c2019-11-14 12:09:58 -07002051 while ((node = rb_first(&ctx->cancel_tree)) != NULL) {
2052 req = rb_entry(node, struct io_kiocb, rb_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002053 io_poll_remove_one(req);
2054 }
2055 spin_unlock_irq(&ctx->completion_lock);
2056}
2057
Jens Axboe47f46762019-11-09 17:43:02 -07002058static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
2059{
Jens Axboeeac406c2019-11-14 12:09:58 -07002060 struct rb_node *p, *parent = NULL;
Jens Axboe47f46762019-11-09 17:43:02 -07002061 struct io_kiocb *req;
2062
Jens Axboeeac406c2019-11-14 12:09:58 -07002063 p = ctx->cancel_tree.rb_node;
2064 while (p) {
2065 parent = p;
2066 req = rb_entry(parent, struct io_kiocb, rb_node);
2067 if (sqe_addr < req->user_data) {
2068 p = p->rb_left;
2069 } else if (sqe_addr > req->user_data) {
2070 p = p->rb_right;
2071 } else {
2072 io_poll_remove_one(req);
2073 return 0;
2074 }
Jens Axboe47f46762019-11-09 17:43:02 -07002075 }
2076
2077 return -ENOENT;
2078}
2079
Jens Axboe221c5eb2019-01-17 09:41:58 -07002080/*
2081 * Find a running poll command that matches one specified in sqe->addr,
2082 * and remove it if found.
2083 */
2084static int io_poll_remove(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2085{
2086 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07002087 int ret;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002088
2089 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2090 return -EINVAL;
2091 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
2092 sqe->poll_events)
2093 return -EINVAL;
2094
2095 spin_lock_irq(&ctx->completion_lock);
Jens Axboe47f46762019-11-09 17:43:02 -07002096 ret = io_poll_cancel(ctx, READ_ONCE(sqe->addr));
Jens Axboe221c5eb2019-01-17 09:41:58 -07002097 spin_unlock_irq(&ctx->completion_lock);
2098
Jens Axboe78e19bb2019-11-06 15:21:34 -07002099 io_cqring_add_event(req, ret);
Jens Axboef1f40852019-11-05 20:33:16 -07002100 if (ret < 0 && (req->flags & REQ_F_LINK))
2101 req->flags |= REQ_F_FAIL_LINK;
Jens Axboee65ef562019-03-12 10:16:44 -06002102 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002103 return 0;
2104}
2105
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002106static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002107{
Jackie Liua197f662019-11-08 08:09:12 -07002108 struct io_ring_ctx *ctx = req->ctx;
2109
Jens Axboe8c838782019-03-12 15:48:16 -06002110 req->poll.done = true;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002111 if (error)
2112 io_cqring_fill_event(req, error);
2113 else
2114 io_cqring_fill_event(req, mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06002115 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002116}
2117
Jens Axboe561fb042019-10-24 07:25:42 -06002118static void io_poll_complete_work(struct io_wq_work **workptr)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002119{
Jens Axboe561fb042019-10-24 07:25:42 -06002120 struct io_wq_work *work = *workptr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002121 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
2122 struct io_poll_iocb *poll = &req->poll;
2123 struct poll_table_struct pt = { ._key = poll->events };
2124 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe89723d02019-11-05 15:32:58 -07002125 struct io_kiocb *nxt = NULL;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002126 __poll_t mask = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002127 int ret = 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002128
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002129 if (work->flags & IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06002130 WRITE_ONCE(poll->canceled, true);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002131 ret = -ECANCELED;
2132 } else if (READ_ONCE(poll->canceled)) {
2133 ret = -ECANCELED;
2134 }
Jens Axboe561fb042019-10-24 07:25:42 -06002135
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002136 if (ret != -ECANCELED)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002137 mask = vfs_poll(poll->file, &pt) & poll->events;
2138
2139 /*
2140 * Note that ->ki_cancel callers also delete iocb from active_reqs after
2141 * calling ->ki_cancel. We need the ctx_lock roundtrip here to
2142 * synchronize with them. In the cancellation case the list_del_init
2143 * itself is not actually needed, but harmless so we keep it in to
2144 * avoid further branches in the fast path.
2145 */
2146 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002147 if (!mask && ret != -ECANCELED) {
Jens Axboe221c5eb2019-01-17 09:41:58 -07002148 add_wait_queue(poll->head, &poll->wait);
2149 spin_unlock_irq(&ctx->completion_lock);
2150 return;
2151 }
Jens Axboeeac406c2019-11-14 12:09:58 -07002152 io_poll_remove_req(req);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002153 io_poll_complete(req, mask, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002154 spin_unlock_irq(&ctx->completion_lock);
2155
Jens Axboe8c838782019-03-12 15:48:16 -06002156 io_cqring_ev_posted(ctx);
Jens Axboe89723d02019-11-05 15:32:58 -07002157
Jens Axboefba38c22019-11-18 12:27:57 -07002158 if (ret < 0 && req->flags & REQ_F_LINK)
2159 req->flags |= REQ_F_FAIL_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08002160 io_put_req_find_next(req, &nxt);
Jens Axboe89723d02019-11-05 15:32:58 -07002161 if (nxt)
2162 *workptr = &nxt->work;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002163}
2164
2165static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
2166 void *key)
2167{
2168 struct io_poll_iocb *poll = container_of(wait, struct io_poll_iocb,
2169 wait);
2170 struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
2171 struct io_ring_ctx *ctx = req->ctx;
2172 __poll_t mask = key_to_poll(key);
Jens Axboe8c838782019-03-12 15:48:16 -06002173 unsigned long flags;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002174
2175 /* for instances that support it check for an event match first: */
Jens Axboe8c838782019-03-12 15:48:16 -06002176 if (mask && !(mask & poll->events))
2177 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002178
2179 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06002180
Jens Axboe7c9e7f02019-11-12 08:15:53 -07002181 /*
2182 * Run completion inline if we can. We're using trylock here because
2183 * we are violating the completion_lock -> poll wq lock ordering.
2184 * If we have a link timeout we're going to need the completion_lock
2185 * for finalizing the request, mark us as having grabbed that already.
2186 */
Jens Axboe8c838782019-03-12 15:48:16 -06002187 if (mask && spin_trylock_irqsave(&ctx->completion_lock, flags)) {
Jens Axboeeac406c2019-11-14 12:09:58 -07002188 io_poll_remove_req(req);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002189 io_poll_complete(req, mask, 0);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07002190 req->flags |= REQ_F_COMP_LOCKED;
2191 io_put_req(req);
Jens Axboe8c838782019-03-12 15:48:16 -06002192 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2193
2194 io_cqring_ev_posted(ctx);
Jens Axboe8c838782019-03-12 15:48:16 -06002195 } else {
Jackie Liua197f662019-11-08 08:09:12 -07002196 io_queue_async_work(req);
Jens Axboe8c838782019-03-12 15:48:16 -06002197 }
2198
Jens Axboe221c5eb2019-01-17 09:41:58 -07002199 return 1;
2200}
2201
2202struct io_poll_table {
2203 struct poll_table_struct pt;
2204 struct io_kiocb *req;
2205 int error;
2206};
2207
2208static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
2209 struct poll_table_struct *p)
2210{
2211 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
2212
2213 if (unlikely(pt->req->poll.head)) {
2214 pt->error = -EINVAL;
2215 return;
2216 }
2217
2218 pt->error = 0;
2219 pt->req->poll.head = head;
2220 add_wait_queue(head, &pt->req->poll.wait);
2221}
2222
Jens Axboeeac406c2019-11-14 12:09:58 -07002223static void io_poll_req_insert(struct io_kiocb *req)
2224{
2225 struct io_ring_ctx *ctx = req->ctx;
2226 struct rb_node **p = &ctx->cancel_tree.rb_node;
2227 struct rb_node *parent = NULL;
2228 struct io_kiocb *tmp;
2229
2230 while (*p) {
2231 parent = *p;
2232 tmp = rb_entry(parent, struct io_kiocb, rb_node);
2233 if (req->user_data < tmp->user_data)
2234 p = &(*p)->rb_left;
2235 else
2236 p = &(*p)->rb_right;
2237 }
2238 rb_link_node(&req->rb_node, parent, p);
2239 rb_insert_color(&req->rb_node, &ctx->cancel_tree);
2240}
2241
Jens Axboe89723d02019-11-05 15:32:58 -07002242static int io_poll_add(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2243 struct io_kiocb **nxt)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002244{
2245 struct io_poll_iocb *poll = &req->poll;
2246 struct io_ring_ctx *ctx = req->ctx;
2247 struct io_poll_table ipt;
Jens Axboe8c838782019-03-12 15:48:16 -06002248 bool cancel = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002249 __poll_t mask;
2250 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002251
2252 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2253 return -EINVAL;
2254 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
2255 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06002256 if (!poll->file)
2257 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002258
Jens Axboe6cc47d12019-09-18 11:18:23 -06002259 req->submit.sqe = NULL;
Jens Axboe561fb042019-10-24 07:25:42 -06002260 INIT_IO_WORK(&req->work, io_poll_complete_work);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002261 events = READ_ONCE(sqe->poll_events);
2262 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
Jens Axboeeac406c2019-11-14 12:09:58 -07002263 RB_CLEAR_NODE(&req->rb_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002264
Jens Axboe221c5eb2019-01-17 09:41:58 -07002265 poll->head = NULL;
Jens Axboe8c838782019-03-12 15:48:16 -06002266 poll->done = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002267 poll->canceled = false;
2268
2269 ipt.pt._qproc = io_poll_queue_proc;
2270 ipt.pt._key = poll->events;
2271 ipt.req = req;
2272 ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
2273
2274 /* initialized the list so that we can do list_empty checks */
2275 INIT_LIST_HEAD(&poll->wait.entry);
2276 init_waitqueue_func_entry(&poll->wait, io_poll_wake);
2277
Jens Axboe36703242019-07-25 10:20:18 -06002278 INIT_LIST_HEAD(&req->list);
2279
Jens Axboe221c5eb2019-01-17 09:41:58 -07002280 mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002281
2282 spin_lock_irq(&ctx->completion_lock);
Jens Axboe8c838782019-03-12 15:48:16 -06002283 if (likely(poll->head)) {
2284 spin_lock(&poll->head->lock);
2285 if (unlikely(list_empty(&poll->wait.entry))) {
2286 if (ipt.error)
2287 cancel = true;
2288 ipt.error = 0;
2289 mask = 0;
2290 }
2291 if (mask || ipt.error)
2292 list_del_init(&poll->wait.entry);
2293 else if (cancel)
2294 WRITE_ONCE(poll->canceled, true);
2295 else if (!poll->done) /* actually waiting for an event */
Jens Axboeeac406c2019-11-14 12:09:58 -07002296 io_poll_req_insert(req);
Jens Axboe8c838782019-03-12 15:48:16 -06002297 spin_unlock(&poll->head->lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002298 }
Jens Axboe8c838782019-03-12 15:48:16 -06002299 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06002300 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002301 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06002302 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07002303 spin_unlock_irq(&ctx->completion_lock);
2304
Jens Axboe8c838782019-03-12 15:48:16 -06002305 if (mask) {
2306 io_cqring_ev_posted(ctx);
Jackie Liuec9c02a2019-11-08 23:50:36 +08002307 io_put_req_find_next(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002308 }
Jens Axboe8c838782019-03-12 15:48:16 -06002309 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002310}
2311
Jens Axboe5262f562019-09-17 12:26:57 -06002312static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
2313{
Jens Axboead8a48a2019-11-15 08:49:11 -07002314 struct io_timeout_data *data = container_of(timer,
2315 struct io_timeout_data, timer);
2316 struct io_kiocb *req = data->req;
2317 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06002318 unsigned long flags;
2319
Jens Axboe5262f562019-09-17 12:26:57 -06002320 atomic_inc(&ctx->cq_timeouts);
2321
2322 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08002323 /*
Jens Axboe11365042019-10-16 09:08:32 -06002324 * We could be racing with timeout deletion. If the list is empty,
2325 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08002326 */
Jens Axboe842f9612019-10-29 12:34:10 -06002327 if (!list_empty(&req->list)) {
Jens Axboe11365042019-10-16 09:08:32 -06002328 struct io_kiocb *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06002329
Jens Axboe11365042019-10-16 09:08:32 -06002330 /*
2331 * Adjust the reqs sequence before the current one because it
2332 * will consume a slot in the cq_ring and the the cq_tail
2333 * pointer will be increased, otherwise other timeout reqs may
2334 * return in advance without waiting for enough wait_nr.
2335 */
2336 prev = req;
2337 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
2338 prev->sequence++;
Jens Axboe11365042019-10-16 09:08:32 -06002339 list_del_init(&req->list);
Jens Axboe11365042019-10-16 09:08:32 -06002340 }
Jens Axboe842f9612019-10-29 12:34:10 -06002341
Jens Axboe78e19bb2019-11-06 15:21:34 -07002342 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06002343 io_commit_cqring(ctx);
2344 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2345
2346 io_cqring_ev_posted(ctx);
Jens Axboef1f40852019-11-05 20:33:16 -07002347 if (req->flags & REQ_F_LINK)
2348 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe5262f562019-09-17 12:26:57 -06002349 io_put_req(req);
2350 return HRTIMER_NORESTART;
2351}
2352
Jens Axboe47f46762019-11-09 17:43:02 -07002353static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
2354{
2355 struct io_kiocb *req;
2356 int ret = -ENOENT;
2357
2358 list_for_each_entry(req, &ctx->timeout_list, list) {
2359 if (user_data == req->user_data) {
2360 list_del_init(&req->list);
2361 ret = 0;
2362 break;
2363 }
2364 }
2365
2366 if (ret == -ENOENT)
2367 return ret;
2368
Jens Axboead8a48a2019-11-15 08:49:11 -07002369 ret = hrtimer_try_to_cancel(&req->timeout.data->timer);
Jens Axboe47f46762019-11-09 17:43:02 -07002370 if (ret == -1)
2371 return -EALREADY;
2372
Jens Axboefba38c22019-11-18 12:27:57 -07002373 if (req->flags & REQ_F_LINK)
2374 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe47f46762019-11-09 17:43:02 -07002375 io_cqring_fill_event(req, -ECANCELED);
2376 io_put_req(req);
2377 return 0;
2378}
2379
Jens Axboe11365042019-10-16 09:08:32 -06002380/*
2381 * Remove or update an existing timeout command
2382 */
2383static int io_timeout_remove(struct io_kiocb *req,
2384 const struct io_uring_sqe *sqe)
2385{
2386 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe11365042019-10-16 09:08:32 -06002387 unsigned flags;
Jens Axboe47f46762019-11-09 17:43:02 -07002388 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06002389
2390 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2391 return -EINVAL;
2392 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
2393 return -EINVAL;
2394 flags = READ_ONCE(sqe->timeout_flags);
2395 if (flags)
2396 return -EINVAL;
2397
Jens Axboe11365042019-10-16 09:08:32 -06002398 spin_lock_irq(&ctx->completion_lock);
Jens Axboe47f46762019-11-09 17:43:02 -07002399 ret = io_timeout_cancel(ctx, READ_ONCE(sqe->addr));
Jens Axboe11365042019-10-16 09:08:32 -06002400
Jens Axboe47f46762019-11-09 17:43:02 -07002401 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06002402 io_commit_cqring(ctx);
2403 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06002404 io_cqring_ev_posted(ctx);
Jens Axboe47f46762019-11-09 17:43:02 -07002405 if (ret < 0 && req->flags & REQ_F_LINK)
2406 req->flags |= REQ_F_FAIL_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08002407 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06002408 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06002409}
2410
Jens Axboead8a48a2019-11-15 08:49:11 -07002411static int io_timeout_setup(struct io_kiocb *req)
Jens Axboe5262f562019-09-17 12:26:57 -06002412{
Jens Axboead8a48a2019-11-15 08:49:11 -07002413 const struct io_uring_sqe *sqe = req->submit.sqe;
2414 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06002415 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06002416
Jens Axboead8a48a2019-11-15 08:49:11 -07002417 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06002418 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07002419 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06002420 return -EINVAL;
2421 flags = READ_ONCE(sqe->timeout_flags);
2422 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06002423 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06002424
Jens Axboead8a48a2019-11-15 08:49:11 -07002425 data = kzalloc(sizeof(struct io_timeout_data), GFP_KERNEL);
2426 if (!data)
2427 return -ENOMEM;
2428 data->req = req;
2429 req->timeout.data = data;
2430 req->flags |= REQ_F_TIMEOUT;
2431
2432 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06002433 return -EFAULT;
2434
Jens Axboe11365042019-10-16 09:08:32 -06002435 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07002436 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06002437 else
Jens Axboead8a48a2019-11-15 08:49:11 -07002438 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06002439
Jens Axboead8a48a2019-11-15 08:49:11 -07002440 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
2441 return 0;
2442}
2443
2444static int io_timeout(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2445{
2446 unsigned count;
2447 struct io_ring_ctx *ctx = req->ctx;
2448 struct io_timeout_data *data;
2449 struct list_head *entry;
2450 unsigned span = 0;
2451 int ret;
2452
2453 ret = io_timeout_setup(req);
2454 /* common setup allows flags (like links) set, we don't */
2455 if (!ret && sqe->flags)
2456 ret = -EINVAL;
2457 if (ret)
2458 return ret;
Jens Axboe93bd25b2019-11-11 23:34:31 -07002459
Jens Axboe5262f562019-09-17 12:26:57 -06002460 /*
2461 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07002462 * timeout event to be satisfied. If it isn't set, then this is
2463 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06002464 */
2465 count = READ_ONCE(sqe->off);
Jens Axboe93bd25b2019-11-11 23:34:31 -07002466 if (!count) {
2467 req->flags |= REQ_F_TIMEOUT_NOSEQ;
2468 spin_lock_irq(&ctx->completion_lock);
2469 entry = ctx->timeout_list.prev;
2470 goto add;
2471 }
Jens Axboe5262f562019-09-17 12:26:57 -06002472
2473 req->sequence = ctx->cached_sq_head + count - 1;
yangerkun5da0fb12019-10-15 21:59:29 +08002474 /* reuse it to store the count */
2475 req->submit.sequence = count;
Jens Axboe5262f562019-09-17 12:26:57 -06002476
2477 /*
2478 * Insertion sort, ensuring the first entry in the list is always
2479 * the one we need first.
2480 */
Jens Axboe5262f562019-09-17 12:26:57 -06002481 spin_lock_irq(&ctx->completion_lock);
2482 list_for_each_prev(entry, &ctx->timeout_list) {
2483 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
yangerkun5da0fb12019-10-15 21:59:29 +08002484 unsigned nxt_sq_head;
2485 long long tmp, tmp_nxt;
Jens Axboe5262f562019-09-17 12:26:57 -06002486
Jens Axboe93bd25b2019-11-11 23:34:31 -07002487 if (nxt->flags & REQ_F_TIMEOUT_NOSEQ)
2488 continue;
2489
yangerkun5da0fb12019-10-15 21:59:29 +08002490 /*
2491 * Since cached_sq_head + count - 1 can overflow, use type long
2492 * long to store it.
2493 */
2494 tmp = (long long)ctx->cached_sq_head + count - 1;
2495 nxt_sq_head = nxt->sequence - nxt->submit.sequence + 1;
2496 tmp_nxt = (long long)nxt_sq_head + nxt->submit.sequence - 1;
2497
2498 /*
2499 * cached_sq_head may overflow, and it will never overflow twice
2500 * once there is some timeout req still be valid.
2501 */
2502 if (ctx->cached_sq_head < nxt_sq_head)
yangerkun8b07a652019-10-17 12:12:35 +08002503 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08002504
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002505 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06002506 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002507
2508 /*
2509 * Sequence of reqs after the insert one and itself should
2510 * be adjusted because each timeout req consumes a slot.
2511 */
2512 span++;
2513 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06002514 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002515 req->sequence -= span;
Jens Axboe93bd25b2019-11-11 23:34:31 -07002516add:
Jens Axboe5262f562019-09-17 12:26:57 -06002517 list_add(&req->list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07002518 data = req->timeout.data;
2519 data->timer.function = io_timeout_fn;
2520 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06002521 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06002522 return 0;
2523}
2524
Jens Axboe62755e32019-10-28 21:49:21 -06002525static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06002526{
Jens Axboe62755e32019-10-28 21:49:21 -06002527 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06002528
Jens Axboe62755e32019-10-28 21:49:21 -06002529 return req->user_data == (unsigned long) data;
2530}
2531
Jens Axboee977d6d2019-11-05 12:39:45 -07002532static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06002533{
Jens Axboe62755e32019-10-28 21:49:21 -06002534 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06002535 int ret = 0;
2536
Jens Axboe62755e32019-10-28 21:49:21 -06002537 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
2538 switch (cancel_ret) {
2539 case IO_WQ_CANCEL_OK:
2540 ret = 0;
2541 break;
2542 case IO_WQ_CANCEL_RUNNING:
2543 ret = -EALREADY;
2544 break;
2545 case IO_WQ_CANCEL_NOTFOUND:
2546 ret = -ENOENT;
2547 break;
2548 }
2549
Jens Axboee977d6d2019-11-05 12:39:45 -07002550 return ret;
2551}
2552
Jens Axboe47f46762019-11-09 17:43:02 -07002553static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
2554 struct io_kiocb *req, __u64 sqe_addr,
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002555 struct io_kiocb **nxt, int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07002556{
2557 unsigned long flags;
2558 int ret;
2559
2560 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
2561 if (ret != -ENOENT) {
2562 spin_lock_irqsave(&ctx->completion_lock, flags);
2563 goto done;
2564 }
2565
2566 spin_lock_irqsave(&ctx->completion_lock, flags);
2567 ret = io_timeout_cancel(ctx, sqe_addr);
2568 if (ret != -ENOENT)
2569 goto done;
2570 ret = io_poll_cancel(ctx, sqe_addr);
2571done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002572 if (!ret)
2573 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07002574 io_cqring_fill_event(req, ret);
2575 io_commit_cqring(ctx);
2576 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2577 io_cqring_ev_posted(ctx);
2578
2579 if (ret < 0 && (req->flags & REQ_F_LINK))
2580 req->flags |= REQ_F_FAIL_LINK;
2581 io_put_req_find_next(req, nxt);
2582}
2583
Jens Axboee977d6d2019-11-05 12:39:45 -07002584static int io_async_cancel(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2585 struct io_kiocb **nxt)
2586{
2587 struct io_ring_ctx *ctx = req->ctx;
Jens Axboee977d6d2019-11-05 12:39:45 -07002588
2589 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2590 return -EINVAL;
2591 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
2592 sqe->cancel_flags)
2593 return -EINVAL;
2594
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002595 io_async_find_and_cancel(ctx, req, READ_ONCE(sqe->addr), nxt, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06002596 return 0;
2597}
2598
Jackie Liua197f662019-11-08 08:09:12 -07002599static int io_req_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -06002600{
Pavel Begunkov267bc902019-11-07 01:41:08 +03002601 const struct io_uring_sqe *sqe = req->submit.sqe;
Jens Axboede0617e2019-04-06 21:51:27 -06002602 struct io_uring_sqe *sqe_copy;
Jackie Liua197f662019-11-08 08:09:12 -07002603 struct io_ring_ctx *ctx = req->ctx;
Jens Axboede0617e2019-04-06 21:51:27 -06002604
Bob Liu9d858b22019-11-13 18:06:25 +08002605 /* Still need defer if there is pending req in defer list. */
2606 if (!req_need_defer(req) && list_empty(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06002607 return 0;
2608
2609 sqe_copy = kmalloc(sizeof(*sqe_copy), GFP_KERNEL);
2610 if (!sqe_copy)
2611 return -EAGAIN;
2612
2613 spin_lock_irq(&ctx->completion_lock);
Bob Liu9d858b22019-11-13 18:06:25 +08002614 if (!req_need_defer(req) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06002615 spin_unlock_irq(&ctx->completion_lock);
2616 kfree(sqe_copy);
2617 return 0;
2618 }
2619
2620 memcpy(sqe_copy, sqe, sizeof(*sqe_copy));
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03002621 req->flags |= REQ_F_FREE_SQE;
Jens Axboede0617e2019-04-06 21:51:27 -06002622 req->submit.sqe = sqe_copy;
2623
Jens Axboe915967f2019-11-21 09:01:20 -07002624 trace_io_uring_defer(ctx, req, req->user_data);
Jens Axboede0617e2019-04-06 21:51:27 -06002625 list_add_tail(&req->list, &ctx->defer_list);
2626 spin_unlock_irq(&ctx->completion_lock);
2627 return -EIOCBQUEUED;
2628}
2629
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03002630__attribute__((nonnull))
Pavel Begunkovd7324472019-11-21 21:24:36 +03002631static int io_issue_sqe(struct io_kiocb *req, struct io_kiocb **nxt,
2632 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002633{
Jens Axboee0c5c572019-03-12 10:18:47 -06002634 int ret, opcode;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002635 struct sqe_submit *s = &req->submit;
Jackie Liua197f662019-11-08 08:09:12 -07002636 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002637
2638 opcode = READ_ONCE(s->sqe->opcode);
2639 switch (opcode) {
2640 case IORING_OP_NOP:
Jens Axboe78e19bb2019-11-06 15:21:34 -07002641 ret = io_nop(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002642 break;
2643 case IORING_OP_READV:
Jens Axboeedafcce2019-01-09 09:16:05 -07002644 if (unlikely(s->sqe->buf_index))
2645 return -EINVAL;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002646 ret = io_read(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002647 break;
2648 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07002649 if (unlikely(s->sqe->buf_index))
2650 return -EINVAL;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002651 ret = io_write(req, nxt, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07002652 break;
2653 case IORING_OP_READ_FIXED:
Pavel Begunkov267bc902019-11-07 01:41:08 +03002654 ret = io_read(req, nxt, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07002655 break;
2656 case IORING_OP_WRITE_FIXED:
Pavel Begunkov267bc902019-11-07 01:41:08 +03002657 ret = io_write(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002658 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002659 case IORING_OP_FSYNC:
Jens Axboeba816ad2019-09-28 11:36:45 -06002660 ret = io_fsync(req, s->sqe, nxt, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002661 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002662 case IORING_OP_POLL_ADD:
Jens Axboe89723d02019-11-05 15:32:58 -07002663 ret = io_poll_add(req, s->sqe, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002664 break;
2665 case IORING_OP_POLL_REMOVE:
2666 ret = io_poll_remove(req, s->sqe);
2667 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002668 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboeba816ad2019-09-28 11:36:45 -06002669 ret = io_sync_file_range(req, s->sqe, nxt, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002670 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002671 case IORING_OP_SENDMSG:
Jens Axboeba816ad2019-09-28 11:36:45 -06002672 ret = io_sendmsg(req, s->sqe, nxt, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06002673 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06002674 case IORING_OP_RECVMSG:
Jens Axboeba816ad2019-09-28 11:36:45 -06002675 ret = io_recvmsg(req, s->sqe, nxt, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06002676 break;
Jens Axboe5262f562019-09-17 12:26:57 -06002677 case IORING_OP_TIMEOUT:
2678 ret = io_timeout(req, s->sqe);
2679 break;
Jens Axboe11365042019-10-16 09:08:32 -06002680 case IORING_OP_TIMEOUT_REMOVE:
2681 ret = io_timeout_remove(req, s->sqe);
2682 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06002683 case IORING_OP_ACCEPT:
2684 ret = io_accept(req, s->sqe, nxt, force_nonblock);
2685 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07002686 case IORING_OP_CONNECT:
2687 ret = io_connect(req, s->sqe, nxt, force_nonblock);
2688 break;
Jens Axboe62755e32019-10-28 21:49:21 -06002689 case IORING_OP_ASYNC_CANCEL:
2690 ret = io_async_cancel(req, s->sqe, nxt);
2691 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002692 default:
2693 ret = -EINVAL;
2694 break;
2695 }
2696
Jens Axboedef596e2019-01-09 08:59:42 -07002697 if (ret)
2698 return ret;
2699
2700 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe9e645e112019-05-10 16:07:28 -06002701 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07002702 return -EAGAIN;
2703
2704 /* workqueue context doesn't hold uring_lock, grab it now */
Jackie Liuba5290c2019-10-09 09:19:59 +08002705 if (s->in_async)
Jens Axboedef596e2019-01-09 08:59:42 -07002706 mutex_lock(&ctx->uring_lock);
2707 io_iopoll_req_issued(req);
Jackie Liuba5290c2019-10-09 09:19:59 +08002708 if (s->in_async)
Jens Axboedef596e2019-01-09 08:59:42 -07002709 mutex_unlock(&ctx->uring_lock);
2710 }
2711
2712 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002713}
2714
Jens Axboeb76da702019-11-20 13:05:32 -07002715static void io_link_work_cb(struct io_wq_work **workptr)
2716{
2717 struct io_wq_work *work = *workptr;
2718 struct io_kiocb *link = work->data;
2719
2720 io_queue_linked_timeout(link);
2721 work->func = io_wq_submit_work;
2722}
2723
Jens Axboe561fb042019-10-24 07:25:42 -06002724static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboe31b51512019-01-18 22:56:34 -07002725{
Jens Axboe561fb042019-10-24 07:25:42 -06002726 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002727 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe561fb042019-10-24 07:25:42 -06002728 struct sqe_submit *s = &req->submit;
Jens Axboe561fb042019-10-24 07:25:42 -06002729 struct io_kiocb *nxt = NULL;
2730 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002731
Jens Axboe561fb042019-10-24 07:25:42 -06002732 /* Ensure we clear previously set non-block flag */
2733 req->rw.ki_flags &= ~IOCB_NOWAIT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002734
Jens Axboe561fb042019-10-24 07:25:42 -06002735 if (work->flags & IO_WQ_WORK_CANCEL)
2736 ret = -ECANCELED;
Jens Axboe31b51512019-01-18 22:56:34 -07002737
Jens Axboe561fb042019-10-24 07:25:42 -06002738 if (!ret) {
2739 s->has_user = (work->flags & IO_WQ_WORK_HAS_MM) != 0;
2740 s->in_async = true;
2741 do {
Pavel Begunkovd7324472019-11-21 21:24:36 +03002742 ret = io_issue_sqe(req, &nxt, false);
Jens Axboe561fb042019-10-24 07:25:42 -06002743 /*
2744 * We can get EAGAIN for polled IO even though we're
2745 * forcing a sync submission from here, since we can't
2746 * wait for request slots on the block side.
2747 */
2748 if (ret != -EAGAIN)
2749 break;
2750 cond_resched();
2751 } while (1);
2752 }
Jens Axboe31b51512019-01-18 22:56:34 -07002753
Jens Axboe561fb042019-10-24 07:25:42 -06002754 /* drop submission reference */
Jackie Liuec9c02a2019-11-08 23:50:36 +08002755 io_put_req(req);
Jens Axboe817869d2019-04-30 14:44:05 -06002756
Jens Axboe561fb042019-10-24 07:25:42 -06002757 if (ret) {
Jens Axboef1f40852019-11-05 20:33:16 -07002758 if (req->flags & REQ_F_LINK)
2759 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07002760 io_cqring_add_event(req, ret);
Jens Axboe817869d2019-04-30 14:44:05 -06002761 io_put_req(req);
Jens Axboeedafcce2019-01-09 09:16:05 -07002762 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002763
Jens Axboe561fb042019-10-24 07:25:42 -06002764 /* if a dependent link is ready, pass it back */
2765 if (!ret && nxt) {
Jens Axboe94ae5e72019-11-14 19:39:52 -07002766 struct io_kiocb *link;
2767
2768 io_prep_async_work(nxt, &link);
Jens Axboe561fb042019-10-24 07:25:42 -06002769 *workptr = &nxt->work;
Jens Axboeb76da702019-11-20 13:05:32 -07002770 if (link) {
2771 nxt->work.flags |= IO_WQ_WORK_CB;
2772 nxt->work.func = io_link_work_cb;
2773 nxt->work.data = link;
2774 }
Jens Axboeedafcce2019-01-09 09:16:05 -07002775 }
Jens Axboe31b51512019-01-18 22:56:34 -07002776}
Jens Axboe2b188cc2019-01-07 10:46:33 -07002777
Jens Axboe09bb8392019-03-13 12:39:28 -06002778static bool io_op_needs_file(const struct io_uring_sqe *sqe)
2779{
2780 int op = READ_ONCE(sqe->opcode);
2781
2782 switch (op) {
2783 case IORING_OP_NOP:
2784 case IORING_OP_POLL_REMOVE:
Pavel Begunkov5683e542019-11-14 00:59:19 +03002785 case IORING_OP_TIMEOUT:
Pavel Begunkova320e9f2019-11-14 00:11:01 +03002786 case IORING_OP_TIMEOUT_REMOVE:
2787 case IORING_OP_ASYNC_CANCEL:
2788 case IORING_OP_LINK_TIMEOUT:
Jens Axboe09bb8392019-03-13 12:39:28 -06002789 return false;
2790 default:
2791 return true;
2792 }
2793}
2794
Jens Axboe65e19f52019-10-26 07:20:21 -06002795static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
2796 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06002797{
Jens Axboe65e19f52019-10-26 07:20:21 -06002798 struct fixed_file_table *table;
2799
2800 table = &ctx->file_table[index >> IORING_FILE_TABLE_SHIFT];
2801 return table->files[index & IORING_FILE_TABLE_MASK];
2802}
2803
Jackie Liua197f662019-11-08 08:09:12 -07002804static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req)
Jens Axboe09bb8392019-03-13 12:39:28 -06002805{
Pavel Begunkov267bc902019-11-07 01:41:08 +03002806 struct sqe_submit *s = &req->submit;
Jackie Liua197f662019-11-08 08:09:12 -07002807 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe09bb8392019-03-13 12:39:28 -06002808 unsigned flags;
2809 int fd;
2810
2811 flags = READ_ONCE(s->sqe->flags);
2812 fd = READ_ONCE(s->sqe->fd);
2813
Jackie Liu4fe2c962019-09-09 20:50:40 +08002814 if (flags & IOSQE_IO_DRAIN)
Jens Axboede0617e2019-04-06 21:51:27 -06002815 req->flags |= REQ_F_IO_DRAIN;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002816 /*
2817 * All io need record the previous position, if LINK vs DARIN,
2818 * it can be used to mark the position of the first IO in the
2819 * link list.
2820 */
2821 req->sequence = s->sequence;
Jens Axboede0617e2019-04-06 21:51:27 -06002822
Jens Axboe60c112b2019-06-21 10:20:18 -06002823 if (!io_op_needs_file(s->sqe))
Jens Axboe09bb8392019-03-13 12:39:28 -06002824 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06002825
2826 if (flags & IOSQE_FIXED_FILE) {
Jens Axboe65e19f52019-10-26 07:20:21 -06002827 if (unlikely(!ctx->file_table ||
Jens Axboe09bb8392019-03-13 12:39:28 -06002828 (unsigned) fd >= ctx->nr_user_files))
2829 return -EBADF;
Jens Axboeb7620122019-10-26 07:22:55 -06002830 fd = array_index_nospec(fd, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06002831 req->file = io_file_from_index(ctx, fd);
2832 if (!req->file)
Jens Axboe08a45172019-10-03 08:11:03 -06002833 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06002834 req->flags |= REQ_F_FIXED_FILE;
2835 } else {
2836 if (s->needs_fixed_file)
2837 return -EBADF;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002838 trace_io_uring_file_get(ctx, fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06002839 req->file = io_file_get(state, fd);
2840 if (unlikely(!req->file))
2841 return -EBADF;
2842 }
2843
2844 return 0;
2845}
2846
Jackie Liua197f662019-11-08 08:09:12 -07002847static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002848{
Jens Axboefcb323c2019-10-24 12:39:47 -06002849 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07002850 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06002851
2852 rcu_read_lock();
2853 spin_lock_irq(&ctx->inflight_lock);
2854 /*
2855 * We use the f_ops->flush() handler to ensure that we can flush
2856 * out work accessing these files if the fd is closed. Check if
2857 * the fd has changed since we started down this path, and disallow
2858 * this operation if it has.
2859 */
2860 if (fcheck(req->submit.ring_fd) == req->submit.ring_file) {
2861 list_add(&req->inflight_entry, &ctx->inflight_list);
2862 req->flags |= REQ_F_INFLIGHT;
2863 req->work.files = current->files;
2864 ret = 0;
2865 }
2866 spin_unlock_irq(&ctx->inflight_lock);
2867 rcu_read_unlock();
2868
2869 return ret;
2870}
2871
Jens Axboe2665abf2019-11-05 12:40:47 -07002872static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
2873{
Jens Axboead8a48a2019-11-15 08:49:11 -07002874 struct io_timeout_data *data = container_of(timer,
2875 struct io_timeout_data, timer);
2876 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07002877 struct io_ring_ctx *ctx = req->ctx;
2878 struct io_kiocb *prev = NULL;
2879 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07002880
2881 spin_lock_irqsave(&ctx->completion_lock, flags);
2882
2883 /*
2884 * We don't expect the list to be empty, that will only happen if we
2885 * race with the completion of the linked work.
2886 */
2887 if (!list_empty(&req->list)) {
2888 prev = list_entry(req->list.prev, struct io_kiocb, link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07002889 if (refcount_inc_not_zero(&prev->refs)) {
Jens Axboe76a46e02019-11-10 23:34:16 -07002890 list_del_init(&req->list);
Jens Axboe5d960722019-11-19 15:31:28 -07002891 prev->flags &= ~REQ_F_LINK_TIMEOUT;
2892 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07002893 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07002894 }
2895
2896 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2897
2898 if (prev) {
Jens Axboefba38c22019-11-18 12:27:57 -07002899 if (prev->flags & REQ_F_LINK)
2900 prev->flags |= REQ_F_FAIL_LINK;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002901 io_async_find_and_cancel(ctx, req, prev->user_data, NULL,
2902 -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07002903 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07002904 } else {
2905 io_cqring_add_event(req, -ETIME);
2906 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07002907 }
Jens Axboe2665abf2019-11-05 12:40:47 -07002908 return HRTIMER_NORESTART;
2909}
2910
Jens Axboead8a48a2019-11-15 08:49:11 -07002911static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07002912{
Jens Axboe76a46e02019-11-10 23:34:16 -07002913 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07002914
Jens Axboe76a46e02019-11-10 23:34:16 -07002915 /*
2916 * If the list is now empty, then our linked request finished before
2917 * we got a chance to setup the timer
2918 */
2919 spin_lock_irq(&ctx->completion_lock);
2920 if (!list_empty(&req->list)) {
Jens Axboe94ae5e72019-11-14 19:39:52 -07002921 struct io_timeout_data *data = req->timeout.data;
2922
Jens Axboead8a48a2019-11-15 08:49:11 -07002923 data->timer.function = io_link_timeout_fn;
2924 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
2925 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07002926 }
Jens Axboe76a46e02019-11-10 23:34:16 -07002927 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07002928
Jens Axboe2665abf2019-11-05 12:40:47 -07002929 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07002930 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07002931}
2932
Jens Axboead8a48a2019-11-15 08:49:11 -07002933static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07002934{
2935 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002936
Jens Axboe2665abf2019-11-05 12:40:47 -07002937 if (!(req->flags & REQ_F_LINK))
2938 return NULL;
2939
2940 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, list);
Jens Axboe76a46e02019-11-10 23:34:16 -07002941 if (!nxt || nxt->submit.sqe->opcode != IORING_OP_LINK_TIMEOUT)
2942 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07002943
Jens Axboe76a46e02019-11-10 23:34:16 -07002944 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07002945 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07002946}
2947
Jens Axboe0e0702d2019-11-14 21:42:10 -07002948static void __io_queue_sqe(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002949{
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03002950 struct io_kiocb *linked_timeout = io_prep_linked_timeout(req);
2951 struct io_kiocb *nxt = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002952 int ret;
2953
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03002954 ret = io_issue_sqe(req, &nxt, true);
2955 if (nxt)
2956 io_queue_async_work(nxt);
Jens Axboe491381ce2019-10-17 09:20:46 -06002957
2958 /*
2959 * We async punt it if the file wasn't marked NOWAIT, or if the file
2960 * doesn't support non-blocking read/write attempts
2961 */
2962 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
2963 (req->flags & REQ_F_MUST_PUNT))) {
Pavel Begunkov267bc902019-11-07 01:41:08 +03002964 struct sqe_submit *s = &req->submit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002965 struct io_uring_sqe *sqe_copy;
2966
Jackie Liu954dab12019-09-18 10:37:52 +08002967 sqe_copy = kmemdup(s->sqe, sizeof(*sqe_copy), GFP_KERNEL);
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03002968 if (!sqe_copy)
2969 goto err;
Jens Axboee65ef562019-03-12 10:16:44 -06002970
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03002971 s->sqe = sqe_copy;
2972 req->flags |= REQ_F_FREE_SQE;
2973
2974 if (req->work.flags & IO_WQ_WORK_NEEDS_FILES) {
2975 ret = io_grab_files(req);
2976 if (ret)
2977 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002978 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03002979
2980 /*
2981 * Queued up for async execution, worker will release
2982 * submit reference when the iocb is actually submitted.
2983 */
2984 io_queue_async_work(req);
2985 return;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002986 }
Jens Axboee65ef562019-03-12 10:16:44 -06002987
Jens Axboefcb323c2019-10-24 12:39:47 -06002988err:
Jens Axboee65ef562019-03-12 10:16:44 -06002989 /* drop submission reference */
2990 io_put_req(req);
2991
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03002992 if (linked_timeout) {
Jens Axboe76a46e02019-11-10 23:34:16 -07002993 if (!ret)
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03002994 io_queue_linked_timeout(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07002995 else
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03002996 io_put_req(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07002997 }
2998
Jens Axboee65ef562019-03-12 10:16:44 -06002999 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06003000 if (ret) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07003001 io_cqring_add_event(req, ret);
Jens Axboe9e645e112019-05-10 16:07:28 -06003002 if (req->flags & REQ_F_LINK)
3003 req->flags |= REQ_F_FAIL_LINK;
Jens Axboee65ef562019-03-12 10:16:44 -06003004 io_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06003005 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003006}
3007
Jens Axboe0e0702d2019-11-14 21:42:10 -07003008static void io_queue_sqe(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08003009{
3010 int ret;
3011
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003012 if (unlikely(req->ctx->drain_next)) {
3013 req->flags |= REQ_F_IO_DRAIN;
3014 req->ctx->drain_next = false;
3015 }
3016 req->ctx->drain_next = (req->flags & REQ_F_DRAIN_LINK);
3017
Jackie Liua197f662019-11-08 08:09:12 -07003018 ret = io_req_defer(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08003019 if (ret) {
3020 if (ret != -EIOCBQUEUED) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07003021 io_cqring_add_event(req, ret);
Pavel Begunkovd3b357962019-11-19 23:32:48 +03003022 if (req->flags & REQ_F_LINK)
3023 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07003024 io_double_put_req(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08003025 }
Jens Axboe0e0702d2019-11-14 21:42:10 -07003026 } else
3027 __io_queue_sqe(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08003028}
3029
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003030static inline void io_queue_link_head(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08003031{
Jens Axboe94ae5e72019-11-14 19:39:52 -07003032 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003033 io_cqring_add_event(req, -ECANCELED);
3034 io_double_put_req(req);
3035 } else
Jens Axboe0e0702d2019-11-14 21:42:10 -07003036 io_queue_sqe(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08003037}
3038
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003039
Jens Axboe9e645e112019-05-10 16:07:28 -06003040#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK)
3041
Jackie Liua197f662019-11-08 08:09:12 -07003042static void io_submit_sqe(struct io_kiocb *req, struct io_submit_state *state,
3043 struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06003044{
Pavel Begunkov267bc902019-11-07 01:41:08 +03003045 struct sqe_submit *s = &req->submit;
Jackie Liua197f662019-11-08 08:09:12 -07003046 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -06003047 int ret;
3048
Jens Axboe78e19bb2019-11-06 15:21:34 -07003049 req->user_data = s->sqe->user_data;
3050
Jens Axboe9e645e112019-05-10 16:07:28 -06003051 /* enforce forwards compatibility on users */
3052 if (unlikely(s->sqe->flags & ~SQE_VALID_FLAGS)) {
3053 ret = -EINVAL;
Pavel Begunkov196be952019-11-07 01:41:06 +03003054 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06003055 }
3056
Jackie Liua197f662019-11-08 08:09:12 -07003057 ret = io_req_set_file(state, req);
Jens Axboe9e645e112019-05-10 16:07:28 -06003058 if (unlikely(ret)) {
3059err_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07003060 io_cqring_add_event(req, ret);
3061 io_double_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06003062 return;
3063 }
3064
Jens Axboe9e645e112019-05-10 16:07:28 -06003065 /*
3066 * If we already have a head request, queue this one for async
3067 * submittal once the head completes. If we don't have a head but
3068 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
3069 * submitted sync once the chain is complete. If none of those
3070 * conditions are true (normal request), then just queue it.
3071 */
3072 if (*link) {
3073 struct io_kiocb *prev = *link;
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03003074 struct io_uring_sqe *sqe_copy;
Jens Axboe9e645e112019-05-10 16:07:28 -06003075
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003076 if (s->sqe->flags & IOSQE_IO_DRAIN)
3077 (*link)->flags |= REQ_F_DRAIN_LINK | REQ_F_IO_DRAIN;
3078
Jens Axboe94ae5e72019-11-14 19:39:52 -07003079 if (READ_ONCE(s->sqe->opcode) == IORING_OP_LINK_TIMEOUT) {
3080 ret = io_timeout_setup(req);
3081 /* common setup allows offset being set, we don't */
3082 if (!ret && s->sqe->off)
3083 ret = -EINVAL;
3084 if (ret) {
3085 prev->flags |= REQ_F_FAIL_LINK;
3086 goto err_req;
3087 }
3088 }
3089
Jens Axboe9e645e112019-05-10 16:07:28 -06003090 sqe_copy = kmemdup(s->sqe, sizeof(*sqe_copy), GFP_KERNEL);
3091 if (!sqe_copy) {
3092 ret = -EAGAIN;
3093 goto err_req;
3094 }
3095
3096 s->sqe = sqe_copy;
Jens Axboe94ae5e72019-11-14 19:39:52 -07003097 req->flags |= REQ_F_FREE_SQE;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02003098 trace_io_uring_link(ctx, req, prev);
Jens Axboe9e645e112019-05-10 16:07:28 -06003099 list_add_tail(&req->list, &prev->link_list);
3100 } else if (s->sqe->flags & IOSQE_IO_LINK) {
3101 req->flags |= REQ_F_LINK;
3102
Jens Axboe9e645e112019-05-10 16:07:28 -06003103 INIT_LIST_HEAD(&req->link_list);
3104 *link = req;
3105 } else {
Jackie Liua197f662019-11-08 08:09:12 -07003106 io_queue_sqe(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06003107 }
3108}
3109
Jens Axboe9a56a232019-01-09 09:06:50 -07003110/*
3111 * Batched submission is done, ensure local IO is flushed out.
3112 */
3113static void io_submit_state_end(struct io_submit_state *state)
3114{
3115 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06003116 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07003117 if (state->free_reqs)
3118 kmem_cache_free_bulk(req_cachep, state->free_reqs,
3119 &state->reqs[state->cur_req]);
Jens Axboe9a56a232019-01-09 09:06:50 -07003120}
3121
3122/*
3123 * Start submission side cache.
3124 */
3125static void io_submit_state_start(struct io_submit_state *state,
3126 struct io_ring_ctx *ctx, unsigned max_ios)
3127{
3128 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07003129 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07003130 state->file = NULL;
3131 state->ios_left = max_ios;
3132}
3133
Jens Axboe2b188cc2019-01-07 10:46:33 -07003134static void io_commit_sqring(struct io_ring_ctx *ctx)
3135{
Hristo Venev75b28af2019-08-26 17:23:46 +00003136 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003137
Hristo Venev75b28af2019-08-26 17:23:46 +00003138 if (ctx->cached_sq_head != READ_ONCE(rings->sq.head)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07003139 /*
3140 * Ensure any loads from the SQEs are done at this point,
3141 * since once we write the new head, the application could
3142 * write new data to them.
3143 */
Hristo Venev75b28af2019-08-26 17:23:46 +00003144 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003145 }
3146}
3147
3148/*
Jens Axboe2b188cc2019-01-07 10:46:33 -07003149 * Fetch an sqe, if one is available. Note that s->sqe will point to memory
3150 * that is mapped by userspace. This means that care needs to be taken to
3151 * ensure that reads are stable, as we cannot rely on userspace always
3152 * being a good citizen. If members of the sqe are validated and then later
3153 * used, it's important that those reads are done through READ_ONCE() to
3154 * prevent a re-load down the line.
3155 */
3156static bool io_get_sqring(struct io_ring_ctx *ctx, struct sqe_submit *s)
3157{
Hristo Venev75b28af2019-08-26 17:23:46 +00003158 struct io_rings *rings = ctx->rings;
3159 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003160 unsigned head;
3161
3162 /*
3163 * The cached sq head (or cq tail) serves two purposes:
3164 *
3165 * 1) allows us to batch the cost of updating the user visible
3166 * head updates.
3167 * 2) allows the kernel side to track the head on its own, even
3168 * though the application is the one updating it.
3169 */
3170 head = ctx->cached_sq_head;
Stefan Bühlere523a292019-04-19 11:57:44 +02003171 /* make sure SQ entry isn't read before tail */
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03003172 if (unlikely(head == smp_load_acquire(&rings->sq.tail)))
Jens Axboe2b188cc2019-01-07 10:46:33 -07003173 return false;
3174
Hristo Venev75b28af2019-08-26 17:23:46 +00003175 head = READ_ONCE(sq_array[head & ctx->sq_mask]);
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03003176 if (likely(head < ctx->sq_entries)) {
Jens Axboefcb323c2019-10-24 12:39:47 -06003177 s->ring_file = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003178 s->sqe = &ctx->sq_sqes[head];
Jackie Liu8776f3f2019-09-09 20:50:39 +08003179 s->sequence = ctx->cached_sq_head;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003180 ctx->cached_sq_head++;
3181 return true;
3182 }
3183
3184 /* drop invalid entries */
3185 ctx->cached_sq_head++;
Jens Axboe498ccd92019-10-25 10:04:25 -06003186 ctx->cached_sq_dropped++;
3187 WRITE_ONCE(rings->sq_dropped, ctx->cached_sq_dropped);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003188 return false;
3189}
3190
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003191static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkovae9428c2019-11-06 00:22:14 +03003192 struct file *ring_file, int ring_fd,
3193 struct mm_struct **mm, bool async)
Jens Axboe6c271ce2019-01-10 11:22:30 -07003194{
3195 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06003196 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06003197 int i, submitted = 0;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03003198 bool mm_fault = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003199
Jens Axboec4a2ed72019-11-21 21:01:26 -07003200 /* if we have a backlog and couldn't flush it all, return BUSY */
3201 if (!list_empty(&ctx->cq_overflow_list) &&
3202 !io_cqring_overflow_flush(ctx, false))
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003203 return -EBUSY;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003204
3205 if (nr > IO_PLUG_THRESHOLD) {
3206 io_submit_state_start(&state, ctx, nr);
3207 statep = &state;
3208 }
3209
3210 for (i = 0; i < nr; i++) {
Pavel Begunkov196be952019-11-07 01:41:06 +03003211 struct io_kiocb *req;
Pavel Begunkov50585b92019-11-07 01:41:07 +03003212 unsigned int sqe_flags;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003213
Pavel Begunkov196be952019-11-07 01:41:06 +03003214 req = io_get_req(ctx, statep);
3215 if (unlikely(!req)) {
3216 if (!submitted)
3217 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003218 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06003219 }
Pavel Begunkov50585b92019-11-07 01:41:07 +03003220 if (!io_get_sqring(ctx, &req->submit)) {
Pavel Begunkov196be952019-11-07 01:41:06 +03003221 __io_free_req(req);
3222 break;
3223 }
Jens Axboe9e645e112019-05-10 16:07:28 -06003224
Pavel Begunkov50585b92019-11-07 01:41:07 +03003225 if (io_sqe_needs_user(req->submit.sqe) && !*mm) {
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03003226 mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm);
3227 if (!mm_fault) {
3228 use_mm(ctx->sqo_mm);
3229 *mm = ctx->sqo_mm;
3230 }
3231 }
3232
Pavel Begunkov50585b92019-11-07 01:41:07 +03003233 sqe_flags = req->submit.sqe->flags;
3234
Pavel Begunkov50585b92019-11-07 01:41:07 +03003235 req->submit.ring_file = ring_file;
3236 req->submit.ring_fd = ring_fd;
3237 req->submit.has_user = *mm != NULL;
3238 req->submit.in_async = async;
3239 req->submit.needs_fixed_file = async;
3240 trace_io_uring_submit_sqe(ctx, req->submit.sqe->user_data,
3241 true, async);
Jackie Liua197f662019-11-08 08:09:12 -07003242 io_submit_sqe(req, statep, &link);
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03003243 submitted++;
Pavel Begunkove5eb6362019-11-06 00:22:15 +03003244
3245 /*
3246 * If previous wasn't linked and we have a linked command,
3247 * that's the end of the chain. Submit the previous link.
3248 */
Pavel Begunkov50585b92019-11-07 01:41:07 +03003249 if (!(sqe_flags & IOSQE_IO_LINK) && link) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003250 io_queue_link_head(link);
Pavel Begunkove5eb6362019-11-06 00:22:15 +03003251 link = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003252 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07003253 }
3254
Jens Axboe9e645e112019-05-10 16:07:28 -06003255 if (link)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003256 io_queue_link_head(link);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003257 if (statep)
3258 io_submit_state_end(&state);
3259
Pavel Begunkovae9428c2019-11-06 00:22:14 +03003260 /* Commit SQ ring head once we've consumed and submitted all SQEs */
3261 io_commit_sqring(ctx);
3262
Jens Axboe6c271ce2019-01-10 11:22:30 -07003263 return submitted;
3264}
3265
3266static int io_sq_thread(void *data)
3267{
Jens Axboe6c271ce2019-01-10 11:22:30 -07003268 struct io_ring_ctx *ctx = data;
3269 struct mm_struct *cur_mm = NULL;
3270 mm_segment_t old_fs;
3271 DEFINE_WAIT(wait);
3272 unsigned inflight;
3273 unsigned long timeout;
Jens Axboec1edbf52019-11-10 16:56:04 -07003274 int ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003275
Jens Axboe206aefd2019-11-07 18:27:42 -07003276 complete(&ctx->completions[1]);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08003277
Jens Axboe6c271ce2019-01-10 11:22:30 -07003278 old_fs = get_fs();
3279 set_fs(USER_DS);
3280
Jens Axboec1edbf52019-11-10 16:56:04 -07003281 ret = timeout = inflight = 0;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003282 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003283 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003284
3285 if (inflight) {
3286 unsigned nr_events = 0;
3287
3288 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe2b2ed972019-10-25 10:06:15 -06003289 /*
3290 * inflight is the count of the maximum possible
3291 * entries we submitted, but it can be smaller
3292 * if we dropped some of them. If we don't have
3293 * poll entries available, then we know that we
3294 * have nothing left to poll for. Reset the
3295 * inflight count to zero in that case.
3296 */
3297 mutex_lock(&ctx->uring_lock);
3298 if (!list_empty(&ctx->poll_list))
3299 __io_iopoll_check(ctx, &nr_events, 0);
3300 else
3301 inflight = 0;
3302 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003303 } else {
3304 /*
3305 * Normal IO, just pretend everything completed.
3306 * We don't have to poll completions for that.
3307 */
3308 nr_events = inflight;
3309 }
3310
3311 inflight -= nr_events;
3312 if (!inflight)
3313 timeout = jiffies + ctx->sq_thread_idle;
3314 }
3315
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003316 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07003317
3318 /*
3319 * If submit got -EBUSY, flag us as needing the application
3320 * to enter the kernel to reap and flush events.
3321 */
3322 if (!to_submit || ret == -EBUSY) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07003323 /*
3324 * We're polling. If we're within the defined idle
3325 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07003326 * to sleep. The exception is if we got EBUSY doing
3327 * more IO, we should wait for the application to
3328 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07003329 */
Jens Axboec1edbf52019-11-10 16:56:04 -07003330 if (inflight ||
3331 (!time_after(jiffies, timeout) && ret != -EBUSY)) {
Jens Axboe9831a902019-09-19 09:48:55 -06003332 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07003333 continue;
3334 }
3335
3336 /*
3337 * Drop cur_mm before scheduling, we can't hold it for
3338 * long periods (or over schedule()). Do this before
3339 * adding ourselves to the waitqueue, as the unuse/drop
3340 * may sleep.
3341 */
3342 if (cur_mm) {
3343 unuse_mm(cur_mm);
3344 mmput(cur_mm);
3345 cur_mm = NULL;
3346 }
3347
3348 prepare_to_wait(&ctx->sqo_wait, &wait,
3349 TASK_INTERRUPTIBLE);
3350
3351 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00003352 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02003353 /* make sure to read SQ tail after writing flags */
3354 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07003355
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003356 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07003357 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003358 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07003359 finish_wait(&ctx->sqo_wait, &wait);
3360 break;
3361 }
3362 if (signal_pending(current))
3363 flush_signals(current);
3364 schedule();
3365 finish_wait(&ctx->sqo_wait, &wait);
3366
Hristo Venev75b28af2019-08-26 17:23:46 +00003367 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003368 continue;
3369 }
3370 finish_wait(&ctx->sqo_wait, &wait);
3371
Hristo Venev75b28af2019-08-26 17:23:46 +00003372 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003373 }
3374
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003375 to_submit = min(to_submit, ctx->sq_entries);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003376 ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true);
3377 if (ret > 0)
3378 inflight += ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003379 }
3380
3381 set_fs(old_fs);
3382 if (cur_mm) {
3383 unuse_mm(cur_mm);
3384 mmput(cur_mm);
3385 }
Jens Axboe06058632019-04-13 09:26:03 -06003386
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003387 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06003388
Jens Axboe6c271ce2019-01-10 11:22:30 -07003389 return 0;
3390}
3391
Jens Axboebda52162019-09-24 13:47:15 -06003392struct io_wait_queue {
3393 struct wait_queue_entry wq;
3394 struct io_ring_ctx *ctx;
3395 unsigned to_wait;
3396 unsigned nr_timeouts;
3397};
3398
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003399static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06003400{
3401 struct io_ring_ctx *ctx = iowq->ctx;
3402
3403 /*
3404 * Wake up if we have enough events, or if a timeout occured since we
3405 * started waiting. For timeouts, we always want to return to userspace,
3406 * regardless of event count.
3407 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003408 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06003409 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
3410}
3411
3412static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
3413 int wake_flags, void *key)
3414{
3415 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
3416 wq);
3417
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003418 /* use noflush == true, as we can't safely rely on locking context */
3419 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06003420 return -1;
3421
3422 return autoremove_wake_function(curr, mode, wake_flags, key);
3423}
3424
Jens Axboe2b188cc2019-01-07 10:46:33 -07003425/*
3426 * Wait until events become available, if we don't already have some. The
3427 * application must reap them itself, as they reside on the shared cq ring.
3428 */
3429static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
3430 const sigset_t __user *sig, size_t sigsz)
3431{
Jens Axboebda52162019-09-24 13:47:15 -06003432 struct io_wait_queue iowq = {
3433 .wq = {
3434 .private = current,
3435 .func = io_wake_function,
3436 .entry = LIST_HEAD_INIT(iowq.wq.entry),
3437 },
3438 .ctx = ctx,
3439 .to_wait = min_events,
3440 };
Hristo Venev75b28af2019-08-26 17:23:46 +00003441 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08003442 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003443
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003444 if (io_cqring_events(ctx, false) >= min_events)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003445 return 0;
3446
3447 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01003448#ifdef CONFIG_COMPAT
3449 if (in_compat_syscall())
3450 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07003451 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01003452 else
3453#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07003454 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01003455
Jens Axboe2b188cc2019-01-07 10:46:33 -07003456 if (ret)
3457 return ret;
3458 }
3459
Jens Axboebda52162019-09-24 13:47:15 -06003460 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02003461 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06003462 do {
3463 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
3464 TASK_INTERRUPTIBLE);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003465 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06003466 break;
3467 schedule();
3468 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08003469 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06003470 break;
3471 }
3472 } while (1);
3473 finish_wait(&ctx->wait, &iowq.wq);
3474
Jackie Liue9ffa5c2019-10-29 11:16:42 +08003475 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003476
Hristo Venev75b28af2019-08-26 17:23:46 +00003477 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003478}
3479
Jens Axboe6b063142019-01-10 22:13:58 -07003480static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
3481{
3482#if defined(CONFIG_UNIX)
3483 if (ctx->ring_sock) {
3484 struct sock *sock = ctx->ring_sock->sk;
3485 struct sk_buff *skb;
3486
3487 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
3488 kfree_skb(skb);
3489 }
3490#else
3491 int i;
3492
Jens Axboe65e19f52019-10-26 07:20:21 -06003493 for (i = 0; i < ctx->nr_user_files; i++) {
3494 struct file *file;
3495
3496 file = io_file_from_index(ctx, i);
3497 if (file)
3498 fput(file);
3499 }
Jens Axboe6b063142019-01-10 22:13:58 -07003500#endif
3501}
3502
3503static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
3504{
Jens Axboe65e19f52019-10-26 07:20:21 -06003505 unsigned nr_tables, i;
3506
3507 if (!ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003508 return -ENXIO;
3509
3510 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06003511 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
3512 for (i = 0; i < nr_tables; i++)
3513 kfree(ctx->file_table[i].files);
3514 kfree(ctx->file_table);
3515 ctx->file_table = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003516 ctx->nr_user_files = 0;
3517 return 0;
3518}
3519
Jens Axboe6c271ce2019-01-10 11:22:30 -07003520static void io_sq_thread_stop(struct io_ring_ctx *ctx)
3521{
3522 if (ctx->sqo_thread) {
Jens Axboe206aefd2019-11-07 18:27:42 -07003523 wait_for_completion(&ctx->completions[1]);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003524 /*
3525 * The park is a bit of a work-around, without it we get
3526 * warning spews on shutdown with SQPOLL set and affinity
3527 * set to a single CPU.
3528 */
Jens Axboe06058632019-04-13 09:26:03 -06003529 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003530 kthread_stop(ctx->sqo_thread);
3531 ctx->sqo_thread = NULL;
3532 }
3533}
3534
Jens Axboe6b063142019-01-10 22:13:58 -07003535static void io_finish_async(struct io_ring_ctx *ctx)
3536{
Jens Axboe6c271ce2019-01-10 11:22:30 -07003537 io_sq_thread_stop(ctx);
3538
Jens Axboe561fb042019-10-24 07:25:42 -06003539 if (ctx->io_wq) {
3540 io_wq_destroy(ctx->io_wq);
3541 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003542 }
3543}
3544
3545#if defined(CONFIG_UNIX)
3546static void io_destruct_skb(struct sk_buff *skb)
3547{
3548 struct io_ring_ctx *ctx = skb->sk->sk_user_data;
3549
Jens Axboe561fb042019-10-24 07:25:42 -06003550 if (ctx->io_wq)
3551 io_wq_flush(ctx->io_wq);
Jens Axboe8a997342019-10-09 14:40:13 -06003552
Jens Axboe6b063142019-01-10 22:13:58 -07003553 unix_destruct_scm(skb);
3554}
3555
3556/*
3557 * Ensure the UNIX gc is aware of our file set, so we are certain that
3558 * the io_uring can be safely unregistered on process exit, even if we have
3559 * loops in the file referencing.
3560 */
3561static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
3562{
3563 struct sock *sk = ctx->ring_sock->sk;
3564 struct scm_fp_list *fpl;
3565 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06003566 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07003567
3568 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
3569 unsigned long inflight = ctx->user->unix_inflight + nr;
3570
3571 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
3572 return -EMFILE;
3573 }
3574
3575 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
3576 if (!fpl)
3577 return -ENOMEM;
3578
3579 skb = alloc_skb(0, GFP_KERNEL);
3580 if (!skb) {
3581 kfree(fpl);
3582 return -ENOMEM;
3583 }
3584
3585 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07003586
Jens Axboe08a45172019-10-03 08:11:03 -06003587 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07003588 fpl->user = get_uid(ctx->user);
3589 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003590 struct file *file = io_file_from_index(ctx, i + offset);
3591
3592 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06003593 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06003594 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06003595 unix_inflight(fpl->user, fpl->fp[nr_files]);
3596 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07003597 }
3598
Jens Axboe08a45172019-10-03 08:11:03 -06003599 if (nr_files) {
3600 fpl->max = SCM_MAX_FD;
3601 fpl->count = nr_files;
3602 UNIXCB(skb).fp = fpl;
3603 skb->destructor = io_destruct_skb;
3604 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
3605 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07003606
Jens Axboe08a45172019-10-03 08:11:03 -06003607 for (i = 0; i < nr_files; i++)
3608 fput(fpl->fp[i]);
3609 } else {
3610 kfree_skb(skb);
3611 kfree(fpl);
3612 }
Jens Axboe6b063142019-01-10 22:13:58 -07003613
3614 return 0;
3615}
3616
3617/*
3618 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
3619 * causes regular reference counting to break down. We rely on the UNIX
3620 * garbage collection to take care of this problem for us.
3621 */
3622static int io_sqe_files_scm(struct io_ring_ctx *ctx)
3623{
3624 unsigned left, total;
3625 int ret = 0;
3626
3627 total = 0;
3628 left = ctx->nr_user_files;
3629 while (left) {
3630 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07003631
3632 ret = __io_sqe_files_scm(ctx, this_files, total);
3633 if (ret)
3634 break;
3635 left -= this_files;
3636 total += this_files;
3637 }
3638
3639 if (!ret)
3640 return 0;
3641
3642 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003643 struct file *file = io_file_from_index(ctx, total);
3644
3645 if (file)
3646 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07003647 total++;
3648 }
3649
3650 return ret;
3651}
3652#else
3653static int io_sqe_files_scm(struct io_ring_ctx *ctx)
3654{
3655 return 0;
3656}
3657#endif
3658
Jens Axboe65e19f52019-10-26 07:20:21 -06003659static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
3660 unsigned nr_files)
3661{
3662 int i;
3663
3664 for (i = 0; i < nr_tables; i++) {
3665 struct fixed_file_table *table = &ctx->file_table[i];
3666 unsigned this_files;
3667
3668 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
3669 table->files = kcalloc(this_files, sizeof(struct file *),
3670 GFP_KERNEL);
3671 if (!table->files)
3672 break;
3673 nr_files -= this_files;
3674 }
3675
3676 if (i == nr_tables)
3677 return 0;
3678
3679 for (i = 0; i < nr_tables; i++) {
3680 struct fixed_file_table *table = &ctx->file_table[i];
3681 kfree(table->files);
3682 }
3683 return 1;
3684}
3685
Jens Axboe6b063142019-01-10 22:13:58 -07003686static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
3687 unsigned nr_args)
3688{
3689 __s32 __user *fds = (__s32 __user *) arg;
Jens Axboe65e19f52019-10-26 07:20:21 -06003690 unsigned nr_tables;
Jens Axboe6b063142019-01-10 22:13:58 -07003691 int fd, ret = 0;
3692 unsigned i;
3693
Jens Axboe65e19f52019-10-26 07:20:21 -06003694 if (ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003695 return -EBUSY;
3696 if (!nr_args)
3697 return -EINVAL;
3698 if (nr_args > IORING_MAX_FIXED_FILES)
3699 return -EMFILE;
3700
Jens Axboe65e19f52019-10-26 07:20:21 -06003701 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
3702 ctx->file_table = kcalloc(nr_tables, sizeof(struct fixed_file_table),
3703 GFP_KERNEL);
3704 if (!ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003705 return -ENOMEM;
3706
Jens Axboe65e19f52019-10-26 07:20:21 -06003707 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
3708 kfree(ctx->file_table);
Jens Axboe46568e92019-11-10 08:40:53 -07003709 ctx->file_table = NULL;
Jens Axboe65e19f52019-10-26 07:20:21 -06003710 return -ENOMEM;
3711 }
3712
Jens Axboe08a45172019-10-03 08:11:03 -06003713 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003714 struct fixed_file_table *table;
3715 unsigned index;
3716
Jens Axboe6b063142019-01-10 22:13:58 -07003717 ret = -EFAULT;
3718 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
3719 break;
Jens Axboe08a45172019-10-03 08:11:03 -06003720 /* allow sparse sets */
3721 if (fd == -1) {
3722 ret = 0;
3723 continue;
3724 }
Jens Axboe6b063142019-01-10 22:13:58 -07003725
Jens Axboe65e19f52019-10-26 07:20:21 -06003726 table = &ctx->file_table[i >> IORING_FILE_TABLE_SHIFT];
3727 index = i & IORING_FILE_TABLE_MASK;
3728 table->files[index] = fget(fd);
Jens Axboe6b063142019-01-10 22:13:58 -07003729
3730 ret = -EBADF;
Jens Axboe65e19f52019-10-26 07:20:21 -06003731 if (!table->files[index])
Jens Axboe6b063142019-01-10 22:13:58 -07003732 break;
3733 /*
3734 * Don't allow io_uring instances to be registered. If UNIX
3735 * isn't enabled, then this causes a reference cycle and this
3736 * instance can never get freed. If UNIX is enabled we'll
3737 * handle it just fine, but there's still no point in allowing
3738 * a ring fd as it doesn't support regular read/write anyway.
3739 */
Jens Axboe65e19f52019-10-26 07:20:21 -06003740 if (table->files[index]->f_op == &io_uring_fops) {
3741 fput(table->files[index]);
Jens Axboe6b063142019-01-10 22:13:58 -07003742 break;
3743 }
Jens Axboe6b063142019-01-10 22:13:58 -07003744 ret = 0;
3745 }
3746
3747 if (ret) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003748 for (i = 0; i < ctx->nr_user_files; i++) {
3749 struct file *file;
Jens Axboe6b063142019-01-10 22:13:58 -07003750
Jens Axboe65e19f52019-10-26 07:20:21 -06003751 file = io_file_from_index(ctx, i);
3752 if (file)
3753 fput(file);
3754 }
3755 for (i = 0; i < nr_tables; i++)
3756 kfree(ctx->file_table[i].files);
3757
3758 kfree(ctx->file_table);
3759 ctx->file_table = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003760 ctx->nr_user_files = 0;
3761 return ret;
3762 }
3763
3764 ret = io_sqe_files_scm(ctx);
3765 if (ret)
3766 io_sqe_files_unregister(ctx);
3767
3768 return ret;
3769}
3770
Jens Axboec3a31e62019-10-03 13:59:56 -06003771static void io_sqe_file_unregister(struct io_ring_ctx *ctx, int index)
3772{
3773#if defined(CONFIG_UNIX)
Jens Axboe65e19f52019-10-26 07:20:21 -06003774 struct file *file = io_file_from_index(ctx, index);
Jens Axboec3a31e62019-10-03 13:59:56 -06003775 struct sock *sock = ctx->ring_sock->sk;
3776 struct sk_buff_head list, *head = &sock->sk_receive_queue;
3777 struct sk_buff *skb;
3778 int i;
3779
3780 __skb_queue_head_init(&list);
3781
3782 /*
3783 * Find the skb that holds this file in its SCM_RIGHTS. When found,
3784 * remove this entry and rearrange the file array.
3785 */
3786 skb = skb_dequeue(head);
3787 while (skb) {
3788 struct scm_fp_list *fp;
3789
3790 fp = UNIXCB(skb).fp;
3791 for (i = 0; i < fp->count; i++) {
3792 int left;
3793
3794 if (fp->fp[i] != file)
3795 continue;
3796
3797 unix_notinflight(fp->user, fp->fp[i]);
3798 left = fp->count - 1 - i;
3799 if (left) {
3800 memmove(&fp->fp[i], &fp->fp[i + 1],
3801 left * sizeof(struct file *));
3802 }
3803 fp->count--;
3804 if (!fp->count) {
3805 kfree_skb(skb);
3806 skb = NULL;
3807 } else {
3808 __skb_queue_tail(&list, skb);
3809 }
3810 fput(file);
3811 file = NULL;
3812 break;
3813 }
3814
3815 if (!file)
3816 break;
3817
3818 __skb_queue_tail(&list, skb);
3819
3820 skb = skb_dequeue(head);
3821 }
3822
3823 if (skb_peek(&list)) {
3824 spin_lock_irq(&head->lock);
3825 while ((skb = __skb_dequeue(&list)) != NULL)
3826 __skb_queue_tail(head, skb);
3827 spin_unlock_irq(&head->lock);
3828 }
3829#else
Jens Axboe65e19f52019-10-26 07:20:21 -06003830 fput(io_file_from_index(ctx, index));
Jens Axboec3a31e62019-10-03 13:59:56 -06003831#endif
3832}
3833
3834static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
3835 int index)
3836{
3837#if defined(CONFIG_UNIX)
3838 struct sock *sock = ctx->ring_sock->sk;
3839 struct sk_buff_head *head = &sock->sk_receive_queue;
3840 struct sk_buff *skb;
3841
3842 /*
3843 * See if we can merge this file into an existing skb SCM_RIGHTS
3844 * file set. If there's no room, fall back to allocating a new skb
3845 * and filling it in.
3846 */
3847 spin_lock_irq(&head->lock);
3848 skb = skb_peek(head);
3849 if (skb) {
3850 struct scm_fp_list *fpl = UNIXCB(skb).fp;
3851
3852 if (fpl->count < SCM_MAX_FD) {
3853 __skb_unlink(skb, head);
3854 spin_unlock_irq(&head->lock);
3855 fpl->fp[fpl->count] = get_file(file);
3856 unix_inflight(fpl->user, fpl->fp[fpl->count]);
3857 fpl->count++;
3858 spin_lock_irq(&head->lock);
3859 __skb_queue_head(head, skb);
3860 } else {
3861 skb = NULL;
3862 }
3863 }
3864 spin_unlock_irq(&head->lock);
3865
3866 if (skb) {
3867 fput(file);
3868 return 0;
3869 }
3870
3871 return __io_sqe_files_scm(ctx, 1, index);
3872#else
3873 return 0;
3874#endif
3875}
3876
3877static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
3878 unsigned nr_args)
3879{
3880 struct io_uring_files_update up;
3881 __s32 __user *fds;
3882 int fd, i, err;
3883 __u32 done;
3884
Jens Axboe65e19f52019-10-26 07:20:21 -06003885 if (!ctx->file_table)
Jens Axboec3a31e62019-10-03 13:59:56 -06003886 return -ENXIO;
3887 if (!nr_args)
3888 return -EINVAL;
3889 if (copy_from_user(&up, arg, sizeof(up)))
3890 return -EFAULT;
3891 if (check_add_overflow(up.offset, nr_args, &done))
3892 return -EOVERFLOW;
3893 if (done > ctx->nr_user_files)
3894 return -EINVAL;
3895
3896 done = 0;
3897 fds = (__s32 __user *) up.fds;
3898 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003899 struct fixed_file_table *table;
3900 unsigned index;
3901
Jens Axboec3a31e62019-10-03 13:59:56 -06003902 err = 0;
3903 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
3904 err = -EFAULT;
3905 break;
3906 }
3907 i = array_index_nospec(up.offset, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06003908 table = &ctx->file_table[i >> IORING_FILE_TABLE_SHIFT];
3909 index = i & IORING_FILE_TABLE_MASK;
3910 if (table->files[index]) {
Jens Axboec3a31e62019-10-03 13:59:56 -06003911 io_sqe_file_unregister(ctx, i);
Jens Axboe65e19f52019-10-26 07:20:21 -06003912 table->files[index] = NULL;
Jens Axboec3a31e62019-10-03 13:59:56 -06003913 }
3914 if (fd != -1) {
3915 struct file *file;
3916
3917 file = fget(fd);
3918 if (!file) {
3919 err = -EBADF;
3920 break;
3921 }
3922 /*
3923 * Don't allow io_uring instances to be registered. If
3924 * UNIX isn't enabled, then this causes a reference
3925 * cycle and this instance can never get freed. If UNIX
3926 * is enabled we'll handle it just fine, but there's
3927 * still no point in allowing a ring fd as it doesn't
3928 * support regular read/write anyway.
3929 */
3930 if (file->f_op == &io_uring_fops) {
3931 fput(file);
3932 err = -EBADF;
3933 break;
3934 }
Jens Axboe65e19f52019-10-26 07:20:21 -06003935 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06003936 err = io_sqe_file_register(ctx, file, i);
3937 if (err)
3938 break;
3939 }
3940 nr_args--;
3941 done++;
3942 up.offset++;
3943 }
3944
3945 return done ? done : err;
3946}
3947
Jens Axboe7d723062019-11-12 22:31:31 -07003948static void io_put_work(struct io_wq_work *work)
3949{
3950 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
3951
3952 io_put_req(req);
3953}
3954
3955static void io_get_work(struct io_wq_work *work)
3956{
3957 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
3958
3959 refcount_inc(&req->refs);
3960}
3961
Jens Axboe6c271ce2019-01-10 11:22:30 -07003962static int io_sq_offload_start(struct io_ring_ctx *ctx,
3963 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003964{
Jens Axboe576a3472019-11-25 08:49:20 -07003965 struct io_wq_data data;
Jens Axboe561fb042019-10-24 07:25:42 -06003966 unsigned concurrency;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003967 int ret;
3968
Jens Axboe6c271ce2019-01-10 11:22:30 -07003969 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003970 mmgrab(current->mm);
3971 ctx->sqo_mm = current->mm;
3972
Jens Axboe6c271ce2019-01-10 11:22:30 -07003973 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06003974 ret = -EPERM;
3975 if (!capable(CAP_SYS_ADMIN))
3976 goto err;
3977
Jens Axboe917257d2019-04-13 09:28:55 -06003978 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
3979 if (!ctx->sq_thread_idle)
3980 ctx->sq_thread_idle = HZ;
3981
Jens Axboe6c271ce2019-01-10 11:22:30 -07003982 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06003983 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003984
Jens Axboe917257d2019-04-13 09:28:55 -06003985 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06003986 if (cpu >= nr_cpu_ids)
3987 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08003988 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06003989 goto err;
3990
Jens Axboe6c271ce2019-01-10 11:22:30 -07003991 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
3992 ctx, cpu,
3993 "io_uring-sq");
3994 } else {
3995 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
3996 "io_uring-sq");
3997 }
3998 if (IS_ERR(ctx->sqo_thread)) {
3999 ret = PTR_ERR(ctx->sqo_thread);
4000 ctx->sqo_thread = NULL;
4001 goto err;
4002 }
4003 wake_up_process(ctx->sqo_thread);
4004 } else if (p->flags & IORING_SETUP_SQ_AFF) {
4005 /* Can't have SQ_AFF without SQPOLL */
4006 ret = -EINVAL;
4007 goto err;
4008 }
4009
Jens Axboe576a3472019-11-25 08:49:20 -07004010 data.mm = ctx->sqo_mm;
4011 data.user = ctx->user;
4012 data.get_work = io_get_work;
4013 data.put_work = io_put_work;
4014
Jens Axboe561fb042019-10-24 07:25:42 -06004015 /* Do QD, or 4 * CPUS, whatever is smallest */
4016 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
Jens Axboe576a3472019-11-25 08:49:20 -07004017 ctx->io_wq = io_wq_create(concurrency, &data);
Jens Axboe975c99a52019-10-30 08:42:56 -06004018 if (IS_ERR(ctx->io_wq)) {
4019 ret = PTR_ERR(ctx->io_wq);
4020 ctx->io_wq = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004021 goto err;
4022 }
4023
4024 return 0;
4025err:
Jens Axboe54a91f32019-09-10 09:15:04 -06004026 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004027 mmdrop(ctx->sqo_mm);
4028 ctx->sqo_mm = NULL;
4029 return ret;
4030}
4031
4032static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
4033{
4034 atomic_long_sub(nr_pages, &user->locked_vm);
4035}
4036
4037static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
4038{
4039 unsigned long page_limit, cur_pages, new_pages;
4040
4041 /* Don't allow more pages than we can safely lock */
4042 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
4043
4044 do {
4045 cur_pages = atomic_long_read(&user->locked_vm);
4046 new_pages = cur_pages + nr_pages;
4047 if (new_pages > page_limit)
4048 return -ENOMEM;
4049 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
4050 new_pages) != cur_pages);
4051
4052 return 0;
4053}
4054
4055static void io_mem_free(void *ptr)
4056{
Mark Rutland52e04ef2019-04-30 17:30:21 +01004057 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004058
Mark Rutland52e04ef2019-04-30 17:30:21 +01004059 if (!ptr)
4060 return;
4061
4062 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004063 if (put_page_testzero(page))
4064 free_compound_page(page);
4065}
4066
4067static void *io_mem_alloc(size_t size)
4068{
4069 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
4070 __GFP_NORETRY;
4071
4072 return (void *) __get_free_pages(gfp_flags, get_order(size));
4073}
4074
Hristo Venev75b28af2019-08-26 17:23:46 +00004075static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
4076 size_t *sq_offset)
4077{
4078 struct io_rings *rings;
4079 size_t off, sq_array_size;
4080
4081 off = struct_size(rings, cqes, cq_entries);
4082 if (off == SIZE_MAX)
4083 return SIZE_MAX;
4084
4085#ifdef CONFIG_SMP
4086 off = ALIGN(off, SMP_CACHE_BYTES);
4087 if (off == 0)
4088 return SIZE_MAX;
4089#endif
4090
4091 sq_array_size = array_size(sizeof(u32), sq_entries);
4092 if (sq_array_size == SIZE_MAX)
4093 return SIZE_MAX;
4094
4095 if (check_add_overflow(off, sq_array_size, &off))
4096 return SIZE_MAX;
4097
4098 if (sq_offset)
4099 *sq_offset = off;
4100
4101 return off;
4102}
4103
Jens Axboe2b188cc2019-01-07 10:46:33 -07004104static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
4105{
Hristo Venev75b28af2019-08-26 17:23:46 +00004106 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004107
Hristo Venev75b28af2019-08-26 17:23:46 +00004108 pages = (size_t)1 << get_order(
4109 rings_size(sq_entries, cq_entries, NULL));
4110 pages += (size_t)1 << get_order(
4111 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07004112
Hristo Venev75b28af2019-08-26 17:23:46 +00004113 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004114}
4115
Jens Axboeedafcce2019-01-09 09:16:05 -07004116static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
4117{
4118 int i, j;
4119
4120 if (!ctx->user_bufs)
4121 return -ENXIO;
4122
4123 for (i = 0; i < ctx->nr_user_bufs; i++) {
4124 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
4125
4126 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbard27c4d3a2019-08-04 19:32:06 -07004127 put_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07004128
4129 if (ctx->account_mem)
4130 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004131 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07004132 imu->nr_bvecs = 0;
4133 }
4134
4135 kfree(ctx->user_bufs);
4136 ctx->user_bufs = NULL;
4137 ctx->nr_user_bufs = 0;
4138 return 0;
4139}
4140
4141static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
4142 void __user *arg, unsigned index)
4143{
4144 struct iovec __user *src;
4145
4146#ifdef CONFIG_COMPAT
4147 if (ctx->compat) {
4148 struct compat_iovec __user *ciovs;
4149 struct compat_iovec ciov;
4150
4151 ciovs = (struct compat_iovec __user *) arg;
4152 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
4153 return -EFAULT;
4154
4155 dst->iov_base = (void __user *) (unsigned long) ciov.iov_base;
4156 dst->iov_len = ciov.iov_len;
4157 return 0;
4158 }
4159#endif
4160 src = (struct iovec __user *) arg;
4161 if (copy_from_user(dst, &src[index], sizeof(*dst)))
4162 return -EFAULT;
4163 return 0;
4164}
4165
4166static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
4167 unsigned nr_args)
4168{
4169 struct vm_area_struct **vmas = NULL;
4170 struct page **pages = NULL;
4171 int i, j, got_pages = 0;
4172 int ret = -EINVAL;
4173
4174 if (ctx->user_bufs)
4175 return -EBUSY;
4176 if (!nr_args || nr_args > UIO_MAXIOV)
4177 return -EINVAL;
4178
4179 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
4180 GFP_KERNEL);
4181 if (!ctx->user_bufs)
4182 return -ENOMEM;
4183
4184 for (i = 0; i < nr_args; i++) {
4185 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
4186 unsigned long off, start, end, ubuf;
4187 int pret, nr_pages;
4188 struct iovec iov;
4189 size_t size;
4190
4191 ret = io_copy_iov(ctx, &iov, arg, i);
4192 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03004193 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07004194
4195 /*
4196 * Don't impose further limits on the size and buffer
4197 * constraints here, we'll -EINVAL later when IO is
4198 * submitted if they are wrong.
4199 */
4200 ret = -EFAULT;
4201 if (!iov.iov_base || !iov.iov_len)
4202 goto err;
4203
4204 /* arbitrary limit, but we need something */
4205 if (iov.iov_len > SZ_1G)
4206 goto err;
4207
4208 ubuf = (unsigned long) iov.iov_base;
4209 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
4210 start = ubuf >> PAGE_SHIFT;
4211 nr_pages = end - start;
4212
4213 if (ctx->account_mem) {
4214 ret = io_account_mem(ctx->user, nr_pages);
4215 if (ret)
4216 goto err;
4217 }
4218
4219 ret = 0;
4220 if (!pages || nr_pages > got_pages) {
4221 kfree(vmas);
4222 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004223 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07004224 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004225 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07004226 sizeof(struct vm_area_struct *),
4227 GFP_KERNEL);
4228 if (!pages || !vmas) {
4229 ret = -ENOMEM;
4230 if (ctx->account_mem)
4231 io_unaccount_mem(ctx->user, nr_pages);
4232 goto err;
4233 }
4234 got_pages = nr_pages;
4235 }
4236
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004237 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07004238 GFP_KERNEL);
4239 ret = -ENOMEM;
4240 if (!imu->bvec) {
4241 if (ctx->account_mem)
4242 io_unaccount_mem(ctx->user, nr_pages);
4243 goto err;
4244 }
4245
4246 ret = 0;
4247 down_read(&current->mm->mmap_sem);
Ira Weiny932f4a62019-05-13 17:17:03 -07004248 pret = get_user_pages(ubuf, nr_pages,
4249 FOLL_WRITE | FOLL_LONGTERM,
4250 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07004251 if (pret == nr_pages) {
4252 /* don't support file backed memory */
4253 for (j = 0; j < nr_pages; j++) {
4254 struct vm_area_struct *vma = vmas[j];
4255
4256 if (vma->vm_file &&
4257 !is_file_hugepages(vma->vm_file)) {
4258 ret = -EOPNOTSUPP;
4259 break;
4260 }
4261 }
4262 } else {
4263 ret = pret < 0 ? pret : -EFAULT;
4264 }
4265 up_read(&current->mm->mmap_sem);
4266 if (ret) {
4267 /*
4268 * if we did partial map, or found file backed vmas,
4269 * release any pages we did get
4270 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07004271 if (pret > 0)
4272 put_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07004273 if (ctx->account_mem)
4274 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004275 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07004276 goto err;
4277 }
4278
4279 off = ubuf & ~PAGE_MASK;
4280 size = iov.iov_len;
4281 for (j = 0; j < nr_pages; j++) {
4282 size_t vec_len;
4283
4284 vec_len = min_t(size_t, size, PAGE_SIZE - off);
4285 imu->bvec[j].bv_page = pages[j];
4286 imu->bvec[j].bv_len = vec_len;
4287 imu->bvec[j].bv_offset = off;
4288 off = 0;
4289 size -= vec_len;
4290 }
4291 /* store original address for later verification */
4292 imu->ubuf = ubuf;
4293 imu->len = iov.iov_len;
4294 imu->nr_bvecs = nr_pages;
4295
4296 ctx->nr_user_bufs++;
4297 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004298 kvfree(pages);
4299 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07004300 return 0;
4301err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004302 kvfree(pages);
4303 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07004304 io_sqe_buffer_unregister(ctx);
4305 return ret;
4306}
4307
Jens Axboe9b402842019-04-11 11:45:41 -06004308static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
4309{
4310 __s32 __user *fds = arg;
4311 int fd;
4312
4313 if (ctx->cq_ev_fd)
4314 return -EBUSY;
4315
4316 if (copy_from_user(&fd, fds, sizeof(*fds)))
4317 return -EFAULT;
4318
4319 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
4320 if (IS_ERR(ctx->cq_ev_fd)) {
4321 int ret = PTR_ERR(ctx->cq_ev_fd);
4322 ctx->cq_ev_fd = NULL;
4323 return ret;
4324 }
4325
4326 return 0;
4327}
4328
4329static int io_eventfd_unregister(struct io_ring_ctx *ctx)
4330{
4331 if (ctx->cq_ev_fd) {
4332 eventfd_ctx_put(ctx->cq_ev_fd);
4333 ctx->cq_ev_fd = NULL;
4334 return 0;
4335 }
4336
4337 return -ENXIO;
4338}
4339
Jens Axboe2b188cc2019-01-07 10:46:33 -07004340static void io_ring_ctx_free(struct io_ring_ctx *ctx)
4341{
Jens Axboe6b063142019-01-10 22:13:58 -07004342 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004343 if (ctx->sqo_mm)
4344 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07004345
4346 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07004347 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07004348 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06004349 io_eventfd_unregister(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07004350
Jens Axboe2b188cc2019-01-07 10:46:33 -07004351#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07004352 if (ctx->ring_sock) {
4353 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004354 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07004355 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004356#endif
4357
Hristo Venev75b28af2019-08-26 17:23:46 +00004358 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004359 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004360
4361 percpu_ref_exit(&ctx->refs);
4362 if (ctx->account_mem)
4363 io_unaccount_mem(ctx->user,
4364 ring_pages(ctx->sq_entries, ctx->cq_entries));
4365 free_uid(ctx->user);
Jens Axboe206aefd2019-11-07 18:27:42 -07004366 kfree(ctx->completions);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07004367 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004368 kfree(ctx);
4369}
4370
4371static __poll_t io_uring_poll(struct file *file, poll_table *wait)
4372{
4373 struct io_ring_ctx *ctx = file->private_data;
4374 __poll_t mask = 0;
4375
4376 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02004377 /*
4378 * synchronizes with barrier from wq_has_sleeper call in
4379 * io_commit_cqring
4380 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004381 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00004382 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
4383 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004384 mask |= EPOLLOUT | EPOLLWRNORM;
yangerkundaa5de52019-09-24 20:53:34 +08004385 if (READ_ONCE(ctx->rings->cq.head) != ctx->cached_cq_tail)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004386 mask |= EPOLLIN | EPOLLRDNORM;
4387
4388 return mask;
4389}
4390
4391static int io_uring_fasync(int fd, struct file *file, int on)
4392{
4393 struct io_ring_ctx *ctx = file->private_data;
4394
4395 return fasync_helper(fd, file, on, &ctx->cq_fasync);
4396}
4397
4398static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
4399{
4400 mutex_lock(&ctx->uring_lock);
4401 percpu_ref_kill(&ctx->refs);
4402 mutex_unlock(&ctx->uring_lock);
4403
Jens Axboe5262f562019-09-17 12:26:57 -06004404 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004405 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06004406
4407 if (ctx->io_wq)
4408 io_wq_cancel_all(ctx->io_wq);
4409
Jens Axboedef596e2019-01-09 08:59:42 -07004410 io_iopoll_reap_events(ctx);
Jens Axboe15dff282019-11-13 09:09:23 -07004411 /* if we failed setting up the ctx, we might not have any rings */
4412 if (ctx->rings)
4413 io_cqring_overflow_flush(ctx, true);
Jens Axboe206aefd2019-11-07 18:27:42 -07004414 wait_for_completion(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004415 io_ring_ctx_free(ctx);
4416}
4417
4418static int io_uring_release(struct inode *inode, struct file *file)
4419{
4420 struct io_ring_ctx *ctx = file->private_data;
4421
4422 file->private_data = NULL;
4423 io_ring_ctx_wait_and_kill(ctx);
4424 return 0;
4425}
4426
Jens Axboefcb323c2019-10-24 12:39:47 -06004427static void io_uring_cancel_files(struct io_ring_ctx *ctx,
4428 struct files_struct *files)
4429{
4430 struct io_kiocb *req;
4431 DEFINE_WAIT(wait);
4432
4433 while (!list_empty_careful(&ctx->inflight_list)) {
Jens Axboe768134d2019-11-10 20:30:53 -07004434 struct io_kiocb *cancel_req = NULL;
Jens Axboefcb323c2019-10-24 12:39:47 -06004435
4436 spin_lock_irq(&ctx->inflight_lock);
4437 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07004438 if (req->work.files != files)
4439 continue;
4440 /* req is being completed, ignore */
4441 if (!refcount_inc_not_zero(&req->refs))
4442 continue;
4443 cancel_req = req;
4444 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06004445 }
Jens Axboe768134d2019-11-10 20:30:53 -07004446 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06004447 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07004448 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06004449 spin_unlock_irq(&ctx->inflight_lock);
4450
Jens Axboe768134d2019-11-10 20:30:53 -07004451 /* We need to keep going until we don't find a matching req */
4452 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06004453 break;
Bob Liu2f6d9b92019-11-13 18:06:24 +08004454
4455 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
4456 io_put_req(cancel_req);
Jens Axboefcb323c2019-10-24 12:39:47 -06004457 schedule();
4458 }
Jens Axboe768134d2019-11-10 20:30:53 -07004459 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06004460}
4461
4462static int io_uring_flush(struct file *file, void *data)
4463{
4464 struct io_ring_ctx *ctx = file->private_data;
4465
4466 io_uring_cancel_files(ctx, data);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004467 if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) {
4468 io_cqring_overflow_flush(ctx, true);
Jens Axboefcb323c2019-10-24 12:39:47 -06004469 io_wq_cancel_all(ctx->io_wq);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004470 }
Jens Axboefcb323c2019-10-24 12:39:47 -06004471 return 0;
4472}
4473
Jens Axboe2b188cc2019-01-07 10:46:33 -07004474static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
4475{
4476 loff_t offset = (loff_t) vma->vm_pgoff << PAGE_SHIFT;
4477 unsigned long sz = vma->vm_end - vma->vm_start;
4478 struct io_ring_ctx *ctx = file->private_data;
4479 unsigned long pfn;
4480 struct page *page;
4481 void *ptr;
4482
4483 switch (offset) {
4484 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00004485 case IORING_OFF_CQ_RING:
4486 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004487 break;
4488 case IORING_OFF_SQES:
4489 ptr = ctx->sq_sqes;
4490 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004491 default:
4492 return -EINVAL;
4493 }
4494
4495 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07004496 if (sz > page_size(page))
Jens Axboe2b188cc2019-01-07 10:46:33 -07004497 return -EINVAL;
4498
4499 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
4500 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
4501}
4502
4503SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
4504 u32, min_complete, u32, flags, const sigset_t __user *, sig,
4505 size_t, sigsz)
4506{
4507 struct io_ring_ctx *ctx;
4508 long ret = -EBADF;
4509 int submitted = 0;
4510 struct fd f;
4511
Jens Axboe6c271ce2019-01-10 11:22:30 -07004512 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07004513 return -EINVAL;
4514
4515 f = fdget(fd);
4516 if (!f.file)
4517 return -EBADF;
4518
4519 ret = -EOPNOTSUPP;
4520 if (f.file->f_op != &io_uring_fops)
4521 goto out_fput;
4522
4523 ret = -ENXIO;
4524 ctx = f.file->private_data;
4525 if (!percpu_ref_tryget(&ctx->refs))
4526 goto out_fput;
4527
Jens Axboe6c271ce2019-01-10 11:22:30 -07004528 /*
4529 * For SQ polling, the thread will do all submissions and completions.
4530 * Just return the requested submit count, and wake the thread if
4531 * we were asked to.
4532 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06004533 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004534 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07004535 if (!list_empty_careful(&ctx->cq_overflow_list))
4536 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004537 if (flags & IORING_ENTER_SQ_WAKEUP)
4538 wake_up(&ctx->sqo_wait);
4539 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06004540 } else if (to_submit) {
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004541 struct mm_struct *cur_mm;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004542
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004543 to_submit = min(to_submit, ctx->sq_entries);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004544 mutex_lock(&ctx->uring_lock);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004545 /* already have mm, so io_submit_sqes() won't try to grab it */
4546 cur_mm = ctx->sqo_mm;
4547 submitted = io_submit_sqes(ctx, to_submit, f.file, fd,
4548 &cur_mm, false);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004549 mutex_unlock(&ctx->uring_lock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004550 }
4551 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07004552 unsigned nr_events = 0;
4553
Jens Axboe2b188cc2019-01-07 10:46:33 -07004554 min_complete = min(min_complete, ctx->cq_entries);
4555
Jens Axboedef596e2019-01-09 08:59:42 -07004556 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07004557 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07004558 } else {
4559 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
4560 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004561 }
4562
Pavel Begunkov6805b322019-10-08 02:18:42 +03004563 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004564out_fput:
4565 fdput(f);
4566 return submitted ? submitted : ret;
4567}
4568
4569static const struct file_operations io_uring_fops = {
4570 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06004571 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07004572 .mmap = io_uring_mmap,
4573 .poll = io_uring_poll,
4574 .fasync = io_uring_fasync,
4575};
4576
4577static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
4578 struct io_uring_params *p)
4579{
Hristo Venev75b28af2019-08-26 17:23:46 +00004580 struct io_rings *rings;
4581 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004582
Hristo Venev75b28af2019-08-26 17:23:46 +00004583 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
4584 if (size == SIZE_MAX)
4585 return -EOVERFLOW;
4586
4587 rings = io_mem_alloc(size);
4588 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004589 return -ENOMEM;
4590
Hristo Venev75b28af2019-08-26 17:23:46 +00004591 ctx->rings = rings;
4592 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
4593 rings->sq_ring_mask = p->sq_entries - 1;
4594 rings->cq_ring_mask = p->cq_entries - 1;
4595 rings->sq_ring_entries = p->sq_entries;
4596 rings->cq_ring_entries = p->cq_entries;
4597 ctx->sq_mask = rings->sq_ring_mask;
4598 ctx->cq_mask = rings->cq_ring_mask;
4599 ctx->sq_entries = rings->sq_ring_entries;
4600 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004601
4602 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07004603 if (size == SIZE_MAX) {
4604 io_mem_free(ctx->rings);
4605 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004606 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07004607 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004608
4609 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07004610 if (!ctx->sq_sqes) {
4611 io_mem_free(ctx->rings);
4612 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004613 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07004614 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004615
Jens Axboe2b188cc2019-01-07 10:46:33 -07004616 return 0;
4617}
4618
4619/*
4620 * Allocate an anonymous fd, this is what constitutes the application
4621 * visible backing of an io_uring instance. The application mmaps this
4622 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
4623 * we have to tie this fd to a socket for file garbage collection purposes.
4624 */
4625static int io_uring_get_fd(struct io_ring_ctx *ctx)
4626{
4627 struct file *file;
4628 int ret;
4629
4630#if defined(CONFIG_UNIX)
4631 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
4632 &ctx->ring_sock);
4633 if (ret)
4634 return ret;
4635#endif
4636
4637 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
4638 if (ret < 0)
4639 goto err;
4640
4641 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
4642 O_RDWR | O_CLOEXEC);
4643 if (IS_ERR(file)) {
4644 put_unused_fd(ret);
4645 ret = PTR_ERR(file);
4646 goto err;
4647 }
4648
4649#if defined(CONFIG_UNIX)
4650 ctx->ring_sock->file = file;
Jens Axboe6b063142019-01-10 22:13:58 -07004651 ctx->ring_sock->sk->sk_user_data = ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004652#endif
4653 fd_install(ret, file);
4654 return ret;
4655err:
4656#if defined(CONFIG_UNIX)
4657 sock_release(ctx->ring_sock);
4658 ctx->ring_sock = NULL;
4659#endif
4660 return ret;
4661}
4662
4663static int io_uring_create(unsigned entries, struct io_uring_params *p)
4664{
4665 struct user_struct *user = NULL;
4666 struct io_ring_ctx *ctx;
4667 bool account_mem;
4668 int ret;
4669
4670 if (!entries || entries > IORING_MAX_ENTRIES)
4671 return -EINVAL;
4672
4673 /*
4674 * Use twice as many entries for the CQ ring. It's possible for the
4675 * application to drive a higher depth than the size of the SQ ring,
4676 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06004677 * some flexibility in overcommitting a bit. If the application has
4678 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
4679 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07004680 */
4681 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06004682 if (p->flags & IORING_SETUP_CQSIZE) {
4683 /*
4684 * If IORING_SETUP_CQSIZE is set, we do the same roundup
4685 * to a power-of-two, if it isn't already. We do NOT impose
4686 * any cq vs sq ring sizing.
4687 */
4688 if (p->cq_entries < p->sq_entries || p->cq_entries > IORING_MAX_CQ_ENTRIES)
4689 return -EINVAL;
4690 p->cq_entries = roundup_pow_of_two(p->cq_entries);
4691 } else {
4692 p->cq_entries = 2 * p->sq_entries;
4693 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004694
4695 user = get_uid(current_user());
4696 account_mem = !capable(CAP_IPC_LOCK);
4697
4698 if (account_mem) {
4699 ret = io_account_mem(user,
4700 ring_pages(p->sq_entries, p->cq_entries));
4701 if (ret) {
4702 free_uid(user);
4703 return ret;
4704 }
4705 }
4706
4707 ctx = io_ring_ctx_alloc(p);
4708 if (!ctx) {
4709 if (account_mem)
4710 io_unaccount_mem(user, ring_pages(p->sq_entries,
4711 p->cq_entries));
4712 free_uid(user);
4713 return -ENOMEM;
4714 }
4715 ctx->compat = in_compat_syscall();
4716 ctx->account_mem = account_mem;
4717 ctx->user = user;
4718
4719 ret = io_allocate_scq_urings(ctx, p);
4720 if (ret)
4721 goto err;
4722
Jens Axboe6c271ce2019-01-10 11:22:30 -07004723 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004724 if (ret)
4725 goto err;
4726
Jens Axboe2b188cc2019-01-07 10:46:33 -07004727 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00004728 p->sq_off.head = offsetof(struct io_rings, sq.head);
4729 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
4730 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
4731 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
4732 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
4733 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
4734 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004735
4736 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00004737 p->cq_off.head = offsetof(struct io_rings, cq.head);
4738 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
4739 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
4740 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
4741 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
4742 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06004743
Jens Axboe044c1ab2019-10-28 09:15:33 -06004744 /*
4745 * Install ring fd as the very last thing, so we don't risk someone
4746 * having closed it before we finish setup
4747 */
4748 ret = io_uring_get_fd(ctx);
4749 if (ret < 0)
4750 goto err;
4751
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004752 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004753 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004754 return ret;
4755err:
4756 io_ring_ctx_wait_and_kill(ctx);
4757 return ret;
4758}
4759
4760/*
4761 * Sets up an aio uring context, and returns the fd. Applications asks for a
4762 * ring size, we return the actual sq/cq ring sizes (among other things) in the
4763 * params structure passed in.
4764 */
4765static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
4766{
4767 struct io_uring_params p;
4768 long ret;
4769 int i;
4770
4771 if (copy_from_user(&p, params, sizeof(p)))
4772 return -EFAULT;
4773 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
4774 if (p.resv[i])
4775 return -EINVAL;
4776 }
4777
Jens Axboe6c271ce2019-01-10 11:22:30 -07004778 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe33a107f2019-10-04 12:10:03 -06004779 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE))
Jens Axboe2b188cc2019-01-07 10:46:33 -07004780 return -EINVAL;
4781
4782 ret = io_uring_create(entries, &p);
4783 if (ret < 0)
4784 return ret;
4785
4786 if (copy_to_user(params, &p, sizeof(p)))
4787 return -EFAULT;
4788
4789 return ret;
4790}
4791
4792SYSCALL_DEFINE2(io_uring_setup, u32, entries,
4793 struct io_uring_params __user *, params)
4794{
4795 return io_uring_setup(entries, params);
4796}
4797
Jens Axboeedafcce2019-01-09 09:16:05 -07004798static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
4799 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06004800 __releases(ctx->uring_lock)
4801 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07004802{
4803 int ret;
4804
Jens Axboe35fa71a2019-04-22 10:23:23 -06004805 /*
4806 * We're inside the ring mutex, if the ref is already dying, then
4807 * someone else killed the ctx or is already going through
4808 * io_uring_register().
4809 */
4810 if (percpu_ref_is_dying(&ctx->refs))
4811 return -ENXIO;
4812
Jens Axboeedafcce2019-01-09 09:16:05 -07004813 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06004814
4815 /*
4816 * Drop uring mutex before waiting for references to exit. If another
4817 * thread is currently inside io_uring_enter() it might need to grab
4818 * the uring_lock to make progress. If we hold it here across the drain
4819 * wait, then we can deadlock. It's safe to drop the mutex here, since
4820 * no new references will come in after we've killed the percpu ref.
4821 */
4822 mutex_unlock(&ctx->uring_lock);
Jens Axboe206aefd2019-11-07 18:27:42 -07004823 wait_for_completion(&ctx->completions[0]);
Jens Axboeb19062a2019-04-15 10:49:38 -06004824 mutex_lock(&ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07004825
4826 switch (opcode) {
4827 case IORING_REGISTER_BUFFERS:
4828 ret = io_sqe_buffer_register(ctx, arg, nr_args);
4829 break;
4830 case IORING_UNREGISTER_BUFFERS:
4831 ret = -EINVAL;
4832 if (arg || nr_args)
4833 break;
4834 ret = io_sqe_buffer_unregister(ctx);
4835 break;
Jens Axboe6b063142019-01-10 22:13:58 -07004836 case IORING_REGISTER_FILES:
4837 ret = io_sqe_files_register(ctx, arg, nr_args);
4838 break;
4839 case IORING_UNREGISTER_FILES:
4840 ret = -EINVAL;
4841 if (arg || nr_args)
4842 break;
4843 ret = io_sqe_files_unregister(ctx);
4844 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06004845 case IORING_REGISTER_FILES_UPDATE:
4846 ret = io_sqe_files_update(ctx, arg, nr_args);
4847 break;
Jens Axboe9b402842019-04-11 11:45:41 -06004848 case IORING_REGISTER_EVENTFD:
4849 ret = -EINVAL;
4850 if (nr_args != 1)
4851 break;
4852 ret = io_eventfd_register(ctx, arg);
4853 break;
4854 case IORING_UNREGISTER_EVENTFD:
4855 ret = -EINVAL;
4856 if (arg || nr_args)
4857 break;
4858 ret = io_eventfd_unregister(ctx);
4859 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07004860 default:
4861 ret = -EINVAL;
4862 break;
4863 }
4864
4865 /* bring the ctx back to life */
Jens Axboe206aefd2019-11-07 18:27:42 -07004866 reinit_completion(&ctx->completions[0]);
Jens Axboeedafcce2019-01-09 09:16:05 -07004867 percpu_ref_reinit(&ctx->refs);
4868 return ret;
4869}
4870
4871SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
4872 void __user *, arg, unsigned int, nr_args)
4873{
4874 struct io_ring_ctx *ctx;
4875 long ret = -EBADF;
4876 struct fd f;
4877
4878 f = fdget(fd);
4879 if (!f.file)
4880 return -EBADF;
4881
4882 ret = -EOPNOTSUPP;
4883 if (f.file->f_op != &io_uring_fops)
4884 goto out_fput;
4885
4886 ctx = f.file->private_data;
4887
4888 mutex_lock(&ctx->uring_lock);
4889 ret = __io_uring_register(ctx, opcode, arg, nr_args);
4890 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004891 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
4892 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07004893out_fput:
4894 fdput(f);
4895 return ret;
4896}
4897
Jens Axboe2b188cc2019-01-07 10:46:33 -07004898static int __init io_uring_init(void)
4899{
4900 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
4901 return 0;
4902};
4903__initcall(io_uring_init);