blob: 41840bf26d3b3024534a8979bd5028bbc643bf4d [file] [log] [blame]
Jens Axboe2b188cc2019-01-07 10:46:33 -07001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Shared application/kernel submission and completion ring pairs, for
4 * supporting fast/efficient IO.
5 *
6 * A note on the read/write ordering memory barriers that are matched between
Stefan Bühler1e84b972019-04-24 23:54:16 +02007 * the application and kernel side.
8 *
9 * After the application reads the CQ ring tail, it must use an
10 * appropriate smp_rmb() to pair with the smp_wmb() the kernel uses
11 * before writing the tail (using smp_load_acquire to read the tail will
12 * do). It also needs a smp_mb() before updating CQ head (ordering the
13 * entry load(s) with the head store), pairing with an implicit barrier
14 * through a control-dependency in io_get_cqring (smp_store_release to
15 * store head will do). Failure to do so could lead to reading invalid
16 * CQ entries.
17 *
18 * Likewise, the application must use an appropriate smp_wmb() before
19 * writing the SQ tail (ordering SQ entry stores with the tail store),
20 * which pairs with smp_load_acquire in io_get_sqring (smp_store_release
21 * to store the tail will do). And it needs a barrier ordering the SQ
22 * head load before writing new SQ entries (smp_load_acquire to read
23 * head will do).
24 *
25 * When using the SQ poll thread (IORING_SETUP_SQPOLL), the application
26 * needs to check the SQ flags for IORING_SQ_NEED_WAKEUP *after*
27 * updating the SQ tail; a full memory barrier smp_mb() is needed
28 * between.
Jens Axboe2b188cc2019-01-07 10:46:33 -070029 *
30 * Also see the examples in the liburing library:
31 *
32 * git://git.kernel.dk/liburing
33 *
34 * io_uring also uses READ/WRITE_ONCE() for _any_ store or load that happens
35 * from data shared between the kernel and application. This is done both
36 * for ordering purposes, but also to ensure that once a value is loaded from
37 * data that the application could potentially modify, it remains stable.
38 *
39 * Copyright (C) 2018-2019 Jens Axboe
Christoph Hellwigc992fe22019-01-11 09:43:02 -070040 * Copyright (c) 2018-2019 Christoph Hellwig
Jens Axboe2b188cc2019-01-07 10:46:33 -070041 */
42#include <linux/kernel.h>
43#include <linux/init.h>
44#include <linux/errno.h>
45#include <linux/syscalls.h>
46#include <linux/compat.h>
47#include <linux/refcount.h>
48#include <linux/uio.h>
49
50#include <linux/sched/signal.h>
51#include <linux/fs.h>
52#include <linux/file.h>
53#include <linux/fdtable.h>
54#include <linux/mm.h>
55#include <linux/mman.h>
56#include <linux/mmu_context.h>
57#include <linux/percpu.h>
58#include <linux/slab.h>
59#include <linux/workqueue.h>
Jens Axboe6c271ce2019-01-10 11:22:30 -070060#include <linux/kthread.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070061#include <linux/blkdev.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070062#include <linux/bvec.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070063#include <linux/net.h>
64#include <net/sock.h>
65#include <net/af_unix.h>
Jens Axboe6b063142019-01-10 22:13:58 -070066#include <net/scm.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070067#include <linux/anon_inodes.h>
68#include <linux/sched/mm.h>
69#include <linux/uaccess.h>
70#include <linux/nospec.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070071#include <linux/sizes.h>
72#include <linux/hugetlb.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070073
74#include <uapi/linux/io_uring.h>
75
76#include "internal.h"
77
78#define IORING_MAX_ENTRIES 4096
Jens Axboe6b063142019-01-10 22:13:58 -070079#define IORING_MAX_FIXED_FILES 1024
Jens Axboe2b188cc2019-01-07 10:46:33 -070080
81struct io_uring {
82 u32 head ____cacheline_aligned_in_smp;
83 u32 tail ____cacheline_aligned_in_smp;
84};
85
Stefan Bühler1e84b972019-04-24 23:54:16 +020086/*
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;
170 off_t io_end;
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 */
206 struct workqueue_struct *sqo_wq;
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{
449 queue_work(ctx->sqo_wq, &req->work);
450}
451
Jens Axboede0617e2019-04-06 21:51:27 -0600452static void io_commit_cqring(struct io_ring_ctx *ctx)
453{
454 struct io_kiocb *req;
455
456 __io_commit_cqring(ctx);
457
458 while ((req = io_get_deferred_req(ctx)) != NULL) {
Jackie Liu4fe2c962019-09-09 20:50:40 +0800459 if (req->flags & REQ_F_SHADOW_DRAIN) {
460 /* Just for drain, free it. */
461 __io_free_req(req);
462 continue;
463 }
Jens Axboede0617e2019-04-06 21:51:27 -0600464 req->flags |= REQ_F_IO_DRAINED;
Jens Axboe18d9be12019-09-10 09:13:05 -0600465 io_queue_async_work(ctx, req);
Jens Axboede0617e2019-04-06 21:51:27 -0600466 }
467}
468
Jens Axboe2b188cc2019-01-07 10:46:33 -0700469static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
470{
Hristo Venev75b28af2019-08-26 17:23:46 +0000471 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700472 unsigned tail;
473
474 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +0200475 /*
476 * writes to the cq entry need to come after reading head; the
477 * control dependency is enough as we're using WRITE_ONCE to
478 * fill the cq entry
479 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000480 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700481 return NULL;
482
483 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +0000484 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -0700485}
486
487static void io_cqring_fill_event(struct io_ring_ctx *ctx, u64 ki_user_data,
Jens Axboec71ffb62019-05-13 20:58:29 -0600488 long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700489{
490 struct io_uring_cqe *cqe;
491
492 /*
493 * If we can't get a cq entry, userspace overflowed the
494 * submission (by quite a lot). Increment the overflow count in
495 * the ring.
496 */
497 cqe = io_get_cqring(ctx);
498 if (cqe) {
499 WRITE_ONCE(cqe->user_data, ki_user_data);
500 WRITE_ONCE(cqe->res, res);
Jens Axboec71ffb62019-05-13 20:58:29 -0600501 WRITE_ONCE(cqe->flags, 0);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700502 } else {
Hristo Venev75b28af2019-08-26 17:23:46 +0000503 unsigned overflow = READ_ONCE(ctx->rings->cq_overflow);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700504
Hristo Venev75b28af2019-08-26 17:23:46 +0000505 WRITE_ONCE(ctx->rings->cq_overflow, overflow + 1);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700506 }
507}
508
Jens Axboe8c838782019-03-12 15:48:16 -0600509static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
510{
511 if (waitqueue_active(&ctx->wait))
512 wake_up(&ctx->wait);
513 if (waitqueue_active(&ctx->sqo_wait))
514 wake_up(&ctx->sqo_wait);
Jens Axboe9b402842019-04-11 11:45:41 -0600515 if (ctx->cq_ev_fd)
516 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -0600517}
518
519static void io_cqring_add_event(struct io_ring_ctx *ctx, u64 user_data,
Jens Axboec71ffb62019-05-13 20:58:29 -0600520 long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700521{
522 unsigned long flags;
523
524 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboec71ffb62019-05-13 20:58:29 -0600525 io_cqring_fill_event(ctx, user_data, res);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700526 io_commit_cqring(ctx);
527 spin_unlock_irqrestore(&ctx->completion_lock, flags);
528
Jens Axboe8c838782019-03-12 15:48:16 -0600529 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700530}
531
532static void io_ring_drop_ctx_refs(struct io_ring_ctx *ctx, unsigned refs)
533{
534 percpu_ref_put_many(&ctx->refs, refs);
535
536 if (waitqueue_active(&ctx->wait))
537 wake_up(&ctx->wait);
538}
539
Jens Axboe2579f912019-01-09 09:10:43 -0700540static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
541 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700542{
Jens Axboefd6fab22019-03-14 16:30:06 -0600543 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700544 struct io_kiocb *req;
545
546 if (!percpu_ref_tryget(&ctx->refs))
547 return NULL;
548
Jens Axboe2579f912019-01-09 09:10:43 -0700549 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -0600550 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -0700551 if (unlikely(!req))
552 goto out;
553 } else if (!state->free_reqs) {
554 size_t sz;
555 int ret;
556
557 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -0600558 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
559
560 /*
561 * Bulk alloc is all-or-nothing. If we fail to get a batch,
562 * retry single alloc to be on the safe side.
563 */
564 if (unlikely(ret <= 0)) {
565 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
566 if (!state->reqs[0])
567 goto out;
568 ret = 1;
569 }
Jens Axboe2579f912019-01-09 09:10:43 -0700570 state->free_reqs = ret - 1;
571 state->cur_req = 1;
572 req = state->reqs[0];
573 } else {
574 req = state->reqs[state->cur_req];
575 state->free_reqs--;
576 state->cur_req++;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700577 }
578
Jens Axboe60c112b2019-06-21 10:20:18 -0600579 req->file = NULL;
Jens Axboe2579f912019-01-09 09:10:43 -0700580 req->ctx = ctx;
581 req->flags = 0;
Jens Axboee65ef562019-03-12 10:16:44 -0600582 /* one is dropped after submission, the other at completion */
583 refcount_set(&req->refs, 2);
Jens Axboe9e645e112019-05-10 16:07:28 -0600584 req->result = 0;
Jens Axboe2579f912019-01-09 09:10:43 -0700585 return req;
586out:
Jens Axboe2b188cc2019-01-07 10:46:33 -0700587 io_ring_drop_ctx_refs(ctx, 1);
588 return NULL;
589}
590
Jens Axboedef596e2019-01-09 08:59:42 -0700591static void io_free_req_many(struct io_ring_ctx *ctx, void **reqs, int *nr)
592{
593 if (*nr) {
594 kmem_cache_free_bulk(req_cachep, *nr, reqs);
595 io_ring_drop_ctx_refs(ctx, *nr);
596 *nr = 0;
597 }
598}
599
Jens Axboe9e645e112019-05-10 16:07:28 -0600600static void __io_free_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700601{
Jens Axboe09bb8392019-03-13 12:39:28 -0600602 if (req->file && !(req->flags & REQ_F_FIXED_FILE))
603 fput(req->file);
Jens Axboee65ef562019-03-12 10:16:44 -0600604 io_ring_drop_ctx_refs(req->ctx, 1);
605 kmem_cache_free(req_cachep, req);
606}
607
Jens Axboe9e645e112019-05-10 16:07:28 -0600608static void io_req_link_next(struct io_kiocb *req)
609{
610 struct io_kiocb *nxt;
611
612 /*
613 * The list should never be empty when we are called here. But could
614 * potentially happen if the chain is messed up, check to be on the
615 * safe side.
616 */
617 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, list);
618 if (nxt) {
619 list_del(&nxt->list);
620 if (!list_empty(&req->link_list)) {
621 INIT_LIST_HEAD(&nxt->link_list);
622 list_splice(&req->link_list, &nxt->link_list);
623 nxt->flags |= REQ_F_LINK;
624 }
625
Zhengyuan Liuf7b76ac2019-07-16 23:26:14 +0800626 nxt->flags |= REQ_F_LINK_DONE;
Jens Axboe9e645e112019-05-10 16:07:28 -0600627 INIT_WORK(&nxt->work, io_sq_wq_submit_work);
Jens Axboe18d9be12019-09-10 09:13:05 -0600628 io_queue_async_work(req->ctx, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -0600629 }
630}
631
632/*
633 * Called if REQ_F_LINK is set, and we fail the head request
634 */
635static void io_fail_links(struct io_kiocb *req)
636{
637 struct io_kiocb *link;
638
639 while (!list_empty(&req->link_list)) {
640 link = list_first_entry(&req->link_list, struct io_kiocb, list);
641 list_del(&link->list);
642
643 io_cqring_add_event(req->ctx, link->user_data, -ECANCELED);
644 __io_free_req(link);
645 }
646}
647
648static void io_free_req(struct io_kiocb *req)
649{
650 /*
651 * If LINK is set, we have dependent requests in this chain. If we
652 * didn't fail this request, queue the first one up, moving any other
653 * dependencies to the next request. In case of failure, fail the rest
654 * of the chain.
655 */
656 if (req->flags & REQ_F_LINK) {
657 if (req->flags & REQ_F_FAIL_LINK)
658 io_fail_links(req);
659 else
660 io_req_link_next(req);
661 }
662
663 __io_free_req(req);
664}
665
Jens Axboee65ef562019-03-12 10:16:44 -0600666static void io_put_req(struct io_kiocb *req)
667{
668 if (refcount_dec_and_test(&req->refs))
669 io_free_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700670}
671
Hristo Venev75b28af2019-08-26 17:23:46 +0000672static unsigned io_cqring_events(struct io_rings *rings)
Jens Axboea3a0e432019-08-20 11:03:11 -0600673{
674 /* See comment at the top of this file */
675 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +0000676 return READ_ONCE(rings->cq.tail) - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -0600677}
678
Jens Axboedef596e2019-01-09 08:59:42 -0700679/*
680 * Find and free completed poll iocbs
681 */
682static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
683 struct list_head *done)
684{
685 void *reqs[IO_IOPOLL_BATCH];
686 struct io_kiocb *req;
Jens Axboe09bb8392019-03-13 12:39:28 -0600687 int to_free;
Jens Axboedef596e2019-01-09 08:59:42 -0700688
Jens Axboe09bb8392019-03-13 12:39:28 -0600689 to_free = 0;
Jens Axboedef596e2019-01-09 08:59:42 -0700690 while (!list_empty(done)) {
691 req = list_first_entry(done, struct io_kiocb, list);
692 list_del(&req->list);
693
Jens Axboe9e645e112019-05-10 16:07:28 -0600694 io_cqring_fill_event(ctx, req->user_data, req->result);
Jens Axboedef596e2019-01-09 08:59:42 -0700695 (*nr_events)++;
696
Jens Axboe09bb8392019-03-13 12:39:28 -0600697 if (refcount_dec_and_test(&req->refs)) {
698 /* If we're not using fixed files, we have to pair the
699 * completion part with the file put. Use regular
700 * completions for those, only batch free for fixed
Jens Axboe9e645e112019-05-10 16:07:28 -0600701 * file and non-linked commands.
Jens Axboe09bb8392019-03-13 12:39:28 -0600702 */
Jens Axboe9e645e112019-05-10 16:07:28 -0600703 if ((req->flags & (REQ_F_FIXED_FILE|REQ_F_LINK)) ==
704 REQ_F_FIXED_FILE) {
Jens Axboe09bb8392019-03-13 12:39:28 -0600705 reqs[to_free++] = req;
706 if (to_free == ARRAY_SIZE(reqs))
707 io_free_req_many(ctx, reqs, &to_free);
Jens Axboe6b063142019-01-10 22:13:58 -0700708 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -0600709 io_free_req(req);
Jens Axboe6b063142019-01-10 22:13:58 -0700710 }
Jens Axboe9a56a232019-01-09 09:06:50 -0700711 }
Jens Axboedef596e2019-01-09 08:59:42 -0700712 }
Jens Axboedef596e2019-01-09 08:59:42 -0700713
Jens Axboe09bb8392019-03-13 12:39:28 -0600714 io_commit_cqring(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -0700715 io_free_req_many(ctx, reqs, &to_free);
716}
717
718static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
719 long min)
720{
721 struct io_kiocb *req, *tmp;
722 LIST_HEAD(done);
723 bool spin;
724 int ret;
725
726 /*
727 * Only spin for completions if we don't have multiple devices hanging
728 * off our complete list, and we're under the requested amount.
729 */
730 spin = !ctx->poll_multi_file && *nr_events < min;
731
732 ret = 0;
733 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
734 struct kiocb *kiocb = &req->rw;
735
736 /*
737 * Move completed entries to our local list. If we find a
738 * request that requires polling, break out and complete
739 * the done list first, if we have entries there.
740 */
741 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
742 list_move_tail(&req->list, &done);
743 continue;
744 }
745 if (!list_empty(&done))
746 break;
747
748 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
749 if (ret < 0)
750 break;
751
752 if (ret && spin)
753 spin = false;
754 ret = 0;
755 }
756
757 if (!list_empty(&done))
758 io_iopoll_complete(ctx, nr_events, &done);
759
760 return ret;
761}
762
763/*
764 * Poll for a mininum of 'min' events. Note that if min == 0 we consider that a
765 * non-spinning poll check - we'll still enter the driver poll loop, but only
766 * as a non-spinning completion check.
767 */
768static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
769 long min)
770{
Jens Axboe08f54392019-08-21 22:19:11 -0600771 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -0700772 int ret;
773
774 ret = io_do_iopoll(ctx, nr_events, min);
775 if (ret < 0)
776 return ret;
777 if (!min || *nr_events >= min)
778 return 0;
779 }
780
781 return 1;
782}
783
784/*
785 * We can't just wait for polled events to come to us, we have to actively
786 * find and complete them.
787 */
788static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
789{
790 if (!(ctx->flags & IORING_SETUP_IOPOLL))
791 return;
792
793 mutex_lock(&ctx->uring_lock);
794 while (!list_empty(&ctx->poll_list)) {
795 unsigned int nr_events = 0;
796
797 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -0600798
799 /*
800 * Ensure we allow local-to-the-cpu processing to take place,
801 * in this case we need to ensure that we reap all events.
802 */
803 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -0700804 }
805 mutex_unlock(&ctx->uring_lock);
806}
807
808static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
809 long min)
810{
Jens Axboe500f9fb2019-08-19 12:15:59 -0600811 int iters, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -0700812
Jens Axboe500f9fb2019-08-19 12:15:59 -0600813 /*
814 * We disallow the app entering submit/complete with polling, but we
815 * still need to lock the ring to prevent racing with polled issue
816 * that got punted to a workqueue.
817 */
818 mutex_lock(&ctx->uring_lock);
819
820 iters = 0;
Jens Axboedef596e2019-01-09 08:59:42 -0700821 do {
822 int tmin = 0;
823
Jens Axboe500f9fb2019-08-19 12:15:59 -0600824 /*
Jens Axboea3a0e432019-08-20 11:03:11 -0600825 * Don't enter poll loop if we already have events pending.
826 * If we do, we can potentially be spinning for commands that
827 * already triggered a CQE (eg in error).
828 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000829 if (io_cqring_events(ctx->rings))
Jens Axboea3a0e432019-08-20 11:03:11 -0600830 break;
831
832 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -0600833 * If a submit got punted to a workqueue, we can have the
834 * application entering polling for a command before it gets
835 * issued. That app will hold the uring_lock for the duration
836 * of the poll right here, so we need to take a breather every
837 * now and then to ensure that the issue has a chance to add
838 * the poll to the issued list. Otherwise we can spin here
839 * forever, while the workqueue is stuck trying to acquire the
840 * very same mutex.
841 */
842 if (!(++iters & 7)) {
843 mutex_unlock(&ctx->uring_lock);
844 mutex_lock(&ctx->uring_lock);
845 }
846
Jens Axboedef596e2019-01-09 08:59:42 -0700847 if (*nr_events < min)
848 tmin = min - *nr_events;
849
850 ret = io_iopoll_getevents(ctx, nr_events, tmin);
851 if (ret <= 0)
852 break;
853 ret = 0;
854 } while (min && !*nr_events && !need_resched());
855
Jens Axboe500f9fb2019-08-19 12:15:59 -0600856 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -0700857 return ret;
858}
859
Jens Axboe2b188cc2019-01-07 10:46:33 -0700860static void kiocb_end_write(struct kiocb *kiocb)
861{
862 if (kiocb->ki_flags & IOCB_WRITE) {
863 struct inode *inode = file_inode(kiocb->ki_filp);
864
865 /*
866 * Tell lockdep we inherited freeze protection from submission
867 * thread.
868 */
869 if (S_ISREG(inode->i_mode))
870 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
871 file_end_write(kiocb->ki_filp);
872 }
873}
874
875static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
876{
877 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
878
879 kiocb_end_write(kiocb);
880
Jens Axboe9e645e112019-05-10 16:07:28 -0600881 if ((req->flags & REQ_F_LINK) && res != req->result)
882 req->flags |= REQ_F_FAIL_LINK;
Jens Axboec71ffb62019-05-13 20:58:29 -0600883 io_cqring_add_event(req->ctx, req->user_data, res);
Jens Axboee65ef562019-03-12 10:16:44 -0600884 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700885}
886
Jens Axboedef596e2019-01-09 08:59:42 -0700887static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
888{
889 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
890
891 kiocb_end_write(kiocb);
892
Jens Axboe9e645e112019-05-10 16:07:28 -0600893 if ((req->flags & REQ_F_LINK) && res != req->result)
894 req->flags |= REQ_F_FAIL_LINK;
895 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -0700896 if (res != -EAGAIN)
897 req->flags |= REQ_F_IOPOLL_COMPLETED;
898}
899
900/*
901 * After the iocb has been issued, it's safe to be found on the poll list.
902 * Adding the kiocb to the list AFTER submission ensures that we don't
903 * find it from a io_iopoll_getevents() thread before the issuer is done
904 * accessing the kiocb cookie.
905 */
906static void io_iopoll_req_issued(struct io_kiocb *req)
907{
908 struct io_ring_ctx *ctx = req->ctx;
909
910 /*
911 * Track whether we have multiple files in our lists. This will impact
912 * how we do polling eventually, not spinning if we're on potentially
913 * different devices.
914 */
915 if (list_empty(&ctx->poll_list)) {
916 ctx->poll_multi_file = false;
917 } else if (!ctx->poll_multi_file) {
918 struct io_kiocb *list_req;
919
920 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
921 list);
922 if (list_req->rw.ki_filp != req->rw.ki_filp)
923 ctx->poll_multi_file = true;
924 }
925
926 /*
927 * For fast devices, IO may have already completed. If it has, add
928 * it to the front so we find it first.
929 */
930 if (req->flags & REQ_F_IOPOLL_COMPLETED)
931 list_add(&req->list, &ctx->poll_list);
932 else
933 list_add_tail(&req->list, &ctx->poll_list);
934}
935
Jens Axboe3d6770f2019-04-13 11:50:54 -0600936static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -0700937{
Jens Axboe3d6770f2019-04-13 11:50:54 -0600938 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -0700939 int diff = state->has_refs - state->used_refs;
940
941 if (diff)
942 fput_many(state->file, diff);
943 state->file = NULL;
944 }
945}
946
947/*
948 * Get as many references to a file as we have IOs left in this submission,
949 * assuming most submissions are for one file, or at least that each file
950 * has more than one submission.
951 */
952static struct file *io_file_get(struct io_submit_state *state, int fd)
953{
954 if (!state)
955 return fget(fd);
956
957 if (state->file) {
958 if (state->fd == fd) {
959 state->used_refs++;
960 state->ios_left--;
961 return state->file;
962 }
Jens Axboe3d6770f2019-04-13 11:50:54 -0600963 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -0700964 }
965 state->file = fget_many(fd, state->ios_left);
966 if (!state->file)
967 return NULL;
968
969 state->fd = fd;
970 state->has_refs = state->ios_left;
971 state->used_refs = 1;
972 state->ios_left--;
973 return state->file;
974}
975
Jens Axboe2b188cc2019-01-07 10:46:33 -0700976/*
977 * If we tracked the file through the SCM inflight mechanism, we could support
978 * any file. For now, just ensure that anything potentially problematic is done
979 * inline.
980 */
981static bool io_file_supports_async(struct file *file)
982{
983 umode_t mode = file_inode(file)->i_mode;
984
985 if (S_ISBLK(mode) || S_ISCHR(mode))
986 return true;
987 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
988 return true;
989
990 return false;
991}
992
Jens Axboe6c271ce2019-01-10 11:22:30 -0700993static int io_prep_rw(struct io_kiocb *req, const struct sqe_submit *s,
Jens Axboe8358e3a2019-04-23 08:17:58 -0600994 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700995{
Jens Axboe6c271ce2019-01-10 11:22:30 -0700996 const struct io_uring_sqe *sqe = s->sqe;
Jens Axboedef596e2019-01-09 08:59:42 -0700997 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700998 struct kiocb *kiocb = &req->rw;
Jens Axboe09bb8392019-03-13 12:39:28 -0600999 unsigned ioprio;
1000 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001001
Jens Axboe09bb8392019-03-13 12:39:28 -06001002 if (!req->file)
1003 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001004
Jens Axboe09bb8392019-03-13 12:39:28 -06001005 if (force_nonblock && !io_file_supports_async(req->file))
1006 force_nonblock = false;
Jens Axboe6b063142019-01-10 22:13:58 -07001007
Jens Axboe2b188cc2019-01-07 10:46:33 -07001008 kiocb->ki_pos = READ_ONCE(sqe->off);
1009 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
1010 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
1011
1012 ioprio = READ_ONCE(sqe->ioprio);
1013 if (ioprio) {
1014 ret = ioprio_check_cap(ioprio);
1015 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06001016 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001017
1018 kiocb->ki_ioprio = ioprio;
1019 } else
1020 kiocb->ki_ioprio = get_current_ioprio();
1021
1022 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
1023 if (unlikely(ret))
Jens Axboe09bb8392019-03-13 12:39:28 -06001024 return ret;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001025
1026 /* don't allow async punt if RWF_NOWAIT was requested */
1027 if (kiocb->ki_flags & IOCB_NOWAIT)
1028 req->flags |= REQ_F_NOWAIT;
1029
1030 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001031 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001032
Jens Axboedef596e2019-01-09 08:59:42 -07001033 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07001034 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
1035 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06001036 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001037
Jens Axboedef596e2019-01-09 08:59:42 -07001038 kiocb->ki_flags |= IOCB_HIPRI;
1039 kiocb->ki_complete = io_complete_rw_iopoll;
1040 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001041 if (kiocb->ki_flags & IOCB_HIPRI)
1042 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07001043 kiocb->ki_complete = io_complete_rw;
1044 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001045 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001046}
1047
1048static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
1049{
1050 switch (ret) {
1051 case -EIOCBQUEUED:
1052 break;
1053 case -ERESTARTSYS:
1054 case -ERESTARTNOINTR:
1055 case -ERESTARTNOHAND:
1056 case -ERESTART_RESTARTBLOCK:
1057 /*
1058 * We can't just restart the syscall, since previously
1059 * submitted sqes may already be in progress. Just fail this
1060 * IO with EINTR.
1061 */
1062 ret = -EINTR;
1063 /* fall through */
1064 default:
1065 kiocb->ki_complete(kiocb, ret, 0);
1066 }
1067}
1068
Jens Axboeedafcce2019-01-09 09:16:05 -07001069static int io_import_fixed(struct io_ring_ctx *ctx, int rw,
1070 const struct io_uring_sqe *sqe,
1071 struct iov_iter *iter)
1072{
1073 size_t len = READ_ONCE(sqe->len);
1074 struct io_mapped_ubuf *imu;
1075 unsigned index, buf_index;
1076 size_t offset;
1077 u64 buf_addr;
1078
1079 /* attempt to use fixed buffers without having provided iovecs */
1080 if (unlikely(!ctx->user_bufs))
1081 return -EFAULT;
1082
1083 buf_index = READ_ONCE(sqe->buf_index);
1084 if (unlikely(buf_index >= ctx->nr_user_bufs))
1085 return -EFAULT;
1086
1087 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
1088 imu = &ctx->user_bufs[index];
1089 buf_addr = READ_ONCE(sqe->addr);
1090
1091 /* overflow */
1092 if (buf_addr + len < buf_addr)
1093 return -EFAULT;
1094 /* not inside the mapped region */
1095 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
1096 return -EFAULT;
1097
1098 /*
1099 * May not be a start of buffer, set size appropriately
1100 * and advance us to the beginning.
1101 */
1102 offset = buf_addr - imu->ubuf;
1103 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06001104
1105 if (offset) {
1106 /*
1107 * Don't use iov_iter_advance() here, as it's really slow for
1108 * using the latter parts of a big fixed buffer - it iterates
1109 * over each segment manually. We can cheat a bit here, because
1110 * we know that:
1111 *
1112 * 1) it's a BVEC iter, we set it up
1113 * 2) all bvecs are PAGE_SIZE in size, except potentially the
1114 * first and last bvec
1115 *
1116 * So just find our index, and adjust the iterator afterwards.
1117 * If the offset is within the first bvec (or the whole first
1118 * bvec, just use iov_iter_advance(). This makes it easier
1119 * since we can just skip the first segment, which may not
1120 * be PAGE_SIZE aligned.
1121 */
1122 const struct bio_vec *bvec = imu->bvec;
1123
1124 if (offset <= bvec->bv_len) {
1125 iov_iter_advance(iter, offset);
1126 } else {
1127 unsigned long seg_skip;
1128
1129 /* skip first vec */
1130 offset -= bvec->bv_len;
1131 seg_skip = 1 + (offset >> PAGE_SHIFT);
1132
1133 iter->bvec = bvec + seg_skip;
1134 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02001135 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001136 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001137 }
1138 }
1139
Jens Axboeedafcce2019-01-09 09:16:05 -07001140 return 0;
1141}
1142
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001143static ssize_t io_import_iovec(struct io_ring_ctx *ctx, int rw,
1144 const struct sqe_submit *s, struct iovec **iovec,
1145 struct iov_iter *iter)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001146{
1147 const struct io_uring_sqe *sqe = s->sqe;
1148 void __user *buf = u64_to_user_ptr(READ_ONCE(sqe->addr));
1149 size_t sqe_len = READ_ONCE(sqe->len);
Jens Axboeedafcce2019-01-09 09:16:05 -07001150 u8 opcode;
1151
1152 /*
1153 * We're reading ->opcode for the second time, but the first read
1154 * doesn't care whether it's _FIXED or not, so it doesn't matter
1155 * whether ->opcode changes concurrently. The first read does care
1156 * about whether it is a READ or a WRITE, so we don't trust this read
1157 * for that purpose and instead let the caller pass in the read/write
1158 * flag.
1159 */
1160 opcode = READ_ONCE(sqe->opcode);
1161 if (opcode == IORING_OP_READ_FIXED ||
1162 opcode == IORING_OP_WRITE_FIXED) {
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001163 ssize_t ret = io_import_fixed(ctx, rw, sqe, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07001164 *iovec = NULL;
1165 return ret;
1166 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001167
1168 if (!s->has_user)
1169 return -EFAULT;
1170
1171#ifdef CONFIG_COMPAT
1172 if (ctx->compat)
1173 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
1174 iovec, iter);
1175#endif
1176
1177 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
1178}
1179
Jens Axboe31b51512019-01-18 22:56:34 -07001180/*
1181 * Make a note of the last file/offset/direction we punted to async
1182 * context. We'll use this information to see if we can piggy back a
1183 * sequential request onto the previous one, if it's still hasn't been
1184 * completed by the async worker.
1185 */
1186static void io_async_list_note(int rw, struct io_kiocb *req, size_t len)
1187{
1188 struct async_list *async_list = &req->ctx->pending_async[rw];
1189 struct kiocb *kiocb = &req->rw;
1190 struct file *filp = kiocb->ki_filp;
1191 off_t io_end = kiocb->ki_pos + len;
1192
1193 if (filp == async_list->file && kiocb->ki_pos == async_list->io_end) {
Zhengyuan Liu9310a7ba2019-07-22 10:23:27 +08001194 unsigned long max_bytes;
Jens Axboe31b51512019-01-18 22:56:34 -07001195
1196 /* Use 8x RA size as a decent limiter for both reads/writes */
Zhengyuan Liu9310a7ba2019-07-22 10:23:27 +08001197 max_bytes = filp->f_ra.ra_pages << (PAGE_SHIFT + 3);
1198 if (!max_bytes)
1199 max_bytes = VM_READAHEAD_PAGES << (PAGE_SHIFT + 3);
Jens Axboe31b51512019-01-18 22:56:34 -07001200
Zhengyuan Liu9310a7ba2019-07-22 10:23:27 +08001201 /* If max len are exceeded, reset the state */
1202 if (async_list->io_len + len <= max_bytes) {
Jens Axboe31b51512019-01-18 22:56:34 -07001203 req->flags |= REQ_F_SEQ_PREV;
Zhengyuan Liu9310a7ba2019-07-22 10:23:27 +08001204 async_list->io_len += len;
Jens Axboe31b51512019-01-18 22:56:34 -07001205 } else {
1206 io_end = 0;
Zhengyuan Liu9310a7ba2019-07-22 10:23:27 +08001207 async_list->io_len = 0;
Jens Axboe31b51512019-01-18 22:56:34 -07001208 }
1209 }
1210
1211 /* New file? Reset state. */
1212 if (async_list->file != filp) {
Zhengyuan Liu9310a7ba2019-07-22 10:23:27 +08001213 async_list->io_len = 0;
Jens Axboe31b51512019-01-18 22:56:34 -07001214 async_list->file = filp;
1215 }
1216 async_list->io_end = io_end;
1217}
1218
Jens Axboee0c5c572019-03-12 10:18:47 -06001219static int io_read(struct io_kiocb *req, const struct sqe_submit *s,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001220 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001221{
1222 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1223 struct kiocb *kiocb = &req->rw;
1224 struct iov_iter iter;
1225 struct file *file;
Jens Axboe31b51512019-01-18 22:56:34 -07001226 size_t iov_count;
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001227 ssize_t read_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001228
Jens Axboe8358e3a2019-04-23 08:17:58 -06001229 ret = io_prep_rw(req, s, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001230 if (ret)
1231 return ret;
1232 file = kiocb->ki_filp;
1233
Jens Axboe2b188cc2019-01-07 10:46:33 -07001234 if (unlikely(!(file->f_mode & FMODE_READ)))
Jens Axboe09bb8392019-03-13 12:39:28 -06001235 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001236 if (unlikely(!file->f_op->read_iter))
Jens Axboe09bb8392019-03-13 12:39:28 -06001237 return -EINVAL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001238
1239 ret = io_import_iovec(req->ctx, READ, s, &iovec, &iter);
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001240 if (ret < 0)
Jens Axboe09bb8392019-03-13 12:39:28 -06001241 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001242
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001243 read_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06001244 if (req->flags & REQ_F_LINK)
1245 req->result = read_size;
1246
Jens Axboe31b51512019-01-18 22:56:34 -07001247 iov_count = iov_iter_count(&iter);
1248 ret = rw_verify_area(READ, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001249 if (!ret) {
1250 ssize_t ret2;
1251
Jens Axboe2b188cc2019-01-07 10:46:33 -07001252 ret2 = call_read_iter(file, kiocb, &iter);
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001253 /*
1254 * In case of a short read, punt to async. This can happen
1255 * if we have data partially cached. Alternatively we can
1256 * return the short read, in which case the application will
1257 * need to issue another SQE and wait for it. That SQE will
1258 * need async punt anyway, so it's more efficient to do it
1259 * here.
1260 */
1261 if (force_nonblock && ret2 > 0 && ret2 < read_size)
1262 ret2 = -EAGAIN;
1263 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboe31b51512019-01-18 22:56:34 -07001264 if (!force_nonblock || ret2 != -EAGAIN) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001265 io_rw_done(kiocb, ret2);
Jens Axboe31b51512019-01-18 22:56:34 -07001266 } else {
1267 /*
1268 * If ->needs_lock is true, we're already in async
1269 * context.
1270 */
1271 if (!s->needs_lock)
1272 io_async_list_note(READ, req, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001273 ret = -EAGAIN;
Jens Axboe31b51512019-01-18 22:56:34 -07001274 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001275 }
1276 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001277 return ret;
1278}
1279
Jens Axboee0c5c572019-03-12 10:18:47 -06001280static int io_write(struct io_kiocb *req, const struct sqe_submit *s,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001281 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001282{
1283 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1284 struct kiocb *kiocb = &req->rw;
1285 struct iov_iter iter;
1286 struct file *file;
Jens Axboe31b51512019-01-18 22:56:34 -07001287 size_t iov_count;
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001288 ssize_t ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001289
Jens Axboe8358e3a2019-04-23 08:17:58 -06001290 ret = io_prep_rw(req, s, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001291 if (ret)
1292 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001293
Jens Axboe2b188cc2019-01-07 10:46:33 -07001294 file = kiocb->ki_filp;
1295 if (unlikely(!(file->f_mode & FMODE_WRITE)))
Jens Axboe09bb8392019-03-13 12:39:28 -06001296 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001297 if (unlikely(!file->f_op->write_iter))
Jens Axboe09bb8392019-03-13 12:39:28 -06001298 return -EINVAL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001299
1300 ret = io_import_iovec(req->ctx, WRITE, s, &iovec, &iter);
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001301 if (ret < 0)
Jens Axboe09bb8392019-03-13 12:39:28 -06001302 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001303
Jens Axboe9e645e112019-05-10 16:07:28 -06001304 if (req->flags & REQ_F_LINK)
1305 req->result = ret;
1306
Jens Axboe31b51512019-01-18 22:56:34 -07001307 iov_count = iov_iter_count(&iter);
1308
1309 ret = -EAGAIN;
1310 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT)) {
1311 /* If ->needs_lock is true, we're already in async context. */
1312 if (!s->needs_lock)
1313 io_async_list_note(WRITE, req, iov_count);
1314 goto out_free;
1315 }
1316
1317 ret = rw_verify_area(WRITE, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001318 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01001319 ssize_t ret2;
1320
Jens Axboe2b188cc2019-01-07 10:46:33 -07001321 /*
1322 * Open-code file_start_write here to grab freeze protection,
1323 * which will be released by another thread in
1324 * io_complete_rw(). Fool lockdep by telling it the lock got
1325 * released so that it doesn't complain about the held lock when
1326 * we return to userspace.
1327 */
1328 if (S_ISREG(file_inode(file)->i_mode)) {
1329 __sb_start_write(file_inode(file)->i_sb,
1330 SB_FREEZE_WRITE, true);
1331 __sb_writers_release(file_inode(file)->i_sb,
1332 SB_FREEZE_WRITE);
1333 }
1334 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01001335
1336 ret2 = call_write_iter(file, kiocb, &iter);
1337 if (!force_nonblock || ret2 != -EAGAIN) {
1338 io_rw_done(kiocb, ret2);
1339 } else {
1340 /*
1341 * If ->needs_lock is true, we're already in async
1342 * context.
1343 */
1344 if (!s->needs_lock)
1345 io_async_list_note(WRITE, req, iov_count);
1346 ret = -EAGAIN;
1347 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001348 }
Jens Axboe31b51512019-01-18 22:56:34 -07001349out_free:
Jens Axboe2b188cc2019-01-07 10:46:33 -07001350 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001351 return ret;
1352}
1353
1354/*
1355 * IORING_OP_NOP just posts a completion event, nothing else.
1356 */
1357static int io_nop(struct io_kiocb *req, u64 user_data)
1358{
1359 struct io_ring_ctx *ctx = req->ctx;
1360 long err = 0;
1361
Jens Axboedef596e2019-01-09 08:59:42 -07001362 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1363 return -EINVAL;
1364
Jens Axboec71ffb62019-05-13 20:58:29 -06001365 io_cqring_add_event(ctx, user_data, err);
Jens Axboee65ef562019-03-12 10:16:44 -06001366 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001367 return 0;
1368}
1369
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001370static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1371{
Jens Axboe6b063142019-01-10 22:13:58 -07001372 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001373
Jens Axboe09bb8392019-03-13 12:39:28 -06001374 if (!req->file)
1375 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001376
Jens Axboe6b063142019-01-10 22:13:58 -07001377 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07001378 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07001379 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001380 return -EINVAL;
1381
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001382 return 0;
1383}
1384
1385static int io_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1386 bool force_nonblock)
1387{
1388 loff_t sqe_off = READ_ONCE(sqe->off);
1389 loff_t sqe_len = READ_ONCE(sqe->len);
1390 loff_t end = sqe_off + sqe_len;
1391 unsigned fsync_flags;
1392 int ret;
1393
1394 fsync_flags = READ_ONCE(sqe->fsync_flags);
1395 if (unlikely(fsync_flags & ~IORING_FSYNC_DATASYNC))
1396 return -EINVAL;
1397
1398 ret = io_prep_fsync(req, sqe);
1399 if (ret)
1400 return ret;
1401
1402 /* fsync always requires a blocking context */
1403 if (force_nonblock)
1404 return -EAGAIN;
1405
1406 ret = vfs_fsync_range(req->rw.ki_filp, sqe_off,
1407 end > 0 ? end : LLONG_MAX,
1408 fsync_flags & IORING_FSYNC_DATASYNC);
1409
Jens Axboe9e645e112019-05-10 16:07:28 -06001410 if (ret < 0 && (req->flags & REQ_F_LINK))
1411 req->flags |= REQ_F_FAIL_LINK;
Jens Axboec71ffb62019-05-13 20:58:29 -06001412 io_cqring_add_event(req->ctx, sqe->user_data, ret);
Jens Axboee65ef562019-03-12 10:16:44 -06001413 io_put_req(req);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001414 return 0;
1415}
1416
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001417static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1418{
1419 struct io_ring_ctx *ctx = req->ctx;
1420 int ret = 0;
1421
1422 if (!req->file)
1423 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001424
1425 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1426 return -EINVAL;
1427 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
1428 return -EINVAL;
1429
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001430 return ret;
1431}
1432
1433static int io_sync_file_range(struct io_kiocb *req,
1434 const struct io_uring_sqe *sqe,
1435 bool force_nonblock)
1436{
1437 loff_t sqe_off;
1438 loff_t sqe_len;
1439 unsigned flags;
1440 int ret;
1441
1442 ret = io_prep_sfr(req, sqe);
1443 if (ret)
1444 return ret;
1445
1446 /* sync_file_range always requires a blocking context */
1447 if (force_nonblock)
1448 return -EAGAIN;
1449
1450 sqe_off = READ_ONCE(sqe->off);
1451 sqe_len = READ_ONCE(sqe->len);
1452 flags = READ_ONCE(sqe->sync_range_flags);
1453
1454 ret = sync_file_range(req->rw.ki_filp, sqe_off, sqe_len, flags);
1455
Jens Axboe9e645e112019-05-10 16:07:28 -06001456 if (ret < 0 && (req->flags & REQ_F_LINK))
1457 req->flags |= REQ_F_FAIL_LINK;
Jens Axboec71ffb62019-05-13 20:58:29 -06001458 io_cqring_add_event(req->ctx, sqe->user_data, ret);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001459 io_put_req(req);
1460 return 0;
1461}
1462
Jens Axboe0fa03c62019-04-19 13:34:07 -06001463#if defined(CONFIG_NET)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001464static int io_send_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1465 bool force_nonblock,
1466 long (*fn)(struct socket *, struct user_msghdr __user *,
1467 unsigned int))
1468{
Jens Axboe0fa03c62019-04-19 13:34:07 -06001469 struct socket *sock;
1470 int ret;
1471
1472 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1473 return -EINVAL;
1474
1475 sock = sock_from_file(req->file, &ret);
1476 if (sock) {
1477 struct user_msghdr __user *msg;
1478 unsigned flags;
1479
1480 flags = READ_ONCE(sqe->msg_flags);
1481 if (flags & MSG_DONTWAIT)
1482 req->flags |= REQ_F_NOWAIT;
1483 else if (force_nonblock)
1484 flags |= MSG_DONTWAIT;
1485
1486 msg = (struct user_msghdr __user *) (unsigned long)
1487 READ_ONCE(sqe->addr);
1488
Jens Axboeaa1fa282019-04-19 13:38:09 -06001489 ret = fn(sock, msg, flags);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001490 if (force_nonblock && ret == -EAGAIN)
1491 return ret;
1492 }
1493
1494 io_cqring_add_event(req->ctx, sqe->user_data, ret);
1495 io_put_req(req);
1496 return 0;
Jens Axboeaa1fa282019-04-19 13:38:09 -06001497}
1498#endif
1499
1500static int io_sendmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1501 bool force_nonblock)
1502{
1503#if defined(CONFIG_NET)
1504 return io_send_recvmsg(req, sqe, force_nonblock, __sys_sendmsg_sock);
1505#else
1506 return -EOPNOTSUPP;
1507#endif
1508}
1509
1510static int io_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1511 bool force_nonblock)
1512{
1513#if defined(CONFIG_NET)
1514 return io_send_recvmsg(req, sqe, force_nonblock, __sys_recvmsg_sock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001515#else
1516 return -EOPNOTSUPP;
1517#endif
1518}
1519
Jens Axboe221c5eb2019-01-17 09:41:58 -07001520static void io_poll_remove_one(struct io_kiocb *req)
1521{
1522 struct io_poll_iocb *poll = &req->poll;
1523
1524 spin_lock(&poll->head->lock);
1525 WRITE_ONCE(poll->canceled, true);
1526 if (!list_empty(&poll->wait.entry)) {
1527 list_del_init(&poll->wait.entry);
Jens Axboe18d9be12019-09-10 09:13:05 -06001528 io_queue_async_work(req->ctx, req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001529 }
1530 spin_unlock(&poll->head->lock);
1531
1532 list_del_init(&req->list);
1533}
1534
1535static void io_poll_remove_all(struct io_ring_ctx *ctx)
1536{
1537 struct io_kiocb *req;
1538
1539 spin_lock_irq(&ctx->completion_lock);
1540 while (!list_empty(&ctx->cancel_list)) {
1541 req = list_first_entry(&ctx->cancel_list, struct io_kiocb,list);
1542 io_poll_remove_one(req);
1543 }
1544 spin_unlock_irq(&ctx->completion_lock);
1545}
1546
1547/*
1548 * Find a running poll command that matches one specified in sqe->addr,
1549 * and remove it if found.
1550 */
1551static int io_poll_remove(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1552{
1553 struct io_ring_ctx *ctx = req->ctx;
1554 struct io_kiocb *poll_req, *next;
1555 int ret = -ENOENT;
1556
1557 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1558 return -EINVAL;
1559 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
1560 sqe->poll_events)
1561 return -EINVAL;
1562
1563 spin_lock_irq(&ctx->completion_lock);
1564 list_for_each_entry_safe(poll_req, next, &ctx->cancel_list, list) {
1565 if (READ_ONCE(sqe->addr) == poll_req->user_data) {
1566 io_poll_remove_one(poll_req);
1567 ret = 0;
1568 break;
1569 }
1570 }
1571 spin_unlock_irq(&ctx->completion_lock);
1572
Jens Axboec71ffb62019-05-13 20:58:29 -06001573 io_cqring_add_event(req->ctx, sqe->user_data, ret);
Jens Axboee65ef562019-03-12 10:16:44 -06001574 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001575 return 0;
1576}
1577
Jens Axboe8c838782019-03-12 15:48:16 -06001578static void io_poll_complete(struct io_ring_ctx *ctx, struct io_kiocb *req,
1579 __poll_t mask)
Jens Axboe221c5eb2019-01-17 09:41:58 -07001580{
Jens Axboe8c838782019-03-12 15:48:16 -06001581 req->poll.done = true;
Jens Axboec71ffb62019-05-13 20:58:29 -06001582 io_cqring_fill_event(ctx, req->user_data, mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06001583 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001584}
1585
1586static void io_poll_complete_work(struct work_struct *work)
1587{
1588 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
1589 struct io_poll_iocb *poll = &req->poll;
1590 struct poll_table_struct pt = { ._key = poll->events };
1591 struct io_ring_ctx *ctx = req->ctx;
1592 __poll_t mask = 0;
1593
1594 if (!READ_ONCE(poll->canceled))
1595 mask = vfs_poll(poll->file, &pt) & poll->events;
1596
1597 /*
1598 * Note that ->ki_cancel callers also delete iocb from active_reqs after
1599 * calling ->ki_cancel. We need the ctx_lock roundtrip here to
1600 * synchronize with them. In the cancellation case the list_del_init
1601 * itself is not actually needed, but harmless so we keep it in to
1602 * avoid further branches in the fast path.
1603 */
1604 spin_lock_irq(&ctx->completion_lock);
1605 if (!mask && !READ_ONCE(poll->canceled)) {
1606 add_wait_queue(poll->head, &poll->wait);
1607 spin_unlock_irq(&ctx->completion_lock);
1608 return;
1609 }
1610 list_del_init(&req->list);
Jens Axboe8c838782019-03-12 15:48:16 -06001611 io_poll_complete(ctx, req, mask);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001612 spin_unlock_irq(&ctx->completion_lock);
1613
Jens Axboe8c838782019-03-12 15:48:16 -06001614 io_cqring_ev_posted(ctx);
1615 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001616}
1617
1618static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
1619 void *key)
1620{
1621 struct io_poll_iocb *poll = container_of(wait, struct io_poll_iocb,
1622 wait);
1623 struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
1624 struct io_ring_ctx *ctx = req->ctx;
1625 __poll_t mask = key_to_poll(key);
Jens Axboe8c838782019-03-12 15:48:16 -06001626 unsigned long flags;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001627
1628 /* for instances that support it check for an event match first: */
Jens Axboe8c838782019-03-12 15:48:16 -06001629 if (mask && !(mask & poll->events))
1630 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001631
1632 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06001633
1634 if (mask && spin_trylock_irqsave(&ctx->completion_lock, flags)) {
1635 list_del(&req->list);
1636 io_poll_complete(ctx, req, mask);
1637 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1638
1639 io_cqring_ev_posted(ctx);
1640 io_put_req(req);
1641 } else {
Jens Axboe18d9be12019-09-10 09:13:05 -06001642 io_queue_async_work(ctx, req);
Jens Axboe8c838782019-03-12 15:48:16 -06001643 }
1644
Jens Axboe221c5eb2019-01-17 09:41:58 -07001645 return 1;
1646}
1647
1648struct io_poll_table {
1649 struct poll_table_struct pt;
1650 struct io_kiocb *req;
1651 int error;
1652};
1653
1654static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
1655 struct poll_table_struct *p)
1656{
1657 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
1658
1659 if (unlikely(pt->req->poll.head)) {
1660 pt->error = -EINVAL;
1661 return;
1662 }
1663
1664 pt->error = 0;
1665 pt->req->poll.head = head;
1666 add_wait_queue(head, &pt->req->poll.wait);
1667}
1668
1669static int io_poll_add(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1670{
1671 struct io_poll_iocb *poll = &req->poll;
1672 struct io_ring_ctx *ctx = req->ctx;
1673 struct io_poll_table ipt;
Jens Axboe8c838782019-03-12 15:48:16 -06001674 bool cancel = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001675 __poll_t mask;
1676 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001677
1678 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1679 return -EINVAL;
1680 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
1681 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06001682 if (!poll->file)
1683 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001684
1685 INIT_WORK(&req->work, io_poll_complete_work);
1686 events = READ_ONCE(sqe->poll_events);
1687 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
1688
Jens Axboe221c5eb2019-01-17 09:41:58 -07001689 poll->head = NULL;
Jens Axboe8c838782019-03-12 15:48:16 -06001690 poll->done = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001691 poll->canceled = false;
1692
1693 ipt.pt._qproc = io_poll_queue_proc;
1694 ipt.pt._key = poll->events;
1695 ipt.req = req;
1696 ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
1697
1698 /* initialized the list so that we can do list_empty checks */
1699 INIT_LIST_HEAD(&poll->wait.entry);
1700 init_waitqueue_func_entry(&poll->wait, io_poll_wake);
1701
Jens Axboe36703242019-07-25 10:20:18 -06001702 INIT_LIST_HEAD(&req->list);
1703
Jens Axboe221c5eb2019-01-17 09:41:58 -07001704 mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001705
1706 spin_lock_irq(&ctx->completion_lock);
Jens Axboe8c838782019-03-12 15:48:16 -06001707 if (likely(poll->head)) {
1708 spin_lock(&poll->head->lock);
1709 if (unlikely(list_empty(&poll->wait.entry))) {
1710 if (ipt.error)
1711 cancel = true;
1712 ipt.error = 0;
1713 mask = 0;
1714 }
1715 if (mask || ipt.error)
1716 list_del_init(&poll->wait.entry);
1717 else if (cancel)
1718 WRITE_ONCE(poll->canceled, true);
1719 else if (!poll->done) /* actually waiting for an event */
1720 list_add_tail(&req->list, &ctx->cancel_list);
1721 spin_unlock(&poll->head->lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001722 }
Jens Axboe8c838782019-03-12 15:48:16 -06001723 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06001724 ipt.error = 0;
1725 io_poll_complete(ctx, req, mask);
1726 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07001727 spin_unlock_irq(&ctx->completion_lock);
1728
Jens Axboe8c838782019-03-12 15:48:16 -06001729 if (mask) {
1730 io_cqring_ev_posted(ctx);
Jens Axboee65ef562019-03-12 10:16:44 -06001731 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001732 }
Jens Axboe8c838782019-03-12 15:48:16 -06001733 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001734}
1735
Jens Axboede0617e2019-04-06 21:51:27 -06001736static int io_req_defer(struct io_ring_ctx *ctx, struct io_kiocb *req,
1737 const struct io_uring_sqe *sqe)
1738{
1739 struct io_uring_sqe *sqe_copy;
1740
1741 if (!io_sequence_defer(ctx, req) && list_empty(&ctx->defer_list))
1742 return 0;
1743
1744 sqe_copy = kmalloc(sizeof(*sqe_copy), GFP_KERNEL);
1745 if (!sqe_copy)
1746 return -EAGAIN;
1747
1748 spin_lock_irq(&ctx->completion_lock);
1749 if (!io_sequence_defer(ctx, req) && list_empty(&ctx->defer_list)) {
1750 spin_unlock_irq(&ctx->completion_lock);
1751 kfree(sqe_copy);
1752 return 0;
1753 }
1754
1755 memcpy(sqe_copy, sqe, sizeof(*sqe_copy));
1756 req->submit.sqe = sqe_copy;
1757
1758 INIT_WORK(&req->work, io_sq_wq_submit_work);
1759 list_add_tail(&req->list, &ctx->defer_list);
1760 spin_unlock_irq(&ctx->completion_lock);
1761 return -EIOCBQUEUED;
1762}
1763
Jens Axboe2b188cc2019-01-07 10:46:33 -07001764static int __io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001765 const struct sqe_submit *s, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001766{
Jens Axboee0c5c572019-03-12 10:18:47 -06001767 int ret, opcode;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001768
Jens Axboe9e645e112019-05-10 16:07:28 -06001769 req->user_data = READ_ONCE(s->sqe->user_data);
1770
Jens Axboe2b188cc2019-01-07 10:46:33 -07001771 if (unlikely(s->index >= ctx->sq_entries))
1772 return -EINVAL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001773
1774 opcode = READ_ONCE(s->sqe->opcode);
1775 switch (opcode) {
1776 case IORING_OP_NOP:
1777 ret = io_nop(req, req->user_data);
1778 break;
1779 case IORING_OP_READV:
Jens Axboeedafcce2019-01-09 09:16:05 -07001780 if (unlikely(s->sqe->buf_index))
1781 return -EINVAL;
Jens Axboe8358e3a2019-04-23 08:17:58 -06001782 ret = io_read(req, s, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001783 break;
1784 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07001785 if (unlikely(s->sqe->buf_index))
1786 return -EINVAL;
Jens Axboe8358e3a2019-04-23 08:17:58 -06001787 ret = io_write(req, s, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07001788 break;
1789 case IORING_OP_READ_FIXED:
Jens Axboe8358e3a2019-04-23 08:17:58 -06001790 ret = io_read(req, s, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07001791 break;
1792 case IORING_OP_WRITE_FIXED:
Jens Axboe8358e3a2019-04-23 08:17:58 -06001793 ret = io_write(req, s, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001794 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001795 case IORING_OP_FSYNC:
1796 ret = io_fsync(req, s->sqe, force_nonblock);
1797 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001798 case IORING_OP_POLL_ADD:
1799 ret = io_poll_add(req, s->sqe);
1800 break;
1801 case IORING_OP_POLL_REMOVE:
1802 ret = io_poll_remove(req, s->sqe);
1803 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001804 case IORING_OP_SYNC_FILE_RANGE:
1805 ret = io_sync_file_range(req, s->sqe, force_nonblock);
1806 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06001807 case IORING_OP_SENDMSG:
1808 ret = io_sendmsg(req, s->sqe, force_nonblock);
1809 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06001810 case IORING_OP_RECVMSG:
1811 ret = io_recvmsg(req, s->sqe, force_nonblock);
1812 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001813 default:
1814 ret = -EINVAL;
1815 break;
1816 }
1817
Jens Axboedef596e2019-01-09 08:59:42 -07001818 if (ret)
1819 return ret;
1820
1821 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe9e645e112019-05-10 16:07:28 -06001822 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07001823 return -EAGAIN;
1824
1825 /* workqueue context doesn't hold uring_lock, grab it now */
1826 if (s->needs_lock)
1827 mutex_lock(&ctx->uring_lock);
1828 io_iopoll_req_issued(req);
1829 if (s->needs_lock)
1830 mutex_unlock(&ctx->uring_lock);
1831 }
1832
1833 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001834}
1835
Jens Axboe31b51512019-01-18 22:56:34 -07001836static struct async_list *io_async_list_from_sqe(struct io_ring_ctx *ctx,
1837 const struct io_uring_sqe *sqe)
1838{
1839 switch (sqe->opcode) {
1840 case IORING_OP_READV:
1841 case IORING_OP_READ_FIXED:
1842 return &ctx->pending_async[READ];
1843 case IORING_OP_WRITEV:
1844 case IORING_OP_WRITE_FIXED:
1845 return &ctx->pending_async[WRITE];
1846 default:
1847 return NULL;
1848 }
1849}
1850
Jens Axboeedafcce2019-01-09 09:16:05 -07001851static inline bool io_sqe_needs_user(const struct io_uring_sqe *sqe)
1852{
1853 u8 opcode = READ_ONCE(sqe->opcode);
1854
1855 return !(opcode == IORING_OP_READ_FIXED ||
1856 opcode == IORING_OP_WRITE_FIXED);
1857}
1858
Jens Axboe2b188cc2019-01-07 10:46:33 -07001859static void io_sq_wq_submit_work(struct work_struct *work)
1860{
1861 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001862 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe31b51512019-01-18 22:56:34 -07001863 struct mm_struct *cur_mm = NULL;
1864 struct async_list *async_list;
1865 LIST_HEAD(req_list);
Jens Axboeedafcce2019-01-09 09:16:05 -07001866 mm_segment_t old_fs;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001867 int ret;
1868
Jens Axboe31b51512019-01-18 22:56:34 -07001869 async_list = io_async_list_from_sqe(ctx, req->submit.sqe);
1870restart:
1871 do {
1872 struct sqe_submit *s = &req->submit;
1873 const struct io_uring_sqe *sqe = s->sqe;
Jackie Liud0ee8792019-07-31 14:39:33 +08001874 unsigned int flags = req->flags;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001875
Stefan Bühler8449eed2019-04-27 20:34:19 +02001876 /* Ensure we clear previously set non-block flag */
Jens Axboe31b51512019-01-18 22:56:34 -07001877 req->rw.ki_flags &= ~IOCB_NOWAIT;
1878
1879 ret = 0;
1880 if (io_sqe_needs_user(sqe) && !cur_mm) {
1881 if (!mmget_not_zero(ctx->sqo_mm)) {
1882 ret = -EFAULT;
1883 } else {
1884 cur_mm = ctx->sqo_mm;
1885 use_mm(cur_mm);
1886 old_fs = get_fs();
1887 set_fs(USER_DS);
1888 }
1889 }
1890
1891 if (!ret) {
1892 s->has_user = cur_mm != NULL;
1893 s->needs_lock = true;
1894 do {
Jens Axboe8358e3a2019-04-23 08:17:58 -06001895 ret = __io_submit_sqe(ctx, req, s, false);
Jens Axboe31b51512019-01-18 22:56:34 -07001896 /*
1897 * We can get EAGAIN for polled IO even though
1898 * we're forcing a sync submission from here,
1899 * since we can't wait for request slots on the
1900 * block side.
1901 */
1902 if (ret != -EAGAIN)
1903 break;
1904 cond_resched();
1905 } while (1);
1906 }
Jens Axboe817869d2019-04-30 14:44:05 -06001907
1908 /* drop submission reference */
1909 io_put_req(req);
1910
Jens Axboe31b51512019-01-18 22:56:34 -07001911 if (ret) {
Jens Axboec71ffb62019-05-13 20:58:29 -06001912 io_cqring_add_event(ctx, sqe->user_data, ret);
Jens Axboee65ef562019-03-12 10:16:44 -06001913 io_put_req(req);
Jens Axboe31b51512019-01-18 22:56:34 -07001914 }
1915
1916 /* async context always use a copy of the sqe */
1917 kfree(sqe);
1918
Zhengyuan Liuf7b76ac2019-07-16 23:26:14 +08001919 /* req from defer and link list needn't decrease async cnt */
Jackie Liud0ee8792019-07-31 14:39:33 +08001920 if (flags & (REQ_F_IO_DRAINED | REQ_F_LINK_DONE))
Zhengyuan Liuf7b76ac2019-07-16 23:26:14 +08001921 goto out;
1922
Jens Axboe31b51512019-01-18 22:56:34 -07001923 if (!async_list)
1924 break;
1925 if (!list_empty(&req_list)) {
1926 req = list_first_entry(&req_list, struct io_kiocb,
1927 list);
1928 list_del(&req->list);
1929 continue;
1930 }
1931 if (list_empty(&async_list->list))
1932 break;
1933
1934 req = NULL;
1935 spin_lock(&async_list->lock);
1936 if (list_empty(&async_list->list)) {
1937 spin_unlock(&async_list->lock);
1938 break;
1939 }
1940 list_splice_init(&async_list->list, &req_list);
1941 spin_unlock(&async_list->lock);
1942
1943 req = list_first_entry(&req_list, struct io_kiocb, list);
1944 list_del(&req->list);
1945 } while (req);
Jens Axboeedafcce2019-01-09 09:16:05 -07001946
1947 /*
Jens Axboe31b51512019-01-18 22:56:34 -07001948 * Rare case of racing with a submitter. If we find the count has
1949 * dropped to zero AND we have pending work items, then restart
1950 * the processing. This is a tiny race window.
Jens Axboeedafcce2019-01-09 09:16:05 -07001951 */
Jens Axboe31b51512019-01-18 22:56:34 -07001952 if (async_list) {
1953 ret = atomic_dec_return(&async_list->cnt);
1954 while (!ret && !list_empty(&async_list->list)) {
1955 spin_lock(&async_list->lock);
1956 atomic_inc(&async_list->cnt);
1957 list_splice_init(&async_list->list, &req_list);
1958 spin_unlock(&async_list->lock);
1959
1960 if (!list_empty(&req_list)) {
1961 req = list_first_entry(&req_list,
1962 struct io_kiocb, list);
1963 list_del(&req->list);
1964 goto restart;
1965 }
1966 ret = atomic_dec_return(&async_list->cnt);
Jens Axboeedafcce2019-01-09 09:16:05 -07001967 }
Jens Axboeedafcce2019-01-09 09:16:05 -07001968 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001969
Zhengyuan Liuf7b76ac2019-07-16 23:26:14 +08001970out:
Jens Axboe31b51512019-01-18 22:56:34 -07001971 if (cur_mm) {
Jens Axboeedafcce2019-01-09 09:16:05 -07001972 set_fs(old_fs);
Jens Axboe31b51512019-01-18 22:56:34 -07001973 unuse_mm(cur_mm);
1974 mmput(cur_mm);
Jens Axboeedafcce2019-01-09 09:16:05 -07001975 }
Jens Axboe31b51512019-01-18 22:56:34 -07001976}
Jens Axboe2b188cc2019-01-07 10:46:33 -07001977
Jens Axboe31b51512019-01-18 22:56:34 -07001978/*
1979 * See if we can piggy back onto previously submitted work, that is still
1980 * running. We currently only allow this if the new request is sequential
1981 * to the previous one we punted.
1982 */
1983static bool io_add_to_prev_work(struct async_list *list, struct io_kiocb *req)
1984{
1985 bool ret = false;
1986
1987 if (!list)
1988 return false;
1989 if (!(req->flags & REQ_F_SEQ_PREV))
1990 return false;
1991 if (!atomic_read(&list->cnt))
1992 return false;
1993
1994 ret = true;
1995 spin_lock(&list->lock);
1996 list_add_tail(&req->list, &list->list);
Zhengyuan Liuc0e48f92019-07-18 20:44:00 +08001997 /*
1998 * Ensure we see a simultaneous modification from io_sq_wq_submit_work()
1999 */
2000 smp_mb();
Jens Axboe31b51512019-01-18 22:56:34 -07002001 if (!atomic_read(&list->cnt)) {
2002 list_del_init(&req->list);
2003 ret = false;
2004 }
2005 spin_unlock(&list->lock);
2006 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002007}
2008
Jens Axboe09bb8392019-03-13 12:39:28 -06002009static bool io_op_needs_file(const struct io_uring_sqe *sqe)
2010{
2011 int op = READ_ONCE(sqe->opcode);
2012
2013 switch (op) {
2014 case IORING_OP_NOP:
2015 case IORING_OP_POLL_REMOVE:
2016 return false;
2017 default:
2018 return true;
2019 }
2020}
2021
2022static int io_req_set_file(struct io_ring_ctx *ctx, const struct sqe_submit *s,
2023 struct io_submit_state *state, struct io_kiocb *req)
2024{
2025 unsigned flags;
2026 int fd;
2027
2028 flags = READ_ONCE(s->sqe->flags);
2029 fd = READ_ONCE(s->sqe->fd);
2030
Jackie Liu4fe2c962019-09-09 20:50:40 +08002031 if (flags & IOSQE_IO_DRAIN)
Jens Axboede0617e2019-04-06 21:51:27 -06002032 req->flags |= REQ_F_IO_DRAIN;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002033 /*
2034 * All io need record the previous position, if LINK vs DARIN,
2035 * it can be used to mark the position of the first IO in the
2036 * link list.
2037 */
2038 req->sequence = s->sequence;
Jens Axboede0617e2019-04-06 21:51:27 -06002039
Jens Axboe60c112b2019-06-21 10:20:18 -06002040 if (!io_op_needs_file(s->sqe))
Jens Axboe09bb8392019-03-13 12:39:28 -06002041 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06002042
2043 if (flags & IOSQE_FIXED_FILE) {
2044 if (unlikely(!ctx->user_files ||
2045 (unsigned) fd >= ctx->nr_user_files))
2046 return -EBADF;
2047 req->file = ctx->user_files[fd];
2048 req->flags |= REQ_F_FIXED_FILE;
2049 } else {
2050 if (s->needs_fixed_file)
2051 return -EBADF;
2052 req->file = io_file_get(state, fd);
2053 if (unlikely(!req->file))
2054 return -EBADF;
2055 }
2056
2057 return 0;
2058}
2059
Jackie Liu4fe2c962019-09-09 20:50:40 +08002060static int __io_queue_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
Jens Axboec57666682019-09-09 16:19:45 -06002061 struct sqe_submit *s, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002062{
Jens Axboee0c5c572019-03-12 10:18:47 -06002063 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002064
Jens Axboec57666682019-09-09 16:19:45 -06002065 ret = __io_submit_sqe(ctx, req, s, force_nonblock);
Stefan Bühler8449eed2019-04-27 20:34:19 +02002066 if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07002067 struct io_uring_sqe *sqe_copy;
2068
2069 sqe_copy = kmalloc(sizeof(*sqe_copy), GFP_KERNEL);
2070 if (sqe_copy) {
Jens Axboe31b51512019-01-18 22:56:34 -07002071 struct async_list *list;
2072
Jens Axboe2b188cc2019-01-07 10:46:33 -07002073 memcpy(sqe_copy, s->sqe, sizeof(*sqe_copy));
2074 s->sqe = sqe_copy;
2075
2076 memcpy(&req->submit, s, sizeof(*s));
Jens Axboe31b51512019-01-18 22:56:34 -07002077 list = io_async_list_from_sqe(ctx, s->sqe);
2078 if (!io_add_to_prev_work(list, req)) {
2079 if (list)
2080 atomic_inc(&list->cnt);
2081 INIT_WORK(&req->work, io_sq_wq_submit_work);
Jens Axboe18d9be12019-09-10 09:13:05 -06002082 io_queue_async_work(ctx, req);
Jens Axboe31b51512019-01-18 22:56:34 -07002083 }
Jens Axboee65ef562019-03-12 10:16:44 -06002084
2085 /*
2086 * Queued up for async execution, worker will release
Jens Axboe9e645e112019-05-10 16:07:28 -06002087 * submit reference when the iocb is actually submitted.
Jens Axboee65ef562019-03-12 10:16:44 -06002088 */
2089 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002090 }
2091 }
Jens Axboee65ef562019-03-12 10:16:44 -06002092
2093 /* drop submission reference */
2094 io_put_req(req);
2095
2096 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06002097 if (ret) {
2098 io_cqring_add_event(ctx, req->user_data, ret);
2099 if (req->flags & REQ_F_LINK)
2100 req->flags |= REQ_F_FAIL_LINK;
Jens Axboee65ef562019-03-12 10:16:44 -06002101 io_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002102 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002103
2104 return ret;
2105}
2106
Jackie Liu4fe2c962019-09-09 20:50:40 +08002107static int io_queue_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
Jens Axboec57666682019-09-09 16:19:45 -06002108 struct sqe_submit *s, bool force_nonblock)
Jackie Liu4fe2c962019-09-09 20:50:40 +08002109{
2110 int ret;
2111
2112 ret = io_req_defer(ctx, req, s->sqe);
2113 if (ret) {
2114 if (ret != -EIOCBQUEUED) {
2115 io_free_req(req);
2116 io_cqring_add_event(ctx, s->sqe->user_data, ret);
2117 }
2118 return 0;
2119 }
2120
Jens Axboec57666682019-09-09 16:19:45 -06002121 return __io_queue_sqe(ctx, req, s, force_nonblock);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002122}
2123
2124static int io_queue_link_head(struct io_ring_ctx *ctx, struct io_kiocb *req,
Jens Axboec57666682019-09-09 16:19:45 -06002125 struct sqe_submit *s, struct io_kiocb *shadow,
2126 bool force_nonblock)
Jackie Liu4fe2c962019-09-09 20:50:40 +08002127{
2128 int ret;
2129 int need_submit = false;
2130
2131 if (!shadow)
Jens Axboec57666682019-09-09 16:19:45 -06002132 return io_queue_sqe(ctx, req, s, force_nonblock);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002133
2134 /*
2135 * Mark the first IO in link list as DRAIN, let all the following
2136 * IOs enter the defer list. all IO needs to be completed before link
2137 * list.
2138 */
2139 req->flags |= REQ_F_IO_DRAIN;
2140 ret = io_req_defer(ctx, req, s->sqe);
2141 if (ret) {
2142 if (ret != -EIOCBQUEUED) {
2143 io_free_req(req);
2144 io_cqring_add_event(ctx, s->sqe->user_data, ret);
2145 return 0;
2146 }
2147 } else {
2148 /*
2149 * If ret == 0 means that all IOs in front of link io are
2150 * running done. let's queue link head.
2151 */
2152 need_submit = true;
2153 }
2154
2155 /* Insert shadow req to defer_list, blocking next IOs */
2156 spin_lock_irq(&ctx->completion_lock);
2157 list_add_tail(&shadow->list, &ctx->defer_list);
2158 spin_unlock_irq(&ctx->completion_lock);
2159
2160 if (need_submit)
Jens Axboec57666682019-09-09 16:19:45 -06002161 return __io_queue_sqe(ctx, req, s, force_nonblock);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002162
2163 return 0;
2164}
2165
Jens Axboe9e645e112019-05-10 16:07:28 -06002166#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK)
2167
2168static void io_submit_sqe(struct io_ring_ctx *ctx, struct sqe_submit *s,
Jens Axboec57666682019-09-09 16:19:45 -06002169 struct io_submit_state *state, struct io_kiocb **link,
2170 bool force_nonblock)
Jens Axboe9e645e112019-05-10 16:07:28 -06002171{
2172 struct io_uring_sqe *sqe_copy;
2173 struct io_kiocb *req;
2174 int ret;
2175
2176 /* enforce forwards compatibility on users */
2177 if (unlikely(s->sqe->flags & ~SQE_VALID_FLAGS)) {
2178 ret = -EINVAL;
2179 goto err;
2180 }
2181
2182 req = io_get_req(ctx, state);
2183 if (unlikely(!req)) {
2184 ret = -EAGAIN;
2185 goto err;
2186 }
2187
2188 ret = io_req_set_file(ctx, s, state, req);
2189 if (unlikely(ret)) {
2190err_req:
2191 io_free_req(req);
2192err:
2193 io_cqring_add_event(ctx, s->sqe->user_data, ret);
2194 return;
2195 }
2196
Jens Axboe9e645e112019-05-10 16:07:28 -06002197 /*
2198 * If we already have a head request, queue this one for async
2199 * submittal once the head completes. If we don't have a head but
2200 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
2201 * submitted sync once the chain is complete. If none of those
2202 * conditions are true (normal request), then just queue it.
2203 */
2204 if (*link) {
2205 struct io_kiocb *prev = *link;
2206
2207 sqe_copy = kmemdup(s->sqe, sizeof(*sqe_copy), GFP_KERNEL);
2208 if (!sqe_copy) {
2209 ret = -EAGAIN;
2210 goto err_req;
2211 }
2212
2213 s->sqe = sqe_copy;
2214 memcpy(&req->submit, s, sizeof(*s));
2215 list_add_tail(&req->list, &prev->link_list);
2216 } else if (s->sqe->flags & IOSQE_IO_LINK) {
2217 req->flags |= REQ_F_LINK;
2218
2219 memcpy(&req->submit, s, sizeof(*s));
2220 INIT_LIST_HEAD(&req->link_list);
2221 *link = req;
2222 } else {
Jens Axboec57666682019-09-09 16:19:45 -06002223 io_queue_sqe(ctx, req, s, force_nonblock);
Jens Axboe9e645e112019-05-10 16:07:28 -06002224 }
2225}
2226
Jens Axboe9a56a232019-01-09 09:06:50 -07002227/*
2228 * Batched submission is done, ensure local IO is flushed out.
2229 */
2230static void io_submit_state_end(struct io_submit_state *state)
2231{
2232 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06002233 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07002234 if (state->free_reqs)
2235 kmem_cache_free_bulk(req_cachep, state->free_reqs,
2236 &state->reqs[state->cur_req]);
Jens Axboe9a56a232019-01-09 09:06:50 -07002237}
2238
2239/*
2240 * Start submission side cache.
2241 */
2242static void io_submit_state_start(struct io_submit_state *state,
2243 struct io_ring_ctx *ctx, unsigned max_ios)
2244{
2245 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07002246 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07002247 state->file = NULL;
2248 state->ios_left = max_ios;
2249}
2250
Jens Axboe2b188cc2019-01-07 10:46:33 -07002251static void io_commit_sqring(struct io_ring_ctx *ctx)
2252{
Hristo Venev75b28af2019-08-26 17:23:46 +00002253 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002254
Hristo Venev75b28af2019-08-26 17:23:46 +00002255 if (ctx->cached_sq_head != READ_ONCE(rings->sq.head)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07002256 /*
2257 * Ensure any loads from the SQEs are done at this point,
2258 * since once we write the new head, the application could
2259 * write new data to them.
2260 */
Hristo Venev75b28af2019-08-26 17:23:46 +00002261 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002262 }
2263}
2264
2265/*
Jens Axboe2b188cc2019-01-07 10:46:33 -07002266 * Fetch an sqe, if one is available. Note that s->sqe will point to memory
2267 * that is mapped by userspace. This means that care needs to be taken to
2268 * ensure that reads are stable, as we cannot rely on userspace always
2269 * being a good citizen. If members of the sqe are validated and then later
2270 * used, it's important that those reads are done through READ_ONCE() to
2271 * prevent a re-load down the line.
2272 */
2273static bool io_get_sqring(struct io_ring_ctx *ctx, struct sqe_submit *s)
2274{
Hristo Venev75b28af2019-08-26 17:23:46 +00002275 struct io_rings *rings = ctx->rings;
2276 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002277 unsigned head;
2278
2279 /*
2280 * The cached sq head (or cq tail) serves two purposes:
2281 *
2282 * 1) allows us to batch the cost of updating the user visible
2283 * head updates.
2284 * 2) allows the kernel side to track the head on its own, even
2285 * though the application is the one updating it.
2286 */
2287 head = ctx->cached_sq_head;
Stefan Bühlere523a292019-04-19 11:57:44 +02002288 /* make sure SQ entry isn't read before tail */
Hristo Venev75b28af2019-08-26 17:23:46 +00002289 if (head == smp_load_acquire(&rings->sq.tail))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002290 return false;
2291
Hristo Venev75b28af2019-08-26 17:23:46 +00002292 head = READ_ONCE(sq_array[head & ctx->sq_mask]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002293 if (head < ctx->sq_entries) {
2294 s->index = head;
2295 s->sqe = &ctx->sq_sqes[head];
Jackie Liu8776f3f2019-09-09 20:50:39 +08002296 s->sequence = ctx->cached_sq_head;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002297 ctx->cached_sq_head++;
2298 return true;
2299 }
2300
2301 /* drop invalid entries */
2302 ctx->cached_sq_head++;
Hristo Venev75b28af2019-08-26 17:23:46 +00002303 rings->sq_dropped++;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002304 return false;
2305}
2306
Jens Axboe6c271ce2019-01-10 11:22:30 -07002307static int io_submit_sqes(struct io_ring_ctx *ctx, struct sqe_submit *sqes,
2308 unsigned int nr, bool has_user, bool mm_fault)
2309{
2310 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002311 struct io_kiocb *link = NULL;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002312 struct io_kiocb *shadow_req = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002313 bool prev_was_link = false;
2314 int i, submitted = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002315
2316 if (nr > IO_PLUG_THRESHOLD) {
2317 io_submit_state_start(&state, ctx, nr);
2318 statep = &state;
2319 }
2320
2321 for (i = 0; i < nr; i++) {
Jens Axboe9e645e112019-05-10 16:07:28 -06002322 /*
2323 * If previous wasn't linked and we have a linked command,
2324 * that's the end of the chain. Submit the previous link.
2325 */
2326 if (!prev_was_link && link) {
Jens Axboec57666682019-09-09 16:19:45 -06002327 io_queue_link_head(ctx, link, &link->submit, shadow_req,
2328 true);
Jens Axboe9e645e112019-05-10 16:07:28 -06002329 link = NULL;
2330 }
2331 prev_was_link = (sqes[i].sqe->flags & IOSQE_IO_LINK) != 0;
2332
Jackie Liu4fe2c962019-09-09 20:50:40 +08002333 if (link && (sqes[i].sqe->flags & IOSQE_IO_DRAIN)) {
2334 if (!shadow_req) {
2335 shadow_req = io_get_req(ctx, NULL);
2336 shadow_req->flags |= (REQ_F_IO_DRAIN | REQ_F_SHADOW_DRAIN);
2337 refcount_dec(&shadow_req->refs);
2338 }
2339 shadow_req->sequence = sqes[i].sequence;
2340 }
2341
Jens Axboe6c271ce2019-01-10 11:22:30 -07002342 if (unlikely(mm_fault)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06002343 io_cqring_add_event(ctx, sqes[i].sqe->user_data,
2344 -EFAULT);
Jens Axboe6c271ce2019-01-10 11:22:30 -07002345 } else {
2346 sqes[i].has_user = has_user;
2347 sqes[i].needs_lock = true;
2348 sqes[i].needs_fixed_file = true;
Jens Axboec57666682019-09-09 16:19:45 -06002349 io_submit_sqe(ctx, &sqes[i], statep, &link, true);
Jens Axboe6c271ce2019-01-10 11:22:30 -07002350 submitted++;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002351 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07002352 }
2353
Jens Axboe9e645e112019-05-10 16:07:28 -06002354 if (link)
Jens Axboec57666682019-09-09 16:19:45 -06002355 io_queue_link_head(ctx, link, &link->submit, shadow_req, true);
Jens Axboe6c271ce2019-01-10 11:22:30 -07002356 if (statep)
2357 io_submit_state_end(&state);
2358
2359 return submitted;
2360}
2361
2362static int io_sq_thread(void *data)
2363{
2364 struct sqe_submit sqes[IO_IOPOLL_BATCH];
2365 struct io_ring_ctx *ctx = data;
2366 struct mm_struct *cur_mm = NULL;
2367 mm_segment_t old_fs;
2368 DEFINE_WAIT(wait);
2369 unsigned inflight;
2370 unsigned long timeout;
2371
Jackie Liua4c0b3d2019-07-08 13:41:12 +08002372 complete(&ctx->sqo_thread_started);
2373
Jens Axboe6c271ce2019-01-10 11:22:30 -07002374 old_fs = get_fs();
2375 set_fs(USER_DS);
2376
2377 timeout = inflight = 0;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02002378 while (!kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07002379 bool all_fixed, mm_fault = false;
2380 int i;
2381
2382 if (inflight) {
2383 unsigned nr_events = 0;
2384
2385 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07002386 io_iopoll_check(ctx, &nr_events, 0);
Jens Axboe6c271ce2019-01-10 11:22:30 -07002387 } else {
2388 /*
2389 * Normal IO, just pretend everything completed.
2390 * We don't have to poll completions for that.
2391 */
2392 nr_events = inflight;
2393 }
2394
2395 inflight -= nr_events;
2396 if (!inflight)
2397 timeout = jiffies + ctx->sq_thread_idle;
2398 }
2399
2400 if (!io_get_sqring(ctx, &sqes[0])) {
2401 /*
2402 * We're polling. If we're within the defined idle
2403 * period, then let us spin without work before going
2404 * to sleep.
2405 */
2406 if (inflight || !time_after(jiffies, timeout)) {
2407 cpu_relax();
2408 continue;
2409 }
2410
2411 /*
2412 * Drop cur_mm before scheduling, we can't hold it for
2413 * long periods (or over schedule()). Do this before
2414 * adding ourselves to the waitqueue, as the unuse/drop
2415 * may sleep.
2416 */
2417 if (cur_mm) {
2418 unuse_mm(cur_mm);
2419 mmput(cur_mm);
2420 cur_mm = NULL;
2421 }
2422
2423 prepare_to_wait(&ctx->sqo_wait, &wait,
2424 TASK_INTERRUPTIBLE);
2425
2426 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00002427 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02002428 /* make sure to read SQ tail after writing flags */
2429 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07002430
2431 if (!io_get_sqring(ctx, &sqes[0])) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02002432 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07002433 finish_wait(&ctx->sqo_wait, &wait);
2434 break;
2435 }
2436 if (signal_pending(current))
2437 flush_signals(current);
2438 schedule();
2439 finish_wait(&ctx->sqo_wait, &wait);
2440
Hristo Venev75b28af2019-08-26 17:23:46 +00002441 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002442 continue;
2443 }
2444 finish_wait(&ctx->sqo_wait, &wait);
2445
Hristo Venev75b28af2019-08-26 17:23:46 +00002446 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002447 }
2448
2449 i = 0;
2450 all_fixed = true;
2451 do {
2452 if (all_fixed && io_sqe_needs_user(sqes[i].sqe))
2453 all_fixed = false;
2454
2455 i++;
2456 if (i == ARRAY_SIZE(sqes))
2457 break;
2458 } while (io_get_sqring(ctx, &sqes[i]));
2459
2460 /* Unless all new commands are FIXED regions, grab mm */
2461 if (!all_fixed && !cur_mm) {
2462 mm_fault = !mmget_not_zero(ctx->sqo_mm);
2463 if (!mm_fault) {
2464 use_mm(ctx->sqo_mm);
2465 cur_mm = ctx->sqo_mm;
2466 }
2467 }
2468
2469 inflight += io_submit_sqes(ctx, sqes, i, cur_mm != NULL,
2470 mm_fault);
2471
2472 /* Commit SQ ring head once we've consumed all SQEs */
2473 io_commit_sqring(ctx);
2474 }
2475
2476 set_fs(old_fs);
2477 if (cur_mm) {
2478 unuse_mm(cur_mm);
2479 mmput(cur_mm);
2480 }
Jens Axboe06058632019-04-13 09:26:03 -06002481
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02002482 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06002483
Jens Axboe6c271ce2019-01-10 11:22:30 -07002484 return 0;
2485}
2486
Jens Axboec57666682019-09-09 16:19:45 -06002487static int io_ring_submit(struct io_ring_ctx *ctx, unsigned int to_submit,
2488 bool block_for_last)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002489{
Jens Axboe9a56a232019-01-09 09:06:50 -07002490 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002491 struct io_kiocb *link = NULL;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002492 struct io_kiocb *shadow_req = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002493 bool prev_was_link = false;
Jens Axboe5c8b0b52019-04-30 10:16:07 -06002494 int i, submit = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002495
Jens Axboe9a56a232019-01-09 09:06:50 -07002496 if (to_submit > IO_PLUG_THRESHOLD) {
2497 io_submit_state_start(&state, ctx, to_submit);
2498 statep = &state;
2499 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002500
2501 for (i = 0; i < to_submit; i++) {
Jens Axboec57666682019-09-09 16:19:45 -06002502 bool force_nonblock = true;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002503 struct sqe_submit s;
2504
2505 if (!io_get_sqring(ctx, &s))
2506 break;
2507
Jens Axboe9e645e112019-05-10 16:07:28 -06002508 /*
2509 * If previous wasn't linked and we have a linked command,
2510 * that's the end of the chain. Submit the previous link.
2511 */
2512 if (!prev_was_link && link) {
Jens Axboec57666682019-09-09 16:19:45 -06002513 io_queue_link_head(ctx, link, &link->submit, shadow_req,
2514 force_nonblock);
Jens Axboe9e645e112019-05-10 16:07:28 -06002515 link = NULL;
2516 }
2517 prev_was_link = (s.sqe->flags & IOSQE_IO_LINK) != 0;
2518
Jackie Liu4fe2c962019-09-09 20:50:40 +08002519 if (link && (s.sqe->flags & IOSQE_IO_DRAIN)) {
2520 if (!shadow_req) {
2521 shadow_req = io_get_req(ctx, NULL);
2522 shadow_req->flags |= (REQ_F_IO_DRAIN | REQ_F_SHADOW_DRAIN);
2523 refcount_dec(&shadow_req->refs);
2524 }
2525 shadow_req->sequence = s.sequence;
2526 }
2527
Jens Axboe2b188cc2019-01-07 10:46:33 -07002528 s.has_user = true;
Jens Axboedef596e2019-01-09 08:59:42 -07002529 s.needs_lock = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002530 s.needs_fixed_file = false;
Jens Axboe5c8b0b52019-04-30 10:16:07 -06002531 submit++;
Jens Axboec57666682019-09-09 16:19:45 -06002532
2533 /*
2534 * The caller will block for events after submit, submit the
2535 * last IO non-blocking. This is either the only IO it's
2536 * submitting, or it already submitted the previous ones. This
2537 * improves performance by avoiding an async punt that we don't
2538 * need to do.
2539 */
2540 if (block_for_last && submit == to_submit)
2541 force_nonblock = false;
2542
2543 io_submit_sqe(ctx, &s, statep, &link, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002544 }
2545 io_commit_sqring(ctx);
2546
Jens Axboe9e645e112019-05-10 16:07:28 -06002547 if (link)
Jens Axboec57666682019-09-09 16:19:45 -06002548 io_queue_link_head(ctx, link, &link->submit, shadow_req,
2549 block_for_last);
Jens Axboe9a56a232019-01-09 09:06:50 -07002550 if (statep)
2551 io_submit_state_end(statep);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002552
Jens Axboe5c8b0b52019-04-30 10:16:07 -06002553 return submit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002554}
2555
Jens Axboe2b188cc2019-01-07 10:46:33 -07002556/*
2557 * Wait until events become available, if we don't already have some. The
2558 * application must reap them itself, as they reside on the shared cq ring.
2559 */
2560static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
2561 const sigset_t __user *sig, size_t sigsz)
2562{
Hristo Venev75b28af2019-08-26 17:23:46 +00002563 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002564 int ret;
2565
Hristo Venev75b28af2019-08-26 17:23:46 +00002566 if (io_cqring_events(rings) >= min_events)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002567 return 0;
2568
2569 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01002570#ifdef CONFIG_COMPAT
2571 if (in_compat_syscall())
2572 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07002573 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01002574 else
2575#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07002576 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01002577
Jens Axboe2b188cc2019-01-07 10:46:33 -07002578 if (ret)
2579 return ret;
2580 }
2581
Hristo Venev75b28af2019-08-26 17:23:46 +00002582 ret = wait_event_interruptible(ctx->wait, io_cqring_events(rings) >= min_events);
Oleg Nesterovb7724342019-07-16 16:29:53 -07002583 restore_saved_sigmask_unless(ret == -ERESTARTSYS);
Oleg Nesterov97abc882019-06-28 12:06:50 -07002584 if (ret == -ERESTARTSYS)
2585 ret = -EINTR;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002586
Hristo Venev75b28af2019-08-26 17:23:46 +00002587 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002588}
2589
Jens Axboe6b063142019-01-10 22:13:58 -07002590static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
2591{
2592#if defined(CONFIG_UNIX)
2593 if (ctx->ring_sock) {
2594 struct sock *sock = ctx->ring_sock->sk;
2595 struct sk_buff *skb;
2596
2597 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
2598 kfree_skb(skb);
2599 }
2600#else
2601 int i;
2602
2603 for (i = 0; i < ctx->nr_user_files; i++)
2604 fput(ctx->user_files[i]);
2605#endif
2606}
2607
2608static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
2609{
2610 if (!ctx->user_files)
2611 return -ENXIO;
2612
2613 __io_sqe_files_unregister(ctx);
2614 kfree(ctx->user_files);
2615 ctx->user_files = NULL;
2616 ctx->nr_user_files = 0;
2617 return 0;
2618}
2619
Jens Axboe6c271ce2019-01-10 11:22:30 -07002620static void io_sq_thread_stop(struct io_ring_ctx *ctx)
2621{
2622 if (ctx->sqo_thread) {
Jackie Liua4c0b3d2019-07-08 13:41:12 +08002623 wait_for_completion(&ctx->sqo_thread_started);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02002624 /*
2625 * The park is a bit of a work-around, without it we get
2626 * warning spews on shutdown with SQPOLL set and affinity
2627 * set to a single CPU.
2628 */
Jens Axboe06058632019-04-13 09:26:03 -06002629 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07002630 kthread_stop(ctx->sqo_thread);
2631 ctx->sqo_thread = NULL;
2632 }
2633}
2634
Jens Axboe6b063142019-01-10 22:13:58 -07002635static void io_finish_async(struct io_ring_ctx *ctx)
2636{
Jens Axboe6c271ce2019-01-10 11:22:30 -07002637 io_sq_thread_stop(ctx);
2638
Jens Axboe6b063142019-01-10 22:13:58 -07002639 if (ctx->sqo_wq) {
2640 destroy_workqueue(ctx->sqo_wq);
2641 ctx->sqo_wq = NULL;
2642 }
2643}
2644
2645#if defined(CONFIG_UNIX)
2646static void io_destruct_skb(struct sk_buff *skb)
2647{
2648 struct io_ring_ctx *ctx = skb->sk->sk_user_data;
2649
2650 io_finish_async(ctx);
2651 unix_destruct_scm(skb);
2652}
2653
2654/*
2655 * Ensure the UNIX gc is aware of our file set, so we are certain that
2656 * the io_uring can be safely unregistered on process exit, even if we have
2657 * loops in the file referencing.
2658 */
2659static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
2660{
2661 struct sock *sk = ctx->ring_sock->sk;
2662 struct scm_fp_list *fpl;
2663 struct sk_buff *skb;
2664 int i;
2665
2666 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
2667 unsigned long inflight = ctx->user->unix_inflight + nr;
2668
2669 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
2670 return -EMFILE;
2671 }
2672
2673 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
2674 if (!fpl)
2675 return -ENOMEM;
2676
2677 skb = alloc_skb(0, GFP_KERNEL);
2678 if (!skb) {
2679 kfree(fpl);
2680 return -ENOMEM;
2681 }
2682
2683 skb->sk = sk;
2684 skb->destructor = io_destruct_skb;
2685
2686 fpl->user = get_uid(ctx->user);
2687 for (i = 0; i < nr; i++) {
2688 fpl->fp[i] = get_file(ctx->user_files[i + offset]);
2689 unix_inflight(fpl->user, fpl->fp[i]);
2690 }
2691
2692 fpl->max = fpl->count = nr;
2693 UNIXCB(skb).fp = fpl;
2694 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
2695 skb_queue_head(&sk->sk_receive_queue, skb);
2696
2697 for (i = 0; i < nr; i++)
2698 fput(fpl->fp[i]);
2699
2700 return 0;
2701}
2702
2703/*
2704 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
2705 * causes regular reference counting to break down. We rely on the UNIX
2706 * garbage collection to take care of this problem for us.
2707 */
2708static int io_sqe_files_scm(struct io_ring_ctx *ctx)
2709{
2710 unsigned left, total;
2711 int ret = 0;
2712
2713 total = 0;
2714 left = ctx->nr_user_files;
2715 while (left) {
2716 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07002717
2718 ret = __io_sqe_files_scm(ctx, this_files, total);
2719 if (ret)
2720 break;
2721 left -= this_files;
2722 total += this_files;
2723 }
2724
2725 if (!ret)
2726 return 0;
2727
2728 while (total < ctx->nr_user_files) {
2729 fput(ctx->user_files[total]);
2730 total++;
2731 }
2732
2733 return ret;
2734}
2735#else
2736static int io_sqe_files_scm(struct io_ring_ctx *ctx)
2737{
2738 return 0;
2739}
2740#endif
2741
2742static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
2743 unsigned nr_args)
2744{
2745 __s32 __user *fds = (__s32 __user *) arg;
2746 int fd, ret = 0;
2747 unsigned i;
2748
2749 if (ctx->user_files)
2750 return -EBUSY;
2751 if (!nr_args)
2752 return -EINVAL;
2753 if (nr_args > IORING_MAX_FIXED_FILES)
2754 return -EMFILE;
2755
2756 ctx->user_files = kcalloc(nr_args, sizeof(struct file *), GFP_KERNEL);
2757 if (!ctx->user_files)
2758 return -ENOMEM;
2759
2760 for (i = 0; i < nr_args; i++) {
2761 ret = -EFAULT;
2762 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
2763 break;
2764
2765 ctx->user_files[i] = fget(fd);
2766
2767 ret = -EBADF;
2768 if (!ctx->user_files[i])
2769 break;
2770 /*
2771 * Don't allow io_uring instances to be registered. If UNIX
2772 * isn't enabled, then this causes a reference cycle and this
2773 * instance can never get freed. If UNIX is enabled we'll
2774 * handle it just fine, but there's still no point in allowing
2775 * a ring fd as it doesn't support regular read/write anyway.
2776 */
2777 if (ctx->user_files[i]->f_op == &io_uring_fops) {
2778 fput(ctx->user_files[i]);
2779 break;
2780 }
2781 ctx->nr_user_files++;
2782 ret = 0;
2783 }
2784
2785 if (ret) {
2786 for (i = 0; i < ctx->nr_user_files; i++)
2787 fput(ctx->user_files[i]);
2788
2789 kfree(ctx->user_files);
Jens Axboe25adf502019-04-03 09:52:40 -06002790 ctx->user_files = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07002791 ctx->nr_user_files = 0;
2792 return ret;
2793 }
2794
2795 ret = io_sqe_files_scm(ctx);
2796 if (ret)
2797 io_sqe_files_unregister(ctx);
2798
2799 return ret;
2800}
2801
Jens Axboe6c271ce2019-01-10 11:22:30 -07002802static int io_sq_offload_start(struct io_ring_ctx *ctx,
2803 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002804{
2805 int ret;
2806
Jens Axboe6c271ce2019-01-10 11:22:30 -07002807 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002808 mmgrab(current->mm);
2809 ctx->sqo_mm = current->mm;
2810
Jens Axboe6c271ce2019-01-10 11:22:30 -07002811 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06002812 ret = -EPERM;
2813 if (!capable(CAP_SYS_ADMIN))
2814 goto err;
2815
Jens Axboe917257d2019-04-13 09:28:55 -06002816 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
2817 if (!ctx->sq_thread_idle)
2818 ctx->sq_thread_idle = HZ;
2819
Jens Axboe6c271ce2019-01-10 11:22:30 -07002820 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06002821 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002822
Jens Axboe917257d2019-04-13 09:28:55 -06002823 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06002824 if (cpu >= nr_cpu_ids)
2825 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08002826 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06002827 goto err;
2828
Jens Axboe6c271ce2019-01-10 11:22:30 -07002829 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
2830 ctx, cpu,
2831 "io_uring-sq");
2832 } else {
2833 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
2834 "io_uring-sq");
2835 }
2836 if (IS_ERR(ctx->sqo_thread)) {
2837 ret = PTR_ERR(ctx->sqo_thread);
2838 ctx->sqo_thread = NULL;
2839 goto err;
2840 }
2841 wake_up_process(ctx->sqo_thread);
2842 } else if (p->flags & IORING_SETUP_SQ_AFF) {
2843 /* Can't have SQ_AFF without SQPOLL */
2844 ret = -EINVAL;
2845 goto err;
2846 }
2847
Jens Axboe2b188cc2019-01-07 10:46:33 -07002848 /* Do QD, or 2 * CPUS, whatever is smallest */
2849 ctx->sqo_wq = alloc_workqueue("io_ring-wq", WQ_UNBOUND | WQ_FREEZABLE,
2850 min(ctx->sq_entries - 1, 2 * num_online_cpus()));
2851 if (!ctx->sqo_wq) {
2852 ret = -ENOMEM;
2853 goto err;
2854 }
2855
2856 return 0;
2857err:
Jens Axboe6c271ce2019-01-10 11:22:30 -07002858 io_sq_thread_stop(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002859 mmdrop(ctx->sqo_mm);
2860 ctx->sqo_mm = NULL;
2861 return ret;
2862}
2863
2864static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
2865{
2866 atomic_long_sub(nr_pages, &user->locked_vm);
2867}
2868
2869static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
2870{
2871 unsigned long page_limit, cur_pages, new_pages;
2872
2873 /* Don't allow more pages than we can safely lock */
2874 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
2875
2876 do {
2877 cur_pages = atomic_long_read(&user->locked_vm);
2878 new_pages = cur_pages + nr_pages;
2879 if (new_pages > page_limit)
2880 return -ENOMEM;
2881 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
2882 new_pages) != cur_pages);
2883
2884 return 0;
2885}
2886
2887static void io_mem_free(void *ptr)
2888{
Mark Rutland52e04ef2019-04-30 17:30:21 +01002889 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002890
Mark Rutland52e04ef2019-04-30 17:30:21 +01002891 if (!ptr)
2892 return;
2893
2894 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002895 if (put_page_testzero(page))
2896 free_compound_page(page);
2897}
2898
2899static void *io_mem_alloc(size_t size)
2900{
2901 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
2902 __GFP_NORETRY;
2903
2904 return (void *) __get_free_pages(gfp_flags, get_order(size));
2905}
2906
Hristo Venev75b28af2019-08-26 17:23:46 +00002907static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
2908 size_t *sq_offset)
2909{
2910 struct io_rings *rings;
2911 size_t off, sq_array_size;
2912
2913 off = struct_size(rings, cqes, cq_entries);
2914 if (off == SIZE_MAX)
2915 return SIZE_MAX;
2916
2917#ifdef CONFIG_SMP
2918 off = ALIGN(off, SMP_CACHE_BYTES);
2919 if (off == 0)
2920 return SIZE_MAX;
2921#endif
2922
2923 sq_array_size = array_size(sizeof(u32), sq_entries);
2924 if (sq_array_size == SIZE_MAX)
2925 return SIZE_MAX;
2926
2927 if (check_add_overflow(off, sq_array_size, &off))
2928 return SIZE_MAX;
2929
2930 if (sq_offset)
2931 *sq_offset = off;
2932
2933 return off;
2934}
2935
Jens Axboe2b188cc2019-01-07 10:46:33 -07002936static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
2937{
Hristo Venev75b28af2019-08-26 17:23:46 +00002938 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002939
Hristo Venev75b28af2019-08-26 17:23:46 +00002940 pages = (size_t)1 << get_order(
2941 rings_size(sq_entries, cq_entries, NULL));
2942 pages += (size_t)1 << get_order(
2943 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07002944
Hristo Venev75b28af2019-08-26 17:23:46 +00002945 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002946}
2947
Jens Axboeedafcce2019-01-09 09:16:05 -07002948static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
2949{
2950 int i, j;
2951
2952 if (!ctx->user_bufs)
2953 return -ENXIO;
2954
2955 for (i = 0; i < ctx->nr_user_bufs; i++) {
2956 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
2957
2958 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbard27c4d3a2019-08-04 19:32:06 -07002959 put_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07002960
2961 if (ctx->account_mem)
2962 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01002963 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07002964 imu->nr_bvecs = 0;
2965 }
2966
2967 kfree(ctx->user_bufs);
2968 ctx->user_bufs = NULL;
2969 ctx->nr_user_bufs = 0;
2970 return 0;
2971}
2972
2973static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
2974 void __user *arg, unsigned index)
2975{
2976 struct iovec __user *src;
2977
2978#ifdef CONFIG_COMPAT
2979 if (ctx->compat) {
2980 struct compat_iovec __user *ciovs;
2981 struct compat_iovec ciov;
2982
2983 ciovs = (struct compat_iovec __user *) arg;
2984 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
2985 return -EFAULT;
2986
2987 dst->iov_base = (void __user *) (unsigned long) ciov.iov_base;
2988 dst->iov_len = ciov.iov_len;
2989 return 0;
2990 }
2991#endif
2992 src = (struct iovec __user *) arg;
2993 if (copy_from_user(dst, &src[index], sizeof(*dst)))
2994 return -EFAULT;
2995 return 0;
2996}
2997
2998static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
2999 unsigned nr_args)
3000{
3001 struct vm_area_struct **vmas = NULL;
3002 struct page **pages = NULL;
3003 int i, j, got_pages = 0;
3004 int ret = -EINVAL;
3005
3006 if (ctx->user_bufs)
3007 return -EBUSY;
3008 if (!nr_args || nr_args > UIO_MAXIOV)
3009 return -EINVAL;
3010
3011 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
3012 GFP_KERNEL);
3013 if (!ctx->user_bufs)
3014 return -ENOMEM;
3015
3016 for (i = 0; i < nr_args; i++) {
3017 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
3018 unsigned long off, start, end, ubuf;
3019 int pret, nr_pages;
3020 struct iovec iov;
3021 size_t size;
3022
3023 ret = io_copy_iov(ctx, &iov, arg, i);
3024 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03003025 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07003026
3027 /*
3028 * Don't impose further limits on the size and buffer
3029 * constraints here, we'll -EINVAL later when IO is
3030 * submitted if they are wrong.
3031 */
3032 ret = -EFAULT;
3033 if (!iov.iov_base || !iov.iov_len)
3034 goto err;
3035
3036 /* arbitrary limit, but we need something */
3037 if (iov.iov_len > SZ_1G)
3038 goto err;
3039
3040 ubuf = (unsigned long) iov.iov_base;
3041 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
3042 start = ubuf >> PAGE_SHIFT;
3043 nr_pages = end - start;
3044
3045 if (ctx->account_mem) {
3046 ret = io_account_mem(ctx->user, nr_pages);
3047 if (ret)
3048 goto err;
3049 }
3050
3051 ret = 0;
3052 if (!pages || nr_pages > got_pages) {
3053 kfree(vmas);
3054 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003055 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07003056 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003057 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07003058 sizeof(struct vm_area_struct *),
3059 GFP_KERNEL);
3060 if (!pages || !vmas) {
3061 ret = -ENOMEM;
3062 if (ctx->account_mem)
3063 io_unaccount_mem(ctx->user, nr_pages);
3064 goto err;
3065 }
3066 got_pages = nr_pages;
3067 }
3068
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003069 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07003070 GFP_KERNEL);
3071 ret = -ENOMEM;
3072 if (!imu->bvec) {
3073 if (ctx->account_mem)
3074 io_unaccount_mem(ctx->user, nr_pages);
3075 goto err;
3076 }
3077
3078 ret = 0;
3079 down_read(&current->mm->mmap_sem);
Ira Weiny932f4a62019-05-13 17:17:03 -07003080 pret = get_user_pages(ubuf, nr_pages,
3081 FOLL_WRITE | FOLL_LONGTERM,
3082 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07003083 if (pret == nr_pages) {
3084 /* don't support file backed memory */
3085 for (j = 0; j < nr_pages; j++) {
3086 struct vm_area_struct *vma = vmas[j];
3087
3088 if (vma->vm_file &&
3089 !is_file_hugepages(vma->vm_file)) {
3090 ret = -EOPNOTSUPP;
3091 break;
3092 }
3093 }
3094 } else {
3095 ret = pret < 0 ? pret : -EFAULT;
3096 }
3097 up_read(&current->mm->mmap_sem);
3098 if (ret) {
3099 /*
3100 * if we did partial map, or found file backed vmas,
3101 * release any pages we did get
3102 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07003103 if (pret > 0)
3104 put_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07003105 if (ctx->account_mem)
3106 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003107 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07003108 goto err;
3109 }
3110
3111 off = ubuf & ~PAGE_MASK;
3112 size = iov.iov_len;
3113 for (j = 0; j < nr_pages; j++) {
3114 size_t vec_len;
3115
3116 vec_len = min_t(size_t, size, PAGE_SIZE - off);
3117 imu->bvec[j].bv_page = pages[j];
3118 imu->bvec[j].bv_len = vec_len;
3119 imu->bvec[j].bv_offset = off;
3120 off = 0;
3121 size -= vec_len;
3122 }
3123 /* store original address for later verification */
3124 imu->ubuf = ubuf;
3125 imu->len = iov.iov_len;
3126 imu->nr_bvecs = nr_pages;
3127
3128 ctx->nr_user_bufs++;
3129 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003130 kvfree(pages);
3131 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07003132 return 0;
3133err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003134 kvfree(pages);
3135 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07003136 io_sqe_buffer_unregister(ctx);
3137 return ret;
3138}
3139
Jens Axboe9b402842019-04-11 11:45:41 -06003140static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
3141{
3142 __s32 __user *fds = arg;
3143 int fd;
3144
3145 if (ctx->cq_ev_fd)
3146 return -EBUSY;
3147
3148 if (copy_from_user(&fd, fds, sizeof(*fds)))
3149 return -EFAULT;
3150
3151 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
3152 if (IS_ERR(ctx->cq_ev_fd)) {
3153 int ret = PTR_ERR(ctx->cq_ev_fd);
3154 ctx->cq_ev_fd = NULL;
3155 return ret;
3156 }
3157
3158 return 0;
3159}
3160
3161static int io_eventfd_unregister(struct io_ring_ctx *ctx)
3162{
3163 if (ctx->cq_ev_fd) {
3164 eventfd_ctx_put(ctx->cq_ev_fd);
3165 ctx->cq_ev_fd = NULL;
3166 return 0;
3167 }
3168
3169 return -ENXIO;
3170}
3171
Jens Axboe2b188cc2019-01-07 10:46:33 -07003172static void io_ring_ctx_free(struct io_ring_ctx *ctx)
3173{
Jens Axboe6b063142019-01-10 22:13:58 -07003174 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003175 if (ctx->sqo_mm)
3176 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07003177
3178 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07003179 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07003180 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06003181 io_eventfd_unregister(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07003182
Jens Axboe2b188cc2019-01-07 10:46:33 -07003183#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07003184 if (ctx->ring_sock) {
3185 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07003186 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07003187 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003188#endif
3189
Hristo Venev75b28af2019-08-26 17:23:46 +00003190 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003191 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003192
3193 percpu_ref_exit(&ctx->refs);
3194 if (ctx->account_mem)
3195 io_unaccount_mem(ctx->user,
3196 ring_pages(ctx->sq_entries, ctx->cq_entries));
3197 free_uid(ctx->user);
3198 kfree(ctx);
3199}
3200
3201static __poll_t io_uring_poll(struct file *file, poll_table *wait)
3202{
3203 struct io_ring_ctx *ctx = file->private_data;
3204 __poll_t mask = 0;
3205
3206 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02003207 /*
3208 * synchronizes with barrier from wq_has_sleeper call in
3209 * io_commit_cqring
3210 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07003211 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00003212 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
3213 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003214 mask |= EPOLLOUT | EPOLLWRNORM;
Hristo Venev75b28af2019-08-26 17:23:46 +00003215 if (READ_ONCE(ctx->rings->sq.head) != ctx->cached_cq_tail)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003216 mask |= EPOLLIN | EPOLLRDNORM;
3217
3218 return mask;
3219}
3220
3221static int io_uring_fasync(int fd, struct file *file, int on)
3222{
3223 struct io_ring_ctx *ctx = file->private_data;
3224
3225 return fasync_helper(fd, file, on, &ctx->cq_fasync);
3226}
3227
3228static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
3229{
3230 mutex_lock(&ctx->uring_lock);
3231 percpu_ref_kill(&ctx->refs);
3232 mutex_unlock(&ctx->uring_lock);
3233
Jens Axboe221c5eb2019-01-17 09:41:58 -07003234 io_poll_remove_all(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07003235 io_iopoll_reap_events(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003236 wait_for_completion(&ctx->ctx_done);
3237 io_ring_ctx_free(ctx);
3238}
3239
3240static int io_uring_release(struct inode *inode, struct file *file)
3241{
3242 struct io_ring_ctx *ctx = file->private_data;
3243
3244 file->private_data = NULL;
3245 io_ring_ctx_wait_and_kill(ctx);
3246 return 0;
3247}
3248
3249static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
3250{
3251 loff_t offset = (loff_t) vma->vm_pgoff << PAGE_SHIFT;
3252 unsigned long sz = vma->vm_end - vma->vm_start;
3253 struct io_ring_ctx *ctx = file->private_data;
3254 unsigned long pfn;
3255 struct page *page;
3256 void *ptr;
3257
3258 switch (offset) {
3259 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00003260 case IORING_OFF_CQ_RING:
3261 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003262 break;
3263 case IORING_OFF_SQES:
3264 ptr = ctx->sq_sqes;
3265 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003266 default:
3267 return -EINVAL;
3268 }
3269
3270 page = virt_to_head_page(ptr);
3271 if (sz > (PAGE_SIZE << compound_order(page)))
3272 return -EINVAL;
3273
3274 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
3275 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
3276}
3277
3278SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
3279 u32, min_complete, u32, flags, const sigset_t __user *, sig,
3280 size_t, sigsz)
3281{
3282 struct io_ring_ctx *ctx;
3283 long ret = -EBADF;
3284 int submitted = 0;
3285 struct fd f;
3286
Jens Axboe6c271ce2019-01-10 11:22:30 -07003287 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07003288 return -EINVAL;
3289
3290 f = fdget(fd);
3291 if (!f.file)
3292 return -EBADF;
3293
3294 ret = -EOPNOTSUPP;
3295 if (f.file->f_op != &io_uring_fops)
3296 goto out_fput;
3297
3298 ret = -ENXIO;
3299 ctx = f.file->private_data;
3300 if (!percpu_ref_tryget(&ctx->refs))
3301 goto out_fput;
3302
Jens Axboe6c271ce2019-01-10 11:22:30 -07003303 /*
3304 * For SQ polling, the thread will do all submissions and completions.
3305 * Just return the requested submit count, and wake the thread if
3306 * we were asked to.
3307 */
3308 if (ctx->flags & IORING_SETUP_SQPOLL) {
3309 if (flags & IORING_ENTER_SQ_WAKEUP)
3310 wake_up(&ctx->sqo_wait);
3311 submitted = to_submit;
3312 goto out_ctx;
3313 }
3314
Jens Axboe2b188cc2019-01-07 10:46:33 -07003315 ret = 0;
3316 if (to_submit) {
Jens Axboec57666682019-09-09 16:19:45 -06003317 bool block_for_last = false;
3318
Jens Axboe2b188cc2019-01-07 10:46:33 -07003319 to_submit = min(to_submit, ctx->sq_entries);
3320
Jens Axboec57666682019-09-09 16:19:45 -06003321 /*
3322 * Allow last submission to block in a series, IFF the caller
3323 * asked to wait for events and we don't currently have
3324 * enough. This potentially avoids an async punt.
3325 */
3326 if (to_submit == min_complete &&
3327 io_cqring_events(ctx->rings) < min_complete)
3328 block_for_last = true;
3329
Jens Axboe2b188cc2019-01-07 10:46:33 -07003330 mutex_lock(&ctx->uring_lock);
Jens Axboec57666682019-09-09 16:19:45 -06003331 submitted = io_ring_submit(ctx, to_submit, block_for_last);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003332 mutex_unlock(&ctx->uring_lock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003333 }
3334 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07003335 unsigned nr_events = 0;
3336
Jens Axboe2b188cc2019-01-07 10:46:33 -07003337 min_complete = min(min_complete, ctx->cq_entries);
3338
Jens Axboedef596e2019-01-09 08:59:42 -07003339 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07003340 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07003341 } else {
3342 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
3343 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003344 }
3345
3346out_ctx:
3347 io_ring_drop_ctx_refs(ctx, 1);
3348out_fput:
3349 fdput(f);
3350 return submitted ? submitted : ret;
3351}
3352
3353static const struct file_operations io_uring_fops = {
3354 .release = io_uring_release,
3355 .mmap = io_uring_mmap,
3356 .poll = io_uring_poll,
3357 .fasync = io_uring_fasync,
3358};
3359
3360static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
3361 struct io_uring_params *p)
3362{
Hristo Venev75b28af2019-08-26 17:23:46 +00003363 struct io_rings *rings;
3364 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003365
Hristo Venev75b28af2019-08-26 17:23:46 +00003366 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
3367 if (size == SIZE_MAX)
3368 return -EOVERFLOW;
3369
3370 rings = io_mem_alloc(size);
3371 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003372 return -ENOMEM;
3373
Hristo Venev75b28af2019-08-26 17:23:46 +00003374 ctx->rings = rings;
3375 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
3376 rings->sq_ring_mask = p->sq_entries - 1;
3377 rings->cq_ring_mask = p->cq_entries - 1;
3378 rings->sq_ring_entries = p->sq_entries;
3379 rings->cq_ring_entries = p->cq_entries;
3380 ctx->sq_mask = rings->sq_ring_mask;
3381 ctx->cq_mask = rings->cq_ring_mask;
3382 ctx->sq_entries = rings->sq_ring_entries;
3383 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003384
3385 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
3386 if (size == SIZE_MAX)
3387 return -EOVERFLOW;
3388
3389 ctx->sq_sqes = io_mem_alloc(size);
Mark Rutland52e04ef2019-04-30 17:30:21 +01003390 if (!ctx->sq_sqes)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003391 return -ENOMEM;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003392
Jens Axboe2b188cc2019-01-07 10:46:33 -07003393 return 0;
3394}
3395
3396/*
3397 * Allocate an anonymous fd, this is what constitutes the application
3398 * visible backing of an io_uring instance. The application mmaps this
3399 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
3400 * we have to tie this fd to a socket for file garbage collection purposes.
3401 */
3402static int io_uring_get_fd(struct io_ring_ctx *ctx)
3403{
3404 struct file *file;
3405 int ret;
3406
3407#if defined(CONFIG_UNIX)
3408 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
3409 &ctx->ring_sock);
3410 if (ret)
3411 return ret;
3412#endif
3413
3414 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
3415 if (ret < 0)
3416 goto err;
3417
3418 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
3419 O_RDWR | O_CLOEXEC);
3420 if (IS_ERR(file)) {
3421 put_unused_fd(ret);
3422 ret = PTR_ERR(file);
3423 goto err;
3424 }
3425
3426#if defined(CONFIG_UNIX)
3427 ctx->ring_sock->file = file;
Jens Axboe6b063142019-01-10 22:13:58 -07003428 ctx->ring_sock->sk->sk_user_data = ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003429#endif
3430 fd_install(ret, file);
3431 return ret;
3432err:
3433#if defined(CONFIG_UNIX)
3434 sock_release(ctx->ring_sock);
3435 ctx->ring_sock = NULL;
3436#endif
3437 return ret;
3438}
3439
3440static int io_uring_create(unsigned entries, struct io_uring_params *p)
3441{
3442 struct user_struct *user = NULL;
3443 struct io_ring_ctx *ctx;
3444 bool account_mem;
3445 int ret;
3446
3447 if (!entries || entries > IORING_MAX_ENTRIES)
3448 return -EINVAL;
3449
3450 /*
3451 * Use twice as many entries for the CQ ring. It's possible for the
3452 * application to drive a higher depth than the size of the SQ ring,
3453 * since the sqes are only used at submission time. This allows for
3454 * some flexibility in overcommitting a bit.
3455 */
3456 p->sq_entries = roundup_pow_of_two(entries);
3457 p->cq_entries = 2 * p->sq_entries;
3458
3459 user = get_uid(current_user());
3460 account_mem = !capable(CAP_IPC_LOCK);
3461
3462 if (account_mem) {
3463 ret = io_account_mem(user,
3464 ring_pages(p->sq_entries, p->cq_entries));
3465 if (ret) {
3466 free_uid(user);
3467 return ret;
3468 }
3469 }
3470
3471 ctx = io_ring_ctx_alloc(p);
3472 if (!ctx) {
3473 if (account_mem)
3474 io_unaccount_mem(user, ring_pages(p->sq_entries,
3475 p->cq_entries));
3476 free_uid(user);
3477 return -ENOMEM;
3478 }
3479 ctx->compat = in_compat_syscall();
3480 ctx->account_mem = account_mem;
3481 ctx->user = user;
3482
3483 ret = io_allocate_scq_urings(ctx, p);
3484 if (ret)
3485 goto err;
3486
Jens Axboe6c271ce2019-01-10 11:22:30 -07003487 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003488 if (ret)
3489 goto err;
3490
3491 ret = io_uring_get_fd(ctx);
3492 if (ret < 0)
3493 goto err;
3494
3495 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00003496 p->sq_off.head = offsetof(struct io_rings, sq.head);
3497 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
3498 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
3499 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
3500 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
3501 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
3502 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003503
3504 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00003505 p->cq_off.head = offsetof(struct io_rings, cq.head);
3506 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
3507 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
3508 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
3509 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
3510 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06003511
3512 p->features = IORING_FEAT_SINGLE_MMAP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003513 return ret;
3514err:
3515 io_ring_ctx_wait_and_kill(ctx);
3516 return ret;
3517}
3518
3519/*
3520 * Sets up an aio uring context, and returns the fd. Applications asks for a
3521 * ring size, we return the actual sq/cq ring sizes (among other things) in the
3522 * params structure passed in.
3523 */
3524static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
3525{
3526 struct io_uring_params p;
3527 long ret;
3528 int i;
3529
3530 if (copy_from_user(&p, params, sizeof(p)))
3531 return -EFAULT;
3532 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
3533 if (p.resv[i])
3534 return -EINVAL;
3535 }
3536
Jens Axboe6c271ce2019-01-10 11:22:30 -07003537 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
3538 IORING_SETUP_SQ_AFF))
Jens Axboe2b188cc2019-01-07 10:46:33 -07003539 return -EINVAL;
3540
3541 ret = io_uring_create(entries, &p);
3542 if (ret < 0)
3543 return ret;
3544
3545 if (copy_to_user(params, &p, sizeof(p)))
3546 return -EFAULT;
3547
3548 return ret;
3549}
3550
3551SYSCALL_DEFINE2(io_uring_setup, u32, entries,
3552 struct io_uring_params __user *, params)
3553{
3554 return io_uring_setup(entries, params);
3555}
3556
Jens Axboeedafcce2019-01-09 09:16:05 -07003557static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
3558 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06003559 __releases(ctx->uring_lock)
3560 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07003561{
3562 int ret;
3563
Jens Axboe35fa71a2019-04-22 10:23:23 -06003564 /*
3565 * We're inside the ring mutex, if the ref is already dying, then
3566 * someone else killed the ctx or is already going through
3567 * io_uring_register().
3568 */
3569 if (percpu_ref_is_dying(&ctx->refs))
3570 return -ENXIO;
3571
Jens Axboeedafcce2019-01-09 09:16:05 -07003572 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06003573
3574 /*
3575 * Drop uring mutex before waiting for references to exit. If another
3576 * thread is currently inside io_uring_enter() it might need to grab
3577 * the uring_lock to make progress. If we hold it here across the drain
3578 * wait, then we can deadlock. It's safe to drop the mutex here, since
3579 * no new references will come in after we've killed the percpu ref.
3580 */
3581 mutex_unlock(&ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07003582 wait_for_completion(&ctx->ctx_done);
Jens Axboeb19062a2019-04-15 10:49:38 -06003583 mutex_lock(&ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07003584
3585 switch (opcode) {
3586 case IORING_REGISTER_BUFFERS:
3587 ret = io_sqe_buffer_register(ctx, arg, nr_args);
3588 break;
3589 case IORING_UNREGISTER_BUFFERS:
3590 ret = -EINVAL;
3591 if (arg || nr_args)
3592 break;
3593 ret = io_sqe_buffer_unregister(ctx);
3594 break;
Jens Axboe6b063142019-01-10 22:13:58 -07003595 case IORING_REGISTER_FILES:
3596 ret = io_sqe_files_register(ctx, arg, nr_args);
3597 break;
3598 case IORING_UNREGISTER_FILES:
3599 ret = -EINVAL;
3600 if (arg || nr_args)
3601 break;
3602 ret = io_sqe_files_unregister(ctx);
3603 break;
Jens Axboe9b402842019-04-11 11:45:41 -06003604 case IORING_REGISTER_EVENTFD:
3605 ret = -EINVAL;
3606 if (nr_args != 1)
3607 break;
3608 ret = io_eventfd_register(ctx, arg);
3609 break;
3610 case IORING_UNREGISTER_EVENTFD:
3611 ret = -EINVAL;
3612 if (arg || nr_args)
3613 break;
3614 ret = io_eventfd_unregister(ctx);
3615 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07003616 default:
3617 ret = -EINVAL;
3618 break;
3619 }
3620
3621 /* bring the ctx back to life */
3622 reinit_completion(&ctx->ctx_done);
3623 percpu_ref_reinit(&ctx->refs);
3624 return ret;
3625}
3626
3627SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
3628 void __user *, arg, unsigned int, nr_args)
3629{
3630 struct io_ring_ctx *ctx;
3631 long ret = -EBADF;
3632 struct fd f;
3633
3634 f = fdget(fd);
3635 if (!f.file)
3636 return -EBADF;
3637
3638 ret = -EOPNOTSUPP;
3639 if (f.file->f_op != &io_uring_fops)
3640 goto out_fput;
3641
3642 ctx = f.file->private_data;
3643
3644 mutex_lock(&ctx->uring_lock);
3645 ret = __io_uring_register(ctx, opcode, arg, nr_args);
3646 mutex_unlock(&ctx->uring_lock);
3647out_fput:
3648 fdput(f);
3649 return ret;
3650}
3651
Jens Axboe2b188cc2019-01-07 10:46:33 -07003652static int __init io_uring_init(void)
3653{
3654 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
3655 return 0;
3656};
3657__initcall(io_uring_init);