blob: f6f5871bd7d2ef8ddcb121b781836e1f10bd3e9b [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:
553 /*
554 * We know REQ_F_ISREG is not set on some of these
555 * opcodes, but this enables us to keep the check in
556 * just one place.
557 */
558 if (!(req->flags & REQ_F_ISREG))
559 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe6cc47d12019-09-18 11:18:23 -0600560 break;
561 }
Jens Axboe561fb042019-10-24 07:25:42 -0600562 if (io_sqe_needs_user(req->submit.sqe))
563 req->work.flags |= IO_WQ_WORK_NEEDS_USER;
Jens Axboe54a91f32019-09-10 09:15:04 -0600564 }
565
Jens Axboe94ae5e72019-11-14 19:39:52 -0700566 *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -0600567 return do_hashed;
568}
569
Jackie Liua197f662019-11-08 08:09:12 -0700570static inline void io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -0600571{
Jackie Liua197f662019-11-08 08:09:12 -0700572 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe94ae5e72019-11-14 19:39:52 -0700573 struct io_kiocb *link;
574 bool do_hashed;
575
576 do_hashed = io_prep_async_work(req, &link);
Jens Axboe561fb042019-10-24 07:25:42 -0600577
578 trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work,
579 req->flags);
580 if (!do_hashed) {
581 io_wq_enqueue(ctx->io_wq, &req->work);
582 } else {
583 io_wq_enqueue_hashed(ctx->io_wq, &req->work,
584 file_inode(req->file));
585 }
Jens Axboe94ae5e72019-11-14 19:39:52 -0700586
587 if (link)
588 io_queue_linked_timeout(link);
Jens Axboe18d9be12019-09-10 09:13:05 -0600589}
590
Jens Axboe5262f562019-09-17 12:26:57 -0600591static void io_kill_timeout(struct io_kiocb *req)
592{
593 int ret;
594
Jens Axboead8a48a2019-11-15 08:49:11 -0700595 ret = hrtimer_try_to_cancel(&req->timeout.data->timer);
Jens Axboe5262f562019-09-17 12:26:57 -0600596 if (ret != -1) {
597 atomic_inc(&req->ctx->cq_timeouts);
Jens Axboe842f9612019-10-29 12:34:10 -0600598 list_del_init(&req->list);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700599 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800600 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -0600601 }
602}
603
604static void io_kill_timeouts(struct io_ring_ctx *ctx)
605{
606 struct io_kiocb *req, *tmp;
607
608 spin_lock_irq(&ctx->completion_lock);
609 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
610 io_kill_timeout(req);
611 spin_unlock_irq(&ctx->completion_lock);
612}
613
Jens Axboede0617e2019-04-06 21:51:27 -0600614static void io_commit_cqring(struct io_ring_ctx *ctx)
615{
616 struct io_kiocb *req;
617
Jens Axboe5262f562019-09-17 12:26:57 -0600618 while ((req = io_get_timeout_req(ctx)) != NULL)
619 io_kill_timeout(req);
620
Jens Axboede0617e2019-04-06 21:51:27 -0600621 __io_commit_cqring(ctx);
622
623 while ((req = io_get_deferred_req(ctx)) != NULL) {
624 req->flags |= REQ_F_IO_DRAINED;
Jackie Liua197f662019-11-08 08:09:12 -0700625 io_queue_async_work(req);
Jens Axboede0617e2019-04-06 21:51:27 -0600626 }
627}
628
Jens Axboe2b188cc2019-01-07 10:46:33 -0700629static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
630{
Hristo Venev75b28af2019-08-26 17:23:46 +0000631 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700632 unsigned tail;
633
634 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +0200635 /*
636 * writes to the cq entry need to come after reading head; the
637 * control dependency is enough as we're using WRITE_ONCE to
638 * fill the cq entry
639 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000640 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700641 return NULL;
642
643 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +0000644 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -0700645}
646
Jens Axboe8c838782019-03-12 15:48:16 -0600647static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
648{
649 if (waitqueue_active(&ctx->wait))
650 wake_up(&ctx->wait);
651 if (waitqueue_active(&ctx->sqo_wait))
652 wake_up(&ctx->sqo_wait);
Jens Axboe9b402842019-04-11 11:45:41 -0600653 if (ctx->cq_ev_fd)
654 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -0600655}
656
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700657static void io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700658{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700659 struct io_rings *rings = ctx->rings;
660 struct io_uring_cqe *cqe;
661 struct io_kiocb *req;
662 unsigned long flags;
663 LIST_HEAD(list);
664
665 if (!force) {
666 if (list_empty_careful(&ctx->cq_overflow_list))
667 return;
668 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
669 rings->cq_ring_entries))
670 return;
671 }
672
673 spin_lock_irqsave(&ctx->completion_lock, flags);
674
675 /* if force is set, the ring is going away. always drop after that */
676 if (force)
677 ctx->cq_overflow_flushed = true;
678
679 while (!list_empty(&ctx->cq_overflow_list)) {
680 cqe = io_get_cqring(ctx);
681 if (!cqe && !force)
682 break;
683
684 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
685 list);
686 list_move(&req->list, &list);
687 if (cqe) {
688 WRITE_ONCE(cqe->user_data, req->user_data);
689 WRITE_ONCE(cqe->res, req->result);
690 WRITE_ONCE(cqe->flags, 0);
691 } else {
692 WRITE_ONCE(ctx->rings->cq_overflow,
693 atomic_inc_return(&ctx->cached_cq_overflow));
694 }
695 }
696
697 io_commit_cqring(ctx);
698 spin_unlock_irqrestore(&ctx->completion_lock, flags);
699 io_cqring_ev_posted(ctx);
700
701 while (!list_empty(&list)) {
702 req = list_first_entry(&list, struct io_kiocb, list);
703 list_del(&req->list);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800704 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700705 }
706}
707
Jens Axboe78e19bb2019-11-06 15:21:34 -0700708static void io_cqring_fill_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700709{
Jens Axboe78e19bb2019-11-06 15:21:34 -0700710 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700711 struct io_uring_cqe *cqe;
712
Jens Axboe78e19bb2019-11-06 15:21:34 -0700713 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -0700714
Jens Axboe2b188cc2019-01-07 10:46:33 -0700715 /*
716 * If we can't get a cq entry, userspace overflowed the
717 * submission (by quite a lot). Increment the overflow count in
718 * the ring.
719 */
720 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700721 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -0700722 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700723 WRITE_ONCE(cqe->res, res);
724 WRITE_ONCE(cqe->flags, 0);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700725 } else if (ctx->cq_overflow_flushed) {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700726 WRITE_ONCE(ctx->rings->cq_overflow,
727 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700728 } else {
729 refcount_inc(&req->refs);
730 req->result = res;
731 list_add_tail(&req->list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700732 }
733}
734
Jens Axboe78e19bb2019-11-06 15:21:34 -0700735static void io_cqring_add_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700736{
Jens Axboe78e19bb2019-11-06 15:21:34 -0700737 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700738 unsigned long flags;
739
740 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700741 io_cqring_fill_event(req, res);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700742 io_commit_cqring(ctx);
743 spin_unlock_irqrestore(&ctx->completion_lock, flags);
744
Jens Axboe8c838782019-03-12 15:48:16 -0600745 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700746}
747
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700748static inline bool io_is_fallback_req(struct io_kiocb *req)
749{
750 return req == (struct io_kiocb *)
751 ((unsigned long) req->ctx->fallback_req & ~1UL);
752}
753
754static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
755{
756 struct io_kiocb *req;
757
758 req = ctx->fallback_req;
759 if (!test_and_set_bit_lock(0, (unsigned long *) ctx->fallback_req))
760 return req;
761
762 return NULL;
763}
764
Jens Axboe2579f912019-01-09 09:10:43 -0700765static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
766 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700767{
Jens Axboefd6fab22019-03-14 16:30:06 -0600768 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700769 struct io_kiocb *req;
770
771 if (!percpu_ref_tryget(&ctx->refs))
772 return NULL;
773
Jens Axboe2579f912019-01-09 09:10:43 -0700774 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -0600775 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -0700776 if (unlikely(!req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700777 goto fallback;
Jens Axboe2579f912019-01-09 09:10:43 -0700778 } else if (!state->free_reqs) {
779 size_t sz;
780 int ret;
781
782 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -0600783 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
784
785 /*
786 * Bulk alloc is all-or-nothing. If we fail to get a batch,
787 * retry single alloc to be on the safe side.
788 */
789 if (unlikely(ret <= 0)) {
790 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
791 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700792 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -0600793 ret = 1;
794 }
Jens Axboe2579f912019-01-09 09:10:43 -0700795 state->free_reqs = ret - 1;
796 state->cur_req = 1;
797 req = state->reqs[0];
798 } else {
799 req = state->reqs[state->cur_req];
800 state->free_reqs--;
801 state->cur_req++;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700802 }
803
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700804got_it:
Jens Axboe60c112b2019-06-21 10:20:18 -0600805 req->file = NULL;
Jens Axboe2579f912019-01-09 09:10:43 -0700806 req->ctx = ctx;
807 req->flags = 0;
Jens Axboee65ef562019-03-12 10:16:44 -0600808 /* one is dropped after submission, the other at completion */
809 refcount_set(&req->refs, 2);
Jens Axboe9e645e112019-05-10 16:07:28 -0600810 req->result = 0;
Jens Axboe561fb042019-10-24 07:25:42 -0600811 INIT_IO_WORK(&req->work, io_wq_submit_work);
Jens Axboe2579f912019-01-09 09:10:43 -0700812 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700813fallback:
814 req = io_get_fallback_req(ctx);
815 if (req)
816 goto got_it;
Pavel Begunkov6805b322019-10-08 02:18:42 +0300817 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700818 return NULL;
819}
820
Jens Axboedef596e2019-01-09 08:59:42 -0700821static void io_free_req_many(struct io_ring_ctx *ctx, void **reqs, int *nr)
822{
823 if (*nr) {
824 kmem_cache_free_bulk(req_cachep, *nr, reqs);
Pavel Begunkov6805b322019-10-08 02:18:42 +0300825 percpu_ref_put_many(&ctx->refs, *nr);
Jens Axboedef596e2019-01-09 08:59:42 -0700826 *nr = 0;
827 }
828}
829
Jens Axboe9e645e112019-05-10 16:07:28 -0600830static void __io_free_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700831{
Jens Axboefcb323c2019-10-24 12:39:47 -0600832 struct io_ring_ctx *ctx = req->ctx;
833
Pavel Begunkovbbad27b2019-11-19 23:32:47 +0300834 if (req->flags & REQ_F_FREE_SQE)
835 kfree(req->submit.sqe);
Jens Axboe09bb8392019-03-13 12:39:28 -0600836 if (req->file && !(req->flags & REQ_F_FIXED_FILE))
837 fput(req->file);
Jens Axboefcb323c2019-10-24 12:39:47 -0600838 if (req->flags & REQ_F_INFLIGHT) {
839 unsigned long flags;
840
841 spin_lock_irqsave(&ctx->inflight_lock, flags);
842 list_del(&req->inflight_entry);
843 if (waitqueue_active(&ctx->inflight_wait))
844 wake_up(&ctx->inflight_wait);
845 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
846 }
Jens Axboead8a48a2019-11-15 08:49:11 -0700847 if (req->flags & REQ_F_TIMEOUT)
848 kfree(req->timeout.data);
Jens Axboefcb323c2019-10-24 12:39:47 -0600849 percpu_ref_put(&ctx->refs);
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700850 if (likely(!io_is_fallback_req(req)))
851 kmem_cache_free(req_cachep, req);
852 else
853 clear_bit_unlock(0, (unsigned long *) ctx->fallback_req);
Jens Axboee65ef562019-03-12 10:16:44 -0600854}
855
Jackie Liua197f662019-11-08 08:09:12 -0700856static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -0600857{
Jackie Liua197f662019-11-08 08:09:12 -0700858 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -0700859 int ret;
860
Jens Axboead8a48a2019-11-15 08:49:11 -0700861 ret = hrtimer_try_to_cancel(&req->timeout.data->timer);
Jens Axboe2665abf2019-11-05 12:40:47 -0700862 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -0700863 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -0700864 io_commit_cqring(ctx);
865 req->flags &= ~REQ_F_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +0800866 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -0700867 return true;
868 }
869
870 return false;
871}
872
Jens Axboeba816ad2019-09-28 11:36:45 -0600873static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -0600874{
Jens Axboe2665abf2019-11-05 12:40:47 -0700875 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -0600876 struct io_kiocb *nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -0700877 bool wake_ev = false;
Jens Axboe9e645e112019-05-10 16:07:28 -0600878
Jens Axboe4d7dd462019-11-20 13:03:52 -0700879 /* Already got next link */
880 if (req->flags & REQ_F_LINK_NEXT)
881 return;
882
Jens Axboe9e645e112019-05-10 16:07:28 -0600883 /*
884 * The list should never be empty when we are called here. But could
885 * potentially happen if the chain is messed up, check to be on the
886 * safe side.
887 */
888 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, list);
Jens Axboe2665abf2019-11-05 12:40:47 -0700889 while (nxt) {
Jens Axboe76a46e02019-11-10 23:34:16 -0700890 list_del_init(&nxt->list);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700891
892 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
893 (nxt->flags & REQ_F_TIMEOUT)) {
894 wake_ev |= io_link_cancel_timeout(nxt);
895 nxt = list_first_entry_or_null(&req->link_list,
896 struct io_kiocb, list);
897 req->flags &= ~REQ_F_LINK_TIMEOUT;
898 continue;
899 }
Jens Axboe9e645e112019-05-10 16:07:28 -0600900 if (!list_empty(&req->link_list)) {
901 INIT_LIST_HEAD(&nxt->link_list);
902 list_splice(&req->link_list, &nxt->link_list);
903 nxt->flags |= REQ_F_LINK;
904 }
905
Pavel Begunkovb18fdf72019-11-21 23:21:02 +0300906 *nxtptr = nxt;
Jens Axboe94ae5e72019-11-14 19:39:52 -0700907 break;
Jens Axboe9e645e112019-05-10 16:07:28 -0600908 }
Jens Axboe2665abf2019-11-05 12:40:47 -0700909
Jens Axboe4d7dd462019-11-20 13:03:52 -0700910 req->flags |= REQ_F_LINK_NEXT;
Jens Axboe2665abf2019-11-05 12:40:47 -0700911 if (wake_ev)
912 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -0600913}
914
915/*
916 * Called if REQ_F_LINK is set, and we fail the head request
917 */
918static void io_fail_links(struct io_kiocb *req)
919{
Jens Axboe2665abf2019-11-05 12:40:47 -0700920 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -0600921 struct io_kiocb *link;
Jens Axboe2665abf2019-11-05 12:40:47 -0700922 unsigned long flags;
923
924 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -0600925
926 while (!list_empty(&req->link_list)) {
927 link = list_first_entry(&req->link_list, struct io_kiocb, list);
Jens Axboe2665abf2019-11-05 12:40:47 -0700928 list_del_init(&link->list);
Jens Axboe9e645e112019-05-10 16:07:28 -0600929
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +0200930 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -0700931
932 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
933 link->submit.sqe->opcode == IORING_OP_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -0700934 io_link_cancel_timeout(link);
Jens Axboe2665abf2019-11-05 12:40:47 -0700935 } else {
Jens Axboe78e19bb2019-11-06 15:21:34 -0700936 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe978db572019-11-14 22:39:04 -0700937 __io_double_put_req(link);
Jens Axboe2665abf2019-11-05 12:40:47 -0700938 }
Jens Axboe5d960722019-11-19 15:31:28 -0700939 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -0600940 }
Jens Axboe2665abf2019-11-05 12:40:47 -0700941
942 io_commit_cqring(ctx);
943 spin_unlock_irqrestore(&ctx->completion_lock, flags);
944 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -0600945}
946
Jens Axboe4d7dd462019-11-20 13:03:52 -0700947static void io_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -0600948{
Jens Axboe4d7dd462019-11-20 13:03:52 -0700949 if (likely(!(req->flags & REQ_F_LINK)))
Jens Axboe2665abf2019-11-05 12:40:47 -0700950 return;
Jens Axboe2665abf2019-11-05 12:40:47 -0700951
Jens Axboe9e645e112019-05-10 16:07:28 -0600952 /*
953 * If LINK is set, we have dependent requests in this chain. If we
954 * didn't fail this request, queue the first one up, moving any other
955 * dependencies to the next request. In case of failure, fail the rest
956 * of the chain.
957 */
Jens Axboe2665abf2019-11-05 12:40:47 -0700958 if (req->flags & REQ_F_FAIL_LINK) {
959 io_fail_links(req);
Jens Axboe7c9e7f02019-11-12 08:15:53 -0700960 } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) ==
961 REQ_F_LINK_TIMEOUT) {
Jens Axboe2665abf2019-11-05 12:40:47 -0700962 struct io_ring_ctx *ctx = req->ctx;
963 unsigned long flags;
964
965 /*
966 * If this is a timeout link, we could be racing with the
967 * timeout timer. Grab the completion lock for this case to
Jens Axboe7c9e7f02019-11-12 08:15:53 -0700968 * protect against that.
Jens Axboe2665abf2019-11-05 12:40:47 -0700969 */
970 spin_lock_irqsave(&ctx->completion_lock, flags);
971 io_req_link_next(req, nxt);
972 spin_unlock_irqrestore(&ctx->completion_lock, flags);
973 } else {
974 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -0600975 }
Jens Axboe4d7dd462019-11-20 13:03:52 -0700976}
Jens Axboe9e645e112019-05-10 16:07:28 -0600977
Jackie Liuc69f8db2019-11-09 11:00:08 +0800978static void io_free_req(struct io_kiocb *req)
979{
Pavel Begunkov944e58b2019-11-21 23:21:01 +0300980 struct io_kiocb *nxt = NULL;
981
982 io_req_find_next(req, &nxt);
Pavel Begunkov70cf9f32019-11-21 23:21:00 +0300983 __io_free_req(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +0300984
985 if (nxt)
986 io_queue_async_work(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +0800987}
988
Jens Axboeba816ad2019-09-28 11:36:45 -0600989/*
990 * Drop reference to request, return next in chain (if there is one) if this
991 * was the last reference to this request.
992 */
Jackie Liuec9c02a2019-11-08 23:50:36 +0800993static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboee65ef562019-03-12 10:16:44 -0600994{
Jens Axboeba816ad2019-09-28 11:36:45 -0600995 struct io_kiocb *nxt = NULL;
996
Jens Axboe4d7dd462019-11-20 13:03:52 -0700997 io_req_find_next(req, &nxt);
998
Jens Axboee65ef562019-03-12 10:16:44 -0600999 if (refcount_dec_and_test(&req->refs))
Jens Axboe4d7dd462019-11-20 13:03:52 -07001000 __io_free_req(req);
Jens Axboeba816ad2019-09-28 11:36:45 -06001001
Jens Axboeba816ad2019-09-28 11:36:45 -06001002 if (nxt) {
Jens Axboe561fb042019-10-24 07:25:42 -06001003 if (nxtptr)
Jens Axboeba816ad2019-09-28 11:36:45 -06001004 *nxtptr = nxt;
Jens Axboe561fb042019-10-24 07:25:42 -06001005 else
Jackie Liua197f662019-11-08 08:09:12 -07001006 io_queue_async_work(nxt);
Jens Axboeba816ad2019-09-28 11:36:45 -06001007 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001008}
1009
Jens Axboe2b188cc2019-01-07 10:46:33 -07001010static void io_put_req(struct io_kiocb *req)
1011{
Jens Axboedef596e2019-01-09 08:59:42 -07001012 if (refcount_dec_and_test(&req->refs))
1013 io_free_req(req);
1014}
1015
Jens Axboe978db572019-11-14 22:39:04 -07001016/*
1017 * Must only be used if we don't need to care about links, usually from
1018 * within the completion handling itself.
1019 */
1020static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06001021{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001022 /* drop both submit and complete references */
1023 if (refcount_sub_and_test(2, &req->refs))
1024 __io_free_req(req);
1025}
1026
Jens Axboe978db572019-11-14 22:39:04 -07001027static void io_double_put_req(struct io_kiocb *req)
1028{
1029 /* drop both submit and complete references */
1030 if (refcount_sub_and_test(2, &req->refs))
1031 io_free_req(req);
1032}
1033
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001034static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06001035{
Jens Axboe84f97dc2019-11-06 11:27:53 -07001036 struct io_rings *rings = ctx->rings;
1037
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001038 /*
1039 * noflush == true is from the waitqueue handler, just ensure we wake
1040 * up the task, and the next invocation will flush the entries. We
1041 * cannot safely to it from here.
1042 */
1043 if (noflush && !list_empty(&ctx->cq_overflow_list))
1044 return -1U;
1045
1046 io_cqring_overflow_flush(ctx, false);
1047
Jens Axboea3a0e432019-08-20 11:03:11 -06001048 /* See comment at the top of this file */
1049 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00001050 return READ_ONCE(rings->cq.tail) - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06001051}
1052
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03001053static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1054{
1055 struct io_rings *rings = ctx->rings;
1056
1057 /* make sure SQ entry isn't read before tail */
1058 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1059}
1060
Jens Axboedef596e2019-01-09 08:59:42 -07001061/*
1062 * Find and free completed poll iocbs
1063 */
1064static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
1065 struct list_head *done)
1066{
1067 void *reqs[IO_IOPOLL_BATCH];
1068 struct io_kiocb *req;
Jens Axboe09bb8392019-03-13 12:39:28 -06001069 int to_free;
Jens Axboedef596e2019-01-09 08:59:42 -07001070
Jens Axboe09bb8392019-03-13 12:39:28 -06001071 to_free = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001072 while (!list_empty(done)) {
1073 req = list_first_entry(done, struct io_kiocb, list);
1074 list_del(&req->list);
1075
Jens Axboe78e19bb2019-11-06 15:21:34 -07001076 io_cqring_fill_event(req, req->result);
Jens Axboedef596e2019-01-09 08:59:42 -07001077 (*nr_events)++;
1078
Jens Axboe09bb8392019-03-13 12:39:28 -06001079 if (refcount_dec_and_test(&req->refs)) {
1080 /* If we're not using fixed files, we have to pair the
1081 * completion part with the file put. Use regular
1082 * completions for those, only batch free for fixed
Jens Axboe9e645e112019-05-10 16:07:28 -06001083 * file and non-linked commands.
Jens Axboe09bb8392019-03-13 12:39:28 -06001084 */
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03001085 if (((req->flags &
1086 (REQ_F_FIXED_FILE|REQ_F_LINK|REQ_F_FREE_SQE)) ==
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001087 REQ_F_FIXED_FILE) && !io_is_fallback_req(req)) {
Jens Axboe09bb8392019-03-13 12:39:28 -06001088 reqs[to_free++] = req;
1089 if (to_free == ARRAY_SIZE(reqs))
1090 io_free_req_many(ctx, reqs, &to_free);
Jens Axboe6b063142019-01-10 22:13:58 -07001091 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001092 io_free_req(req);
Jens Axboe6b063142019-01-10 22:13:58 -07001093 }
Jens Axboe9a56a232019-01-09 09:06:50 -07001094 }
Jens Axboedef596e2019-01-09 08:59:42 -07001095 }
Jens Axboedef596e2019-01-09 08:59:42 -07001096
Jens Axboe09bb8392019-03-13 12:39:28 -06001097 io_commit_cqring(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07001098 io_free_req_many(ctx, reqs, &to_free);
1099}
1100
1101static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1102 long min)
1103{
1104 struct io_kiocb *req, *tmp;
1105 LIST_HEAD(done);
1106 bool spin;
1107 int ret;
1108
1109 /*
1110 * Only spin for completions if we don't have multiple devices hanging
1111 * off our complete list, and we're under the requested amount.
1112 */
1113 spin = !ctx->poll_multi_file && *nr_events < min;
1114
1115 ret = 0;
1116 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
1117 struct kiocb *kiocb = &req->rw;
1118
1119 /*
1120 * Move completed entries to our local list. If we find a
1121 * request that requires polling, break out and complete
1122 * the done list first, if we have entries there.
1123 */
1124 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
1125 list_move_tail(&req->list, &done);
1126 continue;
1127 }
1128 if (!list_empty(&done))
1129 break;
1130
1131 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1132 if (ret < 0)
1133 break;
1134
1135 if (ret && spin)
1136 spin = false;
1137 ret = 0;
1138 }
1139
1140 if (!list_empty(&done))
1141 io_iopoll_complete(ctx, nr_events, &done);
1142
1143 return ret;
1144}
1145
1146/*
1147 * Poll for a mininum of 'min' events. Note that if min == 0 we consider that a
1148 * non-spinning poll check - we'll still enter the driver poll loop, but only
1149 * as a non-spinning completion check.
1150 */
1151static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
1152 long min)
1153{
Jens Axboe08f54392019-08-21 22:19:11 -06001154 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07001155 int ret;
1156
1157 ret = io_do_iopoll(ctx, nr_events, min);
1158 if (ret < 0)
1159 return ret;
1160 if (!min || *nr_events >= min)
1161 return 0;
1162 }
1163
1164 return 1;
1165}
1166
1167/*
1168 * We can't just wait for polled events to come to us, we have to actively
1169 * find and complete them.
1170 */
1171static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
1172{
1173 if (!(ctx->flags & IORING_SETUP_IOPOLL))
1174 return;
1175
1176 mutex_lock(&ctx->uring_lock);
1177 while (!list_empty(&ctx->poll_list)) {
1178 unsigned int nr_events = 0;
1179
1180 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -06001181
1182 /*
1183 * Ensure we allow local-to-the-cpu processing to take place,
1184 * in this case we need to ensure that we reap all events.
1185 */
1186 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -07001187 }
1188 mutex_unlock(&ctx->uring_lock);
1189}
1190
Jens Axboe2b2ed972019-10-25 10:06:15 -06001191static int __io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1192 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07001193{
Jens Axboe2b2ed972019-10-25 10:06:15 -06001194 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001195
1196 do {
1197 int tmin = 0;
1198
Jens Axboe500f9fb2019-08-19 12:15:59 -06001199 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06001200 * Don't enter poll loop if we already have events pending.
1201 * If we do, we can potentially be spinning for commands that
1202 * already triggered a CQE (eg in error).
1203 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001204 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06001205 break;
1206
1207 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06001208 * If a submit got punted to a workqueue, we can have the
1209 * application entering polling for a command before it gets
1210 * issued. That app will hold the uring_lock for the duration
1211 * of the poll right here, so we need to take a breather every
1212 * now and then to ensure that the issue has a chance to add
1213 * the poll to the issued list. Otherwise we can spin here
1214 * forever, while the workqueue is stuck trying to acquire the
1215 * very same mutex.
1216 */
1217 if (!(++iters & 7)) {
1218 mutex_unlock(&ctx->uring_lock);
1219 mutex_lock(&ctx->uring_lock);
1220 }
1221
Jens Axboedef596e2019-01-09 08:59:42 -07001222 if (*nr_events < min)
1223 tmin = min - *nr_events;
1224
1225 ret = io_iopoll_getevents(ctx, nr_events, tmin);
1226 if (ret <= 0)
1227 break;
1228 ret = 0;
1229 } while (min && !*nr_events && !need_resched());
1230
Jens Axboe2b2ed972019-10-25 10:06:15 -06001231 return ret;
1232}
1233
1234static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1235 long min)
1236{
1237 int ret;
1238
1239 /*
1240 * We disallow the app entering submit/complete with polling, but we
1241 * still need to lock the ring to prevent racing with polled issue
1242 * that got punted to a workqueue.
1243 */
1244 mutex_lock(&ctx->uring_lock);
1245 ret = __io_iopoll_check(ctx, nr_events, min);
Jens Axboe500f9fb2019-08-19 12:15:59 -06001246 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001247 return ret;
1248}
1249
Jens Axboe491381ce2019-10-17 09:20:46 -06001250static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001251{
Jens Axboe491381ce2019-10-17 09:20:46 -06001252 /*
1253 * Tell lockdep we inherited freeze protection from submission
1254 * thread.
1255 */
1256 if (req->flags & REQ_F_ISREG) {
1257 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001258
Jens Axboe491381ce2019-10-17 09:20:46 -06001259 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001260 }
Jens Axboe491381ce2019-10-17 09:20:46 -06001261 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001262}
1263
Jens Axboeba816ad2019-09-28 11:36:45 -06001264static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001265{
1266 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1267
Jens Axboe491381ce2019-10-17 09:20:46 -06001268 if (kiocb->ki_flags & IOCB_WRITE)
1269 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001270
Jens Axboe9e645e112019-05-10 16:07:28 -06001271 if ((req->flags & REQ_F_LINK) && res != req->result)
1272 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001273 io_cqring_add_event(req, res);
Jens Axboeba816ad2019-09-28 11:36:45 -06001274}
1275
1276static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1277{
1278 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1279
1280 io_complete_rw_common(kiocb, res);
Jens Axboee65ef562019-03-12 10:16:44 -06001281 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001282}
1283
Jens Axboeba816ad2019-09-28 11:36:45 -06001284static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res)
1285{
1286 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001287 struct io_kiocb *nxt = NULL;
Jens Axboeba816ad2019-09-28 11:36:45 -06001288
1289 io_complete_rw_common(kiocb, res);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001290 io_put_req_find_next(req, &nxt);
1291
1292 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001293}
1294
Jens Axboedef596e2019-01-09 08:59:42 -07001295static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1296{
1297 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1298
Jens Axboe491381ce2019-10-17 09:20:46 -06001299 if (kiocb->ki_flags & IOCB_WRITE)
1300 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001301
Jens Axboe9e645e112019-05-10 16:07:28 -06001302 if ((req->flags & REQ_F_LINK) && res != req->result)
1303 req->flags |= REQ_F_FAIL_LINK;
1304 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -07001305 if (res != -EAGAIN)
1306 req->flags |= REQ_F_IOPOLL_COMPLETED;
1307}
1308
1309/*
1310 * After the iocb has been issued, it's safe to be found on the poll list.
1311 * Adding the kiocb to the list AFTER submission ensures that we don't
1312 * find it from a io_iopoll_getevents() thread before the issuer is done
1313 * accessing the kiocb cookie.
1314 */
1315static void io_iopoll_req_issued(struct io_kiocb *req)
1316{
1317 struct io_ring_ctx *ctx = req->ctx;
1318
1319 /*
1320 * Track whether we have multiple files in our lists. This will impact
1321 * how we do polling eventually, not spinning if we're on potentially
1322 * different devices.
1323 */
1324 if (list_empty(&ctx->poll_list)) {
1325 ctx->poll_multi_file = false;
1326 } else if (!ctx->poll_multi_file) {
1327 struct io_kiocb *list_req;
1328
1329 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1330 list);
1331 if (list_req->rw.ki_filp != req->rw.ki_filp)
1332 ctx->poll_multi_file = true;
1333 }
1334
1335 /*
1336 * For fast devices, IO may have already completed. If it has, add
1337 * it to the front so we find it first.
1338 */
1339 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1340 list_add(&req->list, &ctx->poll_list);
1341 else
1342 list_add_tail(&req->list, &ctx->poll_list);
1343}
1344
Jens Axboe3d6770f2019-04-13 11:50:54 -06001345static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07001346{
Jens Axboe3d6770f2019-04-13 11:50:54 -06001347 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -07001348 int diff = state->has_refs - state->used_refs;
1349
1350 if (diff)
1351 fput_many(state->file, diff);
1352 state->file = NULL;
1353 }
1354}
1355
1356/*
1357 * Get as many references to a file as we have IOs left in this submission,
1358 * assuming most submissions are for one file, or at least that each file
1359 * has more than one submission.
1360 */
1361static struct file *io_file_get(struct io_submit_state *state, int fd)
1362{
1363 if (!state)
1364 return fget(fd);
1365
1366 if (state->file) {
1367 if (state->fd == fd) {
1368 state->used_refs++;
1369 state->ios_left--;
1370 return state->file;
1371 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06001372 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07001373 }
1374 state->file = fget_many(fd, state->ios_left);
1375 if (!state->file)
1376 return NULL;
1377
1378 state->fd = fd;
1379 state->has_refs = state->ios_left;
1380 state->used_refs = 1;
1381 state->ios_left--;
1382 return state->file;
1383}
1384
Jens Axboe2b188cc2019-01-07 10:46:33 -07001385/*
1386 * If we tracked the file through the SCM inflight mechanism, we could support
1387 * any file. For now, just ensure that anything potentially problematic is done
1388 * inline.
1389 */
1390static bool io_file_supports_async(struct file *file)
1391{
1392 umode_t mode = file_inode(file)->i_mode;
1393
1394 if (S_ISBLK(mode) || S_ISCHR(mode))
1395 return true;
1396 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
1397 return true;
1398
1399 return false;
1400}
1401
Pavel Begunkov267bc902019-11-07 01:41:08 +03001402static int io_prep_rw(struct io_kiocb *req, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001403{
Pavel Begunkov267bc902019-11-07 01:41:08 +03001404 const struct io_uring_sqe *sqe = req->submit.sqe;
Jens Axboedef596e2019-01-09 08:59:42 -07001405 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001406 struct kiocb *kiocb = &req->rw;
Jens Axboe09bb8392019-03-13 12:39:28 -06001407 unsigned ioprio;
1408 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001409
Jens Axboe09bb8392019-03-13 12:39:28 -06001410 if (!req->file)
1411 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001412
Jens Axboe491381ce2019-10-17 09:20:46 -06001413 if (S_ISREG(file_inode(req->file)->i_mode))
1414 req->flags |= REQ_F_ISREG;
1415
1416 /*
1417 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
1418 * we know to async punt it even if it was opened O_NONBLOCK
1419 */
1420 if (force_nonblock && !io_file_supports_async(req->file)) {
1421 req->flags |= REQ_F_MUST_PUNT;
1422 return -EAGAIN;
1423 }
Jens Axboe6b063142019-01-10 22:13:58 -07001424
Jens Axboe2b188cc2019-01-07 10:46:33 -07001425 kiocb->ki_pos = READ_ONCE(sqe->off);
1426 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
1427 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
1428
1429 ioprio = READ_ONCE(sqe->ioprio);
1430 if (ioprio) {
1431 ret = ioprio_check_cap(ioprio);
1432 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06001433 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001434
1435 kiocb->ki_ioprio = ioprio;
1436 } else
1437 kiocb->ki_ioprio = get_current_ioprio();
1438
1439 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
1440 if (unlikely(ret))
Jens Axboe09bb8392019-03-13 12:39:28 -06001441 return ret;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001442
1443 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06001444 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
1445 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02001446 req->flags |= REQ_F_NOWAIT;
1447
1448 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001449 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001450
Jens Axboedef596e2019-01-09 08:59:42 -07001451 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07001452 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
1453 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06001454 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001455
Jens Axboedef596e2019-01-09 08:59:42 -07001456 kiocb->ki_flags |= IOCB_HIPRI;
1457 kiocb->ki_complete = io_complete_rw_iopoll;
Jens Axboe6873e0b2019-10-30 13:53:09 -06001458 req->result = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001459 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001460 if (kiocb->ki_flags & IOCB_HIPRI)
1461 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07001462 kiocb->ki_complete = io_complete_rw;
1463 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001464 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001465}
1466
1467static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
1468{
1469 switch (ret) {
1470 case -EIOCBQUEUED:
1471 break;
1472 case -ERESTARTSYS:
1473 case -ERESTARTNOINTR:
1474 case -ERESTARTNOHAND:
1475 case -ERESTART_RESTARTBLOCK:
1476 /*
1477 * We can't just restart the syscall, since previously
1478 * submitted sqes may already be in progress. Just fail this
1479 * IO with EINTR.
1480 */
1481 ret = -EINTR;
1482 /* fall through */
1483 default:
1484 kiocb->ki_complete(kiocb, ret, 0);
1485 }
1486}
1487
Jens Axboeba816ad2019-09-28 11:36:45 -06001488static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt,
1489 bool in_async)
1490{
1491 if (in_async && ret >= 0 && nxt && kiocb->ki_complete == io_complete_rw)
1492 *nxt = __io_complete_rw(kiocb, ret);
1493 else
1494 io_rw_done(kiocb, ret);
1495}
1496
Jens Axboeedafcce2019-01-09 09:16:05 -07001497static int io_import_fixed(struct io_ring_ctx *ctx, int rw,
1498 const struct io_uring_sqe *sqe,
1499 struct iov_iter *iter)
1500{
1501 size_t len = READ_ONCE(sqe->len);
1502 struct io_mapped_ubuf *imu;
1503 unsigned index, buf_index;
1504 size_t offset;
1505 u64 buf_addr;
1506
1507 /* attempt to use fixed buffers without having provided iovecs */
1508 if (unlikely(!ctx->user_bufs))
1509 return -EFAULT;
1510
1511 buf_index = READ_ONCE(sqe->buf_index);
1512 if (unlikely(buf_index >= ctx->nr_user_bufs))
1513 return -EFAULT;
1514
1515 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
1516 imu = &ctx->user_bufs[index];
1517 buf_addr = READ_ONCE(sqe->addr);
1518
1519 /* overflow */
1520 if (buf_addr + len < buf_addr)
1521 return -EFAULT;
1522 /* not inside the mapped region */
1523 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
1524 return -EFAULT;
1525
1526 /*
1527 * May not be a start of buffer, set size appropriately
1528 * and advance us to the beginning.
1529 */
1530 offset = buf_addr - imu->ubuf;
1531 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06001532
1533 if (offset) {
1534 /*
1535 * Don't use iov_iter_advance() here, as it's really slow for
1536 * using the latter parts of a big fixed buffer - it iterates
1537 * over each segment manually. We can cheat a bit here, because
1538 * we know that:
1539 *
1540 * 1) it's a BVEC iter, we set it up
1541 * 2) all bvecs are PAGE_SIZE in size, except potentially the
1542 * first and last bvec
1543 *
1544 * So just find our index, and adjust the iterator afterwards.
1545 * If the offset is within the first bvec (or the whole first
1546 * bvec, just use iov_iter_advance(). This makes it easier
1547 * since we can just skip the first segment, which may not
1548 * be PAGE_SIZE aligned.
1549 */
1550 const struct bio_vec *bvec = imu->bvec;
1551
1552 if (offset <= bvec->bv_len) {
1553 iov_iter_advance(iter, offset);
1554 } else {
1555 unsigned long seg_skip;
1556
1557 /* skip first vec */
1558 offset -= bvec->bv_len;
1559 seg_skip = 1 + (offset >> PAGE_SHIFT);
1560
1561 iter->bvec = bvec + seg_skip;
1562 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02001563 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001564 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001565 }
1566 }
1567
Jens Axboe5e559562019-11-13 16:12:46 -07001568 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001569}
1570
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001571static ssize_t io_import_iovec(struct io_ring_ctx *ctx, int rw,
1572 const struct sqe_submit *s, struct iovec **iovec,
1573 struct iov_iter *iter)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001574{
1575 const struct io_uring_sqe *sqe = s->sqe;
1576 void __user *buf = u64_to_user_ptr(READ_ONCE(sqe->addr));
1577 size_t sqe_len = READ_ONCE(sqe->len);
Jens Axboeedafcce2019-01-09 09:16:05 -07001578 u8 opcode;
1579
1580 /*
1581 * We're reading ->opcode for the second time, but the first read
1582 * doesn't care whether it's _FIXED or not, so it doesn't matter
1583 * whether ->opcode changes concurrently. The first read does care
1584 * about whether it is a READ or a WRITE, so we don't trust this read
1585 * for that purpose and instead let the caller pass in the read/write
1586 * flag.
1587 */
1588 opcode = READ_ONCE(sqe->opcode);
1589 if (opcode == IORING_OP_READ_FIXED ||
1590 opcode == IORING_OP_WRITE_FIXED) {
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001591 ssize_t ret = io_import_fixed(ctx, rw, sqe, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07001592 *iovec = NULL;
1593 return ret;
1594 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001595
1596 if (!s->has_user)
1597 return -EFAULT;
1598
1599#ifdef CONFIG_COMPAT
1600 if (ctx->compat)
1601 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
1602 iovec, iter);
1603#endif
1604
1605 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
1606}
1607
Jens Axboe32960612019-09-23 11:05:34 -06001608/*
1609 * For files that don't have ->read_iter() and ->write_iter(), handle them
1610 * by looping over ->read() or ->write() manually.
1611 */
1612static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
1613 struct iov_iter *iter)
1614{
1615 ssize_t ret = 0;
1616
1617 /*
1618 * Don't support polled IO through this interface, and we can't
1619 * support non-blocking either. For the latter, this just causes
1620 * the kiocb to be handled from an async context.
1621 */
1622 if (kiocb->ki_flags & IOCB_HIPRI)
1623 return -EOPNOTSUPP;
1624 if (kiocb->ki_flags & IOCB_NOWAIT)
1625 return -EAGAIN;
1626
1627 while (iov_iter_count(iter)) {
1628 struct iovec iovec = iov_iter_iovec(iter);
1629 ssize_t nr;
1630
1631 if (rw == READ) {
1632 nr = file->f_op->read(file, iovec.iov_base,
1633 iovec.iov_len, &kiocb->ki_pos);
1634 } else {
1635 nr = file->f_op->write(file, iovec.iov_base,
1636 iovec.iov_len, &kiocb->ki_pos);
1637 }
1638
1639 if (nr < 0) {
1640 if (!ret)
1641 ret = nr;
1642 break;
1643 }
1644 ret += nr;
1645 if (nr != iovec.iov_len)
1646 break;
1647 iov_iter_advance(iter, nr);
1648 }
1649
1650 return ret;
1651}
1652
Pavel Begunkov267bc902019-11-07 01:41:08 +03001653static int io_read(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001654 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001655{
1656 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1657 struct kiocb *kiocb = &req->rw;
1658 struct iov_iter iter;
1659 struct file *file;
Jens Axboe31b51512019-01-18 22:56:34 -07001660 size_t iov_count;
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001661 ssize_t read_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001662
Pavel Begunkov267bc902019-11-07 01:41:08 +03001663 ret = io_prep_rw(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001664 if (ret)
1665 return ret;
1666 file = kiocb->ki_filp;
1667
Jens Axboe2b188cc2019-01-07 10:46:33 -07001668 if (unlikely(!(file->f_mode & FMODE_READ)))
Jens Axboe09bb8392019-03-13 12:39:28 -06001669 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001670
Pavel Begunkov267bc902019-11-07 01:41:08 +03001671 ret = io_import_iovec(req->ctx, READ, &req->submit, &iovec, &iter);
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001672 if (ret < 0)
Jens Axboe09bb8392019-03-13 12:39:28 -06001673 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001674
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001675 read_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06001676 if (req->flags & REQ_F_LINK)
1677 req->result = read_size;
1678
Jens Axboe31b51512019-01-18 22:56:34 -07001679 iov_count = iov_iter_count(&iter);
1680 ret = rw_verify_area(READ, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001681 if (!ret) {
1682 ssize_t ret2;
1683
Jens Axboe32960612019-09-23 11:05:34 -06001684 if (file->f_op->read_iter)
1685 ret2 = call_read_iter(file, kiocb, &iter);
1686 else
1687 ret2 = loop_rw_iter(READ, file, kiocb, &iter);
1688
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001689 /*
1690 * In case of a short read, punt to async. This can happen
1691 * if we have data partially cached. Alternatively we can
1692 * return the short read, in which case the application will
1693 * need to issue another SQE and wait for it. That SQE will
1694 * need async punt anyway, so it's more efficient to do it
1695 * here.
1696 */
Jens Axboe491381ce2019-10-17 09:20:46 -06001697 if (force_nonblock && !(req->flags & REQ_F_NOWAIT) &&
1698 (req->flags & REQ_F_ISREG) &&
1699 ret2 > 0 && ret2 < read_size)
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001700 ret2 = -EAGAIN;
1701 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboe561fb042019-10-24 07:25:42 -06001702 if (!force_nonblock || ret2 != -EAGAIN)
Pavel Begunkov267bc902019-11-07 01:41:08 +03001703 kiocb_done(kiocb, ret2, nxt, req->submit.in_async);
Jens Axboe561fb042019-10-24 07:25:42 -06001704 else
Jens Axboe2b188cc2019-01-07 10:46:33 -07001705 ret = -EAGAIN;
1706 }
1707 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001708 return ret;
1709}
1710
Pavel Begunkov267bc902019-11-07 01:41:08 +03001711static int io_write(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001712 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001713{
1714 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1715 struct kiocb *kiocb = &req->rw;
1716 struct iov_iter iter;
1717 struct file *file;
Jens Axboe31b51512019-01-18 22:56:34 -07001718 size_t iov_count;
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001719 ssize_t ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001720
Pavel Begunkov267bc902019-11-07 01:41:08 +03001721 ret = io_prep_rw(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001722 if (ret)
1723 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001724
Jens Axboe2b188cc2019-01-07 10:46:33 -07001725 file = kiocb->ki_filp;
1726 if (unlikely(!(file->f_mode & FMODE_WRITE)))
Jens Axboe09bb8392019-03-13 12:39:28 -06001727 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001728
Pavel Begunkov267bc902019-11-07 01:41:08 +03001729 ret = io_import_iovec(req->ctx, WRITE, &req->submit, &iovec, &iter);
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001730 if (ret < 0)
Jens Axboe09bb8392019-03-13 12:39:28 -06001731 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001732
Jens Axboe9e645e112019-05-10 16:07:28 -06001733 if (req->flags & REQ_F_LINK)
1734 req->result = ret;
1735
Jens Axboe31b51512019-01-18 22:56:34 -07001736 iov_count = iov_iter_count(&iter);
1737
1738 ret = -EAGAIN;
Jens Axboe561fb042019-10-24 07:25:42 -06001739 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT))
Jens Axboe31b51512019-01-18 22:56:34 -07001740 goto out_free;
Jens Axboe31b51512019-01-18 22:56:34 -07001741
1742 ret = rw_verify_area(WRITE, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001743 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01001744 ssize_t ret2;
1745
Jens Axboe2b188cc2019-01-07 10:46:33 -07001746 /*
1747 * Open-code file_start_write here to grab freeze protection,
1748 * which will be released by another thread in
1749 * io_complete_rw(). Fool lockdep by telling it the lock got
1750 * released so that it doesn't complain about the held lock when
1751 * we return to userspace.
1752 */
Jens Axboe491381ce2019-10-17 09:20:46 -06001753 if (req->flags & REQ_F_ISREG) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001754 __sb_start_write(file_inode(file)->i_sb,
1755 SB_FREEZE_WRITE, true);
1756 __sb_writers_release(file_inode(file)->i_sb,
1757 SB_FREEZE_WRITE);
1758 }
1759 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01001760
Jens Axboe32960612019-09-23 11:05:34 -06001761 if (file->f_op->write_iter)
1762 ret2 = call_write_iter(file, kiocb, &iter);
1763 else
1764 ret2 = loop_rw_iter(WRITE, file, kiocb, &iter);
Jens Axboe561fb042019-10-24 07:25:42 -06001765 if (!force_nonblock || ret2 != -EAGAIN)
Pavel Begunkov267bc902019-11-07 01:41:08 +03001766 kiocb_done(kiocb, ret2, nxt, req->submit.in_async);
Jens Axboe561fb042019-10-24 07:25:42 -06001767 else
Roman Penyaev9bf79332019-03-25 20:09:24 +01001768 ret = -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001769 }
Jens Axboe31b51512019-01-18 22:56:34 -07001770out_free:
Jens Axboe2b188cc2019-01-07 10:46:33 -07001771 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001772 return ret;
1773}
1774
1775/*
1776 * IORING_OP_NOP just posts a completion event, nothing else.
1777 */
Jens Axboe78e19bb2019-11-06 15:21:34 -07001778static int io_nop(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001779{
1780 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001781
Jens Axboedef596e2019-01-09 08:59:42 -07001782 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1783 return -EINVAL;
1784
Jens Axboe78e19bb2019-11-06 15:21:34 -07001785 io_cqring_add_event(req, 0);
Jens Axboee65ef562019-03-12 10:16:44 -06001786 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001787 return 0;
1788}
1789
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001790static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1791{
Jens Axboe6b063142019-01-10 22:13:58 -07001792 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001793
Jens Axboe09bb8392019-03-13 12:39:28 -06001794 if (!req->file)
1795 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001796
Jens Axboe6b063142019-01-10 22:13:58 -07001797 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07001798 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07001799 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001800 return -EINVAL;
1801
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001802 return 0;
1803}
1804
1805static int io_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001806 struct io_kiocb **nxt, bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001807{
1808 loff_t sqe_off = READ_ONCE(sqe->off);
1809 loff_t sqe_len = READ_ONCE(sqe->len);
1810 loff_t end = sqe_off + sqe_len;
1811 unsigned fsync_flags;
1812 int ret;
1813
1814 fsync_flags = READ_ONCE(sqe->fsync_flags);
1815 if (unlikely(fsync_flags & ~IORING_FSYNC_DATASYNC))
1816 return -EINVAL;
1817
1818 ret = io_prep_fsync(req, sqe);
1819 if (ret)
1820 return ret;
1821
1822 /* fsync always requires a blocking context */
1823 if (force_nonblock)
1824 return -EAGAIN;
1825
1826 ret = vfs_fsync_range(req->rw.ki_filp, sqe_off,
1827 end > 0 ? end : LLONG_MAX,
1828 fsync_flags & IORING_FSYNC_DATASYNC);
1829
Jens Axboe9e645e112019-05-10 16:07:28 -06001830 if (ret < 0 && (req->flags & REQ_F_LINK))
1831 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001832 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001833 io_put_req_find_next(req, nxt);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001834 return 0;
1835}
1836
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001837static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1838{
1839 struct io_ring_ctx *ctx = req->ctx;
1840 int ret = 0;
1841
1842 if (!req->file)
1843 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001844
1845 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1846 return -EINVAL;
1847 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
1848 return -EINVAL;
1849
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001850 return ret;
1851}
1852
1853static int io_sync_file_range(struct io_kiocb *req,
1854 const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001855 struct io_kiocb **nxt,
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001856 bool force_nonblock)
1857{
1858 loff_t sqe_off;
1859 loff_t sqe_len;
1860 unsigned flags;
1861 int ret;
1862
1863 ret = io_prep_sfr(req, sqe);
1864 if (ret)
1865 return ret;
1866
1867 /* sync_file_range always requires a blocking context */
1868 if (force_nonblock)
1869 return -EAGAIN;
1870
1871 sqe_off = READ_ONCE(sqe->off);
1872 sqe_len = READ_ONCE(sqe->len);
1873 flags = READ_ONCE(sqe->sync_range_flags);
1874
1875 ret = sync_file_range(req->rw.ki_filp, sqe_off, sqe_len, flags);
1876
Jens Axboe9e645e112019-05-10 16:07:28 -06001877 if (ret < 0 && (req->flags & REQ_F_LINK))
1878 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001879 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001880 io_put_req_find_next(req, nxt);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001881 return 0;
1882}
1883
Jens Axboe0fa03c62019-04-19 13:34:07 -06001884#if defined(CONFIG_NET)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001885static int io_send_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001886 struct io_kiocb **nxt, bool force_nonblock,
Jens Axboeaa1fa282019-04-19 13:38:09 -06001887 long (*fn)(struct socket *, struct user_msghdr __user *,
1888 unsigned int))
1889{
Jens Axboe0fa03c62019-04-19 13:34:07 -06001890 struct socket *sock;
1891 int ret;
1892
1893 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1894 return -EINVAL;
1895
1896 sock = sock_from_file(req->file, &ret);
1897 if (sock) {
1898 struct user_msghdr __user *msg;
1899 unsigned flags;
1900
1901 flags = READ_ONCE(sqe->msg_flags);
1902 if (flags & MSG_DONTWAIT)
1903 req->flags |= REQ_F_NOWAIT;
1904 else if (force_nonblock)
1905 flags |= MSG_DONTWAIT;
1906
1907 msg = (struct user_msghdr __user *) (unsigned long)
1908 READ_ONCE(sqe->addr);
1909
Jens Axboeaa1fa282019-04-19 13:38:09 -06001910 ret = fn(sock, msg, flags);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001911 if (force_nonblock && ret == -EAGAIN)
1912 return ret;
1913 }
1914
Jens Axboe78e19bb2019-11-06 15:21:34 -07001915 io_cqring_add_event(req, ret);
Jens Axboef1f40852019-11-05 20:33:16 -07001916 if (ret < 0 && (req->flags & REQ_F_LINK))
1917 req->flags |= REQ_F_FAIL_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001918 io_put_req_find_next(req, nxt);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001919 return 0;
Jens Axboeaa1fa282019-04-19 13:38:09 -06001920}
1921#endif
1922
1923static int io_sendmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001924 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001925{
1926#if defined(CONFIG_NET)
Jens Axboeba816ad2019-09-28 11:36:45 -06001927 return io_send_recvmsg(req, sqe, nxt, force_nonblock,
1928 __sys_sendmsg_sock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06001929#else
1930 return -EOPNOTSUPP;
1931#endif
1932}
1933
1934static int io_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001935 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001936{
1937#if defined(CONFIG_NET)
Jens Axboeba816ad2019-09-28 11:36:45 -06001938 return io_send_recvmsg(req, sqe, nxt, force_nonblock,
1939 __sys_recvmsg_sock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001940#else
1941 return -EOPNOTSUPP;
1942#endif
1943}
1944
Jens Axboe17f2fe32019-10-17 14:42:58 -06001945static int io_accept(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1946 struct io_kiocb **nxt, bool force_nonblock)
1947{
1948#if defined(CONFIG_NET)
1949 struct sockaddr __user *addr;
1950 int __user *addr_len;
1951 unsigned file_flags;
1952 int flags, ret;
1953
1954 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
1955 return -EINVAL;
1956 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
1957 return -EINVAL;
1958
1959 addr = (struct sockaddr __user *) (unsigned long) READ_ONCE(sqe->addr);
1960 addr_len = (int __user *) (unsigned long) READ_ONCE(sqe->addr2);
1961 flags = READ_ONCE(sqe->accept_flags);
1962 file_flags = force_nonblock ? O_NONBLOCK : 0;
1963
1964 ret = __sys_accept4_file(req->file, file_flags, addr, addr_len, flags);
1965 if (ret == -EAGAIN && force_nonblock) {
1966 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
1967 return -EAGAIN;
1968 }
Jens Axboe8e3cca12019-11-09 19:52:33 -07001969 if (ret == -ERESTARTSYS)
1970 ret = -EINTR;
Jens Axboe17f2fe32019-10-17 14:42:58 -06001971 if (ret < 0 && (req->flags & REQ_F_LINK))
1972 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001973 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001974 io_put_req_find_next(req, nxt);
Jens Axboe17f2fe32019-10-17 14:42:58 -06001975 return 0;
1976#else
1977 return -EOPNOTSUPP;
1978#endif
1979}
1980
Jens Axboeeac406c2019-11-14 12:09:58 -07001981static inline void io_poll_remove_req(struct io_kiocb *req)
1982{
1983 if (!RB_EMPTY_NODE(&req->rb_node)) {
1984 rb_erase(&req->rb_node, &req->ctx->cancel_tree);
1985 RB_CLEAR_NODE(&req->rb_node);
1986 }
1987}
1988
Jens Axboe221c5eb2019-01-17 09:41:58 -07001989static void io_poll_remove_one(struct io_kiocb *req)
1990{
1991 struct io_poll_iocb *poll = &req->poll;
1992
1993 spin_lock(&poll->head->lock);
1994 WRITE_ONCE(poll->canceled, true);
1995 if (!list_empty(&poll->wait.entry)) {
1996 list_del_init(&poll->wait.entry);
Jackie Liua197f662019-11-08 08:09:12 -07001997 io_queue_async_work(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001998 }
1999 spin_unlock(&poll->head->lock);
Jens Axboeeac406c2019-11-14 12:09:58 -07002000 io_poll_remove_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002001}
2002
2003static void io_poll_remove_all(struct io_ring_ctx *ctx)
2004{
Jens Axboeeac406c2019-11-14 12:09:58 -07002005 struct rb_node *node;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002006 struct io_kiocb *req;
2007
2008 spin_lock_irq(&ctx->completion_lock);
Jens Axboeeac406c2019-11-14 12:09:58 -07002009 while ((node = rb_first(&ctx->cancel_tree)) != NULL) {
2010 req = rb_entry(node, struct io_kiocb, rb_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002011 io_poll_remove_one(req);
2012 }
2013 spin_unlock_irq(&ctx->completion_lock);
2014}
2015
Jens Axboe47f46762019-11-09 17:43:02 -07002016static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
2017{
Jens Axboeeac406c2019-11-14 12:09:58 -07002018 struct rb_node *p, *parent = NULL;
Jens Axboe47f46762019-11-09 17:43:02 -07002019 struct io_kiocb *req;
2020
Jens Axboeeac406c2019-11-14 12:09:58 -07002021 p = ctx->cancel_tree.rb_node;
2022 while (p) {
2023 parent = p;
2024 req = rb_entry(parent, struct io_kiocb, rb_node);
2025 if (sqe_addr < req->user_data) {
2026 p = p->rb_left;
2027 } else if (sqe_addr > req->user_data) {
2028 p = p->rb_right;
2029 } else {
2030 io_poll_remove_one(req);
2031 return 0;
2032 }
Jens Axboe47f46762019-11-09 17:43:02 -07002033 }
2034
2035 return -ENOENT;
2036}
2037
Jens Axboe221c5eb2019-01-17 09:41:58 -07002038/*
2039 * Find a running poll command that matches one specified in sqe->addr,
2040 * and remove it if found.
2041 */
2042static int io_poll_remove(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2043{
2044 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07002045 int ret;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002046
2047 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2048 return -EINVAL;
2049 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
2050 sqe->poll_events)
2051 return -EINVAL;
2052
2053 spin_lock_irq(&ctx->completion_lock);
Jens Axboe47f46762019-11-09 17:43:02 -07002054 ret = io_poll_cancel(ctx, READ_ONCE(sqe->addr));
Jens Axboe221c5eb2019-01-17 09:41:58 -07002055 spin_unlock_irq(&ctx->completion_lock);
2056
Jens Axboe78e19bb2019-11-06 15:21:34 -07002057 io_cqring_add_event(req, ret);
Jens Axboef1f40852019-11-05 20:33:16 -07002058 if (ret < 0 && (req->flags & REQ_F_LINK))
2059 req->flags |= REQ_F_FAIL_LINK;
Jens Axboee65ef562019-03-12 10:16:44 -06002060 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002061 return 0;
2062}
2063
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002064static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002065{
Jackie Liua197f662019-11-08 08:09:12 -07002066 struct io_ring_ctx *ctx = req->ctx;
2067
Jens Axboe8c838782019-03-12 15:48:16 -06002068 req->poll.done = true;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002069 if (error)
2070 io_cqring_fill_event(req, error);
2071 else
2072 io_cqring_fill_event(req, mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06002073 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002074}
2075
Jens Axboe561fb042019-10-24 07:25:42 -06002076static void io_poll_complete_work(struct io_wq_work **workptr)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002077{
Jens Axboe561fb042019-10-24 07:25:42 -06002078 struct io_wq_work *work = *workptr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002079 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
2080 struct io_poll_iocb *poll = &req->poll;
2081 struct poll_table_struct pt = { ._key = poll->events };
2082 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe89723d02019-11-05 15:32:58 -07002083 struct io_kiocb *nxt = NULL;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002084 __poll_t mask = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002085 int ret = 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002086
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002087 if (work->flags & IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06002088 WRITE_ONCE(poll->canceled, true);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002089 ret = -ECANCELED;
2090 } else if (READ_ONCE(poll->canceled)) {
2091 ret = -ECANCELED;
2092 }
Jens Axboe561fb042019-10-24 07:25:42 -06002093
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002094 if (ret != -ECANCELED)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002095 mask = vfs_poll(poll->file, &pt) & poll->events;
2096
2097 /*
2098 * Note that ->ki_cancel callers also delete iocb from active_reqs after
2099 * calling ->ki_cancel. We need the ctx_lock roundtrip here to
2100 * synchronize with them. In the cancellation case the list_del_init
2101 * itself is not actually needed, but harmless so we keep it in to
2102 * avoid further branches in the fast path.
2103 */
2104 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002105 if (!mask && ret != -ECANCELED) {
Jens Axboe221c5eb2019-01-17 09:41:58 -07002106 add_wait_queue(poll->head, &poll->wait);
2107 spin_unlock_irq(&ctx->completion_lock);
2108 return;
2109 }
Jens Axboeeac406c2019-11-14 12:09:58 -07002110 io_poll_remove_req(req);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002111 io_poll_complete(req, mask, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002112 spin_unlock_irq(&ctx->completion_lock);
2113
Jens Axboe8c838782019-03-12 15:48:16 -06002114 io_cqring_ev_posted(ctx);
Jens Axboe89723d02019-11-05 15:32:58 -07002115
Jens Axboefba38c22019-11-18 12:27:57 -07002116 if (ret < 0 && req->flags & REQ_F_LINK)
2117 req->flags |= REQ_F_FAIL_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08002118 io_put_req_find_next(req, &nxt);
Jens Axboe89723d02019-11-05 15:32:58 -07002119 if (nxt)
2120 *workptr = &nxt->work;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002121}
2122
2123static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
2124 void *key)
2125{
2126 struct io_poll_iocb *poll = container_of(wait, struct io_poll_iocb,
2127 wait);
2128 struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
2129 struct io_ring_ctx *ctx = req->ctx;
2130 __poll_t mask = key_to_poll(key);
Jens Axboe8c838782019-03-12 15:48:16 -06002131 unsigned long flags;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002132
2133 /* for instances that support it check for an event match first: */
Jens Axboe8c838782019-03-12 15:48:16 -06002134 if (mask && !(mask & poll->events))
2135 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002136
2137 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06002138
Jens Axboe7c9e7f02019-11-12 08:15:53 -07002139 /*
2140 * Run completion inline if we can. We're using trylock here because
2141 * we are violating the completion_lock -> poll wq lock ordering.
2142 * If we have a link timeout we're going to need the completion_lock
2143 * for finalizing the request, mark us as having grabbed that already.
2144 */
Jens Axboe8c838782019-03-12 15:48:16 -06002145 if (mask && spin_trylock_irqsave(&ctx->completion_lock, flags)) {
Jens Axboeeac406c2019-11-14 12:09:58 -07002146 io_poll_remove_req(req);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002147 io_poll_complete(req, mask, 0);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07002148 req->flags |= REQ_F_COMP_LOCKED;
2149 io_put_req(req);
Jens Axboe8c838782019-03-12 15:48:16 -06002150 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2151
2152 io_cqring_ev_posted(ctx);
Jens Axboe8c838782019-03-12 15:48:16 -06002153 } else {
Jackie Liua197f662019-11-08 08:09:12 -07002154 io_queue_async_work(req);
Jens Axboe8c838782019-03-12 15:48:16 -06002155 }
2156
Jens Axboe221c5eb2019-01-17 09:41:58 -07002157 return 1;
2158}
2159
2160struct io_poll_table {
2161 struct poll_table_struct pt;
2162 struct io_kiocb *req;
2163 int error;
2164};
2165
2166static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
2167 struct poll_table_struct *p)
2168{
2169 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
2170
2171 if (unlikely(pt->req->poll.head)) {
2172 pt->error = -EINVAL;
2173 return;
2174 }
2175
2176 pt->error = 0;
2177 pt->req->poll.head = head;
2178 add_wait_queue(head, &pt->req->poll.wait);
2179}
2180
Jens Axboeeac406c2019-11-14 12:09:58 -07002181static void io_poll_req_insert(struct io_kiocb *req)
2182{
2183 struct io_ring_ctx *ctx = req->ctx;
2184 struct rb_node **p = &ctx->cancel_tree.rb_node;
2185 struct rb_node *parent = NULL;
2186 struct io_kiocb *tmp;
2187
2188 while (*p) {
2189 parent = *p;
2190 tmp = rb_entry(parent, struct io_kiocb, rb_node);
2191 if (req->user_data < tmp->user_data)
2192 p = &(*p)->rb_left;
2193 else
2194 p = &(*p)->rb_right;
2195 }
2196 rb_link_node(&req->rb_node, parent, p);
2197 rb_insert_color(&req->rb_node, &ctx->cancel_tree);
2198}
2199
Jens Axboe89723d02019-11-05 15:32:58 -07002200static int io_poll_add(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2201 struct io_kiocb **nxt)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002202{
2203 struct io_poll_iocb *poll = &req->poll;
2204 struct io_ring_ctx *ctx = req->ctx;
2205 struct io_poll_table ipt;
Jens Axboe8c838782019-03-12 15:48:16 -06002206 bool cancel = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002207 __poll_t mask;
2208 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002209
2210 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2211 return -EINVAL;
2212 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
2213 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06002214 if (!poll->file)
2215 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002216
Jens Axboe6cc47d12019-09-18 11:18:23 -06002217 req->submit.sqe = NULL;
Jens Axboe561fb042019-10-24 07:25:42 -06002218 INIT_IO_WORK(&req->work, io_poll_complete_work);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002219 events = READ_ONCE(sqe->poll_events);
2220 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
Jens Axboeeac406c2019-11-14 12:09:58 -07002221 RB_CLEAR_NODE(&req->rb_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002222
Jens Axboe221c5eb2019-01-17 09:41:58 -07002223 poll->head = NULL;
Jens Axboe8c838782019-03-12 15:48:16 -06002224 poll->done = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002225 poll->canceled = false;
2226
2227 ipt.pt._qproc = io_poll_queue_proc;
2228 ipt.pt._key = poll->events;
2229 ipt.req = req;
2230 ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
2231
2232 /* initialized the list so that we can do list_empty checks */
2233 INIT_LIST_HEAD(&poll->wait.entry);
2234 init_waitqueue_func_entry(&poll->wait, io_poll_wake);
2235
Jens Axboe36703242019-07-25 10:20:18 -06002236 INIT_LIST_HEAD(&req->list);
2237
Jens Axboe221c5eb2019-01-17 09:41:58 -07002238 mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002239
2240 spin_lock_irq(&ctx->completion_lock);
Jens Axboe8c838782019-03-12 15:48:16 -06002241 if (likely(poll->head)) {
2242 spin_lock(&poll->head->lock);
2243 if (unlikely(list_empty(&poll->wait.entry))) {
2244 if (ipt.error)
2245 cancel = true;
2246 ipt.error = 0;
2247 mask = 0;
2248 }
2249 if (mask || ipt.error)
2250 list_del_init(&poll->wait.entry);
2251 else if (cancel)
2252 WRITE_ONCE(poll->canceled, true);
2253 else if (!poll->done) /* actually waiting for an event */
Jens Axboeeac406c2019-11-14 12:09:58 -07002254 io_poll_req_insert(req);
Jens Axboe8c838782019-03-12 15:48:16 -06002255 spin_unlock(&poll->head->lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002256 }
Jens Axboe8c838782019-03-12 15:48:16 -06002257 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06002258 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002259 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06002260 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07002261 spin_unlock_irq(&ctx->completion_lock);
2262
Jens Axboe8c838782019-03-12 15:48:16 -06002263 if (mask) {
2264 io_cqring_ev_posted(ctx);
Jackie Liuec9c02a2019-11-08 23:50:36 +08002265 io_put_req_find_next(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002266 }
Jens Axboe8c838782019-03-12 15:48:16 -06002267 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002268}
2269
Jens Axboe5262f562019-09-17 12:26:57 -06002270static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
2271{
Jens Axboead8a48a2019-11-15 08:49:11 -07002272 struct io_timeout_data *data = container_of(timer,
2273 struct io_timeout_data, timer);
2274 struct io_kiocb *req = data->req;
2275 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06002276 unsigned long flags;
2277
Jens Axboe5262f562019-09-17 12:26:57 -06002278 atomic_inc(&ctx->cq_timeouts);
2279
2280 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08002281 /*
Jens Axboe11365042019-10-16 09:08:32 -06002282 * We could be racing with timeout deletion. If the list is empty,
2283 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08002284 */
Jens Axboe842f9612019-10-29 12:34:10 -06002285 if (!list_empty(&req->list)) {
Jens Axboe11365042019-10-16 09:08:32 -06002286 struct io_kiocb *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06002287
Jens Axboe11365042019-10-16 09:08:32 -06002288 /*
2289 * Adjust the reqs sequence before the current one because it
2290 * will consume a slot in the cq_ring and the the cq_tail
2291 * pointer will be increased, otherwise other timeout reqs may
2292 * return in advance without waiting for enough wait_nr.
2293 */
2294 prev = req;
2295 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
2296 prev->sequence++;
Jens Axboe11365042019-10-16 09:08:32 -06002297 list_del_init(&req->list);
Jens Axboe11365042019-10-16 09:08:32 -06002298 }
Jens Axboe842f9612019-10-29 12:34:10 -06002299
Jens Axboe78e19bb2019-11-06 15:21:34 -07002300 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06002301 io_commit_cqring(ctx);
2302 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2303
2304 io_cqring_ev_posted(ctx);
Jens Axboef1f40852019-11-05 20:33:16 -07002305 if (req->flags & REQ_F_LINK)
2306 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe5262f562019-09-17 12:26:57 -06002307 io_put_req(req);
2308 return HRTIMER_NORESTART;
2309}
2310
Jens Axboe47f46762019-11-09 17:43:02 -07002311static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
2312{
2313 struct io_kiocb *req;
2314 int ret = -ENOENT;
2315
2316 list_for_each_entry(req, &ctx->timeout_list, list) {
2317 if (user_data == req->user_data) {
2318 list_del_init(&req->list);
2319 ret = 0;
2320 break;
2321 }
2322 }
2323
2324 if (ret == -ENOENT)
2325 return ret;
2326
Jens Axboead8a48a2019-11-15 08:49:11 -07002327 ret = hrtimer_try_to_cancel(&req->timeout.data->timer);
Jens Axboe47f46762019-11-09 17:43:02 -07002328 if (ret == -1)
2329 return -EALREADY;
2330
Jens Axboefba38c22019-11-18 12:27:57 -07002331 if (req->flags & REQ_F_LINK)
2332 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe47f46762019-11-09 17:43:02 -07002333 io_cqring_fill_event(req, -ECANCELED);
2334 io_put_req(req);
2335 return 0;
2336}
2337
Jens Axboe11365042019-10-16 09:08:32 -06002338/*
2339 * Remove or update an existing timeout command
2340 */
2341static int io_timeout_remove(struct io_kiocb *req,
2342 const struct io_uring_sqe *sqe)
2343{
2344 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe11365042019-10-16 09:08:32 -06002345 unsigned flags;
Jens Axboe47f46762019-11-09 17:43:02 -07002346 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06002347
2348 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2349 return -EINVAL;
2350 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
2351 return -EINVAL;
2352 flags = READ_ONCE(sqe->timeout_flags);
2353 if (flags)
2354 return -EINVAL;
2355
Jens Axboe11365042019-10-16 09:08:32 -06002356 spin_lock_irq(&ctx->completion_lock);
Jens Axboe47f46762019-11-09 17:43:02 -07002357 ret = io_timeout_cancel(ctx, READ_ONCE(sqe->addr));
Jens Axboe11365042019-10-16 09:08:32 -06002358
Jens Axboe47f46762019-11-09 17:43:02 -07002359 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06002360 io_commit_cqring(ctx);
2361 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06002362 io_cqring_ev_posted(ctx);
Jens Axboe47f46762019-11-09 17:43:02 -07002363 if (ret < 0 && req->flags & REQ_F_LINK)
2364 req->flags |= REQ_F_FAIL_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08002365 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06002366 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06002367}
2368
Jens Axboead8a48a2019-11-15 08:49:11 -07002369static int io_timeout_setup(struct io_kiocb *req)
Jens Axboe5262f562019-09-17 12:26:57 -06002370{
Jens Axboead8a48a2019-11-15 08:49:11 -07002371 const struct io_uring_sqe *sqe = req->submit.sqe;
2372 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06002373 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06002374
Jens Axboead8a48a2019-11-15 08:49:11 -07002375 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06002376 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07002377 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06002378 return -EINVAL;
2379 flags = READ_ONCE(sqe->timeout_flags);
2380 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06002381 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06002382
Jens Axboead8a48a2019-11-15 08:49:11 -07002383 data = kzalloc(sizeof(struct io_timeout_data), GFP_KERNEL);
2384 if (!data)
2385 return -ENOMEM;
2386 data->req = req;
2387 req->timeout.data = data;
2388 req->flags |= REQ_F_TIMEOUT;
2389
2390 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06002391 return -EFAULT;
2392
Jens Axboe11365042019-10-16 09:08:32 -06002393 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07002394 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06002395 else
Jens Axboead8a48a2019-11-15 08:49:11 -07002396 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06002397
Jens Axboead8a48a2019-11-15 08:49:11 -07002398 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
2399 return 0;
2400}
2401
2402static int io_timeout(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2403{
2404 unsigned count;
2405 struct io_ring_ctx *ctx = req->ctx;
2406 struct io_timeout_data *data;
2407 struct list_head *entry;
2408 unsigned span = 0;
2409 int ret;
2410
2411 ret = io_timeout_setup(req);
2412 /* common setup allows flags (like links) set, we don't */
2413 if (!ret && sqe->flags)
2414 ret = -EINVAL;
2415 if (ret)
2416 return ret;
Jens Axboe93bd25b2019-11-11 23:34:31 -07002417
Jens Axboe5262f562019-09-17 12:26:57 -06002418 /*
2419 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07002420 * timeout event to be satisfied. If it isn't set, then this is
2421 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06002422 */
2423 count = READ_ONCE(sqe->off);
Jens Axboe93bd25b2019-11-11 23:34:31 -07002424 if (!count) {
2425 req->flags |= REQ_F_TIMEOUT_NOSEQ;
2426 spin_lock_irq(&ctx->completion_lock);
2427 entry = ctx->timeout_list.prev;
2428 goto add;
2429 }
Jens Axboe5262f562019-09-17 12:26:57 -06002430
2431 req->sequence = ctx->cached_sq_head + count - 1;
yangerkun5da0fb12019-10-15 21:59:29 +08002432 /* reuse it to store the count */
2433 req->submit.sequence = count;
Jens Axboe5262f562019-09-17 12:26:57 -06002434
2435 /*
2436 * Insertion sort, ensuring the first entry in the list is always
2437 * the one we need first.
2438 */
Jens Axboe5262f562019-09-17 12:26:57 -06002439 spin_lock_irq(&ctx->completion_lock);
2440 list_for_each_prev(entry, &ctx->timeout_list) {
2441 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
yangerkun5da0fb12019-10-15 21:59:29 +08002442 unsigned nxt_sq_head;
2443 long long tmp, tmp_nxt;
Jens Axboe5262f562019-09-17 12:26:57 -06002444
Jens Axboe93bd25b2019-11-11 23:34:31 -07002445 if (nxt->flags & REQ_F_TIMEOUT_NOSEQ)
2446 continue;
2447
yangerkun5da0fb12019-10-15 21:59:29 +08002448 /*
2449 * Since cached_sq_head + count - 1 can overflow, use type long
2450 * long to store it.
2451 */
2452 tmp = (long long)ctx->cached_sq_head + count - 1;
2453 nxt_sq_head = nxt->sequence - nxt->submit.sequence + 1;
2454 tmp_nxt = (long long)nxt_sq_head + nxt->submit.sequence - 1;
2455
2456 /*
2457 * cached_sq_head may overflow, and it will never overflow twice
2458 * once there is some timeout req still be valid.
2459 */
2460 if (ctx->cached_sq_head < nxt_sq_head)
yangerkun8b07a652019-10-17 12:12:35 +08002461 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08002462
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002463 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06002464 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002465
2466 /*
2467 * Sequence of reqs after the insert one and itself should
2468 * be adjusted because each timeout req consumes a slot.
2469 */
2470 span++;
2471 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06002472 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002473 req->sequence -= span;
Jens Axboe93bd25b2019-11-11 23:34:31 -07002474add:
Jens Axboe5262f562019-09-17 12:26:57 -06002475 list_add(&req->list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07002476 data = req->timeout.data;
2477 data->timer.function = io_timeout_fn;
2478 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06002479 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06002480 return 0;
2481}
2482
Jens Axboe62755e32019-10-28 21:49:21 -06002483static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06002484{
Jens Axboe62755e32019-10-28 21:49:21 -06002485 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06002486
Jens Axboe62755e32019-10-28 21:49:21 -06002487 return req->user_data == (unsigned long) data;
2488}
2489
Jens Axboee977d6d2019-11-05 12:39:45 -07002490static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06002491{
Jens Axboe62755e32019-10-28 21:49:21 -06002492 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06002493 int ret = 0;
2494
Jens Axboe62755e32019-10-28 21:49:21 -06002495 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
2496 switch (cancel_ret) {
2497 case IO_WQ_CANCEL_OK:
2498 ret = 0;
2499 break;
2500 case IO_WQ_CANCEL_RUNNING:
2501 ret = -EALREADY;
2502 break;
2503 case IO_WQ_CANCEL_NOTFOUND:
2504 ret = -ENOENT;
2505 break;
2506 }
2507
Jens Axboee977d6d2019-11-05 12:39:45 -07002508 return ret;
2509}
2510
Jens Axboe47f46762019-11-09 17:43:02 -07002511static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
2512 struct io_kiocb *req, __u64 sqe_addr,
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002513 struct io_kiocb **nxt, int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07002514{
2515 unsigned long flags;
2516 int ret;
2517
2518 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
2519 if (ret != -ENOENT) {
2520 spin_lock_irqsave(&ctx->completion_lock, flags);
2521 goto done;
2522 }
2523
2524 spin_lock_irqsave(&ctx->completion_lock, flags);
2525 ret = io_timeout_cancel(ctx, sqe_addr);
2526 if (ret != -ENOENT)
2527 goto done;
2528 ret = io_poll_cancel(ctx, sqe_addr);
2529done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002530 if (!ret)
2531 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07002532 io_cqring_fill_event(req, ret);
2533 io_commit_cqring(ctx);
2534 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2535 io_cqring_ev_posted(ctx);
2536
2537 if (ret < 0 && (req->flags & REQ_F_LINK))
2538 req->flags |= REQ_F_FAIL_LINK;
2539 io_put_req_find_next(req, nxt);
2540}
2541
Jens Axboee977d6d2019-11-05 12:39:45 -07002542static int io_async_cancel(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2543 struct io_kiocb **nxt)
2544{
2545 struct io_ring_ctx *ctx = req->ctx;
Jens Axboee977d6d2019-11-05 12:39:45 -07002546
2547 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2548 return -EINVAL;
2549 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
2550 sqe->cancel_flags)
2551 return -EINVAL;
2552
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002553 io_async_find_and_cancel(ctx, req, READ_ONCE(sqe->addr), nxt, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06002554 return 0;
2555}
2556
Jackie Liua197f662019-11-08 08:09:12 -07002557static int io_req_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -06002558{
Pavel Begunkov267bc902019-11-07 01:41:08 +03002559 const struct io_uring_sqe *sqe = req->submit.sqe;
Jens Axboede0617e2019-04-06 21:51:27 -06002560 struct io_uring_sqe *sqe_copy;
Jackie Liua197f662019-11-08 08:09:12 -07002561 struct io_ring_ctx *ctx = req->ctx;
Jens Axboede0617e2019-04-06 21:51:27 -06002562
Bob Liu9d858b22019-11-13 18:06:25 +08002563 /* Still need defer if there is pending req in defer list. */
2564 if (!req_need_defer(req) && list_empty(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06002565 return 0;
2566
2567 sqe_copy = kmalloc(sizeof(*sqe_copy), GFP_KERNEL);
2568 if (!sqe_copy)
2569 return -EAGAIN;
2570
2571 spin_lock_irq(&ctx->completion_lock);
Bob Liu9d858b22019-11-13 18:06:25 +08002572 if (!req_need_defer(req) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06002573 spin_unlock_irq(&ctx->completion_lock);
2574 kfree(sqe_copy);
2575 return 0;
2576 }
2577
2578 memcpy(sqe_copy, sqe, sizeof(*sqe_copy));
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03002579 req->flags |= REQ_F_FREE_SQE;
Jens Axboede0617e2019-04-06 21:51:27 -06002580 req->submit.sqe = sqe_copy;
2581
Jens Axboe915967f2019-11-21 09:01:20 -07002582 trace_io_uring_defer(ctx, req, req->user_data);
Jens Axboede0617e2019-04-06 21:51:27 -06002583 list_add_tail(&req->list, &ctx->defer_list);
2584 spin_unlock_irq(&ctx->completion_lock);
2585 return -EIOCBQUEUED;
2586}
2587
Pavel Begunkovd7324472019-11-21 21:24:36 +03002588static int io_issue_sqe(struct io_kiocb *req, struct io_kiocb **nxt,
2589 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002590{
Jens Axboee0c5c572019-03-12 10:18:47 -06002591 int ret, opcode;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002592 struct sqe_submit *s = &req->submit;
Jackie Liua197f662019-11-08 08:09:12 -07002593 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002594
2595 opcode = READ_ONCE(s->sqe->opcode);
2596 switch (opcode) {
2597 case IORING_OP_NOP:
Jens Axboe78e19bb2019-11-06 15:21:34 -07002598 ret = io_nop(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002599 break;
2600 case IORING_OP_READV:
Jens Axboeedafcce2019-01-09 09:16:05 -07002601 if (unlikely(s->sqe->buf_index))
2602 return -EINVAL;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002603 ret = io_read(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002604 break;
2605 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07002606 if (unlikely(s->sqe->buf_index))
2607 return -EINVAL;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002608 ret = io_write(req, nxt, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07002609 break;
2610 case IORING_OP_READ_FIXED:
Pavel Begunkov267bc902019-11-07 01:41:08 +03002611 ret = io_read(req, nxt, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07002612 break;
2613 case IORING_OP_WRITE_FIXED:
Pavel Begunkov267bc902019-11-07 01:41:08 +03002614 ret = io_write(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002615 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002616 case IORING_OP_FSYNC:
Jens Axboeba816ad2019-09-28 11:36:45 -06002617 ret = io_fsync(req, s->sqe, nxt, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002618 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002619 case IORING_OP_POLL_ADD:
Jens Axboe89723d02019-11-05 15:32:58 -07002620 ret = io_poll_add(req, s->sqe, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002621 break;
2622 case IORING_OP_POLL_REMOVE:
2623 ret = io_poll_remove(req, s->sqe);
2624 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002625 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboeba816ad2019-09-28 11:36:45 -06002626 ret = io_sync_file_range(req, s->sqe, nxt, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002627 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002628 case IORING_OP_SENDMSG:
Jens Axboeba816ad2019-09-28 11:36:45 -06002629 ret = io_sendmsg(req, s->sqe, nxt, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06002630 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06002631 case IORING_OP_RECVMSG:
Jens Axboeba816ad2019-09-28 11:36:45 -06002632 ret = io_recvmsg(req, s->sqe, nxt, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06002633 break;
Jens Axboe5262f562019-09-17 12:26:57 -06002634 case IORING_OP_TIMEOUT:
2635 ret = io_timeout(req, s->sqe);
2636 break;
Jens Axboe11365042019-10-16 09:08:32 -06002637 case IORING_OP_TIMEOUT_REMOVE:
2638 ret = io_timeout_remove(req, s->sqe);
2639 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06002640 case IORING_OP_ACCEPT:
2641 ret = io_accept(req, s->sqe, nxt, force_nonblock);
2642 break;
Jens Axboe62755e32019-10-28 21:49:21 -06002643 case IORING_OP_ASYNC_CANCEL:
2644 ret = io_async_cancel(req, s->sqe, nxt);
2645 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002646 default:
2647 ret = -EINVAL;
2648 break;
2649 }
2650
Jens Axboedef596e2019-01-09 08:59:42 -07002651 if (ret)
2652 return ret;
2653
2654 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe9e645e112019-05-10 16:07:28 -06002655 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07002656 return -EAGAIN;
2657
2658 /* workqueue context doesn't hold uring_lock, grab it now */
Jackie Liuba5290c2019-10-09 09:19:59 +08002659 if (s->in_async)
Jens Axboedef596e2019-01-09 08:59:42 -07002660 mutex_lock(&ctx->uring_lock);
2661 io_iopoll_req_issued(req);
Jackie Liuba5290c2019-10-09 09:19:59 +08002662 if (s->in_async)
Jens Axboedef596e2019-01-09 08:59:42 -07002663 mutex_unlock(&ctx->uring_lock);
2664 }
2665
2666 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002667}
2668
Jens Axboeb76da702019-11-20 13:05:32 -07002669static void io_link_work_cb(struct io_wq_work **workptr)
2670{
2671 struct io_wq_work *work = *workptr;
2672 struct io_kiocb *link = work->data;
2673
2674 io_queue_linked_timeout(link);
2675 work->func = io_wq_submit_work;
2676}
2677
Jens Axboe561fb042019-10-24 07:25:42 -06002678static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboe31b51512019-01-18 22:56:34 -07002679{
Jens Axboe561fb042019-10-24 07:25:42 -06002680 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002681 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe561fb042019-10-24 07:25:42 -06002682 struct sqe_submit *s = &req->submit;
Jens Axboe561fb042019-10-24 07:25:42 -06002683 struct io_kiocb *nxt = NULL;
2684 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002685
Jens Axboe561fb042019-10-24 07:25:42 -06002686 /* Ensure we clear previously set non-block flag */
2687 req->rw.ki_flags &= ~IOCB_NOWAIT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002688
Jens Axboe561fb042019-10-24 07:25:42 -06002689 if (work->flags & IO_WQ_WORK_CANCEL)
2690 ret = -ECANCELED;
Jens Axboe31b51512019-01-18 22:56:34 -07002691
Jens Axboe561fb042019-10-24 07:25:42 -06002692 if (!ret) {
2693 s->has_user = (work->flags & IO_WQ_WORK_HAS_MM) != 0;
2694 s->in_async = true;
2695 do {
Pavel Begunkovd7324472019-11-21 21:24:36 +03002696 ret = io_issue_sqe(req, &nxt, false);
Jens Axboe561fb042019-10-24 07:25:42 -06002697 /*
2698 * We can get EAGAIN for polled IO even though we're
2699 * forcing a sync submission from here, since we can't
2700 * wait for request slots on the block side.
2701 */
2702 if (ret != -EAGAIN)
2703 break;
2704 cond_resched();
2705 } while (1);
2706 }
Jens Axboe31b51512019-01-18 22:56:34 -07002707
Jens Axboe561fb042019-10-24 07:25:42 -06002708 /* drop submission reference */
Jackie Liuec9c02a2019-11-08 23:50:36 +08002709 io_put_req(req);
Jens Axboe817869d2019-04-30 14:44:05 -06002710
Jens Axboe561fb042019-10-24 07:25:42 -06002711 if (ret) {
Jens Axboef1f40852019-11-05 20:33:16 -07002712 if (req->flags & REQ_F_LINK)
2713 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07002714 io_cqring_add_event(req, ret);
Jens Axboe817869d2019-04-30 14:44:05 -06002715 io_put_req(req);
Jens Axboeedafcce2019-01-09 09:16:05 -07002716 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002717
Jens Axboe561fb042019-10-24 07:25:42 -06002718 /* if a dependent link is ready, pass it back */
2719 if (!ret && nxt) {
Jens Axboe94ae5e72019-11-14 19:39:52 -07002720 struct io_kiocb *link;
2721
2722 io_prep_async_work(nxt, &link);
Jens Axboe561fb042019-10-24 07:25:42 -06002723 *workptr = &nxt->work;
Jens Axboeb76da702019-11-20 13:05:32 -07002724 if (link) {
2725 nxt->work.flags |= IO_WQ_WORK_CB;
2726 nxt->work.func = io_link_work_cb;
2727 nxt->work.data = link;
2728 }
Jens Axboeedafcce2019-01-09 09:16:05 -07002729 }
Jens Axboe31b51512019-01-18 22:56:34 -07002730}
Jens Axboe2b188cc2019-01-07 10:46:33 -07002731
Jens Axboe09bb8392019-03-13 12:39:28 -06002732static bool io_op_needs_file(const struct io_uring_sqe *sqe)
2733{
2734 int op = READ_ONCE(sqe->opcode);
2735
2736 switch (op) {
2737 case IORING_OP_NOP:
2738 case IORING_OP_POLL_REMOVE:
Pavel Begunkov5683e542019-11-14 00:59:19 +03002739 case IORING_OP_TIMEOUT:
Pavel Begunkova320e9f2019-11-14 00:11:01 +03002740 case IORING_OP_TIMEOUT_REMOVE:
2741 case IORING_OP_ASYNC_CANCEL:
2742 case IORING_OP_LINK_TIMEOUT:
Jens Axboe09bb8392019-03-13 12:39:28 -06002743 return false;
2744 default:
2745 return true;
2746 }
2747}
2748
Jens Axboe65e19f52019-10-26 07:20:21 -06002749static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
2750 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06002751{
Jens Axboe65e19f52019-10-26 07:20:21 -06002752 struct fixed_file_table *table;
2753
2754 table = &ctx->file_table[index >> IORING_FILE_TABLE_SHIFT];
2755 return table->files[index & IORING_FILE_TABLE_MASK];
2756}
2757
Jackie Liua197f662019-11-08 08:09:12 -07002758static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req)
Jens Axboe09bb8392019-03-13 12:39:28 -06002759{
Pavel Begunkov267bc902019-11-07 01:41:08 +03002760 struct sqe_submit *s = &req->submit;
Jackie Liua197f662019-11-08 08:09:12 -07002761 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe09bb8392019-03-13 12:39:28 -06002762 unsigned flags;
2763 int fd;
2764
2765 flags = READ_ONCE(s->sqe->flags);
2766 fd = READ_ONCE(s->sqe->fd);
2767
Jackie Liu4fe2c962019-09-09 20:50:40 +08002768 if (flags & IOSQE_IO_DRAIN)
Jens Axboede0617e2019-04-06 21:51:27 -06002769 req->flags |= REQ_F_IO_DRAIN;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002770 /*
2771 * All io need record the previous position, if LINK vs DARIN,
2772 * it can be used to mark the position of the first IO in the
2773 * link list.
2774 */
2775 req->sequence = s->sequence;
Jens Axboede0617e2019-04-06 21:51:27 -06002776
Jens Axboe60c112b2019-06-21 10:20:18 -06002777 if (!io_op_needs_file(s->sqe))
Jens Axboe09bb8392019-03-13 12:39:28 -06002778 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06002779
2780 if (flags & IOSQE_FIXED_FILE) {
Jens Axboe65e19f52019-10-26 07:20:21 -06002781 if (unlikely(!ctx->file_table ||
Jens Axboe09bb8392019-03-13 12:39:28 -06002782 (unsigned) fd >= ctx->nr_user_files))
2783 return -EBADF;
Jens Axboeb7620122019-10-26 07:22:55 -06002784 fd = array_index_nospec(fd, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06002785 req->file = io_file_from_index(ctx, fd);
2786 if (!req->file)
Jens Axboe08a45172019-10-03 08:11:03 -06002787 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06002788 req->flags |= REQ_F_FIXED_FILE;
2789 } else {
2790 if (s->needs_fixed_file)
2791 return -EBADF;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002792 trace_io_uring_file_get(ctx, fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06002793 req->file = io_file_get(state, fd);
2794 if (unlikely(!req->file))
2795 return -EBADF;
2796 }
2797
2798 return 0;
2799}
2800
Jackie Liua197f662019-11-08 08:09:12 -07002801static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002802{
Jens Axboefcb323c2019-10-24 12:39:47 -06002803 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07002804 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06002805
2806 rcu_read_lock();
2807 spin_lock_irq(&ctx->inflight_lock);
2808 /*
2809 * We use the f_ops->flush() handler to ensure that we can flush
2810 * out work accessing these files if the fd is closed. Check if
2811 * the fd has changed since we started down this path, and disallow
2812 * this operation if it has.
2813 */
2814 if (fcheck(req->submit.ring_fd) == req->submit.ring_file) {
2815 list_add(&req->inflight_entry, &ctx->inflight_list);
2816 req->flags |= REQ_F_INFLIGHT;
2817 req->work.files = current->files;
2818 ret = 0;
2819 }
2820 spin_unlock_irq(&ctx->inflight_lock);
2821 rcu_read_unlock();
2822
2823 return ret;
2824}
2825
Jens Axboe2665abf2019-11-05 12:40:47 -07002826static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
2827{
Jens Axboead8a48a2019-11-15 08:49:11 -07002828 struct io_timeout_data *data = container_of(timer,
2829 struct io_timeout_data, timer);
2830 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07002831 struct io_ring_ctx *ctx = req->ctx;
2832 struct io_kiocb *prev = NULL;
2833 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07002834
2835 spin_lock_irqsave(&ctx->completion_lock, flags);
2836
2837 /*
2838 * We don't expect the list to be empty, that will only happen if we
2839 * race with the completion of the linked work.
2840 */
2841 if (!list_empty(&req->list)) {
2842 prev = list_entry(req->list.prev, struct io_kiocb, link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07002843 if (refcount_inc_not_zero(&prev->refs)) {
Jens Axboe76a46e02019-11-10 23:34:16 -07002844 list_del_init(&req->list);
Jens Axboe5d960722019-11-19 15:31:28 -07002845 prev->flags &= ~REQ_F_LINK_TIMEOUT;
2846 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07002847 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07002848 }
2849
2850 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2851
2852 if (prev) {
Jens Axboefba38c22019-11-18 12:27:57 -07002853 if (prev->flags & REQ_F_LINK)
2854 prev->flags |= REQ_F_FAIL_LINK;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002855 io_async_find_and_cancel(ctx, req, prev->user_data, NULL,
2856 -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07002857 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07002858 } else {
2859 io_cqring_add_event(req, -ETIME);
2860 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07002861 }
Jens Axboe2665abf2019-11-05 12:40:47 -07002862 return HRTIMER_NORESTART;
2863}
2864
Jens Axboead8a48a2019-11-15 08:49:11 -07002865static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07002866{
Jens Axboe76a46e02019-11-10 23:34:16 -07002867 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07002868
Jens Axboe76a46e02019-11-10 23:34:16 -07002869 /*
2870 * If the list is now empty, then our linked request finished before
2871 * we got a chance to setup the timer
2872 */
2873 spin_lock_irq(&ctx->completion_lock);
2874 if (!list_empty(&req->list)) {
Jens Axboe94ae5e72019-11-14 19:39:52 -07002875 struct io_timeout_data *data = req->timeout.data;
2876
Jens Axboead8a48a2019-11-15 08:49:11 -07002877 data->timer.function = io_link_timeout_fn;
2878 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
2879 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07002880 }
Jens Axboe76a46e02019-11-10 23:34:16 -07002881 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07002882
Jens Axboe2665abf2019-11-05 12:40:47 -07002883 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07002884 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07002885}
2886
Jens Axboead8a48a2019-11-15 08:49:11 -07002887static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07002888{
2889 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002890
Jens Axboe2665abf2019-11-05 12:40:47 -07002891 if (!(req->flags & REQ_F_LINK))
2892 return NULL;
2893
2894 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, list);
Jens Axboe76a46e02019-11-10 23:34:16 -07002895 if (!nxt || nxt->submit.sqe->opcode != IORING_OP_LINK_TIMEOUT)
2896 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07002897
Jens Axboe76a46e02019-11-10 23:34:16 -07002898 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07002899 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07002900}
2901
Jens Axboe0e0702d2019-11-14 21:42:10 -07002902static void __io_queue_sqe(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002903{
Jens Axboe94ae5e72019-11-14 19:39:52 -07002904 struct io_kiocb *nxt = io_prep_linked_timeout(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002905 int ret;
2906
Pavel Begunkovd7324472019-11-21 21:24:36 +03002907 ret = io_issue_sqe(req, NULL, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06002908
2909 /*
2910 * We async punt it if the file wasn't marked NOWAIT, or if the file
2911 * doesn't support non-blocking read/write attempts
2912 */
2913 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
2914 (req->flags & REQ_F_MUST_PUNT))) {
Pavel Begunkov267bc902019-11-07 01:41:08 +03002915 struct sqe_submit *s = &req->submit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002916 struct io_uring_sqe *sqe_copy;
2917
Jackie Liu954dab12019-09-18 10:37:52 +08002918 sqe_copy = kmemdup(s->sqe, sizeof(*sqe_copy), GFP_KERNEL);
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03002919 if (!sqe_copy)
2920 goto err;
Jens Axboee65ef562019-03-12 10:16:44 -06002921
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03002922 s->sqe = sqe_copy;
2923 req->flags |= REQ_F_FREE_SQE;
2924
2925 if (req->work.flags & IO_WQ_WORK_NEEDS_FILES) {
2926 ret = io_grab_files(req);
2927 if (ret)
2928 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002929 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03002930
2931 /*
2932 * Queued up for async execution, worker will release
2933 * submit reference when the iocb is actually submitted.
2934 */
2935 io_queue_async_work(req);
2936 return;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002937 }
Jens Axboee65ef562019-03-12 10:16:44 -06002938
Jens Axboefcb323c2019-10-24 12:39:47 -06002939err:
Jens Axboee65ef562019-03-12 10:16:44 -06002940 /* drop submission reference */
2941 io_put_req(req);
2942
Jens Axboe76a46e02019-11-10 23:34:16 -07002943 if (nxt) {
2944 if (!ret)
Jens Axboead8a48a2019-11-15 08:49:11 -07002945 io_queue_linked_timeout(nxt);
Jens Axboe76a46e02019-11-10 23:34:16 -07002946 else
2947 io_put_req(nxt);
2948 }
2949
Jens Axboee65ef562019-03-12 10:16:44 -06002950 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06002951 if (ret) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07002952 io_cqring_add_event(req, ret);
Jens Axboe9e645e112019-05-10 16:07:28 -06002953 if (req->flags & REQ_F_LINK)
2954 req->flags |= REQ_F_FAIL_LINK;
Jens Axboee65ef562019-03-12 10:16:44 -06002955 io_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002956 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002957}
2958
Jens Axboe0e0702d2019-11-14 21:42:10 -07002959static void io_queue_sqe(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08002960{
2961 int ret;
2962
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03002963 if (unlikely(req->ctx->drain_next)) {
2964 req->flags |= REQ_F_IO_DRAIN;
2965 req->ctx->drain_next = false;
2966 }
2967 req->ctx->drain_next = (req->flags & REQ_F_DRAIN_LINK);
2968
Jackie Liua197f662019-11-08 08:09:12 -07002969 ret = io_req_defer(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002970 if (ret) {
2971 if (ret != -EIOCBQUEUED) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07002972 io_cqring_add_event(req, ret);
Pavel Begunkovd3b357962019-11-19 23:32:48 +03002973 if (req->flags & REQ_F_LINK)
2974 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07002975 io_double_put_req(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002976 }
Jens Axboe0e0702d2019-11-14 21:42:10 -07002977 } else
2978 __io_queue_sqe(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002979}
2980
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03002981static inline void io_queue_link_head(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08002982{
Jens Axboe94ae5e72019-11-14 19:39:52 -07002983 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03002984 io_cqring_add_event(req, -ECANCELED);
2985 io_double_put_req(req);
2986 } else
Jens Axboe0e0702d2019-11-14 21:42:10 -07002987 io_queue_sqe(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002988}
2989
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03002990
Jens Axboe9e645e112019-05-10 16:07:28 -06002991#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK)
2992
Jackie Liua197f662019-11-08 08:09:12 -07002993static void io_submit_sqe(struct io_kiocb *req, struct io_submit_state *state,
2994 struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06002995{
Pavel Begunkov267bc902019-11-07 01:41:08 +03002996 struct sqe_submit *s = &req->submit;
Jackie Liua197f662019-11-08 08:09:12 -07002997 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -06002998 int ret;
2999
Jens Axboe78e19bb2019-11-06 15:21:34 -07003000 req->user_data = s->sqe->user_data;
3001
Jens Axboe9e645e112019-05-10 16:07:28 -06003002 /* enforce forwards compatibility on users */
3003 if (unlikely(s->sqe->flags & ~SQE_VALID_FLAGS)) {
3004 ret = -EINVAL;
Pavel Begunkov196be952019-11-07 01:41:06 +03003005 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06003006 }
3007
Jackie Liua197f662019-11-08 08:09:12 -07003008 ret = io_req_set_file(state, req);
Jens Axboe9e645e112019-05-10 16:07:28 -06003009 if (unlikely(ret)) {
3010err_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07003011 io_cqring_add_event(req, ret);
3012 io_double_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06003013 return;
3014 }
3015
Jens Axboe9e645e112019-05-10 16:07:28 -06003016 /*
3017 * If we already have a head request, queue this one for async
3018 * submittal once the head completes. If we don't have a head but
3019 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
3020 * submitted sync once the chain is complete. If none of those
3021 * conditions are true (normal request), then just queue it.
3022 */
3023 if (*link) {
3024 struct io_kiocb *prev = *link;
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03003025 struct io_uring_sqe *sqe_copy;
Jens Axboe9e645e112019-05-10 16:07:28 -06003026
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003027 if (s->sqe->flags & IOSQE_IO_DRAIN)
3028 (*link)->flags |= REQ_F_DRAIN_LINK | REQ_F_IO_DRAIN;
3029
Jens Axboe94ae5e72019-11-14 19:39:52 -07003030 if (READ_ONCE(s->sqe->opcode) == IORING_OP_LINK_TIMEOUT) {
3031 ret = io_timeout_setup(req);
3032 /* common setup allows offset being set, we don't */
3033 if (!ret && s->sqe->off)
3034 ret = -EINVAL;
3035 if (ret) {
3036 prev->flags |= REQ_F_FAIL_LINK;
3037 goto err_req;
3038 }
3039 }
3040
Jens Axboe9e645e112019-05-10 16:07:28 -06003041 sqe_copy = kmemdup(s->sqe, sizeof(*sqe_copy), GFP_KERNEL);
3042 if (!sqe_copy) {
3043 ret = -EAGAIN;
3044 goto err_req;
3045 }
3046
3047 s->sqe = sqe_copy;
Jens Axboe94ae5e72019-11-14 19:39:52 -07003048 req->flags |= REQ_F_FREE_SQE;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02003049 trace_io_uring_link(ctx, req, prev);
Jens Axboe9e645e112019-05-10 16:07:28 -06003050 list_add_tail(&req->list, &prev->link_list);
3051 } else if (s->sqe->flags & IOSQE_IO_LINK) {
3052 req->flags |= REQ_F_LINK;
3053
Jens Axboe9e645e112019-05-10 16:07:28 -06003054 INIT_LIST_HEAD(&req->link_list);
3055 *link = req;
3056 } else {
Jackie Liua197f662019-11-08 08:09:12 -07003057 io_queue_sqe(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06003058 }
3059}
3060
Jens Axboe9a56a232019-01-09 09:06:50 -07003061/*
3062 * Batched submission is done, ensure local IO is flushed out.
3063 */
3064static void io_submit_state_end(struct io_submit_state *state)
3065{
3066 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06003067 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07003068 if (state->free_reqs)
3069 kmem_cache_free_bulk(req_cachep, state->free_reqs,
3070 &state->reqs[state->cur_req]);
Jens Axboe9a56a232019-01-09 09:06:50 -07003071}
3072
3073/*
3074 * Start submission side cache.
3075 */
3076static void io_submit_state_start(struct io_submit_state *state,
3077 struct io_ring_ctx *ctx, unsigned max_ios)
3078{
3079 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07003080 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07003081 state->file = NULL;
3082 state->ios_left = max_ios;
3083}
3084
Jens Axboe2b188cc2019-01-07 10:46:33 -07003085static void io_commit_sqring(struct io_ring_ctx *ctx)
3086{
Hristo Venev75b28af2019-08-26 17:23:46 +00003087 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003088
Hristo Venev75b28af2019-08-26 17:23:46 +00003089 if (ctx->cached_sq_head != READ_ONCE(rings->sq.head)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07003090 /*
3091 * Ensure any loads from the SQEs are done at this point,
3092 * since once we write the new head, the application could
3093 * write new data to them.
3094 */
Hristo Venev75b28af2019-08-26 17:23:46 +00003095 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003096 }
3097}
3098
3099/*
Jens Axboe2b188cc2019-01-07 10:46:33 -07003100 * Fetch an sqe, if one is available. Note that s->sqe will point to memory
3101 * that is mapped by userspace. This means that care needs to be taken to
3102 * ensure that reads are stable, as we cannot rely on userspace always
3103 * being a good citizen. If members of the sqe are validated and then later
3104 * used, it's important that those reads are done through READ_ONCE() to
3105 * prevent a re-load down the line.
3106 */
3107static bool io_get_sqring(struct io_ring_ctx *ctx, struct sqe_submit *s)
3108{
Hristo Venev75b28af2019-08-26 17:23:46 +00003109 struct io_rings *rings = ctx->rings;
3110 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003111 unsigned head;
3112
3113 /*
3114 * The cached sq head (or cq tail) serves two purposes:
3115 *
3116 * 1) allows us to batch the cost of updating the user visible
3117 * head updates.
3118 * 2) allows the kernel side to track the head on its own, even
3119 * though the application is the one updating it.
3120 */
3121 head = ctx->cached_sq_head;
Stefan Bühlere523a292019-04-19 11:57:44 +02003122 /* make sure SQ entry isn't read before tail */
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03003123 if (unlikely(head == smp_load_acquire(&rings->sq.tail)))
Jens Axboe2b188cc2019-01-07 10:46:33 -07003124 return false;
3125
Hristo Venev75b28af2019-08-26 17:23:46 +00003126 head = READ_ONCE(sq_array[head & ctx->sq_mask]);
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03003127 if (likely(head < ctx->sq_entries)) {
Jens Axboefcb323c2019-10-24 12:39:47 -06003128 s->ring_file = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003129 s->sqe = &ctx->sq_sqes[head];
Jackie Liu8776f3f2019-09-09 20:50:39 +08003130 s->sequence = ctx->cached_sq_head;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003131 ctx->cached_sq_head++;
3132 return true;
3133 }
3134
3135 /* drop invalid entries */
3136 ctx->cached_sq_head++;
Jens Axboe498ccd92019-10-25 10:04:25 -06003137 ctx->cached_sq_dropped++;
3138 WRITE_ONCE(rings->sq_dropped, ctx->cached_sq_dropped);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003139 return false;
3140}
3141
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003142static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkovae9428c2019-11-06 00:22:14 +03003143 struct file *ring_file, int ring_fd,
3144 struct mm_struct **mm, bool async)
Jens Axboe6c271ce2019-01-10 11:22:30 -07003145{
3146 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06003147 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06003148 int i, submitted = 0;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03003149 bool mm_fault = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003150
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003151 if (!list_empty(&ctx->cq_overflow_list)) {
3152 io_cqring_overflow_flush(ctx, false);
3153 return -EBUSY;
3154 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07003155
3156 if (nr > IO_PLUG_THRESHOLD) {
3157 io_submit_state_start(&state, ctx, nr);
3158 statep = &state;
3159 }
3160
3161 for (i = 0; i < nr; i++) {
Pavel Begunkov196be952019-11-07 01:41:06 +03003162 struct io_kiocb *req;
Pavel Begunkov50585b92019-11-07 01:41:07 +03003163 unsigned int sqe_flags;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003164
Pavel Begunkov196be952019-11-07 01:41:06 +03003165 req = io_get_req(ctx, statep);
3166 if (unlikely(!req)) {
3167 if (!submitted)
3168 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003169 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06003170 }
Pavel Begunkov50585b92019-11-07 01:41:07 +03003171 if (!io_get_sqring(ctx, &req->submit)) {
Pavel Begunkov196be952019-11-07 01:41:06 +03003172 __io_free_req(req);
3173 break;
3174 }
Jens Axboe9e645e112019-05-10 16:07:28 -06003175
Pavel Begunkov50585b92019-11-07 01:41:07 +03003176 if (io_sqe_needs_user(req->submit.sqe) && !*mm) {
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03003177 mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm);
3178 if (!mm_fault) {
3179 use_mm(ctx->sqo_mm);
3180 *mm = ctx->sqo_mm;
3181 }
3182 }
3183
Pavel Begunkov50585b92019-11-07 01:41:07 +03003184 sqe_flags = req->submit.sqe->flags;
3185
Pavel Begunkov50585b92019-11-07 01:41:07 +03003186 req->submit.ring_file = ring_file;
3187 req->submit.ring_fd = ring_fd;
3188 req->submit.has_user = *mm != NULL;
3189 req->submit.in_async = async;
3190 req->submit.needs_fixed_file = async;
3191 trace_io_uring_submit_sqe(ctx, req->submit.sqe->user_data,
3192 true, async);
Jackie Liua197f662019-11-08 08:09:12 -07003193 io_submit_sqe(req, statep, &link);
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03003194 submitted++;
Pavel Begunkove5eb6362019-11-06 00:22:15 +03003195
3196 /*
3197 * If previous wasn't linked and we have a linked command,
3198 * that's the end of the chain. Submit the previous link.
3199 */
Pavel Begunkov50585b92019-11-07 01:41:07 +03003200 if (!(sqe_flags & IOSQE_IO_LINK) && link) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003201 io_queue_link_head(link);
Pavel Begunkove5eb6362019-11-06 00:22:15 +03003202 link = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003203 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07003204 }
3205
Jens Axboe9e645e112019-05-10 16:07:28 -06003206 if (link)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003207 io_queue_link_head(link);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003208 if (statep)
3209 io_submit_state_end(&state);
3210
Pavel Begunkovae9428c2019-11-06 00:22:14 +03003211 /* Commit SQ ring head once we've consumed and submitted all SQEs */
3212 io_commit_sqring(ctx);
3213
Jens Axboe6c271ce2019-01-10 11:22:30 -07003214 return submitted;
3215}
3216
3217static int io_sq_thread(void *data)
3218{
Jens Axboe6c271ce2019-01-10 11:22:30 -07003219 struct io_ring_ctx *ctx = data;
3220 struct mm_struct *cur_mm = NULL;
3221 mm_segment_t old_fs;
3222 DEFINE_WAIT(wait);
3223 unsigned inflight;
3224 unsigned long timeout;
Jens Axboec1edbf52019-11-10 16:56:04 -07003225 int ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003226
Jens Axboe206aefd2019-11-07 18:27:42 -07003227 complete(&ctx->completions[1]);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08003228
Jens Axboe6c271ce2019-01-10 11:22:30 -07003229 old_fs = get_fs();
3230 set_fs(USER_DS);
3231
Jens Axboec1edbf52019-11-10 16:56:04 -07003232 ret = timeout = inflight = 0;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003233 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003234 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003235
3236 if (inflight) {
3237 unsigned nr_events = 0;
3238
3239 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe2b2ed972019-10-25 10:06:15 -06003240 /*
3241 * inflight is the count of the maximum possible
3242 * entries we submitted, but it can be smaller
3243 * if we dropped some of them. If we don't have
3244 * poll entries available, then we know that we
3245 * have nothing left to poll for. Reset the
3246 * inflight count to zero in that case.
3247 */
3248 mutex_lock(&ctx->uring_lock);
3249 if (!list_empty(&ctx->poll_list))
3250 __io_iopoll_check(ctx, &nr_events, 0);
3251 else
3252 inflight = 0;
3253 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003254 } else {
3255 /*
3256 * Normal IO, just pretend everything completed.
3257 * We don't have to poll completions for that.
3258 */
3259 nr_events = inflight;
3260 }
3261
3262 inflight -= nr_events;
3263 if (!inflight)
3264 timeout = jiffies + ctx->sq_thread_idle;
3265 }
3266
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003267 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07003268
3269 /*
3270 * If submit got -EBUSY, flag us as needing the application
3271 * to enter the kernel to reap and flush events.
3272 */
3273 if (!to_submit || ret == -EBUSY) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07003274 /*
3275 * We're polling. If we're within the defined idle
3276 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07003277 * to sleep. The exception is if we got EBUSY doing
3278 * more IO, we should wait for the application to
3279 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07003280 */
Jens Axboec1edbf52019-11-10 16:56:04 -07003281 if (inflight ||
3282 (!time_after(jiffies, timeout) && ret != -EBUSY)) {
Jens Axboe9831a902019-09-19 09:48:55 -06003283 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07003284 continue;
3285 }
3286
3287 /*
3288 * Drop cur_mm before scheduling, we can't hold it for
3289 * long periods (or over schedule()). Do this before
3290 * adding ourselves to the waitqueue, as the unuse/drop
3291 * may sleep.
3292 */
3293 if (cur_mm) {
3294 unuse_mm(cur_mm);
3295 mmput(cur_mm);
3296 cur_mm = NULL;
3297 }
3298
3299 prepare_to_wait(&ctx->sqo_wait, &wait,
3300 TASK_INTERRUPTIBLE);
3301
3302 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00003303 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02003304 /* make sure to read SQ tail after writing flags */
3305 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07003306
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003307 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07003308 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003309 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07003310 finish_wait(&ctx->sqo_wait, &wait);
3311 break;
3312 }
3313 if (signal_pending(current))
3314 flush_signals(current);
3315 schedule();
3316 finish_wait(&ctx->sqo_wait, &wait);
3317
Hristo Venev75b28af2019-08-26 17:23:46 +00003318 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003319 continue;
3320 }
3321 finish_wait(&ctx->sqo_wait, &wait);
3322
Hristo Venev75b28af2019-08-26 17:23:46 +00003323 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003324 }
3325
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003326 to_submit = min(to_submit, ctx->sq_entries);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003327 ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true);
3328 if (ret > 0)
3329 inflight += ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003330 }
3331
3332 set_fs(old_fs);
3333 if (cur_mm) {
3334 unuse_mm(cur_mm);
3335 mmput(cur_mm);
3336 }
Jens Axboe06058632019-04-13 09:26:03 -06003337
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003338 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06003339
Jens Axboe6c271ce2019-01-10 11:22:30 -07003340 return 0;
3341}
3342
Jens Axboebda52162019-09-24 13:47:15 -06003343struct io_wait_queue {
3344 struct wait_queue_entry wq;
3345 struct io_ring_ctx *ctx;
3346 unsigned to_wait;
3347 unsigned nr_timeouts;
3348};
3349
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003350static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06003351{
3352 struct io_ring_ctx *ctx = iowq->ctx;
3353
3354 /*
3355 * Wake up if we have enough events, or if a timeout occured since we
3356 * started waiting. For timeouts, we always want to return to userspace,
3357 * regardless of event count.
3358 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003359 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06003360 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
3361}
3362
3363static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
3364 int wake_flags, void *key)
3365{
3366 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
3367 wq);
3368
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003369 /* use noflush == true, as we can't safely rely on locking context */
3370 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06003371 return -1;
3372
3373 return autoremove_wake_function(curr, mode, wake_flags, key);
3374}
3375
Jens Axboe2b188cc2019-01-07 10:46:33 -07003376/*
3377 * Wait until events become available, if we don't already have some. The
3378 * application must reap them itself, as they reside on the shared cq ring.
3379 */
3380static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
3381 const sigset_t __user *sig, size_t sigsz)
3382{
Jens Axboebda52162019-09-24 13:47:15 -06003383 struct io_wait_queue iowq = {
3384 .wq = {
3385 .private = current,
3386 .func = io_wake_function,
3387 .entry = LIST_HEAD_INIT(iowq.wq.entry),
3388 },
3389 .ctx = ctx,
3390 .to_wait = min_events,
3391 };
Hristo Venev75b28af2019-08-26 17:23:46 +00003392 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08003393 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003394
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003395 if (io_cqring_events(ctx, false) >= min_events)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003396 return 0;
3397
3398 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01003399#ifdef CONFIG_COMPAT
3400 if (in_compat_syscall())
3401 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07003402 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01003403 else
3404#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07003405 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01003406
Jens Axboe2b188cc2019-01-07 10:46:33 -07003407 if (ret)
3408 return ret;
3409 }
3410
Jens Axboebda52162019-09-24 13:47:15 -06003411 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02003412 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06003413 do {
3414 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
3415 TASK_INTERRUPTIBLE);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003416 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06003417 break;
3418 schedule();
3419 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08003420 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06003421 break;
3422 }
3423 } while (1);
3424 finish_wait(&ctx->wait, &iowq.wq);
3425
Jackie Liue9ffa5c2019-10-29 11:16:42 +08003426 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003427
Hristo Venev75b28af2019-08-26 17:23:46 +00003428 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003429}
3430
Jens Axboe6b063142019-01-10 22:13:58 -07003431static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
3432{
3433#if defined(CONFIG_UNIX)
3434 if (ctx->ring_sock) {
3435 struct sock *sock = ctx->ring_sock->sk;
3436 struct sk_buff *skb;
3437
3438 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
3439 kfree_skb(skb);
3440 }
3441#else
3442 int i;
3443
Jens Axboe65e19f52019-10-26 07:20:21 -06003444 for (i = 0; i < ctx->nr_user_files; i++) {
3445 struct file *file;
3446
3447 file = io_file_from_index(ctx, i);
3448 if (file)
3449 fput(file);
3450 }
Jens Axboe6b063142019-01-10 22:13:58 -07003451#endif
3452}
3453
3454static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
3455{
Jens Axboe65e19f52019-10-26 07:20:21 -06003456 unsigned nr_tables, i;
3457
3458 if (!ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003459 return -ENXIO;
3460
3461 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06003462 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
3463 for (i = 0; i < nr_tables; i++)
3464 kfree(ctx->file_table[i].files);
3465 kfree(ctx->file_table);
3466 ctx->file_table = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003467 ctx->nr_user_files = 0;
3468 return 0;
3469}
3470
Jens Axboe6c271ce2019-01-10 11:22:30 -07003471static void io_sq_thread_stop(struct io_ring_ctx *ctx)
3472{
3473 if (ctx->sqo_thread) {
Jens Axboe206aefd2019-11-07 18:27:42 -07003474 wait_for_completion(&ctx->completions[1]);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003475 /*
3476 * The park is a bit of a work-around, without it we get
3477 * warning spews on shutdown with SQPOLL set and affinity
3478 * set to a single CPU.
3479 */
Jens Axboe06058632019-04-13 09:26:03 -06003480 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003481 kthread_stop(ctx->sqo_thread);
3482 ctx->sqo_thread = NULL;
3483 }
3484}
3485
Jens Axboe6b063142019-01-10 22:13:58 -07003486static void io_finish_async(struct io_ring_ctx *ctx)
3487{
Jens Axboe6c271ce2019-01-10 11:22:30 -07003488 io_sq_thread_stop(ctx);
3489
Jens Axboe561fb042019-10-24 07:25:42 -06003490 if (ctx->io_wq) {
3491 io_wq_destroy(ctx->io_wq);
3492 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003493 }
3494}
3495
3496#if defined(CONFIG_UNIX)
3497static void io_destruct_skb(struct sk_buff *skb)
3498{
3499 struct io_ring_ctx *ctx = skb->sk->sk_user_data;
3500
Jens Axboe561fb042019-10-24 07:25:42 -06003501 if (ctx->io_wq)
3502 io_wq_flush(ctx->io_wq);
Jens Axboe8a997342019-10-09 14:40:13 -06003503
Jens Axboe6b063142019-01-10 22:13:58 -07003504 unix_destruct_scm(skb);
3505}
3506
3507/*
3508 * Ensure the UNIX gc is aware of our file set, so we are certain that
3509 * the io_uring can be safely unregistered on process exit, even if we have
3510 * loops in the file referencing.
3511 */
3512static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
3513{
3514 struct sock *sk = ctx->ring_sock->sk;
3515 struct scm_fp_list *fpl;
3516 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06003517 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07003518
3519 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
3520 unsigned long inflight = ctx->user->unix_inflight + nr;
3521
3522 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
3523 return -EMFILE;
3524 }
3525
3526 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
3527 if (!fpl)
3528 return -ENOMEM;
3529
3530 skb = alloc_skb(0, GFP_KERNEL);
3531 if (!skb) {
3532 kfree(fpl);
3533 return -ENOMEM;
3534 }
3535
3536 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07003537
Jens Axboe08a45172019-10-03 08:11:03 -06003538 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07003539 fpl->user = get_uid(ctx->user);
3540 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003541 struct file *file = io_file_from_index(ctx, i + offset);
3542
3543 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06003544 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06003545 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06003546 unix_inflight(fpl->user, fpl->fp[nr_files]);
3547 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07003548 }
3549
Jens Axboe08a45172019-10-03 08:11:03 -06003550 if (nr_files) {
3551 fpl->max = SCM_MAX_FD;
3552 fpl->count = nr_files;
3553 UNIXCB(skb).fp = fpl;
3554 skb->destructor = io_destruct_skb;
3555 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
3556 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07003557
Jens Axboe08a45172019-10-03 08:11:03 -06003558 for (i = 0; i < nr_files; i++)
3559 fput(fpl->fp[i]);
3560 } else {
3561 kfree_skb(skb);
3562 kfree(fpl);
3563 }
Jens Axboe6b063142019-01-10 22:13:58 -07003564
3565 return 0;
3566}
3567
3568/*
3569 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
3570 * causes regular reference counting to break down. We rely on the UNIX
3571 * garbage collection to take care of this problem for us.
3572 */
3573static int io_sqe_files_scm(struct io_ring_ctx *ctx)
3574{
3575 unsigned left, total;
3576 int ret = 0;
3577
3578 total = 0;
3579 left = ctx->nr_user_files;
3580 while (left) {
3581 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07003582
3583 ret = __io_sqe_files_scm(ctx, this_files, total);
3584 if (ret)
3585 break;
3586 left -= this_files;
3587 total += this_files;
3588 }
3589
3590 if (!ret)
3591 return 0;
3592
3593 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003594 struct file *file = io_file_from_index(ctx, total);
3595
3596 if (file)
3597 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07003598 total++;
3599 }
3600
3601 return ret;
3602}
3603#else
3604static int io_sqe_files_scm(struct io_ring_ctx *ctx)
3605{
3606 return 0;
3607}
3608#endif
3609
Jens Axboe65e19f52019-10-26 07:20:21 -06003610static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
3611 unsigned nr_files)
3612{
3613 int i;
3614
3615 for (i = 0; i < nr_tables; i++) {
3616 struct fixed_file_table *table = &ctx->file_table[i];
3617 unsigned this_files;
3618
3619 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
3620 table->files = kcalloc(this_files, sizeof(struct file *),
3621 GFP_KERNEL);
3622 if (!table->files)
3623 break;
3624 nr_files -= this_files;
3625 }
3626
3627 if (i == nr_tables)
3628 return 0;
3629
3630 for (i = 0; i < nr_tables; i++) {
3631 struct fixed_file_table *table = &ctx->file_table[i];
3632 kfree(table->files);
3633 }
3634 return 1;
3635}
3636
Jens Axboe6b063142019-01-10 22:13:58 -07003637static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
3638 unsigned nr_args)
3639{
3640 __s32 __user *fds = (__s32 __user *) arg;
Jens Axboe65e19f52019-10-26 07:20:21 -06003641 unsigned nr_tables;
Jens Axboe6b063142019-01-10 22:13:58 -07003642 int fd, ret = 0;
3643 unsigned i;
3644
Jens Axboe65e19f52019-10-26 07:20:21 -06003645 if (ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003646 return -EBUSY;
3647 if (!nr_args)
3648 return -EINVAL;
3649 if (nr_args > IORING_MAX_FIXED_FILES)
3650 return -EMFILE;
3651
Jens Axboe65e19f52019-10-26 07:20:21 -06003652 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
3653 ctx->file_table = kcalloc(nr_tables, sizeof(struct fixed_file_table),
3654 GFP_KERNEL);
3655 if (!ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003656 return -ENOMEM;
3657
Jens Axboe65e19f52019-10-26 07:20:21 -06003658 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
3659 kfree(ctx->file_table);
Jens Axboe46568e92019-11-10 08:40:53 -07003660 ctx->file_table = NULL;
Jens Axboe65e19f52019-10-26 07:20:21 -06003661 return -ENOMEM;
3662 }
3663
Jens Axboe08a45172019-10-03 08:11:03 -06003664 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003665 struct fixed_file_table *table;
3666 unsigned index;
3667
Jens Axboe6b063142019-01-10 22:13:58 -07003668 ret = -EFAULT;
3669 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
3670 break;
Jens Axboe08a45172019-10-03 08:11:03 -06003671 /* allow sparse sets */
3672 if (fd == -1) {
3673 ret = 0;
3674 continue;
3675 }
Jens Axboe6b063142019-01-10 22:13:58 -07003676
Jens Axboe65e19f52019-10-26 07:20:21 -06003677 table = &ctx->file_table[i >> IORING_FILE_TABLE_SHIFT];
3678 index = i & IORING_FILE_TABLE_MASK;
3679 table->files[index] = fget(fd);
Jens Axboe6b063142019-01-10 22:13:58 -07003680
3681 ret = -EBADF;
Jens Axboe65e19f52019-10-26 07:20:21 -06003682 if (!table->files[index])
Jens Axboe6b063142019-01-10 22:13:58 -07003683 break;
3684 /*
3685 * Don't allow io_uring instances to be registered. If UNIX
3686 * isn't enabled, then this causes a reference cycle and this
3687 * instance can never get freed. If UNIX is enabled we'll
3688 * handle it just fine, but there's still no point in allowing
3689 * a ring fd as it doesn't support regular read/write anyway.
3690 */
Jens Axboe65e19f52019-10-26 07:20:21 -06003691 if (table->files[index]->f_op == &io_uring_fops) {
3692 fput(table->files[index]);
Jens Axboe6b063142019-01-10 22:13:58 -07003693 break;
3694 }
Jens Axboe6b063142019-01-10 22:13:58 -07003695 ret = 0;
3696 }
3697
3698 if (ret) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003699 for (i = 0; i < ctx->nr_user_files; i++) {
3700 struct file *file;
Jens Axboe6b063142019-01-10 22:13:58 -07003701
Jens Axboe65e19f52019-10-26 07:20:21 -06003702 file = io_file_from_index(ctx, i);
3703 if (file)
3704 fput(file);
3705 }
3706 for (i = 0; i < nr_tables; i++)
3707 kfree(ctx->file_table[i].files);
3708
3709 kfree(ctx->file_table);
3710 ctx->file_table = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003711 ctx->nr_user_files = 0;
3712 return ret;
3713 }
3714
3715 ret = io_sqe_files_scm(ctx);
3716 if (ret)
3717 io_sqe_files_unregister(ctx);
3718
3719 return ret;
3720}
3721
Jens Axboec3a31e62019-10-03 13:59:56 -06003722static void io_sqe_file_unregister(struct io_ring_ctx *ctx, int index)
3723{
3724#if defined(CONFIG_UNIX)
Jens Axboe65e19f52019-10-26 07:20:21 -06003725 struct file *file = io_file_from_index(ctx, index);
Jens Axboec3a31e62019-10-03 13:59:56 -06003726 struct sock *sock = ctx->ring_sock->sk;
3727 struct sk_buff_head list, *head = &sock->sk_receive_queue;
3728 struct sk_buff *skb;
3729 int i;
3730
3731 __skb_queue_head_init(&list);
3732
3733 /*
3734 * Find the skb that holds this file in its SCM_RIGHTS. When found,
3735 * remove this entry and rearrange the file array.
3736 */
3737 skb = skb_dequeue(head);
3738 while (skb) {
3739 struct scm_fp_list *fp;
3740
3741 fp = UNIXCB(skb).fp;
3742 for (i = 0; i < fp->count; i++) {
3743 int left;
3744
3745 if (fp->fp[i] != file)
3746 continue;
3747
3748 unix_notinflight(fp->user, fp->fp[i]);
3749 left = fp->count - 1 - i;
3750 if (left) {
3751 memmove(&fp->fp[i], &fp->fp[i + 1],
3752 left * sizeof(struct file *));
3753 }
3754 fp->count--;
3755 if (!fp->count) {
3756 kfree_skb(skb);
3757 skb = NULL;
3758 } else {
3759 __skb_queue_tail(&list, skb);
3760 }
3761 fput(file);
3762 file = NULL;
3763 break;
3764 }
3765
3766 if (!file)
3767 break;
3768
3769 __skb_queue_tail(&list, skb);
3770
3771 skb = skb_dequeue(head);
3772 }
3773
3774 if (skb_peek(&list)) {
3775 spin_lock_irq(&head->lock);
3776 while ((skb = __skb_dequeue(&list)) != NULL)
3777 __skb_queue_tail(head, skb);
3778 spin_unlock_irq(&head->lock);
3779 }
3780#else
Jens Axboe65e19f52019-10-26 07:20:21 -06003781 fput(io_file_from_index(ctx, index));
Jens Axboec3a31e62019-10-03 13:59:56 -06003782#endif
3783}
3784
3785static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
3786 int index)
3787{
3788#if defined(CONFIG_UNIX)
3789 struct sock *sock = ctx->ring_sock->sk;
3790 struct sk_buff_head *head = &sock->sk_receive_queue;
3791 struct sk_buff *skb;
3792
3793 /*
3794 * See if we can merge this file into an existing skb SCM_RIGHTS
3795 * file set. If there's no room, fall back to allocating a new skb
3796 * and filling it in.
3797 */
3798 spin_lock_irq(&head->lock);
3799 skb = skb_peek(head);
3800 if (skb) {
3801 struct scm_fp_list *fpl = UNIXCB(skb).fp;
3802
3803 if (fpl->count < SCM_MAX_FD) {
3804 __skb_unlink(skb, head);
3805 spin_unlock_irq(&head->lock);
3806 fpl->fp[fpl->count] = get_file(file);
3807 unix_inflight(fpl->user, fpl->fp[fpl->count]);
3808 fpl->count++;
3809 spin_lock_irq(&head->lock);
3810 __skb_queue_head(head, skb);
3811 } else {
3812 skb = NULL;
3813 }
3814 }
3815 spin_unlock_irq(&head->lock);
3816
3817 if (skb) {
3818 fput(file);
3819 return 0;
3820 }
3821
3822 return __io_sqe_files_scm(ctx, 1, index);
3823#else
3824 return 0;
3825#endif
3826}
3827
3828static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
3829 unsigned nr_args)
3830{
3831 struct io_uring_files_update up;
3832 __s32 __user *fds;
3833 int fd, i, err;
3834 __u32 done;
3835
Jens Axboe65e19f52019-10-26 07:20:21 -06003836 if (!ctx->file_table)
Jens Axboec3a31e62019-10-03 13:59:56 -06003837 return -ENXIO;
3838 if (!nr_args)
3839 return -EINVAL;
3840 if (copy_from_user(&up, arg, sizeof(up)))
3841 return -EFAULT;
3842 if (check_add_overflow(up.offset, nr_args, &done))
3843 return -EOVERFLOW;
3844 if (done > ctx->nr_user_files)
3845 return -EINVAL;
3846
3847 done = 0;
3848 fds = (__s32 __user *) up.fds;
3849 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003850 struct fixed_file_table *table;
3851 unsigned index;
3852
Jens Axboec3a31e62019-10-03 13:59:56 -06003853 err = 0;
3854 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
3855 err = -EFAULT;
3856 break;
3857 }
3858 i = array_index_nospec(up.offset, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06003859 table = &ctx->file_table[i >> IORING_FILE_TABLE_SHIFT];
3860 index = i & IORING_FILE_TABLE_MASK;
3861 if (table->files[index]) {
Jens Axboec3a31e62019-10-03 13:59:56 -06003862 io_sqe_file_unregister(ctx, i);
Jens Axboe65e19f52019-10-26 07:20:21 -06003863 table->files[index] = NULL;
Jens Axboec3a31e62019-10-03 13:59:56 -06003864 }
3865 if (fd != -1) {
3866 struct file *file;
3867
3868 file = fget(fd);
3869 if (!file) {
3870 err = -EBADF;
3871 break;
3872 }
3873 /*
3874 * Don't allow io_uring instances to be registered. If
3875 * UNIX isn't enabled, then this causes a reference
3876 * cycle and this instance can never get freed. If UNIX
3877 * is enabled we'll handle it just fine, but there's
3878 * still no point in allowing a ring fd as it doesn't
3879 * support regular read/write anyway.
3880 */
3881 if (file->f_op == &io_uring_fops) {
3882 fput(file);
3883 err = -EBADF;
3884 break;
3885 }
Jens Axboe65e19f52019-10-26 07:20:21 -06003886 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06003887 err = io_sqe_file_register(ctx, file, i);
3888 if (err)
3889 break;
3890 }
3891 nr_args--;
3892 done++;
3893 up.offset++;
3894 }
3895
3896 return done ? done : err;
3897}
3898
Jens Axboe7d723062019-11-12 22:31:31 -07003899static void io_put_work(struct io_wq_work *work)
3900{
3901 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
3902
3903 io_put_req(req);
3904}
3905
3906static void io_get_work(struct io_wq_work *work)
3907{
3908 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
3909
3910 refcount_inc(&req->refs);
3911}
3912
Jens Axboe6c271ce2019-01-10 11:22:30 -07003913static int io_sq_offload_start(struct io_ring_ctx *ctx,
3914 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003915{
Jens Axboe561fb042019-10-24 07:25:42 -06003916 unsigned concurrency;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003917 int ret;
3918
Jens Axboe6c271ce2019-01-10 11:22:30 -07003919 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003920 mmgrab(current->mm);
3921 ctx->sqo_mm = current->mm;
3922
Jens Axboe6c271ce2019-01-10 11:22:30 -07003923 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06003924 ret = -EPERM;
3925 if (!capable(CAP_SYS_ADMIN))
3926 goto err;
3927
Jens Axboe917257d2019-04-13 09:28:55 -06003928 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
3929 if (!ctx->sq_thread_idle)
3930 ctx->sq_thread_idle = HZ;
3931
Jens Axboe6c271ce2019-01-10 11:22:30 -07003932 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06003933 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003934
Jens Axboe917257d2019-04-13 09:28:55 -06003935 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06003936 if (cpu >= nr_cpu_ids)
3937 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08003938 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06003939 goto err;
3940
Jens Axboe6c271ce2019-01-10 11:22:30 -07003941 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
3942 ctx, cpu,
3943 "io_uring-sq");
3944 } else {
3945 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
3946 "io_uring-sq");
3947 }
3948 if (IS_ERR(ctx->sqo_thread)) {
3949 ret = PTR_ERR(ctx->sqo_thread);
3950 ctx->sqo_thread = NULL;
3951 goto err;
3952 }
3953 wake_up_process(ctx->sqo_thread);
3954 } else if (p->flags & IORING_SETUP_SQ_AFF) {
3955 /* Can't have SQ_AFF without SQPOLL */
3956 ret = -EINVAL;
3957 goto err;
3958 }
3959
Jens Axboe561fb042019-10-24 07:25:42 -06003960 /* Do QD, or 4 * CPUS, whatever is smallest */
3961 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
Jens Axboe7d723062019-11-12 22:31:31 -07003962 ctx->io_wq = io_wq_create(concurrency, ctx->sqo_mm, ctx->user,
3963 io_get_work, io_put_work);
Jens Axboe975c99a52019-10-30 08:42:56 -06003964 if (IS_ERR(ctx->io_wq)) {
3965 ret = PTR_ERR(ctx->io_wq);
3966 ctx->io_wq = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003967 goto err;
3968 }
3969
3970 return 0;
3971err:
Jens Axboe54a91f32019-09-10 09:15:04 -06003972 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003973 mmdrop(ctx->sqo_mm);
3974 ctx->sqo_mm = NULL;
3975 return ret;
3976}
3977
3978static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
3979{
3980 atomic_long_sub(nr_pages, &user->locked_vm);
3981}
3982
3983static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
3984{
3985 unsigned long page_limit, cur_pages, new_pages;
3986
3987 /* Don't allow more pages than we can safely lock */
3988 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
3989
3990 do {
3991 cur_pages = atomic_long_read(&user->locked_vm);
3992 new_pages = cur_pages + nr_pages;
3993 if (new_pages > page_limit)
3994 return -ENOMEM;
3995 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
3996 new_pages) != cur_pages);
3997
3998 return 0;
3999}
4000
4001static void io_mem_free(void *ptr)
4002{
Mark Rutland52e04ef2019-04-30 17:30:21 +01004003 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004004
Mark Rutland52e04ef2019-04-30 17:30:21 +01004005 if (!ptr)
4006 return;
4007
4008 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004009 if (put_page_testzero(page))
4010 free_compound_page(page);
4011}
4012
4013static void *io_mem_alloc(size_t size)
4014{
4015 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
4016 __GFP_NORETRY;
4017
4018 return (void *) __get_free_pages(gfp_flags, get_order(size));
4019}
4020
Hristo Venev75b28af2019-08-26 17:23:46 +00004021static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
4022 size_t *sq_offset)
4023{
4024 struct io_rings *rings;
4025 size_t off, sq_array_size;
4026
4027 off = struct_size(rings, cqes, cq_entries);
4028 if (off == SIZE_MAX)
4029 return SIZE_MAX;
4030
4031#ifdef CONFIG_SMP
4032 off = ALIGN(off, SMP_CACHE_BYTES);
4033 if (off == 0)
4034 return SIZE_MAX;
4035#endif
4036
4037 sq_array_size = array_size(sizeof(u32), sq_entries);
4038 if (sq_array_size == SIZE_MAX)
4039 return SIZE_MAX;
4040
4041 if (check_add_overflow(off, sq_array_size, &off))
4042 return SIZE_MAX;
4043
4044 if (sq_offset)
4045 *sq_offset = off;
4046
4047 return off;
4048}
4049
Jens Axboe2b188cc2019-01-07 10:46:33 -07004050static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
4051{
Hristo Venev75b28af2019-08-26 17:23:46 +00004052 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004053
Hristo Venev75b28af2019-08-26 17:23:46 +00004054 pages = (size_t)1 << get_order(
4055 rings_size(sq_entries, cq_entries, NULL));
4056 pages += (size_t)1 << get_order(
4057 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07004058
Hristo Venev75b28af2019-08-26 17:23:46 +00004059 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004060}
4061
Jens Axboeedafcce2019-01-09 09:16:05 -07004062static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
4063{
4064 int i, j;
4065
4066 if (!ctx->user_bufs)
4067 return -ENXIO;
4068
4069 for (i = 0; i < ctx->nr_user_bufs; i++) {
4070 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
4071
4072 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbard27c4d3a2019-08-04 19:32:06 -07004073 put_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07004074
4075 if (ctx->account_mem)
4076 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004077 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07004078 imu->nr_bvecs = 0;
4079 }
4080
4081 kfree(ctx->user_bufs);
4082 ctx->user_bufs = NULL;
4083 ctx->nr_user_bufs = 0;
4084 return 0;
4085}
4086
4087static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
4088 void __user *arg, unsigned index)
4089{
4090 struct iovec __user *src;
4091
4092#ifdef CONFIG_COMPAT
4093 if (ctx->compat) {
4094 struct compat_iovec __user *ciovs;
4095 struct compat_iovec ciov;
4096
4097 ciovs = (struct compat_iovec __user *) arg;
4098 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
4099 return -EFAULT;
4100
4101 dst->iov_base = (void __user *) (unsigned long) ciov.iov_base;
4102 dst->iov_len = ciov.iov_len;
4103 return 0;
4104 }
4105#endif
4106 src = (struct iovec __user *) arg;
4107 if (copy_from_user(dst, &src[index], sizeof(*dst)))
4108 return -EFAULT;
4109 return 0;
4110}
4111
4112static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
4113 unsigned nr_args)
4114{
4115 struct vm_area_struct **vmas = NULL;
4116 struct page **pages = NULL;
4117 int i, j, got_pages = 0;
4118 int ret = -EINVAL;
4119
4120 if (ctx->user_bufs)
4121 return -EBUSY;
4122 if (!nr_args || nr_args > UIO_MAXIOV)
4123 return -EINVAL;
4124
4125 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
4126 GFP_KERNEL);
4127 if (!ctx->user_bufs)
4128 return -ENOMEM;
4129
4130 for (i = 0; i < nr_args; i++) {
4131 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
4132 unsigned long off, start, end, ubuf;
4133 int pret, nr_pages;
4134 struct iovec iov;
4135 size_t size;
4136
4137 ret = io_copy_iov(ctx, &iov, arg, i);
4138 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03004139 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07004140
4141 /*
4142 * Don't impose further limits on the size and buffer
4143 * constraints here, we'll -EINVAL later when IO is
4144 * submitted if they are wrong.
4145 */
4146 ret = -EFAULT;
4147 if (!iov.iov_base || !iov.iov_len)
4148 goto err;
4149
4150 /* arbitrary limit, but we need something */
4151 if (iov.iov_len > SZ_1G)
4152 goto err;
4153
4154 ubuf = (unsigned long) iov.iov_base;
4155 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
4156 start = ubuf >> PAGE_SHIFT;
4157 nr_pages = end - start;
4158
4159 if (ctx->account_mem) {
4160 ret = io_account_mem(ctx->user, nr_pages);
4161 if (ret)
4162 goto err;
4163 }
4164
4165 ret = 0;
4166 if (!pages || nr_pages > got_pages) {
4167 kfree(vmas);
4168 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004169 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07004170 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004171 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07004172 sizeof(struct vm_area_struct *),
4173 GFP_KERNEL);
4174 if (!pages || !vmas) {
4175 ret = -ENOMEM;
4176 if (ctx->account_mem)
4177 io_unaccount_mem(ctx->user, nr_pages);
4178 goto err;
4179 }
4180 got_pages = nr_pages;
4181 }
4182
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004183 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07004184 GFP_KERNEL);
4185 ret = -ENOMEM;
4186 if (!imu->bvec) {
4187 if (ctx->account_mem)
4188 io_unaccount_mem(ctx->user, nr_pages);
4189 goto err;
4190 }
4191
4192 ret = 0;
4193 down_read(&current->mm->mmap_sem);
Ira Weiny932f4a62019-05-13 17:17:03 -07004194 pret = get_user_pages(ubuf, nr_pages,
4195 FOLL_WRITE | FOLL_LONGTERM,
4196 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07004197 if (pret == nr_pages) {
4198 /* don't support file backed memory */
4199 for (j = 0; j < nr_pages; j++) {
4200 struct vm_area_struct *vma = vmas[j];
4201
4202 if (vma->vm_file &&
4203 !is_file_hugepages(vma->vm_file)) {
4204 ret = -EOPNOTSUPP;
4205 break;
4206 }
4207 }
4208 } else {
4209 ret = pret < 0 ? pret : -EFAULT;
4210 }
4211 up_read(&current->mm->mmap_sem);
4212 if (ret) {
4213 /*
4214 * if we did partial map, or found file backed vmas,
4215 * release any pages we did get
4216 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07004217 if (pret > 0)
4218 put_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07004219 if (ctx->account_mem)
4220 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004221 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07004222 goto err;
4223 }
4224
4225 off = ubuf & ~PAGE_MASK;
4226 size = iov.iov_len;
4227 for (j = 0; j < nr_pages; j++) {
4228 size_t vec_len;
4229
4230 vec_len = min_t(size_t, size, PAGE_SIZE - off);
4231 imu->bvec[j].bv_page = pages[j];
4232 imu->bvec[j].bv_len = vec_len;
4233 imu->bvec[j].bv_offset = off;
4234 off = 0;
4235 size -= vec_len;
4236 }
4237 /* store original address for later verification */
4238 imu->ubuf = ubuf;
4239 imu->len = iov.iov_len;
4240 imu->nr_bvecs = nr_pages;
4241
4242 ctx->nr_user_bufs++;
4243 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004244 kvfree(pages);
4245 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07004246 return 0;
4247err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004248 kvfree(pages);
4249 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07004250 io_sqe_buffer_unregister(ctx);
4251 return ret;
4252}
4253
Jens Axboe9b402842019-04-11 11:45:41 -06004254static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
4255{
4256 __s32 __user *fds = arg;
4257 int fd;
4258
4259 if (ctx->cq_ev_fd)
4260 return -EBUSY;
4261
4262 if (copy_from_user(&fd, fds, sizeof(*fds)))
4263 return -EFAULT;
4264
4265 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
4266 if (IS_ERR(ctx->cq_ev_fd)) {
4267 int ret = PTR_ERR(ctx->cq_ev_fd);
4268 ctx->cq_ev_fd = NULL;
4269 return ret;
4270 }
4271
4272 return 0;
4273}
4274
4275static int io_eventfd_unregister(struct io_ring_ctx *ctx)
4276{
4277 if (ctx->cq_ev_fd) {
4278 eventfd_ctx_put(ctx->cq_ev_fd);
4279 ctx->cq_ev_fd = NULL;
4280 return 0;
4281 }
4282
4283 return -ENXIO;
4284}
4285
Jens Axboe2b188cc2019-01-07 10:46:33 -07004286static void io_ring_ctx_free(struct io_ring_ctx *ctx)
4287{
Jens Axboe6b063142019-01-10 22:13:58 -07004288 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004289 if (ctx->sqo_mm)
4290 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07004291
4292 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07004293 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07004294 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06004295 io_eventfd_unregister(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07004296
Jens Axboe2b188cc2019-01-07 10:46:33 -07004297#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07004298 if (ctx->ring_sock) {
4299 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004300 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07004301 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004302#endif
4303
Hristo Venev75b28af2019-08-26 17:23:46 +00004304 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004305 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004306
4307 percpu_ref_exit(&ctx->refs);
4308 if (ctx->account_mem)
4309 io_unaccount_mem(ctx->user,
4310 ring_pages(ctx->sq_entries, ctx->cq_entries));
4311 free_uid(ctx->user);
Jens Axboe206aefd2019-11-07 18:27:42 -07004312 kfree(ctx->completions);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07004313 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004314 kfree(ctx);
4315}
4316
4317static __poll_t io_uring_poll(struct file *file, poll_table *wait)
4318{
4319 struct io_ring_ctx *ctx = file->private_data;
4320 __poll_t mask = 0;
4321
4322 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02004323 /*
4324 * synchronizes with barrier from wq_has_sleeper call in
4325 * io_commit_cqring
4326 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004327 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00004328 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
4329 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004330 mask |= EPOLLOUT | EPOLLWRNORM;
yangerkundaa5de52019-09-24 20:53:34 +08004331 if (READ_ONCE(ctx->rings->cq.head) != ctx->cached_cq_tail)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004332 mask |= EPOLLIN | EPOLLRDNORM;
4333
4334 return mask;
4335}
4336
4337static int io_uring_fasync(int fd, struct file *file, int on)
4338{
4339 struct io_ring_ctx *ctx = file->private_data;
4340
4341 return fasync_helper(fd, file, on, &ctx->cq_fasync);
4342}
4343
4344static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
4345{
4346 mutex_lock(&ctx->uring_lock);
4347 percpu_ref_kill(&ctx->refs);
4348 mutex_unlock(&ctx->uring_lock);
4349
Jens Axboe5262f562019-09-17 12:26:57 -06004350 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004351 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06004352
4353 if (ctx->io_wq)
4354 io_wq_cancel_all(ctx->io_wq);
4355
Jens Axboedef596e2019-01-09 08:59:42 -07004356 io_iopoll_reap_events(ctx);
Jens Axboe15dff282019-11-13 09:09:23 -07004357 /* if we failed setting up the ctx, we might not have any rings */
4358 if (ctx->rings)
4359 io_cqring_overflow_flush(ctx, true);
Jens Axboe206aefd2019-11-07 18:27:42 -07004360 wait_for_completion(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004361 io_ring_ctx_free(ctx);
4362}
4363
4364static int io_uring_release(struct inode *inode, struct file *file)
4365{
4366 struct io_ring_ctx *ctx = file->private_data;
4367
4368 file->private_data = NULL;
4369 io_ring_ctx_wait_and_kill(ctx);
4370 return 0;
4371}
4372
Jens Axboefcb323c2019-10-24 12:39:47 -06004373static void io_uring_cancel_files(struct io_ring_ctx *ctx,
4374 struct files_struct *files)
4375{
4376 struct io_kiocb *req;
4377 DEFINE_WAIT(wait);
4378
4379 while (!list_empty_careful(&ctx->inflight_list)) {
Jens Axboe768134d2019-11-10 20:30:53 -07004380 struct io_kiocb *cancel_req = NULL;
Jens Axboefcb323c2019-10-24 12:39:47 -06004381
4382 spin_lock_irq(&ctx->inflight_lock);
4383 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07004384 if (req->work.files != files)
4385 continue;
4386 /* req is being completed, ignore */
4387 if (!refcount_inc_not_zero(&req->refs))
4388 continue;
4389 cancel_req = req;
4390 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06004391 }
Jens Axboe768134d2019-11-10 20:30:53 -07004392 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06004393 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07004394 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06004395 spin_unlock_irq(&ctx->inflight_lock);
4396
Jens Axboe768134d2019-11-10 20:30:53 -07004397 /* We need to keep going until we don't find a matching req */
4398 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06004399 break;
Bob Liu2f6d9b92019-11-13 18:06:24 +08004400
4401 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
4402 io_put_req(cancel_req);
Jens Axboefcb323c2019-10-24 12:39:47 -06004403 schedule();
4404 }
Jens Axboe768134d2019-11-10 20:30:53 -07004405 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06004406}
4407
4408static int io_uring_flush(struct file *file, void *data)
4409{
4410 struct io_ring_ctx *ctx = file->private_data;
4411
4412 io_uring_cancel_files(ctx, data);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004413 if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) {
4414 io_cqring_overflow_flush(ctx, true);
Jens Axboefcb323c2019-10-24 12:39:47 -06004415 io_wq_cancel_all(ctx->io_wq);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004416 }
Jens Axboefcb323c2019-10-24 12:39:47 -06004417 return 0;
4418}
4419
Jens Axboe2b188cc2019-01-07 10:46:33 -07004420static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
4421{
4422 loff_t offset = (loff_t) vma->vm_pgoff << PAGE_SHIFT;
4423 unsigned long sz = vma->vm_end - vma->vm_start;
4424 struct io_ring_ctx *ctx = file->private_data;
4425 unsigned long pfn;
4426 struct page *page;
4427 void *ptr;
4428
4429 switch (offset) {
4430 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00004431 case IORING_OFF_CQ_RING:
4432 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004433 break;
4434 case IORING_OFF_SQES:
4435 ptr = ctx->sq_sqes;
4436 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004437 default:
4438 return -EINVAL;
4439 }
4440
4441 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07004442 if (sz > page_size(page))
Jens Axboe2b188cc2019-01-07 10:46:33 -07004443 return -EINVAL;
4444
4445 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
4446 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
4447}
4448
4449SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
4450 u32, min_complete, u32, flags, const sigset_t __user *, sig,
4451 size_t, sigsz)
4452{
4453 struct io_ring_ctx *ctx;
4454 long ret = -EBADF;
4455 int submitted = 0;
4456 struct fd f;
4457
Jens Axboe6c271ce2019-01-10 11:22:30 -07004458 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07004459 return -EINVAL;
4460
4461 f = fdget(fd);
4462 if (!f.file)
4463 return -EBADF;
4464
4465 ret = -EOPNOTSUPP;
4466 if (f.file->f_op != &io_uring_fops)
4467 goto out_fput;
4468
4469 ret = -ENXIO;
4470 ctx = f.file->private_data;
4471 if (!percpu_ref_tryget(&ctx->refs))
4472 goto out_fput;
4473
Jens Axboe6c271ce2019-01-10 11:22:30 -07004474 /*
4475 * For SQ polling, the thread will do all submissions and completions.
4476 * Just return the requested submit count, and wake the thread if
4477 * we were asked to.
4478 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06004479 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004480 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07004481 if (!list_empty_careful(&ctx->cq_overflow_list))
4482 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004483 if (flags & IORING_ENTER_SQ_WAKEUP)
4484 wake_up(&ctx->sqo_wait);
4485 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06004486 } else if (to_submit) {
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004487 struct mm_struct *cur_mm;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004488
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004489 to_submit = min(to_submit, ctx->sq_entries);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004490 mutex_lock(&ctx->uring_lock);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004491 /* already have mm, so io_submit_sqes() won't try to grab it */
4492 cur_mm = ctx->sqo_mm;
4493 submitted = io_submit_sqes(ctx, to_submit, f.file, fd,
4494 &cur_mm, false);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004495 mutex_unlock(&ctx->uring_lock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004496 }
4497 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07004498 unsigned nr_events = 0;
4499
Jens Axboe2b188cc2019-01-07 10:46:33 -07004500 min_complete = min(min_complete, ctx->cq_entries);
4501
Jens Axboedef596e2019-01-09 08:59:42 -07004502 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07004503 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07004504 } else {
4505 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
4506 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004507 }
4508
Pavel Begunkov6805b322019-10-08 02:18:42 +03004509 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004510out_fput:
4511 fdput(f);
4512 return submitted ? submitted : ret;
4513}
4514
4515static const struct file_operations io_uring_fops = {
4516 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06004517 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07004518 .mmap = io_uring_mmap,
4519 .poll = io_uring_poll,
4520 .fasync = io_uring_fasync,
4521};
4522
4523static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
4524 struct io_uring_params *p)
4525{
Hristo Venev75b28af2019-08-26 17:23:46 +00004526 struct io_rings *rings;
4527 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004528
Hristo Venev75b28af2019-08-26 17:23:46 +00004529 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
4530 if (size == SIZE_MAX)
4531 return -EOVERFLOW;
4532
4533 rings = io_mem_alloc(size);
4534 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004535 return -ENOMEM;
4536
Hristo Venev75b28af2019-08-26 17:23:46 +00004537 ctx->rings = rings;
4538 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
4539 rings->sq_ring_mask = p->sq_entries - 1;
4540 rings->cq_ring_mask = p->cq_entries - 1;
4541 rings->sq_ring_entries = p->sq_entries;
4542 rings->cq_ring_entries = p->cq_entries;
4543 ctx->sq_mask = rings->sq_ring_mask;
4544 ctx->cq_mask = rings->cq_ring_mask;
4545 ctx->sq_entries = rings->sq_ring_entries;
4546 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004547
4548 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07004549 if (size == SIZE_MAX) {
4550 io_mem_free(ctx->rings);
4551 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004552 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07004553 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004554
4555 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07004556 if (!ctx->sq_sqes) {
4557 io_mem_free(ctx->rings);
4558 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004559 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07004560 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004561
Jens Axboe2b188cc2019-01-07 10:46:33 -07004562 return 0;
4563}
4564
4565/*
4566 * Allocate an anonymous fd, this is what constitutes the application
4567 * visible backing of an io_uring instance. The application mmaps this
4568 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
4569 * we have to tie this fd to a socket for file garbage collection purposes.
4570 */
4571static int io_uring_get_fd(struct io_ring_ctx *ctx)
4572{
4573 struct file *file;
4574 int ret;
4575
4576#if defined(CONFIG_UNIX)
4577 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
4578 &ctx->ring_sock);
4579 if (ret)
4580 return ret;
4581#endif
4582
4583 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
4584 if (ret < 0)
4585 goto err;
4586
4587 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
4588 O_RDWR | O_CLOEXEC);
4589 if (IS_ERR(file)) {
4590 put_unused_fd(ret);
4591 ret = PTR_ERR(file);
4592 goto err;
4593 }
4594
4595#if defined(CONFIG_UNIX)
4596 ctx->ring_sock->file = file;
Jens Axboe6b063142019-01-10 22:13:58 -07004597 ctx->ring_sock->sk->sk_user_data = ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004598#endif
4599 fd_install(ret, file);
4600 return ret;
4601err:
4602#if defined(CONFIG_UNIX)
4603 sock_release(ctx->ring_sock);
4604 ctx->ring_sock = NULL;
4605#endif
4606 return ret;
4607}
4608
4609static int io_uring_create(unsigned entries, struct io_uring_params *p)
4610{
4611 struct user_struct *user = NULL;
4612 struct io_ring_ctx *ctx;
4613 bool account_mem;
4614 int ret;
4615
4616 if (!entries || entries > IORING_MAX_ENTRIES)
4617 return -EINVAL;
4618
4619 /*
4620 * Use twice as many entries for the CQ ring. It's possible for the
4621 * application to drive a higher depth than the size of the SQ ring,
4622 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06004623 * some flexibility in overcommitting a bit. If the application has
4624 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
4625 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07004626 */
4627 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06004628 if (p->flags & IORING_SETUP_CQSIZE) {
4629 /*
4630 * If IORING_SETUP_CQSIZE is set, we do the same roundup
4631 * to a power-of-two, if it isn't already. We do NOT impose
4632 * any cq vs sq ring sizing.
4633 */
4634 if (p->cq_entries < p->sq_entries || p->cq_entries > IORING_MAX_CQ_ENTRIES)
4635 return -EINVAL;
4636 p->cq_entries = roundup_pow_of_two(p->cq_entries);
4637 } else {
4638 p->cq_entries = 2 * p->sq_entries;
4639 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004640
4641 user = get_uid(current_user());
4642 account_mem = !capable(CAP_IPC_LOCK);
4643
4644 if (account_mem) {
4645 ret = io_account_mem(user,
4646 ring_pages(p->sq_entries, p->cq_entries));
4647 if (ret) {
4648 free_uid(user);
4649 return ret;
4650 }
4651 }
4652
4653 ctx = io_ring_ctx_alloc(p);
4654 if (!ctx) {
4655 if (account_mem)
4656 io_unaccount_mem(user, ring_pages(p->sq_entries,
4657 p->cq_entries));
4658 free_uid(user);
4659 return -ENOMEM;
4660 }
4661 ctx->compat = in_compat_syscall();
4662 ctx->account_mem = account_mem;
4663 ctx->user = user;
4664
4665 ret = io_allocate_scq_urings(ctx, p);
4666 if (ret)
4667 goto err;
4668
Jens Axboe6c271ce2019-01-10 11:22:30 -07004669 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004670 if (ret)
4671 goto err;
4672
Jens Axboe2b188cc2019-01-07 10:46:33 -07004673 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00004674 p->sq_off.head = offsetof(struct io_rings, sq.head);
4675 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
4676 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
4677 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
4678 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
4679 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
4680 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004681
4682 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00004683 p->cq_off.head = offsetof(struct io_rings, cq.head);
4684 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
4685 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
4686 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
4687 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
4688 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06004689
Jens Axboe044c1ab2019-10-28 09:15:33 -06004690 /*
4691 * Install ring fd as the very last thing, so we don't risk someone
4692 * having closed it before we finish setup
4693 */
4694 ret = io_uring_get_fd(ctx);
4695 if (ret < 0)
4696 goto err;
4697
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004698 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004699 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004700 return ret;
4701err:
4702 io_ring_ctx_wait_and_kill(ctx);
4703 return ret;
4704}
4705
4706/*
4707 * Sets up an aio uring context, and returns the fd. Applications asks for a
4708 * ring size, we return the actual sq/cq ring sizes (among other things) in the
4709 * params structure passed in.
4710 */
4711static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
4712{
4713 struct io_uring_params p;
4714 long ret;
4715 int i;
4716
4717 if (copy_from_user(&p, params, sizeof(p)))
4718 return -EFAULT;
4719 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
4720 if (p.resv[i])
4721 return -EINVAL;
4722 }
4723
Jens Axboe6c271ce2019-01-10 11:22:30 -07004724 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe33a107f2019-10-04 12:10:03 -06004725 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE))
Jens Axboe2b188cc2019-01-07 10:46:33 -07004726 return -EINVAL;
4727
4728 ret = io_uring_create(entries, &p);
4729 if (ret < 0)
4730 return ret;
4731
4732 if (copy_to_user(params, &p, sizeof(p)))
4733 return -EFAULT;
4734
4735 return ret;
4736}
4737
4738SYSCALL_DEFINE2(io_uring_setup, u32, entries,
4739 struct io_uring_params __user *, params)
4740{
4741 return io_uring_setup(entries, params);
4742}
4743
Jens Axboeedafcce2019-01-09 09:16:05 -07004744static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
4745 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06004746 __releases(ctx->uring_lock)
4747 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07004748{
4749 int ret;
4750
Jens Axboe35fa71a2019-04-22 10:23:23 -06004751 /*
4752 * We're inside the ring mutex, if the ref is already dying, then
4753 * someone else killed the ctx or is already going through
4754 * io_uring_register().
4755 */
4756 if (percpu_ref_is_dying(&ctx->refs))
4757 return -ENXIO;
4758
Jens Axboeedafcce2019-01-09 09:16:05 -07004759 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06004760
4761 /*
4762 * Drop uring mutex before waiting for references to exit. If another
4763 * thread is currently inside io_uring_enter() it might need to grab
4764 * the uring_lock to make progress. If we hold it here across the drain
4765 * wait, then we can deadlock. It's safe to drop the mutex here, since
4766 * no new references will come in after we've killed the percpu ref.
4767 */
4768 mutex_unlock(&ctx->uring_lock);
Jens Axboe206aefd2019-11-07 18:27:42 -07004769 wait_for_completion(&ctx->completions[0]);
Jens Axboeb19062a2019-04-15 10:49:38 -06004770 mutex_lock(&ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07004771
4772 switch (opcode) {
4773 case IORING_REGISTER_BUFFERS:
4774 ret = io_sqe_buffer_register(ctx, arg, nr_args);
4775 break;
4776 case IORING_UNREGISTER_BUFFERS:
4777 ret = -EINVAL;
4778 if (arg || nr_args)
4779 break;
4780 ret = io_sqe_buffer_unregister(ctx);
4781 break;
Jens Axboe6b063142019-01-10 22:13:58 -07004782 case IORING_REGISTER_FILES:
4783 ret = io_sqe_files_register(ctx, arg, nr_args);
4784 break;
4785 case IORING_UNREGISTER_FILES:
4786 ret = -EINVAL;
4787 if (arg || nr_args)
4788 break;
4789 ret = io_sqe_files_unregister(ctx);
4790 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06004791 case IORING_REGISTER_FILES_UPDATE:
4792 ret = io_sqe_files_update(ctx, arg, nr_args);
4793 break;
Jens Axboe9b402842019-04-11 11:45:41 -06004794 case IORING_REGISTER_EVENTFD:
4795 ret = -EINVAL;
4796 if (nr_args != 1)
4797 break;
4798 ret = io_eventfd_register(ctx, arg);
4799 break;
4800 case IORING_UNREGISTER_EVENTFD:
4801 ret = -EINVAL;
4802 if (arg || nr_args)
4803 break;
4804 ret = io_eventfd_unregister(ctx);
4805 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07004806 default:
4807 ret = -EINVAL;
4808 break;
4809 }
4810
4811 /* bring the ctx back to life */
Jens Axboe206aefd2019-11-07 18:27:42 -07004812 reinit_completion(&ctx->completions[0]);
Jens Axboeedafcce2019-01-09 09:16:05 -07004813 percpu_ref_reinit(&ctx->refs);
4814 return ret;
4815}
4816
4817SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
4818 void __user *, arg, unsigned int, nr_args)
4819{
4820 struct io_ring_ctx *ctx;
4821 long ret = -EBADF;
4822 struct fd f;
4823
4824 f = fdget(fd);
4825 if (!f.file)
4826 return -EBADF;
4827
4828 ret = -EOPNOTSUPP;
4829 if (f.file->f_op != &io_uring_fops)
4830 goto out_fput;
4831
4832 ctx = f.file->private_data;
4833
4834 mutex_lock(&ctx->uring_lock);
4835 ret = __io_uring_register(ctx, opcode, arg, nr_args);
4836 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004837 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
4838 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07004839out_fput:
4840 fdput(f);
4841 return ret;
4842}
4843
Jens Axboe2b188cc2019-01-07 10:46:33 -07004844static int __init io_uring_init(void)
4845{
4846 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
4847 return 0;
4848};
4849__initcall(io_uring_init);