blob: ceb3497bdd2a739d114c20d9077c8868747efe6c [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
74#include <uapi/linux/io_uring.h>
75
76#include "internal.h"
77
Daniel Xu5277dea2019-09-14 14:23:45 -070078#define IORING_MAX_ENTRIES 32768
Jens Axboe6b063142019-01-10 22:13:58 -070079#define IORING_MAX_FIXED_FILES 1024
Jens Axboe2b188cc2019-01-07 10:46:33 -070080
81struct io_uring {
82 u32 head ____cacheline_aligned_in_smp;
83 u32 tail ____cacheline_aligned_in_smp;
84};
85
Stefan Bühler1e84b972019-04-24 23:54:16 +020086/*
Hristo Venev75b28af2019-08-26 17:23:46 +000087 * This data is shared with the application through the mmap at offsets
88 * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING.
Stefan Bühler1e84b972019-04-24 23:54:16 +020089 *
90 * The offsets to the member fields are published through struct
91 * io_sqring_offsets when calling io_uring_setup.
92 */
Hristo Venev75b28af2019-08-26 17:23:46 +000093struct io_rings {
Stefan Bühler1e84b972019-04-24 23:54:16 +020094 /*
95 * Head and tail offsets into the ring; the offsets need to be
96 * masked to get valid indices.
97 *
Hristo Venev75b28af2019-08-26 17:23:46 +000098 * The kernel controls head of the sq ring and the tail of the cq ring,
99 * and the application controls tail of the sq ring and the head of the
100 * cq ring.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200101 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000102 struct io_uring sq, cq;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200103 /*
Hristo Venev75b28af2019-08-26 17:23:46 +0000104 * Bitmasks to apply to head and tail offsets (constant, equals
Stefan Bühler1e84b972019-04-24 23:54:16 +0200105 * ring_entries - 1)
106 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000107 u32 sq_ring_mask, cq_ring_mask;
108 /* Ring sizes (constant, power of 2) */
109 u32 sq_ring_entries, cq_ring_entries;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200110 /*
111 * Number of invalid entries dropped by the kernel due to
112 * invalid index stored in array
113 *
114 * Written by the kernel, shouldn't be modified by the
115 * application (i.e. get number of "new events" by comparing to
116 * cached value).
117 *
118 * After a new SQ head value was read by the application this
119 * counter includes all submissions that were dropped reaching
120 * the new SQ head (and possibly more).
121 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000122 u32 sq_dropped;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200123 /*
124 * Runtime flags
125 *
126 * Written by the kernel, shouldn't be modified by the
127 * application.
128 *
129 * The application needs a full memory barrier before checking
130 * for IORING_SQ_NEED_WAKEUP after updating the sq tail.
131 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000132 u32 sq_flags;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200133 /*
134 * Number of completion events lost because the queue was full;
135 * this should be avoided by the application by making sure
136 * there are not more requests pending thatn there is space in
137 * the completion queue.
138 *
139 * Written by the kernel, shouldn't be modified by the
140 * application (i.e. get number of "new events" by comparing to
141 * cached value).
142 *
143 * As completion events come in out of order this counter is not
144 * ordered with any other data.
145 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000146 u32 cq_overflow;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200147 /*
148 * Ring buffer of completion events.
149 *
150 * The kernel writes completion events fresh every time they are
151 * produced, so the application is allowed to modify pending
152 * entries.
153 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000154 struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700155};
156
Jens Axboeedafcce2019-01-09 09:16:05 -0700157struct io_mapped_ubuf {
158 u64 ubuf;
159 size_t len;
160 struct bio_vec *bvec;
161 unsigned int nr_bvecs;
162};
163
Jens Axboe31b51512019-01-18 22:56:34 -0700164struct async_list {
165 spinlock_t lock;
166 atomic_t cnt;
167 struct list_head list;
168
169 struct file *file;
Jens Axboe6d5d5ac2019-09-11 10:16:13 -0600170 off_t io_start;
Zhengyuan Liu9310a7ba2019-07-22 10:23:27 +0800171 size_t io_len;
Jens Axboe31b51512019-01-18 22:56:34 -0700172};
173
Jens Axboe2b188cc2019-01-07 10:46:33 -0700174struct io_ring_ctx {
175 struct {
176 struct percpu_ref refs;
177 } ____cacheline_aligned_in_smp;
178
179 struct {
180 unsigned int flags;
181 bool compat;
182 bool account_mem;
183
Hristo Venev75b28af2019-08-26 17:23:46 +0000184 /*
185 * Ring buffer of indices into array of io_uring_sqe, which is
186 * mmapped by the application using the IORING_OFF_SQES offset.
187 *
188 * This indirection could e.g. be used to assign fixed
189 * io_uring_sqe entries to operations and only submit them to
190 * the queue when needed.
191 *
192 * The kernel modifies neither the indices array nor the entries
193 * array.
194 */
195 u32 *sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700196 unsigned cached_sq_head;
197 unsigned sq_entries;
198 unsigned sq_mask;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700199 unsigned sq_thread_idle;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700200 struct io_uring_sqe *sq_sqes;
Jens Axboede0617e2019-04-06 21:51:27 -0600201
202 struct list_head defer_list;
Jens Axboe5262f562019-09-17 12:26:57 -0600203 struct list_head timeout_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700204 } ____cacheline_aligned_in_smp;
205
206 /* IO offload */
Jens Axboe54a91f32019-09-10 09:15:04 -0600207 struct workqueue_struct *sqo_wq[2];
Jens Axboe6c271ce2019-01-10 11:22:30 -0700208 struct task_struct *sqo_thread; /* if using sq thread polling */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700209 struct mm_struct *sqo_mm;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700210 wait_queue_head_t sqo_wait;
Jackie Liua4c0b3d2019-07-08 13:41:12 +0800211 struct completion sqo_thread_started;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700212
213 struct {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700214 unsigned cached_cq_tail;
215 unsigned cq_entries;
216 unsigned cq_mask;
217 struct wait_queue_head cq_wait;
218 struct fasync_struct *cq_fasync;
Jens Axboe9b402842019-04-11 11:45:41 -0600219 struct eventfd_ctx *cq_ev_fd;
Jens Axboe5262f562019-09-17 12:26:57 -0600220 atomic_t cq_timeouts;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700221 } ____cacheline_aligned_in_smp;
222
Hristo Venev75b28af2019-08-26 17:23:46 +0000223 struct io_rings *rings;
224
Jens Axboe6b063142019-01-10 22:13:58 -0700225 /*
226 * If used, fixed file set. Writers must ensure that ->refs is dead,
227 * readers must ensure that ->refs is alive as long as the file* is
228 * used. Only updated through io_uring_register(2).
229 */
230 struct file **user_files;
231 unsigned nr_user_files;
232
Jens Axboeedafcce2019-01-09 09:16:05 -0700233 /* if used, fixed mapped user buffers */
234 unsigned nr_user_bufs;
235 struct io_mapped_ubuf *user_bufs;
236
Jens Axboe2b188cc2019-01-07 10:46:33 -0700237 struct user_struct *user;
238
239 struct completion ctx_done;
240
241 struct {
242 struct mutex uring_lock;
243 wait_queue_head_t wait;
244 } ____cacheline_aligned_in_smp;
245
246 struct {
247 spinlock_t completion_lock;
Jens Axboedef596e2019-01-09 08:59:42 -0700248 bool poll_multi_file;
249 /*
250 * ->poll_list is protected by the ctx->uring_lock for
251 * io_uring instances that don't use IORING_SETUP_SQPOLL.
252 * For SQPOLL, only the single threaded io_sq_thread() will
253 * manipulate the list, hence no extra locking is needed there.
254 */
255 struct list_head poll_list;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700256 struct list_head cancel_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700257 } ____cacheline_aligned_in_smp;
258
Jens Axboe31b51512019-01-18 22:56:34 -0700259 struct async_list pending_async[2];
260
Jens Axboe2b188cc2019-01-07 10:46:33 -0700261#if defined(CONFIG_UNIX)
262 struct socket *ring_sock;
263#endif
264};
265
266struct sqe_submit {
267 const struct io_uring_sqe *sqe;
268 unsigned short index;
Jackie Liu8776f3f2019-09-09 20:50:39 +0800269 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700270 bool has_user;
Jens Axboedef596e2019-01-09 08:59:42 -0700271 bool needs_lock;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700272 bool needs_fixed_file;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700273};
274
Jens Axboe09bb8392019-03-13 12:39:28 -0600275/*
276 * First field must be the file pointer in all the
277 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
278 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700279struct io_poll_iocb {
280 struct file *file;
281 struct wait_queue_head *head;
282 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600283 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700284 bool canceled;
285 struct wait_queue_entry wait;
286};
287
Jens Axboe5262f562019-09-17 12:26:57 -0600288struct io_timeout {
289 struct file *file;
290 struct hrtimer timer;
291};
292
Jens Axboe09bb8392019-03-13 12:39:28 -0600293/*
294 * NOTE! Each of the iocb union members has the file pointer
295 * as the first entry in their struct definition. So you can
296 * access the file pointer through any of the sub-structs,
297 * or directly as just 'ki_filp' in this struct.
298 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700299struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700300 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600301 struct file *file;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700302 struct kiocb rw;
303 struct io_poll_iocb poll;
Jens Axboe5262f562019-09-17 12:26:57 -0600304 struct io_timeout timeout;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700305 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700306
307 struct sqe_submit submit;
308
309 struct io_ring_ctx *ctx;
310 struct list_head list;
Jens Axboe9e645e112019-05-10 16:07:28 -0600311 struct list_head link_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700312 unsigned int flags;
Jens Axboec16361c2019-01-17 08:39:48 -0700313 refcount_t refs;
Stefan Bühler8449eed2019-04-27 20:34:19 +0200314#define REQ_F_NOWAIT 1 /* must not punt to workers */
Jens Axboedef596e2019-01-09 08:59:42 -0700315#define REQ_F_IOPOLL_COMPLETED 2 /* polled IO has completed */
Jens Axboe6b063142019-01-10 22:13:58 -0700316#define REQ_F_FIXED_FILE 4 /* ctx owns file */
Jens Axboe31b51512019-01-18 22:56:34 -0700317#define REQ_F_SEQ_PREV 8 /* sequential with previous */
Stefan Bühlere2033e32019-05-11 19:08:01 +0200318#define REQ_F_IO_DRAIN 16 /* drain existing IO first */
319#define REQ_F_IO_DRAINED 32 /* drain done */
Jens Axboe9e645e112019-05-10 16:07:28 -0600320#define REQ_F_LINK 64 /* linked sqes */
Zhengyuan Liuf7b76ac2019-07-16 23:26:14 +0800321#define REQ_F_LINK_DONE 128 /* linked sqes done */
322#define REQ_F_FAIL_LINK 256 /* fail rest of links */
Jackie Liu4fe2c962019-09-09 20:50:40 +0800323#define REQ_F_SHADOW_DRAIN 512 /* link-drain shadow req */
Jens Axboe5262f562019-09-17 12:26:57 -0600324#define REQ_F_TIMEOUT 1024 /* timeout request */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700325 u64 user_data;
Jens Axboe9e645e112019-05-10 16:07:28 -0600326 u32 result;
Jens Axboede0617e2019-04-06 21:51:27 -0600327 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700328
329 struct work_struct work;
330};
331
332#define IO_PLUG_THRESHOLD 2
Jens Axboedef596e2019-01-09 08:59:42 -0700333#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700334
Jens Axboe9a56a232019-01-09 09:06:50 -0700335struct io_submit_state {
336 struct blk_plug plug;
337
338 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700339 * io_kiocb alloc cache
340 */
341 void *reqs[IO_IOPOLL_BATCH];
342 unsigned int free_reqs;
343 unsigned int cur_req;
344
345 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700346 * File reference cache
347 */
348 struct file *file;
349 unsigned int fd;
350 unsigned int has_refs;
351 unsigned int used_refs;
352 unsigned int ios_left;
353};
354
Jens Axboede0617e2019-04-06 21:51:27 -0600355static void io_sq_wq_submit_work(struct work_struct *work);
Jens Axboe5262f562019-09-17 12:26:57 -0600356static void io_cqring_fill_event(struct io_ring_ctx *ctx, u64 ki_user_data,
357 long res);
Jackie Liu4fe2c962019-09-09 20:50:40 +0800358static void __io_free_req(struct io_kiocb *req);
Jens Axboede0617e2019-04-06 21:51:27 -0600359
Jens Axboe2b188cc2019-01-07 10:46:33 -0700360static struct kmem_cache *req_cachep;
361
362static const struct file_operations io_uring_fops;
363
364struct sock *io_uring_get_socket(struct file *file)
365{
366#if defined(CONFIG_UNIX)
367 if (file->f_op == &io_uring_fops) {
368 struct io_ring_ctx *ctx = file->private_data;
369
370 return ctx->ring_sock->sk;
371 }
372#endif
373 return NULL;
374}
375EXPORT_SYMBOL(io_uring_get_socket);
376
377static void io_ring_ctx_ref_free(struct percpu_ref *ref)
378{
379 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
380
381 complete(&ctx->ctx_done);
382}
383
384static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
385{
386 struct io_ring_ctx *ctx;
Jens Axboe31b51512019-01-18 22:56:34 -0700387 int i;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700388
389 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
390 if (!ctx)
391 return NULL;
392
Roman Gushchin21482892019-05-07 10:01:48 -0700393 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
394 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700395 kfree(ctx);
396 return NULL;
397 }
398
399 ctx->flags = p->flags;
400 init_waitqueue_head(&ctx->cq_wait);
401 init_completion(&ctx->ctx_done);
Jackie Liua4c0b3d2019-07-08 13:41:12 +0800402 init_completion(&ctx->sqo_thread_started);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700403 mutex_init(&ctx->uring_lock);
404 init_waitqueue_head(&ctx->wait);
Jens Axboe31b51512019-01-18 22:56:34 -0700405 for (i = 0; i < ARRAY_SIZE(ctx->pending_async); i++) {
406 spin_lock_init(&ctx->pending_async[i].lock);
407 INIT_LIST_HEAD(&ctx->pending_async[i].list);
408 atomic_set(&ctx->pending_async[i].cnt, 0);
409 }
Jens Axboe2b188cc2019-01-07 10:46:33 -0700410 spin_lock_init(&ctx->completion_lock);
Jens Axboedef596e2019-01-09 08:59:42 -0700411 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboe221c5eb2019-01-17 09:41:58 -0700412 INIT_LIST_HEAD(&ctx->cancel_list);
Jens Axboede0617e2019-04-06 21:51:27 -0600413 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -0600414 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700415 return ctx;
416}
417
Jens Axboede0617e2019-04-06 21:51:27 -0600418static inline bool io_sequence_defer(struct io_ring_ctx *ctx,
419 struct io_kiocb *req)
420{
Jens Axboe5262f562019-09-17 12:26:57 -0600421 /* timeout requests always honor sequence */
422 if (!(req->flags & REQ_F_TIMEOUT) &&
423 (req->flags & (REQ_F_IO_DRAIN|REQ_F_IO_DRAINED)) != REQ_F_IO_DRAIN)
Jens Axboede0617e2019-04-06 21:51:27 -0600424 return false;
425
Hristo Venev75b28af2019-08-26 17:23:46 +0000426 return req->sequence != ctx->cached_cq_tail + ctx->rings->sq_dropped;
Jens Axboede0617e2019-04-06 21:51:27 -0600427}
428
Jens Axboe5262f562019-09-17 12:26:57 -0600429static struct io_kiocb *__io_get_deferred_req(struct io_ring_ctx *ctx,
430 struct list_head *list)
Jens Axboede0617e2019-04-06 21:51:27 -0600431{
432 struct io_kiocb *req;
433
Jens Axboe5262f562019-09-17 12:26:57 -0600434 if (list_empty(list))
Jens Axboede0617e2019-04-06 21:51:27 -0600435 return NULL;
436
Jens Axboe5262f562019-09-17 12:26:57 -0600437 req = list_first_entry(list, struct io_kiocb, list);
Jens Axboede0617e2019-04-06 21:51:27 -0600438 if (!io_sequence_defer(ctx, req)) {
439 list_del_init(&req->list);
440 return req;
441 }
442
443 return NULL;
444}
445
Jens Axboe5262f562019-09-17 12:26:57 -0600446static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
447{
448 return __io_get_deferred_req(ctx, &ctx->defer_list);
449}
450
451static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx)
452{
453 return __io_get_deferred_req(ctx, &ctx->timeout_list);
454}
455
Jens Axboede0617e2019-04-06 21:51:27 -0600456static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700457{
Hristo Venev75b28af2019-08-26 17:23:46 +0000458 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700459
Hristo Venev75b28af2019-08-26 17:23:46 +0000460 if (ctx->cached_cq_tail != READ_ONCE(rings->cq.tail)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700461 /* order cqe stores with ring update */
Hristo Venev75b28af2019-08-26 17:23:46 +0000462 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700463
Jens Axboe2b188cc2019-01-07 10:46:33 -0700464 if (wq_has_sleeper(&ctx->cq_wait)) {
465 wake_up_interruptible(&ctx->cq_wait);
466 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
467 }
468 }
469}
470
Jens Axboe18d9be12019-09-10 09:13:05 -0600471static inline void io_queue_async_work(struct io_ring_ctx *ctx,
472 struct io_kiocb *req)
473{
Jens Axboe6cc47d12019-09-18 11:18:23 -0600474 int rw = 0;
Jens Axboe54a91f32019-09-10 09:15:04 -0600475
Jens Axboe6cc47d12019-09-18 11:18:23 -0600476 if (req->submit.sqe) {
477 switch (req->submit.sqe->opcode) {
478 case IORING_OP_WRITEV:
479 case IORING_OP_WRITE_FIXED:
480 rw = !(req->rw.ki_flags & IOCB_DIRECT);
481 break;
482 }
Jens Axboe54a91f32019-09-10 09:15:04 -0600483 }
484
485 queue_work(ctx->sqo_wq[rw], &req->work);
Jens Axboe18d9be12019-09-10 09:13:05 -0600486}
487
Jens Axboe5262f562019-09-17 12:26:57 -0600488static void io_kill_timeout(struct io_kiocb *req)
489{
490 int ret;
491
492 ret = hrtimer_try_to_cancel(&req->timeout.timer);
493 if (ret != -1) {
494 atomic_inc(&req->ctx->cq_timeouts);
495 list_del(&req->list);
496 io_cqring_fill_event(req->ctx, req->user_data, 0);
497 __io_free_req(req);
498 }
499}
500
501static void io_kill_timeouts(struct io_ring_ctx *ctx)
502{
503 struct io_kiocb *req, *tmp;
504
505 spin_lock_irq(&ctx->completion_lock);
506 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
507 io_kill_timeout(req);
508 spin_unlock_irq(&ctx->completion_lock);
509}
510
Jens Axboede0617e2019-04-06 21:51:27 -0600511static void io_commit_cqring(struct io_ring_ctx *ctx)
512{
513 struct io_kiocb *req;
514
Jens Axboe5262f562019-09-17 12:26:57 -0600515 while ((req = io_get_timeout_req(ctx)) != NULL)
516 io_kill_timeout(req);
517
Jens Axboede0617e2019-04-06 21:51:27 -0600518 __io_commit_cqring(ctx);
519
520 while ((req = io_get_deferred_req(ctx)) != NULL) {
Jackie Liu4fe2c962019-09-09 20:50:40 +0800521 if (req->flags & REQ_F_SHADOW_DRAIN) {
522 /* Just for drain, free it. */
523 __io_free_req(req);
524 continue;
525 }
Jens Axboede0617e2019-04-06 21:51:27 -0600526 req->flags |= REQ_F_IO_DRAINED;
Jens Axboe18d9be12019-09-10 09:13:05 -0600527 io_queue_async_work(ctx, req);
Jens Axboede0617e2019-04-06 21:51:27 -0600528 }
529}
530
Jens Axboe2b188cc2019-01-07 10:46:33 -0700531static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
532{
Hristo Venev75b28af2019-08-26 17:23:46 +0000533 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700534 unsigned tail;
535
536 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +0200537 /*
538 * writes to the cq entry need to come after reading head; the
539 * control dependency is enough as we're using WRITE_ONCE to
540 * fill the cq entry
541 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000542 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700543 return NULL;
544
545 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +0000546 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -0700547}
548
549static void io_cqring_fill_event(struct io_ring_ctx *ctx, u64 ki_user_data,
Jens Axboec71ffb62019-05-13 20:58:29 -0600550 long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700551{
552 struct io_uring_cqe *cqe;
553
554 /*
555 * If we can't get a cq entry, userspace overflowed the
556 * submission (by quite a lot). Increment the overflow count in
557 * the ring.
558 */
559 cqe = io_get_cqring(ctx);
560 if (cqe) {
561 WRITE_ONCE(cqe->user_data, ki_user_data);
562 WRITE_ONCE(cqe->res, res);
Jens Axboec71ffb62019-05-13 20:58:29 -0600563 WRITE_ONCE(cqe->flags, 0);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700564 } else {
Hristo Venev75b28af2019-08-26 17:23:46 +0000565 unsigned overflow = READ_ONCE(ctx->rings->cq_overflow);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700566
Hristo Venev75b28af2019-08-26 17:23:46 +0000567 WRITE_ONCE(ctx->rings->cq_overflow, overflow + 1);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700568 }
569}
570
Jens Axboe8c838782019-03-12 15:48:16 -0600571static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
572{
573 if (waitqueue_active(&ctx->wait))
574 wake_up(&ctx->wait);
575 if (waitqueue_active(&ctx->sqo_wait))
576 wake_up(&ctx->sqo_wait);
Jens Axboe9b402842019-04-11 11:45:41 -0600577 if (ctx->cq_ev_fd)
578 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -0600579}
580
581static void io_cqring_add_event(struct io_ring_ctx *ctx, u64 user_data,
Jens Axboec71ffb62019-05-13 20:58:29 -0600582 long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700583{
584 unsigned long flags;
585
586 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboec71ffb62019-05-13 20:58:29 -0600587 io_cqring_fill_event(ctx, user_data, res);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700588 io_commit_cqring(ctx);
589 spin_unlock_irqrestore(&ctx->completion_lock, flags);
590
Jens Axboe8c838782019-03-12 15:48:16 -0600591 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700592}
593
Jens Axboe2579f912019-01-09 09:10:43 -0700594static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
595 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700596{
Jens Axboefd6fab22019-03-14 16:30:06 -0600597 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700598 struct io_kiocb *req;
599
600 if (!percpu_ref_tryget(&ctx->refs))
601 return NULL;
602
Jens Axboe2579f912019-01-09 09:10:43 -0700603 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -0600604 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -0700605 if (unlikely(!req))
606 goto out;
607 } else if (!state->free_reqs) {
608 size_t sz;
609 int ret;
610
611 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -0600612 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
613
614 /*
615 * Bulk alloc is all-or-nothing. If we fail to get a batch,
616 * retry single alloc to be on the safe side.
617 */
618 if (unlikely(ret <= 0)) {
619 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
620 if (!state->reqs[0])
621 goto out;
622 ret = 1;
623 }
Jens Axboe2579f912019-01-09 09:10:43 -0700624 state->free_reqs = ret - 1;
625 state->cur_req = 1;
626 req = state->reqs[0];
627 } else {
628 req = state->reqs[state->cur_req];
629 state->free_reqs--;
630 state->cur_req++;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700631 }
632
Jens Axboe60c112b2019-06-21 10:20:18 -0600633 req->file = NULL;
Jens Axboe2579f912019-01-09 09:10:43 -0700634 req->ctx = ctx;
635 req->flags = 0;
Jens Axboee65ef562019-03-12 10:16:44 -0600636 /* one is dropped after submission, the other at completion */
637 refcount_set(&req->refs, 2);
Jens Axboe9e645e112019-05-10 16:07:28 -0600638 req->result = 0;
Jens Axboe2579f912019-01-09 09:10:43 -0700639 return req;
640out:
Pavel Begunkov6805b322019-10-08 02:18:42 +0300641 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700642 return NULL;
643}
644
Jens Axboedef596e2019-01-09 08:59:42 -0700645static void io_free_req_many(struct io_ring_ctx *ctx, void **reqs, int *nr)
646{
647 if (*nr) {
648 kmem_cache_free_bulk(req_cachep, *nr, reqs);
Pavel Begunkov6805b322019-10-08 02:18:42 +0300649 percpu_ref_put_many(&ctx->refs, *nr);
Jens Axboedef596e2019-01-09 08:59:42 -0700650 *nr = 0;
651 }
652}
653
Jens Axboe9e645e112019-05-10 16:07:28 -0600654static void __io_free_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700655{
Jens Axboe09bb8392019-03-13 12:39:28 -0600656 if (req->file && !(req->flags & REQ_F_FIXED_FILE))
657 fput(req->file);
Pavel Begunkov6805b322019-10-08 02:18:42 +0300658 percpu_ref_put(&req->ctx->refs);
Jens Axboee65ef562019-03-12 10:16:44 -0600659 kmem_cache_free(req_cachep, req);
660}
661
Jens Axboe9e645e112019-05-10 16:07:28 -0600662static void io_req_link_next(struct io_kiocb *req)
663{
664 struct io_kiocb *nxt;
665
666 /*
667 * The list should never be empty when we are called here. But could
668 * potentially happen if the chain is messed up, check to be on the
669 * safe side.
670 */
671 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, list);
672 if (nxt) {
673 list_del(&nxt->list);
674 if (!list_empty(&req->link_list)) {
675 INIT_LIST_HEAD(&nxt->link_list);
676 list_splice(&req->link_list, &nxt->link_list);
677 nxt->flags |= REQ_F_LINK;
678 }
679
Zhengyuan Liuf7b76ac2019-07-16 23:26:14 +0800680 nxt->flags |= REQ_F_LINK_DONE;
Jens Axboe9e645e112019-05-10 16:07:28 -0600681 INIT_WORK(&nxt->work, io_sq_wq_submit_work);
Jens Axboe18d9be12019-09-10 09:13:05 -0600682 io_queue_async_work(req->ctx, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -0600683 }
684}
685
686/*
687 * Called if REQ_F_LINK is set, and we fail the head request
688 */
689static void io_fail_links(struct io_kiocb *req)
690{
691 struct io_kiocb *link;
692
693 while (!list_empty(&req->link_list)) {
694 link = list_first_entry(&req->link_list, struct io_kiocb, list);
695 list_del(&link->list);
696
697 io_cqring_add_event(req->ctx, link->user_data, -ECANCELED);
698 __io_free_req(link);
699 }
700}
701
702static void io_free_req(struct io_kiocb *req)
703{
704 /*
705 * If LINK is set, we have dependent requests in this chain. If we
706 * didn't fail this request, queue the first one up, moving any other
707 * dependencies to the next request. In case of failure, fail the rest
708 * of the chain.
709 */
710 if (req->flags & REQ_F_LINK) {
711 if (req->flags & REQ_F_FAIL_LINK)
712 io_fail_links(req);
713 else
714 io_req_link_next(req);
715 }
716
717 __io_free_req(req);
718}
719
Jens Axboee65ef562019-03-12 10:16:44 -0600720static void io_put_req(struct io_kiocb *req)
721{
722 if (refcount_dec_and_test(&req->refs))
723 io_free_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700724}
725
Hristo Venev75b28af2019-08-26 17:23:46 +0000726static unsigned io_cqring_events(struct io_rings *rings)
Jens Axboea3a0e432019-08-20 11:03:11 -0600727{
728 /* See comment at the top of this file */
729 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +0000730 return READ_ONCE(rings->cq.tail) - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -0600731}
732
Jens Axboedef596e2019-01-09 08:59:42 -0700733/*
734 * Find and free completed poll iocbs
735 */
736static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
737 struct list_head *done)
738{
739 void *reqs[IO_IOPOLL_BATCH];
740 struct io_kiocb *req;
Jens Axboe09bb8392019-03-13 12:39:28 -0600741 int to_free;
Jens Axboedef596e2019-01-09 08:59:42 -0700742
Jens Axboe09bb8392019-03-13 12:39:28 -0600743 to_free = 0;
Jens Axboedef596e2019-01-09 08:59:42 -0700744 while (!list_empty(done)) {
745 req = list_first_entry(done, struct io_kiocb, list);
746 list_del(&req->list);
747
Jens Axboe9e645e112019-05-10 16:07:28 -0600748 io_cqring_fill_event(ctx, req->user_data, req->result);
Jens Axboedef596e2019-01-09 08:59:42 -0700749 (*nr_events)++;
750
Jens Axboe09bb8392019-03-13 12:39:28 -0600751 if (refcount_dec_and_test(&req->refs)) {
752 /* If we're not using fixed files, we have to pair the
753 * completion part with the file put. Use regular
754 * completions for those, only batch free for fixed
Jens Axboe9e645e112019-05-10 16:07:28 -0600755 * file and non-linked commands.
Jens Axboe09bb8392019-03-13 12:39:28 -0600756 */
Jens Axboe9e645e112019-05-10 16:07:28 -0600757 if ((req->flags & (REQ_F_FIXED_FILE|REQ_F_LINK)) ==
758 REQ_F_FIXED_FILE) {
Jens Axboe09bb8392019-03-13 12:39:28 -0600759 reqs[to_free++] = req;
760 if (to_free == ARRAY_SIZE(reqs))
761 io_free_req_many(ctx, reqs, &to_free);
Jens Axboe6b063142019-01-10 22:13:58 -0700762 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -0600763 io_free_req(req);
Jens Axboe6b063142019-01-10 22:13:58 -0700764 }
Jens Axboe9a56a232019-01-09 09:06:50 -0700765 }
Jens Axboedef596e2019-01-09 08:59:42 -0700766 }
Jens Axboedef596e2019-01-09 08:59:42 -0700767
Jens Axboe09bb8392019-03-13 12:39:28 -0600768 io_commit_cqring(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -0700769 io_free_req_many(ctx, reqs, &to_free);
770}
771
772static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
773 long min)
774{
775 struct io_kiocb *req, *tmp;
776 LIST_HEAD(done);
777 bool spin;
778 int ret;
779
780 /*
781 * Only spin for completions if we don't have multiple devices hanging
782 * off our complete list, and we're under the requested amount.
783 */
784 spin = !ctx->poll_multi_file && *nr_events < min;
785
786 ret = 0;
787 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
788 struct kiocb *kiocb = &req->rw;
789
790 /*
791 * Move completed entries to our local list. If we find a
792 * request that requires polling, break out and complete
793 * the done list first, if we have entries there.
794 */
795 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
796 list_move_tail(&req->list, &done);
797 continue;
798 }
799 if (!list_empty(&done))
800 break;
801
802 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
803 if (ret < 0)
804 break;
805
806 if (ret && spin)
807 spin = false;
808 ret = 0;
809 }
810
811 if (!list_empty(&done))
812 io_iopoll_complete(ctx, nr_events, &done);
813
814 return ret;
815}
816
817/*
818 * Poll for a mininum of 'min' events. Note that if min == 0 we consider that a
819 * non-spinning poll check - we'll still enter the driver poll loop, but only
820 * as a non-spinning completion check.
821 */
822static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
823 long min)
824{
Jens Axboe08f54392019-08-21 22:19:11 -0600825 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -0700826 int ret;
827
828 ret = io_do_iopoll(ctx, nr_events, min);
829 if (ret < 0)
830 return ret;
831 if (!min || *nr_events >= min)
832 return 0;
833 }
834
835 return 1;
836}
837
838/*
839 * We can't just wait for polled events to come to us, we have to actively
840 * find and complete them.
841 */
842static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
843{
844 if (!(ctx->flags & IORING_SETUP_IOPOLL))
845 return;
846
847 mutex_lock(&ctx->uring_lock);
848 while (!list_empty(&ctx->poll_list)) {
849 unsigned int nr_events = 0;
850
851 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -0600852
853 /*
854 * Ensure we allow local-to-the-cpu processing to take place,
855 * in this case we need to ensure that we reap all events.
856 */
857 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -0700858 }
859 mutex_unlock(&ctx->uring_lock);
860}
861
862static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
863 long min)
864{
Jens Axboe500f9fb2019-08-19 12:15:59 -0600865 int iters, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -0700866
Jens Axboe500f9fb2019-08-19 12:15:59 -0600867 /*
868 * We disallow the app entering submit/complete with polling, but we
869 * still need to lock the ring to prevent racing with polled issue
870 * that got punted to a workqueue.
871 */
872 mutex_lock(&ctx->uring_lock);
873
874 iters = 0;
Jens Axboedef596e2019-01-09 08:59:42 -0700875 do {
876 int tmin = 0;
877
Jens Axboe500f9fb2019-08-19 12:15:59 -0600878 /*
Jens Axboea3a0e432019-08-20 11:03:11 -0600879 * Don't enter poll loop if we already have events pending.
880 * If we do, we can potentially be spinning for commands that
881 * already triggered a CQE (eg in error).
882 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000883 if (io_cqring_events(ctx->rings))
Jens Axboea3a0e432019-08-20 11:03:11 -0600884 break;
885
886 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -0600887 * If a submit got punted to a workqueue, we can have the
888 * application entering polling for a command before it gets
889 * issued. That app will hold the uring_lock for the duration
890 * of the poll right here, so we need to take a breather every
891 * now and then to ensure that the issue has a chance to add
892 * the poll to the issued list. Otherwise we can spin here
893 * forever, while the workqueue is stuck trying to acquire the
894 * very same mutex.
895 */
896 if (!(++iters & 7)) {
897 mutex_unlock(&ctx->uring_lock);
898 mutex_lock(&ctx->uring_lock);
899 }
900
Jens Axboedef596e2019-01-09 08:59:42 -0700901 if (*nr_events < min)
902 tmin = min - *nr_events;
903
904 ret = io_iopoll_getevents(ctx, nr_events, tmin);
905 if (ret <= 0)
906 break;
907 ret = 0;
908 } while (min && !*nr_events && !need_resched());
909
Jens Axboe500f9fb2019-08-19 12:15:59 -0600910 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -0700911 return ret;
912}
913
Jens Axboe2b188cc2019-01-07 10:46:33 -0700914static void kiocb_end_write(struct kiocb *kiocb)
915{
916 if (kiocb->ki_flags & IOCB_WRITE) {
917 struct inode *inode = file_inode(kiocb->ki_filp);
918
919 /*
920 * Tell lockdep we inherited freeze protection from submission
921 * thread.
922 */
923 if (S_ISREG(inode->i_mode))
924 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
925 file_end_write(kiocb->ki_filp);
926 }
927}
928
929static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
930{
931 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
932
933 kiocb_end_write(kiocb);
934
Jens Axboe9e645e112019-05-10 16:07:28 -0600935 if ((req->flags & REQ_F_LINK) && res != req->result)
936 req->flags |= REQ_F_FAIL_LINK;
Jens Axboec71ffb62019-05-13 20:58:29 -0600937 io_cqring_add_event(req->ctx, req->user_data, res);
Jens Axboee65ef562019-03-12 10:16:44 -0600938 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700939}
940
Jens Axboedef596e2019-01-09 08:59:42 -0700941static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
942{
943 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
944
945 kiocb_end_write(kiocb);
946
Jens Axboe9e645e112019-05-10 16:07:28 -0600947 if ((req->flags & REQ_F_LINK) && res != req->result)
948 req->flags |= REQ_F_FAIL_LINK;
949 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -0700950 if (res != -EAGAIN)
951 req->flags |= REQ_F_IOPOLL_COMPLETED;
952}
953
954/*
955 * After the iocb has been issued, it's safe to be found on the poll list.
956 * Adding the kiocb to the list AFTER submission ensures that we don't
957 * find it from a io_iopoll_getevents() thread before the issuer is done
958 * accessing the kiocb cookie.
959 */
960static void io_iopoll_req_issued(struct io_kiocb *req)
961{
962 struct io_ring_ctx *ctx = req->ctx;
963
964 /*
965 * Track whether we have multiple files in our lists. This will impact
966 * how we do polling eventually, not spinning if we're on potentially
967 * different devices.
968 */
969 if (list_empty(&ctx->poll_list)) {
970 ctx->poll_multi_file = false;
971 } else if (!ctx->poll_multi_file) {
972 struct io_kiocb *list_req;
973
974 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
975 list);
976 if (list_req->rw.ki_filp != req->rw.ki_filp)
977 ctx->poll_multi_file = true;
978 }
979
980 /*
981 * For fast devices, IO may have already completed. If it has, add
982 * it to the front so we find it first.
983 */
984 if (req->flags & REQ_F_IOPOLL_COMPLETED)
985 list_add(&req->list, &ctx->poll_list);
986 else
987 list_add_tail(&req->list, &ctx->poll_list);
988}
989
Jens Axboe3d6770f2019-04-13 11:50:54 -0600990static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -0700991{
Jens Axboe3d6770f2019-04-13 11:50:54 -0600992 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -0700993 int diff = state->has_refs - state->used_refs;
994
995 if (diff)
996 fput_many(state->file, diff);
997 state->file = NULL;
998 }
999}
1000
1001/*
1002 * Get as many references to a file as we have IOs left in this submission,
1003 * assuming most submissions are for one file, or at least that each file
1004 * has more than one submission.
1005 */
1006static struct file *io_file_get(struct io_submit_state *state, int fd)
1007{
1008 if (!state)
1009 return fget(fd);
1010
1011 if (state->file) {
1012 if (state->fd == fd) {
1013 state->used_refs++;
1014 state->ios_left--;
1015 return state->file;
1016 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06001017 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07001018 }
1019 state->file = fget_many(fd, state->ios_left);
1020 if (!state->file)
1021 return NULL;
1022
1023 state->fd = fd;
1024 state->has_refs = state->ios_left;
1025 state->used_refs = 1;
1026 state->ios_left--;
1027 return state->file;
1028}
1029
Jens Axboe2b188cc2019-01-07 10:46:33 -07001030/*
1031 * If we tracked the file through the SCM inflight mechanism, we could support
1032 * any file. For now, just ensure that anything potentially problematic is done
1033 * inline.
1034 */
1035static bool io_file_supports_async(struct file *file)
1036{
1037 umode_t mode = file_inode(file)->i_mode;
1038
1039 if (S_ISBLK(mode) || S_ISCHR(mode))
1040 return true;
1041 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
1042 return true;
1043
1044 return false;
1045}
1046
Jens Axboe6c271ce2019-01-10 11:22:30 -07001047static int io_prep_rw(struct io_kiocb *req, const struct sqe_submit *s,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001048 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001049{
Jens Axboe6c271ce2019-01-10 11:22:30 -07001050 const struct io_uring_sqe *sqe = s->sqe;
Jens Axboedef596e2019-01-09 08:59:42 -07001051 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001052 struct kiocb *kiocb = &req->rw;
Jens Axboe09bb8392019-03-13 12:39:28 -06001053 unsigned ioprio;
1054 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001055
Jens Axboe09bb8392019-03-13 12:39:28 -06001056 if (!req->file)
1057 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001058
Jens Axboe09bb8392019-03-13 12:39:28 -06001059 if (force_nonblock && !io_file_supports_async(req->file))
1060 force_nonblock = false;
Jens Axboe6b063142019-01-10 22:13:58 -07001061
Jens Axboe2b188cc2019-01-07 10:46:33 -07001062 kiocb->ki_pos = READ_ONCE(sqe->off);
1063 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
1064 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
1065
1066 ioprio = READ_ONCE(sqe->ioprio);
1067 if (ioprio) {
1068 ret = ioprio_check_cap(ioprio);
1069 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06001070 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001071
1072 kiocb->ki_ioprio = ioprio;
1073 } else
1074 kiocb->ki_ioprio = get_current_ioprio();
1075
1076 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
1077 if (unlikely(ret))
Jens Axboe09bb8392019-03-13 12:39:28 -06001078 return ret;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001079
1080 /* don't allow async punt if RWF_NOWAIT was requested */
1081 if (kiocb->ki_flags & IOCB_NOWAIT)
1082 req->flags |= REQ_F_NOWAIT;
1083
1084 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001085 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001086
Jens Axboedef596e2019-01-09 08:59:42 -07001087 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07001088 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
1089 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06001090 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001091
Jens Axboedef596e2019-01-09 08:59:42 -07001092 kiocb->ki_flags |= IOCB_HIPRI;
1093 kiocb->ki_complete = io_complete_rw_iopoll;
1094 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001095 if (kiocb->ki_flags & IOCB_HIPRI)
1096 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07001097 kiocb->ki_complete = io_complete_rw;
1098 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001099 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001100}
1101
1102static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
1103{
1104 switch (ret) {
1105 case -EIOCBQUEUED:
1106 break;
1107 case -ERESTARTSYS:
1108 case -ERESTARTNOINTR:
1109 case -ERESTARTNOHAND:
1110 case -ERESTART_RESTARTBLOCK:
1111 /*
1112 * We can't just restart the syscall, since previously
1113 * submitted sqes may already be in progress. Just fail this
1114 * IO with EINTR.
1115 */
1116 ret = -EINTR;
1117 /* fall through */
1118 default:
1119 kiocb->ki_complete(kiocb, ret, 0);
1120 }
1121}
1122
Jens Axboeedafcce2019-01-09 09:16:05 -07001123static int io_import_fixed(struct io_ring_ctx *ctx, int rw,
1124 const struct io_uring_sqe *sqe,
1125 struct iov_iter *iter)
1126{
1127 size_t len = READ_ONCE(sqe->len);
1128 struct io_mapped_ubuf *imu;
1129 unsigned index, buf_index;
1130 size_t offset;
1131 u64 buf_addr;
1132
1133 /* attempt to use fixed buffers without having provided iovecs */
1134 if (unlikely(!ctx->user_bufs))
1135 return -EFAULT;
1136
1137 buf_index = READ_ONCE(sqe->buf_index);
1138 if (unlikely(buf_index >= ctx->nr_user_bufs))
1139 return -EFAULT;
1140
1141 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
1142 imu = &ctx->user_bufs[index];
1143 buf_addr = READ_ONCE(sqe->addr);
1144
1145 /* overflow */
1146 if (buf_addr + len < buf_addr)
1147 return -EFAULT;
1148 /* not inside the mapped region */
1149 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
1150 return -EFAULT;
1151
1152 /*
1153 * May not be a start of buffer, set size appropriately
1154 * and advance us to the beginning.
1155 */
1156 offset = buf_addr - imu->ubuf;
1157 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06001158
1159 if (offset) {
1160 /*
1161 * Don't use iov_iter_advance() here, as it's really slow for
1162 * using the latter parts of a big fixed buffer - it iterates
1163 * over each segment manually. We can cheat a bit here, because
1164 * we know that:
1165 *
1166 * 1) it's a BVEC iter, we set it up
1167 * 2) all bvecs are PAGE_SIZE in size, except potentially the
1168 * first and last bvec
1169 *
1170 * So just find our index, and adjust the iterator afterwards.
1171 * If the offset is within the first bvec (or the whole first
1172 * bvec, just use iov_iter_advance(). This makes it easier
1173 * since we can just skip the first segment, which may not
1174 * be PAGE_SIZE aligned.
1175 */
1176 const struct bio_vec *bvec = imu->bvec;
1177
1178 if (offset <= bvec->bv_len) {
1179 iov_iter_advance(iter, offset);
1180 } else {
1181 unsigned long seg_skip;
1182
1183 /* skip first vec */
1184 offset -= bvec->bv_len;
1185 seg_skip = 1 + (offset >> PAGE_SHIFT);
1186
1187 iter->bvec = bvec + seg_skip;
1188 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02001189 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001190 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001191 }
1192 }
1193
Jens Axboeedafcce2019-01-09 09:16:05 -07001194 return 0;
1195}
1196
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001197static ssize_t io_import_iovec(struct io_ring_ctx *ctx, int rw,
1198 const struct sqe_submit *s, struct iovec **iovec,
1199 struct iov_iter *iter)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001200{
1201 const struct io_uring_sqe *sqe = s->sqe;
1202 void __user *buf = u64_to_user_ptr(READ_ONCE(sqe->addr));
1203 size_t sqe_len = READ_ONCE(sqe->len);
Jens Axboeedafcce2019-01-09 09:16:05 -07001204 u8 opcode;
1205
1206 /*
1207 * We're reading ->opcode for the second time, but the first read
1208 * doesn't care whether it's _FIXED or not, so it doesn't matter
1209 * whether ->opcode changes concurrently. The first read does care
1210 * about whether it is a READ or a WRITE, so we don't trust this read
1211 * for that purpose and instead let the caller pass in the read/write
1212 * flag.
1213 */
1214 opcode = READ_ONCE(sqe->opcode);
1215 if (opcode == IORING_OP_READ_FIXED ||
1216 opcode == IORING_OP_WRITE_FIXED) {
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001217 ssize_t ret = io_import_fixed(ctx, rw, sqe, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07001218 *iovec = NULL;
1219 return ret;
1220 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001221
1222 if (!s->has_user)
1223 return -EFAULT;
1224
1225#ifdef CONFIG_COMPAT
1226 if (ctx->compat)
1227 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
1228 iovec, iter);
1229#endif
1230
1231 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
1232}
1233
Jens Axboe6d5d5ac2019-09-11 10:16:13 -06001234static inline bool io_should_merge(struct async_list *al, struct kiocb *kiocb)
1235{
1236 if (al->file == kiocb->ki_filp) {
1237 off_t start, end;
1238
1239 /*
1240 * Allow merging if we're anywhere in the range of the same
1241 * page. Generally this happens for sub-page reads or writes,
1242 * and it's beneficial to allow the first worker to bring the
1243 * page in and the piggy backed work can then work on the
1244 * cached page.
1245 */
1246 start = al->io_start & PAGE_MASK;
1247 end = (al->io_start + al->io_len + PAGE_SIZE - 1) & PAGE_MASK;
1248 if (kiocb->ki_pos >= start && kiocb->ki_pos <= end)
1249 return true;
1250 }
1251
1252 al->file = NULL;
1253 return false;
1254}
1255
Jens Axboe31b51512019-01-18 22:56:34 -07001256/*
1257 * Make a note of the last file/offset/direction we punted to async
1258 * context. We'll use this information to see if we can piggy back a
1259 * sequential request onto the previous one, if it's still hasn't been
1260 * completed by the async worker.
1261 */
1262static void io_async_list_note(int rw, struct io_kiocb *req, size_t len)
1263{
1264 struct async_list *async_list = &req->ctx->pending_async[rw];
1265 struct kiocb *kiocb = &req->rw;
1266 struct file *filp = kiocb->ki_filp;
Jens Axboe31b51512019-01-18 22:56:34 -07001267
Jens Axboe6d5d5ac2019-09-11 10:16:13 -06001268 if (io_should_merge(async_list, kiocb)) {
Zhengyuan Liu9310a7ba2019-07-22 10:23:27 +08001269 unsigned long max_bytes;
Jens Axboe31b51512019-01-18 22:56:34 -07001270
1271 /* Use 8x RA size as a decent limiter for both reads/writes */
Zhengyuan Liu9310a7ba2019-07-22 10:23:27 +08001272 max_bytes = filp->f_ra.ra_pages << (PAGE_SHIFT + 3);
1273 if (!max_bytes)
1274 max_bytes = VM_READAHEAD_PAGES << (PAGE_SHIFT + 3);
Jens Axboe31b51512019-01-18 22:56:34 -07001275
Zhengyuan Liu9310a7ba2019-07-22 10:23:27 +08001276 /* If max len are exceeded, reset the state */
1277 if (async_list->io_len + len <= max_bytes) {
Jens Axboe31b51512019-01-18 22:56:34 -07001278 req->flags |= REQ_F_SEQ_PREV;
Zhengyuan Liu9310a7ba2019-07-22 10:23:27 +08001279 async_list->io_len += len;
Jens Axboe31b51512019-01-18 22:56:34 -07001280 } else {
Jens Axboe6d5d5ac2019-09-11 10:16:13 -06001281 async_list->file = NULL;
Jens Axboe31b51512019-01-18 22:56:34 -07001282 }
1283 }
1284
1285 /* New file? Reset state. */
1286 if (async_list->file != filp) {
Jens Axboe6d5d5ac2019-09-11 10:16:13 -06001287 async_list->io_start = kiocb->ki_pos;
1288 async_list->io_len = len;
Jens Axboe31b51512019-01-18 22:56:34 -07001289 async_list->file = filp;
1290 }
Jens Axboe31b51512019-01-18 22:56:34 -07001291}
1292
Jens Axboe32960612019-09-23 11:05:34 -06001293/*
1294 * For files that don't have ->read_iter() and ->write_iter(), handle them
1295 * by looping over ->read() or ->write() manually.
1296 */
1297static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
1298 struct iov_iter *iter)
1299{
1300 ssize_t ret = 0;
1301
1302 /*
1303 * Don't support polled IO through this interface, and we can't
1304 * support non-blocking either. For the latter, this just causes
1305 * the kiocb to be handled from an async context.
1306 */
1307 if (kiocb->ki_flags & IOCB_HIPRI)
1308 return -EOPNOTSUPP;
1309 if (kiocb->ki_flags & IOCB_NOWAIT)
1310 return -EAGAIN;
1311
1312 while (iov_iter_count(iter)) {
1313 struct iovec iovec = iov_iter_iovec(iter);
1314 ssize_t nr;
1315
1316 if (rw == READ) {
1317 nr = file->f_op->read(file, iovec.iov_base,
1318 iovec.iov_len, &kiocb->ki_pos);
1319 } else {
1320 nr = file->f_op->write(file, iovec.iov_base,
1321 iovec.iov_len, &kiocb->ki_pos);
1322 }
1323
1324 if (nr < 0) {
1325 if (!ret)
1326 ret = nr;
1327 break;
1328 }
1329 ret += nr;
1330 if (nr != iovec.iov_len)
1331 break;
1332 iov_iter_advance(iter, nr);
1333 }
1334
1335 return ret;
1336}
1337
Jens Axboee0c5c572019-03-12 10:18:47 -06001338static int io_read(struct io_kiocb *req, const struct sqe_submit *s,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001339 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001340{
1341 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1342 struct kiocb *kiocb = &req->rw;
1343 struct iov_iter iter;
1344 struct file *file;
Jens Axboe31b51512019-01-18 22:56:34 -07001345 size_t iov_count;
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001346 ssize_t read_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001347
Jens Axboe8358e3a2019-04-23 08:17:58 -06001348 ret = io_prep_rw(req, s, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001349 if (ret)
1350 return ret;
1351 file = kiocb->ki_filp;
1352
Jens Axboe2b188cc2019-01-07 10:46:33 -07001353 if (unlikely(!(file->f_mode & FMODE_READ)))
Jens Axboe09bb8392019-03-13 12:39:28 -06001354 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001355
1356 ret = io_import_iovec(req->ctx, READ, s, &iovec, &iter);
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001357 if (ret < 0)
Jens Axboe09bb8392019-03-13 12:39:28 -06001358 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001359
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001360 read_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06001361 if (req->flags & REQ_F_LINK)
1362 req->result = read_size;
1363
Jens Axboe31b51512019-01-18 22:56:34 -07001364 iov_count = iov_iter_count(&iter);
1365 ret = rw_verify_area(READ, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001366 if (!ret) {
1367 ssize_t ret2;
1368
Jens Axboe32960612019-09-23 11:05:34 -06001369 if (file->f_op->read_iter)
1370 ret2 = call_read_iter(file, kiocb, &iter);
1371 else
1372 ret2 = loop_rw_iter(READ, file, kiocb, &iter);
1373
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001374 /*
1375 * In case of a short read, punt to async. This can happen
1376 * if we have data partially cached. Alternatively we can
1377 * return the short read, in which case the application will
1378 * need to issue another SQE and wait for it. That SQE will
1379 * need async punt anyway, so it's more efficient to do it
1380 * here.
1381 */
1382 if (force_nonblock && ret2 > 0 && ret2 < read_size)
1383 ret2 = -EAGAIN;
1384 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboe31b51512019-01-18 22:56:34 -07001385 if (!force_nonblock || ret2 != -EAGAIN) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001386 io_rw_done(kiocb, ret2);
Jens Axboe31b51512019-01-18 22:56:34 -07001387 } else {
1388 /*
1389 * If ->needs_lock is true, we're already in async
1390 * context.
1391 */
1392 if (!s->needs_lock)
1393 io_async_list_note(READ, req, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001394 ret = -EAGAIN;
Jens Axboe31b51512019-01-18 22:56:34 -07001395 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001396 }
1397 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001398 return ret;
1399}
1400
Jens Axboee0c5c572019-03-12 10:18:47 -06001401static int io_write(struct io_kiocb *req, const struct sqe_submit *s,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001402 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001403{
1404 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1405 struct kiocb *kiocb = &req->rw;
1406 struct iov_iter iter;
1407 struct file *file;
Jens Axboe31b51512019-01-18 22:56:34 -07001408 size_t iov_count;
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001409 ssize_t ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001410
Jens Axboe8358e3a2019-04-23 08:17:58 -06001411 ret = io_prep_rw(req, s, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001412 if (ret)
1413 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001414
Jens Axboe2b188cc2019-01-07 10:46:33 -07001415 file = kiocb->ki_filp;
1416 if (unlikely(!(file->f_mode & FMODE_WRITE)))
Jens Axboe09bb8392019-03-13 12:39:28 -06001417 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001418
1419 ret = io_import_iovec(req->ctx, WRITE, s, &iovec, &iter);
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001420 if (ret < 0)
Jens Axboe09bb8392019-03-13 12:39:28 -06001421 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001422
Jens Axboe9e645e112019-05-10 16:07:28 -06001423 if (req->flags & REQ_F_LINK)
1424 req->result = ret;
1425
Jens Axboe31b51512019-01-18 22:56:34 -07001426 iov_count = iov_iter_count(&iter);
1427
1428 ret = -EAGAIN;
1429 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT)) {
1430 /* If ->needs_lock is true, we're already in async context. */
1431 if (!s->needs_lock)
1432 io_async_list_note(WRITE, req, iov_count);
1433 goto out_free;
1434 }
1435
1436 ret = rw_verify_area(WRITE, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001437 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01001438 ssize_t ret2;
1439
Jens Axboe2b188cc2019-01-07 10:46:33 -07001440 /*
1441 * Open-code file_start_write here to grab freeze protection,
1442 * which will be released by another thread in
1443 * io_complete_rw(). Fool lockdep by telling it the lock got
1444 * released so that it doesn't complain about the held lock when
1445 * we return to userspace.
1446 */
1447 if (S_ISREG(file_inode(file)->i_mode)) {
1448 __sb_start_write(file_inode(file)->i_sb,
1449 SB_FREEZE_WRITE, true);
1450 __sb_writers_release(file_inode(file)->i_sb,
1451 SB_FREEZE_WRITE);
1452 }
1453 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01001454
Jens Axboe32960612019-09-23 11:05:34 -06001455 if (file->f_op->write_iter)
1456 ret2 = call_write_iter(file, kiocb, &iter);
1457 else
1458 ret2 = loop_rw_iter(WRITE, file, kiocb, &iter);
Roman Penyaev9bf79332019-03-25 20:09:24 +01001459 if (!force_nonblock || ret2 != -EAGAIN) {
1460 io_rw_done(kiocb, ret2);
1461 } else {
1462 /*
1463 * If ->needs_lock is true, we're already in async
1464 * context.
1465 */
1466 if (!s->needs_lock)
1467 io_async_list_note(WRITE, req, iov_count);
1468 ret = -EAGAIN;
1469 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001470 }
Jens Axboe31b51512019-01-18 22:56:34 -07001471out_free:
Jens Axboe2b188cc2019-01-07 10:46:33 -07001472 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001473 return ret;
1474}
1475
1476/*
1477 * IORING_OP_NOP just posts a completion event, nothing else.
1478 */
1479static int io_nop(struct io_kiocb *req, u64 user_data)
1480{
1481 struct io_ring_ctx *ctx = req->ctx;
1482 long err = 0;
1483
Jens Axboedef596e2019-01-09 08:59:42 -07001484 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1485 return -EINVAL;
1486
Jens Axboec71ffb62019-05-13 20:58:29 -06001487 io_cqring_add_event(ctx, user_data, err);
Jens Axboee65ef562019-03-12 10:16:44 -06001488 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001489 return 0;
1490}
1491
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001492static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1493{
Jens Axboe6b063142019-01-10 22:13:58 -07001494 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001495
Jens Axboe09bb8392019-03-13 12:39:28 -06001496 if (!req->file)
1497 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001498
Jens Axboe6b063142019-01-10 22:13:58 -07001499 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07001500 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07001501 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001502 return -EINVAL;
1503
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001504 return 0;
1505}
1506
1507static int io_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1508 bool force_nonblock)
1509{
1510 loff_t sqe_off = READ_ONCE(sqe->off);
1511 loff_t sqe_len = READ_ONCE(sqe->len);
1512 loff_t end = sqe_off + sqe_len;
1513 unsigned fsync_flags;
1514 int ret;
1515
1516 fsync_flags = READ_ONCE(sqe->fsync_flags);
1517 if (unlikely(fsync_flags & ~IORING_FSYNC_DATASYNC))
1518 return -EINVAL;
1519
1520 ret = io_prep_fsync(req, sqe);
1521 if (ret)
1522 return ret;
1523
1524 /* fsync always requires a blocking context */
1525 if (force_nonblock)
1526 return -EAGAIN;
1527
1528 ret = vfs_fsync_range(req->rw.ki_filp, sqe_off,
1529 end > 0 ? end : LLONG_MAX,
1530 fsync_flags & IORING_FSYNC_DATASYNC);
1531
Jens Axboe9e645e112019-05-10 16:07:28 -06001532 if (ret < 0 && (req->flags & REQ_F_LINK))
1533 req->flags |= REQ_F_FAIL_LINK;
Jens Axboec71ffb62019-05-13 20:58:29 -06001534 io_cqring_add_event(req->ctx, sqe->user_data, ret);
Jens Axboee65ef562019-03-12 10:16:44 -06001535 io_put_req(req);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001536 return 0;
1537}
1538
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001539static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1540{
1541 struct io_ring_ctx *ctx = req->ctx;
1542 int ret = 0;
1543
1544 if (!req->file)
1545 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001546
1547 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1548 return -EINVAL;
1549 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
1550 return -EINVAL;
1551
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001552 return ret;
1553}
1554
1555static int io_sync_file_range(struct io_kiocb *req,
1556 const struct io_uring_sqe *sqe,
1557 bool force_nonblock)
1558{
1559 loff_t sqe_off;
1560 loff_t sqe_len;
1561 unsigned flags;
1562 int ret;
1563
1564 ret = io_prep_sfr(req, sqe);
1565 if (ret)
1566 return ret;
1567
1568 /* sync_file_range always requires a blocking context */
1569 if (force_nonblock)
1570 return -EAGAIN;
1571
1572 sqe_off = READ_ONCE(sqe->off);
1573 sqe_len = READ_ONCE(sqe->len);
1574 flags = READ_ONCE(sqe->sync_range_flags);
1575
1576 ret = sync_file_range(req->rw.ki_filp, sqe_off, sqe_len, flags);
1577
Jens Axboe9e645e112019-05-10 16:07:28 -06001578 if (ret < 0 && (req->flags & REQ_F_LINK))
1579 req->flags |= REQ_F_FAIL_LINK;
Jens Axboec71ffb62019-05-13 20:58:29 -06001580 io_cqring_add_event(req->ctx, sqe->user_data, ret);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001581 io_put_req(req);
1582 return 0;
1583}
1584
Jens Axboe0fa03c62019-04-19 13:34:07 -06001585#if defined(CONFIG_NET)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001586static int io_send_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1587 bool force_nonblock,
1588 long (*fn)(struct socket *, struct user_msghdr __user *,
1589 unsigned int))
1590{
Jens Axboe0fa03c62019-04-19 13:34:07 -06001591 struct socket *sock;
1592 int ret;
1593
1594 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1595 return -EINVAL;
1596
1597 sock = sock_from_file(req->file, &ret);
1598 if (sock) {
1599 struct user_msghdr __user *msg;
1600 unsigned flags;
1601
1602 flags = READ_ONCE(sqe->msg_flags);
1603 if (flags & MSG_DONTWAIT)
1604 req->flags |= REQ_F_NOWAIT;
1605 else if (force_nonblock)
1606 flags |= MSG_DONTWAIT;
1607
1608 msg = (struct user_msghdr __user *) (unsigned long)
1609 READ_ONCE(sqe->addr);
1610
Jens Axboeaa1fa282019-04-19 13:38:09 -06001611 ret = fn(sock, msg, flags);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001612 if (force_nonblock && ret == -EAGAIN)
1613 return ret;
1614 }
1615
1616 io_cqring_add_event(req->ctx, sqe->user_data, ret);
1617 io_put_req(req);
1618 return 0;
Jens Axboeaa1fa282019-04-19 13:38:09 -06001619}
1620#endif
1621
1622static int io_sendmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1623 bool force_nonblock)
1624{
1625#if defined(CONFIG_NET)
1626 return io_send_recvmsg(req, sqe, force_nonblock, __sys_sendmsg_sock);
1627#else
1628 return -EOPNOTSUPP;
1629#endif
1630}
1631
1632static int io_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1633 bool force_nonblock)
1634{
1635#if defined(CONFIG_NET)
1636 return io_send_recvmsg(req, sqe, force_nonblock, __sys_recvmsg_sock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001637#else
1638 return -EOPNOTSUPP;
1639#endif
1640}
1641
Jens Axboe221c5eb2019-01-17 09:41:58 -07001642static void io_poll_remove_one(struct io_kiocb *req)
1643{
1644 struct io_poll_iocb *poll = &req->poll;
1645
1646 spin_lock(&poll->head->lock);
1647 WRITE_ONCE(poll->canceled, true);
1648 if (!list_empty(&poll->wait.entry)) {
1649 list_del_init(&poll->wait.entry);
Jens Axboe18d9be12019-09-10 09:13:05 -06001650 io_queue_async_work(req->ctx, req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001651 }
1652 spin_unlock(&poll->head->lock);
1653
1654 list_del_init(&req->list);
1655}
1656
1657static void io_poll_remove_all(struct io_ring_ctx *ctx)
1658{
1659 struct io_kiocb *req;
1660
1661 spin_lock_irq(&ctx->completion_lock);
1662 while (!list_empty(&ctx->cancel_list)) {
1663 req = list_first_entry(&ctx->cancel_list, struct io_kiocb,list);
1664 io_poll_remove_one(req);
1665 }
1666 spin_unlock_irq(&ctx->completion_lock);
1667}
1668
1669/*
1670 * Find a running poll command that matches one specified in sqe->addr,
1671 * and remove it if found.
1672 */
1673static int io_poll_remove(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1674{
1675 struct io_ring_ctx *ctx = req->ctx;
1676 struct io_kiocb *poll_req, *next;
1677 int ret = -ENOENT;
1678
1679 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1680 return -EINVAL;
1681 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
1682 sqe->poll_events)
1683 return -EINVAL;
1684
1685 spin_lock_irq(&ctx->completion_lock);
1686 list_for_each_entry_safe(poll_req, next, &ctx->cancel_list, list) {
1687 if (READ_ONCE(sqe->addr) == poll_req->user_data) {
1688 io_poll_remove_one(poll_req);
1689 ret = 0;
1690 break;
1691 }
1692 }
1693 spin_unlock_irq(&ctx->completion_lock);
1694
Jens Axboec71ffb62019-05-13 20:58:29 -06001695 io_cqring_add_event(req->ctx, sqe->user_data, ret);
Jens Axboee65ef562019-03-12 10:16:44 -06001696 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001697 return 0;
1698}
1699
Jens Axboe8c838782019-03-12 15:48:16 -06001700static void io_poll_complete(struct io_ring_ctx *ctx, struct io_kiocb *req,
1701 __poll_t mask)
Jens Axboe221c5eb2019-01-17 09:41:58 -07001702{
Jens Axboe8c838782019-03-12 15:48:16 -06001703 req->poll.done = true;
Jens Axboec71ffb62019-05-13 20:58:29 -06001704 io_cqring_fill_event(ctx, req->user_data, mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06001705 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001706}
1707
1708static void io_poll_complete_work(struct work_struct *work)
1709{
1710 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
1711 struct io_poll_iocb *poll = &req->poll;
1712 struct poll_table_struct pt = { ._key = poll->events };
1713 struct io_ring_ctx *ctx = req->ctx;
1714 __poll_t mask = 0;
1715
1716 if (!READ_ONCE(poll->canceled))
1717 mask = vfs_poll(poll->file, &pt) & poll->events;
1718
1719 /*
1720 * Note that ->ki_cancel callers also delete iocb from active_reqs after
1721 * calling ->ki_cancel. We need the ctx_lock roundtrip here to
1722 * synchronize with them. In the cancellation case the list_del_init
1723 * itself is not actually needed, but harmless so we keep it in to
1724 * avoid further branches in the fast path.
1725 */
1726 spin_lock_irq(&ctx->completion_lock);
1727 if (!mask && !READ_ONCE(poll->canceled)) {
1728 add_wait_queue(poll->head, &poll->wait);
1729 spin_unlock_irq(&ctx->completion_lock);
1730 return;
1731 }
1732 list_del_init(&req->list);
Jens Axboe8c838782019-03-12 15:48:16 -06001733 io_poll_complete(ctx, req, mask);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001734 spin_unlock_irq(&ctx->completion_lock);
1735
Jens Axboe8c838782019-03-12 15:48:16 -06001736 io_cqring_ev_posted(ctx);
1737 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001738}
1739
1740static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
1741 void *key)
1742{
1743 struct io_poll_iocb *poll = container_of(wait, struct io_poll_iocb,
1744 wait);
1745 struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
1746 struct io_ring_ctx *ctx = req->ctx;
1747 __poll_t mask = key_to_poll(key);
Jens Axboe8c838782019-03-12 15:48:16 -06001748 unsigned long flags;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001749
1750 /* for instances that support it check for an event match first: */
Jens Axboe8c838782019-03-12 15:48:16 -06001751 if (mask && !(mask & poll->events))
1752 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001753
1754 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06001755
1756 if (mask && spin_trylock_irqsave(&ctx->completion_lock, flags)) {
1757 list_del(&req->list);
1758 io_poll_complete(ctx, req, mask);
1759 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1760
1761 io_cqring_ev_posted(ctx);
1762 io_put_req(req);
1763 } else {
Jens Axboe18d9be12019-09-10 09:13:05 -06001764 io_queue_async_work(ctx, req);
Jens Axboe8c838782019-03-12 15:48:16 -06001765 }
1766
Jens Axboe221c5eb2019-01-17 09:41:58 -07001767 return 1;
1768}
1769
1770struct io_poll_table {
1771 struct poll_table_struct pt;
1772 struct io_kiocb *req;
1773 int error;
1774};
1775
1776static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
1777 struct poll_table_struct *p)
1778{
1779 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
1780
1781 if (unlikely(pt->req->poll.head)) {
1782 pt->error = -EINVAL;
1783 return;
1784 }
1785
1786 pt->error = 0;
1787 pt->req->poll.head = head;
1788 add_wait_queue(head, &pt->req->poll.wait);
1789}
1790
1791static int io_poll_add(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1792{
1793 struct io_poll_iocb *poll = &req->poll;
1794 struct io_ring_ctx *ctx = req->ctx;
1795 struct io_poll_table ipt;
Jens Axboe8c838782019-03-12 15:48:16 -06001796 bool cancel = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001797 __poll_t mask;
1798 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001799
1800 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1801 return -EINVAL;
1802 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
1803 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06001804 if (!poll->file)
1805 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001806
Jens Axboe6cc47d12019-09-18 11:18:23 -06001807 req->submit.sqe = NULL;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001808 INIT_WORK(&req->work, io_poll_complete_work);
1809 events = READ_ONCE(sqe->poll_events);
1810 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
1811
Jens Axboe221c5eb2019-01-17 09:41:58 -07001812 poll->head = NULL;
Jens Axboe8c838782019-03-12 15:48:16 -06001813 poll->done = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001814 poll->canceled = false;
1815
1816 ipt.pt._qproc = io_poll_queue_proc;
1817 ipt.pt._key = poll->events;
1818 ipt.req = req;
1819 ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
1820
1821 /* initialized the list so that we can do list_empty checks */
1822 INIT_LIST_HEAD(&poll->wait.entry);
1823 init_waitqueue_func_entry(&poll->wait, io_poll_wake);
1824
Jens Axboe36703242019-07-25 10:20:18 -06001825 INIT_LIST_HEAD(&req->list);
1826
Jens Axboe221c5eb2019-01-17 09:41:58 -07001827 mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001828
1829 spin_lock_irq(&ctx->completion_lock);
Jens Axboe8c838782019-03-12 15:48:16 -06001830 if (likely(poll->head)) {
1831 spin_lock(&poll->head->lock);
1832 if (unlikely(list_empty(&poll->wait.entry))) {
1833 if (ipt.error)
1834 cancel = true;
1835 ipt.error = 0;
1836 mask = 0;
1837 }
1838 if (mask || ipt.error)
1839 list_del_init(&poll->wait.entry);
1840 else if (cancel)
1841 WRITE_ONCE(poll->canceled, true);
1842 else if (!poll->done) /* actually waiting for an event */
1843 list_add_tail(&req->list, &ctx->cancel_list);
1844 spin_unlock(&poll->head->lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001845 }
Jens Axboe8c838782019-03-12 15:48:16 -06001846 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06001847 ipt.error = 0;
1848 io_poll_complete(ctx, req, mask);
1849 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07001850 spin_unlock_irq(&ctx->completion_lock);
1851
Jens Axboe8c838782019-03-12 15:48:16 -06001852 if (mask) {
1853 io_cqring_ev_posted(ctx);
Jens Axboee65ef562019-03-12 10:16:44 -06001854 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001855 }
Jens Axboe8c838782019-03-12 15:48:16 -06001856 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001857}
1858
Jens Axboe5262f562019-09-17 12:26:57 -06001859static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
1860{
1861 struct io_ring_ctx *ctx;
1862 struct io_kiocb *req;
1863 unsigned long flags;
1864
1865 req = container_of(timer, struct io_kiocb, timeout.timer);
1866 ctx = req->ctx;
1867 atomic_inc(&ctx->cq_timeouts);
1868
1869 spin_lock_irqsave(&ctx->completion_lock, flags);
1870 list_del(&req->list);
1871
1872 io_cqring_fill_event(ctx, req->user_data, -ETIME);
1873 io_commit_cqring(ctx);
1874 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1875
1876 io_cqring_ev_posted(ctx);
1877
1878 io_put_req(req);
1879 return HRTIMER_NORESTART;
1880}
1881
1882static int io_timeout(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1883{
1884 unsigned count, req_dist, tail_index;
1885 struct io_ring_ctx *ctx = req->ctx;
1886 struct list_head *entry;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06001887 struct timespec64 ts;
Jens Axboe5262f562019-09-17 12:26:57 -06001888
1889 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1890 return -EINVAL;
1891 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->timeout_flags ||
1892 sqe->len != 1)
1893 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06001894
1895 if (get_timespec64(&ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06001896 return -EFAULT;
1897
1898 /*
1899 * sqe->off holds how many events that need to occur for this
1900 * timeout event to be satisfied.
1901 */
1902 count = READ_ONCE(sqe->off);
1903 if (!count)
1904 count = 1;
1905
1906 req->sequence = ctx->cached_sq_head + count - 1;
1907 req->flags |= REQ_F_TIMEOUT;
1908
1909 /*
1910 * Insertion sort, ensuring the first entry in the list is always
1911 * the one we need first.
1912 */
1913 tail_index = ctx->cached_cq_tail - ctx->rings->sq_dropped;
1914 req_dist = req->sequence - tail_index;
1915 spin_lock_irq(&ctx->completion_lock);
1916 list_for_each_prev(entry, &ctx->timeout_list) {
1917 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
1918 unsigned dist;
1919
1920 dist = nxt->sequence - tail_index;
1921 if (req_dist >= dist)
1922 break;
1923 }
1924 list_add(&req->list, entry);
1925 spin_unlock_irq(&ctx->completion_lock);
1926
1927 hrtimer_init(&req->timeout.timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1928 req->timeout.timer.function = io_timeout_fn;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06001929 hrtimer_start(&req->timeout.timer, timespec64_to_ktime(ts),
Jens Axboe5262f562019-09-17 12:26:57 -06001930 HRTIMER_MODE_REL);
1931 return 0;
1932}
1933
Jens Axboede0617e2019-04-06 21:51:27 -06001934static int io_req_defer(struct io_ring_ctx *ctx, struct io_kiocb *req,
1935 const struct io_uring_sqe *sqe)
1936{
1937 struct io_uring_sqe *sqe_copy;
1938
1939 if (!io_sequence_defer(ctx, req) && list_empty(&ctx->defer_list))
1940 return 0;
1941
1942 sqe_copy = kmalloc(sizeof(*sqe_copy), GFP_KERNEL);
1943 if (!sqe_copy)
1944 return -EAGAIN;
1945
1946 spin_lock_irq(&ctx->completion_lock);
1947 if (!io_sequence_defer(ctx, req) && list_empty(&ctx->defer_list)) {
1948 spin_unlock_irq(&ctx->completion_lock);
1949 kfree(sqe_copy);
1950 return 0;
1951 }
1952
1953 memcpy(sqe_copy, sqe, sizeof(*sqe_copy));
1954 req->submit.sqe = sqe_copy;
1955
1956 INIT_WORK(&req->work, io_sq_wq_submit_work);
1957 list_add_tail(&req->list, &ctx->defer_list);
1958 spin_unlock_irq(&ctx->completion_lock);
1959 return -EIOCBQUEUED;
1960}
1961
Jens Axboe2b188cc2019-01-07 10:46:33 -07001962static int __io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001963 const struct sqe_submit *s, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001964{
Jens Axboee0c5c572019-03-12 10:18:47 -06001965 int ret, opcode;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001966
Jens Axboe9e645e112019-05-10 16:07:28 -06001967 req->user_data = READ_ONCE(s->sqe->user_data);
1968
Jens Axboe2b188cc2019-01-07 10:46:33 -07001969 if (unlikely(s->index >= ctx->sq_entries))
1970 return -EINVAL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001971
1972 opcode = READ_ONCE(s->sqe->opcode);
1973 switch (opcode) {
1974 case IORING_OP_NOP:
1975 ret = io_nop(req, req->user_data);
1976 break;
1977 case IORING_OP_READV:
Jens Axboeedafcce2019-01-09 09:16:05 -07001978 if (unlikely(s->sqe->buf_index))
1979 return -EINVAL;
Jens Axboe8358e3a2019-04-23 08:17:58 -06001980 ret = io_read(req, s, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001981 break;
1982 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07001983 if (unlikely(s->sqe->buf_index))
1984 return -EINVAL;
Jens Axboe8358e3a2019-04-23 08:17:58 -06001985 ret = io_write(req, s, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07001986 break;
1987 case IORING_OP_READ_FIXED:
Jens Axboe8358e3a2019-04-23 08:17:58 -06001988 ret = io_read(req, s, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07001989 break;
1990 case IORING_OP_WRITE_FIXED:
Jens Axboe8358e3a2019-04-23 08:17:58 -06001991 ret = io_write(req, s, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001992 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001993 case IORING_OP_FSYNC:
1994 ret = io_fsync(req, s->sqe, force_nonblock);
1995 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001996 case IORING_OP_POLL_ADD:
1997 ret = io_poll_add(req, s->sqe);
1998 break;
1999 case IORING_OP_POLL_REMOVE:
2000 ret = io_poll_remove(req, s->sqe);
2001 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002002 case IORING_OP_SYNC_FILE_RANGE:
2003 ret = io_sync_file_range(req, s->sqe, force_nonblock);
2004 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002005 case IORING_OP_SENDMSG:
2006 ret = io_sendmsg(req, s->sqe, force_nonblock);
2007 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06002008 case IORING_OP_RECVMSG:
2009 ret = io_recvmsg(req, s->sqe, force_nonblock);
2010 break;
Jens Axboe5262f562019-09-17 12:26:57 -06002011 case IORING_OP_TIMEOUT:
2012 ret = io_timeout(req, s->sqe);
2013 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002014 default:
2015 ret = -EINVAL;
2016 break;
2017 }
2018
Jens Axboedef596e2019-01-09 08:59:42 -07002019 if (ret)
2020 return ret;
2021
2022 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe9e645e112019-05-10 16:07:28 -06002023 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07002024 return -EAGAIN;
2025
2026 /* workqueue context doesn't hold uring_lock, grab it now */
2027 if (s->needs_lock)
2028 mutex_lock(&ctx->uring_lock);
2029 io_iopoll_req_issued(req);
2030 if (s->needs_lock)
2031 mutex_unlock(&ctx->uring_lock);
2032 }
2033
2034 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002035}
2036
Jens Axboe31b51512019-01-18 22:56:34 -07002037static struct async_list *io_async_list_from_sqe(struct io_ring_ctx *ctx,
2038 const struct io_uring_sqe *sqe)
2039{
2040 switch (sqe->opcode) {
2041 case IORING_OP_READV:
2042 case IORING_OP_READ_FIXED:
2043 return &ctx->pending_async[READ];
2044 case IORING_OP_WRITEV:
2045 case IORING_OP_WRITE_FIXED:
2046 return &ctx->pending_async[WRITE];
2047 default:
2048 return NULL;
2049 }
2050}
2051
Jens Axboeedafcce2019-01-09 09:16:05 -07002052static inline bool io_sqe_needs_user(const struct io_uring_sqe *sqe)
2053{
2054 u8 opcode = READ_ONCE(sqe->opcode);
2055
2056 return !(opcode == IORING_OP_READ_FIXED ||
2057 opcode == IORING_OP_WRITE_FIXED);
2058}
2059
Jens Axboe2b188cc2019-01-07 10:46:33 -07002060static void io_sq_wq_submit_work(struct work_struct *work)
2061{
2062 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002063 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe31b51512019-01-18 22:56:34 -07002064 struct mm_struct *cur_mm = NULL;
2065 struct async_list *async_list;
2066 LIST_HEAD(req_list);
Jens Axboeedafcce2019-01-09 09:16:05 -07002067 mm_segment_t old_fs;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002068 int ret;
2069
Jens Axboe31b51512019-01-18 22:56:34 -07002070 async_list = io_async_list_from_sqe(ctx, req->submit.sqe);
2071restart:
2072 do {
2073 struct sqe_submit *s = &req->submit;
2074 const struct io_uring_sqe *sqe = s->sqe;
Jackie Liud0ee8792019-07-31 14:39:33 +08002075 unsigned int flags = req->flags;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002076
Stefan Bühler8449eed2019-04-27 20:34:19 +02002077 /* Ensure we clear previously set non-block flag */
Jens Axboe31b51512019-01-18 22:56:34 -07002078 req->rw.ki_flags &= ~IOCB_NOWAIT;
2079
2080 ret = 0;
2081 if (io_sqe_needs_user(sqe) && !cur_mm) {
2082 if (!mmget_not_zero(ctx->sqo_mm)) {
2083 ret = -EFAULT;
2084 } else {
2085 cur_mm = ctx->sqo_mm;
2086 use_mm(cur_mm);
2087 old_fs = get_fs();
2088 set_fs(USER_DS);
2089 }
2090 }
2091
2092 if (!ret) {
2093 s->has_user = cur_mm != NULL;
2094 s->needs_lock = true;
2095 do {
Jens Axboe8358e3a2019-04-23 08:17:58 -06002096 ret = __io_submit_sqe(ctx, req, s, false);
Jens Axboe31b51512019-01-18 22:56:34 -07002097 /*
2098 * We can get EAGAIN for polled IO even though
2099 * we're forcing a sync submission from here,
2100 * since we can't wait for request slots on the
2101 * block side.
2102 */
2103 if (ret != -EAGAIN)
2104 break;
2105 cond_resched();
2106 } while (1);
2107 }
Jens Axboe817869d2019-04-30 14:44:05 -06002108
2109 /* drop submission reference */
2110 io_put_req(req);
2111
Jens Axboe31b51512019-01-18 22:56:34 -07002112 if (ret) {
Jens Axboec71ffb62019-05-13 20:58:29 -06002113 io_cqring_add_event(ctx, sqe->user_data, ret);
Jens Axboee65ef562019-03-12 10:16:44 -06002114 io_put_req(req);
Jens Axboe31b51512019-01-18 22:56:34 -07002115 }
2116
2117 /* async context always use a copy of the sqe */
2118 kfree(sqe);
2119
Zhengyuan Liuf7b76ac2019-07-16 23:26:14 +08002120 /* req from defer and link list needn't decrease async cnt */
Jackie Liud0ee8792019-07-31 14:39:33 +08002121 if (flags & (REQ_F_IO_DRAINED | REQ_F_LINK_DONE))
Zhengyuan Liuf7b76ac2019-07-16 23:26:14 +08002122 goto out;
2123
Jens Axboe31b51512019-01-18 22:56:34 -07002124 if (!async_list)
2125 break;
2126 if (!list_empty(&req_list)) {
2127 req = list_first_entry(&req_list, struct io_kiocb,
2128 list);
2129 list_del(&req->list);
2130 continue;
2131 }
2132 if (list_empty(&async_list->list))
2133 break;
2134
2135 req = NULL;
2136 spin_lock(&async_list->lock);
2137 if (list_empty(&async_list->list)) {
2138 spin_unlock(&async_list->lock);
2139 break;
2140 }
2141 list_splice_init(&async_list->list, &req_list);
2142 spin_unlock(&async_list->lock);
2143
2144 req = list_first_entry(&req_list, struct io_kiocb, list);
2145 list_del(&req->list);
2146 } while (req);
Jens Axboeedafcce2019-01-09 09:16:05 -07002147
2148 /*
Jens Axboe31b51512019-01-18 22:56:34 -07002149 * Rare case of racing with a submitter. If we find the count has
2150 * dropped to zero AND we have pending work items, then restart
2151 * the processing. This is a tiny race window.
Jens Axboeedafcce2019-01-09 09:16:05 -07002152 */
Jens Axboe31b51512019-01-18 22:56:34 -07002153 if (async_list) {
2154 ret = atomic_dec_return(&async_list->cnt);
2155 while (!ret && !list_empty(&async_list->list)) {
2156 spin_lock(&async_list->lock);
2157 atomic_inc(&async_list->cnt);
2158 list_splice_init(&async_list->list, &req_list);
2159 spin_unlock(&async_list->lock);
2160
2161 if (!list_empty(&req_list)) {
2162 req = list_first_entry(&req_list,
2163 struct io_kiocb, list);
2164 list_del(&req->list);
2165 goto restart;
2166 }
2167 ret = atomic_dec_return(&async_list->cnt);
Jens Axboeedafcce2019-01-09 09:16:05 -07002168 }
Jens Axboeedafcce2019-01-09 09:16:05 -07002169 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002170
Zhengyuan Liuf7b76ac2019-07-16 23:26:14 +08002171out:
Jens Axboe31b51512019-01-18 22:56:34 -07002172 if (cur_mm) {
Jens Axboeedafcce2019-01-09 09:16:05 -07002173 set_fs(old_fs);
Jens Axboe31b51512019-01-18 22:56:34 -07002174 unuse_mm(cur_mm);
2175 mmput(cur_mm);
Jens Axboeedafcce2019-01-09 09:16:05 -07002176 }
Jens Axboe31b51512019-01-18 22:56:34 -07002177}
Jens Axboe2b188cc2019-01-07 10:46:33 -07002178
Jens Axboe31b51512019-01-18 22:56:34 -07002179/*
2180 * See if we can piggy back onto previously submitted work, that is still
2181 * running. We currently only allow this if the new request is sequential
2182 * to the previous one we punted.
2183 */
2184static bool io_add_to_prev_work(struct async_list *list, struct io_kiocb *req)
2185{
Jens Axboe6d5d5ac2019-09-11 10:16:13 -06002186 bool ret;
Jens Axboe31b51512019-01-18 22:56:34 -07002187
2188 if (!list)
2189 return false;
2190 if (!(req->flags & REQ_F_SEQ_PREV))
2191 return false;
2192 if (!atomic_read(&list->cnt))
2193 return false;
2194
2195 ret = true;
2196 spin_lock(&list->lock);
2197 list_add_tail(&req->list, &list->list);
Zhengyuan Liuc0e48f92019-07-18 20:44:00 +08002198 /*
2199 * Ensure we see a simultaneous modification from io_sq_wq_submit_work()
2200 */
2201 smp_mb();
Jens Axboe31b51512019-01-18 22:56:34 -07002202 if (!atomic_read(&list->cnt)) {
2203 list_del_init(&req->list);
2204 ret = false;
2205 }
2206 spin_unlock(&list->lock);
2207 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002208}
2209
Jens Axboe09bb8392019-03-13 12:39:28 -06002210static bool io_op_needs_file(const struct io_uring_sqe *sqe)
2211{
2212 int op = READ_ONCE(sqe->opcode);
2213
2214 switch (op) {
2215 case IORING_OP_NOP:
2216 case IORING_OP_POLL_REMOVE:
2217 return false;
2218 default:
2219 return true;
2220 }
2221}
2222
2223static int io_req_set_file(struct io_ring_ctx *ctx, const struct sqe_submit *s,
2224 struct io_submit_state *state, struct io_kiocb *req)
2225{
2226 unsigned flags;
2227 int fd;
2228
2229 flags = READ_ONCE(s->sqe->flags);
2230 fd = READ_ONCE(s->sqe->fd);
2231
Jackie Liu4fe2c962019-09-09 20:50:40 +08002232 if (flags & IOSQE_IO_DRAIN)
Jens Axboede0617e2019-04-06 21:51:27 -06002233 req->flags |= REQ_F_IO_DRAIN;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002234 /*
2235 * All io need record the previous position, if LINK vs DARIN,
2236 * it can be used to mark the position of the first IO in the
2237 * link list.
2238 */
2239 req->sequence = s->sequence;
Jens Axboede0617e2019-04-06 21:51:27 -06002240
Jens Axboe60c112b2019-06-21 10:20:18 -06002241 if (!io_op_needs_file(s->sqe))
Jens Axboe09bb8392019-03-13 12:39:28 -06002242 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06002243
2244 if (flags & IOSQE_FIXED_FILE) {
2245 if (unlikely(!ctx->user_files ||
2246 (unsigned) fd >= ctx->nr_user_files))
2247 return -EBADF;
2248 req->file = ctx->user_files[fd];
2249 req->flags |= REQ_F_FIXED_FILE;
2250 } else {
2251 if (s->needs_fixed_file)
2252 return -EBADF;
2253 req->file = io_file_get(state, fd);
2254 if (unlikely(!req->file))
2255 return -EBADF;
2256 }
2257
2258 return 0;
2259}
2260
Jackie Liu4fe2c962019-09-09 20:50:40 +08002261static int __io_queue_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
Jens Axboec57666682019-09-09 16:19:45 -06002262 struct sqe_submit *s, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002263{
Jens Axboee0c5c572019-03-12 10:18:47 -06002264 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002265
Jens Axboec57666682019-09-09 16:19:45 -06002266 ret = __io_submit_sqe(ctx, req, s, force_nonblock);
Stefan Bühler8449eed2019-04-27 20:34:19 +02002267 if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07002268 struct io_uring_sqe *sqe_copy;
2269
Jackie Liu954dab12019-09-18 10:37:52 +08002270 sqe_copy = kmemdup(s->sqe, sizeof(*sqe_copy), GFP_KERNEL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002271 if (sqe_copy) {
Jens Axboe31b51512019-01-18 22:56:34 -07002272 struct async_list *list;
2273
Jens Axboe2b188cc2019-01-07 10:46:33 -07002274 s->sqe = sqe_copy;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002275 memcpy(&req->submit, s, sizeof(*s));
Jens Axboe31b51512019-01-18 22:56:34 -07002276 list = io_async_list_from_sqe(ctx, s->sqe);
2277 if (!io_add_to_prev_work(list, req)) {
2278 if (list)
2279 atomic_inc(&list->cnt);
2280 INIT_WORK(&req->work, io_sq_wq_submit_work);
Jens Axboe18d9be12019-09-10 09:13:05 -06002281 io_queue_async_work(ctx, req);
Jens Axboe31b51512019-01-18 22:56:34 -07002282 }
Jens Axboee65ef562019-03-12 10:16:44 -06002283
2284 /*
2285 * Queued up for async execution, worker will release
Jens Axboe9e645e112019-05-10 16:07:28 -06002286 * submit reference when the iocb is actually submitted.
Jens Axboee65ef562019-03-12 10:16:44 -06002287 */
2288 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002289 }
2290 }
Jens Axboee65ef562019-03-12 10:16:44 -06002291
2292 /* drop submission reference */
2293 io_put_req(req);
2294
2295 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06002296 if (ret) {
2297 io_cqring_add_event(ctx, req->user_data, ret);
2298 if (req->flags & REQ_F_LINK)
2299 req->flags |= REQ_F_FAIL_LINK;
Jens Axboee65ef562019-03-12 10:16:44 -06002300 io_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002301 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002302
2303 return ret;
2304}
2305
Jackie Liu4fe2c962019-09-09 20:50:40 +08002306static int io_queue_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
Jens Axboec57666682019-09-09 16:19:45 -06002307 struct sqe_submit *s, bool force_nonblock)
Jackie Liu4fe2c962019-09-09 20:50:40 +08002308{
2309 int ret;
2310
2311 ret = io_req_defer(ctx, req, s->sqe);
2312 if (ret) {
2313 if (ret != -EIOCBQUEUED) {
2314 io_free_req(req);
2315 io_cqring_add_event(ctx, s->sqe->user_data, ret);
2316 }
2317 return 0;
2318 }
2319
Jens Axboec57666682019-09-09 16:19:45 -06002320 return __io_queue_sqe(ctx, req, s, force_nonblock);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002321}
2322
2323static int io_queue_link_head(struct io_ring_ctx *ctx, struct io_kiocb *req,
Jens Axboec57666682019-09-09 16:19:45 -06002324 struct sqe_submit *s, struct io_kiocb *shadow,
2325 bool force_nonblock)
Jackie Liu4fe2c962019-09-09 20:50:40 +08002326{
2327 int ret;
2328 int need_submit = false;
2329
2330 if (!shadow)
Jens Axboec57666682019-09-09 16:19:45 -06002331 return io_queue_sqe(ctx, req, s, force_nonblock);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002332
2333 /*
2334 * Mark the first IO in link list as DRAIN, let all the following
2335 * IOs enter the defer list. all IO needs to be completed before link
2336 * list.
2337 */
2338 req->flags |= REQ_F_IO_DRAIN;
2339 ret = io_req_defer(ctx, req, s->sqe);
2340 if (ret) {
2341 if (ret != -EIOCBQUEUED) {
2342 io_free_req(req);
2343 io_cqring_add_event(ctx, s->sqe->user_data, ret);
2344 return 0;
2345 }
2346 } else {
2347 /*
2348 * If ret == 0 means that all IOs in front of link io are
2349 * running done. let's queue link head.
2350 */
2351 need_submit = true;
2352 }
2353
2354 /* Insert shadow req to defer_list, blocking next IOs */
2355 spin_lock_irq(&ctx->completion_lock);
2356 list_add_tail(&shadow->list, &ctx->defer_list);
2357 spin_unlock_irq(&ctx->completion_lock);
2358
2359 if (need_submit)
Jens Axboec57666682019-09-09 16:19:45 -06002360 return __io_queue_sqe(ctx, req, s, force_nonblock);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002361
2362 return 0;
2363}
2364
Jens Axboe9e645e112019-05-10 16:07:28 -06002365#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK)
2366
2367static void io_submit_sqe(struct io_ring_ctx *ctx, struct sqe_submit *s,
Jens Axboec57666682019-09-09 16:19:45 -06002368 struct io_submit_state *state, struct io_kiocb **link,
2369 bool force_nonblock)
Jens Axboe9e645e112019-05-10 16:07:28 -06002370{
2371 struct io_uring_sqe *sqe_copy;
2372 struct io_kiocb *req;
2373 int ret;
2374
2375 /* enforce forwards compatibility on users */
2376 if (unlikely(s->sqe->flags & ~SQE_VALID_FLAGS)) {
2377 ret = -EINVAL;
2378 goto err;
2379 }
2380
2381 req = io_get_req(ctx, state);
2382 if (unlikely(!req)) {
2383 ret = -EAGAIN;
2384 goto err;
2385 }
2386
2387 ret = io_req_set_file(ctx, s, state, req);
2388 if (unlikely(ret)) {
2389err_req:
2390 io_free_req(req);
2391err:
2392 io_cqring_add_event(ctx, s->sqe->user_data, ret);
2393 return;
2394 }
2395
Jens Axboe9e645e112019-05-10 16:07:28 -06002396 /*
2397 * If we already have a head request, queue this one for async
2398 * submittal once the head completes. If we don't have a head but
2399 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
2400 * submitted sync once the chain is complete. If none of those
2401 * conditions are true (normal request), then just queue it.
2402 */
2403 if (*link) {
2404 struct io_kiocb *prev = *link;
2405
2406 sqe_copy = kmemdup(s->sqe, sizeof(*sqe_copy), GFP_KERNEL);
2407 if (!sqe_copy) {
2408 ret = -EAGAIN;
2409 goto err_req;
2410 }
2411
2412 s->sqe = sqe_copy;
2413 memcpy(&req->submit, s, sizeof(*s));
2414 list_add_tail(&req->list, &prev->link_list);
2415 } else if (s->sqe->flags & IOSQE_IO_LINK) {
2416 req->flags |= REQ_F_LINK;
2417
2418 memcpy(&req->submit, s, sizeof(*s));
2419 INIT_LIST_HEAD(&req->link_list);
2420 *link = req;
2421 } else {
Jens Axboec57666682019-09-09 16:19:45 -06002422 io_queue_sqe(ctx, req, s, force_nonblock);
Jens Axboe9e645e112019-05-10 16:07:28 -06002423 }
2424}
2425
Jens Axboe9a56a232019-01-09 09:06:50 -07002426/*
2427 * Batched submission is done, ensure local IO is flushed out.
2428 */
2429static void io_submit_state_end(struct io_submit_state *state)
2430{
2431 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06002432 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07002433 if (state->free_reqs)
2434 kmem_cache_free_bulk(req_cachep, state->free_reqs,
2435 &state->reqs[state->cur_req]);
Jens Axboe9a56a232019-01-09 09:06:50 -07002436}
2437
2438/*
2439 * Start submission side cache.
2440 */
2441static void io_submit_state_start(struct io_submit_state *state,
2442 struct io_ring_ctx *ctx, unsigned max_ios)
2443{
2444 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07002445 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07002446 state->file = NULL;
2447 state->ios_left = max_ios;
2448}
2449
Jens Axboe2b188cc2019-01-07 10:46:33 -07002450static void io_commit_sqring(struct io_ring_ctx *ctx)
2451{
Hristo Venev75b28af2019-08-26 17:23:46 +00002452 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002453
Hristo Venev75b28af2019-08-26 17:23:46 +00002454 if (ctx->cached_sq_head != READ_ONCE(rings->sq.head)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07002455 /*
2456 * Ensure any loads from the SQEs are done at this point,
2457 * since once we write the new head, the application could
2458 * write new data to them.
2459 */
Hristo Venev75b28af2019-08-26 17:23:46 +00002460 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002461 }
2462}
2463
2464/*
Jens Axboe2b188cc2019-01-07 10:46:33 -07002465 * Fetch an sqe, if one is available. Note that s->sqe will point to memory
2466 * that is mapped by userspace. This means that care needs to be taken to
2467 * ensure that reads are stable, as we cannot rely on userspace always
2468 * being a good citizen. If members of the sqe are validated and then later
2469 * used, it's important that those reads are done through READ_ONCE() to
2470 * prevent a re-load down the line.
2471 */
2472static bool io_get_sqring(struct io_ring_ctx *ctx, struct sqe_submit *s)
2473{
Hristo Venev75b28af2019-08-26 17:23:46 +00002474 struct io_rings *rings = ctx->rings;
2475 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002476 unsigned head;
2477
2478 /*
2479 * The cached sq head (or cq tail) serves two purposes:
2480 *
2481 * 1) allows us to batch the cost of updating the user visible
2482 * head updates.
2483 * 2) allows the kernel side to track the head on its own, even
2484 * though the application is the one updating it.
2485 */
2486 head = ctx->cached_sq_head;
Stefan Bühlere523a292019-04-19 11:57:44 +02002487 /* make sure SQ entry isn't read before tail */
Hristo Venev75b28af2019-08-26 17:23:46 +00002488 if (head == smp_load_acquire(&rings->sq.tail))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002489 return false;
2490
Hristo Venev75b28af2019-08-26 17:23:46 +00002491 head = READ_ONCE(sq_array[head & ctx->sq_mask]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002492 if (head < ctx->sq_entries) {
2493 s->index = head;
2494 s->sqe = &ctx->sq_sqes[head];
Jackie Liu8776f3f2019-09-09 20:50:39 +08002495 s->sequence = ctx->cached_sq_head;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002496 ctx->cached_sq_head++;
2497 return true;
2498 }
2499
2500 /* drop invalid entries */
2501 ctx->cached_sq_head++;
Hristo Venev75b28af2019-08-26 17:23:46 +00002502 rings->sq_dropped++;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002503 return false;
2504}
2505
Jens Axboe6c271ce2019-01-10 11:22:30 -07002506static int io_submit_sqes(struct io_ring_ctx *ctx, struct sqe_submit *sqes,
2507 unsigned int nr, bool has_user, bool mm_fault)
2508{
2509 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002510 struct io_kiocb *link = NULL;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002511 struct io_kiocb *shadow_req = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002512 bool prev_was_link = false;
2513 int i, submitted = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002514
2515 if (nr > IO_PLUG_THRESHOLD) {
2516 io_submit_state_start(&state, ctx, nr);
2517 statep = &state;
2518 }
2519
2520 for (i = 0; i < nr; i++) {
Jens Axboe9e645e112019-05-10 16:07:28 -06002521 /*
2522 * If previous wasn't linked and we have a linked command,
2523 * that's the end of the chain. Submit the previous link.
2524 */
2525 if (!prev_was_link && link) {
Jens Axboec57666682019-09-09 16:19:45 -06002526 io_queue_link_head(ctx, link, &link->submit, shadow_req,
2527 true);
Jens Axboe9e645e112019-05-10 16:07:28 -06002528 link = NULL;
Jackie Liu5f5ad9c2019-09-18 10:37:53 +08002529 shadow_req = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002530 }
2531 prev_was_link = (sqes[i].sqe->flags & IOSQE_IO_LINK) != 0;
2532
Jackie Liu4fe2c962019-09-09 20:50:40 +08002533 if (link && (sqes[i].sqe->flags & IOSQE_IO_DRAIN)) {
2534 if (!shadow_req) {
2535 shadow_req = io_get_req(ctx, NULL);
Jackie Liua1041c22019-09-18 17:25:52 +08002536 if (unlikely(!shadow_req))
2537 goto out;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002538 shadow_req->flags |= (REQ_F_IO_DRAIN | REQ_F_SHADOW_DRAIN);
2539 refcount_dec(&shadow_req->refs);
2540 }
2541 shadow_req->sequence = sqes[i].sequence;
2542 }
2543
Jackie Liua1041c22019-09-18 17:25:52 +08002544out:
Jens Axboe6c271ce2019-01-10 11:22:30 -07002545 if (unlikely(mm_fault)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06002546 io_cqring_add_event(ctx, sqes[i].sqe->user_data,
2547 -EFAULT);
Jens Axboe6c271ce2019-01-10 11:22:30 -07002548 } else {
2549 sqes[i].has_user = has_user;
2550 sqes[i].needs_lock = true;
2551 sqes[i].needs_fixed_file = true;
Jens Axboec57666682019-09-09 16:19:45 -06002552 io_submit_sqe(ctx, &sqes[i], statep, &link, true);
Jens Axboe6c271ce2019-01-10 11:22:30 -07002553 submitted++;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002554 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07002555 }
2556
Jens Axboe9e645e112019-05-10 16:07:28 -06002557 if (link)
Jens Axboec57666682019-09-09 16:19:45 -06002558 io_queue_link_head(ctx, link, &link->submit, shadow_req, true);
Jens Axboe6c271ce2019-01-10 11:22:30 -07002559 if (statep)
2560 io_submit_state_end(&state);
2561
2562 return submitted;
2563}
2564
2565static int io_sq_thread(void *data)
2566{
2567 struct sqe_submit sqes[IO_IOPOLL_BATCH];
2568 struct io_ring_ctx *ctx = data;
2569 struct mm_struct *cur_mm = NULL;
2570 mm_segment_t old_fs;
2571 DEFINE_WAIT(wait);
2572 unsigned inflight;
2573 unsigned long timeout;
2574
Jackie Liua4c0b3d2019-07-08 13:41:12 +08002575 complete(&ctx->sqo_thread_started);
2576
Jens Axboe6c271ce2019-01-10 11:22:30 -07002577 old_fs = get_fs();
2578 set_fs(USER_DS);
2579
2580 timeout = inflight = 0;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02002581 while (!kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07002582 bool all_fixed, mm_fault = false;
2583 int i;
2584
2585 if (inflight) {
2586 unsigned nr_events = 0;
2587
2588 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07002589 io_iopoll_check(ctx, &nr_events, 0);
Jens Axboe6c271ce2019-01-10 11:22:30 -07002590 } else {
2591 /*
2592 * Normal IO, just pretend everything completed.
2593 * We don't have to poll completions for that.
2594 */
2595 nr_events = inflight;
2596 }
2597
2598 inflight -= nr_events;
2599 if (!inflight)
2600 timeout = jiffies + ctx->sq_thread_idle;
2601 }
2602
2603 if (!io_get_sqring(ctx, &sqes[0])) {
2604 /*
2605 * We're polling. If we're within the defined idle
2606 * period, then let us spin without work before going
2607 * to sleep.
2608 */
2609 if (inflight || !time_after(jiffies, timeout)) {
Jens Axboe9831a902019-09-19 09:48:55 -06002610 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07002611 continue;
2612 }
2613
2614 /*
2615 * Drop cur_mm before scheduling, we can't hold it for
2616 * long periods (or over schedule()). Do this before
2617 * adding ourselves to the waitqueue, as the unuse/drop
2618 * may sleep.
2619 */
2620 if (cur_mm) {
2621 unuse_mm(cur_mm);
2622 mmput(cur_mm);
2623 cur_mm = NULL;
2624 }
2625
2626 prepare_to_wait(&ctx->sqo_wait, &wait,
2627 TASK_INTERRUPTIBLE);
2628
2629 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00002630 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02002631 /* make sure to read SQ tail after writing flags */
2632 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07002633
2634 if (!io_get_sqring(ctx, &sqes[0])) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02002635 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07002636 finish_wait(&ctx->sqo_wait, &wait);
2637 break;
2638 }
2639 if (signal_pending(current))
2640 flush_signals(current);
2641 schedule();
2642 finish_wait(&ctx->sqo_wait, &wait);
2643
Hristo Venev75b28af2019-08-26 17:23:46 +00002644 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002645 continue;
2646 }
2647 finish_wait(&ctx->sqo_wait, &wait);
2648
Hristo Venev75b28af2019-08-26 17:23:46 +00002649 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002650 }
2651
2652 i = 0;
2653 all_fixed = true;
2654 do {
2655 if (all_fixed && io_sqe_needs_user(sqes[i].sqe))
2656 all_fixed = false;
2657
2658 i++;
2659 if (i == ARRAY_SIZE(sqes))
2660 break;
2661 } while (io_get_sqring(ctx, &sqes[i]));
2662
2663 /* Unless all new commands are FIXED regions, grab mm */
2664 if (!all_fixed && !cur_mm) {
2665 mm_fault = !mmget_not_zero(ctx->sqo_mm);
2666 if (!mm_fault) {
2667 use_mm(ctx->sqo_mm);
2668 cur_mm = ctx->sqo_mm;
2669 }
2670 }
2671
2672 inflight += io_submit_sqes(ctx, sqes, i, cur_mm != NULL,
2673 mm_fault);
2674
2675 /* Commit SQ ring head once we've consumed all SQEs */
2676 io_commit_sqring(ctx);
2677 }
2678
2679 set_fs(old_fs);
2680 if (cur_mm) {
2681 unuse_mm(cur_mm);
2682 mmput(cur_mm);
2683 }
Jens Axboe06058632019-04-13 09:26:03 -06002684
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02002685 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06002686
Jens Axboe6c271ce2019-01-10 11:22:30 -07002687 return 0;
2688}
2689
Jens Axboec57666682019-09-09 16:19:45 -06002690static int io_ring_submit(struct io_ring_ctx *ctx, unsigned int to_submit,
2691 bool block_for_last)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002692{
Jens Axboe9a56a232019-01-09 09:06:50 -07002693 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002694 struct io_kiocb *link = NULL;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002695 struct io_kiocb *shadow_req = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002696 bool prev_was_link = false;
Jens Axboe5c8b0b52019-04-30 10:16:07 -06002697 int i, submit = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002698
Jens Axboe9a56a232019-01-09 09:06:50 -07002699 if (to_submit > IO_PLUG_THRESHOLD) {
2700 io_submit_state_start(&state, ctx, to_submit);
2701 statep = &state;
2702 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002703
2704 for (i = 0; i < to_submit; i++) {
Jens Axboec57666682019-09-09 16:19:45 -06002705 bool force_nonblock = true;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002706 struct sqe_submit s;
2707
2708 if (!io_get_sqring(ctx, &s))
2709 break;
2710
Jens Axboe9e645e112019-05-10 16:07:28 -06002711 /*
2712 * If previous wasn't linked and we have a linked command,
2713 * that's the end of the chain. Submit the previous link.
2714 */
2715 if (!prev_was_link && link) {
Jens Axboec57666682019-09-09 16:19:45 -06002716 io_queue_link_head(ctx, link, &link->submit, shadow_req,
2717 force_nonblock);
Jens Axboe9e645e112019-05-10 16:07:28 -06002718 link = NULL;
Jackie Liu5f5ad9c2019-09-18 10:37:53 +08002719 shadow_req = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002720 }
2721 prev_was_link = (s.sqe->flags & IOSQE_IO_LINK) != 0;
2722
Jackie Liu4fe2c962019-09-09 20:50:40 +08002723 if (link && (s.sqe->flags & IOSQE_IO_DRAIN)) {
2724 if (!shadow_req) {
2725 shadow_req = io_get_req(ctx, NULL);
Jackie Liua1041c22019-09-18 17:25:52 +08002726 if (unlikely(!shadow_req))
2727 goto out;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002728 shadow_req->flags |= (REQ_F_IO_DRAIN | REQ_F_SHADOW_DRAIN);
2729 refcount_dec(&shadow_req->refs);
2730 }
2731 shadow_req->sequence = s.sequence;
2732 }
2733
Jackie Liua1041c22019-09-18 17:25:52 +08002734out:
Jens Axboe2b188cc2019-01-07 10:46:33 -07002735 s.has_user = true;
Jens Axboedef596e2019-01-09 08:59:42 -07002736 s.needs_lock = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002737 s.needs_fixed_file = false;
Jens Axboe5c8b0b52019-04-30 10:16:07 -06002738 submit++;
Jens Axboec57666682019-09-09 16:19:45 -06002739
2740 /*
2741 * The caller will block for events after submit, submit the
2742 * last IO non-blocking. This is either the only IO it's
2743 * submitting, or it already submitted the previous ones. This
2744 * improves performance by avoiding an async punt that we don't
2745 * need to do.
2746 */
2747 if (block_for_last && submit == to_submit)
2748 force_nonblock = false;
2749
2750 io_submit_sqe(ctx, &s, statep, &link, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002751 }
2752 io_commit_sqring(ctx);
2753
Jens Axboe9e645e112019-05-10 16:07:28 -06002754 if (link)
Jens Axboec57666682019-09-09 16:19:45 -06002755 io_queue_link_head(ctx, link, &link->submit, shadow_req,
Pavel Begunkovbf7ec932019-10-04 17:01:08 +03002756 !block_for_last);
Jens Axboe9a56a232019-01-09 09:06:50 -07002757 if (statep)
2758 io_submit_state_end(statep);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002759
Jens Axboe5c8b0b52019-04-30 10:16:07 -06002760 return submit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002761}
2762
Jens Axboe2b188cc2019-01-07 10:46:33 -07002763/*
2764 * Wait until events become available, if we don't already have some. The
2765 * application must reap them itself, as they reside on the shared cq ring.
2766 */
2767static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
2768 const sigset_t __user *sig, size_t sigsz)
2769{
Hristo Venev75b28af2019-08-26 17:23:46 +00002770 struct io_rings *rings = ctx->rings;
Jens Axboe5262f562019-09-17 12:26:57 -06002771 unsigned nr_timeouts;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002772 int ret;
2773
Hristo Venev75b28af2019-08-26 17:23:46 +00002774 if (io_cqring_events(rings) >= min_events)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002775 return 0;
2776
2777 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01002778#ifdef CONFIG_COMPAT
2779 if (in_compat_syscall())
2780 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07002781 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01002782 else
2783#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07002784 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01002785
Jens Axboe2b188cc2019-01-07 10:46:33 -07002786 if (ret)
2787 return ret;
2788 }
2789
Jens Axboe5262f562019-09-17 12:26:57 -06002790 nr_timeouts = atomic_read(&ctx->cq_timeouts);
2791 /*
2792 * Return if we have enough events, or if a timeout occured since
2793 * we started waiting. For timeouts, we always want to return to
2794 * userspace.
2795 */
2796 ret = wait_event_interruptible(ctx->wait,
2797 io_cqring_events(rings) >= min_events ||
2798 atomic_read(&ctx->cq_timeouts) != nr_timeouts);
Oleg Nesterovb7724342019-07-16 16:29:53 -07002799 restore_saved_sigmask_unless(ret == -ERESTARTSYS);
Oleg Nesterov97abc882019-06-28 12:06:50 -07002800 if (ret == -ERESTARTSYS)
2801 ret = -EINTR;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002802
Hristo Venev75b28af2019-08-26 17:23:46 +00002803 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002804}
2805
Jens Axboe6b063142019-01-10 22:13:58 -07002806static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
2807{
2808#if defined(CONFIG_UNIX)
2809 if (ctx->ring_sock) {
2810 struct sock *sock = ctx->ring_sock->sk;
2811 struct sk_buff *skb;
2812
2813 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
2814 kfree_skb(skb);
2815 }
2816#else
2817 int i;
2818
2819 for (i = 0; i < ctx->nr_user_files; i++)
2820 fput(ctx->user_files[i]);
2821#endif
2822}
2823
2824static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
2825{
2826 if (!ctx->user_files)
2827 return -ENXIO;
2828
2829 __io_sqe_files_unregister(ctx);
2830 kfree(ctx->user_files);
2831 ctx->user_files = NULL;
2832 ctx->nr_user_files = 0;
2833 return 0;
2834}
2835
Jens Axboe6c271ce2019-01-10 11:22:30 -07002836static void io_sq_thread_stop(struct io_ring_ctx *ctx)
2837{
2838 if (ctx->sqo_thread) {
Jackie Liua4c0b3d2019-07-08 13:41:12 +08002839 wait_for_completion(&ctx->sqo_thread_started);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02002840 /*
2841 * The park is a bit of a work-around, without it we get
2842 * warning spews on shutdown with SQPOLL set and affinity
2843 * set to a single CPU.
2844 */
Jens Axboe06058632019-04-13 09:26:03 -06002845 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07002846 kthread_stop(ctx->sqo_thread);
2847 ctx->sqo_thread = NULL;
2848 }
2849}
2850
Jens Axboe6b063142019-01-10 22:13:58 -07002851static void io_finish_async(struct io_ring_ctx *ctx)
2852{
Jens Axboe54a91f32019-09-10 09:15:04 -06002853 int i;
2854
Jens Axboe6c271ce2019-01-10 11:22:30 -07002855 io_sq_thread_stop(ctx);
2856
Jens Axboe54a91f32019-09-10 09:15:04 -06002857 for (i = 0; i < ARRAY_SIZE(ctx->sqo_wq); i++) {
2858 if (ctx->sqo_wq[i]) {
2859 destroy_workqueue(ctx->sqo_wq[i]);
2860 ctx->sqo_wq[i] = NULL;
2861 }
Jens Axboe6b063142019-01-10 22:13:58 -07002862 }
2863}
2864
2865#if defined(CONFIG_UNIX)
2866static void io_destruct_skb(struct sk_buff *skb)
2867{
2868 struct io_ring_ctx *ctx = skb->sk->sk_user_data;
2869
2870 io_finish_async(ctx);
2871 unix_destruct_scm(skb);
2872}
2873
2874/*
2875 * Ensure the UNIX gc is aware of our file set, so we are certain that
2876 * the io_uring can be safely unregistered on process exit, even if we have
2877 * loops in the file referencing.
2878 */
2879static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
2880{
2881 struct sock *sk = ctx->ring_sock->sk;
2882 struct scm_fp_list *fpl;
2883 struct sk_buff *skb;
2884 int i;
2885
2886 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
2887 unsigned long inflight = ctx->user->unix_inflight + nr;
2888
2889 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
2890 return -EMFILE;
2891 }
2892
2893 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
2894 if (!fpl)
2895 return -ENOMEM;
2896
2897 skb = alloc_skb(0, GFP_KERNEL);
2898 if (!skb) {
2899 kfree(fpl);
2900 return -ENOMEM;
2901 }
2902
2903 skb->sk = sk;
2904 skb->destructor = io_destruct_skb;
2905
2906 fpl->user = get_uid(ctx->user);
2907 for (i = 0; i < nr; i++) {
2908 fpl->fp[i] = get_file(ctx->user_files[i + offset]);
2909 unix_inflight(fpl->user, fpl->fp[i]);
2910 }
2911
2912 fpl->max = fpl->count = nr;
2913 UNIXCB(skb).fp = fpl;
2914 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
2915 skb_queue_head(&sk->sk_receive_queue, skb);
2916
2917 for (i = 0; i < nr; i++)
2918 fput(fpl->fp[i]);
2919
2920 return 0;
2921}
2922
2923/*
2924 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
2925 * causes regular reference counting to break down. We rely on the UNIX
2926 * garbage collection to take care of this problem for us.
2927 */
2928static int io_sqe_files_scm(struct io_ring_ctx *ctx)
2929{
2930 unsigned left, total;
2931 int ret = 0;
2932
2933 total = 0;
2934 left = ctx->nr_user_files;
2935 while (left) {
2936 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07002937
2938 ret = __io_sqe_files_scm(ctx, this_files, total);
2939 if (ret)
2940 break;
2941 left -= this_files;
2942 total += this_files;
2943 }
2944
2945 if (!ret)
2946 return 0;
2947
2948 while (total < ctx->nr_user_files) {
2949 fput(ctx->user_files[total]);
2950 total++;
2951 }
2952
2953 return ret;
2954}
2955#else
2956static int io_sqe_files_scm(struct io_ring_ctx *ctx)
2957{
2958 return 0;
2959}
2960#endif
2961
2962static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
2963 unsigned nr_args)
2964{
2965 __s32 __user *fds = (__s32 __user *) arg;
2966 int fd, ret = 0;
2967 unsigned i;
2968
2969 if (ctx->user_files)
2970 return -EBUSY;
2971 if (!nr_args)
2972 return -EINVAL;
2973 if (nr_args > IORING_MAX_FIXED_FILES)
2974 return -EMFILE;
2975
2976 ctx->user_files = kcalloc(nr_args, sizeof(struct file *), GFP_KERNEL);
2977 if (!ctx->user_files)
2978 return -ENOMEM;
2979
2980 for (i = 0; i < nr_args; i++) {
2981 ret = -EFAULT;
2982 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
2983 break;
2984
2985 ctx->user_files[i] = fget(fd);
2986
2987 ret = -EBADF;
2988 if (!ctx->user_files[i])
2989 break;
2990 /*
2991 * Don't allow io_uring instances to be registered. If UNIX
2992 * isn't enabled, then this causes a reference cycle and this
2993 * instance can never get freed. If UNIX is enabled we'll
2994 * handle it just fine, but there's still no point in allowing
2995 * a ring fd as it doesn't support regular read/write anyway.
2996 */
2997 if (ctx->user_files[i]->f_op == &io_uring_fops) {
2998 fput(ctx->user_files[i]);
2999 break;
3000 }
3001 ctx->nr_user_files++;
3002 ret = 0;
3003 }
3004
3005 if (ret) {
3006 for (i = 0; i < ctx->nr_user_files; i++)
3007 fput(ctx->user_files[i]);
3008
3009 kfree(ctx->user_files);
Jens Axboe25adf502019-04-03 09:52:40 -06003010 ctx->user_files = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003011 ctx->nr_user_files = 0;
3012 return ret;
3013 }
3014
3015 ret = io_sqe_files_scm(ctx);
3016 if (ret)
3017 io_sqe_files_unregister(ctx);
3018
3019 return ret;
3020}
3021
Jens Axboe6c271ce2019-01-10 11:22:30 -07003022static int io_sq_offload_start(struct io_ring_ctx *ctx,
3023 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003024{
3025 int ret;
3026
Jens Axboe6c271ce2019-01-10 11:22:30 -07003027 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003028 mmgrab(current->mm);
3029 ctx->sqo_mm = current->mm;
3030
Jens Axboe6c271ce2019-01-10 11:22:30 -07003031 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06003032 ret = -EPERM;
3033 if (!capable(CAP_SYS_ADMIN))
3034 goto err;
3035
Jens Axboe917257d2019-04-13 09:28:55 -06003036 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
3037 if (!ctx->sq_thread_idle)
3038 ctx->sq_thread_idle = HZ;
3039
Jens Axboe6c271ce2019-01-10 11:22:30 -07003040 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06003041 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003042
Jens Axboe917257d2019-04-13 09:28:55 -06003043 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06003044 if (cpu >= nr_cpu_ids)
3045 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08003046 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06003047 goto err;
3048
Jens Axboe6c271ce2019-01-10 11:22:30 -07003049 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
3050 ctx, cpu,
3051 "io_uring-sq");
3052 } else {
3053 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
3054 "io_uring-sq");
3055 }
3056 if (IS_ERR(ctx->sqo_thread)) {
3057 ret = PTR_ERR(ctx->sqo_thread);
3058 ctx->sqo_thread = NULL;
3059 goto err;
3060 }
3061 wake_up_process(ctx->sqo_thread);
3062 } else if (p->flags & IORING_SETUP_SQ_AFF) {
3063 /* Can't have SQ_AFF without SQPOLL */
3064 ret = -EINVAL;
3065 goto err;
3066 }
3067
Jens Axboe2b188cc2019-01-07 10:46:33 -07003068 /* Do QD, or 2 * CPUS, whatever is smallest */
Jens Axboe54a91f32019-09-10 09:15:04 -06003069 ctx->sqo_wq[0] = alloc_workqueue("io_ring-wq",
3070 WQ_UNBOUND | WQ_FREEZABLE,
Jens Axboe2b188cc2019-01-07 10:46:33 -07003071 min(ctx->sq_entries - 1, 2 * num_online_cpus()));
Jens Axboe54a91f32019-09-10 09:15:04 -06003072 if (!ctx->sqo_wq[0]) {
3073 ret = -ENOMEM;
3074 goto err;
3075 }
3076
3077 /*
3078 * This is for buffered writes, where we want to limit the parallelism
3079 * due to file locking in file systems. As "normal" buffered writes
3080 * should parellelize on writeout quite nicely, limit us to having 2
3081 * pending. This avoids massive contention on the inode when doing
3082 * buffered async writes.
3083 */
3084 ctx->sqo_wq[1] = alloc_workqueue("io_ring-write-wq",
3085 WQ_UNBOUND | WQ_FREEZABLE, 2);
3086 if (!ctx->sqo_wq[1]) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07003087 ret = -ENOMEM;
3088 goto err;
3089 }
3090
3091 return 0;
3092err:
Jens Axboe54a91f32019-09-10 09:15:04 -06003093 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003094 mmdrop(ctx->sqo_mm);
3095 ctx->sqo_mm = NULL;
3096 return ret;
3097}
3098
3099static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
3100{
3101 atomic_long_sub(nr_pages, &user->locked_vm);
3102}
3103
3104static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
3105{
3106 unsigned long page_limit, cur_pages, new_pages;
3107
3108 /* Don't allow more pages than we can safely lock */
3109 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
3110
3111 do {
3112 cur_pages = atomic_long_read(&user->locked_vm);
3113 new_pages = cur_pages + nr_pages;
3114 if (new_pages > page_limit)
3115 return -ENOMEM;
3116 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
3117 new_pages) != cur_pages);
3118
3119 return 0;
3120}
3121
3122static void io_mem_free(void *ptr)
3123{
Mark Rutland52e04ef2019-04-30 17:30:21 +01003124 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003125
Mark Rutland52e04ef2019-04-30 17:30:21 +01003126 if (!ptr)
3127 return;
3128
3129 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003130 if (put_page_testzero(page))
3131 free_compound_page(page);
3132}
3133
3134static void *io_mem_alloc(size_t size)
3135{
3136 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
3137 __GFP_NORETRY;
3138
3139 return (void *) __get_free_pages(gfp_flags, get_order(size));
3140}
3141
Hristo Venev75b28af2019-08-26 17:23:46 +00003142static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
3143 size_t *sq_offset)
3144{
3145 struct io_rings *rings;
3146 size_t off, sq_array_size;
3147
3148 off = struct_size(rings, cqes, cq_entries);
3149 if (off == SIZE_MAX)
3150 return SIZE_MAX;
3151
3152#ifdef CONFIG_SMP
3153 off = ALIGN(off, SMP_CACHE_BYTES);
3154 if (off == 0)
3155 return SIZE_MAX;
3156#endif
3157
3158 sq_array_size = array_size(sizeof(u32), sq_entries);
3159 if (sq_array_size == SIZE_MAX)
3160 return SIZE_MAX;
3161
3162 if (check_add_overflow(off, sq_array_size, &off))
3163 return SIZE_MAX;
3164
3165 if (sq_offset)
3166 *sq_offset = off;
3167
3168 return off;
3169}
3170
Jens Axboe2b188cc2019-01-07 10:46:33 -07003171static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
3172{
Hristo Venev75b28af2019-08-26 17:23:46 +00003173 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003174
Hristo Venev75b28af2019-08-26 17:23:46 +00003175 pages = (size_t)1 << get_order(
3176 rings_size(sq_entries, cq_entries, NULL));
3177 pages += (size_t)1 << get_order(
3178 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07003179
Hristo Venev75b28af2019-08-26 17:23:46 +00003180 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003181}
3182
Jens Axboeedafcce2019-01-09 09:16:05 -07003183static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
3184{
3185 int i, j;
3186
3187 if (!ctx->user_bufs)
3188 return -ENXIO;
3189
3190 for (i = 0; i < ctx->nr_user_bufs; i++) {
3191 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
3192
3193 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbard27c4d3a2019-08-04 19:32:06 -07003194 put_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07003195
3196 if (ctx->account_mem)
3197 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003198 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07003199 imu->nr_bvecs = 0;
3200 }
3201
3202 kfree(ctx->user_bufs);
3203 ctx->user_bufs = NULL;
3204 ctx->nr_user_bufs = 0;
3205 return 0;
3206}
3207
3208static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
3209 void __user *arg, unsigned index)
3210{
3211 struct iovec __user *src;
3212
3213#ifdef CONFIG_COMPAT
3214 if (ctx->compat) {
3215 struct compat_iovec __user *ciovs;
3216 struct compat_iovec ciov;
3217
3218 ciovs = (struct compat_iovec __user *) arg;
3219 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
3220 return -EFAULT;
3221
3222 dst->iov_base = (void __user *) (unsigned long) ciov.iov_base;
3223 dst->iov_len = ciov.iov_len;
3224 return 0;
3225 }
3226#endif
3227 src = (struct iovec __user *) arg;
3228 if (copy_from_user(dst, &src[index], sizeof(*dst)))
3229 return -EFAULT;
3230 return 0;
3231}
3232
3233static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
3234 unsigned nr_args)
3235{
3236 struct vm_area_struct **vmas = NULL;
3237 struct page **pages = NULL;
3238 int i, j, got_pages = 0;
3239 int ret = -EINVAL;
3240
3241 if (ctx->user_bufs)
3242 return -EBUSY;
3243 if (!nr_args || nr_args > UIO_MAXIOV)
3244 return -EINVAL;
3245
3246 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
3247 GFP_KERNEL);
3248 if (!ctx->user_bufs)
3249 return -ENOMEM;
3250
3251 for (i = 0; i < nr_args; i++) {
3252 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
3253 unsigned long off, start, end, ubuf;
3254 int pret, nr_pages;
3255 struct iovec iov;
3256 size_t size;
3257
3258 ret = io_copy_iov(ctx, &iov, arg, i);
3259 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03003260 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07003261
3262 /*
3263 * Don't impose further limits on the size and buffer
3264 * constraints here, we'll -EINVAL later when IO is
3265 * submitted if they are wrong.
3266 */
3267 ret = -EFAULT;
3268 if (!iov.iov_base || !iov.iov_len)
3269 goto err;
3270
3271 /* arbitrary limit, but we need something */
3272 if (iov.iov_len > SZ_1G)
3273 goto err;
3274
3275 ubuf = (unsigned long) iov.iov_base;
3276 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
3277 start = ubuf >> PAGE_SHIFT;
3278 nr_pages = end - start;
3279
3280 if (ctx->account_mem) {
3281 ret = io_account_mem(ctx->user, nr_pages);
3282 if (ret)
3283 goto err;
3284 }
3285
3286 ret = 0;
3287 if (!pages || nr_pages > got_pages) {
3288 kfree(vmas);
3289 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003290 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07003291 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003292 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07003293 sizeof(struct vm_area_struct *),
3294 GFP_KERNEL);
3295 if (!pages || !vmas) {
3296 ret = -ENOMEM;
3297 if (ctx->account_mem)
3298 io_unaccount_mem(ctx->user, nr_pages);
3299 goto err;
3300 }
3301 got_pages = nr_pages;
3302 }
3303
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003304 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07003305 GFP_KERNEL);
3306 ret = -ENOMEM;
3307 if (!imu->bvec) {
3308 if (ctx->account_mem)
3309 io_unaccount_mem(ctx->user, nr_pages);
3310 goto err;
3311 }
3312
3313 ret = 0;
3314 down_read(&current->mm->mmap_sem);
Ira Weiny932f4a62019-05-13 17:17:03 -07003315 pret = get_user_pages(ubuf, nr_pages,
3316 FOLL_WRITE | FOLL_LONGTERM,
3317 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07003318 if (pret == nr_pages) {
3319 /* don't support file backed memory */
3320 for (j = 0; j < nr_pages; j++) {
3321 struct vm_area_struct *vma = vmas[j];
3322
3323 if (vma->vm_file &&
3324 !is_file_hugepages(vma->vm_file)) {
3325 ret = -EOPNOTSUPP;
3326 break;
3327 }
3328 }
3329 } else {
3330 ret = pret < 0 ? pret : -EFAULT;
3331 }
3332 up_read(&current->mm->mmap_sem);
3333 if (ret) {
3334 /*
3335 * if we did partial map, or found file backed vmas,
3336 * release any pages we did get
3337 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07003338 if (pret > 0)
3339 put_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07003340 if (ctx->account_mem)
3341 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003342 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07003343 goto err;
3344 }
3345
3346 off = ubuf & ~PAGE_MASK;
3347 size = iov.iov_len;
3348 for (j = 0; j < nr_pages; j++) {
3349 size_t vec_len;
3350
3351 vec_len = min_t(size_t, size, PAGE_SIZE - off);
3352 imu->bvec[j].bv_page = pages[j];
3353 imu->bvec[j].bv_len = vec_len;
3354 imu->bvec[j].bv_offset = off;
3355 off = 0;
3356 size -= vec_len;
3357 }
3358 /* store original address for later verification */
3359 imu->ubuf = ubuf;
3360 imu->len = iov.iov_len;
3361 imu->nr_bvecs = nr_pages;
3362
3363 ctx->nr_user_bufs++;
3364 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003365 kvfree(pages);
3366 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07003367 return 0;
3368err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003369 kvfree(pages);
3370 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07003371 io_sqe_buffer_unregister(ctx);
3372 return ret;
3373}
3374
Jens Axboe9b402842019-04-11 11:45:41 -06003375static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
3376{
3377 __s32 __user *fds = arg;
3378 int fd;
3379
3380 if (ctx->cq_ev_fd)
3381 return -EBUSY;
3382
3383 if (copy_from_user(&fd, fds, sizeof(*fds)))
3384 return -EFAULT;
3385
3386 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
3387 if (IS_ERR(ctx->cq_ev_fd)) {
3388 int ret = PTR_ERR(ctx->cq_ev_fd);
3389 ctx->cq_ev_fd = NULL;
3390 return ret;
3391 }
3392
3393 return 0;
3394}
3395
3396static int io_eventfd_unregister(struct io_ring_ctx *ctx)
3397{
3398 if (ctx->cq_ev_fd) {
3399 eventfd_ctx_put(ctx->cq_ev_fd);
3400 ctx->cq_ev_fd = NULL;
3401 return 0;
3402 }
3403
3404 return -ENXIO;
3405}
3406
Jens Axboe2b188cc2019-01-07 10:46:33 -07003407static void io_ring_ctx_free(struct io_ring_ctx *ctx)
3408{
Jens Axboe6b063142019-01-10 22:13:58 -07003409 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003410 if (ctx->sqo_mm)
3411 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07003412
3413 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07003414 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07003415 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06003416 io_eventfd_unregister(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07003417
Jens Axboe2b188cc2019-01-07 10:46:33 -07003418#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07003419 if (ctx->ring_sock) {
3420 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07003421 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07003422 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003423#endif
3424
Hristo Venev75b28af2019-08-26 17:23:46 +00003425 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003426 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003427
3428 percpu_ref_exit(&ctx->refs);
3429 if (ctx->account_mem)
3430 io_unaccount_mem(ctx->user,
3431 ring_pages(ctx->sq_entries, ctx->cq_entries));
3432 free_uid(ctx->user);
3433 kfree(ctx);
3434}
3435
3436static __poll_t io_uring_poll(struct file *file, poll_table *wait)
3437{
3438 struct io_ring_ctx *ctx = file->private_data;
3439 __poll_t mask = 0;
3440
3441 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02003442 /*
3443 * synchronizes with barrier from wq_has_sleeper call in
3444 * io_commit_cqring
3445 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07003446 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00003447 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
3448 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003449 mask |= EPOLLOUT | EPOLLWRNORM;
Hristo Venev75b28af2019-08-26 17:23:46 +00003450 if (READ_ONCE(ctx->rings->sq.head) != ctx->cached_cq_tail)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003451 mask |= EPOLLIN | EPOLLRDNORM;
3452
3453 return mask;
3454}
3455
3456static int io_uring_fasync(int fd, struct file *file, int on)
3457{
3458 struct io_ring_ctx *ctx = file->private_data;
3459
3460 return fasync_helper(fd, file, on, &ctx->cq_fasync);
3461}
3462
3463static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
3464{
3465 mutex_lock(&ctx->uring_lock);
3466 percpu_ref_kill(&ctx->refs);
3467 mutex_unlock(&ctx->uring_lock);
3468
Jens Axboe5262f562019-09-17 12:26:57 -06003469 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003470 io_poll_remove_all(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07003471 io_iopoll_reap_events(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003472 wait_for_completion(&ctx->ctx_done);
3473 io_ring_ctx_free(ctx);
3474}
3475
3476static int io_uring_release(struct inode *inode, struct file *file)
3477{
3478 struct io_ring_ctx *ctx = file->private_data;
3479
3480 file->private_data = NULL;
3481 io_ring_ctx_wait_and_kill(ctx);
3482 return 0;
3483}
3484
3485static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
3486{
3487 loff_t offset = (loff_t) vma->vm_pgoff << PAGE_SHIFT;
3488 unsigned long sz = vma->vm_end - vma->vm_start;
3489 struct io_ring_ctx *ctx = file->private_data;
3490 unsigned long pfn;
3491 struct page *page;
3492 void *ptr;
3493
3494 switch (offset) {
3495 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00003496 case IORING_OFF_CQ_RING:
3497 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003498 break;
3499 case IORING_OFF_SQES:
3500 ptr = ctx->sq_sqes;
3501 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003502 default:
3503 return -EINVAL;
3504 }
3505
3506 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07003507 if (sz > page_size(page))
Jens Axboe2b188cc2019-01-07 10:46:33 -07003508 return -EINVAL;
3509
3510 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
3511 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
3512}
3513
3514SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
3515 u32, min_complete, u32, flags, const sigset_t __user *, sig,
3516 size_t, sigsz)
3517{
3518 struct io_ring_ctx *ctx;
3519 long ret = -EBADF;
3520 int submitted = 0;
3521 struct fd f;
3522
Jens Axboe6c271ce2019-01-10 11:22:30 -07003523 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07003524 return -EINVAL;
3525
3526 f = fdget(fd);
3527 if (!f.file)
3528 return -EBADF;
3529
3530 ret = -EOPNOTSUPP;
3531 if (f.file->f_op != &io_uring_fops)
3532 goto out_fput;
3533
3534 ret = -ENXIO;
3535 ctx = f.file->private_data;
3536 if (!percpu_ref_tryget(&ctx->refs))
3537 goto out_fput;
3538
Jens Axboe6c271ce2019-01-10 11:22:30 -07003539 /*
3540 * For SQ polling, the thread will do all submissions and completions.
3541 * Just return the requested submit count, and wake the thread if
3542 * we were asked to.
3543 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06003544 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003545 if (ctx->flags & IORING_SETUP_SQPOLL) {
3546 if (flags & IORING_ENTER_SQ_WAKEUP)
3547 wake_up(&ctx->sqo_wait);
3548 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06003549 } else if (to_submit) {
Jens Axboec57666682019-09-09 16:19:45 -06003550 bool block_for_last = false;
3551
Jens Axboe2b188cc2019-01-07 10:46:33 -07003552 to_submit = min(to_submit, ctx->sq_entries);
3553
Jens Axboec57666682019-09-09 16:19:45 -06003554 /*
3555 * Allow last submission to block in a series, IFF the caller
3556 * asked to wait for events and we don't currently have
3557 * enough. This potentially avoids an async punt.
3558 */
3559 if (to_submit == min_complete &&
3560 io_cqring_events(ctx->rings) < min_complete)
3561 block_for_last = true;
3562
Jens Axboe2b188cc2019-01-07 10:46:33 -07003563 mutex_lock(&ctx->uring_lock);
Jens Axboec57666682019-09-09 16:19:45 -06003564 submitted = io_ring_submit(ctx, to_submit, block_for_last);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003565 mutex_unlock(&ctx->uring_lock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003566 }
3567 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07003568 unsigned nr_events = 0;
3569
Jens Axboe2b188cc2019-01-07 10:46:33 -07003570 min_complete = min(min_complete, ctx->cq_entries);
3571
Jens Axboedef596e2019-01-09 08:59:42 -07003572 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07003573 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07003574 } else {
3575 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
3576 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003577 }
3578
Pavel Begunkov6805b322019-10-08 02:18:42 +03003579 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003580out_fput:
3581 fdput(f);
3582 return submitted ? submitted : ret;
3583}
3584
3585static const struct file_operations io_uring_fops = {
3586 .release = io_uring_release,
3587 .mmap = io_uring_mmap,
3588 .poll = io_uring_poll,
3589 .fasync = io_uring_fasync,
3590};
3591
3592static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
3593 struct io_uring_params *p)
3594{
Hristo Venev75b28af2019-08-26 17:23:46 +00003595 struct io_rings *rings;
3596 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003597
Hristo Venev75b28af2019-08-26 17:23:46 +00003598 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
3599 if (size == SIZE_MAX)
3600 return -EOVERFLOW;
3601
3602 rings = io_mem_alloc(size);
3603 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003604 return -ENOMEM;
3605
Hristo Venev75b28af2019-08-26 17:23:46 +00003606 ctx->rings = rings;
3607 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
3608 rings->sq_ring_mask = p->sq_entries - 1;
3609 rings->cq_ring_mask = p->cq_entries - 1;
3610 rings->sq_ring_entries = p->sq_entries;
3611 rings->cq_ring_entries = p->cq_entries;
3612 ctx->sq_mask = rings->sq_ring_mask;
3613 ctx->cq_mask = rings->cq_ring_mask;
3614 ctx->sq_entries = rings->sq_ring_entries;
3615 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003616
3617 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
3618 if (size == SIZE_MAX)
3619 return -EOVERFLOW;
3620
3621 ctx->sq_sqes = io_mem_alloc(size);
Mark Rutland52e04ef2019-04-30 17:30:21 +01003622 if (!ctx->sq_sqes)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003623 return -ENOMEM;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003624
Jens Axboe2b188cc2019-01-07 10:46:33 -07003625 return 0;
3626}
3627
3628/*
3629 * Allocate an anonymous fd, this is what constitutes the application
3630 * visible backing of an io_uring instance. The application mmaps this
3631 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
3632 * we have to tie this fd to a socket for file garbage collection purposes.
3633 */
3634static int io_uring_get_fd(struct io_ring_ctx *ctx)
3635{
3636 struct file *file;
3637 int ret;
3638
3639#if defined(CONFIG_UNIX)
3640 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
3641 &ctx->ring_sock);
3642 if (ret)
3643 return ret;
3644#endif
3645
3646 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
3647 if (ret < 0)
3648 goto err;
3649
3650 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
3651 O_RDWR | O_CLOEXEC);
3652 if (IS_ERR(file)) {
3653 put_unused_fd(ret);
3654 ret = PTR_ERR(file);
3655 goto err;
3656 }
3657
3658#if defined(CONFIG_UNIX)
3659 ctx->ring_sock->file = file;
Jens Axboe6b063142019-01-10 22:13:58 -07003660 ctx->ring_sock->sk->sk_user_data = ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003661#endif
3662 fd_install(ret, file);
3663 return ret;
3664err:
3665#if defined(CONFIG_UNIX)
3666 sock_release(ctx->ring_sock);
3667 ctx->ring_sock = NULL;
3668#endif
3669 return ret;
3670}
3671
3672static int io_uring_create(unsigned entries, struct io_uring_params *p)
3673{
3674 struct user_struct *user = NULL;
3675 struct io_ring_ctx *ctx;
3676 bool account_mem;
3677 int ret;
3678
3679 if (!entries || entries > IORING_MAX_ENTRIES)
3680 return -EINVAL;
3681
3682 /*
3683 * Use twice as many entries for the CQ ring. It's possible for the
3684 * application to drive a higher depth than the size of the SQ ring,
3685 * since the sqes are only used at submission time. This allows for
3686 * some flexibility in overcommitting a bit.
3687 */
3688 p->sq_entries = roundup_pow_of_two(entries);
3689 p->cq_entries = 2 * p->sq_entries;
3690
3691 user = get_uid(current_user());
3692 account_mem = !capable(CAP_IPC_LOCK);
3693
3694 if (account_mem) {
3695 ret = io_account_mem(user,
3696 ring_pages(p->sq_entries, p->cq_entries));
3697 if (ret) {
3698 free_uid(user);
3699 return ret;
3700 }
3701 }
3702
3703 ctx = io_ring_ctx_alloc(p);
3704 if (!ctx) {
3705 if (account_mem)
3706 io_unaccount_mem(user, ring_pages(p->sq_entries,
3707 p->cq_entries));
3708 free_uid(user);
3709 return -ENOMEM;
3710 }
3711 ctx->compat = in_compat_syscall();
3712 ctx->account_mem = account_mem;
3713 ctx->user = user;
3714
3715 ret = io_allocate_scq_urings(ctx, p);
3716 if (ret)
3717 goto err;
3718
Jens Axboe6c271ce2019-01-10 11:22:30 -07003719 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003720 if (ret)
3721 goto err;
3722
3723 ret = io_uring_get_fd(ctx);
3724 if (ret < 0)
3725 goto err;
3726
3727 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00003728 p->sq_off.head = offsetof(struct io_rings, sq.head);
3729 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
3730 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
3731 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
3732 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
3733 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
3734 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003735
3736 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00003737 p->cq_off.head = offsetof(struct io_rings, cq.head);
3738 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
3739 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
3740 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
3741 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
3742 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06003743
3744 p->features = IORING_FEAT_SINGLE_MMAP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003745 return ret;
3746err:
3747 io_ring_ctx_wait_and_kill(ctx);
3748 return ret;
3749}
3750
3751/*
3752 * Sets up an aio uring context, and returns the fd. Applications asks for a
3753 * ring size, we return the actual sq/cq ring sizes (among other things) in the
3754 * params structure passed in.
3755 */
3756static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
3757{
3758 struct io_uring_params p;
3759 long ret;
3760 int i;
3761
3762 if (copy_from_user(&p, params, sizeof(p)))
3763 return -EFAULT;
3764 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
3765 if (p.resv[i])
3766 return -EINVAL;
3767 }
3768
Jens Axboe6c271ce2019-01-10 11:22:30 -07003769 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
3770 IORING_SETUP_SQ_AFF))
Jens Axboe2b188cc2019-01-07 10:46:33 -07003771 return -EINVAL;
3772
3773 ret = io_uring_create(entries, &p);
3774 if (ret < 0)
3775 return ret;
3776
3777 if (copy_to_user(params, &p, sizeof(p)))
3778 return -EFAULT;
3779
3780 return ret;
3781}
3782
3783SYSCALL_DEFINE2(io_uring_setup, u32, entries,
3784 struct io_uring_params __user *, params)
3785{
3786 return io_uring_setup(entries, params);
3787}
3788
Jens Axboeedafcce2019-01-09 09:16:05 -07003789static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
3790 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06003791 __releases(ctx->uring_lock)
3792 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07003793{
3794 int ret;
3795
Jens Axboe35fa71a2019-04-22 10:23:23 -06003796 /*
3797 * We're inside the ring mutex, if the ref is already dying, then
3798 * someone else killed the ctx or is already going through
3799 * io_uring_register().
3800 */
3801 if (percpu_ref_is_dying(&ctx->refs))
3802 return -ENXIO;
3803
Jens Axboeedafcce2019-01-09 09:16:05 -07003804 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06003805
3806 /*
3807 * Drop uring mutex before waiting for references to exit. If another
3808 * thread is currently inside io_uring_enter() it might need to grab
3809 * the uring_lock to make progress. If we hold it here across the drain
3810 * wait, then we can deadlock. It's safe to drop the mutex here, since
3811 * no new references will come in after we've killed the percpu ref.
3812 */
3813 mutex_unlock(&ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07003814 wait_for_completion(&ctx->ctx_done);
Jens Axboeb19062a2019-04-15 10:49:38 -06003815 mutex_lock(&ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07003816
3817 switch (opcode) {
3818 case IORING_REGISTER_BUFFERS:
3819 ret = io_sqe_buffer_register(ctx, arg, nr_args);
3820 break;
3821 case IORING_UNREGISTER_BUFFERS:
3822 ret = -EINVAL;
3823 if (arg || nr_args)
3824 break;
3825 ret = io_sqe_buffer_unregister(ctx);
3826 break;
Jens Axboe6b063142019-01-10 22:13:58 -07003827 case IORING_REGISTER_FILES:
3828 ret = io_sqe_files_register(ctx, arg, nr_args);
3829 break;
3830 case IORING_UNREGISTER_FILES:
3831 ret = -EINVAL;
3832 if (arg || nr_args)
3833 break;
3834 ret = io_sqe_files_unregister(ctx);
3835 break;
Jens Axboe9b402842019-04-11 11:45:41 -06003836 case IORING_REGISTER_EVENTFD:
3837 ret = -EINVAL;
3838 if (nr_args != 1)
3839 break;
3840 ret = io_eventfd_register(ctx, arg);
3841 break;
3842 case IORING_UNREGISTER_EVENTFD:
3843 ret = -EINVAL;
3844 if (arg || nr_args)
3845 break;
3846 ret = io_eventfd_unregister(ctx);
3847 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07003848 default:
3849 ret = -EINVAL;
3850 break;
3851 }
3852
3853 /* bring the ctx back to life */
3854 reinit_completion(&ctx->ctx_done);
3855 percpu_ref_reinit(&ctx->refs);
3856 return ret;
3857}
3858
3859SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
3860 void __user *, arg, unsigned int, nr_args)
3861{
3862 struct io_ring_ctx *ctx;
3863 long ret = -EBADF;
3864 struct fd f;
3865
3866 f = fdget(fd);
3867 if (!f.file)
3868 return -EBADF;
3869
3870 ret = -EOPNOTSUPP;
3871 if (f.file->f_op != &io_uring_fops)
3872 goto out_fput;
3873
3874 ctx = f.file->private_data;
3875
3876 mutex_lock(&ctx->uring_lock);
3877 ret = __io_uring_register(ctx, opcode, arg, nr_args);
3878 mutex_unlock(&ctx->uring_lock);
3879out_fput:
3880 fdput(f);
3881 return ret;
3882}
3883
Jens Axboe2b188cc2019-01-07 10:46:33 -07003884static int __init io_uring_init(void)
3885{
3886 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
3887 return 0;
3888};
3889__initcall(io_uring_init);