blob: 7ab93e854eb21e04c0f4c745886cf85a14a32d4f [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
78#define IORING_MAX_ENTRIES 4096
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/*
87 * This data is shared with the application through the mmap at offset
88 * IORING_OFF_SQ_RING.
89 *
90 * The offsets to the member fields are published through struct
91 * io_sqring_offsets when calling io_uring_setup.
92 */
Jens Axboe2b188cc2019-01-07 10:46:33 -070093struct io_sq_ring {
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 *
98 * The kernel controls head and the application controls tail.
99 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700100 struct io_uring r;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200101 /*
102 * Bitmask to apply to head and tail offsets (constant, equals
103 * ring_entries - 1)
104 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700105 u32 ring_mask;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200106 /* Ring size (constant, power of 2) */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700107 u32 ring_entries;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200108 /*
109 * Number of invalid entries dropped by the kernel due to
110 * invalid index stored in array
111 *
112 * Written by the kernel, shouldn't be modified by the
113 * application (i.e. get number of "new events" by comparing to
114 * cached value).
115 *
116 * After a new SQ head value was read by the application this
117 * counter includes all submissions that were dropped reaching
118 * the new SQ head (and possibly more).
119 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700120 u32 dropped;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200121 /*
122 * Runtime flags
123 *
124 * Written by the kernel, shouldn't be modified by the
125 * application.
126 *
127 * The application needs a full memory barrier before checking
128 * for IORING_SQ_NEED_WAKEUP after updating the sq tail.
129 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700130 u32 flags;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200131 /*
132 * Ring buffer of indices into array of io_uring_sqe, which is
133 * mmapped by the application using the IORING_OFF_SQES offset.
134 *
135 * This indirection could e.g. be used to assign fixed
136 * io_uring_sqe entries to operations and only submit them to
137 * the queue when needed.
138 *
139 * The kernel modifies neither the indices array nor the entries
140 * array.
141 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700142 u32 array[];
143};
144
Stefan Bühler1e84b972019-04-24 23:54:16 +0200145/*
146 * This data is shared with the application through the mmap at offset
147 * IORING_OFF_CQ_RING.
148 *
149 * The offsets to the member fields are published through struct
150 * io_cqring_offsets when calling io_uring_setup.
151 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700152struct io_cq_ring {
Stefan Bühler1e84b972019-04-24 23:54:16 +0200153 /*
154 * Head and tail offsets into the ring; the offsets need to be
155 * masked to get valid indices.
156 *
157 * The application controls head and the kernel tail.
158 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700159 struct io_uring r;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200160 /*
161 * Bitmask to apply to head and tail offsets (constant, equals
162 * ring_entries - 1)
163 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700164 u32 ring_mask;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200165 /* Ring size (constant, power of 2) */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700166 u32 ring_entries;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200167 /*
168 * Number of completion events lost because the queue was full;
169 * this should be avoided by the application by making sure
170 * there are not more requests pending thatn there is space in
171 * the completion queue.
172 *
173 * Written by the kernel, shouldn't be modified by the
174 * application (i.e. get number of "new events" by comparing to
175 * cached value).
176 *
177 * As completion events come in out of order this counter is not
178 * ordered with any other data.
179 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700180 u32 overflow;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200181 /*
182 * Ring buffer of completion events.
183 *
184 * The kernel writes completion events fresh every time they are
185 * produced, so the application is allowed to modify pending
186 * entries.
187 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700188 struct io_uring_cqe cqes[];
189};
190
Jens Axboeedafcce2019-01-09 09:16:05 -0700191struct io_mapped_ubuf {
192 u64 ubuf;
193 size_t len;
194 struct bio_vec *bvec;
195 unsigned int nr_bvecs;
196};
197
Jens Axboe31b51512019-01-18 22:56:34 -0700198struct async_list {
199 spinlock_t lock;
200 atomic_t cnt;
201 struct list_head list;
202
203 struct file *file;
204 off_t io_end;
205 size_t io_pages;
206};
207
Jens Axboe2b188cc2019-01-07 10:46:33 -0700208struct io_ring_ctx {
209 struct {
210 struct percpu_ref refs;
211 } ____cacheline_aligned_in_smp;
212
213 struct {
214 unsigned int flags;
215 bool compat;
216 bool account_mem;
217
218 /* SQ ring */
219 struct io_sq_ring *sq_ring;
220 unsigned cached_sq_head;
221 unsigned sq_entries;
222 unsigned sq_mask;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700223 unsigned sq_thread_idle;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700224 struct io_uring_sqe *sq_sqes;
225 } ____cacheline_aligned_in_smp;
226
227 /* IO offload */
228 struct workqueue_struct *sqo_wq;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700229 struct task_struct *sqo_thread; /* if using sq thread polling */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700230 struct mm_struct *sqo_mm;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700231 wait_queue_head_t sqo_wait;
232 unsigned sqo_stop;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700233
234 struct {
235 /* CQ ring */
236 struct io_cq_ring *cq_ring;
237 unsigned cached_cq_tail;
238 unsigned cq_entries;
239 unsigned cq_mask;
240 struct wait_queue_head cq_wait;
241 struct fasync_struct *cq_fasync;
242 } ____cacheline_aligned_in_smp;
243
Jens Axboe6b063142019-01-10 22:13:58 -0700244 /*
245 * If used, fixed file set. Writers must ensure that ->refs is dead,
246 * readers must ensure that ->refs is alive as long as the file* is
247 * used. Only updated through io_uring_register(2).
248 */
249 struct file **user_files;
250 unsigned nr_user_files;
251
Jens Axboeedafcce2019-01-09 09:16:05 -0700252 /* if used, fixed mapped user buffers */
253 unsigned nr_user_bufs;
254 struct io_mapped_ubuf *user_bufs;
255
Jens Axboe2b188cc2019-01-07 10:46:33 -0700256 struct user_struct *user;
257
258 struct completion ctx_done;
259
260 struct {
261 struct mutex uring_lock;
262 wait_queue_head_t wait;
263 } ____cacheline_aligned_in_smp;
264
265 struct {
266 spinlock_t completion_lock;
Jens Axboedef596e2019-01-09 08:59:42 -0700267 bool poll_multi_file;
268 /*
269 * ->poll_list is protected by the ctx->uring_lock for
270 * io_uring instances that don't use IORING_SETUP_SQPOLL.
271 * For SQPOLL, only the single threaded io_sq_thread() will
272 * manipulate the list, hence no extra locking is needed there.
273 */
274 struct list_head poll_list;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700275 struct list_head cancel_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700276 } ____cacheline_aligned_in_smp;
277
Jens Axboe31b51512019-01-18 22:56:34 -0700278 struct async_list pending_async[2];
279
Jens Axboe2b188cc2019-01-07 10:46:33 -0700280#if defined(CONFIG_UNIX)
281 struct socket *ring_sock;
282#endif
283};
284
285struct sqe_submit {
286 const struct io_uring_sqe *sqe;
287 unsigned short index;
288 bool has_user;
Jens Axboedef596e2019-01-09 08:59:42 -0700289 bool needs_lock;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700290 bool needs_fixed_file;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700291};
292
Jens Axboe09bb8392019-03-13 12:39:28 -0600293/*
294 * First field must be the file pointer in all the
295 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
296 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700297struct io_poll_iocb {
298 struct file *file;
299 struct wait_queue_head *head;
300 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600301 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700302 bool canceled;
303 struct wait_queue_entry wait;
304};
305
Jens Axboe09bb8392019-03-13 12:39:28 -0600306/*
307 * NOTE! Each of the iocb union members has the file pointer
308 * as the first entry in their struct definition. So you can
309 * access the file pointer through any of the sub-structs,
310 * or directly as just 'ki_filp' in this struct.
311 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700312struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700313 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600314 struct file *file;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700315 struct kiocb rw;
316 struct io_poll_iocb poll;
317 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700318
319 struct sqe_submit submit;
320
321 struct io_ring_ctx *ctx;
322 struct list_head list;
323 unsigned int flags;
Jens Axboec16361c2019-01-17 08:39:48 -0700324 refcount_t refs;
Stefan Bühler8449eed2019-04-27 20:34:19 +0200325#define REQ_F_NOWAIT 1 /* must not punt to workers */
Jens Axboedef596e2019-01-09 08:59:42 -0700326#define REQ_F_IOPOLL_COMPLETED 2 /* polled IO has completed */
Jens Axboe6b063142019-01-10 22:13:58 -0700327#define REQ_F_FIXED_FILE 4 /* ctx owns file */
Jens Axboe31b51512019-01-18 22:56:34 -0700328#define REQ_F_SEQ_PREV 8 /* sequential with previous */
Jens Axboed530a402019-03-13 12:15:01 -0600329#define REQ_F_PREPPED 16 /* prep already done */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700330 u64 user_data;
Jens Axboedef596e2019-01-09 08:59:42 -0700331 u64 error;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700332
333 struct work_struct work;
334};
335
336#define IO_PLUG_THRESHOLD 2
Jens Axboedef596e2019-01-09 08:59:42 -0700337#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700338
Jens Axboe9a56a232019-01-09 09:06:50 -0700339struct io_submit_state {
340 struct blk_plug plug;
341
342 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700343 * io_kiocb alloc cache
344 */
345 void *reqs[IO_IOPOLL_BATCH];
346 unsigned int free_reqs;
347 unsigned int cur_req;
348
349 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700350 * File reference cache
351 */
352 struct file *file;
353 unsigned int fd;
354 unsigned int has_refs;
355 unsigned int used_refs;
356 unsigned int ios_left;
357};
358
Jens Axboe2b188cc2019-01-07 10:46:33 -0700359static struct kmem_cache *req_cachep;
360
361static const struct file_operations io_uring_fops;
362
363struct sock *io_uring_get_socket(struct file *file)
364{
365#if defined(CONFIG_UNIX)
366 if (file->f_op == &io_uring_fops) {
367 struct io_ring_ctx *ctx = file->private_data;
368
369 return ctx->ring_sock->sk;
370 }
371#endif
372 return NULL;
373}
374EXPORT_SYMBOL(io_uring_get_socket);
375
376static void io_ring_ctx_ref_free(struct percpu_ref *ref)
377{
378 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
379
380 complete(&ctx->ctx_done);
381}
382
383static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
384{
385 struct io_ring_ctx *ctx;
Jens Axboe31b51512019-01-18 22:56:34 -0700386 int i;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700387
388 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
389 if (!ctx)
390 return NULL;
391
392 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free, 0, GFP_KERNEL)) {
393 kfree(ctx);
394 return NULL;
395 }
396
397 ctx->flags = p->flags;
398 init_waitqueue_head(&ctx->cq_wait);
399 init_completion(&ctx->ctx_done);
400 mutex_init(&ctx->uring_lock);
401 init_waitqueue_head(&ctx->wait);
Jens Axboe31b51512019-01-18 22:56:34 -0700402 for (i = 0; i < ARRAY_SIZE(ctx->pending_async); i++) {
403 spin_lock_init(&ctx->pending_async[i].lock);
404 INIT_LIST_HEAD(&ctx->pending_async[i].list);
405 atomic_set(&ctx->pending_async[i].cnt, 0);
406 }
Jens Axboe2b188cc2019-01-07 10:46:33 -0700407 spin_lock_init(&ctx->completion_lock);
Jens Axboedef596e2019-01-09 08:59:42 -0700408 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboe221c5eb2019-01-17 09:41:58 -0700409 INIT_LIST_HEAD(&ctx->cancel_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700410 return ctx;
411}
412
413static void io_commit_cqring(struct io_ring_ctx *ctx)
414{
415 struct io_cq_ring *ring = ctx->cq_ring;
416
417 if (ctx->cached_cq_tail != READ_ONCE(ring->r.tail)) {
418 /* order cqe stores with ring update */
419 smp_store_release(&ring->r.tail, ctx->cached_cq_tail);
420
421 /*
422 * Write sider barrier of tail update, app has read side. See
423 * comment at the top of this file.
424 */
425 smp_wmb();
426
427 if (wq_has_sleeper(&ctx->cq_wait)) {
428 wake_up_interruptible(&ctx->cq_wait);
429 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
430 }
431 }
432}
433
434static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
435{
436 struct io_cq_ring *ring = ctx->cq_ring;
437 unsigned tail;
438
439 tail = ctx->cached_cq_tail;
440 /* See comment at the top of the file */
441 smp_rmb();
Jens Axboe74f464e2019-04-17 08:57:48 -0600442 if (tail - READ_ONCE(ring->r.head) == ring->ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700443 return NULL;
444
445 ctx->cached_cq_tail++;
446 return &ring->cqes[tail & ctx->cq_mask];
447}
448
449static void io_cqring_fill_event(struct io_ring_ctx *ctx, u64 ki_user_data,
450 long res, unsigned ev_flags)
451{
452 struct io_uring_cqe *cqe;
453
454 /*
455 * If we can't get a cq entry, userspace overflowed the
456 * submission (by quite a lot). Increment the overflow count in
457 * the ring.
458 */
459 cqe = io_get_cqring(ctx);
460 if (cqe) {
461 WRITE_ONCE(cqe->user_data, ki_user_data);
462 WRITE_ONCE(cqe->res, res);
463 WRITE_ONCE(cqe->flags, ev_flags);
464 } else {
465 unsigned overflow = READ_ONCE(ctx->cq_ring->overflow);
466
467 WRITE_ONCE(ctx->cq_ring->overflow, overflow + 1);
468 }
469}
470
Jens Axboe8c838782019-03-12 15:48:16 -0600471static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
472{
473 if (waitqueue_active(&ctx->wait))
474 wake_up(&ctx->wait);
475 if (waitqueue_active(&ctx->sqo_wait))
476 wake_up(&ctx->sqo_wait);
477}
478
479static void io_cqring_add_event(struct io_ring_ctx *ctx, u64 user_data,
Jens Axboe2b188cc2019-01-07 10:46:33 -0700480 long res, unsigned ev_flags)
481{
482 unsigned long flags;
483
484 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe8c838782019-03-12 15:48:16 -0600485 io_cqring_fill_event(ctx, user_data, res, ev_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700486 io_commit_cqring(ctx);
487 spin_unlock_irqrestore(&ctx->completion_lock, flags);
488
Jens Axboe8c838782019-03-12 15:48:16 -0600489 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700490}
491
492static void io_ring_drop_ctx_refs(struct io_ring_ctx *ctx, unsigned refs)
493{
494 percpu_ref_put_many(&ctx->refs, refs);
495
496 if (waitqueue_active(&ctx->wait))
497 wake_up(&ctx->wait);
498}
499
Jens Axboe2579f912019-01-09 09:10:43 -0700500static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
501 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700502{
Jens Axboefd6fab22019-03-14 16:30:06 -0600503 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700504 struct io_kiocb *req;
505
506 if (!percpu_ref_tryget(&ctx->refs))
507 return NULL;
508
Jens Axboe2579f912019-01-09 09:10:43 -0700509 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -0600510 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -0700511 if (unlikely(!req))
512 goto out;
513 } else if (!state->free_reqs) {
514 size_t sz;
515 int ret;
516
517 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -0600518 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
519
520 /*
521 * Bulk alloc is all-or-nothing. If we fail to get a batch,
522 * retry single alloc to be on the safe side.
523 */
524 if (unlikely(ret <= 0)) {
525 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
526 if (!state->reqs[0])
527 goto out;
528 ret = 1;
529 }
Jens Axboe2579f912019-01-09 09:10:43 -0700530 state->free_reqs = ret - 1;
531 state->cur_req = 1;
532 req = state->reqs[0];
533 } else {
534 req = state->reqs[state->cur_req];
535 state->free_reqs--;
536 state->cur_req++;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700537 }
538
Jens Axboe2579f912019-01-09 09:10:43 -0700539 req->ctx = ctx;
540 req->flags = 0;
Jens Axboee65ef562019-03-12 10:16:44 -0600541 /* one is dropped after submission, the other at completion */
542 refcount_set(&req->refs, 2);
Jens Axboe2579f912019-01-09 09:10:43 -0700543 return req;
544out:
Jens Axboe2b188cc2019-01-07 10:46:33 -0700545 io_ring_drop_ctx_refs(ctx, 1);
546 return NULL;
547}
548
Jens Axboedef596e2019-01-09 08:59:42 -0700549static void io_free_req_many(struct io_ring_ctx *ctx, void **reqs, int *nr)
550{
551 if (*nr) {
552 kmem_cache_free_bulk(req_cachep, *nr, reqs);
553 io_ring_drop_ctx_refs(ctx, *nr);
554 *nr = 0;
555 }
556}
557
Jens Axboe2b188cc2019-01-07 10:46:33 -0700558static void io_free_req(struct io_kiocb *req)
559{
Jens Axboe09bb8392019-03-13 12:39:28 -0600560 if (req->file && !(req->flags & REQ_F_FIXED_FILE))
561 fput(req->file);
Jens Axboee65ef562019-03-12 10:16:44 -0600562 io_ring_drop_ctx_refs(req->ctx, 1);
563 kmem_cache_free(req_cachep, req);
564}
565
566static void io_put_req(struct io_kiocb *req)
567{
568 if (refcount_dec_and_test(&req->refs))
569 io_free_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700570}
571
Jens Axboedef596e2019-01-09 08:59:42 -0700572/*
573 * Find and free completed poll iocbs
574 */
575static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
576 struct list_head *done)
577{
578 void *reqs[IO_IOPOLL_BATCH];
579 struct io_kiocb *req;
Jens Axboe09bb8392019-03-13 12:39:28 -0600580 int to_free;
Jens Axboedef596e2019-01-09 08:59:42 -0700581
Jens Axboe09bb8392019-03-13 12:39:28 -0600582 to_free = 0;
Jens Axboedef596e2019-01-09 08:59:42 -0700583 while (!list_empty(done)) {
584 req = list_first_entry(done, struct io_kiocb, list);
585 list_del(&req->list);
586
587 io_cqring_fill_event(ctx, req->user_data, req->error, 0);
Jens Axboedef596e2019-01-09 08:59:42 -0700588 (*nr_events)++;
589
Jens Axboe09bb8392019-03-13 12:39:28 -0600590 if (refcount_dec_and_test(&req->refs)) {
591 /* If we're not using fixed files, we have to pair the
592 * completion part with the file put. Use regular
593 * completions for those, only batch free for fixed
594 * file.
595 */
596 if (req->flags & REQ_F_FIXED_FILE) {
597 reqs[to_free++] = req;
598 if (to_free == ARRAY_SIZE(reqs))
599 io_free_req_many(ctx, reqs, &to_free);
Jens Axboe6b063142019-01-10 22:13:58 -0700600 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -0600601 io_free_req(req);
Jens Axboe6b063142019-01-10 22:13:58 -0700602 }
Jens Axboe9a56a232019-01-09 09:06:50 -0700603 }
Jens Axboedef596e2019-01-09 08:59:42 -0700604 }
Jens Axboedef596e2019-01-09 08:59:42 -0700605
Jens Axboe09bb8392019-03-13 12:39:28 -0600606 io_commit_cqring(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -0700607 io_free_req_many(ctx, reqs, &to_free);
608}
609
610static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
611 long min)
612{
613 struct io_kiocb *req, *tmp;
614 LIST_HEAD(done);
615 bool spin;
616 int ret;
617
618 /*
619 * Only spin for completions if we don't have multiple devices hanging
620 * off our complete list, and we're under the requested amount.
621 */
622 spin = !ctx->poll_multi_file && *nr_events < min;
623
624 ret = 0;
625 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
626 struct kiocb *kiocb = &req->rw;
627
628 /*
629 * Move completed entries to our local list. If we find a
630 * request that requires polling, break out and complete
631 * the done list first, if we have entries there.
632 */
633 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
634 list_move_tail(&req->list, &done);
635 continue;
636 }
637 if (!list_empty(&done))
638 break;
639
640 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
641 if (ret < 0)
642 break;
643
644 if (ret && spin)
645 spin = false;
646 ret = 0;
647 }
648
649 if (!list_empty(&done))
650 io_iopoll_complete(ctx, nr_events, &done);
651
652 return ret;
653}
654
655/*
656 * Poll for a mininum of 'min' events. Note that if min == 0 we consider that a
657 * non-spinning poll check - we'll still enter the driver poll loop, but only
658 * as a non-spinning completion check.
659 */
660static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
661 long min)
662{
663 while (!list_empty(&ctx->poll_list)) {
664 int ret;
665
666 ret = io_do_iopoll(ctx, nr_events, min);
667 if (ret < 0)
668 return ret;
669 if (!min || *nr_events >= min)
670 return 0;
671 }
672
673 return 1;
674}
675
676/*
677 * We can't just wait for polled events to come to us, we have to actively
678 * find and complete them.
679 */
680static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
681{
682 if (!(ctx->flags & IORING_SETUP_IOPOLL))
683 return;
684
685 mutex_lock(&ctx->uring_lock);
686 while (!list_empty(&ctx->poll_list)) {
687 unsigned int nr_events = 0;
688
689 io_iopoll_getevents(ctx, &nr_events, 1);
690 }
691 mutex_unlock(&ctx->uring_lock);
692}
693
694static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
695 long min)
696{
697 int ret = 0;
698
699 do {
700 int tmin = 0;
701
702 if (*nr_events < min)
703 tmin = min - *nr_events;
704
705 ret = io_iopoll_getevents(ctx, nr_events, tmin);
706 if (ret <= 0)
707 break;
708 ret = 0;
709 } while (min && !*nr_events && !need_resched());
710
711 return ret;
712}
713
Jens Axboe2b188cc2019-01-07 10:46:33 -0700714static void kiocb_end_write(struct kiocb *kiocb)
715{
716 if (kiocb->ki_flags & IOCB_WRITE) {
717 struct inode *inode = file_inode(kiocb->ki_filp);
718
719 /*
720 * Tell lockdep we inherited freeze protection from submission
721 * thread.
722 */
723 if (S_ISREG(inode->i_mode))
724 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
725 file_end_write(kiocb->ki_filp);
726 }
727}
728
729static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
730{
731 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
732
733 kiocb_end_write(kiocb);
734
Jens Axboe2b188cc2019-01-07 10:46:33 -0700735 io_cqring_add_event(req->ctx, req->user_data, res, 0);
Jens Axboee65ef562019-03-12 10:16:44 -0600736 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700737}
738
Jens Axboedef596e2019-01-09 08:59:42 -0700739static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
740{
741 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
742
743 kiocb_end_write(kiocb);
744
745 req->error = res;
746 if (res != -EAGAIN)
747 req->flags |= REQ_F_IOPOLL_COMPLETED;
748}
749
750/*
751 * After the iocb has been issued, it's safe to be found on the poll list.
752 * Adding the kiocb to the list AFTER submission ensures that we don't
753 * find it from a io_iopoll_getevents() thread before the issuer is done
754 * accessing the kiocb cookie.
755 */
756static void io_iopoll_req_issued(struct io_kiocb *req)
757{
758 struct io_ring_ctx *ctx = req->ctx;
759
760 /*
761 * Track whether we have multiple files in our lists. This will impact
762 * how we do polling eventually, not spinning if we're on potentially
763 * different devices.
764 */
765 if (list_empty(&ctx->poll_list)) {
766 ctx->poll_multi_file = false;
767 } else if (!ctx->poll_multi_file) {
768 struct io_kiocb *list_req;
769
770 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
771 list);
772 if (list_req->rw.ki_filp != req->rw.ki_filp)
773 ctx->poll_multi_file = true;
774 }
775
776 /*
777 * For fast devices, IO may have already completed. If it has, add
778 * it to the front so we find it first.
779 */
780 if (req->flags & REQ_F_IOPOLL_COMPLETED)
781 list_add(&req->list, &ctx->poll_list);
782 else
783 list_add_tail(&req->list, &ctx->poll_list);
784}
785
Jens Axboe3d6770f2019-04-13 11:50:54 -0600786static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -0700787{
Jens Axboe3d6770f2019-04-13 11:50:54 -0600788 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -0700789 int diff = state->has_refs - state->used_refs;
790
791 if (diff)
792 fput_many(state->file, diff);
793 state->file = NULL;
794 }
795}
796
797/*
798 * Get as many references to a file as we have IOs left in this submission,
799 * assuming most submissions are for one file, or at least that each file
800 * has more than one submission.
801 */
802static struct file *io_file_get(struct io_submit_state *state, int fd)
803{
804 if (!state)
805 return fget(fd);
806
807 if (state->file) {
808 if (state->fd == fd) {
809 state->used_refs++;
810 state->ios_left--;
811 return state->file;
812 }
Jens Axboe3d6770f2019-04-13 11:50:54 -0600813 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -0700814 }
815 state->file = fget_many(fd, state->ios_left);
816 if (!state->file)
817 return NULL;
818
819 state->fd = fd;
820 state->has_refs = state->ios_left;
821 state->used_refs = 1;
822 state->ios_left--;
823 return state->file;
824}
825
Jens Axboe2b188cc2019-01-07 10:46:33 -0700826/*
827 * If we tracked the file through the SCM inflight mechanism, we could support
828 * any file. For now, just ensure that anything potentially problematic is done
829 * inline.
830 */
831static bool io_file_supports_async(struct file *file)
832{
833 umode_t mode = file_inode(file)->i_mode;
834
835 if (S_ISBLK(mode) || S_ISCHR(mode))
836 return true;
837 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
838 return true;
839
840 return false;
841}
842
Jens Axboe6c271ce2019-01-10 11:22:30 -0700843static int io_prep_rw(struct io_kiocb *req, const struct sqe_submit *s,
Jens Axboe8358e3a2019-04-23 08:17:58 -0600844 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700845{
Jens Axboe6c271ce2019-01-10 11:22:30 -0700846 const struct io_uring_sqe *sqe = s->sqe;
Jens Axboedef596e2019-01-09 08:59:42 -0700847 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700848 struct kiocb *kiocb = &req->rw;
Jens Axboe09bb8392019-03-13 12:39:28 -0600849 unsigned ioprio;
850 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700851
Jens Axboe09bb8392019-03-13 12:39:28 -0600852 if (!req->file)
853 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700854 /* For -EAGAIN retry, everything is already prepped */
Jens Axboed530a402019-03-13 12:15:01 -0600855 if (req->flags & REQ_F_PREPPED)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700856 return 0;
857
Jens Axboe09bb8392019-03-13 12:39:28 -0600858 if (force_nonblock && !io_file_supports_async(req->file))
859 force_nonblock = false;
Jens Axboe6b063142019-01-10 22:13:58 -0700860
Jens Axboe2b188cc2019-01-07 10:46:33 -0700861 kiocb->ki_pos = READ_ONCE(sqe->off);
862 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
863 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
864
865 ioprio = READ_ONCE(sqe->ioprio);
866 if (ioprio) {
867 ret = ioprio_check_cap(ioprio);
868 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -0600869 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700870
871 kiocb->ki_ioprio = ioprio;
872 } else
873 kiocb->ki_ioprio = get_current_ioprio();
874
875 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
876 if (unlikely(ret))
Jens Axboe09bb8392019-03-13 12:39:28 -0600877 return ret;
Stefan Bühler8449eed2019-04-27 20:34:19 +0200878
879 /* don't allow async punt if RWF_NOWAIT was requested */
880 if (kiocb->ki_flags & IOCB_NOWAIT)
881 req->flags |= REQ_F_NOWAIT;
882
883 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700884 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +0200885
Jens Axboedef596e2019-01-09 08:59:42 -0700886 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -0700887 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
888 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -0600889 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700890
Jens Axboedef596e2019-01-09 08:59:42 -0700891 req->error = 0;
892 kiocb->ki_flags |= IOCB_HIPRI;
893 kiocb->ki_complete = io_complete_rw_iopoll;
894 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -0600895 if (kiocb->ki_flags & IOCB_HIPRI)
896 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -0700897 kiocb->ki_complete = io_complete_rw;
898 }
Jens Axboed530a402019-03-13 12:15:01 -0600899 req->flags |= REQ_F_PREPPED;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700900 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700901}
902
903static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
904{
905 switch (ret) {
906 case -EIOCBQUEUED:
907 break;
908 case -ERESTARTSYS:
909 case -ERESTARTNOINTR:
910 case -ERESTARTNOHAND:
911 case -ERESTART_RESTARTBLOCK:
912 /*
913 * We can't just restart the syscall, since previously
914 * submitted sqes may already be in progress. Just fail this
915 * IO with EINTR.
916 */
917 ret = -EINTR;
918 /* fall through */
919 default:
920 kiocb->ki_complete(kiocb, ret, 0);
921 }
922}
923
Jens Axboeedafcce2019-01-09 09:16:05 -0700924static int io_import_fixed(struct io_ring_ctx *ctx, int rw,
925 const struct io_uring_sqe *sqe,
926 struct iov_iter *iter)
927{
928 size_t len = READ_ONCE(sqe->len);
929 struct io_mapped_ubuf *imu;
930 unsigned index, buf_index;
931 size_t offset;
932 u64 buf_addr;
933
934 /* attempt to use fixed buffers without having provided iovecs */
935 if (unlikely(!ctx->user_bufs))
936 return -EFAULT;
937
938 buf_index = READ_ONCE(sqe->buf_index);
939 if (unlikely(buf_index >= ctx->nr_user_bufs))
940 return -EFAULT;
941
942 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
943 imu = &ctx->user_bufs[index];
944 buf_addr = READ_ONCE(sqe->addr);
945
946 /* overflow */
947 if (buf_addr + len < buf_addr)
948 return -EFAULT;
949 /* not inside the mapped region */
950 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
951 return -EFAULT;
952
953 /*
954 * May not be a start of buffer, set size appropriately
955 * and advance us to the beginning.
956 */
957 offset = buf_addr - imu->ubuf;
958 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
959 if (offset)
960 iov_iter_advance(iter, offset);
Jens Axboe875f1d02019-02-27 13:05:25 -0700961
962 /* don't drop a reference to these pages */
963 iter->type |= ITER_BVEC_FLAG_NO_REF;
Jens Axboeedafcce2019-01-09 09:16:05 -0700964 return 0;
965}
966
Jens Axboe2b188cc2019-01-07 10:46:33 -0700967static int io_import_iovec(struct io_ring_ctx *ctx, int rw,
968 const struct sqe_submit *s, struct iovec **iovec,
969 struct iov_iter *iter)
970{
971 const struct io_uring_sqe *sqe = s->sqe;
972 void __user *buf = u64_to_user_ptr(READ_ONCE(sqe->addr));
973 size_t sqe_len = READ_ONCE(sqe->len);
Jens Axboeedafcce2019-01-09 09:16:05 -0700974 u8 opcode;
975
976 /*
977 * We're reading ->opcode for the second time, but the first read
978 * doesn't care whether it's _FIXED or not, so it doesn't matter
979 * whether ->opcode changes concurrently. The first read does care
980 * about whether it is a READ or a WRITE, so we don't trust this read
981 * for that purpose and instead let the caller pass in the read/write
982 * flag.
983 */
984 opcode = READ_ONCE(sqe->opcode);
985 if (opcode == IORING_OP_READ_FIXED ||
986 opcode == IORING_OP_WRITE_FIXED) {
Jens Axboee0c5c572019-03-12 10:18:47 -0600987 int ret = io_import_fixed(ctx, rw, sqe, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -0700988 *iovec = NULL;
989 return ret;
990 }
Jens Axboe2b188cc2019-01-07 10:46:33 -0700991
992 if (!s->has_user)
993 return -EFAULT;
994
995#ifdef CONFIG_COMPAT
996 if (ctx->compat)
997 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
998 iovec, iter);
999#endif
1000
1001 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
1002}
1003
Jens Axboe31b51512019-01-18 22:56:34 -07001004/*
1005 * Make a note of the last file/offset/direction we punted to async
1006 * context. We'll use this information to see if we can piggy back a
1007 * sequential request onto the previous one, if it's still hasn't been
1008 * completed by the async worker.
1009 */
1010static void io_async_list_note(int rw, struct io_kiocb *req, size_t len)
1011{
1012 struct async_list *async_list = &req->ctx->pending_async[rw];
1013 struct kiocb *kiocb = &req->rw;
1014 struct file *filp = kiocb->ki_filp;
1015 off_t io_end = kiocb->ki_pos + len;
1016
1017 if (filp == async_list->file && kiocb->ki_pos == async_list->io_end) {
1018 unsigned long max_pages;
1019
1020 /* Use 8x RA size as a decent limiter for both reads/writes */
1021 max_pages = filp->f_ra.ra_pages;
1022 if (!max_pages)
Nikolay Borisovb5420232019-03-11 23:28:13 -07001023 max_pages = VM_READAHEAD_PAGES;
Jens Axboe31b51512019-01-18 22:56:34 -07001024 max_pages *= 8;
1025
1026 /* If max pages are exceeded, reset the state */
1027 len >>= PAGE_SHIFT;
1028 if (async_list->io_pages + len <= max_pages) {
1029 req->flags |= REQ_F_SEQ_PREV;
1030 async_list->io_pages += len;
1031 } else {
1032 io_end = 0;
1033 async_list->io_pages = 0;
1034 }
1035 }
1036
1037 /* New file? Reset state. */
1038 if (async_list->file != filp) {
1039 async_list->io_pages = 0;
1040 async_list->file = filp;
1041 }
1042 async_list->io_end = io_end;
1043}
1044
Jens Axboee0c5c572019-03-12 10:18:47 -06001045static int io_read(struct io_kiocb *req, const struct sqe_submit *s,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001046 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001047{
1048 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1049 struct kiocb *kiocb = &req->rw;
1050 struct iov_iter iter;
1051 struct file *file;
Jens Axboe31b51512019-01-18 22:56:34 -07001052 size_t iov_count;
Jens Axboee0c5c572019-03-12 10:18:47 -06001053 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001054
Jens Axboe8358e3a2019-04-23 08:17:58 -06001055 ret = io_prep_rw(req, s, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001056 if (ret)
1057 return ret;
1058 file = kiocb->ki_filp;
1059
Jens Axboe2b188cc2019-01-07 10:46:33 -07001060 if (unlikely(!(file->f_mode & FMODE_READ)))
Jens Axboe09bb8392019-03-13 12:39:28 -06001061 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001062 if (unlikely(!file->f_op->read_iter))
Jens Axboe09bb8392019-03-13 12:39:28 -06001063 return -EINVAL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001064
1065 ret = io_import_iovec(req->ctx, READ, s, &iovec, &iter);
1066 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06001067 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001068
Jens Axboe31b51512019-01-18 22:56:34 -07001069 iov_count = iov_iter_count(&iter);
1070 ret = rw_verify_area(READ, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001071 if (!ret) {
1072 ssize_t ret2;
1073
1074 /* Catch -EAGAIN return for forced non-blocking submission */
1075 ret2 = call_read_iter(file, kiocb, &iter);
Jens Axboe31b51512019-01-18 22:56:34 -07001076 if (!force_nonblock || ret2 != -EAGAIN) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001077 io_rw_done(kiocb, ret2);
Jens Axboe31b51512019-01-18 22:56:34 -07001078 } else {
1079 /*
1080 * If ->needs_lock is true, we're already in async
1081 * context.
1082 */
1083 if (!s->needs_lock)
1084 io_async_list_note(READ, req, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001085 ret = -EAGAIN;
Jens Axboe31b51512019-01-18 22:56:34 -07001086 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001087 }
1088 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001089 return ret;
1090}
1091
Jens Axboee0c5c572019-03-12 10:18:47 -06001092static int io_write(struct io_kiocb *req, const struct sqe_submit *s,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001093 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001094{
1095 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1096 struct kiocb *kiocb = &req->rw;
1097 struct iov_iter iter;
1098 struct file *file;
Jens Axboe31b51512019-01-18 22:56:34 -07001099 size_t iov_count;
Jens Axboee0c5c572019-03-12 10:18:47 -06001100 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001101
Jens Axboe8358e3a2019-04-23 08:17:58 -06001102 ret = io_prep_rw(req, s, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001103 if (ret)
1104 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001105
Jens Axboe2b188cc2019-01-07 10:46:33 -07001106 file = kiocb->ki_filp;
1107 if (unlikely(!(file->f_mode & FMODE_WRITE)))
Jens Axboe09bb8392019-03-13 12:39:28 -06001108 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001109 if (unlikely(!file->f_op->write_iter))
Jens Axboe09bb8392019-03-13 12:39:28 -06001110 return -EINVAL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001111
1112 ret = io_import_iovec(req->ctx, WRITE, s, &iovec, &iter);
1113 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06001114 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001115
Jens Axboe31b51512019-01-18 22:56:34 -07001116 iov_count = iov_iter_count(&iter);
1117
1118 ret = -EAGAIN;
1119 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT)) {
1120 /* If ->needs_lock is true, we're already in async context. */
1121 if (!s->needs_lock)
1122 io_async_list_note(WRITE, req, iov_count);
1123 goto out_free;
1124 }
1125
1126 ret = rw_verify_area(WRITE, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001127 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01001128 ssize_t ret2;
1129
Jens Axboe2b188cc2019-01-07 10:46:33 -07001130 /*
1131 * Open-code file_start_write here to grab freeze protection,
1132 * which will be released by another thread in
1133 * io_complete_rw(). Fool lockdep by telling it the lock got
1134 * released so that it doesn't complain about the held lock when
1135 * we return to userspace.
1136 */
1137 if (S_ISREG(file_inode(file)->i_mode)) {
1138 __sb_start_write(file_inode(file)->i_sb,
1139 SB_FREEZE_WRITE, true);
1140 __sb_writers_release(file_inode(file)->i_sb,
1141 SB_FREEZE_WRITE);
1142 }
1143 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01001144
1145 ret2 = call_write_iter(file, kiocb, &iter);
1146 if (!force_nonblock || ret2 != -EAGAIN) {
1147 io_rw_done(kiocb, ret2);
1148 } else {
1149 /*
1150 * If ->needs_lock is true, we're already in async
1151 * context.
1152 */
1153 if (!s->needs_lock)
1154 io_async_list_note(WRITE, req, iov_count);
1155 ret = -EAGAIN;
1156 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001157 }
Jens Axboe31b51512019-01-18 22:56:34 -07001158out_free:
Jens Axboe2b188cc2019-01-07 10:46:33 -07001159 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001160 return ret;
1161}
1162
1163/*
1164 * IORING_OP_NOP just posts a completion event, nothing else.
1165 */
1166static int io_nop(struct io_kiocb *req, u64 user_data)
1167{
1168 struct io_ring_ctx *ctx = req->ctx;
1169 long err = 0;
1170
Jens Axboedef596e2019-01-09 08:59:42 -07001171 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1172 return -EINVAL;
1173
Jens Axboe2b188cc2019-01-07 10:46:33 -07001174 io_cqring_add_event(ctx, user_data, err, 0);
Jens Axboee65ef562019-03-12 10:16:44 -06001175 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001176 return 0;
1177}
1178
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001179static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1180{
Jens Axboe6b063142019-01-10 22:13:58 -07001181 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001182
Jens Axboe09bb8392019-03-13 12:39:28 -06001183 if (!req->file)
1184 return -EBADF;
Jens Axboed530a402019-03-13 12:15:01 -06001185 /* Prep already done (EAGAIN retry) */
1186 if (req->flags & REQ_F_PREPPED)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001187 return 0;
1188
Jens Axboe6b063142019-01-10 22:13:58 -07001189 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07001190 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07001191 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001192 return -EINVAL;
1193
Jens Axboed530a402019-03-13 12:15:01 -06001194 req->flags |= REQ_F_PREPPED;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001195 return 0;
1196}
1197
1198static int io_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1199 bool force_nonblock)
1200{
1201 loff_t sqe_off = READ_ONCE(sqe->off);
1202 loff_t sqe_len = READ_ONCE(sqe->len);
1203 loff_t end = sqe_off + sqe_len;
1204 unsigned fsync_flags;
1205 int ret;
1206
1207 fsync_flags = READ_ONCE(sqe->fsync_flags);
1208 if (unlikely(fsync_flags & ~IORING_FSYNC_DATASYNC))
1209 return -EINVAL;
1210
1211 ret = io_prep_fsync(req, sqe);
1212 if (ret)
1213 return ret;
1214
1215 /* fsync always requires a blocking context */
1216 if (force_nonblock)
1217 return -EAGAIN;
1218
1219 ret = vfs_fsync_range(req->rw.ki_filp, sqe_off,
1220 end > 0 ? end : LLONG_MAX,
1221 fsync_flags & IORING_FSYNC_DATASYNC);
1222
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001223 io_cqring_add_event(req->ctx, sqe->user_data, ret, 0);
Jens Axboee65ef562019-03-12 10:16:44 -06001224 io_put_req(req);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001225 return 0;
1226}
1227
Jens Axboe221c5eb2019-01-17 09:41:58 -07001228static void io_poll_remove_one(struct io_kiocb *req)
1229{
1230 struct io_poll_iocb *poll = &req->poll;
1231
1232 spin_lock(&poll->head->lock);
1233 WRITE_ONCE(poll->canceled, true);
1234 if (!list_empty(&poll->wait.entry)) {
1235 list_del_init(&poll->wait.entry);
1236 queue_work(req->ctx->sqo_wq, &req->work);
1237 }
1238 spin_unlock(&poll->head->lock);
1239
1240 list_del_init(&req->list);
1241}
1242
1243static void io_poll_remove_all(struct io_ring_ctx *ctx)
1244{
1245 struct io_kiocb *req;
1246
1247 spin_lock_irq(&ctx->completion_lock);
1248 while (!list_empty(&ctx->cancel_list)) {
1249 req = list_first_entry(&ctx->cancel_list, struct io_kiocb,list);
1250 io_poll_remove_one(req);
1251 }
1252 spin_unlock_irq(&ctx->completion_lock);
1253}
1254
1255/*
1256 * Find a running poll command that matches one specified in sqe->addr,
1257 * and remove it if found.
1258 */
1259static int io_poll_remove(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1260{
1261 struct io_ring_ctx *ctx = req->ctx;
1262 struct io_kiocb *poll_req, *next;
1263 int ret = -ENOENT;
1264
1265 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1266 return -EINVAL;
1267 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
1268 sqe->poll_events)
1269 return -EINVAL;
1270
1271 spin_lock_irq(&ctx->completion_lock);
1272 list_for_each_entry_safe(poll_req, next, &ctx->cancel_list, list) {
1273 if (READ_ONCE(sqe->addr) == poll_req->user_data) {
1274 io_poll_remove_one(poll_req);
1275 ret = 0;
1276 break;
1277 }
1278 }
1279 spin_unlock_irq(&ctx->completion_lock);
1280
1281 io_cqring_add_event(req->ctx, sqe->user_data, ret, 0);
Jens Axboee65ef562019-03-12 10:16:44 -06001282 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001283 return 0;
1284}
1285
Jens Axboe8c838782019-03-12 15:48:16 -06001286static void io_poll_complete(struct io_ring_ctx *ctx, struct io_kiocb *req,
1287 __poll_t mask)
Jens Axboe221c5eb2019-01-17 09:41:58 -07001288{
Jens Axboe8c838782019-03-12 15:48:16 -06001289 req->poll.done = true;
1290 io_cqring_fill_event(ctx, req->user_data, mangle_poll(mask), 0);
1291 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001292}
1293
1294static void io_poll_complete_work(struct work_struct *work)
1295{
1296 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
1297 struct io_poll_iocb *poll = &req->poll;
1298 struct poll_table_struct pt = { ._key = poll->events };
1299 struct io_ring_ctx *ctx = req->ctx;
1300 __poll_t mask = 0;
1301
1302 if (!READ_ONCE(poll->canceled))
1303 mask = vfs_poll(poll->file, &pt) & poll->events;
1304
1305 /*
1306 * Note that ->ki_cancel callers also delete iocb from active_reqs after
1307 * calling ->ki_cancel. We need the ctx_lock roundtrip here to
1308 * synchronize with them. In the cancellation case the list_del_init
1309 * itself is not actually needed, but harmless so we keep it in to
1310 * avoid further branches in the fast path.
1311 */
1312 spin_lock_irq(&ctx->completion_lock);
1313 if (!mask && !READ_ONCE(poll->canceled)) {
1314 add_wait_queue(poll->head, &poll->wait);
1315 spin_unlock_irq(&ctx->completion_lock);
1316 return;
1317 }
1318 list_del_init(&req->list);
Jens Axboe8c838782019-03-12 15:48:16 -06001319 io_poll_complete(ctx, req, mask);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001320 spin_unlock_irq(&ctx->completion_lock);
1321
Jens Axboe8c838782019-03-12 15:48:16 -06001322 io_cqring_ev_posted(ctx);
1323 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001324}
1325
1326static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
1327 void *key)
1328{
1329 struct io_poll_iocb *poll = container_of(wait, struct io_poll_iocb,
1330 wait);
1331 struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
1332 struct io_ring_ctx *ctx = req->ctx;
1333 __poll_t mask = key_to_poll(key);
Jens Axboe8c838782019-03-12 15:48:16 -06001334 unsigned long flags;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001335
1336 /* for instances that support it check for an event match first: */
Jens Axboe8c838782019-03-12 15:48:16 -06001337 if (mask && !(mask & poll->events))
1338 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001339
1340 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06001341
1342 if (mask && spin_trylock_irqsave(&ctx->completion_lock, flags)) {
1343 list_del(&req->list);
1344 io_poll_complete(ctx, req, mask);
1345 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1346
1347 io_cqring_ev_posted(ctx);
1348 io_put_req(req);
1349 } else {
1350 queue_work(ctx->sqo_wq, &req->work);
1351 }
1352
Jens Axboe221c5eb2019-01-17 09:41:58 -07001353 return 1;
1354}
1355
1356struct io_poll_table {
1357 struct poll_table_struct pt;
1358 struct io_kiocb *req;
1359 int error;
1360};
1361
1362static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
1363 struct poll_table_struct *p)
1364{
1365 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
1366
1367 if (unlikely(pt->req->poll.head)) {
1368 pt->error = -EINVAL;
1369 return;
1370 }
1371
1372 pt->error = 0;
1373 pt->req->poll.head = head;
1374 add_wait_queue(head, &pt->req->poll.wait);
1375}
1376
1377static int io_poll_add(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1378{
1379 struct io_poll_iocb *poll = &req->poll;
1380 struct io_ring_ctx *ctx = req->ctx;
1381 struct io_poll_table ipt;
Jens Axboe8c838782019-03-12 15:48:16 -06001382 bool cancel = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001383 __poll_t mask;
1384 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001385
1386 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1387 return -EINVAL;
1388 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
1389 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06001390 if (!poll->file)
1391 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001392
1393 INIT_WORK(&req->work, io_poll_complete_work);
1394 events = READ_ONCE(sqe->poll_events);
1395 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
1396
Jens Axboe221c5eb2019-01-17 09:41:58 -07001397 poll->head = NULL;
Jens Axboe8c838782019-03-12 15:48:16 -06001398 poll->done = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001399 poll->canceled = false;
1400
1401 ipt.pt._qproc = io_poll_queue_proc;
1402 ipt.pt._key = poll->events;
1403 ipt.req = req;
1404 ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
1405
1406 /* initialized the list so that we can do list_empty checks */
1407 INIT_LIST_HEAD(&poll->wait.entry);
1408 init_waitqueue_func_entry(&poll->wait, io_poll_wake);
1409
Jens Axboe221c5eb2019-01-17 09:41:58 -07001410 mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001411
1412 spin_lock_irq(&ctx->completion_lock);
Jens Axboe8c838782019-03-12 15:48:16 -06001413 if (likely(poll->head)) {
1414 spin_lock(&poll->head->lock);
1415 if (unlikely(list_empty(&poll->wait.entry))) {
1416 if (ipt.error)
1417 cancel = true;
1418 ipt.error = 0;
1419 mask = 0;
1420 }
1421 if (mask || ipt.error)
1422 list_del_init(&poll->wait.entry);
1423 else if (cancel)
1424 WRITE_ONCE(poll->canceled, true);
1425 else if (!poll->done) /* actually waiting for an event */
1426 list_add_tail(&req->list, &ctx->cancel_list);
1427 spin_unlock(&poll->head->lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001428 }
Jens Axboe8c838782019-03-12 15:48:16 -06001429 if (mask) { /* no async, we'd stolen it */
1430 req->error = mangle_poll(mask);
1431 ipt.error = 0;
1432 io_poll_complete(ctx, req, mask);
1433 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07001434 spin_unlock_irq(&ctx->completion_lock);
1435
Jens Axboe8c838782019-03-12 15:48:16 -06001436 if (mask) {
1437 io_cqring_ev_posted(ctx);
Jens Axboee65ef562019-03-12 10:16:44 -06001438 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001439 }
Jens Axboe8c838782019-03-12 15:48:16 -06001440 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001441}
1442
Jens Axboe2b188cc2019-01-07 10:46:33 -07001443static int __io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001444 const struct sqe_submit *s, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001445{
Jens Axboee0c5c572019-03-12 10:18:47 -06001446 int ret, opcode;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001447
1448 if (unlikely(s->index >= ctx->sq_entries))
1449 return -EINVAL;
1450 req->user_data = READ_ONCE(s->sqe->user_data);
1451
1452 opcode = READ_ONCE(s->sqe->opcode);
1453 switch (opcode) {
1454 case IORING_OP_NOP:
1455 ret = io_nop(req, req->user_data);
1456 break;
1457 case IORING_OP_READV:
Jens Axboeedafcce2019-01-09 09:16:05 -07001458 if (unlikely(s->sqe->buf_index))
1459 return -EINVAL;
Jens Axboe8358e3a2019-04-23 08:17:58 -06001460 ret = io_read(req, s, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001461 break;
1462 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07001463 if (unlikely(s->sqe->buf_index))
1464 return -EINVAL;
Jens Axboe8358e3a2019-04-23 08:17:58 -06001465 ret = io_write(req, s, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07001466 break;
1467 case IORING_OP_READ_FIXED:
Jens Axboe8358e3a2019-04-23 08:17:58 -06001468 ret = io_read(req, s, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07001469 break;
1470 case IORING_OP_WRITE_FIXED:
Jens Axboe8358e3a2019-04-23 08:17:58 -06001471 ret = io_write(req, s, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001472 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001473 case IORING_OP_FSYNC:
1474 ret = io_fsync(req, s->sqe, force_nonblock);
1475 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001476 case IORING_OP_POLL_ADD:
1477 ret = io_poll_add(req, s->sqe);
1478 break;
1479 case IORING_OP_POLL_REMOVE:
1480 ret = io_poll_remove(req, s->sqe);
1481 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001482 default:
1483 ret = -EINVAL;
1484 break;
1485 }
1486
Jens Axboedef596e2019-01-09 08:59:42 -07001487 if (ret)
1488 return ret;
1489
1490 if (ctx->flags & IORING_SETUP_IOPOLL) {
1491 if (req->error == -EAGAIN)
1492 return -EAGAIN;
1493
1494 /* workqueue context doesn't hold uring_lock, grab it now */
1495 if (s->needs_lock)
1496 mutex_lock(&ctx->uring_lock);
1497 io_iopoll_req_issued(req);
1498 if (s->needs_lock)
1499 mutex_unlock(&ctx->uring_lock);
1500 }
1501
1502 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001503}
1504
Jens Axboe31b51512019-01-18 22:56:34 -07001505static struct async_list *io_async_list_from_sqe(struct io_ring_ctx *ctx,
1506 const struct io_uring_sqe *sqe)
1507{
1508 switch (sqe->opcode) {
1509 case IORING_OP_READV:
1510 case IORING_OP_READ_FIXED:
1511 return &ctx->pending_async[READ];
1512 case IORING_OP_WRITEV:
1513 case IORING_OP_WRITE_FIXED:
1514 return &ctx->pending_async[WRITE];
1515 default:
1516 return NULL;
1517 }
1518}
1519
Jens Axboeedafcce2019-01-09 09:16:05 -07001520static inline bool io_sqe_needs_user(const struct io_uring_sqe *sqe)
1521{
1522 u8 opcode = READ_ONCE(sqe->opcode);
1523
1524 return !(opcode == IORING_OP_READ_FIXED ||
1525 opcode == IORING_OP_WRITE_FIXED);
1526}
1527
Jens Axboe2b188cc2019-01-07 10:46:33 -07001528static void io_sq_wq_submit_work(struct work_struct *work)
1529{
1530 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001531 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe31b51512019-01-18 22:56:34 -07001532 struct mm_struct *cur_mm = NULL;
1533 struct async_list *async_list;
1534 LIST_HEAD(req_list);
Jens Axboeedafcce2019-01-09 09:16:05 -07001535 mm_segment_t old_fs;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001536 int ret;
1537
Jens Axboe31b51512019-01-18 22:56:34 -07001538 async_list = io_async_list_from_sqe(ctx, req->submit.sqe);
1539restart:
1540 do {
1541 struct sqe_submit *s = &req->submit;
1542 const struct io_uring_sqe *sqe = s->sqe;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001543
Stefan Bühler8449eed2019-04-27 20:34:19 +02001544 /* Ensure we clear previously set non-block flag */
Jens Axboe31b51512019-01-18 22:56:34 -07001545 req->rw.ki_flags &= ~IOCB_NOWAIT;
1546
1547 ret = 0;
1548 if (io_sqe_needs_user(sqe) && !cur_mm) {
1549 if (!mmget_not_zero(ctx->sqo_mm)) {
1550 ret = -EFAULT;
1551 } else {
1552 cur_mm = ctx->sqo_mm;
1553 use_mm(cur_mm);
1554 old_fs = get_fs();
1555 set_fs(USER_DS);
1556 }
1557 }
1558
1559 if (!ret) {
1560 s->has_user = cur_mm != NULL;
1561 s->needs_lock = true;
1562 do {
Jens Axboe8358e3a2019-04-23 08:17:58 -06001563 ret = __io_submit_sqe(ctx, req, s, false);
Jens Axboe31b51512019-01-18 22:56:34 -07001564 /*
1565 * We can get EAGAIN for polled IO even though
1566 * we're forcing a sync submission from here,
1567 * since we can't wait for request slots on the
1568 * block side.
1569 */
1570 if (ret != -EAGAIN)
1571 break;
1572 cond_resched();
1573 } while (1);
Jens Axboee65ef562019-03-12 10:16:44 -06001574
1575 /* drop submission reference */
1576 io_put_req(req);
Jens Axboe31b51512019-01-18 22:56:34 -07001577 }
1578 if (ret) {
1579 io_cqring_add_event(ctx, sqe->user_data, ret, 0);
Jens Axboee65ef562019-03-12 10:16:44 -06001580 io_put_req(req);
Jens Axboe31b51512019-01-18 22:56:34 -07001581 }
1582
1583 /* async context always use a copy of the sqe */
1584 kfree(sqe);
1585
1586 if (!async_list)
1587 break;
1588 if (!list_empty(&req_list)) {
1589 req = list_first_entry(&req_list, struct io_kiocb,
1590 list);
1591 list_del(&req->list);
1592 continue;
1593 }
1594 if (list_empty(&async_list->list))
1595 break;
1596
1597 req = NULL;
1598 spin_lock(&async_list->lock);
1599 if (list_empty(&async_list->list)) {
1600 spin_unlock(&async_list->lock);
1601 break;
1602 }
1603 list_splice_init(&async_list->list, &req_list);
1604 spin_unlock(&async_list->lock);
1605
1606 req = list_first_entry(&req_list, struct io_kiocb, list);
1607 list_del(&req->list);
1608 } while (req);
Jens Axboeedafcce2019-01-09 09:16:05 -07001609
1610 /*
Jens Axboe31b51512019-01-18 22:56:34 -07001611 * Rare case of racing with a submitter. If we find the count has
1612 * dropped to zero AND we have pending work items, then restart
1613 * the processing. This is a tiny race window.
Jens Axboeedafcce2019-01-09 09:16:05 -07001614 */
Jens Axboe31b51512019-01-18 22:56:34 -07001615 if (async_list) {
1616 ret = atomic_dec_return(&async_list->cnt);
1617 while (!ret && !list_empty(&async_list->list)) {
1618 spin_lock(&async_list->lock);
1619 atomic_inc(&async_list->cnt);
1620 list_splice_init(&async_list->list, &req_list);
1621 spin_unlock(&async_list->lock);
1622
1623 if (!list_empty(&req_list)) {
1624 req = list_first_entry(&req_list,
1625 struct io_kiocb, list);
1626 list_del(&req->list);
1627 goto restart;
1628 }
1629 ret = atomic_dec_return(&async_list->cnt);
Jens Axboeedafcce2019-01-09 09:16:05 -07001630 }
Jens Axboeedafcce2019-01-09 09:16:05 -07001631 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001632
Jens Axboe31b51512019-01-18 22:56:34 -07001633 if (cur_mm) {
Jens Axboeedafcce2019-01-09 09:16:05 -07001634 set_fs(old_fs);
Jens Axboe31b51512019-01-18 22:56:34 -07001635 unuse_mm(cur_mm);
1636 mmput(cur_mm);
Jens Axboeedafcce2019-01-09 09:16:05 -07001637 }
Jens Axboe31b51512019-01-18 22:56:34 -07001638}
Jens Axboe2b188cc2019-01-07 10:46:33 -07001639
Jens Axboe31b51512019-01-18 22:56:34 -07001640/*
1641 * See if we can piggy back onto previously submitted work, that is still
1642 * running. We currently only allow this if the new request is sequential
1643 * to the previous one we punted.
1644 */
1645static bool io_add_to_prev_work(struct async_list *list, struct io_kiocb *req)
1646{
1647 bool ret = false;
1648
1649 if (!list)
1650 return false;
1651 if (!(req->flags & REQ_F_SEQ_PREV))
1652 return false;
1653 if (!atomic_read(&list->cnt))
1654 return false;
1655
1656 ret = true;
1657 spin_lock(&list->lock);
1658 list_add_tail(&req->list, &list->list);
1659 if (!atomic_read(&list->cnt)) {
1660 list_del_init(&req->list);
1661 ret = false;
1662 }
1663 spin_unlock(&list->lock);
1664 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001665}
1666
Jens Axboe09bb8392019-03-13 12:39:28 -06001667static bool io_op_needs_file(const struct io_uring_sqe *sqe)
1668{
1669 int op = READ_ONCE(sqe->opcode);
1670
1671 switch (op) {
1672 case IORING_OP_NOP:
1673 case IORING_OP_POLL_REMOVE:
1674 return false;
1675 default:
1676 return true;
1677 }
1678}
1679
1680static int io_req_set_file(struct io_ring_ctx *ctx, const struct sqe_submit *s,
1681 struct io_submit_state *state, struct io_kiocb *req)
1682{
1683 unsigned flags;
1684 int fd;
1685
1686 flags = READ_ONCE(s->sqe->flags);
1687 fd = READ_ONCE(s->sqe->fd);
1688
1689 if (!io_op_needs_file(s->sqe)) {
1690 req->file = NULL;
1691 return 0;
1692 }
1693
1694 if (flags & IOSQE_FIXED_FILE) {
1695 if (unlikely(!ctx->user_files ||
1696 (unsigned) fd >= ctx->nr_user_files))
1697 return -EBADF;
1698 req->file = ctx->user_files[fd];
1699 req->flags |= REQ_F_FIXED_FILE;
1700 } else {
1701 if (s->needs_fixed_file)
1702 return -EBADF;
1703 req->file = io_file_get(state, fd);
1704 if (unlikely(!req->file))
1705 return -EBADF;
1706 }
1707
1708 return 0;
1709}
1710
Jens Axboe9a56a232019-01-09 09:06:50 -07001711static int io_submit_sqe(struct io_ring_ctx *ctx, struct sqe_submit *s,
1712 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001713{
1714 struct io_kiocb *req;
Jens Axboee0c5c572019-03-12 10:18:47 -06001715 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001716
1717 /* enforce forwards compatibility on users */
Jens Axboe6b063142019-01-10 22:13:58 -07001718 if (unlikely(s->sqe->flags & ~IOSQE_FIXED_FILE))
Jens Axboe2b188cc2019-01-07 10:46:33 -07001719 return -EINVAL;
1720
Jens Axboe2579f912019-01-09 09:10:43 -07001721 req = io_get_req(ctx, state);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001722 if (unlikely(!req))
1723 return -EAGAIN;
1724
Jens Axboe09bb8392019-03-13 12:39:28 -06001725 ret = io_req_set_file(ctx, s, state, req);
1726 if (unlikely(ret))
1727 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001728
Jens Axboe8358e3a2019-04-23 08:17:58 -06001729 ret = __io_submit_sqe(ctx, req, s, true);
Stefan Bühler8449eed2019-04-27 20:34:19 +02001730 if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001731 struct io_uring_sqe *sqe_copy;
1732
1733 sqe_copy = kmalloc(sizeof(*sqe_copy), GFP_KERNEL);
1734 if (sqe_copy) {
Jens Axboe31b51512019-01-18 22:56:34 -07001735 struct async_list *list;
1736
Jens Axboe2b188cc2019-01-07 10:46:33 -07001737 memcpy(sqe_copy, s->sqe, sizeof(*sqe_copy));
1738 s->sqe = sqe_copy;
1739
1740 memcpy(&req->submit, s, sizeof(*s));
Jens Axboe31b51512019-01-18 22:56:34 -07001741 list = io_async_list_from_sqe(ctx, s->sqe);
1742 if (!io_add_to_prev_work(list, req)) {
1743 if (list)
1744 atomic_inc(&list->cnt);
1745 INIT_WORK(&req->work, io_sq_wq_submit_work);
1746 queue_work(ctx->sqo_wq, &req->work);
1747 }
Jens Axboee65ef562019-03-12 10:16:44 -06001748
1749 /*
1750 * Queued up for async execution, worker will release
1751 * submit reference when the iocb is actually
1752 * submitted.
1753 */
1754 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001755 }
1756 }
Jens Axboee65ef562019-03-12 10:16:44 -06001757
Jens Axboe09bb8392019-03-13 12:39:28 -06001758out:
Jens Axboee65ef562019-03-12 10:16:44 -06001759 /* drop submission reference */
1760 io_put_req(req);
1761
1762 /* and drop final reference, if we failed */
Jens Axboe2b188cc2019-01-07 10:46:33 -07001763 if (ret)
Jens Axboee65ef562019-03-12 10:16:44 -06001764 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001765
1766 return ret;
1767}
1768
Jens Axboe9a56a232019-01-09 09:06:50 -07001769/*
1770 * Batched submission is done, ensure local IO is flushed out.
1771 */
1772static void io_submit_state_end(struct io_submit_state *state)
1773{
1774 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06001775 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07001776 if (state->free_reqs)
1777 kmem_cache_free_bulk(req_cachep, state->free_reqs,
1778 &state->reqs[state->cur_req]);
Jens Axboe9a56a232019-01-09 09:06:50 -07001779}
1780
1781/*
1782 * Start submission side cache.
1783 */
1784static void io_submit_state_start(struct io_submit_state *state,
1785 struct io_ring_ctx *ctx, unsigned max_ios)
1786{
1787 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07001788 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07001789 state->file = NULL;
1790 state->ios_left = max_ios;
1791}
1792
Jens Axboe2b188cc2019-01-07 10:46:33 -07001793static void io_commit_sqring(struct io_ring_ctx *ctx)
1794{
1795 struct io_sq_ring *ring = ctx->sq_ring;
1796
1797 if (ctx->cached_sq_head != READ_ONCE(ring->r.head)) {
1798 /*
1799 * Ensure any loads from the SQEs are done at this point,
1800 * since once we write the new head, the application could
1801 * write new data to them.
1802 */
1803 smp_store_release(&ring->r.head, ctx->cached_sq_head);
1804
1805 /*
1806 * write side barrier of head update, app has read side. See
1807 * comment at the top of this file
1808 */
1809 smp_wmb();
1810 }
1811}
1812
1813/*
1814 * Undo last io_get_sqring()
1815 */
1816static void io_drop_sqring(struct io_ring_ctx *ctx)
1817{
1818 ctx->cached_sq_head--;
1819}
1820
1821/*
1822 * Fetch an sqe, if one is available. Note that s->sqe will point to memory
1823 * that is mapped by userspace. This means that care needs to be taken to
1824 * ensure that reads are stable, as we cannot rely on userspace always
1825 * being a good citizen. If members of the sqe are validated and then later
1826 * used, it's important that those reads are done through READ_ONCE() to
1827 * prevent a re-load down the line.
1828 */
1829static bool io_get_sqring(struct io_ring_ctx *ctx, struct sqe_submit *s)
1830{
1831 struct io_sq_ring *ring = ctx->sq_ring;
1832 unsigned head;
1833
1834 /*
1835 * The cached sq head (or cq tail) serves two purposes:
1836 *
1837 * 1) allows us to batch the cost of updating the user visible
1838 * head updates.
1839 * 2) allows the kernel side to track the head on its own, even
1840 * though the application is the one updating it.
1841 */
1842 head = ctx->cached_sq_head;
1843 /* See comment at the top of this file */
1844 smp_rmb();
Stefan Bühlere523a292019-04-19 11:57:44 +02001845 /* make sure SQ entry isn't read before tail */
1846 if (head == smp_load_acquire(&ring->r.tail))
Jens Axboe2b188cc2019-01-07 10:46:33 -07001847 return false;
1848
1849 head = READ_ONCE(ring->array[head & ctx->sq_mask]);
1850 if (head < ctx->sq_entries) {
1851 s->index = head;
1852 s->sqe = &ctx->sq_sqes[head];
1853 ctx->cached_sq_head++;
1854 return true;
1855 }
1856
1857 /* drop invalid entries */
1858 ctx->cached_sq_head++;
1859 ring->dropped++;
1860 /* See comment at the top of this file */
1861 smp_wmb();
1862 return false;
1863}
1864
Jens Axboe6c271ce2019-01-10 11:22:30 -07001865static int io_submit_sqes(struct io_ring_ctx *ctx, struct sqe_submit *sqes,
1866 unsigned int nr, bool has_user, bool mm_fault)
1867{
1868 struct io_submit_state state, *statep = NULL;
1869 int ret, i, submitted = 0;
1870
1871 if (nr > IO_PLUG_THRESHOLD) {
1872 io_submit_state_start(&state, ctx, nr);
1873 statep = &state;
1874 }
1875
1876 for (i = 0; i < nr; i++) {
1877 if (unlikely(mm_fault)) {
1878 ret = -EFAULT;
1879 } else {
1880 sqes[i].has_user = has_user;
1881 sqes[i].needs_lock = true;
1882 sqes[i].needs_fixed_file = true;
1883 ret = io_submit_sqe(ctx, &sqes[i], statep);
1884 }
1885 if (!ret) {
1886 submitted++;
1887 continue;
1888 }
1889
1890 io_cqring_add_event(ctx, sqes[i].sqe->user_data, ret, 0);
1891 }
1892
1893 if (statep)
1894 io_submit_state_end(&state);
1895
1896 return submitted;
1897}
1898
1899static int io_sq_thread(void *data)
1900{
1901 struct sqe_submit sqes[IO_IOPOLL_BATCH];
1902 struct io_ring_ctx *ctx = data;
1903 struct mm_struct *cur_mm = NULL;
1904 mm_segment_t old_fs;
1905 DEFINE_WAIT(wait);
1906 unsigned inflight;
1907 unsigned long timeout;
1908
1909 old_fs = get_fs();
1910 set_fs(USER_DS);
1911
1912 timeout = inflight = 0;
1913 while (!kthread_should_stop() && !ctx->sqo_stop) {
1914 bool all_fixed, mm_fault = false;
1915 int i;
1916
1917 if (inflight) {
1918 unsigned nr_events = 0;
1919
1920 if (ctx->flags & IORING_SETUP_IOPOLL) {
1921 /*
1922 * We disallow the app entering submit/complete
1923 * with polling, but we still need to lock the
1924 * ring to prevent racing with polled issue
1925 * that got punted to a workqueue.
1926 */
1927 mutex_lock(&ctx->uring_lock);
1928 io_iopoll_check(ctx, &nr_events, 0);
1929 mutex_unlock(&ctx->uring_lock);
1930 } else {
1931 /*
1932 * Normal IO, just pretend everything completed.
1933 * We don't have to poll completions for that.
1934 */
1935 nr_events = inflight;
1936 }
1937
1938 inflight -= nr_events;
1939 if (!inflight)
1940 timeout = jiffies + ctx->sq_thread_idle;
1941 }
1942
1943 if (!io_get_sqring(ctx, &sqes[0])) {
1944 /*
1945 * We're polling. If we're within the defined idle
1946 * period, then let us spin without work before going
1947 * to sleep.
1948 */
1949 if (inflight || !time_after(jiffies, timeout)) {
1950 cpu_relax();
1951 continue;
1952 }
1953
1954 /*
1955 * Drop cur_mm before scheduling, we can't hold it for
1956 * long periods (or over schedule()). Do this before
1957 * adding ourselves to the waitqueue, as the unuse/drop
1958 * may sleep.
1959 */
1960 if (cur_mm) {
1961 unuse_mm(cur_mm);
1962 mmput(cur_mm);
1963 cur_mm = NULL;
1964 }
1965
1966 prepare_to_wait(&ctx->sqo_wait, &wait,
1967 TASK_INTERRUPTIBLE);
1968
1969 /* Tell userspace we may need a wakeup call */
1970 ctx->sq_ring->flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02001971 /* make sure to read SQ tail after writing flags */
1972 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07001973
1974 if (!io_get_sqring(ctx, &sqes[0])) {
1975 if (kthread_should_stop()) {
1976 finish_wait(&ctx->sqo_wait, &wait);
1977 break;
1978 }
1979 if (signal_pending(current))
1980 flush_signals(current);
1981 schedule();
1982 finish_wait(&ctx->sqo_wait, &wait);
1983
1984 ctx->sq_ring->flags &= ~IORING_SQ_NEED_WAKEUP;
1985 smp_wmb();
1986 continue;
1987 }
1988 finish_wait(&ctx->sqo_wait, &wait);
1989
1990 ctx->sq_ring->flags &= ~IORING_SQ_NEED_WAKEUP;
1991 smp_wmb();
1992 }
1993
1994 i = 0;
1995 all_fixed = true;
1996 do {
1997 if (all_fixed && io_sqe_needs_user(sqes[i].sqe))
1998 all_fixed = false;
1999
2000 i++;
2001 if (i == ARRAY_SIZE(sqes))
2002 break;
2003 } while (io_get_sqring(ctx, &sqes[i]));
2004
2005 /* Unless all new commands are FIXED regions, grab mm */
2006 if (!all_fixed && !cur_mm) {
2007 mm_fault = !mmget_not_zero(ctx->sqo_mm);
2008 if (!mm_fault) {
2009 use_mm(ctx->sqo_mm);
2010 cur_mm = ctx->sqo_mm;
2011 }
2012 }
2013
2014 inflight += io_submit_sqes(ctx, sqes, i, cur_mm != NULL,
2015 mm_fault);
2016
2017 /* Commit SQ ring head once we've consumed all SQEs */
2018 io_commit_sqring(ctx);
2019 }
2020
2021 set_fs(old_fs);
2022 if (cur_mm) {
2023 unuse_mm(cur_mm);
2024 mmput(cur_mm);
2025 }
Jens Axboe06058632019-04-13 09:26:03 -06002026
2027 if (kthread_should_park())
2028 kthread_parkme();
2029
Jens Axboe6c271ce2019-01-10 11:22:30 -07002030 return 0;
2031}
2032
Jens Axboe2b188cc2019-01-07 10:46:33 -07002033static int io_ring_submit(struct io_ring_ctx *ctx, unsigned int to_submit)
2034{
Jens Axboe9a56a232019-01-09 09:06:50 -07002035 struct io_submit_state state, *statep = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002036 int i, ret = 0, submit = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002037
Jens Axboe9a56a232019-01-09 09:06:50 -07002038 if (to_submit > IO_PLUG_THRESHOLD) {
2039 io_submit_state_start(&state, ctx, to_submit);
2040 statep = &state;
2041 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002042
2043 for (i = 0; i < to_submit; i++) {
2044 struct sqe_submit s;
2045
2046 if (!io_get_sqring(ctx, &s))
2047 break;
2048
2049 s.has_user = true;
Jens Axboedef596e2019-01-09 08:59:42 -07002050 s.needs_lock = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002051 s.needs_fixed_file = false;
Jens Axboedef596e2019-01-09 08:59:42 -07002052
Jens Axboe9a56a232019-01-09 09:06:50 -07002053 ret = io_submit_sqe(ctx, &s, statep);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002054 if (ret) {
2055 io_drop_sqring(ctx);
2056 break;
2057 }
2058
2059 submit++;
2060 }
2061 io_commit_sqring(ctx);
2062
Jens Axboe9a56a232019-01-09 09:06:50 -07002063 if (statep)
2064 io_submit_state_end(statep);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002065
2066 return submit ? submit : ret;
2067}
2068
2069static unsigned io_cqring_events(struct io_cq_ring *ring)
2070{
2071 return READ_ONCE(ring->r.tail) - READ_ONCE(ring->r.head);
2072}
2073
2074/*
2075 * Wait until events become available, if we don't already have some. The
2076 * application must reap them itself, as they reside on the shared cq ring.
2077 */
2078static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
2079 const sigset_t __user *sig, size_t sigsz)
2080{
2081 struct io_cq_ring *ring = ctx->cq_ring;
2082 sigset_t ksigmask, sigsaved;
2083 DEFINE_WAIT(wait);
2084 int ret;
2085
2086 /* See comment at the top of this file */
2087 smp_rmb();
2088 if (io_cqring_events(ring) >= min_events)
2089 return 0;
2090
2091 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01002092#ifdef CONFIG_COMPAT
2093 if (in_compat_syscall())
2094 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
2095 &ksigmask, &sigsaved, sigsz);
2096 else
2097#endif
2098 ret = set_user_sigmask(sig, &ksigmask,
2099 &sigsaved, sigsz);
2100
Jens Axboe2b188cc2019-01-07 10:46:33 -07002101 if (ret)
2102 return ret;
2103 }
2104
2105 do {
2106 prepare_to_wait(&ctx->wait, &wait, TASK_INTERRUPTIBLE);
2107
2108 ret = 0;
2109 /* See comment at the top of this file */
2110 smp_rmb();
2111 if (io_cqring_events(ring) >= min_events)
2112 break;
2113
2114 schedule();
2115
2116 ret = -EINTR;
2117 if (signal_pending(current))
2118 break;
2119 } while (1);
2120
2121 finish_wait(&ctx->wait, &wait);
2122
2123 if (sig)
2124 restore_user_sigmask(sig, &sigsaved);
2125
2126 return READ_ONCE(ring->r.head) == READ_ONCE(ring->r.tail) ? ret : 0;
2127}
2128
Jens Axboe6b063142019-01-10 22:13:58 -07002129static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
2130{
2131#if defined(CONFIG_UNIX)
2132 if (ctx->ring_sock) {
2133 struct sock *sock = ctx->ring_sock->sk;
2134 struct sk_buff *skb;
2135
2136 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
2137 kfree_skb(skb);
2138 }
2139#else
2140 int i;
2141
2142 for (i = 0; i < ctx->nr_user_files; i++)
2143 fput(ctx->user_files[i]);
2144#endif
2145}
2146
2147static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
2148{
2149 if (!ctx->user_files)
2150 return -ENXIO;
2151
2152 __io_sqe_files_unregister(ctx);
2153 kfree(ctx->user_files);
2154 ctx->user_files = NULL;
2155 ctx->nr_user_files = 0;
2156 return 0;
2157}
2158
Jens Axboe6c271ce2019-01-10 11:22:30 -07002159static void io_sq_thread_stop(struct io_ring_ctx *ctx)
2160{
2161 if (ctx->sqo_thread) {
2162 ctx->sqo_stop = 1;
2163 mb();
Jens Axboe06058632019-04-13 09:26:03 -06002164 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07002165 kthread_stop(ctx->sqo_thread);
2166 ctx->sqo_thread = NULL;
2167 }
2168}
2169
Jens Axboe6b063142019-01-10 22:13:58 -07002170static void io_finish_async(struct io_ring_ctx *ctx)
2171{
Jens Axboe6c271ce2019-01-10 11:22:30 -07002172 io_sq_thread_stop(ctx);
2173
Jens Axboe6b063142019-01-10 22:13:58 -07002174 if (ctx->sqo_wq) {
2175 destroy_workqueue(ctx->sqo_wq);
2176 ctx->sqo_wq = NULL;
2177 }
2178}
2179
2180#if defined(CONFIG_UNIX)
2181static void io_destruct_skb(struct sk_buff *skb)
2182{
2183 struct io_ring_ctx *ctx = skb->sk->sk_user_data;
2184
2185 io_finish_async(ctx);
2186 unix_destruct_scm(skb);
2187}
2188
2189/*
2190 * Ensure the UNIX gc is aware of our file set, so we are certain that
2191 * the io_uring can be safely unregistered on process exit, even if we have
2192 * loops in the file referencing.
2193 */
2194static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
2195{
2196 struct sock *sk = ctx->ring_sock->sk;
2197 struct scm_fp_list *fpl;
2198 struct sk_buff *skb;
2199 int i;
2200
2201 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
2202 unsigned long inflight = ctx->user->unix_inflight + nr;
2203
2204 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
2205 return -EMFILE;
2206 }
2207
2208 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
2209 if (!fpl)
2210 return -ENOMEM;
2211
2212 skb = alloc_skb(0, GFP_KERNEL);
2213 if (!skb) {
2214 kfree(fpl);
2215 return -ENOMEM;
2216 }
2217
2218 skb->sk = sk;
2219 skb->destructor = io_destruct_skb;
2220
2221 fpl->user = get_uid(ctx->user);
2222 for (i = 0; i < nr; i++) {
2223 fpl->fp[i] = get_file(ctx->user_files[i + offset]);
2224 unix_inflight(fpl->user, fpl->fp[i]);
2225 }
2226
2227 fpl->max = fpl->count = nr;
2228 UNIXCB(skb).fp = fpl;
2229 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
2230 skb_queue_head(&sk->sk_receive_queue, skb);
2231
2232 for (i = 0; i < nr; i++)
2233 fput(fpl->fp[i]);
2234
2235 return 0;
2236}
2237
2238/*
2239 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
2240 * causes regular reference counting to break down. We rely on the UNIX
2241 * garbage collection to take care of this problem for us.
2242 */
2243static int io_sqe_files_scm(struct io_ring_ctx *ctx)
2244{
2245 unsigned left, total;
2246 int ret = 0;
2247
2248 total = 0;
2249 left = ctx->nr_user_files;
2250 while (left) {
2251 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
2252 int ret;
2253
2254 ret = __io_sqe_files_scm(ctx, this_files, total);
2255 if (ret)
2256 break;
2257 left -= this_files;
2258 total += this_files;
2259 }
2260
2261 if (!ret)
2262 return 0;
2263
2264 while (total < ctx->nr_user_files) {
2265 fput(ctx->user_files[total]);
2266 total++;
2267 }
2268
2269 return ret;
2270}
2271#else
2272static int io_sqe_files_scm(struct io_ring_ctx *ctx)
2273{
2274 return 0;
2275}
2276#endif
2277
2278static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
2279 unsigned nr_args)
2280{
2281 __s32 __user *fds = (__s32 __user *) arg;
2282 int fd, ret = 0;
2283 unsigned i;
2284
2285 if (ctx->user_files)
2286 return -EBUSY;
2287 if (!nr_args)
2288 return -EINVAL;
2289 if (nr_args > IORING_MAX_FIXED_FILES)
2290 return -EMFILE;
2291
2292 ctx->user_files = kcalloc(nr_args, sizeof(struct file *), GFP_KERNEL);
2293 if (!ctx->user_files)
2294 return -ENOMEM;
2295
2296 for (i = 0; i < nr_args; i++) {
2297 ret = -EFAULT;
2298 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
2299 break;
2300
2301 ctx->user_files[i] = fget(fd);
2302
2303 ret = -EBADF;
2304 if (!ctx->user_files[i])
2305 break;
2306 /*
2307 * Don't allow io_uring instances to be registered. If UNIX
2308 * isn't enabled, then this causes a reference cycle and this
2309 * instance can never get freed. If UNIX is enabled we'll
2310 * handle it just fine, but there's still no point in allowing
2311 * a ring fd as it doesn't support regular read/write anyway.
2312 */
2313 if (ctx->user_files[i]->f_op == &io_uring_fops) {
2314 fput(ctx->user_files[i]);
2315 break;
2316 }
2317 ctx->nr_user_files++;
2318 ret = 0;
2319 }
2320
2321 if (ret) {
2322 for (i = 0; i < ctx->nr_user_files; i++)
2323 fput(ctx->user_files[i]);
2324
2325 kfree(ctx->user_files);
Jens Axboe25adf502019-04-03 09:52:40 -06002326 ctx->user_files = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07002327 ctx->nr_user_files = 0;
2328 return ret;
2329 }
2330
2331 ret = io_sqe_files_scm(ctx);
2332 if (ret)
2333 io_sqe_files_unregister(ctx);
2334
2335 return ret;
2336}
2337
Jens Axboe6c271ce2019-01-10 11:22:30 -07002338static int io_sq_offload_start(struct io_ring_ctx *ctx,
2339 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002340{
2341 int ret;
2342
Jens Axboe6c271ce2019-01-10 11:22:30 -07002343 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002344 mmgrab(current->mm);
2345 ctx->sqo_mm = current->mm;
2346
Jens Axboe6c271ce2019-01-10 11:22:30 -07002347 ret = -EINVAL;
2348 if (!cpu_possible(p->sq_thread_cpu))
2349 goto err;
2350
2351 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06002352 ret = -EPERM;
2353 if (!capable(CAP_SYS_ADMIN))
2354 goto err;
2355
Jens Axboe917257d2019-04-13 09:28:55 -06002356 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
2357 if (!ctx->sq_thread_idle)
2358 ctx->sq_thread_idle = HZ;
2359
Jens Axboe6c271ce2019-01-10 11:22:30 -07002360 if (p->flags & IORING_SETUP_SQ_AFF) {
2361 int cpu;
2362
2363 cpu = array_index_nospec(p->sq_thread_cpu, NR_CPUS);
Jens Axboe917257d2019-04-13 09:28:55 -06002364 ret = -EINVAL;
2365 if (!cpu_possible(p->sq_thread_cpu))
2366 goto err;
2367
Jens Axboe6c271ce2019-01-10 11:22:30 -07002368 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
2369 ctx, cpu,
2370 "io_uring-sq");
2371 } else {
2372 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
2373 "io_uring-sq");
2374 }
2375 if (IS_ERR(ctx->sqo_thread)) {
2376 ret = PTR_ERR(ctx->sqo_thread);
2377 ctx->sqo_thread = NULL;
2378 goto err;
2379 }
2380 wake_up_process(ctx->sqo_thread);
2381 } else if (p->flags & IORING_SETUP_SQ_AFF) {
2382 /* Can't have SQ_AFF without SQPOLL */
2383 ret = -EINVAL;
2384 goto err;
2385 }
2386
Jens Axboe2b188cc2019-01-07 10:46:33 -07002387 /* Do QD, or 2 * CPUS, whatever is smallest */
2388 ctx->sqo_wq = alloc_workqueue("io_ring-wq", WQ_UNBOUND | WQ_FREEZABLE,
2389 min(ctx->sq_entries - 1, 2 * num_online_cpus()));
2390 if (!ctx->sqo_wq) {
2391 ret = -ENOMEM;
2392 goto err;
2393 }
2394
2395 return 0;
2396err:
Jens Axboe6c271ce2019-01-10 11:22:30 -07002397 io_sq_thread_stop(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002398 mmdrop(ctx->sqo_mm);
2399 ctx->sqo_mm = NULL;
2400 return ret;
2401}
2402
2403static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
2404{
2405 atomic_long_sub(nr_pages, &user->locked_vm);
2406}
2407
2408static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
2409{
2410 unsigned long page_limit, cur_pages, new_pages;
2411
2412 /* Don't allow more pages than we can safely lock */
2413 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
2414
2415 do {
2416 cur_pages = atomic_long_read(&user->locked_vm);
2417 new_pages = cur_pages + nr_pages;
2418 if (new_pages > page_limit)
2419 return -ENOMEM;
2420 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
2421 new_pages) != cur_pages);
2422
2423 return 0;
2424}
2425
2426static void io_mem_free(void *ptr)
2427{
2428 struct page *page = virt_to_head_page(ptr);
2429
2430 if (put_page_testzero(page))
2431 free_compound_page(page);
2432}
2433
2434static void *io_mem_alloc(size_t size)
2435{
2436 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
2437 __GFP_NORETRY;
2438
2439 return (void *) __get_free_pages(gfp_flags, get_order(size));
2440}
2441
2442static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
2443{
2444 struct io_sq_ring *sq_ring;
2445 struct io_cq_ring *cq_ring;
2446 size_t bytes;
2447
2448 bytes = struct_size(sq_ring, array, sq_entries);
2449 bytes += array_size(sizeof(struct io_uring_sqe), sq_entries);
2450 bytes += struct_size(cq_ring, cqes, cq_entries);
2451
2452 return (bytes + PAGE_SIZE - 1) / PAGE_SIZE;
2453}
2454
Jens Axboeedafcce2019-01-09 09:16:05 -07002455static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
2456{
2457 int i, j;
2458
2459 if (!ctx->user_bufs)
2460 return -ENXIO;
2461
2462 for (i = 0; i < ctx->nr_user_bufs; i++) {
2463 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
2464
2465 for (j = 0; j < imu->nr_bvecs; j++)
2466 put_page(imu->bvec[j].bv_page);
2467
2468 if (ctx->account_mem)
2469 io_unaccount_mem(ctx->user, imu->nr_bvecs);
2470 kfree(imu->bvec);
2471 imu->nr_bvecs = 0;
2472 }
2473
2474 kfree(ctx->user_bufs);
2475 ctx->user_bufs = NULL;
2476 ctx->nr_user_bufs = 0;
2477 return 0;
2478}
2479
2480static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
2481 void __user *arg, unsigned index)
2482{
2483 struct iovec __user *src;
2484
2485#ifdef CONFIG_COMPAT
2486 if (ctx->compat) {
2487 struct compat_iovec __user *ciovs;
2488 struct compat_iovec ciov;
2489
2490 ciovs = (struct compat_iovec __user *) arg;
2491 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
2492 return -EFAULT;
2493
2494 dst->iov_base = (void __user *) (unsigned long) ciov.iov_base;
2495 dst->iov_len = ciov.iov_len;
2496 return 0;
2497 }
2498#endif
2499 src = (struct iovec __user *) arg;
2500 if (copy_from_user(dst, &src[index], sizeof(*dst)))
2501 return -EFAULT;
2502 return 0;
2503}
2504
2505static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
2506 unsigned nr_args)
2507{
2508 struct vm_area_struct **vmas = NULL;
2509 struct page **pages = NULL;
2510 int i, j, got_pages = 0;
2511 int ret = -EINVAL;
2512
2513 if (ctx->user_bufs)
2514 return -EBUSY;
2515 if (!nr_args || nr_args > UIO_MAXIOV)
2516 return -EINVAL;
2517
2518 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
2519 GFP_KERNEL);
2520 if (!ctx->user_bufs)
2521 return -ENOMEM;
2522
2523 for (i = 0; i < nr_args; i++) {
2524 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
2525 unsigned long off, start, end, ubuf;
2526 int pret, nr_pages;
2527 struct iovec iov;
2528 size_t size;
2529
2530 ret = io_copy_iov(ctx, &iov, arg, i);
2531 if (ret)
2532 break;
2533
2534 /*
2535 * Don't impose further limits on the size and buffer
2536 * constraints here, we'll -EINVAL later when IO is
2537 * submitted if they are wrong.
2538 */
2539 ret = -EFAULT;
2540 if (!iov.iov_base || !iov.iov_len)
2541 goto err;
2542
2543 /* arbitrary limit, but we need something */
2544 if (iov.iov_len > SZ_1G)
2545 goto err;
2546
2547 ubuf = (unsigned long) iov.iov_base;
2548 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
2549 start = ubuf >> PAGE_SHIFT;
2550 nr_pages = end - start;
2551
2552 if (ctx->account_mem) {
2553 ret = io_account_mem(ctx->user, nr_pages);
2554 if (ret)
2555 goto err;
2556 }
2557
2558 ret = 0;
2559 if (!pages || nr_pages > got_pages) {
2560 kfree(vmas);
2561 kfree(pages);
2562 pages = kmalloc_array(nr_pages, sizeof(struct page *),
2563 GFP_KERNEL);
2564 vmas = kmalloc_array(nr_pages,
2565 sizeof(struct vm_area_struct *),
2566 GFP_KERNEL);
2567 if (!pages || !vmas) {
2568 ret = -ENOMEM;
2569 if (ctx->account_mem)
2570 io_unaccount_mem(ctx->user, nr_pages);
2571 goto err;
2572 }
2573 got_pages = nr_pages;
2574 }
2575
2576 imu->bvec = kmalloc_array(nr_pages, sizeof(struct bio_vec),
2577 GFP_KERNEL);
2578 ret = -ENOMEM;
2579 if (!imu->bvec) {
2580 if (ctx->account_mem)
2581 io_unaccount_mem(ctx->user, nr_pages);
2582 goto err;
2583 }
2584
2585 ret = 0;
2586 down_read(&current->mm->mmap_sem);
2587 pret = get_user_pages_longterm(ubuf, nr_pages, FOLL_WRITE,
2588 pages, vmas);
2589 if (pret == nr_pages) {
2590 /* don't support file backed memory */
2591 for (j = 0; j < nr_pages; j++) {
2592 struct vm_area_struct *vma = vmas[j];
2593
2594 if (vma->vm_file &&
2595 !is_file_hugepages(vma->vm_file)) {
2596 ret = -EOPNOTSUPP;
2597 break;
2598 }
2599 }
2600 } else {
2601 ret = pret < 0 ? pret : -EFAULT;
2602 }
2603 up_read(&current->mm->mmap_sem);
2604 if (ret) {
2605 /*
2606 * if we did partial map, or found file backed vmas,
2607 * release any pages we did get
2608 */
2609 if (pret > 0) {
2610 for (j = 0; j < pret; j++)
2611 put_page(pages[j]);
2612 }
2613 if (ctx->account_mem)
2614 io_unaccount_mem(ctx->user, nr_pages);
2615 goto err;
2616 }
2617
2618 off = ubuf & ~PAGE_MASK;
2619 size = iov.iov_len;
2620 for (j = 0; j < nr_pages; j++) {
2621 size_t vec_len;
2622
2623 vec_len = min_t(size_t, size, PAGE_SIZE - off);
2624 imu->bvec[j].bv_page = pages[j];
2625 imu->bvec[j].bv_len = vec_len;
2626 imu->bvec[j].bv_offset = off;
2627 off = 0;
2628 size -= vec_len;
2629 }
2630 /* store original address for later verification */
2631 imu->ubuf = ubuf;
2632 imu->len = iov.iov_len;
2633 imu->nr_bvecs = nr_pages;
2634
2635 ctx->nr_user_bufs++;
2636 }
2637 kfree(pages);
2638 kfree(vmas);
2639 return 0;
2640err:
2641 kfree(pages);
2642 kfree(vmas);
2643 io_sqe_buffer_unregister(ctx);
2644 return ret;
2645}
2646
Jens Axboe2b188cc2019-01-07 10:46:33 -07002647static void io_ring_ctx_free(struct io_ring_ctx *ctx)
2648{
Jens Axboe6b063142019-01-10 22:13:58 -07002649 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002650 if (ctx->sqo_mm)
2651 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07002652
2653 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07002654 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07002655 io_sqe_files_unregister(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07002656
Jens Axboe2b188cc2019-01-07 10:46:33 -07002657#if defined(CONFIG_UNIX)
2658 if (ctx->ring_sock)
2659 sock_release(ctx->ring_sock);
2660#endif
2661
2662 io_mem_free(ctx->sq_ring);
2663 io_mem_free(ctx->sq_sqes);
2664 io_mem_free(ctx->cq_ring);
2665
2666 percpu_ref_exit(&ctx->refs);
2667 if (ctx->account_mem)
2668 io_unaccount_mem(ctx->user,
2669 ring_pages(ctx->sq_entries, ctx->cq_entries));
2670 free_uid(ctx->user);
2671 kfree(ctx);
2672}
2673
2674static __poll_t io_uring_poll(struct file *file, poll_table *wait)
2675{
2676 struct io_ring_ctx *ctx = file->private_data;
2677 __poll_t mask = 0;
2678
2679 poll_wait(file, &ctx->cq_wait, wait);
2680 /* See comment at the top of this file */
2681 smp_rmb();
Stefan Bühlerfb775fa2019-04-19 11:57:46 +02002682 if (READ_ONCE(ctx->sq_ring->r.tail) - ctx->cached_sq_head !=
2683 ctx->sq_ring->ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002684 mask |= EPOLLOUT | EPOLLWRNORM;
2685 if (READ_ONCE(ctx->cq_ring->r.head) != ctx->cached_cq_tail)
2686 mask |= EPOLLIN | EPOLLRDNORM;
2687
2688 return mask;
2689}
2690
2691static int io_uring_fasync(int fd, struct file *file, int on)
2692{
2693 struct io_ring_ctx *ctx = file->private_data;
2694
2695 return fasync_helper(fd, file, on, &ctx->cq_fasync);
2696}
2697
2698static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
2699{
2700 mutex_lock(&ctx->uring_lock);
2701 percpu_ref_kill(&ctx->refs);
2702 mutex_unlock(&ctx->uring_lock);
2703
Jens Axboe221c5eb2019-01-17 09:41:58 -07002704 io_poll_remove_all(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07002705 io_iopoll_reap_events(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002706 wait_for_completion(&ctx->ctx_done);
2707 io_ring_ctx_free(ctx);
2708}
2709
2710static int io_uring_release(struct inode *inode, struct file *file)
2711{
2712 struct io_ring_ctx *ctx = file->private_data;
2713
2714 file->private_data = NULL;
2715 io_ring_ctx_wait_and_kill(ctx);
2716 return 0;
2717}
2718
2719static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
2720{
2721 loff_t offset = (loff_t) vma->vm_pgoff << PAGE_SHIFT;
2722 unsigned long sz = vma->vm_end - vma->vm_start;
2723 struct io_ring_ctx *ctx = file->private_data;
2724 unsigned long pfn;
2725 struct page *page;
2726 void *ptr;
2727
2728 switch (offset) {
2729 case IORING_OFF_SQ_RING:
2730 ptr = ctx->sq_ring;
2731 break;
2732 case IORING_OFF_SQES:
2733 ptr = ctx->sq_sqes;
2734 break;
2735 case IORING_OFF_CQ_RING:
2736 ptr = ctx->cq_ring;
2737 break;
2738 default:
2739 return -EINVAL;
2740 }
2741
2742 page = virt_to_head_page(ptr);
2743 if (sz > (PAGE_SIZE << compound_order(page)))
2744 return -EINVAL;
2745
2746 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
2747 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
2748}
2749
2750SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
2751 u32, min_complete, u32, flags, const sigset_t __user *, sig,
2752 size_t, sigsz)
2753{
2754 struct io_ring_ctx *ctx;
2755 long ret = -EBADF;
2756 int submitted = 0;
2757 struct fd f;
2758
Jens Axboe6c271ce2019-01-10 11:22:30 -07002759 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002760 return -EINVAL;
2761
2762 f = fdget(fd);
2763 if (!f.file)
2764 return -EBADF;
2765
2766 ret = -EOPNOTSUPP;
2767 if (f.file->f_op != &io_uring_fops)
2768 goto out_fput;
2769
2770 ret = -ENXIO;
2771 ctx = f.file->private_data;
2772 if (!percpu_ref_tryget(&ctx->refs))
2773 goto out_fput;
2774
Jens Axboe6c271ce2019-01-10 11:22:30 -07002775 /*
2776 * For SQ polling, the thread will do all submissions and completions.
2777 * Just return the requested submit count, and wake the thread if
2778 * we were asked to.
2779 */
2780 if (ctx->flags & IORING_SETUP_SQPOLL) {
2781 if (flags & IORING_ENTER_SQ_WAKEUP)
2782 wake_up(&ctx->sqo_wait);
2783 submitted = to_submit;
2784 goto out_ctx;
2785 }
2786
Jens Axboe2b188cc2019-01-07 10:46:33 -07002787 ret = 0;
2788 if (to_submit) {
2789 to_submit = min(to_submit, ctx->sq_entries);
2790
2791 mutex_lock(&ctx->uring_lock);
2792 submitted = io_ring_submit(ctx, to_submit);
2793 mutex_unlock(&ctx->uring_lock);
2794
2795 if (submitted < 0)
2796 goto out_ctx;
2797 }
2798 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07002799 unsigned nr_events = 0;
2800
Jens Axboe2b188cc2019-01-07 10:46:33 -07002801 min_complete = min(min_complete, ctx->cq_entries);
2802
2803 /*
2804 * The application could have included the 'to_submit' count
2805 * in how many events it wanted to wait for. If we failed to
2806 * submit the desired count, we may need to adjust the number
2807 * of events to poll/wait for.
2808 */
2809 if (submitted < to_submit)
2810 min_complete = min_t(unsigned, submitted, min_complete);
2811
Jens Axboedef596e2019-01-09 08:59:42 -07002812 if (ctx->flags & IORING_SETUP_IOPOLL) {
2813 mutex_lock(&ctx->uring_lock);
2814 ret = io_iopoll_check(ctx, &nr_events, min_complete);
2815 mutex_unlock(&ctx->uring_lock);
2816 } else {
2817 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
2818 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002819 }
2820
2821out_ctx:
2822 io_ring_drop_ctx_refs(ctx, 1);
2823out_fput:
2824 fdput(f);
2825 return submitted ? submitted : ret;
2826}
2827
2828static const struct file_operations io_uring_fops = {
2829 .release = io_uring_release,
2830 .mmap = io_uring_mmap,
2831 .poll = io_uring_poll,
2832 .fasync = io_uring_fasync,
2833};
2834
2835static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
2836 struct io_uring_params *p)
2837{
2838 struct io_sq_ring *sq_ring;
2839 struct io_cq_ring *cq_ring;
2840 size_t size;
2841
2842 sq_ring = io_mem_alloc(struct_size(sq_ring, array, p->sq_entries));
2843 if (!sq_ring)
2844 return -ENOMEM;
2845
2846 ctx->sq_ring = sq_ring;
2847 sq_ring->ring_mask = p->sq_entries - 1;
2848 sq_ring->ring_entries = p->sq_entries;
2849 ctx->sq_mask = sq_ring->ring_mask;
2850 ctx->sq_entries = sq_ring->ring_entries;
2851
2852 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
2853 if (size == SIZE_MAX)
2854 return -EOVERFLOW;
2855
2856 ctx->sq_sqes = io_mem_alloc(size);
2857 if (!ctx->sq_sqes) {
2858 io_mem_free(ctx->sq_ring);
2859 return -ENOMEM;
2860 }
2861
2862 cq_ring = io_mem_alloc(struct_size(cq_ring, cqes, p->cq_entries));
2863 if (!cq_ring) {
2864 io_mem_free(ctx->sq_ring);
2865 io_mem_free(ctx->sq_sqes);
2866 return -ENOMEM;
2867 }
2868
2869 ctx->cq_ring = cq_ring;
2870 cq_ring->ring_mask = p->cq_entries - 1;
2871 cq_ring->ring_entries = p->cq_entries;
2872 ctx->cq_mask = cq_ring->ring_mask;
2873 ctx->cq_entries = cq_ring->ring_entries;
2874 return 0;
2875}
2876
2877/*
2878 * Allocate an anonymous fd, this is what constitutes the application
2879 * visible backing of an io_uring instance. The application mmaps this
2880 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
2881 * we have to tie this fd to a socket for file garbage collection purposes.
2882 */
2883static int io_uring_get_fd(struct io_ring_ctx *ctx)
2884{
2885 struct file *file;
2886 int ret;
2887
2888#if defined(CONFIG_UNIX)
2889 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
2890 &ctx->ring_sock);
2891 if (ret)
2892 return ret;
2893#endif
2894
2895 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
2896 if (ret < 0)
2897 goto err;
2898
2899 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
2900 O_RDWR | O_CLOEXEC);
2901 if (IS_ERR(file)) {
2902 put_unused_fd(ret);
2903 ret = PTR_ERR(file);
2904 goto err;
2905 }
2906
2907#if defined(CONFIG_UNIX)
2908 ctx->ring_sock->file = file;
Jens Axboe6b063142019-01-10 22:13:58 -07002909 ctx->ring_sock->sk->sk_user_data = ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002910#endif
2911 fd_install(ret, file);
2912 return ret;
2913err:
2914#if defined(CONFIG_UNIX)
2915 sock_release(ctx->ring_sock);
2916 ctx->ring_sock = NULL;
2917#endif
2918 return ret;
2919}
2920
2921static int io_uring_create(unsigned entries, struct io_uring_params *p)
2922{
2923 struct user_struct *user = NULL;
2924 struct io_ring_ctx *ctx;
2925 bool account_mem;
2926 int ret;
2927
2928 if (!entries || entries > IORING_MAX_ENTRIES)
2929 return -EINVAL;
2930
2931 /*
2932 * Use twice as many entries for the CQ ring. It's possible for the
2933 * application to drive a higher depth than the size of the SQ ring,
2934 * since the sqes are only used at submission time. This allows for
2935 * some flexibility in overcommitting a bit.
2936 */
2937 p->sq_entries = roundup_pow_of_two(entries);
2938 p->cq_entries = 2 * p->sq_entries;
2939
2940 user = get_uid(current_user());
2941 account_mem = !capable(CAP_IPC_LOCK);
2942
2943 if (account_mem) {
2944 ret = io_account_mem(user,
2945 ring_pages(p->sq_entries, p->cq_entries));
2946 if (ret) {
2947 free_uid(user);
2948 return ret;
2949 }
2950 }
2951
2952 ctx = io_ring_ctx_alloc(p);
2953 if (!ctx) {
2954 if (account_mem)
2955 io_unaccount_mem(user, ring_pages(p->sq_entries,
2956 p->cq_entries));
2957 free_uid(user);
2958 return -ENOMEM;
2959 }
2960 ctx->compat = in_compat_syscall();
2961 ctx->account_mem = account_mem;
2962 ctx->user = user;
2963
2964 ret = io_allocate_scq_urings(ctx, p);
2965 if (ret)
2966 goto err;
2967
Jens Axboe6c271ce2019-01-10 11:22:30 -07002968 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002969 if (ret)
2970 goto err;
2971
2972 ret = io_uring_get_fd(ctx);
2973 if (ret < 0)
2974 goto err;
2975
2976 memset(&p->sq_off, 0, sizeof(p->sq_off));
2977 p->sq_off.head = offsetof(struct io_sq_ring, r.head);
2978 p->sq_off.tail = offsetof(struct io_sq_ring, r.tail);
2979 p->sq_off.ring_mask = offsetof(struct io_sq_ring, ring_mask);
2980 p->sq_off.ring_entries = offsetof(struct io_sq_ring, ring_entries);
2981 p->sq_off.flags = offsetof(struct io_sq_ring, flags);
2982 p->sq_off.dropped = offsetof(struct io_sq_ring, dropped);
2983 p->sq_off.array = offsetof(struct io_sq_ring, array);
2984
2985 memset(&p->cq_off, 0, sizeof(p->cq_off));
2986 p->cq_off.head = offsetof(struct io_cq_ring, r.head);
2987 p->cq_off.tail = offsetof(struct io_cq_ring, r.tail);
2988 p->cq_off.ring_mask = offsetof(struct io_cq_ring, ring_mask);
2989 p->cq_off.ring_entries = offsetof(struct io_cq_ring, ring_entries);
2990 p->cq_off.overflow = offsetof(struct io_cq_ring, overflow);
2991 p->cq_off.cqes = offsetof(struct io_cq_ring, cqes);
2992 return ret;
2993err:
2994 io_ring_ctx_wait_and_kill(ctx);
2995 return ret;
2996}
2997
2998/*
2999 * Sets up an aio uring context, and returns the fd. Applications asks for a
3000 * ring size, we return the actual sq/cq ring sizes (among other things) in the
3001 * params structure passed in.
3002 */
3003static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
3004{
3005 struct io_uring_params p;
3006 long ret;
3007 int i;
3008
3009 if (copy_from_user(&p, params, sizeof(p)))
3010 return -EFAULT;
3011 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
3012 if (p.resv[i])
3013 return -EINVAL;
3014 }
3015
Jens Axboe6c271ce2019-01-10 11:22:30 -07003016 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
3017 IORING_SETUP_SQ_AFF))
Jens Axboe2b188cc2019-01-07 10:46:33 -07003018 return -EINVAL;
3019
3020 ret = io_uring_create(entries, &p);
3021 if (ret < 0)
3022 return ret;
3023
3024 if (copy_to_user(params, &p, sizeof(p)))
3025 return -EFAULT;
3026
3027 return ret;
3028}
3029
3030SYSCALL_DEFINE2(io_uring_setup, u32, entries,
3031 struct io_uring_params __user *, params)
3032{
3033 return io_uring_setup(entries, params);
3034}
3035
Jens Axboeedafcce2019-01-09 09:16:05 -07003036static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
3037 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06003038 __releases(ctx->uring_lock)
3039 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07003040{
3041 int ret;
3042
Jens Axboe35fa71a2019-04-22 10:23:23 -06003043 /*
3044 * We're inside the ring mutex, if the ref is already dying, then
3045 * someone else killed the ctx or is already going through
3046 * io_uring_register().
3047 */
3048 if (percpu_ref_is_dying(&ctx->refs))
3049 return -ENXIO;
3050
Jens Axboeedafcce2019-01-09 09:16:05 -07003051 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06003052
3053 /*
3054 * Drop uring mutex before waiting for references to exit. If another
3055 * thread is currently inside io_uring_enter() it might need to grab
3056 * the uring_lock to make progress. If we hold it here across the drain
3057 * wait, then we can deadlock. It's safe to drop the mutex here, since
3058 * no new references will come in after we've killed the percpu ref.
3059 */
3060 mutex_unlock(&ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07003061 wait_for_completion(&ctx->ctx_done);
Jens Axboeb19062a2019-04-15 10:49:38 -06003062 mutex_lock(&ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07003063
3064 switch (opcode) {
3065 case IORING_REGISTER_BUFFERS:
3066 ret = io_sqe_buffer_register(ctx, arg, nr_args);
3067 break;
3068 case IORING_UNREGISTER_BUFFERS:
3069 ret = -EINVAL;
3070 if (arg || nr_args)
3071 break;
3072 ret = io_sqe_buffer_unregister(ctx);
3073 break;
Jens Axboe6b063142019-01-10 22:13:58 -07003074 case IORING_REGISTER_FILES:
3075 ret = io_sqe_files_register(ctx, arg, nr_args);
3076 break;
3077 case IORING_UNREGISTER_FILES:
3078 ret = -EINVAL;
3079 if (arg || nr_args)
3080 break;
3081 ret = io_sqe_files_unregister(ctx);
3082 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07003083 default:
3084 ret = -EINVAL;
3085 break;
3086 }
3087
3088 /* bring the ctx back to life */
3089 reinit_completion(&ctx->ctx_done);
3090 percpu_ref_reinit(&ctx->refs);
3091 return ret;
3092}
3093
3094SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
3095 void __user *, arg, unsigned int, nr_args)
3096{
3097 struct io_ring_ctx *ctx;
3098 long ret = -EBADF;
3099 struct fd f;
3100
3101 f = fdget(fd);
3102 if (!f.file)
3103 return -EBADF;
3104
3105 ret = -EOPNOTSUPP;
3106 if (f.file->f_op != &io_uring_fops)
3107 goto out_fput;
3108
3109 ctx = f.file->private_data;
3110
3111 mutex_lock(&ctx->uring_lock);
3112 ret = __io_uring_register(ctx, opcode, arg, nr_args);
3113 mutex_unlock(&ctx->uring_lock);
3114out_fput:
3115 fdput(f);
3116 return ret;
3117}
3118
Jens Axboe2b188cc2019-01-07 10:46:33 -07003119static int __init io_uring_init(void)
3120{
3121 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
3122 return 0;
3123};
3124__initcall(io_uring_init);