blob: 5cab7a0473174f6b3f5c24c0bbcd4a9aa7a86301 [file] [log] [blame]
Jens Axboe2b188cc2019-01-07 10:46:33 -07001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Shared application/kernel submission and completion ring pairs, for
4 * supporting fast/efficient IO.
5 *
6 * A note on the read/write ordering memory barriers that are matched between
Stefan Bühler1e84b972019-04-24 23:54:16 +02007 * the application and kernel side.
8 *
9 * After the application reads the CQ ring tail, it must use an
10 * appropriate smp_rmb() to pair with the smp_wmb() the kernel uses
11 * before writing the tail (using smp_load_acquire to read the tail will
12 * do). It also needs a smp_mb() before updating CQ head (ordering the
13 * entry load(s) with the head store), pairing with an implicit barrier
14 * through a control-dependency in io_get_cqring (smp_store_release to
15 * store head will do). Failure to do so could lead to reading invalid
16 * CQ entries.
17 *
18 * Likewise, the application must use an appropriate smp_wmb() before
19 * writing the SQ tail (ordering SQ entry stores with the tail store),
20 * which pairs with smp_load_acquire in io_get_sqring (smp_store_release
21 * to store the tail will do). And it needs a barrier ordering the SQ
22 * head load before writing new SQ entries (smp_load_acquire to read
23 * head will do).
24 *
25 * When using the SQ poll thread (IORING_SETUP_SQPOLL), the application
26 * needs to check the SQ flags for IORING_SQ_NEED_WAKEUP *after*
27 * updating the SQ tail; a full memory barrier smp_mb() is needed
28 * between.
Jens Axboe2b188cc2019-01-07 10:46:33 -070029 *
30 * Also see the examples in the liburing library:
31 *
32 * git://git.kernel.dk/liburing
33 *
34 * io_uring also uses READ/WRITE_ONCE() for _any_ store or load that happens
35 * from data shared between the kernel and application. This is done both
36 * for ordering purposes, but also to ensure that once a value is loaded from
37 * data that the application could potentially modify, it remains stable.
38 *
39 * Copyright (C) 2018-2019 Jens Axboe
Christoph Hellwigc992fe22019-01-11 09:43:02 -070040 * Copyright (c) 2018-2019 Christoph Hellwig
Jens Axboe2b188cc2019-01-07 10:46:33 -070041 */
42#include <linux/kernel.h>
43#include <linux/init.h>
44#include <linux/errno.h>
45#include <linux/syscalls.h>
46#include <linux/compat.h>
47#include <linux/refcount.h>
48#include <linux/uio.h>
49
50#include <linux/sched/signal.h>
51#include <linux/fs.h>
52#include <linux/file.h>
53#include <linux/fdtable.h>
54#include <linux/mm.h>
55#include <linux/mman.h>
56#include <linux/mmu_context.h>
57#include <linux/percpu.h>
58#include <linux/slab.h>
Jens Axboe6c271ce2019-01-10 11:22:30 -070059#include <linux/kthread.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070060#include <linux/blkdev.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070061#include <linux/bvec.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070062#include <linux/net.h>
63#include <net/sock.h>
64#include <net/af_unix.h>
Jens Axboe6b063142019-01-10 22:13:58 -070065#include <net/scm.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070066#include <linux/anon_inodes.h>
67#include <linux/sched/mm.h>
68#include <linux/uaccess.h>
69#include <linux/nospec.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070070#include <linux/sizes.h>
71#include <linux/hugetlb.h>
Jens Axboeaa4c3962019-11-29 10:14:00 -070072#include <linux/highmem.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070073
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020074#define CREATE_TRACE_POINTS
75#include <trace/events/io_uring.h>
76
Jens Axboe2b188cc2019-01-07 10:46:33 -070077#include <uapi/linux/io_uring.h>
78
79#include "internal.h"
Jens Axboe561fb042019-10-24 07:25:42 -060080#include "io-wq.h"
Jens Axboe2b188cc2019-01-07 10:46:33 -070081
Daniel Xu5277dea2019-09-14 14:23:45 -070082#define IORING_MAX_ENTRIES 32768
Jens Axboe33a107f2019-10-04 12:10:03 -060083#define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES)
Jens Axboe65e19f52019-10-26 07:20:21 -060084
85/*
86 * Shift of 9 is 512 entries, or exactly one page on 64-bit archs
87 */
88#define IORING_FILE_TABLE_SHIFT 9
89#define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT)
90#define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1)
91#define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE)
Jens Axboe2b188cc2019-01-07 10:46:33 -070092
93struct io_uring {
94 u32 head ____cacheline_aligned_in_smp;
95 u32 tail ____cacheline_aligned_in_smp;
96};
97
Stefan Bühler1e84b972019-04-24 23:54:16 +020098/*
Hristo Venev75b28af2019-08-26 17:23:46 +000099 * This data is shared with the application through the mmap at offsets
100 * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200101 *
102 * The offsets to the member fields are published through struct
103 * io_sqring_offsets when calling io_uring_setup.
104 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000105struct io_rings {
Stefan Bühler1e84b972019-04-24 23:54:16 +0200106 /*
107 * Head and tail offsets into the ring; the offsets need to be
108 * masked to get valid indices.
109 *
Hristo Venev75b28af2019-08-26 17:23:46 +0000110 * The kernel controls head of the sq ring and the tail of the cq ring,
111 * and the application controls tail of the sq ring and the head of the
112 * cq ring.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200113 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000114 struct io_uring sq, cq;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200115 /*
Hristo Venev75b28af2019-08-26 17:23:46 +0000116 * Bitmasks to apply to head and tail offsets (constant, equals
Stefan Bühler1e84b972019-04-24 23:54:16 +0200117 * ring_entries - 1)
118 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000119 u32 sq_ring_mask, cq_ring_mask;
120 /* Ring sizes (constant, power of 2) */
121 u32 sq_ring_entries, cq_ring_entries;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200122 /*
123 * Number of invalid entries dropped by the kernel due to
124 * invalid index stored in array
125 *
126 * Written by the kernel, shouldn't be modified by the
127 * application (i.e. get number of "new events" by comparing to
128 * cached value).
129 *
130 * After a new SQ head value was read by the application this
131 * counter includes all submissions that were dropped reaching
132 * the new SQ head (and possibly more).
133 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000134 u32 sq_dropped;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200135 /*
136 * Runtime flags
137 *
138 * Written by the kernel, shouldn't be modified by the
139 * application.
140 *
141 * The application needs a full memory barrier before checking
142 * for IORING_SQ_NEED_WAKEUP after updating the sq tail.
143 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000144 u32 sq_flags;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200145 /*
146 * Number of completion events lost because the queue was full;
147 * this should be avoided by the application by making sure
148 * there are not more requests pending thatn there is space in
149 * the completion queue.
150 *
151 * Written by the kernel, shouldn't be modified by the
152 * application (i.e. get number of "new events" by comparing to
153 * cached value).
154 *
155 * As completion events come in out of order this counter is not
156 * ordered with any other data.
157 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000158 u32 cq_overflow;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200159 /*
160 * Ring buffer of completion events.
161 *
162 * The kernel writes completion events fresh every time they are
163 * produced, so the application is allowed to modify pending
164 * entries.
165 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000166 struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700167};
168
Jens Axboeedafcce2019-01-09 09:16:05 -0700169struct io_mapped_ubuf {
170 u64 ubuf;
171 size_t len;
172 struct bio_vec *bvec;
173 unsigned int nr_bvecs;
174};
175
Jens Axboe65e19f52019-10-26 07:20:21 -0600176struct fixed_file_table {
177 struct file **files;
Jens Axboe31b51512019-01-18 22:56:34 -0700178};
179
Jens Axboe2b188cc2019-01-07 10:46:33 -0700180struct io_ring_ctx {
181 struct {
182 struct percpu_ref refs;
183 } ____cacheline_aligned_in_smp;
184
185 struct {
186 unsigned int flags;
187 bool compat;
188 bool account_mem;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700189 bool cq_overflow_flushed;
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +0300190 bool drain_next;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700191
Hristo Venev75b28af2019-08-26 17:23:46 +0000192 /*
193 * Ring buffer of indices into array of io_uring_sqe, which is
194 * mmapped by the application using the IORING_OFF_SQES offset.
195 *
196 * This indirection could e.g. be used to assign fixed
197 * io_uring_sqe entries to operations and only submit them to
198 * the queue when needed.
199 *
200 * The kernel modifies neither the indices array nor the entries
201 * array.
202 */
203 u32 *sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700204 unsigned cached_sq_head;
205 unsigned sq_entries;
206 unsigned sq_mask;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700207 unsigned sq_thread_idle;
Jens Axboe498ccd92019-10-25 10:04:25 -0600208 unsigned cached_sq_dropped;
Jens Axboe206aefd2019-11-07 18:27:42 -0700209 atomic_t cached_cq_overflow;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700210 struct io_uring_sqe *sq_sqes;
Jens Axboede0617e2019-04-06 21:51:27 -0600211
212 struct list_head defer_list;
Jens Axboe5262f562019-09-17 12:26:57 -0600213 struct list_head timeout_list;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700214 struct list_head cq_overflow_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700215
Jens Axboefcb323c2019-10-24 12:39:47 -0600216 wait_queue_head_t inflight_wait;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700217 } ____cacheline_aligned_in_smp;
218
Hristo Venev75b28af2019-08-26 17:23:46 +0000219 struct io_rings *rings;
220
Jens Axboe2b188cc2019-01-07 10:46:33 -0700221 /* IO offload */
Jens Axboe561fb042019-10-24 07:25:42 -0600222 struct io_wq *io_wq;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700223 struct task_struct *sqo_thread; /* if using sq thread polling */
224 struct mm_struct *sqo_mm;
225 wait_queue_head_t sqo_wait;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700226
Jens Axboe6b063142019-01-10 22:13:58 -0700227 /*
228 * If used, fixed file set. Writers must ensure that ->refs is dead,
229 * readers must ensure that ->refs is alive as long as the file* is
230 * used. Only updated through io_uring_register(2).
231 */
Jens Axboe65e19f52019-10-26 07:20:21 -0600232 struct fixed_file_table *file_table;
Jens Axboe6b063142019-01-10 22:13:58 -0700233 unsigned nr_user_files;
234
Jens Axboeedafcce2019-01-09 09:16:05 -0700235 /* if used, fixed mapped user buffers */
236 unsigned nr_user_bufs;
237 struct io_mapped_ubuf *user_bufs;
238
Jens Axboe2b188cc2019-01-07 10:46:33 -0700239 struct user_struct *user;
240
Jens Axboe0b8c0ec2019-12-02 08:50:00 -0700241 const struct cred *creds;
Jens Axboe181e4482019-11-25 08:52:30 -0700242
Jens Axboe206aefd2019-11-07 18:27:42 -0700243 /* 0 is for ctx quiesce/reinit/free, 1 is for sqo_thread started */
244 struct completion *completions;
245
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700246 /* if all else fails... */
247 struct io_kiocb *fallback_req;
248
Jens Axboe206aefd2019-11-07 18:27:42 -0700249#if defined(CONFIG_UNIX)
250 struct socket *ring_sock;
251#endif
252
253 struct {
254 unsigned cached_cq_tail;
255 unsigned cq_entries;
256 unsigned cq_mask;
257 atomic_t cq_timeouts;
258 struct wait_queue_head cq_wait;
259 struct fasync_struct *cq_fasync;
260 struct eventfd_ctx *cq_ev_fd;
261 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700262
263 struct {
264 struct mutex uring_lock;
265 wait_queue_head_t wait;
266 } ____cacheline_aligned_in_smp;
267
268 struct {
269 spinlock_t completion_lock;
Jens Axboedef596e2019-01-09 08:59:42 -0700270 bool poll_multi_file;
271 /*
272 * ->poll_list is protected by the ctx->uring_lock for
273 * io_uring instances that don't use IORING_SETUP_SQPOLL.
274 * For SQPOLL, only the single threaded io_sq_thread() will
275 * manipulate the list, hence no extra locking is needed there.
276 */
277 struct list_head poll_list;
Jens Axboeeac406c2019-11-14 12:09:58 -0700278 struct rb_root cancel_tree;
Jens Axboefcb323c2019-10-24 12:39:47 -0600279
280 spinlock_t inflight_lock;
281 struct list_head inflight_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700282 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700283};
284
Jens Axboe09bb8392019-03-13 12:39:28 -0600285/*
286 * First field must be the file pointer in all the
287 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
288 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700289struct io_poll_iocb {
290 struct file *file;
291 struct wait_queue_head *head;
292 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600293 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700294 bool canceled;
Jens Axboee9444752019-11-26 15:02:04 -0700295 struct wait_queue_entry *wait;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700296};
297
Jens Axboead8a48a2019-11-15 08:49:11 -0700298struct io_timeout_data {
299 struct io_kiocb *req;
300 struct hrtimer timer;
301 struct timespec64 ts;
302 enum hrtimer_mode mode;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +0300303 u32 seq_offset;
Jens Axboead8a48a2019-11-15 08:49:11 -0700304};
305
Jens Axboe5262f562019-09-17 12:26:57 -0600306struct io_timeout {
307 struct file *file;
Jens Axboead8a48a2019-11-15 08:49:11 -0700308 struct io_timeout_data *data;
Jens Axboe5262f562019-09-17 12:26:57 -0600309};
310
Jens Axboe09bb8392019-03-13 12:39:28 -0600311/*
312 * NOTE! Each of the iocb union members has the file pointer
313 * as the first entry in their struct definition. So you can
314 * access the file pointer through any of the sub-structs,
315 * or directly as just 'ki_filp' in this struct.
316 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700317struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700318 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600319 struct file *file;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700320 struct kiocb rw;
321 struct io_poll_iocb poll;
Jens Axboe5262f562019-09-17 12:26:57 -0600322 struct io_timeout timeout;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700323 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700324
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300325 const struct io_uring_sqe *sqe;
326 struct file *ring_file;
327 int ring_fd;
328 bool has_user;
329 bool in_async;
330 bool needs_fixed_file;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700331
332 struct io_ring_ctx *ctx;
Jens Axboeeac406c2019-11-14 12:09:58 -0700333 union {
334 struct list_head list;
335 struct rb_node rb_node;
336 };
Jens Axboe9e645e112019-05-10 16:07:28 -0600337 struct list_head link_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700338 unsigned int flags;
Jens Axboec16361c2019-01-17 08:39:48 -0700339 refcount_t refs;
Stefan Bühler8449eed2019-04-27 20:34:19 +0200340#define REQ_F_NOWAIT 1 /* must not punt to workers */
Jens Axboedef596e2019-01-09 08:59:42 -0700341#define REQ_F_IOPOLL_COMPLETED 2 /* polled IO has completed */
Jens Axboe6b063142019-01-10 22:13:58 -0700342#define REQ_F_FIXED_FILE 4 /* ctx owns file */
Jens Axboe4d7dd462019-11-20 13:03:52 -0700343#define REQ_F_LINK_NEXT 8 /* already grabbed next link */
Stefan Bühlere2033e32019-05-11 19:08:01 +0200344#define REQ_F_IO_DRAIN 16 /* drain existing IO first */
345#define REQ_F_IO_DRAINED 32 /* drain done */
Jens Axboe9e645e112019-05-10 16:07:28 -0600346#define REQ_F_LINK 64 /* linked sqes */
Jens Axboe2665abf2019-11-05 12:40:47 -0700347#define REQ_F_LINK_TIMEOUT 128 /* has linked timeout */
Zhengyuan Liuf7b76ac2019-07-16 23:26:14 +0800348#define REQ_F_FAIL_LINK 256 /* fail rest of links */
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +0300349#define REQ_F_DRAIN_LINK 512 /* link should be fully drained */
Jens Axboe5262f562019-09-17 12:26:57 -0600350#define REQ_F_TIMEOUT 1024 /* timeout request */
Jens Axboe491381ce2019-10-17 09:20:46 -0600351#define REQ_F_ISREG 2048 /* regular file */
352#define REQ_F_MUST_PUNT 4096 /* must be punted even for NONBLOCK */
Jens Axboe93bd25b2019-11-11 23:34:31 -0700353#define REQ_F_TIMEOUT_NOSEQ 8192 /* no timeout sequence */
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800354#define REQ_F_INFLIGHT 16384 /* on inflight list */
355#define REQ_F_COMP_LOCKED 32768 /* completion under lock */
Jens Axboe94ae5e72019-11-14 19:39:52 -0700356#define REQ_F_FREE_SQE 65536 /* free sqe if not async queued */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700357 u64 user_data;
Jens Axboe9e645e112019-05-10 16:07:28 -0600358 u32 result;
Jens Axboede0617e2019-04-06 21:51:27 -0600359 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700360
Jens Axboefcb323c2019-10-24 12:39:47 -0600361 struct list_head inflight_entry;
362
Jens Axboe561fb042019-10-24 07:25:42 -0600363 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700364};
365
366#define IO_PLUG_THRESHOLD 2
Jens Axboedef596e2019-01-09 08:59:42 -0700367#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700368
Jens Axboe9a56a232019-01-09 09:06:50 -0700369struct io_submit_state {
370 struct blk_plug plug;
371
372 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700373 * io_kiocb alloc cache
374 */
375 void *reqs[IO_IOPOLL_BATCH];
376 unsigned int free_reqs;
377 unsigned int cur_req;
378
379 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700380 * File reference cache
381 */
382 struct file *file;
383 unsigned int fd;
384 unsigned int has_refs;
385 unsigned int used_refs;
386 unsigned int ios_left;
387};
388
Jens Axboe561fb042019-10-24 07:25:42 -0600389static void io_wq_submit_work(struct io_wq_work **workptr);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700390static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liu4fe2c962019-09-09 20:50:40 +0800391static void __io_free_req(struct io_kiocb *req);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800392static void io_put_req(struct io_kiocb *req);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700393static void io_double_put_req(struct io_kiocb *req);
Jens Axboe978db572019-11-14 22:39:04 -0700394static void __io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700395static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
396static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboede0617e2019-04-06 21:51:27 -0600397
Jens Axboe2b188cc2019-01-07 10:46:33 -0700398static struct kmem_cache *req_cachep;
399
400static const struct file_operations io_uring_fops;
401
402struct sock *io_uring_get_socket(struct file *file)
403{
404#if defined(CONFIG_UNIX)
405 if (file->f_op == &io_uring_fops) {
406 struct io_ring_ctx *ctx = file->private_data;
407
408 return ctx->ring_sock->sk;
409 }
410#endif
411 return NULL;
412}
413EXPORT_SYMBOL(io_uring_get_socket);
414
415static void io_ring_ctx_ref_free(struct percpu_ref *ref)
416{
417 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
418
Jens Axboe206aefd2019-11-07 18:27:42 -0700419 complete(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700420}
421
422static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
423{
424 struct io_ring_ctx *ctx;
425
426 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
427 if (!ctx)
428 return NULL;
429
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700430 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
431 if (!ctx->fallback_req)
432 goto err;
433
Jens Axboe206aefd2019-11-07 18:27:42 -0700434 ctx->completions = kmalloc(2 * sizeof(struct completion), GFP_KERNEL);
435 if (!ctx->completions)
436 goto err;
437
Roman Gushchin21482892019-05-07 10:01:48 -0700438 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -0700439 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
440 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700441
442 ctx->flags = p->flags;
443 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700444 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe206aefd2019-11-07 18:27:42 -0700445 init_completion(&ctx->completions[0]);
446 init_completion(&ctx->completions[1]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700447 mutex_init(&ctx->uring_lock);
448 init_waitqueue_head(&ctx->wait);
449 spin_lock_init(&ctx->completion_lock);
Jens Axboedef596e2019-01-09 08:59:42 -0700450 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboeeac406c2019-11-14 12:09:58 -0700451 ctx->cancel_tree = RB_ROOT;
Jens Axboede0617e2019-04-06 21:51:27 -0600452 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -0600453 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -0600454 init_waitqueue_head(&ctx->inflight_wait);
455 spin_lock_init(&ctx->inflight_lock);
456 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700457 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -0700458err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700459 if (ctx->fallback_req)
460 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe206aefd2019-11-07 18:27:42 -0700461 kfree(ctx->completions);
462 kfree(ctx);
463 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700464}
465
Bob Liu9d858b22019-11-13 18:06:25 +0800466static inline bool __req_need_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -0600467{
Jackie Liua197f662019-11-08 08:09:12 -0700468 struct io_ring_ctx *ctx = req->ctx;
469
Jens Axboe498ccd92019-10-25 10:04:25 -0600470 return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped
471 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboede0617e2019-04-06 21:51:27 -0600472}
473
Bob Liu9d858b22019-11-13 18:06:25 +0800474static inline bool req_need_defer(struct io_kiocb *req)
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600475{
Bob Liu9d858b22019-11-13 18:06:25 +0800476 if ((req->flags & (REQ_F_IO_DRAIN|REQ_F_IO_DRAINED)) == REQ_F_IO_DRAIN)
477 return __req_need_defer(req);
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600478
Bob Liu9d858b22019-11-13 18:06:25 +0800479 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600480}
481
482static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -0600483{
484 struct io_kiocb *req;
485
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600486 req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list);
Bob Liu9d858b22019-11-13 18:06:25 +0800487 if (req && !req_need_defer(req)) {
Jens Axboede0617e2019-04-06 21:51:27 -0600488 list_del_init(&req->list);
489 return req;
490 }
491
492 return NULL;
493}
494
Jens Axboe5262f562019-09-17 12:26:57 -0600495static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx)
496{
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600497 struct io_kiocb *req;
498
499 req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list);
Jens Axboe93bd25b2019-11-11 23:34:31 -0700500 if (req) {
501 if (req->flags & REQ_F_TIMEOUT_NOSEQ)
502 return NULL;
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800503 if (!__req_need_defer(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -0700504 list_del_init(&req->list);
505 return req;
506 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600507 }
508
509 return NULL;
Jens Axboe5262f562019-09-17 12:26:57 -0600510}
511
Jens Axboede0617e2019-04-06 21:51:27 -0600512static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700513{
Hristo Venev75b28af2019-08-26 17:23:46 +0000514 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700515
Hristo Venev75b28af2019-08-26 17:23:46 +0000516 if (ctx->cached_cq_tail != READ_ONCE(rings->cq.tail)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700517 /* order cqe stores with ring update */
Hristo Venev75b28af2019-08-26 17:23:46 +0000518 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700519
Jens Axboe2b188cc2019-01-07 10:46:33 -0700520 if (wq_has_sleeper(&ctx->cq_wait)) {
521 wake_up_interruptible(&ctx->cq_wait);
522 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
523 }
524 }
525}
526
Jens Axboe561fb042019-10-24 07:25:42 -0600527static inline bool io_sqe_needs_user(const struct io_uring_sqe *sqe)
Jens Axboe18d9be12019-09-10 09:13:05 -0600528{
Jens Axboe561fb042019-10-24 07:25:42 -0600529 u8 opcode = READ_ONCE(sqe->opcode);
530
531 return !(opcode == IORING_OP_READ_FIXED ||
532 opcode == IORING_OP_WRITE_FIXED);
533}
534
Jens Axboe94ae5e72019-11-14 19:39:52 -0700535static inline bool io_prep_async_work(struct io_kiocb *req,
536 struct io_kiocb **link)
Jens Axboe561fb042019-10-24 07:25:42 -0600537{
538 bool do_hashed = false;
Jens Axboe54a91f32019-09-10 09:15:04 -0600539
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300540 if (req->sqe) {
541 switch (req->sqe->opcode) {
Jens Axboe6cc47d12019-09-18 11:18:23 -0600542 case IORING_OP_WRITEV:
543 case IORING_OP_WRITE_FIXED:
Jens Axboe561fb042019-10-24 07:25:42 -0600544 do_hashed = true;
Jens Axboe5f8fd2d32019-11-07 10:57:36 -0700545 /* fall-through */
546 case IORING_OP_READV:
547 case IORING_OP_READ_FIXED:
548 case IORING_OP_SENDMSG:
549 case IORING_OP_RECVMSG:
550 case IORING_OP_ACCEPT:
551 case IORING_OP_POLL_ADD:
Jens Axboef8e85cf2019-11-23 14:24:24 -0700552 case IORING_OP_CONNECT:
Jens Axboe5f8fd2d32019-11-07 10:57:36 -0700553 /*
554 * We know REQ_F_ISREG is not set on some of these
555 * opcodes, but this enables us to keep the check in
556 * just one place.
557 */
558 if (!(req->flags & REQ_F_ISREG))
559 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe6cc47d12019-09-18 11:18:23 -0600560 break;
561 }
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300562 if (io_sqe_needs_user(req->sqe))
Jens Axboe561fb042019-10-24 07:25:42 -0600563 req->work.flags |= IO_WQ_WORK_NEEDS_USER;
Jens Axboe54a91f32019-09-10 09:15:04 -0600564 }
565
Jens Axboe94ae5e72019-11-14 19:39:52 -0700566 *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -0600567 return do_hashed;
568}
569
Jackie Liua197f662019-11-08 08:09:12 -0700570static inline void io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -0600571{
Jackie Liua197f662019-11-08 08:09:12 -0700572 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe94ae5e72019-11-14 19:39:52 -0700573 struct io_kiocb *link;
574 bool do_hashed;
575
576 do_hashed = io_prep_async_work(req, &link);
Jens Axboe561fb042019-10-24 07:25:42 -0600577
578 trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work,
579 req->flags);
580 if (!do_hashed) {
581 io_wq_enqueue(ctx->io_wq, &req->work);
582 } else {
583 io_wq_enqueue_hashed(ctx->io_wq, &req->work,
584 file_inode(req->file));
585 }
Jens Axboe94ae5e72019-11-14 19:39:52 -0700586
587 if (link)
588 io_queue_linked_timeout(link);
Jens Axboe18d9be12019-09-10 09:13:05 -0600589}
590
Jens Axboe5262f562019-09-17 12:26:57 -0600591static void io_kill_timeout(struct io_kiocb *req)
592{
593 int ret;
594
Jens Axboead8a48a2019-11-15 08:49:11 -0700595 ret = hrtimer_try_to_cancel(&req->timeout.data->timer);
Jens Axboe5262f562019-09-17 12:26:57 -0600596 if (ret != -1) {
597 atomic_inc(&req->ctx->cq_timeouts);
Jens Axboe842f9612019-10-29 12:34:10 -0600598 list_del_init(&req->list);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700599 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800600 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -0600601 }
602}
603
604static void io_kill_timeouts(struct io_ring_ctx *ctx)
605{
606 struct io_kiocb *req, *tmp;
607
608 spin_lock_irq(&ctx->completion_lock);
609 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
610 io_kill_timeout(req);
611 spin_unlock_irq(&ctx->completion_lock);
612}
613
Jens Axboede0617e2019-04-06 21:51:27 -0600614static void io_commit_cqring(struct io_ring_ctx *ctx)
615{
616 struct io_kiocb *req;
617
Jens Axboe5262f562019-09-17 12:26:57 -0600618 while ((req = io_get_timeout_req(ctx)) != NULL)
619 io_kill_timeout(req);
620
Jens Axboede0617e2019-04-06 21:51:27 -0600621 __io_commit_cqring(ctx);
622
623 while ((req = io_get_deferred_req(ctx)) != NULL) {
624 req->flags |= REQ_F_IO_DRAINED;
Jackie Liua197f662019-11-08 08:09:12 -0700625 io_queue_async_work(req);
Jens Axboede0617e2019-04-06 21:51:27 -0600626 }
627}
628
Jens Axboe2b188cc2019-01-07 10:46:33 -0700629static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
630{
Hristo Venev75b28af2019-08-26 17:23:46 +0000631 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700632 unsigned tail;
633
634 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +0200635 /*
636 * writes to the cq entry need to come after reading head; the
637 * control dependency is enough as we're using WRITE_ONCE to
638 * fill the cq entry
639 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000640 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700641 return NULL;
642
643 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +0000644 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -0700645}
646
Jens Axboe8c838782019-03-12 15:48:16 -0600647static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
648{
649 if (waitqueue_active(&ctx->wait))
650 wake_up(&ctx->wait);
651 if (waitqueue_active(&ctx->sqo_wait))
652 wake_up(&ctx->sqo_wait);
Jens Axboe9b402842019-04-11 11:45:41 -0600653 if (ctx->cq_ev_fd)
654 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -0600655}
656
Jens Axboec4a2ed72019-11-21 21:01:26 -0700657/* Returns true if there are no backlogged entries after the flush */
658static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700659{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700660 struct io_rings *rings = ctx->rings;
661 struct io_uring_cqe *cqe;
662 struct io_kiocb *req;
663 unsigned long flags;
664 LIST_HEAD(list);
665
666 if (!force) {
667 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -0700668 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700669 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
670 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -0700671 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700672 }
673
674 spin_lock_irqsave(&ctx->completion_lock, flags);
675
676 /* if force is set, the ring is going away. always drop after that */
677 if (force)
678 ctx->cq_overflow_flushed = true;
679
Jens Axboec4a2ed72019-11-21 21:01:26 -0700680 cqe = NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700681 while (!list_empty(&ctx->cq_overflow_list)) {
682 cqe = io_get_cqring(ctx);
683 if (!cqe && !force)
684 break;
685
686 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
687 list);
688 list_move(&req->list, &list);
689 if (cqe) {
690 WRITE_ONCE(cqe->user_data, req->user_data);
691 WRITE_ONCE(cqe->res, req->result);
692 WRITE_ONCE(cqe->flags, 0);
693 } else {
694 WRITE_ONCE(ctx->rings->cq_overflow,
695 atomic_inc_return(&ctx->cached_cq_overflow));
696 }
697 }
698
699 io_commit_cqring(ctx);
700 spin_unlock_irqrestore(&ctx->completion_lock, flags);
701 io_cqring_ev_posted(ctx);
702
703 while (!list_empty(&list)) {
704 req = list_first_entry(&list, struct io_kiocb, list);
705 list_del(&req->list);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800706 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700707 }
Jens Axboec4a2ed72019-11-21 21:01:26 -0700708
709 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700710}
711
Jens Axboe78e19bb2019-11-06 15:21:34 -0700712static void io_cqring_fill_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700713{
Jens Axboe78e19bb2019-11-06 15:21:34 -0700714 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700715 struct io_uring_cqe *cqe;
716
Jens Axboe78e19bb2019-11-06 15:21:34 -0700717 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -0700718
Jens Axboe2b188cc2019-01-07 10:46:33 -0700719 /*
720 * If we can't get a cq entry, userspace overflowed the
721 * submission (by quite a lot). Increment the overflow count in
722 * the ring.
723 */
724 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700725 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -0700726 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700727 WRITE_ONCE(cqe->res, res);
728 WRITE_ONCE(cqe->flags, 0);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700729 } else if (ctx->cq_overflow_flushed) {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700730 WRITE_ONCE(ctx->rings->cq_overflow,
731 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700732 } else {
733 refcount_inc(&req->refs);
734 req->result = res;
735 list_add_tail(&req->list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700736 }
737}
738
Jens Axboe78e19bb2019-11-06 15:21:34 -0700739static void io_cqring_add_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700740{
Jens Axboe78e19bb2019-11-06 15:21:34 -0700741 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700742 unsigned long flags;
743
744 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700745 io_cqring_fill_event(req, res);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700746 io_commit_cqring(ctx);
747 spin_unlock_irqrestore(&ctx->completion_lock, flags);
748
Jens Axboe8c838782019-03-12 15:48:16 -0600749 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700750}
751
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700752static inline bool io_is_fallback_req(struct io_kiocb *req)
753{
754 return req == (struct io_kiocb *)
755 ((unsigned long) req->ctx->fallback_req & ~1UL);
756}
757
758static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
759{
760 struct io_kiocb *req;
761
762 req = ctx->fallback_req;
763 if (!test_and_set_bit_lock(0, (unsigned long *) ctx->fallback_req))
764 return req;
765
766 return NULL;
767}
768
Jens Axboe2579f912019-01-09 09:10:43 -0700769static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
770 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700771{
Jens Axboefd6fab22019-03-14 16:30:06 -0600772 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700773 struct io_kiocb *req;
774
775 if (!percpu_ref_tryget(&ctx->refs))
776 return NULL;
777
Jens Axboe2579f912019-01-09 09:10:43 -0700778 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -0600779 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -0700780 if (unlikely(!req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700781 goto fallback;
Jens Axboe2579f912019-01-09 09:10:43 -0700782 } else if (!state->free_reqs) {
783 size_t sz;
784 int ret;
785
786 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -0600787 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
788
789 /*
790 * Bulk alloc is all-or-nothing. If we fail to get a batch,
791 * retry single alloc to be on the safe side.
792 */
793 if (unlikely(ret <= 0)) {
794 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
795 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700796 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -0600797 ret = 1;
798 }
Jens Axboe2579f912019-01-09 09:10:43 -0700799 state->free_reqs = ret - 1;
800 state->cur_req = 1;
801 req = state->reqs[0];
802 } else {
803 req = state->reqs[state->cur_req];
804 state->free_reqs--;
805 state->cur_req++;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700806 }
807
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700808got_it:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300809 req->ring_file = NULL;
Jens Axboe60c112b2019-06-21 10:20:18 -0600810 req->file = NULL;
Jens Axboe2579f912019-01-09 09:10:43 -0700811 req->ctx = ctx;
812 req->flags = 0;
Jens Axboee65ef562019-03-12 10:16:44 -0600813 /* one is dropped after submission, the other at completion */
814 refcount_set(&req->refs, 2);
Jens Axboe9e645e112019-05-10 16:07:28 -0600815 req->result = 0;
Jens Axboe561fb042019-10-24 07:25:42 -0600816 INIT_IO_WORK(&req->work, io_wq_submit_work);
Jens Axboe2579f912019-01-09 09:10:43 -0700817 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700818fallback:
819 req = io_get_fallback_req(ctx);
820 if (req)
821 goto got_it;
Pavel Begunkov6805b322019-10-08 02:18:42 +0300822 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700823 return NULL;
824}
825
Jens Axboedef596e2019-01-09 08:59:42 -0700826static void io_free_req_many(struct io_ring_ctx *ctx, void **reqs, int *nr)
827{
828 if (*nr) {
829 kmem_cache_free_bulk(req_cachep, *nr, reqs);
Pavel Begunkov6805b322019-10-08 02:18:42 +0300830 percpu_ref_put_many(&ctx->refs, *nr);
Jens Axboedef596e2019-01-09 08:59:42 -0700831 *nr = 0;
832 }
833}
834
Jens Axboe9e645e112019-05-10 16:07:28 -0600835static void __io_free_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700836{
Jens Axboefcb323c2019-10-24 12:39:47 -0600837 struct io_ring_ctx *ctx = req->ctx;
838
Pavel Begunkovbbad27b2019-11-19 23:32:47 +0300839 if (req->flags & REQ_F_FREE_SQE)
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300840 kfree(req->sqe);
Jens Axboe09bb8392019-03-13 12:39:28 -0600841 if (req->file && !(req->flags & REQ_F_FIXED_FILE))
842 fput(req->file);
Jens Axboefcb323c2019-10-24 12:39:47 -0600843 if (req->flags & REQ_F_INFLIGHT) {
844 unsigned long flags;
845
846 spin_lock_irqsave(&ctx->inflight_lock, flags);
847 list_del(&req->inflight_entry);
848 if (waitqueue_active(&ctx->inflight_wait))
849 wake_up(&ctx->inflight_wait);
850 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
851 }
Jens Axboead8a48a2019-11-15 08:49:11 -0700852 if (req->flags & REQ_F_TIMEOUT)
853 kfree(req->timeout.data);
Jens Axboefcb323c2019-10-24 12:39:47 -0600854 percpu_ref_put(&ctx->refs);
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700855 if (likely(!io_is_fallback_req(req)))
856 kmem_cache_free(req_cachep, req);
857 else
858 clear_bit_unlock(0, (unsigned long *) ctx->fallback_req);
Jens Axboee65ef562019-03-12 10:16:44 -0600859}
860
Jackie Liua197f662019-11-08 08:09:12 -0700861static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -0600862{
Jackie Liua197f662019-11-08 08:09:12 -0700863 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -0700864 int ret;
865
Jens Axboead8a48a2019-11-15 08:49:11 -0700866 ret = hrtimer_try_to_cancel(&req->timeout.data->timer);
Jens Axboe2665abf2019-11-05 12:40:47 -0700867 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -0700868 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -0700869 io_commit_cqring(ctx);
870 req->flags &= ~REQ_F_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +0800871 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -0700872 return true;
873 }
874
875 return false;
876}
877
Jens Axboeba816ad2019-09-28 11:36:45 -0600878static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -0600879{
Jens Axboe2665abf2019-11-05 12:40:47 -0700880 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -0600881 struct io_kiocb *nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -0700882 bool wake_ev = false;
Jens Axboe9e645e112019-05-10 16:07:28 -0600883
Jens Axboe4d7dd462019-11-20 13:03:52 -0700884 /* Already got next link */
885 if (req->flags & REQ_F_LINK_NEXT)
886 return;
887
Jens Axboe9e645e112019-05-10 16:07:28 -0600888 /*
889 * The list should never be empty when we are called here. But could
890 * potentially happen if the chain is messed up, check to be on the
891 * safe side.
892 */
893 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, list);
Jens Axboe2665abf2019-11-05 12:40:47 -0700894 while (nxt) {
Jens Axboe76a46e02019-11-10 23:34:16 -0700895 list_del_init(&nxt->list);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700896
897 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
898 (nxt->flags & REQ_F_TIMEOUT)) {
899 wake_ev |= io_link_cancel_timeout(nxt);
900 nxt = list_first_entry_or_null(&req->link_list,
901 struct io_kiocb, list);
902 req->flags &= ~REQ_F_LINK_TIMEOUT;
903 continue;
904 }
Jens Axboe9e645e112019-05-10 16:07:28 -0600905 if (!list_empty(&req->link_list)) {
906 INIT_LIST_HEAD(&nxt->link_list);
907 list_splice(&req->link_list, &nxt->link_list);
908 nxt->flags |= REQ_F_LINK;
909 }
910
Pavel Begunkovb18fdf72019-11-21 23:21:02 +0300911 *nxtptr = nxt;
Jens Axboe94ae5e72019-11-14 19:39:52 -0700912 break;
Jens Axboe9e645e112019-05-10 16:07:28 -0600913 }
Jens Axboe2665abf2019-11-05 12:40:47 -0700914
Jens Axboe4d7dd462019-11-20 13:03:52 -0700915 req->flags |= REQ_F_LINK_NEXT;
Jens Axboe2665abf2019-11-05 12:40:47 -0700916 if (wake_ev)
917 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -0600918}
919
920/*
921 * Called if REQ_F_LINK is set, and we fail the head request
922 */
923static void io_fail_links(struct io_kiocb *req)
924{
Jens Axboe2665abf2019-11-05 12:40:47 -0700925 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -0600926 struct io_kiocb *link;
Jens Axboe2665abf2019-11-05 12:40:47 -0700927 unsigned long flags;
928
929 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -0600930
931 while (!list_empty(&req->link_list)) {
932 link = list_first_entry(&req->link_list, struct io_kiocb, list);
Jens Axboe2665abf2019-11-05 12:40:47 -0700933 list_del_init(&link->list);
Jens Axboe9e645e112019-05-10 16:07:28 -0600934
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +0200935 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -0700936
937 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300938 link->sqe->opcode == IORING_OP_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -0700939 io_link_cancel_timeout(link);
Jens Axboe2665abf2019-11-05 12:40:47 -0700940 } else {
Jens Axboe78e19bb2019-11-06 15:21:34 -0700941 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe978db572019-11-14 22:39:04 -0700942 __io_double_put_req(link);
Jens Axboe2665abf2019-11-05 12:40:47 -0700943 }
Jens Axboe5d960722019-11-19 15:31:28 -0700944 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -0600945 }
Jens Axboe2665abf2019-11-05 12:40:47 -0700946
947 io_commit_cqring(ctx);
948 spin_unlock_irqrestore(&ctx->completion_lock, flags);
949 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -0600950}
951
Jens Axboe4d7dd462019-11-20 13:03:52 -0700952static void io_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -0600953{
Jens Axboe4d7dd462019-11-20 13:03:52 -0700954 if (likely(!(req->flags & REQ_F_LINK)))
Jens Axboe2665abf2019-11-05 12:40:47 -0700955 return;
Jens Axboe2665abf2019-11-05 12:40:47 -0700956
Jens Axboe9e645e112019-05-10 16:07:28 -0600957 /*
958 * If LINK is set, we have dependent requests in this chain. If we
959 * didn't fail this request, queue the first one up, moving any other
960 * dependencies to the next request. In case of failure, fail the rest
961 * of the chain.
962 */
Jens Axboe2665abf2019-11-05 12:40:47 -0700963 if (req->flags & REQ_F_FAIL_LINK) {
964 io_fail_links(req);
Jens Axboe7c9e7f02019-11-12 08:15:53 -0700965 } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) ==
966 REQ_F_LINK_TIMEOUT) {
Jens Axboe2665abf2019-11-05 12:40:47 -0700967 struct io_ring_ctx *ctx = req->ctx;
968 unsigned long flags;
969
970 /*
971 * If this is a timeout link, we could be racing with the
972 * timeout timer. Grab the completion lock for this case to
Jens Axboe7c9e7f02019-11-12 08:15:53 -0700973 * protect against that.
Jens Axboe2665abf2019-11-05 12:40:47 -0700974 */
975 spin_lock_irqsave(&ctx->completion_lock, flags);
976 io_req_link_next(req, nxt);
977 spin_unlock_irqrestore(&ctx->completion_lock, flags);
978 } else {
979 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -0600980 }
Jens Axboe4d7dd462019-11-20 13:03:52 -0700981}
Jens Axboe9e645e112019-05-10 16:07:28 -0600982
Jackie Liuc69f8db2019-11-09 11:00:08 +0800983static void io_free_req(struct io_kiocb *req)
984{
Pavel Begunkov944e58b2019-11-21 23:21:01 +0300985 struct io_kiocb *nxt = NULL;
986
987 io_req_find_next(req, &nxt);
Pavel Begunkov70cf9f32019-11-21 23:21:00 +0300988 __io_free_req(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +0300989
990 if (nxt)
991 io_queue_async_work(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +0800992}
993
Jens Axboeba816ad2019-09-28 11:36:45 -0600994/*
995 * Drop reference to request, return next in chain (if there is one) if this
996 * was the last reference to this request.
997 */
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +0300998__attribute__((nonnull))
Jackie Liuec9c02a2019-11-08 23:50:36 +0800999static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboee65ef562019-03-12 10:16:44 -06001000{
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001001 io_req_find_next(req, nxtptr);
Jens Axboe4d7dd462019-11-20 13:03:52 -07001002
Jens Axboee65ef562019-03-12 10:16:44 -06001003 if (refcount_dec_and_test(&req->refs))
Jens Axboe4d7dd462019-11-20 13:03:52 -07001004 __io_free_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001005}
1006
Jens Axboe2b188cc2019-01-07 10:46:33 -07001007static void io_put_req(struct io_kiocb *req)
1008{
Jens Axboedef596e2019-01-09 08:59:42 -07001009 if (refcount_dec_and_test(&req->refs))
1010 io_free_req(req);
1011}
1012
Jens Axboe978db572019-11-14 22:39:04 -07001013/*
1014 * Must only be used if we don't need to care about links, usually from
1015 * within the completion handling itself.
1016 */
1017static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06001018{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001019 /* drop both submit and complete references */
1020 if (refcount_sub_and_test(2, &req->refs))
1021 __io_free_req(req);
1022}
1023
Jens Axboe978db572019-11-14 22:39:04 -07001024static void io_double_put_req(struct io_kiocb *req)
1025{
1026 /* drop both submit and complete references */
1027 if (refcount_sub_and_test(2, &req->refs))
1028 io_free_req(req);
1029}
1030
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001031static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06001032{
Jens Axboe84f97dc2019-11-06 11:27:53 -07001033 struct io_rings *rings = ctx->rings;
1034
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001035 /*
1036 * noflush == true is from the waitqueue handler, just ensure we wake
1037 * up the task, and the next invocation will flush the entries. We
1038 * cannot safely to it from here.
1039 */
1040 if (noflush && !list_empty(&ctx->cq_overflow_list))
1041 return -1U;
1042
1043 io_cqring_overflow_flush(ctx, false);
1044
Jens Axboea3a0e432019-08-20 11:03:11 -06001045 /* See comment at the top of this file */
1046 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00001047 return READ_ONCE(rings->cq.tail) - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06001048}
1049
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03001050static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1051{
1052 struct io_rings *rings = ctx->rings;
1053
1054 /* make sure SQ entry isn't read before tail */
1055 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1056}
1057
Jens Axboedef596e2019-01-09 08:59:42 -07001058/*
1059 * Find and free completed poll iocbs
1060 */
1061static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
1062 struct list_head *done)
1063{
1064 void *reqs[IO_IOPOLL_BATCH];
1065 struct io_kiocb *req;
Jens Axboe09bb8392019-03-13 12:39:28 -06001066 int to_free;
Jens Axboedef596e2019-01-09 08:59:42 -07001067
Jens Axboe09bb8392019-03-13 12:39:28 -06001068 to_free = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001069 while (!list_empty(done)) {
1070 req = list_first_entry(done, struct io_kiocb, list);
1071 list_del(&req->list);
1072
Jens Axboe78e19bb2019-11-06 15:21:34 -07001073 io_cqring_fill_event(req, req->result);
Jens Axboedef596e2019-01-09 08:59:42 -07001074 (*nr_events)++;
1075
Jens Axboe09bb8392019-03-13 12:39:28 -06001076 if (refcount_dec_and_test(&req->refs)) {
1077 /* If we're not using fixed files, we have to pair the
1078 * completion part with the file put. Use regular
1079 * completions for those, only batch free for fixed
Jens Axboe9e645e112019-05-10 16:07:28 -06001080 * file and non-linked commands.
Jens Axboe09bb8392019-03-13 12:39:28 -06001081 */
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03001082 if (((req->flags &
1083 (REQ_F_FIXED_FILE|REQ_F_LINK|REQ_F_FREE_SQE)) ==
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001084 REQ_F_FIXED_FILE) && !io_is_fallback_req(req)) {
Jens Axboe09bb8392019-03-13 12:39:28 -06001085 reqs[to_free++] = req;
1086 if (to_free == ARRAY_SIZE(reqs))
1087 io_free_req_many(ctx, reqs, &to_free);
Jens Axboe6b063142019-01-10 22:13:58 -07001088 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001089 io_free_req(req);
Jens Axboe6b063142019-01-10 22:13:58 -07001090 }
Jens Axboe9a56a232019-01-09 09:06:50 -07001091 }
Jens Axboedef596e2019-01-09 08:59:42 -07001092 }
Jens Axboedef596e2019-01-09 08:59:42 -07001093
Jens Axboe09bb8392019-03-13 12:39:28 -06001094 io_commit_cqring(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07001095 io_free_req_many(ctx, reqs, &to_free);
1096}
1097
1098static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1099 long min)
1100{
1101 struct io_kiocb *req, *tmp;
1102 LIST_HEAD(done);
1103 bool spin;
1104 int ret;
1105
1106 /*
1107 * Only spin for completions if we don't have multiple devices hanging
1108 * off our complete list, and we're under the requested amount.
1109 */
1110 spin = !ctx->poll_multi_file && *nr_events < min;
1111
1112 ret = 0;
1113 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
1114 struct kiocb *kiocb = &req->rw;
1115
1116 /*
1117 * Move completed entries to our local list. If we find a
1118 * request that requires polling, break out and complete
1119 * the done list first, if we have entries there.
1120 */
1121 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
1122 list_move_tail(&req->list, &done);
1123 continue;
1124 }
1125 if (!list_empty(&done))
1126 break;
1127
1128 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1129 if (ret < 0)
1130 break;
1131
1132 if (ret && spin)
1133 spin = false;
1134 ret = 0;
1135 }
1136
1137 if (!list_empty(&done))
1138 io_iopoll_complete(ctx, nr_events, &done);
1139
1140 return ret;
1141}
1142
1143/*
1144 * Poll for a mininum of 'min' events. Note that if min == 0 we consider that a
1145 * non-spinning poll check - we'll still enter the driver poll loop, but only
1146 * as a non-spinning completion check.
1147 */
1148static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
1149 long min)
1150{
Jens Axboe08f54392019-08-21 22:19:11 -06001151 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07001152 int ret;
1153
1154 ret = io_do_iopoll(ctx, nr_events, min);
1155 if (ret < 0)
1156 return ret;
1157 if (!min || *nr_events >= min)
1158 return 0;
1159 }
1160
1161 return 1;
1162}
1163
1164/*
1165 * We can't just wait for polled events to come to us, we have to actively
1166 * find and complete them.
1167 */
1168static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
1169{
1170 if (!(ctx->flags & IORING_SETUP_IOPOLL))
1171 return;
1172
1173 mutex_lock(&ctx->uring_lock);
1174 while (!list_empty(&ctx->poll_list)) {
1175 unsigned int nr_events = 0;
1176
1177 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -06001178
1179 /*
1180 * Ensure we allow local-to-the-cpu processing to take place,
1181 * in this case we need to ensure that we reap all events.
1182 */
1183 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -07001184 }
1185 mutex_unlock(&ctx->uring_lock);
1186}
1187
Jens Axboe2b2ed972019-10-25 10:06:15 -06001188static int __io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1189 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07001190{
Jens Axboe2b2ed972019-10-25 10:06:15 -06001191 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001192
1193 do {
1194 int tmin = 0;
1195
Jens Axboe500f9fb2019-08-19 12:15:59 -06001196 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06001197 * Don't enter poll loop if we already have events pending.
1198 * If we do, we can potentially be spinning for commands that
1199 * already triggered a CQE (eg in error).
1200 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001201 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06001202 break;
1203
1204 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06001205 * If a submit got punted to a workqueue, we can have the
1206 * application entering polling for a command before it gets
1207 * issued. That app will hold the uring_lock for the duration
1208 * of the poll right here, so we need to take a breather every
1209 * now and then to ensure that the issue has a chance to add
1210 * the poll to the issued list. Otherwise we can spin here
1211 * forever, while the workqueue is stuck trying to acquire the
1212 * very same mutex.
1213 */
1214 if (!(++iters & 7)) {
1215 mutex_unlock(&ctx->uring_lock);
1216 mutex_lock(&ctx->uring_lock);
1217 }
1218
Jens Axboedef596e2019-01-09 08:59:42 -07001219 if (*nr_events < min)
1220 tmin = min - *nr_events;
1221
1222 ret = io_iopoll_getevents(ctx, nr_events, tmin);
1223 if (ret <= 0)
1224 break;
1225 ret = 0;
1226 } while (min && !*nr_events && !need_resched());
1227
Jens Axboe2b2ed972019-10-25 10:06:15 -06001228 return ret;
1229}
1230
1231static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1232 long min)
1233{
1234 int ret;
1235
1236 /*
1237 * We disallow the app entering submit/complete with polling, but we
1238 * still need to lock the ring to prevent racing with polled issue
1239 * that got punted to a workqueue.
1240 */
1241 mutex_lock(&ctx->uring_lock);
1242 ret = __io_iopoll_check(ctx, nr_events, min);
Jens Axboe500f9fb2019-08-19 12:15:59 -06001243 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001244 return ret;
1245}
1246
Jens Axboe491381ce2019-10-17 09:20:46 -06001247static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001248{
Jens Axboe491381ce2019-10-17 09:20:46 -06001249 /*
1250 * Tell lockdep we inherited freeze protection from submission
1251 * thread.
1252 */
1253 if (req->flags & REQ_F_ISREG) {
1254 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001255
Jens Axboe491381ce2019-10-17 09:20:46 -06001256 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001257 }
Jens Axboe491381ce2019-10-17 09:20:46 -06001258 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001259}
1260
Jens Axboeba816ad2019-09-28 11:36:45 -06001261static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001262{
1263 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1264
Jens Axboe491381ce2019-10-17 09:20:46 -06001265 if (kiocb->ki_flags & IOCB_WRITE)
1266 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001267
Jens Axboe9e645e112019-05-10 16:07:28 -06001268 if ((req->flags & REQ_F_LINK) && res != req->result)
1269 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001270 io_cqring_add_event(req, res);
Jens Axboeba816ad2019-09-28 11:36:45 -06001271}
1272
1273static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1274{
1275 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1276
1277 io_complete_rw_common(kiocb, res);
Jens Axboee65ef562019-03-12 10:16:44 -06001278 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001279}
1280
Jens Axboeba816ad2019-09-28 11:36:45 -06001281static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res)
1282{
1283 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001284 struct io_kiocb *nxt = NULL;
Jens Axboeba816ad2019-09-28 11:36:45 -06001285
1286 io_complete_rw_common(kiocb, res);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001287 io_put_req_find_next(req, &nxt);
1288
1289 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001290}
1291
Jens Axboedef596e2019-01-09 08:59:42 -07001292static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1293{
1294 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1295
Jens Axboe491381ce2019-10-17 09:20:46 -06001296 if (kiocb->ki_flags & IOCB_WRITE)
1297 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001298
Jens Axboe9e645e112019-05-10 16:07:28 -06001299 if ((req->flags & REQ_F_LINK) && res != req->result)
1300 req->flags |= REQ_F_FAIL_LINK;
1301 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -07001302 if (res != -EAGAIN)
1303 req->flags |= REQ_F_IOPOLL_COMPLETED;
1304}
1305
1306/*
1307 * After the iocb has been issued, it's safe to be found on the poll list.
1308 * Adding the kiocb to the list AFTER submission ensures that we don't
1309 * find it from a io_iopoll_getevents() thread before the issuer is done
1310 * accessing the kiocb cookie.
1311 */
1312static void io_iopoll_req_issued(struct io_kiocb *req)
1313{
1314 struct io_ring_ctx *ctx = req->ctx;
1315
1316 /*
1317 * Track whether we have multiple files in our lists. This will impact
1318 * how we do polling eventually, not spinning if we're on potentially
1319 * different devices.
1320 */
1321 if (list_empty(&ctx->poll_list)) {
1322 ctx->poll_multi_file = false;
1323 } else if (!ctx->poll_multi_file) {
1324 struct io_kiocb *list_req;
1325
1326 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1327 list);
1328 if (list_req->rw.ki_filp != req->rw.ki_filp)
1329 ctx->poll_multi_file = true;
1330 }
1331
1332 /*
1333 * For fast devices, IO may have already completed. If it has, add
1334 * it to the front so we find it first.
1335 */
1336 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1337 list_add(&req->list, &ctx->poll_list);
1338 else
1339 list_add_tail(&req->list, &ctx->poll_list);
1340}
1341
Jens Axboe3d6770f2019-04-13 11:50:54 -06001342static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07001343{
Jens Axboe3d6770f2019-04-13 11:50:54 -06001344 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -07001345 int diff = state->has_refs - state->used_refs;
1346
1347 if (diff)
1348 fput_many(state->file, diff);
1349 state->file = NULL;
1350 }
1351}
1352
1353/*
1354 * Get as many references to a file as we have IOs left in this submission,
1355 * assuming most submissions are for one file, or at least that each file
1356 * has more than one submission.
1357 */
1358static struct file *io_file_get(struct io_submit_state *state, int fd)
1359{
1360 if (!state)
1361 return fget(fd);
1362
1363 if (state->file) {
1364 if (state->fd == fd) {
1365 state->used_refs++;
1366 state->ios_left--;
1367 return state->file;
1368 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06001369 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07001370 }
1371 state->file = fget_many(fd, state->ios_left);
1372 if (!state->file)
1373 return NULL;
1374
1375 state->fd = fd;
1376 state->has_refs = state->ios_left;
1377 state->used_refs = 1;
1378 state->ios_left--;
1379 return state->file;
1380}
1381
Jens Axboe2b188cc2019-01-07 10:46:33 -07001382/*
1383 * If we tracked the file through the SCM inflight mechanism, we could support
1384 * any file. For now, just ensure that anything potentially problematic is done
1385 * inline.
1386 */
1387static bool io_file_supports_async(struct file *file)
1388{
1389 umode_t mode = file_inode(file)->i_mode;
1390
1391 if (S_ISBLK(mode) || S_ISCHR(mode))
1392 return true;
1393 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
1394 return true;
1395
1396 return false;
1397}
1398
Pavel Begunkov267bc902019-11-07 01:41:08 +03001399static int io_prep_rw(struct io_kiocb *req, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001400{
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001401 const struct io_uring_sqe *sqe = req->sqe;
Jens Axboedef596e2019-01-09 08:59:42 -07001402 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001403 struct kiocb *kiocb = &req->rw;
Jens Axboe09bb8392019-03-13 12:39:28 -06001404 unsigned ioprio;
1405 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001406
Jens Axboe09bb8392019-03-13 12:39:28 -06001407 if (!req->file)
1408 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001409
Jens Axboe491381ce2019-10-17 09:20:46 -06001410 if (S_ISREG(file_inode(req->file)->i_mode))
1411 req->flags |= REQ_F_ISREG;
1412
1413 /*
1414 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
1415 * we know to async punt it even if it was opened O_NONBLOCK
1416 */
1417 if (force_nonblock && !io_file_supports_async(req->file)) {
1418 req->flags |= REQ_F_MUST_PUNT;
1419 return -EAGAIN;
1420 }
Jens Axboe6b063142019-01-10 22:13:58 -07001421
Jens Axboe2b188cc2019-01-07 10:46:33 -07001422 kiocb->ki_pos = READ_ONCE(sqe->off);
1423 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
1424 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
1425
1426 ioprio = READ_ONCE(sqe->ioprio);
1427 if (ioprio) {
1428 ret = ioprio_check_cap(ioprio);
1429 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06001430 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001431
1432 kiocb->ki_ioprio = ioprio;
1433 } else
1434 kiocb->ki_ioprio = get_current_ioprio();
1435
1436 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
1437 if (unlikely(ret))
Jens Axboe09bb8392019-03-13 12:39:28 -06001438 return ret;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001439
1440 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06001441 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
1442 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02001443 req->flags |= REQ_F_NOWAIT;
1444
1445 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001446 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001447
Jens Axboedef596e2019-01-09 08:59:42 -07001448 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07001449 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
1450 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06001451 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001452
Jens Axboedef596e2019-01-09 08:59:42 -07001453 kiocb->ki_flags |= IOCB_HIPRI;
1454 kiocb->ki_complete = io_complete_rw_iopoll;
Jens Axboe6873e0b2019-10-30 13:53:09 -06001455 req->result = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001456 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001457 if (kiocb->ki_flags & IOCB_HIPRI)
1458 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07001459 kiocb->ki_complete = io_complete_rw;
1460 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001461 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001462}
1463
1464static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
1465{
1466 switch (ret) {
1467 case -EIOCBQUEUED:
1468 break;
1469 case -ERESTARTSYS:
1470 case -ERESTARTNOINTR:
1471 case -ERESTARTNOHAND:
1472 case -ERESTART_RESTARTBLOCK:
1473 /*
1474 * We can't just restart the syscall, since previously
1475 * submitted sqes may already be in progress. Just fail this
1476 * IO with EINTR.
1477 */
1478 ret = -EINTR;
1479 /* fall through */
1480 default:
1481 kiocb->ki_complete(kiocb, ret, 0);
1482 }
1483}
1484
Jens Axboeba816ad2019-09-28 11:36:45 -06001485static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt,
1486 bool in_async)
1487{
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001488 if (in_async && ret >= 0 && kiocb->ki_complete == io_complete_rw)
Jens Axboeba816ad2019-09-28 11:36:45 -06001489 *nxt = __io_complete_rw(kiocb, ret);
1490 else
1491 io_rw_done(kiocb, ret);
1492}
1493
Pavel Begunkov7d009162019-11-25 23:14:40 +03001494static ssize_t io_import_fixed(struct io_ring_ctx *ctx, int rw,
1495 const struct io_uring_sqe *sqe,
1496 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07001497{
1498 size_t len = READ_ONCE(sqe->len);
1499 struct io_mapped_ubuf *imu;
1500 unsigned index, buf_index;
1501 size_t offset;
1502 u64 buf_addr;
1503
1504 /* attempt to use fixed buffers without having provided iovecs */
1505 if (unlikely(!ctx->user_bufs))
1506 return -EFAULT;
1507
1508 buf_index = READ_ONCE(sqe->buf_index);
1509 if (unlikely(buf_index >= ctx->nr_user_bufs))
1510 return -EFAULT;
1511
1512 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
1513 imu = &ctx->user_bufs[index];
1514 buf_addr = READ_ONCE(sqe->addr);
1515
1516 /* overflow */
1517 if (buf_addr + len < buf_addr)
1518 return -EFAULT;
1519 /* not inside the mapped region */
1520 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
1521 return -EFAULT;
1522
1523 /*
1524 * May not be a start of buffer, set size appropriately
1525 * and advance us to the beginning.
1526 */
1527 offset = buf_addr - imu->ubuf;
1528 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06001529
1530 if (offset) {
1531 /*
1532 * Don't use iov_iter_advance() here, as it's really slow for
1533 * using the latter parts of a big fixed buffer - it iterates
1534 * over each segment manually. We can cheat a bit here, because
1535 * we know that:
1536 *
1537 * 1) it's a BVEC iter, we set it up
1538 * 2) all bvecs are PAGE_SIZE in size, except potentially the
1539 * first and last bvec
1540 *
1541 * So just find our index, and adjust the iterator afterwards.
1542 * If the offset is within the first bvec (or the whole first
1543 * bvec, just use iov_iter_advance(). This makes it easier
1544 * since we can just skip the first segment, which may not
1545 * be PAGE_SIZE aligned.
1546 */
1547 const struct bio_vec *bvec = imu->bvec;
1548
1549 if (offset <= bvec->bv_len) {
1550 iov_iter_advance(iter, offset);
1551 } else {
1552 unsigned long seg_skip;
1553
1554 /* skip first vec */
1555 offset -= bvec->bv_len;
1556 seg_skip = 1 + (offset >> PAGE_SHIFT);
1557
1558 iter->bvec = bvec + seg_skip;
1559 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02001560 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001561 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001562 }
1563 }
1564
Jens Axboe5e559562019-11-13 16:12:46 -07001565 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001566}
1567
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001568static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
1569 struct iovec **iovec, struct iov_iter *iter)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001570{
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001571 const struct io_uring_sqe *sqe = req->sqe;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001572 void __user *buf = u64_to_user_ptr(READ_ONCE(sqe->addr));
1573 size_t sqe_len = READ_ONCE(sqe->len);
Jens Axboeedafcce2019-01-09 09:16:05 -07001574 u8 opcode;
1575
1576 /*
1577 * We're reading ->opcode for the second time, but the first read
1578 * doesn't care whether it's _FIXED or not, so it doesn't matter
1579 * whether ->opcode changes concurrently. The first read does care
1580 * about whether it is a READ or a WRITE, so we don't trust this read
1581 * for that purpose and instead let the caller pass in the read/write
1582 * flag.
1583 */
1584 opcode = READ_ONCE(sqe->opcode);
Pavel Begunkov7d009162019-11-25 23:14:40 +03001585 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07001586 *iovec = NULL;
Pavel Begunkov7d009162019-11-25 23:14:40 +03001587 return io_import_fixed(req->ctx, rw, sqe, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07001588 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001589
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001590 if (!req->has_user)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001591 return -EFAULT;
1592
1593#ifdef CONFIG_COMPAT
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001594 if (req->ctx->compat)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001595 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
1596 iovec, iter);
1597#endif
1598
1599 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
1600}
1601
Jens Axboe32960612019-09-23 11:05:34 -06001602/*
1603 * For files that don't have ->read_iter() and ->write_iter(), handle them
1604 * by looping over ->read() or ->write() manually.
1605 */
1606static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
1607 struct iov_iter *iter)
1608{
1609 ssize_t ret = 0;
1610
1611 /*
1612 * Don't support polled IO through this interface, and we can't
1613 * support non-blocking either. For the latter, this just causes
1614 * the kiocb to be handled from an async context.
1615 */
1616 if (kiocb->ki_flags & IOCB_HIPRI)
1617 return -EOPNOTSUPP;
1618 if (kiocb->ki_flags & IOCB_NOWAIT)
1619 return -EAGAIN;
1620
1621 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03001622 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06001623 ssize_t nr;
1624
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03001625 if (!iov_iter_is_bvec(iter)) {
1626 iovec = iov_iter_iovec(iter);
1627 } else {
1628 /* fixed buffers import bvec */
1629 iovec.iov_base = kmap(iter->bvec->bv_page)
1630 + iter->iov_offset;
1631 iovec.iov_len = min(iter->count,
1632 iter->bvec->bv_len - iter->iov_offset);
1633 }
1634
Jens Axboe32960612019-09-23 11:05:34 -06001635 if (rw == READ) {
1636 nr = file->f_op->read(file, iovec.iov_base,
1637 iovec.iov_len, &kiocb->ki_pos);
1638 } else {
1639 nr = file->f_op->write(file, iovec.iov_base,
1640 iovec.iov_len, &kiocb->ki_pos);
1641 }
1642
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03001643 if (iov_iter_is_bvec(iter))
1644 kunmap(iter->bvec->bv_page);
1645
Jens Axboe32960612019-09-23 11:05:34 -06001646 if (nr < 0) {
1647 if (!ret)
1648 ret = nr;
1649 break;
1650 }
1651 ret += nr;
1652 if (nr != iovec.iov_len)
1653 break;
1654 iov_iter_advance(iter, nr);
1655 }
1656
1657 return ret;
1658}
1659
Pavel Begunkov267bc902019-11-07 01:41:08 +03001660static int io_read(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001661 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001662{
1663 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1664 struct kiocb *kiocb = &req->rw;
1665 struct iov_iter iter;
1666 struct file *file;
Jens Axboe31b51512019-01-18 22:56:34 -07001667 size_t iov_count;
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001668 ssize_t read_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001669
Pavel Begunkov267bc902019-11-07 01:41:08 +03001670 ret = io_prep_rw(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001671 if (ret)
1672 return ret;
1673 file = kiocb->ki_filp;
1674
Jens Axboe2b188cc2019-01-07 10:46:33 -07001675 if (unlikely(!(file->f_mode & FMODE_READ)))
Jens Axboe09bb8392019-03-13 12:39:28 -06001676 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001677
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001678 ret = io_import_iovec(READ, req, &iovec, &iter);
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001679 if (ret < 0)
Jens Axboe09bb8392019-03-13 12:39:28 -06001680 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001681
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001682 read_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06001683 if (req->flags & REQ_F_LINK)
1684 req->result = read_size;
1685
Jens Axboe31b51512019-01-18 22:56:34 -07001686 iov_count = iov_iter_count(&iter);
1687 ret = rw_verify_area(READ, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001688 if (!ret) {
1689 ssize_t ret2;
1690
Jens Axboe32960612019-09-23 11:05:34 -06001691 if (file->f_op->read_iter)
1692 ret2 = call_read_iter(file, kiocb, &iter);
1693 else
1694 ret2 = loop_rw_iter(READ, file, kiocb, &iter);
1695
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001696 /*
1697 * In case of a short read, punt to async. This can happen
1698 * if we have data partially cached. Alternatively we can
1699 * return the short read, in which case the application will
1700 * need to issue another SQE and wait for it. That SQE will
1701 * need async punt anyway, so it's more efficient to do it
1702 * here.
1703 */
Jens Axboe491381ce2019-10-17 09:20:46 -06001704 if (force_nonblock && !(req->flags & REQ_F_NOWAIT) &&
1705 (req->flags & REQ_F_ISREG) &&
1706 ret2 > 0 && ret2 < read_size)
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001707 ret2 = -EAGAIN;
1708 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboe561fb042019-10-24 07:25:42 -06001709 if (!force_nonblock || ret2 != -EAGAIN)
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001710 kiocb_done(kiocb, ret2, nxt, req->in_async);
Jens Axboe561fb042019-10-24 07:25:42 -06001711 else
Jens Axboe2b188cc2019-01-07 10:46:33 -07001712 ret = -EAGAIN;
1713 }
1714 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001715 return ret;
1716}
1717
Pavel Begunkov267bc902019-11-07 01:41:08 +03001718static int io_write(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001719 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001720{
1721 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1722 struct kiocb *kiocb = &req->rw;
1723 struct iov_iter iter;
1724 struct file *file;
Jens Axboe31b51512019-01-18 22:56:34 -07001725 size_t iov_count;
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001726 ssize_t ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001727
Pavel Begunkov267bc902019-11-07 01:41:08 +03001728 ret = io_prep_rw(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001729 if (ret)
1730 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001731
Jens Axboe2b188cc2019-01-07 10:46:33 -07001732 file = kiocb->ki_filp;
1733 if (unlikely(!(file->f_mode & FMODE_WRITE)))
Jens Axboe09bb8392019-03-13 12:39:28 -06001734 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001735
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001736 ret = io_import_iovec(WRITE, req, &iovec, &iter);
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001737 if (ret < 0)
Jens Axboe09bb8392019-03-13 12:39:28 -06001738 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001739
Jens Axboe9e645e112019-05-10 16:07:28 -06001740 if (req->flags & REQ_F_LINK)
1741 req->result = ret;
1742
Jens Axboe31b51512019-01-18 22:56:34 -07001743 iov_count = iov_iter_count(&iter);
1744
1745 ret = -EAGAIN;
Jens Axboe561fb042019-10-24 07:25:42 -06001746 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT))
Jens Axboe31b51512019-01-18 22:56:34 -07001747 goto out_free;
Jens Axboe31b51512019-01-18 22:56:34 -07001748
1749 ret = rw_verify_area(WRITE, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001750 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01001751 ssize_t ret2;
1752
Jens Axboe2b188cc2019-01-07 10:46:33 -07001753 /*
1754 * Open-code file_start_write here to grab freeze protection,
1755 * which will be released by another thread in
1756 * io_complete_rw(). Fool lockdep by telling it the lock got
1757 * released so that it doesn't complain about the held lock when
1758 * we return to userspace.
1759 */
Jens Axboe491381ce2019-10-17 09:20:46 -06001760 if (req->flags & REQ_F_ISREG) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001761 __sb_start_write(file_inode(file)->i_sb,
1762 SB_FREEZE_WRITE, true);
1763 __sb_writers_release(file_inode(file)->i_sb,
1764 SB_FREEZE_WRITE);
1765 }
1766 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01001767
Jens Axboe32960612019-09-23 11:05:34 -06001768 if (file->f_op->write_iter)
1769 ret2 = call_write_iter(file, kiocb, &iter);
1770 else
1771 ret2 = loop_rw_iter(WRITE, file, kiocb, &iter);
Jens Axboe561fb042019-10-24 07:25:42 -06001772 if (!force_nonblock || ret2 != -EAGAIN)
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001773 kiocb_done(kiocb, ret2, nxt, req->in_async);
Jens Axboe561fb042019-10-24 07:25:42 -06001774 else
Roman Penyaev9bf79332019-03-25 20:09:24 +01001775 ret = -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001776 }
Jens Axboe31b51512019-01-18 22:56:34 -07001777out_free:
Jens Axboe2b188cc2019-01-07 10:46:33 -07001778 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001779 return ret;
1780}
1781
1782/*
1783 * IORING_OP_NOP just posts a completion event, nothing else.
1784 */
Jens Axboe78e19bb2019-11-06 15:21:34 -07001785static int io_nop(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001786{
1787 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001788
Jens Axboedef596e2019-01-09 08:59:42 -07001789 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1790 return -EINVAL;
1791
Jens Axboe78e19bb2019-11-06 15:21:34 -07001792 io_cqring_add_event(req, 0);
Jens Axboee65ef562019-03-12 10:16:44 -06001793 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001794 return 0;
1795}
1796
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001797static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1798{
Jens Axboe6b063142019-01-10 22:13:58 -07001799 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001800
Jens Axboe09bb8392019-03-13 12:39:28 -06001801 if (!req->file)
1802 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001803
Jens Axboe6b063142019-01-10 22:13:58 -07001804 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07001805 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07001806 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001807 return -EINVAL;
1808
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001809 return 0;
1810}
1811
1812static int io_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001813 struct io_kiocb **nxt, bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001814{
1815 loff_t sqe_off = READ_ONCE(sqe->off);
1816 loff_t sqe_len = READ_ONCE(sqe->len);
1817 loff_t end = sqe_off + sqe_len;
1818 unsigned fsync_flags;
1819 int ret;
1820
1821 fsync_flags = READ_ONCE(sqe->fsync_flags);
1822 if (unlikely(fsync_flags & ~IORING_FSYNC_DATASYNC))
1823 return -EINVAL;
1824
1825 ret = io_prep_fsync(req, sqe);
1826 if (ret)
1827 return ret;
1828
1829 /* fsync always requires a blocking context */
1830 if (force_nonblock)
1831 return -EAGAIN;
1832
1833 ret = vfs_fsync_range(req->rw.ki_filp, sqe_off,
1834 end > 0 ? end : LLONG_MAX,
1835 fsync_flags & IORING_FSYNC_DATASYNC);
1836
Jens Axboe9e645e112019-05-10 16:07:28 -06001837 if (ret < 0 && (req->flags & REQ_F_LINK))
1838 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001839 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001840 io_put_req_find_next(req, nxt);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001841 return 0;
1842}
1843
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001844static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1845{
1846 struct io_ring_ctx *ctx = req->ctx;
1847 int ret = 0;
1848
1849 if (!req->file)
1850 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001851
1852 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1853 return -EINVAL;
1854 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
1855 return -EINVAL;
1856
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001857 return ret;
1858}
1859
1860static int io_sync_file_range(struct io_kiocb *req,
1861 const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001862 struct io_kiocb **nxt,
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001863 bool force_nonblock)
1864{
1865 loff_t sqe_off;
1866 loff_t sqe_len;
1867 unsigned flags;
1868 int ret;
1869
1870 ret = io_prep_sfr(req, sqe);
1871 if (ret)
1872 return ret;
1873
1874 /* sync_file_range always requires a blocking context */
1875 if (force_nonblock)
1876 return -EAGAIN;
1877
1878 sqe_off = READ_ONCE(sqe->off);
1879 sqe_len = READ_ONCE(sqe->len);
1880 flags = READ_ONCE(sqe->sync_range_flags);
1881
1882 ret = sync_file_range(req->rw.ki_filp, sqe_off, sqe_len, flags);
1883
Jens Axboe9e645e112019-05-10 16:07:28 -06001884 if (ret < 0 && (req->flags & REQ_F_LINK))
1885 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001886 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001887 io_put_req_find_next(req, nxt);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001888 return 0;
1889}
1890
Jens Axboe0fa03c62019-04-19 13:34:07 -06001891#if defined(CONFIG_NET)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001892static int io_send_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001893 struct io_kiocb **nxt, bool force_nonblock,
Jens Axboeaa1fa282019-04-19 13:38:09 -06001894 long (*fn)(struct socket *, struct user_msghdr __user *,
1895 unsigned int))
1896{
Jens Axboe0fa03c62019-04-19 13:34:07 -06001897 struct socket *sock;
1898 int ret;
1899
1900 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1901 return -EINVAL;
1902
1903 sock = sock_from_file(req->file, &ret);
1904 if (sock) {
1905 struct user_msghdr __user *msg;
1906 unsigned flags;
1907
1908 flags = READ_ONCE(sqe->msg_flags);
1909 if (flags & MSG_DONTWAIT)
1910 req->flags |= REQ_F_NOWAIT;
1911 else if (force_nonblock)
1912 flags |= MSG_DONTWAIT;
1913
1914 msg = (struct user_msghdr __user *) (unsigned long)
1915 READ_ONCE(sqe->addr);
1916
Jens Axboeaa1fa282019-04-19 13:38:09 -06001917 ret = fn(sock, msg, flags);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001918 if (force_nonblock && ret == -EAGAIN)
1919 return ret;
1920 }
1921
Jens Axboe78e19bb2019-11-06 15:21:34 -07001922 io_cqring_add_event(req, ret);
Jens Axboef1f40852019-11-05 20:33:16 -07001923 if (ret < 0 && (req->flags & REQ_F_LINK))
1924 req->flags |= REQ_F_FAIL_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001925 io_put_req_find_next(req, nxt);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001926 return 0;
Jens Axboeaa1fa282019-04-19 13:38:09 -06001927}
1928#endif
1929
1930static int io_sendmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001931 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001932{
1933#if defined(CONFIG_NET)
Jens Axboeba816ad2019-09-28 11:36:45 -06001934 return io_send_recvmsg(req, sqe, nxt, force_nonblock,
1935 __sys_sendmsg_sock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06001936#else
1937 return -EOPNOTSUPP;
1938#endif
1939}
1940
1941static int io_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001942 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001943{
1944#if defined(CONFIG_NET)
Jens Axboeba816ad2019-09-28 11:36:45 -06001945 return io_send_recvmsg(req, sqe, nxt, force_nonblock,
1946 __sys_recvmsg_sock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001947#else
1948 return -EOPNOTSUPP;
1949#endif
1950}
1951
Jens Axboe17f2fe32019-10-17 14:42:58 -06001952static int io_accept(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1953 struct io_kiocb **nxt, bool force_nonblock)
1954{
1955#if defined(CONFIG_NET)
1956 struct sockaddr __user *addr;
1957 int __user *addr_len;
1958 unsigned file_flags;
1959 int flags, ret;
1960
1961 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
1962 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05001963 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06001964 return -EINVAL;
1965
1966 addr = (struct sockaddr __user *) (unsigned long) READ_ONCE(sqe->addr);
1967 addr_len = (int __user *) (unsigned long) READ_ONCE(sqe->addr2);
1968 flags = READ_ONCE(sqe->accept_flags);
1969 file_flags = force_nonblock ? O_NONBLOCK : 0;
1970
1971 ret = __sys_accept4_file(req->file, file_flags, addr, addr_len, flags);
1972 if (ret == -EAGAIN && force_nonblock) {
1973 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
1974 return -EAGAIN;
1975 }
Jens Axboe8e3cca12019-11-09 19:52:33 -07001976 if (ret == -ERESTARTSYS)
1977 ret = -EINTR;
Jens Axboe17f2fe32019-10-17 14:42:58 -06001978 if (ret < 0 && (req->flags & REQ_F_LINK))
1979 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001980 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001981 io_put_req_find_next(req, nxt);
Jens Axboe17f2fe32019-10-17 14:42:58 -06001982 return 0;
1983#else
1984 return -EOPNOTSUPP;
1985#endif
1986}
1987
Jens Axboef8e85cf2019-11-23 14:24:24 -07001988static int io_connect(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1989 struct io_kiocb **nxt, bool force_nonblock)
1990{
1991#if defined(CONFIG_NET)
1992 struct sockaddr __user *addr;
1993 unsigned file_flags;
1994 int addr_len, ret;
1995
1996 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
1997 return -EINVAL;
1998 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
1999 return -EINVAL;
2000
2001 addr = (struct sockaddr __user *) (unsigned long) READ_ONCE(sqe->addr);
2002 addr_len = READ_ONCE(sqe->addr2);
2003 file_flags = force_nonblock ? O_NONBLOCK : 0;
2004
2005 ret = __sys_connect_file(req->file, addr, addr_len, file_flags);
2006 if (ret == -EAGAIN && force_nonblock)
2007 return -EAGAIN;
2008 if (ret == -ERESTARTSYS)
2009 ret = -EINTR;
2010 if (ret < 0 && (req->flags & REQ_F_LINK))
2011 req->flags |= REQ_F_FAIL_LINK;
2012 io_cqring_add_event(req, ret);
2013 io_put_req_find_next(req, nxt);
2014 return 0;
2015#else
2016 return -EOPNOTSUPP;
2017#endif
2018}
2019
Jens Axboeeac406c2019-11-14 12:09:58 -07002020static inline void io_poll_remove_req(struct io_kiocb *req)
2021{
2022 if (!RB_EMPTY_NODE(&req->rb_node)) {
2023 rb_erase(&req->rb_node, &req->ctx->cancel_tree);
2024 RB_CLEAR_NODE(&req->rb_node);
2025 }
2026}
2027
Jens Axboe221c5eb2019-01-17 09:41:58 -07002028static void io_poll_remove_one(struct io_kiocb *req)
2029{
2030 struct io_poll_iocb *poll = &req->poll;
2031
2032 spin_lock(&poll->head->lock);
2033 WRITE_ONCE(poll->canceled, true);
Jens Axboee9444752019-11-26 15:02:04 -07002034 if (!list_empty(&poll->wait->entry)) {
2035 list_del_init(&poll->wait->entry);
Jackie Liua197f662019-11-08 08:09:12 -07002036 io_queue_async_work(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002037 }
2038 spin_unlock(&poll->head->lock);
Jens Axboeeac406c2019-11-14 12:09:58 -07002039 io_poll_remove_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002040}
2041
2042static void io_poll_remove_all(struct io_ring_ctx *ctx)
2043{
Jens Axboeeac406c2019-11-14 12:09:58 -07002044 struct rb_node *node;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002045 struct io_kiocb *req;
2046
2047 spin_lock_irq(&ctx->completion_lock);
Jens Axboeeac406c2019-11-14 12:09:58 -07002048 while ((node = rb_first(&ctx->cancel_tree)) != NULL) {
2049 req = rb_entry(node, struct io_kiocb, rb_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002050 io_poll_remove_one(req);
2051 }
2052 spin_unlock_irq(&ctx->completion_lock);
2053}
2054
Jens Axboe47f46762019-11-09 17:43:02 -07002055static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
2056{
Jens Axboeeac406c2019-11-14 12:09:58 -07002057 struct rb_node *p, *parent = NULL;
Jens Axboe47f46762019-11-09 17:43:02 -07002058 struct io_kiocb *req;
2059
Jens Axboeeac406c2019-11-14 12:09:58 -07002060 p = ctx->cancel_tree.rb_node;
2061 while (p) {
2062 parent = p;
2063 req = rb_entry(parent, struct io_kiocb, rb_node);
2064 if (sqe_addr < req->user_data) {
2065 p = p->rb_left;
2066 } else if (sqe_addr > req->user_data) {
2067 p = p->rb_right;
2068 } else {
2069 io_poll_remove_one(req);
2070 return 0;
2071 }
Jens Axboe47f46762019-11-09 17:43:02 -07002072 }
2073
2074 return -ENOENT;
2075}
2076
Jens Axboe221c5eb2019-01-17 09:41:58 -07002077/*
2078 * Find a running poll command that matches one specified in sqe->addr,
2079 * and remove it if found.
2080 */
2081static int io_poll_remove(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2082{
2083 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07002084 int ret;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002085
2086 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2087 return -EINVAL;
2088 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
2089 sqe->poll_events)
2090 return -EINVAL;
2091
2092 spin_lock_irq(&ctx->completion_lock);
Jens Axboe47f46762019-11-09 17:43:02 -07002093 ret = io_poll_cancel(ctx, READ_ONCE(sqe->addr));
Jens Axboe221c5eb2019-01-17 09:41:58 -07002094 spin_unlock_irq(&ctx->completion_lock);
2095
Jens Axboe78e19bb2019-11-06 15:21:34 -07002096 io_cqring_add_event(req, ret);
Jens Axboef1f40852019-11-05 20:33:16 -07002097 if (ret < 0 && (req->flags & REQ_F_LINK))
2098 req->flags |= REQ_F_FAIL_LINK;
Jens Axboee65ef562019-03-12 10:16:44 -06002099 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002100 return 0;
2101}
2102
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002103static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002104{
Jackie Liua197f662019-11-08 08:09:12 -07002105 struct io_ring_ctx *ctx = req->ctx;
2106
Jens Axboe8c838782019-03-12 15:48:16 -06002107 req->poll.done = true;
Jens Axboee9444752019-11-26 15:02:04 -07002108 kfree(req->poll.wait);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002109 if (error)
2110 io_cqring_fill_event(req, error);
2111 else
2112 io_cqring_fill_event(req, mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06002113 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002114}
2115
Jens Axboe561fb042019-10-24 07:25:42 -06002116static void io_poll_complete_work(struct io_wq_work **workptr)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002117{
Jens Axboe561fb042019-10-24 07:25:42 -06002118 struct io_wq_work *work = *workptr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002119 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
2120 struct io_poll_iocb *poll = &req->poll;
2121 struct poll_table_struct pt = { ._key = poll->events };
2122 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe89723d02019-11-05 15:32:58 -07002123 struct io_kiocb *nxt = NULL;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002124 __poll_t mask = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002125 int ret = 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002126
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002127 if (work->flags & IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06002128 WRITE_ONCE(poll->canceled, true);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002129 ret = -ECANCELED;
2130 } else if (READ_ONCE(poll->canceled)) {
2131 ret = -ECANCELED;
2132 }
Jens Axboe561fb042019-10-24 07:25:42 -06002133
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002134 if (ret != -ECANCELED)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002135 mask = vfs_poll(poll->file, &pt) & poll->events;
2136
2137 /*
2138 * Note that ->ki_cancel callers also delete iocb from active_reqs after
2139 * calling ->ki_cancel. We need the ctx_lock roundtrip here to
2140 * synchronize with them. In the cancellation case the list_del_init
2141 * itself is not actually needed, but harmless so we keep it in to
2142 * avoid further branches in the fast path.
2143 */
2144 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002145 if (!mask && ret != -ECANCELED) {
Jens Axboee9444752019-11-26 15:02:04 -07002146 add_wait_queue(poll->head, poll->wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002147 spin_unlock_irq(&ctx->completion_lock);
2148 return;
2149 }
Jens Axboeeac406c2019-11-14 12:09:58 -07002150 io_poll_remove_req(req);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002151 io_poll_complete(req, mask, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002152 spin_unlock_irq(&ctx->completion_lock);
2153
Jens Axboe8c838782019-03-12 15:48:16 -06002154 io_cqring_ev_posted(ctx);
Jens Axboe89723d02019-11-05 15:32:58 -07002155
Jens Axboefba38c22019-11-18 12:27:57 -07002156 if (ret < 0 && req->flags & REQ_F_LINK)
2157 req->flags |= REQ_F_FAIL_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08002158 io_put_req_find_next(req, &nxt);
Jens Axboe89723d02019-11-05 15:32:58 -07002159 if (nxt)
2160 *workptr = &nxt->work;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002161}
2162
2163static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
2164 void *key)
2165{
Jens Axboee9444752019-11-26 15:02:04 -07002166 struct io_poll_iocb *poll = wait->private;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002167 struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
2168 struct io_ring_ctx *ctx = req->ctx;
2169 __poll_t mask = key_to_poll(key);
Jens Axboe8c838782019-03-12 15:48:16 -06002170 unsigned long flags;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002171
2172 /* for instances that support it check for an event match first: */
Jens Axboe8c838782019-03-12 15:48:16 -06002173 if (mask && !(mask & poll->events))
2174 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002175
Jens Axboee9444752019-11-26 15:02:04 -07002176 list_del_init(&poll->wait->entry);
Jens Axboe8c838782019-03-12 15:48:16 -06002177
Jens Axboe7c9e7f02019-11-12 08:15:53 -07002178 /*
2179 * Run completion inline if we can. We're using trylock here because
2180 * we are violating the completion_lock -> poll wq lock ordering.
2181 * If we have a link timeout we're going to need the completion_lock
2182 * for finalizing the request, mark us as having grabbed that already.
2183 */
Jens Axboe8c838782019-03-12 15:48:16 -06002184 if (mask && spin_trylock_irqsave(&ctx->completion_lock, flags)) {
Jens Axboeeac406c2019-11-14 12:09:58 -07002185 io_poll_remove_req(req);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002186 io_poll_complete(req, mask, 0);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07002187 req->flags |= REQ_F_COMP_LOCKED;
2188 io_put_req(req);
Jens Axboe8c838782019-03-12 15:48:16 -06002189 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2190
2191 io_cqring_ev_posted(ctx);
Jens Axboe8c838782019-03-12 15:48:16 -06002192 } else {
Jackie Liua197f662019-11-08 08:09:12 -07002193 io_queue_async_work(req);
Jens Axboe8c838782019-03-12 15:48:16 -06002194 }
2195
Jens Axboe221c5eb2019-01-17 09:41:58 -07002196 return 1;
2197}
2198
2199struct io_poll_table {
2200 struct poll_table_struct pt;
2201 struct io_kiocb *req;
2202 int error;
2203};
2204
2205static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
2206 struct poll_table_struct *p)
2207{
2208 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
2209
2210 if (unlikely(pt->req->poll.head)) {
2211 pt->error = -EINVAL;
2212 return;
2213 }
2214
2215 pt->error = 0;
2216 pt->req->poll.head = head;
Jens Axboee9444752019-11-26 15:02:04 -07002217 add_wait_queue(head, pt->req->poll.wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002218}
2219
Jens Axboeeac406c2019-11-14 12:09:58 -07002220static void io_poll_req_insert(struct io_kiocb *req)
2221{
2222 struct io_ring_ctx *ctx = req->ctx;
2223 struct rb_node **p = &ctx->cancel_tree.rb_node;
2224 struct rb_node *parent = NULL;
2225 struct io_kiocb *tmp;
2226
2227 while (*p) {
2228 parent = *p;
2229 tmp = rb_entry(parent, struct io_kiocb, rb_node);
2230 if (req->user_data < tmp->user_data)
2231 p = &(*p)->rb_left;
2232 else
2233 p = &(*p)->rb_right;
2234 }
2235 rb_link_node(&req->rb_node, parent, p);
2236 rb_insert_color(&req->rb_node, &ctx->cancel_tree);
2237}
2238
Jens Axboe89723d02019-11-05 15:32:58 -07002239static int io_poll_add(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2240 struct io_kiocb **nxt)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002241{
2242 struct io_poll_iocb *poll = &req->poll;
2243 struct io_ring_ctx *ctx = req->ctx;
2244 struct io_poll_table ipt;
Jens Axboe8c838782019-03-12 15:48:16 -06002245 bool cancel = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002246 __poll_t mask;
2247 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002248
2249 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2250 return -EINVAL;
2251 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
2252 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06002253 if (!poll->file)
2254 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002255
Jens Axboee9444752019-11-26 15:02:04 -07002256 poll->wait = kmalloc(sizeof(*poll->wait), GFP_KERNEL);
2257 if (!poll->wait)
2258 return -ENOMEM;
2259
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002260 req->sqe = NULL;
Jens Axboe561fb042019-10-24 07:25:42 -06002261 INIT_IO_WORK(&req->work, io_poll_complete_work);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002262 events = READ_ONCE(sqe->poll_events);
2263 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
Jens Axboeeac406c2019-11-14 12:09:58 -07002264 RB_CLEAR_NODE(&req->rb_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002265
Jens Axboe221c5eb2019-01-17 09:41:58 -07002266 poll->head = NULL;
Jens Axboe8c838782019-03-12 15:48:16 -06002267 poll->done = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002268 poll->canceled = false;
2269
2270 ipt.pt._qproc = io_poll_queue_proc;
2271 ipt.pt._key = poll->events;
2272 ipt.req = req;
2273 ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
2274
2275 /* initialized the list so that we can do list_empty checks */
Jens Axboee9444752019-11-26 15:02:04 -07002276 INIT_LIST_HEAD(&poll->wait->entry);
2277 init_waitqueue_func_entry(poll->wait, io_poll_wake);
2278 poll->wait->private = poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002279
Jens Axboe36703242019-07-25 10:20:18 -06002280 INIT_LIST_HEAD(&req->list);
2281
Jens Axboe221c5eb2019-01-17 09:41:58 -07002282 mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002283
2284 spin_lock_irq(&ctx->completion_lock);
Jens Axboe8c838782019-03-12 15:48:16 -06002285 if (likely(poll->head)) {
2286 spin_lock(&poll->head->lock);
Jens Axboee9444752019-11-26 15:02:04 -07002287 if (unlikely(list_empty(&poll->wait->entry))) {
Jens Axboe8c838782019-03-12 15:48:16 -06002288 if (ipt.error)
2289 cancel = true;
2290 ipt.error = 0;
2291 mask = 0;
2292 }
2293 if (mask || ipt.error)
Jens Axboee9444752019-11-26 15:02:04 -07002294 list_del_init(&poll->wait->entry);
Jens Axboe8c838782019-03-12 15:48:16 -06002295 else if (cancel)
2296 WRITE_ONCE(poll->canceled, true);
2297 else if (!poll->done) /* actually waiting for an event */
Jens Axboeeac406c2019-11-14 12:09:58 -07002298 io_poll_req_insert(req);
Jens Axboe8c838782019-03-12 15:48:16 -06002299 spin_unlock(&poll->head->lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002300 }
Jens Axboe8c838782019-03-12 15:48:16 -06002301 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06002302 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002303 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06002304 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07002305 spin_unlock_irq(&ctx->completion_lock);
2306
Jens Axboe8c838782019-03-12 15:48:16 -06002307 if (mask) {
2308 io_cqring_ev_posted(ctx);
Jackie Liuec9c02a2019-11-08 23:50:36 +08002309 io_put_req_find_next(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002310 }
Jens Axboe8c838782019-03-12 15:48:16 -06002311 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002312}
2313
Jens Axboe5262f562019-09-17 12:26:57 -06002314static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
2315{
Jens Axboead8a48a2019-11-15 08:49:11 -07002316 struct io_timeout_data *data = container_of(timer,
2317 struct io_timeout_data, timer);
2318 struct io_kiocb *req = data->req;
2319 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06002320 unsigned long flags;
2321
Jens Axboe5262f562019-09-17 12:26:57 -06002322 atomic_inc(&ctx->cq_timeouts);
2323
2324 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08002325 /*
Jens Axboe11365042019-10-16 09:08:32 -06002326 * We could be racing with timeout deletion. If the list is empty,
2327 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08002328 */
Jens Axboe842f9612019-10-29 12:34:10 -06002329 if (!list_empty(&req->list)) {
Jens Axboe11365042019-10-16 09:08:32 -06002330 struct io_kiocb *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06002331
Jens Axboe11365042019-10-16 09:08:32 -06002332 /*
2333 * Adjust the reqs sequence before the current one because it
2334 * will consume a slot in the cq_ring and the the cq_tail
2335 * pointer will be increased, otherwise other timeout reqs may
2336 * return in advance without waiting for enough wait_nr.
2337 */
2338 prev = req;
2339 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
2340 prev->sequence++;
Jens Axboe11365042019-10-16 09:08:32 -06002341 list_del_init(&req->list);
Jens Axboe11365042019-10-16 09:08:32 -06002342 }
Jens Axboe842f9612019-10-29 12:34:10 -06002343
Jens Axboe78e19bb2019-11-06 15:21:34 -07002344 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06002345 io_commit_cqring(ctx);
2346 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2347
2348 io_cqring_ev_posted(ctx);
Jens Axboef1f40852019-11-05 20:33:16 -07002349 if (req->flags & REQ_F_LINK)
2350 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe5262f562019-09-17 12:26:57 -06002351 io_put_req(req);
2352 return HRTIMER_NORESTART;
2353}
2354
Jens Axboe47f46762019-11-09 17:43:02 -07002355static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
2356{
2357 struct io_kiocb *req;
2358 int ret = -ENOENT;
2359
2360 list_for_each_entry(req, &ctx->timeout_list, list) {
2361 if (user_data == req->user_data) {
2362 list_del_init(&req->list);
2363 ret = 0;
2364 break;
2365 }
2366 }
2367
2368 if (ret == -ENOENT)
2369 return ret;
2370
Jens Axboead8a48a2019-11-15 08:49:11 -07002371 ret = hrtimer_try_to_cancel(&req->timeout.data->timer);
Jens Axboe47f46762019-11-09 17:43:02 -07002372 if (ret == -1)
2373 return -EALREADY;
2374
Jens Axboefba38c22019-11-18 12:27:57 -07002375 if (req->flags & REQ_F_LINK)
2376 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe47f46762019-11-09 17:43:02 -07002377 io_cqring_fill_event(req, -ECANCELED);
2378 io_put_req(req);
2379 return 0;
2380}
2381
Jens Axboe11365042019-10-16 09:08:32 -06002382/*
2383 * Remove or update an existing timeout command
2384 */
2385static int io_timeout_remove(struct io_kiocb *req,
2386 const struct io_uring_sqe *sqe)
2387{
2388 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe11365042019-10-16 09:08:32 -06002389 unsigned flags;
Jens Axboe47f46762019-11-09 17:43:02 -07002390 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06002391
2392 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2393 return -EINVAL;
2394 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
2395 return -EINVAL;
2396 flags = READ_ONCE(sqe->timeout_flags);
2397 if (flags)
2398 return -EINVAL;
2399
Jens Axboe11365042019-10-16 09:08:32 -06002400 spin_lock_irq(&ctx->completion_lock);
Jens Axboe47f46762019-11-09 17:43:02 -07002401 ret = io_timeout_cancel(ctx, READ_ONCE(sqe->addr));
Jens Axboe11365042019-10-16 09:08:32 -06002402
Jens Axboe47f46762019-11-09 17:43:02 -07002403 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06002404 io_commit_cqring(ctx);
2405 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06002406 io_cqring_ev_posted(ctx);
Jens Axboe47f46762019-11-09 17:43:02 -07002407 if (ret < 0 && req->flags & REQ_F_LINK)
2408 req->flags |= REQ_F_FAIL_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08002409 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06002410 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06002411}
2412
Jens Axboead8a48a2019-11-15 08:49:11 -07002413static int io_timeout_setup(struct io_kiocb *req)
Jens Axboe5262f562019-09-17 12:26:57 -06002414{
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002415 const struct io_uring_sqe *sqe = req->sqe;
Jens Axboead8a48a2019-11-15 08:49:11 -07002416 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06002417 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06002418
Jens Axboead8a48a2019-11-15 08:49:11 -07002419 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06002420 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07002421 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06002422 return -EINVAL;
2423 flags = READ_ONCE(sqe->timeout_flags);
2424 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06002425 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06002426
Jens Axboead8a48a2019-11-15 08:49:11 -07002427 data = kzalloc(sizeof(struct io_timeout_data), GFP_KERNEL);
2428 if (!data)
2429 return -ENOMEM;
2430 data->req = req;
2431 req->timeout.data = data;
2432 req->flags |= REQ_F_TIMEOUT;
2433
2434 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06002435 return -EFAULT;
2436
Jens Axboe11365042019-10-16 09:08:32 -06002437 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07002438 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06002439 else
Jens Axboead8a48a2019-11-15 08:49:11 -07002440 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06002441
Jens Axboead8a48a2019-11-15 08:49:11 -07002442 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
2443 return 0;
2444}
2445
2446static int io_timeout(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2447{
2448 unsigned count;
2449 struct io_ring_ctx *ctx = req->ctx;
2450 struct io_timeout_data *data;
2451 struct list_head *entry;
2452 unsigned span = 0;
2453 int ret;
2454
2455 ret = io_timeout_setup(req);
2456 /* common setup allows flags (like links) set, we don't */
2457 if (!ret && sqe->flags)
2458 ret = -EINVAL;
2459 if (ret)
2460 return ret;
Jens Axboe93bd25b2019-11-11 23:34:31 -07002461
Jens Axboe5262f562019-09-17 12:26:57 -06002462 /*
2463 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07002464 * timeout event to be satisfied. If it isn't set, then this is
2465 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06002466 */
2467 count = READ_ONCE(sqe->off);
Jens Axboe93bd25b2019-11-11 23:34:31 -07002468 if (!count) {
2469 req->flags |= REQ_F_TIMEOUT_NOSEQ;
2470 spin_lock_irq(&ctx->completion_lock);
2471 entry = ctx->timeout_list.prev;
2472 goto add;
2473 }
Jens Axboe5262f562019-09-17 12:26:57 -06002474
2475 req->sequence = ctx->cached_sq_head + count - 1;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +03002476 req->timeout.data->seq_offset = count;
Jens Axboe5262f562019-09-17 12:26:57 -06002477
2478 /*
2479 * Insertion sort, ensuring the first entry in the list is always
2480 * the one we need first.
2481 */
Jens Axboe5262f562019-09-17 12:26:57 -06002482 spin_lock_irq(&ctx->completion_lock);
2483 list_for_each_prev(entry, &ctx->timeout_list) {
2484 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
yangerkun5da0fb12019-10-15 21:59:29 +08002485 unsigned nxt_sq_head;
2486 long long tmp, tmp_nxt;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +03002487 u32 nxt_offset = nxt->timeout.data->seq_offset;
Jens Axboe5262f562019-09-17 12:26:57 -06002488
Jens Axboe93bd25b2019-11-11 23:34:31 -07002489 if (nxt->flags & REQ_F_TIMEOUT_NOSEQ)
2490 continue;
2491
yangerkun5da0fb12019-10-15 21:59:29 +08002492 /*
2493 * Since cached_sq_head + count - 1 can overflow, use type long
2494 * long to store it.
2495 */
2496 tmp = (long long)ctx->cached_sq_head + count - 1;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +03002497 nxt_sq_head = nxt->sequence - nxt_offset + 1;
2498 tmp_nxt = (long long)nxt_sq_head + nxt_offset - 1;
yangerkun5da0fb12019-10-15 21:59:29 +08002499
2500 /*
2501 * cached_sq_head may overflow, and it will never overflow twice
2502 * once there is some timeout req still be valid.
2503 */
2504 if (ctx->cached_sq_head < nxt_sq_head)
yangerkun8b07a652019-10-17 12:12:35 +08002505 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08002506
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002507 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06002508 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002509
2510 /*
2511 * Sequence of reqs after the insert one and itself should
2512 * be adjusted because each timeout req consumes a slot.
2513 */
2514 span++;
2515 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06002516 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002517 req->sequence -= span;
Jens Axboe93bd25b2019-11-11 23:34:31 -07002518add:
Jens Axboe5262f562019-09-17 12:26:57 -06002519 list_add(&req->list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07002520 data = req->timeout.data;
2521 data->timer.function = io_timeout_fn;
2522 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06002523 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06002524 return 0;
2525}
2526
Jens Axboe62755e32019-10-28 21:49:21 -06002527static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06002528{
Jens Axboe62755e32019-10-28 21:49:21 -06002529 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06002530
Jens Axboe62755e32019-10-28 21:49:21 -06002531 return req->user_data == (unsigned long) data;
2532}
2533
Jens Axboee977d6d2019-11-05 12:39:45 -07002534static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06002535{
Jens Axboe62755e32019-10-28 21:49:21 -06002536 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06002537 int ret = 0;
2538
Jens Axboe62755e32019-10-28 21:49:21 -06002539 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
2540 switch (cancel_ret) {
2541 case IO_WQ_CANCEL_OK:
2542 ret = 0;
2543 break;
2544 case IO_WQ_CANCEL_RUNNING:
2545 ret = -EALREADY;
2546 break;
2547 case IO_WQ_CANCEL_NOTFOUND:
2548 ret = -ENOENT;
2549 break;
2550 }
2551
Jens Axboee977d6d2019-11-05 12:39:45 -07002552 return ret;
2553}
2554
Jens Axboe47f46762019-11-09 17:43:02 -07002555static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
2556 struct io_kiocb *req, __u64 sqe_addr,
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002557 struct io_kiocb **nxt, int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07002558{
2559 unsigned long flags;
2560 int ret;
2561
2562 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
2563 if (ret != -ENOENT) {
2564 spin_lock_irqsave(&ctx->completion_lock, flags);
2565 goto done;
2566 }
2567
2568 spin_lock_irqsave(&ctx->completion_lock, flags);
2569 ret = io_timeout_cancel(ctx, sqe_addr);
2570 if (ret != -ENOENT)
2571 goto done;
2572 ret = io_poll_cancel(ctx, sqe_addr);
2573done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002574 if (!ret)
2575 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07002576 io_cqring_fill_event(req, ret);
2577 io_commit_cqring(ctx);
2578 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2579 io_cqring_ev_posted(ctx);
2580
2581 if (ret < 0 && (req->flags & REQ_F_LINK))
2582 req->flags |= REQ_F_FAIL_LINK;
2583 io_put_req_find_next(req, nxt);
2584}
2585
Jens Axboee977d6d2019-11-05 12:39:45 -07002586static int io_async_cancel(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2587 struct io_kiocb **nxt)
2588{
2589 struct io_ring_ctx *ctx = req->ctx;
Jens Axboee977d6d2019-11-05 12:39:45 -07002590
2591 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2592 return -EINVAL;
2593 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
2594 sqe->cancel_flags)
2595 return -EINVAL;
2596
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002597 io_async_find_and_cancel(ctx, req, READ_ONCE(sqe->addr), nxt, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06002598 return 0;
2599}
2600
Jackie Liua197f662019-11-08 08:09:12 -07002601static int io_req_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -06002602{
2603 struct io_uring_sqe *sqe_copy;
Jackie Liua197f662019-11-08 08:09:12 -07002604 struct io_ring_ctx *ctx = req->ctx;
Jens Axboede0617e2019-04-06 21:51:27 -06002605
Bob Liu9d858b22019-11-13 18:06:25 +08002606 /* Still need defer if there is pending req in defer list. */
2607 if (!req_need_defer(req) && list_empty(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06002608 return 0;
2609
2610 sqe_copy = kmalloc(sizeof(*sqe_copy), GFP_KERNEL);
2611 if (!sqe_copy)
2612 return -EAGAIN;
2613
2614 spin_lock_irq(&ctx->completion_lock);
Bob Liu9d858b22019-11-13 18:06:25 +08002615 if (!req_need_defer(req) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06002616 spin_unlock_irq(&ctx->completion_lock);
2617 kfree(sqe_copy);
2618 return 0;
2619 }
2620
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002621 memcpy(sqe_copy, req->sqe, sizeof(*sqe_copy));
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03002622 req->flags |= REQ_F_FREE_SQE;
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002623 req->sqe = sqe_copy;
Jens Axboede0617e2019-04-06 21:51:27 -06002624
Jens Axboe915967f2019-11-21 09:01:20 -07002625 trace_io_uring_defer(ctx, req, req->user_data);
Jens Axboede0617e2019-04-06 21:51:27 -06002626 list_add_tail(&req->list, &ctx->defer_list);
2627 spin_unlock_irq(&ctx->completion_lock);
2628 return -EIOCBQUEUED;
2629}
2630
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03002631__attribute__((nonnull))
Pavel Begunkovd7324472019-11-21 21:24:36 +03002632static int io_issue_sqe(struct io_kiocb *req, struct io_kiocb **nxt,
2633 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002634{
Jens Axboee0c5c572019-03-12 10:18:47 -06002635 int ret, opcode;
Jackie Liua197f662019-11-08 08:09:12 -07002636 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002637
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002638 opcode = READ_ONCE(req->sqe->opcode);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002639 switch (opcode) {
2640 case IORING_OP_NOP:
Jens Axboe78e19bb2019-11-06 15:21:34 -07002641 ret = io_nop(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002642 break;
2643 case IORING_OP_READV:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002644 if (unlikely(req->sqe->buf_index))
Jens Axboeedafcce2019-01-09 09:16:05 -07002645 return -EINVAL;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002646 ret = io_read(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002647 break;
2648 case IORING_OP_WRITEV:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002649 if (unlikely(req->sqe->buf_index))
Jens Axboeedafcce2019-01-09 09:16:05 -07002650 return -EINVAL;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002651 ret = io_write(req, nxt, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07002652 break;
2653 case IORING_OP_READ_FIXED:
Pavel Begunkov267bc902019-11-07 01:41:08 +03002654 ret = io_read(req, nxt, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07002655 break;
2656 case IORING_OP_WRITE_FIXED:
Pavel Begunkov267bc902019-11-07 01:41:08 +03002657 ret = io_write(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002658 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002659 case IORING_OP_FSYNC:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002660 ret = io_fsync(req, req->sqe, nxt, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002661 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002662 case IORING_OP_POLL_ADD:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002663 ret = io_poll_add(req, req->sqe, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002664 break;
2665 case IORING_OP_POLL_REMOVE:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002666 ret = io_poll_remove(req, req->sqe);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002667 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002668 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002669 ret = io_sync_file_range(req, req->sqe, nxt, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002670 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002671 case IORING_OP_SENDMSG:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002672 ret = io_sendmsg(req, req->sqe, nxt, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06002673 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06002674 case IORING_OP_RECVMSG:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002675 ret = io_recvmsg(req, req->sqe, nxt, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06002676 break;
Jens Axboe5262f562019-09-17 12:26:57 -06002677 case IORING_OP_TIMEOUT:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002678 ret = io_timeout(req, req->sqe);
Jens Axboe5262f562019-09-17 12:26:57 -06002679 break;
Jens Axboe11365042019-10-16 09:08:32 -06002680 case IORING_OP_TIMEOUT_REMOVE:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002681 ret = io_timeout_remove(req, req->sqe);
Jens Axboe11365042019-10-16 09:08:32 -06002682 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06002683 case IORING_OP_ACCEPT:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002684 ret = io_accept(req, req->sqe, nxt, force_nonblock);
Jens Axboe17f2fe32019-10-17 14:42:58 -06002685 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07002686 case IORING_OP_CONNECT:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002687 ret = io_connect(req, req->sqe, nxt, force_nonblock);
Jens Axboef8e85cf2019-11-23 14:24:24 -07002688 break;
Jens Axboe62755e32019-10-28 21:49:21 -06002689 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002690 ret = io_async_cancel(req, req->sqe, nxt);
Jens Axboe62755e32019-10-28 21:49:21 -06002691 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002692 default:
2693 ret = -EINVAL;
2694 break;
2695 }
2696
Jens Axboedef596e2019-01-09 08:59:42 -07002697 if (ret)
2698 return ret;
2699
2700 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe9e645e112019-05-10 16:07:28 -06002701 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07002702 return -EAGAIN;
2703
2704 /* workqueue context doesn't hold uring_lock, grab it now */
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002705 if (req->in_async)
Jens Axboedef596e2019-01-09 08:59:42 -07002706 mutex_lock(&ctx->uring_lock);
2707 io_iopoll_req_issued(req);
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002708 if (req->in_async)
Jens Axboedef596e2019-01-09 08:59:42 -07002709 mutex_unlock(&ctx->uring_lock);
2710 }
2711
2712 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002713}
2714
Jens Axboeb76da702019-11-20 13:05:32 -07002715static void io_link_work_cb(struct io_wq_work **workptr)
2716{
2717 struct io_wq_work *work = *workptr;
2718 struct io_kiocb *link = work->data;
2719
2720 io_queue_linked_timeout(link);
2721 work->func = io_wq_submit_work;
2722}
2723
Jens Axboe561fb042019-10-24 07:25:42 -06002724static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboe31b51512019-01-18 22:56:34 -07002725{
Jens Axboe561fb042019-10-24 07:25:42 -06002726 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002727 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe561fb042019-10-24 07:25:42 -06002728 struct io_kiocb *nxt = NULL;
2729 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002730
Jens Axboe561fb042019-10-24 07:25:42 -06002731 /* Ensure we clear previously set non-block flag */
2732 req->rw.ki_flags &= ~IOCB_NOWAIT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002733
Jens Axboe561fb042019-10-24 07:25:42 -06002734 if (work->flags & IO_WQ_WORK_CANCEL)
2735 ret = -ECANCELED;
Jens Axboe31b51512019-01-18 22:56:34 -07002736
Jens Axboe561fb042019-10-24 07:25:42 -06002737 if (!ret) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002738 req->has_user = (work->flags & IO_WQ_WORK_HAS_MM) != 0;
2739 req->in_async = true;
Jens Axboe561fb042019-10-24 07:25:42 -06002740 do {
Pavel Begunkovd7324472019-11-21 21:24:36 +03002741 ret = io_issue_sqe(req, &nxt, false);
Jens Axboe561fb042019-10-24 07:25:42 -06002742 /*
2743 * We can get EAGAIN for polled IO even though we're
2744 * forcing a sync submission from here, since we can't
2745 * wait for request slots on the block side.
2746 */
2747 if (ret != -EAGAIN)
2748 break;
2749 cond_resched();
2750 } while (1);
2751 }
Jens Axboe31b51512019-01-18 22:56:34 -07002752
Jens Axboe561fb042019-10-24 07:25:42 -06002753 /* drop submission reference */
Jackie Liuec9c02a2019-11-08 23:50:36 +08002754 io_put_req(req);
Jens Axboe817869d2019-04-30 14:44:05 -06002755
Jens Axboe561fb042019-10-24 07:25:42 -06002756 if (ret) {
Jens Axboef1f40852019-11-05 20:33:16 -07002757 if (req->flags & REQ_F_LINK)
2758 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07002759 io_cqring_add_event(req, ret);
Jens Axboe817869d2019-04-30 14:44:05 -06002760 io_put_req(req);
Jens Axboeedafcce2019-01-09 09:16:05 -07002761 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002762
Jens Axboe561fb042019-10-24 07:25:42 -06002763 /* if a dependent link is ready, pass it back */
2764 if (!ret && nxt) {
Jens Axboe94ae5e72019-11-14 19:39:52 -07002765 struct io_kiocb *link;
2766
2767 io_prep_async_work(nxt, &link);
Jens Axboe561fb042019-10-24 07:25:42 -06002768 *workptr = &nxt->work;
Jens Axboeb76da702019-11-20 13:05:32 -07002769 if (link) {
2770 nxt->work.flags |= IO_WQ_WORK_CB;
2771 nxt->work.func = io_link_work_cb;
2772 nxt->work.data = link;
2773 }
Jens Axboeedafcce2019-01-09 09:16:05 -07002774 }
Jens Axboe31b51512019-01-18 22:56:34 -07002775}
Jens Axboe2b188cc2019-01-07 10:46:33 -07002776
Jens Axboe09bb8392019-03-13 12:39:28 -06002777static bool io_op_needs_file(const struct io_uring_sqe *sqe)
2778{
2779 int op = READ_ONCE(sqe->opcode);
2780
2781 switch (op) {
2782 case IORING_OP_NOP:
2783 case IORING_OP_POLL_REMOVE:
Pavel Begunkov5683e542019-11-14 00:59:19 +03002784 case IORING_OP_TIMEOUT:
Pavel Begunkova320e9f2019-11-14 00:11:01 +03002785 case IORING_OP_TIMEOUT_REMOVE:
2786 case IORING_OP_ASYNC_CANCEL:
2787 case IORING_OP_LINK_TIMEOUT:
Jens Axboe09bb8392019-03-13 12:39:28 -06002788 return false;
2789 default:
2790 return true;
2791 }
2792}
2793
Jens Axboe65e19f52019-10-26 07:20:21 -06002794static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
2795 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06002796{
Jens Axboe65e19f52019-10-26 07:20:21 -06002797 struct fixed_file_table *table;
2798
2799 table = &ctx->file_table[index >> IORING_FILE_TABLE_SHIFT];
2800 return table->files[index & IORING_FILE_TABLE_MASK];
2801}
2802
Jackie Liua197f662019-11-08 08:09:12 -07002803static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req)
Jens Axboe09bb8392019-03-13 12:39:28 -06002804{
Jackie Liua197f662019-11-08 08:09:12 -07002805 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe09bb8392019-03-13 12:39:28 -06002806 unsigned flags;
2807 int fd;
2808
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002809 flags = READ_ONCE(req->sqe->flags);
2810 fd = READ_ONCE(req->sqe->fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06002811
Jackie Liu4fe2c962019-09-09 20:50:40 +08002812 if (flags & IOSQE_IO_DRAIN)
Jens Axboede0617e2019-04-06 21:51:27 -06002813 req->flags |= REQ_F_IO_DRAIN;
Jens Axboede0617e2019-04-06 21:51:27 -06002814
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002815 if (!io_op_needs_file(req->sqe))
Jens Axboe09bb8392019-03-13 12:39:28 -06002816 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06002817
2818 if (flags & IOSQE_FIXED_FILE) {
Jens Axboe65e19f52019-10-26 07:20:21 -06002819 if (unlikely(!ctx->file_table ||
Jens Axboe09bb8392019-03-13 12:39:28 -06002820 (unsigned) fd >= ctx->nr_user_files))
2821 return -EBADF;
Jens Axboeb7620122019-10-26 07:22:55 -06002822 fd = array_index_nospec(fd, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06002823 req->file = io_file_from_index(ctx, fd);
2824 if (!req->file)
Jens Axboe08a45172019-10-03 08:11:03 -06002825 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06002826 req->flags |= REQ_F_FIXED_FILE;
2827 } else {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002828 if (req->needs_fixed_file)
Jens Axboe09bb8392019-03-13 12:39:28 -06002829 return -EBADF;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002830 trace_io_uring_file_get(ctx, fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06002831 req->file = io_file_get(state, fd);
2832 if (unlikely(!req->file))
2833 return -EBADF;
2834 }
2835
2836 return 0;
2837}
2838
Jackie Liua197f662019-11-08 08:09:12 -07002839static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002840{
Jens Axboefcb323c2019-10-24 12:39:47 -06002841 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07002842 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06002843
2844 rcu_read_lock();
2845 spin_lock_irq(&ctx->inflight_lock);
2846 /*
2847 * We use the f_ops->flush() handler to ensure that we can flush
2848 * out work accessing these files if the fd is closed. Check if
2849 * the fd has changed since we started down this path, and disallow
2850 * this operation if it has.
2851 */
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002852 if (fcheck(req->ring_fd) == req->ring_file) {
Jens Axboefcb323c2019-10-24 12:39:47 -06002853 list_add(&req->inflight_entry, &ctx->inflight_list);
2854 req->flags |= REQ_F_INFLIGHT;
2855 req->work.files = current->files;
2856 ret = 0;
2857 }
2858 spin_unlock_irq(&ctx->inflight_lock);
2859 rcu_read_unlock();
2860
2861 return ret;
2862}
2863
Jens Axboe2665abf2019-11-05 12:40:47 -07002864static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
2865{
Jens Axboead8a48a2019-11-15 08:49:11 -07002866 struct io_timeout_data *data = container_of(timer,
2867 struct io_timeout_data, timer);
2868 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07002869 struct io_ring_ctx *ctx = req->ctx;
2870 struct io_kiocb *prev = NULL;
2871 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07002872
2873 spin_lock_irqsave(&ctx->completion_lock, flags);
2874
2875 /*
2876 * We don't expect the list to be empty, that will only happen if we
2877 * race with the completion of the linked work.
2878 */
2879 if (!list_empty(&req->list)) {
2880 prev = list_entry(req->list.prev, struct io_kiocb, link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07002881 if (refcount_inc_not_zero(&prev->refs)) {
Jens Axboe76a46e02019-11-10 23:34:16 -07002882 list_del_init(&req->list);
Jens Axboe5d960722019-11-19 15:31:28 -07002883 prev->flags &= ~REQ_F_LINK_TIMEOUT;
2884 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07002885 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07002886 }
2887
2888 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2889
2890 if (prev) {
Jens Axboefba38c22019-11-18 12:27:57 -07002891 if (prev->flags & REQ_F_LINK)
2892 prev->flags |= REQ_F_FAIL_LINK;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002893 io_async_find_and_cancel(ctx, req, prev->user_data, NULL,
2894 -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07002895 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07002896 } else {
2897 io_cqring_add_event(req, -ETIME);
2898 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07002899 }
Jens Axboe2665abf2019-11-05 12:40:47 -07002900 return HRTIMER_NORESTART;
2901}
2902
Jens Axboead8a48a2019-11-15 08:49:11 -07002903static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07002904{
Jens Axboe76a46e02019-11-10 23:34:16 -07002905 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07002906
Jens Axboe76a46e02019-11-10 23:34:16 -07002907 /*
2908 * If the list is now empty, then our linked request finished before
2909 * we got a chance to setup the timer
2910 */
2911 spin_lock_irq(&ctx->completion_lock);
2912 if (!list_empty(&req->list)) {
Jens Axboe94ae5e72019-11-14 19:39:52 -07002913 struct io_timeout_data *data = req->timeout.data;
2914
Jens Axboead8a48a2019-11-15 08:49:11 -07002915 data->timer.function = io_link_timeout_fn;
2916 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
2917 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07002918 }
Jens Axboe76a46e02019-11-10 23:34:16 -07002919 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07002920
Jens Axboe2665abf2019-11-05 12:40:47 -07002921 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07002922 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07002923}
2924
Jens Axboead8a48a2019-11-15 08:49:11 -07002925static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07002926{
2927 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002928
Jens Axboe2665abf2019-11-05 12:40:47 -07002929 if (!(req->flags & REQ_F_LINK))
2930 return NULL;
2931
2932 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, list);
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002933 if (!nxt || nxt->sqe->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07002934 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07002935
Jens Axboe76a46e02019-11-10 23:34:16 -07002936 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07002937 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07002938}
2939
Jens Axboe0e0702d2019-11-14 21:42:10 -07002940static void __io_queue_sqe(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002941{
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03002942 struct io_kiocb *linked_timeout = io_prep_linked_timeout(req);
2943 struct io_kiocb *nxt = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002944 int ret;
2945
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03002946 ret = io_issue_sqe(req, &nxt, true);
2947 if (nxt)
2948 io_queue_async_work(nxt);
Jens Axboe491381ce2019-10-17 09:20:46 -06002949
2950 /*
2951 * We async punt it if the file wasn't marked NOWAIT, or if the file
2952 * doesn't support non-blocking read/write attempts
2953 */
2954 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
2955 (req->flags & REQ_F_MUST_PUNT))) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07002956 struct io_uring_sqe *sqe_copy;
2957
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002958 sqe_copy = kmemdup(req->sqe, sizeof(*sqe_copy), GFP_KERNEL);
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03002959 if (!sqe_copy)
2960 goto err;
Jens Axboee65ef562019-03-12 10:16:44 -06002961
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002962 req->sqe = sqe_copy;
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03002963 req->flags |= REQ_F_FREE_SQE;
2964
2965 if (req->work.flags & IO_WQ_WORK_NEEDS_FILES) {
2966 ret = io_grab_files(req);
2967 if (ret)
2968 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002969 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03002970
2971 /*
2972 * Queued up for async execution, worker will release
2973 * submit reference when the iocb is actually submitted.
2974 */
2975 io_queue_async_work(req);
2976 return;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002977 }
Jens Axboee65ef562019-03-12 10:16:44 -06002978
Jens Axboefcb323c2019-10-24 12:39:47 -06002979err:
Jens Axboee65ef562019-03-12 10:16:44 -06002980 /* drop submission reference */
2981 io_put_req(req);
2982
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03002983 if (linked_timeout) {
Jens Axboe76a46e02019-11-10 23:34:16 -07002984 if (!ret)
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03002985 io_queue_linked_timeout(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07002986 else
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03002987 io_put_req(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07002988 }
2989
Jens Axboee65ef562019-03-12 10:16:44 -06002990 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06002991 if (ret) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07002992 io_cqring_add_event(req, ret);
Jens Axboe9e645e112019-05-10 16:07:28 -06002993 if (req->flags & REQ_F_LINK)
2994 req->flags |= REQ_F_FAIL_LINK;
Jens Axboee65ef562019-03-12 10:16:44 -06002995 io_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002996 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002997}
2998
Jens Axboe0e0702d2019-11-14 21:42:10 -07002999static void io_queue_sqe(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08003000{
3001 int ret;
3002
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003003 if (unlikely(req->ctx->drain_next)) {
3004 req->flags |= REQ_F_IO_DRAIN;
3005 req->ctx->drain_next = false;
3006 }
3007 req->ctx->drain_next = (req->flags & REQ_F_DRAIN_LINK);
3008
Jackie Liua197f662019-11-08 08:09:12 -07003009 ret = io_req_defer(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08003010 if (ret) {
3011 if (ret != -EIOCBQUEUED) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07003012 io_cqring_add_event(req, ret);
Pavel Begunkovd3b357962019-11-19 23:32:48 +03003013 if (req->flags & REQ_F_LINK)
3014 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07003015 io_double_put_req(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08003016 }
Jens Axboe0e0702d2019-11-14 21:42:10 -07003017 } else
3018 __io_queue_sqe(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08003019}
3020
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003021static inline void io_queue_link_head(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08003022{
Jens Axboe94ae5e72019-11-14 19:39:52 -07003023 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003024 io_cqring_add_event(req, -ECANCELED);
3025 io_double_put_req(req);
3026 } else
Jens Axboe0e0702d2019-11-14 21:42:10 -07003027 io_queue_sqe(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08003028}
3029
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003030
Jens Axboe9e645e112019-05-10 16:07:28 -06003031#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK)
3032
Jackie Liua197f662019-11-08 08:09:12 -07003033static void io_submit_sqe(struct io_kiocb *req, struct io_submit_state *state,
3034 struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06003035{
Jackie Liua197f662019-11-08 08:09:12 -07003036 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -06003037 int ret;
3038
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003039 req->user_data = req->sqe->user_data;
Jens Axboe78e19bb2019-11-06 15:21:34 -07003040
Jens Axboe9e645e112019-05-10 16:07:28 -06003041 /* enforce forwards compatibility on users */
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003042 if (unlikely(req->sqe->flags & ~SQE_VALID_FLAGS)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06003043 ret = -EINVAL;
Pavel Begunkov196be952019-11-07 01:41:06 +03003044 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06003045 }
3046
Jackie Liua197f662019-11-08 08:09:12 -07003047 ret = io_req_set_file(state, req);
Jens Axboe9e645e112019-05-10 16:07:28 -06003048 if (unlikely(ret)) {
3049err_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07003050 io_cqring_add_event(req, ret);
3051 io_double_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06003052 return;
3053 }
3054
Jens Axboe9e645e112019-05-10 16:07:28 -06003055 /*
3056 * If we already have a head request, queue this one for async
3057 * submittal once the head completes. If we don't have a head but
3058 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
3059 * submitted sync once the chain is complete. If none of those
3060 * conditions are true (normal request), then just queue it.
3061 */
3062 if (*link) {
3063 struct io_kiocb *prev = *link;
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03003064 struct io_uring_sqe *sqe_copy;
Jens Axboe9e645e112019-05-10 16:07:28 -06003065
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003066 if (req->sqe->flags & IOSQE_IO_DRAIN)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003067 (*link)->flags |= REQ_F_DRAIN_LINK | REQ_F_IO_DRAIN;
3068
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003069 if (READ_ONCE(req->sqe->opcode) == IORING_OP_LINK_TIMEOUT) {
Jens Axboe94ae5e72019-11-14 19:39:52 -07003070 ret = io_timeout_setup(req);
3071 /* common setup allows offset being set, we don't */
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003072 if (!ret && req->sqe->off)
Jens Axboe94ae5e72019-11-14 19:39:52 -07003073 ret = -EINVAL;
3074 if (ret) {
3075 prev->flags |= REQ_F_FAIL_LINK;
3076 goto err_req;
3077 }
3078 }
3079
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003080 sqe_copy = kmemdup(req->sqe, sizeof(*sqe_copy), GFP_KERNEL);
Jens Axboe9e645e112019-05-10 16:07:28 -06003081 if (!sqe_copy) {
3082 ret = -EAGAIN;
3083 goto err_req;
3084 }
3085
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003086 req->sqe = sqe_copy;
Jens Axboe94ae5e72019-11-14 19:39:52 -07003087 req->flags |= REQ_F_FREE_SQE;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02003088 trace_io_uring_link(ctx, req, prev);
Jens Axboe9e645e112019-05-10 16:07:28 -06003089 list_add_tail(&req->list, &prev->link_list);
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003090 } else if (req->sqe->flags & IOSQE_IO_LINK) {
Jens Axboe9e645e112019-05-10 16:07:28 -06003091 req->flags |= REQ_F_LINK;
3092
Jens Axboe9e645e112019-05-10 16:07:28 -06003093 INIT_LIST_HEAD(&req->link_list);
3094 *link = req;
3095 } else {
Jackie Liua197f662019-11-08 08:09:12 -07003096 io_queue_sqe(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06003097 }
3098}
3099
Jens Axboe9a56a232019-01-09 09:06:50 -07003100/*
3101 * Batched submission is done, ensure local IO is flushed out.
3102 */
3103static void io_submit_state_end(struct io_submit_state *state)
3104{
3105 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06003106 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07003107 if (state->free_reqs)
3108 kmem_cache_free_bulk(req_cachep, state->free_reqs,
3109 &state->reqs[state->cur_req]);
Jens Axboe9a56a232019-01-09 09:06:50 -07003110}
3111
3112/*
3113 * Start submission side cache.
3114 */
3115static void io_submit_state_start(struct io_submit_state *state,
3116 struct io_ring_ctx *ctx, unsigned max_ios)
3117{
3118 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07003119 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07003120 state->file = NULL;
3121 state->ios_left = max_ios;
3122}
3123
Jens Axboe2b188cc2019-01-07 10:46:33 -07003124static void io_commit_sqring(struct io_ring_ctx *ctx)
3125{
Hristo Venev75b28af2019-08-26 17:23:46 +00003126 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003127
Hristo Venev75b28af2019-08-26 17:23:46 +00003128 if (ctx->cached_sq_head != READ_ONCE(rings->sq.head)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07003129 /*
3130 * Ensure any loads from the SQEs are done at this point,
3131 * since once we write the new head, the application could
3132 * write new data to them.
3133 */
Hristo Venev75b28af2019-08-26 17:23:46 +00003134 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003135 }
3136}
3137
3138/*
Jens Axboe2b188cc2019-01-07 10:46:33 -07003139 * Fetch an sqe, if one is available. Note that s->sqe will point to memory
3140 * that is mapped by userspace. This means that care needs to be taken to
3141 * ensure that reads are stable, as we cannot rely on userspace always
3142 * being a good citizen. If members of the sqe are validated and then later
3143 * used, it's important that those reads are done through READ_ONCE() to
3144 * prevent a re-load down the line.
3145 */
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003146static bool io_get_sqring(struct io_ring_ctx *ctx, struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003147{
Hristo Venev75b28af2019-08-26 17:23:46 +00003148 struct io_rings *rings = ctx->rings;
3149 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003150 unsigned head;
3151
3152 /*
3153 * The cached sq head (or cq tail) serves two purposes:
3154 *
3155 * 1) allows us to batch the cost of updating the user visible
3156 * head updates.
3157 * 2) allows the kernel side to track the head on its own, even
3158 * though the application is the one updating it.
3159 */
3160 head = ctx->cached_sq_head;
Stefan Bühlere523a292019-04-19 11:57:44 +02003161 /* make sure SQ entry isn't read before tail */
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03003162 if (unlikely(head == smp_load_acquire(&rings->sq.tail)))
Jens Axboe2b188cc2019-01-07 10:46:33 -07003163 return false;
3164
Hristo Venev75b28af2019-08-26 17:23:46 +00003165 head = READ_ONCE(sq_array[head & ctx->sq_mask]);
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03003166 if (likely(head < ctx->sq_entries)) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003167 /*
3168 * All io need record the previous position, if LINK vs DARIN,
3169 * it can be used to mark the position of the first IO in the
3170 * link list.
3171 */
3172 req->sequence = ctx->cached_sq_head;
3173 req->sqe = &ctx->sq_sqes[head];
Jens Axboe2b188cc2019-01-07 10:46:33 -07003174 ctx->cached_sq_head++;
3175 return true;
3176 }
3177
3178 /* drop invalid entries */
3179 ctx->cached_sq_head++;
Jens Axboe498ccd92019-10-25 10:04:25 -06003180 ctx->cached_sq_dropped++;
3181 WRITE_ONCE(rings->sq_dropped, ctx->cached_sq_dropped);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003182 return false;
3183}
3184
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003185static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkovae9428c2019-11-06 00:22:14 +03003186 struct file *ring_file, int ring_fd,
3187 struct mm_struct **mm, bool async)
Jens Axboe6c271ce2019-01-10 11:22:30 -07003188{
3189 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06003190 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06003191 int i, submitted = 0;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03003192 bool mm_fault = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003193
Jens Axboec4a2ed72019-11-21 21:01:26 -07003194 /* if we have a backlog and couldn't flush it all, return BUSY */
3195 if (!list_empty(&ctx->cq_overflow_list) &&
3196 !io_cqring_overflow_flush(ctx, false))
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003197 return -EBUSY;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003198
3199 if (nr > IO_PLUG_THRESHOLD) {
3200 io_submit_state_start(&state, ctx, nr);
3201 statep = &state;
3202 }
3203
3204 for (i = 0; i < nr; i++) {
Pavel Begunkov196be952019-11-07 01:41:06 +03003205 struct io_kiocb *req;
Pavel Begunkov50585b92019-11-07 01:41:07 +03003206 unsigned int sqe_flags;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003207
Pavel Begunkov196be952019-11-07 01:41:06 +03003208 req = io_get_req(ctx, statep);
3209 if (unlikely(!req)) {
3210 if (!submitted)
3211 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003212 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06003213 }
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003214 if (!io_get_sqring(ctx, req)) {
Pavel Begunkov196be952019-11-07 01:41:06 +03003215 __io_free_req(req);
3216 break;
3217 }
Jens Axboe9e645e112019-05-10 16:07:28 -06003218
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003219 if (io_sqe_needs_user(req->sqe) && !*mm) {
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03003220 mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm);
3221 if (!mm_fault) {
3222 use_mm(ctx->sqo_mm);
3223 *mm = ctx->sqo_mm;
3224 }
3225 }
3226
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003227 sqe_flags = req->sqe->flags;
Pavel Begunkov50585b92019-11-07 01:41:07 +03003228
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003229 req->ring_file = ring_file;
3230 req->ring_fd = ring_fd;
3231 req->has_user = *mm != NULL;
3232 req->in_async = async;
3233 req->needs_fixed_file = async;
3234 trace_io_uring_submit_sqe(ctx, req->sqe->user_data,
Pavel Begunkov50585b92019-11-07 01:41:07 +03003235 true, async);
Jackie Liua197f662019-11-08 08:09:12 -07003236 io_submit_sqe(req, statep, &link);
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03003237 submitted++;
Pavel Begunkove5eb6362019-11-06 00:22:15 +03003238
3239 /*
3240 * If previous wasn't linked and we have a linked command,
3241 * that's the end of the chain. Submit the previous link.
3242 */
Pavel Begunkov50585b92019-11-07 01:41:07 +03003243 if (!(sqe_flags & IOSQE_IO_LINK) && link) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003244 io_queue_link_head(link);
Pavel Begunkove5eb6362019-11-06 00:22:15 +03003245 link = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003246 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07003247 }
3248
Jens Axboe9e645e112019-05-10 16:07:28 -06003249 if (link)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003250 io_queue_link_head(link);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003251 if (statep)
3252 io_submit_state_end(&state);
3253
Pavel Begunkovae9428c2019-11-06 00:22:14 +03003254 /* Commit SQ ring head once we've consumed and submitted all SQEs */
3255 io_commit_sqring(ctx);
3256
Jens Axboe6c271ce2019-01-10 11:22:30 -07003257 return submitted;
3258}
3259
3260static int io_sq_thread(void *data)
3261{
Jens Axboe6c271ce2019-01-10 11:22:30 -07003262 struct io_ring_ctx *ctx = data;
3263 struct mm_struct *cur_mm = NULL;
Jens Axboe181e4482019-11-25 08:52:30 -07003264 const struct cred *old_cred;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003265 mm_segment_t old_fs;
3266 DEFINE_WAIT(wait);
3267 unsigned inflight;
3268 unsigned long timeout;
Jens Axboec1edbf52019-11-10 16:56:04 -07003269 int ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003270
Jens Axboe206aefd2019-11-07 18:27:42 -07003271 complete(&ctx->completions[1]);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08003272
Jens Axboe6c271ce2019-01-10 11:22:30 -07003273 old_fs = get_fs();
3274 set_fs(USER_DS);
Jens Axboe181e4482019-11-25 08:52:30 -07003275 old_cred = override_creds(ctx->creds);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003276
Jens Axboec1edbf52019-11-10 16:56:04 -07003277 ret = timeout = inflight = 0;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003278 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003279 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003280
3281 if (inflight) {
3282 unsigned nr_events = 0;
3283
3284 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe2b2ed972019-10-25 10:06:15 -06003285 /*
3286 * inflight is the count of the maximum possible
3287 * entries we submitted, but it can be smaller
3288 * if we dropped some of them. If we don't have
3289 * poll entries available, then we know that we
3290 * have nothing left to poll for. Reset the
3291 * inflight count to zero in that case.
3292 */
3293 mutex_lock(&ctx->uring_lock);
3294 if (!list_empty(&ctx->poll_list))
3295 __io_iopoll_check(ctx, &nr_events, 0);
3296 else
3297 inflight = 0;
3298 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003299 } else {
3300 /*
3301 * Normal IO, just pretend everything completed.
3302 * We don't have to poll completions for that.
3303 */
3304 nr_events = inflight;
3305 }
3306
3307 inflight -= nr_events;
3308 if (!inflight)
3309 timeout = jiffies + ctx->sq_thread_idle;
3310 }
3311
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003312 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07003313
3314 /*
3315 * If submit got -EBUSY, flag us as needing the application
3316 * to enter the kernel to reap and flush events.
3317 */
3318 if (!to_submit || ret == -EBUSY) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07003319 /*
3320 * We're polling. If we're within the defined idle
3321 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07003322 * to sleep. The exception is if we got EBUSY doing
3323 * more IO, we should wait for the application to
3324 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07003325 */
Jens Axboec1edbf52019-11-10 16:56:04 -07003326 if (inflight ||
3327 (!time_after(jiffies, timeout) && ret != -EBUSY)) {
Jens Axboe9831a902019-09-19 09:48:55 -06003328 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07003329 continue;
3330 }
3331
3332 /*
3333 * Drop cur_mm before scheduling, we can't hold it for
3334 * long periods (or over schedule()). Do this before
3335 * adding ourselves to the waitqueue, as the unuse/drop
3336 * may sleep.
3337 */
3338 if (cur_mm) {
3339 unuse_mm(cur_mm);
3340 mmput(cur_mm);
3341 cur_mm = NULL;
3342 }
3343
3344 prepare_to_wait(&ctx->sqo_wait, &wait,
3345 TASK_INTERRUPTIBLE);
3346
3347 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00003348 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02003349 /* make sure to read SQ tail after writing flags */
3350 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07003351
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003352 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07003353 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003354 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07003355 finish_wait(&ctx->sqo_wait, &wait);
3356 break;
3357 }
3358 if (signal_pending(current))
3359 flush_signals(current);
3360 schedule();
3361 finish_wait(&ctx->sqo_wait, &wait);
3362
Hristo Venev75b28af2019-08-26 17:23:46 +00003363 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003364 continue;
3365 }
3366 finish_wait(&ctx->sqo_wait, &wait);
3367
Hristo Venev75b28af2019-08-26 17:23:46 +00003368 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003369 }
3370
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003371 to_submit = min(to_submit, ctx->sq_entries);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003372 ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true);
3373 if (ret > 0)
3374 inflight += ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003375 }
3376
3377 set_fs(old_fs);
3378 if (cur_mm) {
3379 unuse_mm(cur_mm);
3380 mmput(cur_mm);
3381 }
Jens Axboe181e4482019-11-25 08:52:30 -07003382 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06003383
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003384 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06003385
Jens Axboe6c271ce2019-01-10 11:22:30 -07003386 return 0;
3387}
3388
Jens Axboebda52162019-09-24 13:47:15 -06003389struct io_wait_queue {
3390 struct wait_queue_entry wq;
3391 struct io_ring_ctx *ctx;
3392 unsigned to_wait;
3393 unsigned nr_timeouts;
3394};
3395
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003396static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06003397{
3398 struct io_ring_ctx *ctx = iowq->ctx;
3399
3400 /*
3401 * Wake up if we have enough events, or if a timeout occured since we
3402 * started waiting. For timeouts, we always want to return to userspace,
3403 * regardless of event count.
3404 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003405 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06003406 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
3407}
3408
3409static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
3410 int wake_flags, void *key)
3411{
3412 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
3413 wq);
3414
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003415 /* use noflush == true, as we can't safely rely on locking context */
3416 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06003417 return -1;
3418
3419 return autoremove_wake_function(curr, mode, wake_flags, key);
3420}
3421
Jens Axboe2b188cc2019-01-07 10:46:33 -07003422/*
3423 * Wait until events become available, if we don't already have some. The
3424 * application must reap them itself, as they reside on the shared cq ring.
3425 */
3426static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
3427 const sigset_t __user *sig, size_t sigsz)
3428{
Jens Axboebda52162019-09-24 13:47:15 -06003429 struct io_wait_queue iowq = {
3430 .wq = {
3431 .private = current,
3432 .func = io_wake_function,
3433 .entry = LIST_HEAD_INIT(iowq.wq.entry),
3434 },
3435 .ctx = ctx,
3436 .to_wait = min_events,
3437 };
Hristo Venev75b28af2019-08-26 17:23:46 +00003438 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08003439 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003440
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003441 if (io_cqring_events(ctx, false) >= min_events)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003442 return 0;
3443
3444 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01003445#ifdef CONFIG_COMPAT
3446 if (in_compat_syscall())
3447 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07003448 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01003449 else
3450#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07003451 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01003452
Jens Axboe2b188cc2019-01-07 10:46:33 -07003453 if (ret)
3454 return ret;
3455 }
3456
Jens Axboebda52162019-09-24 13:47:15 -06003457 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02003458 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06003459 do {
3460 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
3461 TASK_INTERRUPTIBLE);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003462 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06003463 break;
3464 schedule();
3465 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08003466 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06003467 break;
3468 }
3469 } while (1);
3470 finish_wait(&ctx->wait, &iowq.wq);
3471
Jackie Liue9ffa5c2019-10-29 11:16:42 +08003472 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003473
Hristo Venev75b28af2019-08-26 17:23:46 +00003474 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003475}
3476
Jens Axboe6b063142019-01-10 22:13:58 -07003477static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
3478{
3479#if defined(CONFIG_UNIX)
3480 if (ctx->ring_sock) {
3481 struct sock *sock = ctx->ring_sock->sk;
3482 struct sk_buff *skb;
3483
3484 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
3485 kfree_skb(skb);
3486 }
3487#else
3488 int i;
3489
Jens Axboe65e19f52019-10-26 07:20:21 -06003490 for (i = 0; i < ctx->nr_user_files; i++) {
3491 struct file *file;
3492
3493 file = io_file_from_index(ctx, i);
3494 if (file)
3495 fput(file);
3496 }
Jens Axboe6b063142019-01-10 22:13:58 -07003497#endif
3498}
3499
3500static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
3501{
Jens Axboe65e19f52019-10-26 07:20:21 -06003502 unsigned nr_tables, i;
3503
3504 if (!ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003505 return -ENXIO;
3506
3507 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06003508 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
3509 for (i = 0; i < nr_tables; i++)
3510 kfree(ctx->file_table[i].files);
3511 kfree(ctx->file_table);
3512 ctx->file_table = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003513 ctx->nr_user_files = 0;
3514 return 0;
3515}
3516
Jens Axboe6c271ce2019-01-10 11:22:30 -07003517static void io_sq_thread_stop(struct io_ring_ctx *ctx)
3518{
3519 if (ctx->sqo_thread) {
Jens Axboe206aefd2019-11-07 18:27:42 -07003520 wait_for_completion(&ctx->completions[1]);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003521 /*
3522 * The park is a bit of a work-around, without it we get
3523 * warning spews on shutdown with SQPOLL set and affinity
3524 * set to a single CPU.
3525 */
Jens Axboe06058632019-04-13 09:26:03 -06003526 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003527 kthread_stop(ctx->sqo_thread);
3528 ctx->sqo_thread = NULL;
3529 }
3530}
3531
Jens Axboe6b063142019-01-10 22:13:58 -07003532static void io_finish_async(struct io_ring_ctx *ctx)
3533{
Jens Axboe6c271ce2019-01-10 11:22:30 -07003534 io_sq_thread_stop(ctx);
3535
Jens Axboe561fb042019-10-24 07:25:42 -06003536 if (ctx->io_wq) {
3537 io_wq_destroy(ctx->io_wq);
3538 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003539 }
3540}
3541
3542#if defined(CONFIG_UNIX)
3543static void io_destruct_skb(struct sk_buff *skb)
3544{
3545 struct io_ring_ctx *ctx = skb->sk->sk_user_data;
3546
Jens Axboe561fb042019-10-24 07:25:42 -06003547 if (ctx->io_wq)
3548 io_wq_flush(ctx->io_wq);
Jens Axboe8a997342019-10-09 14:40:13 -06003549
Jens Axboe6b063142019-01-10 22:13:58 -07003550 unix_destruct_scm(skb);
3551}
3552
3553/*
3554 * Ensure the UNIX gc is aware of our file set, so we are certain that
3555 * the io_uring can be safely unregistered on process exit, even if we have
3556 * loops in the file referencing.
3557 */
3558static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
3559{
3560 struct sock *sk = ctx->ring_sock->sk;
3561 struct scm_fp_list *fpl;
3562 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06003563 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07003564
3565 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
3566 unsigned long inflight = ctx->user->unix_inflight + nr;
3567
3568 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
3569 return -EMFILE;
3570 }
3571
3572 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
3573 if (!fpl)
3574 return -ENOMEM;
3575
3576 skb = alloc_skb(0, GFP_KERNEL);
3577 if (!skb) {
3578 kfree(fpl);
3579 return -ENOMEM;
3580 }
3581
3582 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07003583
Jens Axboe08a45172019-10-03 08:11:03 -06003584 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07003585 fpl->user = get_uid(ctx->user);
3586 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003587 struct file *file = io_file_from_index(ctx, i + offset);
3588
3589 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06003590 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06003591 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06003592 unix_inflight(fpl->user, fpl->fp[nr_files]);
3593 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07003594 }
3595
Jens Axboe08a45172019-10-03 08:11:03 -06003596 if (nr_files) {
3597 fpl->max = SCM_MAX_FD;
3598 fpl->count = nr_files;
3599 UNIXCB(skb).fp = fpl;
3600 skb->destructor = io_destruct_skb;
3601 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
3602 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07003603
Jens Axboe08a45172019-10-03 08:11:03 -06003604 for (i = 0; i < nr_files; i++)
3605 fput(fpl->fp[i]);
3606 } else {
3607 kfree_skb(skb);
3608 kfree(fpl);
3609 }
Jens Axboe6b063142019-01-10 22:13:58 -07003610
3611 return 0;
3612}
3613
3614/*
3615 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
3616 * causes regular reference counting to break down. We rely on the UNIX
3617 * garbage collection to take care of this problem for us.
3618 */
3619static int io_sqe_files_scm(struct io_ring_ctx *ctx)
3620{
3621 unsigned left, total;
3622 int ret = 0;
3623
3624 total = 0;
3625 left = ctx->nr_user_files;
3626 while (left) {
3627 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07003628
3629 ret = __io_sqe_files_scm(ctx, this_files, total);
3630 if (ret)
3631 break;
3632 left -= this_files;
3633 total += this_files;
3634 }
3635
3636 if (!ret)
3637 return 0;
3638
3639 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003640 struct file *file = io_file_from_index(ctx, total);
3641
3642 if (file)
3643 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07003644 total++;
3645 }
3646
3647 return ret;
3648}
3649#else
3650static int io_sqe_files_scm(struct io_ring_ctx *ctx)
3651{
3652 return 0;
3653}
3654#endif
3655
Jens Axboe65e19f52019-10-26 07:20:21 -06003656static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
3657 unsigned nr_files)
3658{
3659 int i;
3660
3661 for (i = 0; i < nr_tables; i++) {
3662 struct fixed_file_table *table = &ctx->file_table[i];
3663 unsigned this_files;
3664
3665 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
3666 table->files = kcalloc(this_files, sizeof(struct file *),
3667 GFP_KERNEL);
3668 if (!table->files)
3669 break;
3670 nr_files -= this_files;
3671 }
3672
3673 if (i == nr_tables)
3674 return 0;
3675
3676 for (i = 0; i < nr_tables; i++) {
3677 struct fixed_file_table *table = &ctx->file_table[i];
3678 kfree(table->files);
3679 }
3680 return 1;
3681}
3682
Jens Axboe6b063142019-01-10 22:13:58 -07003683static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
3684 unsigned nr_args)
3685{
3686 __s32 __user *fds = (__s32 __user *) arg;
Jens Axboe65e19f52019-10-26 07:20:21 -06003687 unsigned nr_tables;
Jens Axboe6b063142019-01-10 22:13:58 -07003688 int fd, ret = 0;
3689 unsigned i;
3690
Jens Axboe65e19f52019-10-26 07:20:21 -06003691 if (ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003692 return -EBUSY;
3693 if (!nr_args)
3694 return -EINVAL;
3695 if (nr_args > IORING_MAX_FIXED_FILES)
3696 return -EMFILE;
3697
Jens Axboe65e19f52019-10-26 07:20:21 -06003698 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
3699 ctx->file_table = kcalloc(nr_tables, sizeof(struct fixed_file_table),
3700 GFP_KERNEL);
3701 if (!ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003702 return -ENOMEM;
3703
Jens Axboe65e19f52019-10-26 07:20:21 -06003704 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
3705 kfree(ctx->file_table);
Jens Axboe46568e92019-11-10 08:40:53 -07003706 ctx->file_table = NULL;
Jens Axboe65e19f52019-10-26 07:20:21 -06003707 return -ENOMEM;
3708 }
3709
Jens Axboe08a45172019-10-03 08:11:03 -06003710 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003711 struct fixed_file_table *table;
3712 unsigned index;
3713
Jens Axboe6b063142019-01-10 22:13:58 -07003714 ret = -EFAULT;
3715 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
3716 break;
Jens Axboe08a45172019-10-03 08:11:03 -06003717 /* allow sparse sets */
3718 if (fd == -1) {
3719 ret = 0;
3720 continue;
3721 }
Jens Axboe6b063142019-01-10 22:13:58 -07003722
Jens Axboe65e19f52019-10-26 07:20:21 -06003723 table = &ctx->file_table[i >> IORING_FILE_TABLE_SHIFT];
3724 index = i & IORING_FILE_TABLE_MASK;
3725 table->files[index] = fget(fd);
Jens Axboe6b063142019-01-10 22:13:58 -07003726
3727 ret = -EBADF;
Jens Axboe65e19f52019-10-26 07:20:21 -06003728 if (!table->files[index])
Jens Axboe6b063142019-01-10 22:13:58 -07003729 break;
3730 /*
3731 * Don't allow io_uring instances to be registered. If UNIX
3732 * isn't enabled, then this causes a reference cycle and this
3733 * instance can never get freed. If UNIX is enabled we'll
3734 * handle it just fine, but there's still no point in allowing
3735 * a ring fd as it doesn't support regular read/write anyway.
3736 */
Jens Axboe65e19f52019-10-26 07:20:21 -06003737 if (table->files[index]->f_op == &io_uring_fops) {
3738 fput(table->files[index]);
Jens Axboe6b063142019-01-10 22:13:58 -07003739 break;
3740 }
Jens Axboe6b063142019-01-10 22:13:58 -07003741 ret = 0;
3742 }
3743
3744 if (ret) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003745 for (i = 0; i < ctx->nr_user_files; i++) {
3746 struct file *file;
Jens Axboe6b063142019-01-10 22:13:58 -07003747
Jens Axboe65e19f52019-10-26 07:20:21 -06003748 file = io_file_from_index(ctx, i);
3749 if (file)
3750 fput(file);
3751 }
3752 for (i = 0; i < nr_tables; i++)
3753 kfree(ctx->file_table[i].files);
3754
3755 kfree(ctx->file_table);
3756 ctx->file_table = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003757 ctx->nr_user_files = 0;
3758 return ret;
3759 }
3760
3761 ret = io_sqe_files_scm(ctx);
3762 if (ret)
3763 io_sqe_files_unregister(ctx);
3764
3765 return ret;
3766}
3767
Jens Axboec3a31e62019-10-03 13:59:56 -06003768static void io_sqe_file_unregister(struct io_ring_ctx *ctx, int index)
3769{
3770#if defined(CONFIG_UNIX)
Jens Axboe65e19f52019-10-26 07:20:21 -06003771 struct file *file = io_file_from_index(ctx, index);
Jens Axboec3a31e62019-10-03 13:59:56 -06003772 struct sock *sock = ctx->ring_sock->sk;
3773 struct sk_buff_head list, *head = &sock->sk_receive_queue;
3774 struct sk_buff *skb;
3775 int i;
3776
3777 __skb_queue_head_init(&list);
3778
3779 /*
3780 * Find the skb that holds this file in its SCM_RIGHTS. When found,
3781 * remove this entry and rearrange the file array.
3782 */
3783 skb = skb_dequeue(head);
3784 while (skb) {
3785 struct scm_fp_list *fp;
3786
3787 fp = UNIXCB(skb).fp;
3788 for (i = 0; i < fp->count; i++) {
3789 int left;
3790
3791 if (fp->fp[i] != file)
3792 continue;
3793
3794 unix_notinflight(fp->user, fp->fp[i]);
3795 left = fp->count - 1 - i;
3796 if (left) {
3797 memmove(&fp->fp[i], &fp->fp[i + 1],
3798 left * sizeof(struct file *));
3799 }
3800 fp->count--;
3801 if (!fp->count) {
3802 kfree_skb(skb);
3803 skb = NULL;
3804 } else {
3805 __skb_queue_tail(&list, skb);
3806 }
3807 fput(file);
3808 file = NULL;
3809 break;
3810 }
3811
3812 if (!file)
3813 break;
3814
3815 __skb_queue_tail(&list, skb);
3816
3817 skb = skb_dequeue(head);
3818 }
3819
3820 if (skb_peek(&list)) {
3821 spin_lock_irq(&head->lock);
3822 while ((skb = __skb_dequeue(&list)) != NULL)
3823 __skb_queue_tail(head, skb);
3824 spin_unlock_irq(&head->lock);
3825 }
3826#else
Jens Axboe65e19f52019-10-26 07:20:21 -06003827 fput(io_file_from_index(ctx, index));
Jens Axboec3a31e62019-10-03 13:59:56 -06003828#endif
3829}
3830
3831static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
3832 int index)
3833{
3834#if defined(CONFIG_UNIX)
3835 struct sock *sock = ctx->ring_sock->sk;
3836 struct sk_buff_head *head = &sock->sk_receive_queue;
3837 struct sk_buff *skb;
3838
3839 /*
3840 * See if we can merge this file into an existing skb SCM_RIGHTS
3841 * file set. If there's no room, fall back to allocating a new skb
3842 * and filling it in.
3843 */
3844 spin_lock_irq(&head->lock);
3845 skb = skb_peek(head);
3846 if (skb) {
3847 struct scm_fp_list *fpl = UNIXCB(skb).fp;
3848
3849 if (fpl->count < SCM_MAX_FD) {
3850 __skb_unlink(skb, head);
3851 spin_unlock_irq(&head->lock);
3852 fpl->fp[fpl->count] = get_file(file);
3853 unix_inflight(fpl->user, fpl->fp[fpl->count]);
3854 fpl->count++;
3855 spin_lock_irq(&head->lock);
3856 __skb_queue_head(head, skb);
3857 } else {
3858 skb = NULL;
3859 }
3860 }
3861 spin_unlock_irq(&head->lock);
3862
3863 if (skb) {
3864 fput(file);
3865 return 0;
3866 }
3867
3868 return __io_sqe_files_scm(ctx, 1, index);
3869#else
3870 return 0;
3871#endif
3872}
3873
3874static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
3875 unsigned nr_args)
3876{
3877 struct io_uring_files_update up;
3878 __s32 __user *fds;
3879 int fd, i, err;
3880 __u32 done;
3881
Jens Axboe65e19f52019-10-26 07:20:21 -06003882 if (!ctx->file_table)
Jens Axboec3a31e62019-10-03 13:59:56 -06003883 return -ENXIO;
3884 if (!nr_args)
3885 return -EINVAL;
3886 if (copy_from_user(&up, arg, sizeof(up)))
3887 return -EFAULT;
3888 if (check_add_overflow(up.offset, nr_args, &done))
3889 return -EOVERFLOW;
3890 if (done > ctx->nr_user_files)
3891 return -EINVAL;
3892
3893 done = 0;
3894 fds = (__s32 __user *) up.fds;
3895 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003896 struct fixed_file_table *table;
3897 unsigned index;
3898
Jens Axboec3a31e62019-10-03 13:59:56 -06003899 err = 0;
3900 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
3901 err = -EFAULT;
3902 break;
3903 }
3904 i = array_index_nospec(up.offset, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06003905 table = &ctx->file_table[i >> IORING_FILE_TABLE_SHIFT];
3906 index = i & IORING_FILE_TABLE_MASK;
3907 if (table->files[index]) {
Jens Axboec3a31e62019-10-03 13:59:56 -06003908 io_sqe_file_unregister(ctx, i);
Jens Axboe65e19f52019-10-26 07:20:21 -06003909 table->files[index] = NULL;
Jens Axboec3a31e62019-10-03 13:59:56 -06003910 }
3911 if (fd != -1) {
3912 struct file *file;
3913
3914 file = fget(fd);
3915 if (!file) {
3916 err = -EBADF;
3917 break;
3918 }
3919 /*
3920 * Don't allow io_uring instances to be registered. If
3921 * UNIX isn't enabled, then this causes a reference
3922 * cycle and this instance can never get freed. If UNIX
3923 * is enabled we'll handle it just fine, but there's
3924 * still no point in allowing a ring fd as it doesn't
3925 * support regular read/write anyway.
3926 */
3927 if (file->f_op == &io_uring_fops) {
3928 fput(file);
3929 err = -EBADF;
3930 break;
3931 }
Jens Axboe65e19f52019-10-26 07:20:21 -06003932 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06003933 err = io_sqe_file_register(ctx, file, i);
3934 if (err)
3935 break;
3936 }
3937 nr_args--;
3938 done++;
3939 up.offset++;
3940 }
3941
3942 return done ? done : err;
3943}
3944
Jens Axboe7d723062019-11-12 22:31:31 -07003945static void io_put_work(struct io_wq_work *work)
3946{
3947 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
3948
3949 io_put_req(req);
3950}
3951
3952static void io_get_work(struct io_wq_work *work)
3953{
3954 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
3955
3956 refcount_inc(&req->refs);
3957}
3958
Jens Axboe6c271ce2019-01-10 11:22:30 -07003959static int io_sq_offload_start(struct io_ring_ctx *ctx,
3960 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003961{
Jens Axboe576a3472019-11-25 08:49:20 -07003962 struct io_wq_data data;
Jens Axboe561fb042019-10-24 07:25:42 -06003963 unsigned concurrency;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003964 int ret;
3965
Jens Axboe6c271ce2019-01-10 11:22:30 -07003966 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003967 mmgrab(current->mm);
3968 ctx->sqo_mm = current->mm;
3969
Jens Axboe6c271ce2019-01-10 11:22:30 -07003970 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06003971 ret = -EPERM;
3972 if (!capable(CAP_SYS_ADMIN))
3973 goto err;
3974
Jens Axboe917257d2019-04-13 09:28:55 -06003975 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
3976 if (!ctx->sq_thread_idle)
3977 ctx->sq_thread_idle = HZ;
3978
Jens Axboe6c271ce2019-01-10 11:22:30 -07003979 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06003980 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003981
Jens Axboe917257d2019-04-13 09:28:55 -06003982 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06003983 if (cpu >= nr_cpu_ids)
3984 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08003985 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06003986 goto err;
3987
Jens Axboe6c271ce2019-01-10 11:22:30 -07003988 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
3989 ctx, cpu,
3990 "io_uring-sq");
3991 } else {
3992 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
3993 "io_uring-sq");
3994 }
3995 if (IS_ERR(ctx->sqo_thread)) {
3996 ret = PTR_ERR(ctx->sqo_thread);
3997 ctx->sqo_thread = NULL;
3998 goto err;
3999 }
4000 wake_up_process(ctx->sqo_thread);
4001 } else if (p->flags & IORING_SETUP_SQ_AFF) {
4002 /* Can't have SQ_AFF without SQPOLL */
4003 ret = -EINVAL;
4004 goto err;
4005 }
4006
Jens Axboe576a3472019-11-25 08:49:20 -07004007 data.mm = ctx->sqo_mm;
4008 data.user = ctx->user;
Jens Axboe181e4482019-11-25 08:52:30 -07004009 data.creds = ctx->creds;
Jens Axboe576a3472019-11-25 08:49:20 -07004010 data.get_work = io_get_work;
4011 data.put_work = io_put_work;
4012
Jens Axboe561fb042019-10-24 07:25:42 -06004013 /* Do QD, or 4 * CPUS, whatever is smallest */
4014 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
Jens Axboe576a3472019-11-25 08:49:20 -07004015 ctx->io_wq = io_wq_create(concurrency, &data);
Jens Axboe975c99a52019-10-30 08:42:56 -06004016 if (IS_ERR(ctx->io_wq)) {
4017 ret = PTR_ERR(ctx->io_wq);
4018 ctx->io_wq = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004019 goto err;
4020 }
4021
4022 return 0;
4023err:
Jens Axboe54a91f32019-09-10 09:15:04 -06004024 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004025 mmdrop(ctx->sqo_mm);
4026 ctx->sqo_mm = NULL;
4027 return ret;
4028}
4029
4030static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
4031{
4032 atomic_long_sub(nr_pages, &user->locked_vm);
4033}
4034
4035static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
4036{
4037 unsigned long page_limit, cur_pages, new_pages;
4038
4039 /* Don't allow more pages than we can safely lock */
4040 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
4041
4042 do {
4043 cur_pages = atomic_long_read(&user->locked_vm);
4044 new_pages = cur_pages + nr_pages;
4045 if (new_pages > page_limit)
4046 return -ENOMEM;
4047 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
4048 new_pages) != cur_pages);
4049
4050 return 0;
4051}
4052
4053static void io_mem_free(void *ptr)
4054{
Mark Rutland52e04ef2019-04-30 17:30:21 +01004055 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004056
Mark Rutland52e04ef2019-04-30 17:30:21 +01004057 if (!ptr)
4058 return;
4059
4060 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004061 if (put_page_testzero(page))
4062 free_compound_page(page);
4063}
4064
4065static void *io_mem_alloc(size_t size)
4066{
4067 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
4068 __GFP_NORETRY;
4069
4070 return (void *) __get_free_pages(gfp_flags, get_order(size));
4071}
4072
Hristo Venev75b28af2019-08-26 17:23:46 +00004073static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
4074 size_t *sq_offset)
4075{
4076 struct io_rings *rings;
4077 size_t off, sq_array_size;
4078
4079 off = struct_size(rings, cqes, cq_entries);
4080 if (off == SIZE_MAX)
4081 return SIZE_MAX;
4082
4083#ifdef CONFIG_SMP
4084 off = ALIGN(off, SMP_CACHE_BYTES);
4085 if (off == 0)
4086 return SIZE_MAX;
4087#endif
4088
4089 sq_array_size = array_size(sizeof(u32), sq_entries);
4090 if (sq_array_size == SIZE_MAX)
4091 return SIZE_MAX;
4092
4093 if (check_add_overflow(off, sq_array_size, &off))
4094 return SIZE_MAX;
4095
4096 if (sq_offset)
4097 *sq_offset = off;
4098
4099 return off;
4100}
4101
Jens Axboe2b188cc2019-01-07 10:46:33 -07004102static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
4103{
Hristo Venev75b28af2019-08-26 17:23:46 +00004104 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004105
Hristo Venev75b28af2019-08-26 17:23:46 +00004106 pages = (size_t)1 << get_order(
4107 rings_size(sq_entries, cq_entries, NULL));
4108 pages += (size_t)1 << get_order(
4109 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07004110
Hristo Venev75b28af2019-08-26 17:23:46 +00004111 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004112}
4113
Jens Axboeedafcce2019-01-09 09:16:05 -07004114static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
4115{
4116 int i, j;
4117
4118 if (!ctx->user_bufs)
4119 return -ENXIO;
4120
4121 for (i = 0; i < ctx->nr_user_bufs; i++) {
4122 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
4123
4124 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbard27c4d3a2019-08-04 19:32:06 -07004125 put_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07004126
4127 if (ctx->account_mem)
4128 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004129 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07004130 imu->nr_bvecs = 0;
4131 }
4132
4133 kfree(ctx->user_bufs);
4134 ctx->user_bufs = NULL;
4135 ctx->nr_user_bufs = 0;
4136 return 0;
4137}
4138
4139static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
4140 void __user *arg, unsigned index)
4141{
4142 struct iovec __user *src;
4143
4144#ifdef CONFIG_COMPAT
4145 if (ctx->compat) {
4146 struct compat_iovec __user *ciovs;
4147 struct compat_iovec ciov;
4148
4149 ciovs = (struct compat_iovec __user *) arg;
4150 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
4151 return -EFAULT;
4152
4153 dst->iov_base = (void __user *) (unsigned long) ciov.iov_base;
4154 dst->iov_len = ciov.iov_len;
4155 return 0;
4156 }
4157#endif
4158 src = (struct iovec __user *) arg;
4159 if (copy_from_user(dst, &src[index], sizeof(*dst)))
4160 return -EFAULT;
4161 return 0;
4162}
4163
4164static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
4165 unsigned nr_args)
4166{
4167 struct vm_area_struct **vmas = NULL;
4168 struct page **pages = NULL;
4169 int i, j, got_pages = 0;
4170 int ret = -EINVAL;
4171
4172 if (ctx->user_bufs)
4173 return -EBUSY;
4174 if (!nr_args || nr_args > UIO_MAXIOV)
4175 return -EINVAL;
4176
4177 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
4178 GFP_KERNEL);
4179 if (!ctx->user_bufs)
4180 return -ENOMEM;
4181
4182 for (i = 0; i < nr_args; i++) {
4183 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
4184 unsigned long off, start, end, ubuf;
4185 int pret, nr_pages;
4186 struct iovec iov;
4187 size_t size;
4188
4189 ret = io_copy_iov(ctx, &iov, arg, i);
4190 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03004191 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07004192
4193 /*
4194 * Don't impose further limits on the size and buffer
4195 * constraints here, we'll -EINVAL later when IO is
4196 * submitted if they are wrong.
4197 */
4198 ret = -EFAULT;
4199 if (!iov.iov_base || !iov.iov_len)
4200 goto err;
4201
4202 /* arbitrary limit, but we need something */
4203 if (iov.iov_len > SZ_1G)
4204 goto err;
4205
4206 ubuf = (unsigned long) iov.iov_base;
4207 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
4208 start = ubuf >> PAGE_SHIFT;
4209 nr_pages = end - start;
4210
4211 if (ctx->account_mem) {
4212 ret = io_account_mem(ctx->user, nr_pages);
4213 if (ret)
4214 goto err;
4215 }
4216
4217 ret = 0;
4218 if (!pages || nr_pages > got_pages) {
4219 kfree(vmas);
4220 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004221 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07004222 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004223 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07004224 sizeof(struct vm_area_struct *),
4225 GFP_KERNEL);
4226 if (!pages || !vmas) {
4227 ret = -ENOMEM;
4228 if (ctx->account_mem)
4229 io_unaccount_mem(ctx->user, nr_pages);
4230 goto err;
4231 }
4232 got_pages = nr_pages;
4233 }
4234
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004235 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07004236 GFP_KERNEL);
4237 ret = -ENOMEM;
4238 if (!imu->bvec) {
4239 if (ctx->account_mem)
4240 io_unaccount_mem(ctx->user, nr_pages);
4241 goto err;
4242 }
4243
4244 ret = 0;
4245 down_read(&current->mm->mmap_sem);
Ira Weiny932f4a62019-05-13 17:17:03 -07004246 pret = get_user_pages(ubuf, nr_pages,
4247 FOLL_WRITE | FOLL_LONGTERM,
4248 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07004249 if (pret == nr_pages) {
4250 /* don't support file backed memory */
4251 for (j = 0; j < nr_pages; j++) {
4252 struct vm_area_struct *vma = vmas[j];
4253
4254 if (vma->vm_file &&
4255 !is_file_hugepages(vma->vm_file)) {
4256 ret = -EOPNOTSUPP;
4257 break;
4258 }
4259 }
4260 } else {
4261 ret = pret < 0 ? pret : -EFAULT;
4262 }
4263 up_read(&current->mm->mmap_sem);
4264 if (ret) {
4265 /*
4266 * if we did partial map, or found file backed vmas,
4267 * release any pages we did get
4268 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07004269 if (pret > 0)
4270 put_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07004271 if (ctx->account_mem)
4272 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004273 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07004274 goto err;
4275 }
4276
4277 off = ubuf & ~PAGE_MASK;
4278 size = iov.iov_len;
4279 for (j = 0; j < nr_pages; j++) {
4280 size_t vec_len;
4281
4282 vec_len = min_t(size_t, size, PAGE_SIZE - off);
4283 imu->bvec[j].bv_page = pages[j];
4284 imu->bvec[j].bv_len = vec_len;
4285 imu->bvec[j].bv_offset = off;
4286 off = 0;
4287 size -= vec_len;
4288 }
4289 /* store original address for later verification */
4290 imu->ubuf = ubuf;
4291 imu->len = iov.iov_len;
4292 imu->nr_bvecs = nr_pages;
4293
4294 ctx->nr_user_bufs++;
4295 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004296 kvfree(pages);
4297 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07004298 return 0;
4299err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004300 kvfree(pages);
4301 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07004302 io_sqe_buffer_unregister(ctx);
4303 return ret;
4304}
4305
Jens Axboe9b402842019-04-11 11:45:41 -06004306static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
4307{
4308 __s32 __user *fds = arg;
4309 int fd;
4310
4311 if (ctx->cq_ev_fd)
4312 return -EBUSY;
4313
4314 if (copy_from_user(&fd, fds, sizeof(*fds)))
4315 return -EFAULT;
4316
4317 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
4318 if (IS_ERR(ctx->cq_ev_fd)) {
4319 int ret = PTR_ERR(ctx->cq_ev_fd);
4320 ctx->cq_ev_fd = NULL;
4321 return ret;
4322 }
4323
4324 return 0;
4325}
4326
4327static int io_eventfd_unregister(struct io_ring_ctx *ctx)
4328{
4329 if (ctx->cq_ev_fd) {
4330 eventfd_ctx_put(ctx->cq_ev_fd);
4331 ctx->cq_ev_fd = NULL;
4332 return 0;
4333 }
4334
4335 return -ENXIO;
4336}
4337
Jens Axboe2b188cc2019-01-07 10:46:33 -07004338static void io_ring_ctx_free(struct io_ring_ctx *ctx)
4339{
Jens Axboe6b063142019-01-10 22:13:58 -07004340 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004341 if (ctx->sqo_mm)
4342 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07004343
4344 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07004345 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07004346 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06004347 io_eventfd_unregister(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07004348
Jens Axboe2b188cc2019-01-07 10:46:33 -07004349#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07004350 if (ctx->ring_sock) {
4351 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004352 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07004353 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004354#endif
4355
Hristo Venev75b28af2019-08-26 17:23:46 +00004356 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004357 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004358
4359 percpu_ref_exit(&ctx->refs);
4360 if (ctx->account_mem)
4361 io_unaccount_mem(ctx->user,
4362 ring_pages(ctx->sq_entries, ctx->cq_entries));
4363 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07004364 put_cred(ctx->creds);
Jens Axboe206aefd2019-11-07 18:27:42 -07004365 kfree(ctx->completions);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07004366 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004367 kfree(ctx);
4368}
4369
4370static __poll_t io_uring_poll(struct file *file, poll_table *wait)
4371{
4372 struct io_ring_ctx *ctx = file->private_data;
4373 __poll_t mask = 0;
4374
4375 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02004376 /*
4377 * synchronizes with barrier from wq_has_sleeper call in
4378 * io_commit_cqring
4379 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004380 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00004381 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
4382 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004383 mask |= EPOLLOUT | EPOLLWRNORM;
yangerkundaa5de52019-09-24 20:53:34 +08004384 if (READ_ONCE(ctx->rings->cq.head) != ctx->cached_cq_tail)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004385 mask |= EPOLLIN | EPOLLRDNORM;
4386
4387 return mask;
4388}
4389
4390static int io_uring_fasync(int fd, struct file *file, int on)
4391{
4392 struct io_ring_ctx *ctx = file->private_data;
4393
4394 return fasync_helper(fd, file, on, &ctx->cq_fasync);
4395}
4396
4397static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
4398{
4399 mutex_lock(&ctx->uring_lock);
4400 percpu_ref_kill(&ctx->refs);
4401 mutex_unlock(&ctx->uring_lock);
4402
Jens Axboe5262f562019-09-17 12:26:57 -06004403 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004404 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06004405
4406 if (ctx->io_wq)
4407 io_wq_cancel_all(ctx->io_wq);
4408
Jens Axboedef596e2019-01-09 08:59:42 -07004409 io_iopoll_reap_events(ctx);
Jens Axboe15dff282019-11-13 09:09:23 -07004410 /* if we failed setting up the ctx, we might not have any rings */
4411 if (ctx->rings)
4412 io_cqring_overflow_flush(ctx, true);
Jens Axboe206aefd2019-11-07 18:27:42 -07004413 wait_for_completion(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004414 io_ring_ctx_free(ctx);
4415}
4416
4417static int io_uring_release(struct inode *inode, struct file *file)
4418{
4419 struct io_ring_ctx *ctx = file->private_data;
4420
4421 file->private_data = NULL;
4422 io_ring_ctx_wait_and_kill(ctx);
4423 return 0;
4424}
4425
Jens Axboefcb323c2019-10-24 12:39:47 -06004426static void io_uring_cancel_files(struct io_ring_ctx *ctx,
4427 struct files_struct *files)
4428{
4429 struct io_kiocb *req;
4430 DEFINE_WAIT(wait);
4431
4432 while (!list_empty_careful(&ctx->inflight_list)) {
Jens Axboe768134d2019-11-10 20:30:53 -07004433 struct io_kiocb *cancel_req = NULL;
Jens Axboefcb323c2019-10-24 12:39:47 -06004434
4435 spin_lock_irq(&ctx->inflight_lock);
4436 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07004437 if (req->work.files != files)
4438 continue;
4439 /* req is being completed, ignore */
4440 if (!refcount_inc_not_zero(&req->refs))
4441 continue;
4442 cancel_req = req;
4443 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06004444 }
Jens Axboe768134d2019-11-10 20:30:53 -07004445 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06004446 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07004447 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06004448 spin_unlock_irq(&ctx->inflight_lock);
4449
Jens Axboe768134d2019-11-10 20:30:53 -07004450 /* We need to keep going until we don't find a matching req */
4451 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06004452 break;
Bob Liu2f6d9b92019-11-13 18:06:24 +08004453
4454 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
4455 io_put_req(cancel_req);
Jens Axboefcb323c2019-10-24 12:39:47 -06004456 schedule();
4457 }
Jens Axboe768134d2019-11-10 20:30:53 -07004458 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06004459}
4460
4461static int io_uring_flush(struct file *file, void *data)
4462{
4463 struct io_ring_ctx *ctx = file->private_data;
4464
4465 io_uring_cancel_files(ctx, data);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004466 if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) {
4467 io_cqring_overflow_flush(ctx, true);
Jens Axboefcb323c2019-10-24 12:39:47 -06004468 io_wq_cancel_all(ctx->io_wq);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004469 }
Jens Axboefcb323c2019-10-24 12:39:47 -06004470 return 0;
4471}
4472
Roman Penyaev6c5c2402019-11-28 12:53:22 +01004473static void *io_uring_validate_mmap_request(struct file *file,
4474 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004475{
Jens Axboe2b188cc2019-01-07 10:46:33 -07004476 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01004477 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004478 struct page *page;
4479 void *ptr;
4480
4481 switch (offset) {
4482 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00004483 case IORING_OFF_CQ_RING:
4484 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004485 break;
4486 case IORING_OFF_SQES:
4487 ptr = ctx->sq_sqes;
4488 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004489 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01004490 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004491 }
4492
4493 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07004494 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01004495 return ERR_PTR(-EINVAL);
4496
4497 return ptr;
4498}
4499
4500#ifdef CONFIG_MMU
4501
4502static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
4503{
4504 size_t sz = vma->vm_end - vma->vm_start;
4505 unsigned long pfn;
4506 void *ptr;
4507
4508 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
4509 if (IS_ERR(ptr))
4510 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004511
4512 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
4513 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
4514}
4515
Roman Penyaev6c5c2402019-11-28 12:53:22 +01004516#else /* !CONFIG_MMU */
4517
4518static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
4519{
4520 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
4521}
4522
4523static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
4524{
4525 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
4526}
4527
4528static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
4529 unsigned long addr, unsigned long len,
4530 unsigned long pgoff, unsigned long flags)
4531{
4532 void *ptr;
4533
4534 ptr = io_uring_validate_mmap_request(file, pgoff, len);
4535 if (IS_ERR(ptr))
4536 return PTR_ERR(ptr);
4537
4538 return (unsigned long) ptr;
4539}
4540
4541#endif /* !CONFIG_MMU */
4542
Jens Axboe2b188cc2019-01-07 10:46:33 -07004543SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
4544 u32, min_complete, u32, flags, const sigset_t __user *, sig,
4545 size_t, sigsz)
4546{
4547 struct io_ring_ctx *ctx;
4548 long ret = -EBADF;
4549 int submitted = 0;
4550 struct fd f;
4551
Jens Axboe6c271ce2019-01-10 11:22:30 -07004552 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07004553 return -EINVAL;
4554
4555 f = fdget(fd);
4556 if (!f.file)
4557 return -EBADF;
4558
4559 ret = -EOPNOTSUPP;
4560 if (f.file->f_op != &io_uring_fops)
4561 goto out_fput;
4562
4563 ret = -ENXIO;
4564 ctx = f.file->private_data;
4565 if (!percpu_ref_tryget(&ctx->refs))
4566 goto out_fput;
4567
Jens Axboe6c271ce2019-01-10 11:22:30 -07004568 /*
4569 * For SQ polling, the thread will do all submissions and completions.
4570 * Just return the requested submit count, and wake the thread if
4571 * we were asked to.
4572 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06004573 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004574 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07004575 if (!list_empty_careful(&ctx->cq_overflow_list))
4576 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004577 if (flags & IORING_ENTER_SQ_WAKEUP)
4578 wake_up(&ctx->sqo_wait);
4579 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06004580 } else if (to_submit) {
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004581 struct mm_struct *cur_mm;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004582
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004583 to_submit = min(to_submit, ctx->sq_entries);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004584 mutex_lock(&ctx->uring_lock);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004585 /* already have mm, so io_submit_sqes() won't try to grab it */
4586 cur_mm = ctx->sqo_mm;
4587 submitted = io_submit_sqes(ctx, to_submit, f.file, fd,
4588 &cur_mm, false);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004589 mutex_unlock(&ctx->uring_lock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004590 }
4591 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07004592 unsigned nr_events = 0;
4593
Jens Axboe2b188cc2019-01-07 10:46:33 -07004594 min_complete = min(min_complete, ctx->cq_entries);
4595
Jens Axboedef596e2019-01-09 08:59:42 -07004596 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07004597 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07004598 } else {
4599 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
4600 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004601 }
4602
Pavel Begunkov6805b322019-10-08 02:18:42 +03004603 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004604out_fput:
4605 fdput(f);
4606 return submitted ? submitted : ret;
4607}
4608
4609static const struct file_operations io_uring_fops = {
4610 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06004611 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07004612 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01004613#ifndef CONFIG_MMU
4614 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
4615 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
4616#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07004617 .poll = io_uring_poll,
4618 .fasync = io_uring_fasync,
4619};
4620
4621static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
4622 struct io_uring_params *p)
4623{
Hristo Venev75b28af2019-08-26 17:23:46 +00004624 struct io_rings *rings;
4625 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004626
Hristo Venev75b28af2019-08-26 17:23:46 +00004627 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
4628 if (size == SIZE_MAX)
4629 return -EOVERFLOW;
4630
4631 rings = io_mem_alloc(size);
4632 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004633 return -ENOMEM;
4634
Hristo Venev75b28af2019-08-26 17:23:46 +00004635 ctx->rings = rings;
4636 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
4637 rings->sq_ring_mask = p->sq_entries - 1;
4638 rings->cq_ring_mask = p->cq_entries - 1;
4639 rings->sq_ring_entries = p->sq_entries;
4640 rings->cq_ring_entries = p->cq_entries;
4641 ctx->sq_mask = rings->sq_ring_mask;
4642 ctx->cq_mask = rings->cq_ring_mask;
4643 ctx->sq_entries = rings->sq_ring_entries;
4644 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004645
4646 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07004647 if (size == SIZE_MAX) {
4648 io_mem_free(ctx->rings);
4649 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004650 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07004651 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004652
4653 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07004654 if (!ctx->sq_sqes) {
4655 io_mem_free(ctx->rings);
4656 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004657 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07004658 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004659
Jens Axboe2b188cc2019-01-07 10:46:33 -07004660 return 0;
4661}
4662
4663/*
4664 * Allocate an anonymous fd, this is what constitutes the application
4665 * visible backing of an io_uring instance. The application mmaps this
4666 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
4667 * we have to tie this fd to a socket for file garbage collection purposes.
4668 */
4669static int io_uring_get_fd(struct io_ring_ctx *ctx)
4670{
4671 struct file *file;
4672 int ret;
4673
4674#if defined(CONFIG_UNIX)
4675 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
4676 &ctx->ring_sock);
4677 if (ret)
4678 return ret;
4679#endif
4680
4681 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
4682 if (ret < 0)
4683 goto err;
4684
4685 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
4686 O_RDWR | O_CLOEXEC);
4687 if (IS_ERR(file)) {
4688 put_unused_fd(ret);
4689 ret = PTR_ERR(file);
4690 goto err;
4691 }
4692
4693#if defined(CONFIG_UNIX)
4694 ctx->ring_sock->file = file;
Jens Axboe6b063142019-01-10 22:13:58 -07004695 ctx->ring_sock->sk->sk_user_data = ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004696#endif
4697 fd_install(ret, file);
4698 return ret;
4699err:
4700#if defined(CONFIG_UNIX)
4701 sock_release(ctx->ring_sock);
4702 ctx->ring_sock = NULL;
4703#endif
4704 return ret;
4705}
4706
4707static int io_uring_create(unsigned entries, struct io_uring_params *p)
4708{
4709 struct user_struct *user = NULL;
4710 struct io_ring_ctx *ctx;
4711 bool account_mem;
4712 int ret;
4713
4714 if (!entries || entries > IORING_MAX_ENTRIES)
4715 return -EINVAL;
4716
4717 /*
4718 * Use twice as many entries for the CQ ring. It's possible for the
4719 * application to drive a higher depth than the size of the SQ ring,
4720 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06004721 * some flexibility in overcommitting a bit. If the application has
4722 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
4723 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07004724 */
4725 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06004726 if (p->flags & IORING_SETUP_CQSIZE) {
4727 /*
4728 * If IORING_SETUP_CQSIZE is set, we do the same roundup
4729 * to a power-of-two, if it isn't already. We do NOT impose
4730 * any cq vs sq ring sizing.
4731 */
4732 if (p->cq_entries < p->sq_entries || p->cq_entries > IORING_MAX_CQ_ENTRIES)
4733 return -EINVAL;
4734 p->cq_entries = roundup_pow_of_two(p->cq_entries);
4735 } else {
4736 p->cq_entries = 2 * p->sq_entries;
4737 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004738
4739 user = get_uid(current_user());
4740 account_mem = !capable(CAP_IPC_LOCK);
4741
4742 if (account_mem) {
4743 ret = io_account_mem(user,
4744 ring_pages(p->sq_entries, p->cq_entries));
4745 if (ret) {
4746 free_uid(user);
4747 return ret;
4748 }
4749 }
4750
4751 ctx = io_ring_ctx_alloc(p);
4752 if (!ctx) {
4753 if (account_mem)
4754 io_unaccount_mem(user, ring_pages(p->sq_entries,
4755 p->cq_entries));
4756 free_uid(user);
4757 return -ENOMEM;
4758 }
4759 ctx->compat = in_compat_syscall();
4760 ctx->account_mem = account_mem;
4761 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07004762 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07004763
4764 ret = io_allocate_scq_urings(ctx, p);
4765 if (ret)
4766 goto err;
4767
Jens Axboe6c271ce2019-01-10 11:22:30 -07004768 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004769 if (ret)
4770 goto err;
4771
Jens Axboe2b188cc2019-01-07 10:46:33 -07004772 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00004773 p->sq_off.head = offsetof(struct io_rings, sq.head);
4774 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
4775 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
4776 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
4777 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
4778 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
4779 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004780
4781 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00004782 p->cq_off.head = offsetof(struct io_rings, cq.head);
4783 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
4784 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
4785 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
4786 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
4787 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06004788
Jens Axboe044c1ab2019-10-28 09:15:33 -06004789 /*
4790 * Install ring fd as the very last thing, so we don't risk someone
4791 * having closed it before we finish setup
4792 */
4793 ret = io_uring_get_fd(ctx);
4794 if (ret < 0)
4795 goto err;
4796
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004797 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004798 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004799 return ret;
4800err:
4801 io_ring_ctx_wait_and_kill(ctx);
4802 return ret;
4803}
4804
4805/*
4806 * Sets up an aio uring context, and returns the fd. Applications asks for a
4807 * ring size, we return the actual sq/cq ring sizes (among other things) in the
4808 * params structure passed in.
4809 */
4810static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
4811{
4812 struct io_uring_params p;
4813 long ret;
4814 int i;
4815
4816 if (copy_from_user(&p, params, sizeof(p)))
4817 return -EFAULT;
4818 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
4819 if (p.resv[i])
4820 return -EINVAL;
4821 }
4822
Jens Axboe6c271ce2019-01-10 11:22:30 -07004823 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe33a107f2019-10-04 12:10:03 -06004824 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE))
Jens Axboe2b188cc2019-01-07 10:46:33 -07004825 return -EINVAL;
4826
4827 ret = io_uring_create(entries, &p);
4828 if (ret < 0)
4829 return ret;
4830
4831 if (copy_to_user(params, &p, sizeof(p)))
4832 return -EFAULT;
4833
4834 return ret;
4835}
4836
4837SYSCALL_DEFINE2(io_uring_setup, u32, entries,
4838 struct io_uring_params __user *, params)
4839{
4840 return io_uring_setup(entries, params);
4841}
4842
Jens Axboeedafcce2019-01-09 09:16:05 -07004843static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
4844 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06004845 __releases(ctx->uring_lock)
4846 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07004847{
4848 int ret;
4849
Jens Axboe35fa71a2019-04-22 10:23:23 -06004850 /*
4851 * We're inside the ring mutex, if the ref is already dying, then
4852 * someone else killed the ctx or is already going through
4853 * io_uring_register().
4854 */
4855 if (percpu_ref_is_dying(&ctx->refs))
4856 return -ENXIO;
4857
Jens Axboeedafcce2019-01-09 09:16:05 -07004858 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06004859
4860 /*
4861 * Drop uring mutex before waiting for references to exit. If another
4862 * thread is currently inside io_uring_enter() it might need to grab
4863 * the uring_lock to make progress. If we hold it here across the drain
4864 * wait, then we can deadlock. It's safe to drop the mutex here, since
4865 * no new references will come in after we've killed the percpu ref.
4866 */
4867 mutex_unlock(&ctx->uring_lock);
Jens Axboe206aefd2019-11-07 18:27:42 -07004868 wait_for_completion(&ctx->completions[0]);
Jens Axboeb19062a2019-04-15 10:49:38 -06004869 mutex_lock(&ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07004870
4871 switch (opcode) {
4872 case IORING_REGISTER_BUFFERS:
4873 ret = io_sqe_buffer_register(ctx, arg, nr_args);
4874 break;
4875 case IORING_UNREGISTER_BUFFERS:
4876 ret = -EINVAL;
4877 if (arg || nr_args)
4878 break;
4879 ret = io_sqe_buffer_unregister(ctx);
4880 break;
Jens Axboe6b063142019-01-10 22:13:58 -07004881 case IORING_REGISTER_FILES:
4882 ret = io_sqe_files_register(ctx, arg, nr_args);
4883 break;
4884 case IORING_UNREGISTER_FILES:
4885 ret = -EINVAL;
4886 if (arg || nr_args)
4887 break;
4888 ret = io_sqe_files_unregister(ctx);
4889 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06004890 case IORING_REGISTER_FILES_UPDATE:
4891 ret = io_sqe_files_update(ctx, arg, nr_args);
4892 break;
Jens Axboe9b402842019-04-11 11:45:41 -06004893 case IORING_REGISTER_EVENTFD:
4894 ret = -EINVAL;
4895 if (nr_args != 1)
4896 break;
4897 ret = io_eventfd_register(ctx, arg);
4898 break;
4899 case IORING_UNREGISTER_EVENTFD:
4900 ret = -EINVAL;
4901 if (arg || nr_args)
4902 break;
4903 ret = io_eventfd_unregister(ctx);
4904 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07004905 default:
4906 ret = -EINVAL;
4907 break;
4908 }
4909
4910 /* bring the ctx back to life */
Jens Axboe206aefd2019-11-07 18:27:42 -07004911 reinit_completion(&ctx->completions[0]);
Jens Axboeedafcce2019-01-09 09:16:05 -07004912 percpu_ref_reinit(&ctx->refs);
4913 return ret;
4914}
4915
4916SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
4917 void __user *, arg, unsigned int, nr_args)
4918{
4919 struct io_ring_ctx *ctx;
4920 long ret = -EBADF;
4921 struct fd f;
4922
4923 f = fdget(fd);
4924 if (!f.file)
4925 return -EBADF;
4926
4927 ret = -EOPNOTSUPP;
4928 if (f.file->f_op != &io_uring_fops)
4929 goto out_fput;
4930
4931 ctx = f.file->private_data;
4932
4933 mutex_lock(&ctx->uring_lock);
4934 ret = __io_uring_register(ctx, opcode, arg, nr_args);
4935 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004936 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
4937 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07004938out_fput:
4939 fdput(f);
4940 return ret;
4941}
4942
Jens Axboe2b188cc2019-01-07 10:46:33 -07004943static int __init io_uring_init(void)
4944{
4945 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
4946 return 0;
4947};
4948__initcall(io_uring_init);