blob: c11c4157a4c22c6c0d718749fd0e68beefda409b [file] [log] [blame]
Jens Axboe2b188cc2019-01-07 10:46:33 -07001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Shared application/kernel submission and completion ring pairs, for
4 * supporting fast/efficient IO.
5 *
6 * A note on the read/write ordering memory barriers that are matched between
Stefan Bühler1e84b972019-04-24 23:54:16 +02007 * the application and kernel side.
8 *
9 * After the application reads the CQ ring tail, it must use an
10 * appropriate smp_rmb() to pair with the smp_wmb() the kernel uses
11 * before writing the tail (using smp_load_acquire to read the tail will
12 * do). It also needs a smp_mb() before updating CQ head (ordering the
13 * entry load(s) with the head store), pairing with an implicit barrier
14 * through a control-dependency in io_get_cqring (smp_store_release to
15 * store head will do). Failure to do so could lead to reading invalid
16 * CQ entries.
17 *
18 * Likewise, the application must use an appropriate smp_wmb() before
19 * writing the SQ tail (ordering SQ entry stores with the tail store),
20 * which pairs with smp_load_acquire in io_get_sqring (smp_store_release
21 * to store the tail will do). And it needs a barrier ordering the SQ
22 * head load before writing new SQ entries (smp_load_acquire to read
23 * head will do).
24 *
25 * When using the SQ poll thread (IORING_SETUP_SQPOLL), the application
26 * needs to check the SQ flags for IORING_SQ_NEED_WAKEUP *after*
27 * updating the SQ tail; a full memory barrier smp_mb() is needed
28 * between.
Jens Axboe2b188cc2019-01-07 10:46:33 -070029 *
30 * Also see the examples in the liburing library:
31 *
32 * git://git.kernel.dk/liburing
33 *
34 * io_uring also uses READ/WRITE_ONCE() for _any_ store or load that happens
35 * from data shared between the kernel and application. This is done both
36 * for ordering purposes, but also to ensure that once a value is loaded from
37 * data that the application could potentially modify, it remains stable.
38 *
39 * Copyright (C) 2018-2019 Jens Axboe
Christoph Hellwigc992fe22019-01-11 09:43:02 -070040 * Copyright (c) 2018-2019 Christoph Hellwig
Jens Axboe2b188cc2019-01-07 10:46:33 -070041 */
42#include <linux/kernel.h>
43#include <linux/init.h>
44#include <linux/errno.h>
45#include <linux/syscalls.h>
46#include <linux/compat.h>
47#include <linux/refcount.h>
48#include <linux/uio.h>
49
50#include <linux/sched/signal.h>
51#include <linux/fs.h>
52#include <linux/file.h>
53#include <linux/fdtable.h>
54#include <linux/mm.h>
55#include <linux/mman.h>
56#include <linux/mmu_context.h>
57#include <linux/percpu.h>
58#include <linux/slab.h>
59#include <linux/workqueue.h>
Jens Axboe6c271ce2019-01-10 11:22:30 -070060#include <linux/kthread.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070061#include <linux/blkdev.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070062#include <linux/bvec.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070063#include <linux/net.h>
64#include <net/sock.h>
65#include <net/af_unix.h>
Jens Axboe6b063142019-01-10 22:13:58 -070066#include <net/scm.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070067#include <linux/anon_inodes.h>
68#include <linux/sched/mm.h>
69#include <linux/uaccess.h>
70#include <linux/nospec.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070071#include <linux/sizes.h>
72#include <linux/hugetlb.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070073
74#include <uapi/linux/io_uring.h>
75
76#include "internal.h"
77
Daniel Xu5277dea2019-09-14 14:23:45 -070078#define IORING_MAX_ENTRIES 32768
Jens Axboe6b063142019-01-10 22:13:58 -070079#define IORING_MAX_FIXED_FILES 1024
Jens Axboe2b188cc2019-01-07 10:46:33 -070080
81struct io_uring {
82 u32 head ____cacheline_aligned_in_smp;
83 u32 tail ____cacheline_aligned_in_smp;
84};
85
Stefan Bühler1e84b972019-04-24 23:54:16 +020086/*
Hristo Venev75b28af2019-08-26 17:23:46 +000087 * This data is shared with the application through the mmap at offsets
88 * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING.
Stefan Bühler1e84b972019-04-24 23:54:16 +020089 *
90 * The offsets to the member fields are published through struct
91 * io_sqring_offsets when calling io_uring_setup.
92 */
Hristo Venev75b28af2019-08-26 17:23:46 +000093struct io_rings {
Stefan Bühler1e84b972019-04-24 23:54:16 +020094 /*
95 * Head and tail offsets into the ring; the offsets need to be
96 * masked to get valid indices.
97 *
Hristo Venev75b28af2019-08-26 17:23:46 +000098 * The kernel controls head of the sq ring and the tail of the cq ring,
99 * and the application controls tail of the sq ring and the head of the
100 * cq ring.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200101 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000102 struct io_uring sq, cq;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200103 /*
Hristo Venev75b28af2019-08-26 17:23:46 +0000104 * Bitmasks to apply to head and tail offsets (constant, equals
Stefan Bühler1e84b972019-04-24 23:54:16 +0200105 * ring_entries - 1)
106 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000107 u32 sq_ring_mask, cq_ring_mask;
108 /* Ring sizes (constant, power of 2) */
109 u32 sq_ring_entries, cq_ring_entries;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200110 /*
111 * Number of invalid entries dropped by the kernel due to
112 * invalid index stored in array
113 *
114 * Written by the kernel, shouldn't be modified by the
115 * application (i.e. get number of "new events" by comparing to
116 * cached value).
117 *
118 * After a new SQ head value was read by the application this
119 * counter includes all submissions that were dropped reaching
120 * the new SQ head (and possibly more).
121 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000122 u32 sq_dropped;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200123 /*
124 * Runtime flags
125 *
126 * Written by the kernel, shouldn't be modified by the
127 * application.
128 *
129 * The application needs a full memory barrier before checking
130 * for IORING_SQ_NEED_WAKEUP after updating the sq tail.
131 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000132 u32 sq_flags;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200133 /*
134 * Number of completion events lost because the queue was full;
135 * this should be avoided by the application by making sure
136 * there are not more requests pending thatn there is space in
137 * the completion queue.
138 *
139 * Written by the kernel, shouldn't be modified by the
140 * application (i.e. get number of "new events" by comparing to
141 * cached value).
142 *
143 * As completion events come in out of order this counter is not
144 * ordered with any other data.
145 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000146 u32 cq_overflow;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200147 /*
148 * Ring buffer of completion events.
149 *
150 * The kernel writes completion events fresh every time they are
151 * produced, so the application is allowed to modify pending
152 * entries.
153 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000154 struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700155};
156
Jens Axboeedafcce2019-01-09 09:16:05 -0700157struct io_mapped_ubuf {
158 u64 ubuf;
159 size_t len;
160 struct bio_vec *bvec;
161 unsigned int nr_bvecs;
162};
163
Jens Axboe31b51512019-01-18 22:56:34 -0700164struct async_list {
165 spinlock_t lock;
166 atomic_t cnt;
167 struct list_head list;
168
169 struct file *file;
Jens Axboe6d5d5ac2019-09-11 10:16:13 -0600170 off_t io_start;
Zhengyuan Liu9310a7ba2019-07-22 10:23:27 +0800171 size_t io_len;
Jens Axboe31b51512019-01-18 22:56:34 -0700172};
173
Jens Axboe2b188cc2019-01-07 10:46:33 -0700174struct io_ring_ctx {
175 struct {
176 struct percpu_ref refs;
177 } ____cacheline_aligned_in_smp;
178
179 struct {
180 unsigned int flags;
181 bool compat;
182 bool account_mem;
183
Hristo Venev75b28af2019-08-26 17:23:46 +0000184 /*
185 * Ring buffer of indices into array of io_uring_sqe, which is
186 * mmapped by the application using the IORING_OFF_SQES offset.
187 *
188 * This indirection could e.g. be used to assign fixed
189 * io_uring_sqe entries to operations and only submit them to
190 * the queue when needed.
191 *
192 * The kernel modifies neither the indices array nor the entries
193 * array.
194 */
195 u32 *sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700196 unsigned cached_sq_head;
197 unsigned sq_entries;
198 unsigned sq_mask;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700199 unsigned sq_thread_idle;
Jens Axboe498ccd92019-10-25 10:04:25 -0600200 unsigned cached_sq_dropped;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700201 struct io_uring_sqe *sq_sqes;
Jens Axboede0617e2019-04-06 21:51:27 -0600202
203 struct list_head defer_list;
Jens Axboe5262f562019-09-17 12:26:57 -0600204 struct list_head timeout_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700205 } ____cacheline_aligned_in_smp;
206
207 /* IO offload */
Jens Axboe54a91f32019-09-10 09:15:04 -0600208 struct workqueue_struct *sqo_wq[2];
Jens Axboe6c271ce2019-01-10 11:22:30 -0700209 struct task_struct *sqo_thread; /* if using sq thread polling */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700210 struct mm_struct *sqo_mm;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700211 wait_queue_head_t sqo_wait;
Jackie Liua4c0b3d2019-07-08 13:41:12 +0800212 struct completion sqo_thread_started;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700213
214 struct {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700215 unsigned cached_cq_tail;
Jens Axboe498ccd92019-10-25 10:04:25 -0600216 atomic_t cached_cq_overflow;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700217 unsigned cq_entries;
218 unsigned cq_mask;
219 struct wait_queue_head cq_wait;
220 struct fasync_struct *cq_fasync;
Jens Axboe9b402842019-04-11 11:45:41 -0600221 struct eventfd_ctx *cq_ev_fd;
Jens Axboe5262f562019-09-17 12:26:57 -0600222 atomic_t cq_timeouts;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700223 } ____cacheline_aligned_in_smp;
224
Hristo Venev75b28af2019-08-26 17:23:46 +0000225 struct io_rings *rings;
226
Jens Axboe6b063142019-01-10 22:13:58 -0700227 /*
228 * If used, fixed file set. Writers must ensure that ->refs is dead,
229 * readers must ensure that ->refs is alive as long as the file* is
230 * used. Only updated through io_uring_register(2).
231 */
232 struct file **user_files;
233 unsigned nr_user_files;
234
Jens Axboeedafcce2019-01-09 09:16:05 -0700235 /* if used, fixed mapped user buffers */
236 unsigned nr_user_bufs;
237 struct io_mapped_ubuf *user_bufs;
238
Jens Axboe2b188cc2019-01-07 10:46:33 -0700239 struct user_struct *user;
240
241 struct completion ctx_done;
242
243 struct {
244 struct mutex uring_lock;
245 wait_queue_head_t wait;
246 } ____cacheline_aligned_in_smp;
247
248 struct {
249 spinlock_t completion_lock;
Jens Axboedef596e2019-01-09 08:59:42 -0700250 bool poll_multi_file;
251 /*
252 * ->poll_list is protected by the ctx->uring_lock for
253 * io_uring instances that don't use IORING_SETUP_SQPOLL.
254 * For SQPOLL, only the single threaded io_sq_thread() will
255 * manipulate the list, hence no extra locking is needed there.
256 */
257 struct list_head poll_list;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700258 struct list_head cancel_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700259 } ____cacheline_aligned_in_smp;
260
Jens Axboe31b51512019-01-18 22:56:34 -0700261 struct async_list pending_async[2];
262
Jens Axboe2b188cc2019-01-07 10:46:33 -0700263#if defined(CONFIG_UNIX)
264 struct socket *ring_sock;
265#endif
266};
267
268struct sqe_submit {
269 const struct io_uring_sqe *sqe;
270 unsigned short index;
Jackie Liu8776f3f2019-09-09 20:50:39 +0800271 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700272 bool has_user;
Jens Axboedef596e2019-01-09 08:59:42 -0700273 bool needs_lock;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700274 bool needs_fixed_file;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700275};
276
Jens Axboe09bb8392019-03-13 12:39:28 -0600277/*
278 * First field must be the file pointer in all the
279 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
280 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700281struct io_poll_iocb {
282 struct file *file;
283 struct wait_queue_head *head;
284 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600285 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700286 bool canceled;
287 struct wait_queue_entry wait;
288};
289
Jens Axboe5262f562019-09-17 12:26:57 -0600290struct io_timeout {
291 struct file *file;
292 struct hrtimer timer;
293};
294
Jens Axboe09bb8392019-03-13 12:39:28 -0600295/*
296 * NOTE! Each of the iocb union members has the file pointer
297 * as the first entry in their struct definition. So you can
298 * access the file pointer through any of the sub-structs,
299 * or directly as just 'ki_filp' in this struct.
300 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700301struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700302 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600303 struct file *file;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700304 struct kiocb rw;
305 struct io_poll_iocb poll;
Jens Axboe5262f562019-09-17 12:26:57 -0600306 struct io_timeout timeout;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700307 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700308
309 struct sqe_submit submit;
310
311 struct io_ring_ctx *ctx;
312 struct list_head list;
Jens Axboe9e645e112019-05-10 16:07:28 -0600313 struct list_head link_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700314 unsigned int flags;
Jens Axboec16361c2019-01-17 08:39:48 -0700315 refcount_t refs;
Stefan Bühler8449eed2019-04-27 20:34:19 +0200316#define REQ_F_NOWAIT 1 /* must not punt to workers */
Jens Axboedef596e2019-01-09 08:59:42 -0700317#define REQ_F_IOPOLL_COMPLETED 2 /* polled IO has completed */
Jens Axboe6b063142019-01-10 22:13:58 -0700318#define REQ_F_FIXED_FILE 4 /* ctx owns file */
Jens Axboe31b51512019-01-18 22:56:34 -0700319#define REQ_F_SEQ_PREV 8 /* sequential with previous */
Stefan Bühlere2033e32019-05-11 19:08:01 +0200320#define REQ_F_IO_DRAIN 16 /* drain existing IO first */
321#define REQ_F_IO_DRAINED 32 /* drain done */
Jens Axboe9e645e112019-05-10 16:07:28 -0600322#define REQ_F_LINK 64 /* linked sqes */
Zhengyuan Liuf7b76ac2019-07-16 23:26:14 +0800323#define REQ_F_LINK_DONE 128 /* linked sqes done */
324#define REQ_F_FAIL_LINK 256 /* fail rest of links */
Jackie Liu4fe2c962019-09-09 20:50:40 +0800325#define REQ_F_SHADOW_DRAIN 512 /* link-drain shadow req */
Jens Axboe5262f562019-09-17 12:26:57 -0600326#define REQ_F_TIMEOUT 1024 /* timeout request */
Jens Axboe491381ce2019-10-17 09:20:46 -0600327#define REQ_F_ISREG 2048 /* regular file */
328#define REQ_F_MUST_PUNT 4096 /* must be punted even for NONBLOCK */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700329 u64 user_data;
Jens Axboe9e645e112019-05-10 16:07:28 -0600330 u32 result;
Jens Axboede0617e2019-04-06 21:51:27 -0600331 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700332
333 struct work_struct work;
334};
335
336#define IO_PLUG_THRESHOLD 2
Jens Axboedef596e2019-01-09 08:59:42 -0700337#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700338
Jens Axboe9a56a232019-01-09 09:06:50 -0700339struct io_submit_state {
340 struct blk_plug plug;
341
342 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700343 * io_kiocb alloc cache
344 */
345 void *reqs[IO_IOPOLL_BATCH];
346 unsigned int free_reqs;
347 unsigned int cur_req;
348
349 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700350 * File reference cache
351 */
352 struct file *file;
353 unsigned int fd;
354 unsigned int has_refs;
355 unsigned int used_refs;
356 unsigned int ios_left;
357};
358
Jens Axboede0617e2019-04-06 21:51:27 -0600359static void io_sq_wq_submit_work(struct work_struct *work);
Jens Axboe5262f562019-09-17 12:26:57 -0600360static void io_cqring_fill_event(struct io_ring_ctx *ctx, u64 ki_user_data,
361 long res);
Jackie Liu4fe2c962019-09-09 20:50:40 +0800362static void __io_free_req(struct io_kiocb *req);
Jens Axboede0617e2019-04-06 21:51:27 -0600363
Jens Axboe2b188cc2019-01-07 10:46:33 -0700364static struct kmem_cache *req_cachep;
365
366static const struct file_operations io_uring_fops;
367
368struct sock *io_uring_get_socket(struct file *file)
369{
370#if defined(CONFIG_UNIX)
371 if (file->f_op == &io_uring_fops) {
372 struct io_ring_ctx *ctx = file->private_data;
373
374 return ctx->ring_sock->sk;
375 }
376#endif
377 return NULL;
378}
379EXPORT_SYMBOL(io_uring_get_socket);
380
381static void io_ring_ctx_ref_free(struct percpu_ref *ref)
382{
383 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
384
385 complete(&ctx->ctx_done);
386}
387
388static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
389{
390 struct io_ring_ctx *ctx;
Jens Axboe31b51512019-01-18 22:56:34 -0700391 int i;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700392
393 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
394 if (!ctx)
395 return NULL;
396
Roman Gushchin21482892019-05-07 10:01:48 -0700397 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
398 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700399 kfree(ctx);
400 return NULL;
401 }
402
403 ctx->flags = p->flags;
404 init_waitqueue_head(&ctx->cq_wait);
405 init_completion(&ctx->ctx_done);
Jackie Liua4c0b3d2019-07-08 13:41:12 +0800406 init_completion(&ctx->sqo_thread_started);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700407 mutex_init(&ctx->uring_lock);
408 init_waitqueue_head(&ctx->wait);
Jens Axboe31b51512019-01-18 22:56:34 -0700409 for (i = 0; i < ARRAY_SIZE(ctx->pending_async); i++) {
410 spin_lock_init(&ctx->pending_async[i].lock);
411 INIT_LIST_HEAD(&ctx->pending_async[i].list);
412 atomic_set(&ctx->pending_async[i].cnt, 0);
413 }
Jens Axboe2b188cc2019-01-07 10:46:33 -0700414 spin_lock_init(&ctx->completion_lock);
Jens Axboedef596e2019-01-09 08:59:42 -0700415 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboe221c5eb2019-01-17 09:41:58 -0700416 INIT_LIST_HEAD(&ctx->cancel_list);
Jens Axboede0617e2019-04-06 21:51:27 -0600417 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -0600418 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700419 return ctx;
420}
421
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600422static inline bool __io_sequence_defer(struct io_ring_ctx *ctx,
423 struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -0600424{
Jens Axboe498ccd92019-10-25 10:04:25 -0600425 return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped
426 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboede0617e2019-04-06 21:51:27 -0600427}
428
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600429static inline bool io_sequence_defer(struct io_ring_ctx *ctx,
430 struct io_kiocb *req)
431{
432 if ((req->flags & (REQ_F_IO_DRAIN|REQ_F_IO_DRAINED)) != REQ_F_IO_DRAIN)
433 return false;
434
435 return __io_sequence_defer(ctx, req);
436}
437
438static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -0600439{
440 struct io_kiocb *req;
441
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600442 req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list);
443 if (req && !io_sequence_defer(ctx, req)) {
Jens Axboede0617e2019-04-06 21:51:27 -0600444 list_del_init(&req->list);
445 return req;
446 }
447
448 return NULL;
449}
450
Jens Axboe5262f562019-09-17 12:26:57 -0600451static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx)
452{
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600453 struct io_kiocb *req;
454
455 req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list);
456 if (req && !__io_sequence_defer(ctx, req)) {
457 list_del_init(&req->list);
458 return req;
459 }
460
461 return NULL;
Jens Axboe5262f562019-09-17 12:26:57 -0600462}
463
Jens Axboede0617e2019-04-06 21:51:27 -0600464static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700465{
Hristo Venev75b28af2019-08-26 17:23:46 +0000466 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700467
Hristo Venev75b28af2019-08-26 17:23:46 +0000468 if (ctx->cached_cq_tail != READ_ONCE(rings->cq.tail)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700469 /* order cqe stores with ring update */
Hristo Venev75b28af2019-08-26 17:23:46 +0000470 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700471
Jens Axboe2b188cc2019-01-07 10:46:33 -0700472 if (wq_has_sleeper(&ctx->cq_wait)) {
473 wake_up_interruptible(&ctx->cq_wait);
474 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
475 }
476 }
477}
478
Jens Axboe18d9be12019-09-10 09:13:05 -0600479static inline void io_queue_async_work(struct io_ring_ctx *ctx,
480 struct io_kiocb *req)
481{
Jens Axboe6cc47d12019-09-18 11:18:23 -0600482 int rw = 0;
Jens Axboe54a91f32019-09-10 09:15:04 -0600483
Jens Axboe6cc47d12019-09-18 11:18:23 -0600484 if (req->submit.sqe) {
485 switch (req->submit.sqe->opcode) {
486 case IORING_OP_WRITEV:
487 case IORING_OP_WRITE_FIXED:
488 rw = !(req->rw.ki_flags & IOCB_DIRECT);
489 break;
490 }
Jens Axboe54a91f32019-09-10 09:15:04 -0600491 }
492
493 queue_work(ctx->sqo_wq[rw], &req->work);
Jens Axboe18d9be12019-09-10 09:13:05 -0600494}
495
Jens Axboe5262f562019-09-17 12:26:57 -0600496static void io_kill_timeout(struct io_kiocb *req)
497{
498 int ret;
499
500 ret = hrtimer_try_to_cancel(&req->timeout.timer);
501 if (ret != -1) {
502 atomic_inc(&req->ctx->cq_timeouts);
503 list_del(&req->list);
504 io_cqring_fill_event(req->ctx, req->user_data, 0);
505 __io_free_req(req);
506 }
507}
508
509static void io_kill_timeouts(struct io_ring_ctx *ctx)
510{
511 struct io_kiocb *req, *tmp;
512
513 spin_lock_irq(&ctx->completion_lock);
514 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
515 io_kill_timeout(req);
516 spin_unlock_irq(&ctx->completion_lock);
517}
518
Jens Axboede0617e2019-04-06 21:51:27 -0600519static void io_commit_cqring(struct io_ring_ctx *ctx)
520{
521 struct io_kiocb *req;
522
Jens Axboe5262f562019-09-17 12:26:57 -0600523 while ((req = io_get_timeout_req(ctx)) != NULL)
524 io_kill_timeout(req);
525
Jens Axboede0617e2019-04-06 21:51:27 -0600526 __io_commit_cqring(ctx);
527
528 while ((req = io_get_deferred_req(ctx)) != NULL) {
Jackie Liu4fe2c962019-09-09 20:50:40 +0800529 if (req->flags & REQ_F_SHADOW_DRAIN) {
530 /* Just for drain, free it. */
531 __io_free_req(req);
532 continue;
533 }
Jens Axboede0617e2019-04-06 21:51:27 -0600534 req->flags |= REQ_F_IO_DRAINED;
Jens Axboe18d9be12019-09-10 09:13:05 -0600535 io_queue_async_work(ctx, req);
Jens Axboede0617e2019-04-06 21:51:27 -0600536 }
537}
538
Jens Axboe2b188cc2019-01-07 10:46:33 -0700539static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
540{
Hristo Venev75b28af2019-08-26 17:23:46 +0000541 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700542 unsigned tail;
543
544 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +0200545 /*
546 * writes to the cq entry need to come after reading head; the
547 * control dependency is enough as we're using WRITE_ONCE to
548 * fill the cq entry
549 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000550 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700551 return NULL;
552
553 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +0000554 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -0700555}
556
557static void io_cqring_fill_event(struct io_ring_ctx *ctx, u64 ki_user_data,
Jens Axboec71ffb62019-05-13 20:58:29 -0600558 long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700559{
560 struct io_uring_cqe *cqe;
561
562 /*
563 * If we can't get a cq entry, userspace overflowed the
564 * submission (by quite a lot). Increment the overflow count in
565 * the ring.
566 */
567 cqe = io_get_cqring(ctx);
568 if (cqe) {
569 WRITE_ONCE(cqe->user_data, ki_user_data);
570 WRITE_ONCE(cqe->res, res);
Jens Axboec71ffb62019-05-13 20:58:29 -0600571 WRITE_ONCE(cqe->flags, 0);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700572 } else {
Jens Axboe498ccd92019-10-25 10:04:25 -0600573 WRITE_ONCE(ctx->rings->cq_overflow,
574 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe2b188cc2019-01-07 10:46:33 -0700575 }
576}
577
Jens Axboe8c838782019-03-12 15:48:16 -0600578static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
579{
580 if (waitqueue_active(&ctx->wait))
581 wake_up(&ctx->wait);
582 if (waitqueue_active(&ctx->sqo_wait))
583 wake_up(&ctx->sqo_wait);
Jens Axboe9b402842019-04-11 11:45:41 -0600584 if (ctx->cq_ev_fd)
585 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -0600586}
587
588static void io_cqring_add_event(struct io_ring_ctx *ctx, u64 user_data,
Jens Axboec71ffb62019-05-13 20:58:29 -0600589 long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700590{
591 unsigned long flags;
592
593 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboec71ffb62019-05-13 20:58:29 -0600594 io_cqring_fill_event(ctx, user_data, res);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700595 io_commit_cqring(ctx);
596 spin_unlock_irqrestore(&ctx->completion_lock, flags);
597
Jens Axboe8c838782019-03-12 15:48:16 -0600598 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700599}
600
Jens Axboe2579f912019-01-09 09:10:43 -0700601static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
602 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700603{
Jens Axboefd6fab22019-03-14 16:30:06 -0600604 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700605 struct io_kiocb *req;
606
607 if (!percpu_ref_tryget(&ctx->refs))
608 return NULL;
609
Jens Axboe2579f912019-01-09 09:10:43 -0700610 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -0600611 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -0700612 if (unlikely(!req))
613 goto out;
614 } else if (!state->free_reqs) {
615 size_t sz;
616 int ret;
617
618 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -0600619 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
620
621 /*
622 * Bulk alloc is all-or-nothing. If we fail to get a batch,
623 * retry single alloc to be on the safe side.
624 */
625 if (unlikely(ret <= 0)) {
626 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
627 if (!state->reqs[0])
628 goto out;
629 ret = 1;
630 }
Jens Axboe2579f912019-01-09 09:10:43 -0700631 state->free_reqs = ret - 1;
632 state->cur_req = 1;
633 req = state->reqs[0];
634 } else {
635 req = state->reqs[state->cur_req];
636 state->free_reqs--;
637 state->cur_req++;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700638 }
639
Jens Axboe60c112b2019-06-21 10:20:18 -0600640 req->file = NULL;
Jens Axboe2579f912019-01-09 09:10:43 -0700641 req->ctx = ctx;
642 req->flags = 0;
Jens Axboee65ef562019-03-12 10:16:44 -0600643 /* one is dropped after submission, the other at completion */
644 refcount_set(&req->refs, 2);
Jens Axboe9e645e112019-05-10 16:07:28 -0600645 req->result = 0;
Jens Axboe2579f912019-01-09 09:10:43 -0700646 return req;
647out:
Pavel Begunkov6805b322019-10-08 02:18:42 +0300648 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700649 return NULL;
650}
651
Jens Axboedef596e2019-01-09 08:59:42 -0700652static void io_free_req_many(struct io_ring_ctx *ctx, void **reqs, int *nr)
653{
654 if (*nr) {
655 kmem_cache_free_bulk(req_cachep, *nr, reqs);
Pavel Begunkov6805b322019-10-08 02:18:42 +0300656 percpu_ref_put_many(&ctx->refs, *nr);
Jens Axboedef596e2019-01-09 08:59:42 -0700657 *nr = 0;
658 }
659}
660
Jens Axboe9e645e112019-05-10 16:07:28 -0600661static void __io_free_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700662{
Jens Axboe09bb8392019-03-13 12:39:28 -0600663 if (req->file && !(req->flags & REQ_F_FIXED_FILE))
664 fput(req->file);
Pavel Begunkov6805b322019-10-08 02:18:42 +0300665 percpu_ref_put(&req->ctx->refs);
Jens Axboee65ef562019-03-12 10:16:44 -0600666 kmem_cache_free(req_cachep, req);
667}
668
Jens Axboe9e645e112019-05-10 16:07:28 -0600669static void io_req_link_next(struct io_kiocb *req)
670{
671 struct io_kiocb *nxt;
672
673 /*
674 * The list should never be empty when we are called here. But could
675 * potentially happen if the chain is messed up, check to be on the
676 * safe side.
677 */
678 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, list);
679 if (nxt) {
680 list_del(&nxt->list);
681 if (!list_empty(&req->link_list)) {
682 INIT_LIST_HEAD(&nxt->link_list);
683 list_splice(&req->link_list, &nxt->link_list);
684 nxt->flags |= REQ_F_LINK;
685 }
686
Zhengyuan Liuf7b76ac2019-07-16 23:26:14 +0800687 nxt->flags |= REQ_F_LINK_DONE;
Jens Axboe9e645e112019-05-10 16:07:28 -0600688 INIT_WORK(&nxt->work, io_sq_wq_submit_work);
Jens Axboe18d9be12019-09-10 09:13:05 -0600689 io_queue_async_work(req->ctx, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -0600690 }
691}
692
693/*
694 * Called if REQ_F_LINK is set, and we fail the head request
695 */
696static void io_fail_links(struct io_kiocb *req)
697{
698 struct io_kiocb *link;
699
700 while (!list_empty(&req->link_list)) {
701 link = list_first_entry(&req->link_list, struct io_kiocb, list);
702 list_del(&link->list);
703
704 io_cqring_add_event(req->ctx, link->user_data, -ECANCELED);
705 __io_free_req(link);
706 }
707}
708
709static void io_free_req(struct io_kiocb *req)
710{
711 /*
712 * If LINK is set, we have dependent requests in this chain. If we
713 * didn't fail this request, queue the first one up, moving any other
714 * dependencies to the next request. In case of failure, fail the rest
715 * of the chain.
716 */
717 if (req->flags & REQ_F_LINK) {
718 if (req->flags & REQ_F_FAIL_LINK)
719 io_fail_links(req);
720 else
721 io_req_link_next(req);
722 }
723
724 __io_free_req(req);
725}
726
Jens Axboee65ef562019-03-12 10:16:44 -0600727static void io_put_req(struct io_kiocb *req)
728{
729 if (refcount_dec_and_test(&req->refs))
730 io_free_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700731}
732
Hristo Venev75b28af2019-08-26 17:23:46 +0000733static unsigned io_cqring_events(struct io_rings *rings)
Jens Axboea3a0e432019-08-20 11:03:11 -0600734{
735 /* See comment at the top of this file */
736 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +0000737 return READ_ONCE(rings->cq.tail) - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -0600738}
739
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +0300740static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
741{
742 struct io_rings *rings = ctx->rings;
743
744 /* make sure SQ entry isn't read before tail */
745 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
746}
747
Jens Axboedef596e2019-01-09 08:59:42 -0700748/*
749 * Find and free completed poll iocbs
750 */
751static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
752 struct list_head *done)
753{
754 void *reqs[IO_IOPOLL_BATCH];
755 struct io_kiocb *req;
Jens Axboe09bb8392019-03-13 12:39:28 -0600756 int to_free;
Jens Axboedef596e2019-01-09 08:59:42 -0700757
Jens Axboe09bb8392019-03-13 12:39:28 -0600758 to_free = 0;
Jens Axboedef596e2019-01-09 08:59:42 -0700759 while (!list_empty(done)) {
760 req = list_first_entry(done, struct io_kiocb, list);
761 list_del(&req->list);
762
Jens Axboe9e645e112019-05-10 16:07:28 -0600763 io_cqring_fill_event(ctx, req->user_data, req->result);
Jens Axboedef596e2019-01-09 08:59:42 -0700764 (*nr_events)++;
765
Jens Axboe09bb8392019-03-13 12:39:28 -0600766 if (refcount_dec_and_test(&req->refs)) {
767 /* If we're not using fixed files, we have to pair the
768 * completion part with the file put. Use regular
769 * completions for those, only batch free for fixed
Jens Axboe9e645e112019-05-10 16:07:28 -0600770 * file and non-linked commands.
Jens Axboe09bb8392019-03-13 12:39:28 -0600771 */
Jens Axboe9e645e112019-05-10 16:07:28 -0600772 if ((req->flags & (REQ_F_FIXED_FILE|REQ_F_LINK)) ==
773 REQ_F_FIXED_FILE) {
Jens Axboe09bb8392019-03-13 12:39:28 -0600774 reqs[to_free++] = req;
775 if (to_free == ARRAY_SIZE(reqs))
776 io_free_req_many(ctx, reqs, &to_free);
Jens Axboe6b063142019-01-10 22:13:58 -0700777 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -0600778 io_free_req(req);
Jens Axboe6b063142019-01-10 22:13:58 -0700779 }
Jens Axboe9a56a232019-01-09 09:06:50 -0700780 }
Jens Axboedef596e2019-01-09 08:59:42 -0700781 }
Jens Axboedef596e2019-01-09 08:59:42 -0700782
Jens Axboe09bb8392019-03-13 12:39:28 -0600783 io_commit_cqring(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -0700784 io_free_req_many(ctx, reqs, &to_free);
785}
786
787static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
788 long min)
789{
790 struct io_kiocb *req, *tmp;
791 LIST_HEAD(done);
792 bool spin;
793 int ret;
794
795 /*
796 * Only spin for completions if we don't have multiple devices hanging
797 * off our complete list, and we're under the requested amount.
798 */
799 spin = !ctx->poll_multi_file && *nr_events < min;
800
801 ret = 0;
802 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
803 struct kiocb *kiocb = &req->rw;
804
805 /*
806 * Move completed entries to our local list. If we find a
807 * request that requires polling, break out and complete
808 * the done list first, if we have entries there.
809 */
810 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
811 list_move_tail(&req->list, &done);
812 continue;
813 }
814 if (!list_empty(&done))
815 break;
816
817 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
818 if (ret < 0)
819 break;
820
821 if (ret && spin)
822 spin = false;
823 ret = 0;
824 }
825
826 if (!list_empty(&done))
827 io_iopoll_complete(ctx, nr_events, &done);
828
829 return ret;
830}
831
832/*
833 * Poll for a mininum of 'min' events. Note that if min == 0 we consider that a
834 * non-spinning poll check - we'll still enter the driver poll loop, but only
835 * as a non-spinning completion check.
836 */
837static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
838 long min)
839{
Jens Axboe08f54392019-08-21 22:19:11 -0600840 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -0700841 int ret;
842
843 ret = io_do_iopoll(ctx, nr_events, min);
844 if (ret < 0)
845 return ret;
846 if (!min || *nr_events >= min)
847 return 0;
848 }
849
850 return 1;
851}
852
853/*
854 * We can't just wait for polled events to come to us, we have to actively
855 * find and complete them.
856 */
857static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
858{
859 if (!(ctx->flags & IORING_SETUP_IOPOLL))
860 return;
861
862 mutex_lock(&ctx->uring_lock);
863 while (!list_empty(&ctx->poll_list)) {
864 unsigned int nr_events = 0;
865
866 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -0600867
868 /*
869 * Ensure we allow local-to-the-cpu processing to take place,
870 * in this case we need to ensure that we reap all events.
871 */
872 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -0700873 }
874 mutex_unlock(&ctx->uring_lock);
875}
876
Jens Axboe2b2ed972019-10-25 10:06:15 -0600877static int __io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
878 long min)
Jens Axboedef596e2019-01-09 08:59:42 -0700879{
Jens Axboe2b2ed972019-10-25 10:06:15 -0600880 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -0700881
882 do {
883 int tmin = 0;
884
Jens Axboe500f9fb2019-08-19 12:15:59 -0600885 /*
Jens Axboea3a0e432019-08-20 11:03:11 -0600886 * Don't enter poll loop if we already have events pending.
887 * If we do, we can potentially be spinning for commands that
888 * already triggered a CQE (eg in error).
889 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000890 if (io_cqring_events(ctx->rings))
Jens Axboea3a0e432019-08-20 11:03:11 -0600891 break;
892
893 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -0600894 * If a submit got punted to a workqueue, we can have the
895 * application entering polling for a command before it gets
896 * issued. That app will hold the uring_lock for the duration
897 * of the poll right here, so we need to take a breather every
898 * now and then to ensure that the issue has a chance to add
899 * the poll to the issued list. Otherwise we can spin here
900 * forever, while the workqueue is stuck trying to acquire the
901 * very same mutex.
902 */
903 if (!(++iters & 7)) {
904 mutex_unlock(&ctx->uring_lock);
905 mutex_lock(&ctx->uring_lock);
906 }
907
Jens Axboedef596e2019-01-09 08:59:42 -0700908 if (*nr_events < min)
909 tmin = min - *nr_events;
910
911 ret = io_iopoll_getevents(ctx, nr_events, tmin);
912 if (ret <= 0)
913 break;
914 ret = 0;
915 } while (min && !*nr_events && !need_resched());
916
Jens Axboe2b2ed972019-10-25 10:06:15 -0600917 return ret;
918}
919
920static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
921 long min)
922{
923 int ret;
924
925 /*
926 * We disallow the app entering submit/complete with polling, but we
927 * still need to lock the ring to prevent racing with polled issue
928 * that got punted to a workqueue.
929 */
930 mutex_lock(&ctx->uring_lock);
931 ret = __io_iopoll_check(ctx, nr_events, min);
Jens Axboe500f9fb2019-08-19 12:15:59 -0600932 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -0700933 return ret;
934}
935
Jens Axboe491381ce2019-10-17 09:20:46 -0600936static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700937{
Jens Axboe491381ce2019-10-17 09:20:46 -0600938 /*
939 * Tell lockdep we inherited freeze protection from submission
940 * thread.
941 */
942 if (req->flags & REQ_F_ISREG) {
943 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700944
Jens Axboe491381ce2019-10-17 09:20:46 -0600945 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700946 }
Jens Axboe491381ce2019-10-17 09:20:46 -0600947 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700948}
949
950static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
951{
952 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
953
Jens Axboe491381ce2019-10-17 09:20:46 -0600954 if (kiocb->ki_flags & IOCB_WRITE)
955 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700956
Jens Axboe9e645e112019-05-10 16:07:28 -0600957 if ((req->flags & REQ_F_LINK) && res != req->result)
958 req->flags |= REQ_F_FAIL_LINK;
Jens Axboec71ffb62019-05-13 20:58:29 -0600959 io_cqring_add_event(req->ctx, req->user_data, res);
Jens Axboee65ef562019-03-12 10:16:44 -0600960 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700961}
962
Jens Axboedef596e2019-01-09 08:59:42 -0700963static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
964{
965 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
966
Jens Axboe491381ce2019-10-17 09:20:46 -0600967 if (kiocb->ki_flags & IOCB_WRITE)
968 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -0700969
Jens Axboe9e645e112019-05-10 16:07:28 -0600970 if ((req->flags & REQ_F_LINK) && res != req->result)
971 req->flags |= REQ_F_FAIL_LINK;
972 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -0700973 if (res != -EAGAIN)
974 req->flags |= REQ_F_IOPOLL_COMPLETED;
975}
976
977/*
978 * After the iocb has been issued, it's safe to be found on the poll list.
979 * Adding the kiocb to the list AFTER submission ensures that we don't
980 * find it from a io_iopoll_getevents() thread before the issuer is done
981 * accessing the kiocb cookie.
982 */
983static void io_iopoll_req_issued(struct io_kiocb *req)
984{
985 struct io_ring_ctx *ctx = req->ctx;
986
987 /*
988 * Track whether we have multiple files in our lists. This will impact
989 * how we do polling eventually, not spinning if we're on potentially
990 * different devices.
991 */
992 if (list_empty(&ctx->poll_list)) {
993 ctx->poll_multi_file = false;
994 } else if (!ctx->poll_multi_file) {
995 struct io_kiocb *list_req;
996
997 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
998 list);
999 if (list_req->rw.ki_filp != req->rw.ki_filp)
1000 ctx->poll_multi_file = true;
1001 }
1002
1003 /*
1004 * For fast devices, IO may have already completed. If it has, add
1005 * it to the front so we find it first.
1006 */
1007 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1008 list_add(&req->list, &ctx->poll_list);
1009 else
1010 list_add_tail(&req->list, &ctx->poll_list);
1011}
1012
Jens Axboe3d6770f2019-04-13 11:50:54 -06001013static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07001014{
Jens Axboe3d6770f2019-04-13 11:50:54 -06001015 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -07001016 int diff = state->has_refs - state->used_refs;
1017
1018 if (diff)
1019 fput_many(state->file, diff);
1020 state->file = NULL;
1021 }
1022}
1023
1024/*
1025 * Get as many references to a file as we have IOs left in this submission,
1026 * assuming most submissions are for one file, or at least that each file
1027 * has more than one submission.
1028 */
1029static struct file *io_file_get(struct io_submit_state *state, int fd)
1030{
1031 if (!state)
1032 return fget(fd);
1033
1034 if (state->file) {
1035 if (state->fd == fd) {
1036 state->used_refs++;
1037 state->ios_left--;
1038 return state->file;
1039 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06001040 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07001041 }
1042 state->file = fget_many(fd, state->ios_left);
1043 if (!state->file)
1044 return NULL;
1045
1046 state->fd = fd;
1047 state->has_refs = state->ios_left;
1048 state->used_refs = 1;
1049 state->ios_left--;
1050 return state->file;
1051}
1052
Jens Axboe2b188cc2019-01-07 10:46:33 -07001053/*
1054 * If we tracked the file through the SCM inflight mechanism, we could support
1055 * any file. For now, just ensure that anything potentially problematic is done
1056 * inline.
1057 */
1058static bool io_file_supports_async(struct file *file)
1059{
1060 umode_t mode = file_inode(file)->i_mode;
1061
1062 if (S_ISBLK(mode) || S_ISCHR(mode))
1063 return true;
1064 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
1065 return true;
1066
1067 return false;
1068}
1069
Jens Axboe6c271ce2019-01-10 11:22:30 -07001070static int io_prep_rw(struct io_kiocb *req, const struct sqe_submit *s,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001071 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001072{
Jens Axboe6c271ce2019-01-10 11:22:30 -07001073 const struct io_uring_sqe *sqe = s->sqe;
Jens Axboedef596e2019-01-09 08:59:42 -07001074 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001075 struct kiocb *kiocb = &req->rw;
Jens Axboe09bb8392019-03-13 12:39:28 -06001076 unsigned ioprio;
1077 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001078
Jens Axboe09bb8392019-03-13 12:39:28 -06001079 if (!req->file)
1080 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001081
Jens Axboe491381ce2019-10-17 09:20:46 -06001082 if (S_ISREG(file_inode(req->file)->i_mode))
1083 req->flags |= REQ_F_ISREG;
1084
1085 /*
1086 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
1087 * we know to async punt it even if it was opened O_NONBLOCK
1088 */
1089 if (force_nonblock && !io_file_supports_async(req->file)) {
1090 req->flags |= REQ_F_MUST_PUNT;
1091 return -EAGAIN;
1092 }
Jens Axboe6b063142019-01-10 22:13:58 -07001093
Jens Axboe2b188cc2019-01-07 10:46:33 -07001094 kiocb->ki_pos = READ_ONCE(sqe->off);
1095 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
1096 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
1097
1098 ioprio = READ_ONCE(sqe->ioprio);
1099 if (ioprio) {
1100 ret = ioprio_check_cap(ioprio);
1101 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06001102 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001103
1104 kiocb->ki_ioprio = ioprio;
1105 } else
1106 kiocb->ki_ioprio = get_current_ioprio();
1107
1108 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
1109 if (unlikely(ret))
Jens Axboe09bb8392019-03-13 12:39:28 -06001110 return ret;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001111
1112 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06001113 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
1114 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02001115 req->flags |= REQ_F_NOWAIT;
1116
1117 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001118 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001119
Jens Axboedef596e2019-01-09 08:59:42 -07001120 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07001121 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
1122 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06001123 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001124
Jens Axboedef596e2019-01-09 08:59:42 -07001125 kiocb->ki_flags |= IOCB_HIPRI;
1126 kiocb->ki_complete = io_complete_rw_iopoll;
1127 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001128 if (kiocb->ki_flags & IOCB_HIPRI)
1129 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07001130 kiocb->ki_complete = io_complete_rw;
1131 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001132 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001133}
1134
1135static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
1136{
1137 switch (ret) {
1138 case -EIOCBQUEUED:
1139 break;
1140 case -ERESTARTSYS:
1141 case -ERESTARTNOINTR:
1142 case -ERESTARTNOHAND:
1143 case -ERESTART_RESTARTBLOCK:
1144 /*
1145 * We can't just restart the syscall, since previously
1146 * submitted sqes may already be in progress. Just fail this
1147 * IO with EINTR.
1148 */
1149 ret = -EINTR;
1150 /* fall through */
1151 default:
1152 kiocb->ki_complete(kiocb, ret, 0);
1153 }
1154}
1155
Jens Axboeedafcce2019-01-09 09:16:05 -07001156static int io_import_fixed(struct io_ring_ctx *ctx, int rw,
1157 const struct io_uring_sqe *sqe,
1158 struct iov_iter *iter)
1159{
1160 size_t len = READ_ONCE(sqe->len);
1161 struct io_mapped_ubuf *imu;
1162 unsigned index, buf_index;
1163 size_t offset;
1164 u64 buf_addr;
1165
1166 /* attempt to use fixed buffers without having provided iovecs */
1167 if (unlikely(!ctx->user_bufs))
1168 return -EFAULT;
1169
1170 buf_index = READ_ONCE(sqe->buf_index);
1171 if (unlikely(buf_index >= ctx->nr_user_bufs))
1172 return -EFAULT;
1173
1174 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
1175 imu = &ctx->user_bufs[index];
1176 buf_addr = READ_ONCE(sqe->addr);
1177
1178 /* overflow */
1179 if (buf_addr + len < buf_addr)
1180 return -EFAULT;
1181 /* not inside the mapped region */
1182 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
1183 return -EFAULT;
1184
1185 /*
1186 * May not be a start of buffer, set size appropriately
1187 * and advance us to the beginning.
1188 */
1189 offset = buf_addr - imu->ubuf;
1190 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06001191
1192 if (offset) {
1193 /*
1194 * Don't use iov_iter_advance() here, as it's really slow for
1195 * using the latter parts of a big fixed buffer - it iterates
1196 * over each segment manually. We can cheat a bit here, because
1197 * we know that:
1198 *
1199 * 1) it's a BVEC iter, we set it up
1200 * 2) all bvecs are PAGE_SIZE in size, except potentially the
1201 * first and last bvec
1202 *
1203 * So just find our index, and adjust the iterator afterwards.
1204 * If the offset is within the first bvec (or the whole first
1205 * bvec, just use iov_iter_advance(). This makes it easier
1206 * since we can just skip the first segment, which may not
1207 * be PAGE_SIZE aligned.
1208 */
1209 const struct bio_vec *bvec = imu->bvec;
1210
1211 if (offset <= bvec->bv_len) {
1212 iov_iter_advance(iter, offset);
1213 } else {
1214 unsigned long seg_skip;
1215
1216 /* skip first vec */
1217 offset -= bvec->bv_len;
1218 seg_skip = 1 + (offset >> PAGE_SHIFT);
1219
1220 iter->bvec = bvec + seg_skip;
1221 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02001222 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001223 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001224 }
1225 }
1226
Jens Axboeedafcce2019-01-09 09:16:05 -07001227 return 0;
1228}
1229
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001230static ssize_t io_import_iovec(struct io_ring_ctx *ctx, int rw,
1231 const struct sqe_submit *s, struct iovec **iovec,
1232 struct iov_iter *iter)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001233{
1234 const struct io_uring_sqe *sqe = s->sqe;
1235 void __user *buf = u64_to_user_ptr(READ_ONCE(sqe->addr));
1236 size_t sqe_len = READ_ONCE(sqe->len);
Jens Axboeedafcce2019-01-09 09:16:05 -07001237 u8 opcode;
1238
1239 /*
1240 * We're reading ->opcode for the second time, but the first read
1241 * doesn't care whether it's _FIXED or not, so it doesn't matter
1242 * whether ->opcode changes concurrently. The first read does care
1243 * about whether it is a READ or a WRITE, so we don't trust this read
1244 * for that purpose and instead let the caller pass in the read/write
1245 * flag.
1246 */
1247 opcode = READ_ONCE(sqe->opcode);
1248 if (opcode == IORING_OP_READ_FIXED ||
1249 opcode == IORING_OP_WRITE_FIXED) {
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001250 ssize_t ret = io_import_fixed(ctx, rw, sqe, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07001251 *iovec = NULL;
1252 return ret;
1253 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001254
1255 if (!s->has_user)
1256 return -EFAULT;
1257
1258#ifdef CONFIG_COMPAT
1259 if (ctx->compat)
1260 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
1261 iovec, iter);
1262#endif
1263
1264 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
1265}
1266
Jens Axboe6d5d5ac2019-09-11 10:16:13 -06001267static inline bool io_should_merge(struct async_list *al, struct kiocb *kiocb)
1268{
1269 if (al->file == kiocb->ki_filp) {
1270 off_t start, end;
1271
1272 /*
1273 * Allow merging if we're anywhere in the range of the same
1274 * page. Generally this happens for sub-page reads or writes,
1275 * and it's beneficial to allow the first worker to bring the
1276 * page in and the piggy backed work can then work on the
1277 * cached page.
1278 */
1279 start = al->io_start & PAGE_MASK;
1280 end = (al->io_start + al->io_len + PAGE_SIZE - 1) & PAGE_MASK;
1281 if (kiocb->ki_pos >= start && kiocb->ki_pos <= end)
1282 return true;
1283 }
1284
1285 al->file = NULL;
1286 return false;
1287}
1288
Jens Axboe31b51512019-01-18 22:56:34 -07001289/*
1290 * Make a note of the last file/offset/direction we punted to async
1291 * context. We'll use this information to see if we can piggy back a
1292 * sequential request onto the previous one, if it's still hasn't been
1293 * completed by the async worker.
1294 */
1295static void io_async_list_note(int rw, struct io_kiocb *req, size_t len)
1296{
1297 struct async_list *async_list = &req->ctx->pending_async[rw];
1298 struct kiocb *kiocb = &req->rw;
1299 struct file *filp = kiocb->ki_filp;
Jens Axboe31b51512019-01-18 22:56:34 -07001300
Jens Axboe6d5d5ac2019-09-11 10:16:13 -06001301 if (io_should_merge(async_list, kiocb)) {
Zhengyuan Liu9310a7ba2019-07-22 10:23:27 +08001302 unsigned long max_bytes;
Jens Axboe31b51512019-01-18 22:56:34 -07001303
1304 /* Use 8x RA size as a decent limiter for both reads/writes */
Zhengyuan Liu9310a7ba2019-07-22 10:23:27 +08001305 max_bytes = filp->f_ra.ra_pages << (PAGE_SHIFT + 3);
1306 if (!max_bytes)
1307 max_bytes = VM_READAHEAD_PAGES << (PAGE_SHIFT + 3);
Jens Axboe31b51512019-01-18 22:56:34 -07001308
Zhengyuan Liu9310a7ba2019-07-22 10:23:27 +08001309 /* If max len are exceeded, reset the state */
1310 if (async_list->io_len + len <= max_bytes) {
Jens Axboe31b51512019-01-18 22:56:34 -07001311 req->flags |= REQ_F_SEQ_PREV;
Zhengyuan Liu9310a7ba2019-07-22 10:23:27 +08001312 async_list->io_len += len;
Jens Axboe31b51512019-01-18 22:56:34 -07001313 } else {
Jens Axboe6d5d5ac2019-09-11 10:16:13 -06001314 async_list->file = NULL;
Jens Axboe31b51512019-01-18 22:56:34 -07001315 }
1316 }
1317
1318 /* New file? Reset state. */
1319 if (async_list->file != filp) {
Jens Axboe6d5d5ac2019-09-11 10:16:13 -06001320 async_list->io_start = kiocb->ki_pos;
1321 async_list->io_len = len;
Jens Axboe31b51512019-01-18 22:56:34 -07001322 async_list->file = filp;
1323 }
Jens Axboe31b51512019-01-18 22:56:34 -07001324}
1325
Jens Axboe32960612019-09-23 11:05:34 -06001326/*
1327 * For files that don't have ->read_iter() and ->write_iter(), handle them
1328 * by looping over ->read() or ->write() manually.
1329 */
1330static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
1331 struct iov_iter *iter)
1332{
1333 ssize_t ret = 0;
1334
1335 /*
1336 * Don't support polled IO through this interface, and we can't
1337 * support non-blocking either. For the latter, this just causes
1338 * the kiocb to be handled from an async context.
1339 */
1340 if (kiocb->ki_flags & IOCB_HIPRI)
1341 return -EOPNOTSUPP;
1342 if (kiocb->ki_flags & IOCB_NOWAIT)
1343 return -EAGAIN;
1344
1345 while (iov_iter_count(iter)) {
1346 struct iovec iovec = iov_iter_iovec(iter);
1347 ssize_t nr;
1348
1349 if (rw == READ) {
1350 nr = file->f_op->read(file, iovec.iov_base,
1351 iovec.iov_len, &kiocb->ki_pos);
1352 } else {
1353 nr = file->f_op->write(file, iovec.iov_base,
1354 iovec.iov_len, &kiocb->ki_pos);
1355 }
1356
1357 if (nr < 0) {
1358 if (!ret)
1359 ret = nr;
1360 break;
1361 }
1362 ret += nr;
1363 if (nr != iovec.iov_len)
1364 break;
1365 iov_iter_advance(iter, nr);
1366 }
1367
1368 return ret;
1369}
1370
Jens Axboee0c5c572019-03-12 10:18:47 -06001371static int io_read(struct io_kiocb *req, const struct sqe_submit *s,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001372 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001373{
1374 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1375 struct kiocb *kiocb = &req->rw;
1376 struct iov_iter iter;
1377 struct file *file;
Jens Axboe31b51512019-01-18 22:56:34 -07001378 size_t iov_count;
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001379 ssize_t read_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001380
Jens Axboe8358e3a2019-04-23 08:17:58 -06001381 ret = io_prep_rw(req, s, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001382 if (ret)
1383 return ret;
1384 file = kiocb->ki_filp;
1385
Jens Axboe2b188cc2019-01-07 10:46:33 -07001386 if (unlikely(!(file->f_mode & FMODE_READ)))
Jens Axboe09bb8392019-03-13 12:39:28 -06001387 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001388
1389 ret = io_import_iovec(req->ctx, READ, s, &iovec, &iter);
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001390 if (ret < 0)
Jens Axboe09bb8392019-03-13 12:39:28 -06001391 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001392
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001393 read_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06001394 if (req->flags & REQ_F_LINK)
1395 req->result = read_size;
1396
Jens Axboe31b51512019-01-18 22:56:34 -07001397 iov_count = iov_iter_count(&iter);
1398 ret = rw_verify_area(READ, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001399 if (!ret) {
1400 ssize_t ret2;
1401
Jens Axboe32960612019-09-23 11:05:34 -06001402 if (file->f_op->read_iter)
1403 ret2 = call_read_iter(file, kiocb, &iter);
1404 else
1405 ret2 = loop_rw_iter(READ, file, kiocb, &iter);
1406
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001407 /*
1408 * In case of a short read, punt to async. This can happen
1409 * if we have data partially cached. Alternatively we can
1410 * return the short read, in which case the application will
1411 * need to issue another SQE and wait for it. That SQE will
1412 * need async punt anyway, so it's more efficient to do it
1413 * here.
1414 */
Jens Axboe491381ce2019-10-17 09:20:46 -06001415 if (force_nonblock && !(req->flags & REQ_F_NOWAIT) &&
1416 (req->flags & REQ_F_ISREG) &&
1417 ret2 > 0 && ret2 < read_size)
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001418 ret2 = -EAGAIN;
1419 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboe31b51512019-01-18 22:56:34 -07001420 if (!force_nonblock || ret2 != -EAGAIN) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001421 io_rw_done(kiocb, ret2);
Jens Axboe31b51512019-01-18 22:56:34 -07001422 } else {
1423 /*
1424 * If ->needs_lock is true, we're already in async
1425 * context.
1426 */
1427 if (!s->needs_lock)
1428 io_async_list_note(READ, req, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001429 ret = -EAGAIN;
Jens Axboe31b51512019-01-18 22:56:34 -07001430 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001431 }
1432 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001433 return ret;
1434}
1435
Jens Axboee0c5c572019-03-12 10:18:47 -06001436static int io_write(struct io_kiocb *req, const struct sqe_submit *s,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001437 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001438{
1439 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1440 struct kiocb *kiocb = &req->rw;
1441 struct iov_iter iter;
1442 struct file *file;
Jens Axboe31b51512019-01-18 22:56:34 -07001443 size_t iov_count;
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001444 ssize_t ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001445
Jens Axboe8358e3a2019-04-23 08:17:58 -06001446 ret = io_prep_rw(req, s, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001447 if (ret)
1448 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001449
Jens Axboe2b188cc2019-01-07 10:46:33 -07001450 file = kiocb->ki_filp;
1451 if (unlikely(!(file->f_mode & FMODE_WRITE)))
Jens Axboe09bb8392019-03-13 12:39:28 -06001452 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001453
1454 ret = io_import_iovec(req->ctx, WRITE, s, &iovec, &iter);
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001455 if (ret < 0)
Jens Axboe09bb8392019-03-13 12:39:28 -06001456 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001457
Jens Axboe9e645e112019-05-10 16:07:28 -06001458 if (req->flags & REQ_F_LINK)
1459 req->result = ret;
1460
Jens Axboe31b51512019-01-18 22:56:34 -07001461 iov_count = iov_iter_count(&iter);
1462
1463 ret = -EAGAIN;
1464 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT)) {
1465 /* If ->needs_lock is true, we're already in async context. */
1466 if (!s->needs_lock)
1467 io_async_list_note(WRITE, req, iov_count);
1468 goto out_free;
1469 }
1470
1471 ret = rw_verify_area(WRITE, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001472 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01001473 ssize_t ret2;
1474
Jens Axboe2b188cc2019-01-07 10:46:33 -07001475 /*
1476 * Open-code file_start_write here to grab freeze protection,
1477 * which will be released by another thread in
1478 * io_complete_rw(). Fool lockdep by telling it the lock got
1479 * released so that it doesn't complain about the held lock when
1480 * we return to userspace.
1481 */
Jens Axboe491381ce2019-10-17 09:20:46 -06001482 if (req->flags & REQ_F_ISREG) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001483 __sb_start_write(file_inode(file)->i_sb,
1484 SB_FREEZE_WRITE, true);
1485 __sb_writers_release(file_inode(file)->i_sb,
1486 SB_FREEZE_WRITE);
1487 }
1488 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01001489
Jens Axboe32960612019-09-23 11:05:34 -06001490 if (file->f_op->write_iter)
1491 ret2 = call_write_iter(file, kiocb, &iter);
1492 else
1493 ret2 = loop_rw_iter(WRITE, file, kiocb, &iter);
Roman Penyaev9bf79332019-03-25 20:09:24 +01001494 if (!force_nonblock || ret2 != -EAGAIN) {
1495 io_rw_done(kiocb, ret2);
1496 } else {
1497 /*
1498 * If ->needs_lock is true, we're already in async
1499 * context.
1500 */
1501 if (!s->needs_lock)
1502 io_async_list_note(WRITE, req, iov_count);
1503 ret = -EAGAIN;
1504 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001505 }
Jens Axboe31b51512019-01-18 22:56:34 -07001506out_free:
Jens Axboe2b188cc2019-01-07 10:46:33 -07001507 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001508 return ret;
1509}
1510
1511/*
1512 * IORING_OP_NOP just posts a completion event, nothing else.
1513 */
1514static int io_nop(struct io_kiocb *req, u64 user_data)
1515{
1516 struct io_ring_ctx *ctx = req->ctx;
1517 long err = 0;
1518
Jens Axboedef596e2019-01-09 08:59:42 -07001519 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1520 return -EINVAL;
1521
Jens Axboec71ffb62019-05-13 20:58:29 -06001522 io_cqring_add_event(ctx, user_data, err);
Jens Axboee65ef562019-03-12 10:16:44 -06001523 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001524 return 0;
1525}
1526
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001527static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1528{
Jens Axboe6b063142019-01-10 22:13:58 -07001529 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001530
Jens Axboe09bb8392019-03-13 12:39:28 -06001531 if (!req->file)
1532 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001533
Jens Axboe6b063142019-01-10 22:13:58 -07001534 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07001535 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07001536 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001537 return -EINVAL;
1538
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001539 return 0;
1540}
1541
1542static int io_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1543 bool force_nonblock)
1544{
1545 loff_t sqe_off = READ_ONCE(sqe->off);
1546 loff_t sqe_len = READ_ONCE(sqe->len);
1547 loff_t end = sqe_off + sqe_len;
1548 unsigned fsync_flags;
1549 int ret;
1550
1551 fsync_flags = READ_ONCE(sqe->fsync_flags);
1552 if (unlikely(fsync_flags & ~IORING_FSYNC_DATASYNC))
1553 return -EINVAL;
1554
1555 ret = io_prep_fsync(req, sqe);
1556 if (ret)
1557 return ret;
1558
1559 /* fsync always requires a blocking context */
1560 if (force_nonblock)
1561 return -EAGAIN;
1562
1563 ret = vfs_fsync_range(req->rw.ki_filp, sqe_off,
1564 end > 0 ? end : LLONG_MAX,
1565 fsync_flags & IORING_FSYNC_DATASYNC);
1566
Jens Axboe9e645e112019-05-10 16:07:28 -06001567 if (ret < 0 && (req->flags & REQ_F_LINK))
1568 req->flags |= REQ_F_FAIL_LINK;
Jens Axboec71ffb62019-05-13 20:58:29 -06001569 io_cqring_add_event(req->ctx, sqe->user_data, ret);
Jens Axboee65ef562019-03-12 10:16:44 -06001570 io_put_req(req);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001571 return 0;
1572}
1573
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001574static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1575{
1576 struct io_ring_ctx *ctx = req->ctx;
1577 int ret = 0;
1578
1579 if (!req->file)
1580 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001581
1582 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1583 return -EINVAL;
1584 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
1585 return -EINVAL;
1586
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001587 return ret;
1588}
1589
1590static int io_sync_file_range(struct io_kiocb *req,
1591 const struct io_uring_sqe *sqe,
1592 bool force_nonblock)
1593{
1594 loff_t sqe_off;
1595 loff_t sqe_len;
1596 unsigned flags;
1597 int ret;
1598
1599 ret = io_prep_sfr(req, sqe);
1600 if (ret)
1601 return ret;
1602
1603 /* sync_file_range always requires a blocking context */
1604 if (force_nonblock)
1605 return -EAGAIN;
1606
1607 sqe_off = READ_ONCE(sqe->off);
1608 sqe_len = READ_ONCE(sqe->len);
1609 flags = READ_ONCE(sqe->sync_range_flags);
1610
1611 ret = sync_file_range(req->rw.ki_filp, sqe_off, sqe_len, flags);
1612
Jens Axboe9e645e112019-05-10 16:07:28 -06001613 if (ret < 0 && (req->flags & REQ_F_LINK))
1614 req->flags |= REQ_F_FAIL_LINK;
Jens Axboec71ffb62019-05-13 20:58:29 -06001615 io_cqring_add_event(req->ctx, sqe->user_data, ret);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001616 io_put_req(req);
1617 return 0;
1618}
1619
Jens Axboe0fa03c62019-04-19 13:34:07 -06001620#if defined(CONFIG_NET)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001621static int io_send_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1622 bool force_nonblock,
1623 long (*fn)(struct socket *, struct user_msghdr __user *,
1624 unsigned int))
1625{
Jens Axboe0fa03c62019-04-19 13:34:07 -06001626 struct socket *sock;
1627 int ret;
1628
1629 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1630 return -EINVAL;
1631
1632 sock = sock_from_file(req->file, &ret);
1633 if (sock) {
1634 struct user_msghdr __user *msg;
1635 unsigned flags;
1636
1637 flags = READ_ONCE(sqe->msg_flags);
1638 if (flags & MSG_DONTWAIT)
1639 req->flags |= REQ_F_NOWAIT;
1640 else if (force_nonblock)
1641 flags |= MSG_DONTWAIT;
1642
1643 msg = (struct user_msghdr __user *) (unsigned long)
1644 READ_ONCE(sqe->addr);
1645
Jens Axboeaa1fa282019-04-19 13:38:09 -06001646 ret = fn(sock, msg, flags);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001647 if (force_nonblock && ret == -EAGAIN)
1648 return ret;
1649 }
1650
1651 io_cqring_add_event(req->ctx, sqe->user_data, ret);
1652 io_put_req(req);
1653 return 0;
Jens Axboeaa1fa282019-04-19 13:38:09 -06001654}
1655#endif
1656
1657static int io_sendmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1658 bool force_nonblock)
1659{
1660#if defined(CONFIG_NET)
1661 return io_send_recvmsg(req, sqe, force_nonblock, __sys_sendmsg_sock);
1662#else
1663 return -EOPNOTSUPP;
1664#endif
1665}
1666
1667static int io_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1668 bool force_nonblock)
1669{
1670#if defined(CONFIG_NET)
1671 return io_send_recvmsg(req, sqe, force_nonblock, __sys_recvmsg_sock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001672#else
1673 return -EOPNOTSUPP;
1674#endif
1675}
1676
Jens Axboe221c5eb2019-01-17 09:41:58 -07001677static void io_poll_remove_one(struct io_kiocb *req)
1678{
1679 struct io_poll_iocb *poll = &req->poll;
1680
1681 spin_lock(&poll->head->lock);
1682 WRITE_ONCE(poll->canceled, true);
1683 if (!list_empty(&poll->wait.entry)) {
1684 list_del_init(&poll->wait.entry);
Jens Axboe18d9be12019-09-10 09:13:05 -06001685 io_queue_async_work(req->ctx, req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001686 }
1687 spin_unlock(&poll->head->lock);
1688
1689 list_del_init(&req->list);
1690}
1691
1692static void io_poll_remove_all(struct io_ring_ctx *ctx)
1693{
1694 struct io_kiocb *req;
1695
1696 spin_lock_irq(&ctx->completion_lock);
1697 while (!list_empty(&ctx->cancel_list)) {
1698 req = list_first_entry(&ctx->cancel_list, struct io_kiocb,list);
1699 io_poll_remove_one(req);
1700 }
1701 spin_unlock_irq(&ctx->completion_lock);
1702}
1703
1704/*
1705 * Find a running poll command that matches one specified in sqe->addr,
1706 * and remove it if found.
1707 */
1708static int io_poll_remove(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1709{
1710 struct io_ring_ctx *ctx = req->ctx;
1711 struct io_kiocb *poll_req, *next;
1712 int ret = -ENOENT;
1713
1714 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1715 return -EINVAL;
1716 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
1717 sqe->poll_events)
1718 return -EINVAL;
1719
1720 spin_lock_irq(&ctx->completion_lock);
1721 list_for_each_entry_safe(poll_req, next, &ctx->cancel_list, list) {
1722 if (READ_ONCE(sqe->addr) == poll_req->user_data) {
1723 io_poll_remove_one(poll_req);
1724 ret = 0;
1725 break;
1726 }
1727 }
1728 spin_unlock_irq(&ctx->completion_lock);
1729
Jens Axboec71ffb62019-05-13 20:58:29 -06001730 io_cqring_add_event(req->ctx, sqe->user_data, ret);
Jens Axboee65ef562019-03-12 10:16:44 -06001731 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001732 return 0;
1733}
1734
Jens Axboe8c838782019-03-12 15:48:16 -06001735static void io_poll_complete(struct io_ring_ctx *ctx, struct io_kiocb *req,
1736 __poll_t mask)
Jens Axboe221c5eb2019-01-17 09:41:58 -07001737{
Jens Axboe8c838782019-03-12 15:48:16 -06001738 req->poll.done = true;
Jens Axboec71ffb62019-05-13 20:58:29 -06001739 io_cqring_fill_event(ctx, req->user_data, mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06001740 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001741}
1742
1743static void io_poll_complete_work(struct work_struct *work)
1744{
1745 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
1746 struct io_poll_iocb *poll = &req->poll;
1747 struct poll_table_struct pt = { ._key = poll->events };
1748 struct io_ring_ctx *ctx = req->ctx;
1749 __poll_t mask = 0;
1750
1751 if (!READ_ONCE(poll->canceled))
1752 mask = vfs_poll(poll->file, &pt) & poll->events;
1753
1754 /*
1755 * Note that ->ki_cancel callers also delete iocb from active_reqs after
1756 * calling ->ki_cancel. We need the ctx_lock roundtrip here to
1757 * synchronize with them. In the cancellation case the list_del_init
1758 * itself is not actually needed, but harmless so we keep it in to
1759 * avoid further branches in the fast path.
1760 */
1761 spin_lock_irq(&ctx->completion_lock);
1762 if (!mask && !READ_ONCE(poll->canceled)) {
1763 add_wait_queue(poll->head, &poll->wait);
1764 spin_unlock_irq(&ctx->completion_lock);
1765 return;
1766 }
1767 list_del_init(&req->list);
Jens Axboe8c838782019-03-12 15:48:16 -06001768 io_poll_complete(ctx, req, mask);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001769 spin_unlock_irq(&ctx->completion_lock);
1770
Jens Axboe8c838782019-03-12 15:48:16 -06001771 io_cqring_ev_posted(ctx);
1772 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001773}
1774
1775static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
1776 void *key)
1777{
1778 struct io_poll_iocb *poll = container_of(wait, struct io_poll_iocb,
1779 wait);
1780 struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
1781 struct io_ring_ctx *ctx = req->ctx;
1782 __poll_t mask = key_to_poll(key);
Jens Axboe8c838782019-03-12 15:48:16 -06001783 unsigned long flags;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001784
1785 /* for instances that support it check for an event match first: */
Jens Axboe8c838782019-03-12 15:48:16 -06001786 if (mask && !(mask & poll->events))
1787 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001788
1789 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06001790
1791 if (mask && spin_trylock_irqsave(&ctx->completion_lock, flags)) {
1792 list_del(&req->list);
1793 io_poll_complete(ctx, req, mask);
1794 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1795
1796 io_cqring_ev_posted(ctx);
1797 io_put_req(req);
1798 } else {
Jens Axboe18d9be12019-09-10 09:13:05 -06001799 io_queue_async_work(ctx, req);
Jens Axboe8c838782019-03-12 15:48:16 -06001800 }
1801
Jens Axboe221c5eb2019-01-17 09:41:58 -07001802 return 1;
1803}
1804
1805struct io_poll_table {
1806 struct poll_table_struct pt;
1807 struct io_kiocb *req;
1808 int error;
1809};
1810
1811static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
1812 struct poll_table_struct *p)
1813{
1814 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
1815
1816 if (unlikely(pt->req->poll.head)) {
1817 pt->error = -EINVAL;
1818 return;
1819 }
1820
1821 pt->error = 0;
1822 pt->req->poll.head = head;
1823 add_wait_queue(head, &pt->req->poll.wait);
1824}
1825
1826static int io_poll_add(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1827{
1828 struct io_poll_iocb *poll = &req->poll;
1829 struct io_ring_ctx *ctx = req->ctx;
1830 struct io_poll_table ipt;
Jens Axboe8c838782019-03-12 15:48:16 -06001831 bool cancel = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001832 __poll_t mask;
1833 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001834
1835 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1836 return -EINVAL;
1837 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
1838 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06001839 if (!poll->file)
1840 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001841
Jens Axboe6cc47d12019-09-18 11:18:23 -06001842 req->submit.sqe = NULL;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001843 INIT_WORK(&req->work, io_poll_complete_work);
1844 events = READ_ONCE(sqe->poll_events);
1845 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
1846
Jens Axboe221c5eb2019-01-17 09:41:58 -07001847 poll->head = NULL;
Jens Axboe8c838782019-03-12 15:48:16 -06001848 poll->done = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001849 poll->canceled = false;
1850
1851 ipt.pt._qproc = io_poll_queue_proc;
1852 ipt.pt._key = poll->events;
1853 ipt.req = req;
1854 ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
1855
1856 /* initialized the list so that we can do list_empty checks */
1857 INIT_LIST_HEAD(&poll->wait.entry);
1858 init_waitqueue_func_entry(&poll->wait, io_poll_wake);
1859
Jens Axboe36703242019-07-25 10:20:18 -06001860 INIT_LIST_HEAD(&req->list);
1861
Jens Axboe221c5eb2019-01-17 09:41:58 -07001862 mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001863
1864 spin_lock_irq(&ctx->completion_lock);
Jens Axboe8c838782019-03-12 15:48:16 -06001865 if (likely(poll->head)) {
1866 spin_lock(&poll->head->lock);
1867 if (unlikely(list_empty(&poll->wait.entry))) {
1868 if (ipt.error)
1869 cancel = true;
1870 ipt.error = 0;
1871 mask = 0;
1872 }
1873 if (mask || ipt.error)
1874 list_del_init(&poll->wait.entry);
1875 else if (cancel)
1876 WRITE_ONCE(poll->canceled, true);
1877 else if (!poll->done) /* actually waiting for an event */
1878 list_add_tail(&req->list, &ctx->cancel_list);
1879 spin_unlock(&poll->head->lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001880 }
Jens Axboe8c838782019-03-12 15:48:16 -06001881 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06001882 ipt.error = 0;
1883 io_poll_complete(ctx, req, mask);
1884 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07001885 spin_unlock_irq(&ctx->completion_lock);
1886
Jens Axboe8c838782019-03-12 15:48:16 -06001887 if (mask) {
1888 io_cqring_ev_posted(ctx);
Jens Axboee65ef562019-03-12 10:16:44 -06001889 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001890 }
Jens Axboe8c838782019-03-12 15:48:16 -06001891 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001892}
1893
Jens Axboe5262f562019-09-17 12:26:57 -06001894static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
1895{
1896 struct io_ring_ctx *ctx;
zhangyi (F)ef036812019-10-23 15:10:08 +08001897 struct io_kiocb *req, *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06001898 unsigned long flags;
1899
1900 req = container_of(timer, struct io_kiocb, timeout.timer);
1901 ctx = req->ctx;
1902 atomic_inc(&ctx->cq_timeouts);
1903
1904 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08001905 /*
1906 * Adjust the reqs sequence before the current one because it
1907 * will consume a slot in the cq_ring and the the cq_tail pointer
1908 * will be increased, otherwise other timeout reqs may return in
1909 * advance without waiting for enough wait_nr.
1910 */
1911 prev = req;
1912 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
1913 prev->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06001914 list_del(&req->list);
1915
1916 io_cqring_fill_event(ctx, req->user_data, -ETIME);
1917 io_commit_cqring(ctx);
1918 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1919
1920 io_cqring_ev_posted(ctx);
1921
1922 io_put_req(req);
1923 return HRTIMER_NORESTART;
1924}
1925
1926static int io_timeout(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1927{
yangerkun5da0fb12019-10-15 21:59:29 +08001928 unsigned count;
Jens Axboe5262f562019-09-17 12:26:57 -06001929 struct io_ring_ctx *ctx = req->ctx;
1930 struct list_head *entry;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06001931 struct timespec64 ts;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08001932 unsigned span = 0;
Jens Axboe5262f562019-09-17 12:26:57 -06001933
1934 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1935 return -EINVAL;
1936 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->timeout_flags ||
1937 sqe->len != 1)
1938 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06001939
1940 if (get_timespec64(&ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06001941 return -EFAULT;
1942
1943 /*
1944 * sqe->off holds how many events that need to occur for this
1945 * timeout event to be satisfied.
1946 */
1947 count = READ_ONCE(sqe->off);
1948 if (!count)
1949 count = 1;
1950
1951 req->sequence = ctx->cached_sq_head + count - 1;
yangerkun5da0fb12019-10-15 21:59:29 +08001952 /* reuse it to store the count */
1953 req->submit.sequence = count;
Jens Axboe5262f562019-09-17 12:26:57 -06001954 req->flags |= REQ_F_TIMEOUT;
1955
1956 /*
1957 * Insertion sort, ensuring the first entry in the list is always
1958 * the one we need first.
1959 */
Jens Axboe5262f562019-09-17 12:26:57 -06001960 spin_lock_irq(&ctx->completion_lock);
1961 list_for_each_prev(entry, &ctx->timeout_list) {
1962 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
yangerkun5da0fb12019-10-15 21:59:29 +08001963 unsigned nxt_sq_head;
1964 long long tmp, tmp_nxt;
Jens Axboe5262f562019-09-17 12:26:57 -06001965
yangerkun5da0fb12019-10-15 21:59:29 +08001966 /*
1967 * Since cached_sq_head + count - 1 can overflow, use type long
1968 * long to store it.
1969 */
1970 tmp = (long long)ctx->cached_sq_head + count - 1;
1971 nxt_sq_head = nxt->sequence - nxt->submit.sequence + 1;
1972 tmp_nxt = (long long)nxt_sq_head + nxt->submit.sequence - 1;
1973
1974 /*
1975 * cached_sq_head may overflow, and it will never overflow twice
1976 * once there is some timeout req still be valid.
1977 */
1978 if (ctx->cached_sq_head < nxt_sq_head)
yangerkun8b07a652019-10-17 12:12:35 +08001979 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08001980
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08001981 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06001982 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08001983
1984 /*
1985 * Sequence of reqs after the insert one and itself should
1986 * be adjusted because each timeout req consumes a slot.
1987 */
1988 span++;
1989 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06001990 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08001991 req->sequence -= span;
Jens Axboe5262f562019-09-17 12:26:57 -06001992 list_add(&req->list, entry);
1993 spin_unlock_irq(&ctx->completion_lock);
1994
1995 hrtimer_init(&req->timeout.timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1996 req->timeout.timer.function = io_timeout_fn;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06001997 hrtimer_start(&req->timeout.timer, timespec64_to_ktime(ts),
Jens Axboe5262f562019-09-17 12:26:57 -06001998 HRTIMER_MODE_REL);
1999 return 0;
2000}
2001
Jens Axboede0617e2019-04-06 21:51:27 -06002002static int io_req_defer(struct io_ring_ctx *ctx, struct io_kiocb *req,
2003 const struct io_uring_sqe *sqe)
2004{
2005 struct io_uring_sqe *sqe_copy;
2006
2007 if (!io_sequence_defer(ctx, req) && list_empty(&ctx->defer_list))
2008 return 0;
2009
2010 sqe_copy = kmalloc(sizeof(*sqe_copy), GFP_KERNEL);
2011 if (!sqe_copy)
2012 return -EAGAIN;
2013
2014 spin_lock_irq(&ctx->completion_lock);
2015 if (!io_sequence_defer(ctx, req) && list_empty(&ctx->defer_list)) {
2016 spin_unlock_irq(&ctx->completion_lock);
2017 kfree(sqe_copy);
2018 return 0;
2019 }
2020
2021 memcpy(sqe_copy, sqe, sizeof(*sqe_copy));
2022 req->submit.sqe = sqe_copy;
2023
2024 INIT_WORK(&req->work, io_sq_wq_submit_work);
2025 list_add_tail(&req->list, &ctx->defer_list);
2026 spin_unlock_irq(&ctx->completion_lock);
2027 return -EIOCBQUEUED;
2028}
2029
Jens Axboe2b188cc2019-01-07 10:46:33 -07002030static int __io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
Jens Axboe8358e3a2019-04-23 08:17:58 -06002031 const struct sqe_submit *s, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002032{
Jens Axboee0c5c572019-03-12 10:18:47 -06002033 int ret, opcode;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002034
Jens Axboe9e645e112019-05-10 16:07:28 -06002035 req->user_data = READ_ONCE(s->sqe->user_data);
2036
Jens Axboe2b188cc2019-01-07 10:46:33 -07002037 if (unlikely(s->index >= ctx->sq_entries))
2038 return -EINVAL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002039
2040 opcode = READ_ONCE(s->sqe->opcode);
2041 switch (opcode) {
2042 case IORING_OP_NOP:
2043 ret = io_nop(req, req->user_data);
2044 break;
2045 case IORING_OP_READV:
Jens Axboeedafcce2019-01-09 09:16:05 -07002046 if (unlikely(s->sqe->buf_index))
2047 return -EINVAL;
Jens Axboe8358e3a2019-04-23 08:17:58 -06002048 ret = io_read(req, s, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002049 break;
2050 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07002051 if (unlikely(s->sqe->buf_index))
2052 return -EINVAL;
Jens Axboe8358e3a2019-04-23 08:17:58 -06002053 ret = io_write(req, s, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07002054 break;
2055 case IORING_OP_READ_FIXED:
Jens Axboe8358e3a2019-04-23 08:17:58 -06002056 ret = io_read(req, s, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07002057 break;
2058 case IORING_OP_WRITE_FIXED:
Jens Axboe8358e3a2019-04-23 08:17:58 -06002059 ret = io_write(req, s, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002060 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002061 case IORING_OP_FSYNC:
2062 ret = io_fsync(req, s->sqe, force_nonblock);
2063 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002064 case IORING_OP_POLL_ADD:
2065 ret = io_poll_add(req, s->sqe);
2066 break;
2067 case IORING_OP_POLL_REMOVE:
2068 ret = io_poll_remove(req, s->sqe);
2069 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002070 case IORING_OP_SYNC_FILE_RANGE:
2071 ret = io_sync_file_range(req, s->sqe, force_nonblock);
2072 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002073 case IORING_OP_SENDMSG:
2074 ret = io_sendmsg(req, s->sqe, force_nonblock);
2075 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06002076 case IORING_OP_RECVMSG:
2077 ret = io_recvmsg(req, s->sqe, force_nonblock);
2078 break;
Jens Axboe5262f562019-09-17 12:26:57 -06002079 case IORING_OP_TIMEOUT:
2080 ret = io_timeout(req, s->sqe);
2081 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002082 default:
2083 ret = -EINVAL;
2084 break;
2085 }
2086
Jens Axboedef596e2019-01-09 08:59:42 -07002087 if (ret)
2088 return ret;
2089
2090 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe9e645e112019-05-10 16:07:28 -06002091 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07002092 return -EAGAIN;
2093
2094 /* workqueue context doesn't hold uring_lock, grab it now */
2095 if (s->needs_lock)
2096 mutex_lock(&ctx->uring_lock);
2097 io_iopoll_req_issued(req);
2098 if (s->needs_lock)
2099 mutex_unlock(&ctx->uring_lock);
2100 }
2101
2102 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002103}
2104
Jens Axboe31b51512019-01-18 22:56:34 -07002105static struct async_list *io_async_list_from_sqe(struct io_ring_ctx *ctx,
2106 const struct io_uring_sqe *sqe)
2107{
2108 switch (sqe->opcode) {
2109 case IORING_OP_READV:
2110 case IORING_OP_READ_FIXED:
2111 return &ctx->pending_async[READ];
2112 case IORING_OP_WRITEV:
2113 case IORING_OP_WRITE_FIXED:
2114 return &ctx->pending_async[WRITE];
2115 default:
2116 return NULL;
2117 }
2118}
2119
Jens Axboeedafcce2019-01-09 09:16:05 -07002120static inline bool io_sqe_needs_user(const struct io_uring_sqe *sqe)
2121{
2122 u8 opcode = READ_ONCE(sqe->opcode);
2123
2124 return !(opcode == IORING_OP_READ_FIXED ||
2125 opcode == IORING_OP_WRITE_FIXED);
2126}
2127
Jens Axboe2b188cc2019-01-07 10:46:33 -07002128static void io_sq_wq_submit_work(struct work_struct *work)
2129{
2130 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002131 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe31b51512019-01-18 22:56:34 -07002132 struct mm_struct *cur_mm = NULL;
2133 struct async_list *async_list;
2134 LIST_HEAD(req_list);
Jens Axboeedafcce2019-01-09 09:16:05 -07002135 mm_segment_t old_fs;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002136 int ret;
2137
Jens Axboe31b51512019-01-18 22:56:34 -07002138 async_list = io_async_list_from_sqe(ctx, req->submit.sqe);
2139restart:
2140 do {
2141 struct sqe_submit *s = &req->submit;
2142 const struct io_uring_sqe *sqe = s->sqe;
Jackie Liud0ee8792019-07-31 14:39:33 +08002143 unsigned int flags = req->flags;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002144
Stefan Bühler8449eed2019-04-27 20:34:19 +02002145 /* Ensure we clear previously set non-block flag */
Jens Axboe31b51512019-01-18 22:56:34 -07002146 req->rw.ki_flags &= ~IOCB_NOWAIT;
2147
2148 ret = 0;
2149 if (io_sqe_needs_user(sqe) && !cur_mm) {
2150 if (!mmget_not_zero(ctx->sqo_mm)) {
2151 ret = -EFAULT;
2152 } else {
2153 cur_mm = ctx->sqo_mm;
2154 use_mm(cur_mm);
2155 old_fs = get_fs();
2156 set_fs(USER_DS);
2157 }
2158 }
2159
2160 if (!ret) {
2161 s->has_user = cur_mm != NULL;
2162 s->needs_lock = true;
2163 do {
Jens Axboe8358e3a2019-04-23 08:17:58 -06002164 ret = __io_submit_sqe(ctx, req, s, false);
Jens Axboe31b51512019-01-18 22:56:34 -07002165 /*
2166 * We can get EAGAIN for polled IO even though
2167 * we're forcing a sync submission from here,
2168 * since we can't wait for request slots on the
2169 * block side.
2170 */
2171 if (ret != -EAGAIN)
2172 break;
2173 cond_resched();
2174 } while (1);
2175 }
Jens Axboe817869d2019-04-30 14:44:05 -06002176
2177 /* drop submission reference */
2178 io_put_req(req);
2179
Jens Axboe31b51512019-01-18 22:56:34 -07002180 if (ret) {
Jens Axboec71ffb62019-05-13 20:58:29 -06002181 io_cqring_add_event(ctx, sqe->user_data, ret);
Jens Axboee65ef562019-03-12 10:16:44 -06002182 io_put_req(req);
Jens Axboe31b51512019-01-18 22:56:34 -07002183 }
2184
2185 /* async context always use a copy of the sqe */
2186 kfree(sqe);
2187
Zhengyuan Liuf7b76ac2019-07-16 23:26:14 +08002188 /* req from defer and link list needn't decrease async cnt */
Jackie Liud0ee8792019-07-31 14:39:33 +08002189 if (flags & (REQ_F_IO_DRAINED | REQ_F_LINK_DONE))
Zhengyuan Liuf7b76ac2019-07-16 23:26:14 +08002190 goto out;
2191
Jens Axboe31b51512019-01-18 22:56:34 -07002192 if (!async_list)
2193 break;
2194 if (!list_empty(&req_list)) {
2195 req = list_first_entry(&req_list, struct io_kiocb,
2196 list);
2197 list_del(&req->list);
2198 continue;
2199 }
2200 if (list_empty(&async_list->list))
2201 break;
2202
2203 req = NULL;
2204 spin_lock(&async_list->lock);
2205 if (list_empty(&async_list->list)) {
2206 spin_unlock(&async_list->lock);
2207 break;
2208 }
2209 list_splice_init(&async_list->list, &req_list);
2210 spin_unlock(&async_list->lock);
2211
2212 req = list_first_entry(&req_list, struct io_kiocb, list);
2213 list_del(&req->list);
2214 } while (req);
Jens Axboeedafcce2019-01-09 09:16:05 -07002215
2216 /*
Jens Axboe31b51512019-01-18 22:56:34 -07002217 * Rare case of racing with a submitter. If we find the count has
2218 * dropped to zero AND we have pending work items, then restart
2219 * the processing. This is a tiny race window.
Jens Axboeedafcce2019-01-09 09:16:05 -07002220 */
Jens Axboe31b51512019-01-18 22:56:34 -07002221 if (async_list) {
2222 ret = atomic_dec_return(&async_list->cnt);
2223 while (!ret && !list_empty(&async_list->list)) {
2224 spin_lock(&async_list->lock);
2225 atomic_inc(&async_list->cnt);
2226 list_splice_init(&async_list->list, &req_list);
2227 spin_unlock(&async_list->lock);
2228
2229 if (!list_empty(&req_list)) {
2230 req = list_first_entry(&req_list,
2231 struct io_kiocb, list);
2232 list_del(&req->list);
2233 goto restart;
2234 }
2235 ret = atomic_dec_return(&async_list->cnt);
Jens Axboeedafcce2019-01-09 09:16:05 -07002236 }
Jens Axboeedafcce2019-01-09 09:16:05 -07002237 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002238
Zhengyuan Liuf7b76ac2019-07-16 23:26:14 +08002239out:
Jens Axboe31b51512019-01-18 22:56:34 -07002240 if (cur_mm) {
Jens Axboeedafcce2019-01-09 09:16:05 -07002241 set_fs(old_fs);
Jens Axboe31b51512019-01-18 22:56:34 -07002242 unuse_mm(cur_mm);
2243 mmput(cur_mm);
Jens Axboeedafcce2019-01-09 09:16:05 -07002244 }
Jens Axboe31b51512019-01-18 22:56:34 -07002245}
Jens Axboe2b188cc2019-01-07 10:46:33 -07002246
Jens Axboe31b51512019-01-18 22:56:34 -07002247/*
2248 * See if we can piggy back onto previously submitted work, that is still
2249 * running. We currently only allow this if the new request is sequential
2250 * to the previous one we punted.
2251 */
2252static bool io_add_to_prev_work(struct async_list *list, struct io_kiocb *req)
2253{
Jens Axboe6d5d5ac2019-09-11 10:16:13 -06002254 bool ret;
Jens Axboe31b51512019-01-18 22:56:34 -07002255
2256 if (!list)
2257 return false;
2258 if (!(req->flags & REQ_F_SEQ_PREV))
2259 return false;
2260 if (!atomic_read(&list->cnt))
2261 return false;
2262
2263 ret = true;
2264 spin_lock(&list->lock);
2265 list_add_tail(&req->list, &list->list);
Zhengyuan Liuc0e48f92019-07-18 20:44:00 +08002266 /*
2267 * Ensure we see a simultaneous modification from io_sq_wq_submit_work()
2268 */
2269 smp_mb();
Jens Axboe31b51512019-01-18 22:56:34 -07002270 if (!atomic_read(&list->cnt)) {
2271 list_del_init(&req->list);
2272 ret = false;
2273 }
2274 spin_unlock(&list->lock);
2275 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002276}
2277
Jens Axboe09bb8392019-03-13 12:39:28 -06002278static bool io_op_needs_file(const struct io_uring_sqe *sqe)
2279{
2280 int op = READ_ONCE(sqe->opcode);
2281
2282 switch (op) {
2283 case IORING_OP_NOP:
2284 case IORING_OP_POLL_REMOVE:
2285 return false;
2286 default:
2287 return true;
2288 }
2289}
2290
2291static int io_req_set_file(struct io_ring_ctx *ctx, const struct sqe_submit *s,
2292 struct io_submit_state *state, struct io_kiocb *req)
2293{
2294 unsigned flags;
2295 int fd;
2296
2297 flags = READ_ONCE(s->sqe->flags);
2298 fd = READ_ONCE(s->sqe->fd);
2299
Jackie Liu4fe2c962019-09-09 20:50:40 +08002300 if (flags & IOSQE_IO_DRAIN)
Jens Axboede0617e2019-04-06 21:51:27 -06002301 req->flags |= REQ_F_IO_DRAIN;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002302 /*
2303 * All io need record the previous position, if LINK vs DARIN,
2304 * it can be used to mark the position of the first IO in the
2305 * link list.
2306 */
2307 req->sequence = s->sequence;
Jens Axboede0617e2019-04-06 21:51:27 -06002308
Jens Axboe60c112b2019-06-21 10:20:18 -06002309 if (!io_op_needs_file(s->sqe))
Jens Axboe09bb8392019-03-13 12:39:28 -06002310 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06002311
2312 if (flags & IOSQE_FIXED_FILE) {
2313 if (unlikely(!ctx->user_files ||
2314 (unsigned) fd >= ctx->nr_user_files))
2315 return -EBADF;
2316 req->file = ctx->user_files[fd];
2317 req->flags |= REQ_F_FIXED_FILE;
2318 } else {
2319 if (s->needs_fixed_file)
2320 return -EBADF;
2321 req->file = io_file_get(state, fd);
2322 if (unlikely(!req->file))
2323 return -EBADF;
2324 }
2325
2326 return 0;
2327}
2328
Jackie Liu4fe2c962019-09-09 20:50:40 +08002329static int __io_queue_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
Jens Axboebc808bc2019-10-22 13:14:37 -06002330 struct sqe_submit *s)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002331{
Jens Axboee0c5c572019-03-12 10:18:47 -06002332 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002333
Jens Axboebc808bc2019-10-22 13:14:37 -06002334 ret = __io_submit_sqe(ctx, req, s, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06002335
2336 /*
2337 * We async punt it if the file wasn't marked NOWAIT, or if the file
2338 * doesn't support non-blocking read/write attempts
2339 */
2340 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
2341 (req->flags & REQ_F_MUST_PUNT))) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07002342 struct io_uring_sqe *sqe_copy;
2343
Jackie Liu954dab12019-09-18 10:37:52 +08002344 sqe_copy = kmemdup(s->sqe, sizeof(*sqe_copy), GFP_KERNEL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002345 if (sqe_copy) {
Jens Axboe31b51512019-01-18 22:56:34 -07002346 struct async_list *list;
2347
Jens Axboe2b188cc2019-01-07 10:46:33 -07002348 s->sqe = sqe_copy;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002349 memcpy(&req->submit, s, sizeof(*s));
Jens Axboe31b51512019-01-18 22:56:34 -07002350 list = io_async_list_from_sqe(ctx, s->sqe);
2351 if (!io_add_to_prev_work(list, req)) {
2352 if (list)
2353 atomic_inc(&list->cnt);
2354 INIT_WORK(&req->work, io_sq_wq_submit_work);
Jens Axboe18d9be12019-09-10 09:13:05 -06002355 io_queue_async_work(ctx, req);
Jens Axboe31b51512019-01-18 22:56:34 -07002356 }
Jens Axboee65ef562019-03-12 10:16:44 -06002357
2358 /*
2359 * Queued up for async execution, worker will release
Jens Axboe9e645e112019-05-10 16:07:28 -06002360 * submit reference when the iocb is actually submitted.
Jens Axboee65ef562019-03-12 10:16:44 -06002361 */
2362 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002363 }
2364 }
Jens Axboee65ef562019-03-12 10:16:44 -06002365
2366 /* drop submission reference */
2367 io_put_req(req);
2368
2369 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06002370 if (ret) {
2371 io_cqring_add_event(ctx, req->user_data, ret);
2372 if (req->flags & REQ_F_LINK)
2373 req->flags |= REQ_F_FAIL_LINK;
Jens Axboee65ef562019-03-12 10:16:44 -06002374 io_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002375 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002376
2377 return ret;
2378}
2379
Jackie Liu4fe2c962019-09-09 20:50:40 +08002380static int io_queue_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
Jens Axboebc808bc2019-10-22 13:14:37 -06002381 struct sqe_submit *s)
Jackie Liu4fe2c962019-09-09 20:50:40 +08002382{
2383 int ret;
2384
2385 ret = io_req_defer(ctx, req, s->sqe);
2386 if (ret) {
2387 if (ret != -EIOCBQUEUED) {
2388 io_free_req(req);
2389 io_cqring_add_event(ctx, s->sqe->user_data, ret);
2390 }
2391 return 0;
2392 }
2393
Jens Axboebc808bc2019-10-22 13:14:37 -06002394 return __io_queue_sqe(ctx, req, s);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002395}
2396
2397static int io_queue_link_head(struct io_ring_ctx *ctx, struct io_kiocb *req,
Jens Axboebc808bc2019-10-22 13:14:37 -06002398 struct sqe_submit *s, struct io_kiocb *shadow)
Jackie Liu4fe2c962019-09-09 20:50:40 +08002399{
2400 int ret;
2401 int need_submit = false;
2402
2403 if (!shadow)
Jens Axboebc808bc2019-10-22 13:14:37 -06002404 return io_queue_sqe(ctx, req, s);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002405
2406 /*
2407 * Mark the first IO in link list as DRAIN, let all the following
2408 * IOs enter the defer list. all IO needs to be completed before link
2409 * list.
2410 */
2411 req->flags |= REQ_F_IO_DRAIN;
2412 ret = io_req_defer(ctx, req, s->sqe);
2413 if (ret) {
2414 if (ret != -EIOCBQUEUED) {
2415 io_free_req(req);
Pavel Begunkov7b202382019-10-27 22:10:36 +03002416 __io_free_req(shadow);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002417 io_cqring_add_event(ctx, s->sqe->user_data, ret);
2418 return 0;
2419 }
2420 } else {
2421 /*
2422 * If ret == 0 means that all IOs in front of link io are
2423 * running done. let's queue link head.
2424 */
2425 need_submit = true;
2426 }
2427
2428 /* Insert shadow req to defer_list, blocking next IOs */
2429 spin_lock_irq(&ctx->completion_lock);
2430 list_add_tail(&shadow->list, &ctx->defer_list);
2431 spin_unlock_irq(&ctx->completion_lock);
2432
2433 if (need_submit)
Jens Axboebc808bc2019-10-22 13:14:37 -06002434 return __io_queue_sqe(ctx, req, s);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002435
2436 return 0;
2437}
2438
Jens Axboe9e645e112019-05-10 16:07:28 -06002439#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK)
2440
2441static void io_submit_sqe(struct io_ring_ctx *ctx, struct sqe_submit *s,
Jens Axboebc808bc2019-10-22 13:14:37 -06002442 struct io_submit_state *state, struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06002443{
2444 struct io_uring_sqe *sqe_copy;
2445 struct io_kiocb *req;
2446 int ret;
2447
2448 /* enforce forwards compatibility on users */
2449 if (unlikely(s->sqe->flags & ~SQE_VALID_FLAGS)) {
2450 ret = -EINVAL;
2451 goto err;
2452 }
2453
2454 req = io_get_req(ctx, state);
2455 if (unlikely(!req)) {
2456 ret = -EAGAIN;
2457 goto err;
2458 }
2459
2460 ret = io_req_set_file(ctx, s, state, req);
2461 if (unlikely(ret)) {
2462err_req:
2463 io_free_req(req);
2464err:
2465 io_cqring_add_event(ctx, s->sqe->user_data, ret);
2466 return;
2467 }
2468
Pavel Begunkov84d55dc2019-10-25 12:31:29 +03002469 req->user_data = s->sqe->user_data;
2470
Jens Axboe9e645e112019-05-10 16:07:28 -06002471 /*
2472 * If we already have a head request, queue this one for async
2473 * submittal once the head completes. If we don't have a head but
2474 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
2475 * submitted sync once the chain is complete. If none of those
2476 * conditions are true (normal request), then just queue it.
2477 */
2478 if (*link) {
2479 struct io_kiocb *prev = *link;
2480
2481 sqe_copy = kmemdup(s->sqe, sizeof(*sqe_copy), GFP_KERNEL);
2482 if (!sqe_copy) {
2483 ret = -EAGAIN;
2484 goto err_req;
2485 }
2486
2487 s->sqe = sqe_copy;
2488 memcpy(&req->submit, s, sizeof(*s));
2489 list_add_tail(&req->list, &prev->link_list);
2490 } else if (s->sqe->flags & IOSQE_IO_LINK) {
2491 req->flags |= REQ_F_LINK;
2492
2493 memcpy(&req->submit, s, sizeof(*s));
2494 INIT_LIST_HEAD(&req->link_list);
2495 *link = req;
2496 } else {
Jens Axboebc808bc2019-10-22 13:14:37 -06002497 io_queue_sqe(ctx, req, s);
Jens Axboe9e645e112019-05-10 16:07:28 -06002498 }
2499}
2500
Jens Axboe9a56a232019-01-09 09:06:50 -07002501/*
2502 * Batched submission is done, ensure local IO is flushed out.
2503 */
2504static void io_submit_state_end(struct io_submit_state *state)
2505{
2506 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06002507 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07002508 if (state->free_reqs)
2509 kmem_cache_free_bulk(req_cachep, state->free_reqs,
2510 &state->reqs[state->cur_req]);
Jens Axboe9a56a232019-01-09 09:06:50 -07002511}
2512
2513/*
2514 * Start submission side cache.
2515 */
2516static void io_submit_state_start(struct io_submit_state *state,
2517 struct io_ring_ctx *ctx, unsigned max_ios)
2518{
2519 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07002520 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07002521 state->file = NULL;
2522 state->ios_left = max_ios;
2523}
2524
Jens Axboe2b188cc2019-01-07 10:46:33 -07002525static void io_commit_sqring(struct io_ring_ctx *ctx)
2526{
Hristo Venev75b28af2019-08-26 17:23:46 +00002527 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002528
Hristo Venev75b28af2019-08-26 17:23:46 +00002529 if (ctx->cached_sq_head != READ_ONCE(rings->sq.head)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07002530 /*
2531 * Ensure any loads from the SQEs are done at this point,
2532 * since once we write the new head, the application could
2533 * write new data to them.
2534 */
Hristo Venev75b28af2019-08-26 17:23:46 +00002535 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002536 }
2537}
2538
2539/*
Jens Axboe2b188cc2019-01-07 10:46:33 -07002540 * Fetch an sqe, if one is available. Note that s->sqe will point to memory
2541 * that is mapped by userspace. This means that care needs to be taken to
2542 * ensure that reads are stable, as we cannot rely on userspace always
2543 * being a good citizen. If members of the sqe are validated and then later
2544 * used, it's important that those reads are done through READ_ONCE() to
2545 * prevent a re-load down the line.
2546 */
2547static bool io_get_sqring(struct io_ring_ctx *ctx, struct sqe_submit *s)
2548{
Hristo Venev75b28af2019-08-26 17:23:46 +00002549 struct io_rings *rings = ctx->rings;
2550 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002551 unsigned head;
2552
2553 /*
2554 * The cached sq head (or cq tail) serves two purposes:
2555 *
2556 * 1) allows us to batch the cost of updating the user visible
2557 * head updates.
2558 * 2) allows the kernel side to track the head on its own, even
2559 * though the application is the one updating it.
2560 */
2561 head = ctx->cached_sq_head;
Stefan Bühlere523a292019-04-19 11:57:44 +02002562 /* make sure SQ entry isn't read before tail */
Hristo Venev75b28af2019-08-26 17:23:46 +00002563 if (head == smp_load_acquire(&rings->sq.tail))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002564 return false;
2565
Hristo Venev75b28af2019-08-26 17:23:46 +00002566 head = READ_ONCE(sq_array[head & ctx->sq_mask]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002567 if (head < ctx->sq_entries) {
2568 s->index = head;
2569 s->sqe = &ctx->sq_sqes[head];
Jackie Liu8776f3f2019-09-09 20:50:39 +08002570 s->sequence = ctx->cached_sq_head;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002571 ctx->cached_sq_head++;
2572 return true;
2573 }
2574
2575 /* drop invalid entries */
2576 ctx->cached_sq_head++;
Jens Axboe498ccd92019-10-25 10:04:25 -06002577 ctx->cached_sq_dropped++;
2578 WRITE_ONCE(rings->sq_dropped, ctx->cached_sq_dropped);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002579 return false;
2580}
2581
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002582static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
2583 bool has_user, bool mm_fault)
Jens Axboe6c271ce2019-01-10 11:22:30 -07002584{
2585 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002586 struct io_kiocb *link = NULL;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002587 struct io_kiocb *shadow_req = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002588 bool prev_was_link = false;
2589 int i, submitted = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002590
2591 if (nr > IO_PLUG_THRESHOLD) {
2592 io_submit_state_start(&state, ctx, nr);
2593 statep = &state;
2594 }
2595
2596 for (i = 0; i < nr; i++) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002597 struct sqe_submit s;
2598
2599 if (!io_get_sqring(ctx, &s))
2600 break;
2601
Jens Axboe9e645e112019-05-10 16:07:28 -06002602 /*
2603 * If previous wasn't linked and we have a linked command,
2604 * that's the end of the chain. Submit the previous link.
2605 */
2606 if (!prev_was_link && link) {
Jens Axboebc808bc2019-10-22 13:14:37 -06002607 io_queue_link_head(ctx, link, &link->submit, shadow_req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002608 link = NULL;
Jackie Liu5f5ad9c2019-09-18 10:37:53 +08002609 shadow_req = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002610 }
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002611 prev_was_link = (s.sqe->flags & IOSQE_IO_LINK) != 0;
Jens Axboe9e645e112019-05-10 16:07:28 -06002612
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002613 if (link && (s.sqe->flags & IOSQE_IO_DRAIN)) {
Jackie Liu4fe2c962019-09-09 20:50:40 +08002614 if (!shadow_req) {
2615 shadow_req = io_get_req(ctx, NULL);
Jackie Liua1041c22019-09-18 17:25:52 +08002616 if (unlikely(!shadow_req))
2617 goto out;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002618 shadow_req->flags |= (REQ_F_IO_DRAIN | REQ_F_SHADOW_DRAIN);
2619 refcount_dec(&shadow_req->refs);
2620 }
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002621 shadow_req->sequence = s.sequence;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002622 }
2623
Jackie Liua1041c22019-09-18 17:25:52 +08002624out:
Jens Axboe6c271ce2019-01-10 11:22:30 -07002625 if (unlikely(mm_fault)) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002626 io_cqring_add_event(ctx, s.sqe->user_data,
Jens Axboe9e645e112019-05-10 16:07:28 -06002627 -EFAULT);
Jens Axboe6c271ce2019-01-10 11:22:30 -07002628 } else {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002629 s.has_user = has_user;
2630 s.needs_lock = true;
2631 s.needs_fixed_file = true;
2632 io_submit_sqe(ctx, &s, statep, &link);
Jens Axboe6c271ce2019-01-10 11:22:30 -07002633 submitted++;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002634 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07002635 }
2636
Jens Axboe9e645e112019-05-10 16:07:28 -06002637 if (link)
Jens Axboebc808bc2019-10-22 13:14:37 -06002638 io_queue_link_head(ctx, link, &link->submit, shadow_req);
Jens Axboe6c271ce2019-01-10 11:22:30 -07002639 if (statep)
2640 io_submit_state_end(&state);
2641
2642 return submitted;
2643}
2644
2645static int io_sq_thread(void *data)
2646{
Jens Axboe6c271ce2019-01-10 11:22:30 -07002647 struct io_ring_ctx *ctx = data;
2648 struct mm_struct *cur_mm = NULL;
2649 mm_segment_t old_fs;
2650 DEFINE_WAIT(wait);
2651 unsigned inflight;
2652 unsigned long timeout;
2653
Jackie Liua4c0b3d2019-07-08 13:41:12 +08002654 complete(&ctx->sqo_thread_started);
2655
Jens Axboe6c271ce2019-01-10 11:22:30 -07002656 old_fs = get_fs();
2657 set_fs(USER_DS);
2658
2659 timeout = inflight = 0;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02002660 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002661 bool mm_fault = false;
2662 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002663
2664 if (inflight) {
2665 unsigned nr_events = 0;
2666
2667 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe2b2ed972019-10-25 10:06:15 -06002668 /*
2669 * inflight is the count of the maximum possible
2670 * entries we submitted, but it can be smaller
2671 * if we dropped some of them. If we don't have
2672 * poll entries available, then we know that we
2673 * have nothing left to poll for. Reset the
2674 * inflight count to zero in that case.
2675 */
2676 mutex_lock(&ctx->uring_lock);
2677 if (!list_empty(&ctx->poll_list))
2678 __io_iopoll_check(ctx, &nr_events, 0);
2679 else
2680 inflight = 0;
2681 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07002682 } else {
2683 /*
2684 * Normal IO, just pretend everything completed.
2685 * We don't have to poll completions for that.
2686 */
2687 nr_events = inflight;
2688 }
2689
2690 inflight -= nr_events;
2691 if (!inflight)
2692 timeout = jiffies + ctx->sq_thread_idle;
2693 }
2694
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002695 to_submit = io_sqring_entries(ctx);
2696 if (!to_submit) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07002697 /*
2698 * We're polling. If we're within the defined idle
2699 * period, then let us spin without work before going
2700 * to sleep.
2701 */
2702 if (inflight || !time_after(jiffies, timeout)) {
Jens Axboe9831a902019-09-19 09:48:55 -06002703 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07002704 continue;
2705 }
2706
2707 /*
2708 * Drop cur_mm before scheduling, we can't hold it for
2709 * long periods (or over schedule()). Do this before
2710 * adding ourselves to the waitqueue, as the unuse/drop
2711 * may sleep.
2712 */
2713 if (cur_mm) {
2714 unuse_mm(cur_mm);
2715 mmput(cur_mm);
2716 cur_mm = NULL;
2717 }
2718
2719 prepare_to_wait(&ctx->sqo_wait, &wait,
2720 TASK_INTERRUPTIBLE);
2721
2722 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00002723 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02002724 /* make sure to read SQ tail after writing flags */
2725 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07002726
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002727 to_submit = io_sqring_entries(ctx);
2728 if (!to_submit) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02002729 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07002730 finish_wait(&ctx->sqo_wait, &wait);
2731 break;
2732 }
2733 if (signal_pending(current))
2734 flush_signals(current);
2735 schedule();
2736 finish_wait(&ctx->sqo_wait, &wait);
2737
Hristo Venev75b28af2019-08-26 17:23:46 +00002738 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002739 continue;
2740 }
2741 finish_wait(&ctx->sqo_wait, &wait);
2742
Hristo Venev75b28af2019-08-26 17:23:46 +00002743 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002744 }
2745
Jens Axboe6c271ce2019-01-10 11:22:30 -07002746 /* Unless all new commands are FIXED regions, grab mm */
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002747 if (!cur_mm) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07002748 mm_fault = !mmget_not_zero(ctx->sqo_mm);
2749 if (!mm_fault) {
2750 use_mm(ctx->sqo_mm);
2751 cur_mm = ctx->sqo_mm;
2752 }
2753 }
2754
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002755 to_submit = min(to_submit, ctx->sq_entries);
2756 inflight += io_submit_sqes(ctx, to_submit, cur_mm != NULL,
2757 mm_fault);
Jens Axboe6c271ce2019-01-10 11:22:30 -07002758
2759 /* Commit SQ ring head once we've consumed all SQEs */
2760 io_commit_sqring(ctx);
2761 }
2762
2763 set_fs(old_fs);
2764 if (cur_mm) {
2765 unuse_mm(cur_mm);
2766 mmput(cur_mm);
2767 }
Jens Axboe06058632019-04-13 09:26:03 -06002768
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02002769 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06002770
Jens Axboe6c271ce2019-01-10 11:22:30 -07002771 return 0;
2772}
2773
Jens Axboebc808bc2019-10-22 13:14:37 -06002774static int io_ring_submit(struct io_ring_ctx *ctx, unsigned int to_submit)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002775{
Jens Axboe9a56a232019-01-09 09:06:50 -07002776 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002777 struct io_kiocb *link = NULL;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002778 struct io_kiocb *shadow_req = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002779 bool prev_was_link = false;
Jens Axboe5c8b0b52019-04-30 10:16:07 -06002780 int i, submit = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002781
Jens Axboe9a56a232019-01-09 09:06:50 -07002782 if (to_submit > IO_PLUG_THRESHOLD) {
2783 io_submit_state_start(&state, ctx, to_submit);
2784 statep = &state;
2785 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002786
2787 for (i = 0; i < to_submit; i++) {
2788 struct sqe_submit s;
2789
2790 if (!io_get_sqring(ctx, &s))
2791 break;
2792
Jens Axboe9e645e112019-05-10 16:07:28 -06002793 /*
2794 * If previous wasn't linked and we have a linked command,
2795 * that's the end of the chain. Submit the previous link.
2796 */
2797 if (!prev_was_link && link) {
Jens Axboebc808bc2019-10-22 13:14:37 -06002798 io_queue_link_head(ctx, link, &link->submit, shadow_req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002799 link = NULL;
Jackie Liu5f5ad9c2019-09-18 10:37:53 +08002800 shadow_req = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002801 }
2802 prev_was_link = (s.sqe->flags & IOSQE_IO_LINK) != 0;
2803
Jackie Liu4fe2c962019-09-09 20:50:40 +08002804 if (link && (s.sqe->flags & IOSQE_IO_DRAIN)) {
2805 if (!shadow_req) {
2806 shadow_req = io_get_req(ctx, NULL);
Jackie Liua1041c22019-09-18 17:25:52 +08002807 if (unlikely(!shadow_req))
2808 goto out;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002809 shadow_req->flags |= (REQ_F_IO_DRAIN | REQ_F_SHADOW_DRAIN);
2810 refcount_dec(&shadow_req->refs);
2811 }
2812 shadow_req->sequence = s.sequence;
2813 }
2814
Jackie Liua1041c22019-09-18 17:25:52 +08002815out:
Jens Axboe2b188cc2019-01-07 10:46:33 -07002816 s.has_user = true;
Jens Axboedef596e2019-01-09 08:59:42 -07002817 s.needs_lock = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002818 s.needs_fixed_file = false;
Jens Axboe5c8b0b52019-04-30 10:16:07 -06002819 submit++;
Jens Axboebc808bc2019-10-22 13:14:37 -06002820 io_submit_sqe(ctx, &s, statep, &link);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002821 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002822
Jens Axboe9e645e112019-05-10 16:07:28 -06002823 if (link)
Jens Axboebc808bc2019-10-22 13:14:37 -06002824 io_queue_link_head(ctx, link, &link->submit, shadow_req);
Jens Axboe9a56a232019-01-09 09:06:50 -07002825 if (statep)
2826 io_submit_state_end(statep);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002827
Pavel Begunkov935d1e42019-10-25 12:31:31 +03002828 io_commit_sqring(ctx);
2829
Jens Axboe5c8b0b52019-04-30 10:16:07 -06002830 return submit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002831}
2832
Jens Axboebda52162019-09-24 13:47:15 -06002833struct io_wait_queue {
2834 struct wait_queue_entry wq;
2835 struct io_ring_ctx *ctx;
2836 unsigned to_wait;
2837 unsigned nr_timeouts;
2838};
2839
2840static inline bool io_should_wake(struct io_wait_queue *iowq)
2841{
2842 struct io_ring_ctx *ctx = iowq->ctx;
2843
2844 /*
2845 * Wake up if we have enough events, or if a timeout occured since we
2846 * started waiting. For timeouts, we always want to return to userspace,
2847 * regardless of event count.
2848 */
2849 return io_cqring_events(ctx->rings) >= iowq->to_wait ||
2850 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
2851}
2852
2853static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
2854 int wake_flags, void *key)
2855{
2856 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
2857 wq);
2858
2859 if (!io_should_wake(iowq))
2860 return -1;
2861
2862 return autoremove_wake_function(curr, mode, wake_flags, key);
2863}
2864
Jens Axboe2b188cc2019-01-07 10:46:33 -07002865/*
2866 * Wait until events become available, if we don't already have some. The
2867 * application must reap them itself, as they reside on the shared cq ring.
2868 */
2869static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
2870 const sigset_t __user *sig, size_t sigsz)
2871{
Jens Axboebda52162019-09-24 13:47:15 -06002872 struct io_wait_queue iowq = {
2873 .wq = {
2874 .private = current,
2875 .func = io_wake_function,
2876 .entry = LIST_HEAD_INIT(iowq.wq.entry),
2877 },
2878 .ctx = ctx,
2879 .to_wait = min_events,
2880 };
Hristo Venev75b28af2019-08-26 17:23:46 +00002881 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002882 int ret;
2883
Hristo Venev75b28af2019-08-26 17:23:46 +00002884 if (io_cqring_events(rings) >= min_events)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002885 return 0;
2886
2887 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01002888#ifdef CONFIG_COMPAT
2889 if (in_compat_syscall())
2890 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07002891 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01002892 else
2893#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07002894 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01002895
Jens Axboe2b188cc2019-01-07 10:46:33 -07002896 if (ret)
2897 return ret;
2898 }
2899
Jens Axboebda52162019-09-24 13:47:15 -06002900 ret = 0;
2901 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
2902 do {
2903 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
2904 TASK_INTERRUPTIBLE);
2905 if (io_should_wake(&iowq))
2906 break;
2907 schedule();
2908 if (signal_pending(current)) {
2909 ret = -ERESTARTSYS;
2910 break;
2911 }
2912 } while (1);
2913 finish_wait(&ctx->wait, &iowq.wq);
2914
Oleg Nesterovb7724342019-07-16 16:29:53 -07002915 restore_saved_sigmask_unless(ret == -ERESTARTSYS);
Oleg Nesterov97abc882019-06-28 12:06:50 -07002916 if (ret == -ERESTARTSYS)
2917 ret = -EINTR;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002918
Hristo Venev75b28af2019-08-26 17:23:46 +00002919 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002920}
2921
Jens Axboe6b063142019-01-10 22:13:58 -07002922static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
2923{
2924#if defined(CONFIG_UNIX)
2925 if (ctx->ring_sock) {
2926 struct sock *sock = ctx->ring_sock->sk;
2927 struct sk_buff *skb;
2928
2929 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
2930 kfree_skb(skb);
2931 }
2932#else
2933 int i;
2934
2935 for (i = 0; i < ctx->nr_user_files; i++)
2936 fput(ctx->user_files[i]);
2937#endif
2938}
2939
2940static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
2941{
2942 if (!ctx->user_files)
2943 return -ENXIO;
2944
2945 __io_sqe_files_unregister(ctx);
2946 kfree(ctx->user_files);
2947 ctx->user_files = NULL;
2948 ctx->nr_user_files = 0;
2949 return 0;
2950}
2951
Jens Axboe6c271ce2019-01-10 11:22:30 -07002952static void io_sq_thread_stop(struct io_ring_ctx *ctx)
2953{
2954 if (ctx->sqo_thread) {
Jackie Liua4c0b3d2019-07-08 13:41:12 +08002955 wait_for_completion(&ctx->sqo_thread_started);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02002956 /*
2957 * The park is a bit of a work-around, without it we get
2958 * warning spews on shutdown with SQPOLL set and affinity
2959 * set to a single CPU.
2960 */
Jens Axboe06058632019-04-13 09:26:03 -06002961 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07002962 kthread_stop(ctx->sqo_thread);
2963 ctx->sqo_thread = NULL;
2964 }
2965}
2966
Jens Axboe6b063142019-01-10 22:13:58 -07002967static void io_finish_async(struct io_ring_ctx *ctx)
2968{
Jens Axboe54a91f32019-09-10 09:15:04 -06002969 int i;
2970
Jens Axboe6c271ce2019-01-10 11:22:30 -07002971 io_sq_thread_stop(ctx);
2972
Jens Axboe54a91f32019-09-10 09:15:04 -06002973 for (i = 0; i < ARRAY_SIZE(ctx->sqo_wq); i++) {
2974 if (ctx->sqo_wq[i]) {
2975 destroy_workqueue(ctx->sqo_wq[i]);
2976 ctx->sqo_wq[i] = NULL;
2977 }
Jens Axboe6b063142019-01-10 22:13:58 -07002978 }
2979}
2980
2981#if defined(CONFIG_UNIX)
2982static void io_destruct_skb(struct sk_buff *skb)
2983{
2984 struct io_ring_ctx *ctx = skb->sk->sk_user_data;
Jens Axboe8a997342019-10-09 14:40:13 -06002985 int i;
Jens Axboe6b063142019-01-10 22:13:58 -07002986
Jens Axboe8a997342019-10-09 14:40:13 -06002987 for (i = 0; i < ARRAY_SIZE(ctx->sqo_wq); i++)
2988 if (ctx->sqo_wq[i])
2989 flush_workqueue(ctx->sqo_wq[i]);
2990
Jens Axboe6b063142019-01-10 22:13:58 -07002991 unix_destruct_scm(skb);
2992}
2993
2994/*
2995 * Ensure the UNIX gc is aware of our file set, so we are certain that
2996 * the io_uring can be safely unregistered on process exit, even if we have
2997 * loops in the file referencing.
2998 */
2999static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
3000{
3001 struct sock *sk = ctx->ring_sock->sk;
3002 struct scm_fp_list *fpl;
3003 struct sk_buff *skb;
3004 int i;
3005
3006 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
3007 unsigned long inflight = ctx->user->unix_inflight + nr;
3008
3009 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
3010 return -EMFILE;
3011 }
3012
3013 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
3014 if (!fpl)
3015 return -ENOMEM;
3016
3017 skb = alloc_skb(0, GFP_KERNEL);
3018 if (!skb) {
3019 kfree(fpl);
3020 return -ENOMEM;
3021 }
3022
3023 skb->sk = sk;
3024 skb->destructor = io_destruct_skb;
3025
3026 fpl->user = get_uid(ctx->user);
3027 for (i = 0; i < nr; i++) {
3028 fpl->fp[i] = get_file(ctx->user_files[i + offset]);
3029 unix_inflight(fpl->user, fpl->fp[i]);
3030 }
3031
3032 fpl->max = fpl->count = nr;
3033 UNIXCB(skb).fp = fpl;
3034 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
3035 skb_queue_head(&sk->sk_receive_queue, skb);
3036
3037 for (i = 0; i < nr; i++)
3038 fput(fpl->fp[i]);
3039
3040 return 0;
3041}
3042
3043/*
3044 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
3045 * causes regular reference counting to break down. We rely on the UNIX
3046 * garbage collection to take care of this problem for us.
3047 */
3048static int io_sqe_files_scm(struct io_ring_ctx *ctx)
3049{
3050 unsigned left, total;
3051 int ret = 0;
3052
3053 total = 0;
3054 left = ctx->nr_user_files;
3055 while (left) {
3056 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07003057
3058 ret = __io_sqe_files_scm(ctx, this_files, total);
3059 if (ret)
3060 break;
3061 left -= this_files;
3062 total += this_files;
3063 }
3064
3065 if (!ret)
3066 return 0;
3067
3068 while (total < ctx->nr_user_files) {
3069 fput(ctx->user_files[total]);
3070 total++;
3071 }
3072
3073 return ret;
3074}
3075#else
3076static int io_sqe_files_scm(struct io_ring_ctx *ctx)
3077{
3078 return 0;
3079}
3080#endif
3081
3082static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
3083 unsigned nr_args)
3084{
3085 __s32 __user *fds = (__s32 __user *) arg;
3086 int fd, ret = 0;
3087 unsigned i;
3088
3089 if (ctx->user_files)
3090 return -EBUSY;
3091 if (!nr_args)
3092 return -EINVAL;
3093 if (nr_args > IORING_MAX_FIXED_FILES)
3094 return -EMFILE;
3095
3096 ctx->user_files = kcalloc(nr_args, sizeof(struct file *), GFP_KERNEL);
3097 if (!ctx->user_files)
3098 return -ENOMEM;
3099
3100 for (i = 0; i < nr_args; i++) {
3101 ret = -EFAULT;
3102 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
3103 break;
3104
3105 ctx->user_files[i] = fget(fd);
3106
3107 ret = -EBADF;
3108 if (!ctx->user_files[i])
3109 break;
3110 /*
3111 * Don't allow io_uring instances to be registered. If UNIX
3112 * isn't enabled, then this causes a reference cycle and this
3113 * instance can never get freed. If UNIX is enabled we'll
3114 * handle it just fine, but there's still no point in allowing
3115 * a ring fd as it doesn't support regular read/write anyway.
3116 */
3117 if (ctx->user_files[i]->f_op == &io_uring_fops) {
3118 fput(ctx->user_files[i]);
3119 break;
3120 }
3121 ctx->nr_user_files++;
3122 ret = 0;
3123 }
3124
3125 if (ret) {
3126 for (i = 0; i < ctx->nr_user_files; i++)
3127 fput(ctx->user_files[i]);
3128
3129 kfree(ctx->user_files);
Jens Axboe25adf502019-04-03 09:52:40 -06003130 ctx->user_files = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003131 ctx->nr_user_files = 0;
3132 return ret;
3133 }
3134
3135 ret = io_sqe_files_scm(ctx);
3136 if (ret)
3137 io_sqe_files_unregister(ctx);
3138
3139 return ret;
3140}
3141
Jens Axboe6c271ce2019-01-10 11:22:30 -07003142static int io_sq_offload_start(struct io_ring_ctx *ctx,
3143 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003144{
3145 int ret;
3146
Jens Axboe6c271ce2019-01-10 11:22:30 -07003147 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003148 mmgrab(current->mm);
3149 ctx->sqo_mm = current->mm;
3150
Jens Axboe6c271ce2019-01-10 11:22:30 -07003151 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06003152 ret = -EPERM;
3153 if (!capable(CAP_SYS_ADMIN))
3154 goto err;
3155
Jens Axboe917257d2019-04-13 09:28:55 -06003156 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
3157 if (!ctx->sq_thread_idle)
3158 ctx->sq_thread_idle = HZ;
3159
Jens Axboe6c271ce2019-01-10 11:22:30 -07003160 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06003161 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003162
Jens Axboe917257d2019-04-13 09:28:55 -06003163 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06003164 if (cpu >= nr_cpu_ids)
3165 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08003166 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06003167 goto err;
3168
Jens Axboe6c271ce2019-01-10 11:22:30 -07003169 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
3170 ctx, cpu,
3171 "io_uring-sq");
3172 } else {
3173 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
3174 "io_uring-sq");
3175 }
3176 if (IS_ERR(ctx->sqo_thread)) {
3177 ret = PTR_ERR(ctx->sqo_thread);
3178 ctx->sqo_thread = NULL;
3179 goto err;
3180 }
3181 wake_up_process(ctx->sqo_thread);
3182 } else if (p->flags & IORING_SETUP_SQ_AFF) {
3183 /* Can't have SQ_AFF without SQPOLL */
3184 ret = -EINVAL;
3185 goto err;
3186 }
3187
Jens Axboe2b188cc2019-01-07 10:46:33 -07003188 /* Do QD, or 2 * CPUS, whatever is smallest */
Jens Axboe54a91f32019-09-10 09:15:04 -06003189 ctx->sqo_wq[0] = alloc_workqueue("io_ring-wq",
3190 WQ_UNBOUND | WQ_FREEZABLE,
Jens Axboe2b188cc2019-01-07 10:46:33 -07003191 min(ctx->sq_entries - 1, 2 * num_online_cpus()));
Jens Axboe54a91f32019-09-10 09:15:04 -06003192 if (!ctx->sqo_wq[0]) {
3193 ret = -ENOMEM;
3194 goto err;
3195 }
3196
3197 /*
3198 * This is for buffered writes, where we want to limit the parallelism
3199 * due to file locking in file systems. As "normal" buffered writes
3200 * should parellelize on writeout quite nicely, limit us to having 2
3201 * pending. This avoids massive contention on the inode when doing
3202 * buffered async writes.
3203 */
3204 ctx->sqo_wq[1] = alloc_workqueue("io_ring-write-wq",
3205 WQ_UNBOUND | WQ_FREEZABLE, 2);
3206 if (!ctx->sqo_wq[1]) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07003207 ret = -ENOMEM;
3208 goto err;
3209 }
3210
3211 return 0;
3212err:
Jens Axboe54a91f32019-09-10 09:15:04 -06003213 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003214 mmdrop(ctx->sqo_mm);
3215 ctx->sqo_mm = NULL;
3216 return ret;
3217}
3218
3219static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
3220{
3221 atomic_long_sub(nr_pages, &user->locked_vm);
3222}
3223
3224static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
3225{
3226 unsigned long page_limit, cur_pages, new_pages;
3227
3228 /* Don't allow more pages than we can safely lock */
3229 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
3230
3231 do {
3232 cur_pages = atomic_long_read(&user->locked_vm);
3233 new_pages = cur_pages + nr_pages;
3234 if (new_pages > page_limit)
3235 return -ENOMEM;
3236 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
3237 new_pages) != cur_pages);
3238
3239 return 0;
3240}
3241
3242static void io_mem_free(void *ptr)
3243{
Mark Rutland52e04ef2019-04-30 17:30:21 +01003244 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003245
Mark Rutland52e04ef2019-04-30 17:30:21 +01003246 if (!ptr)
3247 return;
3248
3249 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003250 if (put_page_testzero(page))
3251 free_compound_page(page);
3252}
3253
3254static void *io_mem_alloc(size_t size)
3255{
3256 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
3257 __GFP_NORETRY;
3258
3259 return (void *) __get_free_pages(gfp_flags, get_order(size));
3260}
3261
Hristo Venev75b28af2019-08-26 17:23:46 +00003262static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
3263 size_t *sq_offset)
3264{
3265 struct io_rings *rings;
3266 size_t off, sq_array_size;
3267
3268 off = struct_size(rings, cqes, cq_entries);
3269 if (off == SIZE_MAX)
3270 return SIZE_MAX;
3271
3272#ifdef CONFIG_SMP
3273 off = ALIGN(off, SMP_CACHE_BYTES);
3274 if (off == 0)
3275 return SIZE_MAX;
3276#endif
3277
3278 sq_array_size = array_size(sizeof(u32), sq_entries);
3279 if (sq_array_size == SIZE_MAX)
3280 return SIZE_MAX;
3281
3282 if (check_add_overflow(off, sq_array_size, &off))
3283 return SIZE_MAX;
3284
3285 if (sq_offset)
3286 *sq_offset = off;
3287
3288 return off;
3289}
3290
Jens Axboe2b188cc2019-01-07 10:46:33 -07003291static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
3292{
Hristo Venev75b28af2019-08-26 17:23:46 +00003293 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003294
Hristo Venev75b28af2019-08-26 17:23:46 +00003295 pages = (size_t)1 << get_order(
3296 rings_size(sq_entries, cq_entries, NULL));
3297 pages += (size_t)1 << get_order(
3298 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07003299
Hristo Venev75b28af2019-08-26 17:23:46 +00003300 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003301}
3302
Jens Axboeedafcce2019-01-09 09:16:05 -07003303static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
3304{
3305 int i, j;
3306
3307 if (!ctx->user_bufs)
3308 return -ENXIO;
3309
3310 for (i = 0; i < ctx->nr_user_bufs; i++) {
3311 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
3312
3313 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbard27c4d3a2019-08-04 19:32:06 -07003314 put_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07003315
3316 if (ctx->account_mem)
3317 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003318 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07003319 imu->nr_bvecs = 0;
3320 }
3321
3322 kfree(ctx->user_bufs);
3323 ctx->user_bufs = NULL;
3324 ctx->nr_user_bufs = 0;
3325 return 0;
3326}
3327
3328static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
3329 void __user *arg, unsigned index)
3330{
3331 struct iovec __user *src;
3332
3333#ifdef CONFIG_COMPAT
3334 if (ctx->compat) {
3335 struct compat_iovec __user *ciovs;
3336 struct compat_iovec ciov;
3337
3338 ciovs = (struct compat_iovec __user *) arg;
3339 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
3340 return -EFAULT;
3341
3342 dst->iov_base = (void __user *) (unsigned long) ciov.iov_base;
3343 dst->iov_len = ciov.iov_len;
3344 return 0;
3345 }
3346#endif
3347 src = (struct iovec __user *) arg;
3348 if (copy_from_user(dst, &src[index], sizeof(*dst)))
3349 return -EFAULT;
3350 return 0;
3351}
3352
3353static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
3354 unsigned nr_args)
3355{
3356 struct vm_area_struct **vmas = NULL;
3357 struct page **pages = NULL;
3358 int i, j, got_pages = 0;
3359 int ret = -EINVAL;
3360
3361 if (ctx->user_bufs)
3362 return -EBUSY;
3363 if (!nr_args || nr_args > UIO_MAXIOV)
3364 return -EINVAL;
3365
3366 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
3367 GFP_KERNEL);
3368 if (!ctx->user_bufs)
3369 return -ENOMEM;
3370
3371 for (i = 0; i < nr_args; i++) {
3372 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
3373 unsigned long off, start, end, ubuf;
3374 int pret, nr_pages;
3375 struct iovec iov;
3376 size_t size;
3377
3378 ret = io_copy_iov(ctx, &iov, arg, i);
3379 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03003380 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07003381
3382 /*
3383 * Don't impose further limits on the size and buffer
3384 * constraints here, we'll -EINVAL later when IO is
3385 * submitted if they are wrong.
3386 */
3387 ret = -EFAULT;
3388 if (!iov.iov_base || !iov.iov_len)
3389 goto err;
3390
3391 /* arbitrary limit, but we need something */
3392 if (iov.iov_len > SZ_1G)
3393 goto err;
3394
3395 ubuf = (unsigned long) iov.iov_base;
3396 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
3397 start = ubuf >> PAGE_SHIFT;
3398 nr_pages = end - start;
3399
3400 if (ctx->account_mem) {
3401 ret = io_account_mem(ctx->user, nr_pages);
3402 if (ret)
3403 goto err;
3404 }
3405
3406 ret = 0;
3407 if (!pages || nr_pages > got_pages) {
3408 kfree(vmas);
3409 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003410 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07003411 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003412 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07003413 sizeof(struct vm_area_struct *),
3414 GFP_KERNEL);
3415 if (!pages || !vmas) {
3416 ret = -ENOMEM;
3417 if (ctx->account_mem)
3418 io_unaccount_mem(ctx->user, nr_pages);
3419 goto err;
3420 }
3421 got_pages = nr_pages;
3422 }
3423
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003424 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07003425 GFP_KERNEL);
3426 ret = -ENOMEM;
3427 if (!imu->bvec) {
3428 if (ctx->account_mem)
3429 io_unaccount_mem(ctx->user, nr_pages);
3430 goto err;
3431 }
3432
3433 ret = 0;
3434 down_read(&current->mm->mmap_sem);
Ira Weiny932f4a62019-05-13 17:17:03 -07003435 pret = get_user_pages(ubuf, nr_pages,
3436 FOLL_WRITE | FOLL_LONGTERM,
3437 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07003438 if (pret == nr_pages) {
3439 /* don't support file backed memory */
3440 for (j = 0; j < nr_pages; j++) {
3441 struct vm_area_struct *vma = vmas[j];
3442
3443 if (vma->vm_file &&
3444 !is_file_hugepages(vma->vm_file)) {
3445 ret = -EOPNOTSUPP;
3446 break;
3447 }
3448 }
3449 } else {
3450 ret = pret < 0 ? pret : -EFAULT;
3451 }
3452 up_read(&current->mm->mmap_sem);
3453 if (ret) {
3454 /*
3455 * if we did partial map, or found file backed vmas,
3456 * release any pages we did get
3457 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07003458 if (pret > 0)
3459 put_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07003460 if (ctx->account_mem)
3461 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003462 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07003463 goto err;
3464 }
3465
3466 off = ubuf & ~PAGE_MASK;
3467 size = iov.iov_len;
3468 for (j = 0; j < nr_pages; j++) {
3469 size_t vec_len;
3470
3471 vec_len = min_t(size_t, size, PAGE_SIZE - off);
3472 imu->bvec[j].bv_page = pages[j];
3473 imu->bvec[j].bv_len = vec_len;
3474 imu->bvec[j].bv_offset = off;
3475 off = 0;
3476 size -= vec_len;
3477 }
3478 /* store original address for later verification */
3479 imu->ubuf = ubuf;
3480 imu->len = iov.iov_len;
3481 imu->nr_bvecs = nr_pages;
3482
3483 ctx->nr_user_bufs++;
3484 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003485 kvfree(pages);
3486 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07003487 return 0;
3488err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003489 kvfree(pages);
3490 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07003491 io_sqe_buffer_unregister(ctx);
3492 return ret;
3493}
3494
Jens Axboe9b402842019-04-11 11:45:41 -06003495static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
3496{
3497 __s32 __user *fds = arg;
3498 int fd;
3499
3500 if (ctx->cq_ev_fd)
3501 return -EBUSY;
3502
3503 if (copy_from_user(&fd, fds, sizeof(*fds)))
3504 return -EFAULT;
3505
3506 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
3507 if (IS_ERR(ctx->cq_ev_fd)) {
3508 int ret = PTR_ERR(ctx->cq_ev_fd);
3509 ctx->cq_ev_fd = NULL;
3510 return ret;
3511 }
3512
3513 return 0;
3514}
3515
3516static int io_eventfd_unregister(struct io_ring_ctx *ctx)
3517{
3518 if (ctx->cq_ev_fd) {
3519 eventfd_ctx_put(ctx->cq_ev_fd);
3520 ctx->cq_ev_fd = NULL;
3521 return 0;
3522 }
3523
3524 return -ENXIO;
3525}
3526
Jens Axboe2b188cc2019-01-07 10:46:33 -07003527static void io_ring_ctx_free(struct io_ring_ctx *ctx)
3528{
Jens Axboe6b063142019-01-10 22:13:58 -07003529 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003530 if (ctx->sqo_mm)
3531 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07003532
3533 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07003534 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07003535 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06003536 io_eventfd_unregister(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07003537
Jens Axboe2b188cc2019-01-07 10:46:33 -07003538#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07003539 if (ctx->ring_sock) {
3540 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07003541 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07003542 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003543#endif
3544
Hristo Venev75b28af2019-08-26 17:23:46 +00003545 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003546 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003547
3548 percpu_ref_exit(&ctx->refs);
3549 if (ctx->account_mem)
3550 io_unaccount_mem(ctx->user,
3551 ring_pages(ctx->sq_entries, ctx->cq_entries));
3552 free_uid(ctx->user);
3553 kfree(ctx);
3554}
3555
3556static __poll_t io_uring_poll(struct file *file, poll_table *wait)
3557{
3558 struct io_ring_ctx *ctx = file->private_data;
3559 __poll_t mask = 0;
3560
3561 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02003562 /*
3563 * synchronizes with barrier from wq_has_sleeper call in
3564 * io_commit_cqring
3565 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07003566 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00003567 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
3568 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003569 mask |= EPOLLOUT | EPOLLWRNORM;
yangerkundaa5de52019-09-24 20:53:34 +08003570 if (READ_ONCE(ctx->rings->cq.head) != ctx->cached_cq_tail)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003571 mask |= EPOLLIN | EPOLLRDNORM;
3572
3573 return mask;
3574}
3575
3576static int io_uring_fasync(int fd, struct file *file, int on)
3577{
3578 struct io_ring_ctx *ctx = file->private_data;
3579
3580 return fasync_helper(fd, file, on, &ctx->cq_fasync);
3581}
3582
3583static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
3584{
3585 mutex_lock(&ctx->uring_lock);
3586 percpu_ref_kill(&ctx->refs);
3587 mutex_unlock(&ctx->uring_lock);
3588
Jens Axboe5262f562019-09-17 12:26:57 -06003589 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003590 io_poll_remove_all(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07003591 io_iopoll_reap_events(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003592 wait_for_completion(&ctx->ctx_done);
3593 io_ring_ctx_free(ctx);
3594}
3595
3596static int io_uring_release(struct inode *inode, struct file *file)
3597{
3598 struct io_ring_ctx *ctx = file->private_data;
3599
3600 file->private_data = NULL;
3601 io_ring_ctx_wait_and_kill(ctx);
3602 return 0;
3603}
3604
3605static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
3606{
3607 loff_t offset = (loff_t) vma->vm_pgoff << PAGE_SHIFT;
3608 unsigned long sz = vma->vm_end - vma->vm_start;
3609 struct io_ring_ctx *ctx = file->private_data;
3610 unsigned long pfn;
3611 struct page *page;
3612 void *ptr;
3613
3614 switch (offset) {
3615 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00003616 case IORING_OFF_CQ_RING:
3617 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003618 break;
3619 case IORING_OFF_SQES:
3620 ptr = ctx->sq_sqes;
3621 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003622 default:
3623 return -EINVAL;
3624 }
3625
3626 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07003627 if (sz > page_size(page))
Jens Axboe2b188cc2019-01-07 10:46:33 -07003628 return -EINVAL;
3629
3630 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
3631 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
3632}
3633
3634SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
3635 u32, min_complete, u32, flags, const sigset_t __user *, sig,
3636 size_t, sigsz)
3637{
3638 struct io_ring_ctx *ctx;
3639 long ret = -EBADF;
3640 int submitted = 0;
3641 struct fd f;
3642
Jens Axboe6c271ce2019-01-10 11:22:30 -07003643 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07003644 return -EINVAL;
3645
3646 f = fdget(fd);
3647 if (!f.file)
3648 return -EBADF;
3649
3650 ret = -EOPNOTSUPP;
3651 if (f.file->f_op != &io_uring_fops)
3652 goto out_fput;
3653
3654 ret = -ENXIO;
3655 ctx = f.file->private_data;
3656 if (!percpu_ref_tryget(&ctx->refs))
3657 goto out_fput;
3658
Jens Axboe6c271ce2019-01-10 11:22:30 -07003659 /*
3660 * For SQ polling, the thread will do all submissions and completions.
3661 * Just return the requested submit count, and wake the thread if
3662 * we were asked to.
3663 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06003664 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003665 if (ctx->flags & IORING_SETUP_SQPOLL) {
3666 if (flags & IORING_ENTER_SQ_WAKEUP)
3667 wake_up(&ctx->sqo_wait);
3668 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06003669 } else if (to_submit) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07003670 to_submit = min(to_submit, ctx->sq_entries);
3671
3672 mutex_lock(&ctx->uring_lock);
Jens Axboebc808bc2019-10-22 13:14:37 -06003673 submitted = io_ring_submit(ctx, to_submit);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003674 mutex_unlock(&ctx->uring_lock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003675 }
3676 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07003677 unsigned nr_events = 0;
3678
Jens Axboe2b188cc2019-01-07 10:46:33 -07003679 min_complete = min(min_complete, ctx->cq_entries);
3680
Jens Axboedef596e2019-01-09 08:59:42 -07003681 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07003682 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07003683 } else {
3684 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
3685 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003686 }
3687
Pavel Begunkov6805b322019-10-08 02:18:42 +03003688 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003689out_fput:
3690 fdput(f);
3691 return submitted ? submitted : ret;
3692}
3693
3694static const struct file_operations io_uring_fops = {
3695 .release = io_uring_release,
3696 .mmap = io_uring_mmap,
3697 .poll = io_uring_poll,
3698 .fasync = io_uring_fasync,
3699};
3700
3701static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
3702 struct io_uring_params *p)
3703{
Hristo Venev75b28af2019-08-26 17:23:46 +00003704 struct io_rings *rings;
3705 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003706
Hristo Venev75b28af2019-08-26 17:23:46 +00003707 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
3708 if (size == SIZE_MAX)
3709 return -EOVERFLOW;
3710
3711 rings = io_mem_alloc(size);
3712 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003713 return -ENOMEM;
3714
Hristo Venev75b28af2019-08-26 17:23:46 +00003715 ctx->rings = rings;
3716 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
3717 rings->sq_ring_mask = p->sq_entries - 1;
3718 rings->cq_ring_mask = p->cq_entries - 1;
3719 rings->sq_ring_entries = p->sq_entries;
3720 rings->cq_ring_entries = p->cq_entries;
3721 ctx->sq_mask = rings->sq_ring_mask;
3722 ctx->cq_mask = rings->cq_ring_mask;
3723 ctx->sq_entries = rings->sq_ring_entries;
3724 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003725
3726 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
3727 if (size == SIZE_MAX)
3728 return -EOVERFLOW;
3729
3730 ctx->sq_sqes = io_mem_alloc(size);
Mark Rutland52e04ef2019-04-30 17:30:21 +01003731 if (!ctx->sq_sqes)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003732 return -ENOMEM;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003733
Jens Axboe2b188cc2019-01-07 10:46:33 -07003734 return 0;
3735}
3736
3737/*
3738 * Allocate an anonymous fd, this is what constitutes the application
3739 * visible backing of an io_uring instance. The application mmaps this
3740 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
3741 * we have to tie this fd to a socket for file garbage collection purposes.
3742 */
3743static int io_uring_get_fd(struct io_ring_ctx *ctx)
3744{
3745 struct file *file;
3746 int ret;
3747
3748#if defined(CONFIG_UNIX)
3749 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
3750 &ctx->ring_sock);
3751 if (ret)
3752 return ret;
3753#endif
3754
3755 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
3756 if (ret < 0)
3757 goto err;
3758
3759 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
3760 O_RDWR | O_CLOEXEC);
3761 if (IS_ERR(file)) {
3762 put_unused_fd(ret);
3763 ret = PTR_ERR(file);
3764 goto err;
3765 }
3766
3767#if defined(CONFIG_UNIX)
3768 ctx->ring_sock->file = file;
Jens Axboe6b063142019-01-10 22:13:58 -07003769 ctx->ring_sock->sk->sk_user_data = ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003770#endif
3771 fd_install(ret, file);
3772 return ret;
3773err:
3774#if defined(CONFIG_UNIX)
3775 sock_release(ctx->ring_sock);
3776 ctx->ring_sock = NULL;
3777#endif
3778 return ret;
3779}
3780
3781static int io_uring_create(unsigned entries, struct io_uring_params *p)
3782{
3783 struct user_struct *user = NULL;
3784 struct io_ring_ctx *ctx;
3785 bool account_mem;
3786 int ret;
3787
3788 if (!entries || entries > IORING_MAX_ENTRIES)
3789 return -EINVAL;
3790
3791 /*
3792 * Use twice as many entries for the CQ ring. It's possible for the
3793 * application to drive a higher depth than the size of the SQ ring,
3794 * since the sqes are only used at submission time. This allows for
3795 * some flexibility in overcommitting a bit.
3796 */
3797 p->sq_entries = roundup_pow_of_two(entries);
3798 p->cq_entries = 2 * p->sq_entries;
3799
3800 user = get_uid(current_user());
3801 account_mem = !capable(CAP_IPC_LOCK);
3802
3803 if (account_mem) {
3804 ret = io_account_mem(user,
3805 ring_pages(p->sq_entries, p->cq_entries));
3806 if (ret) {
3807 free_uid(user);
3808 return ret;
3809 }
3810 }
3811
3812 ctx = io_ring_ctx_alloc(p);
3813 if (!ctx) {
3814 if (account_mem)
3815 io_unaccount_mem(user, ring_pages(p->sq_entries,
3816 p->cq_entries));
3817 free_uid(user);
3818 return -ENOMEM;
3819 }
3820 ctx->compat = in_compat_syscall();
3821 ctx->account_mem = account_mem;
3822 ctx->user = user;
3823
3824 ret = io_allocate_scq_urings(ctx, p);
3825 if (ret)
3826 goto err;
3827
Jens Axboe6c271ce2019-01-10 11:22:30 -07003828 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003829 if (ret)
3830 goto err;
3831
Jens Axboe2b188cc2019-01-07 10:46:33 -07003832 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00003833 p->sq_off.head = offsetof(struct io_rings, sq.head);
3834 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
3835 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
3836 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
3837 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
3838 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
3839 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003840
3841 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00003842 p->cq_off.head = offsetof(struct io_rings, cq.head);
3843 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
3844 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
3845 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
3846 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
3847 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06003848
Jens Axboe044c1ab2019-10-28 09:15:33 -06003849 /*
3850 * Install ring fd as the very last thing, so we don't risk someone
3851 * having closed it before we finish setup
3852 */
3853 ret = io_uring_get_fd(ctx);
3854 if (ret < 0)
3855 goto err;
3856
Jens Axboeac90f242019-09-06 10:26:21 -06003857 p->features = IORING_FEAT_SINGLE_MMAP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003858 return ret;
3859err:
3860 io_ring_ctx_wait_and_kill(ctx);
3861 return ret;
3862}
3863
3864/*
3865 * Sets up an aio uring context, and returns the fd. Applications asks for a
3866 * ring size, we return the actual sq/cq ring sizes (among other things) in the
3867 * params structure passed in.
3868 */
3869static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
3870{
3871 struct io_uring_params p;
3872 long ret;
3873 int i;
3874
3875 if (copy_from_user(&p, params, sizeof(p)))
3876 return -EFAULT;
3877 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
3878 if (p.resv[i])
3879 return -EINVAL;
3880 }
3881
Jens Axboe6c271ce2019-01-10 11:22:30 -07003882 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
3883 IORING_SETUP_SQ_AFF))
Jens Axboe2b188cc2019-01-07 10:46:33 -07003884 return -EINVAL;
3885
3886 ret = io_uring_create(entries, &p);
3887 if (ret < 0)
3888 return ret;
3889
3890 if (copy_to_user(params, &p, sizeof(p)))
3891 return -EFAULT;
3892
3893 return ret;
3894}
3895
3896SYSCALL_DEFINE2(io_uring_setup, u32, entries,
3897 struct io_uring_params __user *, params)
3898{
3899 return io_uring_setup(entries, params);
3900}
3901
Jens Axboeedafcce2019-01-09 09:16:05 -07003902static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
3903 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06003904 __releases(ctx->uring_lock)
3905 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07003906{
3907 int ret;
3908
Jens Axboe35fa71a2019-04-22 10:23:23 -06003909 /*
3910 * We're inside the ring mutex, if the ref is already dying, then
3911 * someone else killed the ctx or is already going through
3912 * io_uring_register().
3913 */
3914 if (percpu_ref_is_dying(&ctx->refs))
3915 return -ENXIO;
3916
Jens Axboeedafcce2019-01-09 09:16:05 -07003917 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06003918
3919 /*
3920 * Drop uring mutex before waiting for references to exit. If another
3921 * thread is currently inside io_uring_enter() it might need to grab
3922 * the uring_lock to make progress. If we hold it here across the drain
3923 * wait, then we can deadlock. It's safe to drop the mutex here, since
3924 * no new references will come in after we've killed the percpu ref.
3925 */
3926 mutex_unlock(&ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07003927 wait_for_completion(&ctx->ctx_done);
Jens Axboeb19062a2019-04-15 10:49:38 -06003928 mutex_lock(&ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07003929
3930 switch (opcode) {
3931 case IORING_REGISTER_BUFFERS:
3932 ret = io_sqe_buffer_register(ctx, arg, nr_args);
3933 break;
3934 case IORING_UNREGISTER_BUFFERS:
3935 ret = -EINVAL;
3936 if (arg || nr_args)
3937 break;
3938 ret = io_sqe_buffer_unregister(ctx);
3939 break;
Jens Axboe6b063142019-01-10 22:13:58 -07003940 case IORING_REGISTER_FILES:
3941 ret = io_sqe_files_register(ctx, arg, nr_args);
3942 break;
3943 case IORING_UNREGISTER_FILES:
3944 ret = -EINVAL;
3945 if (arg || nr_args)
3946 break;
3947 ret = io_sqe_files_unregister(ctx);
3948 break;
Jens Axboe9b402842019-04-11 11:45:41 -06003949 case IORING_REGISTER_EVENTFD:
3950 ret = -EINVAL;
3951 if (nr_args != 1)
3952 break;
3953 ret = io_eventfd_register(ctx, arg);
3954 break;
3955 case IORING_UNREGISTER_EVENTFD:
3956 ret = -EINVAL;
3957 if (arg || nr_args)
3958 break;
3959 ret = io_eventfd_unregister(ctx);
3960 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07003961 default:
3962 ret = -EINVAL;
3963 break;
3964 }
3965
3966 /* bring the ctx back to life */
3967 reinit_completion(&ctx->ctx_done);
3968 percpu_ref_reinit(&ctx->refs);
3969 return ret;
3970}
3971
3972SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
3973 void __user *, arg, unsigned int, nr_args)
3974{
3975 struct io_ring_ctx *ctx;
3976 long ret = -EBADF;
3977 struct fd f;
3978
3979 f = fdget(fd);
3980 if (!f.file)
3981 return -EBADF;
3982
3983 ret = -EOPNOTSUPP;
3984 if (f.file->f_op != &io_uring_fops)
3985 goto out_fput;
3986
3987 ctx = f.file->private_data;
3988
3989 mutex_lock(&ctx->uring_lock);
3990 ret = __io_uring_register(ctx, opcode, arg, nr_args);
3991 mutex_unlock(&ctx->uring_lock);
3992out_fput:
3993 fdput(f);
3994 return ret;
3995}
3996
Jens Axboe2b188cc2019-01-07 10:46:33 -07003997static int __init io_uring_init(void)
3998{
3999 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
4000 return 0;
4001};
4002__initcall(io_uring_init);