blob: c846605b8361ef983c38e46939b3edc1fcc8d9d5 [file] [log] [blame]
Jens Axboe2b188cc2019-01-07 10:46:33 -07001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Shared application/kernel submission and completion ring pairs, for
4 * supporting fast/efficient IO.
5 *
6 * A note on the read/write ordering memory barriers that are matched between
Stefan Bühler1e84b972019-04-24 23:54:16 +02007 * the application and kernel side.
8 *
9 * After the application reads the CQ ring tail, it must use an
10 * appropriate smp_rmb() to pair with the smp_wmb() the kernel uses
11 * before writing the tail (using smp_load_acquire to read the tail will
12 * do). It also needs a smp_mb() before updating CQ head (ordering the
13 * entry load(s) with the head store), pairing with an implicit barrier
14 * through a control-dependency in io_get_cqring (smp_store_release to
15 * store head will do). Failure to do so could lead to reading invalid
16 * CQ entries.
17 *
18 * Likewise, the application must use an appropriate smp_wmb() before
19 * writing the SQ tail (ordering SQ entry stores with the tail store),
20 * which pairs with smp_load_acquire in io_get_sqring (smp_store_release
21 * to store the tail will do). And it needs a barrier ordering the SQ
22 * head load before writing new SQ entries (smp_load_acquire to read
23 * head will do).
24 *
25 * When using the SQ poll thread (IORING_SETUP_SQPOLL), the application
26 * needs to check the SQ flags for IORING_SQ_NEED_WAKEUP *after*
27 * updating the SQ tail; a full memory barrier smp_mb() is needed
28 * between.
Jens Axboe2b188cc2019-01-07 10:46:33 -070029 *
30 * Also see the examples in the liburing library:
31 *
32 * git://git.kernel.dk/liburing
33 *
34 * io_uring also uses READ/WRITE_ONCE() for _any_ store or load that happens
35 * from data shared between the kernel and application. This is done both
36 * for ordering purposes, but also to ensure that once a value is loaded from
37 * data that the application could potentially modify, it remains stable.
38 *
39 * Copyright (C) 2018-2019 Jens Axboe
Christoph Hellwigc992fe22019-01-11 09:43:02 -070040 * Copyright (c) 2018-2019 Christoph Hellwig
Jens Axboe2b188cc2019-01-07 10:46:33 -070041 */
42#include <linux/kernel.h>
43#include <linux/init.h>
44#include <linux/errno.h>
45#include <linux/syscalls.h>
46#include <linux/compat.h>
47#include <linux/refcount.h>
48#include <linux/uio.h>
49
50#include <linux/sched/signal.h>
51#include <linux/fs.h>
52#include <linux/file.h>
53#include <linux/fdtable.h>
54#include <linux/mm.h>
55#include <linux/mman.h>
56#include <linux/mmu_context.h>
57#include <linux/percpu.h>
58#include <linux/slab.h>
Jens Axboe6c271ce2019-01-10 11:22:30 -070059#include <linux/kthread.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070060#include <linux/blkdev.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070061#include <linux/bvec.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070062#include <linux/net.h>
63#include <net/sock.h>
64#include <net/af_unix.h>
Jens Axboe6b063142019-01-10 22:13:58 -070065#include <net/scm.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070066#include <linux/anon_inodes.h>
67#include <linux/sched/mm.h>
68#include <linux/uaccess.h>
69#include <linux/nospec.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070070#include <linux/sizes.h>
71#include <linux/hugetlb.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070072
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020073#define CREATE_TRACE_POINTS
74#include <trace/events/io_uring.h>
75
Jens Axboe2b188cc2019-01-07 10:46:33 -070076#include <uapi/linux/io_uring.h>
77
78#include "internal.h"
Jens Axboe561fb042019-10-24 07:25:42 -060079#include "io-wq.h"
Jens Axboe2b188cc2019-01-07 10:46:33 -070080
Daniel Xu5277dea2019-09-14 14:23:45 -070081#define IORING_MAX_ENTRIES 32768
Jens Axboe33a107f2019-10-04 12:10:03 -060082#define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES)
Jens Axboe65e19f52019-10-26 07:20:21 -060083
84/*
85 * Shift of 9 is 512 entries, or exactly one page on 64-bit archs
86 */
87#define IORING_FILE_TABLE_SHIFT 9
88#define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT)
89#define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1)
90#define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE)
Jens Axboe2b188cc2019-01-07 10:46:33 -070091
92struct io_uring {
93 u32 head ____cacheline_aligned_in_smp;
94 u32 tail ____cacheline_aligned_in_smp;
95};
96
Stefan Bühler1e84b972019-04-24 23:54:16 +020097/*
Hristo Venev75b28af2019-08-26 17:23:46 +000098 * This data is shared with the application through the mmap at offsets
99 * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200100 *
101 * The offsets to the member fields are published through struct
102 * io_sqring_offsets when calling io_uring_setup.
103 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000104struct io_rings {
Stefan Bühler1e84b972019-04-24 23:54:16 +0200105 /*
106 * Head and tail offsets into the ring; the offsets need to be
107 * masked to get valid indices.
108 *
Hristo Venev75b28af2019-08-26 17:23:46 +0000109 * The kernel controls head of the sq ring and the tail of the cq ring,
110 * and the application controls tail of the sq ring and the head of the
111 * cq ring.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200112 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000113 struct io_uring sq, cq;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200114 /*
Hristo Venev75b28af2019-08-26 17:23:46 +0000115 * Bitmasks to apply to head and tail offsets (constant, equals
Stefan Bühler1e84b972019-04-24 23:54:16 +0200116 * ring_entries - 1)
117 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000118 u32 sq_ring_mask, cq_ring_mask;
119 /* Ring sizes (constant, power of 2) */
120 u32 sq_ring_entries, cq_ring_entries;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200121 /*
122 * Number of invalid entries dropped by the kernel due to
123 * invalid index stored in array
124 *
125 * Written by the kernel, shouldn't be modified by the
126 * application (i.e. get number of "new events" by comparing to
127 * cached value).
128 *
129 * After a new SQ head value was read by the application this
130 * counter includes all submissions that were dropped reaching
131 * the new SQ head (and possibly more).
132 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000133 u32 sq_dropped;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200134 /*
135 * Runtime flags
136 *
137 * Written by the kernel, shouldn't be modified by the
138 * application.
139 *
140 * The application needs a full memory barrier before checking
141 * for IORING_SQ_NEED_WAKEUP after updating the sq tail.
142 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000143 u32 sq_flags;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200144 /*
145 * Number of completion events lost because the queue was full;
146 * this should be avoided by the application by making sure
147 * there are not more requests pending thatn there is space in
148 * the completion queue.
149 *
150 * Written by the kernel, shouldn't be modified by the
151 * application (i.e. get number of "new events" by comparing to
152 * cached value).
153 *
154 * As completion events come in out of order this counter is not
155 * ordered with any other data.
156 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000157 u32 cq_overflow;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200158 /*
159 * Ring buffer of completion events.
160 *
161 * The kernel writes completion events fresh every time they are
162 * produced, so the application is allowed to modify pending
163 * entries.
164 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000165 struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700166};
167
Jens Axboeedafcce2019-01-09 09:16:05 -0700168struct io_mapped_ubuf {
169 u64 ubuf;
170 size_t len;
171 struct bio_vec *bvec;
172 unsigned int nr_bvecs;
173};
174
Jens Axboe65e19f52019-10-26 07:20:21 -0600175struct fixed_file_table {
176 struct file **files;
Jens Axboe31b51512019-01-18 22:56:34 -0700177};
178
Jens Axboe2b188cc2019-01-07 10:46:33 -0700179struct io_ring_ctx {
180 struct {
181 struct percpu_ref refs;
182 } ____cacheline_aligned_in_smp;
183
184 struct {
185 unsigned int flags;
186 bool compat;
187 bool account_mem;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700188 bool cq_overflow_flushed;
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +0300189 bool drain_next;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700190
Hristo Venev75b28af2019-08-26 17:23:46 +0000191 /*
192 * Ring buffer of indices into array of io_uring_sqe, which is
193 * mmapped by the application using the IORING_OFF_SQES offset.
194 *
195 * This indirection could e.g. be used to assign fixed
196 * io_uring_sqe entries to operations and only submit them to
197 * the queue when needed.
198 *
199 * The kernel modifies neither the indices array nor the entries
200 * array.
201 */
202 u32 *sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700203 unsigned cached_sq_head;
204 unsigned sq_entries;
205 unsigned sq_mask;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700206 unsigned sq_thread_idle;
Jens Axboe498ccd92019-10-25 10:04:25 -0600207 unsigned cached_sq_dropped;
Jens Axboe206aefd2019-11-07 18:27:42 -0700208 atomic_t cached_cq_overflow;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700209 struct io_uring_sqe *sq_sqes;
Jens Axboede0617e2019-04-06 21:51:27 -0600210
211 struct list_head defer_list;
Jens Axboe5262f562019-09-17 12:26:57 -0600212 struct list_head timeout_list;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700213 struct list_head cq_overflow_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700214
Jens Axboefcb323c2019-10-24 12:39:47 -0600215 wait_queue_head_t inflight_wait;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700216 } ____cacheline_aligned_in_smp;
217
Hristo Venev75b28af2019-08-26 17:23:46 +0000218 struct io_rings *rings;
219
Jens Axboe2b188cc2019-01-07 10:46:33 -0700220 /* IO offload */
Jens Axboe561fb042019-10-24 07:25:42 -0600221 struct io_wq *io_wq;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700222 struct task_struct *sqo_thread; /* if using sq thread polling */
223 struct mm_struct *sqo_mm;
224 wait_queue_head_t sqo_wait;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700225
Jens Axboe6b063142019-01-10 22:13:58 -0700226 /*
227 * If used, fixed file set. Writers must ensure that ->refs is dead,
228 * readers must ensure that ->refs is alive as long as the file* is
229 * used. Only updated through io_uring_register(2).
230 */
Jens Axboe65e19f52019-10-26 07:20:21 -0600231 struct fixed_file_table *file_table;
Jens Axboe6b063142019-01-10 22:13:58 -0700232 unsigned nr_user_files;
233
Jens Axboeedafcce2019-01-09 09:16:05 -0700234 /* if used, fixed mapped user buffers */
235 unsigned nr_user_bufs;
236 struct io_mapped_ubuf *user_bufs;
237
Jens Axboe2b188cc2019-01-07 10:46:33 -0700238 struct user_struct *user;
239
Jens Axboe181e4482019-11-25 08:52:30 -0700240 struct cred *creds;
241
Jens Axboe206aefd2019-11-07 18:27:42 -0700242 /* 0 is for ctx quiesce/reinit/free, 1 is for sqo_thread started */
243 struct completion *completions;
244
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700245 /* if all else fails... */
246 struct io_kiocb *fallback_req;
247
Jens Axboe206aefd2019-11-07 18:27:42 -0700248#if defined(CONFIG_UNIX)
249 struct socket *ring_sock;
250#endif
251
252 struct {
253 unsigned cached_cq_tail;
254 unsigned cq_entries;
255 unsigned cq_mask;
256 atomic_t cq_timeouts;
257 struct wait_queue_head cq_wait;
258 struct fasync_struct *cq_fasync;
259 struct eventfd_ctx *cq_ev_fd;
260 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700261
262 struct {
263 struct mutex uring_lock;
264 wait_queue_head_t wait;
265 } ____cacheline_aligned_in_smp;
266
267 struct {
268 spinlock_t completion_lock;
Jens Axboedef596e2019-01-09 08:59:42 -0700269 bool poll_multi_file;
270 /*
271 * ->poll_list is protected by the ctx->uring_lock for
272 * io_uring instances that don't use IORING_SETUP_SQPOLL.
273 * For SQPOLL, only the single threaded io_sq_thread() will
274 * manipulate the list, hence no extra locking is needed there.
275 */
276 struct list_head poll_list;
Jens Axboeeac406c2019-11-14 12:09:58 -0700277 struct rb_root cancel_tree;
Jens Axboefcb323c2019-10-24 12:39:47 -0600278
279 spinlock_t inflight_lock;
280 struct list_head inflight_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700281 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700282};
283
Jens Axboe09bb8392019-03-13 12:39:28 -0600284/*
285 * First field must be the file pointer in all the
286 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
287 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700288struct io_poll_iocb {
289 struct file *file;
290 struct wait_queue_head *head;
291 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600292 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700293 bool canceled;
294 struct wait_queue_entry wait;
295};
296
Jens Axboead8a48a2019-11-15 08:49:11 -0700297struct io_timeout_data {
298 struct io_kiocb *req;
299 struct hrtimer timer;
300 struct timespec64 ts;
301 enum hrtimer_mode mode;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +0300302 u32 seq_offset;
Jens Axboead8a48a2019-11-15 08:49:11 -0700303};
304
Jens Axboe5262f562019-09-17 12:26:57 -0600305struct io_timeout {
306 struct file *file;
Jens Axboead8a48a2019-11-15 08:49:11 -0700307 struct io_timeout_data *data;
Jens Axboe5262f562019-09-17 12:26:57 -0600308};
309
Jens Axboe09bb8392019-03-13 12:39:28 -0600310/*
311 * NOTE! Each of the iocb union members has the file pointer
312 * as the first entry in their struct definition. So you can
313 * access the file pointer through any of the sub-structs,
314 * or directly as just 'ki_filp' in this struct.
315 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700316struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700317 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600318 struct file *file;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700319 struct kiocb rw;
320 struct io_poll_iocb poll;
Jens Axboe5262f562019-09-17 12:26:57 -0600321 struct io_timeout timeout;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700322 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700323
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300324 const struct io_uring_sqe *sqe;
325 struct file *ring_file;
326 int ring_fd;
327 bool has_user;
328 bool in_async;
329 bool needs_fixed_file;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700330
331 struct io_ring_ctx *ctx;
Jens Axboeeac406c2019-11-14 12:09:58 -0700332 union {
333 struct list_head list;
334 struct rb_node rb_node;
335 };
Jens Axboe9e645e112019-05-10 16:07:28 -0600336 struct list_head link_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700337 unsigned int flags;
Jens Axboec16361c2019-01-17 08:39:48 -0700338 refcount_t refs;
Stefan Bühler8449eed2019-04-27 20:34:19 +0200339#define REQ_F_NOWAIT 1 /* must not punt to workers */
Jens Axboedef596e2019-01-09 08:59:42 -0700340#define REQ_F_IOPOLL_COMPLETED 2 /* polled IO has completed */
Jens Axboe6b063142019-01-10 22:13:58 -0700341#define REQ_F_FIXED_FILE 4 /* ctx owns file */
Jens Axboe4d7dd462019-11-20 13:03:52 -0700342#define REQ_F_LINK_NEXT 8 /* already grabbed next link */
Stefan Bühlere2033e32019-05-11 19:08:01 +0200343#define REQ_F_IO_DRAIN 16 /* drain existing IO first */
344#define REQ_F_IO_DRAINED 32 /* drain done */
Jens Axboe9e645e112019-05-10 16:07:28 -0600345#define REQ_F_LINK 64 /* linked sqes */
Jens Axboe2665abf2019-11-05 12:40:47 -0700346#define REQ_F_LINK_TIMEOUT 128 /* has linked timeout */
Zhengyuan Liuf7b76ac2019-07-16 23:26:14 +0800347#define REQ_F_FAIL_LINK 256 /* fail rest of links */
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +0300348#define REQ_F_DRAIN_LINK 512 /* link should be fully drained */
Jens Axboe5262f562019-09-17 12:26:57 -0600349#define REQ_F_TIMEOUT 1024 /* timeout request */
Jens Axboe491381ce2019-10-17 09:20:46 -0600350#define REQ_F_ISREG 2048 /* regular file */
351#define REQ_F_MUST_PUNT 4096 /* must be punted even for NONBLOCK */
Jens Axboe93bd25b2019-11-11 23:34:31 -0700352#define REQ_F_TIMEOUT_NOSEQ 8192 /* no timeout sequence */
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800353#define REQ_F_INFLIGHT 16384 /* on inflight list */
354#define REQ_F_COMP_LOCKED 32768 /* completion under lock */
Jens Axboe94ae5e72019-11-14 19:39:52 -0700355#define REQ_F_FREE_SQE 65536 /* free sqe if not async queued */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700356 u64 user_data;
Jens Axboe9e645e112019-05-10 16:07:28 -0600357 u32 result;
Jens Axboede0617e2019-04-06 21:51:27 -0600358 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700359
Jens Axboefcb323c2019-10-24 12:39:47 -0600360 struct list_head inflight_entry;
361
Jens Axboe561fb042019-10-24 07:25:42 -0600362 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700363};
364
365#define IO_PLUG_THRESHOLD 2
Jens Axboedef596e2019-01-09 08:59:42 -0700366#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700367
Jens Axboe9a56a232019-01-09 09:06:50 -0700368struct io_submit_state {
369 struct blk_plug plug;
370
371 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700372 * io_kiocb alloc cache
373 */
374 void *reqs[IO_IOPOLL_BATCH];
375 unsigned int free_reqs;
376 unsigned int cur_req;
377
378 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700379 * File reference cache
380 */
381 struct file *file;
382 unsigned int fd;
383 unsigned int has_refs;
384 unsigned int used_refs;
385 unsigned int ios_left;
386};
387
Jens Axboe561fb042019-10-24 07:25:42 -0600388static void io_wq_submit_work(struct io_wq_work **workptr);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700389static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liu4fe2c962019-09-09 20:50:40 +0800390static void __io_free_req(struct io_kiocb *req);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800391static void io_put_req(struct io_kiocb *req);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700392static void io_double_put_req(struct io_kiocb *req);
Jens Axboe978db572019-11-14 22:39:04 -0700393static void __io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700394static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
395static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboede0617e2019-04-06 21:51:27 -0600396
Jens Axboe2b188cc2019-01-07 10:46:33 -0700397static struct kmem_cache *req_cachep;
398
399static const struct file_operations io_uring_fops;
400
401struct sock *io_uring_get_socket(struct file *file)
402{
403#if defined(CONFIG_UNIX)
404 if (file->f_op == &io_uring_fops) {
405 struct io_ring_ctx *ctx = file->private_data;
406
407 return ctx->ring_sock->sk;
408 }
409#endif
410 return NULL;
411}
412EXPORT_SYMBOL(io_uring_get_socket);
413
414static void io_ring_ctx_ref_free(struct percpu_ref *ref)
415{
416 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
417
Jens Axboe206aefd2019-11-07 18:27:42 -0700418 complete(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700419}
420
421static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
422{
423 struct io_ring_ctx *ctx;
424
425 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
426 if (!ctx)
427 return NULL;
428
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700429 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
430 if (!ctx->fallback_req)
431 goto err;
432
Jens Axboe206aefd2019-11-07 18:27:42 -0700433 ctx->completions = kmalloc(2 * sizeof(struct completion), GFP_KERNEL);
434 if (!ctx->completions)
435 goto err;
436
Roman Gushchin21482892019-05-07 10:01:48 -0700437 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -0700438 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
439 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700440
441 ctx->flags = p->flags;
442 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700443 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe206aefd2019-11-07 18:27:42 -0700444 init_completion(&ctx->completions[0]);
445 init_completion(&ctx->completions[1]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700446 mutex_init(&ctx->uring_lock);
447 init_waitqueue_head(&ctx->wait);
448 spin_lock_init(&ctx->completion_lock);
Jens Axboedef596e2019-01-09 08:59:42 -0700449 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboeeac406c2019-11-14 12:09:58 -0700450 ctx->cancel_tree = RB_ROOT;
Jens Axboede0617e2019-04-06 21:51:27 -0600451 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -0600452 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -0600453 init_waitqueue_head(&ctx->inflight_wait);
454 spin_lock_init(&ctx->inflight_lock);
455 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700456 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -0700457err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700458 if (ctx->fallback_req)
459 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe206aefd2019-11-07 18:27:42 -0700460 kfree(ctx->completions);
461 kfree(ctx);
462 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700463}
464
Bob Liu9d858b22019-11-13 18:06:25 +0800465static inline bool __req_need_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -0600466{
Jackie Liua197f662019-11-08 08:09:12 -0700467 struct io_ring_ctx *ctx = req->ctx;
468
Jens Axboe498ccd92019-10-25 10:04:25 -0600469 return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped
470 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboede0617e2019-04-06 21:51:27 -0600471}
472
Bob Liu9d858b22019-11-13 18:06:25 +0800473static inline bool req_need_defer(struct io_kiocb *req)
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600474{
Bob Liu9d858b22019-11-13 18:06:25 +0800475 if ((req->flags & (REQ_F_IO_DRAIN|REQ_F_IO_DRAINED)) == REQ_F_IO_DRAIN)
476 return __req_need_defer(req);
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600477
Bob Liu9d858b22019-11-13 18:06:25 +0800478 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600479}
480
481static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -0600482{
483 struct io_kiocb *req;
484
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600485 req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list);
Bob Liu9d858b22019-11-13 18:06:25 +0800486 if (req && !req_need_defer(req)) {
Jens Axboede0617e2019-04-06 21:51:27 -0600487 list_del_init(&req->list);
488 return req;
489 }
490
491 return NULL;
492}
493
Jens Axboe5262f562019-09-17 12:26:57 -0600494static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx)
495{
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600496 struct io_kiocb *req;
497
498 req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list);
Jens Axboe93bd25b2019-11-11 23:34:31 -0700499 if (req) {
500 if (req->flags & REQ_F_TIMEOUT_NOSEQ)
501 return NULL;
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800502 if (!__req_need_defer(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -0700503 list_del_init(&req->list);
504 return req;
505 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600506 }
507
508 return NULL;
Jens Axboe5262f562019-09-17 12:26:57 -0600509}
510
Jens Axboede0617e2019-04-06 21:51:27 -0600511static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700512{
Hristo Venev75b28af2019-08-26 17:23:46 +0000513 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700514
Hristo Venev75b28af2019-08-26 17:23:46 +0000515 if (ctx->cached_cq_tail != READ_ONCE(rings->cq.tail)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700516 /* order cqe stores with ring update */
Hristo Venev75b28af2019-08-26 17:23:46 +0000517 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700518
Jens Axboe2b188cc2019-01-07 10:46:33 -0700519 if (wq_has_sleeper(&ctx->cq_wait)) {
520 wake_up_interruptible(&ctx->cq_wait);
521 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
522 }
523 }
524}
525
Jens Axboe561fb042019-10-24 07:25:42 -0600526static inline bool io_sqe_needs_user(const struct io_uring_sqe *sqe)
Jens Axboe18d9be12019-09-10 09:13:05 -0600527{
Jens Axboe561fb042019-10-24 07:25:42 -0600528 u8 opcode = READ_ONCE(sqe->opcode);
529
530 return !(opcode == IORING_OP_READ_FIXED ||
531 opcode == IORING_OP_WRITE_FIXED);
532}
533
Jens Axboe94ae5e72019-11-14 19:39:52 -0700534static inline bool io_prep_async_work(struct io_kiocb *req,
535 struct io_kiocb **link)
Jens Axboe561fb042019-10-24 07:25:42 -0600536{
537 bool do_hashed = false;
Jens Axboe54a91f32019-09-10 09:15:04 -0600538
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300539 if (req->sqe) {
540 switch (req->sqe->opcode) {
Jens Axboe6cc47d12019-09-18 11:18:23 -0600541 case IORING_OP_WRITEV:
542 case IORING_OP_WRITE_FIXED:
Jens Axboe561fb042019-10-24 07:25:42 -0600543 do_hashed = true;
Jens Axboe5f8fd2d2019-11-07 10:57:36 -0700544 /* fall-through */
545 case IORING_OP_READV:
546 case IORING_OP_READ_FIXED:
547 case IORING_OP_SENDMSG:
548 case IORING_OP_RECVMSG:
549 case IORING_OP_ACCEPT:
550 case IORING_OP_POLL_ADD:
Jens Axboef8e85cf2019-11-23 14:24:24 -0700551 case IORING_OP_CONNECT:
Jens Axboe5f8fd2d2019-11-07 10:57:36 -0700552 /*
553 * We know REQ_F_ISREG is not set on some of these
554 * opcodes, but this enables us to keep the check in
555 * just one place.
556 */
557 if (!(req->flags & REQ_F_ISREG))
558 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe6cc47d12019-09-18 11:18:23 -0600559 break;
560 }
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300561 if (io_sqe_needs_user(req->sqe))
Jens Axboe561fb042019-10-24 07:25:42 -0600562 req->work.flags |= IO_WQ_WORK_NEEDS_USER;
Jens Axboe54a91f32019-09-10 09:15:04 -0600563 }
564
Jens Axboe94ae5e72019-11-14 19:39:52 -0700565 *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -0600566 return do_hashed;
567}
568
Jackie Liua197f662019-11-08 08:09:12 -0700569static inline void io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -0600570{
Jackie Liua197f662019-11-08 08:09:12 -0700571 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe94ae5e72019-11-14 19:39:52 -0700572 struct io_kiocb *link;
573 bool do_hashed;
574
575 do_hashed = io_prep_async_work(req, &link);
Jens Axboe561fb042019-10-24 07:25:42 -0600576
577 trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work,
578 req->flags);
579 if (!do_hashed) {
580 io_wq_enqueue(ctx->io_wq, &req->work);
581 } else {
582 io_wq_enqueue_hashed(ctx->io_wq, &req->work,
583 file_inode(req->file));
584 }
Jens Axboe94ae5e72019-11-14 19:39:52 -0700585
586 if (link)
587 io_queue_linked_timeout(link);
Jens Axboe18d9be12019-09-10 09:13:05 -0600588}
589
Jens Axboe5262f562019-09-17 12:26:57 -0600590static void io_kill_timeout(struct io_kiocb *req)
591{
592 int ret;
593
Jens Axboead8a48a2019-11-15 08:49:11 -0700594 ret = hrtimer_try_to_cancel(&req->timeout.data->timer);
Jens Axboe5262f562019-09-17 12:26:57 -0600595 if (ret != -1) {
596 atomic_inc(&req->ctx->cq_timeouts);
Jens Axboe842f9612019-10-29 12:34:10 -0600597 list_del_init(&req->list);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700598 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800599 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -0600600 }
601}
602
603static void io_kill_timeouts(struct io_ring_ctx *ctx)
604{
605 struct io_kiocb *req, *tmp;
606
607 spin_lock_irq(&ctx->completion_lock);
608 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
609 io_kill_timeout(req);
610 spin_unlock_irq(&ctx->completion_lock);
611}
612
Jens Axboede0617e2019-04-06 21:51:27 -0600613static void io_commit_cqring(struct io_ring_ctx *ctx)
614{
615 struct io_kiocb *req;
616
Jens Axboe5262f562019-09-17 12:26:57 -0600617 while ((req = io_get_timeout_req(ctx)) != NULL)
618 io_kill_timeout(req);
619
Jens Axboede0617e2019-04-06 21:51:27 -0600620 __io_commit_cqring(ctx);
621
622 while ((req = io_get_deferred_req(ctx)) != NULL) {
623 req->flags |= REQ_F_IO_DRAINED;
Jackie Liua197f662019-11-08 08:09:12 -0700624 io_queue_async_work(req);
Jens Axboede0617e2019-04-06 21:51:27 -0600625 }
626}
627
Jens Axboe2b188cc2019-01-07 10:46:33 -0700628static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
629{
Hristo Venev75b28af2019-08-26 17:23:46 +0000630 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700631 unsigned tail;
632
633 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +0200634 /*
635 * writes to the cq entry need to come after reading head; the
636 * control dependency is enough as we're using WRITE_ONCE to
637 * fill the cq entry
638 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000639 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700640 return NULL;
641
642 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +0000643 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -0700644}
645
Jens Axboe8c838782019-03-12 15:48:16 -0600646static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
647{
648 if (waitqueue_active(&ctx->wait))
649 wake_up(&ctx->wait);
650 if (waitqueue_active(&ctx->sqo_wait))
651 wake_up(&ctx->sqo_wait);
Jens Axboe9b402842019-04-11 11:45:41 -0600652 if (ctx->cq_ev_fd)
653 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -0600654}
655
Jens Axboec4a2ed72019-11-21 21:01:26 -0700656/* Returns true if there are no backlogged entries after the flush */
657static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700658{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700659 struct io_rings *rings = ctx->rings;
660 struct io_uring_cqe *cqe;
661 struct io_kiocb *req;
662 unsigned long flags;
663 LIST_HEAD(list);
664
665 if (!force) {
666 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -0700667 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700668 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
669 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -0700670 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700671 }
672
673 spin_lock_irqsave(&ctx->completion_lock, flags);
674
675 /* if force is set, the ring is going away. always drop after that */
676 if (force)
677 ctx->cq_overflow_flushed = true;
678
Jens Axboec4a2ed72019-11-21 21:01:26 -0700679 cqe = NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700680 while (!list_empty(&ctx->cq_overflow_list)) {
681 cqe = io_get_cqring(ctx);
682 if (!cqe && !force)
683 break;
684
685 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
686 list);
687 list_move(&req->list, &list);
688 if (cqe) {
689 WRITE_ONCE(cqe->user_data, req->user_data);
690 WRITE_ONCE(cqe->res, req->result);
691 WRITE_ONCE(cqe->flags, 0);
692 } else {
693 WRITE_ONCE(ctx->rings->cq_overflow,
694 atomic_inc_return(&ctx->cached_cq_overflow));
695 }
696 }
697
698 io_commit_cqring(ctx);
699 spin_unlock_irqrestore(&ctx->completion_lock, flags);
700 io_cqring_ev_posted(ctx);
701
702 while (!list_empty(&list)) {
703 req = list_first_entry(&list, struct io_kiocb, list);
704 list_del(&req->list);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800705 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700706 }
Jens Axboec4a2ed72019-11-21 21:01:26 -0700707
708 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700709}
710
Jens Axboe78e19bb2019-11-06 15:21:34 -0700711static void io_cqring_fill_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700712{
Jens Axboe78e19bb2019-11-06 15:21:34 -0700713 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700714 struct io_uring_cqe *cqe;
715
Jens Axboe78e19bb2019-11-06 15:21:34 -0700716 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -0700717
Jens Axboe2b188cc2019-01-07 10:46:33 -0700718 /*
719 * If we can't get a cq entry, userspace overflowed the
720 * submission (by quite a lot). Increment the overflow count in
721 * the ring.
722 */
723 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700724 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -0700725 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700726 WRITE_ONCE(cqe->res, res);
727 WRITE_ONCE(cqe->flags, 0);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700728 } else if (ctx->cq_overflow_flushed) {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700729 WRITE_ONCE(ctx->rings->cq_overflow,
730 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700731 } else {
732 refcount_inc(&req->refs);
733 req->result = res;
734 list_add_tail(&req->list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700735 }
736}
737
Jens Axboe78e19bb2019-11-06 15:21:34 -0700738static void io_cqring_add_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700739{
Jens Axboe78e19bb2019-11-06 15:21:34 -0700740 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700741 unsigned long flags;
742
743 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700744 io_cqring_fill_event(req, res);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700745 io_commit_cqring(ctx);
746 spin_unlock_irqrestore(&ctx->completion_lock, flags);
747
Jens Axboe8c838782019-03-12 15:48:16 -0600748 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700749}
750
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700751static inline bool io_is_fallback_req(struct io_kiocb *req)
752{
753 return req == (struct io_kiocb *)
754 ((unsigned long) req->ctx->fallback_req & ~1UL);
755}
756
757static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
758{
759 struct io_kiocb *req;
760
761 req = ctx->fallback_req;
762 if (!test_and_set_bit_lock(0, (unsigned long *) ctx->fallback_req))
763 return req;
764
765 return NULL;
766}
767
Jens Axboe2579f912019-01-09 09:10:43 -0700768static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
769 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700770{
Jens Axboefd6fab22019-03-14 16:30:06 -0600771 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700772 struct io_kiocb *req;
773
774 if (!percpu_ref_tryget(&ctx->refs))
775 return NULL;
776
Jens Axboe2579f912019-01-09 09:10:43 -0700777 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -0600778 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -0700779 if (unlikely(!req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700780 goto fallback;
Jens Axboe2579f912019-01-09 09:10:43 -0700781 } else if (!state->free_reqs) {
782 size_t sz;
783 int ret;
784
785 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -0600786 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
787
788 /*
789 * Bulk alloc is all-or-nothing. If we fail to get a batch,
790 * retry single alloc to be on the safe side.
791 */
792 if (unlikely(ret <= 0)) {
793 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
794 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700795 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -0600796 ret = 1;
797 }
Jens Axboe2579f912019-01-09 09:10:43 -0700798 state->free_reqs = ret - 1;
799 state->cur_req = 1;
800 req = state->reqs[0];
801 } else {
802 req = state->reqs[state->cur_req];
803 state->free_reqs--;
804 state->cur_req++;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700805 }
806
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700807got_it:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300808 req->ring_file = NULL;
Jens Axboe60c112b2019-06-21 10:20:18 -0600809 req->file = NULL;
Jens Axboe2579f912019-01-09 09:10:43 -0700810 req->ctx = ctx;
811 req->flags = 0;
Jens Axboee65ef562019-03-12 10:16:44 -0600812 /* one is dropped after submission, the other at completion */
813 refcount_set(&req->refs, 2);
Jens Axboe9e645e112019-05-10 16:07:28 -0600814 req->result = 0;
Jens Axboe561fb042019-10-24 07:25:42 -0600815 INIT_IO_WORK(&req->work, io_wq_submit_work);
Jens Axboe2579f912019-01-09 09:10:43 -0700816 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700817fallback:
818 req = io_get_fallback_req(ctx);
819 if (req)
820 goto got_it;
Pavel Begunkov6805b322019-10-08 02:18:42 +0300821 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700822 return NULL;
823}
824
Jens Axboedef596e2019-01-09 08:59:42 -0700825static void io_free_req_many(struct io_ring_ctx *ctx, void **reqs, int *nr)
826{
827 if (*nr) {
828 kmem_cache_free_bulk(req_cachep, *nr, reqs);
Pavel Begunkov6805b322019-10-08 02:18:42 +0300829 percpu_ref_put_many(&ctx->refs, *nr);
Jens Axboedef596e2019-01-09 08:59:42 -0700830 *nr = 0;
831 }
832}
833
Jens Axboe9e645e112019-05-10 16:07:28 -0600834static void __io_free_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700835{
Jens Axboefcb323c2019-10-24 12:39:47 -0600836 struct io_ring_ctx *ctx = req->ctx;
837
Pavel Begunkovbbad27b2019-11-19 23:32:47 +0300838 if (req->flags & REQ_F_FREE_SQE)
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300839 kfree(req->sqe);
Jens Axboe09bb8392019-03-13 12:39:28 -0600840 if (req->file && !(req->flags & REQ_F_FIXED_FILE))
841 fput(req->file);
Jens Axboefcb323c2019-10-24 12:39:47 -0600842 if (req->flags & REQ_F_INFLIGHT) {
843 unsigned long flags;
844
845 spin_lock_irqsave(&ctx->inflight_lock, flags);
846 list_del(&req->inflight_entry);
847 if (waitqueue_active(&ctx->inflight_wait))
848 wake_up(&ctx->inflight_wait);
849 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
850 }
Jens Axboead8a48a2019-11-15 08:49:11 -0700851 if (req->flags & REQ_F_TIMEOUT)
852 kfree(req->timeout.data);
Jens Axboefcb323c2019-10-24 12:39:47 -0600853 percpu_ref_put(&ctx->refs);
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700854 if (likely(!io_is_fallback_req(req)))
855 kmem_cache_free(req_cachep, req);
856 else
857 clear_bit_unlock(0, (unsigned long *) ctx->fallback_req);
Jens Axboee65ef562019-03-12 10:16:44 -0600858}
859
Jackie Liua197f662019-11-08 08:09:12 -0700860static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -0600861{
Jackie Liua197f662019-11-08 08:09:12 -0700862 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -0700863 int ret;
864
Jens Axboead8a48a2019-11-15 08:49:11 -0700865 ret = hrtimer_try_to_cancel(&req->timeout.data->timer);
Jens Axboe2665abf2019-11-05 12:40:47 -0700866 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -0700867 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -0700868 io_commit_cqring(ctx);
869 req->flags &= ~REQ_F_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +0800870 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -0700871 return true;
872 }
873
874 return false;
875}
876
Jens Axboeba816ad2019-09-28 11:36:45 -0600877static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -0600878{
Jens Axboe2665abf2019-11-05 12:40:47 -0700879 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -0600880 struct io_kiocb *nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -0700881 bool wake_ev = false;
Jens Axboe9e645e112019-05-10 16:07:28 -0600882
Jens Axboe4d7dd462019-11-20 13:03:52 -0700883 /* Already got next link */
884 if (req->flags & REQ_F_LINK_NEXT)
885 return;
886
Jens Axboe9e645e112019-05-10 16:07:28 -0600887 /*
888 * The list should never be empty when we are called here. But could
889 * potentially happen if the chain is messed up, check to be on the
890 * safe side.
891 */
892 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, list);
Jens Axboe2665abf2019-11-05 12:40:47 -0700893 while (nxt) {
Jens Axboe76a46e02019-11-10 23:34:16 -0700894 list_del_init(&nxt->list);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700895
896 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
897 (nxt->flags & REQ_F_TIMEOUT)) {
898 wake_ev |= io_link_cancel_timeout(nxt);
899 nxt = list_first_entry_or_null(&req->link_list,
900 struct io_kiocb, list);
901 req->flags &= ~REQ_F_LINK_TIMEOUT;
902 continue;
903 }
Jens Axboe9e645e112019-05-10 16:07:28 -0600904 if (!list_empty(&req->link_list)) {
905 INIT_LIST_HEAD(&nxt->link_list);
906 list_splice(&req->link_list, &nxt->link_list);
907 nxt->flags |= REQ_F_LINK;
908 }
909
Pavel Begunkovb18fdf72019-11-21 23:21:02 +0300910 *nxtptr = nxt;
Jens Axboe94ae5e72019-11-14 19:39:52 -0700911 break;
Jens Axboe9e645e112019-05-10 16:07:28 -0600912 }
Jens Axboe2665abf2019-11-05 12:40:47 -0700913
Jens Axboe4d7dd462019-11-20 13:03:52 -0700914 req->flags |= REQ_F_LINK_NEXT;
Jens Axboe2665abf2019-11-05 12:40:47 -0700915 if (wake_ev)
916 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -0600917}
918
919/*
920 * Called if REQ_F_LINK is set, and we fail the head request
921 */
922static void io_fail_links(struct io_kiocb *req)
923{
Jens Axboe2665abf2019-11-05 12:40:47 -0700924 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -0600925 struct io_kiocb *link;
Jens Axboe2665abf2019-11-05 12:40:47 -0700926 unsigned long flags;
927
928 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -0600929
930 while (!list_empty(&req->link_list)) {
931 link = list_first_entry(&req->link_list, struct io_kiocb, list);
Jens Axboe2665abf2019-11-05 12:40:47 -0700932 list_del_init(&link->list);
Jens Axboe9e645e112019-05-10 16:07:28 -0600933
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +0200934 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -0700935
936 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300937 link->sqe->opcode == IORING_OP_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -0700938 io_link_cancel_timeout(link);
Jens Axboe2665abf2019-11-05 12:40:47 -0700939 } else {
Jens Axboe78e19bb2019-11-06 15:21:34 -0700940 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe978db572019-11-14 22:39:04 -0700941 __io_double_put_req(link);
Jens Axboe2665abf2019-11-05 12:40:47 -0700942 }
Jens Axboe5d960722019-11-19 15:31:28 -0700943 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -0600944 }
Jens Axboe2665abf2019-11-05 12:40:47 -0700945
946 io_commit_cqring(ctx);
947 spin_unlock_irqrestore(&ctx->completion_lock, flags);
948 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -0600949}
950
Jens Axboe4d7dd462019-11-20 13:03:52 -0700951static void io_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -0600952{
Jens Axboe4d7dd462019-11-20 13:03:52 -0700953 if (likely(!(req->flags & REQ_F_LINK)))
Jens Axboe2665abf2019-11-05 12:40:47 -0700954 return;
Jens Axboe2665abf2019-11-05 12:40:47 -0700955
Jens Axboe9e645e112019-05-10 16:07:28 -0600956 /*
957 * If LINK is set, we have dependent requests in this chain. If we
958 * didn't fail this request, queue the first one up, moving any other
959 * dependencies to the next request. In case of failure, fail the rest
960 * of the chain.
961 */
Jens Axboe2665abf2019-11-05 12:40:47 -0700962 if (req->flags & REQ_F_FAIL_LINK) {
963 io_fail_links(req);
Jens Axboe7c9e7f02019-11-12 08:15:53 -0700964 } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) ==
965 REQ_F_LINK_TIMEOUT) {
Jens Axboe2665abf2019-11-05 12:40:47 -0700966 struct io_ring_ctx *ctx = req->ctx;
967 unsigned long flags;
968
969 /*
970 * If this is a timeout link, we could be racing with the
971 * timeout timer. Grab the completion lock for this case to
Jens Axboe7c9e7f02019-11-12 08:15:53 -0700972 * protect against that.
Jens Axboe2665abf2019-11-05 12:40:47 -0700973 */
974 spin_lock_irqsave(&ctx->completion_lock, flags);
975 io_req_link_next(req, nxt);
976 spin_unlock_irqrestore(&ctx->completion_lock, flags);
977 } else {
978 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -0600979 }
Jens Axboe4d7dd462019-11-20 13:03:52 -0700980}
Jens Axboe9e645e112019-05-10 16:07:28 -0600981
Jackie Liuc69f8db2019-11-09 11:00:08 +0800982static void io_free_req(struct io_kiocb *req)
983{
Pavel Begunkov944e58b2019-11-21 23:21:01 +0300984 struct io_kiocb *nxt = NULL;
985
986 io_req_find_next(req, &nxt);
Pavel Begunkov70cf9f32019-11-21 23:21:00 +0300987 __io_free_req(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +0300988
989 if (nxt)
990 io_queue_async_work(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +0800991}
992
Jens Axboeba816ad2019-09-28 11:36:45 -0600993/*
994 * Drop reference to request, return next in chain (if there is one) if this
995 * was the last reference to this request.
996 */
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +0300997__attribute__((nonnull))
Jackie Liuec9c02a2019-11-08 23:50:36 +0800998static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboee65ef562019-03-12 10:16:44 -0600999{
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001000 io_req_find_next(req, nxtptr);
Jens Axboe4d7dd462019-11-20 13:03:52 -07001001
Jens Axboee65ef562019-03-12 10:16:44 -06001002 if (refcount_dec_and_test(&req->refs))
Jens Axboe4d7dd462019-11-20 13:03:52 -07001003 __io_free_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001004}
1005
Jens Axboe2b188cc2019-01-07 10:46:33 -07001006static void io_put_req(struct io_kiocb *req)
1007{
Jens Axboedef596e2019-01-09 08:59:42 -07001008 if (refcount_dec_and_test(&req->refs))
1009 io_free_req(req);
1010}
1011
Jens Axboe978db572019-11-14 22:39:04 -07001012/*
1013 * Must only be used if we don't need to care about links, usually from
1014 * within the completion handling itself.
1015 */
1016static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06001017{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001018 /* drop both submit and complete references */
1019 if (refcount_sub_and_test(2, &req->refs))
1020 __io_free_req(req);
1021}
1022
Jens Axboe978db572019-11-14 22:39:04 -07001023static void io_double_put_req(struct io_kiocb *req)
1024{
1025 /* drop both submit and complete references */
1026 if (refcount_sub_and_test(2, &req->refs))
1027 io_free_req(req);
1028}
1029
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001030static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06001031{
Jens Axboe84f97dc2019-11-06 11:27:53 -07001032 struct io_rings *rings = ctx->rings;
1033
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001034 /*
1035 * noflush == true is from the waitqueue handler, just ensure we wake
1036 * up the task, and the next invocation will flush the entries. We
1037 * cannot safely to it from here.
1038 */
1039 if (noflush && !list_empty(&ctx->cq_overflow_list))
1040 return -1U;
1041
1042 io_cqring_overflow_flush(ctx, false);
1043
Jens Axboea3a0e432019-08-20 11:03:11 -06001044 /* See comment at the top of this file */
1045 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00001046 return READ_ONCE(rings->cq.tail) - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06001047}
1048
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03001049static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1050{
1051 struct io_rings *rings = ctx->rings;
1052
1053 /* make sure SQ entry isn't read before tail */
1054 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1055}
1056
Jens Axboedef596e2019-01-09 08:59:42 -07001057/*
1058 * Find and free completed poll iocbs
1059 */
1060static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
1061 struct list_head *done)
1062{
1063 void *reqs[IO_IOPOLL_BATCH];
1064 struct io_kiocb *req;
Jens Axboe09bb8392019-03-13 12:39:28 -06001065 int to_free;
Jens Axboedef596e2019-01-09 08:59:42 -07001066
Jens Axboe09bb8392019-03-13 12:39:28 -06001067 to_free = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001068 while (!list_empty(done)) {
1069 req = list_first_entry(done, struct io_kiocb, list);
1070 list_del(&req->list);
1071
Jens Axboe78e19bb2019-11-06 15:21:34 -07001072 io_cqring_fill_event(req, req->result);
Jens Axboedef596e2019-01-09 08:59:42 -07001073 (*nr_events)++;
1074
Jens Axboe09bb8392019-03-13 12:39:28 -06001075 if (refcount_dec_and_test(&req->refs)) {
1076 /* If we're not using fixed files, we have to pair the
1077 * completion part with the file put. Use regular
1078 * completions for those, only batch free for fixed
Jens Axboe9e645e112019-05-10 16:07:28 -06001079 * file and non-linked commands.
Jens Axboe09bb8392019-03-13 12:39:28 -06001080 */
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03001081 if (((req->flags &
1082 (REQ_F_FIXED_FILE|REQ_F_LINK|REQ_F_FREE_SQE)) ==
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001083 REQ_F_FIXED_FILE) && !io_is_fallback_req(req)) {
Jens Axboe09bb8392019-03-13 12:39:28 -06001084 reqs[to_free++] = req;
1085 if (to_free == ARRAY_SIZE(reqs))
1086 io_free_req_many(ctx, reqs, &to_free);
Jens Axboe6b063142019-01-10 22:13:58 -07001087 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001088 io_free_req(req);
Jens Axboe6b063142019-01-10 22:13:58 -07001089 }
Jens Axboe9a56a232019-01-09 09:06:50 -07001090 }
Jens Axboedef596e2019-01-09 08:59:42 -07001091 }
Jens Axboedef596e2019-01-09 08:59:42 -07001092
Jens Axboe09bb8392019-03-13 12:39:28 -06001093 io_commit_cqring(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07001094 io_free_req_many(ctx, reqs, &to_free);
1095}
1096
1097static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1098 long min)
1099{
1100 struct io_kiocb *req, *tmp;
1101 LIST_HEAD(done);
1102 bool spin;
1103 int ret;
1104
1105 /*
1106 * Only spin for completions if we don't have multiple devices hanging
1107 * off our complete list, and we're under the requested amount.
1108 */
1109 spin = !ctx->poll_multi_file && *nr_events < min;
1110
1111 ret = 0;
1112 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
1113 struct kiocb *kiocb = &req->rw;
1114
1115 /*
1116 * Move completed entries to our local list. If we find a
1117 * request that requires polling, break out and complete
1118 * the done list first, if we have entries there.
1119 */
1120 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
1121 list_move_tail(&req->list, &done);
1122 continue;
1123 }
1124 if (!list_empty(&done))
1125 break;
1126
1127 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1128 if (ret < 0)
1129 break;
1130
1131 if (ret && spin)
1132 spin = false;
1133 ret = 0;
1134 }
1135
1136 if (!list_empty(&done))
1137 io_iopoll_complete(ctx, nr_events, &done);
1138
1139 return ret;
1140}
1141
1142/*
1143 * Poll for a mininum of 'min' events. Note that if min == 0 we consider that a
1144 * non-spinning poll check - we'll still enter the driver poll loop, but only
1145 * as a non-spinning completion check.
1146 */
1147static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
1148 long min)
1149{
Jens Axboe08f54392019-08-21 22:19:11 -06001150 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07001151 int ret;
1152
1153 ret = io_do_iopoll(ctx, nr_events, min);
1154 if (ret < 0)
1155 return ret;
1156 if (!min || *nr_events >= min)
1157 return 0;
1158 }
1159
1160 return 1;
1161}
1162
1163/*
1164 * We can't just wait for polled events to come to us, we have to actively
1165 * find and complete them.
1166 */
1167static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
1168{
1169 if (!(ctx->flags & IORING_SETUP_IOPOLL))
1170 return;
1171
1172 mutex_lock(&ctx->uring_lock);
1173 while (!list_empty(&ctx->poll_list)) {
1174 unsigned int nr_events = 0;
1175
1176 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -06001177
1178 /*
1179 * Ensure we allow local-to-the-cpu processing to take place,
1180 * in this case we need to ensure that we reap all events.
1181 */
1182 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -07001183 }
1184 mutex_unlock(&ctx->uring_lock);
1185}
1186
Jens Axboe2b2ed972019-10-25 10:06:15 -06001187static int __io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1188 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07001189{
Jens Axboe2b2ed972019-10-25 10:06:15 -06001190 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001191
1192 do {
1193 int tmin = 0;
1194
Jens Axboe500f9fb2019-08-19 12:15:59 -06001195 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06001196 * Don't enter poll loop if we already have events pending.
1197 * If we do, we can potentially be spinning for commands that
1198 * already triggered a CQE (eg in error).
1199 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001200 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06001201 break;
1202
1203 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06001204 * If a submit got punted to a workqueue, we can have the
1205 * application entering polling for a command before it gets
1206 * issued. That app will hold the uring_lock for the duration
1207 * of the poll right here, so we need to take a breather every
1208 * now and then to ensure that the issue has a chance to add
1209 * the poll to the issued list. Otherwise we can spin here
1210 * forever, while the workqueue is stuck trying to acquire the
1211 * very same mutex.
1212 */
1213 if (!(++iters & 7)) {
1214 mutex_unlock(&ctx->uring_lock);
1215 mutex_lock(&ctx->uring_lock);
1216 }
1217
Jens Axboedef596e2019-01-09 08:59:42 -07001218 if (*nr_events < min)
1219 tmin = min - *nr_events;
1220
1221 ret = io_iopoll_getevents(ctx, nr_events, tmin);
1222 if (ret <= 0)
1223 break;
1224 ret = 0;
1225 } while (min && !*nr_events && !need_resched());
1226
Jens Axboe2b2ed972019-10-25 10:06:15 -06001227 return ret;
1228}
1229
1230static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1231 long min)
1232{
1233 int ret;
1234
1235 /*
1236 * We disallow the app entering submit/complete with polling, but we
1237 * still need to lock the ring to prevent racing with polled issue
1238 * that got punted to a workqueue.
1239 */
1240 mutex_lock(&ctx->uring_lock);
1241 ret = __io_iopoll_check(ctx, nr_events, min);
Jens Axboe500f9fb2019-08-19 12:15:59 -06001242 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001243 return ret;
1244}
1245
Jens Axboe491381ce2019-10-17 09:20:46 -06001246static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001247{
Jens Axboe491381ce2019-10-17 09:20:46 -06001248 /*
1249 * Tell lockdep we inherited freeze protection from submission
1250 * thread.
1251 */
1252 if (req->flags & REQ_F_ISREG) {
1253 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001254
Jens Axboe491381ce2019-10-17 09:20:46 -06001255 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001256 }
Jens Axboe491381ce2019-10-17 09:20:46 -06001257 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001258}
1259
Jens Axboeba816ad2019-09-28 11:36:45 -06001260static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001261{
1262 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1263
Jens Axboe491381ce2019-10-17 09:20:46 -06001264 if (kiocb->ki_flags & IOCB_WRITE)
1265 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001266
Jens Axboe9e645e112019-05-10 16:07:28 -06001267 if ((req->flags & REQ_F_LINK) && res != req->result)
1268 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001269 io_cqring_add_event(req, res);
Jens Axboeba816ad2019-09-28 11:36:45 -06001270}
1271
1272static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1273{
1274 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1275
1276 io_complete_rw_common(kiocb, res);
Jens Axboee65ef562019-03-12 10:16:44 -06001277 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001278}
1279
Jens Axboeba816ad2019-09-28 11:36:45 -06001280static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res)
1281{
1282 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001283 struct io_kiocb *nxt = NULL;
Jens Axboeba816ad2019-09-28 11:36:45 -06001284
1285 io_complete_rw_common(kiocb, res);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001286 io_put_req_find_next(req, &nxt);
1287
1288 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001289}
1290
Jens Axboedef596e2019-01-09 08:59:42 -07001291static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1292{
1293 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1294
Jens Axboe491381ce2019-10-17 09:20:46 -06001295 if (kiocb->ki_flags & IOCB_WRITE)
1296 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001297
Jens Axboe9e645e112019-05-10 16:07:28 -06001298 if ((req->flags & REQ_F_LINK) && res != req->result)
1299 req->flags |= REQ_F_FAIL_LINK;
1300 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -07001301 if (res != -EAGAIN)
1302 req->flags |= REQ_F_IOPOLL_COMPLETED;
1303}
1304
1305/*
1306 * After the iocb has been issued, it's safe to be found on the poll list.
1307 * Adding the kiocb to the list AFTER submission ensures that we don't
1308 * find it from a io_iopoll_getevents() thread before the issuer is done
1309 * accessing the kiocb cookie.
1310 */
1311static void io_iopoll_req_issued(struct io_kiocb *req)
1312{
1313 struct io_ring_ctx *ctx = req->ctx;
1314
1315 /*
1316 * Track whether we have multiple files in our lists. This will impact
1317 * how we do polling eventually, not spinning if we're on potentially
1318 * different devices.
1319 */
1320 if (list_empty(&ctx->poll_list)) {
1321 ctx->poll_multi_file = false;
1322 } else if (!ctx->poll_multi_file) {
1323 struct io_kiocb *list_req;
1324
1325 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1326 list);
1327 if (list_req->rw.ki_filp != req->rw.ki_filp)
1328 ctx->poll_multi_file = true;
1329 }
1330
1331 /*
1332 * For fast devices, IO may have already completed. If it has, add
1333 * it to the front so we find it first.
1334 */
1335 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1336 list_add(&req->list, &ctx->poll_list);
1337 else
1338 list_add_tail(&req->list, &ctx->poll_list);
1339}
1340
Jens Axboe3d6770f2019-04-13 11:50:54 -06001341static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07001342{
Jens Axboe3d6770f2019-04-13 11:50:54 -06001343 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -07001344 int diff = state->has_refs - state->used_refs;
1345
1346 if (diff)
1347 fput_many(state->file, diff);
1348 state->file = NULL;
1349 }
1350}
1351
1352/*
1353 * Get as many references to a file as we have IOs left in this submission,
1354 * assuming most submissions are for one file, or at least that each file
1355 * has more than one submission.
1356 */
1357static struct file *io_file_get(struct io_submit_state *state, int fd)
1358{
1359 if (!state)
1360 return fget(fd);
1361
1362 if (state->file) {
1363 if (state->fd == fd) {
1364 state->used_refs++;
1365 state->ios_left--;
1366 return state->file;
1367 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06001368 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07001369 }
1370 state->file = fget_many(fd, state->ios_left);
1371 if (!state->file)
1372 return NULL;
1373
1374 state->fd = fd;
1375 state->has_refs = state->ios_left;
1376 state->used_refs = 1;
1377 state->ios_left--;
1378 return state->file;
1379}
1380
Jens Axboe2b188cc2019-01-07 10:46:33 -07001381/*
1382 * If we tracked the file through the SCM inflight mechanism, we could support
1383 * any file. For now, just ensure that anything potentially problematic is done
1384 * inline.
1385 */
1386static bool io_file_supports_async(struct file *file)
1387{
1388 umode_t mode = file_inode(file)->i_mode;
1389
1390 if (S_ISBLK(mode) || S_ISCHR(mode))
1391 return true;
1392 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
1393 return true;
1394
1395 return false;
1396}
1397
Pavel Begunkov267bc902019-11-07 01:41:08 +03001398static int io_prep_rw(struct io_kiocb *req, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001399{
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001400 const struct io_uring_sqe *sqe = req->sqe;
Jens Axboedef596e2019-01-09 08:59:42 -07001401 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001402 struct kiocb *kiocb = &req->rw;
Jens Axboe09bb8392019-03-13 12:39:28 -06001403 unsigned ioprio;
1404 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001405
Jens Axboe09bb8392019-03-13 12:39:28 -06001406 if (!req->file)
1407 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001408
Jens Axboe491381ce2019-10-17 09:20:46 -06001409 if (S_ISREG(file_inode(req->file)->i_mode))
1410 req->flags |= REQ_F_ISREG;
1411
1412 /*
1413 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
1414 * we know to async punt it even if it was opened O_NONBLOCK
1415 */
1416 if (force_nonblock && !io_file_supports_async(req->file)) {
1417 req->flags |= REQ_F_MUST_PUNT;
1418 return -EAGAIN;
1419 }
Jens Axboe6b063142019-01-10 22:13:58 -07001420
Jens Axboe2b188cc2019-01-07 10:46:33 -07001421 kiocb->ki_pos = READ_ONCE(sqe->off);
1422 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
1423 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
1424
1425 ioprio = READ_ONCE(sqe->ioprio);
1426 if (ioprio) {
1427 ret = ioprio_check_cap(ioprio);
1428 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06001429 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001430
1431 kiocb->ki_ioprio = ioprio;
1432 } else
1433 kiocb->ki_ioprio = get_current_ioprio();
1434
1435 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
1436 if (unlikely(ret))
Jens Axboe09bb8392019-03-13 12:39:28 -06001437 return ret;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001438
1439 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06001440 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
1441 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02001442 req->flags |= REQ_F_NOWAIT;
1443
1444 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001445 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001446
Jens Axboedef596e2019-01-09 08:59:42 -07001447 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07001448 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
1449 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06001450 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001451
Jens Axboedef596e2019-01-09 08:59:42 -07001452 kiocb->ki_flags |= IOCB_HIPRI;
1453 kiocb->ki_complete = io_complete_rw_iopoll;
Jens Axboe6873e0b2019-10-30 13:53:09 -06001454 req->result = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001455 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001456 if (kiocb->ki_flags & IOCB_HIPRI)
1457 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07001458 kiocb->ki_complete = io_complete_rw;
1459 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001460 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001461}
1462
1463static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
1464{
1465 switch (ret) {
1466 case -EIOCBQUEUED:
1467 break;
1468 case -ERESTARTSYS:
1469 case -ERESTARTNOINTR:
1470 case -ERESTARTNOHAND:
1471 case -ERESTART_RESTARTBLOCK:
1472 /*
1473 * We can't just restart the syscall, since previously
1474 * submitted sqes may already be in progress. Just fail this
1475 * IO with EINTR.
1476 */
1477 ret = -EINTR;
1478 /* fall through */
1479 default:
1480 kiocb->ki_complete(kiocb, ret, 0);
1481 }
1482}
1483
Jens Axboeba816ad2019-09-28 11:36:45 -06001484static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt,
1485 bool in_async)
1486{
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001487 if (in_async && ret >= 0 && kiocb->ki_complete == io_complete_rw)
Jens Axboeba816ad2019-09-28 11:36:45 -06001488 *nxt = __io_complete_rw(kiocb, ret);
1489 else
1490 io_rw_done(kiocb, ret);
1491}
1492
Jens Axboeedafcce2019-01-09 09:16:05 -07001493static int io_import_fixed(struct io_ring_ctx *ctx, int rw,
1494 const struct io_uring_sqe *sqe,
1495 struct iov_iter *iter)
1496{
1497 size_t len = READ_ONCE(sqe->len);
1498 struct io_mapped_ubuf *imu;
1499 unsigned index, buf_index;
1500 size_t offset;
1501 u64 buf_addr;
1502
1503 /* attempt to use fixed buffers without having provided iovecs */
1504 if (unlikely(!ctx->user_bufs))
1505 return -EFAULT;
1506
1507 buf_index = READ_ONCE(sqe->buf_index);
1508 if (unlikely(buf_index >= ctx->nr_user_bufs))
1509 return -EFAULT;
1510
1511 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
1512 imu = &ctx->user_bufs[index];
1513 buf_addr = READ_ONCE(sqe->addr);
1514
1515 /* overflow */
1516 if (buf_addr + len < buf_addr)
1517 return -EFAULT;
1518 /* not inside the mapped region */
1519 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
1520 return -EFAULT;
1521
1522 /*
1523 * May not be a start of buffer, set size appropriately
1524 * and advance us to the beginning.
1525 */
1526 offset = buf_addr - imu->ubuf;
1527 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06001528
1529 if (offset) {
1530 /*
1531 * Don't use iov_iter_advance() here, as it's really slow for
1532 * using the latter parts of a big fixed buffer - it iterates
1533 * over each segment manually. We can cheat a bit here, because
1534 * we know that:
1535 *
1536 * 1) it's a BVEC iter, we set it up
1537 * 2) all bvecs are PAGE_SIZE in size, except potentially the
1538 * first and last bvec
1539 *
1540 * So just find our index, and adjust the iterator afterwards.
1541 * If the offset is within the first bvec (or the whole first
1542 * bvec, just use iov_iter_advance(). This makes it easier
1543 * since we can just skip the first segment, which may not
1544 * be PAGE_SIZE aligned.
1545 */
1546 const struct bio_vec *bvec = imu->bvec;
1547
1548 if (offset <= bvec->bv_len) {
1549 iov_iter_advance(iter, offset);
1550 } else {
1551 unsigned long seg_skip;
1552
1553 /* skip first vec */
1554 offset -= bvec->bv_len;
1555 seg_skip = 1 + (offset >> PAGE_SHIFT);
1556
1557 iter->bvec = bvec + seg_skip;
1558 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02001559 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001560 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001561 }
1562 }
1563
Jens Axboe5e559562019-11-13 16:12:46 -07001564 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001565}
1566
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001567static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
1568 struct iovec **iovec, struct iov_iter *iter)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001569{
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001570 const struct io_uring_sqe *sqe = req->sqe;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001571 void __user *buf = u64_to_user_ptr(READ_ONCE(sqe->addr));
1572 size_t sqe_len = READ_ONCE(sqe->len);
Jens Axboeedafcce2019-01-09 09:16:05 -07001573 u8 opcode;
1574
1575 /*
1576 * We're reading ->opcode for the second time, but the first read
1577 * doesn't care whether it's _FIXED or not, so it doesn't matter
1578 * whether ->opcode changes concurrently. The first read does care
1579 * about whether it is a READ or a WRITE, so we don't trust this read
1580 * for that purpose and instead let the caller pass in the read/write
1581 * flag.
1582 */
1583 opcode = READ_ONCE(sqe->opcode);
1584 if (opcode == IORING_OP_READ_FIXED ||
1585 opcode == IORING_OP_WRITE_FIXED) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001586 ssize_t ret = io_import_fixed(req->ctx, rw, sqe, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07001587 *iovec = NULL;
1588 return ret;
1589 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001590
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001591 if (!req->has_user)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001592 return -EFAULT;
1593
1594#ifdef CONFIG_COMPAT
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001595 if (req->ctx->compat)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001596 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
1597 iovec, iter);
1598#endif
1599
1600 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
1601}
1602
Jens Axboe32960612019-09-23 11:05:34 -06001603/*
1604 * For files that don't have ->read_iter() and ->write_iter(), handle them
1605 * by looping over ->read() or ->write() manually.
1606 */
1607static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
1608 struct iov_iter *iter)
1609{
1610 ssize_t ret = 0;
1611
1612 /*
1613 * Don't support polled IO through this interface, and we can't
1614 * support non-blocking either. For the latter, this just causes
1615 * the kiocb to be handled from an async context.
1616 */
1617 if (kiocb->ki_flags & IOCB_HIPRI)
1618 return -EOPNOTSUPP;
1619 if (kiocb->ki_flags & IOCB_NOWAIT)
1620 return -EAGAIN;
1621
1622 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03001623 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06001624 ssize_t nr;
1625
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03001626 if (!iov_iter_is_bvec(iter)) {
1627 iovec = iov_iter_iovec(iter);
1628 } else {
1629 /* fixed buffers import bvec */
1630 iovec.iov_base = kmap(iter->bvec->bv_page)
1631 + iter->iov_offset;
1632 iovec.iov_len = min(iter->count,
1633 iter->bvec->bv_len - iter->iov_offset);
1634 }
1635
Jens Axboe32960612019-09-23 11:05:34 -06001636 if (rw == READ) {
1637 nr = file->f_op->read(file, iovec.iov_base,
1638 iovec.iov_len, &kiocb->ki_pos);
1639 } else {
1640 nr = file->f_op->write(file, iovec.iov_base,
1641 iovec.iov_len, &kiocb->ki_pos);
1642 }
1643
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03001644 if (iov_iter_is_bvec(iter))
1645 kunmap(iter->bvec->bv_page);
1646
Jens Axboe32960612019-09-23 11:05:34 -06001647 if (nr < 0) {
1648 if (!ret)
1649 ret = nr;
1650 break;
1651 }
1652 ret += nr;
1653 if (nr != iovec.iov_len)
1654 break;
1655 iov_iter_advance(iter, nr);
1656 }
1657
1658 return ret;
1659}
1660
Pavel Begunkov267bc902019-11-07 01:41:08 +03001661static int io_read(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001662 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001663{
1664 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1665 struct kiocb *kiocb = &req->rw;
1666 struct iov_iter iter;
1667 struct file *file;
Jens Axboe31b51512019-01-18 22:56:34 -07001668 size_t iov_count;
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001669 ssize_t read_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001670
Pavel Begunkov267bc902019-11-07 01:41:08 +03001671 ret = io_prep_rw(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001672 if (ret)
1673 return ret;
1674 file = kiocb->ki_filp;
1675
Jens Axboe2b188cc2019-01-07 10:46:33 -07001676 if (unlikely(!(file->f_mode & FMODE_READ)))
Jens Axboe09bb8392019-03-13 12:39:28 -06001677 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001678
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001679 ret = io_import_iovec(READ, req, &iovec, &iter);
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001680 if (ret < 0)
Jens Axboe09bb8392019-03-13 12:39:28 -06001681 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001682
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001683 read_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06001684 if (req->flags & REQ_F_LINK)
1685 req->result = read_size;
1686
Jens Axboe31b51512019-01-18 22:56:34 -07001687 iov_count = iov_iter_count(&iter);
1688 ret = rw_verify_area(READ, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001689 if (!ret) {
1690 ssize_t ret2;
1691
Jens Axboe32960612019-09-23 11:05:34 -06001692 if (file->f_op->read_iter)
1693 ret2 = call_read_iter(file, kiocb, &iter);
1694 else
1695 ret2 = loop_rw_iter(READ, file, kiocb, &iter);
1696
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001697 /*
1698 * In case of a short read, punt to async. This can happen
1699 * if we have data partially cached. Alternatively we can
1700 * return the short read, in which case the application will
1701 * need to issue another SQE and wait for it. That SQE will
1702 * need async punt anyway, so it's more efficient to do it
1703 * here.
1704 */
Jens Axboe491381ce2019-10-17 09:20:46 -06001705 if (force_nonblock && !(req->flags & REQ_F_NOWAIT) &&
1706 (req->flags & REQ_F_ISREG) &&
1707 ret2 > 0 && ret2 < read_size)
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001708 ret2 = -EAGAIN;
1709 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboe561fb042019-10-24 07:25:42 -06001710 if (!force_nonblock || ret2 != -EAGAIN)
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001711 kiocb_done(kiocb, ret2, nxt, req->in_async);
Jens Axboe561fb042019-10-24 07:25:42 -06001712 else
Jens Axboe2b188cc2019-01-07 10:46:33 -07001713 ret = -EAGAIN;
1714 }
1715 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001716 return ret;
1717}
1718
Pavel Begunkov267bc902019-11-07 01:41:08 +03001719static int io_write(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001720 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001721{
1722 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1723 struct kiocb *kiocb = &req->rw;
1724 struct iov_iter iter;
1725 struct file *file;
Jens Axboe31b51512019-01-18 22:56:34 -07001726 size_t iov_count;
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001727 ssize_t ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001728
Pavel Begunkov267bc902019-11-07 01:41:08 +03001729 ret = io_prep_rw(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001730 if (ret)
1731 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001732
Jens Axboe2b188cc2019-01-07 10:46:33 -07001733 file = kiocb->ki_filp;
1734 if (unlikely(!(file->f_mode & FMODE_WRITE)))
Jens Axboe09bb8392019-03-13 12:39:28 -06001735 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001736
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001737 ret = io_import_iovec(WRITE, req, &iovec, &iter);
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001738 if (ret < 0)
Jens Axboe09bb8392019-03-13 12:39:28 -06001739 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001740
Jens Axboe9e645e112019-05-10 16:07:28 -06001741 if (req->flags & REQ_F_LINK)
1742 req->result = ret;
1743
Jens Axboe31b51512019-01-18 22:56:34 -07001744 iov_count = iov_iter_count(&iter);
1745
1746 ret = -EAGAIN;
Jens Axboe561fb042019-10-24 07:25:42 -06001747 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT))
Jens Axboe31b51512019-01-18 22:56:34 -07001748 goto out_free;
Jens Axboe31b51512019-01-18 22:56:34 -07001749
1750 ret = rw_verify_area(WRITE, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001751 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01001752 ssize_t ret2;
1753
Jens Axboe2b188cc2019-01-07 10:46:33 -07001754 /*
1755 * Open-code file_start_write here to grab freeze protection,
1756 * which will be released by another thread in
1757 * io_complete_rw(). Fool lockdep by telling it the lock got
1758 * released so that it doesn't complain about the held lock when
1759 * we return to userspace.
1760 */
Jens Axboe491381ce2019-10-17 09:20:46 -06001761 if (req->flags & REQ_F_ISREG) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001762 __sb_start_write(file_inode(file)->i_sb,
1763 SB_FREEZE_WRITE, true);
1764 __sb_writers_release(file_inode(file)->i_sb,
1765 SB_FREEZE_WRITE);
1766 }
1767 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01001768
Jens Axboe32960612019-09-23 11:05:34 -06001769 if (file->f_op->write_iter)
1770 ret2 = call_write_iter(file, kiocb, &iter);
1771 else
1772 ret2 = loop_rw_iter(WRITE, file, kiocb, &iter);
Jens Axboe561fb042019-10-24 07:25:42 -06001773 if (!force_nonblock || ret2 != -EAGAIN)
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001774 kiocb_done(kiocb, ret2, nxt, req->in_async);
Jens Axboe561fb042019-10-24 07:25:42 -06001775 else
Roman Penyaev9bf79332019-03-25 20:09:24 +01001776 ret = -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001777 }
Jens Axboe31b51512019-01-18 22:56:34 -07001778out_free:
Jens Axboe2b188cc2019-01-07 10:46:33 -07001779 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001780 return ret;
1781}
1782
1783/*
1784 * IORING_OP_NOP just posts a completion event, nothing else.
1785 */
Jens Axboe78e19bb2019-11-06 15:21:34 -07001786static int io_nop(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001787{
1788 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001789
Jens Axboedef596e2019-01-09 08:59:42 -07001790 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1791 return -EINVAL;
1792
Jens Axboe78e19bb2019-11-06 15:21:34 -07001793 io_cqring_add_event(req, 0);
Jens Axboee65ef562019-03-12 10:16:44 -06001794 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001795 return 0;
1796}
1797
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001798static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1799{
Jens Axboe6b063142019-01-10 22:13:58 -07001800 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001801
Jens Axboe09bb8392019-03-13 12:39:28 -06001802 if (!req->file)
1803 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001804
Jens Axboe6b063142019-01-10 22:13:58 -07001805 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07001806 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07001807 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001808 return -EINVAL;
1809
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001810 return 0;
1811}
1812
1813static int io_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001814 struct io_kiocb **nxt, bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001815{
1816 loff_t sqe_off = READ_ONCE(sqe->off);
1817 loff_t sqe_len = READ_ONCE(sqe->len);
1818 loff_t end = sqe_off + sqe_len;
1819 unsigned fsync_flags;
1820 int ret;
1821
1822 fsync_flags = READ_ONCE(sqe->fsync_flags);
1823 if (unlikely(fsync_flags & ~IORING_FSYNC_DATASYNC))
1824 return -EINVAL;
1825
1826 ret = io_prep_fsync(req, sqe);
1827 if (ret)
1828 return ret;
1829
1830 /* fsync always requires a blocking context */
1831 if (force_nonblock)
1832 return -EAGAIN;
1833
1834 ret = vfs_fsync_range(req->rw.ki_filp, sqe_off,
1835 end > 0 ? end : LLONG_MAX,
1836 fsync_flags & IORING_FSYNC_DATASYNC);
1837
Jens Axboe9e645e112019-05-10 16:07:28 -06001838 if (ret < 0 && (req->flags & REQ_F_LINK))
1839 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001840 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001841 io_put_req_find_next(req, nxt);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001842 return 0;
1843}
1844
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001845static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1846{
1847 struct io_ring_ctx *ctx = req->ctx;
1848 int ret = 0;
1849
1850 if (!req->file)
1851 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001852
1853 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1854 return -EINVAL;
1855 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
1856 return -EINVAL;
1857
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001858 return ret;
1859}
1860
1861static int io_sync_file_range(struct io_kiocb *req,
1862 const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001863 struct io_kiocb **nxt,
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001864 bool force_nonblock)
1865{
1866 loff_t sqe_off;
1867 loff_t sqe_len;
1868 unsigned flags;
1869 int ret;
1870
1871 ret = io_prep_sfr(req, sqe);
1872 if (ret)
1873 return ret;
1874
1875 /* sync_file_range always requires a blocking context */
1876 if (force_nonblock)
1877 return -EAGAIN;
1878
1879 sqe_off = READ_ONCE(sqe->off);
1880 sqe_len = READ_ONCE(sqe->len);
1881 flags = READ_ONCE(sqe->sync_range_flags);
1882
1883 ret = sync_file_range(req->rw.ki_filp, sqe_off, sqe_len, flags);
1884
Jens Axboe9e645e112019-05-10 16:07:28 -06001885 if (ret < 0 && (req->flags & REQ_F_LINK))
1886 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001887 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001888 io_put_req_find_next(req, nxt);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001889 return 0;
1890}
1891
Jens Axboe0fa03c62019-04-19 13:34:07 -06001892#if defined(CONFIG_NET)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001893static int io_send_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001894 struct io_kiocb **nxt, bool force_nonblock,
Jens Axboeaa1fa282019-04-19 13:38:09 -06001895 long (*fn)(struct socket *, struct user_msghdr __user *,
1896 unsigned int))
1897{
Jens Axboe0fa03c62019-04-19 13:34:07 -06001898 struct socket *sock;
1899 int ret;
1900
1901 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1902 return -EINVAL;
1903
1904 sock = sock_from_file(req->file, &ret);
1905 if (sock) {
1906 struct user_msghdr __user *msg;
1907 unsigned flags;
1908
1909 flags = READ_ONCE(sqe->msg_flags);
1910 if (flags & MSG_DONTWAIT)
1911 req->flags |= REQ_F_NOWAIT;
1912 else if (force_nonblock)
1913 flags |= MSG_DONTWAIT;
1914
1915 msg = (struct user_msghdr __user *) (unsigned long)
1916 READ_ONCE(sqe->addr);
1917
Jens Axboeaa1fa282019-04-19 13:38:09 -06001918 ret = fn(sock, msg, flags);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001919 if (force_nonblock && ret == -EAGAIN)
1920 return ret;
1921 }
1922
Jens Axboe78e19bb2019-11-06 15:21:34 -07001923 io_cqring_add_event(req, ret);
Jens Axboef1f40852019-11-05 20:33:16 -07001924 if (ret < 0 && (req->flags & REQ_F_LINK))
1925 req->flags |= REQ_F_FAIL_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001926 io_put_req_find_next(req, nxt);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001927 return 0;
Jens Axboeaa1fa282019-04-19 13:38:09 -06001928}
1929#endif
1930
1931static int io_sendmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001932 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001933{
1934#if defined(CONFIG_NET)
Jens Axboeba816ad2019-09-28 11:36:45 -06001935 return io_send_recvmsg(req, sqe, nxt, force_nonblock,
1936 __sys_sendmsg_sock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06001937#else
1938 return -EOPNOTSUPP;
1939#endif
1940}
1941
1942static int io_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001943 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001944{
1945#if defined(CONFIG_NET)
Jens Axboeba816ad2019-09-28 11:36:45 -06001946 return io_send_recvmsg(req, sqe, nxt, force_nonblock,
1947 __sys_recvmsg_sock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001948#else
1949 return -EOPNOTSUPP;
1950#endif
1951}
1952
Jens Axboe17f2fe32019-10-17 14:42:58 -06001953static int io_accept(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1954 struct io_kiocb **nxt, bool force_nonblock)
1955{
1956#if defined(CONFIG_NET)
1957 struct sockaddr __user *addr;
1958 int __user *addr_len;
1959 unsigned file_flags;
1960 int flags, ret;
1961
1962 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
1963 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05001964 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06001965 return -EINVAL;
1966
1967 addr = (struct sockaddr __user *) (unsigned long) READ_ONCE(sqe->addr);
1968 addr_len = (int __user *) (unsigned long) READ_ONCE(sqe->addr2);
1969 flags = READ_ONCE(sqe->accept_flags);
1970 file_flags = force_nonblock ? O_NONBLOCK : 0;
1971
1972 ret = __sys_accept4_file(req->file, file_flags, addr, addr_len, flags);
1973 if (ret == -EAGAIN && force_nonblock) {
1974 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
1975 return -EAGAIN;
1976 }
Jens Axboe8e3cca12019-11-09 19:52:33 -07001977 if (ret == -ERESTARTSYS)
1978 ret = -EINTR;
Jens Axboe17f2fe32019-10-17 14:42:58 -06001979 if (ret < 0 && (req->flags & REQ_F_LINK))
1980 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001981 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001982 io_put_req_find_next(req, nxt);
Jens Axboe17f2fe32019-10-17 14:42:58 -06001983 return 0;
1984#else
1985 return -EOPNOTSUPP;
1986#endif
1987}
1988
Jens Axboef8e85cf2019-11-23 14:24:24 -07001989static int io_connect(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1990 struct io_kiocb **nxt, bool force_nonblock)
1991{
1992#if defined(CONFIG_NET)
1993 struct sockaddr __user *addr;
1994 unsigned file_flags;
1995 int addr_len, ret;
1996
1997 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
1998 return -EINVAL;
1999 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
2000 return -EINVAL;
2001
2002 addr = (struct sockaddr __user *) (unsigned long) READ_ONCE(sqe->addr);
2003 addr_len = READ_ONCE(sqe->addr2);
2004 file_flags = force_nonblock ? O_NONBLOCK : 0;
2005
2006 ret = __sys_connect_file(req->file, addr, addr_len, file_flags);
2007 if (ret == -EAGAIN && force_nonblock)
2008 return -EAGAIN;
2009 if (ret == -ERESTARTSYS)
2010 ret = -EINTR;
2011 if (ret < 0 && (req->flags & REQ_F_LINK))
2012 req->flags |= REQ_F_FAIL_LINK;
2013 io_cqring_add_event(req, ret);
2014 io_put_req_find_next(req, nxt);
2015 return 0;
2016#else
2017 return -EOPNOTSUPP;
2018#endif
2019}
2020
Jens Axboeeac406c2019-11-14 12:09:58 -07002021static inline void io_poll_remove_req(struct io_kiocb *req)
2022{
2023 if (!RB_EMPTY_NODE(&req->rb_node)) {
2024 rb_erase(&req->rb_node, &req->ctx->cancel_tree);
2025 RB_CLEAR_NODE(&req->rb_node);
2026 }
2027}
2028
Jens Axboe221c5eb2019-01-17 09:41:58 -07002029static void io_poll_remove_one(struct io_kiocb *req)
2030{
2031 struct io_poll_iocb *poll = &req->poll;
2032
2033 spin_lock(&poll->head->lock);
2034 WRITE_ONCE(poll->canceled, true);
2035 if (!list_empty(&poll->wait.entry)) {
2036 list_del_init(&poll->wait.entry);
Jackie Liua197f662019-11-08 08:09:12 -07002037 io_queue_async_work(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002038 }
2039 spin_unlock(&poll->head->lock);
Jens Axboeeac406c2019-11-14 12:09:58 -07002040 io_poll_remove_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002041}
2042
2043static void io_poll_remove_all(struct io_ring_ctx *ctx)
2044{
Jens Axboeeac406c2019-11-14 12:09:58 -07002045 struct rb_node *node;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002046 struct io_kiocb *req;
2047
2048 spin_lock_irq(&ctx->completion_lock);
Jens Axboeeac406c2019-11-14 12:09:58 -07002049 while ((node = rb_first(&ctx->cancel_tree)) != NULL) {
2050 req = rb_entry(node, struct io_kiocb, rb_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002051 io_poll_remove_one(req);
2052 }
2053 spin_unlock_irq(&ctx->completion_lock);
2054}
2055
Jens Axboe47f46762019-11-09 17:43:02 -07002056static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
2057{
Jens Axboeeac406c2019-11-14 12:09:58 -07002058 struct rb_node *p, *parent = NULL;
Jens Axboe47f46762019-11-09 17:43:02 -07002059 struct io_kiocb *req;
2060
Jens Axboeeac406c2019-11-14 12:09:58 -07002061 p = ctx->cancel_tree.rb_node;
2062 while (p) {
2063 parent = p;
2064 req = rb_entry(parent, struct io_kiocb, rb_node);
2065 if (sqe_addr < req->user_data) {
2066 p = p->rb_left;
2067 } else if (sqe_addr > req->user_data) {
2068 p = p->rb_right;
2069 } else {
2070 io_poll_remove_one(req);
2071 return 0;
2072 }
Jens Axboe47f46762019-11-09 17:43:02 -07002073 }
2074
2075 return -ENOENT;
2076}
2077
Jens Axboe221c5eb2019-01-17 09:41:58 -07002078/*
2079 * Find a running poll command that matches one specified in sqe->addr,
2080 * and remove it if found.
2081 */
2082static int io_poll_remove(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2083{
2084 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07002085 int ret;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002086
2087 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2088 return -EINVAL;
2089 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
2090 sqe->poll_events)
2091 return -EINVAL;
2092
2093 spin_lock_irq(&ctx->completion_lock);
Jens Axboe47f46762019-11-09 17:43:02 -07002094 ret = io_poll_cancel(ctx, READ_ONCE(sqe->addr));
Jens Axboe221c5eb2019-01-17 09:41:58 -07002095 spin_unlock_irq(&ctx->completion_lock);
2096
Jens Axboe78e19bb2019-11-06 15:21:34 -07002097 io_cqring_add_event(req, ret);
Jens Axboef1f40852019-11-05 20:33:16 -07002098 if (ret < 0 && (req->flags & REQ_F_LINK))
2099 req->flags |= REQ_F_FAIL_LINK;
Jens Axboee65ef562019-03-12 10:16:44 -06002100 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002101 return 0;
2102}
2103
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002104static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002105{
Jackie Liua197f662019-11-08 08:09:12 -07002106 struct io_ring_ctx *ctx = req->ctx;
2107
Jens Axboe8c838782019-03-12 15:48:16 -06002108 req->poll.done = true;
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 Axboe221c5eb2019-01-17 09:41:58 -07002146 add_wait_queue(poll->head, &poll->wait);
2147 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{
2166 struct io_poll_iocb *poll = container_of(wait, struct io_poll_iocb,
2167 wait);
2168 struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
2169 struct io_ring_ctx *ctx = req->ctx;
2170 __poll_t mask = key_to_poll(key);
Jens Axboe8c838782019-03-12 15:48:16 -06002171 unsigned long flags;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002172
2173 /* for instances that support it check for an event match first: */
Jens Axboe8c838782019-03-12 15:48:16 -06002174 if (mask && !(mask & poll->events))
2175 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002176
2177 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06002178
Jens Axboe7c9e7f02019-11-12 08:15:53 -07002179 /*
2180 * Run completion inline if we can. We're using trylock here because
2181 * we are violating the completion_lock -> poll wq lock ordering.
2182 * If we have a link timeout we're going to need the completion_lock
2183 * for finalizing the request, mark us as having grabbed that already.
2184 */
Jens Axboe8c838782019-03-12 15:48:16 -06002185 if (mask && spin_trylock_irqsave(&ctx->completion_lock, flags)) {
Jens Axboeeac406c2019-11-14 12:09:58 -07002186 io_poll_remove_req(req);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002187 io_poll_complete(req, mask, 0);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07002188 req->flags |= REQ_F_COMP_LOCKED;
2189 io_put_req(req);
Jens Axboe8c838782019-03-12 15:48:16 -06002190 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2191
2192 io_cqring_ev_posted(ctx);
Jens Axboe8c838782019-03-12 15:48:16 -06002193 } else {
Jackie Liua197f662019-11-08 08:09:12 -07002194 io_queue_async_work(req);
Jens Axboe8c838782019-03-12 15:48:16 -06002195 }
2196
Jens Axboe221c5eb2019-01-17 09:41:58 -07002197 return 1;
2198}
2199
2200struct io_poll_table {
2201 struct poll_table_struct pt;
2202 struct io_kiocb *req;
2203 int error;
2204};
2205
2206static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
2207 struct poll_table_struct *p)
2208{
2209 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
2210
2211 if (unlikely(pt->req->poll.head)) {
2212 pt->error = -EINVAL;
2213 return;
2214 }
2215
2216 pt->error = 0;
2217 pt->req->poll.head = head;
2218 add_wait_queue(head, &pt->req->poll.wait);
2219}
2220
Jens Axboeeac406c2019-11-14 12:09:58 -07002221static void io_poll_req_insert(struct io_kiocb *req)
2222{
2223 struct io_ring_ctx *ctx = req->ctx;
2224 struct rb_node **p = &ctx->cancel_tree.rb_node;
2225 struct rb_node *parent = NULL;
2226 struct io_kiocb *tmp;
2227
2228 while (*p) {
2229 parent = *p;
2230 tmp = rb_entry(parent, struct io_kiocb, rb_node);
2231 if (req->user_data < tmp->user_data)
2232 p = &(*p)->rb_left;
2233 else
2234 p = &(*p)->rb_right;
2235 }
2236 rb_link_node(&req->rb_node, parent, p);
2237 rb_insert_color(&req->rb_node, &ctx->cancel_tree);
2238}
2239
Jens Axboe89723d02019-11-05 15:32:58 -07002240static int io_poll_add(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2241 struct io_kiocb **nxt)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002242{
2243 struct io_poll_iocb *poll = &req->poll;
2244 struct io_ring_ctx *ctx = req->ctx;
2245 struct io_poll_table ipt;
Jens Axboe8c838782019-03-12 15:48:16 -06002246 bool cancel = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002247 __poll_t mask;
2248 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002249
2250 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2251 return -EINVAL;
2252 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
2253 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06002254 if (!poll->file)
2255 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002256
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002257 req->sqe = NULL;
Jens Axboe561fb042019-10-24 07:25:42 -06002258 INIT_IO_WORK(&req->work, io_poll_complete_work);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002259 events = READ_ONCE(sqe->poll_events);
2260 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
Jens Axboeeac406c2019-11-14 12:09:58 -07002261 RB_CLEAR_NODE(&req->rb_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002262
Jens Axboe221c5eb2019-01-17 09:41:58 -07002263 poll->head = NULL;
Jens Axboe8c838782019-03-12 15:48:16 -06002264 poll->done = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002265 poll->canceled = false;
2266
2267 ipt.pt._qproc = io_poll_queue_proc;
2268 ipt.pt._key = poll->events;
2269 ipt.req = req;
2270 ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
2271
2272 /* initialized the list so that we can do list_empty checks */
2273 INIT_LIST_HEAD(&poll->wait.entry);
2274 init_waitqueue_func_entry(&poll->wait, io_poll_wake);
2275
Jens Axboe36703242019-07-25 10:20:18 -06002276 INIT_LIST_HEAD(&req->list);
2277
Jens Axboe221c5eb2019-01-17 09:41:58 -07002278 mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002279
2280 spin_lock_irq(&ctx->completion_lock);
Jens Axboe8c838782019-03-12 15:48:16 -06002281 if (likely(poll->head)) {
2282 spin_lock(&poll->head->lock);
2283 if (unlikely(list_empty(&poll->wait.entry))) {
2284 if (ipt.error)
2285 cancel = true;
2286 ipt.error = 0;
2287 mask = 0;
2288 }
2289 if (mask || ipt.error)
2290 list_del_init(&poll->wait.entry);
2291 else if (cancel)
2292 WRITE_ONCE(poll->canceled, true);
2293 else if (!poll->done) /* actually waiting for an event */
Jens Axboeeac406c2019-11-14 12:09:58 -07002294 io_poll_req_insert(req);
Jens Axboe8c838782019-03-12 15:48:16 -06002295 spin_unlock(&poll->head->lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002296 }
Jens Axboe8c838782019-03-12 15:48:16 -06002297 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06002298 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002299 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06002300 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07002301 spin_unlock_irq(&ctx->completion_lock);
2302
Jens Axboe8c838782019-03-12 15:48:16 -06002303 if (mask) {
2304 io_cqring_ev_posted(ctx);
Jackie Liuec9c02a2019-11-08 23:50:36 +08002305 io_put_req_find_next(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002306 }
Jens Axboe8c838782019-03-12 15:48:16 -06002307 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002308}
2309
Jens Axboe5262f562019-09-17 12:26:57 -06002310static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
2311{
Jens Axboead8a48a2019-11-15 08:49:11 -07002312 struct io_timeout_data *data = container_of(timer,
2313 struct io_timeout_data, timer);
2314 struct io_kiocb *req = data->req;
2315 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06002316 unsigned long flags;
2317
Jens Axboe5262f562019-09-17 12:26:57 -06002318 atomic_inc(&ctx->cq_timeouts);
2319
2320 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08002321 /*
Jens Axboe11365042019-10-16 09:08:32 -06002322 * We could be racing with timeout deletion. If the list is empty,
2323 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08002324 */
Jens Axboe842f9612019-10-29 12:34:10 -06002325 if (!list_empty(&req->list)) {
Jens Axboe11365042019-10-16 09:08:32 -06002326 struct io_kiocb *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06002327
Jens Axboe11365042019-10-16 09:08:32 -06002328 /*
2329 * Adjust the reqs sequence before the current one because it
2330 * will consume a slot in the cq_ring and the the cq_tail
2331 * pointer will be increased, otherwise other timeout reqs may
2332 * return in advance without waiting for enough wait_nr.
2333 */
2334 prev = req;
2335 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
2336 prev->sequence++;
Jens Axboe11365042019-10-16 09:08:32 -06002337 list_del_init(&req->list);
Jens Axboe11365042019-10-16 09:08:32 -06002338 }
Jens Axboe842f9612019-10-29 12:34:10 -06002339
Jens Axboe78e19bb2019-11-06 15:21:34 -07002340 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06002341 io_commit_cqring(ctx);
2342 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2343
2344 io_cqring_ev_posted(ctx);
Jens Axboef1f40852019-11-05 20:33:16 -07002345 if (req->flags & REQ_F_LINK)
2346 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe5262f562019-09-17 12:26:57 -06002347 io_put_req(req);
2348 return HRTIMER_NORESTART;
2349}
2350
Jens Axboe47f46762019-11-09 17:43:02 -07002351static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
2352{
2353 struct io_kiocb *req;
2354 int ret = -ENOENT;
2355
2356 list_for_each_entry(req, &ctx->timeout_list, list) {
2357 if (user_data == req->user_data) {
2358 list_del_init(&req->list);
2359 ret = 0;
2360 break;
2361 }
2362 }
2363
2364 if (ret == -ENOENT)
2365 return ret;
2366
Jens Axboead8a48a2019-11-15 08:49:11 -07002367 ret = hrtimer_try_to_cancel(&req->timeout.data->timer);
Jens Axboe47f46762019-11-09 17:43:02 -07002368 if (ret == -1)
2369 return -EALREADY;
2370
Jens Axboefba38c22019-11-18 12:27:57 -07002371 if (req->flags & REQ_F_LINK)
2372 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe47f46762019-11-09 17:43:02 -07002373 io_cqring_fill_event(req, -ECANCELED);
2374 io_put_req(req);
2375 return 0;
2376}
2377
Jens Axboe11365042019-10-16 09:08:32 -06002378/*
2379 * Remove or update an existing timeout command
2380 */
2381static int io_timeout_remove(struct io_kiocb *req,
2382 const struct io_uring_sqe *sqe)
2383{
2384 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe11365042019-10-16 09:08:32 -06002385 unsigned flags;
Jens Axboe47f46762019-11-09 17:43:02 -07002386 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06002387
2388 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2389 return -EINVAL;
2390 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
2391 return -EINVAL;
2392 flags = READ_ONCE(sqe->timeout_flags);
2393 if (flags)
2394 return -EINVAL;
2395
Jens Axboe11365042019-10-16 09:08:32 -06002396 spin_lock_irq(&ctx->completion_lock);
Jens Axboe47f46762019-11-09 17:43:02 -07002397 ret = io_timeout_cancel(ctx, READ_ONCE(sqe->addr));
Jens Axboe11365042019-10-16 09:08:32 -06002398
Jens Axboe47f46762019-11-09 17:43:02 -07002399 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06002400 io_commit_cqring(ctx);
2401 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06002402 io_cqring_ev_posted(ctx);
Jens Axboe47f46762019-11-09 17:43:02 -07002403 if (ret < 0 && req->flags & REQ_F_LINK)
2404 req->flags |= REQ_F_FAIL_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08002405 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06002406 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06002407}
2408
Jens Axboead8a48a2019-11-15 08:49:11 -07002409static int io_timeout_setup(struct io_kiocb *req)
Jens Axboe5262f562019-09-17 12:26:57 -06002410{
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002411 const struct io_uring_sqe *sqe = req->sqe;
Jens Axboead8a48a2019-11-15 08:49:11 -07002412 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06002413 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06002414
Jens Axboead8a48a2019-11-15 08:49:11 -07002415 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06002416 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07002417 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06002418 return -EINVAL;
2419 flags = READ_ONCE(sqe->timeout_flags);
2420 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06002421 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06002422
Jens Axboead8a48a2019-11-15 08:49:11 -07002423 data = kzalloc(sizeof(struct io_timeout_data), GFP_KERNEL);
2424 if (!data)
2425 return -ENOMEM;
2426 data->req = req;
2427 req->timeout.data = data;
2428 req->flags |= REQ_F_TIMEOUT;
2429
2430 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06002431 return -EFAULT;
2432
Jens Axboe11365042019-10-16 09:08:32 -06002433 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07002434 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06002435 else
Jens Axboead8a48a2019-11-15 08:49:11 -07002436 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06002437
Jens Axboead8a48a2019-11-15 08:49:11 -07002438 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
2439 return 0;
2440}
2441
2442static int io_timeout(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2443{
2444 unsigned count;
2445 struct io_ring_ctx *ctx = req->ctx;
2446 struct io_timeout_data *data;
2447 struct list_head *entry;
2448 unsigned span = 0;
2449 int ret;
2450
2451 ret = io_timeout_setup(req);
2452 /* common setup allows flags (like links) set, we don't */
2453 if (!ret && sqe->flags)
2454 ret = -EINVAL;
2455 if (ret)
2456 return ret;
Jens Axboe93bd25b2019-11-11 23:34:31 -07002457
Jens Axboe5262f562019-09-17 12:26:57 -06002458 /*
2459 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07002460 * timeout event to be satisfied. If it isn't set, then this is
2461 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06002462 */
2463 count = READ_ONCE(sqe->off);
Jens Axboe93bd25b2019-11-11 23:34:31 -07002464 if (!count) {
2465 req->flags |= REQ_F_TIMEOUT_NOSEQ;
2466 spin_lock_irq(&ctx->completion_lock);
2467 entry = ctx->timeout_list.prev;
2468 goto add;
2469 }
Jens Axboe5262f562019-09-17 12:26:57 -06002470
2471 req->sequence = ctx->cached_sq_head + count - 1;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +03002472 req->timeout.data->seq_offset = count;
Jens Axboe5262f562019-09-17 12:26:57 -06002473
2474 /*
2475 * Insertion sort, ensuring the first entry in the list is always
2476 * the one we need first.
2477 */
Jens Axboe5262f562019-09-17 12:26:57 -06002478 spin_lock_irq(&ctx->completion_lock);
2479 list_for_each_prev(entry, &ctx->timeout_list) {
2480 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
yangerkun5da0fb12019-10-15 21:59:29 +08002481 unsigned nxt_sq_head;
2482 long long tmp, tmp_nxt;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +03002483 u32 nxt_offset = nxt->timeout.data->seq_offset;
Jens Axboe5262f562019-09-17 12:26:57 -06002484
Jens Axboe93bd25b2019-11-11 23:34:31 -07002485 if (nxt->flags & REQ_F_TIMEOUT_NOSEQ)
2486 continue;
2487
yangerkun5da0fb12019-10-15 21:59:29 +08002488 /*
2489 * Since cached_sq_head + count - 1 can overflow, use type long
2490 * long to store it.
2491 */
2492 tmp = (long long)ctx->cached_sq_head + count - 1;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +03002493 nxt_sq_head = nxt->sequence - nxt_offset + 1;
2494 tmp_nxt = (long long)nxt_sq_head + nxt_offset - 1;
yangerkun5da0fb12019-10-15 21:59:29 +08002495
2496 /*
2497 * cached_sq_head may overflow, and it will never overflow twice
2498 * once there is some timeout req still be valid.
2499 */
2500 if (ctx->cached_sq_head < nxt_sq_head)
yangerkun8b07a652019-10-17 12:12:35 +08002501 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08002502
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002503 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06002504 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002505
2506 /*
2507 * Sequence of reqs after the insert one and itself should
2508 * be adjusted because each timeout req consumes a slot.
2509 */
2510 span++;
2511 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06002512 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002513 req->sequence -= span;
Jens Axboe93bd25b2019-11-11 23:34:31 -07002514add:
Jens Axboe5262f562019-09-17 12:26:57 -06002515 list_add(&req->list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07002516 data = req->timeout.data;
2517 data->timer.function = io_timeout_fn;
2518 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06002519 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06002520 return 0;
2521}
2522
Jens Axboe62755e32019-10-28 21:49:21 -06002523static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06002524{
Jens Axboe62755e32019-10-28 21:49:21 -06002525 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06002526
Jens Axboe62755e32019-10-28 21:49:21 -06002527 return req->user_data == (unsigned long) data;
2528}
2529
Jens Axboee977d6d2019-11-05 12:39:45 -07002530static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06002531{
Jens Axboe62755e32019-10-28 21:49:21 -06002532 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06002533 int ret = 0;
2534
Jens Axboe62755e32019-10-28 21:49:21 -06002535 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
2536 switch (cancel_ret) {
2537 case IO_WQ_CANCEL_OK:
2538 ret = 0;
2539 break;
2540 case IO_WQ_CANCEL_RUNNING:
2541 ret = -EALREADY;
2542 break;
2543 case IO_WQ_CANCEL_NOTFOUND:
2544 ret = -ENOENT;
2545 break;
2546 }
2547
Jens Axboee977d6d2019-11-05 12:39:45 -07002548 return ret;
2549}
2550
Jens Axboe47f46762019-11-09 17:43:02 -07002551static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
2552 struct io_kiocb *req, __u64 sqe_addr,
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002553 struct io_kiocb **nxt, int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07002554{
2555 unsigned long flags;
2556 int ret;
2557
2558 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
2559 if (ret != -ENOENT) {
2560 spin_lock_irqsave(&ctx->completion_lock, flags);
2561 goto done;
2562 }
2563
2564 spin_lock_irqsave(&ctx->completion_lock, flags);
2565 ret = io_timeout_cancel(ctx, sqe_addr);
2566 if (ret != -ENOENT)
2567 goto done;
2568 ret = io_poll_cancel(ctx, sqe_addr);
2569done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002570 if (!ret)
2571 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07002572 io_cqring_fill_event(req, ret);
2573 io_commit_cqring(ctx);
2574 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2575 io_cqring_ev_posted(ctx);
2576
2577 if (ret < 0 && (req->flags & REQ_F_LINK))
2578 req->flags |= REQ_F_FAIL_LINK;
2579 io_put_req_find_next(req, nxt);
2580}
2581
Jens Axboee977d6d2019-11-05 12:39:45 -07002582static int io_async_cancel(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2583 struct io_kiocb **nxt)
2584{
2585 struct io_ring_ctx *ctx = req->ctx;
Jens Axboee977d6d2019-11-05 12:39:45 -07002586
2587 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2588 return -EINVAL;
2589 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
2590 sqe->cancel_flags)
2591 return -EINVAL;
2592
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002593 io_async_find_and_cancel(ctx, req, READ_ONCE(sqe->addr), nxt, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06002594 return 0;
2595}
2596
Jackie Liua197f662019-11-08 08:09:12 -07002597static int io_req_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -06002598{
2599 struct io_uring_sqe *sqe_copy;
Jackie Liua197f662019-11-08 08:09:12 -07002600 struct io_ring_ctx *ctx = req->ctx;
Jens Axboede0617e2019-04-06 21:51:27 -06002601
Bob Liu9d858b22019-11-13 18:06:25 +08002602 /* Still need defer if there is pending req in defer list. */
2603 if (!req_need_defer(req) && list_empty(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06002604 return 0;
2605
2606 sqe_copy = kmalloc(sizeof(*sqe_copy), GFP_KERNEL);
2607 if (!sqe_copy)
2608 return -EAGAIN;
2609
2610 spin_lock_irq(&ctx->completion_lock);
Bob Liu9d858b22019-11-13 18:06:25 +08002611 if (!req_need_defer(req) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06002612 spin_unlock_irq(&ctx->completion_lock);
2613 kfree(sqe_copy);
2614 return 0;
2615 }
2616
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002617 memcpy(sqe_copy, req->sqe, sizeof(*sqe_copy));
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03002618 req->flags |= REQ_F_FREE_SQE;
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002619 req->sqe = sqe_copy;
Jens Axboede0617e2019-04-06 21:51:27 -06002620
Jens Axboe915967f2019-11-21 09:01:20 -07002621 trace_io_uring_defer(ctx, req, req->user_data);
Jens Axboede0617e2019-04-06 21:51:27 -06002622 list_add_tail(&req->list, &ctx->defer_list);
2623 spin_unlock_irq(&ctx->completion_lock);
2624 return -EIOCBQUEUED;
2625}
2626
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03002627__attribute__((nonnull))
Pavel Begunkovd7324472019-11-21 21:24:36 +03002628static int io_issue_sqe(struct io_kiocb *req, struct io_kiocb **nxt,
2629 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002630{
Jens Axboee0c5c572019-03-12 10:18:47 -06002631 int ret, opcode;
Jackie Liua197f662019-11-08 08:09:12 -07002632 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002633
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002634 opcode = READ_ONCE(req->sqe->opcode);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002635 switch (opcode) {
2636 case IORING_OP_NOP:
Jens Axboe78e19bb2019-11-06 15:21:34 -07002637 ret = io_nop(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002638 break;
2639 case IORING_OP_READV:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002640 if (unlikely(req->sqe->buf_index))
Jens Axboeedafcce2019-01-09 09:16:05 -07002641 return -EINVAL;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002642 ret = io_read(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002643 break;
2644 case IORING_OP_WRITEV:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002645 if (unlikely(req->sqe->buf_index))
Jens Axboeedafcce2019-01-09 09:16:05 -07002646 return -EINVAL;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002647 ret = io_write(req, nxt, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07002648 break;
2649 case IORING_OP_READ_FIXED:
Pavel Begunkov267bc902019-11-07 01:41:08 +03002650 ret = io_read(req, nxt, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07002651 break;
2652 case IORING_OP_WRITE_FIXED:
Pavel Begunkov267bc902019-11-07 01:41:08 +03002653 ret = io_write(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002654 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002655 case IORING_OP_FSYNC:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002656 ret = io_fsync(req, req->sqe, nxt, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002657 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002658 case IORING_OP_POLL_ADD:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002659 ret = io_poll_add(req, req->sqe, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002660 break;
2661 case IORING_OP_POLL_REMOVE:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002662 ret = io_poll_remove(req, req->sqe);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002663 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002664 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002665 ret = io_sync_file_range(req, req->sqe, nxt, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002666 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002667 case IORING_OP_SENDMSG:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002668 ret = io_sendmsg(req, req->sqe, nxt, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06002669 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06002670 case IORING_OP_RECVMSG:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002671 ret = io_recvmsg(req, req->sqe, nxt, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06002672 break;
Jens Axboe5262f562019-09-17 12:26:57 -06002673 case IORING_OP_TIMEOUT:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002674 ret = io_timeout(req, req->sqe);
Jens Axboe5262f562019-09-17 12:26:57 -06002675 break;
Jens Axboe11365042019-10-16 09:08:32 -06002676 case IORING_OP_TIMEOUT_REMOVE:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002677 ret = io_timeout_remove(req, req->sqe);
Jens Axboe11365042019-10-16 09:08:32 -06002678 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06002679 case IORING_OP_ACCEPT:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002680 ret = io_accept(req, req->sqe, nxt, force_nonblock);
Jens Axboe17f2fe32019-10-17 14:42:58 -06002681 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07002682 case IORING_OP_CONNECT:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002683 ret = io_connect(req, req->sqe, nxt, force_nonblock);
Jens Axboef8e85cf2019-11-23 14:24:24 -07002684 break;
Jens Axboe62755e32019-10-28 21:49:21 -06002685 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002686 ret = io_async_cancel(req, req->sqe, nxt);
Jens Axboe62755e32019-10-28 21:49:21 -06002687 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002688 default:
2689 ret = -EINVAL;
2690 break;
2691 }
2692
Jens Axboedef596e2019-01-09 08:59:42 -07002693 if (ret)
2694 return ret;
2695
2696 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe9e645e112019-05-10 16:07:28 -06002697 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07002698 return -EAGAIN;
2699
2700 /* workqueue context doesn't hold uring_lock, grab it now */
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002701 if (req->in_async)
Jens Axboedef596e2019-01-09 08:59:42 -07002702 mutex_lock(&ctx->uring_lock);
2703 io_iopoll_req_issued(req);
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002704 if (req->in_async)
Jens Axboedef596e2019-01-09 08:59:42 -07002705 mutex_unlock(&ctx->uring_lock);
2706 }
2707
2708 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002709}
2710
Jens Axboeb76da702019-11-20 13:05:32 -07002711static void io_link_work_cb(struct io_wq_work **workptr)
2712{
2713 struct io_wq_work *work = *workptr;
2714 struct io_kiocb *link = work->data;
2715
2716 io_queue_linked_timeout(link);
2717 work->func = io_wq_submit_work;
2718}
2719
Jens Axboe561fb042019-10-24 07:25:42 -06002720static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboe31b51512019-01-18 22:56:34 -07002721{
Jens Axboe561fb042019-10-24 07:25:42 -06002722 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002723 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe561fb042019-10-24 07:25:42 -06002724 struct io_kiocb *nxt = NULL;
2725 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002726
Jens Axboe561fb042019-10-24 07:25:42 -06002727 /* Ensure we clear previously set non-block flag */
2728 req->rw.ki_flags &= ~IOCB_NOWAIT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002729
Jens Axboe561fb042019-10-24 07:25:42 -06002730 if (work->flags & IO_WQ_WORK_CANCEL)
2731 ret = -ECANCELED;
Jens Axboe31b51512019-01-18 22:56:34 -07002732
Jens Axboe561fb042019-10-24 07:25:42 -06002733 if (!ret) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002734 req->has_user = (work->flags & IO_WQ_WORK_HAS_MM) != 0;
2735 req->in_async = true;
Jens Axboe561fb042019-10-24 07:25:42 -06002736 do {
Pavel Begunkovd7324472019-11-21 21:24:36 +03002737 ret = io_issue_sqe(req, &nxt, false);
Jens Axboe561fb042019-10-24 07:25:42 -06002738 /*
2739 * We can get EAGAIN for polled IO even though we're
2740 * forcing a sync submission from here, since we can't
2741 * wait for request slots on the block side.
2742 */
2743 if (ret != -EAGAIN)
2744 break;
2745 cond_resched();
2746 } while (1);
2747 }
Jens Axboe31b51512019-01-18 22:56:34 -07002748
Jens Axboe561fb042019-10-24 07:25:42 -06002749 /* drop submission reference */
Jackie Liuec9c02a2019-11-08 23:50:36 +08002750 io_put_req(req);
Jens Axboe817869d2019-04-30 14:44:05 -06002751
Jens Axboe561fb042019-10-24 07:25:42 -06002752 if (ret) {
Jens Axboef1f40852019-11-05 20:33:16 -07002753 if (req->flags & REQ_F_LINK)
2754 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07002755 io_cqring_add_event(req, ret);
Jens Axboe817869d2019-04-30 14:44:05 -06002756 io_put_req(req);
Jens Axboeedafcce2019-01-09 09:16:05 -07002757 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002758
Jens Axboe561fb042019-10-24 07:25:42 -06002759 /* if a dependent link is ready, pass it back */
2760 if (!ret && nxt) {
Jens Axboe94ae5e72019-11-14 19:39:52 -07002761 struct io_kiocb *link;
2762
2763 io_prep_async_work(nxt, &link);
Jens Axboe561fb042019-10-24 07:25:42 -06002764 *workptr = &nxt->work;
Jens Axboeb76da702019-11-20 13:05:32 -07002765 if (link) {
2766 nxt->work.flags |= IO_WQ_WORK_CB;
2767 nxt->work.func = io_link_work_cb;
2768 nxt->work.data = link;
2769 }
Jens Axboeedafcce2019-01-09 09:16:05 -07002770 }
Jens Axboe31b51512019-01-18 22:56:34 -07002771}
Jens Axboe2b188cc2019-01-07 10:46:33 -07002772
Jens Axboe09bb8392019-03-13 12:39:28 -06002773static bool io_op_needs_file(const struct io_uring_sqe *sqe)
2774{
2775 int op = READ_ONCE(sqe->opcode);
2776
2777 switch (op) {
2778 case IORING_OP_NOP:
2779 case IORING_OP_POLL_REMOVE:
Pavel Begunkov5683e542019-11-14 00:59:19 +03002780 case IORING_OP_TIMEOUT:
Pavel Begunkova320e9f2019-11-14 00:11:01 +03002781 case IORING_OP_TIMEOUT_REMOVE:
2782 case IORING_OP_ASYNC_CANCEL:
2783 case IORING_OP_LINK_TIMEOUT:
Jens Axboe09bb8392019-03-13 12:39:28 -06002784 return false;
2785 default:
2786 return true;
2787 }
2788}
2789
Jens Axboe65e19f52019-10-26 07:20:21 -06002790static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
2791 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06002792{
Jens Axboe65e19f52019-10-26 07:20:21 -06002793 struct fixed_file_table *table;
2794
2795 table = &ctx->file_table[index >> IORING_FILE_TABLE_SHIFT];
2796 return table->files[index & IORING_FILE_TABLE_MASK];
2797}
2798
Jackie Liua197f662019-11-08 08:09:12 -07002799static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req)
Jens Axboe09bb8392019-03-13 12:39:28 -06002800{
Jackie Liua197f662019-11-08 08:09:12 -07002801 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe09bb8392019-03-13 12:39:28 -06002802 unsigned flags;
2803 int fd;
2804
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002805 flags = READ_ONCE(req->sqe->flags);
2806 fd = READ_ONCE(req->sqe->fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06002807
Jackie Liu4fe2c962019-09-09 20:50:40 +08002808 if (flags & IOSQE_IO_DRAIN)
Jens Axboede0617e2019-04-06 21:51:27 -06002809 req->flags |= REQ_F_IO_DRAIN;
Jens Axboede0617e2019-04-06 21:51:27 -06002810
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002811 if (!io_op_needs_file(req->sqe))
Jens Axboe09bb8392019-03-13 12:39:28 -06002812 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06002813
2814 if (flags & IOSQE_FIXED_FILE) {
Jens Axboe65e19f52019-10-26 07:20:21 -06002815 if (unlikely(!ctx->file_table ||
Jens Axboe09bb8392019-03-13 12:39:28 -06002816 (unsigned) fd >= ctx->nr_user_files))
2817 return -EBADF;
Jens Axboeb7620122019-10-26 07:22:55 -06002818 fd = array_index_nospec(fd, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06002819 req->file = io_file_from_index(ctx, fd);
2820 if (!req->file)
Jens Axboe08a45172019-10-03 08:11:03 -06002821 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06002822 req->flags |= REQ_F_FIXED_FILE;
2823 } else {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002824 if (req->needs_fixed_file)
Jens Axboe09bb8392019-03-13 12:39:28 -06002825 return -EBADF;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002826 trace_io_uring_file_get(ctx, fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06002827 req->file = io_file_get(state, fd);
2828 if (unlikely(!req->file))
2829 return -EBADF;
2830 }
2831
2832 return 0;
2833}
2834
Jackie Liua197f662019-11-08 08:09:12 -07002835static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002836{
Jens Axboefcb323c2019-10-24 12:39:47 -06002837 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07002838 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06002839
2840 rcu_read_lock();
2841 spin_lock_irq(&ctx->inflight_lock);
2842 /*
2843 * We use the f_ops->flush() handler to ensure that we can flush
2844 * out work accessing these files if the fd is closed. Check if
2845 * the fd has changed since we started down this path, and disallow
2846 * this operation if it has.
2847 */
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002848 if (fcheck(req->ring_fd) == req->ring_file) {
Jens Axboefcb323c2019-10-24 12:39:47 -06002849 list_add(&req->inflight_entry, &ctx->inflight_list);
2850 req->flags |= REQ_F_INFLIGHT;
2851 req->work.files = current->files;
2852 ret = 0;
2853 }
2854 spin_unlock_irq(&ctx->inflight_lock);
2855 rcu_read_unlock();
2856
2857 return ret;
2858}
2859
Jens Axboe2665abf2019-11-05 12:40:47 -07002860static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
2861{
Jens Axboead8a48a2019-11-15 08:49:11 -07002862 struct io_timeout_data *data = container_of(timer,
2863 struct io_timeout_data, timer);
2864 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07002865 struct io_ring_ctx *ctx = req->ctx;
2866 struct io_kiocb *prev = NULL;
2867 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07002868
2869 spin_lock_irqsave(&ctx->completion_lock, flags);
2870
2871 /*
2872 * We don't expect the list to be empty, that will only happen if we
2873 * race with the completion of the linked work.
2874 */
2875 if (!list_empty(&req->list)) {
2876 prev = list_entry(req->list.prev, struct io_kiocb, link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07002877 if (refcount_inc_not_zero(&prev->refs)) {
Jens Axboe76a46e02019-11-10 23:34:16 -07002878 list_del_init(&req->list);
Jens Axboe5d960722019-11-19 15:31:28 -07002879 prev->flags &= ~REQ_F_LINK_TIMEOUT;
2880 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07002881 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07002882 }
2883
2884 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2885
2886 if (prev) {
Jens Axboefba38c22019-11-18 12:27:57 -07002887 if (prev->flags & REQ_F_LINK)
2888 prev->flags |= REQ_F_FAIL_LINK;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002889 io_async_find_and_cancel(ctx, req, prev->user_data, NULL,
2890 -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07002891 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07002892 } else {
2893 io_cqring_add_event(req, -ETIME);
2894 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07002895 }
Jens Axboe2665abf2019-11-05 12:40:47 -07002896 return HRTIMER_NORESTART;
2897}
2898
Jens Axboead8a48a2019-11-15 08:49:11 -07002899static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07002900{
Jens Axboe76a46e02019-11-10 23:34:16 -07002901 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07002902
Jens Axboe76a46e02019-11-10 23:34:16 -07002903 /*
2904 * If the list is now empty, then our linked request finished before
2905 * we got a chance to setup the timer
2906 */
2907 spin_lock_irq(&ctx->completion_lock);
2908 if (!list_empty(&req->list)) {
Jens Axboe94ae5e72019-11-14 19:39:52 -07002909 struct io_timeout_data *data = req->timeout.data;
2910
Jens Axboead8a48a2019-11-15 08:49:11 -07002911 data->timer.function = io_link_timeout_fn;
2912 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
2913 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07002914 }
Jens Axboe76a46e02019-11-10 23:34:16 -07002915 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07002916
Jens Axboe2665abf2019-11-05 12:40:47 -07002917 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07002918 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07002919}
2920
Jens Axboead8a48a2019-11-15 08:49:11 -07002921static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07002922{
2923 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002924
Jens Axboe2665abf2019-11-05 12:40:47 -07002925 if (!(req->flags & REQ_F_LINK))
2926 return NULL;
2927
2928 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, list);
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002929 if (!nxt || nxt->sqe->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07002930 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07002931
Jens Axboe76a46e02019-11-10 23:34:16 -07002932 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07002933 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07002934}
2935
Jens Axboe0e0702d2019-11-14 21:42:10 -07002936static void __io_queue_sqe(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002937{
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03002938 struct io_kiocb *linked_timeout = io_prep_linked_timeout(req);
2939 struct io_kiocb *nxt = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002940 int ret;
2941
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03002942 ret = io_issue_sqe(req, &nxt, true);
2943 if (nxt)
2944 io_queue_async_work(nxt);
Jens Axboe491381ce2019-10-17 09:20:46 -06002945
2946 /*
2947 * We async punt it if the file wasn't marked NOWAIT, or if the file
2948 * doesn't support non-blocking read/write attempts
2949 */
2950 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
2951 (req->flags & REQ_F_MUST_PUNT))) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07002952 struct io_uring_sqe *sqe_copy;
2953
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002954 sqe_copy = kmemdup(req->sqe, sizeof(*sqe_copy), GFP_KERNEL);
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03002955 if (!sqe_copy)
2956 goto err;
Jens Axboee65ef562019-03-12 10:16:44 -06002957
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002958 req->sqe = sqe_copy;
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03002959 req->flags |= REQ_F_FREE_SQE;
2960
2961 if (req->work.flags & IO_WQ_WORK_NEEDS_FILES) {
2962 ret = io_grab_files(req);
2963 if (ret)
2964 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002965 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03002966
2967 /*
2968 * Queued up for async execution, worker will release
2969 * submit reference when the iocb is actually submitted.
2970 */
2971 io_queue_async_work(req);
2972 return;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002973 }
Jens Axboee65ef562019-03-12 10:16:44 -06002974
Jens Axboefcb323c2019-10-24 12:39:47 -06002975err:
Jens Axboee65ef562019-03-12 10:16:44 -06002976 /* drop submission reference */
2977 io_put_req(req);
2978
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03002979 if (linked_timeout) {
Jens Axboe76a46e02019-11-10 23:34:16 -07002980 if (!ret)
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03002981 io_queue_linked_timeout(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07002982 else
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03002983 io_put_req(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07002984 }
2985
Jens Axboee65ef562019-03-12 10:16:44 -06002986 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06002987 if (ret) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07002988 io_cqring_add_event(req, ret);
Jens Axboe9e645e112019-05-10 16:07:28 -06002989 if (req->flags & REQ_F_LINK)
2990 req->flags |= REQ_F_FAIL_LINK;
Jens Axboee65ef562019-03-12 10:16:44 -06002991 io_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002992 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002993}
2994
Jens Axboe0e0702d2019-11-14 21:42:10 -07002995static void io_queue_sqe(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08002996{
2997 int ret;
2998
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03002999 if (unlikely(req->ctx->drain_next)) {
3000 req->flags |= REQ_F_IO_DRAIN;
3001 req->ctx->drain_next = false;
3002 }
3003 req->ctx->drain_next = (req->flags & REQ_F_DRAIN_LINK);
3004
Jackie Liua197f662019-11-08 08:09:12 -07003005 ret = io_req_defer(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08003006 if (ret) {
3007 if (ret != -EIOCBQUEUED) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07003008 io_cqring_add_event(req, ret);
Pavel Begunkovd3b357962019-11-19 23:32:48 +03003009 if (req->flags & REQ_F_LINK)
3010 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07003011 io_double_put_req(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08003012 }
Jens Axboe0e0702d2019-11-14 21:42:10 -07003013 } else
3014 __io_queue_sqe(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08003015}
3016
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003017static inline void io_queue_link_head(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08003018{
Jens Axboe94ae5e72019-11-14 19:39:52 -07003019 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003020 io_cqring_add_event(req, -ECANCELED);
3021 io_double_put_req(req);
3022 } else
Jens Axboe0e0702d2019-11-14 21:42:10 -07003023 io_queue_sqe(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08003024}
3025
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003026
Jens Axboe9e645e112019-05-10 16:07:28 -06003027#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK)
3028
Jackie Liua197f662019-11-08 08:09:12 -07003029static void io_submit_sqe(struct io_kiocb *req, struct io_submit_state *state,
3030 struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06003031{
Jackie Liua197f662019-11-08 08:09:12 -07003032 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -06003033 int ret;
3034
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003035 req->user_data = req->sqe->user_data;
Jens Axboe78e19bb2019-11-06 15:21:34 -07003036
Jens Axboe9e645e112019-05-10 16:07:28 -06003037 /* enforce forwards compatibility on users */
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003038 if (unlikely(req->sqe->flags & ~SQE_VALID_FLAGS)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06003039 ret = -EINVAL;
Pavel Begunkov196be952019-11-07 01:41:06 +03003040 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06003041 }
3042
Jackie Liua197f662019-11-08 08:09:12 -07003043 ret = io_req_set_file(state, req);
Jens Axboe9e645e112019-05-10 16:07:28 -06003044 if (unlikely(ret)) {
3045err_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07003046 io_cqring_add_event(req, ret);
3047 io_double_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06003048 return;
3049 }
3050
Jens Axboe9e645e112019-05-10 16:07:28 -06003051 /*
3052 * If we already have a head request, queue this one for async
3053 * submittal once the head completes. If we don't have a head but
3054 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
3055 * submitted sync once the chain is complete. If none of those
3056 * conditions are true (normal request), then just queue it.
3057 */
3058 if (*link) {
3059 struct io_kiocb *prev = *link;
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03003060 struct io_uring_sqe *sqe_copy;
Jens Axboe9e645e112019-05-10 16:07:28 -06003061
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003062 if (req->sqe->flags & IOSQE_IO_DRAIN)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003063 (*link)->flags |= REQ_F_DRAIN_LINK | REQ_F_IO_DRAIN;
3064
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003065 if (READ_ONCE(req->sqe->opcode) == IORING_OP_LINK_TIMEOUT) {
Jens Axboe94ae5e72019-11-14 19:39:52 -07003066 ret = io_timeout_setup(req);
3067 /* common setup allows offset being set, we don't */
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003068 if (!ret && req->sqe->off)
Jens Axboe94ae5e72019-11-14 19:39:52 -07003069 ret = -EINVAL;
3070 if (ret) {
3071 prev->flags |= REQ_F_FAIL_LINK;
3072 goto err_req;
3073 }
3074 }
3075
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003076 sqe_copy = kmemdup(req->sqe, sizeof(*sqe_copy), GFP_KERNEL);
Jens Axboe9e645e112019-05-10 16:07:28 -06003077 if (!sqe_copy) {
3078 ret = -EAGAIN;
3079 goto err_req;
3080 }
3081
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003082 req->sqe = sqe_copy;
Jens Axboe94ae5e72019-11-14 19:39:52 -07003083 req->flags |= REQ_F_FREE_SQE;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02003084 trace_io_uring_link(ctx, req, prev);
Jens Axboe9e645e112019-05-10 16:07:28 -06003085 list_add_tail(&req->list, &prev->link_list);
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003086 } else if (req->sqe->flags & IOSQE_IO_LINK) {
Jens Axboe9e645e112019-05-10 16:07:28 -06003087 req->flags |= REQ_F_LINK;
3088
Jens Axboe9e645e112019-05-10 16:07:28 -06003089 INIT_LIST_HEAD(&req->link_list);
3090 *link = req;
3091 } else {
Jackie Liua197f662019-11-08 08:09:12 -07003092 io_queue_sqe(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06003093 }
3094}
3095
Jens Axboe9a56a232019-01-09 09:06:50 -07003096/*
3097 * Batched submission is done, ensure local IO is flushed out.
3098 */
3099static void io_submit_state_end(struct io_submit_state *state)
3100{
3101 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06003102 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07003103 if (state->free_reqs)
3104 kmem_cache_free_bulk(req_cachep, state->free_reqs,
3105 &state->reqs[state->cur_req]);
Jens Axboe9a56a232019-01-09 09:06:50 -07003106}
3107
3108/*
3109 * Start submission side cache.
3110 */
3111static void io_submit_state_start(struct io_submit_state *state,
3112 struct io_ring_ctx *ctx, unsigned max_ios)
3113{
3114 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07003115 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07003116 state->file = NULL;
3117 state->ios_left = max_ios;
3118}
3119
Jens Axboe2b188cc2019-01-07 10:46:33 -07003120static void io_commit_sqring(struct io_ring_ctx *ctx)
3121{
Hristo Venev75b28af2019-08-26 17:23:46 +00003122 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003123
Hristo Venev75b28af2019-08-26 17:23:46 +00003124 if (ctx->cached_sq_head != READ_ONCE(rings->sq.head)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07003125 /*
3126 * Ensure any loads from the SQEs are done at this point,
3127 * since once we write the new head, the application could
3128 * write new data to them.
3129 */
Hristo Venev75b28af2019-08-26 17:23:46 +00003130 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003131 }
3132}
3133
3134/*
Jens Axboe2b188cc2019-01-07 10:46:33 -07003135 * Fetch an sqe, if one is available. Note that s->sqe will point to memory
3136 * that is mapped by userspace. This means that care needs to be taken to
3137 * ensure that reads are stable, as we cannot rely on userspace always
3138 * being a good citizen. If members of the sqe are validated and then later
3139 * used, it's important that those reads are done through READ_ONCE() to
3140 * prevent a re-load down the line.
3141 */
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003142static bool io_get_sqring(struct io_ring_ctx *ctx, struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003143{
Hristo Venev75b28af2019-08-26 17:23:46 +00003144 struct io_rings *rings = ctx->rings;
3145 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003146 unsigned head;
3147
3148 /*
3149 * The cached sq head (or cq tail) serves two purposes:
3150 *
3151 * 1) allows us to batch the cost of updating the user visible
3152 * head updates.
3153 * 2) allows the kernel side to track the head on its own, even
3154 * though the application is the one updating it.
3155 */
3156 head = ctx->cached_sq_head;
Stefan Bühlere523a292019-04-19 11:57:44 +02003157 /* make sure SQ entry isn't read before tail */
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03003158 if (unlikely(head == smp_load_acquire(&rings->sq.tail)))
Jens Axboe2b188cc2019-01-07 10:46:33 -07003159 return false;
3160
Hristo Venev75b28af2019-08-26 17:23:46 +00003161 head = READ_ONCE(sq_array[head & ctx->sq_mask]);
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03003162 if (likely(head < ctx->sq_entries)) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003163 /*
3164 * All io need record the previous position, if LINK vs DARIN,
3165 * it can be used to mark the position of the first IO in the
3166 * link list.
3167 */
3168 req->sequence = ctx->cached_sq_head;
3169 req->sqe = &ctx->sq_sqes[head];
Jens Axboe2b188cc2019-01-07 10:46:33 -07003170 ctx->cached_sq_head++;
3171 return true;
3172 }
3173
3174 /* drop invalid entries */
3175 ctx->cached_sq_head++;
Jens Axboe498ccd92019-10-25 10:04:25 -06003176 ctx->cached_sq_dropped++;
3177 WRITE_ONCE(rings->sq_dropped, ctx->cached_sq_dropped);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003178 return false;
3179}
3180
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003181static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkovae9428c2019-11-06 00:22:14 +03003182 struct file *ring_file, int ring_fd,
3183 struct mm_struct **mm, bool async)
Jens Axboe6c271ce2019-01-10 11:22:30 -07003184{
3185 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06003186 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06003187 int i, submitted = 0;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03003188 bool mm_fault = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003189
Jens Axboec4a2ed72019-11-21 21:01:26 -07003190 /* if we have a backlog and couldn't flush it all, return BUSY */
3191 if (!list_empty(&ctx->cq_overflow_list) &&
3192 !io_cqring_overflow_flush(ctx, false))
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003193 return -EBUSY;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003194
3195 if (nr > IO_PLUG_THRESHOLD) {
3196 io_submit_state_start(&state, ctx, nr);
3197 statep = &state;
3198 }
3199
3200 for (i = 0; i < nr; i++) {
Pavel Begunkov196be952019-11-07 01:41:06 +03003201 struct io_kiocb *req;
Pavel Begunkov50585b92019-11-07 01:41:07 +03003202 unsigned int sqe_flags;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003203
Pavel Begunkov196be952019-11-07 01:41:06 +03003204 req = io_get_req(ctx, statep);
3205 if (unlikely(!req)) {
3206 if (!submitted)
3207 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003208 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06003209 }
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003210 if (!io_get_sqring(ctx, req)) {
Pavel Begunkov196be952019-11-07 01:41:06 +03003211 __io_free_req(req);
3212 break;
3213 }
Jens Axboe9e645e112019-05-10 16:07:28 -06003214
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003215 if (io_sqe_needs_user(req->sqe) && !*mm) {
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03003216 mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm);
3217 if (!mm_fault) {
3218 use_mm(ctx->sqo_mm);
3219 *mm = ctx->sqo_mm;
3220 }
3221 }
3222
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003223 sqe_flags = req->sqe->flags;
Pavel Begunkov50585b92019-11-07 01:41:07 +03003224
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003225 req->ring_file = ring_file;
3226 req->ring_fd = ring_fd;
3227 req->has_user = *mm != NULL;
3228 req->in_async = async;
3229 req->needs_fixed_file = async;
3230 trace_io_uring_submit_sqe(ctx, req->sqe->user_data,
Pavel Begunkov50585b92019-11-07 01:41:07 +03003231 true, async);
Jackie Liua197f662019-11-08 08:09:12 -07003232 io_submit_sqe(req, statep, &link);
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03003233 submitted++;
Pavel Begunkove5eb6362019-11-06 00:22:15 +03003234
3235 /*
3236 * If previous wasn't linked and we have a linked command,
3237 * that's the end of the chain. Submit the previous link.
3238 */
Pavel Begunkov50585b92019-11-07 01:41:07 +03003239 if (!(sqe_flags & IOSQE_IO_LINK) && link) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003240 io_queue_link_head(link);
Pavel Begunkove5eb6362019-11-06 00:22:15 +03003241 link = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003242 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07003243 }
3244
Jens Axboe9e645e112019-05-10 16:07:28 -06003245 if (link)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003246 io_queue_link_head(link);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003247 if (statep)
3248 io_submit_state_end(&state);
3249
Pavel Begunkovae9428c2019-11-06 00:22:14 +03003250 /* Commit SQ ring head once we've consumed and submitted all SQEs */
3251 io_commit_sqring(ctx);
3252
Jens Axboe6c271ce2019-01-10 11:22:30 -07003253 return submitted;
3254}
3255
3256static int io_sq_thread(void *data)
3257{
Jens Axboe6c271ce2019-01-10 11:22:30 -07003258 struct io_ring_ctx *ctx = data;
3259 struct mm_struct *cur_mm = NULL;
Jens Axboe181e4482019-11-25 08:52:30 -07003260 const struct cred *old_cred;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003261 mm_segment_t old_fs;
3262 DEFINE_WAIT(wait);
3263 unsigned inflight;
3264 unsigned long timeout;
Jens Axboec1edbf52019-11-10 16:56:04 -07003265 int ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003266
Jens Axboe206aefd2019-11-07 18:27:42 -07003267 complete(&ctx->completions[1]);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08003268
Jens Axboe6c271ce2019-01-10 11:22:30 -07003269 old_fs = get_fs();
3270 set_fs(USER_DS);
Jens Axboe181e4482019-11-25 08:52:30 -07003271 old_cred = override_creds(ctx->creds);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003272
Jens Axboec1edbf52019-11-10 16:56:04 -07003273 ret = timeout = inflight = 0;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003274 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003275 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003276
3277 if (inflight) {
3278 unsigned nr_events = 0;
3279
3280 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe2b2ed972019-10-25 10:06:15 -06003281 /*
3282 * inflight is the count of the maximum possible
3283 * entries we submitted, but it can be smaller
3284 * if we dropped some of them. If we don't have
3285 * poll entries available, then we know that we
3286 * have nothing left to poll for. Reset the
3287 * inflight count to zero in that case.
3288 */
3289 mutex_lock(&ctx->uring_lock);
3290 if (!list_empty(&ctx->poll_list))
3291 __io_iopoll_check(ctx, &nr_events, 0);
3292 else
3293 inflight = 0;
3294 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003295 } else {
3296 /*
3297 * Normal IO, just pretend everything completed.
3298 * We don't have to poll completions for that.
3299 */
3300 nr_events = inflight;
3301 }
3302
3303 inflight -= nr_events;
3304 if (!inflight)
3305 timeout = jiffies + ctx->sq_thread_idle;
3306 }
3307
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003308 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07003309
3310 /*
3311 * If submit got -EBUSY, flag us as needing the application
3312 * to enter the kernel to reap and flush events.
3313 */
3314 if (!to_submit || ret == -EBUSY) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07003315 /*
3316 * We're polling. If we're within the defined idle
3317 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07003318 * to sleep. The exception is if we got EBUSY doing
3319 * more IO, we should wait for the application to
3320 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07003321 */
Jens Axboec1edbf52019-11-10 16:56:04 -07003322 if (inflight ||
3323 (!time_after(jiffies, timeout) && ret != -EBUSY)) {
Jens Axboe9831a902019-09-19 09:48:55 -06003324 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07003325 continue;
3326 }
3327
3328 /*
3329 * Drop cur_mm before scheduling, we can't hold it for
3330 * long periods (or over schedule()). Do this before
3331 * adding ourselves to the waitqueue, as the unuse/drop
3332 * may sleep.
3333 */
3334 if (cur_mm) {
3335 unuse_mm(cur_mm);
3336 mmput(cur_mm);
3337 cur_mm = NULL;
3338 }
3339
3340 prepare_to_wait(&ctx->sqo_wait, &wait,
3341 TASK_INTERRUPTIBLE);
3342
3343 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00003344 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02003345 /* make sure to read SQ tail after writing flags */
3346 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07003347
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003348 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07003349 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003350 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07003351 finish_wait(&ctx->sqo_wait, &wait);
3352 break;
3353 }
3354 if (signal_pending(current))
3355 flush_signals(current);
3356 schedule();
3357 finish_wait(&ctx->sqo_wait, &wait);
3358
Hristo Venev75b28af2019-08-26 17:23:46 +00003359 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003360 continue;
3361 }
3362 finish_wait(&ctx->sqo_wait, &wait);
3363
Hristo Venev75b28af2019-08-26 17:23:46 +00003364 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003365 }
3366
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003367 to_submit = min(to_submit, ctx->sq_entries);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003368 ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true);
3369 if (ret > 0)
3370 inflight += ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003371 }
3372
3373 set_fs(old_fs);
3374 if (cur_mm) {
3375 unuse_mm(cur_mm);
3376 mmput(cur_mm);
3377 }
Jens Axboe181e4482019-11-25 08:52:30 -07003378 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06003379
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003380 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06003381
Jens Axboe6c271ce2019-01-10 11:22:30 -07003382 return 0;
3383}
3384
Jens Axboebda52162019-09-24 13:47:15 -06003385struct io_wait_queue {
3386 struct wait_queue_entry wq;
3387 struct io_ring_ctx *ctx;
3388 unsigned to_wait;
3389 unsigned nr_timeouts;
3390};
3391
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003392static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06003393{
3394 struct io_ring_ctx *ctx = iowq->ctx;
3395
3396 /*
3397 * Wake up if we have enough events, or if a timeout occured since we
3398 * started waiting. For timeouts, we always want to return to userspace,
3399 * regardless of event count.
3400 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003401 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06003402 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
3403}
3404
3405static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
3406 int wake_flags, void *key)
3407{
3408 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
3409 wq);
3410
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003411 /* use noflush == true, as we can't safely rely on locking context */
3412 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06003413 return -1;
3414
3415 return autoremove_wake_function(curr, mode, wake_flags, key);
3416}
3417
Jens Axboe2b188cc2019-01-07 10:46:33 -07003418/*
3419 * Wait until events become available, if we don't already have some. The
3420 * application must reap them itself, as they reside on the shared cq ring.
3421 */
3422static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
3423 const sigset_t __user *sig, size_t sigsz)
3424{
Jens Axboebda52162019-09-24 13:47:15 -06003425 struct io_wait_queue iowq = {
3426 .wq = {
3427 .private = current,
3428 .func = io_wake_function,
3429 .entry = LIST_HEAD_INIT(iowq.wq.entry),
3430 },
3431 .ctx = ctx,
3432 .to_wait = min_events,
3433 };
Hristo Venev75b28af2019-08-26 17:23:46 +00003434 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08003435 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003436
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003437 if (io_cqring_events(ctx, false) >= min_events)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003438 return 0;
3439
3440 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01003441#ifdef CONFIG_COMPAT
3442 if (in_compat_syscall())
3443 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07003444 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01003445 else
3446#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07003447 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01003448
Jens Axboe2b188cc2019-01-07 10:46:33 -07003449 if (ret)
3450 return ret;
3451 }
3452
Jens Axboebda52162019-09-24 13:47:15 -06003453 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02003454 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06003455 do {
3456 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
3457 TASK_INTERRUPTIBLE);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003458 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06003459 break;
3460 schedule();
3461 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08003462 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06003463 break;
3464 }
3465 } while (1);
3466 finish_wait(&ctx->wait, &iowq.wq);
3467
Jackie Liue9ffa5c2019-10-29 11:16:42 +08003468 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003469
Hristo Venev75b28af2019-08-26 17:23:46 +00003470 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003471}
3472
Jens Axboe6b063142019-01-10 22:13:58 -07003473static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
3474{
3475#if defined(CONFIG_UNIX)
3476 if (ctx->ring_sock) {
3477 struct sock *sock = ctx->ring_sock->sk;
3478 struct sk_buff *skb;
3479
3480 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
3481 kfree_skb(skb);
3482 }
3483#else
3484 int i;
3485
Jens Axboe65e19f52019-10-26 07:20:21 -06003486 for (i = 0; i < ctx->nr_user_files; i++) {
3487 struct file *file;
3488
3489 file = io_file_from_index(ctx, i);
3490 if (file)
3491 fput(file);
3492 }
Jens Axboe6b063142019-01-10 22:13:58 -07003493#endif
3494}
3495
3496static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
3497{
Jens Axboe65e19f52019-10-26 07:20:21 -06003498 unsigned nr_tables, i;
3499
3500 if (!ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003501 return -ENXIO;
3502
3503 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06003504 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
3505 for (i = 0; i < nr_tables; i++)
3506 kfree(ctx->file_table[i].files);
3507 kfree(ctx->file_table);
3508 ctx->file_table = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003509 ctx->nr_user_files = 0;
3510 return 0;
3511}
3512
Jens Axboe6c271ce2019-01-10 11:22:30 -07003513static void io_sq_thread_stop(struct io_ring_ctx *ctx)
3514{
3515 if (ctx->sqo_thread) {
Jens Axboe206aefd2019-11-07 18:27:42 -07003516 wait_for_completion(&ctx->completions[1]);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003517 /*
3518 * The park is a bit of a work-around, without it we get
3519 * warning spews on shutdown with SQPOLL set and affinity
3520 * set to a single CPU.
3521 */
Jens Axboe06058632019-04-13 09:26:03 -06003522 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003523 kthread_stop(ctx->sqo_thread);
3524 ctx->sqo_thread = NULL;
3525 }
3526}
3527
Jens Axboe6b063142019-01-10 22:13:58 -07003528static void io_finish_async(struct io_ring_ctx *ctx)
3529{
Jens Axboe6c271ce2019-01-10 11:22:30 -07003530 io_sq_thread_stop(ctx);
3531
Jens Axboe561fb042019-10-24 07:25:42 -06003532 if (ctx->io_wq) {
3533 io_wq_destroy(ctx->io_wq);
3534 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003535 }
3536}
3537
3538#if defined(CONFIG_UNIX)
3539static void io_destruct_skb(struct sk_buff *skb)
3540{
3541 struct io_ring_ctx *ctx = skb->sk->sk_user_data;
3542
Jens Axboe561fb042019-10-24 07:25:42 -06003543 if (ctx->io_wq)
3544 io_wq_flush(ctx->io_wq);
Jens Axboe8a997342019-10-09 14:40:13 -06003545
Jens Axboe6b063142019-01-10 22:13:58 -07003546 unix_destruct_scm(skb);
3547}
3548
3549/*
3550 * Ensure the UNIX gc is aware of our file set, so we are certain that
3551 * the io_uring can be safely unregistered on process exit, even if we have
3552 * loops in the file referencing.
3553 */
3554static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
3555{
3556 struct sock *sk = ctx->ring_sock->sk;
3557 struct scm_fp_list *fpl;
3558 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06003559 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07003560
3561 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
3562 unsigned long inflight = ctx->user->unix_inflight + nr;
3563
3564 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
3565 return -EMFILE;
3566 }
3567
3568 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
3569 if (!fpl)
3570 return -ENOMEM;
3571
3572 skb = alloc_skb(0, GFP_KERNEL);
3573 if (!skb) {
3574 kfree(fpl);
3575 return -ENOMEM;
3576 }
3577
3578 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07003579
Jens Axboe08a45172019-10-03 08:11:03 -06003580 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07003581 fpl->user = get_uid(ctx->user);
3582 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003583 struct file *file = io_file_from_index(ctx, i + offset);
3584
3585 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06003586 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06003587 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06003588 unix_inflight(fpl->user, fpl->fp[nr_files]);
3589 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07003590 }
3591
Jens Axboe08a45172019-10-03 08:11:03 -06003592 if (nr_files) {
3593 fpl->max = SCM_MAX_FD;
3594 fpl->count = nr_files;
3595 UNIXCB(skb).fp = fpl;
3596 skb->destructor = io_destruct_skb;
3597 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
3598 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07003599
Jens Axboe08a45172019-10-03 08:11:03 -06003600 for (i = 0; i < nr_files; i++)
3601 fput(fpl->fp[i]);
3602 } else {
3603 kfree_skb(skb);
3604 kfree(fpl);
3605 }
Jens Axboe6b063142019-01-10 22:13:58 -07003606
3607 return 0;
3608}
3609
3610/*
3611 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
3612 * causes regular reference counting to break down. We rely on the UNIX
3613 * garbage collection to take care of this problem for us.
3614 */
3615static int io_sqe_files_scm(struct io_ring_ctx *ctx)
3616{
3617 unsigned left, total;
3618 int ret = 0;
3619
3620 total = 0;
3621 left = ctx->nr_user_files;
3622 while (left) {
3623 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07003624
3625 ret = __io_sqe_files_scm(ctx, this_files, total);
3626 if (ret)
3627 break;
3628 left -= this_files;
3629 total += this_files;
3630 }
3631
3632 if (!ret)
3633 return 0;
3634
3635 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003636 struct file *file = io_file_from_index(ctx, total);
3637
3638 if (file)
3639 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07003640 total++;
3641 }
3642
3643 return ret;
3644}
3645#else
3646static int io_sqe_files_scm(struct io_ring_ctx *ctx)
3647{
3648 return 0;
3649}
3650#endif
3651
Jens Axboe65e19f52019-10-26 07:20:21 -06003652static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
3653 unsigned nr_files)
3654{
3655 int i;
3656
3657 for (i = 0; i < nr_tables; i++) {
3658 struct fixed_file_table *table = &ctx->file_table[i];
3659 unsigned this_files;
3660
3661 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
3662 table->files = kcalloc(this_files, sizeof(struct file *),
3663 GFP_KERNEL);
3664 if (!table->files)
3665 break;
3666 nr_files -= this_files;
3667 }
3668
3669 if (i == nr_tables)
3670 return 0;
3671
3672 for (i = 0; i < nr_tables; i++) {
3673 struct fixed_file_table *table = &ctx->file_table[i];
3674 kfree(table->files);
3675 }
3676 return 1;
3677}
3678
Jens Axboe6b063142019-01-10 22:13:58 -07003679static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
3680 unsigned nr_args)
3681{
3682 __s32 __user *fds = (__s32 __user *) arg;
Jens Axboe65e19f52019-10-26 07:20:21 -06003683 unsigned nr_tables;
Jens Axboe6b063142019-01-10 22:13:58 -07003684 int fd, ret = 0;
3685 unsigned i;
3686
Jens Axboe65e19f52019-10-26 07:20:21 -06003687 if (ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003688 return -EBUSY;
3689 if (!nr_args)
3690 return -EINVAL;
3691 if (nr_args > IORING_MAX_FIXED_FILES)
3692 return -EMFILE;
3693
Jens Axboe65e19f52019-10-26 07:20:21 -06003694 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
3695 ctx->file_table = kcalloc(nr_tables, sizeof(struct fixed_file_table),
3696 GFP_KERNEL);
3697 if (!ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003698 return -ENOMEM;
3699
Jens Axboe65e19f52019-10-26 07:20:21 -06003700 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
3701 kfree(ctx->file_table);
Jens Axboe46568e92019-11-10 08:40:53 -07003702 ctx->file_table = NULL;
Jens Axboe65e19f52019-10-26 07:20:21 -06003703 return -ENOMEM;
3704 }
3705
Jens Axboe08a45172019-10-03 08:11:03 -06003706 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003707 struct fixed_file_table *table;
3708 unsigned index;
3709
Jens Axboe6b063142019-01-10 22:13:58 -07003710 ret = -EFAULT;
3711 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
3712 break;
Jens Axboe08a45172019-10-03 08:11:03 -06003713 /* allow sparse sets */
3714 if (fd == -1) {
3715 ret = 0;
3716 continue;
3717 }
Jens Axboe6b063142019-01-10 22:13:58 -07003718
Jens Axboe65e19f52019-10-26 07:20:21 -06003719 table = &ctx->file_table[i >> IORING_FILE_TABLE_SHIFT];
3720 index = i & IORING_FILE_TABLE_MASK;
3721 table->files[index] = fget(fd);
Jens Axboe6b063142019-01-10 22:13:58 -07003722
3723 ret = -EBADF;
Jens Axboe65e19f52019-10-26 07:20:21 -06003724 if (!table->files[index])
Jens Axboe6b063142019-01-10 22:13:58 -07003725 break;
3726 /*
3727 * Don't allow io_uring instances to be registered. If UNIX
3728 * isn't enabled, then this causes a reference cycle and this
3729 * instance can never get freed. If UNIX is enabled we'll
3730 * handle it just fine, but there's still no point in allowing
3731 * a ring fd as it doesn't support regular read/write anyway.
3732 */
Jens Axboe65e19f52019-10-26 07:20:21 -06003733 if (table->files[index]->f_op == &io_uring_fops) {
3734 fput(table->files[index]);
Jens Axboe6b063142019-01-10 22:13:58 -07003735 break;
3736 }
Jens Axboe6b063142019-01-10 22:13:58 -07003737 ret = 0;
3738 }
3739
3740 if (ret) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003741 for (i = 0; i < ctx->nr_user_files; i++) {
3742 struct file *file;
Jens Axboe6b063142019-01-10 22:13:58 -07003743
Jens Axboe65e19f52019-10-26 07:20:21 -06003744 file = io_file_from_index(ctx, i);
3745 if (file)
3746 fput(file);
3747 }
3748 for (i = 0; i < nr_tables; i++)
3749 kfree(ctx->file_table[i].files);
3750
3751 kfree(ctx->file_table);
3752 ctx->file_table = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003753 ctx->nr_user_files = 0;
3754 return ret;
3755 }
3756
3757 ret = io_sqe_files_scm(ctx);
3758 if (ret)
3759 io_sqe_files_unregister(ctx);
3760
3761 return ret;
3762}
3763
Jens Axboec3a31e62019-10-03 13:59:56 -06003764static void io_sqe_file_unregister(struct io_ring_ctx *ctx, int index)
3765{
3766#if defined(CONFIG_UNIX)
Jens Axboe65e19f52019-10-26 07:20:21 -06003767 struct file *file = io_file_from_index(ctx, index);
Jens Axboec3a31e62019-10-03 13:59:56 -06003768 struct sock *sock = ctx->ring_sock->sk;
3769 struct sk_buff_head list, *head = &sock->sk_receive_queue;
3770 struct sk_buff *skb;
3771 int i;
3772
3773 __skb_queue_head_init(&list);
3774
3775 /*
3776 * Find the skb that holds this file in its SCM_RIGHTS. When found,
3777 * remove this entry and rearrange the file array.
3778 */
3779 skb = skb_dequeue(head);
3780 while (skb) {
3781 struct scm_fp_list *fp;
3782
3783 fp = UNIXCB(skb).fp;
3784 for (i = 0; i < fp->count; i++) {
3785 int left;
3786
3787 if (fp->fp[i] != file)
3788 continue;
3789
3790 unix_notinflight(fp->user, fp->fp[i]);
3791 left = fp->count - 1 - i;
3792 if (left) {
3793 memmove(&fp->fp[i], &fp->fp[i + 1],
3794 left * sizeof(struct file *));
3795 }
3796 fp->count--;
3797 if (!fp->count) {
3798 kfree_skb(skb);
3799 skb = NULL;
3800 } else {
3801 __skb_queue_tail(&list, skb);
3802 }
3803 fput(file);
3804 file = NULL;
3805 break;
3806 }
3807
3808 if (!file)
3809 break;
3810
3811 __skb_queue_tail(&list, skb);
3812
3813 skb = skb_dequeue(head);
3814 }
3815
3816 if (skb_peek(&list)) {
3817 spin_lock_irq(&head->lock);
3818 while ((skb = __skb_dequeue(&list)) != NULL)
3819 __skb_queue_tail(head, skb);
3820 spin_unlock_irq(&head->lock);
3821 }
3822#else
Jens Axboe65e19f52019-10-26 07:20:21 -06003823 fput(io_file_from_index(ctx, index));
Jens Axboec3a31e62019-10-03 13:59:56 -06003824#endif
3825}
3826
3827static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
3828 int index)
3829{
3830#if defined(CONFIG_UNIX)
3831 struct sock *sock = ctx->ring_sock->sk;
3832 struct sk_buff_head *head = &sock->sk_receive_queue;
3833 struct sk_buff *skb;
3834
3835 /*
3836 * See if we can merge this file into an existing skb SCM_RIGHTS
3837 * file set. If there's no room, fall back to allocating a new skb
3838 * and filling it in.
3839 */
3840 spin_lock_irq(&head->lock);
3841 skb = skb_peek(head);
3842 if (skb) {
3843 struct scm_fp_list *fpl = UNIXCB(skb).fp;
3844
3845 if (fpl->count < SCM_MAX_FD) {
3846 __skb_unlink(skb, head);
3847 spin_unlock_irq(&head->lock);
3848 fpl->fp[fpl->count] = get_file(file);
3849 unix_inflight(fpl->user, fpl->fp[fpl->count]);
3850 fpl->count++;
3851 spin_lock_irq(&head->lock);
3852 __skb_queue_head(head, skb);
3853 } else {
3854 skb = NULL;
3855 }
3856 }
3857 spin_unlock_irq(&head->lock);
3858
3859 if (skb) {
3860 fput(file);
3861 return 0;
3862 }
3863
3864 return __io_sqe_files_scm(ctx, 1, index);
3865#else
3866 return 0;
3867#endif
3868}
3869
3870static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
3871 unsigned nr_args)
3872{
3873 struct io_uring_files_update up;
3874 __s32 __user *fds;
3875 int fd, i, err;
3876 __u32 done;
3877
Jens Axboe65e19f52019-10-26 07:20:21 -06003878 if (!ctx->file_table)
Jens Axboec3a31e62019-10-03 13:59:56 -06003879 return -ENXIO;
3880 if (!nr_args)
3881 return -EINVAL;
3882 if (copy_from_user(&up, arg, sizeof(up)))
3883 return -EFAULT;
3884 if (check_add_overflow(up.offset, nr_args, &done))
3885 return -EOVERFLOW;
3886 if (done > ctx->nr_user_files)
3887 return -EINVAL;
3888
3889 done = 0;
3890 fds = (__s32 __user *) up.fds;
3891 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003892 struct fixed_file_table *table;
3893 unsigned index;
3894
Jens Axboec3a31e62019-10-03 13:59:56 -06003895 err = 0;
3896 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
3897 err = -EFAULT;
3898 break;
3899 }
3900 i = array_index_nospec(up.offset, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06003901 table = &ctx->file_table[i >> IORING_FILE_TABLE_SHIFT];
3902 index = i & IORING_FILE_TABLE_MASK;
3903 if (table->files[index]) {
Jens Axboec3a31e62019-10-03 13:59:56 -06003904 io_sqe_file_unregister(ctx, i);
Jens Axboe65e19f52019-10-26 07:20:21 -06003905 table->files[index] = NULL;
Jens Axboec3a31e62019-10-03 13:59:56 -06003906 }
3907 if (fd != -1) {
3908 struct file *file;
3909
3910 file = fget(fd);
3911 if (!file) {
3912 err = -EBADF;
3913 break;
3914 }
3915 /*
3916 * Don't allow io_uring instances to be registered. If
3917 * UNIX isn't enabled, then this causes a reference
3918 * cycle and this instance can never get freed. If UNIX
3919 * is enabled we'll handle it just fine, but there's
3920 * still no point in allowing a ring fd as it doesn't
3921 * support regular read/write anyway.
3922 */
3923 if (file->f_op == &io_uring_fops) {
3924 fput(file);
3925 err = -EBADF;
3926 break;
3927 }
Jens Axboe65e19f52019-10-26 07:20:21 -06003928 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06003929 err = io_sqe_file_register(ctx, file, i);
3930 if (err)
3931 break;
3932 }
3933 nr_args--;
3934 done++;
3935 up.offset++;
3936 }
3937
3938 return done ? done : err;
3939}
3940
Jens Axboe7d723062019-11-12 22:31:31 -07003941static void io_put_work(struct io_wq_work *work)
3942{
3943 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
3944
3945 io_put_req(req);
3946}
3947
3948static void io_get_work(struct io_wq_work *work)
3949{
3950 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
3951
3952 refcount_inc(&req->refs);
3953}
3954
Jens Axboe6c271ce2019-01-10 11:22:30 -07003955static int io_sq_offload_start(struct io_ring_ctx *ctx,
3956 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003957{
Jens Axboe576a3472019-11-25 08:49:20 -07003958 struct io_wq_data data;
Jens Axboe561fb042019-10-24 07:25:42 -06003959 unsigned concurrency;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003960 int ret;
3961
Jens Axboe6c271ce2019-01-10 11:22:30 -07003962 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003963 mmgrab(current->mm);
3964 ctx->sqo_mm = current->mm;
3965
Jens Axboe6c271ce2019-01-10 11:22:30 -07003966 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06003967 ret = -EPERM;
3968 if (!capable(CAP_SYS_ADMIN))
3969 goto err;
3970
Jens Axboe917257d2019-04-13 09:28:55 -06003971 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
3972 if (!ctx->sq_thread_idle)
3973 ctx->sq_thread_idle = HZ;
3974
Jens Axboe6c271ce2019-01-10 11:22:30 -07003975 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06003976 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003977
Jens Axboe917257d2019-04-13 09:28:55 -06003978 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06003979 if (cpu >= nr_cpu_ids)
3980 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08003981 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06003982 goto err;
3983
Jens Axboe6c271ce2019-01-10 11:22:30 -07003984 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
3985 ctx, cpu,
3986 "io_uring-sq");
3987 } else {
3988 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
3989 "io_uring-sq");
3990 }
3991 if (IS_ERR(ctx->sqo_thread)) {
3992 ret = PTR_ERR(ctx->sqo_thread);
3993 ctx->sqo_thread = NULL;
3994 goto err;
3995 }
3996 wake_up_process(ctx->sqo_thread);
3997 } else if (p->flags & IORING_SETUP_SQ_AFF) {
3998 /* Can't have SQ_AFF without SQPOLL */
3999 ret = -EINVAL;
4000 goto err;
4001 }
4002
Jens Axboe576a3472019-11-25 08:49:20 -07004003 data.mm = ctx->sqo_mm;
4004 data.user = ctx->user;
Jens Axboe181e4482019-11-25 08:52:30 -07004005 data.creds = ctx->creds;
Jens Axboe576a3472019-11-25 08:49:20 -07004006 data.get_work = io_get_work;
4007 data.put_work = io_put_work;
4008
Jens Axboe561fb042019-10-24 07:25:42 -06004009 /* Do QD, or 4 * CPUS, whatever is smallest */
4010 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
Jens Axboe576a3472019-11-25 08:49:20 -07004011 ctx->io_wq = io_wq_create(concurrency, &data);
Jens Axboe975c99a52019-10-30 08:42:56 -06004012 if (IS_ERR(ctx->io_wq)) {
4013 ret = PTR_ERR(ctx->io_wq);
4014 ctx->io_wq = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004015 goto err;
4016 }
4017
4018 return 0;
4019err:
Jens Axboe54a91f32019-09-10 09:15:04 -06004020 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004021 mmdrop(ctx->sqo_mm);
4022 ctx->sqo_mm = NULL;
4023 return ret;
4024}
4025
4026static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
4027{
4028 atomic_long_sub(nr_pages, &user->locked_vm);
4029}
4030
4031static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
4032{
4033 unsigned long page_limit, cur_pages, new_pages;
4034
4035 /* Don't allow more pages than we can safely lock */
4036 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
4037
4038 do {
4039 cur_pages = atomic_long_read(&user->locked_vm);
4040 new_pages = cur_pages + nr_pages;
4041 if (new_pages > page_limit)
4042 return -ENOMEM;
4043 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
4044 new_pages) != cur_pages);
4045
4046 return 0;
4047}
4048
4049static void io_mem_free(void *ptr)
4050{
Mark Rutland52e04ef2019-04-30 17:30:21 +01004051 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004052
Mark Rutland52e04ef2019-04-30 17:30:21 +01004053 if (!ptr)
4054 return;
4055
4056 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004057 if (put_page_testzero(page))
4058 free_compound_page(page);
4059}
4060
4061static void *io_mem_alloc(size_t size)
4062{
4063 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
4064 __GFP_NORETRY;
4065
4066 return (void *) __get_free_pages(gfp_flags, get_order(size));
4067}
4068
Hristo Venev75b28af2019-08-26 17:23:46 +00004069static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
4070 size_t *sq_offset)
4071{
4072 struct io_rings *rings;
4073 size_t off, sq_array_size;
4074
4075 off = struct_size(rings, cqes, cq_entries);
4076 if (off == SIZE_MAX)
4077 return SIZE_MAX;
4078
4079#ifdef CONFIG_SMP
4080 off = ALIGN(off, SMP_CACHE_BYTES);
4081 if (off == 0)
4082 return SIZE_MAX;
4083#endif
4084
4085 sq_array_size = array_size(sizeof(u32), sq_entries);
4086 if (sq_array_size == SIZE_MAX)
4087 return SIZE_MAX;
4088
4089 if (check_add_overflow(off, sq_array_size, &off))
4090 return SIZE_MAX;
4091
4092 if (sq_offset)
4093 *sq_offset = off;
4094
4095 return off;
4096}
4097
Jens Axboe2b188cc2019-01-07 10:46:33 -07004098static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
4099{
Hristo Venev75b28af2019-08-26 17:23:46 +00004100 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004101
Hristo Venev75b28af2019-08-26 17:23:46 +00004102 pages = (size_t)1 << get_order(
4103 rings_size(sq_entries, cq_entries, NULL));
4104 pages += (size_t)1 << get_order(
4105 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07004106
Hristo Venev75b28af2019-08-26 17:23:46 +00004107 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004108}
4109
Jens Axboeedafcce2019-01-09 09:16:05 -07004110static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
4111{
4112 int i, j;
4113
4114 if (!ctx->user_bufs)
4115 return -ENXIO;
4116
4117 for (i = 0; i < ctx->nr_user_bufs; i++) {
4118 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
4119
4120 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbard27c4d3a2019-08-04 19:32:06 -07004121 put_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07004122
4123 if (ctx->account_mem)
4124 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004125 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07004126 imu->nr_bvecs = 0;
4127 }
4128
4129 kfree(ctx->user_bufs);
4130 ctx->user_bufs = NULL;
4131 ctx->nr_user_bufs = 0;
4132 return 0;
4133}
4134
4135static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
4136 void __user *arg, unsigned index)
4137{
4138 struct iovec __user *src;
4139
4140#ifdef CONFIG_COMPAT
4141 if (ctx->compat) {
4142 struct compat_iovec __user *ciovs;
4143 struct compat_iovec ciov;
4144
4145 ciovs = (struct compat_iovec __user *) arg;
4146 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
4147 return -EFAULT;
4148
4149 dst->iov_base = (void __user *) (unsigned long) ciov.iov_base;
4150 dst->iov_len = ciov.iov_len;
4151 return 0;
4152 }
4153#endif
4154 src = (struct iovec __user *) arg;
4155 if (copy_from_user(dst, &src[index], sizeof(*dst)))
4156 return -EFAULT;
4157 return 0;
4158}
4159
4160static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
4161 unsigned nr_args)
4162{
4163 struct vm_area_struct **vmas = NULL;
4164 struct page **pages = NULL;
4165 int i, j, got_pages = 0;
4166 int ret = -EINVAL;
4167
4168 if (ctx->user_bufs)
4169 return -EBUSY;
4170 if (!nr_args || nr_args > UIO_MAXIOV)
4171 return -EINVAL;
4172
4173 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
4174 GFP_KERNEL);
4175 if (!ctx->user_bufs)
4176 return -ENOMEM;
4177
4178 for (i = 0; i < nr_args; i++) {
4179 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
4180 unsigned long off, start, end, ubuf;
4181 int pret, nr_pages;
4182 struct iovec iov;
4183 size_t size;
4184
4185 ret = io_copy_iov(ctx, &iov, arg, i);
4186 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03004187 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07004188
4189 /*
4190 * Don't impose further limits on the size and buffer
4191 * constraints here, we'll -EINVAL later when IO is
4192 * submitted if they are wrong.
4193 */
4194 ret = -EFAULT;
4195 if (!iov.iov_base || !iov.iov_len)
4196 goto err;
4197
4198 /* arbitrary limit, but we need something */
4199 if (iov.iov_len > SZ_1G)
4200 goto err;
4201
4202 ubuf = (unsigned long) iov.iov_base;
4203 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
4204 start = ubuf >> PAGE_SHIFT;
4205 nr_pages = end - start;
4206
4207 if (ctx->account_mem) {
4208 ret = io_account_mem(ctx->user, nr_pages);
4209 if (ret)
4210 goto err;
4211 }
4212
4213 ret = 0;
4214 if (!pages || nr_pages > got_pages) {
4215 kfree(vmas);
4216 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004217 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07004218 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004219 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07004220 sizeof(struct vm_area_struct *),
4221 GFP_KERNEL);
4222 if (!pages || !vmas) {
4223 ret = -ENOMEM;
4224 if (ctx->account_mem)
4225 io_unaccount_mem(ctx->user, nr_pages);
4226 goto err;
4227 }
4228 got_pages = nr_pages;
4229 }
4230
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004231 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07004232 GFP_KERNEL);
4233 ret = -ENOMEM;
4234 if (!imu->bvec) {
4235 if (ctx->account_mem)
4236 io_unaccount_mem(ctx->user, nr_pages);
4237 goto err;
4238 }
4239
4240 ret = 0;
4241 down_read(&current->mm->mmap_sem);
Ira Weiny932f4a62019-05-13 17:17:03 -07004242 pret = get_user_pages(ubuf, nr_pages,
4243 FOLL_WRITE | FOLL_LONGTERM,
4244 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07004245 if (pret == nr_pages) {
4246 /* don't support file backed memory */
4247 for (j = 0; j < nr_pages; j++) {
4248 struct vm_area_struct *vma = vmas[j];
4249
4250 if (vma->vm_file &&
4251 !is_file_hugepages(vma->vm_file)) {
4252 ret = -EOPNOTSUPP;
4253 break;
4254 }
4255 }
4256 } else {
4257 ret = pret < 0 ? pret : -EFAULT;
4258 }
4259 up_read(&current->mm->mmap_sem);
4260 if (ret) {
4261 /*
4262 * if we did partial map, or found file backed vmas,
4263 * release any pages we did get
4264 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07004265 if (pret > 0)
4266 put_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07004267 if (ctx->account_mem)
4268 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004269 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07004270 goto err;
4271 }
4272
4273 off = ubuf & ~PAGE_MASK;
4274 size = iov.iov_len;
4275 for (j = 0; j < nr_pages; j++) {
4276 size_t vec_len;
4277
4278 vec_len = min_t(size_t, size, PAGE_SIZE - off);
4279 imu->bvec[j].bv_page = pages[j];
4280 imu->bvec[j].bv_len = vec_len;
4281 imu->bvec[j].bv_offset = off;
4282 off = 0;
4283 size -= vec_len;
4284 }
4285 /* store original address for later verification */
4286 imu->ubuf = ubuf;
4287 imu->len = iov.iov_len;
4288 imu->nr_bvecs = nr_pages;
4289
4290 ctx->nr_user_bufs++;
4291 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004292 kvfree(pages);
4293 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07004294 return 0;
4295err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004296 kvfree(pages);
4297 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07004298 io_sqe_buffer_unregister(ctx);
4299 return ret;
4300}
4301
Jens Axboe9b402842019-04-11 11:45:41 -06004302static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
4303{
4304 __s32 __user *fds = arg;
4305 int fd;
4306
4307 if (ctx->cq_ev_fd)
4308 return -EBUSY;
4309
4310 if (copy_from_user(&fd, fds, sizeof(*fds)))
4311 return -EFAULT;
4312
4313 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
4314 if (IS_ERR(ctx->cq_ev_fd)) {
4315 int ret = PTR_ERR(ctx->cq_ev_fd);
4316 ctx->cq_ev_fd = NULL;
4317 return ret;
4318 }
4319
4320 return 0;
4321}
4322
4323static int io_eventfd_unregister(struct io_ring_ctx *ctx)
4324{
4325 if (ctx->cq_ev_fd) {
4326 eventfd_ctx_put(ctx->cq_ev_fd);
4327 ctx->cq_ev_fd = NULL;
4328 return 0;
4329 }
4330
4331 return -ENXIO;
4332}
4333
Jens Axboe2b188cc2019-01-07 10:46:33 -07004334static void io_ring_ctx_free(struct io_ring_ctx *ctx)
4335{
Jens Axboe6b063142019-01-10 22:13:58 -07004336 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004337 if (ctx->sqo_mm)
4338 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07004339
4340 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07004341 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07004342 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06004343 io_eventfd_unregister(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07004344
Jens Axboe2b188cc2019-01-07 10:46:33 -07004345#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07004346 if (ctx->ring_sock) {
4347 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004348 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07004349 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004350#endif
4351
Hristo Venev75b28af2019-08-26 17:23:46 +00004352 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004353 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004354
4355 percpu_ref_exit(&ctx->refs);
4356 if (ctx->account_mem)
4357 io_unaccount_mem(ctx->user,
4358 ring_pages(ctx->sq_entries, ctx->cq_entries));
4359 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07004360 put_cred(ctx->creds);
Jens Axboe206aefd2019-11-07 18:27:42 -07004361 kfree(ctx->completions);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07004362 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004363 kfree(ctx);
4364}
4365
4366static __poll_t io_uring_poll(struct file *file, poll_table *wait)
4367{
4368 struct io_ring_ctx *ctx = file->private_data;
4369 __poll_t mask = 0;
4370
4371 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02004372 /*
4373 * synchronizes with barrier from wq_has_sleeper call in
4374 * io_commit_cqring
4375 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004376 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00004377 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
4378 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004379 mask |= EPOLLOUT | EPOLLWRNORM;
yangerkundaa5de52019-09-24 20:53:34 +08004380 if (READ_ONCE(ctx->rings->cq.head) != ctx->cached_cq_tail)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004381 mask |= EPOLLIN | EPOLLRDNORM;
4382
4383 return mask;
4384}
4385
4386static int io_uring_fasync(int fd, struct file *file, int on)
4387{
4388 struct io_ring_ctx *ctx = file->private_data;
4389
4390 return fasync_helper(fd, file, on, &ctx->cq_fasync);
4391}
4392
4393static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
4394{
4395 mutex_lock(&ctx->uring_lock);
4396 percpu_ref_kill(&ctx->refs);
4397 mutex_unlock(&ctx->uring_lock);
4398
Jens Axboe5262f562019-09-17 12:26:57 -06004399 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004400 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06004401
4402 if (ctx->io_wq)
4403 io_wq_cancel_all(ctx->io_wq);
4404
Jens Axboedef596e2019-01-09 08:59:42 -07004405 io_iopoll_reap_events(ctx);
Jens Axboe15dff282019-11-13 09:09:23 -07004406 /* if we failed setting up the ctx, we might not have any rings */
4407 if (ctx->rings)
4408 io_cqring_overflow_flush(ctx, true);
Jens Axboe206aefd2019-11-07 18:27:42 -07004409 wait_for_completion(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004410 io_ring_ctx_free(ctx);
4411}
4412
4413static int io_uring_release(struct inode *inode, struct file *file)
4414{
4415 struct io_ring_ctx *ctx = file->private_data;
4416
4417 file->private_data = NULL;
4418 io_ring_ctx_wait_and_kill(ctx);
4419 return 0;
4420}
4421
Jens Axboefcb323c2019-10-24 12:39:47 -06004422static void io_uring_cancel_files(struct io_ring_ctx *ctx,
4423 struct files_struct *files)
4424{
4425 struct io_kiocb *req;
4426 DEFINE_WAIT(wait);
4427
4428 while (!list_empty_careful(&ctx->inflight_list)) {
Jens Axboe768134d2019-11-10 20:30:53 -07004429 struct io_kiocb *cancel_req = NULL;
Jens Axboefcb323c2019-10-24 12:39:47 -06004430
4431 spin_lock_irq(&ctx->inflight_lock);
4432 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07004433 if (req->work.files != files)
4434 continue;
4435 /* req is being completed, ignore */
4436 if (!refcount_inc_not_zero(&req->refs))
4437 continue;
4438 cancel_req = req;
4439 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06004440 }
Jens Axboe768134d2019-11-10 20:30:53 -07004441 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06004442 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07004443 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06004444 spin_unlock_irq(&ctx->inflight_lock);
4445
Jens Axboe768134d2019-11-10 20:30:53 -07004446 /* We need to keep going until we don't find a matching req */
4447 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06004448 break;
Bob Liu2f6d9b92019-11-13 18:06:24 +08004449
4450 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
4451 io_put_req(cancel_req);
Jens Axboefcb323c2019-10-24 12:39:47 -06004452 schedule();
4453 }
Jens Axboe768134d2019-11-10 20:30:53 -07004454 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06004455}
4456
4457static int io_uring_flush(struct file *file, void *data)
4458{
4459 struct io_ring_ctx *ctx = file->private_data;
4460
4461 io_uring_cancel_files(ctx, data);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004462 if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) {
4463 io_cqring_overflow_flush(ctx, true);
Jens Axboefcb323c2019-10-24 12:39:47 -06004464 io_wq_cancel_all(ctx->io_wq);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004465 }
Jens Axboefcb323c2019-10-24 12:39:47 -06004466 return 0;
4467}
4468
Jens Axboe2b188cc2019-01-07 10:46:33 -07004469static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
4470{
4471 loff_t offset = (loff_t) vma->vm_pgoff << PAGE_SHIFT;
4472 unsigned long sz = vma->vm_end - vma->vm_start;
4473 struct io_ring_ctx *ctx = file->private_data;
4474 unsigned long pfn;
4475 struct page *page;
4476 void *ptr;
4477
4478 switch (offset) {
4479 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00004480 case IORING_OFF_CQ_RING:
4481 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004482 break;
4483 case IORING_OFF_SQES:
4484 ptr = ctx->sq_sqes;
4485 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004486 default:
4487 return -EINVAL;
4488 }
4489
4490 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07004491 if (sz > page_size(page))
Jens Axboe2b188cc2019-01-07 10:46:33 -07004492 return -EINVAL;
4493
4494 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
4495 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
4496}
4497
4498SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
4499 u32, min_complete, u32, flags, const sigset_t __user *, sig,
4500 size_t, sigsz)
4501{
4502 struct io_ring_ctx *ctx;
4503 long ret = -EBADF;
4504 int submitted = 0;
4505 struct fd f;
4506
Jens Axboe6c271ce2019-01-10 11:22:30 -07004507 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07004508 return -EINVAL;
4509
4510 f = fdget(fd);
4511 if (!f.file)
4512 return -EBADF;
4513
4514 ret = -EOPNOTSUPP;
4515 if (f.file->f_op != &io_uring_fops)
4516 goto out_fput;
4517
4518 ret = -ENXIO;
4519 ctx = f.file->private_data;
4520 if (!percpu_ref_tryget(&ctx->refs))
4521 goto out_fput;
4522
Jens Axboe6c271ce2019-01-10 11:22:30 -07004523 /*
4524 * For SQ polling, the thread will do all submissions and completions.
4525 * Just return the requested submit count, and wake the thread if
4526 * we were asked to.
4527 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06004528 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004529 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07004530 if (!list_empty_careful(&ctx->cq_overflow_list))
4531 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004532 if (flags & IORING_ENTER_SQ_WAKEUP)
4533 wake_up(&ctx->sqo_wait);
4534 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06004535 } else if (to_submit) {
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004536 struct mm_struct *cur_mm;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004537
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004538 to_submit = min(to_submit, ctx->sq_entries);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004539 mutex_lock(&ctx->uring_lock);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004540 /* already have mm, so io_submit_sqes() won't try to grab it */
4541 cur_mm = ctx->sqo_mm;
4542 submitted = io_submit_sqes(ctx, to_submit, f.file, fd,
4543 &cur_mm, false);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004544 mutex_unlock(&ctx->uring_lock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004545 }
4546 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07004547 unsigned nr_events = 0;
4548
Jens Axboe2b188cc2019-01-07 10:46:33 -07004549 min_complete = min(min_complete, ctx->cq_entries);
4550
Jens Axboedef596e2019-01-09 08:59:42 -07004551 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07004552 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07004553 } else {
4554 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
4555 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004556 }
4557
Pavel Begunkov6805b322019-10-08 02:18:42 +03004558 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004559out_fput:
4560 fdput(f);
4561 return submitted ? submitted : ret;
4562}
4563
4564static const struct file_operations io_uring_fops = {
4565 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06004566 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07004567 .mmap = io_uring_mmap,
4568 .poll = io_uring_poll,
4569 .fasync = io_uring_fasync,
4570};
4571
4572static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
4573 struct io_uring_params *p)
4574{
Hristo Venev75b28af2019-08-26 17:23:46 +00004575 struct io_rings *rings;
4576 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004577
Hristo Venev75b28af2019-08-26 17:23:46 +00004578 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
4579 if (size == SIZE_MAX)
4580 return -EOVERFLOW;
4581
4582 rings = io_mem_alloc(size);
4583 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004584 return -ENOMEM;
4585
Hristo Venev75b28af2019-08-26 17:23:46 +00004586 ctx->rings = rings;
4587 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
4588 rings->sq_ring_mask = p->sq_entries - 1;
4589 rings->cq_ring_mask = p->cq_entries - 1;
4590 rings->sq_ring_entries = p->sq_entries;
4591 rings->cq_ring_entries = p->cq_entries;
4592 ctx->sq_mask = rings->sq_ring_mask;
4593 ctx->cq_mask = rings->cq_ring_mask;
4594 ctx->sq_entries = rings->sq_ring_entries;
4595 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004596
4597 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07004598 if (size == SIZE_MAX) {
4599 io_mem_free(ctx->rings);
4600 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004601 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07004602 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004603
4604 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07004605 if (!ctx->sq_sqes) {
4606 io_mem_free(ctx->rings);
4607 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004608 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07004609 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004610
Jens Axboe2b188cc2019-01-07 10:46:33 -07004611 return 0;
4612}
4613
4614/*
4615 * Allocate an anonymous fd, this is what constitutes the application
4616 * visible backing of an io_uring instance. The application mmaps this
4617 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
4618 * we have to tie this fd to a socket for file garbage collection purposes.
4619 */
4620static int io_uring_get_fd(struct io_ring_ctx *ctx)
4621{
4622 struct file *file;
4623 int ret;
4624
4625#if defined(CONFIG_UNIX)
4626 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
4627 &ctx->ring_sock);
4628 if (ret)
4629 return ret;
4630#endif
4631
4632 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
4633 if (ret < 0)
4634 goto err;
4635
4636 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
4637 O_RDWR | O_CLOEXEC);
4638 if (IS_ERR(file)) {
4639 put_unused_fd(ret);
4640 ret = PTR_ERR(file);
4641 goto err;
4642 }
4643
4644#if defined(CONFIG_UNIX)
4645 ctx->ring_sock->file = file;
Jens Axboe6b063142019-01-10 22:13:58 -07004646 ctx->ring_sock->sk->sk_user_data = ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004647#endif
4648 fd_install(ret, file);
4649 return ret;
4650err:
4651#if defined(CONFIG_UNIX)
4652 sock_release(ctx->ring_sock);
4653 ctx->ring_sock = NULL;
4654#endif
4655 return ret;
4656}
4657
4658static int io_uring_create(unsigned entries, struct io_uring_params *p)
4659{
4660 struct user_struct *user = NULL;
4661 struct io_ring_ctx *ctx;
4662 bool account_mem;
4663 int ret;
4664
4665 if (!entries || entries > IORING_MAX_ENTRIES)
4666 return -EINVAL;
4667
4668 /*
4669 * Use twice as many entries for the CQ ring. It's possible for the
4670 * application to drive a higher depth than the size of the SQ ring,
4671 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06004672 * some flexibility in overcommitting a bit. If the application has
4673 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
4674 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07004675 */
4676 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06004677 if (p->flags & IORING_SETUP_CQSIZE) {
4678 /*
4679 * If IORING_SETUP_CQSIZE is set, we do the same roundup
4680 * to a power-of-two, if it isn't already. We do NOT impose
4681 * any cq vs sq ring sizing.
4682 */
4683 if (p->cq_entries < p->sq_entries || p->cq_entries > IORING_MAX_CQ_ENTRIES)
4684 return -EINVAL;
4685 p->cq_entries = roundup_pow_of_two(p->cq_entries);
4686 } else {
4687 p->cq_entries = 2 * p->sq_entries;
4688 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004689
4690 user = get_uid(current_user());
4691 account_mem = !capable(CAP_IPC_LOCK);
4692
4693 if (account_mem) {
4694 ret = io_account_mem(user,
4695 ring_pages(p->sq_entries, p->cq_entries));
4696 if (ret) {
4697 free_uid(user);
4698 return ret;
4699 }
4700 }
4701
4702 ctx = io_ring_ctx_alloc(p);
4703 if (!ctx) {
4704 if (account_mem)
4705 io_unaccount_mem(user, ring_pages(p->sq_entries,
4706 p->cq_entries));
4707 free_uid(user);
4708 return -ENOMEM;
4709 }
4710 ctx->compat = in_compat_syscall();
4711 ctx->account_mem = account_mem;
4712 ctx->user = user;
Jens Axboe181e4482019-11-25 08:52:30 -07004713 ctx->creds = prepare_creds();
Jens Axboe2b188cc2019-01-07 10:46:33 -07004714
4715 ret = io_allocate_scq_urings(ctx, p);
4716 if (ret)
4717 goto err;
4718
Jens Axboe6c271ce2019-01-10 11:22:30 -07004719 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004720 if (ret)
4721 goto err;
4722
Jens Axboe2b188cc2019-01-07 10:46:33 -07004723 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00004724 p->sq_off.head = offsetof(struct io_rings, sq.head);
4725 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
4726 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
4727 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
4728 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
4729 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
4730 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004731
4732 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00004733 p->cq_off.head = offsetof(struct io_rings, cq.head);
4734 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
4735 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
4736 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
4737 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
4738 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06004739
Jens Axboe044c1ab2019-10-28 09:15:33 -06004740 /*
4741 * Install ring fd as the very last thing, so we don't risk someone
4742 * having closed it before we finish setup
4743 */
4744 ret = io_uring_get_fd(ctx);
4745 if (ret < 0)
4746 goto err;
4747
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004748 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004749 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004750 return ret;
4751err:
4752 io_ring_ctx_wait_and_kill(ctx);
4753 return ret;
4754}
4755
4756/*
4757 * Sets up an aio uring context, and returns the fd. Applications asks for a
4758 * ring size, we return the actual sq/cq ring sizes (among other things) in the
4759 * params structure passed in.
4760 */
4761static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
4762{
4763 struct io_uring_params p;
4764 long ret;
4765 int i;
4766
4767 if (copy_from_user(&p, params, sizeof(p)))
4768 return -EFAULT;
4769 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
4770 if (p.resv[i])
4771 return -EINVAL;
4772 }
4773
Jens Axboe6c271ce2019-01-10 11:22:30 -07004774 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe33a107f2019-10-04 12:10:03 -06004775 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE))
Jens Axboe2b188cc2019-01-07 10:46:33 -07004776 return -EINVAL;
4777
4778 ret = io_uring_create(entries, &p);
4779 if (ret < 0)
4780 return ret;
4781
4782 if (copy_to_user(params, &p, sizeof(p)))
4783 return -EFAULT;
4784
4785 return ret;
4786}
4787
4788SYSCALL_DEFINE2(io_uring_setup, u32, entries,
4789 struct io_uring_params __user *, params)
4790{
4791 return io_uring_setup(entries, params);
4792}
4793
Jens Axboeedafcce2019-01-09 09:16:05 -07004794static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
4795 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06004796 __releases(ctx->uring_lock)
4797 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07004798{
4799 int ret;
4800
Jens Axboe35fa71a2019-04-22 10:23:23 -06004801 /*
4802 * We're inside the ring mutex, if the ref is already dying, then
4803 * someone else killed the ctx or is already going through
4804 * io_uring_register().
4805 */
4806 if (percpu_ref_is_dying(&ctx->refs))
4807 return -ENXIO;
4808
Jens Axboeedafcce2019-01-09 09:16:05 -07004809 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06004810
4811 /*
4812 * Drop uring mutex before waiting for references to exit. If another
4813 * thread is currently inside io_uring_enter() it might need to grab
4814 * the uring_lock to make progress. If we hold it here across the drain
4815 * wait, then we can deadlock. It's safe to drop the mutex here, since
4816 * no new references will come in after we've killed the percpu ref.
4817 */
4818 mutex_unlock(&ctx->uring_lock);
Jens Axboe206aefd2019-11-07 18:27:42 -07004819 wait_for_completion(&ctx->completions[0]);
Jens Axboeb19062a2019-04-15 10:49:38 -06004820 mutex_lock(&ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07004821
4822 switch (opcode) {
4823 case IORING_REGISTER_BUFFERS:
4824 ret = io_sqe_buffer_register(ctx, arg, nr_args);
4825 break;
4826 case IORING_UNREGISTER_BUFFERS:
4827 ret = -EINVAL;
4828 if (arg || nr_args)
4829 break;
4830 ret = io_sqe_buffer_unregister(ctx);
4831 break;
Jens Axboe6b063142019-01-10 22:13:58 -07004832 case IORING_REGISTER_FILES:
4833 ret = io_sqe_files_register(ctx, arg, nr_args);
4834 break;
4835 case IORING_UNREGISTER_FILES:
4836 ret = -EINVAL;
4837 if (arg || nr_args)
4838 break;
4839 ret = io_sqe_files_unregister(ctx);
4840 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06004841 case IORING_REGISTER_FILES_UPDATE:
4842 ret = io_sqe_files_update(ctx, arg, nr_args);
4843 break;
Jens Axboe9b402842019-04-11 11:45:41 -06004844 case IORING_REGISTER_EVENTFD:
4845 ret = -EINVAL;
4846 if (nr_args != 1)
4847 break;
4848 ret = io_eventfd_register(ctx, arg);
4849 break;
4850 case IORING_UNREGISTER_EVENTFD:
4851 ret = -EINVAL;
4852 if (arg || nr_args)
4853 break;
4854 ret = io_eventfd_unregister(ctx);
4855 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07004856 default:
4857 ret = -EINVAL;
4858 break;
4859 }
4860
4861 /* bring the ctx back to life */
Jens Axboe206aefd2019-11-07 18:27:42 -07004862 reinit_completion(&ctx->completions[0]);
Jens Axboeedafcce2019-01-09 09:16:05 -07004863 percpu_ref_reinit(&ctx->refs);
4864 return ret;
4865}
4866
4867SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
4868 void __user *, arg, unsigned int, nr_args)
4869{
4870 struct io_ring_ctx *ctx;
4871 long ret = -EBADF;
4872 struct fd f;
4873
4874 f = fdget(fd);
4875 if (!f.file)
4876 return -EBADF;
4877
4878 ret = -EOPNOTSUPP;
4879 if (f.file->f_op != &io_uring_fops)
4880 goto out_fput;
4881
4882 ctx = f.file->private_data;
4883
4884 mutex_lock(&ctx->uring_lock);
4885 ret = __io_uring_register(ctx, opcode, arg, nr_args);
4886 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004887 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
4888 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07004889out_fput:
4890 fdput(f);
4891 return ret;
4892}
4893
Jens Axboe2b188cc2019-01-07 10:46:33 -07004894static int __init io_uring_init(void)
4895{
4896 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
4897 return 0;
4898};
4899__initcall(io_uring_init);