blob: 1597838d507388845bd668c70688f854de2f50c1 [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;
177};
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;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700189
Hristo Venev75b28af2019-08-26 17:23:46 +0000190 /*
191 * Ring buffer of indices into array of io_uring_sqe, which is
192 * mmapped by the application using the IORING_OFF_SQES offset.
193 *
194 * This indirection could e.g. be used to assign fixed
195 * io_uring_sqe entries to operations and only submit them to
196 * the queue when needed.
197 *
198 * The kernel modifies neither the indices array nor the entries
199 * array.
200 */
201 u32 *sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700202 unsigned cached_sq_head;
203 unsigned sq_entries;
204 unsigned sq_mask;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700205 unsigned sq_thread_idle;
Jens Axboe498ccd92019-10-25 10:04:25 -0600206 unsigned cached_sq_dropped;
Jens Axboe206aefd2019-11-07 18:27:42 -0700207 atomic_t cached_cq_overflow;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700208 struct io_uring_sqe *sq_sqes;
Jens Axboede0617e2019-04-06 21:51:27 -0600209
210 struct list_head defer_list;
Jens Axboe5262f562019-09-17 12:26:57 -0600211 struct list_head timeout_list;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700212 struct list_head cq_overflow_list;
Jens Axboefcb323c2019-10-24 12:39:47 -0600213
214 wait_queue_head_t inflight_wait;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700215 } ____cacheline_aligned_in_smp;
216
Jens Axboe206aefd2019-11-07 18:27:42 -0700217 struct io_rings *rings;
218
Jens Axboe2b188cc2019-01-07 10:46:33 -0700219 /* IO offload */
Jens Axboe561fb042019-10-24 07:25:42 -0600220 struct io_wq *io_wq;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700221 struct task_struct *sqo_thread; /* if using sq thread polling */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700222 struct mm_struct *sqo_mm;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700223 wait_queue_head_t sqo_wait;
Hristo Venev75b28af2019-08-26 17:23:46 +0000224
Jens Axboe6b063142019-01-10 22:13:58 -0700225 /*
226 * If used, fixed file set. Writers must ensure that ->refs is dead,
227 * readers must ensure that ->refs is alive as long as the file* is
228 * used. Only updated through io_uring_register(2).
229 */
Jens Axboe65e19f52019-10-26 07:20:21 -0600230 struct fixed_file_table *file_table;
Jens Axboe6b063142019-01-10 22:13:58 -0700231 unsigned nr_user_files;
232
Jens Axboeedafcce2019-01-09 09:16:05 -0700233 /* if used, fixed mapped user buffers */
234 unsigned nr_user_bufs;
235 struct io_mapped_ubuf *user_bufs;
236
Jens Axboe2b188cc2019-01-07 10:46:33 -0700237 struct user_struct *user;
238
Jens Axboe206aefd2019-11-07 18:27:42 -0700239 /* 0 is for ctx quiesce/reinit/free, 1 is for sqo_thread started */
240 struct completion *completions;
241
242#if defined(CONFIG_UNIX)
243 struct socket *ring_sock;
244#endif
245
246 struct {
247 unsigned cached_cq_tail;
248 unsigned cq_entries;
249 unsigned cq_mask;
250 atomic_t cq_timeouts;
251 struct wait_queue_head cq_wait;
252 struct fasync_struct *cq_fasync;
253 struct eventfd_ctx *cq_ev_fd;
254 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700255
256 struct {
257 struct mutex uring_lock;
258 wait_queue_head_t wait;
259 } ____cacheline_aligned_in_smp;
260
261 struct {
262 spinlock_t completion_lock;
Jens Axboedef596e2019-01-09 08:59:42 -0700263 bool poll_multi_file;
264 /*
265 * ->poll_list is protected by the ctx->uring_lock for
266 * io_uring instances that don't use IORING_SETUP_SQPOLL.
267 * For SQPOLL, only the single threaded io_sq_thread() will
268 * manipulate the list, hence no extra locking is needed there.
269 */
270 struct list_head poll_list;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700271 struct list_head cancel_list;
Jens Axboefcb323c2019-10-24 12:39:47 -0600272
273 spinlock_t inflight_lock;
274 struct list_head inflight_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700275 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700276};
277
278struct sqe_submit {
279 const struct io_uring_sqe *sqe;
Jens Axboefcb323c2019-10-24 12:39:47 -0600280 struct file *ring_file;
281 int ring_fd;
Jackie Liu8776f3f2019-09-09 20:50:39 +0800282 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700283 bool has_user;
Jackie Liuba5290c2019-10-09 09:19:59 +0800284 bool in_async;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700285 bool needs_fixed_file;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700286};
287
Jens Axboe09bb8392019-03-13 12:39:28 -0600288/*
289 * First field must be the file pointer in all the
290 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
291 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700292struct io_poll_iocb {
293 struct file *file;
294 struct wait_queue_head *head;
295 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600296 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700297 bool canceled;
298 struct wait_queue_entry wait;
299};
300
Jens Axboe5262f562019-09-17 12:26:57 -0600301struct io_timeout {
302 struct file *file;
303 struct hrtimer timer;
304};
305
Jens Axboe09bb8392019-03-13 12:39:28 -0600306/*
307 * NOTE! Each of the iocb union members has the file pointer
308 * as the first entry in their struct definition. So you can
309 * access the file pointer through any of the sub-structs,
310 * or directly as just 'ki_filp' in this struct.
311 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700312struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700313 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600314 struct file *file;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700315 struct kiocb rw;
316 struct io_poll_iocb poll;
Jens Axboe5262f562019-09-17 12:26:57 -0600317 struct io_timeout timeout;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700318 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700319
320 struct sqe_submit submit;
321
322 struct io_ring_ctx *ctx;
323 struct list_head list;
Jens Axboe9e645e112019-05-10 16:07:28 -0600324 struct list_head link_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700325 unsigned int flags;
Jens Axboec16361c2019-01-17 08:39:48 -0700326 refcount_t refs;
Stefan Bühler8449eed2019-04-27 20:34:19 +0200327#define REQ_F_NOWAIT 1 /* must not punt to workers */
Jens Axboedef596e2019-01-09 08:59:42 -0700328#define REQ_F_IOPOLL_COMPLETED 2 /* polled IO has completed */
Jens Axboe6b063142019-01-10 22:13:58 -0700329#define REQ_F_FIXED_FILE 4 /* ctx owns file */
Jens Axboe31b51512019-01-18 22:56:34 -0700330#define REQ_F_SEQ_PREV 8 /* sequential with previous */
Stefan Bühlere2033e32019-05-11 19:08:01 +0200331#define REQ_F_IO_DRAIN 16 /* drain existing IO first */
332#define REQ_F_IO_DRAINED 32 /* drain done */
Jens Axboe9e645e112019-05-10 16:07:28 -0600333#define REQ_F_LINK 64 /* linked sqes */
Jens Axboe2665abf2019-11-05 12:40:47 -0700334#define REQ_F_LINK_TIMEOUT 128 /* has linked timeout */
Zhengyuan Liuf7b76ac2019-07-16 23:26:14 +0800335#define REQ_F_FAIL_LINK 256 /* fail rest of links */
Jackie Liu4fe2c962019-09-09 20:50:40 +0800336#define REQ_F_SHADOW_DRAIN 512 /* link-drain shadow req */
Jens Axboe5262f562019-09-17 12:26:57 -0600337#define REQ_F_TIMEOUT 1024 /* timeout request */
Jens Axboe491381ce2019-10-17 09:20:46 -0600338#define REQ_F_ISREG 2048 /* regular file */
339#define REQ_F_MUST_PUNT 4096 /* must be punted even for NONBLOCK */
Jens Axboefcb323c2019-10-24 12:39:47 -0600340#define REQ_F_INFLIGHT 8192 /* on inflight list */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700341 u64 user_data;
Jens Axboe9e645e112019-05-10 16:07:28 -0600342 u32 result;
Jens Axboede0617e2019-04-06 21:51:27 -0600343 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700344
Jens Axboefcb323c2019-10-24 12:39:47 -0600345 struct list_head inflight_entry;
346
Jens Axboe561fb042019-10-24 07:25:42 -0600347 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700348};
349
350#define IO_PLUG_THRESHOLD 2
Jens Axboedef596e2019-01-09 08:59:42 -0700351#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700352
Jens Axboe9a56a232019-01-09 09:06:50 -0700353struct io_submit_state {
354 struct blk_plug plug;
355
356 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700357 * io_kiocb alloc cache
358 */
359 void *reqs[IO_IOPOLL_BATCH];
360 unsigned int free_reqs;
361 unsigned int cur_req;
362
363 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700364 * File reference cache
365 */
366 struct file *file;
367 unsigned int fd;
368 unsigned int has_refs;
369 unsigned int used_refs;
370 unsigned int ios_left;
371};
372
Jens Axboe561fb042019-10-24 07:25:42 -0600373static void io_wq_submit_work(struct io_wq_work **workptr);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700374static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liu4fe2c962019-09-09 20:50:40 +0800375static void __io_free_req(struct io_kiocb *req);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800376static void io_put_req(struct io_kiocb *req);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700377static void io_double_put_req(struct io_kiocb *req);
Jens Axboede0617e2019-04-06 21:51:27 -0600378
Jens Axboe2b188cc2019-01-07 10:46:33 -0700379static struct kmem_cache *req_cachep;
380
381static const struct file_operations io_uring_fops;
382
383struct sock *io_uring_get_socket(struct file *file)
384{
385#if defined(CONFIG_UNIX)
386 if (file->f_op == &io_uring_fops) {
387 struct io_ring_ctx *ctx = file->private_data;
388
389 return ctx->ring_sock->sk;
390 }
391#endif
392 return NULL;
393}
394EXPORT_SYMBOL(io_uring_get_socket);
395
396static void io_ring_ctx_ref_free(struct percpu_ref *ref)
397{
398 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
399
Jens Axboe206aefd2019-11-07 18:27:42 -0700400 complete(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700401}
402
403static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
404{
405 struct io_ring_ctx *ctx;
406
407 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
408 if (!ctx)
409 return NULL;
410
Jens Axboe206aefd2019-11-07 18:27:42 -0700411 ctx->completions = kmalloc(2 * sizeof(struct completion), GFP_KERNEL);
412 if (!ctx->completions)
413 goto err;
414
Roman Gushchin21482892019-05-07 10:01:48 -0700415 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -0700416 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
417 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700418
419 ctx->flags = p->flags;
420 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700421 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe206aefd2019-11-07 18:27:42 -0700422 init_completion(&ctx->completions[0]);
423 init_completion(&ctx->completions[1]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700424 mutex_init(&ctx->uring_lock);
425 init_waitqueue_head(&ctx->wait);
426 spin_lock_init(&ctx->completion_lock);
Jens Axboedef596e2019-01-09 08:59:42 -0700427 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboe221c5eb2019-01-17 09:41:58 -0700428 INIT_LIST_HEAD(&ctx->cancel_list);
Jens Axboede0617e2019-04-06 21:51:27 -0600429 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -0600430 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -0600431 init_waitqueue_head(&ctx->inflight_wait);
432 spin_lock_init(&ctx->inflight_lock);
433 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700434 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -0700435err:
436 kfree(ctx->completions);
437 kfree(ctx);
438 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700439}
440
Jackie Liua197f662019-11-08 08:09:12 -0700441static inline bool __io_sequence_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -0600442{
Jackie Liua197f662019-11-08 08:09:12 -0700443 struct io_ring_ctx *ctx = req->ctx;
444
Jens Axboe498ccd92019-10-25 10:04:25 -0600445 return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped
446 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboede0617e2019-04-06 21:51:27 -0600447}
448
Jackie Liua197f662019-11-08 08:09:12 -0700449static inline bool io_sequence_defer(struct io_kiocb *req)
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600450{
451 if ((req->flags & (REQ_F_IO_DRAIN|REQ_F_IO_DRAINED)) != REQ_F_IO_DRAIN)
452 return false;
453
Jackie Liua197f662019-11-08 08:09:12 -0700454 return __io_sequence_defer(req);
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600455}
456
457static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -0600458{
459 struct io_kiocb *req;
460
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600461 req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list);
Jackie Liua197f662019-11-08 08:09:12 -0700462 if (req && !io_sequence_defer(req)) {
Jens Axboede0617e2019-04-06 21:51:27 -0600463 list_del_init(&req->list);
464 return req;
465 }
466
467 return NULL;
468}
469
Jens Axboe5262f562019-09-17 12:26:57 -0600470static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx)
471{
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600472 struct io_kiocb *req;
473
474 req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list);
Jackie Liua197f662019-11-08 08:09:12 -0700475 if (req && !__io_sequence_defer(req)) {
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600476 list_del_init(&req->list);
477 return req;
478 }
479
480 return NULL;
Jens Axboe5262f562019-09-17 12:26:57 -0600481}
482
Jens Axboede0617e2019-04-06 21:51:27 -0600483static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700484{
Hristo Venev75b28af2019-08-26 17:23:46 +0000485 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700486
Hristo Venev75b28af2019-08-26 17:23:46 +0000487 if (ctx->cached_cq_tail != READ_ONCE(rings->cq.tail)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700488 /* order cqe stores with ring update */
Hristo Venev75b28af2019-08-26 17:23:46 +0000489 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700490
Jens Axboe2b188cc2019-01-07 10:46:33 -0700491 if (wq_has_sleeper(&ctx->cq_wait)) {
492 wake_up_interruptible(&ctx->cq_wait);
493 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
494 }
495 }
496}
497
Jens Axboe561fb042019-10-24 07:25:42 -0600498static inline bool io_sqe_needs_user(const struct io_uring_sqe *sqe)
Jens Axboe18d9be12019-09-10 09:13:05 -0600499{
Jens Axboe561fb042019-10-24 07:25:42 -0600500 u8 opcode = READ_ONCE(sqe->opcode);
501
502 return !(opcode == IORING_OP_READ_FIXED ||
503 opcode == IORING_OP_WRITE_FIXED);
504}
505
506static inline bool io_prep_async_work(struct io_kiocb *req)
507{
508 bool do_hashed = false;
Jens Axboe54a91f32019-09-10 09:15:04 -0600509
Jens Axboe6cc47d12019-09-18 11:18:23 -0600510 if (req->submit.sqe) {
511 switch (req->submit.sqe->opcode) {
512 case IORING_OP_WRITEV:
513 case IORING_OP_WRITE_FIXED:
Jens Axboe561fb042019-10-24 07:25:42 -0600514 do_hashed = true;
Jens Axboe5f8fd2d32019-11-07 10:57:36 -0700515 /* fall-through */
516 case IORING_OP_READV:
517 case IORING_OP_READ_FIXED:
518 case IORING_OP_SENDMSG:
519 case IORING_OP_RECVMSG:
520 case IORING_OP_ACCEPT:
521 case IORING_OP_POLL_ADD:
522 /*
523 * We know REQ_F_ISREG is not set on some of these
524 * opcodes, but this enables us to keep the check in
525 * just one place.
526 */
527 if (!(req->flags & REQ_F_ISREG))
528 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe6cc47d12019-09-18 11:18:23 -0600529 break;
530 }
Jens Axboe561fb042019-10-24 07:25:42 -0600531 if (io_sqe_needs_user(req->submit.sqe))
532 req->work.flags |= IO_WQ_WORK_NEEDS_USER;
Jens Axboe54a91f32019-09-10 09:15:04 -0600533 }
534
Jens Axboe561fb042019-10-24 07:25:42 -0600535 return do_hashed;
536}
537
Jackie Liua197f662019-11-08 08:09:12 -0700538static inline void io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -0600539{
540 bool do_hashed = io_prep_async_work(req);
Jackie Liua197f662019-11-08 08:09:12 -0700541 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe561fb042019-10-24 07:25:42 -0600542
543 trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work,
544 req->flags);
545 if (!do_hashed) {
546 io_wq_enqueue(ctx->io_wq, &req->work);
547 } else {
548 io_wq_enqueue_hashed(ctx->io_wq, &req->work,
549 file_inode(req->file));
550 }
Jens Axboe18d9be12019-09-10 09:13:05 -0600551}
552
Jens Axboe5262f562019-09-17 12:26:57 -0600553static void io_kill_timeout(struct io_kiocb *req)
554{
555 int ret;
556
557 ret = hrtimer_try_to_cancel(&req->timeout.timer);
558 if (ret != -1) {
559 atomic_inc(&req->ctx->cq_timeouts);
Jens Axboe842f9612019-10-29 12:34:10 -0600560 list_del_init(&req->list);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700561 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800562 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -0600563 }
564}
565
566static void io_kill_timeouts(struct io_ring_ctx *ctx)
567{
568 struct io_kiocb *req, *tmp;
569
570 spin_lock_irq(&ctx->completion_lock);
571 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
572 io_kill_timeout(req);
573 spin_unlock_irq(&ctx->completion_lock);
574}
575
Jens Axboede0617e2019-04-06 21:51:27 -0600576static void io_commit_cqring(struct io_ring_ctx *ctx)
577{
578 struct io_kiocb *req;
579
Jens Axboe5262f562019-09-17 12:26:57 -0600580 while ((req = io_get_timeout_req(ctx)) != NULL)
581 io_kill_timeout(req);
582
Jens Axboede0617e2019-04-06 21:51:27 -0600583 __io_commit_cqring(ctx);
584
585 while ((req = io_get_deferred_req(ctx)) != NULL) {
Jackie Liu4fe2c962019-09-09 20:50:40 +0800586 if (req->flags & REQ_F_SHADOW_DRAIN) {
587 /* Just for drain, free it. */
588 __io_free_req(req);
589 continue;
590 }
Jens Axboede0617e2019-04-06 21:51:27 -0600591 req->flags |= REQ_F_IO_DRAINED;
Jackie Liua197f662019-11-08 08:09:12 -0700592 io_queue_async_work(req);
Jens Axboede0617e2019-04-06 21:51:27 -0600593 }
594}
595
Jens Axboe2b188cc2019-01-07 10:46:33 -0700596static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
597{
Hristo Venev75b28af2019-08-26 17:23:46 +0000598 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700599 unsigned tail;
600
601 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +0200602 /*
603 * writes to the cq entry need to come after reading head; the
604 * control dependency is enough as we're using WRITE_ONCE to
605 * fill the cq entry
606 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000607 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700608 return NULL;
609
610 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +0000611 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -0700612}
613
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700614static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
615{
616 if (waitqueue_active(&ctx->wait))
617 wake_up(&ctx->wait);
618 if (waitqueue_active(&ctx->sqo_wait))
619 wake_up(&ctx->sqo_wait);
620 if (ctx->cq_ev_fd)
621 eventfd_signal(ctx->cq_ev_fd, 1);
622}
623
624static void io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
625{
626 struct io_rings *rings = ctx->rings;
627 struct io_uring_cqe *cqe;
628 struct io_kiocb *req;
629 unsigned long flags;
630 LIST_HEAD(list);
631
632 if (!force) {
633 if (list_empty_careful(&ctx->cq_overflow_list))
634 return;
635 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
636 rings->cq_ring_entries))
637 return;
638 }
639
640 spin_lock_irqsave(&ctx->completion_lock, flags);
641
642 /* if force is set, the ring is going away. always drop after that */
643 if (force)
644 ctx->cq_overflow_flushed = true;
645
646 while (!list_empty(&ctx->cq_overflow_list)) {
647 cqe = io_get_cqring(ctx);
648 if (!cqe && !force)
649 break;
650
651 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
652 list);
653 list_move(&req->list, &list);
654 if (cqe) {
655 WRITE_ONCE(cqe->user_data, req->user_data);
656 WRITE_ONCE(cqe->res, req->result);
657 WRITE_ONCE(cqe->flags, 0);
658 } else {
659 WRITE_ONCE(ctx->rings->cq_overflow,
660 atomic_inc_return(&ctx->cached_cq_overflow));
661 }
662 }
663
664 io_commit_cqring(ctx);
665 spin_unlock_irqrestore(&ctx->completion_lock, flags);
666 io_cqring_ev_posted(ctx);
667
668 while (!list_empty(&list)) {
669 req = list_first_entry(&list, struct io_kiocb, list);
670 list_del(&req->list);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800671 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700672 }
673}
674
Jens Axboe78e19bb2019-11-06 15:21:34 -0700675static void io_cqring_fill_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700676{
Jens Axboe78e19bb2019-11-06 15:21:34 -0700677 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700678 struct io_uring_cqe *cqe;
679
Jens Axboe78e19bb2019-11-06 15:21:34 -0700680 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -0700681
Jens Axboe2b188cc2019-01-07 10:46:33 -0700682 /*
683 * If we can't get a cq entry, userspace overflowed the
684 * submission (by quite a lot). Increment the overflow count in
685 * the ring.
686 */
687 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700688 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -0700689 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700690 WRITE_ONCE(cqe->res, res);
Jens Axboec71ffb62019-05-13 20:58:29 -0600691 WRITE_ONCE(cqe->flags, 0);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700692 } else if (ctx->cq_overflow_flushed) {
Jens Axboe498ccd92019-10-25 10:04:25 -0600693 WRITE_ONCE(ctx->rings->cq_overflow,
694 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700695 } else {
696 refcount_inc(&req->refs);
697 req->result = res;
698 list_add_tail(&req->list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700699 }
700}
701
Jens Axboe78e19bb2019-11-06 15:21:34 -0700702static void io_cqring_add_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700703{
Jens Axboe78e19bb2019-11-06 15:21:34 -0700704 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700705 unsigned long flags;
706
707 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700708 io_cqring_fill_event(req, res);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700709 io_commit_cqring(ctx);
710 spin_unlock_irqrestore(&ctx->completion_lock, flags);
711
Jens Axboe8c838782019-03-12 15:48:16 -0600712 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700713}
714
Jens Axboe2579f912019-01-09 09:10:43 -0700715static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
716 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700717{
Jens Axboefd6fab22019-03-14 16:30:06 -0600718 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700719 struct io_kiocb *req;
720
721 if (!percpu_ref_tryget(&ctx->refs))
722 return NULL;
723
Jens Axboe2579f912019-01-09 09:10:43 -0700724 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -0600725 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -0700726 if (unlikely(!req))
727 goto out;
728 } else if (!state->free_reqs) {
729 size_t sz;
730 int ret;
731
732 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -0600733 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
734
735 /*
736 * Bulk alloc is all-or-nothing. If we fail to get a batch,
737 * retry single alloc to be on the safe side.
738 */
739 if (unlikely(ret <= 0)) {
740 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
741 if (!state->reqs[0])
742 goto out;
743 ret = 1;
744 }
Jens Axboe2579f912019-01-09 09:10:43 -0700745 state->free_reqs = ret - 1;
746 state->cur_req = 1;
747 req = state->reqs[0];
748 } else {
749 req = state->reqs[state->cur_req];
750 state->free_reqs--;
751 state->cur_req++;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700752 }
753
Jens Axboe60c112b2019-06-21 10:20:18 -0600754 req->file = NULL;
Jens Axboe2579f912019-01-09 09:10:43 -0700755 req->ctx = ctx;
756 req->flags = 0;
Jens Axboee65ef562019-03-12 10:16:44 -0600757 /* one is dropped after submission, the other at completion */
758 refcount_set(&req->refs, 2);
Jens Axboe9e645e112019-05-10 16:07:28 -0600759 req->result = 0;
Jens Axboe561fb042019-10-24 07:25:42 -0600760 INIT_IO_WORK(&req->work, io_wq_submit_work);
Jens Axboe2579f912019-01-09 09:10:43 -0700761 return req;
762out:
Pavel Begunkov6805b322019-10-08 02:18:42 +0300763 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700764 return NULL;
765}
766
Jens Axboedef596e2019-01-09 08:59:42 -0700767static void io_free_req_many(struct io_ring_ctx *ctx, void **reqs, int *nr)
768{
769 if (*nr) {
770 kmem_cache_free_bulk(req_cachep, *nr, reqs);
Pavel Begunkov6805b322019-10-08 02:18:42 +0300771 percpu_ref_put_many(&ctx->refs, *nr);
Jens Axboedef596e2019-01-09 08:59:42 -0700772 *nr = 0;
773 }
774}
775
Jens Axboe9e645e112019-05-10 16:07:28 -0600776static void __io_free_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700777{
Jens Axboefcb323c2019-10-24 12:39:47 -0600778 struct io_ring_ctx *ctx = req->ctx;
779
Jens Axboe09bb8392019-03-13 12:39:28 -0600780 if (req->file && !(req->flags & REQ_F_FIXED_FILE))
781 fput(req->file);
Jens Axboefcb323c2019-10-24 12:39:47 -0600782 if (req->flags & REQ_F_INFLIGHT) {
783 unsigned long flags;
784
785 spin_lock_irqsave(&ctx->inflight_lock, flags);
786 list_del(&req->inflight_entry);
787 if (waitqueue_active(&ctx->inflight_wait))
788 wake_up(&ctx->inflight_wait);
789 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
790 }
791 percpu_ref_put(&ctx->refs);
Jens Axboee65ef562019-03-12 10:16:44 -0600792 kmem_cache_free(req_cachep, req);
793}
794
Jackie Liua197f662019-11-08 08:09:12 -0700795static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -0700796{
Jackie Liua197f662019-11-08 08:09:12 -0700797 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -0700798 int ret;
799
800 ret = hrtimer_try_to_cancel(&req->timeout.timer);
801 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -0700802 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -0700803 io_commit_cqring(ctx);
804 req->flags &= ~REQ_F_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +0800805 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -0700806 return true;
807 }
808
809 return false;
810}
811
Jens Axboeba816ad2019-09-28 11:36:45 -0600812static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -0600813{
Jens Axboe2665abf2019-11-05 12:40:47 -0700814 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -0600815 struct io_kiocb *nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -0700816 bool wake_ev = false;
Jens Axboe9e645e112019-05-10 16:07:28 -0600817
818 /*
819 * The list should never be empty when we are called here. But could
820 * potentially happen if the chain is messed up, check to be on the
821 * safe side.
822 */
823 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, list);
Jens Axboe2665abf2019-11-05 12:40:47 -0700824 while (nxt) {
Jens Axboe9e645e112019-05-10 16:07:28 -0600825 list_del(&nxt->list);
826 if (!list_empty(&req->link_list)) {
827 INIT_LIST_HEAD(&nxt->link_list);
828 list_splice(&req->link_list, &nxt->link_list);
829 nxt->flags |= REQ_F_LINK;
830 }
831
Jens Axboeba816ad2019-09-28 11:36:45 -0600832 /*
833 * If we're in async work, we can continue processing the chain
834 * in this context instead of having to queue up new async work.
835 */
Jens Axboe2665abf2019-11-05 12:40:47 -0700836 if (req->flags & REQ_F_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -0700837 wake_ev = io_link_cancel_timeout(nxt);
Jens Axboe2665abf2019-11-05 12:40:47 -0700838
839 /* we dropped this link, get next */
840 nxt = list_first_entry_or_null(&req->link_list,
841 struct io_kiocb, list);
842 } else if (nxtptr && current_work()) {
Jens Axboeba816ad2019-09-28 11:36:45 -0600843 *nxtptr = nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -0700844 break;
845 } else {
Jackie Liua197f662019-11-08 08:09:12 -0700846 io_queue_async_work(nxt);
Jens Axboe2665abf2019-11-05 12:40:47 -0700847 break;
848 }
Jens Axboe9e645e112019-05-10 16:07:28 -0600849 }
Jens Axboe2665abf2019-11-05 12:40:47 -0700850
851 if (wake_ev)
852 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -0600853}
854
855/*
856 * Called if REQ_F_LINK is set, and we fail the head request
857 */
858static void io_fail_links(struct io_kiocb *req)
859{
Jens Axboe2665abf2019-11-05 12:40:47 -0700860 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -0600861 struct io_kiocb *link;
Jens Axboe2665abf2019-11-05 12:40:47 -0700862 unsigned long flags;
863
864 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -0600865
866 while (!list_empty(&req->link_list)) {
867 link = list_first_entry(&req->link_list, struct io_kiocb, list);
Jens Axboe2665abf2019-11-05 12:40:47 -0700868 list_del_init(&link->list);
Jens Axboe9e645e112019-05-10 16:07:28 -0600869
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +0200870 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -0700871
872 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
873 link->submit.sqe->opcode == IORING_OP_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -0700874 io_link_cancel_timeout(link);
Jens Axboe2665abf2019-11-05 12:40:47 -0700875 } else {
Jens Axboe78e19bb2019-11-06 15:21:34 -0700876 io_cqring_fill_event(link, -ECANCELED);
877 io_double_put_req(link);
Jens Axboe2665abf2019-11-05 12:40:47 -0700878 }
Jens Axboe9e645e112019-05-10 16:07:28 -0600879 }
Jens Axboe2665abf2019-11-05 12:40:47 -0700880
881 io_commit_cqring(ctx);
882 spin_unlock_irqrestore(&ctx->completion_lock, flags);
883 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -0600884}
885
Jens Axboeba816ad2019-09-28 11:36:45 -0600886static void io_free_req(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -0600887{
Jens Axboe2665abf2019-11-05 12:40:47 -0700888 if (likely(!(req->flags & REQ_F_LINK))) {
889 __io_free_req(req);
890 return;
891 }
892
Jens Axboe9e645e112019-05-10 16:07:28 -0600893 /*
894 * If LINK is set, we have dependent requests in this chain. If we
895 * didn't fail this request, queue the first one up, moving any other
896 * dependencies to the next request. In case of failure, fail the rest
897 * of the chain.
898 */
Jens Axboe2665abf2019-11-05 12:40:47 -0700899 if (req->flags & REQ_F_FAIL_LINK) {
900 io_fail_links(req);
901 } else if (req->flags & REQ_F_LINK_TIMEOUT) {
902 struct io_ring_ctx *ctx = req->ctx;
903 unsigned long flags;
904
905 /*
906 * If this is a timeout link, we could be racing with the
907 * timeout timer. Grab the completion lock for this case to
908 * protection against that.
909 */
910 spin_lock_irqsave(&ctx->completion_lock, flags);
911 io_req_link_next(req, nxt);
912 spin_unlock_irqrestore(&ctx->completion_lock, flags);
913 } else {
914 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -0600915 }
916
917 __io_free_req(req);
918}
919
Jens Axboeba816ad2019-09-28 11:36:45 -0600920/*
921 * Drop reference to request, return next in chain (if there is one) if this
922 * was the last reference to this request.
923 */
Jackie Liuec9c02a2019-11-08 23:50:36 +0800924static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboee65ef562019-03-12 10:16:44 -0600925{
Jens Axboeba816ad2019-09-28 11:36:45 -0600926 struct io_kiocb *nxt = NULL;
927
Jens Axboee65ef562019-03-12 10:16:44 -0600928 if (refcount_dec_and_test(&req->refs))
Jens Axboeba816ad2019-09-28 11:36:45 -0600929 io_free_req(req, &nxt);
930
Jens Axboeba816ad2019-09-28 11:36:45 -0600931 if (nxt) {
Jens Axboe561fb042019-10-24 07:25:42 -0600932 if (nxtptr)
Jens Axboeba816ad2019-09-28 11:36:45 -0600933 *nxtptr = nxt;
Jens Axboe561fb042019-10-24 07:25:42 -0600934 else
Jackie Liua197f662019-11-08 08:09:12 -0700935 io_queue_async_work(nxt);
Jens Axboeba816ad2019-09-28 11:36:45 -0600936 }
Jens Axboe2b188cc2019-01-07 10:46:33 -0700937}
938
Jackie Liuec9c02a2019-11-08 23:50:36 +0800939static void io_put_req(struct io_kiocb *req)
940{
941 if (refcount_dec_and_test(&req->refs))
942 io_free_req(req, NULL);
943}
944
Jens Axboe78e19bb2019-11-06 15:21:34 -0700945static void io_double_put_req(struct io_kiocb *req)
946{
947 /* drop both submit and complete references */
948 if (refcount_sub_and_test(2, &req->refs))
949 __io_free_req(req);
950}
951
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700952static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -0600953{
Jens Axboe84f97dc2019-11-06 11:27:53 -0700954 struct io_rings *rings = ctx->rings;
955
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700956 /*
957 * noflush == true is from the waitqueue handler, just ensure we wake
958 * up the task, and the next invocation will flush the entries. We
959 * cannot safely to it from here.
960 */
961 if (noflush && !list_empty(&ctx->cq_overflow_list))
962 return -1U;
963
964 io_cqring_overflow_flush(ctx, false);
965
Jens Axboea3a0e432019-08-20 11:03:11 -0600966 /* See comment at the top of this file */
967 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +0000968 return READ_ONCE(rings->cq.tail) - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -0600969}
970
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +0300971static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
972{
973 struct io_rings *rings = ctx->rings;
974
975 /* make sure SQ entry isn't read before tail */
976 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
977}
978
Jens Axboedef596e2019-01-09 08:59:42 -0700979/*
980 * Find and free completed poll iocbs
981 */
982static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
983 struct list_head *done)
984{
985 void *reqs[IO_IOPOLL_BATCH];
986 struct io_kiocb *req;
Jens Axboe09bb8392019-03-13 12:39:28 -0600987 int to_free;
Jens Axboedef596e2019-01-09 08:59:42 -0700988
Jens Axboe09bb8392019-03-13 12:39:28 -0600989 to_free = 0;
Jens Axboedef596e2019-01-09 08:59:42 -0700990 while (!list_empty(done)) {
991 req = list_first_entry(done, struct io_kiocb, list);
992 list_del(&req->list);
993
Jens Axboe78e19bb2019-11-06 15:21:34 -0700994 io_cqring_fill_event(req, req->result);
Jens Axboedef596e2019-01-09 08:59:42 -0700995 (*nr_events)++;
996
Jens Axboe09bb8392019-03-13 12:39:28 -0600997 if (refcount_dec_and_test(&req->refs)) {
998 /* If we're not using fixed files, we have to pair the
999 * completion part with the file put. Use regular
1000 * completions for those, only batch free for fixed
Jens Axboe9e645e112019-05-10 16:07:28 -06001001 * file and non-linked commands.
Jens Axboe09bb8392019-03-13 12:39:28 -06001002 */
Jens Axboe9e645e112019-05-10 16:07:28 -06001003 if ((req->flags & (REQ_F_FIXED_FILE|REQ_F_LINK)) ==
1004 REQ_F_FIXED_FILE) {
Jens Axboe09bb8392019-03-13 12:39:28 -06001005 reqs[to_free++] = req;
1006 if (to_free == ARRAY_SIZE(reqs))
1007 io_free_req_many(ctx, reqs, &to_free);
Jens Axboe6b063142019-01-10 22:13:58 -07001008 } else {
Jens Axboeba816ad2019-09-28 11:36:45 -06001009 io_free_req(req, NULL);
Jens Axboe6b063142019-01-10 22:13:58 -07001010 }
Jens Axboe9a56a232019-01-09 09:06:50 -07001011 }
Jens Axboedef596e2019-01-09 08:59:42 -07001012 }
Jens Axboedef596e2019-01-09 08:59:42 -07001013
Jens Axboe09bb8392019-03-13 12:39:28 -06001014 io_commit_cqring(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07001015 io_free_req_many(ctx, reqs, &to_free);
1016}
1017
1018static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1019 long min)
1020{
1021 struct io_kiocb *req, *tmp;
1022 LIST_HEAD(done);
1023 bool spin;
1024 int ret;
1025
1026 /*
1027 * Only spin for completions if we don't have multiple devices hanging
1028 * off our complete list, and we're under the requested amount.
1029 */
1030 spin = !ctx->poll_multi_file && *nr_events < min;
1031
1032 ret = 0;
1033 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
1034 struct kiocb *kiocb = &req->rw;
1035
1036 /*
1037 * Move completed entries to our local list. If we find a
1038 * request that requires polling, break out and complete
1039 * the done list first, if we have entries there.
1040 */
1041 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
1042 list_move_tail(&req->list, &done);
1043 continue;
1044 }
1045 if (!list_empty(&done))
1046 break;
1047
1048 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1049 if (ret < 0)
1050 break;
1051
1052 if (ret && spin)
1053 spin = false;
1054 ret = 0;
1055 }
1056
1057 if (!list_empty(&done))
1058 io_iopoll_complete(ctx, nr_events, &done);
1059
1060 return ret;
1061}
1062
1063/*
1064 * Poll for a mininum of 'min' events. Note that if min == 0 we consider that a
1065 * non-spinning poll check - we'll still enter the driver poll loop, but only
1066 * as a non-spinning completion check.
1067 */
1068static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
1069 long min)
1070{
Jens Axboe08f54392019-08-21 22:19:11 -06001071 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07001072 int ret;
1073
1074 ret = io_do_iopoll(ctx, nr_events, min);
1075 if (ret < 0)
1076 return ret;
1077 if (!min || *nr_events >= min)
1078 return 0;
1079 }
1080
1081 return 1;
1082}
1083
1084/*
1085 * We can't just wait for polled events to come to us, we have to actively
1086 * find and complete them.
1087 */
1088static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
1089{
1090 if (!(ctx->flags & IORING_SETUP_IOPOLL))
1091 return;
1092
1093 mutex_lock(&ctx->uring_lock);
1094 while (!list_empty(&ctx->poll_list)) {
1095 unsigned int nr_events = 0;
1096
1097 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -06001098
1099 /*
1100 * Ensure we allow local-to-the-cpu processing to take place,
1101 * in this case we need to ensure that we reap all events.
1102 */
1103 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -07001104 }
1105 mutex_unlock(&ctx->uring_lock);
1106}
1107
Jens Axboe2b2ed972019-10-25 10:06:15 -06001108static int __io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1109 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07001110{
Jens Axboe2b2ed972019-10-25 10:06:15 -06001111 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001112
1113 do {
1114 int tmin = 0;
1115
Jens Axboe500f9fb2019-08-19 12:15:59 -06001116 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06001117 * Don't enter poll loop if we already have events pending.
1118 * If we do, we can potentially be spinning for commands that
1119 * already triggered a CQE (eg in error).
1120 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001121 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06001122 break;
1123
1124 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06001125 * If a submit got punted to a workqueue, we can have the
1126 * application entering polling for a command before it gets
1127 * issued. That app will hold the uring_lock for the duration
1128 * of the poll right here, so we need to take a breather every
1129 * now and then to ensure that the issue has a chance to add
1130 * the poll to the issued list. Otherwise we can spin here
1131 * forever, while the workqueue is stuck trying to acquire the
1132 * very same mutex.
1133 */
1134 if (!(++iters & 7)) {
1135 mutex_unlock(&ctx->uring_lock);
1136 mutex_lock(&ctx->uring_lock);
1137 }
1138
Jens Axboedef596e2019-01-09 08:59:42 -07001139 if (*nr_events < min)
1140 tmin = min - *nr_events;
1141
1142 ret = io_iopoll_getevents(ctx, nr_events, tmin);
1143 if (ret <= 0)
1144 break;
1145 ret = 0;
1146 } while (min && !*nr_events && !need_resched());
1147
Jens Axboe2b2ed972019-10-25 10:06:15 -06001148 return ret;
1149}
1150
1151static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1152 long min)
1153{
1154 int ret;
1155
1156 /*
1157 * We disallow the app entering submit/complete with polling, but we
1158 * still need to lock the ring to prevent racing with polled issue
1159 * that got punted to a workqueue.
1160 */
1161 mutex_lock(&ctx->uring_lock);
1162 ret = __io_iopoll_check(ctx, nr_events, min);
Jens Axboe500f9fb2019-08-19 12:15:59 -06001163 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001164 return ret;
1165}
1166
Jens Axboe491381ce2019-10-17 09:20:46 -06001167static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001168{
Jens Axboe491381ce2019-10-17 09:20:46 -06001169 /*
1170 * Tell lockdep we inherited freeze protection from submission
1171 * thread.
1172 */
1173 if (req->flags & REQ_F_ISREG) {
1174 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001175
Jens Axboe491381ce2019-10-17 09:20:46 -06001176 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001177 }
Jens Axboe491381ce2019-10-17 09:20:46 -06001178 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001179}
1180
Jens Axboeba816ad2019-09-28 11:36:45 -06001181static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001182{
1183 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1184
Jens Axboe491381ce2019-10-17 09:20:46 -06001185 if (kiocb->ki_flags & IOCB_WRITE)
1186 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001187
Jens Axboe9e645e112019-05-10 16:07:28 -06001188 if ((req->flags & REQ_F_LINK) && res != req->result)
1189 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001190 io_cqring_add_event(req, res);
Jens Axboeba816ad2019-09-28 11:36:45 -06001191}
1192
1193static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1194{
1195 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1196
1197 io_complete_rw_common(kiocb, res);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001198 io_put_req(req);
Jens Axboeba816ad2019-09-28 11:36:45 -06001199}
1200
1201static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res)
1202{
1203 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001204 struct io_kiocb *nxt = NULL;
Jens Axboeba816ad2019-09-28 11:36:45 -06001205
1206 io_complete_rw_common(kiocb, res);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001207 io_put_req_find_next(req, &nxt);
1208
1209 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001210}
1211
Jens Axboedef596e2019-01-09 08:59:42 -07001212static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1213{
1214 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1215
Jens Axboe491381ce2019-10-17 09:20:46 -06001216 if (kiocb->ki_flags & IOCB_WRITE)
1217 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001218
Jens Axboe9e645e112019-05-10 16:07:28 -06001219 if ((req->flags & REQ_F_LINK) && res != req->result)
1220 req->flags |= REQ_F_FAIL_LINK;
1221 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -07001222 if (res != -EAGAIN)
1223 req->flags |= REQ_F_IOPOLL_COMPLETED;
1224}
1225
1226/*
1227 * After the iocb has been issued, it's safe to be found on the poll list.
1228 * Adding the kiocb to the list AFTER submission ensures that we don't
1229 * find it from a io_iopoll_getevents() thread before the issuer is done
1230 * accessing the kiocb cookie.
1231 */
1232static void io_iopoll_req_issued(struct io_kiocb *req)
1233{
1234 struct io_ring_ctx *ctx = req->ctx;
1235
1236 /*
1237 * Track whether we have multiple files in our lists. This will impact
1238 * how we do polling eventually, not spinning if we're on potentially
1239 * different devices.
1240 */
1241 if (list_empty(&ctx->poll_list)) {
1242 ctx->poll_multi_file = false;
1243 } else if (!ctx->poll_multi_file) {
1244 struct io_kiocb *list_req;
1245
1246 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1247 list);
1248 if (list_req->rw.ki_filp != req->rw.ki_filp)
1249 ctx->poll_multi_file = true;
1250 }
1251
1252 /*
1253 * For fast devices, IO may have already completed. If it has, add
1254 * it to the front so we find it first.
1255 */
1256 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1257 list_add(&req->list, &ctx->poll_list);
1258 else
1259 list_add_tail(&req->list, &ctx->poll_list);
1260}
1261
Jens Axboe3d6770f2019-04-13 11:50:54 -06001262static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07001263{
Jens Axboe3d6770f2019-04-13 11:50:54 -06001264 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -07001265 int diff = state->has_refs - state->used_refs;
1266
1267 if (diff)
1268 fput_many(state->file, diff);
1269 state->file = NULL;
1270 }
1271}
1272
1273/*
1274 * Get as many references to a file as we have IOs left in this submission,
1275 * assuming most submissions are for one file, or at least that each file
1276 * has more than one submission.
1277 */
1278static struct file *io_file_get(struct io_submit_state *state, int fd)
1279{
1280 if (!state)
1281 return fget(fd);
1282
1283 if (state->file) {
1284 if (state->fd == fd) {
1285 state->used_refs++;
1286 state->ios_left--;
1287 return state->file;
1288 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06001289 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07001290 }
1291 state->file = fget_many(fd, state->ios_left);
1292 if (!state->file)
1293 return NULL;
1294
1295 state->fd = fd;
1296 state->has_refs = state->ios_left;
1297 state->used_refs = 1;
1298 state->ios_left--;
1299 return state->file;
1300}
1301
Jens Axboe2b188cc2019-01-07 10:46:33 -07001302/*
1303 * If we tracked the file through the SCM inflight mechanism, we could support
1304 * any file. For now, just ensure that anything potentially problematic is done
1305 * inline.
1306 */
1307static bool io_file_supports_async(struct file *file)
1308{
1309 umode_t mode = file_inode(file)->i_mode;
1310
1311 if (S_ISBLK(mode) || S_ISCHR(mode))
1312 return true;
1313 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
1314 return true;
1315
1316 return false;
1317}
1318
Pavel Begunkov267bc902019-11-07 01:41:08 +03001319static int io_prep_rw(struct io_kiocb *req, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001320{
Pavel Begunkov267bc902019-11-07 01:41:08 +03001321 const struct io_uring_sqe *sqe = req->submit.sqe;
Jens Axboedef596e2019-01-09 08:59:42 -07001322 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001323 struct kiocb *kiocb = &req->rw;
Jens Axboe09bb8392019-03-13 12:39:28 -06001324 unsigned ioprio;
1325 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001326
Jens Axboe09bb8392019-03-13 12:39:28 -06001327 if (!req->file)
1328 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001329
Jens Axboe491381ce2019-10-17 09:20:46 -06001330 if (S_ISREG(file_inode(req->file)->i_mode))
1331 req->flags |= REQ_F_ISREG;
1332
1333 /*
1334 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
1335 * we know to async punt it even if it was opened O_NONBLOCK
1336 */
1337 if (force_nonblock && !io_file_supports_async(req->file)) {
1338 req->flags |= REQ_F_MUST_PUNT;
1339 return -EAGAIN;
1340 }
Jens Axboe6b063142019-01-10 22:13:58 -07001341
Jens Axboe2b188cc2019-01-07 10:46:33 -07001342 kiocb->ki_pos = READ_ONCE(sqe->off);
1343 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
1344 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
1345
1346 ioprio = READ_ONCE(sqe->ioprio);
1347 if (ioprio) {
1348 ret = ioprio_check_cap(ioprio);
1349 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06001350 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001351
1352 kiocb->ki_ioprio = ioprio;
1353 } else
1354 kiocb->ki_ioprio = get_current_ioprio();
1355
1356 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
1357 if (unlikely(ret))
Jens Axboe09bb8392019-03-13 12:39:28 -06001358 return ret;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001359
1360 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06001361 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
1362 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02001363 req->flags |= REQ_F_NOWAIT;
1364
1365 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001366 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001367
Jens Axboedef596e2019-01-09 08:59:42 -07001368 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07001369 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
1370 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06001371 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001372
Jens Axboedef596e2019-01-09 08:59:42 -07001373 kiocb->ki_flags |= IOCB_HIPRI;
1374 kiocb->ki_complete = io_complete_rw_iopoll;
1375 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001376 if (kiocb->ki_flags & IOCB_HIPRI)
1377 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07001378 kiocb->ki_complete = io_complete_rw;
1379 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001380 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001381}
1382
1383static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
1384{
1385 switch (ret) {
1386 case -EIOCBQUEUED:
1387 break;
1388 case -ERESTARTSYS:
1389 case -ERESTARTNOINTR:
1390 case -ERESTARTNOHAND:
1391 case -ERESTART_RESTARTBLOCK:
1392 /*
1393 * We can't just restart the syscall, since previously
1394 * submitted sqes may already be in progress. Just fail this
1395 * IO with EINTR.
1396 */
1397 ret = -EINTR;
1398 /* fall through */
1399 default:
1400 kiocb->ki_complete(kiocb, ret, 0);
1401 }
1402}
1403
Jens Axboeba816ad2019-09-28 11:36:45 -06001404static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt,
1405 bool in_async)
1406{
1407 if (in_async && ret >= 0 && nxt && kiocb->ki_complete == io_complete_rw)
1408 *nxt = __io_complete_rw(kiocb, ret);
1409 else
1410 io_rw_done(kiocb, ret);
1411}
1412
Jens Axboeedafcce2019-01-09 09:16:05 -07001413static int io_import_fixed(struct io_ring_ctx *ctx, int rw,
1414 const struct io_uring_sqe *sqe,
1415 struct iov_iter *iter)
1416{
1417 size_t len = READ_ONCE(sqe->len);
1418 struct io_mapped_ubuf *imu;
1419 unsigned index, buf_index;
1420 size_t offset;
1421 u64 buf_addr;
1422
1423 /* attempt to use fixed buffers without having provided iovecs */
1424 if (unlikely(!ctx->user_bufs))
1425 return -EFAULT;
1426
1427 buf_index = READ_ONCE(sqe->buf_index);
1428 if (unlikely(buf_index >= ctx->nr_user_bufs))
1429 return -EFAULT;
1430
1431 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
1432 imu = &ctx->user_bufs[index];
1433 buf_addr = READ_ONCE(sqe->addr);
1434
1435 /* overflow */
1436 if (buf_addr + len < buf_addr)
1437 return -EFAULT;
1438 /* not inside the mapped region */
1439 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
1440 return -EFAULT;
1441
1442 /*
1443 * May not be a start of buffer, set size appropriately
1444 * and advance us to the beginning.
1445 */
1446 offset = buf_addr - imu->ubuf;
1447 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06001448
1449 if (offset) {
1450 /*
1451 * Don't use iov_iter_advance() here, as it's really slow for
1452 * using the latter parts of a big fixed buffer - it iterates
1453 * over each segment manually. We can cheat a bit here, because
1454 * we know that:
1455 *
1456 * 1) it's a BVEC iter, we set it up
1457 * 2) all bvecs are PAGE_SIZE in size, except potentially the
1458 * first and last bvec
1459 *
1460 * So just find our index, and adjust the iterator afterwards.
1461 * If the offset is within the first bvec (or the whole first
1462 * bvec, just use iov_iter_advance(). This makes it easier
1463 * since we can just skip the first segment, which may not
1464 * be PAGE_SIZE aligned.
1465 */
1466 const struct bio_vec *bvec = imu->bvec;
1467
1468 if (offset <= bvec->bv_len) {
1469 iov_iter_advance(iter, offset);
1470 } else {
1471 unsigned long seg_skip;
1472
1473 /* skip first vec */
1474 offset -= bvec->bv_len;
1475 seg_skip = 1 + (offset >> PAGE_SHIFT);
1476
1477 iter->bvec = bvec + seg_skip;
1478 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02001479 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001480 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001481 }
1482 }
1483
Jens Axboeedafcce2019-01-09 09:16:05 -07001484 return 0;
1485}
1486
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001487static ssize_t io_import_iovec(struct io_ring_ctx *ctx, int rw,
1488 const struct sqe_submit *s, struct iovec **iovec,
1489 struct iov_iter *iter)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001490{
1491 const struct io_uring_sqe *sqe = s->sqe;
1492 void __user *buf = u64_to_user_ptr(READ_ONCE(sqe->addr));
1493 size_t sqe_len = READ_ONCE(sqe->len);
Jens Axboeedafcce2019-01-09 09:16:05 -07001494 u8 opcode;
1495
1496 /*
1497 * We're reading ->opcode for the second time, but the first read
1498 * doesn't care whether it's _FIXED or not, so it doesn't matter
1499 * whether ->opcode changes concurrently. The first read does care
1500 * about whether it is a READ or a WRITE, so we don't trust this read
1501 * for that purpose and instead let the caller pass in the read/write
1502 * flag.
1503 */
1504 opcode = READ_ONCE(sqe->opcode);
1505 if (opcode == IORING_OP_READ_FIXED ||
1506 opcode == IORING_OP_WRITE_FIXED) {
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001507 ssize_t ret = io_import_fixed(ctx, rw, sqe, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07001508 *iovec = NULL;
1509 return ret;
1510 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001511
1512 if (!s->has_user)
1513 return -EFAULT;
1514
1515#ifdef CONFIG_COMPAT
1516 if (ctx->compat)
1517 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
1518 iovec, iter);
1519#endif
1520
1521 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
1522}
1523
Jens Axboe32960612019-09-23 11:05:34 -06001524/*
1525 * For files that don't have ->read_iter() and ->write_iter(), handle them
1526 * by looping over ->read() or ->write() manually.
1527 */
1528static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
1529 struct iov_iter *iter)
1530{
1531 ssize_t ret = 0;
1532
1533 /*
1534 * Don't support polled IO through this interface, and we can't
1535 * support non-blocking either. For the latter, this just causes
1536 * the kiocb to be handled from an async context.
1537 */
1538 if (kiocb->ki_flags & IOCB_HIPRI)
1539 return -EOPNOTSUPP;
1540 if (kiocb->ki_flags & IOCB_NOWAIT)
1541 return -EAGAIN;
1542
1543 while (iov_iter_count(iter)) {
1544 struct iovec iovec = iov_iter_iovec(iter);
1545 ssize_t nr;
1546
1547 if (rw == READ) {
1548 nr = file->f_op->read(file, iovec.iov_base,
1549 iovec.iov_len, &kiocb->ki_pos);
1550 } else {
1551 nr = file->f_op->write(file, iovec.iov_base,
1552 iovec.iov_len, &kiocb->ki_pos);
1553 }
1554
1555 if (nr < 0) {
1556 if (!ret)
1557 ret = nr;
1558 break;
1559 }
1560 ret += nr;
1561 if (nr != iovec.iov_len)
1562 break;
1563 iov_iter_advance(iter, nr);
1564 }
1565
1566 return ret;
1567}
1568
Pavel Begunkov267bc902019-11-07 01:41:08 +03001569static int io_read(struct io_kiocb *req, struct io_kiocb **nxt,
1570 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001571{
1572 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1573 struct kiocb *kiocb = &req->rw;
1574 struct iov_iter iter;
1575 struct file *file;
Jens Axboe31b51512019-01-18 22:56:34 -07001576 size_t iov_count;
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001577 ssize_t read_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001578
Pavel Begunkov267bc902019-11-07 01:41:08 +03001579 ret = io_prep_rw(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001580 if (ret)
1581 return ret;
1582 file = kiocb->ki_filp;
1583
Jens Axboe2b188cc2019-01-07 10:46:33 -07001584 if (unlikely(!(file->f_mode & FMODE_READ)))
Jens Axboe09bb8392019-03-13 12:39:28 -06001585 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001586
Pavel Begunkov267bc902019-11-07 01:41:08 +03001587 ret = io_import_iovec(req->ctx, READ, &req->submit, &iovec, &iter);
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001588 if (ret < 0)
Jens Axboe09bb8392019-03-13 12:39:28 -06001589 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001590
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001591 read_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06001592 if (req->flags & REQ_F_LINK)
1593 req->result = read_size;
1594
Jens Axboe31b51512019-01-18 22:56:34 -07001595 iov_count = iov_iter_count(&iter);
1596 ret = rw_verify_area(READ, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001597 if (!ret) {
1598 ssize_t ret2;
1599
Jens Axboe32960612019-09-23 11:05:34 -06001600 if (file->f_op->read_iter)
1601 ret2 = call_read_iter(file, kiocb, &iter);
1602 else
1603 ret2 = loop_rw_iter(READ, file, kiocb, &iter);
1604
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001605 /*
1606 * In case of a short read, punt to async. This can happen
1607 * if we have data partially cached. Alternatively we can
1608 * return the short read, in which case the application will
1609 * need to issue another SQE and wait for it. That SQE will
1610 * need async punt anyway, so it's more efficient to do it
1611 * here.
1612 */
Jens Axboe491381ce2019-10-17 09:20:46 -06001613 if (force_nonblock && !(req->flags & REQ_F_NOWAIT) &&
1614 (req->flags & REQ_F_ISREG) &&
1615 ret2 > 0 && ret2 < read_size)
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001616 ret2 = -EAGAIN;
1617 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboe561fb042019-10-24 07:25:42 -06001618 if (!force_nonblock || ret2 != -EAGAIN)
Pavel Begunkov267bc902019-11-07 01:41:08 +03001619 kiocb_done(kiocb, ret2, nxt, req->submit.in_async);
Jens Axboe561fb042019-10-24 07:25:42 -06001620 else
Jens Axboe2b188cc2019-01-07 10:46:33 -07001621 ret = -EAGAIN;
1622 }
1623 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001624 return ret;
1625}
1626
Pavel Begunkov267bc902019-11-07 01:41:08 +03001627static int io_write(struct io_kiocb *req, struct io_kiocb **nxt,
1628 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001629{
1630 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1631 struct kiocb *kiocb = &req->rw;
1632 struct iov_iter iter;
1633 struct file *file;
Jens Axboe31b51512019-01-18 22:56:34 -07001634 size_t iov_count;
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001635 ssize_t ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001636
Pavel Begunkov267bc902019-11-07 01:41:08 +03001637 ret = io_prep_rw(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001638 if (ret)
1639 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001640
Jens Axboe2b188cc2019-01-07 10:46:33 -07001641 file = kiocb->ki_filp;
1642 if (unlikely(!(file->f_mode & FMODE_WRITE)))
Jens Axboe09bb8392019-03-13 12:39:28 -06001643 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001644
Pavel Begunkov267bc902019-11-07 01:41:08 +03001645 ret = io_import_iovec(req->ctx, WRITE, &req->submit, &iovec, &iter);
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001646 if (ret < 0)
Jens Axboe09bb8392019-03-13 12:39:28 -06001647 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001648
Jens Axboe9e645e112019-05-10 16:07:28 -06001649 if (req->flags & REQ_F_LINK)
1650 req->result = ret;
1651
Jens Axboe31b51512019-01-18 22:56:34 -07001652 iov_count = iov_iter_count(&iter);
1653
1654 ret = -EAGAIN;
Jens Axboe561fb042019-10-24 07:25:42 -06001655 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT))
Jens Axboe31b51512019-01-18 22:56:34 -07001656 goto out_free;
Jens Axboe31b51512019-01-18 22:56:34 -07001657
1658 ret = rw_verify_area(WRITE, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001659 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01001660 ssize_t ret2;
1661
Jens Axboe2b188cc2019-01-07 10:46:33 -07001662 /*
1663 * Open-code file_start_write here to grab freeze protection,
1664 * which will be released by another thread in
1665 * io_complete_rw(). Fool lockdep by telling it the lock got
1666 * released so that it doesn't complain about the held lock when
1667 * we return to userspace.
1668 */
Jens Axboe491381ce2019-10-17 09:20:46 -06001669 if (req->flags & REQ_F_ISREG) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001670 __sb_start_write(file_inode(file)->i_sb,
1671 SB_FREEZE_WRITE, true);
1672 __sb_writers_release(file_inode(file)->i_sb,
1673 SB_FREEZE_WRITE);
1674 }
1675 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01001676
Jens Axboe32960612019-09-23 11:05:34 -06001677 if (file->f_op->write_iter)
1678 ret2 = call_write_iter(file, kiocb, &iter);
1679 else
1680 ret2 = loop_rw_iter(WRITE, file, kiocb, &iter);
Jens Axboe561fb042019-10-24 07:25:42 -06001681 if (!force_nonblock || ret2 != -EAGAIN)
Pavel Begunkov267bc902019-11-07 01:41:08 +03001682 kiocb_done(kiocb, ret2, nxt, req->submit.in_async);
Jens Axboe561fb042019-10-24 07:25:42 -06001683 else
Roman Penyaev9bf79332019-03-25 20:09:24 +01001684 ret = -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001685 }
Jens Axboe31b51512019-01-18 22:56:34 -07001686out_free:
Jens Axboe2b188cc2019-01-07 10:46:33 -07001687 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001688 return ret;
1689}
1690
1691/*
1692 * IORING_OP_NOP just posts a completion event, nothing else.
1693 */
Jens Axboe78e19bb2019-11-06 15:21:34 -07001694static int io_nop(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001695{
1696 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001697
Jens Axboedef596e2019-01-09 08:59:42 -07001698 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1699 return -EINVAL;
1700
Jens Axboe78e19bb2019-11-06 15:21:34 -07001701 io_cqring_add_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001702 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001703 return 0;
1704}
1705
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001706static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1707{
Jens Axboe6b063142019-01-10 22:13:58 -07001708 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001709
Jens Axboe09bb8392019-03-13 12:39:28 -06001710 if (!req->file)
1711 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001712
Jens Axboe6b063142019-01-10 22:13:58 -07001713 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07001714 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07001715 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001716 return -EINVAL;
1717
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001718 return 0;
1719}
1720
1721static int io_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001722 struct io_kiocb **nxt, bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001723{
1724 loff_t sqe_off = READ_ONCE(sqe->off);
1725 loff_t sqe_len = READ_ONCE(sqe->len);
1726 loff_t end = sqe_off + sqe_len;
1727 unsigned fsync_flags;
1728 int ret;
1729
1730 fsync_flags = READ_ONCE(sqe->fsync_flags);
1731 if (unlikely(fsync_flags & ~IORING_FSYNC_DATASYNC))
1732 return -EINVAL;
1733
1734 ret = io_prep_fsync(req, sqe);
1735 if (ret)
1736 return ret;
1737
1738 /* fsync always requires a blocking context */
1739 if (force_nonblock)
1740 return -EAGAIN;
1741
1742 ret = vfs_fsync_range(req->rw.ki_filp, sqe_off,
1743 end > 0 ? end : LLONG_MAX,
1744 fsync_flags & IORING_FSYNC_DATASYNC);
1745
Jens Axboe9e645e112019-05-10 16:07:28 -06001746 if (ret < 0 && (req->flags & REQ_F_LINK))
1747 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001748 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001749 io_put_req_find_next(req, nxt);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001750 return 0;
1751}
1752
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001753static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1754{
1755 struct io_ring_ctx *ctx = req->ctx;
1756 int ret = 0;
1757
1758 if (!req->file)
1759 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001760
1761 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1762 return -EINVAL;
1763 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
1764 return -EINVAL;
1765
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001766 return ret;
1767}
1768
1769static int io_sync_file_range(struct io_kiocb *req,
1770 const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001771 struct io_kiocb **nxt,
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001772 bool force_nonblock)
1773{
1774 loff_t sqe_off;
1775 loff_t sqe_len;
1776 unsigned flags;
1777 int ret;
1778
1779 ret = io_prep_sfr(req, sqe);
1780 if (ret)
1781 return ret;
1782
1783 /* sync_file_range always requires a blocking context */
1784 if (force_nonblock)
1785 return -EAGAIN;
1786
1787 sqe_off = READ_ONCE(sqe->off);
1788 sqe_len = READ_ONCE(sqe->len);
1789 flags = READ_ONCE(sqe->sync_range_flags);
1790
1791 ret = sync_file_range(req->rw.ki_filp, sqe_off, sqe_len, flags);
1792
Jens Axboe9e645e112019-05-10 16:07:28 -06001793 if (ret < 0 && (req->flags & REQ_F_LINK))
1794 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001795 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001796 io_put_req_find_next(req, nxt);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001797 return 0;
1798}
1799
Jens Axboe0fa03c62019-04-19 13:34:07 -06001800#if defined(CONFIG_NET)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001801static int io_send_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001802 struct io_kiocb **nxt, bool force_nonblock,
Jens Axboeaa1fa282019-04-19 13:38:09 -06001803 long (*fn)(struct socket *, struct user_msghdr __user *,
1804 unsigned int))
1805{
Jens Axboe0fa03c62019-04-19 13:34:07 -06001806 struct socket *sock;
1807 int ret;
1808
1809 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1810 return -EINVAL;
1811
1812 sock = sock_from_file(req->file, &ret);
1813 if (sock) {
1814 struct user_msghdr __user *msg;
1815 unsigned flags;
1816
1817 flags = READ_ONCE(sqe->msg_flags);
1818 if (flags & MSG_DONTWAIT)
1819 req->flags |= REQ_F_NOWAIT;
1820 else if (force_nonblock)
1821 flags |= MSG_DONTWAIT;
1822
1823 msg = (struct user_msghdr __user *) (unsigned long)
1824 READ_ONCE(sqe->addr);
1825
Jens Axboeaa1fa282019-04-19 13:38:09 -06001826 ret = fn(sock, msg, flags);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001827 if (force_nonblock && ret == -EAGAIN)
1828 return ret;
1829 }
1830
Jens Axboe78e19bb2019-11-06 15:21:34 -07001831 io_cqring_add_event(req, ret);
Jens Axboef1f40852019-11-05 20:33:16 -07001832 if (ret < 0 && (req->flags & REQ_F_LINK))
1833 req->flags |= REQ_F_FAIL_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001834 io_put_req_find_next(req, nxt);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001835 return 0;
Jens Axboeaa1fa282019-04-19 13:38:09 -06001836}
1837#endif
1838
1839static int io_sendmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001840 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001841{
1842#if defined(CONFIG_NET)
Jens Axboeba816ad2019-09-28 11:36:45 -06001843 return io_send_recvmsg(req, sqe, nxt, force_nonblock,
1844 __sys_sendmsg_sock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06001845#else
1846 return -EOPNOTSUPP;
1847#endif
1848}
1849
1850static int io_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001851 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001852{
1853#if defined(CONFIG_NET)
Jens Axboeba816ad2019-09-28 11:36:45 -06001854 return io_send_recvmsg(req, sqe, nxt, force_nonblock,
1855 __sys_recvmsg_sock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001856#else
1857 return -EOPNOTSUPP;
1858#endif
1859}
1860
Jens Axboe17f2fe32019-10-17 14:42:58 -06001861static int io_accept(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1862 struct io_kiocb **nxt, bool force_nonblock)
1863{
1864#if defined(CONFIG_NET)
1865 struct sockaddr __user *addr;
1866 int __user *addr_len;
1867 unsigned file_flags;
1868 int flags, ret;
1869
1870 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
1871 return -EINVAL;
1872 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
1873 return -EINVAL;
1874
1875 addr = (struct sockaddr __user *) (unsigned long) READ_ONCE(sqe->addr);
1876 addr_len = (int __user *) (unsigned long) READ_ONCE(sqe->addr2);
1877 flags = READ_ONCE(sqe->accept_flags);
1878 file_flags = force_nonblock ? O_NONBLOCK : 0;
1879
1880 ret = __sys_accept4_file(req->file, file_flags, addr, addr_len, flags);
1881 if (ret == -EAGAIN && force_nonblock) {
1882 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
1883 return -EAGAIN;
1884 }
1885 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 Axboe17f2fe32019-10-17 14:42:58 -06001889 return 0;
1890#else
1891 return -EOPNOTSUPP;
1892#endif
1893}
1894
Jens Axboe221c5eb2019-01-17 09:41:58 -07001895static void io_poll_remove_one(struct io_kiocb *req)
1896{
1897 struct io_poll_iocb *poll = &req->poll;
1898
1899 spin_lock(&poll->head->lock);
1900 WRITE_ONCE(poll->canceled, true);
1901 if (!list_empty(&poll->wait.entry)) {
1902 list_del_init(&poll->wait.entry);
Jackie Liua197f662019-11-08 08:09:12 -07001903 io_queue_async_work(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001904 }
1905 spin_unlock(&poll->head->lock);
1906
1907 list_del_init(&req->list);
1908}
1909
1910static void io_poll_remove_all(struct io_ring_ctx *ctx)
1911{
1912 struct io_kiocb *req;
1913
1914 spin_lock_irq(&ctx->completion_lock);
1915 while (!list_empty(&ctx->cancel_list)) {
1916 req = list_first_entry(&ctx->cancel_list, struct io_kiocb,list);
1917 io_poll_remove_one(req);
1918 }
1919 spin_unlock_irq(&ctx->completion_lock);
1920}
1921
1922/*
1923 * Find a running poll command that matches one specified in sqe->addr,
1924 * and remove it if found.
1925 */
1926static int io_poll_remove(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1927{
1928 struct io_ring_ctx *ctx = req->ctx;
1929 struct io_kiocb *poll_req, *next;
1930 int ret = -ENOENT;
1931
1932 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1933 return -EINVAL;
1934 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
1935 sqe->poll_events)
1936 return -EINVAL;
1937
1938 spin_lock_irq(&ctx->completion_lock);
1939 list_for_each_entry_safe(poll_req, next, &ctx->cancel_list, list) {
1940 if (READ_ONCE(sqe->addr) == poll_req->user_data) {
1941 io_poll_remove_one(poll_req);
1942 ret = 0;
1943 break;
1944 }
1945 }
1946 spin_unlock_irq(&ctx->completion_lock);
1947
Jens Axboe78e19bb2019-11-06 15:21:34 -07001948 io_cqring_add_event(req, ret);
Jens Axboef1f40852019-11-05 20:33:16 -07001949 if (ret < 0 && (req->flags & REQ_F_LINK))
1950 req->flags |= REQ_F_FAIL_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001951 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001952 return 0;
1953}
1954
Jackie Liua197f662019-11-08 08:09:12 -07001955static void io_poll_complete(struct io_kiocb *req, __poll_t mask)
Jens Axboe221c5eb2019-01-17 09:41:58 -07001956{
Jackie Liua197f662019-11-08 08:09:12 -07001957 struct io_ring_ctx *ctx = req->ctx;
1958
Jens Axboe8c838782019-03-12 15:48:16 -06001959 req->poll.done = true;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001960 io_cqring_fill_event(req, mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06001961 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001962}
1963
Jens Axboe561fb042019-10-24 07:25:42 -06001964static void io_poll_complete_work(struct io_wq_work **workptr)
Jens Axboe221c5eb2019-01-17 09:41:58 -07001965{
Jens Axboe561fb042019-10-24 07:25:42 -06001966 struct io_wq_work *work = *workptr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001967 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
1968 struct io_poll_iocb *poll = &req->poll;
1969 struct poll_table_struct pt = { ._key = poll->events };
1970 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe89723d02019-11-05 15:32:58 -07001971 struct io_kiocb *nxt = NULL;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001972 __poll_t mask = 0;
1973
Jens Axboe561fb042019-10-24 07:25:42 -06001974 if (work->flags & IO_WQ_WORK_CANCEL)
1975 WRITE_ONCE(poll->canceled, true);
1976
Jens Axboe221c5eb2019-01-17 09:41:58 -07001977 if (!READ_ONCE(poll->canceled))
1978 mask = vfs_poll(poll->file, &pt) & poll->events;
1979
1980 /*
1981 * Note that ->ki_cancel callers also delete iocb from active_reqs after
1982 * calling ->ki_cancel. We need the ctx_lock roundtrip here to
1983 * synchronize with them. In the cancellation case the list_del_init
1984 * itself is not actually needed, but harmless so we keep it in to
1985 * avoid further branches in the fast path.
1986 */
1987 spin_lock_irq(&ctx->completion_lock);
1988 if (!mask && !READ_ONCE(poll->canceled)) {
1989 add_wait_queue(poll->head, &poll->wait);
1990 spin_unlock_irq(&ctx->completion_lock);
1991 return;
1992 }
1993 list_del_init(&req->list);
Jackie Liua197f662019-11-08 08:09:12 -07001994 io_poll_complete(req, mask);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001995 spin_unlock_irq(&ctx->completion_lock);
1996
Jens Axboe8c838782019-03-12 15:48:16 -06001997 io_cqring_ev_posted(ctx);
Jens Axboe89723d02019-11-05 15:32:58 -07001998
Jackie Liuec9c02a2019-11-08 23:50:36 +08001999 io_put_req_find_next(req, &nxt);
Jens Axboe89723d02019-11-05 15:32:58 -07002000 if (nxt)
2001 *workptr = &nxt->work;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002002}
2003
2004static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
2005 void *key)
2006{
2007 struct io_poll_iocb *poll = container_of(wait, struct io_poll_iocb,
2008 wait);
2009 struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
2010 struct io_ring_ctx *ctx = req->ctx;
2011 __poll_t mask = key_to_poll(key);
Jens Axboe8c838782019-03-12 15:48:16 -06002012 unsigned long flags;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002013
2014 /* for instances that support it check for an event match first: */
Jens Axboe8c838782019-03-12 15:48:16 -06002015 if (mask && !(mask & poll->events))
2016 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002017
2018 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06002019
2020 if (mask && spin_trylock_irqsave(&ctx->completion_lock, flags)) {
2021 list_del(&req->list);
Jackie Liua197f662019-11-08 08:09:12 -07002022 io_poll_complete(req, mask);
Jens Axboe8c838782019-03-12 15:48:16 -06002023 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2024
2025 io_cqring_ev_posted(ctx);
Jackie Liuec9c02a2019-11-08 23:50:36 +08002026 io_put_req(req);
Jens Axboe8c838782019-03-12 15:48:16 -06002027 } else {
Jackie Liua197f662019-11-08 08:09:12 -07002028 io_queue_async_work(req);
Jens Axboe8c838782019-03-12 15:48:16 -06002029 }
2030
Jens Axboe221c5eb2019-01-17 09:41:58 -07002031 return 1;
2032}
2033
2034struct io_poll_table {
2035 struct poll_table_struct pt;
2036 struct io_kiocb *req;
2037 int error;
2038};
2039
2040static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
2041 struct poll_table_struct *p)
2042{
2043 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
2044
2045 if (unlikely(pt->req->poll.head)) {
2046 pt->error = -EINVAL;
2047 return;
2048 }
2049
2050 pt->error = 0;
2051 pt->req->poll.head = head;
2052 add_wait_queue(head, &pt->req->poll.wait);
2053}
2054
Jens Axboe89723d02019-11-05 15:32:58 -07002055static int io_poll_add(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2056 struct io_kiocb **nxt)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002057{
2058 struct io_poll_iocb *poll = &req->poll;
2059 struct io_ring_ctx *ctx = req->ctx;
2060 struct io_poll_table ipt;
Jens Axboe8c838782019-03-12 15:48:16 -06002061 bool cancel = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002062 __poll_t mask;
2063 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002064
2065 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2066 return -EINVAL;
2067 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
2068 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06002069 if (!poll->file)
2070 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002071
Jens Axboe6cc47d12019-09-18 11:18:23 -06002072 req->submit.sqe = NULL;
Jens Axboe561fb042019-10-24 07:25:42 -06002073 INIT_IO_WORK(&req->work, io_poll_complete_work);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002074 events = READ_ONCE(sqe->poll_events);
2075 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
2076
Jens Axboe221c5eb2019-01-17 09:41:58 -07002077 poll->head = NULL;
Jens Axboe8c838782019-03-12 15:48:16 -06002078 poll->done = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002079 poll->canceled = false;
2080
2081 ipt.pt._qproc = io_poll_queue_proc;
2082 ipt.pt._key = poll->events;
2083 ipt.req = req;
2084 ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
2085
2086 /* initialized the list so that we can do list_empty checks */
2087 INIT_LIST_HEAD(&poll->wait.entry);
2088 init_waitqueue_func_entry(&poll->wait, io_poll_wake);
2089
Jens Axboe36703242019-07-25 10:20:18 -06002090 INIT_LIST_HEAD(&req->list);
2091
Jens Axboe221c5eb2019-01-17 09:41:58 -07002092 mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002093
2094 spin_lock_irq(&ctx->completion_lock);
Jens Axboe8c838782019-03-12 15:48:16 -06002095 if (likely(poll->head)) {
2096 spin_lock(&poll->head->lock);
2097 if (unlikely(list_empty(&poll->wait.entry))) {
2098 if (ipt.error)
2099 cancel = true;
2100 ipt.error = 0;
2101 mask = 0;
2102 }
2103 if (mask || ipt.error)
2104 list_del_init(&poll->wait.entry);
2105 else if (cancel)
2106 WRITE_ONCE(poll->canceled, true);
2107 else if (!poll->done) /* actually waiting for an event */
2108 list_add_tail(&req->list, &ctx->cancel_list);
2109 spin_unlock(&poll->head->lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002110 }
Jens Axboe8c838782019-03-12 15:48:16 -06002111 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06002112 ipt.error = 0;
Jackie Liua197f662019-11-08 08:09:12 -07002113 io_poll_complete(req, mask);
Jens Axboe8c838782019-03-12 15:48:16 -06002114 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07002115 spin_unlock_irq(&ctx->completion_lock);
2116
Jens Axboe8c838782019-03-12 15:48:16 -06002117 if (mask) {
2118 io_cqring_ev_posted(ctx);
Jackie Liuec9c02a2019-11-08 23:50:36 +08002119 io_put_req_find_next(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002120 }
Jens Axboe8c838782019-03-12 15:48:16 -06002121 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002122}
2123
Jens Axboe5262f562019-09-17 12:26:57 -06002124static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
2125{
2126 struct io_ring_ctx *ctx;
Jens Axboe11365042019-10-16 09:08:32 -06002127 struct io_kiocb *req;
Jens Axboe5262f562019-09-17 12:26:57 -06002128 unsigned long flags;
2129
2130 req = container_of(timer, struct io_kiocb, timeout.timer);
2131 ctx = req->ctx;
2132 atomic_inc(&ctx->cq_timeouts);
2133
2134 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08002135 /*
Jens Axboe11365042019-10-16 09:08:32 -06002136 * We could be racing with timeout deletion. If the list is empty,
2137 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08002138 */
Jens Axboe842f9612019-10-29 12:34:10 -06002139 if (!list_empty(&req->list)) {
Jens Axboe11365042019-10-16 09:08:32 -06002140 struct io_kiocb *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06002141
Jens Axboe11365042019-10-16 09:08:32 -06002142 /*
2143 * Adjust the reqs sequence before the current one because it
2144 * will consume a slot in the cq_ring and the the cq_tail
2145 * pointer will be increased, otherwise other timeout reqs may
2146 * return in advance without waiting for enough wait_nr.
2147 */
2148 prev = req;
2149 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
2150 prev->sequence++;
Jens Axboe11365042019-10-16 09:08:32 -06002151 list_del_init(&req->list);
Jens Axboe11365042019-10-16 09:08:32 -06002152 }
Jens Axboe842f9612019-10-29 12:34:10 -06002153
Jens Axboe78e19bb2019-11-06 15:21:34 -07002154 io_cqring_fill_event(req, -ETIME);
Jens Axboe842f9612019-10-29 12:34:10 -06002155 io_commit_cqring(ctx);
Jens Axboe5262f562019-09-17 12:26:57 -06002156 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2157
Jens Axboe842f9612019-10-29 12:34:10 -06002158 io_cqring_ev_posted(ctx);
Jens Axboef1f40852019-11-05 20:33:16 -07002159 if (req->flags & REQ_F_LINK)
2160 req->flags |= REQ_F_FAIL_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08002161 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06002162 return HRTIMER_NORESTART;
2163}
2164
2165/*
2166 * Remove or update an existing timeout command
2167 */
2168static int io_timeout_remove(struct io_kiocb *req,
2169 const struct io_uring_sqe *sqe)
2170{
2171 struct io_ring_ctx *ctx = req->ctx;
2172 struct io_kiocb *treq;
2173 int ret = -ENOENT;
2174 __u64 user_data;
2175 unsigned flags;
2176
2177 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2178 return -EINVAL;
2179 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
2180 return -EINVAL;
2181 flags = READ_ONCE(sqe->timeout_flags);
2182 if (flags)
2183 return -EINVAL;
2184
2185 user_data = READ_ONCE(sqe->addr);
2186 spin_lock_irq(&ctx->completion_lock);
2187 list_for_each_entry(treq, &ctx->timeout_list, list) {
2188 if (user_data == treq->user_data) {
2189 list_del_init(&treq->list);
2190 ret = 0;
2191 break;
2192 }
2193 }
2194
2195 /* didn't find timeout */
2196 if (ret) {
2197fill_ev:
Jens Axboe78e19bb2019-11-06 15:21:34 -07002198 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06002199 io_commit_cqring(ctx);
2200 spin_unlock_irq(&ctx->completion_lock);
2201 io_cqring_ev_posted(ctx);
Jens Axboef1f40852019-11-05 20:33:16 -07002202 if (req->flags & REQ_F_LINK)
2203 req->flags |= REQ_F_FAIL_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08002204 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06002205 return 0;
2206 }
2207
2208 ret = hrtimer_try_to_cancel(&treq->timeout.timer);
2209 if (ret == -1) {
2210 ret = -EBUSY;
2211 goto fill_ev;
2212 }
2213
Jens Axboe78e19bb2019-11-06 15:21:34 -07002214 io_cqring_fill_event(req, 0);
2215 io_cqring_fill_event(treq, -ECANCELED);
Jens Axboe11365042019-10-16 09:08:32 -06002216 io_commit_cqring(ctx);
2217 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06002218 io_cqring_ev_posted(ctx);
2219
Jackie Liuec9c02a2019-11-08 23:50:36 +08002220 io_put_req(treq);
2221 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06002222 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06002223}
2224
2225static int io_timeout(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2226{
yangerkun5da0fb12019-10-15 21:59:29 +08002227 unsigned count;
Jens Axboe5262f562019-09-17 12:26:57 -06002228 struct io_ring_ctx *ctx = req->ctx;
2229 struct list_head *entry;
Jens Axboea41525a2019-10-15 16:48:15 -06002230 enum hrtimer_mode mode;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06002231 struct timespec64 ts;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002232 unsigned span = 0;
Jens Axboea41525a2019-10-15 16:48:15 -06002233 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06002234
2235 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2236 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06002237 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len != 1)
2238 return -EINVAL;
2239 flags = READ_ONCE(sqe->timeout_flags);
2240 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06002241 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06002242
2243 if (get_timespec64(&ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06002244 return -EFAULT;
2245
Jens Axboe11365042019-10-16 09:08:32 -06002246 if (flags & IORING_TIMEOUT_ABS)
2247 mode = HRTIMER_MODE_ABS;
2248 else
2249 mode = HRTIMER_MODE_REL;
2250
2251 hrtimer_init(&req->timeout.timer, CLOCK_MONOTONIC, mode);
2252
Jens Axboe5262f562019-09-17 12:26:57 -06002253 /*
2254 * sqe->off holds how many events that need to occur for this
2255 * timeout event to be satisfied.
2256 */
2257 count = READ_ONCE(sqe->off);
2258 if (!count)
2259 count = 1;
2260
2261 req->sequence = ctx->cached_sq_head + count - 1;
yangerkun5da0fb12019-10-15 21:59:29 +08002262 /* reuse it to store the count */
2263 req->submit.sequence = count;
Jens Axboe5262f562019-09-17 12:26:57 -06002264 req->flags |= REQ_F_TIMEOUT;
2265
2266 /*
2267 * Insertion sort, ensuring the first entry in the list is always
2268 * the one we need first.
2269 */
Jens Axboe5262f562019-09-17 12:26:57 -06002270 spin_lock_irq(&ctx->completion_lock);
2271 list_for_each_prev(entry, &ctx->timeout_list) {
2272 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
yangerkun5da0fb12019-10-15 21:59:29 +08002273 unsigned nxt_sq_head;
2274 long long tmp, tmp_nxt;
Jens Axboe5262f562019-09-17 12:26:57 -06002275
yangerkun5da0fb12019-10-15 21:59:29 +08002276 /*
2277 * Since cached_sq_head + count - 1 can overflow, use type long
2278 * long to store it.
2279 */
2280 tmp = (long long)ctx->cached_sq_head + count - 1;
2281 nxt_sq_head = nxt->sequence - nxt->submit.sequence + 1;
2282 tmp_nxt = (long long)nxt_sq_head + nxt->submit.sequence - 1;
2283
2284 /*
2285 * cached_sq_head may overflow, and it will never overflow twice
2286 * once there is some timeout req still be valid.
2287 */
2288 if (ctx->cached_sq_head < nxt_sq_head)
yangerkun8b07a652019-10-17 12:12:35 +08002289 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08002290
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002291 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06002292 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002293
2294 /*
2295 * Sequence of reqs after the insert one and itself should
2296 * be adjusted because each timeout req consumes a slot.
2297 */
2298 span++;
2299 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06002300 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002301 req->sequence -= span;
Jens Axboe5262f562019-09-17 12:26:57 -06002302 list_add(&req->list, entry);
Jens Axboe5262f562019-09-17 12:26:57 -06002303 req->timeout.timer.function = io_timeout_fn;
Jens Axboea41525a2019-10-15 16:48:15 -06002304 hrtimer_start(&req->timeout.timer, timespec64_to_ktime(ts), mode);
Jens Axboe842f9612019-10-29 12:34:10 -06002305 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06002306 return 0;
2307}
2308
Jens Axboe62755e32019-10-28 21:49:21 -06002309static bool io_cancel_cb(struct io_wq_work *work, void *data)
2310{
2311 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
2312
2313 return req->user_data == (unsigned long) data;
2314}
2315
Jens Axboee977d6d2019-11-05 12:39:45 -07002316static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06002317{
Jens Axboe62755e32019-10-28 21:49:21 -06002318 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06002319 int ret = 0;
2320
Jens Axboe62755e32019-10-28 21:49:21 -06002321 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
2322 switch (cancel_ret) {
2323 case IO_WQ_CANCEL_OK:
2324 ret = 0;
2325 break;
2326 case IO_WQ_CANCEL_RUNNING:
2327 ret = -EALREADY;
2328 break;
2329 case IO_WQ_CANCEL_NOTFOUND:
2330 ret = -ENOENT;
2331 break;
2332 }
2333
Jens Axboee977d6d2019-11-05 12:39:45 -07002334 return ret;
2335}
2336
2337static int io_async_cancel(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2338 struct io_kiocb **nxt)
2339{
2340 struct io_ring_ctx *ctx = req->ctx;
2341 void *sqe_addr;
2342 int ret;
2343
2344 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2345 return -EINVAL;
2346 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
2347 sqe->cancel_flags)
2348 return -EINVAL;
2349
2350 sqe_addr = (void *) (unsigned long) READ_ONCE(sqe->addr);
2351 ret = io_async_cancel_one(ctx, sqe_addr);
2352
Jens Axboe62755e32019-10-28 21:49:21 -06002353 if (ret < 0 && (req->flags & REQ_F_LINK))
2354 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07002355 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08002356 io_put_req_find_next(req, nxt);
Jens Axboe62755e32019-10-28 21:49:21 -06002357 return 0;
2358}
2359
Jackie Liua197f662019-11-08 08:09:12 -07002360static int io_req_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -06002361{
Pavel Begunkov267bc902019-11-07 01:41:08 +03002362 const struct io_uring_sqe *sqe = req->submit.sqe;
Jens Axboede0617e2019-04-06 21:51:27 -06002363 struct io_uring_sqe *sqe_copy;
Jackie Liua197f662019-11-08 08:09:12 -07002364 struct io_ring_ctx *ctx = req->ctx;
Jens Axboede0617e2019-04-06 21:51:27 -06002365
Jackie Liua197f662019-11-08 08:09:12 -07002366 if (!io_sequence_defer(req) && list_empty(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06002367 return 0;
2368
2369 sqe_copy = kmalloc(sizeof(*sqe_copy), GFP_KERNEL);
2370 if (!sqe_copy)
2371 return -EAGAIN;
2372
2373 spin_lock_irq(&ctx->completion_lock);
Jackie Liua197f662019-11-08 08:09:12 -07002374 if (!io_sequence_defer(req) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06002375 spin_unlock_irq(&ctx->completion_lock);
2376 kfree(sqe_copy);
2377 return 0;
2378 }
2379
2380 memcpy(sqe_copy, sqe, sizeof(*sqe_copy));
2381 req->submit.sqe = sqe_copy;
2382
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002383 trace_io_uring_defer(ctx, req, false);
Jens Axboede0617e2019-04-06 21:51:27 -06002384 list_add_tail(&req->list, &ctx->defer_list);
2385 spin_unlock_irq(&ctx->completion_lock);
2386 return -EIOCBQUEUED;
2387}
2388
Jackie Liua197f662019-11-08 08:09:12 -07002389static int __io_submit_sqe(struct io_kiocb *req, struct io_kiocb **nxt,
2390 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002391{
Jens Axboee0c5c572019-03-12 10:18:47 -06002392 int ret, opcode;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002393 struct sqe_submit *s = &req->submit;
Jackie Liua197f662019-11-08 08:09:12 -07002394 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002395
Jens Axboe2b188cc2019-01-07 10:46:33 -07002396 opcode = READ_ONCE(s->sqe->opcode);
2397 switch (opcode) {
2398 case IORING_OP_NOP:
Jens Axboe78e19bb2019-11-06 15:21:34 -07002399 ret = io_nop(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002400 break;
2401 case IORING_OP_READV:
Jens Axboeedafcce2019-01-09 09:16:05 -07002402 if (unlikely(s->sqe->buf_index))
2403 return -EINVAL;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002404 ret = io_read(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002405 break;
2406 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07002407 if (unlikely(s->sqe->buf_index))
2408 return -EINVAL;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002409 ret = io_write(req, nxt, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07002410 break;
2411 case IORING_OP_READ_FIXED:
Pavel Begunkov267bc902019-11-07 01:41:08 +03002412 ret = io_read(req, nxt, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07002413 break;
2414 case IORING_OP_WRITE_FIXED:
Pavel Begunkov267bc902019-11-07 01:41:08 +03002415 ret = io_write(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002416 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002417 case IORING_OP_FSYNC:
Jens Axboeba816ad2019-09-28 11:36:45 -06002418 ret = io_fsync(req, s->sqe, nxt, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002419 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002420 case IORING_OP_POLL_ADD:
Jens Axboe89723d02019-11-05 15:32:58 -07002421 ret = io_poll_add(req, s->sqe, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002422 break;
2423 case IORING_OP_POLL_REMOVE:
2424 ret = io_poll_remove(req, s->sqe);
2425 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002426 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboeba816ad2019-09-28 11:36:45 -06002427 ret = io_sync_file_range(req, s->sqe, nxt, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002428 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002429 case IORING_OP_SENDMSG:
Jens Axboeba816ad2019-09-28 11:36:45 -06002430 ret = io_sendmsg(req, s->sqe, nxt, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06002431 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06002432 case IORING_OP_RECVMSG:
Jens Axboeba816ad2019-09-28 11:36:45 -06002433 ret = io_recvmsg(req, s->sqe, nxt, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06002434 break;
Jens Axboe5262f562019-09-17 12:26:57 -06002435 case IORING_OP_TIMEOUT:
2436 ret = io_timeout(req, s->sqe);
2437 break;
Jens Axboe11365042019-10-16 09:08:32 -06002438 case IORING_OP_TIMEOUT_REMOVE:
2439 ret = io_timeout_remove(req, s->sqe);
2440 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06002441 case IORING_OP_ACCEPT:
2442 ret = io_accept(req, s->sqe, nxt, force_nonblock);
2443 break;
Jens Axboe62755e32019-10-28 21:49:21 -06002444 case IORING_OP_ASYNC_CANCEL:
2445 ret = io_async_cancel(req, s->sqe, nxt);
2446 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002447 default:
2448 ret = -EINVAL;
2449 break;
2450 }
2451
Jens Axboedef596e2019-01-09 08:59:42 -07002452 if (ret)
2453 return ret;
2454
2455 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe9e645e112019-05-10 16:07:28 -06002456 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07002457 return -EAGAIN;
2458
2459 /* workqueue context doesn't hold uring_lock, grab it now */
Jackie Liuba5290c2019-10-09 09:19:59 +08002460 if (s->in_async)
Jens Axboedef596e2019-01-09 08:59:42 -07002461 mutex_lock(&ctx->uring_lock);
2462 io_iopoll_req_issued(req);
Jackie Liuba5290c2019-10-09 09:19:59 +08002463 if (s->in_async)
Jens Axboedef596e2019-01-09 08:59:42 -07002464 mutex_unlock(&ctx->uring_lock);
2465 }
2466
2467 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002468}
2469
Jens Axboe561fb042019-10-24 07:25:42 -06002470static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboe31b51512019-01-18 22:56:34 -07002471{
Jens Axboe561fb042019-10-24 07:25:42 -06002472 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002473 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe561fb042019-10-24 07:25:42 -06002474 struct sqe_submit *s = &req->submit;
2475 const struct io_uring_sqe *sqe = s->sqe;
2476 struct io_kiocb *nxt = NULL;
2477 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002478
Jens Axboe561fb042019-10-24 07:25:42 -06002479 /* Ensure we clear previously set non-block flag */
2480 req->rw.ki_flags &= ~IOCB_NOWAIT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002481
Jens Axboe561fb042019-10-24 07:25:42 -06002482 if (work->flags & IO_WQ_WORK_CANCEL)
2483 ret = -ECANCELED;
Jens Axboe31b51512019-01-18 22:56:34 -07002484
Jens Axboe561fb042019-10-24 07:25:42 -06002485 if (!ret) {
2486 s->has_user = (work->flags & IO_WQ_WORK_HAS_MM) != 0;
2487 s->in_async = true;
2488 do {
Jackie Liua197f662019-11-08 08:09:12 -07002489 ret = __io_submit_sqe(req, &nxt, false);
Jens Axboe561fb042019-10-24 07:25:42 -06002490 /*
2491 * We can get EAGAIN for polled IO even though we're
2492 * forcing a sync submission from here, since we can't
2493 * wait for request slots on the block side.
2494 */
2495 if (ret != -EAGAIN)
2496 break;
2497 cond_resched();
2498 } while (1);
2499 }
Jens Axboe31b51512019-01-18 22:56:34 -07002500
Jens Axboe561fb042019-10-24 07:25:42 -06002501 /* drop submission reference */
Jackie Liuec9c02a2019-11-08 23:50:36 +08002502 io_put_req(req);
Jens Axboe817869d2019-04-30 14:44:05 -06002503
Jens Axboe561fb042019-10-24 07:25:42 -06002504 if (ret) {
Jens Axboef1f40852019-11-05 20:33:16 -07002505 if (req->flags & REQ_F_LINK)
2506 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07002507 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08002508 io_put_req(req);
Jens Axboeedafcce2019-01-09 09:16:05 -07002509 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002510
Jens Axboe561fb042019-10-24 07:25:42 -06002511 /* async context always use a copy of the sqe */
2512 kfree(sqe);
2513
2514 /* if a dependent link is ready, pass it back */
2515 if (!ret && nxt) {
2516 io_prep_async_work(nxt);
2517 *workptr = &nxt->work;
Jens Axboeedafcce2019-01-09 09:16:05 -07002518 }
Jens Axboe31b51512019-01-18 22:56:34 -07002519}
Jens Axboe2b188cc2019-01-07 10:46:33 -07002520
Jens Axboe09bb8392019-03-13 12:39:28 -06002521static bool io_op_needs_file(const struct io_uring_sqe *sqe)
2522{
2523 int op = READ_ONCE(sqe->opcode);
2524
2525 switch (op) {
2526 case IORING_OP_NOP:
2527 case IORING_OP_POLL_REMOVE:
2528 return false;
2529 default:
2530 return true;
2531 }
2532}
2533
Jens Axboe65e19f52019-10-26 07:20:21 -06002534static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
2535 int index)
2536{
2537 struct fixed_file_table *table;
2538
2539 table = &ctx->file_table[index >> IORING_FILE_TABLE_SHIFT];
2540 return table->files[index & IORING_FILE_TABLE_MASK];
2541}
2542
Jackie Liua197f662019-11-08 08:09:12 -07002543static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req)
Jens Axboe09bb8392019-03-13 12:39:28 -06002544{
Pavel Begunkov267bc902019-11-07 01:41:08 +03002545 struct sqe_submit *s = &req->submit;
Jackie Liua197f662019-11-08 08:09:12 -07002546 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe09bb8392019-03-13 12:39:28 -06002547 unsigned flags;
2548 int fd;
2549
2550 flags = READ_ONCE(s->sqe->flags);
2551 fd = READ_ONCE(s->sqe->fd);
2552
Jackie Liu4fe2c962019-09-09 20:50:40 +08002553 if (flags & IOSQE_IO_DRAIN)
Jens Axboede0617e2019-04-06 21:51:27 -06002554 req->flags |= REQ_F_IO_DRAIN;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002555 /*
2556 * All io need record the previous position, if LINK vs DARIN,
2557 * it can be used to mark the position of the first IO in the
2558 * link list.
2559 */
2560 req->sequence = s->sequence;
Jens Axboede0617e2019-04-06 21:51:27 -06002561
Jens Axboe60c112b2019-06-21 10:20:18 -06002562 if (!io_op_needs_file(s->sqe))
Jens Axboe09bb8392019-03-13 12:39:28 -06002563 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06002564
2565 if (flags & IOSQE_FIXED_FILE) {
Jens Axboe65e19f52019-10-26 07:20:21 -06002566 if (unlikely(!ctx->file_table ||
Jens Axboe09bb8392019-03-13 12:39:28 -06002567 (unsigned) fd >= ctx->nr_user_files))
2568 return -EBADF;
Jens Axboeb7620122019-10-26 07:22:55 -06002569 fd = array_index_nospec(fd, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06002570 req->file = io_file_from_index(ctx, fd);
2571 if (!req->file)
Jens Axboe08a45172019-10-03 08:11:03 -06002572 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06002573 req->flags |= REQ_F_FIXED_FILE;
2574 } else {
2575 if (s->needs_fixed_file)
2576 return -EBADF;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002577 trace_io_uring_file_get(ctx, fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06002578 req->file = io_file_get(state, fd);
2579 if (unlikely(!req->file))
2580 return -EBADF;
2581 }
2582
2583 return 0;
2584}
2585
Jackie Liua197f662019-11-08 08:09:12 -07002586static int io_grab_files(struct io_kiocb *req)
Jens Axboefcb323c2019-10-24 12:39:47 -06002587{
2588 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07002589 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06002590
2591 rcu_read_lock();
2592 spin_lock_irq(&ctx->inflight_lock);
2593 /*
2594 * We use the f_ops->flush() handler to ensure that we can flush
2595 * out work accessing these files if the fd is closed. Check if
2596 * the fd has changed since we started down this path, and disallow
2597 * this operation if it has.
2598 */
2599 if (fcheck(req->submit.ring_fd) == req->submit.ring_file) {
2600 list_add(&req->inflight_entry, &ctx->inflight_list);
2601 req->flags |= REQ_F_INFLIGHT;
2602 req->work.files = current->files;
2603 ret = 0;
2604 }
2605 spin_unlock_irq(&ctx->inflight_lock);
2606 rcu_read_unlock();
2607
2608 return ret;
2609}
2610
Jens Axboe2665abf2019-11-05 12:40:47 -07002611static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
2612{
2613 struct io_kiocb *req = container_of(timer, struct io_kiocb,
2614 timeout.timer);
2615 struct io_ring_ctx *ctx = req->ctx;
2616 struct io_kiocb *prev = NULL;
2617 unsigned long flags;
2618 int ret = -ETIME;
2619
2620 spin_lock_irqsave(&ctx->completion_lock, flags);
2621
2622 /*
2623 * We don't expect the list to be empty, that will only happen if we
2624 * race with the completion of the linked work.
2625 */
2626 if (!list_empty(&req->list)) {
2627 prev = list_entry(req->list.prev, struct io_kiocb, link_list);
2628 list_del_init(&req->list);
2629 }
2630
2631 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2632
2633 if (prev) {
2634 void *user_data = (void *) (unsigned long) prev->user_data;
2635 ret = io_async_cancel_one(ctx, user_data);
2636 }
2637
Jens Axboe78e19bb2019-11-06 15:21:34 -07002638 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08002639 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07002640 return HRTIMER_NORESTART;
2641}
2642
2643static int io_queue_linked_timeout(struct io_kiocb *req, struct io_kiocb *nxt)
2644{
2645 const struct io_uring_sqe *sqe = nxt->submit.sqe;
2646 enum hrtimer_mode mode;
2647 struct timespec64 ts;
2648 int ret = -EINVAL;
2649
2650 if (sqe->ioprio || sqe->buf_index || sqe->len != 1 || sqe->off)
2651 goto err;
2652 if (sqe->timeout_flags & ~IORING_TIMEOUT_ABS)
2653 goto err;
2654 if (get_timespec64(&ts, u64_to_user_ptr(sqe->addr))) {
2655 ret = -EFAULT;
2656 goto err;
2657 }
2658
2659 req->flags |= REQ_F_LINK_TIMEOUT;
2660
2661 if (sqe->timeout_flags & IORING_TIMEOUT_ABS)
2662 mode = HRTIMER_MODE_ABS;
2663 else
2664 mode = HRTIMER_MODE_REL;
2665 hrtimer_init(&nxt->timeout.timer, CLOCK_MONOTONIC, mode);
2666 nxt->timeout.timer.function = io_link_timeout_fn;
2667 hrtimer_start(&nxt->timeout.timer, timespec64_to_ktime(ts), mode);
2668 ret = 0;
2669err:
2670 /* drop submission reference */
Jackie Liuec9c02a2019-11-08 23:50:36 +08002671 io_put_req(nxt);
Jens Axboe2665abf2019-11-05 12:40:47 -07002672
2673 if (ret) {
2674 struct io_ring_ctx *ctx = req->ctx;
2675
2676 /*
2677 * Break the link and fail linked timeout, parent will get
2678 * failed by the regular submission path.
2679 */
2680 list_del(&nxt->list);
Jens Axboe78e19bb2019-11-06 15:21:34 -07002681 io_cqring_fill_event(nxt, ret);
Jens Axboe2665abf2019-11-05 12:40:47 -07002682 trace_io_uring_fail_link(req, nxt);
2683 io_commit_cqring(ctx);
Jackie Liuec9c02a2019-11-08 23:50:36 +08002684 io_put_req(nxt);
Jens Axboe2665abf2019-11-05 12:40:47 -07002685 ret = -ECANCELED;
2686 }
2687
2688 return ret;
2689}
2690
2691static inline struct io_kiocb *io_get_linked_timeout(struct io_kiocb *req)
2692{
2693 struct io_kiocb *nxt;
2694
2695 if (!(req->flags & REQ_F_LINK))
2696 return NULL;
2697
2698 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, list);
2699 if (nxt && nxt->submit.sqe->opcode == IORING_OP_LINK_TIMEOUT)
2700 return nxt;
2701
2702 return NULL;
2703}
2704
Jackie Liua197f662019-11-08 08:09:12 -07002705static int __io_queue_sqe(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002706{
Jens Axboe2665abf2019-11-05 12:40:47 -07002707 struct io_kiocb *nxt;
Jens Axboee0c5c572019-03-12 10:18:47 -06002708 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002709
Jens Axboe2665abf2019-11-05 12:40:47 -07002710 nxt = io_get_linked_timeout(req);
2711 if (unlikely(nxt)) {
2712 ret = io_queue_linked_timeout(req, nxt);
2713 if (ret)
2714 goto err;
2715 }
2716
Jackie Liua197f662019-11-08 08:09:12 -07002717 ret = __io_submit_sqe(req, NULL, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06002718
2719 /*
2720 * We async punt it if the file wasn't marked NOWAIT, or if the file
2721 * doesn't support non-blocking read/write attempts
2722 */
2723 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
2724 (req->flags & REQ_F_MUST_PUNT))) {
Pavel Begunkov267bc902019-11-07 01:41:08 +03002725 struct sqe_submit *s = &req->submit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002726 struct io_uring_sqe *sqe_copy;
2727
Jackie Liu954dab12019-09-18 10:37:52 +08002728 sqe_copy = kmemdup(s->sqe, sizeof(*sqe_copy), GFP_KERNEL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002729 if (sqe_copy) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07002730 s->sqe = sqe_copy;
Jens Axboefcb323c2019-10-24 12:39:47 -06002731 if (req->work.flags & IO_WQ_WORK_NEEDS_FILES) {
Jackie Liua197f662019-11-08 08:09:12 -07002732 ret = io_grab_files(req);
Jens Axboefcb323c2019-10-24 12:39:47 -06002733 if (ret) {
2734 kfree(sqe_copy);
2735 goto err;
2736 }
2737 }
Jens Axboee65ef562019-03-12 10:16:44 -06002738
2739 /*
2740 * Queued up for async execution, worker will release
Jens Axboe9e645e112019-05-10 16:07:28 -06002741 * submit reference when the iocb is actually submitted.
Jens Axboee65ef562019-03-12 10:16:44 -06002742 */
Jackie Liua197f662019-11-08 08:09:12 -07002743 io_queue_async_work(req);
Jens Axboee65ef562019-03-12 10:16:44 -06002744 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002745 }
2746 }
Jens Axboee65ef562019-03-12 10:16:44 -06002747
2748 /* drop submission reference */
Jens Axboefcb323c2019-10-24 12:39:47 -06002749err:
Jackie Liuec9c02a2019-11-08 23:50:36 +08002750 io_put_req(req);
Jens Axboee65ef562019-03-12 10:16:44 -06002751
2752 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06002753 if (ret) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07002754 io_cqring_add_event(req, ret);
Jens Axboe9e645e112019-05-10 16:07:28 -06002755 if (req->flags & REQ_F_LINK)
2756 req->flags |= REQ_F_FAIL_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08002757 io_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002758 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002759
2760 return ret;
2761}
2762
Jackie Liua197f662019-11-08 08:09:12 -07002763static int io_queue_sqe(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08002764{
2765 int ret;
2766
Jackie Liua197f662019-11-08 08:09:12 -07002767 ret = io_req_defer(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002768 if (ret) {
2769 if (ret != -EIOCBQUEUED) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07002770 io_cqring_add_event(req, ret);
2771 io_double_put_req(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002772 }
2773 return 0;
2774 }
2775
Jackie Liua197f662019-11-08 08:09:12 -07002776 return __io_queue_sqe(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002777}
2778
Jackie Liua197f662019-11-08 08:09:12 -07002779static int io_queue_link_head(struct io_kiocb *req, struct io_kiocb *shadow)
Jackie Liu4fe2c962019-09-09 20:50:40 +08002780{
2781 int ret;
2782 int need_submit = false;
Jackie Liua197f662019-11-08 08:09:12 -07002783 struct io_ring_ctx *ctx = req->ctx;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002784
2785 if (!shadow)
Jackie Liua197f662019-11-08 08:09:12 -07002786 return io_queue_sqe(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002787
2788 /*
2789 * Mark the first IO in link list as DRAIN, let all the following
2790 * IOs enter the defer list. all IO needs to be completed before link
2791 * list.
2792 */
2793 req->flags |= REQ_F_IO_DRAIN;
Jackie Liua197f662019-11-08 08:09:12 -07002794 ret = io_req_defer(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002795 if (ret) {
2796 if (ret != -EIOCBQUEUED) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07002797 io_cqring_add_event(req, ret);
2798 io_double_put_req(req);
Pavel Begunkov7b202382019-10-27 22:10:36 +03002799 __io_free_req(shadow);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002800 return 0;
2801 }
2802 } else {
2803 /*
2804 * If ret == 0 means that all IOs in front of link io are
2805 * running done. let's queue link head.
2806 */
2807 need_submit = true;
2808 }
2809
2810 /* Insert shadow req to defer_list, blocking next IOs */
2811 spin_lock_irq(&ctx->completion_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002812 trace_io_uring_defer(ctx, shadow, true);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002813 list_add_tail(&shadow->list, &ctx->defer_list);
2814 spin_unlock_irq(&ctx->completion_lock);
2815
2816 if (need_submit)
Jackie Liua197f662019-11-08 08:09:12 -07002817 return __io_queue_sqe(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002818
2819 return 0;
2820}
2821
Jens Axboe9e645e112019-05-10 16:07:28 -06002822#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK)
2823
Jackie Liua197f662019-11-08 08:09:12 -07002824static void io_submit_sqe(struct io_kiocb *req, struct io_submit_state *state,
2825 struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06002826{
2827 struct io_uring_sqe *sqe_copy;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002828 struct sqe_submit *s = &req->submit;
Jackie Liua197f662019-11-08 08:09:12 -07002829 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -06002830 int ret;
2831
Jens Axboe78e19bb2019-11-06 15:21:34 -07002832 req->user_data = s->sqe->user_data;
2833
Jens Axboe9e645e112019-05-10 16:07:28 -06002834 /* enforce forwards compatibility on users */
2835 if (unlikely(s->sqe->flags & ~SQE_VALID_FLAGS)) {
2836 ret = -EINVAL;
Pavel Begunkov196be952019-11-07 01:41:06 +03002837 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06002838 }
2839
Jackie Liua197f662019-11-08 08:09:12 -07002840 ret = io_req_set_file(state, req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002841 if (unlikely(ret)) {
2842err_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07002843 io_cqring_add_event(req, ret);
2844 io_double_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002845 return;
2846 }
2847
Jens Axboe9e645e112019-05-10 16:07:28 -06002848 /*
2849 * If we already have a head request, queue this one for async
2850 * submittal once the head completes. If we don't have a head but
2851 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
2852 * submitted sync once the chain is complete. If none of those
2853 * conditions are true (normal request), then just queue it.
2854 */
2855 if (*link) {
2856 struct io_kiocb *prev = *link;
2857
2858 sqe_copy = kmemdup(s->sqe, sizeof(*sqe_copy), GFP_KERNEL);
2859 if (!sqe_copy) {
2860 ret = -EAGAIN;
2861 goto err_req;
2862 }
2863
2864 s->sqe = sqe_copy;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002865 trace_io_uring_link(ctx, req, prev);
Jens Axboe9e645e112019-05-10 16:07:28 -06002866 list_add_tail(&req->list, &prev->link_list);
2867 } else if (s->sqe->flags & IOSQE_IO_LINK) {
2868 req->flags |= REQ_F_LINK;
2869
Jens Axboe9e645e112019-05-10 16:07:28 -06002870 INIT_LIST_HEAD(&req->link_list);
2871 *link = req;
Jens Axboe2665abf2019-11-05 12:40:47 -07002872 } else if (READ_ONCE(s->sqe->opcode) == IORING_OP_LINK_TIMEOUT) {
2873 /* Only valid as a linked SQE */
2874 ret = -EINVAL;
2875 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06002876 } else {
Jackie Liua197f662019-11-08 08:09:12 -07002877 io_queue_sqe(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002878 }
2879}
2880
Jens Axboe9a56a232019-01-09 09:06:50 -07002881/*
2882 * Batched submission is done, ensure local IO is flushed out.
2883 */
2884static void io_submit_state_end(struct io_submit_state *state)
2885{
2886 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06002887 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07002888 if (state->free_reqs)
2889 kmem_cache_free_bulk(req_cachep, state->free_reqs,
2890 &state->reqs[state->cur_req]);
Jens Axboe9a56a232019-01-09 09:06:50 -07002891}
2892
2893/*
2894 * Start submission side cache.
2895 */
2896static void io_submit_state_start(struct io_submit_state *state,
2897 struct io_ring_ctx *ctx, unsigned max_ios)
2898{
2899 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07002900 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07002901 state->file = NULL;
2902 state->ios_left = max_ios;
2903}
2904
Jens Axboe2b188cc2019-01-07 10:46:33 -07002905static void io_commit_sqring(struct io_ring_ctx *ctx)
2906{
Hristo Venev75b28af2019-08-26 17:23:46 +00002907 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002908
Hristo Venev75b28af2019-08-26 17:23:46 +00002909 if (ctx->cached_sq_head != READ_ONCE(rings->sq.head)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07002910 /*
2911 * Ensure any loads from the SQEs are done at this point,
2912 * since once we write the new head, the application could
2913 * write new data to them.
2914 */
Hristo Venev75b28af2019-08-26 17:23:46 +00002915 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002916 }
2917}
2918
2919/*
Jens Axboe2b188cc2019-01-07 10:46:33 -07002920 * Fetch an sqe, if one is available. Note that s->sqe will point to memory
2921 * that is mapped by userspace. This means that care needs to be taken to
2922 * ensure that reads are stable, as we cannot rely on userspace always
2923 * being a good citizen. If members of the sqe are validated and then later
2924 * used, it's important that those reads are done through READ_ONCE() to
2925 * prevent a re-load down the line.
2926 */
2927static bool io_get_sqring(struct io_ring_ctx *ctx, struct sqe_submit *s)
2928{
Hristo Venev75b28af2019-08-26 17:23:46 +00002929 struct io_rings *rings = ctx->rings;
2930 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002931 unsigned head;
2932
2933 /*
2934 * The cached sq head (or cq tail) serves two purposes:
2935 *
2936 * 1) allows us to batch the cost of updating the user visible
2937 * head updates.
2938 * 2) allows the kernel side to track the head on its own, even
2939 * though the application is the one updating it.
2940 */
2941 head = ctx->cached_sq_head;
Stefan Bühlere523a292019-04-19 11:57:44 +02002942 /* make sure SQ entry isn't read before tail */
Hristo Venev75b28af2019-08-26 17:23:46 +00002943 if (head == smp_load_acquire(&rings->sq.tail))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002944 return false;
2945
Hristo Venev75b28af2019-08-26 17:23:46 +00002946 head = READ_ONCE(sq_array[head & ctx->sq_mask]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002947 if (head < ctx->sq_entries) {
Jens Axboefcb323c2019-10-24 12:39:47 -06002948 s->ring_file = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002949 s->sqe = &ctx->sq_sqes[head];
Jackie Liu8776f3f2019-09-09 20:50:39 +08002950 s->sequence = ctx->cached_sq_head;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002951 ctx->cached_sq_head++;
2952 return true;
2953 }
2954
2955 /* drop invalid entries */
2956 ctx->cached_sq_head++;
Jens Axboe498ccd92019-10-25 10:04:25 -06002957 ctx->cached_sq_dropped++;
2958 WRITE_ONCE(rings->sq_dropped, ctx->cached_sq_dropped);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002959 return false;
2960}
2961
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002962static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkovae9428c2019-11-06 00:22:14 +03002963 struct file *ring_file, int ring_fd,
2964 struct mm_struct **mm, bool async)
Jens Axboe6c271ce2019-01-10 11:22:30 -07002965{
2966 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002967 struct io_kiocb *link = NULL;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002968 struct io_kiocb *shadow_req = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002969 int i, submitted = 0;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03002970 bool mm_fault = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002971
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07002972 if (!list_empty(&ctx->cq_overflow_list)) {
2973 io_cqring_overflow_flush(ctx, false);
2974 return -EBUSY;
2975 }
2976
Jens Axboe6c271ce2019-01-10 11:22:30 -07002977 if (nr > IO_PLUG_THRESHOLD) {
2978 io_submit_state_start(&state, ctx, nr);
2979 statep = &state;
2980 }
2981
2982 for (i = 0; i < nr; i++) {
Pavel Begunkov196be952019-11-07 01:41:06 +03002983 struct io_kiocb *req;
Pavel Begunkov50585b92019-11-07 01:41:07 +03002984 unsigned int sqe_flags;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002985
Pavel Begunkov196be952019-11-07 01:41:06 +03002986 req = io_get_req(ctx, statep);
2987 if (unlikely(!req)) {
2988 if (!submitted)
2989 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002990 break;
Pavel Begunkov196be952019-11-07 01:41:06 +03002991 }
Pavel Begunkov50585b92019-11-07 01:41:07 +03002992 if (!io_get_sqring(ctx, &req->submit)) {
Pavel Begunkov196be952019-11-07 01:41:06 +03002993 __io_free_req(req);
2994 break;
2995 }
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002996
Pavel Begunkov50585b92019-11-07 01:41:07 +03002997 if (io_sqe_needs_user(req->submit.sqe) && !*mm) {
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03002998 mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm);
2999 if (!mm_fault) {
3000 use_mm(ctx->sqo_mm);
3001 *mm = ctx->sqo_mm;
3002 }
3003 }
3004
Pavel Begunkov50585b92019-11-07 01:41:07 +03003005 sqe_flags = req->submit.sqe->flags;
3006
3007 if (link && (sqe_flags & IOSQE_IO_DRAIN)) {
Jackie Liu4fe2c962019-09-09 20:50:40 +08003008 if (!shadow_req) {
3009 shadow_req = io_get_req(ctx, NULL);
Jackie Liua1041c22019-09-18 17:25:52 +08003010 if (unlikely(!shadow_req))
3011 goto out;
Jackie Liu4fe2c962019-09-09 20:50:40 +08003012 shadow_req->flags |= (REQ_F_IO_DRAIN | REQ_F_SHADOW_DRAIN);
3013 refcount_dec(&shadow_req->refs);
3014 }
Pavel Begunkov50585b92019-11-07 01:41:07 +03003015 shadow_req->sequence = req->submit.sequence;
Jackie Liu4fe2c962019-09-09 20:50:40 +08003016 }
3017
Jackie Liua1041c22019-09-18 17:25:52 +08003018out:
Pavel Begunkov50585b92019-11-07 01:41:07 +03003019 req->submit.ring_file = ring_file;
3020 req->submit.ring_fd = ring_fd;
3021 req->submit.has_user = *mm != NULL;
3022 req->submit.in_async = async;
3023 req->submit.needs_fixed_file = async;
3024 trace_io_uring_submit_sqe(ctx, req->submit.sqe->user_data,
3025 true, async);
Jackie Liua197f662019-11-08 08:09:12 -07003026 io_submit_sqe(req, statep, &link);
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03003027 submitted++;
Pavel Begunkove5eb6362019-11-06 00:22:15 +03003028
3029 /*
3030 * If previous wasn't linked and we have a linked command,
3031 * that's the end of the chain. Submit the previous link.
3032 */
Pavel Begunkov50585b92019-11-07 01:41:07 +03003033 if (!(sqe_flags & IOSQE_IO_LINK) && link) {
Jackie Liua197f662019-11-08 08:09:12 -07003034 io_queue_link_head(link, shadow_req);
Pavel Begunkove5eb6362019-11-06 00:22:15 +03003035 link = NULL;
3036 shadow_req = NULL;
3037 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07003038 }
3039
Jens Axboe9e645e112019-05-10 16:07:28 -06003040 if (link)
Jackie Liua197f662019-11-08 08:09:12 -07003041 io_queue_link_head(link, shadow_req);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003042 if (statep)
3043 io_submit_state_end(&state);
3044
Pavel Begunkovae9428c2019-11-06 00:22:14 +03003045 /* Commit SQ ring head once we've consumed and submitted all SQEs */
3046 io_commit_sqring(ctx);
3047
Jens Axboe6c271ce2019-01-10 11:22:30 -07003048 return submitted;
3049}
3050
3051static int io_sq_thread(void *data)
3052{
Jens Axboe6c271ce2019-01-10 11:22:30 -07003053 struct io_ring_ctx *ctx = data;
3054 struct mm_struct *cur_mm = NULL;
3055 mm_segment_t old_fs;
3056 DEFINE_WAIT(wait);
3057 unsigned inflight;
3058 unsigned long timeout;
3059
Jens Axboe206aefd2019-11-07 18:27:42 -07003060 complete(&ctx->completions[1]);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08003061
Jens Axboe6c271ce2019-01-10 11:22:30 -07003062 old_fs = get_fs();
3063 set_fs(USER_DS);
3064
3065 timeout = inflight = 0;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003066 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003067 unsigned int to_submit;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003068 int ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003069
3070 if (inflight) {
3071 unsigned nr_events = 0;
3072
3073 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe2b2ed972019-10-25 10:06:15 -06003074 /*
3075 * inflight is the count of the maximum possible
3076 * entries we submitted, but it can be smaller
3077 * if we dropped some of them. If we don't have
3078 * poll entries available, then we know that we
3079 * have nothing left to poll for. Reset the
3080 * inflight count to zero in that case.
3081 */
3082 mutex_lock(&ctx->uring_lock);
3083 if (!list_empty(&ctx->poll_list))
3084 __io_iopoll_check(ctx, &nr_events, 0);
3085 else
3086 inflight = 0;
3087 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003088 } else {
3089 /*
3090 * Normal IO, just pretend everything completed.
3091 * We don't have to poll completions for that.
3092 */
3093 nr_events = inflight;
3094 }
3095
3096 inflight -= nr_events;
3097 if (!inflight)
3098 timeout = jiffies + ctx->sq_thread_idle;
3099 }
3100
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003101 to_submit = io_sqring_entries(ctx);
3102 if (!to_submit) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07003103 /*
3104 * We're polling. If we're within the defined idle
3105 * period, then let us spin without work before going
3106 * to sleep.
3107 */
3108 if (inflight || !time_after(jiffies, timeout)) {
Jens Axboe9831a902019-09-19 09:48:55 -06003109 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07003110 continue;
3111 }
3112
3113 /*
3114 * Drop cur_mm before scheduling, we can't hold it for
3115 * long periods (or over schedule()). Do this before
3116 * adding ourselves to the waitqueue, as the unuse/drop
3117 * may sleep.
3118 */
3119 if (cur_mm) {
3120 unuse_mm(cur_mm);
3121 mmput(cur_mm);
3122 cur_mm = NULL;
3123 }
3124
3125 prepare_to_wait(&ctx->sqo_wait, &wait,
3126 TASK_INTERRUPTIBLE);
3127
3128 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00003129 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02003130 /* make sure to read SQ tail after writing flags */
3131 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07003132
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003133 to_submit = io_sqring_entries(ctx);
3134 if (!to_submit) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003135 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07003136 finish_wait(&ctx->sqo_wait, &wait);
3137 break;
3138 }
3139 if (signal_pending(current))
3140 flush_signals(current);
3141 schedule();
3142 finish_wait(&ctx->sqo_wait, &wait);
3143
Hristo Venev75b28af2019-08-26 17:23:46 +00003144 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003145 continue;
3146 }
3147 finish_wait(&ctx->sqo_wait, &wait);
3148
Hristo Venev75b28af2019-08-26 17:23:46 +00003149 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003150 }
3151
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003152 to_submit = min(to_submit, ctx->sq_entries);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003153 ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true);
3154 if (ret > 0)
3155 inflight += ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003156 }
3157
3158 set_fs(old_fs);
3159 if (cur_mm) {
3160 unuse_mm(cur_mm);
3161 mmput(cur_mm);
3162 }
Jens Axboe06058632019-04-13 09:26:03 -06003163
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003164 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06003165
Jens Axboe6c271ce2019-01-10 11:22:30 -07003166 return 0;
3167}
3168
Jens Axboebda52162019-09-24 13:47:15 -06003169struct io_wait_queue {
3170 struct wait_queue_entry wq;
3171 struct io_ring_ctx *ctx;
3172 unsigned to_wait;
3173 unsigned nr_timeouts;
3174};
3175
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003176static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06003177{
3178 struct io_ring_ctx *ctx = iowq->ctx;
3179
3180 /*
3181 * Wake up if we have enough events, or if a timeout occured since we
3182 * started waiting. For timeouts, we always want to return to userspace,
3183 * regardless of event count.
3184 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003185 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06003186 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
3187}
3188
3189static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
3190 int wake_flags, void *key)
3191{
3192 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
3193 wq);
3194
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003195 /* use noflush == true, as we can't safely rely on locking context */
3196 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06003197 return -1;
3198
3199 return autoremove_wake_function(curr, mode, wake_flags, key);
3200}
3201
Jens Axboe2b188cc2019-01-07 10:46:33 -07003202/*
3203 * Wait until events become available, if we don't already have some. The
3204 * application must reap them itself, as they reside on the shared cq ring.
3205 */
3206static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
3207 const sigset_t __user *sig, size_t sigsz)
3208{
Jens Axboebda52162019-09-24 13:47:15 -06003209 struct io_wait_queue iowq = {
3210 .wq = {
3211 .private = current,
3212 .func = io_wake_function,
3213 .entry = LIST_HEAD_INIT(iowq.wq.entry),
3214 },
3215 .ctx = ctx,
3216 .to_wait = min_events,
3217 };
Hristo Venev75b28af2019-08-26 17:23:46 +00003218 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08003219 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003220
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003221 if (io_cqring_events(ctx, false) >= min_events)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003222 return 0;
3223
3224 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01003225#ifdef CONFIG_COMPAT
3226 if (in_compat_syscall())
3227 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07003228 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01003229 else
3230#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07003231 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01003232
Jens Axboe2b188cc2019-01-07 10:46:33 -07003233 if (ret)
3234 return ret;
3235 }
3236
Jens Axboebda52162019-09-24 13:47:15 -06003237 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02003238 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06003239 do {
3240 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
3241 TASK_INTERRUPTIBLE);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003242 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06003243 break;
3244 schedule();
3245 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08003246 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06003247 break;
3248 }
3249 } while (1);
3250 finish_wait(&ctx->wait, &iowq.wq);
3251
Jackie Liue9ffa5c2019-10-29 11:16:42 +08003252 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003253
Hristo Venev75b28af2019-08-26 17:23:46 +00003254 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003255}
3256
Jens Axboe6b063142019-01-10 22:13:58 -07003257static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
3258{
3259#if defined(CONFIG_UNIX)
3260 if (ctx->ring_sock) {
3261 struct sock *sock = ctx->ring_sock->sk;
3262 struct sk_buff *skb;
3263
3264 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
3265 kfree_skb(skb);
3266 }
3267#else
3268 int i;
3269
Jens Axboe65e19f52019-10-26 07:20:21 -06003270 for (i = 0; i < ctx->nr_user_files; i++) {
3271 struct file *file;
3272
3273 file = io_file_from_index(ctx, i);
3274 if (file)
3275 fput(file);
3276 }
Jens Axboe6b063142019-01-10 22:13:58 -07003277#endif
3278}
3279
3280static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
3281{
Jens Axboe65e19f52019-10-26 07:20:21 -06003282 unsigned nr_tables, i;
3283
3284 if (!ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003285 return -ENXIO;
3286
3287 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06003288 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
3289 for (i = 0; i < nr_tables; i++)
3290 kfree(ctx->file_table[i].files);
3291 kfree(ctx->file_table);
3292 ctx->file_table = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003293 ctx->nr_user_files = 0;
3294 return 0;
3295}
3296
Jens Axboe6c271ce2019-01-10 11:22:30 -07003297static void io_sq_thread_stop(struct io_ring_ctx *ctx)
3298{
3299 if (ctx->sqo_thread) {
Jens Axboe206aefd2019-11-07 18:27:42 -07003300 wait_for_completion(&ctx->completions[1]);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003301 /*
3302 * The park is a bit of a work-around, without it we get
3303 * warning spews on shutdown with SQPOLL set and affinity
3304 * set to a single CPU.
3305 */
Jens Axboe06058632019-04-13 09:26:03 -06003306 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003307 kthread_stop(ctx->sqo_thread);
3308 ctx->sqo_thread = NULL;
3309 }
3310}
3311
Jens Axboe6b063142019-01-10 22:13:58 -07003312static void io_finish_async(struct io_ring_ctx *ctx)
3313{
Jens Axboe6c271ce2019-01-10 11:22:30 -07003314 io_sq_thread_stop(ctx);
3315
Jens Axboe561fb042019-10-24 07:25:42 -06003316 if (ctx->io_wq) {
3317 io_wq_destroy(ctx->io_wq);
3318 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003319 }
3320}
3321
3322#if defined(CONFIG_UNIX)
3323static void io_destruct_skb(struct sk_buff *skb)
3324{
3325 struct io_ring_ctx *ctx = skb->sk->sk_user_data;
3326
Jens Axboe561fb042019-10-24 07:25:42 -06003327 if (ctx->io_wq)
3328 io_wq_flush(ctx->io_wq);
Jens Axboe8a997342019-10-09 14:40:13 -06003329
Jens Axboe6b063142019-01-10 22:13:58 -07003330 unix_destruct_scm(skb);
3331}
3332
3333/*
3334 * Ensure the UNIX gc is aware of our file set, so we are certain that
3335 * the io_uring can be safely unregistered on process exit, even if we have
3336 * loops in the file referencing.
3337 */
3338static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
3339{
3340 struct sock *sk = ctx->ring_sock->sk;
3341 struct scm_fp_list *fpl;
3342 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06003343 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07003344
3345 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
3346 unsigned long inflight = ctx->user->unix_inflight + nr;
3347
3348 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
3349 return -EMFILE;
3350 }
3351
3352 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
3353 if (!fpl)
3354 return -ENOMEM;
3355
3356 skb = alloc_skb(0, GFP_KERNEL);
3357 if (!skb) {
3358 kfree(fpl);
3359 return -ENOMEM;
3360 }
3361
3362 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07003363
Jens Axboe08a45172019-10-03 08:11:03 -06003364 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07003365 fpl->user = get_uid(ctx->user);
3366 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003367 struct file *file = io_file_from_index(ctx, i + offset);
3368
3369 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06003370 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06003371 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06003372 unix_inflight(fpl->user, fpl->fp[nr_files]);
3373 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07003374 }
3375
Jens Axboe08a45172019-10-03 08:11:03 -06003376 if (nr_files) {
3377 fpl->max = SCM_MAX_FD;
3378 fpl->count = nr_files;
3379 UNIXCB(skb).fp = fpl;
3380 skb->destructor = io_destruct_skb;
3381 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
3382 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07003383
Jens Axboe08a45172019-10-03 08:11:03 -06003384 for (i = 0; i < nr_files; i++)
3385 fput(fpl->fp[i]);
3386 } else {
3387 kfree_skb(skb);
3388 kfree(fpl);
3389 }
Jens Axboe6b063142019-01-10 22:13:58 -07003390
3391 return 0;
3392}
3393
3394/*
3395 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
3396 * causes regular reference counting to break down. We rely on the UNIX
3397 * garbage collection to take care of this problem for us.
3398 */
3399static int io_sqe_files_scm(struct io_ring_ctx *ctx)
3400{
3401 unsigned left, total;
3402 int ret = 0;
3403
3404 total = 0;
3405 left = ctx->nr_user_files;
3406 while (left) {
3407 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07003408
3409 ret = __io_sqe_files_scm(ctx, this_files, total);
3410 if (ret)
3411 break;
3412 left -= this_files;
3413 total += this_files;
3414 }
3415
3416 if (!ret)
3417 return 0;
3418
3419 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003420 struct file *file = io_file_from_index(ctx, total);
3421
3422 if (file)
3423 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07003424 total++;
3425 }
3426
3427 return ret;
3428}
3429#else
3430static int io_sqe_files_scm(struct io_ring_ctx *ctx)
3431{
3432 return 0;
3433}
3434#endif
3435
Jens Axboe65e19f52019-10-26 07:20:21 -06003436static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
3437 unsigned nr_files)
3438{
3439 int i;
3440
3441 for (i = 0; i < nr_tables; i++) {
3442 struct fixed_file_table *table = &ctx->file_table[i];
3443 unsigned this_files;
3444
3445 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
3446 table->files = kcalloc(this_files, sizeof(struct file *),
3447 GFP_KERNEL);
3448 if (!table->files)
3449 break;
3450 nr_files -= this_files;
3451 }
3452
3453 if (i == nr_tables)
3454 return 0;
3455
3456 for (i = 0; i < nr_tables; i++) {
3457 struct fixed_file_table *table = &ctx->file_table[i];
3458 kfree(table->files);
3459 }
3460 return 1;
3461}
3462
Jens Axboe6b063142019-01-10 22:13:58 -07003463static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
3464 unsigned nr_args)
3465{
3466 __s32 __user *fds = (__s32 __user *) arg;
Jens Axboe65e19f52019-10-26 07:20:21 -06003467 unsigned nr_tables;
Jens Axboe6b063142019-01-10 22:13:58 -07003468 int fd, ret = 0;
3469 unsigned i;
3470
Jens Axboe65e19f52019-10-26 07:20:21 -06003471 if (ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003472 return -EBUSY;
3473 if (!nr_args)
3474 return -EINVAL;
3475 if (nr_args > IORING_MAX_FIXED_FILES)
3476 return -EMFILE;
3477
Jens Axboe65e19f52019-10-26 07:20:21 -06003478 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
3479 ctx->file_table = kcalloc(nr_tables, sizeof(struct fixed_file_table),
3480 GFP_KERNEL);
3481 if (!ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003482 return -ENOMEM;
3483
Jens Axboe65e19f52019-10-26 07:20:21 -06003484 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
3485 kfree(ctx->file_table);
3486 return -ENOMEM;
3487 }
3488
Jens Axboe08a45172019-10-03 08:11:03 -06003489 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003490 struct fixed_file_table *table;
3491 unsigned index;
3492
Jens Axboe6b063142019-01-10 22:13:58 -07003493 ret = -EFAULT;
3494 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
3495 break;
Jens Axboe08a45172019-10-03 08:11:03 -06003496 /* allow sparse sets */
3497 if (fd == -1) {
3498 ret = 0;
3499 continue;
3500 }
Jens Axboe6b063142019-01-10 22:13:58 -07003501
Jens Axboe65e19f52019-10-26 07:20:21 -06003502 table = &ctx->file_table[i >> IORING_FILE_TABLE_SHIFT];
3503 index = i & IORING_FILE_TABLE_MASK;
3504 table->files[index] = fget(fd);
Jens Axboe6b063142019-01-10 22:13:58 -07003505
3506 ret = -EBADF;
Jens Axboe65e19f52019-10-26 07:20:21 -06003507 if (!table->files[index])
Jens Axboe6b063142019-01-10 22:13:58 -07003508 break;
3509 /*
3510 * Don't allow io_uring instances to be registered. If UNIX
3511 * isn't enabled, then this causes a reference cycle and this
3512 * instance can never get freed. If UNIX is enabled we'll
3513 * handle it just fine, but there's still no point in allowing
3514 * a ring fd as it doesn't support regular read/write anyway.
3515 */
Jens Axboe65e19f52019-10-26 07:20:21 -06003516 if (table->files[index]->f_op == &io_uring_fops) {
3517 fput(table->files[index]);
Jens Axboe6b063142019-01-10 22:13:58 -07003518 break;
3519 }
Jens Axboe6b063142019-01-10 22:13:58 -07003520 ret = 0;
3521 }
3522
3523 if (ret) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003524 for (i = 0; i < ctx->nr_user_files; i++) {
3525 struct file *file;
Jens Axboe6b063142019-01-10 22:13:58 -07003526
Jens Axboe65e19f52019-10-26 07:20:21 -06003527 file = io_file_from_index(ctx, i);
3528 if (file)
3529 fput(file);
3530 }
3531 for (i = 0; i < nr_tables; i++)
3532 kfree(ctx->file_table[i].files);
3533
3534 kfree(ctx->file_table);
3535 ctx->file_table = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003536 ctx->nr_user_files = 0;
3537 return ret;
3538 }
3539
3540 ret = io_sqe_files_scm(ctx);
3541 if (ret)
3542 io_sqe_files_unregister(ctx);
3543
3544 return ret;
3545}
3546
Jens Axboec3a31e62019-10-03 13:59:56 -06003547static void io_sqe_file_unregister(struct io_ring_ctx *ctx, int index)
3548{
3549#if defined(CONFIG_UNIX)
Jens Axboe65e19f52019-10-26 07:20:21 -06003550 struct file *file = io_file_from_index(ctx, index);
Jens Axboec3a31e62019-10-03 13:59:56 -06003551 struct sock *sock = ctx->ring_sock->sk;
3552 struct sk_buff_head list, *head = &sock->sk_receive_queue;
3553 struct sk_buff *skb;
3554 int i;
3555
3556 __skb_queue_head_init(&list);
3557
3558 /*
3559 * Find the skb that holds this file in its SCM_RIGHTS. When found,
3560 * remove this entry and rearrange the file array.
3561 */
3562 skb = skb_dequeue(head);
3563 while (skb) {
3564 struct scm_fp_list *fp;
3565
3566 fp = UNIXCB(skb).fp;
3567 for (i = 0; i < fp->count; i++) {
3568 int left;
3569
3570 if (fp->fp[i] != file)
3571 continue;
3572
3573 unix_notinflight(fp->user, fp->fp[i]);
3574 left = fp->count - 1 - i;
3575 if (left) {
3576 memmove(&fp->fp[i], &fp->fp[i + 1],
3577 left * sizeof(struct file *));
3578 }
3579 fp->count--;
3580 if (!fp->count) {
3581 kfree_skb(skb);
3582 skb = NULL;
3583 } else {
3584 __skb_queue_tail(&list, skb);
3585 }
3586 fput(file);
3587 file = NULL;
3588 break;
3589 }
3590
3591 if (!file)
3592 break;
3593
3594 __skb_queue_tail(&list, skb);
3595
3596 skb = skb_dequeue(head);
3597 }
3598
3599 if (skb_peek(&list)) {
3600 spin_lock_irq(&head->lock);
3601 while ((skb = __skb_dequeue(&list)) != NULL)
3602 __skb_queue_tail(head, skb);
3603 spin_unlock_irq(&head->lock);
3604 }
3605#else
Jens Axboe65e19f52019-10-26 07:20:21 -06003606 fput(io_file_from_index(ctx, index));
Jens Axboec3a31e62019-10-03 13:59:56 -06003607#endif
3608}
3609
3610static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
3611 int index)
3612{
3613#if defined(CONFIG_UNIX)
3614 struct sock *sock = ctx->ring_sock->sk;
3615 struct sk_buff_head *head = &sock->sk_receive_queue;
3616 struct sk_buff *skb;
3617
3618 /*
3619 * See if we can merge this file into an existing skb SCM_RIGHTS
3620 * file set. If there's no room, fall back to allocating a new skb
3621 * and filling it in.
3622 */
3623 spin_lock_irq(&head->lock);
3624 skb = skb_peek(head);
3625 if (skb) {
3626 struct scm_fp_list *fpl = UNIXCB(skb).fp;
3627
3628 if (fpl->count < SCM_MAX_FD) {
3629 __skb_unlink(skb, head);
3630 spin_unlock_irq(&head->lock);
3631 fpl->fp[fpl->count] = get_file(file);
3632 unix_inflight(fpl->user, fpl->fp[fpl->count]);
3633 fpl->count++;
3634 spin_lock_irq(&head->lock);
3635 __skb_queue_head(head, skb);
3636 } else {
3637 skb = NULL;
3638 }
3639 }
3640 spin_unlock_irq(&head->lock);
3641
3642 if (skb) {
3643 fput(file);
3644 return 0;
3645 }
3646
3647 return __io_sqe_files_scm(ctx, 1, index);
3648#else
3649 return 0;
3650#endif
3651}
3652
3653static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
3654 unsigned nr_args)
3655{
3656 struct io_uring_files_update up;
3657 __s32 __user *fds;
3658 int fd, i, err;
3659 __u32 done;
3660
Jens Axboe65e19f52019-10-26 07:20:21 -06003661 if (!ctx->file_table)
Jens Axboec3a31e62019-10-03 13:59:56 -06003662 return -ENXIO;
3663 if (!nr_args)
3664 return -EINVAL;
3665 if (copy_from_user(&up, arg, sizeof(up)))
3666 return -EFAULT;
3667 if (check_add_overflow(up.offset, nr_args, &done))
3668 return -EOVERFLOW;
3669 if (done > ctx->nr_user_files)
3670 return -EINVAL;
3671
3672 done = 0;
3673 fds = (__s32 __user *) up.fds;
3674 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003675 struct fixed_file_table *table;
3676 unsigned index;
3677
Jens Axboec3a31e62019-10-03 13:59:56 -06003678 err = 0;
3679 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
3680 err = -EFAULT;
3681 break;
3682 }
3683 i = array_index_nospec(up.offset, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06003684 table = &ctx->file_table[i >> IORING_FILE_TABLE_SHIFT];
3685 index = i & IORING_FILE_TABLE_MASK;
3686 if (table->files[index]) {
Jens Axboec3a31e62019-10-03 13:59:56 -06003687 io_sqe_file_unregister(ctx, i);
Jens Axboe65e19f52019-10-26 07:20:21 -06003688 table->files[index] = NULL;
Jens Axboec3a31e62019-10-03 13:59:56 -06003689 }
3690 if (fd != -1) {
3691 struct file *file;
3692
3693 file = fget(fd);
3694 if (!file) {
3695 err = -EBADF;
3696 break;
3697 }
3698 /*
3699 * Don't allow io_uring instances to be registered. If
3700 * UNIX isn't enabled, then this causes a reference
3701 * cycle and this instance can never get freed. If UNIX
3702 * is enabled we'll handle it just fine, but there's
3703 * still no point in allowing a ring fd as it doesn't
3704 * support regular read/write anyway.
3705 */
3706 if (file->f_op == &io_uring_fops) {
3707 fput(file);
3708 err = -EBADF;
3709 break;
3710 }
Jens Axboe65e19f52019-10-26 07:20:21 -06003711 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06003712 err = io_sqe_file_register(ctx, file, i);
3713 if (err)
3714 break;
3715 }
3716 nr_args--;
3717 done++;
3718 up.offset++;
3719 }
3720
3721 return done ? done : err;
3722}
3723
Jens Axboe6c271ce2019-01-10 11:22:30 -07003724static int io_sq_offload_start(struct io_ring_ctx *ctx,
3725 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003726{
Jens Axboe561fb042019-10-24 07:25:42 -06003727 unsigned concurrency;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003728 int ret;
3729
Jens Axboe6c271ce2019-01-10 11:22:30 -07003730 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003731 mmgrab(current->mm);
3732 ctx->sqo_mm = current->mm;
3733
Jens Axboe6c271ce2019-01-10 11:22:30 -07003734 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06003735 ret = -EPERM;
3736 if (!capable(CAP_SYS_ADMIN))
3737 goto err;
3738
Jens Axboe917257d2019-04-13 09:28:55 -06003739 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
3740 if (!ctx->sq_thread_idle)
3741 ctx->sq_thread_idle = HZ;
3742
Jens Axboe6c271ce2019-01-10 11:22:30 -07003743 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06003744 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003745
Jens Axboe917257d2019-04-13 09:28:55 -06003746 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06003747 if (cpu >= nr_cpu_ids)
3748 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08003749 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06003750 goto err;
3751
Jens Axboe6c271ce2019-01-10 11:22:30 -07003752 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
3753 ctx, cpu,
3754 "io_uring-sq");
3755 } else {
3756 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
3757 "io_uring-sq");
3758 }
3759 if (IS_ERR(ctx->sqo_thread)) {
3760 ret = PTR_ERR(ctx->sqo_thread);
3761 ctx->sqo_thread = NULL;
3762 goto err;
3763 }
3764 wake_up_process(ctx->sqo_thread);
3765 } else if (p->flags & IORING_SETUP_SQ_AFF) {
3766 /* Can't have SQ_AFF without SQPOLL */
3767 ret = -EINVAL;
3768 goto err;
3769 }
3770
Jens Axboe561fb042019-10-24 07:25:42 -06003771 /* Do QD, or 4 * CPUS, whatever is smallest */
3772 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
Jens Axboe5f8fd2d32019-11-07 10:57:36 -07003773 ctx->io_wq = io_wq_create(concurrency, ctx->sqo_mm, ctx->user);
Jens Axboe975c99a52019-10-30 08:42:56 -06003774 if (IS_ERR(ctx->io_wq)) {
3775 ret = PTR_ERR(ctx->io_wq);
3776 ctx->io_wq = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003777 goto err;
3778 }
3779
3780 return 0;
3781err:
Jens Axboe54a91f32019-09-10 09:15:04 -06003782 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003783 mmdrop(ctx->sqo_mm);
3784 ctx->sqo_mm = NULL;
3785 return ret;
3786}
3787
3788static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
3789{
3790 atomic_long_sub(nr_pages, &user->locked_vm);
3791}
3792
3793static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
3794{
3795 unsigned long page_limit, cur_pages, new_pages;
3796
3797 /* Don't allow more pages than we can safely lock */
3798 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
3799
3800 do {
3801 cur_pages = atomic_long_read(&user->locked_vm);
3802 new_pages = cur_pages + nr_pages;
3803 if (new_pages > page_limit)
3804 return -ENOMEM;
3805 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
3806 new_pages) != cur_pages);
3807
3808 return 0;
3809}
3810
3811static void io_mem_free(void *ptr)
3812{
Mark Rutland52e04ef2019-04-30 17:30:21 +01003813 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003814
Mark Rutland52e04ef2019-04-30 17:30:21 +01003815 if (!ptr)
3816 return;
3817
3818 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003819 if (put_page_testzero(page))
3820 free_compound_page(page);
3821}
3822
3823static void *io_mem_alloc(size_t size)
3824{
3825 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
3826 __GFP_NORETRY;
3827
3828 return (void *) __get_free_pages(gfp_flags, get_order(size));
3829}
3830
Hristo Venev75b28af2019-08-26 17:23:46 +00003831static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
3832 size_t *sq_offset)
3833{
3834 struct io_rings *rings;
3835 size_t off, sq_array_size;
3836
3837 off = struct_size(rings, cqes, cq_entries);
3838 if (off == SIZE_MAX)
3839 return SIZE_MAX;
3840
3841#ifdef CONFIG_SMP
3842 off = ALIGN(off, SMP_CACHE_BYTES);
3843 if (off == 0)
3844 return SIZE_MAX;
3845#endif
3846
3847 sq_array_size = array_size(sizeof(u32), sq_entries);
3848 if (sq_array_size == SIZE_MAX)
3849 return SIZE_MAX;
3850
3851 if (check_add_overflow(off, sq_array_size, &off))
3852 return SIZE_MAX;
3853
3854 if (sq_offset)
3855 *sq_offset = off;
3856
3857 return off;
3858}
3859
Jens Axboe2b188cc2019-01-07 10:46:33 -07003860static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
3861{
Hristo Venev75b28af2019-08-26 17:23:46 +00003862 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003863
Hristo Venev75b28af2019-08-26 17:23:46 +00003864 pages = (size_t)1 << get_order(
3865 rings_size(sq_entries, cq_entries, NULL));
3866 pages += (size_t)1 << get_order(
3867 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07003868
Hristo Venev75b28af2019-08-26 17:23:46 +00003869 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003870}
3871
Jens Axboeedafcce2019-01-09 09:16:05 -07003872static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
3873{
3874 int i, j;
3875
3876 if (!ctx->user_bufs)
3877 return -ENXIO;
3878
3879 for (i = 0; i < ctx->nr_user_bufs; i++) {
3880 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
3881
3882 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbard27c4d3a2019-08-04 19:32:06 -07003883 put_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07003884
3885 if (ctx->account_mem)
3886 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003887 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07003888 imu->nr_bvecs = 0;
3889 }
3890
3891 kfree(ctx->user_bufs);
3892 ctx->user_bufs = NULL;
3893 ctx->nr_user_bufs = 0;
3894 return 0;
3895}
3896
3897static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
3898 void __user *arg, unsigned index)
3899{
3900 struct iovec __user *src;
3901
3902#ifdef CONFIG_COMPAT
3903 if (ctx->compat) {
3904 struct compat_iovec __user *ciovs;
3905 struct compat_iovec ciov;
3906
3907 ciovs = (struct compat_iovec __user *) arg;
3908 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
3909 return -EFAULT;
3910
3911 dst->iov_base = (void __user *) (unsigned long) ciov.iov_base;
3912 dst->iov_len = ciov.iov_len;
3913 return 0;
3914 }
3915#endif
3916 src = (struct iovec __user *) arg;
3917 if (copy_from_user(dst, &src[index], sizeof(*dst)))
3918 return -EFAULT;
3919 return 0;
3920}
3921
3922static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
3923 unsigned nr_args)
3924{
3925 struct vm_area_struct **vmas = NULL;
3926 struct page **pages = NULL;
3927 int i, j, got_pages = 0;
3928 int ret = -EINVAL;
3929
3930 if (ctx->user_bufs)
3931 return -EBUSY;
3932 if (!nr_args || nr_args > UIO_MAXIOV)
3933 return -EINVAL;
3934
3935 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
3936 GFP_KERNEL);
3937 if (!ctx->user_bufs)
3938 return -ENOMEM;
3939
3940 for (i = 0; i < nr_args; i++) {
3941 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
3942 unsigned long off, start, end, ubuf;
3943 int pret, nr_pages;
3944 struct iovec iov;
3945 size_t size;
3946
3947 ret = io_copy_iov(ctx, &iov, arg, i);
3948 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03003949 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07003950
3951 /*
3952 * Don't impose further limits on the size and buffer
3953 * constraints here, we'll -EINVAL later when IO is
3954 * submitted if they are wrong.
3955 */
3956 ret = -EFAULT;
3957 if (!iov.iov_base || !iov.iov_len)
3958 goto err;
3959
3960 /* arbitrary limit, but we need something */
3961 if (iov.iov_len > SZ_1G)
3962 goto err;
3963
3964 ubuf = (unsigned long) iov.iov_base;
3965 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
3966 start = ubuf >> PAGE_SHIFT;
3967 nr_pages = end - start;
3968
3969 if (ctx->account_mem) {
3970 ret = io_account_mem(ctx->user, nr_pages);
3971 if (ret)
3972 goto err;
3973 }
3974
3975 ret = 0;
3976 if (!pages || nr_pages > got_pages) {
3977 kfree(vmas);
3978 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003979 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07003980 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003981 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07003982 sizeof(struct vm_area_struct *),
3983 GFP_KERNEL);
3984 if (!pages || !vmas) {
3985 ret = -ENOMEM;
3986 if (ctx->account_mem)
3987 io_unaccount_mem(ctx->user, nr_pages);
3988 goto err;
3989 }
3990 got_pages = nr_pages;
3991 }
3992
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003993 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07003994 GFP_KERNEL);
3995 ret = -ENOMEM;
3996 if (!imu->bvec) {
3997 if (ctx->account_mem)
3998 io_unaccount_mem(ctx->user, nr_pages);
3999 goto err;
4000 }
4001
4002 ret = 0;
4003 down_read(&current->mm->mmap_sem);
Ira Weiny932f4a62019-05-13 17:17:03 -07004004 pret = get_user_pages(ubuf, nr_pages,
4005 FOLL_WRITE | FOLL_LONGTERM,
4006 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07004007 if (pret == nr_pages) {
4008 /* don't support file backed memory */
4009 for (j = 0; j < nr_pages; j++) {
4010 struct vm_area_struct *vma = vmas[j];
4011
4012 if (vma->vm_file &&
4013 !is_file_hugepages(vma->vm_file)) {
4014 ret = -EOPNOTSUPP;
4015 break;
4016 }
4017 }
4018 } else {
4019 ret = pret < 0 ? pret : -EFAULT;
4020 }
4021 up_read(&current->mm->mmap_sem);
4022 if (ret) {
4023 /*
4024 * if we did partial map, or found file backed vmas,
4025 * release any pages we did get
4026 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07004027 if (pret > 0)
4028 put_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07004029 if (ctx->account_mem)
4030 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004031 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07004032 goto err;
4033 }
4034
4035 off = ubuf & ~PAGE_MASK;
4036 size = iov.iov_len;
4037 for (j = 0; j < nr_pages; j++) {
4038 size_t vec_len;
4039
4040 vec_len = min_t(size_t, size, PAGE_SIZE - off);
4041 imu->bvec[j].bv_page = pages[j];
4042 imu->bvec[j].bv_len = vec_len;
4043 imu->bvec[j].bv_offset = off;
4044 off = 0;
4045 size -= vec_len;
4046 }
4047 /* store original address for later verification */
4048 imu->ubuf = ubuf;
4049 imu->len = iov.iov_len;
4050 imu->nr_bvecs = nr_pages;
4051
4052 ctx->nr_user_bufs++;
4053 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004054 kvfree(pages);
4055 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07004056 return 0;
4057err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004058 kvfree(pages);
4059 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07004060 io_sqe_buffer_unregister(ctx);
4061 return ret;
4062}
4063
Jens Axboe9b402842019-04-11 11:45:41 -06004064static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
4065{
4066 __s32 __user *fds = arg;
4067 int fd;
4068
4069 if (ctx->cq_ev_fd)
4070 return -EBUSY;
4071
4072 if (copy_from_user(&fd, fds, sizeof(*fds)))
4073 return -EFAULT;
4074
4075 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
4076 if (IS_ERR(ctx->cq_ev_fd)) {
4077 int ret = PTR_ERR(ctx->cq_ev_fd);
4078 ctx->cq_ev_fd = NULL;
4079 return ret;
4080 }
4081
4082 return 0;
4083}
4084
4085static int io_eventfd_unregister(struct io_ring_ctx *ctx)
4086{
4087 if (ctx->cq_ev_fd) {
4088 eventfd_ctx_put(ctx->cq_ev_fd);
4089 ctx->cq_ev_fd = NULL;
4090 return 0;
4091 }
4092
4093 return -ENXIO;
4094}
4095
Jens Axboe2b188cc2019-01-07 10:46:33 -07004096static void io_ring_ctx_free(struct io_ring_ctx *ctx)
4097{
Jens Axboe6b063142019-01-10 22:13:58 -07004098 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004099 if (ctx->sqo_mm)
4100 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07004101
4102 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07004103 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07004104 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06004105 io_eventfd_unregister(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07004106
Jens Axboe2b188cc2019-01-07 10:46:33 -07004107#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07004108 if (ctx->ring_sock) {
4109 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004110 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07004111 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004112#endif
4113
Hristo Venev75b28af2019-08-26 17:23:46 +00004114 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004115 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004116
4117 percpu_ref_exit(&ctx->refs);
4118 if (ctx->account_mem)
4119 io_unaccount_mem(ctx->user,
4120 ring_pages(ctx->sq_entries, ctx->cq_entries));
4121 free_uid(ctx->user);
Jens Axboe206aefd2019-11-07 18:27:42 -07004122 kfree(ctx->completions);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004123 kfree(ctx);
4124}
4125
4126static __poll_t io_uring_poll(struct file *file, poll_table *wait)
4127{
4128 struct io_ring_ctx *ctx = file->private_data;
4129 __poll_t mask = 0;
4130
4131 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02004132 /*
4133 * synchronizes with barrier from wq_has_sleeper call in
4134 * io_commit_cqring
4135 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004136 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00004137 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
4138 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004139 mask |= EPOLLOUT | EPOLLWRNORM;
yangerkundaa5de52019-09-24 20:53:34 +08004140 if (READ_ONCE(ctx->rings->cq.head) != ctx->cached_cq_tail)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004141 mask |= EPOLLIN | EPOLLRDNORM;
4142
4143 return mask;
4144}
4145
4146static int io_uring_fasync(int fd, struct file *file, int on)
4147{
4148 struct io_ring_ctx *ctx = file->private_data;
4149
4150 return fasync_helper(fd, file, on, &ctx->cq_fasync);
4151}
4152
4153static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
4154{
4155 mutex_lock(&ctx->uring_lock);
4156 percpu_ref_kill(&ctx->refs);
4157 mutex_unlock(&ctx->uring_lock);
4158
Jens Axboe5262f562019-09-17 12:26:57 -06004159 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004160 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06004161
4162 if (ctx->io_wq)
4163 io_wq_cancel_all(ctx->io_wq);
4164
Jens Axboedef596e2019-01-09 08:59:42 -07004165 io_iopoll_reap_events(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004166 io_cqring_overflow_flush(ctx, true);
Jens Axboe206aefd2019-11-07 18:27:42 -07004167 wait_for_completion(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004168 io_ring_ctx_free(ctx);
4169}
4170
4171static int io_uring_release(struct inode *inode, struct file *file)
4172{
4173 struct io_ring_ctx *ctx = file->private_data;
4174
4175 file->private_data = NULL;
4176 io_ring_ctx_wait_and_kill(ctx);
4177 return 0;
4178}
4179
Jens Axboefcb323c2019-10-24 12:39:47 -06004180static void io_uring_cancel_files(struct io_ring_ctx *ctx,
4181 struct files_struct *files)
4182{
4183 struct io_kiocb *req;
4184 DEFINE_WAIT(wait);
4185
4186 while (!list_empty_careful(&ctx->inflight_list)) {
4187 enum io_wq_cancel ret = IO_WQ_CANCEL_NOTFOUND;
4188
4189 spin_lock_irq(&ctx->inflight_lock);
4190 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
4191 if (req->work.files == files) {
4192 ret = io_wq_cancel_work(ctx->io_wq, &req->work);
4193 break;
4194 }
4195 }
4196 if (ret == IO_WQ_CANCEL_RUNNING)
4197 prepare_to_wait(&ctx->inflight_wait, &wait,
4198 TASK_UNINTERRUPTIBLE);
4199
4200 spin_unlock_irq(&ctx->inflight_lock);
4201
4202 /*
4203 * We need to keep going until we get NOTFOUND. We only cancel
4204 * one work at the time.
4205 *
4206 * If we get CANCEL_RUNNING, then wait for a work to complete
4207 * before continuing.
4208 */
4209 if (ret == IO_WQ_CANCEL_OK)
4210 continue;
4211 else if (ret != IO_WQ_CANCEL_RUNNING)
4212 break;
4213 schedule();
4214 }
4215}
4216
4217static int io_uring_flush(struct file *file, void *data)
4218{
4219 struct io_ring_ctx *ctx = file->private_data;
4220
4221 io_uring_cancel_files(ctx, data);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004222 if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) {
4223 io_cqring_overflow_flush(ctx, true);
Jens Axboefcb323c2019-10-24 12:39:47 -06004224 io_wq_cancel_all(ctx->io_wq);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004225 }
Jens Axboefcb323c2019-10-24 12:39:47 -06004226 return 0;
4227}
4228
Jens Axboe2b188cc2019-01-07 10:46:33 -07004229static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
4230{
4231 loff_t offset = (loff_t) vma->vm_pgoff << PAGE_SHIFT;
4232 unsigned long sz = vma->vm_end - vma->vm_start;
4233 struct io_ring_ctx *ctx = file->private_data;
4234 unsigned long pfn;
4235 struct page *page;
4236 void *ptr;
4237
4238 switch (offset) {
4239 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00004240 case IORING_OFF_CQ_RING:
4241 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004242 break;
4243 case IORING_OFF_SQES:
4244 ptr = ctx->sq_sqes;
4245 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004246 default:
4247 return -EINVAL;
4248 }
4249
4250 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07004251 if (sz > page_size(page))
Jens Axboe2b188cc2019-01-07 10:46:33 -07004252 return -EINVAL;
4253
4254 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
4255 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
4256}
4257
4258SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
4259 u32, min_complete, u32, flags, const sigset_t __user *, sig,
4260 size_t, sigsz)
4261{
4262 struct io_ring_ctx *ctx;
4263 long ret = -EBADF;
4264 int submitted = 0;
4265 struct fd f;
4266
Jens Axboe6c271ce2019-01-10 11:22:30 -07004267 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07004268 return -EINVAL;
4269
4270 f = fdget(fd);
4271 if (!f.file)
4272 return -EBADF;
4273
4274 ret = -EOPNOTSUPP;
4275 if (f.file->f_op != &io_uring_fops)
4276 goto out_fput;
4277
4278 ret = -ENXIO;
4279 ctx = f.file->private_data;
4280 if (!percpu_ref_tryget(&ctx->refs))
4281 goto out_fput;
4282
Jens Axboe6c271ce2019-01-10 11:22:30 -07004283 /*
4284 * For SQ polling, the thread will do all submissions and completions.
4285 * Just return the requested submit count, and wake the thread if
4286 * we were asked to.
4287 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06004288 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004289 if (ctx->flags & IORING_SETUP_SQPOLL) {
4290 if (flags & IORING_ENTER_SQ_WAKEUP)
4291 wake_up(&ctx->sqo_wait);
4292 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06004293 } else if (to_submit) {
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004294 struct mm_struct *cur_mm;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004295
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004296 to_submit = min(to_submit, ctx->sq_entries);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004297 mutex_lock(&ctx->uring_lock);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004298 /* already have mm, so io_submit_sqes() won't try to grab it */
4299 cur_mm = ctx->sqo_mm;
4300 submitted = io_submit_sqes(ctx, to_submit, f.file, fd,
4301 &cur_mm, false);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004302 mutex_unlock(&ctx->uring_lock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004303 }
4304 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07004305 unsigned nr_events = 0;
4306
Jens Axboe2b188cc2019-01-07 10:46:33 -07004307 min_complete = min(min_complete, ctx->cq_entries);
4308
Jens Axboedef596e2019-01-09 08:59:42 -07004309 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07004310 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07004311 } else {
4312 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
4313 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004314 }
4315
Pavel Begunkov6805b322019-10-08 02:18:42 +03004316 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004317out_fput:
4318 fdput(f);
4319 return submitted ? submitted : ret;
4320}
4321
4322static const struct file_operations io_uring_fops = {
4323 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06004324 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07004325 .mmap = io_uring_mmap,
4326 .poll = io_uring_poll,
4327 .fasync = io_uring_fasync,
4328};
4329
4330static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
4331 struct io_uring_params *p)
4332{
Hristo Venev75b28af2019-08-26 17:23:46 +00004333 struct io_rings *rings;
4334 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004335
Hristo Venev75b28af2019-08-26 17:23:46 +00004336 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
4337 if (size == SIZE_MAX)
4338 return -EOVERFLOW;
4339
4340 rings = io_mem_alloc(size);
4341 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004342 return -ENOMEM;
4343
Hristo Venev75b28af2019-08-26 17:23:46 +00004344 ctx->rings = rings;
4345 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
4346 rings->sq_ring_mask = p->sq_entries - 1;
4347 rings->cq_ring_mask = p->cq_entries - 1;
4348 rings->sq_ring_entries = p->sq_entries;
4349 rings->cq_ring_entries = p->cq_entries;
4350 ctx->sq_mask = rings->sq_ring_mask;
4351 ctx->cq_mask = rings->cq_ring_mask;
4352 ctx->sq_entries = rings->sq_ring_entries;
4353 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004354
4355 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
4356 if (size == SIZE_MAX)
4357 return -EOVERFLOW;
4358
4359 ctx->sq_sqes = io_mem_alloc(size);
Mark Rutland52e04ef2019-04-30 17:30:21 +01004360 if (!ctx->sq_sqes)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004361 return -ENOMEM;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004362
Jens Axboe2b188cc2019-01-07 10:46:33 -07004363 return 0;
4364}
4365
4366/*
4367 * Allocate an anonymous fd, this is what constitutes the application
4368 * visible backing of an io_uring instance. The application mmaps this
4369 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
4370 * we have to tie this fd to a socket for file garbage collection purposes.
4371 */
4372static int io_uring_get_fd(struct io_ring_ctx *ctx)
4373{
4374 struct file *file;
4375 int ret;
4376
4377#if defined(CONFIG_UNIX)
4378 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
4379 &ctx->ring_sock);
4380 if (ret)
4381 return ret;
4382#endif
4383
4384 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
4385 if (ret < 0)
4386 goto err;
4387
4388 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
4389 O_RDWR | O_CLOEXEC);
4390 if (IS_ERR(file)) {
4391 put_unused_fd(ret);
4392 ret = PTR_ERR(file);
4393 goto err;
4394 }
4395
4396#if defined(CONFIG_UNIX)
4397 ctx->ring_sock->file = file;
Jens Axboe6b063142019-01-10 22:13:58 -07004398 ctx->ring_sock->sk->sk_user_data = ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004399#endif
4400 fd_install(ret, file);
4401 return ret;
4402err:
4403#if defined(CONFIG_UNIX)
4404 sock_release(ctx->ring_sock);
4405 ctx->ring_sock = NULL;
4406#endif
4407 return ret;
4408}
4409
4410static int io_uring_create(unsigned entries, struct io_uring_params *p)
4411{
4412 struct user_struct *user = NULL;
4413 struct io_ring_ctx *ctx;
4414 bool account_mem;
4415 int ret;
4416
4417 if (!entries || entries > IORING_MAX_ENTRIES)
4418 return -EINVAL;
4419
4420 /*
4421 * Use twice as many entries for the CQ ring. It's possible for the
4422 * application to drive a higher depth than the size of the SQ ring,
4423 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06004424 * some flexibility in overcommitting a bit. If the application has
4425 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
4426 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07004427 */
4428 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06004429 if (p->flags & IORING_SETUP_CQSIZE) {
4430 /*
4431 * If IORING_SETUP_CQSIZE is set, we do the same roundup
4432 * to a power-of-two, if it isn't already. We do NOT impose
4433 * any cq vs sq ring sizing.
4434 */
4435 if (p->cq_entries < p->sq_entries || p->cq_entries > IORING_MAX_CQ_ENTRIES)
4436 return -EINVAL;
4437 p->cq_entries = roundup_pow_of_two(p->cq_entries);
4438 } else {
4439 p->cq_entries = 2 * p->sq_entries;
4440 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004441
4442 user = get_uid(current_user());
4443 account_mem = !capable(CAP_IPC_LOCK);
4444
4445 if (account_mem) {
4446 ret = io_account_mem(user,
4447 ring_pages(p->sq_entries, p->cq_entries));
4448 if (ret) {
4449 free_uid(user);
4450 return ret;
4451 }
4452 }
4453
4454 ctx = io_ring_ctx_alloc(p);
4455 if (!ctx) {
4456 if (account_mem)
4457 io_unaccount_mem(user, ring_pages(p->sq_entries,
4458 p->cq_entries));
4459 free_uid(user);
4460 return -ENOMEM;
4461 }
4462 ctx->compat = in_compat_syscall();
4463 ctx->account_mem = account_mem;
4464 ctx->user = user;
4465
4466 ret = io_allocate_scq_urings(ctx, p);
4467 if (ret)
4468 goto err;
4469
Jens Axboe6c271ce2019-01-10 11:22:30 -07004470 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004471 if (ret)
4472 goto err;
4473
Jens Axboe2b188cc2019-01-07 10:46:33 -07004474 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00004475 p->sq_off.head = offsetof(struct io_rings, sq.head);
4476 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
4477 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
4478 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
4479 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
4480 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
4481 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004482
4483 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00004484 p->cq_off.head = offsetof(struct io_rings, cq.head);
4485 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
4486 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
4487 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
4488 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
4489 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06004490
Jens Axboe044c1ab2019-10-28 09:15:33 -06004491 /*
4492 * Install ring fd as the very last thing, so we don't risk someone
4493 * having closed it before we finish setup
4494 */
4495 ret = io_uring_get_fd(ctx);
4496 if (ret < 0)
4497 goto err;
4498
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004499 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004500 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004501 return ret;
4502err:
4503 io_ring_ctx_wait_and_kill(ctx);
4504 return ret;
4505}
4506
4507/*
4508 * Sets up an aio uring context, and returns the fd. Applications asks for a
4509 * ring size, we return the actual sq/cq ring sizes (among other things) in the
4510 * params structure passed in.
4511 */
4512static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
4513{
4514 struct io_uring_params p;
4515 long ret;
4516 int i;
4517
4518 if (copy_from_user(&p, params, sizeof(p)))
4519 return -EFAULT;
4520 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
4521 if (p.resv[i])
4522 return -EINVAL;
4523 }
4524
Jens Axboe6c271ce2019-01-10 11:22:30 -07004525 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe33a107f2019-10-04 12:10:03 -06004526 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE))
Jens Axboe2b188cc2019-01-07 10:46:33 -07004527 return -EINVAL;
4528
4529 ret = io_uring_create(entries, &p);
4530 if (ret < 0)
4531 return ret;
4532
4533 if (copy_to_user(params, &p, sizeof(p)))
4534 return -EFAULT;
4535
4536 return ret;
4537}
4538
4539SYSCALL_DEFINE2(io_uring_setup, u32, entries,
4540 struct io_uring_params __user *, params)
4541{
4542 return io_uring_setup(entries, params);
4543}
4544
Jens Axboeedafcce2019-01-09 09:16:05 -07004545static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
4546 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06004547 __releases(ctx->uring_lock)
4548 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07004549{
4550 int ret;
4551
Jens Axboe35fa71a2019-04-22 10:23:23 -06004552 /*
4553 * We're inside the ring mutex, if the ref is already dying, then
4554 * someone else killed the ctx or is already going through
4555 * io_uring_register().
4556 */
4557 if (percpu_ref_is_dying(&ctx->refs))
4558 return -ENXIO;
4559
Jens Axboeedafcce2019-01-09 09:16:05 -07004560 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06004561
4562 /*
4563 * Drop uring mutex before waiting for references to exit. If another
4564 * thread is currently inside io_uring_enter() it might need to grab
4565 * the uring_lock to make progress. If we hold it here across the drain
4566 * wait, then we can deadlock. It's safe to drop the mutex here, since
4567 * no new references will come in after we've killed the percpu ref.
4568 */
4569 mutex_unlock(&ctx->uring_lock);
Jens Axboe206aefd2019-11-07 18:27:42 -07004570 wait_for_completion(&ctx->completions[0]);
Jens Axboeb19062a2019-04-15 10:49:38 -06004571 mutex_lock(&ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07004572
4573 switch (opcode) {
4574 case IORING_REGISTER_BUFFERS:
4575 ret = io_sqe_buffer_register(ctx, arg, nr_args);
4576 break;
4577 case IORING_UNREGISTER_BUFFERS:
4578 ret = -EINVAL;
4579 if (arg || nr_args)
4580 break;
4581 ret = io_sqe_buffer_unregister(ctx);
4582 break;
Jens Axboe6b063142019-01-10 22:13:58 -07004583 case IORING_REGISTER_FILES:
4584 ret = io_sqe_files_register(ctx, arg, nr_args);
4585 break;
4586 case IORING_UNREGISTER_FILES:
4587 ret = -EINVAL;
4588 if (arg || nr_args)
4589 break;
4590 ret = io_sqe_files_unregister(ctx);
4591 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06004592 case IORING_REGISTER_FILES_UPDATE:
4593 ret = io_sqe_files_update(ctx, arg, nr_args);
4594 break;
Jens Axboe9b402842019-04-11 11:45:41 -06004595 case IORING_REGISTER_EVENTFD:
4596 ret = -EINVAL;
4597 if (nr_args != 1)
4598 break;
4599 ret = io_eventfd_register(ctx, arg);
4600 break;
4601 case IORING_UNREGISTER_EVENTFD:
4602 ret = -EINVAL;
4603 if (arg || nr_args)
4604 break;
4605 ret = io_eventfd_unregister(ctx);
4606 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07004607 default:
4608 ret = -EINVAL;
4609 break;
4610 }
4611
4612 /* bring the ctx back to life */
Jens Axboe206aefd2019-11-07 18:27:42 -07004613 reinit_completion(&ctx->completions[0]);
Jens Axboeedafcce2019-01-09 09:16:05 -07004614 percpu_ref_reinit(&ctx->refs);
4615 return ret;
4616}
4617
4618SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
4619 void __user *, arg, unsigned int, nr_args)
4620{
4621 struct io_ring_ctx *ctx;
4622 long ret = -EBADF;
4623 struct fd f;
4624
4625 f = fdget(fd);
4626 if (!f.file)
4627 return -EBADF;
4628
4629 ret = -EOPNOTSUPP;
4630 if (f.file->f_op != &io_uring_fops)
4631 goto out_fput;
4632
4633 ctx = f.file->private_data;
4634
4635 mutex_lock(&ctx->uring_lock);
4636 ret = __io_uring_register(ctx, opcode, arg, nr_args);
4637 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004638 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
4639 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07004640out_fput:
4641 fdput(f);
4642 return ret;
4643}
4644
Jens Axboe2b188cc2019-01-07 10:46:33 -07004645static int __init io_uring_init(void)
4646{
4647 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
4648 return 0;
4649};
4650__initcall(io_uring_init);