blob: 737c311c6da53df808033f5fac29d0de7b2e7ffb [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>
Jens Axboe6c271ce2019-01-10 11:22:30 -070059#include <linux/kthread.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070060#include <linux/blkdev.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070061#include <linux/bvec.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070062#include <linux/net.h>
63#include <net/sock.h>
64#include <net/af_unix.h>
Jens Axboe6b063142019-01-10 22:13:58 -070065#include <net/scm.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070066#include <linux/anon_inodes.h>
67#include <linux/sched/mm.h>
68#include <linux/uaccess.h>
69#include <linux/nospec.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070070#include <linux/sizes.h>
71#include <linux/hugetlb.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070072
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020073#define CREATE_TRACE_POINTS
74#include <trace/events/io_uring.h>
75
Jens Axboe2b188cc2019-01-07 10:46:33 -070076#include <uapi/linux/io_uring.h>
77
78#include "internal.h"
Jens Axboe561fb042019-10-24 07:25:42 -060079#include "io-wq.h"
Jens Axboe2b188cc2019-01-07 10:46:33 -070080
Daniel Xu5277dea2019-09-14 14:23:45 -070081#define IORING_MAX_ENTRIES 32768
Jens Axboe33a107f2019-10-04 12:10:03 -060082#define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES)
Jens Axboe65e19f52019-10-26 07:20:21 -060083
84/*
85 * Shift of 9 is 512 entries, or exactly one page on 64-bit archs
86 */
87#define IORING_FILE_TABLE_SHIFT 9
88#define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT)
89#define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1)
90#define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE)
Jens Axboe2b188cc2019-01-07 10:46:33 -070091
92struct io_uring {
93 u32 head ____cacheline_aligned_in_smp;
94 u32 tail ____cacheline_aligned_in_smp;
95};
96
Stefan Bühler1e84b972019-04-24 23:54:16 +020097/*
Hristo Venev75b28af2019-08-26 17:23:46 +000098 * This data is shared with the application through the mmap at offsets
99 * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200100 *
101 * The offsets to the member fields are published through struct
102 * io_sqring_offsets when calling io_uring_setup.
103 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000104struct io_rings {
Stefan Bühler1e84b972019-04-24 23:54:16 +0200105 /*
106 * Head and tail offsets into the ring; the offsets need to be
107 * masked to get valid indices.
108 *
Hristo Venev75b28af2019-08-26 17:23:46 +0000109 * The kernel controls head of the sq ring and the tail of the cq ring,
110 * and the application controls tail of the sq ring and the head of the
111 * cq ring.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200112 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000113 struct io_uring sq, cq;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200114 /*
Hristo Venev75b28af2019-08-26 17:23:46 +0000115 * Bitmasks to apply to head and tail offsets (constant, equals
Stefan Bühler1e84b972019-04-24 23:54:16 +0200116 * ring_entries - 1)
117 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000118 u32 sq_ring_mask, cq_ring_mask;
119 /* Ring sizes (constant, power of 2) */
120 u32 sq_ring_entries, cq_ring_entries;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200121 /*
122 * Number of invalid entries dropped by the kernel due to
123 * invalid index stored in array
124 *
125 * Written by the kernel, shouldn't be modified by the
126 * application (i.e. get number of "new events" by comparing to
127 * cached value).
128 *
129 * After a new SQ head value was read by the application this
130 * counter includes all submissions that were dropped reaching
131 * the new SQ head (and possibly more).
132 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000133 u32 sq_dropped;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200134 /*
135 * Runtime flags
136 *
137 * Written by the kernel, shouldn't be modified by the
138 * application.
139 *
140 * The application needs a full memory barrier before checking
141 * for IORING_SQ_NEED_WAKEUP after updating the sq tail.
142 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000143 u32 sq_flags;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200144 /*
145 * Number of completion events lost because the queue was full;
146 * this should be avoided by the application by making sure
147 * there are not more requests pending thatn there is space in
148 * the completion queue.
149 *
150 * Written by the kernel, shouldn't be modified by the
151 * application (i.e. get number of "new events" by comparing to
152 * cached value).
153 *
154 * As completion events come in out of order this counter is not
155 * ordered with any other data.
156 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000157 u32 cq_overflow;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200158 /*
159 * Ring buffer of completion events.
160 *
161 * The kernel writes completion events fresh every time they are
162 * produced, so the application is allowed to modify pending
163 * entries.
164 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000165 struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700166};
167
Jens Axboeedafcce2019-01-09 09:16:05 -0700168struct io_mapped_ubuf {
169 u64 ubuf;
170 size_t len;
171 struct bio_vec *bvec;
172 unsigned int nr_bvecs;
173};
174
Jens Axboe65e19f52019-10-26 07:20:21 -0600175struct fixed_file_table {
176 struct file **files;
177};
178
Jens Axboe2b188cc2019-01-07 10:46:33 -0700179struct io_ring_ctx {
180 struct {
181 struct percpu_ref refs;
182 } ____cacheline_aligned_in_smp;
183
184 struct {
185 unsigned int flags;
186 bool compat;
187 bool account_mem;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700188 bool cq_overflow_flushed;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700189
Hristo Venev75b28af2019-08-26 17:23:46 +0000190 /*
191 * Ring buffer of indices into array of io_uring_sqe, which is
192 * mmapped by the application using the IORING_OFF_SQES offset.
193 *
194 * This indirection could e.g. be used to assign fixed
195 * io_uring_sqe entries to operations and only submit them to
196 * the queue when needed.
197 *
198 * The kernel modifies neither the indices array nor the entries
199 * array.
200 */
201 u32 *sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700202 unsigned cached_sq_head;
203 unsigned sq_entries;
204 unsigned sq_mask;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700205 unsigned sq_thread_idle;
Jens Axboe498ccd92019-10-25 10:04:25 -0600206 unsigned cached_sq_dropped;
Jens Axboe206aefd2019-11-07 18:27:42 -0700207 atomic_t cached_cq_overflow;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700208 struct io_uring_sqe *sq_sqes;
Jens Axboede0617e2019-04-06 21:51:27 -0600209
210 struct list_head defer_list;
Jens Axboe5262f562019-09-17 12:26:57 -0600211 struct list_head timeout_list;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700212 struct list_head cq_overflow_list;
Jens Axboefcb323c2019-10-24 12:39:47 -0600213
214 wait_queue_head_t inflight_wait;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700215 } ____cacheline_aligned_in_smp;
216
Jens Axboe206aefd2019-11-07 18:27:42 -0700217 struct io_rings *rings;
218
Jens Axboe2b188cc2019-01-07 10:46:33 -0700219 /* IO offload */
Jens Axboe561fb042019-10-24 07:25:42 -0600220 struct io_wq *io_wq;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700221 struct task_struct *sqo_thread; /* if using sq thread polling */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700222 struct mm_struct *sqo_mm;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700223 wait_queue_head_t sqo_wait;
Hristo Venev75b28af2019-08-26 17:23:46 +0000224
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 */
Jens Axboe65e19f52019-10-26 07:20:21 -0600230 struct fixed_file_table *file_table;
Jens Axboe6b063142019-01-10 22:13:58 -0700231 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
Jens Axboe206aefd2019-11-07 18:27:42 -0700239 /* 0 is for ctx quiesce/reinit/free, 1 is for sqo_thread started */
240 struct completion *completions;
241
242#if defined(CONFIG_UNIX)
243 struct socket *ring_sock;
244#endif
245
246 struct {
247 unsigned cached_cq_tail;
248 unsigned cq_entries;
249 unsigned cq_mask;
250 atomic_t cq_timeouts;
251 struct wait_queue_head cq_wait;
252 struct fasync_struct *cq_fasync;
253 struct eventfd_ctx *cq_ev_fd;
254 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700255
256 struct {
257 struct mutex uring_lock;
258 wait_queue_head_t wait;
259 } ____cacheline_aligned_in_smp;
260
261 struct {
262 spinlock_t completion_lock;
Jens Axboedef596e2019-01-09 08:59:42 -0700263 bool poll_multi_file;
264 /*
265 * ->poll_list is protected by the ctx->uring_lock for
266 * io_uring instances that don't use IORING_SETUP_SQPOLL.
267 * For SQPOLL, only the single threaded io_sq_thread() will
268 * manipulate the list, hence no extra locking is needed there.
269 */
270 struct list_head poll_list;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700271 struct list_head cancel_list;
Jens Axboefcb323c2019-10-24 12:39:47 -0600272
273 spinlock_t inflight_lock;
274 struct list_head inflight_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700275 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700276};
277
278struct sqe_submit {
279 const struct io_uring_sqe *sqe;
Jens Axboefcb323c2019-10-24 12:39:47 -0600280 struct file *ring_file;
281 int ring_fd;
Jackie Liu8776f3f2019-09-09 20:50:39 +0800282 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700283 bool has_user;
Jackie Liuba5290c2019-10-09 09:19:59 +0800284 bool in_async;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700285 bool needs_fixed_file;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700286};
287
Jens Axboe09bb8392019-03-13 12:39:28 -0600288/*
289 * First field must be the file pointer in all the
290 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
291 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700292struct io_poll_iocb {
293 struct file *file;
294 struct wait_queue_head *head;
295 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600296 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700297 bool canceled;
298 struct wait_queue_entry wait;
299};
300
Jens Axboe5262f562019-09-17 12:26:57 -0600301struct io_timeout {
302 struct file *file;
303 struct hrtimer timer;
304};
305
Jens Axboe09bb8392019-03-13 12:39:28 -0600306/*
307 * NOTE! Each of the iocb union members has the file pointer
308 * as the first entry in their struct definition. So you can
309 * access the file pointer through any of the sub-structs,
310 * or directly as just 'ki_filp' in this struct.
311 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700312struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700313 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600314 struct file *file;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700315 struct kiocb rw;
316 struct io_poll_iocb poll;
Jens Axboe5262f562019-09-17 12:26:57 -0600317 struct io_timeout timeout;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700318 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700319
320 struct sqe_submit submit;
321
322 struct io_ring_ctx *ctx;
323 struct list_head list;
Jens Axboe9e645e112019-05-10 16:07:28 -0600324 struct list_head link_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700325 unsigned int flags;
Jens Axboec16361c2019-01-17 08:39:48 -0700326 refcount_t refs;
Stefan Bühler8449eed2019-04-27 20:34:19 +0200327#define REQ_F_NOWAIT 1 /* must not punt to workers */
Jens Axboedef596e2019-01-09 08:59:42 -0700328#define REQ_F_IOPOLL_COMPLETED 2 /* polled IO has completed */
Jens Axboe6b063142019-01-10 22:13:58 -0700329#define REQ_F_FIXED_FILE 4 /* ctx owns file */
Jens Axboe31b51512019-01-18 22:56:34 -0700330#define REQ_F_SEQ_PREV 8 /* sequential with previous */
Stefan Bühlere2033e32019-05-11 19:08:01 +0200331#define REQ_F_IO_DRAIN 16 /* drain existing IO first */
332#define REQ_F_IO_DRAINED 32 /* drain done */
Jens Axboe9e645e112019-05-10 16:07:28 -0600333#define REQ_F_LINK 64 /* linked sqes */
Jens Axboe2665abf2019-11-05 12:40:47 -0700334#define REQ_F_LINK_TIMEOUT 128 /* has linked timeout */
Zhengyuan Liuf7b76ac2019-07-16 23:26:14 +0800335#define REQ_F_FAIL_LINK 256 /* fail rest of links */
Jackie Liu4fe2c962019-09-09 20:50:40 +0800336#define REQ_F_SHADOW_DRAIN 512 /* link-drain shadow req */
Jens Axboe5262f562019-09-17 12:26:57 -0600337#define REQ_F_TIMEOUT 1024 /* timeout request */
Jens Axboe491381ce2019-10-17 09:20:46 -0600338#define REQ_F_ISREG 2048 /* regular file */
339#define REQ_F_MUST_PUNT 4096 /* must be punted even for NONBLOCK */
Jens Axboefcb323c2019-10-24 12:39:47 -0600340#define REQ_F_INFLIGHT 8192 /* on inflight list */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700341 u64 user_data;
Jens Axboe9e645e112019-05-10 16:07:28 -0600342 u32 result;
Jens Axboede0617e2019-04-06 21:51:27 -0600343 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700344
Jens Axboefcb323c2019-10-24 12:39:47 -0600345 struct list_head inflight_entry;
346
Jens Axboe561fb042019-10-24 07:25:42 -0600347 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700348};
349
350#define IO_PLUG_THRESHOLD 2
Jens Axboedef596e2019-01-09 08:59:42 -0700351#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700352
Jens Axboe9a56a232019-01-09 09:06:50 -0700353struct io_submit_state {
354 struct blk_plug plug;
355
356 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700357 * io_kiocb alloc cache
358 */
359 void *reqs[IO_IOPOLL_BATCH];
360 unsigned int free_reqs;
361 unsigned int cur_req;
362
363 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700364 * File reference cache
365 */
366 struct file *file;
367 unsigned int fd;
368 unsigned int has_refs;
369 unsigned int used_refs;
370 unsigned int ios_left;
371};
372
Jens Axboe561fb042019-10-24 07:25:42 -0600373static void io_wq_submit_work(struct io_wq_work **workptr);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700374static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liu4fe2c962019-09-09 20:50:40 +0800375static void __io_free_req(struct io_kiocb *req);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800376static void io_put_req(struct io_kiocb *req);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700377static void io_double_put_req(struct io_kiocb *req);
Jens Axboede0617e2019-04-06 21:51:27 -0600378
Jens Axboe2b188cc2019-01-07 10:46:33 -0700379static struct kmem_cache *req_cachep;
380
381static const struct file_operations io_uring_fops;
382
383struct sock *io_uring_get_socket(struct file *file)
384{
385#if defined(CONFIG_UNIX)
386 if (file->f_op == &io_uring_fops) {
387 struct io_ring_ctx *ctx = file->private_data;
388
389 return ctx->ring_sock->sk;
390 }
391#endif
392 return NULL;
393}
394EXPORT_SYMBOL(io_uring_get_socket);
395
396static void io_ring_ctx_ref_free(struct percpu_ref *ref)
397{
398 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
399
Jens Axboe206aefd2019-11-07 18:27:42 -0700400 complete(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700401}
402
403static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
404{
405 struct io_ring_ctx *ctx;
406
407 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
408 if (!ctx)
409 return NULL;
410
Jens Axboe206aefd2019-11-07 18:27:42 -0700411 ctx->completions = kmalloc(2 * sizeof(struct completion), GFP_KERNEL);
412 if (!ctx->completions)
413 goto err;
414
Roman Gushchin21482892019-05-07 10:01:48 -0700415 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -0700416 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
417 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700418
419 ctx->flags = p->flags;
420 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700421 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe206aefd2019-11-07 18:27:42 -0700422 init_completion(&ctx->completions[0]);
423 init_completion(&ctx->completions[1]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700424 mutex_init(&ctx->uring_lock);
425 init_waitqueue_head(&ctx->wait);
426 spin_lock_init(&ctx->completion_lock);
Jens Axboedef596e2019-01-09 08:59:42 -0700427 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboe221c5eb2019-01-17 09:41:58 -0700428 INIT_LIST_HEAD(&ctx->cancel_list);
Jens Axboede0617e2019-04-06 21:51:27 -0600429 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -0600430 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -0600431 init_waitqueue_head(&ctx->inflight_wait);
432 spin_lock_init(&ctx->inflight_lock);
433 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700434 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -0700435err:
436 kfree(ctx->completions);
437 kfree(ctx);
438 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700439}
440
Jackie Liua197f662019-11-08 08:09:12 -0700441static inline bool __io_sequence_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -0600442{
Jackie Liua197f662019-11-08 08:09:12 -0700443 struct io_ring_ctx *ctx = req->ctx;
444
Jens Axboe498ccd92019-10-25 10:04:25 -0600445 return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped
446 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboede0617e2019-04-06 21:51:27 -0600447}
448
Jackie Liua197f662019-11-08 08:09:12 -0700449static inline bool io_sequence_defer(struct io_kiocb *req)
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600450{
451 if ((req->flags & (REQ_F_IO_DRAIN|REQ_F_IO_DRAINED)) != REQ_F_IO_DRAIN)
452 return false;
453
Jackie Liua197f662019-11-08 08:09:12 -0700454 return __io_sequence_defer(req);
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600455}
456
457static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -0600458{
459 struct io_kiocb *req;
460
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600461 req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list);
Jackie Liua197f662019-11-08 08:09:12 -0700462 if (req && !io_sequence_defer(req)) {
Jens Axboede0617e2019-04-06 21:51:27 -0600463 list_del_init(&req->list);
464 return req;
465 }
466
467 return NULL;
468}
469
Jens Axboe5262f562019-09-17 12:26:57 -0600470static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx)
471{
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600472 struct io_kiocb *req;
473
474 req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list);
Jackie Liua197f662019-11-08 08:09:12 -0700475 if (req && !__io_sequence_defer(req)) {
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600476 list_del_init(&req->list);
477 return req;
478 }
479
480 return NULL;
Jens Axboe5262f562019-09-17 12:26:57 -0600481}
482
Jens Axboede0617e2019-04-06 21:51:27 -0600483static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700484{
Hristo Venev75b28af2019-08-26 17:23:46 +0000485 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700486
Hristo Venev75b28af2019-08-26 17:23:46 +0000487 if (ctx->cached_cq_tail != READ_ONCE(rings->cq.tail)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700488 /* order cqe stores with ring update */
Hristo Venev75b28af2019-08-26 17:23:46 +0000489 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700490
Jens Axboe2b188cc2019-01-07 10:46:33 -0700491 if (wq_has_sleeper(&ctx->cq_wait)) {
492 wake_up_interruptible(&ctx->cq_wait);
493 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
494 }
495 }
496}
497
Jens Axboe561fb042019-10-24 07:25:42 -0600498static inline bool io_sqe_needs_user(const struct io_uring_sqe *sqe)
Jens Axboe18d9be12019-09-10 09:13:05 -0600499{
Jens Axboe561fb042019-10-24 07:25:42 -0600500 u8 opcode = READ_ONCE(sqe->opcode);
501
502 return !(opcode == IORING_OP_READ_FIXED ||
503 opcode == IORING_OP_WRITE_FIXED);
504}
505
506static inline bool io_prep_async_work(struct io_kiocb *req)
507{
508 bool do_hashed = false;
Jens Axboe54a91f32019-09-10 09:15:04 -0600509
Jens Axboe6cc47d12019-09-18 11:18:23 -0600510 if (req->submit.sqe) {
511 switch (req->submit.sqe->opcode) {
512 case IORING_OP_WRITEV:
513 case IORING_OP_WRITE_FIXED:
Jens Axboe561fb042019-10-24 07:25:42 -0600514 do_hashed = true;
Jens Axboe5f8fd2d32019-11-07 10:57:36 -0700515 /* fall-through */
516 case IORING_OP_READV:
517 case IORING_OP_READ_FIXED:
518 case IORING_OP_SENDMSG:
519 case IORING_OP_RECVMSG:
520 case IORING_OP_ACCEPT:
521 case IORING_OP_POLL_ADD:
522 /*
523 * We know REQ_F_ISREG is not set on some of these
524 * opcodes, but this enables us to keep the check in
525 * just one place.
526 */
527 if (!(req->flags & REQ_F_ISREG))
528 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe6cc47d12019-09-18 11:18:23 -0600529 break;
530 }
Jens Axboe561fb042019-10-24 07:25:42 -0600531 if (io_sqe_needs_user(req->submit.sqe))
532 req->work.flags |= IO_WQ_WORK_NEEDS_USER;
Jens Axboe54a91f32019-09-10 09:15:04 -0600533 }
534
Jens Axboe561fb042019-10-24 07:25:42 -0600535 return do_hashed;
536}
537
Jackie Liua197f662019-11-08 08:09:12 -0700538static inline void io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -0600539{
540 bool do_hashed = io_prep_async_work(req);
Jackie Liua197f662019-11-08 08:09:12 -0700541 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe561fb042019-10-24 07:25:42 -0600542
543 trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work,
544 req->flags);
545 if (!do_hashed) {
546 io_wq_enqueue(ctx->io_wq, &req->work);
547 } else {
548 io_wq_enqueue_hashed(ctx->io_wq, &req->work,
549 file_inode(req->file));
550 }
Jens Axboe18d9be12019-09-10 09:13:05 -0600551}
552
Jens Axboe5262f562019-09-17 12:26:57 -0600553static void io_kill_timeout(struct io_kiocb *req)
554{
555 int ret;
556
557 ret = hrtimer_try_to_cancel(&req->timeout.timer);
558 if (ret != -1) {
559 atomic_inc(&req->ctx->cq_timeouts);
Jens Axboe842f9612019-10-29 12:34:10 -0600560 list_del_init(&req->list);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700561 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800562 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -0600563 }
564}
565
566static void io_kill_timeouts(struct io_ring_ctx *ctx)
567{
568 struct io_kiocb *req, *tmp;
569
570 spin_lock_irq(&ctx->completion_lock);
571 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
572 io_kill_timeout(req);
573 spin_unlock_irq(&ctx->completion_lock);
574}
575
Jens Axboede0617e2019-04-06 21:51:27 -0600576static void io_commit_cqring(struct io_ring_ctx *ctx)
577{
578 struct io_kiocb *req;
579
Jens Axboe5262f562019-09-17 12:26:57 -0600580 while ((req = io_get_timeout_req(ctx)) != NULL)
581 io_kill_timeout(req);
582
Jens Axboede0617e2019-04-06 21:51:27 -0600583 __io_commit_cqring(ctx);
584
585 while ((req = io_get_deferred_req(ctx)) != NULL) {
Jackie Liu4fe2c962019-09-09 20:50:40 +0800586 if (req->flags & REQ_F_SHADOW_DRAIN) {
587 /* Just for drain, free it. */
588 __io_free_req(req);
589 continue;
590 }
Jens Axboede0617e2019-04-06 21:51:27 -0600591 req->flags |= REQ_F_IO_DRAINED;
Jackie Liua197f662019-11-08 08:09:12 -0700592 io_queue_async_work(req);
Jens Axboede0617e2019-04-06 21:51:27 -0600593 }
594}
595
Jens Axboe2b188cc2019-01-07 10:46:33 -0700596static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
597{
Hristo Venev75b28af2019-08-26 17:23:46 +0000598 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700599 unsigned tail;
600
601 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +0200602 /*
603 * writes to the cq entry need to come after reading head; the
604 * control dependency is enough as we're using WRITE_ONCE to
605 * fill the cq entry
606 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000607 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700608 return NULL;
609
610 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +0000611 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -0700612}
613
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700614static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
615{
616 if (waitqueue_active(&ctx->wait))
617 wake_up(&ctx->wait);
618 if (waitqueue_active(&ctx->sqo_wait))
619 wake_up(&ctx->sqo_wait);
620 if (ctx->cq_ev_fd)
621 eventfd_signal(ctx->cq_ev_fd, 1);
622}
623
624static void io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
625{
626 struct io_rings *rings = ctx->rings;
627 struct io_uring_cqe *cqe;
628 struct io_kiocb *req;
629 unsigned long flags;
630 LIST_HEAD(list);
631
632 if (!force) {
633 if (list_empty_careful(&ctx->cq_overflow_list))
634 return;
635 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
636 rings->cq_ring_entries))
637 return;
638 }
639
640 spin_lock_irqsave(&ctx->completion_lock, flags);
641
642 /* if force is set, the ring is going away. always drop after that */
643 if (force)
644 ctx->cq_overflow_flushed = true;
645
646 while (!list_empty(&ctx->cq_overflow_list)) {
647 cqe = io_get_cqring(ctx);
648 if (!cqe && !force)
649 break;
650
651 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
652 list);
653 list_move(&req->list, &list);
654 if (cqe) {
655 WRITE_ONCE(cqe->user_data, req->user_data);
656 WRITE_ONCE(cqe->res, req->result);
657 WRITE_ONCE(cqe->flags, 0);
658 } else {
659 WRITE_ONCE(ctx->rings->cq_overflow,
660 atomic_inc_return(&ctx->cached_cq_overflow));
661 }
662 }
663
664 io_commit_cqring(ctx);
665 spin_unlock_irqrestore(&ctx->completion_lock, flags);
666 io_cqring_ev_posted(ctx);
667
668 while (!list_empty(&list)) {
669 req = list_first_entry(&list, struct io_kiocb, list);
670 list_del(&req->list);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800671 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700672 }
673}
674
Jens Axboe78e19bb2019-11-06 15:21:34 -0700675static void io_cqring_fill_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700676{
Jens Axboe78e19bb2019-11-06 15:21:34 -0700677 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700678 struct io_uring_cqe *cqe;
679
Jens Axboe78e19bb2019-11-06 15:21:34 -0700680 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -0700681
Jens Axboe2b188cc2019-01-07 10:46:33 -0700682 /*
683 * If we can't get a cq entry, userspace overflowed the
684 * submission (by quite a lot). Increment the overflow count in
685 * the ring.
686 */
687 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700688 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -0700689 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700690 WRITE_ONCE(cqe->res, res);
Jens Axboec71ffb62019-05-13 20:58:29 -0600691 WRITE_ONCE(cqe->flags, 0);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700692 } else if (ctx->cq_overflow_flushed) {
Jens Axboe498ccd92019-10-25 10:04:25 -0600693 WRITE_ONCE(ctx->rings->cq_overflow,
694 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700695 } else {
696 refcount_inc(&req->refs);
697 req->result = res;
698 list_add_tail(&req->list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700699 }
700}
701
Jens Axboe78e19bb2019-11-06 15:21:34 -0700702static void io_cqring_add_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700703{
Jens Axboe78e19bb2019-11-06 15:21:34 -0700704 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700705 unsigned long flags;
706
707 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700708 io_cqring_fill_event(req, res);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700709 io_commit_cqring(ctx);
710 spin_unlock_irqrestore(&ctx->completion_lock, flags);
711
Jens Axboe8c838782019-03-12 15:48:16 -0600712 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700713}
714
Jens Axboe2579f912019-01-09 09:10:43 -0700715static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
716 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700717{
Jens Axboefd6fab22019-03-14 16:30:06 -0600718 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700719 struct io_kiocb *req;
720
721 if (!percpu_ref_tryget(&ctx->refs))
722 return NULL;
723
Jens Axboe2579f912019-01-09 09:10:43 -0700724 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -0600725 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -0700726 if (unlikely(!req))
727 goto out;
728 } else if (!state->free_reqs) {
729 size_t sz;
730 int ret;
731
732 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -0600733 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
734
735 /*
736 * Bulk alloc is all-or-nothing. If we fail to get a batch,
737 * retry single alloc to be on the safe side.
738 */
739 if (unlikely(ret <= 0)) {
740 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
741 if (!state->reqs[0])
742 goto out;
743 ret = 1;
744 }
Jens Axboe2579f912019-01-09 09:10:43 -0700745 state->free_reqs = ret - 1;
746 state->cur_req = 1;
747 req = state->reqs[0];
748 } else {
749 req = state->reqs[state->cur_req];
750 state->free_reqs--;
751 state->cur_req++;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700752 }
753
Jens Axboe60c112b2019-06-21 10:20:18 -0600754 req->file = NULL;
Jens Axboe2579f912019-01-09 09:10:43 -0700755 req->ctx = ctx;
756 req->flags = 0;
Jens Axboee65ef562019-03-12 10:16:44 -0600757 /* one is dropped after submission, the other at completion */
758 refcount_set(&req->refs, 2);
Jens Axboe9e645e112019-05-10 16:07:28 -0600759 req->result = 0;
Jens Axboe561fb042019-10-24 07:25:42 -0600760 INIT_IO_WORK(&req->work, io_wq_submit_work);
Jens Axboe2579f912019-01-09 09:10:43 -0700761 return req;
762out:
Pavel Begunkov6805b322019-10-08 02:18:42 +0300763 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700764 return NULL;
765}
766
Jens Axboedef596e2019-01-09 08:59:42 -0700767static void io_free_req_many(struct io_ring_ctx *ctx, void **reqs, int *nr)
768{
769 if (*nr) {
770 kmem_cache_free_bulk(req_cachep, *nr, reqs);
Pavel Begunkov6805b322019-10-08 02:18:42 +0300771 percpu_ref_put_many(&ctx->refs, *nr);
Jens Axboedef596e2019-01-09 08:59:42 -0700772 *nr = 0;
773 }
774}
775
Jens Axboe9e645e112019-05-10 16:07:28 -0600776static void __io_free_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700777{
Jens Axboefcb323c2019-10-24 12:39:47 -0600778 struct io_ring_ctx *ctx = req->ctx;
779
Jens Axboe09bb8392019-03-13 12:39:28 -0600780 if (req->file && !(req->flags & REQ_F_FIXED_FILE))
781 fput(req->file);
Jens Axboefcb323c2019-10-24 12:39:47 -0600782 if (req->flags & REQ_F_INFLIGHT) {
783 unsigned long flags;
784
785 spin_lock_irqsave(&ctx->inflight_lock, flags);
786 list_del(&req->inflight_entry);
787 if (waitqueue_active(&ctx->inflight_wait))
788 wake_up(&ctx->inflight_wait);
789 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
790 }
791 percpu_ref_put(&ctx->refs);
Jens Axboee65ef562019-03-12 10:16:44 -0600792 kmem_cache_free(req_cachep, req);
793}
794
Jackie Liua197f662019-11-08 08:09:12 -0700795static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -0700796{
Jackie Liua197f662019-11-08 08:09:12 -0700797 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -0700798 int ret;
799
800 ret = hrtimer_try_to_cancel(&req->timeout.timer);
801 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -0700802 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -0700803 io_commit_cqring(ctx);
804 req->flags &= ~REQ_F_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +0800805 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -0700806 return true;
807 }
808
809 return false;
810}
811
Jens Axboeba816ad2019-09-28 11:36:45 -0600812static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -0600813{
Jens Axboe2665abf2019-11-05 12:40:47 -0700814 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -0600815 struct io_kiocb *nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -0700816 bool wake_ev = false;
Jens Axboe9e645e112019-05-10 16:07:28 -0600817
818 /*
819 * The list should never be empty when we are called here. But could
820 * potentially happen if the chain is messed up, check to be on the
821 * safe side.
822 */
823 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, list);
Jens Axboe2665abf2019-11-05 12:40:47 -0700824 while (nxt) {
Jens Axboe9e645e112019-05-10 16:07:28 -0600825 list_del(&nxt->list);
826 if (!list_empty(&req->link_list)) {
827 INIT_LIST_HEAD(&nxt->link_list);
828 list_splice(&req->link_list, &nxt->link_list);
829 nxt->flags |= REQ_F_LINK;
830 }
831
Jens Axboeba816ad2019-09-28 11:36:45 -0600832 /*
833 * If we're in async work, we can continue processing the chain
834 * in this context instead of having to queue up new async work.
835 */
Jens Axboe2665abf2019-11-05 12:40:47 -0700836 if (req->flags & REQ_F_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -0700837 wake_ev = io_link_cancel_timeout(nxt);
Jens Axboe2665abf2019-11-05 12:40:47 -0700838
839 /* we dropped this link, get next */
840 nxt = list_first_entry_or_null(&req->link_list,
841 struct io_kiocb, list);
842 } else if (nxtptr && current_work()) {
Jens Axboeba816ad2019-09-28 11:36:45 -0600843 *nxtptr = nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -0700844 break;
845 } else {
Jackie Liua197f662019-11-08 08:09:12 -0700846 io_queue_async_work(nxt);
Jens Axboe2665abf2019-11-05 12:40:47 -0700847 break;
848 }
Jens Axboe9e645e112019-05-10 16:07:28 -0600849 }
Jens Axboe2665abf2019-11-05 12:40:47 -0700850
851 if (wake_ev)
852 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -0600853}
854
855/*
856 * Called if REQ_F_LINK is set, and we fail the head request
857 */
858static void io_fail_links(struct io_kiocb *req)
859{
Jens Axboe2665abf2019-11-05 12:40:47 -0700860 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -0600861 struct io_kiocb *link;
Jens Axboe2665abf2019-11-05 12:40:47 -0700862 unsigned long flags;
863
864 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -0600865
866 while (!list_empty(&req->link_list)) {
867 link = list_first_entry(&req->link_list, struct io_kiocb, list);
Jens Axboe2665abf2019-11-05 12:40:47 -0700868 list_del_init(&link->list);
Jens Axboe9e645e112019-05-10 16:07:28 -0600869
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +0200870 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -0700871
872 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
873 link->submit.sqe->opcode == IORING_OP_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -0700874 io_link_cancel_timeout(link);
Jens Axboe2665abf2019-11-05 12:40:47 -0700875 } else {
Jens Axboe78e19bb2019-11-06 15:21:34 -0700876 io_cqring_fill_event(link, -ECANCELED);
877 io_double_put_req(link);
Jens Axboe2665abf2019-11-05 12:40:47 -0700878 }
Jens Axboe9e645e112019-05-10 16:07:28 -0600879 }
Jens Axboe2665abf2019-11-05 12:40:47 -0700880
881 io_commit_cqring(ctx);
882 spin_unlock_irqrestore(&ctx->completion_lock, flags);
883 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -0600884}
885
Jackie Liuc69f8db2019-11-09 11:00:08 +0800886static void io_free_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -0600887{
Jens Axboe2665abf2019-11-05 12:40:47 -0700888 if (likely(!(req->flags & REQ_F_LINK))) {
889 __io_free_req(req);
890 return;
891 }
892
Jens Axboe9e645e112019-05-10 16:07:28 -0600893 /*
894 * If LINK is set, we have dependent requests in this chain. If we
895 * didn't fail this request, queue the first one up, moving any other
896 * dependencies to the next request. In case of failure, fail the rest
897 * of the chain.
898 */
Jens Axboe2665abf2019-11-05 12:40:47 -0700899 if (req->flags & REQ_F_FAIL_LINK) {
900 io_fail_links(req);
901 } else if (req->flags & REQ_F_LINK_TIMEOUT) {
902 struct io_ring_ctx *ctx = req->ctx;
903 unsigned long flags;
904
905 /*
906 * If this is a timeout link, we could be racing with the
907 * timeout timer. Grab the completion lock for this case to
908 * protection against that.
909 */
910 spin_lock_irqsave(&ctx->completion_lock, flags);
911 io_req_link_next(req, nxt);
912 spin_unlock_irqrestore(&ctx->completion_lock, flags);
913 } else {
914 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -0600915 }
916
917 __io_free_req(req);
918}
919
Jackie Liuc69f8db2019-11-09 11:00:08 +0800920static void io_free_req(struct io_kiocb *req)
921{
922 io_free_req_find_next(req, NULL);
923}
924
Jens Axboeba816ad2019-09-28 11:36:45 -0600925/*
926 * Drop reference to request, return next in chain (if there is one) if this
927 * was the last reference to this request.
928 */
Jackie Liuec9c02a2019-11-08 23:50:36 +0800929static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboee65ef562019-03-12 10:16:44 -0600930{
Jens Axboeba816ad2019-09-28 11:36:45 -0600931 struct io_kiocb *nxt = NULL;
932
Jens Axboee65ef562019-03-12 10:16:44 -0600933 if (refcount_dec_and_test(&req->refs))
Jackie Liuc69f8db2019-11-09 11:00:08 +0800934 io_free_req_find_next(req, &nxt);
Jens Axboeba816ad2019-09-28 11:36:45 -0600935
Jens Axboeba816ad2019-09-28 11:36:45 -0600936 if (nxt) {
Jens Axboe561fb042019-10-24 07:25:42 -0600937 if (nxtptr)
Jens Axboeba816ad2019-09-28 11:36:45 -0600938 *nxtptr = nxt;
Jens Axboe561fb042019-10-24 07:25:42 -0600939 else
Jackie Liua197f662019-11-08 08:09:12 -0700940 io_queue_async_work(nxt);
Jens Axboeba816ad2019-09-28 11:36:45 -0600941 }
Jens Axboe2b188cc2019-01-07 10:46:33 -0700942}
943
Jackie Liuec9c02a2019-11-08 23:50:36 +0800944static void io_put_req(struct io_kiocb *req)
945{
946 if (refcount_dec_and_test(&req->refs))
Jackie Liuc69f8db2019-11-09 11:00:08 +0800947 io_free_req(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800948}
949
Jens Axboe78e19bb2019-11-06 15:21:34 -0700950static void io_double_put_req(struct io_kiocb *req)
951{
952 /* drop both submit and complete references */
953 if (refcount_sub_and_test(2, &req->refs))
954 __io_free_req(req);
955}
956
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700957static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -0600958{
Jens Axboe84f97dc2019-11-06 11:27:53 -0700959 struct io_rings *rings = ctx->rings;
960
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700961 /*
962 * noflush == true is from the waitqueue handler, just ensure we wake
963 * up the task, and the next invocation will flush the entries. We
964 * cannot safely to it from here.
965 */
966 if (noflush && !list_empty(&ctx->cq_overflow_list))
967 return -1U;
968
969 io_cqring_overflow_flush(ctx, false);
970
Jens Axboea3a0e432019-08-20 11:03:11 -0600971 /* See comment at the top of this file */
972 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +0000973 return READ_ONCE(rings->cq.tail) - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -0600974}
975
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +0300976static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
977{
978 struct io_rings *rings = ctx->rings;
979
980 /* make sure SQ entry isn't read before tail */
981 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
982}
983
Jens Axboedef596e2019-01-09 08:59:42 -0700984/*
985 * Find and free completed poll iocbs
986 */
987static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
988 struct list_head *done)
989{
990 void *reqs[IO_IOPOLL_BATCH];
991 struct io_kiocb *req;
Jens Axboe09bb8392019-03-13 12:39:28 -0600992 int to_free;
Jens Axboedef596e2019-01-09 08:59:42 -0700993
Jens Axboe09bb8392019-03-13 12:39:28 -0600994 to_free = 0;
Jens Axboedef596e2019-01-09 08:59:42 -0700995 while (!list_empty(done)) {
996 req = list_first_entry(done, struct io_kiocb, list);
997 list_del(&req->list);
998
Jens Axboe78e19bb2019-11-06 15:21:34 -0700999 io_cqring_fill_event(req, req->result);
Jens Axboedef596e2019-01-09 08:59:42 -07001000 (*nr_events)++;
1001
Jens Axboe09bb8392019-03-13 12:39:28 -06001002 if (refcount_dec_and_test(&req->refs)) {
1003 /* If we're not using fixed files, we have to pair the
1004 * completion part with the file put. Use regular
1005 * completions for those, only batch free for fixed
Jens Axboe9e645e112019-05-10 16:07:28 -06001006 * file and non-linked commands.
Jens Axboe09bb8392019-03-13 12:39:28 -06001007 */
Jens Axboe9e645e112019-05-10 16:07:28 -06001008 if ((req->flags & (REQ_F_FIXED_FILE|REQ_F_LINK)) ==
1009 REQ_F_FIXED_FILE) {
Jens Axboe09bb8392019-03-13 12:39:28 -06001010 reqs[to_free++] = req;
1011 if (to_free == ARRAY_SIZE(reqs))
1012 io_free_req_many(ctx, reqs, &to_free);
Jens Axboe6b063142019-01-10 22:13:58 -07001013 } else {
Jackie Liuc69f8db2019-11-09 11:00:08 +08001014 io_free_req(req);
Jens Axboe6b063142019-01-10 22:13:58 -07001015 }
Jens Axboe9a56a232019-01-09 09:06:50 -07001016 }
Jens Axboedef596e2019-01-09 08:59:42 -07001017 }
Jens Axboedef596e2019-01-09 08:59:42 -07001018
Jens Axboe09bb8392019-03-13 12:39:28 -06001019 io_commit_cqring(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07001020 io_free_req_many(ctx, reqs, &to_free);
1021}
1022
1023static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1024 long min)
1025{
1026 struct io_kiocb *req, *tmp;
1027 LIST_HEAD(done);
1028 bool spin;
1029 int ret;
1030
1031 /*
1032 * Only spin for completions if we don't have multiple devices hanging
1033 * off our complete list, and we're under the requested amount.
1034 */
1035 spin = !ctx->poll_multi_file && *nr_events < min;
1036
1037 ret = 0;
1038 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
1039 struct kiocb *kiocb = &req->rw;
1040
1041 /*
1042 * Move completed entries to our local list. If we find a
1043 * request that requires polling, break out and complete
1044 * the done list first, if we have entries there.
1045 */
1046 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
1047 list_move_tail(&req->list, &done);
1048 continue;
1049 }
1050 if (!list_empty(&done))
1051 break;
1052
1053 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1054 if (ret < 0)
1055 break;
1056
1057 if (ret && spin)
1058 spin = false;
1059 ret = 0;
1060 }
1061
1062 if (!list_empty(&done))
1063 io_iopoll_complete(ctx, nr_events, &done);
1064
1065 return ret;
1066}
1067
1068/*
1069 * Poll for a mininum of 'min' events. Note that if min == 0 we consider that a
1070 * non-spinning poll check - we'll still enter the driver poll loop, but only
1071 * as a non-spinning completion check.
1072 */
1073static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
1074 long min)
1075{
Jens Axboe08f54392019-08-21 22:19:11 -06001076 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07001077 int ret;
1078
1079 ret = io_do_iopoll(ctx, nr_events, min);
1080 if (ret < 0)
1081 return ret;
1082 if (!min || *nr_events >= min)
1083 return 0;
1084 }
1085
1086 return 1;
1087}
1088
1089/*
1090 * We can't just wait for polled events to come to us, we have to actively
1091 * find and complete them.
1092 */
1093static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
1094{
1095 if (!(ctx->flags & IORING_SETUP_IOPOLL))
1096 return;
1097
1098 mutex_lock(&ctx->uring_lock);
1099 while (!list_empty(&ctx->poll_list)) {
1100 unsigned int nr_events = 0;
1101
1102 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -06001103
1104 /*
1105 * Ensure we allow local-to-the-cpu processing to take place,
1106 * in this case we need to ensure that we reap all events.
1107 */
1108 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -07001109 }
1110 mutex_unlock(&ctx->uring_lock);
1111}
1112
Jens Axboe2b2ed972019-10-25 10:06:15 -06001113static int __io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1114 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07001115{
Jens Axboe2b2ed972019-10-25 10:06:15 -06001116 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001117
1118 do {
1119 int tmin = 0;
1120
Jens Axboe500f9fb2019-08-19 12:15:59 -06001121 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06001122 * Don't enter poll loop if we already have events pending.
1123 * If we do, we can potentially be spinning for commands that
1124 * already triggered a CQE (eg in error).
1125 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001126 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06001127 break;
1128
1129 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06001130 * If a submit got punted to a workqueue, we can have the
1131 * application entering polling for a command before it gets
1132 * issued. That app will hold the uring_lock for the duration
1133 * of the poll right here, so we need to take a breather every
1134 * now and then to ensure that the issue has a chance to add
1135 * the poll to the issued list. Otherwise we can spin here
1136 * forever, while the workqueue is stuck trying to acquire the
1137 * very same mutex.
1138 */
1139 if (!(++iters & 7)) {
1140 mutex_unlock(&ctx->uring_lock);
1141 mutex_lock(&ctx->uring_lock);
1142 }
1143
Jens Axboedef596e2019-01-09 08:59:42 -07001144 if (*nr_events < min)
1145 tmin = min - *nr_events;
1146
1147 ret = io_iopoll_getevents(ctx, nr_events, tmin);
1148 if (ret <= 0)
1149 break;
1150 ret = 0;
1151 } while (min && !*nr_events && !need_resched());
1152
Jens Axboe2b2ed972019-10-25 10:06:15 -06001153 return ret;
1154}
1155
1156static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1157 long min)
1158{
1159 int ret;
1160
1161 /*
1162 * We disallow the app entering submit/complete with polling, but we
1163 * still need to lock the ring to prevent racing with polled issue
1164 * that got punted to a workqueue.
1165 */
1166 mutex_lock(&ctx->uring_lock);
1167 ret = __io_iopoll_check(ctx, nr_events, min);
Jens Axboe500f9fb2019-08-19 12:15:59 -06001168 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001169 return ret;
1170}
1171
Jens Axboe491381ce2019-10-17 09:20:46 -06001172static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001173{
Jens Axboe491381ce2019-10-17 09:20:46 -06001174 /*
1175 * Tell lockdep we inherited freeze protection from submission
1176 * thread.
1177 */
1178 if (req->flags & REQ_F_ISREG) {
1179 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001180
Jens Axboe491381ce2019-10-17 09:20:46 -06001181 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001182 }
Jens Axboe491381ce2019-10-17 09:20:46 -06001183 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001184}
1185
Jens Axboeba816ad2019-09-28 11:36:45 -06001186static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001187{
1188 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1189
Jens Axboe491381ce2019-10-17 09:20:46 -06001190 if (kiocb->ki_flags & IOCB_WRITE)
1191 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001192
Jens Axboe9e645e112019-05-10 16:07:28 -06001193 if ((req->flags & REQ_F_LINK) && res != req->result)
1194 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001195 io_cqring_add_event(req, res);
Jens Axboeba816ad2019-09-28 11:36:45 -06001196}
1197
1198static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1199{
1200 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1201
1202 io_complete_rw_common(kiocb, res);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001203 io_put_req(req);
Jens Axboeba816ad2019-09-28 11:36:45 -06001204}
1205
1206static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res)
1207{
1208 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001209 struct io_kiocb *nxt = NULL;
Jens Axboeba816ad2019-09-28 11:36:45 -06001210
1211 io_complete_rw_common(kiocb, res);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001212 io_put_req_find_next(req, &nxt);
1213
1214 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001215}
1216
Jens Axboedef596e2019-01-09 08:59:42 -07001217static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1218{
1219 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1220
Jens Axboe491381ce2019-10-17 09:20:46 -06001221 if (kiocb->ki_flags & IOCB_WRITE)
1222 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001223
Jens Axboe9e645e112019-05-10 16:07:28 -06001224 if ((req->flags & REQ_F_LINK) && res != req->result)
1225 req->flags |= REQ_F_FAIL_LINK;
1226 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -07001227 if (res != -EAGAIN)
1228 req->flags |= REQ_F_IOPOLL_COMPLETED;
1229}
1230
1231/*
1232 * After the iocb has been issued, it's safe to be found on the poll list.
1233 * Adding the kiocb to the list AFTER submission ensures that we don't
1234 * find it from a io_iopoll_getevents() thread before the issuer is done
1235 * accessing the kiocb cookie.
1236 */
1237static void io_iopoll_req_issued(struct io_kiocb *req)
1238{
1239 struct io_ring_ctx *ctx = req->ctx;
1240
1241 /*
1242 * Track whether we have multiple files in our lists. This will impact
1243 * how we do polling eventually, not spinning if we're on potentially
1244 * different devices.
1245 */
1246 if (list_empty(&ctx->poll_list)) {
1247 ctx->poll_multi_file = false;
1248 } else if (!ctx->poll_multi_file) {
1249 struct io_kiocb *list_req;
1250
1251 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1252 list);
1253 if (list_req->rw.ki_filp != req->rw.ki_filp)
1254 ctx->poll_multi_file = true;
1255 }
1256
1257 /*
1258 * For fast devices, IO may have already completed. If it has, add
1259 * it to the front so we find it first.
1260 */
1261 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1262 list_add(&req->list, &ctx->poll_list);
1263 else
1264 list_add_tail(&req->list, &ctx->poll_list);
1265}
1266
Jens Axboe3d6770f2019-04-13 11:50:54 -06001267static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07001268{
Jens Axboe3d6770f2019-04-13 11:50:54 -06001269 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -07001270 int diff = state->has_refs - state->used_refs;
1271
1272 if (diff)
1273 fput_many(state->file, diff);
1274 state->file = NULL;
1275 }
1276}
1277
1278/*
1279 * Get as many references to a file as we have IOs left in this submission,
1280 * assuming most submissions are for one file, or at least that each file
1281 * has more than one submission.
1282 */
1283static struct file *io_file_get(struct io_submit_state *state, int fd)
1284{
1285 if (!state)
1286 return fget(fd);
1287
1288 if (state->file) {
1289 if (state->fd == fd) {
1290 state->used_refs++;
1291 state->ios_left--;
1292 return state->file;
1293 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06001294 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07001295 }
1296 state->file = fget_many(fd, state->ios_left);
1297 if (!state->file)
1298 return NULL;
1299
1300 state->fd = fd;
1301 state->has_refs = state->ios_left;
1302 state->used_refs = 1;
1303 state->ios_left--;
1304 return state->file;
1305}
1306
Jens Axboe2b188cc2019-01-07 10:46:33 -07001307/*
1308 * If we tracked the file through the SCM inflight mechanism, we could support
1309 * any file. For now, just ensure that anything potentially problematic is done
1310 * inline.
1311 */
1312static bool io_file_supports_async(struct file *file)
1313{
1314 umode_t mode = file_inode(file)->i_mode;
1315
1316 if (S_ISBLK(mode) || S_ISCHR(mode))
1317 return true;
1318 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
1319 return true;
1320
1321 return false;
1322}
1323
Pavel Begunkov267bc902019-11-07 01:41:08 +03001324static int io_prep_rw(struct io_kiocb *req, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001325{
Pavel Begunkov267bc902019-11-07 01:41:08 +03001326 const struct io_uring_sqe *sqe = req->submit.sqe;
Jens Axboedef596e2019-01-09 08:59:42 -07001327 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001328 struct kiocb *kiocb = &req->rw;
Jens Axboe09bb8392019-03-13 12:39:28 -06001329 unsigned ioprio;
1330 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001331
Jens Axboe09bb8392019-03-13 12:39:28 -06001332 if (!req->file)
1333 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001334
Jens Axboe491381ce2019-10-17 09:20:46 -06001335 if (S_ISREG(file_inode(req->file)->i_mode))
1336 req->flags |= REQ_F_ISREG;
1337
1338 /*
1339 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
1340 * we know to async punt it even if it was opened O_NONBLOCK
1341 */
1342 if (force_nonblock && !io_file_supports_async(req->file)) {
1343 req->flags |= REQ_F_MUST_PUNT;
1344 return -EAGAIN;
1345 }
Jens Axboe6b063142019-01-10 22:13:58 -07001346
Jens Axboe2b188cc2019-01-07 10:46:33 -07001347 kiocb->ki_pos = READ_ONCE(sqe->off);
1348 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
1349 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
1350
1351 ioprio = READ_ONCE(sqe->ioprio);
1352 if (ioprio) {
1353 ret = ioprio_check_cap(ioprio);
1354 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06001355 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001356
1357 kiocb->ki_ioprio = ioprio;
1358 } else
1359 kiocb->ki_ioprio = get_current_ioprio();
1360
1361 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
1362 if (unlikely(ret))
Jens Axboe09bb8392019-03-13 12:39:28 -06001363 return ret;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001364
1365 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06001366 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
1367 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02001368 req->flags |= REQ_F_NOWAIT;
1369
1370 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001371 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001372
Jens Axboedef596e2019-01-09 08:59:42 -07001373 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07001374 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
1375 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06001376 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001377
Jens Axboedef596e2019-01-09 08:59:42 -07001378 kiocb->ki_flags |= IOCB_HIPRI;
1379 kiocb->ki_complete = io_complete_rw_iopoll;
1380 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001381 if (kiocb->ki_flags & IOCB_HIPRI)
1382 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07001383 kiocb->ki_complete = io_complete_rw;
1384 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001385 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001386}
1387
1388static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
1389{
1390 switch (ret) {
1391 case -EIOCBQUEUED:
1392 break;
1393 case -ERESTARTSYS:
1394 case -ERESTARTNOINTR:
1395 case -ERESTARTNOHAND:
1396 case -ERESTART_RESTARTBLOCK:
1397 /*
1398 * We can't just restart the syscall, since previously
1399 * submitted sqes may already be in progress. Just fail this
1400 * IO with EINTR.
1401 */
1402 ret = -EINTR;
1403 /* fall through */
1404 default:
1405 kiocb->ki_complete(kiocb, ret, 0);
1406 }
1407}
1408
Jens Axboeba816ad2019-09-28 11:36:45 -06001409static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt,
1410 bool in_async)
1411{
1412 if (in_async && ret >= 0 && nxt && kiocb->ki_complete == io_complete_rw)
1413 *nxt = __io_complete_rw(kiocb, ret);
1414 else
1415 io_rw_done(kiocb, ret);
1416}
1417
Jens Axboeedafcce2019-01-09 09:16:05 -07001418static int io_import_fixed(struct io_ring_ctx *ctx, int rw,
1419 const struct io_uring_sqe *sqe,
1420 struct iov_iter *iter)
1421{
1422 size_t len = READ_ONCE(sqe->len);
1423 struct io_mapped_ubuf *imu;
1424 unsigned index, buf_index;
1425 size_t offset;
1426 u64 buf_addr;
1427
1428 /* attempt to use fixed buffers without having provided iovecs */
1429 if (unlikely(!ctx->user_bufs))
1430 return -EFAULT;
1431
1432 buf_index = READ_ONCE(sqe->buf_index);
1433 if (unlikely(buf_index >= ctx->nr_user_bufs))
1434 return -EFAULT;
1435
1436 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
1437 imu = &ctx->user_bufs[index];
1438 buf_addr = READ_ONCE(sqe->addr);
1439
1440 /* overflow */
1441 if (buf_addr + len < buf_addr)
1442 return -EFAULT;
1443 /* not inside the mapped region */
1444 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
1445 return -EFAULT;
1446
1447 /*
1448 * May not be a start of buffer, set size appropriately
1449 * and advance us to the beginning.
1450 */
1451 offset = buf_addr - imu->ubuf;
1452 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06001453
1454 if (offset) {
1455 /*
1456 * Don't use iov_iter_advance() here, as it's really slow for
1457 * using the latter parts of a big fixed buffer - it iterates
1458 * over each segment manually. We can cheat a bit here, because
1459 * we know that:
1460 *
1461 * 1) it's a BVEC iter, we set it up
1462 * 2) all bvecs are PAGE_SIZE in size, except potentially the
1463 * first and last bvec
1464 *
1465 * So just find our index, and adjust the iterator afterwards.
1466 * If the offset is within the first bvec (or the whole first
1467 * bvec, just use iov_iter_advance(). This makes it easier
1468 * since we can just skip the first segment, which may not
1469 * be PAGE_SIZE aligned.
1470 */
1471 const struct bio_vec *bvec = imu->bvec;
1472
1473 if (offset <= bvec->bv_len) {
1474 iov_iter_advance(iter, offset);
1475 } else {
1476 unsigned long seg_skip;
1477
1478 /* skip first vec */
1479 offset -= bvec->bv_len;
1480 seg_skip = 1 + (offset >> PAGE_SHIFT);
1481
1482 iter->bvec = bvec + seg_skip;
1483 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02001484 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001485 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001486 }
1487 }
1488
Jens Axboeedafcce2019-01-09 09:16:05 -07001489 return 0;
1490}
1491
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001492static ssize_t io_import_iovec(struct io_ring_ctx *ctx, int rw,
1493 const struct sqe_submit *s, struct iovec **iovec,
1494 struct iov_iter *iter)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001495{
1496 const struct io_uring_sqe *sqe = s->sqe;
1497 void __user *buf = u64_to_user_ptr(READ_ONCE(sqe->addr));
1498 size_t sqe_len = READ_ONCE(sqe->len);
Jens Axboeedafcce2019-01-09 09:16:05 -07001499 u8 opcode;
1500
1501 /*
1502 * We're reading ->opcode for the second time, but the first read
1503 * doesn't care whether it's _FIXED or not, so it doesn't matter
1504 * whether ->opcode changes concurrently. The first read does care
1505 * about whether it is a READ or a WRITE, so we don't trust this read
1506 * for that purpose and instead let the caller pass in the read/write
1507 * flag.
1508 */
1509 opcode = READ_ONCE(sqe->opcode);
1510 if (opcode == IORING_OP_READ_FIXED ||
1511 opcode == IORING_OP_WRITE_FIXED) {
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001512 ssize_t ret = io_import_fixed(ctx, rw, sqe, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07001513 *iovec = NULL;
1514 return ret;
1515 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001516
1517 if (!s->has_user)
1518 return -EFAULT;
1519
1520#ifdef CONFIG_COMPAT
1521 if (ctx->compat)
1522 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
1523 iovec, iter);
1524#endif
1525
1526 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
1527}
1528
Jens Axboe32960612019-09-23 11:05:34 -06001529/*
1530 * For files that don't have ->read_iter() and ->write_iter(), handle them
1531 * by looping over ->read() or ->write() manually.
1532 */
1533static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
1534 struct iov_iter *iter)
1535{
1536 ssize_t ret = 0;
1537
1538 /*
1539 * Don't support polled IO through this interface, and we can't
1540 * support non-blocking either. For the latter, this just causes
1541 * the kiocb to be handled from an async context.
1542 */
1543 if (kiocb->ki_flags & IOCB_HIPRI)
1544 return -EOPNOTSUPP;
1545 if (kiocb->ki_flags & IOCB_NOWAIT)
1546 return -EAGAIN;
1547
1548 while (iov_iter_count(iter)) {
1549 struct iovec iovec = iov_iter_iovec(iter);
1550 ssize_t nr;
1551
1552 if (rw == READ) {
1553 nr = file->f_op->read(file, iovec.iov_base,
1554 iovec.iov_len, &kiocb->ki_pos);
1555 } else {
1556 nr = file->f_op->write(file, iovec.iov_base,
1557 iovec.iov_len, &kiocb->ki_pos);
1558 }
1559
1560 if (nr < 0) {
1561 if (!ret)
1562 ret = nr;
1563 break;
1564 }
1565 ret += nr;
1566 if (nr != iovec.iov_len)
1567 break;
1568 iov_iter_advance(iter, nr);
1569 }
1570
1571 return ret;
1572}
1573
Pavel Begunkov267bc902019-11-07 01:41:08 +03001574static int io_read(struct io_kiocb *req, struct io_kiocb **nxt,
1575 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001576{
1577 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1578 struct kiocb *kiocb = &req->rw;
1579 struct iov_iter iter;
1580 struct file *file;
Jens Axboe31b51512019-01-18 22:56:34 -07001581 size_t iov_count;
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001582 ssize_t read_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001583
Pavel Begunkov267bc902019-11-07 01:41:08 +03001584 ret = io_prep_rw(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001585 if (ret)
1586 return ret;
1587 file = kiocb->ki_filp;
1588
Jens Axboe2b188cc2019-01-07 10:46:33 -07001589 if (unlikely(!(file->f_mode & FMODE_READ)))
Jens Axboe09bb8392019-03-13 12:39:28 -06001590 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001591
Pavel Begunkov267bc902019-11-07 01:41:08 +03001592 ret = io_import_iovec(req->ctx, READ, &req->submit, &iovec, &iter);
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001593 if (ret < 0)
Jens Axboe09bb8392019-03-13 12:39:28 -06001594 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001595
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001596 read_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06001597 if (req->flags & REQ_F_LINK)
1598 req->result = read_size;
1599
Jens Axboe31b51512019-01-18 22:56:34 -07001600 iov_count = iov_iter_count(&iter);
1601 ret = rw_verify_area(READ, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001602 if (!ret) {
1603 ssize_t ret2;
1604
Jens Axboe32960612019-09-23 11:05:34 -06001605 if (file->f_op->read_iter)
1606 ret2 = call_read_iter(file, kiocb, &iter);
1607 else
1608 ret2 = loop_rw_iter(READ, file, kiocb, &iter);
1609
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001610 /*
1611 * In case of a short read, punt to async. This can happen
1612 * if we have data partially cached. Alternatively we can
1613 * return the short read, in which case the application will
1614 * need to issue another SQE and wait for it. That SQE will
1615 * need async punt anyway, so it's more efficient to do it
1616 * here.
1617 */
Jens Axboe491381ce2019-10-17 09:20:46 -06001618 if (force_nonblock && !(req->flags & REQ_F_NOWAIT) &&
1619 (req->flags & REQ_F_ISREG) &&
1620 ret2 > 0 && ret2 < read_size)
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001621 ret2 = -EAGAIN;
1622 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboe561fb042019-10-24 07:25:42 -06001623 if (!force_nonblock || ret2 != -EAGAIN)
Pavel Begunkov267bc902019-11-07 01:41:08 +03001624 kiocb_done(kiocb, ret2, nxt, req->submit.in_async);
Jens Axboe561fb042019-10-24 07:25:42 -06001625 else
Jens Axboe2b188cc2019-01-07 10:46:33 -07001626 ret = -EAGAIN;
1627 }
1628 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001629 return ret;
1630}
1631
Pavel Begunkov267bc902019-11-07 01:41:08 +03001632static int io_write(struct io_kiocb *req, struct io_kiocb **nxt,
1633 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001634{
1635 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1636 struct kiocb *kiocb = &req->rw;
1637 struct iov_iter iter;
1638 struct file *file;
Jens Axboe31b51512019-01-18 22:56:34 -07001639 size_t iov_count;
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001640 ssize_t ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001641
Pavel Begunkov267bc902019-11-07 01:41:08 +03001642 ret = io_prep_rw(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001643 if (ret)
1644 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001645
Jens Axboe2b188cc2019-01-07 10:46:33 -07001646 file = kiocb->ki_filp;
1647 if (unlikely(!(file->f_mode & FMODE_WRITE)))
Jens Axboe09bb8392019-03-13 12:39:28 -06001648 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001649
Pavel Begunkov267bc902019-11-07 01:41:08 +03001650 ret = io_import_iovec(req->ctx, WRITE, &req->submit, &iovec, &iter);
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001651 if (ret < 0)
Jens Axboe09bb8392019-03-13 12:39:28 -06001652 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001653
Jens Axboe9e645e112019-05-10 16:07:28 -06001654 if (req->flags & REQ_F_LINK)
1655 req->result = ret;
1656
Jens Axboe31b51512019-01-18 22:56:34 -07001657 iov_count = iov_iter_count(&iter);
1658
1659 ret = -EAGAIN;
Jens Axboe561fb042019-10-24 07:25:42 -06001660 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT))
Jens Axboe31b51512019-01-18 22:56:34 -07001661 goto out_free;
Jens Axboe31b51512019-01-18 22:56:34 -07001662
1663 ret = rw_verify_area(WRITE, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001664 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01001665 ssize_t ret2;
1666
Jens Axboe2b188cc2019-01-07 10:46:33 -07001667 /*
1668 * Open-code file_start_write here to grab freeze protection,
1669 * which will be released by another thread in
1670 * io_complete_rw(). Fool lockdep by telling it the lock got
1671 * released so that it doesn't complain about the held lock when
1672 * we return to userspace.
1673 */
Jens Axboe491381ce2019-10-17 09:20:46 -06001674 if (req->flags & REQ_F_ISREG) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001675 __sb_start_write(file_inode(file)->i_sb,
1676 SB_FREEZE_WRITE, true);
1677 __sb_writers_release(file_inode(file)->i_sb,
1678 SB_FREEZE_WRITE);
1679 }
1680 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01001681
Jens Axboe32960612019-09-23 11:05:34 -06001682 if (file->f_op->write_iter)
1683 ret2 = call_write_iter(file, kiocb, &iter);
1684 else
1685 ret2 = loop_rw_iter(WRITE, file, kiocb, &iter);
Jens Axboe561fb042019-10-24 07:25:42 -06001686 if (!force_nonblock || ret2 != -EAGAIN)
Pavel Begunkov267bc902019-11-07 01:41:08 +03001687 kiocb_done(kiocb, ret2, nxt, req->submit.in_async);
Jens Axboe561fb042019-10-24 07:25:42 -06001688 else
Roman Penyaev9bf79332019-03-25 20:09:24 +01001689 ret = -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001690 }
Jens Axboe31b51512019-01-18 22:56:34 -07001691out_free:
Jens Axboe2b188cc2019-01-07 10:46:33 -07001692 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001693 return ret;
1694}
1695
1696/*
1697 * IORING_OP_NOP just posts a completion event, nothing else.
1698 */
Jens Axboe78e19bb2019-11-06 15:21:34 -07001699static int io_nop(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001700{
1701 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001702
Jens Axboedef596e2019-01-09 08:59:42 -07001703 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1704 return -EINVAL;
1705
Jens Axboe78e19bb2019-11-06 15:21:34 -07001706 io_cqring_add_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001707 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001708 return 0;
1709}
1710
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001711static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1712{
Jens Axboe6b063142019-01-10 22:13:58 -07001713 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001714
Jens Axboe09bb8392019-03-13 12:39:28 -06001715 if (!req->file)
1716 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001717
Jens Axboe6b063142019-01-10 22:13:58 -07001718 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07001719 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07001720 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001721 return -EINVAL;
1722
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001723 return 0;
1724}
1725
1726static int io_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001727 struct io_kiocb **nxt, bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001728{
1729 loff_t sqe_off = READ_ONCE(sqe->off);
1730 loff_t sqe_len = READ_ONCE(sqe->len);
1731 loff_t end = sqe_off + sqe_len;
1732 unsigned fsync_flags;
1733 int ret;
1734
1735 fsync_flags = READ_ONCE(sqe->fsync_flags);
1736 if (unlikely(fsync_flags & ~IORING_FSYNC_DATASYNC))
1737 return -EINVAL;
1738
1739 ret = io_prep_fsync(req, sqe);
1740 if (ret)
1741 return ret;
1742
1743 /* fsync always requires a blocking context */
1744 if (force_nonblock)
1745 return -EAGAIN;
1746
1747 ret = vfs_fsync_range(req->rw.ki_filp, sqe_off,
1748 end > 0 ? end : LLONG_MAX,
1749 fsync_flags & IORING_FSYNC_DATASYNC);
1750
Jens Axboe9e645e112019-05-10 16:07:28 -06001751 if (ret < 0 && (req->flags & REQ_F_LINK))
1752 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001753 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001754 io_put_req_find_next(req, nxt);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001755 return 0;
1756}
1757
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001758static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1759{
1760 struct io_ring_ctx *ctx = req->ctx;
1761 int ret = 0;
1762
1763 if (!req->file)
1764 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001765
1766 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1767 return -EINVAL;
1768 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
1769 return -EINVAL;
1770
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001771 return ret;
1772}
1773
1774static int io_sync_file_range(struct io_kiocb *req,
1775 const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001776 struct io_kiocb **nxt,
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001777 bool force_nonblock)
1778{
1779 loff_t sqe_off;
1780 loff_t sqe_len;
1781 unsigned flags;
1782 int ret;
1783
1784 ret = io_prep_sfr(req, sqe);
1785 if (ret)
1786 return ret;
1787
1788 /* sync_file_range always requires a blocking context */
1789 if (force_nonblock)
1790 return -EAGAIN;
1791
1792 sqe_off = READ_ONCE(sqe->off);
1793 sqe_len = READ_ONCE(sqe->len);
1794 flags = READ_ONCE(sqe->sync_range_flags);
1795
1796 ret = sync_file_range(req->rw.ki_filp, sqe_off, sqe_len, flags);
1797
Jens Axboe9e645e112019-05-10 16:07:28 -06001798 if (ret < 0 && (req->flags & REQ_F_LINK))
1799 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001800 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001801 io_put_req_find_next(req, nxt);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001802 return 0;
1803}
1804
Jens Axboe0fa03c62019-04-19 13:34:07 -06001805#if defined(CONFIG_NET)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001806static int io_send_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001807 struct io_kiocb **nxt, bool force_nonblock,
Jens Axboeaa1fa282019-04-19 13:38:09 -06001808 long (*fn)(struct socket *, struct user_msghdr __user *,
1809 unsigned int))
1810{
Jens Axboe0fa03c62019-04-19 13:34:07 -06001811 struct socket *sock;
1812 int ret;
1813
1814 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1815 return -EINVAL;
1816
1817 sock = sock_from_file(req->file, &ret);
1818 if (sock) {
1819 struct user_msghdr __user *msg;
1820 unsigned flags;
1821
1822 flags = READ_ONCE(sqe->msg_flags);
1823 if (flags & MSG_DONTWAIT)
1824 req->flags |= REQ_F_NOWAIT;
1825 else if (force_nonblock)
1826 flags |= MSG_DONTWAIT;
1827
1828 msg = (struct user_msghdr __user *) (unsigned long)
1829 READ_ONCE(sqe->addr);
1830
Jens Axboeaa1fa282019-04-19 13:38:09 -06001831 ret = fn(sock, msg, flags);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001832 if (force_nonblock && ret == -EAGAIN)
1833 return ret;
1834 }
1835
Jens Axboe78e19bb2019-11-06 15:21:34 -07001836 io_cqring_add_event(req, ret);
Jens Axboef1f40852019-11-05 20:33:16 -07001837 if (ret < 0 && (req->flags & REQ_F_LINK))
1838 req->flags |= REQ_F_FAIL_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001839 io_put_req_find_next(req, nxt);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001840 return 0;
Jens Axboeaa1fa282019-04-19 13:38:09 -06001841}
1842#endif
1843
1844static int io_sendmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001845 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001846{
1847#if defined(CONFIG_NET)
Jens Axboeba816ad2019-09-28 11:36:45 -06001848 return io_send_recvmsg(req, sqe, nxt, force_nonblock,
1849 __sys_sendmsg_sock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06001850#else
1851 return -EOPNOTSUPP;
1852#endif
1853}
1854
1855static int io_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001856 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001857{
1858#if defined(CONFIG_NET)
Jens Axboeba816ad2019-09-28 11:36:45 -06001859 return io_send_recvmsg(req, sqe, nxt, force_nonblock,
1860 __sys_recvmsg_sock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001861#else
1862 return -EOPNOTSUPP;
1863#endif
1864}
1865
Jens Axboe17f2fe32019-10-17 14:42:58 -06001866static int io_accept(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1867 struct io_kiocb **nxt, bool force_nonblock)
1868{
1869#if defined(CONFIG_NET)
1870 struct sockaddr __user *addr;
1871 int __user *addr_len;
1872 unsigned file_flags;
1873 int flags, ret;
1874
1875 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
1876 return -EINVAL;
1877 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
1878 return -EINVAL;
1879
1880 addr = (struct sockaddr __user *) (unsigned long) READ_ONCE(sqe->addr);
1881 addr_len = (int __user *) (unsigned long) READ_ONCE(sqe->addr2);
1882 flags = READ_ONCE(sqe->accept_flags);
1883 file_flags = force_nonblock ? O_NONBLOCK : 0;
1884
1885 ret = __sys_accept4_file(req->file, file_flags, addr, addr_len, flags);
1886 if (ret == -EAGAIN && force_nonblock) {
1887 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
1888 return -EAGAIN;
1889 }
1890 if (ret < 0 && (req->flags & REQ_F_LINK))
1891 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001892 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001893 io_put_req_find_next(req, nxt);
Jens Axboe17f2fe32019-10-17 14:42:58 -06001894 return 0;
1895#else
1896 return -EOPNOTSUPP;
1897#endif
1898}
1899
Jens Axboe221c5eb2019-01-17 09:41:58 -07001900static void io_poll_remove_one(struct io_kiocb *req)
1901{
1902 struct io_poll_iocb *poll = &req->poll;
1903
1904 spin_lock(&poll->head->lock);
1905 WRITE_ONCE(poll->canceled, true);
1906 if (!list_empty(&poll->wait.entry)) {
1907 list_del_init(&poll->wait.entry);
Jackie Liua197f662019-11-08 08:09:12 -07001908 io_queue_async_work(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001909 }
1910 spin_unlock(&poll->head->lock);
1911
1912 list_del_init(&req->list);
1913}
1914
1915static void io_poll_remove_all(struct io_ring_ctx *ctx)
1916{
1917 struct io_kiocb *req;
1918
1919 spin_lock_irq(&ctx->completion_lock);
1920 while (!list_empty(&ctx->cancel_list)) {
1921 req = list_first_entry(&ctx->cancel_list, struct io_kiocb,list);
1922 io_poll_remove_one(req);
1923 }
1924 spin_unlock_irq(&ctx->completion_lock);
1925}
1926
1927/*
1928 * Find a running poll command that matches one specified in sqe->addr,
1929 * and remove it if found.
1930 */
1931static int io_poll_remove(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1932{
1933 struct io_ring_ctx *ctx = req->ctx;
1934 struct io_kiocb *poll_req, *next;
1935 int ret = -ENOENT;
1936
1937 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1938 return -EINVAL;
1939 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
1940 sqe->poll_events)
1941 return -EINVAL;
1942
1943 spin_lock_irq(&ctx->completion_lock);
1944 list_for_each_entry_safe(poll_req, next, &ctx->cancel_list, list) {
1945 if (READ_ONCE(sqe->addr) == poll_req->user_data) {
1946 io_poll_remove_one(poll_req);
1947 ret = 0;
1948 break;
1949 }
1950 }
1951 spin_unlock_irq(&ctx->completion_lock);
1952
Jens Axboe78e19bb2019-11-06 15:21:34 -07001953 io_cqring_add_event(req, ret);
Jens Axboef1f40852019-11-05 20:33:16 -07001954 if (ret < 0 && (req->flags & REQ_F_LINK))
1955 req->flags |= REQ_F_FAIL_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001956 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001957 return 0;
1958}
1959
Jackie Liua197f662019-11-08 08:09:12 -07001960static void io_poll_complete(struct io_kiocb *req, __poll_t mask)
Jens Axboe221c5eb2019-01-17 09:41:58 -07001961{
Jackie Liua197f662019-11-08 08:09:12 -07001962 struct io_ring_ctx *ctx = req->ctx;
1963
Jens Axboe8c838782019-03-12 15:48:16 -06001964 req->poll.done = true;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001965 io_cqring_fill_event(req, mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06001966 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001967}
1968
Jens Axboe561fb042019-10-24 07:25:42 -06001969static void io_poll_complete_work(struct io_wq_work **workptr)
Jens Axboe221c5eb2019-01-17 09:41:58 -07001970{
Jens Axboe561fb042019-10-24 07:25:42 -06001971 struct io_wq_work *work = *workptr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001972 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
1973 struct io_poll_iocb *poll = &req->poll;
1974 struct poll_table_struct pt = { ._key = poll->events };
1975 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe89723d02019-11-05 15:32:58 -07001976 struct io_kiocb *nxt = NULL;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001977 __poll_t mask = 0;
1978
Jens Axboe561fb042019-10-24 07:25:42 -06001979 if (work->flags & IO_WQ_WORK_CANCEL)
1980 WRITE_ONCE(poll->canceled, true);
1981
Jens Axboe221c5eb2019-01-17 09:41:58 -07001982 if (!READ_ONCE(poll->canceled))
1983 mask = vfs_poll(poll->file, &pt) & poll->events;
1984
1985 /*
1986 * Note that ->ki_cancel callers also delete iocb from active_reqs after
1987 * calling ->ki_cancel. We need the ctx_lock roundtrip here to
1988 * synchronize with them. In the cancellation case the list_del_init
1989 * itself is not actually needed, but harmless so we keep it in to
1990 * avoid further branches in the fast path.
1991 */
1992 spin_lock_irq(&ctx->completion_lock);
1993 if (!mask && !READ_ONCE(poll->canceled)) {
1994 add_wait_queue(poll->head, &poll->wait);
1995 spin_unlock_irq(&ctx->completion_lock);
1996 return;
1997 }
1998 list_del_init(&req->list);
Jackie Liua197f662019-11-08 08:09:12 -07001999 io_poll_complete(req, mask);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002000 spin_unlock_irq(&ctx->completion_lock);
2001
Jens Axboe8c838782019-03-12 15:48:16 -06002002 io_cqring_ev_posted(ctx);
Jens Axboe89723d02019-11-05 15:32:58 -07002003
Jackie Liuec9c02a2019-11-08 23:50:36 +08002004 io_put_req_find_next(req, &nxt);
Jens Axboe89723d02019-11-05 15:32:58 -07002005 if (nxt)
2006 *workptr = &nxt->work;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002007}
2008
2009static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
2010 void *key)
2011{
2012 struct io_poll_iocb *poll = container_of(wait, struct io_poll_iocb,
2013 wait);
2014 struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
2015 struct io_ring_ctx *ctx = req->ctx;
2016 __poll_t mask = key_to_poll(key);
Jens Axboe8c838782019-03-12 15:48:16 -06002017 unsigned long flags;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002018
2019 /* for instances that support it check for an event match first: */
Jens Axboe8c838782019-03-12 15:48:16 -06002020 if (mask && !(mask & poll->events))
2021 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002022
2023 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06002024
2025 if (mask && spin_trylock_irqsave(&ctx->completion_lock, flags)) {
2026 list_del(&req->list);
Jackie Liua197f662019-11-08 08:09:12 -07002027 io_poll_complete(req, mask);
Jens Axboe8c838782019-03-12 15:48:16 -06002028 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2029
2030 io_cqring_ev_posted(ctx);
Jackie Liuec9c02a2019-11-08 23:50:36 +08002031 io_put_req(req);
Jens Axboe8c838782019-03-12 15:48:16 -06002032 } else {
Jackie Liua197f662019-11-08 08:09:12 -07002033 io_queue_async_work(req);
Jens Axboe8c838782019-03-12 15:48:16 -06002034 }
2035
Jens Axboe221c5eb2019-01-17 09:41:58 -07002036 return 1;
2037}
2038
2039struct io_poll_table {
2040 struct poll_table_struct pt;
2041 struct io_kiocb *req;
2042 int error;
2043};
2044
2045static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
2046 struct poll_table_struct *p)
2047{
2048 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
2049
2050 if (unlikely(pt->req->poll.head)) {
2051 pt->error = -EINVAL;
2052 return;
2053 }
2054
2055 pt->error = 0;
2056 pt->req->poll.head = head;
2057 add_wait_queue(head, &pt->req->poll.wait);
2058}
2059
Jens Axboe89723d02019-11-05 15:32:58 -07002060static int io_poll_add(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2061 struct io_kiocb **nxt)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002062{
2063 struct io_poll_iocb *poll = &req->poll;
2064 struct io_ring_ctx *ctx = req->ctx;
2065 struct io_poll_table ipt;
Jens Axboe8c838782019-03-12 15:48:16 -06002066 bool cancel = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002067 __poll_t mask;
2068 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002069
2070 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2071 return -EINVAL;
2072 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
2073 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06002074 if (!poll->file)
2075 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002076
Jens Axboe6cc47d12019-09-18 11:18:23 -06002077 req->submit.sqe = NULL;
Jens Axboe561fb042019-10-24 07:25:42 -06002078 INIT_IO_WORK(&req->work, io_poll_complete_work);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002079 events = READ_ONCE(sqe->poll_events);
2080 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
2081
Jens Axboe221c5eb2019-01-17 09:41:58 -07002082 poll->head = NULL;
Jens Axboe8c838782019-03-12 15:48:16 -06002083 poll->done = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002084 poll->canceled = false;
2085
2086 ipt.pt._qproc = io_poll_queue_proc;
2087 ipt.pt._key = poll->events;
2088 ipt.req = req;
2089 ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
2090
2091 /* initialized the list so that we can do list_empty checks */
2092 INIT_LIST_HEAD(&poll->wait.entry);
2093 init_waitqueue_func_entry(&poll->wait, io_poll_wake);
2094
Jens Axboe36703242019-07-25 10:20:18 -06002095 INIT_LIST_HEAD(&req->list);
2096
Jens Axboe221c5eb2019-01-17 09:41:58 -07002097 mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002098
2099 spin_lock_irq(&ctx->completion_lock);
Jens Axboe8c838782019-03-12 15:48:16 -06002100 if (likely(poll->head)) {
2101 spin_lock(&poll->head->lock);
2102 if (unlikely(list_empty(&poll->wait.entry))) {
2103 if (ipt.error)
2104 cancel = true;
2105 ipt.error = 0;
2106 mask = 0;
2107 }
2108 if (mask || ipt.error)
2109 list_del_init(&poll->wait.entry);
2110 else if (cancel)
2111 WRITE_ONCE(poll->canceled, true);
2112 else if (!poll->done) /* actually waiting for an event */
2113 list_add_tail(&req->list, &ctx->cancel_list);
2114 spin_unlock(&poll->head->lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002115 }
Jens Axboe8c838782019-03-12 15:48:16 -06002116 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06002117 ipt.error = 0;
Jackie Liua197f662019-11-08 08:09:12 -07002118 io_poll_complete(req, mask);
Jens Axboe8c838782019-03-12 15:48:16 -06002119 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07002120 spin_unlock_irq(&ctx->completion_lock);
2121
Jens Axboe8c838782019-03-12 15:48:16 -06002122 if (mask) {
2123 io_cqring_ev_posted(ctx);
Jackie Liuec9c02a2019-11-08 23:50:36 +08002124 io_put_req_find_next(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002125 }
Jens Axboe8c838782019-03-12 15:48:16 -06002126 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002127}
2128
Jens Axboe5262f562019-09-17 12:26:57 -06002129static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
2130{
2131 struct io_ring_ctx *ctx;
Jens Axboe11365042019-10-16 09:08:32 -06002132 struct io_kiocb *req;
Jens Axboe5262f562019-09-17 12:26:57 -06002133 unsigned long flags;
2134
2135 req = container_of(timer, struct io_kiocb, timeout.timer);
2136 ctx = req->ctx;
2137 atomic_inc(&ctx->cq_timeouts);
2138
2139 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08002140 /*
Jens Axboe11365042019-10-16 09:08:32 -06002141 * We could be racing with timeout deletion. If the list is empty,
2142 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08002143 */
Jens Axboe842f9612019-10-29 12:34:10 -06002144 if (!list_empty(&req->list)) {
Jens Axboe11365042019-10-16 09:08:32 -06002145 struct io_kiocb *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06002146
Jens Axboe11365042019-10-16 09:08:32 -06002147 /*
2148 * Adjust the reqs sequence before the current one because it
2149 * will consume a slot in the cq_ring and the the cq_tail
2150 * pointer will be increased, otherwise other timeout reqs may
2151 * return in advance without waiting for enough wait_nr.
2152 */
2153 prev = req;
2154 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
2155 prev->sequence++;
Jens Axboe11365042019-10-16 09:08:32 -06002156 list_del_init(&req->list);
Jens Axboe11365042019-10-16 09:08:32 -06002157 }
Jens Axboe842f9612019-10-29 12:34:10 -06002158
Jens Axboe78e19bb2019-11-06 15:21:34 -07002159 io_cqring_fill_event(req, -ETIME);
Jens Axboe842f9612019-10-29 12:34:10 -06002160 io_commit_cqring(ctx);
Jens Axboe5262f562019-09-17 12:26:57 -06002161 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2162
Jens Axboe842f9612019-10-29 12:34:10 -06002163 io_cqring_ev_posted(ctx);
Jens Axboef1f40852019-11-05 20:33:16 -07002164 if (req->flags & REQ_F_LINK)
2165 req->flags |= REQ_F_FAIL_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08002166 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06002167 return HRTIMER_NORESTART;
2168}
2169
2170/*
2171 * Remove or update an existing timeout command
2172 */
2173static int io_timeout_remove(struct io_kiocb *req,
2174 const struct io_uring_sqe *sqe)
2175{
2176 struct io_ring_ctx *ctx = req->ctx;
2177 struct io_kiocb *treq;
2178 int ret = -ENOENT;
2179 __u64 user_data;
2180 unsigned flags;
2181
2182 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2183 return -EINVAL;
2184 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
2185 return -EINVAL;
2186 flags = READ_ONCE(sqe->timeout_flags);
2187 if (flags)
2188 return -EINVAL;
2189
2190 user_data = READ_ONCE(sqe->addr);
2191 spin_lock_irq(&ctx->completion_lock);
2192 list_for_each_entry(treq, &ctx->timeout_list, list) {
2193 if (user_data == treq->user_data) {
2194 list_del_init(&treq->list);
2195 ret = 0;
2196 break;
2197 }
2198 }
2199
2200 /* didn't find timeout */
2201 if (ret) {
2202fill_ev:
Jens Axboe78e19bb2019-11-06 15:21:34 -07002203 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06002204 io_commit_cqring(ctx);
2205 spin_unlock_irq(&ctx->completion_lock);
2206 io_cqring_ev_posted(ctx);
Jens Axboef1f40852019-11-05 20:33:16 -07002207 if (req->flags & REQ_F_LINK)
2208 req->flags |= REQ_F_FAIL_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08002209 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06002210 return 0;
2211 }
2212
2213 ret = hrtimer_try_to_cancel(&treq->timeout.timer);
2214 if (ret == -1) {
2215 ret = -EBUSY;
2216 goto fill_ev;
2217 }
2218
Jens Axboe78e19bb2019-11-06 15:21:34 -07002219 io_cqring_fill_event(req, 0);
2220 io_cqring_fill_event(treq, -ECANCELED);
Jens Axboe11365042019-10-16 09:08:32 -06002221 io_commit_cqring(ctx);
2222 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06002223 io_cqring_ev_posted(ctx);
2224
Jackie Liuec9c02a2019-11-08 23:50:36 +08002225 io_put_req(treq);
2226 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06002227 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06002228}
2229
2230static int io_timeout(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2231{
yangerkun5da0fb12019-10-15 21:59:29 +08002232 unsigned count;
Jens Axboe5262f562019-09-17 12:26:57 -06002233 struct io_ring_ctx *ctx = req->ctx;
2234 struct list_head *entry;
Jens Axboea41525a2019-10-15 16:48:15 -06002235 enum hrtimer_mode mode;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06002236 struct timespec64 ts;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002237 unsigned span = 0;
Jens Axboea41525a2019-10-15 16:48:15 -06002238 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06002239
2240 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2241 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06002242 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len != 1)
2243 return -EINVAL;
2244 flags = READ_ONCE(sqe->timeout_flags);
2245 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06002246 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06002247
2248 if (get_timespec64(&ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06002249 return -EFAULT;
2250
Jens Axboe11365042019-10-16 09:08:32 -06002251 if (flags & IORING_TIMEOUT_ABS)
2252 mode = HRTIMER_MODE_ABS;
2253 else
2254 mode = HRTIMER_MODE_REL;
2255
2256 hrtimer_init(&req->timeout.timer, CLOCK_MONOTONIC, mode);
2257
Jens Axboe5262f562019-09-17 12:26:57 -06002258 /*
2259 * sqe->off holds how many events that need to occur for this
2260 * timeout event to be satisfied.
2261 */
2262 count = READ_ONCE(sqe->off);
2263 if (!count)
2264 count = 1;
2265
2266 req->sequence = ctx->cached_sq_head + count - 1;
yangerkun5da0fb12019-10-15 21:59:29 +08002267 /* reuse it to store the count */
2268 req->submit.sequence = count;
Jens Axboe5262f562019-09-17 12:26:57 -06002269 req->flags |= REQ_F_TIMEOUT;
2270
2271 /*
2272 * Insertion sort, ensuring the first entry in the list is always
2273 * the one we need first.
2274 */
Jens Axboe5262f562019-09-17 12:26:57 -06002275 spin_lock_irq(&ctx->completion_lock);
2276 list_for_each_prev(entry, &ctx->timeout_list) {
2277 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
yangerkun5da0fb12019-10-15 21:59:29 +08002278 unsigned nxt_sq_head;
2279 long long tmp, tmp_nxt;
Jens Axboe5262f562019-09-17 12:26:57 -06002280
yangerkun5da0fb12019-10-15 21:59:29 +08002281 /*
2282 * Since cached_sq_head + count - 1 can overflow, use type long
2283 * long to store it.
2284 */
2285 tmp = (long long)ctx->cached_sq_head + count - 1;
2286 nxt_sq_head = nxt->sequence - nxt->submit.sequence + 1;
2287 tmp_nxt = (long long)nxt_sq_head + nxt->submit.sequence - 1;
2288
2289 /*
2290 * cached_sq_head may overflow, and it will never overflow twice
2291 * once there is some timeout req still be valid.
2292 */
2293 if (ctx->cached_sq_head < nxt_sq_head)
yangerkun8b07a652019-10-17 12:12:35 +08002294 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08002295
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002296 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06002297 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002298
2299 /*
2300 * Sequence of reqs after the insert one and itself should
2301 * be adjusted because each timeout req consumes a slot.
2302 */
2303 span++;
2304 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06002305 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002306 req->sequence -= span;
Jens Axboe5262f562019-09-17 12:26:57 -06002307 list_add(&req->list, entry);
Jens Axboe5262f562019-09-17 12:26:57 -06002308 req->timeout.timer.function = io_timeout_fn;
Jens Axboea41525a2019-10-15 16:48:15 -06002309 hrtimer_start(&req->timeout.timer, timespec64_to_ktime(ts), mode);
Jens Axboe842f9612019-10-29 12:34:10 -06002310 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06002311 return 0;
2312}
2313
Jens Axboe62755e32019-10-28 21:49:21 -06002314static bool io_cancel_cb(struct io_wq_work *work, void *data)
2315{
2316 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
2317
2318 return req->user_data == (unsigned long) data;
2319}
2320
Jens Axboee977d6d2019-11-05 12:39:45 -07002321static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06002322{
Jens Axboe62755e32019-10-28 21:49:21 -06002323 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06002324 int ret = 0;
2325
Jens Axboe62755e32019-10-28 21:49:21 -06002326 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
2327 switch (cancel_ret) {
2328 case IO_WQ_CANCEL_OK:
2329 ret = 0;
2330 break;
2331 case IO_WQ_CANCEL_RUNNING:
2332 ret = -EALREADY;
2333 break;
2334 case IO_WQ_CANCEL_NOTFOUND:
2335 ret = -ENOENT;
2336 break;
2337 }
2338
Jens Axboee977d6d2019-11-05 12:39:45 -07002339 return ret;
2340}
2341
2342static int io_async_cancel(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2343 struct io_kiocb **nxt)
2344{
2345 struct io_ring_ctx *ctx = req->ctx;
2346 void *sqe_addr;
2347 int ret;
2348
2349 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2350 return -EINVAL;
2351 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
2352 sqe->cancel_flags)
2353 return -EINVAL;
2354
2355 sqe_addr = (void *) (unsigned long) READ_ONCE(sqe->addr);
2356 ret = io_async_cancel_one(ctx, sqe_addr);
2357
Jens Axboe62755e32019-10-28 21:49:21 -06002358 if (ret < 0 && (req->flags & REQ_F_LINK))
2359 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07002360 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08002361 io_put_req_find_next(req, nxt);
Jens Axboe62755e32019-10-28 21:49:21 -06002362 return 0;
2363}
2364
Jackie Liua197f662019-11-08 08:09:12 -07002365static int io_req_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -06002366{
Pavel Begunkov267bc902019-11-07 01:41:08 +03002367 const struct io_uring_sqe *sqe = req->submit.sqe;
Jens Axboede0617e2019-04-06 21:51:27 -06002368 struct io_uring_sqe *sqe_copy;
Jackie Liua197f662019-11-08 08:09:12 -07002369 struct io_ring_ctx *ctx = req->ctx;
Jens Axboede0617e2019-04-06 21:51:27 -06002370
Jackie Liua197f662019-11-08 08:09:12 -07002371 if (!io_sequence_defer(req) && list_empty(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06002372 return 0;
2373
2374 sqe_copy = kmalloc(sizeof(*sqe_copy), GFP_KERNEL);
2375 if (!sqe_copy)
2376 return -EAGAIN;
2377
2378 spin_lock_irq(&ctx->completion_lock);
Jackie Liua197f662019-11-08 08:09:12 -07002379 if (!io_sequence_defer(req) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06002380 spin_unlock_irq(&ctx->completion_lock);
2381 kfree(sqe_copy);
2382 return 0;
2383 }
2384
2385 memcpy(sqe_copy, sqe, sizeof(*sqe_copy));
2386 req->submit.sqe = sqe_copy;
2387
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002388 trace_io_uring_defer(ctx, req, false);
Jens Axboede0617e2019-04-06 21:51:27 -06002389 list_add_tail(&req->list, &ctx->defer_list);
2390 spin_unlock_irq(&ctx->completion_lock);
2391 return -EIOCBQUEUED;
2392}
2393
Jackie Liua197f662019-11-08 08:09:12 -07002394static int __io_submit_sqe(struct io_kiocb *req, struct io_kiocb **nxt,
2395 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002396{
Jens Axboee0c5c572019-03-12 10:18:47 -06002397 int ret, opcode;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002398 struct sqe_submit *s = &req->submit;
Jackie Liua197f662019-11-08 08:09:12 -07002399 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002400
Jens Axboe2b188cc2019-01-07 10:46:33 -07002401 opcode = READ_ONCE(s->sqe->opcode);
2402 switch (opcode) {
2403 case IORING_OP_NOP:
Jens Axboe78e19bb2019-11-06 15:21:34 -07002404 ret = io_nop(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002405 break;
2406 case IORING_OP_READV:
Jens Axboeedafcce2019-01-09 09:16:05 -07002407 if (unlikely(s->sqe->buf_index))
2408 return -EINVAL;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002409 ret = io_read(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002410 break;
2411 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07002412 if (unlikely(s->sqe->buf_index))
2413 return -EINVAL;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002414 ret = io_write(req, nxt, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07002415 break;
2416 case IORING_OP_READ_FIXED:
Pavel Begunkov267bc902019-11-07 01:41:08 +03002417 ret = io_read(req, nxt, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07002418 break;
2419 case IORING_OP_WRITE_FIXED:
Pavel Begunkov267bc902019-11-07 01:41:08 +03002420 ret = io_write(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002421 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002422 case IORING_OP_FSYNC:
Jens Axboeba816ad2019-09-28 11:36:45 -06002423 ret = io_fsync(req, s->sqe, nxt, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002424 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002425 case IORING_OP_POLL_ADD:
Jens Axboe89723d02019-11-05 15:32:58 -07002426 ret = io_poll_add(req, s->sqe, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002427 break;
2428 case IORING_OP_POLL_REMOVE:
2429 ret = io_poll_remove(req, s->sqe);
2430 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002431 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboeba816ad2019-09-28 11:36:45 -06002432 ret = io_sync_file_range(req, s->sqe, nxt, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002433 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002434 case IORING_OP_SENDMSG:
Jens Axboeba816ad2019-09-28 11:36:45 -06002435 ret = io_sendmsg(req, s->sqe, nxt, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06002436 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06002437 case IORING_OP_RECVMSG:
Jens Axboeba816ad2019-09-28 11:36:45 -06002438 ret = io_recvmsg(req, s->sqe, nxt, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06002439 break;
Jens Axboe5262f562019-09-17 12:26:57 -06002440 case IORING_OP_TIMEOUT:
2441 ret = io_timeout(req, s->sqe);
2442 break;
Jens Axboe11365042019-10-16 09:08:32 -06002443 case IORING_OP_TIMEOUT_REMOVE:
2444 ret = io_timeout_remove(req, s->sqe);
2445 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06002446 case IORING_OP_ACCEPT:
2447 ret = io_accept(req, s->sqe, nxt, force_nonblock);
2448 break;
Jens Axboe62755e32019-10-28 21:49:21 -06002449 case IORING_OP_ASYNC_CANCEL:
2450 ret = io_async_cancel(req, s->sqe, nxt);
2451 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002452 default:
2453 ret = -EINVAL;
2454 break;
2455 }
2456
Jens Axboedef596e2019-01-09 08:59:42 -07002457 if (ret)
2458 return ret;
2459
2460 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe9e645e112019-05-10 16:07:28 -06002461 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07002462 return -EAGAIN;
2463
2464 /* workqueue context doesn't hold uring_lock, grab it now */
Jackie Liuba5290c2019-10-09 09:19:59 +08002465 if (s->in_async)
Jens Axboedef596e2019-01-09 08:59:42 -07002466 mutex_lock(&ctx->uring_lock);
2467 io_iopoll_req_issued(req);
Jackie Liuba5290c2019-10-09 09:19:59 +08002468 if (s->in_async)
Jens Axboedef596e2019-01-09 08:59:42 -07002469 mutex_unlock(&ctx->uring_lock);
2470 }
2471
2472 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002473}
2474
Jens Axboe561fb042019-10-24 07:25:42 -06002475static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboe31b51512019-01-18 22:56:34 -07002476{
Jens Axboe561fb042019-10-24 07:25:42 -06002477 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002478 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe561fb042019-10-24 07:25:42 -06002479 struct sqe_submit *s = &req->submit;
2480 const struct io_uring_sqe *sqe = s->sqe;
2481 struct io_kiocb *nxt = NULL;
2482 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002483
Jens Axboe561fb042019-10-24 07:25:42 -06002484 /* Ensure we clear previously set non-block flag */
2485 req->rw.ki_flags &= ~IOCB_NOWAIT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002486
Jens Axboe561fb042019-10-24 07:25:42 -06002487 if (work->flags & IO_WQ_WORK_CANCEL)
2488 ret = -ECANCELED;
Jens Axboe31b51512019-01-18 22:56:34 -07002489
Jens Axboe561fb042019-10-24 07:25:42 -06002490 if (!ret) {
2491 s->has_user = (work->flags & IO_WQ_WORK_HAS_MM) != 0;
2492 s->in_async = true;
2493 do {
Jackie Liua197f662019-11-08 08:09:12 -07002494 ret = __io_submit_sqe(req, &nxt, false);
Jens Axboe561fb042019-10-24 07:25:42 -06002495 /*
2496 * We can get EAGAIN for polled IO even though we're
2497 * forcing a sync submission from here, since we can't
2498 * wait for request slots on the block side.
2499 */
2500 if (ret != -EAGAIN)
2501 break;
2502 cond_resched();
2503 } while (1);
2504 }
Jens Axboe31b51512019-01-18 22:56:34 -07002505
Jens Axboe561fb042019-10-24 07:25:42 -06002506 /* drop submission reference */
Jackie Liuec9c02a2019-11-08 23:50:36 +08002507 io_put_req(req);
Jens Axboe817869d2019-04-30 14:44:05 -06002508
Jens Axboe561fb042019-10-24 07:25:42 -06002509 if (ret) {
Jens Axboef1f40852019-11-05 20:33:16 -07002510 if (req->flags & REQ_F_LINK)
2511 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07002512 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08002513 io_put_req(req);
Jens Axboeedafcce2019-01-09 09:16:05 -07002514 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002515
Jens Axboe561fb042019-10-24 07:25:42 -06002516 /* async context always use a copy of the sqe */
2517 kfree(sqe);
2518
2519 /* if a dependent link is ready, pass it back */
2520 if (!ret && nxt) {
2521 io_prep_async_work(nxt);
2522 *workptr = &nxt->work;
Jens Axboeedafcce2019-01-09 09:16:05 -07002523 }
Jens Axboe31b51512019-01-18 22:56:34 -07002524}
Jens Axboe2b188cc2019-01-07 10:46:33 -07002525
Jens Axboe09bb8392019-03-13 12:39:28 -06002526static bool io_op_needs_file(const struct io_uring_sqe *sqe)
2527{
2528 int op = READ_ONCE(sqe->opcode);
2529
2530 switch (op) {
2531 case IORING_OP_NOP:
2532 case IORING_OP_POLL_REMOVE:
2533 return false;
2534 default:
2535 return true;
2536 }
2537}
2538
Jens Axboe65e19f52019-10-26 07:20:21 -06002539static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
2540 int index)
2541{
2542 struct fixed_file_table *table;
2543
2544 table = &ctx->file_table[index >> IORING_FILE_TABLE_SHIFT];
2545 return table->files[index & IORING_FILE_TABLE_MASK];
2546}
2547
Jackie Liua197f662019-11-08 08:09:12 -07002548static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req)
Jens Axboe09bb8392019-03-13 12:39:28 -06002549{
Pavel Begunkov267bc902019-11-07 01:41:08 +03002550 struct sqe_submit *s = &req->submit;
Jackie Liua197f662019-11-08 08:09:12 -07002551 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe09bb8392019-03-13 12:39:28 -06002552 unsigned flags;
2553 int fd;
2554
2555 flags = READ_ONCE(s->sqe->flags);
2556 fd = READ_ONCE(s->sqe->fd);
2557
Jackie Liu4fe2c962019-09-09 20:50:40 +08002558 if (flags & IOSQE_IO_DRAIN)
Jens Axboede0617e2019-04-06 21:51:27 -06002559 req->flags |= REQ_F_IO_DRAIN;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002560 /*
2561 * All io need record the previous position, if LINK vs DARIN,
2562 * it can be used to mark the position of the first IO in the
2563 * link list.
2564 */
2565 req->sequence = s->sequence;
Jens Axboede0617e2019-04-06 21:51:27 -06002566
Jens Axboe60c112b2019-06-21 10:20:18 -06002567 if (!io_op_needs_file(s->sqe))
Jens Axboe09bb8392019-03-13 12:39:28 -06002568 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06002569
2570 if (flags & IOSQE_FIXED_FILE) {
Jens Axboe65e19f52019-10-26 07:20:21 -06002571 if (unlikely(!ctx->file_table ||
Jens Axboe09bb8392019-03-13 12:39:28 -06002572 (unsigned) fd >= ctx->nr_user_files))
2573 return -EBADF;
Jens Axboeb7620122019-10-26 07:22:55 -06002574 fd = array_index_nospec(fd, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06002575 req->file = io_file_from_index(ctx, fd);
2576 if (!req->file)
Jens Axboe08a45172019-10-03 08:11:03 -06002577 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06002578 req->flags |= REQ_F_FIXED_FILE;
2579 } else {
2580 if (s->needs_fixed_file)
2581 return -EBADF;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002582 trace_io_uring_file_get(ctx, fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06002583 req->file = io_file_get(state, fd);
2584 if (unlikely(!req->file))
2585 return -EBADF;
2586 }
2587
2588 return 0;
2589}
2590
Jackie Liua197f662019-11-08 08:09:12 -07002591static int io_grab_files(struct io_kiocb *req)
Jens Axboefcb323c2019-10-24 12:39:47 -06002592{
2593 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07002594 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06002595
2596 rcu_read_lock();
2597 spin_lock_irq(&ctx->inflight_lock);
2598 /*
2599 * We use the f_ops->flush() handler to ensure that we can flush
2600 * out work accessing these files if the fd is closed. Check if
2601 * the fd has changed since we started down this path, and disallow
2602 * this operation if it has.
2603 */
2604 if (fcheck(req->submit.ring_fd) == req->submit.ring_file) {
2605 list_add(&req->inflight_entry, &ctx->inflight_list);
2606 req->flags |= REQ_F_INFLIGHT;
2607 req->work.files = current->files;
2608 ret = 0;
2609 }
2610 spin_unlock_irq(&ctx->inflight_lock);
2611 rcu_read_unlock();
2612
2613 return ret;
2614}
2615
Jens Axboe2665abf2019-11-05 12:40:47 -07002616static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
2617{
2618 struct io_kiocb *req = container_of(timer, struct io_kiocb,
2619 timeout.timer);
2620 struct io_ring_ctx *ctx = req->ctx;
2621 struct io_kiocb *prev = NULL;
2622 unsigned long flags;
2623 int ret = -ETIME;
2624
2625 spin_lock_irqsave(&ctx->completion_lock, flags);
2626
2627 /*
2628 * We don't expect the list to be empty, that will only happen if we
2629 * race with the completion of the linked work.
2630 */
2631 if (!list_empty(&req->list)) {
2632 prev = list_entry(req->list.prev, struct io_kiocb, link_list);
2633 list_del_init(&req->list);
2634 }
2635
2636 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2637
2638 if (prev) {
2639 void *user_data = (void *) (unsigned long) prev->user_data;
2640 ret = io_async_cancel_one(ctx, user_data);
2641 }
2642
Jens Axboe78e19bb2019-11-06 15:21:34 -07002643 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08002644 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07002645 return HRTIMER_NORESTART;
2646}
2647
2648static int io_queue_linked_timeout(struct io_kiocb *req, struct io_kiocb *nxt)
2649{
2650 const struct io_uring_sqe *sqe = nxt->submit.sqe;
2651 enum hrtimer_mode mode;
2652 struct timespec64 ts;
2653 int ret = -EINVAL;
2654
2655 if (sqe->ioprio || sqe->buf_index || sqe->len != 1 || sqe->off)
2656 goto err;
2657 if (sqe->timeout_flags & ~IORING_TIMEOUT_ABS)
2658 goto err;
2659 if (get_timespec64(&ts, u64_to_user_ptr(sqe->addr))) {
2660 ret = -EFAULT;
2661 goto err;
2662 }
2663
2664 req->flags |= REQ_F_LINK_TIMEOUT;
2665
2666 if (sqe->timeout_flags & IORING_TIMEOUT_ABS)
2667 mode = HRTIMER_MODE_ABS;
2668 else
2669 mode = HRTIMER_MODE_REL;
2670 hrtimer_init(&nxt->timeout.timer, CLOCK_MONOTONIC, mode);
2671 nxt->timeout.timer.function = io_link_timeout_fn;
2672 hrtimer_start(&nxt->timeout.timer, timespec64_to_ktime(ts), mode);
2673 ret = 0;
2674err:
2675 /* drop submission reference */
Jackie Liuec9c02a2019-11-08 23:50:36 +08002676 io_put_req(nxt);
Jens Axboe2665abf2019-11-05 12:40:47 -07002677
2678 if (ret) {
2679 struct io_ring_ctx *ctx = req->ctx;
2680
2681 /*
2682 * Break the link and fail linked timeout, parent will get
2683 * failed by the regular submission path.
2684 */
2685 list_del(&nxt->list);
Jens Axboe78e19bb2019-11-06 15:21:34 -07002686 io_cqring_fill_event(nxt, ret);
Jens Axboe2665abf2019-11-05 12:40:47 -07002687 trace_io_uring_fail_link(req, nxt);
2688 io_commit_cqring(ctx);
Jackie Liuec9c02a2019-11-08 23:50:36 +08002689 io_put_req(nxt);
Jens Axboe2665abf2019-11-05 12:40:47 -07002690 ret = -ECANCELED;
2691 }
2692
2693 return ret;
2694}
2695
2696static inline struct io_kiocb *io_get_linked_timeout(struct io_kiocb *req)
2697{
2698 struct io_kiocb *nxt;
2699
2700 if (!(req->flags & REQ_F_LINK))
2701 return NULL;
2702
2703 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, list);
2704 if (nxt && nxt->submit.sqe->opcode == IORING_OP_LINK_TIMEOUT)
2705 return nxt;
2706
2707 return NULL;
2708}
2709
Jackie Liua197f662019-11-08 08:09:12 -07002710static int __io_queue_sqe(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002711{
Jens Axboe2665abf2019-11-05 12:40:47 -07002712 struct io_kiocb *nxt;
Jens Axboee0c5c572019-03-12 10:18:47 -06002713 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002714
Jens Axboe2665abf2019-11-05 12:40:47 -07002715 nxt = io_get_linked_timeout(req);
2716 if (unlikely(nxt)) {
2717 ret = io_queue_linked_timeout(req, nxt);
2718 if (ret)
2719 goto err;
2720 }
2721
Jackie Liua197f662019-11-08 08:09:12 -07002722 ret = __io_submit_sqe(req, NULL, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06002723
2724 /*
2725 * We async punt it if the file wasn't marked NOWAIT, or if the file
2726 * doesn't support non-blocking read/write attempts
2727 */
2728 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
2729 (req->flags & REQ_F_MUST_PUNT))) {
Pavel Begunkov267bc902019-11-07 01:41:08 +03002730 struct sqe_submit *s = &req->submit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002731 struct io_uring_sqe *sqe_copy;
2732
Jackie Liu954dab12019-09-18 10:37:52 +08002733 sqe_copy = kmemdup(s->sqe, sizeof(*sqe_copy), GFP_KERNEL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002734 if (sqe_copy) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07002735 s->sqe = sqe_copy;
Jens Axboefcb323c2019-10-24 12:39:47 -06002736 if (req->work.flags & IO_WQ_WORK_NEEDS_FILES) {
Jackie Liua197f662019-11-08 08:09:12 -07002737 ret = io_grab_files(req);
Jens Axboefcb323c2019-10-24 12:39:47 -06002738 if (ret) {
2739 kfree(sqe_copy);
2740 goto err;
2741 }
2742 }
Jens Axboee65ef562019-03-12 10:16:44 -06002743
2744 /*
2745 * Queued up for async execution, worker will release
Jens Axboe9e645e112019-05-10 16:07:28 -06002746 * submit reference when the iocb is actually submitted.
Jens Axboee65ef562019-03-12 10:16:44 -06002747 */
Jackie Liua197f662019-11-08 08:09:12 -07002748 io_queue_async_work(req);
Jens Axboee65ef562019-03-12 10:16:44 -06002749 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002750 }
2751 }
Jens Axboee65ef562019-03-12 10:16:44 -06002752
2753 /* drop submission reference */
Jens Axboefcb323c2019-10-24 12:39:47 -06002754err:
Jackie Liuec9c02a2019-11-08 23:50:36 +08002755 io_put_req(req);
Jens Axboee65ef562019-03-12 10:16:44 -06002756
2757 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06002758 if (ret) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07002759 io_cqring_add_event(req, ret);
Jens Axboe9e645e112019-05-10 16:07:28 -06002760 if (req->flags & REQ_F_LINK)
2761 req->flags |= REQ_F_FAIL_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08002762 io_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002763 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002764
2765 return ret;
2766}
2767
Jackie Liua197f662019-11-08 08:09:12 -07002768static int io_queue_sqe(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08002769{
2770 int ret;
2771
Jackie Liua197f662019-11-08 08:09:12 -07002772 ret = io_req_defer(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002773 if (ret) {
2774 if (ret != -EIOCBQUEUED) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07002775 io_cqring_add_event(req, ret);
2776 io_double_put_req(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002777 }
2778 return 0;
2779 }
2780
Jackie Liua197f662019-11-08 08:09:12 -07002781 return __io_queue_sqe(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002782}
2783
Jackie Liua197f662019-11-08 08:09:12 -07002784static int io_queue_link_head(struct io_kiocb *req, struct io_kiocb *shadow)
Jackie Liu4fe2c962019-09-09 20:50:40 +08002785{
2786 int ret;
2787 int need_submit = false;
Jackie Liua197f662019-11-08 08:09:12 -07002788 struct io_ring_ctx *ctx = req->ctx;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002789
2790 if (!shadow)
Jackie Liua197f662019-11-08 08:09:12 -07002791 return io_queue_sqe(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002792
2793 /*
2794 * Mark the first IO in link list as DRAIN, let all the following
2795 * IOs enter the defer list. all IO needs to be completed before link
2796 * list.
2797 */
2798 req->flags |= REQ_F_IO_DRAIN;
Jackie Liua197f662019-11-08 08:09:12 -07002799 ret = io_req_defer(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002800 if (ret) {
2801 if (ret != -EIOCBQUEUED) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07002802 io_cqring_add_event(req, ret);
2803 io_double_put_req(req);
Pavel Begunkov7b202382019-10-27 22:10:36 +03002804 __io_free_req(shadow);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002805 return 0;
2806 }
2807 } else {
2808 /*
2809 * If ret == 0 means that all IOs in front of link io are
2810 * running done. let's queue link head.
2811 */
2812 need_submit = true;
2813 }
2814
2815 /* Insert shadow req to defer_list, blocking next IOs */
2816 spin_lock_irq(&ctx->completion_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002817 trace_io_uring_defer(ctx, shadow, true);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002818 list_add_tail(&shadow->list, &ctx->defer_list);
2819 spin_unlock_irq(&ctx->completion_lock);
2820
2821 if (need_submit)
Jackie Liua197f662019-11-08 08:09:12 -07002822 return __io_queue_sqe(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002823
2824 return 0;
2825}
2826
Jens Axboe9e645e112019-05-10 16:07:28 -06002827#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK)
2828
Jackie Liua197f662019-11-08 08:09:12 -07002829static void io_submit_sqe(struct io_kiocb *req, struct io_submit_state *state,
2830 struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06002831{
2832 struct io_uring_sqe *sqe_copy;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002833 struct sqe_submit *s = &req->submit;
Jackie Liua197f662019-11-08 08:09:12 -07002834 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -06002835 int ret;
2836
Jens Axboe78e19bb2019-11-06 15:21:34 -07002837 req->user_data = s->sqe->user_data;
2838
Jens Axboe9e645e112019-05-10 16:07:28 -06002839 /* enforce forwards compatibility on users */
2840 if (unlikely(s->sqe->flags & ~SQE_VALID_FLAGS)) {
2841 ret = -EINVAL;
Pavel Begunkov196be952019-11-07 01:41:06 +03002842 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06002843 }
2844
Jackie Liua197f662019-11-08 08:09:12 -07002845 ret = io_req_set_file(state, req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002846 if (unlikely(ret)) {
2847err_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07002848 io_cqring_add_event(req, ret);
2849 io_double_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002850 return;
2851 }
2852
Jens Axboe9e645e112019-05-10 16:07:28 -06002853 /*
2854 * If we already have a head request, queue this one for async
2855 * submittal once the head completes. If we don't have a head but
2856 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
2857 * submitted sync once the chain is complete. If none of those
2858 * conditions are true (normal request), then just queue it.
2859 */
2860 if (*link) {
2861 struct io_kiocb *prev = *link;
2862
2863 sqe_copy = kmemdup(s->sqe, sizeof(*sqe_copy), GFP_KERNEL);
2864 if (!sqe_copy) {
2865 ret = -EAGAIN;
2866 goto err_req;
2867 }
2868
2869 s->sqe = sqe_copy;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002870 trace_io_uring_link(ctx, req, prev);
Jens Axboe9e645e112019-05-10 16:07:28 -06002871 list_add_tail(&req->list, &prev->link_list);
2872 } else if (s->sqe->flags & IOSQE_IO_LINK) {
2873 req->flags |= REQ_F_LINK;
2874
Jens Axboe9e645e112019-05-10 16:07:28 -06002875 INIT_LIST_HEAD(&req->link_list);
2876 *link = req;
Jens Axboe2665abf2019-11-05 12:40:47 -07002877 } else if (READ_ONCE(s->sqe->opcode) == IORING_OP_LINK_TIMEOUT) {
2878 /* Only valid as a linked SQE */
2879 ret = -EINVAL;
2880 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06002881 } else {
Jackie Liua197f662019-11-08 08:09:12 -07002882 io_queue_sqe(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002883 }
2884}
2885
Jens Axboe9a56a232019-01-09 09:06:50 -07002886/*
2887 * Batched submission is done, ensure local IO is flushed out.
2888 */
2889static void io_submit_state_end(struct io_submit_state *state)
2890{
2891 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06002892 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07002893 if (state->free_reqs)
2894 kmem_cache_free_bulk(req_cachep, state->free_reqs,
2895 &state->reqs[state->cur_req]);
Jens Axboe9a56a232019-01-09 09:06:50 -07002896}
2897
2898/*
2899 * Start submission side cache.
2900 */
2901static void io_submit_state_start(struct io_submit_state *state,
2902 struct io_ring_ctx *ctx, unsigned max_ios)
2903{
2904 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07002905 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07002906 state->file = NULL;
2907 state->ios_left = max_ios;
2908}
2909
Jens Axboe2b188cc2019-01-07 10:46:33 -07002910static void io_commit_sqring(struct io_ring_ctx *ctx)
2911{
Hristo Venev75b28af2019-08-26 17:23:46 +00002912 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002913
Hristo Venev75b28af2019-08-26 17:23:46 +00002914 if (ctx->cached_sq_head != READ_ONCE(rings->sq.head)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07002915 /*
2916 * Ensure any loads from the SQEs are done at this point,
2917 * since once we write the new head, the application could
2918 * write new data to them.
2919 */
Hristo Venev75b28af2019-08-26 17:23:46 +00002920 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002921 }
2922}
2923
2924/*
Jens Axboe2b188cc2019-01-07 10:46:33 -07002925 * Fetch an sqe, if one is available. Note that s->sqe will point to memory
2926 * that is mapped by userspace. This means that care needs to be taken to
2927 * ensure that reads are stable, as we cannot rely on userspace always
2928 * being a good citizen. If members of the sqe are validated and then later
2929 * used, it's important that those reads are done through READ_ONCE() to
2930 * prevent a re-load down the line.
2931 */
2932static bool io_get_sqring(struct io_ring_ctx *ctx, struct sqe_submit *s)
2933{
Hristo Venev75b28af2019-08-26 17:23:46 +00002934 struct io_rings *rings = ctx->rings;
2935 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002936 unsigned head;
2937
2938 /*
2939 * The cached sq head (or cq tail) serves two purposes:
2940 *
2941 * 1) allows us to batch the cost of updating the user visible
2942 * head updates.
2943 * 2) allows the kernel side to track the head on its own, even
2944 * though the application is the one updating it.
2945 */
2946 head = ctx->cached_sq_head;
Stefan Bühlere523a292019-04-19 11:57:44 +02002947 /* make sure SQ entry isn't read before tail */
Hristo Venev75b28af2019-08-26 17:23:46 +00002948 if (head == smp_load_acquire(&rings->sq.tail))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002949 return false;
2950
Hristo Venev75b28af2019-08-26 17:23:46 +00002951 head = READ_ONCE(sq_array[head & ctx->sq_mask]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002952 if (head < ctx->sq_entries) {
Jens Axboefcb323c2019-10-24 12:39:47 -06002953 s->ring_file = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002954 s->sqe = &ctx->sq_sqes[head];
Jackie Liu8776f3f2019-09-09 20:50:39 +08002955 s->sequence = ctx->cached_sq_head;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002956 ctx->cached_sq_head++;
2957 return true;
2958 }
2959
2960 /* drop invalid entries */
2961 ctx->cached_sq_head++;
Jens Axboe498ccd92019-10-25 10:04:25 -06002962 ctx->cached_sq_dropped++;
2963 WRITE_ONCE(rings->sq_dropped, ctx->cached_sq_dropped);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002964 return false;
2965}
2966
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002967static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkovae9428c2019-11-06 00:22:14 +03002968 struct file *ring_file, int ring_fd,
2969 struct mm_struct **mm, bool async)
Jens Axboe6c271ce2019-01-10 11:22:30 -07002970{
2971 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002972 struct io_kiocb *link = NULL;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002973 struct io_kiocb *shadow_req = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002974 int i, submitted = 0;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03002975 bool mm_fault = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002976
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07002977 if (!list_empty(&ctx->cq_overflow_list)) {
2978 io_cqring_overflow_flush(ctx, false);
2979 return -EBUSY;
2980 }
2981
Jens Axboe6c271ce2019-01-10 11:22:30 -07002982 if (nr > IO_PLUG_THRESHOLD) {
2983 io_submit_state_start(&state, ctx, nr);
2984 statep = &state;
2985 }
2986
2987 for (i = 0; i < nr; i++) {
Pavel Begunkov196be952019-11-07 01:41:06 +03002988 struct io_kiocb *req;
Pavel Begunkov50585b92019-11-07 01:41:07 +03002989 unsigned int sqe_flags;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002990
Pavel Begunkov196be952019-11-07 01:41:06 +03002991 req = io_get_req(ctx, statep);
2992 if (unlikely(!req)) {
2993 if (!submitted)
2994 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002995 break;
Pavel Begunkov196be952019-11-07 01:41:06 +03002996 }
Pavel Begunkov50585b92019-11-07 01:41:07 +03002997 if (!io_get_sqring(ctx, &req->submit)) {
Pavel Begunkov196be952019-11-07 01:41:06 +03002998 __io_free_req(req);
2999 break;
3000 }
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003001
Pavel Begunkov50585b92019-11-07 01:41:07 +03003002 if (io_sqe_needs_user(req->submit.sqe) && !*mm) {
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03003003 mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm);
3004 if (!mm_fault) {
3005 use_mm(ctx->sqo_mm);
3006 *mm = ctx->sqo_mm;
3007 }
3008 }
3009
Pavel Begunkov50585b92019-11-07 01:41:07 +03003010 sqe_flags = req->submit.sqe->flags;
3011
3012 if (link && (sqe_flags & IOSQE_IO_DRAIN)) {
Jackie Liu4fe2c962019-09-09 20:50:40 +08003013 if (!shadow_req) {
3014 shadow_req = io_get_req(ctx, NULL);
Jackie Liua1041c22019-09-18 17:25:52 +08003015 if (unlikely(!shadow_req))
3016 goto out;
Jackie Liu4fe2c962019-09-09 20:50:40 +08003017 shadow_req->flags |= (REQ_F_IO_DRAIN | REQ_F_SHADOW_DRAIN);
3018 refcount_dec(&shadow_req->refs);
3019 }
Pavel Begunkov50585b92019-11-07 01:41:07 +03003020 shadow_req->sequence = req->submit.sequence;
Jackie Liu4fe2c962019-09-09 20:50:40 +08003021 }
3022
Jackie Liua1041c22019-09-18 17:25:52 +08003023out:
Pavel Begunkov50585b92019-11-07 01:41:07 +03003024 req->submit.ring_file = ring_file;
3025 req->submit.ring_fd = ring_fd;
3026 req->submit.has_user = *mm != NULL;
3027 req->submit.in_async = async;
3028 req->submit.needs_fixed_file = async;
3029 trace_io_uring_submit_sqe(ctx, req->submit.sqe->user_data,
3030 true, async);
Jackie Liua197f662019-11-08 08:09:12 -07003031 io_submit_sqe(req, statep, &link);
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03003032 submitted++;
Pavel Begunkove5eb6362019-11-06 00:22:15 +03003033
3034 /*
3035 * If previous wasn't linked and we have a linked command,
3036 * that's the end of the chain. Submit the previous link.
3037 */
Pavel Begunkov50585b92019-11-07 01:41:07 +03003038 if (!(sqe_flags & IOSQE_IO_LINK) && link) {
Jackie Liua197f662019-11-08 08:09:12 -07003039 io_queue_link_head(link, shadow_req);
Pavel Begunkove5eb6362019-11-06 00:22:15 +03003040 link = NULL;
3041 shadow_req = NULL;
3042 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07003043 }
3044
Jens Axboe9e645e112019-05-10 16:07:28 -06003045 if (link)
Jackie Liua197f662019-11-08 08:09:12 -07003046 io_queue_link_head(link, shadow_req);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003047 if (statep)
3048 io_submit_state_end(&state);
3049
Pavel Begunkovae9428c2019-11-06 00:22:14 +03003050 /* Commit SQ ring head once we've consumed and submitted all SQEs */
3051 io_commit_sqring(ctx);
3052
Jens Axboe6c271ce2019-01-10 11:22:30 -07003053 return submitted;
3054}
3055
3056static int io_sq_thread(void *data)
3057{
Jens Axboe6c271ce2019-01-10 11:22:30 -07003058 struct io_ring_ctx *ctx = data;
3059 struct mm_struct *cur_mm = NULL;
3060 mm_segment_t old_fs;
3061 DEFINE_WAIT(wait);
3062 unsigned inflight;
3063 unsigned long timeout;
3064
Jens Axboe206aefd2019-11-07 18:27:42 -07003065 complete(&ctx->completions[1]);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08003066
Jens Axboe6c271ce2019-01-10 11:22:30 -07003067 old_fs = get_fs();
3068 set_fs(USER_DS);
3069
3070 timeout = inflight = 0;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003071 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003072 unsigned int to_submit;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003073 int ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003074
3075 if (inflight) {
3076 unsigned nr_events = 0;
3077
3078 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe2b2ed972019-10-25 10:06:15 -06003079 /*
3080 * inflight is the count of the maximum possible
3081 * entries we submitted, but it can be smaller
3082 * if we dropped some of them. If we don't have
3083 * poll entries available, then we know that we
3084 * have nothing left to poll for. Reset the
3085 * inflight count to zero in that case.
3086 */
3087 mutex_lock(&ctx->uring_lock);
3088 if (!list_empty(&ctx->poll_list))
3089 __io_iopoll_check(ctx, &nr_events, 0);
3090 else
3091 inflight = 0;
3092 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003093 } else {
3094 /*
3095 * Normal IO, just pretend everything completed.
3096 * We don't have to poll completions for that.
3097 */
3098 nr_events = inflight;
3099 }
3100
3101 inflight -= nr_events;
3102 if (!inflight)
3103 timeout = jiffies + ctx->sq_thread_idle;
3104 }
3105
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003106 to_submit = io_sqring_entries(ctx);
3107 if (!to_submit) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07003108 /*
3109 * We're polling. If we're within the defined idle
3110 * period, then let us spin without work before going
3111 * to sleep.
3112 */
3113 if (inflight || !time_after(jiffies, timeout)) {
Jens Axboe9831a902019-09-19 09:48:55 -06003114 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07003115 continue;
3116 }
3117
3118 /*
3119 * Drop cur_mm before scheduling, we can't hold it for
3120 * long periods (or over schedule()). Do this before
3121 * adding ourselves to the waitqueue, as the unuse/drop
3122 * may sleep.
3123 */
3124 if (cur_mm) {
3125 unuse_mm(cur_mm);
3126 mmput(cur_mm);
3127 cur_mm = NULL;
3128 }
3129
3130 prepare_to_wait(&ctx->sqo_wait, &wait,
3131 TASK_INTERRUPTIBLE);
3132
3133 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00003134 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02003135 /* make sure to read SQ tail after writing flags */
3136 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07003137
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003138 to_submit = io_sqring_entries(ctx);
3139 if (!to_submit) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003140 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07003141 finish_wait(&ctx->sqo_wait, &wait);
3142 break;
3143 }
3144 if (signal_pending(current))
3145 flush_signals(current);
3146 schedule();
3147 finish_wait(&ctx->sqo_wait, &wait);
3148
Hristo Venev75b28af2019-08-26 17:23:46 +00003149 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003150 continue;
3151 }
3152 finish_wait(&ctx->sqo_wait, &wait);
3153
Hristo Venev75b28af2019-08-26 17:23:46 +00003154 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003155 }
3156
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003157 to_submit = min(to_submit, ctx->sq_entries);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003158 ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true);
3159 if (ret > 0)
3160 inflight += ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003161 }
3162
3163 set_fs(old_fs);
3164 if (cur_mm) {
3165 unuse_mm(cur_mm);
3166 mmput(cur_mm);
3167 }
Jens Axboe06058632019-04-13 09:26:03 -06003168
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003169 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06003170
Jens Axboe6c271ce2019-01-10 11:22:30 -07003171 return 0;
3172}
3173
Jens Axboebda52162019-09-24 13:47:15 -06003174struct io_wait_queue {
3175 struct wait_queue_entry wq;
3176 struct io_ring_ctx *ctx;
3177 unsigned to_wait;
3178 unsigned nr_timeouts;
3179};
3180
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003181static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06003182{
3183 struct io_ring_ctx *ctx = iowq->ctx;
3184
3185 /*
3186 * Wake up if we have enough events, or if a timeout occured since we
3187 * started waiting. For timeouts, we always want to return to userspace,
3188 * regardless of event count.
3189 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003190 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06003191 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
3192}
3193
3194static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
3195 int wake_flags, void *key)
3196{
3197 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
3198 wq);
3199
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003200 /* use noflush == true, as we can't safely rely on locking context */
3201 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06003202 return -1;
3203
3204 return autoremove_wake_function(curr, mode, wake_flags, key);
3205}
3206
Jens Axboe2b188cc2019-01-07 10:46:33 -07003207/*
3208 * Wait until events become available, if we don't already have some. The
3209 * application must reap them itself, as they reside on the shared cq ring.
3210 */
3211static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
3212 const sigset_t __user *sig, size_t sigsz)
3213{
Jens Axboebda52162019-09-24 13:47:15 -06003214 struct io_wait_queue iowq = {
3215 .wq = {
3216 .private = current,
3217 .func = io_wake_function,
3218 .entry = LIST_HEAD_INIT(iowq.wq.entry),
3219 },
3220 .ctx = ctx,
3221 .to_wait = min_events,
3222 };
Hristo Venev75b28af2019-08-26 17:23:46 +00003223 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08003224 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003225
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003226 if (io_cqring_events(ctx, false) >= min_events)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003227 return 0;
3228
3229 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01003230#ifdef CONFIG_COMPAT
3231 if (in_compat_syscall())
3232 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07003233 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01003234 else
3235#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07003236 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01003237
Jens Axboe2b188cc2019-01-07 10:46:33 -07003238 if (ret)
3239 return ret;
3240 }
3241
Jens Axboebda52162019-09-24 13:47:15 -06003242 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02003243 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06003244 do {
3245 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
3246 TASK_INTERRUPTIBLE);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003247 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06003248 break;
3249 schedule();
3250 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08003251 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06003252 break;
3253 }
3254 } while (1);
3255 finish_wait(&ctx->wait, &iowq.wq);
3256
Jackie Liue9ffa5c2019-10-29 11:16:42 +08003257 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003258
Hristo Venev75b28af2019-08-26 17:23:46 +00003259 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003260}
3261
Jens Axboe6b063142019-01-10 22:13:58 -07003262static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
3263{
3264#if defined(CONFIG_UNIX)
3265 if (ctx->ring_sock) {
3266 struct sock *sock = ctx->ring_sock->sk;
3267 struct sk_buff *skb;
3268
3269 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
3270 kfree_skb(skb);
3271 }
3272#else
3273 int i;
3274
Jens Axboe65e19f52019-10-26 07:20:21 -06003275 for (i = 0; i < ctx->nr_user_files; i++) {
3276 struct file *file;
3277
3278 file = io_file_from_index(ctx, i);
3279 if (file)
3280 fput(file);
3281 }
Jens Axboe6b063142019-01-10 22:13:58 -07003282#endif
3283}
3284
3285static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
3286{
Jens Axboe65e19f52019-10-26 07:20:21 -06003287 unsigned nr_tables, i;
3288
3289 if (!ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003290 return -ENXIO;
3291
3292 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06003293 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
3294 for (i = 0; i < nr_tables; i++)
3295 kfree(ctx->file_table[i].files);
3296 kfree(ctx->file_table);
3297 ctx->file_table = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003298 ctx->nr_user_files = 0;
3299 return 0;
3300}
3301
Jens Axboe6c271ce2019-01-10 11:22:30 -07003302static void io_sq_thread_stop(struct io_ring_ctx *ctx)
3303{
3304 if (ctx->sqo_thread) {
Jens Axboe206aefd2019-11-07 18:27:42 -07003305 wait_for_completion(&ctx->completions[1]);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003306 /*
3307 * The park is a bit of a work-around, without it we get
3308 * warning spews on shutdown with SQPOLL set and affinity
3309 * set to a single CPU.
3310 */
Jens Axboe06058632019-04-13 09:26:03 -06003311 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003312 kthread_stop(ctx->sqo_thread);
3313 ctx->sqo_thread = NULL;
3314 }
3315}
3316
Jens Axboe6b063142019-01-10 22:13:58 -07003317static void io_finish_async(struct io_ring_ctx *ctx)
3318{
Jens Axboe6c271ce2019-01-10 11:22:30 -07003319 io_sq_thread_stop(ctx);
3320
Jens Axboe561fb042019-10-24 07:25:42 -06003321 if (ctx->io_wq) {
3322 io_wq_destroy(ctx->io_wq);
3323 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003324 }
3325}
3326
3327#if defined(CONFIG_UNIX)
3328static void io_destruct_skb(struct sk_buff *skb)
3329{
3330 struct io_ring_ctx *ctx = skb->sk->sk_user_data;
3331
Jens Axboe561fb042019-10-24 07:25:42 -06003332 if (ctx->io_wq)
3333 io_wq_flush(ctx->io_wq);
Jens Axboe8a997342019-10-09 14:40:13 -06003334
Jens Axboe6b063142019-01-10 22:13:58 -07003335 unix_destruct_scm(skb);
3336}
3337
3338/*
3339 * Ensure the UNIX gc is aware of our file set, so we are certain that
3340 * the io_uring can be safely unregistered on process exit, even if we have
3341 * loops in the file referencing.
3342 */
3343static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
3344{
3345 struct sock *sk = ctx->ring_sock->sk;
3346 struct scm_fp_list *fpl;
3347 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06003348 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07003349
3350 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
3351 unsigned long inflight = ctx->user->unix_inflight + nr;
3352
3353 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
3354 return -EMFILE;
3355 }
3356
3357 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
3358 if (!fpl)
3359 return -ENOMEM;
3360
3361 skb = alloc_skb(0, GFP_KERNEL);
3362 if (!skb) {
3363 kfree(fpl);
3364 return -ENOMEM;
3365 }
3366
3367 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07003368
Jens Axboe08a45172019-10-03 08:11:03 -06003369 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07003370 fpl->user = get_uid(ctx->user);
3371 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003372 struct file *file = io_file_from_index(ctx, i + offset);
3373
3374 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06003375 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06003376 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06003377 unix_inflight(fpl->user, fpl->fp[nr_files]);
3378 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07003379 }
3380
Jens Axboe08a45172019-10-03 08:11:03 -06003381 if (nr_files) {
3382 fpl->max = SCM_MAX_FD;
3383 fpl->count = nr_files;
3384 UNIXCB(skb).fp = fpl;
3385 skb->destructor = io_destruct_skb;
3386 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
3387 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07003388
Jens Axboe08a45172019-10-03 08:11:03 -06003389 for (i = 0; i < nr_files; i++)
3390 fput(fpl->fp[i]);
3391 } else {
3392 kfree_skb(skb);
3393 kfree(fpl);
3394 }
Jens Axboe6b063142019-01-10 22:13:58 -07003395
3396 return 0;
3397}
3398
3399/*
3400 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
3401 * causes regular reference counting to break down. We rely on the UNIX
3402 * garbage collection to take care of this problem for us.
3403 */
3404static int io_sqe_files_scm(struct io_ring_ctx *ctx)
3405{
3406 unsigned left, total;
3407 int ret = 0;
3408
3409 total = 0;
3410 left = ctx->nr_user_files;
3411 while (left) {
3412 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07003413
3414 ret = __io_sqe_files_scm(ctx, this_files, total);
3415 if (ret)
3416 break;
3417 left -= this_files;
3418 total += this_files;
3419 }
3420
3421 if (!ret)
3422 return 0;
3423
3424 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003425 struct file *file = io_file_from_index(ctx, total);
3426
3427 if (file)
3428 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07003429 total++;
3430 }
3431
3432 return ret;
3433}
3434#else
3435static int io_sqe_files_scm(struct io_ring_ctx *ctx)
3436{
3437 return 0;
3438}
3439#endif
3440
Jens Axboe65e19f52019-10-26 07:20:21 -06003441static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
3442 unsigned nr_files)
3443{
3444 int i;
3445
3446 for (i = 0; i < nr_tables; i++) {
3447 struct fixed_file_table *table = &ctx->file_table[i];
3448 unsigned this_files;
3449
3450 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
3451 table->files = kcalloc(this_files, sizeof(struct file *),
3452 GFP_KERNEL);
3453 if (!table->files)
3454 break;
3455 nr_files -= this_files;
3456 }
3457
3458 if (i == nr_tables)
3459 return 0;
3460
3461 for (i = 0; i < nr_tables; i++) {
3462 struct fixed_file_table *table = &ctx->file_table[i];
3463 kfree(table->files);
3464 }
3465 return 1;
3466}
3467
Jens Axboe6b063142019-01-10 22:13:58 -07003468static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
3469 unsigned nr_args)
3470{
3471 __s32 __user *fds = (__s32 __user *) arg;
Jens Axboe65e19f52019-10-26 07:20:21 -06003472 unsigned nr_tables;
Jens Axboe6b063142019-01-10 22:13:58 -07003473 int fd, ret = 0;
3474 unsigned i;
3475
Jens Axboe65e19f52019-10-26 07:20:21 -06003476 if (ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003477 return -EBUSY;
3478 if (!nr_args)
3479 return -EINVAL;
3480 if (nr_args > IORING_MAX_FIXED_FILES)
3481 return -EMFILE;
3482
Jens Axboe65e19f52019-10-26 07:20:21 -06003483 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
3484 ctx->file_table = kcalloc(nr_tables, sizeof(struct fixed_file_table),
3485 GFP_KERNEL);
3486 if (!ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003487 return -ENOMEM;
3488
Jens Axboe65e19f52019-10-26 07:20:21 -06003489 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
3490 kfree(ctx->file_table);
Jens Axboe46568e92019-11-10 08:40:53 -07003491 ctx->file_table = NULL;
Jens Axboe65e19f52019-10-26 07:20:21 -06003492 return -ENOMEM;
3493 }
3494
Jens Axboe08a45172019-10-03 08:11:03 -06003495 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003496 struct fixed_file_table *table;
3497 unsigned index;
3498
Jens Axboe6b063142019-01-10 22:13:58 -07003499 ret = -EFAULT;
3500 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
3501 break;
Jens Axboe08a45172019-10-03 08:11:03 -06003502 /* allow sparse sets */
3503 if (fd == -1) {
3504 ret = 0;
3505 continue;
3506 }
Jens Axboe6b063142019-01-10 22:13:58 -07003507
Jens Axboe65e19f52019-10-26 07:20:21 -06003508 table = &ctx->file_table[i >> IORING_FILE_TABLE_SHIFT];
3509 index = i & IORING_FILE_TABLE_MASK;
3510 table->files[index] = fget(fd);
Jens Axboe6b063142019-01-10 22:13:58 -07003511
3512 ret = -EBADF;
Jens Axboe65e19f52019-10-26 07:20:21 -06003513 if (!table->files[index])
Jens Axboe6b063142019-01-10 22:13:58 -07003514 break;
3515 /*
3516 * Don't allow io_uring instances to be registered. If UNIX
3517 * isn't enabled, then this causes a reference cycle and this
3518 * instance can never get freed. If UNIX is enabled we'll
3519 * handle it just fine, but there's still no point in allowing
3520 * a ring fd as it doesn't support regular read/write anyway.
3521 */
Jens Axboe65e19f52019-10-26 07:20:21 -06003522 if (table->files[index]->f_op == &io_uring_fops) {
3523 fput(table->files[index]);
Jens Axboe6b063142019-01-10 22:13:58 -07003524 break;
3525 }
Jens Axboe6b063142019-01-10 22:13:58 -07003526 ret = 0;
3527 }
3528
3529 if (ret) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003530 for (i = 0; i < ctx->nr_user_files; i++) {
3531 struct file *file;
Jens Axboe6b063142019-01-10 22:13:58 -07003532
Jens Axboe65e19f52019-10-26 07:20:21 -06003533 file = io_file_from_index(ctx, i);
3534 if (file)
3535 fput(file);
3536 }
3537 for (i = 0; i < nr_tables; i++)
3538 kfree(ctx->file_table[i].files);
3539
3540 kfree(ctx->file_table);
3541 ctx->file_table = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003542 ctx->nr_user_files = 0;
3543 return ret;
3544 }
3545
3546 ret = io_sqe_files_scm(ctx);
3547 if (ret)
3548 io_sqe_files_unregister(ctx);
3549
3550 return ret;
3551}
3552
Jens Axboec3a31e62019-10-03 13:59:56 -06003553static void io_sqe_file_unregister(struct io_ring_ctx *ctx, int index)
3554{
3555#if defined(CONFIG_UNIX)
Jens Axboe65e19f52019-10-26 07:20:21 -06003556 struct file *file = io_file_from_index(ctx, index);
Jens Axboec3a31e62019-10-03 13:59:56 -06003557 struct sock *sock = ctx->ring_sock->sk;
3558 struct sk_buff_head list, *head = &sock->sk_receive_queue;
3559 struct sk_buff *skb;
3560 int i;
3561
3562 __skb_queue_head_init(&list);
3563
3564 /*
3565 * Find the skb that holds this file in its SCM_RIGHTS. When found,
3566 * remove this entry and rearrange the file array.
3567 */
3568 skb = skb_dequeue(head);
3569 while (skb) {
3570 struct scm_fp_list *fp;
3571
3572 fp = UNIXCB(skb).fp;
3573 for (i = 0; i < fp->count; i++) {
3574 int left;
3575
3576 if (fp->fp[i] != file)
3577 continue;
3578
3579 unix_notinflight(fp->user, fp->fp[i]);
3580 left = fp->count - 1 - i;
3581 if (left) {
3582 memmove(&fp->fp[i], &fp->fp[i + 1],
3583 left * sizeof(struct file *));
3584 }
3585 fp->count--;
3586 if (!fp->count) {
3587 kfree_skb(skb);
3588 skb = NULL;
3589 } else {
3590 __skb_queue_tail(&list, skb);
3591 }
3592 fput(file);
3593 file = NULL;
3594 break;
3595 }
3596
3597 if (!file)
3598 break;
3599
3600 __skb_queue_tail(&list, skb);
3601
3602 skb = skb_dequeue(head);
3603 }
3604
3605 if (skb_peek(&list)) {
3606 spin_lock_irq(&head->lock);
3607 while ((skb = __skb_dequeue(&list)) != NULL)
3608 __skb_queue_tail(head, skb);
3609 spin_unlock_irq(&head->lock);
3610 }
3611#else
Jens Axboe65e19f52019-10-26 07:20:21 -06003612 fput(io_file_from_index(ctx, index));
Jens Axboec3a31e62019-10-03 13:59:56 -06003613#endif
3614}
3615
3616static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
3617 int index)
3618{
3619#if defined(CONFIG_UNIX)
3620 struct sock *sock = ctx->ring_sock->sk;
3621 struct sk_buff_head *head = &sock->sk_receive_queue;
3622 struct sk_buff *skb;
3623
3624 /*
3625 * See if we can merge this file into an existing skb SCM_RIGHTS
3626 * file set. If there's no room, fall back to allocating a new skb
3627 * and filling it in.
3628 */
3629 spin_lock_irq(&head->lock);
3630 skb = skb_peek(head);
3631 if (skb) {
3632 struct scm_fp_list *fpl = UNIXCB(skb).fp;
3633
3634 if (fpl->count < SCM_MAX_FD) {
3635 __skb_unlink(skb, head);
3636 spin_unlock_irq(&head->lock);
3637 fpl->fp[fpl->count] = get_file(file);
3638 unix_inflight(fpl->user, fpl->fp[fpl->count]);
3639 fpl->count++;
3640 spin_lock_irq(&head->lock);
3641 __skb_queue_head(head, skb);
3642 } else {
3643 skb = NULL;
3644 }
3645 }
3646 spin_unlock_irq(&head->lock);
3647
3648 if (skb) {
3649 fput(file);
3650 return 0;
3651 }
3652
3653 return __io_sqe_files_scm(ctx, 1, index);
3654#else
3655 return 0;
3656#endif
3657}
3658
3659static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
3660 unsigned nr_args)
3661{
3662 struct io_uring_files_update up;
3663 __s32 __user *fds;
3664 int fd, i, err;
3665 __u32 done;
3666
Jens Axboe65e19f52019-10-26 07:20:21 -06003667 if (!ctx->file_table)
Jens Axboec3a31e62019-10-03 13:59:56 -06003668 return -ENXIO;
3669 if (!nr_args)
3670 return -EINVAL;
3671 if (copy_from_user(&up, arg, sizeof(up)))
3672 return -EFAULT;
3673 if (check_add_overflow(up.offset, nr_args, &done))
3674 return -EOVERFLOW;
3675 if (done > ctx->nr_user_files)
3676 return -EINVAL;
3677
3678 done = 0;
3679 fds = (__s32 __user *) up.fds;
3680 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003681 struct fixed_file_table *table;
3682 unsigned index;
3683
Jens Axboec3a31e62019-10-03 13:59:56 -06003684 err = 0;
3685 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
3686 err = -EFAULT;
3687 break;
3688 }
3689 i = array_index_nospec(up.offset, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06003690 table = &ctx->file_table[i >> IORING_FILE_TABLE_SHIFT];
3691 index = i & IORING_FILE_TABLE_MASK;
3692 if (table->files[index]) {
Jens Axboec3a31e62019-10-03 13:59:56 -06003693 io_sqe_file_unregister(ctx, i);
Jens Axboe65e19f52019-10-26 07:20:21 -06003694 table->files[index] = NULL;
Jens Axboec3a31e62019-10-03 13:59:56 -06003695 }
3696 if (fd != -1) {
3697 struct file *file;
3698
3699 file = fget(fd);
3700 if (!file) {
3701 err = -EBADF;
3702 break;
3703 }
3704 /*
3705 * Don't allow io_uring instances to be registered. If
3706 * UNIX isn't enabled, then this causes a reference
3707 * cycle and this instance can never get freed. If UNIX
3708 * is enabled we'll handle it just fine, but there's
3709 * still no point in allowing a ring fd as it doesn't
3710 * support regular read/write anyway.
3711 */
3712 if (file->f_op == &io_uring_fops) {
3713 fput(file);
3714 err = -EBADF;
3715 break;
3716 }
Jens Axboe65e19f52019-10-26 07:20:21 -06003717 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06003718 err = io_sqe_file_register(ctx, file, i);
3719 if (err)
3720 break;
3721 }
3722 nr_args--;
3723 done++;
3724 up.offset++;
3725 }
3726
3727 return done ? done : err;
3728}
3729
Jens Axboe6c271ce2019-01-10 11:22:30 -07003730static int io_sq_offload_start(struct io_ring_ctx *ctx,
3731 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003732{
Jens Axboe561fb042019-10-24 07:25:42 -06003733 unsigned concurrency;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003734 int ret;
3735
Jens Axboe6c271ce2019-01-10 11:22:30 -07003736 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003737 mmgrab(current->mm);
3738 ctx->sqo_mm = current->mm;
3739
Jens Axboe6c271ce2019-01-10 11:22:30 -07003740 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06003741 ret = -EPERM;
3742 if (!capable(CAP_SYS_ADMIN))
3743 goto err;
3744
Jens Axboe917257d2019-04-13 09:28:55 -06003745 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
3746 if (!ctx->sq_thread_idle)
3747 ctx->sq_thread_idle = HZ;
3748
Jens Axboe6c271ce2019-01-10 11:22:30 -07003749 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06003750 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003751
Jens Axboe917257d2019-04-13 09:28:55 -06003752 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06003753 if (cpu >= nr_cpu_ids)
3754 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08003755 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06003756 goto err;
3757
Jens Axboe6c271ce2019-01-10 11:22:30 -07003758 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
3759 ctx, cpu,
3760 "io_uring-sq");
3761 } else {
3762 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
3763 "io_uring-sq");
3764 }
3765 if (IS_ERR(ctx->sqo_thread)) {
3766 ret = PTR_ERR(ctx->sqo_thread);
3767 ctx->sqo_thread = NULL;
3768 goto err;
3769 }
3770 wake_up_process(ctx->sqo_thread);
3771 } else if (p->flags & IORING_SETUP_SQ_AFF) {
3772 /* Can't have SQ_AFF without SQPOLL */
3773 ret = -EINVAL;
3774 goto err;
3775 }
3776
Jens Axboe561fb042019-10-24 07:25:42 -06003777 /* Do QD, or 4 * CPUS, whatever is smallest */
3778 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
Jens Axboe5f8fd2d32019-11-07 10:57:36 -07003779 ctx->io_wq = io_wq_create(concurrency, ctx->sqo_mm, ctx->user);
Jens Axboe975c99a52019-10-30 08:42:56 -06003780 if (IS_ERR(ctx->io_wq)) {
3781 ret = PTR_ERR(ctx->io_wq);
3782 ctx->io_wq = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003783 goto err;
3784 }
3785
3786 return 0;
3787err:
Jens Axboe54a91f32019-09-10 09:15:04 -06003788 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003789 mmdrop(ctx->sqo_mm);
3790 ctx->sqo_mm = NULL;
3791 return ret;
3792}
3793
3794static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
3795{
3796 atomic_long_sub(nr_pages, &user->locked_vm);
3797}
3798
3799static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
3800{
3801 unsigned long page_limit, cur_pages, new_pages;
3802
3803 /* Don't allow more pages than we can safely lock */
3804 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
3805
3806 do {
3807 cur_pages = atomic_long_read(&user->locked_vm);
3808 new_pages = cur_pages + nr_pages;
3809 if (new_pages > page_limit)
3810 return -ENOMEM;
3811 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
3812 new_pages) != cur_pages);
3813
3814 return 0;
3815}
3816
3817static void io_mem_free(void *ptr)
3818{
Mark Rutland52e04ef2019-04-30 17:30:21 +01003819 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003820
Mark Rutland52e04ef2019-04-30 17:30:21 +01003821 if (!ptr)
3822 return;
3823
3824 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003825 if (put_page_testzero(page))
3826 free_compound_page(page);
3827}
3828
3829static void *io_mem_alloc(size_t size)
3830{
3831 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
3832 __GFP_NORETRY;
3833
3834 return (void *) __get_free_pages(gfp_flags, get_order(size));
3835}
3836
Hristo Venev75b28af2019-08-26 17:23:46 +00003837static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
3838 size_t *sq_offset)
3839{
3840 struct io_rings *rings;
3841 size_t off, sq_array_size;
3842
3843 off = struct_size(rings, cqes, cq_entries);
3844 if (off == SIZE_MAX)
3845 return SIZE_MAX;
3846
3847#ifdef CONFIG_SMP
3848 off = ALIGN(off, SMP_CACHE_BYTES);
3849 if (off == 0)
3850 return SIZE_MAX;
3851#endif
3852
3853 sq_array_size = array_size(sizeof(u32), sq_entries);
3854 if (sq_array_size == SIZE_MAX)
3855 return SIZE_MAX;
3856
3857 if (check_add_overflow(off, sq_array_size, &off))
3858 return SIZE_MAX;
3859
3860 if (sq_offset)
3861 *sq_offset = off;
3862
3863 return off;
3864}
3865
Jens Axboe2b188cc2019-01-07 10:46:33 -07003866static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
3867{
Hristo Venev75b28af2019-08-26 17:23:46 +00003868 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003869
Hristo Venev75b28af2019-08-26 17:23:46 +00003870 pages = (size_t)1 << get_order(
3871 rings_size(sq_entries, cq_entries, NULL));
3872 pages += (size_t)1 << get_order(
3873 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07003874
Hristo Venev75b28af2019-08-26 17:23:46 +00003875 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003876}
3877
Jens Axboeedafcce2019-01-09 09:16:05 -07003878static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
3879{
3880 int i, j;
3881
3882 if (!ctx->user_bufs)
3883 return -ENXIO;
3884
3885 for (i = 0; i < ctx->nr_user_bufs; i++) {
3886 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
3887
3888 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbard27c4d3a2019-08-04 19:32:06 -07003889 put_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07003890
3891 if (ctx->account_mem)
3892 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003893 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07003894 imu->nr_bvecs = 0;
3895 }
3896
3897 kfree(ctx->user_bufs);
3898 ctx->user_bufs = NULL;
3899 ctx->nr_user_bufs = 0;
3900 return 0;
3901}
3902
3903static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
3904 void __user *arg, unsigned index)
3905{
3906 struct iovec __user *src;
3907
3908#ifdef CONFIG_COMPAT
3909 if (ctx->compat) {
3910 struct compat_iovec __user *ciovs;
3911 struct compat_iovec ciov;
3912
3913 ciovs = (struct compat_iovec __user *) arg;
3914 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
3915 return -EFAULT;
3916
3917 dst->iov_base = (void __user *) (unsigned long) ciov.iov_base;
3918 dst->iov_len = ciov.iov_len;
3919 return 0;
3920 }
3921#endif
3922 src = (struct iovec __user *) arg;
3923 if (copy_from_user(dst, &src[index], sizeof(*dst)))
3924 return -EFAULT;
3925 return 0;
3926}
3927
3928static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
3929 unsigned nr_args)
3930{
3931 struct vm_area_struct **vmas = NULL;
3932 struct page **pages = NULL;
3933 int i, j, got_pages = 0;
3934 int ret = -EINVAL;
3935
3936 if (ctx->user_bufs)
3937 return -EBUSY;
3938 if (!nr_args || nr_args > UIO_MAXIOV)
3939 return -EINVAL;
3940
3941 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
3942 GFP_KERNEL);
3943 if (!ctx->user_bufs)
3944 return -ENOMEM;
3945
3946 for (i = 0; i < nr_args; i++) {
3947 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
3948 unsigned long off, start, end, ubuf;
3949 int pret, nr_pages;
3950 struct iovec iov;
3951 size_t size;
3952
3953 ret = io_copy_iov(ctx, &iov, arg, i);
3954 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03003955 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07003956
3957 /*
3958 * Don't impose further limits on the size and buffer
3959 * constraints here, we'll -EINVAL later when IO is
3960 * submitted if they are wrong.
3961 */
3962 ret = -EFAULT;
3963 if (!iov.iov_base || !iov.iov_len)
3964 goto err;
3965
3966 /* arbitrary limit, but we need something */
3967 if (iov.iov_len > SZ_1G)
3968 goto err;
3969
3970 ubuf = (unsigned long) iov.iov_base;
3971 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
3972 start = ubuf >> PAGE_SHIFT;
3973 nr_pages = end - start;
3974
3975 if (ctx->account_mem) {
3976 ret = io_account_mem(ctx->user, nr_pages);
3977 if (ret)
3978 goto err;
3979 }
3980
3981 ret = 0;
3982 if (!pages || nr_pages > got_pages) {
3983 kfree(vmas);
3984 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003985 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07003986 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003987 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07003988 sizeof(struct vm_area_struct *),
3989 GFP_KERNEL);
3990 if (!pages || !vmas) {
3991 ret = -ENOMEM;
3992 if (ctx->account_mem)
3993 io_unaccount_mem(ctx->user, nr_pages);
3994 goto err;
3995 }
3996 got_pages = nr_pages;
3997 }
3998
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003999 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07004000 GFP_KERNEL);
4001 ret = -ENOMEM;
4002 if (!imu->bvec) {
4003 if (ctx->account_mem)
4004 io_unaccount_mem(ctx->user, nr_pages);
4005 goto err;
4006 }
4007
4008 ret = 0;
4009 down_read(&current->mm->mmap_sem);
Ira Weiny932f4a62019-05-13 17:17:03 -07004010 pret = get_user_pages(ubuf, nr_pages,
4011 FOLL_WRITE | FOLL_LONGTERM,
4012 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07004013 if (pret == nr_pages) {
4014 /* don't support file backed memory */
4015 for (j = 0; j < nr_pages; j++) {
4016 struct vm_area_struct *vma = vmas[j];
4017
4018 if (vma->vm_file &&
4019 !is_file_hugepages(vma->vm_file)) {
4020 ret = -EOPNOTSUPP;
4021 break;
4022 }
4023 }
4024 } else {
4025 ret = pret < 0 ? pret : -EFAULT;
4026 }
4027 up_read(&current->mm->mmap_sem);
4028 if (ret) {
4029 /*
4030 * if we did partial map, or found file backed vmas,
4031 * release any pages we did get
4032 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07004033 if (pret > 0)
4034 put_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07004035 if (ctx->account_mem)
4036 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004037 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07004038 goto err;
4039 }
4040
4041 off = ubuf & ~PAGE_MASK;
4042 size = iov.iov_len;
4043 for (j = 0; j < nr_pages; j++) {
4044 size_t vec_len;
4045
4046 vec_len = min_t(size_t, size, PAGE_SIZE - off);
4047 imu->bvec[j].bv_page = pages[j];
4048 imu->bvec[j].bv_len = vec_len;
4049 imu->bvec[j].bv_offset = off;
4050 off = 0;
4051 size -= vec_len;
4052 }
4053 /* store original address for later verification */
4054 imu->ubuf = ubuf;
4055 imu->len = iov.iov_len;
4056 imu->nr_bvecs = nr_pages;
4057
4058 ctx->nr_user_bufs++;
4059 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004060 kvfree(pages);
4061 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07004062 return 0;
4063err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004064 kvfree(pages);
4065 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07004066 io_sqe_buffer_unregister(ctx);
4067 return ret;
4068}
4069
Jens Axboe9b402842019-04-11 11:45:41 -06004070static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
4071{
4072 __s32 __user *fds = arg;
4073 int fd;
4074
4075 if (ctx->cq_ev_fd)
4076 return -EBUSY;
4077
4078 if (copy_from_user(&fd, fds, sizeof(*fds)))
4079 return -EFAULT;
4080
4081 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
4082 if (IS_ERR(ctx->cq_ev_fd)) {
4083 int ret = PTR_ERR(ctx->cq_ev_fd);
4084 ctx->cq_ev_fd = NULL;
4085 return ret;
4086 }
4087
4088 return 0;
4089}
4090
4091static int io_eventfd_unregister(struct io_ring_ctx *ctx)
4092{
4093 if (ctx->cq_ev_fd) {
4094 eventfd_ctx_put(ctx->cq_ev_fd);
4095 ctx->cq_ev_fd = NULL;
4096 return 0;
4097 }
4098
4099 return -ENXIO;
4100}
4101
Jens Axboe2b188cc2019-01-07 10:46:33 -07004102static void io_ring_ctx_free(struct io_ring_ctx *ctx)
4103{
Jens Axboe6b063142019-01-10 22:13:58 -07004104 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004105 if (ctx->sqo_mm)
4106 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07004107
4108 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07004109 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07004110 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06004111 io_eventfd_unregister(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07004112
Jens Axboe2b188cc2019-01-07 10:46:33 -07004113#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07004114 if (ctx->ring_sock) {
4115 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004116 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07004117 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004118#endif
4119
Hristo Venev75b28af2019-08-26 17:23:46 +00004120 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004121 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004122
4123 percpu_ref_exit(&ctx->refs);
4124 if (ctx->account_mem)
4125 io_unaccount_mem(ctx->user,
4126 ring_pages(ctx->sq_entries, ctx->cq_entries));
4127 free_uid(ctx->user);
Jens Axboe206aefd2019-11-07 18:27:42 -07004128 kfree(ctx->completions);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004129 kfree(ctx);
4130}
4131
4132static __poll_t io_uring_poll(struct file *file, poll_table *wait)
4133{
4134 struct io_ring_ctx *ctx = file->private_data;
4135 __poll_t mask = 0;
4136
4137 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02004138 /*
4139 * synchronizes with barrier from wq_has_sleeper call in
4140 * io_commit_cqring
4141 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004142 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00004143 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
4144 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004145 mask |= EPOLLOUT | EPOLLWRNORM;
yangerkundaa5de52019-09-24 20:53:34 +08004146 if (READ_ONCE(ctx->rings->cq.head) != ctx->cached_cq_tail)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004147 mask |= EPOLLIN | EPOLLRDNORM;
4148
4149 return mask;
4150}
4151
4152static int io_uring_fasync(int fd, struct file *file, int on)
4153{
4154 struct io_ring_ctx *ctx = file->private_data;
4155
4156 return fasync_helper(fd, file, on, &ctx->cq_fasync);
4157}
4158
4159static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
4160{
4161 mutex_lock(&ctx->uring_lock);
4162 percpu_ref_kill(&ctx->refs);
4163 mutex_unlock(&ctx->uring_lock);
4164
Jens Axboe5262f562019-09-17 12:26:57 -06004165 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004166 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06004167
4168 if (ctx->io_wq)
4169 io_wq_cancel_all(ctx->io_wq);
4170
Jens Axboedef596e2019-01-09 08:59:42 -07004171 io_iopoll_reap_events(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004172 io_cqring_overflow_flush(ctx, true);
Jens Axboe206aefd2019-11-07 18:27:42 -07004173 wait_for_completion(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004174 io_ring_ctx_free(ctx);
4175}
4176
4177static int io_uring_release(struct inode *inode, struct file *file)
4178{
4179 struct io_ring_ctx *ctx = file->private_data;
4180
4181 file->private_data = NULL;
4182 io_ring_ctx_wait_and_kill(ctx);
4183 return 0;
4184}
4185
Jens Axboefcb323c2019-10-24 12:39:47 -06004186static void io_uring_cancel_files(struct io_ring_ctx *ctx,
4187 struct files_struct *files)
4188{
4189 struct io_kiocb *req;
4190 DEFINE_WAIT(wait);
4191
4192 while (!list_empty_careful(&ctx->inflight_list)) {
4193 enum io_wq_cancel ret = IO_WQ_CANCEL_NOTFOUND;
4194
4195 spin_lock_irq(&ctx->inflight_lock);
4196 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
4197 if (req->work.files == files) {
4198 ret = io_wq_cancel_work(ctx->io_wq, &req->work);
4199 break;
4200 }
4201 }
4202 if (ret == IO_WQ_CANCEL_RUNNING)
4203 prepare_to_wait(&ctx->inflight_wait, &wait,
4204 TASK_UNINTERRUPTIBLE);
4205
4206 spin_unlock_irq(&ctx->inflight_lock);
4207
4208 /*
4209 * We need to keep going until we get NOTFOUND. We only cancel
4210 * one work at the time.
4211 *
4212 * If we get CANCEL_RUNNING, then wait for a work to complete
4213 * before continuing.
4214 */
4215 if (ret == IO_WQ_CANCEL_OK)
4216 continue;
4217 else if (ret != IO_WQ_CANCEL_RUNNING)
4218 break;
4219 schedule();
4220 }
4221}
4222
4223static int io_uring_flush(struct file *file, void *data)
4224{
4225 struct io_ring_ctx *ctx = file->private_data;
4226
4227 io_uring_cancel_files(ctx, data);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004228 if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) {
4229 io_cqring_overflow_flush(ctx, true);
Jens Axboefcb323c2019-10-24 12:39:47 -06004230 io_wq_cancel_all(ctx->io_wq);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004231 }
Jens Axboefcb323c2019-10-24 12:39:47 -06004232 return 0;
4233}
4234
Jens Axboe2b188cc2019-01-07 10:46:33 -07004235static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
4236{
4237 loff_t offset = (loff_t) vma->vm_pgoff << PAGE_SHIFT;
4238 unsigned long sz = vma->vm_end - vma->vm_start;
4239 struct io_ring_ctx *ctx = file->private_data;
4240 unsigned long pfn;
4241 struct page *page;
4242 void *ptr;
4243
4244 switch (offset) {
4245 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00004246 case IORING_OFF_CQ_RING:
4247 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004248 break;
4249 case IORING_OFF_SQES:
4250 ptr = ctx->sq_sqes;
4251 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004252 default:
4253 return -EINVAL;
4254 }
4255
4256 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07004257 if (sz > page_size(page))
Jens Axboe2b188cc2019-01-07 10:46:33 -07004258 return -EINVAL;
4259
4260 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
4261 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
4262}
4263
4264SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
4265 u32, min_complete, u32, flags, const sigset_t __user *, sig,
4266 size_t, sigsz)
4267{
4268 struct io_ring_ctx *ctx;
4269 long ret = -EBADF;
4270 int submitted = 0;
4271 struct fd f;
4272
Jens Axboe6c271ce2019-01-10 11:22:30 -07004273 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07004274 return -EINVAL;
4275
4276 f = fdget(fd);
4277 if (!f.file)
4278 return -EBADF;
4279
4280 ret = -EOPNOTSUPP;
4281 if (f.file->f_op != &io_uring_fops)
4282 goto out_fput;
4283
4284 ret = -ENXIO;
4285 ctx = f.file->private_data;
4286 if (!percpu_ref_tryget(&ctx->refs))
4287 goto out_fput;
4288
Jens Axboe6c271ce2019-01-10 11:22:30 -07004289 /*
4290 * For SQ polling, the thread will do all submissions and completions.
4291 * Just return the requested submit count, and wake the thread if
4292 * we were asked to.
4293 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06004294 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004295 if (ctx->flags & IORING_SETUP_SQPOLL) {
4296 if (flags & IORING_ENTER_SQ_WAKEUP)
4297 wake_up(&ctx->sqo_wait);
4298 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06004299 } else if (to_submit) {
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004300 struct mm_struct *cur_mm;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004301
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004302 to_submit = min(to_submit, ctx->sq_entries);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004303 mutex_lock(&ctx->uring_lock);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004304 /* already have mm, so io_submit_sqes() won't try to grab it */
4305 cur_mm = ctx->sqo_mm;
4306 submitted = io_submit_sqes(ctx, to_submit, f.file, fd,
4307 &cur_mm, false);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004308 mutex_unlock(&ctx->uring_lock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004309 }
4310 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07004311 unsigned nr_events = 0;
4312
Jens Axboe2b188cc2019-01-07 10:46:33 -07004313 min_complete = min(min_complete, ctx->cq_entries);
4314
Jens Axboedef596e2019-01-09 08:59:42 -07004315 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07004316 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07004317 } else {
4318 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
4319 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004320 }
4321
Pavel Begunkov6805b322019-10-08 02:18:42 +03004322 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004323out_fput:
4324 fdput(f);
4325 return submitted ? submitted : ret;
4326}
4327
4328static const struct file_operations io_uring_fops = {
4329 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06004330 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07004331 .mmap = io_uring_mmap,
4332 .poll = io_uring_poll,
4333 .fasync = io_uring_fasync,
4334};
4335
4336static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
4337 struct io_uring_params *p)
4338{
Hristo Venev75b28af2019-08-26 17:23:46 +00004339 struct io_rings *rings;
4340 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004341
Hristo Venev75b28af2019-08-26 17:23:46 +00004342 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
4343 if (size == SIZE_MAX)
4344 return -EOVERFLOW;
4345
4346 rings = io_mem_alloc(size);
4347 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004348 return -ENOMEM;
4349
Hristo Venev75b28af2019-08-26 17:23:46 +00004350 ctx->rings = rings;
4351 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
4352 rings->sq_ring_mask = p->sq_entries - 1;
4353 rings->cq_ring_mask = p->cq_entries - 1;
4354 rings->sq_ring_entries = p->sq_entries;
4355 rings->cq_ring_entries = p->cq_entries;
4356 ctx->sq_mask = rings->sq_ring_mask;
4357 ctx->cq_mask = rings->cq_ring_mask;
4358 ctx->sq_entries = rings->sq_ring_entries;
4359 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004360
4361 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
4362 if (size == SIZE_MAX)
4363 return -EOVERFLOW;
4364
4365 ctx->sq_sqes = io_mem_alloc(size);
Mark Rutland52e04ef2019-04-30 17:30:21 +01004366 if (!ctx->sq_sqes)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004367 return -ENOMEM;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004368
Jens Axboe2b188cc2019-01-07 10:46:33 -07004369 return 0;
4370}
4371
4372/*
4373 * Allocate an anonymous fd, this is what constitutes the application
4374 * visible backing of an io_uring instance. The application mmaps this
4375 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
4376 * we have to tie this fd to a socket for file garbage collection purposes.
4377 */
4378static int io_uring_get_fd(struct io_ring_ctx *ctx)
4379{
4380 struct file *file;
4381 int ret;
4382
4383#if defined(CONFIG_UNIX)
4384 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
4385 &ctx->ring_sock);
4386 if (ret)
4387 return ret;
4388#endif
4389
4390 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
4391 if (ret < 0)
4392 goto err;
4393
4394 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
4395 O_RDWR | O_CLOEXEC);
4396 if (IS_ERR(file)) {
4397 put_unused_fd(ret);
4398 ret = PTR_ERR(file);
4399 goto err;
4400 }
4401
4402#if defined(CONFIG_UNIX)
4403 ctx->ring_sock->file = file;
Jens Axboe6b063142019-01-10 22:13:58 -07004404 ctx->ring_sock->sk->sk_user_data = ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004405#endif
4406 fd_install(ret, file);
4407 return ret;
4408err:
4409#if defined(CONFIG_UNIX)
4410 sock_release(ctx->ring_sock);
4411 ctx->ring_sock = NULL;
4412#endif
4413 return ret;
4414}
4415
4416static int io_uring_create(unsigned entries, struct io_uring_params *p)
4417{
4418 struct user_struct *user = NULL;
4419 struct io_ring_ctx *ctx;
4420 bool account_mem;
4421 int ret;
4422
4423 if (!entries || entries > IORING_MAX_ENTRIES)
4424 return -EINVAL;
4425
4426 /*
4427 * Use twice as many entries for the CQ ring. It's possible for the
4428 * application to drive a higher depth than the size of the SQ ring,
4429 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06004430 * some flexibility in overcommitting a bit. If the application has
4431 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
4432 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07004433 */
4434 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06004435 if (p->flags & IORING_SETUP_CQSIZE) {
4436 /*
4437 * If IORING_SETUP_CQSIZE is set, we do the same roundup
4438 * to a power-of-two, if it isn't already. We do NOT impose
4439 * any cq vs sq ring sizing.
4440 */
4441 if (p->cq_entries < p->sq_entries || p->cq_entries > IORING_MAX_CQ_ENTRIES)
4442 return -EINVAL;
4443 p->cq_entries = roundup_pow_of_two(p->cq_entries);
4444 } else {
4445 p->cq_entries = 2 * p->sq_entries;
4446 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004447
4448 user = get_uid(current_user());
4449 account_mem = !capable(CAP_IPC_LOCK);
4450
4451 if (account_mem) {
4452 ret = io_account_mem(user,
4453 ring_pages(p->sq_entries, p->cq_entries));
4454 if (ret) {
4455 free_uid(user);
4456 return ret;
4457 }
4458 }
4459
4460 ctx = io_ring_ctx_alloc(p);
4461 if (!ctx) {
4462 if (account_mem)
4463 io_unaccount_mem(user, ring_pages(p->sq_entries,
4464 p->cq_entries));
4465 free_uid(user);
4466 return -ENOMEM;
4467 }
4468 ctx->compat = in_compat_syscall();
4469 ctx->account_mem = account_mem;
4470 ctx->user = user;
4471
4472 ret = io_allocate_scq_urings(ctx, p);
4473 if (ret)
4474 goto err;
4475
Jens Axboe6c271ce2019-01-10 11:22:30 -07004476 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004477 if (ret)
4478 goto err;
4479
Jens Axboe2b188cc2019-01-07 10:46:33 -07004480 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00004481 p->sq_off.head = offsetof(struct io_rings, sq.head);
4482 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
4483 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
4484 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
4485 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
4486 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
4487 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004488
4489 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00004490 p->cq_off.head = offsetof(struct io_rings, cq.head);
4491 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
4492 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
4493 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
4494 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
4495 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06004496
Jens Axboe044c1ab2019-10-28 09:15:33 -06004497 /*
4498 * Install ring fd as the very last thing, so we don't risk someone
4499 * having closed it before we finish setup
4500 */
4501 ret = io_uring_get_fd(ctx);
4502 if (ret < 0)
4503 goto err;
4504
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004505 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004506 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004507 return ret;
4508err:
4509 io_ring_ctx_wait_and_kill(ctx);
4510 return ret;
4511}
4512
4513/*
4514 * Sets up an aio uring context, and returns the fd. Applications asks for a
4515 * ring size, we return the actual sq/cq ring sizes (among other things) in the
4516 * params structure passed in.
4517 */
4518static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
4519{
4520 struct io_uring_params p;
4521 long ret;
4522 int i;
4523
4524 if (copy_from_user(&p, params, sizeof(p)))
4525 return -EFAULT;
4526 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
4527 if (p.resv[i])
4528 return -EINVAL;
4529 }
4530
Jens Axboe6c271ce2019-01-10 11:22:30 -07004531 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe33a107f2019-10-04 12:10:03 -06004532 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE))
Jens Axboe2b188cc2019-01-07 10:46:33 -07004533 return -EINVAL;
4534
4535 ret = io_uring_create(entries, &p);
4536 if (ret < 0)
4537 return ret;
4538
4539 if (copy_to_user(params, &p, sizeof(p)))
4540 return -EFAULT;
4541
4542 return ret;
4543}
4544
4545SYSCALL_DEFINE2(io_uring_setup, u32, entries,
4546 struct io_uring_params __user *, params)
4547{
4548 return io_uring_setup(entries, params);
4549}
4550
Jens Axboeedafcce2019-01-09 09:16:05 -07004551static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
4552 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06004553 __releases(ctx->uring_lock)
4554 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07004555{
4556 int ret;
4557
Jens Axboe35fa71a2019-04-22 10:23:23 -06004558 /*
4559 * We're inside the ring mutex, if the ref is already dying, then
4560 * someone else killed the ctx or is already going through
4561 * io_uring_register().
4562 */
4563 if (percpu_ref_is_dying(&ctx->refs))
4564 return -ENXIO;
4565
Jens Axboeedafcce2019-01-09 09:16:05 -07004566 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06004567
4568 /*
4569 * Drop uring mutex before waiting for references to exit. If another
4570 * thread is currently inside io_uring_enter() it might need to grab
4571 * the uring_lock to make progress. If we hold it here across the drain
4572 * wait, then we can deadlock. It's safe to drop the mutex here, since
4573 * no new references will come in after we've killed the percpu ref.
4574 */
4575 mutex_unlock(&ctx->uring_lock);
Jens Axboe206aefd2019-11-07 18:27:42 -07004576 wait_for_completion(&ctx->completions[0]);
Jens Axboeb19062a2019-04-15 10:49:38 -06004577 mutex_lock(&ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07004578
4579 switch (opcode) {
4580 case IORING_REGISTER_BUFFERS:
4581 ret = io_sqe_buffer_register(ctx, arg, nr_args);
4582 break;
4583 case IORING_UNREGISTER_BUFFERS:
4584 ret = -EINVAL;
4585 if (arg || nr_args)
4586 break;
4587 ret = io_sqe_buffer_unregister(ctx);
4588 break;
Jens Axboe6b063142019-01-10 22:13:58 -07004589 case IORING_REGISTER_FILES:
4590 ret = io_sqe_files_register(ctx, arg, nr_args);
4591 break;
4592 case IORING_UNREGISTER_FILES:
4593 ret = -EINVAL;
4594 if (arg || nr_args)
4595 break;
4596 ret = io_sqe_files_unregister(ctx);
4597 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06004598 case IORING_REGISTER_FILES_UPDATE:
4599 ret = io_sqe_files_update(ctx, arg, nr_args);
4600 break;
Jens Axboe9b402842019-04-11 11:45:41 -06004601 case IORING_REGISTER_EVENTFD:
4602 ret = -EINVAL;
4603 if (nr_args != 1)
4604 break;
4605 ret = io_eventfd_register(ctx, arg);
4606 break;
4607 case IORING_UNREGISTER_EVENTFD:
4608 ret = -EINVAL;
4609 if (arg || nr_args)
4610 break;
4611 ret = io_eventfd_unregister(ctx);
4612 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07004613 default:
4614 ret = -EINVAL;
4615 break;
4616 }
4617
4618 /* bring the ctx back to life */
Jens Axboe206aefd2019-11-07 18:27:42 -07004619 reinit_completion(&ctx->completions[0]);
Jens Axboeedafcce2019-01-09 09:16:05 -07004620 percpu_ref_reinit(&ctx->refs);
4621 return ret;
4622}
4623
4624SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
4625 void __user *, arg, unsigned int, nr_args)
4626{
4627 struct io_ring_ctx *ctx;
4628 long ret = -EBADF;
4629 struct fd f;
4630
4631 f = fdget(fd);
4632 if (!f.file)
4633 return -EBADF;
4634
4635 ret = -EOPNOTSUPP;
4636 if (f.file->f_op != &io_uring_fops)
4637 goto out_fput;
4638
4639 ctx = f.file->private_data;
4640
4641 mutex_lock(&ctx->uring_lock);
4642 ret = __io_uring_register(ctx, opcode, arg, nr_args);
4643 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004644 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
4645 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07004646out_fput:
4647 fdput(f);
4648 return ret;
4649}
4650
Jens Axboe2b188cc2019-01-07 10:46:33 -07004651static int __init io_uring_init(void)
4652{
4653 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
4654 return 0;
4655};
4656__initcall(io_uring_init);