blob: 485832deb7ea1602702bb0e063f1000b5f2c12ed [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;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700234
235 struct {
236 /* CQ ring */
237 struct io_cq_ring *cq_ring;
238 unsigned cached_cq_tail;
239 unsigned cq_entries;
240 unsigned cq_mask;
241 struct wait_queue_head cq_wait;
242 struct fasync_struct *cq_fasync;
Jens Axboe9b402842019-04-11 11:45:41 -0600243 struct eventfd_ctx *cq_ev_fd;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700244 } ____cacheline_aligned_in_smp;
245
Jens Axboe6b063142019-01-10 22:13:58 -0700246 /*
247 * If used, fixed file set. Writers must ensure that ->refs is dead,
248 * readers must ensure that ->refs is alive as long as the file* is
249 * used. Only updated through io_uring_register(2).
250 */
251 struct file **user_files;
252 unsigned nr_user_files;
253
Jens Axboeedafcce2019-01-09 09:16:05 -0700254 /* if used, fixed mapped user buffers */
255 unsigned nr_user_bufs;
256 struct io_mapped_ubuf *user_bufs;
257
Jens Axboe2b188cc2019-01-07 10:46:33 -0700258 struct user_struct *user;
259
260 struct completion ctx_done;
261
262 struct {
263 struct mutex uring_lock;
264 wait_queue_head_t wait;
265 } ____cacheline_aligned_in_smp;
266
267 struct {
268 spinlock_t completion_lock;
Jens Axboedef596e2019-01-09 08:59:42 -0700269 bool poll_multi_file;
270 /*
271 * ->poll_list is protected by the ctx->uring_lock for
272 * io_uring instances that don't use IORING_SETUP_SQPOLL.
273 * For SQPOLL, only the single threaded io_sq_thread() will
274 * manipulate the list, hence no extra locking is needed there.
275 */
276 struct list_head poll_list;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700277 struct list_head cancel_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700278 } ____cacheline_aligned_in_smp;
279
Jens Axboe31b51512019-01-18 22:56:34 -0700280 struct async_list pending_async[2];
281
Jens Axboe2b188cc2019-01-07 10:46:33 -0700282#if defined(CONFIG_UNIX)
283 struct socket *ring_sock;
284#endif
285};
286
287struct sqe_submit {
288 const struct io_uring_sqe *sqe;
289 unsigned short index;
290 bool has_user;
Jens Axboedef596e2019-01-09 08:59:42 -0700291 bool needs_lock;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700292 bool needs_fixed_file;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700293};
294
Jens Axboe09bb8392019-03-13 12:39:28 -0600295/*
296 * First field must be the file pointer in all the
297 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
298 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700299struct io_poll_iocb {
300 struct file *file;
301 struct wait_queue_head *head;
302 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600303 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700304 bool canceled;
305 struct wait_queue_entry wait;
306};
307
Jens Axboe09bb8392019-03-13 12:39:28 -0600308/*
309 * NOTE! Each of the iocb union members has the file pointer
310 * as the first entry in their struct definition. So you can
311 * access the file pointer through any of the sub-structs,
312 * or directly as just 'ki_filp' in this struct.
313 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700314struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700315 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600316 struct file *file;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700317 struct kiocb rw;
318 struct io_poll_iocb poll;
319 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700320
321 struct sqe_submit submit;
322
323 struct io_ring_ctx *ctx;
324 struct list_head list;
325 unsigned int flags;
Jens Axboec16361c2019-01-17 08:39:48 -0700326 refcount_t refs;
Stefan Bühler8449eed2019-04-27 20:34:19 +0200327#define REQ_F_NOWAIT 1 /* must not punt to workers */
Jens Axboedef596e2019-01-09 08:59:42 -0700328#define REQ_F_IOPOLL_COMPLETED 2 /* polled IO has completed */
Jens Axboe6b063142019-01-10 22:13:58 -0700329#define REQ_F_FIXED_FILE 4 /* ctx owns file */
Jens Axboe31b51512019-01-18 22:56:34 -0700330#define REQ_F_SEQ_PREV 8 /* sequential with previous */
Stefan Bühlere2033e32019-05-11 19:08:01 +0200331#define REQ_F_IO_DRAIN 16 /* drain existing IO first */
332#define REQ_F_IO_DRAINED 32 /* drain done */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700333 u64 user_data;
Stefan Bühler5dcf8772019-05-01 13:53:36 +0200334 u32 error; /* iopoll result from callback */
Jens Axboede0617e2019-04-06 21:51:27 -0600335 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700336
337 struct work_struct work;
338};
339
340#define IO_PLUG_THRESHOLD 2
Jens Axboedef596e2019-01-09 08:59:42 -0700341#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700342
Jens Axboe9a56a232019-01-09 09:06:50 -0700343struct io_submit_state {
344 struct blk_plug plug;
345
346 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700347 * io_kiocb alloc cache
348 */
349 void *reqs[IO_IOPOLL_BATCH];
350 unsigned int free_reqs;
351 unsigned int cur_req;
352
353 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700354 * File reference cache
355 */
356 struct file *file;
357 unsigned int fd;
358 unsigned int has_refs;
359 unsigned int used_refs;
360 unsigned int ios_left;
361};
362
Jens Axboede0617e2019-04-06 21:51:27 -0600363static void io_sq_wq_submit_work(struct work_struct *work);
364
Jens Axboe2b188cc2019-01-07 10:46:33 -0700365static struct kmem_cache *req_cachep;
366
367static const struct file_operations io_uring_fops;
368
369struct sock *io_uring_get_socket(struct file *file)
370{
371#if defined(CONFIG_UNIX)
372 if (file->f_op == &io_uring_fops) {
373 struct io_ring_ctx *ctx = file->private_data;
374
375 return ctx->ring_sock->sk;
376 }
377#endif
378 return NULL;
379}
380EXPORT_SYMBOL(io_uring_get_socket);
381
382static void io_ring_ctx_ref_free(struct percpu_ref *ref)
383{
384 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
385
386 complete(&ctx->ctx_done);
387}
388
389static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
390{
391 struct io_ring_ctx *ctx;
Jens Axboe31b51512019-01-18 22:56:34 -0700392 int i;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700393
394 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
395 if (!ctx)
396 return NULL;
397
398 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free, 0, GFP_KERNEL)) {
399 kfree(ctx);
400 return NULL;
401 }
402
403 ctx->flags = p->flags;
404 init_waitqueue_head(&ctx->cq_wait);
405 init_completion(&ctx->ctx_done);
406 mutex_init(&ctx->uring_lock);
407 init_waitqueue_head(&ctx->wait);
Jens Axboe31b51512019-01-18 22:56:34 -0700408 for (i = 0; i < ARRAY_SIZE(ctx->pending_async); i++) {
409 spin_lock_init(&ctx->pending_async[i].lock);
410 INIT_LIST_HEAD(&ctx->pending_async[i].list);
411 atomic_set(&ctx->pending_async[i].cnt, 0);
412 }
Jens Axboe2b188cc2019-01-07 10:46:33 -0700413 spin_lock_init(&ctx->completion_lock);
Jens Axboedef596e2019-01-09 08:59:42 -0700414 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboe221c5eb2019-01-17 09:41:58 -0700415 INIT_LIST_HEAD(&ctx->cancel_list);
Jens Axboede0617e2019-04-06 21:51:27 -0600416 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700417 return ctx;
418}
419
Jens Axboede0617e2019-04-06 21:51:27 -0600420static inline bool io_sequence_defer(struct io_ring_ctx *ctx,
421 struct io_kiocb *req)
422{
423 if ((req->flags & (REQ_F_IO_DRAIN|REQ_F_IO_DRAINED)) != REQ_F_IO_DRAIN)
424 return false;
425
426 return req->sequence > ctx->cached_cq_tail + ctx->sq_ring->dropped;
427}
428
429static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
430{
431 struct io_kiocb *req;
432
433 if (list_empty(&ctx->defer_list))
434 return NULL;
435
436 req = list_first_entry(&ctx->defer_list, struct io_kiocb, list);
437 if (!io_sequence_defer(ctx, req)) {
438 list_del_init(&req->list);
439 return req;
440 }
441
442 return NULL;
443}
444
445static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700446{
447 struct io_cq_ring *ring = ctx->cq_ring;
448
449 if (ctx->cached_cq_tail != READ_ONCE(ring->r.tail)) {
450 /* order cqe stores with ring update */
451 smp_store_release(&ring->r.tail, ctx->cached_cq_tail);
452
Jens Axboe2b188cc2019-01-07 10:46:33 -0700453 if (wq_has_sleeper(&ctx->cq_wait)) {
454 wake_up_interruptible(&ctx->cq_wait);
455 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
456 }
457 }
458}
459
Jens Axboede0617e2019-04-06 21:51:27 -0600460static void io_commit_cqring(struct io_ring_ctx *ctx)
461{
462 struct io_kiocb *req;
463
464 __io_commit_cqring(ctx);
465
466 while ((req = io_get_deferred_req(ctx)) != NULL) {
467 req->flags |= REQ_F_IO_DRAINED;
468 queue_work(ctx->sqo_wq, &req->work);
469 }
470}
471
Jens Axboe2b188cc2019-01-07 10:46:33 -0700472static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
473{
474 struct io_cq_ring *ring = ctx->cq_ring;
475 unsigned tail;
476
477 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +0200478 /*
479 * writes to the cq entry need to come after reading head; the
480 * control dependency is enough as we're using WRITE_ONCE to
481 * fill the cq entry
482 */
Jens Axboe74f464e2019-04-17 08:57:48 -0600483 if (tail - READ_ONCE(ring->r.head) == ring->ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700484 return NULL;
485
486 ctx->cached_cq_tail++;
487 return &ring->cqes[tail & ctx->cq_mask];
488}
489
490static void io_cqring_fill_event(struct io_ring_ctx *ctx, u64 ki_user_data,
Jens Axboec71ffb62019-05-13 20:58:29 -0600491 long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700492{
493 struct io_uring_cqe *cqe;
494
495 /*
496 * If we can't get a cq entry, userspace overflowed the
497 * submission (by quite a lot). Increment the overflow count in
498 * the ring.
499 */
500 cqe = io_get_cqring(ctx);
501 if (cqe) {
502 WRITE_ONCE(cqe->user_data, ki_user_data);
503 WRITE_ONCE(cqe->res, res);
Jens Axboec71ffb62019-05-13 20:58:29 -0600504 WRITE_ONCE(cqe->flags, 0);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700505 } else {
506 unsigned overflow = READ_ONCE(ctx->cq_ring->overflow);
507
508 WRITE_ONCE(ctx->cq_ring->overflow, overflow + 1);
509 }
510}
511
Jens Axboe8c838782019-03-12 15:48:16 -0600512static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
513{
514 if (waitqueue_active(&ctx->wait))
515 wake_up(&ctx->wait);
516 if (waitqueue_active(&ctx->sqo_wait))
517 wake_up(&ctx->sqo_wait);
Jens Axboe9b402842019-04-11 11:45:41 -0600518 if (ctx->cq_ev_fd)
519 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -0600520}
521
522static void io_cqring_add_event(struct io_ring_ctx *ctx, u64 user_data,
Jens Axboec71ffb62019-05-13 20:58:29 -0600523 long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700524{
525 unsigned long flags;
526
527 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboec71ffb62019-05-13 20:58:29 -0600528 io_cqring_fill_event(ctx, user_data, res);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700529 io_commit_cqring(ctx);
530 spin_unlock_irqrestore(&ctx->completion_lock, flags);
531
Jens Axboe8c838782019-03-12 15:48:16 -0600532 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700533}
534
535static void io_ring_drop_ctx_refs(struct io_ring_ctx *ctx, unsigned refs)
536{
537 percpu_ref_put_many(&ctx->refs, refs);
538
539 if (waitqueue_active(&ctx->wait))
540 wake_up(&ctx->wait);
541}
542
Jens Axboe2579f912019-01-09 09:10:43 -0700543static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
544 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700545{
Jens Axboefd6fab22019-03-14 16:30:06 -0600546 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700547 struct io_kiocb *req;
548
549 if (!percpu_ref_tryget(&ctx->refs))
550 return NULL;
551
Jens Axboe2579f912019-01-09 09:10:43 -0700552 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -0600553 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -0700554 if (unlikely(!req))
555 goto out;
556 } else if (!state->free_reqs) {
557 size_t sz;
558 int ret;
559
560 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -0600561 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
562
563 /*
564 * Bulk alloc is all-or-nothing. If we fail to get a batch,
565 * retry single alloc to be on the safe side.
566 */
567 if (unlikely(ret <= 0)) {
568 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
569 if (!state->reqs[0])
570 goto out;
571 ret = 1;
572 }
Jens Axboe2579f912019-01-09 09:10:43 -0700573 state->free_reqs = ret - 1;
574 state->cur_req = 1;
575 req = state->reqs[0];
576 } else {
577 req = state->reqs[state->cur_req];
578 state->free_reqs--;
579 state->cur_req++;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700580 }
581
Jens Axboe60c112b2019-06-21 10:20:18 -0600582 req->file = NULL;
Jens Axboe2579f912019-01-09 09:10:43 -0700583 req->ctx = ctx;
584 req->flags = 0;
Jens Axboee65ef562019-03-12 10:16:44 -0600585 /* one is dropped after submission, the other at completion */
586 refcount_set(&req->refs, 2);
Jens Axboe2579f912019-01-09 09:10:43 -0700587 return req;
588out:
Jens Axboe2b188cc2019-01-07 10:46:33 -0700589 io_ring_drop_ctx_refs(ctx, 1);
590 return NULL;
591}
592
Jens Axboedef596e2019-01-09 08:59:42 -0700593static void io_free_req_many(struct io_ring_ctx *ctx, void **reqs, int *nr)
594{
595 if (*nr) {
596 kmem_cache_free_bulk(req_cachep, *nr, reqs);
597 io_ring_drop_ctx_refs(ctx, *nr);
598 *nr = 0;
599 }
600}
601
Jens Axboe2b188cc2019-01-07 10:46:33 -0700602static void io_free_req(struct io_kiocb *req)
603{
Jens Axboe09bb8392019-03-13 12:39:28 -0600604 if (req->file && !(req->flags & REQ_F_FIXED_FILE))
605 fput(req->file);
Jens Axboee65ef562019-03-12 10:16:44 -0600606 io_ring_drop_ctx_refs(req->ctx, 1);
607 kmem_cache_free(req_cachep, req);
608}
609
610static void io_put_req(struct io_kiocb *req)
611{
612 if (refcount_dec_and_test(&req->refs))
613 io_free_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700614}
615
Jens Axboedef596e2019-01-09 08:59:42 -0700616/*
617 * Find and free completed poll iocbs
618 */
619static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
620 struct list_head *done)
621{
622 void *reqs[IO_IOPOLL_BATCH];
623 struct io_kiocb *req;
Jens Axboe09bb8392019-03-13 12:39:28 -0600624 int to_free;
Jens Axboedef596e2019-01-09 08:59:42 -0700625
Jens Axboe09bb8392019-03-13 12:39:28 -0600626 to_free = 0;
Jens Axboedef596e2019-01-09 08:59:42 -0700627 while (!list_empty(done)) {
628 req = list_first_entry(done, struct io_kiocb, list);
629 list_del(&req->list);
630
Jens Axboec71ffb62019-05-13 20:58:29 -0600631 io_cqring_fill_event(ctx, req->user_data, req->error);
Jens Axboedef596e2019-01-09 08:59:42 -0700632 (*nr_events)++;
633
Jens Axboe09bb8392019-03-13 12:39:28 -0600634 if (refcount_dec_and_test(&req->refs)) {
635 /* If we're not using fixed files, we have to pair the
636 * completion part with the file put. Use regular
637 * completions for those, only batch free for fixed
638 * file.
639 */
640 if (req->flags & REQ_F_FIXED_FILE) {
641 reqs[to_free++] = req;
642 if (to_free == ARRAY_SIZE(reqs))
643 io_free_req_many(ctx, reqs, &to_free);
Jens Axboe6b063142019-01-10 22:13:58 -0700644 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -0600645 io_free_req(req);
Jens Axboe6b063142019-01-10 22:13:58 -0700646 }
Jens Axboe9a56a232019-01-09 09:06:50 -0700647 }
Jens Axboedef596e2019-01-09 08:59:42 -0700648 }
Jens Axboedef596e2019-01-09 08:59:42 -0700649
Jens Axboe09bb8392019-03-13 12:39:28 -0600650 io_commit_cqring(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -0700651 io_free_req_many(ctx, reqs, &to_free);
652}
653
654static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
655 long min)
656{
657 struct io_kiocb *req, *tmp;
658 LIST_HEAD(done);
659 bool spin;
660 int ret;
661
662 /*
663 * Only spin for completions if we don't have multiple devices hanging
664 * off our complete list, and we're under the requested amount.
665 */
666 spin = !ctx->poll_multi_file && *nr_events < min;
667
668 ret = 0;
669 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
670 struct kiocb *kiocb = &req->rw;
671
672 /*
673 * Move completed entries to our local list. If we find a
674 * request that requires polling, break out and complete
675 * the done list first, if we have entries there.
676 */
677 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
678 list_move_tail(&req->list, &done);
679 continue;
680 }
681 if (!list_empty(&done))
682 break;
683
684 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
685 if (ret < 0)
686 break;
687
688 if (ret && spin)
689 spin = false;
690 ret = 0;
691 }
692
693 if (!list_empty(&done))
694 io_iopoll_complete(ctx, nr_events, &done);
695
696 return ret;
697}
698
699/*
700 * Poll for a mininum of 'min' events. Note that if min == 0 we consider that a
701 * non-spinning poll check - we'll still enter the driver poll loop, but only
702 * as a non-spinning completion check.
703 */
704static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
705 long min)
706{
707 while (!list_empty(&ctx->poll_list)) {
708 int ret;
709
710 ret = io_do_iopoll(ctx, nr_events, min);
711 if (ret < 0)
712 return ret;
713 if (!min || *nr_events >= min)
714 return 0;
715 }
716
717 return 1;
718}
719
720/*
721 * We can't just wait for polled events to come to us, we have to actively
722 * find and complete them.
723 */
724static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
725{
726 if (!(ctx->flags & IORING_SETUP_IOPOLL))
727 return;
728
729 mutex_lock(&ctx->uring_lock);
730 while (!list_empty(&ctx->poll_list)) {
731 unsigned int nr_events = 0;
732
733 io_iopoll_getevents(ctx, &nr_events, 1);
734 }
735 mutex_unlock(&ctx->uring_lock);
736}
737
738static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
739 long min)
740{
741 int ret = 0;
742
743 do {
744 int tmin = 0;
745
746 if (*nr_events < min)
747 tmin = min - *nr_events;
748
749 ret = io_iopoll_getevents(ctx, nr_events, tmin);
750 if (ret <= 0)
751 break;
752 ret = 0;
753 } while (min && !*nr_events && !need_resched());
754
755 return ret;
756}
757
Jens Axboe2b188cc2019-01-07 10:46:33 -0700758static void kiocb_end_write(struct kiocb *kiocb)
759{
760 if (kiocb->ki_flags & IOCB_WRITE) {
761 struct inode *inode = file_inode(kiocb->ki_filp);
762
763 /*
764 * Tell lockdep we inherited freeze protection from submission
765 * thread.
766 */
767 if (S_ISREG(inode->i_mode))
768 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
769 file_end_write(kiocb->ki_filp);
770 }
771}
772
773static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
774{
775 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
776
777 kiocb_end_write(kiocb);
778
Jens Axboec71ffb62019-05-13 20:58:29 -0600779 io_cqring_add_event(req->ctx, req->user_data, res);
Jens Axboee65ef562019-03-12 10:16:44 -0600780 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700781}
782
Jens Axboedef596e2019-01-09 08:59:42 -0700783static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
784{
785 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
786
787 kiocb_end_write(kiocb);
788
789 req->error = res;
790 if (res != -EAGAIN)
791 req->flags |= REQ_F_IOPOLL_COMPLETED;
792}
793
794/*
795 * After the iocb has been issued, it's safe to be found on the poll list.
796 * Adding the kiocb to the list AFTER submission ensures that we don't
797 * find it from a io_iopoll_getevents() thread before the issuer is done
798 * accessing the kiocb cookie.
799 */
800static void io_iopoll_req_issued(struct io_kiocb *req)
801{
802 struct io_ring_ctx *ctx = req->ctx;
803
804 /*
805 * Track whether we have multiple files in our lists. This will impact
806 * how we do polling eventually, not spinning if we're on potentially
807 * different devices.
808 */
809 if (list_empty(&ctx->poll_list)) {
810 ctx->poll_multi_file = false;
811 } else if (!ctx->poll_multi_file) {
812 struct io_kiocb *list_req;
813
814 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
815 list);
816 if (list_req->rw.ki_filp != req->rw.ki_filp)
817 ctx->poll_multi_file = true;
818 }
819
820 /*
821 * For fast devices, IO may have already completed. If it has, add
822 * it to the front so we find it first.
823 */
824 if (req->flags & REQ_F_IOPOLL_COMPLETED)
825 list_add(&req->list, &ctx->poll_list);
826 else
827 list_add_tail(&req->list, &ctx->poll_list);
828}
829
Jens Axboe3d6770f2019-04-13 11:50:54 -0600830static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -0700831{
Jens Axboe3d6770f2019-04-13 11:50:54 -0600832 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -0700833 int diff = state->has_refs - state->used_refs;
834
835 if (diff)
836 fput_many(state->file, diff);
837 state->file = NULL;
838 }
839}
840
841/*
842 * Get as many references to a file as we have IOs left in this submission,
843 * assuming most submissions are for one file, or at least that each file
844 * has more than one submission.
845 */
846static struct file *io_file_get(struct io_submit_state *state, int fd)
847{
848 if (!state)
849 return fget(fd);
850
851 if (state->file) {
852 if (state->fd == fd) {
853 state->used_refs++;
854 state->ios_left--;
855 return state->file;
856 }
Jens Axboe3d6770f2019-04-13 11:50:54 -0600857 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -0700858 }
859 state->file = fget_many(fd, state->ios_left);
860 if (!state->file)
861 return NULL;
862
863 state->fd = fd;
864 state->has_refs = state->ios_left;
865 state->used_refs = 1;
866 state->ios_left--;
867 return state->file;
868}
869
Jens Axboe2b188cc2019-01-07 10:46:33 -0700870/*
871 * If we tracked the file through the SCM inflight mechanism, we could support
872 * any file. For now, just ensure that anything potentially problematic is done
873 * inline.
874 */
875static bool io_file_supports_async(struct file *file)
876{
877 umode_t mode = file_inode(file)->i_mode;
878
879 if (S_ISBLK(mode) || S_ISCHR(mode))
880 return true;
881 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
882 return true;
883
884 return false;
885}
886
Jens Axboe6c271ce2019-01-10 11:22:30 -0700887static int io_prep_rw(struct io_kiocb *req, const struct sqe_submit *s,
Jens Axboe8358e3a2019-04-23 08:17:58 -0600888 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700889{
Jens Axboe6c271ce2019-01-10 11:22:30 -0700890 const struct io_uring_sqe *sqe = s->sqe;
Jens Axboedef596e2019-01-09 08:59:42 -0700891 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700892 struct kiocb *kiocb = &req->rw;
Jens Axboe09bb8392019-03-13 12:39:28 -0600893 unsigned ioprio;
894 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700895
Jens Axboe09bb8392019-03-13 12:39:28 -0600896 if (!req->file)
897 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700898
Jens Axboe09bb8392019-03-13 12:39:28 -0600899 if (force_nonblock && !io_file_supports_async(req->file))
900 force_nonblock = false;
Jens Axboe6b063142019-01-10 22:13:58 -0700901
Jens Axboe2b188cc2019-01-07 10:46:33 -0700902 kiocb->ki_pos = READ_ONCE(sqe->off);
903 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
904 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
905
906 ioprio = READ_ONCE(sqe->ioprio);
907 if (ioprio) {
908 ret = ioprio_check_cap(ioprio);
909 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -0600910 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700911
912 kiocb->ki_ioprio = ioprio;
913 } else
914 kiocb->ki_ioprio = get_current_ioprio();
915
916 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
917 if (unlikely(ret))
Jens Axboe09bb8392019-03-13 12:39:28 -0600918 return ret;
Stefan Bühler8449eed2019-04-27 20:34:19 +0200919
920 /* don't allow async punt if RWF_NOWAIT was requested */
921 if (kiocb->ki_flags & IOCB_NOWAIT)
922 req->flags |= REQ_F_NOWAIT;
923
924 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700925 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +0200926
Jens Axboedef596e2019-01-09 08:59:42 -0700927 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -0700928 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
929 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -0600930 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700931
Jens Axboedef596e2019-01-09 08:59:42 -0700932 req->error = 0;
933 kiocb->ki_flags |= IOCB_HIPRI;
934 kiocb->ki_complete = io_complete_rw_iopoll;
935 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -0600936 if (kiocb->ki_flags & IOCB_HIPRI)
937 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -0700938 kiocb->ki_complete = io_complete_rw;
939 }
Jens Axboe2b188cc2019-01-07 10:46:33 -0700940 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700941}
942
943static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
944{
945 switch (ret) {
946 case -EIOCBQUEUED:
947 break;
948 case -ERESTARTSYS:
949 case -ERESTARTNOINTR:
950 case -ERESTARTNOHAND:
951 case -ERESTART_RESTARTBLOCK:
952 /*
953 * We can't just restart the syscall, since previously
954 * submitted sqes may already be in progress. Just fail this
955 * IO with EINTR.
956 */
957 ret = -EINTR;
958 /* fall through */
959 default:
960 kiocb->ki_complete(kiocb, ret, 0);
961 }
962}
963
Jens Axboeedafcce2019-01-09 09:16:05 -0700964static int io_import_fixed(struct io_ring_ctx *ctx, int rw,
965 const struct io_uring_sqe *sqe,
966 struct iov_iter *iter)
967{
968 size_t len = READ_ONCE(sqe->len);
969 struct io_mapped_ubuf *imu;
970 unsigned index, buf_index;
971 size_t offset;
972 u64 buf_addr;
973
974 /* attempt to use fixed buffers without having provided iovecs */
975 if (unlikely(!ctx->user_bufs))
976 return -EFAULT;
977
978 buf_index = READ_ONCE(sqe->buf_index);
979 if (unlikely(buf_index >= ctx->nr_user_bufs))
980 return -EFAULT;
981
982 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
983 imu = &ctx->user_bufs[index];
984 buf_addr = READ_ONCE(sqe->addr);
985
986 /* overflow */
987 if (buf_addr + len < buf_addr)
988 return -EFAULT;
989 /* not inside the mapped region */
990 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
991 return -EFAULT;
992
993 /*
994 * May not be a start of buffer, set size appropriately
995 * and advance us to the beginning.
996 */
997 offset = buf_addr - imu->ubuf;
998 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
999 if (offset)
1000 iov_iter_advance(iter, offset);
Jens Axboe875f1d02019-02-27 13:05:25 -07001001
1002 /* don't drop a reference to these pages */
1003 iter->type |= ITER_BVEC_FLAG_NO_REF;
Jens Axboeedafcce2019-01-09 09:16:05 -07001004 return 0;
1005}
1006
Jens Axboe2b188cc2019-01-07 10:46:33 -07001007static int io_import_iovec(struct io_ring_ctx *ctx, int rw,
1008 const struct sqe_submit *s, struct iovec **iovec,
1009 struct iov_iter *iter)
1010{
1011 const struct io_uring_sqe *sqe = s->sqe;
1012 void __user *buf = u64_to_user_ptr(READ_ONCE(sqe->addr));
1013 size_t sqe_len = READ_ONCE(sqe->len);
Jens Axboeedafcce2019-01-09 09:16:05 -07001014 u8 opcode;
1015
1016 /*
1017 * We're reading ->opcode for the second time, but the first read
1018 * doesn't care whether it's _FIXED or not, so it doesn't matter
1019 * whether ->opcode changes concurrently. The first read does care
1020 * about whether it is a READ or a WRITE, so we don't trust this read
1021 * for that purpose and instead let the caller pass in the read/write
1022 * flag.
1023 */
1024 opcode = READ_ONCE(sqe->opcode);
1025 if (opcode == IORING_OP_READ_FIXED ||
1026 opcode == IORING_OP_WRITE_FIXED) {
Jens Axboee0c5c572019-03-12 10:18:47 -06001027 int ret = io_import_fixed(ctx, rw, sqe, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07001028 *iovec = NULL;
1029 return ret;
1030 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001031
1032 if (!s->has_user)
1033 return -EFAULT;
1034
1035#ifdef CONFIG_COMPAT
1036 if (ctx->compat)
1037 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
1038 iovec, iter);
1039#endif
1040
1041 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
1042}
1043
Jens Axboe31b51512019-01-18 22:56:34 -07001044/*
1045 * Make a note of the last file/offset/direction we punted to async
1046 * context. We'll use this information to see if we can piggy back a
1047 * sequential request onto the previous one, if it's still hasn't been
1048 * completed by the async worker.
1049 */
1050static void io_async_list_note(int rw, struct io_kiocb *req, size_t len)
1051{
1052 struct async_list *async_list = &req->ctx->pending_async[rw];
1053 struct kiocb *kiocb = &req->rw;
1054 struct file *filp = kiocb->ki_filp;
1055 off_t io_end = kiocb->ki_pos + len;
1056
1057 if (filp == async_list->file && kiocb->ki_pos == async_list->io_end) {
1058 unsigned long max_pages;
1059
1060 /* Use 8x RA size as a decent limiter for both reads/writes */
1061 max_pages = filp->f_ra.ra_pages;
1062 if (!max_pages)
Nikolay Borisovb5420232019-03-11 23:28:13 -07001063 max_pages = VM_READAHEAD_PAGES;
Jens Axboe31b51512019-01-18 22:56:34 -07001064 max_pages *= 8;
1065
1066 /* If max pages are exceeded, reset the state */
1067 len >>= PAGE_SHIFT;
1068 if (async_list->io_pages + len <= max_pages) {
1069 req->flags |= REQ_F_SEQ_PREV;
1070 async_list->io_pages += len;
1071 } else {
1072 io_end = 0;
1073 async_list->io_pages = 0;
1074 }
1075 }
1076
1077 /* New file? Reset state. */
1078 if (async_list->file != filp) {
1079 async_list->io_pages = 0;
1080 async_list->file = filp;
1081 }
1082 async_list->io_end = io_end;
1083}
1084
Jens Axboee0c5c572019-03-12 10:18:47 -06001085static int io_read(struct io_kiocb *req, const struct sqe_submit *s,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001086 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001087{
1088 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1089 struct kiocb *kiocb = &req->rw;
1090 struct iov_iter iter;
1091 struct file *file;
Jens Axboe31b51512019-01-18 22:56:34 -07001092 size_t iov_count;
Jens Axboee0c5c572019-03-12 10:18:47 -06001093 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001094
Jens Axboe8358e3a2019-04-23 08:17:58 -06001095 ret = io_prep_rw(req, s, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001096 if (ret)
1097 return ret;
1098 file = kiocb->ki_filp;
1099
Jens Axboe2b188cc2019-01-07 10:46:33 -07001100 if (unlikely(!(file->f_mode & FMODE_READ)))
Jens Axboe09bb8392019-03-13 12:39:28 -06001101 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001102 if (unlikely(!file->f_op->read_iter))
Jens Axboe09bb8392019-03-13 12:39:28 -06001103 return -EINVAL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001104
1105 ret = io_import_iovec(req->ctx, READ, s, &iovec, &iter);
1106 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06001107 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001108
Jens Axboe31b51512019-01-18 22:56:34 -07001109 iov_count = iov_iter_count(&iter);
1110 ret = rw_verify_area(READ, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001111 if (!ret) {
1112 ssize_t ret2;
1113
1114 /* Catch -EAGAIN return for forced non-blocking submission */
1115 ret2 = call_read_iter(file, kiocb, &iter);
Jens Axboe31b51512019-01-18 22:56:34 -07001116 if (!force_nonblock || ret2 != -EAGAIN) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001117 io_rw_done(kiocb, ret2);
Jens Axboe31b51512019-01-18 22:56:34 -07001118 } else {
1119 /*
1120 * If ->needs_lock is true, we're already in async
1121 * context.
1122 */
1123 if (!s->needs_lock)
1124 io_async_list_note(READ, req, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001125 ret = -EAGAIN;
Jens Axboe31b51512019-01-18 22:56:34 -07001126 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001127 }
1128 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001129 return ret;
1130}
1131
Jens Axboee0c5c572019-03-12 10:18:47 -06001132static int io_write(struct io_kiocb *req, const struct sqe_submit *s,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001133 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001134{
1135 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1136 struct kiocb *kiocb = &req->rw;
1137 struct iov_iter iter;
1138 struct file *file;
Jens Axboe31b51512019-01-18 22:56:34 -07001139 size_t iov_count;
Jens Axboee0c5c572019-03-12 10:18:47 -06001140 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001141
Jens Axboe8358e3a2019-04-23 08:17:58 -06001142 ret = io_prep_rw(req, s, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001143 if (ret)
1144 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001145
Jens Axboe2b188cc2019-01-07 10:46:33 -07001146 file = kiocb->ki_filp;
1147 if (unlikely(!(file->f_mode & FMODE_WRITE)))
Jens Axboe09bb8392019-03-13 12:39:28 -06001148 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001149 if (unlikely(!file->f_op->write_iter))
Jens Axboe09bb8392019-03-13 12:39:28 -06001150 return -EINVAL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001151
1152 ret = io_import_iovec(req->ctx, WRITE, s, &iovec, &iter);
1153 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06001154 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001155
Jens Axboe31b51512019-01-18 22:56:34 -07001156 iov_count = iov_iter_count(&iter);
1157
1158 ret = -EAGAIN;
1159 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT)) {
1160 /* If ->needs_lock is true, we're already in async context. */
1161 if (!s->needs_lock)
1162 io_async_list_note(WRITE, req, iov_count);
1163 goto out_free;
1164 }
1165
1166 ret = rw_verify_area(WRITE, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001167 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01001168 ssize_t ret2;
1169
Jens Axboe2b188cc2019-01-07 10:46:33 -07001170 /*
1171 * Open-code file_start_write here to grab freeze protection,
1172 * which will be released by another thread in
1173 * io_complete_rw(). Fool lockdep by telling it the lock got
1174 * released so that it doesn't complain about the held lock when
1175 * we return to userspace.
1176 */
1177 if (S_ISREG(file_inode(file)->i_mode)) {
1178 __sb_start_write(file_inode(file)->i_sb,
1179 SB_FREEZE_WRITE, true);
1180 __sb_writers_release(file_inode(file)->i_sb,
1181 SB_FREEZE_WRITE);
1182 }
1183 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01001184
1185 ret2 = call_write_iter(file, kiocb, &iter);
1186 if (!force_nonblock || ret2 != -EAGAIN) {
1187 io_rw_done(kiocb, ret2);
1188 } else {
1189 /*
1190 * If ->needs_lock is true, we're already in async
1191 * context.
1192 */
1193 if (!s->needs_lock)
1194 io_async_list_note(WRITE, req, iov_count);
1195 ret = -EAGAIN;
1196 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001197 }
Jens Axboe31b51512019-01-18 22:56:34 -07001198out_free:
Jens Axboe2b188cc2019-01-07 10:46:33 -07001199 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001200 return ret;
1201}
1202
1203/*
1204 * IORING_OP_NOP just posts a completion event, nothing else.
1205 */
1206static int io_nop(struct io_kiocb *req, u64 user_data)
1207{
1208 struct io_ring_ctx *ctx = req->ctx;
1209 long err = 0;
1210
Jens Axboedef596e2019-01-09 08:59:42 -07001211 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1212 return -EINVAL;
1213
Jens Axboec71ffb62019-05-13 20:58:29 -06001214 io_cqring_add_event(ctx, user_data, err);
Jens Axboee65ef562019-03-12 10:16:44 -06001215 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001216 return 0;
1217}
1218
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001219static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1220{
Jens Axboe6b063142019-01-10 22:13:58 -07001221 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001222
Jens Axboe09bb8392019-03-13 12:39:28 -06001223 if (!req->file)
1224 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001225
Jens Axboe6b063142019-01-10 22:13:58 -07001226 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07001227 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07001228 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001229 return -EINVAL;
1230
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001231 return 0;
1232}
1233
1234static int io_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1235 bool force_nonblock)
1236{
1237 loff_t sqe_off = READ_ONCE(sqe->off);
1238 loff_t sqe_len = READ_ONCE(sqe->len);
1239 loff_t end = sqe_off + sqe_len;
1240 unsigned fsync_flags;
1241 int ret;
1242
1243 fsync_flags = READ_ONCE(sqe->fsync_flags);
1244 if (unlikely(fsync_flags & ~IORING_FSYNC_DATASYNC))
1245 return -EINVAL;
1246
1247 ret = io_prep_fsync(req, sqe);
1248 if (ret)
1249 return ret;
1250
1251 /* fsync always requires a blocking context */
1252 if (force_nonblock)
1253 return -EAGAIN;
1254
1255 ret = vfs_fsync_range(req->rw.ki_filp, sqe_off,
1256 end > 0 ? end : LLONG_MAX,
1257 fsync_flags & IORING_FSYNC_DATASYNC);
1258
Jens Axboec71ffb62019-05-13 20:58:29 -06001259 io_cqring_add_event(req->ctx, sqe->user_data, ret);
Jens Axboee65ef562019-03-12 10:16:44 -06001260 io_put_req(req);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001261 return 0;
1262}
1263
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001264static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1265{
1266 struct io_ring_ctx *ctx = req->ctx;
1267 int ret = 0;
1268
1269 if (!req->file)
1270 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001271
1272 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1273 return -EINVAL;
1274 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
1275 return -EINVAL;
1276
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001277 return ret;
1278}
1279
1280static int io_sync_file_range(struct io_kiocb *req,
1281 const struct io_uring_sqe *sqe,
1282 bool force_nonblock)
1283{
1284 loff_t sqe_off;
1285 loff_t sqe_len;
1286 unsigned flags;
1287 int ret;
1288
1289 ret = io_prep_sfr(req, sqe);
1290 if (ret)
1291 return ret;
1292
1293 /* sync_file_range always requires a blocking context */
1294 if (force_nonblock)
1295 return -EAGAIN;
1296
1297 sqe_off = READ_ONCE(sqe->off);
1298 sqe_len = READ_ONCE(sqe->len);
1299 flags = READ_ONCE(sqe->sync_range_flags);
1300
1301 ret = sync_file_range(req->rw.ki_filp, sqe_off, sqe_len, flags);
1302
Jens Axboec71ffb62019-05-13 20:58:29 -06001303 io_cqring_add_event(req->ctx, sqe->user_data, ret);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001304 io_put_req(req);
1305 return 0;
1306}
1307
Jens Axboe221c5eb2019-01-17 09:41:58 -07001308static void io_poll_remove_one(struct io_kiocb *req)
1309{
1310 struct io_poll_iocb *poll = &req->poll;
1311
1312 spin_lock(&poll->head->lock);
1313 WRITE_ONCE(poll->canceled, true);
1314 if (!list_empty(&poll->wait.entry)) {
1315 list_del_init(&poll->wait.entry);
1316 queue_work(req->ctx->sqo_wq, &req->work);
1317 }
1318 spin_unlock(&poll->head->lock);
1319
1320 list_del_init(&req->list);
1321}
1322
1323static void io_poll_remove_all(struct io_ring_ctx *ctx)
1324{
1325 struct io_kiocb *req;
1326
1327 spin_lock_irq(&ctx->completion_lock);
1328 while (!list_empty(&ctx->cancel_list)) {
1329 req = list_first_entry(&ctx->cancel_list, struct io_kiocb,list);
1330 io_poll_remove_one(req);
1331 }
1332 spin_unlock_irq(&ctx->completion_lock);
1333}
1334
1335/*
1336 * Find a running poll command that matches one specified in sqe->addr,
1337 * and remove it if found.
1338 */
1339static int io_poll_remove(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1340{
1341 struct io_ring_ctx *ctx = req->ctx;
1342 struct io_kiocb *poll_req, *next;
1343 int ret = -ENOENT;
1344
1345 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1346 return -EINVAL;
1347 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
1348 sqe->poll_events)
1349 return -EINVAL;
1350
1351 spin_lock_irq(&ctx->completion_lock);
1352 list_for_each_entry_safe(poll_req, next, &ctx->cancel_list, list) {
1353 if (READ_ONCE(sqe->addr) == poll_req->user_data) {
1354 io_poll_remove_one(poll_req);
1355 ret = 0;
1356 break;
1357 }
1358 }
1359 spin_unlock_irq(&ctx->completion_lock);
1360
Jens Axboec71ffb62019-05-13 20:58:29 -06001361 io_cqring_add_event(req->ctx, sqe->user_data, ret);
Jens Axboee65ef562019-03-12 10:16:44 -06001362 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001363 return 0;
1364}
1365
Jens Axboe8c838782019-03-12 15:48:16 -06001366static void io_poll_complete(struct io_ring_ctx *ctx, struct io_kiocb *req,
1367 __poll_t mask)
Jens Axboe221c5eb2019-01-17 09:41:58 -07001368{
Jens Axboe8c838782019-03-12 15:48:16 -06001369 req->poll.done = true;
Jens Axboec71ffb62019-05-13 20:58:29 -06001370 io_cqring_fill_event(ctx, req->user_data, mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06001371 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001372}
1373
1374static void io_poll_complete_work(struct work_struct *work)
1375{
1376 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
1377 struct io_poll_iocb *poll = &req->poll;
1378 struct poll_table_struct pt = { ._key = poll->events };
1379 struct io_ring_ctx *ctx = req->ctx;
1380 __poll_t mask = 0;
1381
1382 if (!READ_ONCE(poll->canceled))
1383 mask = vfs_poll(poll->file, &pt) & poll->events;
1384
1385 /*
1386 * Note that ->ki_cancel callers also delete iocb from active_reqs after
1387 * calling ->ki_cancel. We need the ctx_lock roundtrip here to
1388 * synchronize with them. In the cancellation case the list_del_init
1389 * itself is not actually needed, but harmless so we keep it in to
1390 * avoid further branches in the fast path.
1391 */
1392 spin_lock_irq(&ctx->completion_lock);
1393 if (!mask && !READ_ONCE(poll->canceled)) {
1394 add_wait_queue(poll->head, &poll->wait);
1395 spin_unlock_irq(&ctx->completion_lock);
1396 return;
1397 }
1398 list_del_init(&req->list);
Jens Axboe8c838782019-03-12 15:48:16 -06001399 io_poll_complete(ctx, req, mask);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001400 spin_unlock_irq(&ctx->completion_lock);
1401
Jens Axboe8c838782019-03-12 15:48:16 -06001402 io_cqring_ev_posted(ctx);
1403 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001404}
1405
1406static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
1407 void *key)
1408{
1409 struct io_poll_iocb *poll = container_of(wait, struct io_poll_iocb,
1410 wait);
1411 struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
1412 struct io_ring_ctx *ctx = req->ctx;
1413 __poll_t mask = key_to_poll(key);
Jens Axboe8c838782019-03-12 15:48:16 -06001414 unsigned long flags;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001415
1416 /* for instances that support it check for an event match first: */
Jens Axboe8c838782019-03-12 15:48:16 -06001417 if (mask && !(mask & poll->events))
1418 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001419
1420 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06001421
1422 if (mask && spin_trylock_irqsave(&ctx->completion_lock, flags)) {
1423 list_del(&req->list);
1424 io_poll_complete(ctx, req, mask);
1425 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1426
1427 io_cqring_ev_posted(ctx);
1428 io_put_req(req);
1429 } else {
1430 queue_work(ctx->sqo_wq, &req->work);
1431 }
1432
Jens Axboe221c5eb2019-01-17 09:41:58 -07001433 return 1;
1434}
1435
1436struct io_poll_table {
1437 struct poll_table_struct pt;
1438 struct io_kiocb *req;
1439 int error;
1440};
1441
1442static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
1443 struct poll_table_struct *p)
1444{
1445 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
1446
1447 if (unlikely(pt->req->poll.head)) {
1448 pt->error = -EINVAL;
1449 return;
1450 }
1451
1452 pt->error = 0;
1453 pt->req->poll.head = head;
1454 add_wait_queue(head, &pt->req->poll.wait);
1455}
1456
1457static int io_poll_add(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1458{
1459 struct io_poll_iocb *poll = &req->poll;
1460 struct io_ring_ctx *ctx = req->ctx;
1461 struct io_poll_table ipt;
Jens Axboe8c838782019-03-12 15:48:16 -06001462 bool cancel = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001463 __poll_t mask;
1464 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001465
1466 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1467 return -EINVAL;
1468 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
1469 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06001470 if (!poll->file)
1471 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001472
1473 INIT_WORK(&req->work, io_poll_complete_work);
1474 events = READ_ONCE(sqe->poll_events);
1475 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
1476
Jens Axboe221c5eb2019-01-17 09:41:58 -07001477 poll->head = NULL;
Jens Axboe8c838782019-03-12 15:48:16 -06001478 poll->done = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001479 poll->canceled = false;
1480
1481 ipt.pt._qproc = io_poll_queue_proc;
1482 ipt.pt._key = poll->events;
1483 ipt.req = req;
1484 ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
1485
1486 /* initialized the list so that we can do list_empty checks */
1487 INIT_LIST_HEAD(&poll->wait.entry);
1488 init_waitqueue_func_entry(&poll->wait, io_poll_wake);
1489
Jens Axboe221c5eb2019-01-17 09:41:58 -07001490 mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001491
1492 spin_lock_irq(&ctx->completion_lock);
Jens Axboe8c838782019-03-12 15:48:16 -06001493 if (likely(poll->head)) {
1494 spin_lock(&poll->head->lock);
1495 if (unlikely(list_empty(&poll->wait.entry))) {
1496 if (ipt.error)
1497 cancel = true;
1498 ipt.error = 0;
1499 mask = 0;
1500 }
1501 if (mask || ipt.error)
1502 list_del_init(&poll->wait.entry);
1503 else if (cancel)
1504 WRITE_ONCE(poll->canceled, true);
1505 else if (!poll->done) /* actually waiting for an event */
1506 list_add_tail(&req->list, &ctx->cancel_list);
1507 spin_unlock(&poll->head->lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001508 }
Jens Axboe8c838782019-03-12 15:48:16 -06001509 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06001510 ipt.error = 0;
1511 io_poll_complete(ctx, req, mask);
1512 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07001513 spin_unlock_irq(&ctx->completion_lock);
1514
Jens Axboe8c838782019-03-12 15:48:16 -06001515 if (mask) {
1516 io_cqring_ev_posted(ctx);
Jens Axboee65ef562019-03-12 10:16:44 -06001517 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001518 }
Jens Axboe8c838782019-03-12 15:48:16 -06001519 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001520}
1521
Jens Axboede0617e2019-04-06 21:51:27 -06001522static int io_req_defer(struct io_ring_ctx *ctx, struct io_kiocb *req,
1523 const struct io_uring_sqe *sqe)
1524{
1525 struct io_uring_sqe *sqe_copy;
1526
1527 if (!io_sequence_defer(ctx, req) && list_empty(&ctx->defer_list))
1528 return 0;
1529
1530 sqe_copy = kmalloc(sizeof(*sqe_copy), GFP_KERNEL);
1531 if (!sqe_copy)
1532 return -EAGAIN;
1533
1534 spin_lock_irq(&ctx->completion_lock);
1535 if (!io_sequence_defer(ctx, req) && list_empty(&ctx->defer_list)) {
1536 spin_unlock_irq(&ctx->completion_lock);
1537 kfree(sqe_copy);
1538 return 0;
1539 }
1540
1541 memcpy(sqe_copy, sqe, sizeof(*sqe_copy));
1542 req->submit.sqe = sqe_copy;
1543
1544 INIT_WORK(&req->work, io_sq_wq_submit_work);
1545 list_add_tail(&req->list, &ctx->defer_list);
1546 spin_unlock_irq(&ctx->completion_lock);
1547 return -EIOCBQUEUED;
1548}
1549
Jens Axboe2b188cc2019-01-07 10:46:33 -07001550static int __io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001551 const struct sqe_submit *s, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001552{
Jens Axboee0c5c572019-03-12 10:18:47 -06001553 int ret, opcode;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001554
1555 if (unlikely(s->index >= ctx->sq_entries))
1556 return -EINVAL;
1557 req->user_data = READ_ONCE(s->sqe->user_data);
1558
1559 opcode = READ_ONCE(s->sqe->opcode);
1560 switch (opcode) {
1561 case IORING_OP_NOP:
1562 ret = io_nop(req, req->user_data);
1563 break;
1564 case IORING_OP_READV:
Jens Axboeedafcce2019-01-09 09:16:05 -07001565 if (unlikely(s->sqe->buf_index))
1566 return -EINVAL;
Jens Axboe8358e3a2019-04-23 08:17:58 -06001567 ret = io_read(req, s, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001568 break;
1569 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07001570 if (unlikely(s->sqe->buf_index))
1571 return -EINVAL;
Jens Axboe8358e3a2019-04-23 08:17:58 -06001572 ret = io_write(req, s, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07001573 break;
1574 case IORING_OP_READ_FIXED:
Jens Axboe8358e3a2019-04-23 08:17:58 -06001575 ret = io_read(req, s, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07001576 break;
1577 case IORING_OP_WRITE_FIXED:
Jens Axboe8358e3a2019-04-23 08:17:58 -06001578 ret = io_write(req, s, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001579 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001580 case IORING_OP_FSYNC:
1581 ret = io_fsync(req, s->sqe, force_nonblock);
1582 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001583 case IORING_OP_POLL_ADD:
1584 ret = io_poll_add(req, s->sqe);
1585 break;
1586 case IORING_OP_POLL_REMOVE:
1587 ret = io_poll_remove(req, s->sqe);
1588 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001589 case IORING_OP_SYNC_FILE_RANGE:
1590 ret = io_sync_file_range(req, s->sqe, force_nonblock);
1591 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001592 default:
1593 ret = -EINVAL;
1594 break;
1595 }
1596
Jens Axboedef596e2019-01-09 08:59:42 -07001597 if (ret)
1598 return ret;
1599
1600 if (ctx->flags & IORING_SETUP_IOPOLL) {
1601 if (req->error == -EAGAIN)
1602 return -EAGAIN;
1603
1604 /* workqueue context doesn't hold uring_lock, grab it now */
1605 if (s->needs_lock)
1606 mutex_lock(&ctx->uring_lock);
1607 io_iopoll_req_issued(req);
1608 if (s->needs_lock)
1609 mutex_unlock(&ctx->uring_lock);
1610 }
1611
1612 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001613}
1614
Jens Axboe31b51512019-01-18 22:56:34 -07001615static struct async_list *io_async_list_from_sqe(struct io_ring_ctx *ctx,
1616 const struct io_uring_sqe *sqe)
1617{
1618 switch (sqe->opcode) {
1619 case IORING_OP_READV:
1620 case IORING_OP_READ_FIXED:
1621 return &ctx->pending_async[READ];
1622 case IORING_OP_WRITEV:
1623 case IORING_OP_WRITE_FIXED:
1624 return &ctx->pending_async[WRITE];
1625 default:
1626 return NULL;
1627 }
1628}
1629
Jens Axboeedafcce2019-01-09 09:16:05 -07001630static inline bool io_sqe_needs_user(const struct io_uring_sqe *sqe)
1631{
1632 u8 opcode = READ_ONCE(sqe->opcode);
1633
1634 return !(opcode == IORING_OP_READ_FIXED ||
1635 opcode == IORING_OP_WRITE_FIXED);
1636}
1637
Jens Axboe2b188cc2019-01-07 10:46:33 -07001638static void io_sq_wq_submit_work(struct work_struct *work)
1639{
1640 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001641 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe31b51512019-01-18 22:56:34 -07001642 struct mm_struct *cur_mm = NULL;
1643 struct async_list *async_list;
1644 LIST_HEAD(req_list);
Jens Axboeedafcce2019-01-09 09:16:05 -07001645 mm_segment_t old_fs;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001646 int ret;
1647
Jens Axboe31b51512019-01-18 22:56:34 -07001648 async_list = io_async_list_from_sqe(ctx, req->submit.sqe);
1649restart:
1650 do {
1651 struct sqe_submit *s = &req->submit;
1652 const struct io_uring_sqe *sqe = s->sqe;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001653
Stefan Bühler8449eed2019-04-27 20:34:19 +02001654 /* Ensure we clear previously set non-block flag */
Jens Axboe31b51512019-01-18 22:56:34 -07001655 req->rw.ki_flags &= ~IOCB_NOWAIT;
1656
1657 ret = 0;
1658 if (io_sqe_needs_user(sqe) && !cur_mm) {
1659 if (!mmget_not_zero(ctx->sqo_mm)) {
1660 ret = -EFAULT;
1661 } else {
1662 cur_mm = ctx->sqo_mm;
1663 use_mm(cur_mm);
1664 old_fs = get_fs();
1665 set_fs(USER_DS);
1666 }
1667 }
1668
1669 if (!ret) {
1670 s->has_user = cur_mm != NULL;
1671 s->needs_lock = true;
1672 do {
Jens Axboe8358e3a2019-04-23 08:17:58 -06001673 ret = __io_submit_sqe(ctx, req, s, false);
Jens Axboe31b51512019-01-18 22:56:34 -07001674 /*
1675 * We can get EAGAIN for polled IO even though
1676 * we're forcing a sync submission from here,
1677 * since we can't wait for request slots on the
1678 * block side.
1679 */
1680 if (ret != -EAGAIN)
1681 break;
1682 cond_resched();
1683 } while (1);
1684 }
Jens Axboe817869d2019-04-30 14:44:05 -06001685
1686 /* drop submission reference */
1687 io_put_req(req);
1688
Jens Axboe31b51512019-01-18 22:56:34 -07001689 if (ret) {
Jens Axboec71ffb62019-05-13 20:58:29 -06001690 io_cqring_add_event(ctx, sqe->user_data, ret);
Jens Axboee65ef562019-03-12 10:16:44 -06001691 io_put_req(req);
Jens Axboe31b51512019-01-18 22:56:34 -07001692 }
1693
1694 /* async context always use a copy of the sqe */
1695 kfree(sqe);
1696
1697 if (!async_list)
1698 break;
1699 if (!list_empty(&req_list)) {
1700 req = list_first_entry(&req_list, struct io_kiocb,
1701 list);
1702 list_del(&req->list);
1703 continue;
1704 }
1705 if (list_empty(&async_list->list))
1706 break;
1707
1708 req = NULL;
1709 spin_lock(&async_list->lock);
1710 if (list_empty(&async_list->list)) {
1711 spin_unlock(&async_list->lock);
1712 break;
1713 }
1714 list_splice_init(&async_list->list, &req_list);
1715 spin_unlock(&async_list->lock);
1716
1717 req = list_first_entry(&req_list, struct io_kiocb, list);
1718 list_del(&req->list);
1719 } while (req);
Jens Axboeedafcce2019-01-09 09:16:05 -07001720
1721 /*
Jens Axboe31b51512019-01-18 22:56:34 -07001722 * Rare case of racing with a submitter. If we find the count has
1723 * dropped to zero AND we have pending work items, then restart
1724 * the processing. This is a tiny race window.
Jens Axboeedafcce2019-01-09 09:16:05 -07001725 */
Jens Axboe31b51512019-01-18 22:56:34 -07001726 if (async_list) {
1727 ret = atomic_dec_return(&async_list->cnt);
1728 while (!ret && !list_empty(&async_list->list)) {
1729 spin_lock(&async_list->lock);
1730 atomic_inc(&async_list->cnt);
1731 list_splice_init(&async_list->list, &req_list);
1732 spin_unlock(&async_list->lock);
1733
1734 if (!list_empty(&req_list)) {
1735 req = list_first_entry(&req_list,
1736 struct io_kiocb, list);
1737 list_del(&req->list);
1738 goto restart;
1739 }
1740 ret = atomic_dec_return(&async_list->cnt);
Jens Axboeedafcce2019-01-09 09:16:05 -07001741 }
Jens Axboeedafcce2019-01-09 09:16:05 -07001742 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001743
Jens Axboe31b51512019-01-18 22:56:34 -07001744 if (cur_mm) {
Jens Axboeedafcce2019-01-09 09:16:05 -07001745 set_fs(old_fs);
Jens Axboe31b51512019-01-18 22:56:34 -07001746 unuse_mm(cur_mm);
1747 mmput(cur_mm);
Jens Axboeedafcce2019-01-09 09:16:05 -07001748 }
Jens Axboe31b51512019-01-18 22:56:34 -07001749}
Jens Axboe2b188cc2019-01-07 10:46:33 -07001750
Jens Axboe31b51512019-01-18 22:56:34 -07001751/*
1752 * See if we can piggy back onto previously submitted work, that is still
1753 * running. We currently only allow this if the new request is sequential
1754 * to the previous one we punted.
1755 */
1756static bool io_add_to_prev_work(struct async_list *list, struct io_kiocb *req)
1757{
1758 bool ret = false;
1759
1760 if (!list)
1761 return false;
1762 if (!(req->flags & REQ_F_SEQ_PREV))
1763 return false;
1764 if (!atomic_read(&list->cnt))
1765 return false;
1766
1767 ret = true;
1768 spin_lock(&list->lock);
1769 list_add_tail(&req->list, &list->list);
1770 if (!atomic_read(&list->cnt)) {
1771 list_del_init(&req->list);
1772 ret = false;
1773 }
1774 spin_unlock(&list->lock);
1775 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001776}
1777
Jens Axboe09bb8392019-03-13 12:39:28 -06001778static bool io_op_needs_file(const struct io_uring_sqe *sqe)
1779{
1780 int op = READ_ONCE(sqe->opcode);
1781
1782 switch (op) {
1783 case IORING_OP_NOP:
1784 case IORING_OP_POLL_REMOVE:
1785 return false;
1786 default:
1787 return true;
1788 }
1789}
1790
1791static int io_req_set_file(struct io_ring_ctx *ctx, const struct sqe_submit *s,
1792 struct io_submit_state *state, struct io_kiocb *req)
1793{
1794 unsigned flags;
1795 int fd;
1796
1797 flags = READ_ONCE(s->sqe->flags);
1798 fd = READ_ONCE(s->sqe->fd);
1799
Jens Axboede0617e2019-04-06 21:51:27 -06001800 if (flags & IOSQE_IO_DRAIN) {
1801 req->flags |= REQ_F_IO_DRAIN;
1802 req->sequence = ctx->cached_sq_head - 1;
1803 }
1804
Jens Axboe60c112b2019-06-21 10:20:18 -06001805 if (!io_op_needs_file(s->sqe))
Jens Axboe09bb8392019-03-13 12:39:28 -06001806 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06001807
1808 if (flags & IOSQE_FIXED_FILE) {
1809 if (unlikely(!ctx->user_files ||
1810 (unsigned) fd >= ctx->nr_user_files))
1811 return -EBADF;
1812 req->file = ctx->user_files[fd];
1813 req->flags |= REQ_F_FIXED_FILE;
1814 } else {
1815 if (s->needs_fixed_file)
1816 return -EBADF;
1817 req->file = io_file_get(state, fd);
1818 if (unlikely(!req->file))
1819 return -EBADF;
1820 }
1821
1822 return 0;
1823}
1824
Jens Axboe9a56a232019-01-09 09:06:50 -07001825static int io_submit_sqe(struct io_ring_ctx *ctx, struct sqe_submit *s,
1826 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001827{
1828 struct io_kiocb *req;
Jens Axboee0c5c572019-03-12 10:18:47 -06001829 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001830
1831 /* enforce forwards compatibility on users */
Jens Axboede0617e2019-04-06 21:51:27 -06001832 if (unlikely(s->sqe->flags & ~(IOSQE_FIXED_FILE | IOSQE_IO_DRAIN)))
Jens Axboe2b188cc2019-01-07 10:46:33 -07001833 return -EINVAL;
1834
Jens Axboe2579f912019-01-09 09:10:43 -07001835 req = io_get_req(ctx, state);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001836 if (unlikely(!req))
1837 return -EAGAIN;
1838
Jens Axboe09bb8392019-03-13 12:39:28 -06001839 ret = io_req_set_file(ctx, s, state, req);
1840 if (unlikely(ret))
1841 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001842
Jens Axboede0617e2019-04-06 21:51:27 -06001843 ret = io_req_defer(ctx, req, s->sqe);
1844 if (ret) {
1845 if (ret == -EIOCBQUEUED)
1846 ret = 0;
1847 return ret;
1848 }
1849
Jens Axboe8358e3a2019-04-23 08:17:58 -06001850 ret = __io_submit_sqe(ctx, req, s, true);
Stefan Bühler8449eed2019-04-27 20:34:19 +02001851 if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001852 struct io_uring_sqe *sqe_copy;
1853
1854 sqe_copy = kmalloc(sizeof(*sqe_copy), GFP_KERNEL);
1855 if (sqe_copy) {
Jens Axboe31b51512019-01-18 22:56:34 -07001856 struct async_list *list;
1857
Jens Axboe2b188cc2019-01-07 10:46:33 -07001858 memcpy(sqe_copy, s->sqe, sizeof(*sqe_copy));
1859 s->sqe = sqe_copy;
1860
1861 memcpy(&req->submit, s, sizeof(*s));
Jens Axboe31b51512019-01-18 22:56:34 -07001862 list = io_async_list_from_sqe(ctx, s->sqe);
1863 if (!io_add_to_prev_work(list, req)) {
1864 if (list)
1865 atomic_inc(&list->cnt);
1866 INIT_WORK(&req->work, io_sq_wq_submit_work);
1867 queue_work(ctx->sqo_wq, &req->work);
1868 }
Jens Axboee65ef562019-03-12 10:16:44 -06001869
1870 /*
1871 * Queued up for async execution, worker will release
1872 * submit reference when the iocb is actually
1873 * submitted.
1874 */
1875 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001876 }
1877 }
Jens Axboee65ef562019-03-12 10:16:44 -06001878
Jens Axboe09bb8392019-03-13 12:39:28 -06001879out:
Jens Axboee65ef562019-03-12 10:16:44 -06001880 /* drop submission reference */
1881 io_put_req(req);
1882
1883 /* and drop final reference, if we failed */
Jens Axboe2b188cc2019-01-07 10:46:33 -07001884 if (ret)
Jens Axboee65ef562019-03-12 10:16:44 -06001885 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001886
1887 return ret;
1888}
1889
Jens Axboe9a56a232019-01-09 09:06:50 -07001890/*
1891 * Batched submission is done, ensure local IO is flushed out.
1892 */
1893static void io_submit_state_end(struct io_submit_state *state)
1894{
1895 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06001896 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07001897 if (state->free_reqs)
1898 kmem_cache_free_bulk(req_cachep, state->free_reqs,
1899 &state->reqs[state->cur_req]);
Jens Axboe9a56a232019-01-09 09:06:50 -07001900}
1901
1902/*
1903 * Start submission side cache.
1904 */
1905static void io_submit_state_start(struct io_submit_state *state,
1906 struct io_ring_ctx *ctx, unsigned max_ios)
1907{
1908 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07001909 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07001910 state->file = NULL;
1911 state->ios_left = max_ios;
1912}
1913
Jens Axboe2b188cc2019-01-07 10:46:33 -07001914static void io_commit_sqring(struct io_ring_ctx *ctx)
1915{
1916 struct io_sq_ring *ring = ctx->sq_ring;
1917
1918 if (ctx->cached_sq_head != READ_ONCE(ring->r.head)) {
1919 /*
1920 * Ensure any loads from the SQEs are done at this point,
1921 * since once we write the new head, the application could
1922 * write new data to them.
1923 */
1924 smp_store_release(&ring->r.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001925 }
1926}
1927
1928/*
Jens Axboe2b188cc2019-01-07 10:46:33 -07001929 * Fetch an sqe, if one is available. Note that s->sqe will point to memory
1930 * that is mapped by userspace. This means that care needs to be taken to
1931 * ensure that reads are stable, as we cannot rely on userspace always
1932 * being a good citizen. If members of the sqe are validated and then later
1933 * used, it's important that those reads are done through READ_ONCE() to
1934 * prevent a re-load down the line.
1935 */
1936static bool io_get_sqring(struct io_ring_ctx *ctx, struct sqe_submit *s)
1937{
1938 struct io_sq_ring *ring = ctx->sq_ring;
1939 unsigned head;
1940
1941 /*
1942 * The cached sq head (or cq tail) serves two purposes:
1943 *
1944 * 1) allows us to batch the cost of updating the user visible
1945 * head updates.
1946 * 2) allows the kernel side to track the head on its own, even
1947 * though the application is the one updating it.
1948 */
1949 head = ctx->cached_sq_head;
Stefan Bühlere523a292019-04-19 11:57:44 +02001950 /* make sure SQ entry isn't read before tail */
1951 if (head == smp_load_acquire(&ring->r.tail))
Jens Axboe2b188cc2019-01-07 10:46:33 -07001952 return false;
1953
1954 head = READ_ONCE(ring->array[head & ctx->sq_mask]);
1955 if (head < ctx->sq_entries) {
1956 s->index = head;
1957 s->sqe = &ctx->sq_sqes[head];
1958 ctx->cached_sq_head++;
1959 return true;
1960 }
1961
1962 /* drop invalid entries */
1963 ctx->cached_sq_head++;
1964 ring->dropped++;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001965 return false;
1966}
1967
Jens Axboe6c271ce2019-01-10 11:22:30 -07001968static int io_submit_sqes(struct io_ring_ctx *ctx, struct sqe_submit *sqes,
1969 unsigned int nr, bool has_user, bool mm_fault)
1970{
1971 struct io_submit_state state, *statep = NULL;
1972 int ret, i, submitted = 0;
1973
1974 if (nr > IO_PLUG_THRESHOLD) {
1975 io_submit_state_start(&state, ctx, nr);
1976 statep = &state;
1977 }
1978
1979 for (i = 0; i < nr; i++) {
1980 if (unlikely(mm_fault)) {
1981 ret = -EFAULT;
1982 } else {
1983 sqes[i].has_user = has_user;
1984 sqes[i].needs_lock = true;
1985 sqes[i].needs_fixed_file = true;
1986 ret = io_submit_sqe(ctx, &sqes[i], statep);
1987 }
1988 if (!ret) {
1989 submitted++;
1990 continue;
1991 }
1992
Jens Axboec71ffb62019-05-13 20:58:29 -06001993 io_cqring_add_event(ctx, sqes[i].sqe->user_data, ret);
Jens Axboe6c271ce2019-01-10 11:22:30 -07001994 }
1995
1996 if (statep)
1997 io_submit_state_end(&state);
1998
1999 return submitted;
2000}
2001
2002static int io_sq_thread(void *data)
2003{
2004 struct sqe_submit sqes[IO_IOPOLL_BATCH];
2005 struct io_ring_ctx *ctx = data;
2006 struct mm_struct *cur_mm = NULL;
2007 mm_segment_t old_fs;
2008 DEFINE_WAIT(wait);
2009 unsigned inflight;
2010 unsigned long timeout;
2011
2012 old_fs = get_fs();
2013 set_fs(USER_DS);
2014
2015 timeout = inflight = 0;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02002016 while (!kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07002017 bool all_fixed, mm_fault = false;
2018 int i;
2019
2020 if (inflight) {
2021 unsigned nr_events = 0;
2022
2023 if (ctx->flags & IORING_SETUP_IOPOLL) {
2024 /*
2025 * We disallow the app entering submit/complete
2026 * with polling, but we still need to lock the
2027 * ring to prevent racing with polled issue
2028 * that got punted to a workqueue.
2029 */
2030 mutex_lock(&ctx->uring_lock);
2031 io_iopoll_check(ctx, &nr_events, 0);
2032 mutex_unlock(&ctx->uring_lock);
2033 } else {
2034 /*
2035 * Normal IO, just pretend everything completed.
2036 * We don't have to poll completions for that.
2037 */
2038 nr_events = inflight;
2039 }
2040
2041 inflight -= nr_events;
2042 if (!inflight)
2043 timeout = jiffies + ctx->sq_thread_idle;
2044 }
2045
2046 if (!io_get_sqring(ctx, &sqes[0])) {
2047 /*
2048 * We're polling. If we're within the defined idle
2049 * period, then let us spin without work before going
2050 * to sleep.
2051 */
2052 if (inflight || !time_after(jiffies, timeout)) {
2053 cpu_relax();
2054 continue;
2055 }
2056
2057 /*
2058 * Drop cur_mm before scheduling, we can't hold it for
2059 * long periods (or over schedule()). Do this before
2060 * adding ourselves to the waitqueue, as the unuse/drop
2061 * may sleep.
2062 */
2063 if (cur_mm) {
2064 unuse_mm(cur_mm);
2065 mmput(cur_mm);
2066 cur_mm = NULL;
2067 }
2068
2069 prepare_to_wait(&ctx->sqo_wait, &wait,
2070 TASK_INTERRUPTIBLE);
2071
2072 /* Tell userspace we may need a wakeup call */
2073 ctx->sq_ring->flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02002074 /* make sure to read SQ tail after writing flags */
2075 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07002076
2077 if (!io_get_sqring(ctx, &sqes[0])) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02002078 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07002079 finish_wait(&ctx->sqo_wait, &wait);
2080 break;
2081 }
2082 if (signal_pending(current))
2083 flush_signals(current);
2084 schedule();
2085 finish_wait(&ctx->sqo_wait, &wait);
2086
2087 ctx->sq_ring->flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002088 continue;
2089 }
2090 finish_wait(&ctx->sqo_wait, &wait);
2091
2092 ctx->sq_ring->flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002093 }
2094
2095 i = 0;
2096 all_fixed = true;
2097 do {
2098 if (all_fixed && io_sqe_needs_user(sqes[i].sqe))
2099 all_fixed = false;
2100
2101 i++;
2102 if (i == ARRAY_SIZE(sqes))
2103 break;
2104 } while (io_get_sqring(ctx, &sqes[i]));
2105
2106 /* Unless all new commands are FIXED regions, grab mm */
2107 if (!all_fixed && !cur_mm) {
2108 mm_fault = !mmget_not_zero(ctx->sqo_mm);
2109 if (!mm_fault) {
2110 use_mm(ctx->sqo_mm);
2111 cur_mm = ctx->sqo_mm;
2112 }
2113 }
2114
2115 inflight += io_submit_sqes(ctx, sqes, i, cur_mm != NULL,
2116 mm_fault);
2117
2118 /* Commit SQ ring head once we've consumed all SQEs */
2119 io_commit_sqring(ctx);
2120 }
2121
2122 set_fs(old_fs);
2123 if (cur_mm) {
2124 unuse_mm(cur_mm);
2125 mmput(cur_mm);
2126 }
Jens Axboe06058632019-04-13 09:26:03 -06002127
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02002128 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06002129
Jens Axboe6c271ce2019-01-10 11:22:30 -07002130 return 0;
2131}
2132
Jens Axboe2b188cc2019-01-07 10:46:33 -07002133static int io_ring_submit(struct io_ring_ctx *ctx, unsigned int to_submit)
2134{
Jens Axboe9a56a232019-01-09 09:06:50 -07002135 struct io_submit_state state, *statep = NULL;
Jens Axboe5c8b0b52019-04-30 10:16:07 -06002136 int i, submit = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002137
Jens Axboe9a56a232019-01-09 09:06:50 -07002138 if (to_submit > IO_PLUG_THRESHOLD) {
2139 io_submit_state_start(&state, ctx, to_submit);
2140 statep = &state;
2141 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002142
2143 for (i = 0; i < to_submit; i++) {
2144 struct sqe_submit s;
Jens Axboe5c8b0b52019-04-30 10:16:07 -06002145 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002146
2147 if (!io_get_sqring(ctx, &s))
2148 break;
2149
2150 s.has_user = true;
Jens Axboedef596e2019-01-09 08:59:42 -07002151 s.needs_lock = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002152 s.needs_fixed_file = false;
Jens Axboe5c8b0b52019-04-30 10:16:07 -06002153 submit++;
Jens Axboedef596e2019-01-09 08:59:42 -07002154
Jens Axboe9a56a232019-01-09 09:06:50 -07002155 ret = io_submit_sqe(ctx, &s, statep);
Jens Axboe5c8b0b52019-04-30 10:16:07 -06002156 if (ret)
Jens Axboec71ffb62019-05-13 20:58:29 -06002157 io_cqring_add_event(ctx, s.sqe->user_data, ret);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002158 }
2159 io_commit_sqring(ctx);
2160
Jens Axboe9a56a232019-01-09 09:06:50 -07002161 if (statep)
2162 io_submit_state_end(statep);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002163
Jens Axboe5c8b0b52019-04-30 10:16:07 -06002164 return submit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002165}
2166
2167static unsigned io_cqring_events(struct io_cq_ring *ring)
2168{
Jackie Liudc6ce4b2019-05-16 11:46:30 +08002169 /* See comment at the top of this file */
2170 smp_rmb();
Jens Axboe2b188cc2019-01-07 10:46:33 -07002171 return READ_ONCE(ring->r.tail) - READ_ONCE(ring->r.head);
2172}
2173
2174/*
2175 * Wait until events become available, if we don't already have some. The
2176 * application must reap them itself, as they reside on the shared cq ring.
2177 */
2178static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
2179 const sigset_t __user *sig, size_t sigsz)
2180{
2181 struct io_cq_ring *ring = ctx->cq_ring;
2182 sigset_t ksigmask, sigsaved;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002183 int ret;
2184
Jens Axboe2b188cc2019-01-07 10:46:33 -07002185 if (io_cqring_events(ring) >= min_events)
2186 return 0;
2187
2188 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01002189#ifdef CONFIG_COMPAT
2190 if (in_compat_syscall())
2191 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
2192 &ksigmask, &sigsaved, sigsz);
2193 else
2194#endif
2195 ret = set_user_sigmask(sig, &ksigmask,
2196 &sigsaved, sigsz);
2197
Jens Axboe2b188cc2019-01-07 10:46:33 -07002198 if (ret)
2199 return ret;
2200 }
2201
Jackie Liufdb288a2019-05-16 11:46:31 +08002202 ret = wait_event_interruptible(ctx->wait, io_cqring_events(ring) >= min_events);
2203 if (ret == -ERESTARTSYS)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002204 ret = -EINTR;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002205
2206 if (sig)
2207 restore_user_sigmask(sig, &sigsaved);
2208
2209 return READ_ONCE(ring->r.head) == READ_ONCE(ring->r.tail) ? ret : 0;
2210}
2211
Jens Axboe6b063142019-01-10 22:13:58 -07002212static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
2213{
2214#if defined(CONFIG_UNIX)
2215 if (ctx->ring_sock) {
2216 struct sock *sock = ctx->ring_sock->sk;
2217 struct sk_buff *skb;
2218
2219 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
2220 kfree_skb(skb);
2221 }
2222#else
2223 int i;
2224
2225 for (i = 0; i < ctx->nr_user_files; i++)
2226 fput(ctx->user_files[i]);
2227#endif
2228}
2229
2230static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
2231{
2232 if (!ctx->user_files)
2233 return -ENXIO;
2234
2235 __io_sqe_files_unregister(ctx);
2236 kfree(ctx->user_files);
2237 ctx->user_files = NULL;
2238 ctx->nr_user_files = 0;
2239 return 0;
2240}
2241
Jens Axboe6c271ce2019-01-10 11:22:30 -07002242static void io_sq_thread_stop(struct io_ring_ctx *ctx)
2243{
2244 if (ctx->sqo_thread) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02002245 /*
2246 * The park is a bit of a work-around, without it we get
2247 * warning spews on shutdown with SQPOLL set and affinity
2248 * set to a single CPU.
2249 */
Jens Axboe06058632019-04-13 09:26:03 -06002250 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07002251 kthread_stop(ctx->sqo_thread);
2252 ctx->sqo_thread = NULL;
2253 }
2254}
2255
Jens Axboe6b063142019-01-10 22:13:58 -07002256static void io_finish_async(struct io_ring_ctx *ctx)
2257{
Jens Axboe6c271ce2019-01-10 11:22:30 -07002258 io_sq_thread_stop(ctx);
2259
Jens Axboe6b063142019-01-10 22:13:58 -07002260 if (ctx->sqo_wq) {
2261 destroy_workqueue(ctx->sqo_wq);
2262 ctx->sqo_wq = NULL;
2263 }
2264}
2265
2266#if defined(CONFIG_UNIX)
2267static void io_destruct_skb(struct sk_buff *skb)
2268{
2269 struct io_ring_ctx *ctx = skb->sk->sk_user_data;
2270
2271 io_finish_async(ctx);
2272 unix_destruct_scm(skb);
2273}
2274
2275/*
2276 * Ensure the UNIX gc is aware of our file set, so we are certain that
2277 * the io_uring can be safely unregistered on process exit, even if we have
2278 * loops in the file referencing.
2279 */
2280static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
2281{
2282 struct sock *sk = ctx->ring_sock->sk;
2283 struct scm_fp_list *fpl;
2284 struct sk_buff *skb;
2285 int i;
2286
2287 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
2288 unsigned long inflight = ctx->user->unix_inflight + nr;
2289
2290 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
2291 return -EMFILE;
2292 }
2293
2294 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
2295 if (!fpl)
2296 return -ENOMEM;
2297
2298 skb = alloc_skb(0, GFP_KERNEL);
2299 if (!skb) {
2300 kfree(fpl);
2301 return -ENOMEM;
2302 }
2303
2304 skb->sk = sk;
2305 skb->destructor = io_destruct_skb;
2306
2307 fpl->user = get_uid(ctx->user);
2308 for (i = 0; i < nr; i++) {
2309 fpl->fp[i] = get_file(ctx->user_files[i + offset]);
2310 unix_inflight(fpl->user, fpl->fp[i]);
2311 }
2312
2313 fpl->max = fpl->count = nr;
2314 UNIXCB(skb).fp = fpl;
2315 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
2316 skb_queue_head(&sk->sk_receive_queue, skb);
2317
2318 for (i = 0; i < nr; i++)
2319 fput(fpl->fp[i]);
2320
2321 return 0;
2322}
2323
2324/*
2325 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
2326 * causes regular reference counting to break down. We rely on the UNIX
2327 * garbage collection to take care of this problem for us.
2328 */
2329static int io_sqe_files_scm(struct io_ring_ctx *ctx)
2330{
2331 unsigned left, total;
2332 int ret = 0;
2333
2334 total = 0;
2335 left = ctx->nr_user_files;
2336 while (left) {
2337 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07002338
2339 ret = __io_sqe_files_scm(ctx, this_files, total);
2340 if (ret)
2341 break;
2342 left -= this_files;
2343 total += this_files;
2344 }
2345
2346 if (!ret)
2347 return 0;
2348
2349 while (total < ctx->nr_user_files) {
2350 fput(ctx->user_files[total]);
2351 total++;
2352 }
2353
2354 return ret;
2355}
2356#else
2357static int io_sqe_files_scm(struct io_ring_ctx *ctx)
2358{
2359 return 0;
2360}
2361#endif
2362
2363static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
2364 unsigned nr_args)
2365{
2366 __s32 __user *fds = (__s32 __user *) arg;
2367 int fd, ret = 0;
2368 unsigned i;
2369
2370 if (ctx->user_files)
2371 return -EBUSY;
2372 if (!nr_args)
2373 return -EINVAL;
2374 if (nr_args > IORING_MAX_FIXED_FILES)
2375 return -EMFILE;
2376
2377 ctx->user_files = kcalloc(nr_args, sizeof(struct file *), GFP_KERNEL);
2378 if (!ctx->user_files)
2379 return -ENOMEM;
2380
2381 for (i = 0; i < nr_args; i++) {
2382 ret = -EFAULT;
2383 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
2384 break;
2385
2386 ctx->user_files[i] = fget(fd);
2387
2388 ret = -EBADF;
2389 if (!ctx->user_files[i])
2390 break;
2391 /*
2392 * Don't allow io_uring instances to be registered. If UNIX
2393 * isn't enabled, then this causes a reference cycle and this
2394 * instance can never get freed. If UNIX is enabled we'll
2395 * handle it just fine, but there's still no point in allowing
2396 * a ring fd as it doesn't support regular read/write anyway.
2397 */
2398 if (ctx->user_files[i]->f_op == &io_uring_fops) {
2399 fput(ctx->user_files[i]);
2400 break;
2401 }
2402 ctx->nr_user_files++;
2403 ret = 0;
2404 }
2405
2406 if (ret) {
2407 for (i = 0; i < ctx->nr_user_files; i++)
2408 fput(ctx->user_files[i]);
2409
2410 kfree(ctx->user_files);
Jens Axboe25adf502019-04-03 09:52:40 -06002411 ctx->user_files = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07002412 ctx->nr_user_files = 0;
2413 return ret;
2414 }
2415
2416 ret = io_sqe_files_scm(ctx);
2417 if (ret)
2418 io_sqe_files_unregister(ctx);
2419
2420 return ret;
2421}
2422
Jens Axboe6c271ce2019-01-10 11:22:30 -07002423static int io_sq_offload_start(struct io_ring_ctx *ctx,
2424 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002425{
2426 int ret;
2427
Jens Axboe6c271ce2019-01-10 11:22:30 -07002428 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002429 mmgrab(current->mm);
2430 ctx->sqo_mm = current->mm;
2431
Jens Axboe6c271ce2019-01-10 11:22:30 -07002432 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06002433 ret = -EPERM;
2434 if (!capable(CAP_SYS_ADMIN))
2435 goto err;
2436
Jens Axboe917257d2019-04-13 09:28:55 -06002437 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
2438 if (!ctx->sq_thread_idle)
2439 ctx->sq_thread_idle = HZ;
2440
Jens Axboe6c271ce2019-01-10 11:22:30 -07002441 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06002442 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002443
Jens Axboe917257d2019-04-13 09:28:55 -06002444 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06002445 if (cpu >= nr_cpu_ids)
2446 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08002447 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06002448 goto err;
2449
Jens Axboe6c271ce2019-01-10 11:22:30 -07002450 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
2451 ctx, cpu,
2452 "io_uring-sq");
2453 } else {
2454 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
2455 "io_uring-sq");
2456 }
2457 if (IS_ERR(ctx->sqo_thread)) {
2458 ret = PTR_ERR(ctx->sqo_thread);
2459 ctx->sqo_thread = NULL;
2460 goto err;
2461 }
2462 wake_up_process(ctx->sqo_thread);
2463 } else if (p->flags & IORING_SETUP_SQ_AFF) {
2464 /* Can't have SQ_AFF without SQPOLL */
2465 ret = -EINVAL;
2466 goto err;
2467 }
2468
Jens Axboe2b188cc2019-01-07 10:46:33 -07002469 /* Do QD, or 2 * CPUS, whatever is smallest */
2470 ctx->sqo_wq = alloc_workqueue("io_ring-wq", WQ_UNBOUND | WQ_FREEZABLE,
2471 min(ctx->sq_entries - 1, 2 * num_online_cpus()));
2472 if (!ctx->sqo_wq) {
2473 ret = -ENOMEM;
2474 goto err;
2475 }
2476
2477 return 0;
2478err:
Jens Axboe6c271ce2019-01-10 11:22:30 -07002479 io_sq_thread_stop(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002480 mmdrop(ctx->sqo_mm);
2481 ctx->sqo_mm = NULL;
2482 return ret;
2483}
2484
2485static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
2486{
2487 atomic_long_sub(nr_pages, &user->locked_vm);
2488}
2489
2490static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
2491{
2492 unsigned long page_limit, cur_pages, new_pages;
2493
2494 /* Don't allow more pages than we can safely lock */
2495 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
2496
2497 do {
2498 cur_pages = atomic_long_read(&user->locked_vm);
2499 new_pages = cur_pages + nr_pages;
2500 if (new_pages > page_limit)
2501 return -ENOMEM;
2502 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
2503 new_pages) != cur_pages);
2504
2505 return 0;
2506}
2507
2508static void io_mem_free(void *ptr)
2509{
Mark Rutland52e04ef2019-04-30 17:30:21 +01002510 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002511
Mark Rutland52e04ef2019-04-30 17:30:21 +01002512 if (!ptr)
2513 return;
2514
2515 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002516 if (put_page_testzero(page))
2517 free_compound_page(page);
2518}
2519
2520static void *io_mem_alloc(size_t size)
2521{
2522 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
2523 __GFP_NORETRY;
2524
2525 return (void *) __get_free_pages(gfp_flags, get_order(size));
2526}
2527
2528static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
2529{
2530 struct io_sq_ring *sq_ring;
2531 struct io_cq_ring *cq_ring;
2532 size_t bytes;
2533
2534 bytes = struct_size(sq_ring, array, sq_entries);
2535 bytes += array_size(sizeof(struct io_uring_sqe), sq_entries);
2536 bytes += struct_size(cq_ring, cqes, cq_entries);
2537
2538 return (bytes + PAGE_SIZE - 1) / PAGE_SIZE;
2539}
2540
Jens Axboeedafcce2019-01-09 09:16:05 -07002541static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
2542{
2543 int i, j;
2544
2545 if (!ctx->user_bufs)
2546 return -ENXIO;
2547
2548 for (i = 0; i < ctx->nr_user_bufs; i++) {
2549 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
2550
2551 for (j = 0; j < imu->nr_bvecs; j++)
2552 put_page(imu->bvec[j].bv_page);
2553
2554 if (ctx->account_mem)
2555 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01002556 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07002557 imu->nr_bvecs = 0;
2558 }
2559
2560 kfree(ctx->user_bufs);
2561 ctx->user_bufs = NULL;
2562 ctx->nr_user_bufs = 0;
2563 return 0;
2564}
2565
2566static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
2567 void __user *arg, unsigned index)
2568{
2569 struct iovec __user *src;
2570
2571#ifdef CONFIG_COMPAT
2572 if (ctx->compat) {
2573 struct compat_iovec __user *ciovs;
2574 struct compat_iovec ciov;
2575
2576 ciovs = (struct compat_iovec __user *) arg;
2577 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
2578 return -EFAULT;
2579
2580 dst->iov_base = (void __user *) (unsigned long) ciov.iov_base;
2581 dst->iov_len = ciov.iov_len;
2582 return 0;
2583 }
2584#endif
2585 src = (struct iovec __user *) arg;
2586 if (copy_from_user(dst, &src[index], sizeof(*dst)))
2587 return -EFAULT;
2588 return 0;
2589}
2590
2591static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
2592 unsigned nr_args)
2593{
2594 struct vm_area_struct **vmas = NULL;
2595 struct page **pages = NULL;
2596 int i, j, got_pages = 0;
2597 int ret = -EINVAL;
2598
2599 if (ctx->user_bufs)
2600 return -EBUSY;
2601 if (!nr_args || nr_args > UIO_MAXIOV)
2602 return -EINVAL;
2603
2604 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
2605 GFP_KERNEL);
2606 if (!ctx->user_bufs)
2607 return -ENOMEM;
2608
2609 for (i = 0; i < nr_args; i++) {
2610 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
2611 unsigned long off, start, end, ubuf;
2612 int pret, nr_pages;
2613 struct iovec iov;
2614 size_t size;
2615
2616 ret = io_copy_iov(ctx, &iov, arg, i);
2617 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03002618 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07002619
2620 /*
2621 * Don't impose further limits on the size and buffer
2622 * constraints here, we'll -EINVAL later when IO is
2623 * submitted if they are wrong.
2624 */
2625 ret = -EFAULT;
2626 if (!iov.iov_base || !iov.iov_len)
2627 goto err;
2628
2629 /* arbitrary limit, but we need something */
2630 if (iov.iov_len > SZ_1G)
2631 goto err;
2632
2633 ubuf = (unsigned long) iov.iov_base;
2634 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
2635 start = ubuf >> PAGE_SHIFT;
2636 nr_pages = end - start;
2637
2638 if (ctx->account_mem) {
2639 ret = io_account_mem(ctx->user, nr_pages);
2640 if (ret)
2641 goto err;
2642 }
2643
2644 ret = 0;
2645 if (!pages || nr_pages > got_pages) {
2646 kfree(vmas);
2647 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01002648 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07002649 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01002650 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07002651 sizeof(struct vm_area_struct *),
2652 GFP_KERNEL);
2653 if (!pages || !vmas) {
2654 ret = -ENOMEM;
2655 if (ctx->account_mem)
2656 io_unaccount_mem(ctx->user, nr_pages);
2657 goto err;
2658 }
2659 got_pages = nr_pages;
2660 }
2661
Mark Rutlandd4ef6472019-05-01 16:59:16 +01002662 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07002663 GFP_KERNEL);
2664 ret = -ENOMEM;
2665 if (!imu->bvec) {
2666 if (ctx->account_mem)
2667 io_unaccount_mem(ctx->user, nr_pages);
2668 goto err;
2669 }
2670
2671 ret = 0;
2672 down_read(&current->mm->mmap_sem);
Ira Weiny932f4a62019-05-13 17:17:03 -07002673 pret = get_user_pages(ubuf, nr_pages,
2674 FOLL_WRITE | FOLL_LONGTERM,
2675 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07002676 if (pret == nr_pages) {
2677 /* don't support file backed memory */
2678 for (j = 0; j < nr_pages; j++) {
2679 struct vm_area_struct *vma = vmas[j];
2680
2681 if (vma->vm_file &&
2682 !is_file_hugepages(vma->vm_file)) {
2683 ret = -EOPNOTSUPP;
2684 break;
2685 }
2686 }
2687 } else {
2688 ret = pret < 0 ? pret : -EFAULT;
2689 }
2690 up_read(&current->mm->mmap_sem);
2691 if (ret) {
2692 /*
2693 * if we did partial map, or found file backed vmas,
2694 * release any pages we did get
2695 */
2696 if (pret > 0) {
2697 for (j = 0; j < pret; j++)
2698 put_page(pages[j]);
2699 }
2700 if (ctx->account_mem)
2701 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01002702 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07002703 goto err;
2704 }
2705
2706 off = ubuf & ~PAGE_MASK;
2707 size = iov.iov_len;
2708 for (j = 0; j < nr_pages; j++) {
2709 size_t vec_len;
2710
2711 vec_len = min_t(size_t, size, PAGE_SIZE - off);
2712 imu->bvec[j].bv_page = pages[j];
2713 imu->bvec[j].bv_len = vec_len;
2714 imu->bvec[j].bv_offset = off;
2715 off = 0;
2716 size -= vec_len;
2717 }
2718 /* store original address for later verification */
2719 imu->ubuf = ubuf;
2720 imu->len = iov.iov_len;
2721 imu->nr_bvecs = nr_pages;
2722
2723 ctx->nr_user_bufs++;
2724 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01002725 kvfree(pages);
2726 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07002727 return 0;
2728err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01002729 kvfree(pages);
2730 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07002731 io_sqe_buffer_unregister(ctx);
2732 return ret;
2733}
2734
Jens Axboe9b402842019-04-11 11:45:41 -06002735static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
2736{
2737 __s32 __user *fds = arg;
2738 int fd;
2739
2740 if (ctx->cq_ev_fd)
2741 return -EBUSY;
2742
2743 if (copy_from_user(&fd, fds, sizeof(*fds)))
2744 return -EFAULT;
2745
2746 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
2747 if (IS_ERR(ctx->cq_ev_fd)) {
2748 int ret = PTR_ERR(ctx->cq_ev_fd);
2749 ctx->cq_ev_fd = NULL;
2750 return ret;
2751 }
2752
2753 return 0;
2754}
2755
2756static int io_eventfd_unregister(struct io_ring_ctx *ctx)
2757{
2758 if (ctx->cq_ev_fd) {
2759 eventfd_ctx_put(ctx->cq_ev_fd);
2760 ctx->cq_ev_fd = NULL;
2761 return 0;
2762 }
2763
2764 return -ENXIO;
2765}
2766
Jens Axboe2b188cc2019-01-07 10:46:33 -07002767static void io_ring_ctx_free(struct io_ring_ctx *ctx)
2768{
Jens Axboe6b063142019-01-10 22:13:58 -07002769 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002770 if (ctx->sqo_mm)
2771 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07002772
2773 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07002774 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07002775 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06002776 io_eventfd_unregister(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07002777
Jens Axboe2b188cc2019-01-07 10:46:33 -07002778#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07002779 if (ctx->ring_sock) {
2780 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07002781 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07002782 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002783#endif
2784
2785 io_mem_free(ctx->sq_ring);
2786 io_mem_free(ctx->sq_sqes);
2787 io_mem_free(ctx->cq_ring);
2788
2789 percpu_ref_exit(&ctx->refs);
2790 if (ctx->account_mem)
2791 io_unaccount_mem(ctx->user,
2792 ring_pages(ctx->sq_entries, ctx->cq_entries));
2793 free_uid(ctx->user);
2794 kfree(ctx);
2795}
2796
2797static __poll_t io_uring_poll(struct file *file, poll_table *wait)
2798{
2799 struct io_ring_ctx *ctx = file->private_data;
2800 __poll_t mask = 0;
2801
2802 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02002803 /*
2804 * synchronizes with barrier from wq_has_sleeper call in
2805 * io_commit_cqring
2806 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07002807 smp_rmb();
Stefan Bühlerfb775fa2019-04-19 11:57:46 +02002808 if (READ_ONCE(ctx->sq_ring->r.tail) - ctx->cached_sq_head !=
2809 ctx->sq_ring->ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002810 mask |= EPOLLOUT | EPOLLWRNORM;
2811 if (READ_ONCE(ctx->cq_ring->r.head) != ctx->cached_cq_tail)
2812 mask |= EPOLLIN | EPOLLRDNORM;
2813
2814 return mask;
2815}
2816
2817static int io_uring_fasync(int fd, struct file *file, int on)
2818{
2819 struct io_ring_ctx *ctx = file->private_data;
2820
2821 return fasync_helper(fd, file, on, &ctx->cq_fasync);
2822}
2823
2824static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
2825{
2826 mutex_lock(&ctx->uring_lock);
2827 percpu_ref_kill(&ctx->refs);
2828 mutex_unlock(&ctx->uring_lock);
2829
Jens Axboe221c5eb2019-01-17 09:41:58 -07002830 io_poll_remove_all(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07002831 io_iopoll_reap_events(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002832 wait_for_completion(&ctx->ctx_done);
2833 io_ring_ctx_free(ctx);
2834}
2835
2836static int io_uring_release(struct inode *inode, struct file *file)
2837{
2838 struct io_ring_ctx *ctx = file->private_data;
2839
2840 file->private_data = NULL;
2841 io_ring_ctx_wait_and_kill(ctx);
2842 return 0;
2843}
2844
2845static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
2846{
2847 loff_t offset = (loff_t) vma->vm_pgoff << PAGE_SHIFT;
2848 unsigned long sz = vma->vm_end - vma->vm_start;
2849 struct io_ring_ctx *ctx = file->private_data;
2850 unsigned long pfn;
2851 struct page *page;
2852 void *ptr;
2853
2854 switch (offset) {
2855 case IORING_OFF_SQ_RING:
2856 ptr = ctx->sq_ring;
2857 break;
2858 case IORING_OFF_SQES:
2859 ptr = ctx->sq_sqes;
2860 break;
2861 case IORING_OFF_CQ_RING:
2862 ptr = ctx->cq_ring;
2863 break;
2864 default:
2865 return -EINVAL;
2866 }
2867
2868 page = virt_to_head_page(ptr);
2869 if (sz > (PAGE_SIZE << compound_order(page)))
2870 return -EINVAL;
2871
2872 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
2873 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
2874}
2875
2876SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
2877 u32, min_complete, u32, flags, const sigset_t __user *, sig,
2878 size_t, sigsz)
2879{
2880 struct io_ring_ctx *ctx;
2881 long ret = -EBADF;
2882 int submitted = 0;
2883 struct fd f;
2884
Jens Axboe6c271ce2019-01-10 11:22:30 -07002885 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002886 return -EINVAL;
2887
2888 f = fdget(fd);
2889 if (!f.file)
2890 return -EBADF;
2891
2892 ret = -EOPNOTSUPP;
2893 if (f.file->f_op != &io_uring_fops)
2894 goto out_fput;
2895
2896 ret = -ENXIO;
2897 ctx = f.file->private_data;
2898 if (!percpu_ref_tryget(&ctx->refs))
2899 goto out_fput;
2900
Jens Axboe6c271ce2019-01-10 11:22:30 -07002901 /*
2902 * For SQ polling, the thread will do all submissions and completions.
2903 * Just return the requested submit count, and wake the thread if
2904 * we were asked to.
2905 */
2906 if (ctx->flags & IORING_SETUP_SQPOLL) {
2907 if (flags & IORING_ENTER_SQ_WAKEUP)
2908 wake_up(&ctx->sqo_wait);
2909 submitted = to_submit;
2910 goto out_ctx;
2911 }
2912
Jens Axboe2b188cc2019-01-07 10:46:33 -07002913 ret = 0;
2914 if (to_submit) {
2915 to_submit = min(to_submit, ctx->sq_entries);
2916
2917 mutex_lock(&ctx->uring_lock);
2918 submitted = io_ring_submit(ctx, to_submit);
2919 mutex_unlock(&ctx->uring_lock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002920 }
2921 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07002922 unsigned nr_events = 0;
2923
Jens Axboe2b188cc2019-01-07 10:46:33 -07002924 min_complete = min(min_complete, ctx->cq_entries);
2925
Jens Axboedef596e2019-01-09 08:59:42 -07002926 if (ctx->flags & IORING_SETUP_IOPOLL) {
2927 mutex_lock(&ctx->uring_lock);
2928 ret = io_iopoll_check(ctx, &nr_events, min_complete);
2929 mutex_unlock(&ctx->uring_lock);
2930 } else {
2931 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
2932 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002933 }
2934
2935out_ctx:
2936 io_ring_drop_ctx_refs(ctx, 1);
2937out_fput:
2938 fdput(f);
2939 return submitted ? submitted : ret;
2940}
2941
2942static const struct file_operations io_uring_fops = {
2943 .release = io_uring_release,
2944 .mmap = io_uring_mmap,
2945 .poll = io_uring_poll,
2946 .fasync = io_uring_fasync,
2947};
2948
2949static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
2950 struct io_uring_params *p)
2951{
2952 struct io_sq_ring *sq_ring;
2953 struct io_cq_ring *cq_ring;
2954 size_t size;
2955
2956 sq_ring = io_mem_alloc(struct_size(sq_ring, array, p->sq_entries));
2957 if (!sq_ring)
2958 return -ENOMEM;
2959
2960 ctx->sq_ring = sq_ring;
2961 sq_ring->ring_mask = p->sq_entries - 1;
2962 sq_ring->ring_entries = p->sq_entries;
2963 ctx->sq_mask = sq_ring->ring_mask;
2964 ctx->sq_entries = sq_ring->ring_entries;
2965
2966 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
2967 if (size == SIZE_MAX)
2968 return -EOVERFLOW;
2969
2970 ctx->sq_sqes = io_mem_alloc(size);
Mark Rutland52e04ef2019-04-30 17:30:21 +01002971 if (!ctx->sq_sqes)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002972 return -ENOMEM;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002973
2974 cq_ring = io_mem_alloc(struct_size(cq_ring, cqes, p->cq_entries));
Mark Rutland52e04ef2019-04-30 17:30:21 +01002975 if (!cq_ring)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002976 return -ENOMEM;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002977
2978 ctx->cq_ring = cq_ring;
2979 cq_ring->ring_mask = p->cq_entries - 1;
2980 cq_ring->ring_entries = p->cq_entries;
2981 ctx->cq_mask = cq_ring->ring_mask;
2982 ctx->cq_entries = cq_ring->ring_entries;
2983 return 0;
2984}
2985
2986/*
2987 * Allocate an anonymous fd, this is what constitutes the application
2988 * visible backing of an io_uring instance. The application mmaps this
2989 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
2990 * we have to tie this fd to a socket for file garbage collection purposes.
2991 */
2992static int io_uring_get_fd(struct io_ring_ctx *ctx)
2993{
2994 struct file *file;
2995 int ret;
2996
2997#if defined(CONFIG_UNIX)
2998 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
2999 &ctx->ring_sock);
3000 if (ret)
3001 return ret;
3002#endif
3003
3004 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
3005 if (ret < 0)
3006 goto err;
3007
3008 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
3009 O_RDWR | O_CLOEXEC);
3010 if (IS_ERR(file)) {
3011 put_unused_fd(ret);
3012 ret = PTR_ERR(file);
3013 goto err;
3014 }
3015
3016#if defined(CONFIG_UNIX)
3017 ctx->ring_sock->file = file;
Jens Axboe6b063142019-01-10 22:13:58 -07003018 ctx->ring_sock->sk->sk_user_data = ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003019#endif
3020 fd_install(ret, file);
3021 return ret;
3022err:
3023#if defined(CONFIG_UNIX)
3024 sock_release(ctx->ring_sock);
3025 ctx->ring_sock = NULL;
3026#endif
3027 return ret;
3028}
3029
3030static int io_uring_create(unsigned entries, struct io_uring_params *p)
3031{
3032 struct user_struct *user = NULL;
3033 struct io_ring_ctx *ctx;
3034 bool account_mem;
3035 int ret;
3036
3037 if (!entries || entries > IORING_MAX_ENTRIES)
3038 return -EINVAL;
3039
3040 /*
3041 * Use twice as many entries for the CQ ring. It's possible for the
3042 * application to drive a higher depth than the size of the SQ ring,
3043 * since the sqes are only used at submission time. This allows for
3044 * some flexibility in overcommitting a bit.
3045 */
3046 p->sq_entries = roundup_pow_of_two(entries);
3047 p->cq_entries = 2 * p->sq_entries;
3048
3049 user = get_uid(current_user());
3050 account_mem = !capable(CAP_IPC_LOCK);
3051
3052 if (account_mem) {
3053 ret = io_account_mem(user,
3054 ring_pages(p->sq_entries, p->cq_entries));
3055 if (ret) {
3056 free_uid(user);
3057 return ret;
3058 }
3059 }
3060
3061 ctx = io_ring_ctx_alloc(p);
3062 if (!ctx) {
3063 if (account_mem)
3064 io_unaccount_mem(user, ring_pages(p->sq_entries,
3065 p->cq_entries));
3066 free_uid(user);
3067 return -ENOMEM;
3068 }
3069 ctx->compat = in_compat_syscall();
3070 ctx->account_mem = account_mem;
3071 ctx->user = user;
3072
3073 ret = io_allocate_scq_urings(ctx, p);
3074 if (ret)
3075 goto err;
3076
Jens Axboe6c271ce2019-01-10 11:22:30 -07003077 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003078 if (ret)
3079 goto err;
3080
3081 ret = io_uring_get_fd(ctx);
3082 if (ret < 0)
3083 goto err;
3084
3085 memset(&p->sq_off, 0, sizeof(p->sq_off));
3086 p->sq_off.head = offsetof(struct io_sq_ring, r.head);
3087 p->sq_off.tail = offsetof(struct io_sq_ring, r.tail);
3088 p->sq_off.ring_mask = offsetof(struct io_sq_ring, ring_mask);
3089 p->sq_off.ring_entries = offsetof(struct io_sq_ring, ring_entries);
3090 p->sq_off.flags = offsetof(struct io_sq_ring, flags);
3091 p->sq_off.dropped = offsetof(struct io_sq_ring, dropped);
3092 p->sq_off.array = offsetof(struct io_sq_ring, array);
3093
3094 memset(&p->cq_off, 0, sizeof(p->cq_off));
3095 p->cq_off.head = offsetof(struct io_cq_ring, r.head);
3096 p->cq_off.tail = offsetof(struct io_cq_ring, r.tail);
3097 p->cq_off.ring_mask = offsetof(struct io_cq_ring, ring_mask);
3098 p->cq_off.ring_entries = offsetof(struct io_cq_ring, ring_entries);
3099 p->cq_off.overflow = offsetof(struct io_cq_ring, overflow);
3100 p->cq_off.cqes = offsetof(struct io_cq_ring, cqes);
3101 return ret;
3102err:
3103 io_ring_ctx_wait_and_kill(ctx);
3104 return ret;
3105}
3106
3107/*
3108 * Sets up an aio uring context, and returns the fd. Applications asks for a
3109 * ring size, we return the actual sq/cq ring sizes (among other things) in the
3110 * params structure passed in.
3111 */
3112static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
3113{
3114 struct io_uring_params p;
3115 long ret;
3116 int i;
3117
3118 if (copy_from_user(&p, params, sizeof(p)))
3119 return -EFAULT;
3120 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
3121 if (p.resv[i])
3122 return -EINVAL;
3123 }
3124
Jens Axboe6c271ce2019-01-10 11:22:30 -07003125 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
3126 IORING_SETUP_SQ_AFF))
Jens Axboe2b188cc2019-01-07 10:46:33 -07003127 return -EINVAL;
3128
3129 ret = io_uring_create(entries, &p);
3130 if (ret < 0)
3131 return ret;
3132
3133 if (copy_to_user(params, &p, sizeof(p)))
3134 return -EFAULT;
3135
3136 return ret;
3137}
3138
3139SYSCALL_DEFINE2(io_uring_setup, u32, entries,
3140 struct io_uring_params __user *, params)
3141{
3142 return io_uring_setup(entries, params);
3143}
3144
Jens Axboeedafcce2019-01-09 09:16:05 -07003145static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
3146 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06003147 __releases(ctx->uring_lock)
3148 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07003149{
3150 int ret;
3151
Jens Axboe35fa71a2019-04-22 10:23:23 -06003152 /*
3153 * We're inside the ring mutex, if the ref is already dying, then
3154 * someone else killed the ctx or is already going through
3155 * io_uring_register().
3156 */
3157 if (percpu_ref_is_dying(&ctx->refs))
3158 return -ENXIO;
3159
Jens Axboeedafcce2019-01-09 09:16:05 -07003160 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06003161
3162 /*
3163 * Drop uring mutex before waiting for references to exit. If another
3164 * thread is currently inside io_uring_enter() it might need to grab
3165 * the uring_lock to make progress. If we hold it here across the drain
3166 * wait, then we can deadlock. It's safe to drop the mutex here, since
3167 * no new references will come in after we've killed the percpu ref.
3168 */
3169 mutex_unlock(&ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07003170 wait_for_completion(&ctx->ctx_done);
Jens Axboeb19062a2019-04-15 10:49:38 -06003171 mutex_lock(&ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07003172
3173 switch (opcode) {
3174 case IORING_REGISTER_BUFFERS:
3175 ret = io_sqe_buffer_register(ctx, arg, nr_args);
3176 break;
3177 case IORING_UNREGISTER_BUFFERS:
3178 ret = -EINVAL;
3179 if (arg || nr_args)
3180 break;
3181 ret = io_sqe_buffer_unregister(ctx);
3182 break;
Jens Axboe6b063142019-01-10 22:13:58 -07003183 case IORING_REGISTER_FILES:
3184 ret = io_sqe_files_register(ctx, arg, nr_args);
3185 break;
3186 case IORING_UNREGISTER_FILES:
3187 ret = -EINVAL;
3188 if (arg || nr_args)
3189 break;
3190 ret = io_sqe_files_unregister(ctx);
3191 break;
Jens Axboe9b402842019-04-11 11:45:41 -06003192 case IORING_REGISTER_EVENTFD:
3193 ret = -EINVAL;
3194 if (nr_args != 1)
3195 break;
3196 ret = io_eventfd_register(ctx, arg);
3197 break;
3198 case IORING_UNREGISTER_EVENTFD:
3199 ret = -EINVAL;
3200 if (arg || nr_args)
3201 break;
3202 ret = io_eventfd_unregister(ctx);
3203 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07003204 default:
3205 ret = -EINVAL;
3206 break;
3207 }
3208
3209 /* bring the ctx back to life */
3210 reinit_completion(&ctx->ctx_done);
3211 percpu_ref_reinit(&ctx->refs);
3212 return ret;
3213}
3214
3215SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
3216 void __user *, arg, unsigned int, nr_args)
3217{
3218 struct io_ring_ctx *ctx;
3219 long ret = -EBADF;
3220 struct fd f;
3221
3222 f = fdget(fd);
3223 if (!f.file)
3224 return -EBADF;
3225
3226 ret = -EOPNOTSUPP;
3227 if (f.file->f_op != &io_uring_fops)
3228 goto out_fput;
3229
3230 ctx = f.file->private_data;
3231
3232 mutex_lock(&ctx->uring_lock);
3233 ret = __io_uring_register(ctx, opcode, arg, nr_args);
3234 mutex_unlock(&ctx->uring_lock);
3235out_fput:
3236 fdput(f);
3237 return ret;
3238}
3239
Jens Axboe2b188cc2019-01-07 10:46:33 -07003240static int __init io_uring_init(void)
3241{
3242 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
3243 return 0;
3244};
3245__initcall(io_uring_init);