blob: 083c5dd95452086cb6ada5bcc9ec9f4b83db1e1c [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
Roman Gushchin21482892019-05-07 10:01:48 -0700392 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
393 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700394 kfree(ctx);
395 return NULL;
396 }
397
398 ctx->flags = p->flags;
399 init_waitqueue_head(&ctx->cq_wait);
400 init_completion(&ctx->ctx_done);
401 mutex_init(&ctx->uring_lock);
402 init_waitqueue_head(&ctx->wait);
Jens Axboe31b51512019-01-18 22:56:34 -0700403 for (i = 0; i < ARRAY_SIZE(ctx->pending_async); i++) {
404 spin_lock_init(&ctx->pending_async[i].lock);
405 INIT_LIST_HEAD(&ctx->pending_async[i].list);
406 atomic_set(&ctx->pending_async[i].cnt, 0);
407 }
Jens Axboe2b188cc2019-01-07 10:46:33 -0700408 spin_lock_init(&ctx->completion_lock);
Jens Axboedef596e2019-01-09 08:59:42 -0700409 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboe221c5eb2019-01-17 09:41:58 -0700410 INIT_LIST_HEAD(&ctx->cancel_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700411 return ctx;
412}
413
414static void io_commit_cqring(struct io_ring_ctx *ctx)
415{
416 struct io_cq_ring *ring = ctx->cq_ring;
417
418 if (ctx->cached_cq_tail != READ_ONCE(ring->r.tail)) {
419 /* order cqe stores with ring update */
420 smp_store_release(&ring->r.tail, ctx->cached_cq_tail);
421
Jens Axboe2b188cc2019-01-07 10:46:33 -0700422 if (wq_has_sleeper(&ctx->cq_wait)) {
423 wake_up_interruptible(&ctx->cq_wait);
424 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
425 }
426 }
427}
428
429static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
430{
431 struct io_cq_ring *ring = ctx->cq_ring;
432 unsigned tail;
433
434 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +0200435 /*
436 * writes to the cq entry need to come after reading head; the
437 * control dependency is enough as we're using WRITE_ONCE to
438 * fill the cq entry
439 */
Jens Axboe74f464e2019-04-17 08:57:48 -0600440 if (tail - READ_ONCE(ring->r.head) == ring->ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700441 return NULL;
442
443 ctx->cached_cq_tail++;
444 return &ring->cqes[tail & ctx->cq_mask];
445}
446
447static void io_cqring_fill_event(struct io_ring_ctx *ctx, u64 ki_user_data,
448 long res, unsigned ev_flags)
449{
450 struct io_uring_cqe *cqe;
451
452 /*
453 * If we can't get a cq entry, userspace overflowed the
454 * submission (by quite a lot). Increment the overflow count in
455 * the ring.
456 */
457 cqe = io_get_cqring(ctx);
458 if (cqe) {
459 WRITE_ONCE(cqe->user_data, ki_user_data);
460 WRITE_ONCE(cqe->res, res);
461 WRITE_ONCE(cqe->flags, ev_flags);
462 } else {
463 unsigned overflow = READ_ONCE(ctx->cq_ring->overflow);
464
465 WRITE_ONCE(ctx->cq_ring->overflow, overflow + 1);
466 }
467}
468
Jens Axboe8c838782019-03-12 15:48:16 -0600469static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
470{
471 if (waitqueue_active(&ctx->wait))
472 wake_up(&ctx->wait);
473 if (waitqueue_active(&ctx->sqo_wait))
474 wake_up(&ctx->sqo_wait);
475}
476
477static void io_cqring_add_event(struct io_ring_ctx *ctx, u64 user_data,
Jens Axboe2b188cc2019-01-07 10:46:33 -0700478 long res, unsigned ev_flags)
479{
480 unsigned long flags;
481
482 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe8c838782019-03-12 15:48:16 -0600483 io_cqring_fill_event(ctx, user_data, res, ev_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700484 io_commit_cqring(ctx);
485 spin_unlock_irqrestore(&ctx->completion_lock, flags);
486
Jens Axboe8c838782019-03-12 15:48:16 -0600487 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700488}
489
490static void io_ring_drop_ctx_refs(struct io_ring_ctx *ctx, unsigned refs)
491{
492 percpu_ref_put_many(&ctx->refs, refs);
493
494 if (waitqueue_active(&ctx->wait))
495 wake_up(&ctx->wait);
496}
497
Jens Axboe2579f912019-01-09 09:10:43 -0700498static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
499 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700500{
Jens Axboefd6fab22019-03-14 16:30:06 -0600501 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700502 struct io_kiocb *req;
503
504 if (!percpu_ref_tryget(&ctx->refs))
505 return NULL;
506
Jens Axboe2579f912019-01-09 09:10:43 -0700507 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -0600508 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -0700509 if (unlikely(!req))
510 goto out;
511 } else if (!state->free_reqs) {
512 size_t sz;
513 int ret;
514
515 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -0600516 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
517
518 /*
519 * Bulk alloc is all-or-nothing. If we fail to get a batch,
520 * retry single alloc to be on the safe side.
521 */
522 if (unlikely(ret <= 0)) {
523 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
524 if (!state->reqs[0])
525 goto out;
526 ret = 1;
527 }
Jens Axboe2579f912019-01-09 09:10:43 -0700528 state->free_reqs = ret - 1;
529 state->cur_req = 1;
530 req = state->reqs[0];
531 } else {
532 req = state->reqs[state->cur_req];
533 state->free_reqs--;
534 state->cur_req++;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700535 }
536
Jens Axboe2579f912019-01-09 09:10:43 -0700537 req->ctx = ctx;
538 req->flags = 0;
Jens Axboee65ef562019-03-12 10:16:44 -0600539 /* one is dropped after submission, the other at completion */
540 refcount_set(&req->refs, 2);
Jens Axboe2579f912019-01-09 09:10:43 -0700541 return req;
542out:
Jens Axboe2b188cc2019-01-07 10:46:33 -0700543 io_ring_drop_ctx_refs(ctx, 1);
544 return NULL;
545}
546
Jens Axboedef596e2019-01-09 08:59:42 -0700547static void io_free_req_many(struct io_ring_ctx *ctx, void **reqs, int *nr)
548{
549 if (*nr) {
550 kmem_cache_free_bulk(req_cachep, *nr, reqs);
551 io_ring_drop_ctx_refs(ctx, *nr);
552 *nr = 0;
553 }
554}
555
Jens Axboe2b188cc2019-01-07 10:46:33 -0700556static void io_free_req(struct io_kiocb *req)
557{
Jens Axboe09bb8392019-03-13 12:39:28 -0600558 if (req->file && !(req->flags & REQ_F_FIXED_FILE))
559 fput(req->file);
Jens Axboee65ef562019-03-12 10:16:44 -0600560 io_ring_drop_ctx_refs(req->ctx, 1);
561 kmem_cache_free(req_cachep, req);
562}
563
564static void io_put_req(struct io_kiocb *req)
565{
566 if (refcount_dec_and_test(&req->refs))
567 io_free_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700568}
569
Jens Axboedef596e2019-01-09 08:59:42 -0700570/*
571 * Find and free completed poll iocbs
572 */
573static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
574 struct list_head *done)
575{
576 void *reqs[IO_IOPOLL_BATCH];
577 struct io_kiocb *req;
Jens Axboe09bb8392019-03-13 12:39:28 -0600578 int to_free;
Jens Axboedef596e2019-01-09 08:59:42 -0700579
Jens Axboe09bb8392019-03-13 12:39:28 -0600580 to_free = 0;
Jens Axboedef596e2019-01-09 08:59:42 -0700581 while (!list_empty(done)) {
582 req = list_first_entry(done, struct io_kiocb, list);
583 list_del(&req->list);
584
585 io_cqring_fill_event(ctx, req->user_data, req->error, 0);
Jens Axboedef596e2019-01-09 08:59:42 -0700586 (*nr_events)++;
587
Jens Axboe09bb8392019-03-13 12:39:28 -0600588 if (refcount_dec_and_test(&req->refs)) {
589 /* If we're not using fixed files, we have to pair the
590 * completion part with the file put. Use regular
591 * completions for those, only batch free for fixed
592 * file.
593 */
594 if (req->flags & REQ_F_FIXED_FILE) {
595 reqs[to_free++] = req;
596 if (to_free == ARRAY_SIZE(reqs))
597 io_free_req_many(ctx, reqs, &to_free);
Jens Axboe6b063142019-01-10 22:13:58 -0700598 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -0600599 io_free_req(req);
Jens Axboe6b063142019-01-10 22:13:58 -0700600 }
Jens Axboe9a56a232019-01-09 09:06:50 -0700601 }
Jens Axboedef596e2019-01-09 08:59:42 -0700602 }
Jens Axboedef596e2019-01-09 08:59:42 -0700603
Jens Axboe09bb8392019-03-13 12:39:28 -0600604 io_commit_cqring(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -0700605 io_free_req_many(ctx, reqs, &to_free);
606}
607
608static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
609 long min)
610{
611 struct io_kiocb *req, *tmp;
612 LIST_HEAD(done);
613 bool spin;
614 int ret;
615
616 /*
617 * Only spin for completions if we don't have multiple devices hanging
618 * off our complete list, and we're under the requested amount.
619 */
620 spin = !ctx->poll_multi_file && *nr_events < min;
621
622 ret = 0;
623 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
624 struct kiocb *kiocb = &req->rw;
625
626 /*
627 * Move completed entries to our local list. If we find a
628 * request that requires polling, break out and complete
629 * the done list first, if we have entries there.
630 */
631 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
632 list_move_tail(&req->list, &done);
633 continue;
634 }
635 if (!list_empty(&done))
636 break;
637
638 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
639 if (ret < 0)
640 break;
641
642 if (ret && spin)
643 spin = false;
644 ret = 0;
645 }
646
647 if (!list_empty(&done))
648 io_iopoll_complete(ctx, nr_events, &done);
649
650 return ret;
651}
652
653/*
654 * Poll for a mininum of 'min' events. Note that if min == 0 we consider that a
655 * non-spinning poll check - we'll still enter the driver poll loop, but only
656 * as a non-spinning completion check.
657 */
658static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
659 long min)
660{
661 while (!list_empty(&ctx->poll_list)) {
662 int ret;
663
664 ret = io_do_iopoll(ctx, nr_events, min);
665 if (ret < 0)
666 return ret;
667 if (!min || *nr_events >= min)
668 return 0;
669 }
670
671 return 1;
672}
673
674/*
675 * We can't just wait for polled events to come to us, we have to actively
676 * find and complete them.
677 */
678static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
679{
680 if (!(ctx->flags & IORING_SETUP_IOPOLL))
681 return;
682
683 mutex_lock(&ctx->uring_lock);
684 while (!list_empty(&ctx->poll_list)) {
685 unsigned int nr_events = 0;
686
687 io_iopoll_getevents(ctx, &nr_events, 1);
688 }
689 mutex_unlock(&ctx->uring_lock);
690}
691
692static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
693 long min)
694{
695 int ret = 0;
696
697 do {
698 int tmin = 0;
699
700 if (*nr_events < min)
701 tmin = min - *nr_events;
702
703 ret = io_iopoll_getevents(ctx, nr_events, tmin);
704 if (ret <= 0)
705 break;
706 ret = 0;
707 } while (min && !*nr_events && !need_resched());
708
709 return ret;
710}
711
Jens Axboe2b188cc2019-01-07 10:46:33 -0700712static void kiocb_end_write(struct kiocb *kiocb)
713{
714 if (kiocb->ki_flags & IOCB_WRITE) {
715 struct inode *inode = file_inode(kiocb->ki_filp);
716
717 /*
718 * Tell lockdep we inherited freeze protection from submission
719 * thread.
720 */
721 if (S_ISREG(inode->i_mode))
722 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
723 file_end_write(kiocb->ki_filp);
724 }
725}
726
727static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
728{
729 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
730
731 kiocb_end_write(kiocb);
732
Jens Axboe2b188cc2019-01-07 10:46:33 -0700733 io_cqring_add_event(req->ctx, req->user_data, res, 0);
Jens Axboee65ef562019-03-12 10:16:44 -0600734 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700735}
736
Jens Axboedef596e2019-01-09 08:59:42 -0700737static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
738{
739 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
740
741 kiocb_end_write(kiocb);
742
743 req->error = res;
744 if (res != -EAGAIN)
745 req->flags |= REQ_F_IOPOLL_COMPLETED;
746}
747
748/*
749 * After the iocb has been issued, it's safe to be found on the poll list.
750 * Adding the kiocb to the list AFTER submission ensures that we don't
751 * find it from a io_iopoll_getevents() thread before the issuer is done
752 * accessing the kiocb cookie.
753 */
754static void io_iopoll_req_issued(struct io_kiocb *req)
755{
756 struct io_ring_ctx *ctx = req->ctx;
757
758 /*
759 * Track whether we have multiple files in our lists. This will impact
760 * how we do polling eventually, not spinning if we're on potentially
761 * different devices.
762 */
763 if (list_empty(&ctx->poll_list)) {
764 ctx->poll_multi_file = false;
765 } else if (!ctx->poll_multi_file) {
766 struct io_kiocb *list_req;
767
768 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
769 list);
770 if (list_req->rw.ki_filp != req->rw.ki_filp)
771 ctx->poll_multi_file = true;
772 }
773
774 /*
775 * For fast devices, IO may have already completed. If it has, add
776 * it to the front so we find it first.
777 */
778 if (req->flags & REQ_F_IOPOLL_COMPLETED)
779 list_add(&req->list, &ctx->poll_list);
780 else
781 list_add_tail(&req->list, &ctx->poll_list);
782}
783
Jens Axboe3d6770f2019-04-13 11:50:54 -0600784static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -0700785{
Jens Axboe3d6770f2019-04-13 11:50:54 -0600786 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -0700787 int diff = state->has_refs - state->used_refs;
788
789 if (diff)
790 fput_many(state->file, diff);
791 state->file = NULL;
792 }
793}
794
795/*
796 * Get as many references to a file as we have IOs left in this submission,
797 * assuming most submissions are for one file, or at least that each file
798 * has more than one submission.
799 */
800static struct file *io_file_get(struct io_submit_state *state, int fd)
801{
802 if (!state)
803 return fget(fd);
804
805 if (state->file) {
806 if (state->fd == fd) {
807 state->used_refs++;
808 state->ios_left--;
809 return state->file;
810 }
Jens Axboe3d6770f2019-04-13 11:50:54 -0600811 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -0700812 }
813 state->file = fget_many(fd, state->ios_left);
814 if (!state->file)
815 return NULL;
816
817 state->fd = fd;
818 state->has_refs = state->ios_left;
819 state->used_refs = 1;
820 state->ios_left--;
821 return state->file;
822}
823
Jens Axboe2b188cc2019-01-07 10:46:33 -0700824/*
825 * If we tracked the file through the SCM inflight mechanism, we could support
826 * any file. For now, just ensure that anything potentially problematic is done
827 * inline.
828 */
829static bool io_file_supports_async(struct file *file)
830{
831 umode_t mode = file_inode(file)->i_mode;
832
833 if (S_ISBLK(mode) || S_ISCHR(mode))
834 return true;
835 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
836 return true;
837
838 return false;
839}
840
Jens Axboe6c271ce2019-01-10 11:22:30 -0700841static int io_prep_rw(struct io_kiocb *req, const struct sqe_submit *s,
Jens Axboe8358e3a2019-04-23 08:17:58 -0600842 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700843{
Jens Axboe6c271ce2019-01-10 11:22:30 -0700844 const struct io_uring_sqe *sqe = s->sqe;
Jens Axboedef596e2019-01-09 08:59:42 -0700845 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700846 struct kiocb *kiocb = &req->rw;
Jens Axboe09bb8392019-03-13 12:39:28 -0600847 unsigned ioprio;
848 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700849
Jens Axboe09bb8392019-03-13 12:39:28 -0600850 if (!req->file)
851 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700852 /* For -EAGAIN retry, everything is already prepped */
Jens Axboed530a402019-03-13 12:15:01 -0600853 if (req->flags & REQ_F_PREPPED)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700854 return 0;
855
Jens Axboe09bb8392019-03-13 12:39:28 -0600856 if (force_nonblock && !io_file_supports_async(req->file))
857 force_nonblock = false;
Jens Axboe6b063142019-01-10 22:13:58 -0700858
Jens Axboe2b188cc2019-01-07 10:46:33 -0700859 kiocb->ki_pos = READ_ONCE(sqe->off);
860 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
861 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
862
863 ioprio = READ_ONCE(sqe->ioprio);
864 if (ioprio) {
865 ret = ioprio_check_cap(ioprio);
866 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -0600867 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700868
869 kiocb->ki_ioprio = ioprio;
870 } else
871 kiocb->ki_ioprio = get_current_ioprio();
872
873 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
874 if (unlikely(ret))
Jens Axboe09bb8392019-03-13 12:39:28 -0600875 return ret;
Stefan Bühler8449eed2019-04-27 20:34:19 +0200876
877 /* don't allow async punt if RWF_NOWAIT was requested */
878 if (kiocb->ki_flags & IOCB_NOWAIT)
879 req->flags |= REQ_F_NOWAIT;
880
881 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700882 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +0200883
Jens Axboedef596e2019-01-09 08:59:42 -0700884 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -0700885 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
886 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -0600887 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700888
Jens Axboedef596e2019-01-09 08:59:42 -0700889 req->error = 0;
890 kiocb->ki_flags |= IOCB_HIPRI;
891 kiocb->ki_complete = io_complete_rw_iopoll;
892 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -0600893 if (kiocb->ki_flags & IOCB_HIPRI)
894 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -0700895 kiocb->ki_complete = io_complete_rw;
896 }
Jens Axboed530a402019-03-13 12:15:01 -0600897 req->flags |= REQ_F_PREPPED;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700898 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700899}
900
901static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
902{
903 switch (ret) {
904 case -EIOCBQUEUED:
905 break;
906 case -ERESTARTSYS:
907 case -ERESTARTNOINTR:
908 case -ERESTARTNOHAND:
909 case -ERESTART_RESTARTBLOCK:
910 /*
911 * We can't just restart the syscall, since previously
912 * submitted sqes may already be in progress. Just fail this
913 * IO with EINTR.
914 */
915 ret = -EINTR;
916 /* fall through */
917 default:
918 kiocb->ki_complete(kiocb, ret, 0);
919 }
920}
921
Jens Axboeedafcce2019-01-09 09:16:05 -0700922static int io_import_fixed(struct io_ring_ctx *ctx, int rw,
923 const struct io_uring_sqe *sqe,
924 struct iov_iter *iter)
925{
926 size_t len = READ_ONCE(sqe->len);
927 struct io_mapped_ubuf *imu;
928 unsigned index, buf_index;
929 size_t offset;
930 u64 buf_addr;
931
932 /* attempt to use fixed buffers without having provided iovecs */
933 if (unlikely(!ctx->user_bufs))
934 return -EFAULT;
935
936 buf_index = READ_ONCE(sqe->buf_index);
937 if (unlikely(buf_index >= ctx->nr_user_bufs))
938 return -EFAULT;
939
940 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
941 imu = &ctx->user_bufs[index];
942 buf_addr = READ_ONCE(sqe->addr);
943
944 /* overflow */
945 if (buf_addr + len < buf_addr)
946 return -EFAULT;
947 /* not inside the mapped region */
948 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
949 return -EFAULT;
950
951 /*
952 * May not be a start of buffer, set size appropriately
953 * and advance us to the beginning.
954 */
955 offset = buf_addr - imu->ubuf;
956 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
957 if (offset)
958 iov_iter_advance(iter, offset);
Jens Axboe875f1d02019-02-27 13:05:25 -0700959
960 /* don't drop a reference to these pages */
961 iter->type |= ITER_BVEC_FLAG_NO_REF;
Jens Axboeedafcce2019-01-09 09:16:05 -0700962 return 0;
963}
964
Jens Axboe2b188cc2019-01-07 10:46:33 -0700965static int io_import_iovec(struct io_ring_ctx *ctx, int rw,
966 const struct sqe_submit *s, struct iovec **iovec,
967 struct iov_iter *iter)
968{
969 const struct io_uring_sqe *sqe = s->sqe;
970 void __user *buf = u64_to_user_ptr(READ_ONCE(sqe->addr));
971 size_t sqe_len = READ_ONCE(sqe->len);
Jens Axboeedafcce2019-01-09 09:16:05 -0700972 u8 opcode;
973
974 /*
975 * We're reading ->opcode for the second time, but the first read
976 * doesn't care whether it's _FIXED or not, so it doesn't matter
977 * whether ->opcode changes concurrently. The first read does care
978 * about whether it is a READ or a WRITE, so we don't trust this read
979 * for that purpose and instead let the caller pass in the read/write
980 * flag.
981 */
982 opcode = READ_ONCE(sqe->opcode);
983 if (opcode == IORING_OP_READ_FIXED ||
984 opcode == IORING_OP_WRITE_FIXED) {
Jens Axboee0c5c572019-03-12 10:18:47 -0600985 int ret = io_import_fixed(ctx, rw, sqe, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -0700986 *iovec = NULL;
987 return ret;
988 }
Jens Axboe2b188cc2019-01-07 10:46:33 -0700989
990 if (!s->has_user)
991 return -EFAULT;
992
993#ifdef CONFIG_COMPAT
994 if (ctx->compat)
995 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
996 iovec, iter);
997#endif
998
999 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
1000}
1001
Jens Axboe31b51512019-01-18 22:56:34 -07001002/*
1003 * Make a note of the last file/offset/direction we punted to async
1004 * context. We'll use this information to see if we can piggy back a
1005 * sequential request onto the previous one, if it's still hasn't been
1006 * completed by the async worker.
1007 */
1008static void io_async_list_note(int rw, struct io_kiocb *req, size_t len)
1009{
1010 struct async_list *async_list = &req->ctx->pending_async[rw];
1011 struct kiocb *kiocb = &req->rw;
1012 struct file *filp = kiocb->ki_filp;
1013 off_t io_end = kiocb->ki_pos + len;
1014
1015 if (filp == async_list->file && kiocb->ki_pos == async_list->io_end) {
1016 unsigned long max_pages;
1017
1018 /* Use 8x RA size as a decent limiter for both reads/writes */
1019 max_pages = filp->f_ra.ra_pages;
1020 if (!max_pages)
Nikolay Borisovb5420232019-03-11 23:28:13 -07001021 max_pages = VM_READAHEAD_PAGES;
Jens Axboe31b51512019-01-18 22:56:34 -07001022 max_pages *= 8;
1023
1024 /* If max pages are exceeded, reset the state */
1025 len >>= PAGE_SHIFT;
1026 if (async_list->io_pages + len <= max_pages) {
1027 req->flags |= REQ_F_SEQ_PREV;
1028 async_list->io_pages += len;
1029 } else {
1030 io_end = 0;
1031 async_list->io_pages = 0;
1032 }
1033 }
1034
1035 /* New file? Reset state. */
1036 if (async_list->file != filp) {
1037 async_list->io_pages = 0;
1038 async_list->file = filp;
1039 }
1040 async_list->io_end = io_end;
1041}
1042
Jens Axboee0c5c572019-03-12 10:18:47 -06001043static int io_read(struct io_kiocb *req, const struct sqe_submit *s,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001044 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001045{
1046 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1047 struct kiocb *kiocb = &req->rw;
1048 struct iov_iter iter;
1049 struct file *file;
Jens Axboe31b51512019-01-18 22:56:34 -07001050 size_t iov_count;
Jens Axboee0c5c572019-03-12 10:18:47 -06001051 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001052
Jens Axboe8358e3a2019-04-23 08:17:58 -06001053 ret = io_prep_rw(req, s, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001054 if (ret)
1055 return ret;
1056 file = kiocb->ki_filp;
1057
Jens Axboe2b188cc2019-01-07 10:46:33 -07001058 if (unlikely(!(file->f_mode & FMODE_READ)))
Jens Axboe09bb8392019-03-13 12:39:28 -06001059 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001060 if (unlikely(!file->f_op->read_iter))
Jens Axboe09bb8392019-03-13 12:39:28 -06001061 return -EINVAL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001062
1063 ret = io_import_iovec(req->ctx, READ, s, &iovec, &iter);
1064 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06001065 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001066
Jens Axboe31b51512019-01-18 22:56:34 -07001067 iov_count = iov_iter_count(&iter);
1068 ret = rw_verify_area(READ, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001069 if (!ret) {
1070 ssize_t ret2;
1071
1072 /* Catch -EAGAIN return for forced non-blocking submission */
1073 ret2 = call_read_iter(file, kiocb, &iter);
Jens Axboe31b51512019-01-18 22:56:34 -07001074 if (!force_nonblock || ret2 != -EAGAIN) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001075 io_rw_done(kiocb, ret2);
Jens Axboe31b51512019-01-18 22:56:34 -07001076 } else {
1077 /*
1078 * If ->needs_lock is true, we're already in async
1079 * context.
1080 */
1081 if (!s->needs_lock)
1082 io_async_list_note(READ, req, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001083 ret = -EAGAIN;
Jens Axboe31b51512019-01-18 22:56:34 -07001084 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001085 }
1086 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001087 return ret;
1088}
1089
Jens Axboee0c5c572019-03-12 10:18:47 -06001090static int io_write(struct io_kiocb *req, const struct sqe_submit *s,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001091 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001092{
1093 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1094 struct kiocb *kiocb = &req->rw;
1095 struct iov_iter iter;
1096 struct file *file;
Jens Axboe31b51512019-01-18 22:56:34 -07001097 size_t iov_count;
Jens Axboee0c5c572019-03-12 10:18:47 -06001098 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001099
Jens Axboe8358e3a2019-04-23 08:17:58 -06001100 ret = io_prep_rw(req, s, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001101 if (ret)
1102 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001103
Jens Axboe2b188cc2019-01-07 10:46:33 -07001104 file = kiocb->ki_filp;
1105 if (unlikely(!(file->f_mode & FMODE_WRITE)))
Jens Axboe09bb8392019-03-13 12:39:28 -06001106 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001107 if (unlikely(!file->f_op->write_iter))
Jens Axboe09bb8392019-03-13 12:39:28 -06001108 return -EINVAL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001109
1110 ret = io_import_iovec(req->ctx, WRITE, s, &iovec, &iter);
1111 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06001112 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001113
Jens Axboe31b51512019-01-18 22:56:34 -07001114 iov_count = iov_iter_count(&iter);
1115
1116 ret = -EAGAIN;
1117 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT)) {
1118 /* If ->needs_lock is true, we're already in async context. */
1119 if (!s->needs_lock)
1120 io_async_list_note(WRITE, req, iov_count);
1121 goto out_free;
1122 }
1123
1124 ret = rw_verify_area(WRITE, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001125 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01001126 ssize_t ret2;
1127
Jens Axboe2b188cc2019-01-07 10:46:33 -07001128 /*
1129 * Open-code file_start_write here to grab freeze protection,
1130 * which will be released by another thread in
1131 * io_complete_rw(). Fool lockdep by telling it the lock got
1132 * released so that it doesn't complain about the held lock when
1133 * we return to userspace.
1134 */
1135 if (S_ISREG(file_inode(file)->i_mode)) {
1136 __sb_start_write(file_inode(file)->i_sb,
1137 SB_FREEZE_WRITE, true);
1138 __sb_writers_release(file_inode(file)->i_sb,
1139 SB_FREEZE_WRITE);
1140 }
1141 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01001142
1143 ret2 = call_write_iter(file, kiocb, &iter);
1144 if (!force_nonblock || ret2 != -EAGAIN) {
1145 io_rw_done(kiocb, ret2);
1146 } else {
1147 /*
1148 * If ->needs_lock is true, we're already in async
1149 * context.
1150 */
1151 if (!s->needs_lock)
1152 io_async_list_note(WRITE, req, iov_count);
1153 ret = -EAGAIN;
1154 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001155 }
Jens Axboe31b51512019-01-18 22:56:34 -07001156out_free:
Jens Axboe2b188cc2019-01-07 10:46:33 -07001157 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001158 return ret;
1159}
1160
1161/*
1162 * IORING_OP_NOP just posts a completion event, nothing else.
1163 */
1164static int io_nop(struct io_kiocb *req, u64 user_data)
1165{
1166 struct io_ring_ctx *ctx = req->ctx;
1167 long err = 0;
1168
Jens Axboedef596e2019-01-09 08:59:42 -07001169 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1170 return -EINVAL;
1171
Jens Axboe2b188cc2019-01-07 10:46:33 -07001172 io_cqring_add_event(ctx, user_data, err, 0);
Jens Axboee65ef562019-03-12 10:16:44 -06001173 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001174 return 0;
1175}
1176
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001177static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1178{
Jens Axboe6b063142019-01-10 22:13:58 -07001179 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001180
Jens Axboe09bb8392019-03-13 12:39:28 -06001181 if (!req->file)
1182 return -EBADF;
Jens Axboed530a402019-03-13 12:15:01 -06001183 /* Prep already done (EAGAIN retry) */
1184 if (req->flags & REQ_F_PREPPED)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001185 return 0;
1186
Jens Axboe6b063142019-01-10 22:13:58 -07001187 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07001188 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07001189 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001190 return -EINVAL;
1191
Jens Axboed530a402019-03-13 12:15:01 -06001192 req->flags |= REQ_F_PREPPED;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001193 return 0;
1194}
1195
1196static int io_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1197 bool force_nonblock)
1198{
1199 loff_t sqe_off = READ_ONCE(sqe->off);
1200 loff_t sqe_len = READ_ONCE(sqe->len);
1201 loff_t end = sqe_off + sqe_len;
1202 unsigned fsync_flags;
1203 int ret;
1204
1205 fsync_flags = READ_ONCE(sqe->fsync_flags);
1206 if (unlikely(fsync_flags & ~IORING_FSYNC_DATASYNC))
1207 return -EINVAL;
1208
1209 ret = io_prep_fsync(req, sqe);
1210 if (ret)
1211 return ret;
1212
1213 /* fsync always requires a blocking context */
1214 if (force_nonblock)
1215 return -EAGAIN;
1216
1217 ret = vfs_fsync_range(req->rw.ki_filp, sqe_off,
1218 end > 0 ? end : LLONG_MAX,
1219 fsync_flags & IORING_FSYNC_DATASYNC);
1220
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001221 io_cqring_add_event(req->ctx, sqe->user_data, ret, 0);
Jens Axboee65ef562019-03-12 10:16:44 -06001222 io_put_req(req);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001223 return 0;
1224}
1225
Jens Axboe221c5eb2019-01-17 09:41:58 -07001226static void io_poll_remove_one(struct io_kiocb *req)
1227{
1228 struct io_poll_iocb *poll = &req->poll;
1229
1230 spin_lock(&poll->head->lock);
1231 WRITE_ONCE(poll->canceled, true);
1232 if (!list_empty(&poll->wait.entry)) {
1233 list_del_init(&poll->wait.entry);
1234 queue_work(req->ctx->sqo_wq, &req->work);
1235 }
1236 spin_unlock(&poll->head->lock);
1237
1238 list_del_init(&req->list);
1239}
1240
1241static void io_poll_remove_all(struct io_ring_ctx *ctx)
1242{
1243 struct io_kiocb *req;
1244
1245 spin_lock_irq(&ctx->completion_lock);
1246 while (!list_empty(&ctx->cancel_list)) {
1247 req = list_first_entry(&ctx->cancel_list, struct io_kiocb,list);
1248 io_poll_remove_one(req);
1249 }
1250 spin_unlock_irq(&ctx->completion_lock);
1251}
1252
1253/*
1254 * Find a running poll command that matches one specified in sqe->addr,
1255 * and remove it if found.
1256 */
1257static int io_poll_remove(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1258{
1259 struct io_ring_ctx *ctx = req->ctx;
1260 struct io_kiocb *poll_req, *next;
1261 int ret = -ENOENT;
1262
1263 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1264 return -EINVAL;
1265 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
1266 sqe->poll_events)
1267 return -EINVAL;
1268
1269 spin_lock_irq(&ctx->completion_lock);
1270 list_for_each_entry_safe(poll_req, next, &ctx->cancel_list, list) {
1271 if (READ_ONCE(sqe->addr) == poll_req->user_data) {
1272 io_poll_remove_one(poll_req);
1273 ret = 0;
1274 break;
1275 }
1276 }
1277 spin_unlock_irq(&ctx->completion_lock);
1278
1279 io_cqring_add_event(req->ctx, sqe->user_data, ret, 0);
Jens Axboee65ef562019-03-12 10:16:44 -06001280 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001281 return 0;
1282}
1283
Jens Axboe8c838782019-03-12 15:48:16 -06001284static void io_poll_complete(struct io_ring_ctx *ctx, struct io_kiocb *req,
1285 __poll_t mask)
Jens Axboe221c5eb2019-01-17 09:41:58 -07001286{
Jens Axboe8c838782019-03-12 15:48:16 -06001287 req->poll.done = true;
1288 io_cqring_fill_event(ctx, req->user_data, mangle_poll(mask), 0);
1289 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001290}
1291
1292static void io_poll_complete_work(struct work_struct *work)
1293{
1294 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
1295 struct io_poll_iocb *poll = &req->poll;
1296 struct poll_table_struct pt = { ._key = poll->events };
1297 struct io_ring_ctx *ctx = req->ctx;
1298 __poll_t mask = 0;
1299
1300 if (!READ_ONCE(poll->canceled))
1301 mask = vfs_poll(poll->file, &pt) & poll->events;
1302
1303 /*
1304 * Note that ->ki_cancel callers also delete iocb from active_reqs after
1305 * calling ->ki_cancel. We need the ctx_lock roundtrip here to
1306 * synchronize with them. In the cancellation case the list_del_init
1307 * itself is not actually needed, but harmless so we keep it in to
1308 * avoid further branches in the fast path.
1309 */
1310 spin_lock_irq(&ctx->completion_lock);
1311 if (!mask && !READ_ONCE(poll->canceled)) {
1312 add_wait_queue(poll->head, &poll->wait);
1313 spin_unlock_irq(&ctx->completion_lock);
1314 return;
1315 }
1316 list_del_init(&req->list);
Jens Axboe8c838782019-03-12 15:48:16 -06001317 io_poll_complete(ctx, req, mask);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001318 spin_unlock_irq(&ctx->completion_lock);
1319
Jens Axboe8c838782019-03-12 15:48:16 -06001320 io_cqring_ev_posted(ctx);
1321 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001322}
1323
1324static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
1325 void *key)
1326{
1327 struct io_poll_iocb *poll = container_of(wait, struct io_poll_iocb,
1328 wait);
1329 struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
1330 struct io_ring_ctx *ctx = req->ctx;
1331 __poll_t mask = key_to_poll(key);
Jens Axboe8c838782019-03-12 15:48:16 -06001332 unsigned long flags;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001333
1334 /* for instances that support it check for an event match first: */
Jens Axboe8c838782019-03-12 15:48:16 -06001335 if (mask && !(mask & poll->events))
1336 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001337
1338 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06001339
1340 if (mask && spin_trylock_irqsave(&ctx->completion_lock, flags)) {
1341 list_del(&req->list);
1342 io_poll_complete(ctx, req, mask);
1343 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1344
1345 io_cqring_ev_posted(ctx);
1346 io_put_req(req);
1347 } else {
1348 queue_work(ctx->sqo_wq, &req->work);
1349 }
1350
Jens Axboe221c5eb2019-01-17 09:41:58 -07001351 return 1;
1352}
1353
1354struct io_poll_table {
1355 struct poll_table_struct pt;
1356 struct io_kiocb *req;
1357 int error;
1358};
1359
1360static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
1361 struct poll_table_struct *p)
1362{
1363 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
1364
1365 if (unlikely(pt->req->poll.head)) {
1366 pt->error = -EINVAL;
1367 return;
1368 }
1369
1370 pt->error = 0;
1371 pt->req->poll.head = head;
1372 add_wait_queue(head, &pt->req->poll.wait);
1373}
1374
1375static int io_poll_add(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1376{
1377 struct io_poll_iocb *poll = &req->poll;
1378 struct io_ring_ctx *ctx = req->ctx;
1379 struct io_poll_table ipt;
Jens Axboe8c838782019-03-12 15:48:16 -06001380 bool cancel = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001381 __poll_t mask;
1382 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001383
1384 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1385 return -EINVAL;
1386 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
1387 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06001388 if (!poll->file)
1389 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001390
1391 INIT_WORK(&req->work, io_poll_complete_work);
1392 events = READ_ONCE(sqe->poll_events);
1393 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
1394
Jens Axboe221c5eb2019-01-17 09:41:58 -07001395 poll->head = NULL;
Jens Axboe8c838782019-03-12 15:48:16 -06001396 poll->done = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001397 poll->canceled = false;
1398
1399 ipt.pt._qproc = io_poll_queue_proc;
1400 ipt.pt._key = poll->events;
1401 ipt.req = req;
1402 ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
1403
1404 /* initialized the list so that we can do list_empty checks */
1405 INIT_LIST_HEAD(&poll->wait.entry);
1406 init_waitqueue_func_entry(&poll->wait, io_poll_wake);
1407
Jens Axboe221c5eb2019-01-17 09:41:58 -07001408 mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001409
1410 spin_lock_irq(&ctx->completion_lock);
Jens Axboe8c838782019-03-12 15:48:16 -06001411 if (likely(poll->head)) {
1412 spin_lock(&poll->head->lock);
1413 if (unlikely(list_empty(&poll->wait.entry))) {
1414 if (ipt.error)
1415 cancel = true;
1416 ipt.error = 0;
1417 mask = 0;
1418 }
1419 if (mask || ipt.error)
1420 list_del_init(&poll->wait.entry);
1421 else if (cancel)
1422 WRITE_ONCE(poll->canceled, true);
1423 else if (!poll->done) /* actually waiting for an event */
1424 list_add_tail(&req->list, &ctx->cancel_list);
1425 spin_unlock(&poll->head->lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001426 }
Jens Axboe8c838782019-03-12 15:48:16 -06001427 if (mask) { /* no async, we'd stolen it */
1428 req->error = mangle_poll(mask);
1429 ipt.error = 0;
1430 io_poll_complete(ctx, req, mask);
1431 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07001432 spin_unlock_irq(&ctx->completion_lock);
1433
Jens Axboe8c838782019-03-12 15:48:16 -06001434 if (mask) {
1435 io_cqring_ev_posted(ctx);
Jens Axboee65ef562019-03-12 10:16:44 -06001436 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001437 }
Jens Axboe8c838782019-03-12 15:48:16 -06001438 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001439}
1440
Jens Axboe2b188cc2019-01-07 10:46:33 -07001441static int __io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001442 const struct sqe_submit *s, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001443{
Jens Axboee0c5c572019-03-12 10:18:47 -06001444 int ret, opcode;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001445
1446 if (unlikely(s->index >= ctx->sq_entries))
1447 return -EINVAL;
1448 req->user_data = READ_ONCE(s->sqe->user_data);
1449
1450 opcode = READ_ONCE(s->sqe->opcode);
1451 switch (opcode) {
1452 case IORING_OP_NOP:
1453 ret = io_nop(req, req->user_data);
1454 break;
1455 case IORING_OP_READV:
Jens Axboeedafcce2019-01-09 09:16:05 -07001456 if (unlikely(s->sqe->buf_index))
1457 return -EINVAL;
Jens Axboe8358e3a2019-04-23 08:17:58 -06001458 ret = io_read(req, s, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001459 break;
1460 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07001461 if (unlikely(s->sqe->buf_index))
1462 return -EINVAL;
Jens Axboe8358e3a2019-04-23 08:17:58 -06001463 ret = io_write(req, s, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07001464 break;
1465 case IORING_OP_READ_FIXED:
Jens Axboe8358e3a2019-04-23 08:17:58 -06001466 ret = io_read(req, s, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07001467 break;
1468 case IORING_OP_WRITE_FIXED:
Jens Axboe8358e3a2019-04-23 08:17:58 -06001469 ret = io_write(req, s, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001470 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001471 case IORING_OP_FSYNC:
1472 ret = io_fsync(req, s->sqe, force_nonblock);
1473 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001474 case IORING_OP_POLL_ADD:
1475 ret = io_poll_add(req, s->sqe);
1476 break;
1477 case IORING_OP_POLL_REMOVE:
1478 ret = io_poll_remove(req, s->sqe);
1479 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001480 default:
1481 ret = -EINVAL;
1482 break;
1483 }
1484
Jens Axboedef596e2019-01-09 08:59:42 -07001485 if (ret)
1486 return ret;
1487
1488 if (ctx->flags & IORING_SETUP_IOPOLL) {
1489 if (req->error == -EAGAIN)
1490 return -EAGAIN;
1491
1492 /* workqueue context doesn't hold uring_lock, grab it now */
1493 if (s->needs_lock)
1494 mutex_lock(&ctx->uring_lock);
1495 io_iopoll_req_issued(req);
1496 if (s->needs_lock)
1497 mutex_unlock(&ctx->uring_lock);
1498 }
1499
1500 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001501}
1502
Jens Axboe31b51512019-01-18 22:56:34 -07001503static struct async_list *io_async_list_from_sqe(struct io_ring_ctx *ctx,
1504 const struct io_uring_sqe *sqe)
1505{
1506 switch (sqe->opcode) {
1507 case IORING_OP_READV:
1508 case IORING_OP_READ_FIXED:
1509 return &ctx->pending_async[READ];
1510 case IORING_OP_WRITEV:
1511 case IORING_OP_WRITE_FIXED:
1512 return &ctx->pending_async[WRITE];
1513 default:
1514 return NULL;
1515 }
1516}
1517
Jens Axboeedafcce2019-01-09 09:16:05 -07001518static inline bool io_sqe_needs_user(const struct io_uring_sqe *sqe)
1519{
1520 u8 opcode = READ_ONCE(sqe->opcode);
1521
1522 return !(opcode == IORING_OP_READ_FIXED ||
1523 opcode == IORING_OP_WRITE_FIXED);
1524}
1525
Jens Axboe2b188cc2019-01-07 10:46:33 -07001526static void io_sq_wq_submit_work(struct work_struct *work)
1527{
1528 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001529 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe31b51512019-01-18 22:56:34 -07001530 struct mm_struct *cur_mm = NULL;
1531 struct async_list *async_list;
1532 LIST_HEAD(req_list);
Jens Axboeedafcce2019-01-09 09:16:05 -07001533 mm_segment_t old_fs;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001534 int ret;
1535
Jens Axboe31b51512019-01-18 22:56:34 -07001536 async_list = io_async_list_from_sqe(ctx, req->submit.sqe);
1537restart:
1538 do {
1539 struct sqe_submit *s = &req->submit;
1540 const struct io_uring_sqe *sqe = s->sqe;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001541
Stefan Bühler8449eed2019-04-27 20:34:19 +02001542 /* Ensure we clear previously set non-block flag */
Jens Axboe31b51512019-01-18 22:56:34 -07001543 req->rw.ki_flags &= ~IOCB_NOWAIT;
1544
1545 ret = 0;
1546 if (io_sqe_needs_user(sqe) && !cur_mm) {
1547 if (!mmget_not_zero(ctx->sqo_mm)) {
1548 ret = -EFAULT;
1549 } else {
1550 cur_mm = ctx->sqo_mm;
1551 use_mm(cur_mm);
1552 old_fs = get_fs();
1553 set_fs(USER_DS);
1554 }
1555 }
1556
1557 if (!ret) {
1558 s->has_user = cur_mm != NULL;
1559 s->needs_lock = true;
1560 do {
Jens Axboe8358e3a2019-04-23 08:17:58 -06001561 ret = __io_submit_sqe(ctx, req, s, false);
Jens Axboe31b51512019-01-18 22:56:34 -07001562 /*
1563 * We can get EAGAIN for polled IO even though
1564 * we're forcing a sync submission from here,
1565 * since we can't wait for request slots on the
1566 * block side.
1567 */
1568 if (ret != -EAGAIN)
1569 break;
1570 cond_resched();
1571 } while (1);
1572 }
Jens Axboe817869d2019-04-30 14:44:05 -06001573
1574 /* drop submission reference */
1575 io_put_req(req);
1576
Jens Axboe31b51512019-01-18 22:56:34 -07001577 if (ret) {
1578 io_cqring_add_event(ctx, sqe->user_data, ret, 0);
Jens Axboee65ef562019-03-12 10:16:44 -06001579 io_put_req(req);
Jens Axboe31b51512019-01-18 22:56:34 -07001580 }
1581
1582 /* async context always use a copy of the sqe */
1583 kfree(sqe);
1584
1585 if (!async_list)
1586 break;
1587 if (!list_empty(&req_list)) {
1588 req = list_first_entry(&req_list, struct io_kiocb,
1589 list);
1590 list_del(&req->list);
1591 continue;
1592 }
1593 if (list_empty(&async_list->list))
1594 break;
1595
1596 req = NULL;
1597 spin_lock(&async_list->lock);
1598 if (list_empty(&async_list->list)) {
1599 spin_unlock(&async_list->lock);
1600 break;
1601 }
1602 list_splice_init(&async_list->list, &req_list);
1603 spin_unlock(&async_list->lock);
1604
1605 req = list_first_entry(&req_list, struct io_kiocb, list);
1606 list_del(&req->list);
1607 } while (req);
Jens Axboeedafcce2019-01-09 09:16:05 -07001608
1609 /*
Jens Axboe31b51512019-01-18 22:56:34 -07001610 * Rare case of racing with a submitter. If we find the count has
1611 * dropped to zero AND we have pending work items, then restart
1612 * the processing. This is a tiny race window.
Jens Axboeedafcce2019-01-09 09:16:05 -07001613 */
Jens Axboe31b51512019-01-18 22:56:34 -07001614 if (async_list) {
1615 ret = atomic_dec_return(&async_list->cnt);
1616 while (!ret && !list_empty(&async_list->list)) {
1617 spin_lock(&async_list->lock);
1618 atomic_inc(&async_list->cnt);
1619 list_splice_init(&async_list->list, &req_list);
1620 spin_unlock(&async_list->lock);
1621
1622 if (!list_empty(&req_list)) {
1623 req = list_first_entry(&req_list,
1624 struct io_kiocb, list);
1625 list_del(&req->list);
1626 goto restart;
1627 }
1628 ret = atomic_dec_return(&async_list->cnt);
Jens Axboeedafcce2019-01-09 09:16:05 -07001629 }
Jens Axboeedafcce2019-01-09 09:16:05 -07001630 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001631
Jens Axboe31b51512019-01-18 22:56:34 -07001632 if (cur_mm) {
Jens Axboeedafcce2019-01-09 09:16:05 -07001633 set_fs(old_fs);
Jens Axboe31b51512019-01-18 22:56:34 -07001634 unuse_mm(cur_mm);
1635 mmput(cur_mm);
Jens Axboeedafcce2019-01-09 09:16:05 -07001636 }
Jens Axboe31b51512019-01-18 22:56:34 -07001637}
Jens Axboe2b188cc2019-01-07 10:46:33 -07001638
Jens Axboe31b51512019-01-18 22:56:34 -07001639/*
1640 * See if we can piggy back onto previously submitted work, that is still
1641 * running. We currently only allow this if the new request is sequential
1642 * to the previous one we punted.
1643 */
1644static bool io_add_to_prev_work(struct async_list *list, struct io_kiocb *req)
1645{
1646 bool ret = false;
1647
1648 if (!list)
1649 return false;
1650 if (!(req->flags & REQ_F_SEQ_PREV))
1651 return false;
1652 if (!atomic_read(&list->cnt))
1653 return false;
1654
1655 ret = true;
1656 spin_lock(&list->lock);
1657 list_add_tail(&req->list, &list->list);
1658 if (!atomic_read(&list->cnt)) {
1659 list_del_init(&req->list);
1660 ret = false;
1661 }
1662 spin_unlock(&list->lock);
1663 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001664}
1665
Jens Axboe09bb8392019-03-13 12:39:28 -06001666static bool io_op_needs_file(const struct io_uring_sqe *sqe)
1667{
1668 int op = READ_ONCE(sqe->opcode);
1669
1670 switch (op) {
1671 case IORING_OP_NOP:
1672 case IORING_OP_POLL_REMOVE:
1673 return false;
1674 default:
1675 return true;
1676 }
1677}
1678
1679static int io_req_set_file(struct io_ring_ctx *ctx, const struct sqe_submit *s,
1680 struct io_submit_state *state, struct io_kiocb *req)
1681{
1682 unsigned flags;
1683 int fd;
1684
1685 flags = READ_ONCE(s->sqe->flags);
1686 fd = READ_ONCE(s->sqe->fd);
1687
1688 if (!io_op_needs_file(s->sqe)) {
1689 req->file = NULL;
1690 return 0;
1691 }
1692
1693 if (flags & IOSQE_FIXED_FILE) {
1694 if (unlikely(!ctx->user_files ||
1695 (unsigned) fd >= ctx->nr_user_files))
1696 return -EBADF;
1697 req->file = ctx->user_files[fd];
1698 req->flags |= REQ_F_FIXED_FILE;
1699 } else {
1700 if (s->needs_fixed_file)
1701 return -EBADF;
1702 req->file = io_file_get(state, fd);
1703 if (unlikely(!req->file))
1704 return -EBADF;
1705 }
1706
1707 return 0;
1708}
1709
Jens Axboe9a56a232019-01-09 09:06:50 -07001710static int io_submit_sqe(struct io_ring_ctx *ctx, struct sqe_submit *s,
1711 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001712{
1713 struct io_kiocb *req;
Jens Axboee0c5c572019-03-12 10:18:47 -06001714 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001715
1716 /* enforce forwards compatibility on users */
Jens Axboe6b063142019-01-10 22:13:58 -07001717 if (unlikely(s->sqe->flags & ~IOSQE_FIXED_FILE))
Jens Axboe2b188cc2019-01-07 10:46:33 -07001718 return -EINVAL;
1719
Jens Axboe2579f912019-01-09 09:10:43 -07001720 req = io_get_req(ctx, state);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001721 if (unlikely(!req))
1722 return -EAGAIN;
1723
Jens Axboe09bb8392019-03-13 12:39:28 -06001724 ret = io_req_set_file(ctx, s, state, req);
1725 if (unlikely(ret))
1726 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001727
Jens Axboe8358e3a2019-04-23 08:17:58 -06001728 ret = __io_submit_sqe(ctx, req, s, true);
Stefan Bühler8449eed2019-04-27 20:34:19 +02001729 if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001730 struct io_uring_sqe *sqe_copy;
1731
1732 sqe_copy = kmalloc(sizeof(*sqe_copy), GFP_KERNEL);
1733 if (sqe_copy) {
Jens Axboe31b51512019-01-18 22:56:34 -07001734 struct async_list *list;
1735
Jens Axboe2b188cc2019-01-07 10:46:33 -07001736 memcpy(sqe_copy, s->sqe, sizeof(*sqe_copy));
1737 s->sqe = sqe_copy;
1738
1739 memcpy(&req->submit, s, sizeof(*s));
Jens Axboe31b51512019-01-18 22:56:34 -07001740 list = io_async_list_from_sqe(ctx, s->sqe);
1741 if (!io_add_to_prev_work(list, req)) {
1742 if (list)
1743 atomic_inc(&list->cnt);
1744 INIT_WORK(&req->work, io_sq_wq_submit_work);
1745 queue_work(ctx->sqo_wq, &req->work);
1746 }
Jens Axboee65ef562019-03-12 10:16:44 -06001747
1748 /*
1749 * Queued up for async execution, worker will release
1750 * submit reference when the iocb is actually
1751 * submitted.
1752 */
1753 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001754 }
1755 }
Jens Axboee65ef562019-03-12 10:16:44 -06001756
Jens Axboe09bb8392019-03-13 12:39:28 -06001757out:
Jens Axboee65ef562019-03-12 10:16:44 -06001758 /* drop submission reference */
1759 io_put_req(req);
1760
1761 /* and drop final reference, if we failed */
Jens Axboe2b188cc2019-01-07 10:46:33 -07001762 if (ret)
Jens Axboee65ef562019-03-12 10:16:44 -06001763 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001764
1765 return ret;
1766}
1767
Jens Axboe9a56a232019-01-09 09:06:50 -07001768/*
1769 * Batched submission is done, ensure local IO is flushed out.
1770 */
1771static void io_submit_state_end(struct io_submit_state *state)
1772{
1773 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06001774 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07001775 if (state->free_reqs)
1776 kmem_cache_free_bulk(req_cachep, state->free_reqs,
1777 &state->reqs[state->cur_req]);
Jens Axboe9a56a232019-01-09 09:06:50 -07001778}
1779
1780/*
1781 * Start submission side cache.
1782 */
1783static void io_submit_state_start(struct io_submit_state *state,
1784 struct io_ring_ctx *ctx, unsigned max_ios)
1785{
1786 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07001787 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07001788 state->file = NULL;
1789 state->ios_left = max_ios;
1790}
1791
Jens Axboe2b188cc2019-01-07 10:46:33 -07001792static void io_commit_sqring(struct io_ring_ctx *ctx)
1793{
1794 struct io_sq_ring *ring = ctx->sq_ring;
1795
1796 if (ctx->cached_sq_head != READ_ONCE(ring->r.head)) {
1797 /*
1798 * Ensure any loads from the SQEs are done at this point,
1799 * since once we write the new head, the application could
1800 * write new data to them.
1801 */
1802 smp_store_release(&ring->r.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001803 }
1804}
1805
1806/*
Jens Axboe2b188cc2019-01-07 10:46:33 -07001807 * Fetch an sqe, if one is available. Note that s->sqe will point to memory
1808 * that is mapped by userspace. This means that care needs to be taken to
1809 * ensure that reads are stable, as we cannot rely on userspace always
1810 * being a good citizen. If members of the sqe are validated and then later
1811 * used, it's important that those reads are done through READ_ONCE() to
1812 * prevent a re-load down the line.
1813 */
1814static bool io_get_sqring(struct io_ring_ctx *ctx, struct sqe_submit *s)
1815{
1816 struct io_sq_ring *ring = ctx->sq_ring;
1817 unsigned head;
1818
1819 /*
1820 * The cached sq head (or cq tail) serves two purposes:
1821 *
1822 * 1) allows us to batch the cost of updating the user visible
1823 * head updates.
1824 * 2) allows the kernel side to track the head on its own, even
1825 * though the application is the one updating it.
1826 */
1827 head = ctx->cached_sq_head;
Stefan Bühlere523a292019-04-19 11:57:44 +02001828 /* make sure SQ entry isn't read before tail */
1829 if (head == smp_load_acquire(&ring->r.tail))
Jens Axboe2b188cc2019-01-07 10:46:33 -07001830 return false;
1831
1832 head = READ_ONCE(ring->array[head & ctx->sq_mask]);
1833 if (head < ctx->sq_entries) {
1834 s->index = head;
1835 s->sqe = &ctx->sq_sqes[head];
1836 ctx->cached_sq_head++;
1837 return true;
1838 }
1839
1840 /* drop invalid entries */
1841 ctx->cached_sq_head++;
1842 ring->dropped++;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001843 return false;
1844}
1845
Jens Axboe6c271ce2019-01-10 11:22:30 -07001846static int io_submit_sqes(struct io_ring_ctx *ctx, struct sqe_submit *sqes,
1847 unsigned int nr, bool has_user, bool mm_fault)
1848{
1849 struct io_submit_state state, *statep = NULL;
1850 int ret, i, submitted = 0;
1851
1852 if (nr > IO_PLUG_THRESHOLD) {
1853 io_submit_state_start(&state, ctx, nr);
1854 statep = &state;
1855 }
1856
1857 for (i = 0; i < nr; i++) {
1858 if (unlikely(mm_fault)) {
1859 ret = -EFAULT;
1860 } else {
1861 sqes[i].has_user = has_user;
1862 sqes[i].needs_lock = true;
1863 sqes[i].needs_fixed_file = true;
1864 ret = io_submit_sqe(ctx, &sqes[i], statep);
1865 }
1866 if (!ret) {
1867 submitted++;
1868 continue;
1869 }
1870
1871 io_cqring_add_event(ctx, sqes[i].sqe->user_data, ret, 0);
1872 }
1873
1874 if (statep)
1875 io_submit_state_end(&state);
1876
1877 return submitted;
1878}
1879
1880static int io_sq_thread(void *data)
1881{
1882 struct sqe_submit sqes[IO_IOPOLL_BATCH];
1883 struct io_ring_ctx *ctx = data;
1884 struct mm_struct *cur_mm = NULL;
1885 mm_segment_t old_fs;
1886 DEFINE_WAIT(wait);
1887 unsigned inflight;
1888 unsigned long timeout;
1889
1890 old_fs = get_fs();
1891 set_fs(USER_DS);
1892
1893 timeout = inflight = 0;
1894 while (!kthread_should_stop() && !ctx->sqo_stop) {
1895 bool all_fixed, mm_fault = false;
1896 int i;
1897
1898 if (inflight) {
1899 unsigned nr_events = 0;
1900
1901 if (ctx->flags & IORING_SETUP_IOPOLL) {
1902 /*
1903 * We disallow the app entering submit/complete
1904 * with polling, but we still need to lock the
1905 * ring to prevent racing with polled issue
1906 * that got punted to a workqueue.
1907 */
1908 mutex_lock(&ctx->uring_lock);
1909 io_iopoll_check(ctx, &nr_events, 0);
1910 mutex_unlock(&ctx->uring_lock);
1911 } else {
1912 /*
1913 * Normal IO, just pretend everything completed.
1914 * We don't have to poll completions for that.
1915 */
1916 nr_events = inflight;
1917 }
1918
1919 inflight -= nr_events;
1920 if (!inflight)
1921 timeout = jiffies + ctx->sq_thread_idle;
1922 }
1923
1924 if (!io_get_sqring(ctx, &sqes[0])) {
1925 /*
1926 * We're polling. If we're within the defined idle
1927 * period, then let us spin without work before going
1928 * to sleep.
1929 */
1930 if (inflight || !time_after(jiffies, timeout)) {
1931 cpu_relax();
1932 continue;
1933 }
1934
1935 /*
1936 * Drop cur_mm before scheduling, we can't hold it for
1937 * long periods (or over schedule()). Do this before
1938 * adding ourselves to the waitqueue, as the unuse/drop
1939 * may sleep.
1940 */
1941 if (cur_mm) {
1942 unuse_mm(cur_mm);
1943 mmput(cur_mm);
1944 cur_mm = NULL;
1945 }
1946
1947 prepare_to_wait(&ctx->sqo_wait, &wait,
1948 TASK_INTERRUPTIBLE);
1949
1950 /* Tell userspace we may need a wakeup call */
1951 ctx->sq_ring->flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02001952 /* make sure to read SQ tail after writing flags */
1953 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07001954
1955 if (!io_get_sqring(ctx, &sqes[0])) {
1956 if (kthread_should_stop()) {
1957 finish_wait(&ctx->sqo_wait, &wait);
1958 break;
1959 }
1960 if (signal_pending(current))
1961 flush_signals(current);
1962 schedule();
1963 finish_wait(&ctx->sqo_wait, &wait);
1964
1965 ctx->sq_ring->flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07001966 continue;
1967 }
1968 finish_wait(&ctx->sqo_wait, &wait);
1969
1970 ctx->sq_ring->flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07001971 }
1972
1973 i = 0;
1974 all_fixed = true;
1975 do {
1976 if (all_fixed && io_sqe_needs_user(sqes[i].sqe))
1977 all_fixed = false;
1978
1979 i++;
1980 if (i == ARRAY_SIZE(sqes))
1981 break;
1982 } while (io_get_sqring(ctx, &sqes[i]));
1983
1984 /* Unless all new commands are FIXED regions, grab mm */
1985 if (!all_fixed && !cur_mm) {
1986 mm_fault = !mmget_not_zero(ctx->sqo_mm);
1987 if (!mm_fault) {
1988 use_mm(ctx->sqo_mm);
1989 cur_mm = ctx->sqo_mm;
1990 }
1991 }
1992
1993 inflight += io_submit_sqes(ctx, sqes, i, cur_mm != NULL,
1994 mm_fault);
1995
1996 /* Commit SQ ring head once we've consumed all SQEs */
1997 io_commit_sqring(ctx);
1998 }
1999
2000 set_fs(old_fs);
2001 if (cur_mm) {
2002 unuse_mm(cur_mm);
2003 mmput(cur_mm);
2004 }
Jens Axboe06058632019-04-13 09:26:03 -06002005
2006 if (kthread_should_park())
2007 kthread_parkme();
2008
Jens Axboe6c271ce2019-01-10 11:22:30 -07002009 return 0;
2010}
2011
Jens Axboe2b188cc2019-01-07 10:46:33 -07002012static int io_ring_submit(struct io_ring_ctx *ctx, unsigned int to_submit)
2013{
Jens Axboe9a56a232019-01-09 09:06:50 -07002014 struct io_submit_state state, *statep = NULL;
Jens Axboe5c8b0b52019-04-30 10:16:07 -06002015 int i, submit = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002016
Jens Axboe9a56a232019-01-09 09:06:50 -07002017 if (to_submit > IO_PLUG_THRESHOLD) {
2018 io_submit_state_start(&state, ctx, to_submit);
2019 statep = &state;
2020 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002021
2022 for (i = 0; i < to_submit; i++) {
2023 struct sqe_submit s;
Jens Axboe5c8b0b52019-04-30 10:16:07 -06002024 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002025
2026 if (!io_get_sqring(ctx, &s))
2027 break;
2028
2029 s.has_user = true;
Jens Axboedef596e2019-01-09 08:59:42 -07002030 s.needs_lock = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002031 s.needs_fixed_file = false;
Jens Axboe5c8b0b52019-04-30 10:16:07 -06002032 submit++;
Jens Axboedef596e2019-01-09 08:59:42 -07002033
Jens Axboe9a56a232019-01-09 09:06:50 -07002034 ret = io_submit_sqe(ctx, &s, statep);
Jens Axboe5c8b0b52019-04-30 10:16:07 -06002035 if (ret)
2036 io_cqring_add_event(ctx, s.sqe->user_data, ret, 0);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002037 }
2038 io_commit_sqring(ctx);
2039
Jens Axboe9a56a232019-01-09 09:06:50 -07002040 if (statep)
2041 io_submit_state_end(statep);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002042
Jens Axboe5c8b0b52019-04-30 10:16:07 -06002043 return submit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002044}
2045
2046static unsigned io_cqring_events(struct io_cq_ring *ring)
2047{
2048 return READ_ONCE(ring->r.tail) - READ_ONCE(ring->r.head);
2049}
2050
2051/*
2052 * Wait until events become available, if we don't already have some. The
2053 * application must reap them itself, as they reside on the shared cq ring.
2054 */
2055static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
2056 const sigset_t __user *sig, size_t sigsz)
2057{
2058 struct io_cq_ring *ring = ctx->cq_ring;
2059 sigset_t ksigmask, sigsaved;
2060 DEFINE_WAIT(wait);
2061 int ret;
2062
2063 /* See comment at the top of this file */
2064 smp_rmb();
2065 if (io_cqring_events(ring) >= min_events)
2066 return 0;
2067
2068 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01002069#ifdef CONFIG_COMPAT
2070 if (in_compat_syscall())
2071 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
2072 &ksigmask, &sigsaved, sigsz);
2073 else
2074#endif
2075 ret = set_user_sigmask(sig, &ksigmask,
2076 &sigsaved, sigsz);
2077
Jens Axboe2b188cc2019-01-07 10:46:33 -07002078 if (ret)
2079 return ret;
2080 }
2081
2082 do {
2083 prepare_to_wait(&ctx->wait, &wait, TASK_INTERRUPTIBLE);
2084
2085 ret = 0;
2086 /* See comment at the top of this file */
2087 smp_rmb();
2088 if (io_cqring_events(ring) >= min_events)
2089 break;
2090
2091 schedule();
2092
2093 ret = -EINTR;
2094 if (signal_pending(current))
2095 break;
2096 } while (1);
2097
2098 finish_wait(&ctx->wait, &wait);
2099
2100 if (sig)
2101 restore_user_sigmask(sig, &sigsaved);
2102
2103 return READ_ONCE(ring->r.head) == READ_ONCE(ring->r.tail) ? ret : 0;
2104}
2105
Jens Axboe6b063142019-01-10 22:13:58 -07002106static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
2107{
2108#if defined(CONFIG_UNIX)
2109 if (ctx->ring_sock) {
2110 struct sock *sock = ctx->ring_sock->sk;
2111 struct sk_buff *skb;
2112
2113 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
2114 kfree_skb(skb);
2115 }
2116#else
2117 int i;
2118
2119 for (i = 0; i < ctx->nr_user_files; i++)
2120 fput(ctx->user_files[i]);
2121#endif
2122}
2123
2124static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
2125{
2126 if (!ctx->user_files)
2127 return -ENXIO;
2128
2129 __io_sqe_files_unregister(ctx);
2130 kfree(ctx->user_files);
2131 ctx->user_files = NULL;
2132 ctx->nr_user_files = 0;
2133 return 0;
2134}
2135
Jens Axboe6c271ce2019-01-10 11:22:30 -07002136static void io_sq_thread_stop(struct io_ring_ctx *ctx)
2137{
2138 if (ctx->sqo_thread) {
2139 ctx->sqo_stop = 1;
2140 mb();
Jens Axboe06058632019-04-13 09:26:03 -06002141 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07002142 kthread_stop(ctx->sqo_thread);
2143 ctx->sqo_thread = NULL;
2144 }
2145}
2146
Jens Axboe6b063142019-01-10 22:13:58 -07002147static void io_finish_async(struct io_ring_ctx *ctx)
2148{
Jens Axboe6c271ce2019-01-10 11:22:30 -07002149 io_sq_thread_stop(ctx);
2150
Jens Axboe6b063142019-01-10 22:13:58 -07002151 if (ctx->sqo_wq) {
2152 destroy_workqueue(ctx->sqo_wq);
2153 ctx->sqo_wq = NULL;
2154 }
2155}
2156
2157#if defined(CONFIG_UNIX)
2158static void io_destruct_skb(struct sk_buff *skb)
2159{
2160 struct io_ring_ctx *ctx = skb->sk->sk_user_data;
2161
2162 io_finish_async(ctx);
2163 unix_destruct_scm(skb);
2164}
2165
2166/*
2167 * Ensure the UNIX gc is aware of our file set, so we are certain that
2168 * the io_uring can be safely unregistered on process exit, even if we have
2169 * loops in the file referencing.
2170 */
2171static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
2172{
2173 struct sock *sk = ctx->ring_sock->sk;
2174 struct scm_fp_list *fpl;
2175 struct sk_buff *skb;
2176 int i;
2177
2178 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
2179 unsigned long inflight = ctx->user->unix_inflight + nr;
2180
2181 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
2182 return -EMFILE;
2183 }
2184
2185 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
2186 if (!fpl)
2187 return -ENOMEM;
2188
2189 skb = alloc_skb(0, GFP_KERNEL);
2190 if (!skb) {
2191 kfree(fpl);
2192 return -ENOMEM;
2193 }
2194
2195 skb->sk = sk;
2196 skb->destructor = io_destruct_skb;
2197
2198 fpl->user = get_uid(ctx->user);
2199 for (i = 0; i < nr; i++) {
2200 fpl->fp[i] = get_file(ctx->user_files[i + offset]);
2201 unix_inflight(fpl->user, fpl->fp[i]);
2202 }
2203
2204 fpl->max = fpl->count = nr;
2205 UNIXCB(skb).fp = fpl;
2206 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
2207 skb_queue_head(&sk->sk_receive_queue, skb);
2208
2209 for (i = 0; i < nr; i++)
2210 fput(fpl->fp[i]);
2211
2212 return 0;
2213}
2214
2215/*
2216 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
2217 * causes regular reference counting to break down. We rely on the UNIX
2218 * garbage collection to take care of this problem for us.
2219 */
2220static int io_sqe_files_scm(struct io_ring_ctx *ctx)
2221{
2222 unsigned left, total;
2223 int ret = 0;
2224
2225 total = 0;
2226 left = ctx->nr_user_files;
2227 while (left) {
2228 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
2229 int ret;
2230
2231 ret = __io_sqe_files_scm(ctx, this_files, total);
2232 if (ret)
2233 break;
2234 left -= this_files;
2235 total += this_files;
2236 }
2237
2238 if (!ret)
2239 return 0;
2240
2241 while (total < ctx->nr_user_files) {
2242 fput(ctx->user_files[total]);
2243 total++;
2244 }
2245
2246 return ret;
2247}
2248#else
2249static int io_sqe_files_scm(struct io_ring_ctx *ctx)
2250{
2251 return 0;
2252}
2253#endif
2254
2255static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
2256 unsigned nr_args)
2257{
2258 __s32 __user *fds = (__s32 __user *) arg;
2259 int fd, ret = 0;
2260 unsigned i;
2261
2262 if (ctx->user_files)
2263 return -EBUSY;
2264 if (!nr_args)
2265 return -EINVAL;
2266 if (nr_args > IORING_MAX_FIXED_FILES)
2267 return -EMFILE;
2268
2269 ctx->user_files = kcalloc(nr_args, sizeof(struct file *), GFP_KERNEL);
2270 if (!ctx->user_files)
2271 return -ENOMEM;
2272
2273 for (i = 0; i < nr_args; i++) {
2274 ret = -EFAULT;
2275 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
2276 break;
2277
2278 ctx->user_files[i] = fget(fd);
2279
2280 ret = -EBADF;
2281 if (!ctx->user_files[i])
2282 break;
2283 /*
2284 * Don't allow io_uring instances to be registered. If UNIX
2285 * isn't enabled, then this causes a reference cycle and this
2286 * instance can never get freed. If UNIX is enabled we'll
2287 * handle it just fine, but there's still no point in allowing
2288 * a ring fd as it doesn't support regular read/write anyway.
2289 */
2290 if (ctx->user_files[i]->f_op == &io_uring_fops) {
2291 fput(ctx->user_files[i]);
2292 break;
2293 }
2294 ctx->nr_user_files++;
2295 ret = 0;
2296 }
2297
2298 if (ret) {
2299 for (i = 0; i < ctx->nr_user_files; i++)
2300 fput(ctx->user_files[i]);
2301
2302 kfree(ctx->user_files);
Jens Axboe25adf502019-04-03 09:52:40 -06002303 ctx->user_files = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07002304 ctx->nr_user_files = 0;
2305 return ret;
2306 }
2307
2308 ret = io_sqe_files_scm(ctx);
2309 if (ret)
2310 io_sqe_files_unregister(ctx);
2311
2312 return ret;
2313}
2314
Jens Axboe6c271ce2019-01-10 11:22:30 -07002315static int io_sq_offload_start(struct io_ring_ctx *ctx,
2316 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002317{
2318 int ret;
2319
Jens Axboe6c271ce2019-01-10 11:22:30 -07002320 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002321 mmgrab(current->mm);
2322 ctx->sqo_mm = current->mm;
2323
Jens Axboe6c271ce2019-01-10 11:22:30 -07002324 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06002325 ret = -EPERM;
2326 if (!capable(CAP_SYS_ADMIN))
2327 goto err;
2328
Jens Axboe917257d2019-04-13 09:28:55 -06002329 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
2330 if (!ctx->sq_thread_idle)
2331 ctx->sq_thread_idle = HZ;
2332
Jens Axboe6c271ce2019-01-10 11:22:30 -07002333 if (p->flags & IORING_SETUP_SQ_AFF) {
Mark Rutland975554b2019-04-30 13:34:51 +01002334 int cpu = array_index_nospec(p->sq_thread_cpu,
2335 nr_cpu_ids);
Jens Axboe6c271ce2019-01-10 11:22:30 -07002336
Jens Axboe917257d2019-04-13 09:28:55 -06002337 ret = -EINVAL;
Mark Rutland975554b2019-04-30 13:34:51 +01002338 if (!cpu_possible(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06002339 goto err;
2340
Jens Axboe6c271ce2019-01-10 11:22:30 -07002341 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
2342 ctx, cpu,
2343 "io_uring-sq");
2344 } else {
2345 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
2346 "io_uring-sq");
2347 }
2348 if (IS_ERR(ctx->sqo_thread)) {
2349 ret = PTR_ERR(ctx->sqo_thread);
2350 ctx->sqo_thread = NULL;
2351 goto err;
2352 }
2353 wake_up_process(ctx->sqo_thread);
2354 } else if (p->flags & IORING_SETUP_SQ_AFF) {
2355 /* Can't have SQ_AFF without SQPOLL */
2356 ret = -EINVAL;
2357 goto err;
2358 }
2359
Jens Axboe2b188cc2019-01-07 10:46:33 -07002360 /* Do QD, or 2 * CPUS, whatever is smallest */
2361 ctx->sqo_wq = alloc_workqueue("io_ring-wq", WQ_UNBOUND | WQ_FREEZABLE,
2362 min(ctx->sq_entries - 1, 2 * num_online_cpus()));
2363 if (!ctx->sqo_wq) {
2364 ret = -ENOMEM;
2365 goto err;
2366 }
2367
2368 return 0;
2369err:
Jens Axboe6c271ce2019-01-10 11:22:30 -07002370 io_sq_thread_stop(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002371 mmdrop(ctx->sqo_mm);
2372 ctx->sqo_mm = NULL;
2373 return ret;
2374}
2375
2376static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
2377{
2378 atomic_long_sub(nr_pages, &user->locked_vm);
2379}
2380
2381static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
2382{
2383 unsigned long page_limit, cur_pages, new_pages;
2384
2385 /* Don't allow more pages than we can safely lock */
2386 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
2387
2388 do {
2389 cur_pages = atomic_long_read(&user->locked_vm);
2390 new_pages = cur_pages + nr_pages;
2391 if (new_pages > page_limit)
2392 return -ENOMEM;
2393 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
2394 new_pages) != cur_pages);
2395
2396 return 0;
2397}
2398
2399static void io_mem_free(void *ptr)
2400{
Mark Rutland52e04ef2019-04-30 17:30:21 +01002401 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002402
Mark Rutland52e04ef2019-04-30 17:30:21 +01002403 if (!ptr)
2404 return;
2405
2406 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002407 if (put_page_testzero(page))
2408 free_compound_page(page);
2409}
2410
2411static void *io_mem_alloc(size_t size)
2412{
2413 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
2414 __GFP_NORETRY;
2415
2416 return (void *) __get_free_pages(gfp_flags, get_order(size));
2417}
2418
2419static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
2420{
2421 struct io_sq_ring *sq_ring;
2422 struct io_cq_ring *cq_ring;
2423 size_t bytes;
2424
2425 bytes = struct_size(sq_ring, array, sq_entries);
2426 bytes += array_size(sizeof(struct io_uring_sqe), sq_entries);
2427 bytes += struct_size(cq_ring, cqes, cq_entries);
2428
2429 return (bytes + PAGE_SIZE - 1) / PAGE_SIZE;
2430}
2431
Jens Axboeedafcce2019-01-09 09:16:05 -07002432static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
2433{
2434 int i, j;
2435
2436 if (!ctx->user_bufs)
2437 return -ENXIO;
2438
2439 for (i = 0; i < ctx->nr_user_bufs; i++) {
2440 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
2441
2442 for (j = 0; j < imu->nr_bvecs; j++)
2443 put_page(imu->bvec[j].bv_page);
2444
2445 if (ctx->account_mem)
2446 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01002447 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07002448 imu->nr_bvecs = 0;
2449 }
2450
2451 kfree(ctx->user_bufs);
2452 ctx->user_bufs = NULL;
2453 ctx->nr_user_bufs = 0;
2454 return 0;
2455}
2456
2457static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
2458 void __user *arg, unsigned index)
2459{
2460 struct iovec __user *src;
2461
2462#ifdef CONFIG_COMPAT
2463 if (ctx->compat) {
2464 struct compat_iovec __user *ciovs;
2465 struct compat_iovec ciov;
2466
2467 ciovs = (struct compat_iovec __user *) arg;
2468 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
2469 return -EFAULT;
2470
2471 dst->iov_base = (void __user *) (unsigned long) ciov.iov_base;
2472 dst->iov_len = ciov.iov_len;
2473 return 0;
2474 }
2475#endif
2476 src = (struct iovec __user *) arg;
2477 if (copy_from_user(dst, &src[index], sizeof(*dst)))
2478 return -EFAULT;
2479 return 0;
2480}
2481
2482static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
2483 unsigned nr_args)
2484{
2485 struct vm_area_struct **vmas = NULL;
2486 struct page **pages = NULL;
2487 int i, j, got_pages = 0;
2488 int ret = -EINVAL;
2489
2490 if (ctx->user_bufs)
2491 return -EBUSY;
2492 if (!nr_args || nr_args > UIO_MAXIOV)
2493 return -EINVAL;
2494
2495 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
2496 GFP_KERNEL);
2497 if (!ctx->user_bufs)
2498 return -ENOMEM;
2499
2500 for (i = 0; i < nr_args; i++) {
2501 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
2502 unsigned long off, start, end, ubuf;
2503 int pret, nr_pages;
2504 struct iovec iov;
2505 size_t size;
2506
2507 ret = io_copy_iov(ctx, &iov, arg, i);
2508 if (ret)
2509 break;
2510
2511 /*
2512 * Don't impose further limits on the size and buffer
2513 * constraints here, we'll -EINVAL later when IO is
2514 * submitted if they are wrong.
2515 */
2516 ret = -EFAULT;
2517 if (!iov.iov_base || !iov.iov_len)
2518 goto err;
2519
2520 /* arbitrary limit, but we need something */
2521 if (iov.iov_len > SZ_1G)
2522 goto err;
2523
2524 ubuf = (unsigned long) iov.iov_base;
2525 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
2526 start = ubuf >> PAGE_SHIFT;
2527 nr_pages = end - start;
2528
2529 if (ctx->account_mem) {
2530 ret = io_account_mem(ctx->user, nr_pages);
2531 if (ret)
2532 goto err;
2533 }
2534
2535 ret = 0;
2536 if (!pages || nr_pages > got_pages) {
2537 kfree(vmas);
2538 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01002539 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07002540 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01002541 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07002542 sizeof(struct vm_area_struct *),
2543 GFP_KERNEL);
2544 if (!pages || !vmas) {
2545 ret = -ENOMEM;
2546 if (ctx->account_mem)
2547 io_unaccount_mem(ctx->user, nr_pages);
2548 goto err;
2549 }
2550 got_pages = nr_pages;
2551 }
2552
Mark Rutlandd4ef6472019-05-01 16:59:16 +01002553 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07002554 GFP_KERNEL);
2555 ret = -ENOMEM;
2556 if (!imu->bvec) {
2557 if (ctx->account_mem)
2558 io_unaccount_mem(ctx->user, nr_pages);
2559 goto err;
2560 }
2561
2562 ret = 0;
2563 down_read(&current->mm->mmap_sem);
2564 pret = get_user_pages_longterm(ubuf, nr_pages, FOLL_WRITE,
2565 pages, vmas);
2566 if (pret == nr_pages) {
2567 /* don't support file backed memory */
2568 for (j = 0; j < nr_pages; j++) {
2569 struct vm_area_struct *vma = vmas[j];
2570
2571 if (vma->vm_file &&
2572 !is_file_hugepages(vma->vm_file)) {
2573 ret = -EOPNOTSUPP;
2574 break;
2575 }
2576 }
2577 } else {
2578 ret = pret < 0 ? pret : -EFAULT;
2579 }
2580 up_read(&current->mm->mmap_sem);
2581 if (ret) {
2582 /*
2583 * if we did partial map, or found file backed vmas,
2584 * release any pages we did get
2585 */
2586 if (pret > 0) {
2587 for (j = 0; j < pret; j++)
2588 put_page(pages[j]);
2589 }
2590 if (ctx->account_mem)
2591 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01002592 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07002593 goto err;
2594 }
2595
2596 off = ubuf & ~PAGE_MASK;
2597 size = iov.iov_len;
2598 for (j = 0; j < nr_pages; j++) {
2599 size_t vec_len;
2600
2601 vec_len = min_t(size_t, size, PAGE_SIZE - off);
2602 imu->bvec[j].bv_page = pages[j];
2603 imu->bvec[j].bv_len = vec_len;
2604 imu->bvec[j].bv_offset = off;
2605 off = 0;
2606 size -= vec_len;
2607 }
2608 /* store original address for later verification */
2609 imu->ubuf = ubuf;
2610 imu->len = iov.iov_len;
2611 imu->nr_bvecs = nr_pages;
2612
2613 ctx->nr_user_bufs++;
2614 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01002615 kvfree(pages);
2616 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07002617 return 0;
2618err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01002619 kvfree(pages);
2620 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07002621 io_sqe_buffer_unregister(ctx);
2622 return ret;
2623}
2624
Jens Axboe2b188cc2019-01-07 10:46:33 -07002625static void io_ring_ctx_free(struct io_ring_ctx *ctx)
2626{
Jens Axboe6b063142019-01-10 22:13:58 -07002627 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002628 if (ctx->sqo_mm)
2629 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07002630
2631 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07002632 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07002633 io_sqe_files_unregister(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07002634
Jens Axboe2b188cc2019-01-07 10:46:33 -07002635#if defined(CONFIG_UNIX)
2636 if (ctx->ring_sock)
2637 sock_release(ctx->ring_sock);
2638#endif
2639
2640 io_mem_free(ctx->sq_ring);
2641 io_mem_free(ctx->sq_sqes);
2642 io_mem_free(ctx->cq_ring);
2643
2644 percpu_ref_exit(&ctx->refs);
2645 if (ctx->account_mem)
2646 io_unaccount_mem(ctx->user,
2647 ring_pages(ctx->sq_entries, ctx->cq_entries));
2648 free_uid(ctx->user);
2649 kfree(ctx);
2650}
2651
2652static __poll_t io_uring_poll(struct file *file, poll_table *wait)
2653{
2654 struct io_ring_ctx *ctx = file->private_data;
2655 __poll_t mask = 0;
2656
2657 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02002658 /*
2659 * synchronizes with barrier from wq_has_sleeper call in
2660 * io_commit_cqring
2661 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07002662 smp_rmb();
Stefan Bühlerfb775fa2019-04-19 11:57:46 +02002663 if (READ_ONCE(ctx->sq_ring->r.tail) - ctx->cached_sq_head !=
2664 ctx->sq_ring->ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002665 mask |= EPOLLOUT | EPOLLWRNORM;
2666 if (READ_ONCE(ctx->cq_ring->r.head) != ctx->cached_cq_tail)
2667 mask |= EPOLLIN | EPOLLRDNORM;
2668
2669 return mask;
2670}
2671
2672static int io_uring_fasync(int fd, struct file *file, int on)
2673{
2674 struct io_ring_ctx *ctx = file->private_data;
2675
2676 return fasync_helper(fd, file, on, &ctx->cq_fasync);
2677}
2678
2679static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
2680{
2681 mutex_lock(&ctx->uring_lock);
2682 percpu_ref_kill(&ctx->refs);
2683 mutex_unlock(&ctx->uring_lock);
2684
Jens Axboe221c5eb2019-01-17 09:41:58 -07002685 io_poll_remove_all(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07002686 io_iopoll_reap_events(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002687 wait_for_completion(&ctx->ctx_done);
2688 io_ring_ctx_free(ctx);
2689}
2690
2691static int io_uring_release(struct inode *inode, struct file *file)
2692{
2693 struct io_ring_ctx *ctx = file->private_data;
2694
2695 file->private_data = NULL;
2696 io_ring_ctx_wait_and_kill(ctx);
2697 return 0;
2698}
2699
2700static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
2701{
2702 loff_t offset = (loff_t) vma->vm_pgoff << PAGE_SHIFT;
2703 unsigned long sz = vma->vm_end - vma->vm_start;
2704 struct io_ring_ctx *ctx = file->private_data;
2705 unsigned long pfn;
2706 struct page *page;
2707 void *ptr;
2708
2709 switch (offset) {
2710 case IORING_OFF_SQ_RING:
2711 ptr = ctx->sq_ring;
2712 break;
2713 case IORING_OFF_SQES:
2714 ptr = ctx->sq_sqes;
2715 break;
2716 case IORING_OFF_CQ_RING:
2717 ptr = ctx->cq_ring;
2718 break;
2719 default:
2720 return -EINVAL;
2721 }
2722
2723 page = virt_to_head_page(ptr);
2724 if (sz > (PAGE_SIZE << compound_order(page)))
2725 return -EINVAL;
2726
2727 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
2728 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
2729}
2730
2731SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
2732 u32, min_complete, u32, flags, const sigset_t __user *, sig,
2733 size_t, sigsz)
2734{
2735 struct io_ring_ctx *ctx;
2736 long ret = -EBADF;
2737 int submitted = 0;
2738 struct fd f;
2739
Jens Axboe6c271ce2019-01-10 11:22:30 -07002740 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002741 return -EINVAL;
2742
2743 f = fdget(fd);
2744 if (!f.file)
2745 return -EBADF;
2746
2747 ret = -EOPNOTSUPP;
2748 if (f.file->f_op != &io_uring_fops)
2749 goto out_fput;
2750
2751 ret = -ENXIO;
2752 ctx = f.file->private_data;
2753 if (!percpu_ref_tryget(&ctx->refs))
2754 goto out_fput;
2755
Jens Axboe6c271ce2019-01-10 11:22:30 -07002756 /*
2757 * For SQ polling, the thread will do all submissions and completions.
2758 * Just return the requested submit count, and wake the thread if
2759 * we were asked to.
2760 */
2761 if (ctx->flags & IORING_SETUP_SQPOLL) {
2762 if (flags & IORING_ENTER_SQ_WAKEUP)
2763 wake_up(&ctx->sqo_wait);
2764 submitted = to_submit;
2765 goto out_ctx;
2766 }
2767
Jens Axboe2b188cc2019-01-07 10:46:33 -07002768 ret = 0;
2769 if (to_submit) {
2770 to_submit = min(to_submit, ctx->sq_entries);
2771
2772 mutex_lock(&ctx->uring_lock);
2773 submitted = io_ring_submit(ctx, to_submit);
2774 mutex_unlock(&ctx->uring_lock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002775 }
2776 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07002777 unsigned nr_events = 0;
2778
Jens Axboe2b188cc2019-01-07 10:46:33 -07002779 min_complete = min(min_complete, ctx->cq_entries);
2780
Jens Axboedef596e2019-01-09 08:59:42 -07002781 if (ctx->flags & IORING_SETUP_IOPOLL) {
2782 mutex_lock(&ctx->uring_lock);
2783 ret = io_iopoll_check(ctx, &nr_events, min_complete);
2784 mutex_unlock(&ctx->uring_lock);
2785 } else {
2786 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
2787 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002788 }
2789
2790out_ctx:
2791 io_ring_drop_ctx_refs(ctx, 1);
2792out_fput:
2793 fdput(f);
2794 return submitted ? submitted : ret;
2795}
2796
2797static const struct file_operations io_uring_fops = {
2798 .release = io_uring_release,
2799 .mmap = io_uring_mmap,
2800 .poll = io_uring_poll,
2801 .fasync = io_uring_fasync,
2802};
2803
2804static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
2805 struct io_uring_params *p)
2806{
2807 struct io_sq_ring *sq_ring;
2808 struct io_cq_ring *cq_ring;
2809 size_t size;
2810
2811 sq_ring = io_mem_alloc(struct_size(sq_ring, array, p->sq_entries));
2812 if (!sq_ring)
2813 return -ENOMEM;
2814
2815 ctx->sq_ring = sq_ring;
2816 sq_ring->ring_mask = p->sq_entries - 1;
2817 sq_ring->ring_entries = p->sq_entries;
2818 ctx->sq_mask = sq_ring->ring_mask;
2819 ctx->sq_entries = sq_ring->ring_entries;
2820
2821 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
2822 if (size == SIZE_MAX)
2823 return -EOVERFLOW;
2824
2825 ctx->sq_sqes = io_mem_alloc(size);
Mark Rutland52e04ef2019-04-30 17:30:21 +01002826 if (!ctx->sq_sqes)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002827 return -ENOMEM;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002828
2829 cq_ring = io_mem_alloc(struct_size(cq_ring, cqes, p->cq_entries));
Mark Rutland52e04ef2019-04-30 17:30:21 +01002830 if (!cq_ring)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002831 return -ENOMEM;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002832
2833 ctx->cq_ring = cq_ring;
2834 cq_ring->ring_mask = p->cq_entries - 1;
2835 cq_ring->ring_entries = p->cq_entries;
2836 ctx->cq_mask = cq_ring->ring_mask;
2837 ctx->cq_entries = cq_ring->ring_entries;
2838 return 0;
2839}
2840
2841/*
2842 * Allocate an anonymous fd, this is what constitutes the application
2843 * visible backing of an io_uring instance. The application mmaps this
2844 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
2845 * we have to tie this fd to a socket for file garbage collection purposes.
2846 */
2847static int io_uring_get_fd(struct io_ring_ctx *ctx)
2848{
2849 struct file *file;
2850 int ret;
2851
2852#if defined(CONFIG_UNIX)
2853 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
2854 &ctx->ring_sock);
2855 if (ret)
2856 return ret;
2857#endif
2858
2859 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
2860 if (ret < 0)
2861 goto err;
2862
2863 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
2864 O_RDWR | O_CLOEXEC);
2865 if (IS_ERR(file)) {
2866 put_unused_fd(ret);
2867 ret = PTR_ERR(file);
2868 goto err;
2869 }
2870
2871#if defined(CONFIG_UNIX)
2872 ctx->ring_sock->file = file;
Jens Axboe6b063142019-01-10 22:13:58 -07002873 ctx->ring_sock->sk->sk_user_data = ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002874#endif
2875 fd_install(ret, file);
2876 return ret;
2877err:
2878#if defined(CONFIG_UNIX)
2879 sock_release(ctx->ring_sock);
2880 ctx->ring_sock = NULL;
2881#endif
2882 return ret;
2883}
2884
2885static int io_uring_create(unsigned entries, struct io_uring_params *p)
2886{
2887 struct user_struct *user = NULL;
2888 struct io_ring_ctx *ctx;
2889 bool account_mem;
2890 int ret;
2891
2892 if (!entries || entries > IORING_MAX_ENTRIES)
2893 return -EINVAL;
2894
2895 /*
2896 * Use twice as many entries for the CQ ring. It's possible for the
2897 * application to drive a higher depth than the size of the SQ ring,
2898 * since the sqes are only used at submission time. This allows for
2899 * some flexibility in overcommitting a bit.
2900 */
2901 p->sq_entries = roundup_pow_of_two(entries);
2902 p->cq_entries = 2 * p->sq_entries;
2903
2904 user = get_uid(current_user());
2905 account_mem = !capable(CAP_IPC_LOCK);
2906
2907 if (account_mem) {
2908 ret = io_account_mem(user,
2909 ring_pages(p->sq_entries, p->cq_entries));
2910 if (ret) {
2911 free_uid(user);
2912 return ret;
2913 }
2914 }
2915
2916 ctx = io_ring_ctx_alloc(p);
2917 if (!ctx) {
2918 if (account_mem)
2919 io_unaccount_mem(user, ring_pages(p->sq_entries,
2920 p->cq_entries));
2921 free_uid(user);
2922 return -ENOMEM;
2923 }
2924 ctx->compat = in_compat_syscall();
2925 ctx->account_mem = account_mem;
2926 ctx->user = user;
2927
2928 ret = io_allocate_scq_urings(ctx, p);
2929 if (ret)
2930 goto err;
2931
Jens Axboe6c271ce2019-01-10 11:22:30 -07002932 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002933 if (ret)
2934 goto err;
2935
2936 ret = io_uring_get_fd(ctx);
2937 if (ret < 0)
2938 goto err;
2939
2940 memset(&p->sq_off, 0, sizeof(p->sq_off));
2941 p->sq_off.head = offsetof(struct io_sq_ring, r.head);
2942 p->sq_off.tail = offsetof(struct io_sq_ring, r.tail);
2943 p->sq_off.ring_mask = offsetof(struct io_sq_ring, ring_mask);
2944 p->sq_off.ring_entries = offsetof(struct io_sq_ring, ring_entries);
2945 p->sq_off.flags = offsetof(struct io_sq_ring, flags);
2946 p->sq_off.dropped = offsetof(struct io_sq_ring, dropped);
2947 p->sq_off.array = offsetof(struct io_sq_ring, array);
2948
2949 memset(&p->cq_off, 0, sizeof(p->cq_off));
2950 p->cq_off.head = offsetof(struct io_cq_ring, r.head);
2951 p->cq_off.tail = offsetof(struct io_cq_ring, r.tail);
2952 p->cq_off.ring_mask = offsetof(struct io_cq_ring, ring_mask);
2953 p->cq_off.ring_entries = offsetof(struct io_cq_ring, ring_entries);
2954 p->cq_off.overflow = offsetof(struct io_cq_ring, overflow);
2955 p->cq_off.cqes = offsetof(struct io_cq_ring, cqes);
2956 return ret;
2957err:
2958 io_ring_ctx_wait_and_kill(ctx);
2959 return ret;
2960}
2961
2962/*
2963 * Sets up an aio uring context, and returns the fd. Applications asks for a
2964 * ring size, we return the actual sq/cq ring sizes (among other things) in the
2965 * params structure passed in.
2966 */
2967static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
2968{
2969 struct io_uring_params p;
2970 long ret;
2971 int i;
2972
2973 if (copy_from_user(&p, params, sizeof(p)))
2974 return -EFAULT;
2975 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
2976 if (p.resv[i])
2977 return -EINVAL;
2978 }
2979
Jens Axboe6c271ce2019-01-10 11:22:30 -07002980 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
2981 IORING_SETUP_SQ_AFF))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002982 return -EINVAL;
2983
2984 ret = io_uring_create(entries, &p);
2985 if (ret < 0)
2986 return ret;
2987
2988 if (copy_to_user(params, &p, sizeof(p)))
2989 return -EFAULT;
2990
2991 return ret;
2992}
2993
2994SYSCALL_DEFINE2(io_uring_setup, u32, entries,
2995 struct io_uring_params __user *, params)
2996{
2997 return io_uring_setup(entries, params);
2998}
2999
Jens Axboeedafcce2019-01-09 09:16:05 -07003000static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
3001 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06003002 __releases(ctx->uring_lock)
3003 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07003004{
3005 int ret;
3006
Jens Axboe35fa71a2019-04-22 10:23:23 -06003007 /*
3008 * We're inside the ring mutex, if the ref is already dying, then
3009 * someone else killed the ctx or is already going through
3010 * io_uring_register().
3011 */
3012 if (percpu_ref_is_dying(&ctx->refs))
3013 return -ENXIO;
3014
Jens Axboeedafcce2019-01-09 09:16:05 -07003015 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06003016
3017 /*
3018 * Drop uring mutex before waiting for references to exit. If another
3019 * thread is currently inside io_uring_enter() it might need to grab
3020 * the uring_lock to make progress. If we hold it here across the drain
3021 * wait, then we can deadlock. It's safe to drop the mutex here, since
3022 * no new references will come in after we've killed the percpu ref.
3023 */
3024 mutex_unlock(&ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07003025 wait_for_completion(&ctx->ctx_done);
Jens Axboeb19062a2019-04-15 10:49:38 -06003026 mutex_lock(&ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07003027
3028 switch (opcode) {
3029 case IORING_REGISTER_BUFFERS:
3030 ret = io_sqe_buffer_register(ctx, arg, nr_args);
3031 break;
3032 case IORING_UNREGISTER_BUFFERS:
3033 ret = -EINVAL;
3034 if (arg || nr_args)
3035 break;
3036 ret = io_sqe_buffer_unregister(ctx);
3037 break;
Jens Axboe6b063142019-01-10 22:13:58 -07003038 case IORING_REGISTER_FILES:
3039 ret = io_sqe_files_register(ctx, arg, nr_args);
3040 break;
3041 case IORING_UNREGISTER_FILES:
3042 ret = -EINVAL;
3043 if (arg || nr_args)
3044 break;
3045 ret = io_sqe_files_unregister(ctx);
3046 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07003047 default:
3048 ret = -EINVAL;
3049 break;
3050 }
3051
3052 /* bring the ctx back to life */
3053 reinit_completion(&ctx->ctx_done);
3054 percpu_ref_reinit(&ctx->refs);
3055 return ret;
3056}
3057
3058SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
3059 void __user *, arg, unsigned int, nr_args)
3060{
3061 struct io_ring_ctx *ctx;
3062 long ret = -EBADF;
3063 struct fd f;
3064
3065 f = fdget(fd);
3066 if (!f.file)
3067 return -EBADF;
3068
3069 ret = -EOPNOTSUPP;
3070 if (f.file->f_op != &io_uring_fops)
3071 goto out_fput;
3072
3073 ctx = f.file->private_data;
3074
3075 mutex_lock(&ctx->uring_lock);
3076 ret = __io_uring_register(ctx, opcode, arg, nr_args);
3077 mutex_unlock(&ctx->uring_lock);
3078out_fput:
3079 fdput(f);
3080 return ret;
3081}
3082
Jens Axboe2b188cc2019-01-07 10:46:33 -07003083static int __init io_uring_init(void)
3084{
3085 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
3086 return 0;
3087};
3088__initcall(io_uring_init);