blob: 02a4f5e8a6e4bb6d303740ef7227f4b6c92a564b [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 Axboe2b188cc2019-01-07 10:46:33 -0700207 struct io_uring_sqe *sq_sqes;
Jens Axboede0617e2019-04-06 21:51:27 -0600208
209 struct list_head defer_list;
Jens Axboe5262f562019-09-17 12:26:57 -0600210 struct list_head timeout_list;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700211 struct list_head cq_overflow_list;
Jens Axboefcb323c2019-10-24 12:39:47 -0600212
213 wait_queue_head_t inflight_wait;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700214 } ____cacheline_aligned_in_smp;
215
216 /* IO offload */
Jens Axboe561fb042019-10-24 07:25:42 -0600217 struct io_wq *io_wq;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700218 struct task_struct *sqo_thread; /* if using sq thread polling */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700219 struct mm_struct *sqo_mm;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700220 wait_queue_head_t sqo_wait;
Jackie Liua4c0b3d2019-07-08 13:41:12 +0800221 struct completion sqo_thread_started;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700222
223 struct {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700224 unsigned cached_cq_tail;
Jens Axboe498ccd92019-10-25 10:04:25 -0600225 atomic_t cached_cq_overflow;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700226 unsigned cq_entries;
227 unsigned cq_mask;
228 struct wait_queue_head cq_wait;
229 struct fasync_struct *cq_fasync;
Jens Axboe9b402842019-04-11 11:45:41 -0600230 struct eventfd_ctx *cq_ev_fd;
Jens Axboe5262f562019-09-17 12:26:57 -0600231 atomic_t cq_timeouts;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700232 } ____cacheline_aligned_in_smp;
233
Hristo Venev75b28af2019-08-26 17:23:46 +0000234 struct io_rings *rings;
235
Jens Axboe6b063142019-01-10 22:13:58 -0700236 /*
237 * If used, fixed file set. Writers must ensure that ->refs is dead,
238 * readers must ensure that ->refs is alive as long as the file* is
239 * used. Only updated through io_uring_register(2).
240 */
Jens Axboe65e19f52019-10-26 07:20:21 -0600241 struct fixed_file_table *file_table;
Jens Axboe6b063142019-01-10 22:13:58 -0700242 unsigned nr_user_files;
243
Jens Axboeedafcce2019-01-09 09:16:05 -0700244 /* if used, fixed mapped user buffers */
245 unsigned nr_user_bufs;
246 struct io_mapped_ubuf *user_bufs;
247
Jens Axboe2b188cc2019-01-07 10:46:33 -0700248 struct user_struct *user;
249
250 struct completion ctx_done;
251
252 struct {
253 struct mutex uring_lock;
254 wait_queue_head_t wait;
255 } ____cacheline_aligned_in_smp;
256
257 struct {
258 spinlock_t completion_lock;
Jens Axboedef596e2019-01-09 08:59:42 -0700259 bool poll_multi_file;
260 /*
261 * ->poll_list is protected by the ctx->uring_lock for
262 * io_uring instances that don't use IORING_SETUP_SQPOLL.
263 * For SQPOLL, only the single threaded io_sq_thread() will
264 * manipulate the list, hence no extra locking is needed there.
265 */
266 struct list_head poll_list;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700267 struct list_head cancel_list;
Jens Axboefcb323c2019-10-24 12:39:47 -0600268
269 spinlock_t inflight_lock;
270 struct list_head inflight_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700271 } ____cacheline_aligned_in_smp;
272
273#if defined(CONFIG_UNIX)
274 struct socket *ring_sock;
275#endif
276};
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);
Jens Axboe2665abf2019-11-05 12:40:47 -0700376static void io_put_req(struct io_kiocb *req, struct io_kiocb **nxtptr);
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
400 complete(&ctx->ctx_done);
401}
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
Roman Gushchin21482892019-05-07 10:01:48 -0700411 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
412 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700413 kfree(ctx);
414 return NULL;
415 }
416
417 ctx->flags = p->flags;
418 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700419 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700420 init_completion(&ctx->ctx_done);
Jackie Liua4c0b3d2019-07-08 13:41:12 +0800421 init_completion(&ctx->sqo_thread_started);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700422 mutex_init(&ctx->uring_lock);
423 init_waitqueue_head(&ctx->wait);
424 spin_lock_init(&ctx->completion_lock);
Jens Axboedef596e2019-01-09 08:59:42 -0700425 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboe221c5eb2019-01-17 09:41:58 -0700426 INIT_LIST_HEAD(&ctx->cancel_list);
Jens Axboede0617e2019-04-06 21:51:27 -0600427 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -0600428 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -0600429 init_waitqueue_head(&ctx->inflight_wait);
430 spin_lock_init(&ctx->inflight_lock);
431 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700432 return ctx;
433}
434
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600435static inline bool __io_sequence_defer(struct io_ring_ctx *ctx,
436 struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -0600437{
Jens Axboe498ccd92019-10-25 10:04:25 -0600438 return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped
439 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboede0617e2019-04-06 21:51:27 -0600440}
441
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600442static inline bool io_sequence_defer(struct io_ring_ctx *ctx,
443 struct io_kiocb *req)
444{
445 if ((req->flags & (REQ_F_IO_DRAIN|REQ_F_IO_DRAINED)) != REQ_F_IO_DRAIN)
446 return false;
447
448 return __io_sequence_defer(ctx, req);
449}
450
451static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -0600452{
453 struct io_kiocb *req;
454
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600455 req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list);
456 if (req && !io_sequence_defer(ctx, req)) {
Jens Axboede0617e2019-04-06 21:51:27 -0600457 list_del_init(&req->list);
458 return req;
459 }
460
461 return NULL;
462}
463
Jens Axboe5262f562019-09-17 12:26:57 -0600464static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx)
465{
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600466 struct io_kiocb *req;
467
468 req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list);
469 if (req && !__io_sequence_defer(ctx, req)) {
470 list_del_init(&req->list);
471 return req;
472 }
473
474 return NULL;
Jens Axboe5262f562019-09-17 12:26:57 -0600475}
476
Jens Axboede0617e2019-04-06 21:51:27 -0600477static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700478{
Hristo Venev75b28af2019-08-26 17:23:46 +0000479 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700480
Hristo Venev75b28af2019-08-26 17:23:46 +0000481 if (ctx->cached_cq_tail != READ_ONCE(rings->cq.tail)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700482 /* order cqe stores with ring update */
Hristo Venev75b28af2019-08-26 17:23:46 +0000483 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700484
Jens Axboe2b188cc2019-01-07 10:46:33 -0700485 if (wq_has_sleeper(&ctx->cq_wait)) {
486 wake_up_interruptible(&ctx->cq_wait);
487 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
488 }
489 }
490}
491
Jens Axboe561fb042019-10-24 07:25:42 -0600492static inline bool io_sqe_needs_user(const struct io_uring_sqe *sqe)
Jens Axboe18d9be12019-09-10 09:13:05 -0600493{
Jens Axboe561fb042019-10-24 07:25:42 -0600494 u8 opcode = READ_ONCE(sqe->opcode);
495
496 return !(opcode == IORING_OP_READ_FIXED ||
497 opcode == IORING_OP_WRITE_FIXED);
498}
499
500static inline bool io_prep_async_work(struct io_kiocb *req)
501{
502 bool do_hashed = false;
Jens Axboe54a91f32019-09-10 09:15:04 -0600503
Jens Axboe6cc47d12019-09-18 11:18:23 -0600504 if (req->submit.sqe) {
505 switch (req->submit.sqe->opcode) {
506 case IORING_OP_WRITEV:
507 case IORING_OP_WRITE_FIXED:
Jens Axboe561fb042019-10-24 07:25:42 -0600508 do_hashed = true;
Jens Axboe5f8fd2d32019-11-07 10:57:36 -0700509 /* fall-through */
510 case IORING_OP_READV:
511 case IORING_OP_READ_FIXED:
512 case IORING_OP_SENDMSG:
513 case IORING_OP_RECVMSG:
514 case IORING_OP_ACCEPT:
515 case IORING_OP_POLL_ADD:
516 /*
517 * We know REQ_F_ISREG is not set on some of these
518 * opcodes, but this enables us to keep the check in
519 * just one place.
520 */
521 if (!(req->flags & REQ_F_ISREG))
522 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe6cc47d12019-09-18 11:18:23 -0600523 break;
524 }
Jens Axboe561fb042019-10-24 07:25:42 -0600525 if (io_sqe_needs_user(req->submit.sqe))
526 req->work.flags |= IO_WQ_WORK_NEEDS_USER;
Jens Axboe54a91f32019-09-10 09:15:04 -0600527 }
528
Jens Axboe561fb042019-10-24 07:25:42 -0600529 return do_hashed;
530}
531
532static inline void io_queue_async_work(struct io_ring_ctx *ctx,
533 struct io_kiocb *req)
534{
535 bool do_hashed = io_prep_async_work(req);
536
537 trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work,
538 req->flags);
539 if (!do_hashed) {
540 io_wq_enqueue(ctx->io_wq, &req->work);
541 } else {
542 io_wq_enqueue_hashed(ctx->io_wq, &req->work,
543 file_inode(req->file));
544 }
Jens Axboe18d9be12019-09-10 09:13:05 -0600545}
546
Jens Axboe5262f562019-09-17 12:26:57 -0600547static void io_kill_timeout(struct io_kiocb *req)
548{
549 int ret;
550
551 ret = hrtimer_try_to_cancel(&req->timeout.timer);
552 if (ret != -1) {
553 atomic_inc(&req->ctx->cq_timeouts);
Jens Axboe842f9612019-10-29 12:34:10 -0600554 list_del_init(&req->list);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700555 io_cqring_fill_event(req, 0);
556 io_put_req(req, NULL);
Jens Axboe5262f562019-09-17 12:26:57 -0600557 }
558}
559
560static void io_kill_timeouts(struct io_ring_ctx *ctx)
561{
562 struct io_kiocb *req, *tmp;
563
564 spin_lock_irq(&ctx->completion_lock);
565 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
566 io_kill_timeout(req);
567 spin_unlock_irq(&ctx->completion_lock);
568}
569
Jens Axboede0617e2019-04-06 21:51:27 -0600570static void io_commit_cqring(struct io_ring_ctx *ctx)
571{
572 struct io_kiocb *req;
573
Jens Axboe5262f562019-09-17 12:26:57 -0600574 while ((req = io_get_timeout_req(ctx)) != NULL)
575 io_kill_timeout(req);
576
Jens Axboede0617e2019-04-06 21:51:27 -0600577 __io_commit_cqring(ctx);
578
579 while ((req = io_get_deferred_req(ctx)) != NULL) {
Jackie Liu4fe2c962019-09-09 20:50:40 +0800580 if (req->flags & REQ_F_SHADOW_DRAIN) {
581 /* Just for drain, free it. */
582 __io_free_req(req);
583 continue;
584 }
Jens Axboede0617e2019-04-06 21:51:27 -0600585 req->flags |= REQ_F_IO_DRAINED;
Jens Axboe18d9be12019-09-10 09:13:05 -0600586 io_queue_async_work(ctx, req);
Jens Axboede0617e2019-04-06 21:51:27 -0600587 }
588}
589
Jens Axboe2b188cc2019-01-07 10:46:33 -0700590static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
591{
Hristo Venev75b28af2019-08-26 17:23:46 +0000592 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700593 unsigned tail;
594
595 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +0200596 /*
597 * writes to the cq entry need to come after reading head; the
598 * control dependency is enough as we're using WRITE_ONCE to
599 * fill the cq entry
600 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000601 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700602 return NULL;
603
604 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +0000605 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -0700606}
607
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700608static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
609{
610 if (waitqueue_active(&ctx->wait))
611 wake_up(&ctx->wait);
612 if (waitqueue_active(&ctx->sqo_wait))
613 wake_up(&ctx->sqo_wait);
614 if (ctx->cq_ev_fd)
615 eventfd_signal(ctx->cq_ev_fd, 1);
616}
617
618static void io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
619{
620 struct io_rings *rings = ctx->rings;
621 struct io_uring_cqe *cqe;
622 struct io_kiocb *req;
623 unsigned long flags;
624 LIST_HEAD(list);
625
626 if (!force) {
627 if (list_empty_careful(&ctx->cq_overflow_list))
628 return;
629 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
630 rings->cq_ring_entries))
631 return;
632 }
633
634 spin_lock_irqsave(&ctx->completion_lock, flags);
635
636 /* if force is set, the ring is going away. always drop after that */
637 if (force)
638 ctx->cq_overflow_flushed = true;
639
640 while (!list_empty(&ctx->cq_overflow_list)) {
641 cqe = io_get_cqring(ctx);
642 if (!cqe && !force)
643 break;
644
645 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
646 list);
647 list_move(&req->list, &list);
648 if (cqe) {
649 WRITE_ONCE(cqe->user_data, req->user_data);
650 WRITE_ONCE(cqe->res, req->result);
651 WRITE_ONCE(cqe->flags, 0);
652 } else {
653 WRITE_ONCE(ctx->rings->cq_overflow,
654 atomic_inc_return(&ctx->cached_cq_overflow));
655 }
656 }
657
658 io_commit_cqring(ctx);
659 spin_unlock_irqrestore(&ctx->completion_lock, flags);
660 io_cqring_ev_posted(ctx);
661
662 while (!list_empty(&list)) {
663 req = list_first_entry(&list, struct io_kiocb, list);
664 list_del(&req->list);
665 io_put_req(req, NULL);
666 }
667}
668
Jens Axboe78e19bb2019-11-06 15:21:34 -0700669static void io_cqring_fill_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700670{
Jens Axboe78e19bb2019-11-06 15:21:34 -0700671 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700672 struct io_uring_cqe *cqe;
673
Jens Axboe78e19bb2019-11-06 15:21:34 -0700674 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -0700675
Jens Axboe2b188cc2019-01-07 10:46:33 -0700676 /*
677 * If we can't get a cq entry, userspace overflowed the
678 * submission (by quite a lot). Increment the overflow count in
679 * the ring.
680 */
681 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700682 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -0700683 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700684 WRITE_ONCE(cqe->res, res);
Jens Axboec71ffb62019-05-13 20:58:29 -0600685 WRITE_ONCE(cqe->flags, 0);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700686 } else if (ctx->cq_overflow_flushed) {
Jens Axboe498ccd92019-10-25 10:04:25 -0600687 WRITE_ONCE(ctx->rings->cq_overflow,
688 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700689 } else {
690 refcount_inc(&req->refs);
691 req->result = res;
692 list_add_tail(&req->list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700693 }
694}
695
Jens Axboe78e19bb2019-11-06 15:21:34 -0700696static void io_cqring_add_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700697{
Jens Axboe78e19bb2019-11-06 15:21:34 -0700698 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700699 unsigned long flags;
700
701 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700702 io_cqring_fill_event(req, res);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700703 io_commit_cqring(ctx);
704 spin_unlock_irqrestore(&ctx->completion_lock, flags);
705
Jens Axboe8c838782019-03-12 15:48:16 -0600706 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700707}
708
Jens Axboe2579f912019-01-09 09:10:43 -0700709static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
710 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700711{
Jens Axboefd6fab22019-03-14 16:30:06 -0600712 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700713 struct io_kiocb *req;
714
715 if (!percpu_ref_tryget(&ctx->refs))
716 return NULL;
717
Jens Axboe2579f912019-01-09 09:10:43 -0700718 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -0600719 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -0700720 if (unlikely(!req))
721 goto out;
722 } else if (!state->free_reqs) {
723 size_t sz;
724 int ret;
725
726 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -0600727 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
728
729 /*
730 * Bulk alloc is all-or-nothing. If we fail to get a batch,
731 * retry single alloc to be on the safe side.
732 */
733 if (unlikely(ret <= 0)) {
734 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
735 if (!state->reqs[0])
736 goto out;
737 ret = 1;
738 }
Jens Axboe2579f912019-01-09 09:10:43 -0700739 state->free_reqs = ret - 1;
740 state->cur_req = 1;
741 req = state->reqs[0];
742 } else {
743 req = state->reqs[state->cur_req];
744 state->free_reqs--;
745 state->cur_req++;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700746 }
747
Jens Axboe60c112b2019-06-21 10:20:18 -0600748 req->file = NULL;
Jens Axboe2579f912019-01-09 09:10:43 -0700749 req->ctx = ctx;
750 req->flags = 0;
Jens Axboee65ef562019-03-12 10:16:44 -0600751 /* one is dropped after submission, the other at completion */
752 refcount_set(&req->refs, 2);
Jens Axboe9e645e112019-05-10 16:07:28 -0600753 req->result = 0;
Jens Axboe561fb042019-10-24 07:25:42 -0600754 INIT_IO_WORK(&req->work, io_wq_submit_work);
Jens Axboe2579f912019-01-09 09:10:43 -0700755 return req;
756out:
Pavel Begunkov6805b322019-10-08 02:18:42 +0300757 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700758 return NULL;
759}
760
Jens Axboedef596e2019-01-09 08:59:42 -0700761static void io_free_req_many(struct io_ring_ctx *ctx, void **reqs, int *nr)
762{
763 if (*nr) {
764 kmem_cache_free_bulk(req_cachep, *nr, reqs);
Pavel Begunkov6805b322019-10-08 02:18:42 +0300765 percpu_ref_put_many(&ctx->refs, *nr);
Jens Axboedef596e2019-01-09 08:59:42 -0700766 *nr = 0;
767 }
768}
769
Jens Axboe9e645e112019-05-10 16:07:28 -0600770static void __io_free_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700771{
Jens Axboefcb323c2019-10-24 12:39:47 -0600772 struct io_ring_ctx *ctx = req->ctx;
773
Jens Axboe09bb8392019-03-13 12:39:28 -0600774 if (req->file && !(req->flags & REQ_F_FIXED_FILE))
775 fput(req->file);
Jens Axboefcb323c2019-10-24 12:39:47 -0600776 if (req->flags & REQ_F_INFLIGHT) {
777 unsigned long flags;
778
779 spin_lock_irqsave(&ctx->inflight_lock, flags);
780 list_del(&req->inflight_entry);
781 if (waitqueue_active(&ctx->inflight_wait))
782 wake_up(&ctx->inflight_wait);
783 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
784 }
785 percpu_ref_put(&ctx->refs);
Jens Axboee65ef562019-03-12 10:16:44 -0600786 kmem_cache_free(req_cachep, req);
787}
788
Jens Axboe2665abf2019-11-05 12:40:47 -0700789static bool io_link_cancel_timeout(struct io_ring_ctx *ctx,
790 struct io_kiocb *req)
791{
792 int ret;
793
794 ret = hrtimer_try_to_cancel(&req->timeout.timer);
795 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -0700796 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -0700797 io_commit_cqring(ctx);
798 req->flags &= ~REQ_F_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -0700799 io_put_req(req, NULL);
Jens Axboe2665abf2019-11-05 12:40:47 -0700800 return true;
801 }
802
803 return false;
804}
805
Jens Axboeba816ad2019-09-28 11:36:45 -0600806static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -0600807{
Jens Axboe2665abf2019-11-05 12:40:47 -0700808 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -0600809 struct io_kiocb *nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -0700810 bool wake_ev = false;
Jens Axboe9e645e112019-05-10 16:07:28 -0600811
812 /*
813 * The list should never be empty when we are called here. But could
814 * potentially happen if the chain is messed up, check to be on the
815 * safe side.
816 */
817 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, list);
Jens Axboe2665abf2019-11-05 12:40:47 -0700818 while (nxt) {
Jens Axboe9e645e112019-05-10 16:07:28 -0600819 list_del(&nxt->list);
820 if (!list_empty(&req->link_list)) {
821 INIT_LIST_HEAD(&nxt->link_list);
822 list_splice(&req->link_list, &nxt->link_list);
823 nxt->flags |= REQ_F_LINK;
824 }
825
Jens Axboeba816ad2019-09-28 11:36:45 -0600826 /*
827 * If we're in async work, we can continue processing the chain
828 * in this context instead of having to queue up new async work.
829 */
Jens Axboe2665abf2019-11-05 12:40:47 -0700830 if (req->flags & REQ_F_LINK_TIMEOUT) {
831 wake_ev = io_link_cancel_timeout(ctx, nxt);
832
833 /* we dropped this link, get next */
834 nxt = list_first_entry_or_null(&req->link_list,
835 struct io_kiocb, list);
836 } else if (nxtptr && current_work()) {
Jens Axboeba816ad2019-09-28 11:36:45 -0600837 *nxtptr = nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -0700838 break;
839 } else {
Jens Axboeba816ad2019-09-28 11:36:45 -0600840 io_queue_async_work(req->ctx, nxt);
Jens Axboe2665abf2019-11-05 12:40:47 -0700841 break;
842 }
Jens Axboe9e645e112019-05-10 16:07:28 -0600843 }
Jens Axboe2665abf2019-11-05 12:40:47 -0700844
845 if (wake_ev)
846 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -0600847}
848
849/*
850 * Called if REQ_F_LINK is set, and we fail the head request
851 */
852static void io_fail_links(struct io_kiocb *req)
853{
Jens Axboe2665abf2019-11-05 12:40:47 -0700854 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -0600855 struct io_kiocb *link;
Jens Axboe2665abf2019-11-05 12:40:47 -0700856 unsigned long flags;
857
858 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -0600859
860 while (!list_empty(&req->link_list)) {
861 link = list_first_entry(&req->link_list, struct io_kiocb, list);
Jens Axboe2665abf2019-11-05 12:40:47 -0700862 list_del_init(&link->list);
Jens Axboe9e645e112019-05-10 16:07:28 -0600863
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +0200864 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -0700865
866 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
867 link->submit.sqe->opcode == IORING_OP_LINK_TIMEOUT) {
868 io_link_cancel_timeout(ctx, link);
869 } else {
Jens Axboe78e19bb2019-11-06 15:21:34 -0700870 io_cqring_fill_event(link, -ECANCELED);
871 io_double_put_req(link);
Jens Axboe2665abf2019-11-05 12:40:47 -0700872 }
Jens Axboe9e645e112019-05-10 16:07:28 -0600873 }
Jens Axboe2665abf2019-11-05 12:40:47 -0700874
875 io_commit_cqring(ctx);
876 spin_unlock_irqrestore(&ctx->completion_lock, flags);
877 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -0600878}
879
Jens Axboeba816ad2019-09-28 11:36:45 -0600880static void io_free_req(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -0600881{
Jens Axboe2665abf2019-11-05 12:40:47 -0700882 if (likely(!(req->flags & REQ_F_LINK))) {
883 __io_free_req(req);
884 return;
885 }
886
Jens Axboe9e645e112019-05-10 16:07:28 -0600887 /*
888 * If LINK is set, we have dependent requests in this chain. If we
889 * didn't fail this request, queue the first one up, moving any other
890 * dependencies to the next request. In case of failure, fail the rest
891 * of the chain.
892 */
Jens Axboe2665abf2019-11-05 12:40:47 -0700893 if (req->flags & REQ_F_FAIL_LINK) {
894 io_fail_links(req);
895 } else if (req->flags & REQ_F_LINK_TIMEOUT) {
896 struct io_ring_ctx *ctx = req->ctx;
897 unsigned long flags;
898
899 /*
900 * If this is a timeout link, we could be racing with the
901 * timeout timer. Grab the completion lock for this case to
902 * protection against that.
903 */
904 spin_lock_irqsave(&ctx->completion_lock, flags);
905 io_req_link_next(req, nxt);
906 spin_unlock_irqrestore(&ctx->completion_lock, flags);
907 } else {
908 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -0600909 }
910
911 __io_free_req(req);
912}
913
Jens Axboeba816ad2019-09-28 11:36:45 -0600914/*
915 * Drop reference to request, return next in chain (if there is one) if this
916 * was the last reference to this request.
917 */
918static struct io_kiocb *io_put_req_find_next(struct io_kiocb *req)
Jens Axboee65ef562019-03-12 10:16:44 -0600919{
Jens Axboeba816ad2019-09-28 11:36:45 -0600920 struct io_kiocb *nxt = NULL;
921
Jens Axboee65ef562019-03-12 10:16:44 -0600922 if (refcount_dec_and_test(&req->refs))
Jens Axboeba816ad2019-09-28 11:36:45 -0600923 io_free_req(req, &nxt);
924
925 return nxt;
926}
927
928static void io_put_req(struct io_kiocb *req, struct io_kiocb **nxtptr)
929{
930 struct io_kiocb *nxt;
931
932 nxt = io_put_req_find_next(req);
933 if (nxt) {
Jens Axboe561fb042019-10-24 07:25:42 -0600934 if (nxtptr)
Jens Axboeba816ad2019-09-28 11:36:45 -0600935 *nxtptr = nxt;
Jens Axboe561fb042019-10-24 07:25:42 -0600936 else
Jens Axboeba816ad2019-09-28 11:36:45 -0600937 io_queue_async_work(nxt->ctx, nxt);
Jens Axboeba816ad2019-09-28 11:36:45 -0600938 }
Jens Axboe2b188cc2019-01-07 10:46:33 -0700939}
940
Jens Axboe78e19bb2019-11-06 15:21:34 -0700941static void io_double_put_req(struct io_kiocb *req)
942{
943 /* drop both submit and complete references */
944 if (refcount_sub_and_test(2, &req->refs))
945 __io_free_req(req);
946}
947
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700948static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -0600949{
Jens Axboe84f97dc2019-11-06 11:27:53 -0700950 struct io_rings *rings = ctx->rings;
951
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700952 /*
953 * noflush == true is from the waitqueue handler, just ensure we wake
954 * up the task, and the next invocation will flush the entries. We
955 * cannot safely to it from here.
956 */
957 if (noflush && !list_empty(&ctx->cq_overflow_list))
958 return -1U;
959
960 io_cqring_overflow_flush(ctx, false);
961
Jens Axboea3a0e432019-08-20 11:03:11 -0600962 /* See comment at the top of this file */
963 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +0000964 return READ_ONCE(rings->cq.tail) - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -0600965}
966
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +0300967static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
968{
969 struct io_rings *rings = ctx->rings;
970
971 /* make sure SQ entry isn't read before tail */
972 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
973}
974
Jens Axboedef596e2019-01-09 08:59:42 -0700975/*
976 * Find and free completed poll iocbs
977 */
978static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
979 struct list_head *done)
980{
981 void *reqs[IO_IOPOLL_BATCH];
982 struct io_kiocb *req;
Jens Axboe09bb8392019-03-13 12:39:28 -0600983 int to_free;
Jens Axboedef596e2019-01-09 08:59:42 -0700984
Jens Axboe09bb8392019-03-13 12:39:28 -0600985 to_free = 0;
Jens Axboedef596e2019-01-09 08:59:42 -0700986 while (!list_empty(done)) {
987 req = list_first_entry(done, struct io_kiocb, list);
988 list_del(&req->list);
989
Jens Axboe78e19bb2019-11-06 15:21:34 -0700990 io_cqring_fill_event(req, req->result);
Jens Axboedef596e2019-01-09 08:59:42 -0700991 (*nr_events)++;
992
Jens Axboe09bb8392019-03-13 12:39:28 -0600993 if (refcount_dec_and_test(&req->refs)) {
994 /* If we're not using fixed files, we have to pair the
995 * completion part with the file put. Use regular
996 * completions for those, only batch free for fixed
Jens Axboe9e645e112019-05-10 16:07:28 -0600997 * file and non-linked commands.
Jens Axboe09bb8392019-03-13 12:39:28 -0600998 */
Jens Axboe9e645e112019-05-10 16:07:28 -0600999 if ((req->flags & (REQ_F_FIXED_FILE|REQ_F_LINK)) ==
1000 REQ_F_FIXED_FILE) {
Jens Axboe09bb8392019-03-13 12:39:28 -06001001 reqs[to_free++] = req;
1002 if (to_free == ARRAY_SIZE(reqs))
1003 io_free_req_many(ctx, reqs, &to_free);
Jens Axboe6b063142019-01-10 22:13:58 -07001004 } else {
Jens Axboeba816ad2019-09-28 11:36:45 -06001005 io_free_req(req, NULL);
Jens Axboe6b063142019-01-10 22:13:58 -07001006 }
Jens Axboe9a56a232019-01-09 09:06:50 -07001007 }
Jens Axboedef596e2019-01-09 08:59:42 -07001008 }
Jens Axboedef596e2019-01-09 08:59:42 -07001009
Jens Axboe09bb8392019-03-13 12:39:28 -06001010 io_commit_cqring(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07001011 io_free_req_many(ctx, reqs, &to_free);
1012}
1013
1014static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1015 long min)
1016{
1017 struct io_kiocb *req, *tmp;
1018 LIST_HEAD(done);
1019 bool spin;
1020 int ret;
1021
1022 /*
1023 * Only spin for completions if we don't have multiple devices hanging
1024 * off our complete list, and we're under the requested amount.
1025 */
1026 spin = !ctx->poll_multi_file && *nr_events < min;
1027
1028 ret = 0;
1029 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
1030 struct kiocb *kiocb = &req->rw;
1031
1032 /*
1033 * Move completed entries to our local list. If we find a
1034 * request that requires polling, break out and complete
1035 * the done list first, if we have entries there.
1036 */
1037 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
1038 list_move_tail(&req->list, &done);
1039 continue;
1040 }
1041 if (!list_empty(&done))
1042 break;
1043
1044 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1045 if (ret < 0)
1046 break;
1047
1048 if (ret && spin)
1049 spin = false;
1050 ret = 0;
1051 }
1052
1053 if (!list_empty(&done))
1054 io_iopoll_complete(ctx, nr_events, &done);
1055
1056 return ret;
1057}
1058
1059/*
1060 * Poll for a mininum of 'min' events. Note that if min == 0 we consider that a
1061 * non-spinning poll check - we'll still enter the driver poll loop, but only
1062 * as a non-spinning completion check.
1063 */
1064static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
1065 long min)
1066{
Jens Axboe08f54392019-08-21 22:19:11 -06001067 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07001068 int ret;
1069
1070 ret = io_do_iopoll(ctx, nr_events, min);
1071 if (ret < 0)
1072 return ret;
1073 if (!min || *nr_events >= min)
1074 return 0;
1075 }
1076
1077 return 1;
1078}
1079
1080/*
1081 * We can't just wait for polled events to come to us, we have to actively
1082 * find and complete them.
1083 */
1084static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
1085{
1086 if (!(ctx->flags & IORING_SETUP_IOPOLL))
1087 return;
1088
1089 mutex_lock(&ctx->uring_lock);
1090 while (!list_empty(&ctx->poll_list)) {
1091 unsigned int nr_events = 0;
1092
1093 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -06001094
1095 /*
1096 * Ensure we allow local-to-the-cpu processing to take place,
1097 * in this case we need to ensure that we reap all events.
1098 */
1099 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -07001100 }
1101 mutex_unlock(&ctx->uring_lock);
1102}
1103
Jens Axboe2b2ed972019-10-25 10:06:15 -06001104static int __io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1105 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07001106{
Jens Axboe2b2ed972019-10-25 10:06:15 -06001107 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001108
1109 do {
1110 int tmin = 0;
1111
Jens Axboe500f9fb2019-08-19 12:15:59 -06001112 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06001113 * Don't enter poll loop if we already have events pending.
1114 * If we do, we can potentially be spinning for commands that
1115 * already triggered a CQE (eg in error).
1116 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001117 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06001118 break;
1119
1120 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06001121 * If a submit got punted to a workqueue, we can have the
1122 * application entering polling for a command before it gets
1123 * issued. That app will hold the uring_lock for the duration
1124 * of the poll right here, so we need to take a breather every
1125 * now and then to ensure that the issue has a chance to add
1126 * the poll to the issued list. Otherwise we can spin here
1127 * forever, while the workqueue is stuck trying to acquire the
1128 * very same mutex.
1129 */
1130 if (!(++iters & 7)) {
1131 mutex_unlock(&ctx->uring_lock);
1132 mutex_lock(&ctx->uring_lock);
1133 }
1134
Jens Axboedef596e2019-01-09 08:59:42 -07001135 if (*nr_events < min)
1136 tmin = min - *nr_events;
1137
1138 ret = io_iopoll_getevents(ctx, nr_events, tmin);
1139 if (ret <= 0)
1140 break;
1141 ret = 0;
1142 } while (min && !*nr_events && !need_resched());
1143
Jens Axboe2b2ed972019-10-25 10:06:15 -06001144 return ret;
1145}
1146
1147static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1148 long min)
1149{
1150 int ret;
1151
1152 /*
1153 * We disallow the app entering submit/complete with polling, but we
1154 * still need to lock the ring to prevent racing with polled issue
1155 * that got punted to a workqueue.
1156 */
1157 mutex_lock(&ctx->uring_lock);
1158 ret = __io_iopoll_check(ctx, nr_events, min);
Jens Axboe500f9fb2019-08-19 12:15:59 -06001159 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001160 return ret;
1161}
1162
Jens Axboe491381ce2019-10-17 09:20:46 -06001163static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001164{
Jens Axboe491381ce2019-10-17 09:20:46 -06001165 /*
1166 * Tell lockdep we inherited freeze protection from submission
1167 * thread.
1168 */
1169 if (req->flags & REQ_F_ISREG) {
1170 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001171
Jens Axboe491381ce2019-10-17 09:20:46 -06001172 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001173 }
Jens Axboe491381ce2019-10-17 09:20:46 -06001174 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001175}
1176
Jens Axboeba816ad2019-09-28 11:36:45 -06001177static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001178{
1179 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1180
Jens Axboe491381ce2019-10-17 09:20:46 -06001181 if (kiocb->ki_flags & IOCB_WRITE)
1182 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001183
Jens Axboe9e645e112019-05-10 16:07:28 -06001184 if ((req->flags & REQ_F_LINK) && res != req->result)
1185 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001186 io_cqring_add_event(req, res);
Jens Axboeba816ad2019-09-28 11:36:45 -06001187}
1188
1189static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1190{
1191 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1192
1193 io_complete_rw_common(kiocb, res);
1194 io_put_req(req, NULL);
1195}
1196
1197static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res)
1198{
1199 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1200
1201 io_complete_rw_common(kiocb, res);
1202 return io_put_req_find_next(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001203}
1204
Jens Axboedef596e2019-01-09 08:59:42 -07001205static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1206{
1207 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1208
Jens Axboe491381ce2019-10-17 09:20:46 -06001209 if (kiocb->ki_flags & IOCB_WRITE)
1210 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001211
Jens Axboe9e645e112019-05-10 16:07:28 -06001212 if ((req->flags & REQ_F_LINK) && res != req->result)
1213 req->flags |= REQ_F_FAIL_LINK;
1214 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -07001215 if (res != -EAGAIN)
1216 req->flags |= REQ_F_IOPOLL_COMPLETED;
1217}
1218
1219/*
1220 * After the iocb has been issued, it's safe to be found on the poll list.
1221 * Adding the kiocb to the list AFTER submission ensures that we don't
1222 * find it from a io_iopoll_getevents() thread before the issuer is done
1223 * accessing the kiocb cookie.
1224 */
1225static void io_iopoll_req_issued(struct io_kiocb *req)
1226{
1227 struct io_ring_ctx *ctx = req->ctx;
1228
1229 /*
1230 * Track whether we have multiple files in our lists. This will impact
1231 * how we do polling eventually, not spinning if we're on potentially
1232 * different devices.
1233 */
1234 if (list_empty(&ctx->poll_list)) {
1235 ctx->poll_multi_file = false;
1236 } else if (!ctx->poll_multi_file) {
1237 struct io_kiocb *list_req;
1238
1239 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1240 list);
1241 if (list_req->rw.ki_filp != req->rw.ki_filp)
1242 ctx->poll_multi_file = true;
1243 }
1244
1245 /*
1246 * For fast devices, IO may have already completed. If it has, add
1247 * it to the front so we find it first.
1248 */
1249 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1250 list_add(&req->list, &ctx->poll_list);
1251 else
1252 list_add_tail(&req->list, &ctx->poll_list);
1253}
1254
Jens Axboe3d6770f2019-04-13 11:50:54 -06001255static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07001256{
Jens Axboe3d6770f2019-04-13 11:50:54 -06001257 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -07001258 int diff = state->has_refs - state->used_refs;
1259
1260 if (diff)
1261 fput_many(state->file, diff);
1262 state->file = NULL;
1263 }
1264}
1265
1266/*
1267 * Get as many references to a file as we have IOs left in this submission,
1268 * assuming most submissions are for one file, or at least that each file
1269 * has more than one submission.
1270 */
1271static struct file *io_file_get(struct io_submit_state *state, int fd)
1272{
1273 if (!state)
1274 return fget(fd);
1275
1276 if (state->file) {
1277 if (state->fd == fd) {
1278 state->used_refs++;
1279 state->ios_left--;
1280 return state->file;
1281 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06001282 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07001283 }
1284 state->file = fget_many(fd, state->ios_left);
1285 if (!state->file)
1286 return NULL;
1287
1288 state->fd = fd;
1289 state->has_refs = state->ios_left;
1290 state->used_refs = 1;
1291 state->ios_left--;
1292 return state->file;
1293}
1294
Jens Axboe2b188cc2019-01-07 10:46:33 -07001295/*
1296 * If we tracked the file through the SCM inflight mechanism, we could support
1297 * any file. For now, just ensure that anything potentially problematic is done
1298 * inline.
1299 */
1300static bool io_file_supports_async(struct file *file)
1301{
1302 umode_t mode = file_inode(file)->i_mode;
1303
1304 if (S_ISBLK(mode) || S_ISCHR(mode))
1305 return true;
1306 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
1307 return true;
1308
1309 return false;
1310}
1311
Pavel Begunkov267bc902019-11-07 01:41:08 +03001312static int io_prep_rw(struct io_kiocb *req, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001313{
Pavel Begunkov267bc902019-11-07 01:41:08 +03001314 const struct io_uring_sqe *sqe = req->submit.sqe;
Jens Axboedef596e2019-01-09 08:59:42 -07001315 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001316 struct kiocb *kiocb = &req->rw;
Jens Axboe09bb8392019-03-13 12:39:28 -06001317 unsigned ioprio;
1318 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001319
Jens Axboe09bb8392019-03-13 12:39:28 -06001320 if (!req->file)
1321 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001322
Jens Axboe491381ce2019-10-17 09:20:46 -06001323 if (S_ISREG(file_inode(req->file)->i_mode))
1324 req->flags |= REQ_F_ISREG;
1325
1326 /*
1327 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
1328 * we know to async punt it even if it was opened O_NONBLOCK
1329 */
1330 if (force_nonblock && !io_file_supports_async(req->file)) {
1331 req->flags |= REQ_F_MUST_PUNT;
1332 return -EAGAIN;
1333 }
Jens Axboe6b063142019-01-10 22:13:58 -07001334
Jens Axboe2b188cc2019-01-07 10:46:33 -07001335 kiocb->ki_pos = READ_ONCE(sqe->off);
1336 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
1337 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
1338
1339 ioprio = READ_ONCE(sqe->ioprio);
1340 if (ioprio) {
1341 ret = ioprio_check_cap(ioprio);
1342 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06001343 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001344
1345 kiocb->ki_ioprio = ioprio;
1346 } else
1347 kiocb->ki_ioprio = get_current_ioprio();
1348
1349 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
1350 if (unlikely(ret))
Jens Axboe09bb8392019-03-13 12:39:28 -06001351 return ret;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001352
1353 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06001354 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
1355 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02001356 req->flags |= REQ_F_NOWAIT;
1357
1358 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001359 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001360
Jens Axboedef596e2019-01-09 08:59:42 -07001361 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07001362 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
1363 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06001364 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001365
Jens Axboedef596e2019-01-09 08:59:42 -07001366 kiocb->ki_flags |= IOCB_HIPRI;
1367 kiocb->ki_complete = io_complete_rw_iopoll;
1368 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001369 if (kiocb->ki_flags & IOCB_HIPRI)
1370 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07001371 kiocb->ki_complete = io_complete_rw;
1372 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001373 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001374}
1375
1376static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
1377{
1378 switch (ret) {
1379 case -EIOCBQUEUED:
1380 break;
1381 case -ERESTARTSYS:
1382 case -ERESTARTNOINTR:
1383 case -ERESTARTNOHAND:
1384 case -ERESTART_RESTARTBLOCK:
1385 /*
1386 * We can't just restart the syscall, since previously
1387 * submitted sqes may already be in progress. Just fail this
1388 * IO with EINTR.
1389 */
1390 ret = -EINTR;
1391 /* fall through */
1392 default:
1393 kiocb->ki_complete(kiocb, ret, 0);
1394 }
1395}
1396
Jens Axboeba816ad2019-09-28 11:36:45 -06001397static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt,
1398 bool in_async)
1399{
1400 if (in_async && ret >= 0 && nxt && kiocb->ki_complete == io_complete_rw)
1401 *nxt = __io_complete_rw(kiocb, ret);
1402 else
1403 io_rw_done(kiocb, ret);
1404}
1405
Jens Axboeedafcce2019-01-09 09:16:05 -07001406static int io_import_fixed(struct io_ring_ctx *ctx, int rw,
1407 const struct io_uring_sqe *sqe,
1408 struct iov_iter *iter)
1409{
1410 size_t len = READ_ONCE(sqe->len);
1411 struct io_mapped_ubuf *imu;
1412 unsigned index, buf_index;
1413 size_t offset;
1414 u64 buf_addr;
1415
1416 /* attempt to use fixed buffers without having provided iovecs */
1417 if (unlikely(!ctx->user_bufs))
1418 return -EFAULT;
1419
1420 buf_index = READ_ONCE(sqe->buf_index);
1421 if (unlikely(buf_index >= ctx->nr_user_bufs))
1422 return -EFAULT;
1423
1424 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
1425 imu = &ctx->user_bufs[index];
1426 buf_addr = READ_ONCE(sqe->addr);
1427
1428 /* overflow */
1429 if (buf_addr + len < buf_addr)
1430 return -EFAULT;
1431 /* not inside the mapped region */
1432 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
1433 return -EFAULT;
1434
1435 /*
1436 * May not be a start of buffer, set size appropriately
1437 * and advance us to the beginning.
1438 */
1439 offset = buf_addr - imu->ubuf;
1440 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06001441
1442 if (offset) {
1443 /*
1444 * Don't use iov_iter_advance() here, as it's really slow for
1445 * using the latter parts of a big fixed buffer - it iterates
1446 * over each segment manually. We can cheat a bit here, because
1447 * we know that:
1448 *
1449 * 1) it's a BVEC iter, we set it up
1450 * 2) all bvecs are PAGE_SIZE in size, except potentially the
1451 * first and last bvec
1452 *
1453 * So just find our index, and adjust the iterator afterwards.
1454 * If the offset is within the first bvec (or the whole first
1455 * bvec, just use iov_iter_advance(). This makes it easier
1456 * since we can just skip the first segment, which may not
1457 * be PAGE_SIZE aligned.
1458 */
1459 const struct bio_vec *bvec = imu->bvec;
1460
1461 if (offset <= bvec->bv_len) {
1462 iov_iter_advance(iter, offset);
1463 } else {
1464 unsigned long seg_skip;
1465
1466 /* skip first vec */
1467 offset -= bvec->bv_len;
1468 seg_skip = 1 + (offset >> PAGE_SHIFT);
1469
1470 iter->bvec = bvec + seg_skip;
1471 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02001472 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001473 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001474 }
1475 }
1476
Jens Axboeedafcce2019-01-09 09:16:05 -07001477 return 0;
1478}
1479
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001480static ssize_t io_import_iovec(struct io_ring_ctx *ctx, int rw,
1481 const struct sqe_submit *s, struct iovec **iovec,
1482 struct iov_iter *iter)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001483{
1484 const struct io_uring_sqe *sqe = s->sqe;
1485 void __user *buf = u64_to_user_ptr(READ_ONCE(sqe->addr));
1486 size_t sqe_len = READ_ONCE(sqe->len);
Jens Axboeedafcce2019-01-09 09:16:05 -07001487 u8 opcode;
1488
1489 /*
1490 * We're reading ->opcode for the second time, but the first read
1491 * doesn't care whether it's _FIXED or not, so it doesn't matter
1492 * whether ->opcode changes concurrently. The first read does care
1493 * about whether it is a READ or a WRITE, so we don't trust this read
1494 * for that purpose and instead let the caller pass in the read/write
1495 * flag.
1496 */
1497 opcode = READ_ONCE(sqe->opcode);
1498 if (opcode == IORING_OP_READ_FIXED ||
1499 opcode == IORING_OP_WRITE_FIXED) {
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001500 ssize_t ret = io_import_fixed(ctx, rw, sqe, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07001501 *iovec = NULL;
1502 return ret;
1503 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001504
1505 if (!s->has_user)
1506 return -EFAULT;
1507
1508#ifdef CONFIG_COMPAT
1509 if (ctx->compat)
1510 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
1511 iovec, iter);
1512#endif
1513
1514 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
1515}
1516
Jens Axboe32960612019-09-23 11:05:34 -06001517/*
1518 * For files that don't have ->read_iter() and ->write_iter(), handle them
1519 * by looping over ->read() or ->write() manually.
1520 */
1521static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
1522 struct iov_iter *iter)
1523{
1524 ssize_t ret = 0;
1525
1526 /*
1527 * Don't support polled IO through this interface, and we can't
1528 * support non-blocking either. For the latter, this just causes
1529 * the kiocb to be handled from an async context.
1530 */
1531 if (kiocb->ki_flags & IOCB_HIPRI)
1532 return -EOPNOTSUPP;
1533 if (kiocb->ki_flags & IOCB_NOWAIT)
1534 return -EAGAIN;
1535
1536 while (iov_iter_count(iter)) {
1537 struct iovec iovec = iov_iter_iovec(iter);
1538 ssize_t nr;
1539
1540 if (rw == READ) {
1541 nr = file->f_op->read(file, iovec.iov_base,
1542 iovec.iov_len, &kiocb->ki_pos);
1543 } else {
1544 nr = file->f_op->write(file, iovec.iov_base,
1545 iovec.iov_len, &kiocb->ki_pos);
1546 }
1547
1548 if (nr < 0) {
1549 if (!ret)
1550 ret = nr;
1551 break;
1552 }
1553 ret += nr;
1554 if (nr != iovec.iov_len)
1555 break;
1556 iov_iter_advance(iter, nr);
1557 }
1558
1559 return ret;
1560}
1561
Pavel Begunkov267bc902019-11-07 01:41:08 +03001562static int io_read(struct io_kiocb *req, struct io_kiocb **nxt,
1563 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001564{
1565 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1566 struct kiocb *kiocb = &req->rw;
1567 struct iov_iter iter;
1568 struct file *file;
Jens Axboe31b51512019-01-18 22:56:34 -07001569 size_t iov_count;
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001570 ssize_t read_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001571
Pavel Begunkov267bc902019-11-07 01:41:08 +03001572 ret = io_prep_rw(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001573 if (ret)
1574 return ret;
1575 file = kiocb->ki_filp;
1576
Jens Axboe2b188cc2019-01-07 10:46:33 -07001577 if (unlikely(!(file->f_mode & FMODE_READ)))
Jens Axboe09bb8392019-03-13 12:39:28 -06001578 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001579
Pavel Begunkov267bc902019-11-07 01:41:08 +03001580 ret = io_import_iovec(req->ctx, READ, &req->submit, &iovec, &iter);
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001581 if (ret < 0)
Jens Axboe09bb8392019-03-13 12:39:28 -06001582 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001583
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001584 read_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06001585 if (req->flags & REQ_F_LINK)
1586 req->result = read_size;
1587
Jens Axboe31b51512019-01-18 22:56:34 -07001588 iov_count = iov_iter_count(&iter);
1589 ret = rw_verify_area(READ, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001590 if (!ret) {
1591 ssize_t ret2;
1592
Jens Axboe32960612019-09-23 11:05:34 -06001593 if (file->f_op->read_iter)
1594 ret2 = call_read_iter(file, kiocb, &iter);
1595 else
1596 ret2 = loop_rw_iter(READ, file, kiocb, &iter);
1597
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001598 /*
1599 * In case of a short read, punt to async. This can happen
1600 * if we have data partially cached. Alternatively we can
1601 * return the short read, in which case the application will
1602 * need to issue another SQE and wait for it. That SQE will
1603 * need async punt anyway, so it's more efficient to do it
1604 * here.
1605 */
Jens Axboe491381ce2019-10-17 09:20:46 -06001606 if (force_nonblock && !(req->flags & REQ_F_NOWAIT) &&
1607 (req->flags & REQ_F_ISREG) &&
1608 ret2 > 0 && ret2 < read_size)
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001609 ret2 = -EAGAIN;
1610 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboe561fb042019-10-24 07:25:42 -06001611 if (!force_nonblock || ret2 != -EAGAIN)
Pavel Begunkov267bc902019-11-07 01:41:08 +03001612 kiocb_done(kiocb, ret2, nxt, req->submit.in_async);
Jens Axboe561fb042019-10-24 07:25:42 -06001613 else
Jens Axboe2b188cc2019-01-07 10:46:33 -07001614 ret = -EAGAIN;
1615 }
1616 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001617 return ret;
1618}
1619
Pavel Begunkov267bc902019-11-07 01:41:08 +03001620static int io_write(struct io_kiocb *req, struct io_kiocb **nxt,
1621 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001622{
1623 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1624 struct kiocb *kiocb = &req->rw;
1625 struct iov_iter iter;
1626 struct file *file;
Jens Axboe31b51512019-01-18 22:56:34 -07001627 size_t iov_count;
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001628 ssize_t ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001629
Pavel Begunkov267bc902019-11-07 01:41:08 +03001630 ret = io_prep_rw(req, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001631 if (ret)
1632 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001633
Jens Axboe2b188cc2019-01-07 10:46:33 -07001634 file = kiocb->ki_filp;
1635 if (unlikely(!(file->f_mode & FMODE_WRITE)))
Jens Axboe09bb8392019-03-13 12:39:28 -06001636 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001637
Pavel Begunkov267bc902019-11-07 01:41:08 +03001638 ret = io_import_iovec(req->ctx, WRITE, &req->submit, &iovec, &iter);
Jens Axboe87e5e6d2019-05-14 16:02:22 -06001639 if (ret < 0)
Jens Axboe09bb8392019-03-13 12:39:28 -06001640 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001641
Jens Axboe9e645e112019-05-10 16:07:28 -06001642 if (req->flags & REQ_F_LINK)
1643 req->result = ret;
1644
Jens Axboe31b51512019-01-18 22:56:34 -07001645 iov_count = iov_iter_count(&iter);
1646
1647 ret = -EAGAIN;
Jens Axboe561fb042019-10-24 07:25:42 -06001648 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT))
Jens Axboe31b51512019-01-18 22:56:34 -07001649 goto out_free;
Jens Axboe31b51512019-01-18 22:56:34 -07001650
1651 ret = rw_verify_area(WRITE, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001652 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01001653 ssize_t ret2;
1654
Jens Axboe2b188cc2019-01-07 10:46:33 -07001655 /*
1656 * Open-code file_start_write here to grab freeze protection,
1657 * which will be released by another thread in
1658 * io_complete_rw(). Fool lockdep by telling it the lock got
1659 * released so that it doesn't complain about the held lock when
1660 * we return to userspace.
1661 */
Jens Axboe491381ce2019-10-17 09:20:46 -06001662 if (req->flags & REQ_F_ISREG) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001663 __sb_start_write(file_inode(file)->i_sb,
1664 SB_FREEZE_WRITE, true);
1665 __sb_writers_release(file_inode(file)->i_sb,
1666 SB_FREEZE_WRITE);
1667 }
1668 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01001669
Jens Axboe32960612019-09-23 11:05:34 -06001670 if (file->f_op->write_iter)
1671 ret2 = call_write_iter(file, kiocb, &iter);
1672 else
1673 ret2 = loop_rw_iter(WRITE, file, kiocb, &iter);
Jens Axboe561fb042019-10-24 07:25:42 -06001674 if (!force_nonblock || ret2 != -EAGAIN)
Pavel Begunkov267bc902019-11-07 01:41:08 +03001675 kiocb_done(kiocb, ret2, nxt, req->submit.in_async);
Jens Axboe561fb042019-10-24 07:25:42 -06001676 else
Roman Penyaev9bf79332019-03-25 20:09:24 +01001677 ret = -EAGAIN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001678 }
Jens Axboe31b51512019-01-18 22:56:34 -07001679out_free:
Jens Axboe2b188cc2019-01-07 10:46:33 -07001680 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001681 return ret;
1682}
1683
1684/*
1685 * IORING_OP_NOP just posts a completion event, nothing else.
1686 */
Jens Axboe78e19bb2019-11-06 15:21:34 -07001687static int io_nop(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001688{
1689 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001690
Jens Axboedef596e2019-01-09 08:59:42 -07001691 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1692 return -EINVAL;
1693
Jens Axboe78e19bb2019-11-06 15:21:34 -07001694 io_cqring_add_event(req, 0);
Jens Axboeba816ad2019-09-28 11:36:45 -06001695 io_put_req(req, NULL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001696 return 0;
1697}
1698
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001699static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1700{
Jens Axboe6b063142019-01-10 22:13:58 -07001701 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001702
Jens Axboe09bb8392019-03-13 12:39:28 -06001703 if (!req->file)
1704 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001705
Jens Axboe6b063142019-01-10 22:13:58 -07001706 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07001707 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07001708 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001709 return -EINVAL;
1710
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001711 return 0;
1712}
1713
1714static int io_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001715 struct io_kiocb **nxt, bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001716{
1717 loff_t sqe_off = READ_ONCE(sqe->off);
1718 loff_t sqe_len = READ_ONCE(sqe->len);
1719 loff_t end = sqe_off + sqe_len;
1720 unsigned fsync_flags;
1721 int ret;
1722
1723 fsync_flags = READ_ONCE(sqe->fsync_flags);
1724 if (unlikely(fsync_flags & ~IORING_FSYNC_DATASYNC))
1725 return -EINVAL;
1726
1727 ret = io_prep_fsync(req, sqe);
1728 if (ret)
1729 return ret;
1730
1731 /* fsync always requires a blocking context */
1732 if (force_nonblock)
1733 return -EAGAIN;
1734
1735 ret = vfs_fsync_range(req->rw.ki_filp, sqe_off,
1736 end > 0 ? end : LLONG_MAX,
1737 fsync_flags & IORING_FSYNC_DATASYNC);
1738
Jens Axboe9e645e112019-05-10 16:07:28 -06001739 if (ret < 0 && (req->flags & REQ_F_LINK))
1740 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001741 io_cqring_add_event(req, ret);
Jens Axboeba816ad2019-09-28 11:36:45 -06001742 io_put_req(req, nxt);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001743 return 0;
1744}
1745
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001746static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1747{
1748 struct io_ring_ctx *ctx = req->ctx;
1749 int ret = 0;
1750
1751 if (!req->file)
1752 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001753
1754 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1755 return -EINVAL;
1756 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
1757 return -EINVAL;
1758
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001759 return ret;
1760}
1761
1762static int io_sync_file_range(struct io_kiocb *req,
1763 const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001764 struct io_kiocb **nxt,
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001765 bool force_nonblock)
1766{
1767 loff_t sqe_off;
1768 loff_t sqe_len;
1769 unsigned flags;
1770 int ret;
1771
1772 ret = io_prep_sfr(req, sqe);
1773 if (ret)
1774 return ret;
1775
1776 /* sync_file_range always requires a blocking context */
1777 if (force_nonblock)
1778 return -EAGAIN;
1779
1780 sqe_off = READ_ONCE(sqe->off);
1781 sqe_len = READ_ONCE(sqe->len);
1782 flags = READ_ONCE(sqe->sync_range_flags);
1783
1784 ret = sync_file_range(req->rw.ki_filp, sqe_off, sqe_len, flags);
1785
Jens Axboe9e645e112019-05-10 16:07:28 -06001786 if (ret < 0 && (req->flags & REQ_F_LINK))
1787 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001788 io_cqring_add_event(req, ret);
Jens Axboeba816ad2019-09-28 11:36:45 -06001789 io_put_req(req, nxt);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001790 return 0;
1791}
1792
Jens Axboe0fa03c62019-04-19 13:34:07 -06001793#if defined(CONFIG_NET)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001794static int io_send_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001795 struct io_kiocb **nxt, bool force_nonblock,
Jens Axboeaa1fa282019-04-19 13:38:09 -06001796 long (*fn)(struct socket *, struct user_msghdr __user *,
1797 unsigned int))
1798{
Jens Axboe0fa03c62019-04-19 13:34:07 -06001799 struct socket *sock;
1800 int ret;
1801
1802 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1803 return -EINVAL;
1804
1805 sock = sock_from_file(req->file, &ret);
1806 if (sock) {
1807 struct user_msghdr __user *msg;
1808 unsigned flags;
1809
1810 flags = READ_ONCE(sqe->msg_flags);
1811 if (flags & MSG_DONTWAIT)
1812 req->flags |= REQ_F_NOWAIT;
1813 else if (force_nonblock)
1814 flags |= MSG_DONTWAIT;
1815
1816 msg = (struct user_msghdr __user *) (unsigned long)
1817 READ_ONCE(sqe->addr);
1818
Jens Axboeaa1fa282019-04-19 13:38:09 -06001819 ret = fn(sock, msg, flags);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001820 if (force_nonblock && ret == -EAGAIN)
1821 return ret;
1822 }
1823
Jens Axboe78e19bb2019-11-06 15:21:34 -07001824 io_cqring_add_event(req, ret);
Jens Axboef1f40852019-11-05 20:33:16 -07001825 if (ret < 0 && (req->flags & REQ_F_LINK))
1826 req->flags |= REQ_F_FAIL_LINK;
Jens Axboeba816ad2019-09-28 11:36:45 -06001827 io_put_req(req, nxt);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001828 return 0;
Jens Axboeaa1fa282019-04-19 13:38:09 -06001829}
1830#endif
1831
1832static int io_sendmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001833 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001834{
1835#if defined(CONFIG_NET)
Jens Axboeba816ad2019-09-28 11:36:45 -06001836 return io_send_recvmsg(req, sqe, nxt, force_nonblock,
1837 __sys_sendmsg_sock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06001838#else
1839 return -EOPNOTSUPP;
1840#endif
1841}
1842
1843static int io_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001844 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboeaa1fa282019-04-19 13:38:09 -06001845{
1846#if defined(CONFIG_NET)
Jens Axboeba816ad2019-09-28 11:36:45 -06001847 return io_send_recvmsg(req, sqe, nxt, force_nonblock,
1848 __sys_recvmsg_sock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06001849#else
1850 return -EOPNOTSUPP;
1851#endif
1852}
1853
Jens Axboe17f2fe32019-10-17 14:42:58 -06001854static int io_accept(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1855 struct io_kiocb **nxt, bool force_nonblock)
1856{
1857#if defined(CONFIG_NET)
1858 struct sockaddr __user *addr;
1859 int __user *addr_len;
1860 unsigned file_flags;
1861 int flags, ret;
1862
1863 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
1864 return -EINVAL;
1865 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
1866 return -EINVAL;
1867
1868 addr = (struct sockaddr __user *) (unsigned long) READ_ONCE(sqe->addr);
1869 addr_len = (int __user *) (unsigned long) READ_ONCE(sqe->addr2);
1870 flags = READ_ONCE(sqe->accept_flags);
1871 file_flags = force_nonblock ? O_NONBLOCK : 0;
1872
1873 ret = __sys_accept4_file(req->file, file_flags, addr, addr_len, flags);
1874 if (ret == -EAGAIN && force_nonblock) {
1875 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
1876 return -EAGAIN;
1877 }
1878 if (ret < 0 && (req->flags & REQ_F_LINK))
1879 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001880 io_cqring_add_event(req, ret);
Jens Axboe17f2fe32019-10-17 14:42:58 -06001881 io_put_req(req, nxt);
1882 return 0;
1883#else
1884 return -EOPNOTSUPP;
1885#endif
1886}
1887
Jens Axboe221c5eb2019-01-17 09:41:58 -07001888static void io_poll_remove_one(struct io_kiocb *req)
1889{
1890 struct io_poll_iocb *poll = &req->poll;
1891
1892 spin_lock(&poll->head->lock);
1893 WRITE_ONCE(poll->canceled, true);
1894 if (!list_empty(&poll->wait.entry)) {
1895 list_del_init(&poll->wait.entry);
Jens Axboe18d9be12019-09-10 09:13:05 -06001896 io_queue_async_work(req->ctx, req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001897 }
1898 spin_unlock(&poll->head->lock);
1899
1900 list_del_init(&req->list);
1901}
1902
1903static void io_poll_remove_all(struct io_ring_ctx *ctx)
1904{
1905 struct io_kiocb *req;
1906
1907 spin_lock_irq(&ctx->completion_lock);
1908 while (!list_empty(&ctx->cancel_list)) {
1909 req = list_first_entry(&ctx->cancel_list, struct io_kiocb,list);
1910 io_poll_remove_one(req);
1911 }
1912 spin_unlock_irq(&ctx->completion_lock);
1913}
1914
1915/*
1916 * Find a running poll command that matches one specified in sqe->addr,
1917 * and remove it if found.
1918 */
1919static int io_poll_remove(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1920{
1921 struct io_ring_ctx *ctx = req->ctx;
1922 struct io_kiocb *poll_req, *next;
1923 int ret = -ENOENT;
1924
1925 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
1926 return -EINVAL;
1927 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
1928 sqe->poll_events)
1929 return -EINVAL;
1930
1931 spin_lock_irq(&ctx->completion_lock);
1932 list_for_each_entry_safe(poll_req, next, &ctx->cancel_list, list) {
1933 if (READ_ONCE(sqe->addr) == poll_req->user_data) {
1934 io_poll_remove_one(poll_req);
1935 ret = 0;
1936 break;
1937 }
1938 }
1939 spin_unlock_irq(&ctx->completion_lock);
1940
Jens Axboe78e19bb2019-11-06 15:21:34 -07001941 io_cqring_add_event(req, ret);
Jens Axboef1f40852019-11-05 20:33:16 -07001942 if (ret < 0 && (req->flags & REQ_F_LINK))
1943 req->flags |= REQ_F_FAIL_LINK;
Jens Axboeba816ad2019-09-28 11:36:45 -06001944 io_put_req(req, NULL);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001945 return 0;
1946}
1947
Jens Axboe8c838782019-03-12 15:48:16 -06001948static void io_poll_complete(struct io_ring_ctx *ctx, struct io_kiocb *req,
1949 __poll_t mask)
Jens Axboe221c5eb2019-01-17 09:41:58 -07001950{
Jens Axboe8c838782019-03-12 15:48:16 -06001951 req->poll.done = true;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001952 io_cqring_fill_event(req, mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06001953 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001954}
1955
Jens Axboe561fb042019-10-24 07:25:42 -06001956static void io_poll_complete_work(struct io_wq_work **workptr)
Jens Axboe221c5eb2019-01-17 09:41:58 -07001957{
Jens Axboe561fb042019-10-24 07:25:42 -06001958 struct io_wq_work *work = *workptr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001959 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
1960 struct io_poll_iocb *poll = &req->poll;
1961 struct poll_table_struct pt = { ._key = poll->events };
1962 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe89723d02019-11-05 15:32:58 -07001963 struct io_kiocb *nxt = NULL;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001964 __poll_t mask = 0;
1965
Jens Axboe561fb042019-10-24 07:25:42 -06001966 if (work->flags & IO_WQ_WORK_CANCEL)
1967 WRITE_ONCE(poll->canceled, true);
1968
Jens Axboe221c5eb2019-01-17 09:41:58 -07001969 if (!READ_ONCE(poll->canceled))
1970 mask = vfs_poll(poll->file, &pt) & poll->events;
1971
1972 /*
1973 * Note that ->ki_cancel callers also delete iocb from active_reqs after
1974 * calling ->ki_cancel. We need the ctx_lock roundtrip here to
1975 * synchronize with them. In the cancellation case the list_del_init
1976 * itself is not actually needed, but harmless so we keep it in to
1977 * avoid further branches in the fast path.
1978 */
1979 spin_lock_irq(&ctx->completion_lock);
1980 if (!mask && !READ_ONCE(poll->canceled)) {
1981 add_wait_queue(poll->head, &poll->wait);
1982 spin_unlock_irq(&ctx->completion_lock);
1983 return;
1984 }
1985 list_del_init(&req->list);
Jens Axboe8c838782019-03-12 15:48:16 -06001986 io_poll_complete(ctx, req, mask);
Jens Axboe221c5eb2019-01-17 09:41:58 -07001987 spin_unlock_irq(&ctx->completion_lock);
1988
Jens Axboe8c838782019-03-12 15:48:16 -06001989 io_cqring_ev_posted(ctx);
Jens Axboe89723d02019-11-05 15:32:58 -07001990
1991 io_put_req(req, &nxt);
1992 if (nxt)
1993 *workptr = &nxt->work;
Jens Axboe221c5eb2019-01-17 09:41:58 -07001994}
1995
1996static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
1997 void *key)
1998{
1999 struct io_poll_iocb *poll = container_of(wait, struct io_poll_iocb,
2000 wait);
2001 struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
2002 struct io_ring_ctx *ctx = req->ctx;
2003 __poll_t mask = key_to_poll(key);
Jens Axboe8c838782019-03-12 15:48:16 -06002004 unsigned long flags;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002005
2006 /* for instances that support it check for an event match first: */
Jens Axboe8c838782019-03-12 15:48:16 -06002007 if (mask && !(mask & poll->events))
2008 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002009
2010 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06002011
2012 if (mask && spin_trylock_irqsave(&ctx->completion_lock, flags)) {
2013 list_del(&req->list);
2014 io_poll_complete(ctx, req, mask);
2015 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2016
2017 io_cqring_ev_posted(ctx);
Jens Axboeba816ad2019-09-28 11:36:45 -06002018 io_put_req(req, NULL);
Jens Axboe8c838782019-03-12 15:48:16 -06002019 } else {
Jens Axboe18d9be12019-09-10 09:13:05 -06002020 io_queue_async_work(ctx, req);
Jens Axboe8c838782019-03-12 15:48:16 -06002021 }
2022
Jens Axboe221c5eb2019-01-17 09:41:58 -07002023 return 1;
2024}
2025
2026struct io_poll_table {
2027 struct poll_table_struct pt;
2028 struct io_kiocb *req;
2029 int error;
2030};
2031
2032static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
2033 struct poll_table_struct *p)
2034{
2035 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
2036
2037 if (unlikely(pt->req->poll.head)) {
2038 pt->error = -EINVAL;
2039 return;
2040 }
2041
2042 pt->error = 0;
2043 pt->req->poll.head = head;
2044 add_wait_queue(head, &pt->req->poll.wait);
2045}
2046
Jens Axboe89723d02019-11-05 15:32:58 -07002047static int io_poll_add(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2048 struct io_kiocb **nxt)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002049{
2050 struct io_poll_iocb *poll = &req->poll;
2051 struct io_ring_ctx *ctx = req->ctx;
2052 struct io_poll_table ipt;
Jens Axboe8c838782019-03-12 15:48:16 -06002053 bool cancel = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002054 __poll_t mask;
2055 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002056
2057 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2058 return -EINVAL;
2059 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
2060 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06002061 if (!poll->file)
2062 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002063
Jens Axboe6cc47d12019-09-18 11:18:23 -06002064 req->submit.sqe = NULL;
Jens Axboe561fb042019-10-24 07:25:42 -06002065 INIT_IO_WORK(&req->work, io_poll_complete_work);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002066 events = READ_ONCE(sqe->poll_events);
2067 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
2068
Jens Axboe221c5eb2019-01-17 09:41:58 -07002069 poll->head = NULL;
Jens Axboe8c838782019-03-12 15:48:16 -06002070 poll->done = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002071 poll->canceled = false;
2072
2073 ipt.pt._qproc = io_poll_queue_proc;
2074 ipt.pt._key = poll->events;
2075 ipt.req = req;
2076 ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
2077
2078 /* initialized the list so that we can do list_empty checks */
2079 INIT_LIST_HEAD(&poll->wait.entry);
2080 init_waitqueue_func_entry(&poll->wait, io_poll_wake);
2081
Jens Axboe36703242019-07-25 10:20:18 -06002082 INIT_LIST_HEAD(&req->list);
2083
Jens Axboe221c5eb2019-01-17 09:41:58 -07002084 mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002085
2086 spin_lock_irq(&ctx->completion_lock);
Jens Axboe8c838782019-03-12 15:48:16 -06002087 if (likely(poll->head)) {
2088 spin_lock(&poll->head->lock);
2089 if (unlikely(list_empty(&poll->wait.entry))) {
2090 if (ipt.error)
2091 cancel = true;
2092 ipt.error = 0;
2093 mask = 0;
2094 }
2095 if (mask || ipt.error)
2096 list_del_init(&poll->wait.entry);
2097 else if (cancel)
2098 WRITE_ONCE(poll->canceled, true);
2099 else if (!poll->done) /* actually waiting for an event */
2100 list_add_tail(&req->list, &ctx->cancel_list);
2101 spin_unlock(&poll->head->lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002102 }
Jens Axboe8c838782019-03-12 15:48:16 -06002103 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06002104 ipt.error = 0;
2105 io_poll_complete(ctx, req, mask);
2106 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07002107 spin_unlock_irq(&ctx->completion_lock);
2108
Jens Axboe8c838782019-03-12 15:48:16 -06002109 if (mask) {
2110 io_cqring_ev_posted(ctx);
Jens Axboe89723d02019-11-05 15:32:58 -07002111 io_put_req(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002112 }
Jens Axboe8c838782019-03-12 15:48:16 -06002113 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002114}
2115
Jens Axboe5262f562019-09-17 12:26:57 -06002116static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
2117{
2118 struct io_ring_ctx *ctx;
Jens Axboe11365042019-10-16 09:08:32 -06002119 struct io_kiocb *req;
Jens Axboe5262f562019-09-17 12:26:57 -06002120 unsigned long flags;
2121
2122 req = container_of(timer, struct io_kiocb, timeout.timer);
2123 ctx = req->ctx;
2124 atomic_inc(&ctx->cq_timeouts);
2125
2126 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08002127 /*
Jens Axboe11365042019-10-16 09:08:32 -06002128 * We could be racing with timeout deletion. If the list is empty,
2129 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08002130 */
Jens Axboe842f9612019-10-29 12:34:10 -06002131 if (!list_empty(&req->list)) {
Jens Axboe11365042019-10-16 09:08:32 -06002132 struct io_kiocb *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06002133
Jens Axboe11365042019-10-16 09:08:32 -06002134 /*
2135 * Adjust the reqs sequence before the current one because it
2136 * will consume a slot in the cq_ring and the the cq_tail
2137 * pointer will be increased, otherwise other timeout reqs may
2138 * return in advance without waiting for enough wait_nr.
2139 */
2140 prev = req;
2141 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
2142 prev->sequence++;
Jens Axboe11365042019-10-16 09:08:32 -06002143 list_del_init(&req->list);
Jens Axboe11365042019-10-16 09:08:32 -06002144 }
Jens Axboe842f9612019-10-29 12:34:10 -06002145
Jens Axboe78e19bb2019-11-06 15:21:34 -07002146 io_cqring_fill_event(req, -ETIME);
Jens Axboe842f9612019-10-29 12:34:10 -06002147 io_commit_cqring(ctx);
Jens Axboe5262f562019-09-17 12:26:57 -06002148 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2149
Jens Axboe842f9612019-10-29 12:34:10 -06002150 io_cqring_ev_posted(ctx);
Jens Axboef1f40852019-11-05 20:33:16 -07002151 if (req->flags & REQ_F_LINK)
2152 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe842f9612019-10-29 12:34:10 -06002153 io_put_req(req, NULL);
Jens Axboe11365042019-10-16 09:08:32 -06002154 return HRTIMER_NORESTART;
2155}
2156
2157/*
2158 * Remove or update an existing timeout command
2159 */
2160static int io_timeout_remove(struct io_kiocb *req,
2161 const struct io_uring_sqe *sqe)
2162{
2163 struct io_ring_ctx *ctx = req->ctx;
2164 struct io_kiocb *treq;
2165 int ret = -ENOENT;
2166 __u64 user_data;
2167 unsigned flags;
2168
2169 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2170 return -EINVAL;
2171 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
2172 return -EINVAL;
2173 flags = READ_ONCE(sqe->timeout_flags);
2174 if (flags)
2175 return -EINVAL;
2176
2177 user_data = READ_ONCE(sqe->addr);
2178 spin_lock_irq(&ctx->completion_lock);
2179 list_for_each_entry(treq, &ctx->timeout_list, list) {
2180 if (user_data == treq->user_data) {
2181 list_del_init(&treq->list);
2182 ret = 0;
2183 break;
2184 }
2185 }
2186
2187 /* didn't find timeout */
2188 if (ret) {
2189fill_ev:
Jens Axboe78e19bb2019-11-06 15:21:34 -07002190 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06002191 io_commit_cqring(ctx);
2192 spin_unlock_irq(&ctx->completion_lock);
2193 io_cqring_ev_posted(ctx);
Jens Axboef1f40852019-11-05 20:33:16 -07002194 if (req->flags & REQ_F_LINK)
2195 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe11365042019-10-16 09:08:32 -06002196 io_put_req(req, NULL);
2197 return 0;
2198 }
2199
2200 ret = hrtimer_try_to_cancel(&treq->timeout.timer);
2201 if (ret == -1) {
2202 ret = -EBUSY;
2203 goto fill_ev;
2204 }
2205
Jens Axboe78e19bb2019-11-06 15:21:34 -07002206 io_cqring_fill_event(req, 0);
2207 io_cqring_fill_event(treq, -ECANCELED);
Jens Axboe11365042019-10-16 09:08:32 -06002208 io_commit_cqring(ctx);
2209 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06002210 io_cqring_ev_posted(ctx);
2211
Jens Axboe11365042019-10-16 09:08:32 -06002212 io_put_req(treq, NULL);
Jens Axboeba816ad2019-09-28 11:36:45 -06002213 io_put_req(req, NULL);
Jens Axboe11365042019-10-16 09:08:32 -06002214 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06002215}
2216
2217static int io_timeout(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2218{
yangerkun5da0fb12019-10-15 21:59:29 +08002219 unsigned count;
Jens Axboe5262f562019-09-17 12:26:57 -06002220 struct io_ring_ctx *ctx = req->ctx;
2221 struct list_head *entry;
Jens Axboea41525a2019-10-15 16:48:15 -06002222 enum hrtimer_mode mode;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06002223 struct timespec64 ts;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002224 unsigned span = 0;
Jens Axboea41525a2019-10-15 16:48:15 -06002225 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06002226
2227 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2228 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06002229 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len != 1)
2230 return -EINVAL;
2231 flags = READ_ONCE(sqe->timeout_flags);
2232 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06002233 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06002234
2235 if (get_timespec64(&ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06002236 return -EFAULT;
2237
Jens Axboe11365042019-10-16 09:08:32 -06002238 if (flags & IORING_TIMEOUT_ABS)
2239 mode = HRTIMER_MODE_ABS;
2240 else
2241 mode = HRTIMER_MODE_REL;
2242
2243 hrtimer_init(&req->timeout.timer, CLOCK_MONOTONIC, mode);
2244
Jens Axboe5262f562019-09-17 12:26:57 -06002245 /*
2246 * sqe->off holds how many events that need to occur for this
2247 * timeout event to be satisfied.
2248 */
2249 count = READ_ONCE(sqe->off);
2250 if (!count)
2251 count = 1;
2252
2253 req->sequence = ctx->cached_sq_head + count - 1;
yangerkun5da0fb12019-10-15 21:59:29 +08002254 /* reuse it to store the count */
2255 req->submit.sequence = count;
Jens Axboe5262f562019-09-17 12:26:57 -06002256 req->flags |= REQ_F_TIMEOUT;
2257
2258 /*
2259 * Insertion sort, ensuring the first entry in the list is always
2260 * the one we need first.
2261 */
Jens Axboe5262f562019-09-17 12:26:57 -06002262 spin_lock_irq(&ctx->completion_lock);
2263 list_for_each_prev(entry, &ctx->timeout_list) {
2264 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
yangerkun5da0fb12019-10-15 21:59:29 +08002265 unsigned nxt_sq_head;
2266 long long tmp, tmp_nxt;
Jens Axboe5262f562019-09-17 12:26:57 -06002267
yangerkun5da0fb12019-10-15 21:59:29 +08002268 /*
2269 * Since cached_sq_head + count - 1 can overflow, use type long
2270 * long to store it.
2271 */
2272 tmp = (long long)ctx->cached_sq_head + count - 1;
2273 nxt_sq_head = nxt->sequence - nxt->submit.sequence + 1;
2274 tmp_nxt = (long long)nxt_sq_head + nxt->submit.sequence - 1;
2275
2276 /*
2277 * cached_sq_head may overflow, and it will never overflow twice
2278 * once there is some timeout req still be valid.
2279 */
2280 if (ctx->cached_sq_head < nxt_sq_head)
yangerkun8b07a652019-10-17 12:12:35 +08002281 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08002282
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002283 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06002284 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002285
2286 /*
2287 * Sequence of reqs after the insert one and itself should
2288 * be adjusted because each timeout req consumes a slot.
2289 */
2290 span++;
2291 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06002292 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002293 req->sequence -= span;
Jens Axboe5262f562019-09-17 12:26:57 -06002294 list_add(&req->list, entry);
Jens Axboe5262f562019-09-17 12:26:57 -06002295 req->timeout.timer.function = io_timeout_fn;
Jens Axboea41525a2019-10-15 16:48:15 -06002296 hrtimer_start(&req->timeout.timer, timespec64_to_ktime(ts), mode);
Jens Axboe842f9612019-10-29 12:34:10 -06002297 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06002298 return 0;
2299}
2300
Jens Axboe62755e32019-10-28 21:49:21 -06002301static bool io_cancel_cb(struct io_wq_work *work, void *data)
2302{
2303 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
2304
2305 return req->user_data == (unsigned long) data;
2306}
2307
Jens Axboee977d6d2019-11-05 12:39:45 -07002308static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06002309{
Jens Axboe62755e32019-10-28 21:49:21 -06002310 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06002311 int ret = 0;
2312
Jens Axboe62755e32019-10-28 21:49:21 -06002313 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
2314 switch (cancel_ret) {
2315 case IO_WQ_CANCEL_OK:
2316 ret = 0;
2317 break;
2318 case IO_WQ_CANCEL_RUNNING:
2319 ret = -EALREADY;
2320 break;
2321 case IO_WQ_CANCEL_NOTFOUND:
2322 ret = -ENOENT;
2323 break;
2324 }
2325
Jens Axboee977d6d2019-11-05 12:39:45 -07002326 return ret;
2327}
2328
2329static int io_async_cancel(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2330 struct io_kiocb **nxt)
2331{
2332 struct io_ring_ctx *ctx = req->ctx;
2333 void *sqe_addr;
2334 int ret;
2335
2336 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2337 return -EINVAL;
2338 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
2339 sqe->cancel_flags)
2340 return -EINVAL;
2341
2342 sqe_addr = (void *) (unsigned long) READ_ONCE(sqe->addr);
2343 ret = io_async_cancel_one(ctx, sqe_addr);
2344
Jens Axboe62755e32019-10-28 21:49:21 -06002345 if (ret < 0 && (req->flags & REQ_F_LINK))
2346 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07002347 io_cqring_add_event(req, ret);
Jens Axboe62755e32019-10-28 21:49:21 -06002348 io_put_req(req, nxt);
2349 return 0;
2350}
2351
Pavel Begunkov267bc902019-11-07 01:41:08 +03002352static int io_req_defer(struct io_ring_ctx *ctx, struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -06002353{
Pavel Begunkov267bc902019-11-07 01:41:08 +03002354 const struct io_uring_sqe *sqe = req->submit.sqe;
Jens Axboede0617e2019-04-06 21:51:27 -06002355 struct io_uring_sqe *sqe_copy;
2356
2357 if (!io_sequence_defer(ctx, req) && list_empty(&ctx->defer_list))
2358 return 0;
2359
2360 sqe_copy = kmalloc(sizeof(*sqe_copy), GFP_KERNEL);
2361 if (!sqe_copy)
2362 return -EAGAIN;
2363
2364 spin_lock_irq(&ctx->completion_lock);
2365 if (!io_sequence_defer(ctx, req) && list_empty(&ctx->defer_list)) {
2366 spin_unlock_irq(&ctx->completion_lock);
2367 kfree(sqe_copy);
2368 return 0;
2369 }
2370
2371 memcpy(sqe_copy, sqe, sizeof(*sqe_copy));
2372 req->submit.sqe = sqe_copy;
2373
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002374 trace_io_uring_defer(ctx, req, false);
Jens Axboede0617e2019-04-06 21:51:27 -06002375 list_add_tail(&req->list, &ctx->defer_list);
2376 spin_unlock_irq(&ctx->completion_lock);
2377 return -EIOCBQUEUED;
2378}
2379
Jens Axboe2b188cc2019-01-07 10:46:33 -07002380static int __io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
Pavel Begunkov267bc902019-11-07 01:41:08 +03002381 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002382{
Jens Axboee0c5c572019-03-12 10:18:47 -06002383 int ret, opcode;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002384 struct sqe_submit *s = &req->submit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002385
Jens Axboe2b188cc2019-01-07 10:46:33 -07002386 opcode = READ_ONCE(s->sqe->opcode);
2387 switch (opcode) {
2388 case IORING_OP_NOP:
Jens Axboe78e19bb2019-11-06 15:21:34 -07002389 ret = io_nop(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002390 break;
2391 case IORING_OP_READV:
Jens Axboeedafcce2019-01-09 09:16:05 -07002392 if (unlikely(s->sqe->buf_index))
2393 return -EINVAL;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002394 ret = io_read(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002395 break;
2396 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07002397 if (unlikely(s->sqe->buf_index))
2398 return -EINVAL;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002399 ret = io_write(req, nxt, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07002400 break;
2401 case IORING_OP_READ_FIXED:
Pavel Begunkov267bc902019-11-07 01:41:08 +03002402 ret = io_read(req, nxt, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07002403 break;
2404 case IORING_OP_WRITE_FIXED:
Pavel Begunkov267bc902019-11-07 01:41:08 +03002405 ret = io_write(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002406 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002407 case IORING_OP_FSYNC:
Jens Axboeba816ad2019-09-28 11:36:45 -06002408 ret = io_fsync(req, s->sqe, nxt, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002409 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002410 case IORING_OP_POLL_ADD:
Jens Axboe89723d02019-11-05 15:32:58 -07002411 ret = io_poll_add(req, s->sqe, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002412 break;
2413 case IORING_OP_POLL_REMOVE:
2414 ret = io_poll_remove(req, s->sqe);
2415 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002416 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboeba816ad2019-09-28 11:36:45 -06002417 ret = io_sync_file_range(req, s->sqe, nxt, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002418 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002419 case IORING_OP_SENDMSG:
Jens Axboeba816ad2019-09-28 11:36:45 -06002420 ret = io_sendmsg(req, s->sqe, nxt, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06002421 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06002422 case IORING_OP_RECVMSG:
Jens Axboeba816ad2019-09-28 11:36:45 -06002423 ret = io_recvmsg(req, s->sqe, nxt, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06002424 break;
Jens Axboe5262f562019-09-17 12:26:57 -06002425 case IORING_OP_TIMEOUT:
2426 ret = io_timeout(req, s->sqe);
2427 break;
Jens Axboe11365042019-10-16 09:08:32 -06002428 case IORING_OP_TIMEOUT_REMOVE:
2429 ret = io_timeout_remove(req, s->sqe);
2430 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06002431 case IORING_OP_ACCEPT:
2432 ret = io_accept(req, s->sqe, nxt, force_nonblock);
2433 break;
Jens Axboe62755e32019-10-28 21:49:21 -06002434 case IORING_OP_ASYNC_CANCEL:
2435 ret = io_async_cancel(req, s->sqe, nxt);
2436 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002437 default:
2438 ret = -EINVAL;
2439 break;
2440 }
2441
Jens Axboedef596e2019-01-09 08:59:42 -07002442 if (ret)
2443 return ret;
2444
2445 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe9e645e112019-05-10 16:07:28 -06002446 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07002447 return -EAGAIN;
2448
2449 /* workqueue context doesn't hold uring_lock, grab it now */
Jackie Liuba5290c2019-10-09 09:19:59 +08002450 if (s->in_async)
Jens Axboedef596e2019-01-09 08:59:42 -07002451 mutex_lock(&ctx->uring_lock);
2452 io_iopoll_req_issued(req);
Jackie Liuba5290c2019-10-09 09:19:59 +08002453 if (s->in_async)
Jens Axboedef596e2019-01-09 08:59:42 -07002454 mutex_unlock(&ctx->uring_lock);
2455 }
2456
2457 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002458}
2459
Jens Axboe561fb042019-10-24 07:25:42 -06002460static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboe31b51512019-01-18 22:56:34 -07002461{
Jens Axboe561fb042019-10-24 07:25:42 -06002462 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002463 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002464 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe561fb042019-10-24 07:25:42 -06002465 struct sqe_submit *s = &req->submit;
2466 const struct io_uring_sqe *sqe = s->sqe;
2467 struct io_kiocb *nxt = NULL;
2468 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002469
Jens Axboe561fb042019-10-24 07:25:42 -06002470 /* Ensure we clear previously set non-block flag */
2471 req->rw.ki_flags &= ~IOCB_NOWAIT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002472
Jens Axboe561fb042019-10-24 07:25:42 -06002473 if (work->flags & IO_WQ_WORK_CANCEL)
2474 ret = -ECANCELED;
Jens Axboe31b51512019-01-18 22:56:34 -07002475
Jens Axboe561fb042019-10-24 07:25:42 -06002476 if (!ret) {
2477 s->has_user = (work->flags & IO_WQ_WORK_HAS_MM) != 0;
2478 s->in_async = true;
2479 do {
Pavel Begunkov267bc902019-11-07 01:41:08 +03002480 ret = __io_submit_sqe(ctx, req, &nxt, false);
Jens Axboe561fb042019-10-24 07:25:42 -06002481 /*
2482 * We can get EAGAIN for polled IO even though we're
2483 * forcing a sync submission from here, since we can't
2484 * wait for request slots on the block side.
2485 */
2486 if (ret != -EAGAIN)
2487 break;
2488 cond_resched();
2489 } while (1);
2490 }
Jens Axboe31b51512019-01-18 22:56:34 -07002491
Jens Axboe561fb042019-10-24 07:25:42 -06002492 /* drop submission reference */
2493 io_put_req(req, NULL);
Jens Axboe817869d2019-04-30 14:44:05 -06002494
Jens Axboe561fb042019-10-24 07:25:42 -06002495 if (ret) {
Jens Axboef1f40852019-11-05 20:33:16 -07002496 if (req->flags & REQ_F_LINK)
2497 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07002498 io_cqring_add_event(req, ret);
Jens Axboeba816ad2019-09-28 11:36:45 -06002499 io_put_req(req, NULL);
Jens Axboeedafcce2019-01-09 09:16:05 -07002500 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002501
Jens Axboe561fb042019-10-24 07:25:42 -06002502 /* async context always use a copy of the sqe */
2503 kfree(sqe);
2504
2505 /* if a dependent link is ready, pass it back */
2506 if (!ret && nxt) {
2507 io_prep_async_work(nxt);
2508 *workptr = &nxt->work;
Jens Axboeedafcce2019-01-09 09:16:05 -07002509 }
Jens Axboe31b51512019-01-18 22:56:34 -07002510}
Jens Axboe2b188cc2019-01-07 10:46:33 -07002511
Jens Axboe09bb8392019-03-13 12:39:28 -06002512static bool io_op_needs_file(const struct io_uring_sqe *sqe)
2513{
2514 int op = READ_ONCE(sqe->opcode);
2515
2516 switch (op) {
2517 case IORING_OP_NOP:
2518 case IORING_OP_POLL_REMOVE:
2519 return false;
2520 default:
2521 return true;
2522 }
2523}
2524
Jens Axboe65e19f52019-10-26 07:20:21 -06002525static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
2526 int index)
2527{
2528 struct fixed_file_table *table;
2529
2530 table = &ctx->file_table[index >> IORING_FILE_TABLE_SHIFT];
2531 return table->files[index & IORING_FILE_TABLE_MASK];
2532}
2533
Pavel Begunkov267bc902019-11-07 01:41:08 +03002534static int io_req_set_file(struct io_ring_ctx *ctx,
Jens Axboe09bb8392019-03-13 12:39:28 -06002535 struct io_submit_state *state, struct io_kiocb *req)
2536{
Pavel Begunkov267bc902019-11-07 01:41:08 +03002537 struct sqe_submit *s = &req->submit;
Jens Axboe09bb8392019-03-13 12:39:28 -06002538 unsigned flags;
2539 int fd;
2540
2541 flags = READ_ONCE(s->sqe->flags);
2542 fd = READ_ONCE(s->sqe->fd);
2543
Jackie Liu4fe2c962019-09-09 20:50:40 +08002544 if (flags & IOSQE_IO_DRAIN)
Jens Axboede0617e2019-04-06 21:51:27 -06002545 req->flags |= REQ_F_IO_DRAIN;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002546 /*
2547 * All io need record the previous position, if LINK vs DARIN,
2548 * it can be used to mark the position of the first IO in the
2549 * link list.
2550 */
2551 req->sequence = s->sequence;
Jens Axboede0617e2019-04-06 21:51:27 -06002552
Jens Axboe60c112b2019-06-21 10:20:18 -06002553 if (!io_op_needs_file(s->sqe))
Jens Axboe09bb8392019-03-13 12:39:28 -06002554 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06002555
2556 if (flags & IOSQE_FIXED_FILE) {
Jens Axboe65e19f52019-10-26 07:20:21 -06002557 if (unlikely(!ctx->file_table ||
Jens Axboe09bb8392019-03-13 12:39:28 -06002558 (unsigned) fd >= ctx->nr_user_files))
2559 return -EBADF;
Jens Axboeb7620122019-10-26 07:22:55 -06002560 fd = array_index_nospec(fd, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06002561 req->file = io_file_from_index(ctx, fd);
2562 if (!req->file)
Jens Axboe08a45172019-10-03 08:11:03 -06002563 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06002564 req->flags |= REQ_F_FIXED_FILE;
2565 } else {
2566 if (s->needs_fixed_file)
2567 return -EBADF;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002568 trace_io_uring_file_get(ctx, fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06002569 req->file = io_file_get(state, fd);
2570 if (unlikely(!req->file))
2571 return -EBADF;
2572 }
2573
2574 return 0;
2575}
2576
Jens Axboefcb323c2019-10-24 12:39:47 -06002577static int io_grab_files(struct io_ring_ctx *ctx, struct io_kiocb *req)
2578{
2579 int ret = -EBADF;
2580
2581 rcu_read_lock();
2582 spin_lock_irq(&ctx->inflight_lock);
2583 /*
2584 * We use the f_ops->flush() handler to ensure that we can flush
2585 * out work accessing these files if the fd is closed. Check if
2586 * the fd has changed since we started down this path, and disallow
2587 * this operation if it has.
2588 */
2589 if (fcheck(req->submit.ring_fd) == req->submit.ring_file) {
2590 list_add(&req->inflight_entry, &ctx->inflight_list);
2591 req->flags |= REQ_F_INFLIGHT;
2592 req->work.files = current->files;
2593 ret = 0;
2594 }
2595 spin_unlock_irq(&ctx->inflight_lock);
2596 rcu_read_unlock();
2597
2598 return ret;
2599}
2600
Jens Axboe2665abf2019-11-05 12:40:47 -07002601static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
2602{
2603 struct io_kiocb *req = container_of(timer, struct io_kiocb,
2604 timeout.timer);
2605 struct io_ring_ctx *ctx = req->ctx;
2606 struct io_kiocb *prev = NULL;
2607 unsigned long flags;
2608 int ret = -ETIME;
2609
2610 spin_lock_irqsave(&ctx->completion_lock, flags);
2611
2612 /*
2613 * We don't expect the list to be empty, that will only happen if we
2614 * race with the completion of the linked work.
2615 */
2616 if (!list_empty(&req->list)) {
2617 prev = list_entry(req->list.prev, struct io_kiocb, link_list);
2618 list_del_init(&req->list);
2619 }
2620
2621 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2622
2623 if (prev) {
2624 void *user_data = (void *) (unsigned long) prev->user_data;
2625 ret = io_async_cancel_one(ctx, user_data);
2626 }
2627
Jens Axboe78e19bb2019-11-06 15:21:34 -07002628 io_cqring_add_event(req, ret);
Jens Axboe2665abf2019-11-05 12:40:47 -07002629 io_put_req(req, NULL);
2630 return HRTIMER_NORESTART;
2631}
2632
2633static int io_queue_linked_timeout(struct io_kiocb *req, struct io_kiocb *nxt)
2634{
2635 const struct io_uring_sqe *sqe = nxt->submit.sqe;
2636 enum hrtimer_mode mode;
2637 struct timespec64 ts;
2638 int ret = -EINVAL;
2639
2640 if (sqe->ioprio || sqe->buf_index || sqe->len != 1 || sqe->off)
2641 goto err;
2642 if (sqe->timeout_flags & ~IORING_TIMEOUT_ABS)
2643 goto err;
2644 if (get_timespec64(&ts, u64_to_user_ptr(sqe->addr))) {
2645 ret = -EFAULT;
2646 goto err;
2647 }
2648
2649 req->flags |= REQ_F_LINK_TIMEOUT;
2650
2651 if (sqe->timeout_flags & IORING_TIMEOUT_ABS)
2652 mode = HRTIMER_MODE_ABS;
2653 else
2654 mode = HRTIMER_MODE_REL;
2655 hrtimer_init(&nxt->timeout.timer, CLOCK_MONOTONIC, mode);
2656 nxt->timeout.timer.function = io_link_timeout_fn;
2657 hrtimer_start(&nxt->timeout.timer, timespec64_to_ktime(ts), mode);
2658 ret = 0;
2659err:
2660 /* drop submission reference */
2661 io_put_req(nxt, NULL);
2662
2663 if (ret) {
2664 struct io_ring_ctx *ctx = req->ctx;
2665
2666 /*
2667 * Break the link and fail linked timeout, parent will get
2668 * failed by the regular submission path.
2669 */
2670 list_del(&nxt->list);
Jens Axboe78e19bb2019-11-06 15:21:34 -07002671 io_cqring_fill_event(nxt, ret);
Jens Axboe2665abf2019-11-05 12:40:47 -07002672 trace_io_uring_fail_link(req, nxt);
2673 io_commit_cqring(ctx);
2674 io_put_req(nxt, NULL);
2675 ret = -ECANCELED;
2676 }
2677
2678 return ret;
2679}
2680
2681static inline struct io_kiocb *io_get_linked_timeout(struct io_kiocb *req)
2682{
2683 struct io_kiocb *nxt;
2684
2685 if (!(req->flags & REQ_F_LINK))
2686 return NULL;
2687
2688 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, list);
2689 if (nxt && nxt->submit.sqe->opcode == IORING_OP_LINK_TIMEOUT)
2690 return nxt;
2691
2692 return NULL;
2693}
2694
Pavel Begunkov267bc902019-11-07 01:41:08 +03002695static int __io_queue_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002696{
Jens Axboe2665abf2019-11-05 12:40:47 -07002697 struct io_kiocb *nxt;
Jens Axboee0c5c572019-03-12 10:18:47 -06002698 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002699
Jens Axboe2665abf2019-11-05 12:40:47 -07002700 nxt = io_get_linked_timeout(req);
2701 if (unlikely(nxt)) {
2702 ret = io_queue_linked_timeout(req, nxt);
2703 if (ret)
2704 goto err;
2705 }
2706
Pavel Begunkov267bc902019-11-07 01:41:08 +03002707 ret = __io_submit_sqe(ctx, req, NULL, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06002708
2709 /*
2710 * We async punt it if the file wasn't marked NOWAIT, or if the file
2711 * doesn't support non-blocking read/write attempts
2712 */
2713 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
2714 (req->flags & REQ_F_MUST_PUNT))) {
Pavel Begunkov267bc902019-11-07 01:41:08 +03002715 struct sqe_submit *s = &req->submit;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002716 struct io_uring_sqe *sqe_copy;
2717
Jackie Liu954dab12019-09-18 10:37:52 +08002718 sqe_copy = kmemdup(s->sqe, sizeof(*sqe_copy), GFP_KERNEL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002719 if (sqe_copy) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07002720 s->sqe = sqe_copy;
Jens Axboefcb323c2019-10-24 12:39:47 -06002721 if (req->work.flags & IO_WQ_WORK_NEEDS_FILES) {
2722 ret = io_grab_files(ctx, req);
2723 if (ret) {
2724 kfree(sqe_copy);
2725 goto err;
2726 }
2727 }
Jens Axboee65ef562019-03-12 10:16:44 -06002728
2729 /*
2730 * Queued up for async execution, worker will release
Jens Axboe9e645e112019-05-10 16:07:28 -06002731 * submit reference when the iocb is actually submitted.
Jens Axboee65ef562019-03-12 10:16:44 -06002732 */
Jens Axboefcb323c2019-10-24 12:39:47 -06002733 io_queue_async_work(ctx, req);
Jens Axboee65ef562019-03-12 10:16:44 -06002734 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002735 }
2736 }
Jens Axboee65ef562019-03-12 10:16:44 -06002737
2738 /* drop submission reference */
Jens Axboefcb323c2019-10-24 12:39:47 -06002739err:
Jens Axboeba816ad2019-09-28 11:36:45 -06002740 io_put_req(req, NULL);
Jens Axboee65ef562019-03-12 10:16:44 -06002741
2742 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06002743 if (ret) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07002744 io_cqring_add_event(req, ret);
Jens Axboe9e645e112019-05-10 16:07:28 -06002745 if (req->flags & REQ_F_LINK)
2746 req->flags |= REQ_F_FAIL_LINK;
Jens Axboeba816ad2019-09-28 11:36:45 -06002747 io_put_req(req, NULL);
Jens Axboe9e645e112019-05-10 16:07:28 -06002748 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002749
2750 return ret;
2751}
2752
Pavel Begunkov267bc902019-11-07 01:41:08 +03002753static int io_queue_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08002754{
2755 int ret;
2756
Pavel Begunkov267bc902019-11-07 01:41:08 +03002757 ret = io_req_defer(ctx, req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002758 if (ret) {
2759 if (ret != -EIOCBQUEUED) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07002760 io_cqring_add_event(req, ret);
2761 io_double_put_req(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002762 }
2763 return 0;
2764 }
2765
Pavel Begunkov267bc902019-11-07 01:41:08 +03002766 return __io_queue_sqe(ctx, req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002767}
2768
2769static int io_queue_link_head(struct io_ring_ctx *ctx, struct io_kiocb *req,
Pavel Begunkov267bc902019-11-07 01:41:08 +03002770 struct io_kiocb *shadow)
Jackie Liu4fe2c962019-09-09 20:50:40 +08002771{
2772 int ret;
2773 int need_submit = false;
2774
2775 if (!shadow)
Pavel Begunkov267bc902019-11-07 01:41:08 +03002776 return io_queue_sqe(ctx, req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002777
2778 /*
2779 * Mark the first IO in link list as DRAIN, let all the following
2780 * IOs enter the defer list. all IO needs to be completed before link
2781 * list.
2782 */
2783 req->flags |= REQ_F_IO_DRAIN;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002784 ret = io_req_defer(ctx, req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002785 if (ret) {
2786 if (ret != -EIOCBQUEUED) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07002787 io_cqring_add_event(req, ret);
2788 io_double_put_req(req);
Pavel Begunkov7b202382019-10-27 22:10:36 +03002789 __io_free_req(shadow);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002790 return 0;
2791 }
2792 } else {
2793 /*
2794 * If ret == 0 means that all IOs in front of link io are
2795 * running done. let's queue link head.
2796 */
2797 need_submit = true;
2798 }
2799
2800 /* Insert shadow req to defer_list, blocking next IOs */
2801 spin_lock_irq(&ctx->completion_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002802 trace_io_uring_defer(ctx, shadow, true);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002803 list_add_tail(&shadow->list, &ctx->defer_list);
2804 spin_unlock_irq(&ctx->completion_lock);
2805
2806 if (need_submit)
Pavel Begunkov267bc902019-11-07 01:41:08 +03002807 return __io_queue_sqe(ctx, req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08002808
2809 return 0;
2810}
2811
Jens Axboe9e645e112019-05-10 16:07:28 -06002812#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK)
2813
Pavel Begunkov196be952019-11-07 01:41:06 +03002814static void io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req,
Pavel Begunkov267bc902019-11-07 01:41:08 +03002815 struct io_submit_state *state, struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06002816{
2817 struct io_uring_sqe *sqe_copy;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002818 struct sqe_submit *s = &req->submit;
Jens Axboe9e645e112019-05-10 16:07:28 -06002819 int ret;
2820
Jens Axboe78e19bb2019-11-06 15:21:34 -07002821 req->user_data = s->sqe->user_data;
2822
Jens Axboe9e645e112019-05-10 16:07:28 -06002823 /* enforce forwards compatibility on users */
2824 if (unlikely(s->sqe->flags & ~SQE_VALID_FLAGS)) {
2825 ret = -EINVAL;
Pavel Begunkov196be952019-11-07 01:41:06 +03002826 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06002827 }
2828
Pavel Begunkov267bc902019-11-07 01:41:08 +03002829 ret = io_req_set_file(ctx, state, req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002830 if (unlikely(ret)) {
2831err_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07002832 io_cqring_add_event(req, ret);
2833 io_double_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002834 return;
2835 }
2836
Jens Axboe9e645e112019-05-10 16:07:28 -06002837 /*
2838 * If we already have a head request, queue this one for async
2839 * submittal once the head completes. If we don't have a head but
2840 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
2841 * submitted sync once the chain is complete. If none of those
2842 * conditions are true (normal request), then just queue it.
2843 */
2844 if (*link) {
2845 struct io_kiocb *prev = *link;
2846
2847 sqe_copy = kmemdup(s->sqe, sizeof(*sqe_copy), GFP_KERNEL);
2848 if (!sqe_copy) {
2849 ret = -EAGAIN;
2850 goto err_req;
2851 }
2852
2853 s->sqe = sqe_copy;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02002854 trace_io_uring_link(ctx, req, prev);
Jens Axboe9e645e112019-05-10 16:07:28 -06002855 list_add_tail(&req->list, &prev->link_list);
2856 } else if (s->sqe->flags & IOSQE_IO_LINK) {
2857 req->flags |= REQ_F_LINK;
2858
Jens Axboe9e645e112019-05-10 16:07:28 -06002859 INIT_LIST_HEAD(&req->link_list);
2860 *link = req;
Jens Axboe2665abf2019-11-05 12:40:47 -07002861 } else if (READ_ONCE(s->sqe->opcode) == IORING_OP_LINK_TIMEOUT) {
2862 /* Only valid as a linked SQE */
2863 ret = -EINVAL;
2864 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06002865 } else {
Pavel Begunkov267bc902019-11-07 01:41:08 +03002866 io_queue_sqe(ctx, req);
Jens Axboe9e645e112019-05-10 16:07:28 -06002867 }
2868}
2869
Jens Axboe9a56a232019-01-09 09:06:50 -07002870/*
2871 * Batched submission is done, ensure local IO is flushed out.
2872 */
2873static void io_submit_state_end(struct io_submit_state *state)
2874{
2875 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06002876 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07002877 if (state->free_reqs)
2878 kmem_cache_free_bulk(req_cachep, state->free_reqs,
2879 &state->reqs[state->cur_req]);
Jens Axboe9a56a232019-01-09 09:06:50 -07002880}
2881
2882/*
2883 * Start submission side cache.
2884 */
2885static void io_submit_state_start(struct io_submit_state *state,
2886 struct io_ring_ctx *ctx, unsigned max_ios)
2887{
2888 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07002889 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07002890 state->file = NULL;
2891 state->ios_left = max_ios;
2892}
2893
Jens Axboe2b188cc2019-01-07 10:46:33 -07002894static void io_commit_sqring(struct io_ring_ctx *ctx)
2895{
Hristo Venev75b28af2019-08-26 17:23:46 +00002896 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002897
Hristo Venev75b28af2019-08-26 17:23:46 +00002898 if (ctx->cached_sq_head != READ_ONCE(rings->sq.head)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07002899 /*
2900 * Ensure any loads from the SQEs are done at this point,
2901 * since once we write the new head, the application could
2902 * write new data to them.
2903 */
Hristo Venev75b28af2019-08-26 17:23:46 +00002904 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002905 }
2906}
2907
2908/*
Jens Axboe2b188cc2019-01-07 10:46:33 -07002909 * Fetch an sqe, if one is available. Note that s->sqe will point to memory
2910 * that is mapped by userspace. This means that care needs to be taken to
2911 * ensure that reads are stable, as we cannot rely on userspace always
2912 * being a good citizen. If members of the sqe are validated and then later
2913 * used, it's important that those reads are done through READ_ONCE() to
2914 * prevent a re-load down the line.
2915 */
2916static bool io_get_sqring(struct io_ring_ctx *ctx, struct sqe_submit *s)
2917{
Hristo Venev75b28af2019-08-26 17:23:46 +00002918 struct io_rings *rings = ctx->rings;
2919 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002920 unsigned head;
2921
2922 /*
2923 * The cached sq head (or cq tail) serves two purposes:
2924 *
2925 * 1) allows us to batch the cost of updating the user visible
2926 * head updates.
2927 * 2) allows the kernel side to track the head on its own, even
2928 * though the application is the one updating it.
2929 */
2930 head = ctx->cached_sq_head;
Stefan Bühlere523a292019-04-19 11:57:44 +02002931 /* make sure SQ entry isn't read before tail */
Hristo Venev75b28af2019-08-26 17:23:46 +00002932 if (head == smp_load_acquire(&rings->sq.tail))
Jens Axboe2b188cc2019-01-07 10:46:33 -07002933 return false;
2934
Hristo Venev75b28af2019-08-26 17:23:46 +00002935 head = READ_ONCE(sq_array[head & ctx->sq_mask]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002936 if (head < ctx->sq_entries) {
Jens Axboefcb323c2019-10-24 12:39:47 -06002937 s->ring_file = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002938 s->sqe = &ctx->sq_sqes[head];
Jackie Liu8776f3f2019-09-09 20:50:39 +08002939 s->sequence = ctx->cached_sq_head;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002940 ctx->cached_sq_head++;
2941 return true;
2942 }
2943
2944 /* drop invalid entries */
2945 ctx->cached_sq_head++;
Jens Axboe498ccd92019-10-25 10:04:25 -06002946 ctx->cached_sq_dropped++;
2947 WRITE_ONCE(rings->sq_dropped, ctx->cached_sq_dropped);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002948 return false;
2949}
2950
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002951static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkovae9428c2019-11-06 00:22:14 +03002952 struct file *ring_file, int ring_fd,
2953 struct mm_struct **mm, bool async)
Jens Axboe6c271ce2019-01-10 11:22:30 -07002954{
2955 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002956 struct io_kiocb *link = NULL;
Jackie Liu4fe2c962019-09-09 20:50:40 +08002957 struct io_kiocb *shadow_req = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06002958 int i, submitted = 0;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03002959 bool mm_fault = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07002960
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07002961 if (!list_empty(&ctx->cq_overflow_list)) {
2962 io_cqring_overflow_flush(ctx, false);
2963 return -EBUSY;
2964 }
2965
Jens Axboe6c271ce2019-01-10 11:22:30 -07002966 if (nr > IO_PLUG_THRESHOLD) {
2967 io_submit_state_start(&state, ctx, nr);
2968 statep = &state;
2969 }
2970
2971 for (i = 0; i < nr; i++) {
Pavel Begunkov196be952019-11-07 01:41:06 +03002972 struct io_kiocb *req;
Pavel Begunkov50585b92019-11-07 01:41:07 +03002973 unsigned int sqe_flags;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002974
Pavel Begunkov196be952019-11-07 01:41:06 +03002975 req = io_get_req(ctx, statep);
2976 if (unlikely(!req)) {
2977 if (!submitted)
2978 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002979 break;
Pavel Begunkov196be952019-11-07 01:41:06 +03002980 }
Pavel Begunkov50585b92019-11-07 01:41:07 +03002981 if (!io_get_sqring(ctx, &req->submit)) {
Pavel Begunkov196be952019-11-07 01:41:06 +03002982 __io_free_req(req);
2983 break;
2984 }
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03002985
Pavel Begunkov50585b92019-11-07 01:41:07 +03002986 if (io_sqe_needs_user(req->submit.sqe) && !*mm) {
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03002987 mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm);
2988 if (!mm_fault) {
2989 use_mm(ctx->sqo_mm);
2990 *mm = ctx->sqo_mm;
2991 }
2992 }
2993
Pavel Begunkov50585b92019-11-07 01:41:07 +03002994 sqe_flags = req->submit.sqe->flags;
2995
2996 if (link && (sqe_flags & IOSQE_IO_DRAIN)) {
Jackie Liu4fe2c962019-09-09 20:50:40 +08002997 if (!shadow_req) {
2998 shadow_req = io_get_req(ctx, NULL);
Jackie Liua1041c22019-09-18 17:25:52 +08002999 if (unlikely(!shadow_req))
3000 goto out;
Jackie Liu4fe2c962019-09-09 20:50:40 +08003001 shadow_req->flags |= (REQ_F_IO_DRAIN | REQ_F_SHADOW_DRAIN);
3002 refcount_dec(&shadow_req->refs);
3003 }
Pavel Begunkov50585b92019-11-07 01:41:07 +03003004 shadow_req->sequence = req->submit.sequence;
Jackie Liu4fe2c962019-09-09 20:50:40 +08003005 }
3006
Jackie Liua1041c22019-09-18 17:25:52 +08003007out:
Pavel Begunkov50585b92019-11-07 01:41:07 +03003008 req->submit.ring_file = ring_file;
3009 req->submit.ring_fd = ring_fd;
3010 req->submit.has_user = *mm != NULL;
3011 req->submit.in_async = async;
3012 req->submit.needs_fixed_file = async;
3013 trace_io_uring_submit_sqe(ctx, req->submit.sqe->user_data,
3014 true, async);
Pavel Begunkov267bc902019-11-07 01:41:08 +03003015 io_submit_sqe(ctx, req, statep, &link);
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03003016 submitted++;
Pavel Begunkove5eb6362019-11-06 00:22:15 +03003017
3018 /*
3019 * If previous wasn't linked and we have a linked command,
3020 * that's the end of the chain. Submit the previous link.
3021 */
Pavel Begunkov50585b92019-11-07 01:41:07 +03003022 if (!(sqe_flags & IOSQE_IO_LINK) && link) {
Pavel Begunkov267bc902019-11-07 01:41:08 +03003023 io_queue_link_head(ctx, link, shadow_req);
Pavel Begunkove5eb6362019-11-06 00:22:15 +03003024 link = NULL;
3025 shadow_req = NULL;
3026 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07003027 }
3028
Jens Axboe9e645e112019-05-10 16:07:28 -06003029 if (link)
Pavel Begunkov267bc902019-11-07 01:41:08 +03003030 io_queue_link_head(ctx, link, shadow_req);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003031 if (statep)
3032 io_submit_state_end(&state);
3033
Pavel Begunkovae9428c2019-11-06 00:22:14 +03003034 /* Commit SQ ring head once we've consumed and submitted all SQEs */
3035 io_commit_sqring(ctx);
3036
Jens Axboe6c271ce2019-01-10 11:22:30 -07003037 return submitted;
3038}
3039
3040static int io_sq_thread(void *data)
3041{
Jens Axboe6c271ce2019-01-10 11:22:30 -07003042 struct io_ring_ctx *ctx = data;
3043 struct mm_struct *cur_mm = NULL;
3044 mm_segment_t old_fs;
3045 DEFINE_WAIT(wait);
3046 unsigned inflight;
3047 unsigned long timeout;
3048
Jackie Liua4c0b3d2019-07-08 13:41:12 +08003049 complete(&ctx->sqo_thread_started);
3050
Jens Axboe6c271ce2019-01-10 11:22:30 -07003051 old_fs = get_fs();
3052 set_fs(USER_DS);
3053
3054 timeout = inflight = 0;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003055 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003056 unsigned int to_submit;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003057 int ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003058
3059 if (inflight) {
3060 unsigned nr_events = 0;
3061
3062 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe2b2ed972019-10-25 10:06:15 -06003063 /*
3064 * inflight is the count of the maximum possible
3065 * entries we submitted, but it can be smaller
3066 * if we dropped some of them. If we don't have
3067 * poll entries available, then we know that we
3068 * have nothing left to poll for. Reset the
3069 * inflight count to zero in that case.
3070 */
3071 mutex_lock(&ctx->uring_lock);
3072 if (!list_empty(&ctx->poll_list))
3073 __io_iopoll_check(ctx, &nr_events, 0);
3074 else
3075 inflight = 0;
3076 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003077 } else {
3078 /*
3079 * Normal IO, just pretend everything completed.
3080 * We don't have to poll completions for that.
3081 */
3082 nr_events = inflight;
3083 }
3084
3085 inflight -= nr_events;
3086 if (!inflight)
3087 timeout = jiffies + ctx->sq_thread_idle;
3088 }
3089
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003090 to_submit = io_sqring_entries(ctx);
3091 if (!to_submit) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07003092 /*
3093 * We're polling. If we're within the defined idle
3094 * period, then let us spin without work before going
3095 * to sleep.
3096 */
3097 if (inflight || !time_after(jiffies, timeout)) {
Jens Axboe9831a902019-09-19 09:48:55 -06003098 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07003099 continue;
3100 }
3101
3102 /*
3103 * Drop cur_mm before scheduling, we can't hold it for
3104 * long periods (or over schedule()). Do this before
3105 * adding ourselves to the waitqueue, as the unuse/drop
3106 * may sleep.
3107 */
3108 if (cur_mm) {
3109 unuse_mm(cur_mm);
3110 mmput(cur_mm);
3111 cur_mm = NULL;
3112 }
3113
3114 prepare_to_wait(&ctx->sqo_wait, &wait,
3115 TASK_INTERRUPTIBLE);
3116
3117 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00003118 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02003119 /* make sure to read SQ tail after writing flags */
3120 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07003121
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003122 to_submit = io_sqring_entries(ctx);
3123 if (!to_submit) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003124 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07003125 finish_wait(&ctx->sqo_wait, &wait);
3126 break;
3127 }
3128 if (signal_pending(current))
3129 flush_signals(current);
3130 schedule();
3131 finish_wait(&ctx->sqo_wait, &wait);
3132
Hristo Venev75b28af2019-08-26 17:23:46 +00003133 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003134 continue;
3135 }
3136 finish_wait(&ctx->sqo_wait, &wait);
3137
Hristo Venev75b28af2019-08-26 17:23:46 +00003138 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003139 }
3140
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003141 to_submit = min(to_submit, ctx->sq_entries);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003142 ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true);
3143 if (ret > 0)
3144 inflight += ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003145 }
3146
3147 set_fs(old_fs);
3148 if (cur_mm) {
3149 unuse_mm(cur_mm);
3150 mmput(cur_mm);
3151 }
Jens Axboe06058632019-04-13 09:26:03 -06003152
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003153 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06003154
Jens Axboe6c271ce2019-01-10 11:22:30 -07003155 return 0;
3156}
3157
Jens Axboebda52162019-09-24 13:47:15 -06003158struct io_wait_queue {
3159 struct wait_queue_entry wq;
3160 struct io_ring_ctx *ctx;
3161 unsigned to_wait;
3162 unsigned nr_timeouts;
3163};
3164
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003165static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06003166{
3167 struct io_ring_ctx *ctx = iowq->ctx;
3168
3169 /*
3170 * Wake up if we have enough events, or if a timeout occured since we
3171 * started waiting. For timeouts, we always want to return to userspace,
3172 * regardless of event count.
3173 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003174 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06003175 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
3176}
3177
3178static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
3179 int wake_flags, void *key)
3180{
3181 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
3182 wq);
3183
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003184 /* use noflush == true, as we can't safely rely on locking context */
3185 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06003186 return -1;
3187
3188 return autoremove_wake_function(curr, mode, wake_flags, key);
3189}
3190
Jens Axboe2b188cc2019-01-07 10:46:33 -07003191/*
3192 * Wait until events become available, if we don't already have some. The
3193 * application must reap them itself, as they reside on the shared cq ring.
3194 */
3195static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
3196 const sigset_t __user *sig, size_t sigsz)
3197{
Jens Axboebda52162019-09-24 13:47:15 -06003198 struct io_wait_queue iowq = {
3199 .wq = {
3200 .private = current,
3201 .func = io_wake_function,
3202 .entry = LIST_HEAD_INIT(iowq.wq.entry),
3203 },
3204 .ctx = ctx,
3205 .to_wait = min_events,
3206 };
Hristo Venev75b28af2019-08-26 17:23:46 +00003207 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08003208 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003209
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003210 if (io_cqring_events(ctx, false) >= min_events)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003211 return 0;
3212
3213 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01003214#ifdef CONFIG_COMPAT
3215 if (in_compat_syscall())
3216 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07003217 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01003218 else
3219#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07003220 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01003221
Jens Axboe2b188cc2019-01-07 10:46:33 -07003222 if (ret)
3223 return ret;
3224 }
3225
Jens Axboebda52162019-09-24 13:47:15 -06003226 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02003227 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06003228 do {
3229 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
3230 TASK_INTERRUPTIBLE);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003231 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06003232 break;
3233 schedule();
3234 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08003235 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06003236 break;
3237 }
3238 } while (1);
3239 finish_wait(&ctx->wait, &iowq.wq);
3240
Jackie Liue9ffa5c2019-10-29 11:16:42 +08003241 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003242
Hristo Venev75b28af2019-08-26 17:23:46 +00003243 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003244}
3245
Jens Axboe6b063142019-01-10 22:13:58 -07003246static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
3247{
3248#if defined(CONFIG_UNIX)
3249 if (ctx->ring_sock) {
3250 struct sock *sock = ctx->ring_sock->sk;
3251 struct sk_buff *skb;
3252
3253 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
3254 kfree_skb(skb);
3255 }
3256#else
3257 int i;
3258
Jens Axboe65e19f52019-10-26 07:20:21 -06003259 for (i = 0; i < ctx->nr_user_files; i++) {
3260 struct file *file;
3261
3262 file = io_file_from_index(ctx, i);
3263 if (file)
3264 fput(file);
3265 }
Jens Axboe6b063142019-01-10 22:13:58 -07003266#endif
3267}
3268
3269static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
3270{
Jens Axboe65e19f52019-10-26 07:20:21 -06003271 unsigned nr_tables, i;
3272
3273 if (!ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003274 return -ENXIO;
3275
3276 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06003277 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
3278 for (i = 0; i < nr_tables; i++)
3279 kfree(ctx->file_table[i].files);
3280 kfree(ctx->file_table);
3281 ctx->file_table = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003282 ctx->nr_user_files = 0;
3283 return 0;
3284}
3285
Jens Axboe6c271ce2019-01-10 11:22:30 -07003286static void io_sq_thread_stop(struct io_ring_ctx *ctx)
3287{
3288 if (ctx->sqo_thread) {
Jackie Liua4c0b3d2019-07-08 13:41:12 +08003289 wait_for_completion(&ctx->sqo_thread_started);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003290 /*
3291 * The park is a bit of a work-around, without it we get
3292 * warning spews on shutdown with SQPOLL set and affinity
3293 * set to a single CPU.
3294 */
Jens Axboe06058632019-04-13 09:26:03 -06003295 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003296 kthread_stop(ctx->sqo_thread);
3297 ctx->sqo_thread = NULL;
3298 }
3299}
3300
Jens Axboe6b063142019-01-10 22:13:58 -07003301static void io_finish_async(struct io_ring_ctx *ctx)
3302{
Jens Axboe6c271ce2019-01-10 11:22:30 -07003303 io_sq_thread_stop(ctx);
3304
Jens Axboe561fb042019-10-24 07:25:42 -06003305 if (ctx->io_wq) {
3306 io_wq_destroy(ctx->io_wq);
3307 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003308 }
3309}
3310
3311#if defined(CONFIG_UNIX)
3312static void io_destruct_skb(struct sk_buff *skb)
3313{
3314 struct io_ring_ctx *ctx = skb->sk->sk_user_data;
3315
Jens Axboe561fb042019-10-24 07:25:42 -06003316 if (ctx->io_wq)
3317 io_wq_flush(ctx->io_wq);
Jens Axboe8a997342019-10-09 14:40:13 -06003318
Jens Axboe6b063142019-01-10 22:13:58 -07003319 unix_destruct_scm(skb);
3320}
3321
3322/*
3323 * Ensure the UNIX gc is aware of our file set, so we are certain that
3324 * the io_uring can be safely unregistered on process exit, even if we have
3325 * loops in the file referencing.
3326 */
3327static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
3328{
3329 struct sock *sk = ctx->ring_sock->sk;
3330 struct scm_fp_list *fpl;
3331 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06003332 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07003333
3334 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
3335 unsigned long inflight = ctx->user->unix_inflight + nr;
3336
3337 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
3338 return -EMFILE;
3339 }
3340
3341 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
3342 if (!fpl)
3343 return -ENOMEM;
3344
3345 skb = alloc_skb(0, GFP_KERNEL);
3346 if (!skb) {
3347 kfree(fpl);
3348 return -ENOMEM;
3349 }
3350
3351 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07003352
Jens Axboe08a45172019-10-03 08:11:03 -06003353 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07003354 fpl->user = get_uid(ctx->user);
3355 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003356 struct file *file = io_file_from_index(ctx, i + offset);
3357
3358 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06003359 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06003360 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06003361 unix_inflight(fpl->user, fpl->fp[nr_files]);
3362 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07003363 }
3364
Jens Axboe08a45172019-10-03 08:11:03 -06003365 if (nr_files) {
3366 fpl->max = SCM_MAX_FD;
3367 fpl->count = nr_files;
3368 UNIXCB(skb).fp = fpl;
3369 skb->destructor = io_destruct_skb;
3370 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
3371 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07003372
Jens Axboe08a45172019-10-03 08:11:03 -06003373 for (i = 0; i < nr_files; i++)
3374 fput(fpl->fp[i]);
3375 } else {
3376 kfree_skb(skb);
3377 kfree(fpl);
3378 }
Jens Axboe6b063142019-01-10 22:13:58 -07003379
3380 return 0;
3381}
3382
3383/*
3384 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
3385 * causes regular reference counting to break down. We rely on the UNIX
3386 * garbage collection to take care of this problem for us.
3387 */
3388static int io_sqe_files_scm(struct io_ring_ctx *ctx)
3389{
3390 unsigned left, total;
3391 int ret = 0;
3392
3393 total = 0;
3394 left = ctx->nr_user_files;
3395 while (left) {
3396 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07003397
3398 ret = __io_sqe_files_scm(ctx, this_files, total);
3399 if (ret)
3400 break;
3401 left -= this_files;
3402 total += this_files;
3403 }
3404
3405 if (!ret)
3406 return 0;
3407
3408 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003409 struct file *file = io_file_from_index(ctx, total);
3410
3411 if (file)
3412 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07003413 total++;
3414 }
3415
3416 return ret;
3417}
3418#else
3419static int io_sqe_files_scm(struct io_ring_ctx *ctx)
3420{
3421 return 0;
3422}
3423#endif
3424
Jens Axboe65e19f52019-10-26 07:20:21 -06003425static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
3426 unsigned nr_files)
3427{
3428 int i;
3429
3430 for (i = 0; i < nr_tables; i++) {
3431 struct fixed_file_table *table = &ctx->file_table[i];
3432 unsigned this_files;
3433
3434 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
3435 table->files = kcalloc(this_files, sizeof(struct file *),
3436 GFP_KERNEL);
3437 if (!table->files)
3438 break;
3439 nr_files -= this_files;
3440 }
3441
3442 if (i == nr_tables)
3443 return 0;
3444
3445 for (i = 0; i < nr_tables; i++) {
3446 struct fixed_file_table *table = &ctx->file_table[i];
3447 kfree(table->files);
3448 }
3449 return 1;
3450}
3451
Jens Axboe6b063142019-01-10 22:13:58 -07003452static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
3453 unsigned nr_args)
3454{
3455 __s32 __user *fds = (__s32 __user *) arg;
Jens Axboe65e19f52019-10-26 07:20:21 -06003456 unsigned nr_tables;
Jens Axboe6b063142019-01-10 22:13:58 -07003457 int fd, ret = 0;
3458 unsigned i;
3459
Jens Axboe65e19f52019-10-26 07:20:21 -06003460 if (ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003461 return -EBUSY;
3462 if (!nr_args)
3463 return -EINVAL;
3464 if (nr_args > IORING_MAX_FIXED_FILES)
3465 return -EMFILE;
3466
Jens Axboe65e19f52019-10-26 07:20:21 -06003467 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
3468 ctx->file_table = kcalloc(nr_tables, sizeof(struct fixed_file_table),
3469 GFP_KERNEL);
3470 if (!ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003471 return -ENOMEM;
3472
Jens Axboe65e19f52019-10-26 07:20:21 -06003473 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
3474 kfree(ctx->file_table);
3475 return -ENOMEM;
3476 }
3477
Jens Axboe08a45172019-10-03 08:11:03 -06003478 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003479 struct fixed_file_table *table;
3480 unsigned index;
3481
Jens Axboe6b063142019-01-10 22:13:58 -07003482 ret = -EFAULT;
3483 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
3484 break;
Jens Axboe08a45172019-10-03 08:11:03 -06003485 /* allow sparse sets */
3486 if (fd == -1) {
3487 ret = 0;
3488 continue;
3489 }
Jens Axboe6b063142019-01-10 22:13:58 -07003490
Jens Axboe65e19f52019-10-26 07:20:21 -06003491 table = &ctx->file_table[i >> IORING_FILE_TABLE_SHIFT];
3492 index = i & IORING_FILE_TABLE_MASK;
3493 table->files[index] = fget(fd);
Jens Axboe6b063142019-01-10 22:13:58 -07003494
3495 ret = -EBADF;
Jens Axboe65e19f52019-10-26 07:20:21 -06003496 if (!table->files[index])
Jens Axboe6b063142019-01-10 22:13:58 -07003497 break;
3498 /*
3499 * Don't allow io_uring instances to be registered. If UNIX
3500 * isn't enabled, then this causes a reference cycle and this
3501 * instance can never get freed. If UNIX is enabled we'll
3502 * handle it just fine, but there's still no point in allowing
3503 * a ring fd as it doesn't support regular read/write anyway.
3504 */
Jens Axboe65e19f52019-10-26 07:20:21 -06003505 if (table->files[index]->f_op == &io_uring_fops) {
3506 fput(table->files[index]);
Jens Axboe6b063142019-01-10 22:13:58 -07003507 break;
3508 }
Jens Axboe6b063142019-01-10 22:13:58 -07003509 ret = 0;
3510 }
3511
3512 if (ret) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003513 for (i = 0; i < ctx->nr_user_files; i++) {
3514 struct file *file;
Jens Axboe6b063142019-01-10 22:13:58 -07003515
Jens Axboe65e19f52019-10-26 07:20:21 -06003516 file = io_file_from_index(ctx, i);
3517 if (file)
3518 fput(file);
3519 }
3520 for (i = 0; i < nr_tables; i++)
3521 kfree(ctx->file_table[i].files);
3522
3523 kfree(ctx->file_table);
3524 ctx->file_table = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003525 ctx->nr_user_files = 0;
3526 return ret;
3527 }
3528
3529 ret = io_sqe_files_scm(ctx);
3530 if (ret)
3531 io_sqe_files_unregister(ctx);
3532
3533 return ret;
3534}
3535
Jens Axboec3a31e62019-10-03 13:59:56 -06003536static void io_sqe_file_unregister(struct io_ring_ctx *ctx, int index)
3537{
3538#if defined(CONFIG_UNIX)
Jens Axboe65e19f52019-10-26 07:20:21 -06003539 struct file *file = io_file_from_index(ctx, index);
Jens Axboec3a31e62019-10-03 13:59:56 -06003540 struct sock *sock = ctx->ring_sock->sk;
3541 struct sk_buff_head list, *head = &sock->sk_receive_queue;
3542 struct sk_buff *skb;
3543 int i;
3544
3545 __skb_queue_head_init(&list);
3546
3547 /*
3548 * Find the skb that holds this file in its SCM_RIGHTS. When found,
3549 * remove this entry and rearrange the file array.
3550 */
3551 skb = skb_dequeue(head);
3552 while (skb) {
3553 struct scm_fp_list *fp;
3554
3555 fp = UNIXCB(skb).fp;
3556 for (i = 0; i < fp->count; i++) {
3557 int left;
3558
3559 if (fp->fp[i] != file)
3560 continue;
3561
3562 unix_notinflight(fp->user, fp->fp[i]);
3563 left = fp->count - 1 - i;
3564 if (left) {
3565 memmove(&fp->fp[i], &fp->fp[i + 1],
3566 left * sizeof(struct file *));
3567 }
3568 fp->count--;
3569 if (!fp->count) {
3570 kfree_skb(skb);
3571 skb = NULL;
3572 } else {
3573 __skb_queue_tail(&list, skb);
3574 }
3575 fput(file);
3576 file = NULL;
3577 break;
3578 }
3579
3580 if (!file)
3581 break;
3582
3583 __skb_queue_tail(&list, skb);
3584
3585 skb = skb_dequeue(head);
3586 }
3587
3588 if (skb_peek(&list)) {
3589 spin_lock_irq(&head->lock);
3590 while ((skb = __skb_dequeue(&list)) != NULL)
3591 __skb_queue_tail(head, skb);
3592 spin_unlock_irq(&head->lock);
3593 }
3594#else
Jens Axboe65e19f52019-10-26 07:20:21 -06003595 fput(io_file_from_index(ctx, index));
Jens Axboec3a31e62019-10-03 13:59:56 -06003596#endif
3597}
3598
3599static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
3600 int index)
3601{
3602#if defined(CONFIG_UNIX)
3603 struct sock *sock = ctx->ring_sock->sk;
3604 struct sk_buff_head *head = &sock->sk_receive_queue;
3605 struct sk_buff *skb;
3606
3607 /*
3608 * See if we can merge this file into an existing skb SCM_RIGHTS
3609 * file set. If there's no room, fall back to allocating a new skb
3610 * and filling it in.
3611 */
3612 spin_lock_irq(&head->lock);
3613 skb = skb_peek(head);
3614 if (skb) {
3615 struct scm_fp_list *fpl = UNIXCB(skb).fp;
3616
3617 if (fpl->count < SCM_MAX_FD) {
3618 __skb_unlink(skb, head);
3619 spin_unlock_irq(&head->lock);
3620 fpl->fp[fpl->count] = get_file(file);
3621 unix_inflight(fpl->user, fpl->fp[fpl->count]);
3622 fpl->count++;
3623 spin_lock_irq(&head->lock);
3624 __skb_queue_head(head, skb);
3625 } else {
3626 skb = NULL;
3627 }
3628 }
3629 spin_unlock_irq(&head->lock);
3630
3631 if (skb) {
3632 fput(file);
3633 return 0;
3634 }
3635
3636 return __io_sqe_files_scm(ctx, 1, index);
3637#else
3638 return 0;
3639#endif
3640}
3641
3642static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
3643 unsigned nr_args)
3644{
3645 struct io_uring_files_update up;
3646 __s32 __user *fds;
3647 int fd, i, err;
3648 __u32 done;
3649
Jens Axboe65e19f52019-10-26 07:20:21 -06003650 if (!ctx->file_table)
Jens Axboec3a31e62019-10-03 13:59:56 -06003651 return -ENXIO;
3652 if (!nr_args)
3653 return -EINVAL;
3654 if (copy_from_user(&up, arg, sizeof(up)))
3655 return -EFAULT;
3656 if (check_add_overflow(up.offset, nr_args, &done))
3657 return -EOVERFLOW;
3658 if (done > ctx->nr_user_files)
3659 return -EINVAL;
3660
3661 done = 0;
3662 fds = (__s32 __user *) up.fds;
3663 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003664 struct fixed_file_table *table;
3665 unsigned index;
3666
Jens Axboec3a31e62019-10-03 13:59:56 -06003667 err = 0;
3668 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
3669 err = -EFAULT;
3670 break;
3671 }
3672 i = array_index_nospec(up.offset, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06003673 table = &ctx->file_table[i >> IORING_FILE_TABLE_SHIFT];
3674 index = i & IORING_FILE_TABLE_MASK;
3675 if (table->files[index]) {
Jens Axboec3a31e62019-10-03 13:59:56 -06003676 io_sqe_file_unregister(ctx, i);
Jens Axboe65e19f52019-10-26 07:20:21 -06003677 table->files[index] = NULL;
Jens Axboec3a31e62019-10-03 13:59:56 -06003678 }
3679 if (fd != -1) {
3680 struct file *file;
3681
3682 file = fget(fd);
3683 if (!file) {
3684 err = -EBADF;
3685 break;
3686 }
3687 /*
3688 * Don't allow io_uring instances to be registered. If
3689 * UNIX isn't enabled, then this causes a reference
3690 * cycle and this instance can never get freed. If UNIX
3691 * is enabled we'll handle it just fine, but there's
3692 * still no point in allowing a ring fd as it doesn't
3693 * support regular read/write anyway.
3694 */
3695 if (file->f_op == &io_uring_fops) {
3696 fput(file);
3697 err = -EBADF;
3698 break;
3699 }
Jens Axboe65e19f52019-10-26 07:20:21 -06003700 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06003701 err = io_sqe_file_register(ctx, file, i);
3702 if (err)
3703 break;
3704 }
3705 nr_args--;
3706 done++;
3707 up.offset++;
3708 }
3709
3710 return done ? done : err;
3711}
3712
Jens Axboe6c271ce2019-01-10 11:22:30 -07003713static int io_sq_offload_start(struct io_ring_ctx *ctx,
3714 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003715{
Jens Axboe561fb042019-10-24 07:25:42 -06003716 unsigned concurrency;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003717 int ret;
3718
Jens Axboe6c271ce2019-01-10 11:22:30 -07003719 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003720 mmgrab(current->mm);
3721 ctx->sqo_mm = current->mm;
3722
Jens Axboe6c271ce2019-01-10 11:22:30 -07003723 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06003724 ret = -EPERM;
3725 if (!capable(CAP_SYS_ADMIN))
3726 goto err;
3727
Jens Axboe917257d2019-04-13 09:28:55 -06003728 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
3729 if (!ctx->sq_thread_idle)
3730 ctx->sq_thread_idle = HZ;
3731
Jens Axboe6c271ce2019-01-10 11:22:30 -07003732 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06003733 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003734
Jens Axboe917257d2019-04-13 09:28:55 -06003735 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06003736 if (cpu >= nr_cpu_ids)
3737 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08003738 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06003739 goto err;
3740
Jens Axboe6c271ce2019-01-10 11:22:30 -07003741 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
3742 ctx, cpu,
3743 "io_uring-sq");
3744 } else {
3745 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
3746 "io_uring-sq");
3747 }
3748 if (IS_ERR(ctx->sqo_thread)) {
3749 ret = PTR_ERR(ctx->sqo_thread);
3750 ctx->sqo_thread = NULL;
3751 goto err;
3752 }
3753 wake_up_process(ctx->sqo_thread);
3754 } else if (p->flags & IORING_SETUP_SQ_AFF) {
3755 /* Can't have SQ_AFF without SQPOLL */
3756 ret = -EINVAL;
3757 goto err;
3758 }
3759
Jens Axboe561fb042019-10-24 07:25:42 -06003760 /* Do QD, or 4 * CPUS, whatever is smallest */
3761 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
Jens Axboe5f8fd2d32019-11-07 10:57:36 -07003762 ctx->io_wq = io_wq_create(concurrency, ctx->sqo_mm, ctx->user);
Jens Axboe975c99a52019-10-30 08:42:56 -06003763 if (IS_ERR(ctx->io_wq)) {
3764 ret = PTR_ERR(ctx->io_wq);
3765 ctx->io_wq = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003766 goto err;
3767 }
3768
3769 return 0;
3770err:
Jens Axboe54a91f32019-09-10 09:15:04 -06003771 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003772 mmdrop(ctx->sqo_mm);
3773 ctx->sqo_mm = NULL;
3774 return ret;
3775}
3776
3777static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
3778{
3779 atomic_long_sub(nr_pages, &user->locked_vm);
3780}
3781
3782static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
3783{
3784 unsigned long page_limit, cur_pages, new_pages;
3785
3786 /* Don't allow more pages than we can safely lock */
3787 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
3788
3789 do {
3790 cur_pages = atomic_long_read(&user->locked_vm);
3791 new_pages = cur_pages + nr_pages;
3792 if (new_pages > page_limit)
3793 return -ENOMEM;
3794 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
3795 new_pages) != cur_pages);
3796
3797 return 0;
3798}
3799
3800static void io_mem_free(void *ptr)
3801{
Mark Rutland52e04ef2019-04-30 17:30:21 +01003802 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003803
Mark Rutland52e04ef2019-04-30 17:30:21 +01003804 if (!ptr)
3805 return;
3806
3807 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003808 if (put_page_testzero(page))
3809 free_compound_page(page);
3810}
3811
3812static void *io_mem_alloc(size_t size)
3813{
3814 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
3815 __GFP_NORETRY;
3816
3817 return (void *) __get_free_pages(gfp_flags, get_order(size));
3818}
3819
Hristo Venev75b28af2019-08-26 17:23:46 +00003820static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
3821 size_t *sq_offset)
3822{
3823 struct io_rings *rings;
3824 size_t off, sq_array_size;
3825
3826 off = struct_size(rings, cqes, cq_entries);
3827 if (off == SIZE_MAX)
3828 return SIZE_MAX;
3829
3830#ifdef CONFIG_SMP
3831 off = ALIGN(off, SMP_CACHE_BYTES);
3832 if (off == 0)
3833 return SIZE_MAX;
3834#endif
3835
3836 sq_array_size = array_size(sizeof(u32), sq_entries);
3837 if (sq_array_size == SIZE_MAX)
3838 return SIZE_MAX;
3839
3840 if (check_add_overflow(off, sq_array_size, &off))
3841 return SIZE_MAX;
3842
3843 if (sq_offset)
3844 *sq_offset = off;
3845
3846 return off;
3847}
3848
Jens Axboe2b188cc2019-01-07 10:46:33 -07003849static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
3850{
Hristo Venev75b28af2019-08-26 17:23:46 +00003851 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003852
Hristo Venev75b28af2019-08-26 17:23:46 +00003853 pages = (size_t)1 << get_order(
3854 rings_size(sq_entries, cq_entries, NULL));
3855 pages += (size_t)1 << get_order(
3856 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07003857
Hristo Venev75b28af2019-08-26 17:23:46 +00003858 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003859}
3860
Jens Axboeedafcce2019-01-09 09:16:05 -07003861static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
3862{
3863 int i, j;
3864
3865 if (!ctx->user_bufs)
3866 return -ENXIO;
3867
3868 for (i = 0; i < ctx->nr_user_bufs; i++) {
3869 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
3870
3871 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbard27c4d3a2019-08-04 19:32:06 -07003872 put_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07003873
3874 if (ctx->account_mem)
3875 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003876 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07003877 imu->nr_bvecs = 0;
3878 }
3879
3880 kfree(ctx->user_bufs);
3881 ctx->user_bufs = NULL;
3882 ctx->nr_user_bufs = 0;
3883 return 0;
3884}
3885
3886static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
3887 void __user *arg, unsigned index)
3888{
3889 struct iovec __user *src;
3890
3891#ifdef CONFIG_COMPAT
3892 if (ctx->compat) {
3893 struct compat_iovec __user *ciovs;
3894 struct compat_iovec ciov;
3895
3896 ciovs = (struct compat_iovec __user *) arg;
3897 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
3898 return -EFAULT;
3899
3900 dst->iov_base = (void __user *) (unsigned long) ciov.iov_base;
3901 dst->iov_len = ciov.iov_len;
3902 return 0;
3903 }
3904#endif
3905 src = (struct iovec __user *) arg;
3906 if (copy_from_user(dst, &src[index], sizeof(*dst)))
3907 return -EFAULT;
3908 return 0;
3909}
3910
3911static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
3912 unsigned nr_args)
3913{
3914 struct vm_area_struct **vmas = NULL;
3915 struct page **pages = NULL;
3916 int i, j, got_pages = 0;
3917 int ret = -EINVAL;
3918
3919 if (ctx->user_bufs)
3920 return -EBUSY;
3921 if (!nr_args || nr_args > UIO_MAXIOV)
3922 return -EINVAL;
3923
3924 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
3925 GFP_KERNEL);
3926 if (!ctx->user_bufs)
3927 return -ENOMEM;
3928
3929 for (i = 0; i < nr_args; i++) {
3930 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
3931 unsigned long off, start, end, ubuf;
3932 int pret, nr_pages;
3933 struct iovec iov;
3934 size_t size;
3935
3936 ret = io_copy_iov(ctx, &iov, arg, i);
3937 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03003938 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07003939
3940 /*
3941 * Don't impose further limits on the size and buffer
3942 * constraints here, we'll -EINVAL later when IO is
3943 * submitted if they are wrong.
3944 */
3945 ret = -EFAULT;
3946 if (!iov.iov_base || !iov.iov_len)
3947 goto err;
3948
3949 /* arbitrary limit, but we need something */
3950 if (iov.iov_len > SZ_1G)
3951 goto err;
3952
3953 ubuf = (unsigned long) iov.iov_base;
3954 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
3955 start = ubuf >> PAGE_SHIFT;
3956 nr_pages = end - start;
3957
3958 if (ctx->account_mem) {
3959 ret = io_account_mem(ctx->user, nr_pages);
3960 if (ret)
3961 goto err;
3962 }
3963
3964 ret = 0;
3965 if (!pages || nr_pages > got_pages) {
3966 kfree(vmas);
3967 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003968 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07003969 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003970 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07003971 sizeof(struct vm_area_struct *),
3972 GFP_KERNEL);
3973 if (!pages || !vmas) {
3974 ret = -ENOMEM;
3975 if (ctx->account_mem)
3976 io_unaccount_mem(ctx->user, nr_pages);
3977 goto err;
3978 }
3979 got_pages = nr_pages;
3980 }
3981
Mark Rutlandd4ef6472019-05-01 16:59:16 +01003982 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07003983 GFP_KERNEL);
3984 ret = -ENOMEM;
3985 if (!imu->bvec) {
3986 if (ctx->account_mem)
3987 io_unaccount_mem(ctx->user, nr_pages);
3988 goto err;
3989 }
3990
3991 ret = 0;
3992 down_read(&current->mm->mmap_sem);
Ira Weiny932f4a62019-05-13 17:17:03 -07003993 pret = get_user_pages(ubuf, nr_pages,
3994 FOLL_WRITE | FOLL_LONGTERM,
3995 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07003996 if (pret == nr_pages) {
3997 /* don't support file backed memory */
3998 for (j = 0; j < nr_pages; j++) {
3999 struct vm_area_struct *vma = vmas[j];
4000
4001 if (vma->vm_file &&
4002 !is_file_hugepages(vma->vm_file)) {
4003 ret = -EOPNOTSUPP;
4004 break;
4005 }
4006 }
4007 } else {
4008 ret = pret < 0 ? pret : -EFAULT;
4009 }
4010 up_read(&current->mm->mmap_sem);
4011 if (ret) {
4012 /*
4013 * if we did partial map, or found file backed vmas,
4014 * release any pages we did get
4015 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07004016 if (pret > 0)
4017 put_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07004018 if (ctx->account_mem)
4019 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004020 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07004021 goto err;
4022 }
4023
4024 off = ubuf & ~PAGE_MASK;
4025 size = iov.iov_len;
4026 for (j = 0; j < nr_pages; j++) {
4027 size_t vec_len;
4028
4029 vec_len = min_t(size_t, size, PAGE_SIZE - off);
4030 imu->bvec[j].bv_page = pages[j];
4031 imu->bvec[j].bv_len = vec_len;
4032 imu->bvec[j].bv_offset = off;
4033 off = 0;
4034 size -= vec_len;
4035 }
4036 /* store original address for later verification */
4037 imu->ubuf = ubuf;
4038 imu->len = iov.iov_len;
4039 imu->nr_bvecs = nr_pages;
4040
4041 ctx->nr_user_bufs++;
4042 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004043 kvfree(pages);
4044 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07004045 return 0;
4046err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004047 kvfree(pages);
4048 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07004049 io_sqe_buffer_unregister(ctx);
4050 return ret;
4051}
4052
Jens Axboe9b402842019-04-11 11:45:41 -06004053static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
4054{
4055 __s32 __user *fds = arg;
4056 int fd;
4057
4058 if (ctx->cq_ev_fd)
4059 return -EBUSY;
4060
4061 if (copy_from_user(&fd, fds, sizeof(*fds)))
4062 return -EFAULT;
4063
4064 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
4065 if (IS_ERR(ctx->cq_ev_fd)) {
4066 int ret = PTR_ERR(ctx->cq_ev_fd);
4067 ctx->cq_ev_fd = NULL;
4068 return ret;
4069 }
4070
4071 return 0;
4072}
4073
4074static int io_eventfd_unregister(struct io_ring_ctx *ctx)
4075{
4076 if (ctx->cq_ev_fd) {
4077 eventfd_ctx_put(ctx->cq_ev_fd);
4078 ctx->cq_ev_fd = NULL;
4079 return 0;
4080 }
4081
4082 return -ENXIO;
4083}
4084
Jens Axboe2b188cc2019-01-07 10:46:33 -07004085static void io_ring_ctx_free(struct io_ring_ctx *ctx)
4086{
Jens Axboe6b063142019-01-10 22:13:58 -07004087 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004088 if (ctx->sqo_mm)
4089 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07004090
4091 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07004092 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07004093 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06004094 io_eventfd_unregister(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07004095
Jens Axboe2b188cc2019-01-07 10:46:33 -07004096#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07004097 if (ctx->ring_sock) {
4098 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004099 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07004100 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004101#endif
4102
Hristo Venev75b28af2019-08-26 17:23:46 +00004103 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004104 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004105
4106 percpu_ref_exit(&ctx->refs);
4107 if (ctx->account_mem)
4108 io_unaccount_mem(ctx->user,
4109 ring_pages(ctx->sq_entries, ctx->cq_entries));
4110 free_uid(ctx->user);
4111 kfree(ctx);
4112}
4113
4114static __poll_t io_uring_poll(struct file *file, poll_table *wait)
4115{
4116 struct io_ring_ctx *ctx = file->private_data;
4117 __poll_t mask = 0;
4118
4119 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02004120 /*
4121 * synchronizes with barrier from wq_has_sleeper call in
4122 * io_commit_cqring
4123 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004124 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00004125 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
4126 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004127 mask |= EPOLLOUT | EPOLLWRNORM;
yangerkundaa5de52019-09-24 20:53:34 +08004128 if (READ_ONCE(ctx->rings->cq.head) != ctx->cached_cq_tail)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004129 mask |= EPOLLIN | EPOLLRDNORM;
4130
4131 return mask;
4132}
4133
4134static int io_uring_fasync(int fd, struct file *file, int on)
4135{
4136 struct io_ring_ctx *ctx = file->private_data;
4137
4138 return fasync_helper(fd, file, on, &ctx->cq_fasync);
4139}
4140
4141static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
4142{
4143 mutex_lock(&ctx->uring_lock);
4144 percpu_ref_kill(&ctx->refs);
4145 mutex_unlock(&ctx->uring_lock);
4146
Jens Axboe5262f562019-09-17 12:26:57 -06004147 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004148 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06004149
4150 if (ctx->io_wq)
4151 io_wq_cancel_all(ctx->io_wq);
4152
Jens Axboedef596e2019-01-09 08:59:42 -07004153 io_iopoll_reap_events(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004154 io_cqring_overflow_flush(ctx, true);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004155 wait_for_completion(&ctx->ctx_done);
4156 io_ring_ctx_free(ctx);
4157}
4158
4159static int io_uring_release(struct inode *inode, struct file *file)
4160{
4161 struct io_ring_ctx *ctx = file->private_data;
4162
4163 file->private_data = NULL;
4164 io_ring_ctx_wait_and_kill(ctx);
4165 return 0;
4166}
4167
Jens Axboefcb323c2019-10-24 12:39:47 -06004168static void io_uring_cancel_files(struct io_ring_ctx *ctx,
4169 struct files_struct *files)
4170{
4171 struct io_kiocb *req;
4172 DEFINE_WAIT(wait);
4173
4174 while (!list_empty_careful(&ctx->inflight_list)) {
4175 enum io_wq_cancel ret = IO_WQ_CANCEL_NOTFOUND;
4176
4177 spin_lock_irq(&ctx->inflight_lock);
4178 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
4179 if (req->work.files == files) {
4180 ret = io_wq_cancel_work(ctx->io_wq, &req->work);
4181 break;
4182 }
4183 }
4184 if (ret == IO_WQ_CANCEL_RUNNING)
4185 prepare_to_wait(&ctx->inflight_wait, &wait,
4186 TASK_UNINTERRUPTIBLE);
4187
4188 spin_unlock_irq(&ctx->inflight_lock);
4189
4190 /*
4191 * We need to keep going until we get NOTFOUND. We only cancel
4192 * one work at the time.
4193 *
4194 * If we get CANCEL_RUNNING, then wait for a work to complete
4195 * before continuing.
4196 */
4197 if (ret == IO_WQ_CANCEL_OK)
4198 continue;
4199 else if (ret != IO_WQ_CANCEL_RUNNING)
4200 break;
4201 schedule();
4202 }
4203}
4204
4205static int io_uring_flush(struct file *file, void *data)
4206{
4207 struct io_ring_ctx *ctx = file->private_data;
4208
4209 io_uring_cancel_files(ctx, data);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004210 if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) {
4211 io_cqring_overflow_flush(ctx, true);
Jens Axboefcb323c2019-10-24 12:39:47 -06004212 io_wq_cancel_all(ctx->io_wq);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004213 }
Jens Axboefcb323c2019-10-24 12:39:47 -06004214 return 0;
4215}
4216
Jens Axboe2b188cc2019-01-07 10:46:33 -07004217static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
4218{
4219 loff_t offset = (loff_t) vma->vm_pgoff << PAGE_SHIFT;
4220 unsigned long sz = vma->vm_end - vma->vm_start;
4221 struct io_ring_ctx *ctx = file->private_data;
4222 unsigned long pfn;
4223 struct page *page;
4224 void *ptr;
4225
4226 switch (offset) {
4227 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00004228 case IORING_OFF_CQ_RING:
4229 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004230 break;
4231 case IORING_OFF_SQES:
4232 ptr = ctx->sq_sqes;
4233 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004234 default:
4235 return -EINVAL;
4236 }
4237
4238 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07004239 if (sz > page_size(page))
Jens Axboe2b188cc2019-01-07 10:46:33 -07004240 return -EINVAL;
4241
4242 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
4243 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
4244}
4245
4246SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
4247 u32, min_complete, u32, flags, const sigset_t __user *, sig,
4248 size_t, sigsz)
4249{
4250 struct io_ring_ctx *ctx;
4251 long ret = -EBADF;
4252 int submitted = 0;
4253 struct fd f;
4254
Jens Axboe6c271ce2019-01-10 11:22:30 -07004255 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07004256 return -EINVAL;
4257
4258 f = fdget(fd);
4259 if (!f.file)
4260 return -EBADF;
4261
4262 ret = -EOPNOTSUPP;
4263 if (f.file->f_op != &io_uring_fops)
4264 goto out_fput;
4265
4266 ret = -ENXIO;
4267 ctx = f.file->private_data;
4268 if (!percpu_ref_tryget(&ctx->refs))
4269 goto out_fput;
4270
Jens Axboe6c271ce2019-01-10 11:22:30 -07004271 /*
4272 * For SQ polling, the thread will do all submissions and completions.
4273 * Just return the requested submit count, and wake the thread if
4274 * we were asked to.
4275 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06004276 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004277 if (ctx->flags & IORING_SETUP_SQPOLL) {
4278 if (flags & IORING_ENTER_SQ_WAKEUP)
4279 wake_up(&ctx->sqo_wait);
4280 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06004281 } else if (to_submit) {
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004282 struct mm_struct *cur_mm;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004283
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004284 to_submit = min(to_submit, ctx->sq_entries);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004285 mutex_lock(&ctx->uring_lock);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004286 /* already have mm, so io_submit_sqes() won't try to grab it */
4287 cur_mm = ctx->sqo_mm;
4288 submitted = io_submit_sqes(ctx, to_submit, f.file, fd,
4289 &cur_mm, false);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004290 mutex_unlock(&ctx->uring_lock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004291 }
4292 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07004293 unsigned nr_events = 0;
4294
Jens Axboe2b188cc2019-01-07 10:46:33 -07004295 min_complete = min(min_complete, ctx->cq_entries);
4296
Jens Axboedef596e2019-01-09 08:59:42 -07004297 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07004298 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07004299 } else {
4300 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
4301 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004302 }
4303
Pavel Begunkov6805b322019-10-08 02:18:42 +03004304 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004305out_fput:
4306 fdput(f);
4307 return submitted ? submitted : ret;
4308}
4309
4310static const struct file_operations io_uring_fops = {
4311 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06004312 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07004313 .mmap = io_uring_mmap,
4314 .poll = io_uring_poll,
4315 .fasync = io_uring_fasync,
4316};
4317
4318static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
4319 struct io_uring_params *p)
4320{
Hristo Venev75b28af2019-08-26 17:23:46 +00004321 struct io_rings *rings;
4322 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004323
Hristo Venev75b28af2019-08-26 17:23:46 +00004324 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
4325 if (size == SIZE_MAX)
4326 return -EOVERFLOW;
4327
4328 rings = io_mem_alloc(size);
4329 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004330 return -ENOMEM;
4331
Hristo Venev75b28af2019-08-26 17:23:46 +00004332 ctx->rings = rings;
4333 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
4334 rings->sq_ring_mask = p->sq_entries - 1;
4335 rings->cq_ring_mask = p->cq_entries - 1;
4336 rings->sq_ring_entries = p->sq_entries;
4337 rings->cq_ring_entries = p->cq_entries;
4338 ctx->sq_mask = rings->sq_ring_mask;
4339 ctx->cq_mask = rings->cq_ring_mask;
4340 ctx->sq_entries = rings->sq_ring_entries;
4341 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004342
4343 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
4344 if (size == SIZE_MAX)
4345 return -EOVERFLOW;
4346
4347 ctx->sq_sqes = io_mem_alloc(size);
Mark Rutland52e04ef2019-04-30 17:30:21 +01004348 if (!ctx->sq_sqes)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004349 return -ENOMEM;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004350
Jens Axboe2b188cc2019-01-07 10:46:33 -07004351 return 0;
4352}
4353
4354/*
4355 * Allocate an anonymous fd, this is what constitutes the application
4356 * visible backing of an io_uring instance. The application mmaps this
4357 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
4358 * we have to tie this fd to a socket for file garbage collection purposes.
4359 */
4360static int io_uring_get_fd(struct io_ring_ctx *ctx)
4361{
4362 struct file *file;
4363 int ret;
4364
4365#if defined(CONFIG_UNIX)
4366 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
4367 &ctx->ring_sock);
4368 if (ret)
4369 return ret;
4370#endif
4371
4372 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
4373 if (ret < 0)
4374 goto err;
4375
4376 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
4377 O_RDWR | O_CLOEXEC);
4378 if (IS_ERR(file)) {
4379 put_unused_fd(ret);
4380 ret = PTR_ERR(file);
4381 goto err;
4382 }
4383
4384#if defined(CONFIG_UNIX)
4385 ctx->ring_sock->file = file;
Jens Axboe6b063142019-01-10 22:13:58 -07004386 ctx->ring_sock->sk->sk_user_data = ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004387#endif
4388 fd_install(ret, file);
4389 return ret;
4390err:
4391#if defined(CONFIG_UNIX)
4392 sock_release(ctx->ring_sock);
4393 ctx->ring_sock = NULL;
4394#endif
4395 return ret;
4396}
4397
4398static int io_uring_create(unsigned entries, struct io_uring_params *p)
4399{
4400 struct user_struct *user = NULL;
4401 struct io_ring_ctx *ctx;
4402 bool account_mem;
4403 int ret;
4404
4405 if (!entries || entries > IORING_MAX_ENTRIES)
4406 return -EINVAL;
4407
4408 /*
4409 * Use twice as many entries for the CQ ring. It's possible for the
4410 * application to drive a higher depth than the size of the SQ ring,
4411 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06004412 * some flexibility in overcommitting a bit. If the application has
4413 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
4414 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07004415 */
4416 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06004417 if (p->flags & IORING_SETUP_CQSIZE) {
4418 /*
4419 * If IORING_SETUP_CQSIZE is set, we do the same roundup
4420 * to a power-of-two, if it isn't already. We do NOT impose
4421 * any cq vs sq ring sizing.
4422 */
4423 if (p->cq_entries < p->sq_entries || p->cq_entries > IORING_MAX_CQ_ENTRIES)
4424 return -EINVAL;
4425 p->cq_entries = roundup_pow_of_two(p->cq_entries);
4426 } else {
4427 p->cq_entries = 2 * p->sq_entries;
4428 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004429
4430 user = get_uid(current_user());
4431 account_mem = !capable(CAP_IPC_LOCK);
4432
4433 if (account_mem) {
4434 ret = io_account_mem(user,
4435 ring_pages(p->sq_entries, p->cq_entries));
4436 if (ret) {
4437 free_uid(user);
4438 return ret;
4439 }
4440 }
4441
4442 ctx = io_ring_ctx_alloc(p);
4443 if (!ctx) {
4444 if (account_mem)
4445 io_unaccount_mem(user, ring_pages(p->sq_entries,
4446 p->cq_entries));
4447 free_uid(user);
4448 return -ENOMEM;
4449 }
4450 ctx->compat = in_compat_syscall();
4451 ctx->account_mem = account_mem;
4452 ctx->user = user;
4453
4454 ret = io_allocate_scq_urings(ctx, p);
4455 if (ret)
4456 goto err;
4457
Jens Axboe6c271ce2019-01-10 11:22:30 -07004458 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004459 if (ret)
4460 goto err;
4461
Jens Axboe2b188cc2019-01-07 10:46:33 -07004462 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00004463 p->sq_off.head = offsetof(struct io_rings, sq.head);
4464 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
4465 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
4466 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
4467 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
4468 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
4469 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004470
4471 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00004472 p->cq_off.head = offsetof(struct io_rings, cq.head);
4473 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
4474 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
4475 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
4476 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
4477 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06004478
Jens Axboe044c1ab2019-10-28 09:15:33 -06004479 /*
4480 * Install ring fd as the very last thing, so we don't risk someone
4481 * having closed it before we finish setup
4482 */
4483 ret = io_uring_get_fd(ctx);
4484 if (ret < 0)
4485 goto err;
4486
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004487 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004488 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004489 return ret;
4490err:
4491 io_ring_ctx_wait_and_kill(ctx);
4492 return ret;
4493}
4494
4495/*
4496 * Sets up an aio uring context, and returns the fd. Applications asks for a
4497 * ring size, we return the actual sq/cq ring sizes (among other things) in the
4498 * params structure passed in.
4499 */
4500static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
4501{
4502 struct io_uring_params p;
4503 long ret;
4504 int i;
4505
4506 if (copy_from_user(&p, params, sizeof(p)))
4507 return -EFAULT;
4508 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
4509 if (p.resv[i])
4510 return -EINVAL;
4511 }
4512
Jens Axboe6c271ce2019-01-10 11:22:30 -07004513 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe33a107f2019-10-04 12:10:03 -06004514 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE))
Jens Axboe2b188cc2019-01-07 10:46:33 -07004515 return -EINVAL;
4516
4517 ret = io_uring_create(entries, &p);
4518 if (ret < 0)
4519 return ret;
4520
4521 if (copy_to_user(params, &p, sizeof(p)))
4522 return -EFAULT;
4523
4524 return ret;
4525}
4526
4527SYSCALL_DEFINE2(io_uring_setup, u32, entries,
4528 struct io_uring_params __user *, params)
4529{
4530 return io_uring_setup(entries, params);
4531}
4532
Jens Axboeedafcce2019-01-09 09:16:05 -07004533static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
4534 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06004535 __releases(ctx->uring_lock)
4536 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07004537{
4538 int ret;
4539
Jens Axboe35fa71a2019-04-22 10:23:23 -06004540 /*
4541 * We're inside the ring mutex, if the ref is already dying, then
4542 * someone else killed the ctx or is already going through
4543 * io_uring_register().
4544 */
4545 if (percpu_ref_is_dying(&ctx->refs))
4546 return -ENXIO;
4547
Jens Axboeedafcce2019-01-09 09:16:05 -07004548 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06004549
4550 /*
4551 * Drop uring mutex before waiting for references to exit. If another
4552 * thread is currently inside io_uring_enter() it might need to grab
4553 * the uring_lock to make progress. If we hold it here across the drain
4554 * wait, then we can deadlock. It's safe to drop the mutex here, since
4555 * no new references will come in after we've killed the percpu ref.
4556 */
4557 mutex_unlock(&ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07004558 wait_for_completion(&ctx->ctx_done);
Jens Axboeb19062a2019-04-15 10:49:38 -06004559 mutex_lock(&ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07004560
4561 switch (opcode) {
4562 case IORING_REGISTER_BUFFERS:
4563 ret = io_sqe_buffer_register(ctx, arg, nr_args);
4564 break;
4565 case IORING_UNREGISTER_BUFFERS:
4566 ret = -EINVAL;
4567 if (arg || nr_args)
4568 break;
4569 ret = io_sqe_buffer_unregister(ctx);
4570 break;
Jens Axboe6b063142019-01-10 22:13:58 -07004571 case IORING_REGISTER_FILES:
4572 ret = io_sqe_files_register(ctx, arg, nr_args);
4573 break;
4574 case IORING_UNREGISTER_FILES:
4575 ret = -EINVAL;
4576 if (arg || nr_args)
4577 break;
4578 ret = io_sqe_files_unregister(ctx);
4579 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06004580 case IORING_REGISTER_FILES_UPDATE:
4581 ret = io_sqe_files_update(ctx, arg, nr_args);
4582 break;
Jens Axboe9b402842019-04-11 11:45:41 -06004583 case IORING_REGISTER_EVENTFD:
4584 ret = -EINVAL;
4585 if (nr_args != 1)
4586 break;
4587 ret = io_eventfd_register(ctx, arg);
4588 break;
4589 case IORING_UNREGISTER_EVENTFD:
4590 ret = -EINVAL;
4591 if (arg || nr_args)
4592 break;
4593 ret = io_eventfd_unregister(ctx);
4594 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07004595 default:
4596 ret = -EINVAL;
4597 break;
4598 }
4599
4600 /* bring the ctx back to life */
4601 reinit_completion(&ctx->ctx_done);
4602 percpu_ref_reinit(&ctx->refs);
4603 return ret;
4604}
4605
4606SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
4607 void __user *, arg, unsigned int, nr_args)
4608{
4609 struct io_ring_ctx *ctx;
4610 long ret = -EBADF;
4611 struct fd f;
4612
4613 f = fdget(fd);
4614 if (!f.file)
4615 return -EBADF;
4616
4617 ret = -EOPNOTSUPP;
4618 if (f.file->f_op != &io_uring_fops)
4619 goto out_fput;
4620
4621 ctx = f.file->private_data;
4622
4623 mutex_lock(&ctx->uring_lock);
4624 ret = __io_uring_register(ctx, opcode, arg, nr_args);
4625 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004626 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
4627 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07004628out_fput:
4629 fdput(f);
4630 return ret;
4631}
4632
Jens Axboe2b188cc2019-01-07 10:46:33 -07004633static int __init io_uring_init(void)
4634{
4635 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
4636 return 0;
4637};
4638__initcall(io_uring_init);