blob: 6d8665e8f0e823faacf840072ba275687d80cdae [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 Axboe5f8fd2d32019-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
Jens Axboeba816ad2019-09-28 11:36:45 -0600906 /*
907 * If we're in async work, we can continue processing the chain
908 * in this context instead of having to queue up new async work.
909 */
Jens Axboe94ae5e72019-11-14 19:39:52 -0700910 if (nxt) {
911 if (nxtptr && io_wq_current_is_worker())
912 *nxtptr = nxt;
913 else
914 io_queue_async_work(nxt);
Jens Axboe2665abf2019-11-05 12:40:47 -0700915 }
Jens Axboe94ae5e72019-11-14 19:39:52 -0700916 break;
Jens Axboe9e645e112019-05-10 16:07:28 -0600917 }
Jens Axboe2665abf2019-11-05 12:40:47 -0700918
Jens Axboe4d7dd462019-11-20 13:03:52 -0700919 req->flags |= REQ_F_LINK_NEXT;
Jens Axboe2665abf2019-11-05 12:40:47 -0700920 if (wake_ev)
921 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -0600922}
923
924/*
925 * Called if REQ_F_LINK is set, and we fail the head request
926 */
927static void io_fail_links(struct io_kiocb *req)
928{
Jens Axboe2665abf2019-11-05 12:40:47 -0700929 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -0600930 struct io_kiocb *link;
Jens Axboe2665abf2019-11-05 12:40:47 -0700931 unsigned long flags;
932
933 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -0600934
935 while (!list_empty(&req->link_list)) {
936 link = list_first_entry(&req->link_list, struct io_kiocb, list);
Jens Axboe2665abf2019-11-05 12:40:47 -0700937 list_del_init(&link->list);
Jens Axboe9e645e112019-05-10 16:07:28 -0600938
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +0200939 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -0700940
941 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
942 link->submit.sqe->opcode == IORING_OP_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -0700943 io_link_cancel_timeout(link);
Jens Axboe2665abf2019-11-05 12:40:47 -0700944 } else {
Jens Axboe78e19bb2019-11-06 15:21:34 -0700945 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe978db572019-11-14 22:39:04 -0700946 __io_double_put_req(link);
Jens Axboe2665abf2019-11-05 12:40:47 -0700947 }
Jens Axboe5d960722019-11-19 15:31:28 -0700948 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -0600949 }
Jens Axboe2665abf2019-11-05 12:40:47 -0700950
951 io_commit_cqring(ctx);
952 spin_unlock_irqrestore(&ctx->completion_lock, flags);
953 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -0600954}
955
Jens Axboe4d7dd462019-11-20 13:03:52 -0700956static void io_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -0600957{
Jens Axboe4d7dd462019-11-20 13:03:52 -0700958 if (likely(!(req->flags & REQ_F_LINK)))
Jens Axboe2665abf2019-11-05 12:40:47 -0700959 return;
Jens Axboe2665abf2019-11-05 12:40:47 -0700960
Jens Axboe9e645e112019-05-10 16:07:28 -0600961 /*
962 * If LINK is set, we have dependent requests in this chain. If we
963 * didn't fail this request, queue the first one up, moving any other
964 * dependencies to the next request. In case of failure, fail the rest
965 * of the chain.
966 */
Jens Axboe2665abf2019-11-05 12:40:47 -0700967 if (req->flags & REQ_F_FAIL_LINK) {
968 io_fail_links(req);
Jens Axboe7c9e7f02019-11-12 08:15:53 -0700969 } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) ==
970 REQ_F_LINK_TIMEOUT) {
Jens Axboe2665abf2019-11-05 12:40:47 -0700971 struct io_ring_ctx *ctx = req->ctx;
972 unsigned long flags;
973
974 /*
975 * If this is a timeout link, we could be racing with the
976 * timeout timer. Grab the completion lock for this case to
Jens Axboe7c9e7f02019-11-12 08:15:53 -0700977 * protect against that.
Jens Axboe2665abf2019-11-05 12:40:47 -0700978 */
979 spin_lock_irqsave(&ctx->completion_lock, flags);
980 io_req_link_next(req, nxt);
981 spin_unlock_irqrestore(&ctx->completion_lock, flags);
982 } else {
983 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -0600984 }
Jens Axboe4d7dd462019-11-20 13:03:52 -0700985}
Jens Axboe9e645e112019-05-10 16:07:28 -0600986
Jackie Liuc69f8db2019-11-09 11:00:08 +0800987static void io_free_req(struct io_kiocb *req)
988{
Pavel Begunkov70cf9f32019-11-21 23:21:00 +0300989 io_req_find_next(req, NULL);
990 __io_free_req(req);
Jackie Liuc69f8db2019-11-09 11:00:08 +0800991}
992
Jens Axboeba816ad2019-09-28 11:36:45 -0600993/*
994 * Drop reference to request, return next in chain (if there is one) if this
995 * was the last reference to this request.
996 */
Jackie Liuec9c02a2019-11-08 23:50:36 +0800997static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboee65ef562019-03-12 10:16:44 -0600998{
Jens Axboeba816ad2019-09-28 11:36:45 -0600999 struct io_kiocb *nxt = NULL;
1000
Jens Axboe4d7dd462019-11-20 13:03:52 -07001001 io_req_find_next(req, &nxt);
1002
Jens Axboee65ef562019-03-12 10:16:44 -06001003 if (refcount_dec_and_test(&req->refs))
Jens Axboe4d7dd462019-11-20 13:03:52 -07001004 __io_free_req(req);
Jens Axboeba816ad2019-09-28 11:36:45 -06001005
Jens Axboeba816ad2019-09-28 11:36:45 -06001006 if (nxt) {
Jens Axboe561fb042019-10-24 07:25:42 -06001007 if (nxtptr)
Jens Axboeba816ad2019-09-28 11:36:45 -06001008 *nxtptr = nxt;
Jens Axboe561fb042019-10-24 07:25:42 -06001009 else
Jackie Liua197f662019-11-08 08:09:12 -07001010 io_queue_async_work(nxt);
Jens Axboeba816ad2019-09-28 11:36:45 -06001011 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001012}
1013
Jens Axboe2b188cc2019-01-07 10:46:33 -07001014static void io_put_req(struct io_kiocb *req)
1015{
Jens Axboedef596e2019-01-09 08:59:42 -07001016 if (refcount_dec_and_test(&req->refs))
1017 io_free_req(req);
1018}
1019
Jens Axboe978db572019-11-14 22:39:04 -07001020/*
1021 * Must only be used if we don't need to care about links, usually from
1022 * within the completion handling itself.
1023 */
1024static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06001025{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001026 /* drop both submit and complete references */
1027 if (refcount_sub_and_test(2, &req->refs))
1028 __io_free_req(req);
1029}
1030
Jens Axboe978db572019-11-14 22:39:04 -07001031static void io_double_put_req(struct io_kiocb *req)
1032{
1033 /* drop both submit and complete references */
1034 if (refcount_sub_and_test(2, &req->refs))
1035 io_free_req(req);
1036}
1037
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001038static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06001039{
Jens Axboe84f97dc2019-11-06 11:27:53 -07001040 struct io_rings *rings = ctx->rings;
1041
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001042 /*
1043 * noflush == true is from the waitqueue handler, just ensure we wake
1044 * up the task, and the next invocation will flush the entries. We
1045 * cannot safely to it from here.
1046 */
1047 if (noflush && !list_empty(&ctx->cq_overflow_list))
1048 return -1U;
1049
1050 io_cqring_overflow_flush(ctx, false);
1051
Jens Axboea3a0e432019-08-20 11:03:11 -06001052 /* See comment at the top of this file */
1053 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00001054 return READ_ONCE(rings->cq.tail) - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06001055}
1056
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03001057static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1058{
1059 struct io_rings *rings = ctx->rings;
1060
1061 /* make sure SQ entry isn't read before tail */
1062 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1063}
1064
Jens Axboedef596e2019-01-09 08:59:42 -07001065/*
1066 * Find and free completed poll iocbs
1067 */
1068static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
1069 struct list_head *done)
1070{
1071 void *reqs[IO_IOPOLL_BATCH];
1072 struct io_kiocb *req;
Jens Axboe09bb8392019-03-13 12:39:28 -06001073 int to_free;
Jens Axboedef596e2019-01-09 08:59:42 -07001074
Jens Axboe09bb8392019-03-13 12:39:28 -06001075 to_free = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001076 while (!list_empty(done)) {
1077 req = list_first_entry(done, struct io_kiocb, list);
1078 list_del(&req->list);
1079
Jens Axboe78e19bb2019-11-06 15:21:34 -07001080 io_cqring_fill_event(req, req->result);
Jens Axboedef596e2019-01-09 08:59:42 -07001081 (*nr_events)++;
1082
Jens Axboe09bb8392019-03-13 12:39:28 -06001083 if (refcount_dec_and_test(&req->refs)) {
1084 /* If we're not using fixed files, we have to pair the
1085 * completion part with the file put. Use regular
1086 * completions for those, only batch free for fixed
Jens Axboe9e645e112019-05-10 16:07:28 -06001087 * file and non-linked commands.
Jens Axboe09bb8392019-03-13 12:39:28 -06001088 */
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03001089 if (((req->flags &
1090 (REQ_F_FIXED_FILE|REQ_F_LINK|REQ_F_FREE_SQE)) ==
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001091 REQ_F_FIXED_FILE) && !io_is_fallback_req(req)) {
Jens Axboe09bb8392019-03-13 12:39:28 -06001092 reqs[to_free++] = req;
1093 if (to_free == ARRAY_SIZE(reqs))
1094 io_free_req_many(ctx, reqs, &to_free);
Jens Axboe6b063142019-01-10 22:13:58 -07001095 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001096 io_free_req(req);
Jens Axboe6b063142019-01-10 22:13:58 -07001097 }
Jens Axboe9a56a232019-01-09 09:06:50 -07001098 }
Jens Axboedef596e2019-01-09 08:59:42 -07001099 }
Jens Axboedef596e2019-01-09 08:59:42 -07001100
Jens Axboe09bb8392019-03-13 12:39:28 -06001101 io_commit_cqring(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07001102 io_free_req_many(ctx, reqs, &to_free);
1103}
1104
1105static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1106 long min)
1107{
1108 struct io_kiocb *req, *tmp;
1109 LIST_HEAD(done);
1110 bool spin;
1111 int ret;
1112
1113 /*
1114 * Only spin for completions if we don't have multiple devices hanging
1115 * off our complete list, and we're under the requested amount.
1116 */
1117 spin = !ctx->poll_multi_file && *nr_events < min;
1118
1119 ret = 0;
1120 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
1121 struct kiocb *kiocb = &req->rw;
1122
1123 /*
1124 * Move completed entries to our local list. If we find a
1125 * request that requires polling, break out and complete
1126 * the done list first, if we have entries there.
1127 */
1128 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
1129 list_move_tail(&req->list, &done);
1130 continue;
1131 }
1132 if (!list_empty(&done))
1133 break;
1134
1135 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1136 if (ret < 0)
1137 break;
1138
1139 if (ret && spin)
1140 spin = false;
1141 ret = 0;
1142 }
1143
1144 if (!list_empty(&done))
1145 io_iopoll_complete(ctx, nr_events, &done);
1146
1147 return ret;
1148}
1149
1150/*
1151 * Poll for a mininum of 'min' events. Note that if min == 0 we consider that a
1152 * non-spinning poll check - we'll still enter the driver poll loop, but only
1153 * as a non-spinning completion check.
1154 */
1155static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
1156 long min)
1157{
Jens Axboe08f54392019-08-21 22:19:11 -06001158 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07001159 int ret;
1160
1161 ret = io_do_iopoll(ctx, nr_events, min);
1162 if (ret < 0)
1163 return ret;
1164 if (!min || *nr_events >= min)
1165 return 0;
1166 }
1167
1168 return 1;
1169}
1170
1171/*
1172 * We can't just wait for polled events to come to us, we have to actively
1173 * find and complete them.
1174 */
1175static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
1176{
1177 if (!(ctx->flags & IORING_SETUP_IOPOLL))
1178 return;
1179
1180 mutex_lock(&ctx->uring_lock);
1181 while (!list_empty(&ctx->poll_list)) {
1182 unsigned int nr_events = 0;
1183
1184 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -06001185
1186 /*
1187 * Ensure we allow local-to-the-cpu processing to take place,
1188 * in this case we need to ensure that we reap all events.
1189 */
1190 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -07001191 }
1192 mutex_unlock(&ctx->uring_lock);
1193}
1194
Jens Axboe2b2ed972019-10-25 10:06:15 -06001195static int __io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1196 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07001197{
Jens Axboe2b2ed972019-10-25 10:06:15 -06001198 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001199
1200 do {
1201 int tmin = 0;
1202
Jens Axboe500f9fb2019-08-19 12:15:59 -06001203 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06001204 * Don't enter poll loop if we already have events pending.
1205 * If we do, we can potentially be spinning for commands that
1206 * already triggered a CQE (eg in error).
1207 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001208 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06001209 break;
1210
1211 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06001212 * If a submit got punted to a workqueue, we can have the
1213 * application entering polling for a command before it gets
1214 * issued. That app will hold the uring_lock for the duration
1215 * of the poll right here, so we need to take a breather every
1216 * now and then to ensure that the issue has a chance to add
1217 * the poll to the issued list. Otherwise we can spin here
1218 * forever, while the workqueue is stuck trying to acquire the
1219 * very same mutex.
1220 */
1221 if (!(++iters & 7)) {
1222 mutex_unlock(&ctx->uring_lock);
1223 mutex_lock(&ctx->uring_lock);
1224 }
1225
Jens Axboedef596e2019-01-09 08:59:42 -07001226 if (*nr_events < min)
1227 tmin = min - *nr_events;
1228
1229 ret = io_iopoll_getevents(ctx, nr_events, tmin);
1230 if (ret <= 0)
1231 break;
1232 ret = 0;
1233 } while (min && !*nr_events && !need_resched());
1234
Jens Axboe2b2ed972019-10-25 10:06:15 -06001235 return ret;
1236}
1237
1238static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1239 long min)
1240{
1241 int ret;
1242
1243 /*
1244 * We disallow the app entering submit/complete with polling, but we
1245 * still need to lock the ring to prevent racing with polled issue
1246 * that got punted to a workqueue.
1247 */
1248 mutex_lock(&ctx->uring_lock);
1249 ret = __io_iopoll_check(ctx, nr_events, min);
Jens Axboe500f9fb2019-08-19 12:15:59 -06001250 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001251 return ret;
1252}
1253
Jens Axboe491381ce2019-10-17 09:20:46 -06001254static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001255{
Jens Axboe491381ce2019-10-17 09:20:46 -06001256 /*
1257 * Tell lockdep we inherited freeze protection from submission
1258 * thread.
1259 */
1260 if (req->flags & REQ_F_ISREG) {
1261 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001262
Jens Axboe491381ce2019-10-17 09:20:46 -06001263 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001264 }
Jens Axboe491381ce2019-10-17 09:20:46 -06001265 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001266}
1267
Jens Axboeba816ad2019-09-28 11:36:45 -06001268static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001269{
1270 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1271
Jens Axboe491381ce2019-10-17 09:20:46 -06001272 if (kiocb->ki_flags & IOCB_WRITE)
1273 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001274
Jens Axboe9e645e112019-05-10 16:07:28 -06001275 if ((req->flags & REQ_F_LINK) && res != req->result)
1276 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001277 io_cqring_add_event(req, res);
Jens Axboeba816ad2019-09-28 11:36:45 -06001278}
1279
1280static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1281{
1282 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1283
1284 io_complete_rw_common(kiocb, res);
Jens Axboee65ef562019-03-12 10:16:44 -06001285 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001286}
1287
Jens Axboeba816ad2019-09-28 11:36:45 -06001288static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res)
1289{
1290 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001291 struct io_kiocb *nxt = NULL;
Jens Axboeba816ad2019-09-28 11:36:45 -06001292
1293 io_complete_rw_common(kiocb, res);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001294 io_put_req_find_next(req, &nxt);
1295
1296 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001297}
1298
Jens Axboedef596e2019-01-09 08:59:42 -07001299static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1300{
1301 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1302
Jens Axboe491381ce2019-10-17 09:20:46 -06001303 if (kiocb->ki_flags & IOCB_WRITE)
1304 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001305
Jens Axboe9e645e112019-05-10 16:07:28 -06001306 if ((req->flags & REQ_F_LINK) && res != req->result)
1307 req->flags |= REQ_F_FAIL_LINK;
1308 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -07001309 if (res != -EAGAIN)
1310 req->flags |= REQ_F_IOPOLL_COMPLETED;
1311}
1312
1313/*
1314 * After the iocb has been issued, it's safe to be found on the poll list.
1315 * Adding the kiocb to the list AFTER submission ensures that we don't
1316 * find it from a io_iopoll_getevents() thread before the issuer is done
1317 * accessing the kiocb cookie.
1318 */
1319static void io_iopoll_req_issued(struct io_kiocb *req)
1320{
1321 struct io_ring_ctx *ctx = req->ctx;
1322
1323 /*
1324 * Track whether we have multiple files in our lists. This will impact
1325 * how we do polling eventually, not spinning if we're on potentially
1326 * different devices.
1327 */
1328 if (list_empty(&ctx->poll_list)) {
1329 ctx->poll_multi_file = false;
1330 } else if (!ctx->poll_multi_file) {
1331 struct io_kiocb *list_req;
1332
1333 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1334 list);
1335 if (list_req->rw.ki_filp != req->rw.ki_filp)
1336 ctx->poll_multi_file = true;
1337 }
1338
1339 /*
1340 * For fast devices, IO may have already completed. If it has, add
1341 * it to the front so we find it first.
1342 */
1343 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1344 list_add(&req->list, &ctx->poll_list);
1345 else
1346 list_add_tail(&req->list, &ctx->poll_list);
1347}
1348
Jens Axboe3d6770f2019-04-13 11:50:54 -06001349static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07001350{
Jens Axboe3d6770f2019-04-13 11:50:54 -06001351 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -07001352 int diff = state->has_refs - state->used_refs;
1353
1354 if (diff)
1355 fput_many(state->file, diff);
1356 state->file = NULL;
1357 }
1358}
1359
1360/*
1361 * Get as many references to a file as we have IOs left in this submission,
1362 * assuming most submissions are for one file, or at least that each file
1363 * has more than one submission.
1364 */
1365static struct file *io_file_get(struct io_submit_state *state, int fd)
1366{
1367 if (!state)
1368 return fget(fd);
1369
1370 if (state->file) {
1371 if (state->fd == fd) {
1372 state->used_refs++;
1373 state->ios_left--;
1374 return state->file;
1375 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06001376 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07001377 }
1378 state->file = fget_many(fd, state->ios_left);
1379 if (!state->file)
1380 return NULL;
1381
1382 state->fd = fd;
1383 state->has_refs = state->ios_left;
1384 state->used_refs = 1;
1385 state->ios_left--;
1386 return state->file;
1387}
1388
Jens Axboe2b188cc2019-01-07 10:46:33 -07001389/*
1390 * If we tracked the file through the SCM inflight mechanism, we could support
1391 * any file. For now, just ensure that anything potentially problematic is done
1392 * inline.
1393 */
1394static bool io_file_supports_async(struct file *file)
1395{
1396 umode_t mode = file_inode(file)->i_mode;
1397
1398 if (S_ISBLK(mode) || S_ISCHR(mode))
1399 return true;
1400 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
1401 return true;
1402
1403 return false;
1404}
1405
Pavel Begunkov267bc902019-11-07 01:41:08 +03001406static int io_prep_rw(struct io_kiocb *req, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001407{
Pavel Begunkov267bc902019-11-07 01:41:08 +03001408 const struct io_uring_sqe *sqe = req->submit.sqe;
Jens Axboedef596e2019-01-09 08:59:42 -07001409 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001410 struct kiocb *kiocb = &req->rw;
Jens Axboe09bb8392019-03-13 12:39:28 -06001411 unsigned ioprio;
1412 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001413
Jens Axboe09bb8392019-03-13 12:39:28 -06001414 if (!req->file)
1415 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001416
Jens Axboe491381ce2019-10-17 09:20:46 -06001417 if (S_ISREG(file_inode(req->file)->i_mode))
1418 req->flags |= REQ_F_ISREG;
1419
1420 /*
1421 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
1422 * we know to async punt it even if it was opened O_NONBLOCK
1423 */
1424 if (force_nonblock && !io_file_supports_async(req->file)) {
1425 req->flags |= REQ_F_MUST_PUNT;
1426 return -EAGAIN;
1427 }
Jens Axboe6b063142019-01-10 22:13:58 -07001428
Jens Axboe2b188cc2019-01-07 10:46:33 -07001429 kiocb->ki_pos = READ_ONCE(sqe->off);
1430 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
1431 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
1432
1433 ioprio = READ_ONCE(sqe->ioprio);
1434 if (ioprio) {
1435 ret = ioprio_check_cap(ioprio);
1436 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06001437 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001438
1439 kiocb->ki_ioprio = ioprio;
1440 } else
1441 kiocb->ki_ioprio = get_current_ioprio();
1442
1443 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
1444 if (unlikely(ret))
Jens Axboe09bb8392019-03-13 12:39:28 -06001445 return ret;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001446
1447 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06001448 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
1449 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02001450 req->flags |= REQ_F_NOWAIT;
1451
1452 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001453 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001454
Jens Axboedef596e2019-01-09 08:59:42 -07001455 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07001456 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
1457 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06001458 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001459
Jens Axboedef596e2019-01-09 08:59:42 -07001460 kiocb->ki_flags |= IOCB_HIPRI;
1461 kiocb->ki_complete = io_complete_rw_iopoll;
Jens Axboe6873e0b2019-10-30 13:53:09 -06001462 req->result = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001463 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001464 if (kiocb->ki_flags & IOCB_HIPRI)
1465 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07001466 kiocb->ki_complete = io_complete_rw;
1467 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001468 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001469}
1470
1471static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
1472{
1473 switch (ret) {
1474 case -EIOCBQUEUED:
1475 break;
1476 case -ERESTARTSYS:
1477 case -ERESTARTNOINTR:
1478 case -ERESTARTNOHAND:
1479 case -ERESTART_RESTARTBLOCK:
1480 /*
1481 * We can't just restart the syscall, since previously
1482 * submitted sqes may already be in progress. Just fail this
1483 * IO with EINTR.
1484 */
1485 ret = -EINTR;
1486 /* fall through */
1487 default:
1488 kiocb->ki_complete(kiocb, ret, 0);
1489 }
1490}
1491
Jens Axboeba816ad2019-09-28 11:36:45 -06001492static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt,
1493 bool in_async)
1494{
1495 if (in_async && ret >= 0 && nxt && kiocb->ki_complete == io_complete_rw)
1496 *nxt = __io_complete_rw(kiocb, ret);
1497 else
1498 io_rw_done(kiocb, ret);
1499}
1500
Jens Axboeedafcce2019-01-09 09:16:05 -07001501static int io_import_fixed(struct io_ring_ctx *ctx, int rw,
1502 const struct io_uring_sqe *sqe,
1503 struct iov_iter *iter)
1504{
1505 size_t len = READ_ONCE(sqe->len);
1506 struct io_mapped_ubuf *imu;
1507 unsigned index, buf_index;
1508 size_t offset;
1509 u64 buf_addr;
1510
1511 /* attempt to use fixed buffers without having provided iovecs */
1512 if (unlikely(!ctx->user_bufs))
1513 return -EFAULT;
1514
1515 buf_index = READ_ONCE(sqe->buf_index);
1516 if (unlikely(buf_index >= ctx->nr_user_bufs))
1517 return -EFAULT;
1518
1519 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
1520 imu = &ctx->user_bufs[index];
1521 buf_addr = READ_ONCE(sqe->addr);
1522
1523 /* overflow */
1524 if (buf_addr + len < buf_addr)
1525 return -EFAULT;
1526 /* not inside the mapped region */
1527 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
1528 return -EFAULT;
1529
1530 /*
1531 * May not be a start of buffer, set size appropriately
1532 * and advance us to the beginning.
1533 */
1534 offset = buf_addr - imu->ubuf;
1535 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06001536
1537 if (offset) {
1538 /*
1539 * Don't use iov_iter_advance() here, as it's really slow for
1540 * using the latter parts of a big fixed buffer - it iterates
1541 * over each segment manually. We can cheat a bit here, because
1542 * we know that:
1543 *
1544 * 1) it's a BVEC iter, we set it up
1545 * 2) all bvecs are PAGE_SIZE in size, except potentially the
1546 * first and last bvec
1547 *
1548 * So just find our index, and adjust the iterator afterwards.
1549 * If the offset is within the first bvec (or the whole first
1550 * bvec, just use iov_iter_advance(). This makes it easier
1551 * since we can just skip the first segment, which may not
1552 * be PAGE_SIZE aligned.
1553 */
1554 const struct bio_vec *bvec = imu->bvec;
1555
1556 if (offset <= bvec->bv_len) {
1557 iov_iter_advance(iter, offset);
1558 } else {
1559 unsigned long seg_skip;
1560
1561 /* skip first vec */
1562 offset -= bvec->bv_len;
1563 seg_skip = 1 + (offset >> PAGE_SHIFT);
1564
1565 iter->bvec = bvec + seg_skip;
1566 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02001567 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001568 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001569 }
1570 }
1571
Jens Axboe5e559562019-11-13 16:12:46 -07001572 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001573}
1574
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001575static ssize_t io_import_iovec(struct io_ring_ctx *ctx, int rw,
1576 const struct sqe_submit *s, struct iovec **iovec,
1577 struct iov_iter *iter)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001578{
1579 const struct io_uring_sqe *sqe = s->sqe;
1580 void __user *buf = u64_to_user_ptr(READ_ONCE(sqe->addr));
1581 size_t sqe_len = READ_ONCE(sqe->len);
Jens Axboeedafcce2019-01-09 09:16:05 -07001582 u8 opcode;
1583
1584 /*
1585 * We're reading ->opcode for the second time, but the first read
1586 * doesn't care whether it's _FIXED or not, so it doesn't matter
1587 * whether ->opcode changes concurrently. The first read does care
1588 * about whether it is a READ or a WRITE, so we don't trust this read
1589 * for that purpose and instead let the caller pass in the read/write
1590 * flag.
1591 */
1592 opcode = READ_ONCE(sqe->opcode);
1593 if (opcode == IORING_OP_READ_FIXED ||
1594 opcode == IORING_OP_WRITE_FIXED) {
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001595 ssize_t ret = io_import_fixed(ctx, rw, sqe, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07001596 *iovec = NULL;
1597 return ret;
1598 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001599
1600 if (!s->has_user)
1601 return -EFAULT;
1602
1603#ifdef CONFIG_COMPAT
1604 if (ctx->compat)
1605 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
1606 iovec, iter);
1607#endif
1608
1609 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
1610}
1611
Jens Axboe32960612019-09-23 11:05:34 -06001612/*
1613 * For files that don't have ->read_iter() and ->write_iter(), handle them
1614 * by looping over ->read() or ->write() manually.
1615 */
1616static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
1617 struct iov_iter *iter)
1618{
1619 ssize_t ret = 0;
1620
1621 /*
1622 * Don't support polled IO through this interface, and we can't
1623 * support non-blocking either. For the latter, this just causes
1624 * the kiocb to be handled from an async context.
1625 */
1626 if (kiocb->ki_flags & IOCB_HIPRI)
1627 return -EOPNOTSUPP;
1628 if (kiocb->ki_flags & IOCB_NOWAIT)
1629 return -EAGAIN;
1630
1631 while (iov_iter_count(iter)) {
1632 struct iovec iovec = iov_iter_iovec(iter);
1633 ssize_t nr;
1634
1635 if (rw == READ) {
1636 nr = file->f_op->read(file, iovec.iov_base,
1637 iovec.iov_len, &kiocb->ki_pos);
1638 } else {
1639 nr = file->f_op->write(file, iovec.iov_base,
1640 iovec.iov_len, &kiocb->ki_pos);
1641 }
1642
1643 if (nr < 0) {
1644 if (!ret)
1645 ret = nr;
1646 break;
1647 }
1648 ret += nr;
1649 if (nr != iovec.iov_len)
1650 break;
1651 iov_iter_advance(iter, nr);
1652 }
1653
1654 return ret;
1655}
1656
Pavel Begunkov267bc902019-11-07 01:41:08 +03001657static int io_read(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001658 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001659{
1660 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1661 struct kiocb *kiocb = &req->rw;
1662 struct iov_iter iter;
1663 struct file *file;
Jens Axboe31b51512019-01-18 22:56:34 -07001664 size_t iov_count;
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001665 ssize_t read_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001666
Pavel Begunkov267bc902019-11-07 01:41:08 +03001667 ret = io_prep_rw(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001668 if (ret)
1669 return ret;
1670 file = kiocb->ki_filp;
1671
Jens Axboe2b188cc2019-01-07 10:46:33 -07001672 if (unlikely(!(file->f_mode & FMODE_READ)))
Jens Axboe09bb8392019-03-13 12:39:28 -06001673 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001674
Pavel Begunkov267bc902019-11-07 01:41:08 +03001675 ret = io_import_iovec(req->ctx, READ, &req->submit, &iovec, &iter);
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001676 if (ret < 0)
Jens Axboe09bb8392019-03-13 12:39:28 -06001677 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001678
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001679 read_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06001680 if (req->flags & REQ_F_LINK)
1681 req->result = read_size;
1682
Jens Axboe31b51512019-01-18 22:56:34 -07001683 iov_count = iov_iter_count(&iter);
1684 ret = rw_verify_area(READ, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001685 if (!ret) {
1686 ssize_t ret2;
1687
Jens Axboe32960612019-09-23 11:05:34 -06001688 if (file->f_op->read_iter)
1689 ret2 = call_read_iter(file, kiocb, &iter);
1690 else
1691 ret2 = loop_rw_iter(READ, file, kiocb, &iter);
1692
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001693 /*
1694 * In case of a short read, punt to async. This can happen
1695 * if we have data partially cached. Alternatively we can
1696 * return the short read, in which case the application will
1697 * need to issue another SQE and wait for it. That SQE will
1698 * need async punt anyway, so it's more efficient to do it
1699 * here.
1700 */
Jens Axboe491381ce2019-10-17 09:20:46 -06001701 if (force_nonblock && !(req->flags & REQ_F_NOWAIT) &&
1702 (req->flags & REQ_F_ISREG) &&
1703 ret2 > 0 && ret2 < read_size)
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001704 ret2 = -EAGAIN;
1705 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboe561fb042019-10-24 07:25:42 -06001706 if (!force_nonblock || ret2 != -EAGAIN)
Pavel Begunkov267bc902019-11-07 01:41:08 +03001707 kiocb_done(kiocb, ret2, nxt, req->submit.in_async);
Jens Axboe561fb042019-10-24 07:25:42 -06001708 else
Jens Axboe2b188cc2019-01-07 10:46:33 -07001709 ret = -EAGAIN;
1710 }
1711 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001712 return ret;
1713}
1714
Pavel Begunkov267bc902019-11-07 01:41:08 +03001715static int io_write(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001716 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001717{
1718 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1719 struct kiocb *kiocb = &req->rw;
1720 struct iov_iter iter;
1721 struct file *file;
Jens Axboe31b51512019-01-18 22:56:34 -07001722 size_t iov_count;
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001723 ssize_t ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001724
Pavel Begunkov267bc902019-11-07 01:41:08 +03001725 ret = io_prep_rw(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001726 if (ret)
1727 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001728
Jens Axboe2b188cc2019-01-07 10:46:33 -07001729 file = kiocb->ki_filp;
1730 if (unlikely(!(file->f_mode & FMODE_WRITE)))
Jens Axboe09bb8392019-03-13 12:39:28 -06001731 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001732
Pavel Begunkov267bc902019-11-07 01:41:08 +03001733 ret = io_import_iovec(req->ctx, WRITE, &req->submit, &iovec, &iter);
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001734 if (ret < 0)
Jens Axboe09bb8392019-03-13 12:39:28 -06001735 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001736
Jens Axboe9e645e112019-05-10 16:07:28 -06001737 if (req->flags & REQ_F_LINK)
1738 req->result = ret;
1739
Jens Axboe31b51512019-01-18 22:56:34 -07001740 iov_count = iov_iter_count(&iter);
1741
1742 ret = -EAGAIN;
Jens Axboe561fb042019-10-24 07:25:42 -06001743 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT))
Jens Axboe31b51512019-01-18 22:56:34 -07001744 goto out_free;
Jens Axboe31b51512019-01-18 22:56:34 -07001745
1746 ret = rw_verify_area(WRITE, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001747 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01001748 ssize_t ret2;
1749
Jens Axboe2b188cc2019-01-07 10:46:33 -07001750 /*
1751 * Open-code file_start_write here to grab freeze protection,
1752 * which will be released by another thread in
1753 * io_complete_rw(). Fool lockdep by telling it the lock got
1754 * released so that it doesn't complain about the held lock when
1755 * we return to userspace.
1756 */
Jens Axboe491381ce2019-10-17 09:20:46 -06001757 if (req->flags & REQ_F_ISREG) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001758 __sb_start_write(file_inode(file)->i_sb,
1759 SB_FREEZE_WRITE, true);
1760 __sb_writers_release(file_inode(file)->i_sb,
1761 SB_FREEZE_WRITE);
1762 }
1763 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01001764
Jens Axboe32960612019-09-23 11:05:34 -06001765 if (file->f_op->write_iter)
1766 ret2 = call_write_iter(file, kiocb, &iter);
1767 else
1768 ret2 = loop_rw_iter(WRITE, file, kiocb, &iter);
Jens Axboe561fb042019-10-24 07:25:42 -06001769 if (!force_nonblock || ret2 != -EAGAIN)
Pavel Begunkov267bc902019-11-07 01:41:08 +03001770 kiocb_done(kiocb, ret2, nxt, req->submit.in_async);
Jens Axboe561fb042019-10-24 07:25:42 -06001771 else
Roman Penyaev9bf79332019-03-25 20:09:24 +01001772 ret = -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001773 }
Jens Axboe31b51512019-01-18 22:56:34 -07001774out_free:
Jens Axboe2b188cc2019-01-07 10:46:33 -07001775 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001776 return ret;
1777}
1778
1779/*
1780 * IORING_OP_NOP just posts a completion event, nothing else.
1781 */
Jens Axboe78e19bb2019-11-06 15:21:34 -07001782static int io_nop(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001783{
1784 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001785
Jens Axboedef596e2019-01-09 08:59:42 -07001786 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1787 return -EINVAL;
1788
Jens Axboe78e19bb2019-11-06 15:21:34 -07001789 io_cqring_add_event(req, 0);
Jens Axboee65ef562019-03-12 10:16:44 -06001790 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001791 return 0;
1792}
1793
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001794static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1795{
Jens Axboe6b063142019-01-10 22:13:58 -07001796 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001797
Jens Axboe09bb8392019-03-13 12:39:28 -06001798 if (!req->file)
1799 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001800
Jens Axboe6b063142019-01-10 22:13:58 -07001801 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07001802 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07001803 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001804 return -EINVAL;
1805
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001806 return 0;
1807}
1808
1809static int io_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001810 struct io_kiocb **nxt, bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001811{
1812 loff_t sqe_off = READ_ONCE(sqe->off);
1813 loff_t sqe_len = READ_ONCE(sqe->len);
1814 loff_t end = sqe_off + sqe_len;
1815 unsigned fsync_flags;
1816 int ret;
1817
1818 fsync_flags = READ_ONCE(sqe->fsync_flags);
1819 if (unlikely(fsync_flags & ~IORING_FSYNC_DATASYNC))
1820 return -EINVAL;
1821
1822 ret = io_prep_fsync(req, sqe);
1823 if (ret)
1824 return ret;
1825
1826 /* fsync always requires a blocking context */
1827 if (force_nonblock)
1828 return -EAGAIN;
1829
1830 ret = vfs_fsync_range(req->rw.ki_filp, sqe_off,
1831 end > 0 ? end : LLONG_MAX,
1832 fsync_flags & IORING_FSYNC_DATASYNC);
1833
Jens Axboe9e645e112019-05-10 16:07:28 -06001834 if (ret < 0 && (req->flags & REQ_F_LINK))
1835 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001836 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001837 io_put_req_find_next(req, nxt);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001838 return 0;
1839}
1840
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001841static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1842{
1843 struct io_ring_ctx *ctx = req->ctx;
1844 int ret = 0;
1845
1846 if (!req->file)
1847 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001848
1849 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1850 return -EINVAL;
1851 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
1852 return -EINVAL;
1853
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001854 return ret;
1855}
1856
1857static int io_sync_file_range(struct io_kiocb *req,
1858 const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001859 struct io_kiocb **nxt,
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001860 bool force_nonblock)
1861{
1862 loff_t sqe_off;
1863 loff_t sqe_len;
1864 unsigned flags;
1865 int ret;
1866
1867 ret = io_prep_sfr(req, sqe);
1868 if (ret)
1869 return ret;
1870
1871 /* sync_file_range always requires a blocking context */
1872 if (force_nonblock)
1873 return -EAGAIN;
1874
1875 sqe_off = READ_ONCE(sqe->off);
1876 sqe_len = READ_ONCE(sqe->len);
1877 flags = READ_ONCE(sqe->sync_range_flags);
1878
1879 ret = sync_file_range(req->rw.ki_filp, sqe_off, sqe_len, flags);
1880
Jens Axboe9e645e112019-05-10 16:07:28 -06001881 if (ret < 0 && (req->flags & REQ_F_LINK))
1882 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001883 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001884 io_put_req_find_next(req, nxt);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001885 return 0;
1886}
1887
Jens Axboe0fa03c62019-04-19 13:34:07 -06001888#if defined(CONFIG_NET)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001889static int io_send_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001890 struct io_kiocb **nxt, bool force_nonblock,
Jens Axboeaa1fa282019-04-19 13:38:09 -06001891 long (*fn)(struct socket *, struct user_msghdr __user *,
1892 unsigned int))
1893{
Jens Axboe0fa03c62019-04-19 13:34:07 -06001894 struct socket *sock;
1895 int ret;
1896
1897 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1898 return -EINVAL;
1899
1900 sock = sock_from_file(req->file, &ret);
1901 if (sock) {
1902 struct user_msghdr __user *msg;
1903 unsigned flags;
1904
1905 flags = READ_ONCE(sqe->msg_flags);
1906 if (flags & MSG_DONTWAIT)
1907 req->flags |= REQ_F_NOWAIT;
1908 else if (force_nonblock)
1909 flags |= MSG_DONTWAIT;
1910
1911 msg = (struct user_msghdr __user *) (unsigned long)
1912 READ_ONCE(sqe->addr);
1913
Jens Axboeaa1fa282019-04-19 13:38:09 -06001914 ret = fn(sock, msg, flags);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001915 if (force_nonblock && ret == -EAGAIN)
1916 return ret;
1917 }
1918
Jens Axboe78e19bb2019-11-06 15:21:34 -07001919 io_cqring_add_event(req, ret);
Jens Axboef1f40852019-11-05 20:33:16 -07001920 if (ret < 0 && (req->flags & REQ_F_LINK))
1921 req->flags |= REQ_F_FAIL_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001922 io_put_req_find_next(req, nxt);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001923 return 0;
Jens Axboeaa1fa282019-04-19 13:38:09 -06001924}
1925#endif
1926
1927static int io_sendmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001928 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001929{
1930#if defined(CONFIG_NET)
Jens Axboeba816ad2019-09-28 11:36:45 -06001931 return io_send_recvmsg(req, sqe, nxt, force_nonblock,
1932 __sys_sendmsg_sock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06001933#else
1934 return -EOPNOTSUPP;
1935#endif
1936}
1937
1938static int io_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001939 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001940{
1941#if defined(CONFIG_NET)
Jens Axboeba816ad2019-09-28 11:36:45 -06001942 return io_send_recvmsg(req, sqe, nxt, force_nonblock,
1943 __sys_recvmsg_sock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001944#else
1945 return -EOPNOTSUPP;
1946#endif
1947}
1948
Jens Axboe17f2fe32019-10-17 14:42:58 -06001949static int io_accept(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1950 struct io_kiocb **nxt, bool force_nonblock)
1951{
1952#if defined(CONFIG_NET)
1953 struct sockaddr __user *addr;
1954 int __user *addr_len;
1955 unsigned file_flags;
1956 int flags, ret;
1957
1958 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
1959 return -EINVAL;
1960 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
1961 return -EINVAL;
1962
1963 addr = (struct sockaddr __user *) (unsigned long) READ_ONCE(sqe->addr);
1964 addr_len = (int __user *) (unsigned long) READ_ONCE(sqe->addr2);
1965 flags = READ_ONCE(sqe->accept_flags);
1966 file_flags = force_nonblock ? O_NONBLOCK : 0;
1967
1968 ret = __sys_accept4_file(req->file, file_flags, addr, addr_len, flags);
1969 if (ret == -EAGAIN && force_nonblock) {
1970 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
1971 return -EAGAIN;
1972 }
Jens Axboe8e3cca12019-11-09 19:52:33 -07001973 if (ret == -ERESTARTSYS)
1974 ret = -EINTR;
Jens Axboe17f2fe32019-10-17 14:42:58 -06001975 if (ret < 0 && (req->flags & REQ_F_LINK))
1976 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001977 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001978 io_put_req_find_next(req, nxt);
Jens Axboe17f2fe32019-10-17 14:42:58 -06001979 return 0;
1980#else
1981 return -EOPNOTSUPP;
1982#endif
1983}
1984
Jens Axboeeac406c2019-11-14 12:09:58 -07001985static inline void io_poll_remove_req(struct io_kiocb *req)
1986{
1987 if (!RB_EMPTY_NODE(&req->rb_node)) {
1988 rb_erase(&req->rb_node, &req->ctx->cancel_tree);
1989 RB_CLEAR_NODE(&req->rb_node);
1990 }
1991}
1992
Jens Axboe221c5eb2019-01-17 09:41:58 -07001993static void io_poll_remove_one(struct io_kiocb *req)
1994{
1995 struct io_poll_iocb *poll = &req->poll;
1996
1997 spin_lock(&poll->head->lock);
1998 WRITE_ONCE(poll->canceled, true);
1999 if (!list_empty(&poll->wait.entry)) {
2000 list_del_init(&poll->wait.entry);
Jackie Liua197f662019-11-08 08:09:12 -07002001 io_queue_async_work(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002002 }
2003 spin_unlock(&poll->head->lock);
Jens Axboeeac406c2019-11-14 12:09:58 -07002004 io_poll_remove_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002005}
2006
2007static void io_poll_remove_all(struct io_ring_ctx *ctx)
2008{
Jens Axboeeac406c2019-11-14 12:09:58 -07002009 struct rb_node *node;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002010 struct io_kiocb *req;
2011
2012 spin_lock_irq(&ctx->completion_lock);
Jens Axboeeac406c2019-11-14 12:09:58 -07002013 while ((node = rb_first(&ctx->cancel_tree)) != NULL) {
2014 req = rb_entry(node, struct io_kiocb, rb_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002015 io_poll_remove_one(req);
2016 }
2017 spin_unlock_irq(&ctx->completion_lock);
2018}
2019
Jens Axboe47f46762019-11-09 17:43:02 -07002020static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
2021{
Jens Axboeeac406c2019-11-14 12:09:58 -07002022 struct rb_node *p, *parent = NULL;
Jens Axboe47f46762019-11-09 17:43:02 -07002023 struct io_kiocb *req;
2024
Jens Axboeeac406c2019-11-14 12:09:58 -07002025 p = ctx->cancel_tree.rb_node;
2026 while (p) {
2027 parent = p;
2028 req = rb_entry(parent, struct io_kiocb, rb_node);
2029 if (sqe_addr < req->user_data) {
2030 p = p->rb_left;
2031 } else if (sqe_addr > req->user_data) {
2032 p = p->rb_right;
2033 } else {
2034 io_poll_remove_one(req);
2035 return 0;
2036 }
Jens Axboe47f46762019-11-09 17:43:02 -07002037 }
2038
2039 return -ENOENT;
2040}
2041
Jens Axboe221c5eb2019-01-17 09:41:58 -07002042/*
2043 * Find a running poll command that matches one specified in sqe->addr,
2044 * and remove it if found.
2045 */
2046static int io_poll_remove(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2047{
2048 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07002049 int ret;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002050
2051 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2052 return -EINVAL;
2053 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
2054 sqe->poll_events)
2055 return -EINVAL;
2056
2057 spin_lock_irq(&ctx->completion_lock);
Jens Axboe47f46762019-11-09 17:43:02 -07002058 ret = io_poll_cancel(ctx, READ_ONCE(sqe->addr));
Jens Axboe221c5eb2019-01-17 09:41:58 -07002059 spin_unlock_irq(&ctx->completion_lock);
2060
Jens Axboe78e19bb2019-11-06 15:21:34 -07002061 io_cqring_add_event(req, ret);
Jens Axboef1f40852019-11-05 20:33:16 -07002062 if (ret < 0 && (req->flags & REQ_F_LINK))
2063 req->flags |= REQ_F_FAIL_LINK;
Jens Axboee65ef562019-03-12 10:16:44 -06002064 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002065 return 0;
2066}
2067
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002068static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002069{
Jackie Liua197f662019-11-08 08:09:12 -07002070 struct io_ring_ctx *ctx = req->ctx;
2071
Jens Axboe8c838782019-03-12 15:48:16 -06002072 req->poll.done = true;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002073 if (error)
2074 io_cqring_fill_event(req, error);
2075 else
2076 io_cqring_fill_event(req, mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06002077 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002078}
2079
Jens Axboe561fb042019-10-24 07:25:42 -06002080static void io_poll_complete_work(struct io_wq_work **workptr)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002081{
Jens Axboe561fb042019-10-24 07:25:42 -06002082 struct io_wq_work *work = *workptr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002083 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
2084 struct io_poll_iocb *poll = &req->poll;
2085 struct poll_table_struct pt = { ._key = poll->events };
2086 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe89723d02019-11-05 15:32:58 -07002087 struct io_kiocb *nxt = NULL;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002088 __poll_t mask = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002089 int ret = 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002090
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002091 if (work->flags & IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06002092 WRITE_ONCE(poll->canceled, true);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002093 ret = -ECANCELED;
2094 } else if (READ_ONCE(poll->canceled)) {
2095 ret = -ECANCELED;
2096 }
Jens Axboe561fb042019-10-24 07:25:42 -06002097
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002098 if (ret != -ECANCELED)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002099 mask = vfs_poll(poll->file, &pt) & poll->events;
2100
2101 /*
2102 * Note that ->ki_cancel callers also delete iocb from active_reqs after
2103 * calling ->ki_cancel. We need the ctx_lock roundtrip here to
2104 * synchronize with them. In the cancellation case the list_del_init
2105 * itself is not actually needed, but harmless so we keep it in to
2106 * avoid further branches in the fast path.
2107 */
2108 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002109 if (!mask && ret != -ECANCELED) {
Jens Axboe221c5eb2019-01-17 09:41:58 -07002110 add_wait_queue(poll->head, &poll->wait);
2111 spin_unlock_irq(&ctx->completion_lock);
2112 return;
2113 }
Jens Axboeeac406c2019-11-14 12:09:58 -07002114 io_poll_remove_req(req);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002115 io_poll_complete(req, mask, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002116 spin_unlock_irq(&ctx->completion_lock);
2117
Jens Axboe8c838782019-03-12 15:48:16 -06002118 io_cqring_ev_posted(ctx);
Jens Axboe89723d02019-11-05 15:32:58 -07002119
Jens Axboefba38c22019-11-18 12:27:57 -07002120 if (ret < 0 && req->flags & REQ_F_LINK)
2121 req->flags |= REQ_F_FAIL_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08002122 io_put_req_find_next(req, &nxt);
Jens Axboe89723d02019-11-05 15:32:58 -07002123 if (nxt)
2124 *workptr = &nxt->work;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002125}
2126
2127static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
2128 void *key)
2129{
2130 struct io_poll_iocb *poll = container_of(wait, struct io_poll_iocb,
2131 wait);
2132 struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
2133 struct io_ring_ctx *ctx = req->ctx;
2134 __poll_t mask = key_to_poll(key);
Jens Axboe8c838782019-03-12 15:48:16 -06002135 unsigned long flags;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002136
2137 /* for instances that support it check for an event match first: */
Jens Axboe8c838782019-03-12 15:48:16 -06002138 if (mask && !(mask & poll->events))
2139 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002140
2141 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06002142
Jens Axboe7c9e7f02019-11-12 08:15:53 -07002143 /*
2144 * Run completion inline if we can. We're using trylock here because
2145 * we are violating the completion_lock -> poll wq lock ordering.
2146 * If we have a link timeout we're going to need the completion_lock
2147 * for finalizing the request, mark us as having grabbed that already.
2148 */
Jens Axboe8c838782019-03-12 15:48:16 -06002149 if (mask && spin_trylock_irqsave(&ctx->completion_lock, flags)) {
Jens Axboeeac406c2019-11-14 12:09:58 -07002150 io_poll_remove_req(req);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002151 io_poll_complete(req, mask, 0);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07002152 req->flags |= REQ_F_COMP_LOCKED;
2153 io_put_req(req);
Jens Axboe8c838782019-03-12 15:48:16 -06002154 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2155
2156 io_cqring_ev_posted(ctx);
Jens Axboe8c838782019-03-12 15:48:16 -06002157 } else {
Jackie Liua197f662019-11-08 08:09:12 -07002158 io_queue_async_work(req);
Jens Axboe8c838782019-03-12 15:48:16 -06002159 }
2160
Jens Axboe221c5eb2019-01-17 09:41:58 -07002161 return 1;
2162}
2163
2164struct io_poll_table {
2165 struct poll_table_struct pt;
2166 struct io_kiocb *req;
2167 int error;
2168};
2169
2170static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
2171 struct poll_table_struct *p)
2172{
2173 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
2174
2175 if (unlikely(pt->req->poll.head)) {
2176 pt->error = -EINVAL;
2177 return;
2178 }
2179
2180 pt->error = 0;
2181 pt->req->poll.head = head;
2182 add_wait_queue(head, &pt->req->poll.wait);
2183}
2184
Jens Axboeeac406c2019-11-14 12:09:58 -07002185static void io_poll_req_insert(struct io_kiocb *req)
2186{
2187 struct io_ring_ctx *ctx = req->ctx;
2188 struct rb_node **p = &ctx->cancel_tree.rb_node;
2189 struct rb_node *parent = NULL;
2190 struct io_kiocb *tmp;
2191
2192 while (*p) {
2193 parent = *p;
2194 tmp = rb_entry(parent, struct io_kiocb, rb_node);
2195 if (req->user_data < tmp->user_data)
2196 p = &(*p)->rb_left;
2197 else
2198 p = &(*p)->rb_right;
2199 }
2200 rb_link_node(&req->rb_node, parent, p);
2201 rb_insert_color(&req->rb_node, &ctx->cancel_tree);
2202}
2203
Jens Axboe89723d02019-11-05 15:32:58 -07002204static int io_poll_add(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2205 struct io_kiocb **nxt)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002206{
2207 struct io_poll_iocb *poll = &req->poll;
2208 struct io_ring_ctx *ctx = req->ctx;
2209 struct io_poll_table ipt;
Jens Axboe8c838782019-03-12 15:48:16 -06002210 bool cancel = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002211 __poll_t mask;
2212 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002213
2214 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2215 return -EINVAL;
2216 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
2217 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06002218 if (!poll->file)
2219 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002220
Jens Axboe6cc47d12019-09-18 11:18:23 -06002221 req->submit.sqe = NULL;
Jens Axboe561fb042019-10-24 07:25:42 -06002222 INIT_IO_WORK(&req->work, io_poll_complete_work);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002223 events = READ_ONCE(sqe->poll_events);
2224 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
Jens Axboeeac406c2019-11-14 12:09:58 -07002225 RB_CLEAR_NODE(&req->rb_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002226
Jens Axboe221c5eb2019-01-17 09:41:58 -07002227 poll->head = NULL;
Jens Axboe8c838782019-03-12 15:48:16 -06002228 poll->done = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002229 poll->canceled = false;
2230
2231 ipt.pt._qproc = io_poll_queue_proc;
2232 ipt.pt._key = poll->events;
2233 ipt.req = req;
2234 ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
2235
2236 /* initialized the list so that we can do list_empty checks */
2237 INIT_LIST_HEAD(&poll->wait.entry);
2238 init_waitqueue_func_entry(&poll->wait, io_poll_wake);
2239
Jens Axboe36703242019-07-25 10:20:18 -06002240 INIT_LIST_HEAD(&req->list);
2241
Jens Axboe221c5eb2019-01-17 09:41:58 -07002242 mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002243
2244 spin_lock_irq(&ctx->completion_lock);
Jens Axboe8c838782019-03-12 15:48:16 -06002245 if (likely(poll->head)) {
2246 spin_lock(&poll->head->lock);
2247 if (unlikely(list_empty(&poll->wait.entry))) {
2248 if (ipt.error)
2249 cancel = true;
2250 ipt.error = 0;
2251 mask = 0;
2252 }
2253 if (mask || ipt.error)
2254 list_del_init(&poll->wait.entry);
2255 else if (cancel)
2256 WRITE_ONCE(poll->canceled, true);
2257 else if (!poll->done) /* actually waiting for an event */
Jens Axboeeac406c2019-11-14 12:09:58 -07002258 io_poll_req_insert(req);
Jens Axboe8c838782019-03-12 15:48:16 -06002259 spin_unlock(&poll->head->lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002260 }
Jens Axboe8c838782019-03-12 15:48:16 -06002261 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06002262 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002263 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06002264 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07002265 spin_unlock_irq(&ctx->completion_lock);
2266
Jens Axboe8c838782019-03-12 15:48:16 -06002267 if (mask) {
2268 io_cqring_ev_posted(ctx);
Jackie Liuec9c02a2019-11-08 23:50:36 +08002269 io_put_req_find_next(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002270 }
Jens Axboe8c838782019-03-12 15:48:16 -06002271 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002272}
2273
Jens Axboe5262f562019-09-17 12:26:57 -06002274static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
2275{
Jens Axboead8a48a2019-11-15 08:49:11 -07002276 struct io_timeout_data *data = container_of(timer,
2277 struct io_timeout_data, timer);
2278 struct io_kiocb *req = data->req;
2279 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06002280 unsigned long flags;
2281
Jens Axboe5262f562019-09-17 12:26:57 -06002282 atomic_inc(&ctx->cq_timeouts);
2283
2284 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08002285 /*
Jens Axboe11365042019-10-16 09:08:32 -06002286 * We could be racing with timeout deletion. If the list is empty,
2287 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08002288 */
Jens Axboe842f9612019-10-29 12:34:10 -06002289 if (!list_empty(&req->list)) {
Jens Axboe11365042019-10-16 09:08:32 -06002290 struct io_kiocb *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06002291
Jens Axboe11365042019-10-16 09:08:32 -06002292 /*
2293 * Adjust the reqs sequence before the current one because it
2294 * will consume a slot in the cq_ring and the the cq_tail
2295 * pointer will be increased, otherwise other timeout reqs may
2296 * return in advance without waiting for enough wait_nr.
2297 */
2298 prev = req;
2299 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
2300 prev->sequence++;
Jens Axboe11365042019-10-16 09:08:32 -06002301 list_del_init(&req->list);
Jens Axboe11365042019-10-16 09:08:32 -06002302 }
Jens Axboe842f9612019-10-29 12:34:10 -06002303
Jens Axboe78e19bb2019-11-06 15:21:34 -07002304 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06002305 io_commit_cqring(ctx);
2306 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2307
2308 io_cqring_ev_posted(ctx);
Jens Axboef1f40852019-11-05 20:33:16 -07002309 if (req->flags & REQ_F_LINK)
2310 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe5262f562019-09-17 12:26:57 -06002311 io_put_req(req);
2312 return HRTIMER_NORESTART;
2313}
2314
Jens Axboe47f46762019-11-09 17:43:02 -07002315static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
2316{
2317 struct io_kiocb *req;
2318 int ret = -ENOENT;
2319
2320 list_for_each_entry(req, &ctx->timeout_list, list) {
2321 if (user_data == req->user_data) {
2322 list_del_init(&req->list);
2323 ret = 0;
2324 break;
2325 }
2326 }
2327
2328 if (ret == -ENOENT)
2329 return ret;
2330
Jens Axboead8a48a2019-11-15 08:49:11 -07002331 ret = hrtimer_try_to_cancel(&req->timeout.data->timer);
Jens Axboe47f46762019-11-09 17:43:02 -07002332 if (ret == -1)
2333 return -EALREADY;
2334
Jens Axboefba38c22019-11-18 12:27:57 -07002335 if (req->flags & REQ_F_LINK)
2336 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe47f46762019-11-09 17:43:02 -07002337 io_cqring_fill_event(req, -ECANCELED);
2338 io_put_req(req);
2339 return 0;
2340}
2341
Jens Axboe11365042019-10-16 09:08:32 -06002342/*
2343 * Remove or update an existing timeout command
2344 */
2345static int io_timeout_remove(struct io_kiocb *req,
2346 const struct io_uring_sqe *sqe)
2347{
2348 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe11365042019-10-16 09:08:32 -06002349 unsigned flags;
Jens Axboe47f46762019-11-09 17:43:02 -07002350 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06002351
2352 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2353 return -EINVAL;
2354 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
2355 return -EINVAL;
2356 flags = READ_ONCE(sqe->timeout_flags);
2357 if (flags)
2358 return -EINVAL;
2359
Jens Axboe11365042019-10-16 09:08:32 -06002360 spin_lock_irq(&ctx->completion_lock);
Jens Axboe47f46762019-11-09 17:43:02 -07002361 ret = io_timeout_cancel(ctx, READ_ONCE(sqe->addr));
Jens Axboe11365042019-10-16 09:08:32 -06002362
Jens Axboe47f46762019-11-09 17:43:02 -07002363 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06002364 io_commit_cqring(ctx);
2365 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06002366 io_cqring_ev_posted(ctx);
Jens Axboe47f46762019-11-09 17:43:02 -07002367 if (ret < 0 && req->flags & REQ_F_LINK)
2368 req->flags |= REQ_F_FAIL_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08002369 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06002370 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06002371}
2372
Jens Axboead8a48a2019-11-15 08:49:11 -07002373static int io_timeout_setup(struct io_kiocb *req)
Jens Axboe5262f562019-09-17 12:26:57 -06002374{
Jens Axboead8a48a2019-11-15 08:49:11 -07002375 const struct io_uring_sqe *sqe = req->submit.sqe;
2376 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06002377 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06002378
Jens Axboead8a48a2019-11-15 08:49:11 -07002379 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06002380 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07002381 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06002382 return -EINVAL;
2383 flags = READ_ONCE(sqe->timeout_flags);
2384 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06002385 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06002386
Jens Axboead8a48a2019-11-15 08:49:11 -07002387 data = kzalloc(sizeof(struct io_timeout_data), GFP_KERNEL);
2388 if (!data)
2389 return -ENOMEM;
2390 data->req = req;
2391 req->timeout.data = data;
2392 req->flags |= REQ_F_TIMEOUT;
2393
2394 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06002395 return -EFAULT;
2396
Jens Axboe11365042019-10-16 09:08:32 -06002397 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07002398 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06002399 else
Jens Axboead8a48a2019-11-15 08:49:11 -07002400 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06002401
Jens Axboead8a48a2019-11-15 08:49:11 -07002402 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
2403 return 0;
2404}
2405
2406static int io_timeout(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2407{
2408 unsigned count;
2409 struct io_ring_ctx *ctx = req->ctx;
2410 struct io_timeout_data *data;
2411 struct list_head *entry;
2412 unsigned span = 0;
2413 int ret;
2414
2415 ret = io_timeout_setup(req);
2416 /* common setup allows flags (like links) set, we don't */
2417 if (!ret && sqe->flags)
2418 ret = -EINVAL;
2419 if (ret)
2420 return ret;
Jens Axboe93bd25b2019-11-11 23:34:31 -07002421
Jens Axboe5262f562019-09-17 12:26:57 -06002422 /*
2423 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07002424 * timeout event to be satisfied. If it isn't set, then this is
2425 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06002426 */
2427 count = READ_ONCE(sqe->off);
Jens Axboe93bd25b2019-11-11 23:34:31 -07002428 if (!count) {
2429 req->flags |= REQ_F_TIMEOUT_NOSEQ;
2430 spin_lock_irq(&ctx->completion_lock);
2431 entry = ctx->timeout_list.prev;
2432 goto add;
2433 }
Jens Axboe5262f562019-09-17 12:26:57 -06002434
2435 req->sequence = ctx->cached_sq_head + count - 1;
yangerkun5da0fb12019-10-15 21:59:29 +08002436 /* reuse it to store the count */
2437 req->submit.sequence = count;
Jens Axboe5262f562019-09-17 12:26:57 -06002438
2439 /*
2440 * Insertion sort, ensuring the first entry in the list is always
2441 * the one we need first.
2442 */
Jens Axboe5262f562019-09-17 12:26:57 -06002443 spin_lock_irq(&ctx->completion_lock);
2444 list_for_each_prev(entry, &ctx->timeout_list) {
2445 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
yangerkun5da0fb12019-10-15 21:59:29 +08002446 unsigned nxt_sq_head;
2447 long long tmp, tmp_nxt;
Jens Axboe5262f562019-09-17 12:26:57 -06002448
Jens Axboe93bd25b2019-11-11 23:34:31 -07002449 if (nxt->flags & REQ_F_TIMEOUT_NOSEQ)
2450 continue;
2451
yangerkun5da0fb12019-10-15 21:59:29 +08002452 /*
2453 * Since cached_sq_head + count - 1 can overflow, use type long
2454 * long to store it.
2455 */
2456 tmp = (long long)ctx->cached_sq_head + count - 1;
2457 nxt_sq_head = nxt->sequence - nxt->submit.sequence + 1;
2458 tmp_nxt = (long long)nxt_sq_head + nxt->submit.sequence - 1;
2459
2460 /*
2461 * cached_sq_head may overflow, and it will never overflow twice
2462 * once there is some timeout req still be valid.
2463 */
2464 if (ctx->cached_sq_head < nxt_sq_head)
yangerkun8b07a652019-10-17 12:12:35 +08002465 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08002466
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002467 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06002468 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002469
2470 /*
2471 * Sequence of reqs after the insert one and itself should
2472 * be adjusted because each timeout req consumes a slot.
2473 */
2474 span++;
2475 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06002476 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002477 req->sequence -= span;
Jens Axboe93bd25b2019-11-11 23:34:31 -07002478add:
Jens Axboe5262f562019-09-17 12:26:57 -06002479 list_add(&req->list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07002480 data = req->timeout.data;
2481 data->timer.function = io_timeout_fn;
2482 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06002483 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06002484 return 0;
2485}
2486
Jens Axboe62755e32019-10-28 21:49:21 -06002487static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06002488{
Jens Axboe62755e32019-10-28 21:49:21 -06002489 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06002490
Jens Axboe62755e32019-10-28 21:49:21 -06002491 return req->user_data == (unsigned long) data;
2492}
2493
Jens Axboee977d6d2019-11-05 12:39:45 -07002494static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06002495{
Jens Axboe62755e32019-10-28 21:49:21 -06002496 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06002497 int ret = 0;
2498
Jens Axboe62755e32019-10-28 21:49:21 -06002499 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
2500 switch (cancel_ret) {
2501 case IO_WQ_CANCEL_OK:
2502 ret = 0;
2503 break;
2504 case IO_WQ_CANCEL_RUNNING:
2505 ret = -EALREADY;
2506 break;
2507 case IO_WQ_CANCEL_NOTFOUND:
2508 ret = -ENOENT;
2509 break;
2510 }
2511
Jens Axboee977d6d2019-11-05 12:39:45 -07002512 return ret;
2513}
2514
Jens Axboe47f46762019-11-09 17:43:02 -07002515static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
2516 struct io_kiocb *req, __u64 sqe_addr,
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002517 struct io_kiocb **nxt, int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07002518{
2519 unsigned long flags;
2520 int ret;
2521
2522 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
2523 if (ret != -ENOENT) {
2524 spin_lock_irqsave(&ctx->completion_lock, flags);
2525 goto done;
2526 }
2527
2528 spin_lock_irqsave(&ctx->completion_lock, flags);
2529 ret = io_timeout_cancel(ctx, sqe_addr);
2530 if (ret != -ENOENT)
2531 goto done;
2532 ret = io_poll_cancel(ctx, sqe_addr);
2533done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002534 if (!ret)
2535 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07002536 io_cqring_fill_event(req, ret);
2537 io_commit_cqring(ctx);
2538 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2539 io_cqring_ev_posted(ctx);
2540
2541 if (ret < 0 && (req->flags & REQ_F_LINK))
2542 req->flags |= REQ_F_FAIL_LINK;
2543 io_put_req_find_next(req, nxt);
2544}
2545
Jens Axboee977d6d2019-11-05 12:39:45 -07002546static int io_async_cancel(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2547 struct io_kiocb **nxt)
2548{
2549 struct io_ring_ctx *ctx = req->ctx;
Jens Axboee977d6d2019-11-05 12:39:45 -07002550
2551 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2552 return -EINVAL;
2553 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
2554 sqe->cancel_flags)
2555 return -EINVAL;
2556
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002557 io_async_find_and_cancel(ctx, req, READ_ONCE(sqe->addr), nxt, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06002558 return 0;
2559}
2560
Jackie Liua197f662019-11-08 08:09:12 -07002561static int io_req_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -06002562{
Pavel Begunkov267bc902019-11-07 01:41:08 +03002563 const struct io_uring_sqe *sqe = req->submit.sqe;
Jens Axboede0617e2019-04-06 21:51:27 -06002564 struct io_uring_sqe *sqe_copy;
Jackie Liua197f662019-11-08 08:09:12 -07002565 struct io_ring_ctx *ctx = req->ctx;
Jens Axboede0617e2019-04-06 21:51:27 -06002566
Bob Liu9d858b22019-11-13 18:06:25 +08002567 /* Still need defer if there is pending req in defer list. */
2568 if (!req_need_defer(req) && list_empty(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06002569 return 0;
2570
2571 sqe_copy = kmalloc(sizeof(*sqe_copy), GFP_KERNEL);
2572 if (!sqe_copy)
2573 return -EAGAIN;
2574
2575 spin_lock_irq(&ctx->completion_lock);
Bob Liu9d858b22019-11-13 18:06:25 +08002576 if (!req_need_defer(req) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06002577 spin_unlock_irq(&ctx->completion_lock);
2578 kfree(sqe_copy);
2579 return 0;
2580 }
2581
2582 memcpy(sqe_copy, sqe, sizeof(*sqe_copy));
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03002583 req->flags |= REQ_F_FREE_SQE;
Jens Axboede0617e2019-04-06 21:51:27 -06002584 req->submit.sqe = sqe_copy;
2585
Jens Axboe915967f2019-11-21 09:01:20 -07002586 trace_io_uring_defer(ctx, req, req->user_data);
Jens Axboede0617e2019-04-06 21:51:27 -06002587 list_add_tail(&req->list, &ctx->defer_list);
2588 spin_unlock_irq(&ctx->completion_lock);
2589 return -EIOCBQUEUED;
2590}
2591
Pavel Begunkovd7324472019-11-21 21:24:36 +03002592static int io_issue_sqe(struct io_kiocb *req, struct io_kiocb **nxt,
2593 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002594{
Jens Axboee0c5c572019-03-12 10:18:47 -06002595 int ret, opcode;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002596 struct sqe_submit *s = &req->submit;
Jackie Liua197f662019-11-08 08:09:12 -07002597 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002598
2599 opcode = READ_ONCE(s->sqe->opcode);
2600 switch (opcode) {
2601 case IORING_OP_NOP:
Jens Axboe78e19bb2019-11-06 15:21:34 -07002602 ret = io_nop(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002603 break;
2604 case IORING_OP_READV:
Jens Axboeedafcce2019-01-09 09:16:05 -07002605 if (unlikely(s->sqe->buf_index))
2606 return -EINVAL;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002607 ret = io_read(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002608 break;
2609 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07002610 if (unlikely(s->sqe->buf_index))
2611 return -EINVAL;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002612 ret = io_write(req, nxt, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07002613 break;
2614 case IORING_OP_READ_FIXED:
Pavel Begunkov267bc902019-11-07 01:41:08 +03002615 ret = io_read(req, nxt, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07002616 break;
2617 case IORING_OP_WRITE_FIXED:
Pavel Begunkov267bc902019-11-07 01:41:08 +03002618 ret = io_write(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002619 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002620 case IORING_OP_FSYNC:
Jens Axboeba816ad2019-09-28 11:36:45 -06002621 ret = io_fsync(req, s->sqe, nxt, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002622 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002623 case IORING_OP_POLL_ADD:
Jens Axboe89723d02019-11-05 15:32:58 -07002624 ret = io_poll_add(req, s->sqe, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002625 break;
2626 case IORING_OP_POLL_REMOVE:
2627 ret = io_poll_remove(req, s->sqe);
2628 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002629 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboeba816ad2019-09-28 11:36:45 -06002630 ret = io_sync_file_range(req, s->sqe, nxt, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002631 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002632 case IORING_OP_SENDMSG:
Jens Axboeba816ad2019-09-28 11:36:45 -06002633 ret = io_sendmsg(req, s->sqe, nxt, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06002634 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06002635 case IORING_OP_RECVMSG:
Jens Axboeba816ad2019-09-28 11:36:45 -06002636 ret = io_recvmsg(req, s->sqe, nxt, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06002637 break;
Jens Axboe5262f562019-09-17 12:26:57 -06002638 case IORING_OP_TIMEOUT:
2639 ret = io_timeout(req, s->sqe);
2640 break;
Jens Axboe11365042019-10-16 09:08:32 -06002641 case IORING_OP_TIMEOUT_REMOVE:
2642 ret = io_timeout_remove(req, s->sqe);
2643 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06002644 case IORING_OP_ACCEPT:
2645 ret = io_accept(req, s->sqe, nxt, force_nonblock);
2646 break;
Jens Axboe62755e32019-10-28 21:49:21 -06002647 case IORING_OP_ASYNC_CANCEL:
2648 ret = io_async_cancel(req, s->sqe, nxt);
2649 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002650 default:
2651 ret = -EINVAL;
2652 break;
2653 }
2654
Jens Axboedef596e2019-01-09 08:59:42 -07002655 if (ret)
2656 return ret;
2657
2658 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe9e645e112019-05-10 16:07:28 -06002659 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07002660 return -EAGAIN;
2661
2662 /* workqueue context doesn't hold uring_lock, grab it now */
Jackie Liuba5290c2019-10-09 09:19:59 +08002663 if (s->in_async)
Jens Axboedef596e2019-01-09 08:59:42 -07002664 mutex_lock(&ctx->uring_lock);
2665 io_iopoll_req_issued(req);
Jackie Liuba5290c2019-10-09 09:19:59 +08002666 if (s->in_async)
Jens Axboedef596e2019-01-09 08:59:42 -07002667 mutex_unlock(&ctx->uring_lock);
2668 }
2669
2670 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002671}
2672
Jens Axboeb76da702019-11-20 13:05:32 -07002673static void io_link_work_cb(struct io_wq_work **workptr)
2674{
2675 struct io_wq_work *work = *workptr;
2676 struct io_kiocb *link = work->data;
2677
2678 io_queue_linked_timeout(link);
2679 work->func = io_wq_submit_work;
2680}
2681
Jens Axboe561fb042019-10-24 07:25:42 -06002682static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboe31b51512019-01-18 22:56:34 -07002683{
Jens Axboe561fb042019-10-24 07:25:42 -06002684 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002685 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe561fb042019-10-24 07:25:42 -06002686 struct sqe_submit *s = &req->submit;
Jens Axboe561fb042019-10-24 07:25:42 -06002687 struct io_kiocb *nxt = NULL;
2688 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002689
Jens Axboe561fb042019-10-24 07:25:42 -06002690 /* Ensure we clear previously set non-block flag */
2691 req->rw.ki_flags &= ~IOCB_NOWAIT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002692
Jens Axboe561fb042019-10-24 07:25:42 -06002693 if (work->flags & IO_WQ_WORK_CANCEL)
2694 ret = -ECANCELED;
Jens Axboe31b51512019-01-18 22:56:34 -07002695
Jens Axboe561fb042019-10-24 07:25:42 -06002696 if (!ret) {
2697 s->has_user = (work->flags & IO_WQ_WORK_HAS_MM) != 0;
2698 s->in_async = true;
2699 do {
Pavel Begunkovd7324472019-11-21 21:24:36 +03002700 ret = io_issue_sqe(req, &nxt, false);
Jens Axboe561fb042019-10-24 07:25:42 -06002701 /*
2702 * We can get EAGAIN for polled IO even though we're
2703 * forcing a sync submission from here, since we can't
2704 * wait for request slots on the block side.
2705 */
2706 if (ret != -EAGAIN)
2707 break;
2708 cond_resched();
2709 } while (1);
2710 }
Jens Axboe31b51512019-01-18 22:56:34 -07002711
Jens Axboe561fb042019-10-24 07:25:42 -06002712 /* drop submission reference */
Jackie Liuec9c02a2019-11-08 23:50:36 +08002713 io_put_req(req);
Jens Axboe817869d2019-04-30 14:44:05 -06002714
Jens Axboe561fb042019-10-24 07:25:42 -06002715 if (ret) {
Jens Axboef1f40852019-11-05 20:33:16 -07002716 if (req->flags & REQ_F_LINK)
2717 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07002718 io_cqring_add_event(req, ret);
Jens Axboe817869d2019-04-30 14:44:05 -06002719 io_put_req(req);
Jens Axboeedafcce2019-01-09 09:16:05 -07002720 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002721
Jens Axboe561fb042019-10-24 07:25:42 -06002722 /* if a dependent link is ready, pass it back */
2723 if (!ret && nxt) {
Jens Axboe94ae5e72019-11-14 19:39:52 -07002724 struct io_kiocb *link;
2725
2726 io_prep_async_work(nxt, &link);
Jens Axboe561fb042019-10-24 07:25:42 -06002727 *workptr = &nxt->work;
Jens Axboeb76da702019-11-20 13:05:32 -07002728 if (link) {
2729 nxt->work.flags |= IO_WQ_WORK_CB;
2730 nxt->work.func = io_link_work_cb;
2731 nxt->work.data = link;
2732 }
Jens Axboeedafcce2019-01-09 09:16:05 -07002733 }
Jens Axboe31b51512019-01-18 22:56:34 -07002734}
Jens Axboe2b188cc2019-01-07 10:46:33 -07002735
Jens Axboe09bb8392019-03-13 12:39:28 -06002736static bool io_op_needs_file(const struct io_uring_sqe *sqe)
2737{
2738 int op = READ_ONCE(sqe->opcode);
2739
2740 switch (op) {
2741 case IORING_OP_NOP:
2742 case IORING_OP_POLL_REMOVE:
Pavel Begunkov5683e542019-11-14 00:59:19 +03002743 case IORING_OP_TIMEOUT:
Pavel Begunkova320e9f2019-11-14 00:11:01 +03002744 case IORING_OP_TIMEOUT_REMOVE:
2745 case IORING_OP_ASYNC_CANCEL:
2746 case IORING_OP_LINK_TIMEOUT:
Jens Axboe09bb8392019-03-13 12:39:28 -06002747 return false;
2748 default:
2749 return true;
2750 }
2751}
2752
Jens Axboe65e19f52019-10-26 07:20:21 -06002753static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
2754 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06002755{
Jens Axboe65e19f52019-10-26 07:20:21 -06002756 struct fixed_file_table *table;
2757
2758 table = &ctx->file_table[index >> IORING_FILE_TABLE_SHIFT];
2759 return table->files[index & IORING_FILE_TABLE_MASK];
2760}
2761
Jackie Liua197f662019-11-08 08:09:12 -07002762static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req)
Jens Axboe09bb8392019-03-13 12:39:28 -06002763{
Pavel Begunkov267bc902019-11-07 01:41:08 +03002764 struct sqe_submit *s = &req->submit;
Jackie Liua197f662019-11-08 08:09:12 -07002765 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe09bb8392019-03-13 12:39:28 -06002766 unsigned flags;
2767 int fd;
2768
2769 flags = READ_ONCE(s->sqe->flags);
2770 fd = READ_ONCE(s->sqe->fd);
2771
Jackie Liu4fe2c962019-09-09 20:50:40 +08002772 if (flags & IOSQE_IO_DRAIN)
Jens Axboede0617e2019-04-06 21:51:27 -06002773 req->flags |= REQ_F_IO_DRAIN;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002774 /*
2775 * All io need record the previous position, if LINK vs DARIN,
2776 * it can be used to mark the position of the first IO in the
2777 * link list.
2778 */
2779 req->sequence = s->sequence;
Jens Axboede0617e2019-04-06 21:51:27 -06002780
Jens Axboe60c112b2019-06-21 10:20:18 -06002781 if (!io_op_needs_file(s->sqe))
Jens Axboe09bb8392019-03-13 12:39:28 -06002782 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06002783
2784 if (flags & IOSQE_FIXED_FILE) {
Jens Axboe65e19f52019-10-26 07:20:21 -06002785 if (unlikely(!ctx->file_table ||
Jens Axboe09bb8392019-03-13 12:39:28 -06002786 (unsigned) fd >= ctx->nr_user_files))
2787 return -EBADF;
Jens Axboeb7620122019-10-26 07:22:55 -06002788 fd = array_index_nospec(fd, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06002789 req->file = io_file_from_index(ctx, fd);
2790 if (!req->file)
Jens Axboe08a45172019-10-03 08:11:03 -06002791 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06002792 req->flags |= REQ_F_FIXED_FILE;
2793 } else {
2794 if (s->needs_fixed_file)
2795 return -EBADF;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002796 trace_io_uring_file_get(ctx, fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06002797 req->file = io_file_get(state, fd);
2798 if (unlikely(!req->file))
2799 return -EBADF;
2800 }
2801
2802 return 0;
2803}
2804
Jackie Liua197f662019-11-08 08:09:12 -07002805static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002806{
Jens Axboefcb323c2019-10-24 12:39:47 -06002807 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07002808 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06002809
2810 rcu_read_lock();
2811 spin_lock_irq(&ctx->inflight_lock);
2812 /*
2813 * We use the f_ops->flush() handler to ensure that we can flush
2814 * out work accessing these files if the fd is closed. Check if
2815 * the fd has changed since we started down this path, and disallow
2816 * this operation if it has.
2817 */
2818 if (fcheck(req->submit.ring_fd) == req->submit.ring_file) {
2819 list_add(&req->inflight_entry, &ctx->inflight_list);
2820 req->flags |= REQ_F_INFLIGHT;
2821 req->work.files = current->files;
2822 ret = 0;
2823 }
2824 spin_unlock_irq(&ctx->inflight_lock);
2825 rcu_read_unlock();
2826
2827 return ret;
2828}
2829
Jens Axboe2665abf2019-11-05 12:40:47 -07002830static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
2831{
Jens Axboead8a48a2019-11-15 08:49:11 -07002832 struct io_timeout_data *data = container_of(timer,
2833 struct io_timeout_data, timer);
2834 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07002835 struct io_ring_ctx *ctx = req->ctx;
2836 struct io_kiocb *prev = NULL;
2837 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07002838
2839 spin_lock_irqsave(&ctx->completion_lock, flags);
2840
2841 /*
2842 * We don't expect the list to be empty, that will only happen if we
2843 * race with the completion of the linked work.
2844 */
2845 if (!list_empty(&req->list)) {
2846 prev = list_entry(req->list.prev, struct io_kiocb, link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07002847 if (refcount_inc_not_zero(&prev->refs)) {
Jens Axboe76a46e02019-11-10 23:34:16 -07002848 list_del_init(&req->list);
Jens Axboe5d960722019-11-19 15:31:28 -07002849 prev->flags &= ~REQ_F_LINK_TIMEOUT;
2850 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07002851 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07002852 }
2853
2854 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2855
2856 if (prev) {
Jens Axboefba38c22019-11-18 12:27:57 -07002857 if (prev->flags & REQ_F_LINK)
2858 prev->flags |= REQ_F_FAIL_LINK;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002859 io_async_find_and_cancel(ctx, req, prev->user_data, NULL,
2860 -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07002861 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07002862 } else {
2863 io_cqring_add_event(req, -ETIME);
2864 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07002865 }
Jens Axboe2665abf2019-11-05 12:40:47 -07002866 return HRTIMER_NORESTART;
2867}
2868
Jens Axboead8a48a2019-11-15 08:49:11 -07002869static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07002870{
Jens Axboe76a46e02019-11-10 23:34:16 -07002871 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07002872
Jens Axboe76a46e02019-11-10 23:34:16 -07002873 /*
2874 * If the list is now empty, then our linked request finished before
2875 * we got a chance to setup the timer
2876 */
2877 spin_lock_irq(&ctx->completion_lock);
2878 if (!list_empty(&req->list)) {
Jens Axboe94ae5e72019-11-14 19:39:52 -07002879 struct io_timeout_data *data = req->timeout.data;
2880
Jens Axboead8a48a2019-11-15 08:49:11 -07002881 data->timer.function = io_link_timeout_fn;
2882 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
2883 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07002884 }
Jens Axboe76a46e02019-11-10 23:34:16 -07002885 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07002886
Jens Axboe2665abf2019-11-05 12:40:47 -07002887 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07002888 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07002889}
2890
Jens Axboead8a48a2019-11-15 08:49:11 -07002891static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07002892{
2893 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002894
Jens Axboe2665abf2019-11-05 12:40:47 -07002895 if (!(req->flags & REQ_F_LINK))
2896 return NULL;
2897
2898 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, list);
Jens Axboe76a46e02019-11-10 23:34:16 -07002899 if (!nxt || nxt->submit.sqe->opcode != IORING_OP_LINK_TIMEOUT)
2900 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07002901
Jens Axboe76a46e02019-11-10 23:34:16 -07002902 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07002903 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07002904}
2905
Jens Axboe0e0702d2019-11-14 21:42:10 -07002906static void __io_queue_sqe(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002907{
Jens Axboe94ae5e72019-11-14 19:39:52 -07002908 struct io_kiocb *nxt = io_prep_linked_timeout(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002909 int ret;
2910
Pavel Begunkovd7324472019-11-21 21:24:36 +03002911 ret = io_issue_sqe(req, NULL, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06002912
2913 /*
2914 * We async punt it if the file wasn't marked NOWAIT, or if the file
2915 * doesn't support non-blocking read/write attempts
2916 */
2917 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
2918 (req->flags & REQ_F_MUST_PUNT))) {
Pavel Begunkov267bc902019-11-07 01:41:08 +03002919 struct sqe_submit *s = &req->submit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002920 struct io_uring_sqe *sqe_copy;
2921
Jackie Liu954dab12019-09-18 10:37:52 +08002922 sqe_copy = kmemdup(s->sqe, sizeof(*sqe_copy), GFP_KERNEL);
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03002923 if (!sqe_copy)
2924 goto err;
Jens Axboee65ef562019-03-12 10:16:44 -06002925
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03002926 s->sqe = sqe_copy;
2927 req->flags |= REQ_F_FREE_SQE;
2928
2929 if (req->work.flags & IO_WQ_WORK_NEEDS_FILES) {
2930 ret = io_grab_files(req);
2931 if (ret)
2932 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002933 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03002934
2935 /*
2936 * Queued up for async execution, worker will release
2937 * submit reference when the iocb is actually submitted.
2938 */
2939 io_queue_async_work(req);
2940 return;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002941 }
Jens Axboee65ef562019-03-12 10:16:44 -06002942
Jens Axboefcb323c2019-10-24 12:39:47 -06002943err:
Jens Axboee65ef562019-03-12 10:16:44 -06002944 /* drop submission reference */
2945 io_put_req(req);
2946
Jens Axboe76a46e02019-11-10 23:34:16 -07002947 if (nxt) {
2948 if (!ret)
Jens Axboead8a48a2019-11-15 08:49:11 -07002949 io_queue_linked_timeout(nxt);
Jens Axboe76a46e02019-11-10 23:34:16 -07002950 else
2951 io_put_req(nxt);
2952 }
2953
Jens Axboee65ef562019-03-12 10:16:44 -06002954 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06002955 if (ret) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07002956 io_cqring_add_event(req, ret);
Jens Axboe9e645e112019-05-10 16:07:28 -06002957 if (req->flags & REQ_F_LINK)
2958 req->flags |= REQ_F_FAIL_LINK;
Jens Axboee65ef562019-03-12 10:16:44 -06002959 io_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002960 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002961}
2962
Jens Axboe0e0702d2019-11-14 21:42:10 -07002963static void io_queue_sqe(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08002964{
2965 int ret;
2966
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03002967 if (unlikely(req->ctx->drain_next)) {
2968 req->flags |= REQ_F_IO_DRAIN;
2969 req->ctx->drain_next = false;
2970 }
2971 req->ctx->drain_next = (req->flags & REQ_F_DRAIN_LINK);
2972
Jackie Liua197f662019-11-08 08:09:12 -07002973 ret = io_req_defer(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002974 if (ret) {
2975 if (ret != -EIOCBQUEUED) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07002976 io_cqring_add_event(req, ret);
Pavel Begunkovd3b357962019-11-19 23:32:48 +03002977 if (req->flags & REQ_F_LINK)
2978 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07002979 io_double_put_req(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002980 }
Jens Axboe0e0702d2019-11-14 21:42:10 -07002981 } else
2982 __io_queue_sqe(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002983}
2984
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03002985static inline void io_queue_link_head(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08002986{
Jens Axboe94ae5e72019-11-14 19:39:52 -07002987 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03002988 io_cqring_add_event(req, -ECANCELED);
2989 io_double_put_req(req);
2990 } else
Jens Axboe0e0702d2019-11-14 21:42:10 -07002991 io_queue_sqe(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002992}
2993
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03002994
Jens Axboe9e645e112019-05-10 16:07:28 -06002995#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK)
2996
Jackie Liua197f662019-11-08 08:09:12 -07002997static void io_submit_sqe(struct io_kiocb *req, struct io_submit_state *state,
2998 struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06002999{
Pavel Begunkov267bc902019-11-07 01:41:08 +03003000 struct sqe_submit *s = &req->submit;
Jackie Liua197f662019-11-08 08:09:12 -07003001 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -06003002 int ret;
3003
Jens Axboe78e19bb2019-11-06 15:21:34 -07003004 req->user_data = s->sqe->user_data;
3005
Jens Axboe9e645e112019-05-10 16:07:28 -06003006 /* enforce forwards compatibility on users */
3007 if (unlikely(s->sqe->flags & ~SQE_VALID_FLAGS)) {
3008 ret = -EINVAL;
Pavel Begunkov196be952019-11-07 01:41:06 +03003009 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06003010 }
3011
Jackie Liua197f662019-11-08 08:09:12 -07003012 ret = io_req_set_file(state, req);
Jens Axboe9e645e112019-05-10 16:07:28 -06003013 if (unlikely(ret)) {
3014err_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07003015 io_cqring_add_event(req, ret);
3016 io_double_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06003017 return;
3018 }
3019
Jens Axboe9e645e112019-05-10 16:07:28 -06003020 /*
3021 * If we already have a head request, queue this one for async
3022 * submittal once the head completes. If we don't have a head but
3023 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
3024 * submitted sync once the chain is complete. If none of those
3025 * conditions are true (normal request), then just queue it.
3026 */
3027 if (*link) {
3028 struct io_kiocb *prev = *link;
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03003029 struct io_uring_sqe *sqe_copy;
Jens Axboe9e645e112019-05-10 16:07:28 -06003030
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003031 if (s->sqe->flags & IOSQE_IO_DRAIN)
3032 (*link)->flags |= REQ_F_DRAIN_LINK | REQ_F_IO_DRAIN;
3033
Jens Axboe94ae5e72019-11-14 19:39:52 -07003034 if (READ_ONCE(s->sqe->opcode) == IORING_OP_LINK_TIMEOUT) {
3035 ret = io_timeout_setup(req);
3036 /* common setup allows offset being set, we don't */
3037 if (!ret && s->sqe->off)
3038 ret = -EINVAL;
3039 if (ret) {
3040 prev->flags |= REQ_F_FAIL_LINK;
3041 goto err_req;
3042 }
3043 }
3044
Jens Axboe9e645e112019-05-10 16:07:28 -06003045 sqe_copy = kmemdup(s->sqe, sizeof(*sqe_copy), GFP_KERNEL);
3046 if (!sqe_copy) {
3047 ret = -EAGAIN;
3048 goto err_req;
3049 }
3050
3051 s->sqe = sqe_copy;
Jens Axboe94ae5e72019-11-14 19:39:52 -07003052 req->flags |= REQ_F_FREE_SQE;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02003053 trace_io_uring_link(ctx, req, prev);
Jens Axboe9e645e112019-05-10 16:07:28 -06003054 list_add_tail(&req->list, &prev->link_list);
3055 } else if (s->sqe->flags & IOSQE_IO_LINK) {
3056 req->flags |= REQ_F_LINK;
3057
Jens Axboe9e645e112019-05-10 16:07:28 -06003058 INIT_LIST_HEAD(&req->link_list);
3059 *link = req;
3060 } else {
Jackie Liua197f662019-11-08 08:09:12 -07003061 io_queue_sqe(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06003062 }
3063}
3064
Jens Axboe9a56a232019-01-09 09:06:50 -07003065/*
3066 * Batched submission is done, ensure local IO is flushed out.
3067 */
3068static void io_submit_state_end(struct io_submit_state *state)
3069{
3070 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06003071 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07003072 if (state->free_reqs)
3073 kmem_cache_free_bulk(req_cachep, state->free_reqs,
3074 &state->reqs[state->cur_req]);
Jens Axboe9a56a232019-01-09 09:06:50 -07003075}
3076
3077/*
3078 * Start submission side cache.
3079 */
3080static void io_submit_state_start(struct io_submit_state *state,
3081 struct io_ring_ctx *ctx, unsigned max_ios)
3082{
3083 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07003084 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07003085 state->file = NULL;
3086 state->ios_left = max_ios;
3087}
3088
Jens Axboe2b188cc2019-01-07 10:46:33 -07003089static void io_commit_sqring(struct io_ring_ctx *ctx)
3090{
Hristo Venev75b28af2019-08-26 17:23:46 +00003091 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003092
Hristo Venev75b28af2019-08-26 17:23:46 +00003093 if (ctx->cached_sq_head != READ_ONCE(rings->sq.head)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07003094 /*
3095 * Ensure any loads from the SQEs are done at this point,
3096 * since once we write the new head, the application could
3097 * write new data to them.
3098 */
Hristo Venev75b28af2019-08-26 17:23:46 +00003099 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003100 }
3101}
3102
3103/*
Jens Axboe2b188cc2019-01-07 10:46:33 -07003104 * Fetch an sqe, if one is available. Note that s->sqe will point to memory
3105 * that is mapped by userspace. This means that care needs to be taken to
3106 * ensure that reads are stable, as we cannot rely on userspace always
3107 * being a good citizen. If members of the sqe are validated and then later
3108 * used, it's important that those reads are done through READ_ONCE() to
3109 * prevent a re-load down the line.
3110 */
3111static bool io_get_sqring(struct io_ring_ctx *ctx, struct sqe_submit *s)
3112{
Hristo Venev75b28af2019-08-26 17:23:46 +00003113 struct io_rings *rings = ctx->rings;
3114 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003115 unsigned head;
3116
3117 /*
3118 * The cached sq head (or cq tail) serves two purposes:
3119 *
3120 * 1) allows us to batch the cost of updating the user visible
3121 * head updates.
3122 * 2) allows the kernel side to track the head on its own, even
3123 * though the application is the one updating it.
3124 */
3125 head = ctx->cached_sq_head;
Stefan Bühlere523a292019-04-19 11:57:44 +02003126 /* make sure SQ entry isn't read before tail */
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03003127 if (unlikely(head == smp_load_acquire(&rings->sq.tail)))
Jens Axboe2b188cc2019-01-07 10:46:33 -07003128 return false;
3129
Hristo Venev75b28af2019-08-26 17:23:46 +00003130 head = READ_ONCE(sq_array[head & ctx->sq_mask]);
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03003131 if (likely(head < ctx->sq_entries)) {
Jens Axboefcb323c2019-10-24 12:39:47 -06003132 s->ring_file = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003133 s->sqe = &ctx->sq_sqes[head];
Jackie Liu8776f3f2019-09-09 20:50:39 +08003134 s->sequence = ctx->cached_sq_head;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003135 ctx->cached_sq_head++;
3136 return true;
3137 }
3138
3139 /* drop invalid entries */
3140 ctx->cached_sq_head++;
Jens Axboe498ccd92019-10-25 10:04:25 -06003141 ctx->cached_sq_dropped++;
3142 WRITE_ONCE(rings->sq_dropped, ctx->cached_sq_dropped);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003143 return false;
3144}
3145
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003146static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkovae9428c2019-11-06 00:22:14 +03003147 struct file *ring_file, int ring_fd,
3148 struct mm_struct **mm, bool async)
Jens Axboe6c271ce2019-01-10 11:22:30 -07003149{
3150 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06003151 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06003152 int i, submitted = 0;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03003153 bool mm_fault = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003154
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003155 if (!list_empty(&ctx->cq_overflow_list)) {
3156 io_cqring_overflow_flush(ctx, false);
3157 return -EBUSY;
3158 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07003159
3160 if (nr > IO_PLUG_THRESHOLD) {
3161 io_submit_state_start(&state, ctx, nr);
3162 statep = &state;
3163 }
3164
3165 for (i = 0; i < nr; i++) {
Pavel Begunkov196be952019-11-07 01:41:06 +03003166 struct io_kiocb *req;
Pavel Begunkov50585b92019-11-07 01:41:07 +03003167 unsigned int sqe_flags;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003168
Pavel Begunkov196be952019-11-07 01:41:06 +03003169 req = io_get_req(ctx, statep);
3170 if (unlikely(!req)) {
3171 if (!submitted)
3172 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003173 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06003174 }
Pavel Begunkov50585b92019-11-07 01:41:07 +03003175 if (!io_get_sqring(ctx, &req->submit)) {
Pavel Begunkov196be952019-11-07 01:41:06 +03003176 __io_free_req(req);
3177 break;
3178 }
Jens Axboe9e645e112019-05-10 16:07:28 -06003179
Pavel Begunkov50585b92019-11-07 01:41:07 +03003180 if (io_sqe_needs_user(req->submit.sqe) && !*mm) {
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03003181 mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm);
3182 if (!mm_fault) {
3183 use_mm(ctx->sqo_mm);
3184 *mm = ctx->sqo_mm;
3185 }
3186 }
3187
Pavel Begunkov50585b92019-11-07 01:41:07 +03003188 sqe_flags = req->submit.sqe->flags;
3189
Pavel Begunkov50585b92019-11-07 01:41:07 +03003190 req->submit.ring_file = ring_file;
3191 req->submit.ring_fd = ring_fd;
3192 req->submit.has_user = *mm != NULL;
3193 req->submit.in_async = async;
3194 req->submit.needs_fixed_file = async;
3195 trace_io_uring_submit_sqe(ctx, req->submit.sqe->user_data,
3196 true, async);
Jackie Liua197f662019-11-08 08:09:12 -07003197 io_submit_sqe(req, statep, &link);
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03003198 submitted++;
Pavel Begunkove5eb6362019-11-06 00:22:15 +03003199
3200 /*
3201 * If previous wasn't linked and we have a linked command,
3202 * that's the end of the chain. Submit the previous link.
3203 */
Pavel Begunkov50585b92019-11-07 01:41:07 +03003204 if (!(sqe_flags & IOSQE_IO_LINK) && link) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003205 io_queue_link_head(link);
Pavel Begunkove5eb6362019-11-06 00:22:15 +03003206 link = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003207 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07003208 }
3209
Jens Axboe9e645e112019-05-10 16:07:28 -06003210 if (link)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003211 io_queue_link_head(link);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003212 if (statep)
3213 io_submit_state_end(&state);
3214
Pavel Begunkovae9428c2019-11-06 00:22:14 +03003215 /* Commit SQ ring head once we've consumed and submitted all SQEs */
3216 io_commit_sqring(ctx);
3217
Jens Axboe6c271ce2019-01-10 11:22:30 -07003218 return submitted;
3219}
3220
3221static int io_sq_thread(void *data)
3222{
Jens Axboe6c271ce2019-01-10 11:22:30 -07003223 struct io_ring_ctx *ctx = data;
3224 struct mm_struct *cur_mm = NULL;
3225 mm_segment_t old_fs;
3226 DEFINE_WAIT(wait);
3227 unsigned inflight;
3228 unsigned long timeout;
Jens Axboec1edbf52019-11-10 16:56:04 -07003229 int ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003230
Jens Axboe206aefd2019-11-07 18:27:42 -07003231 complete(&ctx->completions[1]);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08003232
Jens Axboe6c271ce2019-01-10 11:22:30 -07003233 old_fs = get_fs();
3234 set_fs(USER_DS);
3235
Jens Axboec1edbf52019-11-10 16:56:04 -07003236 ret = timeout = inflight = 0;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003237 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003238 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003239
3240 if (inflight) {
3241 unsigned nr_events = 0;
3242
3243 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe2b2ed972019-10-25 10:06:15 -06003244 /*
3245 * inflight is the count of the maximum possible
3246 * entries we submitted, but it can be smaller
3247 * if we dropped some of them. If we don't have
3248 * poll entries available, then we know that we
3249 * have nothing left to poll for. Reset the
3250 * inflight count to zero in that case.
3251 */
3252 mutex_lock(&ctx->uring_lock);
3253 if (!list_empty(&ctx->poll_list))
3254 __io_iopoll_check(ctx, &nr_events, 0);
3255 else
3256 inflight = 0;
3257 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003258 } else {
3259 /*
3260 * Normal IO, just pretend everything completed.
3261 * We don't have to poll completions for that.
3262 */
3263 nr_events = inflight;
3264 }
3265
3266 inflight -= nr_events;
3267 if (!inflight)
3268 timeout = jiffies + ctx->sq_thread_idle;
3269 }
3270
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003271 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07003272
3273 /*
3274 * If submit got -EBUSY, flag us as needing the application
3275 * to enter the kernel to reap and flush events.
3276 */
3277 if (!to_submit || ret == -EBUSY) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07003278 /*
3279 * We're polling. If we're within the defined idle
3280 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07003281 * to sleep. The exception is if we got EBUSY doing
3282 * more IO, we should wait for the application to
3283 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07003284 */
Jens Axboec1edbf52019-11-10 16:56:04 -07003285 if (inflight ||
3286 (!time_after(jiffies, timeout) && ret != -EBUSY)) {
Jens Axboe9831a902019-09-19 09:48:55 -06003287 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07003288 continue;
3289 }
3290
3291 /*
3292 * Drop cur_mm before scheduling, we can't hold it for
3293 * long periods (or over schedule()). Do this before
3294 * adding ourselves to the waitqueue, as the unuse/drop
3295 * may sleep.
3296 */
3297 if (cur_mm) {
3298 unuse_mm(cur_mm);
3299 mmput(cur_mm);
3300 cur_mm = NULL;
3301 }
3302
3303 prepare_to_wait(&ctx->sqo_wait, &wait,
3304 TASK_INTERRUPTIBLE);
3305
3306 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00003307 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02003308 /* make sure to read SQ tail after writing flags */
3309 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07003310
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003311 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07003312 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003313 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07003314 finish_wait(&ctx->sqo_wait, &wait);
3315 break;
3316 }
3317 if (signal_pending(current))
3318 flush_signals(current);
3319 schedule();
3320 finish_wait(&ctx->sqo_wait, &wait);
3321
Hristo Venev75b28af2019-08-26 17:23:46 +00003322 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003323 continue;
3324 }
3325 finish_wait(&ctx->sqo_wait, &wait);
3326
Hristo Venev75b28af2019-08-26 17:23:46 +00003327 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003328 }
3329
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003330 to_submit = min(to_submit, ctx->sq_entries);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003331 ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true);
3332 if (ret > 0)
3333 inflight += ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003334 }
3335
3336 set_fs(old_fs);
3337 if (cur_mm) {
3338 unuse_mm(cur_mm);
3339 mmput(cur_mm);
3340 }
Jens Axboe06058632019-04-13 09:26:03 -06003341
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003342 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06003343
Jens Axboe6c271ce2019-01-10 11:22:30 -07003344 return 0;
3345}
3346
Jens Axboebda52162019-09-24 13:47:15 -06003347struct io_wait_queue {
3348 struct wait_queue_entry wq;
3349 struct io_ring_ctx *ctx;
3350 unsigned to_wait;
3351 unsigned nr_timeouts;
3352};
3353
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003354static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06003355{
3356 struct io_ring_ctx *ctx = iowq->ctx;
3357
3358 /*
3359 * Wake up if we have enough events, or if a timeout occured since we
3360 * started waiting. For timeouts, we always want to return to userspace,
3361 * regardless of event count.
3362 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003363 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06003364 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
3365}
3366
3367static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
3368 int wake_flags, void *key)
3369{
3370 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
3371 wq);
3372
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003373 /* use noflush == true, as we can't safely rely on locking context */
3374 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06003375 return -1;
3376
3377 return autoremove_wake_function(curr, mode, wake_flags, key);
3378}
3379
Jens Axboe2b188cc2019-01-07 10:46:33 -07003380/*
3381 * Wait until events become available, if we don't already have some. The
3382 * application must reap them itself, as they reside on the shared cq ring.
3383 */
3384static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
3385 const sigset_t __user *sig, size_t sigsz)
3386{
Jens Axboebda52162019-09-24 13:47:15 -06003387 struct io_wait_queue iowq = {
3388 .wq = {
3389 .private = current,
3390 .func = io_wake_function,
3391 .entry = LIST_HEAD_INIT(iowq.wq.entry),
3392 },
3393 .ctx = ctx,
3394 .to_wait = min_events,
3395 };
Hristo Venev75b28af2019-08-26 17:23:46 +00003396 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08003397 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003398
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003399 if (io_cqring_events(ctx, false) >= min_events)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003400 return 0;
3401
3402 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01003403#ifdef CONFIG_COMPAT
3404 if (in_compat_syscall())
3405 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07003406 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01003407 else
3408#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07003409 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01003410
Jens Axboe2b188cc2019-01-07 10:46:33 -07003411 if (ret)
3412 return ret;
3413 }
3414
Jens Axboebda52162019-09-24 13:47:15 -06003415 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02003416 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06003417 do {
3418 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
3419 TASK_INTERRUPTIBLE);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003420 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06003421 break;
3422 schedule();
3423 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08003424 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06003425 break;
3426 }
3427 } while (1);
3428 finish_wait(&ctx->wait, &iowq.wq);
3429
Jackie Liue9ffa5c2019-10-29 11:16:42 +08003430 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003431
Hristo Venev75b28af2019-08-26 17:23:46 +00003432 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003433}
3434
Jens Axboe6b063142019-01-10 22:13:58 -07003435static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
3436{
3437#if defined(CONFIG_UNIX)
3438 if (ctx->ring_sock) {
3439 struct sock *sock = ctx->ring_sock->sk;
3440 struct sk_buff *skb;
3441
3442 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
3443 kfree_skb(skb);
3444 }
3445#else
3446 int i;
3447
Jens Axboe65e19f52019-10-26 07:20:21 -06003448 for (i = 0; i < ctx->nr_user_files; i++) {
3449 struct file *file;
3450
3451 file = io_file_from_index(ctx, i);
3452 if (file)
3453 fput(file);
3454 }
Jens Axboe6b063142019-01-10 22:13:58 -07003455#endif
3456}
3457
3458static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
3459{
Jens Axboe65e19f52019-10-26 07:20:21 -06003460 unsigned nr_tables, i;
3461
3462 if (!ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003463 return -ENXIO;
3464
3465 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06003466 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
3467 for (i = 0; i < nr_tables; i++)
3468 kfree(ctx->file_table[i].files);
3469 kfree(ctx->file_table);
3470 ctx->file_table = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003471 ctx->nr_user_files = 0;
3472 return 0;
3473}
3474
Jens Axboe6c271ce2019-01-10 11:22:30 -07003475static void io_sq_thread_stop(struct io_ring_ctx *ctx)
3476{
3477 if (ctx->sqo_thread) {
Jens Axboe206aefd2019-11-07 18:27:42 -07003478 wait_for_completion(&ctx->completions[1]);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003479 /*
3480 * The park is a bit of a work-around, without it we get
3481 * warning spews on shutdown with SQPOLL set and affinity
3482 * set to a single CPU.
3483 */
Jens Axboe06058632019-04-13 09:26:03 -06003484 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003485 kthread_stop(ctx->sqo_thread);
3486 ctx->sqo_thread = NULL;
3487 }
3488}
3489
Jens Axboe6b063142019-01-10 22:13:58 -07003490static void io_finish_async(struct io_ring_ctx *ctx)
3491{
Jens Axboe6c271ce2019-01-10 11:22:30 -07003492 io_sq_thread_stop(ctx);
3493
Jens Axboe561fb042019-10-24 07:25:42 -06003494 if (ctx->io_wq) {
3495 io_wq_destroy(ctx->io_wq);
3496 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003497 }
3498}
3499
3500#if defined(CONFIG_UNIX)
3501static void io_destruct_skb(struct sk_buff *skb)
3502{
3503 struct io_ring_ctx *ctx = skb->sk->sk_user_data;
3504
Jens Axboe561fb042019-10-24 07:25:42 -06003505 if (ctx->io_wq)
3506 io_wq_flush(ctx->io_wq);
Jens Axboe8a997342019-10-09 14:40:13 -06003507
Jens Axboe6b063142019-01-10 22:13:58 -07003508 unix_destruct_scm(skb);
3509}
3510
3511/*
3512 * Ensure the UNIX gc is aware of our file set, so we are certain that
3513 * the io_uring can be safely unregistered on process exit, even if we have
3514 * loops in the file referencing.
3515 */
3516static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
3517{
3518 struct sock *sk = ctx->ring_sock->sk;
3519 struct scm_fp_list *fpl;
3520 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06003521 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07003522
3523 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
3524 unsigned long inflight = ctx->user->unix_inflight + nr;
3525
3526 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
3527 return -EMFILE;
3528 }
3529
3530 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
3531 if (!fpl)
3532 return -ENOMEM;
3533
3534 skb = alloc_skb(0, GFP_KERNEL);
3535 if (!skb) {
3536 kfree(fpl);
3537 return -ENOMEM;
3538 }
3539
3540 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07003541
Jens Axboe08a45172019-10-03 08:11:03 -06003542 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07003543 fpl->user = get_uid(ctx->user);
3544 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003545 struct file *file = io_file_from_index(ctx, i + offset);
3546
3547 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06003548 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06003549 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06003550 unix_inflight(fpl->user, fpl->fp[nr_files]);
3551 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07003552 }
3553
Jens Axboe08a45172019-10-03 08:11:03 -06003554 if (nr_files) {
3555 fpl->max = SCM_MAX_FD;
3556 fpl->count = nr_files;
3557 UNIXCB(skb).fp = fpl;
3558 skb->destructor = io_destruct_skb;
3559 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
3560 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07003561
Jens Axboe08a45172019-10-03 08:11:03 -06003562 for (i = 0; i < nr_files; i++)
3563 fput(fpl->fp[i]);
3564 } else {
3565 kfree_skb(skb);
3566 kfree(fpl);
3567 }
Jens Axboe6b063142019-01-10 22:13:58 -07003568
3569 return 0;
3570}
3571
3572/*
3573 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
3574 * causes regular reference counting to break down. We rely on the UNIX
3575 * garbage collection to take care of this problem for us.
3576 */
3577static int io_sqe_files_scm(struct io_ring_ctx *ctx)
3578{
3579 unsigned left, total;
3580 int ret = 0;
3581
3582 total = 0;
3583 left = ctx->nr_user_files;
3584 while (left) {
3585 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07003586
3587 ret = __io_sqe_files_scm(ctx, this_files, total);
3588 if (ret)
3589 break;
3590 left -= this_files;
3591 total += this_files;
3592 }
3593
3594 if (!ret)
3595 return 0;
3596
3597 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003598 struct file *file = io_file_from_index(ctx, total);
3599
3600 if (file)
3601 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07003602 total++;
3603 }
3604
3605 return ret;
3606}
3607#else
3608static int io_sqe_files_scm(struct io_ring_ctx *ctx)
3609{
3610 return 0;
3611}
3612#endif
3613
Jens Axboe65e19f52019-10-26 07:20:21 -06003614static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
3615 unsigned nr_files)
3616{
3617 int i;
3618
3619 for (i = 0; i < nr_tables; i++) {
3620 struct fixed_file_table *table = &ctx->file_table[i];
3621 unsigned this_files;
3622
3623 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
3624 table->files = kcalloc(this_files, sizeof(struct file *),
3625 GFP_KERNEL);
3626 if (!table->files)
3627 break;
3628 nr_files -= this_files;
3629 }
3630
3631 if (i == nr_tables)
3632 return 0;
3633
3634 for (i = 0; i < nr_tables; i++) {
3635 struct fixed_file_table *table = &ctx->file_table[i];
3636 kfree(table->files);
3637 }
3638 return 1;
3639}
3640
Jens Axboe6b063142019-01-10 22:13:58 -07003641static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
3642 unsigned nr_args)
3643{
3644 __s32 __user *fds = (__s32 __user *) arg;
Jens Axboe65e19f52019-10-26 07:20:21 -06003645 unsigned nr_tables;
Jens Axboe6b063142019-01-10 22:13:58 -07003646 int fd, ret = 0;
3647 unsigned i;
3648
Jens Axboe65e19f52019-10-26 07:20:21 -06003649 if (ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003650 return -EBUSY;
3651 if (!nr_args)
3652 return -EINVAL;
3653 if (nr_args > IORING_MAX_FIXED_FILES)
3654 return -EMFILE;
3655
Jens Axboe65e19f52019-10-26 07:20:21 -06003656 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
3657 ctx->file_table = kcalloc(nr_tables, sizeof(struct fixed_file_table),
3658 GFP_KERNEL);
3659 if (!ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003660 return -ENOMEM;
3661
Jens Axboe65e19f52019-10-26 07:20:21 -06003662 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
3663 kfree(ctx->file_table);
Jens Axboe46568e92019-11-10 08:40:53 -07003664 ctx->file_table = NULL;
Jens Axboe65e19f52019-10-26 07:20:21 -06003665 return -ENOMEM;
3666 }
3667
Jens Axboe08a45172019-10-03 08:11:03 -06003668 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003669 struct fixed_file_table *table;
3670 unsigned index;
3671
Jens Axboe6b063142019-01-10 22:13:58 -07003672 ret = -EFAULT;
3673 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
3674 break;
Jens Axboe08a45172019-10-03 08:11:03 -06003675 /* allow sparse sets */
3676 if (fd == -1) {
3677 ret = 0;
3678 continue;
3679 }
Jens Axboe6b063142019-01-10 22:13:58 -07003680
Jens Axboe65e19f52019-10-26 07:20:21 -06003681 table = &ctx->file_table[i >> IORING_FILE_TABLE_SHIFT];
3682 index = i & IORING_FILE_TABLE_MASK;
3683 table->files[index] = fget(fd);
Jens Axboe6b063142019-01-10 22:13:58 -07003684
3685 ret = -EBADF;
Jens Axboe65e19f52019-10-26 07:20:21 -06003686 if (!table->files[index])
Jens Axboe6b063142019-01-10 22:13:58 -07003687 break;
3688 /*
3689 * Don't allow io_uring instances to be registered. If UNIX
3690 * isn't enabled, then this causes a reference cycle and this
3691 * instance can never get freed. If UNIX is enabled we'll
3692 * handle it just fine, but there's still no point in allowing
3693 * a ring fd as it doesn't support regular read/write anyway.
3694 */
Jens Axboe65e19f52019-10-26 07:20:21 -06003695 if (table->files[index]->f_op == &io_uring_fops) {
3696 fput(table->files[index]);
Jens Axboe6b063142019-01-10 22:13:58 -07003697 break;
3698 }
Jens Axboe6b063142019-01-10 22:13:58 -07003699 ret = 0;
3700 }
3701
3702 if (ret) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003703 for (i = 0; i < ctx->nr_user_files; i++) {
3704 struct file *file;
Jens Axboe6b063142019-01-10 22:13:58 -07003705
Jens Axboe65e19f52019-10-26 07:20:21 -06003706 file = io_file_from_index(ctx, i);
3707 if (file)
3708 fput(file);
3709 }
3710 for (i = 0; i < nr_tables; i++)
3711 kfree(ctx->file_table[i].files);
3712
3713 kfree(ctx->file_table);
3714 ctx->file_table = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003715 ctx->nr_user_files = 0;
3716 return ret;
3717 }
3718
3719 ret = io_sqe_files_scm(ctx);
3720 if (ret)
3721 io_sqe_files_unregister(ctx);
3722
3723 return ret;
3724}
3725
Jens Axboec3a31e62019-10-03 13:59:56 -06003726static void io_sqe_file_unregister(struct io_ring_ctx *ctx, int index)
3727{
3728#if defined(CONFIG_UNIX)
Jens Axboe65e19f52019-10-26 07:20:21 -06003729 struct file *file = io_file_from_index(ctx, index);
Jens Axboec3a31e62019-10-03 13:59:56 -06003730 struct sock *sock = ctx->ring_sock->sk;
3731 struct sk_buff_head list, *head = &sock->sk_receive_queue;
3732 struct sk_buff *skb;
3733 int i;
3734
3735 __skb_queue_head_init(&list);
3736
3737 /*
3738 * Find the skb that holds this file in its SCM_RIGHTS. When found,
3739 * remove this entry and rearrange the file array.
3740 */
3741 skb = skb_dequeue(head);
3742 while (skb) {
3743 struct scm_fp_list *fp;
3744
3745 fp = UNIXCB(skb).fp;
3746 for (i = 0; i < fp->count; i++) {
3747 int left;
3748
3749 if (fp->fp[i] != file)
3750 continue;
3751
3752 unix_notinflight(fp->user, fp->fp[i]);
3753 left = fp->count - 1 - i;
3754 if (left) {
3755 memmove(&fp->fp[i], &fp->fp[i + 1],
3756 left * sizeof(struct file *));
3757 }
3758 fp->count--;
3759 if (!fp->count) {
3760 kfree_skb(skb);
3761 skb = NULL;
3762 } else {
3763 __skb_queue_tail(&list, skb);
3764 }
3765 fput(file);
3766 file = NULL;
3767 break;
3768 }
3769
3770 if (!file)
3771 break;
3772
3773 __skb_queue_tail(&list, skb);
3774
3775 skb = skb_dequeue(head);
3776 }
3777
3778 if (skb_peek(&list)) {
3779 spin_lock_irq(&head->lock);
3780 while ((skb = __skb_dequeue(&list)) != NULL)
3781 __skb_queue_tail(head, skb);
3782 spin_unlock_irq(&head->lock);
3783 }
3784#else
Jens Axboe65e19f52019-10-26 07:20:21 -06003785 fput(io_file_from_index(ctx, index));
Jens Axboec3a31e62019-10-03 13:59:56 -06003786#endif
3787}
3788
3789static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
3790 int index)
3791{
3792#if defined(CONFIG_UNIX)
3793 struct sock *sock = ctx->ring_sock->sk;
3794 struct sk_buff_head *head = &sock->sk_receive_queue;
3795 struct sk_buff *skb;
3796
3797 /*
3798 * See if we can merge this file into an existing skb SCM_RIGHTS
3799 * file set. If there's no room, fall back to allocating a new skb
3800 * and filling it in.
3801 */
3802 spin_lock_irq(&head->lock);
3803 skb = skb_peek(head);
3804 if (skb) {
3805 struct scm_fp_list *fpl = UNIXCB(skb).fp;
3806
3807 if (fpl->count < SCM_MAX_FD) {
3808 __skb_unlink(skb, head);
3809 spin_unlock_irq(&head->lock);
3810 fpl->fp[fpl->count] = get_file(file);
3811 unix_inflight(fpl->user, fpl->fp[fpl->count]);
3812 fpl->count++;
3813 spin_lock_irq(&head->lock);
3814 __skb_queue_head(head, skb);
3815 } else {
3816 skb = NULL;
3817 }
3818 }
3819 spin_unlock_irq(&head->lock);
3820
3821 if (skb) {
3822 fput(file);
3823 return 0;
3824 }
3825
3826 return __io_sqe_files_scm(ctx, 1, index);
3827#else
3828 return 0;
3829#endif
3830}
3831
3832static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
3833 unsigned nr_args)
3834{
3835 struct io_uring_files_update up;
3836 __s32 __user *fds;
3837 int fd, i, err;
3838 __u32 done;
3839
Jens Axboe65e19f52019-10-26 07:20:21 -06003840 if (!ctx->file_table)
Jens Axboec3a31e62019-10-03 13:59:56 -06003841 return -ENXIO;
3842 if (!nr_args)
3843 return -EINVAL;
3844 if (copy_from_user(&up, arg, sizeof(up)))
3845 return -EFAULT;
3846 if (check_add_overflow(up.offset, nr_args, &done))
3847 return -EOVERFLOW;
3848 if (done > ctx->nr_user_files)
3849 return -EINVAL;
3850
3851 done = 0;
3852 fds = (__s32 __user *) up.fds;
3853 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003854 struct fixed_file_table *table;
3855 unsigned index;
3856
Jens Axboec3a31e62019-10-03 13:59:56 -06003857 err = 0;
3858 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
3859 err = -EFAULT;
3860 break;
3861 }
3862 i = array_index_nospec(up.offset, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06003863 table = &ctx->file_table[i >> IORING_FILE_TABLE_SHIFT];
3864 index = i & IORING_FILE_TABLE_MASK;
3865 if (table->files[index]) {
Jens Axboec3a31e62019-10-03 13:59:56 -06003866 io_sqe_file_unregister(ctx, i);
Jens Axboe65e19f52019-10-26 07:20:21 -06003867 table->files[index] = NULL;
Jens Axboec3a31e62019-10-03 13:59:56 -06003868 }
3869 if (fd != -1) {
3870 struct file *file;
3871
3872 file = fget(fd);
3873 if (!file) {
3874 err = -EBADF;
3875 break;
3876 }
3877 /*
3878 * Don't allow io_uring instances to be registered. If
3879 * UNIX isn't enabled, then this causes a reference
3880 * cycle and this instance can never get freed. If UNIX
3881 * is enabled we'll handle it just fine, but there's
3882 * still no point in allowing a ring fd as it doesn't
3883 * support regular read/write anyway.
3884 */
3885 if (file->f_op == &io_uring_fops) {
3886 fput(file);
3887 err = -EBADF;
3888 break;
3889 }
Jens Axboe65e19f52019-10-26 07:20:21 -06003890 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06003891 err = io_sqe_file_register(ctx, file, i);
3892 if (err)
3893 break;
3894 }
3895 nr_args--;
3896 done++;
3897 up.offset++;
3898 }
3899
3900 return done ? done : err;
3901}
3902
Jens Axboe7d723062019-11-12 22:31:31 -07003903static void io_put_work(struct io_wq_work *work)
3904{
3905 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
3906
3907 io_put_req(req);
3908}
3909
3910static void io_get_work(struct io_wq_work *work)
3911{
3912 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
3913
3914 refcount_inc(&req->refs);
3915}
3916
Jens Axboe6c271ce2019-01-10 11:22:30 -07003917static int io_sq_offload_start(struct io_ring_ctx *ctx,
3918 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003919{
Jens Axboe561fb042019-10-24 07:25:42 -06003920 unsigned concurrency;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003921 int ret;
3922
Jens Axboe6c271ce2019-01-10 11:22:30 -07003923 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003924 mmgrab(current->mm);
3925 ctx->sqo_mm = current->mm;
3926
Jens Axboe6c271ce2019-01-10 11:22:30 -07003927 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06003928 ret = -EPERM;
3929 if (!capable(CAP_SYS_ADMIN))
3930 goto err;
3931
Jens Axboe917257d2019-04-13 09:28:55 -06003932 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
3933 if (!ctx->sq_thread_idle)
3934 ctx->sq_thread_idle = HZ;
3935
Jens Axboe6c271ce2019-01-10 11:22:30 -07003936 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06003937 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003938
Jens Axboe917257d2019-04-13 09:28:55 -06003939 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06003940 if (cpu >= nr_cpu_ids)
3941 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08003942 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06003943 goto err;
3944
Jens Axboe6c271ce2019-01-10 11:22:30 -07003945 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
3946 ctx, cpu,
3947 "io_uring-sq");
3948 } else {
3949 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
3950 "io_uring-sq");
3951 }
3952 if (IS_ERR(ctx->sqo_thread)) {
3953 ret = PTR_ERR(ctx->sqo_thread);
3954 ctx->sqo_thread = NULL;
3955 goto err;
3956 }
3957 wake_up_process(ctx->sqo_thread);
3958 } else if (p->flags & IORING_SETUP_SQ_AFF) {
3959 /* Can't have SQ_AFF without SQPOLL */
3960 ret = -EINVAL;
3961 goto err;
3962 }
3963
Jens Axboe561fb042019-10-24 07:25:42 -06003964 /* Do QD, or 4 * CPUS, whatever is smallest */
3965 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
Jens Axboe7d723062019-11-12 22:31:31 -07003966 ctx->io_wq = io_wq_create(concurrency, ctx->sqo_mm, ctx->user,
3967 io_get_work, io_put_work);
Jens Axboe975c99a52019-10-30 08:42:56 -06003968 if (IS_ERR(ctx->io_wq)) {
3969 ret = PTR_ERR(ctx->io_wq);
3970 ctx->io_wq = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003971 goto err;
3972 }
3973
3974 return 0;
3975err:
Jens Axboe54a91f32019-09-10 09:15:04 -06003976 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003977 mmdrop(ctx->sqo_mm);
3978 ctx->sqo_mm = NULL;
3979 return ret;
3980}
3981
3982static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
3983{
3984 atomic_long_sub(nr_pages, &user->locked_vm);
3985}
3986
3987static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
3988{
3989 unsigned long page_limit, cur_pages, new_pages;
3990
3991 /* Don't allow more pages than we can safely lock */
3992 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
3993
3994 do {
3995 cur_pages = atomic_long_read(&user->locked_vm);
3996 new_pages = cur_pages + nr_pages;
3997 if (new_pages > page_limit)
3998 return -ENOMEM;
3999 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
4000 new_pages) != cur_pages);
4001
4002 return 0;
4003}
4004
4005static void io_mem_free(void *ptr)
4006{
Mark Rutland52e04ef2019-04-30 17:30:21 +01004007 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004008
Mark Rutland52e04ef2019-04-30 17:30:21 +01004009 if (!ptr)
4010 return;
4011
4012 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004013 if (put_page_testzero(page))
4014 free_compound_page(page);
4015}
4016
4017static void *io_mem_alloc(size_t size)
4018{
4019 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
4020 __GFP_NORETRY;
4021
4022 return (void *) __get_free_pages(gfp_flags, get_order(size));
4023}
4024
Hristo Venev75b28af2019-08-26 17:23:46 +00004025static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
4026 size_t *sq_offset)
4027{
4028 struct io_rings *rings;
4029 size_t off, sq_array_size;
4030
4031 off = struct_size(rings, cqes, cq_entries);
4032 if (off == SIZE_MAX)
4033 return SIZE_MAX;
4034
4035#ifdef CONFIG_SMP
4036 off = ALIGN(off, SMP_CACHE_BYTES);
4037 if (off == 0)
4038 return SIZE_MAX;
4039#endif
4040
4041 sq_array_size = array_size(sizeof(u32), sq_entries);
4042 if (sq_array_size == SIZE_MAX)
4043 return SIZE_MAX;
4044
4045 if (check_add_overflow(off, sq_array_size, &off))
4046 return SIZE_MAX;
4047
4048 if (sq_offset)
4049 *sq_offset = off;
4050
4051 return off;
4052}
4053
Jens Axboe2b188cc2019-01-07 10:46:33 -07004054static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
4055{
Hristo Venev75b28af2019-08-26 17:23:46 +00004056 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004057
Hristo Venev75b28af2019-08-26 17:23:46 +00004058 pages = (size_t)1 << get_order(
4059 rings_size(sq_entries, cq_entries, NULL));
4060 pages += (size_t)1 << get_order(
4061 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07004062
Hristo Venev75b28af2019-08-26 17:23:46 +00004063 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004064}
4065
Jens Axboeedafcce2019-01-09 09:16:05 -07004066static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
4067{
4068 int i, j;
4069
4070 if (!ctx->user_bufs)
4071 return -ENXIO;
4072
4073 for (i = 0; i < ctx->nr_user_bufs; i++) {
4074 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
4075
4076 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbard27c4d3a2019-08-04 19:32:06 -07004077 put_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07004078
4079 if (ctx->account_mem)
4080 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004081 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07004082 imu->nr_bvecs = 0;
4083 }
4084
4085 kfree(ctx->user_bufs);
4086 ctx->user_bufs = NULL;
4087 ctx->nr_user_bufs = 0;
4088 return 0;
4089}
4090
4091static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
4092 void __user *arg, unsigned index)
4093{
4094 struct iovec __user *src;
4095
4096#ifdef CONFIG_COMPAT
4097 if (ctx->compat) {
4098 struct compat_iovec __user *ciovs;
4099 struct compat_iovec ciov;
4100
4101 ciovs = (struct compat_iovec __user *) arg;
4102 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
4103 return -EFAULT;
4104
4105 dst->iov_base = (void __user *) (unsigned long) ciov.iov_base;
4106 dst->iov_len = ciov.iov_len;
4107 return 0;
4108 }
4109#endif
4110 src = (struct iovec __user *) arg;
4111 if (copy_from_user(dst, &src[index], sizeof(*dst)))
4112 return -EFAULT;
4113 return 0;
4114}
4115
4116static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
4117 unsigned nr_args)
4118{
4119 struct vm_area_struct **vmas = NULL;
4120 struct page **pages = NULL;
4121 int i, j, got_pages = 0;
4122 int ret = -EINVAL;
4123
4124 if (ctx->user_bufs)
4125 return -EBUSY;
4126 if (!nr_args || nr_args > UIO_MAXIOV)
4127 return -EINVAL;
4128
4129 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
4130 GFP_KERNEL);
4131 if (!ctx->user_bufs)
4132 return -ENOMEM;
4133
4134 for (i = 0; i < nr_args; i++) {
4135 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
4136 unsigned long off, start, end, ubuf;
4137 int pret, nr_pages;
4138 struct iovec iov;
4139 size_t size;
4140
4141 ret = io_copy_iov(ctx, &iov, arg, i);
4142 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03004143 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07004144
4145 /*
4146 * Don't impose further limits on the size and buffer
4147 * constraints here, we'll -EINVAL later when IO is
4148 * submitted if they are wrong.
4149 */
4150 ret = -EFAULT;
4151 if (!iov.iov_base || !iov.iov_len)
4152 goto err;
4153
4154 /* arbitrary limit, but we need something */
4155 if (iov.iov_len > SZ_1G)
4156 goto err;
4157
4158 ubuf = (unsigned long) iov.iov_base;
4159 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
4160 start = ubuf >> PAGE_SHIFT;
4161 nr_pages = end - start;
4162
4163 if (ctx->account_mem) {
4164 ret = io_account_mem(ctx->user, nr_pages);
4165 if (ret)
4166 goto err;
4167 }
4168
4169 ret = 0;
4170 if (!pages || nr_pages > got_pages) {
4171 kfree(vmas);
4172 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004173 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07004174 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004175 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07004176 sizeof(struct vm_area_struct *),
4177 GFP_KERNEL);
4178 if (!pages || !vmas) {
4179 ret = -ENOMEM;
4180 if (ctx->account_mem)
4181 io_unaccount_mem(ctx->user, nr_pages);
4182 goto err;
4183 }
4184 got_pages = nr_pages;
4185 }
4186
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004187 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07004188 GFP_KERNEL);
4189 ret = -ENOMEM;
4190 if (!imu->bvec) {
4191 if (ctx->account_mem)
4192 io_unaccount_mem(ctx->user, nr_pages);
4193 goto err;
4194 }
4195
4196 ret = 0;
4197 down_read(&current->mm->mmap_sem);
Ira Weiny932f4a62019-05-13 17:17:03 -07004198 pret = get_user_pages(ubuf, nr_pages,
4199 FOLL_WRITE | FOLL_LONGTERM,
4200 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07004201 if (pret == nr_pages) {
4202 /* don't support file backed memory */
4203 for (j = 0; j < nr_pages; j++) {
4204 struct vm_area_struct *vma = vmas[j];
4205
4206 if (vma->vm_file &&
4207 !is_file_hugepages(vma->vm_file)) {
4208 ret = -EOPNOTSUPP;
4209 break;
4210 }
4211 }
4212 } else {
4213 ret = pret < 0 ? pret : -EFAULT;
4214 }
4215 up_read(&current->mm->mmap_sem);
4216 if (ret) {
4217 /*
4218 * if we did partial map, or found file backed vmas,
4219 * release any pages we did get
4220 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07004221 if (pret > 0)
4222 put_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07004223 if (ctx->account_mem)
4224 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004225 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07004226 goto err;
4227 }
4228
4229 off = ubuf & ~PAGE_MASK;
4230 size = iov.iov_len;
4231 for (j = 0; j < nr_pages; j++) {
4232 size_t vec_len;
4233
4234 vec_len = min_t(size_t, size, PAGE_SIZE - off);
4235 imu->bvec[j].bv_page = pages[j];
4236 imu->bvec[j].bv_len = vec_len;
4237 imu->bvec[j].bv_offset = off;
4238 off = 0;
4239 size -= vec_len;
4240 }
4241 /* store original address for later verification */
4242 imu->ubuf = ubuf;
4243 imu->len = iov.iov_len;
4244 imu->nr_bvecs = nr_pages;
4245
4246 ctx->nr_user_bufs++;
4247 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004248 kvfree(pages);
4249 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07004250 return 0;
4251err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004252 kvfree(pages);
4253 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07004254 io_sqe_buffer_unregister(ctx);
4255 return ret;
4256}
4257
Jens Axboe9b402842019-04-11 11:45:41 -06004258static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
4259{
4260 __s32 __user *fds = arg;
4261 int fd;
4262
4263 if (ctx->cq_ev_fd)
4264 return -EBUSY;
4265
4266 if (copy_from_user(&fd, fds, sizeof(*fds)))
4267 return -EFAULT;
4268
4269 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
4270 if (IS_ERR(ctx->cq_ev_fd)) {
4271 int ret = PTR_ERR(ctx->cq_ev_fd);
4272 ctx->cq_ev_fd = NULL;
4273 return ret;
4274 }
4275
4276 return 0;
4277}
4278
4279static int io_eventfd_unregister(struct io_ring_ctx *ctx)
4280{
4281 if (ctx->cq_ev_fd) {
4282 eventfd_ctx_put(ctx->cq_ev_fd);
4283 ctx->cq_ev_fd = NULL;
4284 return 0;
4285 }
4286
4287 return -ENXIO;
4288}
4289
Jens Axboe2b188cc2019-01-07 10:46:33 -07004290static void io_ring_ctx_free(struct io_ring_ctx *ctx)
4291{
Jens Axboe6b063142019-01-10 22:13:58 -07004292 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004293 if (ctx->sqo_mm)
4294 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07004295
4296 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07004297 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07004298 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06004299 io_eventfd_unregister(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07004300
Jens Axboe2b188cc2019-01-07 10:46:33 -07004301#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07004302 if (ctx->ring_sock) {
4303 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004304 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07004305 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004306#endif
4307
Hristo Venev75b28af2019-08-26 17:23:46 +00004308 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004309 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004310
4311 percpu_ref_exit(&ctx->refs);
4312 if (ctx->account_mem)
4313 io_unaccount_mem(ctx->user,
4314 ring_pages(ctx->sq_entries, ctx->cq_entries));
4315 free_uid(ctx->user);
Jens Axboe206aefd2019-11-07 18:27:42 -07004316 kfree(ctx->completions);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07004317 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004318 kfree(ctx);
4319}
4320
4321static __poll_t io_uring_poll(struct file *file, poll_table *wait)
4322{
4323 struct io_ring_ctx *ctx = file->private_data;
4324 __poll_t mask = 0;
4325
4326 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02004327 /*
4328 * synchronizes with barrier from wq_has_sleeper call in
4329 * io_commit_cqring
4330 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004331 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00004332 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
4333 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004334 mask |= EPOLLOUT | EPOLLWRNORM;
yangerkundaa5de52019-09-24 20:53:34 +08004335 if (READ_ONCE(ctx->rings->cq.head) != ctx->cached_cq_tail)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004336 mask |= EPOLLIN | EPOLLRDNORM;
4337
4338 return mask;
4339}
4340
4341static int io_uring_fasync(int fd, struct file *file, int on)
4342{
4343 struct io_ring_ctx *ctx = file->private_data;
4344
4345 return fasync_helper(fd, file, on, &ctx->cq_fasync);
4346}
4347
4348static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
4349{
4350 mutex_lock(&ctx->uring_lock);
4351 percpu_ref_kill(&ctx->refs);
4352 mutex_unlock(&ctx->uring_lock);
4353
Jens Axboe5262f562019-09-17 12:26:57 -06004354 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004355 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06004356
4357 if (ctx->io_wq)
4358 io_wq_cancel_all(ctx->io_wq);
4359
Jens Axboedef596e2019-01-09 08:59:42 -07004360 io_iopoll_reap_events(ctx);
Jens Axboe15dff282019-11-13 09:09:23 -07004361 /* if we failed setting up the ctx, we might not have any rings */
4362 if (ctx->rings)
4363 io_cqring_overflow_flush(ctx, true);
Jens Axboe206aefd2019-11-07 18:27:42 -07004364 wait_for_completion(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004365 io_ring_ctx_free(ctx);
4366}
4367
4368static int io_uring_release(struct inode *inode, struct file *file)
4369{
4370 struct io_ring_ctx *ctx = file->private_data;
4371
4372 file->private_data = NULL;
4373 io_ring_ctx_wait_and_kill(ctx);
4374 return 0;
4375}
4376
Jens Axboefcb323c2019-10-24 12:39:47 -06004377static void io_uring_cancel_files(struct io_ring_ctx *ctx,
4378 struct files_struct *files)
4379{
4380 struct io_kiocb *req;
4381 DEFINE_WAIT(wait);
4382
4383 while (!list_empty_careful(&ctx->inflight_list)) {
Jens Axboe768134d2019-11-10 20:30:53 -07004384 struct io_kiocb *cancel_req = NULL;
Jens Axboefcb323c2019-10-24 12:39:47 -06004385
4386 spin_lock_irq(&ctx->inflight_lock);
4387 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07004388 if (req->work.files != files)
4389 continue;
4390 /* req is being completed, ignore */
4391 if (!refcount_inc_not_zero(&req->refs))
4392 continue;
4393 cancel_req = req;
4394 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06004395 }
Jens Axboe768134d2019-11-10 20:30:53 -07004396 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06004397 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07004398 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06004399 spin_unlock_irq(&ctx->inflight_lock);
4400
Jens Axboe768134d2019-11-10 20:30:53 -07004401 /* We need to keep going until we don't find a matching req */
4402 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06004403 break;
Bob Liu2f6d9b92019-11-13 18:06:24 +08004404
4405 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
4406 io_put_req(cancel_req);
Jens Axboefcb323c2019-10-24 12:39:47 -06004407 schedule();
4408 }
Jens Axboe768134d2019-11-10 20:30:53 -07004409 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06004410}
4411
4412static int io_uring_flush(struct file *file, void *data)
4413{
4414 struct io_ring_ctx *ctx = file->private_data;
4415
4416 io_uring_cancel_files(ctx, data);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004417 if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) {
4418 io_cqring_overflow_flush(ctx, true);
Jens Axboefcb323c2019-10-24 12:39:47 -06004419 io_wq_cancel_all(ctx->io_wq);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004420 }
Jens Axboefcb323c2019-10-24 12:39:47 -06004421 return 0;
4422}
4423
Jens Axboe2b188cc2019-01-07 10:46:33 -07004424static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
4425{
4426 loff_t offset = (loff_t) vma->vm_pgoff << PAGE_SHIFT;
4427 unsigned long sz = vma->vm_end - vma->vm_start;
4428 struct io_ring_ctx *ctx = file->private_data;
4429 unsigned long pfn;
4430 struct page *page;
4431 void *ptr;
4432
4433 switch (offset) {
4434 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00004435 case IORING_OFF_CQ_RING:
4436 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004437 break;
4438 case IORING_OFF_SQES:
4439 ptr = ctx->sq_sqes;
4440 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004441 default:
4442 return -EINVAL;
4443 }
4444
4445 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07004446 if (sz > page_size(page))
Jens Axboe2b188cc2019-01-07 10:46:33 -07004447 return -EINVAL;
4448
4449 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
4450 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
4451}
4452
4453SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
4454 u32, min_complete, u32, flags, const sigset_t __user *, sig,
4455 size_t, sigsz)
4456{
4457 struct io_ring_ctx *ctx;
4458 long ret = -EBADF;
4459 int submitted = 0;
4460 struct fd f;
4461
Jens Axboe6c271ce2019-01-10 11:22:30 -07004462 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07004463 return -EINVAL;
4464
4465 f = fdget(fd);
4466 if (!f.file)
4467 return -EBADF;
4468
4469 ret = -EOPNOTSUPP;
4470 if (f.file->f_op != &io_uring_fops)
4471 goto out_fput;
4472
4473 ret = -ENXIO;
4474 ctx = f.file->private_data;
4475 if (!percpu_ref_tryget(&ctx->refs))
4476 goto out_fput;
4477
Jens Axboe6c271ce2019-01-10 11:22:30 -07004478 /*
4479 * For SQ polling, the thread will do all submissions and completions.
4480 * Just return the requested submit count, and wake the thread if
4481 * we were asked to.
4482 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06004483 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004484 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07004485 if (!list_empty_careful(&ctx->cq_overflow_list))
4486 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004487 if (flags & IORING_ENTER_SQ_WAKEUP)
4488 wake_up(&ctx->sqo_wait);
4489 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06004490 } else if (to_submit) {
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004491 struct mm_struct *cur_mm;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004492
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004493 to_submit = min(to_submit, ctx->sq_entries);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004494 mutex_lock(&ctx->uring_lock);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004495 /* already have mm, so io_submit_sqes() won't try to grab it */
4496 cur_mm = ctx->sqo_mm;
4497 submitted = io_submit_sqes(ctx, to_submit, f.file, fd,
4498 &cur_mm, false);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004499 mutex_unlock(&ctx->uring_lock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004500 }
4501 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07004502 unsigned nr_events = 0;
4503
Jens Axboe2b188cc2019-01-07 10:46:33 -07004504 min_complete = min(min_complete, ctx->cq_entries);
4505
Jens Axboedef596e2019-01-09 08:59:42 -07004506 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07004507 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07004508 } else {
4509 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
4510 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004511 }
4512
Pavel Begunkov6805b322019-10-08 02:18:42 +03004513 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004514out_fput:
4515 fdput(f);
4516 return submitted ? submitted : ret;
4517}
4518
4519static const struct file_operations io_uring_fops = {
4520 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06004521 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07004522 .mmap = io_uring_mmap,
4523 .poll = io_uring_poll,
4524 .fasync = io_uring_fasync,
4525};
4526
4527static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
4528 struct io_uring_params *p)
4529{
Hristo Venev75b28af2019-08-26 17:23:46 +00004530 struct io_rings *rings;
4531 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004532
Hristo Venev75b28af2019-08-26 17:23:46 +00004533 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
4534 if (size == SIZE_MAX)
4535 return -EOVERFLOW;
4536
4537 rings = io_mem_alloc(size);
4538 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004539 return -ENOMEM;
4540
Hristo Venev75b28af2019-08-26 17:23:46 +00004541 ctx->rings = rings;
4542 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
4543 rings->sq_ring_mask = p->sq_entries - 1;
4544 rings->cq_ring_mask = p->cq_entries - 1;
4545 rings->sq_ring_entries = p->sq_entries;
4546 rings->cq_ring_entries = p->cq_entries;
4547 ctx->sq_mask = rings->sq_ring_mask;
4548 ctx->cq_mask = rings->cq_ring_mask;
4549 ctx->sq_entries = rings->sq_ring_entries;
4550 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004551
4552 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07004553 if (size == SIZE_MAX) {
4554 io_mem_free(ctx->rings);
4555 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004556 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07004557 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004558
4559 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07004560 if (!ctx->sq_sqes) {
4561 io_mem_free(ctx->rings);
4562 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004563 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07004564 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004565
Jens Axboe2b188cc2019-01-07 10:46:33 -07004566 return 0;
4567}
4568
4569/*
4570 * Allocate an anonymous fd, this is what constitutes the application
4571 * visible backing of an io_uring instance. The application mmaps this
4572 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
4573 * we have to tie this fd to a socket for file garbage collection purposes.
4574 */
4575static int io_uring_get_fd(struct io_ring_ctx *ctx)
4576{
4577 struct file *file;
4578 int ret;
4579
4580#if defined(CONFIG_UNIX)
4581 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
4582 &ctx->ring_sock);
4583 if (ret)
4584 return ret;
4585#endif
4586
4587 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
4588 if (ret < 0)
4589 goto err;
4590
4591 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
4592 O_RDWR | O_CLOEXEC);
4593 if (IS_ERR(file)) {
4594 put_unused_fd(ret);
4595 ret = PTR_ERR(file);
4596 goto err;
4597 }
4598
4599#if defined(CONFIG_UNIX)
4600 ctx->ring_sock->file = file;
Jens Axboe6b063142019-01-10 22:13:58 -07004601 ctx->ring_sock->sk->sk_user_data = ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004602#endif
4603 fd_install(ret, file);
4604 return ret;
4605err:
4606#if defined(CONFIG_UNIX)
4607 sock_release(ctx->ring_sock);
4608 ctx->ring_sock = NULL;
4609#endif
4610 return ret;
4611}
4612
4613static int io_uring_create(unsigned entries, struct io_uring_params *p)
4614{
4615 struct user_struct *user = NULL;
4616 struct io_ring_ctx *ctx;
4617 bool account_mem;
4618 int ret;
4619
4620 if (!entries || entries > IORING_MAX_ENTRIES)
4621 return -EINVAL;
4622
4623 /*
4624 * Use twice as many entries for the CQ ring. It's possible for the
4625 * application to drive a higher depth than the size of the SQ ring,
4626 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06004627 * some flexibility in overcommitting a bit. If the application has
4628 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
4629 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07004630 */
4631 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06004632 if (p->flags & IORING_SETUP_CQSIZE) {
4633 /*
4634 * If IORING_SETUP_CQSIZE is set, we do the same roundup
4635 * to a power-of-two, if it isn't already. We do NOT impose
4636 * any cq vs sq ring sizing.
4637 */
4638 if (p->cq_entries < p->sq_entries || p->cq_entries > IORING_MAX_CQ_ENTRIES)
4639 return -EINVAL;
4640 p->cq_entries = roundup_pow_of_two(p->cq_entries);
4641 } else {
4642 p->cq_entries = 2 * p->sq_entries;
4643 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004644
4645 user = get_uid(current_user());
4646 account_mem = !capable(CAP_IPC_LOCK);
4647
4648 if (account_mem) {
4649 ret = io_account_mem(user,
4650 ring_pages(p->sq_entries, p->cq_entries));
4651 if (ret) {
4652 free_uid(user);
4653 return ret;
4654 }
4655 }
4656
4657 ctx = io_ring_ctx_alloc(p);
4658 if (!ctx) {
4659 if (account_mem)
4660 io_unaccount_mem(user, ring_pages(p->sq_entries,
4661 p->cq_entries));
4662 free_uid(user);
4663 return -ENOMEM;
4664 }
4665 ctx->compat = in_compat_syscall();
4666 ctx->account_mem = account_mem;
4667 ctx->user = user;
4668
4669 ret = io_allocate_scq_urings(ctx, p);
4670 if (ret)
4671 goto err;
4672
Jens Axboe6c271ce2019-01-10 11:22:30 -07004673 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004674 if (ret)
4675 goto err;
4676
Jens Axboe2b188cc2019-01-07 10:46:33 -07004677 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00004678 p->sq_off.head = offsetof(struct io_rings, sq.head);
4679 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
4680 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
4681 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
4682 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
4683 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
4684 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004685
4686 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00004687 p->cq_off.head = offsetof(struct io_rings, cq.head);
4688 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
4689 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
4690 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
4691 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
4692 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06004693
Jens Axboe044c1ab2019-10-28 09:15:33 -06004694 /*
4695 * Install ring fd as the very last thing, so we don't risk someone
4696 * having closed it before we finish setup
4697 */
4698 ret = io_uring_get_fd(ctx);
4699 if (ret < 0)
4700 goto err;
4701
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004702 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004703 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004704 return ret;
4705err:
4706 io_ring_ctx_wait_and_kill(ctx);
4707 return ret;
4708}
4709
4710/*
4711 * Sets up an aio uring context, and returns the fd. Applications asks for a
4712 * ring size, we return the actual sq/cq ring sizes (among other things) in the
4713 * params structure passed in.
4714 */
4715static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
4716{
4717 struct io_uring_params p;
4718 long ret;
4719 int i;
4720
4721 if (copy_from_user(&p, params, sizeof(p)))
4722 return -EFAULT;
4723 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
4724 if (p.resv[i])
4725 return -EINVAL;
4726 }
4727
Jens Axboe6c271ce2019-01-10 11:22:30 -07004728 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe33a107f2019-10-04 12:10:03 -06004729 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE))
Jens Axboe2b188cc2019-01-07 10:46:33 -07004730 return -EINVAL;
4731
4732 ret = io_uring_create(entries, &p);
4733 if (ret < 0)
4734 return ret;
4735
4736 if (copy_to_user(params, &p, sizeof(p)))
4737 return -EFAULT;
4738
4739 return ret;
4740}
4741
4742SYSCALL_DEFINE2(io_uring_setup, u32, entries,
4743 struct io_uring_params __user *, params)
4744{
4745 return io_uring_setup(entries, params);
4746}
4747
Jens Axboeedafcce2019-01-09 09:16:05 -07004748static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
4749 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06004750 __releases(ctx->uring_lock)
4751 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07004752{
4753 int ret;
4754
Jens Axboe35fa71a2019-04-22 10:23:23 -06004755 /*
4756 * We're inside the ring mutex, if the ref is already dying, then
4757 * someone else killed the ctx or is already going through
4758 * io_uring_register().
4759 */
4760 if (percpu_ref_is_dying(&ctx->refs))
4761 return -ENXIO;
4762
Jens Axboeedafcce2019-01-09 09:16:05 -07004763 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06004764
4765 /*
4766 * Drop uring mutex before waiting for references to exit. If another
4767 * thread is currently inside io_uring_enter() it might need to grab
4768 * the uring_lock to make progress. If we hold it here across the drain
4769 * wait, then we can deadlock. It's safe to drop the mutex here, since
4770 * no new references will come in after we've killed the percpu ref.
4771 */
4772 mutex_unlock(&ctx->uring_lock);
Jens Axboe206aefd2019-11-07 18:27:42 -07004773 wait_for_completion(&ctx->completions[0]);
Jens Axboeb19062a2019-04-15 10:49:38 -06004774 mutex_lock(&ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07004775
4776 switch (opcode) {
4777 case IORING_REGISTER_BUFFERS:
4778 ret = io_sqe_buffer_register(ctx, arg, nr_args);
4779 break;
4780 case IORING_UNREGISTER_BUFFERS:
4781 ret = -EINVAL;
4782 if (arg || nr_args)
4783 break;
4784 ret = io_sqe_buffer_unregister(ctx);
4785 break;
Jens Axboe6b063142019-01-10 22:13:58 -07004786 case IORING_REGISTER_FILES:
4787 ret = io_sqe_files_register(ctx, arg, nr_args);
4788 break;
4789 case IORING_UNREGISTER_FILES:
4790 ret = -EINVAL;
4791 if (arg || nr_args)
4792 break;
4793 ret = io_sqe_files_unregister(ctx);
4794 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06004795 case IORING_REGISTER_FILES_UPDATE:
4796 ret = io_sqe_files_update(ctx, arg, nr_args);
4797 break;
Jens Axboe9b402842019-04-11 11:45:41 -06004798 case IORING_REGISTER_EVENTFD:
4799 ret = -EINVAL;
4800 if (nr_args != 1)
4801 break;
4802 ret = io_eventfd_register(ctx, arg);
4803 break;
4804 case IORING_UNREGISTER_EVENTFD:
4805 ret = -EINVAL;
4806 if (arg || nr_args)
4807 break;
4808 ret = io_eventfd_unregister(ctx);
4809 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07004810 default:
4811 ret = -EINVAL;
4812 break;
4813 }
4814
4815 /* bring the ctx back to life */
Jens Axboe206aefd2019-11-07 18:27:42 -07004816 reinit_completion(&ctx->completions[0]);
Jens Axboeedafcce2019-01-09 09:16:05 -07004817 percpu_ref_reinit(&ctx->refs);
4818 return ret;
4819}
4820
4821SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
4822 void __user *, arg, unsigned int, nr_args)
4823{
4824 struct io_ring_ctx *ctx;
4825 long ret = -EBADF;
4826 struct fd f;
4827
4828 f = fdget(fd);
4829 if (!f.file)
4830 return -EBADF;
4831
4832 ret = -EOPNOTSUPP;
4833 if (f.file->f_op != &io_uring_fops)
4834 goto out_fput;
4835
4836 ctx = f.file->private_data;
4837
4838 mutex_lock(&ctx->uring_lock);
4839 ret = __io_uring_register(ctx, opcode, arg, nr_args);
4840 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004841 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
4842 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07004843out_fput:
4844 fdput(f);
4845 return ret;
4846}
4847
Jens Axboe2b188cc2019-01-07 10:46:33 -07004848static int __init io_uring_init(void)
4849{
4850 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
4851 return 0;
4852};
4853__initcall(io_uring_init);