blob: 292c4c733cbe0ab95e6896f2772ed3e738b0956a [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
877static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
878 long min)
879{
Jens Axboe500f9fb2019-08-19 12:15:59 -0600880 int iters, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -0700881
Jens Axboe500f9fb2019-08-19 12:15:59 -0600882 /*
883 * We disallow the app entering submit/complete with polling, but we
884 * still need to lock the ring to prevent racing with polled issue
885 * that got punted to a workqueue.
886 */
887 mutex_lock(&ctx->uring_lock);
888
889 iters = 0;
Jens Axboedef596e2019-01-09 08:59:42 -0700890 do {
891 int tmin = 0;
892
Jens Axboe500f9fb2019-08-19 12:15:59 -0600893 /*
Jens Axboea3a0e432019-08-20 11:03:11 -0600894 * Don't enter poll loop if we already have events pending.
895 * If we do, we can potentially be spinning for commands that
896 * already triggered a CQE (eg in error).
897 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000898 if (io_cqring_events(ctx->rings))
Jens Axboea3a0e432019-08-20 11:03:11 -0600899 break;
900
901 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -0600902 * If a submit got punted to a workqueue, we can have the
903 * application entering polling for a command before it gets
904 * issued. That app will hold the uring_lock for the duration
905 * of the poll right here, so we need to take a breather every
906 * now and then to ensure that the issue has a chance to add
907 * the poll to the issued list. Otherwise we can spin here
908 * forever, while the workqueue is stuck trying to acquire the
909 * very same mutex.
910 */
911 if (!(++iters & 7)) {
912 mutex_unlock(&ctx->uring_lock);
913 mutex_lock(&ctx->uring_lock);
914 }
915
Jens Axboedef596e2019-01-09 08:59:42 -0700916 if (*nr_events < min)
917 tmin = min - *nr_events;
918
919 ret = io_iopoll_getevents(ctx, nr_events, tmin);
920 if (ret <= 0)
921 break;
922 ret = 0;
923 } while (min && !*nr_events && !need_resched());
924
Jens Axboe500f9fb2019-08-19 12:15:59 -0600925 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -0700926 return ret;
927}
928
Jens Axboe491381ce2019-10-17 09:20:46 -0600929static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700930{
Jens Axboe491381ce2019-10-17 09:20:46 -0600931 /*
932 * Tell lockdep we inherited freeze protection from submission
933 * thread.
934 */
935 if (req->flags & REQ_F_ISREG) {
936 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700937
Jens Axboe491381ce2019-10-17 09:20:46 -0600938 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700939 }
Jens Axboe491381ce2019-10-17 09:20:46 -0600940 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700941}
942
943static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
944{
945 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
946
Jens Axboe491381ce2019-10-17 09:20:46 -0600947 if (kiocb->ki_flags & IOCB_WRITE)
948 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700949
Jens Axboe9e645e112019-05-10 16:07:28 -0600950 if ((req->flags & REQ_F_LINK) && res != req->result)
951 req->flags |= REQ_F_FAIL_LINK;
Jens Axboec71ffb62019-05-13 20:58:29 -0600952 io_cqring_add_event(req->ctx, req->user_data, res);
Jens Axboee65ef562019-03-12 10:16:44 -0600953 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700954}
955
Jens Axboedef596e2019-01-09 08:59:42 -0700956static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
957{
958 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
959
Jens Axboe491381ce2019-10-17 09:20:46 -0600960 if (kiocb->ki_flags & IOCB_WRITE)
961 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -0700962
Jens Axboe9e645e112019-05-10 16:07:28 -0600963 if ((req->flags & REQ_F_LINK) && res != req->result)
964 req->flags |= REQ_F_FAIL_LINK;
965 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -0700966 if (res != -EAGAIN)
967 req->flags |= REQ_F_IOPOLL_COMPLETED;
968}
969
970/*
971 * After the iocb has been issued, it's safe to be found on the poll list.
972 * Adding the kiocb to the list AFTER submission ensures that we don't
973 * find it from a io_iopoll_getevents() thread before the issuer is done
974 * accessing the kiocb cookie.
975 */
976static void io_iopoll_req_issued(struct io_kiocb *req)
977{
978 struct io_ring_ctx *ctx = req->ctx;
979
980 /*
981 * Track whether we have multiple files in our lists. This will impact
982 * how we do polling eventually, not spinning if we're on potentially
983 * different devices.
984 */
985 if (list_empty(&ctx->poll_list)) {
986 ctx->poll_multi_file = false;
987 } else if (!ctx->poll_multi_file) {
988 struct io_kiocb *list_req;
989
990 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
991 list);
992 if (list_req->rw.ki_filp != req->rw.ki_filp)
993 ctx->poll_multi_file = true;
994 }
995
996 /*
997 * For fast devices, IO may have already completed. If it has, add
998 * it to the front so we find it first.
999 */
1000 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1001 list_add(&req->list, &ctx->poll_list);
1002 else
1003 list_add_tail(&req->list, &ctx->poll_list);
1004}
1005
Jens Axboe3d6770f2019-04-13 11:50:54 -06001006static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07001007{
Jens Axboe3d6770f2019-04-13 11:50:54 -06001008 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -07001009 int diff = state->has_refs - state->used_refs;
1010
1011 if (diff)
1012 fput_many(state->file, diff);
1013 state->file = NULL;
1014 }
1015}
1016
1017/*
1018 * Get as many references to a file as we have IOs left in this submission,
1019 * assuming most submissions are for one file, or at least that each file
1020 * has more than one submission.
1021 */
1022static struct file *io_file_get(struct io_submit_state *state, int fd)
1023{
1024 if (!state)
1025 return fget(fd);
1026
1027 if (state->file) {
1028 if (state->fd == fd) {
1029 state->used_refs++;
1030 state->ios_left--;
1031 return state->file;
1032 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06001033 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07001034 }
1035 state->file = fget_many(fd, state->ios_left);
1036 if (!state->file)
1037 return NULL;
1038
1039 state->fd = fd;
1040 state->has_refs = state->ios_left;
1041 state->used_refs = 1;
1042 state->ios_left--;
1043 return state->file;
1044}
1045
Jens Axboe2b188cc2019-01-07 10:46:33 -07001046/*
1047 * If we tracked the file through the SCM inflight mechanism, we could support
1048 * any file. For now, just ensure that anything potentially problematic is done
1049 * inline.
1050 */
1051static bool io_file_supports_async(struct file *file)
1052{
1053 umode_t mode = file_inode(file)->i_mode;
1054
1055 if (S_ISBLK(mode) || S_ISCHR(mode))
1056 return true;
1057 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
1058 return true;
1059
1060 return false;
1061}
1062
Jens Axboe6c271ce2019-01-10 11:22:30 -07001063static int io_prep_rw(struct io_kiocb *req, const struct sqe_submit *s,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001064 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001065{
Jens Axboe6c271ce2019-01-10 11:22:30 -07001066 const struct io_uring_sqe *sqe = s->sqe;
Jens Axboedef596e2019-01-09 08:59:42 -07001067 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001068 struct kiocb *kiocb = &req->rw;
Jens Axboe09bb8392019-03-13 12:39:28 -06001069 unsigned ioprio;
1070 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001071
Jens Axboe09bb8392019-03-13 12:39:28 -06001072 if (!req->file)
1073 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001074
Jens Axboe491381ce2019-10-17 09:20:46 -06001075 if (S_ISREG(file_inode(req->file)->i_mode))
1076 req->flags |= REQ_F_ISREG;
1077
1078 /*
1079 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
1080 * we know to async punt it even if it was opened O_NONBLOCK
1081 */
1082 if (force_nonblock && !io_file_supports_async(req->file)) {
1083 req->flags |= REQ_F_MUST_PUNT;
1084 return -EAGAIN;
1085 }
Jens Axboe6b063142019-01-10 22:13:58 -07001086
Jens Axboe2b188cc2019-01-07 10:46:33 -07001087 kiocb->ki_pos = READ_ONCE(sqe->off);
1088 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
1089 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
1090
1091 ioprio = READ_ONCE(sqe->ioprio);
1092 if (ioprio) {
1093 ret = ioprio_check_cap(ioprio);
1094 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06001095 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001096
1097 kiocb->ki_ioprio = ioprio;
1098 } else
1099 kiocb->ki_ioprio = get_current_ioprio();
1100
1101 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
1102 if (unlikely(ret))
Jens Axboe09bb8392019-03-13 12:39:28 -06001103 return ret;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001104
1105 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06001106 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
1107 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02001108 req->flags |= REQ_F_NOWAIT;
1109
1110 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001111 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001112
Jens Axboedef596e2019-01-09 08:59:42 -07001113 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07001114 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
1115 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06001116 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001117
Jens Axboedef596e2019-01-09 08:59:42 -07001118 kiocb->ki_flags |= IOCB_HIPRI;
1119 kiocb->ki_complete = io_complete_rw_iopoll;
1120 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001121 if (kiocb->ki_flags & IOCB_HIPRI)
1122 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07001123 kiocb->ki_complete = io_complete_rw;
1124 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001125 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001126}
1127
1128static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
1129{
1130 switch (ret) {
1131 case -EIOCBQUEUED:
1132 break;
1133 case -ERESTARTSYS:
1134 case -ERESTARTNOINTR:
1135 case -ERESTARTNOHAND:
1136 case -ERESTART_RESTARTBLOCK:
1137 /*
1138 * We can't just restart the syscall, since previously
1139 * submitted sqes may already be in progress. Just fail this
1140 * IO with EINTR.
1141 */
1142 ret = -EINTR;
1143 /* fall through */
1144 default:
1145 kiocb->ki_complete(kiocb, ret, 0);
1146 }
1147}
1148
Jens Axboeedafcce2019-01-09 09:16:05 -07001149static int io_import_fixed(struct io_ring_ctx *ctx, int rw,
1150 const struct io_uring_sqe *sqe,
1151 struct iov_iter *iter)
1152{
1153 size_t len = READ_ONCE(sqe->len);
1154 struct io_mapped_ubuf *imu;
1155 unsigned index, buf_index;
1156 size_t offset;
1157 u64 buf_addr;
1158
1159 /* attempt to use fixed buffers without having provided iovecs */
1160 if (unlikely(!ctx->user_bufs))
1161 return -EFAULT;
1162
1163 buf_index = READ_ONCE(sqe->buf_index);
1164 if (unlikely(buf_index >= ctx->nr_user_bufs))
1165 return -EFAULT;
1166
1167 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
1168 imu = &ctx->user_bufs[index];
1169 buf_addr = READ_ONCE(sqe->addr);
1170
1171 /* overflow */
1172 if (buf_addr + len < buf_addr)
1173 return -EFAULT;
1174 /* not inside the mapped region */
1175 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
1176 return -EFAULT;
1177
1178 /*
1179 * May not be a start of buffer, set size appropriately
1180 * and advance us to the beginning.
1181 */
1182 offset = buf_addr - imu->ubuf;
1183 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06001184
1185 if (offset) {
1186 /*
1187 * Don't use iov_iter_advance() here, as it's really slow for
1188 * using the latter parts of a big fixed buffer - it iterates
1189 * over each segment manually. We can cheat a bit here, because
1190 * we know that:
1191 *
1192 * 1) it's a BVEC iter, we set it up
1193 * 2) all bvecs are PAGE_SIZE in size, except potentially the
1194 * first and last bvec
1195 *
1196 * So just find our index, and adjust the iterator afterwards.
1197 * If the offset is within the first bvec (or the whole first
1198 * bvec, just use iov_iter_advance(). This makes it easier
1199 * since we can just skip the first segment, which may not
1200 * be PAGE_SIZE aligned.
1201 */
1202 const struct bio_vec *bvec = imu->bvec;
1203
1204 if (offset <= bvec->bv_len) {
1205 iov_iter_advance(iter, offset);
1206 } else {
1207 unsigned long seg_skip;
1208
1209 /* skip first vec */
1210 offset -= bvec->bv_len;
1211 seg_skip = 1 + (offset >> PAGE_SHIFT);
1212
1213 iter->bvec = bvec + seg_skip;
1214 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02001215 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001216 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001217 }
1218 }
1219
Jens Axboeedafcce2019-01-09 09:16:05 -07001220 return 0;
1221}
1222
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001223static ssize_t io_import_iovec(struct io_ring_ctx *ctx, int rw,
1224 const struct sqe_submit *s, struct iovec **iovec,
1225 struct iov_iter *iter)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001226{
1227 const struct io_uring_sqe *sqe = s->sqe;
1228 void __user *buf = u64_to_user_ptr(READ_ONCE(sqe->addr));
1229 size_t sqe_len = READ_ONCE(sqe->len);
Jens Axboeedafcce2019-01-09 09:16:05 -07001230 u8 opcode;
1231
1232 /*
1233 * We're reading ->opcode for the second time, but the first read
1234 * doesn't care whether it's _FIXED or not, so it doesn't matter
1235 * whether ->opcode changes concurrently. The first read does care
1236 * about whether it is a READ or a WRITE, so we don't trust this read
1237 * for that purpose and instead let the caller pass in the read/write
1238 * flag.
1239 */
1240 opcode = READ_ONCE(sqe->opcode);
1241 if (opcode == IORING_OP_READ_FIXED ||
1242 opcode == IORING_OP_WRITE_FIXED) {
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001243 ssize_t ret = io_import_fixed(ctx, rw, sqe, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07001244 *iovec = NULL;
1245 return ret;
1246 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001247
1248 if (!s->has_user)
1249 return -EFAULT;
1250
1251#ifdef CONFIG_COMPAT
1252 if (ctx->compat)
1253 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
1254 iovec, iter);
1255#endif
1256
1257 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
1258}
1259
Jens Axboe6d5d5ac2019-09-11 10:16:13 -06001260static inline bool io_should_merge(struct async_list *al, struct kiocb *kiocb)
1261{
1262 if (al->file == kiocb->ki_filp) {
1263 off_t start, end;
1264
1265 /*
1266 * Allow merging if we're anywhere in the range of the same
1267 * page. Generally this happens for sub-page reads or writes,
1268 * and it's beneficial to allow the first worker to bring the
1269 * page in and the piggy backed work can then work on the
1270 * cached page.
1271 */
1272 start = al->io_start & PAGE_MASK;
1273 end = (al->io_start + al->io_len + PAGE_SIZE - 1) & PAGE_MASK;
1274 if (kiocb->ki_pos >= start && kiocb->ki_pos <= end)
1275 return true;
1276 }
1277
1278 al->file = NULL;
1279 return false;
1280}
1281
Jens Axboe31b51512019-01-18 22:56:34 -07001282/*
1283 * Make a note of the last file/offset/direction we punted to async
1284 * context. We'll use this information to see if we can piggy back a
1285 * sequential request onto the previous one, if it's still hasn't been
1286 * completed by the async worker.
1287 */
1288static void io_async_list_note(int rw, struct io_kiocb *req, size_t len)
1289{
1290 struct async_list *async_list = &req->ctx->pending_async[rw];
1291 struct kiocb *kiocb = &req->rw;
1292 struct file *filp = kiocb->ki_filp;
Jens Axboe31b51512019-01-18 22:56:34 -07001293
Jens Axboe6d5d5ac2019-09-11 10:16:13 -06001294 if (io_should_merge(async_list, kiocb)) {
Zhengyuan Liu9310a7ba2019-07-22 10:23:27 +08001295 unsigned long max_bytes;
Jens Axboe31b51512019-01-18 22:56:34 -07001296
1297 /* Use 8x RA size as a decent limiter for both reads/writes */
Zhengyuan Liu9310a7ba2019-07-22 10:23:27 +08001298 max_bytes = filp->f_ra.ra_pages << (PAGE_SHIFT + 3);
1299 if (!max_bytes)
1300 max_bytes = VM_READAHEAD_PAGES << (PAGE_SHIFT + 3);
Jens Axboe31b51512019-01-18 22:56:34 -07001301
Zhengyuan Liu9310a7ba2019-07-22 10:23:27 +08001302 /* If max len are exceeded, reset the state */
1303 if (async_list->io_len + len <= max_bytes) {
Jens Axboe31b51512019-01-18 22:56:34 -07001304 req->flags |= REQ_F_SEQ_PREV;
Zhengyuan Liu9310a7ba2019-07-22 10:23:27 +08001305 async_list->io_len += len;
Jens Axboe31b51512019-01-18 22:56:34 -07001306 } else {
Jens Axboe6d5d5ac2019-09-11 10:16:13 -06001307 async_list->file = NULL;
Jens Axboe31b51512019-01-18 22:56:34 -07001308 }
1309 }
1310
1311 /* New file? Reset state. */
1312 if (async_list->file != filp) {
Jens Axboe6d5d5ac2019-09-11 10:16:13 -06001313 async_list->io_start = kiocb->ki_pos;
1314 async_list->io_len = len;
Jens Axboe31b51512019-01-18 22:56:34 -07001315 async_list->file = filp;
1316 }
Jens Axboe31b51512019-01-18 22:56:34 -07001317}
1318
Jens Axboe32960612019-09-23 11:05:34 -06001319/*
1320 * For files that don't have ->read_iter() and ->write_iter(), handle them
1321 * by looping over ->read() or ->write() manually.
1322 */
1323static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
1324 struct iov_iter *iter)
1325{
1326 ssize_t ret = 0;
1327
1328 /*
1329 * Don't support polled IO through this interface, and we can't
1330 * support non-blocking either. For the latter, this just causes
1331 * the kiocb to be handled from an async context.
1332 */
1333 if (kiocb->ki_flags & IOCB_HIPRI)
1334 return -EOPNOTSUPP;
1335 if (kiocb->ki_flags & IOCB_NOWAIT)
1336 return -EAGAIN;
1337
1338 while (iov_iter_count(iter)) {
1339 struct iovec iovec = iov_iter_iovec(iter);
1340 ssize_t nr;
1341
1342 if (rw == READ) {
1343 nr = file->f_op->read(file, iovec.iov_base,
1344 iovec.iov_len, &kiocb->ki_pos);
1345 } else {
1346 nr = file->f_op->write(file, iovec.iov_base,
1347 iovec.iov_len, &kiocb->ki_pos);
1348 }
1349
1350 if (nr < 0) {
1351 if (!ret)
1352 ret = nr;
1353 break;
1354 }
1355 ret += nr;
1356 if (nr != iovec.iov_len)
1357 break;
1358 iov_iter_advance(iter, nr);
1359 }
1360
1361 return ret;
1362}
1363
Jens Axboee0c5c572019-03-12 10:18:47 -06001364static int io_read(struct io_kiocb *req, const struct sqe_submit *s,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001365 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001366{
1367 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1368 struct kiocb *kiocb = &req->rw;
1369 struct iov_iter iter;
1370 struct file *file;
Jens Axboe31b51512019-01-18 22:56:34 -07001371 size_t iov_count;
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001372 ssize_t read_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001373
Jens Axboe8358e3a2019-04-23 08:17:58 -06001374 ret = io_prep_rw(req, s, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001375 if (ret)
1376 return ret;
1377 file = kiocb->ki_filp;
1378
Jens Axboe2b188cc2019-01-07 10:46:33 -07001379 if (unlikely(!(file->f_mode & FMODE_READ)))
Jens Axboe09bb8392019-03-13 12:39:28 -06001380 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001381
1382 ret = io_import_iovec(req->ctx, READ, s, &iovec, &iter);
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001383 if (ret < 0)
Jens Axboe09bb8392019-03-13 12:39:28 -06001384 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001385
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001386 read_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06001387 if (req->flags & REQ_F_LINK)
1388 req->result = read_size;
1389
Jens Axboe31b51512019-01-18 22:56:34 -07001390 iov_count = iov_iter_count(&iter);
1391 ret = rw_verify_area(READ, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001392 if (!ret) {
1393 ssize_t ret2;
1394
Jens Axboe32960612019-09-23 11:05:34 -06001395 if (file->f_op->read_iter)
1396 ret2 = call_read_iter(file, kiocb, &iter);
1397 else
1398 ret2 = loop_rw_iter(READ, file, kiocb, &iter);
1399
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001400 /*
1401 * In case of a short read, punt to async. This can happen
1402 * if we have data partially cached. Alternatively we can
1403 * return the short read, in which case the application will
1404 * need to issue another SQE and wait for it. That SQE will
1405 * need async punt anyway, so it's more efficient to do it
1406 * here.
1407 */
Jens Axboe491381ce2019-10-17 09:20:46 -06001408 if (force_nonblock && !(req->flags & REQ_F_NOWAIT) &&
1409 (req->flags & REQ_F_ISREG) &&
1410 ret2 > 0 && ret2 < read_size)
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001411 ret2 = -EAGAIN;
1412 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboe31b51512019-01-18 22:56:34 -07001413 if (!force_nonblock || ret2 != -EAGAIN) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001414 io_rw_done(kiocb, ret2);
Jens Axboe31b51512019-01-18 22:56:34 -07001415 } else {
1416 /*
1417 * If ->needs_lock is true, we're already in async
1418 * context.
1419 */
1420 if (!s->needs_lock)
1421 io_async_list_note(READ, req, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001422 ret = -EAGAIN;
Jens Axboe31b51512019-01-18 22:56:34 -07001423 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001424 }
1425 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001426 return ret;
1427}
1428
Jens Axboee0c5c572019-03-12 10:18:47 -06001429static int io_write(struct io_kiocb *req, const struct sqe_submit *s,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001430 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001431{
1432 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1433 struct kiocb *kiocb = &req->rw;
1434 struct iov_iter iter;
1435 struct file *file;
Jens Axboe31b51512019-01-18 22:56:34 -07001436 size_t iov_count;
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001437 ssize_t ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001438
Jens Axboe8358e3a2019-04-23 08:17:58 -06001439 ret = io_prep_rw(req, s, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001440 if (ret)
1441 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001442
Jens Axboe2b188cc2019-01-07 10:46:33 -07001443 file = kiocb->ki_filp;
1444 if (unlikely(!(file->f_mode & FMODE_WRITE)))
Jens Axboe09bb8392019-03-13 12:39:28 -06001445 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001446
1447 ret = io_import_iovec(req->ctx, WRITE, s, &iovec, &iter);
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001448 if (ret < 0)
Jens Axboe09bb8392019-03-13 12:39:28 -06001449 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001450
Jens Axboe9e645e112019-05-10 16:07:28 -06001451 if (req->flags & REQ_F_LINK)
1452 req->result = ret;
1453
Jens Axboe31b51512019-01-18 22:56:34 -07001454 iov_count = iov_iter_count(&iter);
1455
1456 ret = -EAGAIN;
1457 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT)) {
1458 /* If ->needs_lock is true, we're already in async context. */
1459 if (!s->needs_lock)
1460 io_async_list_note(WRITE, req, iov_count);
1461 goto out_free;
1462 }
1463
1464 ret = rw_verify_area(WRITE, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001465 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01001466 ssize_t ret2;
1467
Jens Axboe2b188cc2019-01-07 10:46:33 -07001468 /*
1469 * Open-code file_start_write here to grab freeze protection,
1470 * which will be released by another thread in
1471 * io_complete_rw(). Fool lockdep by telling it the lock got
1472 * released so that it doesn't complain about the held lock when
1473 * we return to userspace.
1474 */
Jens Axboe491381ce2019-10-17 09:20:46 -06001475 if (req->flags & REQ_F_ISREG) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001476 __sb_start_write(file_inode(file)->i_sb,
1477 SB_FREEZE_WRITE, true);
1478 __sb_writers_release(file_inode(file)->i_sb,
1479 SB_FREEZE_WRITE);
1480 }
1481 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01001482
Jens Axboe32960612019-09-23 11:05:34 -06001483 if (file->f_op->write_iter)
1484 ret2 = call_write_iter(file, kiocb, &iter);
1485 else
1486 ret2 = loop_rw_iter(WRITE, file, kiocb, &iter);
Roman Penyaev9bf79332019-03-25 20:09:24 +01001487 if (!force_nonblock || ret2 != -EAGAIN) {
1488 io_rw_done(kiocb, ret2);
1489 } else {
1490 /*
1491 * If ->needs_lock is true, we're already in async
1492 * context.
1493 */
1494 if (!s->needs_lock)
1495 io_async_list_note(WRITE, req, iov_count);
1496 ret = -EAGAIN;
1497 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001498 }
Jens Axboe31b51512019-01-18 22:56:34 -07001499out_free:
Jens Axboe2b188cc2019-01-07 10:46:33 -07001500 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001501 return ret;
1502}
1503
1504/*
1505 * IORING_OP_NOP just posts a completion event, nothing else.
1506 */
1507static int io_nop(struct io_kiocb *req, u64 user_data)
1508{
1509 struct io_ring_ctx *ctx = req->ctx;
1510 long err = 0;
1511
Jens Axboedef596e2019-01-09 08:59:42 -07001512 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1513 return -EINVAL;
1514
Jens Axboec71ffb62019-05-13 20:58:29 -06001515 io_cqring_add_event(ctx, user_data, err);
Jens Axboee65ef562019-03-12 10:16:44 -06001516 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001517 return 0;
1518}
1519
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001520static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1521{
Jens Axboe6b063142019-01-10 22:13:58 -07001522 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001523
Jens Axboe09bb8392019-03-13 12:39:28 -06001524 if (!req->file)
1525 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001526
Jens Axboe6b063142019-01-10 22:13:58 -07001527 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07001528 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07001529 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001530 return -EINVAL;
1531
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001532 return 0;
1533}
1534
1535static int io_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1536 bool force_nonblock)
1537{
1538 loff_t sqe_off = READ_ONCE(sqe->off);
1539 loff_t sqe_len = READ_ONCE(sqe->len);
1540 loff_t end = sqe_off + sqe_len;
1541 unsigned fsync_flags;
1542 int ret;
1543
1544 fsync_flags = READ_ONCE(sqe->fsync_flags);
1545 if (unlikely(fsync_flags & ~IORING_FSYNC_DATASYNC))
1546 return -EINVAL;
1547
1548 ret = io_prep_fsync(req, sqe);
1549 if (ret)
1550 return ret;
1551
1552 /* fsync always requires a blocking context */
1553 if (force_nonblock)
1554 return -EAGAIN;
1555
1556 ret = vfs_fsync_range(req->rw.ki_filp, sqe_off,
1557 end > 0 ? end : LLONG_MAX,
1558 fsync_flags & IORING_FSYNC_DATASYNC);
1559
Jens Axboe9e645e112019-05-10 16:07:28 -06001560 if (ret < 0 && (req->flags & REQ_F_LINK))
1561 req->flags |= REQ_F_FAIL_LINK;
Jens Axboec71ffb62019-05-13 20:58:29 -06001562 io_cqring_add_event(req->ctx, sqe->user_data, ret);
Jens Axboee65ef562019-03-12 10:16:44 -06001563 io_put_req(req);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001564 return 0;
1565}
1566
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001567static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1568{
1569 struct io_ring_ctx *ctx = req->ctx;
1570 int ret = 0;
1571
1572 if (!req->file)
1573 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001574
1575 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1576 return -EINVAL;
1577 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
1578 return -EINVAL;
1579
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001580 return ret;
1581}
1582
1583static int io_sync_file_range(struct io_kiocb *req,
1584 const struct io_uring_sqe *sqe,
1585 bool force_nonblock)
1586{
1587 loff_t sqe_off;
1588 loff_t sqe_len;
1589 unsigned flags;
1590 int ret;
1591
1592 ret = io_prep_sfr(req, sqe);
1593 if (ret)
1594 return ret;
1595
1596 /* sync_file_range always requires a blocking context */
1597 if (force_nonblock)
1598 return -EAGAIN;
1599
1600 sqe_off = READ_ONCE(sqe->off);
1601 sqe_len = READ_ONCE(sqe->len);
1602 flags = READ_ONCE(sqe->sync_range_flags);
1603
1604 ret = sync_file_range(req->rw.ki_filp, sqe_off, sqe_len, flags);
1605
Jens Axboe9e645e112019-05-10 16:07:28 -06001606 if (ret < 0 && (req->flags & REQ_F_LINK))
1607 req->flags |= REQ_F_FAIL_LINK;
Jens Axboec71ffb62019-05-13 20:58:29 -06001608 io_cqring_add_event(req->ctx, sqe->user_data, ret);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001609 io_put_req(req);
1610 return 0;
1611}
1612
Jens Axboe0fa03c62019-04-19 13:34:07 -06001613#if defined(CONFIG_NET)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001614static int io_send_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1615 bool force_nonblock,
1616 long (*fn)(struct socket *, struct user_msghdr __user *,
1617 unsigned int))
1618{
Jens Axboe0fa03c62019-04-19 13:34:07 -06001619 struct socket *sock;
1620 int ret;
1621
1622 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1623 return -EINVAL;
1624
1625 sock = sock_from_file(req->file, &ret);
1626 if (sock) {
1627 struct user_msghdr __user *msg;
1628 unsigned flags;
1629
1630 flags = READ_ONCE(sqe->msg_flags);
1631 if (flags & MSG_DONTWAIT)
1632 req->flags |= REQ_F_NOWAIT;
1633 else if (force_nonblock)
1634 flags |= MSG_DONTWAIT;
1635
1636 msg = (struct user_msghdr __user *) (unsigned long)
1637 READ_ONCE(sqe->addr);
1638
Jens Axboeaa1fa282019-04-19 13:38:09 -06001639 ret = fn(sock, msg, flags);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001640 if (force_nonblock && ret == -EAGAIN)
1641 return ret;
1642 }
1643
1644 io_cqring_add_event(req->ctx, sqe->user_data, ret);
1645 io_put_req(req);
1646 return 0;
Jens Axboeaa1fa282019-04-19 13:38:09 -06001647}
1648#endif
1649
1650static int io_sendmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1651 bool force_nonblock)
1652{
1653#if defined(CONFIG_NET)
1654 return io_send_recvmsg(req, sqe, force_nonblock, __sys_sendmsg_sock);
1655#else
1656 return -EOPNOTSUPP;
1657#endif
1658}
1659
1660static int io_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1661 bool force_nonblock)
1662{
1663#if defined(CONFIG_NET)
1664 return io_send_recvmsg(req, sqe, force_nonblock, __sys_recvmsg_sock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001665#else
1666 return -EOPNOTSUPP;
1667#endif
1668}
1669
Jens Axboe221c5eb2019-01-17 09:41:58 -07001670static void io_poll_remove_one(struct io_kiocb *req)
1671{
1672 struct io_poll_iocb *poll = &req->poll;
1673
1674 spin_lock(&poll->head->lock);
1675 WRITE_ONCE(poll->canceled, true);
1676 if (!list_empty(&poll->wait.entry)) {
1677 list_del_init(&poll->wait.entry);
Jens Axboe18d9be12019-09-10 09:13:05 -06001678 io_queue_async_work(req->ctx, req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001679 }
1680 spin_unlock(&poll->head->lock);
1681
1682 list_del_init(&req->list);
1683}
1684
1685static void io_poll_remove_all(struct io_ring_ctx *ctx)
1686{
1687 struct io_kiocb *req;
1688
1689 spin_lock_irq(&ctx->completion_lock);
1690 while (!list_empty(&ctx->cancel_list)) {
1691 req = list_first_entry(&ctx->cancel_list, struct io_kiocb,list);
1692 io_poll_remove_one(req);
1693 }
1694 spin_unlock_irq(&ctx->completion_lock);
1695}
1696
1697/*
1698 * Find a running poll command that matches one specified in sqe->addr,
1699 * and remove it if found.
1700 */
1701static int io_poll_remove(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1702{
1703 struct io_ring_ctx *ctx = req->ctx;
1704 struct io_kiocb *poll_req, *next;
1705 int ret = -ENOENT;
1706
1707 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1708 return -EINVAL;
1709 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
1710 sqe->poll_events)
1711 return -EINVAL;
1712
1713 spin_lock_irq(&ctx->completion_lock);
1714 list_for_each_entry_safe(poll_req, next, &ctx->cancel_list, list) {
1715 if (READ_ONCE(sqe->addr) == poll_req->user_data) {
1716 io_poll_remove_one(poll_req);
1717 ret = 0;
1718 break;
1719 }
1720 }
1721 spin_unlock_irq(&ctx->completion_lock);
1722
Jens Axboec71ffb62019-05-13 20:58:29 -06001723 io_cqring_add_event(req->ctx, sqe->user_data, ret);
Jens Axboee65ef562019-03-12 10:16:44 -06001724 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001725 return 0;
1726}
1727
Jens Axboe8c838782019-03-12 15:48:16 -06001728static void io_poll_complete(struct io_ring_ctx *ctx, struct io_kiocb *req,
1729 __poll_t mask)
Jens Axboe221c5eb2019-01-17 09:41:58 -07001730{
Jens Axboe8c838782019-03-12 15:48:16 -06001731 req->poll.done = true;
Jens Axboec71ffb62019-05-13 20:58:29 -06001732 io_cqring_fill_event(ctx, req->user_data, mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06001733 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001734}
1735
1736static void io_poll_complete_work(struct work_struct *work)
1737{
1738 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
1739 struct io_poll_iocb *poll = &req->poll;
1740 struct poll_table_struct pt = { ._key = poll->events };
1741 struct io_ring_ctx *ctx = req->ctx;
1742 __poll_t mask = 0;
1743
1744 if (!READ_ONCE(poll->canceled))
1745 mask = vfs_poll(poll->file, &pt) & poll->events;
1746
1747 /*
1748 * Note that ->ki_cancel callers also delete iocb from active_reqs after
1749 * calling ->ki_cancel. We need the ctx_lock roundtrip here to
1750 * synchronize with them. In the cancellation case the list_del_init
1751 * itself is not actually needed, but harmless so we keep it in to
1752 * avoid further branches in the fast path.
1753 */
1754 spin_lock_irq(&ctx->completion_lock);
1755 if (!mask && !READ_ONCE(poll->canceled)) {
1756 add_wait_queue(poll->head, &poll->wait);
1757 spin_unlock_irq(&ctx->completion_lock);
1758 return;
1759 }
1760 list_del_init(&req->list);
Jens Axboe8c838782019-03-12 15:48:16 -06001761 io_poll_complete(ctx, req, mask);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001762 spin_unlock_irq(&ctx->completion_lock);
1763
Jens Axboe8c838782019-03-12 15:48:16 -06001764 io_cqring_ev_posted(ctx);
1765 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001766}
1767
1768static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
1769 void *key)
1770{
1771 struct io_poll_iocb *poll = container_of(wait, struct io_poll_iocb,
1772 wait);
1773 struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
1774 struct io_ring_ctx *ctx = req->ctx;
1775 __poll_t mask = key_to_poll(key);
Jens Axboe8c838782019-03-12 15:48:16 -06001776 unsigned long flags;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001777
1778 /* for instances that support it check for an event match first: */
Jens Axboe8c838782019-03-12 15:48:16 -06001779 if (mask && !(mask & poll->events))
1780 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001781
1782 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06001783
1784 if (mask && spin_trylock_irqsave(&ctx->completion_lock, flags)) {
1785 list_del(&req->list);
1786 io_poll_complete(ctx, req, mask);
1787 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1788
1789 io_cqring_ev_posted(ctx);
1790 io_put_req(req);
1791 } else {
Jens Axboe18d9be12019-09-10 09:13:05 -06001792 io_queue_async_work(ctx, req);
Jens Axboe8c838782019-03-12 15:48:16 -06001793 }
1794
Jens Axboe221c5eb2019-01-17 09:41:58 -07001795 return 1;
1796}
1797
1798struct io_poll_table {
1799 struct poll_table_struct pt;
1800 struct io_kiocb *req;
1801 int error;
1802};
1803
1804static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
1805 struct poll_table_struct *p)
1806{
1807 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
1808
1809 if (unlikely(pt->req->poll.head)) {
1810 pt->error = -EINVAL;
1811 return;
1812 }
1813
1814 pt->error = 0;
1815 pt->req->poll.head = head;
1816 add_wait_queue(head, &pt->req->poll.wait);
1817}
1818
1819static int io_poll_add(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1820{
1821 struct io_poll_iocb *poll = &req->poll;
1822 struct io_ring_ctx *ctx = req->ctx;
1823 struct io_poll_table ipt;
Jens Axboe8c838782019-03-12 15:48:16 -06001824 bool cancel = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001825 __poll_t mask;
1826 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001827
1828 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1829 return -EINVAL;
1830 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
1831 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06001832 if (!poll->file)
1833 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001834
Jens Axboe6cc47d12019-09-18 11:18:23 -06001835 req->submit.sqe = NULL;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001836 INIT_WORK(&req->work, io_poll_complete_work);
1837 events = READ_ONCE(sqe->poll_events);
1838 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
1839
Jens Axboe221c5eb2019-01-17 09:41:58 -07001840 poll->head = NULL;
Jens Axboe8c838782019-03-12 15:48:16 -06001841 poll->done = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001842 poll->canceled = false;
1843
1844 ipt.pt._qproc = io_poll_queue_proc;
1845 ipt.pt._key = poll->events;
1846 ipt.req = req;
1847 ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
1848
1849 /* initialized the list so that we can do list_empty checks */
1850 INIT_LIST_HEAD(&poll->wait.entry);
1851 init_waitqueue_func_entry(&poll->wait, io_poll_wake);
1852
Jens Axboe36703242019-07-25 10:20:18 -06001853 INIT_LIST_HEAD(&req->list);
1854
Jens Axboe221c5eb2019-01-17 09:41:58 -07001855 mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001856
1857 spin_lock_irq(&ctx->completion_lock);
Jens Axboe8c838782019-03-12 15:48:16 -06001858 if (likely(poll->head)) {
1859 spin_lock(&poll->head->lock);
1860 if (unlikely(list_empty(&poll->wait.entry))) {
1861 if (ipt.error)
1862 cancel = true;
1863 ipt.error = 0;
1864 mask = 0;
1865 }
1866 if (mask || ipt.error)
1867 list_del_init(&poll->wait.entry);
1868 else if (cancel)
1869 WRITE_ONCE(poll->canceled, true);
1870 else if (!poll->done) /* actually waiting for an event */
1871 list_add_tail(&req->list, &ctx->cancel_list);
1872 spin_unlock(&poll->head->lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001873 }
Jens Axboe8c838782019-03-12 15:48:16 -06001874 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06001875 ipt.error = 0;
1876 io_poll_complete(ctx, req, mask);
1877 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07001878 spin_unlock_irq(&ctx->completion_lock);
1879
Jens Axboe8c838782019-03-12 15:48:16 -06001880 if (mask) {
1881 io_cqring_ev_posted(ctx);
Jens Axboee65ef562019-03-12 10:16:44 -06001882 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001883 }
Jens Axboe8c838782019-03-12 15:48:16 -06001884 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001885}
1886
Jens Axboe5262f562019-09-17 12:26:57 -06001887static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
1888{
1889 struct io_ring_ctx *ctx;
zhangyi (F)ef036812019-10-23 15:10:08 +08001890 struct io_kiocb *req, *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06001891 unsigned long flags;
1892
1893 req = container_of(timer, struct io_kiocb, timeout.timer);
1894 ctx = req->ctx;
1895 atomic_inc(&ctx->cq_timeouts);
1896
1897 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08001898 /*
1899 * Adjust the reqs sequence before the current one because it
1900 * will consume a slot in the cq_ring and the the cq_tail pointer
1901 * will be increased, otherwise other timeout reqs may return in
1902 * advance without waiting for enough wait_nr.
1903 */
1904 prev = req;
1905 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
1906 prev->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06001907 list_del(&req->list);
1908
1909 io_cqring_fill_event(ctx, req->user_data, -ETIME);
1910 io_commit_cqring(ctx);
1911 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1912
1913 io_cqring_ev_posted(ctx);
1914
1915 io_put_req(req);
1916 return HRTIMER_NORESTART;
1917}
1918
1919static int io_timeout(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1920{
yangerkun5da0fb12019-10-15 21:59:29 +08001921 unsigned count;
Jens Axboe5262f562019-09-17 12:26:57 -06001922 struct io_ring_ctx *ctx = req->ctx;
1923 struct list_head *entry;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06001924 struct timespec64 ts;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08001925 unsigned span = 0;
Jens Axboe5262f562019-09-17 12:26:57 -06001926
1927 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1928 return -EINVAL;
1929 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->timeout_flags ||
1930 sqe->len != 1)
1931 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06001932
1933 if (get_timespec64(&ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06001934 return -EFAULT;
1935
1936 /*
1937 * sqe->off holds how many events that need to occur for this
1938 * timeout event to be satisfied.
1939 */
1940 count = READ_ONCE(sqe->off);
1941 if (!count)
1942 count = 1;
1943
1944 req->sequence = ctx->cached_sq_head + count - 1;
yangerkun5da0fb12019-10-15 21:59:29 +08001945 /* reuse it to store the count */
1946 req->submit.sequence = count;
Jens Axboe5262f562019-09-17 12:26:57 -06001947 req->flags |= REQ_F_TIMEOUT;
1948
1949 /*
1950 * Insertion sort, ensuring the first entry in the list is always
1951 * the one we need first.
1952 */
Jens Axboe5262f562019-09-17 12:26:57 -06001953 spin_lock_irq(&ctx->completion_lock);
1954 list_for_each_prev(entry, &ctx->timeout_list) {
1955 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
yangerkun5da0fb12019-10-15 21:59:29 +08001956 unsigned nxt_sq_head;
1957 long long tmp, tmp_nxt;
Jens Axboe5262f562019-09-17 12:26:57 -06001958
yangerkun5da0fb12019-10-15 21:59:29 +08001959 /*
1960 * Since cached_sq_head + count - 1 can overflow, use type long
1961 * long to store it.
1962 */
1963 tmp = (long long)ctx->cached_sq_head + count - 1;
1964 nxt_sq_head = nxt->sequence - nxt->submit.sequence + 1;
1965 tmp_nxt = (long long)nxt_sq_head + nxt->submit.sequence - 1;
1966
1967 /*
1968 * cached_sq_head may overflow, and it will never overflow twice
1969 * once there is some timeout req still be valid.
1970 */
1971 if (ctx->cached_sq_head < nxt_sq_head)
yangerkun8b07a652019-10-17 12:12:35 +08001972 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08001973
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08001974 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06001975 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08001976
1977 /*
1978 * Sequence of reqs after the insert one and itself should
1979 * be adjusted because each timeout req consumes a slot.
1980 */
1981 span++;
1982 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06001983 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08001984 req->sequence -= span;
Jens Axboe5262f562019-09-17 12:26:57 -06001985 list_add(&req->list, entry);
1986 spin_unlock_irq(&ctx->completion_lock);
1987
1988 hrtimer_init(&req->timeout.timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1989 req->timeout.timer.function = io_timeout_fn;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06001990 hrtimer_start(&req->timeout.timer, timespec64_to_ktime(ts),
Jens Axboe5262f562019-09-17 12:26:57 -06001991 HRTIMER_MODE_REL);
1992 return 0;
1993}
1994
Jens Axboede0617e2019-04-06 21:51:27 -06001995static int io_req_defer(struct io_ring_ctx *ctx, struct io_kiocb *req,
1996 const struct io_uring_sqe *sqe)
1997{
1998 struct io_uring_sqe *sqe_copy;
1999
2000 if (!io_sequence_defer(ctx, req) && list_empty(&ctx->defer_list))
2001 return 0;
2002
2003 sqe_copy = kmalloc(sizeof(*sqe_copy), GFP_KERNEL);
2004 if (!sqe_copy)
2005 return -EAGAIN;
2006
2007 spin_lock_irq(&ctx->completion_lock);
2008 if (!io_sequence_defer(ctx, req) && list_empty(&ctx->defer_list)) {
2009 spin_unlock_irq(&ctx->completion_lock);
2010 kfree(sqe_copy);
2011 return 0;
2012 }
2013
2014 memcpy(sqe_copy, sqe, sizeof(*sqe_copy));
2015 req->submit.sqe = sqe_copy;
2016
2017 INIT_WORK(&req->work, io_sq_wq_submit_work);
2018 list_add_tail(&req->list, &ctx->defer_list);
2019 spin_unlock_irq(&ctx->completion_lock);
2020 return -EIOCBQUEUED;
2021}
2022
Jens Axboe2b188cc2019-01-07 10:46:33 -07002023static int __io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
Jens Axboe8358e3a2019-04-23 08:17:58 -06002024 const struct sqe_submit *s, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002025{
Jens Axboee0c5c572019-03-12 10:18:47 -06002026 int ret, opcode;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002027
Jens Axboe9e645e112019-05-10 16:07:28 -06002028 req->user_data = READ_ONCE(s->sqe->user_data);
2029
Jens Axboe2b188cc2019-01-07 10:46:33 -07002030 if (unlikely(s->index >= ctx->sq_entries))
2031 return -EINVAL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002032
2033 opcode = READ_ONCE(s->sqe->opcode);
2034 switch (opcode) {
2035 case IORING_OP_NOP:
2036 ret = io_nop(req, req->user_data);
2037 break;
2038 case IORING_OP_READV:
Jens Axboeedafcce2019-01-09 09:16:05 -07002039 if (unlikely(s->sqe->buf_index))
2040 return -EINVAL;
Jens Axboe8358e3a2019-04-23 08:17:58 -06002041 ret = io_read(req, s, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002042 break;
2043 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07002044 if (unlikely(s->sqe->buf_index))
2045 return -EINVAL;
Jens Axboe8358e3a2019-04-23 08:17:58 -06002046 ret = io_write(req, s, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07002047 break;
2048 case IORING_OP_READ_FIXED:
Jens Axboe8358e3a2019-04-23 08:17:58 -06002049 ret = io_read(req, s, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07002050 break;
2051 case IORING_OP_WRITE_FIXED:
Jens Axboe8358e3a2019-04-23 08:17:58 -06002052 ret = io_write(req, s, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002053 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002054 case IORING_OP_FSYNC:
2055 ret = io_fsync(req, s->sqe, force_nonblock);
2056 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002057 case IORING_OP_POLL_ADD:
2058 ret = io_poll_add(req, s->sqe);
2059 break;
2060 case IORING_OP_POLL_REMOVE:
2061 ret = io_poll_remove(req, s->sqe);
2062 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002063 case IORING_OP_SYNC_FILE_RANGE:
2064 ret = io_sync_file_range(req, s->sqe, force_nonblock);
2065 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002066 case IORING_OP_SENDMSG:
2067 ret = io_sendmsg(req, s->sqe, force_nonblock);
2068 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06002069 case IORING_OP_RECVMSG:
2070 ret = io_recvmsg(req, s->sqe, force_nonblock);
2071 break;
Jens Axboe5262f562019-09-17 12:26:57 -06002072 case IORING_OP_TIMEOUT:
2073 ret = io_timeout(req, s->sqe);
2074 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002075 default:
2076 ret = -EINVAL;
2077 break;
2078 }
2079
Jens Axboedef596e2019-01-09 08:59:42 -07002080 if (ret)
2081 return ret;
2082
2083 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe9e645e112019-05-10 16:07:28 -06002084 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07002085 return -EAGAIN;
2086
2087 /* workqueue context doesn't hold uring_lock, grab it now */
2088 if (s->needs_lock)
2089 mutex_lock(&ctx->uring_lock);
2090 io_iopoll_req_issued(req);
2091 if (s->needs_lock)
2092 mutex_unlock(&ctx->uring_lock);
2093 }
2094
2095 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002096}
2097
Jens Axboe31b51512019-01-18 22:56:34 -07002098static struct async_list *io_async_list_from_sqe(struct io_ring_ctx *ctx,
2099 const struct io_uring_sqe *sqe)
2100{
2101 switch (sqe->opcode) {
2102 case IORING_OP_READV:
2103 case IORING_OP_READ_FIXED:
2104 return &ctx->pending_async[READ];
2105 case IORING_OP_WRITEV:
2106 case IORING_OP_WRITE_FIXED:
2107 return &ctx->pending_async[WRITE];
2108 default:
2109 return NULL;
2110 }
2111}
2112
Jens Axboeedafcce2019-01-09 09:16:05 -07002113static inline bool io_sqe_needs_user(const struct io_uring_sqe *sqe)
2114{
2115 u8 opcode = READ_ONCE(sqe->opcode);
2116
2117 return !(opcode == IORING_OP_READ_FIXED ||
2118 opcode == IORING_OP_WRITE_FIXED);
2119}
2120
Jens Axboe2b188cc2019-01-07 10:46:33 -07002121static void io_sq_wq_submit_work(struct work_struct *work)
2122{
2123 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002124 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe31b51512019-01-18 22:56:34 -07002125 struct mm_struct *cur_mm = NULL;
2126 struct async_list *async_list;
2127 LIST_HEAD(req_list);
Jens Axboeedafcce2019-01-09 09:16:05 -07002128 mm_segment_t old_fs;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002129 int ret;
2130
Jens Axboe31b51512019-01-18 22:56:34 -07002131 async_list = io_async_list_from_sqe(ctx, req->submit.sqe);
2132restart:
2133 do {
2134 struct sqe_submit *s = &req->submit;
2135 const struct io_uring_sqe *sqe = s->sqe;
Jackie Liud0ee8792019-07-31 14:39:33 +08002136 unsigned int flags = req->flags;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002137
Stefan Bühler8449eed2019-04-27 20:34:19 +02002138 /* Ensure we clear previously set non-block flag */
Jens Axboe31b51512019-01-18 22:56:34 -07002139 req->rw.ki_flags &= ~IOCB_NOWAIT;
2140
2141 ret = 0;
2142 if (io_sqe_needs_user(sqe) && !cur_mm) {
2143 if (!mmget_not_zero(ctx->sqo_mm)) {
2144 ret = -EFAULT;
2145 } else {
2146 cur_mm = ctx->sqo_mm;
2147 use_mm(cur_mm);
2148 old_fs = get_fs();
2149 set_fs(USER_DS);
2150 }
2151 }
2152
2153 if (!ret) {
2154 s->has_user = cur_mm != NULL;
2155 s->needs_lock = true;
2156 do {
Jens Axboe8358e3a2019-04-23 08:17:58 -06002157 ret = __io_submit_sqe(ctx, req, s, false);
Jens Axboe31b51512019-01-18 22:56:34 -07002158 /*
2159 * We can get EAGAIN for polled IO even though
2160 * we're forcing a sync submission from here,
2161 * since we can't wait for request slots on the
2162 * block side.
2163 */
2164 if (ret != -EAGAIN)
2165 break;
2166 cond_resched();
2167 } while (1);
2168 }
Jens Axboe817869d2019-04-30 14:44:05 -06002169
2170 /* drop submission reference */
2171 io_put_req(req);
2172
Jens Axboe31b51512019-01-18 22:56:34 -07002173 if (ret) {
Jens Axboec71ffb62019-05-13 20:58:29 -06002174 io_cqring_add_event(ctx, sqe->user_data, ret);
Jens Axboee65ef562019-03-12 10:16:44 -06002175 io_put_req(req);
Jens Axboe31b51512019-01-18 22:56:34 -07002176 }
2177
2178 /* async context always use a copy of the sqe */
2179 kfree(sqe);
2180
Zhengyuan Liuf7b76ac2019-07-16 23:26:14 +08002181 /* req from defer and link list needn't decrease async cnt */
Jackie Liud0ee8792019-07-31 14:39:33 +08002182 if (flags & (REQ_F_IO_DRAINED | REQ_F_LINK_DONE))
Zhengyuan Liuf7b76ac2019-07-16 23:26:14 +08002183 goto out;
2184
Jens Axboe31b51512019-01-18 22:56:34 -07002185 if (!async_list)
2186 break;
2187 if (!list_empty(&req_list)) {
2188 req = list_first_entry(&req_list, struct io_kiocb,
2189 list);
2190 list_del(&req->list);
2191 continue;
2192 }
2193 if (list_empty(&async_list->list))
2194 break;
2195
2196 req = NULL;
2197 spin_lock(&async_list->lock);
2198 if (list_empty(&async_list->list)) {
2199 spin_unlock(&async_list->lock);
2200 break;
2201 }
2202 list_splice_init(&async_list->list, &req_list);
2203 spin_unlock(&async_list->lock);
2204
2205 req = list_first_entry(&req_list, struct io_kiocb, list);
2206 list_del(&req->list);
2207 } while (req);
Jens Axboeedafcce2019-01-09 09:16:05 -07002208
2209 /*
Jens Axboe31b51512019-01-18 22:56:34 -07002210 * Rare case of racing with a submitter. If we find the count has
2211 * dropped to zero AND we have pending work items, then restart
2212 * the processing. This is a tiny race window.
Jens Axboeedafcce2019-01-09 09:16:05 -07002213 */
Jens Axboe31b51512019-01-18 22:56:34 -07002214 if (async_list) {
2215 ret = atomic_dec_return(&async_list->cnt);
2216 while (!ret && !list_empty(&async_list->list)) {
2217 spin_lock(&async_list->lock);
2218 atomic_inc(&async_list->cnt);
2219 list_splice_init(&async_list->list, &req_list);
2220 spin_unlock(&async_list->lock);
2221
2222 if (!list_empty(&req_list)) {
2223 req = list_first_entry(&req_list,
2224 struct io_kiocb, list);
2225 list_del(&req->list);
2226 goto restart;
2227 }
2228 ret = atomic_dec_return(&async_list->cnt);
Jens Axboeedafcce2019-01-09 09:16:05 -07002229 }
Jens Axboeedafcce2019-01-09 09:16:05 -07002230 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002231
Zhengyuan Liuf7b76ac2019-07-16 23:26:14 +08002232out:
Jens Axboe31b51512019-01-18 22:56:34 -07002233 if (cur_mm) {
Jens Axboeedafcce2019-01-09 09:16:05 -07002234 set_fs(old_fs);
Jens Axboe31b51512019-01-18 22:56:34 -07002235 unuse_mm(cur_mm);
2236 mmput(cur_mm);
Jens Axboeedafcce2019-01-09 09:16:05 -07002237 }
Jens Axboe31b51512019-01-18 22:56:34 -07002238}
Jens Axboe2b188cc2019-01-07 10:46:33 -07002239
Jens Axboe31b51512019-01-18 22:56:34 -07002240/*
2241 * See if we can piggy back onto previously submitted work, that is still
2242 * running. We currently only allow this if the new request is sequential
2243 * to the previous one we punted.
2244 */
2245static bool io_add_to_prev_work(struct async_list *list, struct io_kiocb *req)
2246{
Jens Axboe6d5d5ac2019-09-11 10:16:13 -06002247 bool ret;
Jens Axboe31b51512019-01-18 22:56:34 -07002248
2249 if (!list)
2250 return false;
2251 if (!(req->flags & REQ_F_SEQ_PREV))
2252 return false;
2253 if (!atomic_read(&list->cnt))
2254 return false;
2255
2256 ret = true;
2257 spin_lock(&list->lock);
2258 list_add_tail(&req->list, &list->list);
Zhengyuan Liuc0e48f92019-07-18 20:44:00 +08002259 /*
2260 * Ensure we see a simultaneous modification from io_sq_wq_submit_work()
2261 */
2262 smp_mb();
Jens Axboe31b51512019-01-18 22:56:34 -07002263 if (!atomic_read(&list->cnt)) {
2264 list_del_init(&req->list);
2265 ret = false;
2266 }
2267 spin_unlock(&list->lock);
2268 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002269}
2270
Jens Axboe09bb8392019-03-13 12:39:28 -06002271static bool io_op_needs_file(const struct io_uring_sqe *sqe)
2272{
2273 int op = READ_ONCE(sqe->opcode);
2274
2275 switch (op) {
2276 case IORING_OP_NOP:
2277 case IORING_OP_POLL_REMOVE:
2278 return false;
2279 default:
2280 return true;
2281 }
2282}
2283
2284static int io_req_set_file(struct io_ring_ctx *ctx, const struct sqe_submit *s,
2285 struct io_submit_state *state, struct io_kiocb *req)
2286{
2287 unsigned flags;
2288 int fd;
2289
2290 flags = READ_ONCE(s->sqe->flags);
2291 fd = READ_ONCE(s->sqe->fd);
2292
Jackie Liu4fe2c962019-09-09 20:50:40 +08002293 if (flags & IOSQE_IO_DRAIN)
Jens Axboede0617e2019-04-06 21:51:27 -06002294 req->flags |= REQ_F_IO_DRAIN;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002295 /*
2296 * All io need record the previous position, if LINK vs DARIN,
2297 * it can be used to mark the position of the first IO in the
2298 * link list.
2299 */
2300 req->sequence = s->sequence;
Jens Axboede0617e2019-04-06 21:51:27 -06002301
Jens Axboe60c112b2019-06-21 10:20:18 -06002302 if (!io_op_needs_file(s->sqe))
Jens Axboe09bb8392019-03-13 12:39:28 -06002303 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06002304
2305 if (flags & IOSQE_FIXED_FILE) {
2306 if (unlikely(!ctx->user_files ||
2307 (unsigned) fd >= ctx->nr_user_files))
2308 return -EBADF;
2309 req->file = ctx->user_files[fd];
2310 req->flags |= REQ_F_FIXED_FILE;
2311 } else {
2312 if (s->needs_fixed_file)
2313 return -EBADF;
2314 req->file = io_file_get(state, fd);
2315 if (unlikely(!req->file))
2316 return -EBADF;
2317 }
2318
2319 return 0;
2320}
2321
Jackie Liu4fe2c962019-09-09 20:50:40 +08002322static int __io_queue_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
Jens Axboebc808bc2019-10-22 13:14:37 -06002323 struct sqe_submit *s)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002324{
Jens Axboee0c5c572019-03-12 10:18:47 -06002325 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002326
Jens Axboebc808bc2019-10-22 13:14:37 -06002327 ret = __io_submit_sqe(ctx, req, s, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06002328
2329 /*
2330 * We async punt it if the file wasn't marked NOWAIT, or if the file
2331 * doesn't support non-blocking read/write attempts
2332 */
2333 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
2334 (req->flags & REQ_F_MUST_PUNT))) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07002335 struct io_uring_sqe *sqe_copy;
2336
Jackie Liu954dab12019-09-18 10:37:52 +08002337 sqe_copy = kmemdup(s->sqe, sizeof(*sqe_copy), GFP_KERNEL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002338 if (sqe_copy) {
Jens Axboe31b51512019-01-18 22:56:34 -07002339 struct async_list *list;
2340
Jens Axboe2b188cc2019-01-07 10:46:33 -07002341 s->sqe = sqe_copy;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002342 memcpy(&req->submit, s, sizeof(*s));
Jens Axboe31b51512019-01-18 22:56:34 -07002343 list = io_async_list_from_sqe(ctx, s->sqe);
2344 if (!io_add_to_prev_work(list, req)) {
2345 if (list)
2346 atomic_inc(&list->cnt);
2347 INIT_WORK(&req->work, io_sq_wq_submit_work);
Jens Axboe18d9be12019-09-10 09:13:05 -06002348 io_queue_async_work(ctx, req);
Jens Axboe31b51512019-01-18 22:56:34 -07002349 }
Jens Axboee65ef562019-03-12 10:16:44 -06002350
2351 /*
2352 * Queued up for async execution, worker will release
Jens Axboe9e645e112019-05-10 16:07:28 -06002353 * submit reference when the iocb is actually submitted.
Jens Axboee65ef562019-03-12 10:16:44 -06002354 */
2355 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002356 }
2357 }
Jens Axboee65ef562019-03-12 10:16:44 -06002358
2359 /* drop submission reference */
2360 io_put_req(req);
2361
2362 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06002363 if (ret) {
2364 io_cqring_add_event(ctx, req->user_data, ret);
2365 if (req->flags & REQ_F_LINK)
2366 req->flags |= REQ_F_FAIL_LINK;
Jens Axboee65ef562019-03-12 10:16:44 -06002367 io_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002368 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002369
2370 return ret;
2371}
2372
Jackie Liu4fe2c962019-09-09 20:50:40 +08002373static int io_queue_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
Jens Axboebc808bc2019-10-22 13:14:37 -06002374 struct sqe_submit *s)
Jackie Liu4fe2c962019-09-09 20:50:40 +08002375{
2376 int ret;
2377
2378 ret = io_req_defer(ctx, req, s->sqe);
2379 if (ret) {
2380 if (ret != -EIOCBQUEUED) {
2381 io_free_req(req);
2382 io_cqring_add_event(ctx, s->sqe->user_data, ret);
2383 }
2384 return 0;
2385 }
2386
Jens Axboebc808bc2019-10-22 13:14:37 -06002387 return __io_queue_sqe(ctx, req, s);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002388}
2389
2390static int io_queue_link_head(struct io_ring_ctx *ctx, struct io_kiocb *req,
Jens Axboebc808bc2019-10-22 13:14:37 -06002391 struct sqe_submit *s, struct io_kiocb *shadow)
Jackie Liu4fe2c962019-09-09 20:50:40 +08002392{
2393 int ret;
2394 int need_submit = false;
2395
2396 if (!shadow)
Jens Axboebc808bc2019-10-22 13:14:37 -06002397 return io_queue_sqe(ctx, req, s);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002398
2399 /*
2400 * Mark the first IO in link list as DRAIN, let all the following
2401 * IOs enter the defer list. all IO needs to be completed before link
2402 * list.
2403 */
2404 req->flags |= REQ_F_IO_DRAIN;
2405 ret = io_req_defer(ctx, req, s->sqe);
2406 if (ret) {
2407 if (ret != -EIOCBQUEUED) {
2408 io_free_req(req);
2409 io_cqring_add_event(ctx, s->sqe->user_data, ret);
2410 return 0;
2411 }
2412 } else {
2413 /*
2414 * If ret == 0 means that all IOs in front of link io are
2415 * running done. let's queue link head.
2416 */
2417 need_submit = true;
2418 }
2419
2420 /* Insert shadow req to defer_list, blocking next IOs */
2421 spin_lock_irq(&ctx->completion_lock);
2422 list_add_tail(&shadow->list, &ctx->defer_list);
2423 spin_unlock_irq(&ctx->completion_lock);
2424
2425 if (need_submit)
Jens Axboebc808bc2019-10-22 13:14:37 -06002426 return __io_queue_sqe(ctx, req, s);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002427
2428 return 0;
2429}
2430
Jens Axboe9e645e112019-05-10 16:07:28 -06002431#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK)
2432
2433static void io_submit_sqe(struct io_ring_ctx *ctx, struct sqe_submit *s,
Jens Axboebc808bc2019-10-22 13:14:37 -06002434 struct io_submit_state *state, struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06002435{
2436 struct io_uring_sqe *sqe_copy;
2437 struct io_kiocb *req;
2438 int ret;
2439
2440 /* enforce forwards compatibility on users */
2441 if (unlikely(s->sqe->flags & ~SQE_VALID_FLAGS)) {
2442 ret = -EINVAL;
2443 goto err;
2444 }
2445
2446 req = io_get_req(ctx, state);
2447 if (unlikely(!req)) {
2448 ret = -EAGAIN;
2449 goto err;
2450 }
2451
2452 ret = io_req_set_file(ctx, s, state, req);
2453 if (unlikely(ret)) {
2454err_req:
2455 io_free_req(req);
2456err:
2457 io_cqring_add_event(ctx, s->sqe->user_data, ret);
2458 return;
2459 }
2460
Pavel Begunkov84d55dc2019-10-25 12:31:29 +03002461 req->user_data = s->sqe->user_data;
2462
Jens Axboe9e645e112019-05-10 16:07:28 -06002463 /*
2464 * If we already have a head request, queue this one for async
2465 * submittal once the head completes. If we don't have a head but
2466 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
2467 * submitted sync once the chain is complete. If none of those
2468 * conditions are true (normal request), then just queue it.
2469 */
2470 if (*link) {
2471 struct io_kiocb *prev = *link;
2472
2473 sqe_copy = kmemdup(s->sqe, sizeof(*sqe_copy), GFP_KERNEL);
2474 if (!sqe_copy) {
2475 ret = -EAGAIN;
2476 goto err_req;
2477 }
2478
2479 s->sqe = sqe_copy;
2480 memcpy(&req->submit, s, sizeof(*s));
2481 list_add_tail(&req->list, &prev->link_list);
2482 } else if (s->sqe->flags & IOSQE_IO_LINK) {
2483 req->flags |= REQ_F_LINK;
2484
2485 memcpy(&req->submit, s, sizeof(*s));
2486 INIT_LIST_HEAD(&req->link_list);
2487 *link = req;
2488 } else {
Jens Axboebc808bc2019-10-22 13:14:37 -06002489 io_queue_sqe(ctx, req, s);
Jens Axboe9e645e112019-05-10 16:07:28 -06002490 }
2491}
2492
Jens Axboe9a56a232019-01-09 09:06:50 -07002493/*
2494 * Batched submission is done, ensure local IO is flushed out.
2495 */
2496static void io_submit_state_end(struct io_submit_state *state)
2497{
2498 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06002499 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07002500 if (state->free_reqs)
2501 kmem_cache_free_bulk(req_cachep, state->free_reqs,
2502 &state->reqs[state->cur_req]);
Jens Axboe9a56a232019-01-09 09:06:50 -07002503}
2504
2505/*
2506 * Start submission side cache.
2507 */
2508static void io_submit_state_start(struct io_submit_state *state,
2509 struct io_ring_ctx *ctx, unsigned max_ios)
2510{
2511 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07002512 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07002513 state->file = NULL;
2514 state->ios_left = max_ios;
2515}
2516
Jens Axboe2b188cc2019-01-07 10:46:33 -07002517static void io_commit_sqring(struct io_ring_ctx *ctx)
2518{
Hristo Venev75b28af2019-08-26 17:23:46 +00002519 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002520
Hristo Venev75b28af2019-08-26 17:23:46 +00002521 if (ctx->cached_sq_head != READ_ONCE(rings->sq.head)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07002522 /*
2523 * Ensure any loads from the SQEs are done at this point,
2524 * since once we write the new head, the application could
2525 * write new data to them.
2526 */
Hristo Venev75b28af2019-08-26 17:23:46 +00002527 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002528 }
2529}
2530
2531/*
Jens Axboe2b188cc2019-01-07 10:46:33 -07002532 * Fetch an sqe, if one is available. Note that s->sqe will point to memory
2533 * that is mapped by userspace. This means that care needs to be taken to
2534 * ensure that reads are stable, as we cannot rely on userspace always
2535 * being a good citizen. If members of the sqe are validated and then later
2536 * used, it's important that those reads are done through READ_ONCE() to
2537 * prevent a re-load down the line.
2538 */
2539static bool io_get_sqring(struct io_ring_ctx *ctx, struct sqe_submit *s)
2540{
Hristo Venev75b28af2019-08-26 17:23:46 +00002541 struct io_rings *rings = ctx->rings;
2542 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002543 unsigned head;
2544
2545 /*
2546 * The cached sq head (or cq tail) serves two purposes:
2547 *
2548 * 1) allows us to batch the cost of updating the user visible
2549 * head updates.
2550 * 2) allows the kernel side to track the head on its own, even
2551 * though the application is the one updating it.
2552 */
2553 head = ctx->cached_sq_head;
Stefan Bühlere523a292019-04-19 11:57:44 +02002554 /* make sure SQ entry isn't read before tail */
Hristo Venev75b28af2019-08-26 17:23:46 +00002555 if (head == smp_load_acquire(&rings->sq.tail))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002556 return false;
2557
Hristo Venev75b28af2019-08-26 17:23:46 +00002558 head = READ_ONCE(sq_array[head & ctx->sq_mask]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002559 if (head < ctx->sq_entries) {
2560 s->index = head;
2561 s->sqe = &ctx->sq_sqes[head];
Jackie Liu8776f3f2019-09-09 20:50:39 +08002562 s->sequence = ctx->cached_sq_head;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002563 ctx->cached_sq_head++;
2564 return true;
2565 }
2566
2567 /* drop invalid entries */
2568 ctx->cached_sq_head++;
Jens Axboe498ccd92019-10-25 10:04:25 -06002569 ctx->cached_sq_dropped++;
2570 WRITE_ONCE(rings->sq_dropped, ctx->cached_sq_dropped);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002571 return false;
2572}
2573
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002574static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
2575 bool has_user, bool mm_fault)
Jens Axboe6c271ce2019-01-10 11:22:30 -07002576{
2577 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002578 struct io_kiocb *link = NULL;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002579 struct io_kiocb *shadow_req = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002580 bool prev_was_link = false;
2581 int i, submitted = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002582
2583 if (nr > IO_PLUG_THRESHOLD) {
2584 io_submit_state_start(&state, ctx, nr);
2585 statep = &state;
2586 }
2587
2588 for (i = 0; i < nr; i++) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002589 struct sqe_submit s;
2590
2591 if (!io_get_sqring(ctx, &s))
2592 break;
2593
Jens Axboe9e645e112019-05-10 16:07:28 -06002594 /*
2595 * If previous wasn't linked and we have a linked command,
2596 * that's the end of the chain. Submit the previous link.
2597 */
2598 if (!prev_was_link && link) {
Jens Axboebc808bc2019-10-22 13:14:37 -06002599 io_queue_link_head(ctx, link, &link->submit, shadow_req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002600 link = NULL;
Jackie Liu5f5ad9c2019-09-18 10:37:53 +08002601 shadow_req = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002602 }
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002603 prev_was_link = (s.sqe->flags & IOSQE_IO_LINK) != 0;
Jens Axboe9e645e112019-05-10 16:07:28 -06002604
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002605 if (link && (s.sqe->flags & IOSQE_IO_DRAIN)) {
Jackie Liu4fe2c962019-09-09 20:50:40 +08002606 if (!shadow_req) {
2607 shadow_req = io_get_req(ctx, NULL);
Jackie Liua1041c22019-09-18 17:25:52 +08002608 if (unlikely(!shadow_req))
2609 goto out;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002610 shadow_req->flags |= (REQ_F_IO_DRAIN | REQ_F_SHADOW_DRAIN);
2611 refcount_dec(&shadow_req->refs);
2612 }
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002613 shadow_req->sequence = s.sequence;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002614 }
2615
Jackie Liua1041c22019-09-18 17:25:52 +08002616out:
Jens Axboe6c271ce2019-01-10 11:22:30 -07002617 if (unlikely(mm_fault)) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002618 io_cqring_add_event(ctx, s.sqe->user_data,
Jens Axboe9e645e112019-05-10 16:07:28 -06002619 -EFAULT);
Jens Axboe6c271ce2019-01-10 11:22:30 -07002620 } else {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002621 s.has_user = has_user;
2622 s.needs_lock = true;
2623 s.needs_fixed_file = true;
2624 io_submit_sqe(ctx, &s, statep, &link);
Jens Axboe6c271ce2019-01-10 11:22:30 -07002625 submitted++;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002626 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07002627 }
2628
Jens Axboe9e645e112019-05-10 16:07:28 -06002629 if (link)
Jens Axboebc808bc2019-10-22 13:14:37 -06002630 io_queue_link_head(ctx, link, &link->submit, shadow_req);
Jens Axboe6c271ce2019-01-10 11:22:30 -07002631 if (statep)
2632 io_submit_state_end(&state);
2633
2634 return submitted;
2635}
2636
2637static int io_sq_thread(void *data)
2638{
Jens Axboe6c271ce2019-01-10 11:22:30 -07002639 struct io_ring_ctx *ctx = data;
2640 struct mm_struct *cur_mm = NULL;
2641 mm_segment_t old_fs;
2642 DEFINE_WAIT(wait);
2643 unsigned inflight;
2644 unsigned long timeout;
2645
Jackie Liua4c0b3d2019-07-08 13:41:12 +08002646 complete(&ctx->sqo_thread_started);
2647
Jens Axboe6c271ce2019-01-10 11:22:30 -07002648 old_fs = get_fs();
2649 set_fs(USER_DS);
2650
2651 timeout = inflight = 0;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02002652 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002653 bool mm_fault = false;
2654 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002655
2656 if (inflight) {
2657 unsigned nr_events = 0;
2658
2659 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07002660 io_iopoll_check(ctx, &nr_events, 0);
Jens Axboe6c271ce2019-01-10 11:22:30 -07002661 } else {
2662 /*
2663 * Normal IO, just pretend everything completed.
2664 * We don't have to poll completions for that.
2665 */
2666 nr_events = inflight;
2667 }
2668
2669 inflight -= nr_events;
2670 if (!inflight)
2671 timeout = jiffies + ctx->sq_thread_idle;
2672 }
2673
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002674 to_submit = io_sqring_entries(ctx);
2675 if (!to_submit) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07002676 /*
2677 * We're polling. If we're within the defined idle
2678 * period, then let us spin without work before going
2679 * to sleep.
2680 */
2681 if (inflight || !time_after(jiffies, timeout)) {
Jens Axboe9831a902019-09-19 09:48:55 -06002682 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07002683 continue;
2684 }
2685
2686 /*
2687 * Drop cur_mm before scheduling, we can't hold it for
2688 * long periods (or over schedule()). Do this before
2689 * adding ourselves to the waitqueue, as the unuse/drop
2690 * may sleep.
2691 */
2692 if (cur_mm) {
2693 unuse_mm(cur_mm);
2694 mmput(cur_mm);
2695 cur_mm = NULL;
2696 }
2697
2698 prepare_to_wait(&ctx->sqo_wait, &wait,
2699 TASK_INTERRUPTIBLE);
2700
2701 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00002702 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02002703 /* make sure to read SQ tail after writing flags */
2704 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07002705
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002706 to_submit = io_sqring_entries(ctx);
2707 if (!to_submit) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02002708 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07002709 finish_wait(&ctx->sqo_wait, &wait);
2710 break;
2711 }
2712 if (signal_pending(current))
2713 flush_signals(current);
2714 schedule();
2715 finish_wait(&ctx->sqo_wait, &wait);
2716
Hristo Venev75b28af2019-08-26 17:23:46 +00002717 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002718 continue;
2719 }
2720 finish_wait(&ctx->sqo_wait, &wait);
2721
Hristo Venev75b28af2019-08-26 17:23:46 +00002722 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002723 }
2724
Jens Axboe6c271ce2019-01-10 11:22:30 -07002725 /* Unless all new commands are FIXED regions, grab mm */
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002726 if (!cur_mm) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07002727 mm_fault = !mmget_not_zero(ctx->sqo_mm);
2728 if (!mm_fault) {
2729 use_mm(ctx->sqo_mm);
2730 cur_mm = ctx->sqo_mm;
2731 }
2732 }
2733
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002734 to_submit = min(to_submit, ctx->sq_entries);
2735 inflight += io_submit_sqes(ctx, to_submit, cur_mm != NULL,
2736 mm_fault);
Jens Axboe6c271ce2019-01-10 11:22:30 -07002737
2738 /* Commit SQ ring head once we've consumed all SQEs */
2739 io_commit_sqring(ctx);
2740 }
2741
2742 set_fs(old_fs);
2743 if (cur_mm) {
2744 unuse_mm(cur_mm);
2745 mmput(cur_mm);
2746 }
Jens Axboe06058632019-04-13 09:26:03 -06002747
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02002748 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06002749
Jens Axboe6c271ce2019-01-10 11:22:30 -07002750 return 0;
2751}
2752
Jens Axboebc808bc2019-10-22 13:14:37 -06002753static int io_ring_submit(struct io_ring_ctx *ctx, unsigned int to_submit)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002754{
Jens Axboe9a56a232019-01-09 09:06:50 -07002755 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002756 struct io_kiocb *link = NULL;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002757 struct io_kiocb *shadow_req = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002758 bool prev_was_link = false;
Jens Axboe5c8b0b52019-04-30 10:16:07 -06002759 int i, submit = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002760
Jens Axboe9a56a232019-01-09 09:06:50 -07002761 if (to_submit > IO_PLUG_THRESHOLD) {
2762 io_submit_state_start(&state, ctx, to_submit);
2763 statep = &state;
2764 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002765
2766 for (i = 0; i < to_submit; i++) {
2767 struct sqe_submit s;
2768
2769 if (!io_get_sqring(ctx, &s))
2770 break;
2771
Jens Axboe9e645e112019-05-10 16:07:28 -06002772 /*
2773 * If previous wasn't linked and we have a linked command,
2774 * that's the end of the chain. Submit the previous link.
2775 */
2776 if (!prev_was_link && link) {
Jens Axboebc808bc2019-10-22 13:14:37 -06002777 io_queue_link_head(ctx, link, &link->submit, shadow_req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002778 link = NULL;
Jackie Liu5f5ad9c2019-09-18 10:37:53 +08002779 shadow_req = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002780 }
2781 prev_was_link = (s.sqe->flags & IOSQE_IO_LINK) != 0;
2782
Jackie Liu4fe2c962019-09-09 20:50:40 +08002783 if (link && (s.sqe->flags & IOSQE_IO_DRAIN)) {
2784 if (!shadow_req) {
2785 shadow_req = io_get_req(ctx, NULL);
Jackie Liua1041c22019-09-18 17:25:52 +08002786 if (unlikely(!shadow_req))
2787 goto out;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002788 shadow_req->flags |= (REQ_F_IO_DRAIN | REQ_F_SHADOW_DRAIN);
2789 refcount_dec(&shadow_req->refs);
2790 }
2791 shadow_req->sequence = s.sequence;
2792 }
2793
Jackie Liua1041c22019-09-18 17:25:52 +08002794out:
Jens Axboe2b188cc2019-01-07 10:46:33 -07002795 s.has_user = true;
Jens Axboedef596e2019-01-09 08:59:42 -07002796 s.needs_lock = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002797 s.needs_fixed_file = false;
Jens Axboe5c8b0b52019-04-30 10:16:07 -06002798 submit++;
Jens Axboebc808bc2019-10-22 13:14:37 -06002799 io_submit_sqe(ctx, &s, statep, &link);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002800 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002801
Jens Axboe9e645e112019-05-10 16:07:28 -06002802 if (link)
Jens Axboebc808bc2019-10-22 13:14:37 -06002803 io_queue_link_head(ctx, link, &link->submit, shadow_req);
Jens Axboe9a56a232019-01-09 09:06:50 -07002804 if (statep)
2805 io_submit_state_end(statep);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002806
Pavel Begunkov935d1e42019-10-25 12:31:31 +03002807 io_commit_sqring(ctx);
2808
Jens Axboe5c8b0b52019-04-30 10:16:07 -06002809 return submit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002810}
2811
Jens Axboebda52162019-09-24 13:47:15 -06002812struct io_wait_queue {
2813 struct wait_queue_entry wq;
2814 struct io_ring_ctx *ctx;
2815 unsigned to_wait;
2816 unsigned nr_timeouts;
2817};
2818
2819static inline bool io_should_wake(struct io_wait_queue *iowq)
2820{
2821 struct io_ring_ctx *ctx = iowq->ctx;
2822
2823 /*
2824 * Wake up if we have enough events, or if a timeout occured since we
2825 * started waiting. For timeouts, we always want to return to userspace,
2826 * regardless of event count.
2827 */
2828 return io_cqring_events(ctx->rings) >= iowq->to_wait ||
2829 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
2830}
2831
2832static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
2833 int wake_flags, void *key)
2834{
2835 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
2836 wq);
2837
2838 if (!io_should_wake(iowq))
2839 return -1;
2840
2841 return autoremove_wake_function(curr, mode, wake_flags, key);
2842}
2843
Jens Axboe2b188cc2019-01-07 10:46:33 -07002844/*
2845 * Wait until events become available, if we don't already have some. The
2846 * application must reap them itself, as they reside on the shared cq ring.
2847 */
2848static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
2849 const sigset_t __user *sig, size_t sigsz)
2850{
Jens Axboebda52162019-09-24 13:47:15 -06002851 struct io_wait_queue iowq = {
2852 .wq = {
2853 .private = current,
2854 .func = io_wake_function,
2855 .entry = LIST_HEAD_INIT(iowq.wq.entry),
2856 },
2857 .ctx = ctx,
2858 .to_wait = min_events,
2859 };
Hristo Venev75b28af2019-08-26 17:23:46 +00002860 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002861 int ret;
2862
Hristo Venev75b28af2019-08-26 17:23:46 +00002863 if (io_cqring_events(rings) >= min_events)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002864 return 0;
2865
2866 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01002867#ifdef CONFIG_COMPAT
2868 if (in_compat_syscall())
2869 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07002870 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01002871 else
2872#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07002873 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01002874
Jens Axboe2b188cc2019-01-07 10:46:33 -07002875 if (ret)
2876 return ret;
2877 }
2878
Jens Axboebda52162019-09-24 13:47:15 -06002879 ret = 0;
2880 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
2881 do {
2882 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
2883 TASK_INTERRUPTIBLE);
2884 if (io_should_wake(&iowq))
2885 break;
2886 schedule();
2887 if (signal_pending(current)) {
2888 ret = -ERESTARTSYS;
2889 break;
2890 }
2891 } while (1);
2892 finish_wait(&ctx->wait, &iowq.wq);
2893
Oleg Nesterovb7724342019-07-16 16:29:53 -07002894 restore_saved_sigmask_unless(ret == -ERESTARTSYS);
Oleg Nesterov97abc882019-06-28 12:06:50 -07002895 if (ret == -ERESTARTSYS)
2896 ret = -EINTR;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002897
Hristo Venev75b28af2019-08-26 17:23:46 +00002898 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002899}
2900
Jens Axboe6b063142019-01-10 22:13:58 -07002901static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
2902{
2903#if defined(CONFIG_UNIX)
2904 if (ctx->ring_sock) {
2905 struct sock *sock = ctx->ring_sock->sk;
2906 struct sk_buff *skb;
2907
2908 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
2909 kfree_skb(skb);
2910 }
2911#else
2912 int i;
2913
2914 for (i = 0; i < ctx->nr_user_files; i++)
2915 fput(ctx->user_files[i]);
2916#endif
2917}
2918
2919static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
2920{
2921 if (!ctx->user_files)
2922 return -ENXIO;
2923
2924 __io_sqe_files_unregister(ctx);
2925 kfree(ctx->user_files);
2926 ctx->user_files = NULL;
2927 ctx->nr_user_files = 0;
2928 return 0;
2929}
2930
Jens Axboe6c271ce2019-01-10 11:22:30 -07002931static void io_sq_thread_stop(struct io_ring_ctx *ctx)
2932{
2933 if (ctx->sqo_thread) {
Jackie Liua4c0b3d2019-07-08 13:41:12 +08002934 wait_for_completion(&ctx->sqo_thread_started);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02002935 /*
2936 * The park is a bit of a work-around, without it we get
2937 * warning spews on shutdown with SQPOLL set and affinity
2938 * set to a single CPU.
2939 */
Jens Axboe06058632019-04-13 09:26:03 -06002940 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07002941 kthread_stop(ctx->sqo_thread);
2942 ctx->sqo_thread = NULL;
2943 }
2944}
2945
Jens Axboe6b063142019-01-10 22:13:58 -07002946static void io_finish_async(struct io_ring_ctx *ctx)
2947{
Jens Axboe54a91f32019-09-10 09:15:04 -06002948 int i;
2949
Jens Axboe6c271ce2019-01-10 11:22:30 -07002950 io_sq_thread_stop(ctx);
2951
Jens Axboe54a91f32019-09-10 09:15:04 -06002952 for (i = 0; i < ARRAY_SIZE(ctx->sqo_wq); i++) {
2953 if (ctx->sqo_wq[i]) {
2954 destroy_workqueue(ctx->sqo_wq[i]);
2955 ctx->sqo_wq[i] = NULL;
2956 }
Jens Axboe6b063142019-01-10 22:13:58 -07002957 }
2958}
2959
2960#if defined(CONFIG_UNIX)
2961static void io_destruct_skb(struct sk_buff *skb)
2962{
2963 struct io_ring_ctx *ctx = skb->sk->sk_user_data;
Jens Axboe8a997342019-10-09 14:40:13 -06002964 int i;
Jens Axboe6b063142019-01-10 22:13:58 -07002965
Jens Axboe8a997342019-10-09 14:40:13 -06002966 for (i = 0; i < ARRAY_SIZE(ctx->sqo_wq); i++)
2967 if (ctx->sqo_wq[i])
2968 flush_workqueue(ctx->sqo_wq[i]);
2969
Jens Axboe6b063142019-01-10 22:13:58 -07002970 unix_destruct_scm(skb);
2971}
2972
2973/*
2974 * Ensure the UNIX gc is aware of our file set, so we are certain that
2975 * the io_uring can be safely unregistered on process exit, even if we have
2976 * loops in the file referencing.
2977 */
2978static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
2979{
2980 struct sock *sk = ctx->ring_sock->sk;
2981 struct scm_fp_list *fpl;
2982 struct sk_buff *skb;
2983 int i;
2984
2985 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
2986 unsigned long inflight = ctx->user->unix_inflight + nr;
2987
2988 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
2989 return -EMFILE;
2990 }
2991
2992 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
2993 if (!fpl)
2994 return -ENOMEM;
2995
2996 skb = alloc_skb(0, GFP_KERNEL);
2997 if (!skb) {
2998 kfree(fpl);
2999 return -ENOMEM;
3000 }
3001
3002 skb->sk = sk;
3003 skb->destructor = io_destruct_skb;
3004
3005 fpl->user = get_uid(ctx->user);
3006 for (i = 0; i < nr; i++) {
3007 fpl->fp[i] = get_file(ctx->user_files[i + offset]);
3008 unix_inflight(fpl->user, fpl->fp[i]);
3009 }
3010
3011 fpl->max = fpl->count = nr;
3012 UNIXCB(skb).fp = fpl;
3013 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
3014 skb_queue_head(&sk->sk_receive_queue, skb);
3015
3016 for (i = 0; i < nr; i++)
3017 fput(fpl->fp[i]);
3018
3019 return 0;
3020}
3021
3022/*
3023 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
3024 * causes regular reference counting to break down. We rely on the UNIX
3025 * garbage collection to take care of this problem for us.
3026 */
3027static int io_sqe_files_scm(struct io_ring_ctx *ctx)
3028{
3029 unsigned left, total;
3030 int ret = 0;
3031
3032 total = 0;
3033 left = ctx->nr_user_files;
3034 while (left) {
3035 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07003036
3037 ret = __io_sqe_files_scm(ctx, this_files, total);
3038 if (ret)
3039 break;
3040 left -= this_files;
3041 total += this_files;
3042 }
3043
3044 if (!ret)
3045 return 0;
3046
3047 while (total < ctx->nr_user_files) {
3048 fput(ctx->user_files[total]);
3049 total++;
3050 }
3051
3052 return ret;
3053}
3054#else
3055static int io_sqe_files_scm(struct io_ring_ctx *ctx)
3056{
3057 return 0;
3058}
3059#endif
3060
3061static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
3062 unsigned nr_args)
3063{
3064 __s32 __user *fds = (__s32 __user *) arg;
3065 int fd, ret = 0;
3066 unsigned i;
3067
3068 if (ctx->user_files)
3069 return -EBUSY;
3070 if (!nr_args)
3071 return -EINVAL;
3072 if (nr_args > IORING_MAX_FIXED_FILES)
3073 return -EMFILE;
3074
3075 ctx->user_files = kcalloc(nr_args, sizeof(struct file *), GFP_KERNEL);
3076 if (!ctx->user_files)
3077 return -ENOMEM;
3078
3079 for (i = 0; i < nr_args; i++) {
3080 ret = -EFAULT;
3081 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
3082 break;
3083
3084 ctx->user_files[i] = fget(fd);
3085
3086 ret = -EBADF;
3087 if (!ctx->user_files[i])
3088 break;
3089 /*
3090 * Don't allow io_uring instances to be registered. If UNIX
3091 * isn't enabled, then this causes a reference cycle and this
3092 * instance can never get freed. If UNIX is enabled we'll
3093 * handle it just fine, but there's still no point in allowing
3094 * a ring fd as it doesn't support regular read/write anyway.
3095 */
3096 if (ctx->user_files[i]->f_op == &io_uring_fops) {
3097 fput(ctx->user_files[i]);
3098 break;
3099 }
3100 ctx->nr_user_files++;
3101 ret = 0;
3102 }
3103
3104 if (ret) {
3105 for (i = 0; i < ctx->nr_user_files; i++)
3106 fput(ctx->user_files[i]);
3107
3108 kfree(ctx->user_files);
Jens Axboe25adf502019-04-03 09:52:40 -06003109 ctx->user_files = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003110 ctx->nr_user_files = 0;
3111 return ret;
3112 }
3113
3114 ret = io_sqe_files_scm(ctx);
3115 if (ret)
3116 io_sqe_files_unregister(ctx);
3117
3118 return ret;
3119}
3120
Jens Axboe6c271ce2019-01-10 11:22:30 -07003121static int io_sq_offload_start(struct io_ring_ctx *ctx,
3122 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003123{
3124 int ret;
3125
Jens Axboe6c271ce2019-01-10 11:22:30 -07003126 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003127 mmgrab(current->mm);
3128 ctx->sqo_mm = current->mm;
3129
Jens Axboe6c271ce2019-01-10 11:22:30 -07003130 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06003131 ret = -EPERM;
3132 if (!capable(CAP_SYS_ADMIN))
3133 goto err;
3134
Jens Axboe917257d2019-04-13 09:28:55 -06003135 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
3136 if (!ctx->sq_thread_idle)
3137 ctx->sq_thread_idle = HZ;
3138
Jens Axboe6c271ce2019-01-10 11:22:30 -07003139 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06003140 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003141
Jens Axboe917257d2019-04-13 09:28:55 -06003142 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06003143 if (cpu >= nr_cpu_ids)
3144 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08003145 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06003146 goto err;
3147
Jens Axboe6c271ce2019-01-10 11:22:30 -07003148 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
3149 ctx, cpu,
3150 "io_uring-sq");
3151 } else {
3152 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
3153 "io_uring-sq");
3154 }
3155 if (IS_ERR(ctx->sqo_thread)) {
3156 ret = PTR_ERR(ctx->sqo_thread);
3157 ctx->sqo_thread = NULL;
3158 goto err;
3159 }
3160 wake_up_process(ctx->sqo_thread);
3161 } else if (p->flags & IORING_SETUP_SQ_AFF) {
3162 /* Can't have SQ_AFF without SQPOLL */
3163 ret = -EINVAL;
3164 goto err;
3165 }
3166
Jens Axboe2b188cc2019-01-07 10:46:33 -07003167 /* Do QD, or 2 * CPUS, whatever is smallest */
Jens Axboe54a91f32019-09-10 09:15:04 -06003168 ctx->sqo_wq[0] = alloc_workqueue("io_ring-wq",
3169 WQ_UNBOUND | WQ_FREEZABLE,
Jens Axboe2b188cc2019-01-07 10:46:33 -07003170 min(ctx->sq_entries - 1, 2 * num_online_cpus()));
Jens Axboe54a91f32019-09-10 09:15:04 -06003171 if (!ctx->sqo_wq[0]) {
3172 ret = -ENOMEM;
3173 goto err;
3174 }
3175
3176 /*
3177 * This is for buffered writes, where we want to limit the parallelism
3178 * due to file locking in file systems. As "normal" buffered writes
3179 * should parellelize on writeout quite nicely, limit us to having 2
3180 * pending. This avoids massive contention on the inode when doing
3181 * buffered async writes.
3182 */
3183 ctx->sqo_wq[1] = alloc_workqueue("io_ring-write-wq",
3184 WQ_UNBOUND | WQ_FREEZABLE, 2);
3185 if (!ctx->sqo_wq[1]) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07003186 ret = -ENOMEM;
3187 goto err;
3188 }
3189
3190 return 0;
3191err:
Jens Axboe54a91f32019-09-10 09:15:04 -06003192 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003193 mmdrop(ctx->sqo_mm);
3194 ctx->sqo_mm = NULL;
3195 return ret;
3196}
3197
3198static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
3199{
3200 atomic_long_sub(nr_pages, &user->locked_vm);
3201}
3202
3203static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
3204{
3205 unsigned long page_limit, cur_pages, new_pages;
3206
3207 /* Don't allow more pages than we can safely lock */
3208 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
3209
3210 do {
3211 cur_pages = atomic_long_read(&user->locked_vm);
3212 new_pages = cur_pages + nr_pages;
3213 if (new_pages > page_limit)
3214 return -ENOMEM;
3215 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
3216 new_pages) != cur_pages);
3217
3218 return 0;
3219}
3220
3221static void io_mem_free(void *ptr)
3222{
Mark Rutland52e04ef2019-04-30 17:30:21 +01003223 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003224
Mark Rutland52e04ef2019-04-30 17:30:21 +01003225 if (!ptr)
3226 return;
3227
3228 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003229 if (put_page_testzero(page))
3230 free_compound_page(page);
3231}
3232
3233static void *io_mem_alloc(size_t size)
3234{
3235 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
3236 __GFP_NORETRY;
3237
3238 return (void *) __get_free_pages(gfp_flags, get_order(size));
3239}
3240
Hristo Venev75b28af2019-08-26 17:23:46 +00003241static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
3242 size_t *sq_offset)
3243{
3244 struct io_rings *rings;
3245 size_t off, sq_array_size;
3246
3247 off = struct_size(rings, cqes, cq_entries);
3248 if (off == SIZE_MAX)
3249 return SIZE_MAX;
3250
3251#ifdef CONFIG_SMP
3252 off = ALIGN(off, SMP_CACHE_BYTES);
3253 if (off == 0)
3254 return SIZE_MAX;
3255#endif
3256
3257 sq_array_size = array_size(sizeof(u32), sq_entries);
3258 if (sq_array_size == SIZE_MAX)
3259 return SIZE_MAX;
3260
3261 if (check_add_overflow(off, sq_array_size, &off))
3262 return SIZE_MAX;
3263
3264 if (sq_offset)
3265 *sq_offset = off;
3266
3267 return off;
3268}
3269
Jens Axboe2b188cc2019-01-07 10:46:33 -07003270static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
3271{
Hristo Venev75b28af2019-08-26 17:23:46 +00003272 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003273
Hristo Venev75b28af2019-08-26 17:23:46 +00003274 pages = (size_t)1 << get_order(
3275 rings_size(sq_entries, cq_entries, NULL));
3276 pages += (size_t)1 << get_order(
3277 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07003278
Hristo Venev75b28af2019-08-26 17:23:46 +00003279 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003280}
3281
Jens Axboeedafcce2019-01-09 09:16:05 -07003282static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
3283{
3284 int i, j;
3285
3286 if (!ctx->user_bufs)
3287 return -ENXIO;
3288
3289 for (i = 0; i < ctx->nr_user_bufs; i++) {
3290 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
3291
3292 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbard27c4d3a2019-08-04 19:32:06 -07003293 put_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07003294
3295 if (ctx->account_mem)
3296 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003297 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07003298 imu->nr_bvecs = 0;
3299 }
3300
3301 kfree(ctx->user_bufs);
3302 ctx->user_bufs = NULL;
3303 ctx->nr_user_bufs = 0;
3304 return 0;
3305}
3306
3307static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
3308 void __user *arg, unsigned index)
3309{
3310 struct iovec __user *src;
3311
3312#ifdef CONFIG_COMPAT
3313 if (ctx->compat) {
3314 struct compat_iovec __user *ciovs;
3315 struct compat_iovec ciov;
3316
3317 ciovs = (struct compat_iovec __user *) arg;
3318 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
3319 return -EFAULT;
3320
3321 dst->iov_base = (void __user *) (unsigned long) ciov.iov_base;
3322 dst->iov_len = ciov.iov_len;
3323 return 0;
3324 }
3325#endif
3326 src = (struct iovec __user *) arg;
3327 if (copy_from_user(dst, &src[index], sizeof(*dst)))
3328 return -EFAULT;
3329 return 0;
3330}
3331
3332static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
3333 unsigned nr_args)
3334{
3335 struct vm_area_struct **vmas = NULL;
3336 struct page **pages = NULL;
3337 int i, j, got_pages = 0;
3338 int ret = -EINVAL;
3339
3340 if (ctx->user_bufs)
3341 return -EBUSY;
3342 if (!nr_args || nr_args > UIO_MAXIOV)
3343 return -EINVAL;
3344
3345 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
3346 GFP_KERNEL);
3347 if (!ctx->user_bufs)
3348 return -ENOMEM;
3349
3350 for (i = 0; i < nr_args; i++) {
3351 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
3352 unsigned long off, start, end, ubuf;
3353 int pret, nr_pages;
3354 struct iovec iov;
3355 size_t size;
3356
3357 ret = io_copy_iov(ctx, &iov, arg, i);
3358 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03003359 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07003360
3361 /*
3362 * Don't impose further limits on the size and buffer
3363 * constraints here, we'll -EINVAL later when IO is
3364 * submitted if they are wrong.
3365 */
3366 ret = -EFAULT;
3367 if (!iov.iov_base || !iov.iov_len)
3368 goto err;
3369
3370 /* arbitrary limit, but we need something */
3371 if (iov.iov_len > SZ_1G)
3372 goto err;
3373
3374 ubuf = (unsigned long) iov.iov_base;
3375 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
3376 start = ubuf >> PAGE_SHIFT;
3377 nr_pages = end - start;
3378
3379 if (ctx->account_mem) {
3380 ret = io_account_mem(ctx->user, nr_pages);
3381 if (ret)
3382 goto err;
3383 }
3384
3385 ret = 0;
3386 if (!pages || nr_pages > got_pages) {
3387 kfree(vmas);
3388 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003389 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07003390 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003391 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07003392 sizeof(struct vm_area_struct *),
3393 GFP_KERNEL);
3394 if (!pages || !vmas) {
3395 ret = -ENOMEM;
3396 if (ctx->account_mem)
3397 io_unaccount_mem(ctx->user, nr_pages);
3398 goto err;
3399 }
3400 got_pages = nr_pages;
3401 }
3402
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003403 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07003404 GFP_KERNEL);
3405 ret = -ENOMEM;
3406 if (!imu->bvec) {
3407 if (ctx->account_mem)
3408 io_unaccount_mem(ctx->user, nr_pages);
3409 goto err;
3410 }
3411
3412 ret = 0;
3413 down_read(&current->mm->mmap_sem);
Ira Weiny932f4a62019-05-13 17:17:03 -07003414 pret = get_user_pages(ubuf, nr_pages,
3415 FOLL_WRITE | FOLL_LONGTERM,
3416 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07003417 if (pret == nr_pages) {
3418 /* don't support file backed memory */
3419 for (j = 0; j < nr_pages; j++) {
3420 struct vm_area_struct *vma = vmas[j];
3421
3422 if (vma->vm_file &&
3423 !is_file_hugepages(vma->vm_file)) {
3424 ret = -EOPNOTSUPP;
3425 break;
3426 }
3427 }
3428 } else {
3429 ret = pret < 0 ? pret : -EFAULT;
3430 }
3431 up_read(&current->mm->mmap_sem);
3432 if (ret) {
3433 /*
3434 * if we did partial map, or found file backed vmas,
3435 * release any pages we did get
3436 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07003437 if (pret > 0)
3438 put_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07003439 if (ctx->account_mem)
3440 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003441 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07003442 goto err;
3443 }
3444
3445 off = ubuf & ~PAGE_MASK;
3446 size = iov.iov_len;
3447 for (j = 0; j < nr_pages; j++) {
3448 size_t vec_len;
3449
3450 vec_len = min_t(size_t, size, PAGE_SIZE - off);
3451 imu->bvec[j].bv_page = pages[j];
3452 imu->bvec[j].bv_len = vec_len;
3453 imu->bvec[j].bv_offset = off;
3454 off = 0;
3455 size -= vec_len;
3456 }
3457 /* store original address for later verification */
3458 imu->ubuf = ubuf;
3459 imu->len = iov.iov_len;
3460 imu->nr_bvecs = nr_pages;
3461
3462 ctx->nr_user_bufs++;
3463 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003464 kvfree(pages);
3465 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07003466 return 0;
3467err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003468 kvfree(pages);
3469 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07003470 io_sqe_buffer_unregister(ctx);
3471 return ret;
3472}
3473
Jens Axboe9b402842019-04-11 11:45:41 -06003474static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
3475{
3476 __s32 __user *fds = arg;
3477 int fd;
3478
3479 if (ctx->cq_ev_fd)
3480 return -EBUSY;
3481
3482 if (copy_from_user(&fd, fds, sizeof(*fds)))
3483 return -EFAULT;
3484
3485 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
3486 if (IS_ERR(ctx->cq_ev_fd)) {
3487 int ret = PTR_ERR(ctx->cq_ev_fd);
3488 ctx->cq_ev_fd = NULL;
3489 return ret;
3490 }
3491
3492 return 0;
3493}
3494
3495static int io_eventfd_unregister(struct io_ring_ctx *ctx)
3496{
3497 if (ctx->cq_ev_fd) {
3498 eventfd_ctx_put(ctx->cq_ev_fd);
3499 ctx->cq_ev_fd = NULL;
3500 return 0;
3501 }
3502
3503 return -ENXIO;
3504}
3505
Jens Axboe2b188cc2019-01-07 10:46:33 -07003506static void io_ring_ctx_free(struct io_ring_ctx *ctx)
3507{
Jens Axboe6b063142019-01-10 22:13:58 -07003508 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003509 if (ctx->sqo_mm)
3510 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07003511
3512 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07003513 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07003514 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06003515 io_eventfd_unregister(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07003516
Jens Axboe2b188cc2019-01-07 10:46:33 -07003517#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07003518 if (ctx->ring_sock) {
3519 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07003520 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07003521 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003522#endif
3523
Hristo Venev75b28af2019-08-26 17:23:46 +00003524 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003525 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003526
3527 percpu_ref_exit(&ctx->refs);
3528 if (ctx->account_mem)
3529 io_unaccount_mem(ctx->user,
3530 ring_pages(ctx->sq_entries, ctx->cq_entries));
3531 free_uid(ctx->user);
3532 kfree(ctx);
3533}
3534
3535static __poll_t io_uring_poll(struct file *file, poll_table *wait)
3536{
3537 struct io_ring_ctx *ctx = file->private_data;
3538 __poll_t mask = 0;
3539
3540 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02003541 /*
3542 * synchronizes with barrier from wq_has_sleeper call in
3543 * io_commit_cqring
3544 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07003545 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00003546 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
3547 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003548 mask |= EPOLLOUT | EPOLLWRNORM;
yangerkundaa5de52019-09-24 20:53:34 +08003549 if (READ_ONCE(ctx->rings->cq.head) != ctx->cached_cq_tail)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003550 mask |= EPOLLIN | EPOLLRDNORM;
3551
3552 return mask;
3553}
3554
3555static int io_uring_fasync(int fd, struct file *file, int on)
3556{
3557 struct io_ring_ctx *ctx = file->private_data;
3558
3559 return fasync_helper(fd, file, on, &ctx->cq_fasync);
3560}
3561
3562static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
3563{
3564 mutex_lock(&ctx->uring_lock);
3565 percpu_ref_kill(&ctx->refs);
3566 mutex_unlock(&ctx->uring_lock);
3567
Jens Axboe5262f562019-09-17 12:26:57 -06003568 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003569 io_poll_remove_all(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07003570 io_iopoll_reap_events(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003571 wait_for_completion(&ctx->ctx_done);
3572 io_ring_ctx_free(ctx);
3573}
3574
3575static int io_uring_release(struct inode *inode, struct file *file)
3576{
3577 struct io_ring_ctx *ctx = file->private_data;
3578
3579 file->private_data = NULL;
3580 io_ring_ctx_wait_and_kill(ctx);
3581 return 0;
3582}
3583
3584static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
3585{
3586 loff_t offset = (loff_t) vma->vm_pgoff << PAGE_SHIFT;
3587 unsigned long sz = vma->vm_end - vma->vm_start;
3588 struct io_ring_ctx *ctx = file->private_data;
3589 unsigned long pfn;
3590 struct page *page;
3591 void *ptr;
3592
3593 switch (offset) {
3594 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00003595 case IORING_OFF_CQ_RING:
3596 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003597 break;
3598 case IORING_OFF_SQES:
3599 ptr = ctx->sq_sqes;
3600 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003601 default:
3602 return -EINVAL;
3603 }
3604
3605 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07003606 if (sz > page_size(page))
Jens Axboe2b188cc2019-01-07 10:46:33 -07003607 return -EINVAL;
3608
3609 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
3610 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
3611}
3612
3613SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
3614 u32, min_complete, u32, flags, const sigset_t __user *, sig,
3615 size_t, sigsz)
3616{
3617 struct io_ring_ctx *ctx;
3618 long ret = -EBADF;
3619 int submitted = 0;
3620 struct fd f;
3621
Jens Axboe6c271ce2019-01-10 11:22:30 -07003622 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07003623 return -EINVAL;
3624
3625 f = fdget(fd);
3626 if (!f.file)
3627 return -EBADF;
3628
3629 ret = -EOPNOTSUPP;
3630 if (f.file->f_op != &io_uring_fops)
3631 goto out_fput;
3632
3633 ret = -ENXIO;
3634 ctx = f.file->private_data;
3635 if (!percpu_ref_tryget(&ctx->refs))
3636 goto out_fput;
3637
Jens Axboe6c271ce2019-01-10 11:22:30 -07003638 /*
3639 * For SQ polling, the thread will do all submissions and completions.
3640 * Just return the requested submit count, and wake the thread if
3641 * we were asked to.
3642 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06003643 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003644 if (ctx->flags & IORING_SETUP_SQPOLL) {
3645 if (flags & IORING_ENTER_SQ_WAKEUP)
3646 wake_up(&ctx->sqo_wait);
3647 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06003648 } else if (to_submit) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07003649 to_submit = min(to_submit, ctx->sq_entries);
3650
3651 mutex_lock(&ctx->uring_lock);
Jens Axboebc808bc2019-10-22 13:14:37 -06003652 submitted = io_ring_submit(ctx, to_submit);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003653 mutex_unlock(&ctx->uring_lock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003654 }
3655 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07003656 unsigned nr_events = 0;
3657
Jens Axboe2b188cc2019-01-07 10:46:33 -07003658 min_complete = min(min_complete, ctx->cq_entries);
3659
Jens Axboedef596e2019-01-09 08:59:42 -07003660 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07003661 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07003662 } else {
3663 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
3664 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003665 }
3666
Pavel Begunkov6805b322019-10-08 02:18:42 +03003667 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003668out_fput:
3669 fdput(f);
3670 return submitted ? submitted : ret;
3671}
3672
3673static const struct file_operations io_uring_fops = {
3674 .release = io_uring_release,
3675 .mmap = io_uring_mmap,
3676 .poll = io_uring_poll,
3677 .fasync = io_uring_fasync,
3678};
3679
3680static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
3681 struct io_uring_params *p)
3682{
Hristo Venev75b28af2019-08-26 17:23:46 +00003683 struct io_rings *rings;
3684 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003685
Hristo Venev75b28af2019-08-26 17:23:46 +00003686 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
3687 if (size == SIZE_MAX)
3688 return -EOVERFLOW;
3689
3690 rings = io_mem_alloc(size);
3691 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003692 return -ENOMEM;
3693
Hristo Venev75b28af2019-08-26 17:23:46 +00003694 ctx->rings = rings;
3695 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
3696 rings->sq_ring_mask = p->sq_entries - 1;
3697 rings->cq_ring_mask = p->cq_entries - 1;
3698 rings->sq_ring_entries = p->sq_entries;
3699 rings->cq_ring_entries = p->cq_entries;
3700 ctx->sq_mask = rings->sq_ring_mask;
3701 ctx->cq_mask = rings->cq_ring_mask;
3702 ctx->sq_entries = rings->sq_ring_entries;
3703 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003704
3705 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
3706 if (size == SIZE_MAX)
3707 return -EOVERFLOW;
3708
3709 ctx->sq_sqes = io_mem_alloc(size);
Mark Rutland52e04ef2019-04-30 17:30:21 +01003710 if (!ctx->sq_sqes)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003711 return -ENOMEM;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003712
Jens Axboe2b188cc2019-01-07 10:46:33 -07003713 return 0;
3714}
3715
3716/*
3717 * Allocate an anonymous fd, this is what constitutes the application
3718 * visible backing of an io_uring instance. The application mmaps this
3719 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
3720 * we have to tie this fd to a socket for file garbage collection purposes.
3721 */
3722static int io_uring_get_fd(struct io_ring_ctx *ctx)
3723{
3724 struct file *file;
3725 int ret;
3726
3727#if defined(CONFIG_UNIX)
3728 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
3729 &ctx->ring_sock);
3730 if (ret)
3731 return ret;
3732#endif
3733
3734 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
3735 if (ret < 0)
3736 goto err;
3737
3738 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
3739 O_RDWR | O_CLOEXEC);
3740 if (IS_ERR(file)) {
3741 put_unused_fd(ret);
3742 ret = PTR_ERR(file);
3743 goto err;
3744 }
3745
3746#if defined(CONFIG_UNIX)
3747 ctx->ring_sock->file = file;
Jens Axboe6b063142019-01-10 22:13:58 -07003748 ctx->ring_sock->sk->sk_user_data = ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003749#endif
3750 fd_install(ret, file);
3751 return ret;
3752err:
3753#if defined(CONFIG_UNIX)
3754 sock_release(ctx->ring_sock);
3755 ctx->ring_sock = NULL;
3756#endif
3757 return ret;
3758}
3759
3760static int io_uring_create(unsigned entries, struct io_uring_params *p)
3761{
3762 struct user_struct *user = NULL;
3763 struct io_ring_ctx *ctx;
3764 bool account_mem;
3765 int ret;
3766
3767 if (!entries || entries > IORING_MAX_ENTRIES)
3768 return -EINVAL;
3769
3770 /*
3771 * Use twice as many entries for the CQ ring. It's possible for the
3772 * application to drive a higher depth than the size of the SQ ring,
3773 * since the sqes are only used at submission time. This allows for
3774 * some flexibility in overcommitting a bit.
3775 */
3776 p->sq_entries = roundup_pow_of_two(entries);
3777 p->cq_entries = 2 * p->sq_entries;
3778
3779 user = get_uid(current_user());
3780 account_mem = !capable(CAP_IPC_LOCK);
3781
3782 if (account_mem) {
3783 ret = io_account_mem(user,
3784 ring_pages(p->sq_entries, p->cq_entries));
3785 if (ret) {
3786 free_uid(user);
3787 return ret;
3788 }
3789 }
3790
3791 ctx = io_ring_ctx_alloc(p);
3792 if (!ctx) {
3793 if (account_mem)
3794 io_unaccount_mem(user, ring_pages(p->sq_entries,
3795 p->cq_entries));
3796 free_uid(user);
3797 return -ENOMEM;
3798 }
3799 ctx->compat = in_compat_syscall();
3800 ctx->account_mem = account_mem;
3801 ctx->user = user;
3802
3803 ret = io_allocate_scq_urings(ctx, p);
3804 if (ret)
3805 goto err;
3806
Jens Axboe6c271ce2019-01-10 11:22:30 -07003807 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003808 if (ret)
3809 goto err;
3810
3811 ret = io_uring_get_fd(ctx);
3812 if (ret < 0)
3813 goto err;
3814
3815 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00003816 p->sq_off.head = offsetof(struct io_rings, sq.head);
3817 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
3818 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
3819 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
3820 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
3821 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
3822 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003823
3824 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00003825 p->cq_off.head = offsetof(struct io_rings, cq.head);
3826 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
3827 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
3828 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
3829 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
3830 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06003831
3832 p->features = IORING_FEAT_SINGLE_MMAP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003833 return ret;
3834err:
3835 io_ring_ctx_wait_and_kill(ctx);
3836 return ret;
3837}
3838
3839/*
3840 * Sets up an aio uring context, and returns the fd. Applications asks for a
3841 * ring size, we return the actual sq/cq ring sizes (among other things) in the
3842 * params structure passed in.
3843 */
3844static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
3845{
3846 struct io_uring_params p;
3847 long ret;
3848 int i;
3849
3850 if (copy_from_user(&p, params, sizeof(p)))
3851 return -EFAULT;
3852 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
3853 if (p.resv[i])
3854 return -EINVAL;
3855 }
3856
Jens Axboe6c271ce2019-01-10 11:22:30 -07003857 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
3858 IORING_SETUP_SQ_AFF))
Jens Axboe2b188cc2019-01-07 10:46:33 -07003859 return -EINVAL;
3860
3861 ret = io_uring_create(entries, &p);
3862 if (ret < 0)
3863 return ret;
3864
3865 if (copy_to_user(params, &p, sizeof(p)))
3866 return -EFAULT;
3867
3868 return ret;
3869}
3870
3871SYSCALL_DEFINE2(io_uring_setup, u32, entries,
3872 struct io_uring_params __user *, params)
3873{
3874 return io_uring_setup(entries, params);
3875}
3876
Jens Axboeedafcce2019-01-09 09:16:05 -07003877static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
3878 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06003879 __releases(ctx->uring_lock)
3880 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07003881{
3882 int ret;
3883
Jens Axboe35fa71a2019-04-22 10:23:23 -06003884 /*
3885 * We're inside the ring mutex, if the ref is already dying, then
3886 * someone else killed the ctx or is already going through
3887 * io_uring_register().
3888 */
3889 if (percpu_ref_is_dying(&ctx->refs))
3890 return -ENXIO;
3891
Jens Axboeedafcce2019-01-09 09:16:05 -07003892 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06003893
3894 /*
3895 * Drop uring mutex before waiting for references to exit. If another
3896 * thread is currently inside io_uring_enter() it might need to grab
3897 * the uring_lock to make progress. If we hold it here across the drain
3898 * wait, then we can deadlock. It's safe to drop the mutex here, since
3899 * no new references will come in after we've killed the percpu ref.
3900 */
3901 mutex_unlock(&ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07003902 wait_for_completion(&ctx->ctx_done);
Jens Axboeb19062a2019-04-15 10:49:38 -06003903 mutex_lock(&ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07003904
3905 switch (opcode) {
3906 case IORING_REGISTER_BUFFERS:
3907 ret = io_sqe_buffer_register(ctx, arg, nr_args);
3908 break;
3909 case IORING_UNREGISTER_BUFFERS:
3910 ret = -EINVAL;
3911 if (arg || nr_args)
3912 break;
3913 ret = io_sqe_buffer_unregister(ctx);
3914 break;
Jens Axboe6b063142019-01-10 22:13:58 -07003915 case IORING_REGISTER_FILES:
3916 ret = io_sqe_files_register(ctx, arg, nr_args);
3917 break;
3918 case IORING_UNREGISTER_FILES:
3919 ret = -EINVAL;
3920 if (arg || nr_args)
3921 break;
3922 ret = io_sqe_files_unregister(ctx);
3923 break;
Jens Axboe9b402842019-04-11 11:45:41 -06003924 case IORING_REGISTER_EVENTFD:
3925 ret = -EINVAL;
3926 if (nr_args != 1)
3927 break;
3928 ret = io_eventfd_register(ctx, arg);
3929 break;
3930 case IORING_UNREGISTER_EVENTFD:
3931 ret = -EINVAL;
3932 if (arg || nr_args)
3933 break;
3934 ret = io_eventfd_unregister(ctx);
3935 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07003936 default:
3937 ret = -EINVAL;
3938 break;
3939 }
3940
3941 /* bring the ctx back to life */
3942 reinit_completion(&ctx->ctx_done);
3943 percpu_ref_reinit(&ctx->refs);
3944 return ret;
3945}
3946
3947SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
3948 void __user *, arg, unsigned int, nr_args)
3949{
3950 struct io_ring_ctx *ctx;
3951 long ret = -EBADF;
3952 struct fd f;
3953
3954 f = fdget(fd);
3955 if (!f.file)
3956 return -EBADF;
3957
3958 ret = -EOPNOTSUPP;
3959 if (f.file->f_op != &io_uring_fops)
3960 goto out_fput;
3961
3962 ctx = f.file->private_data;
3963
3964 mutex_lock(&ctx->uring_lock);
3965 ret = __io_uring_register(ctx, opcode, arg, nr_args);
3966 mutex_unlock(&ctx->uring_lock);
3967out_fput:
3968 fdput(f);
3969 return ret;
3970}
3971
Jens Axboe2b188cc2019-01-07 10:46:33 -07003972static int __init io_uring_init(void)
3973{
3974 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
3975 return 0;
3976};
3977__initcall(io_uring_init);