blob: 39409934e9e6a5005a826e63092cc98c7963a418 [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 Axboe181e4482019-11-25 08:52:30 -0700240 struct cred *creds;
241
Jens Axboe206aefd2019-11-07 18:27:42 -0700242 /* 0 is for ctx quiesce/reinit/free, 1 is for sqo_thread started */
243 struct completion *completions;
244
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700245 /* if all else fails... */
246 struct io_kiocb *fallback_req;
247
Jens Axboe206aefd2019-11-07 18:27:42 -0700248#if defined(CONFIG_UNIX)
249 struct socket *ring_sock;
250#endif
251
252 struct {
253 unsigned cached_cq_tail;
254 unsigned cq_entries;
255 unsigned cq_mask;
256 atomic_t cq_timeouts;
257 struct wait_queue_head cq_wait;
258 struct fasync_struct *cq_fasync;
259 struct eventfd_ctx *cq_ev_fd;
260 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700261
262 struct {
263 struct mutex uring_lock;
264 wait_queue_head_t wait;
265 } ____cacheline_aligned_in_smp;
266
267 struct {
268 spinlock_t completion_lock;
Jens Axboedef596e2019-01-09 08:59:42 -0700269 bool poll_multi_file;
270 /*
271 * ->poll_list is protected by the ctx->uring_lock for
272 * io_uring instances that don't use IORING_SETUP_SQPOLL.
273 * For SQPOLL, only the single threaded io_sq_thread() will
274 * manipulate the list, hence no extra locking is needed there.
275 */
276 struct list_head poll_list;
Jens Axboeeac406c2019-11-14 12:09:58 -0700277 struct rb_root cancel_tree;
Jens Axboefcb323c2019-10-24 12:39:47 -0600278
279 spinlock_t inflight_lock;
280 struct list_head inflight_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700281 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700282};
283
284struct sqe_submit {
285 const struct io_uring_sqe *sqe;
Jens Axboefcb323c2019-10-24 12:39:47 -0600286 struct file *ring_file;
287 int ring_fd;
Jackie Liu8776f3f2019-09-09 20:50:39 +0800288 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700289 bool has_user;
Jackie Liuba5290c2019-10-09 09:19:59 +0800290 bool in_async;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700291 bool needs_fixed_file;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700292};
293
Jens Axboe09bb8392019-03-13 12:39:28 -0600294/*
295 * First field must be the file pointer in all the
296 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
297 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700298struct io_poll_iocb {
299 struct file *file;
300 struct wait_queue_head *head;
301 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600302 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700303 bool canceled;
304 struct wait_queue_entry wait;
305};
306
Jens Axboead8a48a2019-11-15 08:49:11 -0700307struct io_timeout_data {
308 struct io_kiocb *req;
309 struct hrtimer timer;
310 struct timespec64 ts;
311 enum hrtimer_mode mode;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +0300312 u32 seq_offset;
Jens Axboead8a48a2019-11-15 08:49:11 -0700313};
314
Jens Axboe5262f562019-09-17 12:26:57 -0600315struct io_timeout {
316 struct file *file;
Jens Axboead8a48a2019-11-15 08:49:11 -0700317 struct io_timeout_data *data;
Jens Axboe5262f562019-09-17 12:26:57 -0600318};
319
Jens Axboe09bb8392019-03-13 12:39:28 -0600320/*
321 * NOTE! Each of the iocb union members has the file pointer
322 * as the first entry in their struct definition. So you can
323 * access the file pointer through any of the sub-structs,
324 * or directly as just 'ki_filp' in this struct.
325 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700326struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700327 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600328 struct file *file;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700329 struct kiocb rw;
330 struct io_poll_iocb poll;
Jens Axboe5262f562019-09-17 12:26:57 -0600331 struct io_timeout timeout;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700332 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700333
334 struct sqe_submit submit;
335
336 struct io_ring_ctx *ctx;
Jens Axboeeac406c2019-11-14 12:09:58 -0700337 union {
338 struct list_head list;
339 struct rb_node rb_node;
340 };
Jens Axboe9e645e112019-05-10 16:07:28 -0600341 struct list_head link_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700342 unsigned int flags;
Jens Axboec16361c2019-01-17 08:39:48 -0700343 refcount_t refs;
Stefan Bühler8449eed2019-04-27 20:34:19 +0200344#define REQ_F_NOWAIT 1 /* must not punt to workers */
Jens Axboedef596e2019-01-09 08:59:42 -0700345#define REQ_F_IOPOLL_COMPLETED 2 /* polled IO has completed */
Jens Axboe6b063142019-01-10 22:13:58 -0700346#define REQ_F_FIXED_FILE 4 /* ctx owns file */
Jens Axboe4d7dd462019-11-20 13:03:52 -0700347#define REQ_F_LINK_NEXT 8 /* already grabbed next link */
Stefan Bühlere2033e32019-05-11 19:08:01 +0200348#define REQ_F_IO_DRAIN 16 /* drain existing IO first */
349#define REQ_F_IO_DRAINED 32 /* drain done */
Jens Axboe9e645e112019-05-10 16:07:28 -0600350#define REQ_F_LINK 64 /* linked sqes */
Jens Axboe2665abf2019-11-05 12:40:47 -0700351#define REQ_F_LINK_TIMEOUT 128 /* has linked timeout */
Zhengyuan Liuf7b76ac2019-07-16 23:26:14 +0800352#define REQ_F_FAIL_LINK 256 /* fail rest of links */
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +0300353#define REQ_F_DRAIN_LINK 512 /* link should be fully drained */
Jens Axboe5262f562019-09-17 12:26:57 -0600354#define REQ_F_TIMEOUT 1024 /* timeout request */
Jens Axboe491381ce2019-10-17 09:20:46 -0600355#define REQ_F_ISREG 2048 /* regular file */
356#define REQ_F_MUST_PUNT 4096 /* must be punted even for NONBLOCK */
Jens Axboe93bd25b2019-11-11 23:34:31 -0700357#define REQ_F_TIMEOUT_NOSEQ 8192 /* no timeout sequence */
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800358#define REQ_F_INFLIGHT 16384 /* on inflight list */
359#define REQ_F_COMP_LOCKED 32768 /* completion under lock */
Jens Axboe94ae5e72019-11-14 19:39:52 -0700360#define REQ_F_FREE_SQE 65536 /* free sqe if not async queued */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700361 u64 user_data;
Jens Axboe9e645e112019-05-10 16:07:28 -0600362 u32 result;
Jens Axboede0617e2019-04-06 21:51:27 -0600363 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700364
Jens Axboefcb323c2019-10-24 12:39:47 -0600365 struct list_head inflight_entry;
366
Jens Axboe561fb042019-10-24 07:25:42 -0600367 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700368};
369
370#define IO_PLUG_THRESHOLD 2
Jens Axboedef596e2019-01-09 08:59:42 -0700371#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700372
Jens Axboe9a56a232019-01-09 09:06:50 -0700373struct io_submit_state {
374 struct blk_plug plug;
375
376 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700377 * io_kiocb alloc cache
378 */
379 void *reqs[IO_IOPOLL_BATCH];
380 unsigned int free_reqs;
381 unsigned int cur_req;
382
383 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700384 * File reference cache
385 */
386 struct file *file;
387 unsigned int fd;
388 unsigned int has_refs;
389 unsigned int used_refs;
390 unsigned int ios_left;
391};
392
Jens Axboe561fb042019-10-24 07:25:42 -0600393static void io_wq_submit_work(struct io_wq_work **workptr);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700394static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liu4fe2c962019-09-09 20:50:40 +0800395static void __io_free_req(struct io_kiocb *req);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800396static void io_put_req(struct io_kiocb *req);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700397static void io_double_put_req(struct io_kiocb *req);
Jens Axboe978db572019-11-14 22:39:04 -0700398static void __io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700399static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
400static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboede0617e2019-04-06 21:51:27 -0600401
Jens Axboe2b188cc2019-01-07 10:46:33 -0700402static struct kmem_cache *req_cachep;
403
404static const struct file_operations io_uring_fops;
405
406struct sock *io_uring_get_socket(struct file *file)
407{
408#if defined(CONFIG_UNIX)
409 if (file->f_op == &io_uring_fops) {
410 struct io_ring_ctx *ctx = file->private_data;
411
412 return ctx->ring_sock->sk;
413 }
414#endif
415 return NULL;
416}
417EXPORT_SYMBOL(io_uring_get_socket);
418
419static void io_ring_ctx_ref_free(struct percpu_ref *ref)
420{
421 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
422
Jens Axboe206aefd2019-11-07 18:27:42 -0700423 complete(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700424}
425
426static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
427{
428 struct io_ring_ctx *ctx;
429
430 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
431 if (!ctx)
432 return NULL;
433
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700434 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
435 if (!ctx->fallback_req)
436 goto err;
437
Jens Axboe206aefd2019-11-07 18:27:42 -0700438 ctx->completions = kmalloc(2 * sizeof(struct completion), GFP_KERNEL);
439 if (!ctx->completions)
440 goto err;
441
Roman Gushchin21482892019-05-07 10:01:48 -0700442 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -0700443 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
444 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700445
446 ctx->flags = p->flags;
447 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700448 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe206aefd2019-11-07 18:27:42 -0700449 init_completion(&ctx->completions[0]);
450 init_completion(&ctx->completions[1]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700451 mutex_init(&ctx->uring_lock);
452 init_waitqueue_head(&ctx->wait);
453 spin_lock_init(&ctx->completion_lock);
Jens Axboedef596e2019-01-09 08:59:42 -0700454 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboeeac406c2019-11-14 12:09:58 -0700455 ctx->cancel_tree = RB_ROOT;
Jens Axboede0617e2019-04-06 21:51:27 -0600456 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -0600457 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -0600458 init_waitqueue_head(&ctx->inflight_wait);
459 spin_lock_init(&ctx->inflight_lock);
460 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700461 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -0700462err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700463 if (ctx->fallback_req)
464 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe206aefd2019-11-07 18:27:42 -0700465 kfree(ctx->completions);
466 kfree(ctx);
467 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700468}
469
Bob Liu9d858b22019-11-13 18:06:25 +0800470static inline bool __req_need_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -0600471{
Jackie Liua197f662019-11-08 08:09:12 -0700472 struct io_ring_ctx *ctx = req->ctx;
473
Jens Axboe498ccd92019-10-25 10:04:25 -0600474 return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped
475 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboede0617e2019-04-06 21:51:27 -0600476}
477
Bob Liu9d858b22019-11-13 18:06:25 +0800478static inline bool req_need_defer(struct io_kiocb *req)
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600479{
Bob Liu9d858b22019-11-13 18:06:25 +0800480 if ((req->flags & (REQ_F_IO_DRAIN|REQ_F_IO_DRAINED)) == REQ_F_IO_DRAIN)
481 return __req_need_defer(req);
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600482
Bob Liu9d858b22019-11-13 18:06:25 +0800483 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600484}
485
486static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -0600487{
488 struct io_kiocb *req;
489
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600490 req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list);
Bob Liu9d858b22019-11-13 18:06:25 +0800491 if (req && !req_need_defer(req)) {
Jens Axboede0617e2019-04-06 21:51:27 -0600492 list_del_init(&req->list);
493 return req;
494 }
495
496 return NULL;
497}
498
Jens Axboe5262f562019-09-17 12:26:57 -0600499static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx)
500{
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600501 struct io_kiocb *req;
502
503 req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list);
Jens Axboe93bd25b2019-11-11 23:34:31 -0700504 if (req) {
505 if (req->flags & REQ_F_TIMEOUT_NOSEQ)
506 return NULL;
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800507 if (!__req_need_defer(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -0700508 list_del_init(&req->list);
509 return req;
510 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600511 }
512
513 return NULL;
Jens Axboe5262f562019-09-17 12:26:57 -0600514}
515
Jens Axboede0617e2019-04-06 21:51:27 -0600516static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700517{
Hristo Venev75b28af2019-08-26 17:23:46 +0000518 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700519
Hristo Venev75b28af2019-08-26 17:23:46 +0000520 if (ctx->cached_cq_tail != READ_ONCE(rings->cq.tail)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700521 /* order cqe stores with ring update */
Hristo Venev75b28af2019-08-26 17:23:46 +0000522 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700523
Jens Axboe2b188cc2019-01-07 10:46:33 -0700524 if (wq_has_sleeper(&ctx->cq_wait)) {
525 wake_up_interruptible(&ctx->cq_wait);
526 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
527 }
528 }
529}
530
Jens Axboe561fb042019-10-24 07:25:42 -0600531static inline bool io_sqe_needs_user(const struct io_uring_sqe *sqe)
Jens Axboe18d9be12019-09-10 09:13:05 -0600532{
Jens Axboe561fb042019-10-24 07:25:42 -0600533 u8 opcode = READ_ONCE(sqe->opcode);
534
535 return !(opcode == IORING_OP_READ_FIXED ||
536 opcode == IORING_OP_WRITE_FIXED);
537}
538
Jens Axboe94ae5e72019-11-14 19:39:52 -0700539static inline bool io_prep_async_work(struct io_kiocb *req,
540 struct io_kiocb **link)
Jens Axboe561fb042019-10-24 07:25:42 -0600541{
542 bool do_hashed = false;
Jens Axboe54a91f32019-09-10 09:15:04 -0600543
Jens Axboe6cc47d12019-09-18 11:18:23 -0600544 if (req->submit.sqe) {
545 switch (req->submit.sqe->opcode) {
546 case IORING_OP_WRITEV:
547 case IORING_OP_WRITE_FIXED:
Jens Axboe561fb042019-10-24 07:25:42 -0600548 do_hashed = true;
Jens Axboe5f8fd2d2019-11-07 10:57:36 -0700549 /* fall-through */
550 case IORING_OP_READV:
551 case IORING_OP_READ_FIXED:
552 case IORING_OP_SENDMSG:
553 case IORING_OP_RECVMSG:
554 case IORING_OP_ACCEPT:
555 case IORING_OP_POLL_ADD:
Jens Axboef8e85cf2019-11-23 14:24:24 -0700556 case IORING_OP_CONNECT:
Jens Axboe5f8fd2d2019-11-07 10:57:36 -0700557 /*
558 * We know REQ_F_ISREG is not set on some of these
559 * opcodes, but this enables us to keep the check in
560 * just one place.
561 */
562 if (!(req->flags & REQ_F_ISREG))
563 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe6cc47d12019-09-18 11:18:23 -0600564 break;
565 }
Jens Axboe561fb042019-10-24 07:25:42 -0600566 if (io_sqe_needs_user(req->submit.sqe))
567 req->work.flags |= IO_WQ_WORK_NEEDS_USER;
Jens Axboe54a91f32019-09-10 09:15:04 -0600568 }
569
Jens Axboe94ae5e72019-11-14 19:39:52 -0700570 *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -0600571 return do_hashed;
572}
573
Jackie Liua197f662019-11-08 08:09:12 -0700574static inline void io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -0600575{
Jackie Liua197f662019-11-08 08:09:12 -0700576 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe94ae5e72019-11-14 19:39:52 -0700577 struct io_kiocb *link;
578 bool do_hashed;
579
580 do_hashed = io_prep_async_work(req, &link);
Jens Axboe561fb042019-10-24 07:25:42 -0600581
582 trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work,
583 req->flags);
584 if (!do_hashed) {
585 io_wq_enqueue(ctx->io_wq, &req->work);
586 } else {
587 io_wq_enqueue_hashed(ctx->io_wq, &req->work,
588 file_inode(req->file));
589 }
Jens Axboe94ae5e72019-11-14 19:39:52 -0700590
591 if (link)
592 io_queue_linked_timeout(link);
Jens Axboe18d9be12019-09-10 09:13:05 -0600593}
594
Jens Axboe5262f562019-09-17 12:26:57 -0600595static void io_kill_timeout(struct io_kiocb *req)
596{
597 int ret;
598
Jens Axboead8a48a2019-11-15 08:49:11 -0700599 ret = hrtimer_try_to_cancel(&req->timeout.data->timer);
Jens Axboe5262f562019-09-17 12:26:57 -0600600 if (ret != -1) {
601 atomic_inc(&req->ctx->cq_timeouts);
Jens Axboe842f9612019-10-29 12:34:10 -0600602 list_del_init(&req->list);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700603 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800604 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -0600605 }
606}
607
608static void io_kill_timeouts(struct io_ring_ctx *ctx)
609{
610 struct io_kiocb *req, *tmp;
611
612 spin_lock_irq(&ctx->completion_lock);
613 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
614 io_kill_timeout(req);
615 spin_unlock_irq(&ctx->completion_lock);
616}
617
Jens Axboede0617e2019-04-06 21:51:27 -0600618static void io_commit_cqring(struct io_ring_ctx *ctx)
619{
620 struct io_kiocb *req;
621
Jens Axboe5262f562019-09-17 12:26:57 -0600622 while ((req = io_get_timeout_req(ctx)) != NULL)
623 io_kill_timeout(req);
624
Jens Axboede0617e2019-04-06 21:51:27 -0600625 __io_commit_cqring(ctx);
626
627 while ((req = io_get_deferred_req(ctx)) != NULL) {
628 req->flags |= REQ_F_IO_DRAINED;
Jackie Liua197f662019-11-08 08:09:12 -0700629 io_queue_async_work(req);
Jens Axboede0617e2019-04-06 21:51:27 -0600630 }
631}
632
Jens Axboe2b188cc2019-01-07 10:46:33 -0700633static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
634{
Hristo Venev75b28af2019-08-26 17:23:46 +0000635 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700636 unsigned tail;
637
638 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +0200639 /*
640 * writes to the cq entry need to come after reading head; the
641 * control dependency is enough as we're using WRITE_ONCE to
642 * fill the cq entry
643 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000644 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700645 return NULL;
646
647 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +0000648 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -0700649}
650
Jens Axboe8c838782019-03-12 15:48:16 -0600651static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
652{
653 if (waitqueue_active(&ctx->wait))
654 wake_up(&ctx->wait);
655 if (waitqueue_active(&ctx->sqo_wait))
656 wake_up(&ctx->sqo_wait);
Jens Axboe9b402842019-04-11 11:45:41 -0600657 if (ctx->cq_ev_fd)
658 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -0600659}
660
Jens Axboec4a2ed72019-11-21 21:01:26 -0700661/* Returns true if there are no backlogged entries after the flush */
662static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700663{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700664 struct io_rings *rings = ctx->rings;
665 struct io_uring_cqe *cqe;
666 struct io_kiocb *req;
667 unsigned long flags;
668 LIST_HEAD(list);
669
670 if (!force) {
671 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -0700672 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700673 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
674 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -0700675 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700676 }
677
678 spin_lock_irqsave(&ctx->completion_lock, flags);
679
680 /* if force is set, the ring is going away. always drop after that */
681 if (force)
682 ctx->cq_overflow_flushed = true;
683
Jens Axboec4a2ed72019-11-21 21:01:26 -0700684 cqe = NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700685 while (!list_empty(&ctx->cq_overflow_list)) {
686 cqe = io_get_cqring(ctx);
687 if (!cqe && !force)
688 break;
689
690 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
691 list);
692 list_move(&req->list, &list);
693 if (cqe) {
694 WRITE_ONCE(cqe->user_data, req->user_data);
695 WRITE_ONCE(cqe->res, req->result);
696 WRITE_ONCE(cqe->flags, 0);
697 } else {
698 WRITE_ONCE(ctx->rings->cq_overflow,
699 atomic_inc_return(&ctx->cached_cq_overflow));
700 }
701 }
702
703 io_commit_cqring(ctx);
704 spin_unlock_irqrestore(&ctx->completion_lock, flags);
705 io_cqring_ev_posted(ctx);
706
707 while (!list_empty(&list)) {
708 req = list_first_entry(&list, struct io_kiocb, list);
709 list_del(&req->list);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800710 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700711 }
Jens Axboec4a2ed72019-11-21 21:01:26 -0700712
713 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700714}
715
Jens Axboe78e19bb2019-11-06 15:21:34 -0700716static void io_cqring_fill_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700717{
Jens Axboe78e19bb2019-11-06 15:21:34 -0700718 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700719 struct io_uring_cqe *cqe;
720
Jens Axboe78e19bb2019-11-06 15:21:34 -0700721 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -0700722
Jens Axboe2b188cc2019-01-07 10:46:33 -0700723 /*
724 * If we can't get a cq entry, userspace overflowed the
725 * submission (by quite a lot). Increment the overflow count in
726 * the ring.
727 */
728 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700729 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -0700730 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700731 WRITE_ONCE(cqe->res, res);
732 WRITE_ONCE(cqe->flags, 0);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700733 } else if (ctx->cq_overflow_flushed) {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700734 WRITE_ONCE(ctx->rings->cq_overflow,
735 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700736 } else {
737 refcount_inc(&req->refs);
738 req->result = res;
739 list_add_tail(&req->list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700740 }
741}
742
Jens Axboe78e19bb2019-11-06 15:21:34 -0700743static void io_cqring_add_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700744{
Jens Axboe78e19bb2019-11-06 15:21:34 -0700745 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700746 unsigned long flags;
747
748 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700749 io_cqring_fill_event(req, res);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700750 io_commit_cqring(ctx);
751 spin_unlock_irqrestore(&ctx->completion_lock, flags);
752
Jens Axboe8c838782019-03-12 15:48:16 -0600753 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700754}
755
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700756static inline bool io_is_fallback_req(struct io_kiocb *req)
757{
758 return req == (struct io_kiocb *)
759 ((unsigned long) req->ctx->fallback_req & ~1UL);
760}
761
762static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
763{
764 struct io_kiocb *req;
765
766 req = ctx->fallback_req;
767 if (!test_and_set_bit_lock(0, (unsigned long *) ctx->fallback_req))
768 return req;
769
770 return NULL;
771}
772
Jens Axboe2579f912019-01-09 09:10:43 -0700773static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
774 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700775{
Jens Axboefd6fab22019-03-14 16:30:06 -0600776 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700777 struct io_kiocb *req;
778
779 if (!percpu_ref_tryget(&ctx->refs))
780 return NULL;
781
Jens Axboe2579f912019-01-09 09:10:43 -0700782 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -0600783 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -0700784 if (unlikely(!req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700785 goto fallback;
Jens Axboe2579f912019-01-09 09:10:43 -0700786 } else if (!state->free_reqs) {
787 size_t sz;
788 int ret;
789
790 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -0600791 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
792
793 /*
794 * Bulk alloc is all-or-nothing. If we fail to get a batch,
795 * retry single alloc to be on the safe side.
796 */
797 if (unlikely(ret <= 0)) {
798 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
799 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700800 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -0600801 ret = 1;
802 }
Jens Axboe2579f912019-01-09 09:10:43 -0700803 state->free_reqs = ret - 1;
804 state->cur_req = 1;
805 req = state->reqs[0];
806 } else {
807 req = state->reqs[state->cur_req];
808 state->free_reqs--;
809 state->cur_req++;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700810 }
811
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700812got_it:
Jens Axboe60c112b2019-06-21 10:20:18 -0600813 req->file = NULL;
Jens Axboe2579f912019-01-09 09:10:43 -0700814 req->ctx = ctx;
815 req->flags = 0;
Jens Axboee65ef562019-03-12 10:16:44 -0600816 /* one is dropped after submission, the other at completion */
817 refcount_set(&req->refs, 2);
Jens Axboe9e645e112019-05-10 16:07:28 -0600818 req->result = 0;
Jens Axboe561fb042019-10-24 07:25:42 -0600819 INIT_IO_WORK(&req->work, io_wq_submit_work);
Jens Axboe2579f912019-01-09 09:10:43 -0700820 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700821fallback:
822 req = io_get_fallback_req(ctx);
823 if (req)
824 goto got_it;
Pavel Begunkov6805b322019-10-08 02:18:42 +0300825 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700826 return NULL;
827}
828
Jens Axboedef596e2019-01-09 08:59:42 -0700829static void io_free_req_many(struct io_ring_ctx *ctx, void **reqs, int *nr)
830{
831 if (*nr) {
832 kmem_cache_free_bulk(req_cachep, *nr, reqs);
Pavel Begunkov6805b322019-10-08 02:18:42 +0300833 percpu_ref_put_many(&ctx->refs, *nr);
Jens Axboedef596e2019-01-09 08:59:42 -0700834 *nr = 0;
835 }
836}
837
Jens Axboe9e645e112019-05-10 16:07:28 -0600838static void __io_free_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700839{
Jens Axboefcb323c2019-10-24 12:39:47 -0600840 struct io_ring_ctx *ctx = req->ctx;
841
Pavel Begunkovbbad27b2019-11-19 23:32:47 +0300842 if (req->flags & REQ_F_FREE_SQE)
843 kfree(req->submit.sqe);
Jens Axboe09bb8392019-03-13 12:39:28 -0600844 if (req->file && !(req->flags & REQ_F_FIXED_FILE))
845 fput(req->file);
Jens Axboefcb323c2019-10-24 12:39:47 -0600846 if (req->flags & REQ_F_INFLIGHT) {
847 unsigned long flags;
848
849 spin_lock_irqsave(&ctx->inflight_lock, flags);
850 list_del(&req->inflight_entry);
851 if (waitqueue_active(&ctx->inflight_wait))
852 wake_up(&ctx->inflight_wait);
853 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
854 }
Jens Axboead8a48a2019-11-15 08:49:11 -0700855 if (req->flags & REQ_F_TIMEOUT)
856 kfree(req->timeout.data);
Jens Axboefcb323c2019-10-24 12:39:47 -0600857 percpu_ref_put(&ctx->refs);
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700858 if (likely(!io_is_fallback_req(req)))
859 kmem_cache_free(req_cachep, req);
860 else
861 clear_bit_unlock(0, (unsigned long *) ctx->fallback_req);
Jens Axboee65ef562019-03-12 10:16:44 -0600862}
863
Jackie Liua197f662019-11-08 08:09:12 -0700864static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -0600865{
Jackie Liua197f662019-11-08 08:09:12 -0700866 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -0700867 int ret;
868
Jens Axboead8a48a2019-11-15 08:49:11 -0700869 ret = hrtimer_try_to_cancel(&req->timeout.data->timer);
Jens Axboe2665abf2019-11-05 12:40:47 -0700870 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -0700871 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -0700872 io_commit_cqring(ctx);
873 req->flags &= ~REQ_F_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +0800874 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -0700875 return true;
876 }
877
878 return false;
879}
880
Jens Axboeba816ad2019-09-28 11:36:45 -0600881static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -0600882{
Jens Axboe2665abf2019-11-05 12:40:47 -0700883 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -0600884 struct io_kiocb *nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -0700885 bool wake_ev = false;
Jens Axboe9e645e112019-05-10 16:07:28 -0600886
Jens Axboe4d7dd462019-11-20 13:03:52 -0700887 /* Already got next link */
888 if (req->flags & REQ_F_LINK_NEXT)
889 return;
890
Jens Axboe9e645e112019-05-10 16:07:28 -0600891 /*
892 * The list should never be empty when we are called here. But could
893 * potentially happen if the chain is messed up, check to be on the
894 * safe side.
895 */
896 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, list);
Jens Axboe2665abf2019-11-05 12:40:47 -0700897 while (nxt) {
Jens Axboe76a46e02019-11-10 23:34:16 -0700898 list_del_init(&nxt->list);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700899
900 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
901 (nxt->flags & REQ_F_TIMEOUT)) {
902 wake_ev |= io_link_cancel_timeout(nxt);
903 nxt = list_first_entry_or_null(&req->link_list,
904 struct io_kiocb, list);
905 req->flags &= ~REQ_F_LINK_TIMEOUT;
906 continue;
907 }
Jens Axboe9e645e112019-05-10 16:07:28 -0600908 if (!list_empty(&req->link_list)) {
909 INIT_LIST_HEAD(&nxt->link_list);
910 list_splice(&req->link_list, &nxt->link_list);
911 nxt->flags |= REQ_F_LINK;
912 }
913
Pavel Begunkovb18fdf72019-11-21 23:21:02 +0300914 *nxtptr = nxt;
Jens Axboe94ae5e72019-11-14 19:39:52 -0700915 break;
Jens Axboe9e645e112019-05-10 16:07:28 -0600916 }
Jens Axboe2665abf2019-11-05 12:40:47 -0700917
Jens Axboe4d7dd462019-11-20 13:03:52 -0700918 req->flags |= REQ_F_LINK_NEXT;
Jens Axboe2665abf2019-11-05 12:40:47 -0700919 if (wake_ev)
920 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -0600921}
922
923/*
924 * Called if REQ_F_LINK is set, and we fail the head request
925 */
926static void io_fail_links(struct io_kiocb *req)
927{
Jens Axboe2665abf2019-11-05 12:40:47 -0700928 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -0600929 struct io_kiocb *link;
Jens Axboe2665abf2019-11-05 12:40:47 -0700930 unsigned long flags;
931
932 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -0600933
934 while (!list_empty(&req->link_list)) {
935 link = list_first_entry(&req->link_list, struct io_kiocb, list);
Jens Axboe2665abf2019-11-05 12:40:47 -0700936 list_del_init(&link->list);
Jens Axboe9e645e112019-05-10 16:07:28 -0600937
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +0200938 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -0700939
940 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
941 link->submit.sqe->opcode == IORING_OP_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -0700942 io_link_cancel_timeout(link);
Jens Axboe2665abf2019-11-05 12:40:47 -0700943 } else {
Jens Axboe78e19bb2019-11-06 15:21:34 -0700944 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe978db572019-11-14 22:39:04 -0700945 __io_double_put_req(link);
Jens Axboe2665abf2019-11-05 12:40:47 -0700946 }
Jens Axboe5d960722019-11-19 15:31:28 -0700947 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -0600948 }
Jens Axboe2665abf2019-11-05 12:40:47 -0700949
950 io_commit_cqring(ctx);
951 spin_unlock_irqrestore(&ctx->completion_lock, flags);
952 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -0600953}
954
Jens Axboe4d7dd462019-11-20 13:03:52 -0700955static void io_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -0600956{
Jens Axboe4d7dd462019-11-20 13:03:52 -0700957 if (likely(!(req->flags & REQ_F_LINK)))
Jens Axboe2665abf2019-11-05 12:40:47 -0700958 return;
Jens Axboe2665abf2019-11-05 12:40:47 -0700959
Jens Axboe9e645e112019-05-10 16:07:28 -0600960 /*
961 * If LINK is set, we have dependent requests in this chain. If we
962 * didn't fail this request, queue the first one up, moving any other
963 * dependencies to the next request. In case of failure, fail the rest
964 * of the chain.
965 */
Jens Axboe2665abf2019-11-05 12:40:47 -0700966 if (req->flags & REQ_F_FAIL_LINK) {
967 io_fail_links(req);
Jens Axboe7c9e7f02019-11-12 08:15:53 -0700968 } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) ==
969 REQ_F_LINK_TIMEOUT) {
Jens Axboe2665abf2019-11-05 12:40:47 -0700970 struct io_ring_ctx *ctx = req->ctx;
971 unsigned long flags;
972
973 /*
974 * If this is a timeout link, we could be racing with the
975 * timeout timer. Grab the completion lock for this case to
Jens Axboe7c9e7f02019-11-12 08:15:53 -0700976 * protect against that.
Jens Axboe2665abf2019-11-05 12:40:47 -0700977 */
978 spin_lock_irqsave(&ctx->completion_lock, flags);
979 io_req_link_next(req, nxt);
980 spin_unlock_irqrestore(&ctx->completion_lock, flags);
981 } else {
982 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -0600983 }
Jens Axboe4d7dd462019-11-20 13:03:52 -0700984}
Jens Axboe9e645e112019-05-10 16:07:28 -0600985
Jackie Liuc69f8db2019-11-09 11:00:08 +0800986static void io_free_req(struct io_kiocb *req)
987{
Pavel Begunkov944e58b2019-11-21 23:21:01 +0300988 struct io_kiocb *nxt = NULL;
989
990 io_req_find_next(req, &nxt);
Pavel Begunkov70cf9f32019-11-21 23:21:00 +0300991 __io_free_req(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +0300992
993 if (nxt)
994 io_queue_async_work(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +0800995}
996
Jens Axboeba816ad2019-09-28 11:36:45 -0600997/*
998 * Drop reference to request, return next in chain (if there is one) if this
999 * was the last reference to this request.
1000 */
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001001__attribute__((nonnull))
Jackie Liuec9c02a2019-11-08 23:50:36 +08001002static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboee65ef562019-03-12 10:16:44 -06001003{
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001004 io_req_find_next(req, nxtptr);
Jens Axboe4d7dd462019-11-20 13:03:52 -07001005
Jens Axboee65ef562019-03-12 10:16:44 -06001006 if (refcount_dec_and_test(&req->refs))
Jens Axboe4d7dd462019-11-20 13:03:52 -07001007 __io_free_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001008}
1009
Jens Axboe2b188cc2019-01-07 10:46:33 -07001010static void io_put_req(struct io_kiocb *req)
1011{
Jens Axboedef596e2019-01-09 08:59:42 -07001012 if (refcount_dec_and_test(&req->refs))
1013 io_free_req(req);
1014}
1015
Jens Axboe978db572019-11-14 22:39:04 -07001016/*
1017 * Must only be used if we don't need to care about links, usually from
1018 * within the completion handling itself.
1019 */
1020static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06001021{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001022 /* drop both submit and complete references */
1023 if (refcount_sub_and_test(2, &req->refs))
1024 __io_free_req(req);
1025}
1026
Jens Axboe978db572019-11-14 22:39:04 -07001027static void io_double_put_req(struct io_kiocb *req)
1028{
1029 /* drop both submit and complete references */
1030 if (refcount_sub_and_test(2, &req->refs))
1031 io_free_req(req);
1032}
1033
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001034static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06001035{
Jens Axboe84f97dc2019-11-06 11:27:53 -07001036 struct io_rings *rings = ctx->rings;
1037
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001038 /*
1039 * noflush == true is from the waitqueue handler, just ensure we wake
1040 * up the task, and the next invocation will flush the entries. We
1041 * cannot safely to it from here.
1042 */
1043 if (noflush && !list_empty(&ctx->cq_overflow_list))
1044 return -1U;
1045
1046 io_cqring_overflow_flush(ctx, false);
1047
Jens Axboea3a0e432019-08-20 11:03:11 -06001048 /* See comment at the top of this file */
1049 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00001050 return READ_ONCE(rings->cq.tail) - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06001051}
1052
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03001053static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1054{
1055 struct io_rings *rings = ctx->rings;
1056
1057 /* make sure SQ entry isn't read before tail */
1058 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1059}
1060
Jens Axboedef596e2019-01-09 08:59:42 -07001061/*
1062 * Find and free completed poll iocbs
1063 */
1064static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
1065 struct list_head *done)
1066{
1067 void *reqs[IO_IOPOLL_BATCH];
1068 struct io_kiocb *req;
Jens Axboe09bb8392019-03-13 12:39:28 -06001069 int to_free;
Jens Axboedef596e2019-01-09 08:59:42 -07001070
Jens Axboe09bb8392019-03-13 12:39:28 -06001071 to_free = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001072 while (!list_empty(done)) {
1073 req = list_first_entry(done, struct io_kiocb, list);
1074 list_del(&req->list);
1075
Jens Axboe78e19bb2019-11-06 15:21:34 -07001076 io_cqring_fill_event(req, req->result);
Jens Axboedef596e2019-01-09 08:59:42 -07001077 (*nr_events)++;
1078
Jens Axboe09bb8392019-03-13 12:39:28 -06001079 if (refcount_dec_and_test(&req->refs)) {
1080 /* If we're not using fixed files, we have to pair the
1081 * completion part with the file put. Use regular
1082 * completions for those, only batch free for fixed
Jens Axboe9e645e112019-05-10 16:07:28 -06001083 * file and non-linked commands.
Jens Axboe09bb8392019-03-13 12:39:28 -06001084 */
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03001085 if (((req->flags &
1086 (REQ_F_FIXED_FILE|REQ_F_LINK|REQ_F_FREE_SQE)) ==
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001087 REQ_F_FIXED_FILE) && !io_is_fallback_req(req)) {
Jens Axboe09bb8392019-03-13 12:39:28 -06001088 reqs[to_free++] = req;
1089 if (to_free == ARRAY_SIZE(reqs))
1090 io_free_req_many(ctx, reqs, &to_free);
Jens Axboe6b063142019-01-10 22:13:58 -07001091 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001092 io_free_req(req);
Jens Axboe6b063142019-01-10 22:13:58 -07001093 }
Jens Axboe9a56a232019-01-09 09:06:50 -07001094 }
Jens Axboedef596e2019-01-09 08:59:42 -07001095 }
Jens Axboedef596e2019-01-09 08:59:42 -07001096
Jens Axboe09bb8392019-03-13 12:39:28 -06001097 io_commit_cqring(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07001098 io_free_req_many(ctx, reqs, &to_free);
1099}
1100
1101static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1102 long min)
1103{
1104 struct io_kiocb *req, *tmp;
1105 LIST_HEAD(done);
1106 bool spin;
1107 int ret;
1108
1109 /*
1110 * Only spin for completions if we don't have multiple devices hanging
1111 * off our complete list, and we're under the requested amount.
1112 */
1113 spin = !ctx->poll_multi_file && *nr_events < min;
1114
1115 ret = 0;
1116 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
1117 struct kiocb *kiocb = &req->rw;
1118
1119 /*
1120 * Move completed entries to our local list. If we find a
1121 * request that requires polling, break out and complete
1122 * the done list first, if we have entries there.
1123 */
1124 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
1125 list_move_tail(&req->list, &done);
1126 continue;
1127 }
1128 if (!list_empty(&done))
1129 break;
1130
1131 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1132 if (ret < 0)
1133 break;
1134
1135 if (ret && spin)
1136 spin = false;
1137 ret = 0;
1138 }
1139
1140 if (!list_empty(&done))
1141 io_iopoll_complete(ctx, nr_events, &done);
1142
1143 return ret;
1144}
1145
1146/*
1147 * Poll for a mininum of 'min' events. Note that if min == 0 we consider that a
1148 * non-spinning poll check - we'll still enter the driver poll loop, but only
1149 * as a non-spinning completion check.
1150 */
1151static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
1152 long min)
1153{
Jens Axboe08f54392019-08-21 22:19:11 -06001154 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07001155 int ret;
1156
1157 ret = io_do_iopoll(ctx, nr_events, min);
1158 if (ret < 0)
1159 return ret;
1160 if (!min || *nr_events >= min)
1161 return 0;
1162 }
1163
1164 return 1;
1165}
1166
1167/*
1168 * We can't just wait for polled events to come to us, we have to actively
1169 * find and complete them.
1170 */
1171static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
1172{
1173 if (!(ctx->flags & IORING_SETUP_IOPOLL))
1174 return;
1175
1176 mutex_lock(&ctx->uring_lock);
1177 while (!list_empty(&ctx->poll_list)) {
1178 unsigned int nr_events = 0;
1179
1180 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -06001181
1182 /*
1183 * Ensure we allow local-to-the-cpu processing to take place,
1184 * in this case we need to ensure that we reap all events.
1185 */
1186 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -07001187 }
1188 mutex_unlock(&ctx->uring_lock);
1189}
1190
Jens Axboe2b2ed972019-10-25 10:06:15 -06001191static int __io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1192 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07001193{
Jens Axboe2b2ed972019-10-25 10:06:15 -06001194 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001195
1196 do {
1197 int tmin = 0;
1198
Jens Axboe500f9fb2019-08-19 12:15:59 -06001199 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06001200 * Don't enter poll loop if we already have events pending.
1201 * If we do, we can potentially be spinning for commands that
1202 * already triggered a CQE (eg in error).
1203 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001204 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06001205 break;
1206
1207 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06001208 * If a submit got punted to a workqueue, we can have the
1209 * application entering polling for a command before it gets
1210 * issued. That app will hold the uring_lock for the duration
1211 * of the poll right here, so we need to take a breather every
1212 * now and then to ensure that the issue has a chance to add
1213 * the poll to the issued list. Otherwise we can spin here
1214 * forever, while the workqueue is stuck trying to acquire the
1215 * very same mutex.
1216 */
1217 if (!(++iters & 7)) {
1218 mutex_unlock(&ctx->uring_lock);
1219 mutex_lock(&ctx->uring_lock);
1220 }
1221
Jens Axboedef596e2019-01-09 08:59:42 -07001222 if (*nr_events < min)
1223 tmin = min - *nr_events;
1224
1225 ret = io_iopoll_getevents(ctx, nr_events, tmin);
1226 if (ret <= 0)
1227 break;
1228 ret = 0;
1229 } while (min && !*nr_events && !need_resched());
1230
Jens Axboe2b2ed972019-10-25 10:06:15 -06001231 return ret;
1232}
1233
1234static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1235 long min)
1236{
1237 int ret;
1238
1239 /*
1240 * We disallow the app entering submit/complete with polling, but we
1241 * still need to lock the ring to prevent racing with polled issue
1242 * that got punted to a workqueue.
1243 */
1244 mutex_lock(&ctx->uring_lock);
1245 ret = __io_iopoll_check(ctx, nr_events, min);
Jens Axboe500f9fb2019-08-19 12:15:59 -06001246 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001247 return ret;
1248}
1249
Jens Axboe491381ce2019-10-17 09:20:46 -06001250static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001251{
Jens Axboe491381ce2019-10-17 09:20:46 -06001252 /*
1253 * Tell lockdep we inherited freeze protection from submission
1254 * thread.
1255 */
1256 if (req->flags & REQ_F_ISREG) {
1257 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001258
Jens Axboe491381ce2019-10-17 09:20:46 -06001259 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001260 }
Jens Axboe491381ce2019-10-17 09:20:46 -06001261 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001262}
1263
Jens Axboeba816ad2019-09-28 11:36:45 -06001264static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001265{
1266 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1267
Jens Axboe491381ce2019-10-17 09:20:46 -06001268 if (kiocb->ki_flags & IOCB_WRITE)
1269 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001270
Jens Axboe9e645e112019-05-10 16:07:28 -06001271 if ((req->flags & REQ_F_LINK) && res != req->result)
1272 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001273 io_cqring_add_event(req, res);
Jens Axboeba816ad2019-09-28 11:36:45 -06001274}
1275
1276static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1277{
1278 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1279
1280 io_complete_rw_common(kiocb, res);
Jens Axboee65ef562019-03-12 10:16:44 -06001281 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001282}
1283
Jens Axboeba816ad2019-09-28 11:36:45 -06001284static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res)
1285{
1286 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001287 struct io_kiocb *nxt = NULL;
Jens Axboeba816ad2019-09-28 11:36:45 -06001288
1289 io_complete_rw_common(kiocb, res);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001290 io_put_req_find_next(req, &nxt);
1291
1292 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001293}
1294
Jens Axboedef596e2019-01-09 08:59:42 -07001295static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1296{
1297 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1298
Jens Axboe491381ce2019-10-17 09:20:46 -06001299 if (kiocb->ki_flags & IOCB_WRITE)
1300 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001301
Jens Axboe9e645e112019-05-10 16:07:28 -06001302 if ((req->flags & REQ_F_LINK) && res != req->result)
1303 req->flags |= REQ_F_FAIL_LINK;
1304 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -07001305 if (res != -EAGAIN)
1306 req->flags |= REQ_F_IOPOLL_COMPLETED;
1307}
1308
1309/*
1310 * After the iocb has been issued, it's safe to be found on the poll list.
1311 * Adding the kiocb to the list AFTER submission ensures that we don't
1312 * find it from a io_iopoll_getevents() thread before the issuer is done
1313 * accessing the kiocb cookie.
1314 */
1315static void io_iopoll_req_issued(struct io_kiocb *req)
1316{
1317 struct io_ring_ctx *ctx = req->ctx;
1318
1319 /*
1320 * Track whether we have multiple files in our lists. This will impact
1321 * how we do polling eventually, not spinning if we're on potentially
1322 * different devices.
1323 */
1324 if (list_empty(&ctx->poll_list)) {
1325 ctx->poll_multi_file = false;
1326 } else if (!ctx->poll_multi_file) {
1327 struct io_kiocb *list_req;
1328
1329 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1330 list);
1331 if (list_req->rw.ki_filp != req->rw.ki_filp)
1332 ctx->poll_multi_file = true;
1333 }
1334
1335 /*
1336 * For fast devices, IO may have already completed. If it has, add
1337 * it to the front so we find it first.
1338 */
1339 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1340 list_add(&req->list, &ctx->poll_list);
1341 else
1342 list_add_tail(&req->list, &ctx->poll_list);
1343}
1344
Jens Axboe3d6770f2019-04-13 11:50:54 -06001345static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07001346{
Jens Axboe3d6770f2019-04-13 11:50:54 -06001347 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -07001348 int diff = state->has_refs - state->used_refs;
1349
1350 if (diff)
1351 fput_many(state->file, diff);
1352 state->file = NULL;
1353 }
1354}
1355
1356/*
1357 * Get as many references to a file as we have IOs left in this submission,
1358 * assuming most submissions are for one file, or at least that each file
1359 * has more than one submission.
1360 */
1361static struct file *io_file_get(struct io_submit_state *state, int fd)
1362{
1363 if (!state)
1364 return fget(fd);
1365
1366 if (state->file) {
1367 if (state->fd == fd) {
1368 state->used_refs++;
1369 state->ios_left--;
1370 return state->file;
1371 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06001372 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07001373 }
1374 state->file = fget_many(fd, state->ios_left);
1375 if (!state->file)
1376 return NULL;
1377
1378 state->fd = fd;
1379 state->has_refs = state->ios_left;
1380 state->used_refs = 1;
1381 state->ios_left--;
1382 return state->file;
1383}
1384
Jens Axboe2b188cc2019-01-07 10:46:33 -07001385/*
1386 * If we tracked the file through the SCM inflight mechanism, we could support
1387 * any file. For now, just ensure that anything potentially problematic is done
1388 * inline.
1389 */
1390static bool io_file_supports_async(struct file *file)
1391{
1392 umode_t mode = file_inode(file)->i_mode;
1393
1394 if (S_ISBLK(mode) || S_ISCHR(mode))
1395 return true;
1396 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
1397 return true;
1398
1399 return false;
1400}
1401
Pavel Begunkov267bc902019-11-07 01:41:08 +03001402static int io_prep_rw(struct io_kiocb *req, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001403{
Pavel Begunkov267bc902019-11-07 01:41:08 +03001404 const struct io_uring_sqe *sqe = req->submit.sqe;
Jens Axboedef596e2019-01-09 08:59:42 -07001405 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001406 struct kiocb *kiocb = &req->rw;
Jens Axboe09bb8392019-03-13 12:39:28 -06001407 unsigned ioprio;
1408 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001409
Jens Axboe09bb8392019-03-13 12:39:28 -06001410 if (!req->file)
1411 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001412
Jens Axboe491381ce2019-10-17 09:20:46 -06001413 if (S_ISREG(file_inode(req->file)->i_mode))
1414 req->flags |= REQ_F_ISREG;
1415
1416 /*
1417 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
1418 * we know to async punt it even if it was opened O_NONBLOCK
1419 */
1420 if (force_nonblock && !io_file_supports_async(req->file)) {
1421 req->flags |= REQ_F_MUST_PUNT;
1422 return -EAGAIN;
1423 }
Jens Axboe6b063142019-01-10 22:13:58 -07001424
Jens Axboe2b188cc2019-01-07 10:46:33 -07001425 kiocb->ki_pos = READ_ONCE(sqe->off);
1426 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
1427 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
1428
1429 ioprio = READ_ONCE(sqe->ioprio);
1430 if (ioprio) {
1431 ret = ioprio_check_cap(ioprio);
1432 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06001433 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001434
1435 kiocb->ki_ioprio = ioprio;
1436 } else
1437 kiocb->ki_ioprio = get_current_ioprio();
1438
1439 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
1440 if (unlikely(ret))
Jens Axboe09bb8392019-03-13 12:39:28 -06001441 return ret;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001442
1443 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06001444 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
1445 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02001446 req->flags |= REQ_F_NOWAIT;
1447
1448 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001449 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001450
Jens Axboedef596e2019-01-09 08:59:42 -07001451 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07001452 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
1453 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06001454 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001455
Jens Axboedef596e2019-01-09 08:59:42 -07001456 kiocb->ki_flags |= IOCB_HIPRI;
1457 kiocb->ki_complete = io_complete_rw_iopoll;
Jens Axboe6873e0b2019-10-30 13:53:09 -06001458 req->result = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001459 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001460 if (kiocb->ki_flags & IOCB_HIPRI)
1461 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07001462 kiocb->ki_complete = io_complete_rw;
1463 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001464 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001465}
1466
1467static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
1468{
1469 switch (ret) {
1470 case -EIOCBQUEUED:
1471 break;
1472 case -ERESTARTSYS:
1473 case -ERESTARTNOINTR:
1474 case -ERESTARTNOHAND:
1475 case -ERESTART_RESTARTBLOCK:
1476 /*
1477 * We can't just restart the syscall, since previously
1478 * submitted sqes may already be in progress. Just fail this
1479 * IO with EINTR.
1480 */
1481 ret = -EINTR;
1482 /* fall through */
1483 default:
1484 kiocb->ki_complete(kiocb, ret, 0);
1485 }
1486}
1487
Jens Axboeba816ad2019-09-28 11:36:45 -06001488static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt,
1489 bool in_async)
1490{
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001491 if (in_async && ret >= 0 && kiocb->ki_complete == io_complete_rw)
Jens Axboeba816ad2019-09-28 11:36:45 -06001492 *nxt = __io_complete_rw(kiocb, ret);
1493 else
1494 io_rw_done(kiocb, ret);
1495}
1496
Jens Axboeedafcce2019-01-09 09:16:05 -07001497static int io_import_fixed(struct io_ring_ctx *ctx, int rw,
1498 const struct io_uring_sqe *sqe,
1499 struct iov_iter *iter)
1500{
1501 size_t len = READ_ONCE(sqe->len);
1502 struct io_mapped_ubuf *imu;
1503 unsigned index, buf_index;
1504 size_t offset;
1505 u64 buf_addr;
1506
1507 /* attempt to use fixed buffers without having provided iovecs */
1508 if (unlikely(!ctx->user_bufs))
1509 return -EFAULT;
1510
1511 buf_index = READ_ONCE(sqe->buf_index);
1512 if (unlikely(buf_index >= ctx->nr_user_bufs))
1513 return -EFAULT;
1514
1515 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
1516 imu = &ctx->user_bufs[index];
1517 buf_addr = READ_ONCE(sqe->addr);
1518
1519 /* overflow */
1520 if (buf_addr + len < buf_addr)
1521 return -EFAULT;
1522 /* not inside the mapped region */
1523 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
1524 return -EFAULT;
1525
1526 /*
1527 * May not be a start of buffer, set size appropriately
1528 * and advance us to the beginning.
1529 */
1530 offset = buf_addr - imu->ubuf;
1531 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06001532
1533 if (offset) {
1534 /*
1535 * Don't use iov_iter_advance() here, as it's really slow for
1536 * using the latter parts of a big fixed buffer - it iterates
1537 * over each segment manually. We can cheat a bit here, because
1538 * we know that:
1539 *
1540 * 1) it's a BVEC iter, we set it up
1541 * 2) all bvecs are PAGE_SIZE in size, except potentially the
1542 * first and last bvec
1543 *
1544 * So just find our index, and adjust the iterator afterwards.
1545 * If the offset is within the first bvec (or the whole first
1546 * bvec, just use iov_iter_advance(). This makes it easier
1547 * since we can just skip the first segment, which may not
1548 * be PAGE_SIZE aligned.
1549 */
1550 const struct bio_vec *bvec = imu->bvec;
1551
1552 if (offset <= bvec->bv_len) {
1553 iov_iter_advance(iter, offset);
1554 } else {
1555 unsigned long seg_skip;
1556
1557 /* skip first vec */
1558 offset -= bvec->bv_len;
1559 seg_skip = 1 + (offset >> PAGE_SHIFT);
1560
1561 iter->bvec = bvec + seg_skip;
1562 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02001563 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001564 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001565 }
1566 }
1567
Jens Axboe5e559562019-11-13 16:12:46 -07001568 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001569}
1570
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001571static ssize_t io_import_iovec(struct io_ring_ctx *ctx, int rw,
1572 const struct sqe_submit *s, struct iovec **iovec,
1573 struct iov_iter *iter)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001574{
1575 const struct io_uring_sqe *sqe = s->sqe;
1576 void __user *buf = u64_to_user_ptr(READ_ONCE(sqe->addr));
1577 size_t sqe_len = READ_ONCE(sqe->len);
Jens Axboeedafcce2019-01-09 09:16:05 -07001578 u8 opcode;
1579
1580 /*
1581 * We're reading ->opcode for the second time, but the first read
1582 * doesn't care whether it's _FIXED or not, so it doesn't matter
1583 * whether ->opcode changes concurrently. The first read does care
1584 * about whether it is a READ or a WRITE, so we don't trust this read
1585 * for that purpose and instead let the caller pass in the read/write
1586 * flag.
1587 */
1588 opcode = READ_ONCE(sqe->opcode);
1589 if (opcode == IORING_OP_READ_FIXED ||
1590 opcode == IORING_OP_WRITE_FIXED) {
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001591 ssize_t ret = io_import_fixed(ctx, rw, sqe, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07001592 *iovec = NULL;
1593 return ret;
1594 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001595
1596 if (!s->has_user)
1597 return -EFAULT;
1598
1599#ifdef CONFIG_COMPAT
1600 if (ctx->compat)
1601 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
1602 iovec, iter);
1603#endif
1604
1605 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
1606}
1607
Jens Axboe32960612019-09-23 11:05:34 -06001608/*
1609 * For files that don't have ->read_iter() and ->write_iter(), handle them
1610 * by looping over ->read() or ->write() manually.
1611 */
1612static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
1613 struct iov_iter *iter)
1614{
1615 ssize_t ret = 0;
1616
1617 /*
1618 * Don't support polled IO through this interface, and we can't
1619 * support non-blocking either. For the latter, this just causes
1620 * the kiocb to be handled from an async context.
1621 */
1622 if (kiocb->ki_flags & IOCB_HIPRI)
1623 return -EOPNOTSUPP;
1624 if (kiocb->ki_flags & IOCB_NOWAIT)
1625 return -EAGAIN;
1626
1627 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03001628 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06001629 ssize_t nr;
1630
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03001631 if (!iov_iter_is_bvec(iter)) {
1632 iovec = iov_iter_iovec(iter);
1633 } else {
1634 /* fixed buffers import bvec */
1635 iovec.iov_base = kmap(iter->bvec->bv_page)
1636 + iter->iov_offset;
1637 iovec.iov_len = min(iter->count,
1638 iter->bvec->bv_len - iter->iov_offset);
1639 }
1640
Jens Axboe32960612019-09-23 11:05:34 -06001641 if (rw == READ) {
1642 nr = file->f_op->read(file, iovec.iov_base,
1643 iovec.iov_len, &kiocb->ki_pos);
1644 } else {
1645 nr = file->f_op->write(file, iovec.iov_base,
1646 iovec.iov_len, &kiocb->ki_pos);
1647 }
1648
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03001649 if (iov_iter_is_bvec(iter))
1650 kunmap(iter->bvec->bv_page);
1651
Jens Axboe32960612019-09-23 11:05:34 -06001652 if (nr < 0) {
1653 if (!ret)
1654 ret = nr;
1655 break;
1656 }
1657 ret += nr;
1658 if (nr != iovec.iov_len)
1659 break;
1660 iov_iter_advance(iter, nr);
1661 }
1662
1663 return ret;
1664}
1665
Pavel Begunkov267bc902019-11-07 01:41:08 +03001666static int io_read(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001667 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001668{
1669 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1670 struct kiocb *kiocb = &req->rw;
1671 struct iov_iter iter;
1672 struct file *file;
Jens Axboe31b51512019-01-18 22:56:34 -07001673 size_t iov_count;
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001674 ssize_t read_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001675
Pavel Begunkov267bc902019-11-07 01:41:08 +03001676 ret = io_prep_rw(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001677 if (ret)
1678 return ret;
1679 file = kiocb->ki_filp;
1680
Jens Axboe2b188cc2019-01-07 10:46:33 -07001681 if (unlikely(!(file->f_mode & FMODE_READ)))
Jens Axboe09bb8392019-03-13 12:39:28 -06001682 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001683
Pavel Begunkov267bc902019-11-07 01:41:08 +03001684 ret = io_import_iovec(req->ctx, READ, &req->submit, &iovec, &iter);
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001685 if (ret < 0)
Jens Axboe09bb8392019-03-13 12:39:28 -06001686 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001687
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001688 read_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06001689 if (req->flags & REQ_F_LINK)
1690 req->result = read_size;
1691
Jens Axboe31b51512019-01-18 22:56:34 -07001692 iov_count = iov_iter_count(&iter);
1693 ret = rw_verify_area(READ, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001694 if (!ret) {
1695 ssize_t ret2;
1696
Jens Axboe32960612019-09-23 11:05:34 -06001697 if (file->f_op->read_iter)
1698 ret2 = call_read_iter(file, kiocb, &iter);
1699 else
1700 ret2 = loop_rw_iter(READ, file, kiocb, &iter);
1701
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001702 /*
1703 * In case of a short read, punt to async. This can happen
1704 * if we have data partially cached. Alternatively we can
1705 * return the short read, in which case the application will
1706 * need to issue another SQE and wait for it. That SQE will
1707 * need async punt anyway, so it's more efficient to do it
1708 * here.
1709 */
Jens Axboe491381ce2019-10-17 09:20:46 -06001710 if (force_nonblock && !(req->flags & REQ_F_NOWAIT) &&
1711 (req->flags & REQ_F_ISREG) &&
1712 ret2 > 0 && ret2 < read_size)
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001713 ret2 = -EAGAIN;
1714 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboe561fb042019-10-24 07:25:42 -06001715 if (!force_nonblock || ret2 != -EAGAIN)
Pavel Begunkov267bc902019-11-07 01:41:08 +03001716 kiocb_done(kiocb, ret2, nxt, req->submit.in_async);
Jens Axboe561fb042019-10-24 07:25:42 -06001717 else
Jens Axboe2b188cc2019-01-07 10:46:33 -07001718 ret = -EAGAIN;
1719 }
1720 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001721 return ret;
1722}
1723
Pavel Begunkov267bc902019-11-07 01:41:08 +03001724static int io_write(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001725 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001726{
1727 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1728 struct kiocb *kiocb = &req->rw;
1729 struct iov_iter iter;
1730 struct file *file;
Jens Axboe31b51512019-01-18 22:56:34 -07001731 size_t iov_count;
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001732 ssize_t ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001733
Pavel Begunkov267bc902019-11-07 01:41:08 +03001734 ret = io_prep_rw(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001735 if (ret)
1736 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001737
Jens Axboe2b188cc2019-01-07 10:46:33 -07001738 file = kiocb->ki_filp;
1739 if (unlikely(!(file->f_mode & FMODE_WRITE)))
Jens Axboe09bb8392019-03-13 12:39:28 -06001740 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001741
Pavel Begunkov267bc902019-11-07 01:41:08 +03001742 ret = io_import_iovec(req->ctx, WRITE, &req->submit, &iovec, &iter);
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001743 if (ret < 0)
Jens Axboe09bb8392019-03-13 12:39:28 -06001744 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001745
Jens Axboe9e645e112019-05-10 16:07:28 -06001746 if (req->flags & REQ_F_LINK)
1747 req->result = ret;
1748
Jens Axboe31b51512019-01-18 22:56:34 -07001749 iov_count = iov_iter_count(&iter);
1750
1751 ret = -EAGAIN;
Jens Axboe561fb042019-10-24 07:25:42 -06001752 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT))
Jens Axboe31b51512019-01-18 22:56:34 -07001753 goto out_free;
Jens Axboe31b51512019-01-18 22:56:34 -07001754
1755 ret = rw_verify_area(WRITE, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001756 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01001757 ssize_t ret2;
1758
Jens Axboe2b188cc2019-01-07 10:46:33 -07001759 /*
1760 * Open-code file_start_write here to grab freeze protection,
1761 * which will be released by another thread in
1762 * io_complete_rw(). Fool lockdep by telling it the lock got
1763 * released so that it doesn't complain about the held lock when
1764 * we return to userspace.
1765 */
Jens Axboe491381ce2019-10-17 09:20:46 -06001766 if (req->flags & REQ_F_ISREG) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001767 __sb_start_write(file_inode(file)->i_sb,
1768 SB_FREEZE_WRITE, true);
1769 __sb_writers_release(file_inode(file)->i_sb,
1770 SB_FREEZE_WRITE);
1771 }
1772 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01001773
Jens Axboe32960612019-09-23 11:05:34 -06001774 if (file->f_op->write_iter)
1775 ret2 = call_write_iter(file, kiocb, &iter);
1776 else
1777 ret2 = loop_rw_iter(WRITE, file, kiocb, &iter);
Jens Axboe561fb042019-10-24 07:25:42 -06001778 if (!force_nonblock || ret2 != -EAGAIN)
Pavel Begunkov267bc902019-11-07 01:41:08 +03001779 kiocb_done(kiocb, ret2, nxt, req->submit.in_async);
Jens Axboe561fb042019-10-24 07:25:42 -06001780 else
Roman Penyaev9bf79332019-03-25 20:09:24 +01001781 ret = -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001782 }
Jens Axboe31b51512019-01-18 22:56:34 -07001783out_free:
Jens Axboe2b188cc2019-01-07 10:46:33 -07001784 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001785 return ret;
1786}
1787
1788/*
1789 * IORING_OP_NOP just posts a completion event, nothing else.
1790 */
Jens Axboe78e19bb2019-11-06 15:21:34 -07001791static int io_nop(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001792{
1793 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001794
Jens Axboedef596e2019-01-09 08:59:42 -07001795 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1796 return -EINVAL;
1797
Jens Axboe78e19bb2019-11-06 15:21:34 -07001798 io_cqring_add_event(req, 0);
Jens Axboee65ef562019-03-12 10:16:44 -06001799 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001800 return 0;
1801}
1802
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001803static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1804{
Jens Axboe6b063142019-01-10 22:13:58 -07001805 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001806
Jens Axboe09bb8392019-03-13 12:39:28 -06001807 if (!req->file)
1808 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001809
Jens Axboe6b063142019-01-10 22:13:58 -07001810 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07001811 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07001812 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001813 return -EINVAL;
1814
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001815 return 0;
1816}
1817
1818static int io_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001819 struct io_kiocb **nxt, bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001820{
1821 loff_t sqe_off = READ_ONCE(sqe->off);
1822 loff_t sqe_len = READ_ONCE(sqe->len);
1823 loff_t end = sqe_off + sqe_len;
1824 unsigned fsync_flags;
1825 int ret;
1826
1827 fsync_flags = READ_ONCE(sqe->fsync_flags);
1828 if (unlikely(fsync_flags & ~IORING_FSYNC_DATASYNC))
1829 return -EINVAL;
1830
1831 ret = io_prep_fsync(req, sqe);
1832 if (ret)
1833 return ret;
1834
1835 /* fsync always requires a blocking context */
1836 if (force_nonblock)
1837 return -EAGAIN;
1838
1839 ret = vfs_fsync_range(req->rw.ki_filp, sqe_off,
1840 end > 0 ? end : LLONG_MAX,
1841 fsync_flags & IORING_FSYNC_DATASYNC);
1842
Jens Axboe9e645e112019-05-10 16:07:28 -06001843 if (ret < 0 && (req->flags & REQ_F_LINK))
1844 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001845 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001846 io_put_req_find_next(req, nxt);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001847 return 0;
1848}
1849
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001850static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1851{
1852 struct io_ring_ctx *ctx = req->ctx;
1853 int ret = 0;
1854
1855 if (!req->file)
1856 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001857
1858 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1859 return -EINVAL;
1860 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
1861 return -EINVAL;
1862
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001863 return ret;
1864}
1865
1866static int io_sync_file_range(struct io_kiocb *req,
1867 const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001868 struct io_kiocb **nxt,
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001869 bool force_nonblock)
1870{
1871 loff_t sqe_off;
1872 loff_t sqe_len;
1873 unsigned flags;
1874 int ret;
1875
1876 ret = io_prep_sfr(req, sqe);
1877 if (ret)
1878 return ret;
1879
1880 /* sync_file_range always requires a blocking context */
1881 if (force_nonblock)
1882 return -EAGAIN;
1883
1884 sqe_off = READ_ONCE(sqe->off);
1885 sqe_len = READ_ONCE(sqe->len);
1886 flags = READ_ONCE(sqe->sync_range_flags);
1887
1888 ret = sync_file_range(req->rw.ki_filp, sqe_off, sqe_len, flags);
1889
Jens Axboe9e645e112019-05-10 16:07:28 -06001890 if (ret < 0 && (req->flags & REQ_F_LINK))
1891 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001892 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001893 io_put_req_find_next(req, nxt);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001894 return 0;
1895}
1896
Jens Axboe0fa03c62019-04-19 13:34:07 -06001897#if defined(CONFIG_NET)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001898static int io_send_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001899 struct io_kiocb **nxt, bool force_nonblock,
Jens Axboeaa1fa282019-04-19 13:38:09 -06001900 long (*fn)(struct socket *, struct user_msghdr __user *,
1901 unsigned int))
1902{
Jens Axboe0fa03c62019-04-19 13:34:07 -06001903 struct socket *sock;
1904 int ret;
1905
1906 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1907 return -EINVAL;
1908
1909 sock = sock_from_file(req->file, &ret);
1910 if (sock) {
1911 struct user_msghdr __user *msg;
1912 unsigned flags;
1913
1914 flags = READ_ONCE(sqe->msg_flags);
1915 if (flags & MSG_DONTWAIT)
1916 req->flags |= REQ_F_NOWAIT;
1917 else if (force_nonblock)
1918 flags |= MSG_DONTWAIT;
1919
1920 msg = (struct user_msghdr __user *) (unsigned long)
1921 READ_ONCE(sqe->addr);
1922
Jens Axboeaa1fa282019-04-19 13:38:09 -06001923 ret = fn(sock, msg, flags);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001924 if (force_nonblock && ret == -EAGAIN)
1925 return ret;
1926 }
1927
Jens Axboe78e19bb2019-11-06 15:21:34 -07001928 io_cqring_add_event(req, ret);
Jens Axboef1f40852019-11-05 20:33:16 -07001929 if (ret < 0 && (req->flags & REQ_F_LINK))
1930 req->flags |= REQ_F_FAIL_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001931 io_put_req_find_next(req, nxt);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001932 return 0;
Jens Axboeaa1fa282019-04-19 13:38:09 -06001933}
1934#endif
1935
1936static int io_sendmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001937 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001938{
1939#if defined(CONFIG_NET)
Jens Axboeba816ad2019-09-28 11:36:45 -06001940 return io_send_recvmsg(req, sqe, nxt, force_nonblock,
1941 __sys_sendmsg_sock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06001942#else
1943 return -EOPNOTSUPP;
1944#endif
1945}
1946
1947static int io_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001948 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001949{
1950#if defined(CONFIG_NET)
Jens Axboeba816ad2019-09-28 11:36:45 -06001951 return io_send_recvmsg(req, sqe, nxt, force_nonblock,
1952 __sys_recvmsg_sock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001953#else
1954 return -EOPNOTSUPP;
1955#endif
1956}
1957
Jens Axboe17f2fe32019-10-17 14:42:58 -06001958static int io_accept(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1959 struct io_kiocb **nxt, bool force_nonblock)
1960{
1961#if defined(CONFIG_NET)
1962 struct sockaddr __user *addr;
1963 int __user *addr_len;
1964 unsigned file_flags;
1965 int flags, ret;
1966
1967 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
1968 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05001969 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06001970 return -EINVAL;
1971
1972 addr = (struct sockaddr __user *) (unsigned long) READ_ONCE(sqe->addr);
1973 addr_len = (int __user *) (unsigned long) READ_ONCE(sqe->addr2);
1974 flags = READ_ONCE(sqe->accept_flags);
1975 file_flags = force_nonblock ? O_NONBLOCK : 0;
1976
1977 ret = __sys_accept4_file(req->file, file_flags, addr, addr_len, flags);
1978 if (ret == -EAGAIN && force_nonblock) {
1979 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
1980 return -EAGAIN;
1981 }
Jens Axboe8e3cca12019-11-09 19:52:33 -07001982 if (ret == -ERESTARTSYS)
1983 ret = -EINTR;
Jens Axboe17f2fe32019-10-17 14:42:58 -06001984 if (ret < 0 && (req->flags & REQ_F_LINK))
1985 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001986 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001987 io_put_req_find_next(req, nxt);
Jens Axboe17f2fe32019-10-17 14:42:58 -06001988 return 0;
1989#else
1990 return -EOPNOTSUPP;
1991#endif
1992}
1993
Jens Axboef8e85cf2019-11-23 14:24:24 -07001994static int io_connect(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1995 struct io_kiocb **nxt, bool force_nonblock)
1996{
1997#if defined(CONFIG_NET)
1998 struct sockaddr __user *addr;
1999 unsigned file_flags;
2000 int addr_len, ret;
2001
2002 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
2003 return -EINVAL;
2004 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
2005 return -EINVAL;
2006
2007 addr = (struct sockaddr __user *) (unsigned long) READ_ONCE(sqe->addr);
2008 addr_len = READ_ONCE(sqe->addr2);
2009 file_flags = force_nonblock ? O_NONBLOCK : 0;
2010
2011 ret = __sys_connect_file(req->file, addr, addr_len, file_flags);
2012 if (ret == -EAGAIN && force_nonblock)
2013 return -EAGAIN;
2014 if (ret == -ERESTARTSYS)
2015 ret = -EINTR;
2016 if (ret < 0 && (req->flags & REQ_F_LINK))
2017 req->flags |= REQ_F_FAIL_LINK;
2018 io_cqring_add_event(req, ret);
2019 io_put_req_find_next(req, nxt);
2020 return 0;
2021#else
2022 return -EOPNOTSUPP;
2023#endif
2024}
2025
Jens Axboeeac406c2019-11-14 12:09:58 -07002026static inline void io_poll_remove_req(struct io_kiocb *req)
2027{
2028 if (!RB_EMPTY_NODE(&req->rb_node)) {
2029 rb_erase(&req->rb_node, &req->ctx->cancel_tree);
2030 RB_CLEAR_NODE(&req->rb_node);
2031 }
2032}
2033
Jens Axboe221c5eb2019-01-17 09:41:58 -07002034static void io_poll_remove_one(struct io_kiocb *req)
2035{
2036 struct io_poll_iocb *poll = &req->poll;
2037
2038 spin_lock(&poll->head->lock);
2039 WRITE_ONCE(poll->canceled, true);
2040 if (!list_empty(&poll->wait.entry)) {
2041 list_del_init(&poll->wait.entry);
Jackie Liua197f662019-11-08 08:09:12 -07002042 io_queue_async_work(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002043 }
2044 spin_unlock(&poll->head->lock);
Jens Axboeeac406c2019-11-14 12:09:58 -07002045 io_poll_remove_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002046}
2047
2048static void io_poll_remove_all(struct io_ring_ctx *ctx)
2049{
Jens Axboeeac406c2019-11-14 12:09:58 -07002050 struct rb_node *node;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002051 struct io_kiocb *req;
2052
2053 spin_lock_irq(&ctx->completion_lock);
Jens Axboeeac406c2019-11-14 12:09:58 -07002054 while ((node = rb_first(&ctx->cancel_tree)) != NULL) {
2055 req = rb_entry(node, struct io_kiocb, rb_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002056 io_poll_remove_one(req);
2057 }
2058 spin_unlock_irq(&ctx->completion_lock);
2059}
2060
Jens Axboe47f46762019-11-09 17:43:02 -07002061static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
2062{
Jens Axboeeac406c2019-11-14 12:09:58 -07002063 struct rb_node *p, *parent = NULL;
Jens Axboe47f46762019-11-09 17:43:02 -07002064 struct io_kiocb *req;
2065
Jens Axboeeac406c2019-11-14 12:09:58 -07002066 p = ctx->cancel_tree.rb_node;
2067 while (p) {
2068 parent = p;
2069 req = rb_entry(parent, struct io_kiocb, rb_node);
2070 if (sqe_addr < req->user_data) {
2071 p = p->rb_left;
2072 } else if (sqe_addr > req->user_data) {
2073 p = p->rb_right;
2074 } else {
2075 io_poll_remove_one(req);
2076 return 0;
2077 }
Jens Axboe47f46762019-11-09 17:43:02 -07002078 }
2079
2080 return -ENOENT;
2081}
2082
Jens Axboe221c5eb2019-01-17 09:41:58 -07002083/*
2084 * Find a running poll command that matches one specified in sqe->addr,
2085 * and remove it if found.
2086 */
2087static int io_poll_remove(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2088{
2089 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07002090 int ret;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002091
2092 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2093 return -EINVAL;
2094 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
2095 sqe->poll_events)
2096 return -EINVAL;
2097
2098 spin_lock_irq(&ctx->completion_lock);
Jens Axboe47f46762019-11-09 17:43:02 -07002099 ret = io_poll_cancel(ctx, READ_ONCE(sqe->addr));
Jens Axboe221c5eb2019-01-17 09:41:58 -07002100 spin_unlock_irq(&ctx->completion_lock);
2101
Jens Axboe78e19bb2019-11-06 15:21:34 -07002102 io_cqring_add_event(req, ret);
Jens Axboef1f40852019-11-05 20:33:16 -07002103 if (ret < 0 && (req->flags & REQ_F_LINK))
2104 req->flags |= REQ_F_FAIL_LINK;
Jens Axboee65ef562019-03-12 10:16:44 -06002105 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002106 return 0;
2107}
2108
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002109static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002110{
Jackie Liua197f662019-11-08 08:09:12 -07002111 struct io_ring_ctx *ctx = req->ctx;
2112
Jens Axboe8c838782019-03-12 15:48:16 -06002113 req->poll.done = true;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002114 if (error)
2115 io_cqring_fill_event(req, error);
2116 else
2117 io_cqring_fill_event(req, mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06002118 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002119}
2120
Jens Axboe561fb042019-10-24 07:25:42 -06002121static void io_poll_complete_work(struct io_wq_work **workptr)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002122{
Jens Axboe561fb042019-10-24 07:25:42 -06002123 struct io_wq_work *work = *workptr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002124 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
2125 struct io_poll_iocb *poll = &req->poll;
2126 struct poll_table_struct pt = { ._key = poll->events };
2127 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe89723d02019-11-05 15:32:58 -07002128 struct io_kiocb *nxt = NULL;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002129 __poll_t mask = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002130 int ret = 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002131
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002132 if (work->flags & IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06002133 WRITE_ONCE(poll->canceled, true);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002134 ret = -ECANCELED;
2135 } else if (READ_ONCE(poll->canceled)) {
2136 ret = -ECANCELED;
2137 }
Jens Axboe561fb042019-10-24 07:25:42 -06002138
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002139 if (ret != -ECANCELED)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002140 mask = vfs_poll(poll->file, &pt) & poll->events;
2141
2142 /*
2143 * Note that ->ki_cancel callers also delete iocb from active_reqs after
2144 * calling ->ki_cancel. We need the ctx_lock roundtrip here to
2145 * synchronize with them. In the cancellation case the list_del_init
2146 * itself is not actually needed, but harmless so we keep it in to
2147 * avoid further branches in the fast path.
2148 */
2149 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002150 if (!mask && ret != -ECANCELED) {
Jens Axboe221c5eb2019-01-17 09:41:58 -07002151 add_wait_queue(poll->head, &poll->wait);
2152 spin_unlock_irq(&ctx->completion_lock);
2153 return;
2154 }
Jens Axboeeac406c2019-11-14 12:09:58 -07002155 io_poll_remove_req(req);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002156 io_poll_complete(req, mask, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002157 spin_unlock_irq(&ctx->completion_lock);
2158
Jens Axboe8c838782019-03-12 15:48:16 -06002159 io_cqring_ev_posted(ctx);
Jens Axboe89723d02019-11-05 15:32:58 -07002160
Jens Axboefba38c22019-11-18 12:27:57 -07002161 if (ret < 0 && req->flags & REQ_F_LINK)
2162 req->flags |= REQ_F_FAIL_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08002163 io_put_req_find_next(req, &nxt);
Jens Axboe89723d02019-11-05 15:32:58 -07002164 if (nxt)
2165 *workptr = &nxt->work;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002166}
2167
2168static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
2169 void *key)
2170{
2171 struct io_poll_iocb *poll = container_of(wait, struct io_poll_iocb,
2172 wait);
2173 struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
2174 struct io_ring_ctx *ctx = req->ctx;
2175 __poll_t mask = key_to_poll(key);
Jens Axboe8c838782019-03-12 15:48:16 -06002176 unsigned long flags;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002177
2178 /* for instances that support it check for an event match first: */
Jens Axboe8c838782019-03-12 15:48:16 -06002179 if (mask && !(mask & poll->events))
2180 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002181
2182 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06002183
Jens Axboe7c9e7f02019-11-12 08:15:53 -07002184 /*
2185 * Run completion inline if we can. We're using trylock here because
2186 * we are violating the completion_lock -> poll wq lock ordering.
2187 * If we have a link timeout we're going to need the completion_lock
2188 * for finalizing the request, mark us as having grabbed that already.
2189 */
Jens Axboe8c838782019-03-12 15:48:16 -06002190 if (mask && spin_trylock_irqsave(&ctx->completion_lock, flags)) {
Jens Axboeeac406c2019-11-14 12:09:58 -07002191 io_poll_remove_req(req);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002192 io_poll_complete(req, mask, 0);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07002193 req->flags |= REQ_F_COMP_LOCKED;
2194 io_put_req(req);
Jens Axboe8c838782019-03-12 15:48:16 -06002195 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2196
2197 io_cqring_ev_posted(ctx);
Jens Axboe8c838782019-03-12 15:48:16 -06002198 } else {
Jackie Liua197f662019-11-08 08:09:12 -07002199 io_queue_async_work(req);
Jens Axboe8c838782019-03-12 15:48:16 -06002200 }
2201
Jens Axboe221c5eb2019-01-17 09:41:58 -07002202 return 1;
2203}
2204
2205struct io_poll_table {
2206 struct poll_table_struct pt;
2207 struct io_kiocb *req;
2208 int error;
2209};
2210
2211static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
2212 struct poll_table_struct *p)
2213{
2214 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
2215
2216 if (unlikely(pt->req->poll.head)) {
2217 pt->error = -EINVAL;
2218 return;
2219 }
2220
2221 pt->error = 0;
2222 pt->req->poll.head = head;
2223 add_wait_queue(head, &pt->req->poll.wait);
2224}
2225
Jens Axboeeac406c2019-11-14 12:09:58 -07002226static void io_poll_req_insert(struct io_kiocb *req)
2227{
2228 struct io_ring_ctx *ctx = req->ctx;
2229 struct rb_node **p = &ctx->cancel_tree.rb_node;
2230 struct rb_node *parent = NULL;
2231 struct io_kiocb *tmp;
2232
2233 while (*p) {
2234 parent = *p;
2235 tmp = rb_entry(parent, struct io_kiocb, rb_node);
2236 if (req->user_data < tmp->user_data)
2237 p = &(*p)->rb_left;
2238 else
2239 p = &(*p)->rb_right;
2240 }
2241 rb_link_node(&req->rb_node, parent, p);
2242 rb_insert_color(&req->rb_node, &ctx->cancel_tree);
2243}
2244
Jens Axboe89723d02019-11-05 15:32:58 -07002245static int io_poll_add(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2246 struct io_kiocb **nxt)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002247{
2248 struct io_poll_iocb *poll = &req->poll;
2249 struct io_ring_ctx *ctx = req->ctx;
2250 struct io_poll_table ipt;
Jens Axboe8c838782019-03-12 15:48:16 -06002251 bool cancel = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002252 __poll_t mask;
2253 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002254
2255 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2256 return -EINVAL;
2257 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
2258 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06002259 if (!poll->file)
2260 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002261
Jens Axboe6cc47d12019-09-18 11:18:23 -06002262 req->submit.sqe = NULL;
Jens Axboe561fb042019-10-24 07:25:42 -06002263 INIT_IO_WORK(&req->work, io_poll_complete_work);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002264 events = READ_ONCE(sqe->poll_events);
2265 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
Jens Axboeeac406c2019-11-14 12:09:58 -07002266 RB_CLEAR_NODE(&req->rb_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002267
Jens Axboe221c5eb2019-01-17 09:41:58 -07002268 poll->head = NULL;
Jens Axboe8c838782019-03-12 15:48:16 -06002269 poll->done = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002270 poll->canceled = false;
2271
2272 ipt.pt._qproc = io_poll_queue_proc;
2273 ipt.pt._key = poll->events;
2274 ipt.req = req;
2275 ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
2276
2277 /* initialized the list so that we can do list_empty checks */
2278 INIT_LIST_HEAD(&poll->wait.entry);
2279 init_waitqueue_func_entry(&poll->wait, io_poll_wake);
2280
Jens Axboe36703242019-07-25 10:20:18 -06002281 INIT_LIST_HEAD(&req->list);
2282
Jens Axboe221c5eb2019-01-17 09:41:58 -07002283 mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002284
2285 spin_lock_irq(&ctx->completion_lock);
Jens Axboe8c838782019-03-12 15:48:16 -06002286 if (likely(poll->head)) {
2287 spin_lock(&poll->head->lock);
2288 if (unlikely(list_empty(&poll->wait.entry))) {
2289 if (ipt.error)
2290 cancel = true;
2291 ipt.error = 0;
2292 mask = 0;
2293 }
2294 if (mask || ipt.error)
2295 list_del_init(&poll->wait.entry);
2296 else if (cancel)
2297 WRITE_ONCE(poll->canceled, true);
2298 else if (!poll->done) /* actually waiting for an event */
Jens Axboeeac406c2019-11-14 12:09:58 -07002299 io_poll_req_insert(req);
Jens Axboe8c838782019-03-12 15:48:16 -06002300 spin_unlock(&poll->head->lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002301 }
Jens Axboe8c838782019-03-12 15:48:16 -06002302 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06002303 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002304 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06002305 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07002306 spin_unlock_irq(&ctx->completion_lock);
2307
Jens Axboe8c838782019-03-12 15:48:16 -06002308 if (mask) {
2309 io_cqring_ev_posted(ctx);
Jackie Liuec9c02a2019-11-08 23:50:36 +08002310 io_put_req_find_next(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002311 }
Jens Axboe8c838782019-03-12 15:48:16 -06002312 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002313}
2314
Jens Axboe5262f562019-09-17 12:26:57 -06002315static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
2316{
Jens Axboead8a48a2019-11-15 08:49:11 -07002317 struct io_timeout_data *data = container_of(timer,
2318 struct io_timeout_data, timer);
2319 struct io_kiocb *req = data->req;
2320 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06002321 unsigned long flags;
2322
Jens Axboe5262f562019-09-17 12:26:57 -06002323 atomic_inc(&ctx->cq_timeouts);
2324
2325 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08002326 /*
Jens Axboe11365042019-10-16 09:08:32 -06002327 * We could be racing with timeout deletion. If the list is empty,
2328 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08002329 */
Jens Axboe842f9612019-10-29 12:34:10 -06002330 if (!list_empty(&req->list)) {
Jens Axboe11365042019-10-16 09:08:32 -06002331 struct io_kiocb *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06002332
Jens Axboe11365042019-10-16 09:08:32 -06002333 /*
2334 * Adjust the reqs sequence before the current one because it
2335 * will consume a slot in the cq_ring and the the cq_tail
2336 * pointer will be increased, otherwise other timeout reqs may
2337 * return in advance without waiting for enough wait_nr.
2338 */
2339 prev = req;
2340 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
2341 prev->sequence++;
Jens Axboe11365042019-10-16 09:08:32 -06002342 list_del_init(&req->list);
Jens Axboe11365042019-10-16 09:08:32 -06002343 }
Jens Axboe842f9612019-10-29 12:34:10 -06002344
Jens Axboe78e19bb2019-11-06 15:21:34 -07002345 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06002346 io_commit_cqring(ctx);
2347 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2348
2349 io_cqring_ev_posted(ctx);
Jens Axboef1f40852019-11-05 20:33:16 -07002350 if (req->flags & REQ_F_LINK)
2351 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe5262f562019-09-17 12:26:57 -06002352 io_put_req(req);
2353 return HRTIMER_NORESTART;
2354}
2355
Jens Axboe47f46762019-11-09 17:43:02 -07002356static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
2357{
2358 struct io_kiocb *req;
2359 int ret = -ENOENT;
2360
2361 list_for_each_entry(req, &ctx->timeout_list, list) {
2362 if (user_data == req->user_data) {
2363 list_del_init(&req->list);
2364 ret = 0;
2365 break;
2366 }
2367 }
2368
2369 if (ret == -ENOENT)
2370 return ret;
2371
Jens Axboead8a48a2019-11-15 08:49:11 -07002372 ret = hrtimer_try_to_cancel(&req->timeout.data->timer);
Jens Axboe47f46762019-11-09 17:43:02 -07002373 if (ret == -1)
2374 return -EALREADY;
2375
Jens Axboefba38c22019-11-18 12:27:57 -07002376 if (req->flags & REQ_F_LINK)
2377 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe47f46762019-11-09 17:43:02 -07002378 io_cqring_fill_event(req, -ECANCELED);
2379 io_put_req(req);
2380 return 0;
2381}
2382
Jens Axboe11365042019-10-16 09:08:32 -06002383/*
2384 * Remove or update an existing timeout command
2385 */
2386static int io_timeout_remove(struct io_kiocb *req,
2387 const struct io_uring_sqe *sqe)
2388{
2389 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe11365042019-10-16 09:08:32 -06002390 unsigned flags;
Jens Axboe47f46762019-11-09 17:43:02 -07002391 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06002392
2393 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2394 return -EINVAL;
2395 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
2396 return -EINVAL;
2397 flags = READ_ONCE(sqe->timeout_flags);
2398 if (flags)
2399 return -EINVAL;
2400
Jens Axboe11365042019-10-16 09:08:32 -06002401 spin_lock_irq(&ctx->completion_lock);
Jens Axboe47f46762019-11-09 17:43:02 -07002402 ret = io_timeout_cancel(ctx, READ_ONCE(sqe->addr));
Jens Axboe11365042019-10-16 09:08:32 -06002403
Jens Axboe47f46762019-11-09 17:43:02 -07002404 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06002405 io_commit_cqring(ctx);
2406 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06002407 io_cqring_ev_posted(ctx);
Jens Axboe47f46762019-11-09 17:43:02 -07002408 if (ret < 0 && req->flags & REQ_F_LINK)
2409 req->flags |= REQ_F_FAIL_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08002410 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06002411 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06002412}
2413
Jens Axboead8a48a2019-11-15 08:49:11 -07002414static int io_timeout_setup(struct io_kiocb *req)
Jens Axboe5262f562019-09-17 12:26:57 -06002415{
Jens Axboead8a48a2019-11-15 08:49:11 -07002416 const struct io_uring_sqe *sqe = req->submit.sqe;
2417 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06002418 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06002419
Jens Axboead8a48a2019-11-15 08:49:11 -07002420 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06002421 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07002422 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06002423 return -EINVAL;
2424 flags = READ_ONCE(sqe->timeout_flags);
2425 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06002426 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06002427
Jens Axboead8a48a2019-11-15 08:49:11 -07002428 data = kzalloc(sizeof(struct io_timeout_data), GFP_KERNEL);
2429 if (!data)
2430 return -ENOMEM;
2431 data->req = req;
2432 req->timeout.data = data;
2433 req->flags |= REQ_F_TIMEOUT;
2434
2435 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06002436 return -EFAULT;
2437
Jens Axboe11365042019-10-16 09:08:32 -06002438 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07002439 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06002440 else
Jens Axboead8a48a2019-11-15 08:49:11 -07002441 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06002442
Jens Axboead8a48a2019-11-15 08:49:11 -07002443 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
2444 return 0;
2445}
2446
2447static int io_timeout(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2448{
2449 unsigned count;
2450 struct io_ring_ctx *ctx = req->ctx;
2451 struct io_timeout_data *data;
2452 struct list_head *entry;
2453 unsigned span = 0;
2454 int ret;
2455
2456 ret = io_timeout_setup(req);
2457 /* common setup allows flags (like links) set, we don't */
2458 if (!ret && sqe->flags)
2459 ret = -EINVAL;
2460 if (ret)
2461 return ret;
Jens Axboe93bd25b2019-11-11 23:34:31 -07002462
Jens Axboe5262f562019-09-17 12:26:57 -06002463 /*
2464 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07002465 * timeout event to be satisfied. If it isn't set, then this is
2466 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06002467 */
2468 count = READ_ONCE(sqe->off);
Jens Axboe93bd25b2019-11-11 23:34:31 -07002469 if (!count) {
2470 req->flags |= REQ_F_TIMEOUT_NOSEQ;
2471 spin_lock_irq(&ctx->completion_lock);
2472 entry = ctx->timeout_list.prev;
2473 goto add;
2474 }
Jens Axboe5262f562019-09-17 12:26:57 -06002475
2476 req->sequence = ctx->cached_sq_head + count - 1;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +03002477 req->timeout.data->seq_offset = count;
Jens Axboe5262f562019-09-17 12:26:57 -06002478
2479 /*
2480 * Insertion sort, ensuring the first entry in the list is always
2481 * the one we need first.
2482 */
Jens Axboe5262f562019-09-17 12:26:57 -06002483 spin_lock_irq(&ctx->completion_lock);
2484 list_for_each_prev(entry, &ctx->timeout_list) {
2485 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
yangerkun5da0fb12019-10-15 21:59:29 +08002486 unsigned nxt_sq_head;
2487 long long tmp, tmp_nxt;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +03002488 u32 nxt_offset = nxt->timeout.data->seq_offset;
Jens Axboe5262f562019-09-17 12:26:57 -06002489
Jens Axboe93bd25b2019-11-11 23:34:31 -07002490 if (nxt->flags & REQ_F_TIMEOUT_NOSEQ)
2491 continue;
2492
yangerkun5da0fb12019-10-15 21:59:29 +08002493 /*
2494 * Since cached_sq_head + count - 1 can overflow, use type long
2495 * long to store it.
2496 */
2497 tmp = (long long)ctx->cached_sq_head + count - 1;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +03002498 nxt_sq_head = nxt->sequence - nxt_offset + 1;
2499 tmp_nxt = (long long)nxt_sq_head + nxt_offset - 1;
yangerkun5da0fb12019-10-15 21:59:29 +08002500
2501 /*
2502 * cached_sq_head may overflow, and it will never overflow twice
2503 * once there is some timeout req still be valid.
2504 */
2505 if (ctx->cached_sq_head < nxt_sq_head)
yangerkun8b07a652019-10-17 12:12:35 +08002506 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08002507
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002508 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06002509 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002510
2511 /*
2512 * Sequence of reqs after the insert one and itself should
2513 * be adjusted because each timeout req consumes a slot.
2514 */
2515 span++;
2516 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06002517 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002518 req->sequence -= span;
Jens Axboe93bd25b2019-11-11 23:34:31 -07002519add:
Jens Axboe5262f562019-09-17 12:26:57 -06002520 list_add(&req->list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07002521 data = req->timeout.data;
2522 data->timer.function = io_timeout_fn;
2523 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06002524 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06002525 return 0;
2526}
2527
Jens Axboe62755e32019-10-28 21:49:21 -06002528static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06002529{
Jens Axboe62755e32019-10-28 21:49:21 -06002530 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06002531
Jens Axboe62755e32019-10-28 21:49:21 -06002532 return req->user_data == (unsigned long) data;
2533}
2534
Jens Axboee977d6d2019-11-05 12:39:45 -07002535static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06002536{
Jens Axboe62755e32019-10-28 21:49:21 -06002537 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06002538 int ret = 0;
2539
Jens Axboe62755e32019-10-28 21:49:21 -06002540 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
2541 switch (cancel_ret) {
2542 case IO_WQ_CANCEL_OK:
2543 ret = 0;
2544 break;
2545 case IO_WQ_CANCEL_RUNNING:
2546 ret = -EALREADY;
2547 break;
2548 case IO_WQ_CANCEL_NOTFOUND:
2549 ret = -ENOENT;
2550 break;
2551 }
2552
Jens Axboee977d6d2019-11-05 12:39:45 -07002553 return ret;
2554}
2555
Jens Axboe47f46762019-11-09 17:43:02 -07002556static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
2557 struct io_kiocb *req, __u64 sqe_addr,
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002558 struct io_kiocb **nxt, int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07002559{
2560 unsigned long flags;
2561 int ret;
2562
2563 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
2564 if (ret != -ENOENT) {
2565 spin_lock_irqsave(&ctx->completion_lock, flags);
2566 goto done;
2567 }
2568
2569 spin_lock_irqsave(&ctx->completion_lock, flags);
2570 ret = io_timeout_cancel(ctx, sqe_addr);
2571 if (ret != -ENOENT)
2572 goto done;
2573 ret = io_poll_cancel(ctx, sqe_addr);
2574done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002575 if (!ret)
2576 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07002577 io_cqring_fill_event(req, ret);
2578 io_commit_cqring(ctx);
2579 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2580 io_cqring_ev_posted(ctx);
2581
2582 if (ret < 0 && (req->flags & REQ_F_LINK))
2583 req->flags |= REQ_F_FAIL_LINK;
2584 io_put_req_find_next(req, nxt);
2585}
2586
Jens Axboee977d6d2019-11-05 12:39:45 -07002587static int io_async_cancel(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2588 struct io_kiocb **nxt)
2589{
2590 struct io_ring_ctx *ctx = req->ctx;
Jens Axboee977d6d2019-11-05 12:39:45 -07002591
2592 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2593 return -EINVAL;
2594 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
2595 sqe->cancel_flags)
2596 return -EINVAL;
2597
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002598 io_async_find_and_cancel(ctx, req, READ_ONCE(sqe->addr), nxt, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06002599 return 0;
2600}
2601
Jackie Liua197f662019-11-08 08:09:12 -07002602static int io_req_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -06002603{
Pavel Begunkov267bc902019-11-07 01:41:08 +03002604 const struct io_uring_sqe *sqe = req->submit.sqe;
Jens Axboede0617e2019-04-06 21:51:27 -06002605 struct io_uring_sqe *sqe_copy;
Jackie Liua197f662019-11-08 08:09:12 -07002606 struct io_ring_ctx *ctx = req->ctx;
Jens Axboede0617e2019-04-06 21:51:27 -06002607
Bob Liu9d858b22019-11-13 18:06:25 +08002608 /* Still need defer if there is pending req in defer list. */
2609 if (!req_need_defer(req) && list_empty(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06002610 return 0;
2611
2612 sqe_copy = kmalloc(sizeof(*sqe_copy), GFP_KERNEL);
2613 if (!sqe_copy)
2614 return -EAGAIN;
2615
2616 spin_lock_irq(&ctx->completion_lock);
Bob Liu9d858b22019-11-13 18:06:25 +08002617 if (!req_need_defer(req) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06002618 spin_unlock_irq(&ctx->completion_lock);
2619 kfree(sqe_copy);
2620 return 0;
2621 }
2622
2623 memcpy(sqe_copy, sqe, sizeof(*sqe_copy));
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03002624 req->flags |= REQ_F_FREE_SQE;
Jens Axboede0617e2019-04-06 21:51:27 -06002625 req->submit.sqe = sqe_copy;
2626
Jens Axboe915967f2019-11-21 09:01:20 -07002627 trace_io_uring_defer(ctx, req, req->user_data);
Jens Axboede0617e2019-04-06 21:51:27 -06002628 list_add_tail(&req->list, &ctx->defer_list);
2629 spin_unlock_irq(&ctx->completion_lock);
2630 return -EIOCBQUEUED;
2631}
2632
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03002633__attribute__((nonnull))
Pavel Begunkovd7324472019-11-21 21:24:36 +03002634static int io_issue_sqe(struct io_kiocb *req, struct io_kiocb **nxt,
2635 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002636{
Jens Axboee0c5c572019-03-12 10:18:47 -06002637 int ret, opcode;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002638 struct sqe_submit *s = &req->submit;
Jackie Liua197f662019-11-08 08:09:12 -07002639 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002640
2641 opcode = READ_ONCE(s->sqe->opcode);
2642 switch (opcode) {
2643 case IORING_OP_NOP:
Jens Axboe78e19bb2019-11-06 15:21:34 -07002644 ret = io_nop(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002645 break;
2646 case IORING_OP_READV:
Jens Axboeedafcce2019-01-09 09:16:05 -07002647 if (unlikely(s->sqe->buf_index))
2648 return -EINVAL;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002649 ret = io_read(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002650 break;
2651 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07002652 if (unlikely(s->sqe->buf_index))
2653 return -EINVAL;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002654 ret = io_write(req, nxt, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07002655 break;
2656 case IORING_OP_READ_FIXED:
Pavel Begunkov267bc902019-11-07 01:41:08 +03002657 ret = io_read(req, nxt, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07002658 break;
2659 case IORING_OP_WRITE_FIXED:
Pavel Begunkov267bc902019-11-07 01:41:08 +03002660 ret = io_write(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002661 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002662 case IORING_OP_FSYNC:
Jens Axboeba816ad2019-09-28 11:36:45 -06002663 ret = io_fsync(req, s->sqe, nxt, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002664 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002665 case IORING_OP_POLL_ADD:
Jens Axboe89723d02019-11-05 15:32:58 -07002666 ret = io_poll_add(req, s->sqe, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002667 break;
2668 case IORING_OP_POLL_REMOVE:
2669 ret = io_poll_remove(req, s->sqe);
2670 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002671 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboeba816ad2019-09-28 11:36:45 -06002672 ret = io_sync_file_range(req, s->sqe, nxt, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002673 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002674 case IORING_OP_SENDMSG:
Jens Axboeba816ad2019-09-28 11:36:45 -06002675 ret = io_sendmsg(req, s->sqe, nxt, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06002676 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06002677 case IORING_OP_RECVMSG:
Jens Axboeba816ad2019-09-28 11:36:45 -06002678 ret = io_recvmsg(req, s->sqe, nxt, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06002679 break;
Jens Axboe5262f562019-09-17 12:26:57 -06002680 case IORING_OP_TIMEOUT:
2681 ret = io_timeout(req, s->sqe);
2682 break;
Jens Axboe11365042019-10-16 09:08:32 -06002683 case IORING_OP_TIMEOUT_REMOVE:
2684 ret = io_timeout_remove(req, s->sqe);
2685 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06002686 case IORING_OP_ACCEPT:
2687 ret = io_accept(req, s->sqe, nxt, force_nonblock);
2688 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07002689 case IORING_OP_CONNECT:
2690 ret = io_connect(req, s->sqe, nxt, force_nonblock);
2691 break;
Jens Axboe62755e32019-10-28 21:49:21 -06002692 case IORING_OP_ASYNC_CANCEL:
2693 ret = io_async_cancel(req, s->sqe, nxt);
2694 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002695 default:
2696 ret = -EINVAL;
2697 break;
2698 }
2699
Jens Axboedef596e2019-01-09 08:59:42 -07002700 if (ret)
2701 return ret;
2702
2703 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe9e645e112019-05-10 16:07:28 -06002704 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07002705 return -EAGAIN;
2706
2707 /* workqueue context doesn't hold uring_lock, grab it now */
Jackie Liuba5290c2019-10-09 09:19:59 +08002708 if (s->in_async)
Jens Axboedef596e2019-01-09 08:59:42 -07002709 mutex_lock(&ctx->uring_lock);
2710 io_iopoll_req_issued(req);
Jackie Liuba5290c2019-10-09 09:19:59 +08002711 if (s->in_async)
Jens Axboedef596e2019-01-09 08:59:42 -07002712 mutex_unlock(&ctx->uring_lock);
2713 }
2714
2715 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002716}
2717
Jens Axboeb76da702019-11-20 13:05:32 -07002718static void io_link_work_cb(struct io_wq_work **workptr)
2719{
2720 struct io_wq_work *work = *workptr;
2721 struct io_kiocb *link = work->data;
2722
2723 io_queue_linked_timeout(link);
2724 work->func = io_wq_submit_work;
2725}
2726
Jens Axboe561fb042019-10-24 07:25:42 -06002727static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboe31b51512019-01-18 22:56:34 -07002728{
Jens Axboe561fb042019-10-24 07:25:42 -06002729 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002730 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe561fb042019-10-24 07:25:42 -06002731 struct sqe_submit *s = &req->submit;
Jens Axboe561fb042019-10-24 07:25:42 -06002732 struct io_kiocb *nxt = NULL;
2733 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002734
Jens Axboe561fb042019-10-24 07:25:42 -06002735 /* Ensure we clear previously set non-block flag */
2736 req->rw.ki_flags &= ~IOCB_NOWAIT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002737
Jens Axboe561fb042019-10-24 07:25:42 -06002738 if (work->flags & IO_WQ_WORK_CANCEL)
2739 ret = -ECANCELED;
Jens Axboe31b51512019-01-18 22:56:34 -07002740
Jens Axboe561fb042019-10-24 07:25:42 -06002741 if (!ret) {
2742 s->has_user = (work->flags & IO_WQ_WORK_HAS_MM) != 0;
2743 s->in_async = true;
2744 do {
Pavel Begunkovd7324472019-11-21 21:24:36 +03002745 ret = io_issue_sqe(req, &nxt, false);
Jens Axboe561fb042019-10-24 07:25:42 -06002746 /*
2747 * We can get EAGAIN for polled IO even though we're
2748 * forcing a sync submission from here, since we can't
2749 * wait for request slots on the block side.
2750 */
2751 if (ret != -EAGAIN)
2752 break;
2753 cond_resched();
2754 } while (1);
2755 }
Jens Axboe31b51512019-01-18 22:56:34 -07002756
Jens Axboe561fb042019-10-24 07:25:42 -06002757 /* drop submission reference */
Jackie Liuec9c02a2019-11-08 23:50:36 +08002758 io_put_req(req);
Jens Axboe817869d2019-04-30 14:44:05 -06002759
Jens Axboe561fb042019-10-24 07:25:42 -06002760 if (ret) {
Jens Axboef1f40852019-11-05 20:33:16 -07002761 if (req->flags & REQ_F_LINK)
2762 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07002763 io_cqring_add_event(req, ret);
Jens Axboe817869d2019-04-30 14:44:05 -06002764 io_put_req(req);
Jens Axboeedafcce2019-01-09 09:16:05 -07002765 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002766
Jens Axboe561fb042019-10-24 07:25:42 -06002767 /* if a dependent link is ready, pass it back */
2768 if (!ret && nxt) {
Jens Axboe94ae5e72019-11-14 19:39:52 -07002769 struct io_kiocb *link;
2770
2771 io_prep_async_work(nxt, &link);
Jens Axboe561fb042019-10-24 07:25:42 -06002772 *workptr = &nxt->work;
Jens Axboeb76da702019-11-20 13:05:32 -07002773 if (link) {
2774 nxt->work.flags |= IO_WQ_WORK_CB;
2775 nxt->work.func = io_link_work_cb;
2776 nxt->work.data = link;
2777 }
Jens Axboeedafcce2019-01-09 09:16:05 -07002778 }
Jens Axboe31b51512019-01-18 22:56:34 -07002779}
Jens Axboe2b188cc2019-01-07 10:46:33 -07002780
Jens Axboe09bb8392019-03-13 12:39:28 -06002781static bool io_op_needs_file(const struct io_uring_sqe *sqe)
2782{
2783 int op = READ_ONCE(sqe->opcode);
2784
2785 switch (op) {
2786 case IORING_OP_NOP:
2787 case IORING_OP_POLL_REMOVE:
Pavel Begunkov5683e542019-11-14 00:59:19 +03002788 case IORING_OP_TIMEOUT:
Pavel Begunkova320e9f2019-11-14 00:11:01 +03002789 case IORING_OP_TIMEOUT_REMOVE:
2790 case IORING_OP_ASYNC_CANCEL:
2791 case IORING_OP_LINK_TIMEOUT:
Jens Axboe09bb8392019-03-13 12:39:28 -06002792 return false;
2793 default:
2794 return true;
2795 }
2796}
2797
Jens Axboe65e19f52019-10-26 07:20:21 -06002798static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
2799 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06002800{
Jens Axboe65e19f52019-10-26 07:20:21 -06002801 struct fixed_file_table *table;
2802
2803 table = &ctx->file_table[index >> IORING_FILE_TABLE_SHIFT];
2804 return table->files[index & IORING_FILE_TABLE_MASK];
2805}
2806
Jackie Liua197f662019-11-08 08:09:12 -07002807static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req)
Jens Axboe09bb8392019-03-13 12:39:28 -06002808{
Pavel Begunkov267bc902019-11-07 01:41:08 +03002809 struct sqe_submit *s = &req->submit;
Jackie Liua197f662019-11-08 08:09:12 -07002810 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe09bb8392019-03-13 12:39:28 -06002811 unsigned flags;
2812 int fd;
2813
2814 flags = READ_ONCE(s->sqe->flags);
2815 fd = READ_ONCE(s->sqe->fd);
2816
Jackie Liu4fe2c962019-09-09 20:50:40 +08002817 if (flags & IOSQE_IO_DRAIN)
Jens Axboede0617e2019-04-06 21:51:27 -06002818 req->flags |= REQ_F_IO_DRAIN;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002819 /*
2820 * All io need record the previous position, if LINK vs DARIN,
2821 * it can be used to mark the position of the first IO in the
2822 * link list.
2823 */
2824 req->sequence = s->sequence;
Jens Axboede0617e2019-04-06 21:51:27 -06002825
Jens Axboe60c112b2019-06-21 10:20:18 -06002826 if (!io_op_needs_file(s->sqe))
Jens Axboe09bb8392019-03-13 12:39:28 -06002827 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06002828
2829 if (flags & IOSQE_FIXED_FILE) {
Jens Axboe65e19f52019-10-26 07:20:21 -06002830 if (unlikely(!ctx->file_table ||
Jens Axboe09bb8392019-03-13 12:39:28 -06002831 (unsigned) fd >= ctx->nr_user_files))
2832 return -EBADF;
Jens Axboeb7620122019-10-26 07:22:55 -06002833 fd = array_index_nospec(fd, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06002834 req->file = io_file_from_index(ctx, fd);
2835 if (!req->file)
Jens Axboe08a45172019-10-03 08:11:03 -06002836 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06002837 req->flags |= REQ_F_FIXED_FILE;
2838 } else {
2839 if (s->needs_fixed_file)
2840 return -EBADF;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002841 trace_io_uring_file_get(ctx, fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06002842 req->file = io_file_get(state, fd);
2843 if (unlikely(!req->file))
2844 return -EBADF;
2845 }
2846
2847 return 0;
2848}
2849
Jackie Liua197f662019-11-08 08:09:12 -07002850static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002851{
Jens Axboefcb323c2019-10-24 12:39:47 -06002852 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07002853 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06002854
2855 rcu_read_lock();
2856 spin_lock_irq(&ctx->inflight_lock);
2857 /*
2858 * We use the f_ops->flush() handler to ensure that we can flush
2859 * out work accessing these files if the fd is closed. Check if
2860 * the fd has changed since we started down this path, and disallow
2861 * this operation if it has.
2862 */
2863 if (fcheck(req->submit.ring_fd) == req->submit.ring_file) {
2864 list_add(&req->inflight_entry, &ctx->inflight_list);
2865 req->flags |= REQ_F_INFLIGHT;
2866 req->work.files = current->files;
2867 ret = 0;
2868 }
2869 spin_unlock_irq(&ctx->inflight_lock);
2870 rcu_read_unlock();
2871
2872 return ret;
2873}
2874
Jens Axboe2665abf2019-11-05 12:40:47 -07002875static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
2876{
Jens Axboead8a48a2019-11-15 08:49:11 -07002877 struct io_timeout_data *data = container_of(timer,
2878 struct io_timeout_data, timer);
2879 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07002880 struct io_ring_ctx *ctx = req->ctx;
2881 struct io_kiocb *prev = NULL;
2882 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07002883
2884 spin_lock_irqsave(&ctx->completion_lock, flags);
2885
2886 /*
2887 * We don't expect the list to be empty, that will only happen if we
2888 * race with the completion of the linked work.
2889 */
2890 if (!list_empty(&req->list)) {
2891 prev = list_entry(req->list.prev, struct io_kiocb, link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07002892 if (refcount_inc_not_zero(&prev->refs)) {
Jens Axboe76a46e02019-11-10 23:34:16 -07002893 list_del_init(&req->list);
Jens Axboe5d960722019-11-19 15:31:28 -07002894 prev->flags &= ~REQ_F_LINK_TIMEOUT;
2895 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07002896 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07002897 }
2898
2899 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2900
2901 if (prev) {
Jens Axboefba38c22019-11-18 12:27:57 -07002902 if (prev->flags & REQ_F_LINK)
2903 prev->flags |= REQ_F_FAIL_LINK;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002904 io_async_find_and_cancel(ctx, req, prev->user_data, NULL,
2905 -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07002906 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07002907 } else {
2908 io_cqring_add_event(req, -ETIME);
2909 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07002910 }
Jens Axboe2665abf2019-11-05 12:40:47 -07002911 return HRTIMER_NORESTART;
2912}
2913
Jens Axboead8a48a2019-11-15 08:49:11 -07002914static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07002915{
Jens Axboe76a46e02019-11-10 23:34:16 -07002916 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07002917
Jens Axboe76a46e02019-11-10 23:34:16 -07002918 /*
2919 * If the list is now empty, then our linked request finished before
2920 * we got a chance to setup the timer
2921 */
2922 spin_lock_irq(&ctx->completion_lock);
2923 if (!list_empty(&req->list)) {
Jens Axboe94ae5e72019-11-14 19:39:52 -07002924 struct io_timeout_data *data = req->timeout.data;
2925
Jens Axboead8a48a2019-11-15 08:49:11 -07002926 data->timer.function = io_link_timeout_fn;
2927 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
2928 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07002929 }
Jens Axboe76a46e02019-11-10 23:34:16 -07002930 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07002931
Jens Axboe2665abf2019-11-05 12:40:47 -07002932 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07002933 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07002934}
2935
Jens Axboead8a48a2019-11-15 08:49:11 -07002936static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07002937{
2938 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002939
Jens Axboe2665abf2019-11-05 12:40:47 -07002940 if (!(req->flags & REQ_F_LINK))
2941 return NULL;
2942
2943 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, list);
Jens Axboe76a46e02019-11-10 23:34:16 -07002944 if (!nxt || nxt->submit.sqe->opcode != IORING_OP_LINK_TIMEOUT)
2945 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07002946
Jens Axboe76a46e02019-11-10 23:34:16 -07002947 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07002948 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07002949}
2950
Jens Axboe0e0702d2019-11-14 21:42:10 -07002951static void __io_queue_sqe(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002952{
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03002953 struct io_kiocb *linked_timeout = io_prep_linked_timeout(req);
2954 struct io_kiocb *nxt = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002955 int ret;
2956
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03002957 ret = io_issue_sqe(req, &nxt, true);
2958 if (nxt)
2959 io_queue_async_work(nxt);
Jens Axboe491381ce2019-10-17 09:20:46 -06002960
2961 /*
2962 * We async punt it if the file wasn't marked NOWAIT, or if the file
2963 * doesn't support non-blocking read/write attempts
2964 */
2965 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
2966 (req->flags & REQ_F_MUST_PUNT))) {
Pavel Begunkov267bc902019-11-07 01:41:08 +03002967 struct sqe_submit *s = &req->submit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002968 struct io_uring_sqe *sqe_copy;
2969
Jackie Liu954dab12019-09-18 10:37:52 +08002970 sqe_copy = kmemdup(s->sqe, sizeof(*sqe_copy), GFP_KERNEL);
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03002971 if (!sqe_copy)
2972 goto err;
Jens Axboee65ef562019-03-12 10:16:44 -06002973
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03002974 s->sqe = sqe_copy;
2975 req->flags |= REQ_F_FREE_SQE;
2976
2977 if (req->work.flags & IO_WQ_WORK_NEEDS_FILES) {
2978 ret = io_grab_files(req);
2979 if (ret)
2980 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002981 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03002982
2983 /*
2984 * Queued up for async execution, worker will release
2985 * submit reference when the iocb is actually submitted.
2986 */
2987 io_queue_async_work(req);
2988 return;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002989 }
Jens Axboee65ef562019-03-12 10:16:44 -06002990
Jens Axboefcb323c2019-10-24 12:39:47 -06002991err:
Jens Axboee65ef562019-03-12 10:16:44 -06002992 /* drop submission reference */
2993 io_put_req(req);
2994
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03002995 if (linked_timeout) {
Jens Axboe76a46e02019-11-10 23:34:16 -07002996 if (!ret)
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03002997 io_queue_linked_timeout(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07002998 else
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03002999 io_put_req(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07003000 }
3001
Jens Axboee65ef562019-03-12 10:16:44 -06003002 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06003003 if (ret) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07003004 io_cqring_add_event(req, ret);
Jens Axboe9e645e112019-05-10 16:07:28 -06003005 if (req->flags & REQ_F_LINK)
3006 req->flags |= REQ_F_FAIL_LINK;
Jens Axboee65ef562019-03-12 10:16:44 -06003007 io_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06003008 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003009}
3010
Jens Axboe0e0702d2019-11-14 21:42:10 -07003011static void io_queue_sqe(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08003012{
3013 int ret;
3014
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003015 if (unlikely(req->ctx->drain_next)) {
3016 req->flags |= REQ_F_IO_DRAIN;
3017 req->ctx->drain_next = false;
3018 }
3019 req->ctx->drain_next = (req->flags & REQ_F_DRAIN_LINK);
3020
Jackie Liua197f662019-11-08 08:09:12 -07003021 ret = io_req_defer(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08003022 if (ret) {
3023 if (ret != -EIOCBQUEUED) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07003024 io_cqring_add_event(req, ret);
Pavel Begunkovd3b357962019-11-19 23:32:48 +03003025 if (req->flags & REQ_F_LINK)
3026 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07003027 io_double_put_req(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08003028 }
Jens Axboe0e0702d2019-11-14 21:42:10 -07003029 } else
3030 __io_queue_sqe(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08003031}
3032
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003033static inline void io_queue_link_head(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08003034{
Jens Axboe94ae5e72019-11-14 19:39:52 -07003035 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003036 io_cqring_add_event(req, -ECANCELED);
3037 io_double_put_req(req);
3038 } else
Jens Axboe0e0702d2019-11-14 21:42:10 -07003039 io_queue_sqe(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08003040}
3041
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003042
Jens Axboe9e645e112019-05-10 16:07:28 -06003043#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK)
3044
Jackie Liua197f662019-11-08 08:09:12 -07003045static void io_submit_sqe(struct io_kiocb *req, struct io_submit_state *state,
3046 struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06003047{
Pavel Begunkov267bc902019-11-07 01:41:08 +03003048 struct sqe_submit *s = &req->submit;
Jackie Liua197f662019-11-08 08:09:12 -07003049 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -06003050 int ret;
3051
Jens Axboe78e19bb2019-11-06 15:21:34 -07003052 req->user_data = s->sqe->user_data;
3053
Jens Axboe9e645e112019-05-10 16:07:28 -06003054 /* enforce forwards compatibility on users */
3055 if (unlikely(s->sqe->flags & ~SQE_VALID_FLAGS)) {
3056 ret = -EINVAL;
Pavel Begunkov196be952019-11-07 01:41:06 +03003057 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06003058 }
3059
Jackie Liua197f662019-11-08 08:09:12 -07003060 ret = io_req_set_file(state, req);
Jens Axboe9e645e112019-05-10 16:07:28 -06003061 if (unlikely(ret)) {
3062err_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07003063 io_cqring_add_event(req, ret);
3064 io_double_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06003065 return;
3066 }
3067
Jens Axboe9e645e112019-05-10 16:07:28 -06003068 /*
3069 * If we already have a head request, queue this one for async
3070 * submittal once the head completes. If we don't have a head but
3071 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
3072 * submitted sync once the chain is complete. If none of those
3073 * conditions are true (normal request), then just queue it.
3074 */
3075 if (*link) {
3076 struct io_kiocb *prev = *link;
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03003077 struct io_uring_sqe *sqe_copy;
Jens Axboe9e645e112019-05-10 16:07:28 -06003078
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003079 if (s->sqe->flags & IOSQE_IO_DRAIN)
3080 (*link)->flags |= REQ_F_DRAIN_LINK | REQ_F_IO_DRAIN;
3081
Jens Axboe94ae5e72019-11-14 19:39:52 -07003082 if (READ_ONCE(s->sqe->opcode) == IORING_OP_LINK_TIMEOUT) {
3083 ret = io_timeout_setup(req);
3084 /* common setup allows offset being set, we don't */
3085 if (!ret && s->sqe->off)
3086 ret = -EINVAL;
3087 if (ret) {
3088 prev->flags |= REQ_F_FAIL_LINK;
3089 goto err_req;
3090 }
3091 }
3092
Jens Axboe9e645e112019-05-10 16:07:28 -06003093 sqe_copy = kmemdup(s->sqe, sizeof(*sqe_copy), GFP_KERNEL);
3094 if (!sqe_copy) {
3095 ret = -EAGAIN;
3096 goto err_req;
3097 }
3098
3099 s->sqe = sqe_copy;
Jens Axboe94ae5e72019-11-14 19:39:52 -07003100 req->flags |= REQ_F_FREE_SQE;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02003101 trace_io_uring_link(ctx, req, prev);
Jens Axboe9e645e112019-05-10 16:07:28 -06003102 list_add_tail(&req->list, &prev->link_list);
3103 } else if (s->sqe->flags & IOSQE_IO_LINK) {
3104 req->flags |= REQ_F_LINK;
3105
Jens Axboe9e645e112019-05-10 16:07:28 -06003106 INIT_LIST_HEAD(&req->link_list);
3107 *link = req;
3108 } else {
Jackie Liua197f662019-11-08 08:09:12 -07003109 io_queue_sqe(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06003110 }
3111}
3112
Jens Axboe9a56a232019-01-09 09:06:50 -07003113/*
3114 * Batched submission is done, ensure local IO is flushed out.
3115 */
3116static void io_submit_state_end(struct io_submit_state *state)
3117{
3118 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06003119 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07003120 if (state->free_reqs)
3121 kmem_cache_free_bulk(req_cachep, state->free_reqs,
3122 &state->reqs[state->cur_req]);
Jens Axboe9a56a232019-01-09 09:06:50 -07003123}
3124
3125/*
3126 * Start submission side cache.
3127 */
3128static void io_submit_state_start(struct io_submit_state *state,
3129 struct io_ring_ctx *ctx, unsigned max_ios)
3130{
3131 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07003132 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07003133 state->file = NULL;
3134 state->ios_left = max_ios;
3135}
3136
Jens Axboe2b188cc2019-01-07 10:46:33 -07003137static void io_commit_sqring(struct io_ring_ctx *ctx)
3138{
Hristo Venev75b28af2019-08-26 17:23:46 +00003139 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003140
Hristo Venev75b28af2019-08-26 17:23:46 +00003141 if (ctx->cached_sq_head != READ_ONCE(rings->sq.head)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07003142 /*
3143 * Ensure any loads from the SQEs are done at this point,
3144 * since once we write the new head, the application could
3145 * write new data to them.
3146 */
Hristo Venev75b28af2019-08-26 17:23:46 +00003147 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003148 }
3149}
3150
3151/*
Jens Axboe2b188cc2019-01-07 10:46:33 -07003152 * Fetch an sqe, if one is available. Note that s->sqe will point to memory
3153 * that is mapped by userspace. This means that care needs to be taken to
3154 * ensure that reads are stable, as we cannot rely on userspace always
3155 * being a good citizen. If members of the sqe are validated and then later
3156 * used, it's important that those reads are done through READ_ONCE() to
3157 * prevent a re-load down the line.
3158 */
3159static bool io_get_sqring(struct io_ring_ctx *ctx, struct sqe_submit *s)
3160{
Hristo Venev75b28af2019-08-26 17:23:46 +00003161 struct io_rings *rings = ctx->rings;
3162 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003163 unsigned head;
3164
3165 /*
3166 * The cached sq head (or cq tail) serves two purposes:
3167 *
3168 * 1) allows us to batch the cost of updating the user visible
3169 * head updates.
3170 * 2) allows the kernel side to track the head on its own, even
3171 * though the application is the one updating it.
3172 */
3173 head = ctx->cached_sq_head;
Stefan Bühlere523a292019-04-19 11:57:44 +02003174 /* make sure SQ entry isn't read before tail */
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03003175 if (unlikely(head == smp_load_acquire(&rings->sq.tail)))
Jens Axboe2b188cc2019-01-07 10:46:33 -07003176 return false;
3177
Hristo Venev75b28af2019-08-26 17:23:46 +00003178 head = READ_ONCE(sq_array[head & ctx->sq_mask]);
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03003179 if (likely(head < ctx->sq_entries)) {
Jens Axboefcb323c2019-10-24 12:39:47 -06003180 s->ring_file = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003181 s->sqe = &ctx->sq_sqes[head];
Jackie Liu8776f3f2019-09-09 20:50:39 +08003182 s->sequence = ctx->cached_sq_head;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003183 ctx->cached_sq_head++;
3184 return true;
3185 }
3186
3187 /* drop invalid entries */
3188 ctx->cached_sq_head++;
Jens Axboe498ccd92019-10-25 10:04:25 -06003189 ctx->cached_sq_dropped++;
3190 WRITE_ONCE(rings->sq_dropped, ctx->cached_sq_dropped);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003191 return false;
3192}
3193
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003194static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkovae9428c2019-11-06 00:22:14 +03003195 struct file *ring_file, int ring_fd,
3196 struct mm_struct **mm, bool async)
Jens Axboe6c271ce2019-01-10 11:22:30 -07003197{
3198 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06003199 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06003200 int i, submitted = 0;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03003201 bool mm_fault = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003202
Jens Axboec4a2ed72019-11-21 21:01:26 -07003203 /* if we have a backlog and couldn't flush it all, return BUSY */
3204 if (!list_empty(&ctx->cq_overflow_list) &&
3205 !io_cqring_overflow_flush(ctx, false))
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003206 return -EBUSY;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003207
3208 if (nr > IO_PLUG_THRESHOLD) {
3209 io_submit_state_start(&state, ctx, nr);
3210 statep = &state;
3211 }
3212
3213 for (i = 0; i < nr; i++) {
Pavel Begunkov196be952019-11-07 01:41:06 +03003214 struct io_kiocb *req;
Pavel Begunkov50585b92019-11-07 01:41:07 +03003215 unsigned int sqe_flags;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003216
Pavel Begunkov196be952019-11-07 01:41:06 +03003217 req = io_get_req(ctx, statep);
3218 if (unlikely(!req)) {
3219 if (!submitted)
3220 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003221 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06003222 }
Pavel Begunkov50585b92019-11-07 01:41:07 +03003223 if (!io_get_sqring(ctx, &req->submit)) {
Pavel Begunkov196be952019-11-07 01:41:06 +03003224 __io_free_req(req);
3225 break;
3226 }
Jens Axboe9e645e112019-05-10 16:07:28 -06003227
Pavel Begunkov50585b92019-11-07 01:41:07 +03003228 if (io_sqe_needs_user(req->submit.sqe) && !*mm) {
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03003229 mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm);
3230 if (!mm_fault) {
3231 use_mm(ctx->sqo_mm);
3232 *mm = ctx->sqo_mm;
3233 }
3234 }
3235
Pavel Begunkov50585b92019-11-07 01:41:07 +03003236 sqe_flags = req->submit.sqe->flags;
3237
Pavel Begunkov50585b92019-11-07 01:41:07 +03003238 req->submit.ring_file = ring_file;
3239 req->submit.ring_fd = ring_fd;
3240 req->submit.has_user = *mm != NULL;
3241 req->submit.in_async = async;
3242 req->submit.needs_fixed_file = async;
3243 trace_io_uring_submit_sqe(ctx, req->submit.sqe->user_data,
3244 true, async);
Jackie Liua197f662019-11-08 08:09:12 -07003245 io_submit_sqe(req, statep, &link);
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03003246 submitted++;
Pavel Begunkove5eb6362019-11-06 00:22:15 +03003247
3248 /*
3249 * If previous wasn't linked and we have a linked command,
3250 * that's the end of the chain. Submit the previous link.
3251 */
Pavel Begunkov50585b92019-11-07 01:41:07 +03003252 if (!(sqe_flags & IOSQE_IO_LINK) && link) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003253 io_queue_link_head(link);
Pavel Begunkove5eb6362019-11-06 00:22:15 +03003254 link = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003255 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07003256 }
3257
Jens Axboe9e645e112019-05-10 16:07:28 -06003258 if (link)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003259 io_queue_link_head(link);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003260 if (statep)
3261 io_submit_state_end(&state);
3262
Pavel Begunkovae9428c2019-11-06 00:22:14 +03003263 /* Commit SQ ring head once we've consumed and submitted all SQEs */
3264 io_commit_sqring(ctx);
3265
Jens Axboe6c271ce2019-01-10 11:22:30 -07003266 return submitted;
3267}
3268
3269static int io_sq_thread(void *data)
3270{
Jens Axboe6c271ce2019-01-10 11:22:30 -07003271 struct io_ring_ctx *ctx = data;
3272 struct mm_struct *cur_mm = NULL;
Jens Axboe181e4482019-11-25 08:52:30 -07003273 const struct cred *old_cred;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003274 mm_segment_t old_fs;
3275 DEFINE_WAIT(wait);
3276 unsigned inflight;
3277 unsigned long timeout;
Jens Axboec1edbf52019-11-10 16:56:04 -07003278 int ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003279
Jens Axboe206aefd2019-11-07 18:27:42 -07003280 complete(&ctx->completions[1]);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08003281
Jens Axboe6c271ce2019-01-10 11:22:30 -07003282 old_fs = get_fs();
3283 set_fs(USER_DS);
Jens Axboe181e4482019-11-25 08:52:30 -07003284 old_cred = override_creds(ctx->creds);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003285
Jens Axboec1edbf52019-11-10 16:56:04 -07003286 ret = timeout = inflight = 0;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003287 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003288 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003289
3290 if (inflight) {
3291 unsigned nr_events = 0;
3292
3293 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe2b2ed972019-10-25 10:06:15 -06003294 /*
3295 * inflight is the count of the maximum possible
3296 * entries we submitted, but it can be smaller
3297 * if we dropped some of them. If we don't have
3298 * poll entries available, then we know that we
3299 * have nothing left to poll for. Reset the
3300 * inflight count to zero in that case.
3301 */
3302 mutex_lock(&ctx->uring_lock);
3303 if (!list_empty(&ctx->poll_list))
3304 __io_iopoll_check(ctx, &nr_events, 0);
3305 else
3306 inflight = 0;
3307 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003308 } else {
3309 /*
3310 * Normal IO, just pretend everything completed.
3311 * We don't have to poll completions for that.
3312 */
3313 nr_events = inflight;
3314 }
3315
3316 inflight -= nr_events;
3317 if (!inflight)
3318 timeout = jiffies + ctx->sq_thread_idle;
3319 }
3320
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003321 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07003322
3323 /*
3324 * If submit got -EBUSY, flag us as needing the application
3325 * to enter the kernel to reap and flush events.
3326 */
3327 if (!to_submit || ret == -EBUSY) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07003328 /*
3329 * We're polling. If we're within the defined idle
3330 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07003331 * to sleep. The exception is if we got EBUSY doing
3332 * more IO, we should wait for the application to
3333 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07003334 */
Jens Axboec1edbf52019-11-10 16:56:04 -07003335 if (inflight ||
3336 (!time_after(jiffies, timeout) && ret != -EBUSY)) {
Jens Axboe9831a902019-09-19 09:48:55 -06003337 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07003338 continue;
3339 }
3340
3341 /*
3342 * Drop cur_mm before scheduling, we can't hold it for
3343 * long periods (or over schedule()). Do this before
3344 * adding ourselves to the waitqueue, as the unuse/drop
3345 * may sleep.
3346 */
3347 if (cur_mm) {
3348 unuse_mm(cur_mm);
3349 mmput(cur_mm);
3350 cur_mm = NULL;
3351 }
3352
3353 prepare_to_wait(&ctx->sqo_wait, &wait,
3354 TASK_INTERRUPTIBLE);
3355
3356 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00003357 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02003358 /* make sure to read SQ tail after writing flags */
3359 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07003360
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003361 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07003362 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003363 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07003364 finish_wait(&ctx->sqo_wait, &wait);
3365 break;
3366 }
3367 if (signal_pending(current))
3368 flush_signals(current);
3369 schedule();
3370 finish_wait(&ctx->sqo_wait, &wait);
3371
Hristo Venev75b28af2019-08-26 17:23:46 +00003372 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003373 continue;
3374 }
3375 finish_wait(&ctx->sqo_wait, &wait);
3376
Hristo Venev75b28af2019-08-26 17:23:46 +00003377 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003378 }
3379
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003380 to_submit = min(to_submit, ctx->sq_entries);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003381 ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true);
3382 if (ret > 0)
3383 inflight += ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003384 }
3385
3386 set_fs(old_fs);
3387 if (cur_mm) {
3388 unuse_mm(cur_mm);
3389 mmput(cur_mm);
3390 }
Jens Axboe181e4482019-11-25 08:52:30 -07003391 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06003392
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003393 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06003394
Jens Axboe6c271ce2019-01-10 11:22:30 -07003395 return 0;
3396}
3397
Jens Axboebda52162019-09-24 13:47:15 -06003398struct io_wait_queue {
3399 struct wait_queue_entry wq;
3400 struct io_ring_ctx *ctx;
3401 unsigned to_wait;
3402 unsigned nr_timeouts;
3403};
3404
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003405static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06003406{
3407 struct io_ring_ctx *ctx = iowq->ctx;
3408
3409 /*
3410 * Wake up if we have enough events, or if a timeout occured since we
3411 * started waiting. For timeouts, we always want to return to userspace,
3412 * regardless of event count.
3413 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003414 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06003415 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
3416}
3417
3418static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
3419 int wake_flags, void *key)
3420{
3421 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
3422 wq);
3423
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003424 /* use noflush == true, as we can't safely rely on locking context */
3425 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06003426 return -1;
3427
3428 return autoremove_wake_function(curr, mode, wake_flags, key);
3429}
3430
Jens Axboe2b188cc2019-01-07 10:46:33 -07003431/*
3432 * Wait until events become available, if we don't already have some. The
3433 * application must reap them itself, as they reside on the shared cq ring.
3434 */
3435static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
3436 const sigset_t __user *sig, size_t sigsz)
3437{
Jens Axboebda52162019-09-24 13:47:15 -06003438 struct io_wait_queue iowq = {
3439 .wq = {
3440 .private = current,
3441 .func = io_wake_function,
3442 .entry = LIST_HEAD_INIT(iowq.wq.entry),
3443 },
3444 .ctx = ctx,
3445 .to_wait = min_events,
3446 };
Hristo Venev75b28af2019-08-26 17:23:46 +00003447 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08003448 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003449
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003450 if (io_cqring_events(ctx, false) >= min_events)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003451 return 0;
3452
3453 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01003454#ifdef CONFIG_COMPAT
3455 if (in_compat_syscall())
3456 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07003457 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01003458 else
3459#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07003460 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01003461
Jens Axboe2b188cc2019-01-07 10:46:33 -07003462 if (ret)
3463 return ret;
3464 }
3465
Jens Axboebda52162019-09-24 13:47:15 -06003466 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02003467 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06003468 do {
3469 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
3470 TASK_INTERRUPTIBLE);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003471 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06003472 break;
3473 schedule();
3474 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08003475 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06003476 break;
3477 }
3478 } while (1);
3479 finish_wait(&ctx->wait, &iowq.wq);
3480
Jackie Liue9ffa5c2019-10-29 11:16:42 +08003481 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003482
Hristo Venev75b28af2019-08-26 17:23:46 +00003483 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003484}
3485
Jens Axboe6b063142019-01-10 22:13:58 -07003486static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
3487{
3488#if defined(CONFIG_UNIX)
3489 if (ctx->ring_sock) {
3490 struct sock *sock = ctx->ring_sock->sk;
3491 struct sk_buff *skb;
3492
3493 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
3494 kfree_skb(skb);
3495 }
3496#else
3497 int i;
3498
Jens Axboe65e19f52019-10-26 07:20:21 -06003499 for (i = 0; i < ctx->nr_user_files; i++) {
3500 struct file *file;
3501
3502 file = io_file_from_index(ctx, i);
3503 if (file)
3504 fput(file);
3505 }
Jens Axboe6b063142019-01-10 22:13:58 -07003506#endif
3507}
3508
3509static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
3510{
Jens Axboe65e19f52019-10-26 07:20:21 -06003511 unsigned nr_tables, i;
3512
3513 if (!ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003514 return -ENXIO;
3515
3516 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06003517 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
3518 for (i = 0; i < nr_tables; i++)
3519 kfree(ctx->file_table[i].files);
3520 kfree(ctx->file_table);
3521 ctx->file_table = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003522 ctx->nr_user_files = 0;
3523 return 0;
3524}
3525
Jens Axboe6c271ce2019-01-10 11:22:30 -07003526static void io_sq_thread_stop(struct io_ring_ctx *ctx)
3527{
3528 if (ctx->sqo_thread) {
Jens Axboe206aefd2019-11-07 18:27:42 -07003529 wait_for_completion(&ctx->completions[1]);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003530 /*
3531 * The park is a bit of a work-around, without it we get
3532 * warning spews on shutdown with SQPOLL set and affinity
3533 * set to a single CPU.
3534 */
Jens Axboe06058632019-04-13 09:26:03 -06003535 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003536 kthread_stop(ctx->sqo_thread);
3537 ctx->sqo_thread = NULL;
3538 }
3539}
3540
Jens Axboe6b063142019-01-10 22:13:58 -07003541static void io_finish_async(struct io_ring_ctx *ctx)
3542{
Jens Axboe6c271ce2019-01-10 11:22:30 -07003543 io_sq_thread_stop(ctx);
3544
Jens Axboe561fb042019-10-24 07:25:42 -06003545 if (ctx->io_wq) {
3546 io_wq_destroy(ctx->io_wq);
3547 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003548 }
3549}
3550
3551#if defined(CONFIG_UNIX)
3552static void io_destruct_skb(struct sk_buff *skb)
3553{
3554 struct io_ring_ctx *ctx = skb->sk->sk_user_data;
3555
Jens Axboe561fb042019-10-24 07:25:42 -06003556 if (ctx->io_wq)
3557 io_wq_flush(ctx->io_wq);
Jens Axboe8a997342019-10-09 14:40:13 -06003558
Jens Axboe6b063142019-01-10 22:13:58 -07003559 unix_destruct_scm(skb);
3560}
3561
3562/*
3563 * Ensure the UNIX gc is aware of our file set, so we are certain that
3564 * the io_uring can be safely unregistered on process exit, even if we have
3565 * loops in the file referencing.
3566 */
3567static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
3568{
3569 struct sock *sk = ctx->ring_sock->sk;
3570 struct scm_fp_list *fpl;
3571 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06003572 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07003573
3574 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
3575 unsigned long inflight = ctx->user->unix_inflight + nr;
3576
3577 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
3578 return -EMFILE;
3579 }
3580
3581 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
3582 if (!fpl)
3583 return -ENOMEM;
3584
3585 skb = alloc_skb(0, GFP_KERNEL);
3586 if (!skb) {
3587 kfree(fpl);
3588 return -ENOMEM;
3589 }
3590
3591 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07003592
Jens Axboe08a45172019-10-03 08:11:03 -06003593 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07003594 fpl->user = get_uid(ctx->user);
3595 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003596 struct file *file = io_file_from_index(ctx, i + offset);
3597
3598 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06003599 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06003600 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06003601 unix_inflight(fpl->user, fpl->fp[nr_files]);
3602 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07003603 }
3604
Jens Axboe08a45172019-10-03 08:11:03 -06003605 if (nr_files) {
3606 fpl->max = SCM_MAX_FD;
3607 fpl->count = nr_files;
3608 UNIXCB(skb).fp = fpl;
3609 skb->destructor = io_destruct_skb;
3610 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
3611 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07003612
Jens Axboe08a45172019-10-03 08:11:03 -06003613 for (i = 0; i < nr_files; i++)
3614 fput(fpl->fp[i]);
3615 } else {
3616 kfree_skb(skb);
3617 kfree(fpl);
3618 }
Jens Axboe6b063142019-01-10 22:13:58 -07003619
3620 return 0;
3621}
3622
3623/*
3624 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
3625 * causes regular reference counting to break down. We rely on the UNIX
3626 * garbage collection to take care of this problem for us.
3627 */
3628static int io_sqe_files_scm(struct io_ring_ctx *ctx)
3629{
3630 unsigned left, total;
3631 int ret = 0;
3632
3633 total = 0;
3634 left = ctx->nr_user_files;
3635 while (left) {
3636 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07003637
3638 ret = __io_sqe_files_scm(ctx, this_files, total);
3639 if (ret)
3640 break;
3641 left -= this_files;
3642 total += this_files;
3643 }
3644
3645 if (!ret)
3646 return 0;
3647
3648 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003649 struct file *file = io_file_from_index(ctx, total);
3650
3651 if (file)
3652 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07003653 total++;
3654 }
3655
3656 return ret;
3657}
3658#else
3659static int io_sqe_files_scm(struct io_ring_ctx *ctx)
3660{
3661 return 0;
3662}
3663#endif
3664
Jens Axboe65e19f52019-10-26 07:20:21 -06003665static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
3666 unsigned nr_files)
3667{
3668 int i;
3669
3670 for (i = 0; i < nr_tables; i++) {
3671 struct fixed_file_table *table = &ctx->file_table[i];
3672 unsigned this_files;
3673
3674 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
3675 table->files = kcalloc(this_files, sizeof(struct file *),
3676 GFP_KERNEL);
3677 if (!table->files)
3678 break;
3679 nr_files -= this_files;
3680 }
3681
3682 if (i == nr_tables)
3683 return 0;
3684
3685 for (i = 0; i < nr_tables; i++) {
3686 struct fixed_file_table *table = &ctx->file_table[i];
3687 kfree(table->files);
3688 }
3689 return 1;
3690}
3691
Jens Axboe6b063142019-01-10 22:13:58 -07003692static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
3693 unsigned nr_args)
3694{
3695 __s32 __user *fds = (__s32 __user *) arg;
Jens Axboe65e19f52019-10-26 07:20:21 -06003696 unsigned nr_tables;
Jens Axboe6b063142019-01-10 22:13:58 -07003697 int fd, ret = 0;
3698 unsigned i;
3699
Jens Axboe65e19f52019-10-26 07:20:21 -06003700 if (ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003701 return -EBUSY;
3702 if (!nr_args)
3703 return -EINVAL;
3704 if (nr_args > IORING_MAX_FIXED_FILES)
3705 return -EMFILE;
3706
Jens Axboe65e19f52019-10-26 07:20:21 -06003707 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
3708 ctx->file_table = kcalloc(nr_tables, sizeof(struct fixed_file_table),
3709 GFP_KERNEL);
3710 if (!ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003711 return -ENOMEM;
3712
Jens Axboe65e19f52019-10-26 07:20:21 -06003713 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
3714 kfree(ctx->file_table);
Jens Axboe46568e92019-11-10 08:40:53 -07003715 ctx->file_table = NULL;
Jens Axboe65e19f52019-10-26 07:20:21 -06003716 return -ENOMEM;
3717 }
3718
Jens Axboe08a45172019-10-03 08:11:03 -06003719 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003720 struct fixed_file_table *table;
3721 unsigned index;
3722
Jens Axboe6b063142019-01-10 22:13:58 -07003723 ret = -EFAULT;
3724 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
3725 break;
Jens Axboe08a45172019-10-03 08:11:03 -06003726 /* allow sparse sets */
3727 if (fd == -1) {
3728 ret = 0;
3729 continue;
3730 }
Jens Axboe6b063142019-01-10 22:13:58 -07003731
Jens Axboe65e19f52019-10-26 07:20:21 -06003732 table = &ctx->file_table[i >> IORING_FILE_TABLE_SHIFT];
3733 index = i & IORING_FILE_TABLE_MASK;
3734 table->files[index] = fget(fd);
Jens Axboe6b063142019-01-10 22:13:58 -07003735
3736 ret = -EBADF;
Jens Axboe65e19f52019-10-26 07:20:21 -06003737 if (!table->files[index])
Jens Axboe6b063142019-01-10 22:13:58 -07003738 break;
3739 /*
3740 * Don't allow io_uring instances to be registered. If UNIX
3741 * isn't enabled, then this causes a reference cycle and this
3742 * instance can never get freed. If UNIX is enabled we'll
3743 * handle it just fine, but there's still no point in allowing
3744 * a ring fd as it doesn't support regular read/write anyway.
3745 */
Jens Axboe65e19f52019-10-26 07:20:21 -06003746 if (table->files[index]->f_op == &io_uring_fops) {
3747 fput(table->files[index]);
Jens Axboe6b063142019-01-10 22:13:58 -07003748 break;
3749 }
Jens Axboe6b063142019-01-10 22:13:58 -07003750 ret = 0;
3751 }
3752
3753 if (ret) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003754 for (i = 0; i < ctx->nr_user_files; i++) {
3755 struct file *file;
Jens Axboe6b063142019-01-10 22:13:58 -07003756
Jens Axboe65e19f52019-10-26 07:20:21 -06003757 file = io_file_from_index(ctx, i);
3758 if (file)
3759 fput(file);
3760 }
3761 for (i = 0; i < nr_tables; i++)
3762 kfree(ctx->file_table[i].files);
3763
3764 kfree(ctx->file_table);
3765 ctx->file_table = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003766 ctx->nr_user_files = 0;
3767 return ret;
3768 }
3769
3770 ret = io_sqe_files_scm(ctx);
3771 if (ret)
3772 io_sqe_files_unregister(ctx);
3773
3774 return ret;
3775}
3776
Jens Axboec3a31e62019-10-03 13:59:56 -06003777static void io_sqe_file_unregister(struct io_ring_ctx *ctx, int index)
3778{
3779#if defined(CONFIG_UNIX)
Jens Axboe65e19f52019-10-26 07:20:21 -06003780 struct file *file = io_file_from_index(ctx, index);
Jens Axboec3a31e62019-10-03 13:59:56 -06003781 struct sock *sock = ctx->ring_sock->sk;
3782 struct sk_buff_head list, *head = &sock->sk_receive_queue;
3783 struct sk_buff *skb;
3784 int i;
3785
3786 __skb_queue_head_init(&list);
3787
3788 /*
3789 * Find the skb that holds this file in its SCM_RIGHTS. When found,
3790 * remove this entry and rearrange the file array.
3791 */
3792 skb = skb_dequeue(head);
3793 while (skb) {
3794 struct scm_fp_list *fp;
3795
3796 fp = UNIXCB(skb).fp;
3797 for (i = 0; i < fp->count; i++) {
3798 int left;
3799
3800 if (fp->fp[i] != file)
3801 continue;
3802
3803 unix_notinflight(fp->user, fp->fp[i]);
3804 left = fp->count - 1 - i;
3805 if (left) {
3806 memmove(&fp->fp[i], &fp->fp[i + 1],
3807 left * sizeof(struct file *));
3808 }
3809 fp->count--;
3810 if (!fp->count) {
3811 kfree_skb(skb);
3812 skb = NULL;
3813 } else {
3814 __skb_queue_tail(&list, skb);
3815 }
3816 fput(file);
3817 file = NULL;
3818 break;
3819 }
3820
3821 if (!file)
3822 break;
3823
3824 __skb_queue_tail(&list, skb);
3825
3826 skb = skb_dequeue(head);
3827 }
3828
3829 if (skb_peek(&list)) {
3830 spin_lock_irq(&head->lock);
3831 while ((skb = __skb_dequeue(&list)) != NULL)
3832 __skb_queue_tail(head, skb);
3833 spin_unlock_irq(&head->lock);
3834 }
3835#else
Jens Axboe65e19f52019-10-26 07:20:21 -06003836 fput(io_file_from_index(ctx, index));
Jens Axboec3a31e62019-10-03 13:59:56 -06003837#endif
3838}
3839
3840static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
3841 int index)
3842{
3843#if defined(CONFIG_UNIX)
3844 struct sock *sock = ctx->ring_sock->sk;
3845 struct sk_buff_head *head = &sock->sk_receive_queue;
3846 struct sk_buff *skb;
3847
3848 /*
3849 * See if we can merge this file into an existing skb SCM_RIGHTS
3850 * file set. If there's no room, fall back to allocating a new skb
3851 * and filling it in.
3852 */
3853 spin_lock_irq(&head->lock);
3854 skb = skb_peek(head);
3855 if (skb) {
3856 struct scm_fp_list *fpl = UNIXCB(skb).fp;
3857
3858 if (fpl->count < SCM_MAX_FD) {
3859 __skb_unlink(skb, head);
3860 spin_unlock_irq(&head->lock);
3861 fpl->fp[fpl->count] = get_file(file);
3862 unix_inflight(fpl->user, fpl->fp[fpl->count]);
3863 fpl->count++;
3864 spin_lock_irq(&head->lock);
3865 __skb_queue_head(head, skb);
3866 } else {
3867 skb = NULL;
3868 }
3869 }
3870 spin_unlock_irq(&head->lock);
3871
3872 if (skb) {
3873 fput(file);
3874 return 0;
3875 }
3876
3877 return __io_sqe_files_scm(ctx, 1, index);
3878#else
3879 return 0;
3880#endif
3881}
3882
3883static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
3884 unsigned nr_args)
3885{
3886 struct io_uring_files_update up;
3887 __s32 __user *fds;
3888 int fd, i, err;
3889 __u32 done;
3890
Jens Axboe65e19f52019-10-26 07:20:21 -06003891 if (!ctx->file_table)
Jens Axboec3a31e62019-10-03 13:59:56 -06003892 return -ENXIO;
3893 if (!nr_args)
3894 return -EINVAL;
3895 if (copy_from_user(&up, arg, sizeof(up)))
3896 return -EFAULT;
3897 if (check_add_overflow(up.offset, nr_args, &done))
3898 return -EOVERFLOW;
3899 if (done > ctx->nr_user_files)
3900 return -EINVAL;
3901
3902 done = 0;
3903 fds = (__s32 __user *) up.fds;
3904 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003905 struct fixed_file_table *table;
3906 unsigned index;
3907
Jens Axboec3a31e62019-10-03 13:59:56 -06003908 err = 0;
3909 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
3910 err = -EFAULT;
3911 break;
3912 }
3913 i = array_index_nospec(up.offset, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06003914 table = &ctx->file_table[i >> IORING_FILE_TABLE_SHIFT];
3915 index = i & IORING_FILE_TABLE_MASK;
3916 if (table->files[index]) {
Jens Axboec3a31e62019-10-03 13:59:56 -06003917 io_sqe_file_unregister(ctx, i);
Jens Axboe65e19f52019-10-26 07:20:21 -06003918 table->files[index] = NULL;
Jens Axboec3a31e62019-10-03 13:59:56 -06003919 }
3920 if (fd != -1) {
3921 struct file *file;
3922
3923 file = fget(fd);
3924 if (!file) {
3925 err = -EBADF;
3926 break;
3927 }
3928 /*
3929 * Don't allow io_uring instances to be registered. If
3930 * UNIX isn't enabled, then this causes a reference
3931 * cycle and this instance can never get freed. If UNIX
3932 * is enabled we'll handle it just fine, but there's
3933 * still no point in allowing a ring fd as it doesn't
3934 * support regular read/write anyway.
3935 */
3936 if (file->f_op == &io_uring_fops) {
3937 fput(file);
3938 err = -EBADF;
3939 break;
3940 }
Jens Axboe65e19f52019-10-26 07:20:21 -06003941 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06003942 err = io_sqe_file_register(ctx, file, i);
3943 if (err)
3944 break;
3945 }
3946 nr_args--;
3947 done++;
3948 up.offset++;
3949 }
3950
3951 return done ? done : err;
3952}
3953
Jens Axboe7d723062019-11-12 22:31:31 -07003954static void io_put_work(struct io_wq_work *work)
3955{
3956 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
3957
3958 io_put_req(req);
3959}
3960
3961static void io_get_work(struct io_wq_work *work)
3962{
3963 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
3964
3965 refcount_inc(&req->refs);
3966}
3967
Jens Axboe6c271ce2019-01-10 11:22:30 -07003968static int io_sq_offload_start(struct io_ring_ctx *ctx,
3969 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003970{
Jens Axboe576a3472019-11-25 08:49:20 -07003971 struct io_wq_data data;
Jens Axboe561fb042019-10-24 07:25:42 -06003972 unsigned concurrency;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003973 int ret;
3974
Jens Axboe6c271ce2019-01-10 11:22:30 -07003975 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003976 mmgrab(current->mm);
3977 ctx->sqo_mm = current->mm;
3978
Jens Axboe6c271ce2019-01-10 11:22:30 -07003979 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06003980 ret = -EPERM;
3981 if (!capable(CAP_SYS_ADMIN))
3982 goto err;
3983
Jens Axboe917257d2019-04-13 09:28:55 -06003984 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
3985 if (!ctx->sq_thread_idle)
3986 ctx->sq_thread_idle = HZ;
3987
Jens Axboe6c271ce2019-01-10 11:22:30 -07003988 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06003989 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003990
Jens Axboe917257d2019-04-13 09:28:55 -06003991 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06003992 if (cpu >= nr_cpu_ids)
3993 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08003994 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06003995 goto err;
3996
Jens Axboe6c271ce2019-01-10 11:22:30 -07003997 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
3998 ctx, cpu,
3999 "io_uring-sq");
4000 } else {
4001 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
4002 "io_uring-sq");
4003 }
4004 if (IS_ERR(ctx->sqo_thread)) {
4005 ret = PTR_ERR(ctx->sqo_thread);
4006 ctx->sqo_thread = NULL;
4007 goto err;
4008 }
4009 wake_up_process(ctx->sqo_thread);
4010 } else if (p->flags & IORING_SETUP_SQ_AFF) {
4011 /* Can't have SQ_AFF without SQPOLL */
4012 ret = -EINVAL;
4013 goto err;
4014 }
4015
Jens Axboe576a3472019-11-25 08:49:20 -07004016 data.mm = ctx->sqo_mm;
4017 data.user = ctx->user;
Jens Axboe181e4482019-11-25 08:52:30 -07004018 data.creds = ctx->creds;
Jens Axboe576a3472019-11-25 08:49:20 -07004019 data.get_work = io_get_work;
4020 data.put_work = io_put_work;
4021
Jens Axboe561fb042019-10-24 07:25:42 -06004022 /* Do QD, or 4 * CPUS, whatever is smallest */
4023 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
Jens Axboe576a3472019-11-25 08:49:20 -07004024 ctx->io_wq = io_wq_create(concurrency, &data);
Jens Axboe975c99a52019-10-30 08:42:56 -06004025 if (IS_ERR(ctx->io_wq)) {
4026 ret = PTR_ERR(ctx->io_wq);
4027 ctx->io_wq = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004028 goto err;
4029 }
4030
4031 return 0;
4032err:
Jens Axboe54a91f32019-09-10 09:15:04 -06004033 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004034 mmdrop(ctx->sqo_mm);
4035 ctx->sqo_mm = NULL;
4036 return ret;
4037}
4038
4039static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
4040{
4041 atomic_long_sub(nr_pages, &user->locked_vm);
4042}
4043
4044static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
4045{
4046 unsigned long page_limit, cur_pages, new_pages;
4047
4048 /* Don't allow more pages than we can safely lock */
4049 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
4050
4051 do {
4052 cur_pages = atomic_long_read(&user->locked_vm);
4053 new_pages = cur_pages + nr_pages;
4054 if (new_pages > page_limit)
4055 return -ENOMEM;
4056 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
4057 new_pages) != cur_pages);
4058
4059 return 0;
4060}
4061
4062static void io_mem_free(void *ptr)
4063{
Mark Rutland52e04ef2019-04-30 17:30:21 +01004064 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004065
Mark Rutland52e04ef2019-04-30 17:30:21 +01004066 if (!ptr)
4067 return;
4068
4069 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004070 if (put_page_testzero(page))
4071 free_compound_page(page);
4072}
4073
4074static void *io_mem_alloc(size_t size)
4075{
4076 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
4077 __GFP_NORETRY;
4078
4079 return (void *) __get_free_pages(gfp_flags, get_order(size));
4080}
4081
Hristo Venev75b28af2019-08-26 17:23:46 +00004082static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
4083 size_t *sq_offset)
4084{
4085 struct io_rings *rings;
4086 size_t off, sq_array_size;
4087
4088 off = struct_size(rings, cqes, cq_entries);
4089 if (off == SIZE_MAX)
4090 return SIZE_MAX;
4091
4092#ifdef CONFIG_SMP
4093 off = ALIGN(off, SMP_CACHE_BYTES);
4094 if (off == 0)
4095 return SIZE_MAX;
4096#endif
4097
4098 sq_array_size = array_size(sizeof(u32), sq_entries);
4099 if (sq_array_size == SIZE_MAX)
4100 return SIZE_MAX;
4101
4102 if (check_add_overflow(off, sq_array_size, &off))
4103 return SIZE_MAX;
4104
4105 if (sq_offset)
4106 *sq_offset = off;
4107
4108 return off;
4109}
4110
Jens Axboe2b188cc2019-01-07 10:46:33 -07004111static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
4112{
Hristo Venev75b28af2019-08-26 17:23:46 +00004113 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004114
Hristo Venev75b28af2019-08-26 17:23:46 +00004115 pages = (size_t)1 << get_order(
4116 rings_size(sq_entries, cq_entries, NULL));
4117 pages += (size_t)1 << get_order(
4118 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07004119
Hristo Venev75b28af2019-08-26 17:23:46 +00004120 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004121}
4122
Jens Axboeedafcce2019-01-09 09:16:05 -07004123static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
4124{
4125 int i, j;
4126
4127 if (!ctx->user_bufs)
4128 return -ENXIO;
4129
4130 for (i = 0; i < ctx->nr_user_bufs; i++) {
4131 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
4132
4133 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbard27c4d3a2019-08-04 19:32:06 -07004134 put_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07004135
4136 if (ctx->account_mem)
4137 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004138 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07004139 imu->nr_bvecs = 0;
4140 }
4141
4142 kfree(ctx->user_bufs);
4143 ctx->user_bufs = NULL;
4144 ctx->nr_user_bufs = 0;
4145 return 0;
4146}
4147
4148static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
4149 void __user *arg, unsigned index)
4150{
4151 struct iovec __user *src;
4152
4153#ifdef CONFIG_COMPAT
4154 if (ctx->compat) {
4155 struct compat_iovec __user *ciovs;
4156 struct compat_iovec ciov;
4157
4158 ciovs = (struct compat_iovec __user *) arg;
4159 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
4160 return -EFAULT;
4161
4162 dst->iov_base = (void __user *) (unsigned long) ciov.iov_base;
4163 dst->iov_len = ciov.iov_len;
4164 return 0;
4165 }
4166#endif
4167 src = (struct iovec __user *) arg;
4168 if (copy_from_user(dst, &src[index], sizeof(*dst)))
4169 return -EFAULT;
4170 return 0;
4171}
4172
4173static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
4174 unsigned nr_args)
4175{
4176 struct vm_area_struct **vmas = NULL;
4177 struct page **pages = NULL;
4178 int i, j, got_pages = 0;
4179 int ret = -EINVAL;
4180
4181 if (ctx->user_bufs)
4182 return -EBUSY;
4183 if (!nr_args || nr_args > UIO_MAXIOV)
4184 return -EINVAL;
4185
4186 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
4187 GFP_KERNEL);
4188 if (!ctx->user_bufs)
4189 return -ENOMEM;
4190
4191 for (i = 0; i < nr_args; i++) {
4192 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
4193 unsigned long off, start, end, ubuf;
4194 int pret, nr_pages;
4195 struct iovec iov;
4196 size_t size;
4197
4198 ret = io_copy_iov(ctx, &iov, arg, i);
4199 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03004200 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07004201
4202 /*
4203 * Don't impose further limits on the size and buffer
4204 * constraints here, we'll -EINVAL later when IO is
4205 * submitted if they are wrong.
4206 */
4207 ret = -EFAULT;
4208 if (!iov.iov_base || !iov.iov_len)
4209 goto err;
4210
4211 /* arbitrary limit, but we need something */
4212 if (iov.iov_len > SZ_1G)
4213 goto err;
4214
4215 ubuf = (unsigned long) iov.iov_base;
4216 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
4217 start = ubuf >> PAGE_SHIFT;
4218 nr_pages = end - start;
4219
4220 if (ctx->account_mem) {
4221 ret = io_account_mem(ctx->user, nr_pages);
4222 if (ret)
4223 goto err;
4224 }
4225
4226 ret = 0;
4227 if (!pages || nr_pages > got_pages) {
4228 kfree(vmas);
4229 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004230 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07004231 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004232 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07004233 sizeof(struct vm_area_struct *),
4234 GFP_KERNEL);
4235 if (!pages || !vmas) {
4236 ret = -ENOMEM;
4237 if (ctx->account_mem)
4238 io_unaccount_mem(ctx->user, nr_pages);
4239 goto err;
4240 }
4241 got_pages = nr_pages;
4242 }
4243
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004244 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07004245 GFP_KERNEL);
4246 ret = -ENOMEM;
4247 if (!imu->bvec) {
4248 if (ctx->account_mem)
4249 io_unaccount_mem(ctx->user, nr_pages);
4250 goto err;
4251 }
4252
4253 ret = 0;
4254 down_read(&current->mm->mmap_sem);
Ira Weiny932f4a62019-05-13 17:17:03 -07004255 pret = get_user_pages(ubuf, nr_pages,
4256 FOLL_WRITE | FOLL_LONGTERM,
4257 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07004258 if (pret == nr_pages) {
4259 /* don't support file backed memory */
4260 for (j = 0; j < nr_pages; j++) {
4261 struct vm_area_struct *vma = vmas[j];
4262
4263 if (vma->vm_file &&
4264 !is_file_hugepages(vma->vm_file)) {
4265 ret = -EOPNOTSUPP;
4266 break;
4267 }
4268 }
4269 } else {
4270 ret = pret < 0 ? pret : -EFAULT;
4271 }
4272 up_read(&current->mm->mmap_sem);
4273 if (ret) {
4274 /*
4275 * if we did partial map, or found file backed vmas,
4276 * release any pages we did get
4277 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07004278 if (pret > 0)
4279 put_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07004280 if (ctx->account_mem)
4281 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004282 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07004283 goto err;
4284 }
4285
4286 off = ubuf & ~PAGE_MASK;
4287 size = iov.iov_len;
4288 for (j = 0; j < nr_pages; j++) {
4289 size_t vec_len;
4290
4291 vec_len = min_t(size_t, size, PAGE_SIZE - off);
4292 imu->bvec[j].bv_page = pages[j];
4293 imu->bvec[j].bv_len = vec_len;
4294 imu->bvec[j].bv_offset = off;
4295 off = 0;
4296 size -= vec_len;
4297 }
4298 /* store original address for later verification */
4299 imu->ubuf = ubuf;
4300 imu->len = iov.iov_len;
4301 imu->nr_bvecs = nr_pages;
4302
4303 ctx->nr_user_bufs++;
4304 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004305 kvfree(pages);
4306 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07004307 return 0;
4308err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004309 kvfree(pages);
4310 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07004311 io_sqe_buffer_unregister(ctx);
4312 return ret;
4313}
4314
Jens Axboe9b402842019-04-11 11:45:41 -06004315static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
4316{
4317 __s32 __user *fds = arg;
4318 int fd;
4319
4320 if (ctx->cq_ev_fd)
4321 return -EBUSY;
4322
4323 if (copy_from_user(&fd, fds, sizeof(*fds)))
4324 return -EFAULT;
4325
4326 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
4327 if (IS_ERR(ctx->cq_ev_fd)) {
4328 int ret = PTR_ERR(ctx->cq_ev_fd);
4329 ctx->cq_ev_fd = NULL;
4330 return ret;
4331 }
4332
4333 return 0;
4334}
4335
4336static int io_eventfd_unregister(struct io_ring_ctx *ctx)
4337{
4338 if (ctx->cq_ev_fd) {
4339 eventfd_ctx_put(ctx->cq_ev_fd);
4340 ctx->cq_ev_fd = NULL;
4341 return 0;
4342 }
4343
4344 return -ENXIO;
4345}
4346
Jens Axboe2b188cc2019-01-07 10:46:33 -07004347static void io_ring_ctx_free(struct io_ring_ctx *ctx)
4348{
Jens Axboe6b063142019-01-10 22:13:58 -07004349 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004350 if (ctx->sqo_mm)
4351 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07004352
4353 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07004354 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07004355 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06004356 io_eventfd_unregister(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07004357
Jens Axboe2b188cc2019-01-07 10:46:33 -07004358#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07004359 if (ctx->ring_sock) {
4360 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004361 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07004362 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004363#endif
4364
Hristo Venev75b28af2019-08-26 17:23:46 +00004365 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004366 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004367
4368 percpu_ref_exit(&ctx->refs);
4369 if (ctx->account_mem)
4370 io_unaccount_mem(ctx->user,
4371 ring_pages(ctx->sq_entries, ctx->cq_entries));
4372 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07004373 put_cred(ctx->creds);
Jens Axboe206aefd2019-11-07 18:27:42 -07004374 kfree(ctx->completions);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07004375 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004376 kfree(ctx);
4377}
4378
4379static __poll_t io_uring_poll(struct file *file, poll_table *wait)
4380{
4381 struct io_ring_ctx *ctx = file->private_data;
4382 __poll_t mask = 0;
4383
4384 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02004385 /*
4386 * synchronizes with barrier from wq_has_sleeper call in
4387 * io_commit_cqring
4388 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004389 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00004390 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
4391 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004392 mask |= EPOLLOUT | EPOLLWRNORM;
yangerkundaa5de52019-09-24 20:53:34 +08004393 if (READ_ONCE(ctx->rings->cq.head) != ctx->cached_cq_tail)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004394 mask |= EPOLLIN | EPOLLRDNORM;
4395
4396 return mask;
4397}
4398
4399static int io_uring_fasync(int fd, struct file *file, int on)
4400{
4401 struct io_ring_ctx *ctx = file->private_data;
4402
4403 return fasync_helper(fd, file, on, &ctx->cq_fasync);
4404}
4405
4406static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
4407{
4408 mutex_lock(&ctx->uring_lock);
4409 percpu_ref_kill(&ctx->refs);
4410 mutex_unlock(&ctx->uring_lock);
4411
Jens Axboe5262f562019-09-17 12:26:57 -06004412 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004413 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06004414
4415 if (ctx->io_wq)
4416 io_wq_cancel_all(ctx->io_wq);
4417
Jens Axboedef596e2019-01-09 08:59:42 -07004418 io_iopoll_reap_events(ctx);
Jens Axboe15dff282019-11-13 09:09:23 -07004419 /* if we failed setting up the ctx, we might not have any rings */
4420 if (ctx->rings)
4421 io_cqring_overflow_flush(ctx, true);
Jens Axboe206aefd2019-11-07 18:27:42 -07004422 wait_for_completion(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004423 io_ring_ctx_free(ctx);
4424}
4425
4426static int io_uring_release(struct inode *inode, struct file *file)
4427{
4428 struct io_ring_ctx *ctx = file->private_data;
4429
4430 file->private_data = NULL;
4431 io_ring_ctx_wait_and_kill(ctx);
4432 return 0;
4433}
4434
Jens Axboefcb323c2019-10-24 12:39:47 -06004435static void io_uring_cancel_files(struct io_ring_ctx *ctx,
4436 struct files_struct *files)
4437{
4438 struct io_kiocb *req;
4439 DEFINE_WAIT(wait);
4440
4441 while (!list_empty_careful(&ctx->inflight_list)) {
Jens Axboe768134d2019-11-10 20:30:53 -07004442 struct io_kiocb *cancel_req = NULL;
Jens Axboefcb323c2019-10-24 12:39:47 -06004443
4444 spin_lock_irq(&ctx->inflight_lock);
4445 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07004446 if (req->work.files != files)
4447 continue;
4448 /* req is being completed, ignore */
4449 if (!refcount_inc_not_zero(&req->refs))
4450 continue;
4451 cancel_req = req;
4452 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06004453 }
Jens Axboe768134d2019-11-10 20:30:53 -07004454 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06004455 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07004456 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06004457 spin_unlock_irq(&ctx->inflight_lock);
4458
Jens Axboe768134d2019-11-10 20:30:53 -07004459 /* We need to keep going until we don't find a matching req */
4460 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06004461 break;
Bob Liu2f6d9b92019-11-13 18:06:24 +08004462
4463 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
4464 io_put_req(cancel_req);
Jens Axboefcb323c2019-10-24 12:39:47 -06004465 schedule();
4466 }
Jens Axboe768134d2019-11-10 20:30:53 -07004467 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06004468}
4469
4470static int io_uring_flush(struct file *file, void *data)
4471{
4472 struct io_ring_ctx *ctx = file->private_data;
4473
4474 io_uring_cancel_files(ctx, data);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004475 if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) {
4476 io_cqring_overflow_flush(ctx, true);
Jens Axboefcb323c2019-10-24 12:39:47 -06004477 io_wq_cancel_all(ctx->io_wq);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004478 }
Jens Axboefcb323c2019-10-24 12:39:47 -06004479 return 0;
4480}
4481
Jens Axboe2b188cc2019-01-07 10:46:33 -07004482static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
4483{
4484 loff_t offset = (loff_t) vma->vm_pgoff << PAGE_SHIFT;
4485 unsigned long sz = vma->vm_end - vma->vm_start;
4486 struct io_ring_ctx *ctx = file->private_data;
4487 unsigned long pfn;
4488 struct page *page;
4489 void *ptr;
4490
4491 switch (offset) {
4492 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00004493 case IORING_OFF_CQ_RING:
4494 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004495 break;
4496 case IORING_OFF_SQES:
4497 ptr = ctx->sq_sqes;
4498 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004499 default:
4500 return -EINVAL;
4501 }
4502
4503 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07004504 if (sz > page_size(page))
Jens Axboe2b188cc2019-01-07 10:46:33 -07004505 return -EINVAL;
4506
4507 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
4508 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
4509}
4510
4511SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
4512 u32, min_complete, u32, flags, const sigset_t __user *, sig,
4513 size_t, sigsz)
4514{
4515 struct io_ring_ctx *ctx;
4516 long ret = -EBADF;
4517 int submitted = 0;
4518 struct fd f;
4519
Jens Axboe6c271ce2019-01-10 11:22:30 -07004520 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07004521 return -EINVAL;
4522
4523 f = fdget(fd);
4524 if (!f.file)
4525 return -EBADF;
4526
4527 ret = -EOPNOTSUPP;
4528 if (f.file->f_op != &io_uring_fops)
4529 goto out_fput;
4530
4531 ret = -ENXIO;
4532 ctx = f.file->private_data;
4533 if (!percpu_ref_tryget(&ctx->refs))
4534 goto out_fput;
4535
Jens Axboe6c271ce2019-01-10 11:22:30 -07004536 /*
4537 * For SQ polling, the thread will do all submissions and completions.
4538 * Just return the requested submit count, and wake the thread if
4539 * we were asked to.
4540 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06004541 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004542 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07004543 if (!list_empty_careful(&ctx->cq_overflow_list))
4544 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004545 if (flags & IORING_ENTER_SQ_WAKEUP)
4546 wake_up(&ctx->sqo_wait);
4547 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06004548 } else if (to_submit) {
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004549 struct mm_struct *cur_mm;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004550
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004551 to_submit = min(to_submit, ctx->sq_entries);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004552 mutex_lock(&ctx->uring_lock);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004553 /* already have mm, so io_submit_sqes() won't try to grab it */
4554 cur_mm = ctx->sqo_mm;
4555 submitted = io_submit_sqes(ctx, to_submit, f.file, fd,
4556 &cur_mm, false);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004557 mutex_unlock(&ctx->uring_lock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004558 }
4559 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07004560 unsigned nr_events = 0;
4561
Jens Axboe2b188cc2019-01-07 10:46:33 -07004562 min_complete = min(min_complete, ctx->cq_entries);
4563
Jens Axboedef596e2019-01-09 08:59:42 -07004564 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07004565 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07004566 } else {
4567 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
4568 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004569 }
4570
Pavel Begunkov6805b322019-10-08 02:18:42 +03004571 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004572out_fput:
4573 fdput(f);
4574 return submitted ? submitted : ret;
4575}
4576
4577static const struct file_operations io_uring_fops = {
4578 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06004579 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07004580 .mmap = io_uring_mmap,
4581 .poll = io_uring_poll,
4582 .fasync = io_uring_fasync,
4583};
4584
4585static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
4586 struct io_uring_params *p)
4587{
Hristo Venev75b28af2019-08-26 17:23:46 +00004588 struct io_rings *rings;
4589 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004590
Hristo Venev75b28af2019-08-26 17:23:46 +00004591 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
4592 if (size == SIZE_MAX)
4593 return -EOVERFLOW;
4594
4595 rings = io_mem_alloc(size);
4596 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004597 return -ENOMEM;
4598
Hristo Venev75b28af2019-08-26 17:23:46 +00004599 ctx->rings = rings;
4600 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
4601 rings->sq_ring_mask = p->sq_entries - 1;
4602 rings->cq_ring_mask = p->cq_entries - 1;
4603 rings->sq_ring_entries = p->sq_entries;
4604 rings->cq_ring_entries = p->cq_entries;
4605 ctx->sq_mask = rings->sq_ring_mask;
4606 ctx->cq_mask = rings->cq_ring_mask;
4607 ctx->sq_entries = rings->sq_ring_entries;
4608 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004609
4610 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07004611 if (size == SIZE_MAX) {
4612 io_mem_free(ctx->rings);
4613 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004614 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07004615 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004616
4617 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07004618 if (!ctx->sq_sqes) {
4619 io_mem_free(ctx->rings);
4620 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004621 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07004622 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004623
Jens Axboe2b188cc2019-01-07 10:46:33 -07004624 return 0;
4625}
4626
4627/*
4628 * Allocate an anonymous fd, this is what constitutes the application
4629 * visible backing of an io_uring instance. The application mmaps this
4630 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
4631 * we have to tie this fd to a socket for file garbage collection purposes.
4632 */
4633static int io_uring_get_fd(struct io_ring_ctx *ctx)
4634{
4635 struct file *file;
4636 int ret;
4637
4638#if defined(CONFIG_UNIX)
4639 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
4640 &ctx->ring_sock);
4641 if (ret)
4642 return ret;
4643#endif
4644
4645 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
4646 if (ret < 0)
4647 goto err;
4648
4649 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
4650 O_RDWR | O_CLOEXEC);
4651 if (IS_ERR(file)) {
4652 put_unused_fd(ret);
4653 ret = PTR_ERR(file);
4654 goto err;
4655 }
4656
4657#if defined(CONFIG_UNIX)
4658 ctx->ring_sock->file = file;
Jens Axboe6b063142019-01-10 22:13:58 -07004659 ctx->ring_sock->sk->sk_user_data = ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004660#endif
4661 fd_install(ret, file);
4662 return ret;
4663err:
4664#if defined(CONFIG_UNIX)
4665 sock_release(ctx->ring_sock);
4666 ctx->ring_sock = NULL;
4667#endif
4668 return ret;
4669}
4670
4671static int io_uring_create(unsigned entries, struct io_uring_params *p)
4672{
4673 struct user_struct *user = NULL;
4674 struct io_ring_ctx *ctx;
4675 bool account_mem;
4676 int ret;
4677
4678 if (!entries || entries > IORING_MAX_ENTRIES)
4679 return -EINVAL;
4680
4681 /*
4682 * Use twice as many entries for the CQ ring. It's possible for the
4683 * application to drive a higher depth than the size of the SQ ring,
4684 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06004685 * some flexibility in overcommitting a bit. If the application has
4686 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
4687 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07004688 */
4689 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06004690 if (p->flags & IORING_SETUP_CQSIZE) {
4691 /*
4692 * If IORING_SETUP_CQSIZE is set, we do the same roundup
4693 * to a power-of-two, if it isn't already. We do NOT impose
4694 * any cq vs sq ring sizing.
4695 */
4696 if (p->cq_entries < p->sq_entries || p->cq_entries > IORING_MAX_CQ_ENTRIES)
4697 return -EINVAL;
4698 p->cq_entries = roundup_pow_of_two(p->cq_entries);
4699 } else {
4700 p->cq_entries = 2 * p->sq_entries;
4701 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004702
4703 user = get_uid(current_user());
4704 account_mem = !capable(CAP_IPC_LOCK);
4705
4706 if (account_mem) {
4707 ret = io_account_mem(user,
4708 ring_pages(p->sq_entries, p->cq_entries));
4709 if (ret) {
4710 free_uid(user);
4711 return ret;
4712 }
4713 }
4714
4715 ctx = io_ring_ctx_alloc(p);
4716 if (!ctx) {
4717 if (account_mem)
4718 io_unaccount_mem(user, ring_pages(p->sq_entries,
4719 p->cq_entries));
4720 free_uid(user);
4721 return -ENOMEM;
4722 }
4723 ctx->compat = in_compat_syscall();
4724 ctx->account_mem = account_mem;
4725 ctx->user = user;
Jens Axboe181e4482019-11-25 08:52:30 -07004726 ctx->creds = prepare_creds();
Jens Axboe2b188cc2019-01-07 10:46:33 -07004727
4728 ret = io_allocate_scq_urings(ctx, p);
4729 if (ret)
4730 goto err;
4731
Jens Axboe6c271ce2019-01-10 11:22:30 -07004732 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004733 if (ret)
4734 goto err;
4735
Jens Axboe2b188cc2019-01-07 10:46:33 -07004736 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00004737 p->sq_off.head = offsetof(struct io_rings, sq.head);
4738 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
4739 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
4740 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
4741 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
4742 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
4743 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004744
4745 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00004746 p->cq_off.head = offsetof(struct io_rings, cq.head);
4747 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
4748 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
4749 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
4750 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
4751 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06004752
Jens Axboe044c1ab2019-10-28 09:15:33 -06004753 /*
4754 * Install ring fd as the very last thing, so we don't risk someone
4755 * having closed it before we finish setup
4756 */
4757 ret = io_uring_get_fd(ctx);
4758 if (ret < 0)
4759 goto err;
4760
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004761 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004762 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004763 return ret;
4764err:
4765 io_ring_ctx_wait_and_kill(ctx);
4766 return ret;
4767}
4768
4769/*
4770 * Sets up an aio uring context, and returns the fd. Applications asks for a
4771 * ring size, we return the actual sq/cq ring sizes (among other things) in the
4772 * params structure passed in.
4773 */
4774static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
4775{
4776 struct io_uring_params p;
4777 long ret;
4778 int i;
4779
4780 if (copy_from_user(&p, params, sizeof(p)))
4781 return -EFAULT;
4782 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
4783 if (p.resv[i])
4784 return -EINVAL;
4785 }
4786
Jens Axboe6c271ce2019-01-10 11:22:30 -07004787 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe33a107f2019-10-04 12:10:03 -06004788 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE))
Jens Axboe2b188cc2019-01-07 10:46:33 -07004789 return -EINVAL;
4790
4791 ret = io_uring_create(entries, &p);
4792 if (ret < 0)
4793 return ret;
4794
4795 if (copy_to_user(params, &p, sizeof(p)))
4796 return -EFAULT;
4797
4798 return ret;
4799}
4800
4801SYSCALL_DEFINE2(io_uring_setup, u32, entries,
4802 struct io_uring_params __user *, params)
4803{
4804 return io_uring_setup(entries, params);
4805}
4806
Jens Axboeedafcce2019-01-09 09:16:05 -07004807static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
4808 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06004809 __releases(ctx->uring_lock)
4810 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07004811{
4812 int ret;
4813
Jens Axboe35fa71a2019-04-22 10:23:23 -06004814 /*
4815 * We're inside the ring mutex, if the ref is already dying, then
4816 * someone else killed the ctx or is already going through
4817 * io_uring_register().
4818 */
4819 if (percpu_ref_is_dying(&ctx->refs))
4820 return -ENXIO;
4821
Jens Axboeedafcce2019-01-09 09:16:05 -07004822 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06004823
4824 /*
4825 * Drop uring mutex before waiting for references to exit. If another
4826 * thread is currently inside io_uring_enter() it might need to grab
4827 * the uring_lock to make progress. If we hold it here across the drain
4828 * wait, then we can deadlock. It's safe to drop the mutex here, since
4829 * no new references will come in after we've killed the percpu ref.
4830 */
4831 mutex_unlock(&ctx->uring_lock);
Jens Axboe206aefd2019-11-07 18:27:42 -07004832 wait_for_completion(&ctx->completions[0]);
Jens Axboeb19062a2019-04-15 10:49:38 -06004833 mutex_lock(&ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07004834
4835 switch (opcode) {
4836 case IORING_REGISTER_BUFFERS:
4837 ret = io_sqe_buffer_register(ctx, arg, nr_args);
4838 break;
4839 case IORING_UNREGISTER_BUFFERS:
4840 ret = -EINVAL;
4841 if (arg || nr_args)
4842 break;
4843 ret = io_sqe_buffer_unregister(ctx);
4844 break;
Jens Axboe6b063142019-01-10 22:13:58 -07004845 case IORING_REGISTER_FILES:
4846 ret = io_sqe_files_register(ctx, arg, nr_args);
4847 break;
4848 case IORING_UNREGISTER_FILES:
4849 ret = -EINVAL;
4850 if (arg || nr_args)
4851 break;
4852 ret = io_sqe_files_unregister(ctx);
4853 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06004854 case IORING_REGISTER_FILES_UPDATE:
4855 ret = io_sqe_files_update(ctx, arg, nr_args);
4856 break;
Jens Axboe9b402842019-04-11 11:45:41 -06004857 case IORING_REGISTER_EVENTFD:
4858 ret = -EINVAL;
4859 if (nr_args != 1)
4860 break;
4861 ret = io_eventfd_register(ctx, arg);
4862 break;
4863 case IORING_UNREGISTER_EVENTFD:
4864 ret = -EINVAL;
4865 if (arg || nr_args)
4866 break;
4867 ret = io_eventfd_unregister(ctx);
4868 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07004869 default:
4870 ret = -EINVAL;
4871 break;
4872 }
4873
4874 /* bring the ctx back to life */
Jens Axboe206aefd2019-11-07 18:27:42 -07004875 reinit_completion(&ctx->completions[0]);
Jens Axboeedafcce2019-01-09 09:16:05 -07004876 percpu_ref_reinit(&ctx->refs);
4877 return ret;
4878}
4879
4880SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
4881 void __user *, arg, unsigned int, nr_args)
4882{
4883 struct io_ring_ctx *ctx;
4884 long ret = -EBADF;
4885 struct fd f;
4886
4887 f = fdget(fd);
4888 if (!f.file)
4889 return -EBADF;
4890
4891 ret = -EOPNOTSUPP;
4892 if (f.file->f_op != &io_uring_fops)
4893 goto out_fput;
4894
4895 ctx = f.file->private_data;
4896
4897 mutex_lock(&ctx->uring_lock);
4898 ret = __io_uring_register(ctx, opcode, arg, nr_args);
4899 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004900 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
4901 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07004902out_fput:
4903 fdput(f);
4904 return ret;
4905}
4906
Jens Axboe2b188cc2019-01-07 10:46:33 -07004907static int __init io_uring_init(void)
4908{
4909 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
4910 return 0;
4911};
4912__initcall(io_uring_init);