blob: 2a46de56d05cf121e97383f0c0a39fd28a23e110 [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;
Jens Axboede0617e2019-04-06 21:51:27 -0600336 u32 error;
337 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 */
1523 req->error = mangle_poll(mask);
1524 ipt.error = 0;
1525 io_poll_complete(ctx, req, mask);
1526 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07001527 spin_unlock_irq(&ctx->completion_lock);
1528
Jens Axboe8c838782019-03-12 15:48:16 -06001529 if (mask) {
1530 io_cqring_ev_posted(ctx);
Jens Axboee65ef562019-03-12 10:16:44 -06001531 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001532 }
Jens Axboe8c838782019-03-12 15:48:16 -06001533 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001534}
1535
Jens Axboede0617e2019-04-06 21:51:27 -06001536static int io_req_defer(struct io_ring_ctx *ctx, struct io_kiocb *req,
1537 const struct io_uring_sqe *sqe)
1538{
1539 struct io_uring_sqe *sqe_copy;
1540
1541 if (!io_sequence_defer(ctx, req) && list_empty(&ctx->defer_list))
1542 return 0;
1543
1544 sqe_copy = kmalloc(sizeof(*sqe_copy), GFP_KERNEL);
1545 if (!sqe_copy)
1546 return -EAGAIN;
1547
1548 spin_lock_irq(&ctx->completion_lock);
1549 if (!io_sequence_defer(ctx, req) && list_empty(&ctx->defer_list)) {
1550 spin_unlock_irq(&ctx->completion_lock);
1551 kfree(sqe_copy);
1552 return 0;
1553 }
1554
1555 memcpy(sqe_copy, sqe, sizeof(*sqe_copy));
1556 req->submit.sqe = sqe_copy;
1557
1558 INIT_WORK(&req->work, io_sq_wq_submit_work);
1559 list_add_tail(&req->list, &ctx->defer_list);
1560 spin_unlock_irq(&ctx->completion_lock);
1561 return -EIOCBQUEUED;
1562}
1563
Jens Axboe2b188cc2019-01-07 10:46:33 -07001564static int __io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001565 const struct sqe_submit *s, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001566{
Jens Axboee0c5c572019-03-12 10:18:47 -06001567 int ret, opcode;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001568
1569 if (unlikely(s->index >= ctx->sq_entries))
1570 return -EINVAL;
1571 req->user_data = READ_ONCE(s->sqe->user_data);
1572
1573 opcode = READ_ONCE(s->sqe->opcode);
1574 switch (opcode) {
1575 case IORING_OP_NOP:
1576 ret = io_nop(req, req->user_data);
1577 break;
1578 case IORING_OP_READV:
Jens Axboeedafcce2019-01-09 09:16:05 -07001579 if (unlikely(s->sqe->buf_index))
1580 return -EINVAL;
Jens Axboe8358e3a2019-04-23 08:17:58 -06001581 ret = io_read(req, s, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001582 break;
1583 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07001584 if (unlikely(s->sqe->buf_index))
1585 return -EINVAL;
Jens Axboe8358e3a2019-04-23 08:17:58 -06001586 ret = io_write(req, s, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07001587 break;
1588 case IORING_OP_READ_FIXED:
Jens Axboe8358e3a2019-04-23 08:17:58 -06001589 ret = io_read(req, s, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07001590 break;
1591 case IORING_OP_WRITE_FIXED:
Jens Axboe8358e3a2019-04-23 08:17:58 -06001592 ret = io_write(req, s, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001593 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001594 case IORING_OP_FSYNC:
1595 ret = io_fsync(req, s->sqe, force_nonblock);
1596 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001597 case IORING_OP_POLL_ADD:
1598 ret = io_poll_add(req, s->sqe);
1599 break;
1600 case IORING_OP_POLL_REMOVE:
1601 ret = io_poll_remove(req, s->sqe);
1602 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001603 case IORING_OP_SYNC_FILE_RANGE:
1604 ret = io_sync_file_range(req, s->sqe, force_nonblock);
1605 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001606 default:
1607 ret = -EINVAL;
1608 break;
1609 }
1610
Jens Axboedef596e2019-01-09 08:59:42 -07001611 if (ret)
1612 return ret;
1613
1614 if (ctx->flags & IORING_SETUP_IOPOLL) {
1615 if (req->error == -EAGAIN)
1616 return -EAGAIN;
1617
1618 /* workqueue context doesn't hold uring_lock, grab it now */
1619 if (s->needs_lock)
1620 mutex_lock(&ctx->uring_lock);
1621 io_iopoll_req_issued(req);
1622 if (s->needs_lock)
1623 mutex_unlock(&ctx->uring_lock);
1624 }
1625
1626 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001627}
1628
Jens Axboe31b51512019-01-18 22:56:34 -07001629static struct async_list *io_async_list_from_sqe(struct io_ring_ctx *ctx,
1630 const struct io_uring_sqe *sqe)
1631{
1632 switch (sqe->opcode) {
1633 case IORING_OP_READV:
1634 case IORING_OP_READ_FIXED:
1635 return &ctx->pending_async[READ];
1636 case IORING_OP_WRITEV:
1637 case IORING_OP_WRITE_FIXED:
1638 return &ctx->pending_async[WRITE];
1639 default:
1640 return NULL;
1641 }
1642}
1643
Jens Axboeedafcce2019-01-09 09:16:05 -07001644static inline bool io_sqe_needs_user(const struct io_uring_sqe *sqe)
1645{
1646 u8 opcode = READ_ONCE(sqe->opcode);
1647
1648 return !(opcode == IORING_OP_READ_FIXED ||
1649 opcode == IORING_OP_WRITE_FIXED);
1650}
1651
Jens Axboe2b188cc2019-01-07 10:46:33 -07001652static void io_sq_wq_submit_work(struct work_struct *work)
1653{
1654 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001655 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe31b51512019-01-18 22:56:34 -07001656 struct mm_struct *cur_mm = NULL;
1657 struct async_list *async_list;
1658 LIST_HEAD(req_list);
Jens Axboeedafcce2019-01-09 09:16:05 -07001659 mm_segment_t old_fs;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001660 int ret;
1661
Jens Axboe31b51512019-01-18 22:56:34 -07001662 async_list = io_async_list_from_sqe(ctx, req->submit.sqe);
1663restart:
1664 do {
1665 struct sqe_submit *s = &req->submit;
1666 const struct io_uring_sqe *sqe = s->sqe;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001667
Stefan Bühler8449eed2019-04-27 20:34:19 +02001668 /* Ensure we clear previously set non-block flag */
Jens Axboe31b51512019-01-18 22:56:34 -07001669 req->rw.ki_flags &= ~IOCB_NOWAIT;
1670
1671 ret = 0;
1672 if (io_sqe_needs_user(sqe) && !cur_mm) {
1673 if (!mmget_not_zero(ctx->sqo_mm)) {
1674 ret = -EFAULT;
1675 } else {
1676 cur_mm = ctx->sqo_mm;
1677 use_mm(cur_mm);
1678 old_fs = get_fs();
1679 set_fs(USER_DS);
1680 }
1681 }
1682
1683 if (!ret) {
1684 s->has_user = cur_mm != NULL;
1685 s->needs_lock = true;
1686 do {
Jens Axboe8358e3a2019-04-23 08:17:58 -06001687 ret = __io_submit_sqe(ctx, req, s, false);
Jens Axboe31b51512019-01-18 22:56:34 -07001688 /*
1689 * We can get EAGAIN for polled IO even though
1690 * we're forcing a sync submission from here,
1691 * since we can't wait for request slots on the
1692 * block side.
1693 */
1694 if (ret != -EAGAIN)
1695 break;
1696 cond_resched();
1697 } while (1);
1698 }
Jens Axboe817869d2019-04-30 14:44:05 -06001699
1700 /* drop submission reference */
1701 io_put_req(req);
1702
Jens Axboe31b51512019-01-18 22:56:34 -07001703 if (ret) {
1704 io_cqring_add_event(ctx, sqe->user_data, ret, 0);
Jens Axboee65ef562019-03-12 10:16:44 -06001705 io_put_req(req);
Jens Axboe31b51512019-01-18 22:56:34 -07001706 }
1707
1708 /* async context always use a copy of the sqe */
1709 kfree(sqe);
1710
1711 if (!async_list)
1712 break;
1713 if (!list_empty(&req_list)) {
1714 req = list_first_entry(&req_list, struct io_kiocb,
1715 list);
1716 list_del(&req->list);
1717 continue;
1718 }
1719 if (list_empty(&async_list->list))
1720 break;
1721
1722 req = NULL;
1723 spin_lock(&async_list->lock);
1724 if (list_empty(&async_list->list)) {
1725 spin_unlock(&async_list->lock);
1726 break;
1727 }
1728 list_splice_init(&async_list->list, &req_list);
1729 spin_unlock(&async_list->lock);
1730
1731 req = list_first_entry(&req_list, struct io_kiocb, list);
1732 list_del(&req->list);
1733 } while (req);
Jens Axboeedafcce2019-01-09 09:16:05 -07001734
1735 /*
Jens Axboe31b51512019-01-18 22:56:34 -07001736 * Rare case of racing with a submitter. If we find the count has
1737 * dropped to zero AND we have pending work items, then restart
1738 * the processing. This is a tiny race window.
Jens Axboeedafcce2019-01-09 09:16:05 -07001739 */
Jens Axboe31b51512019-01-18 22:56:34 -07001740 if (async_list) {
1741 ret = atomic_dec_return(&async_list->cnt);
1742 while (!ret && !list_empty(&async_list->list)) {
1743 spin_lock(&async_list->lock);
1744 atomic_inc(&async_list->cnt);
1745 list_splice_init(&async_list->list, &req_list);
1746 spin_unlock(&async_list->lock);
1747
1748 if (!list_empty(&req_list)) {
1749 req = list_first_entry(&req_list,
1750 struct io_kiocb, list);
1751 list_del(&req->list);
1752 goto restart;
1753 }
1754 ret = atomic_dec_return(&async_list->cnt);
Jens Axboeedafcce2019-01-09 09:16:05 -07001755 }
Jens Axboeedafcce2019-01-09 09:16:05 -07001756 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001757
Jens Axboe31b51512019-01-18 22:56:34 -07001758 if (cur_mm) {
Jens Axboeedafcce2019-01-09 09:16:05 -07001759 set_fs(old_fs);
Jens Axboe31b51512019-01-18 22:56:34 -07001760 unuse_mm(cur_mm);
1761 mmput(cur_mm);
Jens Axboeedafcce2019-01-09 09:16:05 -07001762 }
Jens Axboe31b51512019-01-18 22:56:34 -07001763}
Jens Axboe2b188cc2019-01-07 10:46:33 -07001764
Jens Axboe31b51512019-01-18 22:56:34 -07001765/*
1766 * See if we can piggy back onto previously submitted work, that is still
1767 * running. We currently only allow this if the new request is sequential
1768 * to the previous one we punted.
1769 */
1770static bool io_add_to_prev_work(struct async_list *list, struct io_kiocb *req)
1771{
1772 bool ret = false;
1773
1774 if (!list)
1775 return false;
1776 if (!(req->flags & REQ_F_SEQ_PREV))
1777 return false;
1778 if (!atomic_read(&list->cnt))
1779 return false;
1780
1781 ret = true;
1782 spin_lock(&list->lock);
1783 list_add_tail(&req->list, &list->list);
1784 if (!atomic_read(&list->cnt)) {
1785 list_del_init(&req->list);
1786 ret = false;
1787 }
1788 spin_unlock(&list->lock);
1789 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001790}
1791
Jens Axboe09bb8392019-03-13 12:39:28 -06001792static bool io_op_needs_file(const struct io_uring_sqe *sqe)
1793{
1794 int op = READ_ONCE(sqe->opcode);
1795
1796 switch (op) {
1797 case IORING_OP_NOP:
1798 case IORING_OP_POLL_REMOVE:
1799 return false;
1800 default:
1801 return true;
1802 }
1803}
1804
1805static int io_req_set_file(struct io_ring_ctx *ctx, const struct sqe_submit *s,
1806 struct io_submit_state *state, struct io_kiocb *req)
1807{
1808 unsigned flags;
1809 int fd;
1810
1811 flags = READ_ONCE(s->sqe->flags);
1812 fd = READ_ONCE(s->sqe->fd);
1813
Jens Axboede0617e2019-04-06 21:51:27 -06001814 if (flags & IOSQE_IO_DRAIN) {
1815 req->flags |= REQ_F_IO_DRAIN;
1816 req->sequence = ctx->cached_sq_head - 1;
1817 }
1818
Jens Axboe09bb8392019-03-13 12:39:28 -06001819 if (!io_op_needs_file(s->sqe)) {
1820 req->file = NULL;
1821 return 0;
1822 }
1823
1824 if (flags & IOSQE_FIXED_FILE) {
1825 if (unlikely(!ctx->user_files ||
1826 (unsigned) fd >= ctx->nr_user_files))
1827 return -EBADF;
1828 req->file = ctx->user_files[fd];
1829 req->flags |= REQ_F_FIXED_FILE;
1830 } else {
1831 if (s->needs_fixed_file)
1832 return -EBADF;
1833 req->file = io_file_get(state, fd);
1834 if (unlikely(!req->file))
1835 return -EBADF;
1836 }
1837
1838 return 0;
1839}
1840
Jens Axboe9a56a232019-01-09 09:06:50 -07001841static int io_submit_sqe(struct io_ring_ctx *ctx, struct sqe_submit *s,
1842 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001843{
1844 struct io_kiocb *req;
Jens Axboee0c5c572019-03-12 10:18:47 -06001845 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001846
1847 /* enforce forwards compatibility on users */
Jens Axboede0617e2019-04-06 21:51:27 -06001848 if (unlikely(s->sqe->flags & ~(IOSQE_FIXED_FILE | IOSQE_IO_DRAIN)))
Jens Axboe2b188cc2019-01-07 10:46:33 -07001849 return -EINVAL;
1850
Jens Axboe2579f912019-01-09 09:10:43 -07001851 req = io_get_req(ctx, state);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001852 if (unlikely(!req))
1853 return -EAGAIN;
1854
Jens Axboe09bb8392019-03-13 12:39:28 -06001855 ret = io_req_set_file(ctx, s, state, req);
1856 if (unlikely(ret))
1857 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001858
Jens Axboede0617e2019-04-06 21:51:27 -06001859 ret = io_req_defer(ctx, req, s->sqe);
1860 if (ret) {
1861 if (ret == -EIOCBQUEUED)
1862 ret = 0;
1863 return ret;
1864 }
1865
Jens Axboe8358e3a2019-04-23 08:17:58 -06001866 ret = __io_submit_sqe(ctx, req, s, true);
Stefan Bühler8449eed2019-04-27 20:34:19 +02001867 if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001868 struct io_uring_sqe *sqe_copy;
1869
1870 sqe_copy = kmalloc(sizeof(*sqe_copy), GFP_KERNEL);
1871 if (sqe_copy) {
Jens Axboe31b51512019-01-18 22:56:34 -07001872 struct async_list *list;
1873
Jens Axboe2b188cc2019-01-07 10:46:33 -07001874 memcpy(sqe_copy, s->sqe, sizeof(*sqe_copy));
1875 s->sqe = sqe_copy;
1876
1877 memcpy(&req->submit, s, sizeof(*s));
Jens Axboe31b51512019-01-18 22:56:34 -07001878 list = io_async_list_from_sqe(ctx, s->sqe);
1879 if (!io_add_to_prev_work(list, req)) {
1880 if (list)
1881 atomic_inc(&list->cnt);
1882 INIT_WORK(&req->work, io_sq_wq_submit_work);
1883 queue_work(ctx->sqo_wq, &req->work);
1884 }
Jens Axboee65ef562019-03-12 10:16:44 -06001885
1886 /*
1887 * Queued up for async execution, worker will release
1888 * submit reference when the iocb is actually
1889 * submitted.
1890 */
1891 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001892 }
1893 }
Jens Axboee65ef562019-03-12 10:16:44 -06001894
Jens Axboe09bb8392019-03-13 12:39:28 -06001895out:
Jens Axboee65ef562019-03-12 10:16:44 -06001896 /* drop submission reference */
1897 io_put_req(req);
1898
1899 /* and drop final reference, if we failed */
Jens Axboe2b188cc2019-01-07 10:46:33 -07001900 if (ret)
Jens Axboee65ef562019-03-12 10:16:44 -06001901 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001902
1903 return ret;
1904}
1905
Jens Axboe9a56a232019-01-09 09:06:50 -07001906/*
1907 * Batched submission is done, ensure local IO is flushed out.
1908 */
1909static void io_submit_state_end(struct io_submit_state *state)
1910{
1911 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06001912 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07001913 if (state->free_reqs)
1914 kmem_cache_free_bulk(req_cachep, state->free_reqs,
1915 &state->reqs[state->cur_req]);
Jens Axboe9a56a232019-01-09 09:06:50 -07001916}
1917
1918/*
1919 * Start submission side cache.
1920 */
1921static void io_submit_state_start(struct io_submit_state *state,
1922 struct io_ring_ctx *ctx, unsigned max_ios)
1923{
1924 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07001925 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07001926 state->file = NULL;
1927 state->ios_left = max_ios;
1928}
1929
Jens Axboe2b188cc2019-01-07 10:46:33 -07001930static void io_commit_sqring(struct io_ring_ctx *ctx)
1931{
1932 struct io_sq_ring *ring = ctx->sq_ring;
1933
1934 if (ctx->cached_sq_head != READ_ONCE(ring->r.head)) {
1935 /*
1936 * Ensure any loads from the SQEs are done at this point,
1937 * since once we write the new head, the application could
1938 * write new data to them.
1939 */
1940 smp_store_release(&ring->r.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001941 }
1942}
1943
1944/*
Jens Axboe2b188cc2019-01-07 10:46:33 -07001945 * Fetch an sqe, if one is available. Note that s->sqe will point to memory
1946 * that is mapped by userspace. This means that care needs to be taken to
1947 * ensure that reads are stable, as we cannot rely on userspace always
1948 * being a good citizen. If members of the sqe are validated and then later
1949 * used, it's important that those reads are done through READ_ONCE() to
1950 * prevent a re-load down the line.
1951 */
1952static bool io_get_sqring(struct io_ring_ctx *ctx, struct sqe_submit *s)
1953{
1954 struct io_sq_ring *ring = ctx->sq_ring;
1955 unsigned head;
1956
1957 /*
1958 * The cached sq head (or cq tail) serves two purposes:
1959 *
1960 * 1) allows us to batch the cost of updating the user visible
1961 * head updates.
1962 * 2) allows the kernel side to track the head on its own, even
1963 * though the application is the one updating it.
1964 */
1965 head = ctx->cached_sq_head;
Stefan Bühlere523a292019-04-19 11:57:44 +02001966 /* make sure SQ entry isn't read before tail */
1967 if (head == smp_load_acquire(&ring->r.tail))
Jens Axboe2b188cc2019-01-07 10:46:33 -07001968 return false;
1969
1970 head = READ_ONCE(ring->array[head & ctx->sq_mask]);
1971 if (head < ctx->sq_entries) {
1972 s->index = head;
1973 s->sqe = &ctx->sq_sqes[head];
1974 ctx->cached_sq_head++;
1975 return true;
1976 }
1977
1978 /* drop invalid entries */
1979 ctx->cached_sq_head++;
1980 ring->dropped++;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001981 return false;
1982}
1983
Jens Axboe6c271ce2019-01-10 11:22:30 -07001984static int io_submit_sqes(struct io_ring_ctx *ctx, struct sqe_submit *sqes,
1985 unsigned int nr, bool has_user, bool mm_fault)
1986{
1987 struct io_submit_state state, *statep = NULL;
1988 int ret, i, submitted = 0;
1989
1990 if (nr > IO_PLUG_THRESHOLD) {
1991 io_submit_state_start(&state, ctx, nr);
1992 statep = &state;
1993 }
1994
1995 for (i = 0; i < nr; i++) {
1996 if (unlikely(mm_fault)) {
1997 ret = -EFAULT;
1998 } else {
1999 sqes[i].has_user = has_user;
2000 sqes[i].needs_lock = true;
2001 sqes[i].needs_fixed_file = true;
2002 ret = io_submit_sqe(ctx, &sqes[i], statep);
2003 }
2004 if (!ret) {
2005 submitted++;
2006 continue;
2007 }
2008
2009 io_cqring_add_event(ctx, sqes[i].sqe->user_data, ret, 0);
2010 }
2011
2012 if (statep)
2013 io_submit_state_end(&state);
2014
2015 return submitted;
2016}
2017
2018static int io_sq_thread(void *data)
2019{
2020 struct sqe_submit sqes[IO_IOPOLL_BATCH];
2021 struct io_ring_ctx *ctx = data;
2022 struct mm_struct *cur_mm = NULL;
2023 mm_segment_t old_fs;
2024 DEFINE_WAIT(wait);
2025 unsigned inflight;
2026 unsigned long timeout;
2027
2028 old_fs = get_fs();
2029 set_fs(USER_DS);
2030
2031 timeout = inflight = 0;
2032 while (!kthread_should_stop() && !ctx->sqo_stop) {
2033 bool all_fixed, mm_fault = false;
2034 int i;
2035
2036 if (inflight) {
2037 unsigned nr_events = 0;
2038
2039 if (ctx->flags & IORING_SETUP_IOPOLL) {
2040 /*
2041 * We disallow the app entering submit/complete
2042 * with polling, but we still need to lock the
2043 * ring to prevent racing with polled issue
2044 * that got punted to a workqueue.
2045 */
2046 mutex_lock(&ctx->uring_lock);
2047 io_iopoll_check(ctx, &nr_events, 0);
2048 mutex_unlock(&ctx->uring_lock);
2049 } else {
2050 /*
2051 * Normal IO, just pretend everything completed.
2052 * We don't have to poll completions for that.
2053 */
2054 nr_events = inflight;
2055 }
2056
2057 inflight -= nr_events;
2058 if (!inflight)
2059 timeout = jiffies + ctx->sq_thread_idle;
2060 }
2061
2062 if (!io_get_sqring(ctx, &sqes[0])) {
2063 /*
2064 * We're polling. If we're within the defined idle
2065 * period, then let us spin without work before going
2066 * to sleep.
2067 */
2068 if (inflight || !time_after(jiffies, timeout)) {
2069 cpu_relax();
2070 continue;
2071 }
2072
2073 /*
2074 * Drop cur_mm before scheduling, we can't hold it for
2075 * long periods (or over schedule()). Do this before
2076 * adding ourselves to the waitqueue, as the unuse/drop
2077 * may sleep.
2078 */
2079 if (cur_mm) {
2080 unuse_mm(cur_mm);
2081 mmput(cur_mm);
2082 cur_mm = NULL;
2083 }
2084
2085 prepare_to_wait(&ctx->sqo_wait, &wait,
2086 TASK_INTERRUPTIBLE);
2087
2088 /* Tell userspace we may need a wakeup call */
2089 ctx->sq_ring->flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02002090 /* make sure to read SQ tail after writing flags */
2091 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07002092
2093 if (!io_get_sqring(ctx, &sqes[0])) {
2094 if (kthread_should_stop()) {
2095 finish_wait(&ctx->sqo_wait, &wait);
2096 break;
2097 }
2098 if (signal_pending(current))
2099 flush_signals(current);
2100 schedule();
2101 finish_wait(&ctx->sqo_wait, &wait);
2102
2103 ctx->sq_ring->flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002104 continue;
2105 }
2106 finish_wait(&ctx->sqo_wait, &wait);
2107
2108 ctx->sq_ring->flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002109 }
2110
2111 i = 0;
2112 all_fixed = true;
2113 do {
2114 if (all_fixed && io_sqe_needs_user(sqes[i].sqe))
2115 all_fixed = false;
2116
2117 i++;
2118 if (i == ARRAY_SIZE(sqes))
2119 break;
2120 } while (io_get_sqring(ctx, &sqes[i]));
2121
2122 /* Unless all new commands are FIXED regions, grab mm */
2123 if (!all_fixed && !cur_mm) {
2124 mm_fault = !mmget_not_zero(ctx->sqo_mm);
2125 if (!mm_fault) {
2126 use_mm(ctx->sqo_mm);
2127 cur_mm = ctx->sqo_mm;
2128 }
2129 }
2130
2131 inflight += io_submit_sqes(ctx, sqes, i, cur_mm != NULL,
2132 mm_fault);
2133
2134 /* Commit SQ ring head once we've consumed all SQEs */
2135 io_commit_sqring(ctx);
2136 }
2137
2138 set_fs(old_fs);
2139 if (cur_mm) {
2140 unuse_mm(cur_mm);
2141 mmput(cur_mm);
2142 }
Jens Axboe06058632019-04-13 09:26:03 -06002143
2144 if (kthread_should_park())
2145 kthread_parkme();
2146
Jens Axboe6c271ce2019-01-10 11:22:30 -07002147 return 0;
2148}
2149
Jens Axboe2b188cc2019-01-07 10:46:33 -07002150static int io_ring_submit(struct io_ring_ctx *ctx, unsigned int to_submit)
2151{
Jens Axboe9a56a232019-01-09 09:06:50 -07002152 struct io_submit_state state, *statep = NULL;
Jens Axboe5c8b0b52019-04-30 10:16:07 -06002153 int i, submit = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002154
Jens Axboe9a56a232019-01-09 09:06:50 -07002155 if (to_submit > IO_PLUG_THRESHOLD) {
2156 io_submit_state_start(&state, ctx, to_submit);
2157 statep = &state;
2158 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002159
2160 for (i = 0; i < to_submit; i++) {
2161 struct sqe_submit s;
Jens Axboe5c8b0b52019-04-30 10:16:07 -06002162 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002163
2164 if (!io_get_sqring(ctx, &s))
2165 break;
2166
2167 s.has_user = true;
Jens Axboedef596e2019-01-09 08:59:42 -07002168 s.needs_lock = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002169 s.needs_fixed_file = false;
Jens Axboe5c8b0b52019-04-30 10:16:07 -06002170 submit++;
Jens Axboedef596e2019-01-09 08:59:42 -07002171
Jens Axboe9a56a232019-01-09 09:06:50 -07002172 ret = io_submit_sqe(ctx, &s, statep);
Jens Axboe5c8b0b52019-04-30 10:16:07 -06002173 if (ret)
2174 io_cqring_add_event(ctx, s.sqe->user_data, ret, 0);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002175 }
2176 io_commit_sqring(ctx);
2177
Jens Axboe9a56a232019-01-09 09:06:50 -07002178 if (statep)
2179 io_submit_state_end(statep);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002180
Jens Axboe5c8b0b52019-04-30 10:16:07 -06002181 return submit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002182}
2183
2184static unsigned io_cqring_events(struct io_cq_ring *ring)
2185{
2186 return READ_ONCE(ring->r.tail) - READ_ONCE(ring->r.head);
2187}
2188
2189/*
2190 * Wait until events become available, if we don't already have some. The
2191 * application must reap them itself, as they reside on the shared cq ring.
2192 */
2193static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
2194 const sigset_t __user *sig, size_t sigsz)
2195{
2196 struct io_cq_ring *ring = ctx->cq_ring;
2197 sigset_t ksigmask, sigsaved;
2198 DEFINE_WAIT(wait);
2199 int ret;
2200
2201 /* See comment at the top of this file */
2202 smp_rmb();
2203 if (io_cqring_events(ring) >= min_events)
2204 return 0;
2205
2206 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01002207#ifdef CONFIG_COMPAT
2208 if (in_compat_syscall())
2209 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
2210 &ksigmask, &sigsaved, sigsz);
2211 else
2212#endif
2213 ret = set_user_sigmask(sig, &ksigmask,
2214 &sigsaved, sigsz);
2215
Jens Axboe2b188cc2019-01-07 10:46:33 -07002216 if (ret)
2217 return ret;
2218 }
2219
2220 do {
2221 prepare_to_wait(&ctx->wait, &wait, TASK_INTERRUPTIBLE);
2222
2223 ret = 0;
2224 /* See comment at the top of this file */
2225 smp_rmb();
2226 if (io_cqring_events(ring) >= min_events)
2227 break;
2228
2229 schedule();
2230
2231 ret = -EINTR;
2232 if (signal_pending(current))
2233 break;
2234 } while (1);
2235
2236 finish_wait(&ctx->wait, &wait);
2237
2238 if (sig)
2239 restore_user_sigmask(sig, &sigsaved);
2240
2241 return READ_ONCE(ring->r.head) == READ_ONCE(ring->r.tail) ? ret : 0;
2242}
2243
Jens Axboe6b063142019-01-10 22:13:58 -07002244static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
2245{
2246#if defined(CONFIG_UNIX)
2247 if (ctx->ring_sock) {
2248 struct sock *sock = ctx->ring_sock->sk;
2249 struct sk_buff *skb;
2250
2251 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
2252 kfree_skb(skb);
2253 }
2254#else
2255 int i;
2256
2257 for (i = 0; i < ctx->nr_user_files; i++)
2258 fput(ctx->user_files[i]);
2259#endif
2260}
2261
2262static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
2263{
2264 if (!ctx->user_files)
2265 return -ENXIO;
2266
2267 __io_sqe_files_unregister(ctx);
2268 kfree(ctx->user_files);
2269 ctx->user_files = NULL;
2270 ctx->nr_user_files = 0;
2271 return 0;
2272}
2273
Jens Axboe6c271ce2019-01-10 11:22:30 -07002274static void io_sq_thread_stop(struct io_ring_ctx *ctx)
2275{
2276 if (ctx->sqo_thread) {
2277 ctx->sqo_stop = 1;
2278 mb();
Jens Axboe06058632019-04-13 09:26:03 -06002279 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07002280 kthread_stop(ctx->sqo_thread);
2281 ctx->sqo_thread = NULL;
2282 }
2283}
2284
Jens Axboe6b063142019-01-10 22:13:58 -07002285static void io_finish_async(struct io_ring_ctx *ctx)
2286{
Jens Axboe6c271ce2019-01-10 11:22:30 -07002287 io_sq_thread_stop(ctx);
2288
Jens Axboe6b063142019-01-10 22:13:58 -07002289 if (ctx->sqo_wq) {
2290 destroy_workqueue(ctx->sqo_wq);
2291 ctx->sqo_wq = NULL;
2292 }
2293}
2294
2295#if defined(CONFIG_UNIX)
2296static void io_destruct_skb(struct sk_buff *skb)
2297{
2298 struct io_ring_ctx *ctx = skb->sk->sk_user_data;
2299
2300 io_finish_async(ctx);
2301 unix_destruct_scm(skb);
2302}
2303
2304/*
2305 * Ensure the UNIX gc is aware of our file set, so we are certain that
2306 * the io_uring can be safely unregistered on process exit, even if we have
2307 * loops in the file referencing.
2308 */
2309static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
2310{
2311 struct sock *sk = ctx->ring_sock->sk;
2312 struct scm_fp_list *fpl;
2313 struct sk_buff *skb;
2314 int i;
2315
2316 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
2317 unsigned long inflight = ctx->user->unix_inflight + nr;
2318
2319 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
2320 return -EMFILE;
2321 }
2322
2323 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
2324 if (!fpl)
2325 return -ENOMEM;
2326
2327 skb = alloc_skb(0, GFP_KERNEL);
2328 if (!skb) {
2329 kfree(fpl);
2330 return -ENOMEM;
2331 }
2332
2333 skb->sk = sk;
2334 skb->destructor = io_destruct_skb;
2335
2336 fpl->user = get_uid(ctx->user);
2337 for (i = 0; i < nr; i++) {
2338 fpl->fp[i] = get_file(ctx->user_files[i + offset]);
2339 unix_inflight(fpl->user, fpl->fp[i]);
2340 }
2341
2342 fpl->max = fpl->count = nr;
2343 UNIXCB(skb).fp = fpl;
2344 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
2345 skb_queue_head(&sk->sk_receive_queue, skb);
2346
2347 for (i = 0; i < nr; i++)
2348 fput(fpl->fp[i]);
2349
2350 return 0;
2351}
2352
2353/*
2354 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
2355 * causes regular reference counting to break down. We rely on the UNIX
2356 * garbage collection to take care of this problem for us.
2357 */
2358static int io_sqe_files_scm(struct io_ring_ctx *ctx)
2359{
2360 unsigned left, total;
2361 int ret = 0;
2362
2363 total = 0;
2364 left = ctx->nr_user_files;
2365 while (left) {
2366 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
2367 int ret;
2368
2369 ret = __io_sqe_files_scm(ctx, this_files, total);
2370 if (ret)
2371 break;
2372 left -= this_files;
2373 total += this_files;
2374 }
2375
2376 if (!ret)
2377 return 0;
2378
2379 while (total < ctx->nr_user_files) {
2380 fput(ctx->user_files[total]);
2381 total++;
2382 }
2383
2384 return ret;
2385}
2386#else
2387static int io_sqe_files_scm(struct io_ring_ctx *ctx)
2388{
2389 return 0;
2390}
2391#endif
2392
2393static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
2394 unsigned nr_args)
2395{
2396 __s32 __user *fds = (__s32 __user *) arg;
2397 int fd, ret = 0;
2398 unsigned i;
2399
2400 if (ctx->user_files)
2401 return -EBUSY;
2402 if (!nr_args)
2403 return -EINVAL;
2404 if (nr_args > IORING_MAX_FIXED_FILES)
2405 return -EMFILE;
2406
2407 ctx->user_files = kcalloc(nr_args, sizeof(struct file *), GFP_KERNEL);
2408 if (!ctx->user_files)
2409 return -ENOMEM;
2410
2411 for (i = 0; i < nr_args; i++) {
2412 ret = -EFAULT;
2413 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
2414 break;
2415
2416 ctx->user_files[i] = fget(fd);
2417
2418 ret = -EBADF;
2419 if (!ctx->user_files[i])
2420 break;
2421 /*
2422 * Don't allow io_uring instances to be registered. If UNIX
2423 * isn't enabled, then this causes a reference cycle and this
2424 * instance can never get freed. If UNIX is enabled we'll
2425 * handle it just fine, but there's still no point in allowing
2426 * a ring fd as it doesn't support regular read/write anyway.
2427 */
2428 if (ctx->user_files[i]->f_op == &io_uring_fops) {
2429 fput(ctx->user_files[i]);
2430 break;
2431 }
2432 ctx->nr_user_files++;
2433 ret = 0;
2434 }
2435
2436 if (ret) {
2437 for (i = 0; i < ctx->nr_user_files; i++)
2438 fput(ctx->user_files[i]);
2439
2440 kfree(ctx->user_files);
Jens Axboe25adf502019-04-03 09:52:40 -06002441 ctx->user_files = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07002442 ctx->nr_user_files = 0;
2443 return ret;
2444 }
2445
2446 ret = io_sqe_files_scm(ctx);
2447 if (ret)
2448 io_sqe_files_unregister(ctx);
2449
2450 return ret;
2451}
2452
Jens Axboe6c271ce2019-01-10 11:22:30 -07002453static int io_sq_offload_start(struct io_ring_ctx *ctx,
2454 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002455{
2456 int ret;
2457
Jens Axboe6c271ce2019-01-10 11:22:30 -07002458 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002459 mmgrab(current->mm);
2460 ctx->sqo_mm = current->mm;
2461
Jens Axboe6c271ce2019-01-10 11:22:30 -07002462 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06002463 ret = -EPERM;
2464 if (!capable(CAP_SYS_ADMIN))
2465 goto err;
2466
Jens Axboe917257d2019-04-13 09:28:55 -06002467 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
2468 if (!ctx->sq_thread_idle)
2469 ctx->sq_thread_idle = HZ;
2470
Jens Axboe6c271ce2019-01-10 11:22:30 -07002471 if (p->flags & IORING_SETUP_SQ_AFF) {
Mark Rutland975554b2019-04-30 13:34:51 +01002472 int cpu = array_index_nospec(p->sq_thread_cpu,
2473 nr_cpu_ids);
Jens Axboe6c271ce2019-01-10 11:22:30 -07002474
Jens Axboe917257d2019-04-13 09:28:55 -06002475 ret = -EINVAL;
Mark Rutland975554b2019-04-30 13:34:51 +01002476 if (!cpu_possible(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06002477 goto err;
2478
Jens Axboe6c271ce2019-01-10 11:22:30 -07002479 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
2480 ctx, cpu,
2481 "io_uring-sq");
2482 } else {
2483 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
2484 "io_uring-sq");
2485 }
2486 if (IS_ERR(ctx->sqo_thread)) {
2487 ret = PTR_ERR(ctx->sqo_thread);
2488 ctx->sqo_thread = NULL;
2489 goto err;
2490 }
2491 wake_up_process(ctx->sqo_thread);
2492 } else if (p->flags & IORING_SETUP_SQ_AFF) {
2493 /* Can't have SQ_AFF without SQPOLL */
2494 ret = -EINVAL;
2495 goto err;
2496 }
2497
Jens Axboe2b188cc2019-01-07 10:46:33 -07002498 /* Do QD, or 2 * CPUS, whatever is smallest */
2499 ctx->sqo_wq = alloc_workqueue("io_ring-wq", WQ_UNBOUND | WQ_FREEZABLE,
2500 min(ctx->sq_entries - 1, 2 * num_online_cpus()));
2501 if (!ctx->sqo_wq) {
2502 ret = -ENOMEM;
2503 goto err;
2504 }
2505
2506 return 0;
2507err:
Jens Axboe6c271ce2019-01-10 11:22:30 -07002508 io_sq_thread_stop(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002509 mmdrop(ctx->sqo_mm);
2510 ctx->sqo_mm = NULL;
2511 return ret;
2512}
2513
2514static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
2515{
2516 atomic_long_sub(nr_pages, &user->locked_vm);
2517}
2518
2519static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
2520{
2521 unsigned long page_limit, cur_pages, new_pages;
2522
2523 /* Don't allow more pages than we can safely lock */
2524 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
2525
2526 do {
2527 cur_pages = atomic_long_read(&user->locked_vm);
2528 new_pages = cur_pages + nr_pages;
2529 if (new_pages > page_limit)
2530 return -ENOMEM;
2531 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
2532 new_pages) != cur_pages);
2533
2534 return 0;
2535}
2536
2537static void io_mem_free(void *ptr)
2538{
Mark Rutland52e04ef2019-04-30 17:30:21 +01002539 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002540
Mark Rutland52e04ef2019-04-30 17:30:21 +01002541 if (!ptr)
2542 return;
2543
2544 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002545 if (put_page_testzero(page))
2546 free_compound_page(page);
2547}
2548
2549static void *io_mem_alloc(size_t size)
2550{
2551 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
2552 __GFP_NORETRY;
2553
2554 return (void *) __get_free_pages(gfp_flags, get_order(size));
2555}
2556
2557static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
2558{
2559 struct io_sq_ring *sq_ring;
2560 struct io_cq_ring *cq_ring;
2561 size_t bytes;
2562
2563 bytes = struct_size(sq_ring, array, sq_entries);
2564 bytes += array_size(sizeof(struct io_uring_sqe), sq_entries);
2565 bytes += struct_size(cq_ring, cqes, cq_entries);
2566
2567 return (bytes + PAGE_SIZE - 1) / PAGE_SIZE;
2568}
2569
Jens Axboeedafcce2019-01-09 09:16:05 -07002570static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
2571{
2572 int i, j;
2573
2574 if (!ctx->user_bufs)
2575 return -ENXIO;
2576
2577 for (i = 0; i < ctx->nr_user_bufs; i++) {
2578 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
2579
2580 for (j = 0; j < imu->nr_bvecs; j++)
2581 put_page(imu->bvec[j].bv_page);
2582
2583 if (ctx->account_mem)
2584 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01002585 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07002586 imu->nr_bvecs = 0;
2587 }
2588
2589 kfree(ctx->user_bufs);
2590 ctx->user_bufs = NULL;
2591 ctx->nr_user_bufs = 0;
2592 return 0;
2593}
2594
2595static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
2596 void __user *arg, unsigned index)
2597{
2598 struct iovec __user *src;
2599
2600#ifdef CONFIG_COMPAT
2601 if (ctx->compat) {
2602 struct compat_iovec __user *ciovs;
2603 struct compat_iovec ciov;
2604
2605 ciovs = (struct compat_iovec __user *) arg;
2606 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
2607 return -EFAULT;
2608
2609 dst->iov_base = (void __user *) (unsigned long) ciov.iov_base;
2610 dst->iov_len = ciov.iov_len;
2611 return 0;
2612 }
2613#endif
2614 src = (struct iovec __user *) arg;
2615 if (copy_from_user(dst, &src[index], sizeof(*dst)))
2616 return -EFAULT;
2617 return 0;
2618}
2619
2620static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
2621 unsigned nr_args)
2622{
2623 struct vm_area_struct **vmas = NULL;
2624 struct page **pages = NULL;
2625 int i, j, got_pages = 0;
2626 int ret = -EINVAL;
2627
2628 if (ctx->user_bufs)
2629 return -EBUSY;
2630 if (!nr_args || nr_args > UIO_MAXIOV)
2631 return -EINVAL;
2632
2633 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
2634 GFP_KERNEL);
2635 if (!ctx->user_bufs)
2636 return -ENOMEM;
2637
2638 for (i = 0; i < nr_args; i++) {
2639 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
2640 unsigned long off, start, end, ubuf;
2641 int pret, nr_pages;
2642 struct iovec iov;
2643 size_t size;
2644
2645 ret = io_copy_iov(ctx, &iov, arg, i);
2646 if (ret)
2647 break;
2648
2649 /*
2650 * Don't impose further limits on the size and buffer
2651 * constraints here, we'll -EINVAL later when IO is
2652 * submitted if they are wrong.
2653 */
2654 ret = -EFAULT;
2655 if (!iov.iov_base || !iov.iov_len)
2656 goto err;
2657
2658 /* arbitrary limit, but we need something */
2659 if (iov.iov_len > SZ_1G)
2660 goto err;
2661
2662 ubuf = (unsigned long) iov.iov_base;
2663 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
2664 start = ubuf >> PAGE_SHIFT;
2665 nr_pages = end - start;
2666
2667 if (ctx->account_mem) {
2668 ret = io_account_mem(ctx->user, nr_pages);
2669 if (ret)
2670 goto err;
2671 }
2672
2673 ret = 0;
2674 if (!pages || nr_pages > got_pages) {
2675 kfree(vmas);
2676 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01002677 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07002678 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01002679 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07002680 sizeof(struct vm_area_struct *),
2681 GFP_KERNEL);
2682 if (!pages || !vmas) {
2683 ret = -ENOMEM;
2684 if (ctx->account_mem)
2685 io_unaccount_mem(ctx->user, nr_pages);
2686 goto err;
2687 }
2688 got_pages = nr_pages;
2689 }
2690
Mark Rutlandd4ef6472019-05-01 16:59:16 +01002691 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07002692 GFP_KERNEL);
2693 ret = -ENOMEM;
2694 if (!imu->bvec) {
2695 if (ctx->account_mem)
2696 io_unaccount_mem(ctx->user, nr_pages);
2697 goto err;
2698 }
2699
2700 ret = 0;
2701 down_read(&current->mm->mmap_sem);
2702 pret = get_user_pages_longterm(ubuf, nr_pages, FOLL_WRITE,
2703 pages, vmas);
2704 if (pret == nr_pages) {
2705 /* don't support file backed memory */
2706 for (j = 0; j < nr_pages; j++) {
2707 struct vm_area_struct *vma = vmas[j];
2708
2709 if (vma->vm_file &&
2710 !is_file_hugepages(vma->vm_file)) {
2711 ret = -EOPNOTSUPP;
2712 break;
2713 }
2714 }
2715 } else {
2716 ret = pret < 0 ? pret : -EFAULT;
2717 }
2718 up_read(&current->mm->mmap_sem);
2719 if (ret) {
2720 /*
2721 * if we did partial map, or found file backed vmas,
2722 * release any pages we did get
2723 */
2724 if (pret > 0) {
2725 for (j = 0; j < pret; j++)
2726 put_page(pages[j]);
2727 }
2728 if (ctx->account_mem)
2729 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01002730 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07002731 goto err;
2732 }
2733
2734 off = ubuf & ~PAGE_MASK;
2735 size = iov.iov_len;
2736 for (j = 0; j < nr_pages; j++) {
2737 size_t vec_len;
2738
2739 vec_len = min_t(size_t, size, PAGE_SIZE - off);
2740 imu->bvec[j].bv_page = pages[j];
2741 imu->bvec[j].bv_len = vec_len;
2742 imu->bvec[j].bv_offset = off;
2743 off = 0;
2744 size -= vec_len;
2745 }
2746 /* store original address for later verification */
2747 imu->ubuf = ubuf;
2748 imu->len = iov.iov_len;
2749 imu->nr_bvecs = nr_pages;
2750
2751 ctx->nr_user_bufs++;
2752 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01002753 kvfree(pages);
2754 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07002755 return 0;
2756err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01002757 kvfree(pages);
2758 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07002759 io_sqe_buffer_unregister(ctx);
2760 return ret;
2761}
2762
Jens Axboe9b402842019-04-11 11:45:41 -06002763static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
2764{
2765 __s32 __user *fds = arg;
2766 int fd;
2767
2768 if (ctx->cq_ev_fd)
2769 return -EBUSY;
2770
2771 if (copy_from_user(&fd, fds, sizeof(*fds)))
2772 return -EFAULT;
2773
2774 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
2775 if (IS_ERR(ctx->cq_ev_fd)) {
2776 int ret = PTR_ERR(ctx->cq_ev_fd);
2777 ctx->cq_ev_fd = NULL;
2778 return ret;
2779 }
2780
2781 return 0;
2782}
2783
2784static int io_eventfd_unregister(struct io_ring_ctx *ctx)
2785{
2786 if (ctx->cq_ev_fd) {
2787 eventfd_ctx_put(ctx->cq_ev_fd);
2788 ctx->cq_ev_fd = NULL;
2789 return 0;
2790 }
2791
2792 return -ENXIO;
2793}
2794
Jens Axboe2b188cc2019-01-07 10:46:33 -07002795static void io_ring_ctx_free(struct io_ring_ctx *ctx)
2796{
Jens Axboe6b063142019-01-10 22:13:58 -07002797 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002798 if (ctx->sqo_mm)
2799 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07002800
2801 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07002802 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07002803 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06002804 io_eventfd_unregister(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07002805
Jens Axboe2b188cc2019-01-07 10:46:33 -07002806#if defined(CONFIG_UNIX)
2807 if (ctx->ring_sock)
2808 sock_release(ctx->ring_sock);
2809#endif
2810
2811 io_mem_free(ctx->sq_ring);
2812 io_mem_free(ctx->sq_sqes);
2813 io_mem_free(ctx->cq_ring);
2814
2815 percpu_ref_exit(&ctx->refs);
2816 if (ctx->account_mem)
2817 io_unaccount_mem(ctx->user,
2818 ring_pages(ctx->sq_entries, ctx->cq_entries));
2819 free_uid(ctx->user);
2820 kfree(ctx);
2821}
2822
2823static __poll_t io_uring_poll(struct file *file, poll_table *wait)
2824{
2825 struct io_ring_ctx *ctx = file->private_data;
2826 __poll_t mask = 0;
2827
2828 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02002829 /*
2830 * synchronizes with barrier from wq_has_sleeper call in
2831 * io_commit_cqring
2832 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07002833 smp_rmb();
Stefan Bühlerfb775fa2019-04-19 11:57:46 +02002834 if (READ_ONCE(ctx->sq_ring->r.tail) - ctx->cached_sq_head !=
2835 ctx->sq_ring->ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002836 mask |= EPOLLOUT | EPOLLWRNORM;
2837 if (READ_ONCE(ctx->cq_ring->r.head) != ctx->cached_cq_tail)
2838 mask |= EPOLLIN | EPOLLRDNORM;
2839
2840 return mask;
2841}
2842
2843static int io_uring_fasync(int fd, struct file *file, int on)
2844{
2845 struct io_ring_ctx *ctx = file->private_data;
2846
2847 return fasync_helper(fd, file, on, &ctx->cq_fasync);
2848}
2849
2850static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
2851{
2852 mutex_lock(&ctx->uring_lock);
2853 percpu_ref_kill(&ctx->refs);
2854 mutex_unlock(&ctx->uring_lock);
2855
Jens Axboe221c5eb2019-01-17 09:41:58 -07002856 io_poll_remove_all(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07002857 io_iopoll_reap_events(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002858 wait_for_completion(&ctx->ctx_done);
2859 io_ring_ctx_free(ctx);
2860}
2861
2862static int io_uring_release(struct inode *inode, struct file *file)
2863{
2864 struct io_ring_ctx *ctx = file->private_data;
2865
2866 file->private_data = NULL;
2867 io_ring_ctx_wait_and_kill(ctx);
2868 return 0;
2869}
2870
2871static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
2872{
2873 loff_t offset = (loff_t) vma->vm_pgoff << PAGE_SHIFT;
2874 unsigned long sz = vma->vm_end - vma->vm_start;
2875 struct io_ring_ctx *ctx = file->private_data;
2876 unsigned long pfn;
2877 struct page *page;
2878 void *ptr;
2879
2880 switch (offset) {
2881 case IORING_OFF_SQ_RING:
2882 ptr = ctx->sq_ring;
2883 break;
2884 case IORING_OFF_SQES:
2885 ptr = ctx->sq_sqes;
2886 break;
2887 case IORING_OFF_CQ_RING:
2888 ptr = ctx->cq_ring;
2889 break;
2890 default:
2891 return -EINVAL;
2892 }
2893
2894 page = virt_to_head_page(ptr);
2895 if (sz > (PAGE_SIZE << compound_order(page)))
2896 return -EINVAL;
2897
2898 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
2899 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
2900}
2901
2902SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
2903 u32, min_complete, u32, flags, const sigset_t __user *, sig,
2904 size_t, sigsz)
2905{
2906 struct io_ring_ctx *ctx;
2907 long ret = -EBADF;
2908 int submitted = 0;
2909 struct fd f;
2910
Jens Axboe6c271ce2019-01-10 11:22:30 -07002911 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002912 return -EINVAL;
2913
2914 f = fdget(fd);
2915 if (!f.file)
2916 return -EBADF;
2917
2918 ret = -EOPNOTSUPP;
2919 if (f.file->f_op != &io_uring_fops)
2920 goto out_fput;
2921
2922 ret = -ENXIO;
2923 ctx = f.file->private_data;
2924 if (!percpu_ref_tryget(&ctx->refs))
2925 goto out_fput;
2926
Jens Axboe6c271ce2019-01-10 11:22:30 -07002927 /*
2928 * For SQ polling, the thread will do all submissions and completions.
2929 * Just return the requested submit count, and wake the thread if
2930 * we were asked to.
2931 */
2932 if (ctx->flags & IORING_SETUP_SQPOLL) {
2933 if (flags & IORING_ENTER_SQ_WAKEUP)
2934 wake_up(&ctx->sqo_wait);
2935 submitted = to_submit;
2936 goto out_ctx;
2937 }
2938
Jens Axboe2b188cc2019-01-07 10:46:33 -07002939 ret = 0;
2940 if (to_submit) {
2941 to_submit = min(to_submit, ctx->sq_entries);
2942
2943 mutex_lock(&ctx->uring_lock);
2944 submitted = io_ring_submit(ctx, to_submit);
2945 mutex_unlock(&ctx->uring_lock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002946 }
2947 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07002948 unsigned nr_events = 0;
2949
Jens Axboe2b188cc2019-01-07 10:46:33 -07002950 min_complete = min(min_complete, ctx->cq_entries);
2951
Jens Axboedef596e2019-01-09 08:59:42 -07002952 if (ctx->flags & IORING_SETUP_IOPOLL) {
2953 mutex_lock(&ctx->uring_lock);
2954 ret = io_iopoll_check(ctx, &nr_events, min_complete);
2955 mutex_unlock(&ctx->uring_lock);
2956 } else {
2957 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
2958 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002959 }
2960
2961out_ctx:
2962 io_ring_drop_ctx_refs(ctx, 1);
2963out_fput:
2964 fdput(f);
2965 return submitted ? submitted : ret;
2966}
2967
2968static const struct file_operations io_uring_fops = {
2969 .release = io_uring_release,
2970 .mmap = io_uring_mmap,
2971 .poll = io_uring_poll,
2972 .fasync = io_uring_fasync,
2973};
2974
2975static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
2976 struct io_uring_params *p)
2977{
2978 struct io_sq_ring *sq_ring;
2979 struct io_cq_ring *cq_ring;
2980 size_t size;
2981
2982 sq_ring = io_mem_alloc(struct_size(sq_ring, array, p->sq_entries));
2983 if (!sq_ring)
2984 return -ENOMEM;
2985
2986 ctx->sq_ring = sq_ring;
2987 sq_ring->ring_mask = p->sq_entries - 1;
2988 sq_ring->ring_entries = p->sq_entries;
2989 ctx->sq_mask = sq_ring->ring_mask;
2990 ctx->sq_entries = sq_ring->ring_entries;
2991
2992 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
2993 if (size == SIZE_MAX)
2994 return -EOVERFLOW;
2995
2996 ctx->sq_sqes = io_mem_alloc(size);
Mark Rutland52e04ef2019-04-30 17:30:21 +01002997 if (!ctx->sq_sqes)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002998 return -ENOMEM;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002999
3000 cq_ring = io_mem_alloc(struct_size(cq_ring, cqes, p->cq_entries));
Mark Rutland52e04ef2019-04-30 17:30:21 +01003001 if (!cq_ring)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003002 return -ENOMEM;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003003
3004 ctx->cq_ring = cq_ring;
3005 cq_ring->ring_mask = p->cq_entries - 1;
3006 cq_ring->ring_entries = p->cq_entries;
3007 ctx->cq_mask = cq_ring->ring_mask;
3008 ctx->cq_entries = cq_ring->ring_entries;
3009 return 0;
3010}
3011
3012/*
3013 * Allocate an anonymous fd, this is what constitutes the application
3014 * visible backing of an io_uring instance. The application mmaps this
3015 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
3016 * we have to tie this fd to a socket for file garbage collection purposes.
3017 */
3018static int io_uring_get_fd(struct io_ring_ctx *ctx)
3019{
3020 struct file *file;
3021 int ret;
3022
3023#if defined(CONFIG_UNIX)
3024 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
3025 &ctx->ring_sock);
3026 if (ret)
3027 return ret;
3028#endif
3029
3030 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
3031 if (ret < 0)
3032 goto err;
3033
3034 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
3035 O_RDWR | O_CLOEXEC);
3036 if (IS_ERR(file)) {
3037 put_unused_fd(ret);
3038 ret = PTR_ERR(file);
3039 goto err;
3040 }
3041
3042#if defined(CONFIG_UNIX)
3043 ctx->ring_sock->file = file;
Jens Axboe6b063142019-01-10 22:13:58 -07003044 ctx->ring_sock->sk->sk_user_data = ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003045#endif
3046 fd_install(ret, file);
3047 return ret;
3048err:
3049#if defined(CONFIG_UNIX)
3050 sock_release(ctx->ring_sock);
3051 ctx->ring_sock = NULL;
3052#endif
3053 return ret;
3054}
3055
3056static int io_uring_create(unsigned entries, struct io_uring_params *p)
3057{
3058 struct user_struct *user = NULL;
3059 struct io_ring_ctx *ctx;
3060 bool account_mem;
3061 int ret;
3062
3063 if (!entries || entries > IORING_MAX_ENTRIES)
3064 return -EINVAL;
3065
3066 /*
3067 * Use twice as many entries for the CQ ring. It's possible for the
3068 * application to drive a higher depth than the size of the SQ ring,
3069 * since the sqes are only used at submission time. This allows for
3070 * some flexibility in overcommitting a bit.
3071 */
3072 p->sq_entries = roundup_pow_of_two(entries);
3073 p->cq_entries = 2 * p->sq_entries;
3074
3075 user = get_uid(current_user());
3076 account_mem = !capable(CAP_IPC_LOCK);
3077
3078 if (account_mem) {
3079 ret = io_account_mem(user,
3080 ring_pages(p->sq_entries, p->cq_entries));
3081 if (ret) {
3082 free_uid(user);
3083 return ret;
3084 }
3085 }
3086
3087 ctx = io_ring_ctx_alloc(p);
3088 if (!ctx) {
3089 if (account_mem)
3090 io_unaccount_mem(user, ring_pages(p->sq_entries,
3091 p->cq_entries));
3092 free_uid(user);
3093 return -ENOMEM;
3094 }
3095 ctx->compat = in_compat_syscall();
3096 ctx->account_mem = account_mem;
3097 ctx->user = user;
3098
3099 ret = io_allocate_scq_urings(ctx, p);
3100 if (ret)
3101 goto err;
3102
Jens Axboe6c271ce2019-01-10 11:22:30 -07003103 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003104 if (ret)
3105 goto err;
3106
3107 ret = io_uring_get_fd(ctx);
3108 if (ret < 0)
3109 goto err;
3110
3111 memset(&p->sq_off, 0, sizeof(p->sq_off));
3112 p->sq_off.head = offsetof(struct io_sq_ring, r.head);
3113 p->sq_off.tail = offsetof(struct io_sq_ring, r.tail);
3114 p->sq_off.ring_mask = offsetof(struct io_sq_ring, ring_mask);
3115 p->sq_off.ring_entries = offsetof(struct io_sq_ring, ring_entries);
3116 p->sq_off.flags = offsetof(struct io_sq_ring, flags);
3117 p->sq_off.dropped = offsetof(struct io_sq_ring, dropped);
3118 p->sq_off.array = offsetof(struct io_sq_ring, array);
3119
3120 memset(&p->cq_off, 0, sizeof(p->cq_off));
3121 p->cq_off.head = offsetof(struct io_cq_ring, r.head);
3122 p->cq_off.tail = offsetof(struct io_cq_ring, r.tail);
3123 p->cq_off.ring_mask = offsetof(struct io_cq_ring, ring_mask);
3124 p->cq_off.ring_entries = offsetof(struct io_cq_ring, ring_entries);
3125 p->cq_off.overflow = offsetof(struct io_cq_ring, overflow);
3126 p->cq_off.cqes = offsetof(struct io_cq_ring, cqes);
3127 return ret;
3128err:
3129 io_ring_ctx_wait_and_kill(ctx);
3130 return ret;
3131}
3132
3133/*
3134 * Sets up an aio uring context, and returns the fd. Applications asks for a
3135 * ring size, we return the actual sq/cq ring sizes (among other things) in the
3136 * params structure passed in.
3137 */
3138static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
3139{
3140 struct io_uring_params p;
3141 long ret;
3142 int i;
3143
3144 if (copy_from_user(&p, params, sizeof(p)))
3145 return -EFAULT;
3146 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
3147 if (p.resv[i])
3148 return -EINVAL;
3149 }
3150
Jens Axboe6c271ce2019-01-10 11:22:30 -07003151 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
3152 IORING_SETUP_SQ_AFF))
Jens Axboe2b188cc2019-01-07 10:46:33 -07003153 return -EINVAL;
3154
3155 ret = io_uring_create(entries, &p);
3156 if (ret < 0)
3157 return ret;
3158
3159 if (copy_to_user(params, &p, sizeof(p)))
3160 return -EFAULT;
3161
3162 return ret;
3163}
3164
3165SYSCALL_DEFINE2(io_uring_setup, u32, entries,
3166 struct io_uring_params __user *, params)
3167{
3168 return io_uring_setup(entries, params);
3169}
3170
Jens Axboeedafcce2019-01-09 09:16:05 -07003171static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
3172 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06003173 __releases(ctx->uring_lock)
3174 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07003175{
3176 int ret;
3177
Jens Axboe35fa71a2019-04-22 10:23:23 -06003178 /*
3179 * We're inside the ring mutex, if the ref is already dying, then
3180 * someone else killed the ctx or is already going through
3181 * io_uring_register().
3182 */
3183 if (percpu_ref_is_dying(&ctx->refs))
3184 return -ENXIO;
3185
Jens Axboeedafcce2019-01-09 09:16:05 -07003186 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06003187
3188 /*
3189 * Drop uring mutex before waiting for references to exit. If another
3190 * thread is currently inside io_uring_enter() it might need to grab
3191 * the uring_lock to make progress. If we hold it here across the drain
3192 * wait, then we can deadlock. It's safe to drop the mutex here, since
3193 * no new references will come in after we've killed the percpu ref.
3194 */
3195 mutex_unlock(&ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07003196 wait_for_completion(&ctx->ctx_done);
Jens Axboeb19062a2019-04-15 10:49:38 -06003197 mutex_lock(&ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07003198
3199 switch (opcode) {
3200 case IORING_REGISTER_BUFFERS:
3201 ret = io_sqe_buffer_register(ctx, arg, nr_args);
3202 break;
3203 case IORING_UNREGISTER_BUFFERS:
3204 ret = -EINVAL;
3205 if (arg || nr_args)
3206 break;
3207 ret = io_sqe_buffer_unregister(ctx);
3208 break;
Jens Axboe6b063142019-01-10 22:13:58 -07003209 case IORING_REGISTER_FILES:
3210 ret = io_sqe_files_register(ctx, arg, nr_args);
3211 break;
3212 case IORING_UNREGISTER_FILES:
3213 ret = -EINVAL;
3214 if (arg || nr_args)
3215 break;
3216 ret = io_sqe_files_unregister(ctx);
3217 break;
Jens Axboe9b402842019-04-11 11:45:41 -06003218 case IORING_REGISTER_EVENTFD:
3219 ret = -EINVAL;
3220 if (nr_args != 1)
3221 break;
3222 ret = io_eventfd_register(ctx, arg);
3223 break;
3224 case IORING_UNREGISTER_EVENTFD:
3225 ret = -EINVAL;
3226 if (arg || nr_args)
3227 break;
3228 ret = io_eventfd_unregister(ctx);
3229 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07003230 default:
3231 ret = -EINVAL;
3232 break;
3233 }
3234
3235 /* bring the ctx back to life */
3236 reinit_completion(&ctx->ctx_done);
3237 percpu_ref_reinit(&ctx->refs);
3238 return ret;
3239}
3240
3241SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
3242 void __user *, arg, unsigned int, nr_args)
3243{
3244 struct io_ring_ctx *ctx;
3245 long ret = -EBADF;
3246 struct fd f;
3247
3248 f = fdget(fd);
3249 if (!f.file)
3250 return -EBADF;
3251
3252 ret = -EOPNOTSUPP;
3253 if (f.file->f_op != &io_uring_fops)
3254 goto out_fput;
3255
3256 ctx = f.file->private_data;
3257
3258 mutex_lock(&ctx->uring_lock);
3259 ret = __io_uring_register(ctx, opcode, arg, nr_args);
3260 mutex_unlock(&ctx->uring_lock);
3261out_fput:
3262 fdput(f);
3263 return ret;
3264}
3265
Jens Axboe2b188cc2019-01-07 10:46:33 -07003266static int __init io_uring_init(void)
3267{
3268 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
3269 return 0;
3270};
3271__initcall(io_uring_init);