blob: a91743e1fa2cf7911e6c80bd3e2fa6bf214ae3ab [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 Axboeaa4c3962019-11-29 10:14:00 -070072#include <linux/highmem.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070073
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020074#define CREATE_TRACE_POINTS
75#include <trace/events/io_uring.h>
76
Jens Axboe2b188cc2019-01-07 10:46:33 -070077#include <uapi/linux/io_uring.h>
78
79#include "internal.h"
Jens Axboe561fb042019-10-24 07:25:42 -060080#include "io-wq.h"
Jens Axboe2b188cc2019-01-07 10:46:33 -070081
Daniel Xu5277dea2019-09-14 14:23:45 -070082#define IORING_MAX_ENTRIES 32768
Jens Axboe33a107f2019-10-04 12:10:03 -060083#define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES)
Jens Axboe65e19f52019-10-26 07:20:21 -060084
85/*
86 * Shift of 9 is 512 entries, or exactly one page on 64-bit archs
87 */
88#define IORING_FILE_TABLE_SHIFT 9
89#define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT)
90#define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1)
91#define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE)
Jens Axboe2b188cc2019-01-07 10:46:33 -070092
93struct io_uring {
94 u32 head ____cacheline_aligned_in_smp;
95 u32 tail ____cacheline_aligned_in_smp;
96};
97
Stefan Bühler1e84b972019-04-24 23:54:16 +020098/*
Hristo Venev75b28af2019-08-26 17:23:46 +000099 * This data is shared with the application through the mmap at offsets
100 * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200101 *
102 * The offsets to the member fields are published through struct
103 * io_sqring_offsets when calling io_uring_setup.
104 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000105struct io_rings {
Stefan Bühler1e84b972019-04-24 23:54:16 +0200106 /*
107 * Head and tail offsets into the ring; the offsets need to be
108 * masked to get valid indices.
109 *
Hristo Venev75b28af2019-08-26 17:23:46 +0000110 * The kernel controls head of the sq ring and the tail of the cq ring,
111 * and the application controls tail of the sq ring and the head of the
112 * cq ring.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200113 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000114 struct io_uring sq, cq;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200115 /*
Hristo Venev75b28af2019-08-26 17:23:46 +0000116 * Bitmasks to apply to head and tail offsets (constant, equals
Stefan Bühler1e84b972019-04-24 23:54:16 +0200117 * ring_entries - 1)
118 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000119 u32 sq_ring_mask, cq_ring_mask;
120 /* Ring sizes (constant, power of 2) */
121 u32 sq_ring_entries, cq_ring_entries;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200122 /*
123 * Number of invalid entries dropped by the kernel due to
124 * invalid index stored in array
125 *
126 * Written by the kernel, shouldn't be modified by the
127 * application (i.e. get number of "new events" by comparing to
128 * cached value).
129 *
130 * After a new SQ head value was read by the application this
131 * counter includes all submissions that were dropped reaching
132 * the new SQ head (and possibly more).
133 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000134 u32 sq_dropped;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200135 /*
136 * Runtime flags
137 *
138 * Written by the kernel, shouldn't be modified by the
139 * application.
140 *
141 * The application needs a full memory barrier before checking
142 * for IORING_SQ_NEED_WAKEUP after updating the sq tail.
143 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000144 u32 sq_flags;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200145 /*
146 * Number of completion events lost because the queue was full;
147 * this should be avoided by the application by making sure
148 * there are not more requests pending thatn there is space in
149 * the completion queue.
150 *
151 * Written by the kernel, shouldn't be modified by the
152 * application (i.e. get number of "new events" by comparing to
153 * cached value).
154 *
155 * As completion events come in out of order this counter is not
156 * ordered with any other data.
157 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000158 u32 cq_overflow;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200159 /*
160 * Ring buffer of completion events.
161 *
162 * The kernel writes completion events fresh every time they are
163 * produced, so the application is allowed to modify pending
164 * entries.
165 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000166 struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700167};
168
Jens Axboeedafcce2019-01-09 09:16:05 -0700169struct io_mapped_ubuf {
170 u64 ubuf;
171 size_t len;
172 struct bio_vec *bvec;
173 unsigned int nr_bvecs;
174};
175
Jens Axboe65e19f52019-10-26 07:20:21 -0600176struct fixed_file_table {
177 struct file **files;
Jens Axboe31b51512019-01-18 22:56:34 -0700178};
179
Jens Axboe2b188cc2019-01-07 10:46:33 -0700180struct io_ring_ctx {
181 struct {
182 struct percpu_ref refs;
183 } ____cacheline_aligned_in_smp;
184
185 struct {
186 unsigned int flags;
187 bool compat;
188 bool account_mem;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700189 bool cq_overflow_flushed;
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +0300190 bool drain_next;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700191
Hristo Venev75b28af2019-08-26 17:23:46 +0000192 /*
193 * Ring buffer of indices into array of io_uring_sqe, which is
194 * mmapped by the application using the IORING_OFF_SQES offset.
195 *
196 * This indirection could e.g. be used to assign fixed
197 * io_uring_sqe entries to operations and only submit them to
198 * the queue when needed.
199 *
200 * The kernel modifies neither the indices array nor the entries
201 * array.
202 */
203 u32 *sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700204 unsigned cached_sq_head;
205 unsigned sq_entries;
206 unsigned sq_mask;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700207 unsigned sq_thread_idle;
Jens Axboe498ccd92019-10-25 10:04:25 -0600208 unsigned cached_sq_dropped;
Jens Axboe206aefd2019-11-07 18:27:42 -0700209 atomic_t cached_cq_overflow;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700210 struct io_uring_sqe *sq_sqes;
Jens Axboede0617e2019-04-06 21:51:27 -0600211
212 struct list_head defer_list;
Jens Axboe5262f562019-09-17 12:26:57 -0600213 struct list_head timeout_list;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700214 struct list_head cq_overflow_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700215
Jens Axboefcb323c2019-10-24 12:39:47 -0600216 wait_queue_head_t inflight_wait;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700217 } ____cacheline_aligned_in_smp;
218
Hristo Venev75b28af2019-08-26 17:23:46 +0000219 struct io_rings *rings;
220
Jens Axboe2b188cc2019-01-07 10:46:33 -0700221 /* IO offload */
Jens Axboe561fb042019-10-24 07:25:42 -0600222 struct io_wq *io_wq;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700223 struct task_struct *sqo_thread; /* if using sq thread polling */
224 struct mm_struct *sqo_mm;
225 wait_queue_head_t sqo_wait;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700226
Jens Axboe6b063142019-01-10 22:13:58 -0700227 /*
228 * If used, fixed file set. Writers must ensure that ->refs is dead,
229 * readers must ensure that ->refs is alive as long as the file* is
230 * used. Only updated through io_uring_register(2).
231 */
Jens Axboe65e19f52019-10-26 07:20:21 -0600232 struct fixed_file_table *file_table;
Jens Axboe6b063142019-01-10 22:13:58 -0700233 unsigned nr_user_files;
234
Jens Axboeedafcce2019-01-09 09:16:05 -0700235 /* if used, fixed mapped user buffers */
236 unsigned nr_user_bufs;
237 struct io_mapped_ubuf *user_bufs;
238
Jens Axboe2b188cc2019-01-07 10:46:33 -0700239 struct user_struct *user;
240
Jens Axboe0b8c0ec2019-12-02 08:50:00 -0700241 const struct cred *creds;
Jens Axboe181e4482019-11-25 08:52:30 -0700242
Jens Axboe206aefd2019-11-07 18:27:42 -0700243 /* 0 is for ctx quiesce/reinit/free, 1 is for sqo_thread started */
244 struct completion *completions;
245
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700246 /* if all else fails... */
247 struct io_kiocb *fallback_req;
248
Jens Axboe206aefd2019-11-07 18:27:42 -0700249#if defined(CONFIG_UNIX)
250 struct socket *ring_sock;
251#endif
252
253 struct {
254 unsigned cached_cq_tail;
255 unsigned cq_entries;
256 unsigned cq_mask;
257 atomic_t cq_timeouts;
258 struct wait_queue_head cq_wait;
259 struct fasync_struct *cq_fasync;
260 struct eventfd_ctx *cq_ev_fd;
261 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700262
263 struct {
264 struct mutex uring_lock;
265 wait_queue_head_t wait;
266 } ____cacheline_aligned_in_smp;
267
268 struct {
269 spinlock_t completion_lock;
Jens Axboedef596e2019-01-09 08:59:42 -0700270 bool poll_multi_file;
271 /*
272 * ->poll_list is protected by the ctx->uring_lock for
273 * io_uring instances that don't use IORING_SETUP_SQPOLL.
274 * For SQPOLL, only the single threaded io_sq_thread() will
275 * manipulate the list, hence no extra locking is needed there.
276 */
277 struct list_head poll_list;
Jens Axboeeac406c2019-11-14 12:09:58 -0700278 struct rb_root cancel_tree;
Jens Axboefcb323c2019-10-24 12:39:47 -0600279
280 spinlock_t inflight_lock;
281 struct list_head inflight_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700282 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700283};
284
Jens Axboe09bb8392019-03-13 12:39:28 -0600285/*
286 * First field must be the file pointer in all the
287 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
288 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700289struct io_poll_iocb {
290 struct file *file;
291 struct wait_queue_head *head;
292 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600293 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700294 bool canceled;
Jens Axboee9444752019-11-26 15:02:04 -0700295 struct wait_queue_entry *wait;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700296};
297
Jens Axboead8a48a2019-11-15 08:49:11 -0700298struct io_timeout_data {
299 struct io_kiocb *req;
300 struct hrtimer timer;
301 struct timespec64 ts;
302 enum hrtimer_mode mode;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +0300303 u32 seq_offset;
Jens Axboead8a48a2019-11-15 08:49:11 -0700304};
305
Jens Axboe5262f562019-09-17 12:26:57 -0600306struct io_timeout {
307 struct file *file;
Jens Axboead8a48a2019-11-15 08:49:11 -0700308 struct io_timeout_data *data;
Jens Axboe5262f562019-09-17 12:26:57 -0600309};
310
Jens Axboe09bb8392019-03-13 12:39:28 -0600311/*
312 * NOTE! Each of the iocb union members has the file pointer
313 * as the first entry in their struct definition. So you can
314 * access the file pointer through any of the sub-structs,
315 * or directly as just 'ki_filp' in this struct.
316 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700317struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700318 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600319 struct file *file;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700320 struct kiocb rw;
321 struct io_poll_iocb poll;
Jens Axboe5262f562019-09-17 12:26:57 -0600322 struct io_timeout timeout;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700323 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700324
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300325 const struct io_uring_sqe *sqe;
326 struct file *ring_file;
327 int ring_fd;
328 bool has_user;
329 bool in_async;
330 bool needs_fixed_file;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700331
332 struct io_ring_ctx *ctx;
Jens Axboeeac406c2019-11-14 12:09:58 -0700333 union {
334 struct list_head list;
335 struct rb_node rb_node;
336 };
Jens Axboe9e645e112019-05-10 16:07:28 -0600337 struct list_head link_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700338 unsigned int flags;
Jens Axboec16361c2019-01-17 08:39:48 -0700339 refcount_t refs;
Stefan Bühler8449eed2019-04-27 20:34:19 +0200340#define REQ_F_NOWAIT 1 /* must not punt to workers */
Jens Axboedef596e2019-01-09 08:59:42 -0700341#define REQ_F_IOPOLL_COMPLETED 2 /* polled IO has completed */
Jens Axboe6b063142019-01-10 22:13:58 -0700342#define REQ_F_FIXED_FILE 4 /* ctx owns file */
Jens Axboe4d7dd462019-11-20 13:03:52 -0700343#define REQ_F_LINK_NEXT 8 /* already grabbed next link */
Stefan Bühlere2033e32019-05-11 19:08:01 +0200344#define REQ_F_IO_DRAIN 16 /* drain existing IO first */
345#define REQ_F_IO_DRAINED 32 /* drain done */
Jens Axboe9e645e112019-05-10 16:07:28 -0600346#define REQ_F_LINK 64 /* linked sqes */
Jens Axboe2665abf2019-11-05 12:40:47 -0700347#define REQ_F_LINK_TIMEOUT 128 /* has linked timeout */
Zhengyuan Liuf7b76ac2019-07-16 23:26:14 +0800348#define REQ_F_FAIL_LINK 256 /* fail rest of links */
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +0300349#define REQ_F_DRAIN_LINK 512 /* link should be fully drained */
Jens Axboe5262f562019-09-17 12:26:57 -0600350#define REQ_F_TIMEOUT 1024 /* timeout request */
Jens Axboe491381ce2019-10-17 09:20:46 -0600351#define REQ_F_ISREG 2048 /* regular file */
352#define REQ_F_MUST_PUNT 4096 /* must be punted even for NONBLOCK */
Jens Axboe93bd25b2019-11-11 23:34:31 -0700353#define REQ_F_TIMEOUT_NOSEQ 8192 /* no timeout sequence */
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800354#define REQ_F_INFLIGHT 16384 /* on inflight list */
355#define REQ_F_COMP_LOCKED 32768 /* completion under lock */
Jens Axboe94ae5e72019-11-14 19:39:52 -0700356#define REQ_F_FREE_SQE 65536 /* free sqe if not async queued */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700357 u64 user_data;
Jens Axboe9e645e112019-05-10 16:07:28 -0600358 u32 result;
Jens Axboede0617e2019-04-06 21:51:27 -0600359 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700360
Jens Axboefcb323c2019-10-24 12:39:47 -0600361 struct list_head inflight_entry;
362
Jens Axboe561fb042019-10-24 07:25:42 -0600363 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700364};
365
366#define IO_PLUG_THRESHOLD 2
Jens Axboedef596e2019-01-09 08:59:42 -0700367#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700368
Jens Axboe9a56a232019-01-09 09:06:50 -0700369struct io_submit_state {
370 struct blk_plug plug;
371
372 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700373 * io_kiocb alloc cache
374 */
375 void *reqs[IO_IOPOLL_BATCH];
376 unsigned int free_reqs;
377 unsigned int cur_req;
378
379 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700380 * File reference cache
381 */
382 struct file *file;
383 unsigned int fd;
384 unsigned int has_refs;
385 unsigned int used_refs;
386 unsigned int ios_left;
387};
388
Jens Axboe561fb042019-10-24 07:25:42 -0600389static void io_wq_submit_work(struct io_wq_work **workptr);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700390static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liu4fe2c962019-09-09 20:50:40 +0800391static void __io_free_req(struct io_kiocb *req);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800392static void io_put_req(struct io_kiocb *req);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700393static void io_double_put_req(struct io_kiocb *req);
Jens Axboe978db572019-11-14 22:39:04 -0700394static void __io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700395static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
396static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboede0617e2019-04-06 21:51:27 -0600397
Jens Axboe2b188cc2019-01-07 10:46:33 -0700398static struct kmem_cache *req_cachep;
399
400static const struct file_operations io_uring_fops;
401
402struct sock *io_uring_get_socket(struct file *file)
403{
404#if defined(CONFIG_UNIX)
405 if (file->f_op == &io_uring_fops) {
406 struct io_ring_ctx *ctx = file->private_data;
407
408 return ctx->ring_sock->sk;
409 }
410#endif
411 return NULL;
412}
413EXPORT_SYMBOL(io_uring_get_socket);
414
415static void io_ring_ctx_ref_free(struct percpu_ref *ref)
416{
417 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
418
Jens Axboe206aefd2019-11-07 18:27:42 -0700419 complete(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700420}
421
422static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
423{
424 struct io_ring_ctx *ctx;
425
426 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
427 if (!ctx)
428 return NULL;
429
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700430 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
431 if (!ctx->fallback_req)
432 goto err;
433
Jens Axboe206aefd2019-11-07 18:27:42 -0700434 ctx->completions = kmalloc(2 * sizeof(struct completion), GFP_KERNEL);
435 if (!ctx->completions)
436 goto err;
437
Roman Gushchin21482892019-05-07 10:01:48 -0700438 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -0700439 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
440 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700441
442 ctx->flags = p->flags;
443 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700444 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe206aefd2019-11-07 18:27:42 -0700445 init_completion(&ctx->completions[0]);
446 init_completion(&ctx->completions[1]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700447 mutex_init(&ctx->uring_lock);
448 init_waitqueue_head(&ctx->wait);
449 spin_lock_init(&ctx->completion_lock);
Jens Axboedef596e2019-01-09 08:59:42 -0700450 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboeeac406c2019-11-14 12:09:58 -0700451 ctx->cancel_tree = RB_ROOT;
Jens Axboede0617e2019-04-06 21:51:27 -0600452 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -0600453 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -0600454 init_waitqueue_head(&ctx->inflight_wait);
455 spin_lock_init(&ctx->inflight_lock);
456 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700457 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -0700458err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700459 if (ctx->fallback_req)
460 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe206aefd2019-11-07 18:27:42 -0700461 kfree(ctx->completions);
462 kfree(ctx);
463 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700464}
465
Bob Liu9d858b22019-11-13 18:06:25 +0800466static inline bool __req_need_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -0600467{
Jackie Liua197f662019-11-08 08:09:12 -0700468 struct io_ring_ctx *ctx = req->ctx;
469
Jens Axboe498ccd92019-10-25 10:04:25 -0600470 return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped
471 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboede0617e2019-04-06 21:51:27 -0600472}
473
Bob Liu9d858b22019-11-13 18:06:25 +0800474static inline bool req_need_defer(struct io_kiocb *req)
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600475{
Bob Liu9d858b22019-11-13 18:06:25 +0800476 if ((req->flags & (REQ_F_IO_DRAIN|REQ_F_IO_DRAINED)) == REQ_F_IO_DRAIN)
477 return __req_need_defer(req);
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600478
Bob Liu9d858b22019-11-13 18:06:25 +0800479 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600480}
481
482static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -0600483{
484 struct io_kiocb *req;
485
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600486 req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list);
Bob Liu9d858b22019-11-13 18:06:25 +0800487 if (req && !req_need_defer(req)) {
Jens Axboede0617e2019-04-06 21:51:27 -0600488 list_del_init(&req->list);
489 return req;
490 }
491
492 return NULL;
493}
494
Jens Axboe5262f562019-09-17 12:26:57 -0600495static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx)
496{
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600497 struct io_kiocb *req;
498
499 req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list);
Jens Axboe93bd25b2019-11-11 23:34:31 -0700500 if (req) {
501 if (req->flags & REQ_F_TIMEOUT_NOSEQ)
502 return NULL;
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800503 if (!__req_need_defer(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -0700504 list_del_init(&req->list);
505 return req;
506 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600507 }
508
509 return NULL;
Jens Axboe5262f562019-09-17 12:26:57 -0600510}
511
Jens Axboede0617e2019-04-06 21:51:27 -0600512static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700513{
Hristo Venev75b28af2019-08-26 17:23:46 +0000514 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700515
Hristo Venev75b28af2019-08-26 17:23:46 +0000516 if (ctx->cached_cq_tail != READ_ONCE(rings->cq.tail)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700517 /* order cqe stores with ring update */
Hristo Venev75b28af2019-08-26 17:23:46 +0000518 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700519
Jens Axboe2b188cc2019-01-07 10:46:33 -0700520 if (wq_has_sleeper(&ctx->cq_wait)) {
521 wake_up_interruptible(&ctx->cq_wait);
522 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
523 }
524 }
525}
526
Jens Axboe561fb042019-10-24 07:25:42 -0600527static inline bool io_sqe_needs_user(const struct io_uring_sqe *sqe)
Jens Axboe18d9be12019-09-10 09:13:05 -0600528{
Jens Axboe561fb042019-10-24 07:25:42 -0600529 u8 opcode = READ_ONCE(sqe->opcode);
530
531 return !(opcode == IORING_OP_READ_FIXED ||
532 opcode == IORING_OP_WRITE_FIXED);
533}
534
Jens Axboe94ae5e72019-11-14 19:39:52 -0700535static inline bool io_prep_async_work(struct io_kiocb *req,
536 struct io_kiocb **link)
Jens Axboe561fb042019-10-24 07:25:42 -0600537{
538 bool do_hashed = false;
Jens Axboe54a91f32019-09-10 09:15:04 -0600539
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300540 if (req->sqe) {
541 switch (req->sqe->opcode) {
Jens Axboe6cc47d12019-09-18 11:18:23 -0600542 case IORING_OP_WRITEV:
543 case IORING_OP_WRITE_FIXED:
Jens Axboe561fb042019-10-24 07:25:42 -0600544 do_hashed = true;
Jens Axboe5f8fd2d32019-11-07 10:57:36 -0700545 /* fall-through */
546 case IORING_OP_READV:
547 case IORING_OP_READ_FIXED:
548 case IORING_OP_SENDMSG:
549 case IORING_OP_RECVMSG:
550 case IORING_OP_ACCEPT:
551 case IORING_OP_POLL_ADD:
Jens Axboef8e85cf2019-11-23 14:24:24 -0700552 case IORING_OP_CONNECT:
Jens Axboe5f8fd2d32019-11-07 10:57:36 -0700553 /*
554 * We know REQ_F_ISREG is not set on some of these
555 * opcodes, but this enables us to keep the check in
556 * just one place.
557 */
558 if (!(req->flags & REQ_F_ISREG))
559 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe6cc47d12019-09-18 11:18:23 -0600560 break;
561 }
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300562 if (io_sqe_needs_user(req->sqe))
Jens Axboe561fb042019-10-24 07:25:42 -0600563 req->work.flags |= IO_WQ_WORK_NEEDS_USER;
Jens Axboe54a91f32019-09-10 09:15:04 -0600564 }
565
Jens Axboe94ae5e72019-11-14 19:39:52 -0700566 *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -0600567 return do_hashed;
568}
569
Jackie Liua197f662019-11-08 08:09:12 -0700570static inline void io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -0600571{
Jackie Liua197f662019-11-08 08:09:12 -0700572 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe94ae5e72019-11-14 19:39:52 -0700573 struct io_kiocb *link;
574 bool do_hashed;
575
576 do_hashed = io_prep_async_work(req, &link);
Jens Axboe561fb042019-10-24 07:25:42 -0600577
578 trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work,
579 req->flags);
580 if (!do_hashed) {
581 io_wq_enqueue(ctx->io_wq, &req->work);
582 } else {
583 io_wq_enqueue_hashed(ctx->io_wq, &req->work,
584 file_inode(req->file));
585 }
Jens Axboe94ae5e72019-11-14 19:39:52 -0700586
587 if (link)
588 io_queue_linked_timeout(link);
Jens Axboe18d9be12019-09-10 09:13:05 -0600589}
590
Jens Axboe5262f562019-09-17 12:26:57 -0600591static void io_kill_timeout(struct io_kiocb *req)
592{
593 int ret;
594
Jens Axboead8a48a2019-11-15 08:49:11 -0700595 ret = hrtimer_try_to_cancel(&req->timeout.data->timer);
Jens Axboe5262f562019-09-17 12:26:57 -0600596 if (ret != -1) {
597 atomic_inc(&req->ctx->cq_timeouts);
Jens Axboe842f9612019-10-29 12:34:10 -0600598 list_del_init(&req->list);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700599 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800600 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -0600601 }
602}
603
604static void io_kill_timeouts(struct io_ring_ctx *ctx)
605{
606 struct io_kiocb *req, *tmp;
607
608 spin_lock_irq(&ctx->completion_lock);
609 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
610 io_kill_timeout(req);
611 spin_unlock_irq(&ctx->completion_lock);
612}
613
Jens Axboede0617e2019-04-06 21:51:27 -0600614static void io_commit_cqring(struct io_ring_ctx *ctx)
615{
616 struct io_kiocb *req;
617
Jens Axboe5262f562019-09-17 12:26:57 -0600618 while ((req = io_get_timeout_req(ctx)) != NULL)
619 io_kill_timeout(req);
620
Jens Axboede0617e2019-04-06 21:51:27 -0600621 __io_commit_cqring(ctx);
622
623 while ((req = io_get_deferred_req(ctx)) != NULL) {
624 req->flags |= REQ_F_IO_DRAINED;
Jackie Liua197f662019-11-08 08:09:12 -0700625 io_queue_async_work(req);
Jens Axboede0617e2019-04-06 21:51:27 -0600626 }
627}
628
Jens Axboe2b188cc2019-01-07 10:46:33 -0700629static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
630{
Hristo Venev75b28af2019-08-26 17:23:46 +0000631 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700632 unsigned tail;
633
634 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +0200635 /*
636 * writes to the cq entry need to come after reading head; the
637 * control dependency is enough as we're using WRITE_ONCE to
638 * fill the cq entry
639 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000640 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700641 return NULL;
642
643 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +0000644 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -0700645}
646
Jens Axboe8c838782019-03-12 15:48:16 -0600647static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
648{
649 if (waitqueue_active(&ctx->wait))
650 wake_up(&ctx->wait);
651 if (waitqueue_active(&ctx->sqo_wait))
652 wake_up(&ctx->sqo_wait);
Jens Axboe9b402842019-04-11 11:45:41 -0600653 if (ctx->cq_ev_fd)
654 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -0600655}
656
Jens Axboec4a2ed72019-11-21 21:01:26 -0700657/* Returns true if there are no backlogged entries after the flush */
658static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700659{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700660 struct io_rings *rings = ctx->rings;
661 struct io_uring_cqe *cqe;
662 struct io_kiocb *req;
663 unsigned long flags;
664 LIST_HEAD(list);
665
666 if (!force) {
667 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -0700668 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700669 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
670 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -0700671 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700672 }
673
674 spin_lock_irqsave(&ctx->completion_lock, flags);
675
676 /* if force is set, the ring is going away. always drop after that */
677 if (force)
678 ctx->cq_overflow_flushed = true;
679
Jens Axboec4a2ed72019-11-21 21:01:26 -0700680 cqe = NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700681 while (!list_empty(&ctx->cq_overflow_list)) {
682 cqe = io_get_cqring(ctx);
683 if (!cqe && !force)
684 break;
685
686 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
687 list);
688 list_move(&req->list, &list);
689 if (cqe) {
690 WRITE_ONCE(cqe->user_data, req->user_data);
691 WRITE_ONCE(cqe->res, req->result);
692 WRITE_ONCE(cqe->flags, 0);
693 } else {
694 WRITE_ONCE(ctx->rings->cq_overflow,
695 atomic_inc_return(&ctx->cached_cq_overflow));
696 }
697 }
698
699 io_commit_cqring(ctx);
700 spin_unlock_irqrestore(&ctx->completion_lock, flags);
701 io_cqring_ev_posted(ctx);
702
703 while (!list_empty(&list)) {
704 req = list_first_entry(&list, struct io_kiocb, list);
705 list_del(&req->list);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800706 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700707 }
Jens Axboec4a2ed72019-11-21 21:01:26 -0700708
709 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700710}
711
Jens Axboe78e19bb2019-11-06 15:21:34 -0700712static void io_cqring_fill_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700713{
Jens Axboe78e19bb2019-11-06 15:21:34 -0700714 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700715 struct io_uring_cqe *cqe;
716
Jens Axboe78e19bb2019-11-06 15:21:34 -0700717 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -0700718
Jens Axboe2b188cc2019-01-07 10:46:33 -0700719 /*
720 * If we can't get a cq entry, userspace overflowed the
721 * submission (by quite a lot). Increment the overflow count in
722 * the ring.
723 */
724 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700725 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -0700726 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700727 WRITE_ONCE(cqe->res, res);
728 WRITE_ONCE(cqe->flags, 0);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700729 } else if (ctx->cq_overflow_flushed) {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700730 WRITE_ONCE(ctx->rings->cq_overflow,
731 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700732 } else {
733 refcount_inc(&req->refs);
734 req->result = res;
735 list_add_tail(&req->list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700736 }
737}
738
Jens Axboe78e19bb2019-11-06 15:21:34 -0700739static void io_cqring_add_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700740{
Jens Axboe78e19bb2019-11-06 15:21:34 -0700741 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700742 unsigned long flags;
743
744 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700745 io_cqring_fill_event(req, res);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700746 io_commit_cqring(ctx);
747 spin_unlock_irqrestore(&ctx->completion_lock, flags);
748
Jens Axboe8c838782019-03-12 15:48:16 -0600749 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700750}
751
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700752static inline bool io_is_fallback_req(struct io_kiocb *req)
753{
754 return req == (struct io_kiocb *)
755 ((unsigned long) req->ctx->fallback_req & ~1UL);
756}
757
758static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
759{
760 struct io_kiocb *req;
761
762 req = ctx->fallback_req;
763 if (!test_and_set_bit_lock(0, (unsigned long *) ctx->fallback_req))
764 return req;
765
766 return NULL;
767}
768
Jens Axboe2579f912019-01-09 09:10:43 -0700769static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
770 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700771{
Jens Axboefd6fab22019-03-14 16:30:06 -0600772 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700773 struct io_kiocb *req;
774
775 if (!percpu_ref_tryget(&ctx->refs))
776 return NULL;
777
Jens Axboe2579f912019-01-09 09:10:43 -0700778 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -0600779 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -0700780 if (unlikely(!req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700781 goto fallback;
Jens Axboe2579f912019-01-09 09:10:43 -0700782 } else if (!state->free_reqs) {
783 size_t sz;
784 int ret;
785
786 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -0600787 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
788
789 /*
790 * Bulk alloc is all-or-nothing. If we fail to get a batch,
791 * retry single alloc to be on the safe side.
792 */
793 if (unlikely(ret <= 0)) {
794 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
795 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700796 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -0600797 ret = 1;
798 }
Jens Axboe2579f912019-01-09 09:10:43 -0700799 state->free_reqs = ret - 1;
800 state->cur_req = 1;
801 req = state->reqs[0];
802 } else {
803 req = state->reqs[state->cur_req];
804 state->free_reqs--;
805 state->cur_req++;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700806 }
807
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700808got_it:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300809 req->ring_file = NULL;
Jens Axboe60c112b2019-06-21 10:20:18 -0600810 req->file = NULL;
Jens Axboe2579f912019-01-09 09:10:43 -0700811 req->ctx = ctx;
812 req->flags = 0;
Jens Axboee65ef562019-03-12 10:16:44 -0600813 /* one is dropped after submission, the other at completion */
814 refcount_set(&req->refs, 2);
Jens Axboe9e645e112019-05-10 16:07:28 -0600815 req->result = 0;
Jens Axboe561fb042019-10-24 07:25:42 -0600816 INIT_IO_WORK(&req->work, io_wq_submit_work);
Jens Axboe2579f912019-01-09 09:10:43 -0700817 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700818fallback:
819 req = io_get_fallback_req(ctx);
820 if (req)
821 goto got_it;
Pavel Begunkov6805b322019-10-08 02:18:42 +0300822 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700823 return NULL;
824}
825
Jens Axboedef596e2019-01-09 08:59:42 -0700826static void io_free_req_many(struct io_ring_ctx *ctx, void **reqs, int *nr)
827{
828 if (*nr) {
829 kmem_cache_free_bulk(req_cachep, *nr, reqs);
Pavel Begunkov6805b322019-10-08 02:18:42 +0300830 percpu_ref_put_many(&ctx->refs, *nr);
Jens Axboedef596e2019-01-09 08:59:42 -0700831 *nr = 0;
832 }
833}
834
Jens Axboe9e645e112019-05-10 16:07:28 -0600835static void __io_free_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700836{
Jens Axboefcb323c2019-10-24 12:39:47 -0600837 struct io_ring_ctx *ctx = req->ctx;
838
Pavel Begunkovbbad27b2019-11-19 23:32:47 +0300839 if (req->flags & REQ_F_FREE_SQE)
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300840 kfree(req->sqe);
Jens Axboe09bb8392019-03-13 12:39:28 -0600841 if (req->file && !(req->flags & REQ_F_FIXED_FILE))
842 fput(req->file);
Jens Axboefcb323c2019-10-24 12:39:47 -0600843 if (req->flags & REQ_F_INFLIGHT) {
844 unsigned long flags;
845
846 spin_lock_irqsave(&ctx->inflight_lock, flags);
847 list_del(&req->inflight_entry);
848 if (waitqueue_active(&ctx->inflight_wait))
849 wake_up(&ctx->inflight_wait);
850 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
851 }
Jens Axboead8a48a2019-11-15 08:49:11 -0700852 if (req->flags & REQ_F_TIMEOUT)
853 kfree(req->timeout.data);
Jens Axboefcb323c2019-10-24 12:39:47 -0600854 percpu_ref_put(&ctx->refs);
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700855 if (likely(!io_is_fallback_req(req)))
856 kmem_cache_free(req_cachep, req);
857 else
858 clear_bit_unlock(0, (unsigned long *) ctx->fallback_req);
Jens Axboee65ef562019-03-12 10:16:44 -0600859}
860
Jackie Liua197f662019-11-08 08:09:12 -0700861static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -0600862{
Jackie Liua197f662019-11-08 08:09:12 -0700863 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -0700864 int ret;
865
Jens Axboead8a48a2019-11-15 08:49:11 -0700866 ret = hrtimer_try_to_cancel(&req->timeout.data->timer);
Jens Axboe2665abf2019-11-05 12:40:47 -0700867 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -0700868 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -0700869 io_commit_cqring(ctx);
870 req->flags &= ~REQ_F_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +0800871 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -0700872 return true;
873 }
874
875 return false;
876}
877
Jens Axboeba816ad2019-09-28 11:36:45 -0600878static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -0600879{
Jens Axboe2665abf2019-11-05 12:40:47 -0700880 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -0600881 struct io_kiocb *nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -0700882 bool wake_ev = false;
Jens Axboe9e645e112019-05-10 16:07:28 -0600883
Jens Axboe4d7dd462019-11-20 13:03:52 -0700884 /* Already got next link */
885 if (req->flags & REQ_F_LINK_NEXT)
886 return;
887
Jens Axboe9e645e112019-05-10 16:07:28 -0600888 /*
889 * The list should never be empty when we are called here. But could
890 * potentially happen if the chain is messed up, check to be on the
891 * safe side.
892 */
893 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, list);
Jens Axboe2665abf2019-11-05 12:40:47 -0700894 while (nxt) {
Jens Axboe76a46e02019-11-10 23:34:16 -0700895 list_del_init(&nxt->list);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700896
897 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
898 (nxt->flags & REQ_F_TIMEOUT)) {
899 wake_ev |= io_link_cancel_timeout(nxt);
900 nxt = list_first_entry_or_null(&req->link_list,
901 struct io_kiocb, list);
902 req->flags &= ~REQ_F_LINK_TIMEOUT;
903 continue;
904 }
Jens Axboe9e645e112019-05-10 16:07:28 -0600905 if (!list_empty(&req->link_list)) {
906 INIT_LIST_HEAD(&nxt->link_list);
907 list_splice(&req->link_list, &nxt->link_list);
908 nxt->flags |= REQ_F_LINK;
909 }
910
Pavel Begunkovb18fdf72019-11-21 23:21:02 +0300911 *nxtptr = nxt;
Jens Axboe94ae5e72019-11-14 19:39:52 -0700912 break;
Jens Axboe9e645e112019-05-10 16:07:28 -0600913 }
Jens Axboe2665abf2019-11-05 12:40:47 -0700914
Jens Axboe4d7dd462019-11-20 13:03:52 -0700915 req->flags |= REQ_F_LINK_NEXT;
Jens Axboe2665abf2019-11-05 12:40:47 -0700916 if (wake_ev)
917 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -0600918}
919
920/*
921 * Called if REQ_F_LINK is set, and we fail the head request
922 */
923static void io_fail_links(struct io_kiocb *req)
924{
Jens Axboe2665abf2019-11-05 12:40:47 -0700925 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -0600926 struct io_kiocb *link;
Jens Axboe2665abf2019-11-05 12:40:47 -0700927 unsigned long flags;
928
929 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -0600930
931 while (!list_empty(&req->link_list)) {
932 link = list_first_entry(&req->link_list, struct io_kiocb, list);
Jens Axboe2665abf2019-11-05 12:40:47 -0700933 list_del_init(&link->list);
Jens Axboe9e645e112019-05-10 16:07:28 -0600934
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +0200935 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -0700936
937 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300938 link->sqe->opcode == IORING_OP_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -0700939 io_link_cancel_timeout(link);
Jens Axboe2665abf2019-11-05 12:40:47 -0700940 } else {
Jens Axboe78e19bb2019-11-06 15:21:34 -0700941 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe978db572019-11-14 22:39:04 -0700942 __io_double_put_req(link);
Jens Axboe2665abf2019-11-05 12:40:47 -0700943 }
Jens Axboe5d960722019-11-19 15:31:28 -0700944 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -0600945 }
Jens Axboe2665abf2019-11-05 12:40:47 -0700946
947 io_commit_cqring(ctx);
948 spin_unlock_irqrestore(&ctx->completion_lock, flags);
949 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -0600950}
951
Jens Axboe4d7dd462019-11-20 13:03:52 -0700952static void io_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -0600953{
Jens Axboe4d7dd462019-11-20 13:03:52 -0700954 if (likely(!(req->flags & REQ_F_LINK)))
Jens Axboe2665abf2019-11-05 12:40:47 -0700955 return;
Jens Axboe2665abf2019-11-05 12:40:47 -0700956
Jens Axboe9e645e112019-05-10 16:07:28 -0600957 /*
958 * If LINK is set, we have dependent requests in this chain. If we
959 * didn't fail this request, queue the first one up, moving any other
960 * dependencies to the next request. In case of failure, fail the rest
961 * of the chain.
962 */
Jens Axboe2665abf2019-11-05 12:40:47 -0700963 if (req->flags & REQ_F_FAIL_LINK) {
964 io_fail_links(req);
Jens Axboe7c9e7f02019-11-12 08:15:53 -0700965 } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) ==
966 REQ_F_LINK_TIMEOUT) {
Jens Axboe2665abf2019-11-05 12:40:47 -0700967 struct io_ring_ctx *ctx = req->ctx;
968 unsigned long flags;
969
970 /*
971 * If this is a timeout link, we could be racing with the
972 * timeout timer. Grab the completion lock for this case to
Jens Axboe7c9e7f02019-11-12 08:15:53 -0700973 * protect against that.
Jens Axboe2665abf2019-11-05 12:40:47 -0700974 */
975 spin_lock_irqsave(&ctx->completion_lock, flags);
976 io_req_link_next(req, nxt);
977 spin_unlock_irqrestore(&ctx->completion_lock, flags);
978 } else {
979 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -0600980 }
Jens Axboe4d7dd462019-11-20 13:03:52 -0700981}
Jens Axboe9e645e112019-05-10 16:07:28 -0600982
Jackie Liuc69f8db2019-11-09 11:00:08 +0800983static void io_free_req(struct io_kiocb *req)
984{
Pavel Begunkov944e58b2019-11-21 23:21:01 +0300985 struct io_kiocb *nxt = NULL;
986
987 io_req_find_next(req, &nxt);
Pavel Begunkov70cf9f32019-11-21 23:21:00 +0300988 __io_free_req(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +0300989
990 if (nxt)
991 io_queue_async_work(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +0800992}
993
Jens Axboeba816ad2019-09-28 11:36:45 -0600994/*
995 * Drop reference to request, return next in chain (if there is one) if this
996 * was the last reference to this request.
997 */
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +0300998__attribute__((nonnull))
Jackie Liuec9c02a2019-11-08 23:50:36 +0800999static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboee65ef562019-03-12 10:16:44 -06001000{
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001001 io_req_find_next(req, nxtptr);
Jens Axboe4d7dd462019-11-20 13:03:52 -07001002
Jens Axboee65ef562019-03-12 10:16:44 -06001003 if (refcount_dec_and_test(&req->refs))
Jens Axboe4d7dd462019-11-20 13:03:52 -07001004 __io_free_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001005}
1006
Jens Axboe2b188cc2019-01-07 10:46:33 -07001007static void io_put_req(struct io_kiocb *req)
1008{
Jens Axboedef596e2019-01-09 08:59:42 -07001009 if (refcount_dec_and_test(&req->refs))
1010 io_free_req(req);
1011}
1012
Jens Axboe978db572019-11-14 22:39:04 -07001013/*
1014 * Must only be used if we don't need to care about links, usually from
1015 * within the completion handling itself.
1016 */
1017static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06001018{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001019 /* drop both submit and complete references */
1020 if (refcount_sub_and_test(2, &req->refs))
1021 __io_free_req(req);
1022}
1023
Jens Axboe978db572019-11-14 22:39:04 -07001024static void io_double_put_req(struct io_kiocb *req)
1025{
1026 /* drop both submit and complete references */
1027 if (refcount_sub_and_test(2, &req->refs))
1028 io_free_req(req);
1029}
1030
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001031static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06001032{
Jens Axboe84f97dc2019-11-06 11:27:53 -07001033 struct io_rings *rings = ctx->rings;
1034
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001035 /*
1036 * noflush == true is from the waitqueue handler, just ensure we wake
1037 * up the task, and the next invocation will flush the entries. We
1038 * cannot safely to it from here.
1039 */
1040 if (noflush && !list_empty(&ctx->cq_overflow_list))
1041 return -1U;
1042
1043 io_cqring_overflow_flush(ctx, false);
1044
Jens Axboea3a0e432019-08-20 11:03:11 -06001045 /* See comment at the top of this file */
1046 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00001047 return READ_ONCE(rings->cq.tail) - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06001048}
1049
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03001050static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1051{
1052 struct io_rings *rings = ctx->rings;
1053
1054 /* make sure SQ entry isn't read before tail */
1055 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1056}
1057
Jens Axboedef596e2019-01-09 08:59:42 -07001058/*
1059 * Find and free completed poll iocbs
1060 */
1061static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
1062 struct list_head *done)
1063{
1064 void *reqs[IO_IOPOLL_BATCH];
1065 struct io_kiocb *req;
Jens Axboe09bb8392019-03-13 12:39:28 -06001066 int to_free;
Jens Axboedef596e2019-01-09 08:59:42 -07001067
Jens Axboe09bb8392019-03-13 12:39:28 -06001068 to_free = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001069 while (!list_empty(done)) {
1070 req = list_first_entry(done, struct io_kiocb, list);
1071 list_del(&req->list);
1072
Jens Axboe78e19bb2019-11-06 15:21:34 -07001073 io_cqring_fill_event(req, req->result);
Jens Axboedef596e2019-01-09 08:59:42 -07001074 (*nr_events)++;
1075
Jens Axboe09bb8392019-03-13 12:39:28 -06001076 if (refcount_dec_and_test(&req->refs)) {
1077 /* If we're not using fixed files, we have to pair the
1078 * completion part with the file put. Use regular
1079 * completions for those, only batch free for fixed
Jens Axboe9e645e112019-05-10 16:07:28 -06001080 * file and non-linked commands.
Jens Axboe09bb8392019-03-13 12:39:28 -06001081 */
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03001082 if (((req->flags &
1083 (REQ_F_FIXED_FILE|REQ_F_LINK|REQ_F_FREE_SQE)) ==
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001084 REQ_F_FIXED_FILE) && !io_is_fallback_req(req)) {
Jens Axboe09bb8392019-03-13 12:39:28 -06001085 reqs[to_free++] = req;
1086 if (to_free == ARRAY_SIZE(reqs))
1087 io_free_req_many(ctx, reqs, &to_free);
Jens Axboe6b063142019-01-10 22:13:58 -07001088 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001089 io_free_req(req);
Jens Axboe6b063142019-01-10 22:13:58 -07001090 }
Jens Axboe9a56a232019-01-09 09:06:50 -07001091 }
Jens Axboedef596e2019-01-09 08:59:42 -07001092 }
Jens Axboedef596e2019-01-09 08:59:42 -07001093
Jens Axboe09bb8392019-03-13 12:39:28 -06001094 io_commit_cqring(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07001095 io_free_req_many(ctx, reqs, &to_free);
1096}
1097
1098static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1099 long min)
1100{
1101 struct io_kiocb *req, *tmp;
1102 LIST_HEAD(done);
1103 bool spin;
1104 int ret;
1105
1106 /*
1107 * Only spin for completions if we don't have multiple devices hanging
1108 * off our complete list, and we're under the requested amount.
1109 */
1110 spin = !ctx->poll_multi_file && *nr_events < min;
1111
1112 ret = 0;
1113 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
1114 struct kiocb *kiocb = &req->rw;
1115
1116 /*
1117 * Move completed entries to our local list. If we find a
1118 * request that requires polling, break out and complete
1119 * the done list first, if we have entries there.
1120 */
1121 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
1122 list_move_tail(&req->list, &done);
1123 continue;
1124 }
1125 if (!list_empty(&done))
1126 break;
1127
1128 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1129 if (ret < 0)
1130 break;
1131
1132 if (ret && spin)
1133 spin = false;
1134 ret = 0;
1135 }
1136
1137 if (!list_empty(&done))
1138 io_iopoll_complete(ctx, nr_events, &done);
1139
1140 return ret;
1141}
1142
1143/*
1144 * Poll for a mininum of 'min' events. Note that if min == 0 we consider that a
1145 * non-spinning poll check - we'll still enter the driver poll loop, but only
1146 * as a non-spinning completion check.
1147 */
1148static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
1149 long min)
1150{
Jens Axboe08f54392019-08-21 22:19:11 -06001151 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07001152 int ret;
1153
1154 ret = io_do_iopoll(ctx, nr_events, min);
1155 if (ret < 0)
1156 return ret;
1157 if (!min || *nr_events >= min)
1158 return 0;
1159 }
1160
1161 return 1;
1162}
1163
1164/*
1165 * We can't just wait for polled events to come to us, we have to actively
1166 * find and complete them.
1167 */
1168static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
1169{
1170 if (!(ctx->flags & IORING_SETUP_IOPOLL))
1171 return;
1172
1173 mutex_lock(&ctx->uring_lock);
1174 while (!list_empty(&ctx->poll_list)) {
1175 unsigned int nr_events = 0;
1176
1177 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -06001178
1179 /*
1180 * Ensure we allow local-to-the-cpu processing to take place,
1181 * in this case we need to ensure that we reap all events.
1182 */
1183 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -07001184 }
1185 mutex_unlock(&ctx->uring_lock);
1186}
1187
Jens Axboe2b2ed972019-10-25 10:06:15 -06001188static int __io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1189 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07001190{
Jens Axboe2b2ed972019-10-25 10:06:15 -06001191 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001192
1193 do {
1194 int tmin = 0;
1195
Jens Axboe500f9fb2019-08-19 12:15:59 -06001196 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06001197 * Don't enter poll loop if we already have events pending.
1198 * If we do, we can potentially be spinning for commands that
1199 * already triggered a CQE (eg in error).
1200 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001201 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06001202 break;
1203
1204 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06001205 * If a submit got punted to a workqueue, we can have the
1206 * application entering polling for a command before it gets
1207 * issued. That app will hold the uring_lock for the duration
1208 * of the poll right here, so we need to take a breather every
1209 * now and then to ensure that the issue has a chance to add
1210 * the poll to the issued list. Otherwise we can spin here
1211 * forever, while the workqueue is stuck trying to acquire the
1212 * very same mutex.
1213 */
1214 if (!(++iters & 7)) {
1215 mutex_unlock(&ctx->uring_lock);
1216 mutex_lock(&ctx->uring_lock);
1217 }
1218
Jens Axboedef596e2019-01-09 08:59:42 -07001219 if (*nr_events < min)
1220 tmin = min - *nr_events;
1221
1222 ret = io_iopoll_getevents(ctx, nr_events, tmin);
1223 if (ret <= 0)
1224 break;
1225 ret = 0;
1226 } while (min && !*nr_events && !need_resched());
1227
Jens Axboe2b2ed972019-10-25 10:06:15 -06001228 return ret;
1229}
1230
1231static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1232 long min)
1233{
1234 int ret;
1235
1236 /*
1237 * We disallow the app entering submit/complete with polling, but we
1238 * still need to lock the ring to prevent racing with polled issue
1239 * that got punted to a workqueue.
1240 */
1241 mutex_lock(&ctx->uring_lock);
1242 ret = __io_iopoll_check(ctx, nr_events, min);
Jens Axboe500f9fb2019-08-19 12:15:59 -06001243 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001244 return ret;
1245}
1246
Jens Axboe491381ce2019-10-17 09:20:46 -06001247static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001248{
Jens Axboe491381ce2019-10-17 09:20:46 -06001249 /*
1250 * Tell lockdep we inherited freeze protection from submission
1251 * thread.
1252 */
1253 if (req->flags & REQ_F_ISREG) {
1254 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001255
Jens Axboe491381ce2019-10-17 09:20:46 -06001256 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001257 }
Jens Axboe491381ce2019-10-17 09:20:46 -06001258 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001259}
1260
Jens Axboeba816ad2019-09-28 11:36:45 -06001261static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001262{
1263 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1264
Jens Axboe491381ce2019-10-17 09:20:46 -06001265 if (kiocb->ki_flags & IOCB_WRITE)
1266 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001267
Jens Axboe9e645e112019-05-10 16:07:28 -06001268 if ((req->flags & REQ_F_LINK) && res != req->result)
1269 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001270 io_cqring_add_event(req, res);
Jens Axboeba816ad2019-09-28 11:36:45 -06001271}
1272
1273static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1274{
1275 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1276
1277 io_complete_rw_common(kiocb, res);
Jens Axboee65ef562019-03-12 10:16:44 -06001278 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001279}
1280
Jens Axboeba816ad2019-09-28 11:36:45 -06001281static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res)
1282{
1283 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001284 struct io_kiocb *nxt = NULL;
Jens Axboeba816ad2019-09-28 11:36:45 -06001285
1286 io_complete_rw_common(kiocb, res);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001287 io_put_req_find_next(req, &nxt);
1288
1289 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001290}
1291
Jens Axboedef596e2019-01-09 08:59:42 -07001292static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1293{
1294 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1295
Jens Axboe491381ce2019-10-17 09:20:46 -06001296 if (kiocb->ki_flags & IOCB_WRITE)
1297 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001298
Jens Axboe9e645e112019-05-10 16:07:28 -06001299 if ((req->flags & REQ_F_LINK) && res != req->result)
1300 req->flags |= REQ_F_FAIL_LINK;
1301 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -07001302 if (res != -EAGAIN)
1303 req->flags |= REQ_F_IOPOLL_COMPLETED;
1304}
1305
1306/*
1307 * After the iocb has been issued, it's safe to be found on the poll list.
1308 * Adding the kiocb to the list AFTER submission ensures that we don't
1309 * find it from a io_iopoll_getevents() thread before the issuer is done
1310 * accessing the kiocb cookie.
1311 */
1312static void io_iopoll_req_issued(struct io_kiocb *req)
1313{
1314 struct io_ring_ctx *ctx = req->ctx;
1315
1316 /*
1317 * Track whether we have multiple files in our lists. This will impact
1318 * how we do polling eventually, not spinning if we're on potentially
1319 * different devices.
1320 */
1321 if (list_empty(&ctx->poll_list)) {
1322 ctx->poll_multi_file = false;
1323 } else if (!ctx->poll_multi_file) {
1324 struct io_kiocb *list_req;
1325
1326 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1327 list);
1328 if (list_req->rw.ki_filp != req->rw.ki_filp)
1329 ctx->poll_multi_file = true;
1330 }
1331
1332 /*
1333 * For fast devices, IO may have already completed. If it has, add
1334 * it to the front so we find it first.
1335 */
1336 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1337 list_add(&req->list, &ctx->poll_list);
1338 else
1339 list_add_tail(&req->list, &ctx->poll_list);
1340}
1341
Jens Axboe3d6770f2019-04-13 11:50:54 -06001342static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07001343{
Jens Axboe3d6770f2019-04-13 11:50:54 -06001344 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -07001345 int diff = state->has_refs - state->used_refs;
1346
1347 if (diff)
1348 fput_many(state->file, diff);
1349 state->file = NULL;
1350 }
1351}
1352
1353/*
1354 * Get as many references to a file as we have IOs left in this submission,
1355 * assuming most submissions are for one file, or at least that each file
1356 * has more than one submission.
1357 */
1358static struct file *io_file_get(struct io_submit_state *state, int fd)
1359{
1360 if (!state)
1361 return fget(fd);
1362
1363 if (state->file) {
1364 if (state->fd == fd) {
1365 state->used_refs++;
1366 state->ios_left--;
1367 return state->file;
1368 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06001369 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07001370 }
1371 state->file = fget_many(fd, state->ios_left);
1372 if (!state->file)
1373 return NULL;
1374
1375 state->fd = fd;
1376 state->has_refs = state->ios_left;
1377 state->used_refs = 1;
1378 state->ios_left--;
1379 return state->file;
1380}
1381
Jens Axboe2b188cc2019-01-07 10:46:33 -07001382/*
1383 * If we tracked the file through the SCM inflight mechanism, we could support
1384 * any file. For now, just ensure that anything potentially problematic is done
1385 * inline.
1386 */
1387static bool io_file_supports_async(struct file *file)
1388{
1389 umode_t mode = file_inode(file)->i_mode;
1390
1391 if (S_ISBLK(mode) || S_ISCHR(mode))
1392 return true;
1393 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
1394 return true;
1395
1396 return false;
1397}
1398
Pavel Begunkov267bc902019-11-07 01:41:08 +03001399static int io_prep_rw(struct io_kiocb *req, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001400{
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001401 const struct io_uring_sqe *sqe = req->sqe;
Jens Axboedef596e2019-01-09 08:59:42 -07001402 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001403 struct kiocb *kiocb = &req->rw;
Jens Axboe09bb8392019-03-13 12:39:28 -06001404 unsigned ioprio;
1405 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001406
Jens Axboe09bb8392019-03-13 12:39:28 -06001407 if (!req->file)
1408 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001409
Jens Axboe491381ce2019-10-17 09:20:46 -06001410 if (S_ISREG(file_inode(req->file)->i_mode))
1411 req->flags |= REQ_F_ISREG;
1412
1413 /*
1414 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
1415 * we know to async punt it even if it was opened O_NONBLOCK
1416 */
1417 if (force_nonblock && !io_file_supports_async(req->file)) {
1418 req->flags |= REQ_F_MUST_PUNT;
1419 return -EAGAIN;
1420 }
Jens Axboe6b063142019-01-10 22:13:58 -07001421
Jens Axboe2b188cc2019-01-07 10:46:33 -07001422 kiocb->ki_pos = READ_ONCE(sqe->off);
1423 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
1424 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
1425
1426 ioprio = READ_ONCE(sqe->ioprio);
1427 if (ioprio) {
1428 ret = ioprio_check_cap(ioprio);
1429 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06001430 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001431
1432 kiocb->ki_ioprio = ioprio;
1433 } else
1434 kiocb->ki_ioprio = get_current_ioprio();
1435
1436 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
1437 if (unlikely(ret))
Jens Axboe09bb8392019-03-13 12:39:28 -06001438 return ret;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001439
1440 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06001441 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
1442 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02001443 req->flags |= REQ_F_NOWAIT;
1444
1445 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001446 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001447
Jens Axboedef596e2019-01-09 08:59:42 -07001448 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07001449 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
1450 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06001451 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001452
Jens Axboedef596e2019-01-09 08:59:42 -07001453 kiocb->ki_flags |= IOCB_HIPRI;
1454 kiocb->ki_complete = io_complete_rw_iopoll;
Jens Axboe6873e0b2019-10-30 13:53:09 -06001455 req->result = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001456 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001457 if (kiocb->ki_flags & IOCB_HIPRI)
1458 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07001459 kiocb->ki_complete = io_complete_rw;
1460 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001461 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001462}
1463
1464static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
1465{
1466 switch (ret) {
1467 case -EIOCBQUEUED:
1468 break;
1469 case -ERESTARTSYS:
1470 case -ERESTARTNOINTR:
1471 case -ERESTARTNOHAND:
1472 case -ERESTART_RESTARTBLOCK:
1473 /*
1474 * We can't just restart the syscall, since previously
1475 * submitted sqes may already be in progress. Just fail this
1476 * IO with EINTR.
1477 */
1478 ret = -EINTR;
1479 /* fall through */
1480 default:
1481 kiocb->ki_complete(kiocb, ret, 0);
1482 }
1483}
1484
Jens Axboeba816ad2019-09-28 11:36:45 -06001485static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt,
1486 bool in_async)
1487{
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001488 if (in_async && ret >= 0 && kiocb->ki_complete == io_complete_rw)
Jens Axboeba816ad2019-09-28 11:36:45 -06001489 *nxt = __io_complete_rw(kiocb, ret);
1490 else
1491 io_rw_done(kiocb, ret);
1492}
1493
Pavel Begunkov7d009162019-11-25 23:14:40 +03001494static ssize_t io_import_fixed(struct io_ring_ctx *ctx, int rw,
1495 const struct io_uring_sqe *sqe,
1496 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07001497{
1498 size_t len = READ_ONCE(sqe->len);
1499 struct io_mapped_ubuf *imu;
1500 unsigned index, buf_index;
1501 size_t offset;
1502 u64 buf_addr;
1503
1504 /* attempt to use fixed buffers without having provided iovecs */
1505 if (unlikely(!ctx->user_bufs))
1506 return -EFAULT;
1507
1508 buf_index = READ_ONCE(sqe->buf_index);
1509 if (unlikely(buf_index >= ctx->nr_user_bufs))
1510 return -EFAULT;
1511
1512 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
1513 imu = &ctx->user_bufs[index];
1514 buf_addr = READ_ONCE(sqe->addr);
1515
1516 /* overflow */
1517 if (buf_addr + len < buf_addr)
1518 return -EFAULT;
1519 /* not inside the mapped region */
1520 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
1521 return -EFAULT;
1522
1523 /*
1524 * May not be a start of buffer, set size appropriately
1525 * and advance us to the beginning.
1526 */
1527 offset = buf_addr - imu->ubuf;
1528 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06001529
1530 if (offset) {
1531 /*
1532 * Don't use iov_iter_advance() here, as it's really slow for
1533 * using the latter parts of a big fixed buffer - it iterates
1534 * over each segment manually. We can cheat a bit here, because
1535 * we know that:
1536 *
1537 * 1) it's a BVEC iter, we set it up
1538 * 2) all bvecs are PAGE_SIZE in size, except potentially the
1539 * first and last bvec
1540 *
1541 * So just find our index, and adjust the iterator afterwards.
1542 * If the offset is within the first bvec (or the whole first
1543 * bvec, just use iov_iter_advance(). This makes it easier
1544 * since we can just skip the first segment, which may not
1545 * be PAGE_SIZE aligned.
1546 */
1547 const struct bio_vec *bvec = imu->bvec;
1548
1549 if (offset <= bvec->bv_len) {
1550 iov_iter_advance(iter, offset);
1551 } else {
1552 unsigned long seg_skip;
1553
1554 /* skip first vec */
1555 offset -= bvec->bv_len;
1556 seg_skip = 1 + (offset >> PAGE_SHIFT);
1557
1558 iter->bvec = bvec + seg_skip;
1559 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02001560 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001561 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001562 }
1563 }
1564
Jens Axboe5e559562019-11-13 16:12:46 -07001565 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001566}
1567
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001568static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
1569 struct iovec **iovec, struct iov_iter *iter)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001570{
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001571 const struct io_uring_sqe *sqe = req->sqe;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001572 void __user *buf = u64_to_user_ptr(READ_ONCE(sqe->addr));
1573 size_t sqe_len = READ_ONCE(sqe->len);
Jens Axboeedafcce2019-01-09 09:16:05 -07001574 u8 opcode;
1575
1576 /*
1577 * We're reading ->opcode for the second time, but the first read
1578 * doesn't care whether it's _FIXED or not, so it doesn't matter
1579 * whether ->opcode changes concurrently. The first read does care
1580 * about whether it is a READ or a WRITE, so we don't trust this read
1581 * for that purpose and instead let the caller pass in the read/write
1582 * flag.
1583 */
1584 opcode = READ_ONCE(sqe->opcode);
Pavel Begunkov7d009162019-11-25 23:14:40 +03001585 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07001586 *iovec = NULL;
Pavel Begunkov7d009162019-11-25 23:14:40 +03001587 return io_import_fixed(req->ctx, rw, sqe, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07001588 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001589
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001590 if (!req->has_user)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001591 return -EFAULT;
1592
1593#ifdef CONFIG_COMPAT
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001594 if (req->ctx->compat)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001595 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
1596 iovec, iter);
1597#endif
1598
1599 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
1600}
1601
Jens Axboe32960612019-09-23 11:05:34 -06001602/*
1603 * For files that don't have ->read_iter() and ->write_iter(), handle them
1604 * by looping over ->read() or ->write() manually.
1605 */
1606static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
1607 struct iov_iter *iter)
1608{
1609 ssize_t ret = 0;
1610
1611 /*
1612 * Don't support polled IO through this interface, and we can't
1613 * support non-blocking either. For the latter, this just causes
1614 * the kiocb to be handled from an async context.
1615 */
1616 if (kiocb->ki_flags & IOCB_HIPRI)
1617 return -EOPNOTSUPP;
1618 if (kiocb->ki_flags & IOCB_NOWAIT)
1619 return -EAGAIN;
1620
1621 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03001622 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06001623 ssize_t nr;
1624
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03001625 if (!iov_iter_is_bvec(iter)) {
1626 iovec = iov_iter_iovec(iter);
1627 } else {
1628 /* fixed buffers import bvec */
1629 iovec.iov_base = kmap(iter->bvec->bv_page)
1630 + iter->iov_offset;
1631 iovec.iov_len = min(iter->count,
1632 iter->bvec->bv_len - iter->iov_offset);
1633 }
1634
Jens Axboe32960612019-09-23 11:05:34 -06001635 if (rw == READ) {
1636 nr = file->f_op->read(file, iovec.iov_base,
1637 iovec.iov_len, &kiocb->ki_pos);
1638 } else {
1639 nr = file->f_op->write(file, iovec.iov_base,
1640 iovec.iov_len, &kiocb->ki_pos);
1641 }
1642
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03001643 if (iov_iter_is_bvec(iter))
1644 kunmap(iter->bvec->bv_page);
1645
Jens Axboe32960612019-09-23 11:05:34 -06001646 if (nr < 0) {
1647 if (!ret)
1648 ret = nr;
1649 break;
1650 }
1651 ret += nr;
1652 if (nr != iovec.iov_len)
1653 break;
1654 iov_iter_advance(iter, nr);
1655 }
1656
1657 return ret;
1658}
1659
Pavel Begunkov267bc902019-11-07 01:41:08 +03001660static int io_read(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001661 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001662{
1663 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1664 struct kiocb *kiocb = &req->rw;
1665 struct iov_iter iter;
1666 struct file *file;
Jens Axboe31b51512019-01-18 22:56:34 -07001667 size_t iov_count;
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001668 ssize_t read_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001669
Pavel Begunkov267bc902019-11-07 01:41:08 +03001670 ret = io_prep_rw(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001671 if (ret)
1672 return ret;
1673 file = kiocb->ki_filp;
1674
Jens Axboe2b188cc2019-01-07 10:46:33 -07001675 if (unlikely(!(file->f_mode & FMODE_READ)))
Jens Axboe09bb8392019-03-13 12:39:28 -06001676 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001677
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001678 ret = io_import_iovec(READ, req, &iovec, &iter);
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001679 if (ret < 0)
Jens Axboe09bb8392019-03-13 12:39:28 -06001680 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001681
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001682 read_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06001683 if (req->flags & REQ_F_LINK)
1684 req->result = read_size;
1685
Jens Axboe31b51512019-01-18 22:56:34 -07001686 iov_count = iov_iter_count(&iter);
1687 ret = rw_verify_area(READ, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001688 if (!ret) {
1689 ssize_t ret2;
1690
Jens Axboe32960612019-09-23 11:05:34 -06001691 if (file->f_op->read_iter)
1692 ret2 = call_read_iter(file, kiocb, &iter);
1693 else
1694 ret2 = loop_rw_iter(READ, file, kiocb, &iter);
1695
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001696 /*
1697 * In case of a short read, punt to async. This can happen
1698 * if we have data partially cached. Alternatively we can
1699 * return the short read, in which case the application will
1700 * need to issue another SQE and wait for it. That SQE will
1701 * need async punt anyway, so it's more efficient to do it
1702 * here.
1703 */
Jens Axboe491381ce2019-10-17 09:20:46 -06001704 if (force_nonblock && !(req->flags & REQ_F_NOWAIT) &&
1705 (req->flags & REQ_F_ISREG) &&
1706 ret2 > 0 && ret2 < read_size)
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001707 ret2 = -EAGAIN;
1708 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboe561fb042019-10-24 07:25:42 -06001709 if (!force_nonblock || ret2 != -EAGAIN)
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001710 kiocb_done(kiocb, ret2, nxt, req->in_async);
Jens Axboe561fb042019-10-24 07:25:42 -06001711 else
Jens Axboe2b188cc2019-01-07 10:46:33 -07001712 ret = -EAGAIN;
1713 }
1714 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001715 return ret;
1716}
1717
Pavel Begunkov267bc902019-11-07 01:41:08 +03001718static int io_write(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001719 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001720{
1721 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1722 struct kiocb *kiocb = &req->rw;
1723 struct iov_iter iter;
1724 struct file *file;
Jens Axboe31b51512019-01-18 22:56:34 -07001725 size_t iov_count;
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001726 ssize_t ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001727
Pavel Begunkov267bc902019-11-07 01:41:08 +03001728 ret = io_prep_rw(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001729 if (ret)
1730 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001731
Jens Axboe2b188cc2019-01-07 10:46:33 -07001732 file = kiocb->ki_filp;
1733 if (unlikely(!(file->f_mode & FMODE_WRITE)))
Jens Axboe09bb8392019-03-13 12:39:28 -06001734 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001735
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001736 ret = io_import_iovec(WRITE, req, &iovec, &iter);
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001737 if (ret < 0)
Jens Axboe09bb8392019-03-13 12:39:28 -06001738 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001739
Jens Axboe9e645e112019-05-10 16:07:28 -06001740 if (req->flags & REQ_F_LINK)
1741 req->result = ret;
1742
Jens Axboe31b51512019-01-18 22:56:34 -07001743 iov_count = iov_iter_count(&iter);
1744
1745 ret = -EAGAIN;
Jens Axboe561fb042019-10-24 07:25:42 -06001746 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT))
Jens Axboe31b51512019-01-18 22:56:34 -07001747 goto out_free;
Jens Axboe31b51512019-01-18 22:56:34 -07001748
1749 ret = rw_verify_area(WRITE, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001750 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01001751 ssize_t ret2;
1752
Jens Axboe2b188cc2019-01-07 10:46:33 -07001753 /*
1754 * Open-code file_start_write here to grab freeze protection,
1755 * which will be released by another thread in
1756 * io_complete_rw(). Fool lockdep by telling it the lock got
1757 * released so that it doesn't complain about the held lock when
1758 * we return to userspace.
1759 */
Jens Axboe491381ce2019-10-17 09:20:46 -06001760 if (req->flags & REQ_F_ISREG) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001761 __sb_start_write(file_inode(file)->i_sb,
1762 SB_FREEZE_WRITE, true);
1763 __sb_writers_release(file_inode(file)->i_sb,
1764 SB_FREEZE_WRITE);
1765 }
1766 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01001767
Jens Axboe32960612019-09-23 11:05:34 -06001768 if (file->f_op->write_iter)
1769 ret2 = call_write_iter(file, kiocb, &iter);
1770 else
1771 ret2 = loop_rw_iter(WRITE, file, kiocb, &iter);
Jens Axboe561fb042019-10-24 07:25:42 -06001772 if (!force_nonblock || ret2 != -EAGAIN)
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001773 kiocb_done(kiocb, ret2, nxt, req->in_async);
Jens Axboe561fb042019-10-24 07:25:42 -06001774 else
Roman Penyaev9bf79332019-03-25 20:09:24 +01001775 ret = -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001776 }
Jens Axboe31b51512019-01-18 22:56:34 -07001777out_free:
Jens Axboe2b188cc2019-01-07 10:46:33 -07001778 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001779 return ret;
1780}
1781
1782/*
1783 * IORING_OP_NOP just posts a completion event, nothing else.
1784 */
Jens Axboe78e19bb2019-11-06 15:21:34 -07001785static int io_nop(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001786{
1787 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001788
Jens Axboedef596e2019-01-09 08:59:42 -07001789 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1790 return -EINVAL;
1791
Jens Axboe78e19bb2019-11-06 15:21:34 -07001792 io_cqring_add_event(req, 0);
Jens Axboee65ef562019-03-12 10:16:44 -06001793 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001794 return 0;
1795}
1796
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001797static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1798{
Jens Axboe6b063142019-01-10 22:13:58 -07001799 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001800
Jens Axboe09bb8392019-03-13 12:39:28 -06001801 if (!req->file)
1802 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001803
Jens Axboe6b063142019-01-10 22:13:58 -07001804 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07001805 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07001806 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001807 return -EINVAL;
1808
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001809 return 0;
1810}
1811
1812static int io_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001813 struct io_kiocb **nxt, bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001814{
1815 loff_t sqe_off = READ_ONCE(sqe->off);
1816 loff_t sqe_len = READ_ONCE(sqe->len);
1817 loff_t end = sqe_off + sqe_len;
1818 unsigned fsync_flags;
1819 int ret;
1820
1821 fsync_flags = READ_ONCE(sqe->fsync_flags);
1822 if (unlikely(fsync_flags & ~IORING_FSYNC_DATASYNC))
1823 return -EINVAL;
1824
1825 ret = io_prep_fsync(req, sqe);
1826 if (ret)
1827 return ret;
1828
1829 /* fsync always requires a blocking context */
1830 if (force_nonblock)
1831 return -EAGAIN;
1832
1833 ret = vfs_fsync_range(req->rw.ki_filp, sqe_off,
1834 end > 0 ? end : LLONG_MAX,
1835 fsync_flags & IORING_FSYNC_DATASYNC);
1836
Jens Axboe9e645e112019-05-10 16:07:28 -06001837 if (ret < 0 && (req->flags & REQ_F_LINK))
1838 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001839 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001840 io_put_req_find_next(req, nxt);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001841 return 0;
1842}
1843
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001844static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1845{
1846 struct io_ring_ctx *ctx = req->ctx;
1847 int ret = 0;
1848
1849 if (!req->file)
1850 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001851
1852 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1853 return -EINVAL;
1854 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
1855 return -EINVAL;
1856
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001857 return ret;
1858}
1859
1860static int io_sync_file_range(struct io_kiocb *req,
1861 const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001862 struct io_kiocb **nxt,
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001863 bool force_nonblock)
1864{
1865 loff_t sqe_off;
1866 loff_t sqe_len;
1867 unsigned flags;
1868 int ret;
1869
1870 ret = io_prep_sfr(req, sqe);
1871 if (ret)
1872 return ret;
1873
1874 /* sync_file_range always requires a blocking context */
1875 if (force_nonblock)
1876 return -EAGAIN;
1877
1878 sqe_off = READ_ONCE(sqe->off);
1879 sqe_len = READ_ONCE(sqe->len);
1880 flags = READ_ONCE(sqe->sync_range_flags);
1881
1882 ret = sync_file_range(req->rw.ki_filp, sqe_off, sqe_len, flags);
1883
Jens Axboe9e645e112019-05-10 16:07:28 -06001884 if (ret < 0 && (req->flags & REQ_F_LINK))
1885 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001886 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001887 io_put_req_find_next(req, nxt);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001888 return 0;
1889}
1890
Jens Axboe0fa03c62019-04-19 13:34:07 -06001891#if defined(CONFIG_NET)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001892static int io_send_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001893 struct io_kiocb **nxt, bool force_nonblock,
Jens Axboeaa1fa282019-04-19 13:38:09 -06001894 long (*fn)(struct socket *, struct user_msghdr __user *,
1895 unsigned int))
1896{
Jens Axboe0fa03c62019-04-19 13:34:07 -06001897 struct socket *sock;
1898 int ret;
1899
1900 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1901 return -EINVAL;
1902
1903 sock = sock_from_file(req->file, &ret);
1904 if (sock) {
1905 struct user_msghdr __user *msg;
1906 unsigned flags;
1907
1908 flags = READ_ONCE(sqe->msg_flags);
1909 if (flags & MSG_DONTWAIT)
1910 req->flags |= REQ_F_NOWAIT;
1911 else if (force_nonblock)
1912 flags |= MSG_DONTWAIT;
1913
1914 msg = (struct user_msghdr __user *) (unsigned long)
1915 READ_ONCE(sqe->addr);
1916
Jens Axboeaa1fa282019-04-19 13:38:09 -06001917 ret = fn(sock, msg, flags);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001918 if (force_nonblock && ret == -EAGAIN)
1919 return ret;
Jens Axboe441cdbd2019-12-02 18:49:10 -07001920 if (ret == -ERESTARTSYS)
1921 ret = -EINTR;
Jens Axboe0fa03c62019-04-19 13:34:07 -06001922 }
1923
Jens Axboe78e19bb2019-11-06 15:21:34 -07001924 io_cqring_add_event(req, ret);
Jens Axboef1f40852019-11-05 20:33:16 -07001925 if (ret < 0 && (req->flags & REQ_F_LINK))
1926 req->flags |= REQ_F_FAIL_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001927 io_put_req_find_next(req, nxt);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001928 return 0;
Jens Axboeaa1fa282019-04-19 13:38:09 -06001929}
1930#endif
1931
1932static int io_sendmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001933 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001934{
1935#if defined(CONFIG_NET)
Jens Axboeba816ad2019-09-28 11:36:45 -06001936 return io_send_recvmsg(req, sqe, nxt, force_nonblock,
1937 __sys_sendmsg_sock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06001938#else
1939 return -EOPNOTSUPP;
1940#endif
1941}
1942
1943static int io_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001944 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001945{
1946#if defined(CONFIG_NET)
Jens Axboeba816ad2019-09-28 11:36:45 -06001947 return io_send_recvmsg(req, sqe, nxt, force_nonblock,
1948 __sys_recvmsg_sock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001949#else
1950 return -EOPNOTSUPP;
1951#endif
1952}
1953
Jens Axboe17f2fe32019-10-17 14:42:58 -06001954static int io_accept(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1955 struct io_kiocb **nxt, bool force_nonblock)
1956{
1957#if defined(CONFIG_NET)
1958 struct sockaddr __user *addr;
1959 int __user *addr_len;
1960 unsigned file_flags;
1961 int flags, ret;
1962
1963 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
1964 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05001965 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06001966 return -EINVAL;
1967
1968 addr = (struct sockaddr __user *) (unsigned long) READ_ONCE(sqe->addr);
1969 addr_len = (int __user *) (unsigned long) READ_ONCE(sqe->addr2);
1970 flags = READ_ONCE(sqe->accept_flags);
1971 file_flags = force_nonblock ? O_NONBLOCK : 0;
1972
1973 ret = __sys_accept4_file(req->file, file_flags, addr, addr_len, flags);
1974 if (ret == -EAGAIN && force_nonblock) {
1975 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
1976 return -EAGAIN;
1977 }
Jens Axboe8e3cca12019-11-09 19:52:33 -07001978 if (ret == -ERESTARTSYS)
1979 ret = -EINTR;
Jens Axboe17f2fe32019-10-17 14:42:58 -06001980 if (ret < 0 && (req->flags & REQ_F_LINK))
1981 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001982 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001983 io_put_req_find_next(req, nxt);
Jens Axboe17f2fe32019-10-17 14:42:58 -06001984 return 0;
1985#else
1986 return -EOPNOTSUPP;
1987#endif
1988}
1989
Jens Axboef8e85cf2019-11-23 14:24:24 -07001990static int io_connect(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1991 struct io_kiocb **nxt, bool force_nonblock)
1992{
1993#if defined(CONFIG_NET)
1994 struct sockaddr __user *addr;
1995 unsigned file_flags;
1996 int addr_len, ret;
1997
1998 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
1999 return -EINVAL;
2000 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
2001 return -EINVAL;
2002
2003 addr = (struct sockaddr __user *) (unsigned long) READ_ONCE(sqe->addr);
2004 addr_len = READ_ONCE(sqe->addr2);
2005 file_flags = force_nonblock ? O_NONBLOCK : 0;
2006
2007 ret = __sys_connect_file(req->file, addr, addr_len, file_flags);
2008 if (ret == -EAGAIN && force_nonblock)
2009 return -EAGAIN;
2010 if (ret == -ERESTARTSYS)
2011 ret = -EINTR;
2012 if (ret < 0 && (req->flags & REQ_F_LINK))
2013 req->flags |= REQ_F_FAIL_LINK;
2014 io_cqring_add_event(req, ret);
2015 io_put_req_find_next(req, nxt);
2016 return 0;
2017#else
2018 return -EOPNOTSUPP;
2019#endif
2020}
2021
Jens Axboeeac406c2019-11-14 12:09:58 -07002022static inline void io_poll_remove_req(struct io_kiocb *req)
2023{
2024 if (!RB_EMPTY_NODE(&req->rb_node)) {
2025 rb_erase(&req->rb_node, &req->ctx->cancel_tree);
2026 RB_CLEAR_NODE(&req->rb_node);
2027 }
2028}
2029
Jens Axboe221c5eb2019-01-17 09:41:58 -07002030static void io_poll_remove_one(struct io_kiocb *req)
2031{
2032 struct io_poll_iocb *poll = &req->poll;
2033
2034 spin_lock(&poll->head->lock);
2035 WRITE_ONCE(poll->canceled, true);
Jens Axboee9444752019-11-26 15:02:04 -07002036 if (!list_empty(&poll->wait->entry)) {
2037 list_del_init(&poll->wait->entry);
Jackie Liua197f662019-11-08 08:09:12 -07002038 io_queue_async_work(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002039 }
2040 spin_unlock(&poll->head->lock);
Jens Axboeeac406c2019-11-14 12:09:58 -07002041 io_poll_remove_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002042}
2043
2044static void io_poll_remove_all(struct io_ring_ctx *ctx)
2045{
Jens Axboeeac406c2019-11-14 12:09:58 -07002046 struct rb_node *node;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002047 struct io_kiocb *req;
2048
2049 spin_lock_irq(&ctx->completion_lock);
Jens Axboeeac406c2019-11-14 12:09:58 -07002050 while ((node = rb_first(&ctx->cancel_tree)) != NULL) {
2051 req = rb_entry(node, struct io_kiocb, rb_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002052 io_poll_remove_one(req);
2053 }
2054 spin_unlock_irq(&ctx->completion_lock);
2055}
2056
Jens Axboe47f46762019-11-09 17:43:02 -07002057static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
2058{
Jens Axboeeac406c2019-11-14 12:09:58 -07002059 struct rb_node *p, *parent = NULL;
Jens Axboe47f46762019-11-09 17:43:02 -07002060 struct io_kiocb *req;
2061
Jens Axboeeac406c2019-11-14 12:09:58 -07002062 p = ctx->cancel_tree.rb_node;
2063 while (p) {
2064 parent = p;
2065 req = rb_entry(parent, struct io_kiocb, rb_node);
2066 if (sqe_addr < req->user_data) {
2067 p = p->rb_left;
2068 } else if (sqe_addr > req->user_data) {
2069 p = p->rb_right;
2070 } else {
2071 io_poll_remove_one(req);
2072 return 0;
2073 }
Jens Axboe47f46762019-11-09 17:43:02 -07002074 }
2075
2076 return -ENOENT;
2077}
2078
Jens Axboe221c5eb2019-01-17 09:41:58 -07002079/*
2080 * Find a running poll command that matches one specified in sqe->addr,
2081 * and remove it if found.
2082 */
2083static int io_poll_remove(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2084{
2085 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07002086 int ret;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002087
2088 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2089 return -EINVAL;
2090 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
2091 sqe->poll_events)
2092 return -EINVAL;
2093
2094 spin_lock_irq(&ctx->completion_lock);
Jens Axboe47f46762019-11-09 17:43:02 -07002095 ret = io_poll_cancel(ctx, READ_ONCE(sqe->addr));
Jens Axboe221c5eb2019-01-17 09:41:58 -07002096 spin_unlock_irq(&ctx->completion_lock);
2097
Jens Axboe78e19bb2019-11-06 15:21:34 -07002098 io_cqring_add_event(req, ret);
Jens Axboef1f40852019-11-05 20:33:16 -07002099 if (ret < 0 && (req->flags & REQ_F_LINK))
2100 req->flags |= REQ_F_FAIL_LINK;
Jens Axboee65ef562019-03-12 10:16:44 -06002101 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002102 return 0;
2103}
2104
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002105static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002106{
Jackie Liua197f662019-11-08 08:09:12 -07002107 struct io_ring_ctx *ctx = req->ctx;
2108
Jens Axboe8c838782019-03-12 15:48:16 -06002109 req->poll.done = true;
Jens Axboee9444752019-11-26 15:02:04 -07002110 kfree(req->poll.wait);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002111 if (error)
2112 io_cqring_fill_event(req, error);
2113 else
2114 io_cqring_fill_event(req, mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06002115 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002116}
2117
Jens Axboe561fb042019-10-24 07:25:42 -06002118static void io_poll_complete_work(struct io_wq_work **workptr)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002119{
Jens Axboe561fb042019-10-24 07:25:42 -06002120 struct io_wq_work *work = *workptr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002121 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
2122 struct io_poll_iocb *poll = &req->poll;
2123 struct poll_table_struct pt = { ._key = poll->events };
2124 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe89723d02019-11-05 15:32:58 -07002125 struct io_kiocb *nxt = NULL;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002126 __poll_t mask = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002127 int ret = 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002128
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002129 if (work->flags & IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06002130 WRITE_ONCE(poll->canceled, true);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002131 ret = -ECANCELED;
2132 } else if (READ_ONCE(poll->canceled)) {
2133 ret = -ECANCELED;
2134 }
Jens Axboe561fb042019-10-24 07:25:42 -06002135
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002136 if (ret != -ECANCELED)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002137 mask = vfs_poll(poll->file, &pt) & poll->events;
2138
2139 /*
2140 * Note that ->ki_cancel callers also delete iocb from active_reqs after
2141 * calling ->ki_cancel. We need the ctx_lock roundtrip here to
2142 * synchronize with them. In the cancellation case the list_del_init
2143 * itself is not actually needed, but harmless so we keep it in to
2144 * avoid further branches in the fast path.
2145 */
2146 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002147 if (!mask && ret != -ECANCELED) {
Jens Axboee9444752019-11-26 15:02:04 -07002148 add_wait_queue(poll->head, poll->wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002149 spin_unlock_irq(&ctx->completion_lock);
2150 return;
2151 }
Jens Axboeeac406c2019-11-14 12:09:58 -07002152 io_poll_remove_req(req);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002153 io_poll_complete(req, mask, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002154 spin_unlock_irq(&ctx->completion_lock);
2155
Jens Axboe8c838782019-03-12 15:48:16 -06002156 io_cqring_ev_posted(ctx);
Jens Axboe89723d02019-11-05 15:32:58 -07002157
Jens Axboefba38c22019-11-18 12:27:57 -07002158 if (ret < 0 && req->flags & REQ_F_LINK)
2159 req->flags |= REQ_F_FAIL_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08002160 io_put_req_find_next(req, &nxt);
Jens Axboe89723d02019-11-05 15:32:58 -07002161 if (nxt)
2162 *workptr = &nxt->work;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002163}
2164
2165static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
2166 void *key)
2167{
Jens Axboee9444752019-11-26 15:02:04 -07002168 struct io_poll_iocb *poll = wait->private;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002169 struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
2170 struct io_ring_ctx *ctx = req->ctx;
2171 __poll_t mask = key_to_poll(key);
Jens Axboe8c838782019-03-12 15:48:16 -06002172 unsigned long flags;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002173
2174 /* for instances that support it check for an event match first: */
Jens Axboe8c838782019-03-12 15:48:16 -06002175 if (mask && !(mask & poll->events))
2176 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002177
Jens Axboee9444752019-11-26 15:02:04 -07002178 list_del_init(&poll->wait->entry);
Jens Axboe8c838782019-03-12 15:48:16 -06002179
Jens Axboe7c9e7f02019-11-12 08:15:53 -07002180 /*
2181 * Run completion inline if we can. We're using trylock here because
2182 * we are violating the completion_lock -> poll wq lock ordering.
2183 * If we have a link timeout we're going to need the completion_lock
2184 * for finalizing the request, mark us as having grabbed that already.
2185 */
Jens Axboe8c838782019-03-12 15:48:16 -06002186 if (mask && spin_trylock_irqsave(&ctx->completion_lock, flags)) {
Jens Axboeeac406c2019-11-14 12:09:58 -07002187 io_poll_remove_req(req);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002188 io_poll_complete(req, mask, 0);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07002189 req->flags |= REQ_F_COMP_LOCKED;
2190 io_put_req(req);
Jens Axboe8c838782019-03-12 15:48:16 -06002191 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2192
2193 io_cqring_ev_posted(ctx);
Jens Axboe8c838782019-03-12 15:48:16 -06002194 } else {
Jackie Liua197f662019-11-08 08:09:12 -07002195 io_queue_async_work(req);
Jens Axboe8c838782019-03-12 15:48:16 -06002196 }
2197
Jens Axboe221c5eb2019-01-17 09:41:58 -07002198 return 1;
2199}
2200
2201struct io_poll_table {
2202 struct poll_table_struct pt;
2203 struct io_kiocb *req;
2204 int error;
2205};
2206
2207static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
2208 struct poll_table_struct *p)
2209{
2210 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
2211
2212 if (unlikely(pt->req->poll.head)) {
2213 pt->error = -EINVAL;
2214 return;
2215 }
2216
2217 pt->error = 0;
2218 pt->req->poll.head = head;
Jens Axboee9444752019-11-26 15:02:04 -07002219 add_wait_queue(head, pt->req->poll.wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002220}
2221
Jens Axboeeac406c2019-11-14 12:09:58 -07002222static void io_poll_req_insert(struct io_kiocb *req)
2223{
2224 struct io_ring_ctx *ctx = req->ctx;
2225 struct rb_node **p = &ctx->cancel_tree.rb_node;
2226 struct rb_node *parent = NULL;
2227 struct io_kiocb *tmp;
2228
2229 while (*p) {
2230 parent = *p;
2231 tmp = rb_entry(parent, struct io_kiocb, rb_node);
2232 if (req->user_data < tmp->user_data)
2233 p = &(*p)->rb_left;
2234 else
2235 p = &(*p)->rb_right;
2236 }
2237 rb_link_node(&req->rb_node, parent, p);
2238 rb_insert_color(&req->rb_node, &ctx->cancel_tree);
2239}
2240
Jens Axboe89723d02019-11-05 15:32:58 -07002241static int io_poll_add(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2242 struct io_kiocb **nxt)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002243{
2244 struct io_poll_iocb *poll = &req->poll;
2245 struct io_ring_ctx *ctx = req->ctx;
2246 struct io_poll_table ipt;
Jens Axboe8c838782019-03-12 15:48:16 -06002247 bool cancel = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002248 __poll_t mask;
2249 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002250
2251 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2252 return -EINVAL;
2253 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
2254 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06002255 if (!poll->file)
2256 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002257
Jens Axboee9444752019-11-26 15:02:04 -07002258 poll->wait = kmalloc(sizeof(*poll->wait), GFP_KERNEL);
2259 if (!poll->wait)
2260 return -ENOMEM;
2261
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002262 req->sqe = NULL;
Jens Axboe561fb042019-10-24 07:25:42 -06002263 INIT_IO_WORK(&req->work, io_poll_complete_work);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002264 events = READ_ONCE(sqe->poll_events);
2265 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
Jens Axboeeac406c2019-11-14 12:09:58 -07002266 RB_CLEAR_NODE(&req->rb_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002267
Jens Axboe221c5eb2019-01-17 09:41:58 -07002268 poll->head = NULL;
Jens Axboe8c838782019-03-12 15:48:16 -06002269 poll->done = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002270 poll->canceled = false;
2271
2272 ipt.pt._qproc = io_poll_queue_proc;
2273 ipt.pt._key = poll->events;
2274 ipt.req = req;
2275 ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
2276
2277 /* initialized the list so that we can do list_empty checks */
Jens Axboee9444752019-11-26 15:02:04 -07002278 INIT_LIST_HEAD(&poll->wait->entry);
2279 init_waitqueue_func_entry(poll->wait, io_poll_wake);
2280 poll->wait->private = poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002281
Jens Axboe36703242019-07-25 10:20:18 -06002282 INIT_LIST_HEAD(&req->list);
2283
Jens Axboe221c5eb2019-01-17 09:41:58 -07002284 mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002285
2286 spin_lock_irq(&ctx->completion_lock);
Jens Axboe8c838782019-03-12 15:48:16 -06002287 if (likely(poll->head)) {
2288 spin_lock(&poll->head->lock);
Jens Axboee9444752019-11-26 15:02:04 -07002289 if (unlikely(list_empty(&poll->wait->entry))) {
Jens Axboe8c838782019-03-12 15:48:16 -06002290 if (ipt.error)
2291 cancel = true;
2292 ipt.error = 0;
2293 mask = 0;
2294 }
2295 if (mask || ipt.error)
Jens Axboee9444752019-11-26 15:02:04 -07002296 list_del_init(&poll->wait->entry);
Jens Axboe8c838782019-03-12 15:48:16 -06002297 else if (cancel)
2298 WRITE_ONCE(poll->canceled, true);
2299 else if (!poll->done) /* actually waiting for an event */
Jens Axboeeac406c2019-11-14 12:09:58 -07002300 io_poll_req_insert(req);
Jens Axboe8c838782019-03-12 15:48:16 -06002301 spin_unlock(&poll->head->lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002302 }
Jens Axboe8c838782019-03-12 15:48:16 -06002303 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06002304 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002305 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06002306 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07002307 spin_unlock_irq(&ctx->completion_lock);
2308
Jens Axboe8c838782019-03-12 15:48:16 -06002309 if (mask) {
2310 io_cqring_ev_posted(ctx);
Jackie Liuec9c02a2019-11-08 23:50:36 +08002311 io_put_req_find_next(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002312 }
Jens Axboe8c838782019-03-12 15:48:16 -06002313 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002314}
2315
Jens Axboe5262f562019-09-17 12:26:57 -06002316static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
2317{
Jens Axboead8a48a2019-11-15 08:49:11 -07002318 struct io_timeout_data *data = container_of(timer,
2319 struct io_timeout_data, timer);
2320 struct io_kiocb *req = data->req;
2321 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06002322 unsigned long flags;
2323
Jens Axboe5262f562019-09-17 12:26:57 -06002324 atomic_inc(&ctx->cq_timeouts);
2325
2326 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08002327 /*
Jens Axboe11365042019-10-16 09:08:32 -06002328 * We could be racing with timeout deletion. If the list is empty,
2329 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08002330 */
Jens Axboe842f9612019-10-29 12:34:10 -06002331 if (!list_empty(&req->list)) {
Jens Axboe11365042019-10-16 09:08:32 -06002332 struct io_kiocb *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06002333
Jens Axboe11365042019-10-16 09:08:32 -06002334 /*
2335 * Adjust the reqs sequence before the current one because it
2336 * will consume a slot in the cq_ring and the the cq_tail
2337 * pointer will be increased, otherwise other timeout reqs may
2338 * return in advance without waiting for enough wait_nr.
2339 */
2340 prev = req;
2341 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
2342 prev->sequence++;
Jens Axboe11365042019-10-16 09:08:32 -06002343 list_del_init(&req->list);
Jens Axboe11365042019-10-16 09:08:32 -06002344 }
Jens Axboe842f9612019-10-29 12:34:10 -06002345
Jens Axboe78e19bb2019-11-06 15:21:34 -07002346 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06002347 io_commit_cqring(ctx);
2348 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2349
2350 io_cqring_ev_posted(ctx);
Jens Axboef1f40852019-11-05 20:33:16 -07002351 if (req->flags & REQ_F_LINK)
2352 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe5262f562019-09-17 12:26:57 -06002353 io_put_req(req);
2354 return HRTIMER_NORESTART;
2355}
2356
Jens Axboe47f46762019-11-09 17:43:02 -07002357static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
2358{
2359 struct io_kiocb *req;
2360 int ret = -ENOENT;
2361
2362 list_for_each_entry(req, &ctx->timeout_list, list) {
2363 if (user_data == req->user_data) {
2364 list_del_init(&req->list);
2365 ret = 0;
2366 break;
2367 }
2368 }
2369
2370 if (ret == -ENOENT)
2371 return ret;
2372
Jens Axboead8a48a2019-11-15 08:49:11 -07002373 ret = hrtimer_try_to_cancel(&req->timeout.data->timer);
Jens Axboe47f46762019-11-09 17:43:02 -07002374 if (ret == -1)
2375 return -EALREADY;
2376
Jens Axboefba38c22019-11-18 12:27:57 -07002377 if (req->flags & REQ_F_LINK)
2378 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe47f46762019-11-09 17:43:02 -07002379 io_cqring_fill_event(req, -ECANCELED);
2380 io_put_req(req);
2381 return 0;
2382}
2383
Jens Axboe11365042019-10-16 09:08:32 -06002384/*
2385 * Remove or update an existing timeout command
2386 */
2387static int io_timeout_remove(struct io_kiocb *req,
2388 const struct io_uring_sqe *sqe)
2389{
2390 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe11365042019-10-16 09:08:32 -06002391 unsigned flags;
Jens Axboe47f46762019-11-09 17:43:02 -07002392 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06002393
2394 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2395 return -EINVAL;
2396 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
2397 return -EINVAL;
2398 flags = READ_ONCE(sqe->timeout_flags);
2399 if (flags)
2400 return -EINVAL;
2401
Jens Axboe11365042019-10-16 09:08:32 -06002402 spin_lock_irq(&ctx->completion_lock);
Jens Axboe47f46762019-11-09 17:43:02 -07002403 ret = io_timeout_cancel(ctx, READ_ONCE(sqe->addr));
Jens Axboe11365042019-10-16 09:08:32 -06002404
Jens Axboe47f46762019-11-09 17:43:02 -07002405 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06002406 io_commit_cqring(ctx);
2407 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06002408 io_cqring_ev_posted(ctx);
Jens Axboe47f46762019-11-09 17:43:02 -07002409 if (ret < 0 && req->flags & REQ_F_LINK)
2410 req->flags |= REQ_F_FAIL_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08002411 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06002412 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06002413}
2414
Jens Axboead8a48a2019-11-15 08:49:11 -07002415static int io_timeout_setup(struct io_kiocb *req)
Jens Axboe5262f562019-09-17 12:26:57 -06002416{
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002417 const struct io_uring_sqe *sqe = req->sqe;
Jens Axboead8a48a2019-11-15 08:49:11 -07002418 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06002419 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06002420
Jens Axboead8a48a2019-11-15 08:49:11 -07002421 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06002422 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07002423 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06002424 return -EINVAL;
2425 flags = READ_ONCE(sqe->timeout_flags);
2426 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06002427 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06002428
Jens Axboead8a48a2019-11-15 08:49:11 -07002429 data = kzalloc(sizeof(struct io_timeout_data), GFP_KERNEL);
2430 if (!data)
2431 return -ENOMEM;
2432 data->req = req;
2433 req->timeout.data = data;
2434 req->flags |= REQ_F_TIMEOUT;
2435
2436 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06002437 return -EFAULT;
2438
Jens Axboe11365042019-10-16 09:08:32 -06002439 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07002440 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06002441 else
Jens Axboead8a48a2019-11-15 08:49:11 -07002442 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06002443
Jens Axboead8a48a2019-11-15 08:49:11 -07002444 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
2445 return 0;
2446}
2447
2448static int io_timeout(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2449{
2450 unsigned count;
2451 struct io_ring_ctx *ctx = req->ctx;
2452 struct io_timeout_data *data;
2453 struct list_head *entry;
2454 unsigned span = 0;
2455 int ret;
2456
2457 ret = io_timeout_setup(req);
2458 /* common setup allows flags (like links) set, we don't */
2459 if (!ret && sqe->flags)
2460 ret = -EINVAL;
2461 if (ret)
2462 return ret;
Jens Axboe93bd25b2019-11-11 23:34:31 -07002463
Jens Axboe5262f562019-09-17 12:26:57 -06002464 /*
2465 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07002466 * timeout event to be satisfied. If it isn't set, then this is
2467 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06002468 */
2469 count = READ_ONCE(sqe->off);
Jens Axboe93bd25b2019-11-11 23:34:31 -07002470 if (!count) {
2471 req->flags |= REQ_F_TIMEOUT_NOSEQ;
2472 spin_lock_irq(&ctx->completion_lock);
2473 entry = ctx->timeout_list.prev;
2474 goto add;
2475 }
Jens Axboe5262f562019-09-17 12:26:57 -06002476
2477 req->sequence = ctx->cached_sq_head + count - 1;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +03002478 req->timeout.data->seq_offset = count;
Jens Axboe5262f562019-09-17 12:26:57 -06002479
2480 /*
2481 * Insertion sort, ensuring the first entry in the list is always
2482 * the one we need first.
2483 */
Jens Axboe5262f562019-09-17 12:26:57 -06002484 spin_lock_irq(&ctx->completion_lock);
2485 list_for_each_prev(entry, &ctx->timeout_list) {
2486 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
yangerkun5da0fb12019-10-15 21:59:29 +08002487 unsigned nxt_sq_head;
2488 long long tmp, tmp_nxt;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +03002489 u32 nxt_offset = nxt->timeout.data->seq_offset;
Jens Axboe5262f562019-09-17 12:26:57 -06002490
Jens Axboe93bd25b2019-11-11 23:34:31 -07002491 if (nxt->flags & REQ_F_TIMEOUT_NOSEQ)
2492 continue;
2493
yangerkun5da0fb12019-10-15 21:59:29 +08002494 /*
2495 * Since cached_sq_head + count - 1 can overflow, use type long
2496 * long to store it.
2497 */
2498 tmp = (long long)ctx->cached_sq_head + count - 1;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +03002499 nxt_sq_head = nxt->sequence - nxt_offset + 1;
2500 tmp_nxt = (long long)nxt_sq_head + nxt_offset - 1;
yangerkun5da0fb12019-10-15 21:59:29 +08002501
2502 /*
2503 * cached_sq_head may overflow, and it will never overflow twice
2504 * once there is some timeout req still be valid.
2505 */
2506 if (ctx->cached_sq_head < nxt_sq_head)
yangerkun8b07a652019-10-17 12:12:35 +08002507 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08002508
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002509 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06002510 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002511
2512 /*
2513 * Sequence of reqs after the insert one and itself should
2514 * be adjusted because each timeout req consumes a slot.
2515 */
2516 span++;
2517 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06002518 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002519 req->sequence -= span;
Jens Axboe93bd25b2019-11-11 23:34:31 -07002520add:
Jens Axboe5262f562019-09-17 12:26:57 -06002521 list_add(&req->list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07002522 data = req->timeout.data;
2523 data->timer.function = io_timeout_fn;
2524 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06002525 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06002526 return 0;
2527}
2528
Jens Axboe62755e32019-10-28 21:49:21 -06002529static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06002530{
Jens Axboe62755e32019-10-28 21:49:21 -06002531 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06002532
Jens Axboe62755e32019-10-28 21:49:21 -06002533 return req->user_data == (unsigned long) data;
2534}
2535
Jens Axboee977d6d2019-11-05 12:39:45 -07002536static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06002537{
Jens Axboe62755e32019-10-28 21:49:21 -06002538 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06002539 int ret = 0;
2540
Jens Axboe62755e32019-10-28 21:49:21 -06002541 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
2542 switch (cancel_ret) {
2543 case IO_WQ_CANCEL_OK:
2544 ret = 0;
2545 break;
2546 case IO_WQ_CANCEL_RUNNING:
2547 ret = -EALREADY;
2548 break;
2549 case IO_WQ_CANCEL_NOTFOUND:
2550 ret = -ENOENT;
2551 break;
2552 }
2553
Jens Axboee977d6d2019-11-05 12:39:45 -07002554 return ret;
2555}
2556
Jens Axboe47f46762019-11-09 17:43:02 -07002557static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
2558 struct io_kiocb *req, __u64 sqe_addr,
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002559 struct io_kiocb **nxt, int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07002560{
2561 unsigned long flags;
2562 int ret;
2563
2564 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
2565 if (ret != -ENOENT) {
2566 spin_lock_irqsave(&ctx->completion_lock, flags);
2567 goto done;
2568 }
2569
2570 spin_lock_irqsave(&ctx->completion_lock, flags);
2571 ret = io_timeout_cancel(ctx, sqe_addr);
2572 if (ret != -ENOENT)
2573 goto done;
2574 ret = io_poll_cancel(ctx, sqe_addr);
2575done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002576 if (!ret)
2577 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07002578 io_cqring_fill_event(req, ret);
2579 io_commit_cqring(ctx);
2580 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2581 io_cqring_ev_posted(ctx);
2582
2583 if (ret < 0 && (req->flags & REQ_F_LINK))
2584 req->flags |= REQ_F_FAIL_LINK;
2585 io_put_req_find_next(req, nxt);
2586}
2587
Jens Axboee977d6d2019-11-05 12:39:45 -07002588static int io_async_cancel(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2589 struct io_kiocb **nxt)
2590{
2591 struct io_ring_ctx *ctx = req->ctx;
Jens Axboee977d6d2019-11-05 12:39:45 -07002592
2593 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2594 return -EINVAL;
2595 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
2596 sqe->cancel_flags)
2597 return -EINVAL;
2598
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002599 io_async_find_and_cancel(ctx, req, READ_ONCE(sqe->addr), nxt, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06002600 return 0;
2601}
2602
Jackie Liua197f662019-11-08 08:09:12 -07002603static int io_req_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -06002604{
2605 struct io_uring_sqe *sqe_copy;
Jackie Liua197f662019-11-08 08:09:12 -07002606 struct io_ring_ctx *ctx = req->ctx;
Jens Axboede0617e2019-04-06 21:51:27 -06002607
Bob Liu9d858b22019-11-13 18:06:25 +08002608 /* Still need defer if there is pending req in defer list. */
2609 if (!req_need_defer(req) && list_empty(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06002610 return 0;
2611
2612 sqe_copy = kmalloc(sizeof(*sqe_copy), GFP_KERNEL);
2613 if (!sqe_copy)
2614 return -EAGAIN;
2615
2616 spin_lock_irq(&ctx->completion_lock);
Bob Liu9d858b22019-11-13 18:06:25 +08002617 if (!req_need_defer(req) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06002618 spin_unlock_irq(&ctx->completion_lock);
2619 kfree(sqe_copy);
2620 return 0;
2621 }
2622
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002623 memcpy(sqe_copy, req->sqe, sizeof(*sqe_copy));
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03002624 req->flags |= REQ_F_FREE_SQE;
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002625 req->sqe = sqe_copy;
Jens Axboede0617e2019-04-06 21:51:27 -06002626
Jens Axboe915967f2019-11-21 09:01:20 -07002627 trace_io_uring_defer(ctx, req, req->user_data);
Jens Axboede0617e2019-04-06 21:51:27 -06002628 list_add_tail(&req->list, &ctx->defer_list);
2629 spin_unlock_irq(&ctx->completion_lock);
2630 return -EIOCBQUEUED;
2631}
2632
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03002633__attribute__((nonnull))
Pavel Begunkovd7324472019-11-21 21:24:36 +03002634static int io_issue_sqe(struct io_kiocb *req, struct io_kiocb **nxt,
2635 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002636{
Jens Axboee0c5c572019-03-12 10:18:47 -06002637 int ret, opcode;
Jackie Liua197f662019-11-08 08:09:12 -07002638 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002639
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002640 opcode = READ_ONCE(req->sqe->opcode);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002641 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:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002646 if (unlikely(req->sqe->buf_index))
Jens Axboeedafcce2019-01-09 09:16:05 -07002647 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:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002651 if (unlikely(req->sqe->buf_index))
Jens Axboeedafcce2019-01-09 09:16:05 -07002652 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:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002662 ret = io_fsync(req, req->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:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002665 ret = io_poll_add(req, req->sqe, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002666 break;
2667 case IORING_OP_POLL_REMOVE:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002668 ret = io_poll_remove(req, req->sqe);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002669 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002670 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002671 ret = io_sync_file_range(req, req->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:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002674 ret = io_sendmsg(req, req->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:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002677 ret = io_recvmsg(req, req->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:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002680 ret = io_timeout(req, req->sqe);
Jens Axboe5262f562019-09-17 12:26:57 -06002681 break;
Jens Axboe11365042019-10-16 09:08:32 -06002682 case IORING_OP_TIMEOUT_REMOVE:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002683 ret = io_timeout_remove(req, req->sqe);
Jens Axboe11365042019-10-16 09:08:32 -06002684 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06002685 case IORING_OP_ACCEPT:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002686 ret = io_accept(req, req->sqe, nxt, force_nonblock);
Jens Axboe17f2fe32019-10-17 14:42:58 -06002687 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07002688 case IORING_OP_CONNECT:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002689 ret = io_connect(req, req->sqe, nxt, force_nonblock);
Jens Axboef8e85cf2019-11-23 14:24:24 -07002690 break;
Jens Axboe62755e32019-10-28 21:49:21 -06002691 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002692 ret = io_async_cancel(req, req->sqe, nxt);
Jens Axboe62755e32019-10-28 21:49:21 -06002693 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 */
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002707 if (req->in_async)
Jens Axboedef596e2019-01-09 08:59:42 -07002708 mutex_lock(&ctx->uring_lock);
2709 io_iopoll_req_issued(req);
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002710 if (req->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 io_kiocb *nxt = NULL;
2731 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002732
Jens Axboe561fb042019-10-24 07:25:42 -06002733 /* Ensure we clear previously set non-block flag */
2734 req->rw.ki_flags &= ~IOCB_NOWAIT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002735
Jens Axboe561fb042019-10-24 07:25:42 -06002736 if (work->flags & IO_WQ_WORK_CANCEL)
2737 ret = -ECANCELED;
Jens Axboe31b51512019-01-18 22:56:34 -07002738
Jens Axboe561fb042019-10-24 07:25:42 -06002739 if (!ret) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002740 req->has_user = (work->flags & IO_WQ_WORK_HAS_MM) != 0;
2741 req->in_async = true;
Jens Axboe561fb042019-10-24 07:25:42 -06002742 do {
Pavel Begunkovd7324472019-11-21 21:24:36 +03002743 ret = io_issue_sqe(req, &nxt, false);
Jens Axboe561fb042019-10-24 07:25:42 -06002744 /*
2745 * We can get EAGAIN for polled IO even though we're
2746 * forcing a sync submission from here, since we can't
2747 * wait for request slots on the block side.
2748 */
2749 if (ret != -EAGAIN)
2750 break;
2751 cond_resched();
2752 } while (1);
2753 }
Jens Axboe31b51512019-01-18 22:56:34 -07002754
Jens Axboe561fb042019-10-24 07:25:42 -06002755 /* drop submission reference */
Jackie Liuec9c02a2019-11-08 23:50:36 +08002756 io_put_req(req);
Jens Axboe817869d2019-04-30 14:44:05 -06002757
Jens Axboe561fb042019-10-24 07:25:42 -06002758 if (ret) {
Jens Axboef1f40852019-11-05 20:33:16 -07002759 if (req->flags & REQ_F_LINK)
2760 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07002761 io_cqring_add_event(req, ret);
Jens Axboe817869d2019-04-30 14:44:05 -06002762 io_put_req(req);
Jens Axboeedafcce2019-01-09 09:16:05 -07002763 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002764
Jens Axboe561fb042019-10-24 07:25:42 -06002765 /* if a dependent link is ready, pass it back */
2766 if (!ret && nxt) {
Jens Axboe94ae5e72019-11-14 19:39:52 -07002767 struct io_kiocb *link;
2768
2769 io_prep_async_work(nxt, &link);
Jens Axboe561fb042019-10-24 07:25:42 -06002770 *workptr = &nxt->work;
Jens Axboeb76da702019-11-20 13:05:32 -07002771 if (link) {
2772 nxt->work.flags |= IO_WQ_WORK_CB;
2773 nxt->work.func = io_link_work_cb;
2774 nxt->work.data = link;
2775 }
Jens Axboeedafcce2019-01-09 09:16:05 -07002776 }
Jens Axboe31b51512019-01-18 22:56:34 -07002777}
Jens Axboe2b188cc2019-01-07 10:46:33 -07002778
Jens Axboe09bb8392019-03-13 12:39:28 -06002779static bool io_op_needs_file(const struct io_uring_sqe *sqe)
2780{
2781 int op = READ_ONCE(sqe->opcode);
2782
2783 switch (op) {
2784 case IORING_OP_NOP:
2785 case IORING_OP_POLL_REMOVE:
Pavel Begunkov5683e542019-11-14 00:59:19 +03002786 case IORING_OP_TIMEOUT:
Pavel Begunkova320e9f2019-11-14 00:11:01 +03002787 case IORING_OP_TIMEOUT_REMOVE:
2788 case IORING_OP_ASYNC_CANCEL:
2789 case IORING_OP_LINK_TIMEOUT:
Jens Axboe09bb8392019-03-13 12:39:28 -06002790 return false;
2791 default:
2792 return true;
2793 }
2794}
2795
Jens Axboe65e19f52019-10-26 07:20:21 -06002796static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
2797 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06002798{
Jens Axboe65e19f52019-10-26 07:20:21 -06002799 struct fixed_file_table *table;
2800
2801 table = &ctx->file_table[index >> IORING_FILE_TABLE_SHIFT];
2802 return table->files[index & IORING_FILE_TABLE_MASK];
2803}
2804
Jackie Liua197f662019-11-08 08:09:12 -07002805static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req)
Jens Axboe09bb8392019-03-13 12:39:28 -06002806{
Jackie Liua197f662019-11-08 08:09:12 -07002807 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe09bb8392019-03-13 12:39:28 -06002808 unsigned flags;
2809 int fd;
2810
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002811 flags = READ_ONCE(req->sqe->flags);
2812 fd = READ_ONCE(req->sqe->fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06002813
Jackie Liu4fe2c962019-09-09 20:50:40 +08002814 if (flags & IOSQE_IO_DRAIN)
Jens Axboede0617e2019-04-06 21:51:27 -06002815 req->flags |= REQ_F_IO_DRAIN;
Jens Axboede0617e2019-04-06 21:51:27 -06002816
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002817 if (!io_op_needs_file(req->sqe))
Jens Axboe09bb8392019-03-13 12:39:28 -06002818 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06002819
2820 if (flags & IOSQE_FIXED_FILE) {
Jens Axboe65e19f52019-10-26 07:20:21 -06002821 if (unlikely(!ctx->file_table ||
Jens Axboe09bb8392019-03-13 12:39:28 -06002822 (unsigned) fd >= ctx->nr_user_files))
2823 return -EBADF;
Jens Axboeb7620122019-10-26 07:22:55 -06002824 fd = array_index_nospec(fd, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06002825 req->file = io_file_from_index(ctx, fd);
2826 if (!req->file)
Jens Axboe08a45172019-10-03 08:11:03 -06002827 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06002828 req->flags |= REQ_F_FIXED_FILE;
2829 } else {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002830 if (req->needs_fixed_file)
Jens Axboe09bb8392019-03-13 12:39:28 -06002831 return -EBADF;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002832 trace_io_uring_file_get(ctx, fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06002833 req->file = io_file_get(state, fd);
2834 if (unlikely(!req->file))
2835 return -EBADF;
2836 }
2837
2838 return 0;
2839}
2840
Jackie Liua197f662019-11-08 08:09:12 -07002841static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002842{
Jens Axboefcb323c2019-10-24 12:39:47 -06002843 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07002844 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06002845
2846 rcu_read_lock();
2847 spin_lock_irq(&ctx->inflight_lock);
2848 /*
2849 * We use the f_ops->flush() handler to ensure that we can flush
2850 * out work accessing these files if the fd is closed. Check if
2851 * the fd has changed since we started down this path, and disallow
2852 * this operation if it has.
2853 */
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002854 if (fcheck(req->ring_fd) == req->ring_file) {
Jens Axboefcb323c2019-10-24 12:39:47 -06002855 list_add(&req->inflight_entry, &ctx->inflight_list);
2856 req->flags |= REQ_F_INFLIGHT;
2857 req->work.files = current->files;
2858 ret = 0;
2859 }
2860 spin_unlock_irq(&ctx->inflight_lock);
2861 rcu_read_unlock();
2862
2863 return ret;
2864}
2865
Jens Axboe2665abf2019-11-05 12:40:47 -07002866static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
2867{
Jens Axboead8a48a2019-11-15 08:49:11 -07002868 struct io_timeout_data *data = container_of(timer,
2869 struct io_timeout_data, timer);
2870 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07002871 struct io_ring_ctx *ctx = req->ctx;
2872 struct io_kiocb *prev = NULL;
2873 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07002874
2875 spin_lock_irqsave(&ctx->completion_lock, flags);
2876
2877 /*
2878 * We don't expect the list to be empty, that will only happen if we
2879 * race with the completion of the linked work.
2880 */
2881 if (!list_empty(&req->list)) {
2882 prev = list_entry(req->list.prev, struct io_kiocb, link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07002883 if (refcount_inc_not_zero(&prev->refs)) {
Jens Axboe76a46e02019-11-10 23:34:16 -07002884 list_del_init(&req->list);
Jens Axboe5d960722019-11-19 15:31:28 -07002885 prev->flags &= ~REQ_F_LINK_TIMEOUT;
2886 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07002887 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07002888 }
2889
2890 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2891
2892 if (prev) {
Jens Axboefba38c22019-11-18 12:27:57 -07002893 if (prev->flags & REQ_F_LINK)
2894 prev->flags |= REQ_F_FAIL_LINK;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002895 io_async_find_and_cancel(ctx, req, prev->user_data, NULL,
2896 -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07002897 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07002898 } else {
2899 io_cqring_add_event(req, -ETIME);
2900 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07002901 }
Jens Axboe2665abf2019-11-05 12:40:47 -07002902 return HRTIMER_NORESTART;
2903}
2904
Jens Axboead8a48a2019-11-15 08:49:11 -07002905static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07002906{
Jens Axboe76a46e02019-11-10 23:34:16 -07002907 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07002908
Jens Axboe76a46e02019-11-10 23:34:16 -07002909 /*
2910 * If the list is now empty, then our linked request finished before
2911 * we got a chance to setup the timer
2912 */
2913 spin_lock_irq(&ctx->completion_lock);
2914 if (!list_empty(&req->list)) {
Jens Axboe94ae5e72019-11-14 19:39:52 -07002915 struct io_timeout_data *data = req->timeout.data;
2916
Jens Axboead8a48a2019-11-15 08:49:11 -07002917 data->timer.function = io_link_timeout_fn;
2918 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
2919 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07002920 }
Jens Axboe76a46e02019-11-10 23:34:16 -07002921 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07002922
Jens Axboe2665abf2019-11-05 12:40:47 -07002923 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07002924 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07002925}
2926
Jens Axboead8a48a2019-11-15 08:49:11 -07002927static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07002928{
2929 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002930
Jens Axboe2665abf2019-11-05 12:40:47 -07002931 if (!(req->flags & REQ_F_LINK))
2932 return NULL;
2933
2934 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, list);
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002935 if (!nxt || nxt->sqe->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07002936 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07002937
Jens Axboe76a46e02019-11-10 23:34:16 -07002938 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07002939 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07002940}
2941
Jens Axboe0e0702d2019-11-14 21:42:10 -07002942static void __io_queue_sqe(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002943{
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03002944 struct io_kiocb *linked_timeout = io_prep_linked_timeout(req);
2945 struct io_kiocb *nxt = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002946 int ret;
2947
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03002948 ret = io_issue_sqe(req, &nxt, true);
2949 if (nxt)
2950 io_queue_async_work(nxt);
Jens Axboe491381ce2019-10-17 09:20:46 -06002951
2952 /*
2953 * We async punt it if the file wasn't marked NOWAIT, or if the file
2954 * doesn't support non-blocking read/write attempts
2955 */
2956 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
2957 (req->flags & REQ_F_MUST_PUNT))) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07002958 struct io_uring_sqe *sqe_copy;
2959
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002960 sqe_copy = kmemdup(req->sqe, sizeof(*sqe_copy), GFP_KERNEL);
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03002961 if (!sqe_copy)
2962 goto err;
Jens Axboee65ef562019-03-12 10:16:44 -06002963
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002964 req->sqe = sqe_copy;
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03002965 req->flags |= REQ_F_FREE_SQE;
2966
2967 if (req->work.flags & IO_WQ_WORK_NEEDS_FILES) {
2968 ret = io_grab_files(req);
2969 if (ret)
2970 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002971 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03002972
2973 /*
2974 * Queued up for async execution, worker will release
2975 * submit reference when the iocb is actually submitted.
2976 */
2977 io_queue_async_work(req);
2978 return;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002979 }
Jens Axboee65ef562019-03-12 10:16:44 -06002980
Jens Axboefcb323c2019-10-24 12:39:47 -06002981err:
Jens Axboee65ef562019-03-12 10:16:44 -06002982 /* drop submission reference */
2983 io_put_req(req);
2984
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03002985 if (linked_timeout) {
Jens Axboe76a46e02019-11-10 23:34:16 -07002986 if (!ret)
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03002987 io_queue_linked_timeout(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07002988 else
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03002989 io_put_req(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07002990 }
2991
Jens Axboee65ef562019-03-12 10:16:44 -06002992 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06002993 if (ret) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07002994 io_cqring_add_event(req, ret);
Jens Axboe9e645e112019-05-10 16:07:28 -06002995 if (req->flags & REQ_F_LINK)
2996 req->flags |= REQ_F_FAIL_LINK;
Jens Axboee65ef562019-03-12 10:16:44 -06002997 io_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002998 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002999}
3000
Jens Axboe0e0702d2019-11-14 21:42:10 -07003001static void io_queue_sqe(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08003002{
3003 int ret;
3004
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003005 if (unlikely(req->ctx->drain_next)) {
3006 req->flags |= REQ_F_IO_DRAIN;
3007 req->ctx->drain_next = false;
3008 }
3009 req->ctx->drain_next = (req->flags & REQ_F_DRAIN_LINK);
3010
Jackie Liua197f662019-11-08 08:09:12 -07003011 ret = io_req_defer(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08003012 if (ret) {
3013 if (ret != -EIOCBQUEUED) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07003014 io_cqring_add_event(req, ret);
Pavel Begunkovd3b357962019-11-19 23:32:48 +03003015 if (req->flags & REQ_F_LINK)
3016 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07003017 io_double_put_req(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08003018 }
Jens Axboe0e0702d2019-11-14 21:42:10 -07003019 } else
3020 __io_queue_sqe(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08003021}
3022
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003023static inline void io_queue_link_head(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08003024{
Jens Axboe94ae5e72019-11-14 19:39:52 -07003025 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003026 io_cqring_add_event(req, -ECANCELED);
3027 io_double_put_req(req);
3028 } else
Jens Axboe0e0702d2019-11-14 21:42:10 -07003029 io_queue_sqe(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08003030}
3031
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003032
Jens Axboe9e645e112019-05-10 16:07:28 -06003033#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK)
3034
Jackie Liua197f662019-11-08 08:09:12 -07003035static void io_submit_sqe(struct io_kiocb *req, struct io_submit_state *state,
3036 struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06003037{
Jackie Liua197f662019-11-08 08:09:12 -07003038 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -06003039 int ret;
3040
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003041 req->user_data = req->sqe->user_data;
Jens Axboe78e19bb2019-11-06 15:21:34 -07003042
Jens Axboe9e645e112019-05-10 16:07:28 -06003043 /* enforce forwards compatibility on users */
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003044 if (unlikely(req->sqe->flags & ~SQE_VALID_FLAGS)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06003045 ret = -EINVAL;
Pavel Begunkov196be952019-11-07 01:41:06 +03003046 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06003047 }
3048
Jackie Liua197f662019-11-08 08:09:12 -07003049 ret = io_req_set_file(state, req);
Jens Axboe9e645e112019-05-10 16:07:28 -06003050 if (unlikely(ret)) {
3051err_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07003052 io_cqring_add_event(req, ret);
3053 io_double_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06003054 return;
3055 }
3056
Jens Axboe9e645e112019-05-10 16:07:28 -06003057 /*
3058 * If we already have a head request, queue this one for async
3059 * submittal once the head completes. If we don't have a head but
3060 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
3061 * submitted sync once the chain is complete. If none of those
3062 * conditions are true (normal request), then just queue it.
3063 */
3064 if (*link) {
3065 struct io_kiocb *prev = *link;
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03003066 struct io_uring_sqe *sqe_copy;
Jens Axboe9e645e112019-05-10 16:07:28 -06003067
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003068 if (req->sqe->flags & IOSQE_IO_DRAIN)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003069 (*link)->flags |= REQ_F_DRAIN_LINK | REQ_F_IO_DRAIN;
3070
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003071 if (READ_ONCE(req->sqe->opcode) == IORING_OP_LINK_TIMEOUT) {
Jens Axboe94ae5e72019-11-14 19:39:52 -07003072 ret = io_timeout_setup(req);
3073 /* common setup allows offset being set, we don't */
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003074 if (!ret && req->sqe->off)
Jens Axboe94ae5e72019-11-14 19:39:52 -07003075 ret = -EINVAL;
3076 if (ret) {
3077 prev->flags |= REQ_F_FAIL_LINK;
3078 goto err_req;
3079 }
3080 }
3081
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003082 sqe_copy = kmemdup(req->sqe, sizeof(*sqe_copy), GFP_KERNEL);
Jens Axboe9e645e112019-05-10 16:07:28 -06003083 if (!sqe_copy) {
3084 ret = -EAGAIN;
3085 goto err_req;
3086 }
3087
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003088 req->sqe = sqe_copy;
Jens Axboe94ae5e72019-11-14 19:39:52 -07003089 req->flags |= REQ_F_FREE_SQE;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02003090 trace_io_uring_link(ctx, req, prev);
Jens Axboe9e645e112019-05-10 16:07:28 -06003091 list_add_tail(&req->list, &prev->link_list);
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003092 } else if (req->sqe->flags & IOSQE_IO_LINK) {
Jens Axboe9e645e112019-05-10 16:07:28 -06003093 req->flags |= REQ_F_LINK;
3094
Jens Axboe9e645e112019-05-10 16:07:28 -06003095 INIT_LIST_HEAD(&req->link_list);
3096 *link = req;
3097 } else {
Jackie Liua197f662019-11-08 08:09:12 -07003098 io_queue_sqe(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06003099 }
3100}
3101
Jens Axboe9a56a232019-01-09 09:06:50 -07003102/*
3103 * Batched submission is done, ensure local IO is flushed out.
3104 */
3105static void io_submit_state_end(struct io_submit_state *state)
3106{
3107 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06003108 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07003109 if (state->free_reqs)
3110 kmem_cache_free_bulk(req_cachep, state->free_reqs,
3111 &state->reqs[state->cur_req]);
Jens Axboe9a56a232019-01-09 09:06:50 -07003112}
3113
3114/*
3115 * Start submission side cache.
3116 */
3117static void io_submit_state_start(struct io_submit_state *state,
3118 struct io_ring_ctx *ctx, unsigned max_ios)
3119{
3120 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07003121 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07003122 state->file = NULL;
3123 state->ios_left = max_ios;
3124}
3125
Jens Axboe2b188cc2019-01-07 10:46:33 -07003126static void io_commit_sqring(struct io_ring_ctx *ctx)
3127{
Hristo Venev75b28af2019-08-26 17:23:46 +00003128 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003129
Hristo Venev75b28af2019-08-26 17:23:46 +00003130 if (ctx->cached_sq_head != READ_ONCE(rings->sq.head)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07003131 /*
3132 * Ensure any loads from the SQEs are done at this point,
3133 * since once we write the new head, the application could
3134 * write new data to them.
3135 */
Hristo Venev75b28af2019-08-26 17:23:46 +00003136 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003137 }
3138}
3139
3140/*
Jens Axboe2b188cc2019-01-07 10:46:33 -07003141 * Fetch an sqe, if one is available. Note that s->sqe will point to memory
3142 * that is mapped by userspace. This means that care needs to be taken to
3143 * ensure that reads are stable, as we cannot rely on userspace always
3144 * being a good citizen. If members of the sqe are validated and then later
3145 * used, it's important that those reads are done through READ_ONCE() to
3146 * prevent a re-load down the line.
3147 */
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003148static bool io_get_sqring(struct io_ring_ctx *ctx, struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003149{
Hristo Venev75b28af2019-08-26 17:23:46 +00003150 struct io_rings *rings = ctx->rings;
3151 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003152 unsigned head;
3153
3154 /*
3155 * The cached sq head (or cq tail) serves two purposes:
3156 *
3157 * 1) allows us to batch the cost of updating the user visible
3158 * head updates.
3159 * 2) allows the kernel side to track the head on its own, even
3160 * though the application is the one updating it.
3161 */
3162 head = ctx->cached_sq_head;
Stefan Bühlere523a292019-04-19 11:57:44 +02003163 /* make sure SQ entry isn't read before tail */
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03003164 if (unlikely(head == smp_load_acquire(&rings->sq.tail)))
Jens Axboe2b188cc2019-01-07 10:46:33 -07003165 return false;
3166
Hristo Venev75b28af2019-08-26 17:23:46 +00003167 head = READ_ONCE(sq_array[head & ctx->sq_mask]);
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03003168 if (likely(head < ctx->sq_entries)) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003169 /*
3170 * All io need record the previous position, if LINK vs DARIN,
3171 * it can be used to mark the position of the first IO in the
3172 * link list.
3173 */
3174 req->sequence = ctx->cached_sq_head;
3175 req->sqe = &ctx->sq_sqes[head];
Jens Axboe2b188cc2019-01-07 10:46:33 -07003176 ctx->cached_sq_head++;
3177 return true;
3178 }
3179
3180 /* drop invalid entries */
3181 ctx->cached_sq_head++;
Jens Axboe498ccd92019-10-25 10:04:25 -06003182 ctx->cached_sq_dropped++;
3183 WRITE_ONCE(rings->sq_dropped, ctx->cached_sq_dropped);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003184 return false;
3185}
3186
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003187static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkovae9428c2019-11-06 00:22:14 +03003188 struct file *ring_file, int ring_fd,
3189 struct mm_struct **mm, bool async)
Jens Axboe6c271ce2019-01-10 11:22:30 -07003190{
3191 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06003192 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06003193 int i, submitted = 0;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03003194 bool mm_fault = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003195
Jens Axboec4a2ed72019-11-21 21:01:26 -07003196 /* if we have a backlog and couldn't flush it all, return BUSY */
3197 if (!list_empty(&ctx->cq_overflow_list) &&
3198 !io_cqring_overflow_flush(ctx, false))
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003199 return -EBUSY;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003200
3201 if (nr > IO_PLUG_THRESHOLD) {
3202 io_submit_state_start(&state, ctx, nr);
3203 statep = &state;
3204 }
3205
3206 for (i = 0; i < nr; i++) {
Pavel Begunkov196be952019-11-07 01:41:06 +03003207 struct io_kiocb *req;
Pavel Begunkov50585b92019-11-07 01:41:07 +03003208 unsigned int sqe_flags;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003209
Pavel Begunkov196be952019-11-07 01:41:06 +03003210 req = io_get_req(ctx, statep);
3211 if (unlikely(!req)) {
3212 if (!submitted)
3213 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003214 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06003215 }
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003216 if (!io_get_sqring(ctx, req)) {
Pavel Begunkov196be952019-11-07 01:41:06 +03003217 __io_free_req(req);
3218 break;
3219 }
Jens Axboe9e645e112019-05-10 16:07:28 -06003220
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003221 if (io_sqe_needs_user(req->sqe) && !*mm) {
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03003222 mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm);
3223 if (!mm_fault) {
3224 use_mm(ctx->sqo_mm);
3225 *mm = ctx->sqo_mm;
3226 }
3227 }
3228
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003229 sqe_flags = req->sqe->flags;
Pavel Begunkov50585b92019-11-07 01:41:07 +03003230
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003231 req->ring_file = ring_file;
3232 req->ring_fd = ring_fd;
3233 req->has_user = *mm != NULL;
3234 req->in_async = async;
3235 req->needs_fixed_file = async;
3236 trace_io_uring_submit_sqe(ctx, req->sqe->user_data,
Pavel Begunkov50585b92019-11-07 01:41:07 +03003237 true, async);
Jackie Liua197f662019-11-08 08:09:12 -07003238 io_submit_sqe(req, statep, &link);
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03003239 submitted++;
Pavel Begunkove5eb6362019-11-06 00:22:15 +03003240
3241 /*
3242 * If previous wasn't linked and we have a linked command,
3243 * that's the end of the chain. Submit the previous link.
3244 */
Pavel Begunkov50585b92019-11-07 01:41:07 +03003245 if (!(sqe_flags & IOSQE_IO_LINK) && link) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003246 io_queue_link_head(link);
Pavel Begunkove5eb6362019-11-06 00:22:15 +03003247 link = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003248 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07003249 }
3250
Jens Axboe9e645e112019-05-10 16:07:28 -06003251 if (link)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003252 io_queue_link_head(link);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003253 if (statep)
3254 io_submit_state_end(&state);
3255
Pavel Begunkovae9428c2019-11-06 00:22:14 +03003256 /* Commit SQ ring head once we've consumed and submitted all SQEs */
3257 io_commit_sqring(ctx);
3258
Jens Axboe6c271ce2019-01-10 11:22:30 -07003259 return submitted;
3260}
3261
3262static int io_sq_thread(void *data)
3263{
Jens Axboe6c271ce2019-01-10 11:22:30 -07003264 struct io_ring_ctx *ctx = data;
3265 struct mm_struct *cur_mm = NULL;
Jens Axboe181e4482019-11-25 08:52:30 -07003266 const struct cred *old_cred;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003267 mm_segment_t old_fs;
3268 DEFINE_WAIT(wait);
3269 unsigned inflight;
3270 unsigned long timeout;
Jens Axboec1edbf52019-11-10 16:56:04 -07003271 int ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003272
Jens Axboe206aefd2019-11-07 18:27:42 -07003273 complete(&ctx->completions[1]);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08003274
Jens Axboe6c271ce2019-01-10 11:22:30 -07003275 old_fs = get_fs();
3276 set_fs(USER_DS);
Jens Axboe181e4482019-11-25 08:52:30 -07003277 old_cred = override_creds(ctx->creds);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003278
Jens Axboec1edbf52019-11-10 16:56:04 -07003279 ret = timeout = inflight = 0;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003280 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003281 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003282
3283 if (inflight) {
3284 unsigned nr_events = 0;
3285
3286 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe2b2ed972019-10-25 10:06:15 -06003287 /*
3288 * inflight is the count of the maximum possible
3289 * entries we submitted, but it can be smaller
3290 * if we dropped some of them. If we don't have
3291 * poll entries available, then we know that we
3292 * have nothing left to poll for. Reset the
3293 * inflight count to zero in that case.
3294 */
3295 mutex_lock(&ctx->uring_lock);
3296 if (!list_empty(&ctx->poll_list))
3297 __io_iopoll_check(ctx, &nr_events, 0);
3298 else
3299 inflight = 0;
3300 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003301 } else {
3302 /*
3303 * Normal IO, just pretend everything completed.
3304 * We don't have to poll completions for that.
3305 */
3306 nr_events = inflight;
3307 }
3308
3309 inflight -= nr_events;
3310 if (!inflight)
3311 timeout = jiffies + ctx->sq_thread_idle;
3312 }
3313
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003314 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07003315
3316 /*
3317 * If submit got -EBUSY, flag us as needing the application
3318 * to enter the kernel to reap and flush events.
3319 */
3320 if (!to_submit || ret == -EBUSY) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07003321 /*
3322 * We're polling. If we're within the defined idle
3323 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07003324 * to sleep. The exception is if we got EBUSY doing
3325 * more IO, we should wait for the application to
3326 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07003327 */
Jens Axboec1edbf52019-11-10 16:56:04 -07003328 if (inflight ||
3329 (!time_after(jiffies, timeout) && ret != -EBUSY)) {
Jens Axboe9831a902019-09-19 09:48:55 -06003330 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07003331 continue;
3332 }
3333
3334 /*
3335 * Drop cur_mm before scheduling, we can't hold it for
3336 * long periods (or over schedule()). Do this before
3337 * adding ourselves to the waitqueue, as the unuse/drop
3338 * may sleep.
3339 */
3340 if (cur_mm) {
3341 unuse_mm(cur_mm);
3342 mmput(cur_mm);
3343 cur_mm = NULL;
3344 }
3345
3346 prepare_to_wait(&ctx->sqo_wait, &wait,
3347 TASK_INTERRUPTIBLE);
3348
3349 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00003350 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02003351 /* make sure to read SQ tail after writing flags */
3352 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07003353
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003354 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07003355 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003356 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07003357 finish_wait(&ctx->sqo_wait, &wait);
3358 break;
3359 }
3360 if (signal_pending(current))
3361 flush_signals(current);
3362 schedule();
3363 finish_wait(&ctx->sqo_wait, &wait);
3364
Hristo Venev75b28af2019-08-26 17:23:46 +00003365 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003366 continue;
3367 }
3368 finish_wait(&ctx->sqo_wait, &wait);
3369
Hristo Venev75b28af2019-08-26 17:23:46 +00003370 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003371 }
3372
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003373 to_submit = min(to_submit, ctx->sq_entries);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003374 ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true);
3375 if (ret > 0)
3376 inflight += ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003377 }
3378
3379 set_fs(old_fs);
3380 if (cur_mm) {
3381 unuse_mm(cur_mm);
3382 mmput(cur_mm);
3383 }
Jens Axboe181e4482019-11-25 08:52:30 -07003384 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06003385
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003386 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06003387
Jens Axboe6c271ce2019-01-10 11:22:30 -07003388 return 0;
3389}
3390
Jens Axboebda52162019-09-24 13:47:15 -06003391struct io_wait_queue {
3392 struct wait_queue_entry wq;
3393 struct io_ring_ctx *ctx;
3394 unsigned to_wait;
3395 unsigned nr_timeouts;
3396};
3397
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003398static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06003399{
3400 struct io_ring_ctx *ctx = iowq->ctx;
3401
3402 /*
3403 * Wake up if we have enough events, or if a timeout occured since we
3404 * started waiting. For timeouts, we always want to return to userspace,
3405 * regardless of event count.
3406 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003407 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06003408 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
3409}
3410
3411static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
3412 int wake_flags, void *key)
3413{
3414 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
3415 wq);
3416
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003417 /* use noflush == true, as we can't safely rely on locking context */
3418 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06003419 return -1;
3420
3421 return autoremove_wake_function(curr, mode, wake_flags, key);
3422}
3423
Jens Axboe2b188cc2019-01-07 10:46:33 -07003424/*
3425 * Wait until events become available, if we don't already have some. The
3426 * application must reap them itself, as they reside on the shared cq ring.
3427 */
3428static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
3429 const sigset_t __user *sig, size_t sigsz)
3430{
Jens Axboebda52162019-09-24 13:47:15 -06003431 struct io_wait_queue iowq = {
3432 .wq = {
3433 .private = current,
3434 .func = io_wake_function,
3435 .entry = LIST_HEAD_INIT(iowq.wq.entry),
3436 },
3437 .ctx = ctx,
3438 .to_wait = min_events,
3439 };
Hristo Venev75b28af2019-08-26 17:23:46 +00003440 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08003441 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003442
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003443 if (io_cqring_events(ctx, false) >= min_events)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003444 return 0;
3445
3446 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01003447#ifdef CONFIG_COMPAT
3448 if (in_compat_syscall())
3449 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07003450 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01003451 else
3452#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07003453 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01003454
Jens Axboe2b188cc2019-01-07 10:46:33 -07003455 if (ret)
3456 return ret;
3457 }
3458
Jens Axboebda52162019-09-24 13:47:15 -06003459 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02003460 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06003461 do {
3462 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
3463 TASK_INTERRUPTIBLE);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003464 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06003465 break;
3466 schedule();
3467 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08003468 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06003469 break;
3470 }
3471 } while (1);
3472 finish_wait(&ctx->wait, &iowq.wq);
3473
Jackie Liue9ffa5c2019-10-29 11:16:42 +08003474 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003475
Hristo Venev75b28af2019-08-26 17:23:46 +00003476 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003477}
3478
Jens Axboe6b063142019-01-10 22:13:58 -07003479static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
3480{
3481#if defined(CONFIG_UNIX)
3482 if (ctx->ring_sock) {
3483 struct sock *sock = ctx->ring_sock->sk;
3484 struct sk_buff *skb;
3485
3486 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
3487 kfree_skb(skb);
3488 }
3489#else
3490 int i;
3491
Jens Axboe65e19f52019-10-26 07:20:21 -06003492 for (i = 0; i < ctx->nr_user_files; i++) {
3493 struct file *file;
3494
3495 file = io_file_from_index(ctx, i);
3496 if (file)
3497 fput(file);
3498 }
Jens Axboe6b063142019-01-10 22:13:58 -07003499#endif
3500}
3501
3502static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
3503{
Jens Axboe65e19f52019-10-26 07:20:21 -06003504 unsigned nr_tables, i;
3505
3506 if (!ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003507 return -ENXIO;
3508
3509 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06003510 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
3511 for (i = 0; i < nr_tables; i++)
3512 kfree(ctx->file_table[i].files);
3513 kfree(ctx->file_table);
3514 ctx->file_table = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003515 ctx->nr_user_files = 0;
3516 return 0;
3517}
3518
Jens Axboe6c271ce2019-01-10 11:22:30 -07003519static void io_sq_thread_stop(struct io_ring_ctx *ctx)
3520{
3521 if (ctx->sqo_thread) {
Jens Axboe206aefd2019-11-07 18:27:42 -07003522 wait_for_completion(&ctx->completions[1]);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003523 /*
3524 * The park is a bit of a work-around, without it we get
3525 * warning spews on shutdown with SQPOLL set and affinity
3526 * set to a single CPU.
3527 */
Jens Axboe06058632019-04-13 09:26:03 -06003528 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003529 kthread_stop(ctx->sqo_thread);
3530 ctx->sqo_thread = NULL;
3531 }
3532}
3533
Jens Axboe6b063142019-01-10 22:13:58 -07003534static void io_finish_async(struct io_ring_ctx *ctx)
3535{
Jens Axboe6c271ce2019-01-10 11:22:30 -07003536 io_sq_thread_stop(ctx);
3537
Jens Axboe561fb042019-10-24 07:25:42 -06003538 if (ctx->io_wq) {
3539 io_wq_destroy(ctx->io_wq);
3540 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003541 }
3542}
3543
3544#if defined(CONFIG_UNIX)
3545static void io_destruct_skb(struct sk_buff *skb)
3546{
3547 struct io_ring_ctx *ctx = skb->sk->sk_user_data;
3548
Jens Axboe561fb042019-10-24 07:25:42 -06003549 if (ctx->io_wq)
3550 io_wq_flush(ctx->io_wq);
Jens Axboe8a997342019-10-09 14:40:13 -06003551
Jens Axboe6b063142019-01-10 22:13:58 -07003552 unix_destruct_scm(skb);
3553}
3554
3555/*
3556 * Ensure the UNIX gc is aware of our file set, so we are certain that
3557 * the io_uring can be safely unregistered on process exit, even if we have
3558 * loops in the file referencing.
3559 */
3560static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
3561{
3562 struct sock *sk = ctx->ring_sock->sk;
3563 struct scm_fp_list *fpl;
3564 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06003565 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07003566
3567 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
3568 unsigned long inflight = ctx->user->unix_inflight + nr;
3569
3570 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
3571 return -EMFILE;
3572 }
3573
3574 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
3575 if (!fpl)
3576 return -ENOMEM;
3577
3578 skb = alloc_skb(0, GFP_KERNEL);
3579 if (!skb) {
3580 kfree(fpl);
3581 return -ENOMEM;
3582 }
3583
3584 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07003585
Jens Axboe08a45172019-10-03 08:11:03 -06003586 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07003587 fpl->user = get_uid(ctx->user);
3588 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003589 struct file *file = io_file_from_index(ctx, i + offset);
3590
3591 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06003592 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06003593 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06003594 unix_inflight(fpl->user, fpl->fp[nr_files]);
3595 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07003596 }
3597
Jens Axboe08a45172019-10-03 08:11:03 -06003598 if (nr_files) {
3599 fpl->max = SCM_MAX_FD;
3600 fpl->count = nr_files;
3601 UNIXCB(skb).fp = fpl;
3602 skb->destructor = io_destruct_skb;
3603 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
3604 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07003605
Jens Axboe08a45172019-10-03 08:11:03 -06003606 for (i = 0; i < nr_files; i++)
3607 fput(fpl->fp[i]);
3608 } else {
3609 kfree_skb(skb);
3610 kfree(fpl);
3611 }
Jens Axboe6b063142019-01-10 22:13:58 -07003612
3613 return 0;
3614}
3615
3616/*
3617 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
3618 * causes regular reference counting to break down. We rely on the UNIX
3619 * garbage collection to take care of this problem for us.
3620 */
3621static int io_sqe_files_scm(struct io_ring_ctx *ctx)
3622{
3623 unsigned left, total;
3624 int ret = 0;
3625
3626 total = 0;
3627 left = ctx->nr_user_files;
3628 while (left) {
3629 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07003630
3631 ret = __io_sqe_files_scm(ctx, this_files, total);
3632 if (ret)
3633 break;
3634 left -= this_files;
3635 total += this_files;
3636 }
3637
3638 if (!ret)
3639 return 0;
3640
3641 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003642 struct file *file = io_file_from_index(ctx, total);
3643
3644 if (file)
3645 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07003646 total++;
3647 }
3648
3649 return ret;
3650}
3651#else
3652static int io_sqe_files_scm(struct io_ring_ctx *ctx)
3653{
3654 return 0;
3655}
3656#endif
3657
Jens Axboe65e19f52019-10-26 07:20:21 -06003658static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
3659 unsigned nr_files)
3660{
3661 int i;
3662
3663 for (i = 0; i < nr_tables; i++) {
3664 struct fixed_file_table *table = &ctx->file_table[i];
3665 unsigned this_files;
3666
3667 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
3668 table->files = kcalloc(this_files, sizeof(struct file *),
3669 GFP_KERNEL);
3670 if (!table->files)
3671 break;
3672 nr_files -= this_files;
3673 }
3674
3675 if (i == nr_tables)
3676 return 0;
3677
3678 for (i = 0; i < nr_tables; i++) {
3679 struct fixed_file_table *table = &ctx->file_table[i];
3680 kfree(table->files);
3681 }
3682 return 1;
3683}
3684
Jens Axboe6b063142019-01-10 22:13:58 -07003685static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
3686 unsigned nr_args)
3687{
3688 __s32 __user *fds = (__s32 __user *) arg;
Jens Axboe65e19f52019-10-26 07:20:21 -06003689 unsigned nr_tables;
Jens Axboe6b063142019-01-10 22:13:58 -07003690 int fd, ret = 0;
3691 unsigned i;
3692
Jens Axboe65e19f52019-10-26 07:20:21 -06003693 if (ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003694 return -EBUSY;
3695 if (!nr_args)
3696 return -EINVAL;
3697 if (nr_args > IORING_MAX_FIXED_FILES)
3698 return -EMFILE;
3699
Jens Axboe65e19f52019-10-26 07:20:21 -06003700 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
3701 ctx->file_table = kcalloc(nr_tables, sizeof(struct fixed_file_table),
3702 GFP_KERNEL);
3703 if (!ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003704 return -ENOMEM;
3705
Jens Axboe65e19f52019-10-26 07:20:21 -06003706 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
3707 kfree(ctx->file_table);
Jens Axboe46568e92019-11-10 08:40:53 -07003708 ctx->file_table = NULL;
Jens Axboe65e19f52019-10-26 07:20:21 -06003709 return -ENOMEM;
3710 }
3711
Jens Axboe08a45172019-10-03 08:11:03 -06003712 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003713 struct fixed_file_table *table;
3714 unsigned index;
3715
Jens Axboe6b063142019-01-10 22:13:58 -07003716 ret = -EFAULT;
3717 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
3718 break;
Jens Axboe08a45172019-10-03 08:11:03 -06003719 /* allow sparse sets */
3720 if (fd == -1) {
3721 ret = 0;
3722 continue;
3723 }
Jens Axboe6b063142019-01-10 22:13:58 -07003724
Jens Axboe65e19f52019-10-26 07:20:21 -06003725 table = &ctx->file_table[i >> IORING_FILE_TABLE_SHIFT];
3726 index = i & IORING_FILE_TABLE_MASK;
3727 table->files[index] = fget(fd);
Jens Axboe6b063142019-01-10 22:13:58 -07003728
3729 ret = -EBADF;
Jens Axboe65e19f52019-10-26 07:20:21 -06003730 if (!table->files[index])
Jens Axboe6b063142019-01-10 22:13:58 -07003731 break;
3732 /*
3733 * Don't allow io_uring instances to be registered. If UNIX
3734 * isn't enabled, then this causes a reference cycle and this
3735 * instance can never get freed. If UNIX is enabled we'll
3736 * handle it just fine, but there's still no point in allowing
3737 * a ring fd as it doesn't support regular read/write anyway.
3738 */
Jens Axboe65e19f52019-10-26 07:20:21 -06003739 if (table->files[index]->f_op == &io_uring_fops) {
3740 fput(table->files[index]);
Jens Axboe6b063142019-01-10 22:13:58 -07003741 break;
3742 }
Jens Axboe6b063142019-01-10 22:13:58 -07003743 ret = 0;
3744 }
3745
3746 if (ret) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003747 for (i = 0; i < ctx->nr_user_files; i++) {
3748 struct file *file;
Jens Axboe6b063142019-01-10 22:13:58 -07003749
Jens Axboe65e19f52019-10-26 07:20:21 -06003750 file = io_file_from_index(ctx, i);
3751 if (file)
3752 fput(file);
3753 }
3754 for (i = 0; i < nr_tables; i++)
3755 kfree(ctx->file_table[i].files);
3756
3757 kfree(ctx->file_table);
3758 ctx->file_table = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003759 ctx->nr_user_files = 0;
3760 return ret;
3761 }
3762
3763 ret = io_sqe_files_scm(ctx);
3764 if (ret)
3765 io_sqe_files_unregister(ctx);
3766
3767 return ret;
3768}
3769
Jens Axboec3a31e62019-10-03 13:59:56 -06003770static void io_sqe_file_unregister(struct io_ring_ctx *ctx, int index)
3771{
3772#if defined(CONFIG_UNIX)
Jens Axboe65e19f52019-10-26 07:20:21 -06003773 struct file *file = io_file_from_index(ctx, index);
Jens Axboec3a31e62019-10-03 13:59:56 -06003774 struct sock *sock = ctx->ring_sock->sk;
3775 struct sk_buff_head list, *head = &sock->sk_receive_queue;
3776 struct sk_buff *skb;
3777 int i;
3778
3779 __skb_queue_head_init(&list);
3780
3781 /*
3782 * Find the skb that holds this file in its SCM_RIGHTS. When found,
3783 * remove this entry and rearrange the file array.
3784 */
3785 skb = skb_dequeue(head);
3786 while (skb) {
3787 struct scm_fp_list *fp;
3788
3789 fp = UNIXCB(skb).fp;
3790 for (i = 0; i < fp->count; i++) {
3791 int left;
3792
3793 if (fp->fp[i] != file)
3794 continue;
3795
3796 unix_notinflight(fp->user, fp->fp[i]);
3797 left = fp->count - 1 - i;
3798 if (left) {
3799 memmove(&fp->fp[i], &fp->fp[i + 1],
3800 left * sizeof(struct file *));
3801 }
3802 fp->count--;
3803 if (!fp->count) {
3804 kfree_skb(skb);
3805 skb = NULL;
3806 } else {
3807 __skb_queue_tail(&list, skb);
3808 }
3809 fput(file);
3810 file = NULL;
3811 break;
3812 }
3813
3814 if (!file)
3815 break;
3816
3817 __skb_queue_tail(&list, skb);
3818
3819 skb = skb_dequeue(head);
3820 }
3821
3822 if (skb_peek(&list)) {
3823 spin_lock_irq(&head->lock);
3824 while ((skb = __skb_dequeue(&list)) != NULL)
3825 __skb_queue_tail(head, skb);
3826 spin_unlock_irq(&head->lock);
3827 }
3828#else
Jens Axboe65e19f52019-10-26 07:20:21 -06003829 fput(io_file_from_index(ctx, index));
Jens Axboec3a31e62019-10-03 13:59:56 -06003830#endif
3831}
3832
3833static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
3834 int index)
3835{
3836#if defined(CONFIG_UNIX)
3837 struct sock *sock = ctx->ring_sock->sk;
3838 struct sk_buff_head *head = &sock->sk_receive_queue;
3839 struct sk_buff *skb;
3840
3841 /*
3842 * See if we can merge this file into an existing skb SCM_RIGHTS
3843 * file set. If there's no room, fall back to allocating a new skb
3844 * and filling it in.
3845 */
3846 spin_lock_irq(&head->lock);
3847 skb = skb_peek(head);
3848 if (skb) {
3849 struct scm_fp_list *fpl = UNIXCB(skb).fp;
3850
3851 if (fpl->count < SCM_MAX_FD) {
3852 __skb_unlink(skb, head);
3853 spin_unlock_irq(&head->lock);
3854 fpl->fp[fpl->count] = get_file(file);
3855 unix_inflight(fpl->user, fpl->fp[fpl->count]);
3856 fpl->count++;
3857 spin_lock_irq(&head->lock);
3858 __skb_queue_head(head, skb);
3859 } else {
3860 skb = NULL;
3861 }
3862 }
3863 spin_unlock_irq(&head->lock);
3864
3865 if (skb) {
3866 fput(file);
3867 return 0;
3868 }
3869
3870 return __io_sqe_files_scm(ctx, 1, index);
3871#else
3872 return 0;
3873#endif
3874}
3875
3876static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
3877 unsigned nr_args)
3878{
3879 struct io_uring_files_update up;
3880 __s32 __user *fds;
3881 int fd, i, err;
3882 __u32 done;
3883
Jens Axboe65e19f52019-10-26 07:20:21 -06003884 if (!ctx->file_table)
Jens Axboec3a31e62019-10-03 13:59:56 -06003885 return -ENXIO;
3886 if (!nr_args)
3887 return -EINVAL;
3888 if (copy_from_user(&up, arg, sizeof(up)))
3889 return -EFAULT;
3890 if (check_add_overflow(up.offset, nr_args, &done))
3891 return -EOVERFLOW;
3892 if (done > ctx->nr_user_files)
3893 return -EINVAL;
3894
3895 done = 0;
3896 fds = (__s32 __user *) up.fds;
3897 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003898 struct fixed_file_table *table;
3899 unsigned index;
3900
Jens Axboec3a31e62019-10-03 13:59:56 -06003901 err = 0;
3902 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
3903 err = -EFAULT;
3904 break;
3905 }
3906 i = array_index_nospec(up.offset, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06003907 table = &ctx->file_table[i >> IORING_FILE_TABLE_SHIFT];
3908 index = i & IORING_FILE_TABLE_MASK;
3909 if (table->files[index]) {
Jens Axboec3a31e62019-10-03 13:59:56 -06003910 io_sqe_file_unregister(ctx, i);
Jens Axboe65e19f52019-10-26 07:20:21 -06003911 table->files[index] = NULL;
Jens Axboec3a31e62019-10-03 13:59:56 -06003912 }
3913 if (fd != -1) {
3914 struct file *file;
3915
3916 file = fget(fd);
3917 if (!file) {
3918 err = -EBADF;
3919 break;
3920 }
3921 /*
3922 * Don't allow io_uring instances to be registered. If
3923 * UNIX isn't enabled, then this causes a reference
3924 * cycle and this instance can never get freed. If UNIX
3925 * is enabled we'll handle it just fine, but there's
3926 * still no point in allowing a ring fd as it doesn't
3927 * support regular read/write anyway.
3928 */
3929 if (file->f_op == &io_uring_fops) {
3930 fput(file);
3931 err = -EBADF;
3932 break;
3933 }
Jens Axboe65e19f52019-10-26 07:20:21 -06003934 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06003935 err = io_sqe_file_register(ctx, file, i);
3936 if (err)
3937 break;
3938 }
3939 nr_args--;
3940 done++;
3941 up.offset++;
3942 }
3943
3944 return done ? done : err;
3945}
3946
Jens Axboe7d723062019-11-12 22:31:31 -07003947static void io_put_work(struct io_wq_work *work)
3948{
3949 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
3950
3951 io_put_req(req);
3952}
3953
3954static void io_get_work(struct io_wq_work *work)
3955{
3956 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
3957
3958 refcount_inc(&req->refs);
3959}
3960
Jens Axboe6c271ce2019-01-10 11:22:30 -07003961static int io_sq_offload_start(struct io_ring_ctx *ctx,
3962 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003963{
Jens Axboe576a3472019-11-25 08:49:20 -07003964 struct io_wq_data data;
Jens Axboe561fb042019-10-24 07:25:42 -06003965 unsigned concurrency;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003966 int ret;
3967
Jens Axboe6c271ce2019-01-10 11:22:30 -07003968 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003969 mmgrab(current->mm);
3970 ctx->sqo_mm = current->mm;
3971
Jens Axboe6c271ce2019-01-10 11:22:30 -07003972 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06003973 ret = -EPERM;
3974 if (!capable(CAP_SYS_ADMIN))
3975 goto err;
3976
Jens Axboe917257d2019-04-13 09:28:55 -06003977 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
3978 if (!ctx->sq_thread_idle)
3979 ctx->sq_thread_idle = HZ;
3980
Jens Axboe6c271ce2019-01-10 11:22:30 -07003981 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06003982 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003983
Jens Axboe917257d2019-04-13 09:28:55 -06003984 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06003985 if (cpu >= nr_cpu_ids)
3986 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08003987 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06003988 goto err;
3989
Jens Axboe6c271ce2019-01-10 11:22:30 -07003990 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
3991 ctx, cpu,
3992 "io_uring-sq");
3993 } else {
3994 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
3995 "io_uring-sq");
3996 }
3997 if (IS_ERR(ctx->sqo_thread)) {
3998 ret = PTR_ERR(ctx->sqo_thread);
3999 ctx->sqo_thread = NULL;
4000 goto err;
4001 }
4002 wake_up_process(ctx->sqo_thread);
4003 } else if (p->flags & IORING_SETUP_SQ_AFF) {
4004 /* Can't have SQ_AFF without SQPOLL */
4005 ret = -EINVAL;
4006 goto err;
4007 }
4008
Jens Axboe576a3472019-11-25 08:49:20 -07004009 data.mm = ctx->sqo_mm;
4010 data.user = ctx->user;
Jens Axboe181e4482019-11-25 08:52:30 -07004011 data.creds = ctx->creds;
Jens Axboe576a3472019-11-25 08:49:20 -07004012 data.get_work = io_get_work;
4013 data.put_work = io_put_work;
4014
Jens Axboe561fb042019-10-24 07:25:42 -06004015 /* Do QD, or 4 * CPUS, whatever is smallest */
4016 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
Jens Axboe576a3472019-11-25 08:49:20 -07004017 ctx->io_wq = io_wq_create(concurrency, &data);
Jens Axboe975c99a52019-10-30 08:42:56 -06004018 if (IS_ERR(ctx->io_wq)) {
4019 ret = PTR_ERR(ctx->io_wq);
4020 ctx->io_wq = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004021 goto err;
4022 }
4023
4024 return 0;
4025err:
Jens Axboe54a91f32019-09-10 09:15:04 -06004026 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004027 mmdrop(ctx->sqo_mm);
4028 ctx->sqo_mm = NULL;
4029 return ret;
4030}
4031
4032static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
4033{
4034 atomic_long_sub(nr_pages, &user->locked_vm);
4035}
4036
4037static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
4038{
4039 unsigned long page_limit, cur_pages, new_pages;
4040
4041 /* Don't allow more pages than we can safely lock */
4042 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
4043
4044 do {
4045 cur_pages = atomic_long_read(&user->locked_vm);
4046 new_pages = cur_pages + nr_pages;
4047 if (new_pages > page_limit)
4048 return -ENOMEM;
4049 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
4050 new_pages) != cur_pages);
4051
4052 return 0;
4053}
4054
4055static void io_mem_free(void *ptr)
4056{
Mark Rutland52e04ef2019-04-30 17:30:21 +01004057 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004058
Mark Rutland52e04ef2019-04-30 17:30:21 +01004059 if (!ptr)
4060 return;
4061
4062 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004063 if (put_page_testzero(page))
4064 free_compound_page(page);
4065}
4066
4067static void *io_mem_alloc(size_t size)
4068{
4069 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
4070 __GFP_NORETRY;
4071
4072 return (void *) __get_free_pages(gfp_flags, get_order(size));
4073}
4074
Hristo Venev75b28af2019-08-26 17:23:46 +00004075static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
4076 size_t *sq_offset)
4077{
4078 struct io_rings *rings;
4079 size_t off, sq_array_size;
4080
4081 off = struct_size(rings, cqes, cq_entries);
4082 if (off == SIZE_MAX)
4083 return SIZE_MAX;
4084
4085#ifdef CONFIG_SMP
4086 off = ALIGN(off, SMP_CACHE_BYTES);
4087 if (off == 0)
4088 return SIZE_MAX;
4089#endif
4090
4091 sq_array_size = array_size(sizeof(u32), sq_entries);
4092 if (sq_array_size == SIZE_MAX)
4093 return SIZE_MAX;
4094
4095 if (check_add_overflow(off, sq_array_size, &off))
4096 return SIZE_MAX;
4097
4098 if (sq_offset)
4099 *sq_offset = off;
4100
4101 return off;
4102}
4103
Jens Axboe2b188cc2019-01-07 10:46:33 -07004104static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
4105{
Hristo Venev75b28af2019-08-26 17:23:46 +00004106 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004107
Hristo Venev75b28af2019-08-26 17:23:46 +00004108 pages = (size_t)1 << get_order(
4109 rings_size(sq_entries, cq_entries, NULL));
4110 pages += (size_t)1 << get_order(
4111 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07004112
Hristo Venev75b28af2019-08-26 17:23:46 +00004113 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004114}
4115
Jens Axboeedafcce2019-01-09 09:16:05 -07004116static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
4117{
4118 int i, j;
4119
4120 if (!ctx->user_bufs)
4121 return -ENXIO;
4122
4123 for (i = 0; i < ctx->nr_user_bufs; i++) {
4124 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
4125
4126 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbard27c4d3a2019-08-04 19:32:06 -07004127 put_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07004128
4129 if (ctx->account_mem)
4130 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004131 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07004132 imu->nr_bvecs = 0;
4133 }
4134
4135 kfree(ctx->user_bufs);
4136 ctx->user_bufs = NULL;
4137 ctx->nr_user_bufs = 0;
4138 return 0;
4139}
4140
4141static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
4142 void __user *arg, unsigned index)
4143{
4144 struct iovec __user *src;
4145
4146#ifdef CONFIG_COMPAT
4147 if (ctx->compat) {
4148 struct compat_iovec __user *ciovs;
4149 struct compat_iovec ciov;
4150
4151 ciovs = (struct compat_iovec __user *) arg;
4152 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
4153 return -EFAULT;
4154
4155 dst->iov_base = (void __user *) (unsigned long) ciov.iov_base;
4156 dst->iov_len = ciov.iov_len;
4157 return 0;
4158 }
4159#endif
4160 src = (struct iovec __user *) arg;
4161 if (copy_from_user(dst, &src[index], sizeof(*dst)))
4162 return -EFAULT;
4163 return 0;
4164}
4165
4166static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
4167 unsigned nr_args)
4168{
4169 struct vm_area_struct **vmas = NULL;
4170 struct page **pages = NULL;
4171 int i, j, got_pages = 0;
4172 int ret = -EINVAL;
4173
4174 if (ctx->user_bufs)
4175 return -EBUSY;
4176 if (!nr_args || nr_args > UIO_MAXIOV)
4177 return -EINVAL;
4178
4179 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
4180 GFP_KERNEL);
4181 if (!ctx->user_bufs)
4182 return -ENOMEM;
4183
4184 for (i = 0; i < nr_args; i++) {
4185 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
4186 unsigned long off, start, end, ubuf;
4187 int pret, nr_pages;
4188 struct iovec iov;
4189 size_t size;
4190
4191 ret = io_copy_iov(ctx, &iov, arg, i);
4192 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03004193 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07004194
4195 /*
4196 * Don't impose further limits on the size and buffer
4197 * constraints here, we'll -EINVAL later when IO is
4198 * submitted if they are wrong.
4199 */
4200 ret = -EFAULT;
4201 if (!iov.iov_base || !iov.iov_len)
4202 goto err;
4203
4204 /* arbitrary limit, but we need something */
4205 if (iov.iov_len > SZ_1G)
4206 goto err;
4207
4208 ubuf = (unsigned long) iov.iov_base;
4209 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
4210 start = ubuf >> PAGE_SHIFT;
4211 nr_pages = end - start;
4212
4213 if (ctx->account_mem) {
4214 ret = io_account_mem(ctx->user, nr_pages);
4215 if (ret)
4216 goto err;
4217 }
4218
4219 ret = 0;
4220 if (!pages || nr_pages > got_pages) {
4221 kfree(vmas);
4222 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004223 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07004224 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004225 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07004226 sizeof(struct vm_area_struct *),
4227 GFP_KERNEL);
4228 if (!pages || !vmas) {
4229 ret = -ENOMEM;
4230 if (ctx->account_mem)
4231 io_unaccount_mem(ctx->user, nr_pages);
4232 goto err;
4233 }
4234 got_pages = nr_pages;
4235 }
4236
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004237 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07004238 GFP_KERNEL);
4239 ret = -ENOMEM;
4240 if (!imu->bvec) {
4241 if (ctx->account_mem)
4242 io_unaccount_mem(ctx->user, nr_pages);
4243 goto err;
4244 }
4245
4246 ret = 0;
4247 down_read(&current->mm->mmap_sem);
Ira Weiny932f4a62019-05-13 17:17:03 -07004248 pret = get_user_pages(ubuf, nr_pages,
4249 FOLL_WRITE | FOLL_LONGTERM,
4250 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07004251 if (pret == nr_pages) {
4252 /* don't support file backed memory */
4253 for (j = 0; j < nr_pages; j++) {
4254 struct vm_area_struct *vma = vmas[j];
4255
4256 if (vma->vm_file &&
4257 !is_file_hugepages(vma->vm_file)) {
4258 ret = -EOPNOTSUPP;
4259 break;
4260 }
4261 }
4262 } else {
4263 ret = pret < 0 ? pret : -EFAULT;
4264 }
4265 up_read(&current->mm->mmap_sem);
4266 if (ret) {
4267 /*
4268 * if we did partial map, or found file backed vmas,
4269 * release any pages we did get
4270 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07004271 if (pret > 0)
4272 put_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07004273 if (ctx->account_mem)
4274 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004275 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07004276 goto err;
4277 }
4278
4279 off = ubuf & ~PAGE_MASK;
4280 size = iov.iov_len;
4281 for (j = 0; j < nr_pages; j++) {
4282 size_t vec_len;
4283
4284 vec_len = min_t(size_t, size, PAGE_SIZE - off);
4285 imu->bvec[j].bv_page = pages[j];
4286 imu->bvec[j].bv_len = vec_len;
4287 imu->bvec[j].bv_offset = off;
4288 off = 0;
4289 size -= vec_len;
4290 }
4291 /* store original address for later verification */
4292 imu->ubuf = ubuf;
4293 imu->len = iov.iov_len;
4294 imu->nr_bvecs = nr_pages;
4295
4296 ctx->nr_user_bufs++;
4297 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004298 kvfree(pages);
4299 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07004300 return 0;
4301err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004302 kvfree(pages);
4303 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07004304 io_sqe_buffer_unregister(ctx);
4305 return ret;
4306}
4307
Jens Axboe9b402842019-04-11 11:45:41 -06004308static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
4309{
4310 __s32 __user *fds = arg;
4311 int fd;
4312
4313 if (ctx->cq_ev_fd)
4314 return -EBUSY;
4315
4316 if (copy_from_user(&fd, fds, sizeof(*fds)))
4317 return -EFAULT;
4318
4319 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
4320 if (IS_ERR(ctx->cq_ev_fd)) {
4321 int ret = PTR_ERR(ctx->cq_ev_fd);
4322 ctx->cq_ev_fd = NULL;
4323 return ret;
4324 }
4325
4326 return 0;
4327}
4328
4329static int io_eventfd_unregister(struct io_ring_ctx *ctx)
4330{
4331 if (ctx->cq_ev_fd) {
4332 eventfd_ctx_put(ctx->cq_ev_fd);
4333 ctx->cq_ev_fd = NULL;
4334 return 0;
4335 }
4336
4337 return -ENXIO;
4338}
4339
Jens Axboe2b188cc2019-01-07 10:46:33 -07004340static void io_ring_ctx_free(struct io_ring_ctx *ctx)
4341{
Jens Axboe6b063142019-01-10 22:13:58 -07004342 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004343 if (ctx->sqo_mm)
4344 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07004345
4346 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07004347 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07004348 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06004349 io_eventfd_unregister(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07004350
Jens Axboe2b188cc2019-01-07 10:46:33 -07004351#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07004352 if (ctx->ring_sock) {
4353 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004354 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07004355 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004356#endif
4357
Hristo Venev75b28af2019-08-26 17:23:46 +00004358 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004359 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004360
4361 percpu_ref_exit(&ctx->refs);
4362 if (ctx->account_mem)
4363 io_unaccount_mem(ctx->user,
4364 ring_pages(ctx->sq_entries, ctx->cq_entries));
4365 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07004366 put_cred(ctx->creds);
Jens Axboe206aefd2019-11-07 18:27:42 -07004367 kfree(ctx->completions);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07004368 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004369 kfree(ctx);
4370}
4371
4372static __poll_t io_uring_poll(struct file *file, poll_table *wait)
4373{
4374 struct io_ring_ctx *ctx = file->private_data;
4375 __poll_t mask = 0;
4376
4377 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02004378 /*
4379 * synchronizes with barrier from wq_has_sleeper call in
4380 * io_commit_cqring
4381 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004382 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00004383 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
4384 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004385 mask |= EPOLLOUT | EPOLLWRNORM;
yangerkundaa5de52019-09-24 20:53:34 +08004386 if (READ_ONCE(ctx->rings->cq.head) != ctx->cached_cq_tail)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004387 mask |= EPOLLIN | EPOLLRDNORM;
4388
4389 return mask;
4390}
4391
4392static int io_uring_fasync(int fd, struct file *file, int on)
4393{
4394 struct io_ring_ctx *ctx = file->private_data;
4395
4396 return fasync_helper(fd, file, on, &ctx->cq_fasync);
4397}
4398
4399static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
4400{
4401 mutex_lock(&ctx->uring_lock);
4402 percpu_ref_kill(&ctx->refs);
4403 mutex_unlock(&ctx->uring_lock);
4404
Jens Axboe5262f562019-09-17 12:26:57 -06004405 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004406 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06004407
4408 if (ctx->io_wq)
4409 io_wq_cancel_all(ctx->io_wq);
4410
Jens Axboedef596e2019-01-09 08:59:42 -07004411 io_iopoll_reap_events(ctx);
Jens Axboe15dff282019-11-13 09:09:23 -07004412 /* if we failed setting up the ctx, we might not have any rings */
4413 if (ctx->rings)
4414 io_cqring_overflow_flush(ctx, true);
Jens Axboe206aefd2019-11-07 18:27:42 -07004415 wait_for_completion(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004416 io_ring_ctx_free(ctx);
4417}
4418
4419static int io_uring_release(struct inode *inode, struct file *file)
4420{
4421 struct io_ring_ctx *ctx = file->private_data;
4422
4423 file->private_data = NULL;
4424 io_ring_ctx_wait_and_kill(ctx);
4425 return 0;
4426}
4427
Jens Axboefcb323c2019-10-24 12:39:47 -06004428static void io_uring_cancel_files(struct io_ring_ctx *ctx,
4429 struct files_struct *files)
4430{
4431 struct io_kiocb *req;
4432 DEFINE_WAIT(wait);
4433
4434 while (!list_empty_careful(&ctx->inflight_list)) {
Jens Axboe768134d2019-11-10 20:30:53 -07004435 struct io_kiocb *cancel_req = NULL;
Jens Axboefcb323c2019-10-24 12:39:47 -06004436
4437 spin_lock_irq(&ctx->inflight_lock);
4438 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07004439 if (req->work.files != files)
4440 continue;
4441 /* req is being completed, ignore */
4442 if (!refcount_inc_not_zero(&req->refs))
4443 continue;
4444 cancel_req = req;
4445 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06004446 }
Jens Axboe768134d2019-11-10 20:30:53 -07004447 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06004448 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07004449 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06004450 spin_unlock_irq(&ctx->inflight_lock);
4451
Jens Axboe768134d2019-11-10 20:30:53 -07004452 /* We need to keep going until we don't find a matching req */
4453 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06004454 break;
Bob Liu2f6d9b92019-11-13 18:06:24 +08004455
4456 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
4457 io_put_req(cancel_req);
Jens Axboefcb323c2019-10-24 12:39:47 -06004458 schedule();
4459 }
Jens Axboe768134d2019-11-10 20:30:53 -07004460 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06004461}
4462
4463static int io_uring_flush(struct file *file, void *data)
4464{
4465 struct io_ring_ctx *ctx = file->private_data;
4466
4467 io_uring_cancel_files(ctx, data);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004468 if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) {
4469 io_cqring_overflow_flush(ctx, true);
Jens Axboefcb323c2019-10-24 12:39:47 -06004470 io_wq_cancel_all(ctx->io_wq);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004471 }
Jens Axboefcb323c2019-10-24 12:39:47 -06004472 return 0;
4473}
4474
Roman Penyaev6c5c2402019-11-28 12:53:22 +01004475static void *io_uring_validate_mmap_request(struct file *file,
4476 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004477{
Jens Axboe2b188cc2019-01-07 10:46:33 -07004478 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01004479 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004480 struct page *page;
4481 void *ptr;
4482
4483 switch (offset) {
4484 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00004485 case IORING_OFF_CQ_RING:
4486 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004487 break;
4488 case IORING_OFF_SQES:
4489 ptr = ctx->sq_sqes;
4490 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004491 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01004492 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004493 }
4494
4495 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07004496 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01004497 return ERR_PTR(-EINVAL);
4498
4499 return ptr;
4500}
4501
4502#ifdef CONFIG_MMU
4503
4504static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
4505{
4506 size_t sz = vma->vm_end - vma->vm_start;
4507 unsigned long pfn;
4508 void *ptr;
4509
4510 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
4511 if (IS_ERR(ptr))
4512 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004513
4514 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
4515 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
4516}
4517
Roman Penyaev6c5c2402019-11-28 12:53:22 +01004518#else /* !CONFIG_MMU */
4519
4520static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
4521{
4522 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
4523}
4524
4525static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
4526{
4527 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
4528}
4529
4530static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
4531 unsigned long addr, unsigned long len,
4532 unsigned long pgoff, unsigned long flags)
4533{
4534 void *ptr;
4535
4536 ptr = io_uring_validate_mmap_request(file, pgoff, len);
4537 if (IS_ERR(ptr))
4538 return PTR_ERR(ptr);
4539
4540 return (unsigned long) ptr;
4541}
4542
4543#endif /* !CONFIG_MMU */
4544
Jens Axboe2b188cc2019-01-07 10:46:33 -07004545SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
4546 u32, min_complete, u32, flags, const sigset_t __user *, sig,
4547 size_t, sigsz)
4548{
4549 struct io_ring_ctx *ctx;
4550 long ret = -EBADF;
4551 int submitted = 0;
4552 struct fd f;
4553
Jens Axboe6c271ce2019-01-10 11:22:30 -07004554 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07004555 return -EINVAL;
4556
4557 f = fdget(fd);
4558 if (!f.file)
4559 return -EBADF;
4560
4561 ret = -EOPNOTSUPP;
4562 if (f.file->f_op != &io_uring_fops)
4563 goto out_fput;
4564
4565 ret = -ENXIO;
4566 ctx = f.file->private_data;
4567 if (!percpu_ref_tryget(&ctx->refs))
4568 goto out_fput;
4569
Jens Axboe6c271ce2019-01-10 11:22:30 -07004570 /*
4571 * For SQ polling, the thread will do all submissions and completions.
4572 * Just return the requested submit count, and wake the thread if
4573 * we were asked to.
4574 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06004575 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004576 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07004577 if (!list_empty_careful(&ctx->cq_overflow_list))
4578 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004579 if (flags & IORING_ENTER_SQ_WAKEUP)
4580 wake_up(&ctx->sqo_wait);
4581 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06004582 } else if (to_submit) {
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004583 struct mm_struct *cur_mm;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004584
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004585 to_submit = min(to_submit, ctx->sq_entries);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004586 mutex_lock(&ctx->uring_lock);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004587 /* already have mm, so io_submit_sqes() won't try to grab it */
4588 cur_mm = ctx->sqo_mm;
4589 submitted = io_submit_sqes(ctx, to_submit, f.file, fd,
4590 &cur_mm, false);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004591 mutex_unlock(&ctx->uring_lock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004592 }
4593 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07004594 unsigned nr_events = 0;
4595
Jens Axboe2b188cc2019-01-07 10:46:33 -07004596 min_complete = min(min_complete, ctx->cq_entries);
4597
Jens Axboedef596e2019-01-09 08:59:42 -07004598 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07004599 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07004600 } else {
4601 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
4602 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004603 }
4604
Pavel Begunkov6805b322019-10-08 02:18:42 +03004605 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004606out_fput:
4607 fdput(f);
4608 return submitted ? submitted : ret;
4609}
4610
4611static const struct file_operations io_uring_fops = {
4612 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06004613 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07004614 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01004615#ifndef CONFIG_MMU
4616 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
4617 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
4618#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07004619 .poll = io_uring_poll,
4620 .fasync = io_uring_fasync,
4621};
4622
4623static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
4624 struct io_uring_params *p)
4625{
Hristo Venev75b28af2019-08-26 17:23:46 +00004626 struct io_rings *rings;
4627 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004628
Hristo Venev75b28af2019-08-26 17:23:46 +00004629 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
4630 if (size == SIZE_MAX)
4631 return -EOVERFLOW;
4632
4633 rings = io_mem_alloc(size);
4634 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004635 return -ENOMEM;
4636
Hristo Venev75b28af2019-08-26 17:23:46 +00004637 ctx->rings = rings;
4638 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
4639 rings->sq_ring_mask = p->sq_entries - 1;
4640 rings->cq_ring_mask = p->cq_entries - 1;
4641 rings->sq_ring_entries = p->sq_entries;
4642 rings->cq_ring_entries = p->cq_entries;
4643 ctx->sq_mask = rings->sq_ring_mask;
4644 ctx->cq_mask = rings->cq_ring_mask;
4645 ctx->sq_entries = rings->sq_ring_entries;
4646 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004647
4648 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07004649 if (size == SIZE_MAX) {
4650 io_mem_free(ctx->rings);
4651 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004652 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07004653 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004654
4655 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07004656 if (!ctx->sq_sqes) {
4657 io_mem_free(ctx->rings);
4658 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004659 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07004660 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004661
Jens Axboe2b188cc2019-01-07 10:46:33 -07004662 return 0;
4663}
4664
4665/*
4666 * Allocate an anonymous fd, this is what constitutes the application
4667 * visible backing of an io_uring instance. The application mmaps this
4668 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
4669 * we have to tie this fd to a socket for file garbage collection purposes.
4670 */
4671static int io_uring_get_fd(struct io_ring_ctx *ctx)
4672{
4673 struct file *file;
4674 int ret;
4675
4676#if defined(CONFIG_UNIX)
4677 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
4678 &ctx->ring_sock);
4679 if (ret)
4680 return ret;
4681#endif
4682
4683 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
4684 if (ret < 0)
4685 goto err;
4686
4687 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
4688 O_RDWR | O_CLOEXEC);
4689 if (IS_ERR(file)) {
4690 put_unused_fd(ret);
4691 ret = PTR_ERR(file);
4692 goto err;
4693 }
4694
4695#if defined(CONFIG_UNIX)
4696 ctx->ring_sock->file = file;
Jens Axboe6b063142019-01-10 22:13:58 -07004697 ctx->ring_sock->sk->sk_user_data = ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004698#endif
4699 fd_install(ret, file);
4700 return ret;
4701err:
4702#if defined(CONFIG_UNIX)
4703 sock_release(ctx->ring_sock);
4704 ctx->ring_sock = NULL;
4705#endif
4706 return ret;
4707}
4708
4709static int io_uring_create(unsigned entries, struct io_uring_params *p)
4710{
4711 struct user_struct *user = NULL;
4712 struct io_ring_ctx *ctx;
4713 bool account_mem;
4714 int ret;
4715
4716 if (!entries || entries > IORING_MAX_ENTRIES)
4717 return -EINVAL;
4718
4719 /*
4720 * Use twice as many entries for the CQ ring. It's possible for the
4721 * application to drive a higher depth than the size of the SQ ring,
4722 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06004723 * some flexibility in overcommitting a bit. If the application has
4724 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
4725 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07004726 */
4727 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06004728 if (p->flags & IORING_SETUP_CQSIZE) {
4729 /*
4730 * If IORING_SETUP_CQSIZE is set, we do the same roundup
4731 * to a power-of-two, if it isn't already. We do NOT impose
4732 * any cq vs sq ring sizing.
4733 */
4734 if (p->cq_entries < p->sq_entries || p->cq_entries > IORING_MAX_CQ_ENTRIES)
4735 return -EINVAL;
4736 p->cq_entries = roundup_pow_of_two(p->cq_entries);
4737 } else {
4738 p->cq_entries = 2 * p->sq_entries;
4739 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004740
4741 user = get_uid(current_user());
4742 account_mem = !capable(CAP_IPC_LOCK);
4743
4744 if (account_mem) {
4745 ret = io_account_mem(user,
4746 ring_pages(p->sq_entries, p->cq_entries));
4747 if (ret) {
4748 free_uid(user);
4749 return ret;
4750 }
4751 }
4752
4753 ctx = io_ring_ctx_alloc(p);
4754 if (!ctx) {
4755 if (account_mem)
4756 io_unaccount_mem(user, ring_pages(p->sq_entries,
4757 p->cq_entries));
4758 free_uid(user);
4759 return -ENOMEM;
4760 }
4761 ctx->compat = in_compat_syscall();
4762 ctx->account_mem = account_mem;
4763 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07004764 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07004765
4766 ret = io_allocate_scq_urings(ctx, p);
4767 if (ret)
4768 goto err;
4769
Jens Axboe6c271ce2019-01-10 11:22:30 -07004770 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004771 if (ret)
4772 goto err;
4773
Jens Axboe2b188cc2019-01-07 10:46:33 -07004774 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00004775 p->sq_off.head = offsetof(struct io_rings, sq.head);
4776 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
4777 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
4778 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
4779 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
4780 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
4781 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004782
4783 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00004784 p->cq_off.head = offsetof(struct io_rings, cq.head);
4785 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
4786 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
4787 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
4788 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
4789 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06004790
Jens Axboe044c1ab2019-10-28 09:15:33 -06004791 /*
4792 * Install ring fd as the very last thing, so we don't risk someone
4793 * having closed it before we finish setup
4794 */
4795 ret = io_uring_get_fd(ctx);
4796 if (ret < 0)
4797 goto err;
4798
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004799 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004800 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004801 return ret;
4802err:
4803 io_ring_ctx_wait_and_kill(ctx);
4804 return ret;
4805}
4806
4807/*
4808 * Sets up an aio uring context, and returns the fd. Applications asks for a
4809 * ring size, we return the actual sq/cq ring sizes (among other things) in the
4810 * params structure passed in.
4811 */
4812static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
4813{
4814 struct io_uring_params p;
4815 long ret;
4816 int i;
4817
4818 if (copy_from_user(&p, params, sizeof(p)))
4819 return -EFAULT;
4820 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
4821 if (p.resv[i])
4822 return -EINVAL;
4823 }
4824
Jens Axboe6c271ce2019-01-10 11:22:30 -07004825 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe33a107f2019-10-04 12:10:03 -06004826 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE))
Jens Axboe2b188cc2019-01-07 10:46:33 -07004827 return -EINVAL;
4828
4829 ret = io_uring_create(entries, &p);
4830 if (ret < 0)
4831 return ret;
4832
4833 if (copy_to_user(params, &p, sizeof(p)))
4834 return -EFAULT;
4835
4836 return ret;
4837}
4838
4839SYSCALL_DEFINE2(io_uring_setup, u32, entries,
4840 struct io_uring_params __user *, params)
4841{
4842 return io_uring_setup(entries, params);
4843}
4844
Jens Axboeedafcce2019-01-09 09:16:05 -07004845static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
4846 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06004847 __releases(ctx->uring_lock)
4848 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07004849{
4850 int ret;
4851
Jens Axboe35fa71a2019-04-22 10:23:23 -06004852 /*
4853 * We're inside the ring mutex, if the ref is already dying, then
4854 * someone else killed the ctx or is already going through
4855 * io_uring_register().
4856 */
4857 if (percpu_ref_is_dying(&ctx->refs))
4858 return -ENXIO;
4859
Jens Axboeedafcce2019-01-09 09:16:05 -07004860 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06004861
4862 /*
4863 * Drop uring mutex before waiting for references to exit. If another
4864 * thread is currently inside io_uring_enter() it might need to grab
4865 * the uring_lock to make progress. If we hold it here across the drain
4866 * wait, then we can deadlock. It's safe to drop the mutex here, since
4867 * no new references will come in after we've killed the percpu ref.
4868 */
4869 mutex_unlock(&ctx->uring_lock);
Jens Axboe206aefd2019-11-07 18:27:42 -07004870 wait_for_completion(&ctx->completions[0]);
Jens Axboeb19062a2019-04-15 10:49:38 -06004871 mutex_lock(&ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07004872
4873 switch (opcode) {
4874 case IORING_REGISTER_BUFFERS:
4875 ret = io_sqe_buffer_register(ctx, arg, nr_args);
4876 break;
4877 case IORING_UNREGISTER_BUFFERS:
4878 ret = -EINVAL;
4879 if (arg || nr_args)
4880 break;
4881 ret = io_sqe_buffer_unregister(ctx);
4882 break;
Jens Axboe6b063142019-01-10 22:13:58 -07004883 case IORING_REGISTER_FILES:
4884 ret = io_sqe_files_register(ctx, arg, nr_args);
4885 break;
4886 case IORING_UNREGISTER_FILES:
4887 ret = -EINVAL;
4888 if (arg || nr_args)
4889 break;
4890 ret = io_sqe_files_unregister(ctx);
4891 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06004892 case IORING_REGISTER_FILES_UPDATE:
4893 ret = io_sqe_files_update(ctx, arg, nr_args);
4894 break;
Jens Axboe9b402842019-04-11 11:45:41 -06004895 case IORING_REGISTER_EVENTFD:
4896 ret = -EINVAL;
4897 if (nr_args != 1)
4898 break;
4899 ret = io_eventfd_register(ctx, arg);
4900 break;
4901 case IORING_UNREGISTER_EVENTFD:
4902 ret = -EINVAL;
4903 if (arg || nr_args)
4904 break;
4905 ret = io_eventfd_unregister(ctx);
4906 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07004907 default:
4908 ret = -EINVAL;
4909 break;
4910 }
4911
4912 /* bring the ctx back to life */
Jens Axboe206aefd2019-11-07 18:27:42 -07004913 reinit_completion(&ctx->completions[0]);
Jens Axboeedafcce2019-01-09 09:16:05 -07004914 percpu_ref_reinit(&ctx->refs);
4915 return ret;
4916}
4917
4918SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
4919 void __user *, arg, unsigned int, nr_args)
4920{
4921 struct io_ring_ctx *ctx;
4922 long ret = -EBADF;
4923 struct fd f;
4924
4925 f = fdget(fd);
4926 if (!f.file)
4927 return -EBADF;
4928
4929 ret = -EOPNOTSUPP;
4930 if (f.file->f_op != &io_uring_fops)
4931 goto out_fput;
4932
4933 ctx = f.file->private_data;
4934
4935 mutex_lock(&ctx->uring_lock);
4936 ret = __io_uring_register(ctx, opcode, arg, nr_args);
4937 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004938 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
4939 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07004940out_fput:
4941 fdput(f);
4942 return ret;
4943}
4944
Jens Axboe2b188cc2019-01-07 10:46:33 -07004945static int __init io_uring_init(void)
4946{
4947 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
4948 return 0;
4949};
4950__initcall(io_uring_init);