blob: 545efd89a1f9a57e17fd987823159700d53f5726 [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;
Jackie Liu8776f3f2019-09-09 20:50:39 +0800274 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700275 bool has_user;
Jackie Liuba5290c2019-10-09 09:19:59 +0800276 bool in_async;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700277 bool needs_fixed_file;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700278};
279
Jens Axboe09bb8392019-03-13 12:39:28 -0600280/*
281 * First field must be the file pointer in all the
282 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
283 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700284struct io_poll_iocb {
285 struct file *file;
286 struct wait_queue_head *head;
287 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600288 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700289 bool canceled;
290 struct wait_queue_entry wait;
291};
292
Jens Axboe5262f562019-09-17 12:26:57 -0600293struct io_timeout {
294 struct file *file;
295 struct hrtimer timer;
296};
297
Jens Axboe09bb8392019-03-13 12:39:28 -0600298/*
299 * NOTE! Each of the iocb union members has the file pointer
300 * as the first entry in their struct definition. So you can
301 * access the file pointer through any of the sub-structs,
302 * or directly as just 'ki_filp' in this struct.
303 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700304struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700305 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600306 struct file *file;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700307 struct kiocb rw;
308 struct io_poll_iocb poll;
Jens Axboe5262f562019-09-17 12:26:57 -0600309 struct io_timeout timeout;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700310 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700311
312 struct sqe_submit submit;
313
314 struct io_ring_ctx *ctx;
315 struct list_head list;
Jens Axboe9e645e112019-05-10 16:07:28 -0600316 struct list_head link_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700317 unsigned int flags;
Jens Axboec16361c2019-01-17 08:39:48 -0700318 refcount_t refs;
Stefan Bühler8449eed2019-04-27 20:34:19 +0200319#define REQ_F_NOWAIT 1 /* must not punt to workers */
Jens Axboedef596e2019-01-09 08:59:42 -0700320#define REQ_F_IOPOLL_COMPLETED 2 /* polled IO has completed */
Jens Axboe6b063142019-01-10 22:13:58 -0700321#define REQ_F_FIXED_FILE 4 /* ctx owns file */
Jens Axboe31b51512019-01-18 22:56:34 -0700322#define REQ_F_SEQ_PREV 8 /* sequential with previous */
Stefan Bühlere2033e32019-05-11 19:08:01 +0200323#define REQ_F_IO_DRAIN 16 /* drain existing IO first */
324#define REQ_F_IO_DRAINED 32 /* drain done */
Jens Axboe9e645e112019-05-10 16:07:28 -0600325#define REQ_F_LINK 64 /* linked sqes */
Zhengyuan Liuf7b76ac2019-07-16 23:26:14 +0800326#define REQ_F_LINK_DONE 128 /* linked sqes done */
327#define REQ_F_FAIL_LINK 256 /* fail rest of links */
Jackie Liu4fe2c962019-09-09 20:50:40 +0800328#define REQ_F_SHADOW_DRAIN 512 /* link-drain shadow req */
Jens Axboe5262f562019-09-17 12:26:57 -0600329#define REQ_F_TIMEOUT 1024 /* timeout request */
Jens Axboe491381ce2019-10-17 09:20:46 -0600330#define REQ_F_ISREG 2048 /* regular file */
331#define REQ_F_MUST_PUNT 4096 /* must be punted even for NONBLOCK */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700332 u64 user_data;
Jens Axboe9e645e112019-05-10 16:07:28 -0600333 u32 result;
Jens Axboede0617e2019-04-06 21:51:27 -0600334 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700335
336 struct work_struct work;
337};
338
339#define IO_PLUG_THRESHOLD 2
Jens Axboedef596e2019-01-09 08:59:42 -0700340#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700341
Jens Axboe9a56a232019-01-09 09:06:50 -0700342struct io_submit_state {
343 struct blk_plug plug;
344
345 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700346 * io_kiocb alloc cache
347 */
348 void *reqs[IO_IOPOLL_BATCH];
349 unsigned int free_reqs;
350 unsigned int cur_req;
351
352 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700353 * File reference cache
354 */
355 struct file *file;
356 unsigned int fd;
357 unsigned int has_refs;
358 unsigned int used_refs;
359 unsigned int ios_left;
360};
361
Jens Axboede0617e2019-04-06 21:51:27 -0600362static void io_sq_wq_submit_work(struct work_struct *work);
Jens Axboe5262f562019-09-17 12:26:57 -0600363static void io_cqring_fill_event(struct io_ring_ctx *ctx, u64 ki_user_data,
364 long res);
Jackie Liu4fe2c962019-09-09 20:50:40 +0800365static void __io_free_req(struct io_kiocb *req);
Jens Axboede0617e2019-04-06 21:51:27 -0600366
Jens Axboe2b188cc2019-01-07 10:46:33 -0700367static struct kmem_cache *req_cachep;
368
369static const struct file_operations io_uring_fops;
370
371struct sock *io_uring_get_socket(struct file *file)
372{
373#if defined(CONFIG_UNIX)
374 if (file->f_op == &io_uring_fops) {
375 struct io_ring_ctx *ctx = file->private_data;
376
377 return ctx->ring_sock->sk;
378 }
379#endif
380 return NULL;
381}
382EXPORT_SYMBOL(io_uring_get_socket);
383
384static void io_ring_ctx_ref_free(struct percpu_ref *ref)
385{
386 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
387
388 complete(&ctx->ctx_done);
389}
390
391static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
392{
393 struct io_ring_ctx *ctx;
Jens Axboe31b51512019-01-18 22:56:34 -0700394 int i;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700395
396 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
397 if (!ctx)
398 return NULL;
399
Roman Gushchin21482892019-05-07 10:01:48 -0700400 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
401 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700402 kfree(ctx);
403 return NULL;
404 }
405
406 ctx->flags = p->flags;
407 init_waitqueue_head(&ctx->cq_wait);
408 init_completion(&ctx->ctx_done);
Jackie Liua4c0b3d2019-07-08 13:41:12 +0800409 init_completion(&ctx->sqo_thread_started);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700410 mutex_init(&ctx->uring_lock);
411 init_waitqueue_head(&ctx->wait);
Jens Axboe31b51512019-01-18 22:56:34 -0700412 for (i = 0; i < ARRAY_SIZE(ctx->pending_async); i++) {
413 spin_lock_init(&ctx->pending_async[i].lock);
414 INIT_LIST_HEAD(&ctx->pending_async[i].list);
415 atomic_set(&ctx->pending_async[i].cnt, 0);
416 }
Jens Axboe2b188cc2019-01-07 10:46:33 -0700417 spin_lock_init(&ctx->completion_lock);
Jens Axboedef596e2019-01-09 08:59:42 -0700418 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboe221c5eb2019-01-17 09:41:58 -0700419 INIT_LIST_HEAD(&ctx->cancel_list);
Jens Axboede0617e2019-04-06 21:51:27 -0600420 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -0600421 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700422 return ctx;
423}
424
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600425static inline bool __io_sequence_defer(struct io_ring_ctx *ctx,
426 struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -0600427{
Jens Axboe498ccd92019-10-25 10:04:25 -0600428 return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped
429 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboede0617e2019-04-06 21:51:27 -0600430}
431
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600432static inline bool io_sequence_defer(struct io_ring_ctx *ctx,
433 struct io_kiocb *req)
434{
435 if ((req->flags & (REQ_F_IO_DRAIN|REQ_F_IO_DRAINED)) != REQ_F_IO_DRAIN)
436 return false;
437
438 return __io_sequence_defer(ctx, req);
439}
440
441static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -0600442{
443 struct io_kiocb *req;
444
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600445 req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list);
446 if (req && !io_sequence_defer(ctx, req)) {
Jens Axboede0617e2019-04-06 21:51:27 -0600447 list_del_init(&req->list);
448 return req;
449 }
450
451 return NULL;
452}
453
Jens Axboe5262f562019-09-17 12:26:57 -0600454static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx)
455{
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600456 struct io_kiocb *req;
457
458 req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list);
459 if (req && !__io_sequence_defer(ctx, req)) {
460 list_del_init(&req->list);
461 return req;
462 }
463
464 return NULL;
Jens Axboe5262f562019-09-17 12:26:57 -0600465}
466
Jens Axboede0617e2019-04-06 21:51:27 -0600467static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700468{
Hristo Venev75b28af2019-08-26 17:23:46 +0000469 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700470
Hristo Venev75b28af2019-08-26 17:23:46 +0000471 if (ctx->cached_cq_tail != READ_ONCE(rings->cq.tail)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700472 /* order cqe stores with ring update */
Hristo Venev75b28af2019-08-26 17:23:46 +0000473 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700474
Jens Axboe2b188cc2019-01-07 10:46:33 -0700475 if (wq_has_sleeper(&ctx->cq_wait)) {
476 wake_up_interruptible(&ctx->cq_wait);
477 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
478 }
479 }
480}
481
Jens Axboe18d9be12019-09-10 09:13:05 -0600482static inline void io_queue_async_work(struct io_ring_ctx *ctx,
483 struct io_kiocb *req)
484{
Jens Axboe6cc47d12019-09-18 11:18:23 -0600485 int rw = 0;
Jens Axboe54a91f32019-09-10 09:15:04 -0600486
Jens Axboe6cc47d12019-09-18 11:18:23 -0600487 if (req->submit.sqe) {
488 switch (req->submit.sqe->opcode) {
489 case IORING_OP_WRITEV:
490 case IORING_OP_WRITE_FIXED:
491 rw = !(req->rw.ki_flags & IOCB_DIRECT);
492 break;
493 }
Jens Axboe54a91f32019-09-10 09:15:04 -0600494 }
495
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +0200496 trace_io_uring_queue_async_work(ctx, rw, req, &req->work, req->flags);
Jens Axboe54a91f32019-09-10 09:15:04 -0600497 queue_work(ctx->sqo_wq[rw], &req->work);
Jens Axboe18d9be12019-09-10 09:13:05 -0600498}
499
Jens Axboe5262f562019-09-17 12:26:57 -0600500static void io_kill_timeout(struct io_kiocb *req)
501{
502 int ret;
503
504 ret = hrtimer_try_to_cancel(&req->timeout.timer);
505 if (ret != -1) {
506 atomic_inc(&req->ctx->cq_timeouts);
507 list_del(&req->list);
508 io_cqring_fill_event(req->ctx, req->user_data, 0);
509 __io_free_req(req);
510 }
511}
512
513static void io_kill_timeouts(struct io_ring_ctx *ctx)
514{
515 struct io_kiocb *req, *tmp;
516
517 spin_lock_irq(&ctx->completion_lock);
518 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
519 io_kill_timeout(req);
520 spin_unlock_irq(&ctx->completion_lock);
521}
522
Jens Axboede0617e2019-04-06 21:51:27 -0600523static void io_commit_cqring(struct io_ring_ctx *ctx)
524{
525 struct io_kiocb *req;
526
Jens Axboe5262f562019-09-17 12:26:57 -0600527 while ((req = io_get_timeout_req(ctx)) != NULL)
528 io_kill_timeout(req);
529
Jens Axboede0617e2019-04-06 21:51:27 -0600530 __io_commit_cqring(ctx);
531
532 while ((req = io_get_deferred_req(ctx)) != NULL) {
Jackie Liu4fe2c962019-09-09 20:50:40 +0800533 if (req->flags & REQ_F_SHADOW_DRAIN) {
534 /* Just for drain, free it. */
535 __io_free_req(req);
536 continue;
537 }
Jens Axboede0617e2019-04-06 21:51:27 -0600538 req->flags |= REQ_F_IO_DRAINED;
Jens Axboe18d9be12019-09-10 09:13:05 -0600539 io_queue_async_work(ctx, req);
Jens Axboede0617e2019-04-06 21:51:27 -0600540 }
541}
542
Jens Axboe2b188cc2019-01-07 10:46:33 -0700543static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
544{
Hristo Venev75b28af2019-08-26 17:23:46 +0000545 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700546 unsigned tail;
547
548 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +0200549 /*
550 * writes to the cq entry need to come after reading head; the
551 * control dependency is enough as we're using WRITE_ONCE to
552 * fill the cq entry
553 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000554 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700555 return NULL;
556
557 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +0000558 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -0700559}
560
561static void io_cqring_fill_event(struct io_ring_ctx *ctx, u64 ki_user_data,
Jens Axboec71ffb62019-05-13 20:58:29 -0600562 long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700563{
564 struct io_uring_cqe *cqe;
565
566 /*
567 * If we can't get a cq entry, userspace overflowed the
568 * submission (by quite a lot). Increment the overflow count in
569 * the ring.
570 */
571 cqe = io_get_cqring(ctx);
572 if (cqe) {
573 WRITE_ONCE(cqe->user_data, ki_user_data);
574 WRITE_ONCE(cqe->res, res);
Jens Axboec71ffb62019-05-13 20:58:29 -0600575 WRITE_ONCE(cqe->flags, 0);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700576 } else {
Jens Axboe498ccd92019-10-25 10:04:25 -0600577 WRITE_ONCE(ctx->rings->cq_overflow,
578 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe2b188cc2019-01-07 10:46:33 -0700579 }
580}
581
Jens Axboe8c838782019-03-12 15:48:16 -0600582static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
583{
584 if (waitqueue_active(&ctx->wait))
585 wake_up(&ctx->wait);
586 if (waitqueue_active(&ctx->sqo_wait))
587 wake_up(&ctx->sqo_wait);
Jens Axboe9b402842019-04-11 11:45:41 -0600588 if (ctx->cq_ev_fd)
589 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -0600590}
591
592static void io_cqring_add_event(struct io_ring_ctx *ctx, u64 user_data,
Jens Axboec71ffb62019-05-13 20:58:29 -0600593 long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700594{
595 unsigned long flags;
596
597 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboec71ffb62019-05-13 20:58:29 -0600598 io_cqring_fill_event(ctx, user_data, res);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700599 io_commit_cqring(ctx);
600 spin_unlock_irqrestore(&ctx->completion_lock, flags);
601
Jens Axboe8c838782019-03-12 15:48:16 -0600602 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700603}
604
Jens Axboe2579f912019-01-09 09:10:43 -0700605static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
606 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700607{
Jens Axboefd6fab22019-03-14 16:30:06 -0600608 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700609 struct io_kiocb *req;
610
611 if (!percpu_ref_tryget(&ctx->refs))
612 return NULL;
613
Jens Axboe2579f912019-01-09 09:10:43 -0700614 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -0600615 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -0700616 if (unlikely(!req))
617 goto out;
618 } else if (!state->free_reqs) {
619 size_t sz;
620 int ret;
621
622 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -0600623 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
624
625 /*
626 * Bulk alloc is all-or-nothing. If we fail to get a batch,
627 * retry single alloc to be on the safe side.
628 */
629 if (unlikely(ret <= 0)) {
630 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
631 if (!state->reqs[0])
632 goto out;
633 ret = 1;
634 }
Jens Axboe2579f912019-01-09 09:10:43 -0700635 state->free_reqs = ret - 1;
636 state->cur_req = 1;
637 req = state->reqs[0];
638 } else {
639 req = state->reqs[state->cur_req];
640 state->free_reqs--;
641 state->cur_req++;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700642 }
643
Jens Axboe60c112b2019-06-21 10:20:18 -0600644 req->file = NULL;
Jens Axboe2579f912019-01-09 09:10:43 -0700645 req->ctx = ctx;
646 req->flags = 0;
Jens Axboee65ef562019-03-12 10:16:44 -0600647 /* one is dropped after submission, the other at completion */
648 refcount_set(&req->refs, 2);
Jens Axboe9e645e112019-05-10 16:07:28 -0600649 req->result = 0;
Jens Axboe2579f912019-01-09 09:10:43 -0700650 return req;
651out:
Pavel Begunkov6805b322019-10-08 02:18:42 +0300652 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700653 return NULL;
654}
655
Jens Axboedef596e2019-01-09 08:59:42 -0700656static void io_free_req_many(struct io_ring_ctx *ctx, void **reqs, int *nr)
657{
658 if (*nr) {
659 kmem_cache_free_bulk(req_cachep, *nr, reqs);
Pavel Begunkov6805b322019-10-08 02:18:42 +0300660 percpu_ref_put_many(&ctx->refs, *nr);
Jens Axboedef596e2019-01-09 08:59:42 -0700661 *nr = 0;
662 }
663}
664
Jens Axboe9e645e112019-05-10 16:07:28 -0600665static void __io_free_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700666{
Jens Axboe09bb8392019-03-13 12:39:28 -0600667 if (req->file && !(req->flags & REQ_F_FIXED_FILE))
668 fput(req->file);
Pavel Begunkov6805b322019-10-08 02:18:42 +0300669 percpu_ref_put(&req->ctx->refs);
Jens Axboee65ef562019-03-12 10:16:44 -0600670 kmem_cache_free(req_cachep, req);
671}
672
Jens Axboeba816ad2019-09-28 11:36:45 -0600673static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -0600674{
675 struct io_kiocb *nxt;
676
677 /*
678 * The list should never be empty when we are called here. But could
679 * potentially happen if the chain is messed up, check to be on the
680 * safe side.
681 */
682 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, list);
683 if (nxt) {
684 list_del(&nxt->list);
685 if (!list_empty(&req->link_list)) {
686 INIT_LIST_HEAD(&nxt->link_list);
687 list_splice(&req->link_list, &nxt->link_list);
688 nxt->flags |= REQ_F_LINK;
689 }
690
Zhengyuan Liuf7b76ac2019-07-16 23:26:14 +0800691 nxt->flags |= REQ_F_LINK_DONE;
Jens Axboeba816ad2019-09-28 11:36:45 -0600692 /*
693 * If we're in async work, we can continue processing the chain
694 * in this context instead of having to queue up new async work.
695 */
696 if (nxtptr && current_work()) {
697 *nxtptr = nxt;
698 } else {
699 INIT_WORK(&nxt->work, io_sq_wq_submit_work);
700 io_queue_async_work(req->ctx, nxt);
701 }
Jens Axboe9e645e112019-05-10 16:07:28 -0600702 }
703}
704
705/*
706 * Called if REQ_F_LINK is set, and we fail the head request
707 */
708static void io_fail_links(struct io_kiocb *req)
709{
710 struct io_kiocb *link;
711
712 while (!list_empty(&req->link_list)) {
713 link = list_first_entry(&req->link_list, struct io_kiocb, list);
714 list_del(&link->list);
715
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +0200716 trace_io_uring_fail_link(req, link);
Jens Axboe9e645e112019-05-10 16:07:28 -0600717 io_cqring_add_event(req->ctx, link->user_data, -ECANCELED);
718 __io_free_req(link);
719 }
720}
721
Jens Axboeba816ad2019-09-28 11:36:45 -0600722static void io_free_req(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -0600723{
724 /*
725 * If LINK is set, we have dependent requests in this chain. If we
726 * didn't fail this request, queue the first one up, moving any other
727 * dependencies to the next request. In case of failure, fail the rest
728 * of the chain.
729 */
730 if (req->flags & REQ_F_LINK) {
731 if (req->flags & REQ_F_FAIL_LINK)
732 io_fail_links(req);
733 else
Jens Axboeba816ad2019-09-28 11:36:45 -0600734 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -0600735 }
736
737 __io_free_req(req);
738}
739
Jens Axboeba816ad2019-09-28 11:36:45 -0600740/*
741 * Drop reference to request, return next in chain (if there is one) if this
742 * was the last reference to this request.
743 */
744static struct io_kiocb *io_put_req_find_next(struct io_kiocb *req)
Jens Axboee65ef562019-03-12 10:16:44 -0600745{
Jens Axboeba816ad2019-09-28 11:36:45 -0600746 struct io_kiocb *nxt = NULL;
747
Jens Axboee65ef562019-03-12 10:16:44 -0600748 if (refcount_dec_and_test(&req->refs))
Jens Axboeba816ad2019-09-28 11:36:45 -0600749 io_free_req(req, &nxt);
750
751 return nxt;
752}
753
754static void io_put_req(struct io_kiocb *req, struct io_kiocb **nxtptr)
755{
756 struct io_kiocb *nxt;
757
758 nxt = io_put_req_find_next(req);
759 if (nxt) {
760 if (nxtptr) {
761 *nxtptr = nxt;
762 } else {
763 INIT_WORK(&nxt->work, io_sq_wq_submit_work);
764 io_queue_async_work(nxt->ctx, nxt);
765 }
766 }
Jens Axboe2b188cc2019-01-07 10:46:33 -0700767}
768
Hristo Venev75b28af2019-08-26 17:23:46 +0000769static unsigned io_cqring_events(struct io_rings *rings)
Jens Axboea3a0e432019-08-20 11:03:11 -0600770{
771 /* See comment at the top of this file */
772 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +0000773 return READ_ONCE(rings->cq.tail) - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -0600774}
775
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +0300776static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
777{
778 struct io_rings *rings = ctx->rings;
779
780 /* make sure SQ entry isn't read before tail */
781 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
782}
783
Jens Axboedef596e2019-01-09 08:59:42 -0700784/*
785 * Find and free completed poll iocbs
786 */
787static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
788 struct list_head *done)
789{
790 void *reqs[IO_IOPOLL_BATCH];
791 struct io_kiocb *req;
Jens Axboe09bb8392019-03-13 12:39:28 -0600792 int to_free;
Jens Axboedef596e2019-01-09 08:59:42 -0700793
Jens Axboe09bb8392019-03-13 12:39:28 -0600794 to_free = 0;
Jens Axboedef596e2019-01-09 08:59:42 -0700795 while (!list_empty(done)) {
796 req = list_first_entry(done, struct io_kiocb, list);
797 list_del(&req->list);
798
Jens Axboe9e645e112019-05-10 16:07:28 -0600799 io_cqring_fill_event(ctx, req->user_data, req->result);
Jens Axboedef596e2019-01-09 08:59:42 -0700800 (*nr_events)++;
801
Jens Axboe09bb8392019-03-13 12:39:28 -0600802 if (refcount_dec_and_test(&req->refs)) {
803 /* If we're not using fixed files, we have to pair the
804 * completion part with the file put. Use regular
805 * completions for those, only batch free for fixed
Jens Axboe9e645e112019-05-10 16:07:28 -0600806 * file and non-linked commands.
Jens Axboe09bb8392019-03-13 12:39:28 -0600807 */
Jens Axboe9e645e112019-05-10 16:07:28 -0600808 if ((req->flags & (REQ_F_FIXED_FILE|REQ_F_LINK)) ==
809 REQ_F_FIXED_FILE) {
Jens Axboe09bb8392019-03-13 12:39:28 -0600810 reqs[to_free++] = req;
811 if (to_free == ARRAY_SIZE(reqs))
812 io_free_req_many(ctx, reqs, &to_free);
Jens Axboe6b063142019-01-10 22:13:58 -0700813 } else {
Jens Axboeba816ad2019-09-28 11:36:45 -0600814 io_free_req(req, NULL);
Jens Axboe6b063142019-01-10 22:13:58 -0700815 }
Jens Axboe9a56a232019-01-09 09:06:50 -0700816 }
Jens Axboedef596e2019-01-09 08:59:42 -0700817 }
Jens Axboedef596e2019-01-09 08:59:42 -0700818
Jens Axboe09bb8392019-03-13 12:39:28 -0600819 io_commit_cqring(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -0700820 io_free_req_many(ctx, reqs, &to_free);
821}
822
823static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
824 long min)
825{
826 struct io_kiocb *req, *tmp;
827 LIST_HEAD(done);
828 bool spin;
829 int ret;
830
831 /*
832 * Only spin for completions if we don't have multiple devices hanging
833 * off our complete list, and we're under the requested amount.
834 */
835 spin = !ctx->poll_multi_file && *nr_events < min;
836
837 ret = 0;
838 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
839 struct kiocb *kiocb = &req->rw;
840
841 /*
842 * Move completed entries to our local list. If we find a
843 * request that requires polling, break out and complete
844 * the done list first, if we have entries there.
845 */
846 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
847 list_move_tail(&req->list, &done);
848 continue;
849 }
850 if (!list_empty(&done))
851 break;
852
853 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
854 if (ret < 0)
855 break;
856
857 if (ret && spin)
858 spin = false;
859 ret = 0;
860 }
861
862 if (!list_empty(&done))
863 io_iopoll_complete(ctx, nr_events, &done);
864
865 return ret;
866}
867
868/*
869 * Poll for a mininum of 'min' events. Note that if min == 0 we consider that a
870 * non-spinning poll check - we'll still enter the driver poll loop, but only
871 * as a non-spinning completion check.
872 */
873static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
874 long min)
875{
Jens Axboe08f54392019-08-21 22:19:11 -0600876 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -0700877 int ret;
878
879 ret = io_do_iopoll(ctx, nr_events, min);
880 if (ret < 0)
881 return ret;
882 if (!min || *nr_events >= min)
883 return 0;
884 }
885
886 return 1;
887}
888
889/*
890 * We can't just wait for polled events to come to us, we have to actively
891 * find and complete them.
892 */
893static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
894{
895 if (!(ctx->flags & IORING_SETUP_IOPOLL))
896 return;
897
898 mutex_lock(&ctx->uring_lock);
899 while (!list_empty(&ctx->poll_list)) {
900 unsigned int nr_events = 0;
901
902 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -0600903
904 /*
905 * Ensure we allow local-to-the-cpu processing to take place,
906 * in this case we need to ensure that we reap all events.
907 */
908 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -0700909 }
910 mutex_unlock(&ctx->uring_lock);
911}
912
Jens Axboe2b2ed972019-10-25 10:06:15 -0600913static int __io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
914 long min)
Jens Axboedef596e2019-01-09 08:59:42 -0700915{
Jens Axboe2b2ed972019-10-25 10:06:15 -0600916 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -0700917
918 do {
919 int tmin = 0;
920
Jens Axboe500f9fb2019-08-19 12:15:59 -0600921 /*
Jens Axboea3a0e432019-08-20 11:03:11 -0600922 * Don't enter poll loop if we already have events pending.
923 * If we do, we can potentially be spinning for commands that
924 * already triggered a CQE (eg in error).
925 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000926 if (io_cqring_events(ctx->rings))
Jens Axboea3a0e432019-08-20 11:03:11 -0600927 break;
928
929 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -0600930 * If a submit got punted to a workqueue, we can have the
931 * application entering polling for a command before it gets
932 * issued. That app will hold the uring_lock for the duration
933 * of the poll right here, so we need to take a breather every
934 * now and then to ensure that the issue has a chance to add
935 * the poll to the issued list. Otherwise we can spin here
936 * forever, while the workqueue is stuck trying to acquire the
937 * very same mutex.
938 */
939 if (!(++iters & 7)) {
940 mutex_unlock(&ctx->uring_lock);
941 mutex_lock(&ctx->uring_lock);
942 }
943
Jens Axboedef596e2019-01-09 08:59:42 -0700944 if (*nr_events < min)
945 tmin = min - *nr_events;
946
947 ret = io_iopoll_getevents(ctx, nr_events, tmin);
948 if (ret <= 0)
949 break;
950 ret = 0;
951 } while (min && !*nr_events && !need_resched());
952
Jens Axboe2b2ed972019-10-25 10:06:15 -0600953 return ret;
954}
955
956static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
957 long min)
958{
959 int ret;
960
961 /*
962 * We disallow the app entering submit/complete with polling, but we
963 * still need to lock the ring to prevent racing with polled issue
964 * that got punted to a workqueue.
965 */
966 mutex_lock(&ctx->uring_lock);
967 ret = __io_iopoll_check(ctx, nr_events, min);
Jens Axboe500f9fb2019-08-19 12:15:59 -0600968 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -0700969 return ret;
970}
971
Jens Axboe491381ce2019-10-17 09:20:46 -0600972static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700973{
Jens Axboe491381ce2019-10-17 09:20:46 -0600974 /*
975 * Tell lockdep we inherited freeze protection from submission
976 * thread.
977 */
978 if (req->flags & REQ_F_ISREG) {
979 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700980
Jens Axboe491381ce2019-10-17 09:20:46 -0600981 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700982 }
Jens Axboe491381ce2019-10-17 09:20:46 -0600983 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700984}
985
Jens Axboeba816ad2019-09-28 11:36:45 -0600986static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700987{
988 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
989
Jens Axboe491381ce2019-10-17 09:20:46 -0600990 if (kiocb->ki_flags & IOCB_WRITE)
991 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700992
Jens Axboe9e645e112019-05-10 16:07:28 -0600993 if ((req->flags & REQ_F_LINK) && res != req->result)
994 req->flags |= REQ_F_FAIL_LINK;
Jens Axboec71ffb62019-05-13 20:58:29 -0600995 io_cqring_add_event(req->ctx, req->user_data, res);
Jens Axboeba816ad2019-09-28 11:36:45 -0600996}
997
998static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
999{
1000 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1001
1002 io_complete_rw_common(kiocb, res);
1003 io_put_req(req, NULL);
1004}
1005
1006static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res)
1007{
1008 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1009
1010 io_complete_rw_common(kiocb, res);
1011 return io_put_req_find_next(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001012}
1013
Jens Axboedef596e2019-01-09 08:59:42 -07001014static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1015{
1016 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1017
Jens Axboe491381ce2019-10-17 09:20:46 -06001018 if (kiocb->ki_flags & IOCB_WRITE)
1019 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001020
Jens Axboe9e645e112019-05-10 16:07:28 -06001021 if ((req->flags & REQ_F_LINK) && res != req->result)
1022 req->flags |= REQ_F_FAIL_LINK;
1023 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -07001024 if (res != -EAGAIN)
1025 req->flags |= REQ_F_IOPOLL_COMPLETED;
1026}
1027
1028/*
1029 * After the iocb has been issued, it's safe to be found on the poll list.
1030 * Adding the kiocb to the list AFTER submission ensures that we don't
1031 * find it from a io_iopoll_getevents() thread before the issuer is done
1032 * accessing the kiocb cookie.
1033 */
1034static void io_iopoll_req_issued(struct io_kiocb *req)
1035{
1036 struct io_ring_ctx *ctx = req->ctx;
1037
1038 /*
1039 * Track whether we have multiple files in our lists. This will impact
1040 * how we do polling eventually, not spinning if we're on potentially
1041 * different devices.
1042 */
1043 if (list_empty(&ctx->poll_list)) {
1044 ctx->poll_multi_file = false;
1045 } else if (!ctx->poll_multi_file) {
1046 struct io_kiocb *list_req;
1047
1048 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1049 list);
1050 if (list_req->rw.ki_filp != req->rw.ki_filp)
1051 ctx->poll_multi_file = true;
1052 }
1053
1054 /*
1055 * For fast devices, IO may have already completed. If it has, add
1056 * it to the front so we find it first.
1057 */
1058 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1059 list_add(&req->list, &ctx->poll_list);
1060 else
1061 list_add_tail(&req->list, &ctx->poll_list);
1062}
1063
Jens Axboe3d6770f2019-04-13 11:50:54 -06001064static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07001065{
Jens Axboe3d6770f2019-04-13 11:50:54 -06001066 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -07001067 int diff = state->has_refs - state->used_refs;
1068
1069 if (diff)
1070 fput_many(state->file, diff);
1071 state->file = NULL;
1072 }
1073}
1074
1075/*
1076 * Get as many references to a file as we have IOs left in this submission,
1077 * assuming most submissions are for one file, or at least that each file
1078 * has more than one submission.
1079 */
1080static struct file *io_file_get(struct io_submit_state *state, int fd)
1081{
1082 if (!state)
1083 return fget(fd);
1084
1085 if (state->file) {
1086 if (state->fd == fd) {
1087 state->used_refs++;
1088 state->ios_left--;
1089 return state->file;
1090 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06001091 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07001092 }
1093 state->file = fget_many(fd, state->ios_left);
1094 if (!state->file)
1095 return NULL;
1096
1097 state->fd = fd;
1098 state->has_refs = state->ios_left;
1099 state->used_refs = 1;
1100 state->ios_left--;
1101 return state->file;
1102}
1103
Jens Axboe2b188cc2019-01-07 10:46:33 -07001104/*
1105 * If we tracked the file through the SCM inflight mechanism, we could support
1106 * any file. For now, just ensure that anything potentially problematic is done
1107 * inline.
1108 */
1109static bool io_file_supports_async(struct file *file)
1110{
1111 umode_t mode = file_inode(file)->i_mode;
1112
1113 if (S_ISBLK(mode) || S_ISCHR(mode))
1114 return true;
1115 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
1116 return true;
1117
1118 return false;
1119}
1120
Jens Axboe6c271ce2019-01-10 11:22:30 -07001121static int io_prep_rw(struct io_kiocb *req, const struct sqe_submit *s,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001122 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001123{
Jens Axboe6c271ce2019-01-10 11:22:30 -07001124 const struct io_uring_sqe *sqe = s->sqe;
Jens Axboedef596e2019-01-09 08:59:42 -07001125 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001126 struct kiocb *kiocb = &req->rw;
Jens Axboe09bb8392019-03-13 12:39:28 -06001127 unsigned ioprio;
1128 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001129
Jens Axboe09bb8392019-03-13 12:39:28 -06001130 if (!req->file)
1131 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001132
Jens Axboe491381ce2019-10-17 09:20:46 -06001133 if (S_ISREG(file_inode(req->file)->i_mode))
1134 req->flags |= REQ_F_ISREG;
1135
1136 /*
1137 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
1138 * we know to async punt it even if it was opened O_NONBLOCK
1139 */
1140 if (force_nonblock && !io_file_supports_async(req->file)) {
1141 req->flags |= REQ_F_MUST_PUNT;
1142 return -EAGAIN;
1143 }
Jens Axboe6b063142019-01-10 22:13:58 -07001144
Jens Axboe2b188cc2019-01-07 10:46:33 -07001145 kiocb->ki_pos = READ_ONCE(sqe->off);
1146 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
1147 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
1148
1149 ioprio = READ_ONCE(sqe->ioprio);
1150 if (ioprio) {
1151 ret = ioprio_check_cap(ioprio);
1152 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06001153 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001154
1155 kiocb->ki_ioprio = ioprio;
1156 } else
1157 kiocb->ki_ioprio = get_current_ioprio();
1158
1159 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
1160 if (unlikely(ret))
Jens Axboe09bb8392019-03-13 12:39:28 -06001161 return ret;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001162
1163 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06001164 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
1165 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02001166 req->flags |= REQ_F_NOWAIT;
1167
1168 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001169 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001170
Jens Axboedef596e2019-01-09 08:59:42 -07001171 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07001172 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
1173 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06001174 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001175
Jens Axboedef596e2019-01-09 08:59:42 -07001176 kiocb->ki_flags |= IOCB_HIPRI;
1177 kiocb->ki_complete = io_complete_rw_iopoll;
1178 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001179 if (kiocb->ki_flags & IOCB_HIPRI)
1180 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07001181 kiocb->ki_complete = io_complete_rw;
1182 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001183 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001184}
1185
1186static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
1187{
1188 switch (ret) {
1189 case -EIOCBQUEUED:
1190 break;
1191 case -ERESTARTSYS:
1192 case -ERESTARTNOINTR:
1193 case -ERESTARTNOHAND:
1194 case -ERESTART_RESTARTBLOCK:
1195 /*
1196 * We can't just restart the syscall, since previously
1197 * submitted sqes may already be in progress. Just fail this
1198 * IO with EINTR.
1199 */
1200 ret = -EINTR;
1201 /* fall through */
1202 default:
1203 kiocb->ki_complete(kiocb, ret, 0);
1204 }
1205}
1206
Jens Axboeba816ad2019-09-28 11:36:45 -06001207static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt,
1208 bool in_async)
1209{
1210 if (in_async && ret >= 0 && nxt && kiocb->ki_complete == io_complete_rw)
1211 *nxt = __io_complete_rw(kiocb, ret);
1212 else
1213 io_rw_done(kiocb, ret);
1214}
1215
Jens Axboeedafcce2019-01-09 09:16:05 -07001216static int io_import_fixed(struct io_ring_ctx *ctx, int rw,
1217 const struct io_uring_sqe *sqe,
1218 struct iov_iter *iter)
1219{
1220 size_t len = READ_ONCE(sqe->len);
1221 struct io_mapped_ubuf *imu;
1222 unsigned index, buf_index;
1223 size_t offset;
1224 u64 buf_addr;
1225
1226 /* attempt to use fixed buffers without having provided iovecs */
1227 if (unlikely(!ctx->user_bufs))
1228 return -EFAULT;
1229
1230 buf_index = READ_ONCE(sqe->buf_index);
1231 if (unlikely(buf_index >= ctx->nr_user_bufs))
1232 return -EFAULT;
1233
1234 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
1235 imu = &ctx->user_bufs[index];
1236 buf_addr = READ_ONCE(sqe->addr);
1237
1238 /* overflow */
1239 if (buf_addr + len < buf_addr)
1240 return -EFAULT;
1241 /* not inside the mapped region */
1242 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
1243 return -EFAULT;
1244
1245 /*
1246 * May not be a start of buffer, set size appropriately
1247 * and advance us to the beginning.
1248 */
1249 offset = buf_addr - imu->ubuf;
1250 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06001251
1252 if (offset) {
1253 /*
1254 * Don't use iov_iter_advance() here, as it's really slow for
1255 * using the latter parts of a big fixed buffer - it iterates
1256 * over each segment manually. We can cheat a bit here, because
1257 * we know that:
1258 *
1259 * 1) it's a BVEC iter, we set it up
1260 * 2) all bvecs are PAGE_SIZE in size, except potentially the
1261 * first and last bvec
1262 *
1263 * So just find our index, and adjust the iterator afterwards.
1264 * If the offset is within the first bvec (or the whole first
1265 * bvec, just use iov_iter_advance(). This makes it easier
1266 * since we can just skip the first segment, which may not
1267 * be PAGE_SIZE aligned.
1268 */
1269 const struct bio_vec *bvec = imu->bvec;
1270
1271 if (offset <= bvec->bv_len) {
1272 iov_iter_advance(iter, offset);
1273 } else {
1274 unsigned long seg_skip;
1275
1276 /* skip first vec */
1277 offset -= bvec->bv_len;
1278 seg_skip = 1 + (offset >> PAGE_SHIFT);
1279
1280 iter->bvec = bvec + seg_skip;
1281 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02001282 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001283 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001284 }
1285 }
1286
Jens Axboeedafcce2019-01-09 09:16:05 -07001287 return 0;
1288}
1289
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001290static ssize_t io_import_iovec(struct io_ring_ctx *ctx, int rw,
1291 const struct sqe_submit *s, struct iovec **iovec,
1292 struct iov_iter *iter)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001293{
1294 const struct io_uring_sqe *sqe = s->sqe;
1295 void __user *buf = u64_to_user_ptr(READ_ONCE(sqe->addr));
1296 size_t sqe_len = READ_ONCE(sqe->len);
Jens Axboeedafcce2019-01-09 09:16:05 -07001297 u8 opcode;
1298
1299 /*
1300 * We're reading ->opcode for the second time, but the first read
1301 * doesn't care whether it's _FIXED or not, so it doesn't matter
1302 * whether ->opcode changes concurrently. The first read does care
1303 * about whether it is a READ or a WRITE, so we don't trust this read
1304 * for that purpose and instead let the caller pass in the read/write
1305 * flag.
1306 */
1307 opcode = READ_ONCE(sqe->opcode);
1308 if (opcode == IORING_OP_READ_FIXED ||
1309 opcode == IORING_OP_WRITE_FIXED) {
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001310 ssize_t ret = io_import_fixed(ctx, rw, sqe, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07001311 *iovec = NULL;
1312 return ret;
1313 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001314
1315 if (!s->has_user)
1316 return -EFAULT;
1317
1318#ifdef CONFIG_COMPAT
1319 if (ctx->compat)
1320 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
1321 iovec, iter);
1322#endif
1323
1324 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
1325}
1326
Jens Axboe6d5d5ac2019-09-11 10:16:13 -06001327static inline bool io_should_merge(struct async_list *al, struct kiocb *kiocb)
1328{
1329 if (al->file == kiocb->ki_filp) {
1330 off_t start, end;
1331
1332 /*
1333 * Allow merging if we're anywhere in the range of the same
1334 * page. Generally this happens for sub-page reads or writes,
1335 * and it's beneficial to allow the first worker to bring the
1336 * page in and the piggy backed work can then work on the
1337 * cached page.
1338 */
1339 start = al->io_start & PAGE_MASK;
1340 end = (al->io_start + al->io_len + PAGE_SIZE - 1) & PAGE_MASK;
1341 if (kiocb->ki_pos >= start && kiocb->ki_pos <= end)
1342 return true;
1343 }
1344
1345 al->file = NULL;
1346 return false;
1347}
1348
Jens Axboe31b51512019-01-18 22:56:34 -07001349/*
1350 * Make a note of the last file/offset/direction we punted to async
1351 * context. We'll use this information to see if we can piggy back a
1352 * sequential request onto the previous one, if it's still hasn't been
1353 * completed by the async worker.
1354 */
1355static void io_async_list_note(int rw, struct io_kiocb *req, size_t len)
1356{
1357 struct async_list *async_list = &req->ctx->pending_async[rw];
1358 struct kiocb *kiocb = &req->rw;
1359 struct file *filp = kiocb->ki_filp;
Jens Axboe31b51512019-01-18 22:56:34 -07001360
Jens Axboe6d5d5ac2019-09-11 10:16:13 -06001361 if (io_should_merge(async_list, kiocb)) {
Zhengyuan Liu9310a7ba2019-07-22 10:23:27 +08001362 unsigned long max_bytes;
Jens Axboe31b51512019-01-18 22:56:34 -07001363
1364 /* Use 8x RA size as a decent limiter for both reads/writes */
Zhengyuan Liu9310a7ba2019-07-22 10:23:27 +08001365 max_bytes = filp->f_ra.ra_pages << (PAGE_SHIFT + 3);
1366 if (!max_bytes)
1367 max_bytes = VM_READAHEAD_PAGES << (PAGE_SHIFT + 3);
Jens Axboe31b51512019-01-18 22:56:34 -07001368
Zhengyuan Liu9310a7ba2019-07-22 10:23:27 +08001369 /* If max len are exceeded, reset the state */
1370 if (async_list->io_len + len <= max_bytes) {
Jens Axboe31b51512019-01-18 22:56:34 -07001371 req->flags |= REQ_F_SEQ_PREV;
Zhengyuan Liu9310a7ba2019-07-22 10:23:27 +08001372 async_list->io_len += len;
Jens Axboe31b51512019-01-18 22:56:34 -07001373 } else {
Jens Axboe6d5d5ac2019-09-11 10:16:13 -06001374 async_list->file = NULL;
Jens Axboe31b51512019-01-18 22:56:34 -07001375 }
1376 }
1377
1378 /* New file? Reset state. */
1379 if (async_list->file != filp) {
Jens Axboe6d5d5ac2019-09-11 10:16:13 -06001380 async_list->io_start = kiocb->ki_pos;
1381 async_list->io_len = len;
Jens Axboe31b51512019-01-18 22:56:34 -07001382 async_list->file = filp;
1383 }
Jens Axboe31b51512019-01-18 22:56:34 -07001384}
1385
Jens Axboe32960612019-09-23 11:05:34 -06001386/*
1387 * For files that don't have ->read_iter() and ->write_iter(), handle them
1388 * by looping over ->read() or ->write() manually.
1389 */
1390static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
1391 struct iov_iter *iter)
1392{
1393 ssize_t ret = 0;
1394
1395 /*
1396 * Don't support polled IO through this interface, and we can't
1397 * support non-blocking either. For the latter, this just causes
1398 * the kiocb to be handled from an async context.
1399 */
1400 if (kiocb->ki_flags & IOCB_HIPRI)
1401 return -EOPNOTSUPP;
1402 if (kiocb->ki_flags & IOCB_NOWAIT)
1403 return -EAGAIN;
1404
1405 while (iov_iter_count(iter)) {
1406 struct iovec iovec = iov_iter_iovec(iter);
1407 ssize_t nr;
1408
1409 if (rw == READ) {
1410 nr = file->f_op->read(file, iovec.iov_base,
1411 iovec.iov_len, &kiocb->ki_pos);
1412 } else {
1413 nr = file->f_op->write(file, iovec.iov_base,
1414 iovec.iov_len, &kiocb->ki_pos);
1415 }
1416
1417 if (nr < 0) {
1418 if (!ret)
1419 ret = nr;
1420 break;
1421 }
1422 ret += nr;
1423 if (nr != iovec.iov_len)
1424 break;
1425 iov_iter_advance(iter, nr);
1426 }
1427
1428 return ret;
1429}
1430
Jens Axboee0c5c572019-03-12 10:18:47 -06001431static int io_read(struct io_kiocb *req, const struct sqe_submit *s,
Jens Axboeba816ad2019-09-28 11:36:45 -06001432 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001433{
1434 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1435 struct kiocb *kiocb = &req->rw;
1436 struct iov_iter iter;
1437 struct file *file;
Jens Axboe31b51512019-01-18 22:56:34 -07001438 size_t iov_count;
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001439 ssize_t read_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001440
Jens Axboe8358e3a2019-04-23 08:17:58 -06001441 ret = io_prep_rw(req, s, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001442 if (ret)
1443 return ret;
1444 file = kiocb->ki_filp;
1445
Jens Axboe2b188cc2019-01-07 10:46:33 -07001446 if (unlikely(!(file->f_mode & FMODE_READ)))
Jens Axboe09bb8392019-03-13 12:39:28 -06001447 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001448
1449 ret = io_import_iovec(req->ctx, READ, s, &iovec, &iter);
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001450 if (ret < 0)
Jens Axboe09bb8392019-03-13 12:39:28 -06001451 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001452
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001453 read_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06001454 if (req->flags & REQ_F_LINK)
1455 req->result = read_size;
1456
Jens Axboe31b51512019-01-18 22:56:34 -07001457 iov_count = iov_iter_count(&iter);
1458 ret = rw_verify_area(READ, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001459 if (!ret) {
1460 ssize_t ret2;
1461
Jens Axboe32960612019-09-23 11:05:34 -06001462 if (file->f_op->read_iter)
1463 ret2 = call_read_iter(file, kiocb, &iter);
1464 else
1465 ret2 = loop_rw_iter(READ, file, kiocb, &iter);
1466
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001467 /*
1468 * In case of a short read, punt to async. This can happen
1469 * if we have data partially cached. Alternatively we can
1470 * return the short read, in which case the application will
1471 * need to issue another SQE and wait for it. That SQE will
1472 * need async punt anyway, so it's more efficient to do it
1473 * here.
1474 */
Jens Axboe491381ce2019-10-17 09:20:46 -06001475 if (force_nonblock && !(req->flags & REQ_F_NOWAIT) &&
1476 (req->flags & REQ_F_ISREG) &&
1477 ret2 > 0 && ret2 < read_size)
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001478 ret2 = -EAGAIN;
1479 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboe31b51512019-01-18 22:56:34 -07001480 if (!force_nonblock || ret2 != -EAGAIN) {
Jackie Liuba5290c2019-10-09 09:19:59 +08001481 kiocb_done(kiocb, ret2, nxt, s->in_async);
Jens Axboe31b51512019-01-18 22:56:34 -07001482 } else {
Jackie Liuba5290c2019-10-09 09:19:59 +08001483 if (!s->in_async)
Jens Axboe31b51512019-01-18 22:56:34 -07001484 io_async_list_note(READ, req, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001485 ret = -EAGAIN;
Jens Axboe31b51512019-01-18 22:56:34 -07001486 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001487 }
1488 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001489 return ret;
1490}
1491
Jens Axboee0c5c572019-03-12 10:18:47 -06001492static int io_write(struct io_kiocb *req, const struct sqe_submit *s,
Jens Axboeba816ad2019-09-28 11:36:45 -06001493 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001494{
1495 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1496 struct kiocb *kiocb = &req->rw;
1497 struct iov_iter iter;
1498 struct file *file;
Jens Axboe31b51512019-01-18 22:56:34 -07001499 size_t iov_count;
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001500 ssize_t ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001501
Jens Axboe8358e3a2019-04-23 08:17:58 -06001502 ret = io_prep_rw(req, s, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001503 if (ret)
1504 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001505
Jens Axboe2b188cc2019-01-07 10:46:33 -07001506 file = kiocb->ki_filp;
1507 if (unlikely(!(file->f_mode & FMODE_WRITE)))
Jens Axboe09bb8392019-03-13 12:39:28 -06001508 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001509
1510 ret = io_import_iovec(req->ctx, WRITE, s, &iovec, &iter);
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001511 if (ret < 0)
Jens Axboe09bb8392019-03-13 12:39:28 -06001512 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001513
Jens Axboe9e645e112019-05-10 16:07:28 -06001514 if (req->flags & REQ_F_LINK)
1515 req->result = ret;
1516
Jens Axboe31b51512019-01-18 22:56:34 -07001517 iov_count = iov_iter_count(&iter);
1518
1519 ret = -EAGAIN;
1520 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT)) {
Jackie Liuba5290c2019-10-09 09:19:59 +08001521 if (!s->in_async)
Jens Axboe31b51512019-01-18 22:56:34 -07001522 io_async_list_note(WRITE, req, iov_count);
1523 goto out_free;
1524 }
1525
1526 ret = rw_verify_area(WRITE, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001527 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01001528 ssize_t ret2;
1529
Jens Axboe2b188cc2019-01-07 10:46:33 -07001530 /*
1531 * Open-code file_start_write here to grab freeze protection,
1532 * which will be released by another thread in
1533 * io_complete_rw(). Fool lockdep by telling it the lock got
1534 * released so that it doesn't complain about the held lock when
1535 * we return to userspace.
1536 */
Jens Axboe491381ce2019-10-17 09:20:46 -06001537 if (req->flags & REQ_F_ISREG) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001538 __sb_start_write(file_inode(file)->i_sb,
1539 SB_FREEZE_WRITE, true);
1540 __sb_writers_release(file_inode(file)->i_sb,
1541 SB_FREEZE_WRITE);
1542 }
1543 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01001544
Jens Axboe32960612019-09-23 11:05:34 -06001545 if (file->f_op->write_iter)
1546 ret2 = call_write_iter(file, kiocb, &iter);
1547 else
1548 ret2 = loop_rw_iter(WRITE, file, kiocb, &iter);
Roman Penyaev9bf79332019-03-25 20:09:24 +01001549 if (!force_nonblock || ret2 != -EAGAIN) {
Jackie Liuba5290c2019-10-09 09:19:59 +08001550 kiocb_done(kiocb, ret2, nxt, s->in_async);
Roman Penyaev9bf79332019-03-25 20:09:24 +01001551 } else {
Jackie Liuba5290c2019-10-09 09:19:59 +08001552 if (!s->in_async)
Roman Penyaev9bf79332019-03-25 20:09:24 +01001553 io_async_list_note(WRITE, req, iov_count);
1554 ret = -EAGAIN;
1555 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001556 }
Jens Axboe31b51512019-01-18 22:56:34 -07001557out_free:
Jens Axboe2b188cc2019-01-07 10:46:33 -07001558 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001559 return ret;
1560}
1561
1562/*
1563 * IORING_OP_NOP just posts a completion event, nothing else.
1564 */
1565static int io_nop(struct io_kiocb *req, u64 user_data)
1566{
1567 struct io_ring_ctx *ctx = req->ctx;
1568 long err = 0;
1569
Jens Axboedef596e2019-01-09 08:59:42 -07001570 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1571 return -EINVAL;
1572
Jens Axboec71ffb62019-05-13 20:58:29 -06001573 io_cqring_add_event(ctx, user_data, err);
Jens Axboeba816ad2019-09-28 11:36:45 -06001574 io_put_req(req, NULL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001575 return 0;
1576}
1577
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001578static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1579{
Jens Axboe6b063142019-01-10 22:13:58 -07001580 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001581
Jens Axboe09bb8392019-03-13 12:39:28 -06001582 if (!req->file)
1583 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001584
Jens Axboe6b063142019-01-10 22:13:58 -07001585 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07001586 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07001587 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001588 return -EINVAL;
1589
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001590 return 0;
1591}
1592
1593static int io_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001594 struct io_kiocb **nxt, bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001595{
1596 loff_t sqe_off = READ_ONCE(sqe->off);
1597 loff_t sqe_len = READ_ONCE(sqe->len);
1598 loff_t end = sqe_off + sqe_len;
1599 unsigned fsync_flags;
1600 int ret;
1601
1602 fsync_flags = READ_ONCE(sqe->fsync_flags);
1603 if (unlikely(fsync_flags & ~IORING_FSYNC_DATASYNC))
1604 return -EINVAL;
1605
1606 ret = io_prep_fsync(req, sqe);
1607 if (ret)
1608 return ret;
1609
1610 /* fsync always requires a blocking context */
1611 if (force_nonblock)
1612 return -EAGAIN;
1613
1614 ret = vfs_fsync_range(req->rw.ki_filp, sqe_off,
1615 end > 0 ? end : LLONG_MAX,
1616 fsync_flags & IORING_FSYNC_DATASYNC);
1617
Jens Axboe9e645e112019-05-10 16:07:28 -06001618 if (ret < 0 && (req->flags & REQ_F_LINK))
1619 req->flags |= REQ_F_FAIL_LINK;
Jens Axboec71ffb62019-05-13 20:58:29 -06001620 io_cqring_add_event(req->ctx, sqe->user_data, ret);
Jens Axboeba816ad2019-09-28 11:36:45 -06001621 io_put_req(req, nxt);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001622 return 0;
1623}
1624
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001625static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1626{
1627 struct io_ring_ctx *ctx = req->ctx;
1628 int ret = 0;
1629
1630 if (!req->file)
1631 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001632
1633 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1634 return -EINVAL;
1635 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
1636 return -EINVAL;
1637
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001638 return ret;
1639}
1640
1641static int io_sync_file_range(struct io_kiocb *req,
1642 const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001643 struct io_kiocb **nxt,
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001644 bool force_nonblock)
1645{
1646 loff_t sqe_off;
1647 loff_t sqe_len;
1648 unsigned flags;
1649 int ret;
1650
1651 ret = io_prep_sfr(req, sqe);
1652 if (ret)
1653 return ret;
1654
1655 /* sync_file_range always requires a blocking context */
1656 if (force_nonblock)
1657 return -EAGAIN;
1658
1659 sqe_off = READ_ONCE(sqe->off);
1660 sqe_len = READ_ONCE(sqe->len);
1661 flags = READ_ONCE(sqe->sync_range_flags);
1662
1663 ret = sync_file_range(req->rw.ki_filp, sqe_off, sqe_len, flags);
1664
Jens Axboe9e645e112019-05-10 16:07:28 -06001665 if (ret < 0 && (req->flags & REQ_F_LINK))
1666 req->flags |= REQ_F_FAIL_LINK;
Jens Axboec71ffb62019-05-13 20:58:29 -06001667 io_cqring_add_event(req->ctx, sqe->user_data, ret);
Jens Axboeba816ad2019-09-28 11:36:45 -06001668 io_put_req(req, nxt);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001669 return 0;
1670}
1671
Jens Axboe0fa03c62019-04-19 13:34:07 -06001672#if defined(CONFIG_NET)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001673static int io_send_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001674 struct io_kiocb **nxt, bool force_nonblock,
Jens Axboeaa1fa282019-04-19 13:38:09 -06001675 long (*fn)(struct socket *, struct user_msghdr __user *,
1676 unsigned int))
1677{
Jens Axboe0fa03c62019-04-19 13:34:07 -06001678 struct socket *sock;
1679 int ret;
1680
1681 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1682 return -EINVAL;
1683
1684 sock = sock_from_file(req->file, &ret);
1685 if (sock) {
1686 struct user_msghdr __user *msg;
1687 unsigned flags;
1688
1689 flags = READ_ONCE(sqe->msg_flags);
1690 if (flags & MSG_DONTWAIT)
1691 req->flags |= REQ_F_NOWAIT;
1692 else if (force_nonblock)
1693 flags |= MSG_DONTWAIT;
1694
1695 msg = (struct user_msghdr __user *) (unsigned long)
1696 READ_ONCE(sqe->addr);
1697
Jens Axboeaa1fa282019-04-19 13:38:09 -06001698 ret = fn(sock, msg, flags);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001699 if (force_nonblock && ret == -EAGAIN)
1700 return ret;
1701 }
1702
1703 io_cqring_add_event(req->ctx, sqe->user_data, ret);
Jens Axboeba816ad2019-09-28 11:36:45 -06001704 io_put_req(req, nxt);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001705 return 0;
Jens Axboeaa1fa282019-04-19 13:38:09 -06001706}
1707#endif
1708
1709static int io_sendmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001710 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001711{
1712#if defined(CONFIG_NET)
Jens Axboeba816ad2019-09-28 11:36:45 -06001713 return io_send_recvmsg(req, sqe, nxt, force_nonblock,
1714 __sys_sendmsg_sock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06001715#else
1716 return -EOPNOTSUPP;
1717#endif
1718}
1719
1720static int io_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001721 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001722{
1723#if defined(CONFIG_NET)
Jens Axboeba816ad2019-09-28 11:36:45 -06001724 return io_send_recvmsg(req, sqe, nxt, force_nonblock,
1725 __sys_recvmsg_sock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001726#else
1727 return -EOPNOTSUPP;
1728#endif
1729}
1730
Jens Axboe221c5eb2019-01-17 09:41:58 -07001731static void io_poll_remove_one(struct io_kiocb *req)
1732{
1733 struct io_poll_iocb *poll = &req->poll;
1734
1735 spin_lock(&poll->head->lock);
1736 WRITE_ONCE(poll->canceled, true);
1737 if (!list_empty(&poll->wait.entry)) {
1738 list_del_init(&poll->wait.entry);
Jens Axboe18d9be12019-09-10 09:13:05 -06001739 io_queue_async_work(req->ctx, req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001740 }
1741 spin_unlock(&poll->head->lock);
1742
1743 list_del_init(&req->list);
1744}
1745
1746static void io_poll_remove_all(struct io_ring_ctx *ctx)
1747{
1748 struct io_kiocb *req;
1749
1750 spin_lock_irq(&ctx->completion_lock);
1751 while (!list_empty(&ctx->cancel_list)) {
1752 req = list_first_entry(&ctx->cancel_list, struct io_kiocb,list);
1753 io_poll_remove_one(req);
1754 }
1755 spin_unlock_irq(&ctx->completion_lock);
1756}
1757
1758/*
1759 * Find a running poll command that matches one specified in sqe->addr,
1760 * and remove it if found.
1761 */
1762static int io_poll_remove(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1763{
1764 struct io_ring_ctx *ctx = req->ctx;
1765 struct io_kiocb *poll_req, *next;
1766 int ret = -ENOENT;
1767
1768 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1769 return -EINVAL;
1770 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
1771 sqe->poll_events)
1772 return -EINVAL;
1773
1774 spin_lock_irq(&ctx->completion_lock);
1775 list_for_each_entry_safe(poll_req, next, &ctx->cancel_list, list) {
1776 if (READ_ONCE(sqe->addr) == poll_req->user_data) {
1777 io_poll_remove_one(poll_req);
1778 ret = 0;
1779 break;
1780 }
1781 }
1782 spin_unlock_irq(&ctx->completion_lock);
1783
Jens Axboec71ffb62019-05-13 20:58:29 -06001784 io_cqring_add_event(req->ctx, sqe->user_data, ret);
Jens Axboeba816ad2019-09-28 11:36:45 -06001785 io_put_req(req, NULL);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001786 return 0;
1787}
1788
Jens Axboe8c838782019-03-12 15:48:16 -06001789static void io_poll_complete(struct io_ring_ctx *ctx, struct io_kiocb *req,
1790 __poll_t mask)
Jens Axboe221c5eb2019-01-17 09:41:58 -07001791{
Jens Axboe8c838782019-03-12 15:48:16 -06001792 req->poll.done = true;
Jens Axboec71ffb62019-05-13 20:58:29 -06001793 io_cqring_fill_event(ctx, req->user_data, mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06001794 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001795}
1796
1797static void io_poll_complete_work(struct work_struct *work)
1798{
1799 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
1800 struct io_poll_iocb *poll = &req->poll;
1801 struct poll_table_struct pt = { ._key = poll->events };
1802 struct io_ring_ctx *ctx = req->ctx;
1803 __poll_t mask = 0;
1804
1805 if (!READ_ONCE(poll->canceled))
1806 mask = vfs_poll(poll->file, &pt) & poll->events;
1807
1808 /*
1809 * Note that ->ki_cancel callers also delete iocb from active_reqs after
1810 * calling ->ki_cancel. We need the ctx_lock roundtrip here to
1811 * synchronize with them. In the cancellation case the list_del_init
1812 * itself is not actually needed, but harmless so we keep it in to
1813 * avoid further branches in the fast path.
1814 */
1815 spin_lock_irq(&ctx->completion_lock);
1816 if (!mask && !READ_ONCE(poll->canceled)) {
1817 add_wait_queue(poll->head, &poll->wait);
1818 spin_unlock_irq(&ctx->completion_lock);
1819 return;
1820 }
1821 list_del_init(&req->list);
Jens Axboe8c838782019-03-12 15:48:16 -06001822 io_poll_complete(ctx, req, mask);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001823 spin_unlock_irq(&ctx->completion_lock);
1824
Jens Axboe8c838782019-03-12 15:48:16 -06001825 io_cqring_ev_posted(ctx);
Jens Axboeba816ad2019-09-28 11:36:45 -06001826 io_put_req(req, NULL);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001827}
1828
1829static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
1830 void *key)
1831{
1832 struct io_poll_iocb *poll = container_of(wait, struct io_poll_iocb,
1833 wait);
1834 struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
1835 struct io_ring_ctx *ctx = req->ctx;
1836 __poll_t mask = key_to_poll(key);
Jens Axboe8c838782019-03-12 15:48:16 -06001837 unsigned long flags;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001838
1839 /* for instances that support it check for an event match first: */
Jens Axboe8c838782019-03-12 15:48:16 -06001840 if (mask && !(mask & poll->events))
1841 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001842
1843 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06001844
1845 if (mask && spin_trylock_irqsave(&ctx->completion_lock, flags)) {
1846 list_del(&req->list);
1847 io_poll_complete(ctx, req, mask);
1848 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1849
1850 io_cqring_ev_posted(ctx);
Jens Axboeba816ad2019-09-28 11:36:45 -06001851 io_put_req(req, NULL);
Jens Axboe8c838782019-03-12 15:48:16 -06001852 } else {
Jens Axboe18d9be12019-09-10 09:13:05 -06001853 io_queue_async_work(ctx, req);
Jens Axboe8c838782019-03-12 15:48:16 -06001854 }
1855
Jens Axboe221c5eb2019-01-17 09:41:58 -07001856 return 1;
1857}
1858
1859struct io_poll_table {
1860 struct poll_table_struct pt;
1861 struct io_kiocb *req;
1862 int error;
1863};
1864
1865static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
1866 struct poll_table_struct *p)
1867{
1868 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
1869
1870 if (unlikely(pt->req->poll.head)) {
1871 pt->error = -EINVAL;
1872 return;
1873 }
1874
1875 pt->error = 0;
1876 pt->req->poll.head = head;
1877 add_wait_queue(head, &pt->req->poll.wait);
1878}
1879
1880static int io_poll_add(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1881{
1882 struct io_poll_iocb *poll = &req->poll;
1883 struct io_ring_ctx *ctx = req->ctx;
1884 struct io_poll_table ipt;
Jens Axboe8c838782019-03-12 15:48:16 -06001885 bool cancel = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001886 __poll_t mask;
1887 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001888
1889 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1890 return -EINVAL;
1891 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
1892 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06001893 if (!poll->file)
1894 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001895
Jens Axboe6cc47d12019-09-18 11:18:23 -06001896 req->submit.sqe = NULL;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001897 INIT_WORK(&req->work, io_poll_complete_work);
1898 events = READ_ONCE(sqe->poll_events);
1899 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
1900
Jens Axboe221c5eb2019-01-17 09:41:58 -07001901 poll->head = NULL;
Jens Axboe8c838782019-03-12 15:48:16 -06001902 poll->done = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001903 poll->canceled = false;
1904
1905 ipt.pt._qproc = io_poll_queue_proc;
1906 ipt.pt._key = poll->events;
1907 ipt.req = req;
1908 ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
1909
1910 /* initialized the list so that we can do list_empty checks */
1911 INIT_LIST_HEAD(&poll->wait.entry);
1912 init_waitqueue_func_entry(&poll->wait, io_poll_wake);
1913
Jens Axboe36703242019-07-25 10:20:18 -06001914 INIT_LIST_HEAD(&req->list);
1915
Jens Axboe221c5eb2019-01-17 09:41:58 -07001916 mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001917
1918 spin_lock_irq(&ctx->completion_lock);
Jens Axboe8c838782019-03-12 15:48:16 -06001919 if (likely(poll->head)) {
1920 spin_lock(&poll->head->lock);
1921 if (unlikely(list_empty(&poll->wait.entry))) {
1922 if (ipt.error)
1923 cancel = true;
1924 ipt.error = 0;
1925 mask = 0;
1926 }
1927 if (mask || ipt.error)
1928 list_del_init(&poll->wait.entry);
1929 else if (cancel)
1930 WRITE_ONCE(poll->canceled, true);
1931 else if (!poll->done) /* actually waiting for an event */
1932 list_add_tail(&req->list, &ctx->cancel_list);
1933 spin_unlock(&poll->head->lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001934 }
Jens Axboe8c838782019-03-12 15:48:16 -06001935 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06001936 ipt.error = 0;
1937 io_poll_complete(ctx, req, mask);
1938 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07001939 spin_unlock_irq(&ctx->completion_lock);
1940
Jens Axboe8c838782019-03-12 15:48:16 -06001941 if (mask) {
1942 io_cqring_ev_posted(ctx);
Jens Axboeba816ad2019-09-28 11:36:45 -06001943 io_put_req(req, NULL);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001944 }
Jens Axboe8c838782019-03-12 15:48:16 -06001945 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001946}
1947
Jens Axboe5262f562019-09-17 12:26:57 -06001948static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
1949{
1950 struct io_ring_ctx *ctx;
Jens Axboe11365042019-10-16 09:08:32 -06001951 struct io_kiocb *req;
Jens Axboe5262f562019-09-17 12:26:57 -06001952 unsigned long flags;
Jens Axboe11365042019-10-16 09:08:32 -06001953 bool comp;
Jens Axboe5262f562019-09-17 12:26:57 -06001954
1955 req = container_of(timer, struct io_kiocb, timeout.timer);
1956 ctx = req->ctx;
1957 atomic_inc(&ctx->cq_timeouts);
1958
1959 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08001960 /*
Jens Axboe11365042019-10-16 09:08:32 -06001961 * We could be racing with timeout deletion. If the list is empty,
1962 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08001963 */
Jens Axboe11365042019-10-16 09:08:32 -06001964 comp = !list_empty(&req->list);
1965 if (comp) {
1966 struct io_kiocb *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06001967
Jens Axboe11365042019-10-16 09:08:32 -06001968 /*
1969 * Adjust the reqs sequence before the current one because it
1970 * will consume a slot in the cq_ring and the the cq_tail
1971 * pointer will be increased, otherwise other timeout reqs may
1972 * return in advance without waiting for enough wait_nr.
1973 */
1974 prev = req;
1975 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
1976 prev->sequence++;
1977
1978 list_del_init(&req->list);
1979 io_cqring_fill_event(ctx, req->user_data, -ETIME);
1980 io_commit_cqring(ctx);
1981 }
Jens Axboe5262f562019-09-17 12:26:57 -06001982 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1983
Jens Axboe11365042019-10-16 09:08:32 -06001984 if (comp) {
1985 io_cqring_ev_posted(ctx);
1986 io_put_req(req, NULL);
1987 }
1988 return HRTIMER_NORESTART;
1989}
1990
1991/*
1992 * Remove or update an existing timeout command
1993 */
1994static int io_timeout_remove(struct io_kiocb *req,
1995 const struct io_uring_sqe *sqe)
1996{
1997 struct io_ring_ctx *ctx = req->ctx;
1998 struct io_kiocb *treq;
1999 int ret = -ENOENT;
2000 __u64 user_data;
2001 unsigned flags;
2002
2003 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2004 return -EINVAL;
2005 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
2006 return -EINVAL;
2007 flags = READ_ONCE(sqe->timeout_flags);
2008 if (flags)
2009 return -EINVAL;
2010
2011 user_data = READ_ONCE(sqe->addr);
2012 spin_lock_irq(&ctx->completion_lock);
2013 list_for_each_entry(treq, &ctx->timeout_list, list) {
2014 if (user_data == treq->user_data) {
2015 list_del_init(&treq->list);
2016 ret = 0;
2017 break;
2018 }
2019 }
2020
2021 /* didn't find timeout */
2022 if (ret) {
2023fill_ev:
2024 io_cqring_fill_event(ctx, req->user_data, ret);
2025 io_commit_cqring(ctx);
2026 spin_unlock_irq(&ctx->completion_lock);
2027 io_cqring_ev_posted(ctx);
2028 io_put_req(req, NULL);
2029 return 0;
2030 }
2031
2032 ret = hrtimer_try_to_cancel(&treq->timeout.timer);
2033 if (ret == -1) {
2034 ret = -EBUSY;
2035 goto fill_ev;
2036 }
2037
2038 io_cqring_fill_event(ctx, req->user_data, 0);
2039 io_cqring_fill_event(ctx, treq->user_data, -ECANCELED);
2040 io_commit_cqring(ctx);
2041 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06002042 io_cqring_ev_posted(ctx);
2043
Jens Axboe11365042019-10-16 09:08:32 -06002044 io_put_req(treq, NULL);
Jens Axboeba816ad2019-09-28 11:36:45 -06002045 io_put_req(req, NULL);
Jens Axboe11365042019-10-16 09:08:32 -06002046 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06002047}
2048
2049static int io_timeout(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2050{
yangerkun5da0fb12019-10-15 21:59:29 +08002051 unsigned count;
Jens Axboe5262f562019-09-17 12:26:57 -06002052 struct io_ring_ctx *ctx = req->ctx;
2053 struct list_head *entry;
Jens Axboea41525a2019-10-15 16:48:15 -06002054 enum hrtimer_mode mode;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06002055 struct timespec64 ts;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002056 unsigned span = 0;
Jens Axboea41525a2019-10-15 16:48:15 -06002057 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06002058
2059 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2060 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06002061 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len != 1)
2062 return -EINVAL;
2063 flags = READ_ONCE(sqe->timeout_flags);
2064 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06002065 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06002066
2067 if (get_timespec64(&ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06002068 return -EFAULT;
2069
Jens Axboe11365042019-10-16 09:08:32 -06002070 if (flags & IORING_TIMEOUT_ABS)
2071 mode = HRTIMER_MODE_ABS;
2072 else
2073 mode = HRTIMER_MODE_REL;
2074
2075 hrtimer_init(&req->timeout.timer, CLOCK_MONOTONIC, mode);
2076
Jens Axboe5262f562019-09-17 12:26:57 -06002077 /*
2078 * sqe->off holds how many events that need to occur for this
2079 * timeout event to be satisfied.
2080 */
2081 count = READ_ONCE(sqe->off);
2082 if (!count)
2083 count = 1;
2084
2085 req->sequence = ctx->cached_sq_head + count - 1;
yangerkun5da0fb12019-10-15 21:59:29 +08002086 /* reuse it to store the count */
2087 req->submit.sequence = count;
Jens Axboe5262f562019-09-17 12:26:57 -06002088 req->flags |= REQ_F_TIMEOUT;
2089
2090 /*
2091 * Insertion sort, ensuring the first entry in the list is always
2092 * the one we need first.
2093 */
Jens Axboe5262f562019-09-17 12:26:57 -06002094 spin_lock_irq(&ctx->completion_lock);
2095 list_for_each_prev(entry, &ctx->timeout_list) {
2096 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
yangerkun5da0fb12019-10-15 21:59:29 +08002097 unsigned nxt_sq_head;
2098 long long tmp, tmp_nxt;
Jens Axboe5262f562019-09-17 12:26:57 -06002099
yangerkun5da0fb12019-10-15 21:59:29 +08002100 /*
2101 * Since cached_sq_head + count - 1 can overflow, use type long
2102 * long to store it.
2103 */
2104 tmp = (long long)ctx->cached_sq_head + count - 1;
2105 nxt_sq_head = nxt->sequence - nxt->submit.sequence + 1;
2106 tmp_nxt = (long long)nxt_sq_head + nxt->submit.sequence - 1;
2107
2108 /*
2109 * cached_sq_head may overflow, and it will never overflow twice
2110 * once there is some timeout req still be valid.
2111 */
2112 if (ctx->cached_sq_head < nxt_sq_head)
yangerkun8b07a652019-10-17 12:12:35 +08002113 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08002114
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002115 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06002116 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002117
2118 /*
2119 * Sequence of reqs after the insert one and itself should
2120 * be adjusted because each timeout req consumes a slot.
2121 */
2122 span++;
2123 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06002124 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002125 req->sequence -= span;
Jens Axboe5262f562019-09-17 12:26:57 -06002126 list_add(&req->list, entry);
2127 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06002128 req->timeout.timer.function = io_timeout_fn;
Jens Axboea41525a2019-10-15 16:48:15 -06002129 hrtimer_start(&req->timeout.timer, timespec64_to_ktime(ts), mode);
Jens Axboe5262f562019-09-17 12:26:57 -06002130 return 0;
2131}
2132
Jens Axboede0617e2019-04-06 21:51:27 -06002133static int io_req_defer(struct io_ring_ctx *ctx, struct io_kiocb *req,
2134 const struct io_uring_sqe *sqe)
2135{
2136 struct io_uring_sqe *sqe_copy;
2137
2138 if (!io_sequence_defer(ctx, req) && list_empty(&ctx->defer_list))
2139 return 0;
2140
2141 sqe_copy = kmalloc(sizeof(*sqe_copy), GFP_KERNEL);
2142 if (!sqe_copy)
2143 return -EAGAIN;
2144
2145 spin_lock_irq(&ctx->completion_lock);
2146 if (!io_sequence_defer(ctx, req) && list_empty(&ctx->defer_list)) {
2147 spin_unlock_irq(&ctx->completion_lock);
2148 kfree(sqe_copy);
2149 return 0;
2150 }
2151
2152 memcpy(sqe_copy, sqe, sizeof(*sqe_copy));
2153 req->submit.sqe = sqe_copy;
2154
2155 INIT_WORK(&req->work, io_sq_wq_submit_work);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002156 trace_io_uring_defer(ctx, req, false);
Jens Axboede0617e2019-04-06 21:51:27 -06002157 list_add_tail(&req->list, &ctx->defer_list);
2158 spin_unlock_irq(&ctx->completion_lock);
2159 return -EIOCBQUEUED;
2160}
2161
Jens Axboe2b188cc2019-01-07 10:46:33 -07002162static int __io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
Jens Axboeba816ad2019-09-28 11:36:45 -06002163 const struct sqe_submit *s, struct io_kiocb **nxt,
2164 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002165{
Jens Axboee0c5c572019-03-12 10:18:47 -06002166 int ret, opcode;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002167
Jens Axboe9e645e112019-05-10 16:07:28 -06002168 req->user_data = READ_ONCE(s->sqe->user_data);
2169
Jens Axboe2b188cc2019-01-07 10:46:33 -07002170 opcode = READ_ONCE(s->sqe->opcode);
2171 switch (opcode) {
2172 case IORING_OP_NOP:
2173 ret = io_nop(req, req->user_data);
2174 break;
2175 case IORING_OP_READV:
Jens Axboeedafcce2019-01-09 09:16:05 -07002176 if (unlikely(s->sqe->buf_index))
2177 return -EINVAL;
Jens Axboeba816ad2019-09-28 11:36:45 -06002178 ret = io_read(req, s, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002179 break;
2180 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07002181 if (unlikely(s->sqe->buf_index))
2182 return -EINVAL;
Jens Axboeba816ad2019-09-28 11:36:45 -06002183 ret = io_write(req, s, nxt, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07002184 break;
2185 case IORING_OP_READ_FIXED:
Jens Axboeba816ad2019-09-28 11:36:45 -06002186 ret = io_read(req, s, nxt, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07002187 break;
2188 case IORING_OP_WRITE_FIXED:
Jens Axboeba816ad2019-09-28 11:36:45 -06002189 ret = io_write(req, s, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002190 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002191 case IORING_OP_FSYNC:
Jens Axboeba816ad2019-09-28 11:36:45 -06002192 ret = io_fsync(req, s->sqe, nxt, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002193 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002194 case IORING_OP_POLL_ADD:
2195 ret = io_poll_add(req, s->sqe);
2196 break;
2197 case IORING_OP_POLL_REMOVE:
2198 ret = io_poll_remove(req, s->sqe);
2199 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002200 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboeba816ad2019-09-28 11:36:45 -06002201 ret = io_sync_file_range(req, s->sqe, nxt, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002202 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002203 case IORING_OP_SENDMSG:
Jens Axboeba816ad2019-09-28 11:36:45 -06002204 ret = io_sendmsg(req, s->sqe, nxt, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06002205 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06002206 case IORING_OP_RECVMSG:
Jens Axboeba816ad2019-09-28 11:36:45 -06002207 ret = io_recvmsg(req, s->sqe, nxt, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06002208 break;
Jens Axboe5262f562019-09-17 12:26:57 -06002209 case IORING_OP_TIMEOUT:
2210 ret = io_timeout(req, s->sqe);
2211 break;
Jens Axboe11365042019-10-16 09:08:32 -06002212 case IORING_OP_TIMEOUT_REMOVE:
2213 ret = io_timeout_remove(req, s->sqe);
2214 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002215 default:
2216 ret = -EINVAL;
2217 break;
2218 }
2219
Jens Axboedef596e2019-01-09 08:59:42 -07002220 if (ret)
2221 return ret;
2222
2223 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe9e645e112019-05-10 16:07:28 -06002224 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07002225 return -EAGAIN;
2226
2227 /* workqueue context doesn't hold uring_lock, grab it now */
Jackie Liuba5290c2019-10-09 09:19:59 +08002228 if (s->in_async)
Jens Axboedef596e2019-01-09 08:59:42 -07002229 mutex_lock(&ctx->uring_lock);
2230 io_iopoll_req_issued(req);
Jackie Liuba5290c2019-10-09 09:19:59 +08002231 if (s->in_async)
Jens Axboedef596e2019-01-09 08:59:42 -07002232 mutex_unlock(&ctx->uring_lock);
2233 }
2234
2235 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002236}
2237
Jens Axboe31b51512019-01-18 22:56:34 -07002238static struct async_list *io_async_list_from_sqe(struct io_ring_ctx *ctx,
2239 const struct io_uring_sqe *sqe)
2240{
2241 switch (sqe->opcode) {
2242 case IORING_OP_READV:
2243 case IORING_OP_READ_FIXED:
2244 return &ctx->pending_async[READ];
2245 case IORING_OP_WRITEV:
2246 case IORING_OP_WRITE_FIXED:
2247 return &ctx->pending_async[WRITE];
2248 default:
2249 return NULL;
2250 }
2251}
2252
Jens Axboeedafcce2019-01-09 09:16:05 -07002253static inline bool io_sqe_needs_user(const struct io_uring_sqe *sqe)
2254{
2255 u8 opcode = READ_ONCE(sqe->opcode);
2256
2257 return !(opcode == IORING_OP_READ_FIXED ||
2258 opcode == IORING_OP_WRITE_FIXED);
2259}
2260
Jens Axboe2b188cc2019-01-07 10:46:33 -07002261static void io_sq_wq_submit_work(struct work_struct *work)
2262{
2263 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002264 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe31b51512019-01-18 22:56:34 -07002265 struct mm_struct *cur_mm = NULL;
2266 struct async_list *async_list;
2267 LIST_HEAD(req_list);
Jens Axboeedafcce2019-01-09 09:16:05 -07002268 mm_segment_t old_fs;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002269 int ret;
2270
Jens Axboe31b51512019-01-18 22:56:34 -07002271 async_list = io_async_list_from_sqe(ctx, req->submit.sqe);
2272restart:
2273 do {
2274 struct sqe_submit *s = &req->submit;
2275 const struct io_uring_sqe *sqe = s->sqe;
Jackie Liud0ee8792019-07-31 14:39:33 +08002276 unsigned int flags = req->flags;
Jens Axboeba816ad2019-09-28 11:36:45 -06002277 struct io_kiocb *nxt = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002278
Stefan Bühler8449eed2019-04-27 20:34:19 +02002279 /* Ensure we clear previously set non-block flag */
Jens Axboe31b51512019-01-18 22:56:34 -07002280 req->rw.ki_flags &= ~IOCB_NOWAIT;
2281
2282 ret = 0;
2283 if (io_sqe_needs_user(sqe) && !cur_mm) {
2284 if (!mmget_not_zero(ctx->sqo_mm)) {
2285 ret = -EFAULT;
2286 } else {
2287 cur_mm = ctx->sqo_mm;
2288 use_mm(cur_mm);
2289 old_fs = get_fs();
2290 set_fs(USER_DS);
2291 }
2292 }
2293
2294 if (!ret) {
2295 s->has_user = cur_mm != NULL;
Jackie Liuba5290c2019-10-09 09:19:59 +08002296 s->in_async = true;
Jens Axboe31b51512019-01-18 22:56:34 -07002297 do {
Jens Axboeba816ad2019-09-28 11:36:45 -06002298 ret = __io_submit_sqe(ctx, req, s, &nxt, false);
Jens Axboe31b51512019-01-18 22:56:34 -07002299 /*
2300 * We can get EAGAIN for polled IO even though
2301 * we're forcing a sync submission from here,
2302 * since we can't wait for request slots on the
2303 * block side.
2304 */
2305 if (ret != -EAGAIN)
2306 break;
2307 cond_resched();
2308 } while (1);
2309 }
Jens Axboe817869d2019-04-30 14:44:05 -06002310
2311 /* drop submission reference */
Jens Axboeba816ad2019-09-28 11:36:45 -06002312 io_put_req(req, NULL);
Jens Axboe817869d2019-04-30 14:44:05 -06002313
Jens Axboe31b51512019-01-18 22:56:34 -07002314 if (ret) {
Jens Axboec71ffb62019-05-13 20:58:29 -06002315 io_cqring_add_event(ctx, sqe->user_data, ret);
Jens Axboeba816ad2019-09-28 11:36:45 -06002316 io_put_req(req, NULL);
Jens Axboe31b51512019-01-18 22:56:34 -07002317 }
2318
2319 /* async context always use a copy of the sqe */
2320 kfree(sqe);
2321
Jens Axboeba816ad2019-09-28 11:36:45 -06002322 /* if a dependent link is ready, do that as the next one */
2323 if (!ret && nxt) {
2324 req = nxt;
2325 continue;
2326 }
2327
Zhengyuan Liuf7b76ac2019-07-16 23:26:14 +08002328 /* req from defer and link list needn't decrease async cnt */
Jackie Liud0ee8792019-07-31 14:39:33 +08002329 if (flags & (REQ_F_IO_DRAINED | REQ_F_LINK_DONE))
Zhengyuan Liuf7b76ac2019-07-16 23:26:14 +08002330 goto out;
2331
Jens Axboe31b51512019-01-18 22:56:34 -07002332 if (!async_list)
2333 break;
2334 if (!list_empty(&req_list)) {
2335 req = list_first_entry(&req_list, struct io_kiocb,
2336 list);
2337 list_del(&req->list);
2338 continue;
2339 }
2340 if (list_empty(&async_list->list))
2341 break;
2342
2343 req = NULL;
2344 spin_lock(&async_list->lock);
2345 if (list_empty(&async_list->list)) {
2346 spin_unlock(&async_list->lock);
2347 break;
2348 }
2349 list_splice_init(&async_list->list, &req_list);
2350 spin_unlock(&async_list->lock);
2351
2352 req = list_first_entry(&req_list, struct io_kiocb, list);
2353 list_del(&req->list);
2354 } while (req);
Jens Axboeedafcce2019-01-09 09:16:05 -07002355
2356 /*
Jens Axboe31b51512019-01-18 22:56:34 -07002357 * Rare case of racing with a submitter. If we find the count has
2358 * dropped to zero AND we have pending work items, then restart
2359 * the processing. This is a tiny race window.
Jens Axboeedafcce2019-01-09 09:16:05 -07002360 */
Jens Axboe31b51512019-01-18 22:56:34 -07002361 if (async_list) {
2362 ret = atomic_dec_return(&async_list->cnt);
2363 while (!ret && !list_empty(&async_list->list)) {
2364 spin_lock(&async_list->lock);
2365 atomic_inc(&async_list->cnt);
2366 list_splice_init(&async_list->list, &req_list);
2367 spin_unlock(&async_list->lock);
2368
2369 if (!list_empty(&req_list)) {
2370 req = list_first_entry(&req_list,
2371 struct io_kiocb, list);
2372 list_del(&req->list);
2373 goto restart;
2374 }
2375 ret = atomic_dec_return(&async_list->cnt);
Jens Axboeedafcce2019-01-09 09:16:05 -07002376 }
Jens Axboeedafcce2019-01-09 09:16:05 -07002377 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002378
Zhengyuan Liuf7b76ac2019-07-16 23:26:14 +08002379out:
Jens Axboe31b51512019-01-18 22:56:34 -07002380 if (cur_mm) {
Jens Axboeedafcce2019-01-09 09:16:05 -07002381 set_fs(old_fs);
Jens Axboe31b51512019-01-18 22:56:34 -07002382 unuse_mm(cur_mm);
2383 mmput(cur_mm);
Jens Axboeedafcce2019-01-09 09:16:05 -07002384 }
Jens Axboe31b51512019-01-18 22:56:34 -07002385}
Jens Axboe2b188cc2019-01-07 10:46:33 -07002386
Jens Axboe31b51512019-01-18 22:56:34 -07002387/*
2388 * See if we can piggy back onto previously submitted work, that is still
2389 * running. We currently only allow this if the new request is sequential
2390 * to the previous one we punted.
2391 */
2392static bool io_add_to_prev_work(struct async_list *list, struct io_kiocb *req)
2393{
Jens Axboe6d5d5ac2019-09-11 10:16:13 -06002394 bool ret;
Jens Axboe31b51512019-01-18 22:56:34 -07002395
2396 if (!list)
2397 return false;
2398 if (!(req->flags & REQ_F_SEQ_PREV))
2399 return false;
2400 if (!atomic_read(&list->cnt))
2401 return false;
2402
2403 ret = true;
2404 spin_lock(&list->lock);
2405 list_add_tail(&req->list, &list->list);
Zhengyuan Liuc0e48f92019-07-18 20:44:00 +08002406 /*
2407 * Ensure we see a simultaneous modification from io_sq_wq_submit_work()
2408 */
2409 smp_mb();
Jens Axboe31b51512019-01-18 22:56:34 -07002410 if (!atomic_read(&list->cnt)) {
2411 list_del_init(&req->list);
2412 ret = false;
2413 }
2414 spin_unlock(&list->lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002415
2416 trace_io_uring_add_to_prev(req, ret);
Jens Axboe31b51512019-01-18 22:56:34 -07002417 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002418}
2419
Jens Axboe09bb8392019-03-13 12:39:28 -06002420static bool io_op_needs_file(const struct io_uring_sqe *sqe)
2421{
2422 int op = READ_ONCE(sqe->opcode);
2423
2424 switch (op) {
2425 case IORING_OP_NOP:
2426 case IORING_OP_POLL_REMOVE:
2427 return false;
2428 default:
2429 return true;
2430 }
2431}
2432
2433static int io_req_set_file(struct io_ring_ctx *ctx, const struct sqe_submit *s,
2434 struct io_submit_state *state, struct io_kiocb *req)
2435{
2436 unsigned flags;
2437 int fd;
2438
2439 flags = READ_ONCE(s->sqe->flags);
2440 fd = READ_ONCE(s->sqe->fd);
2441
Jackie Liu4fe2c962019-09-09 20:50:40 +08002442 if (flags & IOSQE_IO_DRAIN)
Jens Axboede0617e2019-04-06 21:51:27 -06002443 req->flags |= REQ_F_IO_DRAIN;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002444 /*
2445 * All io need record the previous position, if LINK vs DARIN,
2446 * it can be used to mark the position of the first IO in the
2447 * link list.
2448 */
2449 req->sequence = s->sequence;
Jens Axboede0617e2019-04-06 21:51:27 -06002450
Jens Axboe60c112b2019-06-21 10:20:18 -06002451 if (!io_op_needs_file(s->sqe))
Jens Axboe09bb8392019-03-13 12:39:28 -06002452 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06002453
2454 if (flags & IOSQE_FIXED_FILE) {
2455 if (unlikely(!ctx->user_files ||
2456 (unsigned) fd >= ctx->nr_user_files))
2457 return -EBADF;
Jens Axboe08a45172019-10-03 08:11:03 -06002458 if (!ctx->user_files[fd])
2459 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06002460 req->file = ctx->user_files[fd];
2461 req->flags |= REQ_F_FIXED_FILE;
2462 } else {
2463 if (s->needs_fixed_file)
2464 return -EBADF;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002465 trace_io_uring_file_get(ctx, fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06002466 req->file = io_file_get(state, fd);
2467 if (unlikely(!req->file))
2468 return -EBADF;
2469 }
2470
2471 return 0;
2472}
2473
Jackie Liu4fe2c962019-09-09 20:50:40 +08002474static int __io_queue_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
Jens Axboebc808bc2019-10-22 13:14:37 -06002475 struct sqe_submit *s)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002476{
Jens Axboee0c5c572019-03-12 10:18:47 -06002477 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002478
Jens Axboeba816ad2019-09-28 11:36:45 -06002479 ret = __io_submit_sqe(ctx, req, s, NULL, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06002480
2481 /*
2482 * We async punt it if the file wasn't marked NOWAIT, or if the file
2483 * doesn't support non-blocking read/write attempts
2484 */
2485 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
2486 (req->flags & REQ_F_MUST_PUNT))) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07002487 struct io_uring_sqe *sqe_copy;
2488
Jackie Liu954dab12019-09-18 10:37:52 +08002489 sqe_copy = kmemdup(s->sqe, sizeof(*sqe_copy), GFP_KERNEL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002490 if (sqe_copy) {
Jens Axboe31b51512019-01-18 22:56:34 -07002491 struct async_list *list;
2492
Jens Axboe2b188cc2019-01-07 10:46:33 -07002493 s->sqe = sqe_copy;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002494 memcpy(&req->submit, s, sizeof(*s));
Jens Axboe31b51512019-01-18 22:56:34 -07002495 list = io_async_list_from_sqe(ctx, s->sqe);
2496 if (!io_add_to_prev_work(list, req)) {
2497 if (list)
2498 atomic_inc(&list->cnt);
2499 INIT_WORK(&req->work, io_sq_wq_submit_work);
Jens Axboe18d9be12019-09-10 09:13:05 -06002500 io_queue_async_work(ctx, req);
Jens Axboe31b51512019-01-18 22:56:34 -07002501 }
Jens Axboee65ef562019-03-12 10:16:44 -06002502
2503 /*
2504 * Queued up for async execution, worker will release
Jens Axboe9e645e112019-05-10 16:07:28 -06002505 * submit reference when the iocb is actually submitted.
Jens Axboee65ef562019-03-12 10:16:44 -06002506 */
2507 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002508 }
2509 }
Jens Axboee65ef562019-03-12 10:16:44 -06002510
2511 /* drop submission reference */
Jens Axboeba816ad2019-09-28 11:36:45 -06002512 io_put_req(req, NULL);
Jens Axboee65ef562019-03-12 10:16:44 -06002513
2514 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06002515 if (ret) {
2516 io_cqring_add_event(ctx, req->user_data, ret);
2517 if (req->flags & REQ_F_LINK)
2518 req->flags |= REQ_F_FAIL_LINK;
Jens Axboeba816ad2019-09-28 11:36:45 -06002519 io_put_req(req, NULL);
Jens Axboe9e645e112019-05-10 16:07:28 -06002520 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002521
2522 return ret;
2523}
2524
Jackie Liu4fe2c962019-09-09 20:50:40 +08002525static int io_queue_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
Jens Axboebc808bc2019-10-22 13:14:37 -06002526 struct sqe_submit *s)
Jackie Liu4fe2c962019-09-09 20:50:40 +08002527{
2528 int ret;
2529
2530 ret = io_req_defer(ctx, req, s->sqe);
2531 if (ret) {
2532 if (ret != -EIOCBQUEUED) {
Jens Axboeba816ad2019-09-28 11:36:45 -06002533 io_free_req(req, NULL);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002534 io_cqring_add_event(ctx, s->sqe->user_data, ret);
2535 }
2536 return 0;
2537 }
2538
Jens Axboebc808bc2019-10-22 13:14:37 -06002539 return __io_queue_sqe(ctx, req, s);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002540}
2541
2542static int io_queue_link_head(struct io_ring_ctx *ctx, struct io_kiocb *req,
Jens Axboebc808bc2019-10-22 13:14:37 -06002543 struct sqe_submit *s, struct io_kiocb *shadow)
Jackie Liu4fe2c962019-09-09 20:50:40 +08002544{
2545 int ret;
2546 int need_submit = false;
2547
2548 if (!shadow)
Jens Axboebc808bc2019-10-22 13:14:37 -06002549 return io_queue_sqe(ctx, req, s);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002550
2551 /*
2552 * Mark the first IO in link list as DRAIN, let all the following
2553 * IOs enter the defer list. all IO needs to be completed before link
2554 * list.
2555 */
2556 req->flags |= REQ_F_IO_DRAIN;
2557 ret = io_req_defer(ctx, req, s->sqe);
2558 if (ret) {
2559 if (ret != -EIOCBQUEUED) {
Jens Axboeba816ad2019-09-28 11:36:45 -06002560 io_free_req(req, NULL);
Pavel Begunkov7b202382019-10-27 22:10:36 +03002561 __io_free_req(shadow);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002562 io_cqring_add_event(ctx, s->sqe->user_data, ret);
2563 return 0;
2564 }
2565 } else {
2566 /*
2567 * If ret == 0 means that all IOs in front of link io are
2568 * running done. let's queue link head.
2569 */
2570 need_submit = true;
2571 }
2572
2573 /* Insert shadow req to defer_list, blocking next IOs */
2574 spin_lock_irq(&ctx->completion_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002575 trace_io_uring_defer(ctx, shadow, true);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002576 list_add_tail(&shadow->list, &ctx->defer_list);
2577 spin_unlock_irq(&ctx->completion_lock);
2578
2579 if (need_submit)
Jens Axboebc808bc2019-10-22 13:14:37 -06002580 return __io_queue_sqe(ctx, req, s);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002581
2582 return 0;
2583}
2584
Jens Axboe9e645e112019-05-10 16:07:28 -06002585#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK)
2586
2587static void io_submit_sqe(struct io_ring_ctx *ctx, struct sqe_submit *s,
Jens Axboebc808bc2019-10-22 13:14:37 -06002588 struct io_submit_state *state, struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06002589{
2590 struct io_uring_sqe *sqe_copy;
2591 struct io_kiocb *req;
2592 int ret;
2593
2594 /* enforce forwards compatibility on users */
2595 if (unlikely(s->sqe->flags & ~SQE_VALID_FLAGS)) {
2596 ret = -EINVAL;
2597 goto err;
2598 }
2599
2600 req = io_get_req(ctx, state);
2601 if (unlikely(!req)) {
2602 ret = -EAGAIN;
2603 goto err;
2604 }
2605
2606 ret = io_req_set_file(ctx, s, state, req);
2607 if (unlikely(ret)) {
2608err_req:
Jens Axboeba816ad2019-09-28 11:36:45 -06002609 io_free_req(req, NULL);
Jens Axboe9e645e112019-05-10 16:07:28 -06002610err:
2611 io_cqring_add_event(ctx, s->sqe->user_data, ret);
2612 return;
2613 }
2614
Pavel Begunkov84d55dc2019-10-25 12:31:29 +03002615 req->user_data = s->sqe->user_data;
2616
Jens Axboe9e645e112019-05-10 16:07:28 -06002617 /*
2618 * If we already have a head request, queue this one for async
2619 * submittal once the head completes. If we don't have a head but
2620 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
2621 * submitted sync once the chain is complete. If none of those
2622 * conditions are true (normal request), then just queue it.
2623 */
2624 if (*link) {
2625 struct io_kiocb *prev = *link;
2626
2627 sqe_copy = kmemdup(s->sqe, sizeof(*sqe_copy), GFP_KERNEL);
2628 if (!sqe_copy) {
2629 ret = -EAGAIN;
2630 goto err_req;
2631 }
2632
2633 s->sqe = sqe_copy;
2634 memcpy(&req->submit, s, sizeof(*s));
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002635 trace_io_uring_link(ctx, req, prev);
Jens Axboe9e645e112019-05-10 16:07:28 -06002636 list_add_tail(&req->list, &prev->link_list);
2637 } else if (s->sqe->flags & IOSQE_IO_LINK) {
2638 req->flags |= REQ_F_LINK;
2639
2640 memcpy(&req->submit, s, sizeof(*s));
2641 INIT_LIST_HEAD(&req->link_list);
2642 *link = req;
2643 } else {
Jens Axboebc808bc2019-10-22 13:14:37 -06002644 io_queue_sqe(ctx, req, s);
Jens Axboe9e645e112019-05-10 16:07:28 -06002645 }
2646}
2647
Jens Axboe9a56a232019-01-09 09:06:50 -07002648/*
2649 * Batched submission is done, ensure local IO is flushed out.
2650 */
2651static void io_submit_state_end(struct io_submit_state *state)
2652{
2653 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06002654 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07002655 if (state->free_reqs)
2656 kmem_cache_free_bulk(req_cachep, state->free_reqs,
2657 &state->reqs[state->cur_req]);
Jens Axboe9a56a232019-01-09 09:06:50 -07002658}
2659
2660/*
2661 * Start submission side cache.
2662 */
2663static void io_submit_state_start(struct io_submit_state *state,
2664 struct io_ring_ctx *ctx, unsigned max_ios)
2665{
2666 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07002667 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07002668 state->file = NULL;
2669 state->ios_left = max_ios;
2670}
2671
Jens Axboe2b188cc2019-01-07 10:46:33 -07002672static void io_commit_sqring(struct io_ring_ctx *ctx)
2673{
Hristo Venev75b28af2019-08-26 17:23:46 +00002674 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002675
Hristo Venev75b28af2019-08-26 17:23:46 +00002676 if (ctx->cached_sq_head != READ_ONCE(rings->sq.head)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07002677 /*
2678 * Ensure any loads from the SQEs are done at this point,
2679 * since once we write the new head, the application could
2680 * write new data to them.
2681 */
Hristo Venev75b28af2019-08-26 17:23:46 +00002682 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002683 }
2684}
2685
2686/*
Jens Axboe2b188cc2019-01-07 10:46:33 -07002687 * Fetch an sqe, if one is available. Note that s->sqe will point to memory
2688 * that is mapped by userspace. This means that care needs to be taken to
2689 * ensure that reads are stable, as we cannot rely on userspace always
2690 * being a good citizen. If members of the sqe are validated and then later
2691 * used, it's important that those reads are done through READ_ONCE() to
2692 * prevent a re-load down the line.
2693 */
2694static bool io_get_sqring(struct io_ring_ctx *ctx, struct sqe_submit *s)
2695{
Hristo Venev75b28af2019-08-26 17:23:46 +00002696 struct io_rings *rings = ctx->rings;
2697 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002698 unsigned head;
2699
2700 /*
2701 * The cached sq head (or cq tail) serves two purposes:
2702 *
2703 * 1) allows us to batch the cost of updating the user visible
2704 * head updates.
2705 * 2) allows the kernel side to track the head on its own, even
2706 * though the application is the one updating it.
2707 */
2708 head = ctx->cached_sq_head;
Stefan Bühlere523a292019-04-19 11:57:44 +02002709 /* make sure SQ entry isn't read before tail */
Hristo Venev75b28af2019-08-26 17:23:46 +00002710 if (head == smp_load_acquire(&rings->sq.tail))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002711 return false;
2712
Hristo Venev75b28af2019-08-26 17:23:46 +00002713 head = READ_ONCE(sq_array[head & ctx->sq_mask]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002714 if (head < ctx->sq_entries) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07002715 s->sqe = &ctx->sq_sqes[head];
Jackie Liu8776f3f2019-09-09 20:50:39 +08002716 s->sequence = ctx->cached_sq_head;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002717 ctx->cached_sq_head++;
2718 return true;
2719 }
2720
2721 /* drop invalid entries */
2722 ctx->cached_sq_head++;
Jens Axboe498ccd92019-10-25 10:04:25 -06002723 ctx->cached_sq_dropped++;
2724 WRITE_ONCE(rings->sq_dropped, ctx->cached_sq_dropped);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002725 return false;
2726}
2727
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002728static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
2729 bool has_user, bool mm_fault)
Jens Axboe6c271ce2019-01-10 11:22:30 -07002730{
2731 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002732 struct io_kiocb *link = NULL;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002733 struct io_kiocb *shadow_req = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002734 bool prev_was_link = false;
2735 int i, submitted = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002736
2737 if (nr > IO_PLUG_THRESHOLD) {
2738 io_submit_state_start(&state, ctx, nr);
2739 statep = &state;
2740 }
2741
2742 for (i = 0; i < nr; i++) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002743 struct sqe_submit s;
2744
2745 if (!io_get_sqring(ctx, &s))
2746 break;
2747
Jens Axboe9e645e112019-05-10 16:07:28 -06002748 /*
2749 * If previous wasn't linked and we have a linked command,
2750 * that's the end of the chain. Submit the previous link.
2751 */
2752 if (!prev_was_link && link) {
Jens Axboebc808bc2019-10-22 13:14:37 -06002753 io_queue_link_head(ctx, link, &link->submit, shadow_req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002754 link = NULL;
Jackie Liu5f5ad9c2019-09-18 10:37:53 +08002755 shadow_req = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002756 }
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002757 prev_was_link = (s.sqe->flags & IOSQE_IO_LINK) != 0;
Jens Axboe9e645e112019-05-10 16:07:28 -06002758
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002759 if (link && (s.sqe->flags & IOSQE_IO_DRAIN)) {
Jackie Liu4fe2c962019-09-09 20:50:40 +08002760 if (!shadow_req) {
2761 shadow_req = io_get_req(ctx, NULL);
Jackie Liua1041c22019-09-18 17:25:52 +08002762 if (unlikely(!shadow_req))
2763 goto out;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002764 shadow_req->flags |= (REQ_F_IO_DRAIN | REQ_F_SHADOW_DRAIN);
2765 refcount_dec(&shadow_req->refs);
2766 }
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002767 shadow_req->sequence = s.sequence;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002768 }
2769
Jackie Liua1041c22019-09-18 17:25:52 +08002770out:
Jens Axboe6c271ce2019-01-10 11:22:30 -07002771 if (unlikely(mm_fault)) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002772 io_cqring_add_event(ctx, s.sqe->user_data,
Jens Axboe9e645e112019-05-10 16:07:28 -06002773 -EFAULT);
Jens Axboe6c271ce2019-01-10 11:22:30 -07002774 } else {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002775 s.has_user = has_user;
Jackie Liuba5290c2019-10-09 09:19:59 +08002776 s.in_async = true;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002777 s.needs_fixed_file = true;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002778 trace_io_uring_submit_sqe(ctx, true, true);
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002779 io_submit_sqe(ctx, &s, statep, &link);
Jens Axboe6c271ce2019-01-10 11:22:30 -07002780 submitted++;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002781 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07002782 }
2783
Jens Axboe9e645e112019-05-10 16:07:28 -06002784 if (link)
Jens Axboebc808bc2019-10-22 13:14:37 -06002785 io_queue_link_head(ctx, link, &link->submit, shadow_req);
Jens Axboe6c271ce2019-01-10 11:22:30 -07002786 if (statep)
2787 io_submit_state_end(&state);
2788
2789 return submitted;
2790}
2791
2792static int io_sq_thread(void *data)
2793{
Jens Axboe6c271ce2019-01-10 11:22:30 -07002794 struct io_ring_ctx *ctx = data;
2795 struct mm_struct *cur_mm = NULL;
2796 mm_segment_t old_fs;
2797 DEFINE_WAIT(wait);
2798 unsigned inflight;
2799 unsigned long timeout;
2800
Jackie Liua4c0b3d2019-07-08 13:41:12 +08002801 complete(&ctx->sqo_thread_started);
2802
Jens Axboe6c271ce2019-01-10 11:22:30 -07002803 old_fs = get_fs();
2804 set_fs(USER_DS);
2805
2806 timeout = inflight = 0;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02002807 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002808 bool mm_fault = false;
2809 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002810
2811 if (inflight) {
2812 unsigned nr_events = 0;
2813
2814 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe2b2ed972019-10-25 10:06:15 -06002815 /*
2816 * inflight is the count of the maximum possible
2817 * entries we submitted, but it can be smaller
2818 * if we dropped some of them. If we don't have
2819 * poll entries available, then we know that we
2820 * have nothing left to poll for. Reset the
2821 * inflight count to zero in that case.
2822 */
2823 mutex_lock(&ctx->uring_lock);
2824 if (!list_empty(&ctx->poll_list))
2825 __io_iopoll_check(ctx, &nr_events, 0);
2826 else
2827 inflight = 0;
2828 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07002829 } else {
2830 /*
2831 * Normal IO, just pretend everything completed.
2832 * We don't have to poll completions for that.
2833 */
2834 nr_events = inflight;
2835 }
2836
2837 inflight -= nr_events;
2838 if (!inflight)
2839 timeout = jiffies + ctx->sq_thread_idle;
2840 }
2841
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002842 to_submit = io_sqring_entries(ctx);
2843 if (!to_submit) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07002844 /*
2845 * We're polling. If we're within the defined idle
2846 * period, then let us spin without work before going
2847 * to sleep.
2848 */
2849 if (inflight || !time_after(jiffies, timeout)) {
Jens Axboe9831a902019-09-19 09:48:55 -06002850 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07002851 continue;
2852 }
2853
2854 /*
2855 * Drop cur_mm before scheduling, we can't hold it for
2856 * long periods (or over schedule()). Do this before
2857 * adding ourselves to the waitqueue, as the unuse/drop
2858 * may sleep.
2859 */
2860 if (cur_mm) {
2861 unuse_mm(cur_mm);
2862 mmput(cur_mm);
2863 cur_mm = NULL;
2864 }
2865
2866 prepare_to_wait(&ctx->sqo_wait, &wait,
2867 TASK_INTERRUPTIBLE);
2868
2869 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00002870 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02002871 /* make sure to read SQ tail after writing flags */
2872 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07002873
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002874 to_submit = io_sqring_entries(ctx);
2875 if (!to_submit) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02002876 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07002877 finish_wait(&ctx->sqo_wait, &wait);
2878 break;
2879 }
2880 if (signal_pending(current))
2881 flush_signals(current);
2882 schedule();
2883 finish_wait(&ctx->sqo_wait, &wait);
2884
Hristo Venev75b28af2019-08-26 17:23:46 +00002885 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002886 continue;
2887 }
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 }
2892
Jens Axboe6c271ce2019-01-10 11:22:30 -07002893 /* Unless all new commands are FIXED regions, grab mm */
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002894 if (!cur_mm) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07002895 mm_fault = !mmget_not_zero(ctx->sqo_mm);
2896 if (!mm_fault) {
2897 use_mm(ctx->sqo_mm);
2898 cur_mm = ctx->sqo_mm;
2899 }
2900 }
2901
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002902 to_submit = min(to_submit, ctx->sq_entries);
2903 inflight += io_submit_sqes(ctx, to_submit, cur_mm != NULL,
2904 mm_fault);
Jens Axboe6c271ce2019-01-10 11:22:30 -07002905
2906 /* Commit SQ ring head once we've consumed all SQEs */
2907 io_commit_sqring(ctx);
2908 }
2909
2910 set_fs(old_fs);
2911 if (cur_mm) {
2912 unuse_mm(cur_mm);
2913 mmput(cur_mm);
2914 }
Jens Axboe06058632019-04-13 09:26:03 -06002915
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02002916 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06002917
Jens Axboe6c271ce2019-01-10 11:22:30 -07002918 return 0;
2919}
2920
Jens Axboebc808bc2019-10-22 13:14:37 -06002921static int io_ring_submit(struct io_ring_ctx *ctx, unsigned int to_submit)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002922{
Jens Axboe9a56a232019-01-09 09:06:50 -07002923 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002924 struct io_kiocb *link = NULL;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002925 struct io_kiocb *shadow_req = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002926 bool prev_was_link = false;
Jens Axboe5c8b0b52019-04-30 10:16:07 -06002927 int i, submit = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002928
Jens Axboe9a56a232019-01-09 09:06:50 -07002929 if (to_submit > IO_PLUG_THRESHOLD) {
2930 io_submit_state_start(&state, ctx, to_submit);
2931 statep = &state;
2932 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002933
2934 for (i = 0; i < to_submit; i++) {
2935 struct sqe_submit s;
2936
2937 if (!io_get_sqring(ctx, &s))
2938 break;
2939
Jens Axboe9e645e112019-05-10 16:07:28 -06002940 /*
2941 * If previous wasn't linked and we have a linked command,
2942 * that's the end of the chain. Submit the previous link.
2943 */
2944 if (!prev_was_link && link) {
Jens Axboebc808bc2019-10-22 13:14:37 -06002945 io_queue_link_head(ctx, link, &link->submit, shadow_req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002946 link = NULL;
Jackie Liu5f5ad9c2019-09-18 10:37:53 +08002947 shadow_req = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002948 }
2949 prev_was_link = (s.sqe->flags & IOSQE_IO_LINK) != 0;
2950
Jackie Liu4fe2c962019-09-09 20:50:40 +08002951 if (link && (s.sqe->flags & IOSQE_IO_DRAIN)) {
2952 if (!shadow_req) {
2953 shadow_req = io_get_req(ctx, NULL);
Jackie Liua1041c22019-09-18 17:25:52 +08002954 if (unlikely(!shadow_req))
2955 goto out;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002956 shadow_req->flags |= (REQ_F_IO_DRAIN | REQ_F_SHADOW_DRAIN);
2957 refcount_dec(&shadow_req->refs);
2958 }
2959 shadow_req->sequence = s.sequence;
2960 }
2961
Jackie Liua1041c22019-09-18 17:25:52 +08002962out:
Jens Axboe2b188cc2019-01-07 10:46:33 -07002963 s.has_user = true;
Jackie Liuba5290c2019-10-09 09:19:59 +08002964 s.in_async = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002965 s.needs_fixed_file = false;
Jens Axboe5c8b0b52019-04-30 10:16:07 -06002966 submit++;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002967 trace_io_uring_submit_sqe(ctx, true, false);
Jens Axboebc808bc2019-10-22 13:14:37 -06002968 io_submit_sqe(ctx, &s, statep, &link);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002969 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002970
Jens Axboe9e645e112019-05-10 16:07:28 -06002971 if (link)
Jens Axboebc808bc2019-10-22 13:14:37 -06002972 io_queue_link_head(ctx, link, &link->submit, shadow_req);
Jens Axboe9a56a232019-01-09 09:06:50 -07002973 if (statep)
2974 io_submit_state_end(statep);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002975
Pavel Begunkov935d1e42019-10-25 12:31:31 +03002976 io_commit_sqring(ctx);
2977
Jens Axboe5c8b0b52019-04-30 10:16:07 -06002978 return submit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002979}
2980
Jens Axboebda52162019-09-24 13:47:15 -06002981struct io_wait_queue {
2982 struct wait_queue_entry wq;
2983 struct io_ring_ctx *ctx;
2984 unsigned to_wait;
2985 unsigned nr_timeouts;
2986};
2987
2988static inline bool io_should_wake(struct io_wait_queue *iowq)
2989{
2990 struct io_ring_ctx *ctx = iowq->ctx;
2991
2992 /*
2993 * Wake up if we have enough events, or if a timeout occured since we
2994 * started waiting. For timeouts, we always want to return to userspace,
2995 * regardless of event count.
2996 */
2997 return io_cqring_events(ctx->rings) >= iowq->to_wait ||
2998 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
2999}
3000
3001static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
3002 int wake_flags, void *key)
3003{
3004 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
3005 wq);
3006
3007 if (!io_should_wake(iowq))
3008 return -1;
3009
3010 return autoremove_wake_function(curr, mode, wake_flags, key);
3011}
3012
Jens Axboe2b188cc2019-01-07 10:46:33 -07003013/*
3014 * Wait until events become available, if we don't already have some. The
3015 * application must reap them itself, as they reside on the shared cq ring.
3016 */
3017static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
3018 const sigset_t __user *sig, size_t sigsz)
3019{
Jens Axboebda52162019-09-24 13:47:15 -06003020 struct io_wait_queue iowq = {
3021 .wq = {
3022 .private = current,
3023 .func = io_wake_function,
3024 .entry = LIST_HEAD_INIT(iowq.wq.entry),
3025 },
3026 .ctx = ctx,
3027 .to_wait = min_events,
3028 };
Hristo Venev75b28af2019-08-26 17:23:46 +00003029 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003030 int ret;
3031
Hristo Venev75b28af2019-08-26 17:23:46 +00003032 if (io_cqring_events(rings) >= min_events)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003033 return 0;
3034
3035 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01003036#ifdef CONFIG_COMPAT
3037 if (in_compat_syscall())
3038 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07003039 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01003040 else
3041#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07003042 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01003043
Jens Axboe2b188cc2019-01-07 10:46:33 -07003044 if (ret)
3045 return ret;
3046 }
3047
Jens Axboebda52162019-09-24 13:47:15 -06003048 ret = 0;
3049 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02003050 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06003051 do {
3052 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
3053 TASK_INTERRUPTIBLE);
3054 if (io_should_wake(&iowq))
3055 break;
3056 schedule();
3057 if (signal_pending(current)) {
3058 ret = -ERESTARTSYS;
3059 break;
3060 }
3061 } while (1);
3062 finish_wait(&ctx->wait, &iowq.wq);
3063
Oleg Nesterovb7724342019-07-16 16:29:53 -07003064 restore_saved_sigmask_unless(ret == -ERESTARTSYS);
Oleg Nesterov97abc882019-06-28 12:06:50 -07003065 if (ret == -ERESTARTSYS)
3066 ret = -EINTR;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003067
Hristo Venev75b28af2019-08-26 17:23:46 +00003068 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003069}
3070
Jens Axboe6b063142019-01-10 22:13:58 -07003071static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
3072{
3073#if defined(CONFIG_UNIX)
3074 if (ctx->ring_sock) {
3075 struct sock *sock = ctx->ring_sock->sk;
3076 struct sk_buff *skb;
3077
3078 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
3079 kfree_skb(skb);
3080 }
3081#else
3082 int i;
3083
3084 for (i = 0; i < ctx->nr_user_files; i++)
Jens Axboe08a45172019-10-03 08:11:03 -06003085 if (ctx->user_files[i])
3086 fput(ctx->user_files[i]);
Jens Axboe6b063142019-01-10 22:13:58 -07003087#endif
3088}
3089
3090static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
3091{
3092 if (!ctx->user_files)
3093 return -ENXIO;
3094
3095 __io_sqe_files_unregister(ctx);
3096 kfree(ctx->user_files);
3097 ctx->user_files = NULL;
3098 ctx->nr_user_files = 0;
3099 return 0;
3100}
3101
Jens Axboe6c271ce2019-01-10 11:22:30 -07003102static void io_sq_thread_stop(struct io_ring_ctx *ctx)
3103{
3104 if (ctx->sqo_thread) {
Jackie Liua4c0b3d2019-07-08 13:41:12 +08003105 wait_for_completion(&ctx->sqo_thread_started);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003106 /*
3107 * The park is a bit of a work-around, without it we get
3108 * warning spews on shutdown with SQPOLL set and affinity
3109 * set to a single CPU.
3110 */
Jens Axboe06058632019-04-13 09:26:03 -06003111 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003112 kthread_stop(ctx->sqo_thread);
3113 ctx->sqo_thread = NULL;
3114 }
3115}
3116
Jens Axboe6b063142019-01-10 22:13:58 -07003117static void io_finish_async(struct io_ring_ctx *ctx)
3118{
Jens Axboe54a91f32019-09-10 09:15:04 -06003119 int i;
3120
Jens Axboe6c271ce2019-01-10 11:22:30 -07003121 io_sq_thread_stop(ctx);
3122
Jens Axboe54a91f32019-09-10 09:15:04 -06003123 for (i = 0; i < ARRAY_SIZE(ctx->sqo_wq); i++) {
3124 if (ctx->sqo_wq[i]) {
3125 destroy_workqueue(ctx->sqo_wq[i]);
3126 ctx->sqo_wq[i] = NULL;
3127 }
Jens Axboe6b063142019-01-10 22:13:58 -07003128 }
3129}
3130
3131#if defined(CONFIG_UNIX)
3132static void io_destruct_skb(struct sk_buff *skb)
3133{
3134 struct io_ring_ctx *ctx = skb->sk->sk_user_data;
Jens Axboe8a997342019-10-09 14:40:13 -06003135 int i;
Jens Axboe6b063142019-01-10 22:13:58 -07003136
Jens Axboe8a997342019-10-09 14:40:13 -06003137 for (i = 0; i < ARRAY_SIZE(ctx->sqo_wq); i++)
3138 if (ctx->sqo_wq[i])
3139 flush_workqueue(ctx->sqo_wq[i]);
3140
Jens Axboe6b063142019-01-10 22:13:58 -07003141 unix_destruct_scm(skb);
3142}
3143
3144/*
3145 * Ensure the UNIX gc is aware of our file set, so we are certain that
3146 * the io_uring can be safely unregistered on process exit, even if we have
3147 * loops in the file referencing.
3148 */
3149static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
3150{
3151 struct sock *sk = ctx->ring_sock->sk;
3152 struct scm_fp_list *fpl;
3153 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06003154 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07003155
3156 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
3157 unsigned long inflight = ctx->user->unix_inflight + nr;
3158
3159 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
3160 return -EMFILE;
3161 }
3162
3163 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
3164 if (!fpl)
3165 return -ENOMEM;
3166
3167 skb = alloc_skb(0, GFP_KERNEL);
3168 if (!skb) {
3169 kfree(fpl);
3170 return -ENOMEM;
3171 }
3172
3173 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07003174
Jens Axboe08a45172019-10-03 08:11:03 -06003175 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07003176 fpl->user = get_uid(ctx->user);
3177 for (i = 0; i < nr; i++) {
Jens Axboe08a45172019-10-03 08:11:03 -06003178 if (!ctx->user_files[i + offset])
3179 continue;
3180 fpl->fp[nr_files] = get_file(ctx->user_files[i + offset]);
3181 unix_inflight(fpl->user, fpl->fp[nr_files]);
3182 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07003183 }
3184
Jens Axboe08a45172019-10-03 08:11:03 -06003185 if (nr_files) {
3186 fpl->max = SCM_MAX_FD;
3187 fpl->count = nr_files;
3188 UNIXCB(skb).fp = fpl;
3189 skb->destructor = io_destruct_skb;
3190 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
3191 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07003192
Jens Axboe08a45172019-10-03 08:11:03 -06003193 for (i = 0; i < nr_files; i++)
3194 fput(fpl->fp[i]);
3195 } else {
3196 kfree_skb(skb);
3197 kfree(fpl);
3198 }
Jens Axboe6b063142019-01-10 22:13:58 -07003199
3200 return 0;
3201}
3202
3203/*
3204 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
3205 * causes regular reference counting to break down. We rely on the UNIX
3206 * garbage collection to take care of this problem for us.
3207 */
3208static int io_sqe_files_scm(struct io_ring_ctx *ctx)
3209{
3210 unsigned left, total;
3211 int ret = 0;
3212
3213 total = 0;
3214 left = ctx->nr_user_files;
3215 while (left) {
3216 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07003217
3218 ret = __io_sqe_files_scm(ctx, this_files, total);
3219 if (ret)
3220 break;
3221 left -= this_files;
3222 total += this_files;
3223 }
3224
3225 if (!ret)
3226 return 0;
3227
3228 while (total < ctx->nr_user_files) {
Jens Axboe08a45172019-10-03 08:11:03 -06003229 if (ctx->user_files[total])
3230 fput(ctx->user_files[total]);
Jens Axboe6b063142019-01-10 22:13:58 -07003231 total++;
3232 }
3233
3234 return ret;
3235}
3236#else
3237static int io_sqe_files_scm(struct io_ring_ctx *ctx)
3238{
3239 return 0;
3240}
3241#endif
3242
3243static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
3244 unsigned nr_args)
3245{
3246 __s32 __user *fds = (__s32 __user *) arg;
3247 int fd, ret = 0;
3248 unsigned i;
3249
3250 if (ctx->user_files)
3251 return -EBUSY;
3252 if (!nr_args)
3253 return -EINVAL;
3254 if (nr_args > IORING_MAX_FIXED_FILES)
3255 return -EMFILE;
3256
3257 ctx->user_files = kcalloc(nr_args, sizeof(struct file *), GFP_KERNEL);
3258 if (!ctx->user_files)
3259 return -ENOMEM;
3260
Jens Axboe08a45172019-10-03 08:11:03 -06003261 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
Jens Axboe6b063142019-01-10 22:13:58 -07003262 ret = -EFAULT;
3263 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
3264 break;
Jens Axboe08a45172019-10-03 08:11:03 -06003265 /* allow sparse sets */
3266 if (fd == -1) {
3267 ret = 0;
3268 continue;
3269 }
Jens Axboe6b063142019-01-10 22:13:58 -07003270
3271 ctx->user_files[i] = fget(fd);
3272
3273 ret = -EBADF;
3274 if (!ctx->user_files[i])
3275 break;
3276 /*
3277 * Don't allow io_uring instances to be registered. If UNIX
3278 * isn't enabled, then this causes a reference cycle and this
3279 * instance can never get freed. If UNIX is enabled we'll
3280 * handle it just fine, but there's still no point in allowing
3281 * a ring fd as it doesn't support regular read/write anyway.
3282 */
3283 if (ctx->user_files[i]->f_op == &io_uring_fops) {
3284 fput(ctx->user_files[i]);
3285 break;
3286 }
Jens Axboe6b063142019-01-10 22:13:58 -07003287 ret = 0;
3288 }
3289
3290 if (ret) {
3291 for (i = 0; i < ctx->nr_user_files; i++)
Jens Axboe08a45172019-10-03 08:11:03 -06003292 if (ctx->user_files[i])
3293 fput(ctx->user_files[i]);
Jens Axboe6b063142019-01-10 22:13:58 -07003294
3295 kfree(ctx->user_files);
Jens Axboe25adf502019-04-03 09:52:40 -06003296 ctx->user_files = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003297 ctx->nr_user_files = 0;
3298 return ret;
3299 }
3300
3301 ret = io_sqe_files_scm(ctx);
3302 if (ret)
3303 io_sqe_files_unregister(ctx);
3304
3305 return ret;
3306}
3307
Jens Axboec3a31e62019-10-03 13:59:56 -06003308static void io_sqe_file_unregister(struct io_ring_ctx *ctx, int index)
3309{
3310#if defined(CONFIG_UNIX)
3311 struct file *file = ctx->user_files[index];
3312 struct sock *sock = ctx->ring_sock->sk;
3313 struct sk_buff_head list, *head = &sock->sk_receive_queue;
3314 struct sk_buff *skb;
3315 int i;
3316
3317 __skb_queue_head_init(&list);
3318
3319 /*
3320 * Find the skb that holds this file in its SCM_RIGHTS. When found,
3321 * remove this entry and rearrange the file array.
3322 */
3323 skb = skb_dequeue(head);
3324 while (skb) {
3325 struct scm_fp_list *fp;
3326
3327 fp = UNIXCB(skb).fp;
3328 for (i = 0; i < fp->count; i++) {
3329 int left;
3330
3331 if (fp->fp[i] != file)
3332 continue;
3333
3334 unix_notinflight(fp->user, fp->fp[i]);
3335 left = fp->count - 1 - i;
3336 if (left) {
3337 memmove(&fp->fp[i], &fp->fp[i + 1],
3338 left * sizeof(struct file *));
3339 }
3340 fp->count--;
3341 if (!fp->count) {
3342 kfree_skb(skb);
3343 skb = NULL;
3344 } else {
3345 __skb_queue_tail(&list, skb);
3346 }
3347 fput(file);
3348 file = NULL;
3349 break;
3350 }
3351
3352 if (!file)
3353 break;
3354
3355 __skb_queue_tail(&list, skb);
3356
3357 skb = skb_dequeue(head);
3358 }
3359
3360 if (skb_peek(&list)) {
3361 spin_lock_irq(&head->lock);
3362 while ((skb = __skb_dequeue(&list)) != NULL)
3363 __skb_queue_tail(head, skb);
3364 spin_unlock_irq(&head->lock);
3365 }
3366#else
3367 fput(ctx->user_files[index]);
3368#endif
3369}
3370
3371static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
3372 int index)
3373{
3374#if defined(CONFIG_UNIX)
3375 struct sock *sock = ctx->ring_sock->sk;
3376 struct sk_buff_head *head = &sock->sk_receive_queue;
3377 struct sk_buff *skb;
3378
3379 /*
3380 * See if we can merge this file into an existing skb SCM_RIGHTS
3381 * file set. If there's no room, fall back to allocating a new skb
3382 * and filling it in.
3383 */
3384 spin_lock_irq(&head->lock);
3385 skb = skb_peek(head);
3386 if (skb) {
3387 struct scm_fp_list *fpl = UNIXCB(skb).fp;
3388
3389 if (fpl->count < SCM_MAX_FD) {
3390 __skb_unlink(skb, head);
3391 spin_unlock_irq(&head->lock);
3392 fpl->fp[fpl->count] = get_file(file);
3393 unix_inflight(fpl->user, fpl->fp[fpl->count]);
3394 fpl->count++;
3395 spin_lock_irq(&head->lock);
3396 __skb_queue_head(head, skb);
3397 } else {
3398 skb = NULL;
3399 }
3400 }
3401 spin_unlock_irq(&head->lock);
3402
3403 if (skb) {
3404 fput(file);
3405 return 0;
3406 }
3407
3408 return __io_sqe_files_scm(ctx, 1, index);
3409#else
3410 return 0;
3411#endif
3412}
3413
3414static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
3415 unsigned nr_args)
3416{
3417 struct io_uring_files_update up;
3418 __s32 __user *fds;
3419 int fd, i, err;
3420 __u32 done;
3421
3422 if (!ctx->user_files)
3423 return -ENXIO;
3424 if (!nr_args)
3425 return -EINVAL;
3426 if (copy_from_user(&up, arg, sizeof(up)))
3427 return -EFAULT;
3428 if (check_add_overflow(up.offset, nr_args, &done))
3429 return -EOVERFLOW;
3430 if (done > ctx->nr_user_files)
3431 return -EINVAL;
3432
3433 done = 0;
3434 fds = (__s32 __user *) up.fds;
3435 while (nr_args) {
3436 err = 0;
3437 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
3438 err = -EFAULT;
3439 break;
3440 }
3441 i = array_index_nospec(up.offset, ctx->nr_user_files);
3442 if (ctx->user_files[i]) {
3443 io_sqe_file_unregister(ctx, i);
3444 ctx->user_files[i] = NULL;
3445 }
3446 if (fd != -1) {
3447 struct file *file;
3448
3449 file = fget(fd);
3450 if (!file) {
3451 err = -EBADF;
3452 break;
3453 }
3454 /*
3455 * Don't allow io_uring instances to be registered. If
3456 * UNIX isn't enabled, then this causes a reference
3457 * cycle and this instance can never get freed. If UNIX
3458 * is enabled we'll handle it just fine, but there's
3459 * still no point in allowing a ring fd as it doesn't
3460 * support regular read/write anyway.
3461 */
3462 if (file->f_op == &io_uring_fops) {
3463 fput(file);
3464 err = -EBADF;
3465 break;
3466 }
3467 ctx->user_files[i] = file;
3468 err = io_sqe_file_register(ctx, file, i);
3469 if (err)
3470 break;
3471 }
3472 nr_args--;
3473 done++;
3474 up.offset++;
3475 }
3476
3477 return done ? done : err;
3478}
3479
Jens Axboe6c271ce2019-01-10 11:22:30 -07003480static int io_sq_offload_start(struct io_ring_ctx *ctx,
3481 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003482{
3483 int ret;
3484
Jens Axboe6c271ce2019-01-10 11:22:30 -07003485 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003486 mmgrab(current->mm);
3487 ctx->sqo_mm = current->mm;
3488
Jens Axboe6c271ce2019-01-10 11:22:30 -07003489 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06003490 ret = -EPERM;
3491 if (!capable(CAP_SYS_ADMIN))
3492 goto err;
3493
Jens Axboe917257d2019-04-13 09:28:55 -06003494 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
3495 if (!ctx->sq_thread_idle)
3496 ctx->sq_thread_idle = HZ;
3497
Jens Axboe6c271ce2019-01-10 11:22:30 -07003498 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06003499 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003500
Jens Axboe917257d2019-04-13 09:28:55 -06003501 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06003502 if (cpu >= nr_cpu_ids)
3503 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08003504 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06003505 goto err;
3506
Jens Axboe6c271ce2019-01-10 11:22:30 -07003507 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
3508 ctx, cpu,
3509 "io_uring-sq");
3510 } else {
3511 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
3512 "io_uring-sq");
3513 }
3514 if (IS_ERR(ctx->sqo_thread)) {
3515 ret = PTR_ERR(ctx->sqo_thread);
3516 ctx->sqo_thread = NULL;
3517 goto err;
3518 }
3519 wake_up_process(ctx->sqo_thread);
3520 } else if (p->flags & IORING_SETUP_SQ_AFF) {
3521 /* Can't have SQ_AFF without SQPOLL */
3522 ret = -EINVAL;
3523 goto err;
3524 }
3525
Jens Axboe2b188cc2019-01-07 10:46:33 -07003526 /* Do QD, or 2 * CPUS, whatever is smallest */
Jens Axboe54a91f32019-09-10 09:15:04 -06003527 ctx->sqo_wq[0] = alloc_workqueue("io_ring-wq",
3528 WQ_UNBOUND | WQ_FREEZABLE,
Jens Axboe2b188cc2019-01-07 10:46:33 -07003529 min(ctx->sq_entries - 1, 2 * num_online_cpus()));
Jens Axboe54a91f32019-09-10 09:15:04 -06003530 if (!ctx->sqo_wq[0]) {
3531 ret = -ENOMEM;
3532 goto err;
3533 }
3534
3535 /*
3536 * This is for buffered writes, where we want to limit the parallelism
3537 * due to file locking in file systems. As "normal" buffered writes
3538 * should parellelize on writeout quite nicely, limit us to having 2
3539 * pending. This avoids massive contention on the inode when doing
3540 * buffered async writes.
3541 */
3542 ctx->sqo_wq[1] = alloc_workqueue("io_ring-write-wq",
3543 WQ_UNBOUND | WQ_FREEZABLE, 2);
3544 if (!ctx->sqo_wq[1]) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07003545 ret = -ENOMEM;
3546 goto err;
3547 }
3548
3549 return 0;
3550err:
Jens Axboe54a91f32019-09-10 09:15:04 -06003551 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003552 mmdrop(ctx->sqo_mm);
3553 ctx->sqo_mm = NULL;
3554 return ret;
3555}
3556
3557static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
3558{
3559 atomic_long_sub(nr_pages, &user->locked_vm);
3560}
3561
3562static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
3563{
3564 unsigned long page_limit, cur_pages, new_pages;
3565
3566 /* Don't allow more pages than we can safely lock */
3567 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
3568
3569 do {
3570 cur_pages = atomic_long_read(&user->locked_vm);
3571 new_pages = cur_pages + nr_pages;
3572 if (new_pages > page_limit)
3573 return -ENOMEM;
3574 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
3575 new_pages) != cur_pages);
3576
3577 return 0;
3578}
3579
3580static void io_mem_free(void *ptr)
3581{
Mark Rutland52e04ef2019-04-30 17:30:21 +01003582 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003583
Mark Rutland52e04ef2019-04-30 17:30:21 +01003584 if (!ptr)
3585 return;
3586
3587 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003588 if (put_page_testzero(page))
3589 free_compound_page(page);
3590}
3591
3592static void *io_mem_alloc(size_t size)
3593{
3594 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
3595 __GFP_NORETRY;
3596
3597 return (void *) __get_free_pages(gfp_flags, get_order(size));
3598}
3599
Hristo Venev75b28af2019-08-26 17:23:46 +00003600static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
3601 size_t *sq_offset)
3602{
3603 struct io_rings *rings;
3604 size_t off, sq_array_size;
3605
3606 off = struct_size(rings, cqes, cq_entries);
3607 if (off == SIZE_MAX)
3608 return SIZE_MAX;
3609
3610#ifdef CONFIG_SMP
3611 off = ALIGN(off, SMP_CACHE_BYTES);
3612 if (off == 0)
3613 return SIZE_MAX;
3614#endif
3615
3616 sq_array_size = array_size(sizeof(u32), sq_entries);
3617 if (sq_array_size == SIZE_MAX)
3618 return SIZE_MAX;
3619
3620 if (check_add_overflow(off, sq_array_size, &off))
3621 return SIZE_MAX;
3622
3623 if (sq_offset)
3624 *sq_offset = off;
3625
3626 return off;
3627}
3628
Jens Axboe2b188cc2019-01-07 10:46:33 -07003629static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
3630{
Hristo Venev75b28af2019-08-26 17:23:46 +00003631 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003632
Hristo Venev75b28af2019-08-26 17:23:46 +00003633 pages = (size_t)1 << get_order(
3634 rings_size(sq_entries, cq_entries, NULL));
3635 pages += (size_t)1 << get_order(
3636 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07003637
Hristo Venev75b28af2019-08-26 17:23:46 +00003638 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003639}
3640
Jens Axboeedafcce2019-01-09 09:16:05 -07003641static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
3642{
3643 int i, j;
3644
3645 if (!ctx->user_bufs)
3646 return -ENXIO;
3647
3648 for (i = 0; i < ctx->nr_user_bufs; i++) {
3649 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
3650
3651 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbard27c4d3a2019-08-04 19:32:06 -07003652 put_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07003653
3654 if (ctx->account_mem)
3655 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003656 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07003657 imu->nr_bvecs = 0;
3658 }
3659
3660 kfree(ctx->user_bufs);
3661 ctx->user_bufs = NULL;
3662 ctx->nr_user_bufs = 0;
3663 return 0;
3664}
3665
3666static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
3667 void __user *arg, unsigned index)
3668{
3669 struct iovec __user *src;
3670
3671#ifdef CONFIG_COMPAT
3672 if (ctx->compat) {
3673 struct compat_iovec __user *ciovs;
3674 struct compat_iovec ciov;
3675
3676 ciovs = (struct compat_iovec __user *) arg;
3677 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
3678 return -EFAULT;
3679
3680 dst->iov_base = (void __user *) (unsigned long) ciov.iov_base;
3681 dst->iov_len = ciov.iov_len;
3682 return 0;
3683 }
3684#endif
3685 src = (struct iovec __user *) arg;
3686 if (copy_from_user(dst, &src[index], sizeof(*dst)))
3687 return -EFAULT;
3688 return 0;
3689}
3690
3691static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
3692 unsigned nr_args)
3693{
3694 struct vm_area_struct **vmas = NULL;
3695 struct page **pages = NULL;
3696 int i, j, got_pages = 0;
3697 int ret = -EINVAL;
3698
3699 if (ctx->user_bufs)
3700 return -EBUSY;
3701 if (!nr_args || nr_args > UIO_MAXIOV)
3702 return -EINVAL;
3703
3704 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
3705 GFP_KERNEL);
3706 if (!ctx->user_bufs)
3707 return -ENOMEM;
3708
3709 for (i = 0; i < nr_args; i++) {
3710 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
3711 unsigned long off, start, end, ubuf;
3712 int pret, nr_pages;
3713 struct iovec iov;
3714 size_t size;
3715
3716 ret = io_copy_iov(ctx, &iov, arg, i);
3717 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03003718 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07003719
3720 /*
3721 * Don't impose further limits on the size and buffer
3722 * constraints here, we'll -EINVAL later when IO is
3723 * submitted if they are wrong.
3724 */
3725 ret = -EFAULT;
3726 if (!iov.iov_base || !iov.iov_len)
3727 goto err;
3728
3729 /* arbitrary limit, but we need something */
3730 if (iov.iov_len > SZ_1G)
3731 goto err;
3732
3733 ubuf = (unsigned long) iov.iov_base;
3734 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
3735 start = ubuf >> PAGE_SHIFT;
3736 nr_pages = end - start;
3737
3738 if (ctx->account_mem) {
3739 ret = io_account_mem(ctx->user, nr_pages);
3740 if (ret)
3741 goto err;
3742 }
3743
3744 ret = 0;
3745 if (!pages || nr_pages > got_pages) {
3746 kfree(vmas);
3747 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003748 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07003749 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003750 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07003751 sizeof(struct vm_area_struct *),
3752 GFP_KERNEL);
3753 if (!pages || !vmas) {
3754 ret = -ENOMEM;
3755 if (ctx->account_mem)
3756 io_unaccount_mem(ctx->user, nr_pages);
3757 goto err;
3758 }
3759 got_pages = nr_pages;
3760 }
3761
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003762 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07003763 GFP_KERNEL);
3764 ret = -ENOMEM;
3765 if (!imu->bvec) {
3766 if (ctx->account_mem)
3767 io_unaccount_mem(ctx->user, nr_pages);
3768 goto err;
3769 }
3770
3771 ret = 0;
3772 down_read(&current->mm->mmap_sem);
Ira Weiny932f4a62019-05-13 17:17:03 -07003773 pret = get_user_pages(ubuf, nr_pages,
3774 FOLL_WRITE | FOLL_LONGTERM,
3775 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07003776 if (pret == nr_pages) {
3777 /* don't support file backed memory */
3778 for (j = 0; j < nr_pages; j++) {
3779 struct vm_area_struct *vma = vmas[j];
3780
3781 if (vma->vm_file &&
3782 !is_file_hugepages(vma->vm_file)) {
3783 ret = -EOPNOTSUPP;
3784 break;
3785 }
3786 }
3787 } else {
3788 ret = pret < 0 ? pret : -EFAULT;
3789 }
3790 up_read(&current->mm->mmap_sem);
3791 if (ret) {
3792 /*
3793 * if we did partial map, or found file backed vmas,
3794 * release any pages we did get
3795 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07003796 if (pret > 0)
3797 put_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07003798 if (ctx->account_mem)
3799 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003800 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07003801 goto err;
3802 }
3803
3804 off = ubuf & ~PAGE_MASK;
3805 size = iov.iov_len;
3806 for (j = 0; j < nr_pages; j++) {
3807 size_t vec_len;
3808
3809 vec_len = min_t(size_t, size, PAGE_SIZE - off);
3810 imu->bvec[j].bv_page = pages[j];
3811 imu->bvec[j].bv_len = vec_len;
3812 imu->bvec[j].bv_offset = off;
3813 off = 0;
3814 size -= vec_len;
3815 }
3816 /* store original address for later verification */
3817 imu->ubuf = ubuf;
3818 imu->len = iov.iov_len;
3819 imu->nr_bvecs = nr_pages;
3820
3821 ctx->nr_user_bufs++;
3822 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003823 kvfree(pages);
3824 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07003825 return 0;
3826err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003827 kvfree(pages);
3828 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07003829 io_sqe_buffer_unregister(ctx);
3830 return ret;
3831}
3832
Jens Axboe9b402842019-04-11 11:45:41 -06003833static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
3834{
3835 __s32 __user *fds = arg;
3836 int fd;
3837
3838 if (ctx->cq_ev_fd)
3839 return -EBUSY;
3840
3841 if (copy_from_user(&fd, fds, sizeof(*fds)))
3842 return -EFAULT;
3843
3844 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
3845 if (IS_ERR(ctx->cq_ev_fd)) {
3846 int ret = PTR_ERR(ctx->cq_ev_fd);
3847 ctx->cq_ev_fd = NULL;
3848 return ret;
3849 }
3850
3851 return 0;
3852}
3853
3854static int io_eventfd_unregister(struct io_ring_ctx *ctx)
3855{
3856 if (ctx->cq_ev_fd) {
3857 eventfd_ctx_put(ctx->cq_ev_fd);
3858 ctx->cq_ev_fd = NULL;
3859 return 0;
3860 }
3861
3862 return -ENXIO;
3863}
3864
Jens Axboe2b188cc2019-01-07 10:46:33 -07003865static void io_ring_ctx_free(struct io_ring_ctx *ctx)
3866{
Jens Axboe6b063142019-01-10 22:13:58 -07003867 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003868 if (ctx->sqo_mm)
3869 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07003870
3871 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07003872 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07003873 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06003874 io_eventfd_unregister(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07003875
Jens Axboe2b188cc2019-01-07 10:46:33 -07003876#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07003877 if (ctx->ring_sock) {
3878 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07003879 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07003880 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003881#endif
3882
Hristo Venev75b28af2019-08-26 17:23:46 +00003883 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003884 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003885
3886 percpu_ref_exit(&ctx->refs);
3887 if (ctx->account_mem)
3888 io_unaccount_mem(ctx->user,
3889 ring_pages(ctx->sq_entries, ctx->cq_entries));
3890 free_uid(ctx->user);
3891 kfree(ctx);
3892}
3893
3894static __poll_t io_uring_poll(struct file *file, poll_table *wait)
3895{
3896 struct io_ring_ctx *ctx = file->private_data;
3897 __poll_t mask = 0;
3898
3899 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02003900 /*
3901 * synchronizes with barrier from wq_has_sleeper call in
3902 * io_commit_cqring
3903 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07003904 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00003905 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
3906 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003907 mask |= EPOLLOUT | EPOLLWRNORM;
yangerkundaa5de52019-09-24 20:53:34 +08003908 if (READ_ONCE(ctx->rings->cq.head) != ctx->cached_cq_tail)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003909 mask |= EPOLLIN | EPOLLRDNORM;
3910
3911 return mask;
3912}
3913
3914static int io_uring_fasync(int fd, struct file *file, int on)
3915{
3916 struct io_ring_ctx *ctx = file->private_data;
3917
3918 return fasync_helper(fd, file, on, &ctx->cq_fasync);
3919}
3920
3921static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
3922{
3923 mutex_lock(&ctx->uring_lock);
3924 percpu_ref_kill(&ctx->refs);
3925 mutex_unlock(&ctx->uring_lock);
3926
Jens Axboe5262f562019-09-17 12:26:57 -06003927 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003928 io_poll_remove_all(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07003929 io_iopoll_reap_events(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003930 wait_for_completion(&ctx->ctx_done);
3931 io_ring_ctx_free(ctx);
3932}
3933
3934static int io_uring_release(struct inode *inode, struct file *file)
3935{
3936 struct io_ring_ctx *ctx = file->private_data;
3937
3938 file->private_data = NULL;
3939 io_ring_ctx_wait_and_kill(ctx);
3940 return 0;
3941}
3942
3943static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
3944{
3945 loff_t offset = (loff_t) vma->vm_pgoff << PAGE_SHIFT;
3946 unsigned long sz = vma->vm_end - vma->vm_start;
3947 struct io_ring_ctx *ctx = file->private_data;
3948 unsigned long pfn;
3949 struct page *page;
3950 void *ptr;
3951
3952 switch (offset) {
3953 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00003954 case IORING_OFF_CQ_RING:
3955 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003956 break;
3957 case IORING_OFF_SQES:
3958 ptr = ctx->sq_sqes;
3959 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003960 default:
3961 return -EINVAL;
3962 }
3963
3964 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07003965 if (sz > page_size(page))
Jens Axboe2b188cc2019-01-07 10:46:33 -07003966 return -EINVAL;
3967
3968 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
3969 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
3970}
3971
3972SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
3973 u32, min_complete, u32, flags, const sigset_t __user *, sig,
3974 size_t, sigsz)
3975{
3976 struct io_ring_ctx *ctx;
3977 long ret = -EBADF;
3978 int submitted = 0;
3979 struct fd f;
3980
Jens Axboe6c271ce2019-01-10 11:22:30 -07003981 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07003982 return -EINVAL;
3983
3984 f = fdget(fd);
3985 if (!f.file)
3986 return -EBADF;
3987
3988 ret = -EOPNOTSUPP;
3989 if (f.file->f_op != &io_uring_fops)
3990 goto out_fput;
3991
3992 ret = -ENXIO;
3993 ctx = f.file->private_data;
3994 if (!percpu_ref_tryget(&ctx->refs))
3995 goto out_fput;
3996
Jens Axboe6c271ce2019-01-10 11:22:30 -07003997 /*
3998 * For SQ polling, the thread will do all submissions and completions.
3999 * Just return the requested submit count, and wake the thread if
4000 * we were asked to.
4001 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06004002 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004003 if (ctx->flags & IORING_SETUP_SQPOLL) {
4004 if (flags & IORING_ENTER_SQ_WAKEUP)
4005 wake_up(&ctx->sqo_wait);
4006 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06004007 } else if (to_submit) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07004008 to_submit = min(to_submit, ctx->sq_entries);
4009
4010 mutex_lock(&ctx->uring_lock);
Jens Axboebc808bc2019-10-22 13:14:37 -06004011 submitted = io_ring_submit(ctx, to_submit);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004012 mutex_unlock(&ctx->uring_lock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004013 }
4014 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07004015 unsigned nr_events = 0;
4016
Jens Axboe2b188cc2019-01-07 10:46:33 -07004017 min_complete = min(min_complete, ctx->cq_entries);
4018
Jens Axboedef596e2019-01-09 08:59:42 -07004019 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07004020 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07004021 } else {
4022 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
4023 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004024 }
4025
Pavel Begunkov6805b322019-10-08 02:18:42 +03004026 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004027out_fput:
4028 fdput(f);
4029 return submitted ? submitted : ret;
4030}
4031
4032static const struct file_operations io_uring_fops = {
4033 .release = io_uring_release,
4034 .mmap = io_uring_mmap,
4035 .poll = io_uring_poll,
4036 .fasync = io_uring_fasync,
4037};
4038
4039static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
4040 struct io_uring_params *p)
4041{
Hristo Venev75b28af2019-08-26 17:23:46 +00004042 struct io_rings *rings;
4043 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004044
Hristo Venev75b28af2019-08-26 17:23:46 +00004045 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
4046 if (size == SIZE_MAX)
4047 return -EOVERFLOW;
4048
4049 rings = io_mem_alloc(size);
4050 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004051 return -ENOMEM;
4052
Hristo Venev75b28af2019-08-26 17:23:46 +00004053 ctx->rings = rings;
4054 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
4055 rings->sq_ring_mask = p->sq_entries - 1;
4056 rings->cq_ring_mask = p->cq_entries - 1;
4057 rings->sq_ring_entries = p->sq_entries;
4058 rings->cq_ring_entries = p->cq_entries;
4059 ctx->sq_mask = rings->sq_ring_mask;
4060 ctx->cq_mask = rings->cq_ring_mask;
4061 ctx->sq_entries = rings->sq_ring_entries;
4062 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004063
4064 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
4065 if (size == SIZE_MAX)
4066 return -EOVERFLOW;
4067
4068 ctx->sq_sqes = io_mem_alloc(size);
Mark Rutland52e04ef2019-04-30 17:30:21 +01004069 if (!ctx->sq_sqes)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004070 return -ENOMEM;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004071
Jens Axboe2b188cc2019-01-07 10:46:33 -07004072 return 0;
4073}
4074
4075/*
4076 * Allocate an anonymous fd, this is what constitutes the application
4077 * visible backing of an io_uring instance. The application mmaps this
4078 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
4079 * we have to tie this fd to a socket for file garbage collection purposes.
4080 */
4081static int io_uring_get_fd(struct io_ring_ctx *ctx)
4082{
4083 struct file *file;
4084 int ret;
4085
4086#if defined(CONFIG_UNIX)
4087 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
4088 &ctx->ring_sock);
4089 if (ret)
4090 return ret;
4091#endif
4092
4093 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
4094 if (ret < 0)
4095 goto err;
4096
4097 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
4098 O_RDWR | O_CLOEXEC);
4099 if (IS_ERR(file)) {
4100 put_unused_fd(ret);
4101 ret = PTR_ERR(file);
4102 goto err;
4103 }
4104
4105#if defined(CONFIG_UNIX)
4106 ctx->ring_sock->file = file;
Jens Axboe6b063142019-01-10 22:13:58 -07004107 ctx->ring_sock->sk->sk_user_data = ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004108#endif
4109 fd_install(ret, file);
4110 return ret;
4111err:
4112#if defined(CONFIG_UNIX)
4113 sock_release(ctx->ring_sock);
4114 ctx->ring_sock = NULL;
4115#endif
4116 return ret;
4117}
4118
4119static int io_uring_create(unsigned entries, struct io_uring_params *p)
4120{
4121 struct user_struct *user = NULL;
4122 struct io_ring_ctx *ctx;
4123 bool account_mem;
4124 int ret;
4125
4126 if (!entries || entries > IORING_MAX_ENTRIES)
4127 return -EINVAL;
4128
4129 /*
4130 * Use twice as many entries for the CQ ring. It's possible for the
4131 * application to drive a higher depth than the size of the SQ ring,
4132 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06004133 * some flexibility in overcommitting a bit. If the application has
4134 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
4135 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07004136 */
4137 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06004138 if (p->flags & IORING_SETUP_CQSIZE) {
4139 /*
4140 * If IORING_SETUP_CQSIZE is set, we do the same roundup
4141 * to a power-of-two, if it isn't already. We do NOT impose
4142 * any cq vs sq ring sizing.
4143 */
4144 if (p->cq_entries < p->sq_entries || p->cq_entries > IORING_MAX_CQ_ENTRIES)
4145 return -EINVAL;
4146 p->cq_entries = roundup_pow_of_two(p->cq_entries);
4147 } else {
4148 p->cq_entries = 2 * p->sq_entries;
4149 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004150
4151 user = get_uid(current_user());
4152 account_mem = !capable(CAP_IPC_LOCK);
4153
4154 if (account_mem) {
4155 ret = io_account_mem(user,
4156 ring_pages(p->sq_entries, p->cq_entries));
4157 if (ret) {
4158 free_uid(user);
4159 return ret;
4160 }
4161 }
4162
4163 ctx = io_ring_ctx_alloc(p);
4164 if (!ctx) {
4165 if (account_mem)
4166 io_unaccount_mem(user, ring_pages(p->sq_entries,
4167 p->cq_entries));
4168 free_uid(user);
4169 return -ENOMEM;
4170 }
4171 ctx->compat = in_compat_syscall();
4172 ctx->account_mem = account_mem;
4173 ctx->user = user;
4174
4175 ret = io_allocate_scq_urings(ctx, p);
4176 if (ret)
4177 goto err;
4178
Jens Axboe6c271ce2019-01-10 11:22:30 -07004179 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004180 if (ret)
4181 goto err;
4182
Jens Axboe2b188cc2019-01-07 10:46:33 -07004183 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00004184 p->sq_off.head = offsetof(struct io_rings, sq.head);
4185 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
4186 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
4187 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
4188 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
4189 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
4190 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004191
4192 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00004193 p->cq_off.head = offsetof(struct io_rings, cq.head);
4194 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
4195 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
4196 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
4197 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
4198 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06004199
Jens Axboe044c1ab2019-10-28 09:15:33 -06004200 /*
4201 * Install ring fd as the very last thing, so we don't risk someone
4202 * having closed it before we finish setup
4203 */
4204 ret = io_uring_get_fd(ctx);
4205 if (ret < 0)
4206 goto err;
4207
Jens Axboeac90f242019-09-06 10:26:21 -06004208 p->features = IORING_FEAT_SINGLE_MMAP;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004209 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004210 return ret;
4211err:
4212 io_ring_ctx_wait_and_kill(ctx);
4213 return ret;
4214}
4215
4216/*
4217 * Sets up an aio uring context, and returns the fd. Applications asks for a
4218 * ring size, we return the actual sq/cq ring sizes (among other things) in the
4219 * params structure passed in.
4220 */
4221static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
4222{
4223 struct io_uring_params p;
4224 long ret;
4225 int i;
4226
4227 if (copy_from_user(&p, params, sizeof(p)))
4228 return -EFAULT;
4229 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
4230 if (p.resv[i])
4231 return -EINVAL;
4232 }
4233
Jens Axboe6c271ce2019-01-10 11:22:30 -07004234 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe33a107f2019-10-04 12:10:03 -06004235 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE))
Jens Axboe2b188cc2019-01-07 10:46:33 -07004236 return -EINVAL;
4237
4238 ret = io_uring_create(entries, &p);
4239 if (ret < 0)
4240 return ret;
4241
4242 if (copy_to_user(params, &p, sizeof(p)))
4243 return -EFAULT;
4244
4245 return ret;
4246}
4247
4248SYSCALL_DEFINE2(io_uring_setup, u32, entries,
4249 struct io_uring_params __user *, params)
4250{
4251 return io_uring_setup(entries, params);
4252}
4253
Jens Axboeedafcce2019-01-09 09:16:05 -07004254static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
4255 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06004256 __releases(ctx->uring_lock)
4257 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07004258{
4259 int ret;
4260
Jens Axboe35fa71a2019-04-22 10:23:23 -06004261 /*
4262 * We're inside the ring mutex, if the ref is already dying, then
4263 * someone else killed the ctx or is already going through
4264 * io_uring_register().
4265 */
4266 if (percpu_ref_is_dying(&ctx->refs))
4267 return -ENXIO;
4268
Jens Axboeedafcce2019-01-09 09:16:05 -07004269 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06004270
4271 /*
4272 * Drop uring mutex before waiting for references to exit. If another
4273 * thread is currently inside io_uring_enter() it might need to grab
4274 * the uring_lock to make progress. If we hold it here across the drain
4275 * wait, then we can deadlock. It's safe to drop the mutex here, since
4276 * no new references will come in after we've killed the percpu ref.
4277 */
4278 mutex_unlock(&ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07004279 wait_for_completion(&ctx->ctx_done);
Jens Axboeb19062a2019-04-15 10:49:38 -06004280 mutex_lock(&ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07004281
4282 switch (opcode) {
4283 case IORING_REGISTER_BUFFERS:
4284 ret = io_sqe_buffer_register(ctx, arg, nr_args);
4285 break;
4286 case IORING_UNREGISTER_BUFFERS:
4287 ret = -EINVAL;
4288 if (arg || nr_args)
4289 break;
4290 ret = io_sqe_buffer_unregister(ctx);
4291 break;
Jens Axboe6b063142019-01-10 22:13:58 -07004292 case IORING_REGISTER_FILES:
4293 ret = io_sqe_files_register(ctx, arg, nr_args);
4294 break;
4295 case IORING_UNREGISTER_FILES:
4296 ret = -EINVAL;
4297 if (arg || nr_args)
4298 break;
4299 ret = io_sqe_files_unregister(ctx);
4300 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06004301 case IORING_REGISTER_FILES_UPDATE:
4302 ret = io_sqe_files_update(ctx, arg, nr_args);
4303 break;
Jens Axboe9b402842019-04-11 11:45:41 -06004304 case IORING_REGISTER_EVENTFD:
4305 ret = -EINVAL;
4306 if (nr_args != 1)
4307 break;
4308 ret = io_eventfd_register(ctx, arg);
4309 break;
4310 case IORING_UNREGISTER_EVENTFD:
4311 ret = -EINVAL;
4312 if (arg || nr_args)
4313 break;
4314 ret = io_eventfd_unregister(ctx);
4315 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07004316 default:
4317 ret = -EINVAL;
4318 break;
4319 }
4320
4321 /* bring the ctx back to life */
4322 reinit_completion(&ctx->ctx_done);
4323 percpu_ref_reinit(&ctx->refs);
4324 return ret;
4325}
4326
4327SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
4328 void __user *, arg, unsigned int, nr_args)
4329{
4330 struct io_ring_ctx *ctx;
4331 long ret = -EBADF;
4332 struct fd f;
4333
4334 f = fdget(fd);
4335 if (!f.file)
4336 return -EBADF;
4337
4338 ret = -EOPNOTSUPP;
4339 if (f.file->f_op != &io_uring_fops)
4340 goto out_fput;
4341
4342 ctx = f.file->private_data;
4343
4344 mutex_lock(&ctx->uring_lock);
4345 ret = __io_uring_register(ctx, opcode, arg, nr_args);
4346 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004347 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
4348 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07004349out_fput:
4350 fdput(f);
4351 return ret;
4352}
4353
Jens Axboe2b188cc2019-01-07 10:46:33 -07004354static int __init io_uring_init(void)
4355{
4356 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
4357 return 0;
4358};
4359__initcall(io_uring_init);