blob: 831bea0fbc75bff4197ef1011850e455735807ca [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 Axboe2b188cc2019-01-07 10:46:33 -0700207 struct io_uring_sqe *sq_sqes;
Jens Axboede0617e2019-04-06 21:51:27 -0600208
209 struct list_head defer_list;
Jens Axboe5262f562019-09-17 12:26:57 -0600210 struct list_head timeout_list;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700211 struct list_head cq_overflow_list;
Jens Axboefcb323c2019-10-24 12:39:47 -0600212
213 wait_queue_head_t inflight_wait;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700214 } ____cacheline_aligned_in_smp;
215
216 /* IO offload */
Jens Axboe561fb042019-10-24 07:25:42 -0600217 struct io_wq *io_wq;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700218 struct task_struct *sqo_thread; /* if using sq thread polling */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700219 struct mm_struct *sqo_mm;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700220 wait_queue_head_t sqo_wait;
Jackie Liua4c0b3d2019-07-08 13:41:12 +0800221 struct completion sqo_thread_started;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700222
223 struct {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700224 unsigned cached_cq_tail;
Jens Axboe498ccd92019-10-25 10:04:25 -0600225 atomic_t cached_cq_overflow;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700226 unsigned cq_entries;
227 unsigned cq_mask;
228 struct wait_queue_head cq_wait;
229 struct fasync_struct *cq_fasync;
Jens Axboe9b402842019-04-11 11:45:41 -0600230 struct eventfd_ctx *cq_ev_fd;
Jens Axboe5262f562019-09-17 12:26:57 -0600231 atomic_t cq_timeouts;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700232 } ____cacheline_aligned_in_smp;
233
Hristo Venev75b28af2019-08-26 17:23:46 +0000234 struct io_rings *rings;
235
Jens Axboe6b063142019-01-10 22:13:58 -0700236 /*
237 * If used, fixed file set. Writers must ensure that ->refs is dead,
238 * readers must ensure that ->refs is alive as long as the file* is
239 * used. Only updated through io_uring_register(2).
240 */
Jens Axboe65e19f52019-10-26 07:20:21 -0600241 struct fixed_file_table *file_table;
Jens Axboe6b063142019-01-10 22:13:58 -0700242 unsigned nr_user_files;
243
Jens Axboeedafcce2019-01-09 09:16:05 -0700244 /* if used, fixed mapped user buffers */
245 unsigned nr_user_bufs;
246 struct io_mapped_ubuf *user_bufs;
247
Jens Axboe2b188cc2019-01-07 10:46:33 -0700248 struct user_struct *user;
249
250 struct completion ctx_done;
251
252 struct {
253 struct mutex uring_lock;
254 wait_queue_head_t wait;
255 } ____cacheline_aligned_in_smp;
256
257 struct {
258 spinlock_t completion_lock;
Jens Axboedef596e2019-01-09 08:59:42 -0700259 bool poll_multi_file;
260 /*
261 * ->poll_list is protected by the ctx->uring_lock for
262 * io_uring instances that don't use IORING_SETUP_SQPOLL.
263 * For SQPOLL, only the single threaded io_sq_thread() will
264 * manipulate the list, hence no extra locking is needed there.
265 */
266 struct list_head poll_list;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700267 struct list_head cancel_list;
Jens Axboefcb323c2019-10-24 12:39:47 -0600268
269 spinlock_t inflight_lock;
270 struct list_head inflight_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700271 } ____cacheline_aligned_in_smp;
272
273#if defined(CONFIG_UNIX)
274 struct socket *ring_sock;
275#endif
276};
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);
Jens Axboe2665abf2019-11-05 12:40:47 -0700376static void io_put_req(struct io_kiocb *req, struct io_kiocb **nxtptr);
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
400 complete(&ctx->ctx_done);
401}
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
Roman Gushchin21482892019-05-07 10:01:48 -0700411 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
412 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700413 kfree(ctx);
414 return NULL;
415 }
416
417 ctx->flags = p->flags;
418 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700419 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700420 init_completion(&ctx->ctx_done);
Jackie Liua4c0b3d2019-07-08 13:41:12 +0800421 init_completion(&ctx->sqo_thread_started);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700422 mutex_init(&ctx->uring_lock);
423 init_waitqueue_head(&ctx->wait);
424 spin_lock_init(&ctx->completion_lock);
Jens Axboedef596e2019-01-09 08:59:42 -0700425 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboe221c5eb2019-01-17 09:41:58 -0700426 INIT_LIST_HEAD(&ctx->cancel_list);
Jens Axboede0617e2019-04-06 21:51:27 -0600427 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -0600428 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -0600429 init_waitqueue_head(&ctx->inflight_wait);
430 spin_lock_init(&ctx->inflight_lock);
431 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700432 return ctx;
433}
434
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600435static inline bool __io_sequence_defer(struct io_ring_ctx *ctx,
436 struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -0600437{
Jens Axboe498ccd92019-10-25 10:04:25 -0600438 return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped
439 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboede0617e2019-04-06 21:51:27 -0600440}
441
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600442static inline bool io_sequence_defer(struct io_ring_ctx *ctx,
443 struct io_kiocb *req)
444{
445 if ((req->flags & (REQ_F_IO_DRAIN|REQ_F_IO_DRAINED)) != REQ_F_IO_DRAIN)
446 return false;
447
448 return __io_sequence_defer(ctx, req);
449}
450
451static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -0600452{
453 struct io_kiocb *req;
454
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600455 req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list);
456 if (req && !io_sequence_defer(ctx, req)) {
Jens Axboede0617e2019-04-06 21:51:27 -0600457 list_del_init(&req->list);
458 return req;
459 }
460
461 return NULL;
462}
463
Jens Axboe5262f562019-09-17 12:26:57 -0600464static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx)
465{
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600466 struct io_kiocb *req;
467
468 req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list);
469 if (req && !__io_sequence_defer(ctx, req)) {
470 list_del_init(&req->list);
471 return req;
472 }
473
474 return NULL;
Jens Axboe5262f562019-09-17 12:26:57 -0600475}
476
Jens Axboede0617e2019-04-06 21:51:27 -0600477static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700478{
Hristo Venev75b28af2019-08-26 17:23:46 +0000479 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700480
Hristo Venev75b28af2019-08-26 17:23:46 +0000481 if (ctx->cached_cq_tail != READ_ONCE(rings->cq.tail)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700482 /* order cqe stores with ring update */
Hristo Venev75b28af2019-08-26 17:23:46 +0000483 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700484
Jens Axboe2b188cc2019-01-07 10:46:33 -0700485 if (wq_has_sleeper(&ctx->cq_wait)) {
486 wake_up_interruptible(&ctx->cq_wait);
487 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
488 }
489 }
490}
491
Jens Axboe561fb042019-10-24 07:25:42 -0600492static inline bool io_sqe_needs_user(const struct io_uring_sqe *sqe)
Jens Axboe18d9be12019-09-10 09:13:05 -0600493{
Jens Axboe561fb042019-10-24 07:25:42 -0600494 u8 opcode = READ_ONCE(sqe->opcode);
495
496 return !(opcode == IORING_OP_READ_FIXED ||
497 opcode == IORING_OP_WRITE_FIXED);
498}
499
500static inline bool io_prep_async_work(struct io_kiocb *req)
501{
502 bool do_hashed = false;
Jens Axboe54a91f32019-09-10 09:15:04 -0600503
Jens Axboe6cc47d12019-09-18 11:18:23 -0600504 if (req->submit.sqe) {
505 switch (req->submit.sqe->opcode) {
506 case IORING_OP_WRITEV:
507 case IORING_OP_WRITE_FIXED:
Jens Axboe561fb042019-10-24 07:25:42 -0600508 do_hashed = true;
Jens Axboe6cc47d12019-09-18 11:18:23 -0600509 break;
510 }
Jens Axboe561fb042019-10-24 07:25:42 -0600511 if (io_sqe_needs_user(req->submit.sqe))
512 req->work.flags |= IO_WQ_WORK_NEEDS_USER;
Jens Axboe54a91f32019-09-10 09:15:04 -0600513 }
514
Jens Axboe561fb042019-10-24 07:25:42 -0600515 return do_hashed;
516}
517
518static inline void io_queue_async_work(struct io_ring_ctx *ctx,
519 struct io_kiocb *req)
520{
521 bool do_hashed = io_prep_async_work(req);
522
523 trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work,
524 req->flags);
525 if (!do_hashed) {
526 io_wq_enqueue(ctx->io_wq, &req->work);
527 } else {
528 io_wq_enqueue_hashed(ctx->io_wq, &req->work,
529 file_inode(req->file));
530 }
Jens Axboe18d9be12019-09-10 09:13:05 -0600531}
532
Jens Axboe5262f562019-09-17 12:26:57 -0600533static void io_kill_timeout(struct io_kiocb *req)
534{
535 int ret;
536
537 ret = hrtimer_try_to_cancel(&req->timeout.timer);
538 if (ret != -1) {
539 atomic_inc(&req->ctx->cq_timeouts);
Jens Axboe842f9612019-10-29 12:34:10 -0600540 list_del_init(&req->list);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700541 io_cqring_fill_event(req, 0);
542 io_put_req(req, NULL);
Jens Axboe5262f562019-09-17 12:26:57 -0600543 }
544}
545
546static void io_kill_timeouts(struct io_ring_ctx *ctx)
547{
548 struct io_kiocb *req, *tmp;
549
550 spin_lock_irq(&ctx->completion_lock);
551 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
552 io_kill_timeout(req);
553 spin_unlock_irq(&ctx->completion_lock);
554}
555
Jens Axboede0617e2019-04-06 21:51:27 -0600556static void io_commit_cqring(struct io_ring_ctx *ctx)
557{
558 struct io_kiocb *req;
559
Jens Axboe5262f562019-09-17 12:26:57 -0600560 while ((req = io_get_timeout_req(ctx)) != NULL)
561 io_kill_timeout(req);
562
Jens Axboede0617e2019-04-06 21:51:27 -0600563 __io_commit_cqring(ctx);
564
565 while ((req = io_get_deferred_req(ctx)) != NULL) {
Jackie Liu4fe2c962019-09-09 20:50:40 +0800566 if (req->flags & REQ_F_SHADOW_DRAIN) {
567 /* Just for drain, free it. */
568 __io_free_req(req);
569 continue;
570 }
Jens Axboede0617e2019-04-06 21:51:27 -0600571 req->flags |= REQ_F_IO_DRAINED;
Jens Axboe18d9be12019-09-10 09:13:05 -0600572 io_queue_async_work(ctx, req);
Jens Axboede0617e2019-04-06 21:51:27 -0600573 }
574}
575
Jens Axboe2b188cc2019-01-07 10:46:33 -0700576static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
577{
Hristo Venev75b28af2019-08-26 17:23:46 +0000578 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700579 unsigned tail;
580
581 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +0200582 /*
583 * writes to the cq entry need to come after reading head; the
584 * control dependency is enough as we're using WRITE_ONCE to
585 * fill the cq entry
586 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000587 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700588 return NULL;
589
590 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +0000591 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -0700592}
593
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700594static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
595{
596 if (waitqueue_active(&ctx->wait))
597 wake_up(&ctx->wait);
598 if (waitqueue_active(&ctx->sqo_wait))
599 wake_up(&ctx->sqo_wait);
600 if (ctx->cq_ev_fd)
601 eventfd_signal(ctx->cq_ev_fd, 1);
602}
603
604static void io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
605{
606 struct io_rings *rings = ctx->rings;
607 struct io_uring_cqe *cqe;
608 struct io_kiocb *req;
609 unsigned long flags;
610 LIST_HEAD(list);
611
612 if (!force) {
613 if (list_empty_careful(&ctx->cq_overflow_list))
614 return;
615 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
616 rings->cq_ring_entries))
617 return;
618 }
619
620 spin_lock_irqsave(&ctx->completion_lock, flags);
621
622 /* if force is set, the ring is going away. always drop after that */
623 if (force)
624 ctx->cq_overflow_flushed = true;
625
626 while (!list_empty(&ctx->cq_overflow_list)) {
627 cqe = io_get_cqring(ctx);
628 if (!cqe && !force)
629 break;
630
631 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
632 list);
633 list_move(&req->list, &list);
634 if (cqe) {
635 WRITE_ONCE(cqe->user_data, req->user_data);
636 WRITE_ONCE(cqe->res, req->result);
637 WRITE_ONCE(cqe->flags, 0);
638 } else {
639 WRITE_ONCE(ctx->rings->cq_overflow,
640 atomic_inc_return(&ctx->cached_cq_overflow));
641 }
642 }
643
644 io_commit_cqring(ctx);
645 spin_unlock_irqrestore(&ctx->completion_lock, flags);
646 io_cqring_ev_posted(ctx);
647
648 while (!list_empty(&list)) {
649 req = list_first_entry(&list, struct io_kiocb, list);
650 list_del(&req->list);
651 io_put_req(req, NULL);
652 }
653}
654
Jens Axboe78e19bb2019-11-06 15:21:34 -0700655static void io_cqring_fill_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700656{
Jens Axboe78e19bb2019-11-06 15:21:34 -0700657 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700658 struct io_uring_cqe *cqe;
659
Jens Axboe78e19bb2019-11-06 15:21:34 -0700660 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -0700661
Jens Axboe2b188cc2019-01-07 10:46:33 -0700662 /*
663 * If we can't get a cq entry, userspace overflowed the
664 * submission (by quite a lot). Increment the overflow count in
665 * the ring.
666 */
667 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700668 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -0700669 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700670 WRITE_ONCE(cqe->res, res);
Jens Axboec71ffb62019-05-13 20:58:29 -0600671 WRITE_ONCE(cqe->flags, 0);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700672 } else if (ctx->cq_overflow_flushed) {
Jens Axboe498ccd92019-10-25 10:04:25 -0600673 WRITE_ONCE(ctx->rings->cq_overflow,
674 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700675 } else {
676 refcount_inc(&req->refs);
677 req->result = res;
678 list_add_tail(&req->list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700679 }
680}
681
Jens Axboe78e19bb2019-11-06 15:21:34 -0700682static void io_cqring_add_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700683{
Jens Axboe78e19bb2019-11-06 15:21:34 -0700684 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700685 unsigned long flags;
686
687 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700688 io_cqring_fill_event(req, res);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700689 io_commit_cqring(ctx);
690 spin_unlock_irqrestore(&ctx->completion_lock, flags);
691
Jens Axboe8c838782019-03-12 15:48:16 -0600692 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700693}
694
Jens Axboe2579f912019-01-09 09:10:43 -0700695static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
696 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700697{
Jens Axboefd6fab22019-03-14 16:30:06 -0600698 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700699 struct io_kiocb *req;
700
701 if (!percpu_ref_tryget(&ctx->refs))
702 return NULL;
703
Jens Axboe2579f912019-01-09 09:10:43 -0700704 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -0600705 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -0700706 if (unlikely(!req))
707 goto out;
708 } else if (!state->free_reqs) {
709 size_t sz;
710 int ret;
711
712 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -0600713 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
714
715 /*
716 * Bulk alloc is all-or-nothing. If we fail to get a batch,
717 * retry single alloc to be on the safe side.
718 */
719 if (unlikely(ret <= 0)) {
720 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
721 if (!state->reqs[0])
722 goto out;
723 ret = 1;
724 }
Jens Axboe2579f912019-01-09 09:10:43 -0700725 state->free_reqs = ret - 1;
726 state->cur_req = 1;
727 req = state->reqs[0];
728 } else {
729 req = state->reqs[state->cur_req];
730 state->free_reqs--;
731 state->cur_req++;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700732 }
733
Jens Axboe60c112b2019-06-21 10:20:18 -0600734 req->file = NULL;
Jens Axboe2579f912019-01-09 09:10:43 -0700735 req->ctx = ctx;
736 req->flags = 0;
Jens Axboee65ef562019-03-12 10:16:44 -0600737 /* one is dropped after submission, the other at completion */
738 refcount_set(&req->refs, 2);
Jens Axboe9e645e112019-05-10 16:07:28 -0600739 req->result = 0;
Jens Axboe561fb042019-10-24 07:25:42 -0600740 INIT_IO_WORK(&req->work, io_wq_submit_work);
Jens Axboe2579f912019-01-09 09:10:43 -0700741 return req;
742out:
Pavel Begunkov6805b322019-10-08 02:18:42 +0300743 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700744 return NULL;
745}
746
Jens Axboedef596e2019-01-09 08:59:42 -0700747static void io_free_req_many(struct io_ring_ctx *ctx, void **reqs, int *nr)
748{
749 if (*nr) {
750 kmem_cache_free_bulk(req_cachep, *nr, reqs);
Pavel Begunkov6805b322019-10-08 02:18:42 +0300751 percpu_ref_put_many(&ctx->refs, *nr);
Jens Axboedef596e2019-01-09 08:59:42 -0700752 *nr = 0;
753 }
754}
755
Jens Axboe9e645e112019-05-10 16:07:28 -0600756static void __io_free_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700757{
Jens Axboefcb323c2019-10-24 12:39:47 -0600758 struct io_ring_ctx *ctx = req->ctx;
759
Jens Axboe09bb8392019-03-13 12:39:28 -0600760 if (req->file && !(req->flags & REQ_F_FIXED_FILE))
761 fput(req->file);
Jens Axboefcb323c2019-10-24 12:39:47 -0600762 if (req->flags & REQ_F_INFLIGHT) {
763 unsigned long flags;
764
765 spin_lock_irqsave(&ctx->inflight_lock, flags);
766 list_del(&req->inflight_entry);
767 if (waitqueue_active(&ctx->inflight_wait))
768 wake_up(&ctx->inflight_wait);
769 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
770 }
771 percpu_ref_put(&ctx->refs);
Jens Axboee65ef562019-03-12 10:16:44 -0600772 kmem_cache_free(req_cachep, req);
773}
774
Jens Axboe2665abf2019-11-05 12:40:47 -0700775static bool io_link_cancel_timeout(struct io_ring_ctx *ctx,
776 struct io_kiocb *req)
777{
778 int ret;
779
780 ret = hrtimer_try_to_cancel(&req->timeout.timer);
781 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -0700782 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -0700783 io_commit_cqring(ctx);
784 req->flags &= ~REQ_F_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -0700785 io_put_req(req, NULL);
Jens Axboe2665abf2019-11-05 12:40:47 -0700786 return true;
787 }
788
789 return false;
790}
791
Jens Axboeba816ad2019-09-28 11:36:45 -0600792static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -0600793{
Jens Axboe2665abf2019-11-05 12:40:47 -0700794 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -0600795 struct io_kiocb *nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -0700796 bool wake_ev = false;
Jens Axboe9e645e112019-05-10 16:07:28 -0600797
798 /*
799 * The list should never be empty when we are called here. But could
800 * potentially happen if the chain is messed up, check to be on the
801 * safe side.
802 */
803 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, list);
Jens Axboe2665abf2019-11-05 12:40:47 -0700804 while (nxt) {
Jens Axboe9e645e112019-05-10 16:07:28 -0600805 list_del(&nxt->list);
806 if (!list_empty(&req->link_list)) {
807 INIT_LIST_HEAD(&nxt->link_list);
808 list_splice(&req->link_list, &nxt->link_list);
809 nxt->flags |= REQ_F_LINK;
810 }
811
Jens Axboeba816ad2019-09-28 11:36:45 -0600812 /*
813 * If we're in async work, we can continue processing the chain
814 * in this context instead of having to queue up new async work.
815 */
Jens Axboe2665abf2019-11-05 12:40:47 -0700816 if (req->flags & REQ_F_LINK_TIMEOUT) {
817 wake_ev = io_link_cancel_timeout(ctx, nxt);
818
819 /* we dropped this link, get next */
820 nxt = list_first_entry_or_null(&req->link_list,
821 struct io_kiocb, list);
822 } else if (nxtptr && current_work()) {
Jens Axboeba816ad2019-09-28 11:36:45 -0600823 *nxtptr = nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -0700824 break;
825 } else {
Jens Axboeba816ad2019-09-28 11:36:45 -0600826 io_queue_async_work(req->ctx, nxt);
Jens Axboe2665abf2019-11-05 12:40:47 -0700827 break;
828 }
Jens Axboe9e645e112019-05-10 16:07:28 -0600829 }
Jens Axboe2665abf2019-11-05 12:40:47 -0700830
831 if (wake_ev)
832 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -0600833}
834
835/*
836 * Called if REQ_F_LINK is set, and we fail the head request
837 */
838static void io_fail_links(struct io_kiocb *req)
839{
Jens Axboe2665abf2019-11-05 12:40:47 -0700840 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -0600841 struct io_kiocb *link;
Jens Axboe2665abf2019-11-05 12:40:47 -0700842 unsigned long flags;
843
844 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -0600845
846 while (!list_empty(&req->link_list)) {
847 link = list_first_entry(&req->link_list, struct io_kiocb, list);
Jens Axboe2665abf2019-11-05 12:40:47 -0700848 list_del_init(&link->list);
Jens Axboe9e645e112019-05-10 16:07:28 -0600849
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +0200850 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -0700851
852 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
853 link->submit.sqe->opcode == IORING_OP_LINK_TIMEOUT) {
854 io_link_cancel_timeout(ctx, link);
855 } else {
Jens Axboe78e19bb2019-11-06 15:21:34 -0700856 io_cqring_fill_event(link, -ECANCELED);
857 io_double_put_req(link);
Jens Axboe2665abf2019-11-05 12:40:47 -0700858 }
Jens Axboe9e645e112019-05-10 16:07:28 -0600859 }
Jens Axboe2665abf2019-11-05 12:40:47 -0700860
861 io_commit_cqring(ctx);
862 spin_unlock_irqrestore(&ctx->completion_lock, flags);
863 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -0600864}
865
Jens Axboeba816ad2019-09-28 11:36:45 -0600866static void io_free_req(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -0600867{
Jens Axboe2665abf2019-11-05 12:40:47 -0700868 if (likely(!(req->flags & REQ_F_LINK))) {
869 __io_free_req(req);
870 return;
871 }
872
Jens Axboe9e645e112019-05-10 16:07:28 -0600873 /*
874 * If LINK is set, we have dependent requests in this chain. If we
875 * didn't fail this request, queue the first one up, moving any other
876 * dependencies to the next request. In case of failure, fail the rest
877 * of the chain.
878 */
Jens Axboe2665abf2019-11-05 12:40:47 -0700879 if (req->flags & REQ_F_FAIL_LINK) {
880 io_fail_links(req);
881 } else if (req->flags & REQ_F_LINK_TIMEOUT) {
882 struct io_ring_ctx *ctx = req->ctx;
883 unsigned long flags;
884
885 /*
886 * If this is a timeout link, we could be racing with the
887 * timeout timer. Grab the completion lock for this case to
888 * protection against that.
889 */
890 spin_lock_irqsave(&ctx->completion_lock, flags);
891 io_req_link_next(req, nxt);
892 spin_unlock_irqrestore(&ctx->completion_lock, flags);
893 } else {
894 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -0600895 }
896
897 __io_free_req(req);
898}
899
Jens Axboeba816ad2019-09-28 11:36:45 -0600900/*
901 * Drop reference to request, return next in chain (if there is one) if this
902 * was the last reference to this request.
903 */
904static struct io_kiocb *io_put_req_find_next(struct io_kiocb *req)
Jens Axboee65ef562019-03-12 10:16:44 -0600905{
Jens Axboeba816ad2019-09-28 11:36:45 -0600906 struct io_kiocb *nxt = NULL;
907
Jens Axboee65ef562019-03-12 10:16:44 -0600908 if (refcount_dec_and_test(&req->refs))
Jens Axboeba816ad2019-09-28 11:36:45 -0600909 io_free_req(req, &nxt);
910
911 return nxt;
912}
913
914static void io_put_req(struct io_kiocb *req, struct io_kiocb **nxtptr)
915{
916 struct io_kiocb *nxt;
917
918 nxt = io_put_req_find_next(req);
919 if (nxt) {
Jens Axboe561fb042019-10-24 07:25:42 -0600920 if (nxtptr)
Jens Axboeba816ad2019-09-28 11:36:45 -0600921 *nxtptr = nxt;
Jens Axboe561fb042019-10-24 07:25:42 -0600922 else
Jens Axboeba816ad2019-09-28 11:36:45 -0600923 io_queue_async_work(nxt->ctx, nxt);
Jens Axboeba816ad2019-09-28 11:36:45 -0600924 }
Jens Axboe2b188cc2019-01-07 10:46:33 -0700925}
926
Jens Axboe78e19bb2019-11-06 15:21:34 -0700927static void io_double_put_req(struct io_kiocb *req)
928{
929 /* drop both submit and complete references */
930 if (refcount_sub_and_test(2, &req->refs))
931 __io_free_req(req);
932}
933
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700934static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -0600935{
Jens Axboe84f97dc2019-11-06 11:27:53 -0700936 struct io_rings *rings = ctx->rings;
937
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700938 /*
939 * noflush == true is from the waitqueue handler, just ensure we wake
940 * up the task, and the next invocation will flush the entries. We
941 * cannot safely to it from here.
942 */
943 if (noflush && !list_empty(&ctx->cq_overflow_list))
944 return -1U;
945
946 io_cqring_overflow_flush(ctx, false);
947
Jens Axboea3a0e432019-08-20 11:03:11 -0600948 /* See comment at the top of this file */
949 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +0000950 return READ_ONCE(rings->cq.tail) - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -0600951}
952
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +0300953static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
954{
955 struct io_rings *rings = ctx->rings;
956
957 /* make sure SQ entry isn't read before tail */
958 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
959}
960
Jens Axboedef596e2019-01-09 08:59:42 -0700961/*
962 * Find and free completed poll iocbs
963 */
964static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
965 struct list_head *done)
966{
967 void *reqs[IO_IOPOLL_BATCH];
968 struct io_kiocb *req;
Jens Axboe09bb8392019-03-13 12:39:28 -0600969 int to_free;
Jens Axboedef596e2019-01-09 08:59:42 -0700970
Jens Axboe09bb8392019-03-13 12:39:28 -0600971 to_free = 0;
Jens Axboedef596e2019-01-09 08:59:42 -0700972 while (!list_empty(done)) {
973 req = list_first_entry(done, struct io_kiocb, list);
974 list_del(&req->list);
975
Jens Axboe78e19bb2019-11-06 15:21:34 -0700976 io_cqring_fill_event(req, req->result);
Jens Axboedef596e2019-01-09 08:59:42 -0700977 (*nr_events)++;
978
Jens Axboe09bb8392019-03-13 12:39:28 -0600979 if (refcount_dec_and_test(&req->refs)) {
980 /* If we're not using fixed files, we have to pair the
981 * completion part with the file put. Use regular
982 * completions for those, only batch free for fixed
Jens Axboe9e645e112019-05-10 16:07:28 -0600983 * file and non-linked commands.
Jens Axboe09bb8392019-03-13 12:39:28 -0600984 */
Jens Axboe9e645e112019-05-10 16:07:28 -0600985 if ((req->flags & (REQ_F_FIXED_FILE|REQ_F_LINK)) ==
986 REQ_F_FIXED_FILE) {
Jens Axboe09bb8392019-03-13 12:39:28 -0600987 reqs[to_free++] = req;
988 if (to_free == ARRAY_SIZE(reqs))
989 io_free_req_many(ctx, reqs, &to_free);
Jens Axboe6b063142019-01-10 22:13:58 -0700990 } else {
Jens Axboeba816ad2019-09-28 11:36:45 -0600991 io_free_req(req, NULL);
Jens Axboe6b063142019-01-10 22:13:58 -0700992 }
Jens Axboe9a56a232019-01-09 09:06:50 -0700993 }
Jens Axboedef596e2019-01-09 08:59:42 -0700994 }
Jens Axboedef596e2019-01-09 08:59:42 -0700995
Jens Axboe09bb8392019-03-13 12:39:28 -0600996 io_commit_cqring(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -0700997 io_free_req_many(ctx, reqs, &to_free);
998}
999
1000static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1001 long min)
1002{
1003 struct io_kiocb *req, *tmp;
1004 LIST_HEAD(done);
1005 bool spin;
1006 int ret;
1007
1008 /*
1009 * Only spin for completions if we don't have multiple devices hanging
1010 * off our complete list, and we're under the requested amount.
1011 */
1012 spin = !ctx->poll_multi_file && *nr_events < min;
1013
1014 ret = 0;
1015 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
1016 struct kiocb *kiocb = &req->rw;
1017
1018 /*
1019 * Move completed entries to our local list. If we find a
1020 * request that requires polling, break out and complete
1021 * the done list first, if we have entries there.
1022 */
1023 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
1024 list_move_tail(&req->list, &done);
1025 continue;
1026 }
1027 if (!list_empty(&done))
1028 break;
1029
1030 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1031 if (ret < 0)
1032 break;
1033
1034 if (ret && spin)
1035 spin = false;
1036 ret = 0;
1037 }
1038
1039 if (!list_empty(&done))
1040 io_iopoll_complete(ctx, nr_events, &done);
1041
1042 return ret;
1043}
1044
1045/*
1046 * Poll for a mininum of 'min' events. Note that if min == 0 we consider that a
1047 * non-spinning poll check - we'll still enter the driver poll loop, but only
1048 * as a non-spinning completion check.
1049 */
1050static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
1051 long min)
1052{
Jens Axboe08f54392019-08-21 22:19:11 -06001053 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07001054 int ret;
1055
1056 ret = io_do_iopoll(ctx, nr_events, min);
1057 if (ret < 0)
1058 return ret;
1059 if (!min || *nr_events >= min)
1060 return 0;
1061 }
1062
1063 return 1;
1064}
1065
1066/*
1067 * We can't just wait for polled events to come to us, we have to actively
1068 * find and complete them.
1069 */
1070static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
1071{
1072 if (!(ctx->flags & IORING_SETUP_IOPOLL))
1073 return;
1074
1075 mutex_lock(&ctx->uring_lock);
1076 while (!list_empty(&ctx->poll_list)) {
1077 unsigned int nr_events = 0;
1078
1079 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -06001080
1081 /*
1082 * Ensure we allow local-to-the-cpu processing to take place,
1083 * in this case we need to ensure that we reap all events.
1084 */
1085 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -07001086 }
1087 mutex_unlock(&ctx->uring_lock);
1088}
1089
Jens Axboe2b2ed972019-10-25 10:06:15 -06001090static int __io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1091 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07001092{
Jens Axboe2b2ed972019-10-25 10:06:15 -06001093 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001094
1095 do {
1096 int tmin = 0;
1097
Jens Axboe500f9fb2019-08-19 12:15:59 -06001098 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06001099 * Don't enter poll loop if we already have events pending.
1100 * If we do, we can potentially be spinning for commands that
1101 * already triggered a CQE (eg in error).
1102 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001103 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06001104 break;
1105
1106 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06001107 * If a submit got punted to a workqueue, we can have the
1108 * application entering polling for a command before it gets
1109 * issued. That app will hold the uring_lock for the duration
1110 * of the poll right here, so we need to take a breather every
1111 * now and then to ensure that the issue has a chance to add
1112 * the poll to the issued list. Otherwise we can spin here
1113 * forever, while the workqueue is stuck trying to acquire the
1114 * very same mutex.
1115 */
1116 if (!(++iters & 7)) {
1117 mutex_unlock(&ctx->uring_lock);
1118 mutex_lock(&ctx->uring_lock);
1119 }
1120
Jens Axboedef596e2019-01-09 08:59:42 -07001121 if (*nr_events < min)
1122 tmin = min - *nr_events;
1123
1124 ret = io_iopoll_getevents(ctx, nr_events, tmin);
1125 if (ret <= 0)
1126 break;
1127 ret = 0;
1128 } while (min && !*nr_events && !need_resched());
1129
Jens Axboe2b2ed972019-10-25 10:06:15 -06001130 return ret;
1131}
1132
1133static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1134 long min)
1135{
1136 int ret;
1137
1138 /*
1139 * We disallow the app entering submit/complete with polling, but we
1140 * still need to lock the ring to prevent racing with polled issue
1141 * that got punted to a workqueue.
1142 */
1143 mutex_lock(&ctx->uring_lock);
1144 ret = __io_iopoll_check(ctx, nr_events, min);
Jens Axboe500f9fb2019-08-19 12:15:59 -06001145 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001146 return ret;
1147}
1148
Jens Axboe491381ce2019-10-17 09:20:46 -06001149static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001150{
Jens Axboe491381ce2019-10-17 09:20:46 -06001151 /*
1152 * Tell lockdep we inherited freeze protection from submission
1153 * thread.
1154 */
1155 if (req->flags & REQ_F_ISREG) {
1156 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001157
Jens Axboe491381ce2019-10-17 09:20:46 -06001158 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001159 }
Jens Axboe491381ce2019-10-17 09:20:46 -06001160 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001161}
1162
Jens Axboeba816ad2019-09-28 11:36:45 -06001163static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001164{
1165 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1166
Jens Axboe491381ce2019-10-17 09:20:46 -06001167 if (kiocb->ki_flags & IOCB_WRITE)
1168 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001169
Jens Axboe9e645e112019-05-10 16:07:28 -06001170 if ((req->flags & REQ_F_LINK) && res != req->result)
1171 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001172 io_cqring_add_event(req, res);
Jens Axboeba816ad2019-09-28 11:36:45 -06001173}
1174
1175static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1176{
1177 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1178
1179 io_complete_rw_common(kiocb, res);
1180 io_put_req(req, NULL);
1181}
1182
1183static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res)
1184{
1185 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1186
1187 io_complete_rw_common(kiocb, res);
1188 return io_put_req_find_next(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001189}
1190
Jens Axboedef596e2019-01-09 08:59:42 -07001191static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1192{
1193 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1194
Jens Axboe491381ce2019-10-17 09:20:46 -06001195 if (kiocb->ki_flags & IOCB_WRITE)
1196 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001197
Jens Axboe9e645e112019-05-10 16:07:28 -06001198 if ((req->flags & REQ_F_LINK) && res != req->result)
1199 req->flags |= REQ_F_FAIL_LINK;
1200 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -07001201 if (res != -EAGAIN)
1202 req->flags |= REQ_F_IOPOLL_COMPLETED;
1203}
1204
1205/*
1206 * After the iocb has been issued, it's safe to be found on the poll list.
1207 * Adding the kiocb to the list AFTER submission ensures that we don't
1208 * find it from a io_iopoll_getevents() thread before the issuer is done
1209 * accessing the kiocb cookie.
1210 */
1211static void io_iopoll_req_issued(struct io_kiocb *req)
1212{
1213 struct io_ring_ctx *ctx = req->ctx;
1214
1215 /*
1216 * Track whether we have multiple files in our lists. This will impact
1217 * how we do polling eventually, not spinning if we're on potentially
1218 * different devices.
1219 */
1220 if (list_empty(&ctx->poll_list)) {
1221 ctx->poll_multi_file = false;
1222 } else if (!ctx->poll_multi_file) {
1223 struct io_kiocb *list_req;
1224
1225 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1226 list);
1227 if (list_req->rw.ki_filp != req->rw.ki_filp)
1228 ctx->poll_multi_file = true;
1229 }
1230
1231 /*
1232 * For fast devices, IO may have already completed. If it has, add
1233 * it to the front so we find it first.
1234 */
1235 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1236 list_add(&req->list, &ctx->poll_list);
1237 else
1238 list_add_tail(&req->list, &ctx->poll_list);
1239}
1240
Jens Axboe3d6770f2019-04-13 11:50:54 -06001241static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07001242{
Jens Axboe3d6770f2019-04-13 11:50:54 -06001243 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -07001244 int diff = state->has_refs - state->used_refs;
1245
1246 if (diff)
1247 fput_many(state->file, diff);
1248 state->file = NULL;
1249 }
1250}
1251
1252/*
1253 * Get as many references to a file as we have IOs left in this submission,
1254 * assuming most submissions are for one file, or at least that each file
1255 * has more than one submission.
1256 */
1257static struct file *io_file_get(struct io_submit_state *state, int fd)
1258{
1259 if (!state)
1260 return fget(fd);
1261
1262 if (state->file) {
1263 if (state->fd == fd) {
1264 state->used_refs++;
1265 state->ios_left--;
1266 return state->file;
1267 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06001268 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07001269 }
1270 state->file = fget_many(fd, state->ios_left);
1271 if (!state->file)
1272 return NULL;
1273
1274 state->fd = fd;
1275 state->has_refs = state->ios_left;
1276 state->used_refs = 1;
1277 state->ios_left--;
1278 return state->file;
1279}
1280
Jens Axboe2b188cc2019-01-07 10:46:33 -07001281/*
1282 * If we tracked the file through the SCM inflight mechanism, we could support
1283 * any file. For now, just ensure that anything potentially problematic is done
1284 * inline.
1285 */
1286static bool io_file_supports_async(struct file *file)
1287{
1288 umode_t mode = file_inode(file)->i_mode;
1289
1290 if (S_ISBLK(mode) || S_ISCHR(mode))
1291 return true;
1292 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
1293 return true;
1294
1295 return false;
1296}
1297
Pavel Begunkov267bc902019-11-07 01:41:08 +03001298static int io_prep_rw(struct io_kiocb *req, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001299{
Pavel Begunkov267bc902019-11-07 01:41:08 +03001300 const struct io_uring_sqe *sqe = req->submit.sqe;
Jens Axboedef596e2019-01-09 08:59:42 -07001301 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001302 struct kiocb *kiocb = &req->rw;
Jens Axboe09bb8392019-03-13 12:39:28 -06001303 unsigned ioprio;
1304 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001305
Jens Axboe09bb8392019-03-13 12:39:28 -06001306 if (!req->file)
1307 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001308
Jens Axboe491381ce2019-10-17 09:20:46 -06001309 if (S_ISREG(file_inode(req->file)->i_mode))
1310 req->flags |= REQ_F_ISREG;
1311
1312 /*
1313 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
1314 * we know to async punt it even if it was opened O_NONBLOCK
1315 */
1316 if (force_nonblock && !io_file_supports_async(req->file)) {
1317 req->flags |= REQ_F_MUST_PUNT;
1318 return -EAGAIN;
1319 }
Jens Axboe6b063142019-01-10 22:13:58 -07001320
Jens Axboe2b188cc2019-01-07 10:46:33 -07001321 kiocb->ki_pos = READ_ONCE(sqe->off);
1322 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
1323 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
1324
1325 ioprio = READ_ONCE(sqe->ioprio);
1326 if (ioprio) {
1327 ret = ioprio_check_cap(ioprio);
1328 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06001329 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001330
1331 kiocb->ki_ioprio = ioprio;
1332 } else
1333 kiocb->ki_ioprio = get_current_ioprio();
1334
1335 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
1336 if (unlikely(ret))
Jens Axboe09bb8392019-03-13 12:39:28 -06001337 return ret;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001338
1339 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06001340 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
1341 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02001342 req->flags |= REQ_F_NOWAIT;
1343
1344 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001345 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001346
Jens Axboedef596e2019-01-09 08:59:42 -07001347 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07001348 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
1349 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06001350 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001351
Jens Axboedef596e2019-01-09 08:59:42 -07001352 kiocb->ki_flags |= IOCB_HIPRI;
1353 kiocb->ki_complete = io_complete_rw_iopoll;
1354 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001355 if (kiocb->ki_flags & IOCB_HIPRI)
1356 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07001357 kiocb->ki_complete = io_complete_rw;
1358 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001359 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001360}
1361
1362static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
1363{
1364 switch (ret) {
1365 case -EIOCBQUEUED:
1366 break;
1367 case -ERESTARTSYS:
1368 case -ERESTARTNOINTR:
1369 case -ERESTARTNOHAND:
1370 case -ERESTART_RESTARTBLOCK:
1371 /*
1372 * We can't just restart the syscall, since previously
1373 * submitted sqes may already be in progress. Just fail this
1374 * IO with EINTR.
1375 */
1376 ret = -EINTR;
1377 /* fall through */
1378 default:
1379 kiocb->ki_complete(kiocb, ret, 0);
1380 }
1381}
1382
Jens Axboeba816ad2019-09-28 11:36:45 -06001383static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt,
1384 bool in_async)
1385{
1386 if (in_async && ret >= 0 && nxt && kiocb->ki_complete == io_complete_rw)
1387 *nxt = __io_complete_rw(kiocb, ret);
1388 else
1389 io_rw_done(kiocb, ret);
1390}
1391
Jens Axboeedafcce2019-01-09 09:16:05 -07001392static int io_import_fixed(struct io_ring_ctx *ctx, int rw,
1393 const struct io_uring_sqe *sqe,
1394 struct iov_iter *iter)
1395{
1396 size_t len = READ_ONCE(sqe->len);
1397 struct io_mapped_ubuf *imu;
1398 unsigned index, buf_index;
1399 size_t offset;
1400 u64 buf_addr;
1401
1402 /* attempt to use fixed buffers without having provided iovecs */
1403 if (unlikely(!ctx->user_bufs))
1404 return -EFAULT;
1405
1406 buf_index = READ_ONCE(sqe->buf_index);
1407 if (unlikely(buf_index >= ctx->nr_user_bufs))
1408 return -EFAULT;
1409
1410 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
1411 imu = &ctx->user_bufs[index];
1412 buf_addr = READ_ONCE(sqe->addr);
1413
1414 /* overflow */
1415 if (buf_addr + len < buf_addr)
1416 return -EFAULT;
1417 /* not inside the mapped region */
1418 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
1419 return -EFAULT;
1420
1421 /*
1422 * May not be a start of buffer, set size appropriately
1423 * and advance us to the beginning.
1424 */
1425 offset = buf_addr - imu->ubuf;
1426 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06001427
1428 if (offset) {
1429 /*
1430 * Don't use iov_iter_advance() here, as it's really slow for
1431 * using the latter parts of a big fixed buffer - it iterates
1432 * over each segment manually. We can cheat a bit here, because
1433 * we know that:
1434 *
1435 * 1) it's a BVEC iter, we set it up
1436 * 2) all bvecs are PAGE_SIZE in size, except potentially the
1437 * first and last bvec
1438 *
1439 * So just find our index, and adjust the iterator afterwards.
1440 * If the offset is within the first bvec (or the whole first
1441 * bvec, just use iov_iter_advance(). This makes it easier
1442 * since we can just skip the first segment, which may not
1443 * be PAGE_SIZE aligned.
1444 */
1445 const struct bio_vec *bvec = imu->bvec;
1446
1447 if (offset <= bvec->bv_len) {
1448 iov_iter_advance(iter, offset);
1449 } else {
1450 unsigned long seg_skip;
1451
1452 /* skip first vec */
1453 offset -= bvec->bv_len;
1454 seg_skip = 1 + (offset >> PAGE_SHIFT);
1455
1456 iter->bvec = bvec + seg_skip;
1457 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02001458 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001459 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001460 }
1461 }
1462
Jens Axboeedafcce2019-01-09 09:16:05 -07001463 return 0;
1464}
1465
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001466static ssize_t io_import_iovec(struct io_ring_ctx *ctx, int rw,
1467 const struct sqe_submit *s, struct iovec **iovec,
1468 struct iov_iter *iter)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001469{
1470 const struct io_uring_sqe *sqe = s->sqe;
1471 void __user *buf = u64_to_user_ptr(READ_ONCE(sqe->addr));
1472 size_t sqe_len = READ_ONCE(sqe->len);
Jens Axboeedafcce2019-01-09 09:16:05 -07001473 u8 opcode;
1474
1475 /*
1476 * We're reading ->opcode for the second time, but the first read
1477 * doesn't care whether it's _FIXED or not, so it doesn't matter
1478 * whether ->opcode changes concurrently. The first read does care
1479 * about whether it is a READ or a WRITE, so we don't trust this read
1480 * for that purpose and instead let the caller pass in the read/write
1481 * flag.
1482 */
1483 opcode = READ_ONCE(sqe->opcode);
1484 if (opcode == IORING_OP_READ_FIXED ||
1485 opcode == IORING_OP_WRITE_FIXED) {
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001486 ssize_t ret = io_import_fixed(ctx, rw, sqe, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07001487 *iovec = NULL;
1488 return ret;
1489 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001490
1491 if (!s->has_user)
1492 return -EFAULT;
1493
1494#ifdef CONFIG_COMPAT
1495 if (ctx->compat)
1496 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
1497 iovec, iter);
1498#endif
1499
1500 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
1501}
1502
Jens Axboe32960612019-09-23 11:05:34 -06001503/*
1504 * For files that don't have ->read_iter() and ->write_iter(), handle them
1505 * by looping over ->read() or ->write() manually.
1506 */
1507static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
1508 struct iov_iter *iter)
1509{
1510 ssize_t ret = 0;
1511
1512 /*
1513 * Don't support polled IO through this interface, and we can't
1514 * support non-blocking either. For the latter, this just causes
1515 * the kiocb to be handled from an async context.
1516 */
1517 if (kiocb->ki_flags & IOCB_HIPRI)
1518 return -EOPNOTSUPP;
1519 if (kiocb->ki_flags & IOCB_NOWAIT)
1520 return -EAGAIN;
1521
1522 while (iov_iter_count(iter)) {
1523 struct iovec iovec = iov_iter_iovec(iter);
1524 ssize_t nr;
1525
1526 if (rw == READ) {
1527 nr = file->f_op->read(file, iovec.iov_base,
1528 iovec.iov_len, &kiocb->ki_pos);
1529 } else {
1530 nr = file->f_op->write(file, iovec.iov_base,
1531 iovec.iov_len, &kiocb->ki_pos);
1532 }
1533
1534 if (nr < 0) {
1535 if (!ret)
1536 ret = nr;
1537 break;
1538 }
1539 ret += nr;
1540 if (nr != iovec.iov_len)
1541 break;
1542 iov_iter_advance(iter, nr);
1543 }
1544
1545 return ret;
1546}
1547
Pavel Begunkov267bc902019-11-07 01:41:08 +03001548static int io_read(struct io_kiocb *req, struct io_kiocb **nxt,
1549 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001550{
1551 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1552 struct kiocb *kiocb = &req->rw;
1553 struct iov_iter iter;
1554 struct file *file;
Jens Axboe31b51512019-01-18 22:56:34 -07001555 size_t iov_count;
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001556 ssize_t read_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001557
Pavel Begunkov267bc902019-11-07 01:41:08 +03001558 ret = io_prep_rw(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001559 if (ret)
1560 return ret;
1561 file = kiocb->ki_filp;
1562
Jens Axboe2b188cc2019-01-07 10:46:33 -07001563 if (unlikely(!(file->f_mode & FMODE_READ)))
Jens Axboe09bb8392019-03-13 12:39:28 -06001564 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001565
Pavel Begunkov267bc902019-11-07 01:41:08 +03001566 ret = io_import_iovec(req->ctx, READ, &req->submit, &iovec, &iter);
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001567 if (ret < 0)
Jens Axboe09bb8392019-03-13 12:39:28 -06001568 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001569
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001570 read_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06001571 if (req->flags & REQ_F_LINK)
1572 req->result = read_size;
1573
Jens Axboe31b51512019-01-18 22:56:34 -07001574 iov_count = iov_iter_count(&iter);
1575 ret = rw_verify_area(READ, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001576 if (!ret) {
1577 ssize_t ret2;
1578
Jens Axboe32960612019-09-23 11:05:34 -06001579 if (file->f_op->read_iter)
1580 ret2 = call_read_iter(file, kiocb, &iter);
1581 else
1582 ret2 = loop_rw_iter(READ, file, kiocb, &iter);
1583
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001584 /*
1585 * In case of a short read, punt to async. This can happen
1586 * if we have data partially cached. Alternatively we can
1587 * return the short read, in which case the application will
1588 * need to issue another SQE and wait for it. That SQE will
1589 * need async punt anyway, so it's more efficient to do it
1590 * here.
1591 */
Jens Axboe491381ce2019-10-17 09:20:46 -06001592 if (force_nonblock && !(req->flags & REQ_F_NOWAIT) &&
1593 (req->flags & REQ_F_ISREG) &&
1594 ret2 > 0 && ret2 < read_size)
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001595 ret2 = -EAGAIN;
1596 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboe561fb042019-10-24 07:25:42 -06001597 if (!force_nonblock || ret2 != -EAGAIN)
Pavel Begunkov267bc902019-11-07 01:41:08 +03001598 kiocb_done(kiocb, ret2, nxt, req->submit.in_async);
Jens Axboe561fb042019-10-24 07:25:42 -06001599 else
Jens Axboe2b188cc2019-01-07 10:46:33 -07001600 ret = -EAGAIN;
1601 }
1602 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001603 return ret;
1604}
1605
Pavel Begunkov267bc902019-11-07 01:41:08 +03001606static int io_write(struct io_kiocb *req, struct io_kiocb **nxt,
1607 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001608{
1609 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1610 struct kiocb *kiocb = &req->rw;
1611 struct iov_iter iter;
1612 struct file *file;
Jens Axboe31b51512019-01-18 22:56:34 -07001613 size_t iov_count;
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001614 ssize_t ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001615
Pavel Begunkov267bc902019-11-07 01:41:08 +03001616 ret = io_prep_rw(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001617 if (ret)
1618 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001619
Jens Axboe2b188cc2019-01-07 10:46:33 -07001620 file = kiocb->ki_filp;
1621 if (unlikely(!(file->f_mode & FMODE_WRITE)))
Jens Axboe09bb8392019-03-13 12:39:28 -06001622 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001623
Pavel Begunkov267bc902019-11-07 01:41:08 +03001624 ret = io_import_iovec(req->ctx, WRITE, &req->submit, &iovec, &iter);
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001625 if (ret < 0)
Jens Axboe09bb8392019-03-13 12:39:28 -06001626 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001627
Jens Axboe9e645e112019-05-10 16:07:28 -06001628 if (req->flags & REQ_F_LINK)
1629 req->result = ret;
1630
Jens Axboe31b51512019-01-18 22:56:34 -07001631 iov_count = iov_iter_count(&iter);
1632
1633 ret = -EAGAIN;
Jens Axboe561fb042019-10-24 07:25:42 -06001634 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT))
Jens Axboe31b51512019-01-18 22:56:34 -07001635 goto out_free;
Jens Axboe31b51512019-01-18 22:56:34 -07001636
1637 ret = rw_verify_area(WRITE, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001638 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01001639 ssize_t ret2;
1640
Jens Axboe2b188cc2019-01-07 10:46:33 -07001641 /*
1642 * Open-code file_start_write here to grab freeze protection,
1643 * which will be released by another thread in
1644 * io_complete_rw(). Fool lockdep by telling it the lock got
1645 * released so that it doesn't complain about the held lock when
1646 * we return to userspace.
1647 */
Jens Axboe491381ce2019-10-17 09:20:46 -06001648 if (req->flags & REQ_F_ISREG) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001649 __sb_start_write(file_inode(file)->i_sb,
1650 SB_FREEZE_WRITE, true);
1651 __sb_writers_release(file_inode(file)->i_sb,
1652 SB_FREEZE_WRITE);
1653 }
1654 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01001655
Jens Axboe32960612019-09-23 11:05:34 -06001656 if (file->f_op->write_iter)
1657 ret2 = call_write_iter(file, kiocb, &iter);
1658 else
1659 ret2 = loop_rw_iter(WRITE, file, kiocb, &iter);
Jens Axboe561fb042019-10-24 07:25:42 -06001660 if (!force_nonblock || ret2 != -EAGAIN)
Pavel Begunkov267bc902019-11-07 01:41:08 +03001661 kiocb_done(kiocb, ret2, nxt, req->submit.in_async);
Jens Axboe561fb042019-10-24 07:25:42 -06001662 else
Roman Penyaev9bf79332019-03-25 20:09:24 +01001663 ret = -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001664 }
Jens Axboe31b51512019-01-18 22:56:34 -07001665out_free:
Jens Axboe2b188cc2019-01-07 10:46:33 -07001666 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001667 return ret;
1668}
1669
1670/*
1671 * IORING_OP_NOP just posts a completion event, nothing else.
1672 */
Jens Axboe78e19bb2019-11-06 15:21:34 -07001673static int io_nop(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001674{
1675 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001676
Jens Axboedef596e2019-01-09 08:59:42 -07001677 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1678 return -EINVAL;
1679
Jens Axboe78e19bb2019-11-06 15:21:34 -07001680 io_cqring_add_event(req, 0);
Jens Axboeba816ad2019-09-28 11:36:45 -06001681 io_put_req(req, NULL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001682 return 0;
1683}
1684
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001685static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1686{
Jens Axboe6b063142019-01-10 22:13:58 -07001687 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001688
Jens Axboe09bb8392019-03-13 12:39:28 -06001689 if (!req->file)
1690 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001691
Jens Axboe6b063142019-01-10 22:13:58 -07001692 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07001693 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07001694 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001695 return -EINVAL;
1696
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001697 return 0;
1698}
1699
1700static int io_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001701 struct io_kiocb **nxt, bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001702{
1703 loff_t sqe_off = READ_ONCE(sqe->off);
1704 loff_t sqe_len = READ_ONCE(sqe->len);
1705 loff_t end = sqe_off + sqe_len;
1706 unsigned fsync_flags;
1707 int ret;
1708
1709 fsync_flags = READ_ONCE(sqe->fsync_flags);
1710 if (unlikely(fsync_flags & ~IORING_FSYNC_DATASYNC))
1711 return -EINVAL;
1712
1713 ret = io_prep_fsync(req, sqe);
1714 if (ret)
1715 return ret;
1716
1717 /* fsync always requires a blocking context */
1718 if (force_nonblock)
1719 return -EAGAIN;
1720
1721 ret = vfs_fsync_range(req->rw.ki_filp, sqe_off,
1722 end > 0 ? end : LLONG_MAX,
1723 fsync_flags & IORING_FSYNC_DATASYNC);
1724
Jens Axboe9e645e112019-05-10 16:07:28 -06001725 if (ret < 0 && (req->flags & REQ_F_LINK))
1726 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001727 io_cqring_add_event(req, ret);
Jens Axboeba816ad2019-09-28 11:36:45 -06001728 io_put_req(req, nxt);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001729 return 0;
1730}
1731
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001732static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1733{
1734 struct io_ring_ctx *ctx = req->ctx;
1735 int ret = 0;
1736
1737 if (!req->file)
1738 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001739
1740 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1741 return -EINVAL;
1742 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
1743 return -EINVAL;
1744
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001745 return ret;
1746}
1747
1748static int io_sync_file_range(struct io_kiocb *req,
1749 const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001750 struct io_kiocb **nxt,
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001751 bool force_nonblock)
1752{
1753 loff_t sqe_off;
1754 loff_t sqe_len;
1755 unsigned flags;
1756 int ret;
1757
1758 ret = io_prep_sfr(req, sqe);
1759 if (ret)
1760 return ret;
1761
1762 /* sync_file_range always requires a blocking context */
1763 if (force_nonblock)
1764 return -EAGAIN;
1765
1766 sqe_off = READ_ONCE(sqe->off);
1767 sqe_len = READ_ONCE(sqe->len);
1768 flags = READ_ONCE(sqe->sync_range_flags);
1769
1770 ret = sync_file_range(req->rw.ki_filp, sqe_off, sqe_len, flags);
1771
Jens Axboe9e645e112019-05-10 16:07:28 -06001772 if (ret < 0 && (req->flags & REQ_F_LINK))
1773 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001774 io_cqring_add_event(req, ret);
Jens Axboeba816ad2019-09-28 11:36:45 -06001775 io_put_req(req, nxt);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001776 return 0;
1777}
1778
Jens Axboe0fa03c62019-04-19 13:34:07 -06001779#if defined(CONFIG_NET)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001780static int io_send_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001781 struct io_kiocb **nxt, bool force_nonblock,
Jens Axboeaa1fa282019-04-19 13:38:09 -06001782 long (*fn)(struct socket *, struct user_msghdr __user *,
1783 unsigned int))
1784{
Jens Axboe0fa03c62019-04-19 13:34:07 -06001785 struct socket *sock;
1786 int ret;
1787
1788 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1789 return -EINVAL;
1790
1791 sock = sock_from_file(req->file, &ret);
1792 if (sock) {
1793 struct user_msghdr __user *msg;
1794 unsigned flags;
1795
1796 flags = READ_ONCE(sqe->msg_flags);
1797 if (flags & MSG_DONTWAIT)
1798 req->flags |= REQ_F_NOWAIT;
1799 else if (force_nonblock)
1800 flags |= MSG_DONTWAIT;
1801
1802 msg = (struct user_msghdr __user *) (unsigned long)
1803 READ_ONCE(sqe->addr);
1804
Jens Axboeaa1fa282019-04-19 13:38:09 -06001805 ret = fn(sock, msg, flags);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001806 if (force_nonblock && ret == -EAGAIN)
1807 return ret;
1808 }
1809
Jens Axboe78e19bb2019-11-06 15:21:34 -07001810 io_cqring_add_event(req, ret);
Jens Axboef1f40852019-11-05 20:33:16 -07001811 if (ret < 0 && (req->flags & REQ_F_LINK))
1812 req->flags |= REQ_F_FAIL_LINK;
Jens Axboeba816ad2019-09-28 11:36:45 -06001813 io_put_req(req, nxt);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001814 return 0;
Jens Axboeaa1fa282019-04-19 13:38:09 -06001815}
1816#endif
1817
1818static int io_sendmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001819 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001820{
1821#if defined(CONFIG_NET)
Jens Axboeba816ad2019-09-28 11:36:45 -06001822 return io_send_recvmsg(req, sqe, nxt, force_nonblock,
1823 __sys_sendmsg_sock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06001824#else
1825 return -EOPNOTSUPP;
1826#endif
1827}
1828
1829static int io_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001830 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001831{
1832#if defined(CONFIG_NET)
Jens Axboeba816ad2019-09-28 11:36:45 -06001833 return io_send_recvmsg(req, sqe, nxt, force_nonblock,
1834 __sys_recvmsg_sock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001835#else
1836 return -EOPNOTSUPP;
1837#endif
1838}
1839
Jens Axboe17f2fe32019-10-17 14:42:58 -06001840static int io_accept(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1841 struct io_kiocb **nxt, bool force_nonblock)
1842{
1843#if defined(CONFIG_NET)
1844 struct sockaddr __user *addr;
1845 int __user *addr_len;
1846 unsigned file_flags;
1847 int flags, ret;
1848
1849 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
1850 return -EINVAL;
1851 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
1852 return -EINVAL;
1853
1854 addr = (struct sockaddr __user *) (unsigned long) READ_ONCE(sqe->addr);
1855 addr_len = (int __user *) (unsigned long) READ_ONCE(sqe->addr2);
1856 flags = READ_ONCE(sqe->accept_flags);
1857 file_flags = force_nonblock ? O_NONBLOCK : 0;
1858
1859 ret = __sys_accept4_file(req->file, file_flags, addr, addr_len, flags);
1860 if (ret == -EAGAIN && force_nonblock) {
1861 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
1862 return -EAGAIN;
1863 }
1864 if (ret < 0 && (req->flags & REQ_F_LINK))
1865 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001866 io_cqring_add_event(req, ret);
Jens Axboe17f2fe32019-10-17 14:42:58 -06001867 io_put_req(req, nxt);
1868 return 0;
1869#else
1870 return -EOPNOTSUPP;
1871#endif
1872}
1873
Jens Axboe221c5eb2019-01-17 09:41:58 -07001874static void io_poll_remove_one(struct io_kiocb *req)
1875{
1876 struct io_poll_iocb *poll = &req->poll;
1877
1878 spin_lock(&poll->head->lock);
1879 WRITE_ONCE(poll->canceled, true);
1880 if (!list_empty(&poll->wait.entry)) {
1881 list_del_init(&poll->wait.entry);
Jens Axboe18d9be12019-09-10 09:13:05 -06001882 io_queue_async_work(req->ctx, req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001883 }
1884 spin_unlock(&poll->head->lock);
1885
1886 list_del_init(&req->list);
1887}
1888
1889static void io_poll_remove_all(struct io_ring_ctx *ctx)
1890{
1891 struct io_kiocb *req;
1892
1893 spin_lock_irq(&ctx->completion_lock);
1894 while (!list_empty(&ctx->cancel_list)) {
1895 req = list_first_entry(&ctx->cancel_list, struct io_kiocb,list);
1896 io_poll_remove_one(req);
1897 }
1898 spin_unlock_irq(&ctx->completion_lock);
1899}
1900
1901/*
1902 * Find a running poll command that matches one specified in sqe->addr,
1903 * and remove it if found.
1904 */
1905static int io_poll_remove(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1906{
1907 struct io_ring_ctx *ctx = req->ctx;
1908 struct io_kiocb *poll_req, *next;
1909 int ret = -ENOENT;
1910
1911 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1912 return -EINVAL;
1913 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
1914 sqe->poll_events)
1915 return -EINVAL;
1916
1917 spin_lock_irq(&ctx->completion_lock);
1918 list_for_each_entry_safe(poll_req, next, &ctx->cancel_list, list) {
1919 if (READ_ONCE(sqe->addr) == poll_req->user_data) {
1920 io_poll_remove_one(poll_req);
1921 ret = 0;
1922 break;
1923 }
1924 }
1925 spin_unlock_irq(&ctx->completion_lock);
1926
Jens Axboe78e19bb2019-11-06 15:21:34 -07001927 io_cqring_add_event(req, ret);
Jens Axboef1f40852019-11-05 20:33:16 -07001928 if (ret < 0 && (req->flags & REQ_F_LINK))
1929 req->flags |= REQ_F_FAIL_LINK;
Jens Axboeba816ad2019-09-28 11:36:45 -06001930 io_put_req(req, NULL);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001931 return 0;
1932}
1933
Jens Axboe8c838782019-03-12 15:48:16 -06001934static void io_poll_complete(struct io_ring_ctx *ctx, struct io_kiocb *req,
1935 __poll_t mask)
Jens Axboe221c5eb2019-01-17 09:41:58 -07001936{
Jens Axboe8c838782019-03-12 15:48:16 -06001937 req->poll.done = true;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001938 io_cqring_fill_event(req, mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06001939 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001940}
1941
Jens Axboe561fb042019-10-24 07:25:42 -06001942static void io_poll_complete_work(struct io_wq_work **workptr)
Jens Axboe221c5eb2019-01-17 09:41:58 -07001943{
Jens Axboe561fb042019-10-24 07:25:42 -06001944 struct io_wq_work *work = *workptr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001945 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
1946 struct io_poll_iocb *poll = &req->poll;
1947 struct poll_table_struct pt = { ._key = poll->events };
1948 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe89723d02019-11-05 15:32:58 -07001949 struct io_kiocb *nxt = NULL;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001950 __poll_t mask = 0;
1951
Jens Axboe561fb042019-10-24 07:25:42 -06001952 if (work->flags & IO_WQ_WORK_CANCEL)
1953 WRITE_ONCE(poll->canceled, true);
1954
Jens Axboe221c5eb2019-01-17 09:41:58 -07001955 if (!READ_ONCE(poll->canceled))
1956 mask = vfs_poll(poll->file, &pt) & poll->events;
1957
1958 /*
1959 * Note that ->ki_cancel callers also delete iocb from active_reqs after
1960 * calling ->ki_cancel. We need the ctx_lock roundtrip here to
1961 * synchronize with them. In the cancellation case the list_del_init
1962 * itself is not actually needed, but harmless so we keep it in to
1963 * avoid further branches in the fast path.
1964 */
1965 spin_lock_irq(&ctx->completion_lock);
1966 if (!mask && !READ_ONCE(poll->canceled)) {
1967 add_wait_queue(poll->head, &poll->wait);
1968 spin_unlock_irq(&ctx->completion_lock);
1969 return;
1970 }
1971 list_del_init(&req->list);
Jens Axboe8c838782019-03-12 15:48:16 -06001972 io_poll_complete(ctx, req, mask);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001973 spin_unlock_irq(&ctx->completion_lock);
1974
Jens Axboe8c838782019-03-12 15:48:16 -06001975 io_cqring_ev_posted(ctx);
Jens Axboe89723d02019-11-05 15:32:58 -07001976
1977 io_put_req(req, &nxt);
1978 if (nxt)
1979 *workptr = &nxt->work;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001980}
1981
1982static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
1983 void *key)
1984{
1985 struct io_poll_iocb *poll = container_of(wait, struct io_poll_iocb,
1986 wait);
1987 struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
1988 struct io_ring_ctx *ctx = req->ctx;
1989 __poll_t mask = key_to_poll(key);
Jens Axboe8c838782019-03-12 15:48:16 -06001990 unsigned long flags;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001991
1992 /* for instances that support it check for an event match first: */
Jens Axboe8c838782019-03-12 15:48:16 -06001993 if (mask && !(mask & poll->events))
1994 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001995
1996 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06001997
1998 if (mask && spin_trylock_irqsave(&ctx->completion_lock, flags)) {
1999 list_del(&req->list);
2000 io_poll_complete(ctx, req, mask);
2001 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2002
2003 io_cqring_ev_posted(ctx);
Jens Axboeba816ad2019-09-28 11:36:45 -06002004 io_put_req(req, NULL);
Jens Axboe8c838782019-03-12 15:48:16 -06002005 } else {
Jens Axboe18d9be12019-09-10 09:13:05 -06002006 io_queue_async_work(ctx, req);
Jens Axboe8c838782019-03-12 15:48:16 -06002007 }
2008
Jens Axboe221c5eb2019-01-17 09:41:58 -07002009 return 1;
2010}
2011
2012struct io_poll_table {
2013 struct poll_table_struct pt;
2014 struct io_kiocb *req;
2015 int error;
2016};
2017
2018static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
2019 struct poll_table_struct *p)
2020{
2021 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
2022
2023 if (unlikely(pt->req->poll.head)) {
2024 pt->error = -EINVAL;
2025 return;
2026 }
2027
2028 pt->error = 0;
2029 pt->req->poll.head = head;
2030 add_wait_queue(head, &pt->req->poll.wait);
2031}
2032
Jens Axboe89723d02019-11-05 15:32:58 -07002033static int io_poll_add(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2034 struct io_kiocb **nxt)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002035{
2036 struct io_poll_iocb *poll = &req->poll;
2037 struct io_ring_ctx *ctx = req->ctx;
2038 struct io_poll_table ipt;
Jens Axboe8c838782019-03-12 15:48:16 -06002039 bool cancel = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002040 __poll_t mask;
2041 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002042
2043 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2044 return -EINVAL;
2045 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
2046 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06002047 if (!poll->file)
2048 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002049
Jens Axboe6cc47d12019-09-18 11:18:23 -06002050 req->submit.sqe = NULL;
Jens Axboe561fb042019-10-24 07:25:42 -06002051 INIT_IO_WORK(&req->work, io_poll_complete_work);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002052 events = READ_ONCE(sqe->poll_events);
2053 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
2054
Jens Axboe221c5eb2019-01-17 09:41:58 -07002055 poll->head = NULL;
Jens Axboe8c838782019-03-12 15:48:16 -06002056 poll->done = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002057 poll->canceled = false;
2058
2059 ipt.pt._qproc = io_poll_queue_proc;
2060 ipt.pt._key = poll->events;
2061 ipt.req = req;
2062 ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
2063
2064 /* initialized the list so that we can do list_empty checks */
2065 INIT_LIST_HEAD(&poll->wait.entry);
2066 init_waitqueue_func_entry(&poll->wait, io_poll_wake);
2067
Jens Axboe36703242019-07-25 10:20:18 -06002068 INIT_LIST_HEAD(&req->list);
2069
Jens Axboe221c5eb2019-01-17 09:41:58 -07002070 mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002071
2072 spin_lock_irq(&ctx->completion_lock);
Jens Axboe8c838782019-03-12 15:48:16 -06002073 if (likely(poll->head)) {
2074 spin_lock(&poll->head->lock);
2075 if (unlikely(list_empty(&poll->wait.entry))) {
2076 if (ipt.error)
2077 cancel = true;
2078 ipt.error = 0;
2079 mask = 0;
2080 }
2081 if (mask || ipt.error)
2082 list_del_init(&poll->wait.entry);
2083 else if (cancel)
2084 WRITE_ONCE(poll->canceled, true);
2085 else if (!poll->done) /* actually waiting for an event */
2086 list_add_tail(&req->list, &ctx->cancel_list);
2087 spin_unlock(&poll->head->lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002088 }
Jens Axboe8c838782019-03-12 15:48:16 -06002089 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06002090 ipt.error = 0;
2091 io_poll_complete(ctx, req, mask);
2092 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07002093 spin_unlock_irq(&ctx->completion_lock);
2094
Jens Axboe8c838782019-03-12 15:48:16 -06002095 if (mask) {
2096 io_cqring_ev_posted(ctx);
Jens Axboe89723d02019-11-05 15:32:58 -07002097 io_put_req(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002098 }
Jens Axboe8c838782019-03-12 15:48:16 -06002099 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002100}
2101
Jens Axboe5262f562019-09-17 12:26:57 -06002102static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
2103{
2104 struct io_ring_ctx *ctx;
Jens Axboe11365042019-10-16 09:08:32 -06002105 struct io_kiocb *req;
Jens Axboe5262f562019-09-17 12:26:57 -06002106 unsigned long flags;
2107
2108 req = container_of(timer, struct io_kiocb, timeout.timer);
2109 ctx = req->ctx;
2110 atomic_inc(&ctx->cq_timeouts);
2111
2112 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08002113 /*
Jens Axboe11365042019-10-16 09:08:32 -06002114 * We could be racing with timeout deletion. If the list is empty,
2115 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08002116 */
Jens Axboe842f9612019-10-29 12:34:10 -06002117 if (!list_empty(&req->list)) {
Jens Axboe11365042019-10-16 09:08:32 -06002118 struct io_kiocb *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06002119
Jens Axboe11365042019-10-16 09:08:32 -06002120 /*
2121 * Adjust the reqs sequence before the current one because it
2122 * will consume a slot in the cq_ring and the the cq_tail
2123 * pointer will be increased, otherwise other timeout reqs may
2124 * return in advance without waiting for enough wait_nr.
2125 */
2126 prev = req;
2127 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
2128 prev->sequence++;
Jens Axboe11365042019-10-16 09:08:32 -06002129 list_del_init(&req->list);
Jens Axboe11365042019-10-16 09:08:32 -06002130 }
Jens Axboe842f9612019-10-29 12:34:10 -06002131
Jens Axboe78e19bb2019-11-06 15:21:34 -07002132 io_cqring_fill_event(req, -ETIME);
Jens Axboe842f9612019-10-29 12:34:10 -06002133 io_commit_cqring(ctx);
Jens Axboe5262f562019-09-17 12:26:57 -06002134 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2135
Jens Axboe842f9612019-10-29 12:34:10 -06002136 io_cqring_ev_posted(ctx);
Jens Axboef1f40852019-11-05 20:33:16 -07002137 if (req->flags & REQ_F_LINK)
2138 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe842f9612019-10-29 12:34:10 -06002139 io_put_req(req, NULL);
Jens Axboe11365042019-10-16 09:08:32 -06002140 return HRTIMER_NORESTART;
2141}
2142
2143/*
2144 * Remove or update an existing timeout command
2145 */
2146static int io_timeout_remove(struct io_kiocb *req,
2147 const struct io_uring_sqe *sqe)
2148{
2149 struct io_ring_ctx *ctx = req->ctx;
2150 struct io_kiocb *treq;
2151 int ret = -ENOENT;
2152 __u64 user_data;
2153 unsigned flags;
2154
2155 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2156 return -EINVAL;
2157 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
2158 return -EINVAL;
2159 flags = READ_ONCE(sqe->timeout_flags);
2160 if (flags)
2161 return -EINVAL;
2162
2163 user_data = READ_ONCE(sqe->addr);
2164 spin_lock_irq(&ctx->completion_lock);
2165 list_for_each_entry(treq, &ctx->timeout_list, list) {
2166 if (user_data == treq->user_data) {
2167 list_del_init(&treq->list);
2168 ret = 0;
2169 break;
2170 }
2171 }
2172
2173 /* didn't find timeout */
2174 if (ret) {
2175fill_ev:
Jens Axboe78e19bb2019-11-06 15:21:34 -07002176 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06002177 io_commit_cqring(ctx);
2178 spin_unlock_irq(&ctx->completion_lock);
2179 io_cqring_ev_posted(ctx);
Jens Axboef1f40852019-11-05 20:33:16 -07002180 if (req->flags & REQ_F_LINK)
2181 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe11365042019-10-16 09:08:32 -06002182 io_put_req(req, NULL);
2183 return 0;
2184 }
2185
2186 ret = hrtimer_try_to_cancel(&treq->timeout.timer);
2187 if (ret == -1) {
2188 ret = -EBUSY;
2189 goto fill_ev;
2190 }
2191
Jens Axboe78e19bb2019-11-06 15:21:34 -07002192 io_cqring_fill_event(req, 0);
2193 io_cqring_fill_event(treq, -ECANCELED);
Jens Axboe11365042019-10-16 09:08:32 -06002194 io_commit_cqring(ctx);
2195 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06002196 io_cqring_ev_posted(ctx);
2197
Jens Axboe11365042019-10-16 09:08:32 -06002198 io_put_req(treq, NULL);
Jens Axboeba816ad2019-09-28 11:36:45 -06002199 io_put_req(req, NULL);
Jens Axboe11365042019-10-16 09:08:32 -06002200 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06002201}
2202
2203static int io_timeout(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2204{
yangerkun5da0fb12019-10-15 21:59:29 +08002205 unsigned count;
Jens Axboe5262f562019-09-17 12:26:57 -06002206 struct io_ring_ctx *ctx = req->ctx;
2207 struct list_head *entry;
Jens Axboea41525a2019-10-15 16:48:15 -06002208 enum hrtimer_mode mode;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06002209 struct timespec64 ts;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002210 unsigned span = 0;
Jens Axboea41525a2019-10-15 16:48:15 -06002211 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06002212
2213 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2214 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06002215 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len != 1)
2216 return -EINVAL;
2217 flags = READ_ONCE(sqe->timeout_flags);
2218 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06002219 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06002220
2221 if (get_timespec64(&ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06002222 return -EFAULT;
2223
Jens Axboe11365042019-10-16 09:08:32 -06002224 if (flags & IORING_TIMEOUT_ABS)
2225 mode = HRTIMER_MODE_ABS;
2226 else
2227 mode = HRTIMER_MODE_REL;
2228
2229 hrtimer_init(&req->timeout.timer, CLOCK_MONOTONIC, mode);
2230
Jens Axboe5262f562019-09-17 12:26:57 -06002231 /*
2232 * sqe->off holds how many events that need to occur for this
2233 * timeout event to be satisfied.
2234 */
2235 count = READ_ONCE(sqe->off);
2236 if (!count)
2237 count = 1;
2238
2239 req->sequence = ctx->cached_sq_head + count - 1;
yangerkun5da0fb12019-10-15 21:59:29 +08002240 /* reuse it to store the count */
2241 req->submit.sequence = count;
Jens Axboe5262f562019-09-17 12:26:57 -06002242 req->flags |= REQ_F_TIMEOUT;
2243
2244 /*
2245 * Insertion sort, ensuring the first entry in the list is always
2246 * the one we need first.
2247 */
Jens Axboe5262f562019-09-17 12:26:57 -06002248 spin_lock_irq(&ctx->completion_lock);
2249 list_for_each_prev(entry, &ctx->timeout_list) {
2250 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
yangerkun5da0fb12019-10-15 21:59:29 +08002251 unsigned nxt_sq_head;
2252 long long tmp, tmp_nxt;
Jens Axboe5262f562019-09-17 12:26:57 -06002253
yangerkun5da0fb12019-10-15 21:59:29 +08002254 /*
2255 * Since cached_sq_head + count - 1 can overflow, use type long
2256 * long to store it.
2257 */
2258 tmp = (long long)ctx->cached_sq_head + count - 1;
2259 nxt_sq_head = nxt->sequence - nxt->submit.sequence + 1;
2260 tmp_nxt = (long long)nxt_sq_head + nxt->submit.sequence - 1;
2261
2262 /*
2263 * cached_sq_head may overflow, and it will never overflow twice
2264 * once there is some timeout req still be valid.
2265 */
2266 if (ctx->cached_sq_head < nxt_sq_head)
yangerkun8b07a652019-10-17 12:12:35 +08002267 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08002268
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002269 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06002270 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002271
2272 /*
2273 * Sequence of reqs after the insert one and itself should
2274 * be adjusted because each timeout req consumes a slot.
2275 */
2276 span++;
2277 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06002278 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002279 req->sequence -= span;
Jens Axboe5262f562019-09-17 12:26:57 -06002280 list_add(&req->list, entry);
Jens Axboe5262f562019-09-17 12:26:57 -06002281 req->timeout.timer.function = io_timeout_fn;
Jens Axboea41525a2019-10-15 16:48:15 -06002282 hrtimer_start(&req->timeout.timer, timespec64_to_ktime(ts), mode);
Jens Axboe842f9612019-10-29 12:34:10 -06002283 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06002284 return 0;
2285}
2286
Jens Axboe62755e32019-10-28 21:49:21 -06002287static bool io_cancel_cb(struct io_wq_work *work, void *data)
2288{
2289 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
2290
2291 return req->user_data == (unsigned long) data;
2292}
2293
Jens Axboee977d6d2019-11-05 12:39:45 -07002294static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06002295{
Jens Axboe62755e32019-10-28 21:49:21 -06002296 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06002297 int ret = 0;
2298
Jens Axboe62755e32019-10-28 21:49:21 -06002299 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
2300 switch (cancel_ret) {
2301 case IO_WQ_CANCEL_OK:
2302 ret = 0;
2303 break;
2304 case IO_WQ_CANCEL_RUNNING:
2305 ret = -EALREADY;
2306 break;
2307 case IO_WQ_CANCEL_NOTFOUND:
2308 ret = -ENOENT;
2309 break;
2310 }
2311
Jens Axboee977d6d2019-11-05 12:39:45 -07002312 return ret;
2313}
2314
2315static int io_async_cancel(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2316 struct io_kiocb **nxt)
2317{
2318 struct io_ring_ctx *ctx = req->ctx;
2319 void *sqe_addr;
2320 int ret;
2321
2322 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2323 return -EINVAL;
2324 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
2325 sqe->cancel_flags)
2326 return -EINVAL;
2327
2328 sqe_addr = (void *) (unsigned long) READ_ONCE(sqe->addr);
2329 ret = io_async_cancel_one(ctx, sqe_addr);
2330
Jens Axboe62755e32019-10-28 21:49:21 -06002331 if (ret < 0 && (req->flags & REQ_F_LINK))
2332 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07002333 io_cqring_add_event(req, ret);
Jens Axboe62755e32019-10-28 21:49:21 -06002334 io_put_req(req, nxt);
2335 return 0;
2336}
2337
Pavel Begunkov267bc902019-11-07 01:41:08 +03002338static int io_req_defer(struct io_ring_ctx *ctx, struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -06002339{
Pavel Begunkov267bc902019-11-07 01:41:08 +03002340 const struct io_uring_sqe *sqe = req->submit.sqe;
Jens Axboede0617e2019-04-06 21:51:27 -06002341 struct io_uring_sqe *sqe_copy;
2342
2343 if (!io_sequence_defer(ctx, req) && list_empty(&ctx->defer_list))
2344 return 0;
2345
2346 sqe_copy = kmalloc(sizeof(*sqe_copy), GFP_KERNEL);
2347 if (!sqe_copy)
2348 return -EAGAIN;
2349
2350 spin_lock_irq(&ctx->completion_lock);
2351 if (!io_sequence_defer(ctx, req) && list_empty(&ctx->defer_list)) {
2352 spin_unlock_irq(&ctx->completion_lock);
2353 kfree(sqe_copy);
2354 return 0;
2355 }
2356
2357 memcpy(sqe_copy, sqe, sizeof(*sqe_copy));
2358 req->submit.sqe = sqe_copy;
2359
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002360 trace_io_uring_defer(ctx, req, false);
Jens Axboede0617e2019-04-06 21:51:27 -06002361 list_add_tail(&req->list, &ctx->defer_list);
2362 spin_unlock_irq(&ctx->completion_lock);
2363 return -EIOCBQUEUED;
2364}
2365
Jens Axboe2b188cc2019-01-07 10:46:33 -07002366static int __io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
Pavel Begunkov267bc902019-11-07 01:41:08 +03002367 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002368{
Jens Axboee0c5c572019-03-12 10:18:47 -06002369 int ret, opcode;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002370 struct sqe_submit *s = &req->submit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002371
Jens Axboe2b188cc2019-01-07 10:46:33 -07002372 opcode = READ_ONCE(s->sqe->opcode);
2373 switch (opcode) {
2374 case IORING_OP_NOP:
Jens Axboe78e19bb2019-11-06 15:21:34 -07002375 ret = io_nop(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002376 break;
2377 case IORING_OP_READV:
Jens Axboeedafcce2019-01-09 09:16:05 -07002378 if (unlikely(s->sqe->buf_index))
2379 return -EINVAL;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002380 ret = io_read(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002381 break;
2382 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07002383 if (unlikely(s->sqe->buf_index))
2384 return -EINVAL;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002385 ret = io_write(req, nxt, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07002386 break;
2387 case IORING_OP_READ_FIXED:
Pavel Begunkov267bc902019-11-07 01:41:08 +03002388 ret = io_read(req, nxt, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07002389 break;
2390 case IORING_OP_WRITE_FIXED:
Pavel Begunkov267bc902019-11-07 01:41:08 +03002391 ret = io_write(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002392 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002393 case IORING_OP_FSYNC:
Jens Axboeba816ad2019-09-28 11:36:45 -06002394 ret = io_fsync(req, s->sqe, nxt, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002395 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002396 case IORING_OP_POLL_ADD:
Jens Axboe89723d02019-11-05 15:32:58 -07002397 ret = io_poll_add(req, s->sqe, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002398 break;
2399 case IORING_OP_POLL_REMOVE:
2400 ret = io_poll_remove(req, s->sqe);
2401 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002402 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboeba816ad2019-09-28 11:36:45 -06002403 ret = io_sync_file_range(req, s->sqe, nxt, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002404 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002405 case IORING_OP_SENDMSG:
Jens Axboeba816ad2019-09-28 11:36:45 -06002406 ret = io_sendmsg(req, s->sqe, nxt, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06002407 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06002408 case IORING_OP_RECVMSG:
Jens Axboeba816ad2019-09-28 11:36:45 -06002409 ret = io_recvmsg(req, s->sqe, nxt, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06002410 break;
Jens Axboe5262f562019-09-17 12:26:57 -06002411 case IORING_OP_TIMEOUT:
2412 ret = io_timeout(req, s->sqe);
2413 break;
Jens Axboe11365042019-10-16 09:08:32 -06002414 case IORING_OP_TIMEOUT_REMOVE:
2415 ret = io_timeout_remove(req, s->sqe);
2416 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06002417 case IORING_OP_ACCEPT:
2418 ret = io_accept(req, s->sqe, nxt, force_nonblock);
2419 break;
Jens Axboe62755e32019-10-28 21:49:21 -06002420 case IORING_OP_ASYNC_CANCEL:
2421 ret = io_async_cancel(req, s->sqe, nxt);
2422 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002423 default:
2424 ret = -EINVAL;
2425 break;
2426 }
2427
Jens Axboedef596e2019-01-09 08:59:42 -07002428 if (ret)
2429 return ret;
2430
2431 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe9e645e112019-05-10 16:07:28 -06002432 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07002433 return -EAGAIN;
2434
2435 /* workqueue context doesn't hold uring_lock, grab it now */
Jackie Liuba5290c2019-10-09 09:19:59 +08002436 if (s->in_async)
Jens Axboedef596e2019-01-09 08:59:42 -07002437 mutex_lock(&ctx->uring_lock);
2438 io_iopoll_req_issued(req);
Jackie Liuba5290c2019-10-09 09:19:59 +08002439 if (s->in_async)
Jens Axboedef596e2019-01-09 08:59:42 -07002440 mutex_unlock(&ctx->uring_lock);
2441 }
2442
2443 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002444}
2445
Jens Axboe561fb042019-10-24 07:25:42 -06002446static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboe31b51512019-01-18 22:56:34 -07002447{
Jens Axboe561fb042019-10-24 07:25:42 -06002448 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002449 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002450 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe561fb042019-10-24 07:25:42 -06002451 struct sqe_submit *s = &req->submit;
2452 const struct io_uring_sqe *sqe = s->sqe;
2453 struct io_kiocb *nxt = NULL;
2454 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002455
Jens Axboe561fb042019-10-24 07:25:42 -06002456 /* Ensure we clear previously set non-block flag */
2457 req->rw.ki_flags &= ~IOCB_NOWAIT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002458
Jens Axboe561fb042019-10-24 07:25:42 -06002459 if (work->flags & IO_WQ_WORK_CANCEL)
2460 ret = -ECANCELED;
Jens Axboe31b51512019-01-18 22:56:34 -07002461
Jens Axboe561fb042019-10-24 07:25:42 -06002462 if (!ret) {
2463 s->has_user = (work->flags & IO_WQ_WORK_HAS_MM) != 0;
2464 s->in_async = true;
2465 do {
Pavel Begunkov267bc902019-11-07 01:41:08 +03002466 ret = __io_submit_sqe(ctx, req, &nxt, false);
Jens Axboe561fb042019-10-24 07:25:42 -06002467 /*
2468 * We can get EAGAIN for polled IO even though we're
2469 * forcing a sync submission from here, since we can't
2470 * wait for request slots on the block side.
2471 */
2472 if (ret != -EAGAIN)
2473 break;
2474 cond_resched();
2475 } while (1);
2476 }
Jens Axboe31b51512019-01-18 22:56:34 -07002477
Jens Axboe561fb042019-10-24 07:25:42 -06002478 /* drop submission reference */
2479 io_put_req(req, NULL);
Jens Axboe817869d2019-04-30 14:44:05 -06002480
Jens Axboe561fb042019-10-24 07:25:42 -06002481 if (ret) {
Jens Axboef1f40852019-11-05 20:33:16 -07002482 if (req->flags & REQ_F_LINK)
2483 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07002484 io_cqring_add_event(req, ret);
Jens Axboeba816ad2019-09-28 11:36:45 -06002485 io_put_req(req, NULL);
Jens Axboeedafcce2019-01-09 09:16:05 -07002486 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002487
Jens Axboe561fb042019-10-24 07:25:42 -06002488 /* async context always use a copy of the sqe */
2489 kfree(sqe);
2490
2491 /* if a dependent link is ready, pass it back */
2492 if (!ret && nxt) {
2493 io_prep_async_work(nxt);
2494 *workptr = &nxt->work;
Jens Axboeedafcce2019-01-09 09:16:05 -07002495 }
Jens Axboe31b51512019-01-18 22:56:34 -07002496}
Jens Axboe2b188cc2019-01-07 10:46:33 -07002497
Jens Axboe09bb8392019-03-13 12:39:28 -06002498static bool io_op_needs_file(const struct io_uring_sqe *sqe)
2499{
2500 int op = READ_ONCE(sqe->opcode);
2501
2502 switch (op) {
2503 case IORING_OP_NOP:
2504 case IORING_OP_POLL_REMOVE:
2505 return false;
2506 default:
2507 return true;
2508 }
2509}
2510
Jens Axboe65e19f52019-10-26 07:20:21 -06002511static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
2512 int index)
2513{
2514 struct fixed_file_table *table;
2515
2516 table = &ctx->file_table[index >> IORING_FILE_TABLE_SHIFT];
2517 return table->files[index & IORING_FILE_TABLE_MASK];
2518}
2519
Pavel Begunkov267bc902019-11-07 01:41:08 +03002520static int io_req_set_file(struct io_ring_ctx *ctx,
Jens Axboe09bb8392019-03-13 12:39:28 -06002521 struct io_submit_state *state, struct io_kiocb *req)
2522{
Pavel Begunkov267bc902019-11-07 01:41:08 +03002523 struct sqe_submit *s = &req->submit;
Jens Axboe09bb8392019-03-13 12:39:28 -06002524 unsigned flags;
2525 int fd;
2526
2527 flags = READ_ONCE(s->sqe->flags);
2528 fd = READ_ONCE(s->sqe->fd);
2529
Jackie Liu4fe2c962019-09-09 20:50:40 +08002530 if (flags & IOSQE_IO_DRAIN)
Jens Axboede0617e2019-04-06 21:51:27 -06002531 req->flags |= REQ_F_IO_DRAIN;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002532 /*
2533 * All io need record the previous position, if LINK vs DARIN,
2534 * it can be used to mark the position of the first IO in the
2535 * link list.
2536 */
2537 req->sequence = s->sequence;
Jens Axboede0617e2019-04-06 21:51:27 -06002538
Jens Axboe60c112b2019-06-21 10:20:18 -06002539 if (!io_op_needs_file(s->sqe))
Jens Axboe09bb8392019-03-13 12:39:28 -06002540 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06002541
2542 if (flags & IOSQE_FIXED_FILE) {
Jens Axboe65e19f52019-10-26 07:20:21 -06002543 if (unlikely(!ctx->file_table ||
Jens Axboe09bb8392019-03-13 12:39:28 -06002544 (unsigned) fd >= ctx->nr_user_files))
2545 return -EBADF;
Jens Axboeb7620122019-10-26 07:22:55 -06002546 fd = array_index_nospec(fd, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06002547 req->file = io_file_from_index(ctx, fd);
2548 if (!req->file)
Jens Axboe08a45172019-10-03 08:11:03 -06002549 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06002550 req->flags |= REQ_F_FIXED_FILE;
2551 } else {
2552 if (s->needs_fixed_file)
2553 return -EBADF;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002554 trace_io_uring_file_get(ctx, fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06002555 req->file = io_file_get(state, fd);
2556 if (unlikely(!req->file))
2557 return -EBADF;
2558 }
2559
2560 return 0;
2561}
2562
Jens Axboefcb323c2019-10-24 12:39:47 -06002563static int io_grab_files(struct io_ring_ctx *ctx, struct io_kiocb *req)
2564{
2565 int ret = -EBADF;
2566
2567 rcu_read_lock();
2568 spin_lock_irq(&ctx->inflight_lock);
2569 /*
2570 * We use the f_ops->flush() handler to ensure that we can flush
2571 * out work accessing these files if the fd is closed. Check if
2572 * the fd has changed since we started down this path, and disallow
2573 * this operation if it has.
2574 */
2575 if (fcheck(req->submit.ring_fd) == req->submit.ring_file) {
2576 list_add(&req->inflight_entry, &ctx->inflight_list);
2577 req->flags |= REQ_F_INFLIGHT;
2578 req->work.files = current->files;
2579 ret = 0;
2580 }
2581 spin_unlock_irq(&ctx->inflight_lock);
2582 rcu_read_unlock();
2583
2584 return ret;
2585}
2586
Jens Axboe2665abf2019-11-05 12:40:47 -07002587static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
2588{
2589 struct io_kiocb *req = container_of(timer, struct io_kiocb,
2590 timeout.timer);
2591 struct io_ring_ctx *ctx = req->ctx;
2592 struct io_kiocb *prev = NULL;
2593 unsigned long flags;
2594 int ret = -ETIME;
2595
2596 spin_lock_irqsave(&ctx->completion_lock, flags);
2597
2598 /*
2599 * We don't expect the list to be empty, that will only happen if we
2600 * race with the completion of the linked work.
2601 */
2602 if (!list_empty(&req->list)) {
2603 prev = list_entry(req->list.prev, struct io_kiocb, link_list);
2604 list_del_init(&req->list);
2605 }
2606
2607 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2608
2609 if (prev) {
2610 void *user_data = (void *) (unsigned long) prev->user_data;
2611 ret = io_async_cancel_one(ctx, user_data);
2612 }
2613
Jens Axboe78e19bb2019-11-06 15:21:34 -07002614 io_cqring_add_event(req, ret);
Jens Axboe2665abf2019-11-05 12:40:47 -07002615 io_put_req(req, NULL);
2616 return HRTIMER_NORESTART;
2617}
2618
2619static int io_queue_linked_timeout(struct io_kiocb *req, struct io_kiocb *nxt)
2620{
2621 const struct io_uring_sqe *sqe = nxt->submit.sqe;
2622 enum hrtimer_mode mode;
2623 struct timespec64 ts;
2624 int ret = -EINVAL;
2625
2626 if (sqe->ioprio || sqe->buf_index || sqe->len != 1 || sqe->off)
2627 goto err;
2628 if (sqe->timeout_flags & ~IORING_TIMEOUT_ABS)
2629 goto err;
2630 if (get_timespec64(&ts, u64_to_user_ptr(sqe->addr))) {
2631 ret = -EFAULT;
2632 goto err;
2633 }
2634
2635 req->flags |= REQ_F_LINK_TIMEOUT;
2636
2637 if (sqe->timeout_flags & IORING_TIMEOUT_ABS)
2638 mode = HRTIMER_MODE_ABS;
2639 else
2640 mode = HRTIMER_MODE_REL;
2641 hrtimer_init(&nxt->timeout.timer, CLOCK_MONOTONIC, mode);
2642 nxt->timeout.timer.function = io_link_timeout_fn;
2643 hrtimer_start(&nxt->timeout.timer, timespec64_to_ktime(ts), mode);
2644 ret = 0;
2645err:
2646 /* drop submission reference */
2647 io_put_req(nxt, NULL);
2648
2649 if (ret) {
2650 struct io_ring_ctx *ctx = req->ctx;
2651
2652 /*
2653 * Break the link and fail linked timeout, parent will get
2654 * failed by the regular submission path.
2655 */
2656 list_del(&nxt->list);
Jens Axboe78e19bb2019-11-06 15:21:34 -07002657 io_cqring_fill_event(nxt, ret);
Jens Axboe2665abf2019-11-05 12:40:47 -07002658 trace_io_uring_fail_link(req, nxt);
2659 io_commit_cqring(ctx);
2660 io_put_req(nxt, NULL);
2661 ret = -ECANCELED;
2662 }
2663
2664 return ret;
2665}
2666
2667static inline struct io_kiocb *io_get_linked_timeout(struct io_kiocb *req)
2668{
2669 struct io_kiocb *nxt;
2670
2671 if (!(req->flags & REQ_F_LINK))
2672 return NULL;
2673
2674 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, list);
2675 if (nxt && nxt->submit.sqe->opcode == IORING_OP_LINK_TIMEOUT)
2676 return nxt;
2677
2678 return NULL;
2679}
2680
Pavel Begunkov267bc902019-11-07 01:41:08 +03002681static int __io_queue_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002682{
Jens Axboe2665abf2019-11-05 12:40:47 -07002683 struct io_kiocb *nxt;
Jens Axboee0c5c572019-03-12 10:18:47 -06002684 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002685
Jens Axboe2665abf2019-11-05 12:40:47 -07002686 nxt = io_get_linked_timeout(req);
2687 if (unlikely(nxt)) {
2688 ret = io_queue_linked_timeout(req, nxt);
2689 if (ret)
2690 goto err;
2691 }
2692
Pavel Begunkov267bc902019-11-07 01:41:08 +03002693 ret = __io_submit_sqe(ctx, req, NULL, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06002694
2695 /*
2696 * We async punt it if the file wasn't marked NOWAIT, or if the file
2697 * doesn't support non-blocking read/write attempts
2698 */
2699 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
2700 (req->flags & REQ_F_MUST_PUNT))) {
Pavel Begunkov267bc902019-11-07 01:41:08 +03002701 struct sqe_submit *s = &req->submit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002702 struct io_uring_sqe *sqe_copy;
2703
Jackie Liu954dab12019-09-18 10:37:52 +08002704 sqe_copy = kmemdup(s->sqe, sizeof(*sqe_copy), GFP_KERNEL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002705 if (sqe_copy) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07002706 s->sqe = sqe_copy;
Jens Axboefcb323c2019-10-24 12:39:47 -06002707 if (req->work.flags & IO_WQ_WORK_NEEDS_FILES) {
2708 ret = io_grab_files(ctx, req);
2709 if (ret) {
2710 kfree(sqe_copy);
2711 goto err;
2712 }
2713 }
Jens Axboee65ef562019-03-12 10:16:44 -06002714
2715 /*
2716 * Queued up for async execution, worker will release
Jens Axboe9e645e112019-05-10 16:07:28 -06002717 * submit reference when the iocb is actually submitted.
Jens Axboee65ef562019-03-12 10:16:44 -06002718 */
Jens Axboefcb323c2019-10-24 12:39:47 -06002719 io_queue_async_work(ctx, req);
Jens Axboee65ef562019-03-12 10:16:44 -06002720 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002721 }
2722 }
Jens Axboee65ef562019-03-12 10:16:44 -06002723
2724 /* drop submission reference */
Jens Axboefcb323c2019-10-24 12:39:47 -06002725err:
Jens Axboeba816ad2019-09-28 11:36:45 -06002726 io_put_req(req, NULL);
Jens Axboee65ef562019-03-12 10:16:44 -06002727
2728 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06002729 if (ret) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07002730 io_cqring_add_event(req, ret);
Jens Axboe9e645e112019-05-10 16:07:28 -06002731 if (req->flags & REQ_F_LINK)
2732 req->flags |= REQ_F_FAIL_LINK;
Jens Axboeba816ad2019-09-28 11:36:45 -06002733 io_put_req(req, NULL);
Jens Axboe9e645e112019-05-10 16:07:28 -06002734 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002735
2736 return ret;
2737}
2738
Pavel Begunkov267bc902019-11-07 01:41:08 +03002739static int io_queue_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08002740{
2741 int ret;
2742
Pavel Begunkov267bc902019-11-07 01:41:08 +03002743 ret = io_req_defer(ctx, req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002744 if (ret) {
2745 if (ret != -EIOCBQUEUED) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07002746 io_cqring_add_event(req, ret);
2747 io_double_put_req(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002748 }
2749 return 0;
2750 }
2751
Pavel Begunkov267bc902019-11-07 01:41:08 +03002752 return __io_queue_sqe(ctx, req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002753}
2754
2755static int io_queue_link_head(struct io_ring_ctx *ctx, struct io_kiocb *req,
Pavel Begunkov267bc902019-11-07 01:41:08 +03002756 struct io_kiocb *shadow)
Jackie Liu4fe2c962019-09-09 20:50:40 +08002757{
2758 int ret;
2759 int need_submit = false;
2760
2761 if (!shadow)
Pavel Begunkov267bc902019-11-07 01:41:08 +03002762 return io_queue_sqe(ctx, req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002763
2764 /*
2765 * Mark the first IO in link list as DRAIN, let all the following
2766 * IOs enter the defer list. all IO needs to be completed before link
2767 * list.
2768 */
2769 req->flags |= REQ_F_IO_DRAIN;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002770 ret = io_req_defer(ctx, req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002771 if (ret) {
2772 if (ret != -EIOCBQUEUED) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07002773 io_cqring_add_event(req, ret);
2774 io_double_put_req(req);
Pavel Begunkov7b202382019-10-27 22:10:36 +03002775 __io_free_req(shadow);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002776 return 0;
2777 }
2778 } else {
2779 /*
2780 * If ret == 0 means that all IOs in front of link io are
2781 * running done. let's queue link head.
2782 */
2783 need_submit = true;
2784 }
2785
2786 /* Insert shadow req to defer_list, blocking next IOs */
2787 spin_lock_irq(&ctx->completion_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002788 trace_io_uring_defer(ctx, shadow, true);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002789 list_add_tail(&shadow->list, &ctx->defer_list);
2790 spin_unlock_irq(&ctx->completion_lock);
2791
2792 if (need_submit)
Pavel Begunkov267bc902019-11-07 01:41:08 +03002793 return __io_queue_sqe(ctx, req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002794
2795 return 0;
2796}
2797
Jens Axboe9e645e112019-05-10 16:07:28 -06002798#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK)
2799
Pavel Begunkov196be952019-11-07 01:41:06 +03002800static void io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
Pavel Begunkov267bc902019-11-07 01:41:08 +03002801 struct io_submit_state *state, struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06002802{
2803 struct io_uring_sqe *sqe_copy;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002804 struct sqe_submit *s = &req->submit;
Jens Axboe9e645e112019-05-10 16:07:28 -06002805 int ret;
2806
Jens Axboe78e19bb2019-11-06 15:21:34 -07002807 req->user_data = s->sqe->user_data;
2808
Jens Axboe9e645e112019-05-10 16:07:28 -06002809 /* enforce forwards compatibility on users */
2810 if (unlikely(s->sqe->flags & ~SQE_VALID_FLAGS)) {
2811 ret = -EINVAL;
Pavel Begunkov196be952019-11-07 01:41:06 +03002812 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06002813 }
2814
Pavel Begunkov267bc902019-11-07 01:41:08 +03002815 ret = io_req_set_file(ctx, state, req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002816 if (unlikely(ret)) {
2817err_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07002818 io_cqring_add_event(req, ret);
2819 io_double_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002820 return;
2821 }
2822
Jens Axboe9e645e112019-05-10 16:07:28 -06002823 /*
2824 * If we already have a head request, queue this one for async
2825 * submittal once the head completes. If we don't have a head but
2826 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
2827 * submitted sync once the chain is complete. If none of those
2828 * conditions are true (normal request), then just queue it.
2829 */
2830 if (*link) {
2831 struct io_kiocb *prev = *link;
2832
2833 sqe_copy = kmemdup(s->sqe, sizeof(*sqe_copy), GFP_KERNEL);
2834 if (!sqe_copy) {
2835 ret = -EAGAIN;
2836 goto err_req;
2837 }
2838
2839 s->sqe = sqe_copy;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002840 trace_io_uring_link(ctx, req, prev);
Jens Axboe9e645e112019-05-10 16:07:28 -06002841 list_add_tail(&req->list, &prev->link_list);
2842 } else if (s->sqe->flags & IOSQE_IO_LINK) {
2843 req->flags |= REQ_F_LINK;
2844
Jens Axboe9e645e112019-05-10 16:07:28 -06002845 INIT_LIST_HEAD(&req->link_list);
2846 *link = req;
Jens Axboe2665abf2019-11-05 12:40:47 -07002847 } else if (READ_ONCE(s->sqe->opcode) == IORING_OP_LINK_TIMEOUT) {
2848 /* Only valid as a linked SQE */
2849 ret = -EINVAL;
2850 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06002851 } else {
Pavel Begunkov267bc902019-11-07 01:41:08 +03002852 io_queue_sqe(ctx, req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002853 }
2854}
2855
Jens Axboe9a56a232019-01-09 09:06:50 -07002856/*
2857 * Batched submission is done, ensure local IO is flushed out.
2858 */
2859static void io_submit_state_end(struct io_submit_state *state)
2860{
2861 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06002862 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07002863 if (state->free_reqs)
2864 kmem_cache_free_bulk(req_cachep, state->free_reqs,
2865 &state->reqs[state->cur_req]);
Jens Axboe9a56a232019-01-09 09:06:50 -07002866}
2867
2868/*
2869 * Start submission side cache.
2870 */
2871static void io_submit_state_start(struct io_submit_state *state,
2872 struct io_ring_ctx *ctx, unsigned max_ios)
2873{
2874 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07002875 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07002876 state->file = NULL;
2877 state->ios_left = max_ios;
2878}
2879
Jens Axboe2b188cc2019-01-07 10:46:33 -07002880static void io_commit_sqring(struct io_ring_ctx *ctx)
2881{
Hristo Venev75b28af2019-08-26 17:23:46 +00002882 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002883
Hristo Venev75b28af2019-08-26 17:23:46 +00002884 if (ctx->cached_sq_head != READ_ONCE(rings->sq.head)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07002885 /*
2886 * Ensure any loads from the SQEs are done at this point,
2887 * since once we write the new head, the application could
2888 * write new data to them.
2889 */
Hristo Venev75b28af2019-08-26 17:23:46 +00002890 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002891 }
2892}
2893
2894/*
Jens Axboe2b188cc2019-01-07 10:46:33 -07002895 * Fetch an sqe, if one is available. Note that s->sqe will point to memory
2896 * that is mapped by userspace. This means that care needs to be taken to
2897 * ensure that reads are stable, as we cannot rely on userspace always
2898 * being a good citizen. If members of the sqe are validated and then later
2899 * used, it's important that those reads are done through READ_ONCE() to
2900 * prevent a re-load down the line.
2901 */
2902static bool io_get_sqring(struct io_ring_ctx *ctx, struct sqe_submit *s)
2903{
Hristo Venev75b28af2019-08-26 17:23:46 +00002904 struct io_rings *rings = ctx->rings;
2905 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002906 unsigned head;
2907
2908 /*
2909 * The cached sq head (or cq tail) serves two purposes:
2910 *
2911 * 1) allows us to batch the cost of updating the user visible
2912 * head updates.
2913 * 2) allows the kernel side to track the head on its own, even
2914 * though the application is the one updating it.
2915 */
2916 head = ctx->cached_sq_head;
Stefan Bühlere523a292019-04-19 11:57:44 +02002917 /* make sure SQ entry isn't read before tail */
Hristo Venev75b28af2019-08-26 17:23:46 +00002918 if (head == smp_load_acquire(&rings->sq.tail))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002919 return false;
2920
Hristo Venev75b28af2019-08-26 17:23:46 +00002921 head = READ_ONCE(sq_array[head & ctx->sq_mask]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002922 if (head < ctx->sq_entries) {
Jens Axboefcb323c2019-10-24 12:39:47 -06002923 s->ring_file = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002924 s->sqe = &ctx->sq_sqes[head];
Jackie Liu8776f3f2019-09-09 20:50:39 +08002925 s->sequence = ctx->cached_sq_head;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002926 ctx->cached_sq_head++;
2927 return true;
2928 }
2929
2930 /* drop invalid entries */
2931 ctx->cached_sq_head++;
Jens Axboe498ccd92019-10-25 10:04:25 -06002932 ctx->cached_sq_dropped++;
2933 WRITE_ONCE(rings->sq_dropped, ctx->cached_sq_dropped);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002934 return false;
2935}
2936
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002937static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkovae9428c2019-11-06 00:22:14 +03002938 struct file *ring_file, int ring_fd,
2939 struct mm_struct **mm, bool async)
Jens Axboe6c271ce2019-01-10 11:22:30 -07002940{
2941 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002942 struct io_kiocb *link = NULL;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002943 struct io_kiocb *shadow_req = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002944 int i, submitted = 0;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03002945 bool mm_fault = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002946
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07002947 if (!list_empty(&ctx->cq_overflow_list)) {
2948 io_cqring_overflow_flush(ctx, false);
2949 return -EBUSY;
2950 }
2951
Jens Axboe6c271ce2019-01-10 11:22:30 -07002952 if (nr > IO_PLUG_THRESHOLD) {
2953 io_submit_state_start(&state, ctx, nr);
2954 statep = &state;
2955 }
2956
2957 for (i = 0; i < nr; i++) {
Pavel Begunkov196be952019-11-07 01:41:06 +03002958 struct io_kiocb *req;
Pavel Begunkov50585b92019-11-07 01:41:07 +03002959 unsigned int sqe_flags;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002960
Pavel Begunkov196be952019-11-07 01:41:06 +03002961 req = io_get_req(ctx, statep);
2962 if (unlikely(!req)) {
2963 if (!submitted)
2964 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002965 break;
Pavel Begunkov196be952019-11-07 01:41:06 +03002966 }
Pavel Begunkov50585b92019-11-07 01:41:07 +03002967 if (!io_get_sqring(ctx, &req->submit)) {
Pavel Begunkov196be952019-11-07 01:41:06 +03002968 __io_free_req(req);
2969 break;
2970 }
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002971
Pavel Begunkov50585b92019-11-07 01:41:07 +03002972 if (io_sqe_needs_user(req->submit.sqe) && !*mm) {
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03002973 mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm);
2974 if (!mm_fault) {
2975 use_mm(ctx->sqo_mm);
2976 *mm = ctx->sqo_mm;
2977 }
2978 }
2979
Pavel Begunkov50585b92019-11-07 01:41:07 +03002980 sqe_flags = req->submit.sqe->flags;
2981
2982 if (link && (sqe_flags & IOSQE_IO_DRAIN)) {
Jackie Liu4fe2c962019-09-09 20:50:40 +08002983 if (!shadow_req) {
2984 shadow_req = io_get_req(ctx, NULL);
Jackie Liua1041c22019-09-18 17:25:52 +08002985 if (unlikely(!shadow_req))
2986 goto out;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002987 shadow_req->flags |= (REQ_F_IO_DRAIN | REQ_F_SHADOW_DRAIN);
2988 refcount_dec(&shadow_req->refs);
2989 }
Pavel Begunkov50585b92019-11-07 01:41:07 +03002990 shadow_req->sequence = req->submit.sequence;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002991 }
2992
Jackie Liua1041c22019-09-18 17:25:52 +08002993out:
Pavel Begunkov50585b92019-11-07 01:41:07 +03002994 req->submit.ring_file = ring_file;
2995 req->submit.ring_fd = ring_fd;
2996 req->submit.has_user = *mm != NULL;
2997 req->submit.in_async = async;
2998 req->submit.needs_fixed_file = async;
2999 trace_io_uring_submit_sqe(ctx, req->submit.sqe->user_data,
3000 true, async);
Pavel Begunkov267bc902019-11-07 01:41:08 +03003001 io_submit_sqe(ctx, req, statep, &link);
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03003002 submitted++;
Pavel Begunkove5eb6362019-11-06 00:22:15 +03003003
3004 /*
3005 * If previous wasn't linked and we have a linked command,
3006 * that's the end of the chain. Submit the previous link.
3007 */
Pavel Begunkov50585b92019-11-07 01:41:07 +03003008 if (!(sqe_flags & IOSQE_IO_LINK) && link) {
Pavel Begunkov267bc902019-11-07 01:41:08 +03003009 io_queue_link_head(ctx, link, shadow_req);
Pavel Begunkove5eb6362019-11-06 00:22:15 +03003010 link = NULL;
3011 shadow_req = NULL;
3012 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07003013 }
3014
Jens Axboe9e645e112019-05-10 16:07:28 -06003015 if (link)
Pavel Begunkov267bc902019-11-07 01:41:08 +03003016 io_queue_link_head(ctx, link, shadow_req);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003017 if (statep)
3018 io_submit_state_end(&state);
3019
Pavel Begunkovae9428c2019-11-06 00:22:14 +03003020 /* Commit SQ ring head once we've consumed and submitted all SQEs */
3021 io_commit_sqring(ctx);
3022
Jens Axboe6c271ce2019-01-10 11:22:30 -07003023 return submitted;
3024}
3025
3026static int io_sq_thread(void *data)
3027{
Jens Axboe6c271ce2019-01-10 11:22:30 -07003028 struct io_ring_ctx *ctx = data;
3029 struct mm_struct *cur_mm = NULL;
3030 mm_segment_t old_fs;
3031 DEFINE_WAIT(wait);
3032 unsigned inflight;
3033 unsigned long timeout;
3034
Jackie Liua4c0b3d2019-07-08 13:41:12 +08003035 complete(&ctx->sqo_thread_started);
3036
Jens Axboe6c271ce2019-01-10 11:22:30 -07003037 old_fs = get_fs();
3038 set_fs(USER_DS);
3039
3040 timeout = inflight = 0;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003041 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003042 unsigned int to_submit;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003043 int ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003044
3045 if (inflight) {
3046 unsigned nr_events = 0;
3047
3048 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe2b2ed972019-10-25 10:06:15 -06003049 /*
3050 * inflight is the count of the maximum possible
3051 * entries we submitted, but it can be smaller
3052 * if we dropped some of them. If we don't have
3053 * poll entries available, then we know that we
3054 * have nothing left to poll for. Reset the
3055 * inflight count to zero in that case.
3056 */
3057 mutex_lock(&ctx->uring_lock);
3058 if (!list_empty(&ctx->poll_list))
3059 __io_iopoll_check(ctx, &nr_events, 0);
3060 else
3061 inflight = 0;
3062 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003063 } else {
3064 /*
3065 * Normal IO, just pretend everything completed.
3066 * We don't have to poll completions for that.
3067 */
3068 nr_events = inflight;
3069 }
3070
3071 inflight -= nr_events;
3072 if (!inflight)
3073 timeout = jiffies + ctx->sq_thread_idle;
3074 }
3075
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003076 to_submit = io_sqring_entries(ctx);
3077 if (!to_submit) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07003078 /*
3079 * We're polling. If we're within the defined idle
3080 * period, then let us spin without work before going
3081 * to sleep.
3082 */
3083 if (inflight || !time_after(jiffies, timeout)) {
Jens Axboe9831a902019-09-19 09:48:55 -06003084 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07003085 continue;
3086 }
3087
3088 /*
3089 * Drop cur_mm before scheduling, we can't hold it for
3090 * long periods (or over schedule()). Do this before
3091 * adding ourselves to the waitqueue, as the unuse/drop
3092 * may sleep.
3093 */
3094 if (cur_mm) {
3095 unuse_mm(cur_mm);
3096 mmput(cur_mm);
3097 cur_mm = NULL;
3098 }
3099
3100 prepare_to_wait(&ctx->sqo_wait, &wait,
3101 TASK_INTERRUPTIBLE);
3102
3103 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00003104 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02003105 /* make sure to read SQ tail after writing flags */
3106 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07003107
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003108 to_submit = io_sqring_entries(ctx);
3109 if (!to_submit) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003110 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07003111 finish_wait(&ctx->sqo_wait, &wait);
3112 break;
3113 }
3114 if (signal_pending(current))
3115 flush_signals(current);
3116 schedule();
3117 finish_wait(&ctx->sqo_wait, &wait);
3118
Hristo Venev75b28af2019-08-26 17:23:46 +00003119 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003120 continue;
3121 }
3122 finish_wait(&ctx->sqo_wait, &wait);
3123
Hristo Venev75b28af2019-08-26 17:23:46 +00003124 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003125 }
3126
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003127 to_submit = min(to_submit, ctx->sq_entries);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003128 ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true);
3129 if (ret > 0)
3130 inflight += ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003131 }
3132
3133 set_fs(old_fs);
3134 if (cur_mm) {
3135 unuse_mm(cur_mm);
3136 mmput(cur_mm);
3137 }
Jens Axboe06058632019-04-13 09:26:03 -06003138
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003139 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06003140
Jens Axboe6c271ce2019-01-10 11:22:30 -07003141 return 0;
3142}
3143
Jens Axboebda52162019-09-24 13:47:15 -06003144struct io_wait_queue {
3145 struct wait_queue_entry wq;
3146 struct io_ring_ctx *ctx;
3147 unsigned to_wait;
3148 unsigned nr_timeouts;
3149};
3150
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003151static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06003152{
3153 struct io_ring_ctx *ctx = iowq->ctx;
3154
3155 /*
3156 * Wake up if we have enough events, or if a timeout occured since we
3157 * started waiting. For timeouts, we always want to return to userspace,
3158 * regardless of event count.
3159 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003160 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06003161 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
3162}
3163
3164static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
3165 int wake_flags, void *key)
3166{
3167 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
3168 wq);
3169
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003170 /* use noflush == true, as we can't safely rely on locking context */
3171 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06003172 return -1;
3173
3174 return autoremove_wake_function(curr, mode, wake_flags, key);
3175}
3176
Jens Axboe2b188cc2019-01-07 10:46:33 -07003177/*
3178 * Wait until events become available, if we don't already have some. The
3179 * application must reap them itself, as they reside on the shared cq ring.
3180 */
3181static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
3182 const sigset_t __user *sig, size_t sigsz)
3183{
Jens Axboebda52162019-09-24 13:47:15 -06003184 struct io_wait_queue iowq = {
3185 .wq = {
3186 .private = current,
3187 .func = io_wake_function,
3188 .entry = LIST_HEAD_INIT(iowq.wq.entry),
3189 },
3190 .ctx = ctx,
3191 .to_wait = min_events,
3192 };
Hristo Venev75b28af2019-08-26 17:23:46 +00003193 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08003194 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003195
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003196 if (io_cqring_events(ctx, false) >= min_events)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003197 return 0;
3198
3199 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01003200#ifdef CONFIG_COMPAT
3201 if (in_compat_syscall())
3202 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07003203 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01003204 else
3205#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07003206 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01003207
Jens Axboe2b188cc2019-01-07 10:46:33 -07003208 if (ret)
3209 return ret;
3210 }
3211
Jens Axboebda52162019-09-24 13:47:15 -06003212 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02003213 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06003214 do {
3215 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
3216 TASK_INTERRUPTIBLE);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003217 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06003218 break;
3219 schedule();
3220 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08003221 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06003222 break;
3223 }
3224 } while (1);
3225 finish_wait(&ctx->wait, &iowq.wq);
3226
Jackie Liue9ffa5c2019-10-29 11:16:42 +08003227 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003228
Hristo Venev75b28af2019-08-26 17:23:46 +00003229 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003230}
3231
Jens Axboe6b063142019-01-10 22:13:58 -07003232static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
3233{
3234#if defined(CONFIG_UNIX)
3235 if (ctx->ring_sock) {
3236 struct sock *sock = ctx->ring_sock->sk;
3237 struct sk_buff *skb;
3238
3239 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
3240 kfree_skb(skb);
3241 }
3242#else
3243 int i;
3244
Jens Axboe65e19f52019-10-26 07:20:21 -06003245 for (i = 0; i < ctx->nr_user_files; i++) {
3246 struct file *file;
3247
3248 file = io_file_from_index(ctx, i);
3249 if (file)
3250 fput(file);
3251 }
Jens Axboe6b063142019-01-10 22:13:58 -07003252#endif
3253}
3254
3255static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
3256{
Jens Axboe65e19f52019-10-26 07:20:21 -06003257 unsigned nr_tables, i;
3258
3259 if (!ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003260 return -ENXIO;
3261
3262 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06003263 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
3264 for (i = 0; i < nr_tables; i++)
3265 kfree(ctx->file_table[i].files);
3266 kfree(ctx->file_table);
3267 ctx->file_table = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003268 ctx->nr_user_files = 0;
3269 return 0;
3270}
3271
Jens Axboe6c271ce2019-01-10 11:22:30 -07003272static void io_sq_thread_stop(struct io_ring_ctx *ctx)
3273{
3274 if (ctx->sqo_thread) {
Jackie Liua4c0b3d2019-07-08 13:41:12 +08003275 wait_for_completion(&ctx->sqo_thread_started);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003276 /*
3277 * The park is a bit of a work-around, without it we get
3278 * warning spews on shutdown with SQPOLL set and affinity
3279 * set to a single CPU.
3280 */
Jens Axboe06058632019-04-13 09:26:03 -06003281 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003282 kthread_stop(ctx->sqo_thread);
3283 ctx->sqo_thread = NULL;
3284 }
3285}
3286
Jens Axboe6b063142019-01-10 22:13:58 -07003287static void io_finish_async(struct io_ring_ctx *ctx)
3288{
Jens Axboe6c271ce2019-01-10 11:22:30 -07003289 io_sq_thread_stop(ctx);
3290
Jens Axboe561fb042019-10-24 07:25:42 -06003291 if (ctx->io_wq) {
3292 io_wq_destroy(ctx->io_wq);
3293 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003294 }
3295}
3296
3297#if defined(CONFIG_UNIX)
3298static void io_destruct_skb(struct sk_buff *skb)
3299{
3300 struct io_ring_ctx *ctx = skb->sk->sk_user_data;
3301
Jens Axboe561fb042019-10-24 07:25:42 -06003302 if (ctx->io_wq)
3303 io_wq_flush(ctx->io_wq);
Jens Axboe8a997342019-10-09 14:40:13 -06003304
Jens Axboe6b063142019-01-10 22:13:58 -07003305 unix_destruct_scm(skb);
3306}
3307
3308/*
3309 * Ensure the UNIX gc is aware of our file set, so we are certain that
3310 * the io_uring can be safely unregistered on process exit, even if we have
3311 * loops in the file referencing.
3312 */
3313static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
3314{
3315 struct sock *sk = ctx->ring_sock->sk;
3316 struct scm_fp_list *fpl;
3317 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06003318 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07003319
3320 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
3321 unsigned long inflight = ctx->user->unix_inflight + nr;
3322
3323 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
3324 return -EMFILE;
3325 }
3326
3327 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
3328 if (!fpl)
3329 return -ENOMEM;
3330
3331 skb = alloc_skb(0, GFP_KERNEL);
3332 if (!skb) {
3333 kfree(fpl);
3334 return -ENOMEM;
3335 }
3336
3337 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07003338
Jens Axboe08a45172019-10-03 08:11:03 -06003339 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07003340 fpl->user = get_uid(ctx->user);
3341 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003342 struct file *file = io_file_from_index(ctx, i + offset);
3343
3344 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06003345 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06003346 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06003347 unix_inflight(fpl->user, fpl->fp[nr_files]);
3348 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07003349 }
3350
Jens Axboe08a45172019-10-03 08:11:03 -06003351 if (nr_files) {
3352 fpl->max = SCM_MAX_FD;
3353 fpl->count = nr_files;
3354 UNIXCB(skb).fp = fpl;
3355 skb->destructor = io_destruct_skb;
3356 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
3357 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07003358
Jens Axboe08a45172019-10-03 08:11:03 -06003359 for (i = 0; i < nr_files; i++)
3360 fput(fpl->fp[i]);
3361 } else {
3362 kfree_skb(skb);
3363 kfree(fpl);
3364 }
Jens Axboe6b063142019-01-10 22:13:58 -07003365
3366 return 0;
3367}
3368
3369/*
3370 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
3371 * causes regular reference counting to break down. We rely on the UNIX
3372 * garbage collection to take care of this problem for us.
3373 */
3374static int io_sqe_files_scm(struct io_ring_ctx *ctx)
3375{
3376 unsigned left, total;
3377 int ret = 0;
3378
3379 total = 0;
3380 left = ctx->nr_user_files;
3381 while (left) {
3382 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07003383
3384 ret = __io_sqe_files_scm(ctx, this_files, total);
3385 if (ret)
3386 break;
3387 left -= this_files;
3388 total += this_files;
3389 }
3390
3391 if (!ret)
3392 return 0;
3393
3394 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003395 struct file *file = io_file_from_index(ctx, total);
3396
3397 if (file)
3398 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07003399 total++;
3400 }
3401
3402 return ret;
3403}
3404#else
3405static int io_sqe_files_scm(struct io_ring_ctx *ctx)
3406{
3407 return 0;
3408}
3409#endif
3410
Jens Axboe65e19f52019-10-26 07:20:21 -06003411static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
3412 unsigned nr_files)
3413{
3414 int i;
3415
3416 for (i = 0; i < nr_tables; i++) {
3417 struct fixed_file_table *table = &ctx->file_table[i];
3418 unsigned this_files;
3419
3420 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
3421 table->files = kcalloc(this_files, sizeof(struct file *),
3422 GFP_KERNEL);
3423 if (!table->files)
3424 break;
3425 nr_files -= this_files;
3426 }
3427
3428 if (i == nr_tables)
3429 return 0;
3430
3431 for (i = 0; i < nr_tables; i++) {
3432 struct fixed_file_table *table = &ctx->file_table[i];
3433 kfree(table->files);
3434 }
3435 return 1;
3436}
3437
Jens Axboe6b063142019-01-10 22:13:58 -07003438static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
3439 unsigned nr_args)
3440{
3441 __s32 __user *fds = (__s32 __user *) arg;
Jens Axboe65e19f52019-10-26 07:20:21 -06003442 unsigned nr_tables;
Jens Axboe6b063142019-01-10 22:13:58 -07003443 int fd, ret = 0;
3444 unsigned i;
3445
Jens Axboe65e19f52019-10-26 07:20:21 -06003446 if (ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003447 return -EBUSY;
3448 if (!nr_args)
3449 return -EINVAL;
3450 if (nr_args > IORING_MAX_FIXED_FILES)
3451 return -EMFILE;
3452
Jens Axboe65e19f52019-10-26 07:20:21 -06003453 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
3454 ctx->file_table = kcalloc(nr_tables, sizeof(struct fixed_file_table),
3455 GFP_KERNEL);
3456 if (!ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003457 return -ENOMEM;
3458
Jens Axboe65e19f52019-10-26 07:20:21 -06003459 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
3460 kfree(ctx->file_table);
3461 return -ENOMEM;
3462 }
3463
Jens Axboe08a45172019-10-03 08:11:03 -06003464 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003465 struct fixed_file_table *table;
3466 unsigned index;
3467
Jens Axboe6b063142019-01-10 22:13:58 -07003468 ret = -EFAULT;
3469 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
3470 break;
Jens Axboe08a45172019-10-03 08:11:03 -06003471 /* allow sparse sets */
3472 if (fd == -1) {
3473 ret = 0;
3474 continue;
3475 }
Jens Axboe6b063142019-01-10 22:13:58 -07003476
Jens Axboe65e19f52019-10-26 07:20:21 -06003477 table = &ctx->file_table[i >> IORING_FILE_TABLE_SHIFT];
3478 index = i & IORING_FILE_TABLE_MASK;
3479 table->files[index] = fget(fd);
Jens Axboe6b063142019-01-10 22:13:58 -07003480
3481 ret = -EBADF;
Jens Axboe65e19f52019-10-26 07:20:21 -06003482 if (!table->files[index])
Jens Axboe6b063142019-01-10 22:13:58 -07003483 break;
3484 /*
3485 * Don't allow io_uring instances to be registered. If UNIX
3486 * isn't enabled, then this causes a reference cycle and this
3487 * instance can never get freed. If UNIX is enabled we'll
3488 * handle it just fine, but there's still no point in allowing
3489 * a ring fd as it doesn't support regular read/write anyway.
3490 */
Jens Axboe65e19f52019-10-26 07:20:21 -06003491 if (table->files[index]->f_op == &io_uring_fops) {
3492 fput(table->files[index]);
Jens Axboe6b063142019-01-10 22:13:58 -07003493 break;
3494 }
Jens Axboe6b063142019-01-10 22:13:58 -07003495 ret = 0;
3496 }
3497
3498 if (ret) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003499 for (i = 0; i < ctx->nr_user_files; i++) {
3500 struct file *file;
Jens Axboe6b063142019-01-10 22:13:58 -07003501
Jens Axboe65e19f52019-10-26 07:20:21 -06003502 file = io_file_from_index(ctx, i);
3503 if (file)
3504 fput(file);
3505 }
3506 for (i = 0; i < nr_tables; i++)
3507 kfree(ctx->file_table[i].files);
3508
3509 kfree(ctx->file_table);
3510 ctx->file_table = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003511 ctx->nr_user_files = 0;
3512 return ret;
3513 }
3514
3515 ret = io_sqe_files_scm(ctx);
3516 if (ret)
3517 io_sqe_files_unregister(ctx);
3518
3519 return ret;
3520}
3521
Jens Axboec3a31e62019-10-03 13:59:56 -06003522static void io_sqe_file_unregister(struct io_ring_ctx *ctx, int index)
3523{
3524#if defined(CONFIG_UNIX)
Jens Axboe65e19f52019-10-26 07:20:21 -06003525 struct file *file = io_file_from_index(ctx, index);
Jens Axboec3a31e62019-10-03 13:59:56 -06003526 struct sock *sock = ctx->ring_sock->sk;
3527 struct sk_buff_head list, *head = &sock->sk_receive_queue;
3528 struct sk_buff *skb;
3529 int i;
3530
3531 __skb_queue_head_init(&list);
3532
3533 /*
3534 * Find the skb that holds this file in its SCM_RIGHTS. When found,
3535 * remove this entry and rearrange the file array.
3536 */
3537 skb = skb_dequeue(head);
3538 while (skb) {
3539 struct scm_fp_list *fp;
3540
3541 fp = UNIXCB(skb).fp;
3542 for (i = 0; i < fp->count; i++) {
3543 int left;
3544
3545 if (fp->fp[i] != file)
3546 continue;
3547
3548 unix_notinflight(fp->user, fp->fp[i]);
3549 left = fp->count - 1 - i;
3550 if (left) {
3551 memmove(&fp->fp[i], &fp->fp[i + 1],
3552 left * sizeof(struct file *));
3553 }
3554 fp->count--;
3555 if (!fp->count) {
3556 kfree_skb(skb);
3557 skb = NULL;
3558 } else {
3559 __skb_queue_tail(&list, skb);
3560 }
3561 fput(file);
3562 file = NULL;
3563 break;
3564 }
3565
3566 if (!file)
3567 break;
3568
3569 __skb_queue_tail(&list, skb);
3570
3571 skb = skb_dequeue(head);
3572 }
3573
3574 if (skb_peek(&list)) {
3575 spin_lock_irq(&head->lock);
3576 while ((skb = __skb_dequeue(&list)) != NULL)
3577 __skb_queue_tail(head, skb);
3578 spin_unlock_irq(&head->lock);
3579 }
3580#else
Jens Axboe65e19f52019-10-26 07:20:21 -06003581 fput(io_file_from_index(ctx, index));
Jens Axboec3a31e62019-10-03 13:59:56 -06003582#endif
3583}
3584
3585static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
3586 int index)
3587{
3588#if defined(CONFIG_UNIX)
3589 struct sock *sock = ctx->ring_sock->sk;
3590 struct sk_buff_head *head = &sock->sk_receive_queue;
3591 struct sk_buff *skb;
3592
3593 /*
3594 * See if we can merge this file into an existing skb SCM_RIGHTS
3595 * file set. If there's no room, fall back to allocating a new skb
3596 * and filling it in.
3597 */
3598 spin_lock_irq(&head->lock);
3599 skb = skb_peek(head);
3600 if (skb) {
3601 struct scm_fp_list *fpl = UNIXCB(skb).fp;
3602
3603 if (fpl->count < SCM_MAX_FD) {
3604 __skb_unlink(skb, head);
3605 spin_unlock_irq(&head->lock);
3606 fpl->fp[fpl->count] = get_file(file);
3607 unix_inflight(fpl->user, fpl->fp[fpl->count]);
3608 fpl->count++;
3609 spin_lock_irq(&head->lock);
3610 __skb_queue_head(head, skb);
3611 } else {
3612 skb = NULL;
3613 }
3614 }
3615 spin_unlock_irq(&head->lock);
3616
3617 if (skb) {
3618 fput(file);
3619 return 0;
3620 }
3621
3622 return __io_sqe_files_scm(ctx, 1, index);
3623#else
3624 return 0;
3625#endif
3626}
3627
3628static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
3629 unsigned nr_args)
3630{
3631 struct io_uring_files_update up;
3632 __s32 __user *fds;
3633 int fd, i, err;
3634 __u32 done;
3635
Jens Axboe65e19f52019-10-26 07:20:21 -06003636 if (!ctx->file_table)
Jens Axboec3a31e62019-10-03 13:59:56 -06003637 return -ENXIO;
3638 if (!nr_args)
3639 return -EINVAL;
3640 if (copy_from_user(&up, arg, sizeof(up)))
3641 return -EFAULT;
3642 if (check_add_overflow(up.offset, nr_args, &done))
3643 return -EOVERFLOW;
3644 if (done > ctx->nr_user_files)
3645 return -EINVAL;
3646
3647 done = 0;
3648 fds = (__s32 __user *) up.fds;
3649 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003650 struct fixed_file_table *table;
3651 unsigned index;
3652
Jens Axboec3a31e62019-10-03 13:59:56 -06003653 err = 0;
3654 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
3655 err = -EFAULT;
3656 break;
3657 }
3658 i = array_index_nospec(up.offset, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06003659 table = &ctx->file_table[i >> IORING_FILE_TABLE_SHIFT];
3660 index = i & IORING_FILE_TABLE_MASK;
3661 if (table->files[index]) {
Jens Axboec3a31e62019-10-03 13:59:56 -06003662 io_sqe_file_unregister(ctx, i);
Jens Axboe65e19f52019-10-26 07:20:21 -06003663 table->files[index] = NULL;
Jens Axboec3a31e62019-10-03 13:59:56 -06003664 }
3665 if (fd != -1) {
3666 struct file *file;
3667
3668 file = fget(fd);
3669 if (!file) {
3670 err = -EBADF;
3671 break;
3672 }
3673 /*
3674 * Don't allow io_uring instances to be registered. If
3675 * UNIX isn't enabled, then this causes a reference
3676 * cycle and this instance can never get freed. If UNIX
3677 * is enabled we'll handle it just fine, but there's
3678 * still no point in allowing a ring fd as it doesn't
3679 * support regular read/write anyway.
3680 */
3681 if (file->f_op == &io_uring_fops) {
3682 fput(file);
3683 err = -EBADF;
3684 break;
3685 }
Jens Axboe65e19f52019-10-26 07:20:21 -06003686 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06003687 err = io_sqe_file_register(ctx, file, i);
3688 if (err)
3689 break;
3690 }
3691 nr_args--;
3692 done++;
3693 up.offset++;
3694 }
3695
3696 return done ? done : err;
3697}
3698
Jens Axboe6c271ce2019-01-10 11:22:30 -07003699static int io_sq_offload_start(struct io_ring_ctx *ctx,
3700 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003701{
Jens Axboe561fb042019-10-24 07:25:42 -06003702 unsigned concurrency;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003703 int ret;
3704
Jens Axboe6c271ce2019-01-10 11:22:30 -07003705 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003706 mmgrab(current->mm);
3707 ctx->sqo_mm = current->mm;
3708
Jens Axboe6c271ce2019-01-10 11:22:30 -07003709 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06003710 ret = -EPERM;
3711 if (!capable(CAP_SYS_ADMIN))
3712 goto err;
3713
Jens Axboe917257d2019-04-13 09:28:55 -06003714 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
3715 if (!ctx->sq_thread_idle)
3716 ctx->sq_thread_idle = HZ;
3717
Jens Axboe6c271ce2019-01-10 11:22:30 -07003718 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06003719 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003720
Jens Axboe917257d2019-04-13 09:28:55 -06003721 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06003722 if (cpu >= nr_cpu_ids)
3723 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08003724 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06003725 goto err;
3726
Jens Axboe6c271ce2019-01-10 11:22:30 -07003727 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
3728 ctx, cpu,
3729 "io_uring-sq");
3730 } else {
3731 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
3732 "io_uring-sq");
3733 }
3734 if (IS_ERR(ctx->sqo_thread)) {
3735 ret = PTR_ERR(ctx->sqo_thread);
3736 ctx->sqo_thread = NULL;
3737 goto err;
3738 }
3739 wake_up_process(ctx->sqo_thread);
3740 } else if (p->flags & IORING_SETUP_SQ_AFF) {
3741 /* Can't have SQ_AFF without SQPOLL */
3742 ret = -EINVAL;
3743 goto err;
3744 }
3745
Jens Axboe561fb042019-10-24 07:25:42 -06003746 /* Do QD, or 4 * CPUS, whatever is smallest */
3747 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
Jens Axboec5def4a2019-11-07 11:41:16 -07003748 ctx->io_wq = io_wq_create(concurrency, ctx->sqo_mm, NULL);
Jens Axboe975c99a52019-10-30 08:42:56 -06003749 if (IS_ERR(ctx->io_wq)) {
3750 ret = PTR_ERR(ctx->io_wq);
3751 ctx->io_wq = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003752 goto err;
3753 }
3754
3755 return 0;
3756err:
Jens Axboe54a91f32019-09-10 09:15:04 -06003757 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003758 mmdrop(ctx->sqo_mm);
3759 ctx->sqo_mm = NULL;
3760 return ret;
3761}
3762
3763static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
3764{
3765 atomic_long_sub(nr_pages, &user->locked_vm);
3766}
3767
3768static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
3769{
3770 unsigned long page_limit, cur_pages, new_pages;
3771
3772 /* Don't allow more pages than we can safely lock */
3773 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
3774
3775 do {
3776 cur_pages = atomic_long_read(&user->locked_vm);
3777 new_pages = cur_pages + nr_pages;
3778 if (new_pages > page_limit)
3779 return -ENOMEM;
3780 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
3781 new_pages) != cur_pages);
3782
3783 return 0;
3784}
3785
3786static void io_mem_free(void *ptr)
3787{
Mark Rutland52e04ef2019-04-30 17:30:21 +01003788 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003789
Mark Rutland52e04ef2019-04-30 17:30:21 +01003790 if (!ptr)
3791 return;
3792
3793 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003794 if (put_page_testzero(page))
3795 free_compound_page(page);
3796}
3797
3798static void *io_mem_alloc(size_t size)
3799{
3800 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
3801 __GFP_NORETRY;
3802
3803 return (void *) __get_free_pages(gfp_flags, get_order(size));
3804}
3805
Hristo Venev75b28af2019-08-26 17:23:46 +00003806static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
3807 size_t *sq_offset)
3808{
3809 struct io_rings *rings;
3810 size_t off, sq_array_size;
3811
3812 off = struct_size(rings, cqes, cq_entries);
3813 if (off == SIZE_MAX)
3814 return SIZE_MAX;
3815
3816#ifdef CONFIG_SMP
3817 off = ALIGN(off, SMP_CACHE_BYTES);
3818 if (off == 0)
3819 return SIZE_MAX;
3820#endif
3821
3822 sq_array_size = array_size(sizeof(u32), sq_entries);
3823 if (sq_array_size == SIZE_MAX)
3824 return SIZE_MAX;
3825
3826 if (check_add_overflow(off, sq_array_size, &off))
3827 return SIZE_MAX;
3828
3829 if (sq_offset)
3830 *sq_offset = off;
3831
3832 return off;
3833}
3834
Jens Axboe2b188cc2019-01-07 10:46:33 -07003835static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
3836{
Hristo Venev75b28af2019-08-26 17:23:46 +00003837 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003838
Hristo Venev75b28af2019-08-26 17:23:46 +00003839 pages = (size_t)1 << get_order(
3840 rings_size(sq_entries, cq_entries, NULL));
3841 pages += (size_t)1 << get_order(
3842 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07003843
Hristo Venev75b28af2019-08-26 17:23:46 +00003844 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003845}
3846
Jens Axboeedafcce2019-01-09 09:16:05 -07003847static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
3848{
3849 int i, j;
3850
3851 if (!ctx->user_bufs)
3852 return -ENXIO;
3853
3854 for (i = 0; i < ctx->nr_user_bufs; i++) {
3855 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
3856
3857 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbard27c4d3a2019-08-04 19:32:06 -07003858 put_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07003859
3860 if (ctx->account_mem)
3861 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003862 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07003863 imu->nr_bvecs = 0;
3864 }
3865
3866 kfree(ctx->user_bufs);
3867 ctx->user_bufs = NULL;
3868 ctx->nr_user_bufs = 0;
3869 return 0;
3870}
3871
3872static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
3873 void __user *arg, unsigned index)
3874{
3875 struct iovec __user *src;
3876
3877#ifdef CONFIG_COMPAT
3878 if (ctx->compat) {
3879 struct compat_iovec __user *ciovs;
3880 struct compat_iovec ciov;
3881
3882 ciovs = (struct compat_iovec __user *) arg;
3883 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
3884 return -EFAULT;
3885
3886 dst->iov_base = (void __user *) (unsigned long) ciov.iov_base;
3887 dst->iov_len = ciov.iov_len;
3888 return 0;
3889 }
3890#endif
3891 src = (struct iovec __user *) arg;
3892 if (copy_from_user(dst, &src[index], sizeof(*dst)))
3893 return -EFAULT;
3894 return 0;
3895}
3896
3897static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
3898 unsigned nr_args)
3899{
3900 struct vm_area_struct **vmas = NULL;
3901 struct page **pages = NULL;
3902 int i, j, got_pages = 0;
3903 int ret = -EINVAL;
3904
3905 if (ctx->user_bufs)
3906 return -EBUSY;
3907 if (!nr_args || nr_args > UIO_MAXIOV)
3908 return -EINVAL;
3909
3910 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
3911 GFP_KERNEL);
3912 if (!ctx->user_bufs)
3913 return -ENOMEM;
3914
3915 for (i = 0; i < nr_args; i++) {
3916 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
3917 unsigned long off, start, end, ubuf;
3918 int pret, nr_pages;
3919 struct iovec iov;
3920 size_t size;
3921
3922 ret = io_copy_iov(ctx, &iov, arg, i);
3923 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03003924 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07003925
3926 /*
3927 * Don't impose further limits on the size and buffer
3928 * constraints here, we'll -EINVAL later when IO is
3929 * submitted if they are wrong.
3930 */
3931 ret = -EFAULT;
3932 if (!iov.iov_base || !iov.iov_len)
3933 goto err;
3934
3935 /* arbitrary limit, but we need something */
3936 if (iov.iov_len > SZ_1G)
3937 goto err;
3938
3939 ubuf = (unsigned long) iov.iov_base;
3940 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
3941 start = ubuf >> PAGE_SHIFT;
3942 nr_pages = end - start;
3943
3944 if (ctx->account_mem) {
3945 ret = io_account_mem(ctx->user, nr_pages);
3946 if (ret)
3947 goto err;
3948 }
3949
3950 ret = 0;
3951 if (!pages || nr_pages > got_pages) {
3952 kfree(vmas);
3953 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003954 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07003955 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003956 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07003957 sizeof(struct vm_area_struct *),
3958 GFP_KERNEL);
3959 if (!pages || !vmas) {
3960 ret = -ENOMEM;
3961 if (ctx->account_mem)
3962 io_unaccount_mem(ctx->user, nr_pages);
3963 goto err;
3964 }
3965 got_pages = nr_pages;
3966 }
3967
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003968 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07003969 GFP_KERNEL);
3970 ret = -ENOMEM;
3971 if (!imu->bvec) {
3972 if (ctx->account_mem)
3973 io_unaccount_mem(ctx->user, nr_pages);
3974 goto err;
3975 }
3976
3977 ret = 0;
3978 down_read(&current->mm->mmap_sem);
Ira Weiny932f4a62019-05-13 17:17:03 -07003979 pret = get_user_pages(ubuf, nr_pages,
3980 FOLL_WRITE | FOLL_LONGTERM,
3981 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07003982 if (pret == nr_pages) {
3983 /* don't support file backed memory */
3984 for (j = 0; j < nr_pages; j++) {
3985 struct vm_area_struct *vma = vmas[j];
3986
3987 if (vma->vm_file &&
3988 !is_file_hugepages(vma->vm_file)) {
3989 ret = -EOPNOTSUPP;
3990 break;
3991 }
3992 }
3993 } else {
3994 ret = pret < 0 ? pret : -EFAULT;
3995 }
3996 up_read(&current->mm->mmap_sem);
3997 if (ret) {
3998 /*
3999 * if we did partial map, or found file backed vmas,
4000 * release any pages we did get
4001 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07004002 if (pret > 0)
4003 put_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07004004 if (ctx->account_mem)
4005 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004006 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07004007 goto err;
4008 }
4009
4010 off = ubuf & ~PAGE_MASK;
4011 size = iov.iov_len;
4012 for (j = 0; j < nr_pages; j++) {
4013 size_t vec_len;
4014
4015 vec_len = min_t(size_t, size, PAGE_SIZE - off);
4016 imu->bvec[j].bv_page = pages[j];
4017 imu->bvec[j].bv_len = vec_len;
4018 imu->bvec[j].bv_offset = off;
4019 off = 0;
4020 size -= vec_len;
4021 }
4022 /* store original address for later verification */
4023 imu->ubuf = ubuf;
4024 imu->len = iov.iov_len;
4025 imu->nr_bvecs = nr_pages;
4026
4027 ctx->nr_user_bufs++;
4028 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004029 kvfree(pages);
4030 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07004031 return 0;
4032err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004033 kvfree(pages);
4034 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07004035 io_sqe_buffer_unregister(ctx);
4036 return ret;
4037}
4038
Jens Axboe9b402842019-04-11 11:45:41 -06004039static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
4040{
4041 __s32 __user *fds = arg;
4042 int fd;
4043
4044 if (ctx->cq_ev_fd)
4045 return -EBUSY;
4046
4047 if (copy_from_user(&fd, fds, sizeof(*fds)))
4048 return -EFAULT;
4049
4050 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
4051 if (IS_ERR(ctx->cq_ev_fd)) {
4052 int ret = PTR_ERR(ctx->cq_ev_fd);
4053 ctx->cq_ev_fd = NULL;
4054 return ret;
4055 }
4056
4057 return 0;
4058}
4059
4060static int io_eventfd_unregister(struct io_ring_ctx *ctx)
4061{
4062 if (ctx->cq_ev_fd) {
4063 eventfd_ctx_put(ctx->cq_ev_fd);
4064 ctx->cq_ev_fd = NULL;
4065 return 0;
4066 }
4067
4068 return -ENXIO;
4069}
4070
Jens Axboe2b188cc2019-01-07 10:46:33 -07004071static void io_ring_ctx_free(struct io_ring_ctx *ctx)
4072{
Jens Axboe6b063142019-01-10 22:13:58 -07004073 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004074 if (ctx->sqo_mm)
4075 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07004076
4077 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07004078 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07004079 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06004080 io_eventfd_unregister(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07004081
Jens Axboe2b188cc2019-01-07 10:46:33 -07004082#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07004083 if (ctx->ring_sock) {
4084 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004085 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07004086 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004087#endif
4088
Hristo Venev75b28af2019-08-26 17:23:46 +00004089 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004090 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004091
4092 percpu_ref_exit(&ctx->refs);
4093 if (ctx->account_mem)
4094 io_unaccount_mem(ctx->user,
4095 ring_pages(ctx->sq_entries, ctx->cq_entries));
4096 free_uid(ctx->user);
4097 kfree(ctx);
4098}
4099
4100static __poll_t io_uring_poll(struct file *file, poll_table *wait)
4101{
4102 struct io_ring_ctx *ctx = file->private_data;
4103 __poll_t mask = 0;
4104
4105 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02004106 /*
4107 * synchronizes with barrier from wq_has_sleeper call in
4108 * io_commit_cqring
4109 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004110 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00004111 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
4112 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004113 mask |= EPOLLOUT | EPOLLWRNORM;
yangerkundaa5de52019-09-24 20:53:34 +08004114 if (READ_ONCE(ctx->rings->cq.head) != ctx->cached_cq_tail)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004115 mask |= EPOLLIN | EPOLLRDNORM;
4116
4117 return mask;
4118}
4119
4120static int io_uring_fasync(int fd, struct file *file, int on)
4121{
4122 struct io_ring_ctx *ctx = file->private_data;
4123
4124 return fasync_helper(fd, file, on, &ctx->cq_fasync);
4125}
4126
4127static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
4128{
4129 mutex_lock(&ctx->uring_lock);
4130 percpu_ref_kill(&ctx->refs);
4131 mutex_unlock(&ctx->uring_lock);
4132
Jens Axboe5262f562019-09-17 12:26:57 -06004133 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004134 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06004135
4136 if (ctx->io_wq)
4137 io_wq_cancel_all(ctx->io_wq);
4138
Jens Axboedef596e2019-01-09 08:59:42 -07004139 io_iopoll_reap_events(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004140 io_cqring_overflow_flush(ctx, true);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004141 wait_for_completion(&ctx->ctx_done);
4142 io_ring_ctx_free(ctx);
4143}
4144
4145static int io_uring_release(struct inode *inode, struct file *file)
4146{
4147 struct io_ring_ctx *ctx = file->private_data;
4148
4149 file->private_data = NULL;
4150 io_ring_ctx_wait_and_kill(ctx);
4151 return 0;
4152}
4153
Jens Axboefcb323c2019-10-24 12:39:47 -06004154static void io_uring_cancel_files(struct io_ring_ctx *ctx,
4155 struct files_struct *files)
4156{
4157 struct io_kiocb *req;
4158 DEFINE_WAIT(wait);
4159
4160 while (!list_empty_careful(&ctx->inflight_list)) {
4161 enum io_wq_cancel ret = IO_WQ_CANCEL_NOTFOUND;
4162
4163 spin_lock_irq(&ctx->inflight_lock);
4164 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
4165 if (req->work.files == files) {
4166 ret = io_wq_cancel_work(ctx->io_wq, &req->work);
4167 break;
4168 }
4169 }
4170 if (ret == IO_WQ_CANCEL_RUNNING)
4171 prepare_to_wait(&ctx->inflight_wait, &wait,
4172 TASK_UNINTERRUPTIBLE);
4173
4174 spin_unlock_irq(&ctx->inflight_lock);
4175
4176 /*
4177 * We need to keep going until we get NOTFOUND. We only cancel
4178 * one work at the time.
4179 *
4180 * If we get CANCEL_RUNNING, then wait for a work to complete
4181 * before continuing.
4182 */
4183 if (ret == IO_WQ_CANCEL_OK)
4184 continue;
4185 else if (ret != IO_WQ_CANCEL_RUNNING)
4186 break;
4187 schedule();
4188 }
4189}
4190
4191static int io_uring_flush(struct file *file, void *data)
4192{
4193 struct io_ring_ctx *ctx = file->private_data;
4194
4195 io_uring_cancel_files(ctx, data);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004196 if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) {
4197 io_cqring_overflow_flush(ctx, true);
Jens Axboefcb323c2019-10-24 12:39:47 -06004198 io_wq_cancel_all(ctx->io_wq);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004199 }
Jens Axboefcb323c2019-10-24 12:39:47 -06004200 return 0;
4201}
4202
Jens Axboe2b188cc2019-01-07 10:46:33 -07004203static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
4204{
4205 loff_t offset = (loff_t) vma->vm_pgoff << PAGE_SHIFT;
4206 unsigned long sz = vma->vm_end - vma->vm_start;
4207 struct io_ring_ctx *ctx = file->private_data;
4208 unsigned long pfn;
4209 struct page *page;
4210 void *ptr;
4211
4212 switch (offset) {
4213 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00004214 case IORING_OFF_CQ_RING:
4215 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004216 break;
4217 case IORING_OFF_SQES:
4218 ptr = ctx->sq_sqes;
4219 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004220 default:
4221 return -EINVAL;
4222 }
4223
4224 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07004225 if (sz > page_size(page))
Jens Axboe2b188cc2019-01-07 10:46:33 -07004226 return -EINVAL;
4227
4228 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
4229 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
4230}
4231
4232SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
4233 u32, min_complete, u32, flags, const sigset_t __user *, sig,
4234 size_t, sigsz)
4235{
4236 struct io_ring_ctx *ctx;
4237 long ret = -EBADF;
4238 int submitted = 0;
4239 struct fd f;
4240
Jens Axboe6c271ce2019-01-10 11:22:30 -07004241 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07004242 return -EINVAL;
4243
4244 f = fdget(fd);
4245 if (!f.file)
4246 return -EBADF;
4247
4248 ret = -EOPNOTSUPP;
4249 if (f.file->f_op != &io_uring_fops)
4250 goto out_fput;
4251
4252 ret = -ENXIO;
4253 ctx = f.file->private_data;
4254 if (!percpu_ref_tryget(&ctx->refs))
4255 goto out_fput;
4256
Jens Axboe6c271ce2019-01-10 11:22:30 -07004257 /*
4258 * For SQ polling, the thread will do all submissions and completions.
4259 * Just return the requested submit count, and wake the thread if
4260 * we were asked to.
4261 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06004262 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004263 if (ctx->flags & IORING_SETUP_SQPOLL) {
4264 if (flags & IORING_ENTER_SQ_WAKEUP)
4265 wake_up(&ctx->sqo_wait);
4266 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06004267 } else if (to_submit) {
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004268 struct mm_struct *cur_mm;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004269
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004270 to_submit = min(to_submit, ctx->sq_entries);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004271 mutex_lock(&ctx->uring_lock);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004272 /* already have mm, so io_submit_sqes() won't try to grab it */
4273 cur_mm = ctx->sqo_mm;
4274 submitted = io_submit_sqes(ctx, to_submit, f.file, fd,
4275 &cur_mm, false);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004276 mutex_unlock(&ctx->uring_lock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004277 }
4278 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07004279 unsigned nr_events = 0;
4280
Jens Axboe2b188cc2019-01-07 10:46:33 -07004281 min_complete = min(min_complete, ctx->cq_entries);
4282
Jens Axboedef596e2019-01-09 08:59:42 -07004283 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07004284 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07004285 } else {
4286 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
4287 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004288 }
4289
Pavel Begunkov6805b322019-10-08 02:18:42 +03004290 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004291out_fput:
4292 fdput(f);
4293 return submitted ? submitted : ret;
4294}
4295
4296static const struct file_operations io_uring_fops = {
4297 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06004298 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07004299 .mmap = io_uring_mmap,
4300 .poll = io_uring_poll,
4301 .fasync = io_uring_fasync,
4302};
4303
4304static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
4305 struct io_uring_params *p)
4306{
Hristo Venev75b28af2019-08-26 17:23:46 +00004307 struct io_rings *rings;
4308 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004309
Hristo Venev75b28af2019-08-26 17:23:46 +00004310 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
4311 if (size == SIZE_MAX)
4312 return -EOVERFLOW;
4313
4314 rings = io_mem_alloc(size);
4315 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004316 return -ENOMEM;
4317
Hristo Venev75b28af2019-08-26 17:23:46 +00004318 ctx->rings = rings;
4319 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
4320 rings->sq_ring_mask = p->sq_entries - 1;
4321 rings->cq_ring_mask = p->cq_entries - 1;
4322 rings->sq_ring_entries = p->sq_entries;
4323 rings->cq_ring_entries = p->cq_entries;
4324 ctx->sq_mask = rings->sq_ring_mask;
4325 ctx->cq_mask = rings->cq_ring_mask;
4326 ctx->sq_entries = rings->sq_ring_entries;
4327 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004328
4329 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
4330 if (size == SIZE_MAX)
4331 return -EOVERFLOW;
4332
4333 ctx->sq_sqes = io_mem_alloc(size);
Mark Rutland52e04ef2019-04-30 17:30:21 +01004334 if (!ctx->sq_sqes)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004335 return -ENOMEM;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004336
Jens Axboe2b188cc2019-01-07 10:46:33 -07004337 return 0;
4338}
4339
4340/*
4341 * Allocate an anonymous fd, this is what constitutes the application
4342 * visible backing of an io_uring instance. The application mmaps this
4343 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
4344 * we have to tie this fd to a socket for file garbage collection purposes.
4345 */
4346static int io_uring_get_fd(struct io_ring_ctx *ctx)
4347{
4348 struct file *file;
4349 int ret;
4350
4351#if defined(CONFIG_UNIX)
4352 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
4353 &ctx->ring_sock);
4354 if (ret)
4355 return ret;
4356#endif
4357
4358 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
4359 if (ret < 0)
4360 goto err;
4361
4362 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
4363 O_RDWR | O_CLOEXEC);
4364 if (IS_ERR(file)) {
4365 put_unused_fd(ret);
4366 ret = PTR_ERR(file);
4367 goto err;
4368 }
4369
4370#if defined(CONFIG_UNIX)
4371 ctx->ring_sock->file = file;
Jens Axboe6b063142019-01-10 22:13:58 -07004372 ctx->ring_sock->sk->sk_user_data = ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004373#endif
4374 fd_install(ret, file);
4375 return ret;
4376err:
4377#if defined(CONFIG_UNIX)
4378 sock_release(ctx->ring_sock);
4379 ctx->ring_sock = NULL;
4380#endif
4381 return ret;
4382}
4383
4384static int io_uring_create(unsigned entries, struct io_uring_params *p)
4385{
4386 struct user_struct *user = NULL;
4387 struct io_ring_ctx *ctx;
4388 bool account_mem;
4389 int ret;
4390
4391 if (!entries || entries > IORING_MAX_ENTRIES)
4392 return -EINVAL;
4393
4394 /*
4395 * Use twice as many entries for the CQ ring. It's possible for the
4396 * application to drive a higher depth than the size of the SQ ring,
4397 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06004398 * some flexibility in overcommitting a bit. If the application has
4399 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
4400 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07004401 */
4402 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06004403 if (p->flags & IORING_SETUP_CQSIZE) {
4404 /*
4405 * If IORING_SETUP_CQSIZE is set, we do the same roundup
4406 * to a power-of-two, if it isn't already. We do NOT impose
4407 * any cq vs sq ring sizing.
4408 */
4409 if (p->cq_entries < p->sq_entries || p->cq_entries > IORING_MAX_CQ_ENTRIES)
4410 return -EINVAL;
4411 p->cq_entries = roundup_pow_of_two(p->cq_entries);
4412 } else {
4413 p->cq_entries = 2 * p->sq_entries;
4414 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004415
4416 user = get_uid(current_user());
4417 account_mem = !capable(CAP_IPC_LOCK);
4418
4419 if (account_mem) {
4420 ret = io_account_mem(user,
4421 ring_pages(p->sq_entries, p->cq_entries));
4422 if (ret) {
4423 free_uid(user);
4424 return ret;
4425 }
4426 }
4427
4428 ctx = io_ring_ctx_alloc(p);
4429 if (!ctx) {
4430 if (account_mem)
4431 io_unaccount_mem(user, ring_pages(p->sq_entries,
4432 p->cq_entries));
4433 free_uid(user);
4434 return -ENOMEM;
4435 }
4436 ctx->compat = in_compat_syscall();
4437 ctx->account_mem = account_mem;
4438 ctx->user = user;
4439
4440 ret = io_allocate_scq_urings(ctx, p);
4441 if (ret)
4442 goto err;
4443
Jens Axboe6c271ce2019-01-10 11:22:30 -07004444 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004445 if (ret)
4446 goto err;
4447
Jens Axboe2b188cc2019-01-07 10:46:33 -07004448 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00004449 p->sq_off.head = offsetof(struct io_rings, sq.head);
4450 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
4451 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
4452 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
4453 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
4454 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
4455 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004456
4457 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00004458 p->cq_off.head = offsetof(struct io_rings, cq.head);
4459 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
4460 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
4461 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
4462 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
4463 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06004464
Jens Axboe044c1ab2019-10-28 09:15:33 -06004465 /*
4466 * Install ring fd as the very last thing, so we don't risk someone
4467 * having closed it before we finish setup
4468 */
4469 ret = io_uring_get_fd(ctx);
4470 if (ret < 0)
4471 goto err;
4472
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004473 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004474 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004475 return ret;
4476err:
4477 io_ring_ctx_wait_and_kill(ctx);
4478 return ret;
4479}
4480
4481/*
4482 * Sets up an aio uring context, and returns the fd. Applications asks for a
4483 * ring size, we return the actual sq/cq ring sizes (among other things) in the
4484 * params structure passed in.
4485 */
4486static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
4487{
4488 struct io_uring_params p;
4489 long ret;
4490 int i;
4491
4492 if (copy_from_user(&p, params, sizeof(p)))
4493 return -EFAULT;
4494 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
4495 if (p.resv[i])
4496 return -EINVAL;
4497 }
4498
Jens Axboe6c271ce2019-01-10 11:22:30 -07004499 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe33a107f2019-10-04 12:10:03 -06004500 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE))
Jens Axboe2b188cc2019-01-07 10:46:33 -07004501 return -EINVAL;
4502
4503 ret = io_uring_create(entries, &p);
4504 if (ret < 0)
4505 return ret;
4506
4507 if (copy_to_user(params, &p, sizeof(p)))
4508 return -EFAULT;
4509
4510 return ret;
4511}
4512
4513SYSCALL_DEFINE2(io_uring_setup, u32, entries,
4514 struct io_uring_params __user *, params)
4515{
4516 return io_uring_setup(entries, params);
4517}
4518
Jens Axboeedafcce2019-01-09 09:16:05 -07004519static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
4520 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06004521 __releases(ctx->uring_lock)
4522 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07004523{
4524 int ret;
4525
Jens Axboe35fa71a2019-04-22 10:23:23 -06004526 /*
4527 * We're inside the ring mutex, if the ref is already dying, then
4528 * someone else killed the ctx or is already going through
4529 * io_uring_register().
4530 */
4531 if (percpu_ref_is_dying(&ctx->refs))
4532 return -ENXIO;
4533
Jens Axboeedafcce2019-01-09 09:16:05 -07004534 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06004535
4536 /*
4537 * Drop uring mutex before waiting for references to exit. If another
4538 * thread is currently inside io_uring_enter() it might need to grab
4539 * the uring_lock to make progress. If we hold it here across the drain
4540 * wait, then we can deadlock. It's safe to drop the mutex here, since
4541 * no new references will come in after we've killed the percpu ref.
4542 */
4543 mutex_unlock(&ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07004544 wait_for_completion(&ctx->ctx_done);
Jens Axboeb19062a2019-04-15 10:49:38 -06004545 mutex_lock(&ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07004546
4547 switch (opcode) {
4548 case IORING_REGISTER_BUFFERS:
4549 ret = io_sqe_buffer_register(ctx, arg, nr_args);
4550 break;
4551 case IORING_UNREGISTER_BUFFERS:
4552 ret = -EINVAL;
4553 if (arg || nr_args)
4554 break;
4555 ret = io_sqe_buffer_unregister(ctx);
4556 break;
Jens Axboe6b063142019-01-10 22:13:58 -07004557 case IORING_REGISTER_FILES:
4558 ret = io_sqe_files_register(ctx, arg, nr_args);
4559 break;
4560 case IORING_UNREGISTER_FILES:
4561 ret = -EINVAL;
4562 if (arg || nr_args)
4563 break;
4564 ret = io_sqe_files_unregister(ctx);
4565 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06004566 case IORING_REGISTER_FILES_UPDATE:
4567 ret = io_sqe_files_update(ctx, arg, nr_args);
4568 break;
Jens Axboe9b402842019-04-11 11:45:41 -06004569 case IORING_REGISTER_EVENTFD:
4570 ret = -EINVAL;
4571 if (nr_args != 1)
4572 break;
4573 ret = io_eventfd_register(ctx, arg);
4574 break;
4575 case IORING_UNREGISTER_EVENTFD:
4576 ret = -EINVAL;
4577 if (arg || nr_args)
4578 break;
4579 ret = io_eventfd_unregister(ctx);
4580 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07004581 default:
4582 ret = -EINVAL;
4583 break;
4584 }
4585
4586 /* bring the ctx back to life */
4587 reinit_completion(&ctx->ctx_done);
4588 percpu_ref_reinit(&ctx->refs);
4589 return ret;
4590}
4591
4592SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
4593 void __user *, arg, unsigned int, nr_args)
4594{
4595 struct io_ring_ctx *ctx;
4596 long ret = -EBADF;
4597 struct fd f;
4598
4599 f = fdget(fd);
4600 if (!f.file)
4601 return -EBADF;
4602
4603 ret = -EOPNOTSUPP;
4604 if (f.file->f_op != &io_uring_fops)
4605 goto out_fput;
4606
4607 ctx = f.file->private_data;
4608
4609 mutex_lock(&ctx->uring_lock);
4610 ret = __io_uring_register(ctx, opcode, arg, nr_args);
4611 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004612 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
4613 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07004614out_fput:
4615 fdput(f);
4616 return ret;
4617}
4618
Jens Axboe2b188cc2019-01-07 10:46:33 -07004619static int __init io_uring_init(void)
4620{
4621 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
4622 return 0;
4623};
4624__initcall(io_uring_init);