blob: f83465fc1ed128625d39cb0f214d0c75dc171f18 [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>
59#include <linux/workqueue.h>
Jens Axboe6c271ce2019-01-10 11:22:30 -070060#include <linux/kthread.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070061#include <linux/blkdev.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070062#include <linux/bvec.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070063#include <linux/net.h>
64#include <net/sock.h>
65#include <net/af_unix.h>
Jens Axboe6b063142019-01-10 22:13:58 -070066#include <net/scm.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070067#include <linux/anon_inodes.h>
68#include <linux/sched/mm.h>
69#include <linux/uaccess.h>
70#include <linux/nospec.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070071#include <linux/sizes.h>
72#include <linux/hugetlb.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070073
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020074#define CREATE_TRACE_POINTS
75#include <trace/events/io_uring.h>
76
Jens Axboe2b188cc2019-01-07 10:46:33 -070077#include <uapi/linux/io_uring.h>
78
79#include "internal.h"
80
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 Axboe31b51512019-01-18 22:56:34 -0700168struct async_list {
169 spinlock_t lock;
170 atomic_t cnt;
171 struct list_head list;
172
173 struct file *file;
Jens Axboe6d5d5ac2019-09-11 10:16:13 -0600174 off_t io_start;
Zhengyuan Liu9310a7ba2019-07-22 10:23:27 +0800175 size_t io_len;
Jens Axboe31b51512019-01-18 22:56:34 -0700176};
177
Jens Axboe2b188cc2019-01-07 10:46:33 -0700178struct io_ring_ctx {
179 struct {
180 struct percpu_ref refs;
181 } ____cacheline_aligned_in_smp;
182
183 struct {
184 unsigned int flags;
185 bool compat;
186 bool account_mem;
187
Hristo Venev75b28af2019-08-26 17:23:46 +0000188 /*
189 * Ring buffer of indices into array of io_uring_sqe, which is
190 * mmapped by the application using the IORING_OFF_SQES offset.
191 *
192 * This indirection could e.g. be used to assign fixed
193 * io_uring_sqe entries to operations and only submit them to
194 * the queue when needed.
195 *
196 * The kernel modifies neither the indices array nor the entries
197 * array.
198 */
199 u32 *sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700200 unsigned cached_sq_head;
201 unsigned sq_entries;
202 unsigned sq_mask;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700203 unsigned sq_thread_idle;
Jens Axboe498ccd92019-10-25 10:04:25 -0600204 unsigned cached_sq_dropped;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700205 struct io_uring_sqe *sq_sqes;
Jens Axboede0617e2019-04-06 21:51:27 -0600206
207 struct list_head defer_list;
Jens Axboe5262f562019-09-17 12:26:57 -0600208 struct list_head timeout_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700209 } ____cacheline_aligned_in_smp;
210
211 /* IO offload */
Jens Axboe54a91f32019-09-10 09:15:04 -0600212 struct workqueue_struct *sqo_wq[2];
Jens Axboe6c271ce2019-01-10 11:22:30 -0700213 struct task_struct *sqo_thread; /* if using sq thread polling */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700214 struct mm_struct *sqo_mm;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700215 wait_queue_head_t sqo_wait;
Jackie Liua4c0b3d2019-07-08 13:41:12 +0800216 struct completion sqo_thread_started;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700217
218 struct {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700219 unsigned cached_cq_tail;
Jens Axboe498ccd92019-10-25 10:04:25 -0600220 atomic_t cached_cq_overflow;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700221 unsigned cq_entries;
222 unsigned cq_mask;
223 struct wait_queue_head cq_wait;
224 struct fasync_struct *cq_fasync;
Jens Axboe9b402842019-04-11 11:45:41 -0600225 struct eventfd_ctx *cq_ev_fd;
Jens Axboe5262f562019-09-17 12:26:57 -0600226 atomic_t cq_timeouts;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700227 } ____cacheline_aligned_in_smp;
228
Hristo Venev75b28af2019-08-26 17:23:46 +0000229 struct io_rings *rings;
230
Jens Axboe6b063142019-01-10 22:13:58 -0700231 /*
232 * If used, fixed file set. Writers must ensure that ->refs is dead,
233 * readers must ensure that ->refs is alive as long as the file* is
234 * used. Only updated through io_uring_register(2).
235 */
236 struct file **user_files;
237 unsigned nr_user_files;
238
Jens Axboeedafcce2019-01-09 09:16:05 -0700239 /* if used, fixed mapped user buffers */
240 unsigned nr_user_bufs;
241 struct io_mapped_ubuf *user_bufs;
242
Jens Axboe2b188cc2019-01-07 10:46:33 -0700243 struct user_struct *user;
244
245 struct completion ctx_done;
246
247 struct {
248 struct mutex uring_lock;
249 wait_queue_head_t wait;
250 } ____cacheline_aligned_in_smp;
251
252 struct {
253 spinlock_t completion_lock;
Jens Axboedef596e2019-01-09 08:59:42 -0700254 bool poll_multi_file;
255 /*
256 * ->poll_list is protected by the ctx->uring_lock for
257 * io_uring instances that don't use IORING_SETUP_SQPOLL.
258 * For SQPOLL, only the single threaded io_sq_thread() will
259 * manipulate the list, hence no extra locking is needed there.
260 */
261 struct list_head poll_list;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700262 struct list_head cancel_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700263 } ____cacheline_aligned_in_smp;
264
Jens Axboe31b51512019-01-18 22:56:34 -0700265 struct async_list pending_async[2];
266
Jens Axboe2b188cc2019-01-07 10:46:33 -0700267#if defined(CONFIG_UNIX)
268 struct socket *ring_sock;
269#endif
270};
271
272struct sqe_submit {
273 const struct io_uring_sqe *sqe;
274 unsigned short index;
Jackie Liu8776f3f2019-09-09 20:50:39 +0800275 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700276 bool has_user;
Jackie Liuba5290c2019-10-09 09:19:59 +0800277 bool in_async;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700278 bool needs_fixed_file;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700279};
280
Jens Axboe09bb8392019-03-13 12:39:28 -0600281/*
282 * First field must be the file pointer in all the
283 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
284 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700285struct io_poll_iocb {
286 struct file *file;
287 struct wait_queue_head *head;
288 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600289 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700290 bool canceled;
291 struct wait_queue_entry wait;
292};
293
Jens Axboe5262f562019-09-17 12:26:57 -0600294struct io_timeout {
295 struct file *file;
296 struct hrtimer timer;
297};
298
Jens Axboe09bb8392019-03-13 12:39:28 -0600299/*
300 * NOTE! Each of the iocb union members has the file pointer
301 * as the first entry in their struct definition. So you can
302 * access the file pointer through any of the sub-structs,
303 * or directly as just 'ki_filp' in this struct.
304 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700305struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700306 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600307 struct file *file;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700308 struct kiocb rw;
309 struct io_poll_iocb poll;
Jens Axboe5262f562019-09-17 12:26:57 -0600310 struct io_timeout timeout;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700311 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700312
313 struct sqe_submit submit;
314
315 struct io_ring_ctx *ctx;
316 struct list_head list;
Jens Axboe9e645e112019-05-10 16:07:28 -0600317 struct list_head link_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700318 unsigned int flags;
Jens Axboec16361c2019-01-17 08:39:48 -0700319 refcount_t refs;
Stefan Bühler8449eed2019-04-27 20:34:19 +0200320#define REQ_F_NOWAIT 1 /* must not punt to workers */
Jens Axboedef596e2019-01-09 08:59:42 -0700321#define REQ_F_IOPOLL_COMPLETED 2 /* polled IO has completed */
Jens Axboe6b063142019-01-10 22:13:58 -0700322#define REQ_F_FIXED_FILE 4 /* ctx owns file */
Jens Axboe31b51512019-01-18 22:56:34 -0700323#define REQ_F_SEQ_PREV 8 /* sequential with previous */
Stefan Bühlere2033e32019-05-11 19:08:01 +0200324#define REQ_F_IO_DRAIN 16 /* drain existing IO first */
325#define REQ_F_IO_DRAINED 32 /* drain done */
Jens Axboe9e645e112019-05-10 16:07:28 -0600326#define REQ_F_LINK 64 /* linked sqes */
Zhengyuan Liuf7b76ac2019-07-16 23:26:14 +0800327#define REQ_F_LINK_DONE 128 /* linked sqes done */
328#define REQ_F_FAIL_LINK 256 /* fail rest of links */
Jackie Liu4fe2c962019-09-09 20:50:40 +0800329#define REQ_F_SHADOW_DRAIN 512 /* link-drain shadow req */
Jens Axboe5262f562019-09-17 12:26:57 -0600330#define REQ_F_TIMEOUT 1024 /* timeout request */
Jens Axboe491381ce2019-10-17 09:20:46 -0600331#define REQ_F_ISREG 2048 /* regular file */
332#define REQ_F_MUST_PUNT 4096 /* must be punted even for NONBLOCK */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700333 u64 user_data;
Jens Axboe9e645e112019-05-10 16:07:28 -0600334 u32 result;
Jens Axboede0617e2019-04-06 21:51:27 -0600335 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700336
337 struct work_struct work;
338};
339
340#define IO_PLUG_THRESHOLD 2
Jens Axboedef596e2019-01-09 08:59:42 -0700341#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700342
Jens Axboe9a56a232019-01-09 09:06:50 -0700343struct io_submit_state {
344 struct blk_plug plug;
345
346 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700347 * io_kiocb alloc cache
348 */
349 void *reqs[IO_IOPOLL_BATCH];
350 unsigned int free_reqs;
351 unsigned int cur_req;
352
353 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700354 * File reference cache
355 */
356 struct file *file;
357 unsigned int fd;
358 unsigned int has_refs;
359 unsigned int used_refs;
360 unsigned int ios_left;
361};
362
Jens Axboede0617e2019-04-06 21:51:27 -0600363static void io_sq_wq_submit_work(struct work_struct *work);
Jens Axboe5262f562019-09-17 12:26:57 -0600364static void io_cqring_fill_event(struct io_ring_ctx *ctx, u64 ki_user_data,
365 long res);
Jackie Liu4fe2c962019-09-09 20:50:40 +0800366static void __io_free_req(struct io_kiocb *req);
Jens Axboede0617e2019-04-06 21:51:27 -0600367
Jens Axboe2b188cc2019-01-07 10:46:33 -0700368static struct kmem_cache *req_cachep;
369
370static const struct file_operations io_uring_fops;
371
372struct sock *io_uring_get_socket(struct file *file)
373{
374#if defined(CONFIG_UNIX)
375 if (file->f_op == &io_uring_fops) {
376 struct io_ring_ctx *ctx = file->private_data;
377
378 return ctx->ring_sock->sk;
379 }
380#endif
381 return NULL;
382}
383EXPORT_SYMBOL(io_uring_get_socket);
384
385static void io_ring_ctx_ref_free(struct percpu_ref *ref)
386{
387 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
388
389 complete(&ctx->ctx_done);
390}
391
392static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
393{
394 struct io_ring_ctx *ctx;
Jens Axboe31b51512019-01-18 22:56:34 -0700395 int i;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700396
397 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
398 if (!ctx)
399 return NULL;
400
Roman Gushchin21482892019-05-07 10:01:48 -0700401 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
402 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700403 kfree(ctx);
404 return NULL;
405 }
406
407 ctx->flags = p->flags;
408 init_waitqueue_head(&ctx->cq_wait);
409 init_completion(&ctx->ctx_done);
Jackie Liua4c0b3d2019-07-08 13:41:12 +0800410 init_completion(&ctx->sqo_thread_started);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700411 mutex_init(&ctx->uring_lock);
412 init_waitqueue_head(&ctx->wait);
Jens Axboe31b51512019-01-18 22:56:34 -0700413 for (i = 0; i < ARRAY_SIZE(ctx->pending_async); i++) {
414 spin_lock_init(&ctx->pending_async[i].lock);
415 INIT_LIST_HEAD(&ctx->pending_async[i].list);
416 atomic_set(&ctx->pending_async[i].cnt, 0);
417 }
Jens Axboe2b188cc2019-01-07 10:46:33 -0700418 spin_lock_init(&ctx->completion_lock);
Jens Axboedef596e2019-01-09 08:59:42 -0700419 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboe221c5eb2019-01-17 09:41:58 -0700420 INIT_LIST_HEAD(&ctx->cancel_list);
Jens Axboede0617e2019-04-06 21:51:27 -0600421 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -0600422 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700423 return ctx;
424}
425
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600426static inline bool __io_sequence_defer(struct io_ring_ctx *ctx,
427 struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -0600428{
Jens Axboe498ccd92019-10-25 10:04:25 -0600429 return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped
430 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboede0617e2019-04-06 21:51:27 -0600431}
432
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600433static inline bool io_sequence_defer(struct io_ring_ctx *ctx,
434 struct io_kiocb *req)
435{
436 if ((req->flags & (REQ_F_IO_DRAIN|REQ_F_IO_DRAINED)) != REQ_F_IO_DRAIN)
437 return false;
438
439 return __io_sequence_defer(ctx, req);
440}
441
442static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -0600443{
444 struct io_kiocb *req;
445
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600446 req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list);
447 if (req && !io_sequence_defer(ctx, req)) {
Jens Axboede0617e2019-04-06 21:51:27 -0600448 list_del_init(&req->list);
449 return req;
450 }
451
452 return NULL;
453}
454
Jens Axboe5262f562019-09-17 12:26:57 -0600455static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx)
456{
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600457 struct io_kiocb *req;
458
459 req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list);
460 if (req && !__io_sequence_defer(ctx, req)) {
461 list_del_init(&req->list);
462 return req;
463 }
464
465 return NULL;
Jens Axboe5262f562019-09-17 12:26:57 -0600466}
467
Jens Axboede0617e2019-04-06 21:51:27 -0600468static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700469{
Hristo Venev75b28af2019-08-26 17:23:46 +0000470 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700471
Hristo Venev75b28af2019-08-26 17:23:46 +0000472 if (ctx->cached_cq_tail != READ_ONCE(rings->cq.tail)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700473 /* order cqe stores with ring update */
Hristo Venev75b28af2019-08-26 17:23:46 +0000474 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700475
Jens Axboe2b188cc2019-01-07 10:46:33 -0700476 if (wq_has_sleeper(&ctx->cq_wait)) {
477 wake_up_interruptible(&ctx->cq_wait);
478 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
479 }
480 }
481}
482
Jens Axboe18d9be12019-09-10 09:13:05 -0600483static inline void io_queue_async_work(struct io_ring_ctx *ctx,
484 struct io_kiocb *req)
485{
Jens Axboe6cc47d12019-09-18 11:18:23 -0600486 int rw = 0;
Jens Axboe54a91f32019-09-10 09:15:04 -0600487
Jens Axboe6cc47d12019-09-18 11:18:23 -0600488 if (req->submit.sqe) {
489 switch (req->submit.sqe->opcode) {
490 case IORING_OP_WRITEV:
491 case IORING_OP_WRITE_FIXED:
492 rw = !(req->rw.ki_flags & IOCB_DIRECT);
493 break;
494 }
Jens Axboe54a91f32019-09-10 09:15:04 -0600495 }
496
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +0200497 trace_io_uring_queue_async_work(ctx, rw, req, &req->work, req->flags);
Jens Axboe54a91f32019-09-10 09:15:04 -0600498 queue_work(ctx->sqo_wq[rw], &req->work);
Jens Axboe18d9be12019-09-10 09:13:05 -0600499}
500
Jens Axboe5262f562019-09-17 12:26:57 -0600501static void io_kill_timeout(struct io_kiocb *req)
502{
503 int ret;
504
505 ret = hrtimer_try_to_cancel(&req->timeout.timer);
506 if (ret != -1) {
507 atomic_inc(&req->ctx->cq_timeouts);
508 list_del(&req->list);
509 io_cqring_fill_event(req->ctx, req->user_data, 0);
510 __io_free_req(req);
511 }
512}
513
514static void io_kill_timeouts(struct io_ring_ctx *ctx)
515{
516 struct io_kiocb *req, *tmp;
517
518 spin_lock_irq(&ctx->completion_lock);
519 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
520 io_kill_timeout(req);
521 spin_unlock_irq(&ctx->completion_lock);
522}
523
Jens Axboede0617e2019-04-06 21:51:27 -0600524static void io_commit_cqring(struct io_ring_ctx *ctx)
525{
526 struct io_kiocb *req;
527
Jens Axboe5262f562019-09-17 12:26:57 -0600528 while ((req = io_get_timeout_req(ctx)) != NULL)
529 io_kill_timeout(req);
530
Jens Axboede0617e2019-04-06 21:51:27 -0600531 __io_commit_cqring(ctx);
532
533 while ((req = io_get_deferred_req(ctx)) != NULL) {
Jackie Liu4fe2c962019-09-09 20:50:40 +0800534 if (req->flags & REQ_F_SHADOW_DRAIN) {
535 /* Just for drain, free it. */
536 __io_free_req(req);
537 continue;
538 }
Jens Axboede0617e2019-04-06 21:51:27 -0600539 req->flags |= REQ_F_IO_DRAINED;
Jens Axboe18d9be12019-09-10 09:13:05 -0600540 io_queue_async_work(ctx, req);
Jens Axboede0617e2019-04-06 21:51:27 -0600541 }
542}
543
Jens Axboe2b188cc2019-01-07 10:46:33 -0700544static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
545{
Hristo Venev75b28af2019-08-26 17:23:46 +0000546 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700547 unsigned tail;
548
549 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +0200550 /*
551 * writes to the cq entry need to come after reading head; the
552 * control dependency is enough as we're using WRITE_ONCE to
553 * fill the cq entry
554 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000555 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700556 return NULL;
557
558 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +0000559 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -0700560}
561
562static void io_cqring_fill_event(struct io_ring_ctx *ctx, u64 ki_user_data,
Jens Axboec71ffb62019-05-13 20:58:29 -0600563 long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700564{
565 struct io_uring_cqe *cqe;
566
567 /*
568 * If we can't get a cq entry, userspace overflowed the
569 * submission (by quite a lot). Increment the overflow count in
570 * the ring.
571 */
572 cqe = io_get_cqring(ctx);
573 if (cqe) {
574 WRITE_ONCE(cqe->user_data, ki_user_data);
575 WRITE_ONCE(cqe->res, res);
Jens Axboec71ffb62019-05-13 20:58:29 -0600576 WRITE_ONCE(cqe->flags, 0);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700577 } else {
Jens Axboe498ccd92019-10-25 10:04:25 -0600578 WRITE_ONCE(ctx->rings->cq_overflow,
579 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe2b188cc2019-01-07 10:46:33 -0700580 }
581}
582
Jens Axboe8c838782019-03-12 15:48:16 -0600583static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
584{
585 if (waitqueue_active(&ctx->wait))
586 wake_up(&ctx->wait);
587 if (waitqueue_active(&ctx->sqo_wait))
588 wake_up(&ctx->sqo_wait);
Jens Axboe9b402842019-04-11 11:45:41 -0600589 if (ctx->cq_ev_fd)
590 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -0600591}
592
593static void io_cqring_add_event(struct io_ring_ctx *ctx, u64 user_data,
Jens Axboec71ffb62019-05-13 20:58:29 -0600594 long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700595{
596 unsigned long flags;
597
598 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboec71ffb62019-05-13 20:58:29 -0600599 io_cqring_fill_event(ctx, user_data, res);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700600 io_commit_cqring(ctx);
601 spin_unlock_irqrestore(&ctx->completion_lock, flags);
602
Jens Axboe8c838782019-03-12 15:48:16 -0600603 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700604}
605
Jens Axboe2579f912019-01-09 09:10:43 -0700606static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
607 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700608{
Jens Axboefd6fab22019-03-14 16:30:06 -0600609 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700610 struct io_kiocb *req;
611
612 if (!percpu_ref_tryget(&ctx->refs))
613 return NULL;
614
Jens Axboe2579f912019-01-09 09:10:43 -0700615 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -0600616 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -0700617 if (unlikely(!req))
618 goto out;
619 } else if (!state->free_reqs) {
620 size_t sz;
621 int ret;
622
623 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -0600624 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
625
626 /*
627 * Bulk alloc is all-or-nothing. If we fail to get a batch,
628 * retry single alloc to be on the safe side.
629 */
630 if (unlikely(ret <= 0)) {
631 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
632 if (!state->reqs[0])
633 goto out;
634 ret = 1;
635 }
Jens Axboe2579f912019-01-09 09:10:43 -0700636 state->free_reqs = ret - 1;
637 state->cur_req = 1;
638 req = state->reqs[0];
639 } else {
640 req = state->reqs[state->cur_req];
641 state->free_reqs--;
642 state->cur_req++;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700643 }
644
Jens Axboe60c112b2019-06-21 10:20:18 -0600645 req->file = NULL;
Jens Axboe2579f912019-01-09 09:10:43 -0700646 req->ctx = ctx;
647 req->flags = 0;
Jens Axboee65ef562019-03-12 10:16:44 -0600648 /* one is dropped after submission, the other at completion */
649 refcount_set(&req->refs, 2);
Jens Axboe9e645e112019-05-10 16:07:28 -0600650 req->result = 0;
Jens Axboe2579f912019-01-09 09:10:43 -0700651 return req;
652out:
Pavel Begunkov6805b322019-10-08 02:18:42 +0300653 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700654 return NULL;
655}
656
Jens Axboedef596e2019-01-09 08:59:42 -0700657static void io_free_req_many(struct io_ring_ctx *ctx, void **reqs, int *nr)
658{
659 if (*nr) {
660 kmem_cache_free_bulk(req_cachep, *nr, reqs);
Pavel Begunkov6805b322019-10-08 02:18:42 +0300661 percpu_ref_put_many(&ctx->refs, *nr);
Jens Axboedef596e2019-01-09 08:59:42 -0700662 *nr = 0;
663 }
664}
665
Jens Axboe9e645e112019-05-10 16:07:28 -0600666static void __io_free_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700667{
Jens Axboe09bb8392019-03-13 12:39:28 -0600668 if (req->file && !(req->flags & REQ_F_FIXED_FILE))
669 fput(req->file);
Pavel Begunkov6805b322019-10-08 02:18:42 +0300670 percpu_ref_put(&req->ctx->refs);
Jens Axboee65ef562019-03-12 10:16:44 -0600671 kmem_cache_free(req_cachep, req);
672}
673
Jens Axboeba816ad2019-09-28 11:36:45 -0600674static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -0600675{
676 struct io_kiocb *nxt;
677
678 /*
679 * The list should never be empty when we are called here. But could
680 * potentially happen if the chain is messed up, check to be on the
681 * safe side.
682 */
683 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, list);
684 if (nxt) {
685 list_del(&nxt->list);
686 if (!list_empty(&req->link_list)) {
687 INIT_LIST_HEAD(&nxt->link_list);
688 list_splice(&req->link_list, &nxt->link_list);
689 nxt->flags |= REQ_F_LINK;
690 }
691
Zhengyuan Liuf7b76ac2019-07-16 23:26:14 +0800692 nxt->flags |= REQ_F_LINK_DONE;
Jens Axboeba816ad2019-09-28 11:36:45 -0600693 /*
694 * If we're in async work, we can continue processing the chain
695 * in this context instead of having to queue up new async work.
696 */
697 if (nxtptr && current_work()) {
698 *nxtptr = nxt;
699 } else {
700 INIT_WORK(&nxt->work, io_sq_wq_submit_work);
701 io_queue_async_work(req->ctx, nxt);
702 }
Jens Axboe9e645e112019-05-10 16:07:28 -0600703 }
704}
705
706/*
707 * Called if REQ_F_LINK is set, and we fail the head request
708 */
709static void io_fail_links(struct io_kiocb *req)
710{
711 struct io_kiocb *link;
712
713 while (!list_empty(&req->link_list)) {
714 link = list_first_entry(&req->link_list, struct io_kiocb, list);
715 list_del(&link->list);
716
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +0200717 trace_io_uring_fail_link(req, link);
Jens Axboe9e645e112019-05-10 16:07:28 -0600718 io_cqring_add_event(req->ctx, link->user_data, -ECANCELED);
719 __io_free_req(link);
720 }
721}
722
Jens Axboeba816ad2019-09-28 11:36:45 -0600723static void io_free_req(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -0600724{
725 /*
726 * If LINK is set, we have dependent requests in this chain. If we
727 * didn't fail this request, queue the first one up, moving any other
728 * dependencies to the next request. In case of failure, fail the rest
729 * of the chain.
730 */
731 if (req->flags & REQ_F_LINK) {
732 if (req->flags & REQ_F_FAIL_LINK)
733 io_fail_links(req);
734 else
Jens Axboeba816ad2019-09-28 11:36:45 -0600735 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -0600736 }
737
738 __io_free_req(req);
739}
740
Jens Axboeba816ad2019-09-28 11:36:45 -0600741/*
742 * Drop reference to request, return next in chain (if there is one) if this
743 * was the last reference to this request.
744 */
745static struct io_kiocb *io_put_req_find_next(struct io_kiocb *req)
Jens Axboee65ef562019-03-12 10:16:44 -0600746{
Jens Axboeba816ad2019-09-28 11:36:45 -0600747 struct io_kiocb *nxt = NULL;
748
Jens Axboee65ef562019-03-12 10:16:44 -0600749 if (refcount_dec_and_test(&req->refs))
Jens Axboeba816ad2019-09-28 11:36:45 -0600750 io_free_req(req, &nxt);
751
752 return nxt;
753}
754
755static void io_put_req(struct io_kiocb *req, struct io_kiocb **nxtptr)
756{
757 struct io_kiocb *nxt;
758
759 nxt = io_put_req_find_next(req);
760 if (nxt) {
761 if (nxtptr) {
762 *nxtptr = nxt;
763 } else {
764 INIT_WORK(&nxt->work, io_sq_wq_submit_work);
765 io_queue_async_work(nxt->ctx, nxt);
766 }
767 }
Jens Axboe2b188cc2019-01-07 10:46:33 -0700768}
769
Hristo Venev75b28af2019-08-26 17:23:46 +0000770static unsigned io_cqring_events(struct io_rings *rings)
Jens Axboea3a0e432019-08-20 11:03:11 -0600771{
772 /* See comment at the top of this file */
773 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +0000774 return READ_ONCE(rings->cq.tail) - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -0600775}
776
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +0300777static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
778{
779 struct io_rings *rings = ctx->rings;
780
781 /* make sure SQ entry isn't read before tail */
782 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
783}
784
Jens Axboedef596e2019-01-09 08:59:42 -0700785/*
786 * Find and free completed poll iocbs
787 */
788static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
789 struct list_head *done)
790{
791 void *reqs[IO_IOPOLL_BATCH];
792 struct io_kiocb *req;
Jens Axboe09bb8392019-03-13 12:39:28 -0600793 int to_free;
Jens Axboedef596e2019-01-09 08:59:42 -0700794
Jens Axboe09bb8392019-03-13 12:39:28 -0600795 to_free = 0;
Jens Axboedef596e2019-01-09 08:59:42 -0700796 while (!list_empty(done)) {
797 req = list_first_entry(done, struct io_kiocb, list);
798 list_del(&req->list);
799
Jens Axboe9e645e112019-05-10 16:07:28 -0600800 io_cqring_fill_event(ctx, req->user_data, req->result);
Jens Axboedef596e2019-01-09 08:59:42 -0700801 (*nr_events)++;
802
Jens Axboe09bb8392019-03-13 12:39:28 -0600803 if (refcount_dec_and_test(&req->refs)) {
804 /* If we're not using fixed files, we have to pair the
805 * completion part with the file put. Use regular
806 * completions for those, only batch free for fixed
Jens Axboe9e645e112019-05-10 16:07:28 -0600807 * file and non-linked commands.
Jens Axboe09bb8392019-03-13 12:39:28 -0600808 */
Jens Axboe9e645e112019-05-10 16:07:28 -0600809 if ((req->flags & (REQ_F_FIXED_FILE|REQ_F_LINK)) ==
810 REQ_F_FIXED_FILE) {
Jens Axboe09bb8392019-03-13 12:39:28 -0600811 reqs[to_free++] = req;
812 if (to_free == ARRAY_SIZE(reqs))
813 io_free_req_many(ctx, reqs, &to_free);
Jens Axboe6b063142019-01-10 22:13:58 -0700814 } else {
Jens Axboeba816ad2019-09-28 11:36:45 -0600815 io_free_req(req, NULL);
Jens Axboe6b063142019-01-10 22:13:58 -0700816 }
Jens Axboe9a56a232019-01-09 09:06:50 -0700817 }
Jens Axboedef596e2019-01-09 08:59:42 -0700818 }
Jens Axboedef596e2019-01-09 08:59:42 -0700819
Jens Axboe09bb8392019-03-13 12:39:28 -0600820 io_commit_cqring(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -0700821 io_free_req_many(ctx, reqs, &to_free);
822}
823
824static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
825 long min)
826{
827 struct io_kiocb *req, *tmp;
828 LIST_HEAD(done);
829 bool spin;
830 int ret;
831
832 /*
833 * Only spin for completions if we don't have multiple devices hanging
834 * off our complete list, and we're under the requested amount.
835 */
836 spin = !ctx->poll_multi_file && *nr_events < min;
837
838 ret = 0;
839 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
840 struct kiocb *kiocb = &req->rw;
841
842 /*
843 * Move completed entries to our local list. If we find a
844 * request that requires polling, break out and complete
845 * the done list first, if we have entries there.
846 */
847 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
848 list_move_tail(&req->list, &done);
849 continue;
850 }
851 if (!list_empty(&done))
852 break;
853
854 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
855 if (ret < 0)
856 break;
857
858 if (ret && spin)
859 spin = false;
860 ret = 0;
861 }
862
863 if (!list_empty(&done))
864 io_iopoll_complete(ctx, nr_events, &done);
865
866 return ret;
867}
868
869/*
870 * Poll for a mininum of 'min' events. Note that if min == 0 we consider that a
871 * non-spinning poll check - we'll still enter the driver poll loop, but only
872 * as a non-spinning completion check.
873 */
874static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
875 long min)
876{
Jens Axboe08f54392019-08-21 22:19:11 -0600877 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -0700878 int ret;
879
880 ret = io_do_iopoll(ctx, nr_events, min);
881 if (ret < 0)
882 return ret;
883 if (!min || *nr_events >= min)
884 return 0;
885 }
886
887 return 1;
888}
889
890/*
891 * We can't just wait for polled events to come to us, we have to actively
892 * find and complete them.
893 */
894static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
895{
896 if (!(ctx->flags & IORING_SETUP_IOPOLL))
897 return;
898
899 mutex_lock(&ctx->uring_lock);
900 while (!list_empty(&ctx->poll_list)) {
901 unsigned int nr_events = 0;
902
903 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -0600904
905 /*
906 * Ensure we allow local-to-the-cpu processing to take place,
907 * in this case we need to ensure that we reap all events.
908 */
909 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -0700910 }
911 mutex_unlock(&ctx->uring_lock);
912}
913
Jens Axboe2b2ed972019-10-25 10:06:15 -0600914static int __io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
915 long min)
Jens Axboedef596e2019-01-09 08:59:42 -0700916{
Jens Axboe2b2ed972019-10-25 10:06:15 -0600917 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -0700918
919 do {
920 int tmin = 0;
921
Jens Axboe500f9fb2019-08-19 12:15:59 -0600922 /*
Jens Axboea3a0e432019-08-20 11:03:11 -0600923 * Don't enter poll loop if we already have events pending.
924 * If we do, we can potentially be spinning for commands that
925 * already triggered a CQE (eg in error).
926 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000927 if (io_cqring_events(ctx->rings))
Jens Axboea3a0e432019-08-20 11:03:11 -0600928 break;
929
930 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -0600931 * If a submit got punted to a workqueue, we can have the
932 * application entering polling for a command before it gets
933 * issued. That app will hold the uring_lock for the duration
934 * of the poll right here, so we need to take a breather every
935 * now and then to ensure that the issue has a chance to add
936 * the poll to the issued list. Otherwise we can spin here
937 * forever, while the workqueue is stuck trying to acquire the
938 * very same mutex.
939 */
940 if (!(++iters & 7)) {
941 mutex_unlock(&ctx->uring_lock);
942 mutex_lock(&ctx->uring_lock);
943 }
944
Jens Axboedef596e2019-01-09 08:59:42 -0700945 if (*nr_events < min)
946 tmin = min - *nr_events;
947
948 ret = io_iopoll_getevents(ctx, nr_events, tmin);
949 if (ret <= 0)
950 break;
951 ret = 0;
952 } while (min && !*nr_events && !need_resched());
953
Jens Axboe2b2ed972019-10-25 10:06:15 -0600954 return ret;
955}
956
957static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
958 long min)
959{
960 int ret;
961
962 /*
963 * We disallow the app entering submit/complete with polling, but we
964 * still need to lock the ring to prevent racing with polled issue
965 * that got punted to a workqueue.
966 */
967 mutex_lock(&ctx->uring_lock);
968 ret = __io_iopoll_check(ctx, nr_events, min);
Jens Axboe500f9fb2019-08-19 12:15:59 -0600969 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -0700970 return ret;
971}
972
Jens Axboe491381ce2019-10-17 09:20:46 -0600973static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700974{
Jens Axboe491381ce2019-10-17 09:20:46 -0600975 /*
976 * Tell lockdep we inherited freeze protection from submission
977 * thread.
978 */
979 if (req->flags & REQ_F_ISREG) {
980 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700981
Jens Axboe491381ce2019-10-17 09:20:46 -0600982 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700983 }
Jens Axboe491381ce2019-10-17 09:20:46 -0600984 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700985}
986
Jens Axboeba816ad2019-09-28 11:36:45 -0600987static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700988{
989 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
990
Jens Axboe491381ce2019-10-17 09:20:46 -0600991 if (kiocb->ki_flags & IOCB_WRITE)
992 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700993
Jens Axboe9e645e112019-05-10 16:07:28 -0600994 if ((req->flags & REQ_F_LINK) && res != req->result)
995 req->flags |= REQ_F_FAIL_LINK;
Jens Axboec71ffb62019-05-13 20:58:29 -0600996 io_cqring_add_event(req->ctx, req->user_data, res);
Jens Axboeba816ad2019-09-28 11:36:45 -0600997}
998
999static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1000{
1001 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1002
1003 io_complete_rw_common(kiocb, res);
1004 io_put_req(req, NULL);
1005}
1006
1007static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res)
1008{
1009 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1010
1011 io_complete_rw_common(kiocb, res);
1012 return io_put_req_find_next(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001013}
1014
Jens Axboedef596e2019-01-09 08:59:42 -07001015static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1016{
1017 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1018
Jens Axboe491381ce2019-10-17 09:20:46 -06001019 if (kiocb->ki_flags & IOCB_WRITE)
1020 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001021
Jens Axboe9e645e112019-05-10 16:07:28 -06001022 if ((req->flags & REQ_F_LINK) && res != req->result)
1023 req->flags |= REQ_F_FAIL_LINK;
1024 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -07001025 if (res != -EAGAIN)
1026 req->flags |= REQ_F_IOPOLL_COMPLETED;
1027}
1028
1029/*
1030 * After the iocb has been issued, it's safe to be found on the poll list.
1031 * Adding the kiocb to the list AFTER submission ensures that we don't
1032 * find it from a io_iopoll_getevents() thread before the issuer is done
1033 * accessing the kiocb cookie.
1034 */
1035static void io_iopoll_req_issued(struct io_kiocb *req)
1036{
1037 struct io_ring_ctx *ctx = req->ctx;
1038
1039 /*
1040 * Track whether we have multiple files in our lists. This will impact
1041 * how we do polling eventually, not spinning if we're on potentially
1042 * different devices.
1043 */
1044 if (list_empty(&ctx->poll_list)) {
1045 ctx->poll_multi_file = false;
1046 } else if (!ctx->poll_multi_file) {
1047 struct io_kiocb *list_req;
1048
1049 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1050 list);
1051 if (list_req->rw.ki_filp != req->rw.ki_filp)
1052 ctx->poll_multi_file = true;
1053 }
1054
1055 /*
1056 * For fast devices, IO may have already completed. If it has, add
1057 * it to the front so we find it first.
1058 */
1059 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1060 list_add(&req->list, &ctx->poll_list);
1061 else
1062 list_add_tail(&req->list, &ctx->poll_list);
1063}
1064
Jens Axboe3d6770f2019-04-13 11:50:54 -06001065static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07001066{
Jens Axboe3d6770f2019-04-13 11:50:54 -06001067 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -07001068 int diff = state->has_refs - state->used_refs;
1069
1070 if (diff)
1071 fput_many(state->file, diff);
1072 state->file = NULL;
1073 }
1074}
1075
1076/*
1077 * Get as many references to a file as we have IOs left in this submission,
1078 * assuming most submissions are for one file, or at least that each file
1079 * has more than one submission.
1080 */
1081static struct file *io_file_get(struct io_submit_state *state, int fd)
1082{
1083 if (!state)
1084 return fget(fd);
1085
1086 if (state->file) {
1087 if (state->fd == fd) {
1088 state->used_refs++;
1089 state->ios_left--;
1090 return state->file;
1091 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06001092 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07001093 }
1094 state->file = fget_many(fd, state->ios_left);
1095 if (!state->file)
1096 return NULL;
1097
1098 state->fd = fd;
1099 state->has_refs = state->ios_left;
1100 state->used_refs = 1;
1101 state->ios_left--;
1102 return state->file;
1103}
1104
Jens Axboe2b188cc2019-01-07 10:46:33 -07001105/*
1106 * If we tracked the file through the SCM inflight mechanism, we could support
1107 * any file. For now, just ensure that anything potentially problematic is done
1108 * inline.
1109 */
1110static bool io_file_supports_async(struct file *file)
1111{
1112 umode_t mode = file_inode(file)->i_mode;
1113
1114 if (S_ISBLK(mode) || S_ISCHR(mode))
1115 return true;
1116 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
1117 return true;
1118
1119 return false;
1120}
1121
Jens Axboe6c271ce2019-01-10 11:22:30 -07001122static int io_prep_rw(struct io_kiocb *req, const struct sqe_submit *s,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001123 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001124{
Jens Axboe6c271ce2019-01-10 11:22:30 -07001125 const struct io_uring_sqe *sqe = s->sqe;
Jens Axboedef596e2019-01-09 08:59:42 -07001126 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001127 struct kiocb *kiocb = &req->rw;
Jens Axboe09bb8392019-03-13 12:39:28 -06001128 unsigned ioprio;
1129 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001130
Jens Axboe09bb8392019-03-13 12:39:28 -06001131 if (!req->file)
1132 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001133
Jens Axboe491381ce2019-10-17 09:20:46 -06001134 if (S_ISREG(file_inode(req->file)->i_mode))
1135 req->flags |= REQ_F_ISREG;
1136
1137 /*
1138 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
1139 * we know to async punt it even if it was opened O_NONBLOCK
1140 */
1141 if (force_nonblock && !io_file_supports_async(req->file)) {
1142 req->flags |= REQ_F_MUST_PUNT;
1143 return -EAGAIN;
1144 }
Jens Axboe6b063142019-01-10 22:13:58 -07001145
Jens Axboe2b188cc2019-01-07 10:46:33 -07001146 kiocb->ki_pos = READ_ONCE(sqe->off);
1147 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
1148 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
1149
1150 ioprio = READ_ONCE(sqe->ioprio);
1151 if (ioprio) {
1152 ret = ioprio_check_cap(ioprio);
1153 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06001154 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001155
1156 kiocb->ki_ioprio = ioprio;
1157 } else
1158 kiocb->ki_ioprio = get_current_ioprio();
1159
1160 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
1161 if (unlikely(ret))
Jens Axboe09bb8392019-03-13 12:39:28 -06001162 return ret;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001163
1164 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06001165 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
1166 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02001167 req->flags |= REQ_F_NOWAIT;
1168
1169 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001170 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001171
Jens Axboedef596e2019-01-09 08:59:42 -07001172 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07001173 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
1174 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06001175 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001176
Jens Axboedef596e2019-01-09 08:59:42 -07001177 kiocb->ki_flags |= IOCB_HIPRI;
1178 kiocb->ki_complete = io_complete_rw_iopoll;
1179 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001180 if (kiocb->ki_flags & IOCB_HIPRI)
1181 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07001182 kiocb->ki_complete = io_complete_rw;
1183 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001184 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001185}
1186
1187static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
1188{
1189 switch (ret) {
1190 case -EIOCBQUEUED:
1191 break;
1192 case -ERESTARTSYS:
1193 case -ERESTARTNOINTR:
1194 case -ERESTARTNOHAND:
1195 case -ERESTART_RESTARTBLOCK:
1196 /*
1197 * We can't just restart the syscall, since previously
1198 * submitted sqes may already be in progress. Just fail this
1199 * IO with EINTR.
1200 */
1201 ret = -EINTR;
1202 /* fall through */
1203 default:
1204 kiocb->ki_complete(kiocb, ret, 0);
1205 }
1206}
1207
Jens Axboeba816ad2019-09-28 11:36:45 -06001208static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt,
1209 bool in_async)
1210{
1211 if (in_async && ret >= 0 && nxt && kiocb->ki_complete == io_complete_rw)
1212 *nxt = __io_complete_rw(kiocb, ret);
1213 else
1214 io_rw_done(kiocb, ret);
1215}
1216
Jens Axboeedafcce2019-01-09 09:16:05 -07001217static int io_import_fixed(struct io_ring_ctx *ctx, int rw,
1218 const struct io_uring_sqe *sqe,
1219 struct iov_iter *iter)
1220{
1221 size_t len = READ_ONCE(sqe->len);
1222 struct io_mapped_ubuf *imu;
1223 unsigned index, buf_index;
1224 size_t offset;
1225 u64 buf_addr;
1226
1227 /* attempt to use fixed buffers without having provided iovecs */
1228 if (unlikely(!ctx->user_bufs))
1229 return -EFAULT;
1230
1231 buf_index = READ_ONCE(sqe->buf_index);
1232 if (unlikely(buf_index >= ctx->nr_user_bufs))
1233 return -EFAULT;
1234
1235 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
1236 imu = &ctx->user_bufs[index];
1237 buf_addr = READ_ONCE(sqe->addr);
1238
1239 /* overflow */
1240 if (buf_addr + len < buf_addr)
1241 return -EFAULT;
1242 /* not inside the mapped region */
1243 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
1244 return -EFAULT;
1245
1246 /*
1247 * May not be a start of buffer, set size appropriately
1248 * and advance us to the beginning.
1249 */
1250 offset = buf_addr - imu->ubuf;
1251 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06001252
1253 if (offset) {
1254 /*
1255 * Don't use iov_iter_advance() here, as it's really slow for
1256 * using the latter parts of a big fixed buffer - it iterates
1257 * over each segment manually. We can cheat a bit here, because
1258 * we know that:
1259 *
1260 * 1) it's a BVEC iter, we set it up
1261 * 2) all bvecs are PAGE_SIZE in size, except potentially the
1262 * first and last bvec
1263 *
1264 * So just find our index, and adjust the iterator afterwards.
1265 * If the offset is within the first bvec (or the whole first
1266 * bvec, just use iov_iter_advance(). This makes it easier
1267 * since we can just skip the first segment, which may not
1268 * be PAGE_SIZE aligned.
1269 */
1270 const struct bio_vec *bvec = imu->bvec;
1271
1272 if (offset <= bvec->bv_len) {
1273 iov_iter_advance(iter, offset);
1274 } else {
1275 unsigned long seg_skip;
1276
1277 /* skip first vec */
1278 offset -= bvec->bv_len;
1279 seg_skip = 1 + (offset >> PAGE_SHIFT);
1280
1281 iter->bvec = bvec + seg_skip;
1282 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02001283 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001284 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001285 }
1286 }
1287
Jens Axboeedafcce2019-01-09 09:16:05 -07001288 return 0;
1289}
1290
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001291static ssize_t io_import_iovec(struct io_ring_ctx *ctx, int rw,
1292 const struct sqe_submit *s, struct iovec **iovec,
1293 struct iov_iter *iter)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001294{
1295 const struct io_uring_sqe *sqe = s->sqe;
1296 void __user *buf = u64_to_user_ptr(READ_ONCE(sqe->addr));
1297 size_t sqe_len = READ_ONCE(sqe->len);
Jens Axboeedafcce2019-01-09 09:16:05 -07001298 u8 opcode;
1299
1300 /*
1301 * We're reading ->opcode for the second time, but the first read
1302 * doesn't care whether it's _FIXED or not, so it doesn't matter
1303 * whether ->opcode changes concurrently. The first read does care
1304 * about whether it is a READ or a WRITE, so we don't trust this read
1305 * for that purpose and instead let the caller pass in the read/write
1306 * flag.
1307 */
1308 opcode = READ_ONCE(sqe->opcode);
1309 if (opcode == IORING_OP_READ_FIXED ||
1310 opcode == IORING_OP_WRITE_FIXED) {
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001311 ssize_t ret = io_import_fixed(ctx, rw, sqe, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07001312 *iovec = NULL;
1313 return ret;
1314 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001315
1316 if (!s->has_user)
1317 return -EFAULT;
1318
1319#ifdef CONFIG_COMPAT
1320 if (ctx->compat)
1321 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
1322 iovec, iter);
1323#endif
1324
1325 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
1326}
1327
Jens Axboe6d5d5ac2019-09-11 10:16:13 -06001328static inline bool io_should_merge(struct async_list *al, struct kiocb *kiocb)
1329{
1330 if (al->file == kiocb->ki_filp) {
1331 off_t start, end;
1332
1333 /*
1334 * Allow merging if we're anywhere in the range of the same
1335 * page. Generally this happens for sub-page reads or writes,
1336 * and it's beneficial to allow the first worker to bring the
1337 * page in and the piggy backed work can then work on the
1338 * cached page.
1339 */
1340 start = al->io_start & PAGE_MASK;
1341 end = (al->io_start + al->io_len + PAGE_SIZE - 1) & PAGE_MASK;
1342 if (kiocb->ki_pos >= start && kiocb->ki_pos <= end)
1343 return true;
1344 }
1345
1346 al->file = NULL;
1347 return false;
1348}
1349
Jens Axboe31b51512019-01-18 22:56:34 -07001350/*
1351 * Make a note of the last file/offset/direction we punted to async
1352 * context. We'll use this information to see if we can piggy back a
1353 * sequential request onto the previous one, if it's still hasn't been
1354 * completed by the async worker.
1355 */
1356static void io_async_list_note(int rw, struct io_kiocb *req, size_t len)
1357{
1358 struct async_list *async_list = &req->ctx->pending_async[rw];
1359 struct kiocb *kiocb = &req->rw;
1360 struct file *filp = kiocb->ki_filp;
Jens Axboe31b51512019-01-18 22:56:34 -07001361
Jens Axboe6d5d5ac2019-09-11 10:16:13 -06001362 if (io_should_merge(async_list, kiocb)) {
Zhengyuan Liu9310a7ba2019-07-22 10:23:27 +08001363 unsigned long max_bytes;
Jens Axboe31b51512019-01-18 22:56:34 -07001364
1365 /* Use 8x RA size as a decent limiter for both reads/writes */
Zhengyuan Liu9310a7ba2019-07-22 10:23:27 +08001366 max_bytes = filp->f_ra.ra_pages << (PAGE_SHIFT + 3);
1367 if (!max_bytes)
1368 max_bytes = VM_READAHEAD_PAGES << (PAGE_SHIFT + 3);
Jens Axboe31b51512019-01-18 22:56:34 -07001369
Zhengyuan Liu9310a7ba2019-07-22 10:23:27 +08001370 /* If max len are exceeded, reset the state */
1371 if (async_list->io_len + len <= max_bytes) {
Jens Axboe31b51512019-01-18 22:56:34 -07001372 req->flags |= REQ_F_SEQ_PREV;
Zhengyuan Liu9310a7ba2019-07-22 10:23:27 +08001373 async_list->io_len += len;
Jens Axboe31b51512019-01-18 22:56:34 -07001374 } else {
Jens Axboe6d5d5ac2019-09-11 10:16:13 -06001375 async_list->file = NULL;
Jens Axboe31b51512019-01-18 22:56:34 -07001376 }
1377 }
1378
1379 /* New file? Reset state. */
1380 if (async_list->file != filp) {
Jens Axboe6d5d5ac2019-09-11 10:16:13 -06001381 async_list->io_start = kiocb->ki_pos;
1382 async_list->io_len = len;
Jens Axboe31b51512019-01-18 22:56:34 -07001383 async_list->file = filp;
1384 }
Jens Axboe31b51512019-01-18 22:56:34 -07001385}
1386
Jens Axboe32960612019-09-23 11:05:34 -06001387/*
1388 * For files that don't have ->read_iter() and ->write_iter(), handle them
1389 * by looping over ->read() or ->write() manually.
1390 */
1391static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
1392 struct iov_iter *iter)
1393{
1394 ssize_t ret = 0;
1395
1396 /*
1397 * Don't support polled IO through this interface, and we can't
1398 * support non-blocking either. For the latter, this just causes
1399 * the kiocb to be handled from an async context.
1400 */
1401 if (kiocb->ki_flags & IOCB_HIPRI)
1402 return -EOPNOTSUPP;
1403 if (kiocb->ki_flags & IOCB_NOWAIT)
1404 return -EAGAIN;
1405
1406 while (iov_iter_count(iter)) {
1407 struct iovec iovec = iov_iter_iovec(iter);
1408 ssize_t nr;
1409
1410 if (rw == READ) {
1411 nr = file->f_op->read(file, iovec.iov_base,
1412 iovec.iov_len, &kiocb->ki_pos);
1413 } else {
1414 nr = file->f_op->write(file, iovec.iov_base,
1415 iovec.iov_len, &kiocb->ki_pos);
1416 }
1417
1418 if (nr < 0) {
1419 if (!ret)
1420 ret = nr;
1421 break;
1422 }
1423 ret += nr;
1424 if (nr != iovec.iov_len)
1425 break;
1426 iov_iter_advance(iter, nr);
1427 }
1428
1429 return ret;
1430}
1431
Jens Axboee0c5c572019-03-12 10:18:47 -06001432static int io_read(struct io_kiocb *req, const struct sqe_submit *s,
Jens Axboeba816ad2019-09-28 11:36:45 -06001433 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001434{
1435 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1436 struct kiocb *kiocb = &req->rw;
1437 struct iov_iter iter;
1438 struct file *file;
Jens Axboe31b51512019-01-18 22:56:34 -07001439 size_t iov_count;
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001440 ssize_t read_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001441
Jens Axboe8358e3a2019-04-23 08:17:58 -06001442 ret = io_prep_rw(req, s, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001443 if (ret)
1444 return ret;
1445 file = kiocb->ki_filp;
1446
Jens Axboe2b188cc2019-01-07 10:46:33 -07001447 if (unlikely(!(file->f_mode & FMODE_READ)))
Jens Axboe09bb8392019-03-13 12:39:28 -06001448 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001449
1450 ret = io_import_iovec(req->ctx, READ, s, &iovec, &iter);
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001451 if (ret < 0)
Jens Axboe09bb8392019-03-13 12:39:28 -06001452 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001453
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001454 read_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06001455 if (req->flags & REQ_F_LINK)
1456 req->result = read_size;
1457
Jens Axboe31b51512019-01-18 22:56:34 -07001458 iov_count = iov_iter_count(&iter);
1459 ret = rw_verify_area(READ, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001460 if (!ret) {
1461 ssize_t ret2;
1462
Jens Axboe32960612019-09-23 11:05:34 -06001463 if (file->f_op->read_iter)
1464 ret2 = call_read_iter(file, kiocb, &iter);
1465 else
1466 ret2 = loop_rw_iter(READ, file, kiocb, &iter);
1467
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001468 /*
1469 * In case of a short read, punt to async. This can happen
1470 * if we have data partially cached. Alternatively we can
1471 * return the short read, in which case the application will
1472 * need to issue another SQE and wait for it. That SQE will
1473 * need async punt anyway, so it's more efficient to do it
1474 * here.
1475 */
Jens Axboe491381ce2019-10-17 09:20:46 -06001476 if (force_nonblock && !(req->flags & REQ_F_NOWAIT) &&
1477 (req->flags & REQ_F_ISREG) &&
1478 ret2 > 0 && ret2 < read_size)
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001479 ret2 = -EAGAIN;
1480 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboe31b51512019-01-18 22:56:34 -07001481 if (!force_nonblock || ret2 != -EAGAIN) {
Jackie Liuba5290c2019-10-09 09:19:59 +08001482 kiocb_done(kiocb, ret2, nxt, s->in_async);
Jens Axboe31b51512019-01-18 22:56:34 -07001483 } else {
Jackie Liuba5290c2019-10-09 09:19:59 +08001484 if (!s->in_async)
Jens Axboe31b51512019-01-18 22:56:34 -07001485 io_async_list_note(READ, req, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001486 ret = -EAGAIN;
Jens Axboe31b51512019-01-18 22:56:34 -07001487 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001488 }
1489 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001490 return ret;
1491}
1492
Jens Axboee0c5c572019-03-12 10:18:47 -06001493static int io_write(struct io_kiocb *req, const struct sqe_submit *s,
Jens Axboeba816ad2019-09-28 11:36:45 -06001494 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001495{
1496 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1497 struct kiocb *kiocb = &req->rw;
1498 struct iov_iter iter;
1499 struct file *file;
Jens Axboe31b51512019-01-18 22:56:34 -07001500 size_t iov_count;
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001501 ssize_t ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001502
Jens Axboe8358e3a2019-04-23 08:17:58 -06001503 ret = io_prep_rw(req, s, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001504 if (ret)
1505 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001506
Jens Axboe2b188cc2019-01-07 10:46:33 -07001507 file = kiocb->ki_filp;
1508 if (unlikely(!(file->f_mode & FMODE_WRITE)))
Jens Axboe09bb8392019-03-13 12:39:28 -06001509 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001510
1511 ret = io_import_iovec(req->ctx, WRITE, s, &iovec, &iter);
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001512 if (ret < 0)
Jens Axboe09bb8392019-03-13 12:39:28 -06001513 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001514
Jens Axboe9e645e112019-05-10 16:07:28 -06001515 if (req->flags & REQ_F_LINK)
1516 req->result = ret;
1517
Jens Axboe31b51512019-01-18 22:56:34 -07001518 iov_count = iov_iter_count(&iter);
1519
1520 ret = -EAGAIN;
1521 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT)) {
Jackie Liuba5290c2019-10-09 09:19:59 +08001522 if (!s->in_async)
Jens Axboe31b51512019-01-18 22:56:34 -07001523 io_async_list_note(WRITE, req, iov_count);
1524 goto out_free;
1525 }
1526
1527 ret = rw_verify_area(WRITE, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001528 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01001529 ssize_t ret2;
1530
Jens Axboe2b188cc2019-01-07 10:46:33 -07001531 /*
1532 * Open-code file_start_write here to grab freeze protection,
1533 * which will be released by another thread in
1534 * io_complete_rw(). Fool lockdep by telling it the lock got
1535 * released so that it doesn't complain about the held lock when
1536 * we return to userspace.
1537 */
Jens Axboe491381ce2019-10-17 09:20:46 -06001538 if (req->flags & REQ_F_ISREG) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001539 __sb_start_write(file_inode(file)->i_sb,
1540 SB_FREEZE_WRITE, true);
1541 __sb_writers_release(file_inode(file)->i_sb,
1542 SB_FREEZE_WRITE);
1543 }
1544 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01001545
Jens Axboe32960612019-09-23 11:05:34 -06001546 if (file->f_op->write_iter)
1547 ret2 = call_write_iter(file, kiocb, &iter);
1548 else
1549 ret2 = loop_rw_iter(WRITE, file, kiocb, &iter);
Roman Penyaev9bf79332019-03-25 20:09:24 +01001550 if (!force_nonblock || ret2 != -EAGAIN) {
Jackie Liuba5290c2019-10-09 09:19:59 +08001551 kiocb_done(kiocb, ret2, nxt, s->in_async);
Roman Penyaev9bf79332019-03-25 20:09:24 +01001552 } else {
Jackie Liuba5290c2019-10-09 09:19:59 +08001553 if (!s->in_async)
Roman Penyaev9bf79332019-03-25 20:09:24 +01001554 io_async_list_note(WRITE, req, iov_count);
1555 ret = -EAGAIN;
1556 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001557 }
Jens Axboe31b51512019-01-18 22:56:34 -07001558out_free:
Jens Axboe2b188cc2019-01-07 10:46:33 -07001559 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001560 return ret;
1561}
1562
1563/*
1564 * IORING_OP_NOP just posts a completion event, nothing else.
1565 */
1566static int io_nop(struct io_kiocb *req, u64 user_data)
1567{
1568 struct io_ring_ctx *ctx = req->ctx;
1569 long err = 0;
1570
Jens Axboedef596e2019-01-09 08:59:42 -07001571 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1572 return -EINVAL;
1573
Jens Axboec71ffb62019-05-13 20:58:29 -06001574 io_cqring_add_event(ctx, user_data, err);
Jens Axboeba816ad2019-09-28 11:36:45 -06001575 io_put_req(req, NULL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001576 return 0;
1577}
1578
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001579static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1580{
Jens Axboe6b063142019-01-10 22:13:58 -07001581 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001582
Jens Axboe09bb8392019-03-13 12:39:28 -06001583 if (!req->file)
1584 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001585
Jens Axboe6b063142019-01-10 22:13:58 -07001586 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07001587 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07001588 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001589 return -EINVAL;
1590
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001591 return 0;
1592}
1593
1594static int io_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001595 struct io_kiocb **nxt, bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001596{
1597 loff_t sqe_off = READ_ONCE(sqe->off);
1598 loff_t sqe_len = READ_ONCE(sqe->len);
1599 loff_t end = sqe_off + sqe_len;
1600 unsigned fsync_flags;
1601 int ret;
1602
1603 fsync_flags = READ_ONCE(sqe->fsync_flags);
1604 if (unlikely(fsync_flags & ~IORING_FSYNC_DATASYNC))
1605 return -EINVAL;
1606
1607 ret = io_prep_fsync(req, sqe);
1608 if (ret)
1609 return ret;
1610
1611 /* fsync always requires a blocking context */
1612 if (force_nonblock)
1613 return -EAGAIN;
1614
1615 ret = vfs_fsync_range(req->rw.ki_filp, sqe_off,
1616 end > 0 ? end : LLONG_MAX,
1617 fsync_flags & IORING_FSYNC_DATASYNC);
1618
Jens Axboe9e645e112019-05-10 16:07:28 -06001619 if (ret < 0 && (req->flags & REQ_F_LINK))
1620 req->flags |= REQ_F_FAIL_LINK;
Jens Axboec71ffb62019-05-13 20:58:29 -06001621 io_cqring_add_event(req->ctx, sqe->user_data, ret);
Jens Axboeba816ad2019-09-28 11:36:45 -06001622 io_put_req(req, nxt);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001623 return 0;
1624}
1625
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001626static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1627{
1628 struct io_ring_ctx *ctx = req->ctx;
1629 int ret = 0;
1630
1631 if (!req->file)
1632 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001633
1634 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1635 return -EINVAL;
1636 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
1637 return -EINVAL;
1638
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001639 return ret;
1640}
1641
1642static int io_sync_file_range(struct io_kiocb *req,
1643 const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001644 struct io_kiocb **nxt,
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001645 bool force_nonblock)
1646{
1647 loff_t sqe_off;
1648 loff_t sqe_len;
1649 unsigned flags;
1650 int ret;
1651
1652 ret = io_prep_sfr(req, sqe);
1653 if (ret)
1654 return ret;
1655
1656 /* sync_file_range always requires a blocking context */
1657 if (force_nonblock)
1658 return -EAGAIN;
1659
1660 sqe_off = READ_ONCE(sqe->off);
1661 sqe_len = READ_ONCE(sqe->len);
1662 flags = READ_ONCE(sqe->sync_range_flags);
1663
1664 ret = sync_file_range(req->rw.ki_filp, sqe_off, sqe_len, flags);
1665
Jens Axboe9e645e112019-05-10 16:07:28 -06001666 if (ret < 0 && (req->flags & REQ_F_LINK))
1667 req->flags |= REQ_F_FAIL_LINK;
Jens Axboec71ffb62019-05-13 20:58:29 -06001668 io_cqring_add_event(req->ctx, sqe->user_data, ret);
Jens Axboeba816ad2019-09-28 11:36:45 -06001669 io_put_req(req, nxt);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001670 return 0;
1671}
1672
Jens Axboe0fa03c62019-04-19 13:34:07 -06001673#if defined(CONFIG_NET)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001674static int io_send_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001675 struct io_kiocb **nxt, bool force_nonblock,
Jens Axboeaa1fa282019-04-19 13:38:09 -06001676 long (*fn)(struct socket *, struct user_msghdr __user *,
1677 unsigned int))
1678{
Jens Axboe0fa03c62019-04-19 13:34:07 -06001679 struct socket *sock;
1680 int ret;
1681
1682 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1683 return -EINVAL;
1684
1685 sock = sock_from_file(req->file, &ret);
1686 if (sock) {
1687 struct user_msghdr __user *msg;
1688 unsigned flags;
1689
1690 flags = READ_ONCE(sqe->msg_flags);
1691 if (flags & MSG_DONTWAIT)
1692 req->flags |= REQ_F_NOWAIT;
1693 else if (force_nonblock)
1694 flags |= MSG_DONTWAIT;
1695
1696 msg = (struct user_msghdr __user *) (unsigned long)
1697 READ_ONCE(sqe->addr);
1698
Jens Axboeaa1fa282019-04-19 13:38:09 -06001699 ret = fn(sock, msg, flags);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001700 if (force_nonblock && ret == -EAGAIN)
1701 return ret;
1702 }
1703
1704 io_cqring_add_event(req->ctx, sqe->user_data, ret);
Jens Axboeba816ad2019-09-28 11:36:45 -06001705 io_put_req(req, nxt);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001706 return 0;
Jens Axboeaa1fa282019-04-19 13:38:09 -06001707}
1708#endif
1709
1710static int io_sendmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001711 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001712{
1713#if defined(CONFIG_NET)
Jens Axboeba816ad2019-09-28 11:36:45 -06001714 return io_send_recvmsg(req, sqe, nxt, force_nonblock,
1715 __sys_sendmsg_sock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06001716#else
1717 return -EOPNOTSUPP;
1718#endif
1719}
1720
1721static int io_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001722 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001723{
1724#if defined(CONFIG_NET)
Jens Axboeba816ad2019-09-28 11:36:45 -06001725 return io_send_recvmsg(req, sqe, nxt, force_nonblock,
1726 __sys_recvmsg_sock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001727#else
1728 return -EOPNOTSUPP;
1729#endif
1730}
1731
Jens Axboe221c5eb2019-01-17 09:41:58 -07001732static void io_poll_remove_one(struct io_kiocb *req)
1733{
1734 struct io_poll_iocb *poll = &req->poll;
1735
1736 spin_lock(&poll->head->lock);
1737 WRITE_ONCE(poll->canceled, true);
1738 if (!list_empty(&poll->wait.entry)) {
1739 list_del_init(&poll->wait.entry);
Jens Axboe18d9be12019-09-10 09:13:05 -06001740 io_queue_async_work(req->ctx, req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001741 }
1742 spin_unlock(&poll->head->lock);
1743
1744 list_del_init(&req->list);
1745}
1746
1747static void io_poll_remove_all(struct io_ring_ctx *ctx)
1748{
1749 struct io_kiocb *req;
1750
1751 spin_lock_irq(&ctx->completion_lock);
1752 while (!list_empty(&ctx->cancel_list)) {
1753 req = list_first_entry(&ctx->cancel_list, struct io_kiocb,list);
1754 io_poll_remove_one(req);
1755 }
1756 spin_unlock_irq(&ctx->completion_lock);
1757}
1758
1759/*
1760 * Find a running poll command that matches one specified in sqe->addr,
1761 * and remove it if found.
1762 */
1763static int io_poll_remove(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1764{
1765 struct io_ring_ctx *ctx = req->ctx;
1766 struct io_kiocb *poll_req, *next;
1767 int ret = -ENOENT;
1768
1769 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1770 return -EINVAL;
1771 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
1772 sqe->poll_events)
1773 return -EINVAL;
1774
1775 spin_lock_irq(&ctx->completion_lock);
1776 list_for_each_entry_safe(poll_req, next, &ctx->cancel_list, list) {
1777 if (READ_ONCE(sqe->addr) == poll_req->user_data) {
1778 io_poll_remove_one(poll_req);
1779 ret = 0;
1780 break;
1781 }
1782 }
1783 spin_unlock_irq(&ctx->completion_lock);
1784
Jens Axboec71ffb62019-05-13 20:58:29 -06001785 io_cqring_add_event(req->ctx, sqe->user_data, ret);
Jens Axboeba816ad2019-09-28 11:36:45 -06001786 io_put_req(req, NULL);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001787 return 0;
1788}
1789
Jens Axboe8c838782019-03-12 15:48:16 -06001790static void io_poll_complete(struct io_ring_ctx *ctx, struct io_kiocb *req,
1791 __poll_t mask)
Jens Axboe221c5eb2019-01-17 09:41:58 -07001792{
Jens Axboe8c838782019-03-12 15:48:16 -06001793 req->poll.done = true;
Jens Axboec71ffb62019-05-13 20:58:29 -06001794 io_cqring_fill_event(ctx, req->user_data, mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06001795 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001796}
1797
1798static void io_poll_complete_work(struct work_struct *work)
1799{
1800 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
1801 struct io_poll_iocb *poll = &req->poll;
1802 struct poll_table_struct pt = { ._key = poll->events };
1803 struct io_ring_ctx *ctx = req->ctx;
1804 __poll_t mask = 0;
1805
1806 if (!READ_ONCE(poll->canceled))
1807 mask = vfs_poll(poll->file, &pt) & poll->events;
1808
1809 /*
1810 * Note that ->ki_cancel callers also delete iocb from active_reqs after
1811 * calling ->ki_cancel. We need the ctx_lock roundtrip here to
1812 * synchronize with them. In the cancellation case the list_del_init
1813 * itself is not actually needed, but harmless so we keep it in to
1814 * avoid further branches in the fast path.
1815 */
1816 spin_lock_irq(&ctx->completion_lock);
1817 if (!mask && !READ_ONCE(poll->canceled)) {
1818 add_wait_queue(poll->head, &poll->wait);
1819 spin_unlock_irq(&ctx->completion_lock);
1820 return;
1821 }
1822 list_del_init(&req->list);
Jens Axboe8c838782019-03-12 15:48:16 -06001823 io_poll_complete(ctx, req, mask);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001824 spin_unlock_irq(&ctx->completion_lock);
1825
Jens Axboe8c838782019-03-12 15:48:16 -06001826 io_cqring_ev_posted(ctx);
Jens Axboeba816ad2019-09-28 11:36:45 -06001827 io_put_req(req, NULL);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001828}
1829
1830static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
1831 void *key)
1832{
1833 struct io_poll_iocb *poll = container_of(wait, struct io_poll_iocb,
1834 wait);
1835 struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
1836 struct io_ring_ctx *ctx = req->ctx;
1837 __poll_t mask = key_to_poll(key);
Jens Axboe8c838782019-03-12 15:48:16 -06001838 unsigned long flags;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001839
1840 /* for instances that support it check for an event match first: */
Jens Axboe8c838782019-03-12 15:48:16 -06001841 if (mask && !(mask & poll->events))
1842 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001843
1844 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06001845
1846 if (mask && spin_trylock_irqsave(&ctx->completion_lock, flags)) {
1847 list_del(&req->list);
1848 io_poll_complete(ctx, req, mask);
1849 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1850
1851 io_cqring_ev_posted(ctx);
Jens Axboeba816ad2019-09-28 11:36:45 -06001852 io_put_req(req, NULL);
Jens Axboe8c838782019-03-12 15:48:16 -06001853 } else {
Jens Axboe18d9be12019-09-10 09:13:05 -06001854 io_queue_async_work(ctx, req);
Jens Axboe8c838782019-03-12 15:48:16 -06001855 }
1856
Jens Axboe221c5eb2019-01-17 09:41:58 -07001857 return 1;
1858}
1859
1860struct io_poll_table {
1861 struct poll_table_struct pt;
1862 struct io_kiocb *req;
1863 int error;
1864};
1865
1866static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
1867 struct poll_table_struct *p)
1868{
1869 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
1870
1871 if (unlikely(pt->req->poll.head)) {
1872 pt->error = -EINVAL;
1873 return;
1874 }
1875
1876 pt->error = 0;
1877 pt->req->poll.head = head;
1878 add_wait_queue(head, &pt->req->poll.wait);
1879}
1880
1881static int io_poll_add(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1882{
1883 struct io_poll_iocb *poll = &req->poll;
1884 struct io_ring_ctx *ctx = req->ctx;
1885 struct io_poll_table ipt;
Jens Axboe8c838782019-03-12 15:48:16 -06001886 bool cancel = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001887 __poll_t mask;
1888 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001889
1890 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1891 return -EINVAL;
1892 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
1893 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06001894 if (!poll->file)
1895 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001896
Jens Axboe6cc47d12019-09-18 11:18:23 -06001897 req->submit.sqe = NULL;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001898 INIT_WORK(&req->work, io_poll_complete_work);
1899 events = READ_ONCE(sqe->poll_events);
1900 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
1901
Jens Axboe221c5eb2019-01-17 09:41:58 -07001902 poll->head = NULL;
Jens Axboe8c838782019-03-12 15:48:16 -06001903 poll->done = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001904 poll->canceled = false;
1905
1906 ipt.pt._qproc = io_poll_queue_proc;
1907 ipt.pt._key = poll->events;
1908 ipt.req = req;
1909 ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
1910
1911 /* initialized the list so that we can do list_empty checks */
1912 INIT_LIST_HEAD(&poll->wait.entry);
1913 init_waitqueue_func_entry(&poll->wait, io_poll_wake);
1914
Jens Axboe36703242019-07-25 10:20:18 -06001915 INIT_LIST_HEAD(&req->list);
1916
Jens Axboe221c5eb2019-01-17 09:41:58 -07001917 mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001918
1919 spin_lock_irq(&ctx->completion_lock);
Jens Axboe8c838782019-03-12 15:48:16 -06001920 if (likely(poll->head)) {
1921 spin_lock(&poll->head->lock);
1922 if (unlikely(list_empty(&poll->wait.entry))) {
1923 if (ipt.error)
1924 cancel = true;
1925 ipt.error = 0;
1926 mask = 0;
1927 }
1928 if (mask || ipt.error)
1929 list_del_init(&poll->wait.entry);
1930 else if (cancel)
1931 WRITE_ONCE(poll->canceled, true);
1932 else if (!poll->done) /* actually waiting for an event */
1933 list_add_tail(&req->list, &ctx->cancel_list);
1934 spin_unlock(&poll->head->lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001935 }
Jens Axboe8c838782019-03-12 15:48:16 -06001936 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06001937 ipt.error = 0;
1938 io_poll_complete(ctx, req, mask);
1939 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07001940 spin_unlock_irq(&ctx->completion_lock);
1941
Jens Axboe8c838782019-03-12 15:48:16 -06001942 if (mask) {
1943 io_cqring_ev_posted(ctx);
Jens Axboeba816ad2019-09-28 11:36:45 -06001944 io_put_req(req, NULL);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001945 }
Jens Axboe8c838782019-03-12 15:48:16 -06001946 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001947}
1948
Jens Axboe5262f562019-09-17 12:26:57 -06001949static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
1950{
1951 struct io_ring_ctx *ctx;
Jens Axboe11365042019-10-16 09:08:32 -06001952 struct io_kiocb *req;
Jens Axboe5262f562019-09-17 12:26:57 -06001953 unsigned long flags;
Jens Axboe11365042019-10-16 09:08:32 -06001954 bool comp;
Jens Axboe5262f562019-09-17 12:26:57 -06001955
1956 req = container_of(timer, struct io_kiocb, timeout.timer);
1957 ctx = req->ctx;
1958 atomic_inc(&ctx->cq_timeouts);
1959
1960 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08001961 /*
Jens Axboe11365042019-10-16 09:08:32 -06001962 * We could be racing with timeout deletion. If the list is empty,
1963 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08001964 */
Jens Axboe11365042019-10-16 09:08:32 -06001965 comp = !list_empty(&req->list);
1966 if (comp) {
1967 struct io_kiocb *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06001968
Jens Axboe11365042019-10-16 09:08:32 -06001969 /*
1970 * Adjust the reqs sequence before the current one because it
1971 * will consume a slot in the cq_ring and the the cq_tail
1972 * pointer will be increased, otherwise other timeout reqs may
1973 * return in advance without waiting for enough wait_nr.
1974 */
1975 prev = req;
1976 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
1977 prev->sequence++;
1978
1979 list_del_init(&req->list);
1980 io_cqring_fill_event(ctx, req->user_data, -ETIME);
1981 io_commit_cqring(ctx);
1982 }
Jens Axboe5262f562019-09-17 12:26:57 -06001983 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1984
Jens Axboe11365042019-10-16 09:08:32 -06001985 if (comp) {
1986 io_cqring_ev_posted(ctx);
1987 io_put_req(req, NULL);
1988 }
1989 return HRTIMER_NORESTART;
1990}
1991
1992/*
1993 * Remove or update an existing timeout command
1994 */
1995static int io_timeout_remove(struct io_kiocb *req,
1996 const struct io_uring_sqe *sqe)
1997{
1998 struct io_ring_ctx *ctx = req->ctx;
1999 struct io_kiocb *treq;
2000 int ret = -ENOENT;
2001 __u64 user_data;
2002 unsigned flags;
2003
2004 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2005 return -EINVAL;
2006 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
2007 return -EINVAL;
2008 flags = READ_ONCE(sqe->timeout_flags);
2009 if (flags)
2010 return -EINVAL;
2011
2012 user_data = READ_ONCE(sqe->addr);
2013 spin_lock_irq(&ctx->completion_lock);
2014 list_for_each_entry(treq, &ctx->timeout_list, list) {
2015 if (user_data == treq->user_data) {
2016 list_del_init(&treq->list);
2017 ret = 0;
2018 break;
2019 }
2020 }
2021
2022 /* didn't find timeout */
2023 if (ret) {
2024fill_ev:
2025 io_cqring_fill_event(ctx, req->user_data, ret);
2026 io_commit_cqring(ctx);
2027 spin_unlock_irq(&ctx->completion_lock);
2028 io_cqring_ev_posted(ctx);
2029 io_put_req(req, NULL);
2030 return 0;
2031 }
2032
2033 ret = hrtimer_try_to_cancel(&treq->timeout.timer);
2034 if (ret == -1) {
2035 ret = -EBUSY;
2036 goto fill_ev;
2037 }
2038
2039 io_cqring_fill_event(ctx, req->user_data, 0);
2040 io_cqring_fill_event(ctx, treq->user_data, -ECANCELED);
2041 io_commit_cqring(ctx);
2042 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06002043 io_cqring_ev_posted(ctx);
2044
Jens Axboe11365042019-10-16 09:08:32 -06002045 io_put_req(treq, NULL);
Jens Axboeba816ad2019-09-28 11:36:45 -06002046 io_put_req(req, NULL);
Jens Axboe11365042019-10-16 09:08:32 -06002047 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06002048}
2049
2050static int io_timeout(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2051{
yangerkun5da0fb12019-10-15 21:59:29 +08002052 unsigned count;
Jens Axboe5262f562019-09-17 12:26:57 -06002053 struct io_ring_ctx *ctx = req->ctx;
2054 struct list_head *entry;
Jens Axboea41525a2019-10-15 16:48:15 -06002055 enum hrtimer_mode mode;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06002056 struct timespec64 ts;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002057 unsigned span = 0;
Jens Axboea41525a2019-10-15 16:48:15 -06002058 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06002059
2060 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2061 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06002062 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len != 1)
2063 return -EINVAL;
2064 flags = READ_ONCE(sqe->timeout_flags);
2065 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06002066 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06002067
2068 if (get_timespec64(&ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06002069 return -EFAULT;
2070
Jens Axboe11365042019-10-16 09:08:32 -06002071 if (flags & IORING_TIMEOUT_ABS)
2072 mode = HRTIMER_MODE_ABS;
2073 else
2074 mode = HRTIMER_MODE_REL;
2075
2076 hrtimer_init(&req->timeout.timer, CLOCK_MONOTONIC, mode);
2077
Jens Axboe5262f562019-09-17 12:26:57 -06002078 /*
2079 * sqe->off holds how many events that need to occur for this
2080 * timeout event to be satisfied.
2081 */
2082 count = READ_ONCE(sqe->off);
2083 if (!count)
2084 count = 1;
2085
2086 req->sequence = ctx->cached_sq_head + count - 1;
yangerkun5da0fb12019-10-15 21:59:29 +08002087 /* reuse it to store the count */
2088 req->submit.sequence = count;
Jens Axboe5262f562019-09-17 12:26:57 -06002089 req->flags |= REQ_F_TIMEOUT;
2090
2091 /*
2092 * Insertion sort, ensuring the first entry in the list is always
2093 * the one we need first.
2094 */
Jens Axboe5262f562019-09-17 12:26:57 -06002095 spin_lock_irq(&ctx->completion_lock);
2096 list_for_each_prev(entry, &ctx->timeout_list) {
2097 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
yangerkun5da0fb12019-10-15 21:59:29 +08002098 unsigned nxt_sq_head;
2099 long long tmp, tmp_nxt;
Jens Axboe5262f562019-09-17 12:26:57 -06002100
yangerkun5da0fb12019-10-15 21:59:29 +08002101 /*
2102 * Since cached_sq_head + count - 1 can overflow, use type long
2103 * long to store it.
2104 */
2105 tmp = (long long)ctx->cached_sq_head + count - 1;
2106 nxt_sq_head = nxt->sequence - nxt->submit.sequence + 1;
2107 tmp_nxt = (long long)nxt_sq_head + nxt->submit.sequence - 1;
2108
2109 /*
2110 * cached_sq_head may overflow, and it will never overflow twice
2111 * once there is some timeout req still be valid.
2112 */
2113 if (ctx->cached_sq_head < nxt_sq_head)
yangerkun8b07a652019-10-17 12:12:35 +08002114 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08002115
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002116 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06002117 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002118
2119 /*
2120 * Sequence of reqs after the insert one and itself should
2121 * be adjusted because each timeout req consumes a slot.
2122 */
2123 span++;
2124 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06002125 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002126 req->sequence -= span;
Jens Axboe5262f562019-09-17 12:26:57 -06002127 list_add(&req->list, entry);
2128 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06002129 req->timeout.timer.function = io_timeout_fn;
Jens Axboea41525a2019-10-15 16:48:15 -06002130 hrtimer_start(&req->timeout.timer, timespec64_to_ktime(ts), mode);
Jens Axboe5262f562019-09-17 12:26:57 -06002131 return 0;
2132}
2133
Jens Axboede0617e2019-04-06 21:51:27 -06002134static int io_req_defer(struct io_ring_ctx *ctx, struct io_kiocb *req,
2135 const struct io_uring_sqe *sqe)
2136{
2137 struct io_uring_sqe *sqe_copy;
2138
2139 if (!io_sequence_defer(ctx, req) && list_empty(&ctx->defer_list))
2140 return 0;
2141
2142 sqe_copy = kmalloc(sizeof(*sqe_copy), GFP_KERNEL);
2143 if (!sqe_copy)
2144 return -EAGAIN;
2145
2146 spin_lock_irq(&ctx->completion_lock);
2147 if (!io_sequence_defer(ctx, req) && list_empty(&ctx->defer_list)) {
2148 spin_unlock_irq(&ctx->completion_lock);
2149 kfree(sqe_copy);
2150 return 0;
2151 }
2152
2153 memcpy(sqe_copy, sqe, sizeof(*sqe_copy));
2154 req->submit.sqe = sqe_copy;
2155
2156 INIT_WORK(&req->work, io_sq_wq_submit_work);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002157 trace_io_uring_defer(ctx, req, false);
Jens Axboede0617e2019-04-06 21:51:27 -06002158 list_add_tail(&req->list, &ctx->defer_list);
2159 spin_unlock_irq(&ctx->completion_lock);
2160 return -EIOCBQUEUED;
2161}
2162
Jens Axboe2b188cc2019-01-07 10:46:33 -07002163static int __io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
Jens Axboeba816ad2019-09-28 11:36:45 -06002164 const struct sqe_submit *s, struct io_kiocb **nxt,
2165 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002166{
Jens Axboee0c5c572019-03-12 10:18:47 -06002167 int ret, opcode;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002168
Jens Axboe9e645e112019-05-10 16:07:28 -06002169 req->user_data = READ_ONCE(s->sqe->user_data);
2170
Jens Axboe2b188cc2019-01-07 10:46:33 -07002171 if (unlikely(s->index >= ctx->sq_entries))
2172 return -EINVAL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002173
2174 opcode = READ_ONCE(s->sqe->opcode);
2175 switch (opcode) {
2176 case IORING_OP_NOP:
2177 ret = io_nop(req, req->user_data);
2178 break;
2179 case IORING_OP_READV:
Jens Axboeedafcce2019-01-09 09:16:05 -07002180 if (unlikely(s->sqe->buf_index))
2181 return -EINVAL;
Jens Axboeba816ad2019-09-28 11:36:45 -06002182 ret = io_read(req, s, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002183 break;
2184 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07002185 if (unlikely(s->sqe->buf_index))
2186 return -EINVAL;
Jens Axboeba816ad2019-09-28 11:36:45 -06002187 ret = io_write(req, s, nxt, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07002188 break;
2189 case IORING_OP_READ_FIXED:
Jens Axboeba816ad2019-09-28 11:36:45 -06002190 ret = io_read(req, s, nxt, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07002191 break;
2192 case IORING_OP_WRITE_FIXED:
Jens Axboeba816ad2019-09-28 11:36:45 -06002193 ret = io_write(req, s, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002194 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002195 case IORING_OP_FSYNC:
Jens Axboeba816ad2019-09-28 11:36:45 -06002196 ret = io_fsync(req, s->sqe, nxt, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002197 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002198 case IORING_OP_POLL_ADD:
2199 ret = io_poll_add(req, s->sqe);
2200 break;
2201 case IORING_OP_POLL_REMOVE:
2202 ret = io_poll_remove(req, s->sqe);
2203 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002204 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboeba816ad2019-09-28 11:36:45 -06002205 ret = io_sync_file_range(req, s->sqe, nxt, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002206 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002207 case IORING_OP_SENDMSG:
Jens Axboeba816ad2019-09-28 11:36:45 -06002208 ret = io_sendmsg(req, s->sqe, nxt, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06002209 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06002210 case IORING_OP_RECVMSG:
Jens Axboeba816ad2019-09-28 11:36:45 -06002211 ret = io_recvmsg(req, s->sqe, nxt, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06002212 break;
Jens Axboe5262f562019-09-17 12:26:57 -06002213 case IORING_OP_TIMEOUT:
2214 ret = io_timeout(req, s->sqe);
2215 break;
Jens Axboe11365042019-10-16 09:08:32 -06002216 case IORING_OP_TIMEOUT_REMOVE:
2217 ret = io_timeout_remove(req, s->sqe);
2218 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002219 default:
2220 ret = -EINVAL;
2221 break;
2222 }
2223
Jens Axboedef596e2019-01-09 08:59:42 -07002224 if (ret)
2225 return ret;
2226
2227 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe9e645e112019-05-10 16:07:28 -06002228 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07002229 return -EAGAIN;
2230
2231 /* workqueue context doesn't hold uring_lock, grab it now */
Jackie Liuba5290c2019-10-09 09:19:59 +08002232 if (s->in_async)
Jens Axboedef596e2019-01-09 08:59:42 -07002233 mutex_lock(&ctx->uring_lock);
2234 io_iopoll_req_issued(req);
Jackie Liuba5290c2019-10-09 09:19:59 +08002235 if (s->in_async)
Jens Axboedef596e2019-01-09 08:59:42 -07002236 mutex_unlock(&ctx->uring_lock);
2237 }
2238
2239 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002240}
2241
Jens Axboe31b51512019-01-18 22:56:34 -07002242static struct async_list *io_async_list_from_sqe(struct io_ring_ctx *ctx,
2243 const struct io_uring_sqe *sqe)
2244{
2245 switch (sqe->opcode) {
2246 case IORING_OP_READV:
2247 case IORING_OP_READ_FIXED:
2248 return &ctx->pending_async[READ];
2249 case IORING_OP_WRITEV:
2250 case IORING_OP_WRITE_FIXED:
2251 return &ctx->pending_async[WRITE];
2252 default:
2253 return NULL;
2254 }
2255}
2256
Jens Axboeedafcce2019-01-09 09:16:05 -07002257static inline bool io_sqe_needs_user(const struct io_uring_sqe *sqe)
2258{
2259 u8 opcode = READ_ONCE(sqe->opcode);
2260
2261 return !(opcode == IORING_OP_READ_FIXED ||
2262 opcode == IORING_OP_WRITE_FIXED);
2263}
2264
Jens Axboe2b188cc2019-01-07 10:46:33 -07002265static void io_sq_wq_submit_work(struct work_struct *work)
2266{
2267 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002268 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe31b51512019-01-18 22:56:34 -07002269 struct mm_struct *cur_mm = NULL;
2270 struct async_list *async_list;
2271 LIST_HEAD(req_list);
Jens Axboeedafcce2019-01-09 09:16:05 -07002272 mm_segment_t old_fs;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002273 int ret;
2274
Jens Axboe31b51512019-01-18 22:56:34 -07002275 async_list = io_async_list_from_sqe(ctx, req->submit.sqe);
2276restart:
2277 do {
2278 struct sqe_submit *s = &req->submit;
2279 const struct io_uring_sqe *sqe = s->sqe;
Jackie Liud0ee8792019-07-31 14:39:33 +08002280 unsigned int flags = req->flags;
Jens Axboeba816ad2019-09-28 11:36:45 -06002281 struct io_kiocb *nxt = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002282
Stefan Bühler8449eed2019-04-27 20:34:19 +02002283 /* Ensure we clear previously set non-block flag */
Jens Axboe31b51512019-01-18 22:56:34 -07002284 req->rw.ki_flags &= ~IOCB_NOWAIT;
2285
2286 ret = 0;
2287 if (io_sqe_needs_user(sqe) && !cur_mm) {
2288 if (!mmget_not_zero(ctx->sqo_mm)) {
2289 ret = -EFAULT;
2290 } else {
2291 cur_mm = ctx->sqo_mm;
2292 use_mm(cur_mm);
2293 old_fs = get_fs();
2294 set_fs(USER_DS);
2295 }
2296 }
2297
2298 if (!ret) {
2299 s->has_user = cur_mm != NULL;
Jackie Liuba5290c2019-10-09 09:19:59 +08002300 s->in_async = true;
Jens Axboe31b51512019-01-18 22:56:34 -07002301 do {
Jens Axboeba816ad2019-09-28 11:36:45 -06002302 ret = __io_submit_sqe(ctx, req, s, &nxt, false);
Jens Axboe31b51512019-01-18 22:56:34 -07002303 /*
2304 * We can get EAGAIN for polled IO even though
2305 * we're forcing a sync submission from here,
2306 * since we can't wait for request slots on the
2307 * block side.
2308 */
2309 if (ret != -EAGAIN)
2310 break;
2311 cond_resched();
2312 } while (1);
2313 }
Jens Axboe817869d2019-04-30 14:44:05 -06002314
2315 /* drop submission reference */
Jens Axboeba816ad2019-09-28 11:36:45 -06002316 io_put_req(req, NULL);
Jens Axboe817869d2019-04-30 14:44:05 -06002317
Jens Axboe31b51512019-01-18 22:56:34 -07002318 if (ret) {
Jens Axboec71ffb62019-05-13 20:58:29 -06002319 io_cqring_add_event(ctx, sqe->user_data, ret);
Jens Axboeba816ad2019-09-28 11:36:45 -06002320 io_put_req(req, NULL);
Jens Axboe31b51512019-01-18 22:56:34 -07002321 }
2322
2323 /* async context always use a copy of the sqe */
2324 kfree(sqe);
2325
Jens Axboeba816ad2019-09-28 11:36:45 -06002326 /* if a dependent link is ready, do that as the next one */
2327 if (!ret && nxt) {
2328 req = nxt;
2329 continue;
2330 }
2331
Zhengyuan Liuf7b76ac2019-07-16 23:26:14 +08002332 /* req from defer and link list needn't decrease async cnt */
Jackie Liud0ee8792019-07-31 14:39:33 +08002333 if (flags & (REQ_F_IO_DRAINED | REQ_F_LINK_DONE))
Zhengyuan Liuf7b76ac2019-07-16 23:26:14 +08002334 goto out;
2335
Jens Axboe31b51512019-01-18 22:56:34 -07002336 if (!async_list)
2337 break;
2338 if (!list_empty(&req_list)) {
2339 req = list_first_entry(&req_list, struct io_kiocb,
2340 list);
2341 list_del(&req->list);
2342 continue;
2343 }
2344 if (list_empty(&async_list->list))
2345 break;
2346
2347 req = NULL;
2348 spin_lock(&async_list->lock);
2349 if (list_empty(&async_list->list)) {
2350 spin_unlock(&async_list->lock);
2351 break;
2352 }
2353 list_splice_init(&async_list->list, &req_list);
2354 spin_unlock(&async_list->lock);
2355
2356 req = list_first_entry(&req_list, struct io_kiocb, list);
2357 list_del(&req->list);
2358 } while (req);
Jens Axboeedafcce2019-01-09 09:16:05 -07002359
2360 /*
Jens Axboe31b51512019-01-18 22:56:34 -07002361 * Rare case of racing with a submitter. If we find the count has
2362 * dropped to zero AND we have pending work items, then restart
2363 * the processing. This is a tiny race window.
Jens Axboeedafcce2019-01-09 09:16:05 -07002364 */
Jens Axboe31b51512019-01-18 22:56:34 -07002365 if (async_list) {
2366 ret = atomic_dec_return(&async_list->cnt);
2367 while (!ret && !list_empty(&async_list->list)) {
2368 spin_lock(&async_list->lock);
2369 atomic_inc(&async_list->cnt);
2370 list_splice_init(&async_list->list, &req_list);
2371 spin_unlock(&async_list->lock);
2372
2373 if (!list_empty(&req_list)) {
2374 req = list_first_entry(&req_list,
2375 struct io_kiocb, list);
2376 list_del(&req->list);
2377 goto restart;
2378 }
2379 ret = atomic_dec_return(&async_list->cnt);
Jens Axboeedafcce2019-01-09 09:16:05 -07002380 }
Jens Axboeedafcce2019-01-09 09:16:05 -07002381 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002382
Zhengyuan Liuf7b76ac2019-07-16 23:26:14 +08002383out:
Jens Axboe31b51512019-01-18 22:56:34 -07002384 if (cur_mm) {
Jens Axboeedafcce2019-01-09 09:16:05 -07002385 set_fs(old_fs);
Jens Axboe31b51512019-01-18 22:56:34 -07002386 unuse_mm(cur_mm);
2387 mmput(cur_mm);
Jens Axboeedafcce2019-01-09 09:16:05 -07002388 }
Jens Axboe31b51512019-01-18 22:56:34 -07002389}
Jens Axboe2b188cc2019-01-07 10:46:33 -07002390
Jens Axboe31b51512019-01-18 22:56:34 -07002391/*
2392 * See if we can piggy back onto previously submitted work, that is still
2393 * running. We currently only allow this if the new request is sequential
2394 * to the previous one we punted.
2395 */
2396static bool io_add_to_prev_work(struct async_list *list, struct io_kiocb *req)
2397{
Jens Axboe6d5d5ac2019-09-11 10:16:13 -06002398 bool ret;
Jens Axboe31b51512019-01-18 22:56:34 -07002399
2400 if (!list)
2401 return false;
2402 if (!(req->flags & REQ_F_SEQ_PREV))
2403 return false;
2404 if (!atomic_read(&list->cnt))
2405 return false;
2406
2407 ret = true;
2408 spin_lock(&list->lock);
2409 list_add_tail(&req->list, &list->list);
Zhengyuan Liuc0e48f92019-07-18 20:44:00 +08002410 /*
2411 * Ensure we see a simultaneous modification from io_sq_wq_submit_work()
2412 */
2413 smp_mb();
Jens Axboe31b51512019-01-18 22:56:34 -07002414 if (!atomic_read(&list->cnt)) {
2415 list_del_init(&req->list);
2416 ret = false;
2417 }
2418 spin_unlock(&list->lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002419
2420 trace_io_uring_add_to_prev(req, ret);
Jens Axboe31b51512019-01-18 22:56:34 -07002421 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002422}
2423
Jens Axboe09bb8392019-03-13 12:39:28 -06002424static bool io_op_needs_file(const struct io_uring_sqe *sqe)
2425{
2426 int op = READ_ONCE(sqe->opcode);
2427
2428 switch (op) {
2429 case IORING_OP_NOP:
2430 case IORING_OP_POLL_REMOVE:
2431 return false;
2432 default:
2433 return true;
2434 }
2435}
2436
2437static int io_req_set_file(struct io_ring_ctx *ctx, const struct sqe_submit *s,
2438 struct io_submit_state *state, struct io_kiocb *req)
2439{
2440 unsigned flags;
2441 int fd;
2442
2443 flags = READ_ONCE(s->sqe->flags);
2444 fd = READ_ONCE(s->sqe->fd);
2445
Jackie Liu4fe2c962019-09-09 20:50:40 +08002446 if (flags & IOSQE_IO_DRAIN)
Jens Axboede0617e2019-04-06 21:51:27 -06002447 req->flags |= REQ_F_IO_DRAIN;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002448 /*
2449 * All io need record the previous position, if LINK vs DARIN,
2450 * it can be used to mark the position of the first IO in the
2451 * link list.
2452 */
2453 req->sequence = s->sequence;
Jens Axboede0617e2019-04-06 21:51:27 -06002454
Jens Axboe60c112b2019-06-21 10:20:18 -06002455 if (!io_op_needs_file(s->sqe))
Jens Axboe09bb8392019-03-13 12:39:28 -06002456 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06002457
2458 if (flags & IOSQE_FIXED_FILE) {
2459 if (unlikely(!ctx->user_files ||
2460 (unsigned) fd >= ctx->nr_user_files))
2461 return -EBADF;
Jens Axboe08a45172019-10-03 08:11:03 -06002462 if (!ctx->user_files[fd])
2463 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06002464 req->file = ctx->user_files[fd];
2465 req->flags |= REQ_F_FIXED_FILE;
2466 } else {
2467 if (s->needs_fixed_file)
2468 return -EBADF;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002469 trace_io_uring_file_get(ctx, fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06002470 req->file = io_file_get(state, fd);
2471 if (unlikely(!req->file))
2472 return -EBADF;
2473 }
2474
2475 return 0;
2476}
2477
Jackie Liu4fe2c962019-09-09 20:50:40 +08002478static int __io_queue_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
Jens Axboebc808bc2019-10-22 13:14:37 -06002479 struct sqe_submit *s)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002480{
Jens Axboee0c5c572019-03-12 10:18:47 -06002481 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002482
Jens Axboeba816ad2019-09-28 11:36:45 -06002483 ret = __io_submit_sqe(ctx, req, s, NULL, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06002484
2485 /*
2486 * We async punt it if the file wasn't marked NOWAIT, or if the file
2487 * doesn't support non-blocking read/write attempts
2488 */
2489 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
2490 (req->flags & REQ_F_MUST_PUNT))) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07002491 struct io_uring_sqe *sqe_copy;
2492
Jackie Liu954dab12019-09-18 10:37:52 +08002493 sqe_copy = kmemdup(s->sqe, sizeof(*sqe_copy), GFP_KERNEL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002494 if (sqe_copy) {
Jens Axboe31b51512019-01-18 22:56:34 -07002495 struct async_list *list;
2496
Jens Axboe2b188cc2019-01-07 10:46:33 -07002497 s->sqe = sqe_copy;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002498 memcpy(&req->submit, s, sizeof(*s));
Jens Axboe31b51512019-01-18 22:56:34 -07002499 list = io_async_list_from_sqe(ctx, s->sqe);
2500 if (!io_add_to_prev_work(list, req)) {
2501 if (list)
2502 atomic_inc(&list->cnt);
2503 INIT_WORK(&req->work, io_sq_wq_submit_work);
Jens Axboe18d9be12019-09-10 09:13:05 -06002504 io_queue_async_work(ctx, req);
Jens Axboe31b51512019-01-18 22:56:34 -07002505 }
Jens Axboee65ef562019-03-12 10:16:44 -06002506
2507 /*
2508 * Queued up for async execution, worker will release
Jens Axboe9e645e112019-05-10 16:07:28 -06002509 * submit reference when the iocb is actually submitted.
Jens Axboee65ef562019-03-12 10:16:44 -06002510 */
2511 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002512 }
2513 }
Jens Axboee65ef562019-03-12 10:16:44 -06002514
2515 /* drop submission reference */
Jens Axboeba816ad2019-09-28 11:36:45 -06002516 io_put_req(req, NULL);
Jens Axboee65ef562019-03-12 10:16:44 -06002517
2518 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06002519 if (ret) {
2520 io_cqring_add_event(ctx, req->user_data, ret);
2521 if (req->flags & REQ_F_LINK)
2522 req->flags |= REQ_F_FAIL_LINK;
Jens Axboeba816ad2019-09-28 11:36:45 -06002523 io_put_req(req, NULL);
Jens Axboe9e645e112019-05-10 16:07:28 -06002524 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002525
2526 return ret;
2527}
2528
Jackie Liu4fe2c962019-09-09 20:50:40 +08002529static int io_queue_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
Jens Axboebc808bc2019-10-22 13:14:37 -06002530 struct sqe_submit *s)
Jackie Liu4fe2c962019-09-09 20:50:40 +08002531{
2532 int ret;
2533
2534 ret = io_req_defer(ctx, req, s->sqe);
2535 if (ret) {
2536 if (ret != -EIOCBQUEUED) {
Jens Axboeba816ad2019-09-28 11:36:45 -06002537 io_free_req(req, NULL);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002538 io_cqring_add_event(ctx, s->sqe->user_data, ret);
2539 }
2540 return 0;
2541 }
2542
Jens Axboebc808bc2019-10-22 13:14:37 -06002543 return __io_queue_sqe(ctx, req, s);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002544}
2545
2546static int io_queue_link_head(struct io_ring_ctx *ctx, struct io_kiocb *req,
Jens Axboebc808bc2019-10-22 13:14:37 -06002547 struct sqe_submit *s, struct io_kiocb *shadow)
Jackie Liu4fe2c962019-09-09 20:50:40 +08002548{
2549 int ret;
2550 int need_submit = false;
2551
2552 if (!shadow)
Jens Axboebc808bc2019-10-22 13:14:37 -06002553 return io_queue_sqe(ctx, req, s);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002554
2555 /*
2556 * Mark the first IO in link list as DRAIN, let all the following
2557 * IOs enter the defer list. all IO needs to be completed before link
2558 * list.
2559 */
2560 req->flags |= REQ_F_IO_DRAIN;
2561 ret = io_req_defer(ctx, req, s->sqe);
2562 if (ret) {
2563 if (ret != -EIOCBQUEUED) {
Jens Axboeba816ad2019-09-28 11:36:45 -06002564 io_free_req(req, NULL);
Pavel Begunkov7b202382019-10-27 22:10:36 +03002565 __io_free_req(shadow);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002566 io_cqring_add_event(ctx, s->sqe->user_data, ret);
2567 return 0;
2568 }
2569 } else {
2570 /*
2571 * If ret == 0 means that all IOs in front of link io are
2572 * running done. let's queue link head.
2573 */
2574 need_submit = true;
2575 }
2576
2577 /* Insert shadow req to defer_list, blocking next IOs */
2578 spin_lock_irq(&ctx->completion_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002579 trace_io_uring_defer(ctx, shadow, true);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002580 list_add_tail(&shadow->list, &ctx->defer_list);
2581 spin_unlock_irq(&ctx->completion_lock);
2582
2583 if (need_submit)
Jens Axboebc808bc2019-10-22 13:14:37 -06002584 return __io_queue_sqe(ctx, req, s);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002585
2586 return 0;
2587}
2588
Jens Axboe9e645e112019-05-10 16:07:28 -06002589#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK)
2590
2591static void io_submit_sqe(struct io_ring_ctx *ctx, struct sqe_submit *s,
Jens Axboebc808bc2019-10-22 13:14:37 -06002592 struct io_submit_state *state, struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06002593{
2594 struct io_uring_sqe *sqe_copy;
2595 struct io_kiocb *req;
2596 int ret;
2597
2598 /* enforce forwards compatibility on users */
2599 if (unlikely(s->sqe->flags & ~SQE_VALID_FLAGS)) {
2600 ret = -EINVAL;
2601 goto err;
2602 }
2603
2604 req = io_get_req(ctx, state);
2605 if (unlikely(!req)) {
2606 ret = -EAGAIN;
2607 goto err;
2608 }
2609
2610 ret = io_req_set_file(ctx, s, state, req);
2611 if (unlikely(ret)) {
2612err_req:
Jens Axboeba816ad2019-09-28 11:36:45 -06002613 io_free_req(req, NULL);
Jens Axboe9e645e112019-05-10 16:07:28 -06002614err:
2615 io_cqring_add_event(ctx, s->sqe->user_data, ret);
2616 return;
2617 }
2618
Pavel Begunkov84d55dc2019-10-25 12:31:29 +03002619 req->user_data = s->sqe->user_data;
2620
Jens Axboe9e645e112019-05-10 16:07:28 -06002621 /*
2622 * If we already have a head request, queue this one for async
2623 * submittal once the head completes. If we don't have a head but
2624 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
2625 * submitted sync once the chain is complete. If none of those
2626 * conditions are true (normal request), then just queue it.
2627 */
2628 if (*link) {
2629 struct io_kiocb *prev = *link;
2630
2631 sqe_copy = kmemdup(s->sqe, sizeof(*sqe_copy), GFP_KERNEL);
2632 if (!sqe_copy) {
2633 ret = -EAGAIN;
2634 goto err_req;
2635 }
2636
2637 s->sqe = sqe_copy;
2638 memcpy(&req->submit, s, sizeof(*s));
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002639 trace_io_uring_link(ctx, req, prev);
Jens Axboe9e645e112019-05-10 16:07:28 -06002640 list_add_tail(&req->list, &prev->link_list);
2641 } else if (s->sqe->flags & IOSQE_IO_LINK) {
2642 req->flags |= REQ_F_LINK;
2643
2644 memcpy(&req->submit, s, sizeof(*s));
2645 INIT_LIST_HEAD(&req->link_list);
2646 *link = req;
2647 } else {
Jens Axboebc808bc2019-10-22 13:14:37 -06002648 io_queue_sqe(ctx, req, s);
Jens Axboe9e645e112019-05-10 16:07:28 -06002649 }
2650}
2651
Jens Axboe9a56a232019-01-09 09:06:50 -07002652/*
2653 * Batched submission is done, ensure local IO is flushed out.
2654 */
2655static void io_submit_state_end(struct io_submit_state *state)
2656{
2657 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06002658 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07002659 if (state->free_reqs)
2660 kmem_cache_free_bulk(req_cachep, state->free_reqs,
2661 &state->reqs[state->cur_req]);
Jens Axboe9a56a232019-01-09 09:06:50 -07002662}
2663
2664/*
2665 * Start submission side cache.
2666 */
2667static void io_submit_state_start(struct io_submit_state *state,
2668 struct io_ring_ctx *ctx, unsigned max_ios)
2669{
2670 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07002671 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07002672 state->file = NULL;
2673 state->ios_left = max_ios;
2674}
2675
Jens Axboe2b188cc2019-01-07 10:46:33 -07002676static void io_commit_sqring(struct io_ring_ctx *ctx)
2677{
Hristo Venev75b28af2019-08-26 17:23:46 +00002678 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002679
Hristo Venev75b28af2019-08-26 17:23:46 +00002680 if (ctx->cached_sq_head != READ_ONCE(rings->sq.head)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07002681 /*
2682 * Ensure any loads from the SQEs are done at this point,
2683 * since once we write the new head, the application could
2684 * write new data to them.
2685 */
Hristo Venev75b28af2019-08-26 17:23:46 +00002686 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002687 }
2688}
2689
2690/*
Jens Axboe2b188cc2019-01-07 10:46:33 -07002691 * Fetch an sqe, if one is available. Note that s->sqe will point to memory
2692 * that is mapped by userspace. This means that care needs to be taken to
2693 * ensure that reads are stable, as we cannot rely on userspace always
2694 * being a good citizen. If members of the sqe are validated and then later
2695 * used, it's important that those reads are done through READ_ONCE() to
2696 * prevent a re-load down the line.
2697 */
2698static bool io_get_sqring(struct io_ring_ctx *ctx, struct sqe_submit *s)
2699{
Hristo Venev75b28af2019-08-26 17:23:46 +00002700 struct io_rings *rings = ctx->rings;
2701 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002702 unsigned head;
2703
2704 /*
2705 * The cached sq head (or cq tail) serves two purposes:
2706 *
2707 * 1) allows us to batch the cost of updating the user visible
2708 * head updates.
2709 * 2) allows the kernel side to track the head on its own, even
2710 * though the application is the one updating it.
2711 */
2712 head = ctx->cached_sq_head;
Stefan Bühlere523a292019-04-19 11:57:44 +02002713 /* make sure SQ entry isn't read before tail */
Hristo Venev75b28af2019-08-26 17:23:46 +00002714 if (head == smp_load_acquire(&rings->sq.tail))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002715 return false;
2716
Hristo Venev75b28af2019-08-26 17:23:46 +00002717 head = READ_ONCE(sq_array[head & ctx->sq_mask]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002718 if (head < ctx->sq_entries) {
2719 s->index = head;
2720 s->sqe = &ctx->sq_sqes[head];
Jackie Liu8776f3f2019-09-09 20:50:39 +08002721 s->sequence = ctx->cached_sq_head;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002722 ctx->cached_sq_head++;
2723 return true;
2724 }
2725
2726 /* drop invalid entries */
2727 ctx->cached_sq_head++;
Jens Axboe498ccd92019-10-25 10:04:25 -06002728 ctx->cached_sq_dropped++;
2729 WRITE_ONCE(rings->sq_dropped, ctx->cached_sq_dropped);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002730 return false;
2731}
2732
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002733static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
2734 bool has_user, bool mm_fault)
Jens Axboe6c271ce2019-01-10 11:22:30 -07002735{
2736 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002737 struct io_kiocb *link = NULL;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002738 struct io_kiocb *shadow_req = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002739 bool prev_was_link = false;
2740 int i, submitted = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002741
2742 if (nr > IO_PLUG_THRESHOLD) {
2743 io_submit_state_start(&state, ctx, nr);
2744 statep = &state;
2745 }
2746
2747 for (i = 0; i < nr; i++) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002748 struct sqe_submit s;
2749
2750 if (!io_get_sqring(ctx, &s))
2751 break;
2752
Jens Axboe9e645e112019-05-10 16:07:28 -06002753 /*
2754 * If previous wasn't linked and we have a linked command,
2755 * that's the end of the chain. Submit the previous link.
2756 */
2757 if (!prev_was_link && link) {
Jens Axboebc808bc2019-10-22 13:14:37 -06002758 io_queue_link_head(ctx, link, &link->submit, shadow_req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002759 link = NULL;
Jackie Liu5f5ad9c2019-09-18 10:37:53 +08002760 shadow_req = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002761 }
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002762 prev_was_link = (s.sqe->flags & IOSQE_IO_LINK) != 0;
Jens Axboe9e645e112019-05-10 16:07:28 -06002763
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002764 if (link && (s.sqe->flags & IOSQE_IO_DRAIN)) {
Jackie Liu4fe2c962019-09-09 20:50:40 +08002765 if (!shadow_req) {
2766 shadow_req = io_get_req(ctx, NULL);
Jackie Liua1041c22019-09-18 17:25:52 +08002767 if (unlikely(!shadow_req))
2768 goto out;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002769 shadow_req->flags |= (REQ_F_IO_DRAIN | REQ_F_SHADOW_DRAIN);
2770 refcount_dec(&shadow_req->refs);
2771 }
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002772 shadow_req->sequence = s.sequence;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002773 }
2774
Jackie Liua1041c22019-09-18 17:25:52 +08002775out:
Jens Axboe6c271ce2019-01-10 11:22:30 -07002776 if (unlikely(mm_fault)) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002777 io_cqring_add_event(ctx, s.sqe->user_data,
Jens Axboe9e645e112019-05-10 16:07:28 -06002778 -EFAULT);
Jens Axboe6c271ce2019-01-10 11:22:30 -07002779 } else {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002780 s.has_user = has_user;
Jackie Liuba5290c2019-10-09 09:19:59 +08002781 s.in_async = true;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002782 s.needs_fixed_file = true;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002783 trace_io_uring_submit_sqe(ctx, true, true);
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002784 io_submit_sqe(ctx, &s, statep, &link);
Jens Axboe6c271ce2019-01-10 11:22:30 -07002785 submitted++;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002786 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07002787 }
2788
Jens Axboe9e645e112019-05-10 16:07:28 -06002789 if (link)
Jens Axboebc808bc2019-10-22 13:14:37 -06002790 io_queue_link_head(ctx, link, &link->submit, shadow_req);
Jens Axboe6c271ce2019-01-10 11:22:30 -07002791 if (statep)
2792 io_submit_state_end(&state);
2793
2794 return submitted;
2795}
2796
2797static int io_sq_thread(void *data)
2798{
Jens Axboe6c271ce2019-01-10 11:22:30 -07002799 struct io_ring_ctx *ctx = data;
2800 struct mm_struct *cur_mm = NULL;
2801 mm_segment_t old_fs;
2802 DEFINE_WAIT(wait);
2803 unsigned inflight;
2804 unsigned long timeout;
2805
Jackie Liua4c0b3d2019-07-08 13:41:12 +08002806 complete(&ctx->sqo_thread_started);
2807
Jens Axboe6c271ce2019-01-10 11:22:30 -07002808 old_fs = get_fs();
2809 set_fs(USER_DS);
2810
2811 timeout = inflight = 0;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02002812 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002813 bool mm_fault = false;
2814 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002815
2816 if (inflight) {
2817 unsigned nr_events = 0;
2818
2819 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe2b2ed972019-10-25 10:06:15 -06002820 /*
2821 * inflight is the count of the maximum possible
2822 * entries we submitted, but it can be smaller
2823 * if we dropped some of them. If we don't have
2824 * poll entries available, then we know that we
2825 * have nothing left to poll for. Reset the
2826 * inflight count to zero in that case.
2827 */
2828 mutex_lock(&ctx->uring_lock);
2829 if (!list_empty(&ctx->poll_list))
2830 __io_iopoll_check(ctx, &nr_events, 0);
2831 else
2832 inflight = 0;
2833 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07002834 } else {
2835 /*
2836 * Normal IO, just pretend everything completed.
2837 * We don't have to poll completions for that.
2838 */
2839 nr_events = inflight;
2840 }
2841
2842 inflight -= nr_events;
2843 if (!inflight)
2844 timeout = jiffies + ctx->sq_thread_idle;
2845 }
2846
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002847 to_submit = io_sqring_entries(ctx);
2848 if (!to_submit) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07002849 /*
2850 * We're polling. If we're within the defined idle
2851 * period, then let us spin without work before going
2852 * to sleep.
2853 */
2854 if (inflight || !time_after(jiffies, timeout)) {
Jens Axboe9831a902019-09-19 09:48:55 -06002855 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07002856 continue;
2857 }
2858
2859 /*
2860 * Drop cur_mm before scheduling, we can't hold it for
2861 * long periods (or over schedule()). Do this before
2862 * adding ourselves to the waitqueue, as the unuse/drop
2863 * may sleep.
2864 */
2865 if (cur_mm) {
2866 unuse_mm(cur_mm);
2867 mmput(cur_mm);
2868 cur_mm = NULL;
2869 }
2870
2871 prepare_to_wait(&ctx->sqo_wait, &wait,
2872 TASK_INTERRUPTIBLE);
2873
2874 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00002875 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02002876 /* make sure to read SQ tail after writing flags */
2877 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07002878
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002879 to_submit = io_sqring_entries(ctx);
2880 if (!to_submit) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02002881 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07002882 finish_wait(&ctx->sqo_wait, &wait);
2883 break;
2884 }
2885 if (signal_pending(current))
2886 flush_signals(current);
2887 schedule();
2888 finish_wait(&ctx->sqo_wait, &wait);
2889
Hristo Venev75b28af2019-08-26 17:23:46 +00002890 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002891 continue;
2892 }
2893 finish_wait(&ctx->sqo_wait, &wait);
2894
Hristo Venev75b28af2019-08-26 17:23:46 +00002895 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002896 }
2897
Jens Axboe6c271ce2019-01-10 11:22:30 -07002898 /* Unless all new commands are FIXED regions, grab mm */
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002899 if (!cur_mm) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07002900 mm_fault = !mmget_not_zero(ctx->sqo_mm);
2901 if (!mm_fault) {
2902 use_mm(ctx->sqo_mm);
2903 cur_mm = ctx->sqo_mm;
2904 }
2905 }
2906
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002907 to_submit = min(to_submit, ctx->sq_entries);
2908 inflight += io_submit_sqes(ctx, to_submit, cur_mm != NULL,
2909 mm_fault);
Jens Axboe6c271ce2019-01-10 11:22:30 -07002910
2911 /* Commit SQ ring head once we've consumed all SQEs */
2912 io_commit_sqring(ctx);
2913 }
2914
2915 set_fs(old_fs);
2916 if (cur_mm) {
2917 unuse_mm(cur_mm);
2918 mmput(cur_mm);
2919 }
Jens Axboe06058632019-04-13 09:26:03 -06002920
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02002921 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06002922
Jens Axboe6c271ce2019-01-10 11:22:30 -07002923 return 0;
2924}
2925
Jens Axboebc808bc2019-10-22 13:14:37 -06002926static int io_ring_submit(struct io_ring_ctx *ctx, unsigned int to_submit)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002927{
Jens Axboe9a56a232019-01-09 09:06:50 -07002928 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002929 struct io_kiocb *link = NULL;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002930 struct io_kiocb *shadow_req = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002931 bool prev_was_link = false;
Jens Axboe5c8b0b52019-04-30 10:16:07 -06002932 int i, submit = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002933
Jens Axboe9a56a232019-01-09 09:06:50 -07002934 if (to_submit > IO_PLUG_THRESHOLD) {
2935 io_submit_state_start(&state, ctx, to_submit);
2936 statep = &state;
2937 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002938
2939 for (i = 0; i < to_submit; i++) {
2940 struct sqe_submit s;
2941
2942 if (!io_get_sqring(ctx, &s))
2943 break;
2944
Jens Axboe9e645e112019-05-10 16:07:28 -06002945 /*
2946 * If previous wasn't linked and we have a linked command,
2947 * that's the end of the chain. Submit the previous link.
2948 */
2949 if (!prev_was_link && link) {
Jens Axboebc808bc2019-10-22 13:14:37 -06002950 io_queue_link_head(ctx, link, &link->submit, shadow_req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002951 link = NULL;
Jackie Liu5f5ad9c2019-09-18 10:37:53 +08002952 shadow_req = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002953 }
2954 prev_was_link = (s.sqe->flags & IOSQE_IO_LINK) != 0;
2955
Jackie Liu4fe2c962019-09-09 20:50:40 +08002956 if (link && (s.sqe->flags & IOSQE_IO_DRAIN)) {
2957 if (!shadow_req) {
2958 shadow_req = io_get_req(ctx, NULL);
Jackie Liua1041c22019-09-18 17:25:52 +08002959 if (unlikely(!shadow_req))
2960 goto out;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002961 shadow_req->flags |= (REQ_F_IO_DRAIN | REQ_F_SHADOW_DRAIN);
2962 refcount_dec(&shadow_req->refs);
2963 }
2964 shadow_req->sequence = s.sequence;
2965 }
2966
Jackie Liua1041c22019-09-18 17:25:52 +08002967out:
Jens Axboe2b188cc2019-01-07 10:46:33 -07002968 s.has_user = true;
Jackie Liuba5290c2019-10-09 09:19:59 +08002969 s.in_async = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002970 s.needs_fixed_file = false;
Jens Axboe5c8b0b52019-04-30 10:16:07 -06002971 submit++;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002972 trace_io_uring_submit_sqe(ctx, true, false);
Jens Axboebc808bc2019-10-22 13:14:37 -06002973 io_submit_sqe(ctx, &s, statep, &link);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002974 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002975
Jens Axboe9e645e112019-05-10 16:07:28 -06002976 if (link)
Jens Axboebc808bc2019-10-22 13:14:37 -06002977 io_queue_link_head(ctx, link, &link->submit, shadow_req);
Jens Axboe9a56a232019-01-09 09:06:50 -07002978 if (statep)
2979 io_submit_state_end(statep);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002980
Pavel Begunkov935d1e42019-10-25 12:31:31 +03002981 io_commit_sqring(ctx);
2982
Jens Axboe5c8b0b52019-04-30 10:16:07 -06002983 return submit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002984}
2985
Jens Axboebda52162019-09-24 13:47:15 -06002986struct io_wait_queue {
2987 struct wait_queue_entry wq;
2988 struct io_ring_ctx *ctx;
2989 unsigned to_wait;
2990 unsigned nr_timeouts;
2991};
2992
2993static inline bool io_should_wake(struct io_wait_queue *iowq)
2994{
2995 struct io_ring_ctx *ctx = iowq->ctx;
2996
2997 /*
2998 * Wake up if we have enough events, or if a timeout occured since we
2999 * started waiting. For timeouts, we always want to return to userspace,
3000 * regardless of event count.
3001 */
3002 return io_cqring_events(ctx->rings) >= iowq->to_wait ||
3003 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
3004}
3005
3006static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
3007 int wake_flags, void *key)
3008{
3009 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
3010 wq);
3011
3012 if (!io_should_wake(iowq))
3013 return -1;
3014
3015 return autoremove_wake_function(curr, mode, wake_flags, key);
3016}
3017
Jens Axboe2b188cc2019-01-07 10:46:33 -07003018/*
3019 * Wait until events become available, if we don't already have some. The
3020 * application must reap them itself, as they reside on the shared cq ring.
3021 */
3022static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
3023 const sigset_t __user *sig, size_t sigsz)
3024{
Jens Axboebda52162019-09-24 13:47:15 -06003025 struct io_wait_queue iowq = {
3026 .wq = {
3027 .private = current,
3028 .func = io_wake_function,
3029 .entry = LIST_HEAD_INIT(iowq.wq.entry),
3030 },
3031 .ctx = ctx,
3032 .to_wait = min_events,
3033 };
Hristo Venev75b28af2019-08-26 17:23:46 +00003034 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003035 int ret;
3036
Hristo Venev75b28af2019-08-26 17:23:46 +00003037 if (io_cqring_events(rings) >= min_events)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003038 return 0;
3039
3040 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01003041#ifdef CONFIG_COMPAT
3042 if (in_compat_syscall())
3043 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07003044 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01003045 else
3046#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07003047 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01003048
Jens Axboe2b188cc2019-01-07 10:46:33 -07003049 if (ret)
3050 return ret;
3051 }
3052
Jens Axboebda52162019-09-24 13:47:15 -06003053 ret = 0;
3054 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02003055 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06003056 do {
3057 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
3058 TASK_INTERRUPTIBLE);
3059 if (io_should_wake(&iowq))
3060 break;
3061 schedule();
3062 if (signal_pending(current)) {
3063 ret = -ERESTARTSYS;
3064 break;
3065 }
3066 } while (1);
3067 finish_wait(&ctx->wait, &iowq.wq);
3068
Oleg Nesterovb7724342019-07-16 16:29:53 -07003069 restore_saved_sigmask_unless(ret == -ERESTARTSYS);
Oleg Nesterov97abc882019-06-28 12:06:50 -07003070 if (ret == -ERESTARTSYS)
3071 ret = -EINTR;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003072
Hristo Venev75b28af2019-08-26 17:23:46 +00003073 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003074}
3075
Jens Axboe6b063142019-01-10 22:13:58 -07003076static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
3077{
3078#if defined(CONFIG_UNIX)
3079 if (ctx->ring_sock) {
3080 struct sock *sock = ctx->ring_sock->sk;
3081 struct sk_buff *skb;
3082
3083 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
3084 kfree_skb(skb);
3085 }
3086#else
3087 int i;
3088
3089 for (i = 0; i < ctx->nr_user_files; i++)
Jens Axboe08a45172019-10-03 08:11:03 -06003090 if (ctx->user_files[i])
3091 fput(ctx->user_files[i]);
Jens Axboe6b063142019-01-10 22:13:58 -07003092#endif
3093}
3094
3095static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
3096{
3097 if (!ctx->user_files)
3098 return -ENXIO;
3099
3100 __io_sqe_files_unregister(ctx);
3101 kfree(ctx->user_files);
3102 ctx->user_files = NULL;
3103 ctx->nr_user_files = 0;
3104 return 0;
3105}
3106
Jens Axboe6c271ce2019-01-10 11:22:30 -07003107static void io_sq_thread_stop(struct io_ring_ctx *ctx)
3108{
3109 if (ctx->sqo_thread) {
Jackie Liua4c0b3d2019-07-08 13:41:12 +08003110 wait_for_completion(&ctx->sqo_thread_started);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003111 /*
3112 * The park is a bit of a work-around, without it we get
3113 * warning spews on shutdown with SQPOLL set and affinity
3114 * set to a single CPU.
3115 */
Jens Axboe06058632019-04-13 09:26:03 -06003116 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003117 kthread_stop(ctx->sqo_thread);
3118 ctx->sqo_thread = NULL;
3119 }
3120}
3121
Jens Axboe6b063142019-01-10 22:13:58 -07003122static void io_finish_async(struct io_ring_ctx *ctx)
3123{
Jens Axboe54a91f32019-09-10 09:15:04 -06003124 int i;
3125
Jens Axboe6c271ce2019-01-10 11:22:30 -07003126 io_sq_thread_stop(ctx);
3127
Jens Axboe54a91f32019-09-10 09:15:04 -06003128 for (i = 0; i < ARRAY_SIZE(ctx->sqo_wq); i++) {
3129 if (ctx->sqo_wq[i]) {
3130 destroy_workqueue(ctx->sqo_wq[i]);
3131 ctx->sqo_wq[i] = NULL;
3132 }
Jens Axboe6b063142019-01-10 22:13:58 -07003133 }
3134}
3135
3136#if defined(CONFIG_UNIX)
3137static void io_destruct_skb(struct sk_buff *skb)
3138{
3139 struct io_ring_ctx *ctx = skb->sk->sk_user_data;
Jens Axboe8a997342019-10-09 14:40:13 -06003140 int i;
Jens Axboe6b063142019-01-10 22:13:58 -07003141
Jens Axboe8a997342019-10-09 14:40:13 -06003142 for (i = 0; i < ARRAY_SIZE(ctx->sqo_wq); i++)
3143 if (ctx->sqo_wq[i])
3144 flush_workqueue(ctx->sqo_wq[i]);
3145
Jens Axboe6b063142019-01-10 22:13:58 -07003146 unix_destruct_scm(skb);
3147}
3148
3149/*
3150 * Ensure the UNIX gc is aware of our file set, so we are certain that
3151 * the io_uring can be safely unregistered on process exit, even if we have
3152 * loops in the file referencing.
3153 */
3154static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
3155{
3156 struct sock *sk = ctx->ring_sock->sk;
3157 struct scm_fp_list *fpl;
3158 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06003159 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07003160
3161 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
3162 unsigned long inflight = ctx->user->unix_inflight + nr;
3163
3164 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
3165 return -EMFILE;
3166 }
3167
3168 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
3169 if (!fpl)
3170 return -ENOMEM;
3171
3172 skb = alloc_skb(0, GFP_KERNEL);
3173 if (!skb) {
3174 kfree(fpl);
3175 return -ENOMEM;
3176 }
3177
3178 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07003179
Jens Axboe08a45172019-10-03 08:11:03 -06003180 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07003181 fpl->user = get_uid(ctx->user);
3182 for (i = 0; i < nr; i++) {
Jens Axboe08a45172019-10-03 08:11:03 -06003183 if (!ctx->user_files[i + offset])
3184 continue;
3185 fpl->fp[nr_files] = get_file(ctx->user_files[i + offset]);
3186 unix_inflight(fpl->user, fpl->fp[nr_files]);
3187 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07003188 }
3189
Jens Axboe08a45172019-10-03 08:11:03 -06003190 if (nr_files) {
3191 fpl->max = SCM_MAX_FD;
3192 fpl->count = nr_files;
3193 UNIXCB(skb).fp = fpl;
3194 skb->destructor = io_destruct_skb;
3195 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
3196 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07003197
Jens Axboe08a45172019-10-03 08:11:03 -06003198 for (i = 0; i < nr_files; i++)
3199 fput(fpl->fp[i]);
3200 } else {
3201 kfree_skb(skb);
3202 kfree(fpl);
3203 }
Jens Axboe6b063142019-01-10 22:13:58 -07003204
3205 return 0;
3206}
3207
3208/*
3209 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
3210 * causes regular reference counting to break down. We rely on the UNIX
3211 * garbage collection to take care of this problem for us.
3212 */
3213static int io_sqe_files_scm(struct io_ring_ctx *ctx)
3214{
3215 unsigned left, total;
3216 int ret = 0;
3217
3218 total = 0;
3219 left = ctx->nr_user_files;
3220 while (left) {
3221 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07003222
3223 ret = __io_sqe_files_scm(ctx, this_files, total);
3224 if (ret)
3225 break;
3226 left -= this_files;
3227 total += this_files;
3228 }
3229
3230 if (!ret)
3231 return 0;
3232
3233 while (total < ctx->nr_user_files) {
Jens Axboe08a45172019-10-03 08:11:03 -06003234 if (ctx->user_files[total])
3235 fput(ctx->user_files[total]);
Jens Axboe6b063142019-01-10 22:13:58 -07003236 total++;
3237 }
3238
3239 return ret;
3240}
3241#else
3242static int io_sqe_files_scm(struct io_ring_ctx *ctx)
3243{
3244 return 0;
3245}
3246#endif
3247
3248static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
3249 unsigned nr_args)
3250{
3251 __s32 __user *fds = (__s32 __user *) arg;
3252 int fd, ret = 0;
3253 unsigned i;
3254
3255 if (ctx->user_files)
3256 return -EBUSY;
3257 if (!nr_args)
3258 return -EINVAL;
3259 if (nr_args > IORING_MAX_FIXED_FILES)
3260 return -EMFILE;
3261
3262 ctx->user_files = kcalloc(nr_args, sizeof(struct file *), GFP_KERNEL);
3263 if (!ctx->user_files)
3264 return -ENOMEM;
3265
Jens Axboe08a45172019-10-03 08:11:03 -06003266 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
Jens Axboe6b063142019-01-10 22:13:58 -07003267 ret = -EFAULT;
3268 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
3269 break;
Jens Axboe08a45172019-10-03 08:11:03 -06003270 /* allow sparse sets */
3271 if (fd == -1) {
3272 ret = 0;
3273 continue;
3274 }
Jens Axboe6b063142019-01-10 22:13:58 -07003275
3276 ctx->user_files[i] = fget(fd);
3277
3278 ret = -EBADF;
3279 if (!ctx->user_files[i])
3280 break;
3281 /*
3282 * Don't allow io_uring instances to be registered. If UNIX
3283 * isn't enabled, then this causes a reference cycle and this
3284 * instance can never get freed. If UNIX is enabled we'll
3285 * handle it just fine, but there's still no point in allowing
3286 * a ring fd as it doesn't support regular read/write anyway.
3287 */
3288 if (ctx->user_files[i]->f_op == &io_uring_fops) {
3289 fput(ctx->user_files[i]);
3290 break;
3291 }
Jens Axboe6b063142019-01-10 22:13:58 -07003292 ret = 0;
3293 }
3294
3295 if (ret) {
3296 for (i = 0; i < ctx->nr_user_files; i++)
Jens Axboe08a45172019-10-03 08:11:03 -06003297 if (ctx->user_files[i])
3298 fput(ctx->user_files[i]);
Jens Axboe6b063142019-01-10 22:13:58 -07003299
3300 kfree(ctx->user_files);
Jens Axboe25adf502019-04-03 09:52:40 -06003301 ctx->user_files = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003302 ctx->nr_user_files = 0;
3303 return ret;
3304 }
3305
3306 ret = io_sqe_files_scm(ctx);
3307 if (ret)
3308 io_sqe_files_unregister(ctx);
3309
3310 return ret;
3311}
3312
Jens Axboec3a31e62019-10-03 13:59:56 -06003313static void io_sqe_file_unregister(struct io_ring_ctx *ctx, int index)
3314{
3315#if defined(CONFIG_UNIX)
3316 struct file *file = ctx->user_files[index];
3317 struct sock *sock = ctx->ring_sock->sk;
3318 struct sk_buff_head list, *head = &sock->sk_receive_queue;
3319 struct sk_buff *skb;
3320 int i;
3321
3322 __skb_queue_head_init(&list);
3323
3324 /*
3325 * Find the skb that holds this file in its SCM_RIGHTS. When found,
3326 * remove this entry and rearrange the file array.
3327 */
3328 skb = skb_dequeue(head);
3329 while (skb) {
3330 struct scm_fp_list *fp;
3331
3332 fp = UNIXCB(skb).fp;
3333 for (i = 0; i < fp->count; i++) {
3334 int left;
3335
3336 if (fp->fp[i] != file)
3337 continue;
3338
3339 unix_notinflight(fp->user, fp->fp[i]);
3340 left = fp->count - 1 - i;
3341 if (left) {
3342 memmove(&fp->fp[i], &fp->fp[i + 1],
3343 left * sizeof(struct file *));
3344 }
3345 fp->count--;
3346 if (!fp->count) {
3347 kfree_skb(skb);
3348 skb = NULL;
3349 } else {
3350 __skb_queue_tail(&list, skb);
3351 }
3352 fput(file);
3353 file = NULL;
3354 break;
3355 }
3356
3357 if (!file)
3358 break;
3359
3360 __skb_queue_tail(&list, skb);
3361
3362 skb = skb_dequeue(head);
3363 }
3364
3365 if (skb_peek(&list)) {
3366 spin_lock_irq(&head->lock);
3367 while ((skb = __skb_dequeue(&list)) != NULL)
3368 __skb_queue_tail(head, skb);
3369 spin_unlock_irq(&head->lock);
3370 }
3371#else
3372 fput(ctx->user_files[index]);
3373#endif
3374}
3375
3376static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
3377 int index)
3378{
3379#if defined(CONFIG_UNIX)
3380 struct sock *sock = ctx->ring_sock->sk;
3381 struct sk_buff_head *head = &sock->sk_receive_queue;
3382 struct sk_buff *skb;
3383
3384 /*
3385 * See if we can merge this file into an existing skb SCM_RIGHTS
3386 * file set. If there's no room, fall back to allocating a new skb
3387 * and filling it in.
3388 */
3389 spin_lock_irq(&head->lock);
3390 skb = skb_peek(head);
3391 if (skb) {
3392 struct scm_fp_list *fpl = UNIXCB(skb).fp;
3393
3394 if (fpl->count < SCM_MAX_FD) {
3395 __skb_unlink(skb, head);
3396 spin_unlock_irq(&head->lock);
3397 fpl->fp[fpl->count] = get_file(file);
3398 unix_inflight(fpl->user, fpl->fp[fpl->count]);
3399 fpl->count++;
3400 spin_lock_irq(&head->lock);
3401 __skb_queue_head(head, skb);
3402 } else {
3403 skb = NULL;
3404 }
3405 }
3406 spin_unlock_irq(&head->lock);
3407
3408 if (skb) {
3409 fput(file);
3410 return 0;
3411 }
3412
3413 return __io_sqe_files_scm(ctx, 1, index);
3414#else
3415 return 0;
3416#endif
3417}
3418
3419static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
3420 unsigned nr_args)
3421{
3422 struct io_uring_files_update up;
3423 __s32 __user *fds;
3424 int fd, i, err;
3425 __u32 done;
3426
3427 if (!ctx->user_files)
3428 return -ENXIO;
3429 if (!nr_args)
3430 return -EINVAL;
3431 if (copy_from_user(&up, arg, sizeof(up)))
3432 return -EFAULT;
3433 if (check_add_overflow(up.offset, nr_args, &done))
3434 return -EOVERFLOW;
3435 if (done > ctx->nr_user_files)
3436 return -EINVAL;
3437
3438 done = 0;
3439 fds = (__s32 __user *) up.fds;
3440 while (nr_args) {
3441 err = 0;
3442 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
3443 err = -EFAULT;
3444 break;
3445 }
3446 i = array_index_nospec(up.offset, ctx->nr_user_files);
3447 if (ctx->user_files[i]) {
3448 io_sqe_file_unregister(ctx, i);
3449 ctx->user_files[i] = NULL;
3450 }
3451 if (fd != -1) {
3452 struct file *file;
3453
3454 file = fget(fd);
3455 if (!file) {
3456 err = -EBADF;
3457 break;
3458 }
3459 /*
3460 * Don't allow io_uring instances to be registered. If
3461 * UNIX isn't enabled, then this causes a reference
3462 * cycle and this instance can never get freed. If UNIX
3463 * is enabled we'll handle it just fine, but there's
3464 * still no point in allowing a ring fd as it doesn't
3465 * support regular read/write anyway.
3466 */
3467 if (file->f_op == &io_uring_fops) {
3468 fput(file);
3469 err = -EBADF;
3470 break;
3471 }
3472 ctx->user_files[i] = file;
3473 err = io_sqe_file_register(ctx, file, i);
3474 if (err)
3475 break;
3476 }
3477 nr_args--;
3478 done++;
3479 up.offset++;
3480 }
3481
3482 return done ? done : err;
3483}
3484
Jens Axboe6c271ce2019-01-10 11:22:30 -07003485static int io_sq_offload_start(struct io_ring_ctx *ctx,
3486 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003487{
3488 int ret;
3489
Jens Axboe6c271ce2019-01-10 11:22:30 -07003490 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003491 mmgrab(current->mm);
3492 ctx->sqo_mm = current->mm;
3493
Jens Axboe6c271ce2019-01-10 11:22:30 -07003494 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06003495 ret = -EPERM;
3496 if (!capable(CAP_SYS_ADMIN))
3497 goto err;
3498
Jens Axboe917257d2019-04-13 09:28:55 -06003499 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
3500 if (!ctx->sq_thread_idle)
3501 ctx->sq_thread_idle = HZ;
3502
Jens Axboe6c271ce2019-01-10 11:22:30 -07003503 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06003504 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003505
Jens Axboe917257d2019-04-13 09:28:55 -06003506 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06003507 if (cpu >= nr_cpu_ids)
3508 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08003509 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06003510 goto err;
3511
Jens Axboe6c271ce2019-01-10 11:22:30 -07003512 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
3513 ctx, cpu,
3514 "io_uring-sq");
3515 } else {
3516 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
3517 "io_uring-sq");
3518 }
3519 if (IS_ERR(ctx->sqo_thread)) {
3520 ret = PTR_ERR(ctx->sqo_thread);
3521 ctx->sqo_thread = NULL;
3522 goto err;
3523 }
3524 wake_up_process(ctx->sqo_thread);
3525 } else if (p->flags & IORING_SETUP_SQ_AFF) {
3526 /* Can't have SQ_AFF without SQPOLL */
3527 ret = -EINVAL;
3528 goto err;
3529 }
3530
Jens Axboe2b188cc2019-01-07 10:46:33 -07003531 /* Do QD, or 2 * CPUS, whatever is smallest */
Jens Axboe54a91f32019-09-10 09:15:04 -06003532 ctx->sqo_wq[0] = alloc_workqueue("io_ring-wq",
3533 WQ_UNBOUND | WQ_FREEZABLE,
Jens Axboe2b188cc2019-01-07 10:46:33 -07003534 min(ctx->sq_entries - 1, 2 * num_online_cpus()));
Jens Axboe54a91f32019-09-10 09:15:04 -06003535 if (!ctx->sqo_wq[0]) {
3536 ret = -ENOMEM;
3537 goto err;
3538 }
3539
3540 /*
3541 * This is for buffered writes, where we want to limit the parallelism
3542 * due to file locking in file systems. As "normal" buffered writes
3543 * should parellelize on writeout quite nicely, limit us to having 2
3544 * pending. This avoids massive contention on the inode when doing
3545 * buffered async writes.
3546 */
3547 ctx->sqo_wq[1] = alloc_workqueue("io_ring-write-wq",
3548 WQ_UNBOUND | WQ_FREEZABLE, 2);
3549 if (!ctx->sqo_wq[1]) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07003550 ret = -ENOMEM;
3551 goto err;
3552 }
3553
3554 return 0;
3555err:
Jens Axboe54a91f32019-09-10 09:15:04 -06003556 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003557 mmdrop(ctx->sqo_mm);
3558 ctx->sqo_mm = NULL;
3559 return ret;
3560}
3561
3562static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
3563{
3564 atomic_long_sub(nr_pages, &user->locked_vm);
3565}
3566
3567static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
3568{
3569 unsigned long page_limit, cur_pages, new_pages;
3570
3571 /* Don't allow more pages than we can safely lock */
3572 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
3573
3574 do {
3575 cur_pages = atomic_long_read(&user->locked_vm);
3576 new_pages = cur_pages + nr_pages;
3577 if (new_pages > page_limit)
3578 return -ENOMEM;
3579 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
3580 new_pages) != cur_pages);
3581
3582 return 0;
3583}
3584
3585static void io_mem_free(void *ptr)
3586{
Mark Rutland52e04ef2019-04-30 17:30:21 +01003587 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003588
Mark Rutland52e04ef2019-04-30 17:30:21 +01003589 if (!ptr)
3590 return;
3591
3592 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003593 if (put_page_testzero(page))
3594 free_compound_page(page);
3595}
3596
3597static void *io_mem_alloc(size_t size)
3598{
3599 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
3600 __GFP_NORETRY;
3601
3602 return (void *) __get_free_pages(gfp_flags, get_order(size));
3603}
3604
Hristo Venev75b28af2019-08-26 17:23:46 +00003605static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
3606 size_t *sq_offset)
3607{
3608 struct io_rings *rings;
3609 size_t off, sq_array_size;
3610
3611 off = struct_size(rings, cqes, cq_entries);
3612 if (off == SIZE_MAX)
3613 return SIZE_MAX;
3614
3615#ifdef CONFIG_SMP
3616 off = ALIGN(off, SMP_CACHE_BYTES);
3617 if (off == 0)
3618 return SIZE_MAX;
3619#endif
3620
3621 sq_array_size = array_size(sizeof(u32), sq_entries);
3622 if (sq_array_size == SIZE_MAX)
3623 return SIZE_MAX;
3624
3625 if (check_add_overflow(off, sq_array_size, &off))
3626 return SIZE_MAX;
3627
3628 if (sq_offset)
3629 *sq_offset = off;
3630
3631 return off;
3632}
3633
Jens Axboe2b188cc2019-01-07 10:46:33 -07003634static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
3635{
Hristo Venev75b28af2019-08-26 17:23:46 +00003636 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003637
Hristo Venev75b28af2019-08-26 17:23:46 +00003638 pages = (size_t)1 << get_order(
3639 rings_size(sq_entries, cq_entries, NULL));
3640 pages += (size_t)1 << get_order(
3641 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07003642
Hristo Venev75b28af2019-08-26 17:23:46 +00003643 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003644}
3645
Jens Axboeedafcce2019-01-09 09:16:05 -07003646static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
3647{
3648 int i, j;
3649
3650 if (!ctx->user_bufs)
3651 return -ENXIO;
3652
3653 for (i = 0; i < ctx->nr_user_bufs; i++) {
3654 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
3655
3656 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbard27c4d3a2019-08-04 19:32:06 -07003657 put_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07003658
3659 if (ctx->account_mem)
3660 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003661 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07003662 imu->nr_bvecs = 0;
3663 }
3664
3665 kfree(ctx->user_bufs);
3666 ctx->user_bufs = NULL;
3667 ctx->nr_user_bufs = 0;
3668 return 0;
3669}
3670
3671static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
3672 void __user *arg, unsigned index)
3673{
3674 struct iovec __user *src;
3675
3676#ifdef CONFIG_COMPAT
3677 if (ctx->compat) {
3678 struct compat_iovec __user *ciovs;
3679 struct compat_iovec ciov;
3680
3681 ciovs = (struct compat_iovec __user *) arg;
3682 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
3683 return -EFAULT;
3684
3685 dst->iov_base = (void __user *) (unsigned long) ciov.iov_base;
3686 dst->iov_len = ciov.iov_len;
3687 return 0;
3688 }
3689#endif
3690 src = (struct iovec __user *) arg;
3691 if (copy_from_user(dst, &src[index], sizeof(*dst)))
3692 return -EFAULT;
3693 return 0;
3694}
3695
3696static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
3697 unsigned nr_args)
3698{
3699 struct vm_area_struct **vmas = NULL;
3700 struct page **pages = NULL;
3701 int i, j, got_pages = 0;
3702 int ret = -EINVAL;
3703
3704 if (ctx->user_bufs)
3705 return -EBUSY;
3706 if (!nr_args || nr_args > UIO_MAXIOV)
3707 return -EINVAL;
3708
3709 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
3710 GFP_KERNEL);
3711 if (!ctx->user_bufs)
3712 return -ENOMEM;
3713
3714 for (i = 0; i < nr_args; i++) {
3715 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
3716 unsigned long off, start, end, ubuf;
3717 int pret, nr_pages;
3718 struct iovec iov;
3719 size_t size;
3720
3721 ret = io_copy_iov(ctx, &iov, arg, i);
3722 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03003723 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07003724
3725 /*
3726 * Don't impose further limits on the size and buffer
3727 * constraints here, we'll -EINVAL later when IO is
3728 * submitted if they are wrong.
3729 */
3730 ret = -EFAULT;
3731 if (!iov.iov_base || !iov.iov_len)
3732 goto err;
3733
3734 /* arbitrary limit, but we need something */
3735 if (iov.iov_len > SZ_1G)
3736 goto err;
3737
3738 ubuf = (unsigned long) iov.iov_base;
3739 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
3740 start = ubuf >> PAGE_SHIFT;
3741 nr_pages = end - start;
3742
3743 if (ctx->account_mem) {
3744 ret = io_account_mem(ctx->user, nr_pages);
3745 if (ret)
3746 goto err;
3747 }
3748
3749 ret = 0;
3750 if (!pages || nr_pages > got_pages) {
3751 kfree(vmas);
3752 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003753 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07003754 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003755 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07003756 sizeof(struct vm_area_struct *),
3757 GFP_KERNEL);
3758 if (!pages || !vmas) {
3759 ret = -ENOMEM;
3760 if (ctx->account_mem)
3761 io_unaccount_mem(ctx->user, nr_pages);
3762 goto err;
3763 }
3764 got_pages = nr_pages;
3765 }
3766
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003767 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07003768 GFP_KERNEL);
3769 ret = -ENOMEM;
3770 if (!imu->bvec) {
3771 if (ctx->account_mem)
3772 io_unaccount_mem(ctx->user, nr_pages);
3773 goto err;
3774 }
3775
3776 ret = 0;
3777 down_read(&current->mm->mmap_sem);
Ira Weiny932f4a62019-05-13 17:17:03 -07003778 pret = get_user_pages(ubuf, nr_pages,
3779 FOLL_WRITE | FOLL_LONGTERM,
3780 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07003781 if (pret == nr_pages) {
3782 /* don't support file backed memory */
3783 for (j = 0; j < nr_pages; j++) {
3784 struct vm_area_struct *vma = vmas[j];
3785
3786 if (vma->vm_file &&
3787 !is_file_hugepages(vma->vm_file)) {
3788 ret = -EOPNOTSUPP;
3789 break;
3790 }
3791 }
3792 } else {
3793 ret = pret < 0 ? pret : -EFAULT;
3794 }
3795 up_read(&current->mm->mmap_sem);
3796 if (ret) {
3797 /*
3798 * if we did partial map, or found file backed vmas,
3799 * release any pages we did get
3800 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07003801 if (pret > 0)
3802 put_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07003803 if (ctx->account_mem)
3804 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003805 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07003806 goto err;
3807 }
3808
3809 off = ubuf & ~PAGE_MASK;
3810 size = iov.iov_len;
3811 for (j = 0; j < nr_pages; j++) {
3812 size_t vec_len;
3813
3814 vec_len = min_t(size_t, size, PAGE_SIZE - off);
3815 imu->bvec[j].bv_page = pages[j];
3816 imu->bvec[j].bv_len = vec_len;
3817 imu->bvec[j].bv_offset = off;
3818 off = 0;
3819 size -= vec_len;
3820 }
3821 /* store original address for later verification */
3822 imu->ubuf = ubuf;
3823 imu->len = iov.iov_len;
3824 imu->nr_bvecs = nr_pages;
3825
3826 ctx->nr_user_bufs++;
3827 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003828 kvfree(pages);
3829 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07003830 return 0;
3831err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003832 kvfree(pages);
3833 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07003834 io_sqe_buffer_unregister(ctx);
3835 return ret;
3836}
3837
Jens Axboe9b402842019-04-11 11:45:41 -06003838static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
3839{
3840 __s32 __user *fds = arg;
3841 int fd;
3842
3843 if (ctx->cq_ev_fd)
3844 return -EBUSY;
3845
3846 if (copy_from_user(&fd, fds, sizeof(*fds)))
3847 return -EFAULT;
3848
3849 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
3850 if (IS_ERR(ctx->cq_ev_fd)) {
3851 int ret = PTR_ERR(ctx->cq_ev_fd);
3852 ctx->cq_ev_fd = NULL;
3853 return ret;
3854 }
3855
3856 return 0;
3857}
3858
3859static int io_eventfd_unregister(struct io_ring_ctx *ctx)
3860{
3861 if (ctx->cq_ev_fd) {
3862 eventfd_ctx_put(ctx->cq_ev_fd);
3863 ctx->cq_ev_fd = NULL;
3864 return 0;
3865 }
3866
3867 return -ENXIO;
3868}
3869
Jens Axboe2b188cc2019-01-07 10:46:33 -07003870static void io_ring_ctx_free(struct io_ring_ctx *ctx)
3871{
Jens Axboe6b063142019-01-10 22:13:58 -07003872 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003873 if (ctx->sqo_mm)
3874 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07003875
3876 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07003877 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07003878 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06003879 io_eventfd_unregister(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07003880
Jens Axboe2b188cc2019-01-07 10:46:33 -07003881#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07003882 if (ctx->ring_sock) {
3883 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07003884 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07003885 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003886#endif
3887
Hristo Venev75b28af2019-08-26 17:23:46 +00003888 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003889 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003890
3891 percpu_ref_exit(&ctx->refs);
3892 if (ctx->account_mem)
3893 io_unaccount_mem(ctx->user,
3894 ring_pages(ctx->sq_entries, ctx->cq_entries));
3895 free_uid(ctx->user);
3896 kfree(ctx);
3897}
3898
3899static __poll_t io_uring_poll(struct file *file, poll_table *wait)
3900{
3901 struct io_ring_ctx *ctx = file->private_data;
3902 __poll_t mask = 0;
3903
3904 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02003905 /*
3906 * synchronizes with barrier from wq_has_sleeper call in
3907 * io_commit_cqring
3908 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07003909 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00003910 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
3911 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003912 mask |= EPOLLOUT | EPOLLWRNORM;
yangerkundaa5de52019-09-24 20:53:34 +08003913 if (READ_ONCE(ctx->rings->cq.head) != ctx->cached_cq_tail)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003914 mask |= EPOLLIN | EPOLLRDNORM;
3915
3916 return mask;
3917}
3918
3919static int io_uring_fasync(int fd, struct file *file, int on)
3920{
3921 struct io_ring_ctx *ctx = file->private_data;
3922
3923 return fasync_helper(fd, file, on, &ctx->cq_fasync);
3924}
3925
3926static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
3927{
3928 mutex_lock(&ctx->uring_lock);
3929 percpu_ref_kill(&ctx->refs);
3930 mutex_unlock(&ctx->uring_lock);
3931
Jens Axboe5262f562019-09-17 12:26:57 -06003932 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003933 io_poll_remove_all(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07003934 io_iopoll_reap_events(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003935 wait_for_completion(&ctx->ctx_done);
3936 io_ring_ctx_free(ctx);
3937}
3938
3939static int io_uring_release(struct inode *inode, struct file *file)
3940{
3941 struct io_ring_ctx *ctx = file->private_data;
3942
3943 file->private_data = NULL;
3944 io_ring_ctx_wait_and_kill(ctx);
3945 return 0;
3946}
3947
3948static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
3949{
3950 loff_t offset = (loff_t) vma->vm_pgoff << PAGE_SHIFT;
3951 unsigned long sz = vma->vm_end - vma->vm_start;
3952 struct io_ring_ctx *ctx = file->private_data;
3953 unsigned long pfn;
3954 struct page *page;
3955 void *ptr;
3956
3957 switch (offset) {
3958 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00003959 case IORING_OFF_CQ_RING:
3960 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003961 break;
3962 case IORING_OFF_SQES:
3963 ptr = ctx->sq_sqes;
3964 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003965 default:
3966 return -EINVAL;
3967 }
3968
3969 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07003970 if (sz > page_size(page))
Jens Axboe2b188cc2019-01-07 10:46:33 -07003971 return -EINVAL;
3972
3973 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
3974 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
3975}
3976
3977SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
3978 u32, min_complete, u32, flags, const sigset_t __user *, sig,
3979 size_t, sigsz)
3980{
3981 struct io_ring_ctx *ctx;
3982 long ret = -EBADF;
3983 int submitted = 0;
3984 struct fd f;
3985
Jens Axboe6c271ce2019-01-10 11:22:30 -07003986 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07003987 return -EINVAL;
3988
3989 f = fdget(fd);
3990 if (!f.file)
3991 return -EBADF;
3992
3993 ret = -EOPNOTSUPP;
3994 if (f.file->f_op != &io_uring_fops)
3995 goto out_fput;
3996
3997 ret = -ENXIO;
3998 ctx = f.file->private_data;
3999 if (!percpu_ref_tryget(&ctx->refs))
4000 goto out_fput;
4001
Jens Axboe6c271ce2019-01-10 11:22:30 -07004002 /*
4003 * For SQ polling, the thread will do all submissions and completions.
4004 * Just return the requested submit count, and wake the thread if
4005 * we were asked to.
4006 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06004007 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004008 if (ctx->flags & IORING_SETUP_SQPOLL) {
4009 if (flags & IORING_ENTER_SQ_WAKEUP)
4010 wake_up(&ctx->sqo_wait);
4011 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06004012 } else if (to_submit) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07004013 to_submit = min(to_submit, ctx->sq_entries);
4014
4015 mutex_lock(&ctx->uring_lock);
Jens Axboebc808bc2019-10-22 13:14:37 -06004016 submitted = io_ring_submit(ctx, to_submit);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004017 mutex_unlock(&ctx->uring_lock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004018 }
4019 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07004020 unsigned nr_events = 0;
4021
Jens Axboe2b188cc2019-01-07 10:46:33 -07004022 min_complete = min(min_complete, ctx->cq_entries);
4023
Jens Axboedef596e2019-01-09 08:59:42 -07004024 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07004025 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07004026 } else {
4027 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
4028 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004029 }
4030
Pavel Begunkov6805b322019-10-08 02:18:42 +03004031 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004032out_fput:
4033 fdput(f);
4034 return submitted ? submitted : ret;
4035}
4036
4037static const struct file_operations io_uring_fops = {
4038 .release = io_uring_release,
4039 .mmap = io_uring_mmap,
4040 .poll = io_uring_poll,
4041 .fasync = io_uring_fasync,
4042};
4043
4044static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
4045 struct io_uring_params *p)
4046{
Hristo Venev75b28af2019-08-26 17:23:46 +00004047 struct io_rings *rings;
4048 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004049
Hristo Venev75b28af2019-08-26 17:23:46 +00004050 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
4051 if (size == SIZE_MAX)
4052 return -EOVERFLOW;
4053
4054 rings = io_mem_alloc(size);
4055 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004056 return -ENOMEM;
4057
Hristo Venev75b28af2019-08-26 17:23:46 +00004058 ctx->rings = rings;
4059 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
4060 rings->sq_ring_mask = p->sq_entries - 1;
4061 rings->cq_ring_mask = p->cq_entries - 1;
4062 rings->sq_ring_entries = p->sq_entries;
4063 rings->cq_ring_entries = p->cq_entries;
4064 ctx->sq_mask = rings->sq_ring_mask;
4065 ctx->cq_mask = rings->cq_ring_mask;
4066 ctx->sq_entries = rings->sq_ring_entries;
4067 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004068
4069 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
4070 if (size == SIZE_MAX)
4071 return -EOVERFLOW;
4072
4073 ctx->sq_sqes = io_mem_alloc(size);
Mark Rutland52e04ef2019-04-30 17:30:21 +01004074 if (!ctx->sq_sqes)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004075 return -ENOMEM;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004076
Jens Axboe2b188cc2019-01-07 10:46:33 -07004077 return 0;
4078}
4079
4080/*
4081 * Allocate an anonymous fd, this is what constitutes the application
4082 * visible backing of an io_uring instance. The application mmaps this
4083 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
4084 * we have to tie this fd to a socket for file garbage collection purposes.
4085 */
4086static int io_uring_get_fd(struct io_ring_ctx *ctx)
4087{
4088 struct file *file;
4089 int ret;
4090
4091#if defined(CONFIG_UNIX)
4092 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
4093 &ctx->ring_sock);
4094 if (ret)
4095 return ret;
4096#endif
4097
4098 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
4099 if (ret < 0)
4100 goto err;
4101
4102 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
4103 O_RDWR | O_CLOEXEC);
4104 if (IS_ERR(file)) {
4105 put_unused_fd(ret);
4106 ret = PTR_ERR(file);
4107 goto err;
4108 }
4109
4110#if defined(CONFIG_UNIX)
4111 ctx->ring_sock->file = file;
Jens Axboe6b063142019-01-10 22:13:58 -07004112 ctx->ring_sock->sk->sk_user_data = ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004113#endif
4114 fd_install(ret, file);
4115 return ret;
4116err:
4117#if defined(CONFIG_UNIX)
4118 sock_release(ctx->ring_sock);
4119 ctx->ring_sock = NULL;
4120#endif
4121 return ret;
4122}
4123
4124static int io_uring_create(unsigned entries, struct io_uring_params *p)
4125{
4126 struct user_struct *user = NULL;
4127 struct io_ring_ctx *ctx;
4128 bool account_mem;
4129 int ret;
4130
4131 if (!entries || entries > IORING_MAX_ENTRIES)
4132 return -EINVAL;
4133
4134 /*
4135 * Use twice as many entries for the CQ ring. It's possible for the
4136 * application to drive a higher depth than the size of the SQ ring,
4137 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06004138 * some flexibility in overcommitting a bit. If the application has
4139 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
4140 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07004141 */
4142 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06004143 if (p->flags & IORING_SETUP_CQSIZE) {
4144 /*
4145 * If IORING_SETUP_CQSIZE is set, we do the same roundup
4146 * to a power-of-two, if it isn't already. We do NOT impose
4147 * any cq vs sq ring sizing.
4148 */
4149 if (p->cq_entries < p->sq_entries || p->cq_entries > IORING_MAX_CQ_ENTRIES)
4150 return -EINVAL;
4151 p->cq_entries = roundup_pow_of_two(p->cq_entries);
4152 } else {
4153 p->cq_entries = 2 * p->sq_entries;
4154 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004155
4156 user = get_uid(current_user());
4157 account_mem = !capable(CAP_IPC_LOCK);
4158
4159 if (account_mem) {
4160 ret = io_account_mem(user,
4161 ring_pages(p->sq_entries, p->cq_entries));
4162 if (ret) {
4163 free_uid(user);
4164 return ret;
4165 }
4166 }
4167
4168 ctx = io_ring_ctx_alloc(p);
4169 if (!ctx) {
4170 if (account_mem)
4171 io_unaccount_mem(user, ring_pages(p->sq_entries,
4172 p->cq_entries));
4173 free_uid(user);
4174 return -ENOMEM;
4175 }
4176 ctx->compat = in_compat_syscall();
4177 ctx->account_mem = account_mem;
4178 ctx->user = user;
4179
4180 ret = io_allocate_scq_urings(ctx, p);
4181 if (ret)
4182 goto err;
4183
Jens Axboe6c271ce2019-01-10 11:22:30 -07004184 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004185 if (ret)
4186 goto err;
4187
Jens Axboe2b188cc2019-01-07 10:46:33 -07004188 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00004189 p->sq_off.head = offsetof(struct io_rings, sq.head);
4190 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
4191 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
4192 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
4193 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
4194 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
4195 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004196
4197 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00004198 p->cq_off.head = offsetof(struct io_rings, cq.head);
4199 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
4200 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
4201 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
4202 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
4203 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06004204
Jens Axboe044c1ab2019-10-28 09:15:33 -06004205 /*
4206 * Install ring fd as the very last thing, so we don't risk someone
4207 * having closed it before we finish setup
4208 */
4209 ret = io_uring_get_fd(ctx);
4210 if (ret < 0)
4211 goto err;
4212
Jens Axboeac90f242019-09-06 10:26:21 -06004213 p->features = IORING_FEAT_SINGLE_MMAP;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004214 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004215 return ret;
4216err:
4217 io_ring_ctx_wait_and_kill(ctx);
4218 return ret;
4219}
4220
4221/*
4222 * Sets up an aio uring context, and returns the fd. Applications asks for a
4223 * ring size, we return the actual sq/cq ring sizes (among other things) in the
4224 * params structure passed in.
4225 */
4226static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
4227{
4228 struct io_uring_params p;
4229 long ret;
4230 int i;
4231
4232 if (copy_from_user(&p, params, sizeof(p)))
4233 return -EFAULT;
4234 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
4235 if (p.resv[i])
4236 return -EINVAL;
4237 }
4238
Jens Axboe6c271ce2019-01-10 11:22:30 -07004239 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe33a107f2019-10-04 12:10:03 -06004240 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE))
Jens Axboe2b188cc2019-01-07 10:46:33 -07004241 return -EINVAL;
4242
4243 ret = io_uring_create(entries, &p);
4244 if (ret < 0)
4245 return ret;
4246
4247 if (copy_to_user(params, &p, sizeof(p)))
4248 return -EFAULT;
4249
4250 return ret;
4251}
4252
4253SYSCALL_DEFINE2(io_uring_setup, u32, entries,
4254 struct io_uring_params __user *, params)
4255{
4256 return io_uring_setup(entries, params);
4257}
4258
Jens Axboeedafcce2019-01-09 09:16:05 -07004259static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
4260 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06004261 __releases(ctx->uring_lock)
4262 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07004263{
4264 int ret;
4265
Jens Axboe35fa71a2019-04-22 10:23:23 -06004266 /*
4267 * We're inside the ring mutex, if the ref is already dying, then
4268 * someone else killed the ctx or is already going through
4269 * io_uring_register().
4270 */
4271 if (percpu_ref_is_dying(&ctx->refs))
4272 return -ENXIO;
4273
Jens Axboeedafcce2019-01-09 09:16:05 -07004274 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06004275
4276 /*
4277 * Drop uring mutex before waiting for references to exit. If another
4278 * thread is currently inside io_uring_enter() it might need to grab
4279 * the uring_lock to make progress. If we hold it here across the drain
4280 * wait, then we can deadlock. It's safe to drop the mutex here, since
4281 * no new references will come in after we've killed the percpu ref.
4282 */
4283 mutex_unlock(&ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07004284 wait_for_completion(&ctx->ctx_done);
Jens Axboeb19062a2019-04-15 10:49:38 -06004285 mutex_lock(&ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07004286
4287 switch (opcode) {
4288 case IORING_REGISTER_BUFFERS:
4289 ret = io_sqe_buffer_register(ctx, arg, nr_args);
4290 break;
4291 case IORING_UNREGISTER_BUFFERS:
4292 ret = -EINVAL;
4293 if (arg || nr_args)
4294 break;
4295 ret = io_sqe_buffer_unregister(ctx);
4296 break;
Jens Axboe6b063142019-01-10 22:13:58 -07004297 case IORING_REGISTER_FILES:
4298 ret = io_sqe_files_register(ctx, arg, nr_args);
4299 break;
4300 case IORING_UNREGISTER_FILES:
4301 ret = -EINVAL;
4302 if (arg || nr_args)
4303 break;
4304 ret = io_sqe_files_unregister(ctx);
4305 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06004306 case IORING_REGISTER_FILES_UPDATE:
4307 ret = io_sqe_files_update(ctx, arg, nr_args);
4308 break;
Jens Axboe9b402842019-04-11 11:45:41 -06004309 case IORING_REGISTER_EVENTFD:
4310 ret = -EINVAL;
4311 if (nr_args != 1)
4312 break;
4313 ret = io_eventfd_register(ctx, arg);
4314 break;
4315 case IORING_UNREGISTER_EVENTFD:
4316 ret = -EINVAL;
4317 if (arg || nr_args)
4318 break;
4319 ret = io_eventfd_unregister(ctx);
4320 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07004321 default:
4322 ret = -EINVAL;
4323 break;
4324 }
4325
4326 /* bring the ctx back to life */
4327 reinit_completion(&ctx->ctx_done);
4328 percpu_ref_reinit(&ctx->refs);
4329 return ret;
4330}
4331
4332SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
4333 void __user *, arg, unsigned int, nr_args)
4334{
4335 struct io_ring_ctx *ctx;
4336 long ret = -EBADF;
4337 struct fd f;
4338
4339 f = fdget(fd);
4340 if (!f.file)
4341 return -EBADF;
4342
4343 ret = -EOPNOTSUPP;
4344 if (f.file->f_op != &io_uring_fops)
4345 goto out_fput;
4346
4347 ctx = f.file->private_data;
4348
4349 mutex_lock(&ctx->uring_lock);
4350 ret = __io_uring_register(ctx, opcode, arg, nr_args);
4351 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004352 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
4353 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07004354out_fput:
4355 fdput(f);
4356 return ret;
4357}
4358
Jens Axboe2b188cc2019-01-07 10:46:33 -07004359static int __init io_uring_init(void)
4360{
4361 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
4362 return 0;
4363};
4364__initcall(io_uring_init);