blob: bbbd9f664b1ea0681e41aec537b58a0d98452fbc [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 Axboe1a6b74f2019-12-02 10:33:15 -0700311struct io_async_ctx {
312 struct io_uring_sqe sqe;
313};
314
Jens Axboe09bb8392019-03-13 12:39:28 -0600315/*
316 * NOTE! Each of the iocb union members has the file pointer
317 * as the first entry in their struct definition. So you can
318 * access the file pointer through any of the sub-structs,
319 * or directly as just 'ki_filp' in this struct.
320 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700321struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700322 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600323 struct file *file;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700324 struct kiocb rw;
325 struct io_poll_iocb poll;
Jens Axboe5262f562019-09-17 12:26:57 -0600326 struct io_timeout timeout;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700327 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700328
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300329 const struct io_uring_sqe *sqe;
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700330 struct io_async_ctx *io;
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300331 struct file *ring_file;
332 int ring_fd;
333 bool has_user;
334 bool in_async;
335 bool needs_fixed_file;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700336
337 struct io_ring_ctx *ctx;
Jens Axboeeac406c2019-11-14 12:09:58 -0700338 union {
339 struct list_head list;
340 struct rb_node rb_node;
341 };
Jens Axboe9e645e112019-05-10 16:07:28 -0600342 struct list_head link_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700343 unsigned int flags;
Jens Axboec16361c2019-01-17 08:39:48 -0700344 refcount_t refs;
Stefan Bühler8449eed2019-04-27 20:34:19 +0200345#define REQ_F_NOWAIT 1 /* must not punt to workers */
Jens Axboedef596e2019-01-09 08:59:42 -0700346#define REQ_F_IOPOLL_COMPLETED 2 /* polled IO has completed */
Jens Axboe6b063142019-01-10 22:13:58 -0700347#define REQ_F_FIXED_FILE 4 /* ctx owns file */
Jens Axboe4d7dd462019-11-20 13:03:52 -0700348#define REQ_F_LINK_NEXT 8 /* already grabbed next link */
Stefan Bühlere2033e32019-05-11 19:08:01 +0200349#define REQ_F_IO_DRAIN 16 /* drain existing IO first */
350#define REQ_F_IO_DRAINED 32 /* drain done */
Jens Axboe9e645e112019-05-10 16:07:28 -0600351#define REQ_F_LINK 64 /* linked sqes */
Jens Axboe2665abf2019-11-05 12:40:47 -0700352#define REQ_F_LINK_TIMEOUT 128 /* has linked timeout */
Zhengyuan Liuf7b76ac2019-07-16 23:26:14 +0800353#define REQ_F_FAIL_LINK 256 /* fail rest of links */
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +0300354#define REQ_F_DRAIN_LINK 512 /* link should be fully drained */
Jens Axboe5262f562019-09-17 12:26:57 -0600355#define REQ_F_TIMEOUT 1024 /* timeout request */
Jens Axboe491381ce2019-10-17 09:20:46 -0600356#define REQ_F_ISREG 2048 /* regular file */
357#define REQ_F_MUST_PUNT 4096 /* must be punted even for NONBLOCK */
Jens Axboe93bd25b2019-11-11 23:34:31 -0700358#define REQ_F_TIMEOUT_NOSEQ 8192 /* no timeout sequence */
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800359#define REQ_F_INFLIGHT 16384 /* on inflight list */
360#define REQ_F_COMP_LOCKED 32768 /* completion under lock */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700361 u64 user_data;
Jens Axboe9e645e112019-05-10 16:07:28 -0600362 u32 result;
Jens Axboede0617e2019-04-06 21:51:27 -0600363 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700364
Jens Axboefcb323c2019-10-24 12:39:47 -0600365 struct list_head inflight_entry;
366
Jens Axboe561fb042019-10-24 07:25:42 -0600367 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700368};
369
370#define IO_PLUG_THRESHOLD 2
Jens Axboedef596e2019-01-09 08:59:42 -0700371#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700372
Jens Axboe9a56a232019-01-09 09:06:50 -0700373struct io_submit_state {
374 struct blk_plug plug;
375
376 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700377 * io_kiocb alloc cache
378 */
379 void *reqs[IO_IOPOLL_BATCH];
380 unsigned int free_reqs;
381 unsigned int cur_req;
382
383 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700384 * File reference cache
385 */
386 struct file *file;
387 unsigned int fd;
388 unsigned int has_refs;
389 unsigned int used_refs;
390 unsigned int ios_left;
391};
392
Jens Axboe561fb042019-10-24 07:25:42 -0600393static void io_wq_submit_work(struct io_wq_work **workptr);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700394static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liu4fe2c962019-09-09 20:50:40 +0800395static void __io_free_req(struct io_kiocb *req);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800396static void io_put_req(struct io_kiocb *req);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700397static void io_double_put_req(struct io_kiocb *req);
Jens Axboe978db572019-11-14 22:39:04 -0700398static void __io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700399static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
400static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboede0617e2019-04-06 21:51:27 -0600401
Jens Axboe2b188cc2019-01-07 10:46:33 -0700402static struct kmem_cache *req_cachep;
403
404static const struct file_operations io_uring_fops;
405
406struct sock *io_uring_get_socket(struct file *file)
407{
408#if defined(CONFIG_UNIX)
409 if (file->f_op == &io_uring_fops) {
410 struct io_ring_ctx *ctx = file->private_data;
411
412 return ctx->ring_sock->sk;
413 }
414#endif
415 return NULL;
416}
417EXPORT_SYMBOL(io_uring_get_socket);
418
419static void io_ring_ctx_ref_free(struct percpu_ref *ref)
420{
421 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
422
Jens Axboe206aefd2019-11-07 18:27:42 -0700423 complete(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700424}
425
426static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
427{
428 struct io_ring_ctx *ctx;
429
430 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
431 if (!ctx)
432 return NULL;
433
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700434 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
435 if (!ctx->fallback_req)
436 goto err;
437
Jens Axboe206aefd2019-11-07 18:27:42 -0700438 ctx->completions = kmalloc(2 * sizeof(struct completion), GFP_KERNEL);
439 if (!ctx->completions)
440 goto err;
441
Roman Gushchin21482892019-05-07 10:01:48 -0700442 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -0700443 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
444 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700445
446 ctx->flags = p->flags;
447 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700448 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe206aefd2019-11-07 18:27:42 -0700449 init_completion(&ctx->completions[0]);
450 init_completion(&ctx->completions[1]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700451 mutex_init(&ctx->uring_lock);
452 init_waitqueue_head(&ctx->wait);
453 spin_lock_init(&ctx->completion_lock);
Jens Axboedef596e2019-01-09 08:59:42 -0700454 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboeeac406c2019-11-14 12:09:58 -0700455 ctx->cancel_tree = RB_ROOT;
Jens Axboede0617e2019-04-06 21:51:27 -0600456 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -0600457 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -0600458 init_waitqueue_head(&ctx->inflight_wait);
459 spin_lock_init(&ctx->inflight_lock);
460 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700461 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -0700462err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700463 if (ctx->fallback_req)
464 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe206aefd2019-11-07 18:27:42 -0700465 kfree(ctx->completions);
466 kfree(ctx);
467 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700468}
469
Bob Liu9d858b22019-11-13 18:06:25 +0800470static inline bool __req_need_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -0600471{
Jackie Liua197f662019-11-08 08:09:12 -0700472 struct io_ring_ctx *ctx = req->ctx;
473
Jens Axboe498ccd92019-10-25 10:04:25 -0600474 return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped
475 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboede0617e2019-04-06 21:51:27 -0600476}
477
Bob Liu9d858b22019-11-13 18:06:25 +0800478static inline bool req_need_defer(struct io_kiocb *req)
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600479{
Bob Liu9d858b22019-11-13 18:06:25 +0800480 if ((req->flags & (REQ_F_IO_DRAIN|REQ_F_IO_DRAINED)) == REQ_F_IO_DRAIN)
481 return __req_need_defer(req);
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600482
Bob Liu9d858b22019-11-13 18:06:25 +0800483 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600484}
485
486static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -0600487{
488 struct io_kiocb *req;
489
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600490 req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list);
Bob Liu9d858b22019-11-13 18:06:25 +0800491 if (req && !req_need_defer(req)) {
Jens Axboede0617e2019-04-06 21:51:27 -0600492 list_del_init(&req->list);
493 return req;
494 }
495
496 return NULL;
497}
498
Jens Axboe5262f562019-09-17 12:26:57 -0600499static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx)
500{
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600501 struct io_kiocb *req;
502
503 req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list);
Jens Axboe93bd25b2019-11-11 23:34:31 -0700504 if (req) {
505 if (req->flags & REQ_F_TIMEOUT_NOSEQ)
506 return NULL;
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800507 if (!__req_need_defer(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -0700508 list_del_init(&req->list);
509 return req;
510 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600511 }
512
513 return NULL;
Jens Axboe5262f562019-09-17 12:26:57 -0600514}
515
Jens Axboede0617e2019-04-06 21:51:27 -0600516static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700517{
Hristo Venev75b28af2019-08-26 17:23:46 +0000518 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700519
Hristo Venev75b28af2019-08-26 17:23:46 +0000520 if (ctx->cached_cq_tail != READ_ONCE(rings->cq.tail)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700521 /* order cqe stores with ring update */
Hristo Venev75b28af2019-08-26 17:23:46 +0000522 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700523
Jens Axboe2b188cc2019-01-07 10:46:33 -0700524 if (wq_has_sleeper(&ctx->cq_wait)) {
525 wake_up_interruptible(&ctx->cq_wait);
526 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
527 }
528 }
529}
530
Jens Axboe561fb042019-10-24 07:25:42 -0600531static inline bool io_sqe_needs_user(const struct io_uring_sqe *sqe)
Jens Axboe18d9be12019-09-10 09:13:05 -0600532{
Jens Axboe561fb042019-10-24 07:25:42 -0600533 u8 opcode = READ_ONCE(sqe->opcode);
534
535 return !(opcode == IORING_OP_READ_FIXED ||
536 opcode == IORING_OP_WRITE_FIXED);
537}
538
Jens Axboe94ae5e72019-11-14 19:39:52 -0700539static inline bool io_prep_async_work(struct io_kiocb *req,
540 struct io_kiocb **link)
Jens Axboe561fb042019-10-24 07:25:42 -0600541{
542 bool do_hashed = false;
Jens Axboe54a91f32019-09-10 09:15:04 -0600543
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300544 if (req->sqe) {
545 switch (req->sqe->opcode) {
Jens Axboe6cc47d12019-09-18 11:18:23 -0600546 case IORING_OP_WRITEV:
547 case IORING_OP_WRITE_FIXED:
Jens Axboe561fb042019-10-24 07:25:42 -0600548 do_hashed = true;
Jens Axboe5f8fd2d32019-11-07 10:57:36 -0700549 /* fall-through */
550 case IORING_OP_READV:
551 case IORING_OP_READ_FIXED:
552 case IORING_OP_SENDMSG:
553 case IORING_OP_RECVMSG:
554 case IORING_OP_ACCEPT:
555 case IORING_OP_POLL_ADD:
Jens Axboef8e85cf2019-11-23 14:24:24 -0700556 case IORING_OP_CONNECT:
Jens Axboe5f8fd2d32019-11-07 10:57:36 -0700557 /*
558 * We know REQ_F_ISREG is not set on some of these
559 * opcodes, but this enables us to keep the check in
560 * just one place.
561 */
562 if (!(req->flags & REQ_F_ISREG))
563 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe6cc47d12019-09-18 11:18:23 -0600564 break;
565 }
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300566 if (io_sqe_needs_user(req->sqe))
Jens Axboe561fb042019-10-24 07:25:42 -0600567 req->work.flags |= IO_WQ_WORK_NEEDS_USER;
Jens Axboe54a91f32019-09-10 09:15:04 -0600568 }
569
Jens Axboe94ae5e72019-11-14 19:39:52 -0700570 *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -0600571 return do_hashed;
572}
573
Jackie Liua197f662019-11-08 08:09:12 -0700574static inline void io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -0600575{
Jackie Liua197f662019-11-08 08:09:12 -0700576 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe94ae5e72019-11-14 19:39:52 -0700577 struct io_kiocb *link;
578 bool do_hashed;
579
580 do_hashed = io_prep_async_work(req, &link);
Jens Axboe561fb042019-10-24 07:25:42 -0600581
582 trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work,
583 req->flags);
584 if (!do_hashed) {
585 io_wq_enqueue(ctx->io_wq, &req->work);
586 } else {
587 io_wq_enqueue_hashed(ctx->io_wq, &req->work,
588 file_inode(req->file));
589 }
Jens Axboe94ae5e72019-11-14 19:39:52 -0700590
591 if (link)
592 io_queue_linked_timeout(link);
Jens Axboe18d9be12019-09-10 09:13:05 -0600593}
594
Jens Axboe5262f562019-09-17 12:26:57 -0600595static void io_kill_timeout(struct io_kiocb *req)
596{
597 int ret;
598
Jens Axboead8a48a2019-11-15 08:49:11 -0700599 ret = hrtimer_try_to_cancel(&req->timeout.data->timer);
Jens Axboe5262f562019-09-17 12:26:57 -0600600 if (ret != -1) {
601 atomic_inc(&req->ctx->cq_timeouts);
Jens Axboe842f9612019-10-29 12:34:10 -0600602 list_del_init(&req->list);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700603 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800604 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -0600605 }
606}
607
608static void io_kill_timeouts(struct io_ring_ctx *ctx)
609{
610 struct io_kiocb *req, *tmp;
611
612 spin_lock_irq(&ctx->completion_lock);
613 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
614 io_kill_timeout(req);
615 spin_unlock_irq(&ctx->completion_lock);
616}
617
Jens Axboede0617e2019-04-06 21:51:27 -0600618static void io_commit_cqring(struct io_ring_ctx *ctx)
619{
620 struct io_kiocb *req;
621
Jens Axboe5262f562019-09-17 12:26:57 -0600622 while ((req = io_get_timeout_req(ctx)) != NULL)
623 io_kill_timeout(req);
624
Jens Axboede0617e2019-04-06 21:51:27 -0600625 __io_commit_cqring(ctx);
626
627 while ((req = io_get_deferred_req(ctx)) != NULL) {
628 req->flags |= REQ_F_IO_DRAINED;
Jackie Liua197f662019-11-08 08:09:12 -0700629 io_queue_async_work(req);
Jens Axboede0617e2019-04-06 21:51:27 -0600630 }
631}
632
Jens Axboe2b188cc2019-01-07 10:46:33 -0700633static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
634{
Hristo Venev75b28af2019-08-26 17:23:46 +0000635 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700636 unsigned tail;
637
638 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +0200639 /*
640 * writes to the cq entry need to come after reading head; the
641 * control dependency is enough as we're using WRITE_ONCE to
642 * fill the cq entry
643 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000644 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700645 return NULL;
646
647 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +0000648 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -0700649}
650
Jens Axboe8c838782019-03-12 15:48:16 -0600651static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
652{
653 if (waitqueue_active(&ctx->wait))
654 wake_up(&ctx->wait);
655 if (waitqueue_active(&ctx->sqo_wait))
656 wake_up(&ctx->sqo_wait);
Jens Axboe9b402842019-04-11 11:45:41 -0600657 if (ctx->cq_ev_fd)
658 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -0600659}
660
Jens Axboec4a2ed72019-11-21 21:01:26 -0700661/* Returns true if there are no backlogged entries after the flush */
662static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700663{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700664 struct io_rings *rings = ctx->rings;
665 struct io_uring_cqe *cqe;
666 struct io_kiocb *req;
667 unsigned long flags;
668 LIST_HEAD(list);
669
670 if (!force) {
671 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -0700672 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700673 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
674 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -0700675 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700676 }
677
678 spin_lock_irqsave(&ctx->completion_lock, flags);
679
680 /* if force is set, the ring is going away. always drop after that */
681 if (force)
682 ctx->cq_overflow_flushed = true;
683
Jens Axboec4a2ed72019-11-21 21:01:26 -0700684 cqe = NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700685 while (!list_empty(&ctx->cq_overflow_list)) {
686 cqe = io_get_cqring(ctx);
687 if (!cqe && !force)
688 break;
689
690 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
691 list);
692 list_move(&req->list, &list);
693 if (cqe) {
694 WRITE_ONCE(cqe->user_data, req->user_data);
695 WRITE_ONCE(cqe->res, req->result);
696 WRITE_ONCE(cqe->flags, 0);
697 } else {
698 WRITE_ONCE(ctx->rings->cq_overflow,
699 atomic_inc_return(&ctx->cached_cq_overflow));
700 }
701 }
702
703 io_commit_cqring(ctx);
704 spin_unlock_irqrestore(&ctx->completion_lock, flags);
705 io_cqring_ev_posted(ctx);
706
707 while (!list_empty(&list)) {
708 req = list_first_entry(&list, struct io_kiocb, list);
709 list_del(&req->list);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800710 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700711 }
Jens Axboec4a2ed72019-11-21 21:01:26 -0700712
713 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700714}
715
Jens Axboe78e19bb2019-11-06 15:21:34 -0700716static void io_cqring_fill_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700717{
Jens Axboe78e19bb2019-11-06 15:21:34 -0700718 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700719 struct io_uring_cqe *cqe;
720
Jens Axboe78e19bb2019-11-06 15:21:34 -0700721 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -0700722
Jens Axboe2b188cc2019-01-07 10:46:33 -0700723 /*
724 * If we can't get a cq entry, userspace overflowed the
725 * submission (by quite a lot). Increment the overflow count in
726 * the ring.
727 */
728 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700729 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -0700730 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700731 WRITE_ONCE(cqe->res, res);
732 WRITE_ONCE(cqe->flags, 0);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700733 } else if (ctx->cq_overflow_flushed) {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700734 WRITE_ONCE(ctx->rings->cq_overflow,
735 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700736 } else {
737 refcount_inc(&req->refs);
738 req->result = res;
739 list_add_tail(&req->list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700740 }
741}
742
Jens Axboe78e19bb2019-11-06 15:21:34 -0700743static void io_cqring_add_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700744{
Jens Axboe78e19bb2019-11-06 15:21:34 -0700745 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700746 unsigned long flags;
747
748 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700749 io_cqring_fill_event(req, res);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700750 io_commit_cqring(ctx);
751 spin_unlock_irqrestore(&ctx->completion_lock, flags);
752
Jens Axboe8c838782019-03-12 15:48:16 -0600753 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700754}
755
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700756static inline bool io_is_fallback_req(struct io_kiocb *req)
757{
758 return req == (struct io_kiocb *)
759 ((unsigned long) req->ctx->fallback_req & ~1UL);
760}
761
762static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
763{
764 struct io_kiocb *req;
765
766 req = ctx->fallback_req;
767 if (!test_and_set_bit_lock(0, (unsigned long *) ctx->fallback_req))
768 return req;
769
770 return NULL;
771}
772
Jens Axboe2579f912019-01-09 09:10:43 -0700773static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
774 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700775{
Jens Axboefd6fab22019-03-14 16:30:06 -0600776 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700777 struct io_kiocb *req;
778
779 if (!percpu_ref_tryget(&ctx->refs))
780 return NULL;
781
Jens Axboe2579f912019-01-09 09:10:43 -0700782 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -0600783 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -0700784 if (unlikely(!req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700785 goto fallback;
Jens Axboe2579f912019-01-09 09:10:43 -0700786 } else if (!state->free_reqs) {
787 size_t sz;
788 int ret;
789
790 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -0600791 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
792
793 /*
794 * Bulk alloc is all-or-nothing. If we fail to get a batch,
795 * retry single alloc to be on the safe side.
796 */
797 if (unlikely(ret <= 0)) {
798 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
799 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700800 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -0600801 ret = 1;
802 }
Jens Axboe2579f912019-01-09 09:10:43 -0700803 state->free_reqs = ret - 1;
804 state->cur_req = 1;
805 req = state->reqs[0];
806 } else {
807 req = state->reqs[state->cur_req];
808 state->free_reqs--;
809 state->cur_req++;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700810 }
811
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700812got_it:
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700813 req->io = NULL;
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300814 req->ring_file = NULL;
Jens Axboe60c112b2019-06-21 10:20:18 -0600815 req->file = NULL;
Jens Axboe2579f912019-01-09 09:10:43 -0700816 req->ctx = ctx;
817 req->flags = 0;
Jens Axboee65ef562019-03-12 10:16:44 -0600818 /* one is dropped after submission, the other at completion */
819 refcount_set(&req->refs, 2);
Jens Axboe9e645e112019-05-10 16:07:28 -0600820 req->result = 0;
Jens Axboe561fb042019-10-24 07:25:42 -0600821 INIT_IO_WORK(&req->work, io_wq_submit_work);
Jens Axboe2579f912019-01-09 09:10:43 -0700822 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700823fallback:
824 req = io_get_fallback_req(ctx);
825 if (req)
826 goto got_it;
Pavel Begunkov6805b322019-10-08 02:18:42 +0300827 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700828 return NULL;
829}
830
Jens Axboedef596e2019-01-09 08:59:42 -0700831static void io_free_req_many(struct io_ring_ctx *ctx, void **reqs, int *nr)
832{
833 if (*nr) {
834 kmem_cache_free_bulk(req_cachep, *nr, reqs);
Pavel Begunkov6805b322019-10-08 02:18:42 +0300835 percpu_ref_put_many(&ctx->refs, *nr);
Jens Axboedef596e2019-01-09 08:59:42 -0700836 *nr = 0;
837 }
838}
839
Jens Axboe9e645e112019-05-10 16:07:28 -0600840static void __io_free_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700841{
Jens Axboefcb323c2019-10-24 12:39:47 -0600842 struct io_ring_ctx *ctx = req->ctx;
843
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700844 if (req->io)
845 kfree(req->io);
Jens Axboe09bb8392019-03-13 12:39:28 -0600846 if (req->file && !(req->flags & REQ_F_FIXED_FILE))
847 fput(req->file);
Jens Axboefcb323c2019-10-24 12:39:47 -0600848 if (req->flags & REQ_F_INFLIGHT) {
849 unsigned long flags;
850
851 spin_lock_irqsave(&ctx->inflight_lock, flags);
852 list_del(&req->inflight_entry);
853 if (waitqueue_active(&ctx->inflight_wait))
854 wake_up(&ctx->inflight_wait);
855 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
856 }
Jens Axboead8a48a2019-11-15 08:49:11 -0700857 if (req->flags & REQ_F_TIMEOUT)
858 kfree(req->timeout.data);
Jens Axboefcb323c2019-10-24 12:39:47 -0600859 percpu_ref_put(&ctx->refs);
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700860 if (likely(!io_is_fallback_req(req)))
861 kmem_cache_free(req_cachep, req);
862 else
863 clear_bit_unlock(0, (unsigned long *) ctx->fallback_req);
Jens Axboee65ef562019-03-12 10:16:44 -0600864}
865
Jackie Liua197f662019-11-08 08:09:12 -0700866static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -0600867{
Jackie Liua197f662019-11-08 08:09:12 -0700868 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -0700869 int ret;
870
Jens Axboead8a48a2019-11-15 08:49:11 -0700871 ret = hrtimer_try_to_cancel(&req->timeout.data->timer);
Jens Axboe2665abf2019-11-05 12:40:47 -0700872 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -0700873 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -0700874 io_commit_cqring(ctx);
875 req->flags &= ~REQ_F_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +0800876 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -0700877 return true;
878 }
879
880 return false;
881}
882
Jens Axboeba816ad2019-09-28 11:36:45 -0600883static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -0600884{
Jens Axboe2665abf2019-11-05 12:40:47 -0700885 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -0600886 struct io_kiocb *nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -0700887 bool wake_ev = false;
Jens Axboe9e645e112019-05-10 16:07:28 -0600888
Jens Axboe4d7dd462019-11-20 13:03:52 -0700889 /* Already got next link */
890 if (req->flags & REQ_F_LINK_NEXT)
891 return;
892
Jens Axboe9e645e112019-05-10 16:07:28 -0600893 /*
894 * The list should never be empty when we are called here. But could
895 * potentially happen if the chain is messed up, check to be on the
896 * safe side.
897 */
898 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, list);
Jens Axboe2665abf2019-11-05 12:40:47 -0700899 while (nxt) {
Jens Axboe76a46e02019-11-10 23:34:16 -0700900 list_del_init(&nxt->list);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700901
902 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
903 (nxt->flags & REQ_F_TIMEOUT)) {
904 wake_ev |= io_link_cancel_timeout(nxt);
905 nxt = list_first_entry_or_null(&req->link_list,
906 struct io_kiocb, list);
907 req->flags &= ~REQ_F_LINK_TIMEOUT;
908 continue;
909 }
Jens Axboe9e645e112019-05-10 16:07:28 -0600910 if (!list_empty(&req->link_list)) {
911 INIT_LIST_HEAD(&nxt->link_list);
912 list_splice(&req->link_list, &nxt->link_list);
913 nxt->flags |= REQ_F_LINK;
914 }
915
Pavel Begunkovb18fdf72019-11-21 23:21:02 +0300916 *nxtptr = nxt;
Jens Axboe94ae5e72019-11-14 19:39:52 -0700917 break;
Jens Axboe9e645e112019-05-10 16:07:28 -0600918 }
Jens Axboe2665abf2019-11-05 12:40:47 -0700919
Jens Axboe4d7dd462019-11-20 13:03:52 -0700920 req->flags |= REQ_F_LINK_NEXT;
Jens Axboe2665abf2019-11-05 12:40:47 -0700921 if (wake_ev)
922 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -0600923}
924
925/*
926 * Called if REQ_F_LINK is set, and we fail the head request
927 */
928static void io_fail_links(struct io_kiocb *req)
929{
Jens Axboe2665abf2019-11-05 12:40:47 -0700930 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -0600931 struct io_kiocb *link;
Jens Axboe2665abf2019-11-05 12:40:47 -0700932 unsigned long flags;
933
934 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -0600935
936 while (!list_empty(&req->link_list)) {
937 link = list_first_entry(&req->link_list, struct io_kiocb, list);
Jens Axboe2665abf2019-11-05 12:40:47 -0700938 list_del_init(&link->list);
Jens Axboe9e645e112019-05-10 16:07:28 -0600939
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +0200940 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -0700941
942 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300943 link->sqe->opcode == IORING_OP_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -0700944 io_link_cancel_timeout(link);
Jens Axboe2665abf2019-11-05 12:40:47 -0700945 } else {
Jens Axboe78e19bb2019-11-06 15:21:34 -0700946 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe978db572019-11-14 22:39:04 -0700947 __io_double_put_req(link);
Jens Axboe2665abf2019-11-05 12:40:47 -0700948 }
Jens Axboe5d960722019-11-19 15:31:28 -0700949 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -0600950 }
Jens Axboe2665abf2019-11-05 12:40:47 -0700951
952 io_commit_cqring(ctx);
953 spin_unlock_irqrestore(&ctx->completion_lock, flags);
954 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -0600955}
956
Jens Axboe4d7dd462019-11-20 13:03:52 -0700957static void io_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -0600958{
Jens Axboe4d7dd462019-11-20 13:03:52 -0700959 if (likely(!(req->flags & REQ_F_LINK)))
Jens Axboe2665abf2019-11-05 12:40:47 -0700960 return;
Jens Axboe2665abf2019-11-05 12:40:47 -0700961
Jens Axboe9e645e112019-05-10 16:07:28 -0600962 /*
963 * If LINK is set, we have dependent requests in this chain. If we
964 * didn't fail this request, queue the first one up, moving any other
965 * dependencies to the next request. In case of failure, fail the rest
966 * of the chain.
967 */
Jens Axboe2665abf2019-11-05 12:40:47 -0700968 if (req->flags & REQ_F_FAIL_LINK) {
969 io_fail_links(req);
Jens Axboe7c9e7f02019-11-12 08:15:53 -0700970 } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) ==
971 REQ_F_LINK_TIMEOUT) {
Jens Axboe2665abf2019-11-05 12:40:47 -0700972 struct io_ring_ctx *ctx = req->ctx;
973 unsigned long flags;
974
975 /*
976 * If this is a timeout link, we could be racing with the
977 * timeout timer. Grab the completion lock for this case to
Jens Axboe7c9e7f02019-11-12 08:15:53 -0700978 * protect against that.
Jens Axboe2665abf2019-11-05 12:40:47 -0700979 */
980 spin_lock_irqsave(&ctx->completion_lock, flags);
981 io_req_link_next(req, nxt);
982 spin_unlock_irqrestore(&ctx->completion_lock, flags);
983 } else {
984 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -0600985 }
Jens Axboe4d7dd462019-11-20 13:03:52 -0700986}
Jens Axboe9e645e112019-05-10 16:07:28 -0600987
Jackie Liuc69f8db2019-11-09 11:00:08 +0800988static void io_free_req(struct io_kiocb *req)
989{
Pavel Begunkov944e58b2019-11-21 23:21:01 +0300990 struct io_kiocb *nxt = NULL;
991
992 io_req_find_next(req, &nxt);
Pavel Begunkov70cf9f32019-11-21 23:21:00 +0300993 __io_free_req(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +0300994
995 if (nxt)
996 io_queue_async_work(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +0800997}
998
Jens Axboeba816ad2019-09-28 11:36:45 -0600999/*
1000 * Drop reference to request, return next in chain (if there is one) if this
1001 * was the last reference to this request.
1002 */
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001003__attribute__((nonnull))
Jackie Liuec9c02a2019-11-08 23:50:36 +08001004static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboee65ef562019-03-12 10:16:44 -06001005{
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001006 io_req_find_next(req, nxtptr);
Jens Axboe4d7dd462019-11-20 13:03:52 -07001007
Jens Axboee65ef562019-03-12 10:16:44 -06001008 if (refcount_dec_and_test(&req->refs))
Jens Axboe4d7dd462019-11-20 13:03:52 -07001009 __io_free_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001010}
1011
Jens Axboe2b188cc2019-01-07 10:46:33 -07001012static void io_put_req(struct io_kiocb *req)
1013{
Jens Axboedef596e2019-01-09 08:59:42 -07001014 if (refcount_dec_and_test(&req->refs))
1015 io_free_req(req);
1016}
1017
Jens Axboe978db572019-11-14 22:39:04 -07001018/*
1019 * Must only be used if we don't need to care about links, usually from
1020 * within the completion handling itself.
1021 */
1022static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06001023{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001024 /* drop both submit and complete references */
1025 if (refcount_sub_and_test(2, &req->refs))
1026 __io_free_req(req);
1027}
1028
Jens Axboe978db572019-11-14 22:39:04 -07001029static void io_double_put_req(struct io_kiocb *req)
1030{
1031 /* drop both submit and complete references */
1032 if (refcount_sub_and_test(2, &req->refs))
1033 io_free_req(req);
1034}
1035
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001036static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06001037{
Jens Axboe84f97dc2019-11-06 11:27:53 -07001038 struct io_rings *rings = ctx->rings;
1039
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001040 /*
1041 * noflush == true is from the waitqueue handler, just ensure we wake
1042 * up the task, and the next invocation will flush the entries. We
1043 * cannot safely to it from here.
1044 */
1045 if (noflush && !list_empty(&ctx->cq_overflow_list))
1046 return -1U;
1047
1048 io_cqring_overflow_flush(ctx, false);
1049
Jens Axboea3a0e432019-08-20 11:03:11 -06001050 /* See comment at the top of this file */
1051 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00001052 return READ_ONCE(rings->cq.tail) - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06001053}
1054
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03001055static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1056{
1057 struct io_rings *rings = ctx->rings;
1058
1059 /* make sure SQ entry isn't read before tail */
1060 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1061}
1062
Jens Axboedef596e2019-01-09 08:59:42 -07001063/*
1064 * Find and free completed poll iocbs
1065 */
1066static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
1067 struct list_head *done)
1068{
1069 void *reqs[IO_IOPOLL_BATCH];
1070 struct io_kiocb *req;
Jens Axboe09bb8392019-03-13 12:39:28 -06001071 int to_free;
Jens Axboedef596e2019-01-09 08:59:42 -07001072
Jens Axboe09bb8392019-03-13 12:39:28 -06001073 to_free = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001074 while (!list_empty(done)) {
1075 req = list_first_entry(done, struct io_kiocb, list);
1076 list_del(&req->list);
1077
Jens Axboe78e19bb2019-11-06 15:21:34 -07001078 io_cqring_fill_event(req, req->result);
Jens Axboedef596e2019-01-09 08:59:42 -07001079 (*nr_events)++;
1080
Jens Axboe09bb8392019-03-13 12:39:28 -06001081 if (refcount_dec_and_test(&req->refs)) {
1082 /* If we're not using fixed files, we have to pair the
1083 * completion part with the file put. Use regular
1084 * completions for those, only batch free for fixed
Jens Axboe9e645e112019-05-10 16:07:28 -06001085 * file and non-linked commands.
Jens Axboe09bb8392019-03-13 12:39:28 -06001086 */
Jens Axboe1a6b74f2019-12-02 10:33:15 -07001087 if (((req->flags & (REQ_F_FIXED_FILE|REQ_F_LINK)) ==
1088 REQ_F_FIXED_FILE) && !io_is_fallback_req(req) &&
1089 !req->io) {
Jens Axboe09bb8392019-03-13 12:39:28 -06001090 reqs[to_free++] = req;
1091 if (to_free == ARRAY_SIZE(reqs))
1092 io_free_req_many(ctx, reqs, &to_free);
Jens Axboe6b063142019-01-10 22:13:58 -07001093 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001094 io_free_req(req);
Jens Axboe6b063142019-01-10 22:13:58 -07001095 }
Jens Axboe9a56a232019-01-09 09:06:50 -07001096 }
Jens Axboedef596e2019-01-09 08:59:42 -07001097 }
Jens Axboedef596e2019-01-09 08:59:42 -07001098
Jens Axboe09bb8392019-03-13 12:39:28 -06001099 io_commit_cqring(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07001100 io_free_req_many(ctx, reqs, &to_free);
1101}
1102
1103static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1104 long min)
1105{
1106 struct io_kiocb *req, *tmp;
1107 LIST_HEAD(done);
1108 bool spin;
1109 int ret;
1110
1111 /*
1112 * Only spin for completions if we don't have multiple devices hanging
1113 * off our complete list, and we're under the requested amount.
1114 */
1115 spin = !ctx->poll_multi_file && *nr_events < min;
1116
1117 ret = 0;
1118 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
1119 struct kiocb *kiocb = &req->rw;
1120
1121 /*
1122 * Move completed entries to our local list. If we find a
1123 * request that requires polling, break out and complete
1124 * the done list first, if we have entries there.
1125 */
1126 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
1127 list_move_tail(&req->list, &done);
1128 continue;
1129 }
1130 if (!list_empty(&done))
1131 break;
1132
1133 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1134 if (ret < 0)
1135 break;
1136
1137 if (ret && spin)
1138 spin = false;
1139 ret = 0;
1140 }
1141
1142 if (!list_empty(&done))
1143 io_iopoll_complete(ctx, nr_events, &done);
1144
1145 return ret;
1146}
1147
1148/*
1149 * Poll for a mininum of 'min' events. Note that if min == 0 we consider that a
1150 * non-spinning poll check - we'll still enter the driver poll loop, but only
1151 * as a non-spinning completion check.
1152 */
1153static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
1154 long min)
1155{
Jens Axboe08f54392019-08-21 22:19:11 -06001156 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07001157 int ret;
1158
1159 ret = io_do_iopoll(ctx, nr_events, min);
1160 if (ret < 0)
1161 return ret;
1162 if (!min || *nr_events >= min)
1163 return 0;
1164 }
1165
1166 return 1;
1167}
1168
1169/*
1170 * We can't just wait for polled events to come to us, we have to actively
1171 * find and complete them.
1172 */
1173static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
1174{
1175 if (!(ctx->flags & IORING_SETUP_IOPOLL))
1176 return;
1177
1178 mutex_lock(&ctx->uring_lock);
1179 while (!list_empty(&ctx->poll_list)) {
1180 unsigned int nr_events = 0;
1181
1182 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -06001183
1184 /*
1185 * Ensure we allow local-to-the-cpu processing to take place,
1186 * in this case we need to ensure that we reap all events.
1187 */
1188 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -07001189 }
1190 mutex_unlock(&ctx->uring_lock);
1191}
1192
Jens Axboe2b2ed972019-10-25 10:06:15 -06001193static int __io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1194 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07001195{
Jens Axboe2b2ed972019-10-25 10:06:15 -06001196 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001197
1198 do {
1199 int tmin = 0;
1200
Jens Axboe500f9fb2019-08-19 12:15:59 -06001201 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06001202 * Don't enter poll loop if we already have events pending.
1203 * If we do, we can potentially be spinning for commands that
1204 * already triggered a CQE (eg in error).
1205 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001206 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06001207 break;
1208
1209 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06001210 * If a submit got punted to a workqueue, we can have the
1211 * application entering polling for a command before it gets
1212 * issued. That app will hold the uring_lock for the duration
1213 * of the poll right here, so we need to take a breather every
1214 * now and then to ensure that the issue has a chance to add
1215 * the poll to the issued list. Otherwise we can spin here
1216 * forever, while the workqueue is stuck trying to acquire the
1217 * very same mutex.
1218 */
1219 if (!(++iters & 7)) {
1220 mutex_unlock(&ctx->uring_lock);
1221 mutex_lock(&ctx->uring_lock);
1222 }
1223
Jens Axboedef596e2019-01-09 08:59:42 -07001224 if (*nr_events < min)
1225 tmin = min - *nr_events;
1226
1227 ret = io_iopoll_getevents(ctx, nr_events, tmin);
1228 if (ret <= 0)
1229 break;
1230 ret = 0;
1231 } while (min && !*nr_events && !need_resched());
1232
Jens Axboe2b2ed972019-10-25 10:06:15 -06001233 return ret;
1234}
1235
1236static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1237 long min)
1238{
1239 int ret;
1240
1241 /*
1242 * We disallow the app entering submit/complete with polling, but we
1243 * still need to lock the ring to prevent racing with polled issue
1244 * that got punted to a workqueue.
1245 */
1246 mutex_lock(&ctx->uring_lock);
1247 ret = __io_iopoll_check(ctx, nr_events, min);
Jens Axboe500f9fb2019-08-19 12:15:59 -06001248 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001249 return ret;
1250}
1251
Jens Axboe491381ce2019-10-17 09:20:46 -06001252static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001253{
Jens Axboe491381ce2019-10-17 09:20:46 -06001254 /*
1255 * Tell lockdep we inherited freeze protection from submission
1256 * thread.
1257 */
1258 if (req->flags & REQ_F_ISREG) {
1259 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001260
Jens Axboe491381ce2019-10-17 09:20:46 -06001261 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001262 }
Jens Axboe491381ce2019-10-17 09:20:46 -06001263 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001264}
1265
Jens Axboeba816ad2019-09-28 11:36:45 -06001266static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001267{
1268 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1269
Jens Axboe491381ce2019-10-17 09:20:46 -06001270 if (kiocb->ki_flags & IOCB_WRITE)
1271 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001272
Jens Axboe9e645e112019-05-10 16:07:28 -06001273 if ((req->flags & REQ_F_LINK) && res != req->result)
1274 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001275 io_cqring_add_event(req, res);
Jens Axboeba816ad2019-09-28 11:36:45 -06001276}
1277
1278static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1279{
1280 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1281
1282 io_complete_rw_common(kiocb, res);
Jens Axboee65ef562019-03-12 10:16:44 -06001283 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001284}
1285
Jens Axboeba816ad2019-09-28 11:36:45 -06001286static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res)
1287{
1288 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001289 struct io_kiocb *nxt = NULL;
Jens Axboeba816ad2019-09-28 11:36:45 -06001290
1291 io_complete_rw_common(kiocb, res);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001292 io_put_req_find_next(req, &nxt);
1293
1294 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001295}
1296
Jens Axboedef596e2019-01-09 08:59:42 -07001297static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1298{
1299 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1300
Jens Axboe491381ce2019-10-17 09:20:46 -06001301 if (kiocb->ki_flags & IOCB_WRITE)
1302 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001303
Jens Axboe9e645e112019-05-10 16:07:28 -06001304 if ((req->flags & REQ_F_LINK) && res != req->result)
1305 req->flags |= REQ_F_FAIL_LINK;
1306 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -07001307 if (res != -EAGAIN)
1308 req->flags |= REQ_F_IOPOLL_COMPLETED;
1309}
1310
1311/*
1312 * After the iocb has been issued, it's safe to be found on the poll list.
1313 * Adding the kiocb to the list AFTER submission ensures that we don't
1314 * find it from a io_iopoll_getevents() thread before the issuer is done
1315 * accessing the kiocb cookie.
1316 */
1317static void io_iopoll_req_issued(struct io_kiocb *req)
1318{
1319 struct io_ring_ctx *ctx = req->ctx;
1320
1321 /*
1322 * Track whether we have multiple files in our lists. This will impact
1323 * how we do polling eventually, not spinning if we're on potentially
1324 * different devices.
1325 */
1326 if (list_empty(&ctx->poll_list)) {
1327 ctx->poll_multi_file = false;
1328 } else if (!ctx->poll_multi_file) {
1329 struct io_kiocb *list_req;
1330
1331 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1332 list);
1333 if (list_req->rw.ki_filp != req->rw.ki_filp)
1334 ctx->poll_multi_file = true;
1335 }
1336
1337 /*
1338 * For fast devices, IO may have already completed. If it has, add
1339 * it to the front so we find it first.
1340 */
1341 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1342 list_add(&req->list, &ctx->poll_list);
1343 else
1344 list_add_tail(&req->list, &ctx->poll_list);
1345}
1346
Jens Axboe3d6770f2019-04-13 11:50:54 -06001347static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07001348{
Jens Axboe3d6770f2019-04-13 11:50:54 -06001349 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -07001350 int diff = state->has_refs - state->used_refs;
1351
1352 if (diff)
1353 fput_many(state->file, diff);
1354 state->file = NULL;
1355 }
1356}
1357
1358/*
1359 * Get as many references to a file as we have IOs left in this submission,
1360 * assuming most submissions are for one file, or at least that each file
1361 * has more than one submission.
1362 */
1363static struct file *io_file_get(struct io_submit_state *state, int fd)
1364{
1365 if (!state)
1366 return fget(fd);
1367
1368 if (state->file) {
1369 if (state->fd == fd) {
1370 state->used_refs++;
1371 state->ios_left--;
1372 return state->file;
1373 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06001374 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07001375 }
1376 state->file = fget_many(fd, state->ios_left);
1377 if (!state->file)
1378 return NULL;
1379
1380 state->fd = fd;
1381 state->has_refs = state->ios_left;
1382 state->used_refs = 1;
1383 state->ios_left--;
1384 return state->file;
1385}
1386
Jens Axboe2b188cc2019-01-07 10:46:33 -07001387/*
1388 * If we tracked the file through the SCM inflight mechanism, we could support
1389 * any file. For now, just ensure that anything potentially problematic is done
1390 * inline.
1391 */
1392static bool io_file_supports_async(struct file *file)
1393{
1394 umode_t mode = file_inode(file)->i_mode;
1395
1396 if (S_ISBLK(mode) || S_ISCHR(mode))
1397 return true;
1398 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
1399 return true;
1400
1401 return false;
1402}
1403
Pavel Begunkov267bc902019-11-07 01:41:08 +03001404static int io_prep_rw(struct io_kiocb *req, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001405{
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001406 const struct io_uring_sqe *sqe = req->sqe;
Jens Axboedef596e2019-01-09 08:59:42 -07001407 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001408 struct kiocb *kiocb = &req->rw;
Jens Axboe09bb8392019-03-13 12:39:28 -06001409 unsigned ioprio;
1410 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001411
Jens Axboe09bb8392019-03-13 12:39:28 -06001412 if (!req->file)
1413 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001414
Jens Axboe491381ce2019-10-17 09:20:46 -06001415 if (S_ISREG(file_inode(req->file)->i_mode))
1416 req->flags |= REQ_F_ISREG;
1417
1418 /*
1419 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
1420 * we know to async punt it even if it was opened O_NONBLOCK
1421 */
1422 if (force_nonblock && !io_file_supports_async(req->file)) {
1423 req->flags |= REQ_F_MUST_PUNT;
1424 return -EAGAIN;
1425 }
Jens Axboe6b063142019-01-10 22:13:58 -07001426
Jens Axboe2b188cc2019-01-07 10:46:33 -07001427 kiocb->ki_pos = READ_ONCE(sqe->off);
1428 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
1429 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
1430
1431 ioprio = READ_ONCE(sqe->ioprio);
1432 if (ioprio) {
1433 ret = ioprio_check_cap(ioprio);
1434 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06001435 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001436
1437 kiocb->ki_ioprio = ioprio;
1438 } else
1439 kiocb->ki_ioprio = get_current_ioprio();
1440
1441 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
1442 if (unlikely(ret))
Jens Axboe09bb8392019-03-13 12:39:28 -06001443 return ret;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001444
1445 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06001446 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
1447 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02001448 req->flags |= REQ_F_NOWAIT;
1449
1450 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001451 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001452
Jens Axboedef596e2019-01-09 08:59:42 -07001453 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07001454 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
1455 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06001456 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001457
Jens Axboedef596e2019-01-09 08:59:42 -07001458 kiocb->ki_flags |= IOCB_HIPRI;
1459 kiocb->ki_complete = io_complete_rw_iopoll;
Jens Axboe6873e0b2019-10-30 13:53:09 -06001460 req->result = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001461 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001462 if (kiocb->ki_flags & IOCB_HIPRI)
1463 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07001464 kiocb->ki_complete = io_complete_rw;
1465 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001466 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001467}
1468
1469static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
1470{
1471 switch (ret) {
1472 case -EIOCBQUEUED:
1473 break;
1474 case -ERESTARTSYS:
1475 case -ERESTARTNOINTR:
1476 case -ERESTARTNOHAND:
1477 case -ERESTART_RESTARTBLOCK:
1478 /*
1479 * We can't just restart the syscall, since previously
1480 * submitted sqes may already be in progress. Just fail this
1481 * IO with EINTR.
1482 */
1483 ret = -EINTR;
1484 /* fall through */
1485 default:
1486 kiocb->ki_complete(kiocb, ret, 0);
1487 }
1488}
1489
Jens Axboeba816ad2019-09-28 11:36:45 -06001490static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt,
1491 bool in_async)
1492{
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001493 if (in_async && ret >= 0 && kiocb->ki_complete == io_complete_rw)
Jens Axboeba816ad2019-09-28 11:36:45 -06001494 *nxt = __io_complete_rw(kiocb, ret);
1495 else
1496 io_rw_done(kiocb, ret);
1497}
1498
Pavel Begunkov7d009162019-11-25 23:14:40 +03001499static ssize_t io_import_fixed(struct io_ring_ctx *ctx, int rw,
1500 const struct io_uring_sqe *sqe,
1501 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07001502{
1503 size_t len = READ_ONCE(sqe->len);
1504 struct io_mapped_ubuf *imu;
1505 unsigned index, buf_index;
1506 size_t offset;
1507 u64 buf_addr;
1508
1509 /* attempt to use fixed buffers without having provided iovecs */
1510 if (unlikely(!ctx->user_bufs))
1511 return -EFAULT;
1512
1513 buf_index = READ_ONCE(sqe->buf_index);
1514 if (unlikely(buf_index >= ctx->nr_user_bufs))
1515 return -EFAULT;
1516
1517 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
1518 imu = &ctx->user_bufs[index];
1519 buf_addr = READ_ONCE(sqe->addr);
1520
1521 /* overflow */
1522 if (buf_addr + len < buf_addr)
1523 return -EFAULT;
1524 /* not inside the mapped region */
1525 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
1526 return -EFAULT;
1527
1528 /*
1529 * May not be a start of buffer, set size appropriately
1530 * and advance us to the beginning.
1531 */
1532 offset = buf_addr - imu->ubuf;
1533 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06001534
1535 if (offset) {
1536 /*
1537 * Don't use iov_iter_advance() here, as it's really slow for
1538 * using the latter parts of a big fixed buffer - it iterates
1539 * over each segment manually. We can cheat a bit here, because
1540 * we know that:
1541 *
1542 * 1) it's a BVEC iter, we set it up
1543 * 2) all bvecs are PAGE_SIZE in size, except potentially the
1544 * first and last bvec
1545 *
1546 * So just find our index, and adjust the iterator afterwards.
1547 * If the offset is within the first bvec (or the whole first
1548 * bvec, just use iov_iter_advance(). This makes it easier
1549 * since we can just skip the first segment, which may not
1550 * be PAGE_SIZE aligned.
1551 */
1552 const struct bio_vec *bvec = imu->bvec;
1553
1554 if (offset <= bvec->bv_len) {
1555 iov_iter_advance(iter, offset);
1556 } else {
1557 unsigned long seg_skip;
1558
1559 /* skip first vec */
1560 offset -= bvec->bv_len;
1561 seg_skip = 1 + (offset >> PAGE_SHIFT);
1562
1563 iter->bvec = bvec + seg_skip;
1564 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02001565 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001566 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001567 }
1568 }
1569
Jens Axboe5e559562019-11-13 16:12:46 -07001570 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001571}
1572
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001573static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
1574 struct iovec **iovec, struct iov_iter *iter)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001575{
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001576 const struct io_uring_sqe *sqe = req->sqe;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001577 void __user *buf = u64_to_user_ptr(READ_ONCE(sqe->addr));
1578 size_t sqe_len = READ_ONCE(sqe->len);
Jens Axboeedafcce2019-01-09 09:16:05 -07001579 u8 opcode;
1580
1581 /*
1582 * We're reading ->opcode for the second time, but the first read
1583 * doesn't care whether it's _FIXED or not, so it doesn't matter
1584 * whether ->opcode changes concurrently. The first read does care
1585 * about whether it is a READ or a WRITE, so we don't trust this read
1586 * for that purpose and instead let the caller pass in the read/write
1587 * flag.
1588 */
1589 opcode = READ_ONCE(sqe->opcode);
Pavel Begunkov7d009162019-11-25 23:14:40 +03001590 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07001591 *iovec = NULL;
Pavel Begunkov7d009162019-11-25 23:14:40 +03001592 return io_import_fixed(req->ctx, rw, sqe, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07001593 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001594
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001595 if (!req->has_user)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001596 return -EFAULT;
1597
1598#ifdef CONFIG_COMPAT
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001599 if (req->ctx->compat)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001600 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
1601 iovec, iter);
1602#endif
1603
1604 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
1605}
1606
Jens Axboe32960612019-09-23 11:05:34 -06001607/*
1608 * For files that don't have ->read_iter() and ->write_iter(), handle them
1609 * by looping over ->read() or ->write() manually.
1610 */
1611static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
1612 struct iov_iter *iter)
1613{
1614 ssize_t ret = 0;
1615
1616 /*
1617 * Don't support polled IO through this interface, and we can't
1618 * support non-blocking either. For the latter, this just causes
1619 * the kiocb to be handled from an async context.
1620 */
1621 if (kiocb->ki_flags & IOCB_HIPRI)
1622 return -EOPNOTSUPP;
1623 if (kiocb->ki_flags & IOCB_NOWAIT)
1624 return -EAGAIN;
1625
1626 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03001627 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06001628 ssize_t nr;
1629
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03001630 if (!iov_iter_is_bvec(iter)) {
1631 iovec = iov_iter_iovec(iter);
1632 } else {
1633 /* fixed buffers import bvec */
1634 iovec.iov_base = kmap(iter->bvec->bv_page)
1635 + iter->iov_offset;
1636 iovec.iov_len = min(iter->count,
1637 iter->bvec->bv_len - iter->iov_offset);
1638 }
1639
Jens Axboe32960612019-09-23 11:05:34 -06001640 if (rw == READ) {
1641 nr = file->f_op->read(file, iovec.iov_base,
1642 iovec.iov_len, &kiocb->ki_pos);
1643 } else {
1644 nr = file->f_op->write(file, iovec.iov_base,
1645 iovec.iov_len, &kiocb->ki_pos);
1646 }
1647
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03001648 if (iov_iter_is_bvec(iter))
1649 kunmap(iter->bvec->bv_page);
1650
Jens Axboe32960612019-09-23 11:05:34 -06001651 if (nr < 0) {
1652 if (!ret)
1653 ret = nr;
1654 break;
1655 }
1656 ret += nr;
1657 if (nr != iovec.iov_len)
1658 break;
1659 iov_iter_advance(iter, nr);
1660 }
1661
1662 return ret;
1663}
1664
Pavel Begunkov267bc902019-11-07 01:41:08 +03001665static int io_read(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001666 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001667{
1668 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1669 struct kiocb *kiocb = &req->rw;
1670 struct iov_iter iter;
1671 struct file *file;
Jens Axboe31b51512019-01-18 22:56:34 -07001672 size_t iov_count;
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001673 ssize_t read_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001674
Pavel Begunkov267bc902019-11-07 01:41:08 +03001675 ret = io_prep_rw(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001676 if (ret)
1677 return ret;
1678 file = kiocb->ki_filp;
1679
Jens Axboe2b188cc2019-01-07 10:46:33 -07001680 if (unlikely(!(file->f_mode & FMODE_READ)))
Jens Axboe09bb8392019-03-13 12:39:28 -06001681 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001682
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001683 ret = io_import_iovec(READ, req, &iovec, &iter);
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001684 if (ret < 0)
Jens Axboe09bb8392019-03-13 12:39:28 -06001685 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001686
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001687 read_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06001688 if (req->flags & REQ_F_LINK)
1689 req->result = read_size;
1690
Jens Axboe31b51512019-01-18 22:56:34 -07001691 iov_count = iov_iter_count(&iter);
1692 ret = rw_verify_area(READ, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001693 if (!ret) {
1694 ssize_t ret2;
1695
Jens Axboe32960612019-09-23 11:05:34 -06001696 if (file->f_op->read_iter)
1697 ret2 = call_read_iter(file, kiocb, &iter);
1698 else
1699 ret2 = loop_rw_iter(READ, file, kiocb, &iter);
1700
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001701 /*
1702 * In case of a short read, punt to async. This can happen
1703 * if we have data partially cached. Alternatively we can
1704 * return the short read, in which case the application will
1705 * need to issue another SQE and wait for it. That SQE will
1706 * need async punt anyway, so it's more efficient to do it
1707 * here.
1708 */
Jens Axboe491381ce2019-10-17 09:20:46 -06001709 if (force_nonblock && !(req->flags & REQ_F_NOWAIT) &&
1710 (req->flags & REQ_F_ISREG) &&
1711 ret2 > 0 && ret2 < read_size)
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001712 ret2 = -EAGAIN;
1713 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboe561fb042019-10-24 07:25:42 -06001714 if (!force_nonblock || ret2 != -EAGAIN)
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001715 kiocb_done(kiocb, ret2, nxt, req->in_async);
Jens Axboe561fb042019-10-24 07:25:42 -06001716 else
Jens Axboe2b188cc2019-01-07 10:46:33 -07001717 ret = -EAGAIN;
1718 }
1719 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001720 return ret;
1721}
1722
Pavel Begunkov267bc902019-11-07 01:41:08 +03001723static int io_write(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001724 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001725{
1726 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1727 struct kiocb *kiocb = &req->rw;
1728 struct iov_iter iter;
1729 struct file *file;
Jens Axboe31b51512019-01-18 22:56:34 -07001730 size_t iov_count;
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001731 ssize_t ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001732
Pavel Begunkov267bc902019-11-07 01:41:08 +03001733 ret = io_prep_rw(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001734 if (ret)
1735 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001736
Jens Axboe2b188cc2019-01-07 10:46:33 -07001737 file = kiocb->ki_filp;
1738 if (unlikely(!(file->f_mode & FMODE_WRITE)))
Jens Axboe09bb8392019-03-13 12:39:28 -06001739 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001740
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001741 ret = io_import_iovec(WRITE, req, &iovec, &iter);
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001742 if (ret < 0)
Jens Axboe09bb8392019-03-13 12:39:28 -06001743 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001744
Jens Axboe9e645e112019-05-10 16:07:28 -06001745 if (req->flags & REQ_F_LINK)
1746 req->result = ret;
1747
Jens Axboe31b51512019-01-18 22:56:34 -07001748 iov_count = iov_iter_count(&iter);
1749
1750 ret = -EAGAIN;
Jens Axboe561fb042019-10-24 07:25:42 -06001751 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT))
Jens Axboe31b51512019-01-18 22:56:34 -07001752 goto out_free;
Jens Axboe31b51512019-01-18 22:56:34 -07001753
1754 ret = rw_verify_area(WRITE, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001755 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01001756 ssize_t ret2;
1757
Jens Axboe2b188cc2019-01-07 10:46:33 -07001758 /*
1759 * Open-code file_start_write here to grab freeze protection,
1760 * which will be released by another thread in
1761 * io_complete_rw(). Fool lockdep by telling it the lock got
1762 * released so that it doesn't complain about the held lock when
1763 * we return to userspace.
1764 */
Jens Axboe491381ce2019-10-17 09:20:46 -06001765 if (req->flags & REQ_F_ISREG) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001766 __sb_start_write(file_inode(file)->i_sb,
1767 SB_FREEZE_WRITE, true);
1768 __sb_writers_release(file_inode(file)->i_sb,
1769 SB_FREEZE_WRITE);
1770 }
1771 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01001772
Jens Axboe32960612019-09-23 11:05:34 -06001773 if (file->f_op->write_iter)
1774 ret2 = call_write_iter(file, kiocb, &iter);
1775 else
1776 ret2 = loop_rw_iter(WRITE, file, kiocb, &iter);
Jens Axboe561fb042019-10-24 07:25:42 -06001777 if (!force_nonblock || ret2 != -EAGAIN)
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001778 kiocb_done(kiocb, ret2, nxt, req->in_async);
Jens Axboe561fb042019-10-24 07:25:42 -06001779 else
Roman Penyaev9bf79332019-03-25 20:09:24 +01001780 ret = -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001781 }
Jens Axboe31b51512019-01-18 22:56:34 -07001782out_free:
Jens Axboe2b188cc2019-01-07 10:46:33 -07001783 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001784 return ret;
1785}
1786
1787/*
1788 * IORING_OP_NOP just posts a completion event, nothing else.
1789 */
Jens Axboe78e19bb2019-11-06 15:21:34 -07001790static int io_nop(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001791{
1792 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001793
Jens Axboedef596e2019-01-09 08:59:42 -07001794 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1795 return -EINVAL;
1796
Jens Axboe78e19bb2019-11-06 15:21:34 -07001797 io_cqring_add_event(req, 0);
Jens Axboee65ef562019-03-12 10:16:44 -06001798 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001799 return 0;
1800}
1801
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001802static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1803{
Jens Axboe6b063142019-01-10 22:13:58 -07001804 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001805
Jens Axboe09bb8392019-03-13 12:39:28 -06001806 if (!req->file)
1807 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001808
Jens Axboe6b063142019-01-10 22:13:58 -07001809 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07001810 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07001811 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001812 return -EINVAL;
1813
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001814 return 0;
1815}
1816
1817static int io_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001818 struct io_kiocb **nxt, bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001819{
1820 loff_t sqe_off = READ_ONCE(sqe->off);
1821 loff_t sqe_len = READ_ONCE(sqe->len);
1822 loff_t end = sqe_off + sqe_len;
1823 unsigned fsync_flags;
1824 int ret;
1825
1826 fsync_flags = READ_ONCE(sqe->fsync_flags);
1827 if (unlikely(fsync_flags & ~IORING_FSYNC_DATASYNC))
1828 return -EINVAL;
1829
1830 ret = io_prep_fsync(req, sqe);
1831 if (ret)
1832 return ret;
1833
1834 /* fsync always requires a blocking context */
1835 if (force_nonblock)
1836 return -EAGAIN;
1837
1838 ret = vfs_fsync_range(req->rw.ki_filp, sqe_off,
1839 end > 0 ? end : LLONG_MAX,
1840 fsync_flags & IORING_FSYNC_DATASYNC);
1841
Jens Axboe9e645e112019-05-10 16:07:28 -06001842 if (ret < 0 && (req->flags & REQ_F_LINK))
1843 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001844 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001845 io_put_req_find_next(req, nxt);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001846 return 0;
1847}
1848
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001849static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1850{
1851 struct io_ring_ctx *ctx = req->ctx;
1852 int ret = 0;
1853
1854 if (!req->file)
1855 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001856
1857 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1858 return -EINVAL;
1859 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
1860 return -EINVAL;
1861
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001862 return ret;
1863}
1864
1865static int io_sync_file_range(struct io_kiocb *req,
1866 const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001867 struct io_kiocb **nxt,
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001868 bool force_nonblock)
1869{
1870 loff_t sqe_off;
1871 loff_t sqe_len;
1872 unsigned flags;
1873 int ret;
1874
1875 ret = io_prep_sfr(req, sqe);
1876 if (ret)
1877 return ret;
1878
1879 /* sync_file_range always requires a blocking context */
1880 if (force_nonblock)
1881 return -EAGAIN;
1882
1883 sqe_off = READ_ONCE(sqe->off);
1884 sqe_len = READ_ONCE(sqe->len);
1885 flags = READ_ONCE(sqe->sync_range_flags);
1886
1887 ret = sync_file_range(req->rw.ki_filp, sqe_off, sqe_len, flags);
1888
Jens Axboe9e645e112019-05-10 16:07:28 -06001889 if (ret < 0 && (req->flags & REQ_F_LINK))
1890 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001891 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001892 io_put_req_find_next(req, nxt);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001893 return 0;
1894}
1895
Jens Axboe0fa03c62019-04-19 13:34:07 -06001896#if defined(CONFIG_NET)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001897static int io_send_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001898 struct io_kiocb **nxt, bool force_nonblock,
Jens Axboeaa1fa282019-04-19 13:38:09 -06001899 long (*fn)(struct socket *, struct user_msghdr __user *,
1900 unsigned int))
1901{
Jens Axboe0fa03c62019-04-19 13:34:07 -06001902 struct socket *sock;
1903 int ret;
1904
1905 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1906 return -EINVAL;
1907
1908 sock = sock_from_file(req->file, &ret);
1909 if (sock) {
1910 struct user_msghdr __user *msg;
1911 unsigned flags;
1912
1913 flags = READ_ONCE(sqe->msg_flags);
1914 if (flags & MSG_DONTWAIT)
1915 req->flags |= REQ_F_NOWAIT;
1916 else if (force_nonblock)
1917 flags |= MSG_DONTWAIT;
1918
1919 msg = (struct user_msghdr __user *) (unsigned long)
1920 READ_ONCE(sqe->addr);
1921
Jens Axboeaa1fa282019-04-19 13:38:09 -06001922 ret = fn(sock, msg, flags);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001923 if (force_nonblock && ret == -EAGAIN)
1924 return ret;
Jens Axboe441cdbd2019-12-02 18:49:10 -07001925 if (ret == -ERESTARTSYS)
1926 ret = -EINTR;
Jens Axboe0fa03c62019-04-19 13:34:07 -06001927 }
1928
Jens Axboe78e19bb2019-11-06 15:21:34 -07001929 io_cqring_add_event(req, ret);
Jens Axboef1f40852019-11-05 20:33:16 -07001930 if (ret < 0 && (req->flags & REQ_F_LINK))
1931 req->flags |= REQ_F_FAIL_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001932 io_put_req_find_next(req, nxt);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001933 return 0;
Jens Axboeaa1fa282019-04-19 13:38:09 -06001934}
1935#endif
1936
1937static int io_sendmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001938 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001939{
1940#if defined(CONFIG_NET)
Jens Axboeba816ad2019-09-28 11:36:45 -06001941 return io_send_recvmsg(req, sqe, nxt, force_nonblock,
1942 __sys_sendmsg_sock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06001943#else
1944 return -EOPNOTSUPP;
1945#endif
1946}
1947
1948static int io_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001949 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001950{
1951#if defined(CONFIG_NET)
Jens Axboeba816ad2019-09-28 11:36:45 -06001952 return io_send_recvmsg(req, sqe, nxt, force_nonblock,
1953 __sys_recvmsg_sock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001954#else
1955 return -EOPNOTSUPP;
1956#endif
1957}
1958
Jens Axboe17f2fe32019-10-17 14:42:58 -06001959static int io_accept(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1960 struct io_kiocb **nxt, bool force_nonblock)
1961{
1962#if defined(CONFIG_NET)
1963 struct sockaddr __user *addr;
1964 int __user *addr_len;
1965 unsigned file_flags;
1966 int flags, ret;
1967
1968 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
1969 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05001970 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06001971 return -EINVAL;
1972
1973 addr = (struct sockaddr __user *) (unsigned long) READ_ONCE(sqe->addr);
1974 addr_len = (int __user *) (unsigned long) READ_ONCE(sqe->addr2);
1975 flags = READ_ONCE(sqe->accept_flags);
1976 file_flags = force_nonblock ? O_NONBLOCK : 0;
1977
1978 ret = __sys_accept4_file(req->file, file_flags, addr, addr_len, flags);
1979 if (ret == -EAGAIN && force_nonblock) {
1980 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
1981 return -EAGAIN;
1982 }
Jens Axboe8e3cca12019-11-09 19:52:33 -07001983 if (ret == -ERESTARTSYS)
1984 ret = -EINTR;
Jens Axboe17f2fe32019-10-17 14:42:58 -06001985 if (ret < 0 && (req->flags & REQ_F_LINK))
1986 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001987 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001988 io_put_req_find_next(req, nxt);
Jens Axboe17f2fe32019-10-17 14:42:58 -06001989 return 0;
1990#else
1991 return -EOPNOTSUPP;
1992#endif
1993}
1994
Jens Axboef8e85cf2019-11-23 14:24:24 -07001995static int io_connect(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1996 struct io_kiocb **nxt, bool force_nonblock)
1997{
1998#if defined(CONFIG_NET)
1999 struct sockaddr __user *addr;
2000 unsigned file_flags;
2001 int addr_len, ret;
2002
2003 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
2004 return -EINVAL;
2005 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
2006 return -EINVAL;
2007
2008 addr = (struct sockaddr __user *) (unsigned long) READ_ONCE(sqe->addr);
2009 addr_len = READ_ONCE(sqe->addr2);
2010 file_flags = force_nonblock ? O_NONBLOCK : 0;
2011
2012 ret = __sys_connect_file(req->file, addr, addr_len, file_flags);
2013 if (ret == -EAGAIN && force_nonblock)
2014 return -EAGAIN;
2015 if (ret == -ERESTARTSYS)
2016 ret = -EINTR;
2017 if (ret < 0 && (req->flags & REQ_F_LINK))
2018 req->flags |= REQ_F_FAIL_LINK;
2019 io_cqring_add_event(req, ret);
2020 io_put_req_find_next(req, nxt);
2021 return 0;
2022#else
2023 return -EOPNOTSUPP;
2024#endif
2025}
2026
Jens Axboeeac406c2019-11-14 12:09:58 -07002027static inline void io_poll_remove_req(struct io_kiocb *req)
2028{
2029 if (!RB_EMPTY_NODE(&req->rb_node)) {
2030 rb_erase(&req->rb_node, &req->ctx->cancel_tree);
2031 RB_CLEAR_NODE(&req->rb_node);
2032 }
2033}
2034
Jens Axboe221c5eb2019-01-17 09:41:58 -07002035static void io_poll_remove_one(struct io_kiocb *req)
2036{
2037 struct io_poll_iocb *poll = &req->poll;
2038
2039 spin_lock(&poll->head->lock);
2040 WRITE_ONCE(poll->canceled, true);
Jens Axboee9444752019-11-26 15:02:04 -07002041 if (!list_empty(&poll->wait->entry)) {
2042 list_del_init(&poll->wait->entry);
Jackie Liua197f662019-11-08 08:09:12 -07002043 io_queue_async_work(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002044 }
2045 spin_unlock(&poll->head->lock);
Jens Axboeeac406c2019-11-14 12:09:58 -07002046 io_poll_remove_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002047}
2048
2049static void io_poll_remove_all(struct io_ring_ctx *ctx)
2050{
Jens Axboeeac406c2019-11-14 12:09:58 -07002051 struct rb_node *node;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002052 struct io_kiocb *req;
2053
2054 spin_lock_irq(&ctx->completion_lock);
Jens Axboeeac406c2019-11-14 12:09:58 -07002055 while ((node = rb_first(&ctx->cancel_tree)) != NULL) {
2056 req = rb_entry(node, struct io_kiocb, rb_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002057 io_poll_remove_one(req);
2058 }
2059 spin_unlock_irq(&ctx->completion_lock);
2060}
2061
Jens Axboe47f46762019-11-09 17:43:02 -07002062static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
2063{
Jens Axboeeac406c2019-11-14 12:09:58 -07002064 struct rb_node *p, *parent = NULL;
Jens Axboe47f46762019-11-09 17:43:02 -07002065 struct io_kiocb *req;
2066
Jens Axboeeac406c2019-11-14 12:09:58 -07002067 p = ctx->cancel_tree.rb_node;
2068 while (p) {
2069 parent = p;
2070 req = rb_entry(parent, struct io_kiocb, rb_node);
2071 if (sqe_addr < req->user_data) {
2072 p = p->rb_left;
2073 } else if (sqe_addr > req->user_data) {
2074 p = p->rb_right;
2075 } else {
2076 io_poll_remove_one(req);
2077 return 0;
2078 }
Jens Axboe47f46762019-11-09 17:43:02 -07002079 }
2080
2081 return -ENOENT;
2082}
2083
Jens Axboe221c5eb2019-01-17 09:41:58 -07002084/*
2085 * Find a running poll command that matches one specified in sqe->addr,
2086 * and remove it if found.
2087 */
2088static int io_poll_remove(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2089{
2090 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07002091 int ret;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002092
2093 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2094 return -EINVAL;
2095 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
2096 sqe->poll_events)
2097 return -EINVAL;
2098
2099 spin_lock_irq(&ctx->completion_lock);
Jens Axboe47f46762019-11-09 17:43:02 -07002100 ret = io_poll_cancel(ctx, READ_ONCE(sqe->addr));
Jens Axboe221c5eb2019-01-17 09:41:58 -07002101 spin_unlock_irq(&ctx->completion_lock);
2102
Jens Axboe78e19bb2019-11-06 15:21:34 -07002103 io_cqring_add_event(req, ret);
Jens Axboef1f40852019-11-05 20:33:16 -07002104 if (ret < 0 && (req->flags & REQ_F_LINK))
2105 req->flags |= REQ_F_FAIL_LINK;
Jens Axboee65ef562019-03-12 10:16:44 -06002106 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002107 return 0;
2108}
2109
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002110static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002111{
Jackie Liua197f662019-11-08 08:09:12 -07002112 struct io_ring_ctx *ctx = req->ctx;
2113
Jens Axboe8c838782019-03-12 15:48:16 -06002114 req->poll.done = true;
Jens Axboee9444752019-11-26 15:02:04 -07002115 kfree(req->poll.wait);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002116 if (error)
2117 io_cqring_fill_event(req, error);
2118 else
2119 io_cqring_fill_event(req, mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06002120 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002121}
2122
Jens Axboe561fb042019-10-24 07:25:42 -06002123static void io_poll_complete_work(struct io_wq_work **workptr)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002124{
Jens Axboe561fb042019-10-24 07:25:42 -06002125 struct io_wq_work *work = *workptr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002126 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
2127 struct io_poll_iocb *poll = &req->poll;
2128 struct poll_table_struct pt = { ._key = poll->events };
2129 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe89723d02019-11-05 15:32:58 -07002130 struct io_kiocb *nxt = NULL;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002131 __poll_t mask = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002132 int ret = 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002133
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002134 if (work->flags & IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06002135 WRITE_ONCE(poll->canceled, true);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002136 ret = -ECANCELED;
2137 } else if (READ_ONCE(poll->canceled)) {
2138 ret = -ECANCELED;
2139 }
Jens Axboe561fb042019-10-24 07:25:42 -06002140
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002141 if (ret != -ECANCELED)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002142 mask = vfs_poll(poll->file, &pt) & poll->events;
2143
2144 /*
2145 * Note that ->ki_cancel callers also delete iocb from active_reqs after
2146 * calling ->ki_cancel. We need the ctx_lock roundtrip here to
2147 * synchronize with them. In the cancellation case the list_del_init
2148 * itself is not actually needed, but harmless so we keep it in to
2149 * avoid further branches in the fast path.
2150 */
2151 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002152 if (!mask && ret != -ECANCELED) {
Jens Axboee9444752019-11-26 15:02:04 -07002153 add_wait_queue(poll->head, poll->wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002154 spin_unlock_irq(&ctx->completion_lock);
2155 return;
2156 }
Jens Axboeeac406c2019-11-14 12:09:58 -07002157 io_poll_remove_req(req);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002158 io_poll_complete(req, mask, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002159 spin_unlock_irq(&ctx->completion_lock);
2160
Jens Axboe8c838782019-03-12 15:48:16 -06002161 io_cqring_ev_posted(ctx);
Jens Axboe89723d02019-11-05 15:32:58 -07002162
Jens Axboefba38c22019-11-18 12:27:57 -07002163 if (ret < 0 && req->flags & REQ_F_LINK)
2164 req->flags |= REQ_F_FAIL_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08002165 io_put_req_find_next(req, &nxt);
Jens Axboe89723d02019-11-05 15:32:58 -07002166 if (nxt)
2167 *workptr = &nxt->work;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002168}
2169
2170static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
2171 void *key)
2172{
Jens Axboee9444752019-11-26 15:02:04 -07002173 struct io_poll_iocb *poll = wait->private;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002174 struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
2175 struct io_ring_ctx *ctx = req->ctx;
2176 __poll_t mask = key_to_poll(key);
Jens Axboe8c838782019-03-12 15:48:16 -06002177 unsigned long flags;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002178
2179 /* for instances that support it check for an event match first: */
Jens Axboe8c838782019-03-12 15:48:16 -06002180 if (mask && !(mask & poll->events))
2181 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002182
Jens Axboee9444752019-11-26 15:02:04 -07002183 list_del_init(&poll->wait->entry);
Jens Axboe8c838782019-03-12 15:48:16 -06002184
Jens Axboe7c9e7f02019-11-12 08:15:53 -07002185 /*
2186 * Run completion inline if we can. We're using trylock here because
2187 * we are violating the completion_lock -> poll wq lock ordering.
2188 * If we have a link timeout we're going to need the completion_lock
2189 * for finalizing the request, mark us as having grabbed that already.
2190 */
Jens Axboe8c838782019-03-12 15:48:16 -06002191 if (mask && spin_trylock_irqsave(&ctx->completion_lock, flags)) {
Jens Axboeeac406c2019-11-14 12:09:58 -07002192 io_poll_remove_req(req);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002193 io_poll_complete(req, mask, 0);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07002194 req->flags |= REQ_F_COMP_LOCKED;
2195 io_put_req(req);
Jens Axboe8c838782019-03-12 15:48:16 -06002196 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2197
2198 io_cqring_ev_posted(ctx);
Jens Axboe8c838782019-03-12 15:48:16 -06002199 } else {
Jackie Liua197f662019-11-08 08:09:12 -07002200 io_queue_async_work(req);
Jens Axboe8c838782019-03-12 15:48:16 -06002201 }
2202
Jens Axboe221c5eb2019-01-17 09:41:58 -07002203 return 1;
2204}
2205
2206struct io_poll_table {
2207 struct poll_table_struct pt;
2208 struct io_kiocb *req;
2209 int error;
2210};
2211
2212static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
2213 struct poll_table_struct *p)
2214{
2215 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
2216
2217 if (unlikely(pt->req->poll.head)) {
2218 pt->error = -EINVAL;
2219 return;
2220 }
2221
2222 pt->error = 0;
2223 pt->req->poll.head = head;
Jens Axboee9444752019-11-26 15:02:04 -07002224 add_wait_queue(head, pt->req->poll.wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002225}
2226
Jens Axboeeac406c2019-11-14 12:09:58 -07002227static void io_poll_req_insert(struct io_kiocb *req)
2228{
2229 struct io_ring_ctx *ctx = req->ctx;
2230 struct rb_node **p = &ctx->cancel_tree.rb_node;
2231 struct rb_node *parent = NULL;
2232 struct io_kiocb *tmp;
2233
2234 while (*p) {
2235 parent = *p;
2236 tmp = rb_entry(parent, struct io_kiocb, rb_node);
2237 if (req->user_data < tmp->user_data)
2238 p = &(*p)->rb_left;
2239 else
2240 p = &(*p)->rb_right;
2241 }
2242 rb_link_node(&req->rb_node, parent, p);
2243 rb_insert_color(&req->rb_node, &ctx->cancel_tree);
2244}
2245
Jens Axboe89723d02019-11-05 15:32:58 -07002246static int io_poll_add(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2247 struct io_kiocb **nxt)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002248{
2249 struct io_poll_iocb *poll = &req->poll;
2250 struct io_ring_ctx *ctx = req->ctx;
2251 struct io_poll_table ipt;
Jens Axboe8c838782019-03-12 15:48:16 -06002252 bool cancel = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002253 __poll_t mask;
2254 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002255
2256 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2257 return -EINVAL;
2258 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
2259 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06002260 if (!poll->file)
2261 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002262
Jens Axboee9444752019-11-26 15:02:04 -07002263 poll->wait = kmalloc(sizeof(*poll->wait), GFP_KERNEL);
2264 if (!poll->wait)
2265 return -ENOMEM;
2266
Jens Axboe1a6b74f2019-12-02 10:33:15 -07002267 req->io = NULL;
Jens Axboe561fb042019-10-24 07:25:42 -06002268 INIT_IO_WORK(&req->work, io_poll_complete_work);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002269 events = READ_ONCE(sqe->poll_events);
2270 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
Jens Axboeeac406c2019-11-14 12:09:58 -07002271 RB_CLEAR_NODE(&req->rb_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002272
Jens Axboe221c5eb2019-01-17 09:41:58 -07002273 poll->head = NULL;
Jens Axboe8c838782019-03-12 15:48:16 -06002274 poll->done = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002275 poll->canceled = false;
2276
2277 ipt.pt._qproc = io_poll_queue_proc;
2278 ipt.pt._key = poll->events;
2279 ipt.req = req;
2280 ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
2281
2282 /* initialized the list so that we can do list_empty checks */
Jens Axboee9444752019-11-26 15:02:04 -07002283 INIT_LIST_HEAD(&poll->wait->entry);
2284 init_waitqueue_func_entry(poll->wait, io_poll_wake);
2285 poll->wait->private = poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002286
Jens Axboe36703242019-07-25 10:20:18 -06002287 INIT_LIST_HEAD(&req->list);
2288
Jens Axboe221c5eb2019-01-17 09:41:58 -07002289 mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002290
2291 spin_lock_irq(&ctx->completion_lock);
Jens Axboe8c838782019-03-12 15:48:16 -06002292 if (likely(poll->head)) {
2293 spin_lock(&poll->head->lock);
Jens Axboee9444752019-11-26 15:02:04 -07002294 if (unlikely(list_empty(&poll->wait->entry))) {
Jens Axboe8c838782019-03-12 15:48:16 -06002295 if (ipt.error)
2296 cancel = true;
2297 ipt.error = 0;
2298 mask = 0;
2299 }
2300 if (mask || ipt.error)
Jens Axboee9444752019-11-26 15:02:04 -07002301 list_del_init(&poll->wait->entry);
Jens Axboe8c838782019-03-12 15:48:16 -06002302 else if (cancel)
2303 WRITE_ONCE(poll->canceled, true);
2304 else if (!poll->done) /* actually waiting for an event */
Jens Axboeeac406c2019-11-14 12:09:58 -07002305 io_poll_req_insert(req);
Jens Axboe8c838782019-03-12 15:48:16 -06002306 spin_unlock(&poll->head->lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002307 }
Jens Axboe8c838782019-03-12 15:48:16 -06002308 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06002309 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002310 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06002311 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07002312 spin_unlock_irq(&ctx->completion_lock);
2313
Jens Axboe8c838782019-03-12 15:48:16 -06002314 if (mask) {
2315 io_cqring_ev_posted(ctx);
Jackie Liuec9c02a2019-11-08 23:50:36 +08002316 io_put_req_find_next(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002317 }
Jens Axboe8c838782019-03-12 15:48:16 -06002318 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002319}
2320
Jens Axboe5262f562019-09-17 12:26:57 -06002321static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
2322{
Jens Axboead8a48a2019-11-15 08:49:11 -07002323 struct io_timeout_data *data = container_of(timer,
2324 struct io_timeout_data, timer);
2325 struct io_kiocb *req = data->req;
2326 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06002327 unsigned long flags;
2328
Jens Axboe5262f562019-09-17 12:26:57 -06002329 atomic_inc(&ctx->cq_timeouts);
2330
2331 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08002332 /*
Jens Axboe11365042019-10-16 09:08:32 -06002333 * We could be racing with timeout deletion. If the list is empty,
2334 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08002335 */
Jens Axboe842f9612019-10-29 12:34:10 -06002336 if (!list_empty(&req->list)) {
Jens Axboe11365042019-10-16 09:08:32 -06002337 struct io_kiocb *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06002338
Jens Axboe11365042019-10-16 09:08:32 -06002339 /*
2340 * Adjust the reqs sequence before the current one because it
2341 * will consume a slot in the cq_ring and the the cq_tail
2342 * pointer will be increased, otherwise other timeout reqs may
2343 * return in advance without waiting for enough wait_nr.
2344 */
2345 prev = req;
2346 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
2347 prev->sequence++;
Jens Axboe11365042019-10-16 09:08:32 -06002348 list_del_init(&req->list);
Jens Axboe11365042019-10-16 09:08:32 -06002349 }
Jens Axboe842f9612019-10-29 12:34:10 -06002350
Jens Axboe78e19bb2019-11-06 15:21:34 -07002351 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06002352 io_commit_cqring(ctx);
2353 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2354
2355 io_cqring_ev_posted(ctx);
Jens Axboef1f40852019-11-05 20:33:16 -07002356 if (req->flags & REQ_F_LINK)
2357 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe5262f562019-09-17 12:26:57 -06002358 io_put_req(req);
2359 return HRTIMER_NORESTART;
2360}
2361
Jens Axboe47f46762019-11-09 17:43:02 -07002362static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
2363{
2364 struct io_kiocb *req;
2365 int ret = -ENOENT;
2366
2367 list_for_each_entry(req, &ctx->timeout_list, list) {
2368 if (user_data == req->user_data) {
2369 list_del_init(&req->list);
2370 ret = 0;
2371 break;
2372 }
2373 }
2374
2375 if (ret == -ENOENT)
2376 return ret;
2377
Jens Axboead8a48a2019-11-15 08:49:11 -07002378 ret = hrtimer_try_to_cancel(&req->timeout.data->timer);
Jens Axboe47f46762019-11-09 17:43:02 -07002379 if (ret == -1)
2380 return -EALREADY;
2381
Jens Axboefba38c22019-11-18 12:27:57 -07002382 if (req->flags & REQ_F_LINK)
2383 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe47f46762019-11-09 17:43:02 -07002384 io_cqring_fill_event(req, -ECANCELED);
2385 io_put_req(req);
2386 return 0;
2387}
2388
Jens Axboe11365042019-10-16 09:08:32 -06002389/*
2390 * Remove or update an existing timeout command
2391 */
2392static int io_timeout_remove(struct io_kiocb *req,
2393 const struct io_uring_sqe *sqe)
2394{
2395 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe11365042019-10-16 09:08:32 -06002396 unsigned flags;
Jens Axboe47f46762019-11-09 17:43:02 -07002397 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06002398
2399 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2400 return -EINVAL;
2401 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
2402 return -EINVAL;
2403 flags = READ_ONCE(sqe->timeout_flags);
2404 if (flags)
2405 return -EINVAL;
2406
Jens Axboe11365042019-10-16 09:08:32 -06002407 spin_lock_irq(&ctx->completion_lock);
Jens Axboe47f46762019-11-09 17:43:02 -07002408 ret = io_timeout_cancel(ctx, READ_ONCE(sqe->addr));
Jens Axboe11365042019-10-16 09:08:32 -06002409
Jens Axboe47f46762019-11-09 17:43:02 -07002410 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06002411 io_commit_cqring(ctx);
2412 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06002413 io_cqring_ev_posted(ctx);
Jens Axboe47f46762019-11-09 17:43:02 -07002414 if (ret < 0 && req->flags & REQ_F_LINK)
2415 req->flags |= REQ_F_FAIL_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08002416 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06002417 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06002418}
2419
Jens Axboead8a48a2019-11-15 08:49:11 -07002420static int io_timeout_setup(struct io_kiocb *req)
Jens Axboe5262f562019-09-17 12:26:57 -06002421{
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002422 const struct io_uring_sqe *sqe = req->sqe;
Jens Axboead8a48a2019-11-15 08:49:11 -07002423 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06002424 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06002425
Jens Axboead8a48a2019-11-15 08:49:11 -07002426 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06002427 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07002428 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06002429 return -EINVAL;
2430 flags = READ_ONCE(sqe->timeout_flags);
2431 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06002432 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06002433
Jens Axboead8a48a2019-11-15 08:49:11 -07002434 data = kzalloc(sizeof(struct io_timeout_data), GFP_KERNEL);
2435 if (!data)
2436 return -ENOMEM;
2437 data->req = req;
2438 req->timeout.data = data;
2439 req->flags |= REQ_F_TIMEOUT;
2440
2441 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06002442 return -EFAULT;
2443
Jens Axboe11365042019-10-16 09:08:32 -06002444 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07002445 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06002446 else
Jens Axboead8a48a2019-11-15 08:49:11 -07002447 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06002448
Jens Axboead8a48a2019-11-15 08:49:11 -07002449 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
2450 return 0;
2451}
2452
2453static int io_timeout(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2454{
2455 unsigned count;
2456 struct io_ring_ctx *ctx = req->ctx;
2457 struct io_timeout_data *data;
2458 struct list_head *entry;
2459 unsigned span = 0;
2460 int ret;
2461
2462 ret = io_timeout_setup(req);
2463 /* common setup allows flags (like links) set, we don't */
2464 if (!ret && sqe->flags)
2465 ret = -EINVAL;
2466 if (ret)
2467 return ret;
Jens Axboe93bd25b2019-11-11 23:34:31 -07002468
Jens Axboe5262f562019-09-17 12:26:57 -06002469 /*
2470 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07002471 * timeout event to be satisfied. If it isn't set, then this is
2472 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06002473 */
2474 count = READ_ONCE(sqe->off);
Jens Axboe93bd25b2019-11-11 23:34:31 -07002475 if (!count) {
2476 req->flags |= REQ_F_TIMEOUT_NOSEQ;
2477 spin_lock_irq(&ctx->completion_lock);
2478 entry = ctx->timeout_list.prev;
2479 goto add;
2480 }
Jens Axboe5262f562019-09-17 12:26:57 -06002481
2482 req->sequence = ctx->cached_sq_head + count - 1;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +03002483 req->timeout.data->seq_offset = count;
Jens Axboe5262f562019-09-17 12:26:57 -06002484
2485 /*
2486 * Insertion sort, ensuring the first entry in the list is always
2487 * the one we need first.
2488 */
Jens Axboe5262f562019-09-17 12:26:57 -06002489 spin_lock_irq(&ctx->completion_lock);
2490 list_for_each_prev(entry, &ctx->timeout_list) {
2491 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
yangerkun5da0fb12019-10-15 21:59:29 +08002492 unsigned nxt_sq_head;
2493 long long tmp, tmp_nxt;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +03002494 u32 nxt_offset = nxt->timeout.data->seq_offset;
Jens Axboe5262f562019-09-17 12:26:57 -06002495
Jens Axboe93bd25b2019-11-11 23:34:31 -07002496 if (nxt->flags & REQ_F_TIMEOUT_NOSEQ)
2497 continue;
2498
yangerkun5da0fb12019-10-15 21:59:29 +08002499 /*
2500 * Since cached_sq_head + count - 1 can overflow, use type long
2501 * long to store it.
2502 */
2503 tmp = (long long)ctx->cached_sq_head + count - 1;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +03002504 nxt_sq_head = nxt->sequence - nxt_offset + 1;
2505 tmp_nxt = (long long)nxt_sq_head + nxt_offset - 1;
yangerkun5da0fb12019-10-15 21:59:29 +08002506
2507 /*
2508 * cached_sq_head may overflow, and it will never overflow twice
2509 * once there is some timeout req still be valid.
2510 */
2511 if (ctx->cached_sq_head < nxt_sq_head)
yangerkun8b07a652019-10-17 12:12:35 +08002512 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08002513
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002514 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06002515 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002516
2517 /*
2518 * Sequence of reqs after the insert one and itself should
2519 * be adjusted because each timeout req consumes a slot.
2520 */
2521 span++;
2522 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06002523 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002524 req->sequence -= span;
Jens Axboe93bd25b2019-11-11 23:34:31 -07002525add:
Jens Axboe5262f562019-09-17 12:26:57 -06002526 list_add(&req->list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07002527 data = req->timeout.data;
2528 data->timer.function = io_timeout_fn;
2529 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06002530 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06002531 return 0;
2532}
2533
Jens Axboe62755e32019-10-28 21:49:21 -06002534static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06002535{
Jens Axboe62755e32019-10-28 21:49:21 -06002536 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06002537
Jens Axboe62755e32019-10-28 21:49:21 -06002538 return req->user_data == (unsigned long) data;
2539}
2540
Jens Axboee977d6d2019-11-05 12:39:45 -07002541static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06002542{
Jens Axboe62755e32019-10-28 21:49:21 -06002543 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06002544 int ret = 0;
2545
Jens Axboe62755e32019-10-28 21:49:21 -06002546 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
2547 switch (cancel_ret) {
2548 case IO_WQ_CANCEL_OK:
2549 ret = 0;
2550 break;
2551 case IO_WQ_CANCEL_RUNNING:
2552 ret = -EALREADY;
2553 break;
2554 case IO_WQ_CANCEL_NOTFOUND:
2555 ret = -ENOENT;
2556 break;
2557 }
2558
Jens Axboee977d6d2019-11-05 12:39:45 -07002559 return ret;
2560}
2561
Jens Axboe47f46762019-11-09 17:43:02 -07002562static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
2563 struct io_kiocb *req, __u64 sqe_addr,
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002564 struct io_kiocb **nxt, int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07002565{
2566 unsigned long flags;
2567 int ret;
2568
2569 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
2570 if (ret != -ENOENT) {
2571 spin_lock_irqsave(&ctx->completion_lock, flags);
2572 goto done;
2573 }
2574
2575 spin_lock_irqsave(&ctx->completion_lock, flags);
2576 ret = io_timeout_cancel(ctx, sqe_addr);
2577 if (ret != -ENOENT)
2578 goto done;
2579 ret = io_poll_cancel(ctx, sqe_addr);
2580done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002581 if (!ret)
2582 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07002583 io_cqring_fill_event(req, ret);
2584 io_commit_cqring(ctx);
2585 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2586 io_cqring_ev_posted(ctx);
2587
2588 if (ret < 0 && (req->flags & REQ_F_LINK))
2589 req->flags |= REQ_F_FAIL_LINK;
2590 io_put_req_find_next(req, nxt);
2591}
2592
Jens Axboee977d6d2019-11-05 12:39:45 -07002593static int io_async_cancel(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2594 struct io_kiocb **nxt)
2595{
2596 struct io_ring_ctx *ctx = req->ctx;
Jens Axboee977d6d2019-11-05 12:39:45 -07002597
2598 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2599 return -EINVAL;
2600 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
2601 sqe->cancel_flags)
2602 return -EINVAL;
2603
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002604 io_async_find_and_cancel(ctx, req, READ_ONCE(sqe->addr), nxt, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06002605 return 0;
2606}
2607
Jackie Liua197f662019-11-08 08:09:12 -07002608static int io_req_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -06002609{
Jackie Liua197f662019-11-08 08:09:12 -07002610 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe1a6b74f2019-12-02 10:33:15 -07002611 struct io_async_ctx *io;
Jens Axboede0617e2019-04-06 21:51:27 -06002612
Bob Liu9d858b22019-11-13 18:06:25 +08002613 /* Still need defer if there is pending req in defer list. */
2614 if (!req_need_defer(req) && list_empty(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06002615 return 0;
2616
Jens Axboe1a6b74f2019-12-02 10:33:15 -07002617 io = kmalloc(sizeof(*io), GFP_KERNEL);
2618 if (!io)
Jens Axboede0617e2019-04-06 21:51:27 -06002619 return -EAGAIN;
2620
2621 spin_lock_irq(&ctx->completion_lock);
Bob Liu9d858b22019-11-13 18:06:25 +08002622 if (!req_need_defer(req) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06002623 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe1a6b74f2019-12-02 10:33:15 -07002624 kfree(io);
Jens Axboede0617e2019-04-06 21:51:27 -06002625 return 0;
2626 }
2627
Jens Axboe1a6b74f2019-12-02 10:33:15 -07002628 memcpy(&io->sqe, req->sqe, sizeof(io->sqe));
2629 req->sqe = &io->sqe;
2630 req->io = io;
Jens Axboede0617e2019-04-06 21:51:27 -06002631
Jens Axboe915967f2019-11-21 09:01:20 -07002632 trace_io_uring_defer(ctx, req, req->user_data);
Jens Axboede0617e2019-04-06 21:51:27 -06002633 list_add_tail(&req->list, &ctx->defer_list);
2634 spin_unlock_irq(&ctx->completion_lock);
2635 return -EIOCBQUEUED;
2636}
2637
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03002638__attribute__((nonnull))
Pavel Begunkovd7324472019-11-21 21:24:36 +03002639static int io_issue_sqe(struct io_kiocb *req, struct io_kiocb **nxt,
2640 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002641{
Jens Axboee0c5c572019-03-12 10:18:47 -06002642 int ret, opcode;
Jackie Liua197f662019-11-08 08:09:12 -07002643 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002644
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002645 opcode = READ_ONCE(req->sqe->opcode);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002646 switch (opcode) {
2647 case IORING_OP_NOP:
Jens Axboe78e19bb2019-11-06 15:21:34 -07002648 ret = io_nop(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002649 break;
2650 case IORING_OP_READV:
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_read(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002654 break;
2655 case IORING_OP_WRITEV:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002656 if (unlikely(req->sqe->buf_index))
Jens Axboeedafcce2019-01-09 09:16:05 -07002657 return -EINVAL;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002658 ret = io_write(req, nxt, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07002659 break;
2660 case IORING_OP_READ_FIXED:
Pavel Begunkov267bc902019-11-07 01:41:08 +03002661 ret = io_read(req, nxt, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07002662 break;
2663 case IORING_OP_WRITE_FIXED:
Pavel Begunkov267bc902019-11-07 01:41:08 +03002664 ret = io_write(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002665 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002666 case IORING_OP_FSYNC:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002667 ret = io_fsync(req, req->sqe, nxt, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002668 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002669 case IORING_OP_POLL_ADD:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002670 ret = io_poll_add(req, req->sqe, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002671 break;
2672 case IORING_OP_POLL_REMOVE:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002673 ret = io_poll_remove(req, req->sqe);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002674 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002675 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002676 ret = io_sync_file_range(req, req->sqe, nxt, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002677 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002678 case IORING_OP_SENDMSG:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002679 ret = io_sendmsg(req, req->sqe, nxt, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06002680 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06002681 case IORING_OP_RECVMSG:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002682 ret = io_recvmsg(req, req->sqe, nxt, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06002683 break;
Jens Axboe5262f562019-09-17 12:26:57 -06002684 case IORING_OP_TIMEOUT:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002685 ret = io_timeout(req, req->sqe);
Jens Axboe5262f562019-09-17 12:26:57 -06002686 break;
Jens Axboe11365042019-10-16 09:08:32 -06002687 case IORING_OP_TIMEOUT_REMOVE:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002688 ret = io_timeout_remove(req, req->sqe);
Jens Axboe11365042019-10-16 09:08:32 -06002689 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06002690 case IORING_OP_ACCEPT:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002691 ret = io_accept(req, req->sqe, nxt, force_nonblock);
Jens Axboe17f2fe32019-10-17 14:42:58 -06002692 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07002693 case IORING_OP_CONNECT:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002694 ret = io_connect(req, req->sqe, nxt, force_nonblock);
Jens Axboef8e85cf2019-11-23 14:24:24 -07002695 break;
Jens Axboe62755e32019-10-28 21:49:21 -06002696 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002697 ret = io_async_cancel(req, req->sqe, nxt);
Jens Axboe62755e32019-10-28 21:49:21 -06002698 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002699 default:
2700 ret = -EINVAL;
2701 break;
2702 }
2703
Jens Axboedef596e2019-01-09 08:59:42 -07002704 if (ret)
2705 return ret;
2706
2707 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe9e645e112019-05-10 16:07:28 -06002708 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07002709 return -EAGAIN;
2710
2711 /* workqueue context doesn't hold uring_lock, grab it now */
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002712 if (req->in_async)
Jens Axboedef596e2019-01-09 08:59:42 -07002713 mutex_lock(&ctx->uring_lock);
2714 io_iopoll_req_issued(req);
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002715 if (req->in_async)
Jens Axboedef596e2019-01-09 08:59:42 -07002716 mutex_unlock(&ctx->uring_lock);
2717 }
2718
2719 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002720}
2721
Jens Axboeb76da702019-11-20 13:05:32 -07002722static void io_link_work_cb(struct io_wq_work **workptr)
2723{
2724 struct io_wq_work *work = *workptr;
2725 struct io_kiocb *link = work->data;
2726
2727 io_queue_linked_timeout(link);
2728 work->func = io_wq_submit_work;
2729}
2730
Jens Axboe561fb042019-10-24 07:25:42 -06002731static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboe31b51512019-01-18 22:56:34 -07002732{
Jens Axboe561fb042019-10-24 07:25:42 -06002733 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002734 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe561fb042019-10-24 07:25:42 -06002735 struct io_kiocb *nxt = NULL;
2736 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002737
Jens Axboe561fb042019-10-24 07:25:42 -06002738 /* Ensure we clear previously set non-block flag */
2739 req->rw.ki_flags &= ~IOCB_NOWAIT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002740
Jens Axboe561fb042019-10-24 07:25:42 -06002741 if (work->flags & IO_WQ_WORK_CANCEL)
2742 ret = -ECANCELED;
Jens Axboe31b51512019-01-18 22:56:34 -07002743
Jens Axboe561fb042019-10-24 07:25:42 -06002744 if (!ret) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002745 req->has_user = (work->flags & IO_WQ_WORK_HAS_MM) != 0;
2746 req->in_async = true;
Jens Axboe561fb042019-10-24 07:25:42 -06002747 do {
Pavel Begunkovd7324472019-11-21 21:24:36 +03002748 ret = io_issue_sqe(req, &nxt, false);
Jens Axboe561fb042019-10-24 07:25:42 -06002749 /*
2750 * We can get EAGAIN for polled IO even though we're
2751 * forcing a sync submission from here, since we can't
2752 * wait for request slots on the block side.
2753 */
2754 if (ret != -EAGAIN)
2755 break;
2756 cond_resched();
2757 } while (1);
2758 }
Jens Axboe31b51512019-01-18 22:56:34 -07002759
Jens Axboe561fb042019-10-24 07:25:42 -06002760 /* drop submission reference */
Jackie Liuec9c02a2019-11-08 23:50:36 +08002761 io_put_req(req);
Jens Axboe817869d2019-04-30 14:44:05 -06002762
Jens Axboe561fb042019-10-24 07:25:42 -06002763 if (ret) {
Jens Axboef1f40852019-11-05 20:33:16 -07002764 if (req->flags & REQ_F_LINK)
2765 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07002766 io_cqring_add_event(req, ret);
Jens Axboe817869d2019-04-30 14:44:05 -06002767 io_put_req(req);
Jens Axboeedafcce2019-01-09 09:16:05 -07002768 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002769
Jens Axboe561fb042019-10-24 07:25:42 -06002770 /* if a dependent link is ready, pass it back */
2771 if (!ret && nxt) {
Jens Axboe94ae5e72019-11-14 19:39:52 -07002772 struct io_kiocb *link;
2773
2774 io_prep_async_work(nxt, &link);
Jens Axboe561fb042019-10-24 07:25:42 -06002775 *workptr = &nxt->work;
Jens Axboeb76da702019-11-20 13:05:32 -07002776 if (link) {
2777 nxt->work.flags |= IO_WQ_WORK_CB;
2778 nxt->work.func = io_link_work_cb;
2779 nxt->work.data = link;
2780 }
Jens Axboeedafcce2019-01-09 09:16:05 -07002781 }
Jens Axboe31b51512019-01-18 22:56:34 -07002782}
Jens Axboe2b188cc2019-01-07 10:46:33 -07002783
Jens Axboe09bb8392019-03-13 12:39:28 -06002784static bool io_op_needs_file(const struct io_uring_sqe *sqe)
2785{
2786 int op = READ_ONCE(sqe->opcode);
2787
2788 switch (op) {
2789 case IORING_OP_NOP:
2790 case IORING_OP_POLL_REMOVE:
Pavel Begunkov5683e542019-11-14 00:59:19 +03002791 case IORING_OP_TIMEOUT:
Pavel Begunkova320e9f2019-11-14 00:11:01 +03002792 case IORING_OP_TIMEOUT_REMOVE:
2793 case IORING_OP_ASYNC_CANCEL:
2794 case IORING_OP_LINK_TIMEOUT:
Jens Axboe09bb8392019-03-13 12:39:28 -06002795 return false;
2796 default:
2797 return true;
2798 }
2799}
2800
Jens Axboe65e19f52019-10-26 07:20:21 -06002801static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
2802 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06002803{
Jens Axboe65e19f52019-10-26 07:20:21 -06002804 struct fixed_file_table *table;
2805
2806 table = &ctx->file_table[index >> IORING_FILE_TABLE_SHIFT];
2807 return table->files[index & IORING_FILE_TABLE_MASK];
2808}
2809
Jackie Liua197f662019-11-08 08:09:12 -07002810static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req)
Jens Axboe09bb8392019-03-13 12:39:28 -06002811{
Jackie Liua197f662019-11-08 08:09:12 -07002812 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe09bb8392019-03-13 12:39:28 -06002813 unsigned flags;
2814 int fd;
2815
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002816 flags = READ_ONCE(req->sqe->flags);
2817 fd = READ_ONCE(req->sqe->fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06002818
Jackie Liu4fe2c962019-09-09 20:50:40 +08002819 if (flags & IOSQE_IO_DRAIN)
Jens Axboede0617e2019-04-06 21:51:27 -06002820 req->flags |= REQ_F_IO_DRAIN;
Jens Axboede0617e2019-04-06 21:51:27 -06002821
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002822 if (!io_op_needs_file(req->sqe))
Jens Axboe09bb8392019-03-13 12:39:28 -06002823 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06002824
2825 if (flags & IOSQE_FIXED_FILE) {
Jens Axboe65e19f52019-10-26 07:20:21 -06002826 if (unlikely(!ctx->file_table ||
Jens Axboe09bb8392019-03-13 12:39:28 -06002827 (unsigned) fd >= ctx->nr_user_files))
2828 return -EBADF;
Jens Axboeb7620122019-10-26 07:22:55 -06002829 fd = array_index_nospec(fd, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06002830 req->file = io_file_from_index(ctx, fd);
2831 if (!req->file)
Jens Axboe08a45172019-10-03 08:11:03 -06002832 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06002833 req->flags |= REQ_F_FIXED_FILE;
2834 } else {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002835 if (req->needs_fixed_file)
Jens Axboe09bb8392019-03-13 12:39:28 -06002836 return -EBADF;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002837 trace_io_uring_file_get(ctx, fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06002838 req->file = io_file_get(state, fd);
2839 if (unlikely(!req->file))
2840 return -EBADF;
2841 }
2842
2843 return 0;
2844}
2845
Jackie Liua197f662019-11-08 08:09:12 -07002846static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002847{
Jens Axboefcb323c2019-10-24 12:39:47 -06002848 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07002849 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06002850
2851 rcu_read_lock();
2852 spin_lock_irq(&ctx->inflight_lock);
2853 /*
2854 * We use the f_ops->flush() handler to ensure that we can flush
2855 * out work accessing these files if the fd is closed. Check if
2856 * the fd has changed since we started down this path, and disallow
2857 * this operation if it has.
2858 */
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002859 if (fcheck(req->ring_fd) == req->ring_file) {
Jens Axboefcb323c2019-10-24 12:39:47 -06002860 list_add(&req->inflight_entry, &ctx->inflight_list);
2861 req->flags |= REQ_F_INFLIGHT;
2862 req->work.files = current->files;
2863 ret = 0;
2864 }
2865 spin_unlock_irq(&ctx->inflight_lock);
2866 rcu_read_unlock();
2867
2868 return ret;
2869}
2870
Jens Axboe2665abf2019-11-05 12:40:47 -07002871static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
2872{
Jens Axboead8a48a2019-11-15 08:49:11 -07002873 struct io_timeout_data *data = container_of(timer,
2874 struct io_timeout_data, timer);
2875 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07002876 struct io_ring_ctx *ctx = req->ctx;
2877 struct io_kiocb *prev = NULL;
2878 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07002879
2880 spin_lock_irqsave(&ctx->completion_lock, flags);
2881
2882 /*
2883 * We don't expect the list to be empty, that will only happen if we
2884 * race with the completion of the linked work.
2885 */
2886 if (!list_empty(&req->list)) {
2887 prev = list_entry(req->list.prev, struct io_kiocb, link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07002888 if (refcount_inc_not_zero(&prev->refs)) {
Jens Axboe76a46e02019-11-10 23:34:16 -07002889 list_del_init(&req->list);
Jens Axboe5d960722019-11-19 15:31:28 -07002890 prev->flags &= ~REQ_F_LINK_TIMEOUT;
2891 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07002892 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07002893 }
2894
2895 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2896
2897 if (prev) {
Jens Axboefba38c22019-11-18 12:27:57 -07002898 if (prev->flags & REQ_F_LINK)
2899 prev->flags |= REQ_F_FAIL_LINK;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002900 io_async_find_and_cancel(ctx, req, prev->user_data, NULL,
2901 -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07002902 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07002903 } else {
2904 io_cqring_add_event(req, -ETIME);
2905 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07002906 }
Jens Axboe2665abf2019-11-05 12:40:47 -07002907 return HRTIMER_NORESTART;
2908}
2909
Jens Axboead8a48a2019-11-15 08:49:11 -07002910static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07002911{
Jens Axboe76a46e02019-11-10 23:34:16 -07002912 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07002913
Jens Axboe76a46e02019-11-10 23:34:16 -07002914 /*
2915 * If the list is now empty, then our linked request finished before
2916 * we got a chance to setup the timer
2917 */
2918 spin_lock_irq(&ctx->completion_lock);
2919 if (!list_empty(&req->list)) {
Jens Axboe94ae5e72019-11-14 19:39:52 -07002920 struct io_timeout_data *data = req->timeout.data;
2921
Jens Axboead8a48a2019-11-15 08:49:11 -07002922 data->timer.function = io_link_timeout_fn;
2923 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
2924 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07002925 }
Jens Axboe76a46e02019-11-10 23:34:16 -07002926 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07002927
Jens Axboe2665abf2019-11-05 12:40:47 -07002928 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07002929 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07002930}
2931
Jens Axboead8a48a2019-11-15 08:49:11 -07002932static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07002933{
2934 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002935
Jens Axboe2665abf2019-11-05 12:40:47 -07002936 if (!(req->flags & REQ_F_LINK))
2937 return NULL;
2938
2939 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, list);
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002940 if (!nxt || nxt->sqe->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07002941 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07002942
Jens Axboe76a46e02019-11-10 23:34:16 -07002943 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07002944 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07002945}
2946
Jens Axboe0e0702d2019-11-14 21:42:10 -07002947static void __io_queue_sqe(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002948{
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03002949 struct io_kiocb *linked_timeout = io_prep_linked_timeout(req);
2950 struct io_kiocb *nxt = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002951 int ret;
2952
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03002953 ret = io_issue_sqe(req, &nxt, true);
2954 if (nxt)
2955 io_queue_async_work(nxt);
Jens Axboe491381ce2019-10-17 09:20:46 -06002956
2957 /*
2958 * We async punt it if the file wasn't marked NOWAIT, or if the file
2959 * doesn't support non-blocking read/write attempts
2960 */
2961 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
2962 (req->flags & REQ_F_MUST_PUNT))) {
Jens Axboe1a6b74f2019-12-02 10:33:15 -07002963 struct io_async_ctx *io;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002964
Jens Axboe1a6b74f2019-12-02 10:33:15 -07002965 io = kmalloc(sizeof(*io), GFP_KERNEL);
2966 if (!io)
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03002967 goto err;
Jens Axboee65ef562019-03-12 10:16:44 -06002968
Jens Axboe1a6b74f2019-12-02 10:33:15 -07002969 memcpy(&io->sqe, req->sqe, sizeof(io->sqe));
2970
2971 req->sqe = &io->sqe;
2972 req->io = io;
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03002973
2974 if (req->work.flags & IO_WQ_WORK_NEEDS_FILES) {
2975 ret = io_grab_files(req);
2976 if (ret)
2977 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002978 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03002979
2980 /*
2981 * Queued up for async execution, worker will release
2982 * submit reference when the iocb is actually submitted.
2983 */
2984 io_queue_async_work(req);
2985 return;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002986 }
Jens Axboee65ef562019-03-12 10:16:44 -06002987
Jens Axboefcb323c2019-10-24 12:39:47 -06002988err:
Jens Axboee65ef562019-03-12 10:16:44 -06002989 /* drop submission reference */
2990 io_put_req(req);
2991
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03002992 if (linked_timeout) {
Jens Axboe76a46e02019-11-10 23:34:16 -07002993 if (!ret)
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03002994 io_queue_linked_timeout(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07002995 else
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03002996 io_put_req(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07002997 }
2998
Jens Axboee65ef562019-03-12 10:16:44 -06002999 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06003000 if (ret) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07003001 io_cqring_add_event(req, ret);
Jens Axboe9e645e112019-05-10 16:07:28 -06003002 if (req->flags & REQ_F_LINK)
3003 req->flags |= REQ_F_FAIL_LINK;
Jens Axboee65ef562019-03-12 10:16:44 -06003004 io_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06003005 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003006}
3007
Jens Axboe0e0702d2019-11-14 21:42:10 -07003008static void io_queue_sqe(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08003009{
3010 int ret;
3011
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003012 if (unlikely(req->ctx->drain_next)) {
3013 req->flags |= REQ_F_IO_DRAIN;
3014 req->ctx->drain_next = false;
3015 }
3016 req->ctx->drain_next = (req->flags & REQ_F_DRAIN_LINK);
3017
Jackie Liua197f662019-11-08 08:09:12 -07003018 ret = io_req_defer(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08003019 if (ret) {
3020 if (ret != -EIOCBQUEUED) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07003021 io_cqring_add_event(req, ret);
Pavel Begunkovd3b357962019-11-19 23:32:48 +03003022 if (req->flags & REQ_F_LINK)
3023 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07003024 io_double_put_req(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08003025 }
Jens Axboe0e0702d2019-11-14 21:42:10 -07003026 } else
3027 __io_queue_sqe(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08003028}
3029
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003030static inline void io_queue_link_head(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08003031{
Jens Axboe94ae5e72019-11-14 19:39:52 -07003032 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003033 io_cqring_add_event(req, -ECANCELED);
3034 io_double_put_req(req);
3035 } else
Jens Axboe0e0702d2019-11-14 21:42:10 -07003036 io_queue_sqe(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08003037}
3038
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003039
Jens Axboe9e645e112019-05-10 16:07:28 -06003040#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK)
3041
Jackie Liua197f662019-11-08 08:09:12 -07003042static void io_submit_sqe(struct io_kiocb *req, struct io_submit_state *state,
3043 struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06003044{
Jackie Liua197f662019-11-08 08:09:12 -07003045 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -06003046 int ret;
3047
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003048 req->user_data = req->sqe->user_data;
Jens Axboe78e19bb2019-11-06 15:21:34 -07003049
Jens Axboe9e645e112019-05-10 16:07:28 -06003050 /* enforce forwards compatibility on users */
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003051 if (unlikely(req->sqe->flags & ~SQE_VALID_FLAGS)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06003052 ret = -EINVAL;
Pavel Begunkov196be952019-11-07 01:41:06 +03003053 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06003054 }
3055
Jackie Liua197f662019-11-08 08:09:12 -07003056 ret = io_req_set_file(state, req);
Jens Axboe9e645e112019-05-10 16:07:28 -06003057 if (unlikely(ret)) {
3058err_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07003059 io_cqring_add_event(req, ret);
3060 io_double_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06003061 return;
3062 }
3063
Jens Axboe9e645e112019-05-10 16:07:28 -06003064 /*
3065 * If we already have a head request, queue this one for async
3066 * submittal once the head completes. If we don't have a head but
3067 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
3068 * submitted sync once the chain is complete. If none of those
3069 * conditions are true (normal request), then just queue it.
3070 */
3071 if (*link) {
3072 struct io_kiocb *prev = *link;
Jens Axboe1a6b74f2019-12-02 10:33:15 -07003073 struct io_async_ctx *io;
Jens Axboe9e645e112019-05-10 16:07:28 -06003074
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003075 if (req->sqe->flags & IOSQE_IO_DRAIN)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003076 (*link)->flags |= REQ_F_DRAIN_LINK | REQ_F_IO_DRAIN;
3077
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003078 if (READ_ONCE(req->sqe->opcode) == IORING_OP_LINK_TIMEOUT) {
Jens Axboe94ae5e72019-11-14 19:39:52 -07003079 ret = io_timeout_setup(req);
3080 /* common setup allows offset being set, we don't */
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003081 if (!ret && req->sqe->off)
Jens Axboe94ae5e72019-11-14 19:39:52 -07003082 ret = -EINVAL;
3083 if (ret) {
3084 prev->flags |= REQ_F_FAIL_LINK;
3085 goto err_req;
3086 }
3087 }
3088
Jens Axboe1a6b74f2019-12-02 10:33:15 -07003089 io = kmalloc(sizeof(*io), GFP_KERNEL);
3090 if (!io) {
Jens Axboe9e645e112019-05-10 16:07:28 -06003091 ret = -EAGAIN;
3092 goto err_req;
3093 }
3094
Jens Axboe1a6b74f2019-12-02 10:33:15 -07003095 memcpy(&io->sqe, req->sqe, sizeof(io->sqe));
3096 req->sqe = &io->sqe;
3097 req->io = io;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02003098 trace_io_uring_link(ctx, req, prev);
Jens Axboe9e645e112019-05-10 16:07:28 -06003099 list_add_tail(&req->list, &prev->link_list);
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003100 } else if (req->sqe->flags & IOSQE_IO_LINK) {
Jens Axboe9e645e112019-05-10 16:07:28 -06003101 req->flags |= REQ_F_LINK;
3102
Jens Axboe9e645e112019-05-10 16:07:28 -06003103 INIT_LIST_HEAD(&req->link_list);
3104 *link = req;
3105 } else {
Jackie Liua197f662019-11-08 08:09:12 -07003106 io_queue_sqe(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06003107 }
3108}
3109
Jens Axboe9a56a232019-01-09 09:06:50 -07003110/*
3111 * Batched submission is done, ensure local IO is flushed out.
3112 */
3113static void io_submit_state_end(struct io_submit_state *state)
3114{
3115 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06003116 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07003117 if (state->free_reqs)
3118 kmem_cache_free_bulk(req_cachep, state->free_reqs,
3119 &state->reqs[state->cur_req]);
Jens Axboe9a56a232019-01-09 09:06:50 -07003120}
3121
3122/*
3123 * Start submission side cache.
3124 */
3125static void io_submit_state_start(struct io_submit_state *state,
3126 struct io_ring_ctx *ctx, unsigned max_ios)
3127{
3128 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07003129 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07003130 state->file = NULL;
3131 state->ios_left = max_ios;
3132}
3133
Jens Axboe2b188cc2019-01-07 10:46:33 -07003134static void io_commit_sqring(struct io_ring_ctx *ctx)
3135{
Hristo Venev75b28af2019-08-26 17:23:46 +00003136 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003137
Hristo Venev75b28af2019-08-26 17:23:46 +00003138 if (ctx->cached_sq_head != READ_ONCE(rings->sq.head)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07003139 /*
3140 * Ensure any loads from the SQEs are done at this point,
3141 * since once we write the new head, the application could
3142 * write new data to them.
3143 */
Hristo Venev75b28af2019-08-26 17:23:46 +00003144 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003145 }
3146}
3147
3148/*
Jens Axboe2b188cc2019-01-07 10:46:33 -07003149 * Fetch an sqe, if one is available. Note that s->sqe will point to memory
3150 * that is mapped by userspace. This means that care needs to be taken to
3151 * ensure that reads are stable, as we cannot rely on userspace always
3152 * being a good citizen. If members of the sqe are validated and then later
3153 * used, it's important that those reads are done through READ_ONCE() to
3154 * prevent a re-load down the line.
3155 */
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003156static bool io_get_sqring(struct io_ring_ctx *ctx, struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003157{
Hristo Venev75b28af2019-08-26 17:23:46 +00003158 struct io_rings *rings = ctx->rings;
3159 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003160 unsigned head;
3161
3162 /*
3163 * The cached sq head (or cq tail) serves two purposes:
3164 *
3165 * 1) allows us to batch the cost of updating the user visible
3166 * head updates.
3167 * 2) allows the kernel side to track the head on its own, even
3168 * though the application is the one updating it.
3169 */
3170 head = ctx->cached_sq_head;
Stefan Bühlere523a292019-04-19 11:57:44 +02003171 /* make sure SQ entry isn't read before tail */
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03003172 if (unlikely(head == smp_load_acquire(&rings->sq.tail)))
Jens Axboe2b188cc2019-01-07 10:46:33 -07003173 return false;
3174
Hristo Venev75b28af2019-08-26 17:23:46 +00003175 head = READ_ONCE(sq_array[head & ctx->sq_mask]);
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03003176 if (likely(head < ctx->sq_entries)) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003177 /*
3178 * All io need record the previous position, if LINK vs DARIN,
3179 * it can be used to mark the position of the first IO in the
3180 * link list.
3181 */
3182 req->sequence = ctx->cached_sq_head;
3183 req->sqe = &ctx->sq_sqes[head];
Jens Axboe2b188cc2019-01-07 10:46:33 -07003184 ctx->cached_sq_head++;
3185 return true;
3186 }
3187
3188 /* drop invalid entries */
3189 ctx->cached_sq_head++;
Jens Axboe498ccd92019-10-25 10:04:25 -06003190 ctx->cached_sq_dropped++;
3191 WRITE_ONCE(rings->sq_dropped, ctx->cached_sq_dropped);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003192 return false;
3193}
3194
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003195static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkovae9428c2019-11-06 00:22:14 +03003196 struct file *ring_file, int ring_fd,
3197 struct mm_struct **mm, bool async)
Jens Axboe6c271ce2019-01-10 11:22:30 -07003198{
3199 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06003200 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06003201 int i, submitted = 0;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03003202 bool mm_fault = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003203
Jens Axboec4a2ed72019-11-21 21:01:26 -07003204 /* if we have a backlog and couldn't flush it all, return BUSY */
3205 if (!list_empty(&ctx->cq_overflow_list) &&
3206 !io_cqring_overflow_flush(ctx, false))
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003207 return -EBUSY;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003208
3209 if (nr > IO_PLUG_THRESHOLD) {
3210 io_submit_state_start(&state, ctx, nr);
3211 statep = &state;
3212 }
3213
3214 for (i = 0; i < nr; i++) {
Pavel Begunkov196be952019-11-07 01:41:06 +03003215 struct io_kiocb *req;
Pavel Begunkov50585b92019-11-07 01:41:07 +03003216 unsigned int sqe_flags;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003217
Pavel Begunkov196be952019-11-07 01:41:06 +03003218 req = io_get_req(ctx, statep);
3219 if (unlikely(!req)) {
3220 if (!submitted)
3221 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003222 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06003223 }
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003224 if (!io_get_sqring(ctx, req)) {
Pavel Begunkov196be952019-11-07 01:41:06 +03003225 __io_free_req(req);
3226 break;
3227 }
Jens Axboe9e645e112019-05-10 16:07:28 -06003228
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003229 if (io_sqe_needs_user(req->sqe) && !*mm) {
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03003230 mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm);
3231 if (!mm_fault) {
3232 use_mm(ctx->sqo_mm);
3233 *mm = ctx->sqo_mm;
3234 }
3235 }
3236
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003237 sqe_flags = req->sqe->flags;
Pavel Begunkov50585b92019-11-07 01:41:07 +03003238
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003239 req->ring_file = ring_file;
3240 req->ring_fd = ring_fd;
3241 req->has_user = *mm != NULL;
3242 req->in_async = async;
3243 req->needs_fixed_file = async;
3244 trace_io_uring_submit_sqe(ctx, req->sqe->user_data,
Pavel Begunkov50585b92019-11-07 01:41:07 +03003245 true, async);
Jackie Liua197f662019-11-08 08:09:12 -07003246 io_submit_sqe(req, statep, &link);
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03003247 submitted++;
Pavel Begunkove5eb6362019-11-06 00:22:15 +03003248
3249 /*
3250 * If previous wasn't linked and we have a linked command,
3251 * that's the end of the chain. Submit the previous link.
3252 */
Pavel Begunkov50585b92019-11-07 01:41:07 +03003253 if (!(sqe_flags & IOSQE_IO_LINK) && link) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003254 io_queue_link_head(link);
Pavel Begunkove5eb6362019-11-06 00:22:15 +03003255 link = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003256 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07003257 }
3258
Jens Axboe9e645e112019-05-10 16:07:28 -06003259 if (link)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003260 io_queue_link_head(link);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003261 if (statep)
3262 io_submit_state_end(&state);
3263
Pavel Begunkovae9428c2019-11-06 00:22:14 +03003264 /* Commit SQ ring head once we've consumed and submitted all SQEs */
3265 io_commit_sqring(ctx);
3266
Jens Axboe6c271ce2019-01-10 11:22:30 -07003267 return submitted;
3268}
3269
3270static int io_sq_thread(void *data)
3271{
Jens Axboe6c271ce2019-01-10 11:22:30 -07003272 struct io_ring_ctx *ctx = data;
3273 struct mm_struct *cur_mm = NULL;
Jens Axboe181e4482019-11-25 08:52:30 -07003274 const struct cred *old_cred;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003275 mm_segment_t old_fs;
3276 DEFINE_WAIT(wait);
3277 unsigned inflight;
3278 unsigned long timeout;
Jens Axboec1edbf52019-11-10 16:56:04 -07003279 int ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003280
Jens Axboe206aefd2019-11-07 18:27:42 -07003281 complete(&ctx->completions[1]);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08003282
Jens Axboe6c271ce2019-01-10 11:22:30 -07003283 old_fs = get_fs();
3284 set_fs(USER_DS);
Jens Axboe181e4482019-11-25 08:52:30 -07003285 old_cred = override_creds(ctx->creds);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003286
Jens Axboec1edbf52019-11-10 16:56:04 -07003287 ret = timeout = inflight = 0;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003288 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003289 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003290
3291 if (inflight) {
3292 unsigned nr_events = 0;
3293
3294 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe2b2ed972019-10-25 10:06:15 -06003295 /*
3296 * inflight is the count of the maximum possible
3297 * entries we submitted, but it can be smaller
3298 * if we dropped some of them. If we don't have
3299 * poll entries available, then we know that we
3300 * have nothing left to poll for. Reset the
3301 * inflight count to zero in that case.
3302 */
3303 mutex_lock(&ctx->uring_lock);
3304 if (!list_empty(&ctx->poll_list))
3305 __io_iopoll_check(ctx, &nr_events, 0);
3306 else
3307 inflight = 0;
3308 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003309 } else {
3310 /*
3311 * Normal IO, just pretend everything completed.
3312 * We don't have to poll completions for that.
3313 */
3314 nr_events = inflight;
3315 }
3316
3317 inflight -= nr_events;
3318 if (!inflight)
3319 timeout = jiffies + ctx->sq_thread_idle;
3320 }
3321
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003322 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07003323
3324 /*
3325 * If submit got -EBUSY, flag us as needing the application
3326 * to enter the kernel to reap and flush events.
3327 */
3328 if (!to_submit || ret == -EBUSY) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07003329 /*
3330 * We're polling. If we're within the defined idle
3331 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07003332 * to sleep. The exception is if we got EBUSY doing
3333 * more IO, we should wait for the application to
3334 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07003335 */
Jens Axboec1edbf52019-11-10 16:56:04 -07003336 if (inflight ||
3337 (!time_after(jiffies, timeout) && ret != -EBUSY)) {
Jens Axboe9831a902019-09-19 09:48:55 -06003338 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07003339 continue;
3340 }
3341
3342 /*
3343 * Drop cur_mm before scheduling, we can't hold it for
3344 * long periods (or over schedule()). Do this before
3345 * adding ourselves to the waitqueue, as the unuse/drop
3346 * may sleep.
3347 */
3348 if (cur_mm) {
3349 unuse_mm(cur_mm);
3350 mmput(cur_mm);
3351 cur_mm = NULL;
3352 }
3353
3354 prepare_to_wait(&ctx->sqo_wait, &wait,
3355 TASK_INTERRUPTIBLE);
3356
3357 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00003358 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02003359 /* make sure to read SQ tail after writing flags */
3360 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07003361
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003362 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07003363 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003364 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07003365 finish_wait(&ctx->sqo_wait, &wait);
3366 break;
3367 }
3368 if (signal_pending(current))
3369 flush_signals(current);
3370 schedule();
3371 finish_wait(&ctx->sqo_wait, &wait);
3372
Hristo Venev75b28af2019-08-26 17:23:46 +00003373 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003374 continue;
3375 }
3376 finish_wait(&ctx->sqo_wait, &wait);
3377
Hristo Venev75b28af2019-08-26 17:23:46 +00003378 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003379 }
3380
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003381 to_submit = min(to_submit, ctx->sq_entries);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003382 ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true);
3383 if (ret > 0)
3384 inflight += ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003385 }
3386
3387 set_fs(old_fs);
3388 if (cur_mm) {
3389 unuse_mm(cur_mm);
3390 mmput(cur_mm);
3391 }
Jens Axboe181e4482019-11-25 08:52:30 -07003392 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06003393
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003394 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06003395
Jens Axboe6c271ce2019-01-10 11:22:30 -07003396 return 0;
3397}
3398
Jens Axboebda52162019-09-24 13:47:15 -06003399struct io_wait_queue {
3400 struct wait_queue_entry wq;
3401 struct io_ring_ctx *ctx;
3402 unsigned to_wait;
3403 unsigned nr_timeouts;
3404};
3405
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003406static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06003407{
3408 struct io_ring_ctx *ctx = iowq->ctx;
3409
3410 /*
3411 * Wake up if we have enough events, or if a timeout occured since we
3412 * started waiting. For timeouts, we always want to return to userspace,
3413 * regardless of event count.
3414 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003415 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06003416 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
3417}
3418
3419static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
3420 int wake_flags, void *key)
3421{
3422 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
3423 wq);
3424
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003425 /* use noflush == true, as we can't safely rely on locking context */
3426 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06003427 return -1;
3428
3429 return autoremove_wake_function(curr, mode, wake_flags, key);
3430}
3431
Jens Axboe2b188cc2019-01-07 10:46:33 -07003432/*
3433 * Wait until events become available, if we don't already have some. The
3434 * application must reap them itself, as they reside on the shared cq ring.
3435 */
3436static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
3437 const sigset_t __user *sig, size_t sigsz)
3438{
Jens Axboebda52162019-09-24 13:47:15 -06003439 struct io_wait_queue iowq = {
3440 .wq = {
3441 .private = current,
3442 .func = io_wake_function,
3443 .entry = LIST_HEAD_INIT(iowq.wq.entry),
3444 },
3445 .ctx = ctx,
3446 .to_wait = min_events,
3447 };
Hristo Venev75b28af2019-08-26 17:23:46 +00003448 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08003449 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003450
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003451 if (io_cqring_events(ctx, false) >= min_events)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003452 return 0;
3453
3454 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01003455#ifdef CONFIG_COMPAT
3456 if (in_compat_syscall())
3457 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07003458 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01003459 else
3460#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07003461 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01003462
Jens Axboe2b188cc2019-01-07 10:46:33 -07003463 if (ret)
3464 return ret;
3465 }
3466
Jens Axboebda52162019-09-24 13:47:15 -06003467 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02003468 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06003469 do {
3470 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
3471 TASK_INTERRUPTIBLE);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003472 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06003473 break;
3474 schedule();
3475 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08003476 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06003477 break;
3478 }
3479 } while (1);
3480 finish_wait(&ctx->wait, &iowq.wq);
3481
Jackie Liue9ffa5c2019-10-29 11:16:42 +08003482 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003483
Hristo Venev75b28af2019-08-26 17:23:46 +00003484 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003485}
3486
Jens Axboe6b063142019-01-10 22:13:58 -07003487static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
3488{
3489#if defined(CONFIG_UNIX)
3490 if (ctx->ring_sock) {
3491 struct sock *sock = ctx->ring_sock->sk;
3492 struct sk_buff *skb;
3493
3494 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
3495 kfree_skb(skb);
3496 }
3497#else
3498 int i;
3499
Jens Axboe65e19f52019-10-26 07:20:21 -06003500 for (i = 0; i < ctx->nr_user_files; i++) {
3501 struct file *file;
3502
3503 file = io_file_from_index(ctx, i);
3504 if (file)
3505 fput(file);
3506 }
Jens Axboe6b063142019-01-10 22:13:58 -07003507#endif
3508}
3509
3510static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
3511{
Jens Axboe65e19f52019-10-26 07:20:21 -06003512 unsigned nr_tables, i;
3513
3514 if (!ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003515 return -ENXIO;
3516
3517 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06003518 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
3519 for (i = 0; i < nr_tables; i++)
3520 kfree(ctx->file_table[i].files);
3521 kfree(ctx->file_table);
3522 ctx->file_table = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003523 ctx->nr_user_files = 0;
3524 return 0;
3525}
3526
Jens Axboe6c271ce2019-01-10 11:22:30 -07003527static void io_sq_thread_stop(struct io_ring_ctx *ctx)
3528{
3529 if (ctx->sqo_thread) {
Jens Axboe206aefd2019-11-07 18:27:42 -07003530 wait_for_completion(&ctx->completions[1]);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003531 /*
3532 * The park is a bit of a work-around, without it we get
3533 * warning spews on shutdown with SQPOLL set and affinity
3534 * set to a single CPU.
3535 */
Jens Axboe06058632019-04-13 09:26:03 -06003536 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003537 kthread_stop(ctx->sqo_thread);
3538 ctx->sqo_thread = NULL;
3539 }
3540}
3541
Jens Axboe6b063142019-01-10 22:13:58 -07003542static void io_finish_async(struct io_ring_ctx *ctx)
3543{
Jens Axboe6c271ce2019-01-10 11:22:30 -07003544 io_sq_thread_stop(ctx);
3545
Jens Axboe561fb042019-10-24 07:25:42 -06003546 if (ctx->io_wq) {
3547 io_wq_destroy(ctx->io_wq);
3548 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003549 }
3550}
3551
3552#if defined(CONFIG_UNIX)
3553static void io_destruct_skb(struct sk_buff *skb)
3554{
3555 struct io_ring_ctx *ctx = skb->sk->sk_user_data;
3556
Jens Axboe561fb042019-10-24 07:25:42 -06003557 if (ctx->io_wq)
3558 io_wq_flush(ctx->io_wq);
Jens Axboe8a997342019-10-09 14:40:13 -06003559
Jens Axboe6b063142019-01-10 22:13:58 -07003560 unix_destruct_scm(skb);
3561}
3562
3563/*
3564 * Ensure the UNIX gc is aware of our file set, so we are certain that
3565 * the io_uring can be safely unregistered on process exit, even if we have
3566 * loops in the file referencing.
3567 */
3568static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
3569{
3570 struct sock *sk = ctx->ring_sock->sk;
3571 struct scm_fp_list *fpl;
3572 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06003573 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07003574
3575 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
3576 unsigned long inflight = ctx->user->unix_inflight + nr;
3577
3578 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
3579 return -EMFILE;
3580 }
3581
3582 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
3583 if (!fpl)
3584 return -ENOMEM;
3585
3586 skb = alloc_skb(0, GFP_KERNEL);
3587 if (!skb) {
3588 kfree(fpl);
3589 return -ENOMEM;
3590 }
3591
3592 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07003593
Jens Axboe08a45172019-10-03 08:11:03 -06003594 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07003595 fpl->user = get_uid(ctx->user);
3596 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003597 struct file *file = io_file_from_index(ctx, i + offset);
3598
3599 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06003600 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06003601 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06003602 unix_inflight(fpl->user, fpl->fp[nr_files]);
3603 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07003604 }
3605
Jens Axboe08a45172019-10-03 08:11:03 -06003606 if (nr_files) {
3607 fpl->max = SCM_MAX_FD;
3608 fpl->count = nr_files;
3609 UNIXCB(skb).fp = fpl;
3610 skb->destructor = io_destruct_skb;
3611 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
3612 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07003613
Jens Axboe08a45172019-10-03 08:11:03 -06003614 for (i = 0; i < nr_files; i++)
3615 fput(fpl->fp[i]);
3616 } else {
3617 kfree_skb(skb);
3618 kfree(fpl);
3619 }
Jens Axboe6b063142019-01-10 22:13:58 -07003620
3621 return 0;
3622}
3623
3624/*
3625 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
3626 * causes regular reference counting to break down. We rely on the UNIX
3627 * garbage collection to take care of this problem for us.
3628 */
3629static int io_sqe_files_scm(struct io_ring_ctx *ctx)
3630{
3631 unsigned left, total;
3632 int ret = 0;
3633
3634 total = 0;
3635 left = ctx->nr_user_files;
3636 while (left) {
3637 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07003638
3639 ret = __io_sqe_files_scm(ctx, this_files, total);
3640 if (ret)
3641 break;
3642 left -= this_files;
3643 total += this_files;
3644 }
3645
3646 if (!ret)
3647 return 0;
3648
3649 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003650 struct file *file = io_file_from_index(ctx, total);
3651
3652 if (file)
3653 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07003654 total++;
3655 }
3656
3657 return ret;
3658}
3659#else
3660static int io_sqe_files_scm(struct io_ring_ctx *ctx)
3661{
3662 return 0;
3663}
3664#endif
3665
Jens Axboe65e19f52019-10-26 07:20:21 -06003666static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
3667 unsigned nr_files)
3668{
3669 int i;
3670
3671 for (i = 0; i < nr_tables; i++) {
3672 struct fixed_file_table *table = &ctx->file_table[i];
3673 unsigned this_files;
3674
3675 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
3676 table->files = kcalloc(this_files, sizeof(struct file *),
3677 GFP_KERNEL);
3678 if (!table->files)
3679 break;
3680 nr_files -= this_files;
3681 }
3682
3683 if (i == nr_tables)
3684 return 0;
3685
3686 for (i = 0; i < nr_tables; i++) {
3687 struct fixed_file_table *table = &ctx->file_table[i];
3688 kfree(table->files);
3689 }
3690 return 1;
3691}
3692
Jens Axboe6b063142019-01-10 22:13:58 -07003693static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
3694 unsigned nr_args)
3695{
3696 __s32 __user *fds = (__s32 __user *) arg;
Jens Axboe65e19f52019-10-26 07:20:21 -06003697 unsigned nr_tables;
Jens Axboe6b063142019-01-10 22:13:58 -07003698 int fd, ret = 0;
3699 unsigned i;
3700
Jens Axboe65e19f52019-10-26 07:20:21 -06003701 if (ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003702 return -EBUSY;
3703 if (!nr_args)
3704 return -EINVAL;
3705 if (nr_args > IORING_MAX_FIXED_FILES)
3706 return -EMFILE;
3707
Jens Axboe65e19f52019-10-26 07:20:21 -06003708 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
3709 ctx->file_table = kcalloc(nr_tables, sizeof(struct fixed_file_table),
3710 GFP_KERNEL);
3711 if (!ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003712 return -ENOMEM;
3713
Jens Axboe65e19f52019-10-26 07:20:21 -06003714 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
3715 kfree(ctx->file_table);
Jens Axboe46568e92019-11-10 08:40:53 -07003716 ctx->file_table = NULL;
Jens Axboe65e19f52019-10-26 07:20:21 -06003717 return -ENOMEM;
3718 }
3719
Jens Axboe08a45172019-10-03 08:11:03 -06003720 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003721 struct fixed_file_table *table;
3722 unsigned index;
3723
Jens Axboe6b063142019-01-10 22:13:58 -07003724 ret = -EFAULT;
3725 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
3726 break;
Jens Axboe08a45172019-10-03 08:11:03 -06003727 /* allow sparse sets */
3728 if (fd == -1) {
3729 ret = 0;
3730 continue;
3731 }
Jens Axboe6b063142019-01-10 22:13:58 -07003732
Jens Axboe65e19f52019-10-26 07:20:21 -06003733 table = &ctx->file_table[i >> IORING_FILE_TABLE_SHIFT];
3734 index = i & IORING_FILE_TABLE_MASK;
3735 table->files[index] = fget(fd);
Jens Axboe6b063142019-01-10 22:13:58 -07003736
3737 ret = -EBADF;
Jens Axboe65e19f52019-10-26 07:20:21 -06003738 if (!table->files[index])
Jens Axboe6b063142019-01-10 22:13:58 -07003739 break;
3740 /*
3741 * Don't allow io_uring instances to be registered. If UNIX
3742 * isn't enabled, then this causes a reference cycle and this
3743 * instance can never get freed. If UNIX is enabled we'll
3744 * handle it just fine, but there's still no point in allowing
3745 * a ring fd as it doesn't support regular read/write anyway.
3746 */
Jens Axboe65e19f52019-10-26 07:20:21 -06003747 if (table->files[index]->f_op == &io_uring_fops) {
3748 fput(table->files[index]);
Jens Axboe6b063142019-01-10 22:13:58 -07003749 break;
3750 }
Jens Axboe6b063142019-01-10 22:13:58 -07003751 ret = 0;
3752 }
3753
3754 if (ret) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003755 for (i = 0; i < ctx->nr_user_files; i++) {
3756 struct file *file;
Jens Axboe6b063142019-01-10 22:13:58 -07003757
Jens Axboe65e19f52019-10-26 07:20:21 -06003758 file = io_file_from_index(ctx, i);
3759 if (file)
3760 fput(file);
3761 }
3762 for (i = 0; i < nr_tables; i++)
3763 kfree(ctx->file_table[i].files);
3764
3765 kfree(ctx->file_table);
3766 ctx->file_table = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003767 ctx->nr_user_files = 0;
3768 return ret;
3769 }
3770
3771 ret = io_sqe_files_scm(ctx);
3772 if (ret)
3773 io_sqe_files_unregister(ctx);
3774
3775 return ret;
3776}
3777
Jens Axboec3a31e62019-10-03 13:59:56 -06003778static void io_sqe_file_unregister(struct io_ring_ctx *ctx, int index)
3779{
3780#if defined(CONFIG_UNIX)
Jens Axboe65e19f52019-10-26 07:20:21 -06003781 struct file *file = io_file_from_index(ctx, index);
Jens Axboec3a31e62019-10-03 13:59:56 -06003782 struct sock *sock = ctx->ring_sock->sk;
3783 struct sk_buff_head list, *head = &sock->sk_receive_queue;
3784 struct sk_buff *skb;
3785 int i;
3786
3787 __skb_queue_head_init(&list);
3788
3789 /*
3790 * Find the skb that holds this file in its SCM_RIGHTS. When found,
3791 * remove this entry and rearrange the file array.
3792 */
3793 skb = skb_dequeue(head);
3794 while (skb) {
3795 struct scm_fp_list *fp;
3796
3797 fp = UNIXCB(skb).fp;
3798 for (i = 0; i < fp->count; i++) {
3799 int left;
3800
3801 if (fp->fp[i] != file)
3802 continue;
3803
3804 unix_notinflight(fp->user, fp->fp[i]);
3805 left = fp->count - 1 - i;
3806 if (left) {
3807 memmove(&fp->fp[i], &fp->fp[i + 1],
3808 left * sizeof(struct file *));
3809 }
3810 fp->count--;
3811 if (!fp->count) {
3812 kfree_skb(skb);
3813 skb = NULL;
3814 } else {
3815 __skb_queue_tail(&list, skb);
3816 }
3817 fput(file);
3818 file = NULL;
3819 break;
3820 }
3821
3822 if (!file)
3823 break;
3824
3825 __skb_queue_tail(&list, skb);
3826
3827 skb = skb_dequeue(head);
3828 }
3829
3830 if (skb_peek(&list)) {
3831 spin_lock_irq(&head->lock);
3832 while ((skb = __skb_dequeue(&list)) != NULL)
3833 __skb_queue_tail(head, skb);
3834 spin_unlock_irq(&head->lock);
3835 }
3836#else
Jens Axboe65e19f52019-10-26 07:20:21 -06003837 fput(io_file_from_index(ctx, index));
Jens Axboec3a31e62019-10-03 13:59:56 -06003838#endif
3839}
3840
3841static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
3842 int index)
3843{
3844#if defined(CONFIG_UNIX)
3845 struct sock *sock = ctx->ring_sock->sk;
3846 struct sk_buff_head *head = &sock->sk_receive_queue;
3847 struct sk_buff *skb;
3848
3849 /*
3850 * See if we can merge this file into an existing skb SCM_RIGHTS
3851 * file set. If there's no room, fall back to allocating a new skb
3852 * and filling it in.
3853 */
3854 spin_lock_irq(&head->lock);
3855 skb = skb_peek(head);
3856 if (skb) {
3857 struct scm_fp_list *fpl = UNIXCB(skb).fp;
3858
3859 if (fpl->count < SCM_MAX_FD) {
3860 __skb_unlink(skb, head);
3861 spin_unlock_irq(&head->lock);
3862 fpl->fp[fpl->count] = get_file(file);
3863 unix_inflight(fpl->user, fpl->fp[fpl->count]);
3864 fpl->count++;
3865 spin_lock_irq(&head->lock);
3866 __skb_queue_head(head, skb);
3867 } else {
3868 skb = NULL;
3869 }
3870 }
3871 spin_unlock_irq(&head->lock);
3872
3873 if (skb) {
3874 fput(file);
3875 return 0;
3876 }
3877
3878 return __io_sqe_files_scm(ctx, 1, index);
3879#else
3880 return 0;
3881#endif
3882}
3883
3884static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
3885 unsigned nr_args)
3886{
3887 struct io_uring_files_update up;
3888 __s32 __user *fds;
3889 int fd, i, err;
3890 __u32 done;
3891
Jens Axboe65e19f52019-10-26 07:20:21 -06003892 if (!ctx->file_table)
Jens Axboec3a31e62019-10-03 13:59:56 -06003893 return -ENXIO;
3894 if (!nr_args)
3895 return -EINVAL;
3896 if (copy_from_user(&up, arg, sizeof(up)))
3897 return -EFAULT;
3898 if (check_add_overflow(up.offset, nr_args, &done))
3899 return -EOVERFLOW;
3900 if (done > ctx->nr_user_files)
3901 return -EINVAL;
3902
3903 done = 0;
3904 fds = (__s32 __user *) up.fds;
3905 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003906 struct fixed_file_table *table;
3907 unsigned index;
3908
Jens Axboec3a31e62019-10-03 13:59:56 -06003909 err = 0;
3910 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
3911 err = -EFAULT;
3912 break;
3913 }
3914 i = array_index_nospec(up.offset, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06003915 table = &ctx->file_table[i >> IORING_FILE_TABLE_SHIFT];
3916 index = i & IORING_FILE_TABLE_MASK;
3917 if (table->files[index]) {
Jens Axboec3a31e62019-10-03 13:59:56 -06003918 io_sqe_file_unregister(ctx, i);
Jens Axboe65e19f52019-10-26 07:20:21 -06003919 table->files[index] = NULL;
Jens Axboec3a31e62019-10-03 13:59:56 -06003920 }
3921 if (fd != -1) {
3922 struct file *file;
3923
3924 file = fget(fd);
3925 if (!file) {
3926 err = -EBADF;
3927 break;
3928 }
3929 /*
3930 * Don't allow io_uring instances to be registered. If
3931 * UNIX isn't enabled, then this causes a reference
3932 * cycle and this instance can never get freed. If UNIX
3933 * is enabled we'll handle it just fine, but there's
3934 * still no point in allowing a ring fd as it doesn't
3935 * support regular read/write anyway.
3936 */
3937 if (file->f_op == &io_uring_fops) {
3938 fput(file);
3939 err = -EBADF;
3940 break;
3941 }
Jens Axboe65e19f52019-10-26 07:20:21 -06003942 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06003943 err = io_sqe_file_register(ctx, file, i);
3944 if (err)
3945 break;
3946 }
3947 nr_args--;
3948 done++;
3949 up.offset++;
3950 }
3951
3952 return done ? done : err;
3953}
3954
Jens Axboe7d723062019-11-12 22:31:31 -07003955static void io_put_work(struct io_wq_work *work)
3956{
3957 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
3958
3959 io_put_req(req);
3960}
3961
3962static void io_get_work(struct io_wq_work *work)
3963{
3964 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
3965
3966 refcount_inc(&req->refs);
3967}
3968
Jens Axboe6c271ce2019-01-10 11:22:30 -07003969static int io_sq_offload_start(struct io_ring_ctx *ctx,
3970 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003971{
Jens Axboe576a3472019-11-25 08:49:20 -07003972 struct io_wq_data data;
Jens Axboe561fb042019-10-24 07:25:42 -06003973 unsigned concurrency;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003974 int ret;
3975
Jens Axboe6c271ce2019-01-10 11:22:30 -07003976 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003977 mmgrab(current->mm);
3978 ctx->sqo_mm = current->mm;
3979
Jens Axboe6c271ce2019-01-10 11:22:30 -07003980 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06003981 ret = -EPERM;
3982 if (!capable(CAP_SYS_ADMIN))
3983 goto err;
3984
Jens Axboe917257d2019-04-13 09:28:55 -06003985 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
3986 if (!ctx->sq_thread_idle)
3987 ctx->sq_thread_idle = HZ;
3988
Jens Axboe6c271ce2019-01-10 11:22:30 -07003989 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06003990 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003991
Jens Axboe917257d2019-04-13 09:28:55 -06003992 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06003993 if (cpu >= nr_cpu_ids)
3994 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08003995 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06003996 goto err;
3997
Jens Axboe6c271ce2019-01-10 11:22:30 -07003998 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
3999 ctx, cpu,
4000 "io_uring-sq");
4001 } else {
4002 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
4003 "io_uring-sq");
4004 }
4005 if (IS_ERR(ctx->sqo_thread)) {
4006 ret = PTR_ERR(ctx->sqo_thread);
4007 ctx->sqo_thread = NULL;
4008 goto err;
4009 }
4010 wake_up_process(ctx->sqo_thread);
4011 } else if (p->flags & IORING_SETUP_SQ_AFF) {
4012 /* Can't have SQ_AFF without SQPOLL */
4013 ret = -EINVAL;
4014 goto err;
4015 }
4016
Jens Axboe576a3472019-11-25 08:49:20 -07004017 data.mm = ctx->sqo_mm;
4018 data.user = ctx->user;
Jens Axboe181e4482019-11-25 08:52:30 -07004019 data.creds = ctx->creds;
Jens Axboe576a3472019-11-25 08:49:20 -07004020 data.get_work = io_get_work;
4021 data.put_work = io_put_work;
4022
Jens Axboe561fb042019-10-24 07:25:42 -06004023 /* Do QD, or 4 * CPUS, whatever is smallest */
4024 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
Jens Axboe576a3472019-11-25 08:49:20 -07004025 ctx->io_wq = io_wq_create(concurrency, &data);
Jens Axboe975c99a52019-10-30 08:42:56 -06004026 if (IS_ERR(ctx->io_wq)) {
4027 ret = PTR_ERR(ctx->io_wq);
4028 ctx->io_wq = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004029 goto err;
4030 }
4031
4032 return 0;
4033err:
Jens Axboe54a91f32019-09-10 09:15:04 -06004034 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004035 mmdrop(ctx->sqo_mm);
4036 ctx->sqo_mm = NULL;
4037 return ret;
4038}
4039
4040static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
4041{
4042 atomic_long_sub(nr_pages, &user->locked_vm);
4043}
4044
4045static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
4046{
4047 unsigned long page_limit, cur_pages, new_pages;
4048
4049 /* Don't allow more pages than we can safely lock */
4050 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
4051
4052 do {
4053 cur_pages = atomic_long_read(&user->locked_vm);
4054 new_pages = cur_pages + nr_pages;
4055 if (new_pages > page_limit)
4056 return -ENOMEM;
4057 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
4058 new_pages) != cur_pages);
4059
4060 return 0;
4061}
4062
4063static void io_mem_free(void *ptr)
4064{
Mark Rutland52e04ef2019-04-30 17:30:21 +01004065 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004066
Mark Rutland52e04ef2019-04-30 17:30:21 +01004067 if (!ptr)
4068 return;
4069
4070 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004071 if (put_page_testzero(page))
4072 free_compound_page(page);
4073}
4074
4075static void *io_mem_alloc(size_t size)
4076{
4077 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
4078 __GFP_NORETRY;
4079
4080 return (void *) __get_free_pages(gfp_flags, get_order(size));
4081}
4082
Hristo Venev75b28af2019-08-26 17:23:46 +00004083static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
4084 size_t *sq_offset)
4085{
4086 struct io_rings *rings;
4087 size_t off, sq_array_size;
4088
4089 off = struct_size(rings, cqes, cq_entries);
4090 if (off == SIZE_MAX)
4091 return SIZE_MAX;
4092
4093#ifdef CONFIG_SMP
4094 off = ALIGN(off, SMP_CACHE_BYTES);
4095 if (off == 0)
4096 return SIZE_MAX;
4097#endif
4098
4099 sq_array_size = array_size(sizeof(u32), sq_entries);
4100 if (sq_array_size == SIZE_MAX)
4101 return SIZE_MAX;
4102
4103 if (check_add_overflow(off, sq_array_size, &off))
4104 return SIZE_MAX;
4105
4106 if (sq_offset)
4107 *sq_offset = off;
4108
4109 return off;
4110}
4111
Jens Axboe2b188cc2019-01-07 10:46:33 -07004112static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
4113{
Hristo Venev75b28af2019-08-26 17:23:46 +00004114 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004115
Hristo Venev75b28af2019-08-26 17:23:46 +00004116 pages = (size_t)1 << get_order(
4117 rings_size(sq_entries, cq_entries, NULL));
4118 pages += (size_t)1 << get_order(
4119 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07004120
Hristo Venev75b28af2019-08-26 17:23:46 +00004121 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004122}
4123
Jens Axboeedafcce2019-01-09 09:16:05 -07004124static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
4125{
4126 int i, j;
4127
4128 if (!ctx->user_bufs)
4129 return -ENXIO;
4130
4131 for (i = 0; i < ctx->nr_user_bufs; i++) {
4132 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
4133
4134 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbard27c4d3a2019-08-04 19:32:06 -07004135 put_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07004136
4137 if (ctx->account_mem)
4138 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004139 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07004140 imu->nr_bvecs = 0;
4141 }
4142
4143 kfree(ctx->user_bufs);
4144 ctx->user_bufs = NULL;
4145 ctx->nr_user_bufs = 0;
4146 return 0;
4147}
4148
4149static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
4150 void __user *arg, unsigned index)
4151{
4152 struct iovec __user *src;
4153
4154#ifdef CONFIG_COMPAT
4155 if (ctx->compat) {
4156 struct compat_iovec __user *ciovs;
4157 struct compat_iovec ciov;
4158
4159 ciovs = (struct compat_iovec __user *) arg;
4160 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
4161 return -EFAULT;
4162
4163 dst->iov_base = (void __user *) (unsigned long) ciov.iov_base;
4164 dst->iov_len = ciov.iov_len;
4165 return 0;
4166 }
4167#endif
4168 src = (struct iovec __user *) arg;
4169 if (copy_from_user(dst, &src[index], sizeof(*dst)))
4170 return -EFAULT;
4171 return 0;
4172}
4173
4174static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
4175 unsigned nr_args)
4176{
4177 struct vm_area_struct **vmas = NULL;
4178 struct page **pages = NULL;
4179 int i, j, got_pages = 0;
4180 int ret = -EINVAL;
4181
4182 if (ctx->user_bufs)
4183 return -EBUSY;
4184 if (!nr_args || nr_args > UIO_MAXIOV)
4185 return -EINVAL;
4186
4187 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
4188 GFP_KERNEL);
4189 if (!ctx->user_bufs)
4190 return -ENOMEM;
4191
4192 for (i = 0; i < nr_args; i++) {
4193 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
4194 unsigned long off, start, end, ubuf;
4195 int pret, nr_pages;
4196 struct iovec iov;
4197 size_t size;
4198
4199 ret = io_copy_iov(ctx, &iov, arg, i);
4200 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03004201 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07004202
4203 /*
4204 * Don't impose further limits on the size and buffer
4205 * constraints here, we'll -EINVAL later when IO is
4206 * submitted if they are wrong.
4207 */
4208 ret = -EFAULT;
4209 if (!iov.iov_base || !iov.iov_len)
4210 goto err;
4211
4212 /* arbitrary limit, but we need something */
4213 if (iov.iov_len > SZ_1G)
4214 goto err;
4215
4216 ubuf = (unsigned long) iov.iov_base;
4217 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
4218 start = ubuf >> PAGE_SHIFT;
4219 nr_pages = end - start;
4220
4221 if (ctx->account_mem) {
4222 ret = io_account_mem(ctx->user, nr_pages);
4223 if (ret)
4224 goto err;
4225 }
4226
4227 ret = 0;
4228 if (!pages || nr_pages > got_pages) {
4229 kfree(vmas);
4230 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004231 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07004232 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004233 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07004234 sizeof(struct vm_area_struct *),
4235 GFP_KERNEL);
4236 if (!pages || !vmas) {
4237 ret = -ENOMEM;
4238 if (ctx->account_mem)
4239 io_unaccount_mem(ctx->user, nr_pages);
4240 goto err;
4241 }
4242 got_pages = nr_pages;
4243 }
4244
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004245 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07004246 GFP_KERNEL);
4247 ret = -ENOMEM;
4248 if (!imu->bvec) {
4249 if (ctx->account_mem)
4250 io_unaccount_mem(ctx->user, nr_pages);
4251 goto err;
4252 }
4253
4254 ret = 0;
4255 down_read(&current->mm->mmap_sem);
Ira Weiny932f4a62019-05-13 17:17:03 -07004256 pret = get_user_pages(ubuf, nr_pages,
4257 FOLL_WRITE | FOLL_LONGTERM,
4258 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07004259 if (pret == nr_pages) {
4260 /* don't support file backed memory */
4261 for (j = 0; j < nr_pages; j++) {
4262 struct vm_area_struct *vma = vmas[j];
4263
4264 if (vma->vm_file &&
4265 !is_file_hugepages(vma->vm_file)) {
4266 ret = -EOPNOTSUPP;
4267 break;
4268 }
4269 }
4270 } else {
4271 ret = pret < 0 ? pret : -EFAULT;
4272 }
4273 up_read(&current->mm->mmap_sem);
4274 if (ret) {
4275 /*
4276 * if we did partial map, or found file backed vmas,
4277 * release any pages we did get
4278 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07004279 if (pret > 0)
4280 put_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07004281 if (ctx->account_mem)
4282 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004283 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07004284 goto err;
4285 }
4286
4287 off = ubuf & ~PAGE_MASK;
4288 size = iov.iov_len;
4289 for (j = 0; j < nr_pages; j++) {
4290 size_t vec_len;
4291
4292 vec_len = min_t(size_t, size, PAGE_SIZE - off);
4293 imu->bvec[j].bv_page = pages[j];
4294 imu->bvec[j].bv_len = vec_len;
4295 imu->bvec[j].bv_offset = off;
4296 off = 0;
4297 size -= vec_len;
4298 }
4299 /* store original address for later verification */
4300 imu->ubuf = ubuf;
4301 imu->len = iov.iov_len;
4302 imu->nr_bvecs = nr_pages;
4303
4304 ctx->nr_user_bufs++;
4305 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004306 kvfree(pages);
4307 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07004308 return 0;
4309err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004310 kvfree(pages);
4311 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07004312 io_sqe_buffer_unregister(ctx);
4313 return ret;
4314}
4315
Jens Axboe9b402842019-04-11 11:45:41 -06004316static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
4317{
4318 __s32 __user *fds = arg;
4319 int fd;
4320
4321 if (ctx->cq_ev_fd)
4322 return -EBUSY;
4323
4324 if (copy_from_user(&fd, fds, sizeof(*fds)))
4325 return -EFAULT;
4326
4327 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
4328 if (IS_ERR(ctx->cq_ev_fd)) {
4329 int ret = PTR_ERR(ctx->cq_ev_fd);
4330 ctx->cq_ev_fd = NULL;
4331 return ret;
4332 }
4333
4334 return 0;
4335}
4336
4337static int io_eventfd_unregister(struct io_ring_ctx *ctx)
4338{
4339 if (ctx->cq_ev_fd) {
4340 eventfd_ctx_put(ctx->cq_ev_fd);
4341 ctx->cq_ev_fd = NULL;
4342 return 0;
4343 }
4344
4345 return -ENXIO;
4346}
4347
Jens Axboe2b188cc2019-01-07 10:46:33 -07004348static void io_ring_ctx_free(struct io_ring_ctx *ctx)
4349{
Jens Axboe6b063142019-01-10 22:13:58 -07004350 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004351 if (ctx->sqo_mm)
4352 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07004353
4354 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07004355 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07004356 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06004357 io_eventfd_unregister(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07004358
Jens Axboe2b188cc2019-01-07 10:46:33 -07004359#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07004360 if (ctx->ring_sock) {
4361 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004362 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07004363 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004364#endif
4365
Hristo Venev75b28af2019-08-26 17:23:46 +00004366 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004367 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004368
4369 percpu_ref_exit(&ctx->refs);
4370 if (ctx->account_mem)
4371 io_unaccount_mem(ctx->user,
4372 ring_pages(ctx->sq_entries, ctx->cq_entries));
4373 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07004374 put_cred(ctx->creds);
Jens Axboe206aefd2019-11-07 18:27:42 -07004375 kfree(ctx->completions);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07004376 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004377 kfree(ctx);
4378}
4379
4380static __poll_t io_uring_poll(struct file *file, poll_table *wait)
4381{
4382 struct io_ring_ctx *ctx = file->private_data;
4383 __poll_t mask = 0;
4384
4385 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02004386 /*
4387 * synchronizes with barrier from wq_has_sleeper call in
4388 * io_commit_cqring
4389 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004390 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00004391 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
4392 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004393 mask |= EPOLLOUT | EPOLLWRNORM;
yangerkundaa5de52019-09-24 20:53:34 +08004394 if (READ_ONCE(ctx->rings->cq.head) != ctx->cached_cq_tail)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004395 mask |= EPOLLIN | EPOLLRDNORM;
4396
4397 return mask;
4398}
4399
4400static int io_uring_fasync(int fd, struct file *file, int on)
4401{
4402 struct io_ring_ctx *ctx = file->private_data;
4403
4404 return fasync_helper(fd, file, on, &ctx->cq_fasync);
4405}
4406
4407static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
4408{
4409 mutex_lock(&ctx->uring_lock);
4410 percpu_ref_kill(&ctx->refs);
4411 mutex_unlock(&ctx->uring_lock);
4412
Jens Axboe5262f562019-09-17 12:26:57 -06004413 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004414 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06004415
4416 if (ctx->io_wq)
4417 io_wq_cancel_all(ctx->io_wq);
4418
Jens Axboedef596e2019-01-09 08:59:42 -07004419 io_iopoll_reap_events(ctx);
Jens Axboe15dff282019-11-13 09:09:23 -07004420 /* if we failed setting up the ctx, we might not have any rings */
4421 if (ctx->rings)
4422 io_cqring_overflow_flush(ctx, true);
Jens Axboe206aefd2019-11-07 18:27:42 -07004423 wait_for_completion(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004424 io_ring_ctx_free(ctx);
4425}
4426
4427static int io_uring_release(struct inode *inode, struct file *file)
4428{
4429 struct io_ring_ctx *ctx = file->private_data;
4430
4431 file->private_data = NULL;
4432 io_ring_ctx_wait_and_kill(ctx);
4433 return 0;
4434}
4435
Jens Axboefcb323c2019-10-24 12:39:47 -06004436static void io_uring_cancel_files(struct io_ring_ctx *ctx,
4437 struct files_struct *files)
4438{
4439 struct io_kiocb *req;
4440 DEFINE_WAIT(wait);
4441
4442 while (!list_empty_careful(&ctx->inflight_list)) {
Jens Axboe768134d2019-11-10 20:30:53 -07004443 struct io_kiocb *cancel_req = NULL;
Jens Axboefcb323c2019-10-24 12:39:47 -06004444
4445 spin_lock_irq(&ctx->inflight_lock);
4446 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07004447 if (req->work.files != files)
4448 continue;
4449 /* req is being completed, ignore */
4450 if (!refcount_inc_not_zero(&req->refs))
4451 continue;
4452 cancel_req = req;
4453 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06004454 }
Jens Axboe768134d2019-11-10 20:30:53 -07004455 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06004456 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07004457 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06004458 spin_unlock_irq(&ctx->inflight_lock);
4459
Jens Axboe768134d2019-11-10 20:30:53 -07004460 /* We need to keep going until we don't find a matching req */
4461 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06004462 break;
Bob Liu2f6d9b92019-11-13 18:06:24 +08004463
4464 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
4465 io_put_req(cancel_req);
Jens Axboefcb323c2019-10-24 12:39:47 -06004466 schedule();
4467 }
Jens Axboe768134d2019-11-10 20:30:53 -07004468 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06004469}
4470
4471static int io_uring_flush(struct file *file, void *data)
4472{
4473 struct io_ring_ctx *ctx = file->private_data;
4474
4475 io_uring_cancel_files(ctx, data);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004476 if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) {
4477 io_cqring_overflow_flush(ctx, true);
Jens Axboefcb323c2019-10-24 12:39:47 -06004478 io_wq_cancel_all(ctx->io_wq);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004479 }
Jens Axboefcb323c2019-10-24 12:39:47 -06004480 return 0;
4481}
4482
Roman Penyaev6c5c2402019-11-28 12:53:22 +01004483static void *io_uring_validate_mmap_request(struct file *file,
4484 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004485{
Jens Axboe2b188cc2019-01-07 10:46:33 -07004486 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01004487 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004488 struct page *page;
4489 void *ptr;
4490
4491 switch (offset) {
4492 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00004493 case IORING_OFF_CQ_RING:
4494 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004495 break;
4496 case IORING_OFF_SQES:
4497 ptr = ctx->sq_sqes;
4498 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004499 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01004500 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004501 }
4502
4503 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07004504 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01004505 return ERR_PTR(-EINVAL);
4506
4507 return ptr;
4508}
4509
4510#ifdef CONFIG_MMU
4511
4512static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
4513{
4514 size_t sz = vma->vm_end - vma->vm_start;
4515 unsigned long pfn;
4516 void *ptr;
4517
4518 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
4519 if (IS_ERR(ptr))
4520 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004521
4522 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
4523 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
4524}
4525
Roman Penyaev6c5c2402019-11-28 12:53:22 +01004526#else /* !CONFIG_MMU */
4527
4528static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
4529{
4530 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
4531}
4532
4533static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
4534{
4535 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
4536}
4537
4538static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
4539 unsigned long addr, unsigned long len,
4540 unsigned long pgoff, unsigned long flags)
4541{
4542 void *ptr;
4543
4544 ptr = io_uring_validate_mmap_request(file, pgoff, len);
4545 if (IS_ERR(ptr))
4546 return PTR_ERR(ptr);
4547
4548 return (unsigned long) ptr;
4549}
4550
4551#endif /* !CONFIG_MMU */
4552
Jens Axboe2b188cc2019-01-07 10:46:33 -07004553SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
4554 u32, min_complete, u32, flags, const sigset_t __user *, sig,
4555 size_t, sigsz)
4556{
4557 struct io_ring_ctx *ctx;
4558 long ret = -EBADF;
4559 int submitted = 0;
4560 struct fd f;
4561
Jens Axboe6c271ce2019-01-10 11:22:30 -07004562 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07004563 return -EINVAL;
4564
4565 f = fdget(fd);
4566 if (!f.file)
4567 return -EBADF;
4568
4569 ret = -EOPNOTSUPP;
4570 if (f.file->f_op != &io_uring_fops)
4571 goto out_fput;
4572
4573 ret = -ENXIO;
4574 ctx = f.file->private_data;
4575 if (!percpu_ref_tryget(&ctx->refs))
4576 goto out_fput;
4577
Jens Axboe6c271ce2019-01-10 11:22:30 -07004578 /*
4579 * For SQ polling, the thread will do all submissions and completions.
4580 * Just return the requested submit count, and wake the thread if
4581 * we were asked to.
4582 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06004583 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004584 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07004585 if (!list_empty_careful(&ctx->cq_overflow_list))
4586 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004587 if (flags & IORING_ENTER_SQ_WAKEUP)
4588 wake_up(&ctx->sqo_wait);
4589 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06004590 } else if (to_submit) {
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004591 struct mm_struct *cur_mm;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004592
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004593 to_submit = min(to_submit, ctx->sq_entries);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004594 mutex_lock(&ctx->uring_lock);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004595 /* already have mm, so io_submit_sqes() won't try to grab it */
4596 cur_mm = ctx->sqo_mm;
4597 submitted = io_submit_sqes(ctx, to_submit, f.file, fd,
4598 &cur_mm, false);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004599 mutex_unlock(&ctx->uring_lock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004600 }
4601 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07004602 unsigned nr_events = 0;
4603
Jens Axboe2b188cc2019-01-07 10:46:33 -07004604 min_complete = min(min_complete, ctx->cq_entries);
4605
Jens Axboedef596e2019-01-09 08:59:42 -07004606 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07004607 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07004608 } else {
4609 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
4610 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004611 }
4612
Pavel Begunkov6805b322019-10-08 02:18:42 +03004613 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004614out_fput:
4615 fdput(f);
4616 return submitted ? submitted : ret;
4617}
4618
4619static const struct file_operations io_uring_fops = {
4620 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06004621 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07004622 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01004623#ifndef CONFIG_MMU
4624 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
4625 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
4626#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07004627 .poll = io_uring_poll,
4628 .fasync = io_uring_fasync,
4629};
4630
4631static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
4632 struct io_uring_params *p)
4633{
Hristo Venev75b28af2019-08-26 17:23:46 +00004634 struct io_rings *rings;
4635 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004636
Hristo Venev75b28af2019-08-26 17:23:46 +00004637 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
4638 if (size == SIZE_MAX)
4639 return -EOVERFLOW;
4640
4641 rings = io_mem_alloc(size);
4642 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004643 return -ENOMEM;
4644
Hristo Venev75b28af2019-08-26 17:23:46 +00004645 ctx->rings = rings;
4646 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
4647 rings->sq_ring_mask = p->sq_entries - 1;
4648 rings->cq_ring_mask = p->cq_entries - 1;
4649 rings->sq_ring_entries = p->sq_entries;
4650 rings->cq_ring_entries = p->cq_entries;
4651 ctx->sq_mask = rings->sq_ring_mask;
4652 ctx->cq_mask = rings->cq_ring_mask;
4653 ctx->sq_entries = rings->sq_ring_entries;
4654 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004655
4656 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07004657 if (size == SIZE_MAX) {
4658 io_mem_free(ctx->rings);
4659 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004660 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07004661 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004662
4663 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07004664 if (!ctx->sq_sqes) {
4665 io_mem_free(ctx->rings);
4666 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004667 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07004668 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004669
Jens Axboe2b188cc2019-01-07 10:46:33 -07004670 return 0;
4671}
4672
4673/*
4674 * Allocate an anonymous fd, this is what constitutes the application
4675 * visible backing of an io_uring instance. The application mmaps this
4676 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
4677 * we have to tie this fd to a socket for file garbage collection purposes.
4678 */
4679static int io_uring_get_fd(struct io_ring_ctx *ctx)
4680{
4681 struct file *file;
4682 int ret;
4683
4684#if defined(CONFIG_UNIX)
4685 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
4686 &ctx->ring_sock);
4687 if (ret)
4688 return ret;
4689#endif
4690
4691 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
4692 if (ret < 0)
4693 goto err;
4694
4695 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
4696 O_RDWR | O_CLOEXEC);
4697 if (IS_ERR(file)) {
4698 put_unused_fd(ret);
4699 ret = PTR_ERR(file);
4700 goto err;
4701 }
4702
4703#if defined(CONFIG_UNIX)
4704 ctx->ring_sock->file = file;
Jens Axboe6b063142019-01-10 22:13:58 -07004705 ctx->ring_sock->sk->sk_user_data = ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004706#endif
4707 fd_install(ret, file);
4708 return ret;
4709err:
4710#if defined(CONFIG_UNIX)
4711 sock_release(ctx->ring_sock);
4712 ctx->ring_sock = NULL;
4713#endif
4714 return ret;
4715}
4716
4717static int io_uring_create(unsigned entries, struct io_uring_params *p)
4718{
4719 struct user_struct *user = NULL;
4720 struct io_ring_ctx *ctx;
4721 bool account_mem;
4722 int ret;
4723
4724 if (!entries || entries > IORING_MAX_ENTRIES)
4725 return -EINVAL;
4726
4727 /*
4728 * Use twice as many entries for the CQ ring. It's possible for the
4729 * application to drive a higher depth than the size of the SQ ring,
4730 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06004731 * some flexibility in overcommitting a bit. If the application has
4732 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
4733 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07004734 */
4735 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06004736 if (p->flags & IORING_SETUP_CQSIZE) {
4737 /*
4738 * If IORING_SETUP_CQSIZE is set, we do the same roundup
4739 * to a power-of-two, if it isn't already. We do NOT impose
4740 * any cq vs sq ring sizing.
4741 */
4742 if (p->cq_entries < p->sq_entries || p->cq_entries > IORING_MAX_CQ_ENTRIES)
4743 return -EINVAL;
4744 p->cq_entries = roundup_pow_of_two(p->cq_entries);
4745 } else {
4746 p->cq_entries = 2 * p->sq_entries;
4747 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004748
4749 user = get_uid(current_user());
4750 account_mem = !capable(CAP_IPC_LOCK);
4751
4752 if (account_mem) {
4753 ret = io_account_mem(user,
4754 ring_pages(p->sq_entries, p->cq_entries));
4755 if (ret) {
4756 free_uid(user);
4757 return ret;
4758 }
4759 }
4760
4761 ctx = io_ring_ctx_alloc(p);
4762 if (!ctx) {
4763 if (account_mem)
4764 io_unaccount_mem(user, ring_pages(p->sq_entries,
4765 p->cq_entries));
4766 free_uid(user);
4767 return -ENOMEM;
4768 }
4769 ctx->compat = in_compat_syscall();
4770 ctx->account_mem = account_mem;
4771 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07004772 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07004773
4774 ret = io_allocate_scq_urings(ctx, p);
4775 if (ret)
4776 goto err;
4777
Jens Axboe6c271ce2019-01-10 11:22:30 -07004778 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004779 if (ret)
4780 goto err;
4781
Jens Axboe2b188cc2019-01-07 10:46:33 -07004782 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00004783 p->sq_off.head = offsetof(struct io_rings, sq.head);
4784 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
4785 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
4786 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
4787 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
4788 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
4789 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004790
4791 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00004792 p->cq_off.head = offsetof(struct io_rings, cq.head);
4793 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
4794 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
4795 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
4796 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
4797 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06004798
Jens Axboe044c1ab2019-10-28 09:15:33 -06004799 /*
4800 * Install ring fd as the very last thing, so we don't risk someone
4801 * having closed it before we finish setup
4802 */
4803 ret = io_uring_get_fd(ctx);
4804 if (ret < 0)
4805 goto err;
4806
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004807 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004808 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004809 return ret;
4810err:
4811 io_ring_ctx_wait_and_kill(ctx);
4812 return ret;
4813}
4814
4815/*
4816 * Sets up an aio uring context, and returns the fd. Applications asks for a
4817 * ring size, we return the actual sq/cq ring sizes (among other things) in the
4818 * params structure passed in.
4819 */
4820static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
4821{
4822 struct io_uring_params p;
4823 long ret;
4824 int i;
4825
4826 if (copy_from_user(&p, params, sizeof(p)))
4827 return -EFAULT;
4828 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
4829 if (p.resv[i])
4830 return -EINVAL;
4831 }
4832
Jens Axboe6c271ce2019-01-10 11:22:30 -07004833 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe33a107f2019-10-04 12:10:03 -06004834 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE))
Jens Axboe2b188cc2019-01-07 10:46:33 -07004835 return -EINVAL;
4836
4837 ret = io_uring_create(entries, &p);
4838 if (ret < 0)
4839 return ret;
4840
4841 if (copy_to_user(params, &p, sizeof(p)))
4842 return -EFAULT;
4843
4844 return ret;
4845}
4846
4847SYSCALL_DEFINE2(io_uring_setup, u32, entries,
4848 struct io_uring_params __user *, params)
4849{
4850 return io_uring_setup(entries, params);
4851}
4852
Jens Axboeedafcce2019-01-09 09:16:05 -07004853static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
4854 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06004855 __releases(ctx->uring_lock)
4856 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07004857{
4858 int ret;
4859
Jens Axboe35fa71a2019-04-22 10:23:23 -06004860 /*
4861 * We're inside the ring mutex, if the ref is already dying, then
4862 * someone else killed the ctx or is already going through
4863 * io_uring_register().
4864 */
4865 if (percpu_ref_is_dying(&ctx->refs))
4866 return -ENXIO;
4867
Jens Axboeedafcce2019-01-09 09:16:05 -07004868 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06004869
4870 /*
4871 * Drop uring mutex before waiting for references to exit. If another
4872 * thread is currently inside io_uring_enter() it might need to grab
4873 * the uring_lock to make progress. If we hold it here across the drain
4874 * wait, then we can deadlock. It's safe to drop the mutex here, since
4875 * no new references will come in after we've killed the percpu ref.
4876 */
4877 mutex_unlock(&ctx->uring_lock);
Jens Axboe206aefd2019-11-07 18:27:42 -07004878 wait_for_completion(&ctx->completions[0]);
Jens Axboeb19062a2019-04-15 10:49:38 -06004879 mutex_lock(&ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07004880
4881 switch (opcode) {
4882 case IORING_REGISTER_BUFFERS:
4883 ret = io_sqe_buffer_register(ctx, arg, nr_args);
4884 break;
4885 case IORING_UNREGISTER_BUFFERS:
4886 ret = -EINVAL;
4887 if (arg || nr_args)
4888 break;
4889 ret = io_sqe_buffer_unregister(ctx);
4890 break;
Jens Axboe6b063142019-01-10 22:13:58 -07004891 case IORING_REGISTER_FILES:
4892 ret = io_sqe_files_register(ctx, arg, nr_args);
4893 break;
4894 case IORING_UNREGISTER_FILES:
4895 ret = -EINVAL;
4896 if (arg || nr_args)
4897 break;
4898 ret = io_sqe_files_unregister(ctx);
4899 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06004900 case IORING_REGISTER_FILES_UPDATE:
4901 ret = io_sqe_files_update(ctx, arg, nr_args);
4902 break;
Jens Axboe9b402842019-04-11 11:45:41 -06004903 case IORING_REGISTER_EVENTFD:
4904 ret = -EINVAL;
4905 if (nr_args != 1)
4906 break;
4907 ret = io_eventfd_register(ctx, arg);
4908 break;
4909 case IORING_UNREGISTER_EVENTFD:
4910 ret = -EINVAL;
4911 if (arg || nr_args)
4912 break;
4913 ret = io_eventfd_unregister(ctx);
4914 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07004915 default:
4916 ret = -EINVAL;
4917 break;
4918 }
4919
4920 /* bring the ctx back to life */
Jens Axboe206aefd2019-11-07 18:27:42 -07004921 reinit_completion(&ctx->completions[0]);
Jens Axboeedafcce2019-01-09 09:16:05 -07004922 percpu_ref_reinit(&ctx->refs);
4923 return ret;
4924}
4925
4926SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
4927 void __user *, arg, unsigned int, nr_args)
4928{
4929 struct io_ring_ctx *ctx;
4930 long ret = -EBADF;
4931 struct fd f;
4932
4933 f = fdget(fd);
4934 if (!f.file)
4935 return -EBADF;
4936
4937 ret = -EOPNOTSUPP;
4938 if (f.file->f_op != &io_uring_fops)
4939 goto out_fput;
4940
4941 ctx = f.file->private_data;
4942
4943 mutex_lock(&ctx->uring_lock);
4944 ret = __io_uring_register(ctx, opcode, arg, nr_args);
4945 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004946 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
4947 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07004948out_fput:
4949 fdput(f);
4950 return ret;
4951}
4952
Jens Axboe2b188cc2019-01-07 10:46:33 -07004953static int __init io_uring_init(void)
4954{
4955 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
4956 return 0;
4957};
4958__initcall(io_uring_init);