blob: 05a299e80159b558124c7d3895388e5340e938cc [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
Daniel Xu5277dea2019-09-14 14:23:45 -070078#define IORING_MAX_ENTRIES 32768
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/*
Hristo Venev75b28af2019-08-26 17:23:46 +000087 * This data is shared with the application through the mmap at offsets
88 * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING.
Stefan Bühler1e84b972019-04-24 23:54:16 +020089 *
90 * The offsets to the member fields are published through struct
91 * io_sqring_offsets when calling io_uring_setup.
92 */
Hristo Venev75b28af2019-08-26 17:23:46 +000093struct io_rings {
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 *
Hristo Venev75b28af2019-08-26 17:23:46 +000098 * The kernel controls head of the sq ring and the tail of the cq ring,
99 * and the application controls tail of the sq ring and the head of the
100 * cq ring.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200101 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000102 struct io_uring sq, cq;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200103 /*
Hristo Venev75b28af2019-08-26 17:23:46 +0000104 * Bitmasks to apply to head and tail offsets (constant, equals
Stefan Bühler1e84b972019-04-24 23:54:16 +0200105 * ring_entries - 1)
106 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000107 u32 sq_ring_mask, cq_ring_mask;
108 /* Ring sizes (constant, power of 2) */
109 u32 sq_ring_entries, cq_ring_entries;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200110 /*
111 * Number of invalid entries dropped by the kernel due to
112 * invalid index stored in array
113 *
114 * Written by the kernel, shouldn't be modified by the
115 * application (i.e. get number of "new events" by comparing to
116 * cached value).
117 *
118 * After a new SQ head value was read by the application this
119 * counter includes all submissions that were dropped reaching
120 * the new SQ head (and possibly more).
121 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000122 u32 sq_dropped;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200123 /*
124 * Runtime flags
125 *
126 * Written by the kernel, shouldn't be modified by the
127 * application.
128 *
129 * The application needs a full memory barrier before checking
130 * for IORING_SQ_NEED_WAKEUP after updating the sq tail.
131 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000132 u32 sq_flags;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200133 /*
134 * Number of completion events lost because the queue was full;
135 * this should be avoided by the application by making sure
136 * there are not more requests pending thatn there is space in
137 * the completion queue.
138 *
139 * Written by the kernel, shouldn't be modified by the
140 * application (i.e. get number of "new events" by comparing to
141 * cached value).
142 *
143 * As completion events come in out of order this counter is not
144 * ordered with any other data.
145 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000146 u32 cq_overflow;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200147 /*
148 * Ring buffer of completion events.
149 *
150 * The kernel writes completion events fresh every time they are
151 * produced, so the application is allowed to modify pending
152 * entries.
153 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000154 struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700155};
156
Jens Axboeedafcce2019-01-09 09:16:05 -0700157struct io_mapped_ubuf {
158 u64 ubuf;
159 size_t len;
160 struct bio_vec *bvec;
161 unsigned int nr_bvecs;
162};
163
Jens Axboe31b51512019-01-18 22:56:34 -0700164struct async_list {
165 spinlock_t lock;
166 atomic_t cnt;
167 struct list_head list;
168
169 struct file *file;
Jens Axboe6d5d5ac2019-09-11 10:16:13 -0600170 off_t io_start;
Zhengyuan Liu9310a7ba2019-07-22 10:23:27 +0800171 size_t io_len;
Jens Axboe31b51512019-01-18 22:56:34 -0700172};
173
Jens Axboe2b188cc2019-01-07 10:46:33 -0700174struct io_ring_ctx {
175 struct {
176 struct percpu_ref refs;
177 } ____cacheline_aligned_in_smp;
178
179 struct {
180 unsigned int flags;
181 bool compat;
182 bool account_mem;
183
Hristo Venev75b28af2019-08-26 17:23:46 +0000184 /*
185 * Ring buffer of indices into array of io_uring_sqe, which is
186 * mmapped by the application using the IORING_OFF_SQES offset.
187 *
188 * This indirection could e.g. be used to assign fixed
189 * io_uring_sqe entries to operations and only submit them to
190 * the queue when needed.
191 *
192 * The kernel modifies neither the indices array nor the entries
193 * array.
194 */
195 u32 *sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700196 unsigned cached_sq_head;
197 unsigned sq_entries;
198 unsigned sq_mask;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700199 unsigned sq_thread_idle;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700200 struct io_uring_sqe *sq_sqes;
Jens Axboede0617e2019-04-06 21:51:27 -0600201
202 struct list_head defer_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700203 } ____cacheline_aligned_in_smp;
204
205 /* IO offload */
Jens Axboe54a91f32019-09-10 09:15:04 -0600206 struct workqueue_struct *sqo_wq[2];
Jens Axboe6c271ce2019-01-10 11:22:30 -0700207 struct task_struct *sqo_thread; /* if using sq thread polling */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700208 struct mm_struct *sqo_mm;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700209 wait_queue_head_t sqo_wait;
Jackie Liua4c0b3d2019-07-08 13:41:12 +0800210 struct completion sqo_thread_started;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700211
212 struct {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700213 unsigned cached_cq_tail;
214 unsigned cq_entries;
215 unsigned cq_mask;
216 struct wait_queue_head cq_wait;
217 struct fasync_struct *cq_fasync;
Jens Axboe9b402842019-04-11 11:45:41 -0600218 struct eventfd_ctx *cq_ev_fd;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700219 } ____cacheline_aligned_in_smp;
220
Hristo Venev75b28af2019-08-26 17:23:46 +0000221 struct io_rings *rings;
222
Jens Axboe6b063142019-01-10 22:13:58 -0700223 /*
224 * If used, fixed file set. Writers must ensure that ->refs is dead,
225 * readers must ensure that ->refs is alive as long as the file* is
226 * used. Only updated through io_uring_register(2).
227 */
228 struct file **user_files;
229 unsigned nr_user_files;
230
Jens Axboeedafcce2019-01-09 09:16:05 -0700231 /* if used, fixed mapped user buffers */
232 unsigned nr_user_bufs;
233 struct io_mapped_ubuf *user_bufs;
234
Jens Axboe2b188cc2019-01-07 10:46:33 -0700235 struct user_struct *user;
236
237 struct completion ctx_done;
238
239 struct {
240 struct mutex uring_lock;
241 wait_queue_head_t wait;
242 } ____cacheline_aligned_in_smp;
243
244 struct {
245 spinlock_t completion_lock;
Jens Axboedef596e2019-01-09 08:59:42 -0700246 bool poll_multi_file;
247 /*
248 * ->poll_list is protected by the ctx->uring_lock for
249 * io_uring instances that don't use IORING_SETUP_SQPOLL.
250 * For SQPOLL, only the single threaded io_sq_thread() will
251 * manipulate the list, hence no extra locking is needed there.
252 */
253 struct list_head poll_list;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700254 struct list_head cancel_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700255 } ____cacheline_aligned_in_smp;
256
Jens Axboe31b51512019-01-18 22:56:34 -0700257 struct async_list pending_async[2];
258
Jens Axboe2b188cc2019-01-07 10:46:33 -0700259#if defined(CONFIG_UNIX)
260 struct socket *ring_sock;
261#endif
262};
263
264struct sqe_submit {
265 const struct io_uring_sqe *sqe;
266 unsigned short index;
Jackie Liu8776f3f2019-09-09 20:50:39 +0800267 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700268 bool has_user;
Jens Axboedef596e2019-01-09 08:59:42 -0700269 bool needs_lock;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700270 bool needs_fixed_file;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700271};
272
Jens Axboe09bb8392019-03-13 12:39:28 -0600273/*
274 * First field must be the file pointer in all the
275 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
276 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700277struct io_poll_iocb {
278 struct file *file;
279 struct wait_queue_head *head;
280 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600281 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700282 bool canceled;
283 struct wait_queue_entry wait;
284};
285
Jens Axboe09bb8392019-03-13 12:39:28 -0600286/*
287 * NOTE! Each of the iocb union members has the file pointer
288 * as the first entry in their struct definition. So you can
289 * access the file pointer through any of the sub-structs,
290 * or directly as just 'ki_filp' in this struct.
291 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700292struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700293 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600294 struct file *file;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700295 struct kiocb rw;
296 struct io_poll_iocb poll;
297 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700298
299 struct sqe_submit submit;
300
301 struct io_ring_ctx *ctx;
302 struct list_head list;
Jens Axboe9e645e112019-05-10 16:07:28 -0600303 struct list_head link_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700304 unsigned int flags;
Jens Axboec16361c2019-01-17 08:39:48 -0700305 refcount_t refs;
Stefan Bühler8449eed2019-04-27 20:34:19 +0200306#define REQ_F_NOWAIT 1 /* must not punt to workers */
Jens Axboedef596e2019-01-09 08:59:42 -0700307#define REQ_F_IOPOLL_COMPLETED 2 /* polled IO has completed */
Jens Axboe6b063142019-01-10 22:13:58 -0700308#define REQ_F_FIXED_FILE 4 /* ctx owns file */
Jens Axboe31b51512019-01-18 22:56:34 -0700309#define REQ_F_SEQ_PREV 8 /* sequential with previous */
Stefan Bühlere2033e32019-05-11 19:08:01 +0200310#define REQ_F_IO_DRAIN 16 /* drain existing IO first */
311#define REQ_F_IO_DRAINED 32 /* drain done */
Jens Axboe9e645e112019-05-10 16:07:28 -0600312#define REQ_F_LINK 64 /* linked sqes */
Zhengyuan Liuf7b76ac2019-07-16 23:26:14 +0800313#define REQ_F_LINK_DONE 128 /* linked sqes done */
314#define REQ_F_FAIL_LINK 256 /* fail rest of links */
Jackie Liu4fe2c962019-09-09 20:50:40 +0800315#define REQ_F_SHADOW_DRAIN 512 /* link-drain shadow req */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700316 u64 user_data;
Jens Axboe9e645e112019-05-10 16:07:28 -0600317 u32 result;
Jens Axboede0617e2019-04-06 21:51:27 -0600318 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700319
320 struct work_struct work;
321};
322
323#define IO_PLUG_THRESHOLD 2
Jens Axboedef596e2019-01-09 08:59:42 -0700324#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700325
Jens Axboe9a56a232019-01-09 09:06:50 -0700326struct io_submit_state {
327 struct blk_plug plug;
328
329 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700330 * io_kiocb alloc cache
331 */
332 void *reqs[IO_IOPOLL_BATCH];
333 unsigned int free_reqs;
334 unsigned int cur_req;
335
336 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700337 * File reference cache
338 */
339 struct file *file;
340 unsigned int fd;
341 unsigned int has_refs;
342 unsigned int used_refs;
343 unsigned int ios_left;
344};
345
Jens Axboede0617e2019-04-06 21:51:27 -0600346static void io_sq_wq_submit_work(struct work_struct *work);
Jackie Liu4fe2c962019-09-09 20:50:40 +0800347static void __io_free_req(struct io_kiocb *req);
Jens Axboede0617e2019-04-06 21:51:27 -0600348
Jens Axboe2b188cc2019-01-07 10:46:33 -0700349static struct kmem_cache *req_cachep;
350
351static const struct file_operations io_uring_fops;
352
353struct sock *io_uring_get_socket(struct file *file)
354{
355#if defined(CONFIG_UNIX)
356 if (file->f_op == &io_uring_fops) {
357 struct io_ring_ctx *ctx = file->private_data;
358
359 return ctx->ring_sock->sk;
360 }
361#endif
362 return NULL;
363}
364EXPORT_SYMBOL(io_uring_get_socket);
365
366static void io_ring_ctx_ref_free(struct percpu_ref *ref)
367{
368 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
369
370 complete(&ctx->ctx_done);
371}
372
373static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
374{
375 struct io_ring_ctx *ctx;
Jens Axboe31b51512019-01-18 22:56:34 -0700376 int i;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700377
378 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
379 if (!ctx)
380 return NULL;
381
Roman Gushchin21482892019-05-07 10:01:48 -0700382 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
383 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700384 kfree(ctx);
385 return NULL;
386 }
387
388 ctx->flags = p->flags;
389 init_waitqueue_head(&ctx->cq_wait);
390 init_completion(&ctx->ctx_done);
Jackie Liua4c0b3d2019-07-08 13:41:12 +0800391 init_completion(&ctx->sqo_thread_started);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700392 mutex_init(&ctx->uring_lock);
393 init_waitqueue_head(&ctx->wait);
Jens Axboe31b51512019-01-18 22:56:34 -0700394 for (i = 0; i < ARRAY_SIZE(ctx->pending_async); i++) {
395 spin_lock_init(&ctx->pending_async[i].lock);
396 INIT_LIST_HEAD(&ctx->pending_async[i].list);
397 atomic_set(&ctx->pending_async[i].cnt, 0);
398 }
Jens Axboe2b188cc2019-01-07 10:46:33 -0700399 spin_lock_init(&ctx->completion_lock);
Jens Axboedef596e2019-01-09 08:59:42 -0700400 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboe221c5eb2019-01-17 09:41:58 -0700401 INIT_LIST_HEAD(&ctx->cancel_list);
Jens Axboede0617e2019-04-06 21:51:27 -0600402 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700403 return ctx;
404}
405
Jens Axboede0617e2019-04-06 21:51:27 -0600406static inline bool io_sequence_defer(struct io_ring_ctx *ctx,
407 struct io_kiocb *req)
408{
409 if ((req->flags & (REQ_F_IO_DRAIN|REQ_F_IO_DRAINED)) != REQ_F_IO_DRAIN)
410 return false;
411
Hristo Venev75b28af2019-08-26 17:23:46 +0000412 return req->sequence != ctx->cached_cq_tail + ctx->rings->sq_dropped;
Jens Axboede0617e2019-04-06 21:51:27 -0600413}
414
415static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
416{
417 struct io_kiocb *req;
418
419 if (list_empty(&ctx->defer_list))
420 return NULL;
421
422 req = list_first_entry(&ctx->defer_list, struct io_kiocb, list);
423 if (!io_sequence_defer(ctx, req)) {
424 list_del_init(&req->list);
425 return req;
426 }
427
428 return NULL;
429}
430
431static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700432{
Hristo Venev75b28af2019-08-26 17:23:46 +0000433 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700434
Hristo Venev75b28af2019-08-26 17:23:46 +0000435 if (ctx->cached_cq_tail != READ_ONCE(rings->cq.tail)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700436 /* order cqe stores with ring update */
Hristo Venev75b28af2019-08-26 17:23:46 +0000437 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700438
Jens Axboe2b188cc2019-01-07 10:46:33 -0700439 if (wq_has_sleeper(&ctx->cq_wait)) {
440 wake_up_interruptible(&ctx->cq_wait);
441 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
442 }
443 }
444}
445
Jens Axboe18d9be12019-09-10 09:13:05 -0600446static inline void io_queue_async_work(struct io_ring_ctx *ctx,
447 struct io_kiocb *req)
448{
Jens Axboe6cc47d12019-09-18 11:18:23 -0600449 int rw = 0;
Jens Axboe54a91f32019-09-10 09:15:04 -0600450
Jens Axboe6cc47d12019-09-18 11:18:23 -0600451 if (req->submit.sqe) {
452 switch (req->submit.sqe->opcode) {
453 case IORING_OP_WRITEV:
454 case IORING_OP_WRITE_FIXED:
455 rw = !(req->rw.ki_flags & IOCB_DIRECT);
456 break;
457 }
Jens Axboe54a91f32019-09-10 09:15:04 -0600458 }
459
460 queue_work(ctx->sqo_wq[rw], &req->work);
Jens Axboe18d9be12019-09-10 09:13:05 -0600461}
462
Jens Axboede0617e2019-04-06 21:51:27 -0600463static void io_commit_cqring(struct io_ring_ctx *ctx)
464{
465 struct io_kiocb *req;
466
467 __io_commit_cqring(ctx);
468
469 while ((req = io_get_deferred_req(ctx)) != NULL) {
Jackie Liu4fe2c962019-09-09 20:50:40 +0800470 if (req->flags & REQ_F_SHADOW_DRAIN) {
471 /* Just for drain, free it. */
472 __io_free_req(req);
473 continue;
474 }
Jens Axboede0617e2019-04-06 21:51:27 -0600475 req->flags |= REQ_F_IO_DRAINED;
Jens Axboe18d9be12019-09-10 09:13:05 -0600476 io_queue_async_work(ctx, req);
Jens Axboede0617e2019-04-06 21:51:27 -0600477 }
478}
479
Jens Axboe2b188cc2019-01-07 10:46:33 -0700480static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
481{
Hristo Venev75b28af2019-08-26 17:23:46 +0000482 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700483 unsigned tail;
484
485 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +0200486 /*
487 * writes to the cq entry need to come after reading head; the
488 * control dependency is enough as we're using WRITE_ONCE to
489 * fill the cq entry
490 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000491 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700492 return NULL;
493
494 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +0000495 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -0700496}
497
498static void io_cqring_fill_event(struct io_ring_ctx *ctx, u64 ki_user_data,
Jens Axboec71ffb62019-05-13 20:58:29 -0600499 long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700500{
501 struct io_uring_cqe *cqe;
502
503 /*
504 * If we can't get a cq entry, userspace overflowed the
505 * submission (by quite a lot). Increment the overflow count in
506 * the ring.
507 */
508 cqe = io_get_cqring(ctx);
509 if (cqe) {
510 WRITE_ONCE(cqe->user_data, ki_user_data);
511 WRITE_ONCE(cqe->res, res);
Jens Axboec71ffb62019-05-13 20:58:29 -0600512 WRITE_ONCE(cqe->flags, 0);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700513 } else {
Hristo Venev75b28af2019-08-26 17:23:46 +0000514 unsigned overflow = READ_ONCE(ctx->rings->cq_overflow);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700515
Hristo Venev75b28af2019-08-26 17:23:46 +0000516 WRITE_ONCE(ctx->rings->cq_overflow, overflow + 1);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700517 }
518}
519
Jens Axboe8c838782019-03-12 15:48:16 -0600520static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
521{
522 if (waitqueue_active(&ctx->wait))
523 wake_up(&ctx->wait);
524 if (waitqueue_active(&ctx->sqo_wait))
525 wake_up(&ctx->sqo_wait);
Jens Axboe9b402842019-04-11 11:45:41 -0600526 if (ctx->cq_ev_fd)
527 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -0600528}
529
530static void io_cqring_add_event(struct io_ring_ctx *ctx, u64 user_data,
Jens Axboec71ffb62019-05-13 20:58:29 -0600531 long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700532{
533 unsigned long flags;
534
535 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboec71ffb62019-05-13 20:58:29 -0600536 io_cqring_fill_event(ctx, user_data, res);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700537 io_commit_cqring(ctx);
538 spin_unlock_irqrestore(&ctx->completion_lock, flags);
539
Jens Axboe8c838782019-03-12 15:48:16 -0600540 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700541}
542
543static void io_ring_drop_ctx_refs(struct io_ring_ctx *ctx, unsigned refs)
544{
545 percpu_ref_put_many(&ctx->refs, refs);
546
547 if (waitqueue_active(&ctx->wait))
548 wake_up(&ctx->wait);
549}
550
Jens Axboe2579f912019-01-09 09:10:43 -0700551static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
552 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700553{
Jens Axboefd6fab22019-03-14 16:30:06 -0600554 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700555 struct io_kiocb *req;
556
557 if (!percpu_ref_tryget(&ctx->refs))
558 return NULL;
559
Jens Axboe2579f912019-01-09 09:10:43 -0700560 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -0600561 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -0700562 if (unlikely(!req))
563 goto out;
564 } else if (!state->free_reqs) {
565 size_t sz;
566 int ret;
567
568 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -0600569 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
570
571 /*
572 * Bulk alloc is all-or-nothing. If we fail to get a batch,
573 * retry single alloc to be on the safe side.
574 */
575 if (unlikely(ret <= 0)) {
576 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
577 if (!state->reqs[0])
578 goto out;
579 ret = 1;
580 }
Jens Axboe2579f912019-01-09 09:10:43 -0700581 state->free_reqs = ret - 1;
582 state->cur_req = 1;
583 req = state->reqs[0];
584 } else {
585 req = state->reqs[state->cur_req];
586 state->free_reqs--;
587 state->cur_req++;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700588 }
589
Jens Axboe60c112b2019-06-21 10:20:18 -0600590 req->file = NULL;
Jens Axboe2579f912019-01-09 09:10:43 -0700591 req->ctx = ctx;
592 req->flags = 0;
Jens Axboee65ef562019-03-12 10:16:44 -0600593 /* one is dropped after submission, the other at completion */
594 refcount_set(&req->refs, 2);
Jens Axboe9e645e112019-05-10 16:07:28 -0600595 req->result = 0;
Jens Axboe2579f912019-01-09 09:10:43 -0700596 return req;
597out:
Jens Axboe2b188cc2019-01-07 10:46:33 -0700598 io_ring_drop_ctx_refs(ctx, 1);
599 return NULL;
600}
601
Jens Axboedef596e2019-01-09 08:59:42 -0700602static void io_free_req_many(struct io_ring_ctx *ctx, void **reqs, int *nr)
603{
604 if (*nr) {
605 kmem_cache_free_bulk(req_cachep, *nr, reqs);
606 io_ring_drop_ctx_refs(ctx, *nr);
607 *nr = 0;
608 }
609}
610
Jens Axboe9e645e112019-05-10 16:07:28 -0600611static void __io_free_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700612{
Jens Axboe09bb8392019-03-13 12:39:28 -0600613 if (req->file && !(req->flags & REQ_F_FIXED_FILE))
614 fput(req->file);
Jens Axboee65ef562019-03-12 10:16:44 -0600615 io_ring_drop_ctx_refs(req->ctx, 1);
616 kmem_cache_free(req_cachep, req);
617}
618
Jens Axboe9e645e112019-05-10 16:07:28 -0600619static void io_req_link_next(struct io_kiocb *req)
620{
621 struct io_kiocb *nxt;
622
623 /*
624 * The list should never be empty when we are called here. But could
625 * potentially happen if the chain is messed up, check to be on the
626 * safe side.
627 */
628 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, list);
629 if (nxt) {
630 list_del(&nxt->list);
631 if (!list_empty(&req->link_list)) {
632 INIT_LIST_HEAD(&nxt->link_list);
633 list_splice(&req->link_list, &nxt->link_list);
634 nxt->flags |= REQ_F_LINK;
635 }
636
Zhengyuan Liuf7b76ac2019-07-16 23:26:14 +0800637 nxt->flags |= REQ_F_LINK_DONE;
Jens Axboe9e645e112019-05-10 16:07:28 -0600638 INIT_WORK(&nxt->work, io_sq_wq_submit_work);
Jens Axboe18d9be12019-09-10 09:13:05 -0600639 io_queue_async_work(req->ctx, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -0600640 }
641}
642
643/*
644 * Called if REQ_F_LINK is set, and we fail the head request
645 */
646static void io_fail_links(struct io_kiocb *req)
647{
648 struct io_kiocb *link;
649
650 while (!list_empty(&req->link_list)) {
651 link = list_first_entry(&req->link_list, struct io_kiocb, list);
652 list_del(&link->list);
653
654 io_cqring_add_event(req->ctx, link->user_data, -ECANCELED);
655 __io_free_req(link);
656 }
657}
658
659static void io_free_req(struct io_kiocb *req)
660{
661 /*
662 * If LINK is set, we have dependent requests in this chain. If we
663 * didn't fail this request, queue the first one up, moving any other
664 * dependencies to the next request. In case of failure, fail the rest
665 * of the chain.
666 */
667 if (req->flags & REQ_F_LINK) {
668 if (req->flags & REQ_F_FAIL_LINK)
669 io_fail_links(req);
670 else
671 io_req_link_next(req);
672 }
673
674 __io_free_req(req);
675}
676
Jens Axboee65ef562019-03-12 10:16:44 -0600677static void io_put_req(struct io_kiocb *req)
678{
679 if (refcount_dec_and_test(&req->refs))
680 io_free_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700681}
682
Hristo Venev75b28af2019-08-26 17:23:46 +0000683static unsigned io_cqring_events(struct io_rings *rings)
Jens Axboea3a0e432019-08-20 11:03:11 -0600684{
685 /* See comment at the top of this file */
686 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +0000687 return READ_ONCE(rings->cq.tail) - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -0600688}
689
Jens Axboedef596e2019-01-09 08:59:42 -0700690/*
691 * Find and free completed poll iocbs
692 */
693static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
694 struct list_head *done)
695{
696 void *reqs[IO_IOPOLL_BATCH];
697 struct io_kiocb *req;
Jens Axboe09bb8392019-03-13 12:39:28 -0600698 int to_free;
Jens Axboedef596e2019-01-09 08:59:42 -0700699
Jens Axboe09bb8392019-03-13 12:39:28 -0600700 to_free = 0;
Jens Axboedef596e2019-01-09 08:59:42 -0700701 while (!list_empty(done)) {
702 req = list_first_entry(done, struct io_kiocb, list);
703 list_del(&req->list);
704
Jens Axboe9e645e112019-05-10 16:07:28 -0600705 io_cqring_fill_event(ctx, req->user_data, req->result);
Jens Axboedef596e2019-01-09 08:59:42 -0700706 (*nr_events)++;
707
Jens Axboe09bb8392019-03-13 12:39:28 -0600708 if (refcount_dec_and_test(&req->refs)) {
709 /* If we're not using fixed files, we have to pair the
710 * completion part with the file put. Use regular
711 * completions for those, only batch free for fixed
Jens Axboe9e645e112019-05-10 16:07:28 -0600712 * file and non-linked commands.
Jens Axboe09bb8392019-03-13 12:39:28 -0600713 */
Jens Axboe9e645e112019-05-10 16:07:28 -0600714 if ((req->flags & (REQ_F_FIXED_FILE|REQ_F_LINK)) ==
715 REQ_F_FIXED_FILE) {
Jens Axboe09bb8392019-03-13 12:39:28 -0600716 reqs[to_free++] = req;
717 if (to_free == ARRAY_SIZE(reqs))
718 io_free_req_many(ctx, reqs, &to_free);
Jens Axboe6b063142019-01-10 22:13:58 -0700719 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -0600720 io_free_req(req);
Jens Axboe6b063142019-01-10 22:13:58 -0700721 }
Jens Axboe9a56a232019-01-09 09:06:50 -0700722 }
Jens Axboedef596e2019-01-09 08:59:42 -0700723 }
Jens Axboedef596e2019-01-09 08:59:42 -0700724
Jens Axboe09bb8392019-03-13 12:39:28 -0600725 io_commit_cqring(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -0700726 io_free_req_many(ctx, reqs, &to_free);
727}
728
729static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
730 long min)
731{
732 struct io_kiocb *req, *tmp;
733 LIST_HEAD(done);
734 bool spin;
735 int ret;
736
737 /*
738 * Only spin for completions if we don't have multiple devices hanging
739 * off our complete list, and we're under the requested amount.
740 */
741 spin = !ctx->poll_multi_file && *nr_events < min;
742
743 ret = 0;
744 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
745 struct kiocb *kiocb = &req->rw;
746
747 /*
748 * Move completed entries to our local list. If we find a
749 * request that requires polling, break out and complete
750 * the done list first, if we have entries there.
751 */
752 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
753 list_move_tail(&req->list, &done);
754 continue;
755 }
756 if (!list_empty(&done))
757 break;
758
759 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
760 if (ret < 0)
761 break;
762
763 if (ret && spin)
764 spin = false;
765 ret = 0;
766 }
767
768 if (!list_empty(&done))
769 io_iopoll_complete(ctx, nr_events, &done);
770
771 return ret;
772}
773
774/*
775 * Poll for a mininum of 'min' events. Note that if min == 0 we consider that a
776 * non-spinning poll check - we'll still enter the driver poll loop, but only
777 * as a non-spinning completion check.
778 */
779static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
780 long min)
781{
Jens Axboe08f54392019-08-21 22:19:11 -0600782 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -0700783 int ret;
784
785 ret = io_do_iopoll(ctx, nr_events, min);
786 if (ret < 0)
787 return ret;
788 if (!min || *nr_events >= min)
789 return 0;
790 }
791
792 return 1;
793}
794
795/*
796 * We can't just wait for polled events to come to us, we have to actively
797 * find and complete them.
798 */
799static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
800{
801 if (!(ctx->flags & IORING_SETUP_IOPOLL))
802 return;
803
804 mutex_lock(&ctx->uring_lock);
805 while (!list_empty(&ctx->poll_list)) {
806 unsigned int nr_events = 0;
807
808 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -0600809
810 /*
811 * Ensure we allow local-to-the-cpu processing to take place,
812 * in this case we need to ensure that we reap all events.
813 */
814 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -0700815 }
816 mutex_unlock(&ctx->uring_lock);
817}
818
819static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
820 long min)
821{
Jens Axboe500f9fb2019-08-19 12:15:59 -0600822 int iters, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -0700823
Jens Axboe500f9fb2019-08-19 12:15:59 -0600824 /*
825 * We disallow the app entering submit/complete with polling, but we
826 * still need to lock the ring to prevent racing with polled issue
827 * that got punted to a workqueue.
828 */
829 mutex_lock(&ctx->uring_lock);
830
831 iters = 0;
Jens Axboedef596e2019-01-09 08:59:42 -0700832 do {
833 int tmin = 0;
834
Jens Axboe500f9fb2019-08-19 12:15:59 -0600835 /*
Jens Axboea3a0e432019-08-20 11:03:11 -0600836 * Don't enter poll loop if we already have events pending.
837 * If we do, we can potentially be spinning for commands that
838 * already triggered a CQE (eg in error).
839 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000840 if (io_cqring_events(ctx->rings))
Jens Axboea3a0e432019-08-20 11:03:11 -0600841 break;
842
843 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -0600844 * If a submit got punted to a workqueue, we can have the
845 * application entering polling for a command before it gets
846 * issued. That app will hold the uring_lock for the duration
847 * of the poll right here, so we need to take a breather every
848 * now and then to ensure that the issue has a chance to add
849 * the poll to the issued list. Otherwise we can spin here
850 * forever, while the workqueue is stuck trying to acquire the
851 * very same mutex.
852 */
853 if (!(++iters & 7)) {
854 mutex_unlock(&ctx->uring_lock);
855 mutex_lock(&ctx->uring_lock);
856 }
857
Jens Axboedef596e2019-01-09 08:59:42 -0700858 if (*nr_events < min)
859 tmin = min - *nr_events;
860
861 ret = io_iopoll_getevents(ctx, nr_events, tmin);
862 if (ret <= 0)
863 break;
864 ret = 0;
865 } while (min && !*nr_events && !need_resched());
866
Jens Axboe500f9fb2019-08-19 12:15:59 -0600867 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -0700868 return ret;
869}
870
Jens Axboe2b188cc2019-01-07 10:46:33 -0700871static void kiocb_end_write(struct kiocb *kiocb)
872{
873 if (kiocb->ki_flags & IOCB_WRITE) {
874 struct inode *inode = file_inode(kiocb->ki_filp);
875
876 /*
877 * Tell lockdep we inherited freeze protection from submission
878 * thread.
879 */
880 if (S_ISREG(inode->i_mode))
881 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
882 file_end_write(kiocb->ki_filp);
883 }
884}
885
886static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
887{
888 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
889
890 kiocb_end_write(kiocb);
891
Jens Axboe9e645e112019-05-10 16:07:28 -0600892 if ((req->flags & REQ_F_LINK) && res != req->result)
893 req->flags |= REQ_F_FAIL_LINK;
Jens Axboec71ffb62019-05-13 20:58:29 -0600894 io_cqring_add_event(req->ctx, req->user_data, res);
Jens Axboee65ef562019-03-12 10:16:44 -0600895 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700896}
897
Jens Axboedef596e2019-01-09 08:59:42 -0700898static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
899{
900 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
901
902 kiocb_end_write(kiocb);
903
Jens Axboe9e645e112019-05-10 16:07:28 -0600904 if ((req->flags & REQ_F_LINK) && res != req->result)
905 req->flags |= REQ_F_FAIL_LINK;
906 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -0700907 if (res != -EAGAIN)
908 req->flags |= REQ_F_IOPOLL_COMPLETED;
909}
910
911/*
912 * After the iocb has been issued, it's safe to be found on the poll list.
913 * Adding the kiocb to the list AFTER submission ensures that we don't
914 * find it from a io_iopoll_getevents() thread before the issuer is done
915 * accessing the kiocb cookie.
916 */
917static void io_iopoll_req_issued(struct io_kiocb *req)
918{
919 struct io_ring_ctx *ctx = req->ctx;
920
921 /*
922 * Track whether we have multiple files in our lists. This will impact
923 * how we do polling eventually, not spinning if we're on potentially
924 * different devices.
925 */
926 if (list_empty(&ctx->poll_list)) {
927 ctx->poll_multi_file = false;
928 } else if (!ctx->poll_multi_file) {
929 struct io_kiocb *list_req;
930
931 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
932 list);
933 if (list_req->rw.ki_filp != req->rw.ki_filp)
934 ctx->poll_multi_file = true;
935 }
936
937 /*
938 * For fast devices, IO may have already completed. If it has, add
939 * it to the front so we find it first.
940 */
941 if (req->flags & REQ_F_IOPOLL_COMPLETED)
942 list_add(&req->list, &ctx->poll_list);
943 else
944 list_add_tail(&req->list, &ctx->poll_list);
945}
946
Jens Axboe3d6770f2019-04-13 11:50:54 -0600947static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -0700948{
Jens Axboe3d6770f2019-04-13 11:50:54 -0600949 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -0700950 int diff = state->has_refs - state->used_refs;
951
952 if (diff)
953 fput_many(state->file, diff);
954 state->file = NULL;
955 }
956}
957
958/*
959 * Get as many references to a file as we have IOs left in this submission,
960 * assuming most submissions are for one file, or at least that each file
961 * has more than one submission.
962 */
963static struct file *io_file_get(struct io_submit_state *state, int fd)
964{
965 if (!state)
966 return fget(fd);
967
968 if (state->file) {
969 if (state->fd == fd) {
970 state->used_refs++;
971 state->ios_left--;
972 return state->file;
973 }
Jens Axboe3d6770f2019-04-13 11:50:54 -0600974 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -0700975 }
976 state->file = fget_many(fd, state->ios_left);
977 if (!state->file)
978 return NULL;
979
980 state->fd = fd;
981 state->has_refs = state->ios_left;
982 state->used_refs = 1;
983 state->ios_left--;
984 return state->file;
985}
986
Jens Axboe2b188cc2019-01-07 10:46:33 -0700987/*
988 * If we tracked the file through the SCM inflight mechanism, we could support
989 * any file. For now, just ensure that anything potentially problematic is done
990 * inline.
991 */
992static bool io_file_supports_async(struct file *file)
993{
994 umode_t mode = file_inode(file)->i_mode;
995
996 if (S_ISBLK(mode) || S_ISCHR(mode))
997 return true;
998 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
999 return true;
1000
1001 return false;
1002}
1003
Jens Axboe6c271ce2019-01-10 11:22:30 -07001004static int io_prep_rw(struct io_kiocb *req, const struct sqe_submit *s,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001005 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001006{
Jens Axboe6c271ce2019-01-10 11:22:30 -07001007 const struct io_uring_sqe *sqe = s->sqe;
Jens Axboedef596e2019-01-09 08:59:42 -07001008 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001009 struct kiocb *kiocb = &req->rw;
Jens Axboe09bb8392019-03-13 12:39:28 -06001010 unsigned ioprio;
1011 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001012
Jens Axboe09bb8392019-03-13 12:39:28 -06001013 if (!req->file)
1014 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001015
Jens Axboe09bb8392019-03-13 12:39:28 -06001016 if (force_nonblock && !io_file_supports_async(req->file))
1017 force_nonblock = false;
Jens Axboe6b063142019-01-10 22:13:58 -07001018
Jens Axboe2b188cc2019-01-07 10:46:33 -07001019 kiocb->ki_pos = READ_ONCE(sqe->off);
1020 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
1021 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
1022
1023 ioprio = READ_ONCE(sqe->ioprio);
1024 if (ioprio) {
1025 ret = ioprio_check_cap(ioprio);
1026 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06001027 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001028
1029 kiocb->ki_ioprio = ioprio;
1030 } else
1031 kiocb->ki_ioprio = get_current_ioprio();
1032
1033 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
1034 if (unlikely(ret))
Jens Axboe09bb8392019-03-13 12:39:28 -06001035 return ret;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001036
1037 /* don't allow async punt if RWF_NOWAIT was requested */
1038 if (kiocb->ki_flags & IOCB_NOWAIT)
1039 req->flags |= REQ_F_NOWAIT;
1040
1041 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001042 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001043
Jens Axboedef596e2019-01-09 08:59:42 -07001044 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07001045 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
1046 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06001047 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001048
Jens Axboedef596e2019-01-09 08:59:42 -07001049 kiocb->ki_flags |= IOCB_HIPRI;
1050 kiocb->ki_complete = io_complete_rw_iopoll;
1051 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001052 if (kiocb->ki_flags & IOCB_HIPRI)
1053 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07001054 kiocb->ki_complete = io_complete_rw;
1055 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001056 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001057}
1058
1059static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
1060{
1061 switch (ret) {
1062 case -EIOCBQUEUED:
1063 break;
1064 case -ERESTARTSYS:
1065 case -ERESTARTNOINTR:
1066 case -ERESTARTNOHAND:
1067 case -ERESTART_RESTARTBLOCK:
1068 /*
1069 * We can't just restart the syscall, since previously
1070 * submitted sqes may already be in progress. Just fail this
1071 * IO with EINTR.
1072 */
1073 ret = -EINTR;
1074 /* fall through */
1075 default:
1076 kiocb->ki_complete(kiocb, ret, 0);
1077 }
1078}
1079
Jens Axboeedafcce2019-01-09 09:16:05 -07001080static int io_import_fixed(struct io_ring_ctx *ctx, int rw,
1081 const struct io_uring_sqe *sqe,
1082 struct iov_iter *iter)
1083{
1084 size_t len = READ_ONCE(sqe->len);
1085 struct io_mapped_ubuf *imu;
1086 unsigned index, buf_index;
1087 size_t offset;
1088 u64 buf_addr;
1089
1090 /* attempt to use fixed buffers without having provided iovecs */
1091 if (unlikely(!ctx->user_bufs))
1092 return -EFAULT;
1093
1094 buf_index = READ_ONCE(sqe->buf_index);
1095 if (unlikely(buf_index >= ctx->nr_user_bufs))
1096 return -EFAULT;
1097
1098 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
1099 imu = &ctx->user_bufs[index];
1100 buf_addr = READ_ONCE(sqe->addr);
1101
1102 /* overflow */
1103 if (buf_addr + len < buf_addr)
1104 return -EFAULT;
1105 /* not inside the mapped region */
1106 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
1107 return -EFAULT;
1108
1109 /*
1110 * May not be a start of buffer, set size appropriately
1111 * and advance us to the beginning.
1112 */
1113 offset = buf_addr - imu->ubuf;
1114 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06001115
1116 if (offset) {
1117 /*
1118 * Don't use iov_iter_advance() here, as it's really slow for
1119 * using the latter parts of a big fixed buffer - it iterates
1120 * over each segment manually. We can cheat a bit here, because
1121 * we know that:
1122 *
1123 * 1) it's a BVEC iter, we set it up
1124 * 2) all bvecs are PAGE_SIZE in size, except potentially the
1125 * first and last bvec
1126 *
1127 * So just find our index, and adjust the iterator afterwards.
1128 * If the offset is within the first bvec (or the whole first
1129 * bvec, just use iov_iter_advance(). This makes it easier
1130 * since we can just skip the first segment, which may not
1131 * be PAGE_SIZE aligned.
1132 */
1133 const struct bio_vec *bvec = imu->bvec;
1134
1135 if (offset <= bvec->bv_len) {
1136 iov_iter_advance(iter, offset);
1137 } else {
1138 unsigned long seg_skip;
1139
1140 /* skip first vec */
1141 offset -= bvec->bv_len;
1142 seg_skip = 1 + (offset >> PAGE_SHIFT);
1143
1144 iter->bvec = bvec + seg_skip;
1145 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02001146 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001147 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001148 }
1149 }
1150
Jens Axboeedafcce2019-01-09 09:16:05 -07001151 return 0;
1152}
1153
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001154static ssize_t io_import_iovec(struct io_ring_ctx *ctx, int rw,
1155 const struct sqe_submit *s, struct iovec **iovec,
1156 struct iov_iter *iter)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001157{
1158 const struct io_uring_sqe *sqe = s->sqe;
1159 void __user *buf = u64_to_user_ptr(READ_ONCE(sqe->addr));
1160 size_t sqe_len = READ_ONCE(sqe->len);
Jens Axboeedafcce2019-01-09 09:16:05 -07001161 u8 opcode;
1162
1163 /*
1164 * We're reading ->opcode for the second time, but the first read
1165 * doesn't care whether it's _FIXED or not, so it doesn't matter
1166 * whether ->opcode changes concurrently. The first read does care
1167 * about whether it is a READ or a WRITE, so we don't trust this read
1168 * for that purpose and instead let the caller pass in the read/write
1169 * flag.
1170 */
1171 opcode = READ_ONCE(sqe->opcode);
1172 if (opcode == IORING_OP_READ_FIXED ||
1173 opcode == IORING_OP_WRITE_FIXED) {
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001174 ssize_t ret = io_import_fixed(ctx, rw, sqe, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07001175 *iovec = NULL;
1176 return ret;
1177 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001178
1179 if (!s->has_user)
1180 return -EFAULT;
1181
1182#ifdef CONFIG_COMPAT
1183 if (ctx->compat)
1184 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
1185 iovec, iter);
1186#endif
1187
1188 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
1189}
1190
Jens Axboe6d5d5ac2019-09-11 10:16:13 -06001191static inline bool io_should_merge(struct async_list *al, struct kiocb *kiocb)
1192{
1193 if (al->file == kiocb->ki_filp) {
1194 off_t start, end;
1195
1196 /*
1197 * Allow merging if we're anywhere in the range of the same
1198 * page. Generally this happens for sub-page reads or writes,
1199 * and it's beneficial to allow the first worker to bring the
1200 * page in and the piggy backed work can then work on the
1201 * cached page.
1202 */
1203 start = al->io_start & PAGE_MASK;
1204 end = (al->io_start + al->io_len + PAGE_SIZE - 1) & PAGE_MASK;
1205 if (kiocb->ki_pos >= start && kiocb->ki_pos <= end)
1206 return true;
1207 }
1208
1209 al->file = NULL;
1210 return false;
1211}
1212
Jens Axboe31b51512019-01-18 22:56:34 -07001213/*
1214 * Make a note of the last file/offset/direction we punted to async
1215 * context. We'll use this information to see if we can piggy back a
1216 * sequential request onto the previous one, if it's still hasn't been
1217 * completed by the async worker.
1218 */
1219static void io_async_list_note(int rw, struct io_kiocb *req, size_t len)
1220{
1221 struct async_list *async_list = &req->ctx->pending_async[rw];
1222 struct kiocb *kiocb = &req->rw;
1223 struct file *filp = kiocb->ki_filp;
Jens Axboe31b51512019-01-18 22:56:34 -07001224
Jens Axboe6d5d5ac2019-09-11 10:16:13 -06001225 if (io_should_merge(async_list, kiocb)) {
Zhengyuan Liu9310a7ba2019-07-22 10:23:27 +08001226 unsigned long max_bytes;
Jens Axboe31b51512019-01-18 22:56:34 -07001227
1228 /* Use 8x RA size as a decent limiter for both reads/writes */
Zhengyuan Liu9310a7ba2019-07-22 10:23:27 +08001229 max_bytes = filp->f_ra.ra_pages << (PAGE_SHIFT + 3);
1230 if (!max_bytes)
1231 max_bytes = VM_READAHEAD_PAGES << (PAGE_SHIFT + 3);
Jens Axboe31b51512019-01-18 22:56:34 -07001232
Zhengyuan Liu9310a7ba2019-07-22 10:23:27 +08001233 /* If max len are exceeded, reset the state */
1234 if (async_list->io_len + len <= max_bytes) {
Jens Axboe31b51512019-01-18 22:56:34 -07001235 req->flags |= REQ_F_SEQ_PREV;
Zhengyuan Liu9310a7ba2019-07-22 10:23:27 +08001236 async_list->io_len += len;
Jens Axboe31b51512019-01-18 22:56:34 -07001237 } else {
Jens Axboe6d5d5ac2019-09-11 10:16:13 -06001238 async_list->file = NULL;
Jens Axboe31b51512019-01-18 22:56:34 -07001239 }
1240 }
1241
1242 /* New file? Reset state. */
1243 if (async_list->file != filp) {
Jens Axboe6d5d5ac2019-09-11 10:16:13 -06001244 async_list->io_start = kiocb->ki_pos;
1245 async_list->io_len = len;
Jens Axboe31b51512019-01-18 22:56:34 -07001246 async_list->file = filp;
1247 }
Jens Axboe31b51512019-01-18 22:56:34 -07001248}
1249
Jens Axboee0c5c572019-03-12 10:18:47 -06001250static int io_read(struct io_kiocb *req, const struct sqe_submit *s,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001251 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001252{
1253 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1254 struct kiocb *kiocb = &req->rw;
1255 struct iov_iter iter;
1256 struct file *file;
Jens Axboe31b51512019-01-18 22:56:34 -07001257 size_t iov_count;
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001258 ssize_t read_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001259
Jens Axboe8358e3a2019-04-23 08:17:58 -06001260 ret = io_prep_rw(req, s, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001261 if (ret)
1262 return ret;
1263 file = kiocb->ki_filp;
1264
Jens Axboe2b188cc2019-01-07 10:46:33 -07001265 if (unlikely(!(file->f_mode & FMODE_READ)))
Jens Axboe09bb8392019-03-13 12:39:28 -06001266 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001267 if (unlikely(!file->f_op->read_iter))
Jens Axboe09bb8392019-03-13 12:39:28 -06001268 return -EINVAL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001269
1270 ret = io_import_iovec(req->ctx, READ, s, &iovec, &iter);
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001271 if (ret < 0)
Jens Axboe09bb8392019-03-13 12:39:28 -06001272 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001273
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001274 read_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06001275 if (req->flags & REQ_F_LINK)
1276 req->result = read_size;
1277
Jens Axboe31b51512019-01-18 22:56:34 -07001278 iov_count = iov_iter_count(&iter);
1279 ret = rw_verify_area(READ, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001280 if (!ret) {
1281 ssize_t ret2;
1282
Jens Axboe2b188cc2019-01-07 10:46:33 -07001283 ret2 = call_read_iter(file, kiocb, &iter);
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001284 /*
1285 * In case of a short read, punt to async. This can happen
1286 * if we have data partially cached. Alternatively we can
1287 * return the short read, in which case the application will
1288 * need to issue another SQE and wait for it. That SQE will
1289 * need async punt anyway, so it's more efficient to do it
1290 * here.
1291 */
1292 if (force_nonblock && ret2 > 0 && ret2 < read_size)
1293 ret2 = -EAGAIN;
1294 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboe31b51512019-01-18 22:56:34 -07001295 if (!force_nonblock || ret2 != -EAGAIN) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001296 io_rw_done(kiocb, ret2);
Jens Axboe31b51512019-01-18 22:56:34 -07001297 } else {
1298 /*
1299 * If ->needs_lock is true, we're already in async
1300 * context.
1301 */
1302 if (!s->needs_lock)
1303 io_async_list_note(READ, req, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001304 ret = -EAGAIN;
Jens Axboe31b51512019-01-18 22:56:34 -07001305 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001306 }
1307 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001308 return ret;
1309}
1310
Jens Axboee0c5c572019-03-12 10:18:47 -06001311static int io_write(struct io_kiocb *req, const struct sqe_submit *s,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001312 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001313{
1314 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1315 struct kiocb *kiocb = &req->rw;
1316 struct iov_iter iter;
1317 struct file *file;
Jens Axboe31b51512019-01-18 22:56:34 -07001318 size_t iov_count;
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001319 ssize_t ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001320
Jens Axboe8358e3a2019-04-23 08:17:58 -06001321 ret = io_prep_rw(req, s, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001322 if (ret)
1323 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001324
Jens Axboe2b188cc2019-01-07 10:46:33 -07001325 file = kiocb->ki_filp;
1326 if (unlikely(!(file->f_mode & FMODE_WRITE)))
Jens Axboe09bb8392019-03-13 12:39:28 -06001327 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001328 if (unlikely(!file->f_op->write_iter))
Jens Axboe09bb8392019-03-13 12:39:28 -06001329 return -EINVAL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001330
1331 ret = io_import_iovec(req->ctx, WRITE, s, &iovec, &iter);
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001332 if (ret < 0)
Jens Axboe09bb8392019-03-13 12:39:28 -06001333 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001334
Jens Axboe9e645e112019-05-10 16:07:28 -06001335 if (req->flags & REQ_F_LINK)
1336 req->result = ret;
1337
Jens Axboe31b51512019-01-18 22:56:34 -07001338 iov_count = iov_iter_count(&iter);
1339
1340 ret = -EAGAIN;
1341 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT)) {
1342 /* If ->needs_lock is true, we're already in async context. */
1343 if (!s->needs_lock)
1344 io_async_list_note(WRITE, req, iov_count);
1345 goto out_free;
1346 }
1347
1348 ret = rw_verify_area(WRITE, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001349 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01001350 ssize_t ret2;
1351
Jens Axboe2b188cc2019-01-07 10:46:33 -07001352 /*
1353 * Open-code file_start_write here to grab freeze protection,
1354 * which will be released by another thread in
1355 * io_complete_rw(). Fool lockdep by telling it the lock got
1356 * released so that it doesn't complain about the held lock when
1357 * we return to userspace.
1358 */
1359 if (S_ISREG(file_inode(file)->i_mode)) {
1360 __sb_start_write(file_inode(file)->i_sb,
1361 SB_FREEZE_WRITE, true);
1362 __sb_writers_release(file_inode(file)->i_sb,
1363 SB_FREEZE_WRITE);
1364 }
1365 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01001366
1367 ret2 = call_write_iter(file, kiocb, &iter);
1368 if (!force_nonblock || ret2 != -EAGAIN) {
1369 io_rw_done(kiocb, ret2);
1370 } else {
1371 /*
1372 * If ->needs_lock is true, we're already in async
1373 * context.
1374 */
1375 if (!s->needs_lock)
1376 io_async_list_note(WRITE, req, iov_count);
1377 ret = -EAGAIN;
1378 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001379 }
Jens Axboe31b51512019-01-18 22:56:34 -07001380out_free:
Jens Axboe2b188cc2019-01-07 10:46:33 -07001381 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001382 return ret;
1383}
1384
1385/*
1386 * IORING_OP_NOP just posts a completion event, nothing else.
1387 */
1388static int io_nop(struct io_kiocb *req, u64 user_data)
1389{
1390 struct io_ring_ctx *ctx = req->ctx;
1391 long err = 0;
1392
Jens Axboedef596e2019-01-09 08:59:42 -07001393 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1394 return -EINVAL;
1395
Jens Axboec71ffb62019-05-13 20:58:29 -06001396 io_cqring_add_event(ctx, user_data, err);
Jens Axboee65ef562019-03-12 10:16:44 -06001397 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001398 return 0;
1399}
1400
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001401static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1402{
Jens Axboe6b063142019-01-10 22:13:58 -07001403 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001404
Jens Axboe09bb8392019-03-13 12:39:28 -06001405 if (!req->file)
1406 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001407
Jens Axboe6b063142019-01-10 22:13:58 -07001408 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07001409 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07001410 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001411 return -EINVAL;
1412
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001413 return 0;
1414}
1415
1416static int io_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1417 bool force_nonblock)
1418{
1419 loff_t sqe_off = READ_ONCE(sqe->off);
1420 loff_t sqe_len = READ_ONCE(sqe->len);
1421 loff_t end = sqe_off + sqe_len;
1422 unsigned fsync_flags;
1423 int ret;
1424
1425 fsync_flags = READ_ONCE(sqe->fsync_flags);
1426 if (unlikely(fsync_flags & ~IORING_FSYNC_DATASYNC))
1427 return -EINVAL;
1428
1429 ret = io_prep_fsync(req, sqe);
1430 if (ret)
1431 return ret;
1432
1433 /* fsync always requires a blocking context */
1434 if (force_nonblock)
1435 return -EAGAIN;
1436
1437 ret = vfs_fsync_range(req->rw.ki_filp, sqe_off,
1438 end > 0 ? end : LLONG_MAX,
1439 fsync_flags & IORING_FSYNC_DATASYNC);
1440
Jens Axboe9e645e112019-05-10 16:07:28 -06001441 if (ret < 0 && (req->flags & REQ_F_LINK))
1442 req->flags |= REQ_F_FAIL_LINK;
Jens Axboec71ffb62019-05-13 20:58:29 -06001443 io_cqring_add_event(req->ctx, sqe->user_data, ret);
Jens Axboee65ef562019-03-12 10:16:44 -06001444 io_put_req(req);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001445 return 0;
1446}
1447
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001448static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1449{
1450 struct io_ring_ctx *ctx = req->ctx;
1451 int ret = 0;
1452
1453 if (!req->file)
1454 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001455
1456 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1457 return -EINVAL;
1458 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
1459 return -EINVAL;
1460
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001461 return ret;
1462}
1463
1464static int io_sync_file_range(struct io_kiocb *req,
1465 const struct io_uring_sqe *sqe,
1466 bool force_nonblock)
1467{
1468 loff_t sqe_off;
1469 loff_t sqe_len;
1470 unsigned flags;
1471 int ret;
1472
1473 ret = io_prep_sfr(req, sqe);
1474 if (ret)
1475 return ret;
1476
1477 /* sync_file_range always requires a blocking context */
1478 if (force_nonblock)
1479 return -EAGAIN;
1480
1481 sqe_off = READ_ONCE(sqe->off);
1482 sqe_len = READ_ONCE(sqe->len);
1483 flags = READ_ONCE(sqe->sync_range_flags);
1484
1485 ret = sync_file_range(req->rw.ki_filp, sqe_off, sqe_len, flags);
1486
Jens Axboe9e645e112019-05-10 16:07:28 -06001487 if (ret < 0 && (req->flags & REQ_F_LINK))
1488 req->flags |= REQ_F_FAIL_LINK;
Jens Axboec71ffb62019-05-13 20:58:29 -06001489 io_cqring_add_event(req->ctx, sqe->user_data, ret);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001490 io_put_req(req);
1491 return 0;
1492}
1493
Jens Axboe0fa03c62019-04-19 13:34:07 -06001494#if defined(CONFIG_NET)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001495static int io_send_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1496 bool force_nonblock,
1497 long (*fn)(struct socket *, struct user_msghdr __user *,
1498 unsigned int))
1499{
Jens Axboe0fa03c62019-04-19 13:34:07 -06001500 struct socket *sock;
1501 int ret;
1502
1503 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1504 return -EINVAL;
1505
1506 sock = sock_from_file(req->file, &ret);
1507 if (sock) {
1508 struct user_msghdr __user *msg;
1509 unsigned flags;
1510
1511 flags = READ_ONCE(sqe->msg_flags);
1512 if (flags & MSG_DONTWAIT)
1513 req->flags |= REQ_F_NOWAIT;
1514 else if (force_nonblock)
1515 flags |= MSG_DONTWAIT;
1516
1517 msg = (struct user_msghdr __user *) (unsigned long)
1518 READ_ONCE(sqe->addr);
1519
Jens Axboeaa1fa282019-04-19 13:38:09 -06001520 ret = fn(sock, msg, flags);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001521 if (force_nonblock && ret == -EAGAIN)
1522 return ret;
1523 }
1524
1525 io_cqring_add_event(req->ctx, sqe->user_data, ret);
1526 io_put_req(req);
1527 return 0;
Jens Axboeaa1fa282019-04-19 13:38:09 -06001528}
1529#endif
1530
1531static int io_sendmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1532 bool force_nonblock)
1533{
1534#if defined(CONFIG_NET)
1535 return io_send_recvmsg(req, sqe, force_nonblock, __sys_sendmsg_sock);
1536#else
1537 return -EOPNOTSUPP;
1538#endif
1539}
1540
1541static int io_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1542 bool force_nonblock)
1543{
1544#if defined(CONFIG_NET)
1545 return io_send_recvmsg(req, sqe, force_nonblock, __sys_recvmsg_sock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001546#else
1547 return -EOPNOTSUPP;
1548#endif
1549}
1550
Jens Axboe221c5eb2019-01-17 09:41:58 -07001551static void io_poll_remove_one(struct io_kiocb *req)
1552{
1553 struct io_poll_iocb *poll = &req->poll;
1554
1555 spin_lock(&poll->head->lock);
1556 WRITE_ONCE(poll->canceled, true);
1557 if (!list_empty(&poll->wait.entry)) {
1558 list_del_init(&poll->wait.entry);
Jens Axboe18d9be12019-09-10 09:13:05 -06001559 io_queue_async_work(req->ctx, req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001560 }
1561 spin_unlock(&poll->head->lock);
1562
1563 list_del_init(&req->list);
1564}
1565
1566static void io_poll_remove_all(struct io_ring_ctx *ctx)
1567{
1568 struct io_kiocb *req;
1569
1570 spin_lock_irq(&ctx->completion_lock);
1571 while (!list_empty(&ctx->cancel_list)) {
1572 req = list_first_entry(&ctx->cancel_list, struct io_kiocb,list);
1573 io_poll_remove_one(req);
1574 }
1575 spin_unlock_irq(&ctx->completion_lock);
1576}
1577
1578/*
1579 * Find a running poll command that matches one specified in sqe->addr,
1580 * and remove it if found.
1581 */
1582static int io_poll_remove(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1583{
1584 struct io_ring_ctx *ctx = req->ctx;
1585 struct io_kiocb *poll_req, *next;
1586 int ret = -ENOENT;
1587
1588 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1589 return -EINVAL;
1590 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
1591 sqe->poll_events)
1592 return -EINVAL;
1593
1594 spin_lock_irq(&ctx->completion_lock);
1595 list_for_each_entry_safe(poll_req, next, &ctx->cancel_list, list) {
1596 if (READ_ONCE(sqe->addr) == poll_req->user_data) {
1597 io_poll_remove_one(poll_req);
1598 ret = 0;
1599 break;
1600 }
1601 }
1602 spin_unlock_irq(&ctx->completion_lock);
1603
Jens Axboec71ffb62019-05-13 20:58:29 -06001604 io_cqring_add_event(req->ctx, sqe->user_data, ret);
Jens Axboee65ef562019-03-12 10:16:44 -06001605 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001606 return 0;
1607}
1608
Jens Axboe8c838782019-03-12 15:48:16 -06001609static void io_poll_complete(struct io_ring_ctx *ctx, struct io_kiocb *req,
1610 __poll_t mask)
Jens Axboe221c5eb2019-01-17 09:41:58 -07001611{
Jens Axboe8c838782019-03-12 15:48:16 -06001612 req->poll.done = true;
Jens Axboec71ffb62019-05-13 20:58:29 -06001613 io_cqring_fill_event(ctx, req->user_data, mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06001614 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001615}
1616
1617static void io_poll_complete_work(struct work_struct *work)
1618{
1619 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
1620 struct io_poll_iocb *poll = &req->poll;
1621 struct poll_table_struct pt = { ._key = poll->events };
1622 struct io_ring_ctx *ctx = req->ctx;
1623 __poll_t mask = 0;
1624
1625 if (!READ_ONCE(poll->canceled))
1626 mask = vfs_poll(poll->file, &pt) & poll->events;
1627
1628 /*
1629 * Note that ->ki_cancel callers also delete iocb from active_reqs after
1630 * calling ->ki_cancel. We need the ctx_lock roundtrip here to
1631 * synchronize with them. In the cancellation case the list_del_init
1632 * itself is not actually needed, but harmless so we keep it in to
1633 * avoid further branches in the fast path.
1634 */
1635 spin_lock_irq(&ctx->completion_lock);
1636 if (!mask && !READ_ONCE(poll->canceled)) {
1637 add_wait_queue(poll->head, &poll->wait);
1638 spin_unlock_irq(&ctx->completion_lock);
1639 return;
1640 }
1641 list_del_init(&req->list);
Jens Axboe8c838782019-03-12 15:48:16 -06001642 io_poll_complete(ctx, req, mask);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001643 spin_unlock_irq(&ctx->completion_lock);
1644
Jens Axboe8c838782019-03-12 15:48:16 -06001645 io_cqring_ev_posted(ctx);
1646 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001647}
1648
1649static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
1650 void *key)
1651{
1652 struct io_poll_iocb *poll = container_of(wait, struct io_poll_iocb,
1653 wait);
1654 struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
1655 struct io_ring_ctx *ctx = req->ctx;
1656 __poll_t mask = key_to_poll(key);
Jens Axboe8c838782019-03-12 15:48:16 -06001657 unsigned long flags;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001658
1659 /* for instances that support it check for an event match first: */
Jens Axboe8c838782019-03-12 15:48:16 -06001660 if (mask && !(mask & poll->events))
1661 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001662
1663 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06001664
1665 if (mask && spin_trylock_irqsave(&ctx->completion_lock, flags)) {
1666 list_del(&req->list);
1667 io_poll_complete(ctx, req, mask);
1668 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1669
1670 io_cqring_ev_posted(ctx);
1671 io_put_req(req);
1672 } else {
Jens Axboe18d9be12019-09-10 09:13:05 -06001673 io_queue_async_work(ctx, req);
Jens Axboe8c838782019-03-12 15:48:16 -06001674 }
1675
Jens Axboe221c5eb2019-01-17 09:41:58 -07001676 return 1;
1677}
1678
1679struct io_poll_table {
1680 struct poll_table_struct pt;
1681 struct io_kiocb *req;
1682 int error;
1683};
1684
1685static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
1686 struct poll_table_struct *p)
1687{
1688 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
1689
1690 if (unlikely(pt->req->poll.head)) {
1691 pt->error = -EINVAL;
1692 return;
1693 }
1694
1695 pt->error = 0;
1696 pt->req->poll.head = head;
1697 add_wait_queue(head, &pt->req->poll.wait);
1698}
1699
1700static int io_poll_add(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1701{
1702 struct io_poll_iocb *poll = &req->poll;
1703 struct io_ring_ctx *ctx = req->ctx;
1704 struct io_poll_table ipt;
Jens Axboe8c838782019-03-12 15:48:16 -06001705 bool cancel = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001706 __poll_t mask;
1707 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001708
1709 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1710 return -EINVAL;
1711 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
1712 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06001713 if (!poll->file)
1714 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001715
Jens Axboe6cc47d12019-09-18 11:18:23 -06001716 req->submit.sqe = NULL;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001717 INIT_WORK(&req->work, io_poll_complete_work);
1718 events = READ_ONCE(sqe->poll_events);
1719 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
1720
Jens Axboe221c5eb2019-01-17 09:41:58 -07001721 poll->head = NULL;
Jens Axboe8c838782019-03-12 15:48:16 -06001722 poll->done = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001723 poll->canceled = false;
1724
1725 ipt.pt._qproc = io_poll_queue_proc;
1726 ipt.pt._key = poll->events;
1727 ipt.req = req;
1728 ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
1729
1730 /* initialized the list so that we can do list_empty checks */
1731 INIT_LIST_HEAD(&poll->wait.entry);
1732 init_waitqueue_func_entry(&poll->wait, io_poll_wake);
1733
Jens Axboe36703242019-07-25 10:20:18 -06001734 INIT_LIST_HEAD(&req->list);
1735
Jens Axboe221c5eb2019-01-17 09:41:58 -07001736 mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001737
1738 spin_lock_irq(&ctx->completion_lock);
Jens Axboe8c838782019-03-12 15:48:16 -06001739 if (likely(poll->head)) {
1740 spin_lock(&poll->head->lock);
1741 if (unlikely(list_empty(&poll->wait.entry))) {
1742 if (ipt.error)
1743 cancel = true;
1744 ipt.error = 0;
1745 mask = 0;
1746 }
1747 if (mask || ipt.error)
1748 list_del_init(&poll->wait.entry);
1749 else if (cancel)
1750 WRITE_ONCE(poll->canceled, true);
1751 else if (!poll->done) /* actually waiting for an event */
1752 list_add_tail(&req->list, &ctx->cancel_list);
1753 spin_unlock(&poll->head->lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001754 }
Jens Axboe8c838782019-03-12 15:48:16 -06001755 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06001756 ipt.error = 0;
1757 io_poll_complete(ctx, req, mask);
1758 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07001759 spin_unlock_irq(&ctx->completion_lock);
1760
Jens Axboe8c838782019-03-12 15:48:16 -06001761 if (mask) {
1762 io_cqring_ev_posted(ctx);
Jens Axboee65ef562019-03-12 10:16:44 -06001763 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001764 }
Jens Axboe8c838782019-03-12 15:48:16 -06001765 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001766}
1767
Jens Axboede0617e2019-04-06 21:51:27 -06001768static int io_req_defer(struct io_ring_ctx *ctx, struct io_kiocb *req,
1769 const struct io_uring_sqe *sqe)
1770{
1771 struct io_uring_sqe *sqe_copy;
1772
1773 if (!io_sequence_defer(ctx, req) && list_empty(&ctx->defer_list))
1774 return 0;
1775
1776 sqe_copy = kmalloc(sizeof(*sqe_copy), GFP_KERNEL);
1777 if (!sqe_copy)
1778 return -EAGAIN;
1779
1780 spin_lock_irq(&ctx->completion_lock);
1781 if (!io_sequence_defer(ctx, req) && list_empty(&ctx->defer_list)) {
1782 spin_unlock_irq(&ctx->completion_lock);
1783 kfree(sqe_copy);
1784 return 0;
1785 }
1786
1787 memcpy(sqe_copy, sqe, sizeof(*sqe_copy));
1788 req->submit.sqe = sqe_copy;
1789
1790 INIT_WORK(&req->work, io_sq_wq_submit_work);
1791 list_add_tail(&req->list, &ctx->defer_list);
1792 spin_unlock_irq(&ctx->completion_lock);
1793 return -EIOCBQUEUED;
1794}
1795
Jens Axboe2b188cc2019-01-07 10:46:33 -07001796static int __io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001797 const struct sqe_submit *s, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001798{
Jens Axboee0c5c572019-03-12 10:18:47 -06001799 int ret, opcode;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001800
Jens Axboe9e645e112019-05-10 16:07:28 -06001801 req->user_data = READ_ONCE(s->sqe->user_data);
1802
Jens Axboe2b188cc2019-01-07 10:46:33 -07001803 if (unlikely(s->index >= ctx->sq_entries))
1804 return -EINVAL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001805
1806 opcode = READ_ONCE(s->sqe->opcode);
1807 switch (opcode) {
1808 case IORING_OP_NOP:
1809 ret = io_nop(req, req->user_data);
1810 break;
1811 case IORING_OP_READV:
Jens Axboeedafcce2019-01-09 09:16:05 -07001812 if (unlikely(s->sqe->buf_index))
1813 return -EINVAL;
Jens Axboe8358e3a2019-04-23 08:17:58 -06001814 ret = io_read(req, s, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001815 break;
1816 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07001817 if (unlikely(s->sqe->buf_index))
1818 return -EINVAL;
Jens Axboe8358e3a2019-04-23 08:17:58 -06001819 ret = io_write(req, s, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07001820 break;
1821 case IORING_OP_READ_FIXED:
Jens Axboe8358e3a2019-04-23 08:17:58 -06001822 ret = io_read(req, s, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07001823 break;
1824 case IORING_OP_WRITE_FIXED:
Jens Axboe8358e3a2019-04-23 08:17:58 -06001825 ret = io_write(req, s, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001826 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001827 case IORING_OP_FSYNC:
1828 ret = io_fsync(req, s->sqe, force_nonblock);
1829 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001830 case IORING_OP_POLL_ADD:
1831 ret = io_poll_add(req, s->sqe);
1832 break;
1833 case IORING_OP_POLL_REMOVE:
1834 ret = io_poll_remove(req, s->sqe);
1835 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001836 case IORING_OP_SYNC_FILE_RANGE:
1837 ret = io_sync_file_range(req, s->sqe, force_nonblock);
1838 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06001839 case IORING_OP_SENDMSG:
1840 ret = io_sendmsg(req, s->sqe, force_nonblock);
1841 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06001842 case IORING_OP_RECVMSG:
1843 ret = io_recvmsg(req, s->sqe, force_nonblock);
1844 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001845 default:
1846 ret = -EINVAL;
1847 break;
1848 }
1849
Jens Axboedef596e2019-01-09 08:59:42 -07001850 if (ret)
1851 return ret;
1852
1853 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe9e645e112019-05-10 16:07:28 -06001854 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07001855 return -EAGAIN;
1856
1857 /* workqueue context doesn't hold uring_lock, grab it now */
1858 if (s->needs_lock)
1859 mutex_lock(&ctx->uring_lock);
1860 io_iopoll_req_issued(req);
1861 if (s->needs_lock)
1862 mutex_unlock(&ctx->uring_lock);
1863 }
1864
1865 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001866}
1867
Jens Axboe31b51512019-01-18 22:56:34 -07001868static struct async_list *io_async_list_from_sqe(struct io_ring_ctx *ctx,
1869 const struct io_uring_sqe *sqe)
1870{
1871 switch (sqe->opcode) {
1872 case IORING_OP_READV:
1873 case IORING_OP_READ_FIXED:
1874 return &ctx->pending_async[READ];
1875 case IORING_OP_WRITEV:
1876 case IORING_OP_WRITE_FIXED:
1877 return &ctx->pending_async[WRITE];
1878 default:
1879 return NULL;
1880 }
1881}
1882
Jens Axboeedafcce2019-01-09 09:16:05 -07001883static inline bool io_sqe_needs_user(const struct io_uring_sqe *sqe)
1884{
1885 u8 opcode = READ_ONCE(sqe->opcode);
1886
1887 return !(opcode == IORING_OP_READ_FIXED ||
1888 opcode == IORING_OP_WRITE_FIXED);
1889}
1890
Jens Axboe2b188cc2019-01-07 10:46:33 -07001891static void io_sq_wq_submit_work(struct work_struct *work)
1892{
1893 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001894 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe31b51512019-01-18 22:56:34 -07001895 struct mm_struct *cur_mm = NULL;
1896 struct async_list *async_list;
1897 LIST_HEAD(req_list);
Jens Axboeedafcce2019-01-09 09:16:05 -07001898 mm_segment_t old_fs;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001899 int ret;
1900
Jens Axboe31b51512019-01-18 22:56:34 -07001901 async_list = io_async_list_from_sqe(ctx, req->submit.sqe);
1902restart:
1903 do {
1904 struct sqe_submit *s = &req->submit;
1905 const struct io_uring_sqe *sqe = s->sqe;
Jackie Liud0ee8792019-07-31 14:39:33 +08001906 unsigned int flags = req->flags;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001907
Stefan Bühler8449eed2019-04-27 20:34:19 +02001908 /* Ensure we clear previously set non-block flag */
Jens Axboe31b51512019-01-18 22:56:34 -07001909 req->rw.ki_flags &= ~IOCB_NOWAIT;
1910
1911 ret = 0;
1912 if (io_sqe_needs_user(sqe) && !cur_mm) {
1913 if (!mmget_not_zero(ctx->sqo_mm)) {
1914 ret = -EFAULT;
1915 } else {
1916 cur_mm = ctx->sqo_mm;
1917 use_mm(cur_mm);
1918 old_fs = get_fs();
1919 set_fs(USER_DS);
1920 }
1921 }
1922
1923 if (!ret) {
1924 s->has_user = cur_mm != NULL;
1925 s->needs_lock = true;
1926 do {
Jens Axboe8358e3a2019-04-23 08:17:58 -06001927 ret = __io_submit_sqe(ctx, req, s, false);
Jens Axboe31b51512019-01-18 22:56:34 -07001928 /*
1929 * We can get EAGAIN for polled IO even though
1930 * we're forcing a sync submission from here,
1931 * since we can't wait for request slots on the
1932 * block side.
1933 */
1934 if (ret != -EAGAIN)
1935 break;
1936 cond_resched();
1937 } while (1);
1938 }
Jens Axboe817869d2019-04-30 14:44:05 -06001939
1940 /* drop submission reference */
1941 io_put_req(req);
1942
Jens Axboe31b51512019-01-18 22:56:34 -07001943 if (ret) {
Jens Axboec71ffb62019-05-13 20:58:29 -06001944 io_cqring_add_event(ctx, sqe->user_data, ret);
Jens Axboee65ef562019-03-12 10:16:44 -06001945 io_put_req(req);
Jens Axboe31b51512019-01-18 22:56:34 -07001946 }
1947
1948 /* async context always use a copy of the sqe */
1949 kfree(sqe);
1950
Zhengyuan Liuf7b76ac2019-07-16 23:26:14 +08001951 /* req from defer and link list needn't decrease async cnt */
Jackie Liud0ee8792019-07-31 14:39:33 +08001952 if (flags & (REQ_F_IO_DRAINED | REQ_F_LINK_DONE))
Zhengyuan Liuf7b76ac2019-07-16 23:26:14 +08001953 goto out;
1954
Jens Axboe31b51512019-01-18 22:56:34 -07001955 if (!async_list)
1956 break;
1957 if (!list_empty(&req_list)) {
1958 req = list_first_entry(&req_list, struct io_kiocb,
1959 list);
1960 list_del(&req->list);
1961 continue;
1962 }
1963 if (list_empty(&async_list->list))
1964 break;
1965
1966 req = NULL;
1967 spin_lock(&async_list->lock);
1968 if (list_empty(&async_list->list)) {
1969 spin_unlock(&async_list->lock);
1970 break;
1971 }
1972 list_splice_init(&async_list->list, &req_list);
1973 spin_unlock(&async_list->lock);
1974
1975 req = list_first_entry(&req_list, struct io_kiocb, list);
1976 list_del(&req->list);
1977 } while (req);
Jens Axboeedafcce2019-01-09 09:16:05 -07001978
1979 /*
Jens Axboe31b51512019-01-18 22:56:34 -07001980 * Rare case of racing with a submitter. If we find the count has
1981 * dropped to zero AND we have pending work items, then restart
1982 * the processing. This is a tiny race window.
Jens Axboeedafcce2019-01-09 09:16:05 -07001983 */
Jens Axboe31b51512019-01-18 22:56:34 -07001984 if (async_list) {
1985 ret = atomic_dec_return(&async_list->cnt);
1986 while (!ret && !list_empty(&async_list->list)) {
1987 spin_lock(&async_list->lock);
1988 atomic_inc(&async_list->cnt);
1989 list_splice_init(&async_list->list, &req_list);
1990 spin_unlock(&async_list->lock);
1991
1992 if (!list_empty(&req_list)) {
1993 req = list_first_entry(&req_list,
1994 struct io_kiocb, list);
1995 list_del(&req->list);
1996 goto restart;
1997 }
1998 ret = atomic_dec_return(&async_list->cnt);
Jens Axboeedafcce2019-01-09 09:16:05 -07001999 }
Jens Axboeedafcce2019-01-09 09:16:05 -07002000 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002001
Zhengyuan Liuf7b76ac2019-07-16 23:26:14 +08002002out:
Jens Axboe31b51512019-01-18 22:56:34 -07002003 if (cur_mm) {
Jens Axboeedafcce2019-01-09 09:16:05 -07002004 set_fs(old_fs);
Jens Axboe31b51512019-01-18 22:56:34 -07002005 unuse_mm(cur_mm);
2006 mmput(cur_mm);
Jens Axboeedafcce2019-01-09 09:16:05 -07002007 }
Jens Axboe31b51512019-01-18 22:56:34 -07002008}
Jens Axboe2b188cc2019-01-07 10:46:33 -07002009
Jens Axboe31b51512019-01-18 22:56:34 -07002010/*
2011 * See if we can piggy back onto previously submitted work, that is still
2012 * running. We currently only allow this if the new request is sequential
2013 * to the previous one we punted.
2014 */
2015static bool io_add_to_prev_work(struct async_list *list, struct io_kiocb *req)
2016{
Jens Axboe6d5d5ac2019-09-11 10:16:13 -06002017 bool ret;
Jens Axboe31b51512019-01-18 22:56:34 -07002018
2019 if (!list)
2020 return false;
2021 if (!(req->flags & REQ_F_SEQ_PREV))
2022 return false;
2023 if (!atomic_read(&list->cnt))
2024 return false;
2025
2026 ret = true;
2027 spin_lock(&list->lock);
2028 list_add_tail(&req->list, &list->list);
Zhengyuan Liuc0e48f92019-07-18 20:44:00 +08002029 /*
2030 * Ensure we see a simultaneous modification from io_sq_wq_submit_work()
2031 */
2032 smp_mb();
Jens Axboe31b51512019-01-18 22:56:34 -07002033 if (!atomic_read(&list->cnt)) {
2034 list_del_init(&req->list);
2035 ret = false;
2036 }
2037 spin_unlock(&list->lock);
2038 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002039}
2040
Jens Axboe09bb8392019-03-13 12:39:28 -06002041static bool io_op_needs_file(const struct io_uring_sqe *sqe)
2042{
2043 int op = READ_ONCE(sqe->opcode);
2044
2045 switch (op) {
2046 case IORING_OP_NOP:
2047 case IORING_OP_POLL_REMOVE:
2048 return false;
2049 default:
2050 return true;
2051 }
2052}
2053
2054static int io_req_set_file(struct io_ring_ctx *ctx, const struct sqe_submit *s,
2055 struct io_submit_state *state, struct io_kiocb *req)
2056{
2057 unsigned flags;
2058 int fd;
2059
2060 flags = READ_ONCE(s->sqe->flags);
2061 fd = READ_ONCE(s->sqe->fd);
2062
Jackie Liu4fe2c962019-09-09 20:50:40 +08002063 if (flags & IOSQE_IO_DRAIN)
Jens Axboede0617e2019-04-06 21:51:27 -06002064 req->flags |= REQ_F_IO_DRAIN;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002065 /*
2066 * All io need record the previous position, if LINK vs DARIN,
2067 * it can be used to mark the position of the first IO in the
2068 * link list.
2069 */
2070 req->sequence = s->sequence;
Jens Axboede0617e2019-04-06 21:51:27 -06002071
Jens Axboe60c112b2019-06-21 10:20:18 -06002072 if (!io_op_needs_file(s->sqe))
Jens Axboe09bb8392019-03-13 12:39:28 -06002073 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06002074
2075 if (flags & IOSQE_FIXED_FILE) {
2076 if (unlikely(!ctx->user_files ||
2077 (unsigned) fd >= ctx->nr_user_files))
2078 return -EBADF;
2079 req->file = ctx->user_files[fd];
2080 req->flags |= REQ_F_FIXED_FILE;
2081 } else {
2082 if (s->needs_fixed_file)
2083 return -EBADF;
2084 req->file = io_file_get(state, fd);
2085 if (unlikely(!req->file))
2086 return -EBADF;
2087 }
2088
2089 return 0;
2090}
2091
Jackie Liu4fe2c962019-09-09 20:50:40 +08002092static int __io_queue_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
Jens Axboec57666682019-09-09 16:19:45 -06002093 struct sqe_submit *s, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002094{
Jens Axboee0c5c572019-03-12 10:18:47 -06002095 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002096
Jens Axboec57666682019-09-09 16:19:45 -06002097 ret = __io_submit_sqe(ctx, req, s, force_nonblock);
Stefan Bühler8449eed2019-04-27 20:34:19 +02002098 if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07002099 struct io_uring_sqe *sqe_copy;
2100
Jackie Liu954dab12019-09-18 10:37:52 +08002101 sqe_copy = kmemdup(s->sqe, sizeof(*sqe_copy), GFP_KERNEL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002102 if (sqe_copy) {
Jens Axboe31b51512019-01-18 22:56:34 -07002103 struct async_list *list;
2104
Jens Axboe2b188cc2019-01-07 10:46:33 -07002105 s->sqe = sqe_copy;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002106 memcpy(&req->submit, s, sizeof(*s));
Jens Axboe31b51512019-01-18 22:56:34 -07002107 list = io_async_list_from_sqe(ctx, s->sqe);
2108 if (!io_add_to_prev_work(list, req)) {
2109 if (list)
2110 atomic_inc(&list->cnt);
2111 INIT_WORK(&req->work, io_sq_wq_submit_work);
Jens Axboe18d9be12019-09-10 09:13:05 -06002112 io_queue_async_work(ctx, req);
Jens Axboe31b51512019-01-18 22:56:34 -07002113 }
Jens Axboee65ef562019-03-12 10:16:44 -06002114
2115 /*
2116 * Queued up for async execution, worker will release
Jens Axboe9e645e112019-05-10 16:07:28 -06002117 * submit reference when the iocb is actually submitted.
Jens Axboee65ef562019-03-12 10:16:44 -06002118 */
2119 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002120 }
2121 }
Jens Axboee65ef562019-03-12 10:16:44 -06002122
2123 /* drop submission reference */
2124 io_put_req(req);
2125
2126 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06002127 if (ret) {
2128 io_cqring_add_event(ctx, req->user_data, ret);
2129 if (req->flags & REQ_F_LINK)
2130 req->flags |= REQ_F_FAIL_LINK;
Jens Axboee65ef562019-03-12 10:16:44 -06002131 io_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002132 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002133
2134 return ret;
2135}
2136
Jackie Liu4fe2c962019-09-09 20:50:40 +08002137static int io_queue_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
Jens Axboec57666682019-09-09 16:19:45 -06002138 struct sqe_submit *s, bool force_nonblock)
Jackie Liu4fe2c962019-09-09 20:50:40 +08002139{
2140 int ret;
2141
2142 ret = io_req_defer(ctx, req, s->sqe);
2143 if (ret) {
2144 if (ret != -EIOCBQUEUED) {
2145 io_free_req(req);
2146 io_cqring_add_event(ctx, s->sqe->user_data, ret);
2147 }
2148 return 0;
2149 }
2150
Jens Axboec57666682019-09-09 16:19:45 -06002151 return __io_queue_sqe(ctx, req, s, force_nonblock);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002152}
2153
2154static int io_queue_link_head(struct io_ring_ctx *ctx, struct io_kiocb *req,
Jens Axboec57666682019-09-09 16:19:45 -06002155 struct sqe_submit *s, struct io_kiocb *shadow,
2156 bool force_nonblock)
Jackie Liu4fe2c962019-09-09 20:50:40 +08002157{
2158 int ret;
2159 int need_submit = false;
2160
2161 if (!shadow)
Jens Axboec57666682019-09-09 16:19:45 -06002162 return io_queue_sqe(ctx, req, s, force_nonblock);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002163
2164 /*
2165 * Mark the first IO in link list as DRAIN, let all the following
2166 * IOs enter the defer list. all IO needs to be completed before link
2167 * list.
2168 */
2169 req->flags |= REQ_F_IO_DRAIN;
2170 ret = io_req_defer(ctx, req, s->sqe);
2171 if (ret) {
2172 if (ret != -EIOCBQUEUED) {
2173 io_free_req(req);
2174 io_cqring_add_event(ctx, s->sqe->user_data, ret);
2175 return 0;
2176 }
2177 } else {
2178 /*
2179 * If ret == 0 means that all IOs in front of link io are
2180 * running done. let's queue link head.
2181 */
2182 need_submit = true;
2183 }
2184
2185 /* Insert shadow req to defer_list, blocking next IOs */
2186 spin_lock_irq(&ctx->completion_lock);
2187 list_add_tail(&shadow->list, &ctx->defer_list);
2188 spin_unlock_irq(&ctx->completion_lock);
2189
2190 if (need_submit)
Jens Axboec57666682019-09-09 16:19:45 -06002191 return __io_queue_sqe(ctx, req, s, force_nonblock);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002192
2193 return 0;
2194}
2195
Jens Axboe9e645e112019-05-10 16:07:28 -06002196#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK)
2197
2198static void io_submit_sqe(struct io_ring_ctx *ctx, struct sqe_submit *s,
Jens Axboec57666682019-09-09 16:19:45 -06002199 struct io_submit_state *state, struct io_kiocb **link,
2200 bool force_nonblock)
Jens Axboe9e645e112019-05-10 16:07:28 -06002201{
2202 struct io_uring_sqe *sqe_copy;
2203 struct io_kiocb *req;
2204 int ret;
2205
2206 /* enforce forwards compatibility on users */
2207 if (unlikely(s->sqe->flags & ~SQE_VALID_FLAGS)) {
2208 ret = -EINVAL;
2209 goto err;
2210 }
2211
2212 req = io_get_req(ctx, state);
2213 if (unlikely(!req)) {
2214 ret = -EAGAIN;
2215 goto err;
2216 }
2217
2218 ret = io_req_set_file(ctx, s, state, req);
2219 if (unlikely(ret)) {
2220err_req:
2221 io_free_req(req);
2222err:
2223 io_cqring_add_event(ctx, s->sqe->user_data, ret);
2224 return;
2225 }
2226
Jens Axboe9e645e112019-05-10 16:07:28 -06002227 /*
2228 * If we already have a head request, queue this one for async
2229 * submittal once the head completes. If we don't have a head but
2230 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
2231 * submitted sync once the chain is complete. If none of those
2232 * conditions are true (normal request), then just queue it.
2233 */
2234 if (*link) {
2235 struct io_kiocb *prev = *link;
2236
2237 sqe_copy = kmemdup(s->sqe, sizeof(*sqe_copy), GFP_KERNEL);
2238 if (!sqe_copy) {
2239 ret = -EAGAIN;
2240 goto err_req;
2241 }
2242
2243 s->sqe = sqe_copy;
2244 memcpy(&req->submit, s, sizeof(*s));
2245 list_add_tail(&req->list, &prev->link_list);
2246 } else if (s->sqe->flags & IOSQE_IO_LINK) {
2247 req->flags |= REQ_F_LINK;
2248
2249 memcpy(&req->submit, s, sizeof(*s));
2250 INIT_LIST_HEAD(&req->link_list);
2251 *link = req;
2252 } else {
Jens Axboec57666682019-09-09 16:19:45 -06002253 io_queue_sqe(ctx, req, s, force_nonblock);
Jens Axboe9e645e112019-05-10 16:07:28 -06002254 }
2255}
2256
Jens Axboe9a56a232019-01-09 09:06:50 -07002257/*
2258 * Batched submission is done, ensure local IO is flushed out.
2259 */
2260static void io_submit_state_end(struct io_submit_state *state)
2261{
2262 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06002263 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07002264 if (state->free_reqs)
2265 kmem_cache_free_bulk(req_cachep, state->free_reqs,
2266 &state->reqs[state->cur_req]);
Jens Axboe9a56a232019-01-09 09:06:50 -07002267}
2268
2269/*
2270 * Start submission side cache.
2271 */
2272static void io_submit_state_start(struct io_submit_state *state,
2273 struct io_ring_ctx *ctx, unsigned max_ios)
2274{
2275 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07002276 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07002277 state->file = NULL;
2278 state->ios_left = max_ios;
2279}
2280
Jens Axboe2b188cc2019-01-07 10:46:33 -07002281static void io_commit_sqring(struct io_ring_ctx *ctx)
2282{
Hristo Venev75b28af2019-08-26 17:23:46 +00002283 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002284
Hristo Venev75b28af2019-08-26 17:23:46 +00002285 if (ctx->cached_sq_head != READ_ONCE(rings->sq.head)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07002286 /*
2287 * Ensure any loads from the SQEs are done at this point,
2288 * since once we write the new head, the application could
2289 * write new data to them.
2290 */
Hristo Venev75b28af2019-08-26 17:23:46 +00002291 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002292 }
2293}
2294
2295/*
Jens Axboe2b188cc2019-01-07 10:46:33 -07002296 * Fetch an sqe, if one is available. Note that s->sqe will point to memory
2297 * that is mapped by userspace. This means that care needs to be taken to
2298 * ensure that reads are stable, as we cannot rely on userspace always
2299 * being a good citizen. If members of the sqe are validated and then later
2300 * used, it's important that those reads are done through READ_ONCE() to
2301 * prevent a re-load down the line.
2302 */
2303static bool io_get_sqring(struct io_ring_ctx *ctx, struct sqe_submit *s)
2304{
Hristo Venev75b28af2019-08-26 17:23:46 +00002305 struct io_rings *rings = ctx->rings;
2306 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002307 unsigned head;
2308
2309 /*
2310 * The cached sq head (or cq tail) serves two purposes:
2311 *
2312 * 1) allows us to batch the cost of updating the user visible
2313 * head updates.
2314 * 2) allows the kernel side to track the head on its own, even
2315 * though the application is the one updating it.
2316 */
2317 head = ctx->cached_sq_head;
Stefan Bühlere523a292019-04-19 11:57:44 +02002318 /* make sure SQ entry isn't read before tail */
Hristo Venev75b28af2019-08-26 17:23:46 +00002319 if (head == smp_load_acquire(&rings->sq.tail))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002320 return false;
2321
Hristo Venev75b28af2019-08-26 17:23:46 +00002322 head = READ_ONCE(sq_array[head & ctx->sq_mask]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002323 if (head < ctx->sq_entries) {
2324 s->index = head;
2325 s->sqe = &ctx->sq_sqes[head];
Jackie Liu8776f3f2019-09-09 20:50:39 +08002326 s->sequence = ctx->cached_sq_head;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002327 ctx->cached_sq_head++;
2328 return true;
2329 }
2330
2331 /* drop invalid entries */
2332 ctx->cached_sq_head++;
Hristo Venev75b28af2019-08-26 17:23:46 +00002333 rings->sq_dropped++;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002334 return false;
2335}
2336
Jens Axboe6c271ce2019-01-10 11:22:30 -07002337static int io_submit_sqes(struct io_ring_ctx *ctx, struct sqe_submit *sqes,
2338 unsigned int nr, bool has_user, bool mm_fault)
2339{
2340 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002341 struct io_kiocb *link = NULL;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002342 struct io_kiocb *shadow_req = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002343 bool prev_was_link = false;
2344 int i, submitted = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002345
2346 if (nr > IO_PLUG_THRESHOLD) {
2347 io_submit_state_start(&state, ctx, nr);
2348 statep = &state;
2349 }
2350
2351 for (i = 0; i < nr; i++) {
Jens Axboe9e645e112019-05-10 16:07:28 -06002352 /*
2353 * If previous wasn't linked and we have a linked command,
2354 * that's the end of the chain. Submit the previous link.
2355 */
2356 if (!prev_was_link && link) {
Jens Axboec57666682019-09-09 16:19:45 -06002357 io_queue_link_head(ctx, link, &link->submit, shadow_req,
2358 true);
Jens Axboe9e645e112019-05-10 16:07:28 -06002359 link = NULL;
Jackie Liu5f5ad9c2019-09-18 10:37:53 +08002360 shadow_req = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002361 }
2362 prev_was_link = (sqes[i].sqe->flags & IOSQE_IO_LINK) != 0;
2363
Jackie Liu4fe2c962019-09-09 20:50:40 +08002364 if (link && (sqes[i].sqe->flags & IOSQE_IO_DRAIN)) {
2365 if (!shadow_req) {
2366 shadow_req = io_get_req(ctx, NULL);
Jackie Liua1041c22019-09-18 17:25:52 +08002367 if (unlikely(!shadow_req))
2368 goto out;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002369 shadow_req->flags |= (REQ_F_IO_DRAIN | REQ_F_SHADOW_DRAIN);
2370 refcount_dec(&shadow_req->refs);
2371 }
2372 shadow_req->sequence = sqes[i].sequence;
2373 }
2374
Jackie Liua1041c22019-09-18 17:25:52 +08002375out:
Jens Axboe6c271ce2019-01-10 11:22:30 -07002376 if (unlikely(mm_fault)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06002377 io_cqring_add_event(ctx, sqes[i].sqe->user_data,
2378 -EFAULT);
Jens Axboe6c271ce2019-01-10 11:22:30 -07002379 } else {
2380 sqes[i].has_user = has_user;
2381 sqes[i].needs_lock = true;
2382 sqes[i].needs_fixed_file = true;
Jens Axboec57666682019-09-09 16:19:45 -06002383 io_submit_sqe(ctx, &sqes[i], statep, &link, true);
Jens Axboe6c271ce2019-01-10 11:22:30 -07002384 submitted++;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002385 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07002386 }
2387
Jens Axboe9e645e112019-05-10 16:07:28 -06002388 if (link)
Jens Axboec57666682019-09-09 16:19:45 -06002389 io_queue_link_head(ctx, link, &link->submit, shadow_req, true);
Jens Axboe6c271ce2019-01-10 11:22:30 -07002390 if (statep)
2391 io_submit_state_end(&state);
2392
2393 return submitted;
2394}
2395
2396static int io_sq_thread(void *data)
2397{
2398 struct sqe_submit sqes[IO_IOPOLL_BATCH];
2399 struct io_ring_ctx *ctx = data;
2400 struct mm_struct *cur_mm = NULL;
2401 mm_segment_t old_fs;
2402 DEFINE_WAIT(wait);
2403 unsigned inflight;
2404 unsigned long timeout;
2405
Jackie Liua4c0b3d2019-07-08 13:41:12 +08002406 complete(&ctx->sqo_thread_started);
2407
Jens Axboe6c271ce2019-01-10 11:22:30 -07002408 old_fs = get_fs();
2409 set_fs(USER_DS);
2410
2411 timeout = inflight = 0;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02002412 while (!kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07002413 bool all_fixed, mm_fault = false;
2414 int i;
2415
2416 if (inflight) {
2417 unsigned nr_events = 0;
2418
2419 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07002420 io_iopoll_check(ctx, &nr_events, 0);
Jens Axboe6c271ce2019-01-10 11:22:30 -07002421 } else {
2422 /*
2423 * Normal IO, just pretend everything completed.
2424 * We don't have to poll completions for that.
2425 */
2426 nr_events = inflight;
2427 }
2428
2429 inflight -= nr_events;
2430 if (!inflight)
2431 timeout = jiffies + ctx->sq_thread_idle;
2432 }
2433
2434 if (!io_get_sqring(ctx, &sqes[0])) {
2435 /*
2436 * We're polling. If we're within the defined idle
2437 * period, then let us spin without work before going
2438 * to sleep.
2439 */
2440 if (inflight || !time_after(jiffies, timeout)) {
Jens Axboe9831a902019-09-19 09:48:55 -06002441 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07002442 continue;
2443 }
2444
2445 /*
2446 * Drop cur_mm before scheduling, we can't hold it for
2447 * long periods (or over schedule()). Do this before
2448 * adding ourselves to the waitqueue, as the unuse/drop
2449 * may sleep.
2450 */
2451 if (cur_mm) {
2452 unuse_mm(cur_mm);
2453 mmput(cur_mm);
2454 cur_mm = NULL;
2455 }
2456
2457 prepare_to_wait(&ctx->sqo_wait, &wait,
2458 TASK_INTERRUPTIBLE);
2459
2460 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00002461 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02002462 /* make sure to read SQ tail after writing flags */
2463 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07002464
2465 if (!io_get_sqring(ctx, &sqes[0])) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02002466 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07002467 finish_wait(&ctx->sqo_wait, &wait);
2468 break;
2469 }
2470 if (signal_pending(current))
2471 flush_signals(current);
2472 schedule();
2473 finish_wait(&ctx->sqo_wait, &wait);
2474
Hristo Venev75b28af2019-08-26 17:23:46 +00002475 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002476 continue;
2477 }
2478 finish_wait(&ctx->sqo_wait, &wait);
2479
Hristo Venev75b28af2019-08-26 17:23:46 +00002480 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002481 }
2482
2483 i = 0;
2484 all_fixed = true;
2485 do {
2486 if (all_fixed && io_sqe_needs_user(sqes[i].sqe))
2487 all_fixed = false;
2488
2489 i++;
2490 if (i == ARRAY_SIZE(sqes))
2491 break;
2492 } while (io_get_sqring(ctx, &sqes[i]));
2493
2494 /* Unless all new commands are FIXED regions, grab mm */
2495 if (!all_fixed && !cur_mm) {
2496 mm_fault = !mmget_not_zero(ctx->sqo_mm);
2497 if (!mm_fault) {
2498 use_mm(ctx->sqo_mm);
2499 cur_mm = ctx->sqo_mm;
2500 }
2501 }
2502
2503 inflight += io_submit_sqes(ctx, sqes, i, cur_mm != NULL,
2504 mm_fault);
2505
2506 /* Commit SQ ring head once we've consumed all SQEs */
2507 io_commit_sqring(ctx);
2508 }
2509
2510 set_fs(old_fs);
2511 if (cur_mm) {
2512 unuse_mm(cur_mm);
2513 mmput(cur_mm);
2514 }
Jens Axboe06058632019-04-13 09:26:03 -06002515
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02002516 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06002517
Jens Axboe6c271ce2019-01-10 11:22:30 -07002518 return 0;
2519}
2520
Jens Axboec57666682019-09-09 16:19:45 -06002521static int io_ring_submit(struct io_ring_ctx *ctx, unsigned int to_submit,
2522 bool block_for_last)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002523{
Jens Axboe9a56a232019-01-09 09:06:50 -07002524 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002525 struct io_kiocb *link = NULL;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002526 struct io_kiocb *shadow_req = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002527 bool prev_was_link = false;
Jens Axboe5c8b0b52019-04-30 10:16:07 -06002528 int i, submit = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002529
Jens Axboe9a56a232019-01-09 09:06:50 -07002530 if (to_submit > IO_PLUG_THRESHOLD) {
2531 io_submit_state_start(&state, ctx, to_submit);
2532 statep = &state;
2533 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002534
2535 for (i = 0; i < to_submit; i++) {
Jens Axboec57666682019-09-09 16:19:45 -06002536 bool force_nonblock = true;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002537 struct sqe_submit s;
2538
2539 if (!io_get_sqring(ctx, &s))
2540 break;
2541
Jens Axboe9e645e112019-05-10 16:07:28 -06002542 /*
2543 * If previous wasn't linked and we have a linked command,
2544 * that's the end of the chain. Submit the previous link.
2545 */
2546 if (!prev_was_link && link) {
Jens Axboec57666682019-09-09 16:19:45 -06002547 io_queue_link_head(ctx, link, &link->submit, shadow_req,
2548 force_nonblock);
Jens Axboe9e645e112019-05-10 16:07:28 -06002549 link = NULL;
Jackie Liu5f5ad9c2019-09-18 10:37:53 +08002550 shadow_req = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002551 }
2552 prev_was_link = (s.sqe->flags & IOSQE_IO_LINK) != 0;
2553
Jackie Liu4fe2c962019-09-09 20:50:40 +08002554 if (link && (s.sqe->flags & IOSQE_IO_DRAIN)) {
2555 if (!shadow_req) {
2556 shadow_req = io_get_req(ctx, NULL);
Jackie Liua1041c22019-09-18 17:25:52 +08002557 if (unlikely(!shadow_req))
2558 goto out;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002559 shadow_req->flags |= (REQ_F_IO_DRAIN | REQ_F_SHADOW_DRAIN);
2560 refcount_dec(&shadow_req->refs);
2561 }
2562 shadow_req->sequence = s.sequence;
2563 }
2564
Jackie Liua1041c22019-09-18 17:25:52 +08002565out:
Jens Axboe2b188cc2019-01-07 10:46:33 -07002566 s.has_user = true;
Jens Axboedef596e2019-01-09 08:59:42 -07002567 s.needs_lock = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002568 s.needs_fixed_file = false;
Jens Axboe5c8b0b52019-04-30 10:16:07 -06002569 submit++;
Jens Axboec57666682019-09-09 16:19:45 -06002570
2571 /*
2572 * The caller will block for events after submit, submit the
2573 * last IO non-blocking. This is either the only IO it's
2574 * submitting, or it already submitted the previous ones. This
2575 * improves performance by avoiding an async punt that we don't
2576 * need to do.
2577 */
2578 if (block_for_last && submit == to_submit)
2579 force_nonblock = false;
2580
2581 io_submit_sqe(ctx, &s, statep, &link, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002582 }
2583 io_commit_sqring(ctx);
2584
Jens Axboe9e645e112019-05-10 16:07:28 -06002585 if (link)
Jens Axboec57666682019-09-09 16:19:45 -06002586 io_queue_link_head(ctx, link, &link->submit, shadow_req,
2587 block_for_last);
Jens Axboe9a56a232019-01-09 09:06:50 -07002588 if (statep)
2589 io_submit_state_end(statep);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002590
Jens Axboe5c8b0b52019-04-30 10:16:07 -06002591 return submit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002592}
2593
Jens Axboe2b188cc2019-01-07 10:46:33 -07002594/*
2595 * Wait until events become available, if we don't already have some. The
2596 * application must reap them itself, as they reside on the shared cq ring.
2597 */
2598static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
2599 const sigset_t __user *sig, size_t sigsz)
2600{
Hristo Venev75b28af2019-08-26 17:23:46 +00002601 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002602 int ret;
2603
Hristo Venev75b28af2019-08-26 17:23:46 +00002604 if (io_cqring_events(rings) >= min_events)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002605 return 0;
2606
2607 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01002608#ifdef CONFIG_COMPAT
2609 if (in_compat_syscall())
2610 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07002611 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01002612 else
2613#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07002614 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01002615
Jens Axboe2b188cc2019-01-07 10:46:33 -07002616 if (ret)
2617 return ret;
2618 }
2619
Hristo Venev75b28af2019-08-26 17:23:46 +00002620 ret = wait_event_interruptible(ctx->wait, io_cqring_events(rings) >= min_events);
Oleg Nesterovb7724342019-07-16 16:29:53 -07002621 restore_saved_sigmask_unless(ret == -ERESTARTSYS);
Oleg Nesterov97abc882019-06-28 12:06:50 -07002622 if (ret == -ERESTARTSYS)
2623 ret = -EINTR;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002624
Hristo Venev75b28af2019-08-26 17:23:46 +00002625 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002626}
2627
Jens Axboe6b063142019-01-10 22:13:58 -07002628static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
2629{
2630#if defined(CONFIG_UNIX)
2631 if (ctx->ring_sock) {
2632 struct sock *sock = ctx->ring_sock->sk;
2633 struct sk_buff *skb;
2634
2635 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
2636 kfree_skb(skb);
2637 }
2638#else
2639 int i;
2640
2641 for (i = 0; i < ctx->nr_user_files; i++)
2642 fput(ctx->user_files[i]);
2643#endif
2644}
2645
2646static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
2647{
2648 if (!ctx->user_files)
2649 return -ENXIO;
2650
2651 __io_sqe_files_unregister(ctx);
2652 kfree(ctx->user_files);
2653 ctx->user_files = NULL;
2654 ctx->nr_user_files = 0;
2655 return 0;
2656}
2657
Jens Axboe6c271ce2019-01-10 11:22:30 -07002658static void io_sq_thread_stop(struct io_ring_ctx *ctx)
2659{
2660 if (ctx->sqo_thread) {
Jackie Liua4c0b3d2019-07-08 13:41:12 +08002661 wait_for_completion(&ctx->sqo_thread_started);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02002662 /*
2663 * The park is a bit of a work-around, without it we get
2664 * warning spews on shutdown with SQPOLL set and affinity
2665 * set to a single CPU.
2666 */
Jens Axboe06058632019-04-13 09:26:03 -06002667 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07002668 kthread_stop(ctx->sqo_thread);
2669 ctx->sqo_thread = NULL;
2670 }
2671}
2672
Jens Axboe6b063142019-01-10 22:13:58 -07002673static void io_finish_async(struct io_ring_ctx *ctx)
2674{
Jens Axboe54a91f32019-09-10 09:15:04 -06002675 int i;
2676
Jens Axboe6c271ce2019-01-10 11:22:30 -07002677 io_sq_thread_stop(ctx);
2678
Jens Axboe54a91f32019-09-10 09:15:04 -06002679 for (i = 0; i < ARRAY_SIZE(ctx->sqo_wq); i++) {
2680 if (ctx->sqo_wq[i]) {
2681 destroy_workqueue(ctx->sqo_wq[i]);
2682 ctx->sqo_wq[i] = NULL;
2683 }
Jens Axboe6b063142019-01-10 22:13:58 -07002684 }
2685}
2686
2687#if defined(CONFIG_UNIX)
2688static void io_destruct_skb(struct sk_buff *skb)
2689{
2690 struct io_ring_ctx *ctx = skb->sk->sk_user_data;
2691
2692 io_finish_async(ctx);
2693 unix_destruct_scm(skb);
2694}
2695
2696/*
2697 * Ensure the UNIX gc is aware of our file set, so we are certain that
2698 * the io_uring can be safely unregistered on process exit, even if we have
2699 * loops in the file referencing.
2700 */
2701static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
2702{
2703 struct sock *sk = ctx->ring_sock->sk;
2704 struct scm_fp_list *fpl;
2705 struct sk_buff *skb;
2706 int i;
2707
2708 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
2709 unsigned long inflight = ctx->user->unix_inflight + nr;
2710
2711 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
2712 return -EMFILE;
2713 }
2714
2715 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
2716 if (!fpl)
2717 return -ENOMEM;
2718
2719 skb = alloc_skb(0, GFP_KERNEL);
2720 if (!skb) {
2721 kfree(fpl);
2722 return -ENOMEM;
2723 }
2724
2725 skb->sk = sk;
2726 skb->destructor = io_destruct_skb;
2727
2728 fpl->user = get_uid(ctx->user);
2729 for (i = 0; i < nr; i++) {
2730 fpl->fp[i] = get_file(ctx->user_files[i + offset]);
2731 unix_inflight(fpl->user, fpl->fp[i]);
2732 }
2733
2734 fpl->max = fpl->count = nr;
2735 UNIXCB(skb).fp = fpl;
2736 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
2737 skb_queue_head(&sk->sk_receive_queue, skb);
2738
2739 for (i = 0; i < nr; i++)
2740 fput(fpl->fp[i]);
2741
2742 return 0;
2743}
2744
2745/*
2746 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
2747 * causes regular reference counting to break down. We rely on the UNIX
2748 * garbage collection to take care of this problem for us.
2749 */
2750static int io_sqe_files_scm(struct io_ring_ctx *ctx)
2751{
2752 unsigned left, total;
2753 int ret = 0;
2754
2755 total = 0;
2756 left = ctx->nr_user_files;
2757 while (left) {
2758 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07002759
2760 ret = __io_sqe_files_scm(ctx, this_files, total);
2761 if (ret)
2762 break;
2763 left -= this_files;
2764 total += this_files;
2765 }
2766
2767 if (!ret)
2768 return 0;
2769
2770 while (total < ctx->nr_user_files) {
2771 fput(ctx->user_files[total]);
2772 total++;
2773 }
2774
2775 return ret;
2776}
2777#else
2778static int io_sqe_files_scm(struct io_ring_ctx *ctx)
2779{
2780 return 0;
2781}
2782#endif
2783
2784static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
2785 unsigned nr_args)
2786{
2787 __s32 __user *fds = (__s32 __user *) arg;
2788 int fd, ret = 0;
2789 unsigned i;
2790
2791 if (ctx->user_files)
2792 return -EBUSY;
2793 if (!nr_args)
2794 return -EINVAL;
2795 if (nr_args > IORING_MAX_FIXED_FILES)
2796 return -EMFILE;
2797
2798 ctx->user_files = kcalloc(nr_args, sizeof(struct file *), GFP_KERNEL);
2799 if (!ctx->user_files)
2800 return -ENOMEM;
2801
2802 for (i = 0; i < nr_args; i++) {
2803 ret = -EFAULT;
2804 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
2805 break;
2806
2807 ctx->user_files[i] = fget(fd);
2808
2809 ret = -EBADF;
2810 if (!ctx->user_files[i])
2811 break;
2812 /*
2813 * Don't allow io_uring instances to be registered. If UNIX
2814 * isn't enabled, then this causes a reference cycle and this
2815 * instance can never get freed. If UNIX is enabled we'll
2816 * handle it just fine, but there's still no point in allowing
2817 * a ring fd as it doesn't support regular read/write anyway.
2818 */
2819 if (ctx->user_files[i]->f_op == &io_uring_fops) {
2820 fput(ctx->user_files[i]);
2821 break;
2822 }
2823 ctx->nr_user_files++;
2824 ret = 0;
2825 }
2826
2827 if (ret) {
2828 for (i = 0; i < ctx->nr_user_files; i++)
2829 fput(ctx->user_files[i]);
2830
2831 kfree(ctx->user_files);
Jens Axboe25adf502019-04-03 09:52:40 -06002832 ctx->user_files = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07002833 ctx->nr_user_files = 0;
2834 return ret;
2835 }
2836
2837 ret = io_sqe_files_scm(ctx);
2838 if (ret)
2839 io_sqe_files_unregister(ctx);
2840
2841 return ret;
2842}
2843
Jens Axboe6c271ce2019-01-10 11:22:30 -07002844static int io_sq_offload_start(struct io_ring_ctx *ctx,
2845 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002846{
2847 int ret;
2848
Jens Axboe6c271ce2019-01-10 11:22:30 -07002849 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002850 mmgrab(current->mm);
2851 ctx->sqo_mm = current->mm;
2852
Jens Axboe6c271ce2019-01-10 11:22:30 -07002853 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06002854 ret = -EPERM;
2855 if (!capable(CAP_SYS_ADMIN))
2856 goto err;
2857
Jens Axboe917257d2019-04-13 09:28:55 -06002858 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
2859 if (!ctx->sq_thread_idle)
2860 ctx->sq_thread_idle = HZ;
2861
Jens Axboe6c271ce2019-01-10 11:22:30 -07002862 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06002863 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002864
Jens Axboe917257d2019-04-13 09:28:55 -06002865 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06002866 if (cpu >= nr_cpu_ids)
2867 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08002868 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06002869 goto err;
2870
Jens Axboe6c271ce2019-01-10 11:22:30 -07002871 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
2872 ctx, cpu,
2873 "io_uring-sq");
2874 } else {
2875 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
2876 "io_uring-sq");
2877 }
2878 if (IS_ERR(ctx->sqo_thread)) {
2879 ret = PTR_ERR(ctx->sqo_thread);
2880 ctx->sqo_thread = NULL;
2881 goto err;
2882 }
2883 wake_up_process(ctx->sqo_thread);
2884 } else if (p->flags & IORING_SETUP_SQ_AFF) {
2885 /* Can't have SQ_AFF without SQPOLL */
2886 ret = -EINVAL;
2887 goto err;
2888 }
2889
Jens Axboe2b188cc2019-01-07 10:46:33 -07002890 /* Do QD, or 2 * CPUS, whatever is smallest */
Jens Axboe54a91f32019-09-10 09:15:04 -06002891 ctx->sqo_wq[0] = alloc_workqueue("io_ring-wq",
2892 WQ_UNBOUND | WQ_FREEZABLE,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002893 min(ctx->sq_entries - 1, 2 * num_online_cpus()));
Jens Axboe54a91f32019-09-10 09:15:04 -06002894 if (!ctx->sqo_wq[0]) {
2895 ret = -ENOMEM;
2896 goto err;
2897 }
2898
2899 /*
2900 * This is for buffered writes, where we want to limit the parallelism
2901 * due to file locking in file systems. As "normal" buffered writes
2902 * should parellelize on writeout quite nicely, limit us to having 2
2903 * pending. This avoids massive contention on the inode when doing
2904 * buffered async writes.
2905 */
2906 ctx->sqo_wq[1] = alloc_workqueue("io_ring-write-wq",
2907 WQ_UNBOUND | WQ_FREEZABLE, 2);
2908 if (!ctx->sqo_wq[1]) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07002909 ret = -ENOMEM;
2910 goto err;
2911 }
2912
2913 return 0;
2914err:
Jens Axboe54a91f32019-09-10 09:15:04 -06002915 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002916 mmdrop(ctx->sqo_mm);
2917 ctx->sqo_mm = NULL;
2918 return ret;
2919}
2920
2921static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
2922{
2923 atomic_long_sub(nr_pages, &user->locked_vm);
2924}
2925
2926static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
2927{
2928 unsigned long page_limit, cur_pages, new_pages;
2929
2930 /* Don't allow more pages than we can safely lock */
2931 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
2932
2933 do {
2934 cur_pages = atomic_long_read(&user->locked_vm);
2935 new_pages = cur_pages + nr_pages;
2936 if (new_pages > page_limit)
2937 return -ENOMEM;
2938 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
2939 new_pages) != cur_pages);
2940
2941 return 0;
2942}
2943
2944static void io_mem_free(void *ptr)
2945{
Mark Rutland52e04ef2019-04-30 17:30:21 +01002946 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002947
Mark Rutland52e04ef2019-04-30 17:30:21 +01002948 if (!ptr)
2949 return;
2950
2951 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002952 if (put_page_testzero(page))
2953 free_compound_page(page);
2954}
2955
2956static void *io_mem_alloc(size_t size)
2957{
2958 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
2959 __GFP_NORETRY;
2960
2961 return (void *) __get_free_pages(gfp_flags, get_order(size));
2962}
2963
Hristo Venev75b28af2019-08-26 17:23:46 +00002964static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
2965 size_t *sq_offset)
2966{
2967 struct io_rings *rings;
2968 size_t off, sq_array_size;
2969
2970 off = struct_size(rings, cqes, cq_entries);
2971 if (off == SIZE_MAX)
2972 return SIZE_MAX;
2973
2974#ifdef CONFIG_SMP
2975 off = ALIGN(off, SMP_CACHE_BYTES);
2976 if (off == 0)
2977 return SIZE_MAX;
2978#endif
2979
2980 sq_array_size = array_size(sizeof(u32), sq_entries);
2981 if (sq_array_size == SIZE_MAX)
2982 return SIZE_MAX;
2983
2984 if (check_add_overflow(off, sq_array_size, &off))
2985 return SIZE_MAX;
2986
2987 if (sq_offset)
2988 *sq_offset = off;
2989
2990 return off;
2991}
2992
Jens Axboe2b188cc2019-01-07 10:46:33 -07002993static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
2994{
Hristo Venev75b28af2019-08-26 17:23:46 +00002995 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002996
Hristo Venev75b28af2019-08-26 17:23:46 +00002997 pages = (size_t)1 << get_order(
2998 rings_size(sq_entries, cq_entries, NULL));
2999 pages += (size_t)1 << get_order(
3000 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07003001
Hristo Venev75b28af2019-08-26 17:23:46 +00003002 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003003}
3004
Jens Axboeedafcce2019-01-09 09:16:05 -07003005static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
3006{
3007 int i, j;
3008
3009 if (!ctx->user_bufs)
3010 return -ENXIO;
3011
3012 for (i = 0; i < ctx->nr_user_bufs; i++) {
3013 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
3014
3015 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbard27c4d3a2019-08-04 19:32:06 -07003016 put_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07003017
3018 if (ctx->account_mem)
3019 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003020 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07003021 imu->nr_bvecs = 0;
3022 }
3023
3024 kfree(ctx->user_bufs);
3025 ctx->user_bufs = NULL;
3026 ctx->nr_user_bufs = 0;
3027 return 0;
3028}
3029
3030static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
3031 void __user *arg, unsigned index)
3032{
3033 struct iovec __user *src;
3034
3035#ifdef CONFIG_COMPAT
3036 if (ctx->compat) {
3037 struct compat_iovec __user *ciovs;
3038 struct compat_iovec ciov;
3039
3040 ciovs = (struct compat_iovec __user *) arg;
3041 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
3042 return -EFAULT;
3043
3044 dst->iov_base = (void __user *) (unsigned long) ciov.iov_base;
3045 dst->iov_len = ciov.iov_len;
3046 return 0;
3047 }
3048#endif
3049 src = (struct iovec __user *) arg;
3050 if (copy_from_user(dst, &src[index], sizeof(*dst)))
3051 return -EFAULT;
3052 return 0;
3053}
3054
3055static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
3056 unsigned nr_args)
3057{
3058 struct vm_area_struct **vmas = NULL;
3059 struct page **pages = NULL;
3060 int i, j, got_pages = 0;
3061 int ret = -EINVAL;
3062
3063 if (ctx->user_bufs)
3064 return -EBUSY;
3065 if (!nr_args || nr_args > UIO_MAXIOV)
3066 return -EINVAL;
3067
3068 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
3069 GFP_KERNEL);
3070 if (!ctx->user_bufs)
3071 return -ENOMEM;
3072
3073 for (i = 0; i < nr_args; i++) {
3074 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
3075 unsigned long off, start, end, ubuf;
3076 int pret, nr_pages;
3077 struct iovec iov;
3078 size_t size;
3079
3080 ret = io_copy_iov(ctx, &iov, arg, i);
3081 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03003082 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07003083
3084 /*
3085 * Don't impose further limits on the size and buffer
3086 * constraints here, we'll -EINVAL later when IO is
3087 * submitted if they are wrong.
3088 */
3089 ret = -EFAULT;
3090 if (!iov.iov_base || !iov.iov_len)
3091 goto err;
3092
3093 /* arbitrary limit, but we need something */
3094 if (iov.iov_len > SZ_1G)
3095 goto err;
3096
3097 ubuf = (unsigned long) iov.iov_base;
3098 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
3099 start = ubuf >> PAGE_SHIFT;
3100 nr_pages = end - start;
3101
3102 if (ctx->account_mem) {
3103 ret = io_account_mem(ctx->user, nr_pages);
3104 if (ret)
3105 goto err;
3106 }
3107
3108 ret = 0;
3109 if (!pages || nr_pages > got_pages) {
3110 kfree(vmas);
3111 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003112 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07003113 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003114 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07003115 sizeof(struct vm_area_struct *),
3116 GFP_KERNEL);
3117 if (!pages || !vmas) {
3118 ret = -ENOMEM;
3119 if (ctx->account_mem)
3120 io_unaccount_mem(ctx->user, nr_pages);
3121 goto err;
3122 }
3123 got_pages = nr_pages;
3124 }
3125
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003126 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07003127 GFP_KERNEL);
3128 ret = -ENOMEM;
3129 if (!imu->bvec) {
3130 if (ctx->account_mem)
3131 io_unaccount_mem(ctx->user, nr_pages);
3132 goto err;
3133 }
3134
3135 ret = 0;
3136 down_read(&current->mm->mmap_sem);
Ira Weiny932f4a62019-05-13 17:17:03 -07003137 pret = get_user_pages(ubuf, nr_pages,
3138 FOLL_WRITE | FOLL_LONGTERM,
3139 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07003140 if (pret == nr_pages) {
3141 /* don't support file backed memory */
3142 for (j = 0; j < nr_pages; j++) {
3143 struct vm_area_struct *vma = vmas[j];
3144
3145 if (vma->vm_file &&
3146 !is_file_hugepages(vma->vm_file)) {
3147 ret = -EOPNOTSUPP;
3148 break;
3149 }
3150 }
3151 } else {
3152 ret = pret < 0 ? pret : -EFAULT;
3153 }
3154 up_read(&current->mm->mmap_sem);
3155 if (ret) {
3156 /*
3157 * if we did partial map, or found file backed vmas,
3158 * release any pages we did get
3159 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07003160 if (pret > 0)
3161 put_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07003162 if (ctx->account_mem)
3163 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003164 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07003165 goto err;
3166 }
3167
3168 off = ubuf & ~PAGE_MASK;
3169 size = iov.iov_len;
3170 for (j = 0; j < nr_pages; j++) {
3171 size_t vec_len;
3172
3173 vec_len = min_t(size_t, size, PAGE_SIZE - off);
3174 imu->bvec[j].bv_page = pages[j];
3175 imu->bvec[j].bv_len = vec_len;
3176 imu->bvec[j].bv_offset = off;
3177 off = 0;
3178 size -= vec_len;
3179 }
3180 /* store original address for later verification */
3181 imu->ubuf = ubuf;
3182 imu->len = iov.iov_len;
3183 imu->nr_bvecs = nr_pages;
3184
3185 ctx->nr_user_bufs++;
3186 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003187 kvfree(pages);
3188 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07003189 return 0;
3190err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003191 kvfree(pages);
3192 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07003193 io_sqe_buffer_unregister(ctx);
3194 return ret;
3195}
3196
Jens Axboe9b402842019-04-11 11:45:41 -06003197static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
3198{
3199 __s32 __user *fds = arg;
3200 int fd;
3201
3202 if (ctx->cq_ev_fd)
3203 return -EBUSY;
3204
3205 if (copy_from_user(&fd, fds, sizeof(*fds)))
3206 return -EFAULT;
3207
3208 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
3209 if (IS_ERR(ctx->cq_ev_fd)) {
3210 int ret = PTR_ERR(ctx->cq_ev_fd);
3211 ctx->cq_ev_fd = NULL;
3212 return ret;
3213 }
3214
3215 return 0;
3216}
3217
3218static int io_eventfd_unregister(struct io_ring_ctx *ctx)
3219{
3220 if (ctx->cq_ev_fd) {
3221 eventfd_ctx_put(ctx->cq_ev_fd);
3222 ctx->cq_ev_fd = NULL;
3223 return 0;
3224 }
3225
3226 return -ENXIO;
3227}
3228
Jens Axboe2b188cc2019-01-07 10:46:33 -07003229static void io_ring_ctx_free(struct io_ring_ctx *ctx)
3230{
Jens Axboe6b063142019-01-10 22:13:58 -07003231 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003232 if (ctx->sqo_mm)
3233 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07003234
3235 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07003236 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07003237 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06003238 io_eventfd_unregister(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07003239
Jens Axboe2b188cc2019-01-07 10:46:33 -07003240#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07003241 if (ctx->ring_sock) {
3242 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07003243 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07003244 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003245#endif
3246
Hristo Venev75b28af2019-08-26 17:23:46 +00003247 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003248 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003249
3250 percpu_ref_exit(&ctx->refs);
3251 if (ctx->account_mem)
3252 io_unaccount_mem(ctx->user,
3253 ring_pages(ctx->sq_entries, ctx->cq_entries));
3254 free_uid(ctx->user);
3255 kfree(ctx);
3256}
3257
3258static __poll_t io_uring_poll(struct file *file, poll_table *wait)
3259{
3260 struct io_ring_ctx *ctx = file->private_data;
3261 __poll_t mask = 0;
3262
3263 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02003264 /*
3265 * synchronizes with barrier from wq_has_sleeper call in
3266 * io_commit_cqring
3267 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07003268 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00003269 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
3270 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003271 mask |= EPOLLOUT | EPOLLWRNORM;
Hristo Venev75b28af2019-08-26 17:23:46 +00003272 if (READ_ONCE(ctx->rings->sq.head) != ctx->cached_cq_tail)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003273 mask |= EPOLLIN | EPOLLRDNORM;
3274
3275 return mask;
3276}
3277
3278static int io_uring_fasync(int fd, struct file *file, int on)
3279{
3280 struct io_ring_ctx *ctx = file->private_data;
3281
3282 return fasync_helper(fd, file, on, &ctx->cq_fasync);
3283}
3284
3285static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
3286{
3287 mutex_lock(&ctx->uring_lock);
3288 percpu_ref_kill(&ctx->refs);
3289 mutex_unlock(&ctx->uring_lock);
3290
Jens Axboe221c5eb2019-01-17 09:41:58 -07003291 io_poll_remove_all(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07003292 io_iopoll_reap_events(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003293 wait_for_completion(&ctx->ctx_done);
3294 io_ring_ctx_free(ctx);
3295}
3296
3297static int io_uring_release(struct inode *inode, struct file *file)
3298{
3299 struct io_ring_ctx *ctx = file->private_data;
3300
3301 file->private_data = NULL;
3302 io_ring_ctx_wait_and_kill(ctx);
3303 return 0;
3304}
3305
3306static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
3307{
3308 loff_t offset = (loff_t) vma->vm_pgoff << PAGE_SHIFT;
3309 unsigned long sz = vma->vm_end - vma->vm_start;
3310 struct io_ring_ctx *ctx = file->private_data;
3311 unsigned long pfn;
3312 struct page *page;
3313 void *ptr;
3314
3315 switch (offset) {
3316 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00003317 case IORING_OFF_CQ_RING:
3318 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003319 break;
3320 case IORING_OFF_SQES:
3321 ptr = ctx->sq_sqes;
3322 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003323 default:
3324 return -EINVAL;
3325 }
3326
3327 page = virt_to_head_page(ptr);
3328 if (sz > (PAGE_SIZE << compound_order(page)))
3329 return -EINVAL;
3330
3331 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
3332 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
3333}
3334
3335SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
3336 u32, min_complete, u32, flags, const sigset_t __user *, sig,
3337 size_t, sigsz)
3338{
3339 struct io_ring_ctx *ctx;
3340 long ret = -EBADF;
3341 int submitted = 0;
3342 struct fd f;
3343
Jens Axboe6c271ce2019-01-10 11:22:30 -07003344 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07003345 return -EINVAL;
3346
3347 f = fdget(fd);
3348 if (!f.file)
3349 return -EBADF;
3350
3351 ret = -EOPNOTSUPP;
3352 if (f.file->f_op != &io_uring_fops)
3353 goto out_fput;
3354
3355 ret = -ENXIO;
3356 ctx = f.file->private_data;
3357 if (!percpu_ref_tryget(&ctx->refs))
3358 goto out_fput;
3359
Jens Axboe6c271ce2019-01-10 11:22:30 -07003360 /*
3361 * For SQ polling, the thread will do all submissions and completions.
3362 * Just return the requested submit count, and wake the thread if
3363 * we were asked to.
3364 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06003365 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003366 if (ctx->flags & IORING_SETUP_SQPOLL) {
3367 if (flags & IORING_ENTER_SQ_WAKEUP)
3368 wake_up(&ctx->sqo_wait);
3369 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06003370 } else if (to_submit) {
Jens Axboec57666682019-09-09 16:19:45 -06003371 bool block_for_last = false;
3372
Jens Axboe2b188cc2019-01-07 10:46:33 -07003373 to_submit = min(to_submit, ctx->sq_entries);
3374
Jens Axboec57666682019-09-09 16:19:45 -06003375 /*
3376 * Allow last submission to block in a series, IFF the caller
3377 * asked to wait for events and we don't currently have
3378 * enough. This potentially avoids an async punt.
3379 */
3380 if (to_submit == min_complete &&
3381 io_cqring_events(ctx->rings) < min_complete)
3382 block_for_last = true;
3383
Jens Axboe2b188cc2019-01-07 10:46:33 -07003384 mutex_lock(&ctx->uring_lock);
Jens Axboec57666682019-09-09 16:19:45 -06003385 submitted = io_ring_submit(ctx, to_submit, block_for_last);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003386 mutex_unlock(&ctx->uring_lock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003387 }
3388 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07003389 unsigned nr_events = 0;
3390
Jens Axboe2b188cc2019-01-07 10:46:33 -07003391 min_complete = min(min_complete, ctx->cq_entries);
3392
Jens Axboedef596e2019-01-09 08:59:42 -07003393 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07003394 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07003395 } else {
3396 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
3397 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003398 }
3399
Jens Axboe2b188cc2019-01-07 10:46:33 -07003400 io_ring_drop_ctx_refs(ctx, 1);
3401out_fput:
3402 fdput(f);
3403 return submitted ? submitted : ret;
3404}
3405
3406static const struct file_operations io_uring_fops = {
3407 .release = io_uring_release,
3408 .mmap = io_uring_mmap,
3409 .poll = io_uring_poll,
3410 .fasync = io_uring_fasync,
3411};
3412
3413static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
3414 struct io_uring_params *p)
3415{
Hristo Venev75b28af2019-08-26 17:23:46 +00003416 struct io_rings *rings;
3417 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003418
Hristo Venev75b28af2019-08-26 17:23:46 +00003419 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
3420 if (size == SIZE_MAX)
3421 return -EOVERFLOW;
3422
3423 rings = io_mem_alloc(size);
3424 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003425 return -ENOMEM;
3426
Hristo Venev75b28af2019-08-26 17:23:46 +00003427 ctx->rings = rings;
3428 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
3429 rings->sq_ring_mask = p->sq_entries - 1;
3430 rings->cq_ring_mask = p->cq_entries - 1;
3431 rings->sq_ring_entries = p->sq_entries;
3432 rings->cq_ring_entries = p->cq_entries;
3433 ctx->sq_mask = rings->sq_ring_mask;
3434 ctx->cq_mask = rings->cq_ring_mask;
3435 ctx->sq_entries = rings->sq_ring_entries;
3436 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003437
3438 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
3439 if (size == SIZE_MAX)
3440 return -EOVERFLOW;
3441
3442 ctx->sq_sqes = io_mem_alloc(size);
Mark Rutland52e04ef2019-04-30 17:30:21 +01003443 if (!ctx->sq_sqes)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003444 return -ENOMEM;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003445
Jens Axboe2b188cc2019-01-07 10:46:33 -07003446 return 0;
3447}
3448
3449/*
3450 * Allocate an anonymous fd, this is what constitutes the application
3451 * visible backing of an io_uring instance. The application mmaps this
3452 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
3453 * we have to tie this fd to a socket for file garbage collection purposes.
3454 */
3455static int io_uring_get_fd(struct io_ring_ctx *ctx)
3456{
3457 struct file *file;
3458 int ret;
3459
3460#if defined(CONFIG_UNIX)
3461 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
3462 &ctx->ring_sock);
3463 if (ret)
3464 return ret;
3465#endif
3466
3467 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
3468 if (ret < 0)
3469 goto err;
3470
3471 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
3472 O_RDWR | O_CLOEXEC);
3473 if (IS_ERR(file)) {
3474 put_unused_fd(ret);
3475 ret = PTR_ERR(file);
3476 goto err;
3477 }
3478
3479#if defined(CONFIG_UNIX)
3480 ctx->ring_sock->file = file;
Jens Axboe6b063142019-01-10 22:13:58 -07003481 ctx->ring_sock->sk->sk_user_data = ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003482#endif
3483 fd_install(ret, file);
3484 return ret;
3485err:
3486#if defined(CONFIG_UNIX)
3487 sock_release(ctx->ring_sock);
3488 ctx->ring_sock = NULL;
3489#endif
3490 return ret;
3491}
3492
3493static int io_uring_create(unsigned entries, struct io_uring_params *p)
3494{
3495 struct user_struct *user = NULL;
3496 struct io_ring_ctx *ctx;
3497 bool account_mem;
3498 int ret;
3499
3500 if (!entries || entries > IORING_MAX_ENTRIES)
3501 return -EINVAL;
3502
3503 /*
3504 * Use twice as many entries for the CQ ring. It's possible for the
3505 * application to drive a higher depth than the size of the SQ ring,
3506 * since the sqes are only used at submission time. This allows for
3507 * some flexibility in overcommitting a bit.
3508 */
3509 p->sq_entries = roundup_pow_of_two(entries);
3510 p->cq_entries = 2 * p->sq_entries;
3511
3512 user = get_uid(current_user());
3513 account_mem = !capable(CAP_IPC_LOCK);
3514
3515 if (account_mem) {
3516 ret = io_account_mem(user,
3517 ring_pages(p->sq_entries, p->cq_entries));
3518 if (ret) {
3519 free_uid(user);
3520 return ret;
3521 }
3522 }
3523
3524 ctx = io_ring_ctx_alloc(p);
3525 if (!ctx) {
3526 if (account_mem)
3527 io_unaccount_mem(user, ring_pages(p->sq_entries,
3528 p->cq_entries));
3529 free_uid(user);
3530 return -ENOMEM;
3531 }
3532 ctx->compat = in_compat_syscall();
3533 ctx->account_mem = account_mem;
3534 ctx->user = user;
3535
3536 ret = io_allocate_scq_urings(ctx, p);
3537 if (ret)
3538 goto err;
3539
Jens Axboe6c271ce2019-01-10 11:22:30 -07003540 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003541 if (ret)
3542 goto err;
3543
3544 ret = io_uring_get_fd(ctx);
3545 if (ret < 0)
3546 goto err;
3547
3548 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00003549 p->sq_off.head = offsetof(struct io_rings, sq.head);
3550 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
3551 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
3552 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
3553 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
3554 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
3555 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003556
3557 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00003558 p->cq_off.head = offsetof(struct io_rings, cq.head);
3559 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
3560 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
3561 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
3562 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
3563 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06003564
3565 p->features = IORING_FEAT_SINGLE_MMAP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003566 return ret;
3567err:
3568 io_ring_ctx_wait_and_kill(ctx);
3569 return ret;
3570}
3571
3572/*
3573 * Sets up an aio uring context, and returns the fd. Applications asks for a
3574 * ring size, we return the actual sq/cq ring sizes (among other things) in the
3575 * params structure passed in.
3576 */
3577static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
3578{
3579 struct io_uring_params p;
3580 long ret;
3581 int i;
3582
3583 if (copy_from_user(&p, params, sizeof(p)))
3584 return -EFAULT;
3585 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
3586 if (p.resv[i])
3587 return -EINVAL;
3588 }
3589
Jens Axboe6c271ce2019-01-10 11:22:30 -07003590 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
3591 IORING_SETUP_SQ_AFF))
Jens Axboe2b188cc2019-01-07 10:46:33 -07003592 return -EINVAL;
3593
3594 ret = io_uring_create(entries, &p);
3595 if (ret < 0)
3596 return ret;
3597
3598 if (copy_to_user(params, &p, sizeof(p)))
3599 return -EFAULT;
3600
3601 return ret;
3602}
3603
3604SYSCALL_DEFINE2(io_uring_setup, u32, entries,
3605 struct io_uring_params __user *, params)
3606{
3607 return io_uring_setup(entries, params);
3608}
3609
Jens Axboeedafcce2019-01-09 09:16:05 -07003610static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
3611 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06003612 __releases(ctx->uring_lock)
3613 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07003614{
3615 int ret;
3616
Jens Axboe35fa71a2019-04-22 10:23:23 -06003617 /*
3618 * We're inside the ring mutex, if the ref is already dying, then
3619 * someone else killed the ctx or is already going through
3620 * io_uring_register().
3621 */
3622 if (percpu_ref_is_dying(&ctx->refs))
3623 return -ENXIO;
3624
Jens Axboeedafcce2019-01-09 09:16:05 -07003625 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06003626
3627 /*
3628 * Drop uring mutex before waiting for references to exit. If another
3629 * thread is currently inside io_uring_enter() it might need to grab
3630 * the uring_lock to make progress. If we hold it here across the drain
3631 * wait, then we can deadlock. It's safe to drop the mutex here, since
3632 * no new references will come in after we've killed the percpu ref.
3633 */
3634 mutex_unlock(&ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07003635 wait_for_completion(&ctx->ctx_done);
Jens Axboeb19062a2019-04-15 10:49:38 -06003636 mutex_lock(&ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07003637
3638 switch (opcode) {
3639 case IORING_REGISTER_BUFFERS:
3640 ret = io_sqe_buffer_register(ctx, arg, nr_args);
3641 break;
3642 case IORING_UNREGISTER_BUFFERS:
3643 ret = -EINVAL;
3644 if (arg || nr_args)
3645 break;
3646 ret = io_sqe_buffer_unregister(ctx);
3647 break;
Jens Axboe6b063142019-01-10 22:13:58 -07003648 case IORING_REGISTER_FILES:
3649 ret = io_sqe_files_register(ctx, arg, nr_args);
3650 break;
3651 case IORING_UNREGISTER_FILES:
3652 ret = -EINVAL;
3653 if (arg || nr_args)
3654 break;
3655 ret = io_sqe_files_unregister(ctx);
3656 break;
Jens Axboe9b402842019-04-11 11:45:41 -06003657 case IORING_REGISTER_EVENTFD:
3658 ret = -EINVAL;
3659 if (nr_args != 1)
3660 break;
3661 ret = io_eventfd_register(ctx, arg);
3662 break;
3663 case IORING_UNREGISTER_EVENTFD:
3664 ret = -EINVAL;
3665 if (arg || nr_args)
3666 break;
3667 ret = io_eventfd_unregister(ctx);
3668 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07003669 default:
3670 ret = -EINVAL;
3671 break;
3672 }
3673
3674 /* bring the ctx back to life */
3675 reinit_completion(&ctx->ctx_done);
3676 percpu_ref_reinit(&ctx->refs);
3677 return ret;
3678}
3679
3680SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
3681 void __user *, arg, unsigned int, nr_args)
3682{
3683 struct io_ring_ctx *ctx;
3684 long ret = -EBADF;
3685 struct fd f;
3686
3687 f = fdget(fd);
3688 if (!f.file)
3689 return -EBADF;
3690
3691 ret = -EOPNOTSUPP;
3692 if (f.file->f_op != &io_uring_fops)
3693 goto out_fput;
3694
3695 ctx = f.file->private_data;
3696
3697 mutex_lock(&ctx->uring_lock);
3698 ret = __io_uring_register(ctx, opcode, arg, nr_args);
3699 mutex_unlock(&ctx->uring_lock);
3700out_fput:
3701 fdput(f);
3702 return ret;
3703}
3704
Jens Axboe2b188cc2019-01-07 10:46:33 -07003705static int __init io_uring_init(void)
3706{
3707 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
3708 return 0;
3709};
3710__initcall(io_uring_init);