blob: 7412fdefa35a034dd3008fb0c9dd2b909758c0df [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;
312};
313
Jens Axboe5262f562019-09-17 12:26:57 -0600314struct io_timeout {
315 struct file *file;
Jens Axboead8a48a2019-11-15 08:49:11 -0700316 struct io_timeout_data *data;
Jens Axboe5262f562019-09-17 12:26:57 -0600317};
318
Jens Axboe09bb8392019-03-13 12:39:28 -0600319/*
320 * NOTE! Each of the iocb union members has the file pointer
321 * as the first entry in their struct definition. So you can
322 * access the file pointer through any of the sub-structs,
323 * or directly as just 'ki_filp' in this struct.
324 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700325struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700326 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600327 struct file *file;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700328 struct kiocb rw;
329 struct io_poll_iocb poll;
Jens Axboe5262f562019-09-17 12:26:57 -0600330 struct io_timeout timeout;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700331 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700332
333 struct sqe_submit submit;
334
335 struct io_ring_ctx *ctx;
Jens Axboeeac406c2019-11-14 12:09:58 -0700336 union {
337 struct list_head list;
338 struct rb_node rb_node;
339 };
Jens Axboe9e645e112019-05-10 16:07:28 -0600340 struct list_head link_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700341 unsigned int flags;
Jens Axboec16361c2019-01-17 08:39:48 -0700342 refcount_t refs;
Stefan Bühler8449eed2019-04-27 20:34:19 +0200343#define REQ_F_NOWAIT 1 /* must not punt to workers */
Jens Axboedef596e2019-01-09 08:59:42 -0700344#define REQ_F_IOPOLL_COMPLETED 2 /* polled IO has completed */
Jens Axboe6b063142019-01-10 22:13:58 -0700345#define REQ_F_FIXED_FILE 4 /* ctx owns file */
Jens Axboe4d7dd462019-11-20 13:03:52 -0700346#define REQ_F_LINK_NEXT 8 /* already grabbed next link */
Stefan Bühlere2033e32019-05-11 19:08:01 +0200347#define REQ_F_IO_DRAIN 16 /* drain existing IO first */
348#define REQ_F_IO_DRAINED 32 /* drain done */
Jens Axboe9e645e112019-05-10 16:07:28 -0600349#define REQ_F_LINK 64 /* linked sqes */
Jens Axboe2665abf2019-11-05 12:40:47 -0700350#define REQ_F_LINK_TIMEOUT 128 /* has linked timeout */
Zhengyuan Liuf7b76ac2019-07-16 23:26:14 +0800351#define REQ_F_FAIL_LINK 256 /* fail rest of links */
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +0300352#define REQ_F_DRAIN_LINK 512 /* link should be fully drained */
Jens Axboe5262f562019-09-17 12:26:57 -0600353#define REQ_F_TIMEOUT 1024 /* timeout request */
Jens Axboe491381ce2019-10-17 09:20:46 -0600354#define REQ_F_ISREG 2048 /* regular file */
355#define REQ_F_MUST_PUNT 4096 /* must be punted even for NONBLOCK */
Jens Axboe93bd25b2019-11-11 23:34:31 -0700356#define REQ_F_TIMEOUT_NOSEQ 8192 /* no timeout sequence */
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800357#define REQ_F_INFLIGHT 16384 /* on inflight list */
358#define REQ_F_COMP_LOCKED 32768 /* completion under lock */
Jens Axboe94ae5e72019-11-14 19:39:52 -0700359#define REQ_F_FREE_SQE 65536 /* free sqe if not async queued */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700360 u64 user_data;
Jens Axboe9e645e112019-05-10 16:07:28 -0600361 u32 result;
Jens Axboede0617e2019-04-06 21:51:27 -0600362 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700363
Jens Axboefcb323c2019-10-24 12:39:47 -0600364 struct list_head inflight_entry;
365
Jens Axboe561fb042019-10-24 07:25:42 -0600366 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700367};
368
369#define IO_PLUG_THRESHOLD 2
Jens Axboedef596e2019-01-09 08:59:42 -0700370#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700371
Jens Axboe9a56a232019-01-09 09:06:50 -0700372struct io_submit_state {
373 struct blk_plug plug;
374
375 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700376 * io_kiocb alloc cache
377 */
378 void *reqs[IO_IOPOLL_BATCH];
379 unsigned int free_reqs;
380 unsigned int cur_req;
381
382 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700383 * File reference cache
384 */
385 struct file *file;
386 unsigned int fd;
387 unsigned int has_refs;
388 unsigned int used_refs;
389 unsigned int ios_left;
390};
391
Jens Axboe561fb042019-10-24 07:25:42 -0600392static void io_wq_submit_work(struct io_wq_work **workptr);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700393static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liu4fe2c962019-09-09 20:50:40 +0800394static void __io_free_req(struct io_kiocb *req);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800395static void io_put_req(struct io_kiocb *req);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700396static void io_double_put_req(struct io_kiocb *req);
Jens Axboe978db572019-11-14 22:39:04 -0700397static void __io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700398static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
399static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboede0617e2019-04-06 21:51:27 -0600400
Jens Axboe2b188cc2019-01-07 10:46:33 -0700401static struct kmem_cache *req_cachep;
402
403static const struct file_operations io_uring_fops;
404
405struct sock *io_uring_get_socket(struct file *file)
406{
407#if defined(CONFIG_UNIX)
408 if (file->f_op == &io_uring_fops) {
409 struct io_ring_ctx *ctx = file->private_data;
410
411 return ctx->ring_sock->sk;
412 }
413#endif
414 return NULL;
415}
416EXPORT_SYMBOL(io_uring_get_socket);
417
418static void io_ring_ctx_ref_free(struct percpu_ref *ref)
419{
420 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
421
Jens Axboe206aefd2019-11-07 18:27:42 -0700422 complete(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700423}
424
425static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
426{
427 struct io_ring_ctx *ctx;
428
429 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
430 if (!ctx)
431 return NULL;
432
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700433 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
434 if (!ctx->fallback_req)
435 goto err;
436
Jens Axboe206aefd2019-11-07 18:27:42 -0700437 ctx->completions = kmalloc(2 * sizeof(struct completion), GFP_KERNEL);
438 if (!ctx->completions)
439 goto err;
440
Roman Gushchin21482892019-05-07 10:01:48 -0700441 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -0700442 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
443 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700444
445 ctx->flags = p->flags;
446 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700447 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe206aefd2019-11-07 18:27:42 -0700448 init_completion(&ctx->completions[0]);
449 init_completion(&ctx->completions[1]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700450 mutex_init(&ctx->uring_lock);
451 init_waitqueue_head(&ctx->wait);
452 spin_lock_init(&ctx->completion_lock);
Jens Axboedef596e2019-01-09 08:59:42 -0700453 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboeeac406c2019-11-14 12:09:58 -0700454 ctx->cancel_tree = RB_ROOT;
Jens Axboede0617e2019-04-06 21:51:27 -0600455 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -0600456 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -0600457 init_waitqueue_head(&ctx->inflight_wait);
458 spin_lock_init(&ctx->inflight_lock);
459 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700460 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -0700461err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700462 if (ctx->fallback_req)
463 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe206aefd2019-11-07 18:27:42 -0700464 kfree(ctx->completions);
465 kfree(ctx);
466 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700467}
468
Bob Liu9d858b22019-11-13 18:06:25 +0800469static inline bool __req_need_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -0600470{
Jackie Liua197f662019-11-08 08:09:12 -0700471 struct io_ring_ctx *ctx = req->ctx;
472
Jens Axboe498ccd92019-10-25 10:04:25 -0600473 return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped
474 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboede0617e2019-04-06 21:51:27 -0600475}
476
Bob Liu9d858b22019-11-13 18:06:25 +0800477static inline bool req_need_defer(struct io_kiocb *req)
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600478{
Bob Liu9d858b22019-11-13 18:06:25 +0800479 if ((req->flags & (REQ_F_IO_DRAIN|REQ_F_IO_DRAINED)) == REQ_F_IO_DRAIN)
480 return __req_need_defer(req);
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600481
Bob Liu9d858b22019-11-13 18:06:25 +0800482 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600483}
484
485static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -0600486{
487 struct io_kiocb *req;
488
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600489 req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list);
Bob Liu9d858b22019-11-13 18:06:25 +0800490 if (req && !req_need_defer(req)) {
Jens Axboede0617e2019-04-06 21:51:27 -0600491 list_del_init(&req->list);
492 return req;
493 }
494
495 return NULL;
496}
497
Jens Axboe5262f562019-09-17 12:26:57 -0600498static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx)
499{
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600500 struct io_kiocb *req;
501
502 req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list);
Jens Axboe93bd25b2019-11-11 23:34:31 -0700503 if (req) {
504 if (req->flags & REQ_F_TIMEOUT_NOSEQ)
505 return NULL;
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800506 if (!__req_need_defer(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -0700507 list_del_init(&req->list);
508 return req;
509 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600510 }
511
512 return NULL;
Jens Axboe5262f562019-09-17 12:26:57 -0600513}
514
Jens Axboede0617e2019-04-06 21:51:27 -0600515static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700516{
Hristo Venev75b28af2019-08-26 17:23:46 +0000517 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700518
Hristo Venev75b28af2019-08-26 17:23:46 +0000519 if (ctx->cached_cq_tail != READ_ONCE(rings->cq.tail)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700520 /* order cqe stores with ring update */
Hristo Venev75b28af2019-08-26 17:23:46 +0000521 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700522
Jens Axboe2b188cc2019-01-07 10:46:33 -0700523 if (wq_has_sleeper(&ctx->cq_wait)) {
524 wake_up_interruptible(&ctx->cq_wait);
525 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
526 }
527 }
528}
529
Jens Axboe561fb042019-10-24 07:25:42 -0600530static inline bool io_sqe_needs_user(const struct io_uring_sqe *sqe)
Jens Axboe18d9be12019-09-10 09:13:05 -0600531{
Jens Axboe561fb042019-10-24 07:25:42 -0600532 u8 opcode = READ_ONCE(sqe->opcode);
533
534 return !(opcode == IORING_OP_READ_FIXED ||
535 opcode == IORING_OP_WRITE_FIXED);
536}
537
Jens Axboe94ae5e72019-11-14 19:39:52 -0700538static inline bool io_prep_async_work(struct io_kiocb *req,
539 struct io_kiocb **link)
Jens Axboe561fb042019-10-24 07:25:42 -0600540{
541 bool do_hashed = false;
Jens Axboe54a91f32019-09-10 09:15:04 -0600542
Jens Axboe6cc47d12019-09-18 11:18:23 -0600543 if (req->submit.sqe) {
544 switch (req->submit.sqe->opcode) {
545 case IORING_OP_WRITEV:
546 case IORING_OP_WRITE_FIXED:
Jens Axboe561fb042019-10-24 07:25:42 -0600547 do_hashed = true;
Jens Axboe5f8fd2d2019-11-07 10:57:36 -0700548 /* fall-through */
549 case IORING_OP_READV:
550 case IORING_OP_READ_FIXED:
551 case IORING_OP_SENDMSG:
552 case IORING_OP_RECVMSG:
553 case IORING_OP_ACCEPT:
554 case IORING_OP_POLL_ADD:
Jens Axboef8e85cf2019-11-23 14:24:24 -0700555 case IORING_OP_CONNECT:
Jens Axboe5f8fd2d2019-11-07 10:57:36 -0700556 /*
557 * We know REQ_F_ISREG is not set on some of these
558 * opcodes, but this enables us to keep the check in
559 * just one place.
560 */
561 if (!(req->flags & REQ_F_ISREG))
562 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe6cc47d12019-09-18 11:18:23 -0600563 break;
564 }
Jens Axboe561fb042019-10-24 07:25:42 -0600565 if (io_sqe_needs_user(req->submit.sqe))
566 req->work.flags |= IO_WQ_WORK_NEEDS_USER;
Jens Axboe54a91f32019-09-10 09:15:04 -0600567 }
568
Jens Axboe94ae5e72019-11-14 19:39:52 -0700569 *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -0600570 return do_hashed;
571}
572
Jackie Liua197f662019-11-08 08:09:12 -0700573static inline void io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -0600574{
Jackie Liua197f662019-11-08 08:09:12 -0700575 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe94ae5e72019-11-14 19:39:52 -0700576 struct io_kiocb *link;
577 bool do_hashed;
578
579 do_hashed = io_prep_async_work(req, &link);
Jens Axboe561fb042019-10-24 07:25:42 -0600580
581 trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work,
582 req->flags);
583 if (!do_hashed) {
584 io_wq_enqueue(ctx->io_wq, &req->work);
585 } else {
586 io_wq_enqueue_hashed(ctx->io_wq, &req->work,
587 file_inode(req->file));
588 }
Jens Axboe94ae5e72019-11-14 19:39:52 -0700589
590 if (link)
591 io_queue_linked_timeout(link);
Jens Axboe18d9be12019-09-10 09:13:05 -0600592}
593
Jens Axboe5262f562019-09-17 12:26:57 -0600594static void io_kill_timeout(struct io_kiocb *req)
595{
596 int ret;
597
Jens Axboead8a48a2019-11-15 08:49:11 -0700598 ret = hrtimer_try_to_cancel(&req->timeout.data->timer);
Jens Axboe5262f562019-09-17 12:26:57 -0600599 if (ret != -1) {
600 atomic_inc(&req->ctx->cq_timeouts);
Jens Axboe842f9612019-10-29 12:34:10 -0600601 list_del_init(&req->list);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700602 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800603 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -0600604 }
605}
606
607static void io_kill_timeouts(struct io_ring_ctx *ctx)
608{
609 struct io_kiocb *req, *tmp;
610
611 spin_lock_irq(&ctx->completion_lock);
612 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
613 io_kill_timeout(req);
614 spin_unlock_irq(&ctx->completion_lock);
615}
616
Jens Axboede0617e2019-04-06 21:51:27 -0600617static void io_commit_cqring(struct io_ring_ctx *ctx)
618{
619 struct io_kiocb *req;
620
Jens Axboe5262f562019-09-17 12:26:57 -0600621 while ((req = io_get_timeout_req(ctx)) != NULL)
622 io_kill_timeout(req);
623
Jens Axboede0617e2019-04-06 21:51:27 -0600624 __io_commit_cqring(ctx);
625
626 while ((req = io_get_deferred_req(ctx)) != NULL) {
627 req->flags |= REQ_F_IO_DRAINED;
Jackie Liua197f662019-11-08 08:09:12 -0700628 io_queue_async_work(req);
Jens Axboede0617e2019-04-06 21:51:27 -0600629 }
630}
631
Jens Axboe2b188cc2019-01-07 10:46:33 -0700632static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
633{
Hristo Venev75b28af2019-08-26 17:23:46 +0000634 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700635 unsigned tail;
636
637 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +0200638 /*
639 * writes to the cq entry need to come after reading head; the
640 * control dependency is enough as we're using WRITE_ONCE to
641 * fill the cq entry
642 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000643 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700644 return NULL;
645
646 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +0000647 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -0700648}
649
Jens Axboe8c838782019-03-12 15:48:16 -0600650static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
651{
652 if (waitqueue_active(&ctx->wait))
653 wake_up(&ctx->wait);
654 if (waitqueue_active(&ctx->sqo_wait))
655 wake_up(&ctx->sqo_wait);
Jens Axboe9b402842019-04-11 11:45:41 -0600656 if (ctx->cq_ev_fd)
657 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -0600658}
659
Jens Axboec4a2ed72019-11-21 21:01:26 -0700660/* Returns true if there are no backlogged entries after the flush */
661static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700662{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700663 struct io_rings *rings = ctx->rings;
664 struct io_uring_cqe *cqe;
665 struct io_kiocb *req;
666 unsigned long flags;
667 LIST_HEAD(list);
668
669 if (!force) {
670 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -0700671 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700672 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
673 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -0700674 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700675 }
676
677 spin_lock_irqsave(&ctx->completion_lock, flags);
678
679 /* if force is set, the ring is going away. always drop after that */
680 if (force)
681 ctx->cq_overflow_flushed = true;
682
Jens Axboec4a2ed72019-11-21 21:01:26 -0700683 cqe = NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700684 while (!list_empty(&ctx->cq_overflow_list)) {
685 cqe = io_get_cqring(ctx);
686 if (!cqe && !force)
687 break;
688
689 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
690 list);
691 list_move(&req->list, &list);
692 if (cqe) {
693 WRITE_ONCE(cqe->user_data, req->user_data);
694 WRITE_ONCE(cqe->res, req->result);
695 WRITE_ONCE(cqe->flags, 0);
696 } else {
697 WRITE_ONCE(ctx->rings->cq_overflow,
698 atomic_inc_return(&ctx->cached_cq_overflow));
699 }
700 }
701
702 io_commit_cqring(ctx);
703 spin_unlock_irqrestore(&ctx->completion_lock, flags);
704 io_cqring_ev_posted(ctx);
705
706 while (!list_empty(&list)) {
707 req = list_first_entry(&list, struct io_kiocb, list);
708 list_del(&req->list);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800709 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700710 }
Jens Axboec4a2ed72019-11-21 21:01:26 -0700711
712 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700713}
714
Jens Axboe78e19bb2019-11-06 15:21:34 -0700715static void io_cqring_fill_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700716{
Jens Axboe78e19bb2019-11-06 15:21:34 -0700717 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700718 struct io_uring_cqe *cqe;
719
Jens Axboe78e19bb2019-11-06 15:21:34 -0700720 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -0700721
Jens Axboe2b188cc2019-01-07 10:46:33 -0700722 /*
723 * If we can't get a cq entry, userspace overflowed the
724 * submission (by quite a lot). Increment the overflow count in
725 * the ring.
726 */
727 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700728 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -0700729 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700730 WRITE_ONCE(cqe->res, res);
731 WRITE_ONCE(cqe->flags, 0);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700732 } else if (ctx->cq_overflow_flushed) {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700733 WRITE_ONCE(ctx->rings->cq_overflow,
734 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700735 } else {
736 refcount_inc(&req->refs);
737 req->result = res;
738 list_add_tail(&req->list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700739 }
740}
741
Jens Axboe78e19bb2019-11-06 15:21:34 -0700742static void io_cqring_add_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700743{
Jens Axboe78e19bb2019-11-06 15:21:34 -0700744 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700745 unsigned long flags;
746
747 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700748 io_cqring_fill_event(req, res);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700749 io_commit_cqring(ctx);
750 spin_unlock_irqrestore(&ctx->completion_lock, flags);
751
Jens Axboe8c838782019-03-12 15:48:16 -0600752 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700753}
754
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700755static inline bool io_is_fallback_req(struct io_kiocb *req)
756{
757 return req == (struct io_kiocb *)
758 ((unsigned long) req->ctx->fallback_req & ~1UL);
759}
760
761static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
762{
763 struct io_kiocb *req;
764
765 req = ctx->fallback_req;
766 if (!test_and_set_bit_lock(0, (unsigned long *) ctx->fallback_req))
767 return req;
768
769 return NULL;
770}
771
Jens Axboe2579f912019-01-09 09:10:43 -0700772static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
773 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700774{
Jens Axboefd6fab22019-03-14 16:30:06 -0600775 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700776 struct io_kiocb *req;
777
778 if (!percpu_ref_tryget(&ctx->refs))
779 return NULL;
780
Jens Axboe2579f912019-01-09 09:10:43 -0700781 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -0600782 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -0700783 if (unlikely(!req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700784 goto fallback;
Jens Axboe2579f912019-01-09 09:10:43 -0700785 } else if (!state->free_reqs) {
786 size_t sz;
787 int ret;
788
789 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -0600790 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
791
792 /*
793 * Bulk alloc is all-or-nothing. If we fail to get a batch,
794 * retry single alloc to be on the safe side.
795 */
796 if (unlikely(ret <= 0)) {
797 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
798 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700799 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -0600800 ret = 1;
801 }
Jens Axboe2579f912019-01-09 09:10:43 -0700802 state->free_reqs = ret - 1;
803 state->cur_req = 1;
804 req = state->reqs[0];
805 } else {
806 req = state->reqs[state->cur_req];
807 state->free_reqs--;
808 state->cur_req++;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700809 }
810
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700811got_it:
Jens Axboe60c112b2019-06-21 10:20:18 -0600812 req->file = NULL;
Jens Axboe2579f912019-01-09 09:10:43 -0700813 req->ctx = ctx;
814 req->flags = 0;
Jens Axboee65ef562019-03-12 10:16:44 -0600815 /* one is dropped after submission, the other at completion */
816 refcount_set(&req->refs, 2);
Jens Axboe9e645e112019-05-10 16:07:28 -0600817 req->result = 0;
Jens Axboe561fb042019-10-24 07:25:42 -0600818 INIT_IO_WORK(&req->work, io_wq_submit_work);
Jens Axboe2579f912019-01-09 09:10:43 -0700819 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700820fallback:
821 req = io_get_fallback_req(ctx);
822 if (req)
823 goto got_it;
Pavel Begunkov6805b322019-10-08 02:18:42 +0300824 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700825 return NULL;
826}
827
Jens Axboedef596e2019-01-09 08:59:42 -0700828static void io_free_req_many(struct io_ring_ctx *ctx, void **reqs, int *nr)
829{
830 if (*nr) {
831 kmem_cache_free_bulk(req_cachep, *nr, reqs);
Pavel Begunkov6805b322019-10-08 02:18:42 +0300832 percpu_ref_put_many(&ctx->refs, *nr);
Jens Axboedef596e2019-01-09 08:59:42 -0700833 *nr = 0;
834 }
835}
836
Jens Axboe9e645e112019-05-10 16:07:28 -0600837static void __io_free_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700838{
Jens Axboefcb323c2019-10-24 12:39:47 -0600839 struct io_ring_ctx *ctx = req->ctx;
840
Pavel Begunkovbbad27b2019-11-19 23:32:47 +0300841 if (req->flags & REQ_F_FREE_SQE)
842 kfree(req->submit.sqe);
Jens Axboe09bb8392019-03-13 12:39:28 -0600843 if (req->file && !(req->flags & REQ_F_FIXED_FILE))
844 fput(req->file);
Jens Axboefcb323c2019-10-24 12:39:47 -0600845 if (req->flags & REQ_F_INFLIGHT) {
846 unsigned long flags;
847
848 spin_lock_irqsave(&ctx->inflight_lock, flags);
849 list_del(&req->inflight_entry);
850 if (waitqueue_active(&ctx->inflight_wait))
851 wake_up(&ctx->inflight_wait);
852 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
853 }
Jens Axboead8a48a2019-11-15 08:49:11 -0700854 if (req->flags & REQ_F_TIMEOUT)
855 kfree(req->timeout.data);
Jens Axboefcb323c2019-10-24 12:39:47 -0600856 percpu_ref_put(&ctx->refs);
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700857 if (likely(!io_is_fallback_req(req)))
858 kmem_cache_free(req_cachep, req);
859 else
860 clear_bit_unlock(0, (unsigned long *) ctx->fallback_req);
Jens Axboee65ef562019-03-12 10:16:44 -0600861}
862
Jackie Liua197f662019-11-08 08:09:12 -0700863static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -0600864{
Jackie Liua197f662019-11-08 08:09:12 -0700865 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -0700866 int ret;
867
Jens Axboead8a48a2019-11-15 08:49:11 -0700868 ret = hrtimer_try_to_cancel(&req->timeout.data->timer);
Jens Axboe2665abf2019-11-05 12:40:47 -0700869 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -0700870 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -0700871 io_commit_cqring(ctx);
872 req->flags &= ~REQ_F_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +0800873 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -0700874 return true;
875 }
876
877 return false;
878}
879
Jens Axboeba816ad2019-09-28 11:36:45 -0600880static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -0600881{
Jens Axboe2665abf2019-11-05 12:40:47 -0700882 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -0600883 struct io_kiocb *nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -0700884 bool wake_ev = false;
Jens Axboe9e645e112019-05-10 16:07:28 -0600885
Jens Axboe4d7dd462019-11-20 13:03:52 -0700886 /* Already got next link */
887 if (req->flags & REQ_F_LINK_NEXT)
888 return;
889
Jens Axboe9e645e112019-05-10 16:07:28 -0600890 /*
891 * The list should never be empty when we are called here. But could
892 * potentially happen if the chain is messed up, check to be on the
893 * safe side.
894 */
895 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, list);
Jens Axboe2665abf2019-11-05 12:40:47 -0700896 while (nxt) {
Jens Axboe76a46e02019-11-10 23:34:16 -0700897 list_del_init(&nxt->list);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700898
899 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
900 (nxt->flags & REQ_F_TIMEOUT)) {
901 wake_ev |= io_link_cancel_timeout(nxt);
902 nxt = list_first_entry_or_null(&req->link_list,
903 struct io_kiocb, list);
904 req->flags &= ~REQ_F_LINK_TIMEOUT;
905 continue;
906 }
Jens Axboe9e645e112019-05-10 16:07:28 -0600907 if (!list_empty(&req->link_list)) {
908 INIT_LIST_HEAD(&nxt->link_list);
909 list_splice(&req->link_list, &nxt->link_list);
910 nxt->flags |= REQ_F_LINK;
911 }
912
Pavel Begunkovb18fdf72019-11-21 23:21:02 +0300913 *nxtptr = nxt;
Jens Axboe94ae5e72019-11-14 19:39:52 -0700914 break;
Jens Axboe9e645e112019-05-10 16:07:28 -0600915 }
Jens Axboe2665abf2019-11-05 12:40:47 -0700916
Jens Axboe4d7dd462019-11-20 13:03:52 -0700917 req->flags |= REQ_F_LINK_NEXT;
Jens Axboe2665abf2019-11-05 12:40:47 -0700918 if (wake_ev)
919 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -0600920}
921
922/*
923 * Called if REQ_F_LINK is set, and we fail the head request
924 */
925static void io_fail_links(struct io_kiocb *req)
926{
Jens Axboe2665abf2019-11-05 12:40:47 -0700927 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -0600928 struct io_kiocb *link;
Jens Axboe2665abf2019-11-05 12:40:47 -0700929 unsigned long flags;
930
931 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -0600932
933 while (!list_empty(&req->link_list)) {
934 link = list_first_entry(&req->link_list, struct io_kiocb, list);
Jens Axboe2665abf2019-11-05 12:40:47 -0700935 list_del_init(&link->list);
Jens Axboe9e645e112019-05-10 16:07:28 -0600936
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +0200937 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -0700938
939 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
940 link->submit.sqe->opcode == IORING_OP_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -0700941 io_link_cancel_timeout(link);
Jens Axboe2665abf2019-11-05 12:40:47 -0700942 } else {
Jens Axboe78e19bb2019-11-06 15:21:34 -0700943 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe978db572019-11-14 22:39:04 -0700944 __io_double_put_req(link);
Jens Axboe2665abf2019-11-05 12:40:47 -0700945 }
Jens Axboe5d960722019-11-19 15:31:28 -0700946 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -0600947 }
Jens Axboe2665abf2019-11-05 12:40:47 -0700948
949 io_commit_cqring(ctx);
950 spin_unlock_irqrestore(&ctx->completion_lock, flags);
951 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -0600952}
953
Jens Axboe4d7dd462019-11-20 13:03:52 -0700954static void io_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -0600955{
Jens Axboe4d7dd462019-11-20 13:03:52 -0700956 if (likely(!(req->flags & REQ_F_LINK)))
Jens Axboe2665abf2019-11-05 12:40:47 -0700957 return;
Jens Axboe2665abf2019-11-05 12:40:47 -0700958
Jens Axboe9e645e112019-05-10 16:07:28 -0600959 /*
960 * If LINK is set, we have dependent requests in this chain. If we
961 * didn't fail this request, queue the first one up, moving any other
962 * dependencies to the next request. In case of failure, fail the rest
963 * of the chain.
964 */
Jens Axboe2665abf2019-11-05 12:40:47 -0700965 if (req->flags & REQ_F_FAIL_LINK) {
966 io_fail_links(req);
Jens Axboe7c9e7f02019-11-12 08:15:53 -0700967 } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) ==
968 REQ_F_LINK_TIMEOUT) {
Jens Axboe2665abf2019-11-05 12:40:47 -0700969 struct io_ring_ctx *ctx = req->ctx;
970 unsigned long flags;
971
972 /*
973 * If this is a timeout link, we could be racing with the
974 * timeout timer. Grab the completion lock for this case to
Jens Axboe7c9e7f02019-11-12 08:15:53 -0700975 * protect against that.
Jens Axboe2665abf2019-11-05 12:40:47 -0700976 */
977 spin_lock_irqsave(&ctx->completion_lock, flags);
978 io_req_link_next(req, nxt);
979 spin_unlock_irqrestore(&ctx->completion_lock, flags);
980 } else {
981 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -0600982 }
Jens Axboe4d7dd462019-11-20 13:03:52 -0700983}
Jens Axboe9e645e112019-05-10 16:07:28 -0600984
Jackie Liuc69f8db2019-11-09 11:00:08 +0800985static void io_free_req(struct io_kiocb *req)
986{
Pavel Begunkov944e58b2019-11-21 23:21:01 +0300987 struct io_kiocb *nxt = NULL;
988
989 io_req_find_next(req, &nxt);
Pavel Begunkov70cf9f32019-11-21 23:21:00 +0300990 __io_free_req(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +0300991
992 if (nxt)
993 io_queue_async_work(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +0800994}
995
Jens Axboeba816ad2019-09-28 11:36:45 -0600996/*
997 * Drop reference to request, return next in chain (if there is one) if this
998 * was the last reference to this request.
999 */
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001000__attribute__((nonnull))
Jackie Liuec9c02a2019-11-08 23:50:36 +08001001static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboee65ef562019-03-12 10:16:44 -06001002{
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001003 io_req_find_next(req, nxtptr);
Jens Axboe4d7dd462019-11-20 13:03:52 -07001004
Jens Axboee65ef562019-03-12 10:16:44 -06001005 if (refcount_dec_and_test(&req->refs))
Jens Axboe4d7dd462019-11-20 13:03:52 -07001006 __io_free_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001007}
1008
Jens Axboe2b188cc2019-01-07 10:46:33 -07001009static void io_put_req(struct io_kiocb *req)
1010{
Jens Axboedef596e2019-01-09 08:59:42 -07001011 if (refcount_dec_and_test(&req->refs))
1012 io_free_req(req);
1013}
1014
Jens Axboe978db572019-11-14 22:39:04 -07001015/*
1016 * Must only be used if we don't need to care about links, usually from
1017 * within the completion handling itself.
1018 */
1019static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06001020{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001021 /* drop both submit and complete references */
1022 if (refcount_sub_and_test(2, &req->refs))
1023 __io_free_req(req);
1024}
1025
Jens Axboe978db572019-11-14 22:39:04 -07001026static void io_double_put_req(struct io_kiocb *req)
1027{
1028 /* drop both submit and complete references */
1029 if (refcount_sub_and_test(2, &req->refs))
1030 io_free_req(req);
1031}
1032
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001033static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06001034{
Jens Axboe84f97dc2019-11-06 11:27:53 -07001035 struct io_rings *rings = ctx->rings;
1036
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001037 /*
1038 * noflush == true is from the waitqueue handler, just ensure we wake
1039 * up the task, and the next invocation will flush the entries. We
1040 * cannot safely to it from here.
1041 */
1042 if (noflush && !list_empty(&ctx->cq_overflow_list))
1043 return -1U;
1044
1045 io_cqring_overflow_flush(ctx, false);
1046
Jens Axboea3a0e432019-08-20 11:03:11 -06001047 /* See comment at the top of this file */
1048 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00001049 return READ_ONCE(rings->cq.tail) - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06001050}
1051
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03001052static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1053{
1054 struct io_rings *rings = ctx->rings;
1055
1056 /* make sure SQ entry isn't read before tail */
1057 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1058}
1059
Jens Axboedef596e2019-01-09 08:59:42 -07001060/*
1061 * Find and free completed poll iocbs
1062 */
1063static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
1064 struct list_head *done)
1065{
1066 void *reqs[IO_IOPOLL_BATCH];
1067 struct io_kiocb *req;
Jens Axboe09bb8392019-03-13 12:39:28 -06001068 int to_free;
Jens Axboedef596e2019-01-09 08:59:42 -07001069
Jens Axboe09bb8392019-03-13 12:39:28 -06001070 to_free = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001071 while (!list_empty(done)) {
1072 req = list_first_entry(done, struct io_kiocb, list);
1073 list_del(&req->list);
1074
Jens Axboe78e19bb2019-11-06 15:21:34 -07001075 io_cqring_fill_event(req, req->result);
Jens Axboedef596e2019-01-09 08:59:42 -07001076 (*nr_events)++;
1077
Jens Axboe09bb8392019-03-13 12:39:28 -06001078 if (refcount_dec_and_test(&req->refs)) {
1079 /* If we're not using fixed files, we have to pair the
1080 * completion part with the file put. Use regular
1081 * completions for those, only batch free for fixed
Jens Axboe9e645e112019-05-10 16:07:28 -06001082 * file and non-linked commands.
Jens Axboe09bb8392019-03-13 12:39:28 -06001083 */
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03001084 if (((req->flags &
1085 (REQ_F_FIXED_FILE|REQ_F_LINK|REQ_F_FREE_SQE)) ==
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001086 REQ_F_FIXED_FILE) && !io_is_fallback_req(req)) {
Jens Axboe09bb8392019-03-13 12:39:28 -06001087 reqs[to_free++] = req;
1088 if (to_free == ARRAY_SIZE(reqs))
1089 io_free_req_many(ctx, reqs, &to_free);
Jens Axboe6b063142019-01-10 22:13:58 -07001090 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001091 io_free_req(req);
Jens Axboe6b063142019-01-10 22:13:58 -07001092 }
Jens Axboe9a56a232019-01-09 09:06:50 -07001093 }
Jens Axboedef596e2019-01-09 08:59:42 -07001094 }
Jens Axboedef596e2019-01-09 08:59:42 -07001095
Jens Axboe09bb8392019-03-13 12:39:28 -06001096 io_commit_cqring(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07001097 io_free_req_many(ctx, reqs, &to_free);
1098}
1099
1100static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1101 long min)
1102{
1103 struct io_kiocb *req, *tmp;
1104 LIST_HEAD(done);
1105 bool spin;
1106 int ret;
1107
1108 /*
1109 * Only spin for completions if we don't have multiple devices hanging
1110 * off our complete list, and we're under the requested amount.
1111 */
1112 spin = !ctx->poll_multi_file && *nr_events < min;
1113
1114 ret = 0;
1115 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
1116 struct kiocb *kiocb = &req->rw;
1117
1118 /*
1119 * Move completed entries to our local list. If we find a
1120 * request that requires polling, break out and complete
1121 * the done list first, if we have entries there.
1122 */
1123 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
1124 list_move_tail(&req->list, &done);
1125 continue;
1126 }
1127 if (!list_empty(&done))
1128 break;
1129
1130 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1131 if (ret < 0)
1132 break;
1133
1134 if (ret && spin)
1135 spin = false;
1136 ret = 0;
1137 }
1138
1139 if (!list_empty(&done))
1140 io_iopoll_complete(ctx, nr_events, &done);
1141
1142 return ret;
1143}
1144
1145/*
1146 * Poll for a mininum of 'min' events. Note that if min == 0 we consider that a
1147 * non-spinning poll check - we'll still enter the driver poll loop, but only
1148 * as a non-spinning completion check.
1149 */
1150static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
1151 long min)
1152{
Jens Axboe08f54392019-08-21 22:19:11 -06001153 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07001154 int ret;
1155
1156 ret = io_do_iopoll(ctx, nr_events, min);
1157 if (ret < 0)
1158 return ret;
1159 if (!min || *nr_events >= min)
1160 return 0;
1161 }
1162
1163 return 1;
1164}
1165
1166/*
1167 * We can't just wait for polled events to come to us, we have to actively
1168 * find and complete them.
1169 */
1170static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
1171{
1172 if (!(ctx->flags & IORING_SETUP_IOPOLL))
1173 return;
1174
1175 mutex_lock(&ctx->uring_lock);
1176 while (!list_empty(&ctx->poll_list)) {
1177 unsigned int nr_events = 0;
1178
1179 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -06001180
1181 /*
1182 * Ensure we allow local-to-the-cpu processing to take place,
1183 * in this case we need to ensure that we reap all events.
1184 */
1185 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -07001186 }
1187 mutex_unlock(&ctx->uring_lock);
1188}
1189
Jens Axboe2b2ed972019-10-25 10:06:15 -06001190static int __io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1191 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07001192{
Jens Axboe2b2ed972019-10-25 10:06:15 -06001193 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001194
1195 do {
1196 int tmin = 0;
1197
Jens Axboe500f9fb2019-08-19 12:15:59 -06001198 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06001199 * Don't enter poll loop if we already have events pending.
1200 * If we do, we can potentially be spinning for commands that
1201 * already triggered a CQE (eg in error).
1202 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001203 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06001204 break;
1205
1206 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06001207 * If a submit got punted to a workqueue, we can have the
1208 * application entering polling for a command before it gets
1209 * issued. That app will hold the uring_lock for the duration
1210 * of the poll right here, so we need to take a breather every
1211 * now and then to ensure that the issue has a chance to add
1212 * the poll to the issued list. Otherwise we can spin here
1213 * forever, while the workqueue is stuck trying to acquire the
1214 * very same mutex.
1215 */
1216 if (!(++iters & 7)) {
1217 mutex_unlock(&ctx->uring_lock);
1218 mutex_lock(&ctx->uring_lock);
1219 }
1220
Jens Axboedef596e2019-01-09 08:59:42 -07001221 if (*nr_events < min)
1222 tmin = min - *nr_events;
1223
1224 ret = io_iopoll_getevents(ctx, nr_events, tmin);
1225 if (ret <= 0)
1226 break;
1227 ret = 0;
1228 } while (min && !*nr_events && !need_resched());
1229
Jens Axboe2b2ed972019-10-25 10:06:15 -06001230 return ret;
1231}
1232
1233static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1234 long min)
1235{
1236 int ret;
1237
1238 /*
1239 * We disallow the app entering submit/complete with polling, but we
1240 * still need to lock the ring to prevent racing with polled issue
1241 * that got punted to a workqueue.
1242 */
1243 mutex_lock(&ctx->uring_lock);
1244 ret = __io_iopoll_check(ctx, nr_events, min);
Jens Axboe500f9fb2019-08-19 12:15:59 -06001245 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001246 return ret;
1247}
1248
Jens Axboe491381ce2019-10-17 09:20:46 -06001249static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001250{
Jens Axboe491381ce2019-10-17 09:20:46 -06001251 /*
1252 * Tell lockdep we inherited freeze protection from submission
1253 * thread.
1254 */
1255 if (req->flags & REQ_F_ISREG) {
1256 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001257
Jens Axboe491381ce2019-10-17 09:20:46 -06001258 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001259 }
Jens Axboe491381ce2019-10-17 09:20:46 -06001260 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001261}
1262
Jens Axboeba816ad2019-09-28 11:36:45 -06001263static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001264{
1265 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1266
Jens Axboe491381ce2019-10-17 09:20:46 -06001267 if (kiocb->ki_flags & IOCB_WRITE)
1268 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001269
Jens Axboe9e645e112019-05-10 16:07:28 -06001270 if ((req->flags & REQ_F_LINK) && res != req->result)
1271 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001272 io_cqring_add_event(req, res);
Jens Axboeba816ad2019-09-28 11:36:45 -06001273}
1274
1275static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1276{
1277 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1278
1279 io_complete_rw_common(kiocb, res);
Jens Axboee65ef562019-03-12 10:16:44 -06001280 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001281}
1282
Jens Axboeba816ad2019-09-28 11:36:45 -06001283static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res)
1284{
1285 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001286 struct io_kiocb *nxt = NULL;
Jens Axboeba816ad2019-09-28 11:36:45 -06001287
1288 io_complete_rw_common(kiocb, res);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001289 io_put_req_find_next(req, &nxt);
1290
1291 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001292}
1293
Jens Axboedef596e2019-01-09 08:59:42 -07001294static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1295{
1296 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1297
Jens Axboe491381ce2019-10-17 09:20:46 -06001298 if (kiocb->ki_flags & IOCB_WRITE)
1299 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001300
Jens Axboe9e645e112019-05-10 16:07:28 -06001301 if ((req->flags & REQ_F_LINK) && res != req->result)
1302 req->flags |= REQ_F_FAIL_LINK;
1303 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -07001304 if (res != -EAGAIN)
1305 req->flags |= REQ_F_IOPOLL_COMPLETED;
1306}
1307
1308/*
1309 * After the iocb has been issued, it's safe to be found on the poll list.
1310 * Adding the kiocb to the list AFTER submission ensures that we don't
1311 * find it from a io_iopoll_getevents() thread before the issuer is done
1312 * accessing the kiocb cookie.
1313 */
1314static void io_iopoll_req_issued(struct io_kiocb *req)
1315{
1316 struct io_ring_ctx *ctx = req->ctx;
1317
1318 /*
1319 * Track whether we have multiple files in our lists. This will impact
1320 * how we do polling eventually, not spinning if we're on potentially
1321 * different devices.
1322 */
1323 if (list_empty(&ctx->poll_list)) {
1324 ctx->poll_multi_file = false;
1325 } else if (!ctx->poll_multi_file) {
1326 struct io_kiocb *list_req;
1327
1328 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1329 list);
1330 if (list_req->rw.ki_filp != req->rw.ki_filp)
1331 ctx->poll_multi_file = true;
1332 }
1333
1334 /*
1335 * For fast devices, IO may have already completed. If it has, add
1336 * it to the front so we find it first.
1337 */
1338 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1339 list_add(&req->list, &ctx->poll_list);
1340 else
1341 list_add_tail(&req->list, &ctx->poll_list);
1342}
1343
Jens Axboe3d6770f2019-04-13 11:50:54 -06001344static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07001345{
Jens Axboe3d6770f2019-04-13 11:50:54 -06001346 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -07001347 int diff = state->has_refs - state->used_refs;
1348
1349 if (diff)
1350 fput_many(state->file, diff);
1351 state->file = NULL;
1352 }
1353}
1354
1355/*
1356 * Get as many references to a file as we have IOs left in this submission,
1357 * assuming most submissions are for one file, or at least that each file
1358 * has more than one submission.
1359 */
1360static struct file *io_file_get(struct io_submit_state *state, int fd)
1361{
1362 if (!state)
1363 return fget(fd);
1364
1365 if (state->file) {
1366 if (state->fd == fd) {
1367 state->used_refs++;
1368 state->ios_left--;
1369 return state->file;
1370 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06001371 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07001372 }
1373 state->file = fget_many(fd, state->ios_left);
1374 if (!state->file)
1375 return NULL;
1376
1377 state->fd = fd;
1378 state->has_refs = state->ios_left;
1379 state->used_refs = 1;
1380 state->ios_left--;
1381 return state->file;
1382}
1383
Jens Axboe2b188cc2019-01-07 10:46:33 -07001384/*
1385 * If we tracked the file through the SCM inflight mechanism, we could support
1386 * any file. For now, just ensure that anything potentially problematic is done
1387 * inline.
1388 */
1389static bool io_file_supports_async(struct file *file)
1390{
1391 umode_t mode = file_inode(file)->i_mode;
1392
1393 if (S_ISBLK(mode) || S_ISCHR(mode))
1394 return true;
1395 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
1396 return true;
1397
1398 return false;
1399}
1400
Pavel Begunkov267bc902019-11-07 01:41:08 +03001401static int io_prep_rw(struct io_kiocb *req, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001402{
Pavel Begunkov267bc902019-11-07 01:41:08 +03001403 const struct io_uring_sqe *sqe = req->submit.sqe;
Jens Axboedef596e2019-01-09 08:59:42 -07001404 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001405 struct kiocb *kiocb = &req->rw;
Jens Axboe09bb8392019-03-13 12:39:28 -06001406 unsigned ioprio;
1407 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001408
Jens Axboe09bb8392019-03-13 12:39:28 -06001409 if (!req->file)
1410 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001411
Jens Axboe491381ce2019-10-17 09:20:46 -06001412 if (S_ISREG(file_inode(req->file)->i_mode))
1413 req->flags |= REQ_F_ISREG;
1414
1415 /*
1416 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
1417 * we know to async punt it even if it was opened O_NONBLOCK
1418 */
1419 if (force_nonblock && !io_file_supports_async(req->file)) {
1420 req->flags |= REQ_F_MUST_PUNT;
1421 return -EAGAIN;
1422 }
Jens Axboe6b063142019-01-10 22:13:58 -07001423
Jens Axboe2b188cc2019-01-07 10:46:33 -07001424 kiocb->ki_pos = READ_ONCE(sqe->off);
1425 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
1426 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
1427
1428 ioprio = READ_ONCE(sqe->ioprio);
1429 if (ioprio) {
1430 ret = ioprio_check_cap(ioprio);
1431 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06001432 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001433
1434 kiocb->ki_ioprio = ioprio;
1435 } else
1436 kiocb->ki_ioprio = get_current_ioprio();
1437
1438 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
1439 if (unlikely(ret))
Jens Axboe09bb8392019-03-13 12:39:28 -06001440 return ret;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001441
1442 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06001443 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
1444 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02001445 req->flags |= REQ_F_NOWAIT;
1446
1447 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001448 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001449
Jens Axboedef596e2019-01-09 08:59:42 -07001450 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07001451 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
1452 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06001453 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001454
Jens Axboedef596e2019-01-09 08:59:42 -07001455 kiocb->ki_flags |= IOCB_HIPRI;
1456 kiocb->ki_complete = io_complete_rw_iopoll;
Jens Axboe6873e0b2019-10-30 13:53:09 -06001457 req->result = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001458 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001459 if (kiocb->ki_flags & IOCB_HIPRI)
1460 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07001461 kiocb->ki_complete = io_complete_rw;
1462 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001463 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001464}
1465
1466static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
1467{
1468 switch (ret) {
1469 case -EIOCBQUEUED:
1470 break;
1471 case -ERESTARTSYS:
1472 case -ERESTARTNOINTR:
1473 case -ERESTARTNOHAND:
1474 case -ERESTART_RESTARTBLOCK:
1475 /*
1476 * We can't just restart the syscall, since previously
1477 * submitted sqes may already be in progress. Just fail this
1478 * IO with EINTR.
1479 */
1480 ret = -EINTR;
1481 /* fall through */
1482 default:
1483 kiocb->ki_complete(kiocb, ret, 0);
1484 }
1485}
1486
Jens Axboeba816ad2019-09-28 11:36:45 -06001487static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt,
1488 bool in_async)
1489{
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001490 if (in_async && ret >= 0 && kiocb->ki_complete == io_complete_rw)
Jens Axboeba816ad2019-09-28 11:36:45 -06001491 *nxt = __io_complete_rw(kiocb, ret);
1492 else
1493 io_rw_done(kiocb, ret);
1494}
1495
Jens Axboeedafcce2019-01-09 09:16:05 -07001496static int io_import_fixed(struct io_ring_ctx *ctx, int rw,
1497 const struct io_uring_sqe *sqe,
1498 struct iov_iter *iter)
1499{
1500 size_t len = READ_ONCE(sqe->len);
1501 struct io_mapped_ubuf *imu;
1502 unsigned index, buf_index;
1503 size_t offset;
1504 u64 buf_addr;
1505
1506 /* attempt to use fixed buffers without having provided iovecs */
1507 if (unlikely(!ctx->user_bufs))
1508 return -EFAULT;
1509
1510 buf_index = READ_ONCE(sqe->buf_index);
1511 if (unlikely(buf_index >= ctx->nr_user_bufs))
1512 return -EFAULT;
1513
1514 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
1515 imu = &ctx->user_bufs[index];
1516 buf_addr = READ_ONCE(sqe->addr);
1517
1518 /* overflow */
1519 if (buf_addr + len < buf_addr)
1520 return -EFAULT;
1521 /* not inside the mapped region */
1522 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
1523 return -EFAULT;
1524
1525 /*
1526 * May not be a start of buffer, set size appropriately
1527 * and advance us to the beginning.
1528 */
1529 offset = buf_addr - imu->ubuf;
1530 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06001531
1532 if (offset) {
1533 /*
1534 * Don't use iov_iter_advance() here, as it's really slow for
1535 * using the latter parts of a big fixed buffer - it iterates
1536 * over each segment manually. We can cheat a bit here, because
1537 * we know that:
1538 *
1539 * 1) it's a BVEC iter, we set it up
1540 * 2) all bvecs are PAGE_SIZE in size, except potentially the
1541 * first and last bvec
1542 *
1543 * So just find our index, and adjust the iterator afterwards.
1544 * If the offset is within the first bvec (or the whole first
1545 * bvec, just use iov_iter_advance(). This makes it easier
1546 * since we can just skip the first segment, which may not
1547 * be PAGE_SIZE aligned.
1548 */
1549 const struct bio_vec *bvec = imu->bvec;
1550
1551 if (offset <= bvec->bv_len) {
1552 iov_iter_advance(iter, offset);
1553 } else {
1554 unsigned long seg_skip;
1555
1556 /* skip first vec */
1557 offset -= bvec->bv_len;
1558 seg_skip = 1 + (offset >> PAGE_SHIFT);
1559
1560 iter->bvec = bvec + seg_skip;
1561 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02001562 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001563 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001564 }
1565 }
1566
Jens Axboe5e559562019-11-13 16:12:46 -07001567 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001568}
1569
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001570static ssize_t io_import_iovec(struct io_ring_ctx *ctx, int rw,
1571 const struct sqe_submit *s, struct iovec **iovec,
1572 struct iov_iter *iter)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001573{
1574 const struct io_uring_sqe *sqe = s->sqe;
1575 void __user *buf = u64_to_user_ptr(READ_ONCE(sqe->addr));
1576 size_t sqe_len = READ_ONCE(sqe->len);
Jens Axboeedafcce2019-01-09 09:16:05 -07001577 u8 opcode;
1578
1579 /*
1580 * We're reading ->opcode for the second time, but the first read
1581 * doesn't care whether it's _FIXED or not, so it doesn't matter
1582 * whether ->opcode changes concurrently. The first read does care
1583 * about whether it is a READ or a WRITE, so we don't trust this read
1584 * for that purpose and instead let the caller pass in the read/write
1585 * flag.
1586 */
1587 opcode = READ_ONCE(sqe->opcode);
1588 if (opcode == IORING_OP_READ_FIXED ||
1589 opcode == IORING_OP_WRITE_FIXED) {
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001590 ssize_t ret = io_import_fixed(ctx, rw, sqe, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07001591 *iovec = NULL;
1592 return ret;
1593 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001594
1595 if (!s->has_user)
1596 return -EFAULT;
1597
1598#ifdef CONFIG_COMPAT
1599 if (ctx->compat)
1600 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
1601 iovec, iter);
1602#endif
1603
1604 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
1605}
1606
Jens Axboe32960612019-09-23 11:05:34 -06001607/*
1608 * For files that don't have ->read_iter() and ->write_iter(), handle them
1609 * by looping over ->read() or ->write() manually.
1610 */
1611static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
1612 struct iov_iter *iter)
1613{
1614 ssize_t ret = 0;
1615
1616 /*
1617 * Don't support polled IO through this interface, and we can't
1618 * support non-blocking either. For the latter, this just causes
1619 * the kiocb to be handled from an async context.
1620 */
1621 if (kiocb->ki_flags & IOCB_HIPRI)
1622 return -EOPNOTSUPP;
1623 if (kiocb->ki_flags & IOCB_NOWAIT)
1624 return -EAGAIN;
1625
1626 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03001627 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06001628 ssize_t nr;
1629
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03001630 if (!iov_iter_is_bvec(iter)) {
1631 iovec = iov_iter_iovec(iter);
1632 } else {
1633 /* fixed buffers import bvec */
1634 iovec.iov_base = kmap(iter->bvec->bv_page)
1635 + iter->iov_offset;
1636 iovec.iov_len = min(iter->count,
1637 iter->bvec->bv_len - iter->iov_offset);
1638 }
1639
Jens Axboe32960612019-09-23 11:05:34 -06001640 if (rw == READ) {
1641 nr = file->f_op->read(file, iovec.iov_base,
1642 iovec.iov_len, &kiocb->ki_pos);
1643 } else {
1644 nr = file->f_op->write(file, iovec.iov_base,
1645 iovec.iov_len, &kiocb->ki_pos);
1646 }
1647
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03001648 if (iov_iter_is_bvec(iter))
1649 kunmap(iter->bvec->bv_page);
1650
Jens Axboe32960612019-09-23 11:05:34 -06001651 if (nr < 0) {
1652 if (!ret)
1653 ret = nr;
1654 break;
1655 }
1656 ret += nr;
1657 if (nr != iovec.iov_len)
1658 break;
1659 iov_iter_advance(iter, nr);
1660 }
1661
1662 return ret;
1663}
1664
Pavel Begunkov267bc902019-11-07 01:41:08 +03001665static int io_read(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001666 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001667{
1668 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1669 struct kiocb *kiocb = &req->rw;
1670 struct iov_iter iter;
1671 struct file *file;
Jens Axboe31b51512019-01-18 22:56:34 -07001672 size_t iov_count;
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001673 ssize_t read_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001674
Pavel Begunkov267bc902019-11-07 01:41:08 +03001675 ret = io_prep_rw(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001676 if (ret)
1677 return ret;
1678 file = kiocb->ki_filp;
1679
Jens Axboe2b188cc2019-01-07 10:46:33 -07001680 if (unlikely(!(file->f_mode & FMODE_READ)))
Jens Axboe09bb8392019-03-13 12:39:28 -06001681 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001682
Pavel Begunkov267bc902019-11-07 01:41:08 +03001683 ret = io_import_iovec(req->ctx, READ, &req->submit, &iovec, &iter);
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001684 if (ret < 0)
Jens Axboe09bb8392019-03-13 12:39:28 -06001685 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001686
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001687 read_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06001688 if (req->flags & REQ_F_LINK)
1689 req->result = read_size;
1690
Jens Axboe31b51512019-01-18 22:56:34 -07001691 iov_count = iov_iter_count(&iter);
1692 ret = rw_verify_area(READ, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001693 if (!ret) {
1694 ssize_t ret2;
1695
Jens Axboe32960612019-09-23 11:05:34 -06001696 if (file->f_op->read_iter)
1697 ret2 = call_read_iter(file, kiocb, &iter);
1698 else
1699 ret2 = loop_rw_iter(READ, file, kiocb, &iter);
1700
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001701 /*
1702 * In case of a short read, punt to async. This can happen
1703 * if we have data partially cached. Alternatively we can
1704 * return the short read, in which case the application will
1705 * need to issue another SQE and wait for it. That SQE will
1706 * need async punt anyway, so it's more efficient to do it
1707 * here.
1708 */
Jens Axboe491381ce2019-10-17 09:20:46 -06001709 if (force_nonblock && !(req->flags & REQ_F_NOWAIT) &&
1710 (req->flags & REQ_F_ISREG) &&
1711 ret2 > 0 && ret2 < read_size)
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001712 ret2 = -EAGAIN;
1713 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboe561fb042019-10-24 07:25:42 -06001714 if (!force_nonblock || ret2 != -EAGAIN)
Pavel Begunkov267bc902019-11-07 01:41:08 +03001715 kiocb_done(kiocb, ret2, nxt, req->submit.in_async);
Jens Axboe561fb042019-10-24 07:25:42 -06001716 else
Jens Axboe2b188cc2019-01-07 10:46:33 -07001717 ret = -EAGAIN;
1718 }
1719 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001720 return ret;
1721}
1722
Pavel Begunkov267bc902019-11-07 01:41:08 +03001723static int io_write(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001724 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001725{
1726 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1727 struct kiocb *kiocb = &req->rw;
1728 struct iov_iter iter;
1729 struct file *file;
Jens Axboe31b51512019-01-18 22:56:34 -07001730 size_t iov_count;
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001731 ssize_t ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001732
Pavel Begunkov267bc902019-11-07 01:41:08 +03001733 ret = io_prep_rw(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001734 if (ret)
1735 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001736
Jens Axboe2b188cc2019-01-07 10:46:33 -07001737 file = kiocb->ki_filp;
1738 if (unlikely(!(file->f_mode & FMODE_WRITE)))
Jens Axboe09bb8392019-03-13 12:39:28 -06001739 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001740
Pavel Begunkov267bc902019-11-07 01:41:08 +03001741 ret = io_import_iovec(req->ctx, WRITE, &req->submit, &iovec, &iter);
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001742 if (ret < 0)
Jens Axboe09bb8392019-03-13 12:39:28 -06001743 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001744
Jens Axboe9e645e112019-05-10 16:07:28 -06001745 if (req->flags & REQ_F_LINK)
1746 req->result = ret;
1747
Jens Axboe31b51512019-01-18 22:56:34 -07001748 iov_count = iov_iter_count(&iter);
1749
1750 ret = -EAGAIN;
Jens Axboe561fb042019-10-24 07:25:42 -06001751 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT))
Jens Axboe31b51512019-01-18 22:56:34 -07001752 goto out_free;
Jens Axboe31b51512019-01-18 22:56:34 -07001753
1754 ret = rw_verify_area(WRITE, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001755 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01001756 ssize_t ret2;
1757
Jens Axboe2b188cc2019-01-07 10:46:33 -07001758 /*
1759 * Open-code file_start_write here to grab freeze protection,
1760 * which will be released by another thread in
1761 * io_complete_rw(). Fool lockdep by telling it the lock got
1762 * released so that it doesn't complain about the held lock when
1763 * we return to userspace.
1764 */
Jens Axboe491381ce2019-10-17 09:20:46 -06001765 if (req->flags & REQ_F_ISREG) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001766 __sb_start_write(file_inode(file)->i_sb,
1767 SB_FREEZE_WRITE, true);
1768 __sb_writers_release(file_inode(file)->i_sb,
1769 SB_FREEZE_WRITE);
1770 }
1771 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01001772
Jens Axboe32960612019-09-23 11:05:34 -06001773 if (file->f_op->write_iter)
1774 ret2 = call_write_iter(file, kiocb, &iter);
1775 else
1776 ret2 = loop_rw_iter(WRITE, file, kiocb, &iter);
Jens Axboe561fb042019-10-24 07:25:42 -06001777 if (!force_nonblock || ret2 != -EAGAIN)
Pavel Begunkov267bc902019-11-07 01:41:08 +03001778 kiocb_done(kiocb, ret2, nxt, req->submit.in_async);
Jens Axboe561fb042019-10-24 07:25:42 -06001779 else
Roman Penyaev9bf79332019-03-25 20:09:24 +01001780 ret = -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001781 }
Jens Axboe31b51512019-01-18 22:56:34 -07001782out_free:
Jens Axboe2b188cc2019-01-07 10:46:33 -07001783 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001784 return ret;
1785}
1786
1787/*
1788 * IORING_OP_NOP just posts a completion event, nothing else.
1789 */
Jens Axboe78e19bb2019-11-06 15:21:34 -07001790static int io_nop(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001791{
1792 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001793
Jens Axboedef596e2019-01-09 08:59:42 -07001794 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1795 return -EINVAL;
1796
Jens Axboe78e19bb2019-11-06 15:21:34 -07001797 io_cqring_add_event(req, 0);
Jens Axboee65ef562019-03-12 10:16:44 -06001798 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001799 return 0;
1800}
1801
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001802static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1803{
Jens Axboe6b063142019-01-10 22:13:58 -07001804 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001805
Jens Axboe09bb8392019-03-13 12:39:28 -06001806 if (!req->file)
1807 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001808
Jens Axboe6b063142019-01-10 22:13:58 -07001809 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07001810 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07001811 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001812 return -EINVAL;
1813
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001814 return 0;
1815}
1816
1817static int io_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001818 struct io_kiocb **nxt, bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001819{
1820 loff_t sqe_off = READ_ONCE(sqe->off);
1821 loff_t sqe_len = READ_ONCE(sqe->len);
1822 loff_t end = sqe_off + sqe_len;
1823 unsigned fsync_flags;
1824 int ret;
1825
1826 fsync_flags = READ_ONCE(sqe->fsync_flags);
1827 if (unlikely(fsync_flags & ~IORING_FSYNC_DATASYNC))
1828 return -EINVAL;
1829
1830 ret = io_prep_fsync(req, sqe);
1831 if (ret)
1832 return ret;
1833
1834 /* fsync always requires a blocking context */
1835 if (force_nonblock)
1836 return -EAGAIN;
1837
1838 ret = vfs_fsync_range(req->rw.ki_filp, sqe_off,
1839 end > 0 ? end : LLONG_MAX,
1840 fsync_flags & IORING_FSYNC_DATASYNC);
1841
Jens Axboe9e645e112019-05-10 16:07:28 -06001842 if (ret < 0 && (req->flags & REQ_F_LINK))
1843 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001844 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001845 io_put_req_find_next(req, nxt);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001846 return 0;
1847}
1848
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001849static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1850{
1851 struct io_ring_ctx *ctx = req->ctx;
1852 int ret = 0;
1853
1854 if (!req->file)
1855 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001856
1857 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1858 return -EINVAL;
1859 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
1860 return -EINVAL;
1861
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001862 return ret;
1863}
1864
1865static int io_sync_file_range(struct io_kiocb *req,
1866 const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001867 struct io_kiocb **nxt,
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001868 bool force_nonblock)
1869{
1870 loff_t sqe_off;
1871 loff_t sqe_len;
1872 unsigned flags;
1873 int ret;
1874
1875 ret = io_prep_sfr(req, sqe);
1876 if (ret)
1877 return ret;
1878
1879 /* sync_file_range always requires a blocking context */
1880 if (force_nonblock)
1881 return -EAGAIN;
1882
1883 sqe_off = READ_ONCE(sqe->off);
1884 sqe_len = READ_ONCE(sqe->len);
1885 flags = READ_ONCE(sqe->sync_range_flags);
1886
1887 ret = sync_file_range(req->rw.ki_filp, sqe_off, sqe_len, flags);
1888
Jens Axboe9e645e112019-05-10 16:07:28 -06001889 if (ret < 0 && (req->flags & REQ_F_LINK))
1890 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001891 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001892 io_put_req_find_next(req, nxt);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001893 return 0;
1894}
1895
Jens Axboe0fa03c62019-04-19 13:34:07 -06001896#if defined(CONFIG_NET)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001897static int io_send_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001898 struct io_kiocb **nxt, bool force_nonblock,
Jens Axboeaa1fa282019-04-19 13:38:09 -06001899 long (*fn)(struct socket *, struct user_msghdr __user *,
1900 unsigned int))
1901{
Jens Axboe0fa03c62019-04-19 13:34:07 -06001902 struct socket *sock;
1903 int ret;
1904
1905 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1906 return -EINVAL;
1907
1908 sock = sock_from_file(req->file, &ret);
1909 if (sock) {
1910 struct user_msghdr __user *msg;
1911 unsigned flags;
1912
1913 flags = READ_ONCE(sqe->msg_flags);
1914 if (flags & MSG_DONTWAIT)
1915 req->flags |= REQ_F_NOWAIT;
1916 else if (force_nonblock)
1917 flags |= MSG_DONTWAIT;
1918
1919 msg = (struct user_msghdr __user *) (unsigned long)
1920 READ_ONCE(sqe->addr);
1921
Jens Axboeaa1fa282019-04-19 13:38:09 -06001922 ret = fn(sock, msg, flags);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001923 if (force_nonblock && ret == -EAGAIN)
1924 return ret;
1925 }
1926
Jens Axboe78e19bb2019-11-06 15:21:34 -07001927 io_cqring_add_event(req, ret);
Jens Axboef1f40852019-11-05 20:33:16 -07001928 if (ret < 0 && (req->flags & REQ_F_LINK))
1929 req->flags |= REQ_F_FAIL_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001930 io_put_req_find_next(req, nxt);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001931 return 0;
Jens Axboeaa1fa282019-04-19 13:38:09 -06001932}
1933#endif
1934
1935static int io_sendmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001936 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001937{
1938#if defined(CONFIG_NET)
Jens Axboeba816ad2019-09-28 11:36:45 -06001939 return io_send_recvmsg(req, sqe, nxt, force_nonblock,
1940 __sys_sendmsg_sock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06001941#else
1942 return -EOPNOTSUPP;
1943#endif
1944}
1945
1946static int io_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001947 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001948{
1949#if defined(CONFIG_NET)
Jens Axboeba816ad2019-09-28 11:36:45 -06001950 return io_send_recvmsg(req, sqe, nxt, force_nonblock,
1951 __sys_recvmsg_sock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001952#else
1953 return -EOPNOTSUPP;
1954#endif
1955}
1956
Jens Axboe17f2fe32019-10-17 14:42:58 -06001957static int io_accept(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1958 struct io_kiocb **nxt, bool force_nonblock)
1959{
1960#if defined(CONFIG_NET)
1961 struct sockaddr __user *addr;
1962 int __user *addr_len;
1963 unsigned file_flags;
1964 int flags, ret;
1965
1966 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
1967 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05001968 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06001969 return -EINVAL;
1970
1971 addr = (struct sockaddr __user *) (unsigned long) READ_ONCE(sqe->addr);
1972 addr_len = (int __user *) (unsigned long) READ_ONCE(sqe->addr2);
1973 flags = READ_ONCE(sqe->accept_flags);
1974 file_flags = force_nonblock ? O_NONBLOCK : 0;
1975
1976 ret = __sys_accept4_file(req->file, file_flags, addr, addr_len, flags);
1977 if (ret == -EAGAIN && force_nonblock) {
1978 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
1979 return -EAGAIN;
1980 }
Jens Axboe8e3cca12019-11-09 19:52:33 -07001981 if (ret == -ERESTARTSYS)
1982 ret = -EINTR;
Jens Axboe17f2fe32019-10-17 14:42:58 -06001983 if (ret < 0 && (req->flags & REQ_F_LINK))
1984 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001985 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001986 io_put_req_find_next(req, nxt);
Jens Axboe17f2fe32019-10-17 14:42:58 -06001987 return 0;
1988#else
1989 return -EOPNOTSUPP;
1990#endif
1991}
1992
Jens Axboef8e85cf2019-11-23 14:24:24 -07001993static int io_connect(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1994 struct io_kiocb **nxt, bool force_nonblock)
1995{
1996#if defined(CONFIG_NET)
1997 struct sockaddr __user *addr;
1998 unsigned file_flags;
1999 int addr_len, ret;
2000
2001 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
2002 return -EINVAL;
2003 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
2004 return -EINVAL;
2005
2006 addr = (struct sockaddr __user *) (unsigned long) READ_ONCE(sqe->addr);
2007 addr_len = READ_ONCE(sqe->addr2);
2008 file_flags = force_nonblock ? O_NONBLOCK : 0;
2009
2010 ret = __sys_connect_file(req->file, addr, addr_len, file_flags);
2011 if (ret == -EAGAIN && force_nonblock)
2012 return -EAGAIN;
2013 if (ret == -ERESTARTSYS)
2014 ret = -EINTR;
2015 if (ret < 0 && (req->flags & REQ_F_LINK))
2016 req->flags |= REQ_F_FAIL_LINK;
2017 io_cqring_add_event(req, ret);
2018 io_put_req_find_next(req, nxt);
2019 return 0;
2020#else
2021 return -EOPNOTSUPP;
2022#endif
2023}
2024
Jens Axboeeac406c2019-11-14 12:09:58 -07002025static inline void io_poll_remove_req(struct io_kiocb *req)
2026{
2027 if (!RB_EMPTY_NODE(&req->rb_node)) {
2028 rb_erase(&req->rb_node, &req->ctx->cancel_tree);
2029 RB_CLEAR_NODE(&req->rb_node);
2030 }
2031}
2032
Jens Axboe221c5eb2019-01-17 09:41:58 -07002033static void io_poll_remove_one(struct io_kiocb *req)
2034{
2035 struct io_poll_iocb *poll = &req->poll;
2036
2037 spin_lock(&poll->head->lock);
2038 WRITE_ONCE(poll->canceled, true);
2039 if (!list_empty(&poll->wait.entry)) {
2040 list_del_init(&poll->wait.entry);
Jackie Liua197f662019-11-08 08:09:12 -07002041 io_queue_async_work(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002042 }
2043 spin_unlock(&poll->head->lock);
Jens Axboeeac406c2019-11-14 12:09:58 -07002044 io_poll_remove_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002045}
2046
2047static void io_poll_remove_all(struct io_ring_ctx *ctx)
2048{
Jens Axboeeac406c2019-11-14 12:09:58 -07002049 struct rb_node *node;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002050 struct io_kiocb *req;
2051
2052 spin_lock_irq(&ctx->completion_lock);
Jens Axboeeac406c2019-11-14 12:09:58 -07002053 while ((node = rb_first(&ctx->cancel_tree)) != NULL) {
2054 req = rb_entry(node, struct io_kiocb, rb_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002055 io_poll_remove_one(req);
2056 }
2057 spin_unlock_irq(&ctx->completion_lock);
2058}
2059
Jens Axboe47f46762019-11-09 17:43:02 -07002060static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
2061{
Jens Axboeeac406c2019-11-14 12:09:58 -07002062 struct rb_node *p, *parent = NULL;
Jens Axboe47f46762019-11-09 17:43:02 -07002063 struct io_kiocb *req;
2064
Jens Axboeeac406c2019-11-14 12:09:58 -07002065 p = ctx->cancel_tree.rb_node;
2066 while (p) {
2067 parent = p;
2068 req = rb_entry(parent, struct io_kiocb, rb_node);
2069 if (sqe_addr < req->user_data) {
2070 p = p->rb_left;
2071 } else if (sqe_addr > req->user_data) {
2072 p = p->rb_right;
2073 } else {
2074 io_poll_remove_one(req);
2075 return 0;
2076 }
Jens Axboe47f46762019-11-09 17:43:02 -07002077 }
2078
2079 return -ENOENT;
2080}
2081
Jens Axboe221c5eb2019-01-17 09:41:58 -07002082/*
2083 * Find a running poll command that matches one specified in sqe->addr,
2084 * and remove it if found.
2085 */
2086static int io_poll_remove(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2087{
2088 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07002089 int ret;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002090
2091 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2092 return -EINVAL;
2093 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
2094 sqe->poll_events)
2095 return -EINVAL;
2096
2097 spin_lock_irq(&ctx->completion_lock);
Jens Axboe47f46762019-11-09 17:43:02 -07002098 ret = io_poll_cancel(ctx, READ_ONCE(sqe->addr));
Jens Axboe221c5eb2019-01-17 09:41:58 -07002099 spin_unlock_irq(&ctx->completion_lock);
2100
Jens Axboe78e19bb2019-11-06 15:21:34 -07002101 io_cqring_add_event(req, ret);
Jens Axboef1f40852019-11-05 20:33:16 -07002102 if (ret < 0 && (req->flags & REQ_F_LINK))
2103 req->flags |= REQ_F_FAIL_LINK;
Jens Axboee65ef562019-03-12 10:16:44 -06002104 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002105 return 0;
2106}
2107
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002108static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002109{
Jackie Liua197f662019-11-08 08:09:12 -07002110 struct io_ring_ctx *ctx = req->ctx;
2111
Jens Axboe8c838782019-03-12 15:48:16 -06002112 req->poll.done = true;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002113 if (error)
2114 io_cqring_fill_event(req, error);
2115 else
2116 io_cqring_fill_event(req, mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06002117 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002118}
2119
Jens Axboe561fb042019-10-24 07:25:42 -06002120static void io_poll_complete_work(struct io_wq_work **workptr)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002121{
Jens Axboe561fb042019-10-24 07:25:42 -06002122 struct io_wq_work *work = *workptr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002123 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
2124 struct io_poll_iocb *poll = &req->poll;
2125 struct poll_table_struct pt = { ._key = poll->events };
2126 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe89723d02019-11-05 15:32:58 -07002127 struct io_kiocb *nxt = NULL;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002128 __poll_t mask = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002129 int ret = 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002130
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002131 if (work->flags & IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06002132 WRITE_ONCE(poll->canceled, true);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002133 ret = -ECANCELED;
2134 } else if (READ_ONCE(poll->canceled)) {
2135 ret = -ECANCELED;
2136 }
Jens Axboe561fb042019-10-24 07:25:42 -06002137
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002138 if (ret != -ECANCELED)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002139 mask = vfs_poll(poll->file, &pt) & poll->events;
2140
2141 /*
2142 * Note that ->ki_cancel callers also delete iocb from active_reqs after
2143 * calling ->ki_cancel. We need the ctx_lock roundtrip here to
2144 * synchronize with them. In the cancellation case the list_del_init
2145 * itself is not actually needed, but harmless so we keep it in to
2146 * avoid further branches in the fast path.
2147 */
2148 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002149 if (!mask && ret != -ECANCELED) {
Jens Axboe221c5eb2019-01-17 09:41:58 -07002150 add_wait_queue(poll->head, &poll->wait);
2151 spin_unlock_irq(&ctx->completion_lock);
2152 return;
2153 }
Jens Axboeeac406c2019-11-14 12:09:58 -07002154 io_poll_remove_req(req);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002155 io_poll_complete(req, mask, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002156 spin_unlock_irq(&ctx->completion_lock);
2157
Jens Axboe8c838782019-03-12 15:48:16 -06002158 io_cqring_ev_posted(ctx);
Jens Axboe89723d02019-11-05 15:32:58 -07002159
Jens Axboefba38c22019-11-18 12:27:57 -07002160 if (ret < 0 && req->flags & REQ_F_LINK)
2161 req->flags |= REQ_F_FAIL_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08002162 io_put_req_find_next(req, &nxt);
Jens Axboe89723d02019-11-05 15:32:58 -07002163 if (nxt)
2164 *workptr = &nxt->work;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002165}
2166
2167static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
2168 void *key)
2169{
2170 struct io_poll_iocb *poll = container_of(wait, struct io_poll_iocb,
2171 wait);
2172 struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
2173 struct io_ring_ctx *ctx = req->ctx;
2174 __poll_t mask = key_to_poll(key);
Jens Axboe8c838782019-03-12 15:48:16 -06002175 unsigned long flags;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002176
2177 /* for instances that support it check for an event match first: */
Jens Axboe8c838782019-03-12 15:48:16 -06002178 if (mask && !(mask & poll->events))
2179 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002180
2181 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06002182
Jens Axboe7c9e7f02019-11-12 08:15:53 -07002183 /*
2184 * Run completion inline if we can. We're using trylock here because
2185 * we are violating the completion_lock -> poll wq lock ordering.
2186 * If we have a link timeout we're going to need the completion_lock
2187 * for finalizing the request, mark us as having grabbed that already.
2188 */
Jens Axboe8c838782019-03-12 15:48:16 -06002189 if (mask && spin_trylock_irqsave(&ctx->completion_lock, flags)) {
Jens Axboeeac406c2019-11-14 12:09:58 -07002190 io_poll_remove_req(req);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002191 io_poll_complete(req, mask, 0);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07002192 req->flags |= REQ_F_COMP_LOCKED;
2193 io_put_req(req);
Jens Axboe8c838782019-03-12 15:48:16 -06002194 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2195
2196 io_cqring_ev_posted(ctx);
Jens Axboe8c838782019-03-12 15:48:16 -06002197 } else {
Jackie Liua197f662019-11-08 08:09:12 -07002198 io_queue_async_work(req);
Jens Axboe8c838782019-03-12 15:48:16 -06002199 }
2200
Jens Axboe221c5eb2019-01-17 09:41:58 -07002201 return 1;
2202}
2203
2204struct io_poll_table {
2205 struct poll_table_struct pt;
2206 struct io_kiocb *req;
2207 int error;
2208};
2209
2210static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
2211 struct poll_table_struct *p)
2212{
2213 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
2214
2215 if (unlikely(pt->req->poll.head)) {
2216 pt->error = -EINVAL;
2217 return;
2218 }
2219
2220 pt->error = 0;
2221 pt->req->poll.head = head;
2222 add_wait_queue(head, &pt->req->poll.wait);
2223}
2224
Jens Axboeeac406c2019-11-14 12:09:58 -07002225static void io_poll_req_insert(struct io_kiocb *req)
2226{
2227 struct io_ring_ctx *ctx = req->ctx;
2228 struct rb_node **p = &ctx->cancel_tree.rb_node;
2229 struct rb_node *parent = NULL;
2230 struct io_kiocb *tmp;
2231
2232 while (*p) {
2233 parent = *p;
2234 tmp = rb_entry(parent, struct io_kiocb, rb_node);
2235 if (req->user_data < tmp->user_data)
2236 p = &(*p)->rb_left;
2237 else
2238 p = &(*p)->rb_right;
2239 }
2240 rb_link_node(&req->rb_node, parent, p);
2241 rb_insert_color(&req->rb_node, &ctx->cancel_tree);
2242}
2243
Jens Axboe89723d02019-11-05 15:32:58 -07002244static int io_poll_add(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2245 struct io_kiocb **nxt)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002246{
2247 struct io_poll_iocb *poll = &req->poll;
2248 struct io_ring_ctx *ctx = req->ctx;
2249 struct io_poll_table ipt;
Jens Axboe8c838782019-03-12 15:48:16 -06002250 bool cancel = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002251 __poll_t mask;
2252 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002253
2254 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2255 return -EINVAL;
2256 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
2257 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06002258 if (!poll->file)
2259 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002260
Jens Axboe6cc47d12019-09-18 11:18:23 -06002261 req->submit.sqe = NULL;
Jens Axboe561fb042019-10-24 07:25:42 -06002262 INIT_IO_WORK(&req->work, io_poll_complete_work);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002263 events = READ_ONCE(sqe->poll_events);
2264 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
Jens Axboeeac406c2019-11-14 12:09:58 -07002265 RB_CLEAR_NODE(&req->rb_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002266
Jens Axboe221c5eb2019-01-17 09:41:58 -07002267 poll->head = NULL;
Jens Axboe8c838782019-03-12 15:48:16 -06002268 poll->done = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002269 poll->canceled = false;
2270
2271 ipt.pt._qproc = io_poll_queue_proc;
2272 ipt.pt._key = poll->events;
2273 ipt.req = req;
2274 ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
2275
2276 /* initialized the list so that we can do list_empty checks */
2277 INIT_LIST_HEAD(&poll->wait.entry);
2278 init_waitqueue_func_entry(&poll->wait, io_poll_wake);
2279
Jens Axboe36703242019-07-25 10:20:18 -06002280 INIT_LIST_HEAD(&req->list);
2281
Jens Axboe221c5eb2019-01-17 09:41:58 -07002282 mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002283
2284 spin_lock_irq(&ctx->completion_lock);
Jens Axboe8c838782019-03-12 15:48:16 -06002285 if (likely(poll->head)) {
2286 spin_lock(&poll->head->lock);
2287 if (unlikely(list_empty(&poll->wait.entry))) {
2288 if (ipt.error)
2289 cancel = true;
2290 ipt.error = 0;
2291 mask = 0;
2292 }
2293 if (mask || ipt.error)
2294 list_del_init(&poll->wait.entry);
2295 else if (cancel)
2296 WRITE_ONCE(poll->canceled, true);
2297 else if (!poll->done) /* actually waiting for an event */
Jens Axboeeac406c2019-11-14 12:09:58 -07002298 io_poll_req_insert(req);
Jens Axboe8c838782019-03-12 15:48:16 -06002299 spin_unlock(&poll->head->lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002300 }
Jens Axboe8c838782019-03-12 15:48:16 -06002301 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06002302 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002303 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06002304 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07002305 spin_unlock_irq(&ctx->completion_lock);
2306
Jens Axboe8c838782019-03-12 15:48:16 -06002307 if (mask) {
2308 io_cqring_ev_posted(ctx);
Jackie Liuec9c02a2019-11-08 23:50:36 +08002309 io_put_req_find_next(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002310 }
Jens Axboe8c838782019-03-12 15:48:16 -06002311 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002312}
2313
Jens Axboe5262f562019-09-17 12:26:57 -06002314static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
2315{
Jens Axboead8a48a2019-11-15 08:49:11 -07002316 struct io_timeout_data *data = container_of(timer,
2317 struct io_timeout_data, timer);
2318 struct io_kiocb *req = data->req;
2319 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06002320 unsigned long flags;
2321
Jens Axboe5262f562019-09-17 12:26:57 -06002322 atomic_inc(&ctx->cq_timeouts);
2323
2324 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08002325 /*
Jens Axboe11365042019-10-16 09:08:32 -06002326 * We could be racing with timeout deletion. If the list is empty,
2327 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08002328 */
Jens Axboe842f9612019-10-29 12:34:10 -06002329 if (!list_empty(&req->list)) {
Jens Axboe11365042019-10-16 09:08:32 -06002330 struct io_kiocb *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06002331
Jens Axboe11365042019-10-16 09:08:32 -06002332 /*
2333 * Adjust the reqs sequence before the current one because it
2334 * will consume a slot in the cq_ring and the the cq_tail
2335 * pointer will be increased, otherwise other timeout reqs may
2336 * return in advance without waiting for enough wait_nr.
2337 */
2338 prev = req;
2339 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
2340 prev->sequence++;
Jens Axboe11365042019-10-16 09:08:32 -06002341 list_del_init(&req->list);
Jens Axboe11365042019-10-16 09:08:32 -06002342 }
Jens Axboe842f9612019-10-29 12:34:10 -06002343
Jens Axboe78e19bb2019-11-06 15:21:34 -07002344 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06002345 io_commit_cqring(ctx);
2346 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2347
2348 io_cqring_ev_posted(ctx);
Jens Axboef1f40852019-11-05 20:33:16 -07002349 if (req->flags & REQ_F_LINK)
2350 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe5262f562019-09-17 12:26:57 -06002351 io_put_req(req);
2352 return HRTIMER_NORESTART;
2353}
2354
Jens Axboe47f46762019-11-09 17:43:02 -07002355static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
2356{
2357 struct io_kiocb *req;
2358 int ret = -ENOENT;
2359
2360 list_for_each_entry(req, &ctx->timeout_list, list) {
2361 if (user_data == req->user_data) {
2362 list_del_init(&req->list);
2363 ret = 0;
2364 break;
2365 }
2366 }
2367
2368 if (ret == -ENOENT)
2369 return ret;
2370
Jens Axboead8a48a2019-11-15 08:49:11 -07002371 ret = hrtimer_try_to_cancel(&req->timeout.data->timer);
Jens Axboe47f46762019-11-09 17:43:02 -07002372 if (ret == -1)
2373 return -EALREADY;
2374
Jens Axboefba38c22019-11-18 12:27:57 -07002375 if (req->flags & REQ_F_LINK)
2376 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe47f46762019-11-09 17:43:02 -07002377 io_cqring_fill_event(req, -ECANCELED);
2378 io_put_req(req);
2379 return 0;
2380}
2381
Jens Axboe11365042019-10-16 09:08:32 -06002382/*
2383 * Remove or update an existing timeout command
2384 */
2385static int io_timeout_remove(struct io_kiocb *req,
2386 const struct io_uring_sqe *sqe)
2387{
2388 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe11365042019-10-16 09:08:32 -06002389 unsigned flags;
Jens Axboe47f46762019-11-09 17:43:02 -07002390 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06002391
2392 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2393 return -EINVAL;
2394 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
2395 return -EINVAL;
2396 flags = READ_ONCE(sqe->timeout_flags);
2397 if (flags)
2398 return -EINVAL;
2399
Jens Axboe11365042019-10-16 09:08:32 -06002400 spin_lock_irq(&ctx->completion_lock);
Jens Axboe47f46762019-11-09 17:43:02 -07002401 ret = io_timeout_cancel(ctx, READ_ONCE(sqe->addr));
Jens Axboe11365042019-10-16 09:08:32 -06002402
Jens Axboe47f46762019-11-09 17:43:02 -07002403 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06002404 io_commit_cqring(ctx);
2405 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06002406 io_cqring_ev_posted(ctx);
Jens Axboe47f46762019-11-09 17:43:02 -07002407 if (ret < 0 && req->flags & REQ_F_LINK)
2408 req->flags |= REQ_F_FAIL_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08002409 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06002410 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06002411}
2412
Jens Axboead8a48a2019-11-15 08:49:11 -07002413static int io_timeout_setup(struct io_kiocb *req)
Jens Axboe5262f562019-09-17 12:26:57 -06002414{
Jens Axboead8a48a2019-11-15 08:49:11 -07002415 const struct io_uring_sqe *sqe = req->submit.sqe;
2416 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06002417 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06002418
Jens Axboead8a48a2019-11-15 08:49:11 -07002419 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06002420 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07002421 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06002422 return -EINVAL;
2423 flags = READ_ONCE(sqe->timeout_flags);
2424 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06002425 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06002426
Jens Axboead8a48a2019-11-15 08:49:11 -07002427 data = kzalloc(sizeof(struct io_timeout_data), GFP_KERNEL);
2428 if (!data)
2429 return -ENOMEM;
2430 data->req = req;
2431 req->timeout.data = data;
2432 req->flags |= REQ_F_TIMEOUT;
2433
2434 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06002435 return -EFAULT;
2436
Jens Axboe11365042019-10-16 09:08:32 -06002437 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07002438 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06002439 else
Jens Axboead8a48a2019-11-15 08:49:11 -07002440 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06002441
Jens Axboead8a48a2019-11-15 08:49:11 -07002442 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
2443 return 0;
2444}
2445
2446static int io_timeout(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2447{
2448 unsigned count;
2449 struct io_ring_ctx *ctx = req->ctx;
2450 struct io_timeout_data *data;
2451 struct list_head *entry;
2452 unsigned span = 0;
2453 int ret;
2454
2455 ret = io_timeout_setup(req);
2456 /* common setup allows flags (like links) set, we don't */
2457 if (!ret && sqe->flags)
2458 ret = -EINVAL;
2459 if (ret)
2460 return ret;
Jens Axboe93bd25b2019-11-11 23:34:31 -07002461
Jens Axboe5262f562019-09-17 12:26:57 -06002462 /*
2463 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07002464 * timeout event to be satisfied. If it isn't set, then this is
2465 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06002466 */
2467 count = READ_ONCE(sqe->off);
Jens Axboe93bd25b2019-11-11 23:34:31 -07002468 if (!count) {
2469 req->flags |= REQ_F_TIMEOUT_NOSEQ;
2470 spin_lock_irq(&ctx->completion_lock);
2471 entry = ctx->timeout_list.prev;
2472 goto add;
2473 }
Jens Axboe5262f562019-09-17 12:26:57 -06002474
2475 req->sequence = ctx->cached_sq_head + count - 1;
yangerkun5da0fb12019-10-15 21:59:29 +08002476 /* reuse it to store the count */
2477 req->submit.sequence = 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;
Jens Axboe5262f562019-09-17 12:26:57 -06002488
Jens Axboe93bd25b2019-11-11 23:34:31 -07002489 if (nxt->flags & REQ_F_TIMEOUT_NOSEQ)
2490 continue;
2491
yangerkun5da0fb12019-10-15 21:59:29 +08002492 /*
2493 * Since cached_sq_head + count - 1 can overflow, use type long
2494 * long to store it.
2495 */
2496 tmp = (long long)ctx->cached_sq_head + count - 1;
2497 nxt_sq_head = nxt->sequence - nxt->submit.sequence + 1;
2498 tmp_nxt = (long long)nxt_sq_head + nxt->submit.sequence - 1;
2499
2500 /*
2501 * cached_sq_head may overflow, and it will never overflow twice
2502 * once there is some timeout req still be valid.
2503 */
2504 if (ctx->cached_sq_head < nxt_sq_head)
yangerkun8b07a652019-10-17 12:12:35 +08002505 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08002506
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002507 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06002508 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002509
2510 /*
2511 * Sequence of reqs after the insert one and itself should
2512 * be adjusted because each timeout req consumes a slot.
2513 */
2514 span++;
2515 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06002516 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002517 req->sequence -= span;
Jens Axboe93bd25b2019-11-11 23:34:31 -07002518add:
Jens Axboe5262f562019-09-17 12:26:57 -06002519 list_add(&req->list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07002520 data = req->timeout.data;
2521 data->timer.function = io_timeout_fn;
2522 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06002523 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06002524 return 0;
2525}
2526
Jens Axboe62755e32019-10-28 21:49:21 -06002527static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06002528{
Jens Axboe62755e32019-10-28 21:49:21 -06002529 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06002530
Jens Axboe62755e32019-10-28 21:49:21 -06002531 return req->user_data == (unsigned long) data;
2532}
2533
Jens Axboee977d6d2019-11-05 12:39:45 -07002534static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06002535{
Jens Axboe62755e32019-10-28 21:49:21 -06002536 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06002537 int ret = 0;
2538
Jens Axboe62755e32019-10-28 21:49:21 -06002539 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
2540 switch (cancel_ret) {
2541 case IO_WQ_CANCEL_OK:
2542 ret = 0;
2543 break;
2544 case IO_WQ_CANCEL_RUNNING:
2545 ret = -EALREADY;
2546 break;
2547 case IO_WQ_CANCEL_NOTFOUND:
2548 ret = -ENOENT;
2549 break;
2550 }
2551
Jens Axboee977d6d2019-11-05 12:39:45 -07002552 return ret;
2553}
2554
Jens Axboe47f46762019-11-09 17:43:02 -07002555static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
2556 struct io_kiocb *req, __u64 sqe_addr,
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002557 struct io_kiocb **nxt, int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07002558{
2559 unsigned long flags;
2560 int ret;
2561
2562 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
2563 if (ret != -ENOENT) {
2564 spin_lock_irqsave(&ctx->completion_lock, flags);
2565 goto done;
2566 }
2567
2568 spin_lock_irqsave(&ctx->completion_lock, flags);
2569 ret = io_timeout_cancel(ctx, sqe_addr);
2570 if (ret != -ENOENT)
2571 goto done;
2572 ret = io_poll_cancel(ctx, sqe_addr);
2573done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002574 if (!ret)
2575 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07002576 io_cqring_fill_event(req, ret);
2577 io_commit_cqring(ctx);
2578 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2579 io_cqring_ev_posted(ctx);
2580
2581 if (ret < 0 && (req->flags & REQ_F_LINK))
2582 req->flags |= REQ_F_FAIL_LINK;
2583 io_put_req_find_next(req, nxt);
2584}
2585
Jens Axboee977d6d2019-11-05 12:39:45 -07002586static int io_async_cancel(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2587 struct io_kiocb **nxt)
2588{
2589 struct io_ring_ctx *ctx = req->ctx;
Jens Axboee977d6d2019-11-05 12:39:45 -07002590
2591 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2592 return -EINVAL;
2593 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
2594 sqe->cancel_flags)
2595 return -EINVAL;
2596
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002597 io_async_find_and_cancel(ctx, req, READ_ONCE(sqe->addr), nxt, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06002598 return 0;
2599}
2600
Jackie Liua197f662019-11-08 08:09:12 -07002601static int io_req_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -06002602{
Pavel Begunkov267bc902019-11-07 01:41:08 +03002603 const struct io_uring_sqe *sqe = req->submit.sqe;
Jens Axboede0617e2019-04-06 21:51:27 -06002604 struct io_uring_sqe *sqe_copy;
Jackie Liua197f662019-11-08 08:09:12 -07002605 struct io_ring_ctx *ctx = req->ctx;
Jens Axboede0617e2019-04-06 21:51:27 -06002606
Bob Liu9d858b22019-11-13 18:06:25 +08002607 /* Still need defer if there is pending req in defer list. */
2608 if (!req_need_defer(req) && list_empty(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06002609 return 0;
2610
2611 sqe_copy = kmalloc(sizeof(*sqe_copy), GFP_KERNEL);
2612 if (!sqe_copy)
2613 return -EAGAIN;
2614
2615 spin_lock_irq(&ctx->completion_lock);
Bob Liu9d858b22019-11-13 18:06:25 +08002616 if (!req_need_defer(req) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06002617 spin_unlock_irq(&ctx->completion_lock);
2618 kfree(sqe_copy);
2619 return 0;
2620 }
2621
2622 memcpy(sqe_copy, sqe, sizeof(*sqe_copy));
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03002623 req->flags |= REQ_F_FREE_SQE;
Jens Axboede0617e2019-04-06 21:51:27 -06002624 req->submit.sqe = sqe_copy;
2625
Jens Axboe915967f2019-11-21 09:01:20 -07002626 trace_io_uring_defer(ctx, req, req->user_data);
Jens Axboede0617e2019-04-06 21:51:27 -06002627 list_add_tail(&req->list, &ctx->defer_list);
2628 spin_unlock_irq(&ctx->completion_lock);
2629 return -EIOCBQUEUED;
2630}
2631
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03002632__attribute__((nonnull))
Pavel Begunkovd7324472019-11-21 21:24:36 +03002633static int io_issue_sqe(struct io_kiocb *req, struct io_kiocb **nxt,
2634 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002635{
Jens Axboee0c5c572019-03-12 10:18:47 -06002636 int ret, opcode;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002637 struct sqe_submit *s = &req->submit;
Jackie Liua197f662019-11-08 08:09:12 -07002638 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002639
2640 opcode = READ_ONCE(s->sqe->opcode);
2641 switch (opcode) {
2642 case IORING_OP_NOP:
Jens Axboe78e19bb2019-11-06 15:21:34 -07002643 ret = io_nop(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002644 break;
2645 case IORING_OP_READV:
Jens Axboeedafcce2019-01-09 09:16:05 -07002646 if (unlikely(s->sqe->buf_index))
2647 return -EINVAL;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002648 ret = io_read(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002649 break;
2650 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07002651 if (unlikely(s->sqe->buf_index))
2652 return -EINVAL;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002653 ret = io_write(req, nxt, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07002654 break;
2655 case IORING_OP_READ_FIXED:
Pavel Begunkov267bc902019-11-07 01:41:08 +03002656 ret = io_read(req, nxt, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07002657 break;
2658 case IORING_OP_WRITE_FIXED:
Pavel Begunkov267bc902019-11-07 01:41:08 +03002659 ret = io_write(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002660 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002661 case IORING_OP_FSYNC:
Jens Axboeba816ad2019-09-28 11:36:45 -06002662 ret = io_fsync(req, s->sqe, nxt, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002663 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002664 case IORING_OP_POLL_ADD:
Jens Axboe89723d02019-11-05 15:32:58 -07002665 ret = io_poll_add(req, s->sqe, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002666 break;
2667 case IORING_OP_POLL_REMOVE:
2668 ret = io_poll_remove(req, s->sqe);
2669 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002670 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboeba816ad2019-09-28 11:36:45 -06002671 ret = io_sync_file_range(req, s->sqe, nxt, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002672 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002673 case IORING_OP_SENDMSG:
Jens Axboeba816ad2019-09-28 11:36:45 -06002674 ret = io_sendmsg(req, s->sqe, nxt, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06002675 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06002676 case IORING_OP_RECVMSG:
Jens Axboeba816ad2019-09-28 11:36:45 -06002677 ret = io_recvmsg(req, s->sqe, nxt, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06002678 break;
Jens Axboe5262f562019-09-17 12:26:57 -06002679 case IORING_OP_TIMEOUT:
2680 ret = io_timeout(req, s->sqe);
2681 break;
Jens Axboe11365042019-10-16 09:08:32 -06002682 case IORING_OP_TIMEOUT_REMOVE:
2683 ret = io_timeout_remove(req, s->sqe);
2684 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06002685 case IORING_OP_ACCEPT:
2686 ret = io_accept(req, s->sqe, nxt, force_nonblock);
2687 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07002688 case IORING_OP_CONNECT:
2689 ret = io_connect(req, s->sqe, nxt, force_nonblock);
2690 break;
Jens Axboe62755e32019-10-28 21:49:21 -06002691 case IORING_OP_ASYNC_CANCEL:
2692 ret = io_async_cancel(req, s->sqe, nxt);
2693 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002694 default:
2695 ret = -EINVAL;
2696 break;
2697 }
2698
Jens Axboedef596e2019-01-09 08:59:42 -07002699 if (ret)
2700 return ret;
2701
2702 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe9e645e112019-05-10 16:07:28 -06002703 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07002704 return -EAGAIN;
2705
2706 /* workqueue context doesn't hold uring_lock, grab it now */
Jackie Liuba5290c2019-10-09 09:19:59 +08002707 if (s->in_async)
Jens Axboedef596e2019-01-09 08:59:42 -07002708 mutex_lock(&ctx->uring_lock);
2709 io_iopoll_req_issued(req);
Jackie Liuba5290c2019-10-09 09:19:59 +08002710 if (s->in_async)
Jens Axboedef596e2019-01-09 08:59:42 -07002711 mutex_unlock(&ctx->uring_lock);
2712 }
2713
2714 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002715}
2716
Jens Axboeb76da702019-11-20 13:05:32 -07002717static void io_link_work_cb(struct io_wq_work **workptr)
2718{
2719 struct io_wq_work *work = *workptr;
2720 struct io_kiocb *link = work->data;
2721
2722 io_queue_linked_timeout(link);
2723 work->func = io_wq_submit_work;
2724}
2725
Jens Axboe561fb042019-10-24 07:25:42 -06002726static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboe31b51512019-01-18 22:56:34 -07002727{
Jens Axboe561fb042019-10-24 07:25:42 -06002728 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002729 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe561fb042019-10-24 07:25:42 -06002730 struct sqe_submit *s = &req->submit;
Jens Axboe561fb042019-10-24 07:25:42 -06002731 struct io_kiocb *nxt = NULL;
2732 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002733
Jens Axboe561fb042019-10-24 07:25:42 -06002734 /* Ensure we clear previously set non-block flag */
2735 req->rw.ki_flags &= ~IOCB_NOWAIT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002736
Jens Axboe561fb042019-10-24 07:25:42 -06002737 if (work->flags & IO_WQ_WORK_CANCEL)
2738 ret = -ECANCELED;
Jens Axboe31b51512019-01-18 22:56:34 -07002739
Jens Axboe561fb042019-10-24 07:25:42 -06002740 if (!ret) {
2741 s->has_user = (work->flags & IO_WQ_WORK_HAS_MM) != 0;
2742 s->in_async = true;
2743 do {
Pavel Begunkovd7324472019-11-21 21:24:36 +03002744 ret = io_issue_sqe(req, &nxt, false);
Jens Axboe561fb042019-10-24 07:25:42 -06002745 /*
2746 * We can get EAGAIN for polled IO even though we're
2747 * forcing a sync submission from here, since we can't
2748 * wait for request slots on the block side.
2749 */
2750 if (ret != -EAGAIN)
2751 break;
2752 cond_resched();
2753 } while (1);
2754 }
Jens Axboe31b51512019-01-18 22:56:34 -07002755
Jens Axboe561fb042019-10-24 07:25:42 -06002756 /* drop submission reference */
Jackie Liuec9c02a2019-11-08 23:50:36 +08002757 io_put_req(req);
Jens Axboe817869d2019-04-30 14:44:05 -06002758
Jens Axboe561fb042019-10-24 07:25:42 -06002759 if (ret) {
Jens Axboef1f40852019-11-05 20:33:16 -07002760 if (req->flags & REQ_F_LINK)
2761 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07002762 io_cqring_add_event(req, ret);
Jens Axboe817869d2019-04-30 14:44:05 -06002763 io_put_req(req);
Jens Axboeedafcce2019-01-09 09:16:05 -07002764 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002765
Jens Axboe561fb042019-10-24 07:25:42 -06002766 /* if a dependent link is ready, pass it back */
2767 if (!ret && nxt) {
Jens Axboe94ae5e72019-11-14 19:39:52 -07002768 struct io_kiocb *link;
2769
2770 io_prep_async_work(nxt, &link);
Jens Axboe561fb042019-10-24 07:25:42 -06002771 *workptr = &nxt->work;
Jens Axboeb76da702019-11-20 13:05:32 -07002772 if (link) {
2773 nxt->work.flags |= IO_WQ_WORK_CB;
2774 nxt->work.func = io_link_work_cb;
2775 nxt->work.data = link;
2776 }
Jens Axboeedafcce2019-01-09 09:16:05 -07002777 }
Jens Axboe31b51512019-01-18 22:56:34 -07002778}
Jens Axboe2b188cc2019-01-07 10:46:33 -07002779
Jens Axboe09bb8392019-03-13 12:39:28 -06002780static bool io_op_needs_file(const struct io_uring_sqe *sqe)
2781{
2782 int op = READ_ONCE(sqe->opcode);
2783
2784 switch (op) {
2785 case IORING_OP_NOP:
2786 case IORING_OP_POLL_REMOVE:
Pavel Begunkov5683e542019-11-14 00:59:19 +03002787 case IORING_OP_TIMEOUT:
Pavel Begunkova320e9f2019-11-14 00:11:01 +03002788 case IORING_OP_TIMEOUT_REMOVE:
2789 case IORING_OP_ASYNC_CANCEL:
2790 case IORING_OP_LINK_TIMEOUT:
Jens Axboe09bb8392019-03-13 12:39:28 -06002791 return false;
2792 default:
2793 return true;
2794 }
2795}
2796
Jens Axboe65e19f52019-10-26 07:20:21 -06002797static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
2798 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06002799{
Jens Axboe65e19f52019-10-26 07:20:21 -06002800 struct fixed_file_table *table;
2801
2802 table = &ctx->file_table[index >> IORING_FILE_TABLE_SHIFT];
2803 return table->files[index & IORING_FILE_TABLE_MASK];
2804}
2805
Jackie Liua197f662019-11-08 08:09:12 -07002806static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req)
Jens Axboe09bb8392019-03-13 12:39:28 -06002807{
Pavel Begunkov267bc902019-11-07 01:41:08 +03002808 struct sqe_submit *s = &req->submit;
Jackie Liua197f662019-11-08 08:09:12 -07002809 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe09bb8392019-03-13 12:39:28 -06002810 unsigned flags;
2811 int fd;
2812
2813 flags = READ_ONCE(s->sqe->flags);
2814 fd = READ_ONCE(s->sqe->fd);
2815
Jackie Liu4fe2c962019-09-09 20:50:40 +08002816 if (flags & IOSQE_IO_DRAIN)
Jens Axboede0617e2019-04-06 21:51:27 -06002817 req->flags |= REQ_F_IO_DRAIN;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002818 /*
2819 * All io need record the previous position, if LINK vs DARIN,
2820 * it can be used to mark the position of the first IO in the
2821 * link list.
2822 */
2823 req->sequence = s->sequence;
Jens Axboede0617e2019-04-06 21:51:27 -06002824
Jens Axboe60c112b2019-06-21 10:20:18 -06002825 if (!io_op_needs_file(s->sqe))
Jens Axboe09bb8392019-03-13 12:39:28 -06002826 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06002827
2828 if (flags & IOSQE_FIXED_FILE) {
Jens Axboe65e19f52019-10-26 07:20:21 -06002829 if (unlikely(!ctx->file_table ||
Jens Axboe09bb8392019-03-13 12:39:28 -06002830 (unsigned) fd >= ctx->nr_user_files))
2831 return -EBADF;
Jens Axboeb7620122019-10-26 07:22:55 -06002832 fd = array_index_nospec(fd, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06002833 req->file = io_file_from_index(ctx, fd);
2834 if (!req->file)
Jens Axboe08a45172019-10-03 08:11:03 -06002835 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06002836 req->flags |= REQ_F_FIXED_FILE;
2837 } else {
2838 if (s->needs_fixed_file)
2839 return -EBADF;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002840 trace_io_uring_file_get(ctx, fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06002841 req->file = io_file_get(state, fd);
2842 if (unlikely(!req->file))
2843 return -EBADF;
2844 }
2845
2846 return 0;
2847}
2848
Jackie Liua197f662019-11-08 08:09:12 -07002849static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002850{
Jens Axboefcb323c2019-10-24 12:39:47 -06002851 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07002852 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06002853
2854 rcu_read_lock();
2855 spin_lock_irq(&ctx->inflight_lock);
2856 /*
2857 * We use the f_ops->flush() handler to ensure that we can flush
2858 * out work accessing these files if the fd is closed. Check if
2859 * the fd has changed since we started down this path, and disallow
2860 * this operation if it has.
2861 */
2862 if (fcheck(req->submit.ring_fd) == req->submit.ring_file) {
2863 list_add(&req->inflight_entry, &ctx->inflight_list);
2864 req->flags |= REQ_F_INFLIGHT;
2865 req->work.files = current->files;
2866 ret = 0;
2867 }
2868 spin_unlock_irq(&ctx->inflight_lock);
2869 rcu_read_unlock();
2870
2871 return ret;
2872}
2873
Jens Axboe2665abf2019-11-05 12:40:47 -07002874static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
2875{
Jens Axboead8a48a2019-11-15 08:49:11 -07002876 struct io_timeout_data *data = container_of(timer,
2877 struct io_timeout_data, timer);
2878 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07002879 struct io_ring_ctx *ctx = req->ctx;
2880 struct io_kiocb *prev = NULL;
2881 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07002882
2883 spin_lock_irqsave(&ctx->completion_lock, flags);
2884
2885 /*
2886 * We don't expect the list to be empty, that will only happen if we
2887 * race with the completion of the linked work.
2888 */
2889 if (!list_empty(&req->list)) {
2890 prev = list_entry(req->list.prev, struct io_kiocb, link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07002891 if (refcount_inc_not_zero(&prev->refs)) {
Jens Axboe76a46e02019-11-10 23:34:16 -07002892 list_del_init(&req->list);
Jens Axboe5d960722019-11-19 15:31:28 -07002893 prev->flags &= ~REQ_F_LINK_TIMEOUT;
2894 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07002895 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07002896 }
2897
2898 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2899
2900 if (prev) {
Jens Axboefba38c22019-11-18 12:27:57 -07002901 if (prev->flags & REQ_F_LINK)
2902 prev->flags |= REQ_F_FAIL_LINK;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002903 io_async_find_and_cancel(ctx, req, prev->user_data, NULL,
2904 -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07002905 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07002906 } else {
2907 io_cqring_add_event(req, -ETIME);
2908 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07002909 }
Jens Axboe2665abf2019-11-05 12:40:47 -07002910 return HRTIMER_NORESTART;
2911}
2912
Jens Axboead8a48a2019-11-15 08:49:11 -07002913static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07002914{
Jens Axboe76a46e02019-11-10 23:34:16 -07002915 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07002916
Jens Axboe76a46e02019-11-10 23:34:16 -07002917 /*
2918 * If the list is now empty, then our linked request finished before
2919 * we got a chance to setup the timer
2920 */
2921 spin_lock_irq(&ctx->completion_lock);
2922 if (!list_empty(&req->list)) {
Jens Axboe94ae5e72019-11-14 19:39:52 -07002923 struct io_timeout_data *data = req->timeout.data;
2924
Jens Axboead8a48a2019-11-15 08:49:11 -07002925 data->timer.function = io_link_timeout_fn;
2926 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
2927 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07002928 }
Jens Axboe76a46e02019-11-10 23:34:16 -07002929 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07002930
Jens Axboe2665abf2019-11-05 12:40:47 -07002931 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07002932 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07002933}
2934
Jens Axboead8a48a2019-11-15 08:49:11 -07002935static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07002936{
2937 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002938
Jens Axboe2665abf2019-11-05 12:40:47 -07002939 if (!(req->flags & REQ_F_LINK))
2940 return NULL;
2941
2942 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, list);
Jens Axboe76a46e02019-11-10 23:34:16 -07002943 if (!nxt || nxt->submit.sqe->opcode != IORING_OP_LINK_TIMEOUT)
2944 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07002945
Jens Axboe76a46e02019-11-10 23:34:16 -07002946 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07002947 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07002948}
2949
Jens Axboe0e0702d2019-11-14 21:42:10 -07002950static void __io_queue_sqe(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002951{
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03002952 struct io_kiocb *linked_timeout = io_prep_linked_timeout(req);
2953 struct io_kiocb *nxt = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002954 int ret;
2955
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03002956 ret = io_issue_sqe(req, &nxt, true);
2957 if (nxt)
2958 io_queue_async_work(nxt);
Jens Axboe491381ce2019-10-17 09:20:46 -06002959
2960 /*
2961 * We async punt it if the file wasn't marked NOWAIT, or if the file
2962 * doesn't support non-blocking read/write attempts
2963 */
2964 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
2965 (req->flags & REQ_F_MUST_PUNT))) {
Pavel Begunkov267bc902019-11-07 01:41:08 +03002966 struct sqe_submit *s = &req->submit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002967 struct io_uring_sqe *sqe_copy;
2968
Jackie Liu954dab12019-09-18 10:37:52 +08002969 sqe_copy = kmemdup(s->sqe, sizeof(*sqe_copy), GFP_KERNEL);
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03002970 if (!sqe_copy)
2971 goto err;
Jens Axboee65ef562019-03-12 10:16:44 -06002972
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03002973 s->sqe = sqe_copy;
2974 req->flags |= REQ_F_FREE_SQE;
2975
2976 if (req->work.flags & IO_WQ_WORK_NEEDS_FILES) {
2977 ret = io_grab_files(req);
2978 if (ret)
2979 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002980 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03002981
2982 /*
2983 * Queued up for async execution, worker will release
2984 * submit reference when the iocb is actually submitted.
2985 */
2986 io_queue_async_work(req);
2987 return;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002988 }
Jens Axboee65ef562019-03-12 10:16:44 -06002989
Jens Axboefcb323c2019-10-24 12:39:47 -06002990err:
Jens Axboee65ef562019-03-12 10:16:44 -06002991 /* drop submission reference */
2992 io_put_req(req);
2993
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03002994 if (linked_timeout) {
Jens Axboe76a46e02019-11-10 23:34:16 -07002995 if (!ret)
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03002996 io_queue_linked_timeout(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07002997 else
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03002998 io_put_req(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07002999 }
3000
Jens Axboee65ef562019-03-12 10:16:44 -06003001 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06003002 if (ret) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07003003 io_cqring_add_event(req, ret);
Jens Axboe9e645e112019-05-10 16:07:28 -06003004 if (req->flags & REQ_F_LINK)
3005 req->flags |= REQ_F_FAIL_LINK;
Jens Axboee65ef562019-03-12 10:16:44 -06003006 io_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06003007 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003008}
3009
Jens Axboe0e0702d2019-11-14 21:42:10 -07003010static void io_queue_sqe(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08003011{
3012 int ret;
3013
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003014 if (unlikely(req->ctx->drain_next)) {
3015 req->flags |= REQ_F_IO_DRAIN;
3016 req->ctx->drain_next = false;
3017 }
3018 req->ctx->drain_next = (req->flags & REQ_F_DRAIN_LINK);
3019
Jackie Liua197f662019-11-08 08:09:12 -07003020 ret = io_req_defer(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08003021 if (ret) {
3022 if (ret != -EIOCBQUEUED) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07003023 io_cqring_add_event(req, ret);
Pavel Begunkovd3b357962019-11-19 23:32:48 +03003024 if (req->flags & REQ_F_LINK)
3025 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07003026 io_double_put_req(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08003027 }
Jens Axboe0e0702d2019-11-14 21:42:10 -07003028 } else
3029 __io_queue_sqe(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08003030}
3031
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003032static inline void io_queue_link_head(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08003033{
Jens Axboe94ae5e72019-11-14 19:39:52 -07003034 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003035 io_cqring_add_event(req, -ECANCELED);
3036 io_double_put_req(req);
3037 } else
Jens Axboe0e0702d2019-11-14 21:42:10 -07003038 io_queue_sqe(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08003039}
3040
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003041
Jens Axboe9e645e112019-05-10 16:07:28 -06003042#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK)
3043
Jackie Liua197f662019-11-08 08:09:12 -07003044static void io_submit_sqe(struct io_kiocb *req, struct io_submit_state *state,
3045 struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06003046{
Pavel Begunkov267bc902019-11-07 01:41:08 +03003047 struct sqe_submit *s = &req->submit;
Jackie Liua197f662019-11-08 08:09:12 -07003048 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -06003049 int ret;
3050
Jens Axboe78e19bb2019-11-06 15:21:34 -07003051 req->user_data = s->sqe->user_data;
3052
Jens Axboe9e645e112019-05-10 16:07:28 -06003053 /* enforce forwards compatibility on users */
3054 if (unlikely(s->sqe->flags & ~SQE_VALID_FLAGS)) {
3055 ret = -EINVAL;
Pavel Begunkov196be952019-11-07 01:41:06 +03003056 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06003057 }
3058
Jackie Liua197f662019-11-08 08:09:12 -07003059 ret = io_req_set_file(state, req);
Jens Axboe9e645e112019-05-10 16:07:28 -06003060 if (unlikely(ret)) {
3061err_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07003062 io_cqring_add_event(req, ret);
3063 io_double_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06003064 return;
3065 }
3066
Jens Axboe9e645e112019-05-10 16:07:28 -06003067 /*
3068 * If we already have a head request, queue this one for async
3069 * submittal once the head completes. If we don't have a head but
3070 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
3071 * submitted sync once the chain is complete. If none of those
3072 * conditions are true (normal request), then just queue it.
3073 */
3074 if (*link) {
3075 struct io_kiocb *prev = *link;
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03003076 struct io_uring_sqe *sqe_copy;
Jens Axboe9e645e112019-05-10 16:07:28 -06003077
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003078 if (s->sqe->flags & IOSQE_IO_DRAIN)
3079 (*link)->flags |= REQ_F_DRAIN_LINK | REQ_F_IO_DRAIN;
3080
Jens Axboe94ae5e72019-11-14 19:39:52 -07003081 if (READ_ONCE(s->sqe->opcode) == IORING_OP_LINK_TIMEOUT) {
3082 ret = io_timeout_setup(req);
3083 /* common setup allows offset being set, we don't */
3084 if (!ret && s->sqe->off)
3085 ret = -EINVAL;
3086 if (ret) {
3087 prev->flags |= REQ_F_FAIL_LINK;
3088 goto err_req;
3089 }
3090 }
3091
Jens Axboe9e645e112019-05-10 16:07:28 -06003092 sqe_copy = kmemdup(s->sqe, sizeof(*sqe_copy), GFP_KERNEL);
3093 if (!sqe_copy) {
3094 ret = -EAGAIN;
3095 goto err_req;
3096 }
3097
3098 s->sqe = sqe_copy;
Jens Axboe94ae5e72019-11-14 19:39:52 -07003099 req->flags |= REQ_F_FREE_SQE;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02003100 trace_io_uring_link(ctx, req, prev);
Jens Axboe9e645e112019-05-10 16:07:28 -06003101 list_add_tail(&req->list, &prev->link_list);
3102 } else if (s->sqe->flags & IOSQE_IO_LINK) {
3103 req->flags |= REQ_F_LINK;
3104
Jens Axboe9e645e112019-05-10 16:07:28 -06003105 INIT_LIST_HEAD(&req->link_list);
3106 *link = req;
3107 } else {
Jackie Liua197f662019-11-08 08:09:12 -07003108 io_queue_sqe(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06003109 }
3110}
3111
Jens Axboe9a56a232019-01-09 09:06:50 -07003112/*
3113 * Batched submission is done, ensure local IO is flushed out.
3114 */
3115static void io_submit_state_end(struct io_submit_state *state)
3116{
3117 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06003118 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07003119 if (state->free_reqs)
3120 kmem_cache_free_bulk(req_cachep, state->free_reqs,
3121 &state->reqs[state->cur_req]);
Jens Axboe9a56a232019-01-09 09:06:50 -07003122}
3123
3124/*
3125 * Start submission side cache.
3126 */
3127static void io_submit_state_start(struct io_submit_state *state,
3128 struct io_ring_ctx *ctx, unsigned max_ios)
3129{
3130 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07003131 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07003132 state->file = NULL;
3133 state->ios_left = max_ios;
3134}
3135
Jens Axboe2b188cc2019-01-07 10:46:33 -07003136static void io_commit_sqring(struct io_ring_ctx *ctx)
3137{
Hristo Venev75b28af2019-08-26 17:23:46 +00003138 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003139
Hristo Venev75b28af2019-08-26 17:23:46 +00003140 if (ctx->cached_sq_head != READ_ONCE(rings->sq.head)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07003141 /*
3142 * Ensure any loads from the SQEs are done at this point,
3143 * since once we write the new head, the application could
3144 * write new data to them.
3145 */
Hristo Venev75b28af2019-08-26 17:23:46 +00003146 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003147 }
3148}
3149
3150/*
Jens Axboe2b188cc2019-01-07 10:46:33 -07003151 * Fetch an sqe, if one is available. Note that s->sqe will point to memory
3152 * that is mapped by userspace. This means that care needs to be taken to
3153 * ensure that reads are stable, as we cannot rely on userspace always
3154 * being a good citizen. If members of the sqe are validated and then later
3155 * used, it's important that those reads are done through READ_ONCE() to
3156 * prevent a re-load down the line.
3157 */
3158static bool io_get_sqring(struct io_ring_ctx *ctx, struct sqe_submit *s)
3159{
Hristo Venev75b28af2019-08-26 17:23:46 +00003160 struct io_rings *rings = ctx->rings;
3161 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003162 unsigned head;
3163
3164 /*
3165 * The cached sq head (or cq tail) serves two purposes:
3166 *
3167 * 1) allows us to batch the cost of updating the user visible
3168 * head updates.
3169 * 2) allows the kernel side to track the head on its own, even
3170 * though the application is the one updating it.
3171 */
3172 head = ctx->cached_sq_head;
Stefan Bühlere523a292019-04-19 11:57:44 +02003173 /* make sure SQ entry isn't read before tail */
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03003174 if (unlikely(head == smp_load_acquire(&rings->sq.tail)))
Jens Axboe2b188cc2019-01-07 10:46:33 -07003175 return false;
3176
Hristo Venev75b28af2019-08-26 17:23:46 +00003177 head = READ_ONCE(sq_array[head & ctx->sq_mask]);
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03003178 if (likely(head < ctx->sq_entries)) {
Jens Axboefcb323c2019-10-24 12:39:47 -06003179 s->ring_file = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003180 s->sqe = &ctx->sq_sqes[head];
Jackie Liu8776f3f2019-09-09 20:50:39 +08003181 s->sequence = ctx->cached_sq_head;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003182 ctx->cached_sq_head++;
3183 return true;
3184 }
3185
3186 /* drop invalid entries */
3187 ctx->cached_sq_head++;
Jens Axboe498ccd92019-10-25 10:04:25 -06003188 ctx->cached_sq_dropped++;
3189 WRITE_ONCE(rings->sq_dropped, ctx->cached_sq_dropped);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003190 return false;
3191}
3192
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003193static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkovae9428c2019-11-06 00:22:14 +03003194 struct file *ring_file, int ring_fd,
3195 struct mm_struct **mm, bool async)
Jens Axboe6c271ce2019-01-10 11:22:30 -07003196{
3197 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06003198 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06003199 int i, submitted = 0;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03003200 bool mm_fault = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003201
Jens Axboec4a2ed72019-11-21 21:01:26 -07003202 /* if we have a backlog and couldn't flush it all, return BUSY */
3203 if (!list_empty(&ctx->cq_overflow_list) &&
3204 !io_cqring_overflow_flush(ctx, false))
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003205 return -EBUSY;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003206
3207 if (nr > IO_PLUG_THRESHOLD) {
3208 io_submit_state_start(&state, ctx, nr);
3209 statep = &state;
3210 }
3211
3212 for (i = 0; i < nr; i++) {
Pavel Begunkov196be952019-11-07 01:41:06 +03003213 struct io_kiocb *req;
Pavel Begunkov50585b92019-11-07 01:41:07 +03003214 unsigned int sqe_flags;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003215
Pavel Begunkov196be952019-11-07 01:41:06 +03003216 req = io_get_req(ctx, statep);
3217 if (unlikely(!req)) {
3218 if (!submitted)
3219 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003220 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06003221 }
Pavel Begunkov50585b92019-11-07 01:41:07 +03003222 if (!io_get_sqring(ctx, &req->submit)) {
Pavel Begunkov196be952019-11-07 01:41:06 +03003223 __io_free_req(req);
3224 break;
3225 }
Jens Axboe9e645e112019-05-10 16:07:28 -06003226
Pavel Begunkov50585b92019-11-07 01:41:07 +03003227 if (io_sqe_needs_user(req->submit.sqe) && !*mm) {
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03003228 mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm);
3229 if (!mm_fault) {
3230 use_mm(ctx->sqo_mm);
3231 *mm = ctx->sqo_mm;
3232 }
3233 }
3234
Pavel Begunkov50585b92019-11-07 01:41:07 +03003235 sqe_flags = req->submit.sqe->flags;
3236
Pavel Begunkov50585b92019-11-07 01:41:07 +03003237 req->submit.ring_file = ring_file;
3238 req->submit.ring_fd = ring_fd;
3239 req->submit.has_user = *mm != NULL;
3240 req->submit.in_async = async;
3241 req->submit.needs_fixed_file = async;
3242 trace_io_uring_submit_sqe(ctx, req->submit.sqe->user_data,
3243 true, async);
Jackie Liua197f662019-11-08 08:09:12 -07003244 io_submit_sqe(req, statep, &link);
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03003245 submitted++;
Pavel Begunkove5eb6362019-11-06 00:22:15 +03003246
3247 /*
3248 * If previous wasn't linked and we have a linked command,
3249 * that's the end of the chain. Submit the previous link.
3250 */
Pavel Begunkov50585b92019-11-07 01:41:07 +03003251 if (!(sqe_flags & IOSQE_IO_LINK) && link) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003252 io_queue_link_head(link);
Pavel Begunkove5eb6362019-11-06 00:22:15 +03003253 link = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003254 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07003255 }
3256
Jens Axboe9e645e112019-05-10 16:07:28 -06003257 if (link)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003258 io_queue_link_head(link);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003259 if (statep)
3260 io_submit_state_end(&state);
3261
Pavel Begunkovae9428c2019-11-06 00:22:14 +03003262 /* Commit SQ ring head once we've consumed and submitted all SQEs */
3263 io_commit_sqring(ctx);
3264
Jens Axboe6c271ce2019-01-10 11:22:30 -07003265 return submitted;
3266}
3267
3268static int io_sq_thread(void *data)
3269{
Jens Axboe6c271ce2019-01-10 11:22:30 -07003270 struct io_ring_ctx *ctx = data;
3271 struct mm_struct *cur_mm = NULL;
Jens Axboe181e4482019-11-25 08:52:30 -07003272 const struct cred *old_cred;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003273 mm_segment_t old_fs;
3274 DEFINE_WAIT(wait);
3275 unsigned inflight;
3276 unsigned long timeout;
Jens Axboec1edbf52019-11-10 16:56:04 -07003277 int ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003278
Jens Axboe206aefd2019-11-07 18:27:42 -07003279 complete(&ctx->completions[1]);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08003280
Jens Axboe6c271ce2019-01-10 11:22:30 -07003281 old_fs = get_fs();
3282 set_fs(USER_DS);
Jens Axboe181e4482019-11-25 08:52:30 -07003283 old_cred = override_creds(ctx->creds);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003284
Jens Axboec1edbf52019-11-10 16:56:04 -07003285 ret = timeout = inflight = 0;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003286 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003287 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003288
3289 if (inflight) {
3290 unsigned nr_events = 0;
3291
3292 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe2b2ed972019-10-25 10:06:15 -06003293 /*
3294 * inflight is the count of the maximum possible
3295 * entries we submitted, but it can be smaller
3296 * if we dropped some of them. If we don't have
3297 * poll entries available, then we know that we
3298 * have nothing left to poll for. Reset the
3299 * inflight count to zero in that case.
3300 */
3301 mutex_lock(&ctx->uring_lock);
3302 if (!list_empty(&ctx->poll_list))
3303 __io_iopoll_check(ctx, &nr_events, 0);
3304 else
3305 inflight = 0;
3306 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003307 } else {
3308 /*
3309 * Normal IO, just pretend everything completed.
3310 * We don't have to poll completions for that.
3311 */
3312 nr_events = inflight;
3313 }
3314
3315 inflight -= nr_events;
3316 if (!inflight)
3317 timeout = jiffies + ctx->sq_thread_idle;
3318 }
3319
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003320 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07003321
3322 /*
3323 * If submit got -EBUSY, flag us as needing the application
3324 * to enter the kernel to reap and flush events.
3325 */
3326 if (!to_submit || ret == -EBUSY) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07003327 /*
3328 * We're polling. If we're within the defined idle
3329 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07003330 * to sleep. The exception is if we got EBUSY doing
3331 * more IO, we should wait for the application to
3332 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07003333 */
Jens Axboec1edbf52019-11-10 16:56:04 -07003334 if (inflight ||
3335 (!time_after(jiffies, timeout) && ret != -EBUSY)) {
Jens Axboe9831a902019-09-19 09:48:55 -06003336 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07003337 continue;
3338 }
3339
3340 /*
3341 * Drop cur_mm before scheduling, we can't hold it for
3342 * long periods (or over schedule()). Do this before
3343 * adding ourselves to the waitqueue, as the unuse/drop
3344 * may sleep.
3345 */
3346 if (cur_mm) {
3347 unuse_mm(cur_mm);
3348 mmput(cur_mm);
3349 cur_mm = NULL;
3350 }
3351
3352 prepare_to_wait(&ctx->sqo_wait, &wait,
3353 TASK_INTERRUPTIBLE);
3354
3355 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00003356 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02003357 /* make sure to read SQ tail after writing flags */
3358 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07003359
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003360 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07003361 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003362 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07003363 finish_wait(&ctx->sqo_wait, &wait);
3364 break;
3365 }
3366 if (signal_pending(current))
3367 flush_signals(current);
3368 schedule();
3369 finish_wait(&ctx->sqo_wait, &wait);
3370
Hristo Venev75b28af2019-08-26 17:23:46 +00003371 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003372 continue;
3373 }
3374 finish_wait(&ctx->sqo_wait, &wait);
3375
Hristo Venev75b28af2019-08-26 17:23:46 +00003376 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003377 }
3378
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003379 to_submit = min(to_submit, ctx->sq_entries);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003380 ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true);
3381 if (ret > 0)
3382 inflight += ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003383 }
3384
3385 set_fs(old_fs);
3386 if (cur_mm) {
3387 unuse_mm(cur_mm);
3388 mmput(cur_mm);
3389 }
Jens Axboe181e4482019-11-25 08:52:30 -07003390 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06003391
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003392 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06003393
Jens Axboe6c271ce2019-01-10 11:22:30 -07003394 return 0;
3395}
3396
Jens Axboebda52162019-09-24 13:47:15 -06003397struct io_wait_queue {
3398 struct wait_queue_entry wq;
3399 struct io_ring_ctx *ctx;
3400 unsigned to_wait;
3401 unsigned nr_timeouts;
3402};
3403
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003404static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06003405{
3406 struct io_ring_ctx *ctx = iowq->ctx;
3407
3408 /*
3409 * Wake up if we have enough events, or if a timeout occured since we
3410 * started waiting. For timeouts, we always want to return to userspace,
3411 * regardless of event count.
3412 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003413 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06003414 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
3415}
3416
3417static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
3418 int wake_flags, void *key)
3419{
3420 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
3421 wq);
3422
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003423 /* use noflush == true, as we can't safely rely on locking context */
3424 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06003425 return -1;
3426
3427 return autoremove_wake_function(curr, mode, wake_flags, key);
3428}
3429
Jens Axboe2b188cc2019-01-07 10:46:33 -07003430/*
3431 * Wait until events become available, if we don't already have some. The
3432 * application must reap them itself, as they reside on the shared cq ring.
3433 */
3434static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
3435 const sigset_t __user *sig, size_t sigsz)
3436{
Jens Axboebda52162019-09-24 13:47:15 -06003437 struct io_wait_queue iowq = {
3438 .wq = {
3439 .private = current,
3440 .func = io_wake_function,
3441 .entry = LIST_HEAD_INIT(iowq.wq.entry),
3442 },
3443 .ctx = ctx,
3444 .to_wait = min_events,
3445 };
Hristo Venev75b28af2019-08-26 17:23:46 +00003446 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08003447 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003448
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003449 if (io_cqring_events(ctx, false) >= min_events)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003450 return 0;
3451
3452 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01003453#ifdef CONFIG_COMPAT
3454 if (in_compat_syscall())
3455 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07003456 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01003457 else
3458#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07003459 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01003460
Jens Axboe2b188cc2019-01-07 10:46:33 -07003461 if (ret)
3462 return ret;
3463 }
3464
Jens Axboebda52162019-09-24 13:47:15 -06003465 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02003466 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06003467 do {
3468 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
3469 TASK_INTERRUPTIBLE);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003470 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06003471 break;
3472 schedule();
3473 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08003474 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06003475 break;
3476 }
3477 } while (1);
3478 finish_wait(&ctx->wait, &iowq.wq);
3479
Jackie Liue9ffa5c2019-10-29 11:16:42 +08003480 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003481
Hristo Venev75b28af2019-08-26 17:23:46 +00003482 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003483}
3484
Jens Axboe6b063142019-01-10 22:13:58 -07003485static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
3486{
3487#if defined(CONFIG_UNIX)
3488 if (ctx->ring_sock) {
3489 struct sock *sock = ctx->ring_sock->sk;
3490 struct sk_buff *skb;
3491
3492 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
3493 kfree_skb(skb);
3494 }
3495#else
3496 int i;
3497
Jens Axboe65e19f52019-10-26 07:20:21 -06003498 for (i = 0; i < ctx->nr_user_files; i++) {
3499 struct file *file;
3500
3501 file = io_file_from_index(ctx, i);
3502 if (file)
3503 fput(file);
3504 }
Jens Axboe6b063142019-01-10 22:13:58 -07003505#endif
3506}
3507
3508static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
3509{
Jens Axboe65e19f52019-10-26 07:20:21 -06003510 unsigned nr_tables, i;
3511
3512 if (!ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003513 return -ENXIO;
3514
3515 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06003516 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
3517 for (i = 0; i < nr_tables; i++)
3518 kfree(ctx->file_table[i].files);
3519 kfree(ctx->file_table);
3520 ctx->file_table = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003521 ctx->nr_user_files = 0;
3522 return 0;
3523}
3524
Jens Axboe6c271ce2019-01-10 11:22:30 -07003525static void io_sq_thread_stop(struct io_ring_ctx *ctx)
3526{
3527 if (ctx->sqo_thread) {
Jens Axboe206aefd2019-11-07 18:27:42 -07003528 wait_for_completion(&ctx->completions[1]);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003529 /*
3530 * The park is a bit of a work-around, without it we get
3531 * warning spews on shutdown with SQPOLL set and affinity
3532 * set to a single CPU.
3533 */
Jens Axboe06058632019-04-13 09:26:03 -06003534 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003535 kthread_stop(ctx->sqo_thread);
3536 ctx->sqo_thread = NULL;
3537 }
3538}
3539
Jens Axboe6b063142019-01-10 22:13:58 -07003540static void io_finish_async(struct io_ring_ctx *ctx)
3541{
Jens Axboe6c271ce2019-01-10 11:22:30 -07003542 io_sq_thread_stop(ctx);
3543
Jens Axboe561fb042019-10-24 07:25:42 -06003544 if (ctx->io_wq) {
3545 io_wq_destroy(ctx->io_wq);
3546 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003547 }
3548}
3549
3550#if defined(CONFIG_UNIX)
3551static void io_destruct_skb(struct sk_buff *skb)
3552{
3553 struct io_ring_ctx *ctx = skb->sk->sk_user_data;
3554
Jens Axboe561fb042019-10-24 07:25:42 -06003555 if (ctx->io_wq)
3556 io_wq_flush(ctx->io_wq);
Jens Axboe8a997342019-10-09 14:40:13 -06003557
Jens Axboe6b063142019-01-10 22:13:58 -07003558 unix_destruct_scm(skb);
3559}
3560
3561/*
3562 * Ensure the UNIX gc is aware of our file set, so we are certain that
3563 * the io_uring can be safely unregistered on process exit, even if we have
3564 * loops in the file referencing.
3565 */
3566static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
3567{
3568 struct sock *sk = ctx->ring_sock->sk;
3569 struct scm_fp_list *fpl;
3570 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06003571 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07003572
3573 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
3574 unsigned long inflight = ctx->user->unix_inflight + nr;
3575
3576 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
3577 return -EMFILE;
3578 }
3579
3580 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
3581 if (!fpl)
3582 return -ENOMEM;
3583
3584 skb = alloc_skb(0, GFP_KERNEL);
3585 if (!skb) {
3586 kfree(fpl);
3587 return -ENOMEM;
3588 }
3589
3590 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07003591
Jens Axboe08a45172019-10-03 08:11:03 -06003592 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07003593 fpl->user = get_uid(ctx->user);
3594 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003595 struct file *file = io_file_from_index(ctx, i + offset);
3596
3597 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06003598 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06003599 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06003600 unix_inflight(fpl->user, fpl->fp[nr_files]);
3601 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07003602 }
3603
Jens Axboe08a45172019-10-03 08:11:03 -06003604 if (nr_files) {
3605 fpl->max = SCM_MAX_FD;
3606 fpl->count = nr_files;
3607 UNIXCB(skb).fp = fpl;
3608 skb->destructor = io_destruct_skb;
3609 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
3610 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07003611
Jens Axboe08a45172019-10-03 08:11:03 -06003612 for (i = 0; i < nr_files; i++)
3613 fput(fpl->fp[i]);
3614 } else {
3615 kfree_skb(skb);
3616 kfree(fpl);
3617 }
Jens Axboe6b063142019-01-10 22:13:58 -07003618
3619 return 0;
3620}
3621
3622/*
3623 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
3624 * causes regular reference counting to break down. We rely on the UNIX
3625 * garbage collection to take care of this problem for us.
3626 */
3627static int io_sqe_files_scm(struct io_ring_ctx *ctx)
3628{
3629 unsigned left, total;
3630 int ret = 0;
3631
3632 total = 0;
3633 left = ctx->nr_user_files;
3634 while (left) {
3635 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07003636
3637 ret = __io_sqe_files_scm(ctx, this_files, total);
3638 if (ret)
3639 break;
3640 left -= this_files;
3641 total += this_files;
3642 }
3643
3644 if (!ret)
3645 return 0;
3646
3647 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003648 struct file *file = io_file_from_index(ctx, total);
3649
3650 if (file)
3651 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07003652 total++;
3653 }
3654
3655 return ret;
3656}
3657#else
3658static int io_sqe_files_scm(struct io_ring_ctx *ctx)
3659{
3660 return 0;
3661}
3662#endif
3663
Jens Axboe65e19f52019-10-26 07:20:21 -06003664static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
3665 unsigned nr_files)
3666{
3667 int i;
3668
3669 for (i = 0; i < nr_tables; i++) {
3670 struct fixed_file_table *table = &ctx->file_table[i];
3671 unsigned this_files;
3672
3673 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
3674 table->files = kcalloc(this_files, sizeof(struct file *),
3675 GFP_KERNEL);
3676 if (!table->files)
3677 break;
3678 nr_files -= this_files;
3679 }
3680
3681 if (i == nr_tables)
3682 return 0;
3683
3684 for (i = 0; i < nr_tables; i++) {
3685 struct fixed_file_table *table = &ctx->file_table[i];
3686 kfree(table->files);
3687 }
3688 return 1;
3689}
3690
Jens Axboe6b063142019-01-10 22:13:58 -07003691static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
3692 unsigned nr_args)
3693{
3694 __s32 __user *fds = (__s32 __user *) arg;
Jens Axboe65e19f52019-10-26 07:20:21 -06003695 unsigned nr_tables;
Jens Axboe6b063142019-01-10 22:13:58 -07003696 int fd, ret = 0;
3697 unsigned i;
3698
Jens Axboe65e19f52019-10-26 07:20:21 -06003699 if (ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003700 return -EBUSY;
3701 if (!nr_args)
3702 return -EINVAL;
3703 if (nr_args > IORING_MAX_FIXED_FILES)
3704 return -EMFILE;
3705
Jens Axboe65e19f52019-10-26 07:20:21 -06003706 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
3707 ctx->file_table = kcalloc(nr_tables, sizeof(struct fixed_file_table),
3708 GFP_KERNEL);
3709 if (!ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003710 return -ENOMEM;
3711
Jens Axboe65e19f52019-10-26 07:20:21 -06003712 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
3713 kfree(ctx->file_table);
Jens Axboe46568e92019-11-10 08:40:53 -07003714 ctx->file_table = NULL;
Jens Axboe65e19f52019-10-26 07:20:21 -06003715 return -ENOMEM;
3716 }
3717
Jens Axboe08a45172019-10-03 08:11:03 -06003718 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003719 struct fixed_file_table *table;
3720 unsigned index;
3721
Jens Axboe6b063142019-01-10 22:13:58 -07003722 ret = -EFAULT;
3723 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
3724 break;
Jens Axboe08a45172019-10-03 08:11:03 -06003725 /* allow sparse sets */
3726 if (fd == -1) {
3727 ret = 0;
3728 continue;
3729 }
Jens Axboe6b063142019-01-10 22:13:58 -07003730
Jens Axboe65e19f52019-10-26 07:20:21 -06003731 table = &ctx->file_table[i >> IORING_FILE_TABLE_SHIFT];
3732 index = i & IORING_FILE_TABLE_MASK;
3733 table->files[index] = fget(fd);
Jens Axboe6b063142019-01-10 22:13:58 -07003734
3735 ret = -EBADF;
Jens Axboe65e19f52019-10-26 07:20:21 -06003736 if (!table->files[index])
Jens Axboe6b063142019-01-10 22:13:58 -07003737 break;
3738 /*
3739 * Don't allow io_uring instances to be registered. If UNIX
3740 * isn't enabled, then this causes a reference cycle and this
3741 * instance can never get freed. If UNIX is enabled we'll
3742 * handle it just fine, but there's still no point in allowing
3743 * a ring fd as it doesn't support regular read/write anyway.
3744 */
Jens Axboe65e19f52019-10-26 07:20:21 -06003745 if (table->files[index]->f_op == &io_uring_fops) {
3746 fput(table->files[index]);
Jens Axboe6b063142019-01-10 22:13:58 -07003747 break;
3748 }
Jens Axboe6b063142019-01-10 22:13:58 -07003749 ret = 0;
3750 }
3751
3752 if (ret) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003753 for (i = 0; i < ctx->nr_user_files; i++) {
3754 struct file *file;
Jens Axboe6b063142019-01-10 22:13:58 -07003755
Jens Axboe65e19f52019-10-26 07:20:21 -06003756 file = io_file_from_index(ctx, i);
3757 if (file)
3758 fput(file);
3759 }
3760 for (i = 0; i < nr_tables; i++)
3761 kfree(ctx->file_table[i].files);
3762
3763 kfree(ctx->file_table);
3764 ctx->file_table = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003765 ctx->nr_user_files = 0;
3766 return ret;
3767 }
3768
3769 ret = io_sqe_files_scm(ctx);
3770 if (ret)
3771 io_sqe_files_unregister(ctx);
3772
3773 return ret;
3774}
3775
Jens Axboec3a31e62019-10-03 13:59:56 -06003776static void io_sqe_file_unregister(struct io_ring_ctx *ctx, int index)
3777{
3778#if defined(CONFIG_UNIX)
Jens Axboe65e19f52019-10-26 07:20:21 -06003779 struct file *file = io_file_from_index(ctx, index);
Jens Axboec3a31e62019-10-03 13:59:56 -06003780 struct sock *sock = ctx->ring_sock->sk;
3781 struct sk_buff_head list, *head = &sock->sk_receive_queue;
3782 struct sk_buff *skb;
3783 int i;
3784
3785 __skb_queue_head_init(&list);
3786
3787 /*
3788 * Find the skb that holds this file in its SCM_RIGHTS. When found,
3789 * remove this entry and rearrange the file array.
3790 */
3791 skb = skb_dequeue(head);
3792 while (skb) {
3793 struct scm_fp_list *fp;
3794
3795 fp = UNIXCB(skb).fp;
3796 for (i = 0; i < fp->count; i++) {
3797 int left;
3798
3799 if (fp->fp[i] != file)
3800 continue;
3801
3802 unix_notinflight(fp->user, fp->fp[i]);
3803 left = fp->count - 1 - i;
3804 if (left) {
3805 memmove(&fp->fp[i], &fp->fp[i + 1],
3806 left * sizeof(struct file *));
3807 }
3808 fp->count--;
3809 if (!fp->count) {
3810 kfree_skb(skb);
3811 skb = NULL;
3812 } else {
3813 __skb_queue_tail(&list, skb);
3814 }
3815 fput(file);
3816 file = NULL;
3817 break;
3818 }
3819
3820 if (!file)
3821 break;
3822
3823 __skb_queue_tail(&list, skb);
3824
3825 skb = skb_dequeue(head);
3826 }
3827
3828 if (skb_peek(&list)) {
3829 spin_lock_irq(&head->lock);
3830 while ((skb = __skb_dequeue(&list)) != NULL)
3831 __skb_queue_tail(head, skb);
3832 spin_unlock_irq(&head->lock);
3833 }
3834#else
Jens Axboe65e19f52019-10-26 07:20:21 -06003835 fput(io_file_from_index(ctx, index));
Jens Axboec3a31e62019-10-03 13:59:56 -06003836#endif
3837}
3838
3839static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
3840 int index)
3841{
3842#if defined(CONFIG_UNIX)
3843 struct sock *sock = ctx->ring_sock->sk;
3844 struct sk_buff_head *head = &sock->sk_receive_queue;
3845 struct sk_buff *skb;
3846
3847 /*
3848 * See if we can merge this file into an existing skb SCM_RIGHTS
3849 * file set. If there's no room, fall back to allocating a new skb
3850 * and filling it in.
3851 */
3852 spin_lock_irq(&head->lock);
3853 skb = skb_peek(head);
3854 if (skb) {
3855 struct scm_fp_list *fpl = UNIXCB(skb).fp;
3856
3857 if (fpl->count < SCM_MAX_FD) {
3858 __skb_unlink(skb, head);
3859 spin_unlock_irq(&head->lock);
3860 fpl->fp[fpl->count] = get_file(file);
3861 unix_inflight(fpl->user, fpl->fp[fpl->count]);
3862 fpl->count++;
3863 spin_lock_irq(&head->lock);
3864 __skb_queue_head(head, skb);
3865 } else {
3866 skb = NULL;
3867 }
3868 }
3869 spin_unlock_irq(&head->lock);
3870
3871 if (skb) {
3872 fput(file);
3873 return 0;
3874 }
3875
3876 return __io_sqe_files_scm(ctx, 1, index);
3877#else
3878 return 0;
3879#endif
3880}
3881
3882static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
3883 unsigned nr_args)
3884{
3885 struct io_uring_files_update up;
3886 __s32 __user *fds;
3887 int fd, i, err;
3888 __u32 done;
3889
Jens Axboe65e19f52019-10-26 07:20:21 -06003890 if (!ctx->file_table)
Jens Axboec3a31e62019-10-03 13:59:56 -06003891 return -ENXIO;
3892 if (!nr_args)
3893 return -EINVAL;
3894 if (copy_from_user(&up, arg, sizeof(up)))
3895 return -EFAULT;
3896 if (check_add_overflow(up.offset, nr_args, &done))
3897 return -EOVERFLOW;
3898 if (done > ctx->nr_user_files)
3899 return -EINVAL;
3900
3901 done = 0;
3902 fds = (__s32 __user *) up.fds;
3903 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003904 struct fixed_file_table *table;
3905 unsigned index;
3906
Jens Axboec3a31e62019-10-03 13:59:56 -06003907 err = 0;
3908 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
3909 err = -EFAULT;
3910 break;
3911 }
3912 i = array_index_nospec(up.offset, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06003913 table = &ctx->file_table[i >> IORING_FILE_TABLE_SHIFT];
3914 index = i & IORING_FILE_TABLE_MASK;
3915 if (table->files[index]) {
Jens Axboec3a31e62019-10-03 13:59:56 -06003916 io_sqe_file_unregister(ctx, i);
Jens Axboe65e19f52019-10-26 07:20:21 -06003917 table->files[index] = NULL;
Jens Axboec3a31e62019-10-03 13:59:56 -06003918 }
3919 if (fd != -1) {
3920 struct file *file;
3921
3922 file = fget(fd);
3923 if (!file) {
3924 err = -EBADF;
3925 break;
3926 }
3927 /*
3928 * Don't allow io_uring instances to be registered. If
3929 * UNIX isn't enabled, then this causes a reference
3930 * cycle and this instance can never get freed. If UNIX
3931 * is enabled we'll handle it just fine, but there's
3932 * still no point in allowing a ring fd as it doesn't
3933 * support regular read/write anyway.
3934 */
3935 if (file->f_op == &io_uring_fops) {
3936 fput(file);
3937 err = -EBADF;
3938 break;
3939 }
Jens Axboe65e19f52019-10-26 07:20:21 -06003940 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06003941 err = io_sqe_file_register(ctx, file, i);
3942 if (err)
3943 break;
3944 }
3945 nr_args--;
3946 done++;
3947 up.offset++;
3948 }
3949
3950 return done ? done : err;
3951}
3952
Jens Axboe7d723062019-11-12 22:31:31 -07003953static void io_put_work(struct io_wq_work *work)
3954{
3955 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
3956
3957 io_put_req(req);
3958}
3959
3960static void io_get_work(struct io_wq_work *work)
3961{
3962 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
3963
3964 refcount_inc(&req->refs);
3965}
3966
Jens Axboe6c271ce2019-01-10 11:22:30 -07003967static int io_sq_offload_start(struct io_ring_ctx *ctx,
3968 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003969{
Jens Axboe576a3472019-11-25 08:49:20 -07003970 struct io_wq_data data;
Jens Axboe561fb042019-10-24 07:25:42 -06003971 unsigned concurrency;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003972 int ret;
3973
Jens Axboe6c271ce2019-01-10 11:22:30 -07003974 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003975 mmgrab(current->mm);
3976 ctx->sqo_mm = current->mm;
3977
Jens Axboe6c271ce2019-01-10 11:22:30 -07003978 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06003979 ret = -EPERM;
3980 if (!capable(CAP_SYS_ADMIN))
3981 goto err;
3982
Jens Axboe917257d2019-04-13 09:28:55 -06003983 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
3984 if (!ctx->sq_thread_idle)
3985 ctx->sq_thread_idle = HZ;
3986
Jens Axboe6c271ce2019-01-10 11:22:30 -07003987 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06003988 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003989
Jens Axboe917257d2019-04-13 09:28:55 -06003990 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06003991 if (cpu >= nr_cpu_ids)
3992 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08003993 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06003994 goto err;
3995
Jens Axboe6c271ce2019-01-10 11:22:30 -07003996 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
3997 ctx, cpu,
3998 "io_uring-sq");
3999 } else {
4000 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
4001 "io_uring-sq");
4002 }
4003 if (IS_ERR(ctx->sqo_thread)) {
4004 ret = PTR_ERR(ctx->sqo_thread);
4005 ctx->sqo_thread = NULL;
4006 goto err;
4007 }
4008 wake_up_process(ctx->sqo_thread);
4009 } else if (p->flags & IORING_SETUP_SQ_AFF) {
4010 /* Can't have SQ_AFF without SQPOLL */
4011 ret = -EINVAL;
4012 goto err;
4013 }
4014
Jens Axboe576a3472019-11-25 08:49:20 -07004015 data.mm = ctx->sqo_mm;
4016 data.user = ctx->user;
Jens Axboe181e4482019-11-25 08:52:30 -07004017 data.creds = ctx->creds;
Jens Axboe576a3472019-11-25 08:49:20 -07004018 data.get_work = io_get_work;
4019 data.put_work = io_put_work;
4020
Jens Axboe561fb042019-10-24 07:25:42 -06004021 /* Do QD, or 4 * CPUS, whatever is smallest */
4022 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
Jens Axboe576a3472019-11-25 08:49:20 -07004023 ctx->io_wq = io_wq_create(concurrency, &data);
Jens Axboe975c99a52019-10-30 08:42:56 -06004024 if (IS_ERR(ctx->io_wq)) {
4025 ret = PTR_ERR(ctx->io_wq);
4026 ctx->io_wq = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004027 goto err;
4028 }
4029
4030 return 0;
4031err:
Jens Axboe54a91f32019-09-10 09:15:04 -06004032 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004033 mmdrop(ctx->sqo_mm);
4034 ctx->sqo_mm = NULL;
4035 return ret;
4036}
4037
4038static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
4039{
4040 atomic_long_sub(nr_pages, &user->locked_vm);
4041}
4042
4043static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
4044{
4045 unsigned long page_limit, cur_pages, new_pages;
4046
4047 /* Don't allow more pages than we can safely lock */
4048 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
4049
4050 do {
4051 cur_pages = atomic_long_read(&user->locked_vm);
4052 new_pages = cur_pages + nr_pages;
4053 if (new_pages > page_limit)
4054 return -ENOMEM;
4055 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
4056 new_pages) != cur_pages);
4057
4058 return 0;
4059}
4060
4061static void io_mem_free(void *ptr)
4062{
Mark Rutland52e04ef2019-04-30 17:30:21 +01004063 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004064
Mark Rutland52e04ef2019-04-30 17:30:21 +01004065 if (!ptr)
4066 return;
4067
4068 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004069 if (put_page_testzero(page))
4070 free_compound_page(page);
4071}
4072
4073static void *io_mem_alloc(size_t size)
4074{
4075 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
4076 __GFP_NORETRY;
4077
4078 return (void *) __get_free_pages(gfp_flags, get_order(size));
4079}
4080
Hristo Venev75b28af2019-08-26 17:23:46 +00004081static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
4082 size_t *sq_offset)
4083{
4084 struct io_rings *rings;
4085 size_t off, sq_array_size;
4086
4087 off = struct_size(rings, cqes, cq_entries);
4088 if (off == SIZE_MAX)
4089 return SIZE_MAX;
4090
4091#ifdef CONFIG_SMP
4092 off = ALIGN(off, SMP_CACHE_BYTES);
4093 if (off == 0)
4094 return SIZE_MAX;
4095#endif
4096
4097 sq_array_size = array_size(sizeof(u32), sq_entries);
4098 if (sq_array_size == SIZE_MAX)
4099 return SIZE_MAX;
4100
4101 if (check_add_overflow(off, sq_array_size, &off))
4102 return SIZE_MAX;
4103
4104 if (sq_offset)
4105 *sq_offset = off;
4106
4107 return off;
4108}
4109
Jens Axboe2b188cc2019-01-07 10:46:33 -07004110static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
4111{
Hristo Venev75b28af2019-08-26 17:23:46 +00004112 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004113
Hristo Venev75b28af2019-08-26 17:23:46 +00004114 pages = (size_t)1 << get_order(
4115 rings_size(sq_entries, cq_entries, NULL));
4116 pages += (size_t)1 << get_order(
4117 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07004118
Hristo Venev75b28af2019-08-26 17:23:46 +00004119 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004120}
4121
Jens Axboeedafcce2019-01-09 09:16:05 -07004122static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
4123{
4124 int i, j;
4125
4126 if (!ctx->user_bufs)
4127 return -ENXIO;
4128
4129 for (i = 0; i < ctx->nr_user_bufs; i++) {
4130 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
4131
4132 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbard27c4d3a2019-08-04 19:32:06 -07004133 put_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07004134
4135 if (ctx->account_mem)
4136 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004137 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07004138 imu->nr_bvecs = 0;
4139 }
4140
4141 kfree(ctx->user_bufs);
4142 ctx->user_bufs = NULL;
4143 ctx->nr_user_bufs = 0;
4144 return 0;
4145}
4146
4147static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
4148 void __user *arg, unsigned index)
4149{
4150 struct iovec __user *src;
4151
4152#ifdef CONFIG_COMPAT
4153 if (ctx->compat) {
4154 struct compat_iovec __user *ciovs;
4155 struct compat_iovec ciov;
4156
4157 ciovs = (struct compat_iovec __user *) arg;
4158 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
4159 return -EFAULT;
4160
4161 dst->iov_base = (void __user *) (unsigned long) ciov.iov_base;
4162 dst->iov_len = ciov.iov_len;
4163 return 0;
4164 }
4165#endif
4166 src = (struct iovec __user *) arg;
4167 if (copy_from_user(dst, &src[index], sizeof(*dst)))
4168 return -EFAULT;
4169 return 0;
4170}
4171
4172static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
4173 unsigned nr_args)
4174{
4175 struct vm_area_struct **vmas = NULL;
4176 struct page **pages = NULL;
4177 int i, j, got_pages = 0;
4178 int ret = -EINVAL;
4179
4180 if (ctx->user_bufs)
4181 return -EBUSY;
4182 if (!nr_args || nr_args > UIO_MAXIOV)
4183 return -EINVAL;
4184
4185 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
4186 GFP_KERNEL);
4187 if (!ctx->user_bufs)
4188 return -ENOMEM;
4189
4190 for (i = 0; i < nr_args; i++) {
4191 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
4192 unsigned long off, start, end, ubuf;
4193 int pret, nr_pages;
4194 struct iovec iov;
4195 size_t size;
4196
4197 ret = io_copy_iov(ctx, &iov, arg, i);
4198 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03004199 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07004200
4201 /*
4202 * Don't impose further limits on the size and buffer
4203 * constraints here, we'll -EINVAL later when IO is
4204 * submitted if they are wrong.
4205 */
4206 ret = -EFAULT;
4207 if (!iov.iov_base || !iov.iov_len)
4208 goto err;
4209
4210 /* arbitrary limit, but we need something */
4211 if (iov.iov_len > SZ_1G)
4212 goto err;
4213
4214 ubuf = (unsigned long) iov.iov_base;
4215 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
4216 start = ubuf >> PAGE_SHIFT;
4217 nr_pages = end - start;
4218
4219 if (ctx->account_mem) {
4220 ret = io_account_mem(ctx->user, nr_pages);
4221 if (ret)
4222 goto err;
4223 }
4224
4225 ret = 0;
4226 if (!pages || nr_pages > got_pages) {
4227 kfree(vmas);
4228 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004229 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07004230 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004231 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07004232 sizeof(struct vm_area_struct *),
4233 GFP_KERNEL);
4234 if (!pages || !vmas) {
4235 ret = -ENOMEM;
4236 if (ctx->account_mem)
4237 io_unaccount_mem(ctx->user, nr_pages);
4238 goto err;
4239 }
4240 got_pages = nr_pages;
4241 }
4242
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004243 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07004244 GFP_KERNEL);
4245 ret = -ENOMEM;
4246 if (!imu->bvec) {
4247 if (ctx->account_mem)
4248 io_unaccount_mem(ctx->user, nr_pages);
4249 goto err;
4250 }
4251
4252 ret = 0;
4253 down_read(&current->mm->mmap_sem);
Ira Weiny932f4a62019-05-13 17:17:03 -07004254 pret = get_user_pages(ubuf, nr_pages,
4255 FOLL_WRITE | FOLL_LONGTERM,
4256 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07004257 if (pret == nr_pages) {
4258 /* don't support file backed memory */
4259 for (j = 0; j < nr_pages; j++) {
4260 struct vm_area_struct *vma = vmas[j];
4261
4262 if (vma->vm_file &&
4263 !is_file_hugepages(vma->vm_file)) {
4264 ret = -EOPNOTSUPP;
4265 break;
4266 }
4267 }
4268 } else {
4269 ret = pret < 0 ? pret : -EFAULT;
4270 }
4271 up_read(&current->mm->mmap_sem);
4272 if (ret) {
4273 /*
4274 * if we did partial map, or found file backed vmas,
4275 * release any pages we did get
4276 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07004277 if (pret > 0)
4278 put_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07004279 if (ctx->account_mem)
4280 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004281 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07004282 goto err;
4283 }
4284
4285 off = ubuf & ~PAGE_MASK;
4286 size = iov.iov_len;
4287 for (j = 0; j < nr_pages; j++) {
4288 size_t vec_len;
4289
4290 vec_len = min_t(size_t, size, PAGE_SIZE - off);
4291 imu->bvec[j].bv_page = pages[j];
4292 imu->bvec[j].bv_len = vec_len;
4293 imu->bvec[j].bv_offset = off;
4294 off = 0;
4295 size -= vec_len;
4296 }
4297 /* store original address for later verification */
4298 imu->ubuf = ubuf;
4299 imu->len = iov.iov_len;
4300 imu->nr_bvecs = nr_pages;
4301
4302 ctx->nr_user_bufs++;
4303 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004304 kvfree(pages);
4305 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07004306 return 0;
4307err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004308 kvfree(pages);
4309 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07004310 io_sqe_buffer_unregister(ctx);
4311 return ret;
4312}
4313
Jens Axboe9b402842019-04-11 11:45:41 -06004314static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
4315{
4316 __s32 __user *fds = arg;
4317 int fd;
4318
4319 if (ctx->cq_ev_fd)
4320 return -EBUSY;
4321
4322 if (copy_from_user(&fd, fds, sizeof(*fds)))
4323 return -EFAULT;
4324
4325 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
4326 if (IS_ERR(ctx->cq_ev_fd)) {
4327 int ret = PTR_ERR(ctx->cq_ev_fd);
4328 ctx->cq_ev_fd = NULL;
4329 return ret;
4330 }
4331
4332 return 0;
4333}
4334
4335static int io_eventfd_unregister(struct io_ring_ctx *ctx)
4336{
4337 if (ctx->cq_ev_fd) {
4338 eventfd_ctx_put(ctx->cq_ev_fd);
4339 ctx->cq_ev_fd = NULL;
4340 return 0;
4341 }
4342
4343 return -ENXIO;
4344}
4345
Jens Axboe2b188cc2019-01-07 10:46:33 -07004346static void io_ring_ctx_free(struct io_ring_ctx *ctx)
4347{
Jens Axboe6b063142019-01-10 22:13:58 -07004348 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004349 if (ctx->sqo_mm)
4350 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07004351
4352 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07004353 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07004354 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06004355 io_eventfd_unregister(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07004356
Jens Axboe2b188cc2019-01-07 10:46:33 -07004357#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07004358 if (ctx->ring_sock) {
4359 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004360 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07004361 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004362#endif
4363
Hristo Venev75b28af2019-08-26 17:23:46 +00004364 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004365 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004366
4367 percpu_ref_exit(&ctx->refs);
4368 if (ctx->account_mem)
4369 io_unaccount_mem(ctx->user,
4370 ring_pages(ctx->sq_entries, ctx->cq_entries));
4371 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07004372 put_cred(ctx->creds);
Jens Axboe206aefd2019-11-07 18:27:42 -07004373 kfree(ctx->completions);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07004374 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004375 kfree(ctx);
4376}
4377
4378static __poll_t io_uring_poll(struct file *file, poll_table *wait)
4379{
4380 struct io_ring_ctx *ctx = file->private_data;
4381 __poll_t mask = 0;
4382
4383 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02004384 /*
4385 * synchronizes with barrier from wq_has_sleeper call in
4386 * io_commit_cqring
4387 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004388 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00004389 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
4390 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004391 mask |= EPOLLOUT | EPOLLWRNORM;
yangerkundaa5de52019-09-24 20:53:34 +08004392 if (READ_ONCE(ctx->rings->cq.head) != ctx->cached_cq_tail)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004393 mask |= EPOLLIN | EPOLLRDNORM;
4394
4395 return mask;
4396}
4397
4398static int io_uring_fasync(int fd, struct file *file, int on)
4399{
4400 struct io_ring_ctx *ctx = file->private_data;
4401
4402 return fasync_helper(fd, file, on, &ctx->cq_fasync);
4403}
4404
4405static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
4406{
4407 mutex_lock(&ctx->uring_lock);
4408 percpu_ref_kill(&ctx->refs);
4409 mutex_unlock(&ctx->uring_lock);
4410
Jens Axboe5262f562019-09-17 12:26:57 -06004411 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004412 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06004413
4414 if (ctx->io_wq)
4415 io_wq_cancel_all(ctx->io_wq);
4416
Jens Axboedef596e2019-01-09 08:59:42 -07004417 io_iopoll_reap_events(ctx);
Jens Axboe15dff282019-11-13 09:09:23 -07004418 /* if we failed setting up the ctx, we might not have any rings */
4419 if (ctx->rings)
4420 io_cqring_overflow_flush(ctx, true);
Jens Axboe206aefd2019-11-07 18:27:42 -07004421 wait_for_completion(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004422 io_ring_ctx_free(ctx);
4423}
4424
4425static int io_uring_release(struct inode *inode, struct file *file)
4426{
4427 struct io_ring_ctx *ctx = file->private_data;
4428
4429 file->private_data = NULL;
4430 io_ring_ctx_wait_and_kill(ctx);
4431 return 0;
4432}
4433
Jens Axboefcb323c2019-10-24 12:39:47 -06004434static void io_uring_cancel_files(struct io_ring_ctx *ctx,
4435 struct files_struct *files)
4436{
4437 struct io_kiocb *req;
4438 DEFINE_WAIT(wait);
4439
4440 while (!list_empty_careful(&ctx->inflight_list)) {
Jens Axboe768134d2019-11-10 20:30:53 -07004441 struct io_kiocb *cancel_req = NULL;
Jens Axboefcb323c2019-10-24 12:39:47 -06004442
4443 spin_lock_irq(&ctx->inflight_lock);
4444 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07004445 if (req->work.files != files)
4446 continue;
4447 /* req is being completed, ignore */
4448 if (!refcount_inc_not_zero(&req->refs))
4449 continue;
4450 cancel_req = req;
4451 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06004452 }
Jens Axboe768134d2019-11-10 20:30:53 -07004453 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06004454 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07004455 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06004456 spin_unlock_irq(&ctx->inflight_lock);
4457
Jens Axboe768134d2019-11-10 20:30:53 -07004458 /* We need to keep going until we don't find a matching req */
4459 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06004460 break;
Bob Liu2f6d9b92019-11-13 18:06:24 +08004461
4462 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
4463 io_put_req(cancel_req);
Jens Axboefcb323c2019-10-24 12:39:47 -06004464 schedule();
4465 }
Jens Axboe768134d2019-11-10 20:30:53 -07004466 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06004467}
4468
4469static int io_uring_flush(struct file *file, void *data)
4470{
4471 struct io_ring_ctx *ctx = file->private_data;
4472
4473 io_uring_cancel_files(ctx, data);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004474 if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) {
4475 io_cqring_overflow_flush(ctx, true);
Jens Axboefcb323c2019-10-24 12:39:47 -06004476 io_wq_cancel_all(ctx->io_wq);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004477 }
Jens Axboefcb323c2019-10-24 12:39:47 -06004478 return 0;
4479}
4480
Jens Axboe2b188cc2019-01-07 10:46:33 -07004481static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
4482{
4483 loff_t offset = (loff_t) vma->vm_pgoff << PAGE_SHIFT;
4484 unsigned long sz = vma->vm_end - vma->vm_start;
4485 struct io_ring_ctx *ctx = file->private_data;
4486 unsigned long pfn;
4487 struct page *page;
4488 void *ptr;
4489
4490 switch (offset) {
4491 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00004492 case IORING_OFF_CQ_RING:
4493 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004494 break;
4495 case IORING_OFF_SQES:
4496 ptr = ctx->sq_sqes;
4497 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004498 default:
4499 return -EINVAL;
4500 }
4501
4502 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07004503 if (sz > page_size(page))
Jens Axboe2b188cc2019-01-07 10:46:33 -07004504 return -EINVAL;
4505
4506 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
4507 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
4508}
4509
4510SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
4511 u32, min_complete, u32, flags, const sigset_t __user *, sig,
4512 size_t, sigsz)
4513{
4514 struct io_ring_ctx *ctx;
4515 long ret = -EBADF;
4516 int submitted = 0;
4517 struct fd f;
4518
Jens Axboe6c271ce2019-01-10 11:22:30 -07004519 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07004520 return -EINVAL;
4521
4522 f = fdget(fd);
4523 if (!f.file)
4524 return -EBADF;
4525
4526 ret = -EOPNOTSUPP;
4527 if (f.file->f_op != &io_uring_fops)
4528 goto out_fput;
4529
4530 ret = -ENXIO;
4531 ctx = f.file->private_data;
4532 if (!percpu_ref_tryget(&ctx->refs))
4533 goto out_fput;
4534
Jens Axboe6c271ce2019-01-10 11:22:30 -07004535 /*
4536 * For SQ polling, the thread will do all submissions and completions.
4537 * Just return the requested submit count, and wake the thread if
4538 * we were asked to.
4539 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06004540 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004541 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07004542 if (!list_empty_careful(&ctx->cq_overflow_list))
4543 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004544 if (flags & IORING_ENTER_SQ_WAKEUP)
4545 wake_up(&ctx->sqo_wait);
4546 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06004547 } else if (to_submit) {
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004548 struct mm_struct *cur_mm;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004549
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004550 to_submit = min(to_submit, ctx->sq_entries);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004551 mutex_lock(&ctx->uring_lock);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004552 /* already have mm, so io_submit_sqes() won't try to grab it */
4553 cur_mm = ctx->sqo_mm;
4554 submitted = io_submit_sqes(ctx, to_submit, f.file, fd,
4555 &cur_mm, false);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004556 mutex_unlock(&ctx->uring_lock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004557 }
4558 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07004559 unsigned nr_events = 0;
4560
Jens Axboe2b188cc2019-01-07 10:46:33 -07004561 min_complete = min(min_complete, ctx->cq_entries);
4562
Jens Axboedef596e2019-01-09 08:59:42 -07004563 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07004564 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07004565 } else {
4566 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
4567 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004568 }
4569
Pavel Begunkov6805b322019-10-08 02:18:42 +03004570 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004571out_fput:
4572 fdput(f);
4573 return submitted ? submitted : ret;
4574}
4575
4576static const struct file_operations io_uring_fops = {
4577 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06004578 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07004579 .mmap = io_uring_mmap,
4580 .poll = io_uring_poll,
4581 .fasync = io_uring_fasync,
4582};
4583
4584static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
4585 struct io_uring_params *p)
4586{
Hristo Venev75b28af2019-08-26 17:23:46 +00004587 struct io_rings *rings;
4588 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004589
Hristo Venev75b28af2019-08-26 17:23:46 +00004590 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
4591 if (size == SIZE_MAX)
4592 return -EOVERFLOW;
4593
4594 rings = io_mem_alloc(size);
4595 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004596 return -ENOMEM;
4597
Hristo Venev75b28af2019-08-26 17:23:46 +00004598 ctx->rings = rings;
4599 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
4600 rings->sq_ring_mask = p->sq_entries - 1;
4601 rings->cq_ring_mask = p->cq_entries - 1;
4602 rings->sq_ring_entries = p->sq_entries;
4603 rings->cq_ring_entries = p->cq_entries;
4604 ctx->sq_mask = rings->sq_ring_mask;
4605 ctx->cq_mask = rings->cq_ring_mask;
4606 ctx->sq_entries = rings->sq_ring_entries;
4607 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004608
4609 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07004610 if (size == SIZE_MAX) {
4611 io_mem_free(ctx->rings);
4612 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004613 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07004614 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004615
4616 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07004617 if (!ctx->sq_sqes) {
4618 io_mem_free(ctx->rings);
4619 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004620 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07004621 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004622
Jens Axboe2b188cc2019-01-07 10:46:33 -07004623 return 0;
4624}
4625
4626/*
4627 * Allocate an anonymous fd, this is what constitutes the application
4628 * visible backing of an io_uring instance. The application mmaps this
4629 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
4630 * we have to tie this fd to a socket for file garbage collection purposes.
4631 */
4632static int io_uring_get_fd(struct io_ring_ctx *ctx)
4633{
4634 struct file *file;
4635 int ret;
4636
4637#if defined(CONFIG_UNIX)
4638 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
4639 &ctx->ring_sock);
4640 if (ret)
4641 return ret;
4642#endif
4643
4644 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
4645 if (ret < 0)
4646 goto err;
4647
4648 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
4649 O_RDWR | O_CLOEXEC);
4650 if (IS_ERR(file)) {
4651 put_unused_fd(ret);
4652 ret = PTR_ERR(file);
4653 goto err;
4654 }
4655
4656#if defined(CONFIG_UNIX)
4657 ctx->ring_sock->file = file;
Jens Axboe6b063142019-01-10 22:13:58 -07004658 ctx->ring_sock->sk->sk_user_data = ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004659#endif
4660 fd_install(ret, file);
4661 return ret;
4662err:
4663#if defined(CONFIG_UNIX)
4664 sock_release(ctx->ring_sock);
4665 ctx->ring_sock = NULL;
4666#endif
4667 return ret;
4668}
4669
4670static int io_uring_create(unsigned entries, struct io_uring_params *p)
4671{
4672 struct user_struct *user = NULL;
4673 struct io_ring_ctx *ctx;
4674 bool account_mem;
4675 int ret;
4676
4677 if (!entries || entries > IORING_MAX_ENTRIES)
4678 return -EINVAL;
4679
4680 /*
4681 * Use twice as many entries for the CQ ring. It's possible for the
4682 * application to drive a higher depth than the size of the SQ ring,
4683 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06004684 * some flexibility in overcommitting a bit. If the application has
4685 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
4686 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07004687 */
4688 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06004689 if (p->flags & IORING_SETUP_CQSIZE) {
4690 /*
4691 * If IORING_SETUP_CQSIZE is set, we do the same roundup
4692 * to a power-of-two, if it isn't already. We do NOT impose
4693 * any cq vs sq ring sizing.
4694 */
4695 if (p->cq_entries < p->sq_entries || p->cq_entries > IORING_MAX_CQ_ENTRIES)
4696 return -EINVAL;
4697 p->cq_entries = roundup_pow_of_two(p->cq_entries);
4698 } else {
4699 p->cq_entries = 2 * p->sq_entries;
4700 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004701
4702 user = get_uid(current_user());
4703 account_mem = !capable(CAP_IPC_LOCK);
4704
4705 if (account_mem) {
4706 ret = io_account_mem(user,
4707 ring_pages(p->sq_entries, p->cq_entries));
4708 if (ret) {
4709 free_uid(user);
4710 return ret;
4711 }
4712 }
4713
4714 ctx = io_ring_ctx_alloc(p);
4715 if (!ctx) {
4716 if (account_mem)
4717 io_unaccount_mem(user, ring_pages(p->sq_entries,
4718 p->cq_entries));
4719 free_uid(user);
4720 return -ENOMEM;
4721 }
4722 ctx->compat = in_compat_syscall();
4723 ctx->account_mem = account_mem;
4724 ctx->user = user;
Jens Axboe181e4482019-11-25 08:52:30 -07004725 ctx->creds = prepare_creds();
Jens Axboe2b188cc2019-01-07 10:46:33 -07004726
4727 ret = io_allocate_scq_urings(ctx, p);
4728 if (ret)
4729 goto err;
4730
Jens Axboe6c271ce2019-01-10 11:22:30 -07004731 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004732 if (ret)
4733 goto err;
4734
Jens Axboe2b188cc2019-01-07 10:46:33 -07004735 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00004736 p->sq_off.head = offsetof(struct io_rings, sq.head);
4737 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
4738 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
4739 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
4740 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
4741 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
4742 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004743
4744 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00004745 p->cq_off.head = offsetof(struct io_rings, cq.head);
4746 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
4747 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
4748 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
4749 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
4750 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06004751
Jens Axboe044c1ab2019-10-28 09:15:33 -06004752 /*
4753 * Install ring fd as the very last thing, so we don't risk someone
4754 * having closed it before we finish setup
4755 */
4756 ret = io_uring_get_fd(ctx);
4757 if (ret < 0)
4758 goto err;
4759
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004760 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004761 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004762 return ret;
4763err:
4764 io_ring_ctx_wait_and_kill(ctx);
4765 return ret;
4766}
4767
4768/*
4769 * Sets up an aio uring context, and returns the fd. Applications asks for a
4770 * ring size, we return the actual sq/cq ring sizes (among other things) in the
4771 * params structure passed in.
4772 */
4773static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
4774{
4775 struct io_uring_params p;
4776 long ret;
4777 int i;
4778
4779 if (copy_from_user(&p, params, sizeof(p)))
4780 return -EFAULT;
4781 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
4782 if (p.resv[i])
4783 return -EINVAL;
4784 }
4785
Jens Axboe6c271ce2019-01-10 11:22:30 -07004786 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe33a107f2019-10-04 12:10:03 -06004787 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE))
Jens Axboe2b188cc2019-01-07 10:46:33 -07004788 return -EINVAL;
4789
4790 ret = io_uring_create(entries, &p);
4791 if (ret < 0)
4792 return ret;
4793
4794 if (copy_to_user(params, &p, sizeof(p)))
4795 return -EFAULT;
4796
4797 return ret;
4798}
4799
4800SYSCALL_DEFINE2(io_uring_setup, u32, entries,
4801 struct io_uring_params __user *, params)
4802{
4803 return io_uring_setup(entries, params);
4804}
4805
Jens Axboeedafcce2019-01-09 09:16:05 -07004806static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
4807 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06004808 __releases(ctx->uring_lock)
4809 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07004810{
4811 int ret;
4812
Jens Axboe35fa71a2019-04-22 10:23:23 -06004813 /*
4814 * We're inside the ring mutex, if the ref is already dying, then
4815 * someone else killed the ctx or is already going through
4816 * io_uring_register().
4817 */
4818 if (percpu_ref_is_dying(&ctx->refs))
4819 return -ENXIO;
4820
Jens Axboeedafcce2019-01-09 09:16:05 -07004821 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06004822
4823 /*
4824 * Drop uring mutex before waiting for references to exit. If another
4825 * thread is currently inside io_uring_enter() it might need to grab
4826 * the uring_lock to make progress. If we hold it here across the drain
4827 * wait, then we can deadlock. It's safe to drop the mutex here, since
4828 * no new references will come in after we've killed the percpu ref.
4829 */
4830 mutex_unlock(&ctx->uring_lock);
Jens Axboe206aefd2019-11-07 18:27:42 -07004831 wait_for_completion(&ctx->completions[0]);
Jens Axboeb19062a2019-04-15 10:49:38 -06004832 mutex_lock(&ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07004833
4834 switch (opcode) {
4835 case IORING_REGISTER_BUFFERS:
4836 ret = io_sqe_buffer_register(ctx, arg, nr_args);
4837 break;
4838 case IORING_UNREGISTER_BUFFERS:
4839 ret = -EINVAL;
4840 if (arg || nr_args)
4841 break;
4842 ret = io_sqe_buffer_unregister(ctx);
4843 break;
Jens Axboe6b063142019-01-10 22:13:58 -07004844 case IORING_REGISTER_FILES:
4845 ret = io_sqe_files_register(ctx, arg, nr_args);
4846 break;
4847 case IORING_UNREGISTER_FILES:
4848 ret = -EINVAL;
4849 if (arg || nr_args)
4850 break;
4851 ret = io_sqe_files_unregister(ctx);
4852 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06004853 case IORING_REGISTER_FILES_UPDATE:
4854 ret = io_sqe_files_update(ctx, arg, nr_args);
4855 break;
Jens Axboe9b402842019-04-11 11:45:41 -06004856 case IORING_REGISTER_EVENTFD:
4857 ret = -EINVAL;
4858 if (nr_args != 1)
4859 break;
4860 ret = io_eventfd_register(ctx, arg);
4861 break;
4862 case IORING_UNREGISTER_EVENTFD:
4863 ret = -EINVAL;
4864 if (arg || nr_args)
4865 break;
4866 ret = io_eventfd_unregister(ctx);
4867 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07004868 default:
4869 ret = -EINVAL;
4870 break;
4871 }
4872
4873 /* bring the ctx back to life */
Jens Axboe206aefd2019-11-07 18:27:42 -07004874 reinit_completion(&ctx->completions[0]);
Jens Axboeedafcce2019-01-09 09:16:05 -07004875 percpu_ref_reinit(&ctx->refs);
4876 return ret;
4877}
4878
4879SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
4880 void __user *, arg, unsigned int, nr_args)
4881{
4882 struct io_ring_ctx *ctx;
4883 long ret = -EBADF;
4884 struct fd f;
4885
4886 f = fdget(fd);
4887 if (!f.file)
4888 return -EBADF;
4889
4890 ret = -EOPNOTSUPP;
4891 if (f.file->f_op != &io_uring_fops)
4892 goto out_fput;
4893
4894 ctx = f.file->private_data;
4895
4896 mutex_lock(&ctx->uring_lock);
4897 ret = __io_uring_register(ctx, opcode, arg, nr_args);
4898 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004899 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
4900 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07004901out_fput:
4902 fdput(f);
4903 return ret;
4904}
4905
Jens Axboe2b188cc2019-01-07 10:46:33 -07004906static int __init io_uring_init(void)
4907{
4908 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
4909 return 0;
4910};
4911__initcall(io_uring_init);