blob: f9eff8f62ddb95aba07cd05203dd44a0f2bd0143 [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,
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03002729 struct mm_struct **mm)
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;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03002736 bool mm_fault = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002737
2738 if (nr > IO_PLUG_THRESHOLD) {
2739 io_submit_state_start(&state, ctx, nr);
2740 statep = &state;
2741 }
2742
2743 for (i = 0; i < nr; i++) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002744 struct sqe_submit s;
2745
2746 if (!io_get_sqring(ctx, &s))
2747 break;
2748
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03002749 if (io_sqe_needs_user(s.sqe) && !*mm) {
2750 mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm);
2751 if (!mm_fault) {
2752 use_mm(ctx->sqo_mm);
2753 *mm = ctx->sqo_mm;
2754 }
2755 }
2756
Jens Axboe9e645e112019-05-10 16:07:28 -06002757 /*
2758 * If previous wasn't linked and we have a linked command,
2759 * that's the end of the chain. Submit the previous link.
2760 */
2761 if (!prev_was_link && link) {
Jens Axboebc808bc2019-10-22 13:14:37 -06002762 io_queue_link_head(ctx, link, &link->submit, shadow_req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002763 link = NULL;
Jackie Liu5f5ad9c2019-09-18 10:37:53 +08002764 shadow_req = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002765 }
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002766 prev_was_link = (s.sqe->flags & IOSQE_IO_LINK) != 0;
Jens Axboe9e645e112019-05-10 16:07:28 -06002767
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002768 if (link && (s.sqe->flags & IOSQE_IO_DRAIN)) {
Jackie Liu4fe2c962019-09-09 20:50:40 +08002769 if (!shadow_req) {
2770 shadow_req = io_get_req(ctx, NULL);
Jackie Liua1041c22019-09-18 17:25:52 +08002771 if (unlikely(!shadow_req))
2772 goto out;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002773 shadow_req->flags |= (REQ_F_IO_DRAIN | REQ_F_SHADOW_DRAIN);
2774 refcount_dec(&shadow_req->refs);
2775 }
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002776 shadow_req->sequence = s.sequence;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002777 }
2778
Jackie Liua1041c22019-09-18 17:25:52 +08002779out:
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03002780 s.has_user = *mm != NULL;
2781 s.in_async = true;
2782 s.needs_fixed_file = true;
2783 trace_io_uring_submit_sqe(ctx, true, true);
2784 io_submit_sqe(ctx, &s, statep, &link);
2785 submitted++;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002786 }
2787
Jens Axboe9e645e112019-05-10 16:07:28 -06002788 if (link)
Jens Axboebc808bc2019-10-22 13:14:37 -06002789 io_queue_link_head(ctx, link, &link->submit, shadow_req);
Jens Axboe6c271ce2019-01-10 11:22:30 -07002790 if (statep)
2791 io_submit_state_end(&state);
2792
2793 return submitted;
2794}
2795
2796static int io_sq_thread(void *data)
2797{
Jens Axboe6c271ce2019-01-10 11:22:30 -07002798 struct io_ring_ctx *ctx = data;
2799 struct mm_struct *cur_mm = NULL;
2800 mm_segment_t old_fs;
2801 DEFINE_WAIT(wait);
2802 unsigned inflight;
2803 unsigned long timeout;
2804
Jackie Liua4c0b3d2019-07-08 13:41:12 +08002805 complete(&ctx->sqo_thread_started);
2806
Jens Axboe6c271ce2019-01-10 11:22:30 -07002807 old_fs = get_fs();
2808 set_fs(USER_DS);
2809
2810 timeout = inflight = 0;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02002811 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002812 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002813
2814 if (inflight) {
2815 unsigned nr_events = 0;
2816
2817 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe2b2ed972019-10-25 10:06:15 -06002818 /*
2819 * inflight is the count of the maximum possible
2820 * entries we submitted, but it can be smaller
2821 * if we dropped some of them. If we don't have
2822 * poll entries available, then we know that we
2823 * have nothing left to poll for. Reset the
2824 * inflight count to zero in that case.
2825 */
2826 mutex_lock(&ctx->uring_lock);
2827 if (!list_empty(&ctx->poll_list))
2828 __io_iopoll_check(ctx, &nr_events, 0);
2829 else
2830 inflight = 0;
2831 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07002832 } else {
2833 /*
2834 * Normal IO, just pretend everything completed.
2835 * We don't have to poll completions for that.
2836 */
2837 nr_events = inflight;
2838 }
2839
2840 inflight -= nr_events;
2841 if (!inflight)
2842 timeout = jiffies + ctx->sq_thread_idle;
2843 }
2844
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002845 to_submit = io_sqring_entries(ctx);
2846 if (!to_submit) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07002847 /*
2848 * We're polling. If we're within the defined idle
2849 * period, then let us spin without work before going
2850 * to sleep.
2851 */
2852 if (inflight || !time_after(jiffies, timeout)) {
Jens Axboe9831a902019-09-19 09:48:55 -06002853 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07002854 continue;
2855 }
2856
2857 /*
2858 * Drop cur_mm before scheduling, we can't hold it for
2859 * long periods (or over schedule()). Do this before
2860 * adding ourselves to the waitqueue, as the unuse/drop
2861 * may sleep.
2862 */
2863 if (cur_mm) {
2864 unuse_mm(cur_mm);
2865 mmput(cur_mm);
2866 cur_mm = NULL;
2867 }
2868
2869 prepare_to_wait(&ctx->sqo_wait, &wait,
2870 TASK_INTERRUPTIBLE);
2871
2872 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00002873 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02002874 /* make sure to read SQ tail after writing flags */
2875 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07002876
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002877 to_submit = io_sqring_entries(ctx);
2878 if (!to_submit) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02002879 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07002880 finish_wait(&ctx->sqo_wait, &wait);
2881 break;
2882 }
2883 if (signal_pending(current))
2884 flush_signals(current);
2885 schedule();
2886 finish_wait(&ctx->sqo_wait, &wait);
2887
Hristo Venev75b28af2019-08-26 17:23:46 +00002888 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002889 continue;
2890 }
2891 finish_wait(&ctx->sqo_wait, &wait);
2892
Hristo Venev75b28af2019-08-26 17:23:46 +00002893 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002894 }
2895
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002896 to_submit = min(to_submit, ctx->sq_entries);
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03002897 inflight += io_submit_sqes(ctx, to_submit, &cur_mm);
Jens Axboe6c271ce2019-01-10 11:22:30 -07002898
2899 /* Commit SQ ring head once we've consumed all SQEs */
2900 io_commit_sqring(ctx);
2901 }
2902
2903 set_fs(old_fs);
2904 if (cur_mm) {
2905 unuse_mm(cur_mm);
2906 mmput(cur_mm);
2907 }
Jens Axboe06058632019-04-13 09:26:03 -06002908
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02002909 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06002910
Jens Axboe6c271ce2019-01-10 11:22:30 -07002911 return 0;
2912}
2913
Jens Axboebc808bc2019-10-22 13:14:37 -06002914static int io_ring_submit(struct io_ring_ctx *ctx, unsigned int to_submit)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002915{
Jens Axboe9a56a232019-01-09 09:06:50 -07002916 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002917 struct io_kiocb *link = NULL;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002918 struct io_kiocb *shadow_req = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002919 bool prev_was_link = false;
Jens Axboe5c8b0b52019-04-30 10:16:07 -06002920 int i, submit = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002921
Jens Axboe9a56a232019-01-09 09:06:50 -07002922 if (to_submit > IO_PLUG_THRESHOLD) {
2923 io_submit_state_start(&state, ctx, to_submit);
2924 statep = &state;
2925 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002926
2927 for (i = 0; i < to_submit; i++) {
2928 struct sqe_submit s;
2929
2930 if (!io_get_sqring(ctx, &s))
2931 break;
2932
Jens Axboe9e645e112019-05-10 16:07:28 -06002933 /*
2934 * If previous wasn't linked and we have a linked command,
2935 * that's the end of the chain. Submit the previous link.
2936 */
2937 if (!prev_was_link && link) {
Jens Axboebc808bc2019-10-22 13:14:37 -06002938 io_queue_link_head(ctx, link, &link->submit, shadow_req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002939 link = NULL;
Jackie Liu5f5ad9c2019-09-18 10:37:53 +08002940 shadow_req = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002941 }
2942 prev_was_link = (s.sqe->flags & IOSQE_IO_LINK) != 0;
2943
Jackie Liu4fe2c962019-09-09 20:50:40 +08002944 if (link && (s.sqe->flags & IOSQE_IO_DRAIN)) {
2945 if (!shadow_req) {
2946 shadow_req = io_get_req(ctx, NULL);
Jackie Liua1041c22019-09-18 17:25:52 +08002947 if (unlikely(!shadow_req))
2948 goto out;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002949 shadow_req->flags |= (REQ_F_IO_DRAIN | REQ_F_SHADOW_DRAIN);
2950 refcount_dec(&shadow_req->refs);
2951 }
2952 shadow_req->sequence = s.sequence;
2953 }
2954
Jackie Liua1041c22019-09-18 17:25:52 +08002955out:
Jens Axboe2b188cc2019-01-07 10:46:33 -07002956 s.has_user = true;
Jackie Liuba5290c2019-10-09 09:19:59 +08002957 s.in_async = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002958 s.needs_fixed_file = false;
Jens Axboe5c8b0b52019-04-30 10:16:07 -06002959 submit++;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002960 trace_io_uring_submit_sqe(ctx, true, false);
Jens Axboebc808bc2019-10-22 13:14:37 -06002961 io_submit_sqe(ctx, &s, statep, &link);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002962 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002963
Jens Axboe9e645e112019-05-10 16:07:28 -06002964 if (link)
Jens Axboebc808bc2019-10-22 13:14:37 -06002965 io_queue_link_head(ctx, link, &link->submit, shadow_req);
Jens Axboe9a56a232019-01-09 09:06:50 -07002966 if (statep)
2967 io_submit_state_end(statep);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002968
Pavel Begunkov935d1e42019-10-25 12:31:31 +03002969 io_commit_sqring(ctx);
2970
Jens Axboe5c8b0b52019-04-30 10:16:07 -06002971 return submit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002972}
2973
Jens Axboebda52162019-09-24 13:47:15 -06002974struct io_wait_queue {
2975 struct wait_queue_entry wq;
2976 struct io_ring_ctx *ctx;
2977 unsigned to_wait;
2978 unsigned nr_timeouts;
2979};
2980
2981static inline bool io_should_wake(struct io_wait_queue *iowq)
2982{
2983 struct io_ring_ctx *ctx = iowq->ctx;
2984
2985 /*
2986 * Wake up if we have enough events, or if a timeout occured since we
2987 * started waiting. For timeouts, we always want to return to userspace,
2988 * regardless of event count.
2989 */
2990 return io_cqring_events(ctx->rings) >= iowq->to_wait ||
2991 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
2992}
2993
2994static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
2995 int wake_flags, void *key)
2996{
2997 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
2998 wq);
2999
3000 if (!io_should_wake(iowq))
3001 return -1;
3002
3003 return autoremove_wake_function(curr, mode, wake_flags, key);
3004}
3005
Jens Axboe2b188cc2019-01-07 10:46:33 -07003006/*
3007 * Wait until events become available, if we don't already have some. The
3008 * application must reap them itself, as they reside on the shared cq ring.
3009 */
3010static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
3011 const sigset_t __user *sig, size_t sigsz)
3012{
Jens Axboebda52162019-09-24 13:47:15 -06003013 struct io_wait_queue iowq = {
3014 .wq = {
3015 .private = current,
3016 .func = io_wake_function,
3017 .entry = LIST_HEAD_INIT(iowq.wq.entry),
3018 },
3019 .ctx = ctx,
3020 .to_wait = min_events,
3021 };
Hristo Venev75b28af2019-08-26 17:23:46 +00003022 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003023 int ret;
3024
Hristo Venev75b28af2019-08-26 17:23:46 +00003025 if (io_cqring_events(rings) >= min_events)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003026 return 0;
3027
3028 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01003029#ifdef CONFIG_COMPAT
3030 if (in_compat_syscall())
3031 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07003032 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01003033 else
3034#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07003035 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01003036
Jens Axboe2b188cc2019-01-07 10:46:33 -07003037 if (ret)
3038 return ret;
3039 }
3040
Jens Axboebda52162019-09-24 13:47:15 -06003041 ret = 0;
3042 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02003043 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06003044 do {
3045 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
3046 TASK_INTERRUPTIBLE);
3047 if (io_should_wake(&iowq))
3048 break;
3049 schedule();
3050 if (signal_pending(current)) {
3051 ret = -ERESTARTSYS;
3052 break;
3053 }
3054 } while (1);
3055 finish_wait(&ctx->wait, &iowq.wq);
3056
Oleg Nesterovb7724342019-07-16 16:29:53 -07003057 restore_saved_sigmask_unless(ret == -ERESTARTSYS);
Oleg Nesterov97abc882019-06-28 12:06:50 -07003058 if (ret == -ERESTARTSYS)
3059 ret = -EINTR;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003060
Hristo Venev75b28af2019-08-26 17:23:46 +00003061 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003062}
3063
Jens Axboe6b063142019-01-10 22:13:58 -07003064static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
3065{
3066#if defined(CONFIG_UNIX)
3067 if (ctx->ring_sock) {
3068 struct sock *sock = ctx->ring_sock->sk;
3069 struct sk_buff *skb;
3070
3071 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
3072 kfree_skb(skb);
3073 }
3074#else
3075 int i;
3076
3077 for (i = 0; i < ctx->nr_user_files; i++)
Jens Axboe08a45172019-10-03 08:11:03 -06003078 if (ctx->user_files[i])
3079 fput(ctx->user_files[i]);
Jens Axboe6b063142019-01-10 22:13:58 -07003080#endif
3081}
3082
3083static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
3084{
3085 if (!ctx->user_files)
3086 return -ENXIO;
3087
3088 __io_sqe_files_unregister(ctx);
3089 kfree(ctx->user_files);
3090 ctx->user_files = NULL;
3091 ctx->nr_user_files = 0;
3092 return 0;
3093}
3094
Jens Axboe6c271ce2019-01-10 11:22:30 -07003095static void io_sq_thread_stop(struct io_ring_ctx *ctx)
3096{
3097 if (ctx->sqo_thread) {
Jackie Liua4c0b3d2019-07-08 13:41:12 +08003098 wait_for_completion(&ctx->sqo_thread_started);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003099 /*
3100 * The park is a bit of a work-around, without it we get
3101 * warning spews on shutdown with SQPOLL set and affinity
3102 * set to a single CPU.
3103 */
Jens Axboe06058632019-04-13 09:26:03 -06003104 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003105 kthread_stop(ctx->sqo_thread);
3106 ctx->sqo_thread = NULL;
3107 }
3108}
3109
Jens Axboe6b063142019-01-10 22:13:58 -07003110static void io_finish_async(struct io_ring_ctx *ctx)
3111{
Jens Axboe54a91f32019-09-10 09:15:04 -06003112 int i;
3113
Jens Axboe6c271ce2019-01-10 11:22:30 -07003114 io_sq_thread_stop(ctx);
3115
Jens Axboe54a91f32019-09-10 09:15:04 -06003116 for (i = 0; i < ARRAY_SIZE(ctx->sqo_wq); i++) {
3117 if (ctx->sqo_wq[i]) {
3118 destroy_workqueue(ctx->sqo_wq[i]);
3119 ctx->sqo_wq[i] = NULL;
3120 }
Jens Axboe6b063142019-01-10 22:13:58 -07003121 }
3122}
3123
3124#if defined(CONFIG_UNIX)
3125static void io_destruct_skb(struct sk_buff *skb)
3126{
3127 struct io_ring_ctx *ctx = skb->sk->sk_user_data;
Jens Axboe8a997342019-10-09 14:40:13 -06003128 int i;
Jens Axboe6b063142019-01-10 22:13:58 -07003129
Jens Axboe8a997342019-10-09 14:40:13 -06003130 for (i = 0; i < ARRAY_SIZE(ctx->sqo_wq); i++)
3131 if (ctx->sqo_wq[i])
3132 flush_workqueue(ctx->sqo_wq[i]);
3133
Jens Axboe6b063142019-01-10 22:13:58 -07003134 unix_destruct_scm(skb);
3135}
3136
3137/*
3138 * Ensure the UNIX gc is aware of our file set, so we are certain that
3139 * the io_uring can be safely unregistered on process exit, even if we have
3140 * loops in the file referencing.
3141 */
3142static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
3143{
3144 struct sock *sk = ctx->ring_sock->sk;
3145 struct scm_fp_list *fpl;
3146 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06003147 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07003148
3149 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
3150 unsigned long inflight = ctx->user->unix_inflight + nr;
3151
3152 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
3153 return -EMFILE;
3154 }
3155
3156 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
3157 if (!fpl)
3158 return -ENOMEM;
3159
3160 skb = alloc_skb(0, GFP_KERNEL);
3161 if (!skb) {
3162 kfree(fpl);
3163 return -ENOMEM;
3164 }
3165
3166 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07003167
Jens Axboe08a45172019-10-03 08:11:03 -06003168 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07003169 fpl->user = get_uid(ctx->user);
3170 for (i = 0; i < nr; i++) {
Jens Axboe08a45172019-10-03 08:11:03 -06003171 if (!ctx->user_files[i + offset])
3172 continue;
3173 fpl->fp[nr_files] = get_file(ctx->user_files[i + offset]);
3174 unix_inflight(fpl->user, fpl->fp[nr_files]);
3175 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07003176 }
3177
Jens Axboe08a45172019-10-03 08:11:03 -06003178 if (nr_files) {
3179 fpl->max = SCM_MAX_FD;
3180 fpl->count = nr_files;
3181 UNIXCB(skb).fp = fpl;
3182 skb->destructor = io_destruct_skb;
3183 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
3184 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07003185
Jens Axboe08a45172019-10-03 08:11:03 -06003186 for (i = 0; i < nr_files; i++)
3187 fput(fpl->fp[i]);
3188 } else {
3189 kfree_skb(skb);
3190 kfree(fpl);
3191 }
Jens Axboe6b063142019-01-10 22:13:58 -07003192
3193 return 0;
3194}
3195
3196/*
3197 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
3198 * causes regular reference counting to break down. We rely on the UNIX
3199 * garbage collection to take care of this problem for us.
3200 */
3201static int io_sqe_files_scm(struct io_ring_ctx *ctx)
3202{
3203 unsigned left, total;
3204 int ret = 0;
3205
3206 total = 0;
3207 left = ctx->nr_user_files;
3208 while (left) {
3209 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07003210
3211 ret = __io_sqe_files_scm(ctx, this_files, total);
3212 if (ret)
3213 break;
3214 left -= this_files;
3215 total += this_files;
3216 }
3217
3218 if (!ret)
3219 return 0;
3220
3221 while (total < ctx->nr_user_files) {
Jens Axboe08a45172019-10-03 08:11:03 -06003222 if (ctx->user_files[total])
3223 fput(ctx->user_files[total]);
Jens Axboe6b063142019-01-10 22:13:58 -07003224 total++;
3225 }
3226
3227 return ret;
3228}
3229#else
3230static int io_sqe_files_scm(struct io_ring_ctx *ctx)
3231{
3232 return 0;
3233}
3234#endif
3235
3236static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
3237 unsigned nr_args)
3238{
3239 __s32 __user *fds = (__s32 __user *) arg;
3240 int fd, ret = 0;
3241 unsigned i;
3242
3243 if (ctx->user_files)
3244 return -EBUSY;
3245 if (!nr_args)
3246 return -EINVAL;
3247 if (nr_args > IORING_MAX_FIXED_FILES)
3248 return -EMFILE;
3249
3250 ctx->user_files = kcalloc(nr_args, sizeof(struct file *), GFP_KERNEL);
3251 if (!ctx->user_files)
3252 return -ENOMEM;
3253
Jens Axboe08a45172019-10-03 08:11:03 -06003254 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
Jens Axboe6b063142019-01-10 22:13:58 -07003255 ret = -EFAULT;
3256 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
3257 break;
Jens Axboe08a45172019-10-03 08:11:03 -06003258 /* allow sparse sets */
3259 if (fd == -1) {
3260 ret = 0;
3261 continue;
3262 }
Jens Axboe6b063142019-01-10 22:13:58 -07003263
3264 ctx->user_files[i] = fget(fd);
3265
3266 ret = -EBADF;
3267 if (!ctx->user_files[i])
3268 break;
3269 /*
3270 * Don't allow io_uring instances to be registered. If UNIX
3271 * isn't enabled, then this causes a reference cycle and this
3272 * instance can never get freed. If UNIX is enabled we'll
3273 * handle it just fine, but there's still no point in allowing
3274 * a ring fd as it doesn't support regular read/write anyway.
3275 */
3276 if (ctx->user_files[i]->f_op == &io_uring_fops) {
3277 fput(ctx->user_files[i]);
3278 break;
3279 }
Jens Axboe6b063142019-01-10 22:13:58 -07003280 ret = 0;
3281 }
3282
3283 if (ret) {
3284 for (i = 0; i < ctx->nr_user_files; i++)
Jens Axboe08a45172019-10-03 08:11:03 -06003285 if (ctx->user_files[i])
3286 fput(ctx->user_files[i]);
Jens Axboe6b063142019-01-10 22:13:58 -07003287
3288 kfree(ctx->user_files);
Jens Axboe25adf502019-04-03 09:52:40 -06003289 ctx->user_files = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003290 ctx->nr_user_files = 0;
3291 return ret;
3292 }
3293
3294 ret = io_sqe_files_scm(ctx);
3295 if (ret)
3296 io_sqe_files_unregister(ctx);
3297
3298 return ret;
3299}
3300
Jens Axboec3a31e62019-10-03 13:59:56 -06003301static void io_sqe_file_unregister(struct io_ring_ctx *ctx, int index)
3302{
3303#if defined(CONFIG_UNIX)
3304 struct file *file = ctx->user_files[index];
3305 struct sock *sock = ctx->ring_sock->sk;
3306 struct sk_buff_head list, *head = &sock->sk_receive_queue;
3307 struct sk_buff *skb;
3308 int i;
3309
3310 __skb_queue_head_init(&list);
3311
3312 /*
3313 * Find the skb that holds this file in its SCM_RIGHTS. When found,
3314 * remove this entry and rearrange the file array.
3315 */
3316 skb = skb_dequeue(head);
3317 while (skb) {
3318 struct scm_fp_list *fp;
3319
3320 fp = UNIXCB(skb).fp;
3321 for (i = 0; i < fp->count; i++) {
3322 int left;
3323
3324 if (fp->fp[i] != file)
3325 continue;
3326
3327 unix_notinflight(fp->user, fp->fp[i]);
3328 left = fp->count - 1 - i;
3329 if (left) {
3330 memmove(&fp->fp[i], &fp->fp[i + 1],
3331 left * sizeof(struct file *));
3332 }
3333 fp->count--;
3334 if (!fp->count) {
3335 kfree_skb(skb);
3336 skb = NULL;
3337 } else {
3338 __skb_queue_tail(&list, skb);
3339 }
3340 fput(file);
3341 file = NULL;
3342 break;
3343 }
3344
3345 if (!file)
3346 break;
3347
3348 __skb_queue_tail(&list, skb);
3349
3350 skb = skb_dequeue(head);
3351 }
3352
3353 if (skb_peek(&list)) {
3354 spin_lock_irq(&head->lock);
3355 while ((skb = __skb_dequeue(&list)) != NULL)
3356 __skb_queue_tail(head, skb);
3357 spin_unlock_irq(&head->lock);
3358 }
3359#else
3360 fput(ctx->user_files[index]);
3361#endif
3362}
3363
3364static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
3365 int index)
3366{
3367#if defined(CONFIG_UNIX)
3368 struct sock *sock = ctx->ring_sock->sk;
3369 struct sk_buff_head *head = &sock->sk_receive_queue;
3370 struct sk_buff *skb;
3371
3372 /*
3373 * See if we can merge this file into an existing skb SCM_RIGHTS
3374 * file set. If there's no room, fall back to allocating a new skb
3375 * and filling it in.
3376 */
3377 spin_lock_irq(&head->lock);
3378 skb = skb_peek(head);
3379 if (skb) {
3380 struct scm_fp_list *fpl = UNIXCB(skb).fp;
3381
3382 if (fpl->count < SCM_MAX_FD) {
3383 __skb_unlink(skb, head);
3384 spin_unlock_irq(&head->lock);
3385 fpl->fp[fpl->count] = get_file(file);
3386 unix_inflight(fpl->user, fpl->fp[fpl->count]);
3387 fpl->count++;
3388 spin_lock_irq(&head->lock);
3389 __skb_queue_head(head, skb);
3390 } else {
3391 skb = NULL;
3392 }
3393 }
3394 spin_unlock_irq(&head->lock);
3395
3396 if (skb) {
3397 fput(file);
3398 return 0;
3399 }
3400
3401 return __io_sqe_files_scm(ctx, 1, index);
3402#else
3403 return 0;
3404#endif
3405}
3406
3407static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
3408 unsigned nr_args)
3409{
3410 struct io_uring_files_update up;
3411 __s32 __user *fds;
3412 int fd, i, err;
3413 __u32 done;
3414
3415 if (!ctx->user_files)
3416 return -ENXIO;
3417 if (!nr_args)
3418 return -EINVAL;
3419 if (copy_from_user(&up, arg, sizeof(up)))
3420 return -EFAULT;
3421 if (check_add_overflow(up.offset, nr_args, &done))
3422 return -EOVERFLOW;
3423 if (done > ctx->nr_user_files)
3424 return -EINVAL;
3425
3426 done = 0;
3427 fds = (__s32 __user *) up.fds;
3428 while (nr_args) {
3429 err = 0;
3430 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
3431 err = -EFAULT;
3432 break;
3433 }
3434 i = array_index_nospec(up.offset, ctx->nr_user_files);
3435 if (ctx->user_files[i]) {
3436 io_sqe_file_unregister(ctx, i);
3437 ctx->user_files[i] = NULL;
3438 }
3439 if (fd != -1) {
3440 struct file *file;
3441
3442 file = fget(fd);
3443 if (!file) {
3444 err = -EBADF;
3445 break;
3446 }
3447 /*
3448 * Don't allow io_uring instances to be registered. If
3449 * UNIX isn't enabled, then this causes a reference
3450 * cycle and this instance can never get freed. If UNIX
3451 * is enabled we'll handle it just fine, but there's
3452 * still no point in allowing a ring fd as it doesn't
3453 * support regular read/write anyway.
3454 */
3455 if (file->f_op == &io_uring_fops) {
3456 fput(file);
3457 err = -EBADF;
3458 break;
3459 }
3460 ctx->user_files[i] = file;
3461 err = io_sqe_file_register(ctx, file, i);
3462 if (err)
3463 break;
3464 }
3465 nr_args--;
3466 done++;
3467 up.offset++;
3468 }
3469
3470 return done ? done : err;
3471}
3472
Jens Axboe6c271ce2019-01-10 11:22:30 -07003473static int io_sq_offload_start(struct io_ring_ctx *ctx,
3474 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003475{
3476 int ret;
3477
Jens Axboe6c271ce2019-01-10 11:22:30 -07003478 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003479 mmgrab(current->mm);
3480 ctx->sqo_mm = current->mm;
3481
Jens Axboe6c271ce2019-01-10 11:22:30 -07003482 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06003483 ret = -EPERM;
3484 if (!capable(CAP_SYS_ADMIN))
3485 goto err;
3486
Jens Axboe917257d2019-04-13 09:28:55 -06003487 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
3488 if (!ctx->sq_thread_idle)
3489 ctx->sq_thread_idle = HZ;
3490
Jens Axboe6c271ce2019-01-10 11:22:30 -07003491 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06003492 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003493
Jens Axboe917257d2019-04-13 09:28:55 -06003494 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06003495 if (cpu >= nr_cpu_ids)
3496 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08003497 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06003498 goto err;
3499
Jens Axboe6c271ce2019-01-10 11:22:30 -07003500 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
3501 ctx, cpu,
3502 "io_uring-sq");
3503 } else {
3504 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
3505 "io_uring-sq");
3506 }
3507 if (IS_ERR(ctx->sqo_thread)) {
3508 ret = PTR_ERR(ctx->sqo_thread);
3509 ctx->sqo_thread = NULL;
3510 goto err;
3511 }
3512 wake_up_process(ctx->sqo_thread);
3513 } else if (p->flags & IORING_SETUP_SQ_AFF) {
3514 /* Can't have SQ_AFF without SQPOLL */
3515 ret = -EINVAL;
3516 goto err;
3517 }
3518
Jens Axboe2b188cc2019-01-07 10:46:33 -07003519 /* Do QD, or 2 * CPUS, whatever is smallest */
Jens Axboe54a91f32019-09-10 09:15:04 -06003520 ctx->sqo_wq[0] = alloc_workqueue("io_ring-wq",
3521 WQ_UNBOUND | WQ_FREEZABLE,
Jens Axboe2b188cc2019-01-07 10:46:33 -07003522 min(ctx->sq_entries - 1, 2 * num_online_cpus()));
Jens Axboe54a91f32019-09-10 09:15:04 -06003523 if (!ctx->sqo_wq[0]) {
3524 ret = -ENOMEM;
3525 goto err;
3526 }
3527
3528 /*
3529 * This is for buffered writes, where we want to limit the parallelism
3530 * due to file locking in file systems. As "normal" buffered writes
3531 * should parellelize on writeout quite nicely, limit us to having 2
3532 * pending. This avoids massive contention on the inode when doing
3533 * buffered async writes.
3534 */
3535 ctx->sqo_wq[1] = alloc_workqueue("io_ring-write-wq",
3536 WQ_UNBOUND | WQ_FREEZABLE, 2);
3537 if (!ctx->sqo_wq[1]) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07003538 ret = -ENOMEM;
3539 goto err;
3540 }
3541
3542 return 0;
3543err:
Jens Axboe54a91f32019-09-10 09:15:04 -06003544 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003545 mmdrop(ctx->sqo_mm);
3546 ctx->sqo_mm = NULL;
3547 return ret;
3548}
3549
3550static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
3551{
3552 atomic_long_sub(nr_pages, &user->locked_vm);
3553}
3554
3555static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
3556{
3557 unsigned long page_limit, cur_pages, new_pages;
3558
3559 /* Don't allow more pages than we can safely lock */
3560 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
3561
3562 do {
3563 cur_pages = atomic_long_read(&user->locked_vm);
3564 new_pages = cur_pages + nr_pages;
3565 if (new_pages > page_limit)
3566 return -ENOMEM;
3567 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
3568 new_pages) != cur_pages);
3569
3570 return 0;
3571}
3572
3573static void io_mem_free(void *ptr)
3574{
Mark Rutland52e04ef2019-04-30 17:30:21 +01003575 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003576
Mark Rutland52e04ef2019-04-30 17:30:21 +01003577 if (!ptr)
3578 return;
3579
3580 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003581 if (put_page_testzero(page))
3582 free_compound_page(page);
3583}
3584
3585static void *io_mem_alloc(size_t size)
3586{
3587 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
3588 __GFP_NORETRY;
3589
3590 return (void *) __get_free_pages(gfp_flags, get_order(size));
3591}
3592
Hristo Venev75b28af2019-08-26 17:23:46 +00003593static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
3594 size_t *sq_offset)
3595{
3596 struct io_rings *rings;
3597 size_t off, sq_array_size;
3598
3599 off = struct_size(rings, cqes, cq_entries);
3600 if (off == SIZE_MAX)
3601 return SIZE_MAX;
3602
3603#ifdef CONFIG_SMP
3604 off = ALIGN(off, SMP_CACHE_BYTES);
3605 if (off == 0)
3606 return SIZE_MAX;
3607#endif
3608
3609 sq_array_size = array_size(sizeof(u32), sq_entries);
3610 if (sq_array_size == SIZE_MAX)
3611 return SIZE_MAX;
3612
3613 if (check_add_overflow(off, sq_array_size, &off))
3614 return SIZE_MAX;
3615
3616 if (sq_offset)
3617 *sq_offset = off;
3618
3619 return off;
3620}
3621
Jens Axboe2b188cc2019-01-07 10:46:33 -07003622static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
3623{
Hristo Venev75b28af2019-08-26 17:23:46 +00003624 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003625
Hristo Venev75b28af2019-08-26 17:23:46 +00003626 pages = (size_t)1 << get_order(
3627 rings_size(sq_entries, cq_entries, NULL));
3628 pages += (size_t)1 << get_order(
3629 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07003630
Hristo Venev75b28af2019-08-26 17:23:46 +00003631 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003632}
3633
Jens Axboeedafcce2019-01-09 09:16:05 -07003634static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
3635{
3636 int i, j;
3637
3638 if (!ctx->user_bufs)
3639 return -ENXIO;
3640
3641 for (i = 0; i < ctx->nr_user_bufs; i++) {
3642 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
3643
3644 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbard27c4d3a2019-08-04 19:32:06 -07003645 put_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07003646
3647 if (ctx->account_mem)
3648 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003649 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07003650 imu->nr_bvecs = 0;
3651 }
3652
3653 kfree(ctx->user_bufs);
3654 ctx->user_bufs = NULL;
3655 ctx->nr_user_bufs = 0;
3656 return 0;
3657}
3658
3659static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
3660 void __user *arg, unsigned index)
3661{
3662 struct iovec __user *src;
3663
3664#ifdef CONFIG_COMPAT
3665 if (ctx->compat) {
3666 struct compat_iovec __user *ciovs;
3667 struct compat_iovec ciov;
3668
3669 ciovs = (struct compat_iovec __user *) arg;
3670 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
3671 return -EFAULT;
3672
3673 dst->iov_base = (void __user *) (unsigned long) ciov.iov_base;
3674 dst->iov_len = ciov.iov_len;
3675 return 0;
3676 }
3677#endif
3678 src = (struct iovec __user *) arg;
3679 if (copy_from_user(dst, &src[index], sizeof(*dst)))
3680 return -EFAULT;
3681 return 0;
3682}
3683
3684static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
3685 unsigned nr_args)
3686{
3687 struct vm_area_struct **vmas = NULL;
3688 struct page **pages = NULL;
3689 int i, j, got_pages = 0;
3690 int ret = -EINVAL;
3691
3692 if (ctx->user_bufs)
3693 return -EBUSY;
3694 if (!nr_args || nr_args > UIO_MAXIOV)
3695 return -EINVAL;
3696
3697 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
3698 GFP_KERNEL);
3699 if (!ctx->user_bufs)
3700 return -ENOMEM;
3701
3702 for (i = 0; i < nr_args; i++) {
3703 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
3704 unsigned long off, start, end, ubuf;
3705 int pret, nr_pages;
3706 struct iovec iov;
3707 size_t size;
3708
3709 ret = io_copy_iov(ctx, &iov, arg, i);
3710 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03003711 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07003712
3713 /*
3714 * Don't impose further limits on the size and buffer
3715 * constraints here, we'll -EINVAL later when IO is
3716 * submitted if they are wrong.
3717 */
3718 ret = -EFAULT;
3719 if (!iov.iov_base || !iov.iov_len)
3720 goto err;
3721
3722 /* arbitrary limit, but we need something */
3723 if (iov.iov_len > SZ_1G)
3724 goto err;
3725
3726 ubuf = (unsigned long) iov.iov_base;
3727 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
3728 start = ubuf >> PAGE_SHIFT;
3729 nr_pages = end - start;
3730
3731 if (ctx->account_mem) {
3732 ret = io_account_mem(ctx->user, nr_pages);
3733 if (ret)
3734 goto err;
3735 }
3736
3737 ret = 0;
3738 if (!pages || nr_pages > got_pages) {
3739 kfree(vmas);
3740 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003741 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07003742 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003743 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07003744 sizeof(struct vm_area_struct *),
3745 GFP_KERNEL);
3746 if (!pages || !vmas) {
3747 ret = -ENOMEM;
3748 if (ctx->account_mem)
3749 io_unaccount_mem(ctx->user, nr_pages);
3750 goto err;
3751 }
3752 got_pages = nr_pages;
3753 }
3754
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003755 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07003756 GFP_KERNEL);
3757 ret = -ENOMEM;
3758 if (!imu->bvec) {
3759 if (ctx->account_mem)
3760 io_unaccount_mem(ctx->user, nr_pages);
3761 goto err;
3762 }
3763
3764 ret = 0;
3765 down_read(&current->mm->mmap_sem);
Ira Weiny932f4a62019-05-13 17:17:03 -07003766 pret = get_user_pages(ubuf, nr_pages,
3767 FOLL_WRITE | FOLL_LONGTERM,
3768 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07003769 if (pret == nr_pages) {
3770 /* don't support file backed memory */
3771 for (j = 0; j < nr_pages; j++) {
3772 struct vm_area_struct *vma = vmas[j];
3773
3774 if (vma->vm_file &&
3775 !is_file_hugepages(vma->vm_file)) {
3776 ret = -EOPNOTSUPP;
3777 break;
3778 }
3779 }
3780 } else {
3781 ret = pret < 0 ? pret : -EFAULT;
3782 }
3783 up_read(&current->mm->mmap_sem);
3784 if (ret) {
3785 /*
3786 * if we did partial map, or found file backed vmas,
3787 * release any pages we did get
3788 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07003789 if (pret > 0)
3790 put_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07003791 if (ctx->account_mem)
3792 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003793 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07003794 goto err;
3795 }
3796
3797 off = ubuf & ~PAGE_MASK;
3798 size = iov.iov_len;
3799 for (j = 0; j < nr_pages; j++) {
3800 size_t vec_len;
3801
3802 vec_len = min_t(size_t, size, PAGE_SIZE - off);
3803 imu->bvec[j].bv_page = pages[j];
3804 imu->bvec[j].bv_len = vec_len;
3805 imu->bvec[j].bv_offset = off;
3806 off = 0;
3807 size -= vec_len;
3808 }
3809 /* store original address for later verification */
3810 imu->ubuf = ubuf;
3811 imu->len = iov.iov_len;
3812 imu->nr_bvecs = nr_pages;
3813
3814 ctx->nr_user_bufs++;
3815 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003816 kvfree(pages);
3817 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07003818 return 0;
3819err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003820 kvfree(pages);
3821 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07003822 io_sqe_buffer_unregister(ctx);
3823 return ret;
3824}
3825
Jens Axboe9b402842019-04-11 11:45:41 -06003826static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
3827{
3828 __s32 __user *fds = arg;
3829 int fd;
3830
3831 if (ctx->cq_ev_fd)
3832 return -EBUSY;
3833
3834 if (copy_from_user(&fd, fds, sizeof(*fds)))
3835 return -EFAULT;
3836
3837 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
3838 if (IS_ERR(ctx->cq_ev_fd)) {
3839 int ret = PTR_ERR(ctx->cq_ev_fd);
3840 ctx->cq_ev_fd = NULL;
3841 return ret;
3842 }
3843
3844 return 0;
3845}
3846
3847static int io_eventfd_unregister(struct io_ring_ctx *ctx)
3848{
3849 if (ctx->cq_ev_fd) {
3850 eventfd_ctx_put(ctx->cq_ev_fd);
3851 ctx->cq_ev_fd = NULL;
3852 return 0;
3853 }
3854
3855 return -ENXIO;
3856}
3857
Jens Axboe2b188cc2019-01-07 10:46:33 -07003858static void io_ring_ctx_free(struct io_ring_ctx *ctx)
3859{
Jens Axboe6b063142019-01-10 22:13:58 -07003860 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003861 if (ctx->sqo_mm)
3862 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07003863
3864 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07003865 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07003866 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06003867 io_eventfd_unregister(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07003868
Jens Axboe2b188cc2019-01-07 10:46:33 -07003869#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07003870 if (ctx->ring_sock) {
3871 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07003872 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07003873 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003874#endif
3875
Hristo Venev75b28af2019-08-26 17:23:46 +00003876 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003877 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003878
3879 percpu_ref_exit(&ctx->refs);
3880 if (ctx->account_mem)
3881 io_unaccount_mem(ctx->user,
3882 ring_pages(ctx->sq_entries, ctx->cq_entries));
3883 free_uid(ctx->user);
3884 kfree(ctx);
3885}
3886
3887static __poll_t io_uring_poll(struct file *file, poll_table *wait)
3888{
3889 struct io_ring_ctx *ctx = file->private_data;
3890 __poll_t mask = 0;
3891
3892 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02003893 /*
3894 * synchronizes with barrier from wq_has_sleeper call in
3895 * io_commit_cqring
3896 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07003897 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00003898 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
3899 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003900 mask |= EPOLLOUT | EPOLLWRNORM;
yangerkundaa5de52019-09-24 20:53:34 +08003901 if (READ_ONCE(ctx->rings->cq.head) != ctx->cached_cq_tail)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003902 mask |= EPOLLIN | EPOLLRDNORM;
3903
3904 return mask;
3905}
3906
3907static int io_uring_fasync(int fd, struct file *file, int on)
3908{
3909 struct io_ring_ctx *ctx = file->private_data;
3910
3911 return fasync_helper(fd, file, on, &ctx->cq_fasync);
3912}
3913
3914static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
3915{
3916 mutex_lock(&ctx->uring_lock);
3917 percpu_ref_kill(&ctx->refs);
3918 mutex_unlock(&ctx->uring_lock);
3919
Jens Axboe5262f562019-09-17 12:26:57 -06003920 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003921 io_poll_remove_all(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07003922 io_iopoll_reap_events(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003923 wait_for_completion(&ctx->ctx_done);
3924 io_ring_ctx_free(ctx);
3925}
3926
3927static int io_uring_release(struct inode *inode, struct file *file)
3928{
3929 struct io_ring_ctx *ctx = file->private_data;
3930
3931 file->private_data = NULL;
3932 io_ring_ctx_wait_and_kill(ctx);
3933 return 0;
3934}
3935
3936static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
3937{
3938 loff_t offset = (loff_t) vma->vm_pgoff << PAGE_SHIFT;
3939 unsigned long sz = vma->vm_end - vma->vm_start;
3940 struct io_ring_ctx *ctx = file->private_data;
3941 unsigned long pfn;
3942 struct page *page;
3943 void *ptr;
3944
3945 switch (offset) {
3946 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00003947 case IORING_OFF_CQ_RING:
3948 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003949 break;
3950 case IORING_OFF_SQES:
3951 ptr = ctx->sq_sqes;
3952 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003953 default:
3954 return -EINVAL;
3955 }
3956
3957 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07003958 if (sz > page_size(page))
Jens Axboe2b188cc2019-01-07 10:46:33 -07003959 return -EINVAL;
3960
3961 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
3962 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
3963}
3964
3965SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
3966 u32, min_complete, u32, flags, const sigset_t __user *, sig,
3967 size_t, sigsz)
3968{
3969 struct io_ring_ctx *ctx;
3970 long ret = -EBADF;
3971 int submitted = 0;
3972 struct fd f;
3973
Jens Axboe6c271ce2019-01-10 11:22:30 -07003974 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07003975 return -EINVAL;
3976
3977 f = fdget(fd);
3978 if (!f.file)
3979 return -EBADF;
3980
3981 ret = -EOPNOTSUPP;
3982 if (f.file->f_op != &io_uring_fops)
3983 goto out_fput;
3984
3985 ret = -ENXIO;
3986 ctx = f.file->private_data;
3987 if (!percpu_ref_tryget(&ctx->refs))
3988 goto out_fput;
3989
Jens Axboe6c271ce2019-01-10 11:22:30 -07003990 /*
3991 * For SQ polling, the thread will do all submissions and completions.
3992 * Just return the requested submit count, and wake the thread if
3993 * we were asked to.
3994 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06003995 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003996 if (ctx->flags & IORING_SETUP_SQPOLL) {
3997 if (flags & IORING_ENTER_SQ_WAKEUP)
3998 wake_up(&ctx->sqo_wait);
3999 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06004000 } else if (to_submit) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07004001 to_submit = min(to_submit, ctx->sq_entries);
4002
4003 mutex_lock(&ctx->uring_lock);
Jens Axboebc808bc2019-10-22 13:14:37 -06004004 submitted = io_ring_submit(ctx, to_submit);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004005 mutex_unlock(&ctx->uring_lock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004006 }
4007 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07004008 unsigned nr_events = 0;
4009
Jens Axboe2b188cc2019-01-07 10:46:33 -07004010 min_complete = min(min_complete, ctx->cq_entries);
4011
Jens Axboedef596e2019-01-09 08:59:42 -07004012 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07004013 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07004014 } else {
4015 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
4016 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004017 }
4018
Pavel Begunkov6805b322019-10-08 02:18:42 +03004019 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004020out_fput:
4021 fdput(f);
4022 return submitted ? submitted : ret;
4023}
4024
4025static const struct file_operations io_uring_fops = {
4026 .release = io_uring_release,
4027 .mmap = io_uring_mmap,
4028 .poll = io_uring_poll,
4029 .fasync = io_uring_fasync,
4030};
4031
4032static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
4033 struct io_uring_params *p)
4034{
Hristo Venev75b28af2019-08-26 17:23:46 +00004035 struct io_rings *rings;
4036 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004037
Hristo Venev75b28af2019-08-26 17:23:46 +00004038 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
4039 if (size == SIZE_MAX)
4040 return -EOVERFLOW;
4041
4042 rings = io_mem_alloc(size);
4043 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004044 return -ENOMEM;
4045
Hristo Venev75b28af2019-08-26 17:23:46 +00004046 ctx->rings = rings;
4047 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
4048 rings->sq_ring_mask = p->sq_entries - 1;
4049 rings->cq_ring_mask = p->cq_entries - 1;
4050 rings->sq_ring_entries = p->sq_entries;
4051 rings->cq_ring_entries = p->cq_entries;
4052 ctx->sq_mask = rings->sq_ring_mask;
4053 ctx->cq_mask = rings->cq_ring_mask;
4054 ctx->sq_entries = rings->sq_ring_entries;
4055 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004056
4057 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
4058 if (size == SIZE_MAX)
4059 return -EOVERFLOW;
4060
4061 ctx->sq_sqes = io_mem_alloc(size);
Mark Rutland52e04ef2019-04-30 17:30:21 +01004062 if (!ctx->sq_sqes)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004063 return -ENOMEM;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004064
Jens Axboe2b188cc2019-01-07 10:46:33 -07004065 return 0;
4066}
4067
4068/*
4069 * Allocate an anonymous fd, this is what constitutes the application
4070 * visible backing of an io_uring instance. The application mmaps this
4071 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
4072 * we have to tie this fd to a socket for file garbage collection purposes.
4073 */
4074static int io_uring_get_fd(struct io_ring_ctx *ctx)
4075{
4076 struct file *file;
4077 int ret;
4078
4079#if defined(CONFIG_UNIX)
4080 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
4081 &ctx->ring_sock);
4082 if (ret)
4083 return ret;
4084#endif
4085
4086 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
4087 if (ret < 0)
4088 goto err;
4089
4090 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
4091 O_RDWR | O_CLOEXEC);
4092 if (IS_ERR(file)) {
4093 put_unused_fd(ret);
4094 ret = PTR_ERR(file);
4095 goto err;
4096 }
4097
4098#if defined(CONFIG_UNIX)
4099 ctx->ring_sock->file = file;
Jens Axboe6b063142019-01-10 22:13:58 -07004100 ctx->ring_sock->sk->sk_user_data = ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004101#endif
4102 fd_install(ret, file);
4103 return ret;
4104err:
4105#if defined(CONFIG_UNIX)
4106 sock_release(ctx->ring_sock);
4107 ctx->ring_sock = NULL;
4108#endif
4109 return ret;
4110}
4111
4112static int io_uring_create(unsigned entries, struct io_uring_params *p)
4113{
4114 struct user_struct *user = NULL;
4115 struct io_ring_ctx *ctx;
4116 bool account_mem;
4117 int ret;
4118
4119 if (!entries || entries > IORING_MAX_ENTRIES)
4120 return -EINVAL;
4121
4122 /*
4123 * Use twice as many entries for the CQ ring. It's possible for the
4124 * application to drive a higher depth than the size of the SQ ring,
4125 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06004126 * some flexibility in overcommitting a bit. If the application has
4127 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
4128 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07004129 */
4130 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06004131 if (p->flags & IORING_SETUP_CQSIZE) {
4132 /*
4133 * If IORING_SETUP_CQSIZE is set, we do the same roundup
4134 * to a power-of-two, if it isn't already. We do NOT impose
4135 * any cq vs sq ring sizing.
4136 */
4137 if (p->cq_entries < p->sq_entries || p->cq_entries > IORING_MAX_CQ_ENTRIES)
4138 return -EINVAL;
4139 p->cq_entries = roundup_pow_of_two(p->cq_entries);
4140 } else {
4141 p->cq_entries = 2 * p->sq_entries;
4142 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004143
4144 user = get_uid(current_user());
4145 account_mem = !capable(CAP_IPC_LOCK);
4146
4147 if (account_mem) {
4148 ret = io_account_mem(user,
4149 ring_pages(p->sq_entries, p->cq_entries));
4150 if (ret) {
4151 free_uid(user);
4152 return ret;
4153 }
4154 }
4155
4156 ctx = io_ring_ctx_alloc(p);
4157 if (!ctx) {
4158 if (account_mem)
4159 io_unaccount_mem(user, ring_pages(p->sq_entries,
4160 p->cq_entries));
4161 free_uid(user);
4162 return -ENOMEM;
4163 }
4164 ctx->compat = in_compat_syscall();
4165 ctx->account_mem = account_mem;
4166 ctx->user = user;
4167
4168 ret = io_allocate_scq_urings(ctx, p);
4169 if (ret)
4170 goto err;
4171
Jens Axboe6c271ce2019-01-10 11:22:30 -07004172 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004173 if (ret)
4174 goto err;
4175
Jens Axboe2b188cc2019-01-07 10:46:33 -07004176 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00004177 p->sq_off.head = offsetof(struct io_rings, sq.head);
4178 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
4179 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
4180 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
4181 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
4182 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
4183 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004184
4185 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00004186 p->cq_off.head = offsetof(struct io_rings, cq.head);
4187 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
4188 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
4189 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
4190 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
4191 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06004192
Jens Axboe044c1ab2019-10-28 09:15:33 -06004193 /*
4194 * Install ring fd as the very last thing, so we don't risk someone
4195 * having closed it before we finish setup
4196 */
4197 ret = io_uring_get_fd(ctx);
4198 if (ret < 0)
4199 goto err;
4200
Jens Axboeac90f242019-09-06 10:26:21 -06004201 p->features = IORING_FEAT_SINGLE_MMAP;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004202 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004203 return ret;
4204err:
4205 io_ring_ctx_wait_and_kill(ctx);
4206 return ret;
4207}
4208
4209/*
4210 * Sets up an aio uring context, and returns the fd. Applications asks for a
4211 * ring size, we return the actual sq/cq ring sizes (among other things) in the
4212 * params structure passed in.
4213 */
4214static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
4215{
4216 struct io_uring_params p;
4217 long ret;
4218 int i;
4219
4220 if (copy_from_user(&p, params, sizeof(p)))
4221 return -EFAULT;
4222 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
4223 if (p.resv[i])
4224 return -EINVAL;
4225 }
4226
Jens Axboe6c271ce2019-01-10 11:22:30 -07004227 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe33a107f2019-10-04 12:10:03 -06004228 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE))
Jens Axboe2b188cc2019-01-07 10:46:33 -07004229 return -EINVAL;
4230
4231 ret = io_uring_create(entries, &p);
4232 if (ret < 0)
4233 return ret;
4234
4235 if (copy_to_user(params, &p, sizeof(p)))
4236 return -EFAULT;
4237
4238 return ret;
4239}
4240
4241SYSCALL_DEFINE2(io_uring_setup, u32, entries,
4242 struct io_uring_params __user *, params)
4243{
4244 return io_uring_setup(entries, params);
4245}
4246
Jens Axboeedafcce2019-01-09 09:16:05 -07004247static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
4248 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06004249 __releases(ctx->uring_lock)
4250 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07004251{
4252 int ret;
4253
Jens Axboe35fa71a2019-04-22 10:23:23 -06004254 /*
4255 * We're inside the ring mutex, if the ref is already dying, then
4256 * someone else killed the ctx or is already going through
4257 * io_uring_register().
4258 */
4259 if (percpu_ref_is_dying(&ctx->refs))
4260 return -ENXIO;
4261
Jens Axboeedafcce2019-01-09 09:16:05 -07004262 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06004263
4264 /*
4265 * Drop uring mutex before waiting for references to exit. If another
4266 * thread is currently inside io_uring_enter() it might need to grab
4267 * the uring_lock to make progress. If we hold it here across the drain
4268 * wait, then we can deadlock. It's safe to drop the mutex here, since
4269 * no new references will come in after we've killed the percpu ref.
4270 */
4271 mutex_unlock(&ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07004272 wait_for_completion(&ctx->ctx_done);
Jens Axboeb19062a2019-04-15 10:49:38 -06004273 mutex_lock(&ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07004274
4275 switch (opcode) {
4276 case IORING_REGISTER_BUFFERS:
4277 ret = io_sqe_buffer_register(ctx, arg, nr_args);
4278 break;
4279 case IORING_UNREGISTER_BUFFERS:
4280 ret = -EINVAL;
4281 if (arg || nr_args)
4282 break;
4283 ret = io_sqe_buffer_unregister(ctx);
4284 break;
Jens Axboe6b063142019-01-10 22:13:58 -07004285 case IORING_REGISTER_FILES:
4286 ret = io_sqe_files_register(ctx, arg, nr_args);
4287 break;
4288 case IORING_UNREGISTER_FILES:
4289 ret = -EINVAL;
4290 if (arg || nr_args)
4291 break;
4292 ret = io_sqe_files_unregister(ctx);
4293 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06004294 case IORING_REGISTER_FILES_UPDATE:
4295 ret = io_sqe_files_update(ctx, arg, nr_args);
4296 break;
Jens Axboe9b402842019-04-11 11:45:41 -06004297 case IORING_REGISTER_EVENTFD:
4298 ret = -EINVAL;
4299 if (nr_args != 1)
4300 break;
4301 ret = io_eventfd_register(ctx, arg);
4302 break;
4303 case IORING_UNREGISTER_EVENTFD:
4304 ret = -EINVAL;
4305 if (arg || nr_args)
4306 break;
4307 ret = io_eventfd_unregister(ctx);
4308 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07004309 default:
4310 ret = -EINVAL;
4311 break;
4312 }
4313
4314 /* bring the ctx back to life */
4315 reinit_completion(&ctx->ctx_done);
4316 percpu_ref_reinit(&ctx->refs);
4317 return ret;
4318}
4319
4320SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
4321 void __user *, arg, unsigned int, nr_args)
4322{
4323 struct io_ring_ctx *ctx;
4324 long ret = -EBADF;
4325 struct fd f;
4326
4327 f = fdget(fd);
4328 if (!f.file)
4329 return -EBADF;
4330
4331 ret = -EOPNOTSUPP;
4332 if (f.file->f_op != &io_uring_fops)
4333 goto out_fput;
4334
4335 ctx = f.file->private_data;
4336
4337 mutex_lock(&ctx->uring_lock);
4338 ret = __io_uring_register(ctx, opcode, arg, nr_args);
4339 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004340 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
4341 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07004342out_fput:
4343 fdput(f);
4344 return ret;
4345}
4346
Jens Axboe2b188cc2019-01-07 10:46:33 -07004347static int __init io_uring_init(void)
4348{
4349 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
4350 return 0;
4351};
4352__initcall(io_uring_init);