blob: 949c82a40d167a314dbe223cd5fe2dcc8a2d920d [file] [log] [blame]
Jens Axboe2b188cc2019-01-07 10:46:33 -07001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Shared application/kernel submission and completion ring pairs, for
4 * supporting fast/efficient IO.
5 *
6 * A note on the read/write ordering memory barriers that are matched between
Stefan Bühler1e84b972019-04-24 23:54:16 +02007 * the application and kernel side.
8 *
9 * After the application reads the CQ ring tail, it must use an
10 * appropriate smp_rmb() to pair with the smp_wmb() the kernel uses
11 * before writing the tail (using smp_load_acquire to read the tail will
12 * do). It also needs a smp_mb() before updating CQ head (ordering the
13 * entry load(s) with the head store), pairing with an implicit barrier
14 * through a control-dependency in io_get_cqring (smp_store_release to
15 * store head will do). Failure to do so could lead to reading invalid
16 * CQ entries.
17 *
18 * Likewise, the application must use an appropriate smp_wmb() before
19 * writing the SQ tail (ordering SQ entry stores with the tail store),
20 * which pairs with smp_load_acquire in io_get_sqring (smp_store_release
21 * to store the tail will do). And it needs a barrier ordering the SQ
22 * head load before writing new SQ entries (smp_load_acquire to read
23 * head will do).
24 *
25 * When using the SQ poll thread (IORING_SETUP_SQPOLL), the application
26 * needs to check the SQ flags for IORING_SQ_NEED_WAKEUP *after*
27 * updating the SQ tail; a full memory barrier smp_mb() is needed
28 * between.
Jens Axboe2b188cc2019-01-07 10:46:33 -070029 *
30 * Also see the examples in the liburing library:
31 *
32 * git://git.kernel.dk/liburing
33 *
34 * io_uring also uses READ/WRITE_ONCE() for _any_ store or load that happens
35 * from data shared between the kernel and application. This is done both
36 * for ordering purposes, but also to ensure that once a value is loaded from
37 * data that the application could potentially modify, it remains stable.
38 *
39 * Copyright (C) 2018-2019 Jens Axboe
Christoph Hellwigc992fe22019-01-11 09:43:02 -070040 * Copyright (c) 2018-2019 Christoph Hellwig
Jens Axboe2b188cc2019-01-07 10:46:33 -070041 */
42#include <linux/kernel.h>
43#include <linux/init.h>
44#include <linux/errno.h>
45#include <linux/syscalls.h>
46#include <linux/compat.h>
47#include <linux/refcount.h>
48#include <linux/uio.h>
49
50#include <linux/sched/signal.h>
51#include <linux/fs.h>
52#include <linux/file.h>
53#include <linux/fdtable.h>
54#include <linux/mm.h>
55#include <linux/mman.h>
56#include <linux/mmu_context.h>
57#include <linux/percpu.h>
58#include <linux/slab.h>
59#include <linux/workqueue.h>
Jens Axboe6c271ce2019-01-10 11:22:30 -070060#include <linux/kthread.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070061#include <linux/blkdev.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070062#include <linux/bvec.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070063#include <linux/net.h>
64#include <net/sock.h>
65#include <net/af_unix.h>
Jens Axboe6b063142019-01-10 22:13:58 -070066#include <net/scm.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070067#include <linux/anon_inodes.h>
68#include <linux/sched/mm.h>
69#include <linux/uaccess.h>
70#include <linux/nospec.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070071#include <linux/sizes.h>
72#include <linux/hugetlb.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070073
74#include <uapi/linux/io_uring.h>
75
76#include "internal.h"
77
Daniel Xu5277dea2019-09-14 14:23:45 -070078#define IORING_MAX_ENTRIES 32768
Jens Axboe6b063142019-01-10 22:13:58 -070079#define IORING_MAX_FIXED_FILES 1024
Jens Axboe2b188cc2019-01-07 10:46:33 -070080
81struct io_uring {
82 u32 head ____cacheline_aligned_in_smp;
83 u32 tail ____cacheline_aligned_in_smp;
84};
85
Stefan Bühler1e84b972019-04-24 23:54:16 +020086/*
Hristo Venev75b28af2019-08-26 17:23:46 +000087 * This data is shared with the application through the mmap at offsets
88 * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING.
Stefan Bühler1e84b972019-04-24 23:54:16 +020089 *
90 * The offsets to the member fields are published through struct
91 * io_sqring_offsets when calling io_uring_setup.
92 */
Hristo Venev75b28af2019-08-26 17:23:46 +000093struct io_rings {
Stefan Bühler1e84b972019-04-24 23:54:16 +020094 /*
95 * Head and tail offsets into the ring; the offsets need to be
96 * masked to get valid indices.
97 *
Hristo Venev75b28af2019-08-26 17:23:46 +000098 * The kernel controls head of the sq ring and the tail of the cq ring,
99 * and the application controls tail of the sq ring and the head of the
100 * cq ring.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200101 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000102 struct io_uring sq, cq;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200103 /*
Hristo Venev75b28af2019-08-26 17:23:46 +0000104 * Bitmasks to apply to head and tail offsets (constant, equals
Stefan Bühler1e84b972019-04-24 23:54:16 +0200105 * ring_entries - 1)
106 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000107 u32 sq_ring_mask, cq_ring_mask;
108 /* Ring sizes (constant, power of 2) */
109 u32 sq_ring_entries, cq_ring_entries;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200110 /*
111 * Number of invalid entries dropped by the kernel due to
112 * invalid index stored in array
113 *
114 * Written by the kernel, shouldn't be modified by the
115 * application (i.e. get number of "new events" by comparing to
116 * cached value).
117 *
118 * After a new SQ head value was read by the application this
119 * counter includes all submissions that were dropped reaching
120 * the new SQ head (and possibly more).
121 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000122 u32 sq_dropped;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200123 /*
124 * Runtime flags
125 *
126 * Written by the kernel, shouldn't be modified by the
127 * application.
128 *
129 * The application needs a full memory barrier before checking
130 * for IORING_SQ_NEED_WAKEUP after updating the sq tail.
131 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000132 u32 sq_flags;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200133 /*
134 * Number of completion events lost because the queue was full;
135 * this should be avoided by the application by making sure
136 * there are not more requests pending thatn there is space in
137 * the completion queue.
138 *
139 * Written by the kernel, shouldn't be modified by the
140 * application (i.e. get number of "new events" by comparing to
141 * cached value).
142 *
143 * As completion events come in out of order this counter is not
144 * ordered with any other data.
145 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000146 u32 cq_overflow;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200147 /*
148 * Ring buffer of completion events.
149 *
150 * The kernel writes completion events fresh every time they are
151 * produced, so the application is allowed to modify pending
152 * entries.
153 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000154 struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700155};
156
Jens Axboeedafcce2019-01-09 09:16:05 -0700157struct io_mapped_ubuf {
158 u64 ubuf;
159 size_t len;
160 struct bio_vec *bvec;
161 unsigned int nr_bvecs;
162};
163
Jens Axboe31b51512019-01-18 22:56:34 -0700164struct async_list {
165 spinlock_t lock;
166 atomic_t cnt;
167 struct list_head list;
168
169 struct file *file;
Jens Axboe6d5d5ac2019-09-11 10:16:13 -0600170 off_t io_start;
Zhengyuan Liu9310a7ba2019-07-22 10:23:27 +0800171 size_t io_len;
Jens Axboe31b51512019-01-18 22:56:34 -0700172};
173
Jens Axboe2b188cc2019-01-07 10:46:33 -0700174struct io_ring_ctx {
175 struct {
176 struct percpu_ref refs;
177 } ____cacheline_aligned_in_smp;
178
179 struct {
180 unsigned int flags;
181 bool compat;
182 bool account_mem;
183
Hristo Venev75b28af2019-08-26 17:23:46 +0000184 /*
185 * Ring buffer of indices into array of io_uring_sqe, which is
186 * mmapped by the application using the IORING_OFF_SQES offset.
187 *
188 * This indirection could e.g. be used to assign fixed
189 * io_uring_sqe entries to operations and only submit them to
190 * the queue when needed.
191 *
192 * The kernel modifies neither the indices array nor the entries
193 * array.
194 */
195 u32 *sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700196 unsigned cached_sq_head;
197 unsigned sq_entries;
198 unsigned sq_mask;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700199 unsigned sq_thread_idle;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700200 struct io_uring_sqe *sq_sqes;
Jens Axboede0617e2019-04-06 21:51:27 -0600201
202 struct list_head defer_list;
Jens Axboe5262f562019-09-17 12:26:57 -0600203 struct list_head timeout_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700204 } ____cacheline_aligned_in_smp;
205
206 /* IO offload */
Jens Axboe54a91f32019-09-10 09:15:04 -0600207 struct workqueue_struct *sqo_wq[2];
Jens Axboe6c271ce2019-01-10 11:22:30 -0700208 struct task_struct *sqo_thread; /* if using sq thread polling */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700209 struct mm_struct *sqo_mm;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700210 wait_queue_head_t sqo_wait;
Jackie Liua4c0b3d2019-07-08 13:41:12 +0800211 struct completion sqo_thread_started;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700212
213 struct {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700214 unsigned cached_cq_tail;
215 unsigned cq_entries;
216 unsigned cq_mask;
217 struct wait_queue_head cq_wait;
218 struct fasync_struct *cq_fasync;
Jens Axboe9b402842019-04-11 11:45:41 -0600219 struct eventfd_ctx *cq_ev_fd;
Jens Axboe5262f562019-09-17 12:26:57 -0600220 atomic_t cq_timeouts;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700221 } ____cacheline_aligned_in_smp;
222
Hristo Venev75b28af2019-08-26 17:23:46 +0000223 struct io_rings *rings;
224
Jens Axboe6b063142019-01-10 22:13:58 -0700225 /*
226 * If used, fixed file set. Writers must ensure that ->refs is dead,
227 * readers must ensure that ->refs is alive as long as the file* is
228 * used. Only updated through io_uring_register(2).
229 */
230 struct file **user_files;
231 unsigned nr_user_files;
232
Jens Axboeedafcce2019-01-09 09:16:05 -0700233 /* if used, fixed mapped user buffers */
234 unsigned nr_user_bufs;
235 struct io_mapped_ubuf *user_bufs;
236
Jens Axboe2b188cc2019-01-07 10:46:33 -0700237 struct user_struct *user;
238
239 struct completion ctx_done;
240
241 struct {
242 struct mutex uring_lock;
243 wait_queue_head_t wait;
244 } ____cacheline_aligned_in_smp;
245
246 struct {
247 spinlock_t completion_lock;
Jens Axboedef596e2019-01-09 08:59:42 -0700248 bool poll_multi_file;
249 /*
250 * ->poll_list is protected by the ctx->uring_lock for
251 * io_uring instances that don't use IORING_SETUP_SQPOLL.
252 * For SQPOLL, only the single threaded io_sq_thread() will
253 * manipulate the list, hence no extra locking is needed there.
254 */
255 struct list_head poll_list;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700256 struct list_head cancel_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700257 } ____cacheline_aligned_in_smp;
258
Jens Axboe31b51512019-01-18 22:56:34 -0700259 struct async_list pending_async[2];
260
Jens Axboe2b188cc2019-01-07 10:46:33 -0700261#if defined(CONFIG_UNIX)
262 struct socket *ring_sock;
263#endif
264};
265
266struct sqe_submit {
267 const struct io_uring_sqe *sqe;
268 unsigned short index;
Jackie Liu8776f3f2019-09-09 20:50:39 +0800269 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700270 bool has_user;
Jens Axboedef596e2019-01-09 08:59:42 -0700271 bool needs_lock;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700272 bool needs_fixed_file;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700273};
274
Jens Axboe09bb8392019-03-13 12:39:28 -0600275/*
276 * First field must be the file pointer in all the
277 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
278 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700279struct io_poll_iocb {
280 struct file *file;
281 struct wait_queue_head *head;
282 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600283 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700284 bool canceled;
285 struct wait_queue_entry wait;
286};
287
Jens Axboe5262f562019-09-17 12:26:57 -0600288struct io_timeout {
289 struct file *file;
290 struct hrtimer timer;
291};
292
Jens Axboe09bb8392019-03-13 12:39:28 -0600293/*
294 * NOTE! Each of the iocb union members has the file pointer
295 * as the first entry in their struct definition. So you can
296 * access the file pointer through any of the sub-structs,
297 * or directly as just 'ki_filp' in this struct.
298 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700299struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700300 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600301 struct file *file;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700302 struct kiocb rw;
303 struct io_poll_iocb poll;
Jens Axboe5262f562019-09-17 12:26:57 -0600304 struct io_timeout timeout;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700305 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700306
307 struct sqe_submit submit;
308
309 struct io_ring_ctx *ctx;
310 struct list_head list;
Jens Axboe9e645e112019-05-10 16:07:28 -0600311 struct list_head link_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700312 unsigned int flags;
Jens Axboec16361c2019-01-17 08:39:48 -0700313 refcount_t refs;
Stefan Bühler8449eed2019-04-27 20:34:19 +0200314#define REQ_F_NOWAIT 1 /* must not punt to workers */
Jens Axboedef596e2019-01-09 08:59:42 -0700315#define REQ_F_IOPOLL_COMPLETED 2 /* polled IO has completed */
Jens Axboe6b063142019-01-10 22:13:58 -0700316#define REQ_F_FIXED_FILE 4 /* ctx owns file */
Jens Axboe31b51512019-01-18 22:56:34 -0700317#define REQ_F_SEQ_PREV 8 /* sequential with previous */
Stefan Bühlere2033e32019-05-11 19:08:01 +0200318#define REQ_F_IO_DRAIN 16 /* drain existing IO first */
319#define REQ_F_IO_DRAINED 32 /* drain done */
Jens Axboe9e645e112019-05-10 16:07:28 -0600320#define REQ_F_LINK 64 /* linked sqes */
Zhengyuan Liuf7b76ac2019-07-16 23:26:14 +0800321#define REQ_F_LINK_DONE 128 /* linked sqes done */
322#define REQ_F_FAIL_LINK 256 /* fail rest of links */
Jackie Liu4fe2c962019-09-09 20:50:40 +0800323#define REQ_F_SHADOW_DRAIN 512 /* link-drain shadow req */
Jens Axboe5262f562019-09-17 12:26:57 -0600324#define REQ_F_TIMEOUT 1024 /* timeout request */
Jens Axboe491381ce2019-10-17 09:20:46 -0600325#define REQ_F_ISREG 2048 /* regular file */
326#define REQ_F_MUST_PUNT 4096 /* must be punted even for NONBLOCK */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700327 u64 user_data;
Jens Axboe9e645e112019-05-10 16:07:28 -0600328 u32 result;
Jens Axboede0617e2019-04-06 21:51:27 -0600329 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700330
331 struct work_struct work;
332};
333
334#define IO_PLUG_THRESHOLD 2
Jens Axboedef596e2019-01-09 08:59:42 -0700335#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700336
Jens Axboe9a56a232019-01-09 09:06:50 -0700337struct io_submit_state {
338 struct blk_plug plug;
339
340 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700341 * io_kiocb alloc cache
342 */
343 void *reqs[IO_IOPOLL_BATCH];
344 unsigned int free_reqs;
345 unsigned int cur_req;
346
347 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700348 * File reference cache
349 */
350 struct file *file;
351 unsigned int fd;
352 unsigned int has_refs;
353 unsigned int used_refs;
354 unsigned int ios_left;
355};
356
Jens Axboede0617e2019-04-06 21:51:27 -0600357static void io_sq_wq_submit_work(struct work_struct *work);
Jens Axboe5262f562019-09-17 12:26:57 -0600358static void io_cqring_fill_event(struct io_ring_ctx *ctx, u64 ki_user_data,
359 long res);
Jackie Liu4fe2c962019-09-09 20:50:40 +0800360static void __io_free_req(struct io_kiocb *req);
Jens Axboede0617e2019-04-06 21:51:27 -0600361
Jens Axboe2b188cc2019-01-07 10:46:33 -0700362static struct kmem_cache *req_cachep;
363
364static const struct file_operations io_uring_fops;
365
366struct sock *io_uring_get_socket(struct file *file)
367{
368#if defined(CONFIG_UNIX)
369 if (file->f_op == &io_uring_fops) {
370 struct io_ring_ctx *ctx = file->private_data;
371
372 return ctx->ring_sock->sk;
373 }
374#endif
375 return NULL;
376}
377EXPORT_SYMBOL(io_uring_get_socket);
378
379static void io_ring_ctx_ref_free(struct percpu_ref *ref)
380{
381 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
382
383 complete(&ctx->ctx_done);
384}
385
386static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
387{
388 struct io_ring_ctx *ctx;
Jens Axboe31b51512019-01-18 22:56:34 -0700389 int i;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700390
391 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
392 if (!ctx)
393 return NULL;
394
Roman Gushchin21482892019-05-07 10:01:48 -0700395 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
396 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700397 kfree(ctx);
398 return NULL;
399 }
400
401 ctx->flags = p->flags;
402 init_waitqueue_head(&ctx->cq_wait);
403 init_completion(&ctx->ctx_done);
Jackie Liua4c0b3d2019-07-08 13:41:12 +0800404 init_completion(&ctx->sqo_thread_started);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700405 mutex_init(&ctx->uring_lock);
406 init_waitqueue_head(&ctx->wait);
Jens Axboe31b51512019-01-18 22:56:34 -0700407 for (i = 0; i < ARRAY_SIZE(ctx->pending_async); i++) {
408 spin_lock_init(&ctx->pending_async[i].lock);
409 INIT_LIST_HEAD(&ctx->pending_async[i].list);
410 atomic_set(&ctx->pending_async[i].cnt, 0);
411 }
Jens Axboe2b188cc2019-01-07 10:46:33 -0700412 spin_lock_init(&ctx->completion_lock);
Jens Axboedef596e2019-01-09 08:59:42 -0700413 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboe221c5eb2019-01-17 09:41:58 -0700414 INIT_LIST_HEAD(&ctx->cancel_list);
Jens Axboede0617e2019-04-06 21:51:27 -0600415 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -0600416 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700417 return ctx;
418}
419
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600420static inline bool __io_sequence_defer(struct io_ring_ctx *ctx,
421 struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -0600422{
Hristo Venev75b28af2019-08-26 17:23:46 +0000423 return req->sequence != ctx->cached_cq_tail + ctx->rings->sq_dropped;
Jens Axboede0617e2019-04-06 21:51:27 -0600424}
425
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600426static inline bool io_sequence_defer(struct io_ring_ctx *ctx,
427 struct io_kiocb *req)
428{
429 if ((req->flags & (REQ_F_IO_DRAIN|REQ_F_IO_DRAINED)) != REQ_F_IO_DRAIN)
430 return false;
431
432 return __io_sequence_defer(ctx, req);
433}
434
435static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -0600436{
437 struct io_kiocb *req;
438
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600439 req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list);
440 if (req && !io_sequence_defer(ctx, req)) {
Jens Axboede0617e2019-04-06 21:51:27 -0600441 list_del_init(&req->list);
442 return req;
443 }
444
445 return NULL;
446}
447
Jens Axboe5262f562019-09-17 12:26:57 -0600448static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx)
449{
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600450 struct io_kiocb *req;
451
452 req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list);
453 if (req && !__io_sequence_defer(ctx, req)) {
454 list_del_init(&req->list);
455 return req;
456 }
457
458 return NULL;
Jens Axboe5262f562019-09-17 12:26:57 -0600459}
460
Jens Axboede0617e2019-04-06 21:51:27 -0600461static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700462{
Hristo Venev75b28af2019-08-26 17:23:46 +0000463 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700464
Hristo Venev75b28af2019-08-26 17:23:46 +0000465 if (ctx->cached_cq_tail != READ_ONCE(rings->cq.tail)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700466 /* order cqe stores with ring update */
Hristo Venev75b28af2019-08-26 17:23:46 +0000467 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700468
Jens Axboe2b188cc2019-01-07 10:46:33 -0700469 if (wq_has_sleeper(&ctx->cq_wait)) {
470 wake_up_interruptible(&ctx->cq_wait);
471 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
472 }
473 }
474}
475
Jens Axboe18d9be12019-09-10 09:13:05 -0600476static inline void io_queue_async_work(struct io_ring_ctx *ctx,
477 struct io_kiocb *req)
478{
Jens Axboe6cc47d12019-09-18 11:18:23 -0600479 int rw = 0;
Jens Axboe54a91f32019-09-10 09:15:04 -0600480
Jens Axboe6cc47d12019-09-18 11:18:23 -0600481 if (req->submit.sqe) {
482 switch (req->submit.sqe->opcode) {
483 case IORING_OP_WRITEV:
484 case IORING_OP_WRITE_FIXED:
485 rw = !(req->rw.ki_flags & IOCB_DIRECT);
486 break;
487 }
Jens Axboe54a91f32019-09-10 09:15:04 -0600488 }
489
490 queue_work(ctx->sqo_wq[rw], &req->work);
Jens Axboe18d9be12019-09-10 09:13:05 -0600491}
492
Jens Axboe5262f562019-09-17 12:26:57 -0600493static void io_kill_timeout(struct io_kiocb *req)
494{
495 int ret;
496
497 ret = hrtimer_try_to_cancel(&req->timeout.timer);
498 if (ret != -1) {
499 atomic_inc(&req->ctx->cq_timeouts);
500 list_del(&req->list);
501 io_cqring_fill_event(req->ctx, req->user_data, 0);
502 __io_free_req(req);
503 }
504}
505
506static void io_kill_timeouts(struct io_ring_ctx *ctx)
507{
508 struct io_kiocb *req, *tmp;
509
510 spin_lock_irq(&ctx->completion_lock);
511 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
512 io_kill_timeout(req);
513 spin_unlock_irq(&ctx->completion_lock);
514}
515
Jens Axboede0617e2019-04-06 21:51:27 -0600516static void io_commit_cqring(struct io_ring_ctx *ctx)
517{
518 struct io_kiocb *req;
519
Jens Axboe5262f562019-09-17 12:26:57 -0600520 while ((req = io_get_timeout_req(ctx)) != NULL)
521 io_kill_timeout(req);
522
Jens Axboede0617e2019-04-06 21:51:27 -0600523 __io_commit_cqring(ctx);
524
525 while ((req = io_get_deferred_req(ctx)) != NULL) {
Jackie Liu4fe2c962019-09-09 20:50:40 +0800526 if (req->flags & REQ_F_SHADOW_DRAIN) {
527 /* Just for drain, free it. */
528 __io_free_req(req);
529 continue;
530 }
Jens Axboede0617e2019-04-06 21:51:27 -0600531 req->flags |= REQ_F_IO_DRAINED;
Jens Axboe18d9be12019-09-10 09:13:05 -0600532 io_queue_async_work(ctx, req);
Jens Axboede0617e2019-04-06 21:51:27 -0600533 }
534}
535
Jens Axboe2b188cc2019-01-07 10:46:33 -0700536static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
537{
Hristo Venev75b28af2019-08-26 17:23:46 +0000538 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700539 unsigned tail;
540
541 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +0200542 /*
543 * writes to the cq entry need to come after reading head; the
544 * control dependency is enough as we're using WRITE_ONCE to
545 * fill the cq entry
546 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000547 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700548 return NULL;
549
550 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +0000551 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -0700552}
553
554static void io_cqring_fill_event(struct io_ring_ctx *ctx, u64 ki_user_data,
Jens Axboec71ffb62019-05-13 20:58:29 -0600555 long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700556{
557 struct io_uring_cqe *cqe;
558
559 /*
560 * If we can't get a cq entry, userspace overflowed the
561 * submission (by quite a lot). Increment the overflow count in
562 * the ring.
563 */
564 cqe = io_get_cqring(ctx);
565 if (cqe) {
566 WRITE_ONCE(cqe->user_data, ki_user_data);
567 WRITE_ONCE(cqe->res, res);
Jens Axboec71ffb62019-05-13 20:58:29 -0600568 WRITE_ONCE(cqe->flags, 0);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700569 } else {
Hristo Venev75b28af2019-08-26 17:23:46 +0000570 unsigned overflow = READ_ONCE(ctx->rings->cq_overflow);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700571
Hristo Venev75b28af2019-08-26 17:23:46 +0000572 WRITE_ONCE(ctx->rings->cq_overflow, overflow + 1);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700573 }
574}
575
Jens Axboe8c838782019-03-12 15:48:16 -0600576static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
577{
578 if (waitqueue_active(&ctx->wait))
579 wake_up(&ctx->wait);
580 if (waitqueue_active(&ctx->sqo_wait))
581 wake_up(&ctx->sqo_wait);
Jens Axboe9b402842019-04-11 11:45:41 -0600582 if (ctx->cq_ev_fd)
583 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -0600584}
585
586static void io_cqring_add_event(struct io_ring_ctx *ctx, u64 user_data,
Jens Axboec71ffb62019-05-13 20:58:29 -0600587 long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700588{
589 unsigned long flags;
590
591 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboec71ffb62019-05-13 20:58:29 -0600592 io_cqring_fill_event(ctx, user_data, res);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700593 io_commit_cqring(ctx);
594 spin_unlock_irqrestore(&ctx->completion_lock, flags);
595
Jens Axboe8c838782019-03-12 15:48:16 -0600596 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700597}
598
Jens Axboe2579f912019-01-09 09:10:43 -0700599static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
600 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700601{
Jens Axboefd6fab22019-03-14 16:30:06 -0600602 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700603 struct io_kiocb *req;
604
605 if (!percpu_ref_tryget(&ctx->refs))
606 return NULL;
607
Jens Axboe2579f912019-01-09 09:10:43 -0700608 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -0600609 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -0700610 if (unlikely(!req))
611 goto out;
612 } else if (!state->free_reqs) {
613 size_t sz;
614 int ret;
615
616 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -0600617 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
618
619 /*
620 * Bulk alloc is all-or-nothing. If we fail to get a batch,
621 * retry single alloc to be on the safe side.
622 */
623 if (unlikely(ret <= 0)) {
624 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
625 if (!state->reqs[0])
626 goto out;
627 ret = 1;
628 }
Jens Axboe2579f912019-01-09 09:10:43 -0700629 state->free_reqs = ret - 1;
630 state->cur_req = 1;
631 req = state->reqs[0];
632 } else {
633 req = state->reqs[state->cur_req];
634 state->free_reqs--;
635 state->cur_req++;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700636 }
637
Jens Axboe60c112b2019-06-21 10:20:18 -0600638 req->file = NULL;
Jens Axboe2579f912019-01-09 09:10:43 -0700639 req->ctx = ctx;
640 req->flags = 0;
Jens Axboee65ef562019-03-12 10:16:44 -0600641 /* one is dropped after submission, the other at completion */
642 refcount_set(&req->refs, 2);
Jens Axboe9e645e112019-05-10 16:07:28 -0600643 req->result = 0;
Jens Axboe2579f912019-01-09 09:10:43 -0700644 return req;
645out:
Pavel Begunkov6805b322019-10-08 02:18:42 +0300646 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700647 return NULL;
648}
649
Jens Axboedef596e2019-01-09 08:59:42 -0700650static void io_free_req_many(struct io_ring_ctx *ctx, void **reqs, int *nr)
651{
652 if (*nr) {
653 kmem_cache_free_bulk(req_cachep, *nr, reqs);
Pavel Begunkov6805b322019-10-08 02:18:42 +0300654 percpu_ref_put_many(&ctx->refs, *nr);
Jens Axboedef596e2019-01-09 08:59:42 -0700655 *nr = 0;
656 }
657}
658
Jens Axboe9e645e112019-05-10 16:07:28 -0600659static void __io_free_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700660{
Jens Axboe09bb8392019-03-13 12:39:28 -0600661 if (req->file && !(req->flags & REQ_F_FIXED_FILE))
662 fput(req->file);
Pavel Begunkov6805b322019-10-08 02:18:42 +0300663 percpu_ref_put(&req->ctx->refs);
Jens Axboee65ef562019-03-12 10:16:44 -0600664 kmem_cache_free(req_cachep, req);
665}
666
Jens Axboe9e645e112019-05-10 16:07:28 -0600667static void io_req_link_next(struct io_kiocb *req)
668{
669 struct io_kiocb *nxt;
670
671 /*
672 * The list should never be empty when we are called here. But could
673 * potentially happen if the chain is messed up, check to be on the
674 * safe side.
675 */
676 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, list);
677 if (nxt) {
678 list_del(&nxt->list);
679 if (!list_empty(&req->link_list)) {
680 INIT_LIST_HEAD(&nxt->link_list);
681 list_splice(&req->link_list, &nxt->link_list);
682 nxt->flags |= REQ_F_LINK;
683 }
684
Zhengyuan Liuf7b76ac2019-07-16 23:26:14 +0800685 nxt->flags |= REQ_F_LINK_DONE;
Jens Axboe9e645e112019-05-10 16:07:28 -0600686 INIT_WORK(&nxt->work, io_sq_wq_submit_work);
Jens Axboe18d9be12019-09-10 09:13:05 -0600687 io_queue_async_work(req->ctx, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -0600688 }
689}
690
691/*
692 * Called if REQ_F_LINK is set, and we fail the head request
693 */
694static void io_fail_links(struct io_kiocb *req)
695{
696 struct io_kiocb *link;
697
698 while (!list_empty(&req->link_list)) {
699 link = list_first_entry(&req->link_list, struct io_kiocb, list);
700 list_del(&link->list);
701
702 io_cqring_add_event(req->ctx, link->user_data, -ECANCELED);
703 __io_free_req(link);
704 }
705}
706
707static void io_free_req(struct io_kiocb *req)
708{
709 /*
710 * If LINK is set, we have dependent requests in this chain. If we
711 * didn't fail this request, queue the first one up, moving any other
712 * dependencies to the next request. In case of failure, fail the rest
713 * of the chain.
714 */
715 if (req->flags & REQ_F_LINK) {
716 if (req->flags & REQ_F_FAIL_LINK)
717 io_fail_links(req);
718 else
719 io_req_link_next(req);
720 }
721
722 __io_free_req(req);
723}
724
Jens Axboee65ef562019-03-12 10:16:44 -0600725static void io_put_req(struct io_kiocb *req)
726{
727 if (refcount_dec_and_test(&req->refs))
728 io_free_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700729}
730
Hristo Venev75b28af2019-08-26 17:23:46 +0000731static unsigned io_cqring_events(struct io_rings *rings)
Jens Axboea3a0e432019-08-20 11:03:11 -0600732{
733 /* See comment at the top of this file */
734 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +0000735 return READ_ONCE(rings->cq.tail) - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -0600736}
737
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +0300738static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
739{
740 struct io_rings *rings = ctx->rings;
741
742 /* make sure SQ entry isn't read before tail */
743 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
744}
745
Jens Axboedef596e2019-01-09 08:59:42 -0700746/*
747 * Find and free completed poll iocbs
748 */
749static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
750 struct list_head *done)
751{
752 void *reqs[IO_IOPOLL_BATCH];
753 struct io_kiocb *req;
Jens Axboe09bb8392019-03-13 12:39:28 -0600754 int to_free;
Jens Axboedef596e2019-01-09 08:59:42 -0700755
Jens Axboe09bb8392019-03-13 12:39:28 -0600756 to_free = 0;
Jens Axboedef596e2019-01-09 08:59:42 -0700757 while (!list_empty(done)) {
758 req = list_first_entry(done, struct io_kiocb, list);
759 list_del(&req->list);
760
Jens Axboe9e645e112019-05-10 16:07:28 -0600761 io_cqring_fill_event(ctx, req->user_data, req->result);
Jens Axboedef596e2019-01-09 08:59:42 -0700762 (*nr_events)++;
763
Jens Axboe09bb8392019-03-13 12:39:28 -0600764 if (refcount_dec_and_test(&req->refs)) {
765 /* If we're not using fixed files, we have to pair the
766 * completion part with the file put. Use regular
767 * completions for those, only batch free for fixed
Jens Axboe9e645e112019-05-10 16:07:28 -0600768 * file and non-linked commands.
Jens Axboe09bb8392019-03-13 12:39:28 -0600769 */
Jens Axboe9e645e112019-05-10 16:07:28 -0600770 if ((req->flags & (REQ_F_FIXED_FILE|REQ_F_LINK)) ==
771 REQ_F_FIXED_FILE) {
Jens Axboe09bb8392019-03-13 12:39:28 -0600772 reqs[to_free++] = req;
773 if (to_free == ARRAY_SIZE(reqs))
774 io_free_req_many(ctx, reqs, &to_free);
Jens Axboe6b063142019-01-10 22:13:58 -0700775 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -0600776 io_free_req(req);
Jens Axboe6b063142019-01-10 22:13:58 -0700777 }
Jens Axboe9a56a232019-01-09 09:06:50 -0700778 }
Jens Axboedef596e2019-01-09 08:59:42 -0700779 }
Jens Axboedef596e2019-01-09 08:59:42 -0700780
Jens Axboe09bb8392019-03-13 12:39:28 -0600781 io_commit_cqring(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -0700782 io_free_req_many(ctx, reqs, &to_free);
783}
784
785static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
786 long min)
787{
788 struct io_kiocb *req, *tmp;
789 LIST_HEAD(done);
790 bool spin;
791 int ret;
792
793 /*
794 * Only spin for completions if we don't have multiple devices hanging
795 * off our complete list, and we're under the requested amount.
796 */
797 spin = !ctx->poll_multi_file && *nr_events < min;
798
799 ret = 0;
800 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
801 struct kiocb *kiocb = &req->rw;
802
803 /*
804 * Move completed entries to our local list. If we find a
805 * request that requires polling, break out and complete
806 * the done list first, if we have entries there.
807 */
808 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
809 list_move_tail(&req->list, &done);
810 continue;
811 }
812 if (!list_empty(&done))
813 break;
814
815 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
816 if (ret < 0)
817 break;
818
819 if (ret && spin)
820 spin = false;
821 ret = 0;
822 }
823
824 if (!list_empty(&done))
825 io_iopoll_complete(ctx, nr_events, &done);
826
827 return ret;
828}
829
830/*
831 * Poll for a mininum of 'min' events. Note that if min == 0 we consider that a
832 * non-spinning poll check - we'll still enter the driver poll loop, but only
833 * as a non-spinning completion check.
834 */
835static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
836 long min)
837{
Jens Axboe08f54392019-08-21 22:19:11 -0600838 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -0700839 int ret;
840
841 ret = io_do_iopoll(ctx, nr_events, min);
842 if (ret < 0)
843 return ret;
844 if (!min || *nr_events >= min)
845 return 0;
846 }
847
848 return 1;
849}
850
851/*
852 * We can't just wait for polled events to come to us, we have to actively
853 * find and complete them.
854 */
855static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
856{
857 if (!(ctx->flags & IORING_SETUP_IOPOLL))
858 return;
859
860 mutex_lock(&ctx->uring_lock);
861 while (!list_empty(&ctx->poll_list)) {
862 unsigned int nr_events = 0;
863
864 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -0600865
866 /*
867 * Ensure we allow local-to-the-cpu processing to take place,
868 * in this case we need to ensure that we reap all events.
869 */
870 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -0700871 }
872 mutex_unlock(&ctx->uring_lock);
873}
874
875static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
876 long min)
877{
Jens Axboe500f9fb2019-08-19 12:15:59 -0600878 int iters, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -0700879
Jens Axboe500f9fb2019-08-19 12:15:59 -0600880 /*
881 * We disallow the app entering submit/complete with polling, but we
882 * still need to lock the ring to prevent racing with polled issue
883 * that got punted to a workqueue.
884 */
885 mutex_lock(&ctx->uring_lock);
886
887 iters = 0;
Jens Axboedef596e2019-01-09 08:59:42 -0700888 do {
889 int tmin = 0;
890
Jens Axboe500f9fb2019-08-19 12:15:59 -0600891 /*
Jens Axboea3a0e432019-08-20 11:03:11 -0600892 * Don't enter poll loop if we already have events pending.
893 * If we do, we can potentially be spinning for commands that
894 * already triggered a CQE (eg in error).
895 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000896 if (io_cqring_events(ctx->rings))
Jens Axboea3a0e432019-08-20 11:03:11 -0600897 break;
898
899 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -0600900 * If a submit got punted to a workqueue, we can have the
901 * application entering polling for a command before it gets
902 * issued. That app will hold the uring_lock for the duration
903 * of the poll right here, so we need to take a breather every
904 * now and then to ensure that the issue has a chance to add
905 * the poll to the issued list. Otherwise we can spin here
906 * forever, while the workqueue is stuck trying to acquire the
907 * very same mutex.
908 */
909 if (!(++iters & 7)) {
910 mutex_unlock(&ctx->uring_lock);
911 mutex_lock(&ctx->uring_lock);
912 }
913
Jens Axboedef596e2019-01-09 08:59:42 -0700914 if (*nr_events < min)
915 tmin = min - *nr_events;
916
917 ret = io_iopoll_getevents(ctx, nr_events, tmin);
918 if (ret <= 0)
919 break;
920 ret = 0;
921 } while (min && !*nr_events && !need_resched());
922
Jens Axboe500f9fb2019-08-19 12:15:59 -0600923 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -0700924 return ret;
925}
926
Jens Axboe491381ce2019-10-17 09:20:46 -0600927static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700928{
Jens Axboe491381ce2019-10-17 09:20:46 -0600929 /*
930 * Tell lockdep we inherited freeze protection from submission
931 * thread.
932 */
933 if (req->flags & REQ_F_ISREG) {
934 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700935
Jens Axboe491381ce2019-10-17 09:20:46 -0600936 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700937 }
Jens Axboe491381ce2019-10-17 09:20:46 -0600938 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700939}
940
941static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
942{
943 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
944
Jens Axboe491381ce2019-10-17 09:20:46 -0600945 if (kiocb->ki_flags & IOCB_WRITE)
946 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700947
Jens Axboe9e645e112019-05-10 16:07:28 -0600948 if ((req->flags & REQ_F_LINK) && res != req->result)
949 req->flags |= REQ_F_FAIL_LINK;
Jens Axboec71ffb62019-05-13 20:58:29 -0600950 io_cqring_add_event(req->ctx, req->user_data, res);
Jens Axboee65ef562019-03-12 10:16:44 -0600951 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700952}
953
Jens Axboedef596e2019-01-09 08:59:42 -0700954static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
955{
956 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
957
Jens Axboe491381ce2019-10-17 09:20:46 -0600958 if (kiocb->ki_flags & IOCB_WRITE)
959 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -0700960
Jens Axboe9e645e112019-05-10 16:07:28 -0600961 if ((req->flags & REQ_F_LINK) && res != req->result)
962 req->flags |= REQ_F_FAIL_LINK;
963 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -0700964 if (res != -EAGAIN)
965 req->flags |= REQ_F_IOPOLL_COMPLETED;
966}
967
968/*
969 * After the iocb has been issued, it's safe to be found on the poll list.
970 * Adding the kiocb to the list AFTER submission ensures that we don't
971 * find it from a io_iopoll_getevents() thread before the issuer is done
972 * accessing the kiocb cookie.
973 */
974static void io_iopoll_req_issued(struct io_kiocb *req)
975{
976 struct io_ring_ctx *ctx = req->ctx;
977
978 /*
979 * Track whether we have multiple files in our lists. This will impact
980 * how we do polling eventually, not spinning if we're on potentially
981 * different devices.
982 */
983 if (list_empty(&ctx->poll_list)) {
984 ctx->poll_multi_file = false;
985 } else if (!ctx->poll_multi_file) {
986 struct io_kiocb *list_req;
987
988 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
989 list);
990 if (list_req->rw.ki_filp != req->rw.ki_filp)
991 ctx->poll_multi_file = true;
992 }
993
994 /*
995 * For fast devices, IO may have already completed. If it has, add
996 * it to the front so we find it first.
997 */
998 if (req->flags & REQ_F_IOPOLL_COMPLETED)
999 list_add(&req->list, &ctx->poll_list);
1000 else
1001 list_add_tail(&req->list, &ctx->poll_list);
1002}
1003
Jens Axboe3d6770f2019-04-13 11:50:54 -06001004static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07001005{
Jens Axboe3d6770f2019-04-13 11:50:54 -06001006 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -07001007 int diff = state->has_refs - state->used_refs;
1008
1009 if (diff)
1010 fput_many(state->file, diff);
1011 state->file = NULL;
1012 }
1013}
1014
1015/*
1016 * Get as many references to a file as we have IOs left in this submission,
1017 * assuming most submissions are for one file, or at least that each file
1018 * has more than one submission.
1019 */
1020static struct file *io_file_get(struct io_submit_state *state, int fd)
1021{
1022 if (!state)
1023 return fget(fd);
1024
1025 if (state->file) {
1026 if (state->fd == fd) {
1027 state->used_refs++;
1028 state->ios_left--;
1029 return state->file;
1030 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06001031 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07001032 }
1033 state->file = fget_many(fd, state->ios_left);
1034 if (!state->file)
1035 return NULL;
1036
1037 state->fd = fd;
1038 state->has_refs = state->ios_left;
1039 state->used_refs = 1;
1040 state->ios_left--;
1041 return state->file;
1042}
1043
Jens Axboe2b188cc2019-01-07 10:46:33 -07001044/*
1045 * If we tracked the file through the SCM inflight mechanism, we could support
1046 * any file. For now, just ensure that anything potentially problematic is done
1047 * inline.
1048 */
1049static bool io_file_supports_async(struct file *file)
1050{
1051 umode_t mode = file_inode(file)->i_mode;
1052
1053 if (S_ISBLK(mode) || S_ISCHR(mode))
1054 return true;
1055 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
1056 return true;
1057
1058 return false;
1059}
1060
Jens Axboe6c271ce2019-01-10 11:22:30 -07001061static int io_prep_rw(struct io_kiocb *req, const struct sqe_submit *s,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001062 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001063{
Jens Axboe6c271ce2019-01-10 11:22:30 -07001064 const struct io_uring_sqe *sqe = s->sqe;
Jens Axboedef596e2019-01-09 08:59:42 -07001065 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001066 struct kiocb *kiocb = &req->rw;
Jens Axboe09bb8392019-03-13 12:39:28 -06001067 unsigned ioprio;
1068 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001069
Jens Axboe09bb8392019-03-13 12:39:28 -06001070 if (!req->file)
1071 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001072
Jens Axboe491381ce2019-10-17 09:20:46 -06001073 if (S_ISREG(file_inode(req->file)->i_mode))
1074 req->flags |= REQ_F_ISREG;
1075
1076 /*
1077 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
1078 * we know to async punt it even if it was opened O_NONBLOCK
1079 */
1080 if (force_nonblock && !io_file_supports_async(req->file)) {
1081 req->flags |= REQ_F_MUST_PUNT;
1082 return -EAGAIN;
1083 }
Jens Axboe6b063142019-01-10 22:13:58 -07001084
Jens Axboe2b188cc2019-01-07 10:46:33 -07001085 kiocb->ki_pos = READ_ONCE(sqe->off);
1086 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
1087 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
1088
1089 ioprio = READ_ONCE(sqe->ioprio);
1090 if (ioprio) {
1091 ret = ioprio_check_cap(ioprio);
1092 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06001093 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001094
1095 kiocb->ki_ioprio = ioprio;
1096 } else
1097 kiocb->ki_ioprio = get_current_ioprio();
1098
1099 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
1100 if (unlikely(ret))
Jens Axboe09bb8392019-03-13 12:39:28 -06001101 return ret;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001102
1103 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06001104 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
1105 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02001106 req->flags |= REQ_F_NOWAIT;
1107
1108 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001109 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001110
Jens Axboedef596e2019-01-09 08:59:42 -07001111 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07001112 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
1113 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06001114 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001115
Jens Axboedef596e2019-01-09 08:59:42 -07001116 kiocb->ki_flags |= IOCB_HIPRI;
1117 kiocb->ki_complete = io_complete_rw_iopoll;
1118 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001119 if (kiocb->ki_flags & IOCB_HIPRI)
1120 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07001121 kiocb->ki_complete = io_complete_rw;
1122 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001123 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001124}
1125
1126static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
1127{
1128 switch (ret) {
1129 case -EIOCBQUEUED:
1130 break;
1131 case -ERESTARTSYS:
1132 case -ERESTARTNOINTR:
1133 case -ERESTARTNOHAND:
1134 case -ERESTART_RESTARTBLOCK:
1135 /*
1136 * We can't just restart the syscall, since previously
1137 * submitted sqes may already be in progress. Just fail this
1138 * IO with EINTR.
1139 */
1140 ret = -EINTR;
1141 /* fall through */
1142 default:
1143 kiocb->ki_complete(kiocb, ret, 0);
1144 }
1145}
1146
Jens Axboeedafcce2019-01-09 09:16:05 -07001147static int io_import_fixed(struct io_ring_ctx *ctx, int rw,
1148 const struct io_uring_sqe *sqe,
1149 struct iov_iter *iter)
1150{
1151 size_t len = READ_ONCE(sqe->len);
1152 struct io_mapped_ubuf *imu;
1153 unsigned index, buf_index;
1154 size_t offset;
1155 u64 buf_addr;
1156
1157 /* attempt to use fixed buffers without having provided iovecs */
1158 if (unlikely(!ctx->user_bufs))
1159 return -EFAULT;
1160
1161 buf_index = READ_ONCE(sqe->buf_index);
1162 if (unlikely(buf_index >= ctx->nr_user_bufs))
1163 return -EFAULT;
1164
1165 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
1166 imu = &ctx->user_bufs[index];
1167 buf_addr = READ_ONCE(sqe->addr);
1168
1169 /* overflow */
1170 if (buf_addr + len < buf_addr)
1171 return -EFAULT;
1172 /* not inside the mapped region */
1173 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
1174 return -EFAULT;
1175
1176 /*
1177 * May not be a start of buffer, set size appropriately
1178 * and advance us to the beginning.
1179 */
1180 offset = buf_addr - imu->ubuf;
1181 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06001182
1183 if (offset) {
1184 /*
1185 * Don't use iov_iter_advance() here, as it's really slow for
1186 * using the latter parts of a big fixed buffer - it iterates
1187 * over each segment manually. We can cheat a bit here, because
1188 * we know that:
1189 *
1190 * 1) it's a BVEC iter, we set it up
1191 * 2) all bvecs are PAGE_SIZE in size, except potentially the
1192 * first and last bvec
1193 *
1194 * So just find our index, and adjust the iterator afterwards.
1195 * If the offset is within the first bvec (or the whole first
1196 * bvec, just use iov_iter_advance(). This makes it easier
1197 * since we can just skip the first segment, which may not
1198 * be PAGE_SIZE aligned.
1199 */
1200 const struct bio_vec *bvec = imu->bvec;
1201
1202 if (offset <= bvec->bv_len) {
1203 iov_iter_advance(iter, offset);
1204 } else {
1205 unsigned long seg_skip;
1206
1207 /* skip first vec */
1208 offset -= bvec->bv_len;
1209 seg_skip = 1 + (offset >> PAGE_SHIFT);
1210
1211 iter->bvec = bvec + seg_skip;
1212 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02001213 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001214 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001215 }
1216 }
1217
Jens Axboeedafcce2019-01-09 09:16:05 -07001218 return 0;
1219}
1220
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001221static ssize_t io_import_iovec(struct io_ring_ctx *ctx, int rw,
1222 const struct sqe_submit *s, struct iovec **iovec,
1223 struct iov_iter *iter)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001224{
1225 const struct io_uring_sqe *sqe = s->sqe;
1226 void __user *buf = u64_to_user_ptr(READ_ONCE(sqe->addr));
1227 size_t sqe_len = READ_ONCE(sqe->len);
Jens Axboeedafcce2019-01-09 09:16:05 -07001228 u8 opcode;
1229
1230 /*
1231 * We're reading ->opcode for the second time, but the first read
1232 * doesn't care whether it's _FIXED or not, so it doesn't matter
1233 * whether ->opcode changes concurrently. The first read does care
1234 * about whether it is a READ or a WRITE, so we don't trust this read
1235 * for that purpose and instead let the caller pass in the read/write
1236 * flag.
1237 */
1238 opcode = READ_ONCE(sqe->opcode);
1239 if (opcode == IORING_OP_READ_FIXED ||
1240 opcode == IORING_OP_WRITE_FIXED) {
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001241 ssize_t ret = io_import_fixed(ctx, rw, sqe, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07001242 *iovec = NULL;
1243 return ret;
1244 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001245
1246 if (!s->has_user)
1247 return -EFAULT;
1248
1249#ifdef CONFIG_COMPAT
1250 if (ctx->compat)
1251 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
1252 iovec, iter);
1253#endif
1254
1255 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
1256}
1257
Jens Axboe6d5d5ac2019-09-11 10:16:13 -06001258static inline bool io_should_merge(struct async_list *al, struct kiocb *kiocb)
1259{
1260 if (al->file == kiocb->ki_filp) {
1261 off_t start, end;
1262
1263 /*
1264 * Allow merging if we're anywhere in the range of the same
1265 * page. Generally this happens for sub-page reads or writes,
1266 * and it's beneficial to allow the first worker to bring the
1267 * page in and the piggy backed work can then work on the
1268 * cached page.
1269 */
1270 start = al->io_start & PAGE_MASK;
1271 end = (al->io_start + al->io_len + PAGE_SIZE - 1) & PAGE_MASK;
1272 if (kiocb->ki_pos >= start && kiocb->ki_pos <= end)
1273 return true;
1274 }
1275
1276 al->file = NULL;
1277 return false;
1278}
1279
Jens Axboe31b51512019-01-18 22:56:34 -07001280/*
1281 * Make a note of the last file/offset/direction we punted to async
1282 * context. We'll use this information to see if we can piggy back a
1283 * sequential request onto the previous one, if it's still hasn't been
1284 * completed by the async worker.
1285 */
1286static void io_async_list_note(int rw, struct io_kiocb *req, size_t len)
1287{
1288 struct async_list *async_list = &req->ctx->pending_async[rw];
1289 struct kiocb *kiocb = &req->rw;
1290 struct file *filp = kiocb->ki_filp;
Jens Axboe31b51512019-01-18 22:56:34 -07001291
Jens Axboe6d5d5ac2019-09-11 10:16:13 -06001292 if (io_should_merge(async_list, kiocb)) {
Zhengyuan Liu9310a7ba2019-07-22 10:23:27 +08001293 unsigned long max_bytes;
Jens Axboe31b51512019-01-18 22:56:34 -07001294
1295 /* Use 8x RA size as a decent limiter for both reads/writes */
Zhengyuan Liu9310a7ba2019-07-22 10:23:27 +08001296 max_bytes = filp->f_ra.ra_pages << (PAGE_SHIFT + 3);
1297 if (!max_bytes)
1298 max_bytes = VM_READAHEAD_PAGES << (PAGE_SHIFT + 3);
Jens Axboe31b51512019-01-18 22:56:34 -07001299
Zhengyuan Liu9310a7ba2019-07-22 10:23:27 +08001300 /* If max len are exceeded, reset the state */
1301 if (async_list->io_len + len <= max_bytes) {
Jens Axboe31b51512019-01-18 22:56:34 -07001302 req->flags |= REQ_F_SEQ_PREV;
Zhengyuan Liu9310a7ba2019-07-22 10:23:27 +08001303 async_list->io_len += len;
Jens Axboe31b51512019-01-18 22:56:34 -07001304 } else {
Jens Axboe6d5d5ac2019-09-11 10:16:13 -06001305 async_list->file = NULL;
Jens Axboe31b51512019-01-18 22:56:34 -07001306 }
1307 }
1308
1309 /* New file? Reset state. */
1310 if (async_list->file != filp) {
Jens Axboe6d5d5ac2019-09-11 10:16:13 -06001311 async_list->io_start = kiocb->ki_pos;
1312 async_list->io_len = len;
Jens Axboe31b51512019-01-18 22:56:34 -07001313 async_list->file = filp;
1314 }
Jens Axboe31b51512019-01-18 22:56:34 -07001315}
1316
Jens Axboe32960612019-09-23 11:05:34 -06001317/*
1318 * For files that don't have ->read_iter() and ->write_iter(), handle them
1319 * by looping over ->read() or ->write() manually.
1320 */
1321static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
1322 struct iov_iter *iter)
1323{
1324 ssize_t ret = 0;
1325
1326 /*
1327 * Don't support polled IO through this interface, and we can't
1328 * support non-blocking either. For the latter, this just causes
1329 * the kiocb to be handled from an async context.
1330 */
1331 if (kiocb->ki_flags & IOCB_HIPRI)
1332 return -EOPNOTSUPP;
1333 if (kiocb->ki_flags & IOCB_NOWAIT)
1334 return -EAGAIN;
1335
1336 while (iov_iter_count(iter)) {
1337 struct iovec iovec = iov_iter_iovec(iter);
1338 ssize_t nr;
1339
1340 if (rw == READ) {
1341 nr = file->f_op->read(file, iovec.iov_base,
1342 iovec.iov_len, &kiocb->ki_pos);
1343 } else {
1344 nr = file->f_op->write(file, iovec.iov_base,
1345 iovec.iov_len, &kiocb->ki_pos);
1346 }
1347
1348 if (nr < 0) {
1349 if (!ret)
1350 ret = nr;
1351 break;
1352 }
1353 ret += nr;
1354 if (nr != iovec.iov_len)
1355 break;
1356 iov_iter_advance(iter, nr);
1357 }
1358
1359 return ret;
1360}
1361
Jens Axboee0c5c572019-03-12 10:18:47 -06001362static int io_read(struct io_kiocb *req, const struct sqe_submit *s,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001363 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001364{
1365 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1366 struct kiocb *kiocb = &req->rw;
1367 struct iov_iter iter;
1368 struct file *file;
Jens Axboe31b51512019-01-18 22:56:34 -07001369 size_t iov_count;
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001370 ssize_t read_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001371
Jens Axboe8358e3a2019-04-23 08:17:58 -06001372 ret = io_prep_rw(req, s, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001373 if (ret)
1374 return ret;
1375 file = kiocb->ki_filp;
1376
Jens Axboe2b188cc2019-01-07 10:46:33 -07001377 if (unlikely(!(file->f_mode & FMODE_READ)))
Jens Axboe09bb8392019-03-13 12:39:28 -06001378 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001379
1380 ret = io_import_iovec(req->ctx, READ, s, &iovec, &iter);
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001381 if (ret < 0)
Jens Axboe09bb8392019-03-13 12:39:28 -06001382 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001383
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001384 read_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06001385 if (req->flags & REQ_F_LINK)
1386 req->result = read_size;
1387
Jens Axboe31b51512019-01-18 22:56:34 -07001388 iov_count = iov_iter_count(&iter);
1389 ret = rw_verify_area(READ, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001390 if (!ret) {
1391 ssize_t ret2;
1392
Jens Axboe32960612019-09-23 11:05:34 -06001393 if (file->f_op->read_iter)
1394 ret2 = call_read_iter(file, kiocb, &iter);
1395 else
1396 ret2 = loop_rw_iter(READ, file, kiocb, &iter);
1397
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001398 /*
1399 * In case of a short read, punt to async. This can happen
1400 * if we have data partially cached. Alternatively we can
1401 * return the short read, in which case the application will
1402 * need to issue another SQE and wait for it. That SQE will
1403 * need async punt anyway, so it's more efficient to do it
1404 * here.
1405 */
Jens Axboe491381ce2019-10-17 09:20:46 -06001406 if (force_nonblock && !(req->flags & REQ_F_NOWAIT) &&
1407 (req->flags & REQ_F_ISREG) &&
1408 ret2 > 0 && ret2 < read_size)
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001409 ret2 = -EAGAIN;
1410 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboe31b51512019-01-18 22:56:34 -07001411 if (!force_nonblock || ret2 != -EAGAIN) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001412 io_rw_done(kiocb, ret2);
Jens Axboe31b51512019-01-18 22:56:34 -07001413 } else {
1414 /*
1415 * If ->needs_lock is true, we're already in async
1416 * context.
1417 */
1418 if (!s->needs_lock)
1419 io_async_list_note(READ, req, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001420 ret = -EAGAIN;
Jens Axboe31b51512019-01-18 22:56:34 -07001421 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001422 }
1423 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001424 return ret;
1425}
1426
Jens Axboee0c5c572019-03-12 10:18:47 -06001427static int io_write(struct io_kiocb *req, const struct sqe_submit *s,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001428 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001429{
1430 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1431 struct kiocb *kiocb = &req->rw;
1432 struct iov_iter iter;
1433 struct file *file;
Jens Axboe31b51512019-01-18 22:56:34 -07001434 size_t iov_count;
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001435 ssize_t ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001436
Jens Axboe8358e3a2019-04-23 08:17:58 -06001437 ret = io_prep_rw(req, s, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001438 if (ret)
1439 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001440
Jens Axboe2b188cc2019-01-07 10:46:33 -07001441 file = kiocb->ki_filp;
1442 if (unlikely(!(file->f_mode & FMODE_WRITE)))
Jens Axboe09bb8392019-03-13 12:39:28 -06001443 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001444
1445 ret = io_import_iovec(req->ctx, WRITE, s, &iovec, &iter);
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001446 if (ret < 0)
Jens Axboe09bb8392019-03-13 12:39:28 -06001447 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001448
Jens Axboe9e645e112019-05-10 16:07:28 -06001449 if (req->flags & REQ_F_LINK)
1450 req->result = ret;
1451
Jens Axboe31b51512019-01-18 22:56:34 -07001452 iov_count = iov_iter_count(&iter);
1453
1454 ret = -EAGAIN;
1455 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT)) {
1456 /* If ->needs_lock is true, we're already in async context. */
1457 if (!s->needs_lock)
1458 io_async_list_note(WRITE, req, iov_count);
1459 goto out_free;
1460 }
1461
1462 ret = rw_verify_area(WRITE, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001463 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01001464 ssize_t ret2;
1465
Jens Axboe2b188cc2019-01-07 10:46:33 -07001466 /*
1467 * Open-code file_start_write here to grab freeze protection,
1468 * which will be released by another thread in
1469 * io_complete_rw(). Fool lockdep by telling it the lock got
1470 * released so that it doesn't complain about the held lock when
1471 * we return to userspace.
1472 */
Jens Axboe491381ce2019-10-17 09:20:46 -06001473 if (req->flags & REQ_F_ISREG) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001474 __sb_start_write(file_inode(file)->i_sb,
1475 SB_FREEZE_WRITE, true);
1476 __sb_writers_release(file_inode(file)->i_sb,
1477 SB_FREEZE_WRITE);
1478 }
1479 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01001480
Jens Axboe32960612019-09-23 11:05:34 -06001481 if (file->f_op->write_iter)
1482 ret2 = call_write_iter(file, kiocb, &iter);
1483 else
1484 ret2 = loop_rw_iter(WRITE, file, kiocb, &iter);
Roman Penyaev9bf79332019-03-25 20:09:24 +01001485 if (!force_nonblock || ret2 != -EAGAIN) {
1486 io_rw_done(kiocb, ret2);
1487 } else {
1488 /*
1489 * If ->needs_lock is true, we're already in async
1490 * context.
1491 */
1492 if (!s->needs_lock)
1493 io_async_list_note(WRITE, req, iov_count);
1494 ret = -EAGAIN;
1495 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001496 }
Jens Axboe31b51512019-01-18 22:56:34 -07001497out_free:
Jens Axboe2b188cc2019-01-07 10:46:33 -07001498 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001499 return ret;
1500}
1501
1502/*
1503 * IORING_OP_NOP just posts a completion event, nothing else.
1504 */
1505static int io_nop(struct io_kiocb *req, u64 user_data)
1506{
1507 struct io_ring_ctx *ctx = req->ctx;
1508 long err = 0;
1509
Jens Axboedef596e2019-01-09 08:59:42 -07001510 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1511 return -EINVAL;
1512
Jens Axboec71ffb62019-05-13 20:58:29 -06001513 io_cqring_add_event(ctx, user_data, err);
Jens Axboee65ef562019-03-12 10:16:44 -06001514 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001515 return 0;
1516}
1517
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001518static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1519{
Jens Axboe6b063142019-01-10 22:13:58 -07001520 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001521
Jens Axboe09bb8392019-03-13 12:39:28 -06001522 if (!req->file)
1523 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001524
Jens Axboe6b063142019-01-10 22:13:58 -07001525 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07001526 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07001527 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001528 return -EINVAL;
1529
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001530 return 0;
1531}
1532
1533static int io_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1534 bool force_nonblock)
1535{
1536 loff_t sqe_off = READ_ONCE(sqe->off);
1537 loff_t sqe_len = READ_ONCE(sqe->len);
1538 loff_t end = sqe_off + sqe_len;
1539 unsigned fsync_flags;
1540 int ret;
1541
1542 fsync_flags = READ_ONCE(sqe->fsync_flags);
1543 if (unlikely(fsync_flags & ~IORING_FSYNC_DATASYNC))
1544 return -EINVAL;
1545
1546 ret = io_prep_fsync(req, sqe);
1547 if (ret)
1548 return ret;
1549
1550 /* fsync always requires a blocking context */
1551 if (force_nonblock)
1552 return -EAGAIN;
1553
1554 ret = vfs_fsync_range(req->rw.ki_filp, sqe_off,
1555 end > 0 ? end : LLONG_MAX,
1556 fsync_flags & IORING_FSYNC_DATASYNC);
1557
Jens Axboe9e645e112019-05-10 16:07:28 -06001558 if (ret < 0 && (req->flags & REQ_F_LINK))
1559 req->flags |= REQ_F_FAIL_LINK;
Jens Axboec71ffb62019-05-13 20:58:29 -06001560 io_cqring_add_event(req->ctx, sqe->user_data, ret);
Jens Axboee65ef562019-03-12 10:16:44 -06001561 io_put_req(req);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001562 return 0;
1563}
1564
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001565static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1566{
1567 struct io_ring_ctx *ctx = req->ctx;
1568 int ret = 0;
1569
1570 if (!req->file)
1571 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001572
1573 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1574 return -EINVAL;
1575 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
1576 return -EINVAL;
1577
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001578 return ret;
1579}
1580
1581static int io_sync_file_range(struct io_kiocb *req,
1582 const struct io_uring_sqe *sqe,
1583 bool force_nonblock)
1584{
1585 loff_t sqe_off;
1586 loff_t sqe_len;
1587 unsigned flags;
1588 int ret;
1589
1590 ret = io_prep_sfr(req, sqe);
1591 if (ret)
1592 return ret;
1593
1594 /* sync_file_range always requires a blocking context */
1595 if (force_nonblock)
1596 return -EAGAIN;
1597
1598 sqe_off = READ_ONCE(sqe->off);
1599 sqe_len = READ_ONCE(sqe->len);
1600 flags = READ_ONCE(sqe->sync_range_flags);
1601
1602 ret = sync_file_range(req->rw.ki_filp, sqe_off, sqe_len, flags);
1603
Jens Axboe9e645e112019-05-10 16:07:28 -06001604 if (ret < 0 && (req->flags & REQ_F_LINK))
1605 req->flags |= REQ_F_FAIL_LINK;
Jens Axboec71ffb62019-05-13 20:58:29 -06001606 io_cqring_add_event(req->ctx, sqe->user_data, ret);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001607 io_put_req(req);
1608 return 0;
1609}
1610
Jens Axboe0fa03c62019-04-19 13:34:07 -06001611#if defined(CONFIG_NET)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001612static int io_send_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1613 bool force_nonblock,
1614 long (*fn)(struct socket *, struct user_msghdr __user *,
1615 unsigned int))
1616{
Jens Axboe0fa03c62019-04-19 13:34:07 -06001617 struct socket *sock;
1618 int ret;
1619
1620 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1621 return -EINVAL;
1622
1623 sock = sock_from_file(req->file, &ret);
1624 if (sock) {
1625 struct user_msghdr __user *msg;
1626 unsigned flags;
1627
1628 flags = READ_ONCE(sqe->msg_flags);
1629 if (flags & MSG_DONTWAIT)
1630 req->flags |= REQ_F_NOWAIT;
1631 else if (force_nonblock)
1632 flags |= MSG_DONTWAIT;
1633
1634 msg = (struct user_msghdr __user *) (unsigned long)
1635 READ_ONCE(sqe->addr);
1636
Jens Axboeaa1fa282019-04-19 13:38:09 -06001637 ret = fn(sock, msg, flags);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001638 if (force_nonblock && ret == -EAGAIN)
1639 return ret;
1640 }
1641
1642 io_cqring_add_event(req->ctx, sqe->user_data, ret);
1643 io_put_req(req);
1644 return 0;
Jens Axboeaa1fa282019-04-19 13:38:09 -06001645}
1646#endif
1647
1648static int io_sendmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1649 bool force_nonblock)
1650{
1651#if defined(CONFIG_NET)
1652 return io_send_recvmsg(req, sqe, force_nonblock, __sys_sendmsg_sock);
1653#else
1654 return -EOPNOTSUPP;
1655#endif
1656}
1657
1658static int io_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1659 bool force_nonblock)
1660{
1661#if defined(CONFIG_NET)
1662 return io_send_recvmsg(req, sqe, force_nonblock, __sys_recvmsg_sock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001663#else
1664 return -EOPNOTSUPP;
1665#endif
1666}
1667
Jens Axboe221c5eb2019-01-17 09:41:58 -07001668static void io_poll_remove_one(struct io_kiocb *req)
1669{
1670 struct io_poll_iocb *poll = &req->poll;
1671
1672 spin_lock(&poll->head->lock);
1673 WRITE_ONCE(poll->canceled, true);
1674 if (!list_empty(&poll->wait.entry)) {
1675 list_del_init(&poll->wait.entry);
Jens Axboe18d9be12019-09-10 09:13:05 -06001676 io_queue_async_work(req->ctx, req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001677 }
1678 spin_unlock(&poll->head->lock);
1679
1680 list_del_init(&req->list);
1681}
1682
1683static void io_poll_remove_all(struct io_ring_ctx *ctx)
1684{
1685 struct io_kiocb *req;
1686
1687 spin_lock_irq(&ctx->completion_lock);
1688 while (!list_empty(&ctx->cancel_list)) {
1689 req = list_first_entry(&ctx->cancel_list, struct io_kiocb,list);
1690 io_poll_remove_one(req);
1691 }
1692 spin_unlock_irq(&ctx->completion_lock);
1693}
1694
1695/*
1696 * Find a running poll command that matches one specified in sqe->addr,
1697 * and remove it if found.
1698 */
1699static int io_poll_remove(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1700{
1701 struct io_ring_ctx *ctx = req->ctx;
1702 struct io_kiocb *poll_req, *next;
1703 int ret = -ENOENT;
1704
1705 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1706 return -EINVAL;
1707 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
1708 sqe->poll_events)
1709 return -EINVAL;
1710
1711 spin_lock_irq(&ctx->completion_lock);
1712 list_for_each_entry_safe(poll_req, next, &ctx->cancel_list, list) {
1713 if (READ_ONCE(sqe->addr) == poll_req->user_data) {
1714 io_poll_remove_one(poll_req);
1715 ret = 0;
1716 break;
1717 }
1718 }
1719 spin_unlock_irq(&ctx->completion_lock);
1720
Jens Axboec71ffb62019-05-13 20:58:29 -06001721 io_cqring_add_event(req->ctx, sqe->user_data, ret);
Jens Axboee65ef562019-03-12 10:16:44 -06001722 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001723 return 0;
1724}
1725
Jens Axboe8c838782019-03-12 15:48:16 -06001726static void io_poll_complete(struct io_ring_ctx *ctx, struct io_kiocb *req,
1727 __poll_t mask)
Jens Axboe221c5eb2019-01-17 09:41:58 -07001728{
Jens Axboe8c838782019-03-12 15:48:16 -06001729 req->poll.done = true;
Jens Axboec71ffb62019-05-13 20:58:29 -06001730 io_cqring_fill_event(ctx, req->user_data, mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06001731 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001732}
1733
1734static void io_poll_complete_work(struct work_struct *work)
1735{
1736 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
1737 struct io_poll_iocb *poll = &req->poll;
1738 struct poll_table_struct pt = { ._key = poll->events };
1739 struct io_ring_ctx *ctx = req->ctx;
1740 __poll_t mask = 0;
1741
1742 if (!READ_ONCE(poll->canceled))
1743 mask = vfs_poll(poll->file, &pt) & poll->events;
1744
1745 /*
1746 * Note that ->ki_cancel callers also delete iocb from active_reqs after
1747 * calling ->ki_cancel. We need the ctx_lock roundtrip here to
1748 * synchronize with them. In the cancellation case the list_del_init
1749 * itself is not actually needed, but harmless so we keep it in to
1750 * avoid further branches in the fast path.
1751 */
1752 spin_lock_irq(&ctx->completion_lock);
1753 if (!mask && !READ_ONCE(poll->canceled)) {
1754 add_wait_queue(poll->head, &poll->wait);
1755 spin_unlock_irq(&ctx->completion_lock);
1756 return;
1757 }
1758 list_del_init(&req->list);
Jens Axboe8c838782019-03-12 15:48:16 -06001759 io_poll_complete(ctx, req, mask);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001760 spin_unlock_irq(&ctx->completion_lock);
1761
Jens Axboe8c838782019-03-12 15:48:16 -06001762 io_cqring_ev_posted(ctx);
1763 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001764}
1765
1766static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
1767 void *key)
1768{
1769 struct io_poll_iocb *poll = container_of(wait, struct io_poll_iocb,
1770 wait);
1771 struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
1772 struct io_ring_ctx *ctx = req->ctx;
1773 __poll_t mask = key_to_poll(key);
Jens Axboe8c838782019-03-12 15:48:16 -06001774 unsigned long flags;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001775
1776 /* for instances that support it check for an event match first: */
Jens Axboe8c838782019-03-12 15:48:16 -06001777 if (mask && !(mask & poll->events))
1778 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001779
1780 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06001781
1782 if (mask && spin_trylock_irqsave(&ctx->completion_lock, flags)) {
1783 list_del(&req->list);
1784 io_poll_complete(ctx, req, mask);
1785 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1786
1787 io_cqring_ev_posted(ctx);
1788 io_put_req(req);
1789 } else {
Jens Axboe18d9be12019-09-10 09:13:05 -06001790 io_queue_async_work(ctx, req);
Jens Axboe8c838782019-03-12 15:48:16 -06001791 }
1792
Jens Axboe221c5eb2019-01-17 09:41:58 -07001793 return 1;
1794}
1795
1796struct io_poll_table {
1797 struct poll_table_struct pt;
1798 struct io_kiocb *req;
1799 int error;
1800};
1801
1802static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
1803 struct poll_table_struct *p)
1804{
1805 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
1806
1807 if (unlikely(pt->req->poll.head)) {
1808 pt->error = -EINVAL;
1809 return;
1810 }
1811
1812 pt->error = 0;
1813 pt->req->poll.head = head;
1814 add_wait_queue(head, &pt->req->poll.wait);
1815}
1816
1817static int io_poll_add(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1818{
1819 struct io_poll_iocb *poll = &req->poll;
1820 struct io_ring_ctx *ctx = req->ctx;
1821 struct io_poll_table ipt;
Jens Axboe8c838782019-03-12 15:48:16 -06001822 bool cancel = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001823 __poll_t mask;
1824 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001825
1826 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1827 return -EINVAL;
1828 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
1829 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06001830 if (!poll->file)
1831 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001832
Jens Axboe6cc47d12019-09-18 11:18:23 -06001833 req->submit.sqe = NULL;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001834 INIT_WORK(&req->work, io_poll_complete_work);
1835 events = READ_ONCE(sqe->poll_events);
1836 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
1837
Jens Axboe221c5eb2019-01-17 09:41:58 -07001838 poll->head = NULL;
Jens Axboe8c838782019-03-12 15:48:16 -06001839 poll->done = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001840 poll->canceled = false;
1841
1842 ipt.pt._qproc = io_poll_queue_proc;
1843 ipt.pt._key = poll->events;
1844 ipt.req = req;
1845 ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
1846
1847 /* initialized the list so that we can do list_empty checks */
1848 INIT_LIST_HEAD(&poll->wait.entry);
1849 init_waitqueue_func_entry(&poll->wait, io_poll_wake);
1850
Jens Axboe36703242019-07-25 10:20:18 -06001851 INIT_LIST_HEAD(&req->list);
1852
Jens Axboe221c5eb2019-01-17 09:41:58 -07001853 mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001854
1855 spin_lock_irq(&ctx->completion_lock);
Jens Axboe8c838782019-03-12 15:48:16 -06001856 if (likely(poll->head)) {
1857 spin_lock(&poll->head->lock);
1858 if (unlikely(list_empty(&poll->wait.entry))) {
1859 if (ipt.error)
1860 cancel = true;
1861 ipt.error = 0;
1862 mask = 0;
1863 }
1864 if (mask || ipt.error)
1865 list_del_init(&poll->wait.entry);
1866 else if (cancel)
1867 WRITE_ONCE(poll->canceled, true);
1868 else if (!poll->done) /* actually waiting for an event */
1869 list_add_tail(&req->list, &ctx->cancel_list);
1870 spin_unlock(&poll->head->lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001871 }
Jens Axboe8c838782019-03-12 15:48:16 -06001872 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06001873 ipt.error = 0;
1874 io_poll_complete(ctx, req, mask);
1875 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07001876 spin_unlock_irq(&ctx->completion_lock);
1877
Jens Axboe8c838782019-03-12 15:48:16 -06001878 if (mask) {
1879 io_cqring_ev_posted(ctx);
Jens Axboee65ef562019-03-12 10:16:44 -06001880 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001881 }
Jens Axboe8c838782019-03-12 15:48:16 -06001882 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001883}
1884
Jens Axboe5262f562019-09-17 12:26:57 -06001885static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
1886{
1887 struct io_ring_ctx *ctx;
zhangyi (F)ef036812019-10-23 15:10:08 +08001888 struct io_kiocb *req, *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06001889 unsigned long flags;
1890
1891 req = container_of(timer, struct io_kiocb, timeout.timer);
1892 ctx = req->ctx;
1893 atomic_inc(&ctx->cq_timeouts);
1894
1895 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08001896 /*
1897 * Adjust the reqs sequence before the current one because it
1898 * will consume a slot in the cq_ring and the the cq_tail pointer
1899 * will be increased, otherwise other timeout reqs may return in
1900 * advance without waiting for enough wait_nr.
1901 */
1902 prev = req;
1903 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
1904 prev->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06001905 list_del(&req->list);
1906
1907 io_cqring_fill_event(ctx, req->user_data, -ETIME);
1908 io_commit_cqring(ctx);
1909 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1910
1911 io_cqring_ev_posted(ctx);
1912
1913 io_put_req(req);
1914 return HRTIMER_NORESTART;
1915}
1916
1917static int io_timeout(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1918{
yangerkun5da0fb12019-10-15 21:59:29 +08001919 unsigned count;
Jens Axboe5262f562019-09-17 12:26:57 -06001920 struct io_ring_ctx *ctx = req->ctx;
1921 struct list_head *entry;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06001922 struct timespec64 ts;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08001923 unsigned span = 0;
Jens Axboe5262f562019-09-17 12:26:57 -06001924
1925 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1926 return -EINVAL;
1927 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->timeout_flags ||
1928 sqe->len != 1)
1929 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06001930
1931 if (get_timespec64(&ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06001932 return -EFAULT;
1933
1934 /*
1935 * sqe->off holds how many events that need to occur for this
1936 * timeout event to be satisfied.
1937 */
1938 count = READ_ONCE(sqe->off);
1939 if (!count)
1940 count = 1;
1941
1942 req->sequence = ctx->cached_sq_head + count - 1;
yangerkun5da0fb12019-10-15 21:59:29 +08001943 /* reuse it to store the count */
1944 req->submit.sequence = count;
Jens Axboe5262f562019-09-17 12:26:57 -06001945 req->flags |= REQ_F_TIMEOUT;
1946
1947 /*
1948 * Insertion sort, ensuring the first entry in the list is always
1949 * the one we need first.
1950 */
Jens Axboe5262f562019-09-17 12:26:57 -06001951 spin_lock_irq(&ctx->completion_lock);
1952 list_for_each_prev(entry, &ctx->timeout_list) {
1953 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
yangerkun5da0fb12019-10-15 21:59:29 +08001954 unsigned nxt_sq_head;
1955 long long tmp, tmp_nxt;
Jens Axboe5262f562019-09-17 12:26:57 -06001956
yangerkun5da0fb12019-10-15 21:59:29 +08001957 /*
1958 * Since cached_sq_head + count - 1 can overflow, use type long
1959 * long to store it.
1960 */
1961 tmp = (long long)ctx->cached_sq_head + count - 1;
1962 nxt_sq_head = nxt->sequence - nxt->submit.sequence + 1;
1963 tmp_nxt = (long long)nxt_sq_head + nxt->submit.sequence - 1;
1964
1965 /*
1966 * cached_sq_head may overflow, and it will never overflow twice
1967 * once there is some timeout req still be valid.
1968 */
1969 if (ctx->cached_sq_head < nxt_sq_head)
yangerkun8b07a652019-10-17 12:12:35 +08001970 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08001971
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08001972 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06001973 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08001974
1975 /*
1976 * Sequence of reqs after the insert one and itself should
1977 * be adjusted because each timeout req consumes a slot.
1978 */
1979 span++;
1980 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06001981 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08001982 req->sequence -= span;
Jens Axboe5262f562019-09-17 12:26:57 -06001983 list_add(&req->list, entry);
1984 spin_unlock_irq(&ctx->completion_lock);
1985
1986 hrtimer_init(&req->timeout.timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1987 req->timeout.timer.function = io_timeout_fn;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06001988 hrtimer_start(&req->timeout.timer, timespec64_to_ktime(ts),
Jens Axboe5262f562019-09-17 12:26:57 -06001989 HRTIMER_MODE_REL);
1990 return 0;
1991}
1992
Jens Axboede0617e2019-04-06 21:51:27 -06001993static int io_req_defer(struct io_ring_ctx *ctx, struct io_kiocb *req,
1994 const struct io_uring_sqe *sqe)
1995{
1996 struct io_uring_sqe *sqe_copy;
1997
1998 if (!io_sequence_defer(ctx, req) && list_empty(&ctx->defer_list))
1999 return 0;
2000
2001 sqe_copy = kmalloc(sizeof(*sqe_copy), GFP_KERNEL);
2002 if (!sqe_copy)
2003 return -EAGAIN;
2004
2005 spin_lock_irq(&ctx->completion_lock);
2006 if (!io_sequence_defer(ctx, req) && list_empty(&ctx->defer_list)) {
2007 spin_unlock_irq(&ctx->completion_lock);
2008 kfree(sqe_copy);
2009 return 0;
2010 }
2011
2012 memcpy(sqe_copy, sqe, sizeof(*sqe_copy));
2013 req->submit.sqe = sqe_copy;
2014
2015 INIT_WORK(&req->work, io_sq_wq_submit_work);
2016 list_add_tail(&req->list, &ctx->defer_list);
2017 spin_unlock_irq(&ctx->completion_lock);
2018 return -EIOCBQUEUED;
2019}
2020
Jens Axboe2b188cc2019-01-07 10:46:33 -07002021static int __io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
Jens Axboe8358e3a2019-04-23 08:17:58 -06002022 const struct sqe_submit *s, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002023{
Jens Axboee0c5c572019-03-12 10:18:47 -06002024 int ret, opcode;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002025
Jens Axboe9e645e112019-05-10 16:07:28 -06002026 req->user_data = READ_ONCE(s->sqe->user_data);
2027
Jens Axboe2b188cc2019-01-07 10:46:33 -07002028 if (unlikely(s->index >= ctx->sq_entries))
2029 return -EINVAL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002030
2031 opcode = READ_ONCE(s->sqe->opcode);
2032 switch (opcode) {
2033 case IORING_OP_NOP:
2034 ret = io_nop(req, req->user_data);
2035 break;
2036 case IORING_OP_READV:
Jens Axboeedafcce2019-01-09 09:16:05 -07002037 if (unlikely(s->sqe->buf_index))
2038 return -EINVAL;
Jens Axboe8358e3a2019-04-23 08:17:58 -06002039 ret = io_read(req, s, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002040 break;
2041 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07002042 if (unlikely(s->sqe->buf_index))
2043 return -EINVAL;
Jens Axboe8358e3a2019-04-23 08:17:58 -06002044 ret = io_write(req, s, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07002045 break;
2046 case IORING_OP_READ_FIXED:
Jens Axboe8358e3a2019-04-23 08:17:58 -06002047 ret = io_read(req, s, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07002048 break;
2049 case IORING_OP_WRITE_FIXED:
Jens Axboe8358e3a2019-04-23 08:17:58 -06002050 ret = io_write(req, s, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002051 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002052 case IORING_OP_FSYNC:
2053 ret = io_fsync(req, s->sqe, force_nonblock);
2054 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002055 case IORING_OP_POLL_ADD:
2056 ret = io_poll_add(req, s->sqe);
2057 break;
2058 case IORING_OP_POLL_REMOVE:
2059 ret = io_poll_remove(req, s->sqe);
2060 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002061 case IORING_OP_SYNC_FILE_RANGE:
2062 ret = io_sync_file_range(req, s->sqe, force_nonblock);
2063 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002064 case IORING_OP_SENDMSG:
2065 ret = io_sendmsg(req, s->sqe, force_nonblock);
2066 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06002067 case IORING_OP_RECVMSG:
2068 ret = io_recvmsg(req, s->sqe, force_nonblock);
2069 break;
Jens Axboe5262f562019-09-17 12:26:57 -06002070 case IORING_OP_TIMEOUT:
2071 ret = io_timeout(req, s->sqe);
2072 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002073 default:
2074 ret = -EINVAL;
2075 break;
2076 }
2077
Jens Axboedef596e2019-01-09 08:59:42 -07002078 if (ret)
2079 return ret;
2080
2081 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe9e645e112019-05-10 16:07:28 -06002082 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07002083 return -EAGAIN;
2084
2085 /* workqueue context doesn't hold uring_lock, grab it now */
2086 if (s->needs_lock)
2087 mutex_lock(&ctx->uring_lock);
2088 io_iopoll_req_issued(req);
2089 if (s->needs_lock)
2090 mutex_unlock(&ctx->uring_lock);
2091 }
2092
2093 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002094}
2095
Jens Axboe31b51512019-01-18 22:56:34 -07002096static struct async_list *io_async_list_from_sqe(struct io_ring_ctx *ctx,
2097 const struct io_uring_sqe *sqe)
2098{
2099 switch (sqe->opcode) {
2100 case IORING_OP_READV:
2101 case IORING_OP_READ_FIXED:
2102 return &ctx->pending_async[READ];
2103 case IORING_OP_WRITEV:
2104 case IORING_OP_WRITE_FIXED:
2105 return &ctx->pending_async[WRITE];
2106 default:
2107 return NULL;
2108 }
2109}
2110
Jens Axboeedafcce2019-01-09 09:16:05 -07002111static inline bool io_sqe_needs_user(const struct io_uring_sqe *sqe)
2112{
2113 u8 opcode = READ_ONCE(sqe->opcode);
2114
2115 return !(opcode == IORING_OP_READ_FIXED ||
2116 opcode == IORING_OP_WRITE_FIXED);
2117}
2118
Jens Axboe2b188cc2019-01-07 10:46:33 -07002119static void io_sq_wq_submit_work(struct work_struct *work)
2120{
2121 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002122 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe31b51512019-01-18 22:56:34 -07002123 struct mm_struct *cur_mm = NULL;
2124 struct async_list *async_list;
2125 LIST_HEAD(req_list);
Jens Axboeedafcce2019-01-09 09:16:05 -07002126 mm_segment_t old_fs;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002127 int ret;
2128
Jens Axboe31b51512019-01-18 22:56:34 -07002129 async_list = io_async_list_from_sqe(ctx, req->submit.sqe);
2130restart:
2131 do {
2132 struct sqe_submit *s = &req->submit;
2133 const struct io_uring_sqe *sqe = s->sqe;
Jackie Liud0ee8792019-07-31 14:39:33 +08002134 unsigned int flags = req->flags;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002135
Stefan Bühler8449eed2019-04-27 20:34:19 +02002136 /* Ensure we clear previously set non-block flag */
Jens Axboe31b51512019-01-18 22:56:34 -07002137 req->rw.ki_flags &= ~IOCB_NOWAIT;
2138
2139 ret = 0;
2140 if (io_sqe_needs_user(sqe) && !cur_mm) {
2141 if (!mmget_not_zero(ctx->sqo_mm)) {
2142 ret = -EFAULT;
2143 } else {
2144 cur_mm = ctx->sqo_mm;
2145 use_mm(cur_mm);
2146 old_fs = get_fs();
2147 set_fs(USER_DS);
2148 }
2149 }
2150
2151 if (!ret) {
2152 s->has_user = cur_mm != NULL;
2153 s->needs_lock = true;
2154 do {
Jens Axboe8358e3a2019-04-23 08:17:58 -06002155 ret = __io_submit_sqe(ctx, req, s, false);
Jens Axboe31b51512019-01-18 22:56:34 -07002156 /*
2157 * We can get EAGAIN for polled IO even though
2158 * we're forcing a sync submission from here,
2159 * since we can't wait for request slots on the
2160 * block side.
2161 */
2162 if (ret != -EAGAIN)
2163 break;
2164 cond_resched();
2165 } while (1);
2166 }
Jens Axboe817869d2019-04-30 14:44:05 -06002167
2168 /* drop submission reference */
2169 io_put_req(req);
2170
Jens Axboe31b51512019-01-18 22:56:34 -07002171 if (ret) {
Jens Axboec71ffb62019-05-13 20:58:29 -06002172 io_cqring_add_event(ctx, sqe->user_data, ret);
Jens Axboee65ef562019-03-12 10:16:44 -06002173 io_put_req(req);
Jens Axboe31b51512019-01-18 22:56:34 -07002174 }
2175
2176 /* async context always use a copy of the sqe */
2177 kfree(sqe);
2178
Zhengyuan Liuf7b76ac2019-07-16 23:26:14 +08002179 /* req from defer and link list needn't decrease async cnt */
Jackie Liud0ee8792019-07-31 14:39:33 +08002180 if (flags & (REQ_F_IO_DRAINED | REQ_F_LINK_DONE))
Zhengyuan Liuf7b76ac2019-07-16 23:26:14 +08002181 goto out;
2182
Jens Axboe31b51512019-01-18 22:56:34 -07002183 if (!async_list)
2184 break;
2185 if (!list_empty(&req_list)) {
2186 req = list_first_entry(&req_list, struct io_kiocb,
2187 list);
2188 list_del(&req->list);
2189 continue;
2190 }
2191 if (list_empty(&async_list->list))
2192 break;
2193
2194 req = NULL;
2195 spin_lock(&async_list->lock);
2196 if (list_empty(&async_list->list)) {
2197 spin_unlock(&async_list->lock);
2198 break;
2199 }
2200 list_splice_init(&async_list->list, &req_list);
2201 spin_unlock(&async_list->lock);
2202
2203 req = list_first_entry(&req_list, struct io_kiocb, list);
2204 list_del(&req->list);
2205 } while (req);
Jens Axboeedafcce2019-01-09 09:16:05 -07002206
2207 /*
Jens Axboe31b51512019-01-18 22:56:34 -07002208 * Rare case of racing with a submitter. If we find the count has
2209 * dropped to zero AND we have pending work items, then restart
2210 * the processing. This is a tiny race window.
Jens Axboeedafcce2019-01-09 09:16:05 -07002211 */
Jens Axboe31b51512019-01-18 22:56:34 -07002212 if (async_list) {
2213 ret = atomic_dec_return(&async_list->cnt);
2214 while (!ret && !list_empty(&async_list->list)) {
2215 spin_lock(&async_list->lock);
2216 atomic_inc(&async_list->cnt);
2217 list_splice_init(&async_list->list, &req_list);
2218 spin_unlock(&async_list->lock);
2219
2220 if (!list_empty(&req_list)) {
2221 req = list_first_entry(&req_list,
2222 struct io_kiocb, list);
2223 list_del(&req->list);
2224 goto restart;
2225 }
2226 ret = atomic_dec_return(&async_list->cnt);
Jens Axboeedafcce2019-01-09 09:16:05 -07002227 }
Jens Axboeedafcce2019-01-09 09:16:05 -07002228 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002229
Zhengyuan Liuf7b76ac2019-07-16 23:26:14 +08002230out:
Jens Axboe31b51512019-01-18 22:56:34 -07002231 if (cur_mm) {
Jens Axboeedafcce2019-01-09 09:16:05 -07002232 set_fs(old_fs);
Jens Axboe31b51512019-01-18 22:56:34 -07002233 unuse_mm(cur_mm);
2234 mmput(cur_mm);
Jens Axboeedafcce2019-01-09 09:16:05 -07002235 }
Jens Axboe31b51512019-01-18 22:56:34 -07002236}
Jens Axboe2b188cc2019-01-07 10:46:33 -07002237
Jens Axboe31b51512019-01-18 22:56:34 -07002238/*
2239 * See if we can piggy back onto previously submitted work, that is still
2240 * running. We currently only allow this if the new request is sequential
2241 * to the previous one we punted.
2242 */
2243static bool io_add_to_prev_work(struct async_list *list, struct io_kiocb *req)
2244{
Jens Axboe6d5d5ac2019-09-11 10:16:13 -06002245 bool ret;
Jens Axboe31b51512019-01-18 22:56:34 -07002246
2247 if (!list)
2248 return false;
2249 if (!(req->flags & REQ_F_SEQ_PREV))
2250 return false;
2251 if (!atomic_read(&list->cnt))
2252 return false;
2253
2254 ret = true;
2255 spin_lock(&list->lock);
2256 list_add_tail(&req->list, &list->list);
Zhengyuan Liuc0e48f92019-07-18 20:44:00 +08002257 /*
2258 * Ensure we see a simultaneous modification from io_sq_wq_submit_work()
2259 */
2260 smp_mb();
Jens Axboe31b51512019-01-18 22:56:34 -07002261 if (!atomic_read(&list->cnt)) {
2262 list_del_init(&req->list);
2263 ret = false;
2264 }
2265 spin_unlock(&list->lock);
2266 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002267}
2268
Jens Axboe09bb8392019-03-13 12:39:28 -06002269static bool io_op_needs_file(const struct io_uring_sqe *sqe)
2270{
2271 int op = READ_ONCE(sqe->opcode);
2272
2273 switch (op) {
2274 case IORING_OP_NOP:
2275 case IORING_OP_POLL_REMOVE:
2276 return false;
2277 default:
2278 return true;
2279 }
2280}
2281
2282static int io_req_set_file(struct io_ring_ctx *ctx, const struct sqe_submit *s,
2283 struct io_submit_state *state, struct io_kiocb *req)
2284{
2285 unsigned flags;
2286 int fd;
2287
2288 flags = READ_ONCE(s->sqe->flags);
2289 fd = READ_ONCE(s->sqe->fd);
2290
Jackie Liu4fe2c962019-09-09 20:50:40 +08002291 if (flags & IOSQE_IO_DRAIN)
Jens Axboede0617e2019-04-06 21:51:27 -06002292 req->flags |= REQ_F_IO_DRAIN;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002293 /*
2294 * All io need record the previous position, if LINK vs DARIN,
2295 * it can be used to mark the position of the first IO in the
2296 * link list.
2297 */
2298 req->sequence = s->sequence;
Jens Axboede0617e2019-04-06 21:51:27 -06002299
Jens Axboe60c112b2019-06-21 10:20:18 -06002300 if (!io_op_needs_file(s->sqe))
Jens Axboe09bb8392019-03-13 12:39:28 -06002301 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06002302
2303 if (flags & IOSQE_FIXED_FILE) {
2304 if (unlikely(!ctx->user_files ||
2305 (unsigned) fd >= ctx->nr_user_files))
2306 return -EBADF;
2307 req->file = ctx->user_files[fd];
2308 req->flags |= REQ_F_FIXED_FILE;
2309 } else {
2310 if (s->needs_fixed_file)
2311 return -EBADF;
2312 req->file = io_file_get(state, fd);
2313 if (unlikely(!req->file))
2314 return -EBADF;
2315 }
2316
2317 return 0;
2318}
2319
Jackie Liu4fe2c962019-09-09 20:50:40 +08002320static int __io_queue_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
Jens Axboebc808bc2019-10-22 13:14:37 -06002321 struct sqe_submit *s)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002322{
Jens Axboee0c5c572019-03-12 10:18:47 -06002323 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002324
Jens Axboebc808bc2019-10-22 13:14:37 -06002325 ret = __io_submit_sqe(ctx, req, s, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06002326
2327 /*
2328 * We async punt it if the file wasn't marked NOWAIT, or if the file
2329 * doesn't support non-blocking read/write attempts
2330 */
2331 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
2332 (req->flags & REQ_F_MUST_PUNT))) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07002333 struct io_uring_sqe *sqe_copy;
2334
Jackie Liu954dab12019-09-18 10:37:52 +08002335 sqe_copy = kmemdup(s->sqe, sizeof(*sqe_copy), GFP_KERNEL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002336 if (sqe_copy) {
Jens Axboe31b51512019-01-18 22:56:34 -07002337 struct async_list *list;
2338
Jens Axboe2b188cc2019-01-07 10:46:33 -07002339 s->sqe = sqe_copy;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002340 memcpy(&req->submit, s, sizeof(*s));
Jens Axboe31b51512019-01-18 22:56:34 -07002341 list = io_async_list_from_sqe(ctx, s->sqe);
2342 if (!io_add_to_prev_work(list, req)) {
2343 if (list)
2344 atomic_inc(&list->cnt);
2345 INIT_WORK(&req->work, io_sq_wq_submit_work);
Jens Axboe18d9be12019-09-10 09:13:05 -06002346 io_queue_async_work(ctx, req);
Jens Axboe31b51512019-01-18 22:56:34 -07002347 }
Jens Axboee65ef562019-03-12 10:16:44 -06002348
2349 /*
2350 * Queued up for async execution, worker will release
Jens Axboe9e645e112019-05-10 16:07:28 -06002351 * submit reference when the iocb is actually submitted.
Jens Axboee65ef562019-03-12 10:16:44 -06002352 */
2353 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002354 }
2355 }
Jens Axboee65ef562019-03-12 10:16:44 -06002356
2357 /* drop submission reference */
2358 io_put_req(req);
2359
2360 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06002361 if (ret) {
2362 io_cqring_add_event(ctx, req->user_data, ret);
2363 if (req->flags & REQ_F_LINK)
2364 req->flags |= REQ_F_FAIL_LINK;
Jens Axboee65ef562019-03-12 10:16:44 -06002365 io_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002366 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002367
2368 return ret;
2369}
2370
Jackie Liu4fe2c962019-09-09 20:50:40 +08002371static int io_queue_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
Jens Axboebc808bc2019-10-22 13:14:37 -06002372 struct sqe_submit *s)
Jackie Liu4fe2c962019-09-09 20:50:40 +08002373{
2374 int ret;
2375
2376 ret = io_req_defer(ctx, req, s->sqe);
2377 if (ret) {
2378 if (ret != -EIOCBQUEUED) {
2379 io_free_req(req);
2380 io_cqring_add_event(ctx, s->sqe->user_data, ret);
2381 }
2382 return 0;
2383 }
2384
Jens Axboebc808bc2019-10-22 13:14:37 -06002385 return __io_queue_sqe(ctx, req, s);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002386}
2387
2388static int io_queue_link_head(struct io_ring_ctx *ctx, struct io_kiocb *req,
Jens Axboebc808bc2019-10-22 13:14:37 -06002389 struct sqe_submit *s, struct io_kiocb *shadow)
Jackie Liu4fe2c962019-09-09 20:50:40 +08002390{
2391 int ret;
2392 int need_submit = false;
2393
2394 if (!shadow)
Jens Axboebc808bc2019-10-22 13:14:37 -06002395 return io_queue_sqe(ctx, req, s);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002396
2397 /*
2398 * Mark the first IO in link list as DRAIN, let all the following
2399 * IOs enter the defer list. all IO needs to be completed before link
2400 * list.
2401 */
2402 req->flags |= REQ_F_IO_DRAIN;
2403 ret = io_req_defer(ctx, req, s->sqe);
2404 if (ret) {
2405 if (ret != -EIOCBQUEUED) {
2406 io_free_req(req);
2407 io_cqring_add_event(ctx, s->sqe->user_data, ret);
2408 return 0;
2409 }
2410 } else {
2411 /*
2412 * If ret == 0 means that all IOs in front of link io are
2413 * running done. let's queue link head.
2414 */
2415 need_submit = true;
2416 }
2417
2418 /* Insert shadow req to defer_list, blocking next IOs */
2419 spin_lock_irq(&ctx->completion_lock);
2420 list_add_tail(&shadow->list, &ctx->defer_list);
2421 spin_unlock_irq(&ctx->completion_lock);
2422
2423 if (need_submit)
Jens Axboebc808bc2019-10-22 13:14:37 -06002424 return __io_queue_sqe(ctx, req, s);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002425
2426 return 0;
2427}
2428
Jens Axboe9e645e112019-05-10 16:07:28 -06002429#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK)
2430
2431static void io_submit_sqe(struct io_ring_ctx *ctx, struct sqe_submit *s,
Jens Axboebc808bc2019-10-22 13:14:37 -06002432 struct io_submit_state *state, struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06002433{
2434 struct io_uring_sqe *sqe_copy;
2435 struct io_kiocb *req;
2436 int ret;
2437
2438 /* enforce forwards compatibility on users */
2439 if (unlikely(s->sqe->flags & ~SQE_VALID_FLAGS)) {
2440 ret = -EINVAL;
2441 goto err;
2442 }
2443
2444 req = io_get_req(ctx, state);
2445 if (unlikely(!req)) {
2446 ret = -EAGAIN;
2447 goto err;
2448 }
2449
2450 ret = io_req_set_file(ctx, s, state, req);
2451 if (unlikely(ret)) {
2452err_req:
2453 io_free_req(req);
2454err:
2455 io_cqring_add_event(ctx, s->sqe->user_data, ret);
2456 return;
2457 }
2458
Pavel Begunkov84d55dc2019-10-25 12:31:29 +03002459 req->user_data = s->sqe->user_data;
2460
Jens Axboe9e645e112019-05-10 16:07:28 -06002461 /*
2462 * If we already have a head request, queue this one for async
2463 * submittal once the head completes. If we don't have a head but
2464 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
2465 * submitted sync once the chain is complete. If none of those
2466 * conditions are true (normal request), then just queue it.
2467 */
2468 if (*link) {
2469 struct io_kiocb *prev = *link;
2470
2471 sqe_copy = kmemdup(s->sqe, sizeof(*sqe_copy), GFP_KERNEL);
2472 if (!sqe_copy) {
2473 ret = -EAGAIN;
2474 goto err_req;
2475 }
2476
2477 s->sqe = sqe_copy;
2478 memcpy(&req->submit, s, sizeof(*s));
2479 list_add_tail(&req->list, &prev->link_list);
2480 } else if (s->sqe->flags & IOSQE_IO_LINK) {
2481 req->flags |= REQ_F_LINK;
2482
2483 memcpy(&req->submit, s, sizeof(*s));
2484 INIT_LIST_HEAD(&req->link_list);
2485 *link = req;
2486 } else {
Jens Axboebc808bc2019-10-22 13:14:37 -06002487 io_queue_sqe(ctx, req, s);
Jens Axboe9e645e112019-05-10 16:07:28 -06002488 }
2489}
2490
Jens Axboe9a56a232019-01-09 09:06:50 -07002491/*
2492 * Batched submission is done, ensure local IO is flushed out.
2493 */
2494static void io_submit_state_end(struct io_submit_state *state)
2495{
2496 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06002497 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07002498 if (state->free_reqs)
2499 kmem_cache_free_bulk(req_cachep, state->free_reqs,
2500 &state->reqs[state->cur_req]);
Jens Axboe9a56a232019-01-09 09:06:50 -07002501}
2502
2503/*
2504 * Start submission side cache.
2505 */
2506static void io_submit_state_start(struct io_submit_state *state,
2507 struct io_ring_ctx *ctx, unsigned max_ios)
2508{
2509 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07002510 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07002511 state->file = NULL;
2512 state->ios_left = max_ios;
2513}
2514
Jens Axboe2b188cc2019-01-07 10:46:33 -07002515static void io_commit_sqring(struct io_ring_ctx *ctx)
2516{
Hristo Venev75b28af2019-08-26 17:23:46 +00002517 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002518
Hristo Venev75b28af2019-08-26 17:23:46 +00002519 if (ctx->cached_sq_head != READ_ONCE(rings->sq.head)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07002520 /*
2521 * Ensure any loads from the SQEs are done at this point,
2522 * since once we write the new head, the application could
2523 * write new data to them.
2524 */
Hristo Venev75b28af2019-08-26 17:23:46 +00002525 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002526 }
2527}
2528
2529/*
Jens Axboe2b188cc2019-01-07 10:46:33 -07002530 * Fetch an sqe, if one is available. Note that s->sqe will point to memory
2531 * that is mapped by userspace. This means that care needs to be taken to
2532 * ensure that reads are stable, as we cannot rely on userspace always
2533 * being a good citizen. If members of the sqe are validated and then later
2534 * used, it's important that those reads are done through READ_ONCE() to
2535 * prevent a re-load down the line.
2536 */
2537static bool io_get_sqring(struct io_ring_ctx *ctx, struct sqe_submit *s)
2538{
Hristo Venev75b28af2019-08-26 17:23:46 +00002539 struct io_rings *rings = ctx->rings;
2540 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002541 unsigned head;
2542
2543 /*
2544 * The cached sq head (or cq tail) serves two purposes:
2545 *
2546 * 1) allows us to batch the cost of updating the user visible
2547 * head updates.
2548 * 2) allows the kernel side to track the head on its own, even
2549 * though the application is the one updating it.
2550 */
2551 head = ctx->cached_sq_head;
Stefan Bühlere523a292019-04-19 11:57:44 +02002552 /* make sure SQ entry isn't read before tail */
Hristo Venev75b28af2019-08-26 17:23:46 +00002553 if (head == smp_load_acquire(&rings->sq.tail))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002554 return false;
2555
Hristo Venev75b28af2019-08-26 17:23:46 +00002556 head = READ_ONCE(sq_array[head & ctx->sq_mask]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002557 if (head < ctx->sq_entries) {
2558 s->index = head;
2559 s->sqe = &ctx->sq_sqes[head];
Jackie Liu8776f3f2019-09-09 20:50:39 +08002560 s->sequence = ctx->cached_sq_head;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002561 ctx->cached_sq_head++;
2562 return true;
2563 }
2564
2565 /* drop invalid entries */
2566 ctx->cached_sq_head++;
Hristo Venev75b28af2019-08-26 17:23:46 +00002567 rings->sq_dropped++;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002568 return false;
2569}
2570
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002571static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
2572 bool has_user, bool mm_fault)
Jens Axboe6c271ce2019-01-10 11:22:30 -07002573{
2574 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002575 struct io_kiocb *link = NULL;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002576 struct io_kiocb *shadow_req = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002577 bool prev_was_link = false;
2578 int i, submitted = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002579
2580 if (nr > IO_PLUG_THRESHOLD) {
2581 io_submit_state_start(&state, ctx, nr);
2582 statep = &state;
2583 }
2584
2585 for (i = 0; i < nr; i++) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002586 struct sqe_submit s;
2587
2588 if (!io_get_sqring(ctx, &s))
2589 break;
2590
Jens Axboe9e645e112019-05-10 16:07:28 -06002591 /*
2592 * If previous wasn't linked and we have a linked command,
2593 * that's the end of the chain. Submit the previous link.
2594 */
2595 if (!prev_was_link && link) {
Jens Axboebc808bc2019-10-22 13:14:37 -06002596 io_queue_link_head(ctx, link, &link->submit, shadow_req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002597 link = NULL;
Jackie Liu5f5ad9c2019-09-18 10:37:53 +08002598 shadow_req = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002599 }
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002600 prev_was_link = (s.sqe->flags & IOSQE_IO_LINK) != 0;
Jens Axboe9e645e112019-05-10 16:07:28 -06002601
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002602 if (link && (s.sqe->flags & IOSQE_IO_DRAIN)) {
Jackie Liu4fe2c962019-09-09 20:50:40 +08002603 if (!shadow_req) {
2604 shadow_req = io_get_req(ctx, NULL);
Jackie Liua1041c22019-09-18 17:25:52 +08002605 if (unlikely(!shadow_req))
2606 goto out;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002607 shadow_req->flags |= (REQ_F_IO_DRAIN | REQ_F_SHADOW_DRAIN);
2608 refcount_dec(&shadow_req->refs);
2609 }
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002610 shadow_req->sequence = s.sequence;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002611 }
2612
Jackie Liua1041c22019-09-18 17:25:52 +08002613out:
Jens Axboe6c271ce2019-01-10 11:22:30 -07002614 if (unlikely(mm_fault)) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002615 io_cqring_add_event(ctx, s.sqe->user_data,
Jens Axboe9e645e112019-05-10 16:07:28 -06002616 -EFAULT);
Jens Axboe6c271ce2019-01-10 11:22:30 -07002617 } else {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002618 s.has_user = has_user;
2619 s.needs_lock = true;
2620 s.needs_fixed_file = true;
2621 io_submit_sqe(ctx, &s, statep, &link);
Jens Axboe6c271ce2019-01-10 11:22:30 -07002622 submitted++;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002623 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07002624 }
2625
Jens Axboe9e645e112019-05-10 16:07:28 -06002626 if (link)
Jens Axboebc808bc2019-10-22 13:14:37 -06002627 io_queue_link_head(ctx, link, &link->submit, shadow_req);
Jens Axboe6c271ce2019-01-10 11:22:30 -07002628 if (statep)
2629 io_submit_state_end(&state);
2630
2631 return submitted;
2632}
2633
2634static int io_sq_thread(void *data)
2635{
Jens Axboe6c271ce2019-01-10 11:22:30 -07002636 struct io_ring_ctx *ctx = data;
2637 struct mm_struct *cur_mm = NULL;
2638 mm_segment_t old_fs;
2639 DEFINE_WAIT(wait);
2640 unsigned inflight;
2641 unsigned long timeout;
2642
Jackie Liua4c0b3d2019-07-08 13:41:12 +08002643 complete(&ctx->sqo_thread_started);
2644
Jens Axboe6c271ce2019-01-10 11:22:30 -07002645 old_fs = get_fs();
2646 set_fs(USER_DS);
2647
2648 timeout = inflight = 0;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02002649 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002650 bool mm_fault = false;
2651 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002652
2653 if (inflight) {
2654 unsigned nr_events = 0;
2655
2656 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07002657 io_iopoll_check(ctx, &nr_events, 0);
Jens Axboe6c271ce2019-01-10 11:22:30 -07002658 } else {
2659 /*
2660 * Normal IO, just pretend everything completed.
2661 * We don't have to poll completions for that.
2662 */
2663 nr_events = inflight;
2664 }
2665
2666 inflight -= nr_events;
2667 if (!inflight)
2668 timeout = jiffies + ctx->sq_thread_idle;
2669 }
2670
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002671 to_submit = io_sqring_entries(ctx);
2672 if (!to_submit) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07002673 /*
2674 * We're polling. If we're within the defined idle
2675 * period, then let us spin without work before going
2676 * to sleep.
2677 */
2678 if (inflight || !time_after(jiffies, timeout)) {
Jens Axboe9831a902019-09-19 09:48:55 -06002679 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07002680 continue;
2681 }
2682
2683 /*
2684 * Drop cur_mm before scheduling, we can't hold it for
2685 * long periods (or over schedule()). Do this before
2686 * adding ourselves to the waitqueue, as the unuse/drop
2687 * may sleep.
2688 */
2689 if (cur_mm) {
2690 unuse_mm(cur_mm);
2691 mmput(cur_mm);
2692 cur_mm = NULL;
2693 }
2694
2695 prepare_to_wait(&ctx->sqo_wait, &wait,
2696 TASK_INTERRUPTIBLE);
2697
2698 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00002699 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02002700 /* make sure to read SQ tail after writing flags */
2701 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07002702
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002703 to_submit = io_sqring_entries(ctx);
2704 if (!to_submit) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02002705 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07002706 finish_wait(&ctx->sqo_wait, &wait);
2707 break;
2708 }
2709 if (signal_pending(current))
2710 flush_signals(current);
2711 schedule();
2712 finish_wait(&ctx->sqo_wait, &wait);
2713
Hristo Venev75b28af2019-08-26 17:23:46 +00002714 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002715 continue;
2716 }
2717 finish_wait(&ctx->sqo_wait, &wait);
2718
Hristo Venev75b28af2019-08-26 17:23:46 +00002719 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002720 }
2721
Jens Axboe6c271ce2019-01-10 11:22:30 -07002722 /* Unless all new commands are FIXED regions, grab mm */
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002723 if (!cur_mm) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07002724 mm_fault = !mmget_not_zero(ctx->sqo_mm);
2725 if (!mm_fault) {
2726 use_mm(ctx->sqo_mm);
2727 cur_mm = ctx->sqo_mm;
2728 }
2729 }
2730
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002731 to_submit = min(to_submit, ctx->sq_entries);
2732 inflight += io_submit_sqes(ctx, to_submit, cur_mm != NULL,
2733 mm_fault);
Jens Axboe6c271ce2019-01-10 11:22:30 -07002734
2735 /* Commit SQ ring head once we've consumed all SQEs */
2736 io_commit_sqring(ctx);
2737 }
2738
2739 set_fs(old_fs);
2740 if (cur_mm) {
2741 unuse_mm(cur_mm);
2742 mmput(cur_mm);
2743 }
Jens Axboe06058632019-04-13 09:26:03 -06002744
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02002745 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06002746
Jens Axboe6c271ce2019-01-10 11:22:30 -07002747 return 0;
2748}
2749
Jens Axboebc808bc2019-10-22 13:14:37 -06002750static int io_ring_submit(struct io_ring_ctx *ctx, unsigned int to_submit)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002751{
Jens Axboe9a56a232019-01-09 09:06:50 -07002752 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002753 struct io_kiocb *link = NULL;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002754 struct io_kiocb *shadow_req = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002755 bool prev_was_link = false;
Jens Axboe5c8b0b52019-04-30 10:16:07 -06002756 int i, submit = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002757
Jens Axboe9a56a232019-01-09 09:06:50 -07002758 if (to_submit > IO_PLUG_THRESHOLD) {
2759 io_submit_state_start(&state, ctx, to_submit);
2760 statep = &state;
2761 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002762
2763 for (i = 0; i < to_submit; i++) {
2764 struct sqe_submit s;
2765
2766 if (!io_get_sqring(ctx, &s))
2767 break;
2768
Jens Axboe9e645e112019-05-10 16:07:28 -06002769 /*
2770 * If previous wasn't linked and we have a linked command,
2771 * that's the end of the chain. Submit the previous link.
2772 */
2773 if (!prev_was_link && link) {
Jens Axboebc808bc2019-10-22 13:14:37 -06002774 io_queue_link_head(ctx, link, &link->submit, shadow_req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002775 link = NULL;
Jackie Liu5f5ad9c2019-09-18 10:37:53 +08002776 shadow_req = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002777 }
2778 prev_was_link = (s.sqe->flags & IOSQE_IO_LINK) != 0;
2779
Jackie Liu4fe2c962019-09-09 20:50:40 +08002780 if (link && (s.sqe->flags & IOSQE_IO_DRAIN)) {
2781 if (!shadow_req) {
2782 shadow_req = io_get_req(ctx, NULL);
Jackie Liua1041c22019-09-18 17:25:52 +08002783 if (unlikely(!shadow_req))
2784 goto out;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002785 shadow_req->flags |= (REQ_F_IO_DRAIN | REQ_F_SHADOW_DRAIN);
2786 refcount_dec(&shadow_req->refs);
2787 }
2788 shadow_req->sequence = s.sequence;
2789 }
2790
Jackie Liua1041c22019-09-18 17:25:52 +08002791out:
Jens Axboe2b188cc2019-01-07 10:46:33 -07002792 s.has_user = true;
Jens Axboedef596e2019-01-09 08:59:42 -07002793 s.needs_lock = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002794 s.needs_fixed_file = false;
Jens Axboe5c8b0b52019-04-30 10:16:07 -06002795 submit++;
Jens Axboebc808bc2019-10-22 13:14:37 -06002796 io_submit_sqe(ctx, &s, statep, &link);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002797 }
2798 io_commit_sqring(ctx);
2799
Jens Axboe9e645e112019-05-10 16:07:28 -06002800 if (link)
Jens Axboebc808bc2019-10-22 13:14:37 -06002801 io_queue_link_head(ctx, link, &link->submit, shadow_req);
Jens Axboe9a56a232019-01-09 09:06:50 -07002802 if (statep)
2803 io_submit_state_end(statep);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002804
Jens Axboe5c8b0b52019-04-30 10:16:07 -06002805 return submit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002806}
2807
Jens Axboebda52162019-09-24 13:47:15 -06002808struct io_wait_queue {
2809 struct wait_queue_entry wq;
2810 struct io_ring_ctx *ctx;
2811 unsigned to_wait;
2812 unsigned nr_timeouts;
2813};
2814
2815static inline bool io_should_wake(struct io_wait_queue *iowq)
2816{
2817 struct io_ring_ctx *ctx = iowq->ctx;
2818
2819 /*
2820 * Wake up if we have enough events, or if a timeout occured since we
2821 * started waiting. For timeouts, we always want to return to userspace,
2822 * regardless of event count.
2823 */
2824 return io_cqring_events(ctx->rings) >= iowq->to_wait ||
2825 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
2826}
2827
2828static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
2829 int wake_flags, void *key)
2830{
2831 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
2832 wq);
2833
2834 if (!io_should_wake(iowq))
2835 return -1;
2836
2837 return autoremove_wake_function(curr, mode, wake_flags, key);
2838}
2839
Jens Axboe2b188cc2019-01-07 10:46:33 -07002840/*
2841 * Wait until events become available, if we don't already have some. The
2842 * application must reap them itself, as they reside on the shared cq ring.
2843 */
2844static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
2845 const sigset_t __user *sig, size_t sigsz)
2846{
Jens Axboebda52162019-09-24 13:47:15 -06002847 struct io_wait_queue iowq = {
2848 .wq = {
2849 .private = current,
2850 .func = io_wake_function,
2851 .entry = LIST_HEAD_INIT(iowq.wq.entry),
2852 },
2853 .ctx = ctx,
2854 .to_wait = min_events,
2855 };
Hristo Venev75b28af2019-08-26 17:23:46 +00002856 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002857 int ret;
2858
Hristo Venev75b28af2019-08-26 17:23:46 +00002859 if (io_cqring_events(rings) >= min_events)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002860 return 0;
2861
2862 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01002863#ifdef CONFIG_COMPAT
2864 if (in_compat_syscall())
2865 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07002866 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01002867 else
2868#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07002869 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01002870
Jens Axboe2b188cc2019-01-07 10:46:33 -07002871 if (ret)
2872 return ret;
2873 }
2874
Jens Axboebda52162019-09-24 13:47:15 -06002875 ret = 0;
2876 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
2877 do {
2878 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
2879 TASK_INTERRUPTIBLE);
2880 if (io_should_wake(&iowq))
2881 break;
2882 schedule();
2883 if (signal_pending(current)) {
2884 ret = -ERESTARTSYS;
2885 break;
2886 }
2887 } while (1);
2888 finish_wait(&ctx->wait, &iowq.wq);
2889
Oleg Nesterovb7724342019-07-16 16:29:53 -07002890 restore_saved_sigmask_unless(ret == -ERESTARTSYS);
Oleg Nesterov97abc882019-06-28 12:06:50 -07002891 if (ret == -ERESTARTSYS)
2892 ret = -EINTR;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002893
Hristo Venev75b28af2019-08-26 17:23:46 +00002894 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002895}
2896
Jens Axboe6b063142019-01-10 22:13:58 -07002897static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
2898{
2899#if defined(CONFIG_UNIX)
2900 if (ctx->ring_sock) {
2901 struct sock *sock = ctx->ring_sock->sk;
2902 struct sk_buff *skb;
2903
2904 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
2905 kfree_skb(skb);
2906 }
2907#else
2908 int i;
2909
2910 for (i = 0; i < ctx->nr_user_files; i++)
2911 fput(ctx->user_files[i]);
2912#endif
2913}
2914
2915static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
2916{
2917 if (!ctx->user_files)
2918 return -ENXIO;
2919
2920 __io_sqe_files_unregister(ctx);
2921 kfree(ctx->user_files);
2922 ctx->user_files = NULL;
2923 ctx->nr_user_files = 0;
2924 return 0;
2925}
2926
Jens Axboe6c271ce2019-01-10 11:22:30 -07002927static void io_sq_thread_stop(struct io_ring_ctx *ctx)
2928{
2929 if (ctx->sqo_thread) {
Jackie Liua4c0b3d2019-07-08 13:41:12 +08002930 wait_for_completion(&ctx->sqo_thread_started);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02002931 /*
2932 * The park is a bit of a work-around, without it we get
2933 * warning spews on shutdown with SQPOLL set and affinity
2934 * set to a single CPU.
2935 */
Jens Axboe06058632019-04-13 09:26:03 -06002936 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07002937 kthread_stop(ctx->sqo_thread);
2938 ctx->sqo_thread = NULL;
2939 }
2940}
2941
Jens Axboe6b063142019-01-10 22:13:58 -07002942static void io_finish_async(struct io_ring_ctx *ctx)
2943{
Jens Axboe54a91f32019-09-10 09:15:04 -06002944 int i;
2945
Jens Axboe6c271ce2019-01-10 11:22:30 -07002946 io_sq_thread_stop(ctx);
2947
Jens Axboe54a91f32019-09-10 09:15:04 -06002948 for (i = 0; i < ARRAY_SIZE(ctx->sqo_wq); i++) {
2949 if (ctx->sqo_wq[i]) {
2950 destroy_workqueue(ctx->sqo_wq[i]);
2951 ctx->sqo_wq[i] = NULL;
2952 }
Jens Axboe6b063142019-01-10 22:13:58 -07002953 }
2954}
2955
2956#if defined(CONFIG_UNIX)
2957static void io_destruct_skb(struct sk_buff *skb)
2958{
2959 struct io_ring_ctx *ctx = skb->sk->sk_user_data;
Jens Axboe8a997342019-10-09 14:40:13 -06002960 int i;
Jens Axboe6b063142019-01-10 22:13:58 -07002961
Jens Axboe8a997342019-10-09 14:40:13 -06002962 for (i = 0; i < ARRAY_SIZE(ctx->sqo_wq); i++)
2963 if (ctx->sqo_wq[i])
2964 flush_workqueue(ctx->sqo_wq[i]);
2965
Jens Axboe6b063142019-01-10 22:13:58 -07002966 unix_destruct_scm(skb);
2967}
2968
2969/*
2970 * Ensure the UNIX gc is aware of our file set, so we are certain that
2971 * the io_uring can be safely unregistered on process exit, even if we have
2972 * loops in the file referencing.
2973 */
2974static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
2975{
2976 struct sock *sk = ctx->ring_sock->sk;
2977 struct scm_fp_list *fpl;
2978 struct sk_buff *skb;
2979 int i;
2980
2981 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
2982 unsigned long inflight = ctx->user->unix_inflight + nr;
2983
2984 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
2985 return -EMFILE;
2986 }
2987
2988 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
2989 if (!fpl)
2990 return -ENOMEM;
2991
2992 skb = alloc_skb(0, GFP_KERNEL);
2993 if (!skb) {
2994 kfree(fpl);
2995 return -ENOMEM;
2996 }
2997
2998 skb->sk = sk;
2999 skb->destructor = io_destruct_skb;
3000
3001 fpl->user = get_uid(ctx->user);
3002 for (i = 0; i < nr; i++) {
3003 fpl->fp[i] = get_file(ctx->user_files[i + offset]);
3004 unix_inflight(fpl->user, fpl->fp[i]);
3005 }
3006
3007 fpl->max = fpl->count = nr;
3008 UNIXCB(skb).fp = fpl;
3009 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
3010 skb_queue_head(&sk->sk_receive_queue, skb);
3011
3012 for (i = 0; i < nr; i++)
3013 fput(fpl->fp[i]);
3014
3015 return 0;
3016}
3017
3018/*
3019 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
3020 * causes regular reference counting to break down. We rely on the UNIX
3021 * garbage collection to take care of this problem for us.
3022 */
3023static int io_sqe_files_scm(struct io_ring_ctx *ctx)
3024{
3025 unsigned left, total;
3026 int ret = 0;
3027
3028 total = 0;
3029 left = ctx->nr_user_files;
3030 while (left) {
3031 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07003032
3033 ret = __io_sqe_files_scm(ctx, this_files, total);
3034 if (ret)
3035 break;
3036 left -= this_files;
3037 total += this_files;
3038 }
3039
3040 if (!ret)
3041 return 0;
3042
3043 while (total < ctx->nr_user_files) {
3044 fput(ctx->user_files[total]);
3045 total++;
3046 }
3047
3048 return ret;
3049}
3050#else
3051static int io_sqe_files_scm(struct io_ring_ctx *ctx)
3052{
3053 return 0;
3054}
3055#endif
3056
3057static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
3058 unsigned nr_args)
3059{
3060 __s32 __user *fds = (__s32 __user *) arg;
3061 int fd, ret = 0;
3062 unsigned i;
3063
3064 if (ctx->user_files)
3065 return -EBUSY;
3066 if (!nr_args)
3067 return -EINVAL;
3068 if (nr_args > IORING_MAX_FIXED_FILES)
3069 return -EMFILE;
3070
3071 ctx->user_files = kcalloc(nr_args, sizeof(struct file *), GFP_KERNEL);
3072 if (!ctx->user_files)
3073 return -ENOMEM;
3074
3075 for (i = 0; i < nr_args; i++) {
3076 ret = -EFAULT;
3077 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
3078 break;
3079
3080 ctx->user_files[i] = fget(fd);
3081
3082 ret = -EBADF;
3083 if (!ctx->user_files[i])
3084 break;
3085 /*
3086 * Don't allow io_uring instances to be registered. If UNIX
3087 * isn't enabled, then this causes a reference cycle and this
3088 * instance can never get freed. If UNIX is enabled we'll
3089 * handle it just fine, but there's still no point in allowing
3090 * a ring fd as it doesn't support regular read/write anyway.
3091 */
3092 if (ctx->user_files[i]->f_op == &io_uring_fops) {
3093 fput(ctx->user_files[i]);
3094 break;
3095 }
3096 ctx->nr_user_files++;
3097 ret = 0;
3098 }
3099
3100 if (ret) {
3101 for (i = 0; i < ctx->nr_user_files; i++)
3102 fput(ctx->user_files[i]);
3103
3104 kfree(ctx->user_files);
Jens Axboe25adf502019-04-03 09:52:40 -06003105 ctx->user_files = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003106 ctx->nr_user_files = 0;
3107 return ret;
3108 }
3109
3110 ret = io_sqe_files_scm(ctx);
3111 if (ret)
3112 io_sqe_files_unregister(ctx);
3113
3114 return ret;
3115}
3116
Jens Axboe6c271ce2019-01-10 11:22:30 -07003117static int io_sq_offload_start(struct io_ring_ctx *ctx,
3118 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003119{
3120 int ret;
3121
Jens Axboe6c271ce2019-01-10 11:22:30 -07003122 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003123 mmgrab(current->mm);
3124 ctx->sqo_mm = current->mm;
3125
Jens Axboe6c271ce2019-01-10 11:22:30 -07003126 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06003127 ret = -EPERM;
3128 if (!capable(CAP_SYS_ADMIN))
3129 goto err;
3130
Jens Axboe917257d2019-04-13 09:28:55 -06003131 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
3132 if (!ctx->sq_thread_idle)
3133 ctx->sq_thread_idle = HZ;
3134
Jens Axboe6c271ce2019-01-10 11:22:30 -07003135 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06003136 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003137
Jens Axboe917257d2019-04-13 09:28:55 -06003138 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06003139 if (cpu >= nr_cpu_ids)
3140 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08003141 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06003142 goto err;
3143
Jens Axboe6c271ce2019-01-10 11:22:30 -07003144 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
3145 ctx, cpu,
3146 "io_uring-sq");
3147 } else {
3148 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
3149 "io_uring-sq");
3150 }
3151 if (IS_ERR(ctx->sqo_thread)) {
3152 ret = PTR_ERR(ctx->sqo_thread);
3153 ctx->sqo_thread = NULL;
3154 goto err;
3155 }
3156 wake_up_process(ctx->sqo_thread);
3157 } else if (p->flags & IORING_SETUP_SQ_AFF) {
3158 /* Can't have SQ_AFF without SQPOLL */
3159 ret = -EINVAL;
3160 goto err;
3161 }
3162
Jens Axboe2b188cc2019-01-07 10:46:33 -07003163 /* Do QD, or 2 * CPUS, whatever is smallest */
Jens Axboe54a91f32019-09-10 09:15:04 -06003164 ctx->sqo_wq[0] = alloc_workqueue("io_ring-wq",
3165 WQ_UNBOUND | WQ_FREEZABLE,
Jens Axboe2b188cc2019-01-07 10:46:33 -07003166 min(ctx->sq_entries - 1, 2 * num_online_cpus()));
Jens Axboe54a91f32019-09-10 09:15:04 -06003167 if (!ctx->sqo_wq[0]) {
3168 ret = -ENOMEM;
3169 goto err;
3170 }
3171
3172 /*
3173 * This is for buffered writes, where we want to limit the parallelism
3174 * due to file locking in file systems. As "normal" buffered writes
3175 * should parellelize on writeout quite nicely, limit us to having 2
3176 * pending. This avoids massive contention on the inode when doing
3177 * buffered async writes.
3178 */
3179 ctx->sqo_wq[1] = alloc_workqueue("io_ring-write-wq",
3180 WQ_UNBOUND | WQ_FREEZABLE, 2);
3181 if (!ctx->sqo_wq[1]) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07003182 ret = -ENOMEM;
3183 goto err;
3184 }
3185
3186 return 0;
3187err:
Jens Axboe54a91f32019-09-10 09:15:04 -06003188 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003189 mmdrop(ctx->sqo_mm);
3190 ctx->sqo_mm = NULL;
3191 return ret;
3192}
3193
3194static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
3195{
3196 atomic_long_sub(nr_pages, &user->locked_vm);
3197}
3198
3199static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
3200{
3201 unsigned long page_limit, cur_pages, new_pages;
3202
3203 /* Don't allow more pages than we can safely lock */
3204 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
3205
3206 do {
3207 cur_pages = atomic_long_read(&user->locked_vm);
3208 new_pages = cur_pages + nr_pages;
3209 if (new_pages > page_limit)
3210 return -ENOMEM;
3211 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
3212 new_pages) != cur_pages);
3213
3214 return 0;
3215}
3216
3217static void io_mem_free(void *ptr)
3218{
Mark Rutland52e04ef2019-04-30 17:30:21 +01003219 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003220
Mark Rutland52e04ef2019-04-30 17:30:21 +01003221 if (!ptr)
3222 return;
3223
3224 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003225 if (put_page_testzero(page))
3226 free_compound_page(page);
3227}
3228
3229static void *io_mem_alloc(size_t size)
3230{
3231 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
3232 __GFP_NORETRY;
3233
3234 return (void *) __get_free_pages(gfp_flags, get_order(size));
3235}
3236
Hristo Venev75b28af2019-08-26 17:23:46 +00003237static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
3238 size_t *sq_offset)
3239{
3240 struct io_rings *rings;
3241 size_t off, sq_array_size;
3242
3243 off = struct_size(rings, cqes, cq_entries);
3244 if (off == SIZE_MAX)
3245 return SIZE_MAX;
3246
3247#ifdef CONFIG_SMP
3248 off = ALIGN(off, SMP_CACHE_BYTES);
3249 if (off == 0)
3250 return SIZE_MAX;
3251#endif
3252
3253 sq_array_size = array_size(sizeof(u32), sq_entries);
3254 if (sq_array_size == SIZE_MAX)
3255 return SIZE_MAX;
3256
3257 if (check_add_overflow(off, sq_array_size, &off))
3258 return SIZE_MAX;
3259
3260 if (sq_offset)
3261 *sq_offset = off;
3262
3263 return off;
3264}
3265
Jens Axboe2b188cc2019-01-07 10:46:33 -07003266static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
3267{
Hristo Venev75b28af2019-08-26 17:23:46 +00003268 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003269
Hristo Venev75b28af2019-08-26 17:23:46 +00003270 pages = (size_t)1 << get_order(
3271 rings_size(sq_entries, cq_entries, NULL));
3272 pages += (size_t)1 << get_order(
3273 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07003274
Hristo Venev75b28af2019-08-26 17:23:46 +00003275 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003276}
3277
Jens Axboeedafcce2019-01-09 09:16:05 -07003278static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
3279{
3280 int i, j;
3281
3282 if (!ctx->user_bufs)
3283 return -ENXIO;
3284
3285 for (i = 0; i < ctx->nr_user_bufs; i++) {
3286 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
3287
3288 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbard27c4d3a2019-08-04 19:32:06 -07003289 put_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07003290
3291 if (ctx->account_mem)
3292 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003293 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07003294 imu->nr_bvecs = 0;
3295 }
3296
3297 kfree(ctx->user_bufs);
3298 ctx->user_bufs = NULL;
3299 ctx->nr_user_bufs = 0;
3300 return 0;
3301}
3302
3303static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
3304 void __user *arg, unsigned index)
3305{
3306 struct iovec __user *src;
3307
3308#ifdef CONFIG_COMPAT
3309 if (ctx->compat) {
3310 struct compat_iovec __user *ciovs;
3311 struct compat_iovec ciov;
3312
3313 ciovs = (struct compat_iovec __user *) arg;
3314 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
3315 return -EFAULT;
3316
3317 dst->iov_base = (void __user *) (unsigned long) ciov.iov_base;
3318 dst->iov_len = ciov.iov_len;
3319 return 0;
3320 }
3321#endif
3322 src = (struct iovec __user *) arg;
3323 if (copy_from_user(dst, &src[index], sizeof(*dst)))
3324 return -EFAULT;
3325 return 0;
3326}
3327
3328static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
3329 unsigned nr_args)
3330{
3331 struct vm_area_struct **vmas = NULL;
3332 struct page **pages = NULL;
3333 int i, j, got_pages = 0;
3334 int ret = -EINVAL;
3335
3336 if (ctx->user_bufs)
3337 return -EBUSY;
3338 if (!nr_args || nr_args > UIO_MAXIOV)
3339 return -EINVAL;
3340
3341 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
3342 GFP_KERNEL);
3343 if (!ctx->user_bufs)
3344 return -ENOMEM;
3345
3346 for (i = 0; i < nr_args; i++) {
3347 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
3348 unsigned long off, start, end, ubuf;
3349 int pret, nr_pages;
3350 struct iovec iov;
3351 size_t size;
3352
3353 ret = io_copy_iov(ctx, &iov, arg, i);
3354 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03003355 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07003356
3357 /*
3358 * Don't impose further limits on the size and buffer
3359 * constraints here, we'll -EINVAL later when IO is
3360 * submitted if they are wrong.
3361 */
3362 ret = -EFAULT;
3363 if (!iov.iov_base || !iov.iov_len)
3364 goto err;
3365
3366 /* arbitrary limit, but we need something */
3367 if (iov.iov_len > SZ_1G)
3368 goto err;
3369
3370 ubuf = (unsigned long) iov.iov_base;
3371 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
3372 start = ubuf >> PAGE_SHIFT;
3373 nr_pages = end - start;
3374
3375 if (ctx->account_mem) {
3376 ret = io_account_mem(ctx->user, nr_pages);
3377 if (ret)
3378 goto err;
3379 }
3380
3381 ret = 0;
3382 if (!pages || nr_pages > got_pages) {
3383 kfree(vmas);
3384 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003385 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07003386 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003387 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07003388 sizeof(struct vm_area_struct *),
3389 GFP_KERNEL);
3390 if (!pages || !vmas) {
3391 ret = -ENOMEM;
3392 if (ctx->account_mem)
3393 io_unaccount_mem(ctx->user, nr_pages);
3394 goto err;
3395 }
3396 got_pages = nr_pages;
3397 }
3398
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003399 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07003400 GFP_KERNEL);
3401 ret = -ENOMEM;
3402 if (!imu->bvec) {
3403 if (ctx->account_mem)
3404 io_unaccount_mem(ctx->user, nr_pages);
3405 goto err;
3406 }
3407
3408 ret = 0;
3409 down_read(&current->mm->mmap_sem);
Ira Weiny932f4a62019-05-13 17:17:03 -07003410 pret = get_user_pages(ubuf, nr_pages,
3411 FOLL_WRITE | FOLL_LONGTERM,
3412 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07003413 if (pret == nr_pages) {
3414 /* don't support file backed memory */
3415 for (j = 0; j < nr_pages; j++) {
3416 struct vm_area_struct *vma = vmas[j];
3417
3418 if (vma->vm_file &&
3419 !is_file_hugepages(vma->vm_file)) {
3420 ret = -EOPNOTSUPP;
3421 break;
3422 }
3423 }
3424 } else {
3425 ret = pret < 0 ? pret : -EFAULT;
3426 }
3427 up_read(&current->mm->mmap_sem);
3428 if (ret) {
3429 /*
3430 * if we did partial map, or found file backed vmas,
3431 * release any pages we did get
3432 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07003433 if (pret > 0)
3434 put_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07003435 if (ctx->account_mem)
3436 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003437 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07003438 goto err;
3439 }
3440
3441 off = ubuf & ~PAGE_MASK;
3442 size = iov.iov_len;
3443 for (j = 0; j < nr_pages; j++) {
3444 size_t vec_len;
3445
3446 vec_len = min_t(size_t, size, PAGE_SIZE - off);
3447 imu->bvec[j].bv_page = pages[j];
3448 imu->bvec[j].bv_len = vec_len;
3449 imu->bvec[j].bv_offset = off;
3450 off = 0;
3451 size -= vec_len;
3452 }
3453 /* store original address for later verification */
3454 imu->ubuf = ubuf;
3455 imu->len = iov.iov_len;
3456 imu->nr_bvecs = nr_pages;
3457
3458 ctx->nr_user_bufs++;
3459 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003460 kvfree(pages);
3461 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07003462 return 0;
3463err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003464 kvfree(pages);
3465 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07003466 io_sqe_buffer_unregister(ctx);
3467 return ret;
3468}
3469
Jens Axboe9b402842019-04-11 11:45:41 -06003470static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
3471{
3472 __s32 __user *fds = arg;
3473 int fd;
3474
3475 if (ctx->cq_ev_fd)
3476 return -EBUSY;
3477
3478 if (copy_from_user(&fd, fds, sizeof(*fds)))
3479 return -EFAULT;
3480
3481 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
3482 if (IS_ERR(ctx->cq_ev_fd)) {
3483 int ret = PTR_ERR(ctx->cq_ev_fd);
3484 ctx->cq_ev_fd = NULL;
3485 return ret;
3486 }
3487
3488 return 0;
3489}
3490
3491static int io_eventfd_unregister(struct io_ring_ctx *ctx)
3492{
3493 if (ctx->cq_ev_fd) {
3494 eventfd_ctx_put(ctx->cq_ev_fd);
3495 ctx->cq_ev_fd = NULL;
3496 return 0;
3497 }
3498
3499 return -ENXIO;
3500}
3501
Jens Axboe2b188cc2019-01-07 10:46:33 -07003502static void io_ring_ctx_free(struct io_ring_ctx *ctx)
3503{
Jens Axboe6b063142019-01-10 22:13:58 -07003504 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003505 if (ctx->sqo_mm)
3506 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07003507
3508 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07003509 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07003510 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06003511 io_eventfd_unregister(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07003512
Jens Axboe2b188cc2019-01-07 10:46:33 -07003513#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07003514 if (ctx->ring_sock) {
3515 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07003516 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07003517 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003518#endif
3519
Hristo Venev75b28af2019-08-26 17:23:46 +00003520 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003521 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003522
3523 percpu_ref_exit(&ctx->refs);
3524 if (ctx->account_mem)
3525 io_unaccount_mem(ctx->user,
3526 ring_pages(ctx->sq_entries, ctx->cq_entries));
3527 free_uid(ctx->user);
3528 kfree(ctx);
3529}
3530
3531static __poll_t io_uring_poll(struct file *file, poll_table *wait)
3532{
3533 struct io_ring_ctx *ctx = file->private_data;
3534 __poll_t mask = 0;
3535
3536 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02003537 /*
3538 * synchronizes with barrier from wq_has_sleeper call in
3539 * io_commit_cqring
3540 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07003541 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00003542 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
3543 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003544 mask |= EPOLLOUT | EPOLLWRNORM;
yangerkundaa5de52019-09-24 20:53:34 +08003545 if (READ_ONCE(ctx->rings->cq.head) != ctx->cached_cq_tail)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003546 mask |= EPOLLIN | EPOLLRDNORM;
3547
3548 return mask;
3549}
3550
3551static int io_uring_fasync(int fd, struct file *file, int on)
3552{
3553 struct io_ring_ctx *ctx = file->private_data;
3554
3555 return fasync_helper(fd, file, on, &ctx->cq_fasync);
3556}
3557
3558static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
3559{
3560 mutex_lock(&ctx->uring_lock);
3561 percpu_ref_kill(&ctx->refs);
3562 mutex_unlock(&ctx->uring_lock);
3563
Jens Axboe5262f562019-09-17 12:26:57 -06003564 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003565 io_poll_remove_all(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07003566 io_iopoll_reap_events(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003567 wait_for_completion(&ctx->ctx_done);
3568 io_ring_ctx_free(ctx);
3569}
3570
3571static int io_uring_release(struct inode *inode, struct file *file)
3572{
3573 struct io_ring_ctx *ctx = file->private_data;
3574
3575 file->private_data = NULL;
3576 io_ring_ctx_wait_and_kill(ctx);
3577 return 0;
3578}
3579
3580static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
3581{
3582 loff_t offset = (loff_t) vma->vm_pgoff << PAGE_SHIFT;
3583 unsigned long sz = vma->vm_end - vma->vm_start;
3584 struct io_ring_ctx *ctx = file->private_data;
3585 unsigned long pfn;
3586 struct page *page;
3587 void *ptr;
3588
3589 switch (offset) {
3590 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00003591 case IORING_OFF_CQ_RING:
3592 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003593 break;
3594 case IORING_OFF_SQES:
3595 ptr = ctx->sq_sqes;
3596 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003597 default:
3598 return -EINVAL;
3599 }
3600
3601 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07003602 if (sz > page_size(page))
Jens Axboe2b188cc2019-01-07 10:46:33 -07003603 return -EINVAL;
3604
3605 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
3606 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
3607}
3608
3609SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
3610 u32, min_complete, u32, flags, const sigset_t __user *, sig,
3611 size_t, sigsz)
3612{
3613 struct io_ring_ctx *ctx;
3614 long ret = -EBADF;
3615 int submitted = 0;
3616 struct fd f;
3617
Jens Axboe6c271ce2019-01-10 11:22:30 -07003618 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07003619 return -EINVAL;
3620
3621 f = fdget(fd);
3622 if (!f.file)
3623 return -EBADF;
3624
3625 ret = -EOPNOTSUPP;
3626 if (f.file->f_op != &io_uring_fops)
3627 goto out_fput;
3628
3629 ret = -ENXIO;
3630 ctx = f.file->private_data;
3631 if (!percpu_ref_tryget(&ctx->refs))
3632 goto out_fput;
3633
Jens Axboe6c271ce2019-01-10 11:22:30 -07003634 /*
3635 * For SQ polling, the thread will do all submissions and completions.
3636 * Just return the requested submit count, and wake the thread if
3637 * we were asked to.
3638 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06003639 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003640 if (ctx->flags & IORING_SETUP_SQPOLL) {
3641 if (flags & IORING_ENTER_SQ_WAKEUP)
3642 wake_up(&ctx->sqo_wait);
3643 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06003644 } else if (to_submit) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07003645 to_submit = min(to_submit, ctx->sq_entries);
3646
3647 mutex_lock(&ctx->uring_lock);
Jens Axboebc808bc2019-10-22 13:14:37 -06003648 submitted = io_ring_submit(ctx, to_submit);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003649 mutex_unlock(&ctx->uring_lock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003650 }
3651 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07003652 unsigned nr_events = 0;
3653
Jens Axboe2b188cc2019-01-07 10:46:33 -07003654 min_complete = min(min_complete, ctx->cq_entries);
3655
Jens Axboedef596e2019-01-09 08:59:42 -07003656 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07003657 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07003658 } else {
3659 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
3660 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003661 }
3662
Pavel Begunkov6805b322019-10-08 02:18:42 +03003663 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003664out_fput:
3665 fdput(f);
3666 return submitted ? submitted : ret;
3667}
3668
3669static const struct file_operations io_uring_fops = {
3670 .release = io_uring_release,
3671 .mmap = io_uring_mmap,
3672 .poll = io_uring_poll,
3673 .fasync = io_uring_fasync,
3674};
3675
3676static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
3677 struct io_uring_params *p)
3678{
Hristo Venev75b28af2019-08-26 17:23:46 +00003679 struct io_rings *rings;
3680 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003681
Hristo Venev75b28af2019-08-26 17:23:46 +00003682 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
3683 if (size == SIZE_MAX)
3684 return -EOVERFLOW;
3685
3686 rings = io_mem_alloc(size);
3687 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003688 return -ENOMEM;
3689
Hristo Venev75b28af2019-08-26 17:23:46 +00003690 ctx->rings = rings;
3691 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
3692 rings->sq_ring_mask = p->sq_entries - 1;
3693 rings->cq_ring_mask = p->cq_entries - 1;
3694 rings->sq_ring_entries = p->sq_entries;
3695 rings->cq_ring_entries = p->cq_entries;
3696 ctx->sq_mask = rings->sq_ring_mask;
3697 ctx->cq_mask = rings->cq_ring_mask;
3698 ctx->sq_entries = rings->sq_ring_entries;
3699 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003700
3701 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
3702 if (size == SIZE_MAX)
3703 return -EOVERFLOW;
3704
3705 ctx->sq_sqes = io_mem_alloc(size);
Mark Rutland52e04ef2019-04-30 17:30:21 +01003706 if (!ctx->sq_sqes)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003707 return -ENOMEM;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003708
Jens Axboe2b188cc2019-01-07 10:46:33 -07003709 return 0;
3710}
3711
3712/*
3713 * Allocate an anonymous fd, this is what constitutes the application
3714 * visible backing of an io_uring instance. The application mmaps this
3715 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
3716 * we have to tie this fd to a socket for file garbage collection purposes.
3717 */
3718static int io_uring_get_fd(struct io_ring_ctx *ctx)
3719{
3720 struct file *file;
3721 int ret;
3722
3723#if defined(CONFIG_UNIX)
3724 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
3725 &ctx->ring_sock);
3726 if (ret)
3727 return ret;
3728#endif
3729
3730 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
3731 if (ret < 0)
3732 goto err;
3733
3734 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
3735 O_RDWR | O_CLOEXEC);
3736 if (IS_ERR(file)) {
3737 put_unused_fd(ret);
3738 ret = PTR_ERR(file);
3739 goto err;
3740 }
3741
3742#if defined(CONFIG_UNIX)
3743 ctx->ring_sock->file = file;
Jens Axboe6b063142019-01-10 22:13:58 -07003744 ctx->ring_sock->sk->sk_user_data = ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003745#endif
3746 fd_install(ret, file);
3747 return ret;
3748err:
3749#if defined(CONFIG_UNIX)
3750 sock_release(ctx->ring_sock);
3751 ctx->ring_sock = NULL;
3752#endif
3753 return ret;
3754}
3755
3756static int io_uring_create(unsigned entries, struct io_uring_params *p)
3757{
3758 struct user_struct *user = NULL;
3759 struct io_ring_ctx *ctx;
3760 bool account_mem;
3761 int ret;
3762
3763 if (!entries || entries > IORING_MAX_ENTRIES)
3764 return -EINVAL;
3765
3766 /*
3767 * Use twice as many entries for the CQ ring. It's possible for the
3768 * application to drive a higher depth than the size of the SQ ring,
3769 * since the sqes are only used at submission time. This allows for
3770 * some flexibility in overcommitting a bit.
3771 */
3772 p->sq_entries = roundup_pow_of_two(entries);
3773 p->cq_entries = 2 * p->sq_entries;
3774
3775 user = get_uid(current_user());
3776 account_mem = !capable(CAP_IPC_LOCK);
3777
3778 if (account_mem) {
3779 ret = io_account_mem(user,
3780 ring_pages(p->sq_entries, p->cq_entries));
3781 if (ret) {
3782 free_uid(user);
3783 return ret;
3784 }
3785 }
3786
3787 ctx = io_ring_ctx_alloc(p);
3788 if (!ctx) {
3789 if (account_mem)
3790 io_unaccount_mem(user, ring_pages(p->sq_entries,
3791 p->cq_entries));
3792 free_uid(user);
3793 return -ENOMEM;
3794 }
3795 ctx->compat = in_compat_syscall();
3796 ctx->account_mem = account_mem;
3797 ctx->user = user;
3798
3799 ret = io_allocate_scq_urings(ctx, p);
3800 if (ret)
3801 goto err;
3802
Jens Axboe6c271ce2019-01-10 11:22:30 -07003803 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003804 if (ret)
3805 goto err;
3806
3807 ret = io_uring_get_fd(ctx);
3808 if (ret < 0)
3809 goto err;
3810
3811 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00003812 p->sq_off.head = offsetof(struct io_rings, sq.head);
3813 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
3814 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
3815 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
3816 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
3817 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
3818 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003819
3820 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00003821 p->cq_off.head = offsetof(struct io_rings, cq.head);
3822 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
3823 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
3824 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
3825 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
3826 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06003827
3828 p->features = IORING_FEAT_SINGLE_MMAP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003829 return ret;
3830err:
3831 io_ring_ctx_wait_and_kill(ctx);
3832 return ret;
3833}
3834
3835/*
3836 * Sets up an aio uring context, and returns the fd. Applications asks for a
3837 * ring size, we return the actual sq/cq ring sizes (among other things) in the
3838 * params structure passed in.
3839 */
3840static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
3841{
3842 struct io_uring_params p;
3843 long ret;
3844 int i;
3845
3846 if (copy_from_user(&p, params, sizeof(p)))
3847 return -EFAULT;
3848 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
3849 if (p.resv[i])
3850 return -EINVAL;
3851 }
3852
Jens Axboe6c271ce2019-01-10 11:22:30 -07003853 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
3854 IORING_SETUP_SQ_AFF))
Jens Axboe2b188cc2019-01-07 10:46:33 -07003855 return -EINVAL;
3856
3857 ret = io_uring_create(entries, &p);
3858 if (ret < 0)
3859 return ret;
3860
3861 if (copy_to_user(params, &p, sizeof(p)))
3862 return -EFAULT;
3863
3864 return ret;
3865}
3866
3867SYSCALL_DEFINE2(io_uring_setup, u32, entries,
3868 struct io_uring_params __user *, params)
3869{
3870 return io_uring_setup(entries, params);
3871}
3872
Jens Axboeedafcce2019-01-09 09:16:05 -07003873static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
3874 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06003875 __releases(ctx->uring_lock)
3876 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07003877{
3878 int ret;
3879
Jens Axboe35fa71a2019-04-22 10:23:23 -06003880 /*
3881 * We're inside the ring mutex, if the ref is already dying, then
3882 * someone else killed the ctx or is already going through
3883 * io_uring_register().
3884 */
3885 if (percpu_ref_is_dying(&ctx->refs))
3886 return -ENXIO;
3887
Jens Axboeedafcce2019-01-09 09:16:05 -07003888 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06003889
3890 /*
3891 * Drop uring mutex before waiting for references to exit. If another
3892 * thread is currently inside io_uring_enter() it might need to grab
3893 * the uring_lock to make progress. If we hold it here across the drain
3894 * wait, then we can deadlock. It's safe to drop the mutex here, since
3895 * no new references will come in after we've killed the percpu ref.
3896 */
3897 mutex_unlock(&ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07003898 wait_for_completion(&ctx->ctx_done);
Jens Axboeb19062a2019-04-15 10:49:38 -06003899 mutex_lock(&ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07003900
3901 switch (opcode) {
3902 case IORING_REGISTER_BUFFERS:
3903 ret = io_sqe_buffer_register(ctx, arg, nr_args);
3904 break;
3905 case IORING_UNREGISTER_BUFFERS:
3906 ret = -EINVAL;
3907 if (arg || nr_args)
3908 break;
3909 ret = io_sqe_buffer_unregister(ctx);
3910 break;
Jens Axboe6b063142019-01-10 22:13:58 -07003911 case IORING_REGISTER_FILES:
3912 ret = io_sqe_files_register(ctx, arg, nr_args);
3913 break;
3914 case IORING_UNREGISTER_FILES:
3915 ret = -EINVAL;
3916 if (arg || nr_args)
3917 break;
3918 ret = io_sqe_files_unregister(ctx);
3919 break;
Jens Axboe9b402842019-04-11 11:45:41 -06003920 case IORING_REGISTER_EVENTFD:
3921 ret = -EINVAL;
3922 if (nr_args != 1)
3923 break;
3924 ret = io_eventfd_register(ctx, arg);
3925 break;
3926 case IORING_UNREGISTER_EVENTFD:
3927 ret = -EINVAL;
3928 if (arg || nr_args)
3929 break;
3930 ret = io_eventfd_unregister(ctx);
3931 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07003932 default:
3933 ret = -EINVAL;
3934 break;
3935 }
3936
3937 /* bring the ctx back to life */
3938 reinit_completion(&ctx->ctx_done);
3939 percpu_ref_reinit(&ctx->refs);
3940 return ret;
3941}
3942
3943SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
3944 void __user *, arg, unsigned int, nr_args)
3945{
3946 struct io_ring_ctx *ctx;
3947 long ret = -EBADF;
3948 struct fd f;
3949
3950 f = fdget(fd);
3951 if (!f.file)
3952 return -EBADF;
3953
3954 ret = -EOPNOTSUPP;
3955 if (f.file->f_op != &io_uring_fops)
3956 goto out_fput;
3957
3958 ctx = f.file->private_data;
3959
3960 mutex_lock(&ctx->uring_lock);
3961 ret = __io_uring_register(ctx, opcode, arg, nr_args);
3962 mutex_unlock(&ctx->uring_lock);
3963out_fput:
3964 fdput(f);
3965 return ret;
3966}
3967
Jens Axboe2b188cc2019-01-07 10:46:33 -07003968static int __init io_uring_init(void)
3969{
3970 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
3971 return 0;
3972};
3973__initcall(io_uring_init);