blob: 7743b180a3e0a0a206a39840d3d5e2ea726a500a [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 Axboe6b063142019-01-10 22:13:58 -070083#define IORING_MAX_FIXED_FILES 1024
Jens Axboe2b188cc2019-01-07 10:46:33 -070084
85struct io_uring {
86 u32 head ____cacheline_aligned_in_smp;
87 u32 tail ____cacheline_aligned_in_smp;
88};
89
Stefan Bühler1e84b972019-04-24 23:54:16 +020090/*
Hristo Venev75b28af2019-08-26 17:23:46 +000091 * This data is shared with the application through the mmap at offsets
92 * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING.
Stefan Bühler1e84b972019-04-24 23:54:16 +020093 *
94 * The offsets to the member fields are published through struct
95 * io_sqring_offsets when calling io_uring_setup.
96 */
Hristo Venev75b28af2019-08-26 17:23:46 +000097struct io_rings {
Stefan Bühler1e84b972019-04-24 23:54:16 +020098 /*
99 * Head and tail offsets into the ring; the offsets need to be
100 * masked to get valid indices.
101 *
Hristo Venev75b28af2019-08-26 17:23:46 +0000102 * The kernel controls head of the sq ring and the tail of the cq ring,
103 * and the application controls tail of the sq ring and the head of the
104 * cq ring.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200105 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000106 struct io_uring sq, cq;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200107 /*
Hristo Venev75b28af2019-08-26 17:23:46 +0000108 * Bitmasks to apply to head and tail offsets (constant, equals
Stefan Bühler1e84b972019-04-24 23:54:16 +0200109 * ring_entries - 1)
110 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000111 u32 sq_ring_mask, cq_ring_mask;
112 /* Ring sizes (constant, power of 2) */
113 u32 sq_ring_entries, cq_ring_entries;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200114 /*
115 * Number of invalid entries dropped by the kernel due to
116 * invalid index stored in array
117 *
118 * Written by the kernel, shouldn't be modified by the
119 * application (i.e. get number of "new events" by comparing to
120 * cached value).
121 *
122 * After a new SQ head value was read by the application this
123 * counter includes all submissions that were dropped reaching
124 * the new SQ head (and possibly more).
125 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000126 u32 sq_dropped;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200127 /*
128 * Runtime flags
129 *
130 * Written by the kernel, shouldn't be modified by the
131 * application.
132 *
133 * The application needs a full memory barrier before checking
134 * for IORING_SQ_NEED_WAKEUP after updating the sq tail.
135 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000136 u32 sq_flags;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200137 /*
138 * Number of completion events lost because the queue was full;
139 * this should be avoided by the application by making sure
140 * there are not more requests pending thatn there is space in
141 * the completion queue.
142 *
143 * Written by the kernel, shouldn't be modified by the
144 * application (i.e. get number of "new events" by comparing to
145 * cached value).
146 *
147 * As completion events come in out of order this counter is not
148 * ordered with any other data.
149 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000150 u32 cq_overflow;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200151 /*
152 * Ring buffer of completion events.
153 *
154 * The kernel writes completion events fresh every time they are
155 * produced, so the application is allowed to modify pending
156 * entries.
157 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000158 struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700159};
160
Jens Axboeedafcce2019-01-09 09:16:05 -0700161struct io_mapped_ubuf {
162 u64 ubuf;
163 size_t len;
164 struct bio_vec *bvec;
165 unsigned int nr_bvecs;
166};
167
Jens Axboe2b188cc2019-01-07 10:46:33 -0700168struct io_ring_ctx {
169 struct {
170 struct percpu_ref refs;
171 } ____cacheline_aligned_in_smp;
172
173 struct {
174 unsigned int flags;
175 bool compat;
176 bool account_mem;
177
Hristo Venev75b28af2019-08-26 17:23:46 +0000178 /*
179 * Ring buffer of indices into array of io_uring_sqe, which is
180 * mmapped by the application using the IORING_OFF_SQES offset.
181 *
182 * This indirection could e.g. be used to assign fixed
183 * io_uring_sqe entries to operations and only submit them to
184 * the queue when needed.
185 *
186 * The kernel modifies neither the indices array nor the entries
187 * array.
188 */
189 u32 *sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700190 unsigned cached_sq_head;
191 unsigned sq_entries;
192 unsigned sq_mask;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700193 unsigned sq_thread_idle;
Jens Axboe498ccd92019-10-25 10:04:25 -0600194 unsigned cached_sq_dropped;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700195 struct io_uring_sqe *sq_sqes;
Jens Axboede0617e2019-04-06 21:51:27 -0600196
197 struct list_head defer_list;
Jens Axboe5262f562019-09-17 12:26:57 -0600198 struct list_head timeout_list;
Jens Axboefcb323c2019-10-24 12:39:47 -0600199
200 wait_queue_head_t inflight_wait;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700201 } ____cacheline_aligned_in_smp;
202
203 /* IO offload */
Jens Axboe561fb042019-10-24 07:25:42 -0600204 struct io_wq *io_wq;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700205 struct task_struct *sqo_thread; /* if using sq thread polling */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700206 struct mm_struct *sqo_mm;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700207 wait_queue_head_t sqo_wait;
Jackie Liua4c0b3d2019-07-08 13:41:12 +0800208 struct completion sqo_thread_started;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700209
210 struct {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700211 unsigned cached_cq_tail;
Jens Axboe498ccd92019-10-25 10:04:25 -0600212 atomic_t cached_cq_overflow;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700213 unsigned cq_entries;
214 unsigned cq_mask;
215 struct wait_queue_head cq_wait;
216 struct fasync_struct *cq_fasync;
Jens Axboe9b402842019-04-11 11:45:41 -0600217 struct eventfd_ctx *cq_ev_fd;
Jens Axboe5262f562019-09-17 12:26:57 -0600218 atomic_t cq_timeouts;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700219 } ____cacheline_aligned_in_smp;
220
Hristo Venev75b28af2019-08-26 17:23:46 +0000221 struct io_rings *rings;
222
Jens Axboe6b063142019-01-10 22:13:58 -0700223 /*
224 * If used, fixed file set. Writers must ensure that ->refs is dead,
225 * readers must ensure that ->refs is alive as long as the file* is
226 * used. Only updated through io_uring_register(2).
227 */
228 struct file **user_files;
229 unsigned nr_user_files;
230
Jens Axboeedafcce2019-01-09 09:16:05 -0700231 /* if used, fixed mapped user buffers */
232 unsigned nr_user_bufs;
233 struct io_mapped_ubuf *user_bufs;
234
Jens Axboe2b188cc2019-01-07 10:46:33 -0700235 struct user_struct *user;
236
237 struct completion ctx_done;
238
239 struct {
240 struct mutex uring_lock;
241 wait_queue_head_t wait;
242 } ____cacheline_aligned_in_smp;
243
244 struct {
245 spinlock_t completion_lock;
Jens Axboedef596e2019-01-09 08:59:42 -0700246 bool poll_multi_file;
247 /*
248 * ->poll_list is protected by the ctx->uring_lock for
249 * io_uring instances that don't use IORING_SETUP_SQPOLL.
250 * For SQPOLL, only the single threaded io_sq_thread() will
251 * manipulate the list, hence no extra locking is needed there.
252 */
253 struct list_head poll_list;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700254 struct list_head cancel_list;
Jens Axboefcb323c2019-10-24 12:39:47 -0600255
256 spinlock_t inflight_lock;
257 struct list_head inflight_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700258 } ____cacheline_aligned_in_smp;
259
260#if defined(CONFIG_UNIX)
261 struct socket *ring_sock;
262#endif
263};
264
265struct sqe_submit {
266 const struct io_uring_sqe *sqe;
Jens Axboefcb323c2019-10-24 12:39:47 -0600267 struct file *ring_file;
268 int ring_fd;
Jackie Liu8776f3f2019-09-09 20:50:39 +0800269 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700270 bool has_user;
Jackie Liuba5290c2019-10-09 09:19:59 +0800271 bool in_async;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700272 bool needs_fixed_file;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700273};
274
Jens Axboe09bb8392019-03-13 12:39:28 -0600275/*
276 * First field must be the file pointer in all the
277 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
278 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700279struct io_poll_iocb {
280 struct file *file;
281 struct wait_queue_head *head;
282 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600283 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700284 bool canceled;
285 struct wait_queue_entry wait;
286};
287
Jens Axboe5262f562019-09-17 12:26:57 -0600288struct io_timeout {
289 struct file *file;
290 struct hrtimer timer;
291};
292
Jens Axboe09bb8392019-03-13 12:39:28 -0600293/*
294 * NOTE! Each of the iocb union members has the file pointer
295 * as the first entry in their struct definition. So you can
296 * access the file pointer through any of the sub-structs,
297 * or directly as just 'ki_filp' in this struct.
298 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700299struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700300 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600301 struct file *file;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700302 struct kiocb rw;
303 struct io_poll_iocb poll;
Jens Axboe5262f562019-09-17 12:26:57 -0600304 struct io_timeout timeout;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700305 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700306
307 struct sqe_submit submit;
308
309 struct io_ring_ctx *ctx;
310 struct list_head list;
Jens Axboe9e645e112019-05-10 16:07:28 -0600311 struct list_head link_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700312 unsigned int flags;
Jens Axboec16361c2019-01-17 08:39:48 -0700313 refcount_t refs;
Stefan Bühler8449eed2019-04-27 20:34:19 +0200314#define REQ_F_NOWAIT 1 /* must not punt to workers */
Jens Axboedef596e2019-01-09 08:59:42 -0700315#define REQ_F_IOPOLL_COMPLETED 2 /* polled IO has completed */
Jens Axboe6b063142019-01-10 22:13:58 -0700316#define REQ_F_FIXED_FILE 4 /* ctx owns file */
Jens Axboe31b51512019-01-18 22:56:34 -0700317#define REQ_F_SEQ_PREV 8 /* sequential with previous */
Stefan Bühlere2033e32019-05-11 19:08:01 +0200318#define REQ_F_IO_DRAIN 16 /* drain existing IO first */
319#define REQ_F_IO_DRAINED 32 /* drain done */
Jens Axboe9e645e112019-05-10 16:07:28 -0600320#define REQ_F_LINK 64 /* linked sqes */
Zhengyuan Liuf7b76ac2019-07-16 23:26:14 +0800321#define REQ_F_LINK_DONE 128 /* linked sqes done */
322#define REQ_F_FAIL_LINK 256 /* fail rest of links */
Jackie Liu4fe2c962019-09-09 20:50:40 +0800323#define REQ_F_SHADOW_DRAIN 512 /* link-drain shadow req */
Jens Axboe5262f562019-09-17 12:26:57 -0600324#define REQ_F_TIMEOUT 1024 /* timeout request */
Jens Axboe491381ce2019-10-17 09:20:46 -0600325#define REQ_F_ISREG 2048 /* regular file */
326#define REQ_F_MUST_PUNT 4096 /* must be punted even for NONBLOCK */
Jens Axboefcb323c2019-10-24 12:39:47 -0600327#define REQ_F_INFLIGHT 8192 /* on inflight list */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700328 u64 user_data;
Jens Axboe9e645e112019-05-10 16:07:28 -0600329 u32 result;
Jens Axboede0617e2019-04-06 21:51:27 -0600330 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700331
Jens Axboefcb323c2019-10-24 12:39:47 -0600332 struct list_head inflight_entry;
333
Jens Axboe561fb042019-10-24 07:25:42 -0600334 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700335};
336
337#define IO_PLUG_THRESHOLD 2
Jens Axboedef596e2019-01-09 08:59:42 -0700338#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700339
Jens Axboe9a56a232019-01-09 09:06:50 -0700340struct io_submit_state {
341 struct blk_plug plug;
342
343 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700344 * io_kiocb alloc cache
345 */
346 void *reqs[IO_IOPOLL_BATCH];
347 unsigned int free_reqs;
348 unsigned int cur_req;
349
350 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700351 * File reference cache
352 */
353 struct file *file;
354 unsigned int fd;
355 unsigned int has_refs;
356 unsigned int used_refs;
357 unsigned int ios_left;
358};
359
Jens Axboe561fb042019-10-24 07:25:42 -0600360static void io_wq_submit_work(struct io_wq_work **workptr);
Jens Axboe5262f562019-09-17 12:26:57 -0600361static void io_cqring_fill_event(struct io_ring_ctx *ctx, u64 ki_user_data,
362 long res);
Jackie Liu4fe2c962019-09-09 20:50:40 +0800363static void __io_free_req(struct io_kiocb *req);
Jens Axboede0617e2019-04-06 21:51:27 -0600364
Jens Axboe2b188cc2019-01-07 10:46:33 -0700365static struct kmem_cache *req_cachep;
366
367static const struct file_operations io_uring_fops;
368
369struct sock *io_uring_get_socket(struct file *file)
370{
371#if defined(CONFIG_UNIX)
372 if (file->f_op == &io_uring_fops) {
373 struct io_ring_ctx *ctx = file->private_data;
374
375 return ctx->ring_sock->sk;
376 }
377#endif
378 return NULL;
379}
380EXPORT_SYMBOL(io_uring_get_socket);
381
382static void io_ring_ctx_ref_free(struct percpu_ref *ref)
383{
384 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
385
386 complete(&ctx->ctx_done);
387}
388
389static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
390{
391 struct io_ring_ctx *ctx;
392
393 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
394 if (!ctx)
395 return NULL;
396
Roman Gushchin21482892019-05-07 10:01:48 -0700397 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
398 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700399 kfree(ctx);
400 return NULL;
401 }
402
403 ctx->flags = p->flags;
404 init_waitqueue_head(&ctx->cq_wait);
405 init_completion(&ctx->ctx_done);
Jackie Liua4c0b3d2019-07-08 13:41:12 +0800406 init_completion(&ctx->sqo_thread_started);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700407 mutex_init(&ctx->uring_lock);
408 init_waitqueue_head(&ctx->wait);
409 spin_lock_init(&ctx->completion_lock);
Jens Axboedef596e2019-01-09 08:59:42 -0700410 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboe221c5eb2019-01-17 09:41:58 -0700411 INIT_LIST_HEAD(&ctx->cancel_list);
Jens Axboede0617e2019-04-06 21:51:27 -0600412 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -0600413 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -0600414 init_waitqueue_head(&ctx->inflight_wait);
415 spin_lock_init(&ctx->inflight_lock);
416 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700417 return ctx;
418}
419
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600420static inline bool __io_sequence_defer(struct io_ring_ctx *ctx,
421 struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -0600422{
Jens Axboe498ccd92019-10-25 10:04:25 -0600423 return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped
424 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboede0617e2019-04-06 21:51:27 -0600425}
426
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600427static inline bool io_sequence_defer(struct io_ring_ctx *ctx,
428 struct io_kiocb *req)
429{
430 if ((req->flags & (REQ_F_IO_DRAIN|REQ_F_IO_DRAINED)) != REQ_F_IO_DRAIN)
431 return false;
432
433 return __io_sequence_defer(ctx, req);
434}
435
436static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -0600437{
438 struct io_kiocb *req;
439
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600440 req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list);
441 if (req && !io_sequence_defer(ctx, req)) {
Jens Axboede0617e2019-04-06 21:51:27 -0600442 list_del_init(&req->list);
443 return req;
444 }
445
446 return NULL;
447}
448
Jens Axboe5262f562019-09-17 12:26:57 -0600449static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx)
450{
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600451 struct io_kiocb *req;
452
453 req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list);
454 if (req && !__io_sequence_defer(ctx, req)) {
455 list_del_init(&req->list);
456 return req;
457 }
458
459 return NULL;
Jens Axboe5262f562019-09-17 12:26:57 -0600460}
461
Jens Axboede0617e2019-04-06 21:51:27 -0600462static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700463{
Hristo Venev75b28af2019-08-26 17:23:46 +0000464 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700465
Hristo Venev75b28af2019-08-26 17:23:46 +0000466 if (ctx->cached_cq_tail != READ_ONCE(rings->cq.tail)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700467 /* order cqe stores with ring update */
Hristo Venev75b28af2019-08-26 17:23:46 +0000468 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700469
Jens Axboe2b188cc2019-01-07 10:46:33 -0700470 if (wq_has_sleeper(&ctx->cq_wait)) {
471 wake_up_interruptible(&ctx->cq_wait);
472 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
473 }
474 }
475}
476
Jens Axboe561fb042019-10-24 07:25:42 -0600477static inline bool io_sqe_needs_user(const struct io_uring_sqe *sqe)
Jens Axboe18d9be12019-09-10 09:13:05 -0600478{
Jens Axboe561fb042019-10-24 07:25:42 -0600479 u8 opcode = READ_ONCE(sqe->opcode);
480
481 return !(opcode == IORING_OP_READ_FIXED ||
482 opcode == IORING_OP_WRITE_FIXED);
483}
484
485static inline bool io_prep_async_work(struct io_kiocb *req)
486{
487 bool do_hashed = false;
Jens Axboe54a91f32019-09-10 09:15:04 -0600488
Jens Axboe6cc47d12019-09-18 11:18:23 -0600489 if (req->submit.sqe) {
490 switch (req->submit.sqe->opcode) {
491 case IORING_OP_WRITEV:
492 case IORING_OP_WRITE_FIXED:
Jens Axboe561fb042019-10-24 07:25:42 -0600493 do_hashed = true;
Jens Axboe6cc47d12019-09-18 11:18:23 -0600494 break;
495 }
Jens Axboe561fb042019-10-24 07:25:42 -0600496 if (io_sqe_needs_user(req->submit.sqe))
497 req->work.flags |= IO_WQ_WORK_NEEDS_USER;
Jens Axboe54a91f32019-09-10 09:15:04 -0600498 }
499
Jens Axboe561fb042019-10-24 07:25:42 -0600500 return do_hashed;
501}
502
503static inline void io_queue_async_work(struct io_ring_ctx *ctx,
504 struct io_kiocb *req)
505{
506 bool do_hashed = io_prep_async_work(req);
507
508 trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work,
509 req->flags);
510 if (!do_hashed) {
511 io_wq_enqueue(ctx->io_wq, &req->work);
512 } else {
513 io_wq_enqueue_hashed(ctx->io_wq, &req->work,
514 file_inode(req->file));
515 }
Jens Axboe18d9be12019-09-10 09:13:05 -0600516}
517
Jens Axboe5262f562019-09-17 12:26:57 -0600518static void io_kill_timeout(struct io_kiocb *req)
519{
520 int ret;
521
522 ret = hrtimer_try_to_cancel(&req->timeout.timer);
523 if (ret != -1) {
524 atomic_inc(&req->ctx->cq_timeouts);
525 list_del(&req->list);
526 io_cqring_fill_event(req->ctx, req->user_data, 0);
527 __io_free_req(req);
528 }
529}
530
531static void io_kill_timeouts(struct io_ring_ctx *ctx)
532{
533 struct io_kiocb *req, *tmp;
534
535 spin_lock_irq(&ctx->completion_lock);
536 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
537 io_kill_timeout(req);
538 spin_unlock_irq(&ctx->completion_lock);
539}
540
Jens Axboede0617e2019-04-06 21:51:27 -0600541static void io_commit_cqring(struct io_ring_ctx *ctx)
542{
543 struct io_kiocb *req;
544
Jens Axboe5262f562019-09-17 12:26:57 -0600545 while ((req = io_get_timeout_req(ctx)) != NULL)
546 io_kill_timeout(req);
547
Jens Axboede0617e2019-04-06 21:51:27 -0600548 __io_commit_cqring(ctx);
549
550 while ((req = io_get_deferred_req(ctx)) != NULL) {
Jackie Liu4fe2c962019-09-09 20:50:40 +0800551 if (req->flags & REQ_F_SHADOW_DRAIN) {
552 /* Just for drain, free it. */
553 __io_free_req(req);
554 continue;
555 }
Jens Axboede0617e2019-04-06 21:51:27 -0600556 req->flags |= REQ_F_IO_DRAINED;
Jens Axboe18d9be12019-09-10 09:13:05 -0600557 io_queue_async_work(ctx, req);
Jens Axboede0617e2019-04-06 21:51:27 -0600558 }
559}
560
Jens Axboe2b188cc2019-01-07 10:46:33 -0700561static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
562{
Hristo Venev75b28af2019-08-26 17:23:46 +0000563 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700564 unsigned tail;
565
566 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +0200567 /*
568 * writes to the cq entry need to come after reading head; the
569 * control dependency is enough as we're using WRITE_ONCE to
570 * fill the cq entry
571 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000572 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700573 return NULL;
574
575 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +0000576 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -0700577}
578
579static void io_cqring_fill_event(struct io_ring_ctx *ctx, u64 ki_user_data,
Jens Axboec71ffb62019-05-13 20:58:29 -0600580 long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700581{
582 struct io_uring_cqe *cqe;
583
584 /*
585 * If we can't get a cq entry, userspace overflowed the
586 * submission (by quite a lot). Increment the overflow count in
587 * the ring.
588 */
589 cqe = io_get_cqring(ctx);
590 if (cqe) {
591 WRITE_ONCE(cqe->user_data, ki_user_data);
592 WRITE_ONCE(cqe->res, res);
Jens Axboec71ffb62019-05-13 20:58:29 -0600593 WRITE_ONCE(cqe->flags, 0);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700594 } else {
Jens Axboe498ccd92019-10-25 10:04:25 -0600595 WRITE_ONCE(ctx->rings->cq_overflow,
596 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe2b188cc2019-01-07 10:46:33 -0700597 }
598}
599
Jens Axboe8c838782019-03-12 15:48:16 -0600600static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
601{
602 if (waitqueue_active(&ctx->wait))
603 wake_up(&ctx->wait);
604 if (waitqueue_active(&ctx->sqo_wait))
605 wake_up(&ctx->sqo_wait);
Jens Axboe9b402842019-04-11 11:45:41 -0600606 if (ctx->cq_ev_fd)
607 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -0600608}
609
610static void io_cqring_add_event(struct io_ring_ctx *ctx, u64 user_data,
Jens Axboec71ffb62019-05-13 20:58:29 -0600611 long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700612{
613 unsigned long flags;
614
615 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboec71ffb62019-05-13 20:58:29 -0600616 io_cqring_fill_event(ctx, user_data, res);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700617 io_commit_cqring(ctx);
618 spin_unlock_irqrestore(&ctx->completion_lock, flags);
619
Jens Axboe8c838782019-03-12 15:48:16 -0600620 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700621}
622
Jens Axboe2579f912019-01-09 09:10:43 -0700623static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
624 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700625{
Jens Axboefd6fab22019-03-14 16:30:06 -0600626 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700627 struct io_kiocb *req;
628
629 if (!percpu_ref_tryget(&ctx->refs))
630 return NULL;
631
Jens Axboe2579f912019-01-09 09:10:43 -0700632 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -0600633 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -0700634 if (unlikely(!req))
635 goto out;
636 } else if (!state->free_reqs) {
637 size_t sz;
638 int ret;
639
640 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -0600641 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
642
643 /*
644 * Bulk alloc is all-or-nothing. If we fail to get a batch,
645 * retry single alloc to be on the safe side.
646 */
647 if (unlikely(ret <= 0)) {
648 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
649 if (!state->reqs[0])
650 goto out;
651 ret = 1;
652 }
Jens Axboe2579f912019-01-09 09:10:43 -0700653 state->free_reqs = ret - 1;
654 state->cur_req = 1;
655 req = state->reqs[0];
656 } else {
657 req = state->reqs[state->cur_req];
658 state->free_reqs--;
659 state->cur_req++;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700660 }
661
Jens Axboe60c112b2019-06-21 10:20:18 -0600662 req->file = NULL;
Jens Axboe2579f912019-01-09 09:10:43 -0700663 req->ctx = ctx;
664 req->flags = 0;
Jens Axboee65ef562019-03-12 10:16:44 -0600665 /* one is dropped after submission, the other at completion */
666 refcount_set(&req->refs, 2);
Jens Axboe9e645e112019-05-10 16:07:28 -0600667 req->result = 0;
Jens Axboe561fb042019-10-24 07:25:42 -0600668 INIT_IO_WORK(&req->work, io_wq_submit_work);
Jens Axboe2579f912019-01-09 09:10:43 -0700669 return req;
670out:
Pavel Begunkov6805b322019-10-08 02:18:42 +0300671 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700672 return NULL;
673}
674
Jens Axboedef596e2019-01-09 08:59:42 -0700675static void io_free_req_many(struct io_ring_ctx *ctx, void **reqs, int *nr)
676{
677 if (*nr) {
678 kmem_cache_free_bulk(req_cachep, *nr, reqs);
Pavel Begunkov6805b322019-10-08 02:18:42 +0300679 percpu_ref_put_many(&ctx->refs, *nr);
Jens Axboedef596e2019-01-09 08:59:42 -0700680 *nr = 0;
681 }
682}
683
Jens Axboe9e645e112019-05-10 16:07:28 -0600684static void __io_free_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700685{
Jens Axboefcb323c2019-10-24 12:39:47 -0600686 struct io_ring_ctx *ctx = req->ctx;
687
Jens Axboe09bb8392019-03-13 12:39:28 -0600688 if (req->file && !(req->flags & REQ_F_FIXED_FILE))
689 fput(req->file);
Jens Axboefcb323c2019-10-24 12:39:47 -0600690 if (req->flags & REQ_F_INFLIGHT) {
691 unsigned long flags;
692
693 spin_lock_irqsave(&ctx->inflight_lock, flags);
694 list_del(&req->inflight_entry);
695 if (waitqueue_active(&ctx->inflight_wait))
696 wake_up(&ctx->inflight_wait);
697 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
698 }
699 percpu_ref_put(&ctx->refs);
Jens Axboee65ef562019-03-12 10:16:44 -0600700 kmem_cache_free(req_cachep, req);
701}
702
Jens Axboeba816ad2019-09-28 11:36:45 -0600703static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -0600704{
705 struct io_kiocb *nxt;
706
707 /*
708 * The list should never be empty when we are called here. But could
709 * potentially happen if the chain is messed up, check to be on the
710 * safe side.
711 */
712 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, list);
713 if (nxt) {
714 list_del(&nxt->list);
715 if (!list_empty(&req->link_list)) {
716 INIT_LIST_HEAD(&nxt->link_list);
717 list_splice(&req->link_list, &nxt->link_list);
718 nxt->flags |= REQ_F_LINK;
719 }
720
Zhengyuan Liuf7b76ac2019-07-16 23:26:14 +0800721 nxt->flags |= REQ_F_LINK_DONE;
Jens Axboeba816ad2019-09-28 11:36:45 -0600722 /*
723 * If we're in async work, we can continue processing the chain
724 * in this context instead of having to queue up new async work.
725 */
Jens Axboe561fb042019-10-24 07:25:42 -0600726 if (nxtptr && current_work())
Jens Axboeba816ad2019-09-28 11:36:45 -0600727 *nxtptr = nxt;
Jens Axboe561fb042019-10-24 07:25:42 -0600728 else
Jens Axboeba816ad2019-09-28 11:36:45 -0600729 io_queue_async_work(req->ctx, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -0600730 }
731}
732
733/*
734 * Called if REQ_F_LINK is set, and we fail the head request
735 */
736static void io_fail_links(struct io_kiocb *req)
737{
738 struct io_kiocb *link;
739
740 while (!list_empty(&req->link_list)) {
741 link = list_first_entry(&req->link_list, struct io_kiocb, list);
742 list_del(&link->list);
743
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +0200744 trace_io_uring_fail_link(req, link);
Jens Axboe9e645e112019-05-10 16:07:28 -0600745 io_cqring_add_event(req->ctx, link->user_data, -ECANCELED);
746 __io_free_req(link);
747 }
748}
749
Jens Axboeba816ad2019-09-28 11:36:45 -0600750static void io_free_req(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -0600751{
752 /*
753 * If LINK is set, we have dependent requests in this chain. If we
754 * didn't fail this request, queue the first one up, moving any other
755 * dependencies to the next request. In case of failure, fail the rest
756 * of the chain.
757 */
758 if (req->flags & REQ_F_LINK) {
759 if (req->flags & REQ_F_FAIL_LINK)
760 io_fail_links(req);
761 else
Jens Axboeba816ad2019-09-28 11:36:45 -0600762 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -0600763 }
764
765 __io_free_req(req);
766}
767
Jens Axboeba816ad2019-09-28 11:36:45 -0600768/*
769 * Drop reference to request, return next in chain (if there is one) if this
770 * was the last reference to this request.
771 */
772static struct io_kiocb *io_put_req_find_next(struct io_kiocb *req)
Jens Axboee65ef562019-03-12 10:16:44 -0600773{
Jens Axboeba816ad2019-09-28 11:36:45 -0600774 struct io_kiocb *nxt = NULL;
775
Jens Axboee65ef562019-03-12 10:16:44 -0600776 if (refcount_dec_and_test(&req->refs))
Jens Axboeba816ad2019-09-28 11:36:45 -0600777 io_free_req(req, &nxt);
778
779 return nxt;
780}
781
782static void io_put_req(struct io_kiocb *req, struct io_kiocb **nxtptr)
783{
784 struct io_kiocb *nxt;
785
786 nxt = io_put_req_find_next(req);
787 if (nxt) {
Jens Axboe561fb042019-10-24 07:25:42 -0600788 if (nxtptr)
Jens Axboeba816ad2019-09-28 11:36:45 -0600789 *nxtptr = nxt;
Jens Axboe561fb042019-10-24 07:25:42 -0600790 else
Jens Axboeba816ad2019-09-28 11:36:45 -0600791 io_queue_async_work(nxt->ctx, nxt);
Jens Axboeba816ad2019-09-28 11:36:45 -0600792 }
Jens Axboe2b188cc2019-01-07 10:46:33 -0700793}
794
Hristo Venev75b28af2019-08-26 17:23:46 +0000795static unsigned io_cqring_events(struct io_rings *rings)
Jens Axboea3a0e432019-08-20 11:03:11 -0600796{
797 /* See comment at the top of this file */
798 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +0000799 return READ_ONCE(rings->cq.tail) - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -0600800}
801
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +0300802static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
803{
804 struct io_rings *rings = ctx->rings;
805
806 /* make sure SQ entry isn't read before tail */
807 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
808}
809
Jens Axboedef596e2019-01-09 08:59:42 -0700810/*
811 * Find and free completed poll iocbs
812 */
813static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
814 struct list_head *done)
815{
816 void *reqs[IO_IOPOLL_BATCH];
817 struct io_kiocb *req;
Jens Axboe09bb8392019-03-13 12:39:28 -0600818 int to_free;
Jens Axboedef596e2019-01-09 08:59:42 -0700819
Jens Axboe09bb8392019-03-13 12:39:28 -0600820 to_free = 0;
Jens Axboedef596e2019-01-09 08:59:42 -0700821 while (!list_empty(done)) {
822 req = list_first_entry(done, struct io_kiocb, list);
823 list_del(&req->list);
824
Jens Axboe9e645e112019-05-10 16:07:28 -0600825 io_cqring_fill_event(ctx, req->user_data, req->result);
Jens Axboedef596e2019-01-09 08:59:42 -0700826 (*nr_events)++;
827
Jens Axboe09bb8392019-03-13 12:39:28 -0600828 if (refcount_dec_and_test(&req->refs)) {
829 /* If we're not using fixed files, we have to pair the
830 * completion part with the file put. Use regular
831 * completions for those, only batch free for fixed
Jens Axboe9e645e112019-05-10 16:07:28 -0600832 * file and non-linked commands.
Jens Axboe09bb8392019-03-13 12:39:28 -0600833 */
Jens Axboe9e645e112019-05-10 16:07:28 -0600834 if ((req->flags & (REQ_F_FIXED_FILE|REQ_F_LINK)) ==
835 REQ_F_FIXED_FILE) {
Jens Axboe09bb8392019-03-13 12:39:28 -0600836 reqs[to_free++] = req;
837 if (to_free == ARRAY_SIZE(reqs))
838 io_free_req_many(ctx, reqs, &to_free);
Jens Axboe6b063142019-01-10 22:13:58 -0700839 } else {
Jens Axboeba816ad2019-09-28 11:36:45 -0600840 io_free_req(req, NULL);
Jens Axboe6b063142019-01-10 22:13:58 -0700841 }
Jens Axboe9a56a232019-01-09 09:06:50 -0700842 }
Jens Axboedef596e2019-01-09 08:59:42 -0700843 }
Jens Axboedef596e2019-01-09 08:59:42 -0700844
Jens Axboe09bb8392019-03-13 12:39:28 -0600845 io_commit_cqring(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -0700846 io_free_req_many(ctx, reqs, &to_free);
847}
848
849static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
850 long min)
851{
852 struct io_kiocb *req, *tmp;
853 LIST_HEAD(done);
854 bool spin;
855 int ret;
856
857 /*
858 * Only spin for completions if we don't have multiple devices hanging
859 * off our complete list, and we're under the requested amount.
860 */
861 spin = !ctx->poll_multi_file && *nr_events < min;
862
863 ret = 0;
864 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
865 struct kiocb *kiocb = &req->rw;
866
867 /*
868 * Move completed entries to our local list. If we find a
869 * request that requires polling, break out and complete
870 * the done list first, if we have entries there.
871 */
872 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
873 list_move_tail(&req->list, &done);
874 continue;
875 }
876 if (!list_empty(&done))
877 break;
878
879 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
880 if (ret < 0)
881 break;
882
883 if (ret && spin)
884 spin = false;
885 ret = 0;
886 }
887
888 if (!list_empty(&done))
889 io_iopoll_complete(ctx, nr_events, &done);
890
891 return ret;
892}
893
894/*
895 * Poll for a mininum of 'min' events. Note that if min == 0 we consider that a
896 * non-spinning poll check - we'll still enter the driver poll loop, but only
897 * as a non-spinning completion check.
898 */
899static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
900 long min)
901{
Jens Axboe08f54392019-08-21 22:19:11 -0600902 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -0700903 int ret;
904
905 ret = io_do_iopoll(ctx, nr_events, min);
906 if (ret < 0)
907 return ret;
908 if (!min || *nr_events >= min)
909 return 0;
910 }
911
912 return 1;
913}
914
915/*
916 * We can't just wait for polled events to come to us, we have to actively
917 * find and complete them.
918 */
919static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
920{
921 if (!(ctx->flags & IORING_SETUP_IOPOLL))
922 return;
923
924 mutex_lock(&ctx->uring_lock);
925 while (!list_empty(&ctx->poll_list)) {
926 unsigned int nr_events = 0;
927
928 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -0600929
930 /*
931 * Ensure we allow local-to-the-cpu processing to take place,
932 * in this case we need to ensure that we reap all events.
933 */
934 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -0700935 }
936 mutex_unlock(&ctx->uring_lock);
937}
938
Jens Axboe2b2ed972019-10-25 10:06:15 -0600939static int __io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
940 long min)
Jens Axboedef596e2019-01-09 08:59:42 -0700941{
Jens Axboe2b2ed972019-10-25 10:06:15 -0600942 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -0700943
944 do {
945 int tmin = 0;
946
Jens Axboe500f9fb2019-08-19 12:15:59 -0600947 /*
Jens Axboea3a0e432019-08-20 11:03:11 -0600948 * Don't enter poll loop if we already have events pending.
949 * If we do, we can potentially be spinning for commands that
950 * already triggered a CQE (eg in error).
951 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000952 if (io_cqring_events(ctx->rings))
Jens Axboea3a0e432019-08-20 11:03:11 -0600953 break;
954
955 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -0600956 * If a submit got punted to a workqueue, we can have the
957 * application entering polling for a command before it gets
958 * issued. That app will hold the uring_lock for the duration
959 * of the poll right here, so we need to take a breather every
960 * now and then to ensure that the issue has a chance to add
961 * the poll to the issued list. Otherwise we can spin here
962 * forever, while the workqueue is stuck trying to acquire the
963 * very same mutex.
964 */
965 if (!(++iters & 7)) {
966 mutex_unlock(&ctx->uring_lock);
967 mutex_lock(&ctx->uring_lock);
968 }
969
Jens Axboedef596e2019-01-09 08:59:42 -0700970 if (*nr_events < min)
971 tmin = min - *nr_events;
972
973 ret = io_iopoll_getevents(ctx, nr_events, tmin);
974 if (ret <= 0)
975 break;
976 ret = 0;
977 } while (min && !*nr_events && !need_resched());
978
Jens Axboe2b2ed972019-10-25 10:06:15 -0600979 return ret;
980}
981
982static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
983 long min)
984{
985 int ret;
986
987 /*
988 * We disallow the app entering submit/complete with polling, but we
989 * still need to lock the ring to prevent racing with polled issue
990 * that got punted to a workqueue.
991 */
992 mutex_lock(&ctx->uring_lock);
993 ret = __io_iopoll_check(ctx, nr_events, min);
Jens Axboe500f9fb2019-08-19 12:15:59 -0600994 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -0700995 return ret;
996}
997
Jens Axboe491381ce2019-10-17 09:20:46 -0600998static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700999{
Jens Axboe491381ce2019-10-17 09:20:46 -06001000 /*
1001 * Tell lockdep we inherited freeze protection from submission
1002 * thread.
1003 */
1004 if (req->flags & REQ_F_ISREG) {
1005 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001006
Jens Axboe491381ce2019-10-17 09:20:46 -06001007 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001008 }
Jens Axboe491381ce2019-10-17 09:20:46 -06001009 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001010}
1011
Jens Axboeba816ad2019-09-28 11:36:45 -06001012static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001013{
1014 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1015
Jens Axboe491381ce2019-10-17 09:20:46 -06001016 if (kiocb->ki_flags & IOCB_WRITE)
1017 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001018
Jens Axboe9e645e112019-05-10 16:07:28 -06001019 if ((req->flags & REQ_F_LINK) && res != req->result)
1020 req->flags |= REQ_F_FAIL_LINK;
Jens Axboec71ffb62019-05-13 20:58:29 -06001021 io_cqring_add_event(req->ctx, req->user_data, res);
Jens Axboeba816ad2019-09-28 11:36:45 -06001022}
1023
1024static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1025{
1026 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1027
1028 io_complete_rw_common(kiocb, res);
1029 io_put_req(req, NULL);
1030}
1031
1032static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res)
1033{
1034 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1035
1036 io_complete_rw_common(kiocb, res);
1037 return io_put_req_find_next(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001038}
1039
Jens Axboedef596e2019-01-09 08:59:42 -07001040static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1041{
1042 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1043
Jens Axboe491381ce2019-10-17 09:20:46 -06001044 if (kiocb->ki_flags & IOCB_WRITE)
1045 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001046
Jens Axboe9e645e112019-05-10 16:07:28 -06001047 if ((req->flags & REQ_F_LINK) && res != req->result)
1048 req->flags |= REQ_F_FAIL_LINK;
1049 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -07001050 if (res != -EAGAIN)
1051 req->flags |= REQ_F_IOPOLL_COMPLETED;
1052}
1053
1054/*
1055 * After the iocb has been issued, it's safe to be found on the poll list.
1056 * Adding the kiocb to the list AFTER submission ensures that we don't
1057 * find it from a io_iopoll_getevents() thread before the issuer is done
1058 * accessing the kiocb cookie.
1059 */
1060static void io_iopoll_req_issued(struct io_kiocb *req)
1061{
1062 struct io_ring_ctx *ctx = req->ctx;
1063
1064 /*
1065 * Track whether we have multiple files in our lists. This will impact
1066 * how we do polling eventually, not spinning if we're on potentially
1067 * different devices.
1068 */
1069 if (list_empty(&ctx->poll_list)) {
1070 ctx->poll_multi_file = false;
1071 } else if (!ctx->poll_multi_file) {
1072 struct io_kiocb *list_req;
1073
1074 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1075 list);
1076 if (list_req->rw.ki_filp != req->rw.ki_filp)
1077 ctx->poll_multi_file = true;
1078 }
1079
1080 /*
1081 * For fast devices, IO may have already completed. If it has, add
1082 * it to the front so we find it first.
1083 */
1084 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1085 list_add(&req->list, &ctx->poll_list);
1086 else
1087 list_add_tail(&req->list, &ctx->poll_list);
1088}
1089
Jens Axboe3d6770f2019-04-13 11:50:54 -06001090static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07001091{
Jens Axboe3d6770f2019-04-13 11:50:54 -06001092 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -07001093 int diff = state->has_refs - state->used_refs;
1094
1095 if (diff)
1096 fput_many(state->file, diff);
1097 state->file = NULL;
1098 }
1099}
1100
1101/*
1102 * Get as many references to a file as we have IOs left in this submission,
1103 * assuming most submissions are for one file, or at least that each file
1104 * has more than one submission.
1105 */
1106static struct file *io_file_get(struct io_submit_state *state, int fd)
1107{
1108 if (!state)
1109 return fget(fd);
1110
1111 if (state->file) {
1112 if (state->fd == fd) {
1113 state->used_refs++;
1114 state->ios_left--;
1115 return state->file;
1116 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06001117 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07001118 }
1119 state->file = fget_many(fd, state->ios_left);
1120 if (!state->file)
1121 return NULL;
1122
1123 state->fd = fd;
1124 state->has_refs = state->ios_left;
1125 state->used_refs = 1;
1126 state->ios_left--;
1127 return state->file;
1128}
1129
Jens Axboe2b188cc2019-01-07 10:46:33 -07001130/*
1131 * If we tracked the file through the SCM inflight mechanism, we could support
1132 * any file. For now, just ensure that anything potentially problematic is done
1133 * inline.
1134 */
1135static bool io_file_supports_async(struct file *file)
1136{
1137 umode_t mode = file_inode(file)->i_mode;
1138
1139 if (S_ISBLK(mode) || S_ISCHR(mode))
1140 return true;
1141 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
1142 return true;
1143
1144 return false;
1145}
1146
Jens Axboe6c271ce2019-01-10 11:22:30 -07001147static int io_prep_rw(struct io_kiocb *req, const struct sqe_submit *s,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001148 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001149{
Jens Axboe6c271ce2019-01-10 11:22:30 -07001150 const struct io_uring_sqe *sqe = s->sqe;
Jens Axboedef596e2019-01-09 08:59:42 -07001151 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001152 struct kiocb *kiocb = &req->rw;
Jens Axboe09bb8392019-03-13 12:39:28 -06001153 unsigned ioprio;
1154 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001155
Jens Axboe09bb8392019-03-13 12:39:28 -06001156 if (!req->file)
1157 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001158
Jens Axboe491381ce2019-10-17 09:20:46 -06001159 if (S_ISREG(file_inode(req->file)->i_mode))
1160 req->flags |= REQ_F_ISREG;
1161
1162 /*
1163 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
1164 * we know to async punt it even if it was opened O_NONBLOCK
1165 */
1166 if (force_nonblock && !io_file_supports_async(req->file)) {
1167 req->flags |= REQ_F_MUST_PUNT;
1168 return -EAGAIN;
1169 }
Jens Axboe6b063142019-01-10 22:13:58 -07001170
Jens Axboe2b188cc2019-01-07 10:46:33 -07001171 kiocb->ki_pos = READ_ONCE(sqe->off);
1172 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
1173 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
1174
1175 ioprio = READ_ONCE(sqe->ioprio);
1176 if (ioprio) {
1177 ret = ioprio_check_cap(ioprio);
1178 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06001179 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001180
1181 kiocb->ki_ioprio = ioprio;
1182 } else
1183 kiocb->ki_ioprio = get_current_ioprio();
1184
1185 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
1186 if (unlikely(ret))
Jens Axboe09bb8392019-03-13 12:39:28 -06001187 return ret;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001188
1189 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06001190 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
1191 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02001192 req->flags |= REQ_F_NOWAIT;
1193
1194 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001195 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001196
Jens Axboedef596e2019-01-09 08:59:42 -07001197 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07001198 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
1199 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06001200 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001201
Jens Axboedef596e2019-01-09 08:59:42 -07001202 kiocb->ki_flags |= IOCB_HIPRI;
1203 kiocb->ki_complete = io_complete_rw_iopoll;
1204 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001205 if (kiocb->ki_flags & IOCB_HIPRI)
1206 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07001207 kiocb->ki_complete = io_complete_rw;
1208 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001209 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001210}
1211
1212static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
1213{
1214 switch (ret) {
1215 case -EIOCBQUEUED:
1216 break;
1217 case -ERESTARTSYS:
1218 case -ERESTARTNOINTR:
1219 case -ERESTARTNOHAND:
1220 case -ERESTART_RESTARTBLOCK:
1221 /*
1222 * We can't just restart the syscall, since previously
1223 * submitted sqes may already be in progress. Just fail this
1224 * IO with EINTR.
1225 */
1226 ret = -EINTR;
1227 /* fall through */
1228 default:
1229 kiocb->ki_complete(kiocb, ret, 0);
1230 }
1231}
1232
Jens Axboeba816ad2019-09-28 11:36:45 -06001233static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt,
1234 bool in_async)
1235{
1236 if (in_async && ret >= 0 && nxt && kiocb->ki_complete == io_complete_rw)
1237 *nxt = __io_complete_rw(kiocb, ret);
1238 else
1239 io_rw_done(kiocb, ret);
1240}
1241
Jens Axboeedafcce2019-01-09 09:16:05 -07001242static int io_import_fixed(struct io_ring_ctx *ctx, int rw,
1243 const struct io_uring_sqe *sqe,
1244 struct iov_iter *iter)
1245{
1246 size_t len = READ_ONCE(sqe->len);
1247 struct io_mapped_ubuf *imu;
1248 unsigned index, buf_index;
1249 size_t offset;
1250 u64 buf_addr;
1251
1252 /* attempt to use fixed buffers without having provided iovecs */
1253 if (unlikely(!ctx->user_bufs))
1254 return -EFAULT;
1255
1256 buf_index = READ_ONCE(sqe->buf_index);
1257 if (unlikely(buf_index >= ctx->nr_user_bufs))
1258 return -EFAULT;
1259
1260 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
1261 imu = &ctx->user_bufs[index];
1262 buf_addr = READ_ONCE(sqe->addr);
1263
1264 /* overflow */
1265 if (buf_addr + len < buf_addr)
1266 return -EFAULT;
1267 /* not inside the mapped region */
1268 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
1269 return -EFAULT;
1270
1271 /*
1272 * May not be a start of buffer, set size appropriately
1273 * and advance us to the beginning.
1274 */
1275 offset = buf_addr - imu->ubuf;
1276 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06001277
1278 if (offset) {
1279 /*
1280 * Don't use iov_iter_advance() here, as it's really slow for
1281 * using the latter parts of a big fixed buffer - it iterates
1282 * over each segment manually. We can cheat a bit here, because
1283 * we know that:
1284 *
1285 * 1) it's a BVEC iter, we set it up
1286 * 2) all bvecs are PAGE_SIZE in size, except potentially the
1287 * first and last bvec
1288 *
1289 * So just find our index, and adjust the iterator afterwards.
1290 * If the offset is within the first bvec (or the whole first
1291 * bvec, just use iov_iter_advance(). This makes it easier
1292 * since we can just skip the first segment, which may not
1293 * be PAGE_SIZE aligned.
1294 */
1295 const struct bio_vec *bvec = imu->bvec;
1296
1297 if (offset <= bvec->bv_len) {
1298 iov_iter_advance(iter, offset);
1299 } else {
1300 unsigned long seg_skip;
1301
1302 /* skip first vec */
1303 offset -= bvec->bv_len;
1304 seg_skip = 1 + (offset >> PAGE_SHIFT);
1305
1306 iter->bvec = bvec + seg_skip;
1307 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02001308 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001309 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001310 }
1311 }
1312
Jens Axboeedafcce2019-01-09 09:16:05 -07001313 return 0;
1314}
1315
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001316static ssize_t io_import_iovec(struct io_ring_ctx *ctx, int rw,
1317 const struct sqe_submit *s, struct iovec **iovec,
1318 struct iov_iter *iter)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001319{
1320 const struct io_uring_sqe *sqe = s->sqe;
1321 void __user *buf = u64_to_user_ptr(READ_ONCE(sqe->addr));
1322 size_t sqe_len = READ_ONCE(sqe->len);
Jens Axboeedafcce2019-01-09 09:16:05 -07001323 u8 opcode;
1324
1325 /*
1326 * We're reading ->opcode for the second time, but the first read
1327 * doesn't care whether it's _FIXED or not, so it doesn't matter
1328 * whether ->opcode changes concurrently. The first read does care
1329 * about whether it is a READ or a WRITE, so we don't trust this read
1330 * for that purpose and instead let the caller pass in the read/write
1331 * flag.
1332 */
1333 opcode = READ_ONCE(sqe->opcode);
1334 if (opcode == IORING_OP_READ_FIXED ||
1335 opcode == IORING_OP_WRITE_FIXED) {
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001336 ssize_t ret = io_import_fixed(ctx, rw, sqe, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07001337 *iovec = NULL;
1338 return ret;
1339 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001340
1341 if (!s->has_user)
1342 return -EFAULT;
1343
1344#ifdef CONFIG_COMPAT
1345 if (ctx->compat)
1346 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
1347 iovec, iter);
1348#endif
1349
1350 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
1351}
1352
Jens Axboe32960612019-09-23 11:05:34 -06001353/*
1354 * For files that don't have ->read_iter() and ->write_iter(), handle them
1355 * by looping over ->read() or ->write() manually.
1356 */
1357static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
1358 struct iov_iter *iter)
1359{
1360 ssize_t ret = 0;
1361
1362 /*
1363 * Don't support polled IO through this interface, and we can't
1364 * support non-blocking either. For the latter, this just causes
1365 * the kiocb to be handled from an async context.
1366 */
1367 if (kiocb->ki_flags & IOCB_HIPRI)
1368 return -EOPNOTSUPP;
1369 if (kiocb->ki_flags & IOCB_NOWAIT)
1370 return -EAGAIN;
1371
1372 while (iov_iter_count(iter)) {
1373 struct iovec iovec = iov_iter_iovec(iter);
1374 ssize_t nr;
1375
1376 if (rw == READ) {
1377 nr = file->f_op->read(file, iovec.iov_base,
1378 iovec.iov_len, &kiocb->ki_pos);
1379 } else {
1380 nr = file->f_op->write(file, iovec.iov_base,
1381 iovec.iov_len, &kiocb->ki_pos);
1382 }
1383
1384 if (nr < 0) {
1385 if (!ret)
1386 ret = nr;
1387 break;
1388 }
1389 ret += nr;
1390 if (nr != iovec.iov_len)
1391 break;
1392 iov_iter_advance(iter, nr);
1393 }
1394
1395 return ret;
1396}
1397
Jens Axboee0c5c572019-03-12 10:18:47 -06001398static int io_read(struct io_kiocb *req, const struct sqe_submit *s,
Jens Axboeba816ad2019-09-28 11:36:45 -06001399 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001400{
1401 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1402 struct kiocb *kiocb = &req->rw;
1403 struct iov_iter iter;
1404 struct file *file;
Jens Axboe31b51512019-01-18 22:56:34 -07001405 size_t iov_count;
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001406 ssize_t read_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001407
Jens Axboe8358e3a2019-04-23 08:17:58 -06001408 ret = io_prep_rw(req, s, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001409 if (ret)
1410 return ret;
1411 file = kiocb->ki_filp;
1412
Jens Axboe2b188cc2019-01-07 10:46:33 -07001413 if (unlikely(!(file->f_mode & FMODE_READ)))
Jens Axboe09bb8392019-03-13 12:39:28 -06001414 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001415
1416 ret = io_import_iovec(req->ctx, READ, s, &iovec, &iter);
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001417 if (ret < 0)
Jens Axboe09bb8392019-03-13 12:39:28 -06001418 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001419
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001420 read_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06001421 if (req->flags & REQ_F_LINK)
1422 req->result = read_size;
1423
Jens Axboe31b51512019-01-18 22:56:34 -07001424 iov_count = iov_iter_count(&iter);
1425 ret = rw_verify_area(READ, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001426 if (!ret) {
1427 ssize_t ret2;
1428
Jens Axboe32960612019-09-23 11:05:34 -06001429 if (file->f_op->read_iter)
1430 ret2 = call_read_iter(file, kiocb, &iter);
1431 else
1432 ret2 = loop_rw_iter(READ, file, kiocb, &iter);
1433
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001434 /*
1435 * In case of a short read, punt to async. This can happen
1436 * if we have data partially cached. Alternatively we can
1437 * return the short read, in which case the application will
1438 * need to issue another SQE and wait for it. That SQE will
1439 * need async punt anyway, so it's more efficient to do it
1440 * here.
1441 */
Jens Axboe491381ce2019-10-17 09:20:46 -06001442 if (force_nonblock && !(req->flags & REQ_F_NOWAIT) &&
1443 (req->flags & REQ_F_ISREG) &&
1444 ret2 > 0 && ret2 < read_size)
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001445 ret2 = -EAGAIN;
1446 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboe561fb042019-10-24 07:25:42 -06001447 if (!force_nonblock || ret2 != -EAGAIN)
Jackie Liuba5290c2019-10-09 09:19:59 +08001448 kiocb_done(kiocb, ret2, nxt, s->in_async);
Jens Axboe561fb042019-10-24 07:25:42 -06001449 else
Jens Axboe2b188cc2019-01-07 10:46:33 -07001450 ret = -EAGAIN;
1451 }
1452 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001453 return ret;
1454}
1455
Jens Axboee0c5c572019-03-12 10:18:47 -06001456static int io_write(struct io_kiocb *req, const struct sqe_submit *s,
Jens Axboeba816ad2019-09-28 11:36:45 -06001457 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001458{
1459 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1460 struct kiocb *kiocb = &req->rw;
1461 struct iov_iter iter;
1462 struct file *file;
Jens Axboe31b51512019-01-18 22:56:34 -07001463 size_t iov_count;
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001464 ssize_t ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001465
Jens Axboe8358e3a2019-04-23 08:17:58 -06001466 ret = io_prep_rw(req, s, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001467 if (ret)
1468 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001469
Jens Axboe2b188cc2019-01-07 10:46:33 -07001470 file = kiocb->ki_filp;
1471 if (unlikely(!(file->f_mode & FMODE_WRITE)))
Jens Axboe09bb8392019-03-13 12:39:28 -06001472 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001473
1474 ret = io_import_iovec(req->ctx, WRITE, s, &iovec, &iter);
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001475 if (ret < 0)
Jens Axboe09bb8392019-03-13 12:39:28 -06001476 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001477
Jens Axboe9e645e112019-05-10 16:07:28 -06001478 if (req->flags & REQ_F_LINK)
1479 req->result = ret;
1480
Jens Axboe31b51512019-01-18 22:56:34 -07001481 iov_count = iov_iter_count(&iter);
1482
1483 ret = -EAGAIN;
Jens Axboe561fb042019-10-24 07:25:42 -06001484 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT))
Jens Axboe31b51512019-01-18 22:56:34 -07001485 goto out_free;
Jens Axboe31b51512019-01-18 22:56:34 -07001486
1487 ret = rw_verify_area(WRITE, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001488 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01001489 ssize_t ret2;
1490
Jens Axboe2b188cc2019-01-07 10:46:33 -07001491 /*
1492 * Open-code file_start_write here to grab freeze protection,
1493 * which will be released by another thread in
1494 * io_complete_rw(). Fool lockdep by telling it the lock got
1495 * released so that it doesn't complain about the held lock when
1496 * we return to userspace.
1497 */
Jens Axboe491381ce2019-10-17 09:20:46 -06001498 if (req->flags & REQ_F_ISREG) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001499 __sb_start_write(file_inode(file)->i_sb,
1500 SB_FREEZE_WRITE, true);
1501 __sb_writers_release(file_inode(file)->i_sb,
1502 SB_FREEZE_WRITE);
1503 }
1504 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01001505
Jens Axboe32960612019-09-23 11:05:34 -06001506 if (file->f_op->write_iter)
1507 ret2 = call_write_iter(file, kiocb, &iter);
1508 else
1509 ret2 = loop_rw_iter(WRITE, file, kiocb, &iter);
Jens Axboe561fb042019-10-24 07:25:42 -06001510 if (!force_nonblock || ret2 != -EAGAIN)
Jackie Liuba5290c2019-10-09 09:19:59 +08001511 kiocb_done(kiocb, ret2, nxt, s->in_async);
Jens Axboe561fb042019-10-24 07:25:42 -06001512 else
Roman Penyaev9bf79332019-03-25 20:09:24 +01001513 ret = -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001514 }
Jens Axboe31b51512019-01-18 22:56:34 -07001515out_free:
Jens Axboe2b188cc2019-01-07 10:46:33 -07001516 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001517 return ret;
1518}
1519
1520/*
1521 * IORING_OP_NOP just posts a completion event, nothing else.
1522 */
1523static int io_nop(struct io_kiocb *req, u64 user_data)
1524{
1525 struct io_ring_ctx *ctx = req->ctx;
1526 long err = 0;
1527
Jens Axboedef596e2019-01-09 08:59:42 -07001528 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1529 return -EINVAL;
1530
Jens Axboec71ffb62019-05-13 20:58:29 -06001531 io_cqring_add_event(ctx, user_data, err);
Jens Axboeba816ad2019-09-28 11:36:45 -06001532 io_put_req(req, NULL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001533 return 0;
1534}
1535
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001536static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1537{
Jens Axboe6b063142019-01-10 22:13:58 -07001538 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001539
Jens Axboe09bb8392019-03-13 12:39:28 -06001540 if (!req->file)
1541 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001542
Jens Axboe6b063142019-01-10 22:13:58 -07001543 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07001544 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07001545 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001546 return -EINVAL;
1547
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001548 return 0;
1549}
1550
1551static int io_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001552 struct io_kiocb **nxt, bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001553{
1554 loff_t sqe_off = READ_ONCE(sqe->off);
1555 loff_t sqe_len = READ_ONCE(sqe->len);
1556 loff_t end = sqe_off + sqe_len;
1557 unsigned fsync_flags;
1558 int ret;
1559
1560 fsync_flags = READ_ONCE(sqe->fsync_flags);
1561 if (unlikely(fsync_flags & ~IORING_FSYNC_DATASYNC))
1562 return -EINVAL;
1563
1564 ret = io_prep_fsync(req, sqe);
1565 if (ret)
1566 return ret;
1567
1568 /* fsync always requires a blocking context */
1569 if (force_nonblock)
1570 return -EAGAIN;
1571
1572 ret = vfs_fsync_range(req->rw.ki_filp, sqe_off,
1573 end > 0 ? end : LLONG_MAX,
1574 fsync_flags & IORING_FSYNC_DATASYNC);
1575
Jens Axboe9e645e112019-05-10 16:07:28 -06001576 if (ret < 0 && (req->flags & REQ_F_LINK))
1577 req->flags |= REQ_F_FAIL_LINK;
Jens Axboec71ffb62019-05-13 20:58:29 -06001578 io_cqring_add_event(req->ctx, sqe->user_data, ret);
Jens Axboeba816ad2019-09-28 11:36:45 -06001579 io_put_req(req, nxt);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001580 return 0;
1581}
1582
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001583static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1584{
1585 struct io_ring_ctx *ctx = req->ctx;
1586 int ret = 0;
1587
1588 if (!req->file)
1589 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001590
1591 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1592 return -EINVAL;
1593 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
1594 return -EINVAL;
1595
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001596 return ret;
1597}
1598
1599static int io_sync_file_range(struct io_kiocb *req,
1600 const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001601 struct io_kiocb **nxt,
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001602 bool force_nonblock)
1603{
1604 loff_t sqe_off;
1605 loff_t sqe_len;
1606 unsigned flags;
1607 int ret;
1608
1609 ret = io_prep_sfr(req, sqe);
1610 if (ret)
1611 return ret;
1612
1613 /* sync_file_range always requires a blocking context */
1614 if (force_nonblock)
1615 return -EAGAIN;
1616
1617 sqe_off = READ_ONCE(sqe->off);
1618 sqe_len = READ_ONCE(sqe->len);
1619 flags = READ_ONCE(sqe->sync_range_flags);
1620
1621 ret = sync_file_range(req->rw.ki_filp, sqe_off, sqe_len, flags);
1622
Jens Axboe9e645e112019-05-10 16:07:28 -06001623 if (ret < 0 && (req->flags & REQ_F_LINK))
1624 req->flags |= REQ_F_FAIL_LINK;
Jens Axboec71ffb62019-05-13 20:58:29 -06001625 io_cqring_add_event(req->ctx, sqe->user_data, ret);
Jens Axboeba816ad2019-09-28 11:36:45 -06001626 io_put_req(req, nxt);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001627 return 0;
1628}
1629
Jens Axboe0fa03c62019-04-19 13:34:07 -06001630#if defined(CONFIG_NET)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001631static int io_send_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001632 struct io_kiocb **nxt, bool force_nonblock,
Jens Axboeaa1fa282019-04-19 13:38:09 -06001633 long (*fn)(struct socket *, struct user_msghdr __user *,
1634 unsigned int))
1635{
Jens Axboe0fa03c62019-04-19 13:34:07 -06001636 struct socket *sock;
1637 int ret;
1638
1639 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1640 return -EINVAL;
1641
1642 sock = sock_from_file(req->file, &ret);
1643 if (sock) {
1644 struct user_msghdr __user *msg;
1645 unsigned flags;
1646
1647 flags = READ_ONCE(sqe->msg_flags);
1648 if (flags & MSG_DONTWAIT)
1649 req->flags |= REQ_F_NOWAIT;
1650 else if (force_nonblock)
1651 flags |= MSG_DONTWAIT;
1652
1653 msg = (struct user_msghdr __user *) (unsigned long)
1654 READ_ONCE(sqe->addr);
1655
Jens Axboeaa1fa282019-04-19 13:38:09 -06001656 ret = fn(sock, msg, flags);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001657 if (force_nonblock && ret == -EAGAIN)
1658 return ret;
1659 }
1660
1661 io_cqring_add_event(req->ctx, sqe->user_data, ret);
Jens Axboeba816ad2019-09-28 11:36:45 -06001662 io_put_req(req, nxt);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001663 return 0;
Jens Axboeaa1fa282019-04-19 13:38:09 -06001664}
1665#endif
1666
1667static int io_sendmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001668 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001669{
1670#if defined(CONFIG_NET)
Jens Axboeba816ad2019-09-28 11:36:45 -06001671 return io_send_recvmsg(req, sqe, nxt, force_nonblock,
1672 __sys_sendmsg_sock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06001673#else
1674 return -EOPNOTSUPP;
1675#endif
1676}
1677
1678static int io_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001679 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001680{
1681#if defined(CONFIG_NET)
Jens Axboeba816ad2019-09-28 11:36:45 -06001682 return io_send_recvmsg(req, sqe, nxt, force_nonblock,
1683 __sys_recvmsg_sock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001684#else
1685 return -EOPNOTSUPP;
1686#endif
1687}
1688
Jens Axboe17f2fe32019-10-17 14:42:58 -06001689static int io_accept(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1690 struct io_kiocb **nxt, bool force_nonblock)
1691{
1692#if defined(CONFIG_NET)
1693 struct sockaddr __user *addr;
1694 int __user *addr_len;
1695 unsigned file_flags;
1696 int flags, ret;
1697
1698 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
1699 return -EINVAL;
1700 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
1701 return -EINVAL;
1702
1703 addr = (struct sockaddr __user *) (unsigned long) READ_ONCE(sqe->addr);
1704 addr_len = (int __user *) (unsigned long) READ_ONCE(sqe->addr2);
1705 flags = READ_ONCE(sqe->accept_flags);
1706 file_flags = force_nonblock ? O_NONBLOCK : 0;
1707
1708 ret = __sys_accept4_file(req->file, file_flags, addr, addr_len, flags);
1709 if (ret == -EAGAIN && force_nonblock) {
1710 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
1711 return -EAGAIN;
1712 }
1713 if (ret < 0 && (req->flags & REQ_F_LINK))
1714 req->flags |= REQ_F_FAIL_LINK;
1715 io_cqring_add_event(req->ctx, sqe->user_data, ret);
1716 io_put_req(req, nxt);
1717 return 0;
1718#else
1719 return -EOPNOTSUPP;
1720#endif
1721}
1722
Jens Axboe221c5eb2019-01-17 09:41:58 -07001723static void io_poll_remove_one(struct io_kiocb *req)
1724{
1725 struct io_poll_iocb *poll = &req->poll;
1726
1727 spin_lock(&poll->head->lock);
1728 WRITE_ONCE(poll->canceled, true);
1729 if (!list_empty(&poll->wait.entry)) {
1730 list_del_init(&poll->wait.entry);
Jens Axboe18d9be12019-09-10 09:13:05 -06001731 io_queue_async_work(req->ctx, req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001732 }
1733 spin_unlock(&poll->head->lock);
1734
1735 list_del_init(&req->list);
1736}
1737
1738static void io_poll_remove_all(struct io_ring_ctx *ctx)
1739{
1740 struct io_kiocb *req;
1741
1742 spin_lock_irq(&ctx->completion_lock);
1743 while (!list_empty(&ctx->cancel_list)) {
1744 req = list_first_entry(&ctx->cancel_list, struct io_kiocb,list);
1745 io_poll_remove_one(req);
1746 }
1747 spin_unlock_irq(&ctx->completion_lock);
1748}
1749
1750/*
1751 * Find a running poll command that matches one specified in sqe->addr,
1752 * and remove it if found.
1753 */
1754static int io_poll_remove(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1755{
1756 struct io_ring_ctx *ctx = req->ctx;
1757 struct io_kiocb *poll_req, *next;
1758 int ret = -ENOENT;
1759
1760 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1761 return -EINVAL;
1762 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
1763 sqe->poll_events)
1764 return -EINVAL;
1765
1766 spin_lock_irq(&ctx->completion_lock);
1767 list_for_each_entry_safe(poll_req, next, &ctx->cancel_list, list) {
1768 if (READ_ONCE(sqe->addr) == poll_req->user_data) {
1769 io_poll_remove_one(poll_req);
1770 ret = 0;
1771 break;
1772 }
1773 }
1774 spin_unlock_irq(&ctx->completion_lock);
1775
Jens Axboec71ffb62019-05-13 20:58:29 -06001776 io_cqring_add_event(req->ctx, sqe->user_data, ret);
Jens Axboeba816ad2019-09-28 11:36:45 -06001777 io_put_req(req, NULL);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001778 return 0;
1779}
1780
Jens Axboe8c838782019-03-12 15:48:16 -06001781static void io_poll_complete(struct io_ring_ctx *ctx, struct io_kiocb *req,
1782 __poll_t mask)
Jens Axboe221c5eb2019-01-17 09:41:58 -07001783{
Jens Axboe8c838782019-03-12 15:48:16 -06001784 req->poll.done = true;
Jens Axboec71ffb62019-05-13 20:58:29 -06001785 io_cqring_fill_event(ctx, req->user_data, mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06001786 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001787}
1788
Jens Axboe561fb042019-10-24 07:25:42 -06001789static void io_poll_complete_work(struct io_wq_work **workptr)
Jens Axboe221c5eb2019-01-17 09:41:58 -07001790{
Jens Axboe561fb042019-10-24 07:25:42 -06001791 struct io_wq_work *work = *workptr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001792 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
1793 struct io_poll_iocb *poll = &req->poll;
1794 struct poll_table_struct pt = { ._key = poll->events };
1795 struct io_ring_ctx *ctx = req->ctx;
1796 __poll_t mask = 0;
1797
Jens Axboe561fb042019-10-24 07:25:42 -06001798 if (work->flags & IO_WQ_WORK_CANCEL)
1799 WRITE_ONCE(poll->canceled, true);
1800
Jens Axboe221c5eb2019-01-17 09:41:58 -07001801 if (!READ_ONCE(poll->canceled))
1802 mask = vfs_poll(poll->file, &pt) & poll->events;
1803
1804 /*
1805 * Note that ->ki_cancel callers also delete iocb from active_reqs after
1806 * calling ->ki_cancel. We need the ctx_lock roundtrip here to
1807 * synchronize with them. In the cancellation case the list_del_init
1808 * itself is not actually needed, but harmless so we keep it in to
1809 * avoid further branches in the fast path.
1810 */
1811 spin_lock_irq(&ctx->completion_lock);
1812 if (!mask && !READ_ONCE(poll->canceled)) {
1813 add_wait_queue(poll->head, &poll->wait);
1814 spin_unlock_irq(&ctx->completion_lock);
1815 return;
1816 }
1817 list_del_init(&req->list);
Jens Axboe8c838782019-03-12 15:48:16 -06001818 io_poll_complete(ctx, req, mask);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001819 spin_unlock_irq(&ctx->completion_lock);
1820
Jens Axboe8c838782019-03-12 15:48:16 -06001821 io_cqring_ev_posted(ctx);
Jens Axboeba816ad2019-09-28 11:36:45 -06001822 io_put_req(req, NULL);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001823}
1824
1825static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
1826 void *key)
1827{
1828 struct io_poll_iocb *poll = container_of(wait, struct io_poll_iocb,
1829 wait);
1830 struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
1831 struct io_ring_ctx *ctx = req->ctx;
1832 __poll_t mask = key_to_poll(key);
Jens Axboe8c838782019-03-12 15:48:16 -06001833 unsigned long flags;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001834
1835 /* for instances that support it check for an event match first: */
Jens Axboe8c838782019-03-12 15:48:16 -06001836 if (mask && !(mask & poll->events))
1837 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001838
1839 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06001840
1841 if (mask && spin_trylock_irqsave(&ctx->completion_lock, flags)) {
1842 list_del(&req->list);
1843 io_poll_complete(ctx, req, mask);
1844 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1845
1846 io_cqring_ev_posted(ctx);
Jens Axboeba816ad2019-09-28 11:36:45 -06001847 io_put_req(req, NULL);
Jens Axboe8c838782019-03-12 15:48:16 -06001848 } else {
Jens Axboe18d9be12019-09-10 09:13:05 -06001849 io_queue_async_work(ctx, req);
Jens Axboe8c838782019-03-12 15:48:16 -06001850 }
1851
Jens Axboe221c5eb2019-01-17 09:41:58 -07001852 return 1;
1853}
1854
1855struct io_poll_table {
1856 struct poll_table_struct pt;
1857 struct io_kiocb *req;
1858 int error;
1859};
1860
1861static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
1862 struct poll_table_struct *p)
1863{
1864 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
1865
1866 if (unlikely(pt->req->poll.head)) {
1867 pt->error = -EINVAL;
1868 return;
1869 }
1870
1871 pt->error = 0;
1872 pt->req->poll.head = head;
1873 add_wait_queue(head, &pt->req->poll.wait);
1874}
1875
1876static int io_poll_add(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1877{
1878 struct io_poll_iocb *poll = &req->poll;
1879 struct io_ring_ctx *ctx = req->ctx;
1880 struct io_poll_table ipt;
Jens Axboe8c838782019-03-12 15:48:16 -06001881 bool cancel = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001882 __poll_t mask;
1883 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001884
1885 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1886 return -EINVAL;
1887 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
1888 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06001889 if (!poll->file)
1890 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001891
Jens Axboe6cc47d12019-09-18 11:18:23 -06001892 req->submit.sqe = NULL;
Jens Axboe561fb042019-10-24 07:25:42 -06001893 INIT_IO_WORK(&req->work, io_poll_complete_work);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001894 events = READ_ONCE(sqe->poll_events);
1895 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
1896
Jens Axboe221c5eb2019-01-17 09:41:58 -07001897 poll->head = NULL;
Jens Axboe8c838782019-03-12 15:48:16 -06001898 poll->done = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001899 poll->canceled = false;
1900
1901 ipt.pt._qproc = io_poll_queue_proc;
1902 ipt.pt._key = poll->events;
1903 ipt.req = req;
1904 ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
1905
1906 /* initialized the list so that we can do list_empty checks */
1907 INIT_LIST_HEAD(&poll->wait.entry);
1908 init_waitqueue_func_entry(&poll->wait, io_poll_wake);
1909
Jens Axboe36703242019-07-25 10:20:18 -06001910 INIT_LIST_HEAD(&req->list);
1911
Jens Axboe221c5eb2019-01-17 09:41:58 -07001912 mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001913
1914 spin_lock_irq(&ctx->completion_lock);
Jens Axboe8c838782019-03-12 15:48:16 -06001915 if (likely(poll->head)) {
1916 spin_lock(&poll->head->lock);
1917 if (unlikely(list_empty(&poll->wait.entry))) {
1918 if (ipt.error)
1919 cancel = true;
1920 ipt.error = 0;
1921 mask = 0;
1922 }
1923 if (mask || ipt.error)
1924 list_del_init(&poll->wait.entry);
1925 else if (cancel)
1926 WRITE_ONCE(poll->canceled, true);
1927 else if (!poll->done) /* actually waiting for an event */
1928 list_add_tail(&req->list, &ctx->cancel_list);
1929 spin_unlock(&poll->head->lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001930 }
Jens Axboe8c838782019-03-12 15:48:16 -06001931 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06001932 ipt.error = 0;
1933 io_poll_complete(ctx, req, mask);
1934 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07001935 spin_unlock_irq(&ctx->completion_lock);
1936
Jens Axboe8c838782019-03-12 15:48:16 -06001937 if (mask) {
1938 io_cqring_ev_posted(ctx);
Jens Axboeba816ad2019-09-28 11:36:45 -06001939 io_put_req(req, NULL);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001940 }
Jens Axboe8c838782019-03-12 15:48:16 -06001941 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001942}
1943
Jens Axboe5262f562019-09-17 12:26:57 -06001944static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
1945{
1946 struct io_ring_ctx *ctx;
Jens Axboe11365042019-10-16 09:08:32 -06001947 struct io_kiocb *req;
Jens Axboe5262f562019-09-17 12:26:57 -06001948 unsigned long flags;
Jens Axboe11365042019-10-16 09:08:32 -06001949 bool comp;
Jens Axboe5262f562019-09-17 12:26:57 -06001950
1951 req = container_of(timer, struct io_kiocb, timeout.timer);
1952 ctx = req->ctx;
1953 atomic_inc(&ctx->cq_timeouts);
1954
1955 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08001956 /*
Jens Axboe11365042019-10-16 09:08:32 -06001957 * We could be racing with timeout deletion. If the list is empty,
1958 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08001959 */
Jens Axboe11365042019-10-16 09:08:32 -06001960 comp = !list_empty(&req->list);
1961 if (comp) {
1962 struct io_kiocb *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06001963
Jens Axboe11365042019-10-16 09:08:32 -06001964 /*
1965 * Adjust the reqs sequence before the current one because it
1966 * will consume a slot in the cq_ring and the the cq_tail
1967 * pointer will be increased, otherwise other timeout reqs may
1968 * return in advance without waiting for enough wait_nr.
1969 */
1970 prev = req;
1971 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
1972 prev->sequence++;
1973
1974 list_del_init(&req->list);
1975 io_cqring_fill_event(ctx, req->user_data, -ETIME);
1976 io_commit_cqring(ctx);
1977 }
Jens Axboe5262f562019-09-17 12:26:57 -06001978 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1979
Jens Axboe11365042019-10-16 09:08:32 -06001980 if (comp) {
1981 io_cqring_ev_posted(ctx);
1982 io_put_req(req, NULL);
1983 }
1984 return HRTIMER_NORESTART;
1985}
1986
1987/*
1988 * Remove or update an existing timeout command
1989 */
1990static int io_timeout_remove(struct io_kiocb *req,
1991 const struct io_uring_sqe *sqe)
1992{
1993 struct io_ring_ctx *ctx = req->ctx;
1994 struct io_kiocb *treq;
1995 int ret = -ENOENT;
1996 __u64 user_data;
1997 unsigned flags;
1998
1999 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2000 return -EINVAL;
2001 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
2002 return -EINVAL;
2003 flags = READ_ONCE(sqe->timeout_flags);
2004 if (flags)
2005 return -EINVAL;
2006
2007 user_data = READ_ONCE(sqe->addr);
2008 spin_lock_irq(&ctx->completion_lock);
2009 list_for_each_entry(treq, &ctx->timeout_list, list) {
2010 if (user_data == treq->user_data) {
2011 list_del_init(&treq->list);
2012 ret = 0;
2013 break;
2014 }
2015 }
2016
2017 /* didn't find timeout */
2018 if (ret) {
2019fill_ev:
2020 io_cqring_fill_event(ctx, req->user_data, ret);
2021 io_commit_cqring(ctx);
2022 spin_unlock_irq(&ctx->completion_lock);
2023 io_cqring_ev_posted(ctx);
2024 io_put_req(req, NULL);
2025 return 0;
2026 }
2027
2028 ret = hrtimer_try_to_cancel(&treq->timeout.timer);
2029 if (ret == -1) {
2030 ret = -EBUSY;
2031 goto fill_ev;
2032 }
2033
2034 io_cqring_fill_event(ctx, req->user_data, 0);
2035 io_cqring_fill_event(ctx, treq->user_data, -ECANCELED);
2036 io_commit_cqring(ctx);
2037 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06002038 io_cqring_ev_posted(ctx);
2039
Jens Axboe11365042019-10-16 09:08:32 -06002040 io_put_req(treq, NULL);
Jens Axboeba816ad2019-09-28 11:36:45 -06002041 io_put_req(req, NULL);
Jens Axboe11365042019-10-16 09:08:32 -06002042 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06002043}
2044
2045static int io_timeout(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2046{
yangerkun5da0fb12019-10-15 21:59:29 +08002047 unsigned count;
Jens Axboe5262f562019-09-17 12:26:57 -06002048 struct io_ring_ctx *ctx = req->ctx;
2049 struct list_head *entry;
Jens Axboea41525a2019-10-15 16:48:15 -06002050 enum hrtimer_mode mode;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06002051 struct timespec64 ts;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002052 unsigned span = 0;
Jens Axboea41525a2019-10-15 16:48:15 -06002053 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06002054
2055 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2056 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06002057 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len != 1)
2058 return -EINVAL;
2059 flags = READ_ONCE(sqe->timeout_flags);
2060 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06002061 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06002062
2063 if (get_timespec64(&ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06002064 return -EFAULT;
2065
Jens Axboe11365042019-10-16 09:08:32 -06002066 if (flags & IORING_TIMEOUT_ABS)
2067 mode = HRTIMER_MODE_ABS;
2068 else
2069 mode = HRTIMER_MODE_REL;
2070
2071 hrtimer_init(&req->timeout.timer, CLOCK_MONOTONIC, mode);
2072
Jens Axboe5262f562019-09-17 12:26:57 -06002073 /*
2074 * sqe->off holds how many events that need to occur for this
2075 * timeout event to be satisfied.
2076 */
2077 count = READ_ONCE(sqe->off);
2078 if (!count)
2079 count = 1;
2080
2081 req->sequence = ctx->cached_sq_head + count - 1;
yangerkun5da0fb12019-10-15 21:59:29 +08002082 /* reuse it to store the count */
2083 req->submit.sequence = count;
Jens Axboe5262f562019-09-17 12:26:57 -06002084 req->flags |= REQ_F_TIMEOUT;
2085
2086 /*
2087 * Insertion sort, ensuring the first entry in the list is always
2088 * the one we need first.
2089 */
Jens Axboe5262f562019-09-17 12:26:57 -06002090 spin_lock_irq(&ctx->completion_lock);
2091 list_for_each_prev(entry, &ctx->timeout_list) {
2092 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
yangerkun5da0fb12019-10-15 21:59:29 +08002093 unsigned nxt_sq_head;
2094 long long tmp, tmp_nxt;
Jens Axboe5262f562019-09-17 12:26:57 -06002095
yangerkun5da0fb12019-10-15 21:59:29 +08002096 /*
2097 * Since cached_sq_head + count - 1 can overflow, use type long
2098 * long to store it.
2099 */
2100 tmp = (long long)ctx->cached_sq_head + count - 1;
2101 nxt_sq_head = nxt->sequence - nxt->submit.sequence + 1;
2102 tmp_nxt = (long long)nxt_sq_head + nxt->submit.sequence - 1;
2103
2104 /*
2105 * cached_sq_head may overflow, and it will never overflow twice
2106 * once there is some timeout req still be valid.
2107 */
2108 if (ctx->cached_sq_head < nxt_sq_head)
yangerkun8b07a652019-10-17 12:12:35 +08002109 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08002110
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002111 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06002112 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002113
2114 /*
2115 * Sequence of reqs after the insert one and itself should
2116 * be adjusted because each timeout req consumes a slot.
2117 */
2118 span++;
2119 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06002120 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002121 req->sequence -= span;
Jens Axboe5262f562019-09-17 12:26:57 -06002122 list_add(&req->list, entry);
2123 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06002124 req->timeout.timer.function = io_timeout_fn;
Jens Axboea41525a2019-10-15 16:48:15 -06002125 hrtimer_start(&req->timeout.timer, timespec64_to_ktime(ts), mode);
Jens Axboe5262f562019-09-17 12:26:57 -06002126 return 0;
2127}
2128
Jens Axboede0617e2019-04-06 21:51:27 -06002129static int io_req_defer(struct io_ring_ctx *ctx, struct io_kiocb *req,
2130 const struct io_uring_sqe *sqe)
2131{
2132 struct io_uring_sqe *sqe_copy;
2133
2134 if (!io_sequence_defer(ctx, req) && list_empty(&ctx->defer_list))
2135 return 0;
2136
2137 sqe_copy = kmalloc(sizeof(*sqe_copy), GFP_KERNEL);
2138 if (!sqe_copy)
2139 return -EAGAIN;
2140
2141 spin_lock_irq(&ctx->completion_lock);
2142 if (!io_sequence_defer(ctx, req) && list_empty(&ctx->defer_list)) {
2143 spin_unlock_irq(&ctx->completion_lock);
2144 kfree(sqe_copy);
2145 return 0;
2146 }
2147
2148 memcpy(sqe_copy, sqe, sizeof(*sqe_copy));
2149 req->submit.sqe = sqe_copy;
2150
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002151 trace_io_uring_defer(ctx, req, false);
Jens Axboede0617e2019-04-06 21:51:27 -06002152 list_add_tail(&req->list, &ctx->defer_list);
2153 spin_unlock_irq(&ctx->completion_lock);
2154 return -EIOCBQUEUED;
2155}
2156
Jens Axboe2b188cc2019-01-07 10:46:33 -07002157static int __io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
Jens Axboeba816ad2019-09-28 11:36:45 -06002158 const struct sqe_submit *s, struct io_kiocb **nxt,
2159 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002160{
Jens Axboee0c5c572019-03-12 10:18:47 -06002161 int ret, opcode;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002162
Jens Axboe9e645e112019-05-10 16:07:28 -06002163 req->user_data = READ_ONCE(s->sqe->user_data);
2164
Jens Axboe2b188cc2019-01-07 10:46:33 -07002165 opcode = READ_ONCE(s->sqe->opcode);
2166 switch (opcode) {
2167 case IORING_OP_NOP:
2168 ret = io_nop(req, req->user_data);
2169 break;
2170 case IORING_OP_READV:
Jens Axboeedafcce2019-01-09 09:16:05 -07002171 if (unlikely(s->sqe->buf_index))
2172 return -EINVAL;
Jens Axboeba816ad2019-09-28 11:36:45 -06002173 ret = io_read(req, s, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002174 break;
2175 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07002176 if (unlikely(s->sqe->buf_index))
2177 return -EINVAL;
Jens Axboeba816ad2019-09-28 11:36:45 -06002178 ret = io_write(req, s, nxt, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07002179 break;
2180 case IORING_OP_READ_FIXED:
Jens Axboeba816ad2019-09-28 11:36:45 -06002181 ret = io_read(req, s, nxt, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07002182 break;
2183 case IORING_OP_WRITE_FIXED:
Jens Axboeba816ad2019-09-28 11:36:45 -06002184 ret = io_write(req, s, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002185 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002186 case IORING_OP_FSYNC:
Jens Axboeba816ad2019-09-28 11:36:45 -06002187 ret = io_fsync(req, s->sqe, nxt, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002188 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002189 case IORING_OP_POLL_ADD:
2190 ret = io_poll_add(req, s->sqe);
2191 break;
2192 case IORING_OP_POLL_REMOVE:
2193 ret = io_poll_remove(req, s->sqe);
2194 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002195 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboeba816ad2019-09-28 11:36:45 -06002196 ret = io_sync_file_range(req, s->sqe, nxt, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002197 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002198 case IORING_OP_SENDMSG:
Jens Axboeba816ad2019-09-28 11:36:45 -06002199 ret = io_sendmsg(req, s->sqe, nxt, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06002200 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06002201 case IORING_OP_RECVMSG:
Jens Axboeba816ad2019-09-28 11:36:45 -06002202 ret = io_recvmsg(req, s->sqe, nxt, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06002203 break;
Jens Axboe5262f562019-09-17 12:26:57 -06002204 case IORING_OP_TIMEOUT:
2205 ret = io_timeout(req, s->sqe);
2206 break;
Jens Axboe11365042019-10-16 09:08:32 -06002207 case IORING_OP_TIMEOUT_REMOVE:
2208 ret = io_timeout_remove(req, s->sqe);
2209 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06002210 case IORING_OP_ACCEPT:
2211 ret = io_accept(req, s->sqe, nxt, force_nonblock);
2212 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002213 default:
2214 ret = -EINVAL;
2215 break;
2216 }
2217
Jens Axboedef596e2019-01-09 08:59:42 -07002218 if (ret)
2219 return ret;
2220
2221 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe9e645e112019-05-10 16:07:28 -06002222 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07002223 return -EAGAIN;
2224
2225 /* workqueue context doesn't hold uring_lock, grab it now */
Jackie Liuba5290c2019-10-09 09:19:59 +08002226 if (s->in_async)
Jens Axboedef596e2019-01-09 08:59:42 -07002227 mutex_lock(&ctx->uring_lock);
2228 io_iopoll_req_issued(req);
Jackie Liuba5290c2019-10-09 09:19:59 +08002229 if (s->in_async)
Jens Axboedef596e2019-01-09 08:59:42 -07002230 mutex_unlock(&ctx->uring_lock);
2231 }
2232
2233 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002234}
2235
Jens Axboe561fb042019-10-24 07:25:42 -06002236static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboe31b51512019-01-18 22:56:34 -07002237{
Jens Axboe561fb042019-10-24 07:25:42 -06002238 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002239 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002240 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe561fb042019-10-24 07:25:42 -06002241 struct sqe_submit *s = &req->submit;
2242 const struct io_uring_sqe *sqe = s->sqe;
2243 struct io_kiocb *nxt = NULL;
2244 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002245
Jens Axboe561fb042019-10-24 07:25:42 -06002246 /* Ensure we clear previously set non-block flag */
2247 req->rw.ki_flags &= ~IOCB_NOWAIT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002248
Jens Axboe561fb042019-10-24 07:25:42 -06002249 if (work->flags & IO_WQ_WORK_CANCEL)
2250 ret = -ECANCELED;
Jens Axboe31b51512019-01-18 22:56:34 -07002251
Jens Axboe561fb042019-10-24 07:25:42 -06002252 if (!ret) {
2253 s->has_user = (work->flags & IO_WQ_WORK_HAS_MM) != 0;
2254 s->in_async = true;
2255 do {
2256 ret = __io_submit_sqe(ctx, req, s, &nxt, false);
2257 /*
2258 * We can get EAGAIN for polled IO even though we're
2259 * forcing a sync submission from here, since we can't
2260 * wait for request slots on the block side.
2261 */
2262 if (ret != -EAGAIN)
2263 break;
2264 cond_resched();
2265 } while (1);
2266 }
Jens Axboe31b51512019-01-18 22:56:34 -07002267
Jens Axboe561fb042019-10-24 07:25:42 -06002268 /* drop submission reference */
2269 io_put_req(req, NULL);
Jens Axboe817869d2019-04-30 14:44:05 -06002270
Jens Axboe561fb042019-10-24 07:25:42 -06002271 if (ret) {
2272 io_cqring_add_event(ctx, sqe->user_data, ret);
Jens Axboeba816ad2019-09-28 11:36:45 -06002273 io_put_req(req, NULL);
Jens Axboeedafcce2019-01-09 09:16:05 -07002274 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002275
Jens Axboe561fb042019-10-24 07:25:42 -06002276 /* async context always use a copy of the sqe */
2277 kfree(sqe);
2278
2279 /* if a dependent link is ready, pass it back */
2280 if (!ret && nxt) {
2281 io_prep_async_work(nxt);
2282 *workptr = &nxt->work;
Jens Axboeedafcce2019-01-09 09:16:05 -07002283 }
Jens Axboe31b51512019-01-18 22:56:34 -07002284}
Jens Axboe2b188cc2019-01-07 10:46:33 -07002285
Jens Axboe09bb8392019-03-13 12:39:28 -06002286static bool io_op_needs_file(const struct io_uring_sqe *sqe)
2287{
2288 int op = READ_ONCE(sqe->opcode);
2289
2290 switch (op) {
2291 case IORING_OP_NOP:
2292 case IORING_OP_POLL_REMOVE:
2293 return false;
2294 default:
2295 return true;
2296 }
2297}
2298
2299static int io_req_set_file(struct io_ring_ctx *ctx, const struct sqe_submit *s,
2300 struct io_submit_state *state, struct io_kiocb *req)
2301{
2302 unsigned flags;
2303 int fd;
2304
2305 flags = READ_ONCE(s->sqe->flags);
2306 fd = READ_ONCE(s->sqe->fd);
2307
Jackie Liu4fe2c962019-09-09 20:50:40 +08002308 if (flags & IOSQE_IO_DRAIN)
Jens Axboede0617e2019-04-06 21:51:27 -06002309 req->flags |= REQ_F_IO_DRAIN;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002310 /*
2311 * All io need record the previous position, if LINK vs DARIN,
2312 * it can be used to mark the position of the first IO in the
2313 * link list.
2314 */
2315 req->sequence = s->sequence;
Jens Axboede0617e2019-04-06 21:51:27 -06002316
Jens Axboe60c112b2019-06-21 10:20:18 -06002317 if (!io_op_needs_file(s->sqe))
Jens Axboe09bb8392019-03-13 12:39:28 -06002318 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06002319
2320 if (flags & IOSQE_FIXED_FILE) {
2321 if (unlikely(!ctx->user_files ||
2322 (unsigned) fd >= ctx->nr_user_files))
2323 return -EBADF;
Jens Axboeb7620122019-10-26 07:22:55 -06002324 fd = array_index_nospec(fd, ctx->nr_user_files);
Jens Axboe08a45172019-10-03 08:11:03 -06002325 if (!ctx->user_files[fd])
2326 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06002327 req->file = ctx->user_files[fd];
2328 req->flags |= REQ_F_FIXED_FILE;
2329 } else {
2330 if (s->needs_fixed_file)
2331 return -EBADF;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002332 trace_io_uring_file_get(ctx, fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06002333 req->file = io_file_get(state, fd);
2334 if (unlikely(!req->file))
2335 return -EBADF;
2336 }
2337
2338 return 0;
2339}
2340
Jens Axboefcb323c2019-10-24 12:39:47 -06002341static int io_grab_files(struct io_ring_ctx *ctx, struct io_kiocb *req)
2342{
2343 int ret = -EBADF;
2344
2345 rcu_read_lock();
2346 spin_lock_irq(&ctx->inflight_lock);
2347 /*
2348 * We use the f_ops->flush() handler to ensure that we can flush
2349 * out work accessing these files if the fd is closed. Check if
2350 * the fd has changed since we started down this path, and disallow
2351 * this operation if it has.
2352 */
2353 if (fcheck(req->submit.ring_fd) == req->submit.ring_file) {
2354 list_add(&req->inflight_entry, &ctx->inflight_list);
2355 req->flags |= REQ_F_INFLIGHT;
2356 req->work.files = current->files;
2357 ret = 0;
2358 }
2359 spin_unlock_irq(&ctx->inflight_lock);
2360 rcu_read_unlock();
2361
2362 return ret;
2363}
2364
Jackie Liu4fe2c962019-09-09 20:50:40 +08002365static int __io_queue_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
Jens Axboebc808bc2019-10-22 13:14:37 -06002366 struct sqe_submit *s)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002367{
Jens Axboee0c5c572019-03-12 10:18:47 -06002368 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002369
Jens Axboeba816ad2019-09-28 11:36:45 -06002370 ret = __io_submit_sqe(ctx, req, s, NULL, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06002371
2372 /*
2373 * We async punt it if the file wasn't marked NOWAIT, or if the file
2374 * doesn't support non-blocking read/write attempts
2375 */
2376 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
2377 (req->flags & REQ_F_MUST_PUNT))) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07002378 struct io_uring_sqe *sqe_copy;
2379
Jackie Liu954dab12019-09-18 10:37:52 +08002380 sqe_copy = kmemdup(s->sqe, sizeof(*sqe_copy), GFP_KERNEL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002381 if (sqe_copy) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07002382 s->sqe = sqe_copy;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002383 memcpy(&req->submit, s, sizeof(*s));
Jens Axboefcb323c2019-10-24 12:39:47 -06002384 if (req->work.flags & IO_WQ_WORK_NEEDS_FILES) {
2385 ret = io_grab_files(ctx, req);
2386 if (ret) {
2387 kfree(sqe_copy);
2388 goto err;
2389 }
2390 }
Jens Axboee65ef562019-03-12 10:16:44 -06002391
2392 /*
2393 * Queued up for async execution, worker will release
Jens Axboe9e645e112019-05-10 16:07:28 -06002394 * submit reference when the iocb is actually submitted.
Jens Axboee65ef562019-03-12 10:16:44 -06002395 */
Jens Axboefcb323c2019-10-24 12:39:47 -06002396 io_queue_async_work(ctx, req);
Jens Axboee65ef562019-03-12 10:16:44 -06002397 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002398 }
2399 }
Jens Axboee65ef562019-03-12 10:16:44 -06002400
2401 /* drop submission reference */
Jens Axboefcb323c2019-10-24 12:39:47 -06002402err:
Jens Axboeba816ad2019-09-28 11:36:45 -06002403 io_put_req(req, NULL);
Jens Axboee65ef562019-03-12 10:16:44 -06002404
2405 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06002406 if (ret) {
2407 io_cqring_add_event(ctx, req->user_data, ret);
2408 if (req->flags & REQ_F_LINK)
2409 req->flags |= REQ_F_FAIL_LINK;
Jens Axboeba816ad2019-09-28 11:36:45 -06002410 io_put_req(req, NULL);
Jens Axboe9e645e112019-05-10 16:07:28 -06002411 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002412
2413 return ret;
2414}
2415
Jackie Liu4fe2c962019-09-09 20:50:40 +08002416static int io_queue_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
Jens Axboebc808bc2019-10-22 13:14:37 -06002417 struct sqe_submit *s)
Jackie Liu4fe2c962019-09-09 20:50:40 +08002418{
2419 int ret;
2420
2421 ret = io_req_defer(ctx, req, s->sqe);
2422 if (ret) {
2423 if (ret != -EIOCBQUEUED) {
Jens Axboeba816ad2019-09-28 11:36:45 -06002424 io_free_req(req, NULL);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002425 io_cqring_add_event(ctx, s->sqe->user_data, ret);
2426 }
2427 return 0;
2428 }
2429
Jens Axboebc808bc2019-10-22 13:14:37 -06002430 return __io_queue_sqe(ctx, req, s);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002431}
2432
2433static int io_queue_link_head(struct io_ring_ctx *ctx, struct io_kiocb *req,
Jens Axboebc808bc2019-10-22 13:14:37 -06002434 struct sqe_submit *s, struct io_kiocb *shadow)
Jackie Liu4fe2c962019-09-09 20:50:40 +08002435{
2436 int ret;
2437 int need_submit = false;
2438
2439 if (!shadow)
Jens Axboebc808bc2019-10-22 13:14:37 -06002440 return io_queue_sqe(ctx, req, s);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002441
2442 /*
2443 * Mark the first IO in link list as DRAIN, let all the following
2444 * IOs enter the defer list. all IO needs to be completed before link
2445 * list.
2446 */
2447 req->flags |= REQ_F_IO_DRAIN;
2448 ret = io_req_defer(ctx, req, s->sqe);
2449 if (ret) {
2450 if (ret != -EIOCBQUEUED) {
Jens Axboeba816ad2019-09-28 11:36:45 -06002451 io_free_req(req, NULL);
Pavel Begunkov7b202382019-10-27 22:10:36 +03002452 __io_free_req(shadow);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002453 io_cqring_add_event(ctx, s->sqe->user_data, ret);
2454 return 0;
2455 }
2456 } else {
2457 /*
2458 * If ret == 0 means that all IOs in front of link io are
2459 * running done. let's queue link head.
2460 */
2461 need_submit = true;
2462 }
2463
2464 /* Insert shadow req to defer_list, blocking next IOs */
2465 spin_lock_irq(&ctx->completion_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002466 trace_io_uring_defer(ctx, shadow, true);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002467 list_add_tail(&shadow->list, &ctx->defer_list);
2468 spin_unlock_irq(&ctx->completion_lock);
2469
2470 if (need_submit)
Jens Axboebc808bc2019-10-22 13:14:37 -06002471 return __io_queue_sqe(ctx, req, s);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002472
2473 return 0;
2474}
2475
Jens Axboe9e645e112019-05-10 16:07:28 -06002476#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK)
2477
2478static void io_submit_sqe(struct io_ring_ctx *ctx, struct sqe_submit *s,
Jens Axboebc808bc2019-10-22 13:14:37 -06002479 struct io_submit_state *state, struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06002480{
2481 struct io_uring_sqe *sqe_copy;
2482 struct io_kiocb *req;
2483 int ret;
2484
2485 /* enforce forwards compatibility on users */
2486 if (unlikely(s->sqe->flags & ~SQE_VALID_FLAGS)) {
2487 ret = -EINVAL;
2488 goto err;
2489 }
2490
2491 req = io_get_req(ctx, state);
2492 if (unlikely(!req)) {
2493 ret = -EAGAIN;
2494 goto err;
2495 }
2496
2497 ret = io_req_set_file(ctx, s, state, req);
2498 if (unlikely(ret)) {
2499err_req:
Jens Axboeba816ad2019-09-28 11:36:45 -06002500 io_free_req(req, NULL);
Jens Axboe9e645e112019-05-10 16:07:28 -06002501err:
2502 io_cqring_add_event(ctx, s->sqe->user_data, ret);
2503 return;
2504 }
2505
Pavel Begunkov84d55dc2019-10-25 12:31:29 +03002506 req->user_data = s->sqe->user_data;
2507
Jens Axboe9e645e112019-05-10 16:07:28 -06002508 /*
2509 * If we already have a head request, queue this one for async
2510 * submittal once the head completes. If we don't have a head but
2511 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
2512 * submitted sync once the chain is complete. If none of those
2513 * conditions are true (normal request), then just queue it.
2514 */
2515 if (*link) {
2516 struct io_kiocb *prev = *link;
2517
2518 sqe_copy = kmemdup(s->sqe, sizeof(*sqe_copy), GFP_KERNEL);
2519 if (!sqe_copy) {
2520 ret = -EAGAIN;
2521 goto err_req;
2522 }
2523
2524 s->sqe = sqe_copy;
2525 memcpy(&req->submit, s, sizeof(*s));
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002526 trace_io_uring_link(ctx, req, prev);
Jens Axboe9e645e112019-05-10 16:07:28 -06002527 list_add_tail(&req->list, &prev->link_list);
2528 } else if (s->sqe->flags & IOSQE_IO_LINK) {
2529 req->flags |= REQ_F_LINK;
2530
2531 memcpy(&req->submit, s, sizeof(*s));
2532 INIT_LIST_HEAD(&req->link_list);
2533 *link = req;
2534 } else {
Jens Axboebc808bc2019-10-22 13:14:37 -06002535 io_queue_sqe(ctx, req, s);
Jens Axboe9e645e112019-05-10 16:07:28 -06002536 }
2537}
2538
Jens Axboe9a56a232019-01-09 09:06:50 -07002539/*
2540 * Batched submission is done, ensure local IO is flushed out.
2541 */
2542static void io_submit_state_end(struct io_submit_state *state)
2543{
2544 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06002545 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07002546 if (state->free_reqs)
2547 kmem_cache_free_bulk(req_cachep, state->free_reqs,
2548 &state->reqs[state->cur_req]);
Jens Axboe9a56a232019-01-09 09:06:50 -07002549}
2550
2551/*
2552 * Start submission side cache.
2553 */
2554static void io_submit_state_start(struct io_submit_state *state,
2555 struct io_ring_ctx *ctx, unsigned max_ios)
2556{
2557 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07002558 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07002559 state->file = NULL;
2560 state->ios_left = max_ios;
2561}
2562
Jens Axboe2b188cc2019-01-07 10:46:33 -07002563static void io_commit_sqring(struct io_ring_ctx *ctx)
2564{
Hristo Venev75b28af2019-08-26 17:23:46 +00002565 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002566
Hristo Venev75b28af2019-08-26 17:23:46 +00002567 if (ctx->cached_sq_head != READ_ONCE(rings->sq.head)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07002568 /*
2569 * Ensure any loads from the SQEs are done at this point,
2570 * since once we write the new head, the application could
2571 * write new data to them.
2572 */
Hristo Venev75b28af2019-08-26 17:23:46 +00002573 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002574 }
2575}
2576
2577/*
Jens Axboe2b188cc2019-01-07 10:46:33 -07002578 * Fetch an sqe, if one is available. Note that s->sqe will point to memory
2579 * that is mapped by userspace. This means that care needs to be taken to
2580 * ensure that reads are stable, as we cannot rely on userspace always
2581 * being a good citizen. If members of the sqe are validated and then later
2582 * used, it's important that those reads are done through READ_ONCE() to
2583 * prevent a re-load down the line.
2584 */
2585static bool io_get_sqring(struct io_ring_ctx *ctx, struct sqe_submit *s)
2586{
Hristo Venev75b28af2019-08-26 17:23:46 +00002587 struct io_rings *rings = ctx->rings;
2588 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002589 unsigned head;
2590
2591 /*
2592 * The cached sq head (or cq tail) serves two purposes:
2593 *
2594 * 1) allows us to batch the cost of updating the user visible
2595 * head updates.
2596 * 2) allows the kernel side to track the head on its own, even
2597 * though the application is the one updating it.
2598 */
2599 head = ctx->cached_sq_head;
Stefan Bühlere523a292019-04-19 11:57:44 +02002600 /* make sure SQ entry isn't read before tail */
Hristo Venev75b28af2019-08-26 17:23:46 +00002601 if (head == smp_load_acquire(&rings->sq.tail))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002602 return false;
2603
Hristo Venev75b28af2019-08-26 17:23:46 +00002604 head = READ_ONCE(sq_array[head & ctx->sq_mask]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002605 if (head < ctx->sq_entries) {
Jens Axboefcb323c2019-10-24 12:39:47 -06002606 s->ring_file = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002607 s->sqe = &ctx->sq_sqes[head];
Jackie Liu8776f3f2019-09-09 20:50:39 +08002608 s->sequence = ctx->cached_sq_head;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002609 ctx->cached_sq_head++;
2610 return true;
2611 }
2612
2613 /* drop invalid entries */
2614 ctx->cached_sq_head++;
Jens Axboe498ccd92019-10-25 10:04:25 -06002615 ctx->cached_sq_dropped++;
2616 WRITE_ONCE(rings->sq_dropped, ctx->cached_sq_dropped);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002617 return false;
2618}
2619
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002620static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03002621 struct mm_struct **mm)
Jens Axboe6c271ce2019-01-10 11:22:30 -07002622{
2623 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002624 struct io_kiocb *link = NULL;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002625 struct io_kiocb *shadow_req = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002626 bool prev_was_link = false;
2627 int i, submitted = 0;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03002628 bool mm_fault = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002629
2630 if (nr > IO_PLUG_THRESHOLD) {
2631 io_submit_state_start(&state, ctx, nr);
2632 statep = &state;
2633 }
2634
2635 for (i = 0; i < nr; i++) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002636 struct sqe_submit s;
2637
2638 if (!io_get_sqring(ctx, &s))
2639 break;
2640
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03002641 if (io_sqe_needs_user(s.sqe) && !*mm) {
2642 mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm);
2643 if (!mm_fault) {
2644 use_mm(ctx->sqo_mm);
2645 *mm = ctx->sqo_mm;
2646 }
2647 }
2648
Jens Axboe9e645e112019-05-10 16:07:28 -06002649 /*
2650 * If previous wasn't linked and we have a linked command,
2651 * that's the end of the chain. Submit the previous link.
2652 */
2653 if (!prev_was_link && link) {
Jens Axboebc808bc2019-10-22 13:14:37 -06002654 io_queue_link_head(ctx, link, &link->submit, shadow_req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002655 link = NULL;
Jackie Liu5f5ad9c2019-09-18 10:37:53 +08002656 shadow_req = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002657 }
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002658 prev_was_link = (s.sqe->flags & IOSQE_IO_LINK) != 0;
Jens Axboe9e645e112019-05-10 16:07:28 -06002659
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002660 if (link && (s.sqe->flags & IOSQE_IO_DRAIN)) {
Jackie Liu4fe2c962019-09-09 20:50:40 +08002661 if (!shadow_req) {
2662 shadow_req = io_get_req(ctx, NULL);
Jackie Liua1041c22019-09-18 17:25:52 +08002663 if (unlikely(!shadow_req))
2664 goto out;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002665 shadow_req->flags |= (REQ_F_IO_DRAIN | REQ_F_SHADOW_DRAIN);
2666 refcount_dec(&shadow_req->refs);
2667 }
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002668 shadow_req->sequence = s.sequence;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002669 }
2670
Jackie Liua1041c22019-09-18 17:25:52 +08002671out:
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03002672 s.has_user = *mm != NULL;
2673 s.in_async = true;
2674 s.needs_fixed_file = true;
2675 trace_io_uring_submit_sqe(ctx, true, true);
2676 io_submit_sqe(ctx, &s, statep, &link);
2677 submitted++;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002678 }
2679
Jens Axboe9e645e112019-05-10 16:07:28 -06002680 if (link)
Jens Axboebc808bc2019-10-22 13:14:37 -06002681 io_queue_link_head(ctx, link, &link->submit, shadow_req);
Jens Axboe6c271ce2019-01-10 11:22:30 -07002682 if (statep)
2683 io_submit_state_end(&state);
2684
2685 return submitted;
2686}
2687
2688static int io_sq_thread(void *data)
2689{
Jens Axboe6c271ce2019-01-10 11:22:30 -07002690 struct io_ring_ctx *ctx = data;
2691 struct mm_struct *cur_mm = NULL;
2692 mm_segment_t old_fs;
2693 DEFINE_WAIT(wait);
2694 unsigned inflight;
2695 unsigned long timeout;
2696
Jackie Liua4c0b3d2019-07-08 13:41:12 +08002697 complete(&ctx->sqo_thread_started);
2698
Jens Axboe6c271ce2019-01-10 11:22:30 -07002699 old_fs = get_fs();
2700 set_fs(USER_DS);
2701
2702 timeout = inflight = 0;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02002703 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002704 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002705
2706 if (inflight) {
2707 unsigned nr_events = 0;
2708
2709 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe2b2ed972019-10-25 10:06:15 -06002710 /*
2711 * inflight is the count of the maximum possible
2712 * entries we submitted, but it can be smaller
2713 * if we dropped some of them. If we don't have
2714 * poll entries available, then we know that we
2715 * have nothing left to poll for. Reset the
2716 * inflight count to zero in that case.
2717 */
2718 mutex_lock(&ctx->uring_lock);
2719 if (!list_empty(&ctx->poll_list))
2720 __io_iopoll_check(ctx, &nr_events, 0);
2721 else
2722 inflight = 0;
2723 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07002724 } else {
2725 /*
2726 * Normal IO, just pretend everything completed.
2727 * We don't have to poll completions for that.
2728 */
2729 nr_events = inflight;
2730 }
2731
2732 inflight -= nr_events;
2733 if (!inflight)
2734 timeout = jiffies + ctx->sq_thread_idle;
2735 }
2736
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002737 to_submit = io_sqring_entries(ctx);
2738 if (!to_submit) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07002739 /*
2740 * We're polling. If we're within the defined idle
2741 * period, then let us spin without work before going
2742 * to sleep.
2743 */
2744 if (inflight || !time_after(jiffies, timeout)) {
Jens Axboe9831a902019-09-19 09:48:55 -06002745 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07002746 continue;
2747 }
2748
2749 /*
2750 * Drop cur_mm before scheduling, we can't hold it for
2751 * long periods (or over schedule()). Do this before
2752 * adding ourselves to the waitqueue, as the unuse/drop
2753 * may sleep.
2754 */
2755 if (cur_mm) {
2756 unuse_mm(cur_mm);
2757 mmput(cur_mm);
2758 cur_mm = NULL;
2759 }
2760
2761 prepare_to_wait(&ctx->sqo_wait, &wait,
2762 TASK_INTERRUPTIBLE);
2763
2764 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00002765 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02002766 /* make sure to read SQ tail after writing flags */
2767 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07002768
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002769 to_submit = io_sqring_entries(ctx);
2770 if (!to_submit) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02002771 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07002772 finish_wait(&ctx->sqo_wait, &wait);
2773 break;
2774 }
2775 if (signal_pending(current))
2776 flush_signals(current);
2777 schedule();
2778 finish_wait(&ctx->sqo_wait, &wait);
2779
Hristo Venev75b28af2019-08-26 17:23:46 +00002780 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002781 continue;
2782 }
2783 finish_wait(&ctx->sqo_wait, &wait);
2784
Hristo Venev75b28af2019-08-26 17:23:46 +00002785 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002786 }
2787
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002788 to_submit = min(to_submit, ctx->sq_entries);
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03002789 inflight += io_submit_sqes(ctx, to_submit, &cur_mm);
Jens Axboe6c271ce2019-01-10 11:22:30 -07002790
2791 /* Commit SQ ring head once we've consumed all SQEs */
2792 io_commit_sqring(ctx);
2793 }
2794
2795 set_fs(old_fs);
2796 if (cur_mm) {
2797 unuse_mm(cur_mm);
2798 mmput(cur_mm);
2799 }
Jens Axboe06058632019-04-13 09:26:03 -06002800
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02002801 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06002802
Jens Axboe6c271ce2019-01-10 11:22:30 -07002803 return 0;
2804}
2805
Jens Axboefcb323c2019-10-24 12:39:47 -06002806static int io_ring_submit(struct io_ring_ctx *ctx, unsigned int to_submit,
2807 struct file *ring_file, int ring_fd)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002808{
Jens Axboe9a56a232019-01-09 09:06:50 -07002809 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002810 struct io_kiocb *link = NULL;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002811 struct io_kiocb *shadow_req = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002812 bool prev_was_link = false;
Jens Axboe5c8b0b52019-04-30 10:16:07 -06002813 int i, submit = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002814
Jens Axboe9a56a232019-01-09 09:06:50 -07002815 if (to_submit > IO_PLUG_THRESHOLD) {
2816 io_submit_state_start(&state, ctx, to_submit);
2817 statep = &state;
2818 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002819
2820 for (i = 0; i < to_submit; i++) {
2821 struct sqe_submit s;
2822
2823 if (!io_get_sqring(ctx, &s))
2824 break;
2825
Jens Axboe9e645e112019-05-10 16:07:28 -06002826 /*
2827 * If previous wasn't linked and we have a linked command,
2828 * that's the end of the chain. Submit the previous link.
2829 */
2830 if (!prev_was_link && link) {
Jens Axboebc808bc2019-10-22 13:14:37 -06002831 io_queue_link_head(ctx, link, &link->submit, shadow_req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002832 link = NULL;
Jackie Liu5f5ad9c2019-09-18 10:37:53 +08002833 shadow_req = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002834 }
2835 prev_was_link = (s.sqe->flags & IOSQE_IO_LINK) != 0;
2836
Jackie Liu4fe2c962019-09-09 20:50:40 +08002837 if (link && (s.sqe->flags & IOSQE_IO_DRAIN)) {
2838 if (!shadow_req) {
2839 shadow_req = io_get_req(ctx, NULL);
Jackie Liua1041c22019-09-18 17:25:52 +08002840 if (unlikely(!shadow_req))
2841 goto out;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002842 shadow_req->flags |= (REQ_F_IO_DRAIN | REQ_F_SHADOW_DRAIN);
2843 refcount_dec(&shadow_req->refs);
2844 }
2845 shadow_req->sequence = s.sequence;
2846 }
2847
Jackie Liua1041c22019-09-18 17:25:52 +08002848out:
Jens Axboefcb323c2019-10-24 12:39:47 -06002849 s.ring_file = ring_file;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002850 s.has_user = true;
Jackie Liuba5290c2019-10-09 09:19:59 +08002851 s.in_async = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002852 s.needs_fixed_file = false;
Jens Axboefcb323c2019-10-24 12:39:47 -06002853 s.ring_fd = ring_fd;
Jens Axboe5c8b0b52019-04-30 10:16:07 -06002854 submit++;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002855 trace_io_uring_submit_sqe(ctx, true, false);
Jens Axboebc808bc2019-10-22 13:14:37 -06002856 io_submit_sqe(ctx, &s, statep, &link);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002857 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002858
Jens Axboe9e645e112019-05-10 16:07:28 -06002859 if (link)
Jens Axboebc808bc2019-10-22 13:14:37 -06002860 io_queue_link_head(ctx, link, &link->submit, shadow_req);
Jens Axboe9a56a232019-01-09 09:06:50 -07002861 if (statep)
2862 io_submit_state_end(statep);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002863
Pavel Begunkov935d1e42019-10-25 12:31:31 +03002864 io_commit_sqring(ctx);
2865
Jens Axboe5c8b0b52019-04-30 10:16:07 -06002866 return submit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002867}
2868
Jens Axboebda52162019-09-24 13:47:15 -06002869struct io_wait_queue {
2870 struct wait_queue_entry wq;
2871 struct io_ring_ctx *ctx;
2872 unsigned to_wait;
2873 unsigned nr_timeouts;
2874};
2875
2876static inline bool io_should_wake(struct io_wait_queue *iowq)
2877{
2878 struct io_ring_ctx *ctx = iowq->ctx;
2879
2880 /*
2881 * Wake up if we have enough events, or if a timeout occured since we
2882 * started waiting. For timeouts, we always want to return to userspace,
2883 * regardless of event count.
2884 */
2885 return io_cqring_events(ctx->rings) >= iowq->to_wait ||
2886 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
2887}
2888
2889static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
2890 int wake_flags, void *key)
2891{
2892 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
2893 wq);
2894
2895 if (!io_should_wake(iowq))
2896 return -1;
2897
2898 return autoremove_wake_function(curr, mode, wake_flags, key);
2899}
2900
Jens Axboe2b188cc2019-01-07 10:46:33 -07002901/*
2902 * Wait until events become available, if we don't already have some. The
2903 * application must reap them itself, as they reside on the shared cq ring.
2904 */
2905static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
2906 const sigset_t __user *sig, size_t sigsz)
2907{
Jens Axboebda52162019-09-24 13:47:15 -06002908 struct io_wait_queue iowq = {
2909 .wq = {
2910 .private = current,
2911 .func = io_wake_function,
2912 .entry = LIST_HEAD_INIT(iowq.wq.entry),
2913 },
2914 .ctx = ctx,
2915 .to_wait = min_events,
2916 };
Hristo Venev75b28af2019-08-26 17:23:46 +00002917 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002918 int ret;
2919
Hristo Venev75b28af2019-08-26 17:23:46 +00002920 if (io_cqring_events(rings) >= min_events)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002921 return 0;
2922
2923 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01002924#ifdef CONFIG_COMPAT
2925 if (in_compat_syscall())
2926 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07002927 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01002928 else
2929#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07002930 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01002931
Jens Axboe2b188cc2019-01-07 10:46:33 -07002932 if (ret)
2933 return ret;
2934 }
2935
Jens Axboebda52162019-09-24 13:47:15 -06002936 ret = 0;
2937 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002938 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06002939 do {
2940 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
2941 TASK_INTERRUPTIBLE);
2942 if (io_should_wake(&iowq))
2943 break;
2944 schedule();
2945 if (signal_pending(current)) {
2946 ret = -ERESTARTSYS;
2947 break;
2948 }
2949 } while (1);
2950 finish_wait(&ctx->wait, &iowq.wq);
2951
Oleg Nesterovb7724342019-07-16 16:29:53 -07002952 restore_saved_sigmask_unless(ret == -ERESTARTSYS);
Oleg Nesterov97abc882019-06-28 12:06:50 -07002953 if (ret == -ERESTARTSYS)
2954 ret = -EINTR;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002955
Hristo Venev75b28af2019-08-26 17:23:46 +00002956 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002957}
2958
Jens Axboe6b063142019-01-10 22:13:58 -07002959static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
2960{
2961#if defined(CONFIG_UNIX)
2962 if (ctx->ring_sock) {
2963 struct sock *sock = ctx->ring_sock->sk;
2964 struct sk_buff *skb;
2965
2966 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
2967 kfree_skb(skb);
2968 }
2969#else
2970 int i;
2971
2972 for (i = 0; i < ctx->nr_user_files; i++)
Jens Axboe08a45172019-10-03 08:11:03 -06002973 if (ctx->user_files[i])
2974 fput(ctx->user_files[i]);
Jens Axboe6b063142019-01-10 22:13:58 -07002975#endif
2976}
2977
2978static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
2979{
2980 if (!ctx->user_files)
2981 return -ENXIO;
2982
2983 __io_sqe_files_unregister(ctx);
2984 kfree(ctx->user_files);
2985 ctx->user_files = NULL;
2986 ctx->nr_user_files = 0;
2987 return 0;
2988}
2989
Jens Axboe6c271ce2019-01-10 11:22:30 -07002990static void io_sq_thread_stop(struct io_ring_ctx *ctx)
2991{
2992 if (ctx->sqo_thread) {
Jackie Liua4c0b3d2019-07-08 13:41:12 +08002993 wait_for_completion(&ctx->sqo_thread_started);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02002994 /*
2995 * The park is a bit of a work-around, without it we get
2996 * warning spews on shutdown with SQPOLL set and affinity
2997 * set to a single CPU.
2998 */
Jens Axboe06058632019-04-13 09:26:03 -06002999 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003000 kthread_stop(ctx->sqo_thread);
3001 ctx->sqo_thread = NULL;
3002 }
3003}
3004
Jens Axboe6b063142019-01-10 22:13:58 -07003005static void io_finish_async(struct io_ring_ctx *ctx)
3006{
Jens Axboe6c271ce2019-01-10 11:22:30 -07003007 io_sq_thread_stop(ctx);
3008
Jens Axboe561fb042019-10-24 07:25:42 -06003009 if (ctx->io_wq) {
3010 io_wq_destroy(ctx->io_wq);
3011 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003012 }
3013}
3014
3015#if defined(CONFIG_UNIX)
3016static void io_destruct_skb(struct sk_buff *skb)
3017{
3018 struct io_ring_ctx *ctx = skb->sk->sk_user_data;
3019
Jens Axboe561fb042019-10-24 07:25:42 -06003020 if (ctx->io_wq)
3021 io_wq_flush(ctx->io_wq);
Jens Axboe8a997342019-10-09 14:40:13 -06003022
Jens Axboe6b063142019-01-10 22:13:58 -07003023 unix_destruct_scm(skb);
3024}
3025
3026/*
3027 * Ensure the UNIX gc is aware of our file set, so we are certain that
3028 * the io_uring can be safely unregistered on process exit, even if we have
3029 * loops in the file referencing.
3030 */
3031static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
3032{
3033 struct sock *sk = ctx->ring_sock->sk;
3034 struct scm_fp_list *fpl;
3035 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06003036 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07003037
3038 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
3039 unsigned long inflight = ctx->user->unix_inflight + nr;
3040
3041 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
3042 return -EMFILE;
3043 }
3044
3045 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
3046 if (!fpl)
3047 return -ENOMEM;
3048
3049 skb = alloc_skb(0, GFP_KERNEL);
3050 if (!skb) {
3051 kfree(fpl);
3052 return -ENOMEM;
3053 }
3054
3055 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07003056
Jens Axboe08a45172019-10-03 08:11:03 -06003057 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07003058 fpl->user = get_uid(ctx->user);
3059 for (i = 0; i < nr; i++) {
Jens Axboe08a45172019-10-03 08:11:03 -06003060 if (!ctx->user_files[i + offset])
3061 continue;
3062 fpl->fp[nr_files] = get_file(ctx->user_files[i + offset]);
3063 unix_inflight(fpl->user, fpl->fp[nr_files]);
3064 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07003065 }
3066
Jens Axboe08a45172019-10-03 08:11:03 -06003067 if (nr_files) {
3068 fpl->max = SCM_MAX_FD;
3069 fpl->count = nr_files;
3070 UNIXCB(skb).fp = fpl;
3071 skb->destructor = io_destruct_skb;
3072 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
3073 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07003074
Jens Axboe08a45172019-10-03 08:11:03 -06003075 for (i = 0; i < nr_files; i++)
3076 fput(fpl->fp[i]);
3077 } else {
3078 kfree_skb(skb);
3079 kfree(fpl);
3080 }
Jens Axboe6b063142019-01-10 22:13:58 -07003081
3082 return 0;
3083}
3084
3085/*
3086 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
3087 * causes regular reference counting to break down. We rely on the UNIX
3088 * garbage collection to take care of this problem for us.
3089 */
3090static int io_sqe_files_scm(struct io_ring_ctx *ctx)
3091{
3092 unsigned left, total;
3093 int ret = 0;
3094
3095 total = 0;
3096 left = ctx->nr_user_files;
3097 while (left) {
3098 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07003099
3100 ret = __io_sqe_files_scm(ctx, this_files, total);
3101 if (ret)
3102 break;
3103 left -= this_files;
3104 total += this_files;
3105 }
3106
3107 if (!ret)
3108 return 0;
3109
3110 while (total < ctx->nr_user_files) {
Jens Axboe08a45172019-10-03 08:11:03 -06003111 if (ctx->user_files[total])
3112 fput(ctx->user_files[total]);
Jens Axboe6b063142019-01-10 22:13:58 -07003113 total++;
3114 }
3115
3116 return ret;
3117}
3118#else
3119static int io_sqe_files_scm(struct io_ring_ctx *ctx)
3120{
3121 return 0;
3122}
3123#endif
3124
3125static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
3126 unsigned nr_args)
3127{
3128 __s32 __user *fds = (__s32 __user *) arg;
3129 int fd, ret = 0;
3130 unsigned i;
3131
3132 if (ctx->user_files)
3133 return -EBUSY;
3134 if (!nr_args)
3135 return -EINVAL;
3136 if (nr_args > IORING_MAX_FIXED_FILES)
3137 return -EMFILE;
3138
3139 ctx->user_files = kcalloc(nr_args, sizeof(struct file *), GFP_KERNEL);
3140 if (!ctx->user_files)
3141 return -ENOMEM;
3142
Jens Axboe08a45172019-10-03 08:11:03 -06003143 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
Jens Axboe6b063142019-01-10 22:13:58 -07003144 ret = -EFAULT;
3145 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
3146 break;
Jens Axboe08a45172019-10-03 08:11:03 -06003147 /* allow sparse sets */
3148 if (fd == -1) {
3149 ret = 0;
3150 continue;
3151 }
Jens Axboe6b063142019-01-10 22:13:58 -07003152
3153 ctx->user_files[i] = fget(fd);
3154
3155 ret = -EBADF;
3156 if (!ctx->user_files[i])
3157 break;
3158 /*
3159 * Don't allow io_uring instances to be registered. If UNIX
3160 * isn't enabled, then this causes a reference cycle and this
3161 * instance can never get freed. If UNIX is enabled we'll
3162 * handle it just fine, but there's still no point in allowing
3163 * a ring fd as it doesn't support regular read/write anyway.
3164 */
3165 if (ctx->user_files[i]->f_op == &io_uring_fops) {
3166 fput(ctx->user_files[i]);
3167 break;
3168 }
Jens Axboe6b063142019-01-10 22:13:58 -07003169 ret = 0;
3170 }
3171
3172 if (ret) {
3173 for (i = 0; i < ctx->nr_user_files; i++)
Jens Axboe08a45172019-10-03 08:11:03 -06003174 if (ctx->user_files[i])
3175 fput(ctx->user_files[i]);
Jens Axboe6b063142019-01-10 22:13:58 -07003176
3177 kfree(ctx->user_files);
Jens Axboe25adf502019-04-03 09:52:40 -06003178 ctx->user_files = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003179 ctx->nr_user_files = 0;
3180 return ret;
3181 }
3182
3183 ret = io_sqe_files_scm(ctx);
3184 if (ret)
3185 io_sqe_files_unregister(ctx);
3186
3187 return ret;
3188}
3189
Jens Axboec3a31e62019-10-03 13:59:56 -06003190static void io_sqe_file_unregister(struct io_ring_ctx *ctx, int index)
3191{
3192#if defined(CONFIG_UNIX)
3193 struct file *file = ctx->user_files[index];
3194 struct sock *sock = ctx->ring_sock->sk;
3195 struct sk_buff_head list, *head = &sock->sk_receive_queue;
3196 struct sk_buff *skb;
3197 int i;
3198
3199 __skb_queue_head_init(&list);
3200
3201 /*
3202 * Find the skb that holds this file in its SCM_RIGHTS. When found,
3203 * remove this entry and rearrange the file array.
3204 */
3205 skb = skb_dequeue(head);
3206 while (skb) {
3207 struct scm_fp_list *fp;
3208
3209 fp = UNIXCB(skb).fp;
3210 for (i = 0; i < fp->count; i++) {
3211 int left;
3212
3213 if (fp->fp[i] != file)
3214 continue;
3215
3216 unix_notinflight(fp->user, fp->fp[i]);
3217 left = fp->count - 1 - i;
3218 if (left) {
3219 memmove(&fp->fp[i], &fp->fp[i + 1],
3220 left * sizeof(struct file *));
3221 }
3222 fp->count--;
3223 if (!fp->count) {
3224 kfree_skb(skb);
3225 skb = NULL;
3226 } else {
3227 __skb_queue_tail(&list, skb);
3228 }
3229 fput(file);
3230 file = NULL;
3231 break;
3232 }
3233
3234 if (!file)
3235 break;
3236
3237 __skb_queue_tail(&list, skb);
3238
3239 skb = skb_dequeue(head);
3240 }
3241
3242 if (skb_peek(&list)) {
3243 spin_lock_irq(&head->lock);
3244 while ((skb = __skb_dequeue(&list)) != NULL)
3245 __skb_queue_tail(head, skb);
3246 spin_unlock_irq(&head->lock);
3247 }
3248#else
3249 fput(ctx->user_files[index]);
3250#endif
3251}
3252
3253static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
3254 int index)
3255{
3256#if defined(CONFIG_UNIX)
3257 struct sock *sock = ctx->ring_sock->sk;
3258 struct sk_buff_head *head = &sock->sk_receive_queue;
3259 struct sk_buff *skb;
3260
3261 /*
3262 * See if we can merge this file into an existing skb SCM_RIGHTS
3263 * file set. If there's no room, fall back to allocating a new skb
3264 * and filling it in.
3265 */
3266 spin_lock_irq(&head->lock);
3267 skb = skb_peek(head);
3268 if (skb) {
3269 struct scm_fp_list *fpl = UNIXCB(skb).fp;
3270
3271 if (fpl->count < SCM_MAX_FD) {
3272 __skb_unlink(skb, head);
3273 spin_unlock_irq(&head->lock);
3274 fpl->fp[fpl->count] = get_file(file);
3275 unix_inflight(fpl->user, fpl->fp[fpl->count]);
3276 fpl->count++;
3277 spin_lock_irq(&head->lock);
3278 __skb_queue_head(head, skb);
3279 } else {
3280 skb = NULL;
3281 }
3282 }
3283 spin_unlock_irq(&head->lock);
3284
3285 if (skb) {
3286 fput(file);
3287 return 0;
3288 }
3289
3290 return __io_sqe_files_scm(ctx, 1, index);
3291#else
3292 return 0;
3293#endif
3294}
3295
3296static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
3297 unsigned nr_args)
3298{
3299 struct io_uring_files_update up;
3300 __s32 __user *fds;
3301 int fd, i, err;
3302 __u32 done;
3303
3304 if (!ctx->user_files)
3305 return -ENXIO;
3306 if (!nr_args)
3307 return -EINVAL;
3308 if (copy_from_user(&up, arg, sizeof(up)))
3309 return -EFAULT;
3310 if (check_add_overflow(up.offset, nr_args, &done))
3311 return -EOVERFLOW;
3312 if (done > ctx->nr_user_files)
3313 return -EINVAL;
3314
3315 done = 0;
3316 fds = (__s32 __user *) up.fds;
3317 while (nr_args) {
3318 err = 0;
3319 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
3320 err = -EFAULT;
3321 break;
3322 }
3323 i = array_index_nospec(up.offset, ctx->nr_user_files);
3324 if (ctx->user_files[i]) {
3325 io_sqe_file_unregister(ctx, i);
3326 ctx->user_files[i] = NULL;
3327 }
3328 if (fd != -1) {
3329 struct file *file;
3330
3331 file = fget(fd);
3332 if (!file) {
3333 err = -EBADF;
3334 break;
3335 }
3336 /*
3337 * Don't allow io_uring instances to be registered. If
3338 * UNIX isn't enabled, then this causes a reference
3339 * cycle and this instance can never get freed. If UNIX
3340 * is enabled we'll handle it just fine, but there's
3341 * still no point in allowing a ring fd as it doesn't
3342 * support regular read/write anyway.
3343 */
3344 if (file->f_op == &io_uring_fops) {
3345 fput(file);
3346 err = -EBADF;
3347 break;
3348 }
3349 ctx->user_files[i] = file;
3350 err = io_sqe_file_register(ctx, file, i);
3351 if (err)
3352 break;
3353 }
3354 nr_args--;
3355 done++;
3356 up.offset++;
3357 }
3358
3359 return done ? done : err;
3360}
3361
Jens Axboe6c271ce2019-01-10 11:22:30 -07003362static int io_sq_offload_start(struct io_ring_ctx *ctx,
3363 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003364{
Jens Axboe561fb042019-10-24 07:25:42 -06003365 unsigned concurrency;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003366 int ret;
3367
Jens Axboe6c271ce2019-01-10 11:22:30 -07003368 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003369 mmgrab(current->mm);
3370 ctx->sqo_mm = current->mm;
3371
Jens Axboe6c271ce2019-01-10 11:22:30 -07003372 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06003373 ret = -EPERM;
3374 if (!capable(CAP_SYS_ADMIN))
3375 goto err;
3376
Jens Axboe917257d2019-04-13 09:28:55 -06003377 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
3378 if (!ctx->sq_thread_idle)
3379 ctx->sq_thread_idle = HZ;
3380
Jens Axboe6c271ce2019-01-10 11:22:30 -07003381 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06003382 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003383
Jens Axboe917257d2019-04-13 09:28:55 -06003384 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06003385 if (cpu >= nr_cpu_ids)
3386 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08003387 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06003388 goto err;
3389
Jens Axboe6c271ce2019-01-10 11:22:30 -07003390 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
3391 ctx, cpu,
3392 "io_uring-sq");
3393 } else {
3394 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
3395 "io_uring-sq");
3396 }
3397 if (IS_ERR(ctx->sqo_thread)) {
3398 ret = PTR_ERR(ctx->sqo_thread);
3399 ctx->sqo_thread = NULL;
3400 goto err;
3401 }
3402 wake_up_process(ctx->sqo_thread);
3403 } else if (p->flags & IORING_SETUP_SQ_AFF) {
3404 /* Can't have SQ_AFF without SQPOLL */
3405 ret = -EINVAL;
3406 goto err;
3407 }
3408
Jens Axboe561fb042019-10-24 07:25:42 -06003409 /* Do QD, or 4 * CPUS, whatever is smallest */
3410 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
3411 ctx->io_wq = io_wq_create(concurrency, ctx->sqo_mm);
3412 if (!ctx->io_wq) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07003413 ret = -ENOMEM;
3414 goto err;
3415 }
3416
3417 return 0;
3418err:
Jens Axboe54a91f32019-09-10 09:15:04 -06003419 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003420 mmdrop(ctx->sqo_mm);
3421 ctx->sqo_mm = NULL;
3422 return ret;
3423}
3424
3425static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
3426{
3427 atomic_long_sub(nr_pages, &user->locked_vm);
3428}
3429
3430static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
3431{
3432 unsigned long page_limit, cur_pages, new_pages;
3433
3434 /* Don't allow more pages than we can safely lock */
3435 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
3436
3437 do {
3438 cur_pages = atomic_long_read(&user->locked_vm);
3439 new_pages = cur_pages + nr_pages;
3440 if (new_pages > page_limit)
3441 return -ENOMEM;
3442 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
3443 new_pages) != cur_pages);
3444
3445 return 0;
3446}
3447
3448static void io_mem_free(void *ptr)
3449{
Mark Rutland52e04ef2019-04-30 17:30:21 +01003450 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003451
Mark Rutland52e04ef2019-04-30 17:30:21 +01003452 if (!ptr)
3453 return;
3454
3455 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003456 if (put_page_testzero(page))
3457 free_compound_page(page);
3458}
3459
3460static void *io_mem_alloc(size_t size)
3461{
3462 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
3463 __GFP_NORETRY;
3464
3465 return (void *) __get_free_pages(gfp_flags, get_order(size));
3466}
3467
Hristo Venev75b28af2019-08-26 17:23:46 +00003468static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
3469 size_t *sq_offset)
3470{
3471 struct io_rings *rings;
3472 size_t off, sq_array_size;
3473
3474 off = struct_size(rings, cqes, cq_entries);
3475 if (off == SIZE_MAX)
3476 return SIZE_MAX;
3477
3478#ifdef CONFIG_SMP
3479 off = ALIGN(off, SMP_CACHE_BYTES);
3480 if (off == 0)
3481 return SIZE_MAX;
3482#endif
3483
3484 sq_array_size = array_size(sizeof(u32), sq_entries);
3485 if (sq_array_size == SIZE_MAX)
3486 return SIZE_MAX;
3487
3488 if (check_add_overflow(off, sq_array_size, &off))
3489 return SIZE_MAX;
3490
3491 if (sq_offset)
3492 *sq_offset = off;
3493
3494 return off;
3495}
3496
Jens Axboe2b188cc2019-01-07 10:46:33 -07003497static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
3498{
Hristo Venev75b28af2019-08-26 17:23:46 +00003499 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003500
Hristo Venev75b28af2019-08-26 17:23:46 +00003501 pages = (size_t)1 << get_order(
3502 rings_size(sq_entries, cq_entries, NULL));
3503 pages += (size_t)1 << get_order(
3504 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07003505
Hristo Venev75b28af2019-08-26 17:23:46 +00003506 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003507}
3508
Jens Axboeedafcce2019-01-09 09:16:05 -07003509static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
3510{
3511 int i, j;
3512
3513 if (!ctx->user_bufs)
3514 return -ENXIO;
3515
3516 for (i = 0; i < ctx->nr_user_bufs; i++) {
3517 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
3518
3519 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbard27c4d3a2019-08-04 19:32:06 -07003520 put_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07003521
3522 if (ctx->account_mem)
3523 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003524 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07003525 imu->nr_bvecs = 0;
3526 }
3527
3528 kfree(ctx->user_bufs);
3529 ctx->user_bufs = NULL;
3530 ctx->nr_user_bufs = 0;
3531 return 0;
3532}
3533
3534static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
3535 void __user *arg, unsigned index)
3536{
3537 struct iovec __user *src;
3538
3539#ifdef CONFIG_COMPAT
3540 if (ctx->compat) {
3541 struct compat_iovec __user *ciovs;
3542 struct compat_iovec ciov;
3543
3544 ciovs = (struct compat_iovec __user *) arg;
3545 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
3546 return -EFAULT;
3547
3548 dst->iov_base = (void __user *) (unsigned long) ciov.iov_base;
3549 dst->iov_len = ciov.iov_len;
3550 return 0;
3551 }
3552#endif
3553 src = (struct iovec __user *) arg;
3554 if (copy_from_user(dst, &src[index], sizeof(*dst)))
3555 return -EFAULT;
3556 return 0;
3557}
3558
3559static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
3560 unsigned nr_args)
3561{
3562 struct vm_area_struct **vmas = NULL;
3563 struct page **pages = NULL;
3564 int i, j, got_pages = 0;
3565 int ret = -EINVAL;
3566
3567 if (ctx->user_bufs)
3568 return -EBUSY;
3569 if (!nr_args || nr_args > UIO_MAXIOV)
3570 return -EINVAL;
3571
3572 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
3573 GFP_KERNEL);
3574 if (!ctx->user_bufs)
3575 return -ENOMEM;
3576
3577 for (i = 0; i < nr_args; i++) {
3578 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
3579 unsigned long off, start, end, ubuf;
3580 int pret, nr_pages;
3581 struct iovec iov;
3582 size_t size;
3583
3584 ret = io_copy_iov(ctx, &iov, arg, i);
3585 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03003586 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07003587
3588 /*
3589 * Don't impose further limits on the size and buffer
3590 * constraints here, we'll -EINVAL later when IO is
3591 * submitted if they are wrong.
3592 */
3593 ret = -EFAULT;
3594 if (!iov.iov_base || !iov.iov_len)
3595 goto err;
3596
3597 /* arbitrary limit, but we need something */
3598 if (iov.iov_len > SZ_1G)
3599 goto err;
3600
3601 ubuf = (unsigned long) iov.iov_base;
3602 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
3603 start = ubuf >> PAGE_SHIFT;
3604 nr_pages = end - start;
3605
3606 if (ctx->account_mem) {
3607 ret = io_account_mem(ctx->user, nr_pages);
3608 if (ret)
3609 goto err;
3610 }
3611
3612 ret = 0;
3613 if (!pages || nr_pages > got_pages) {
3614 kfree(vmas);
3615 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003616 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07003617 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003618 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07003619 sizeof(struct vm_area_struct *),
3620 GFP_KERNEL);
3621 if (!pages || !vmas) {
3622 ret = -ENOMEM;
3623 if (ctx->account_mem)
3624 io_unaccount_mem(ctx->user, nr_pages);
3625 goto err;
3626 }
3627 got_pages = nr_pages;
3628 }
3629
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003630 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07003631 GFP_KERNEL);
3632 ret = -ENOMEM;
3633 if (!imu->bvec) {
3634 if (ctx->account_mem)
3635 io_unaccount_mem(ctx->user, nr_pages);
3636 goto err;
3637 }
3638
3639 ret = 0;
3640 down_read(&current->mm->mmap_sem);
Ira Weiny932f4a62019-05-13 17:17:03 -07003641 pret = get_user_pages(ubuf, nr_pages,
3642 FOLL_WRITE | FOLL_LONGTERM,
3643 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07003644 if (pret == nr_pages) {
3645 /* don't support file backed memory */
3646 for (j = 0; j < nr_pages; j++) {
3647 struct vm_area_struct *vma = vmas[j];
3648
3649 if (vma->vm_file &&
3650 !is_file_hugepages(vma->vm_file)) {
3651 ret = -EOPNOTSUPP;
3652 break;
3653 }
3654 }
3655 } else {
3656 ret = pret < 0 ? pret : -EFAULT;
3657 }
3658 up_read(&current->mm->mmap_sem);
3659 if (ret) {
3660 /*
3661 * if we did partial map, or found file backed vmas,
3662 * release any pages we did get
3663 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07003664 if (pret > 0)
3665 put_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07003666 if (ctx->account_mem)
3667 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003668 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07003669 goto err;
3670 }
3671
3672 off = ubuf & ~PAGE_MASK;
3673 size = iov.iov_len;
3674 for (j = 0; j < nr_pages; j++) {
3675 size_t vec_len;
3676
3677 vec_len = min_t(size_t, size, PAGE_SIZE - off);
3678 imu->bvec[j].bv_page = pages[j];
3679 imu->bvec[j].bv_len = vec_len;
3680 imu->bvec[j].bv_offset = off;
3681 off = 0;
3682 size -= vec_len;
3683 }
3684 /* store original address for later verification */
3685 imu->ubuf = ubuf;
3686 imu->len = iov.iov_len;
3687 imu->nr_bvecs = nr_pages;
3688
3689 ctx->nr_user_bufs++;
3690 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003691 kvfree(pages);
3692 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07003693 return 0;
3694err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003695 kvfree(pages);
3696 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07003697 io_sqe_buffer_unregister(ctx);
3698 return ret;
3699}
3700
Jens Axboe9b402842019-04-11 11:45:41 -06003701static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
3702{
3703 __s32 __user *fds = arg;
3704 int fd;
3705
3706 if (ctx->cq_ev_fd)
3707 return -EBUSY;
3708
3709 if (copy_from_user(&fd, fds, sizeof(*fds)))
3710 return -EFAULT;
3711
3712 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
3713 if (IS_ERR(ctx->cq_ev_fd)) {
3714 int ret = PTR_ERR(ctx->cq_ev_fd);
3715 ctx->cq_ev_fd = NULL;
3716 return ret;
3717 }
3718
3719 return 0;
3720}
3721
3722static int io_eventfd_unregister(struct io_ring_ctx *ctx)
3723{
3724 if (ctx->cq_ev_fd) {
3725 eventfd_ctx_put(ctx->cq_ev_fd);
3726 ctx->cq_ev_fd = NULL;
3727 return 0;
3728 }
3729
3730 return -ENXIO;
3731}
3732
Jens Axboe2b188cc2019-01-07 10:46:33 -07003733static void io_ring_ctx_free(struct io_ring_ctx *ctx)
3734{
Jens Axboe6b063142019-01-10 22:13:58 -07003735 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003736 if (ctx->sqo_mm)
3737 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07003738
3739 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07003740 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07003741 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06003742 io_eventfd_unregister(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07003743
Jens Axboe2b188cc2019-01-07 10:46:33 -07003744#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07003745 if (ctx->ring_sock) {
3746 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07003747 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07003748 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003749#endif
3750
Hristo Venev75b28af2019-08-26 17:23:46 +00003751 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003752 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003753
3754 percpu_ref_exit(&ctx->refs);
3755 if (ctx->account_mem)
3756 io_unaccount_mem(ctx->user,
3757 ring_pages(ctx->sq_entries, ctx->cq_entries));
3758 free_uid(ctx->user);
3759 kfree(ctx);
3760}
3761
3762static __poll_t io_uring_poll(struct file *file, poll_table *wait)
3763{
3764 struct io_ring_ctx *ctx = file->private_data;
3765 __poll_t mask = 0;
3766
3767 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02003768 /*
3769 * synchronizes with barrier from wq_has_sleeper call in
3770 * io_commit_cqring
3771 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07003772 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00003773 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
3774 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003775 mask |= EPOLLOUT | EPOLLWRNORM;
yangerkundaa5de52019-09-24 20:53:34 +08003776 if (READ_ONCE(ctx->rings->cq.head) != ctx->cached_cq_tail)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003777 mask |= EPOLLIN | EPOLLRDNORM;
3778
3779 return mask;
3780}
3781
3782static int io_uring_fasync(int fd, struct file *file, int on)
3783{
3784 struct io_ring_ctx *ctx = file->private_data;
3785
3786 return fasync_helper(fd, file, on, &ctx->cq_fasync);
3787}
3788
3789static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
3790{
3791 mutex_lock(&ctx->uring_lock);
3792 percpu_ref_kill(&ctx->refs);
3793 mutex_unlock(&ctx->uring_lock);
3794
Jens Axboe5262f562019-09-17 12:26:57 -06003795 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003796 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06003797
3798 if (ctx->io_wq)
3799 io_wq_cancel_all(ctx->io_wq);
3800
Jens Axboedef596e2019-01-09 08:59:42 -07003801 io_iopoll_reap_events(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003802 wait_for_completion(&ctx->ctx_done);
3803 io_ring_ctx_free(ctx);
3804}
3805
3806static int io_uring_release(struct inode *inode, struct file *file)
3807{
3808 struct io_ring_ctx *ctx = file->private_data;
3809
3810 file->private_data = NULL;
3811 io_ring_ctx_wait_and_kill(ctx);
3812 return 0;
3813}
3814
Jens Axboefcb323c2019-10-24 12:39:47 -06003815static void io_uring_cancel_files(struct io_ring_ctx *ctx,
3816 struct files_struct *files)
3817{
3818 struct io_kiocb *req;
3819 DEFINE_WAIT(wait);
3820
3821 while (!list_empty_careful(&ctx->inflight_list)) {
3822 enum io_wq_cancel ret = IO_WQ_CANCEL_NOTFOUND;
3823
3824 spin_lock_irq(&ctx->inflight_lock);
3825 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
3826 if (req->work.files == files) {
3827 ret = io_wq_cancel_work(ctx->io_wq, &req->work);
3828 break;
3829 }
3830 }
3831 if (ret == IO_WQ_CANCEL_RUNNING)
3832 prepare_to_wait(&ctx->inflight_wait, &wait,
3833 TASK_UNINTERRUPTIBLE);
3834
3835 spin_unlock_irq(&ctx->inflight_lock);
3836
3837 /*
3838 * We need to keep going until we get NOTFOUND. We only cancel
3839 * one work at the time.
3840 *
3841 * If we get CANCEL_RUNNING, then wait for a work to complete
3842 * before continuing.
3843 */
3844 if (ret == IO_WQ_CANCEL_OK)
3845 continue;
3846 else if (ret != IO_WQ_CANCEL_RUNNING)
3847 break;
3848 schedule();
3849 }
3850}
3851
3852static int io_uring_flush(struct file *file, void *data)
3853{
3854 struct io_ring_ctx *ctx = file->private_data;
3855
3856 io_uring_cancel_files(ctx, data);
3857 if (fatal_signal_pending(current) || (current->flags & PF_EXITING))
3858 io_wq_cancel_all(ctx->io_wq);
3859 return 0;
3860}
3861
Jens Axboe2b188cc2019-01-07 10:46:33 -07003862static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
3863{
3864 loff_t offset = (loff_t) vma->vm_pgoff << PAGE_SHIFT;
3865 unsigned long sz = vma->vm_end - vma->vm_start;
3866 struct io_ring_ctx *ctx = file->private_data;
3867 unsigned long pfn;
3868 struct page *page;
3869 void *ptr;
3870
3871 switch (offset) {
3872 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00003873 case IORING_OFF_CQ_RING:
3874 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003875 break;
3876 case IORING_OFF_SQES:
3877 ptr = ctx->sq_sqes;
3878 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003879 default:
3880 return -EINVAL;
3881 }
3882
3883 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07003884 if (sz > page_size(page))
Jens Axboe2b188cc2019-01-07 10:46:33 -07003885 return -EINVAL;
3886
3887 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
3888 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
3889}
3890
3891SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
3892 u32, min_complete, u32, flags, const sigset_t __user *, sig,
3893 size_t, sigsz)
3894{
3895 struct io_ring_ctx *ctx;
3896 long ret = -EBADF;
3897 int submitted = 0;
3898 struct fd f;
3899
Jens Axboe6c271ce2019-01-10 11:22:30 -07003900 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07003901 return -EINVAL;
3902
3903 f = fdget(fd);
3904 if (!f.file)
3905 return -EBADF;
3906
3907 ret = -EOPNOTSUPP;
3908 if (f.file->f_op != &io_uring_fops)
3909 goto out_fput;
3910
3911 ret = -ENXIO;
3912 ctx = f.file->private_data;
3913 if (!percpu_ref_tryget(&ctx->refs))
3914 goto out_fput;
3915
Jens Axboe6c271ce2019-01-10 11:22:30 -07003916 /*
3917 * For SQ polling, the thread will do all submissions and completions.
3918 * Just return the requested submit count, and wake the thread if
3919 * we were asked to.
3920 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06003921 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003922 if (ctx->flags & IORING_SETUP_SQPOLL) {
3923 if (flags & IORING_ENTER_SQ_WAKEUP)
3924 wake_up(&ctx->sqo_wait);
3925 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06003926 } else if (to_submit) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07003927 to_submit = min(to_submit, ctx->sq_entries);
3928
3929 mutex_lock(&ctx->uring_lock);
Jens Axboefcb323c2019-10-24 12:39:47 -06003930 submitted = io_ring_submit(ctx, to_submit, f.file, fd);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003931 mutex_unlock(&ctx->uring_lock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003932 }
3933 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07003934 unsigned nr_events = 0;
3935
Jens Axboe2b188cc2019-01-07 10:46:33 -07003936 min_complete = min(min_complete, ctx->cq_entries);
3937
Jens Axboedef596e2019-01-09 08:59:42 -07003938 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07003939 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07003940 } else {
3941 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
3942 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003943 }
3944
Pavel Begunkov6805b322019-10-08 02:18:42 +03003945 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003946out_fput:
3947 fdput(f);
3948 return submitted ? submitted : ret;
3949}
3950
3951static const struct file_operations io_uring_fops = {
3952 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06003953 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07003954 .mmap = io_uring_mmap,
3955 .poll = io_uring_poll,
3956 .fasync = io_uring_fasync,
3957};
3958
3959static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
3960 struct io_uring_params *p)
3961{
Hristo Venev75b28af2019-08-26 17:23:46 +00003962 struct io_rings *rings;
3963 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003964
Hristo Venev75b28af2019-08-26 17:23:46 +00003965 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
3966 if (size == SIZE_MAX)
3967 return -EOVERFLOW;
3968
3969 rings = io_mem_alloc(size);
3970 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003971 return -ENOMEM;
3972
Hristo Venev75b28af2019-08-26 17:23:46 +00003973 ctx->rings = rings;
3974 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
3975 rings->sq_ring_mask = p->sq_entries - 1;
3976 rings->cq_ring_mask = p->cq_entries - 1;
3977 rings->sq_ring_entries = p->sq_entries;
3978 rings->cq_ring_entries = p->cq_entries;
3979 ctx->sq_mask = rings->sq_ring_mask;
3980 ctx->cq_mask = rings->cq_ring_mask;
3981 ctx->sq_entries = rings->sq_ring_entries;
3982 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003983
3984 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
3985 if (size == SIZE_MAX)
3986 return -EOVERFLOW;
3987
3988 ctx->sq_sqes = io_mem_alloc(size);
Mark Rutland52e04ef2019-04-30 17:30:21 +01003989 if (!ctx->sq_sqes)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003990 return -ENOMEM;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003991
Jens Axboe2b188cc2019-01-07 10:46:33 -07003992 return 0;
3993}
3994
3995/*
3996 * Allocate an anonymous fd, this is what constitutes the application
3997 * visible backing of an io_uring instance. The application mmaps this
3998 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
3999 * we have to tie this fd to a socket for file garbage collection purposes.
4000 */
4001static int io_uring_get_fd(struct io_ring_ctx *ctx)
4002{
4003 struct file *file;
4004 int ret;
4005
4006#if defined(CONFIG_UNIX)
4007 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
4008 &ctx->ring_sock);
4009 if (ret)
4010 return ret;
4011#endif
4012
4013 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
4014 if (ret < 0)
4015 goto err;
4016
4017 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
4018 O_RDWR | O_CLOEXEC);
4019 if (IS_ERR(file)) {
4020 put_unused_fd(ret);
4021 ret = PTR_ERR(file);
4022 goto err;
4023 }
4024
4025#if defined(CONFIG_UNIX)
4026 ctx->ring_sock->file = file;
Jens Axboe6b063142019-01-10 22:13:58 -07004027 ctx->ring_sock->sk->sk_user_data = ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004028#endif
4029 fd_install(ret, file);
4030 return ret;
4031err:
4032#if defined(CONFIG_UNIX)
4033 sock_release(ctx->ring_sock);
4034 ctx->ring_sock = NULL;
4035#endif
4036 return ret;
4037}
4038
4039static int io_uring_create(unsigned entries, struct io_uring_params *p)
4040{
4041 struct user_struct *user = NULL;
4042 struct io_ring_ctx *ctx;
4043 bool account_mem;
4044 int ret;
4045
4046 if (!entries || entries > IORING_MAX_ENTRIES)
4047 return -EINVAL;
4048
4049 /*
4050 * Use twice as many entries for the CQ ring. It's possible for the
4051 * application to drive a higher depth than the size of the SQ ring,
4052 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06004053 * some flexibility in overcommitting a bit. If the application has
4054 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
4055 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07004056 */
4057 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06004058 if (p->flags & IORING_SETUP_CQSIZE) {
4059 /*
4060 * If IORING_SETUP_CQSIZE is set, we do the same roundup
4061 * to a power-of-two, if it isn't already. We do NOT impose
4062 * any cq vs sq ring sizing.
4063 */
4064 if (p->cq_entries < p->sq_entries || p->cq_entries > IORING_MAX_CQ_ENTRIES)
4065 return -EINVAL;
4066 p->cq_entries = roundup_pow_of_two(p->cq_entries);
4067 } else {
4068 p->cq_entries = 2 * p->sq_entries;
4069 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004070
4071 user = get_uid(current_user());
4072 account_mem = !capable(CAP_IPC_LOCK);
4073
4074 if (account_mem) {
4075 ret = io_account_mem(user,
4076 ring_pages(p->sq_entries, p->cq_entries));
4077 if (ret) {
4078 free_uid(user);
4079 return ret;
4080 }
4081 }
4082
4083 ctx = io_ring_ctx_alloc(p);
4084 if (!ctx) {
4085 if (account_mem)
4086 io_unaccount_mem(user, ring_pages(p->sq_entries,
4087 p->cq_entries));
4088 free_uid(user);
4089 return -ENOMEM;
4090 }
4091 ctx->compat = in_compat_syscall();
4092 ctx->account_mem = account_mem;
4093 ctx->user = user;
4094
4095 ret = io_allocate_scq_urings(ctx, p);
4096 if (ret)
4097 goto err;
4098
Jens Axboe6c271ce2019-01-10 11:22:30 -07004099 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004100 if (ret)
4101 goto err;
4102
Jens Axboe2b188cc2019-01-07 10:46:33 -07004103 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00004104 p->sq_off.head = offsetof(struct io_rings, sq.head);
4105 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
4106 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
4107 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
4108 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
4109 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
4110 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004111
4112 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00004113 p->cq_off.head = offsetof(struct io_rings, cq.head);
4114 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
4115 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
4116 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
4117 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
4118 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06004119
Jens Axboe044c1ab2019-10-28 09:15:33 -06004120 /*
4121 * Install ring fd as the very last thing, so we don't risk someone
4122 * having closed it before we finish setup
4123 */
4124 ret = io_uring_get_fd(ctx);
4125 if (ret < 0)
4126 goto err;
4127
Jens Axboeac90f242019-09-06 10:26:21 -06004128 p->features = IORING_FEAT_SINGLE_MMAP;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004129 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004130 return ret;
4131err:
4132 io_ring_ctx_wait_and_kill(ctx);
4133 return ret;
4134}
4135
4136/*
4137 * Sets up an aio uring context, and returns the fd. Applications asks for a
4138 * ring size, we return the actual sq/cq ring sizes (among other things) in the
4139 * params structure passed in.
4140 */
4141static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
4142{
4143 struct io_uring_params p;
4144 long ret;
4145 int i;
4146
4147 if (copy_from_user(&p, params, sizeof(p)))
4148 return -EFAULT;
4149 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
4150 if (p.resv[i])
4151 return -EINVAL;
4152 }
4153
Jens Axboe6c271ce2019-01-10 11:22:30 -07004154 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe33a107f2019-10-04 12:10:03 -06004155 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE))
Jens Axboe2b188cc2019-01-07 10:46:33 -07004156 return -EINVAL;
4157
4158 ret = io_uring_create(entries, &p);
4159 if (ret < 0)
4160 return ret;
4161
4162 if (copy_to_user(params, &p, sizeof(p)))
4163 return -EFAULT;
4164
4165 return ret;
4166}
4167
4168SYSCALL_DEFINE2(io_uring_setup, u32, entries,
4169 struct io_uring_params __user *, params)
4170{
4171 return io_uring_setup(entries, params);
4172}
4173
Jens Axboeedafcce2019-01-09 09:16:05 -07004174static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
4175 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06004176 __releases(ctx->uring_lock)
4177 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07004178{
4179 int ret;
4180
Jens Axboe35fa71a2019-04-22 10:23:23 -06004181 /*
4182 * We're inside the ring mutex, if the ref is already dying, then
4183 * someone else killed the ctx or is already going through
4184 * io_uring_register().
4185 */
4186 if (percpu_ref_is_dying(&ctx->refs))
4187 return -ENXIO;
4188
Jens Axboeedafcce2019-01-09 09:16:05 -07004189 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06004190
4191 /*
4192 * Drop uring mutex before waiting for references to exit. If another
4193 * thread is currently inside io_uring_enter() it might need to grab
4194 * the uring_lock to make progress. If we hold it here across the drain
4195 * wait, then we can deadlock. It's safe to drop the mutex here, since
4196 * no new references will come in after we've killed the percpu ref.
4197 */
4198 mutex_unlock(&ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07004199 wait_for_completion(&ctx->ctx_done);
Jens Axboeb19062a2019-04-15 10:49:38 -06004200 mutex_lock(&ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07004201
4202 switch (opcode) {
4203 case IORING_REGISTER_BUFFERS:
4204 ret = io_sqe_buffer_register(ctx, arg, nr_args);
4205 break;
4206 case IORING_UNREGISTER_BUFFERS:
4207 ret = -EINVAL;
4208 if (arg || nr_args)
4209 break;
4210 ret = io_sqe_buffer_unregister(ctx);
4211 break;
Jens Axboe6b063142019-01-10 22:13:58 -07004212 case IORING_REGISTER_FILES:
4213 ret = io_sqe_files_register(ctx, arg, nr_args);
4214 break;
4215 case IORING_UNREGISTER_FILES:
4216 ret = -EINVAL;
4217 if (arg || nr_args)
4218 break;
4219 ret = io_sqe_files_unregister(ctx);
4220 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06004221 case IORING_REGISTER_FILES_UPDATE:
4222 ret = io_sqe_files_update(ctx, arg, nr_args);
4223 break;
Jens Axboe9b402842019-04-11 11:45:41 -06004224 case IORING_REGISTER_EVENTFD:
4225 ret = -EINVAL;
4226 if (nr_args != 1)
4227 break;
4228 ret = io_eventfd_register(ctx, arg);
4229 break;
4230 case IORING_UNREGISTER_EVENTFD:
4231 ret = -EINVAL;
4232 if (arg || nr_args)
4233 break;
4234 ret = io_eventfd_unregister(ctx);
4235 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07004236 default:
4237 ret = -EINVAL;
4238 break;
4239 }
4240
4241 /* bring the ctx back to life */
4242 reinit_completion(&ctx->ctx_done);
4243 percpu_ref_reinit(&ctx->refs);
4244 return ret;
4245}
4246
4247SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
4248 void __user *, arg, unsigned int, nr_args)
4249{
4250 struct io_ring_ctx *ctx;
4251 long ret = -EBADF;
4252 struct fd f;
4253
4254 f = fdget(fd);
4255 if (!f.file)
4256 return -EBADF;
4257
4258 ret = -EOPNOTSUPP;
4259 if (f.file->f_op != &io_uring_fops)
4260 goto out_fput;
4261
4262 ctx = f.file->private_data;
4263
4264 mutex_lock(&ctx->uring_lock);
4265 ret = __io_uring_register(ctx, opcode, arg, nr_args);
4266 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004267 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
4268 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07004269out_fput:
4270 fdput(f);
4271 return ret;
4272}
4273
Jens Axboe2b188cc2019-01-07 10:46:33 -07004274static int __init io_uring_init(void)
4275{
4276 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
4277 return 0;
4278};
4279__initcall(io_uring_init);