blob: d91cbd53d3ca4cc21f2420217dc48e6f0b8fe1c8 [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;
Jens Axboede0617e2019-04-06 21:51:27 -0600225
226 struct list_head defer_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700227 } ____cacheline_aligned_in_smp;
228
229 /* IO offload */
230 struct workqueue_struct *sqo_wq;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700231 struct task_struct *sqo_thread; /* if using sq thread polling */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700232 struct mm_struct *sqo_mm;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700233 wait_queue_head_t sqo_wait;
234 unsigned sqo_stop;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700235
236 struct {
237 /* CQ ring */
238 struct io_cq_ring *cq_ring;
239 unsigned cached_cq_tail;
240 unsigned cq_entries;
241 unsigned cq_mask;
242 struct wait_queue_head cq_wait;
243 struct fasync_struct *cq_fasync;
Jens Axboe9b402842019-04-11 11:45:41 -0600244 struct eventfd_ctx *cq_ev_fd;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700245 } ____cacheline_aligned_in_smp;
246
Jens Axboe6b063142019-01-10 22:13:58 -0700247 /*
248 * If used, fixed file set. Writers must ensure that ->refs is dead,
249 * readers must ensure that ->refs is alive as long as the file* is
250 * used. Only updated through io_uring_register(2).
251 */
252 struct file **user_files;
253 unsigned nr_user_files;
254
Jens Axboeedafcce2019-01-09 09:16:05 -0700255 /* if used, fixed mapped user buffers */
256 unsigned nr_user_bufs;
257 struct io_mapped_ubuf *user_bufs;
258
Jens Axboe2b188cc2019-01-07 10:46:33 -0700259 struct user_struct *user;
260
261 struct completion ctx_done;
262
263 struct {
264 struct mutex uring_lock;
265 wait_queue_head_t wait;
266 } ____cacheline_aligned_in_smp;
267
268 struct {
269 spinlock_t completion_lock;
Jens Axboedef596e2019-01-09 08:59:42 -0700270 bool poll_multi_file;
271 /*
272 * ->poll_list is protected by the ctx->uring_lock for
273 * io_uring instances that don't use IORING_SETUP_SQPOLL.
274 * For SQPOLL, only the single threaded io_sq_thread() will
275 * manipulate the list, hence no extra locking is needed there.
276 */
277 struct list_head poll_list;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700278 struct list_head cancel_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700279 } ____cacheline_aligned_in_smp;
280
Jens Axboe31b51512019-01-18 22:56:34 -0700281 struct async_list pending_async[2];
282
Jens Axboe2b188cc2019-01-07 10:46:33 -0700283#if defined(CONFIG_UNIX)
284 struct socket *ring_sock;
285#endif
286};
287
288struct sqe_submit {
289 const struct io_uring_sqe *sqe;
290 unsigned short index;
291 bool has_user;
Jens Axboedef596e2019-01-09 08:59:42 -0700292 bool needs_lock;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700293 bool needs_fixed_file;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700294};
295
Jens Axboe09bb8392019-03-13 12:39:28 -0600296/*
297 * First field must be the file pointer in all the
298 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
299 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700300struct io_poll_iocb {
301 struct file *file;
302 struct wait_queue_head *head;
303 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600304 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700305 bool canceled;
306 struct wait_queue_entry wait;
307};
308
Jens Axboe09bb8392019-03-13 12:39:28 -0600309/*
310 * NOTE! Each of the iocb union members has the file pointer
311 * as the first entry in their struct definition. So you can
312 * access the file pointer through any of the sub-structs,
313 * or directly as just 'ki_filp' in this struct.
314 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700315struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700316 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600317 struct file *file;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700318 struct kiocb rw;
319 struct io_poll_iocb poll;
320 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700321
322 struct sqe_submit submit;
323
324 struct io_ring_ctx *ctx;
325 struct list_head list;
326 unsigned int flags;
Jens Axboec16361c2019-01-17 08:39:48 -0700327 refcount_t refs;
Stefan Bühler8449eed2019-04-27 20:34:19 +0200328#define REQ_F_NOWAIT 1 /* must not punt to workers */
Jens Axboedef596e2019-01-09 08:59:42 -0700329#define REQ_F_IOPOLL_COMPLETED 2 /* polled IO has completed */
Jens Axboe6b063142019-01-10 22:13:58 -0700330#define REQ_F_FIXED_FILE 4 /* ctx owns file */
Jens Axboe31b51512019-01-18 22:56:34 -0700331#define REQ_F_SEQ_PREV 8 /* sequential with previous */
Jens Axboed530a402019-03-13 12:15:01 -0600332#define REQ_F_PREPPED 16 /* prep already done */
Jens Axboede0617e2019-04-06 21:51:27 -0600333#define REQ_F_IO_DRAIN 32 /* drain existing IO first */
334#define REQ_F_IO_DRAINED 64 /* drain done */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700335 u64 user_data;
Stefan Bühler5dcf8772019-05-01 13:53:36 +0200336 u32 error; /* iopoll result from callback */
Jens Axboede0617e2019-04-06 21:51:27 -0600337 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700338
339 struct work_struct work;
340};
341
342#define IO_PLUG_THRESHOLD 2
Jens Axboedef596e2019-01-09 08:59:42 -0700343#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700344
Jens Axboe9a56a232019-01-09 09:06:50 -0700345struct io_submit_state {
346 struct blk_plug plug;
347
348 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700349 * io_kiocb alloc cache
350 */
351 void *reqs[IO_IOPOLL_BATCH];
352 unsigned int free_reqs;
353 unsigned int cur_req;
354
355 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700356 * File reference cache
357 */
358 struct file *file;
359 unsigned int fd;
360 unsigned int has_refs;
361 unsigned int used_refs;
362 unsigned int ios_left;
363};
364
Jens Axboede0617e2019-04-06 21:51:27 -0600365static void io_sq_wq_submit_work(struct work_struct *work);
366
Jens Axboe2b188cc2019-01-07 10:46:33 -0700367static struct kmem_cache *req_cachep;
368
369static const struct file_operations io_uring_fops;
370
371struct sock *io_uring_get_socket(struct file *file)
372{
373#if defined(CONFIG_UNIX)
374 if (file->f_op == &io_uring_fops) {
375 struct io_ring_ctx *ctx = file->private_data;
376
377 return ctx->ring_sock->sk;
378 }
379#endif
380 return NULL;
381}
382EXPORT_SYMBOL(io_uring_get_socket);
383
384static void io_ring_ctx_ref_free(struct percpu_ref *ref)
385{
386 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
387
388 complete(&ctx->ctx_done);
389}
390
391static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
392{
393 struct io_ring_ctx *ctx;
Jens Axboe31b51512019-01-18 22:56:34 -0700394 int i;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700395
396 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
397 if (!ctx)
398 return NULL;
399
400 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free, 0, GFP_KERNEL)) {
401 kfree(ctx);
402 return NULL;
403 }
404
405 ctx->flags = p->flags;
406 init_waitqueue_head(&ctx->cq_wait);
407 init_completion(&ctx->ctx_done);
408 mutex_init(&ctx->uring_lock);
409 init_waitqueue_head(&ctx->wait);
Jens Axboe31b51512019-01-18 22:56:34 -0700410 for (i = 0; i < ARRAY_SIZE(ctx->pending_async); i++) {
411 spin_lock_init(&ctx->pending_async[i].lock);
412 INIT_LIST_HEAD(&ctx->pending_async[i].list);
413 atomic_set(&ctx->pending_async[i].cnt, 0);
414 }
Jens Axboe2b188cc2019-01-07 10:46:33 -0700415 spin_lock_init(&ctx->completion_lock);
Jens Axboedef596e2019-01-09 08:59:42 -0700416 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboe221c5eb2019-01-17 09:41:58 -0700417 INIT_LIST_HEAD(&ctx->cancel_list);
Jens Axboede0617e2019-04-06 21:51:27 -0600418 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700419 return ctx;
420}
421
Jens Axboede0617e2019-04-06 21:51:27 -0600422static inline bool io_sequence_defer(struct io_ring_ctx *ctx,
423 struct io_kiocb *req)
424{
425 if ((req->flags & (REQ_F_IO_DRAIN|REQ_F_IO_DRAINED)) != REQ_F_IO_DRAIN)
426 return false;
427
428 return req->sequence > ctx->cached_cq_tail + ctx->sq_ring->dropped;
429}
430
431static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
432{
433 struct io_kiocb *req;
434
435 if (list_empty(&ctx->defer_list))
436 return NULL;
437
438 req = list_first_entry(&ctx->defer_list, struct io_kiocb, list);
439 if (!io_sequence_defer(ctx, req)) {
440 list_del_init(&req->list);
441 return req;
442 }
443
444 return NULL;
445}
446
447static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700448{
449 struct io_cq_ring *ring = ctx->cq_ring;
450
451 if (ctx->cached_cq_tail != READ_ONCE(ring->r.tail)) {
452 /* order cqe stores with ring update */
453 smp_store_release(&ring->r.tail, ctx->cached_cq_tail);
454
Jens Axboe2b188cc2019-01-07 10:46:33 -0700455 if (wq_has_sleeper(&ctx->cq_wait)) {
456 wake_up_interruptible(&ctx->cq_wait);
457 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
458 }
459 }
460}
461
Jens Axboede0617e2019-04-06 21:51:27 -0600462static void io_commit_cqring(struct io_ring_ctx *ctx)
463{
464 struct io_kiocb *req;
465
466 __io_commit_cqring(ctx);
467
468 while ((req = io_get_deferred_req(ctx)) != NULL) {
469 req->flags |= REQ_F_IO_DRAINED;
470 queue_work(ctx->sqo_wq, &req->work);
471 }
472}
473
Jens Axboe2b188cc2019-01-07 10:46:33 -0700474static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
475{
476 struct io_cq_ring *ring = ctx->cq_ring;
477 unsigned tail;
478
479 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +0200480 /*
481 * writes to the cq entry need to come after reading head; the
482 * control dependency is enough as we're using WRITE_ONCE to
483 * fill the cq entry
484 */
Jens Axboe74f464e2019-04-17 08:57:48 -0600485 if (tail - READ_ONCE(ring->r.head) == ring->ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700486 return NULL;
487
488 ctx->cached_cq_tail++;
489 return &ring->cqes[tail & ctx->cq_mask];
490}
491
492static void io_cqring_fill_event(struct io_ring_ctx *ctx, u64 ki_user_data,
493 long res, unsigned ev_flags)
494{
495 struct io_uring_cqe *cqe;
496
497 /*
498 * If we can't get a cq entry, userspace overflowed the
499 * submission (by quite a lot). Increment the overflow count in
500 * the ring.
501 */
502 cqe = io_get_cqring(ctx);
503 if (cqe) {
504 WRITE_ONCE(cqe->user_data, ki_user_data);
505 WRITE_ONCE(cqe->res, res);
506 WRITE_ONCE(cqe->flags, ev_flags);
507 } else {
508 unsigned overflow = READ_ONCE(ctx->cq_ring->overflow);
509
510 WRITE_ONCE(ctx->cq_ring->overflow, overflow + 1);
511 }
512}
513
Jens Axboe8c838782019-03-12 15:48:16 -0600514static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
515{
516 if (waitqueue_active(&ctx->wait))
517 wake_up(&ctx->wait);
518 if (waitqueue_active(&ctx->sqo_wait))
519 wake_up(&ctx->sqo_wait);
Jens Axboe9b402842019-04-11 11:45:41 -0600520 if (ctx->cq_ev_fd)
521 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -0600522}
523
524static void io_cqring_add_event(struct io_ring_ctx *ctx, u64 user_data,
Jens Axboe2b188cc2019-01-07 10:46:33 -0700525 long res, unsigned ev_flags)
526{
527 unsigned long flags;
528
529 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe8c838782019-03-12 15:48:16 -0600530 io_cqring_fill_event(ctx, user_data, res, ev_flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700531 io_commit_cqring(ctx);
532 spin_unlock_irqrestore(&ctx->completion_lock, flags);
533
Jens Axboe8c838782019-03-12 15:48:16 -0600534 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700535}
536
537static void io_ring_drop_ctx_refs(struct io_ring_ctx *ctx, unsigned refs)
538{
539 percpu_ref_put_many(&ctx->refs, refs);
540
541 if (waitqueue_active(&ctx->wait))
542 wake_up(&ctx->wait);
543}
544
Jens Axboe2579f912019-01-09 09:10:43 -0700545static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
546 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700547{
Jens Axboefd6fab22019-03-14 16:30:06 -0600548 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700549 struct io_kiocb *req;
550
551 if (!percpu_ref_tryget(&ctx->refs))
552 return NULL;
553
Jens Axboe2579f912019-01-09 09:10:43 -0700554 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -0600555 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -0700556 if (unlikely(!req))
557 goto out;
558 } else if (!state->free_reqs) {
559 size_t sz;
560 int ret;
561
562 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -0600563 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
564
565 /*
566 * Bulk alloc is all-or-nothing. If we fail to get a batch,
567 * retry single alloc to be on the safe side.
568 */
569 if (unlikely(ret <= 0)) {
570 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
571 if (!state->reqs[0])
572 goto out;
573 ret = 1;
574 }
Jens Axboe2579f912019-01-09 09:10:43 -0700575 state->free_reqs = ret - 1;
576 state->cur_req = 1;
577 req = state->reqs[0];
578 } else {
579 req = state->reqs[state->cur_req];
580 state->free_reqs--;
581 state->cur_req++;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700582 }
583
Jens Axboe2579f912019-01-09 09:10:43 -0700584 req->ctx = ctx;
585 req->flags = 0;
Jens Axboee65ef562019-03-12 10:16:44 -0600586 /* one is dropped after submission, the other at completion */
587 refcount_set(&req->refs, 2);
Jens Axboe2579f912019-01-09 09:10:43 -0700588 return req;
589out:
Jens Axboe2b188cc2019-01-07 10:46:33 -0700590 io_ring_drop_ctx_refs(ctx, 1);
591 return NULL;
592}
593
Jens Axboedef596e2019-01-09 08:59:42 -0700594static void io_free_req_many(struct io_ring_ctx *ctx, void **reqs, int *nr)
595{
596 if (*nr) {
597 kmem_cache_free_bulk(req_cachep, *nr, reqs);
598 io_ring_drop_ctx_refs(ctx, *nr);
599 *nr = 0;
600 }
601}
602
Jens Axboe2b188cc2019-01-07 10:46:33 -0700603static void io_free_req(struct io_kiocb *req)
604{
Jens Axboe09bb8392019-03-13 12:39:28 -0600605 if (req->file && !(req->flags & REQ_F_FIXED_FILE))
606 fput(req->file);
Jens Axboee65ef562019-03-12 10:16:44 -0600607 io_ring_drop_ctx_refs(req->ctx, 1);
608 kmem_cache_free(req_cachep, req);
609}
610
611static void io_put_req(struct io_kiocb *req)
612{
613 if (refcount_dec_and_test(&req->refs))
614 io_free_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700615}
616
Jens Axboedef596e2019-01-09 08:59:42 -0700617/*
618 * Find and free completed poll iocbs
619 */
620static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
621 struct list_head *done)
622{
623 void *reqs[IO_IOPOLL_BATCH];
624 struct io_kiocb *req;
Jens Axboe09bb8392019-03-13 12:39:28 -0600625 int to_free;
Jens Axboedef596e2019-01-09 08:59:42 -0700626
Jens Axboe09bb8392019-03-13 12:39:28 -0600627 to_free = 0;
Jens Axboedef596e2019-01-09 08:59:42 -0700628 while (!list_empty(done)) {
629 req = list_first_entry(done, struct io_kiocb, list);
630 list_del(&req->list);
631
632 io_cqring_fill_event(ctx, req->user_data, req->error, 0);
Jens Axboedef596e2019-01-09 08:59:42 -0700633 (*nr_events)++;
634
Jens Axboe09bb8392019-03-13 12:39:28 -0600635 if (refcount_dec_and_test(&req->refs)) {
636 /* If we're not using fixed files, we have to pair the
637 * completion part with the file put. Use regular
638 * completions for those, only batch free for fixed
639 * file.
640 */
641 if (req->flags & REQ_F_FIXED_FILE) {
642 reqs[to_free++] = req;
643 if (to_free == ARRAY_SIZE(reqs))
644 io_free_req_many(ctx, reqs, &to_free);
Jens Axboe6b063142019-01-10 22:13:58 -0700645 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -0600646 io_free_req(req);
Jens Axboe6b063142019-01-10 22:13:58 -0700647 }
Jens Axboe9a56a232019-01-09 09:06:50 -0700648 }
Jens Axboedef596e2019-01-09 08:59:42 -0700649 }
Jens Axboedef596e2019-01-09 08:59:42 -0700650
Jens Axboe09bb8392019-03-13 12:39:28 -0600651 io_commit_cqring(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -0700652 io_free_req_many(ctx, reqs, &to_free);
653}
654
655static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
656 long min)
657{
658 struct io_kiocb *req, *tmp;
659 LIST_HEAD(done);
660 bool spin;
661 int ret;
662
663 /*
664 * Only spin for completions if we don't have multiple devices hanging
665 * off our complete list, and we're under the requested amount.
666 */
667 spin = !ctx->poll_multi_file && *nr_events < min;
668
669 ret = 0;
670 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
671 struct kiocb *kiocb = &req->rw;
672
673 /*
674 * Move completed entries to our local list. If we find a
675 * request that requires polling, break out and complete
676 * the done list first, if we have entries there.
677 */
678 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
679 list_move_tail(&req->list, &done);
680 continue;
681 }
682 if (!list_empty(&done))
683 break;
684
685 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
686 if (ret < 0)
687 break;
688
689 if (ret && spin)
690 spin = false;
691 ret = 0;
692 }
693
694 if (!list_empty(&done))
695 io_iopoll_complete(ctx, nr_events, &done);
696
697 return ret;
698}
699
700/*
701 * Poll for a mininum of 'min' events. Note that if min == 0 we consider that a
702 * non-spinning poll check - we'll still enter the driver poll loop, but only
703 * as a non-spinning completion check.
704 */
705static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
706 long min)
707{
708 while (!list_empty(&ctx->poll_list)) {
709 int ret;
710
711 ret = io_do_iopoll(ctx, nr_events, min);
712 if (ret < 0)
713 return ret;
714 if (!min || *nr_events >= min)
715 return 0;
716 }
717
718 return 1;
719}
720
721/*
722 * We can't just wait for polled events to come to us, we have to actively
723 * find and complete them.
724 */
725static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
726{
727 if (!(ctx->flags & IORING_SETUP_IOPOLL))
728 return;
729
730 mutex_lock(&ctx->uring_lock);
731 while (!list_empty(&ctx->poll_list)) {
732 unsigned int nr_events = 0;
733
734 io_iopoll_getevents(ctx, &nr_events, 1);
735 }
736 mutex_unlock(&ctx->uring_lock);
737}
738
739static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
740 long min)
741{
742 int ret = 0;
743
744 do {
745 int tmin = 0;
746
747 if (*nr_events < min)
748 tmin = min - *nr_events;
749
750 ret = io_iopoll_getevents(ctx, nr_events, tmin);
751 if (ret <= 0)
752 break;
753 ret = 0;
754 } while (min && !*nr_events && !need_resched());
755
756 return ret;
757}
758
Jens Axboe2b188cc2019-01-07 10:46:33 -0700759static void kiocb_end_write(struct kiocb *kiocb)
760{
761 if (kiocb->ki_flags & IOCB_WRITE) {
762 struct inode *inode = file_inode(kiocb->ki_filp);
763
764 /*
765 * Tell lockdep we inherited freeze protection from submission
766 * thread.
767 */
768 if (S_ISREG(inode->i_mode))
769 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
770 file_end_write(kiocb->ki_filp);
771 }
772}
773
774static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
775{
776 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
777
778 kiocb_end_write(kiocb);
779
Jens Axboe2b188cc2019-01-07 10:46:33 -0700780 io_cqring_add_event(req->ctx, req->user_data, res, 0);
Jens Axboee65ef562019-03-12 10:16:44 -0600781 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700782}
783
Jens Axboedef596e2019-01-09 08:59:42 -0700784static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
785{
786 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
787
788 kiocb_end_write(kiocb);
789
790 req->error = res;
791 if (res != -EAGAIN)
792 req->flags |= REQ_F_IOPOLL_COMPLETED;
793}
794
795/*
796 * After the iocb has been issued, it's safe to be found on the poll list.
797 * Adding the kiocb to the list AFTER submission ensures that we don't
798 * find it from a io_iopoll_getevents() thread before the issuer is done
799 * accessing the kiocb cookie.
800 */
801static void io_iopoll_req_issued(struct io_kiocb *req)
802{
803 struct io_ring_ctx *ctx = req->ctx;
804
805 /*
806 * Track whether we have multiple files in our lists. This will impact
807 * how we do polling eventually, not spinning if we're on potentially
808 * different devices.
809 */
810 if (list_empty(&ctx->poll_list)) {
811 ctx->poll_multi_file = false;
812 } else if (!ctx->poll_multi_file) {
813 struct io_kiocb *list_req;
814
815 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
816 list);
817 if (list_req->rw.ki_filp != req->rw.ki_filp)
818 ctx->poll_multi_file = true;
819 }
820
821 /*
822 * For fast devices, IO may have already completed. If it has, add
823 * it to the front so we find it first.
824 */
825 if (req->flags & REQ_F_IOPOLL_COMPLETED)
826 list_add(&req->list, &ctx->poll_list);
827 else
828 list_add_tail(&req->list, &ctx->poll_list);
829}
830
Jens Axboe3d6770f2019-04-13 11:50:54 -0600831static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -0700832{
Jens Axboe3d6770f2019-04-13 11:50:54 -0600833 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -0700834 int diff = state->has_refs - state->used_refs;
835
836 if (diff)
837 fput_many(state->file, diff);
838 state->file = NULL;
839 }
840}
841
842/*
843 * Get as many references to a file as we have IOs left in this submission,
844 * assuming most submissions are for one file, or at least that each file
845 * has more than one submission.
846 */
847static struct file *io_file_get(struct io_submit_state *state, int fd)
848{
849 if (!state)
850 return fget(fd);
851
852 if (state->file) {
853 if (state->fd == fd) {
854 state->used_refs++;
855 state->ios_left--;
856 return state->file;
857 }
Jens Axboe3d6770f2019-04-13 11:50:54 -0600858 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -0700859 }
860 state->file = fget_many(fd, state->ios_left);
861 if (!state->file)
862 return NULL;
863
864 state->fd = fd;
865 state->has_refs = state->ios_left;
866 state->used_refs = 1;
867 state->ios_left--;
868 return state->file;
869}
870
Jens Axboe2b188cc2019-01-07 10:46:33 -0700871/*
872 * If we tracked the file through the SCM inflight mechanism, we could support
873 * any file. For now, just ensure that anything potentially problematic is done
874 * inline.
875 */
876static bool io_file_supports_async(struct file *file)
877{
878 umode_t mode = file_inode(file)->i_mode;
879
880 if (S_ISBLK(mode) || S_ISCHR(mode))
881 return true;
882 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
883 return true;
884
885 return false;
886}
887
Jens Axboe6c271ce2019-01-10 11:22:30 -0700888static int io_prep_rw(struct io_kiocb *req, const struct sqe_submit *s,
Jens Axboe8358e3a2019-04-23 08:17:58 -0600889 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700890{
Jens Axboe6c271ce2019-01-10 11:22:30 -0700891 const struct io_uring_sqe *sqe = s->sqe;
Jens Axboedef596e2019-01-09 08:59:42 -0700892 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700893 struct kiocb *kiocb = &req->rw;
Jens Axboe09bb8392019-03-13 12:39:28 -0600894 unsigned ioprio;
895 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700896
Jens Axboe09bb8392019-03-13 12:39:28 -0600897 if (!req->file)
898 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700899 /* For -EAGAIN retry, everything is already prepped */
Jens Axboed530a402019-03-13 12:15:01 -0600900 if (req->flags & REQ_F_PREPPED)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700901 return 0;
902
Jens Axboe09bb8392019-03-13 12:39:28 -0600903 if (force_nonblock && !io_file_supports_async(req->file))
904 force_nonblock = false;
Jens Axboe6b063142019-01-10 22:13:58 -0700905
Jens Axboe2b188cc2019-01-07 10:46:33 -0700906 kiocb->ki_pos = READ_ONCE(sqe->off);
907 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
908 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
909
910 ioprio = READ_ONCE(sqe->ioprio);
911 if (ioprio) {
912 ret = ioprio_check_cap(ioprio);
913 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -0600914 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700915
916 kiocb->ki_ioprio = ioprio;
917 } else
918 kiocb->ki_ioprio = get_current_ioprio();
919
920 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
921 if (unlikely(ret))
Jens Axboe09bb8392019-03-13 12:39:28 -0600922 return ret;
Stefan Bühler8449eed2019-04-27 20:34:19 +0200923
924 /* don't allow async punt if RWF_NOWAIT was requested */
925 if (kiocb->ki_flags & IOCB_NOWAIT)
926 req->flags |= REQ_F_NOWAIT;
927
928 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700929 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +0200930
Jens Axboedef596e2019-01-09 08:59:42 -0700931 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -0700932 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
933 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -0600934 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700935
Jens Axboedef596e2019-01-09 08:59:42 -0700936 req->error = 0;
937 kiocb->ki_flags |= IOCB_HIPRI;
938 kiocb->ki_complete = io_complete_rw_iopoll;
939 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -0600940 if (kiocb->ki_flags & IOCB_HIPRI)
941 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -0700942 kiocb->ki_complete = io_complete_rw;
943 }
Jens Axboed530a402019-03-13 12:15:01 -0600944 req->flags |= REQ_F_PREPPED;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700945 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700946}
947
948static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
949{
950 switch (ret) {
951 case -EIOCBQUEUED:
952 break;
953 case -ERESTARTSYS:
954 case -ERESTARTNOINTR:
955 case -ERESTARTNOHAND:
956 case -ERESTART_RESTARTBLOCK:
957 /*
958 * We can't just restart the syscall, since previously
959 * submitted sqes may already be in progress. Just fail this
960 * IO with EINTR.
961 */
962 ret = -EINTR;
963 /* fall through */
964 default:
965 kiocb->ki_complete(kiocb, ret, 0);
966 }
967}
968
Jens Axboeedafcce2019-01-09 09:16:05 -0700969static int io_import_fixed(struct io_ring_ctx *ctx, int rw,
970 const struct io_uring_sqe *sqe,
971 struct iov_iter *iter)
972{
973 size_t len = READ_ONCE(sqe->len);
974 struct io_mapped_ubuf *imu;
975 unsigned index, buf_index;
976 size_t offset;
977 u64 buf_addr;
978
979 /* attempt to use fixed buffers without having provided iovecs */
980 if (unlikely(!ctx->user_bufs))
981 return -EFAULT;
982
983 buf_index = READ_ONCE(sqe->buf_index);
984 if (unlikely(buf_index >= ctx->nr_user_bufs))
985 return -EFAULT;
986
987 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
988 imu = &ctx->user_bufs[index];
989 buf_addr = READ_ONCE(sqe->addr);
990
991 /* overflow */
992 if (buf_addr + len < buf_addr)
993 return -EFAULT;
994 /* not inside the mapped region */
995 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
996 return -EFAULT;
997
998 /*
999 * May not be a start of buffer, set size appropriately
1000 * and advance us to the beginning.
1001 */
1002 offset = buf_addr - imu->ubuf;
1003 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
1004 if (offset)
1005 iov_iter_advance(iter, offset);
Jens Axboe875f1d02019-02-27 13:05:25 -07001006
1007 /* don't drop a reference to these pages */
1008 iter->type |= ITER_BVEC_FLAG_NO_REF;
Jens Axboeedafcce2019-01-09 09:16:05 -07001009 return 0;
1010}
1011
Jens Axboe2b188cc2019-01-07 10:46:33 -07001012static int io_import_iovec(struct io_ring_ctx *ctx, int rw,
1013 const struct sqe_submit *s, struct iovec **iovec,
1014 struct iov_iter *iter)
1015{
1016 const struct io_uring_sqe *sqe = s->sqe;
1017 void __user *buf = u64_to_user_ptr(READ_ONCE(sqe->addr));
1018 size_t sqe_len = READ_ONCE(sqe->len);
Jens Axboeedafcce2019-01-09 09:16:05 -07001019 u8 opcode;
1020
1021 /*
1022 * We're reading ->opcode for the second time, but the first read
1023 * doesn't care whether it's _FIXED or not, so it doesn't matter
1024 * whether ->opcode changes concurrently. The first read does care
1025 * about whether it is a READ or a WRITE, so we don't trust this read
1026 * for that purpose and instead let the caller pass in the read/write
1027 * flag.
1028 */
1029 opcode = READ_ONCE(sqe->opcode);
1030 if (opcode == IORING_OP_READ_FIXED ||
1031 opcode == IORING_OP_WRITE_FIXED) {
Jens Axboee0c5c572019-03-12 10:18:47 -06001032 int ret = io_import_fixed(ctx, rw, sqe, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07001033 *iovec = NULL;
1034 return ret;
1035 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001036
1037 if (!s->has_user)
1038 return -EFAULT;
1039
1040#ifdef CONFIG_COMPAT
1041 if (ctx->compat)
1042 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
1043 iovec, iter);
1044#endif
1045
1046 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
1047}
1048
Jens Axboe31b51512019-01-18 22:56:34 -07001049/*
1050 * Make a note of the last file/offset/direction we punted to async
1051 * context. We'll use this information to see if we can piggy back a
1052 * sequential request onto the previous one, if it's still hasn't been
1053 * completed by the async worker.
1054 */
1055static void io_async_list_note(int rw, struct io_kiocb *req, size_t len)
1056{
1057 struct async_list *async_list = &req->ctx->pending_async[rw];
1058 struct kiocb *kiocb = &req->rw;
1059 struct file *filp = kiocb->ki_filp;
1060 off_t io_end = kiocb->ki_pos + len;
1061
1062 if (filp == async_list->file && kiocb->ki_pos == async_list->io_end) {
1063 unsigned long max_pages;
1064
1065 /* Use 8x RA size as a decent limiter for both reads/writes */
1066 max_pages = filp->f_ra.ra_pages;
1067 if (!max_pages)
Nikolay Borisovb5420232019-03-11 23:28:13 -07001068 max_pages = VM_READAHEAD_PAGES;
Jens Axboe31b51512019-01-18 22:56:34 -07001069 max_pages *= 8;
1070
1071 /* If max pages are exceeded, reset the state */
1072 len >>= PAGE_SHIFT;
1073 if (async_list->io_pages + len <= max_pages) {
1074 req->flags |= REQ_F_SEQ_PREV;
1075 async_list->io_pages += len;
1076 } else {
1077 io_end = 0;
1078 async_list->io_pages = 0;
1079 }
1080 }
1081
1082 /* New file? Reset state. */
1083 if (async_list->file != filp) {
1084 async_list->io_pages = 0;
1085 async_list->file = filp;
1086 }
1087 async_list->io_end = io_end;
1088}
1089
Jens Axboee0c5c572019-03-12 10:18:47 -06001090static int io_read(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;
1103 file = kiocb->ki_filp;
1104
Jens Axboe2b188cc2019-01-07 10:46:33 -07001105 if (unlikely(!(file->f_mode & FMODE_READ)))
Jens Axboe09bb8392019-03-13 12:39:28 -06001106 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001107 if (unlikely(!file->f_op->read_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, READ, 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 ret = rw_verify_area(READ, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001116 if (!ret) {
1117 ssize_t ret2;
1118
1119 /* Catch -EAGAIN return for forced non-blocking submission */
1120 ret2 = call_read_iter(file, kiocb, &iter);
Jens Axboe31b51512019-01-18 22:56:34 -07001121 if (!force_nonblock || ret2 != -EAGAIN) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001122 io_rw_done(kiocb, ret2);
Jens Axboe31b51512019-01-18 22:56:34 -07001123 } else {
1124 /*
1125 * If ->needs_lock is true, we're already in async
1126 * context.
1127 */
1128 if (!s->needs_lock)
1129 io_async_list_note(READ, req, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001130 ret = -EAGAIN;
Jens Axboe31b51512019-01-18 22:56:34 -07001131 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001132 }
1133 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001134 return ret;
1135}
1136
Jens Axboee0c5c572019-03-12 10:18:47 -06001137static int io_write(struct io_kiocb *req, const struct sqe_submit *s,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001138 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001139{
1140 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1141 struct kiocb *kiocb = &req->rw;
1142 struct iov_iter iter;
1143 struct file *file;
Jens Axboe31b51512019-01-18 22:56:34 -07001144 size_t iov_count;
Jens Axboee0c5c572019-03-12 10:18:47 -06001145 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001146
Jens Axboe8358e3a2019-04-23 08:17:58 -06001147 ret = io_prep_rw(req, s, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001148 if (ret)
1149 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001150
Jens Axboe2b188cc2019-01-07 10:46:33 -07001151 file = kiocb->ki_filp;
1152 if (unlikely(!(file->f_mode & FMODE_WRITE)))
Jens Axboe09bb8392019-03-13 12:39:28 -06001153 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001154 if (unlikely(!file->f_op->write_iter))
Jens Axboe09bb8392019-03-13 12:39:28 -06001155 return -EINVAL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001156
1157 ret = io_import_iovec(req->ctx, WRITE, s, &iovec, &iter);
1158 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06001159 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001160
Jens Axboe31b51512019-01-18 22:56:34 -07001161 iov_count = iov_iter_count(&iter);
1162
1163 ret = -EAGAIN;
1164 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT)) {
1165 /* If ->needs_lock is true, we're already in async context. */
1166 if (!s->needs_lock)
1167 io_async_list_note(WRITE, req, iov_count);
1168 goto out_free;
1169 }
1170
1171 ret = rw_verify_area(WRITE, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001172 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01001173 ssize_t ret2;
1174
Jens Axboe2b188cc2019-01-07 10:46:33 -07001175 /*
1176 * Open-code file_start_write here to grab freeze protection,
1177 * which will be released by another thread in
1178 * io_complete_rw(). Fool lockdep by telling it the lock got
1179 * released so that it doesn't complain about the held lock when
1180 * we return to userspace.
1181 */
1182 if (S_ISREG(file_inode(file)->i_mode)) {
1183 __sb_start_write(file_inode(file)->i_sb,
1184 SB_FREEZE_WRITE, true);
1185 __sb_writers_release(file_inode(file)->i_sb,
1186 SB_FREEZE_WRITE);
1187 }
1188 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01001189
1190 ret2 = call_write_iter(file, kiocb, &iter);
1191 if (!force_nonblock || ret2 != -EAGAIN) {
1192 io_rw_done(kiocb, ret2);
1193 } else {
1194 /*
1195 * If ->needs_lock is true, we're already in async
1196 * context.
1197 */
1198 if (!s->needs_lock)
1199 io_async_list_note(WRITE, req, iov_count);
1200 ret = -EAGAIN;
1201 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001202 }
Jens Axboe31b51512019-01-18 22:56:34 -07001203out_free:
Jens Axboe2b188cc2019-01-07 10:46:33 -07001204 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001205 return ret;
1206}
1207
1208/*
1209 * IORING_OP_NOP just posts a completion event, nothing else.
1210 */
1211static int io_nop(struct io_kiocb *req, u64 user_data)
1212{
1213 struct io_ring_ctx *ctx = req->ctx;
1214 long err = 0;
1215
Jens Axboedef596e2019-01-09 08:59:42 -07001216 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1217 return -EINVAL;
1218
Jens Axboe2b188cc2019-01-07 10:46:33 -07001219 io_cqring_add_event(ctx, user_data, err, 0);
Jens Axboee65ef562019-03-12 10:16:44 -06001220 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001221 return 0;
1222}
1223
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001224static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1225{
Jens Axboe6b063142019-01-10 22:13:58 -07001226 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001227
Jens Axboe09bb8392019-03-13 12:39:28 -06001228 if (!req->file)
1229 return -EBADF;
Jens Axboed530a402019-03-13 12:15:01 -06001230 /* Prep already done (EAGAIN retry) */
1231 if (req->flags & REQ_F_PREPPED)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001232 return 0;
1233
Jens Axboe6b063142019-01-10 22:13:58 -07001234 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07001235 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07001236 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001237 return -EINVAL;
1238
Jens Axboed530a402019-03-13 12:15:01 -06001239 req->flags |= REQ_F_PREPPED;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001240 return 0;
1241}
1242
1243static int io_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1244 bool force_nonblock)
1245{
1246 loff_t sqe_off = READ_ONCE(sqe->off);
1247 loff_t sqe_len = READ_ONCE(sqe->len);
1248 loff_t end = sqe_off + sqe_len;
1249 unsigned fsync_flags;
1250 int ret;
1251
1252 fsync_flags = READ_ONCE(sqe->fsync_flags);
1253 if (unlikely(fsync_flags & ~IORING_FSYNC_DATASYNC))
1254 return -EINVAL;
1255
1256 ret = io_prep_fsync(req, sqe);
1257 if (ret)
1258 return ret;
1259
1260 /* fsync always requires a blocking context */
1261 if (force_nonblock)
1262 return -EAGAIN;
1263
1264 ret = vfs_fsync_range(req->rw.ki_filp, sqe_off,
1265 end > 0 ? end : LLONG_MAX,
1266 fsync_flags & IORING_FSYNC_DATASYNC);
1267
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001268 io_cqring_add_event(req->ctx, sqe->user_data, ret, 0);
Jens Axboee65ef562019-03-12 10:16:44 -06001269 io_put_req(req);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001270 return 0;
1271}
1272
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001273static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1274{
1275 struct io_ring_ctx *ctx = req->ctx;
1276 int ret = 0;
1277
1278 if (!req->file)
1279 return -EBADF;
1280 /* Prep already done (EAGAIN retry) */
1281 if (req->flags & REQ_F_PREPPED)
1282 return 0;
1283
1284 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1285 return -EINVAL;
1286 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
1287 return -EINVAL;
1288
1289 req->flags |= REQ_F_PREPPED;
1290 return ret;
1291}
1292
1293static int io_sync_file_range(struct io_kiocb *req,
1294 const struct io_uring_sqe *sqe,
1295 bool force_nonblock)
1296{
1297 loff_t sqe_off;
1298 loff_t sqe_len;
1299 unsigned flags;
1300 int ret;
1301
1302 ret = io_prep_sfr(req, sqe);
1303 if (ret)
1304 return ret;
1305
1306 /* sync_file_range always requires a blocking context */
1307 if (force_nonblock)
1308 return -EAGAIN;
1309
1310 sqe_off = READ_ONCE(sqe->off);
1311 sqe_len = READ_ONCE(sqe->len);
1312 flags = READ_ONCE(sqe->sync_range_flags);
1313
1314 ret = sync_file_range(req->rw.ki_filp, sqe_off, sqe_len, flags);
1315
1316 io_cqring_add_event(req->ctx, sqe->user_data, ret, 0);
1317 io_put_req(req);
1318 return 0;
1319}
1320
Jens Axboe221c5eb2019-01-17 09:41:58 -07001321static void io_poll_remove_one(struct io_kiocb *req)
1322{
1323 struct io_poll_iocb *poll = &req->poll;
1324
1325 spin_lock(&poll->head->lock);
1326 WRITE_ONCE(poll->canceled, true);
1327 if (!list_empty(&poll->wait.entry)) {
1328 list_del_init(&poll->wait.entry);
1329 queue_work(req->ctx->sqo_wq, &req->work);
1330 }
1331 spin_unlock(&poll->head->lock);
1332
1333 list_del_init(&req->list);
1334}
1335
1336static void io_poll_remove_all(struct io_ring_ctx *ctx)
1337{
1338 struct io_kiocb *req;
1339
1340 spin_lock_irq(&ctx->completion_lock);
1341 while (!list_empty(&ctx->cancel_list)) {
1342 req = list_first_entry(&ctx->cancel_list, struct io_kiocb,list);
1343 io_poll_remove_one(req);
1344 }
1345 spin_unlock_irq(&ctx->completion_lock);
1346}
1347
1348/*
1349 * Find a running poll command that matches one specified in sqe->addr,
1350 * and remove it if found.
1351 */
1352static int io_poll_remove(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1353{
1354 struct io_ring_ctx *ctx = req->ctx;
1355 struct io_kiocb *poll_req, *next;
1356 int ret = -ENOENT;
1357
1358 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1359 return -EINVAL;
1360 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
1361 sqe->poll_events)
1362 return -EINVAL;
1363
1364 spin_lock_irq(&ctx->completion_lock);
1365 list_for_each_entry_safe(poll_req, next, &ctx->cancel_list, list) {
1366 if (READ_ONCE(sqe->addr) == poll_req->user_data) {
1367 io_poll_remove_one(poll_req);
1368 ret = 0;
1369 break;
1370 }
1371 }
1372 spin_unlock_irq(&ctx->completion_lock);
1373
1374 io_cqring_add_event(req->ctx, sqe->user_data, ret, 0);
Jens Axboee65ef562019-03-12 10:16:44 -06001375 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001376 return 0;
1377}
1378
Jens Axboe8c838782019-03-12 15:48:16 -06001379static void io_poll_complete(struct io_ring_ctx *ctx, struct io_kiocb *req,
1380 __poll_t mask)
Jens Axboe221c5eb2019-01-17 09:41:58 -07001381{
Jens Axboe8c838782019-03-12 15:48:16 -06001382 req->poll.done = true;
1383 io_cqring_fill_event(ctx, req->user_data, mangle_poll(mask), 0);
1384 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001385}
1386
1387static void io_poll_complete_work(struct work_struct *work)
1388{
1389 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
1390 struct io_poll_iocb *poll = &req->poll;
1391 struct poll_table_struct pt = { ._key = poll->events };
1392 struct io_ring_ctx *ctx = req->ctx;
1393 __poll_t mask = 0;
1394
1395 if (!READ_ONCE(poll->canceled))
1396 mask = vfs_poll(poll->file, &pt) & poll->events;
1397
1398 /*
1399 * Note that ->ki_cancel callers also delete iocb from active_reqs after
1400 * calling ->ki_cancel. We need the ctx_lock roundtrip here to
1401 * synchronize with them. In the cancellation case the list_del_init
1402 * itself is not actually needed, but harmless so we keep it in to
1403 * avoid further branches in the fast path.
1404 */
1405 spin_lock_irq(&ctx->completion_lock);
1406 if (!mask && !READ_ONCE(poll->canceled)) {
1407 add_wait_queue(poll->head, &poll->wait);
1408 spin_unlock_irq(&ctx->completion_lock);
1409 return;
1410 }
1411 list_del_init(&req->list);
Jens Axboe8c838782019-03-12 15:48:16 -06001412 io_poll_complete(ctx, req, mask);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001413 spin_unlock_irq(&ctx->completion_lock);
1414
Jens Axboe8c838782019-03-12 15:48:16 -06001415 io_cqring_ev_posted(ctx);
1416 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001417}
1418
1419static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
1420 void *key)
1421{
1422 struct io_poll_iocb *poll = container_of(wait, struct io_poll_iocb,
1423 wait);
1424 struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
1425 struct io_ring_ctx *ctx = req->ctx;
1426 __poll_t mask = key_to_poll(key);
Jens Axboe8c838782019-03-12 15:48:16 -06001427 unsigned long flags;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001428
1429 /* for instances that support it check for an event match first: */
Jens Axboe8c838782019-03-12 15:48:16 -06001430 if (mask && !(mask & poll->events))
1431 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001432
1433 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06001434
1435 if (mask && spin_trylock_irqsave(&ctx->completion_lock, flags)) {
1436 list_del(&req->list);
1437 io_poll_complete(ctx, req, mask);
1438 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1439
1440 io_cqring_ev_posted(ctx);
1441 io_put_req(req);
1442 } else {
1443 queue_work(ctx->sqo_wq, &req->work);
1444 }
1445
Jens Axboe221c5eb2019-01-17 09:41:58 -07001446 return 1;
1447}
1448
1449struct io_poll_table {
1450 struct poll_table_struct pt;
1451 struct io_kiocb *req;
1452 int error;
1453};
1454
1455static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
1456 struct poll_table_struct *p)
1457{
1458 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
1459
1460 if (unlikely(pt->req->poll.head)) {
1461 pt->error = -EINVAL;
1462 return;
1463 }
1464
1465 pt->error = 0;
1466 pt->req->poll.head = head;
1467 add_wait_queue(head, &pt->req->poll.wait);
1468}
1469
1470static int io_poll_add(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1471{
1472 struct io_poll_iocb *poll = &req->poll;
1473 struct io_ring_ctx *ctx = req->ctx;
1474 struct io_poll_table ipt;
Jens Axboe8c838782019-03-12 15:48:16 -06001475 bool cancel = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001476 __poll_t mask;
1477 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001478
1479 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1480 return -EINVAL;
1481 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
1482 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06001483 if (!poll->file)
1484 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001485
1486 INIT_WORK(&req->work, io_poll_complete_work);
1487 events = READ_ONCE(sqe->poll_events);
1488 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
1489
Jens Axboe221c5eb2019-01-17 09:41:58 -07001490 poll->head = NULL;
Jens Axboe8c838782019-03-12 15:48:16 -06001491 poll->done = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001492 poll->canceled = false;
1493
1494 ipt.pt._qproc = io_poll_queue_proc;
1495 ipt.pt._key = poll->events;
1496 ipt.req = req;
1497 ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
1498
1499 /* initialized the list so that we can do list_empty checks */
1500 INIT_LIST_HEAD(&poll->wait.entry);
1501 init_waitqueue_func_entry(&poll->wait, io_poll_wake);
1502
Jens Axboe221c5eb2019-01-17 09:41:58 -07001503 mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001504
1505 spin_lock_irq(&ctx->completion_lock);
Jens Axboe8c838782019-03-12 15:48:16 -06001506 if (likely(poll->head)) {
1507 spin_lock(&poll->head->lock);
1508 if (unlikely(list_empty(&poll->wait.entry))) {
1509 if (ipt.error)
1510 cancel = true;
1511 ipt.error = 0;
1512 mask = 0;
1513 }
1514 if (mask || ipt.error)
1515 list_del_init(&poll->wait.entry);
1516 else if (cancel)
1517 WRITE_ONCE(poll->canceled, true);
1518 else if (!poll->done) /* actually waiting for an event */
1519 list_add_tail(&req->list, &ctx->cancel_list);
1520 spin_unlock(&poll->head->lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001521 }
Jens Axboe8c838782019-03-12 15:48:16 -06001522 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06001523 ipt.error = 0;
1524 io_poll_complete(ctx, req, mask);
1525 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07001526 spin_unlock_irq(&ctx->completion_lock);
1527
Jens Axboe8c838782019-03-12 15:48:16 -06001528 if (mask) {
1529 io_cqring_ev_posted(ctx);
Jens Axboee65ef562019-03-12 10:16:44 -06001530 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001531 }
Jens Axboe8c838782019-03-12 15:48:16 -06001532 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001533}
1534
Jens Axboede0617e2019-04-06 21:51:27 -06001535static int io_req_defer(struct io_ring_ctx *ctx, struct io_kiocb *req,
1536 const struct io_uring_sqe *sqe)
1537{
1538 struct io_uring_sqe *sqe_copy;
1539
1540 if (!io_sequence_defer(ctx, req) && list_empty(&ctx->defer_list))
1541 return 0;
1542
1543 sqe_copy = kmalloc(sizeof(*sqe_copy), GFP_KERNEL);
1544 if (!sqe_copy)
1545 return -EAGAIN;
1546
1547 spin_lock_irq(&ctx->completion_lock);
1548 if (!io_sequence_defer(ctx, req) && list_empty(&ctx->defer_list)) {
1549 spin_unlock_irq(&ctx->completion_lock);
1550 kfree(sqe_copy);
1551 return 0;
1552 }
1553
1554 memcpy(sqe_copy, sqe, sizeof(*sqe_copy));
1555 req->submit.sqe = sqe_copy;
1556
1557 INIT_WORK(&req->work, io_sq_wq_submit_work);
1558 list_add_tail(&req->list, &ctx->defer_list);
1559 spin_unlock_irq(&ctx->completion_lock);
1560 return -EIOCBQUEUED;
1561}
1562
Jens Axboe2b188cc2019-01-07 10:46:33 -07001563static int __io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001564 const struct sqe_submit *s, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001565{
Jens Axboee0c5c572019-03-12 10:18:47 -06001566 int ret, opcode;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001567
1568 if (unlikely(s->index >= ctx->sq_entries))
1569 return -EINVAL;
1570 req->user_data = READ_ONCE(s->sqe->user_data);
1571
1572 opcode = READ_ONCE(s->sqe->opcode);
1573 switch (opcode) {
1574 case IORING_OP_NOP:
1575 ret = io_nop(req, req->user_data);
1576 break;
1577 case IORING_OP_READV:
Jens Axboeedafcce2019-01-09 09:16:05 -07001578 if (unlikely(s->sqe->buf_index))
1579 return -EINVAL;
Jens Axboe8358e3a2019-04-23 08:17:58 -06001580 ret = io_read(req, s, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001581 break;
1582 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07001583 if (unlikely(s->sqe->buf_index))
1584 return -EINVAL;
Jens Axboe8358e3a2019-04-23 08:17:58 -06001585 ret = io_write(req, s, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07001586 break;
1587 case IORING_OP_READ_FIXED:
Jens Axboe8358e3a2019-04-23 08:17:58 -06001588 ret = io_read(req, s, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07001589 break;
1590 case IORING_OP_WRITE_FIXED:
Jens Axboe8358e3a2019-04-23 08:17:58 -06001591 ret = io_write(req, s, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001592 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001593 case IORING_OP_FSYNC:
1594 ret = io_fsync(req, s->sqe, force_nonblock);
1595 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001596 case IORING_OP_POLL_ADD:
1597 ret = io_poll_add(req, s->sqe);
1598 break;
1599 case IORING_OP_POLL_REMOVE:
1600 ret = io_poll_remove(req, s->sqe);
1601 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001602 case IORING_OP_SYNC_FILE_RANGE:
1603 ret = io_sync_file_range(req, s->sqe, force_nonblock);
1604 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001605 default:
1606 ret = -EINVAL;
1607 break;
1608 }
1609
Jens Axboedef596e2019-01-09 08:59:42 -07001610 if (ret)
1611 return ret;
1612
1613 if (ctx->flags & IORING_SETUP_IOPOLL) {
1614 if (req->error == -EAGAIN)
1615 return -EAGAIN;
1616
1617 /* workqueue context doesn't hold uring_lock, grab it now */
1618 if (s->needs_lock)
1619 mutex_lock(&ctx->uring_lock);
1620 io_iopoll_req_issued(req);
1621 if (s->needs_lock)
1622 mutex_unlock(&ctx->uring_lock);
1623 }
1624
1625 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001626}
1627
Jens Axboe31b51512019-01-18 22:56:34 -07001628static struct async_list *io_async_list_from_sqe(struct io_ring_ctx *ctx,
1629 const struct io_uring_sqe *sqe)
1630{
1631 switch (sqe->opcode) {
1632 case IORING_OP_READV:
1633 case IORING_OP_READ_FIXED:
1634 return &ctx->pending_async[READ];
1635 case IORING_OP_WRITEV:
1636 case IORING_OP_WRITE_FIXED:
1637 return &ctx->pending_async[WRITE];
1638 default:
1639 return NULL;
1640 }
1641}
1642
Jens Axboeedafcce2019-01-09 09:16:05 -07001643static inline bool io_sqe_needs_user(const struct io_uring_sqe *sqe)
1644{
1645 u8 opcode = READ_ONCE(sqe->opcode);
1646
1647 return !(opcode == IORING_OP_READ_FIXED ||
1648 opcode == IORING_OP_WRITE_FIXED);
1649}
1650
Jens Axboe2b188cc2019-01-07 10:46:33 -07001651static void io_sq_wq_submit_work(struct work_struct *work)
1652{
1653 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001654 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe31b51512019-01-18 22:56:34 -07001655 struct mm_struct *cur_mm = NULL;
1656 struct async_list *async_list;
1657 LIST_HEAD(req_list);
Jens Axboeedafcce2019-01-09 09:16:05 -07001658 mm_segment_t old_fs;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001659 int ret;
1660
Jens Axboe31b51512019-01-18 22:56:34 -07001661 async_list = io_async_list_from_sqe(ctx, req->submit.sqe);
1662restart:
1663 do {
1664 struct sqe_submit *s = &req->submit;
1665 const struct io_uring_sqe *sqe = s->sqe;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001666
Stefan Bühler8449eed2019-04-27 20:34:19 +02001667 /* Ensure we clear previously set non-block flag */
Jens Axboe31b51512019-01-18 22:56:34 -07001668 req->rw.ki_flags &= ~IOCB_NOWAIT;
1669
1670 ret = 0;
1671 if (io_sqe_needs_user(sqe) && !cur_mm) {
1672 if (!mmget_not_zero(ctx->sqo_mm)) {
1673 ret = -EFAULT;
1674 } else {
1675 cur_mm = ctx->sqo_mm;
1676 use_mm(cur_mm);
1677 old_fs = get_fs();
1678 set_fs(USER_DS);
1679 }
1680 }
1681
1682 if (!ret) {
1683 s->has_user = cur_mm != NULL;
1684 s->needs_lock = true;
1685 do {
Jens Axboe8358e3a2019-04-23 08:17:58 -06001686 ret = __io_submit_sqe(ctx, req, s, false);
Jens Axboe31b51512019-01-18 22:56:34 -07001687 /*
1688 * We can get EAGAIN for polled IO even though
1689 * we're forcing a sync submission from here,
1690 * since we can't wait for request slots on the
1691 * block side.
1692 */
1693 if (ret != -EAGAIN)
1694 break;
1695 cond_resched();
1696 } while (1);
1697 }
Jens Axboe817869d2019-04-30 14:44:05 -06001698
1699 /* drop submission reference */
1700 io_put_req(req);
1701
Jens Axboe31b51512019-01-18 22:56:34 -07001702 if (ret) {
1703 io_cqring_add_event(ctx, sqe->user_data, ret, 0);
Jens Axboee65ef562019-03-12 10:16:44 -06001704 io_put_req(req);
Jens Axboe31b51512019-01-18 22:56:34 -07001705 }
1706
1707 /* async context always use a copy of the sqe */
1708 kfree(sqe);
1709
1710 if (!async_list)
1711 break;
1712 if (!list_empty(&req_list)) {
1713 req = list_first_entry(&req_list, struct io_kiocb,
1714 list);
1715 list_del(&req->list);
1716 continue;
1717 }
1718 if (list_empty(&async_list->list))
1719 break;
1720
1721 req = NULL;
1722 spin_lock(&async_list->lock);
1723 if (list_empty(&async_list->list)) {
1724 spin_unlock(&async_list->lock);
1725 break;
1726 }
1727 list_splice_init(&async_list->list, &req_list);
1728 spin_unlock(&async_list->lock);
1729
1730 req = list_first_entry(&req_list, struct io_kiocb, list);
1731 list_del(&req->list);
1732 } while (req);
Jens Axboeedafcce2019-01-09 09:16:05 -07001733
1734 /*
Jens Axboe31b51512019-01-18 22:56:34 -07001735 * Rare case of racing with a submitter. If we find the count has
1736 * dropped to zero AND we have pending work items, then restart
1737 * the processing. This is a tiny race window.
Jens Axboeedafcce2019-01-09 09:16:05 -07001738 */
Jens Axboe31b51512019-01-18 22:56:34 -07001739 if (async_list) {
1740 ret = atomic_dec_return(&async_list->cnt);
1741 while (!ret && !list_empty(&async_list->list)) {
1742 spin_lock(&async_list->lock);
1743 atomic_inc(&async_list->cnt);
1744 list_splice_init(&async_list->list, &req_list);
1745 spin_unlock(&async_list->lock);
1746
1747 if (!list_empty(&req_list)) {
1748 req = list_first_entry(&req_list,
1749 struct io_kiocb, list);
1750 list_del(&req->list);
1751 goto restart;
1752 }
1753 ret = atomic_dec_return(&async_list->cnt);
Jens Axboeedafcce2019-01-09 09:16:05 -07001754 }
Jens Axboeedafcce2019-01-09 09:16:05 -07001755 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001756
Jens Axboe31b51512019-01-18 22:56:34 -07001757 if (cur_mm) {
Jens Axboeedafcce2019-01-09 09:16:05 -07001758 set_fs(old_fs);
Jens Axboe31b51512019-01-18 22:56:34 -07001759 unuse_mm(cur_mm);
1760 mmput(cur_mm);
Jens Axboeedafcce2019-01-09 09:16:05 -07001761 }
Jens Axboe31b51512019-01-18 22:56:34 -07001762}
Jens Axboe2b188cc2019-01-07 10:46:33 -07001763
Jens Axboe31b51512019-01-18 22:56:34 -07001764/*
1765 * See if we can piggy back onto previously submitted work, that is still
1766 * running. We currently only allow this if the new request is sequential
1767 * to the previous one we punted.
1768 */
1769static bool io_add_to_prev_work(struct async_list *list, struct io_kiocb *req)
1770{
1771 bool ret = false;
1772
1773 if (!list)
1774 return false;
1775 if (!(req->flags & REQ_F_SEQ_PREV))
1776 return false;
1777 if (!atomic_read(&list->cnt))
1778 return false;
1779
1780 ret = true;
1781 spin_lock(&list->lock);
1782 list_add_tail(&req->list, &list->list);
1783 if (!atomic_read(&list->cnt)) {
1784 list_del_init(&req->list);
1785 ret = false;
1786 }
1787 spin_unlock(&list->lock);
1788 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001789}
1790
Jens Axboe09bb8392019-03-13 12:39:28 -06001791static bool io_op_needs_file(const struct io_uring_sqe *sqe)
1792{
1793 int op = READ_ONCE(sqe->opcode);
1794
1795 switch (op) {
1796 case IORING_OP_NOP:
1797 case IORING_OP_POLL_REMOVE:
1798 return false;
1799 default:
1800 return true;
1801 }
1802}
1803
1804static int io_req_set_file(struct io_ring_ctx *ctx, const struct sqe_submit *s,
1805 struct io_submit_state *state, struct io_kiocb *req)
1806{
1807 unsigned flags;
1808 int fd;
1809
1810 flags = READ_ONCE(s->sqe->flags);
1811 fd = READ_ONCE(s->sqe->fd);
1812
Jens Axboede0617e2019-04-06 21:51:27 -06001813 if (flags & IOSQE_IO_DRAIN) {
1814 req->flags |= REQ_F_IO_DRAIN;
1815 req->sequence = ctx->cached_sq_head - 1;
1816 }
1817
Jens Axboe09bb8392019-03-13 12:39:28 -06001818 if (!io_op_needs_file(s->sqe)) {
1819 req->file = NULL;
1820 return 0;
1821 }
1822
1823 if (flags & IOSQE_FIXED_FILE) {
1824 if (unlikely(!ctx->user_files ||
1825 (unsigned) fd >= ctx->nr_user_files))
1826 return -EBADF;
1827 req->file = ctx->user_files[fd];
1828 req->flags |= REQ_F_FIXED_FILE;
1829 } else {
1830 if (s->needs_fixed_file)
1831 return -EBADF;
1832 req->file = io_file_get(state, fd);
1833 if (unlikely(!req->file))
1834 return -EBADF;
1835 }
1836
1837 return 0;
1838}
1839
Jens Axboe9a56a232019-01-09 09:06:50 -07001840static int io_submit_sqe(struct io_ring_ctx *ctx, struct sqe_submit *s,
1841 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001842{
1843 struct io_kiocb *req;
Jens Axboee0c5c572019-03-12 10:18:47 -06001844 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001845
1846 /* enforce forwards compatibility on users */
Jens Axboede0617e2019-04-06 21:51:27 -06001847 if (unlikely(s->sqe->flags & ~(IOSQE_FIXED_FILE | IOSQE_IO_DRAIN)))
Jens Axboe2b188cc2019-01-07 10:46:33 -07001848 return -EINVAL;
1849
Jens Axboe2579f912019-01-09 09:10:43 -07001850 req = io_get_req(ctx, state);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001851 if (unlikely(!req))
1852 return -EAGAIN;
1853
Jens Axboe09bb8392019-03-13 12:39:28 -06001854 ret = io_req_set_file(ctx, s, state, req);
1855 if (unlikely(ret))
1856 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001857
Jens Axboede0617e2019-04-06 21:51:27 -06001858 ret = io_req_defer(ctx, req, s->sqe);
1859 if (ret) {
1860 if (ret == -EIOCBQUEUED)
1861 ret = 0;
1862 return ret;
1863 }
1864
Jens Axboe8358e3a2019-04-23 08:17:58 -06001865 ret = __io_submit_sqe(ctx, req, s, true);
Stefan Bühler8449eed2019-04-27 20:34:19 +02001866 if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001867 struct io_uring_sqe *sqe_copy;
1868
1869 sqe_copy = kmalloc(sizeof(*sqe_copy), GFP_KERNEL);
1870 if (sqe_copy) {
Jens Axboe31b51512019-01-18 22:56:34 -07001871 struct async_list *list;
1872
Jens Axboe2b188cc2019-01-07 10:46:33 -07001873 memcpy(sqe_copy, s->sqe, sizeof(*sqe_copy));
1874 s->sqe = sqe_copy;
1875
1876 memcpy(&req->submit, s, sizeof(*s));
Jens Axboe31b51512019-01-18 22:56:34 -07001877 list = io_async_list_from_sqe(ctx, s->sqe);
1878 if (!io_add_to_prev_work(list, req)) {
1879 if (list)
1880 atomic_inc(&list->cnt);
1881 INIT_WORK(&req->work, io_sq_wq_submit_work);
1882 queue_work(ctx->sqo_wq, &req->work);
1883 }
Jens Axboee65ef562019-03-12 10:16:44 -06001884
1885 /*
1886 * Queued up for async execution, worker will release
1887 * submit reference when the iocb is actually
1888 * submitted.
1889 */
1890 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001891 }
1892 }
Jens Axboee65ef562019-03-12 10:16:44 -06001893
Jens Axboe09bb8392019-03-13 12:39:28 -06001894out:
Jens Axboee65ef562019-03-12 10:16:44 -06001895 /* drop submission reference */
1896 io_put_req(req);
1897
1898 /* and drop final reference, if we failed */
Jens Axboe2b188cc2019-01-07 10:46:33 -07001899 if (ret)
Jens Axboee65ef562019-03-12 10:16:44 -06001900 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001901
1902 return ret;
1903}
1904
Jens Axboe9a56a232019-01-09 09:06:50 -07001905/*
1906 * Batched submission is done, ensure local IO is flushed out.
1907 */
1908static void io_submit_state_end(struct io_submit_state *state)
1909{
1910 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06001911 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07001912 if (state->free_reqs)
1913 kmem_cache_free_bulk(req_cachep, state->free_reqs,
1914 &state->reqs[state->cur_req]);
Jens Axboe9a56a232019-01-09 09:06:50 -07001915}
1916
1917/*
1918 * Start submission side cache.
1919 */
1920static void io_submit_state_start(struct io_submit_state *state,
1921 struct io_ring_ctx *ctx, unsigned max_ios)
1922{
1923 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07001924 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07001925 state->file = NULL;
1926 state->ios_left = max_ios;
1927}
1928
Jens Axboe2b188cc2019-01-07 10:46:33 -07001929static void io_commit_sqring(struct io_ring_ctx *ctx)
1930{
1931 struct io_sq_ring *ring = ctx->sq_ring;
1932
1933 if (ctx->cached_sq_head != READ_ONCE(ring->r.head)) {
1934 /*
1935 * Ensure any loads from the SQEs are done at this point,
1936 * since once we write the new head, the application could
1937 * write new data to them.
1938 */
1939 smp_store_release(&ring->r.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001940 }
1941}
1942
1943/*
Jens Axboe2b188cc2019-01-07 10:46:33 -07001944 * Fetch an sqe, if one is available. Note that s->sqe will point to memory
1945 * that is mapped by userspace. This means that care needs to be taken to
1946 * ensure that reads are stable, as we cannot rely on userspace always
1947 * being a good citizen. If members of the sqe are validated and then later
1948 * used, it's important that those reads are done through READ_ONCE() to
1949 * prevent a re-load down the line.
1950 */
1951static bool io_get_sqring(struct io_ring_ctx *ctx, struct sqe_submit *s)
1952{
1953 struct io_sq_ring *ring = ctx->sq_ring;
1954 unsigned head;
1955
1956 /*
1957 * The cached sq head (or cq tail) serves two purposes:
1958 *
1959 * 1) allows us to batch the cost of updating the user visible
1960 * head updates.
1961 * 2) allows the kernel side to track the head on its own, even
1962 * though the application is the one updating it.
1963 */
1964 head = ctx->cached_sq_head;
Stefan Bühlere523a292019-04-19 11:57:44 +02001965 /* make sure SQ entry isn't read before tail */
1966 if (head == smp_load_acquire(&ring->r.tail))
Jens Axboe2b188cc2019-01-07 10:46:33 -07001967 return false;
1968
1969 head = READ_ONCE(ring->array[head & ctx->sq_mask]);
1970 if (head < ctx->sq_entries) {
1971 s->index = head;
1972 s->sqe = &ctx->sq_sqes[head];
1973 ctx->cached_sq_head++;
1974 return true;
1975 }
1976
1977 /* drop invalid entries */
1978 ctx->cached_sq_head++;
1979 ring->dropped++;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001980 return false;
1981}
1982
Jens Axboe6c271ce2019-01-10 11:22:30 -07001983static int io_submit_sqes(struct io_ring_ctx *ctx, struct sqe_submit *sqes,
1984 unsigned int nr, bool has_user, bool mm_fault)
1985{
1986 struct io_submit_state state, *statep = NULL;
1987 int ret, i, submitted = 0;
1988
1989 if (nr > IO_PLUG_THRESHOLD) {
1990 io_submit_state_start(&state, ctx, nr);
1991 statep = &state;
1992 }
1993
1994 for (i = 0; i < nr; i++) {
1995 if (unlikely(mm_fault)) {
1996 ret = -EFAULT;
1997 } else {
1998 sqes[i].has_user = has_user;
1999 sqes[i].needs_lock = true;
2000 sqes[i].needs_fixed_file = true;
2001 ret = io_submit_sqe(ctx, &sqes[i], statep);
2002 }
2003 if (!ret) {
2004 submitted++;
2005 continue;
2006 }
2007
2008 io_cqring_add_event(ctx, sqes[i].sqe->user_data, ret, 0);
2009 }
2010
2011 if (statep)
2012 io_submit_state_end(&state);
2013
2014 return submitted;
2015}
2016
2017static int io_sq_thread(void *data)
2018{
2019 struct sqe_submit sqes[IO_IOPOLL_BATCH];
2020 struct io_ring_ctx *ctx = data;
2021 struct mm_struct *cur_mm = NULL;
2022 mm_segment_t old_fs;
2023 DEFINE_WAIT(wait);
2024 unsigned inflight;
2025 unsigned long timeout;
2026
2027 old_fs = get_fs();
2028 set_fs(USER_DS);
2029
2030 timeout = inflight = 0;
2031 while (!kthread_should_stop() && !ctx->sqo_stop) {
2032 bool all_fixed, mm_fault = false;
2033 int i;
2034
2035 if (inflight) {
2036 unsigned nr_events = 0;
2037
2038 if (ctx->flags & IORING_SETUP_IOPOLL) {
2039 /*
2040 * We disallow the app entering submit/complete
2041 * with polling, but we still need to lock the
2042 * ring to prevent racing with polled issue
2043 * that got punted to a workqueue.
2044 */
2045 mutex_lock(&ctx->uring_lock);
2046 io_iopoll_check(ctx, &nr_events, 0);
2047 mutex_unlock(&ctx->uring_lock);
2048 } else {
2049 /*
2050 * Normal IO, just pretend everything completed.
2051 * We don't have to poll completions for that.
2052 */
2053 nr_events = inflight;
2054 }
2055
2056 inflight -= nr_events;
2057 if (!inflight)
2058 timeout = jiffies + ctx->sq_thread_idle;
2059 }
2060
2061 if (!io_get_sqring(ctx, &sqes[0])) {
2062 /*
2063 * We're polling. If we're within the defined idle
2064 * period, then let us spin without work before going
2065 * to sleep.
2066 */
2067 if (inflight || !time_after(jiffies, timeout)) {
2068 cpu_relax();
2069 continue;
2070 }
2071
2072 /*
2073 * Drop cur_mm before scheduling, we can't hold it for
2074 * long periods (or over schedule()). Do this before
2075 * adding ourselves to the waitqueue, as the unuse/drop
2076 * may sleep.
2077 */
2078 if (cur_mm) {
2079 unuse_mm(cur_mm);
2080 mmput(cur_mm);
2081 cur_mm = NULL;
2082 }
2083
2084 prepare_to_wait(&ctx->sqo_wait, &wait,
2085 TASK_INTERRUPTIBLE);
2086
2087 /* Tell userspace we may need a wakeup call */
2088 ctx->sq_ring->flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02002089 /* make sure to read SQ tail after writing flags */
2090 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07002091
2092 if (!io_get_sqring(ctx, &sqes[0])) {
2093 if (kthread_should_stop()) {
2094 finish_wait(&ctx->sqo_wait, &wait);
2095 break;
2096 }
2097 if (signal_pending(current))
2098 flush_signals(current);
2099 schedule();
2100 finish_wait(&ctx->sqo_wait, &wait);
2101
2102 ctx->sq_ring->flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002103 continue;
2104 }
2105 finish_wait(&ctx->sqo_wait, &wait);
2106
2107 ctx->sq_ring->flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002108 }
2109
2110 i = 0;
2111 all_fixed = true;
2112 do {
2113 if (all_fixed && io_sqe_needs_user(sqes[i].sqe))
2114 all_fixed = false;
2115
2116 i++;
2117 if (i == ARRAY_SIZE(sqes))
2118 break;
2119 } while (io_get_sqring(ctx, &sqes[i]));
2120
2121 /* Unless all new commands are FIXED regions, grab mm */
2122 if (!all_fixed && !cur_mm) {
2123 mm_fault = !mmget_not_zero(ctx->sqo_mm);
2124 if (!mm_fault) {
2125 use_mm(ctx->sqo_mm);
2126 cur_mm = ctx->sqo_mm;
2127 }
2128 }
2129
2130 inflight += io_submit_sqes(ctx, sqes, i, cur_mm != NULL,
2131 mm_fault);
2132
2133 /* Commit SQ ring head once we've consumed all SQEs */
2134 io_commit_sqring(ctx);
2135 }
2136
2137 set_fs(old_fs);
2138 if (cur_mm) {
2139 unuse_mm(cur_mm);
2140 mmput(cur_mm);
2141 }
Jens Axboe06058632019-04-13 09:26:03 -06002142
2143 if (kthread_should_park())
2144 kthread_parkme();
2145
Jens Axboe6c271ce2019-01-10 11:22:30 -07002146 return 0;
2147}
2148
Jens Axboe2b188cc2019-01-07 10:46:33 -07002149static int io_ring_submit(struct io_ring_ctx *ctx, unsigned int to_submit)
2150{
Jens Axboe9a56a232019-01-09 09:06:50 -07002151 struct io_submit_state state, *statep = NULL;
Jens Axboe5c8b0b52019-04-30 10:16:07 -06002152 int i, submit = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002153
Jens Axboe9a56a232019-01-09 09:06:50 -07002154 if (to_submit > IO_PLUG_THRESHOLD) {
2155 io_submit_state_start(&state, ctx, to_submit);
2156 statep = &state;
2157 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002158
2159 for (i = 0; i < to_submit; i++) {
2160 struct sqe_submit s;
Jens Axboe5c8b0b52019-04-30 10:16:07 -06002161 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002162
2163 if (!io_get_sqring(ctx, &s))
2164 break;
2165
2166 s.has_user = true;
Jens Axboedef596e2019-01-09 08:59:42 -07002167 s.needs_lock = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002168 s.needs_fixed_file = false;
Jens Axboe5c8b0b52019-04-30 10:16:07 -06002169 submit++;
Jens Axboedef596e2019-01-09 08:59:42 -07002170
Jens Axboe9a56a232019-01-09 09:06:50 -07002171 ret = io_submit_sqe(ctx, &s, statep);
Jens Axboe5c8b0b52019-04-30 10:16:07 -06002172 if (ret)
2173 io_cqring_add_event(ctx, s.sqe->user_data, ret, 0);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002174 }
2175 io_commit_sqring(ctx);
2176
Jens Axboe9a56a232019-01-09 09:06:50 -07002177 if (statep)
2178 io_submit_state_end(statep);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002179
Jens Axboe5c8b0b52019-04-30 10:16:07 -06002180 return submit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002181}
2182
2183static unsigned io_cqring_events(struct io_cq_ring *ring)
2184{
2185 return READ_ONCE(ring->r.tail) - READ_ONCE(ring->r.head);
2186}
2187
2188/*
2189 * Wait until events become available, if we don't already have some. The
2190 * application must reap them itself, as they reside on the shared cq ring.
2191 */
2192static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
2193 const sigset_t __user *sig, size_t sigsz)
2194{
2195 struct io_cq_ring *ring = ctx->cq_ring;
2196 sigset_t ksigmask, sigsaved;
2197 DEFINE_WAIT(wait);
2198 int ret;
2199
2200 /* See comment at the top of this file */
2201 smp_rmb();
2202 if (io_cqring_events(ring) >= min_events)
2203 return 0;
2204
2205 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01002206#ifdef CONFIG_COMPAT
2207 if (in_compat_syscall())
2208 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
2209 &ksigmask, &sigsaved, sigsz);
2210 else
2211#endif
2212 ret = set_user_sigmask(sig, &ksigmask,
2213 &sigsaved, sigsz);
2214
Jens Axboe2b188cc2019-01-07 10:46:33 -07002215 if (ret)
2216 return ret;
2217 }
2218
2219 do {
2220 prepare_to_wait(&ctx->wait, &wait, TASK_INTERRUPTIBLE);
2221
2222 ret = 0;
2223 /* See comment at the top of this file */
2224 smp_rmb();
2225 if (io_cqring_events(ring) >= min_events)
2226 break;
2227
2228 schedule();
2229
2230 ret = -EINTR;
2231 if (signal_pending(current))
2232 break;
2233 } while (1);
2234
2235 finish_wait(&ctx->wait, &wait);
2236
2237 if (sig)
2238 restore_user_sigmask(sig, &sigsaved);
2239
2240 return READ_ONCE(ring->r.head) == READ_ONCE(ring->r.tail) ? ret : 0;
2241}
2242
Jens Axboe6b063142019-01-10 22:13:58 -07002243static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
2244{
2245#if defined(CONFIG_UNIX)
2246 if (ctx->ring_sock) {
2247 struct sock *sock = ctx->ring_sock->sk;
2248 struct sk_buff *skb;
2249
2250 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
2251 kfree_skb(skb);
2252 }
2253#else
2254 int i;
2255
2256 for (i = 0; i < ctx->nr_user_files; i++)
2257 fput(ctx->user_files[i]);
2258#endif
2259}
2260
2261static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
2262{
2263 if (!ctx->user_files)
2264 return -ENXIO;
2265
2266 __io_sqe_files_unregister(ctx);
2267 kfree(ctx->user_files);
2268 ctx->user_files = NULL;
2269 ctx->nr_user_files = 0;
2270 return 0;
2271}
2272
Jens Axboe6c271ce2019-01-10 11:22:30 -07002273static void io_sq_thread_stop(struct io_ring_ctx *ctx)
2274{
2275 if (ctx->sqo_thread) {
2276 ctx->sqo_stop = 1;
2277 mb();
Jens Axboe06058632019-04-13 09:26:03 -06002278 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07002279 kthread_stop(ctx->sqo_thread);
2280 ctx->sqo_thread = NULL;
2281 }
2282}
2283
Jens Axboe6b063142019-01-10 22:13:58 -07002284static void io_finish_async(struct io_ring_ctx *ctx)
2285{
Jens Axboe6c271ce2019-01-10 11:22:30 -07002286 io_sq_thread_stop(ctx);
2287
Jens Axboe6b063142019-01-10 22:13:58 -07002288 if (ctx->sqo_wq) {
2289 destroy_workqueue(ctx->sqo_wq);
2290 ctx->sqo_wq = NULL;
2291 }
2292}
2293
2294#if defined(CONFIG_UNIX)
2295static void io_destruct_skb(struct sk_buff *skb)
2296{
2297 struct io_ring_ctx *ctx = skb->sk->sk_user_data;
2298
2299 io_finish_async(ctx);
2300 unix_destruct_scm(skb);
2301}
2302
2303/*
2304 * Ensure the UNIX gc is aware of our file set, so we are certain that
2305 * the io_uring can be safely unregistered on process exit, even if we have
2306 * loops in the file referencing.
2307 */
2308static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
2309{
2310 struct sock *sk = ctx->ring_sock->sk;
2311 struct scm_fp_list *fpl;
2312 struct sk_buff *skb;
2313 int i;
2314
2315 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
2316 unsigned long inflight = ctx->user->unix_inflight + nr;
2317
2318 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
2319 return -EMFILE;
2320 }
2321
2322 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
2323 if (!fpl)
2324 return -ENOMEM;
2325
2326 skb = alloc_skb(0, GFP_KERNEL);
2327 if (!skb) {
2328 kfree(fpl);
2329 return -ENOMEM;
2330 }
2331
2332 skb->sk = sk;
2333 skb->destructor = io_destruct_skb;
2334
2335 fpl->user = get_uid(ctx->user);
2336 for (i = 0; i < nr; i++) {
2337 fpl->fp[i] = get_file(ctx->user_files[i + offset]);
2338 unix_inflight(fpl->user, fpl->fp[i]);
2339 }
2340
2341 fpl->max = fpl->count = nr;
2342 UNIXCB(skb).fp = fpl;
2343 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
2344 skb_queue_head(&sk->sk_receive_queue, skb);
2345
2346 for (i = 0; i < nr; i++)
2347 fput(fpl->fp[i]);
2348
2349 return 0;
2350}
2351
2352/*
2353 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
2354 * causes regular reference counting to break down. We rely on the UNIX
2355 * garbage collection to take care of this problem for us.
2356 */
2357static int io_sqe_files_scm(struct io_ring_ctx *ctx)
2358{
2359 unsigned left, total;
2360 int ret = 0;
2361
2362 total = 0;
2363 left = ctx->nr_user_files;
2364 while (left) {
2365 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
2366 int ret;
2367
2368 ret = __io_sqe_files_scm(ctx, this_files, total);
2369 if (ret)
2370 break;
2371 left -= this_files;
2372 total += this_files;
2373 }
2374
2375 if (!ret)
2376 return 0;
2377
2378 while (total < ctx->nr_user_files) {
2379 fput(ctx->user_files[total]);
2380 total++;
2381 }
2382
2383 return ret;
2384}
2385#else
2386static int io_sqe_files_scm(struct io_ring_ctx *ctx)
2387{
2388 return 0;
2389}
2390#endif
2391
2392static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
2393 unsigned nr_args)
2394{
2395 __s32 __user *fds = (__s32 __user *) arg;
2396 int fd, ret = 0;
2397 unsigned i;
2398
2399 if (ctx->user_files)
2400 return -EBUSY;
2401 if (!nr_args)
2402 return -EINVAL;
2403 if (nr_args > IORING_MAX_FIXED_FILES)
2404 return -EMFILE;
2405
2406 ctx->user_files = kcalloc(nr_args, sizeof(struct file *), GFP_KERNEL);
2407 if (!ctx->user_files)
2408 return -ENOMEM;
2409
2410 for (i = 0; i < nr_args; i++) {
2411 ret = -EFAULT;
2412 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
2413 break;
2414
2415 ctx->user_files[i] = fget(fd);
2416
2417 ret = -EBADF;
2418 if (!ctx->user_files[i])
2419 break;
2420 /*
2421 * Don't allow io_uring instances to be registered. If UNIX
2422 * isn't enabled, then this causes a reference cycle and this
2423 * instance can never get freed. If UNIX is enabled we'll
2424 * handle it just fine, but there's still no point in allowing
2425 * a ring fd as it doesn't support regular read/write anyway.
2426 */
2427 if (ctx->user_files[i]->f_op == &io_uring_fops) {
2428 fput(ctx->user_files[i]);
2429 break;
2430 }
2431 ctx->nr_user_files++;
2432 ret = 0;
2433 }
2434
2435 if (ret) {
2436 for (i = 0; i < ctx->nr_user_files; i++)
2437 fput(ctx->user_files[i]);
2438
2439 kfree(ctx->user_files);
Jens Axboe25adf502019-04-03 09:52:40 -06002440 ctx->user_files = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07002441 ctx->nr_user_files = 0;
2442 return ret;
2443 }
2444
2445 ret = io_sqe_files_scm(ctx);
2446 if (ret)
2447 io_sqe_files_unregister(ctx);
2448
2449 return ret;
2450}
2451
Jens Axboe6c271ce2019-01-10 11:22:30 -07002452static int io_sq_offload_start(struct io_ring_ctx *ctx,
2453 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002454{
2455 int ret;
2456
Jens Axboe6c271ce2019-01-10 11:22:30 -07002457 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002458 mmgrab(current->mm);
2459 ctx->sqo_mm = current->mm;
2460
Jens Axboe6c271ce2019-01-10 11:22:30 -07002461 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06002462 ret = -EPERM;
2463 if (!capable(CAP_SYS_ADMIN))
2464 goto err;
2465
Jens Axboe917257d2019-04-13 09:28:55 -06002466 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
2467 if (!ctx->sq_thread_idle)
2468 ctx->sq_thread_idle = HZ;
2469
Jens Axboe6c271ce2019-01-10 11:22:30 -07002470 if (p->flags & IORING_SETUP_SQ_AFF) {
Mark Rutland975554b2019-04-30 13:34:51 +01002471 int cpu = array_index_nospec(p->sq_thread_cpu,
2472 nr_cpu_ids);
Jens Axboe6c271ce2019-01-10 11:22:30 -07002473
Jens Axboe917257d2019-04-13 09:28:55 -06002474 ret = -EINVAL;
Mark Rutland975554b2019-04-30 13:34:51 +01002475 if (!cpu_possible(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06002476 goto err;
2477
Jens Axboe6c271ce2019-01-10 11:22:30 -07002478 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
2479 ctx, cpu,
2480 "io_uring-sq");
2481 } else {
2482 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
2483 "io_uring-sq");
2484 }
2485 if (IS_ERR(ctx->sqo_thread)) {
2486 ret = PTR_ERR(ctx->sqo_thread);
2487 ctx->sqo_thread = NULL;
2488 goto err;
2489 }
2490 wake_up_process(ctx->sqo_thread);
2491 } else if (p->flags & IORING_SETUP_SQ_AFF) {
2492 /* Can't have SQ_AFF without SQPOLL */
2493 ret = -EINVAL;
2494 goto err;
2495 }
2496
Jens Axboe2b188cc2019-01-07 10:46:33 -07002497 /* Do QD, or 2 * CPUS, whatever is smallest */
2498 ctx->sqo_wq = alloc_workqueue("io_ring-wq", WQ_UNBOUND | WQ_FREEZABLE,
2499 min(ctx->sq_entries - 1, 2 * num_online_cpus()));
2500 if (!ctx->sqo_wq) {
2501 ret = -ENOMEM;
2502 goto err;
2503 }
2504
2505 return 0;
2506err:
Jens Axboe6c271ce2019-01-10 11:22:30 -07002507 io_sq_thread_stop(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002508 mmdrop(ctx->sqo_mm);
2509 ctx->sqo_mm = NULL;
2510 return ret;
2511}
2512
2513static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
2514{
2515 atomic_long_sub(nr_pages, &user->locked_vm);
2516}
2517
2518static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
2519{
2520 unsigned long page_limit, cur_pages, new_pages;
2521
2522 /* Don't allow more pages than we can safely lock */
2523 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
2524
2525 do {
2526 cur_pages = atomic_long_read(&user->locked_vm);
2527 new_pages = cur_pages + nr_pages;
2528 if (new_pages > page_limit)
2529 return -ENOMEM;
2530 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
2531 new_pages) != cur_pages);
2532
2533 return 0;
2534}
2535
2536static void io_mem_free(void *ptr)
2537{
Mark Rutland52e04ef2019-04-30 17:30:21 +01002538 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002539
Mark Rutland52e04ef2019-04-30 17:30:21 +01002540 if (!ptr)
2541 return;
2542
2543 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002544 if (put_page_testzero(page))
2545 free_compound_page(page);
2546}
2547
2548static void *io_mem_alloc(size_t size)
2549{
2550 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
2551 __GFP_NORETRY;
2552
2553 return (void *) __get_free_pages(gfp_flags, get_order(size));
2554}
2555
2556static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
2557{
2558 struct io_sq_ring *sq_ring;
2559 struct io_cq_ring *cq_ring;
2560 size_t bytes;
2561
2562 bytes = struct_size(sq_ring, array, sq_entries);
2563 bytes += array_size(sizeof(struct io_uring_sqe), sq_entries);
2564 bytes += struct_size(cq_ring, cqes, cq_entries);
2565
2566 return (bytes + PAGE_SIZE - 1) / PAGE_SIZE;
2567}
2568
Jens Axboeedafcce2019-01-09 09:16:05 -07002569static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
2570{
2571 int i, j;
2572
2573 if (!ctx->user_bufs)
2574 return -ENXIO;
2575
2576 for (i = 0; i < ctx->nr_user_bufs; i++) {
2577 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
2578
2579 for (j = 0; j < imu->nr_bvecs; j++)
2580 put_page(imu->bvec[j].bv_page);
2581
2582 if (ctx->account_mem)
2583 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01002584 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07002585 imu->nr_bvecs = 0;
2586 }
2587
2588 kfree(ctx->user_bufs);
2589 ctx->user_bufs = NULL;
2590 ctx->nr_user_bufs = 0;
2591 return 0;
2592}
2593
2594static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
2595 void __user *arg, unsigned index)
2596{
2597 struct iovec __user *src;
2598
2599#ifdef CONFIG_COMPAT
2600 if (ctx->compat) {
2601 struct compat_iovec __user *ciovs;
2602 struct compat_iovec ciov;
2603
2604 ciovs = (struct compat_iovec __user *) arg;
2605 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
2606 return -EFAULT;
2607
2608 dst->iov_base = (void __user *) (unsigned long) ciov.iov_base;
2609 dst->iov_len = ciov.iov_len;
2610 return 0;
2611 }
2612#endif
2613 src = (struct iovec __user *) arg;
2614 if (copy_from_user(dst, &src[index], sizeof(*dst)))
2615 return -EFAULT;
2616 return 0;
2617}
2618
2619static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
2620 unsigned nr_args)
2621{
2622 struct vm_area_struct **vmas = NULL;
2623 struct page **pages = NULL;
2624 int i, j, got_pages = 0;
2625 int ret = -EINVAL;
2626
2627 if (ctx->user_bufs)
2628 return -EBUSY;
2629 if (!nr_args || nr_args > UIO_MAXIOV)
2630 return -EINVAL;
2631
2632 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
2633 GFP_KERNEL);
2634 if (!ctx->user_bufs)
2635 return -ENOMEM;
2636
2637 for (i = 0; i < nr_args; i++) {
2638 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
2639 unsigned long off, start, end, ubuf;
2640 int pret, nr_pages;
2641 struct iovec iov;
2642 size_t size;
2643
2644 ret = io_copy_iov(ctx, &iov, arg, i);
2645 if (ret)
2646 break;
2647
2648 /*
2649 * Don't impose further limits on the size and buffer
2650 * constraints here, we'll -EINVAL later when IO is
2651 * submitted if they are wrong.
2652 */
2653 ret = -EFAULT;
2654 if (!iov.iov_base || !iov.iov_len)
2655 goto err;
2656
2657 /* arbitrary limit, but we need something */
2658 if (iov.iov_len > SZ_1G)
2659 goto err;
2660
2661 ubuf = (unsigned long) iov.iov_base;
2662 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
2663 start = ubuf >> PAGE_SHIFT;
2664 nr_pages = end - start;
2665
2666 if (ctx->account_mem) {
2667 ret = io_account_mem(ctx->user, nr_pages);
2668 if (ret)
2669 goto err;
2670 }
2671
2672 ret = 0;
2673 if (!pages || nr_pages > got_pages) {
2674 kfree(vmas);
2675 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01002676 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07002677 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01002678 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07002679 sizeof(struct vm_area_struct *),
2680 GFP_KERNEL);
2681 if (!pages || !vmas) {
2682 ret = -ENOMEM;
2683 if (ctx->account_mem)
2684 io_unaccount_mem(ctx->user, nr_pages);
2685 goto err;
2686 }
2687 got_pages = nr_pages;
2688 }
2689
Mark Rutlandd4ef6472019-05-01 16:59:16 +01002690 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07002691 GFP_KERNEL);
2692 ret = -ENOMEM;
2693 if (!imu->bvec) {
2694 if (ctx->account_mem)
2695 io_unaccount_mem(ctx->user, nr_pages);
2696 goto err;
2697 }
2698
2699 ret = 0;
2700 down_read(&current->mm->mmap_sem);
2701 pret = get_user_pages_longterm(ubuf, nr_pages, FOLL_WRITE,
2702 pages, vmas);
2703 if (pret == nr_pages) {
2704 /* don't support file backed memory */
2705 for (j = 0; j < nr_pages; j++) {
2706 struct vm_area_struct *vma = vmas[j];
2707
2708 if (vma->vm_file &&
2709 !is_file_hugepages(vma->vm_file)) {
2710 ret = -EOPNOTSUPP;
2711 break;
2712 }
2713 }
2714 } else {
2715 ret = pret < 0 ? pret : -EFAULT;
2716 }
2717 up_read(&current->mm->mmap_sem);
2718 if (ret) {
2719 /*
2720 * if we did partial map, or found file backed vmas,
2721 * release any pages we did get
2722 */
2723 if (pret > 0) {
2724 for (j = 0; j < pret; j++)
2725 put_page(pages[j]);
2726 }
2727 if (ctx->account_mem)
2728 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01002729 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07002730 goto err;
2731 }
2732
2733 off = ubuf & ~PAGE_MASK;
2734 size = iov.iov_len;
2735 for (j = 0; j < nr_pages; j++) {
2736 size_t vec_len;
2737
2738 vec_len = min_t(size_t, size, PAGE_SIZE - off);
2739 imu->bvec[j].bv_page = pages[j];
2740 imu->bvec[j].bv_len = vec_len;
2741 imu->bvec[j].bv_offset = off;
2742 off = 0;
2743 size -= vec_len;
2744 }
2745 /* store original address for later verification */
2746 imu->ubuf = ubuf;
2747 imu->len = iov.iov_len;
2748 imu->nr_bvecs = nr_pages;
2749
2750 ctx->nr_user_bufs++;
2751 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01002752 kvfree(pages);
2753 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07002754 return 0;
2755err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01002756 kvfree(pages);
2757 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07002758 io_sqe_buffer_unregister(ctx);
2759 return ret;
2760}
2761
Jens Axboe9b402842019-04-11 11:45:41 -06002762static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
2763{
2764 __s32 __user *fds = arg;
2765 int fd;
2766
2767 if (ctx->cq_ev_fd)
2768 return -EBUSY;
2769
2770 if (copy_from_user(&fd, fds, sizeof(*fds)))
2771 return -EFAULT;
2772
2773 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
2774 if (IS_ERR(ctx->cq_ev_fd)) {
2775 int ret = PTR_ERR(ctx->cq_ev_fd);
2776 ctx->cq_ev_fd = NULL;
2777 return ret;
2778 }
2779
2780 return 0;
2781}
2782
2783static int io_eventfd_unregister(struct io_ring_ctx *ctx)
2784{
2785 if (ctx->cq_ev_fd) {
2786 eventfd_ctx_put(ctx->cq_ev_fd);
2787 ctx->cq_ev_fd = NULL;
2788 return 0;
2789 }
2790
2791 return -ENXIO;
2792}
2793
Jens Axboe2b188cc2019-01-07 10:46:33 -07002794static void io_ring_ctx_free(struct io_ring_ctx *ctx)
2795{
Jens Axboe6b063142019-01-10 22:13:58 -07002796 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002797 if (ctx->sqo_mm)
2798 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07002799
2800 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07002801 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07002802 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06002803 io_eventfd_unregister(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07002804
Jens Axboe2b188cc2019-01-07 10:46:33 -07002805#if defined(CONFIG_UNIX)
2806 if (ctx->ring_sock)
2807 sock_release(ctx->ring_sock);
2808#endif
2809
2810 io_mem_free(ctx->sq_ring);
2811 io_mem_free(ctx->sq_sqes);
2812 io_mem_free(ctx->cq_ring);
2813
2814 percpu_ref_exit(&ctx->refs);
2815 if (ctx->account_mem)
2816 io_unaccount_mem(ctx->user,
2817 ring_pages(ctx->sq_entries, ctx->cq_entries));
2818 free_uid(ctx->user);
2819 kfree(ctx);
2820}
2821
2822static __poll_t io_uring_poll(struct file *file, poll_table *wait)
2823{
2824 struct io_ring_ctx *ctx = file->private_data;
2825 __poll_t mask = 0;
2826
2827 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02002828 /*
2829 * synchronizes with barrier from wq_has_sleeper call in
2830 * io_commit_cqring
2831 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07002832 smp_rmb();
Stefan Bühlerfb775fa2019-04-19 11:57:46 +02002833 if (READ_ONCE(ctx->sq_ring->r.tail) - ctx->cached_sq_head !=
2834 ctx->sq_ring->ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002835 mask |= EPOLLOUT | EPOLLWRNORM;
2836 if (READ_ONCE(ctx->cq_ring->r.head) != ctx->cached_cq_tail)
2837 mask |= EPOLLIN | EPOLLRDNORM;
2838
2839 return mask;
2840}
2841
2842static int io_uring_fasync(int fd, struct file *file, int on)
2843{
2844 struct io_ring_ctx *ctx = file->private_data;
2845
2846 return fasync_helper(fd, file, on, &ctx->cq_fasync);
2847}
2848
2849static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
2850{
2851 mutex_lock(&ctx->uring_lock);
2852 percpu_ref_kill(&ctx->refs);
2853 mutex_unlock(&ctx->uring_lock);
2854
Jens Axboe221c5eb2019-01-17 09:41:58 -07002855 io_poll_remove_all(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07002856 io_iopoll_reap_events(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002857 wait_for_completion(&ctx->ctx_done);
2858 io_ring_ctx_free(ctx);
2859}
2860
2861static int io_uring_release(struct inode *inode, struct file *file)
2862{
2863 struct io_ring_ctx *ctx = file->private_data;
2864
2865 file->private_data = NULL;
2866 io_ring_ctx_wait_and_kill(ctx);
2867 return 0;
2868}
2869
2870static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
2871{
2872 loff_t offset = (loff_t) vma->vm_pgoff << PAGE_SHIFT;
2873 unsigned long sz = vma->vm_end - vma->vm_start;
2874 struct io_ring_ctx *ctx = file->private_data;
2875 unsigned long pfn;
2876 struct page *page;
2877 void *ptr;
2878
2879 switch (offset) {
2880 case IORING_OFF_SQ_RING:
2881 ptr = ctx->sq_ring;
2882 break;
2883 case IORING_OFF_SQES:
2884 ptr = ctx->sq_sqes;
2885 break;
2886 case IORING_OFF_CQ_RING:
2887 ptr = ctx->cq_ring;
2888 break;
2889 default:
2890 return -EINVAL;
2891 }
2892
2893 page = virt_to_head_page(ptr);
2894 if (sz > (PAGE_SIZE << compound_order(page)))
2895 return -EINVAL;
2896
2897 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
2898 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
2899}
2900
2901SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
2902 u32, min_complete, u32, flags, const sigset_t __user *, sig,
2903 size_t, sigsz)
2904{
2905 struct io_ring_ctx *ctx;
2906 long ret = -EBADF;
2907 int submitted = 0;
2908 struct fd f;
2909
Jens Axboe6c271ce2019-01-10 11:22:30 -07002910 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002911 return -EINVAL;
2912
2913 f = fdget(fd);
2914 if (!f.file)
2915 return -EBADF;
2916
2917 ret = -EOPNOTSUPP;
2918 if (f.file->f_op != &io_uring_fops)
2919 goto out_fput;
2920
2921 ret = -ENXIO;
2922 ctx = f.file->private_data;
2923 if (!percpu_ref_tryget(&ctx->refs))
2924 goto out_fput;
2925
Jens Axboe6c271ce2019-01-10 11:22:30 -07002926 /*
2927 * For SQ polling, the thread will do all submissions and completions.
2928 * Just return the requested submit count, and wake the thread if
2929 * we were asked to.
2930 */
2931 if (ctx->flags & IORING_SETUP_SQPOLL) {
2932 if (flags & IORING_ENTER_SQ_WAKEUP)
2933 wake_up(&ctx->sqo_wait);
2934 submitted = to_submit;
2935 goto out_ctx;
2936 }
2937
Jens Axboe2b188cc2019-01-07 10:46:33 -07002938 ret = 0;
2939 if (to_submit) {
2940 to_submit = min(to_submit, ctx->sq_entries);
2941
2942 mutex_lock(&ctx->uring_lock);
2943 submitted = io_ring_submit(ctx, to_submit);
2944 mutex_unlock(&ctx->uring_lock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002945 }
2946 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07002947 unsigned nr_events = 0;
2948
Jens Axboe2b188cc2019-01-07 10:46:33 -07002949 min_complete = min(min_complete, ctx->cq_entries);
2950
Jens Axboedef596e2019-01-09 08:59:42 -07002951 if (ctx->flags & IORING_SETUP_IOPOLL) {
2952 mutex_lock(&ctx->uring_lock);
2953 ret = io_iopoll_check(ctx, &nr_events, min_complete);
2954 mutex_unlock(&ctx->uring_lock);
2955 } else {
2956 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
2957 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002958 }
2959
2960out_ctx:
2961 io_ring_drop_ctx_refs(ctx, 1);
2962out_fput:
2963 fdput(f);
2964 return submitted ? submitted : ret;
2965}
2966
2967static const struct file_operations io_uring_fops = {
2968 .release = io_uring_release,
2969 .mmap = io_uring_mmap,
2970 .poll = io_uring_poll,
2971 .fasync = io_uring_fasync,
2972};
2973
2974static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
2975 struct io_uring_params *p)
2976{
2977 struct io_sq_ring *sq_ring;
2978 struct io_cq_ring *cq_ring;
2979 size_t size;
2980
2981 sq_ring = io_mem_alloc(struct_size(sq_ring, array, p->sq_entries));
2982 if (!sq_ring)
2983 return -ENOMEM;
2984
2985 ctx->sq_ring = sq_ring;
2986 sq_ring->ring_mask = p->sq_entries - 1;
2987 sq_ring->ring_entries = p->sq_entries;
2988 ctx->sq_mask = sq_ring->ring_mask;
2989 ctx->sq_entries = sq_ring->ring_entries;
2990
2991 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
2992 if (size == SIZE_MAX)
2993 return -EOVERFLOW;
2994
2995 ctx->sq_sqes = io_mem_alloc(size);
Mark Rutland52e04ef2019-04-30 17:30:21 +01002996 if (!ctx->sq_sqes)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002997 return -ENOMEM;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002998
2999 cq_ring = io_mem_alloc(struct_size(cq_ring, cqes, p->cq_entries));
Mark Rutland52e04ef2019-04-30 17:30:21 +01003000 if (!cq_ring)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003001 return -ENOMEM;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003002
3003 ctx->cq_ring = cq_ring;
3004 cq_ring->ring_mask = p->cq_entries - 1;
3005 cq_ring->ring_entries = p->cq_entries;
3006 ctx->cq_mask = cq_ring->ring_mask;
3007 ctx->cq_entries = cq_ring->ring_entries;
3008 return 0;
3009}
3010
3011/*
3012 * Allocate an anonymous fd, this is what constitutes the application
3013 * visible backing of an io_uring instance. The application mmaps this
3014 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
3015 * we have to tie this fd to a socket for file garbage collection purposes.
3016 */
3017static int io_uring_get_fd(struct io_ring_ctx *ctx)
3018{
3019 struct file *file;
3020 int ret;
3021
3022#if defined(CONFIG_UNIX)
3023 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
3024 &ctx->ring_sock);
3025 if (ret)
3026 return ret;
3027#endif
3028
3029 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
3030 if (ret < 0)
3031 goto err;
3032
3033 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
3034 O_RDWR | O_CLOEXEC);
3035 if (IS_ERR(file)) {
3036 put_unused_fd(ret);
3037 ret = PTR_ERR(file);
3038 goto err;
3039 }
3040
3041#if defined(CONFIG_UNIX)
3042 ctx->ring_sock->file = file;
Jens Axboe6b063142019-01-10 22:13:58 -07003043 ctx->ring_sock->sk->sk_user_data = ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003044#endif
3045 fd_install(ret, file);
3046 return ret;
3047err:
3048#if defined(CONFIG_UNIX)
3049 sock_release(ctx->ring_sock);
3050 ctx->ring_sock = NULL;
3051#endif
3052 return ret;
3053}
3054
3055static int io_uring_create(unsigned entries, struct io_uring_params *p)
3056{
3057 struct user_struct *user = NULL;
3058 struct io_ring_ctx *ctx;
3059 bool account_mem;
3060 int ret;
3061
3062 if (!entries || entries > IORING_MAX_ENTRIES)
3063 return -EINVAL;
3064
3065 /*
3066 * Use twice as many entries for the CQ ring. It's possible for the
3067 * application to drive a higher depth than the size of the SQ ring,
3068 * since the sqes are only used at submission time. This allows for
3069 * some flexibility in overcommitting a bit.
3070 */
3071 p->sq_entries = roundup_pow_of_two(entries);
3072 p->cq_entries = 2 * p->sq_entries;
3073
3074 user = get_uid(current_user());
3075 account_mem = !capable(CAP_IPC_LOCK);
3076
3077 if (account_mem) {
3078 ret = io_account_mem(user,
3079 ring_pages(p->sq_entries, p->cq_entries));
3080 if (ret) {
3081 free_uid(user);
3082 return ret;
3083 }
3084 }
3085
3086 ctx = io_ring_ctx_alloc(p);
3087 if (!ctx) {
3088 if (account_mem)
3089 io_unaccount_mem(user, ring_pages(p->sq_entries,
3090 p->cq_entries));
3091 free_uid(user);
3092 return -ENOMEM;
3093 }
3094 ctx->compat = in_compat_syscall();
3095 ctx->account_mem = account_mem;
3096 ctx->user = user;
3097
3098 ret = io_allocate_scq_urings(ctx, p);
3099 if (ret)
3100 goto err;
3101
Jens Axboe6c271ce2019-01-10 11:22:30 -07003102 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003103 if (ret)
3104 goto err;
3105
3106 ret = io_uring_get_fd(ctx);
3107 if (ret < 0)
3108 goto err;
3109
3110 memset(&p->sq_off, 0, sizeof(p->sq_off));
3111 p->sq_off.head = offsetof(struct io_sq_ring, r.head);
3112 p->sq_off.tail = offsetof(struct io_sq_ring, r.tail);
3113 p->sq_off.ring_mask = offsetof(struct io_sq_ring, ring_mask);
3114 p->sq_off.ring_entries = offsetof(struct io_sq_ring, ring_entries);
3115 p->sq_off.flags = offsetof(struct io_sq_ring, flags);
3116 p->sq_off.dropped = offsetof(struct io_sq_ring, dropped);
3117 p->sq_off.array = offsetof(struct io_sq_ring, array);
3118
3119 memset(&p->cq_off, 0, sizeof(p->cq_off));
3120 p->cq_off.head = offsetof(struct io_cq_ring, r.head);
3121 p->cq_off.tail = offsetof(struct io_cq_ring, r.tail);
3122 p->cq_off.ring_mask = offsetof(struct io_cq_ring, ring_mask);
3123 p->cq_off.ring_entries = offsetof(struct io_cq_ring, ring_entries);
3124 p->cq_off.overflow = offsetof(struct io_cq_ring, overflow);
3125 p->cq_off.cqes = offsetof(struct io_cq_ring, cqes);
3126 return ret;
3127err:
3128 io_ring_ctx_wait_and_kill(ctx);
3129 return ret;
3130}
3131
3132/*
3133 * Sets up an aio uring context, and returns the fd. Applications asks for a
3134 * ring size, we return the actual sq/cq ring sizes (among other things) in the
3135 * params structure passed in.
3136 */
3137static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
3138{
3139 struct io_uring_params p;
3140 long ret;
3141 int i;
3142
3143 if (copy_from_user(&p, params, sizeof(p)))
3144 return -EFAULT;
3145 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
3146 if (p.resv[i])
3147 return -EINVAL;
3148 }
3149
Jens Axboe6c271ce2019-01-10 11:22:30 -07003150 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
3151 IORING_SETUP_SQ_AFF))
Jens Axboe2b188cc2019-01-07 10:46:33 -07003152 return -EINVAL;
3153
3154 ret = io_uring_create(entries, &p);
3155 if (ret < 0)
3156 return ret;
3157
3158 if (copy_to_user(params, &p, sizeof(p)))
3159 return -EFAULT;
3160
3161 return ret;
3162}
3163
3164SYSCALL_DEFINE2(io_uring_setup, u32, entries,
3165 struct io_uring_params __user *, params)
3166{
3167 return io_uring_setup(entries, params);
3168}
3169
Jens Axboeedafcce2019-01-09 09:16:05 -07003170static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
3171 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06003172 __releases(ctx->uring_lock)
3173 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07003174{
3175 int ret;
3176
Jens Axboe35fa71a2019-04-22 10:23:23 -06003177 /*
3178 * We're inside the ring mutex, if the ref is already dying, then
3179 * someone else killed the ctx or is already going through
3180 * io_uring_register().
3181 */
3182 if (percpu_ref_is_dying(&ctx->refs))
3183 return -ENXIO;
3184
Jens Axboeedafcce2019-01-09 09:16:05 -07003185 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06003186
3187 /*
3188 * Drop uring mutex before waiting for references to exit. If another
3189 * thread is currently inside io_uring_enter() it might need to grab
3190 * the uring_lock to make progress. If we hold it here across the drain
3191 * wait, then we can deadlock. It's safe to drop the mutex here, since
3192 * no new references will come in after we've killed the percpu ref.
3193 */
3194 mutex_unlock(&ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07003195 wait_for_completion(&ctx->ctx_done);
Jens Axboeb19062a2019-04-15 10:49:38 -06003196 mutex_lock(&ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07003197
3198 switch (opcode) {
3199 case IORING_REGISTER_BUFFERS:
3200 ret = io_sqe_buffer_register(ctx, arg, nr_args);
3201 break;
3202 case IORING_UNREGISTER_BUFFERS:
3203 ret = -EINVAL;
3204 if (arg || nr_args)
3205 break;
3206 ret = io_sqe_buffer_unregister(ctx);
3207 break;
Jens Axboe6b063142019-01-10 22:13:58 -07003208 case IORING_REGISTER_FILES:
3209 ret = io_sqe_files_register(ctx, arg, nr_args);
3210 break;
3211 case IORING_UNREGISTER_FILES:
3212 ret = -EINVAL;
3213 if (arg || nr_args)
3214 break;
3215 ret = io_sqe_files_unregister(ctx);
3216 break;
Jens Axboe9b402842019-04-11 11:45:41 -06003217 case IORING_REGISTER_EVENTFD:
3218 ret = -EINVAL;
3219 if (nr_args != 1)
3220 break;
3221 ret = io_eventfd_register(ctx, arg);
3222 break;
3223 case IORING_UNREGISTER_EVENTFD:
3224 ret = -EINVAL;
3225 if (arg || nr_args)
3226 break;
3227 ret = io_eventfd_unregister(ctx);
3228 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07003229 default:
3230 ret = -EINVAL;
3231 break;
3232 }
3233
3234 /* bring the ctx back to life */
3235 reinit_completion(&ctx->ctx_done);
3236 percpu_ref_reinit(&ctx->refs);
3237 return ret;
3238}
3239
3240SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
3241 void __user *, arg, unsigned int, nr_args)
3242{
3243 struct io_ring_ctx *ctx;
3244 long ret = -EBADF;
3245 struct fd f;
3246
3247 f = fdget(fd);
3248 if (!f.file)
3249 return -EBADF;
3250
3251 ret = -EOPNOTSUPP;
3252 if (f.file->f_op != &io_uring_fops)
3253 goto out_fput;
3254
3255 ctx = f.file->private_data;
3256
3257 mutex_lock(&ctx->uring_lock);
3258 ret = __io_uring_register(ctx, opcode, arg, nr_args);
3259 mutex_unlock(&ctx->uring_lock);
3260out_fput:
3261 fdput(f);
3262 return ret;
3263}
3264
Jens Axboe2b188cc2019-01-07 10:46:33 -07003265static int __init io_uring_init(void)
3266{
3267 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
3268 return 0;
3269};
3270__initcall(io_uring_init);