blob: eff29d705a2613a6f3675ed01465cf8d12d7e2cf [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 Axboede0617e2019-04-06 21:51:27 -0600446static void io_commit_cqring(struct io_ring_ctx *ctx)
447{
448 struct io_kiocb *req;
449
450 __io_commit_cqring(ctx);
451
452 while ((req = io_get_deferred_req(ctx)) != NULL) {
Jackie Liu4fe2c962019-09-09 20:50:40 +0800453 if (req->flags & REQ_F_SHADOW_DRAIN) {
454 /* Just for drain, free it. */
455 __io_free_req(req);
456 continue;
457 }
Jens Axboede0617e2019-04-06 21:51:27 -0600458 req->flags |= REQ_F_IO_DRAINED;
459 queue_work(ctx->sqo_wq, &req->work);
460 }
461}
462
Jens Axboe2b188cc2019-01-07 10:46:33 -0700463static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
464{
Hristo Venev75b28af2019-08-26 17:23:46 +0000465 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700466 unsigned tail;
467
468 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +0200469 /*
470 * writes to the cq entry need to come after reading head; the
471 * control dependency is enough as we're using WRITE_ONCE to
472 * fill the cq entry
473 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000474 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700475 return NULL;
476
477 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +0000478 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -0700479}
480
481static void io_cqring_fill_event(struct io_ring_ctx *ctx, u64 ki_user_data,
Jens Axboec71ffb62019-05-13 20:58:29 -0600482 long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700483{
484 struct io_uring_cqe *cqe;
485
486 /*
487 * If we can't get a cq entry, userspace overflowed the
488 * submission (by quite a lot). Increment the overflow count in
489 * the ring.
490 */
491 cqe = io_get_cqring(ctx);
492 if (cqe) {
493 WRITE_ONCE(cqe->user_data, ki_user_data);
494 WRITE_ONCE(cqe->res, res);
Jens Axboec71ffb62019-05-13 20:58:29 -0600495 WRITE_ONCE(cqe->flags, 0);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700496 } else {
Hristo Venev75b28af2019-08-26 17:23:46 +0000497 unsigned overflow = READ_ONCE(ctx->rings->cq_overflow);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700498
Hristo Venev75b28af2019-08-26 17:23:46 +0000499 WRITE_ONCE(ctx->rings->cq_overflow, overflow + 1);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700500 }
501}
502
Jens Axboe8c838782019-03-12 15:48:16 -0600503static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
504{
505 if (waitqueue_active(&ctx->wait))
506 wake_up(&ctx->wait);
507 if (waitqueue_active(&ctx->sqo_wait))
508 wake_up(&ctx->sqo_wait);
Jens Axboe9b402842019-04-11 11:45:41 -0600509 if (ctx->cq_ev_fd)
510 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -0600511}
512
513static void io_cqring_add_event(struct io_ring_ctx *ctx, u64 user_data,
Jens Axboec71ffb62019-05-13 20:58:29 -0600514 long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700515{
516 unsigned long flags;
517
518 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboec71ffb62019-05-13 20:58:29 -0600519 io_cqring_fill_event(ctx, user_data, res);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700520 io_commit_cqring(ctx);
521 spin_unlock_irqrestore(&ctx->completion_lock, flags);
522
Jens Axboe8c838782019-03-12 15:48:16 -0600523 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700524}
525
526static void io_ring_drop_ctx_refs(struct io_ring_ctx *ctx, unsigned refs)
527{
528 percpu_ref_put_many(&ctx->refs, refs);
529
530 if (waitqueue_active(&ctx->wait))
531 wake_up(&ctx->wait);
532}
533
Jens Axboe2579f912019-01-09 09:10:43 -0700534static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
535 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700536{
Jens Axboefd6fab22019-03-14 16:30:06 -0600537 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700538 struct io_kiocb *req;
539
540 if (!percpu_ref_tryget(&ctx->refs))
541 return NULL;
542
Jens Axboe2579f912019-01-09 09:10:43 -0700543 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -0600544 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -0700545 if (unlikely(!req))
546 goto out;
547 } else if (!state->free_reqs) {
548 size_t sz;
549 int ret;
550
551 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -0600552 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
553
554 /*
555 * Bulk alloc is all-or-nothing. If we fail to get a batch,
556 * retry single alloc to be on the safe side.
557 */
558 if (unlikely(ret <= 0)) {
559 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
560 if (!state->reqs[0])
561 goto out;
562 ret = 1;
563 }
Jens Axboe2579f912019-01-09 09:10:43 -0700564 state->free_reqs = ret - 1;
565 state->cur_req = 1;
566 req = state->reqs[0];
567 } else {
568 req = state->reqs[state->cur_req];
569 state->free_reqs--;
570 state->cur_req++;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700571 }
572
Jens Axboe60c112b2019-06-21 10:20:18 -0600573 req->file = NULL;
Jens Axboe2579f912019-01-09 09:10:43 -0700574 req->ctx = ctx;
575 req->flags = 0;
Jens Axboee65ef562019-03-12 10:16:44 -0600576 /* one is dropped after submission, the other at completion */
577 refcount_set(&req->refs, 2);
Jens Axboe9e645e112019-05-10 16:07:28 -0600578 req->result = 0;
Jens Axboe2579f912019-01-09 09:10:43 -0700579 return req;
580out:
Jens Axboe2b188cc2019-01-07 10:46:33 -0700581 io_ring_drop_ctx_refs(ctx, 1);
582 return NULL;
583}
584
Jens Axboedef596e2019-01-09 08:59:42 -0700585static void io_free_req_many(struct io_ring_ctx *ctx, void **reqs, int *nr)
586{
587 if (*nr) {
588 kmem_cache_free_bulk(req_cachep, *nr, reqs);
589 io_ring_drop_ctx_refs(ctx, *nr);
590 *nr = 0;
591 }
592}
593
Jens Axboe9e645e112019-05-10 16:07:28 -0600594static void __io_free_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700595{
Jens Axboe09bb8392019-03-13 12:39:28 -0600596 if (req->file && !(req->flags & REQ_F_FIXED_FILE))
597 fput(req->file);
Jens Axboee65ef562019-03-12 10:16:44 -0600598 io_ring_drop_ctx_refs(req->ctx, 1);
599 kmem_cache_free(req_cachep, req);
600}
601
Jens Axboe9e645e112019-05-10 16:07:28 -0600602static void io_req_link_next(struct io_kiocb *req)
603{
604 struct io_kiocb *nxt;
605
606 /*
607 * The list should never be empty when we are called here. But could
608 * potentially happen if the chain is messed up, check to be on the
609 * safe side.
610 */
611 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, list);
612 if (nxt) {
613 list_del(&nxt->list);
614 if (!list_empty(&req->link_list)) {
615 INIT_LIST_HEAD(&nxt->link_list);
616 list_splice(&req->link_list, &nxt->link_list);
617 nxt->flags |= REQ_F_LINK;
618 }
619
Zhengyuan Liuf7b76ac2019-07-16 23:26:14 +0800620 nxt->flags |= REQ_F_LINK_DONE;
Jens Axboe9e645e112019-05-10 16:07:28 -0600621 INIT_WORK(&nxt->work, io_sq_wq_submit_work);
622 queue_work(req->ctx->sqo_wq, &nxt->work);
623 }
624}
625
626/*
627 * Called if REQ_F_LINK is set, and we fail the head request
628 */
629static void io_fail_links(struct io_kiocb *req)
630{
631 struct io_kiocb *link;
632
633 while (!list_empty(&req->link_list)) {
634 link = list_first_entry(&req->link_list, struct io_kiocb, list);
635 list_del(&link->list);
636
637 io_cqring_add_event(req->ctx, link->user_data, -ECANCELED);
638 __io_free_req(link);
639 }
640}
641
642static void io_free_req(struct io_kiocb *req)
643{
644 /*
645 * If LINK is set, we have dependent requests in this chain. If we
646 * didn't fail this request, queue the first one up, moving any other
647 * dependencies to the next request. In case of failure, fail the rest
648 * of the chain.
649 */
650 if (req->flags & REQ_F_LINK) {
651 if (req->flags & REQ_F_FAIL_LINK)
652 io_fail_links(req);
653 else
654 io_req_link_next(req);
655 }
656
657 __io_free_req(req);
658}
659
Jens Axboee65ef562019-03-12 10:16:44 -0600660static void io_put_req(struct io_kiocb *req)
661{
662 if (refcount_dec_and_test(&req->refs))
663 io_free_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700664}
665
Hristo Venev75b28af2019-08-26 17:23:46 +0000666static unsigned io_cqring_events(struct io_rings *rings)
Jens Axboea3a0e432019-08-20 11:03:11 -0600667{
668 /* See comment at the top of this file */
669 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +0000670 return READ_ONCE(rings->cq.tail) - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -0600671}
672
Jens Axboedef596e2019-01-09 08:59:42 -0700673/*
674 * Find and free completed poll iocbs
675 */
676static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
677 struct list_head *done)
678{
679 void *reqs[IO_IOPOLL_BATCH];
680 struct io_kiocb *req;
Jens Axboe09bb8392019-03-13 12:39:28 -0600681 int to_free;
Jens Axboedef596e2019-01-09 08:59:42 -0700682
Jens Axboe09bb8392019-03-13 12:39:28 -0600683 to_free = 0;
Jens Axboedef596e2019-01-09 08:59:42 -0700684 while (!list_empty(done)) {
685 req = list_first_entry(done, struct io_kiocb, list);
686 list_del(&req->list);
687
Jens Axboe9e645e112019-05-10 16:07:28 -0600688 io_cqring_fill_event(ctx, req->user_data, req->result);
Jens Axboedef596e2019-01-09 08:59:42 -0700689 (*nr_events)++;
690
Jens Axboe09bb8392019-03-13 12:39:28 -0600691 if (refcount_dec_and_test(&req->refs)) {
692 /* If we're not using fixed files, we have to pair the
693 * completion part with the file put. Use regular
694 * completions for those, only batch free for fixed
Jens Axboe9e645e112019-05-10 16:07:28 -0600695 * file and non-linked commands.
Jens Axboe09bb8392019-03-13 12:39:28 -0600696 */
Jens Axboe9e645e112019-05-10 16:07:28 -0600697 if ((req->flags & (REQ_F_FIXED_FILE|REQ_F_LINK)) ==
698 REQ_F_FIXED_FILE) {
Jens Axboe09bb8392019-03-13 12:39:28 -0600699 reqs[to_free++] = req;
700 if (to_free == ARRAY_SIZE(reqs))
701 io_free_req_many(ctx, reqs, &to_free);
Jens Axboe6b063142019-01-10 22:13:58 -0700702 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -0600703 io_free_req(req);
Jens Axboe6b063142019-01-10 22:13:58 -0700704 }
Jens Axboe9a56a232019-01-09 09:06:50 -0700705 }
Jens Axboedef596e2019-01-09 08:59:42 -0700706 }
Jens Axboedef596e2019-01-09 08:59:42 -0700707
Jens Axboe09bb8392019-03-13 12:39:28 -0600708 io_commit_cqring(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -0700709 io_free_req_many(ctx, reqs, &to_free);
710}
711
712static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
713 long min)
714{
715 struct io_kiocb *req, *tmp;
716 LIST_HEAD(done);
717 bool spin;
718 int ret;
719
720 /*
721 * Only spin for completions if we don't have multiple devices hanging
722 * off our complete list, and we're under the requested amount.
723 */
724 spin = !ctx->poll_multi_file && *nr_events < min;
725
726 ret = 0;
727 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
728 struct kiocb *kiocb = &req->rw;
729
730 /*
731 * Move completed entries to our local list. If we find a
732 * request that requires polling, break out and complete
733 * the done list first, if we have entries there.
734 */
735 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
736 list_move_tail(&req->list, &done);
737 continue;
738 }
739 if (!list_empty(&done))
740 break;
741
742 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
743 if (ret < 0)
744 break;
745
746 if (ret && spin)
747 spin = false;
748 ret = 0;
749 }
750
751 if (!list_empty(&done))
752 io_iopoll_complete(ctx, nr_events, &done);
753
754 return ret;
755}
756
757/*
758 * Poll for a mininum of 'min' events. Note that if min == 0 we consider that a
759 * non-spinning poll check - we'll still enter the driver poll loop, but only
760 * as a non-spinning completion check.
761 */
762static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
763 long min)
764{
Jens Axboe08f54392019-08-21 22:19:11 -0600765 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -0700766 int ret;
767
768 ret = io_do_iopoll(ctx, nr_events, min);
769 if (ret < 0)
770 return ret;
771 if (!min || *nr_events >= min)
772 return 0;
773 }
774
775 return 1;
776}
777
778/*
779 * We can't just wait for polled events to come to us, we have to actively
780 * find and complete them.
781 */
782static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
783{
784 if (!(ctx->flags & IORING_SETUP_IOPOLL))
785 return;
786
787 mutex_lock(&ctx->uring_lock);
788 while (!list_empty(&ctx->poll_list)) {
789 unsigned int nr_events = 0;
790
791 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -0600792
793 /*
794 * Ensure we allow local-to-the-cpu processing to take place,
795 * in this case we need to ensure that we reap all events.
796 */
797 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -0700798 }
799 mutex_unlock(&ctx->uring_lock);
800}
801
802static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
803 long min)
804{
Jens Axboe500f9fb2019-08-19 12:15:59 -0600805 int iters, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -0700806
Jens Axboe500f9fb2019-08-19 12:15:59 -0600807 /*
808 * We disallow the app entering submit/complete with polling, but we
809 * still need to lock the ring to prevent racing with polled issue
810 * that got punted to a workqueue.
811 */
812 mutex_lock(&ctx->uring_lock);
813
814 iters = 0;
Jens Axboedef596e2019-01-09 08:59:42 -0700815 do {
816 int tmin = 0;
817
Jens Axboe500f9fb2019-08-19 12:15:59 -0600818 /*
Jens Axboea3a0e432019-08-20 11:03:11 -0600819 * Don't enter poll loop if we already have events pending.
820 * If we do, we can potentially be spinning for commands that
821 * already triggered a CQE (eg in error).
822 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000823 if (io_cqring_events(ctx->rings))
Jens Axboea3a0e432019-08-20 11:03:11 -0600824 break;
825
826 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -0600827 * If a submit got punted to a workqueue, we can have the
828 * application entering polling for a command before it gets
829 * issued. That app will hold the uring_lock for the duration
830 * of the poll right here, so we need to take a breather every
831 * now and then to ensure that the issue has a chance to add
832 * the poll to the issued list. Otherwise we can spin here
833 * forever, while the workqueue is stuck trying to acquire the
834 * very same mutex.
835 */
836 if (!(++iters & 7)) {
837 mutex_unlock(&ctx->uring_lock);
838 mutex_lock(&ctx->uring_lock);
839 }
840
Jens Axboedef596e2019-01-09 08:59:42 -0700841 if (*nr_events < min)
842 tmin = min - *nr_events;
843
844 ret = io_iopoll_getevents(ctx, nr_events, tmin);
845 if (ret <= 0)
846 break;
847 ret = 0;
848 } while (min && !*nr_events && !need_resched());
849
Jens Axboe500f9fb2019-08-19 12:15:59 -0600850 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -0700851 return ret;
852}
853
Jens Axboe2b188cc2019-01-07 10:46:33 -0700854static void kiocb_end_write(struct kiocb *kiocb)
855{
856 if (kiocb->ki_flags & IOCB_WRITE) {
857 struct inode *inode = file_inode(kiocb->ki_filp);
858
859 /*
860 * Tell lockdep we inherited freeze protection from submission
861 * thread.
862 */
863 if (S_ISREG(inode->i_mode))
864 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
865 file_end_write(kiocb->ki_filp);
866 }
867}
868
869static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
870{
871 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
872
873 kiocb_end_write(kiocb);
874
Jens Axboe9e645e112019-05-10 16:07:28 -0600875 if ((req->flags & REQ_F_LINK) && res != req->result)
876 req->flags |= REQ_F_FAIL_LINK;
Jens Axboec71ffb62019-05-13 20:58:29 -0600877 io_cqring_add_event(req->ctx, req->user_data, res);
Jens Axboee65ef562019-03-12 10:16:44 -0600878 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700879}
880
Jens Axboedef596e2019-01-09 08:59:42 -0700881static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
882{
883 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
884
885 kiocb_end_write(kiocb);
886
Jens Axboe9e645e112019-05-10 16:07:28 -0600887 if ((req->flags & REQ_F_LINK) && res != req->result)
888 req->flags |= REQ_F_FAIL_LINK;
889 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -0700890 if (res != -EAGAIN)
891 req->flags |= REQ_F_IOPOLL_COMPLETED;
892}
893
894/*
895 * After the iocb has been issued, it's safe to be found on the poll list.
896 * Adding the kiocb to the list AFTER submission ensures that we don't
897 * find it from a io_iopoll_getevents() thread before the issuer is done
898 * accessing the kiocb cookie.
899 */
900static void io_iopoll_req_issued(struct io_kiocb *req)
901{
902 struct io_ring_ctx *ctx = req->ctx;
903
904 /*
905 * Track whether we have multiple files in our lists. This will impact
906 * how we do polling eventually, not spinning if we're on potentially
907 * different devices.
908 */
909 if (list_empty(&ctx->poll_list)) {
910 ctx->poll_multi_file = false;
911 } else if (!ctx->poll_multi_file) {
912 struct io_kiocb *list_req;
913
914 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
915 list);
916 if (list_req->rw.ki_filp != req->rw.ki_filp)
917 ctx->poll_multi_file = true;
918 }
919
920 /*
921 * For fast devices, IO may have already completed. If it has, add
922 * it to the front so we find it first.
923 */
924 if (req->flags & REQ_F_IOPOLL_COMPLETED)
925 list_add(&req->list, &ctx->poll_list);
926 else
927 list_add_tail(&req->list, &ctx->poll_list);
928}
929
Jens Axboe3d6770f2019-04-13 11:50:54 -0600930static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -0700931{
Jens Axboe3d6770f2019-04-13 11:50:54 -0600932 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -0700933 int diff = state->has_refs - state->used_refs;
934
935 if (diff)
936 fput_many(state->file, diff);
937 state->file = NULL;
938 }
939}
940
941/*
942 * Get as many references to a file as we have IOs left in this submission,
943 * assuming most submissions are for one file, or at least that each file
944 * has more than one submission.
945 */
946static struct file *io_file_get(struct io_submit_state *state, int fd)
947{
948 if (!state)
949 return fget(fd);
950
951 if (state->file) {
952 if (state->fd == fd) {
953 state->used_refs++;
954 state->ios_left--;
955 return state->file;
956 }
Jens Axboe3d6770f2019-04-13 11:50:54 -0600957 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -0700958 }
959 state->file = fget_many(fd, state->ios_left);
960 if (!state->file)
961 return NULL;
962
963 state->fd = fd;
964 state->has_refs = state->ios_left;
965 state->used_refs = 1;
966 state->ios_left--;
967 return state->file;
968}
969
Jens Axboe2b188cc2019-01-07 10:46:33 -0700970/*
971 * If we tracked the file through the SCM inflight mechanism, we could support
972 * any file. For now, just ensure that anything potentially problematic is done
973 * inline.
974 */
975static bool io_file_supports_async(struct file *file)
976{
977 umode_t mode = file_inode(file)->i_mode;
978
979 if (S_ISBLK(mode) || S_ISCHR(mode))
980 return true;
981 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
982 return true;
983
984 return false;
985}
986
Jens Axboe6c271ce2019-01-10 11:22:30 -0700987static int io_prep_rw(struct io_kiocb *req, const struct sqe_submit *s,
Jens Axboe8358e3a2019-04-23 08:17:58 -0600988 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700989{
Jens Axboe6c271ce2019-01-10 11:22:30 -0700990 const struct io_uring_sqe *sqe = s->sqe;
Jens Axboedef596e2019-01-09 08:59:42 -0700991 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700992 struct kiocb *kiocb = &req->rw;
Jens Axboe09bb8392019-03-13 12:39:28 -0600993 unsigned ioprio;
994 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700995
Jens Axboe09bb8392019-03-13 12:39:28 -0600996 if (!req->file)
997 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700998
Jens Axboe09bb8392019-03-13 12:39:28 -0600999 if (force_nonblock && !io_file_supports_async(req->file))
1000 force_nonblock = false;
Jens Axboe6b063142019-01-10 22:13:58 -07001001
Jens Axboe2b188cc2019-01-07 10:46:33 -07001002 kiocb->ki_pos = READ_ONCE(sqe->off);
1003 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
1004 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
1005
1006 ioprio = READ_ONCE(sqe->ioprio);
1007 if (ioprio) {
1008 ret = ioprio_check_cap(ioprio);
1009 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06001010 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001011
1012 kiocb->ki_ioprio = ioprio;
1013 } else
1014 kiocb->ki_ioprio = get_current_ioprio();
1015
1016 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
1017 if (unlikely(ret))
Jens Axboe09bb8392019-03-13 12:39:28 -06001018 return ret;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001019
1020 /* don't allow async punt if RWF_NOWAIT was requested */
1021 if (kiocb->ki_flags & IOCB_NOWAIT)
1022 req->flags |= REQ_F_NOWAIT;
1023
1024 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001025 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001026
Jens Axboedef596e2019-01-09 08:59:42 -07001027 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07001028 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
1029 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06001030 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001031
Jens Axboedef596e2019-01-09 08:59:42 -07001032 kiocb->ki_flags |= IOCB_HIPRI;
1033 kiocb->ki_complete = io_complete_rw_iopoll;
1034 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001035 if (kiocb->ki_flags & IOCB_HIPRI)
1036 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07001037 kiocb->ki_complete = io_complete_rw;
1038 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001039 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001040}
1041
1042static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
1043{
1044 switch (ret) {
1045 case -EIOCBQUEUED:
1046 break;
1047 case -ERESTARTSYS:
1048 case -ERESTARTNOINTR:
1049 case -ERESTARTNOHAND:
1050 case -ERESTART_RESTARTBLOCK:
1051 /*
1052 * We can't just restart the syscall, since previously
1053 * submitted sqes may already be in progress. Just fail this
1054 * IO with EINTR.
1055 */
1056 ret = -EINTR;
1057 /* fall through */
1058 default:
1059 kiocb->ki_complete(kiocb, ret, 0);
1060 }
1061}
1062
Jens Axboeedafcce2019-01-09 09:16:05 -07001063static int io_import_fixed(struct io_ring_ctx *ctx, int rw,
1064 const struct io_uring_sqe *sqe,
1065 struct iov_iter *iter)
1066{
1067 size_t len = READ_ONCE(sqe->len);
1068 struct io_mapped_ubuf *imu;
1069 unsigned index, buf_index;
1070 size_t offset;
1071 u64 buf_addr;
1072
1073 /* attempt to use fixed buffers without having provided iovecs */
1074 if (unlikely(!ctx->user_bufs))
1075 return -EFAULT;
1076
1077 buf_index = READ_ONCE(sqe->buf_index);
1078 if (unlikely(buf_index >= ctx->nr_user_bufs))
1079 return -EFAULT;
1080
1081 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
1082 imu = &ctx->user_bufs[index];
1083 buf_addr = READ_ONCE(sqe->addr);
1084
1085 /* overflow */
1086 if (buf_addr + len < buf_addr)
1087 return -EFAULT;
1088 /* not inside the mapped region */
1089 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
1090 return -EFAULT;
1091
1092 /*
1093 * May not be a start of buffer, set size appropriately
1094 * and advance us to the beginning.
1095 */
1096 offset = buf_addr - imu->ubuf;
1097 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06001098
1099 if (offset) {
1100 /*
1101 * Don't use iov_iter_advance() here, as it's really slow for
1102 * using the latter parts of a big fixed buffer - it iterates
1103 * over each segment manually. We can cheat a bit here, because
1104 * we know that:
1105 *
1106 * 1) it's a BVEC iter, we set it up
1107 * 2) all bvecs are PAGE_SIZE in size, except potentially the
1108 * first and last bvec
1109 *
1110 * So just find our index, and adjust the iterator afterwards.
1111 * If the offset is within the first bvec (or the whole first
1112 * bvec, just use iov_iter_advance(). This makes it easier
1113 * since we can just skip the first segment, which may not
1114 * be PAGE_SIZE aligned.
1115 */
1116 const struct bio_vec *bvec = imu->bvec;
1117
1118 if (offset <= bvec->bv_len) {
1119 iov_iter_advance(iter, offset);
1120 } else {
1121 unsigned long seg_skip;
1122
1123 /* skip first vec */
1124 offset -= bvec->bv_len;
1125 seg_skip = 1 + (offset >> PAGE_SHIFT);
1126
1127 iter->bvec = bvec + seg_skip;
1128 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02001129 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001130 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001131 }
1132 }
1133
Jens Axboeedafcce2019-01-09 09:16:05 -07001134 return 0;
1135}
1136
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001137static ssize_t io_import_iovec(struct io_ring_ctx *ctx, int rw,
1138 const struct sqe_submit *s, struct iovec **iovec,
1139 struct iov_iter *iter)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001140{
1141 const struct io_uring_sqe *sqe = s->sqe;
1142 void __user *buf = u64_to_user_ptr(READ_ONCE(sqe->addr));
1143 size_t sqe_len = READ_ONCE(sqe->len);
Jens Axboeedafcce2019-01-09 09:16:05 -07001144 u8 opcode;
1145
1146 /*
1147 * We're reading ->opcode for the second time, but the first read
1148 * doesn't care whether it's _FIXED or not, so it doesn't matter
1149 * whether ->opcode changes concurrently. The first read does care
1150 * about whether it is a READ or a WRITE, so we don't trust this read
1151 * for that purpose and instead let the caller pass in the read/write
1152 * flag.
1153 */
1154 opcode = READ_ONCE(sqe->opcode);
1155 if (opcode == IORING_OP_READ_FIXED ||
1156 opcode == IORING_OP_WRITE_FIXED) {
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001157 ssize_t ret = io_import_fixed(ctx, rw, sqe, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07001158 *iovec = NULL;
1159 return ret;
1160 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001161
1162 if (!s->has_user)
1163 return -EFAULT;
1164
1165#ifdef CONFIG_COMPAT
1166 if (ctx->compat)
1167 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
1168 iovec, iter);
1169#endif
1170
1171 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
1172}
1173
Jens Axboe31b51512019-01-18 22:56:34 -07001174/*
1175 * Make a note of the last file/offset/direction we punted to async
1176 * context. We'll use this information to see if we can piggy back a
1177 * sequential request onto the previous one, if it's still hasn't been
1178 * completed by the async worker.
1179 */
1180static void io_async_list_note(int rw, struct io_kiocb *req, size_t len)
1181{
1182 struct async_list *async_list = &req->ctx->pending_async[rw];
1183 struct kiocb *kiocb = &req->rw;
1184 struct file *filp = kiocb->ki_filp;
1185 off_t io_end = kiocb->ki_pos + len;
1186
1187 if (filp == async_list->file && kiocb->ki_pos == async_list->io_end) {
Zhengyuan Liu9310a7ba2019-07-22 10:23:27 +08001188 unsigned long max_bytes;
Jens Axboe31b51512019-01-18 22:56:34 -07001189
1190 /* Use 8x RA size as a decent limiter for both reads/writes */
Zhengyuan Liu9310a7ba2019-07-22 10:23:27 +08001191 max_bytes = filp->f_ra.ra_pages << (PAGE_SHIFT + 3);
1192 if (!max_bytes)
1193 max_bytes = VM_READAHEAD_PAGES << (PAGE_SHIFT + 3);
Jens Axboe31b51512019-01-18 22:56:34 -07001194
Zhengyuan Liu9310a7ba2019-07-22 10:23:27 +08001195 /* If max len are exceeded, reset the state */
1196 if (async_list->io_len + len <= max_bytes) {
Jens Axboe31b51512019-01-18 22:56:34 -07001197 req->flags |= REQ_F_SEQ_PREV;
Zhengyuan Liu9310a7ba2019-07-22 10:23:27 +08001198 async_list->io_len += len;
Jens Axboe31b51512019-01-18 22:56:34 -07001199 } else {
1200 io_end = 0;
Zhengyuan Liu9310a7ba2019-07-22 10:23:27 +08001201 async_list->io_len = 0;
Jens Axboe31b51512019-01-18 22:56:34 -07001202 }
1203 }
1204
1205 /* New file? Reset state. */
1206 if (async_list->file != filp) {
Zhengyuan Liu9310a7ba2019-07-22 10:23:27 +08001207 async_list->io_len = 0;
Jens Axboe31b51512019-01-18 22:56:34 -07001208 async_list->file = filp;
1209 }
1210 async_list->io_end = io_end;
1211}
1212
Jens Axboee0c5c572019-03-12 10:18:47 -06001213static int io_read(struct io_kiocb *req, const struct sqe_submit *s,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001214 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001215{
1216 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1217 struct kiocb *kiocb = &req->rw;
1218 struct iov_iter iter;
1219 struct file *file;
Jens Axboe31b51512019-01-18 22:56:34 -07001220 size_t iov_count;
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001221 ssize_t read_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001222
Jens Axboe8358e3a2019-04-23 08:17:58 -06001223 ret = io_prep_rw(req, s, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001224 if (ret)
1225 return ret;
1226 file = kiocb->ki_filp;
1227
Jens Axboe2b188cc2019-01-07 10:46:33 -07001228 if (unlikely(!(file->f_mode & FMODE_READ)))
Jens Axboe09bb8392019-03-13 12:39:28 -06001229 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001230 if (unlikely(!file->f_op->read_iter))
Jens Axboe09bb8392019-03-13 12:39:28 -06001231 return -EINVAL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001232
1233 ret = io_import_iovec(req->ctx, READ, s, &iovec, &iter);
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001234 if (ret < 0)
Jens Axboe09bb8392019-03-13 12:39:28 -06001235 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001236
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001237 read_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06001238 if (req->flags & REQ_F_LINK)
1239 req->result = read_size;
1240
Jens Axboe31b51512019-01-18 22:56:34 -07001241 iov_count = iov_iter_count(&iter);
1242 ret = rw_verify_area(READ, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001243 if (!ret) {
1244 ssize_t ret2;
1245
Jens Axboe2b188cc2019-01-07 10:46:33 -07001246 ret2 = call_read_iter(file, kiocb, &iter);
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001247 /*
1248 * In case of a short read, punt to async. This can happen
1249 * if we have data partially cached. Alternatively we can
1250 * return the short read, in which case the application will
1251 * need to issue another SQE and wait for it. That SQE will
1252 * need async punt anyway, so it's more efficient to do it
1253 * here.
1254 */
1255 if (force_nonblock && ret2 > 0 && ret2 < read_size)
1256 ret2 = -EAGAIN;
1257 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboe31b51512019-01-18 22:56:34 -07001258 if (!force_nonblock || ret2 != -EAGAIN) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001259 io_rw_done(kiocb, ret2);
Jens Axboe31b51512019-01-18 22:56:34 -07001260 } else {
1261 /*
1262 * If ->needs_lock is true, we're already in async
1263 * context.
1264 */
1265 if (!s->needs_lock)
1266 io_async_list_note(READ, req, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001267 ret = -EAGAIN;
Jens Axboe31b51512019-01-18 22:56:34 -07001268 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001269 }
1270 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001271 return ret;
1272}
1273
Jens Axboee0c5c572019-03-12 10:18:47 -06001274static int io_write(struct io_kiocb *req, const struct sqe_submit *s,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001275 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001276{
1277 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1278 struct kiocb *kiocb = &req->rw;
1279 struct iov_iter iter;
1280 struct file *file;
Jens Axboe31b51512019-01-18 22:56:34 -07001281 size_t iov_count;
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001282 ssize_t ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001283
Jens Axboe8358e3a2019-04-23 08:17:58 -06001284 ret = io_prep_rw(req, s, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001285 if (ret)
1286 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001287
Jens Axboe2b188cc2019-01-07 10:46:33 -07001288 file = kiocb->ki_filp;
1289 if (unlikely(!(file->f_mode & FMODE_WRITE)))
Jens Axboe09bb8392019-03-13 12:39:28 -06001290 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001291 if (unlikely(!file->f_op->write_iter))
Jens Axboe09bb8392019-03-13 12:39:28 -06001292 return -EINVAL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001293
1294 ret = io_import_iovec(req->ctx, WRITE, s, &iovec, &iter);
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001295 if (ret < 0)
Jens Axboe09bb8392019-03-13 12:39:28 -06001296 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001297
Jens Axboe9e645e112019-05-10 16:07:28 -06001298 if (req->flags & REQ_F_LINK)
1299 req->result = ret;
1300
Jens Axboe31b51512019-01-18 22:56:34 -07001301 iov_count = iov_iter_count(&iter);
1302
1303 ret = -EAGAIN;
1304 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT)) {
1305 /* If ->needs_lock is true, we're already in async context. */
1306 if (!s->needs_lock)
1307 io_async_list_note(WRITE, req, iov_count);
1308 goto out_free;
1309 }
1310
1311 ret = rw_verify_area(WRITE, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001312 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01001313 ssize_t ret2;
1314
Jens Axboe2b188cc2019-01-07 10:46:33 -07001315 /*
1316 * Open-code file_start_write here to grab freeze protection,
1317 * which will be released by another thread in
1318 * io_complete_rw(). Fool lockdep by telling it the lock got
1319 * released so that it doesn't complain about the held lock when
1320 * we return to userspace.
1321 */
1322 if (S_ISREG(file_inode(file)->i_mode)) {
1323 __sb_start_write(file_inode(file)->i_sb,
1324 SB_FREEZE_WRITE, true);
1325 __sb_writers_release(file_inode(file)->i_sb,
1326 SB_FREEZE_WRITE);
1327 }
1328 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01001329
1330 ret2 = call_write_iter(file, kiocb, &iter);
1331 if (!force_nonblock || ret2 != -EAGAIN) {
1332 io_rw_done(kiocb, ret2);
1333 } else {
1334 /*
1335 * If ->needs_lock is true, we're already in async
1336 * context.
1337 */
1338 if (!s->needs_lock)
1339 io_async_list_note(WRITE, req, iov_count);
1340 ret = -EAGAIN;
1341 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001342 }
Jens Axboe31b51512019-01-18 22:56:34 -07001343out_free:
Jens Axboe2b188cc2019-01-07 10:46:33 -07001344 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001345 return ret;
1346}
1347
1348/*
1349 * IORING_OP_NOP just posts a completion event, nothing else.
1350 */
1351static int io_nop(struct io_kiocb *req, u64 user_data)
1352{
1353 struct io_ring_ctx *ctx = req->ctx;
1354 long err = 0;
1355
Jens Axboedef596e2019-01-09 08:59:42 -07001356 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1357 return -EINVAL;
1358
Jens Axboec71ffb62019-05-13 20:58:29 -06001359 io_cqring_add_event(ctx, user_data, err);
Jens Axboee65ef562019-03-12 10:16:44 -06001360 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001361 return 0;
1362}
1363
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001364static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1365{
Jens Axboe6b063142019-01-10 22:13:58 -07001366 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001367
Jens Axboe09bb8392019-03-13 12:39:28 -06001368 if (!req->file)
1369 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001370
Jens Axboe6b063142019-01-10 22:13:58 -07001371 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07001372 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07001373 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001374 return -EINVAL;
1375
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001376 return 0;
1377}
1378
1379static int io_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1380 bool force_nonblock)
1381{
1382 loff_t sqe_off = READ_ONCE(sqe->off);
1383 loff_t sqe_len = READ_ONCE(sqe->len);
1384 loff_t end = sqe_off + sqe_len;
1385 unsigned fsync_flags;
1386 int ret;
1387
1388 fsync_flags = READ_ONCE(sqe->fsync_flags);
1389 if (unlikely(fsync_flags & ~IORING_FSYNC_DATASYNC))
1390 return -EINVAL;
1391
1392 ret = io_prep_fsync(req, sqe);
1393 if (ret)
1394 return ret;
1395
1396 /* fsync always requires a blocking context */
1397 if (force_nonblock)
1398 return -EAGAIN;
1399
1400 ret = vfs_fsync_range(req->rw.ki_filp, sqe_off,
1401 end > 0 ? end : LLONG_MAX,
1402 fsync_flags & IORING_FSYNC_DATASYNC);
1403
Jens Axboe9e645e112019-05-10 16:07:28 -06001404 if (ret < 0 && (req->flags & REQ_F_LINK))
1405 req->flags |= REQ_F_FAIL_LINK;
Jens Axboec71ffb62019-05-13 20:58:29 -06001406 io_cqring_add_event(req->ctx, sqe->user_data, ret);
Jens Axboee65ef562019-03-12 10:16:44 -06001407 io_put_req(req);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001408 return 0;
1409}
1410
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001411static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1412{
1413 struct io_ring_ctx *ctx = req->ctx;
1414 int ret = 0;
1415
1416 if (!req->file)
1417 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001418
1419 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1420 return -EINVAL;
1421 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
1422 return -EINVAL;
1423
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001424 return ret;
1425}
1426
1427static int io_sync_file_range(struct io_kiocb *req,
1428 const struct io_uring_sqe *sqe,
1429 bool force_nonblock)
1430{
1431 loff_t sqe_off;
1432 loff_t sqe_len;
1433 unsigned flags;
1434 int ret;
1435
1436 ret = io_prep_sfr(req, sqe);
1437 if (ret)
1438 return ret;
1439
1440 /* sync_file_range always requires a blocking context */
1441 if (force_nonblock)
1442 return -EAGAIN;
1443
1444 sqe_off = READ_ONCE(sqe->off);
1445 sqe_len = READ_ONCE(sqe->len);
1446 flags = READ_ONCE(sqe->sync_range_flags);
1447
1448 ret = sync_file_range(req->rw.ki_filp, sqe_off, sqe_len, flags);
1449
Jens Axboe9e645e112019-05-10 16:07:28 -06001450 if (ret < 0 && (req->flags & REQ_F_LINK))
1451 req->flags |= REQ_F_FAIL_LINK;
Jens Axboec71ffb62019-05-13 20:58:29 -06001452 io_cqring_add_event(req->ctx, sqe->user_data, ret);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001453 io_put_req(req);
1454 return 0;
1455}
1456
Jens Axboe0fa03c62019-04-19 13:34:07 -06001457#if defined(CONFIG_NET)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001458static int io_send_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1459 bool force_nonblock,
1460 long (*fn)(struct socket *, struct user_msghdr __user *,
1461 unsigned int))
1462{
Jens Axboe0fa03c62019-04-19 13:34:07 -06001463 struct socket *sock;
1464 int ret;
1465
1466 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1467 return -EINVAL;
1468
1469 sock = sock_from_file(req->file, &ret);
1470 if (sock) {
1471 struct user_msghdr __user *msg;
1472 unsigned flags;
1473
1474 flags = READ_ONCE(sqe->msg_flags);
1475 if (flags & MSG_DONTWAIT)
1476 req->flags |= REQ_F_NOWAIT;
1477 else if (force_nonblock)
1478 flags |= MSG_DONTWAIT;
1479
1480 msg = (struct user_msghdr __user *) (unsigned long)
1481 READ_ONCE(sqe->addr);
1482
Jens Axboeaa1fa282019-04-19 13:38:09 -06001483 ret = fn(sock, msg, flags);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001484 if (force_nonblock && ret == -EAGAIN)
1485 return ret;
1486 }
1487
1488 io_cqring_add_event(req->ctx, sqe->user_data, ret);
1489 io_put_req(req);
1490 return 0;
Jens Axboeaa1fa282019-04-19 13:38:09 -06001491}
1492#endif
1493
1494static int io_sendmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1495 bool force_nonblock)
1496{
1497#if defined(CONFIG_NET)
1498 return io_send_recvmsg(req, sqe, force_nonblock, __sys_sendmsg_sock);
1499#else
1500 return -EOPNOTSUPP;
1501#endif
1502}
1503
1504static int io_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1505 bool force_nonblock)
1506{
1507#if defined(CONFIG_NET)
1508 return io_send_recvmsg(req, sqe, force_nonblock, __sys_recvmsg_sock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001509#else
1510 return -EOPNOTSUPP;
1511#endif
1512}
1513
Jens Axboe221c5eb2019-01-17 09:41:58 -07001514static void io_poll_remove_one(struct io_kiocb *req)
1515{
1516 struct io_poll_iocb *poll = &req->poll;
1517
1518 spin_lock(&poll->head->lock);
1519 WRITE_ONCE(poll->canceled, true);
1520 if (!list_empty(&poll->wait.entry)) {
1521 list_del_init(&poll->wait.entry);
1522 queue_work(req->ctx->sqo_wq, &req->work);
1523 }
1524 spin_unlock(&poll->head->lock);
1525
1526 list_del_init(&req->list);
1527}
1528
1529static void io_poll_remove_all(struct io_ring_ctx *ctx)
1530{
1531 struct io_kiocb *req;
1532
1533 spin_lock_irq(&ctx->completion_lock);
1534 while (!list_empty(&ctx->cancel_list)) {
1535 req = list_first_entry(&ctx->cancel_list, struct io_kiocb,list);
1536 io_poll_remove_one(req);
1537 }
1538 spin_unlock_irq(&ctx->completion_lock);
1539}
1540
1541/*
1542 * Find a running poll command that matches one specified in sqe->addr,
1543 * and remove it if found.
1544 */
1545static int io_poll_remove(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1546{
1547 struct io_ring_ctx *ctx = req->ctx;
1548 struct io_kiocb *poll_req, *next;
1549 int ret = -ENOENT;
1550
1551 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1552 return -EINVAL;
1553 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
1554 sqe->poll_events)
1555 return -EINVAL;
1556
1557 spin_lock_irq(&ctx->completion_lock);
1558 list_for_each_entry_safe(poll_req, next, &ctx->cancel_list, list) {
1559 if (READ_ONCE(sqe->addr) == poll_req->user_data) {
1560 io_poll_remove_one(poll_req);
1561 ret = 0;
1562 break;
1563 }
1564 }
1565 spin_unlock_irq(&ctx->completion_lock);
1566
Jens Axboec71ffb62019-05-13 20:58:29 -06001567 io_cqring_add_event(req->ctx, sqe->user_data, ret);
Jens Axboee65ef562019-03-12 10:16:44 -06001568 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001569 return 0;
1570}
1571
Jens Axboe8c838782019-03-12 15:48:16 -06001572static void io_poll_complete(struct io_ring_ctx *ctx, struct io_kiocb *req,
1573 __poll_t mask)
Jens Axboe221c5eb2019-01-17 09:41:58 -07001574{
Jens Axboe8c838782019-03-12 15:48:16 -06001575 req->poll.done = true;
Jens Axboec71ffb62019-05-13 20:58:29 -06001576 io_cqring_fill_event(ctx, req->user_data, mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06001577 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001578}
1579
1580static void io_poll_complete_work(struct work_struct *work)
1581{
1582 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
1583 struct io_poll_iocb *poll = &req->poll;
1584 struct poll_table_struct pt = { ._key = poll->events };
1585 struct io_ring_ctx *ctx = req->ctx;
1586 __poll_t mask = 0;
1587
1588 if (!READ_ONCE(poll->canceled))
1589 mask = vfs_poll(poll->file, &pt) & poll->events;
1590
1591 /*
1592 * Note that ->ki_cancel callers also delete iocb from active_reqs after
1593 * calling ->ki_cancel. We need the ctx_lock roundtrip here to
1594 * synchronize with them. In the cancellation case the list_del_init
1595 * itself is not actually needed, but harmless so we keep it in to
1596 * avoid further branches in the fast path.
1597 */
1598 spin_lock_irq(&ctx->completion_lock);
1599 if (!mask && !READ_ONCE(poll->canceled)) {
1600 add_wait_queue(poll->head, &poll->wait);
1601 spin_unlock_irq(&ctx->completion_lock);
1602 return;
1603 }
1604 list_del_init(&req->list);
Jens Axboe8c838782019-03-12 15:48:16 -06001605 io_poll_complete(ctx, req, mask);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001606 spin_unlock_irq(&ctx->completion_lock);
1607
Jens Axboe8c838782019-03-12 15:48:16 -06001608 io_cqring_ev_posted(ctx);
1609 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001610}
1611
1612static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
1613 void *key)
1614{
1615 struct io_poll_iocb *poll = container_of(wait, struct io_poll_iocb,
1616 wait);
1617 struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
1618 struct io_ring_ctx *ctx = req->ctx;
1619 __poll_t mask = key_to_poll(key);
Jens Axboe8c838782019-03-12 15:48:16 -06001620 unsigned long flags;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001621
1622 /* for instances that support it check for an event match first: */
Jens Axboe8c838782019-03-12 15:48:16 -06001623 if (mask && !(mask & poll->events))
1624 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001625
1626 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06001627
1628 if (mask && spin_trylock_irqsave(&ctx->completion_lock, flags)) {
1629 list_del(&req->list);
1630 io_poll_complete(ctx, req, mask);
1631 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1632
1633 io_cqring_ev_posted(ctx);
1634 io_put_req(req);
1635 } else {
1636 queue_work(ctx->sqo_wq, &req->work);
1637 }
1638
Jens Axboe221c5eb2019-01-17 09:41:58 -07001639 return 1;
1640}
1641
1642struct io_poll_table {
1643 struct poll_table_struct pt;
1644 struct io_kiocb *req;
1645 int error;
1646};
1647
1648static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
1649 struct poll_table_struct *p)
1650{
1651 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
1652
1653 if (unlikely(pt->req->poll.head)) {
1654 pt->error = -EINVAL;
1655 return;
1656 }
1657
1658 pt->error = 0;
1659 pt->req->poll.head = head;
1660 add_wait_queue(head, &pt->req->poll.wait);
1661}
1662
1663static int io_poll_add(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1664{
1665 struct io_poll_iocb *poll = &req->poll;
1666 struct io_ring_ctx *ctx = req->ctx;
1667 struct io_poll_table ipt;
Jens Axboe8c838782019-03-12 15:48:16 -06001668 bool cancel = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001669 __poll_t mask;
1670 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001671
1672 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1673 return -EINVAL;
1674 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
1675 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06001676 if (!poll->file)
1677 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001678
1679 INIT_WORK(&req->work, io_poll_complete_work);
1680 events = READ_ONCE(sqe->poll_events);
1681 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
1682
Jens Axboe221c5eb2019-01-17 09:41:58 -07001683 poll->head = NULL;
Jens Axboe8c838782019-03-12 15:48:16 -06001684 poll->done = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001685 poll->canceled = false;
1686
1687 ipt.pt._qproc = io_poll_queue_proc;
1688 ipt.pt._key = poll->events;
1689 ipt.req = req;
1690 ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
1691
1692 /* initialized the list so that we can do list_empty checks */
1693 INIT_LIST_HEAD(&poll->wait.entry);
1694 init_waitqueue_func_entry(&poll->wait, io_poll_wake);
1695
Jens Axboe36703242019-07-25 10:20:18 -06001696 INIT_LIST_HEAD(&req->list);
1697
Jens Axboe221c5eb2019-01-17 09:41:58 -07001698 mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001699
1700 spin_lock_irq(&ctx->completion_lock);
Jens Axboe8c838782019-03-12 15:48:16 -06001701 if (likely(poll->head)) {
1702 spin_lock(&poll->head->lock);
1703 if (unlikely(list_empty(&poll->wait.entry))) {
1704 if (ipt.error)
1705 cancel = true;
1706 ipt.error = 0;
1707 mask = 0;
1708 }
1709 if (mask || ipt.error)
1710 list_del_init(&poll->wait.entry);
1711 else if (cancel)
1712 WRITE_ONCE(poll->canceled, true);
1713 else if (!poll->done) /* actually waiting for an event */
1714 list_add_tail(&req->list, &ctx->cancel_list);
1715 spin_unlock(&poll->head->lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001716 }
Jens Axboe8c838782019-03-12 15:48:16 -06001717 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06001718 ipt.error = 0;
1719 io_poll_complete(ctx, req, mask);
1720 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07001721 spin_unlock_irq(&ctx->completion_lock);
1722
Jens Axboe8c838782019-03-12 15:48:16 -06001723 if (mask) {
1724 io_cqring_ev_posted(ctx);
Jens Axboee65ef562019-03-12 10:16:44 -06001725 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001726 }
Jens Axboe8c838782019-03-12 15:48:16 -06001727 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001728}
1729
Jens Axboede0617e2019-04-06 21:51:27 -06001730static int io_req_defer(struct io_ring_ctx *ctx, struct io_kiocb *req,
1731 const struct io_uring_sqe *sqe)
1732{
1733 struct io_uring_sqe *sqe_copy;
1734
1735 if (!io_sequence_defer(ctx, req) && list_empty(&ctx->defer_list))
1736 return 0;
1737
1738 sqe_copy = kmalloc(sizeof(*sqe_copy), GFP_KERNEL);
1739 if (!sqe_copy)
1740 return -EAGAIN;
1741
1742 spin_lock_irq(&ctx->completion_lock);
1743 if (!io_sequence_defer(ctx, req) && list_empty(&ctx->defer_list)) {
1744 spin_unlock_irq(&ctx->completion_lock);
1745 kfree(sqe_copy);
1746 return 0;
1747 }
1748
1749 memcpy(sqe_copy, sqe, sizeof(*sqe_copy));
1750 req->submit.sqe = sqe_copy;
1751
1752 INIT_WORK(&req->work, io_sq_wq_submit_work);
1753 list_add_tail(&req->list, &ctx->defer_list);
1754 spin_unlock_irq(&ctx->completion_lock);
1755 return -EIOCBQUEUED;
1756}
1757
Jens Axboe2b188cc2019-01-07 10:46:33 -07001758static int __io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001759 const struct sqe_submit *s, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001760{
Jens Axboee0c5c572019-03-12 10:18:47 -06001761 int ret, opcode;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001762
Jens Axboe9e645e112019-05-10 16:07:28 -06001763 req->user_data = READ_ONCE(s->sqe->user_data);
1764
Jens Axboe2b188cc2019-01-07 10:46:33 -07001765 if (unlikely(s->index >= ctx->sq_entries))
1766 return -EINVAL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001767
1768 opcode = READ_ONCE(s->sqe->opcode);
1769 switch (opcode) {
1770 case IORING_OP_NOP:
1771 ret = io_nop(req, req->user_data);
1772 break;
1773 case IORING_OP_READV:
Jens Axboeedafcce2019-01-09 09:16:05 -07001774 if (unlikely(s->sqe->buf_index))
1775 return -EINVAL;
Jens Axboe8358e3a2019-04-23 08:17:58 -06001776 ret = io_read(req, s, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001777 break;
1778 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07001779 if (unlikely(s->sqe->buf_index))
1780 return -EINVAL;
Jens Axboe8358e3a2019-04-23 08:17:58 -06001781 ret = io_write(req, s, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07001782 break;
1783 case IORING_OP_READ_FIXED:
Jens Axboe8358e3a2019-04-23 08:17:58 -06001784 ret = io_read(req, s, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07001785 break;
1786 case IORING_OP_WRITE_FIXED:
Jens Axboe8358e3a2019-04-23 08:17:58 -06001787 ret = io_write(req, s, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001788 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001789 case IORING_OP_FSYNC:
1790 ret = io_fsync(req, s->sqe, force_nonblock);
1791 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001792 case IORING_OP_POLL_ADD:
1793 ret = io_poll_add(req, s->sqe);
1794 break;
1795 case IORING_OP_POLL_REMOVE:
1796 ret = io_poll_remove(req, s->sqe);
1797 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001798 case IORING_OP_SYNC_FILE_RANGE:
1799 ret = io_sync_file_range(req, s->sqe, force_nonblock);
1800 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06001801 case IORING_OP_SENDMSG:
1802 ret = io_sendmsg(req, s->sqe, force_nonblock);
1803 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06001804 case IORING_OP_RECVMSG:
1805 ret = io_recvmsg(req, s->sqe, force_nonblock);
1806 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001807 default:
1808 ret = -EINVAL;
1809 break;
1810 }
1811
Jens Axboedef596e2019-01-09 08:59:42 -07001812 if (ret)
1813 return ret;
1814
1815 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe9e645e112019-05-10 16:07:28 -06001816 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07001817 return -EAGAIN;
1818
1819 /* workqueue context doesn't hold uring_lock, grab it now */
1820 if (s->needs_lock)
1821 mutex_lock(&ctx->uring_lock);
1822 io_iopoll_req_issued(req);
1823 if (s->needs_lock)
1824 mutex_unlock(&ctx->uring_lock);
1825 }
1826
1827 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001828}
1829
Jens Axboe31b51512019-01-18 22:56:34 -07001830static struct async_list *io_async_list_from_sqe(struct io_ring_ctx *ctx,
1831 const struct io_uring_sqe *sqe)
1832{
1833 switch (sqe->opcode) {
1834 case IORING_OP_READV:
1835 case IORING_OP_READ_FIXED:
1836 return &ctx->pending_async[READ];
1837 case IORING_OP_WRITEV:
1838 case IORING_OP_WRITE_FIXED:
1839 return &ctx->pending_async[WRITE];
1840 default:
1841 return NULL;
1842 }
1843}
1844
Jens Axboeedafcce2019-01-09 09:16:05 -07001845static inline bool io_sqe_needs_user(const struct io_uring_sqe *sqe)
1846{
1847 u8 opcode = READ_ONCE(sqe->opcode);
1848
1849 return !(opcode == IORING_OP_READ_FIXED ||
1850 opcode == IORING_OP_WRITE_FIXED);
1851}
1852
Jens Axboe2b188cc2019-01-07 10:46:33 -07001853static void io_sq_wq_submit_work(struct work_struct *work)
1854{
1855 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001856 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe31b51512019-01-18 22:56:34 -07001857 struct mm_struct *cur_mm = NULL;
1858 struct async_list *async_list;
1859 LIST_HEAD(req_list);
Jens Axboeedafcce2019-01-09 09:16:05 -07001860 mm_segment_t old_fs;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001861 int ret;
1862
Jens Axboe31b51512019-01-18 22:56:34 -07001863 async_list = io_async_list_from_sqe(ctx, req->submit.sqe);
1864restart:
1865 do {
1866 struct sqe_submit *s = &req->submit;
1867 const struct io_uring_sqe *sqe = s->sqe;
Jackie Liud0ee8792019-07-31 14:39:33 +08001868 unsigned int flags = req->flags;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001869
Stefan Bühler8449eed2019-04-27 20:34:19 +02001870 /* Ensure we clear previously set non-block flag */
Jens Axboe31b51512019-01-18 22:56:34 -07001871 req->rw.ki_flags &= ~IOCB_NOWAIT;
1872
1873 ret = 0;
1874 if (io_sqe_needs_user(sqe) && !cur_mm) {
1875 if (!mmget_not_zero(ctx->sqo_mm)) {
1876 ret = -EFAULT;
1877 } else {
1878 cur_mm = ctx->sqo_mm;
1879 use_mm(cur_mm);
1880 old_fs = get_fs();
1881 set_fs(USER_DS);
1882 }
1883 }
1884
1885 if (!ret) {
1886 s->has_user = cur_mm != NULL;
1887 s->needs_lock = true;
1888 do {
Jens Axboe8358e3a2019-04-23 08:17:58 -06001889 ret = __io_submit_sqe(ctx, req, s, false);
Jens Axboe31b51512019-01-18 22:56:34 -07001890 /*
1891 * We can get EAGAIN for polled IO even though
1892 * we're forcing a sync submission from here,
1893 * since we can't wait for request slots on the
1894 * block side.
1895 */
1896 if (ret != -EAGAIN)
1897 break;
1898 cond_resched();
1899 } while (1);
1900 }
Jens Axboe817869d2019-04-30 14:44:05 -06001901
1902 /* drop submission reference */
1903 io_put_req(req);
1904
Jens Axboe31b51512019-01-18 22:56:34 -07001905 if (ret) {
Jens Axboec71ffb62019-05-13 20:58:29 -06001906 io_cqring_add_event(ctx, sqe->user_data, ret);
Jens Axboee65ef562019-03-12 10:16:44 -06001907 io_put_req(req);
Jens Axboe31b51512019-01-18 22:56:34 -07001908 }
1909
1910 /* async context always use a copy of the sqe */
1911 kfree(sqe);
1912
Zhengyuan Liuf7b76ac2019-07-16 23:26:14 +08001913 /* req from defer and link list needn't decrease async cnt */
Jackie Liud0ee8792019-07-31 14:39:33 +08001914 if (flags & (REQ_F_IO_DRAINED | REQ_F_LINK_DONE))
Zhengyuan Liuf7b76ac2019-07-16 23:26:14 +08001915 goto out;
1916
Jens Axboe31b51512019-01-18 22:56:34 -07001917 if (!async_list)
1918 break;
1919 if (!list_empty(&req_list)) {
1920 req = list_first_entry(&req_list, struct io_kiocb,
1921 list);
1922 list_del(&req->list);
1923 continue;
1924 }
1925 if (list_empty(&async_list->list))
1926 break;
1927
1928 req = NULL;
1929 spin_lock(&async_list->lock);
1930 if (list_empty(&async_list->list)) {
1931 spin_unlock(&async_list->lock);
1932 break;
1933 }
1934 list_splice_init(&async_list->list, &req_list);
1935 spin_unlock(&async_list->lock);
1936
1937 req = list_first_entry(&req_list, struct io_kiocb, list);
1938 list_del(&req->list);
1939 } while (req);
Jens Axboeedafcce2019-01-09 09:16:05 -07001940
1941 /*
Jens Axboe31b51512019-01-18 22:56:34 -07001942 * Rare case of racing with a submitter. If we find the count has
1943 * dropped to zero AND we have pending work items, then restart
1944 * the processing. This is a tiny race window.
Jens Axboeedafcce2019-01-09 09:16:05 -07001945 */
Jens Axboe31b51512019-01-18 22:56:34 -07001946 if (async_list) {
1947 ret = atomic_dec_return(&async_list->cnt);
1948 while (!ret && !list_empty(&async_list->list)) {
1949 spin_lock(&async_list->lock);
1950 atomic_inc(&async_list->cnt);
1951 list_splice_init(&async_list->list, &req_list);
1952 spin_unlock(&async_list->lock);
1953
1954 if (!list_empty(&req_list)) {
1955 req = list_first_entry(&req_list,
1956 struct io_kiocb, list);
1957 list_del(&req->list);
1958 goto restart;
1959 }
1960 ret = atomic_dec_return(&async_list->cnt);
Jens Axboeedafcce2019-01-09 09:16:05 -07001961 }
Jens Axboeedafcce2019-01-09 09:16:05 -07001962 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001963
Zhengyuan Liuf7b76ac2019-07-16 23:26:14 +08001964out:
Jens Axboe31b51512019-01-18 22:56:34 -07001965 if (cur_mm) {
Jens Axboeedafcce2019-01-09 09:16:05 -07001966 set_fs(old_fs);
Jens Axboe31b51512019-01-18 22:56:34 -07001967 unuse_mm(cur_mm);
1968 mmput(cur_mm);
Jens Axboeedafcce2019-01-09 09:16:05 -07001969 }
Jens Axboe31b51512019-01-18 22:56:34 -07001970}
Jens Axboe2b188cc2019-01-07 10:46:33 -07001971
Jens Axboe31b51512019-01-18 22:56:34 -07001972/*
1973 * See if we can piggy back onto previously submitted work, that is still
1974 * running. We currently only allow this if the new request is sequential
1975 * to the previous one we punted.
1976 */
1977static bool io_add_to_prev_work(struct async_list *list, struct io_kiocb *req)
1978{
1979 bool ret = false;
1980
1981 if (!list)
1982 return false;
1983 if (!(req->flags & REQ_F_SEQ_PREV))
1984 return false;
1985 if (!atomic_read(&list->cnt))
1986 return false;
1987
1988 ret = true;
1989 spin_lock(&list->lock);
1990 list_add_tail(&req->list, &list->list);
Zhengyuan Liuc0e48f92019-07-18 20:44:00 +08001991 /*
1992 * Ensure we see a simultaneous modification from io_sq_wq_submit_work()
1993 */
1994 smp_mb();
Jens Axboe31b51512019-01-18 22:56:34 -07001995 if (!atomic_read(&list->cnt)) {
1996 list_del_init(&req->list);
1997 ret = false;
1998 }
1999 spin_unlock(&list->lock);
2000 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002001}
2002
Jens Axboe09bb8392019-03-13 12:39:28 -06002003static bool io_op_needs_file(const struct io_uring_sqe *sqe)
2004{
2005 int op = READ_ONCE(sqe->opcode);
2006
2007 switch (op) {
2008 case IORING_OP_NOP:
2009 case IORING_OP_POLL_REMOVE:
2010 return false;
2011 default:
2012 return true;
2013 }
2014}
2015
2016static int io_req_set_file(struct io_ring_ctx *ctx, const struct sqe_submit *s,
2017 struct io_submit_state *state, struct io_kiocb *req)
2018{
2019 unsigned flags;
2020 int fd;
2021
2022 flags = READ_ONCE(s->sqe->flags);
2023 fd = READ_ONCE(s->sqe->fd);
2024
Jackie Liu4fe2c962019-09-09 20:50:40 +08002025 if (flags & IOSQE_IO_DRAIN)
Jens Axboede0617e2019-04-06 21:51:27 -06002026 req->flags |= REQ_F_IO_DRAIN;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002027 /*
2028 * All io need record the previous position, if LINK vs DARIN,
2029 * it can be used to mark the position of the first IO in the
2030 * link list.
2031 */
2032 req->sequence = s->sequence;
Jens Axboede0617e2019-04-06 21:51:27 -06002033
Jens Axboe60c112b2019-06-21 10:20:18 -06002034 if (!io_op_needs_file(s->sqe))
Jens Axboe09bb8392019-03-13 12:39:28 -06002035 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06002036
2037 if (flags & IOSQE_FIXED_FILE) {
2038 if (unlikely(!ctx->user_files ||
2039 (unsigned) fd >= ctx->nr_user_files))
2040 return -EBADF;
2041 req->file = ctx->user_files[fd];
2042 req->flags |= REQ_F_FIXED_FILE;
2043 } else {
2044 if (s->needs_fixed_file)
2045 return -EBADF;
2046 req->file = io_file_get(state, fd);
2047 if (unlikely(!req->file))
2048 return -EBADF;
2049 }
2050
2051 return 0;
2052}
2053
Jackie Liu4fe2c962019-09-09 20:50:40 +08002054static int __io_queue_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
Jens Axboe9e645e112019-05-10 16:07:28 -06002055 struct sqe_submit *s)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002056{
Jens Axboee0c5c572019-03-12 10:18:47 -06002057 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002058
Jens Axboe8358e3a2019-04-23 08:17:58 -06002059 ret = __io_submit_sqe(ctx, req, s, true);
Stefan Bühler8449eed2019-04-27 20:34:19 +02002060 if (ret == -EAGAIN && !(req->flags & REQ_F_NOWAIT)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07002061 struct io_uring_sqe *sqe_copy;
2062
2063 sqe_copy = kmalloc(sizeof(*sqe_copy), GFP_KERNEL);
2064 if (sqe_copy) {
Jens Axboe31b51512019-01-18 22:56:34 -07002065 struct async_list *list;
2066
Jens Axboe2b188cc2019-01-07 10:46:33 -07002067 memcpy(sqe_copy, s->sqe, sizeof(*sqe_copy));
2068 s->sqe = sqe_copy;
2069
2070 memcpy(&req->submit, s, sizeof(*s));
Jens Axboe31b51512019-01-18 22:56:34 -07002071 list = io_async_list_from_sqe(ctx, s->sqe);
2072 if (!io_add_to_prev_work(list, req)) {
2073 if (list)
2074 atomic_inc(&list->cnt);
2075 INIT_WORK(&req->work, io_sq_wq_submit_work);
2076 queue_work(ctx->sqo_wq, &req->work);
2077 }
Jens Axboee65ef562019-03-12 10:16:44 -06002078
2079 /*
2080 * Queued up for async execution, worker will release
Jens Axboe9e645e112019-05-10 16:07:28 -06002081 * submit reference when the iocb is actually submitted.
Jens Axboee65ef562019-03-12 10:16:44 -06002082 */
2083 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002084 }
2085 }
Jens Axboee65ef562019-03-12 10:16:44 -06002086
2087 /* drop submission reference */
2088 io_put_req(req);
2089
2090 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06002091 if (ret) {
2092 io_cqring_add_event(ctx, req->user_data, ret);
2093 if (req->flags & REQ_F_LINK)
2094 req->flags |= REQ_F_FAIL_LINK;
Jens Axboee65ef562019-03-12 10:16:44 -06002095 io_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002096 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002097
2098 return ret;
2099}
2100
Jackie Liu4fe2c962019-09-09 20:50:40 +08002101static int io_queue_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
2102 struct sqe_submit *s)
2103{
2104 int ret;
2105
2106 ret = io_req_defer(ctx, req, s->sqe);
2107 if (ret) {
2108 if (ret != -EIOCBQUEUED) {
2109 io_free_req(req);
2110 io_cqring_add_event(ctx, s->sqe->user_data, ret);
2111 }
2112 return 0;
2113 }
2114
2115 return __io_queue_sqe(ctx, req, s);
2116}
2117
2118static int io_queue_link_head(struct io_ring_ctx *ctx, struct io_kiocb *req,
2119 struct sqe_submit *s, struct io_kiocb *shadow)
2120{
2121 int ret;
2122 int need_submit = false;
2123
2124 if (!shadow)
2125 return io_queue_sqe(ctx, req, s);
2126
2127 /*
2128 * Mark the first IO in link list as DRAIN, let all the following
2129 * IOs enter the defer list. all IO needs to be completed before link
2130 * list.
2131 */
2132 req->flags |= REQ_F_IO_DRAIN;
2133 ret = io_req_defer(ctx, req, s->sqe);
2134 if (ret) {
2135 if (ret != -EIOCBQUEUED) {
2136 io_free_req(req);
2137 io_cqring_add_event(ctx, s->sqe->user_data, ret);
2138 return 0;
2139 }
2140 } else {
2141 /*
2142 * If ret == 0 means that all IOs in front of link io are
2143 * running done. let's queue link head.
2144 */
2145 need_submit = true;
2146 }
2147
2148 /* Insert shadow req to defer_list, blocking next IOs */
2149 spin_lock_irq(&ctx->completion_lock);
2150 list_add_tail(&shadow->list, &ctx->defer_list);
2151 spin_unlock_irq(&ctx->completion_lock);
2152
2153 if (need_submit)
2154 return __io_queue_sqe(ctx, req, s);
2155
2156 return 0;
2157}
2158
Jens Axboe9e645e112019-05-10 16:07:28 -06002159#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK)
2160
2161static void io_submit_sqe(struct io_ring_ctx *ctx, struct sqe_submit *s,
2162 struct io_submit_state *state, struct io_kiocb **link)
2163{
2164 struct io_uring_sqe *sqe_copy;
2165 struct io_kiocb *req;
2166 int ret;
2167
2168 /* enforce forwards compatibility on users */
2169 if (unlikely(s->sqe->flags & ~SQE_VALID_FLAGS)) {
2170 ret = -EINVAL;
2171 goto err;
2172 }
2173
2174 req = io_get_req(ctx, state);
2175 if (unlikely(!req)) {
2176 ret = -EAGAIN;
2177 goto err;
2178 }
2179
2180 ret = io_req_set_file(ctx, s, state, req);
2181 if (unlikely(ret)) {
2182err_req:
2183 io_free_req(req);
2184err:
2185 io_cqring_add_event(ctx, s->sqe->user_data, ret);
2186 return;
2187 }
2188
Jens Axboe9e645e112019-05-10 16:07:28 -06002189 /*
2190 * If we already have a head request, queue this one for async
2191 * submittal once the head completes. If we don't have a head but
2192 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
2193 * submitted sync once the chain is complete. If none of those
2194 * conditions are true (normal request), then just queue it.
2195 */
2196 if (*link) {
2197 struct io_kiocb *prev = *link;
2198
2199 sqe_copy = kmemdup(s->sqe, sizeof(*sqe_copy), GFP_KERNEL);
2200 if (!sqe_copy) {
2201 ret = -EAGAIN;
2202 goto err_req;
2203 }
2204
2205 s->sqe = sqe_copy;
2206 memcpy(&req->submit, s, sizeof(*s));
2207 list_add_tail(&req->list, &prev->link_list);
2208 } else if (s->sqe->flags & IOSQE_IO_LINK) {
2209 req->flags |= REQ_F_LINK;
2210
2211 memcpy(&req->submit, s, sizeof(*s));
2212 INIT_LIST_HEAD(&req->link_list);
2213 *link = req;
2214 } else {
2215 io_queue_sqe(ctx, req, s);
2216 }
2217}
2218
Jens Axboe9a56a232019-01-09 09:06:50 -07002219/*
2220 * Batched submission is done, ensure local IO is flushed out.
2221 */
2222static void io_submit_state_end(struct io_submit_state *state)
2223{
2224 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06002225 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07002226 if (state->free_reqs)
2227 kmem_cache_free_bulk(req_cachep, state->free_reqs,
2228 &state->reqs[state->cur_req]);
Jens Axboe9a56a232019-01-09 09:06:50 -07002229}
2230
2231/*
2232 * Start submission side cache.
2233 */
2234static void io_submit_state_start(struct io_submit_state *state,
2235 struct io_ring_ctx *ctx, unsigned max_ios)
2236{
2237 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07002238 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07002239 state->file = NULL;
2240 state->ios_left = max_ios;
2241}
2242
Jens Axboe2b188cc2019-01-07 10:46:33 -07002243static void io_commit_sqring(struct io_ring_ctx *ctx)
2244{
Hristo Venev75b28af2019-08-26 17:23:46 +00002245 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002246
Hristo Venev75b28af2019-08-26 17:23:46 +00002247 if (ctx->cached_sq_head != READ_ONCE(rings->sq.head)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07002248 /*
2249 * Ensure any loads from the SQEs are done at this point,
2250 * since once we write the new head, the application could
2251 * write new data to them.
2252 */
Hristo Venev75b28af2019-08-26 17:23:46 +00002253 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002254 }
2255}
2256
2257/*
Jens Axboe2b188cc2019-01-07 10:46:33 -07002258 * Fetch an sqe, if one is available. Note that s->sqe will point to memory
2259 * that is mapped by userspace. This means that care needs to be taken to
2260 * ensure that reads are stable, as we cannot rely on userspace always
2261 * being a good citizen. If members of the sqe are validated and then later
2262 * used, it's important that those reads are done through READ_ONCE() to
2263 * prevent a re-load down the line.
2264 */
2265static bool io_get_sqring(struct io_ring_ctx *ctx, struct sqe_submit *s)
2266{
Hristo Venev75b28af2019-08-26 17:23:46 +00002267 struct io_rings *rings = ctx->rings;
2268 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002269 unsigned head;
2270
2271 /*
2272 * The cached sq head (or cq tail) serves two purposes:
2273 *
2274 * 1) allows us to batch the cost of updating the user visible
2275 * head updates.
2276 * 2) allows the kernel side to track the head on its own, even
2277 * though the application is the one updating it.
2278 */
2279 head = ctx->cached_sq_head;
Stefan Bühlere523a292019-04-19 11:57:44 +02002280 /* make sure SQ entry isn't read before tail */
Hristo Venev75b28af2019-08-26 17:23:46 +00002281 if (head == smp_load_acquire(&rings->sq.tail))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002282 return false;
2283
Hristo Venev75b28af2019-08-26 17:23:46 +00002284 head = READ_ONCE(sq_array[head & ctx->sq_mask]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002285 if (head < ctx->sq_entries) {
2286 s->index = head;
2287 s->sqe = &ctx->sq_sqes[head];
Jackie Liu8776f3f2019-09-09 20:50:39 +08002288 s->sequence = ctx->cached_sq_head;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002289 ctx->cached_sq_head++;
2290 return true;
2291 }
2292
2293 /* drop invalid entries */
2294 ctx->cached_sq_head++;
Hristo Venev75b28af2019-08-26 17:23:46 +00002295 rings->sq_dropped++;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002296 return false;
2297}
2298
Jens Axboe6c271ce2019-01-10 11:22:30 -07002299static int io_submit_sqes(struct io_ring_ctx *ctx, struct sqe_submit *sqes,
2300 unsigned int nr, bool has_user, bool mm_fault)
2301{
2302 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002303 struct io_kiocb *link = NULL;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002304 struct io_kiocb *shadow_req = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002305 bool prev_was_link = false;
2306 int i, submitted = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002307
2308 if (nr > IO_PLUG_THRESHOLD) {
2309 io_submit_state_start(&state, ctx, nr);
2310 statep = &state;
2311 }
2312
2313 for (i = 0; i < nr; i++) {
Jens Axboe9e645e112019-05-10 16:07:28 -06002314 /*
2315 * If previous wasn't linked and we have a linked command,
2316 * that's the end of the chain. Submit the previous link.
2317 */
2318 if (!prev_was_link && link) {
Jackie Liu4fe2c962019-09-09 20:50:40 +08002319 io_queue_link_head(ctx, link, &link->submit, shadow_req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002320 link = NULL;
2321 }
2322 prev_was_link = (sqes[i].sqe->flags & IOSQE_IO_LINK) != 0;
2323
Jackie Liu4fe2c962019-09-09 20:50:40 +08002324 if (link && (sqes[i].sqe->flags & IOSQE_IO_DRAIN)) {
2325 if (!shadow_req) {
2326 shadow_req = io_get_req(ctx, NULL);
2327 shadow_req->flags |= (REQ_F_IO_DRAIN | REQ_F_SHADOW_DRAIN);
2328 refcount_dec(&shadow_req->refs);
2329 }
2330 shadow_req->sequence = sqes[i].sequence;
2331 }
2332
Jens Axboe6c271ce2019-01-10 11:22:30 -07002333 if (unlikely(mm_fault)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06002334 io_cqring_add_event(ctx, sqes[i].sqe->user_data,
2335 -EFAULT);
Jens Axboe6c271ce2019-01-10 11:22:30 -07002336 } else {
2337 sqes[i].has_user = has_user;
2338 sqes[i].needs_lock = true;
2339 sqes[i].needs_fixed_file = true;
Jens Axboe9e645e112019-05-10 16:07:28 -06002340 io_submit_sqe(ctx, &sqes[i], statep, &link);
Jens Axboe6c271ce2019-01-10 11:22:30 -07002341 submitted++;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002342 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07002343 }
2344
Jens Axboe9e645e112019-05-10 16:07:28 -06002345 if (link)
Jackie Liu4fe2c962019-09-09 20:50:40 +08002346 io_queue_link_head(ctx, link, &link->submit, shadow_req);
Jens Axboe6c271ce2019-01-10 11:22:30 -07002347 if (statep)
2348 io_submit_state_end(&state);
2349
2350 return submitted;
2351}
2352
2353static int io_sq_thread(void *data)
2354{
2355 struct sqe_submit sqes[IO_IOPOLL_BATCH];
2356 struct io_ring_ctx *ctx = data;
2357 struct mm_struct *cur_mm = NULL;
2358 mm_segment_t old_fs;
2359 DEFINE_WAIT(wait);
2360 unsigned inflight;
2361 unsigned long timeout;
2362
Jackie Liua4c0b3d2019-07-08 13:41:12 +08002363 complete(&ctx->sqo_thread_started);
2364
Jens Axboe6c271ce2019-01-10 11:22:30 -07002365 old_fs = get_fs();
2366 set_fs(USER_DS);
2367
2368 timeout = inflight = 0;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02002369 while (!kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07002370 bool all_fixed, mm_fault = false;
2371 int i;
2372
2373 if (inflight) {
2374 unsigned nr_events = 0;
2375
2376 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07002377 io_iopoll_check(ctx, &nr_events, 0);
Jens Axboe6c271ce2019-01-10 11:22:30 -07002378 } else {
2379 /*
2380 * Normal IO, just pretend everything completed.
2381 * We don't have to poll completions for that.
2382 */
2383 nr_events = inflight;
2384 }
2385
2386 inflight -= nr_events;
2387 if (!inflight)
2388 timeout = jiffies + ctx->sq_thread_idle;
2389 }
2390
2391 if (!io_get_sqring(ctx, &sqes[0])) {
2392 /*
2393 * We're polling. If we're within the defined idle
2394 * period, then let us spin without work before going
2395 * to sleep.
2396 */
2397 if (inflight || !time_after(jiffies, timeout)) {
2398 cpu_relax();
2399 continue;
2400 }
2401
2402 /*
2403 * Drop cur_mm before scheduling, we can't hold it for
2404 * long periods (or over schedule()). Do this before
2405 * adding ourselves to the waitqueue, as the unuse/drop
2406 * may sleep.
2407 */
2408 if (cur_mm) {
2409 unuse_mm(cur_mm);
2410 mmput(cur_mm);
2411 cur_mm = NULL;
2412 }
2413
2414 prepare_to_wait(&ctx->sqo_wait, &wait,
2415 TASK_INTERRUPTIBLE);
2416
2417 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00002418 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02002419 /* make sure to read SQ tail after writing flags */
2420 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07002421
2422 if (!io_get_sqring(ctx, &sqes[0])) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02002423 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07002424 finish_wait(&ctx->sqo_wait, &wait);
2425 break;
2426 }
2427 if (signal_pending(current))
2428 flush_signals(current);
2429 schedule();
2430 finish_wait(&ctx->sqo_wait, &wait);
2431
Hristo Venev75b28af2019-08-26 17:23:46 +00002432 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002433 continue;
2434 }
2435 finish_wait(&ctx->sqo_wait, &wait);
2436
Hristo Venev75b28af2019-08-26 17:23:46 +00002437 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002438 }
2439
2440 i = 0;
2441 all_fixed = true;
2442 do {
2443 if (all_fixed && io_sqe_needs_user(sqes[i].sqe))
2444 all_fixed = false;
2445
2446 i++;
2447 if (i == ARRAY_SIZE(sqes))
2448 break;
2449 } while (io_get_sqring(ctx, &sqes[i]));
2450
2451 /* Unless all new commands are FIXED regions, grab mm */
2452 if (!all_fixed && !cur_mm) {
2453 mm_fault = !mmget_not_zero(ctx->sqo_mm);
2454 if (!mm_fault) {
2455 use_mm(ctx->sqo_mm);
2456 cur_mm = ctx->sqo_mm;
2457 }
2458 }
2459
2460 inflight += io_submit_sqes(ctx, sqes, i, cur_mm != NULL,
2461 mm_fault);
2462
2463 /* Commit SQ ring head once we've consumed all SQEs */
2464 io_commit_sqring(ctx);
2465 }
2466
2467 set_fs(old_fs);
2468 if (cur_mm) {
2469 unuse_mm(cur_mm);
2470 mmput(cur_mm);
2471 }
Jens Axboe06058632019-04-13 09:26:03 -06002472
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02002473 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06002474
Jens Axboe6c271ce2019-01-10 11:22:30 -07002475 return 0;
2476}
2477
Jens Axboe2b188cc2019-01-07 10:46:33 -07002478static int io_ring_submit(struct io_ring_ctx *ctx, unsigned int to_submit)
2479{
Jens Axboe9a56a232019-01-09 09:06:50 -07002480 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002481 struct io_kiocb *link = NULL;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002482 struct io_kiocb *shadow_req = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002483 bool prev_was_link = false;
Jens Axboe5c8b0b52019-04-30 10:16:07 -06002484 int i, submit = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002485
Jens Axboe9a56a232019-01-09 09:06:50 -07002486 if (to_submit > IO_PLUG_THRESHOLD) {
2487 io_submit_state_start(&state, ctx, to_submit);
2488 statep = &state;
2489 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002490
2491 for (i = 0; i < to_submit; i++) {
2492 struct sqe_submit s;
2493
2494 if (!io_get_sqring(ctx, &s))
2495 break;
2496
Jens Axboe9e645e112019-05-10 16:07:28 -06002497 /*
2498 * If previous wasn't linked and we have a linked command,
2499 * that's the end of the chain. Submit the previous link.
2500 */
2501 if (!prev_was_link && link) {
Jackie Liu4fe2c962019-09-09 20:50:40 +08002502 io_queue_link_head(ctx, link, &link->submit, shadow_req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002503 link = NULL;
2504 }
2505 prev_was_link = (s.sqe->flags & IOSQE_IO_LINK) != 0;
2506
Jackie Liu4fe2c962019-09-09 20:50:40 +08002507 if (link && (s.sqe->flags & IOSQE_IO_DRAIN)) {
2508 if (!shadow_req) {
2509 shadow_req = io_get_req(ctx, NULL);
2510 shadow_req->flags |= (REQ_F_IO_DRAIN | REQ_F_SHADOW_DRAIN);
2511 refcount_dec(&shadow_req->refs);
2512 }
2513 shadow_req->sequence = s.sequence;
2514 }
2515
Jens Axboe2b188cc2019-01-07 10:46:33 -07002516 s.has_user = true;
Jens Axboedef596e2019-01-09 08:59:42 -07002517 s.needs_lock = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002518 s.needs_fixed_file = false;
Jens Axboe5c8b0b52019-04-30 10:16:07 -06002519 submit++;
Jens Axboe9e645e112019-05-10 16:07:28 -06002520 io_submit_sqe(ctx, &s, statep, &link);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002521 }
2522 io_commit_sqring(ctx);
2523
Jens Axboe9e645e112019-05-10 16:07:28 -06002524 if (link)
Jackie Liu4fe2c962019-09-09 20:50:40 +08002525 io_queue_link_head(ctx, link, &link->submit, shadow_req);
Jens Axboe9a56a232019-01-09 09:06:50 -07002526 if (statep)
2527 io_submit_state_end(statep);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002528
Jens Axboe5c8b0b52019-04-30 10:16:07 -06002529 return submit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002530}
2531
Jens Axboe2b188cc2019-01-07 10:46:33 -07002532/*
2533 * Wait until events become available, if we don't already have some. The
2534 * application must reap them itself, as they reside on the shared cq ring.
2535 */
2536static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
2537 const sigset_t __user *sig, size_t sigsz)
2538{
Hristo Venev75b28af2019-08-26 17:23:46 +00002539 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002540 int ret;
2541
Hristo Venev75b28af2019-08-26 17:23:46 +00002542 if (io_cqring_events(rings) >= min_events)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002543 return 0;
2544
2545 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01002546#ifdef CONFIG_COMPAT
2547 if (in_compat_syscall())
2548 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07002549 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01002550 else
2551#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07002552 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01002553
Jens Axboe2b188cc2019-01-07 10:46:33 -07002554 if (ret)
2555 return ret;
2556 }
2557
Hristo Venev75b28af2019-08-26 17:23:46 +00002558 ret = wait_event_interruptible(ctx->wait, io_cqring_events(rings) >= min_events);
Oleg Nesterovb7724342019-07-16 16:29:53 -07002559 restore_saved_sigmask_unless(ret == -ERESTARTSYS);
Oleg Nesterov97abc882019-06-28 12:06:50 -07002560 if (ret == -ERESTARTSYS)
2561 ret = -EINTR;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002562
Hristo Venev75b28af2019-08-26 17:23:46 +00002563 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002564}
2565
Jens Axboe6b063142019-01-10 22:13:58 -07002566static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
2567{
2568#if defined(CONFIG_UNIX)
2569 if (ctx->ring_sock) {
2570 struct sock *sock = ctx->ring_sock->sk;
2571 struct sk_buff *skb;
2572
2573 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
2574 kfree_skb(skb);
2575 }
2576#else
2577 int i;
2578
2579 for (i = 0; i < ctx->nr_user_files; i++)
2580 fput(ctx->user_files[i]);
2581#endif
2582}
2583
2584static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
2585{
2586 if (!ctx->user_files)
2587 return -ENXIO;
2588
2589 __io_sqe_files_unregister(ctx);
2590 kfree(ctx->user_files);
2591 ctx->user_files = NULL;
2592 ctx->nr_user_files = 0;
2593 return 0;
2594}
2595
Jens Axboe6c271ce2019-01-10 11:22:30 -07002596static void io_sq_thread_stop(struct io_ring_ctx *ctx)
2597{
2598 if (ctx->sqo_thread) {
Jackie Liua4c0b3d2019-07-08 13:41:12 +08002599 wait_for_completion(&ctx->sqo_thread_started);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02002600 /*
2601 * The park is a bit of a work-around, without it we get
2602 * warning spews on shutdown with SQPOLL set and affinity
2603 * set to a single CPU.
2604 */
Jens Axboe06058632019-04-13 09:26:03 -06002605 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07002606 kthread_stop(ctx->sqo_thread);
2607 ctx->sqo_thread = NULL;
2608 }
2609}
2610
Jens Axboe6b063142019-01-10 22:13:58 -07002611static void io_finish_async(struct io_ring_ctx *ctx)
2612{
Jens Axboe6c271ce2019-01-10 11:22:30 -07002613 io_sq_thread_stop(ctx);
2614
Jens Axboe6b063142019-01-10 22:13:58 -07002615 if (ctx->sqo_wq) {
2616 destroy_workqueue(ctx->sqo_wq);
2617 ctx->sqo_wq = NULL;
2618 }
2619}
2620
2621#if defined(CONFIG_UNIX)
2622static void io_destruct_skb(struct sk_buff *skb)
2623{
2624 struct io_ring_ctx *ctx = skb->sk->sk_user_data;
2625
2626 io_finish_async(ctx);
2627 unix_destruct_scm(skb);
2628}
2629
2630/*
2631 * Ensure the UNIX gc is aware of our file set, so we are certain that
2632 * the io_uring can be safely unregistered on process exit, even if we have
2633 * loops in the file referencing.
2634 */
2635static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
2636{
2637 struct sock *sk = ctx->ring_sock->sk;
2638 struct scm_fp_list *fpl;
2639 struct sk_buff *skb;
2640 int i;
2641
2642 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
2643 unsigned long inflight = ctx->user->unix_inflight + nr;
2644
2645 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
2646 return -EMFILE;
2647 }
2648
2649 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
2650 if (!fpl)
2651 return -ENOMEM;
2652
2653 skb = alloc_skb(0, GFP_KERNEL);
2654 if (!skb) {
2655 kfree(fpl);
2656 return -ENOMEM;
2657 }
2658
2659 skb->sk = sk;
2660 skb->destructor = io_destruct_skb;
2661
2662 fpl->user = get_uid(ctx->user);
2663 for (i = 0; i < nr; i++) {
2664 fpl->fp[i] = get_file(ctx->user_files[i + offset]);
2665 unix_inflight(fpl->user, fpl->fp[i]);
2666 }
2667
2668 fpl->max = fpl->count = nr;
2669 UNIXCB(skb).fp = fpl;
2670 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
2671 skb_queue_head(&sk->sk_receive_queue, skb);
2672
2673 for (i = 0; i < nr; i++)
2674 fput(fpl->fp[i]);
2675
2676 return 0;
2677}
2678
2679/*
2680 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
2681 * causes regular reference counting to break down. We rely on the UNIX
2682 * garbage collection to take care of this problem for us.
2683 */
2684static int io_sqe_files_scm(struct io_ring_ctx *ctx)
2685{
2686 unsigned left, total;
2687 int ret = 0;
2688
2689 total = 0;
2690 left = ctx->nr_user_files;
2691 while (left) {
2692 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07002693
2694 ret = __io_sqe_files_scm(ctx, this_files, total);
2695 if (ret)
2696 break;
2697 left -= this_files;
2698 total += this_files;
2699 }
2700
2701 if (!ret)
2702 return 0;
2703
2704 while (total < ctx->nr_user_files) {
2705 fput(ctx->user_files[total]);
2706 total++;
2707 }
2708
2709 return ret;
2710}
2711#else
2712static int io_sqe_files_scm(struct io_ring_ctx *ctx)
2713{
2714 return 0;
2715}
2716#endif
2717
2718static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
2719 unsigned nr_args)
2720{
2721 __s32 __user *fds = (__s32 __user *) arg;
2722 int fd, ret = 0;
2723 unsigned i;
2724
2725 if (ctx->user_files)
2726 return -EBUSY;
2727 if (!nr_args)
2728 return -EINVAL;
2729 if (nr_args > IORING_MAX_FIXED_FILES)
2730 return -EMFILE;
2731
2732 ctx->user_files = kcalloc(nr_args, sizeof(struct file *), GFP_KERNEL);
2733 if (!ctx->user_files)
2734 return -ENOMEM;
2735
2736 for (i = 0; i < nr_args; i++) {
2737 ret = -EFAULT;
2738 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
2739 break;
2740
2741 ctx->user_files[i] = fget(fd);
2742
2743 ret = -EBADF;
2744 if (!ctx->user_files[i])
2745 break;
2746 /*
2747 * Don't allow io_uring instances to be registered. If UNIX
2748 * isn't enabled, then this causes a reference cycle and this
2749 * instance can never get freed. If UNIX is enabled we'll
2750 * handle it just fine, but there's still no point in allowing
2751 * a ring fd as it doesn't support regular read/write anyway.
2752 */
2753 if (ctx->user_files[i]->f_op == &io_uring_fops) {
2754 fput(ctx->user_files[i]);
2755 break;
2756 }
2757 ctx->nr_user_files++;
2758 ret = 0;
2759 }
2760
2761 if (ret) {
2762 for (i = 0; i < ctx->nr_user_files; i++)
2763 fput(ctx->user_files[i]);
2764
2765 kfree(ctx->user_files);
Jens Axboe25adf502019-04-03 09:52:40 -06002766 ctx->user_files = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07002767 ctx->nr_user_files = 0;
2768 return ret;
2769 }
2770
2771 ret = io_sqe_files_scm(ctx);
2772 if (ret)
2773 io_sqe_files_unregister(ctx);
2774
2775 return ret;
2776}
2777
Jens Axboe6c271ce2019-01-10 11:22:30 -07002778static int io_sq_offload_start(struct io_ring_ctx *ctx,
2779 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002780{
2781 int ret;
2782
Jens Axboe6c271ce2019-01-10 11:22:30 -07002783 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002784 mmgrab(current->mm);
2785 ctx->sqo_mm = current->mm;
2786
Jens Axboe6c271ce2019-01-10 11:22:30 -07002787 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06002788 ret = -EPERM;
2789 if (!capable(CAP_SYS_ADMIN))
2790 goto err;
2791
Jens Axboe917257d2019-04-13 09:28:55 -06002792 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
2793 if (!ctx->sq_thread_idle)
2794 ctx->sq_thread_idle = HZ;
2795
Jens Axboe6c271ce2019-01-10 11:22:30 -07002796 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06002797 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002798
Jens Axboe917257d2019-04-13 09:28:55 -06002799 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06002800 if (cpu >= nr_cpu_ids)
2801 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08002802 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06002803 goto err;
2804
Jens Axboe6c271ce2019-01-10 11:22:30 -07002805 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
2806 ctx, cpu,
2807 "io_uring-sq");
2808 } else {
2809 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
2810 "io_uring-sq");
2811 }
2812 if (IS_ERR(ctx->sqo_thread)) {
2813 ret = PTR_ERR(ctx->sqo_thread);
2814 ctx->sqo_thread = NULL;
2815 goto err;
2816 }
2817 wake_up_process(ctx->sqo_thread);
2818 } else if (p->flags & IORING_SETUP_SQ_AFF) {
2819 /* Can't have SQ_AFF without SQPOLL */
2820 ret = -EINVAL;
2821 goto err;
2822 }
2823
Jens Axboe2b188cc2019-01-07 10:46:33 -07002824 /* Do QD, or 2 * CPUS, whatever is smallest */
2825 ctx->sqo_wq = alloc_workqueue("io_ring-wq", WQ_UNBOUND | WQ_FREEZABLE,
2826 min(ctx->sq_entries - 1, 2 * num_online_cpus()));
2827 if (!ctx->sqo_wq) {
2828 ret = -ENOMEM;
2829 goto err;
2830 }
2831
2832 return 0;
2833err:
Jens Axboe6c271ce2019-01-10 11:22:30 -07002834 io_sq_thread_stop(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002835 mmdrop(ctx->sqo_mm);
2836 ctx->sqo_mm = NULL;
2837 return ret;
2838}
2839
2840static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
2841{
2842 atomic_long_sub(nr_pages, &user->locked_vm);
2843}
2844
2845static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
2846{
2847 unsigned long page_limit, cur_pages, new_pages;
2848
2849 /* Don't allow more pages than we can safely lock */
2850 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
2851
2852 do {
2853 cur_pages = atomic_long_read(&user->locked_vm);
2854 new_pages = cur_pages + nr_pages;
2855 if (new_pages > page_limit)
2856 return -ENOMEM;
2857 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
2858 new_pages) != cur_pages);
2859
2860 return 0;
2861}
2862
2863static void io_mem_free(void *ptr)
2864{
Mark Rutland52e04ef2019-04-30 17:30:21 +01002865 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002866
Mark Rutland52e04ef2019-04-30 17:30:21 +01002867 if (!ptr)
2868 return;
2869
2870 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002871 if (put_page_testzero(page))
2872 free_compound_page(page);
2873}
2874
2875static void *io_mem_alloc(size_t size)
2876{
2877 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
2878 __GFP_NORETRY;
2879
2880 return (void *) __get_free_pages(gfp_flags, get_order(size));
2881}
2882
Hristo Venev75b28af2019-08-26 17:23:46 +00002883static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
2884 size_t *sq_offset)
2885{
2886 struct io_rings *rings;
2887 size_t off, sq_array_size;
2888
2889 off = struct_size(rings, cqes, cq_entries);
2890 if (off == SIZE_MAX)
2891 return SIZE_MAX;
2892
2893#ifdef CONFIG_SMP
2894 off = ALIGN(off, SMP_CACHE_BYTES);
2895 if (off == 0)
2896 return SIZE_MAX;
2897#endif
2898
2899 sq_array_size = array_size(sizeof(u32), sq_entries);
2900 if (sq_array_size == SIZE_MAX)
2901 return SIZE_MAX;
2902
2903 if (check_add_overflow(off, sq_array_size, &off))
2904 return SIZE_MAX;
2905
2906 if (sq_offset)
2907 *sq_offset = off;
2908
2909 return off;
2910}
2911
Jens Axboe2b188cc2019-01-07 10:46:33 -07002912static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
2913{
Hristo Venev75b28af2019-08-26 17:23:46 +00002914 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002915
Hristo Venev75b28af2019-08-26 17:23:46 +00002916 pages = (size_t)1 << get_order(
2917 rings_size(sq_entries, cq_entries, NULL));
2918 pages += (size_t)1 << get_order(
2919 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07002920
Hristo Venev75b28af2019-08-26 17:23:46 +00002921 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002922}
2923
Jens Axboeedafcce2019-01-09 09:16:05 -07002924static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
2925{
2926 int i, j;
2927
2928 if (!ctx->user_bufs)
2929 return -ENXIO;
2930
2931 for (i = 0; i < ctx->nr_user_bufs; i++) {
2932 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
2933
2934 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbard27c4d3a2019-08-04 19:32:06 -07002935 put_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07002936
2937 if (ctx->account_mem)
2938 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01002939 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07002940 imu->nr_bvecs = 0;
2941 }
2942
2943 kfree(ctx->user_bufs);
2944 ctx->user_bufs = NULL;
2945 ctx->nr_user_bufs = 0;
2946 return 0;
2947}
2948
2949static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
2950 void __user *arg, unsigned index)
2951{
2952 struct iovec __user *src;
2953
2954#ifdef CONFIG_COMPAT
2955 if (ctx->compat) {
2956 struct compat_iovec __user *ciovs;
2957 struct compat_iovec ciov;
2958
2959 ciovs = (struct compat_iovec __user *) arg;
2960 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
2961 return -EFAULT;
2962
2963 dst->iov_base = (void __user *) (unsigned long) ciov.iov_base;
2964 dst->iov_len = ciov.iov_len;
2965 return 0;
2966 }
2967#endif
2968 src = (struct iovec __user *) arg;
2969 if (copy_from_user(dst, &src[index], sizeof(*dst)))
2970 return -EFAULT;
2971 return 0;
2972}
2973
2974static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
2975 unsigned nr_args)
2976{
2977 struct vm_area_struct **vmas = NULL;
2978 struct page **pages = NULL;
2979 int i, j, got_pages = 0;
2980 int ret = -EINVAL;
2981
2982 if (ctx->user_bufs)
2983 return -EBUSY;
2984 if (!nr_args || nr_args > UIO_MAXIOV)
2985 return -EINVAL;
2986
2987 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
2988 GFP_KERNEL);
2989 if (!ctx->user_bufs)
2990 return -ENOMEM;
2991
2992 for (i = 0; i < nr_args; i++) {
2993 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
2994 unsigned long off, start, end, ubuf;
2995 int pret, nr_pages;
2996 struct iovec iov;
2997 size_t size;
2998
2999 ret = io_copy_iov(ctx, &iov, arg, i);
3000 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03003001 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07003002
3003 /*
3004 * Don't impose further limits on the size and buffer
3005 * constraints here, we'll -EINVAL later when IO is
3006 * submitted if they are wrong.
3007 */
3008 ret = -EFAULT;
3009 if (!iov.iov_base || !iov.iov_len)
3010 goto err;
3011
3012 /* arbitrary limit, but we need something */
3013 if (iov.iov_len > SZ_1G)
3014 goto err;
3015
3016 ubuf = (unsigned long) iov.iov_base;
3017 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
3018 start = ubuf >> PAGE_SHIFT;
3019 nr_pages = end - start;
3020
3021 if (ctx->account_mem) {
3022 ret = io_account_mem(ctx->user, nr_pages);
3023 if (ret)
3024 goto err;
3025 }
3026
3027 ret = 0;
3028 if (!pages || nr_pages > got_pages) {
3029 kfree(vmas);
3030 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003031 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07003032 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003033 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07003034 sizeof(struct vm_area_struct *),
3035 GFP_KERNEL);
3036 if (!pages || !vmas) {
3037 ret = -ENOMEM;
3038 if (ctx->account_mem)
3039 io_unaccount_mem(ctx->user, nr_pages);
3040 goto err;
3041 }
3042 got_pages = nr_pages;
3043 }
3044
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003045 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07003046 GFP_KERNEL);
3047 ret = -ENOMEM;
3048 if (!imu->bvec) {
3049 if (ctx->account_mem)
3050 io_unaccount_mem(ctx->user, nr_pages);
3051 goto err;
3052 }
3053
3054 ret = 0;
3055 down_read(&current->mm->mmap_sem);
Ira Weiny932f4a62019-05-13 17:17:03 -07003056 pret = get_user_pages(ubuf, nr_pages,
3057 FOLL_WRITE | FOLL_LONGTERM,
3058 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07003059 if (pret == nr_pages) {
3060 /* don't support file backed memory */
3061 for (j = 0; j < nr_pages; j++) {
3062 struct vm_area_struct *vma = vmas[j];
3063
3064 if (vma->vm_file &&
3065 !is_file_hugepages(vma->vm_file)) {
3066 ret = -EOPNOTSUPP;
3067 break;
3068 }
3069 }
3070 } else {
3071 ret = pret < 0 ? pret : -EFAULT;
3072 }
3073 up_read(&current->mm->mmap_sem);
3074 if (ret) {
3075 /*
3076 * if we did partial map, or found file backed vmas,
3077 * release any pages we did get
3078 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07003079 if (pret > 0)
3080 put_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07003081 if (ctx->account_mem)
3082 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003083 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07003084 goto err;
3085 }
3086
3087 off = ubuf & ~PAGE_MASK;
3088 size = iov.iov_len;
3089 for (j = 0; j < nr_pages; j++) {
3090 size_t vec_len;
3091
3092 vec_len = min_t(size_t, size, PAGE_SIZE - off);
3093 imu->bvec[j].bv_page = pages[j];
3094 imu->bvec[j].bv_len = vec_len;
3095 imu->bvec[j].bv_offset = off;
3096 off = 0;
3097 size -= vec_len;
3098 }
3099 /* store original address for later verification */
3100 imu->ubuf = ubuf;
3101 imu->len = iov.iov_len;
3102 imu->nr_bvecs = nr_pages;
3103
3104 ctx->nr_user_bufs++;
3105 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003106 kvfree(pages);
3107 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07003108 return 0;
3109err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003110 kvfree(pages);
3111 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07003112 io_sqe_buffer_unregister(ctx);
3113 return ret;
3114}
3115
Jens Axboe9b402842019-04-11 11:45:41 -06003116static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
3117{
3118 __s32 __user *fds = arg;
3119 int fd;
3120
3121 if (ctx->cq_ev_fd)
3122 return -EBUSY;
3123
3124 if (copy_from_user(&fd, fds, sizeof(*fds)))
3125 return -EFAULT;
3126
3127 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
3128 if (IS_ERR(ctx->cq_ev_fd)) {
3129 int ret = PTR_ERR(ctx->cq_ev_fd);
3130 ctx->cq_ev_fd = NULL;
3131 return ret;
3132 }
3133
3134 return 0;
3135}
3136
3137static int io_eventfd_unregister(struct io_ring_ctx *ctx)
3138{
3139 if (ctx->cq_ev_fd) {
3140 eventfd_ctx_put(ctx->cq_ev_fd);
3141 ctx->cq_ev_fd = NULL;
3142 return 0;
3143 }
3144
3145 return -ENXIO;
3146}
3147
Jens Axboe2b188cc2019-01-07 10:46:33 -07003148static void io_ring_ctx_free(struct io_ring_ctx *ctx)
3149{
Jens Axboe6b063142019-01-10 22:13:58 -07003150 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003151 if (ctx->sqo_mm)
3152 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07003153
3154 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07003155 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07003156 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06003157 io_eventfd_unregister(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07003158
Jens Axboe2b188cc2019-01-07 10:46:33 -07003159#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07003160 if (ctx->ring_sock) {
3161 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07003162 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07003163 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003164#endif
3165
Hristo Venev75b28af2019-08-26 17:23:46 +00003166 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003167 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003168
3169 percpu_ref_exit(&ctx->refs);
3170 if (ctx->account_mem)
3171 io_unaccount_mem(ctx->user,
3172 ring_pages(ctx->sq_entries, ctx->cq_entries));
3173 free_uid(ctx->user);
3174 kfree(ctx);
3175}
3176
3177static __poll_t io_uring_poll(struct file *file, poll_table *wait)
3178{
3179 struct io_ring_ctx *ctx = file->private_data;
3180 __poll_t mask = 0;
3181
3182 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02003183 /*
3184 * synchronizes with barrier from wq_has_sleeper call in
3185 * io_commit_cqring
3186 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07003187 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00003188 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
3189 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003190 mask |= EPOLLOUT | EPOLLWRNORM;
Hristo Venev75b28af2019-08-26 17:23:46 +00003191 if (READ_ONCE(ctx->rings->sq.head) != ctx->cached_cq_tail)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003192 mask |= EPOLLIN | EPOLLRDNORM;
3193
3194 return mask;
3195}
3196
3197static int io_uring_fasync(int fd, struct file *file, int on)
3198{
3199 struct io_ring_ctx *ctx = file->private_data;
3200
3201 return fasync_helper(fd, file, on, &ctx->cq_fasync);
3202}
3203
3204static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
3205{
3206 mutex_lock(&ctx->uring_lock);
3207 percpu_ref_kill(&ctx->refs);
3208 mutex_unlock(&ctx->uring_lock);
3209
Jens Axboe221c5eb2019-01-17 09:41:58 -07003210 io_poll_remove_all(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07003211 io_iopoll_reap_events(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003212 wait_for_completion(&ctx->ctx_done);
3213 io_ring_ctx_free(ctx);
3214}
3215
3216static int io_uring_release(struct inode *inode, struct file *file)
3217{
3218 struct io_ring_ctx *ctx = file->private_data;
3219
3220 file->private_data = NULL;
3221 io_ring_ctx_wait_and_kill(ctx);
3222 return 0;
3223}
3224
3225static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
3226{
3227 loff_t offset = (loff_t) vma->vm_pgoff << PAGE_SHIFT;
3228 unsigned long sz = vma->vm_end - vma->vm_start;
3229 struct io_ring_ctx *ctx = file->private_data;
3230 unsigned long pfn;
3231 struct page *page;
3232 void *ptr;
3233
3234 switch (offset) {
3235 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00003236 case IORING_OFF_CQ_RING:
3237 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003238 break;
3239 case IORING_OFF_SQES:
3240 ptr = ctx->sq_sqes;
3241 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003242 default:
3243 return -EINVAL;
3244 }
3245
3246 page = virt_to_head_page(ptr);
3247 if (sz > (PAGE_SIZE << compound_order(page)))
3248 return -EINVAL;
3249
3250 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
3251 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
3252}
3253
3254SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
3255 u32, min_complete, u32, flags, const sigset_t __user *, sig,
3256 size_t, sigsz)
3257{
3258 struct io_ring_ctx *ctx;
3259 long ret = -EBADF;
3260 int submitted = 0;
3261 struct fd f;
3262
Jens Axboe6c271ce2019-01-10 11:22:30 -07003263 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07003264 return -EINVAL;
3265
3266 f = fdget(fd);
3267 if (!f.file)
3268 return -EBADF;
3269
3270 ret = -EOPNOTSUPP;
3271 if (f.file->f_op != &io_uring_fops)
3272 goto out_fput;
3273
3274 ret = -ENXIO;
3275 ctx = f.file->private_data;
3276 if (!percpu_ref_tryget(&ctx->refs))
3277 goto out_fput;
3278
Jens Axboe6c271ce2019-01-10 11:22:30 -07003279 /*
3280 * For SQ polling, the thread will do all submissions and completions.
3281 * Just return the requested submit count, and wake the thread if
3282 * we were asked to.
3283 */
3284 if (ctx->flags & IORING_SETUP_SQPOLL) {
3285 if (flags & IORING_ENTER_SQ_WAKEUP)
3286 wake_up(&ctx->sqo_wait);
3287 submitted = to_submit;
3288 goto out_ctx;
3289 }
3290
Jens Axboe2b188cc2019-01-07 10:46:33 -07003291 ret = 0;
3292 if (to_submit) {
3293 to_submit = min(to_submit, ctx->sq_entries);
3294
3295 mutex_lock(&ctx->uring_lock);
3296 submitted = io_ring_submit(ctx, to_submit);
3297 mutex_unlock(&ctx->uring_lock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003298 }
3299 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07003300 unsigned nr_events = 0;
3301
Jens Axboe2b188cc2019-01-07 10:46:33 -07003302 min_complete = min(min_complete, ctx->cq_entries);
3303
Jens Axboedef596e2019-01-09 08:59:42 -07003304 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07003305 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07003306 } else {
3307 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
3308 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003309 }
3310
3311out_ctx:
3312 io_ring_drop_ctx_refs(ctx, 1);
3313out_fput:
3314 fdput(f);
3315 return submitted ? submitted : ret;
3316}
3317
3318static const struct file_operations io_uring_fops = {
3319 .release = io_uring_release,
3320 .mmap = io_uring_mmap,
3321 .poll = io_uring_poll,
3322 .fasync = io_uring_fasync,
3323};
3324
3325static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
3326 struct io_uring_params *p)
3327{
Hristo Venev75b28af2019-08-26 17:23:46 +00003328 struct io_rings *rings;
3329 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003330
Hristo Venev75b28af2019-08-26 17:23:46 +00003331 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
3332 if (size == SIZE_MAX)
3333 return -EOVERFLOW;
3334
3335 rings = io_mem_alloc(size);
3336 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003337 return -ENOMEM;
3338
Hristo Venev75b28af2019-08-26 17:23:46 +00003339 ctx->rings = rings;
3340 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
3341 rings->sq_ring_mask = p->sq_entries - 1;
3342 rings->cq_ring_mask = p->cq_entries - 1;
3343 rings->sq_ring_entries = p->sq_entries;
3344 rings->cq_ring_entries = p->cq_entries;
3345 ctx->sq_mask = rings->sq_ring_mask;
3346 ctx->cq_mask = rings->cq_ring_mask;
3347 ctx->sq_entries = rings->sq_ring_entries;
3348 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003349
3350 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
3351 if (size == SIZE_MAX)
3352 return -EOVERFLOW;
3353
3354 ctx->sq_sqes = io_mem_alloc(size);
Mark Rutland52e04ef2019-04-30 17:30:21 +01003355 if (!ctx->sq_sqes)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003356 return -ENOMEM;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003357
Jens Axboe2b188cc2019-01-07 10:46:33 -07003358 return 0;
3359}
3360
3361/*
3362 * Allocate an anonymous fd, this is what constitutes the application
3363 * visible backing of an io_uring instance. The application mmaps this
3364 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
3365 * we have to tie this fd to a socket for file garbage collection purposes.
3366 */
3367static int io_uring_get_fd(struct io_ring_ctx *ctx)
3368{
3369 struct file *file;
3370 int ret;
3371
3372#if defined(CONFIG_UNIX)
3373 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
3374 &ctx->ring_sock);
3375 if (ret)
3376 return ret;
3377#endif
3378
3379 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
3380 if (ret < 0)
3381 goto err;
3382
3383 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
3384 O_RDWR | O_CLOEXEC);
3385 if (IS_ERR(file)) {
3386 put_unused_fd(ret);
3387 ret = PTR_ERR(file);
3388 goto err;
3389 }
3390
3391#if defined(CONFIG_UNIX)
3392 ctx->ring_sock->file = file;
Jens Axboe6b063142019-01-10 22:13:58 -07003393 ctx->ring_sock->sk->sk_user_data = ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003394#endif
3395 fd_install(ret, file);
3396 return ret;
3397err:
3398#if defined(CONFIG_UNIX)
3399 sock_release(ctx->ring_sock);
3400 ctx->ring_sock = NULL;
3401#endif
3402 return ret;
3403}
3404
3405static int io_uring_create(unsigned entries, struct io_uring_params *p)
3406{
3407 struct user_struct *user = NULL;
3408 struct io_ring_ctx *ctx;
3409 bool account_mem;
3410 int ret;
3411
3412 if (!entries || entries > IORING_MAX_ENTRIES)
3413 return -EINVAL;
3414
3415 /*
3416 * Use twice as many entries for the CQ ring. It's possible for the
3417 * application to drive a higher depth than the size of the SQ ring,
3418 * since the sqes are only used at submission time. This allows for
3419 * some flexibility in overcommitting a bit.
3420 */
3421 p->sq_entries = roundup_pow_of_two(entries);
3422 p->cq_entries = 2 * p->sq_entries;
3423
3424 user = get_uid(current_user());
3425 account_mem = !capable(CAP_IPC_LOCK);
3426
3427 if (account_mem) {
3428 ret = io_account_mem(user,
3429 ring_pages(p->sq_entries, p->cq_entries));
3430 if (ret) {
3431 free_uid(user);
3432 return ret;
3433 }
3434 }
3435
3436 ctx = io_ring_ctx_alloc(p);
3437 if (!ctx) {
3438 if (account_mem)
3439 io_unaccount_mem(user, ring_pages(p->sq_entries,
3440 p->cq_entries));
3441 free_uid(user);
3442 return -ENOMEM;
3443 }
3444 ctx->compat = in_compat_syscall();
3445 ctx->account_mem = account_mem;
3446 ctx->user = user;
3447
3448 ret = io_allocate_scq_urings(ctx, p);
3449 if (ret)
3450 goto err;
3451
Jens Axboe6c271ce2019-01-10 11:22:30 -07003452 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003453 if (ret)
3454 goto err;
3455
3456 ret = io_uring_get_fd(ctx);
3457 if (ret < 0)
3458 goto err;
3459
3460 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00003461 p->sq_off.head = offsetof(struct io_rings, sq.head);
3462 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
3463 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
3464 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
3465 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
3466 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
3467 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003468
3469 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00003470 p->cq_off.head = offsetof(struct io_rings, cq.head);
3471 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
3472 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
3473 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
3474 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
3475 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06003476
3477 p->features = IORING_FEAT_SINGLE_MMAP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003478 return ret;
3479err:
3480 io_ring_ctx_wait_and_kill(ctx);
3481 return ret;
3482}
3483
3484/*
3485 * Sets up an aio uring context, and returns the fd. Applications asks for a
3486 * ring size, we return the actual sq/cq ring sizes (among other things) in the
3487 * params structure passed in.
3488 */
3489static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
3490{
3491 struct io_uring_params p;
3492 long ret;
3493 int i;
3494
3495 if (copy_from_user(&p, params, sizeof(p)))
3496 return -EFAULT;
3497 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
3498 if (p.resv[i])
3499 return -EINVAL;
3500 }
3501
Jens Axboe6c271ce2019-01-10 11:22:30 -07003502 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
3503 IORING_SETUP_SQ_AFF))
Jens Axboe2b188cc2019-01-07 10:46:33 -07003504 return -EINVAL;
3505
3506 ret = io_uring_create(entries, &p);
3507 if (ret < 0)
3508 return ret;
3509
3510 if (copy_to_user(params, &p, sizeof(p)))
3511 return -EFAULT;
3512
3513 return ret;
3514}
3515
3516SYSCALL_DEFINE2(io_uring_setup, u32, entries,
3517 struct io_uring_params __user *, params)
3518{
3519 return io_uring_setup(entries, params);
3520}
3521
Jens Axboeedafcce2019-01-09 09:16:05 -07003522static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
3523 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06003524 __releases(ctx->uring_lock)
3525 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07003526{
3527 int ret;
3528
Jens Axboe35fa71a2019-04-22 10:23:23 -06003529 /*
3530 * We're inside the ring mutex, if the ref is already dying, then
3531 * someone else killed the ctx or is already going through
3532 * io_uring_register().
3533 */
3534 if (percpu_ref_is_dying(&ctx->refs))
3535 return -ENXIO;
3536
Jens Axboeedafcce2019-01-09 09:16:05 -07003537 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06003538
3539 /*
3540 * Drop uring mutex before waiting for references to exit. If another
3541 * thread is currently inside io_uring_enter() it might need to grab
3542 * the uring_lock to make progress. If we hold it here across the drain
3543 * wait, then we can deadlock. It's safe to drop the mutex here, since
3544 * no new references will come in after we've killed the percpu ref.
3545 */
3546 mutex_unlock(&ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07003547 wait_for_completion(&ctx->ctx_done);
Jens Axboeb19062a2019-04-15 10:49:38 -06003548 mutex_lock(&ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07003549
3550 switch (opcode) {
3551 case IORING_REGISTER_BUFFERS:
3552 ret = io_sqe_buffer_register(ctx, arg, nr_args);
3553 break;
3554 case IORING_UNREGISTER_BUFFERS:
3555 ret = -EINVAL;
3556 if (arg || nr_args)
3557 break;
3558 ret = io_sqe_buffer_unregister(ctx);
3559 break;
Jens Axboe6b063142019-01-10 22:13:58 -07003560 case IORING_REGISTER_FILES:
3561 ret = io_sqe_files_register(ctx, arg, nr_args);
3562 break;
3563 case IORING_UNREGISTER_FILES:
3564 ret = -EINVAL;
3565 if (arg || nr_args)
3566 break;
3567 ret = io_sqe_files_unregister(ctx);
3568 break;
Jens Axboe9b402842019-04-11 11:45:41 -06003569 case IORING_REGISTER_EVENTFD:
3570 ret = -EINVAL;
3571 if (nr_args != 1)
3572 break;
3573 ret = io_eventfd_register(ctx, arg);
3574 break;
3575 case IORING_UNREGISTER_EVENTFD:
3576 ret = -EINVAL;
3577 if (arg || nr_args)
3578 break;
3579 ret = io_eventfd_unregister(ctx);
3580 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07003581 default:
3582 ret = -EINVAL;
3583 break;
3584 }
3585
3586 /* bring the ctx back to life */
3587 reinit_completion(&ctx->ctx_done);
3588 percpu_ref_reinit(&ctx->refs);
3589 return ret;
3590}
3591
3592SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
3593 void __user *, arg, unsigned int, nr_args)
3594{
3595 struct io_ring_ctx *ctx;
3596 long ret = -EBADF;
3597 struct fd f;
3598
3599 f = fdget(fd);
3600 if (!f.file)
3601 return -EBADF;
3602
3603 ret = -EOPNOTSUPP;
3604 if (f.file->f_op != &io_uring_fops)
3605 goto out_fput;
3606
3607 ctx = f.file->private_data;
3608
3609 mutex_lock(&ctx->uring_lock);
3610 ret = __io_uring_register(ctx, opcode, arg, nr_args);
3611 mutex_unlock(&ctx->uring_lock);
3612out_fput:
3613 fdput(f);
3614 return ret;
3615}
3616
Jens Axboe2b188cc2019-01-07 10:46:33 -07003617static int __init io_uring_init(void)
3618{
3619 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
3620 return 0;
3621};
3622__initcall(io_uring_init);