blob: 2efe1ac7352aa82358ef1d52971ac768e6e7a54d [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 Axboeaa4c3962019-11-29 10:14:00 -070072#include <linux/highmem.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070073
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020074#define CREATE_TRACE_POINTS
75#include <trace/events/io_uring.h>
76
Jens Axboe2b188cc2019-01-07 10:46:33 -070077#include <uapi/linux/io_uring.h>
78
79#include "internal.h"
Jens Axboe561fb042019-10-24 07:25:42 -060080#include "io-wq.h"
Jens Axboe2b188cc2019-01-07 10:46:33 -070081
Daniel Xu5277dea2019-09-14 14:23:45 -070082#define IORING_MAX_ENTRIES 32768
Jens Axboe33a107f2019-10-04 12:10:03 -060083#define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES)
Jens Axboe65e19f52019-10-26 07:20:21 -060084
85/*
86 * Shift of 9 is 512 entries, or exactly one page on 64-bit archs
87 */
88#define IORING_FILE_TABLE_SHIFT 9
89#define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT)
90#define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1)
91#define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE)
Jens Axboe2b188cc2019-01-07 10:46:33 -070092
93struct io_uring {
94 u32 head ____cacheline_aligned_in_smp;
95 u32 tail ____cacheline_aligned_in_smp;
96};
97
Stefan Bühler1e84b972019-04-24 23:54:16 +020098/*
Hristo Venev75b28af2019-08-26 17:23:46 +000099 * This data is shared with the application through the mmap at offsets
100 * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200101 *
102 * The offsets to the member fields are published through struct
103 * io_sqring_offsets when calling io_uring_setup.
104 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000105struct io_rings {
Stefan Bühler1e84b972019-04-24 23:54:16 +0200106 /*
107 * Head and tail offsets into the ring; the offsets need to be
108 * masked to get valid indices.
109 *
Hristo Venev75b28af2019-08-26 17:23:46 +0000110 * The kernel controls head of the sq ring and the tail of the cq ring,
111 * and the application controls tail of the sq ring and the head of the
112 * cq ring.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200113 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000114 struct io_uring sq, cq;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200115 /*
Hristo Venev75b28af2019-08-26 17:23:46 +0000116 * Bitmasks to apply to head and tail offsets (constant, equals
Stefan Bühler1e84b972019-04-24 23:54:16 +0200117 * ring_entries - 1)
118 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000119 u32 sq_ring_mask, cq_ring_mask;
120 /* Ring sizes (constant, power of 2) */
121 u32 sq_ring_entries, cq_ring_entries;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200122 /*
123 * Number of invalid entries dropped by the kernel due to
124 * invalid index stored in array
125 *
126 * Written by the kernel, shouldn't be modified by the
127 * application (i.e. get number of "new events" by comparing to
128 * cached value).
129 *
130 * After a new SQ head value was read by the application this
131 * counter includes all submissions that were dropped reaching
132 * the new SQ head (and possibly more).
133 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000134 u32 sq_dropped;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200135 /*
136 * Runtime flags
137 *
138 * Written by the kernel, shouldn't be modified by the
139 * application.
140 *
141 * The application needs a full memory barrier before checking
142 * for IORING_SQ_NEED_WAKEUP after updating the sq tail.
143 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000144 u32 sq_flags;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200145 /*
146 * Number of completion events lost because the queue was full;
147 * this should be avoided by the application by making sure
148 * there are not more requests pending thatn there is space in
149 * the completion queue.
150 *
151 * Written by the kernel, shouldn't be modified by the
152 * application (i.e. get number of "new events" by comparing to
153 * cached value).
154 *
155 * As completion events come in out of order this counter is not
156 * ordered with any other data.
157 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000158 u32 cq_overflow;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200159 /*
160 * Ring buffer of completion events.
161 *
162 * The kernel writes completion events fresh every time they are
163 * produced, so the application is allowed to modify pending
164 * entries.
165 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000166 struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700167};
168
Jens Axboeedafcce2019-01-09 09:16:05 -0700169struct io_mapped_ubuf {
170 u64 ubuf;
171 size_t len;
172 struct bio_vec *bvec;
173 unsigned int nr_bvecs;
174};
175
Jens Axboe65e19f52019-10-26 07:20:21 -0600176struct fixed_file_table {
177 struct file **files;
Jens Axboe31b51512019-01-18 22:56:34 -0700178};
179
Jens Axboe2b188cc2019-01-07 10:46:33 -0700180struct io_ring_ctx {
181 struct {
182 struct percpu_ref refs;
183 } ____cacheline_aligned_in_smp;
184
185 struct {
186 unsigned int flags;
187 bool compat;
188 bool account_mem;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700189 bool cq_overflow_flushed;
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +0300190 bool drain_next;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700191
Hristo Venev75b28af2019-08-26 17:23:46 +0000192 /*
193 * Ring buffer of indices into array of io_uring_sqe, which is
194 * mmapped by the application using the IORING_OFF_SQES offset.
195 *
196 * This indirection could e.g. be used to assign fixed
197 * io_uring_sqe entries to operations and only submit them to
198 * the queue when needed.
199 *
200 * The kernel modifies neither the indices array nor the entries
201 * array.
202 */
203 u32 *sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700204 unsigned cached_sq_head;
205 unsigned sq_entries;
206 unsigned sq_mask;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700207 unsigned sq_thread_idle;
Jens Axboe498ccd92019-10-25 10:04:25 -0600208 unsigned cached_sq_dropped;
Jens Axboe206aefd2019-11-07 18:27:42 -0700209 atomic_t cached_cq_overflow;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700210 struct io_uring_sqe *sq_sqes;
Jens Axboede0617e2019-04-06 21:51:27 -0600211
212 struct list_head defer_list;
Jens Axboe5262f562019-09-17 12:26:57 -0600213 struct list_head timeout_list;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700214 struct list_head cq_overflow_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700215
Jens Axboefcb323c2019-10-24 12:39:47 -0600216 wait_queue_head_t inflight_wait;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700217 } ____cacheline_aligned_in_smp;
218
Hristo Venev75b28af2019-08-26 17:23:46 +0000219 struct io_rings *rings;
220
Jens Axboe2b188cc2019-01-07 10:46:33 -0700221 /* IO offload */
Jens Axboe561fb042019-10-24 07:25:42 -0600222 struct io_wq *io_wq;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700223 struct task_struct *sqo_thread; /* if using sq thread polling */
224 struct mm_struct *sqo_mm;
225 wait_queue_head_t sqo_wait;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700226
Jens Axboe6b063142019-01-10 22:13:58 -0700227 /*
228 * If used, fixed file set. Writers must ensure that ->refs is dead,
229 * readers must ensure that ->refs is alive as long as the file* is
230 * used. Only updated through io_uring_register(2).
231 */
Jens Axboe65e19f52019-10-26 07:20:21 -0600232 struct fixed_file_table *file_table;
Jens Axboe6b063142019-01-10 22:13:58 -0700233 unsigned nr_user_files;
234
Jens Axboeedafcce2019-01-09 09:16:05 -0700235 /* if used, fixed mapped user buffers */
236 unsigned nr_user_bufs;
237 struct io_mapped_ubuf *user_bufs;
238
Jens Axboe2b188cc2019-01-07 10:46:33 -0700239 struct user_struct *user;
240
Jens Axboe0b8c0ec2019-12-02 08:50:00 -0700241 const struct cred *creds;
Jens Axboe181e4482019-11-25 08:52:30 -0700242
Jens Axboe206aefd2019-11-07 18:27:42 -0700243 /* 0 is for ctx quiesce/reinit/free, 1 is for sqo_thread started */
244 struct completion *completions;
245
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700246 /* if all else fails... */
247 struct io_kiocb *fallback_req;
248
Jens Axboe206aefd2019-11-07 18:27:42 -0700249#if defined(CONFIG_UNIX)
250 struct socket *ring_sock;
251#endif
252
253 struct {
254 unsigned cached_cq_tail;
255 unsigned cq_entries;
256 unsigned cq_mask;
257 atomic_t cq_timeouts;
258 struct wait_queue_head cq_wait;
259 struct fasync_struct *cq_fasync;
260 struct eventfd_ctx *cq_ev_fd;
261 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700262
263 struct {
264 struct mutex uring_lock;
265 wait_queue_head_t wait;
266 } ____cacheline_aligned_in_smp;
267
268 struct {
269 spinlock_t completion_lock;
Jens Axboedef596e2019-01-09 08:59:42 -0700270 bool poll_multi_file;
271 /*
272 * ->poll_list is protected by the ctx->uring_lock for
273 * io_uring instances that don't use IORING_SETUP_SQPOLL.
274 * For SQPOLL, only the single threaded io_sq_thread() will
275 * manipulate the list, hence no extra locking is needed there.
276 */
277 struct list_head poll_list;
Jens Axboeeac406c2019-11-14 12:09:58 -0700278 struct rb_root cancel_tree;
Jens Axboefcb323c2019-10-24 12:39:47 -0600279
280 spinlock_t inflight_lock;
281 struct list_head inflight_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700282 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700283};
284
Jens Axboe09bb8392019-03-13 12:39:28 -0600285/*
286 * First field must be the file pointer in all the
287 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
288 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700289struct io_poll_iocb {
290 struct file *file;
291 struct wait_queue_head *head;
292 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600293 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700294 bool canceled;
Jens Axboee9444752019-11-26 15:02:04 -0700295 struct wait_queue_entry *wait;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700296};
297
Jens Axboead8a48a2019-11-15 08:49:11 -0700298struct io_timeout_data {
299 struct io_kiocb *req;
300 struct hrtimer timer;
301 struct timespec64 ts;
302 enum hrtimer_mode mode;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +0300303 u32 seq_offset;
Jens Axboead8a48a2019-11-15 08:49:11 -0700304};
305
Jens Axboef499a022019-12-02 16:28:46 -0700306struct io_async_connect {
307 struct sockaddr_storage address;
308};
309
Jens Axboe03b12302019-12-02 18:50:25 -0700310struct io_async_msghdr {
311 struct iovec fast_iov[UIO_FASTIOV];
312 struct iovec *iov;
313 struct sockaddr __user *uaddr;
314 struct msghdr msg;
315};
316
Jens Axboef67676d2019-12-02 11:03:47 -0700317struct io_async_rw {
318 struct iovec fast_iov[UIO_FASTIOV];
319 struct iovec *iov;
320 ssize_t nr_segs;
321 ssize_t size;
322};
323
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700324struct io_async_ctx {
325 struct io_uring_sqe sqe;
Jens Axboef67676d2019-12-02 11:03:47 -0700326 union {
327 struct io_async_rw rw;
Jens Axboe03b12302019-12-02 18:50:25 -0700328 struct io_async_msghdr msg;
Jens Axboef499a022019-12-02 16:28:46 -0700329 struct io_async_connect connect;
Jens Axboe2d283902019-12-04 11:08:05 -0700330 struct io_timeout_data timeout;
Jens Axboef67676d2019-12-02 11:03:47 -0700331 };
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700332};
333
Jens Axboe09bb8392019-03-13 12:39:28 -0600334/*
335 * NOTE! Each of the iocb union members has the file pointer
336 * as the first entry in their struct definition. So you can
337 * access the file pointer through any of the sub-structs,
338 * or directly as just 'ki_filp' in this struct.
339 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700340struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700341 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600342 struct file *file;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700343 struct kiocb rw;
344 struct io_poll_iocb poll;
345 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700346
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300347 const struct io_uring_sqe *sqe;
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700348 struct io_async_ctx *io;
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300349 struct file *ring_file;
350 int ring_fd;
351 bool has_user;
352 bool in_async;
353 bool needs_fixed_file;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700354
355 struct io_ring_ctx *ctx;
Jens Axboeeac406c2019-11-14 12:09:58 -0700356 union {
357 struct list_head list;
358 struct rb_node rb_node;
359 };
Jens Axboe9e645e112019-05-10 16:07:28 -0600360 struct list_head link_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700361 unsigned int flags;
Jens Axboec16361c2019-01-17 08:39:48 -0700362 refcount_t refs;
Stefan Bühler8449eed2019-04-27 20:34:19 +0200363#define REQ_F_NOWAIT 1 /* must not punt to workers */
Jens Axboedef596e2019-01-09 08:59:42 -0700364#define REQ_F_IOPOLL_COMPLETED 2 /* polled IO has completed */
Jens Axboe6b063142019-01-10 22:13:58 -0700365#define REQ_F_FIXED_FILE 4 /* ctx owns file */
Jens Axboe4d7dd462019-11-20 13:03:52 -0700366#define REQ_F_LINK_NEXT 8 /* already grabbed next link */
Stefan Bühlere2033e32019-05-11 19:08:01 +0200367#define REQ_F_IO_DRAIN 16 /* drain existing IO first */
368#define REQ_F_IO_DRAINED 32 /* drain done */
Jens Axboe9e645e112019-05-10 16:07:28 -0600369#define REQ_F_LINK 64 /* linked sqes */
Jens Axboe2665abf2019-11-05 12:40:47 -0700370#define REQ_F_LINK_TIMEOUT 128 /* has linked timeout */
Zhengyuan Liuf7b76ac2019-07-16 23:26:14 +0800371#define REQ_F_FAIL_LINK 256 /* fail rest of links */
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +0300372#define REQ_F_DRAIN_LINK 512 /* link should be fully drained */
Jens Axboe5262f562019-09-17 12:26:57 -0600373#define REQ_F_TIMEOUT 1024 /* timeout request */
Jens Axboe491381ce2019-10-17 09:20:46 -0600374#define REQ_F_ISREG 2048 /* regular file */
375#define REQ_F_MUST_PUNT 4096 /* must be punted even for NONBLOCK */
Jens Axboe93bd25b2019-11-11 23:34:31 -0700376#define REQ_F_TIMEOUT_NOSEQ 8192 /* no timeout sequence */
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800377#define REQ_F_INFLIGHT 16384 /* on inflight list */
378#define REQ_F_COMP_LOCKED 32768 /* completion under lock */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700379 u64 user_data;
Jens Axboe9e645e112019-05-10 16:07:28 -0600380 u32 result;
Jens Axboede0617e2019-04-06 21:51:27 -0600381 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700382
Jens Axboefcb323c2019-10-24 12:39:47 -0600383 struct list_head inflight_entry;
384
Jens Axboe561fb042019-10-24 07:25:42 -0600385 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700386};
387
388#define IO_PLUG_THRESHOLD 2
Jens Axboedef596e2019-01-09 08:59:42 -0700389#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700390
Jens Axboe9a56a232019-01-09 09:06:50 -0700391struct io_submit_state {
392 struct blk_plug plug;
393
394 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700395 * io_kiocb alloc cache
396 */
397 void *reqs[IO_IOPOLL_BATCH];
398 unsigned int free_reqs;
399 unsigned int cur_req;
400
401 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700402 * File reference cache
403 */
404 struct file *file;
405 unsigned int fd;
406 unsigned int has_refs;
407 unsigned int used_refs;
408 unsigned int ios_left;
409};
410
Jens Axboe561fb042019-10-24 07:25:42 -0600411static void io_wq_submit_work(struct io_wq_work **workptr);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700412static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liu4fe2c962019-09-09 20:50:40 +0800413static void __io_free_req(struct io_kiocb *req);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800414static void io_put_req(struct io_kiocb *req);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700415static void io_double_put_req(struct io_kiocb *req);
Jens Axboe978db572019-11-14 22:39:04 -0700416static void __io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700417static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
418static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboede0617e2019-04-06 21:51:27 -0600419
Jens Axboe2b188cc2019-01-07 10:46:33 -0700420static struct kmem_cache *req_cachep;
421
422static const struct file_operations io_uring_fops;
423
424struct sock *io_uring_get_socket(struct file *file)
425{
426#if defined(CONFIG_UNIX)
427 if (file->f_op == &io_uring_fops) {
428 struct io_ring_ctx *ctx = file->private_data;
429
430 return ctx->ring_sock->sk;
431 }
432#endif
433 return NULL;
434}
435EXPORT_SYMBOL(io_uring_get_socket);
436
437static void io_ring_ctx_ref_free(struct percpu_ref *ref)
438{
439 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
440
Jens Axboe206aefd2019-11-07 18:27:42 -0700441 complete(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700442}
443
444static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
445{
446 struct io_ring_ctx *ctx;
447
448 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
449 if (!ctx)
450 return NULL;
451
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700452 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
453 if (!ctx->fallback_req)
454 goto err;
455
Jens Axboe206aefd2019-11-07 18:27:42 -0700456 ctx->completions = kmalloc(2 * sizeof(struct completion), GFP_KERNEL);
457 if (!ctx->completions)
458 goto err;
459
Roman Gushchin21482892019-05-07 10:01:48 -0700460 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -0700461 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
462 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700463
464 ctx->flags = p->flags;
465 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700466 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe206aefd2019-11-07 18:27:42 -0700467 init_completion(&ctx->completions[0]);
468 init_completion(&ctx->completions[1]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700469 mutex_init(&ctx->uring_lock);
470 init_waitqueue_head(&ctx->wait);
471 spin_lock_init(&ctx->completion_lock);
Jens Axboedef596e2019-01-09 08:59:42 -0700472 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboeeac406c2019-11-14 12:09:58 -0700473 ctx->cancel_tree = RB_ROOT;
Jens Axboede0617e2019-04-06 21:51:27 -0600474 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -0600475 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -0600476 init_waitqueue_head(&ctx->inflight_wait);
477 spin_lock_init(&ctx->inflight_lock);
478 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700479 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -0700480err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700481 if (ctx->fallback_req)
482 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe206aefd2019-11-07 18:27:42 -0700483 kfree(ctx->completions);
484 kfree(ctx);
485 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700486}
487
Bob Liu9d858b22019-11-13 18:06:25 +0800488static inline bool __req_need_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -0600489{
Jackie Liua197f662019-11-08 08:09:12 -0700490 struct io_ring_ctx *ctx = req->ctx;
491
Jens Axboe498ccd92019-10-25 10:04:25 -0600492 return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped
493 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboede0617e2019-04-06 21:51:27 -0600494}
495
Bob Liu9d858b22019-11-13 18:06:25 +0800496static inline bool req_need_defer(struct io_kiocb *req)
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600497{
Bob Liu9d858b22019-11-13 18:06:25 +0800498 if ((req->flags & (REQ_F_IO_DRAIN|REQ_F_IO_DRAINED)) == REQ_F_IO_DRAIN)
499 return __req_need_defer(req);
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600500
Bob Liu9d858b22019-11-13 18:06:25 +0800501 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600502}
503
504static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -0600505{
506 struct io_kiocb *req;
507
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600508 req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list);
Bob Liu9d858b22019-11-13 18:06:25 +0800509 if (req && !req_need_defer(req)) {
Jens Axboede0617e2019-04-06 21:51:27 -0600510 list_del_init(&req->list);
511 return req;
512 }
513
514 return NULL;
515}
516
Jens Axboe5262f562019-09-17 12:26:57 -0600517static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx)
518{
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600519 struct io_kiocb *req;
520
521 req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list);
Jens Axboe93bd25b2019-11-11 23:34:31 -0700522 if (req) {
523 if (req->flags & REQ_F_TIMEOUT_NOSEQ)
524 return NULL;
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800525 if (!__req_need_defer(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -0700526 list_del_init(&req->list);
527 return req;
528 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600529 }
530
531 return NULL;
Jens Axboe5262f562019-09-17 12:26:57 -0600532}
533
Jens Axboede0617e2019-04-06 21:51:27 -0600534static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700535{
Hristo Venev75b28af2019-08-26 17:23:46 +0000536 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700537
Hristo Venev75b28af2019-08-26 17:23:46 +0000538 if (ctx->cached_cq_tail != READ_ONCE(rings->cq.tail)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700539 /* order cqe stores with ring update */
Hristo Venev75b28af2019-08-26 17:23:46 +0000540 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700541
Jens Axboe2b188cc2019-01-07 10:46:33 -0700542 if (wq_has_sleeper(&ctx->cq_wait)) {
543 wake_up_interruptible(&ctx->cq_wait);
544 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
545 }
546 }
547}
548
Jens Axboe561fb042019-10-24 07:25:42 -0600549static inline bool io_sqe_needs_user(const struct io_uring_sqe *sqe)
Jens Axboe18d9be12019-09-10 09:13:05 -0600550{
Jens Axboe561fb042019-10-24 07:25:42 -0600551 u8 opcode = READ_ONCE(sqe->opcode);
552
553 return !(opcode == IORING_OP_READ_FIXED ||
554 opcode == IORING_OP_WRITE_FIXED);
555}
556
Jens Axboe94ae5e72019-11-14 19:39:52 -0700557static inline bool io_prep_async_work(struct io_kiocb *req,
558 struct io_kiocb **link)
Jens Axboe561fb042019-10-24 07:25:42 -0600559{
560 bool do_hashed = false;
Jens Axboe54a91f32019-09-10 09:15:04 -0600561
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300562 if (req->sqe) {
563 switch (req->sqe->opcode) {
Jens Axboe6cc47d12019-09-18 11:18:23 -0600564 case IORING_OP_WRITEV:
565 case IORING_OP_WRITE_FIXED:
Jens Axboe561fb042019-10-24 07:25:42 -0600566 do_hashed = true;
Jens Axboe5f8fd2d32019-11-07 10:57:36 -0700567 /* fall-through */
568 case IORING_OP_READV:
569 case IORING_OP_READ_FIXED:
570 case IORING_OP_SENDMSG:
571 case IORING_OP_RECVMSG:
572 case IORING_OP_ACCEPT:
573 case IORING_OP_POLL_ADD:
Jens Axboef8e85cf2019-11-23 14:24:24 -0700574 case IORING_OP_CONNECT:
Jens Axboe5f8fd2d32019-11-07 10:57:36 -0700575 /*
576 * We know REQ_F_ISREG is not set on some of these
577 * opcodes, but this enables us to keep the check in
578 * just one place.
579 */
580 if (!(req->flags & REQ_F_ISREG))
581 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe6cc47d12019-09-18 11:18:23 -0600582 break;
583 }
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300584 if (io_sqe_needs_user(req->sqe))
Jens Axboe561fb042019-10-24 07:25:42 -0600585 req->work.flags |= IO_WQ_WORK_NEEDS_USER;
Jens Axboe54a91f32019-09-10 09:15:04 -0600586 }
587
Jens Axboe94ae5e72019-11-14 19:39:52 -0700588 *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -0600589 return do_hashed;
590}
591
Jackie Liua197f662019-11-08 08:09:12 -0700592static inline void io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -0600593{
Jackie Liua197f662019-11-08 08:09:12 -0700594 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe94ae5e72019-11-14 19:39:52 -0700595 struct io_kiocb *link;
596 bool do_hashed;
597
598 do_hashed = io_prep_async_work(req, &link);
Jens Axboe561fb042019-10-24 07:25:42 -0600599
600 trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work,
601 req->flags);
602 if (!do_hashed) {
603 io_wq_enqueue(ctx->io_wq, &req->work);
604 } else {
605 io_wq_enqueue_hashed(ctx->io_wq, &req->work,
606 file_inode(req->file));
607 }
Jens Axboe94ae5e72019-11-14 19:39:52 -0700608
609 if (link)
610 io_queue_linked_timeout(link);
Jens Axboe18d9be12019-09-10 09:13:05 -0600611}
612
Jens Axboe5262f562019-09-17 12:26:57 -0600613static void io_kill_timeout(struct io_kiocb *req)
614{
615 int ret;
616
Jens Axboe2d283902019-12-04 11:08:05 -0700617 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe5262f562019-09-17 12:26:57 -0600618 if (ret != -1) {
619 atomic_inc(&req->ctx->cq_timeouts);
Jens Axboe842f9612019-10-29 12:34:10 -0600620 list_del_init(&req->list);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700621 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800622 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -0600623 }
624}
625
626static void io_kill_timeouts(struct io_ring_ctx *ctx)
627{
628 struct io_kiocb *req, *tmp;
629
630 spin_lock_irq(&ctx->completion_lock);
631 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
632 io_kill_timeout(req);
633 spin_unlock_irq(&ctx->completion_lock);
634}
635
Jens Axboede0617e2019-04-06 21:51:27 -0600636static void io_commit_cqring(struct io_ring_ctx *ctx)
637{
638 struct io_kiocb *req;
639
Jens Axboe5262f562019-09-17 12:26:57 -0600640 while ((req = io_get_timeout_req(ctx)) != NULL)
641 io_kill_timeout(req);
642
Jens Axboede0617e2019-04-06 21:51:27 -0600643 __io_commit_cqring(ctx);
644
645 while ((req = io_get_deferred_req(ctx)) != NULL) {
646 req->flags |= REQ_F_IO_DRAINED;
Jackie Liua197f662019-11-08 08:09:12 -0700647 io_queue_async_work(req);
Jens Axboede0617e2019-04-06 21:51:27 -0600648 }
649}
650
Jens Axboe2b188cc2019-01-07 10:46:33 -0700651static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
652{
Hristo Venev75b28af2019-08-26 17:23:46 +0000653 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700654 unsigned tail;
655
656 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +0200657 /*
658 * writes to the cq entry need to come after reading head; the
659 * control dependency is enough as we're using WRITE_ONCE to
660 * fill the cq entry
661 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000662 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700663 return NULL;
664
665 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +0000666 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -0700667}
668
Jens Axboe8c838782019-03-12 15:48:16 -0600669static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
670{
671 if (waitqueue_active(&ctx->wait))
672 wake_up(&ctx->wait);
673 if (waitqueue_active(&ctx->sqo_wait))
674 wake_up(&ctx->sqo_wait);
Jens Axboe9b402842019-04-11 11:45:41 -0600675 if (ctx->cq_ev_fd)
676 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -0600677}
678
Jens Axboec4a2ed72019-11-21 21:01:26 -0700679/* Returns true if there are no backlogged entries after the flush */
680static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700681{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700682 struct io_rings *rings = ctx->rings;
683 struct io_uring_cqe *cqe;
684 struct io_kiocb *req;
685 unsigned long flags;
686 LIST_HEAD(list);
687
688 if (!force) {
689 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -0700690 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700691 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
692 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -0700693 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700694 }
695
696 spin_lock_irqsave(&ctx->completion_lock, flags);
697
698 /* if force is set, the ring is going away. always drop after that */
699 if (force)
700 ctx->cq_overflow_flushed = true;
701
Jens Axboec4a2ed72019-11-21 21:01:26 -0700702 cqe = NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700703 while (!list_empty(&ctx->cq_overflow_list)) {
704 cqe = io_get_cqring(ctx);
705 if (!cqe && !force)
706 break;
707
708 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
709 list);
710 list_move(&req->list, &list);
711 if (cqe) {
712 WRITE_ONCE(cqe->user_data, req->user_data);
713 WRITE_ONCE(cqe->res, req->result);
714 WRITE_ONCE(cqe->flags, 0);
715 } else {
716 WRITE_ONCE(ctx->rings->cq_overflow,
717 atomic_inc_return(&ctx->cached_cq_overflow));
718 }
719 }
720
721 io_commit_cqring(ctx);
722 spin_unlock_irqrestore(&ctx->completion_lock, flags);
723 io_cqring_ev_posted(ctx);
724
725 while (!list_empty(&list)) {
726 req = list_first_entry(&list, struct io_kiocb, list);
727 list_del(&req->list);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800728 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700729 }
Jens Axboec4a2ed72019-11-21 21:01:26 -0700730
731 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700732}
733
Jens Axboe78e19bb2019-11-06 15:21:34 -0700734static void io_cqring_fill_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700735{
Jens Axboe78e19bb2019-11-06 15:21:34 -0700736 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700737 struct io_uring_cqe *cqe;
738
Jens Axboe78e19bb2019-11-06 15:21:34 -0700739 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -0700740
Jens Axboe2b188cc2019-01-07 10:46:33 -0700741 /*
742 * If we can't get a cq entry, userspace overflowed the
743 * submission (by quite a lot). Increment the overflow count in
744 * the ring.
745 */
746 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700747 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -0700748 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700749 WRITE_ONCE(cqe->res, res);
750 WRITE_ONCE(cqe->flags, 0);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700751 } else if (ctx->cq_overflow_flushed) {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700752 WRITE_ONCE(ctx->rings->cq_overflow,
753 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700754 } else {
755 refcount_inc(&req->refs);
756 req->result = res;
757 list_add_tail(&req->list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700758 }
759}
760
Jens Axboe78e19bb2019-11-06 15:21:34 -0700761static void io_cqring_add_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700762{
Jens Axboe78e19bb2019-11-06 15:21:34 -0700763 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700764 unsigned long flags;
765
766 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700767 io_cqring_fill_event(req, res);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700768 io_commit_cqring(ctx);
769 spin_unlock_irqrestore(&ctx->completion_lock, flags);
770
Jens Axboe8c838782019-03-12 15:48:16 -0600771 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700772}
773
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700774static inline bool io_is_fallback_req(struct io_kiocb *req)
775{
776 return req == (struct io_kiocb *)
777 ((unsigned long) req->ctx->fallback_req & ~1UL);
778}
779
780static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
781{
782 struct io_kiocb *req;
783
784 req = ctx->fallback_req;
785 if (!test_and_set_bit_lock(0, (unsigned long *) ctx->fallback_req))
786 return req;
787
788 return NULL;
789}
790
Jens Axboe2579f912019-01-09 09:10:43 -0700791static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
792 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700793{
Jens Axboefd6fab22019-03-14 16:30:06 -0600794 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700795 struct io_kiocb *req;
796
797 if (!percpu_ref_tryget(&ctx->refs))
798 return NULL;
799
Jens Axboe2579f912019-01-09 09:10:43 -0700800 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -0600801 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -0700802 if (unlikely(!req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700803 goto fallback;
Jens Axboe2579f912019-01-09 09:10:43 -0700804 } else if (!state->free_reqs) {
805 size_t sz;
806 int ret;
807
808 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -0600809 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
810
811 /*
812 * Bulk alloc is all-or-nothing. If we fail to get a batch,
813 * retry single alloc to be on the safe side.
814 */
815 if (unlikely(ret <= 0)) {
816 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
817 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700818 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -0600819 ret = 1;
820 }
Jens Axboe2579f912019-01-09 09:10:43 -0700821 state->free_reqs = ret - 1;
822 state->cur_req = 1;
823 req = state->reqs[0];
824 } else {
825 req = state->reqs[state->cur_req];
826 state->free_reqs--;
827 state->cur_req++;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700828 }
829
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700830got_it:
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700831 req->io = NULL;
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300832 req->ring_file = NULL;
Jens Axboe60c112b2019-06-21 10:20:18 -0600833 req->file = NULL;
Jens Axboe2579f912019-01-09 09:10:43 -0700834 req->ctx = ctx;
835 req->flags = 0;
Jens Axboee65ef562019-03-12 10:16:44 -0600836 /* one is dropped after submission, the other at completion */
837 refcount_set(&req->refs, 2);
Jens Axboe9e645e112019-05-10 16:07:28 -0600838 req->result = 0;
Jens Axboe561fb042019-10-24 07:25:42 -0600839 INIT_IO_WORK(&req->work, io_wq_submit_work);
Jens Axboe2579f912019-01-09 09:10:43 -0700840 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700841fallback:
842 req = io_get_fallback_req(ctx);
843 if (req)
844 goto got_it;
Pavel Begunkov6805b322019-10-08 02:18:42 +0300845 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700846 return NULL;
847}
848
Jens Axboedef596e2019-01-09 08:59:42 -0700849static void io_free_req_many(struct io_ring_ctx *ctx, void **reqs, int *nr)
850{
851 if (*nr) {
852 kmem_cache_free_bulk(req_cachep, *nr, reqs);
Pavel Begunkov6805b322019-10-08 02:18:42 +0300853 percpu_ref_put_many(&ctx->refs, *nr);
Jens Axboedef596e2019-01-09 08:59:42 -0700854 *nr = 0;
855 }
856}
857
Jens Axboe9e645e112019-05-10 16:07:28 -0600858static void __io_free_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700859{
Jens Axboefcb323c2019-10-24 12:39:47 -0600860 struct io_ring_ctx *ctx = req->ctx;
861
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700862 if (req->io)
863 kfree(req->io);
Jens Axboe09bb8392019-03-13 12:39:28 -0600864 if (req->file && !(req->flags & REQ_F_FIXED_FILE))
865 fput(req->file);
Jens Axboefcb323c2019-10-24 12:39:47 -0600866 if (req->flags & REQ_F_INFLIGHT) {
867 unsigned long flags;
868
869 spin_lock_irqsave(&ctx->inflight_lock, flags);
870 list_del(&req->inflight_entry);
871 if (waitqueue_active(&ctx->inflight_wait))
872 wake_up(&ctx->inflight_wait);
873 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
874 }
875 percpu_ref_put(&ctx->refs);
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700876 if (likely(!io_is_fallback_req(req)))
877 kmem_cache_free(req_cachep, req);
878 else
879 clear_bit_unlock(0, (unsigned long *) ctx->fallback_req);
Jens Axboee65ef562019-03-12 10:16:44 -0600880}
881
Jackie Liua197f662019-11-08 08:09:12 -0700882static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -0600883{
Jackie Liua197f662019-11-08 08:09:12 -0700884 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -0700885 int ret;
886
Jens Axboe2d283902019-12-04 11:08:05 -0700887 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe2665abf2019-11-05 12:40:47 -0700888 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -0700889 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -0700890 io_commit_cqring(ctx);
891 req->flags &= ~REQ_F_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +0800892 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -0700893 return true;
894 }
895
896 return false;
897}
898
Jens Axboeba816ad2019-09-28 11:36:45 -0600899static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -0600900{
Jens Axboe2665abf2019-11-05 12:40:47 -0700901 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -0600902 struct io_kiocb *nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -0700903 bool wake_ev = false;
Jens Axboe9e645e112019-05-10 16:07:28 -0600904
Jens Axboe4d7dd462019-11-20 13:03:52 -0700905 /* Already got next link */
906 if (req->flags & REQ_F_LINK_NEXT)
907 return;
908
Jens Axboe9e645e112019-05-10 16:07:28 -0600909 /*
910 * The list should never be empty when we are called here. But could
911 * potentially happen if the chain is messed up, check to be on the
912 * safe side.
913 */
914 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, list);
Jens Axboe2665abf2019-11-05 12:40:47 -0700915 while (nxt) {
Jens Axboe76a46e02019-11-10 23:34:16 -0700916 list_del_init(&nxt->list);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700917
918 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
919 (nxt->flags & REQ_F_TIMEOUT)) {
920 wake_ev |= io_link_cancel_timeout(nxt);
921 nxt = list_first_entry_or_null(&req->link_list,
922 struct io_kiocb, list);
923 req->flags &= ~REQ_F_LINK_TIMEOUT;
924 continue;
925 }
Jens Axboe9e645e112019-05-10 16:07:28 -0600926 if (!list_empty(&req->link_list)) {
927 INIT_LIST_HEAD(&nxt->link_list);
928 list_splice(&req->link_list, &nxt->link_list);
929 nxt->flags |= REQ_F_LINK;
930 }
931
Pavel Begunkovb18fdf72019-11-21 23:21:02 +0300932 *nxtptr = nxt;
Jens Axboe94ae5e72019-11-14 19:39:52 -0700933 break;
Jens Axboe9e645e112019-05-10 16:07:28 -0600934 }
Jens Axboe2665abf2019-11-05 12:40:47 -0700935
Jens Axboe4d7dd462019-11-20 13:03:52 -0700936 req->flags |= REQ_F_LINK_NEXT;
Jens Axboe2665abf2019-11-05 12:40:47 -0700937 if (wake_ev)
938 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -0600939}
940
941/*
942 * Called if REQ_F_LINK is set, and we fail the head request
943 */
944static void io_fail_links(struct io_kiocb *req)
945{
Jens Axboe2665abf2019-11-05 12:40:47 -0700946 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -0600947 struct io_kiocb *link;
Jens Axboe2665abf2019-11-05 12:40:47 -0700948 unsigned long flags;
949
950 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -0600951
952 while (!list_empty(&req->link_list)) {
953 link = list_first_entry(&req->link_list, struct io_kiocb, list);
Jens Axboe2665abf2019-11-05 12:40:47 -0700954 list_del_init(&link->list);
Jens Axboe9e645e112019-05-10 16:07:28 -0600955
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +0200956 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -0700957
958 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300959 link->sqe->opcode == IORING_OP_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -0700960 io_link_cancel_timeout(link);
Jens Axboe2665abf2019-11-05 12:40:47 -0700961 } else {
Jens Axboe78e19bb2019-11-06 15:21:34 -0700962 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe978db572019-11-14 22:39:04 -0700963 __io_double_put_req(link);
Jens Axboe2665abf2019-11-05 12:40:47 -0700964 }
Jens Axboe5d960722019-11-19 15:31:28 -0700965 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -0600966 }
Jens Axboe2665abf2019-11-05 12:40:47 -0700967
968 io_commit_cqring(ctx);
969 spin_unlock_irqrestore(&ctx->completion_lock, flags);
970 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -0600971}
972
Jens Axboe4d7dd462019-11-20 13:03:52 -0700973static void io_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -0600974{
Jens Axboe4d7dd462019-11-20 13:03:52 -0700975 if (likely(!(req->flags & REQ_F_LINK)))
Jens Axboe2665abf2019-11-05 12:40:47 -0700976 return;
Jens Axboe2665abf2019-11-05 12:40:47 -0700977
Jens Axboe9e645e112019-05-10 16:07:28 -0600978 /*
979 * If LINK is set, we have dependent requests in this chain. If we
980 * didn't fail this request, queue the first one up, moving any other
981 * dependencies to the next request. In case of failure, fail the rest
982 * of the chain.
983 */
Jens Axboe2665abf2019-11-05 12:40:47 -0700984 if (req->flags & REQ_F_FAIL_LINK) {
985 io_fail_links(req);
Jens Axboe7c9e7f02019-11-12 08:15:53 -0700986 } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) ==
987 REQ_F_LINK_TIMEOUT) {
Jens Axboe2665abf2019-11-05 12:40:47 -0700988 struct io_ring_ctx *ctx = req->ctx;
989 unsigned long flags;
990
991 /*
992 * If this is a timeout link, we could be racing with the
993 * timeout timer. Grab the completion lock for this case to
Jens Axboe7c9e7f02019-11-12 08:15:53 -0700994 * protect against that.
Jens Axboe2665abf2019-11-05 12:40:47 -0700995 */
996 spin_lock_irqsave(&ctx->completion_lock, flags);
997 io_req_link_next(req, nxt);
998 spin_unlock_irqrestore(&ctx->completion_lock, flags);
999 } else {
1000 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -06001001 }
Jens Axboe4d7dd462019-11-20 13:03:52 -07001002}
Jens Axboe9e645e112019-05-10 16:07:28 -06001003
Jackie Liuc69f8db2019-11-09 11:00:08 +08001004static void io_free_req(struct io_kiocb *req)
1005{
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001006 struct io_kiocb *nxt = NULL;
1007
1008 io_req_find_next(req, &nxt);
Pavel Begunkov70cf9f32019-11-21 23:21:00 +03001009 __io_free_req(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001010
1011 if (nxt)
1012 io_queue_async_work(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08001013}
1014
Jens Axboeba816ad2019-09-28 11:36:45 -06001015/*
1016 * Drop reference to request, return next in chain (if there is one) if this
1017 * was the last reference to this request.
1018 */
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001019__attribute__((nonnull))
Jackie Liuec9c02a2019-11-08 23:50:36 +08001020static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboee65ef562019-03-12 10:16:44 -06001021{
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001022 io_req_find_next(req, nxtptr);
Jens Axboe4d7dd462019-11-20 13:03:52 -07001023
Jens Axboee65ef562019-03-12 10:16:44 -06001024 if (refcount_dec_and_test(&req->refs))
Jens Axboe4d7dd462019-11-20 13:03:52 -07001025 __io_free_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001026}
1027
Jens Axboe2b188cc2019-01-07 10:46:33 -07001028static void io_put_req(struct io_kiocb *req)
1029{
Jens Axboedef596e2019-01-09 08:59:42 -07001030 if (refcount_dec_and_test(&req->refs))
1031 io_free_req(req);
1032}
1033
Jens Axboe978db572019-11-14 22:39:04 -07001034/*
1035 * Must only be used if we don't need to care about links, usually from
1036 * within the completion handling itself.
1037 */
1038static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06001039{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001040 /* drop both submit and complete references */
1041 if (refcount_sub_and_test(2, &req->refs))
1042 __io_free_req(req);
1043}
1044
Jens Axboe978db572019-11-14 22:39:04 -07001045static void io_double_put_req(struct io_kiocb *req)
1046{
1047 /* drop both submit and complete references */
1048 if (refcount_sub_and_test(2, &req->refs))
1049 io_free_req(req);
1050}
1051
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001052static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06001053{
Jens Axboe84f97dc2019-11-06 11:27:53 -07001054 struct io_rings *rings = ctx->rings;
1055
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001056 /*
1057 * noflush == true is from the waitqueue handler, just ensure we wake
1058 * up the task, and the next invocation will flush the entries. We
1059 * cannot safely to it from here.
1060 */
1061 if (noflush && !list_empty(&ctx->cq_overflow_list))
1062 return -1U;
1063
1064 io_cqring_overflow_flush(ctx, false);
1065
Jens Axboea3a0e432019-08-20 11:03:11 -06001066 /* See comment at the top of this file */
1067 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00001068 return READ_ONCE(rings->cq.tail) - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06001069}
1070
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03001071static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1072{
1073 struct io_rings *rings = ctx->rings;
1074
1075 /* make sure SQ entry isn't read before tail */
1076 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1077}
1078
Jens Axboedef596e2019-01-09 08:59:42 -07001079/*
1080 * Find and free completed poll iocbs
1081 */
1082static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
1083 struct list_head *done)
1084{
1085 void *reqs[IO_IOPOLL_BATCH];
1086 struct io_kiocb *req;
Jens Axboe09bb8392019-03-13 12:39:28 -06001087 int to_free;
Jens Axboedef596e2019-01-09 08:59:42 -07001088
Jens Axboe09bb8392019-03-13 12:39:28 -06001089 to_free = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001090 while (!list_empty(done)) {
1091 req = list_first_entry(done, struct io_kiocb, list);
1092 list_del(&req->list);
1093
Jens Axboe78e19bb2019-11-06 15:21:34 -07001094 io_cqring_fill_event(req, req->result);
Jens Axboedef596e2019-01-09 08:59:42 -07001095 (*nr_events)++;
1096
Jens Axboe09bb8392019-03-13 12:39:28 -06001097 if (refcount_dec_and_test(&req->refs)) {
1098 /* If we're not using fixed files, we have to pair the
1099 * completion part with the file put. Use regular
1100 * completions for those, only batch free for fixed
Jens Axboe9e645e112019-05-10 16:07:28 -06001101 * file and non-linked commands.
Jens Axboe09bb8392019-03-13 12:39:28 -06001102 */
Jens Axboe1a6b74f2019-12-02 10:33:15 -07001103 if (((req->flags & (REQ_F_FIXED_FILE|REQ_F_LINK)) ==
1104 REQ_F_FIXED_FILE) && !io_is_fallback_req(req) &&
1105 !req->io) {
Jens Axboe09bb8392019-03-13 12:39:28 -06001106 reqs[to_free++] = req;
1107 if (to_free == ARRAY_SIZE(reqs))
1108 io_free_req_many(ctx, reqs, &to_free);
Jens Axboe6b063142019-01-10 22:13:58 -07001109 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001110 io_free_req(req);
Jens Axboe6b063142019-01-10 22:13:58 -07001111 }
Jens Axboe9a56a232019-01-09 09:06:50 -07001112 }
Jens Axboedef596e2019-01-09 08:59:42 -07001113 }
Jens Axboedef596e2019-01-09 08:59:42 -07001114
Jens Axboe09bb8392019-03-13 12:39:28 -06001115 io_commit_cqring(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07001116 io_free_req_many(ctx, reqs, &to_free);
1117}
1118
1119static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1120 long min)
1121{
1122 struct io_kiocb *req, *tmp;
1123 LIST_HEAD(done);
1124 bool spin;
1125 int ret;
1126
1127 /*
1128 * Only spin for completions if we don't have multiple devices hanging
1129 * off our complete list, and we're under the requested amount.
1130 */
1131 spin = !ctx->poll_multi_file && *nr_events < min;
1132
1133 ret = 0;
1134 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
1135 struct kiocb *kiocb = &req->rw;
1136
1137 /*
1138 * Move completed entries to our local list. If we find a
1139 * request that requires polling, break out and complete
1140 * the done list first, if we have entries there.
1141 */
1142 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
1143 list_move_tail(&req->list, &done);
1144 continue;
1145 }
1146 if (!list_empty(&done))
1147 break;
1148
1149 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1150 if (ret < 0)
1151 break;
1152
1153 if (ret && spin)
1154 spin = false;
1155 ret = 0;
1156 }
1157
1158 if (!list_empty(&done))
1159 io_iopoll_complete(ctx, nr_events, &done);
1160
1161 return ret;
1162}
1163
1164/*
1165 * Poll for a mininum of 'min' events. Note that if min == 0 we consider that a
1166 * non-spinning poll check - we'll still enter the driver poll loop, but only
1167 * as a non-spinning completion check.
1168 */
1169static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
1170 long min)
1171{
Jens Axboe08f54392019-08-21 22:19:11 -06001172 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07001173 int ret;
1174
1175 ret = io_do_iopoll(ctx, nr_events, min);
1176 if (ret < 0)
1177 return ret;
1178 if (!min || *nr_events >= min)
1179 return 0;
1180 }
1181
1182 return 1;
1183}
1184
1185/*
1186 * We can't just wait for polled events to come to us, we have to actively
1187 * find and complete them.
1188 */
1189static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
1190{
1191 if (!(ctx->flags & IORING_SETUP_IOPOLL))
1192 return;
1193
1194 mutex_lock(&ctx->uring_lock);
1195 while (!list_empty(&ctx->poll_list)) {
1196 unsigned int nr_events = 0;
1197
1198 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -06001199
1200 /*
1201 * Ensure we allow local-to-the-cpu processing to take place,
1202 * in this case we need to ensure that we reap all events.
1203 */
1204 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -07001205 }
1206 mutex_unlock(&ctx->uring_lock);
1207}
1208
Jens Axboe2b2ed972019-10-25 10:06:15 -06001209static int __io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1210 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07001211{
Jens Axboe2b2ed972019-10-25 10:06:15 -06001212 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001213
1214 do {
1215 int tmin = 0;
1216
Jens Axboe500f9fb2019-08-19 12:15:59 -06001217 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06001218 * Don't enter poll loop if we already have events pending.
1219 * If we do, we can potentially be spinning for commands that
1220 * already triggered a CQE (eg in error).
1221 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001222 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06001223 break;
1224
1225 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06001226 * If a submit got punted to a workqueue, we can have the
1227 * application entering polling for a command before it gets
1228 * issued. That app will hold the uring_lock for the duration
1229 * of the poll right here, so we need to take a breather every
1230 * now and then to ensure that the issue has a chance to add
1231 * the poll to the issued list. Otherwise we can spin here
1232 * forever, while the workqueue is stuck trying to acquire the
1233 * very same mutex.
1234 */
1235 if (!(++iters & 7)) {
1236 mutex_unlock(&ctx->uring_lock);
1237 mutex_lock(&ctx->uring_lock);
1238 }
1239
Jens Axboedef596e2019-01-09 08:59:42 -07001240 if (*nr_events < min)
1241 tmin = min - *nr_events;
1242
1243 ret = io_iopoll_getevents(ctx, nr_events, tmin);
1244 if (ret <= 0)
1245 break;
1246 ret = 0;
1247 } while (min && !*nr_events && !need_resched());
1248
Jens Axboe2b2ed972019-10-25 10:06:15 -06001249 return ret;
1250}
1251
1252static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1253 long min)
1254{
1255 int ret;
1256
1257 /*
1258 * We disallow the app entering submit/complete with polling, but we
1259 * still need to lock the ring to prevent racing with polled issue
1260 * that got punted to a workqueue.
1261 */
1262 mutex_lock(&ctx->uring_lock);
1263 ret = __io_iopoll_check(ctx, nr_events, min);
Jens Axboe500f9fb2019-08-19 12:15:59 -06001264 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001265 return ret;
1266}
1267
Jens Axboe491381ce2019-10-17 09:20:46 -06001268static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001269{
Jens Axboe491381ce2019-10-17 09:20:46 -06001270 /*
1271 * Tell lockdep we inherited freeze protection from submission
1272 * thread.
1273 */
1274 if (req->flags & REQ_F_ISREG) {
1275 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001276
Jens Axboe491381ce2019-10-17 09:20:46 -06001277 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001278 }
Jens Axboe491381ce2019-10-17 09:20:46 -06001279 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001280}
1281
Jens Axboeba816ad2019-09-28 11:36:45 -06001282static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001283{
1284 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1285
Jens Axboe491381ce2019-10-17 09:20:46 -06001286 if (kiocb->ki_flags & IOCB_WRITE)
1287 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001288
Jens Axboe9e645e112019-05-10 16:07:28 -06001289 if ((req->flags & REQ_F_LINK) && res != req->result)
1290 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001291 io_cqring_add_event(req, res);
Jens Axboeba816ad2019-09-28 11:36:45 -06001292}
1293
1294static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1295{
1296 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1297
1298 io_complete_rw_common(kiocb, res);
Jens Axboee65ef562019-03-12 10:16:44 -06001299 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001300}
1301
Jens Axboeba816ad2019-09-28 11:36:45 -06001302static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res)
1303{
1304 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001305 struct io_kiocb *nxt = NULL;
Jens Axboeba816ad2019-09-28 11:36:45 -06001306
1307 io_complete_rw_common(kiocb, res);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001308 io_put_req_find_next(req, &nxt);
1309
1310 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001311}
1312
Jens Axboedef596e2019-01-09 08:59:42 -07001313static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1314{
1315 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1316
Jens Axboe491381ce2019-10-17 09:20:46 -06001317 if (kiocb->ki_flags & IOCB_WRITE)
1318 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001319
Jens Axboe9e645e112019-05-10 16:07:28 -06001320 if ((req->flags & REQ_F_LINK) && res != req->result)
1321 req->flags |= REQ_F_FAIL_LINK;
1322 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -07001323 if (res != -EAGAIN)
1324 req->flags |= REQ_F_IOPOLL_COMPLETED;
1325}
1326
1327/*
1328 * After the iocb has been issued, it's safe to be found on the poll list.
1329 * Adding the kiocb to the list AFTER submission ensures that we don't
1330 * find it from a io_iopoll_getevents() thread before the issuer is done
1331 * accessing the kiocb cookie.
1332 */
1333static void io_iopoll_req_issued(struct io_kiocb *req)
1334{
1335 struct io_ring_ctx *ctx = req->ctx;
1336
1337 /*
1338 * Track whether we have multiple files in our lists. This will impact
1339 * how we do polling eventually, not spinning if we're on potentially
1340 * different devices.
1341 */
1342 if (list_empty(&ctx->poll_list)) {
1343 ctx->poll_multi_file = false;
1344 } else if (!ctx->poll_multi_file) {
1345 struct io_kiocb *list_req;
1346
1347 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1348 list);
1349 if (list_req->rw.ki_filp != req->rw.ki_filp)
1350 ctx->poll_multi_file = true;
1351 }
1352
1353 /*
1354 * For fast devices, IO may have already completed. If it has, add
1355 * it to the front so we find it first.
1356 */
1357 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1358 list_add(&req->list, &ctx->poll_list);
1359 else
1360 list_add_tail(&req->list, &ctx->poll_list);
1361}
1362
Jens Axboe3d6770f2019-04-13 11:50:54 -06001363static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07001364{
Jens Axboe3d6770f2019-04-13 11:50:54 -06001365 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -07001366 int diff = state->has_refs - state->used_refs;
1367
1368 if (diff)
1369 fput_many(state->file, diff);
1370 state->file = NULL;
1371 }
1372}
1373
1374/*
1375 * Get as many references to a file as we have IOs left in this submission,
1376 * assuming most submissions are for one file, or at least that each file
1377 * has more than one submission.
1378 */
1379static struct file *io_file_get(struct io_submit_state *state, int fd)
1380{
1381 if (!state)
1382 return fget(fd);
1383
1384 if (state->file) {
1385 if (state->fd == fd) {
1386 state->used_refs++;
1387 state->ios_left--;
1388 return state->file;
1389 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06001390 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07001391 }
1392 state->file = fget_many(fd, state->ios_left);
1393 if (!state->file)
1394 return NULL;
1395
1396 state->fd = fd;
1397 state->has_refs = state->ios_left;
1398 state->used_refs = 1;
1399 state->ios_left--;
1400 return state->file;
1401}
1402
Jens Axboe2b188cc2019-01-07 10:46:33 -07001403/*
1404 * If we tracked the file through the SCM inflight mechanism, we could support
1405 * any file. For now, just ensure that anything potentially problematic is done
1406 * inline.
1407 */
1408static bool io_file_supports_async(struct file *file)
1409{
1410 umode_t mode = file_inode(file)->i_mode;
1411
1412 if (S_ISBLK(mode) || S_ISCHR(mode))
1413 return true;
1414 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
1415 return true;
1416
1417 return false;
1418}
1419
Pavel Begunkov267bc902019-11-07 01:41:08 +03001420static int io_prep_rw(struct io_kiocb *req, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001421{
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001422 const struct io_uring_sqe *sqe = req->sqe;
Jens Axboedef596e2019-01-09 08:59:42 -07001423 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001424 struct kiocb *kiocb = &req->rw;
Jens Axboe09bb8392019-03-13 12:39:28 -06001425 unsigned ioprio;
1426 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001427
Jens Axboe09bb8392019-03-13 12:39:28 -06001428 if (!req->file)
1429 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001430
Jens Axboe491381ce2019-10-17 09:20:46 -06001431 if (S_ISREG(file_inode(req->file)->i_mode))
1432 req->flags |= REQ_F_ISREG;
1433
Jens Axboe2b188cc2019-01-07 10:46:33 -07001434 kiocb->ki_pos = READ_ONCE(sqe->off);
1435 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
1436 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
1437
1438 ioprio = READ_ONCE(sqe->ioprio);
1439 if (ioprio) {
1440 ret = ioprio_check_cap(ioprio);
1441 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06001442 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001443
1444 kiocb->ki_ioprio = ioprio;
1445 } else
1446 kiocb->ki_ioprio = get_current_ioprio();
1447
1448 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
1449 if (unlikely(ret))
Jens Axboe09bb8392019-03-13 12:39:28 -06001450 return ret;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001451
1452 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06001453 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
1454 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02001455 req->flags |= REQ_F_NOWAIT;
1456
1457 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001458 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001459
Jens Axboedef596e2019-01-09 08:59:42 -07001460 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07001461 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
1462 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06001463 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001464
Jens Axboedef596e2019-01-09 08:59:42 -07001465 kiocb->ki_flags |= IOCB_HIPRI;
1466 kiocb->ki_complete = io_complete_rw_iopoll;
Jens Axboe6873e0b2019-10-30 13:53:09 -06001467 req->result = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001468 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001469 if (kiocb->ki_flags & IOCB_HIPRI)
1470 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07001471 kiocb->ki_complete = io_complete_rw;
1472 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001473 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001474}
1475
1476static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
1477{
1478 switch (ret) {
1479 case -EIOCBQUEUED:
1480 break;
1481 case -ERESTARTSYS:
1482 case -ERESTARTNOINTR:
1483 case -ERESTARTNOHAND:
1484 case -ERESTART_RESTARTBLOCK:
1485 /*
1486 * We can't just restart the syscall, since previously
1487 * submitted sqes may already be in progress. Just fail this
1488 * IO with EINTR.
1489 */
1490 ret = -EINTR;
1491 /* fall through */
1492 default:
1493 kiocb->ki_complete(kiocb, ret, 0);
1494 }
1495}
1496
Jens Axboeba816ad2019-09-28 11:36:45 -06001497static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt,
1498 bool in_async)
1499{
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001500 if (in_async && ret >= 0 && kiocb->ki_complete == io_complete_rw)
Jens Axboeba816ad2019-09-28 11:36:45 -06001501 *nxt = __io_complete_rw(kiocb, ret);
1502 else
1503 io_rw_done(kiocb, ret);
1504}
1505
Pavel Begunkov7d009162019-11-25 23:14:40 +03001506static ssize_t io_import_fixed(struct io_ring_ctx *ctx, int rw,
1507 const struct io_uring_sqe *sqe,
1508 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07001509{
1510 size_t len = READ_ONCE(sqe->len);
1511 struct io_mapped_ubuf *imu;
1512 unsigned index, buf_index;
1513 size_t offset;
1514 u64 buf_addr;
1515
1516 /* attempt to use fixed buffers without having provided iovecs */
1517 if (unlikely(!ctx->user_bufs))
1518 return -EFAULT;
1519
1520 buf_index = READ_ONCE(sqe->buf_index);
1521 if (unlikely(buf_index >= ctx->nr_user_bufs))
1522 return -EFAULT;
1523
1524 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
1525 imu = &ctx->user_bufs[index];
1526 buf_addr = READ_ONCE(sqe->addr);
1527
1528 /* overflow */
1529 if (buf_addr + len < buf_addr)
1530 return -EFAULT;
1531 /* not inside the mapped region */
1532 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
1533 return -EFAULT;
1534
1535 /*
1536 * May not be a start of buffer, set size appropriately
1537 * and advance us to the beginning.
1538 */
1539 offset = buf_addr - imu->ubuf;
1540 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06001541
1542 if (offset) {
1543 /*
1544 * Don't use iov_iter_advance() here, as it's really slow for
1545 * using the latter parts of a big fixed buffer - it iterates
1546 * over each segment manually. We can cheat a bit here, because
1547 * we know that:
1548 *
1549 * 1) it's a BVEC iter, we set it up
1550 * 2) all bvecs are PAGE_SIZE in size, except potentially the
1551 * first and last bvec
1552 *
1553 * So just find our index, and adjust the iterator afterwards.
1554 * If the offset is within the first bvec (or the whole first
1555 * bvec, just use iov_iter_advance(). This makes it easier
1556 * since we can just skip the first segment, which may not
1557 * be PAGE_SIZE aligned.
1558 */
1559 const struct bio_vec *bvec = imu->bvec;
1560
1561 if (offset <= bvec->bv_len) {
1562 iov_iter_advance(iter, offset);
1563 } else {
1564 unsigned long seg_skip;
1565
1566 /* skip first vec */
1567 offset -= bvec->bv_len;
1568 seg_skip = 1 + (offset >> PAGE_SHIFT);
1569
1570 iter->bvec = bvec + seg_skip;
1571 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02001572 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001573 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001574 }
1575 }
1576
Jens Axboe5e559562019-11-13 16:12:46 -07001577 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001578}
1579
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001580static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
1581 struct iovec **iovec, struct iov_iter *iter)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001582{
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001583 const struct io_uring_sqe *sqe = req->sqe;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001584 void __user *buf = u64_to_user_ptr(READ_ONCE(sqe->addr));
1585 size_t sqe_len = READ_ONCE(sqe->len);
Jens Axboeedafcce2019-01-09 09:16:05 -07001586 u8 opcode;
1587
1588 /*
1589 * We're reading ->opcode for the second time, but the first read
1590 * doesn't care whether it's _FIXED or not, so it doesn't matter
1591 * whether ->opcode changes concurrently. The first read does care
1592 * about whether it is a READ or a WRITE, so we don't trust this read
1593 * for that purpose and instead let the caller pass in the read/write
1594 * flag.
1595 */
1596 opcode = READ_ONCE(sqe->opcode);
Pavel Begunkov7d009162019-11-25 23:14:40 +03001597 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07001598 *iovec = NULL;
Pavel Begunkov7d009162019-11-25 23:14:40 +03001599 return io_import_fixed(req->ctx, rw, sqe, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07001600 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001601
Jens Axboef67676d2019-12-02 11:03:47 -07001602 if (req->io) {
1603 struct io_async_rw *iorw = &req->io->rw;
1604
1605 *iovec = iorw->iov;
1606 iov_iter_init(iter, rw, *iovec, iorw->nr_segs, iorw->size);
1607 if (iorw->iov == iorw->fast_iov)
1608 *iovec = NULL;
1609 return iorw->size;
1610 }
1611
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001612 if (!req->has_user)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001613 return -EFAULT;
1614
1615#ifdef CONFIG_COMPAT
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001616 if (req->ctx->compat)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001617 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
1618 iovec, iter);
1619#endif
1620
1621 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
1622}
1623
Jens Axboe32960612019-09-23 11:05:34 -06001624/*
1625 * For files that don't have ->read_iter() and ->write_iter(), handle them
1626 * by looping over ->read() or ->write() manually.
1627 */
1628static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
1629 struct iov_iter *iter)
1630{
1631 ssize_t ret = 0;
1632
1633 /*
1634 * Don't support polled IO through this interface, and we can't
1635 * support non-blocking either. For the latter, this just causes
1636 * the kiocb to be handled from an async context.
1637 */
1638 if (kiocb->ki_flags & IOCB_HIPRI)
1639 return -EOPNOTSUPP;
1640 if (kiocb->ki_flags & IOCB_NOWAIT)
1641 return -EAGAIN;
1642
1643 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03001644 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06001645 ssize_t nr;
1646
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03001647 if (!iov_iter_is_bvec(iter)) {
1648 iovec = iov_iter_iovec(iter);
1649 } else {
1650 /* fixed buffers import bvec */
1651 iovec.iov_base = kmap(iter->bvec->bv_page)
1652 + iter->iov_offset;
1653 iovec.iov_len = min(iter->count,
1654 iter->bvec->bv_len - iter->iov_offset);
1655 }
1656
Jens Axboe32960612019-09-23 11:05:34 -06001657 if (rw == READ) {
1658 nr = file->f_op->read(file, iovec.iov_base,
1659 iovec.iov_len, &kiocb->ki_pos);
1660 } else {
1661 nr = file->f_op->write(file, iovec.iov_base,
1662 iovec.iov_len, &kiocb->ki_pos);
1663 }
1664
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03001665 if (iov_iter_is_bvec(iter))
1666 kunmap(iter->bvec->bv_page);
1667
Jens Axboe32960612019-09-23 11:05:34 -06001668 if (nr < 0) {
1669 if (!ret)
1670 ret = nr;
1671 break;
1672 }
1673 ret += nr;
1674 if (nr != iovec.iov_len)
1675 break;
1676 iov_iter_advance(iter, nr);
1677 }
1678
1679 return ret;
1680}
1681
Jens Axboef67676d2019-12-02 11:03:47 -07001682static void io_req_map_io(struct io_kiocb *req, ssize_t io_size,
1683 struct iovec *iovec, struct iovec *fast_iov,
1684 struct iov_iter *iter)
1685{
1686 req->io->rw.nr_segs = iter->nr_segs;
1687 req->io->rw.size = io_size;
1688 req->io->rw.iov = iovec;
1689 if (!req->io->rw.iov) {
1690 req->io->rw.iov = req->io->rw.fast_iov;
1691 memcpy(req->io->rw.iov, fast_iov,
1692 sizeof(struct iovec) * iter->nr_segs);
1693 }
1694}
1695
1696static int io_setup_async_io(struct io_kiocb *req, ssize_t io_size,
1697 struct iovec *iovec, struct iovec *fast_iov,
1698 struct iov_iter *iter)
1699{
1700 req->io = kmalloc(sizeof(*req->io), GFP_KERNEL);
1701 if (req->io) {
1702 io_req_map_io(req, io_size, iovec, fast_iov, iter);
1703 memcpy(&req->io->sqe, req->sqe, sizeof(req->io->sqe));
1704 req->sqe = &req->io->sqe;
1705 return 0;
1706 }
1707
1708 return -ENOMEM;
1709}
1710
1711static int io_read_prep(struct io_kiocb *req, struct iovec **iovec,
1712 struct iov_iter *iter, bool force_nonblock)
1713{
1714 ssize_t ret;
1715
1716 ret = io_prep_rw(req, force_nonblock);
1717 if (ret)
1718 return ret;
1719
1720 if (unlikely(!(req->file->f_mode & FMODE_READ)))
1721 return -EBADF;
1722
1723 return io_import_iovec(READ, req, iovec, iter);
1724}
1725
Pavel Begunkov267bc902019-11-07 01:41:08 +03001726static int io_read(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001727 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001728{
1729 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1730 struct kiocb *kiocb = &req->rw;
1731 struct iov_iter iter;
1732 struct file *file;
Jens Axboe31b51512019-01-18 22:56:34 -07001733 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07001734 ssize_t io_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001735
Jens Axboef67676d2019-12-02 11:03:47 -07001736 if (!req->io) {
1737 ret = io_read_prep(req, &iovec, &iter, force_nonblock);
1738 if (ret < 0)
1739 return ret;
1740 } else {
1741 ret = io_import_iovec(READ, req, &iovec, &iter);
1742 if (ret < 0)
1743 return ret;
1744 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001745
Jens Axboef67676d2019-12-02 11:03:47 -07001746 file = req->file;
1747 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06001748 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07001749 req->result = io_size;
1750
1751 /*
1752 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
1753 * we know to async punt it even if it was opened O_NONBLOCK
1754 */
1755 if (force_nonblock && !io_file_supports_async(file)) {
1756 req->flags |= REQ_F_MUST_PUNT;
1757 goto copy_iov;
1758 }
Jens Axboe9e645e112019-05-10 16:07:28 -06001759
Jens Axboe31b51512019-01-18 22:56:34 -07001760 iov_count = iov_iter_count(&iter);
1761 ret = rw_verify_area(READ, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001762 if (!ret) {
1763 ssize_t ret2;
1764
Jens Axboe32960612019-09-23 11:05:34 -06001765 if (file->f_op->read_iter)
1766 ret2 = call_read_iter(file, kiocb, &iter);
1767 else
1768 ret2 = loop_rw_iter(READ, file, kiocb, &iter);
1769
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001770 /*
1771 * In case of a short read, punt to async. This can happen
1772 * if we have data partially cached. Alternatively we can
1773 * return the short read, in which case the application will
1774 * need to issue another SQE and wait for it. That SQE will
1775 * need async punt anyway, so it's more efficient to do it
1776 * here.
1777 */
Jens Axboe491381ce2019-10-17 09:20:46 -06001778 if (force_nonblock && !(req->flags & REQ_F_NOWAIT) &&
1779 (req->flags & REQ_F_ISREG) &&
Jens Axboef67676d2019-12-02 11:03:47 -07001780 ret2 > 0 && ret2 < io_size)
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001781 ret2 = -EAGAIN;
1782 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboef67676d2019-12-02 11:03:47 -07001783 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001784 kiocb_done(kiocb, ret2, nxt, req->in_async);
Jens Axboef67676d2019-12-02 11:03:47 -07001785 } else {
1786copy_iov:
1787 ret = io_setup_async_io(req, io_size, iovec,
1788 inline_vecs, &iter);
1789 if (ret)
1790 goto out_free;
1791 return -EAGAIN;
1792 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001793 }
Jens Axboef67676d2019-12-02 11:03:47 -07001794out_free:
Jens Axboe2b188cc2019-01-07 10:46:33 -07001795 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001796 return ret;
1797}
1798
Jens Axboef67676d2019-12-02 11:03:47 -07001799static int io_write_prep(struct io_kiocb *req, struct iovec **iovec,
1800 struct iov_iter *iter, bool force_nonblock)
1801{
1802 ssize_t ret;
1803
1804 ret = io_prep_rw(req, force_nonblock);
1805 if (ret)
1806 return ret;
1807
1808 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
1809 return -EBADF;
1810
1811 return io_import_iovec(WRITE, req, iovec, iter);
1812}
1813
Pavel Begunkov267bc902019-11-07 01:41:08 +03001814static int io_write(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001815 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001816{
1817 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1818 struct kiocb *kiocb = &req->rw;
1819 struct iov_iter iter;
1820 struct file *file;
Jens Axboe31b51512019-01-18 22:56:34 -07001821 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07001822 ssize_t ret, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001823
Jens Axboef67676d2019-12-02 11:03:47 -07001824 if (!req->io) {
1825 ret = io_write_prep(req, &iovec, &iter, force_nonblock);
1826 if (ret < 0)
1827 return ret;
1828 } else {
1829 ret = io_import_iovec(WRITE, req, &iovec, &iter);
1830 if (ret < 0)
1831 return ret;
1832 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001833
Jens Axboe2b188cc2019-01-07 10:46:33 -07001834 file = kiocb->ki_filp;
Jens Axboef67676d2019-12-02 11:03:47 -07001835 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06001836 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07001837 req->result = io_size;
1838
1839 /*
1840 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
1841 * we know to async punt it even if it was opened O_NONBLOCK
1842 */
1843 if (force_nonblock && !io_file_supports_async(req->file)) {
1844 req->flags |= REQ_F_MUST_PUNT;
1845 goto copy_iov;
1846 }
1847
1848 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT))
1849 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06001850
Jens Axboe31b51512019-01-18 22:56:34 -07001851 iov_count = iov_iter_count(&iter);
Jens Axboe31b51512019-01-18 22:56:34 -07001852 ret = rw_verify_area(WRITE, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001853 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01001854 ssize_t ret2;
1855
Jens Axboe2b188cc2019-01-07 10:46:33 -07001856 /*
1857 * Open-code file_start_write here to grab freeze protection,
1858 * which will be released by another thread in
1859 * io_complete_rw(). Fool lockdep by telling it the lock got
1860 * released so that it doesn't complain about the held lock when
1861 * we return to userspace.
1862 */
Jens Axboe491381ce2019-10-17 09:20:46 -06001863 if (req->flags & REQ_F_ISREG) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001864 __sb_start_write(file_inode(file)->i_sb,
1865 SB_FREEZE_WRITE, true);
1866 __sb_writers_release(file_inode(file)->i_sb,
1867 SB_FREEZE_WRITE);
1868 }
1869 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01001870
Jens Axboe32960612019-09-23 11:05:34 -06001871 if (file->f_op->write_iter)
1872 ret2 = call_write_iter(file, kiocb, &iter);
1873 else
1874 ret2 = loop_rw_iter(WRITE, file, kiocb, &iter);
Jens Axboef67676d2019-12-02 11:03:47 -07001875 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001876 kiocb_done(kiocb, ret2, nxt, req->in_async);
Jens Axboef67676d2019-12-02 11:03:47 -07001877 } else {
1878copy_iov:
1879 ret = io_setup_async_io(req, io_size, iovec,
1880 inline_vecs, &iter);
1881 if (ret)
1882 goto out_free;
1883 return -EAGAIN;
1884 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001885 }
Jens Axboe31b51512019-01-18 22:56:34 -07001886out_free:
Jens Axboe2b188cc2019-01-07 10:46:33 -07001887 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001888 return ret;
1889}
1890
1891/*
1892 * IORING_OP_NOP just posts a completion event, nothing else.
1893 */
Jens Axboe78e19bb2019-11-06 15:21:34 -07001894static int io_nop(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001895{
1896 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001897
Jens Axboedef596e2019-01-09 08:59:42 -07001898 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1899 return -EINVAL;
1900
Jens Axboe78e19bb2019-11-06 15:21:34 -07001901 io_cqring_add_event(req, 0);
Jens Axboee65ef562019-03-12 10:16:44 -06001902 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001903 return 0;
1904}
1905
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001906static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1907{
Jens Axboe6b063142019-01-10 22:13:58 -07001908 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001909
Jens Axboe09bb8392019-03-13 12:39:28 -06001910 if (!req->file)
1911 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001912
Jens Axboe6b063142019-01-10 22:13:58 -07001913 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07001914 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07001915 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001916 return -EINVAL;
1917
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001918 return 0;
1919}
1920
1921static int io_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001922 struct io_kiocb **nxt, bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001923{
1924 loff_t sqe_off = READ_ONCE(sqe->off);
1925 loff_t sqe_len = READ_ONCE(sqe->len);
1926 loff_t end = sqe_off + sqe_len;
1927 unsigned fsync_flags;
1928 int ret;
1929
1930 fsync_flags = READ_ONCE(sqe->fsync_flags);
1931 if (unlikely(fsync_flags & ~IORING_FSYNC_DATASYNC))
1932 return -EINVAL;
1933
1934 ret = io_prep_fsync(req, sqe);
1935 if (ret)
1936 return ret;
1937
1938 /* fsync always requires a blocking context */
1939 if (force_nonblock)
1940 return -EAGAIN;
1941
1942 ret = vfs_fsync_range(req->rw.ki_filp, sqe_off,
1943 end > 0 ? end : LLONG_MAX,
1944 fsync_flags & IORING_FSYNC_DATASYNC);
1945
Jens Axboe9e645e112019-05-10 16:07:28 -06001946 if (ret < 0 && (req->flags & REQ_F_LINK))
1947 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001948 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001949 io_put_req_find_next(req, nxt);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001950 return 0;
1951}
1952
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001953static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1954{
1955 struct io_ring_ctx *ctx = req->ctx;
1956 int ret = 0;
1957
1958 if (!req->file)
1959 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001960
1961 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1962 return -EINVAL;
1963 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
1964 return -EINVAL;
1965
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001966 return ret;
1967}
1968
1969static int io_sync_file_range(struct io_kiocb *req,
1970 const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001971 struct io_kiocb **nxt,
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001972 bool force_nonblock)
1973{
1974 loff_t sqe_off;
1975 loff_t sqe_len;
1976 unsigned flags;
1977 int ret;
1978
1979 ret = io_prep_sfr(req, sqe);
1980 if (ret)
1981 return ret;
1982
1983 /* sync_file_range always requires a blocking context */
1984 if (force_nonblock)
1985 return -EAGAIN;
1986
1987 sqe_off = READ_ONCE(sqe->off);
1988 sqe_len = READ_ONCE(sqe->len);
1989 flags = READ_ONCE(sqe->sync_range_flags);
1990
1991 ret = sync_file_range(req->rw.ki_filp, sqe_off, sqe_len, flags);
1992
Jens Axboe9e645e112019-05-10 16:07:28 -06001993 if (ret < 0 && (req->flags & REQ_F_LINK))
1994 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001995 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001996 io_put_req_find_next(req, nxt);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001997 return 0;
1998}
1999
Jens Axboe03b12302019-12-02 18:50:25 -07002000static int io_sendmsg_prep(struct io_kiocb *req, struct io_async_ctx *io)
Jens Axboeaa1fa282019-04-19 13:38:09 -06002001{
Jens Axboe03b12302019-12-02 18:50:25 -07002002#if defined(CONFIG_NET)
2003 const struct io_uring_sqe *sqe = req->sqe;
2004 struct user_msghdr __user *msg;
2005 unsigned flags;
2006
2007 flags = READ_ONCE(sqe->msg_flags);
2008 msg = (struct user_msghdr __user *)(unsigned long) READ_ONCE(sqe->addr);
2009 return sendmsg_copy_msghdr(&io->msg.msg, msg, flags, &io->msg.iov);
2010#else
2011 return 0;
2012#endif
2013}
2014
2015static int io_sendmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2016 struct io_kiocb **nxt, bool force_nonblock)
2017{
2018#if defined(CONFIG_NET)
2019 struct socket *sock;
2020 int ret;
2021
2022 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2023 return -EINVAL;
2024
2025 sock = sock_from_file(req->file, &ret);
2026 if (sock) {
2027 struct io_async_ctx io, *copy;
2028 struct sockaddr_storage addr;
2029 struct msghdr *kmsg;
2030 unsigned flags;
2031
2032 flags = READ_ONCE(sqe->msg_flags);
2033 if (flags & MSG_DONTWAIT)
2034 req->flags |= REQ_F_NOWAIT;
2035 else if (force_nonblock)
2036 flags |= MSG_DONTWAIT;
2037
2038 if (req->io) {
2039 kmsg = &req->io->msg.msg;
2040 kmsg->msg_name = &addr;
2041 } else {
2042 kmsg = &io.msg.msg;
2043 kmsg->msg_name = &addr;
2044 io.msg.iov = io.msg.fast_iov;
2045 ret = io_sendmsg_prep(req, &io);
2046 if (ret)
2047 goto out;
2048 }
2049
2050 ret = __sys_sendmsg_sock(sock, kmsg, flags);
2051 if (force_nonblock && ret == -EAGAIN) {
2052 copy = kmalloc(sizeof(*copy), GFP_KERNEL);
2053 if (!copy) {
2054 ret = -ENOMEM;
2055 goto out;
2056 }
2057 memcpy(&copy->msg, &io.msg, sizeof(copy->msg));
2058 req->io = copy;
2059 memcpy(&req->io->sqe, req->sqe, sizeof(*req->sqe));
2060 req->sqe = &req->io->sqe;
2061 return ret;
2062 }
2063 if (ret == -ERESTARTSYS)
2064 ret = -EINTR;
2065 }
2066
2067out:
2068 io_cqring_add_event(req, ret);
2069 if (ret < 0 && (req->flags & REQ_F_LINK))
2070 req->flags |= REQ_F_FAIL_LINK;
2071 io_put_req_find_next(req, nxt);
2072 return 0;
2073#else
2074 return -EOPNOTSUPP;
2075#endif
2076}
2077
2078static int io_recvmsg_prep(struct io_kiocb *req, struct io_async_ctx *io)
2079{
2080#if defined(CONFIG_NET)
2081 const struct io_uring_sqe *sqe = req->sqe;
2082 struct user_msghdr __user *msg;
2083 unsigned flags;
2084
2085 flags = READ_ONCE(sqe->msg_flags);
2086 msg = (struct user_msghdr __user *)(unsigned long) READ_ONCE(sqe->addr);
2087 return recvmsg_copy_msghdr(&io->msg.msg, msg, flags, &io->msg.uaddr,
2088 &io->msg.iov);
2089#else
2090 return 0;
2091#endif
2092}
2093
2094static int io_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2095 struct io_kiocb **nxt, bool force_nonblock)
2096{
2097#if defined(CONFIG_NET)
Jens Axboe0fa03c62019-04-19 13:34:07 -06002098 struct socket *sock;
2099 int ret;
2100
2101 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2102 return -EINVAL;
2103
2104 sock = sock_from_file(req->file, &ret);
2105 if (sock) {
2106 struct user_msghdr __user *msg;
Jens Axboe03b12302019-12-02 18:50:25 -07002107 struct io_async_ctx io, *copy;
2108 struct sockaddr_storage addr;
2109 struct msghdr *kmsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002110 unsigned flags;
2111
2112 flags = READ_ONCE(sqe->msg_flags);
2113 if (flags & MSG_DONTWAIT)
2114 req->flags |= REQ_F_NOWAIT;
2115 else if (force_nonblock)
2116 flags |= MSG_DONTWAIT;
2117
2118 msg = (struct user_msghdr __user *) (unsigned long)
2119 READ_ONCE(sqe->addr);
Jens Axboe03b12302019-12-02 18:50:25 -07002120 if (req->io) {
2121 kmsg = &req->io->msg.msg;
2122 kmsg->msg_name = &addr;
2123 } else {
2124 kmsg = &io.msg.msg;
2125 kmsg->msg_name = &addr;
2126 io.msg.iov = io.msg.fast_iov;
2127 ret = io_recvmsg_prep(req, &io);
2128 if (ret)
2129 goto out;
2130 }
Jens Axboe0fa03c62019-04-19 13:34:07 -06002131
Jens Axboe03b12302019-12-02 18:50:25 -07002132 ret = __sys_recvmsg_sock(sock, kmsg, msg, io.msg.uaddr, flags);
2133 if (force_nonblock && ret == -EAGAIN) {
2134 copy = kmalloc(sizeof(*copy), GFP_KERNEL);
2135 if (!copy) {
2136 ret = -ENOMEM;
2137 goto out;
2138 }
2139 memcpy(copy, &io, sizeof(*copy));
2140 req->io = copy;
2141 memcpy(&req->io->sqe, req->sqe, sizeof(*req->sqe));
2142 req->sqe = &req->io->sqe;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002143 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07002144 }
Jens Axboe441cdbd2019-12-02 18:49:10 -07002145 if (ret == -ERESTARTSYS)
2146 ret = -EINTR;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002147 }
2148
Jens Axboe03b12302019-12-02 18:50:25 -07002149out:
Jens Axboe78e19bb2019-11-06 15:21:34 -07002150 io_cqring_add_event(req, ret);
Jens Axboef1f40852019-11-05 20:33:16 -07002151 if (ret < 0 && (req->flags & REQ_F_LINK))
2152 req->flags |= REQ_F_FAIL_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08002153 io_put_req_find_next(req, nxt);
Jens Axboe0fa03c62019-04-19 13:34:07 -06002154 return 0;
2155#else
2156 return -EOPNOTSUPP;
2157#endif
2158}
2159
Jens Axboe17f2fe32019-10-17 14:42:58 -06002160static int io_accept(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2161 struct io_kiocb **nxt, bool force_nonblock)
2162{
2163#if defined(CONFIG_NET)
2164 struct sockaddr __user *addr;
2165 int __user *addr_len;
2166 unsigned file_flags;
2167 int flags, ret;
2168
2169 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
2170 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05002171 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06002172 return -EINVAL;
2173
2174 addr = (struct sockaddr __user *) (unsigned long) READ_ONCE(sqe->addr);
2175 addr_len = (int __user *) (unsigned long) READ_ONCE(sqe->addr2);
2176 flags = READ_ONCE(sqe->accept_flags);
2177 file_flags = force_nonblock ? O_NONBLOCK : 0;
2178
2179 ret = __sys_accept4_file(req->file, file_flags, addr, addr_len, flags);
2180 if (ret == -EAGAIN && force_nonblock) {
2181 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
2182 return -EAGAIN;
2183 }
Jens Axboe8e3cca12019-11-09 19:52:33 -07002184 if (ret == -ERESTARTSYS)
2185 ret = -EINTR;
Jens Axboe17f2fe32019-10-17 14:42:58 -06002186 if (ret < 0 && (req->flags & REQ_F_LINK))
2187 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07002188 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08002189 io_put_req_find_next(req, nxt);
Jens Axboe17f2fe32019-10-17 14:42:58 -06002190 return 0;
2191#else
2192 return -EOPNOTSUPP;
2193#endif
2194}
2195
Jens Axboef499a022019-12-02 16:28:46 -07002196static int io_connect_prep(struct io_kiocb *req, struct io_async_ctx *io)
2197{
2198#if defined(CONFIG_NET)
2199 const struct io_uring_sqe *sqe = req->sqe;
2200 struct sockaddr __user *addr;
2201 int addr_len;
2202
2203 addr = (struct sockaddr __user *) (unsigned long) READ_ONCE(sqe->addr);
2204 addr_len = READ_ONCE(sqe->addr2);
2205 return move_addr_to_kernel(addr, addr_len, &io->connect.address);
2206#else
2207 return 0;
2208#endif
2209}
2210
Jens Axboef8e85cf2019-11-23 14:24:24 -07002211static int io_connect(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2212 struct io_kiocb **nxt, bool force_nonblock)
2213{
2214#if defined(CONFIG_NET)
Jens Axboef499a022019-12-02 16:28:46 -07002215 struct io_async_ctx __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07002216 unsigned file_flags;
2217 int addr_len, ret;
2218
2219 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
2220 return -EINVAL;
2221 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
2222 return -EINVAL;
2223
Jens Axboef8e85cf2019-11-23 14:24:24 -07002224 addr_len = READ_ONCE(sqe->addr2);
2225 file_flags = force_nonblock ? O_NONBLOCK : 0;
2226
Jens Axboef499a022019-12-02 16:28:46 -07002227 if (req->io) {
2228 io = req->io;
2229 } else {
2230 ret = io_connect_prep(req, &__io);
2231 if (ret)
2232 goto out;
2233 io = &__io;
2234 }
2235
2236 ret = __sys_connect_file(req->file, &io->connect.address, addr_len,
2237 file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07002238 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboef499a022019-12-02 16:28:46 -07002239 io = kmalloc(sizeof(*io), GFP_KERNEL);
2240 if (!io) {
2241 ret = -ENOMEM;
2242 goto out;
2243 }
2244 memcpy(&io->connect, &__io.connect, sizeof(io->connect));
2245 req->io = io;
2246 memcpy(&io->sqe, req->sqe, sizeof(*req->sqe));
2247 req->sqe = &io->sqe;
Jens Axboef8e85cf2019-11-23 14:24:24 -07002248 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07002249 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07002250 if (ret == -ERESTARTSYS)
2251 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07002252out:
Jens Axboef8e85cf2019-11-23 14:24:24 -07002253 if (ret < 0 && (req->flags & REQ_F_LINK))
2254 req->flags |= REQ_F_FAIL_LINK;
2255 io_cqring_add_event(req, ret);
2256 io_put_req_find_next(req, nxt);
2257 return 0;
2258#else
2259 return -EOPNOTSUPP;
2260#endif
2261}
2262
Jens Axboeeac406c2019-11-14 12:09:58 -07002263static inline void io_poll_remove_req(struct io_kiocb *req)
2264{
2265 if (!RB_EMPTY_NODE(&req->rb_node)) {
2266 rb_erase(&req->rb_node, &req->ctx->cancel_tree);
2267 RB_CLEAR_NODE(&req->rb_node);
2268 }
2269}
2270
Jens Axboe221c5eb2019-01-17 09:41:58 -07002271static void io_poll_remove_one(struct io_kiocb *req)
2272{
2273 struct io_poll_iocb *poll = &req->poll;
2274
2275 spin_lock(&poll->head->lock);
2276 WRITE_ONCE(poll->canceled, true);
Jens Axboee9444752019-11-26 15:02:04 -07002277 if (!list_empty(&poll->wait->entry)) {
2278 list_del_init(&poll->wait->entry);
Jackie Liua197f662019-11-08 08:09:12 -07002279 io_queue_async_work(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002280 }
2281 spin_unlock(&poll->head->lock);
Jens Axboeeac406c2019-11-14 12:09:58 -07002282 io_poll_remove_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002283}
2284
2285static void io_poll_remove_all(struct io_ring_ctx *ctx)
2286{
Jens Axboeeac406c2019-11-14 12:09:58 -07002287 struct rb_node *node;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002288 struct io_kiocb *req;
2289
2290 spin_lock_irq(&ctx->completion_lock);
Jens Axboeeac406c2019-11-14 12:09:58 -07002291 while ((node = rb_first(&ctx->cancel_tree)) != NULL) {
2292 req = rb_entry(node, struct io_kiocb, rb_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002293 io_poll_remove_one(req);
2294 }
2295 spin_unlock_irq(&ctx->completion_lock);
2296}
2297
Jens Axboe47f46762019-11-09 17:43:02 -07002298static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
2299{
Jens Axboeeac406c2019-11-14 12:09:58 -07002300 struct rb_node *p, *parent = NULL;
Jens Axboe47f46762019-11-09 17:43:02 -07002301 struct io_kiocb *req;
2302
Jens Axboeeac406c2019-11-14 12:09:58 -07002303 p = ctx->cancel_tree.rb_node;
2304 while (p) {
2305 parent = p;
2306 req = rb_entry(parent, struct io_kiocb, rb_node);
2307 if (sqe_addr < req->user_data) {
2308 p = p->rb_left;
2309 } else if (sqe_addr > req->user_data) {
2310 p = p->rb_right;
2311 } else {
2312 io_poll_remove_one(req);
2313 return 0;
2314 }
Jens Axboe47f46762019-11-09 17:43:02 -07002315 }
2316
2317 return -ENOENT;
2318}
2319
Jens Axboe221c5eb2019-01-17 09:41:58 -07002320/*
2321 * Find a running poll command that matches one specified in sqe->addr,
2322 * and remove it if found.
2323 */
2324static int io_poll_remove(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2325{
2326 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07002327 int ret;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002328
2329 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2330 return -EINVAL;
2331 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
2332 sqe->poll_events)
2333 return -EINVAL;
2334
2335 spin_lock_irq(&ctx->completion_lock);
Jens Axboe47f46762019-11-09 17:43:02 -07002336 ret = io_poll_cancel(ctx, READ_ONCE(sqe->addr));
Jens Axboe221c5eb2019-01-17 09:41:58 -07002337 spin_unlock_irq(&ctx->completion_lock);
2338
Jens Axboe78e19bb2019-11-06 15:21:34 -07002339 io_cqring_add_event(req, ret);
Jens Axboef1f40852019-11-05 20:33:16 -07002340 if (ret < 0 && (req->flags & REQ_F_LINK))
2341 req->flags |= REQ_F_FAIL_LINK;
Jens Axboee65ef562019-03-12 10:16:44 -06002342 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002343 return 0;
2344}
2345
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002346static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002347{
Jackie Liua197f662019-11-08 08:09:12 -07002348 struct io_ring_ctx *ctx = req->ctx;
2349
Jens Axboe8c838782019-03-12 15:48:16 -06002350 req->poll.done = true;
Jens Axboee9444752019-11-26 15:02:04 -07002351 kfree(req->poll.wait);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002352 if (error)
2353 io_cqring_fill_event(req, error);
2354 else
2355 io_cqring_fill_event(req, mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06002356 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002357}
2358
Jens Axboe561fb042019-10-24 07:25:42 -06002359static void io_poll_complete_work(struct io_wq_work **workptr)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002360{
Jens Axboe561fb042019-10-24 07:25:42 -06002361 struct io_wq_work *work = *workptr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002362 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
2363 struct io_poll_iocb *poll = &req->poll;
2364 struct poll_table_struct pt = { ._key = poll->events };
2365 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe89723d02019-11-05 15:32:58 -07002366 struct io_kiocb *nxt = NULL;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002367 __poll_t mask = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002368 int ret = 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002369
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002370 if (work->flags & IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06002371 WRITE_ONCE(poll->canceled, true);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002372 ret = -ECANCELED;
2373 } else if (READ_ONCE(poll->canceled)) {
2374 ret = -ECANCELED;
2375 }
Jens Axboe561fb042019-10-24 07:25:42 -06002376
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002377 if (ret != -ECANCELED)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002378 mask = vfs_poll(poll->file, &pt) & poll->events;
2379
2380 /*
2381 * Note that ->ki_cancel callers also delete iocb from active_reqs after
2382 * calling ->ki_cancel. We need the ctx_lock roundtrip here to
2383 * synchronize with them. In the cancellation case the list_del_init
2384 * itself is not actually needed, but harmless so we keep it in to
2385 * avoid further branches in the fast path.
2386 */
2387 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002388 if (!mask && ret != -ECANCELED) {
Jens Axboee9444752019-11-26 15:02:04 -07002389 add_wait_queue(poll->head, poll->wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002390 spin_unlock_irq(&ctx->completion_lock);
2391 return;
2392 }
Jens Axboeeac406c2019-11-14 12:09:58 -07002393 io_poll_remove_req(req);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002394 io_poll_complete(req, mask, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002395 spin_unlock_irq(&ctx->completion_lock);
2396
Jens Axboe8c838782019-03-12 15:48:16 -06002397 io_cqring_ev_posted(ctx);
Jens Axboe89723d02019-11-05 15:32:58 -07002398
Jens Axboefba38c22019-11-18 12:27:57 -07002399 if (ret < 0 && req->flags & REQ_F_LINK)
2400 req->flags |= REQ_F_FAIL_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08002401 io_put_req_find_next(req, &nxt);
Jens Axboe89723d02019-11-05 15:32:58 -07002402 if (nxt)
2403 *workptr = &nxt->work;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002404}
2405
2406static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
2407 void *key)
2408{
Jens Axboee9444752019-11-26 15:02:04 -07002409 struct io_poll_iocb *poll = wait->private;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002410 struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
2411 struct io_ring_ctx *ctx = req->ctx;
2412 __poll_t mask = key_to_poll(key);
Jens Axboe8c838782019-03-12 15:48:16 -06002413 unsigned long flags;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002414
2415 /* for instances that support it check for an event match first: */
Jens Axboe8c838782019-03-12 15:48:16 -06002416 if (mask && !(mask & poll->events))
2417 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002418
Jens Axboee9444752019-11-26 15:02:04 -07002419 list_del_init(&poll->wait->entry);
Jens Axboe8c838782019-03-12 15:48:16 -06002420
Jens Axboe7c9e7f02019-11-12 08:15:53 -07002421 /*
2422 * Run completion inline if we can. We're using trylock here because
2423 * we are violating the completion_lock -> poll wq lock ordering.
2424 * If we have a link timeout we're going to need the completion_lock
2425 * for finalizing the request, mark us as having grabbed that already.
2426 */
Jens Axboe8c838782019-03-12 15:48:16 -06002427 if (mask && spin_trylock_irqsave(&ctx->completion_lock, flags)) {
Jens Axboeeac406c2019-11-14 12:09:58 -07002428 io_poll_remove_req(req);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002429 io_poll_complete(req, mask, 0);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07002430 req->flags |= REQ_F_COMP_LOCKED;
2431 io_put_req(req);
Jens Axboe8c838782019-03-12 15:48:16 -06002432 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2433
2434 io_cqring_ev_posted(ctx);
Jens Axboe8c838782019-03-12 15:48:16 -06002435 } else {
Jackie Liua197f662019-11-08 08:09:12 -07002436 io_queue_async_work(req);
Jens Axboe8c838782019-03-12 15:48:16 -06002437 }
2438
Jens Axboe221c5eb2019-01-17 09:41:58 -07002439 return 1;
2440}
2441
2442struct io_poll_table {
2443 struct poll_table_struct pt;
2444 struct io_kiocb *req;
2445 int error;
2446};
2447
2448static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
2449 struct poll_table_struct *p)
2450{
2451 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
2452
2453 if (unlikely(pt->req->poll.head)) {
2454 pt->error = -EINVAL;
2455 return;
2456 }
2457
2458 pt->error = 0;
2459 pt->req->poll.head = head;
Jens Axboee9444752019-11-26 15:02:04 -07002460 add_wait_queue(head, pt->req->poll.wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002461}
2462
Jens Axboeeac406c2019-11-14 12:09:58 -07002463static void io_poll_req_insert(struct io_kiocb *req)
2464{
2465 struct io_ring_ctx *ctx = req->ctx;
2466 struct rb_node **p = &ctx->cancel_tree.rb_node;
2467 struct rb_node *parent = NULL;
2468 struct io_kiocb *tmp;
2469
2470 while (*p) {
2471 parent = *p;
2472 tmp = rb_entry(parent, struct io_kiocb, rb_node);
2473 if (req->user_data < tmp->user_data)
2474 p = &(*p)->rb_left;
2475 else
2476 p = &(*p)->rb_right;
2477 }
2478 rb_link_node(&req->rb_node, parent, p);
2479 rb_insert_color(&req->rb_node, &ctx->cancel_tree);
2480}
2481
Jens Axboe89723d02019-11-05 15:32:58 -07002482static int io_poll_add(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2483 struct io_kiocb **nxt)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002484{
2485 struct io_poll_iocb *poll = &req->poll;
2486 struct io_ring_ctx *ctx = req->ctx;
2487 struct io_poll_table ipt;
Jens Axboe8c838782019-03-12 15:48:16 -06002488 bool cancel = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002489 __poll_t mask;
2490 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002491
2492 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2493 return -EINVAL;
2494 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
2495 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06002496 if (!poll->file)
2497 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002498
Jens Axboee9444752019-11-26 15:02:04 -07002499 poll->wait = kmalloc(sizeof(*poll->wait), GFP_KERNEL);
2500 if (!poll->wait)
2501 return -ENOMEM;
2502
Jens Axboe1a6b74f2019-12-02 10:33:15 -07002503 req->io = NULL;
Jens Axboe561fb042019-10-24 07:25:42 -06002504 INIT_IO_WORK(&req->work, io_poll_complete_work);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002505 events = READ_ONCE(sqe->poll_events);
2506 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
Jens Axboeeac406c2019-11-14 12:09:58 -07002507 RB_CLEAR_NODE(&req->rb_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002508
Jens Axboe221c5eb2019-01-17 09:41:58 -07002509 poll->head = NULL;
Jens Axboe8c838782019-03-12 15:48:16 -06002510 poll->done = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002511 poll->canceled = false;
2512
2513 ipt.pt._qproc = io_poll_queue_proc;
2514 ipt.pt._key = poll->events;
2515 ipt.req = req;
2516 ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
2517
2518 /* initialized the list so that we can do list_empty checks */
Jens Axboee9444752019-11-26 15:02:04 -07002519 INIT_LIST_HEAD(&poll->wait->entry);
2520 init_waitqueue_func_entry(poll->wait, io_poll_wake);
2521 poll->wait->private = poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002522
Jens Axboe36703242019-07-25 10:20:18 -06002523 INIT_LIST_HEAD(&req->list);
2524
Jens Axboe221c5eb2019-01-17 09:41:58 -07002525 mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002526
2527 spin_lock_irq(&ctx->completion_lock);
Jens Axboe8c838782019-03-12 15:48:16 -06002528 if (likely(poll->head)) {
2529 spin_lock(&poll->head->lock);
Jens Axboee9444752019-11-26 15:02:04 -07002530 if (unlikely(list_empty(&poll->wait->entry))) {
Jens Axboe8c838782019-03-12 15:48:16 -06002531 if (ipt.error)
2532 cancel = true;
2533 ipt.error = 0;
2534 mask = 0;
2535 }
2536 if (mask || ipt.error)
Jens Axboee9444752019-11-26 15:02:04 -07002537 list_del_init(&poll->wait->entry);
Jens Axboe8c838782019-03-12 15:48:16 -06002538 else if (cancel)
2539 WRITE_ONCE(poll->canceled, true);
2540 else if (!poll->done) /* actually waiting for an event */
Jens Axboeeac406c2019-11-14 12:09:58 -07002541 io_poll_req_insert(req);
Jens Axboe8c838782019-03-12 15:48:16 -06002542 spin_unlock(&poll->head->lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002543 }
Jens Axboe8c838782019-03-12 15:48:16 -06002544 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06002545 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002546 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06002547 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07002548 spin_unlock_irq(&ctx->completion_lock);
2549
Jens Axboe8c838782019-03-12 15:48:16 -06002550 if (mask) {
2551 io_cqring_ev_posted(ctx);
Jackie Liuec9c02a2019-11-08 23:50:36 +08002552 io_put_req_find_next(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002553 }
Jens Axboe8c838782019-03-12 15:48:16 -06002554 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002555}
2556
Jens Axboe5262f562019-09-17 12:26:57 -06002557static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
2558{
Jens Axboead8a48a2019-11-15 08:49:11 -07002559 struct io_timeout_data *data = container_of(timer,
2560 struct io_timeout_data, timer);
2561 struct io_kiocb *req = data->req;
2562 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06002563 unsigned long flags;
2564
Jens Axboe5262f562019-09-17 12:26:57 -06002565 atomic_inc(&ctx->cq_timeouts);
2566
2567 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08002568 /*
Jens Axboe11365042019-10-16 09:08:32 -06002569 * We could be racing with timeout deletion. If the list is empty,
2570 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08002571 */
Jens Axboe842f9612019-10-29 12:34:10 -06002572 if (!list_empty(&req->list)) {
Jens Axboe11365042019-10-16 09:08:32 -06002573 struct io_kiocb *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06002574
Jens Axboe11365042019-10-16 09:08:32 -06002575 /*
2576 * Adjust the reqs sequence before the current one because it
2577 * will consume a slot in the cq_ring and the the cq_tail
2578 * pointer will be increased, otherwise other timeout reqs may
2579 * return in advance without waiting for enough wait_nr.
2580 */
2581 prev = req;
2582 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
2583 prev->sequence++;
Jens Axboe11365042019-10-16 09:08:32 -06002584 list_del_init(&req->list);
Jens Axboe11365042019-10-16 09:08:32 -06002585 }
Jens Axboe842f9612019-10-29 12:34:10 -06002586
Jens Axboe78e19bb2019-11-06 15:21:34 -07002587 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06002588 io_commit_cqring(ctx);
2589 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2590
2591 io_cqring_ev_posted(ctx);
Jens Axboef1f40852019-11-05 20:33:16 -07002592 if (req->flags & REQ_F_LINK)
2593 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe5262f562019-09-17 12:26:57 -06002594 io_put_req(req);
2595 return HRTIMER_NORESTART;
2596}
2597
Jens Axboe47f46762019-11-09 17:43:02 -07002598static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
2599{
2600 struct io_kiocb *req;
2601 int ret = -ENOENT;
2602
2603 list_for_each_entry(req, &ctx->timeout_list, list) {
2604 if (user_data == req->user_data) {
2605 list_del_init(&req->list);
2606 ret = 0;
2607 break;
2608 }
2609 }
2610
2611 if (ret == -ENOENT)
2612 return ret;
2613
Jens Axboe2d283902019-12-04 11:08:05 -07002614 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe47f46762019-11-09 17:43:02 -07002615 if (ret == -1)
2616 return -EALREADY;
2617
Jens Axboefba38c22019-11-18 12:27:57 -07002618 if (req->flags & REQ_F_LINK)
2619 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe47f46762019-11-09 17:43:02 -07002620 io_cqring_fill_event(req, -ECANCELED);
2621 io_put_req(req);
2622 return 0;
2623}
2624
Jens Axboe11365042019-10-16 09:08:32 -06002625/*
2626 * Remove or update an existing timeout command
2627 */
2628static int io_timeout_remove(struct io_kiocb *req,
2629 const struct io_uring_sqe *sqe)
2630{
2631 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe11365042019-10-16 09:08:32 -06002632 unsigned flags;
Jens Axboe47f46762019-11-09 17:43:02 -07002633 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06002634
2635 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2636 return -EINVAL;
2637 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
2638 return -EINVAL;
2639 flags = READ_ONCE(sqe->timeout_flags);
2640 if (flags)
2641 return -EINVAL;
2642
Jens Axboe11365042019-10-16 09:08:32 -06002643 spin_lock_irq(&ctx->completion_lock);
Jens Axboe47f46762019-11-09 17:43:02 -07002644 ret = io_timeout_cancel(ctx, READ_ONCE(sqe->addr));
Jens Axboe11365042019-10-16 09:08:32 -06002645
Jens Axboe47f46762019-11-09 17:43:02 -07002646 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06002647 io_commit_cqring(ctx);
2648 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06002649 io_cqring_ev_posted(ctx);
Jens Axboe47f46762019-11-09 17:43:02 -07002650 if (ret < 0 && req->flags & REQ_F_LINK)
2651 req->flags |= REQ_F_FAIL_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08002652 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06002653 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06002654}
2655
Jens Axboe2d283902019-12-04 11:08:05 -07002656static int io_timeout_prep(struct io_kiocb *req, struct io_async_ctx *io,
2657 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06002658{
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002659 const struct io_uring_sqe *sqe = req->sqe;
Jens Axboead8a48a2019-11-15 08:49:11 -07002660 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06002661 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06002662
Jens Axboead8a48a2019-11-15 08:49:11 -07002663 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06002664 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07002665 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06002666 return -EINVAL;
Jens Axboe2d283902019-12-04 11:08:05 -07002667 if (sqe->off && is_timeout_link)
2668 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06002669 flags = READ_ONCE(sqe->timeout_flags);
2670 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06002671 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06002672
Jens Axboe2d283902019-12-04 11:08:05 -07002673 data = &io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07002674 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07002675 req->flags |= REQ_F_TIMEOUT;
2676
2677 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06002678 return -EFAULT;
2679
Jens Axboe11365042019-10-16 09:08:32 -06002680 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07002681 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06002682 else
Jens Axboead8a48a2019-11-15 08:49:11 -07002683 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06002684
Jens Axboead8a48a2019-11-15 08:49:11 -07002685 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
Jens Axboe2d283902019-12-04 11:08:05 -07002686 req->io = io;
Jens Axboead8a48a2019-11-15 08:49:11 -07002687 return 0;
2688}
2689
2690static int io_timeout(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2691{
2692 unsigned count;
2693 struct io_ring_ctx *ctx = req->ctx;
2694 struct io_timeout_data *data;
Jens Axboe2d283902019-12-04 11:08:05 -07002695 struct io_async_ctx *io;
Jens Axboead8a48a2019-11-15 08:49:11 -07002696 struct list_head *entry;
2697 unsigned span = 0;
Jens Axboead8a48a2019-11-15 08:49:11 -07002698
Jens Axboe2d283902019-12-04 11:08:05 -07002699 io = req->io;
2700 if (!io) {
2701 int ret;
2702
2703 io = kmalloc(sizeof(*io), GFP_KERNEL);
2704 if (!io)
2705 return -ENOMEM;
2706 ret = io_timeout_prep(req, io, false);
2707 if (ret) {
2708 kfree(io);
2709 return ret;
2710 }
2711 }
2712 data = &req->io->timeout;
Jens Axboe93bd25b2019-11-11 23:34:31 -07002713
Jens Axboe5262f562019-09-17 12:26:57 -06002714 /*
2715 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07002716 * timeout event to be satisfied. If it isn't set, then this is
2717 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06002718 */
2719 count = READ_ONCE(sqe->off);
Jens Axboe93bd25b2019-11-11 23:34:31 -07002720 if (!count) {
2721 req->flags |= REQ_F_TIMEOUT_NOSEQ;
2722 spin_lock_irq(&ctx->completion_lock);
2723 entry = ctx->timeout_list.prev;
2724 goto add;
2725 }
Jens Axboe5262f562019-09-17 12:26:57 -06002726
2727 req->sequence = ctx->cached_sq_head + count - 1;
Jens Axboe2d283902019-12-04 11:08:05 -07002728 data->seq_offset = count;
Jens Axboe5262f562019-09-17 12:26:57 -06002729
2730 /*
2731 * Insertion sort, ensuring the first entry in the list is always
2732 * the one we need first.
2733 */
Jens Axboe5262f562019-09-17 12:26:57 -06002734 spin_lock_irq(&ctx->completion_lock);
2735 list_for_each_prev(entry, &ctx->timeout_list) {
2736 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
yangerkun5da0fb12019-10-15 21:59:29 +08002737 unsigned nxt_sq_head;
2738 long long tmp, tmp_nxt;
Jens Axboe2d283902019-12-04 11:08:05 -07002739 u32 nxt_offset = nxt->io->timeout.seq_offset;
Jens Axboe5262f562019-09-17 12:26:57 -06002740
Jens Axboe93bd25b2019-11-11 23:34:31 -07002741 if (nxt->flags & REQ_F_TIMEOUT_NOSEQ)
2742 continue;
2743
yangerkun5da0fb12019-10-15 21:59:29 +08002744 /*
2745 * Since cached_sq_head + count - 1 can overflow, use type long
2746 * long to store it.
2747 */
2748 tmp = (long long)ctx->cached_sq_head + count - 1;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +03002749 nxt_sq_head = nxt->sequence - nxt_offset + 1;
2750 tmp_nxt = (long long)nxt_sq_head + nxt_offset - 1;
yangerkun5da0fb12019-10-15 21:59:29 +08002751
2752 /*
2753 * cached_sq_head may overflow, and it will never overflow twice
2754 * once there is some timeout req still be valid.
2755 */
2756 if (ctx->cached_sq_head < nxt_sq_head)
yangerkun8b07a652019-10-17 12:12:35 +08002757 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08002758
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002759 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06002760 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002761
2762 /*
2763 * Sequence of reqs after the insert one and itself should
2764 * be adjusted because each timeout req consumes a slot.
2765 */
2766 span++;
2767 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06002768 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002769 req->sequence -= span;
Jens Axboe93bd25b2019-11-11 23:34:31 -07002770add:
Jens Axboe5262f562019-09-17 12:26:57 -06002771 list_add(&req->list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07002772 data->timer.function = io_timeout_fn;
2773 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06002774 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06002775 return 0;
2776}
2777
Jens Axboe62755e32019-10-28 21:49:21 -06002778static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06002779{
Jens Axboe62755e32019-10-28 21:49:21 -06002780 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06002781
Jens Axboe62755e32019-10-28 21:49:21 -06002782 return req->user_data == (unsigned long) data;
2783}
2784
Jens Axboee977d6d2019-11-05 12:39:45 -07002785static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06002786{
Jens Axboe62755e32019-10-28 21:49:21 -06002787 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06002788 int ret = 0;
2789
Jens Axboe62755e32019-10-28 21:49:21 -06002790 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
2791 switch (cancel_ret) {
2792 case IO_WQ_CANCEL_OK:
2793 ret = 0;
2794 break;
2795 case IO_WQ_CANCEL_RUNNING:
2796 ret = -EALREADY;
2797 break;
2798 case IO_WQ_CANCEL_NOTFOUND:
2799 ret = -ENOENT;
2800 break;
2801 }
2802
Jens Axboee977d6d2019-11-05 12:39:45 -07002803 return ret;
2804}
2805
Jens Axboe47f46762019-11-09 17:43:02 -07002806static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
2807 struct io_kiocb *req, __u64 sqe_addr,
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002808 struct io_kiocb **nxt, int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07002809{
2810 unsigned long flags;
2811 int ret;
2812
2813 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
2814 if (ret != -ENOENT) {
2815 spin_lock_irqsave(&ctx->completion_lock, flags);
2816 goto done;
2817 }
2818
2819 spin_lock_irqsave(&ctx->completion_lock, flags);
2820 ret = io_timeout_cancel(ctx, sqe_addr);
2821 if (ret != -ENOENT)
2822 goto done;
2823 ret = io_poll_cancel(ctx, sqe_addr);
2824done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002825 if (!ret)
2826 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07002827 io_cqring_fill_event(req, ret);
2828 io_commit_cqring(ctx);
2829 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2830 io_cqring_ev_posted(ctx);
2831
2832 if (ret < 0 && (req->flags & REQ_F_LINK))
2833 req->flags |= REQ_F_FAIL_LINK;
2834 io_put_req_find_next(req, nxt);
2835}
2836
Jens Axboee977d6d2019-11-05 12:39:45 -07002837static int io_async_cancel(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2838 struct io_kiocb **nxt)
2839{
2840 struct io_ring_ctx *ctx = req->ctx;
Jens Axboee977d6d2019-11-05 12:39:45 -07002841
2842 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2843 return -EINVAL;
2844 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
2845 sqe->cancel_flags)
2846 return -EINVAL;
2847
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002848 io_async_find_and_cancel(ctx, req, READ_ONCE(sqe->addr), nxt, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06002849 return 0;
2850}
2851
Jens Axboef67676d2019-12-02 11:03:47 -07002852static int io_req_defer_prep(struct io_kiocb *req, struct io_async_ctx *io)
2853{
2854 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
2855 struct iov_iter iter;
2856 ssize_t ret;
2857
2858 memcpy(&io->sqe, req->sqe, sizeof(io->sqe));
2859 req->sqe = &io->sqe;
2860
2861 switch (io->sqe.opcode) {
2862 case IORING_OP_READV:
2863 case IORING_OP_READ_FIXED:
2864 ret = io_read_prep(req, &iovec, &iter, true);
2865 break;
2866 case IORING_OP_WRITEV:
2867 case IORING_OP_WRITE_FIXED:
2868 ret = io_write_prep(req, &iovec, &iter, true);
2869 break;
Jens Axboe03b12302019-12-02 18:50:25 -07002870 case IORING_OP_SENDMSG:
2871 ret = io_sendmsg_prep(req, io);
2872 break;
2873 case IORING_OP_RECVMSG:
2874 ret = io_recvmsg_prep(req, io);
2875 break;
Jens Axboef499a022019-12-02 16:28:46 -07002876 case IORING_OP_CONNECT:
2877 ret = io_connect_prep(req, io);
2878 break;
Jens Axboe2d283902019-12-04 11:08:05 -07002879 case IORING_OP_TIMEOUT:
2880 return io_timeout_prep(req, io, false);
2881 case IORING_OP_LINK_TIMEOUT:
2882 return io_timeout_prep(req, io, true);
Jens Axboef67676d2019-12-02 11:03:47 -07002883 default:
2884 req->io = io;
2885 return 0;
2886 }
2887
2888 if (ret < 0)
2889 return ret;
2890
2891 req->io = io;
2892 io_req_map_io(req, ret, iovec, inline_vecs, &iter);
2893 return 0;
2894}
2895
Jackie Liua197f662019-11-08 08:09:12 -07002896static int io_req_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -06002897{
Jackie Liua197f662019-11-08 08:09:12 -07002898 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe1a6b74f2019-12-02 10:33:15 -07002899 struct io_async_ctx *io;
Jens Axboef67676d2019-12-02 11:03:47 -07002900 int ret;
Jens Axboede0617e2019-04-06 21:51:27 -06002901
Bob Liu9d858b22019-11-13 18:06:25 +08002902 /* Still need defer if there is pending req in defer list. */
2903 if (!req_need_defer(req) && list_empty(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06002904 return 0;
2905
Jens Axboe1a6b74f2019-12-02 10:33:15 -07002906 io = kmalloc(sizeof(*io), GFP_KERNEL);
2907 if (!io)
Jens Axboede0617e2019-04-06 21:51:27 -06002908 return -EAGAIN;
2909
Jens Axboe2d283902019-12-04 11:08:05 -07002910 ret = io_req_defer_prep(req, io);
2911 if (ret < 0) {
2912 kfree(io);
2913 return ret;
2914 }
2915
Jens Axboede0617e2019-04-06 21:51:27 -06002916 spin_lock_irq(&ctx->completion_lock);
Bob Liu9d858b22019-11-13 18:06:25 +08002917 if (!req_need_defer(req) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06002918 spin_unlock_irq(&ctx->completion_lock);
Jens Axboede0617e2019-04-06 21:51:27 -06002919 return 0;
2920 }
2921
Jens Axboe915967f2019-11-21 09:01:20 -07002922 trace_io_uring_defer(ctx, req, req->user_data);
Jens Axboede0617e2019-04-06 21:51:27 -06002923 list_add_tail(&req->list, &ctx->defer_list);
2924 spin_unlock_irq(&ctx->completion_lock);
2925 return -EIOCBQUEUED;
2926}
2927
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03002928__attribute__((nonnull))
Pavel Begunkovd7324472019-11-21 21:24:36 +03002929static int io_issue_sqe(struct io_kiocb *req, struct io_kiocb **nxt,
2930 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002931{
Jens Axboee0c5c572019-03-12 10:18:47 -06002932 int ret, opcode;
Jackie Liua197f662019-11-08 08:09:12 -07002933 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002934
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002935 opcode = READ_ONCE(req->sqe->opcode);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002936 switch (opcode) {
2937 case IORING_OP_NOP:
Jens Axboe78e19bb2019-11-06 15:21:34 -07002938 ret = io_nop(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002939 break;
2940 case IORING_OP_READV:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002941 if (unlikely(req->sqe->buf_index))
Jens Axboeedafcce2019-01-09 09:16:05 -07002942 return -EINVAL;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002943 ret = io_read(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002944 break;
2945 case IORING_OP_WRITEV:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002946 if (unlikely(req->sqe->buf_index))
Jens Axboeedafcce2019-01-09 09:16:05 -07002947 return -EINVAL;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002948 ret = io_write(req, nxt, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07002949 break;
2950 case IORING_OP_READ_FIXED:
Pavel Begunkov267bc902019-11-07 01:41:08 +03002951 ret = io_read(req, nxt, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07002952 break;
2953 case IORING_OP_WRITE_FIXED:
Pavel Begunkov267bc902019-11-07 01:41:08 +03002954 ret = io_write(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002955 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002956 case IORING_OP_FSYNC:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002957 ret = io_fsync(req, req->sqe, nxt, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002958 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002959 case IORING_OP_POLL_ADD:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002960 ret = io_poll_add(req, req->sqe, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002961 break;
2962 case IORING_OP_POLL_REMOVE:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002963 ret = io_poll_remove(req, req->sqe);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002964 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002965 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002966 ret = io_sync_file_range(req, req->sqe, nxt, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002967 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002968 case IORING_OP_SENDMSG:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002969 ret = io_sendmsg(req, req->sqe, nxt, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06002970 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06002971 case IORING_OP_RECVMSG:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002972 ret = io_recvmsg(req, req->sqe, nxt, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06002973 break;
Jens Axboe5262f562019-09-17 12:26:57 -06002974 case IORING_OP_TIMEOUT:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002975 ret = io_timeout(req, req->sqe);
Jens Axboe5262f562019-09-17 12:26:57 -06002976 break;
Jens Axboe11365042019-10-16 09:08:32 -06002977 case IORING_OP_TIMEOUT_REMOVE:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002978 ret = io_timeout_remove(req, req->sqe);
Jens Axboe11365042019-10-16 09:08:32 -06002979 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06002980 case IORING_OP_ACCEPT:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002981 ret = io_accept(req, req->sqe, nxt, force_nonblock);
Jens Axboe17f2fe32019-10-17 14:42:58 -06002982 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07002983 case IORING_OP_CONNECT:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002984 ret = io_connect(req, req->sqe, nxt, force_nonblock);
Jens Axboef8e85cf2019-11-23 14:24:24 -07002985 break;
Jens Axboe62755e32019-10-28 21:49:21 -06002986 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002987 ret = io_async_cancel(req, req->sqe, nxt);
Jens Axboe62755e32019-10-28 21:49:21 -06002988 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002989 default:
2990 ret = -EINVAL;
2991 break;
2992 }
2993
Jens Axboedef596e2019-01-09 08:59:42 -07002994 if (ret)
2995 return ret;
2996
2997 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe9e645e112019-05-10 16:07:28 -06002998 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07002999 return -EAGAIN;
3000
3001 /* workqueue context doesn't hold uring_lock, grab it now */
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003002 if (req->in_async)
Jens Axboedef596e2019-01-09 08:59:42 -07003003 mutex_lock(&ctx->uring_lock);
3004 io_iopoll_req_issued(req);
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003005 if (req->in_async)
Jens Axboedef596e2019-01-09 08:59:42 -07003006 mutex_unlock(&ctx->uring_lock);
3007 }
3008
3009 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003010}
3011
Jens Axboeb76da702019-11-20 13:05:32 -07003012static void io_link_work_cb(struct io_wq_work **workptr)
3013{
3014 struct io_wq_work *work = *workptr;
3015 struct io_kiocb *link = work->data;
3016
3017 io_queue_linked_timeout(link);
3018 work->func = io_wq_submit_work;
3019}
3020
Jens Axboe561fb042019-10-24 07:25:42 -06003021static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboe31b51512019-01-18 22:56:34 -07003022{
Jens Axboe561fb042019-10-24 07:25:42 -06003023 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003024 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe561fb042019-10-24 07:25:42 -06003025 struct io_kiocb *nxt = NULL;
3026 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003027
Jens Axboe561fb042019-10-24 07:25:42 -06003028 /* Ensure we clear previously set non-block flag */
3029 req->rw.ki_flags &= ~IOCB_NOWAIT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003030
Jens Axboe561fb042019-10-24 07:25:42 -06003031 if (work->flags & IO_WQ_WORK_CANCEL)
3032 ret = -ECANCELED;
Jens Axboe31b51512019-01-18 22:56:34 -07003033
Jens Axboe561fb042019-10-24 07:25:42 -06003034 if (!ret) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003035 req->has_user = (work->flags & IO_WQ_WORK_HAS_MM) != 0;
3036 req->in_async = true;
Jens Axboe561fb042019-10-24 07:25:42 -06003037 do {
Pavel Begunkovd7324472019-11-21 21:24:36 +03003038 ret = io_issue_sqe(req, &nxt, false);
Jens Axboe561fb042019-10-24 07:25:42 -06003039 /*
3040 * We can get EAGAIN for polled IO even though we're
3041 * forcing a sync submission from here, since we can't
3042 * wait for request slots on the block side.
3043 */
3044 if (ret != -EAGAIN)
3045 break;
3046 cond_resched();
3047 } while (1);
3048 }
Jens Axboe31b51512019-01-18 22:56:34 -07003049
Jens Axboe561fb042019-10-24 07:25:42 -06003050 /* drop submission reference */
Jackie Liuec9c02a2019-11-08 23:50:36 +08003051 io_put_req(req);
Jens Axboe817869d2019-04-30 14:44:05 -06003052
Jens Axboe561fb042019-10-24 07:25:42 -06003053 if (ret) {
Jens Axboef1f40852019-11-05 20:33:16 -07003054 if (req->flags & REQ_F_LINK)
3055 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07003056 io_cqring_add_event(req, ret);
Jens Axboe817869d2019-04-30 14:44:05 -06003057 io_put_req(req);
Jens Axboeedafcce2019-01-09 09:16:05 -07003058 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003059
Jens Axboe561fb042019-10-24 07:25:42 -06003060 /* if a dependent link is ready, pass it back */
3061 if (!ret && nxt) {
Jens Axboe94ae5e72019-11-14 19:39:52 -07003062 struct io_kiocb *link;
3063
3064 io_prep_async_work(nxt, &link);
Jens Axboe561fb042019-10-24 07:25:42 -06003065 *workptr = &nxt->work;
Jens Axboeb76da702019-11-20 13:05:32 -07003066 if (link) {
3067 nxt->work.flags |= IO_WQ_WORK_CB;
3068 nxt->work.func = io_link_work_cb;
3069 nxt->work.data = link;
3070 }
Jens Axboeedafcce2019-01-09 09:16:05 -07003071 }
Jens Axboe31b51512019-01-18 22:56:34 -07003072}
Jens Axboe2b188cc2019-01-07 10:46:33 -07003073
Jens Axboe09bb8392019-03-13 12:39:28 -06003074static bool io_op_needs_file(const struct io_uring_sqe *sqe)
3075{
3076 int op = READ_ONCE(sqe->opcode);
3077
3078 switch (op) {
3079 case IORING_OP_NOP:
3080 case IORING_OP_POLL_REMOVE:
Pavel Begunkov5683e542019-11-14 00:59:19 +03003081 case IORING_OP_TIMEOUT:
Pavel Begunkova320e9f2019-11-14 00:11:01 +03003082 case IORING_OP_TIMEOUT_REMOVE:
3083 case IORING_OP_ASYNC_CANCEL:
3084 case IORING_OP_LINK_TIMEOUT:
Jens Axboe09bb8392019-03-13 12:39:28 -06003085 return false;
3086 default:
3087 return true;
3088 }
3089}
3090
Jens Axboe65e19f52019-10-26 07:20:21 -06003091static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
3092 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06003093{
Jens Axboe65e19f52019-10-26 07:20:21 -06003094 struct fixed_file_table *table;
3095
3096 table = &ctx->file_table[index >> IORING_FILE_TABLE_SHIFT];
3097 return table->files[index & IORING_FILE_TABLE_MASK];
3098}
3099
Jackie Liua197f662019-11-08 08:09:12 -07003100static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req)
Jens Axboe09bb8392019-03-13 12:39:28 -06003101{
Jackie Liua197f662019-11-08 08:09:12 -07003102 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe09bb8392019-03-13 12:39:28 -06003103 unsigned flags;
3104 int fd;
3105
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003106 flags = READ_ONCE(req->sqe->flags);
3107 fd = READ_ONCE(req->sqe->fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06003108
Jackie Liu4fe2c962019-09-09 20:50:40 +08003109 if (flags & IOSQE_IO_DRAIN)
Jens Axboede0617e2019-04-06 21:51:27 -06003110 req->flags |= REQ_F_IO_DRAIN;
Jens Axboede0617e2019-04-06 21:51:27 -06003111
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003112 if (!io_op_needs_file(req->sqe))
Jens Axboe09bb8392019-03-13 12:39:28 -06003113 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06003114
3115 if (flags & IOSQE_FIXED_FILE) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003116 if (unlikely(!ctx->file_table ||
Jens Axboe09bb8392019-03-13 12:39:28 -06003117 (unsigned) fd >= ctx->nr_user_files))
3118 return -EBADF;
Jens Axboeb7620122019-10-26 07:22:55 -06003119 fd = array_index_nospec(fd, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06003120 req->file = io_file_from_index(ctx, fd);
3121 if (!req->file)
Jens Axboe08a45172019-10-03 08:11:03 -06003122 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06003123 req->flags |= REQ_F_FIXED_FILE;
3124 } else {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003125 if (req->needs_fixed_file)
Jens Axboe09bb8392019-03-13 12:39:28 -06003126 return -EBADF;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02003127 trace_io_uring_file_get(ctx, fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06003128 req->file = io_file_get(state, fd);
3129 if (unlikely(!req->file))
3130 return -EBADF;
3131 }
3132
3133 return 0;
3134}
3135
Jackie Liua197f662019-11-08 08:09:12 -07003136static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003137{
Jens Axboefcb323c2019-10-24 12:39:47 -06003138 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07003139 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06003140
3141 rcu_read_lock();
3142 spin_lock_irq(&ctx->inflight_lock);
3143 /*
3144 * We use the f_ops->flush() handler to ensure that we can flush
3145 * out work accessing these files if the fd is closed. Check if
3146 * the fd has changed since we started down this path, and disallow
3147 * this operation if it has.
3148 */
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003149 if (fcheck(req->ring_fd) == req->ring_file) {
Jens Axboefcb323c2019-10-24 12:39:47 -06003150 list_add(&req->inflight_entry, &ctx->inflight_list);
3151 req->flags |= REQ_F_INFLIGHT;
3152 req->work.files = current->files;
3153 ret = 0;
3154 }
3155 spin_unlock_irq(&ctx->inflight_lock);
3156 rcu_read_unlock();
3157
3158 return ret;
3159}
3160
Jens Axboe2665abf2019-11-05 12:40:47 -07003161static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
3162{
Jens Axboead8a48a2019-11-15 08:49:11 -07003163 struct io_timeout_data *data = container_of(timer,
3164 struct io_timeout_data, timer);
3165 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07003166 struct io_ring_ctx *ctx = req->ctx;
3167 struct io_kiocb *prev = NULL;
3168 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07003169
3170 spin_lock_irqsave(&ctx->completion_lock, flags);
3171
3172 /*
3173 * We don't expect the list to be empty, that will only happen if we
3174 * race with the completion of the linked work.
3175 */
3176 if (!list_empty(&req->list)) {
3177 prev = list_entry(req->list.prev, struct io_kiocb, link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07003178 if (refcount_inc_not_zero(&prev->refs)) {
Jens Axboe76a46e02019-11-10 23:34:16 -07003179 list_del_init(&req->list);
Jens Axboe5d960722019-11-19 15:31:28 -07003180 prev->flags &= ~REQ_F_LINK_TIMEOUT;
3181 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07003182 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07003183 }
3184
3185 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3186
3187 if (prev) {
Jens Axboefba38c22019-11-18 12:27:57 -07003188 if (prev->flags & REQ_F_LINK)
3189 prev->flags |= REQ_F_FAIL_LINK;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003190 io_async_find_and_cancel(ctx, req, prev->user_data, NULL,
3191 -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07003192 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07003193 } else {
3194 io_cqring_add_event(req, -ETIME);
3195 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07003196 }
Jens Axboe2665abf2019-11-05 12:40:47 -07003197 return HRTIMER_NORESTART;
3198}
3199
Jens Axboead8a48a2019-11-15 08:49:11 -07003200static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07003201{
Jens Axboe76a46e02019-11-10 23:34:16 -07003202 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07003203
Jens Axboe76a46e02019-11-10 23:34:16 -07003204 /*
3205 * If the list is now empty, then our linked request finished before
3206 * we got a chance to setup the timer
3207 */
3208 spin_lock_irq(&ctx->completion_lock);
3209 if (!list_empty(&req->list)) {
Jens Axboe2d283902019-12-04 11:08:05 -07003210 struct io_timeout_data *data = &req->io->timeout;
Jens Axboe94ae5e72019-11-14 19:39:52 -07003211
Jens Axboead8a48a2019-11-15 08:49:11 -07003212 data->timer.function = io_link_timeout_fn;
3213 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
3214 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07003215 }
Jens Axboe76a46e02019-11-10 23:34:16 -07003216 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07003217
Jens Axboe2665abf2019-11-05 12:40:47 -07003218 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07003219 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07003220}
3221
Jens Axboead8a48a2019-11-15 08:49:11 -07003222static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07003223{
3224 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003225
Jens Axboe2665abf2019-11-05 12:40:47 -07003226 if (!(req->flags & REQ_F_LINK))
3227 return NULL;
3228
3229 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, list);
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003230 if (!nxt || nxt->sqe->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07003231 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07003232
Jens Axboe76a46e02019-11-10 23:34:16 -07003233 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07003234 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07003235}
3236
Jens Axboe0e0702d2019-11-14 21:42:10 -07003237static void __io_queue_sqe(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003238{
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03003239 struct io_kiocb *linked_timeout = io_prep_linked_timeout(req);
3240 struct io_kiocb *nxt = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003241 int ret;
3242
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03003243 ret = io_issue_sqe(req, &nxt, true);
3244 if (nxt)
3245 io_queue_async_work(nxt);
Jens Axboe491381ce2019-10-17 09:20:46 -06003246
3247 /*
3248 * We async punt it if the file wasn't marked NOWAIT, or if the file
3249 * doesn't support non-blocking read/write attempts
3250 */
3251 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
3252 (req->flags & REQ_F_MUST_PUNT))) {
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03003253 if (req->work.flags & IO_WQ_WORK_NEEDS_FILES) {
3254 ret = io_grab_files(req);
3255 if (ret)
3256 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003257 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03003258
3259 /*
3260 * Queued up for async execution, worker will release
3261 * submit reference when the iocb is actually submitted.
3262 */
3263 io_queue_async_work(req);
3264 return;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003265 }
Jens Axboee65ef562019-03-12 10:16:44 -06003266
Jens Axboefcb323c2019-10-24 12:39:47 -06003267err:
Jens Axboee65ef562019-03-12 10:16:44 -06003268 /* drop submission reference */
3269 io_put_req(req);
3270
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03003271 if (linked_timeout) {
Jens Axboe76a46e02019-11-10 23:34:16 -07003272 if (!ret)
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03003273 io_queue_linked_timeout(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07003274 else
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03003275 io_put_req(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07003276 }
3277
Jens Axboee65ef562019-03-12 10:16:44 -06003278 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06003279 if (ret) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07003280 io_cqring_add_event(req, ret);
Jens Axboe9e645e112019-05-10 16:07:28 -06003281 if (req->flags & REQ_F_LINK)
3282 req->flags |= REQ_F_FAIL_LINK;
Jens Axboee65ef562019-03-12 10:16:44 -06003283 io_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06003284 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003285}
3286
Jens Axboe0e0702d2019-11-14 21:42:10 -07003287static void io_queue_sqe(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08003288{
3289 int ret;
3290
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003291 if (unlikely(req->ctx->drain_next)) {
3292 req->flags |= REQ_F_IO_DRAIN;
3293 req->ctx->drain_next = false;
3294 }
3295 req->ctx->drain_next = (req->flags & REQ_F_DRAIN_LINK);
3296
Jackie Liua197f662019-11-08 08:09:12 -07003297 ret = io_req_defer(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08003298 if (ret) {
3299 if (ret != -EIOCBQUEUED) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07003300 io_cqring_add_event(req, ret);
Pavel Begunkovd3b357962019-11-19 23:32:48 +03003301 if (req->flags & REQ_F_LINK)
3302 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07003303 io_double_put_req(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08003304 }
Jens Axboe0e0702d2019-11-14 21:42:10 -07003305 } else
3306 __io_queue_sqe(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08003307}
3308
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003309static inline void io_queue_link_head(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08003310{
Jens Axboe94ae5e72019-11-14 19:39:52 -07003311 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003312 io_cqring_add_event(req, -ECANCELED);
3313 io_double_put_req(req);
3314 } else
Jens Axboe0e0702d2019-11-14 21:42:10 -07003315 io_queue_sqe(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08003316}
3317
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003318
Jens Axboe9e645e112019-05-10 16:07:28 -06003319#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK)
3320
Jackie Liua197f662019-11-08 08:09:12 -07003321static void io_submit_sqe(struct io_kiocb *req, struct io_submit_state *state,
3322 struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06003323{
Jackie Liua197f662019-11-08 08:09:12 -07003324 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -06003325 int ret;
3326
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003327 req->user_data = req->sqe->user_data;
Jens Axboe78e19bb2019-11-06 15:21:34 -07003328
Jens Axboe9e645e112019-05-10 16:07:28 -06003329 /* enforce forwards compatibility on users */
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003330 if (unlikely(req->sqe->flags & ~SQE_VALID_FLAGS)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06003331 ret = -EINVAL;
Pavel Begunkov196be952019-11-07 01:41:06 +03003332 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06003333 }
3334
Jackie Liua197f662019-11-08 08:09:12 -07003335 ret = io_req_set_file(state, req);
Jens Axboe9e645e112019-05-10 16:07:28 -06003336 if (unlikely(ret)) {
3337err_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07003338 io_cqring_add_event(req, ret);
3339 io_double_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06003340 return;
3341 }
3342
Jens Axboe9e645e112019-05-10 16:07:28 -06003343 /*
3344 * If we already have a head request, queue this one for async
3345 * submittal once the head completes. If we don't have a head but
3346 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
3347 * submitted sync once the chain is complete. If none of those
3348 * conditions are true (normal request), then just queue it.
3349 */
3350 if (*link) {
3351 struct io_kiocb *prev = *link;
Jens Axboe1a6b74f2019-12-02 10:33:15 -07003352 struct io_async_ctx *io;
Jens Axboe9e645e112019-05-10 16:07:28 -06003353
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003354 if (req->sqe->flags & IOSQE_IO_DRAIN)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003355 (*link)->flags |= REQ_F_DRAIN_LINK | REQ_F_IO_DRAIN;
3356
Jens Axboe1a6b74f2019-12-02 10:33:15 -07003357 io = kmalloc(sizeof(*io), GFP_KERNEL);
3358 if (!io) {
Jens Axboe9e645e112019-05-10 16:07:28 -06003359 ret = -EAGAIN;
3360 goto err_req;
3361 }
3362
Jens Axboef67676d2019-12-02 11:03:47 -07003363 ret = io_req_defer_prep(req, io);
Jens Axboe2d283902019-12-04 11:08:05 -07003364 if (ret) {
3365 kfree(io);
3366 prev->flags |= REQ_F_FAIL_LINK;
Jens Axboef67676d2019-12-02 11:03:47 -07003367 goto err_req;
Jens Axboe2d283902019-12-04 11:08:05 -07003368 }
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02003369 trace_io_uring_link(ctx, req, prev);
Jens Axboe9e645e112019-05-10 16:07:28 -06003370 list_add_tail(&req->list, &prev->link_list);
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003371 } else if (req->sqe->flags & IOSQE_IO_LINK) {
Jens Axboe9e645e112019-05-10 16:07:28 -06003372 req->flags |= REQ_F_LINK;
3373
Jens Axboe9e645e112019-05-10 16:07:28 -06003374 INIT_LIST_HEAD(&req->link_list);
3375 *link = req;
3376 } else {
Jackie Liua197f662019-11-08 08:09:12 -07003377 io_queue_sqe(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06003378 }
3379}
3380
Jens Axboe9a56a232019-01-09 09:06:50 -07003381/*
3382 * Batched submission is done, ensure local IO is flushed out.
3383 */
3384static void io_submit_state_end(struct io_submit_state *state)
3385{
3386 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06003387 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07003388 if (state->free_reqs)
3389 kmem_cache_free_bulk(req_cachep, state->free_reqs,
3390 &state->reqs[state->cur_req]);
Jens Axboe9a56a232019-01-09 09:06:50 -07003391}
3392
3393/*
3394 * Start submission side cache.
3395 */
3396static void io_submit_state_start(struct io_submit_state *state,
Jackie Liu22efde52019-12-02 17:14:52 +08003397 unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07003398{
3399 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07003400 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07003401 state->file = NULL;
3402 state->ios_left = max_ios;
3403}
3404
Jens Axboe2b188cc2019-01-07 10:46:33 -07003405static void io_commit_sqring(struct io_ring_ctx *ctx)
3406{
Hristo Venev75b28af2019-08-26 17:23:46 +00003407 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003408
Hristo Venev75b28af2019-08-26 17:23:46 +00003409 if (ctx->cached_sq_head != READ_ONCE(rings->sq.head)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07003410 /*
3411 * Ensure any loads from the SQEs are done at this point,
3412 * since once we write the new head, the application could
3413 * write new data to them.
3414 */
Hristo Venev75b28af2019-08-26 17:23:46 +00003415 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003416 }
3417}
3418
3419/*
Jens Axboe2b188cc2019-01-07 10:46:33 -07003420 * Fetch an sqe, if one is available. Note that s->sqe will point to memory
3421 * that is mapped by userspace. This means that care needs to be taken to
3422 * ensure that reads are stable, as we cannot rely on userspace always
3423 * being a good citizen. If members of the sqe are validated and then later
3424 * used, it's important that those reads are done through READ_ONCE() to
3425 * prevent a re-load down the line.
3426 */
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003427static bool io_get_sqring(struct io_ring_ctx *ctx, struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003428{
Hristo Venev75b28af2019-08-26 17:23:46 +00003429 struct io_rings *rings = ctx->rings;
3430 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003431 unsigned head;
3432
3433 /*
3434 * The cached sq head (or cq tail) serves two purposes:
3435 *
3436 * 1) allows us to batch the cost of updating the user visible
3437 * head updates.
3438 * 2) allows the kernel side to track the head on its own, even
3439 * though the application is the one updating it.
3440 */
3441 head = ctx->cached_sq_head;
Stefan Bühlere523a292019-04-19 11:57:44 +02003442 /* make sure SQ entry isn't read before tail */
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03003443 if (unlikely(head == smp_load_acquire(&rings->sq.tail)))
Jens Axboe2b188cc2019-01-07 10:46:33 -07003444 return false;
3445
Hristo Venev75b28af2019-08-26 17:23:46 +00003446 head = READ_ONCE(sq_array[head & ctx->sq_mask]);
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03003447 if (likely(head < ctx->sq_entries)) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003448 /*
3449 * All io need record the previous position, if LINK vs DARIN,
3450 * it can be used to mark the position of the first IO in the
3451 * link list.
3452 */
3453 req->sequence = ctx->cached_sq_head;
3454 req->sqe = &ctx->sq_sqes[head];
Jens Axboe2b188cc2019-01-07 10:46:33 -07003455 ctx->cached_sq_head++;
3456 return true;
3457 }
3458
3459 /* drop invalid entries */
3460 ctx->cached_sq_head++;
Jens Axboe498ccd92019-10-25 10:04:25 -06003461 ctx->cached_sq_dropped++;
3462 WRITE_ONCE(rings->sq_dropped, ctx->cached_sq_dropped);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003463 return false;
3464}
3465
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003466static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkovae9428c2019-11-06 00:22:14 +03003467 struct file *ring_file, int ring_fd,
3468 struct mm_struct **mm, bool async)
Jens Axboe6c271ce2019-01-10 11:22:30 -07003469{
3470 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06003471 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06003472 int i, submitted = 0;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03003473 bool mm_fault = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003474
Jens Axboec4a2ed72019-11-21 21:01:26 -07003475 /* if we have a backlog and couldn't flush it all, return BUSY */
3476 if (!list_empty(&ctx->cq_overflow_list) &&
3477 !io_cqring_overflow_flush(ctx, false))
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003478 return -EBUSY;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003479
3480 if (nr > IO_PLUG_THRESHOLD) {
Jackie Liu22efde52019-12-02 17:14:52 +08003481 io_submit_state_start(&state, nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003482 statep = &state;
3483 }
3484
3485 for (i = 0; i < nr; i++) {
Pavel Begunkov196be952019-11-07 01:41:06 +03003486 struct io_kiocb *req;
Pavel Begunkov50585b92019-11-07 01:41:07 +03003487 unsigned int sqe_flags;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003488
Pavel Begunkov196be952019-11-07 01:41:06 +03003489 req = io_get_req(ctx, statep);
3490 if (unlikely(!req)) {
3491 if (!submitted)
3492 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003493 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06003494 }
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003495 if (!io_get_sqring(ctx, req)) {
Pavel Begunkov196be952019-11-07 01:41:06 +03003496 __io_free_req(req);
3497 break;
3498 }
Jens Axboe9e645e112019-05-10 16:07:28 -06003499
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003500 if (io_sqe_needs_user(req->sqe) && !*mm) {
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03003501 mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm);
3502 if (!mm_fault) {
3503 use_mm(ctx->sqo_mm);
3504 *mm = ctx->sqo_mm;
3505 }
3506 }
3507
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003508 sqe_flags = req->sqe->flags;
Pavel Begunkov50585b92019-11-07 01:41:07 +03003509
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003510 req->ring_file = ring_file;
3511 req->ring_fd = ring_fd;
3512 req->has_user = *mm != NULL;
3513 req->in_async = async;
3514 req->needs_fixed_file = async;
3515 trace_io_uring_submit_sqe(ctx, req->sqe->user_data,
Pavel Begunkov50585b92019-11-07 01:41:07 +03003516 true, async);
Jackie Liua197f662019-11-08 08:09:12 -07003517 io_submit_sqe(req, statep, &link);
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03003518 submitted++;
Pavel Begunkove5eb6362019-11-06 00:22:15 +03003519
3520 /*
3521 * If previous wasn't linked and we have a linked command,
3522 * that's the end of the chain. Submit the previous link.
3523 */
Pavel Begunkov50585b92019-11-07 01:41:07 +03003524 if (!(sqe_flags & IOSQE_IO_LINK) && link) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003525 io_queue_link_head(link);
Pavel Begunkove5eb6362019-11-06 00:22:15 +03003526 link = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003527 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07003528 }
3529
Jens Axboe9e645e112019-05-10 16:07:28 -06003530 if (link)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003531 io_queue_link_head(link);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003532 if (statep)
3533 io_submit_state_end(&state);
3534
Pavel Begunkovae9428c2019-11-06 00:22:14 +03003535 /* Commit SQ ring head once we've consumed and submitted all SQEs */
3536 io_commit_sqring(ctx);
3537
Jens Axboe6c271ce2019-01-10 11:22:30 -07003538 return submitted;
3539}
3540
3541static int io_sq_thread(void *data)
3542{
Jens Axboe6c271ce2019-01-10 11:22:30 -07003543 struct io_ring_ctx *ctx = data;
3544 struct mm_struct *cur_mm = NULL;
Jens Axboe181e4482019-11-25 08:52:30 -07003545 const struct cred *old_cred;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003546 mm_segment_t old_fs;
3547 DEFINE_WAIT(wait);
3548 unsigned inflight;
3549 unsigned long timeout;
Jens Axboec1edbf52019-11-10 16:56:04 -07003550 int ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003551
Jens Axboe206aefd2019-11-07 18:27:42 -07003552 complete(&ctx->completions[1]);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08003553
Jens Axboe6c271ce2019-01-10 11:22:30 -07003554 old_fs = get_fs();
3555 set_fs(USER_DS);
Jens Axboe181e4482019-11-25 08:52:30 -07003556 old_cred = override_creds(ctx->creds);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003557
Jens Axboec1edbf52019-11-10 16:56:04 -07003558 ret = timeout = inflight = 0;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003559 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003560 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003561
3562 if (inflight) {
3563 unsigned nr_events = 0;
3564
3565 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe2b2ed972019-10-25 10:06:15 -06003566 /*
3567 * inflight is the count of the maximum possible
3568 * entries we submitted, but it can be smaller
3569 * if we dropped some of them. If we don't have
3570 * poll entries available, then we know that we
3571 * have nothing left to poll for. Reset the
3572 * inflight count to zero in that case.
3573 */
3574 mutex_lock(&ctx->uring_lock);
3575 if (!list_empty(&ctx->poll_list))
3576 __io_iopoll_check(ctx, &nr_events, 0);
3577 else
3578 inflight = 0;
3579 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003580 } else {
3581 /*
3582 * Normal IO, just pretend everything completed.
3583 * We don't have to poll completions for that.
3584 */
3585 nr_events = inflight;
3586 }
3587
3588 inflight -= nr_events;
3589 if (!inflight)
3590 timeout = jiffies + ctx->sq_thread_idle;
3591 }
3592
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003593 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07003594
3595 /*
3596 * If submit got -EBUSY, flag us as needing the application
3597 * to enter the kernel to reap and flush events.
3598 */
3599 if (!to_submit || ret == -EBUSY) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07003600 /*
3601 * We're polling. If we're within the defined idle
3602 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07003603 * to sleep. The exception is if we got EBUSY doing
3604 * more IO, we should wait for the application to
3605 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07003606 */
Jens Axboec1edbf52019-11-10 16:56:04 -07003607 if (inflight ||
3608 (!time_after(jiffies, timeout) && ret != -EBUSY)) {
Jens Axboe9831a902019-09-19 09:48:55 -06003609 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07003610 continue;
3611 }
3612
3613 /*
3614 * Drop cur_mm before scheduling, we can't hold it for
3615 * long periods (or over schedule()). Do this before
3616 * adding ourselves to the waitqueue, as the unuse/drop
3617 * may sleep.
3618 */
3619 if (cur_mm) {
3620 unuse_mm(cur_mm);
3621 mmput(cur_mm);
3622 cur_mm = NULL;
3623 }
3624
3625 prepare_to_wait(&ctx->sqo_wait, &wait,
3626 TASK_INTERRUPTIBLE);
3627
3628 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00003629 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02003630 /* make sure to read SQ tail after writing flags */
3631 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07003632
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003633 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07003634 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003635 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07003636 finish_wait(&ctx->sqo_wait, &wait);
3637 break;
3638 }
3639 if (signal_pending(current))
3640 flush_signals(current);
3641 schedule();
3642 finish_wait(&ctx->sqo_wait, &wait);
3643
Hristo Venev75b28af2019-08-26 17:23:46 +00003644 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003645 continue;
3646 }
3647 finish_wait(&ctx->sqo_wait, &wait);
3648
Hristo Venev75b28af2019-08-26 17:23:46 +00003649 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003650 }
3651
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003652 to_submit = min(to_submit, ctx->sq_entries);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003653 ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true);
3654 if (ret > 0)
3655 inflight += ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003656 }
3657
3658 set_fs(old_fs);
3659 if (cur_mm) {
3660 unuse_mm(cur_mm);
3661 mmput(cur_mm);
3662 }
Jens Axboe181e4482019-11-25 08:52:30 -07003663 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06003664
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003665 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06003666
Jens Axboe6c271ce2019-01-10 11:22:30 -07003667 return 0;
3668}
3669
Jens Axboebda52162019-09-24 13:47:15 -06003670struct io_wait_queue {
3671 struct wait_queue_entry wq;
3672 struct io_ring_ctx *ctx;
3673 unsigned to_wait;
3674 unsigned nr_timeouts;
3675};
3676
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003677static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06003678{
3679 struct io_ring_ctx *ctx = iowq->ctx;
3680
3681 /*
3682 * Wake up if we have enough events, or if a timeout occured since we
3683 * started waiting. For timeouts, we always want to return to userspace,
3684 * regardless of event count.
3685 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003686 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06003687 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
3688}
3689
3690static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
3691 int wake_flags, void *key)
3692{
3693 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
3694 wq);
3695
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003696 /* use noflush == true, as we can't safely rely on locking context */
3697 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06003698 return -1;
3699
3700 return autoremove_wake_function(curr, mode, wake_flags, key);
3701}
3702
Jens Axboe2b188cc2019-01-07 10:46:33 -07003703/*
3704 * Wait until events become available, if we don't already have some. The
3705 * application must reap them itself, as they reside on the shared cq ring.
3706 */
3707static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
3708 const sigset_t __user *sig, size_t sigsz)
3709{
Jens Axboebda52162019-09-24 13:47:15 -06003710 struct io_wait_queue iowq = {
3711 .wq = {
3712 .private = current,
3713 .func = io_wake_function,
3714 .entry = LIST_HEAD_INIT(iowq.wq.entry),
3715 },
3716 .ctx = ctx,
3717 .to_wait = min_events,
3718 };
Hristo Venev75b28af2019-08-26 17:23:46 +00003719 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08003720 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003721
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003722 if (io_cqring_events(ctx, false) >= min_events)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003723 return 0;
3724
3725 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01003726#ifdef CONFIG_COMPAT
3727 if (in_compat_syscall())
3728 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07003729 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01003730 else
3731#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07003732 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01003733
Jens Axboe2b188cc2019-01-07 10:46:33 -07003734 if (ret)
3735 return ret;
3736 }
3737
Jens Axboebda52162019-09-24 13:47:15 -06003738 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02003739 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06003740 do {
3741 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
3742 TASK_INTERRUPTIBLE);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003743 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06003744 break;
3745 schedule();
3746 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08003747 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06003748 break;
3749 }
3750 } while (1);
3751 finish_wait(&ctx->wait, &iowq.wq);
3752
Jackie Liue9ffa5c2019-10-29 11:16:42 +08003753 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003754
Hristo Venev75b28af2019-08-26 17:23:46 +00003755 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003756}
3757
Jens Axboe6b063142019-01-10 22:13:58 -07003758static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
3759{
3760#if defined(CONFIG_UNIX)
3761 if (ctx->ring_sock) {
3762 struct sock *sock = ctx->ring_sock->sk;
3763 struct sk_buff *skb;
3764
3765 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
3766 kfree_skb(skb);
3767 }
3768#else
3769 int i;
3770
Jens Axboe65e19f52019-10-26 07:20:21 -06003771 for (i = 0; i < ctx->nr_user_files; i++) {
3772 struct file *file;
3773
3774 file = io_file_from_index(ctx, i);
3775 if (file)
3776 fput(file);
3777 }
Jens Axboe6b063142019-01-10 22:13:58 -07003778#endif
3779}
3780
3781static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
3782{
Jens Axboe65e19f52019-10-26 07:20:21 -06003783 unsigned nr_tables, i;
3784
3785 if (!ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003786 return -ENXIO;
3787
3788 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06003789 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
3790 for (i = 0; i < nr_tables; i++)
3791 kfree(ctx->file_table[i].files);
3792 kfree(ctx->file_table);
3793 ctx->file_table = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003794 ctx->nr_user_files = 0;
3795 return 0;
3796}
3797
Jens Axboe6c271ce2019-01-10 11:22:30 -07003798static void io_sq_thread_stop(struct io_ring_ctx *ctx)
3799{
3800 if (ctx->sqo_thread) {
Jens Axboe206aefd2019-11-07 18:27:42 -07003801 wait_for_completion(&ctx->completions[1]);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003802 /*
3803 * The park is a bit of a work-around, without it we get
3804 * warning spews on shutdown with SQPOLL set and affinity
3805 * set to a single CPU.
3806 */
Jens Axboe06058632019-04-13 09:26:03 -06003807 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003808 kthread_stop(ctx->sqo_thread);
3809 ctx->sqo_thread = NULL;
3810 }
3811}
3812
Jens Axboe6b063142019-01-10 22:13:58 -07003813static void io_finish_async(struct io_ring_ctx *ctx)
3814{
Jens Axboe6c271ce2019-01-10 11:22:30 -07003815 io_sq_thread_stop(ctx);
3816
Jens Axboe561fb042019-10-24 07:25:42 -06003817 if (ctx->io_wq) {
3818 io_wq_destroy(ctx->io_wq);
3819 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003820 }
3821}
3822
3823#if defined(CONFIG_UNIX)
3824static void io_destruct_skb(struct sk_buff *skb)
3825{
3826 struct io_ring_ctx *ctx = skb->sk->sk_user_data;
3827
Jens Axboe561fb042019-10-24 07:25:42 -06003828 if (ctx->io_wq)
3829 io_wq_flush(ctx->io_wq);
Jens Axboe8a997342019-10-09 14:40:13 -06003830
Jens Axboe6b063142019-01-10 22:13:58 -07003831 unix_destruct_scm(skb);
3832}
3833
3834/*
3835 * Ensure the UNIX gc is aware of our file set, so we are certain that
3836 * the io_uring can be safely unregistered on process exit, even if we have
3837 * loops in the file referencing.
3838 */
3839static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
3840{
3841 struct sock *sk = ctx->ring_sock->sk;
3842 struct scm_fp_list *fpl;
3843 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06003844 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07003845
3846 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
3847 unsigned long inflight = ctx->user->unix_inflight + nr;
3848
3849 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
3850 return -EMFILE;
3851 }
3852
3853 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
3854 if (!fpl)
3855 return -ENOMEM;
3856
3857 skb = alloc_skb(0, GFP_KERNEL);
3858 if (!skb) {
3859 kfree(fpl);
3860 return -ENOMEM;
3861 }
3862
3863 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07003864
Jens Axboe08a45172019-10-03 08:11:03 -06003865 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07003866 fpl->user = get_uid(ctx->user);
3867 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003868 struct file *file = io_file_from_index(ctx, i + offset);
3869
3870 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06003871 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06003872 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06003873 unix_inflight(fpl->user, fpl->fp[nr_files]);
3874 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07003875 }
3876
Jens Axboe08a45172019-10-03 08:11:03 -06003877 if (nr_files) {
3878 fpl->max = SCM_MAX_FD;
3879 fpl->count = nr_files;
3880 UNIXCB(skb).fp = fpl;
3881 skb->destructor = io_destruct_skb;
3882 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
3883 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07003884
Jens Axboe08a45172019-10-03 08:11:03 -06003885 for (i = 0; i < nr_files; i++)
3886 fput(fpl->fp[i]);
3887 } else {
3888 kfree_skb(skb);
3889 kfree(fpl);
3890 }
Jens Axboe6b063142019-01-10 22:13:58 -07003891
3892 return 0;
3893}
3894
3895/*
3896 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
3897 * causes regular reference counting to break down. We rely on the UNIX
3898 * garbage collection to take care of this problem for us.
3899 */
3900static int io_sqe_files_scm(struct io_ring_ctx *ctx)
3901{
3902 unsigned left, total;
3903 int ret = 0;
3904
3905 total = 0;
3906 left = ctx->nr_user_files;
3907 while (left) {
3908 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07003909
3910 ret = __io_sqe_files_scm(ctx, this_files, total);
3911 if (ret)
3912 break;
3913 left -= this_files;
3914 total += this_files;
3915 }
3916
3917 if (!ret)
3918 return 0;
3919
3920 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003921 struct file *file = io_file_from_index(ctx, total);
3922
3923 if (file)
3924 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07003925 total++;
3926 }
3927
3928 return ret;
3929}
3930#else
3931static int io_sqe_files_scm(struct io_ring_ctx *ctx)
3932{
3933 return 0;
3934}
3935#endif
3936
Jens Axboe65e19f52019-10-26 07:20:21 -06003937static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
3938 unsigned nr_files)
3939{
3940 int i;
3941
3942 for (i = 0; i < nr_tables; i++) {
3943 struct fixed_file_table *table = &ctx->file_table[i];
3944 unsigned this_files;
3945
3946 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
3947 table->files = kcalloc(this_files, sizeof(struct file *),
3948 GFP_KERNEL);
3949 if (!table->files)
3950 break;
3951 nr_files -= this_files;
3952 }
3953
3954 if (i == nr_tables)
3955 return 0;
3956
3957 for (i = 0; i < nr_tables; i++) {
3958 struct fixed_file_table *table = &ctx->file_table[i];
3959 kfree(table->files);
3960 }
3961 return 1;
3962}
3963
Jens Axboe6b063142019-01-10 22:13:58 -07003964static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
3965 unsigned nr_args)
3966{
3967 __s32 __user *fds = (__s32 __user *) arg;
Jens Axboe65e19f52019-10-26 07:20:21 -06003968 unsigned nr_tables;
Jens Axboe6b063142019-01-10 22:13:58 -07003969 int fd, ret = 0;
3970 unsigned i;
3971
Jens Axboe65e19f52019-10-26 07:20:21 -06003972 if (ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003973 return -EBUSY;
3974 if (!nr_args)
3975 return -EINVAL;
3976 if (nr_args > IORING_MAX_FIXED_FILES)
3977 return -EMFILE;
3978
Jens Axboe65e19f52019-10-26 07:20:21 -06003979 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
3980 ctx->file_table = kcalloc(nr_tables, sizeof(struct fixed_file_table),
3981 GFP_KERNEL);
3982 if (!ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003983 return -ENOMEM;
3984
Jens Axboe65e19f52019-10-26 07:20:21 -06003985 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
3986 kfree(ctx->file_table);
Jens Axboe46568e92019-11-10 08:40:53 -07003987 ctx->file_table = NULL;
Jens Axboe65e19f52019-10-26 07:20:21 -06003988 return -ENOMEM;
3989 }
3990
Jens Axboe08a45172019-10-03 08:11:03 -06003991 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003992 struct fixed_file_table *table;
3993 unsigned index;
3994
Jens Axboe6b063142019-01-10 22:13:58 -07003995 ret = -EFAULT;
3996 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
3997 break;
Jens Axboe08a45172019-10-03 08:11:03 -06003998 /* allow sparse sets */
3999 if (fd == -1) {
4000 ret = 0;
4001 continue;
4002 }
Jens Axboe6b063142019-01-10 22:13:58 -07004003
Jens Axboe65e19f52019-10-26 07:20:21 -06004004 table = &ctx->file_table[i >> IORING_FILE_TABLE_SHIFT];
4005 index = i & IORING_FILE_TABLE_MASK;
4006 table->files[index] = fget(fd);
Jens Axboe6b063142019-01-10 22:13:58 -07004007
4008 ret = -EBADF;
Jens Axboe65e19f52019-10-26 07:20:21 -06004009 if (!table->files[index])
Jens Axboe6b063142019-01-10 22:13:58 -07004010 break;
4011 /*
4012 * Don't allow io_uring instances to be registered. If UNIX
4013 * isn't enabled, then this causes a reference cycle and this
4014 * instance can never get freed. If UNIX is enabled we'll
4015 * handle it just fine, but there's still no point in allowing
4016 * a ring fd as it doesn't support regular read/write anyway.
4017 */
Jens Axboe65e19f52019-10-26 07:20:21 -06004018 if (table->files[index]->f_op == &io_uring_fops) {
4019 fput(table->files[index]);
Jens Axboe6b063142019-01-10 22:13:58 -07004020 break;
4021 }
Jens Axboe6b063142019-01-10 22:13:58 -07004022 ret = 0;
4023 }
4024
4025 if (ret) {
Jens Axboe65e19f52019-10-26 07:20:21 -06004026 for (i = 0; i < ctx->nr_user_files; i++) {
4027 struct file *file;
Jens Axboe6b063142019-01-10 22:13:58 -07004028
Jens Axboe65e19f52019-10-26 07:20:21 -06004029 file = io_file_from_index(ctx, i);
4030 if (file)
4031 fput(file);
4032 }
4033 for (i = 0; i < nr_tables; i++)
4034 kfree(ctx->file_table[i].files);
4035
4036 kfree(ctx->file_table);
4037 ctx->file_table = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07004038 ctx->nr_user_files = 0;
4039 return ret;
4040 }
4041
4042 ret = io_sqe_files_scm(ctx);
4043 if (ret)
4044 io_sqe_files_unregister(ctx);
4045
4046 return ret;
4047}
4048
Jens Axboec3a31e62019-10-03 13:59:56 -06004049static void io_sqe_file_unregister(struct io_ring_ctx *ctx, int index)
4050{
4051#if defined(CONFIG_UNIX)
Jens Axboe65e19f52019-10-26 07:20:21 -06004052 struct file *file = io_file_from_index(ctx, index);
Jens Axboec3a31e62019-10-03 13:59:56 -06004053 struct sock *sock = ctx->ring_sock->sk;
4054 struct sk_buff_head list, *head = &sock->sk_receive_queue;
4055 struct sk_buff *skb;
4056 int i;
4057
4058 __skb_queue_head_init(&list);
4059
4060 /*
4061 * Find the skb that holds this file in its SCM_RIGHTS. When found,
4062 * remove this entry and rearrange the file array.
4063 */
4064 skb = skb_dequeue(head);
4065 while (skb) {
4066 struct scm_fp_list *fp;
4067
4068 fp = UNIXCB(skb).fp;
4069 for (i = 0; i < fp->count; i++) {
4070 int left;
4071
4072 if (fp->fp[i] != file)
4073 continue;
4074
4075 unix_notinflight(fp->user, fp->fp[i]);
4076 left = fp->count - 1 - i;
4077 if (left) {
4078 memmove(&fp->fp[i], &fp->fp[i + 1],
4079 left * sizeof(struct file *));
4080 }
4081 fp->count--;
4082 if (!fp->count) {
4083 kfree_skb(skb);
4084 skb = NULL;
4085 } else {
4086 __skb_queue_tail(&list, skb);
4087 }
4088 fput(file);
4089 file = NULL;
4090 break;
4091 }
4092
4093 if (!file)
4094 break;
4095
4096 __skb_queue_tail(&list, skb);
4097
4098 skb = skb_dequeue(head);
4099 }
4100
4101 if (skb_peek(&list)) {
4102 spin_lock_irq(&head->lock);
4103 while ((skb = __skb_dequeue(&list)) != NULL)
4104 __skb_queue_tail(head, skb);
4105 spin_unlock_irq(&head->lock);
4106 }
4107#else
Jens Axboe65e19f52019-10-26 07:20:21 -06004108 fput(io_file_from_index(ctx, index));
Jens Axboec3a31e62019-10-03 13:59:56 -06004109#endif
4110}
4111
4112static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
4113 int index)
4114{
4115#if defined(CONFIG_UNIX)
4116 struct sock *sock = ctx->ring_sock->sk;
4117 struct sk_buff_head *head = &sock->sk_receive_queue;
4118 struct sk_buff *skb;
4119
4120 /*
4121 * See if we can merge this file into an existing skb SCM_RIGHTS
4122 * file set. If there's no room, fall back to allocating a new skb
4123 * and filling it in.
4124 */
4125 spin_lock_irq(&head->lock);
4126 skb = skb_peek(head);
4127 if (skb) {
4128 struct scm_fp_list *fpl = UNIXCB(skb).fp;
4129
4130 if (fpl->count < SCM_MAX_FD) {
4131 __skb_unlink(skb, head);
4132 spin_unlock_irq(&head->lock);
4133 fpl->fp[fpl->count] = get_file(file);
4134 unix_inflight(fpl->user, fpl->fp[fpl->count]);
4135 fpl->count++;
4136 spin_lock_irq(&head->lock);
4137 __skb_queue_head(head, skb);
4138 } else {
4139 skb = NULL;
4140 }
4141 }
4142 spin_unlock_irq(&head->lock);
4143
4144 if (skb) {
4145 fput(file);
4146 return 0;
4147 }
4148
4149 return __io_sqe_files_scm(ctx, 1, index);
4150#else
4151 return 0;
4152#endif
4153}
4154
4155static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
4156 unsigned nr_args)
4157{
4158 struct io_uring_files_update up;
4159 __s32 __user *fds;
4160 int fd, i, err;
4161 __u32 done;
4162
Jens Axboe65e19f52019-10-26 07:20:21 -06004163 if (!ctx->file_table)
Jens Axboec3a31e62019-10-03 13:59:56 -06004164 return -ENXIO;
4165 if (!nr_args)
4166 return -EINVAL;
4167 if (copy_from_user(&up, arg, sizeof(up)))
4168 return -EFAULT;
4169 if (check_add_overflow(up.offset, nr_args, &done))
4170 return -EOVERFLOW;
4171 if (done > ctx->nr_user_files)
4172 return -EINVAL;
4173
4174 done = 0;
4175 fds = (__s32 __user *) up.fds;
4176 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06004177 struct fixed_file_table *table;
4178 unsigned index;
4179
Jens Axboec3a31e62019-10-03 13:59:56 -06004180 err = 0;
4181 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
4182 err = -EFAULT;
4183 break;
4184 }
4185 i = array_index_nospec(up.offset, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06004186 table = &ctx->file_table[i >> IORING_FILE_TABLE_SHIFT];
4187 index = i & IORING_FILE_TABLE_MASK;
4188 if (table->files[index]) {
Jens Axboec3a31e62019-10-03 13:59:56 -06004189 io_sqe_file_unregister(ctx, i);
Jens Axboe65e19f52019-10-26 07:20:21 -06004190 table->files[index] = NULL;
Jens Axboec3a31e62019-10-03 13:59:56 -06004191 }
4192 if (fd != -1) {
4193 struct file *file;
4194
4195 file = fget(fd);
4196 if (!file) {
4197 err = -EBADF;
4198 break;
4199 }
4200 /*
4201 * Don't allow io_uring instances to be registered. If
4202 * UNIX isn't enabled, then this causes a reference
4203 * cycle and this instance can never get freed. If UNIX
4204 * is enabled we'll handle it just fine, but there's
4205 * still no point in allowing a ring fd as it doesn't
4206 * support regular read/write anyway.
4207 */
4208 if (file->f_op == &io_uring_fops) {
4209 fput(file);
4210 err = -EBADF;
4211 break;
4212 }
Jens Axboe65e19f52019-10-26 07:20:21 -06004213 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06004214 err = io_sqe_file_register(ctx, file, i);
4215 if (err)
4216 break;
4217 }
4218 nr_args--;
4219 done++;
4220 up.offset++;
4221 }
4222
4223 return done ? done : err;
4224}
4225
Jens Axboe7d723062019-11-12 22:31:31 -07004226static void io_put_work(struct io_wq_work *work)
4227{
4228 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
4229
4230 io_put_req(req);
4231}
4232
4233static void io_get_work(struct io_wq_work *work)
4234{
4235 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
4236
4237 refcount_inc(&req->refs);
4238}
4239
Jens Axboe6c271ce2019-01-10 11:22:30 -07004240static int io_sq_offload_start(struct io_ring_ctx *ctx,
4241 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004242{
Jens Axboe576a3472019-11-25 08:49:20 -07004243 struct io_wq_data data;
Jens Axboe561fb042019-10-24 07:25:42 -06004244 unsigned concurrency;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004245 int ret;
4246
Jens Axboe6c271ce2019-01-10 11:22:30 -07004247 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004248 mmgrab(current->mm);
4249 ctx->sqo_mm = current->mm;
4250
Jens Axboe6c271ce2019-01-10 11:22:30 -07004251 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06004252 ret = -EPERM;
4253 if (!capable(CAP_SYS_ADMIN))
4254 goto err;
4255
Jens Axboe917257d2019-04-13 09:28:55 -06004256 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
4257 if (!ctx->sq_thread_idle)
4258 ctx->sq_thread_idle = HZ;
4259
Jens Axboe6c271ce2019-01-10 11:22:30 -07004260 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06004261 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004262
Jens Axboe917257d2019-04-13 09:28:55 -06004263 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06004264 if (cpu >= nr_cpu_ids)
4265 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08004266 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06004267 goto err;
4268
Jens Axboe6c271ce2019-01-10 11:22:30 -07004269 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
4270 ctx, cpu,
4271 "io_uring-sq");
4272 } else {
4273 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
4274 "io_uring-sq");
4275 }
4276 if (IS_ERR(ctx->sqo_thread)) {
4277 ret = PTR_ERR(ctx->sqo_thread);
4278 ctx->sqo_thread = NULL;
4279 goto err;
4280 }
4281 wake_up_process(ctx->sqo_thread);
4282 } else if (p->flags & IORING_SETUP_SQ_AFF) {
4283 /* Can't have SQ_AFF without SQPOLL */
4284 ret = -EINVAL;
4285 goto err;
4286 }
4287
Jens Axboe576a3472019-11-25 08:49:20 -07004288 data.mm = ctx->sqo_mm;
4289 data.user = ctx->user;
Jens Axboe181e4482019-11-25 08:52:30 -07004290 data.creds = ctx->creds;
Jens Axboe576a3472019-11-25 08:49:20 -07004291 data.get_work = io_get_work;
4292 data.put_work = io_put_work;
4293
Jens Axboe561fb042019-10-24 07:25:42 -06004294 /* Do QD, or 4 * CPUS, whatever is smallest */
4295 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
Jens Axboe576a3472019-11-25 08:49:20 -07004296 ctx->io_wq = io_wq_create(concurrency, &data);
Jens Axboe975c99a52019-10-30 08:42:56 -06004297 if (IS_ERR(ctx->io_wq)) {
4298 ret = PTR_ERR(ctx->io_wq);
4299 ctx->io_wq = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004300 goto err;
4301 }
4302
4303 return 0;
4304err:
Jens Axboe54a91f32019-09-10 09:15:04 -06004305 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004306 mmdrop(ctx->sqo_mm);
4307 ctx->sqo_mm = NULL;
4308 return ret;
4309}
4310
4311static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
4312{
4313 atomic_long_sub(nr_pages, &user->locked_vm);
4314}
4315
4316static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
4317{
4318 unsigned long page_limit, cur_pages, new_pages;
4319
4320 /* Don't allow more pages than we can safely lock */
4321 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
4322
4323 do {
4324 cur_pages = atomic_long_read(&user->locked_vm);
4325 new_pages = cur_pages + nr_pages;
4326 if (new_pages > page_limit)
4327 return -ENOMEM;
4328 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
4329 new_pages) != cur_pages);
4330
4331 return 0;
4332}
4333
4334static void io_mem_free(void *ptr)
4335{
Mark Rutland52e04ef2019-04-30 17:30:21 +01004336 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004337
Mark Rutland52e04ef2019-04-30 17:30:21 +01004338 if (!ptr)
4339 return;
4340
4341 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004342 if (put_page_testzero(page))
4343 free_compound_page(page);
4344}
4345
4346static void *io_mem_alloc(size_t size)
4347{
4348 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
4349 __GFP_NORETRY;
4350
4351 return (void *) __get_free_pages(gfp_flags, get_order(size));
4352}
4353
Hristo Venev75b28af2019-08-26 17:23:46 +00004354static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
4355 size_t *sq_offset)
4356{
4357 struct io_rings *rings;
4358 size_t off, sq_array_size;
4359
4360 off = struct_size(rings, cqes, cq_entries);
4361 if (off == SIZE_MAX)
4362 return SIZE_MAX;
4363
4364#ifdef CONFIG_SMP
4365 off = ALIGN(off, SMP_CACHE_BYTES);
4366 if (off == 0)
4367 return SIZE_MAX;
4368#endif
4369
4370 sq_array_size = array_size(sizeof(u32), sq_entries);
4371 if (sq_array_size == SIZE_MAX)
4372 return SIZE_MAX;
4373
4374 if (check_add_overflow(off, sq_array_size, &off))
4375 return SIZE_MAX;
4376
4377 if (sq_offset)
4378 *sq_offset = off;
4379
4380 return off;
4381}
4382
Jens Axboe2b188cc2019-01-07 10:46:33 -07004383static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
4384{
Hristo Venev75b28af2019-08-26 17:23:46 +00004385 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004386
Hristo Venev75b28af2019-08-26 17:23:46 +00004387 pages = (size_t)1 << get_order(
4388 rings_size(sq_entries, cq_entries, NULL));
4389 pages += (size_t)1 << get_order(
4390 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07004391
Hristo Venev75b28af2019-08-26 17:23:46 +00004392 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004393}
4394
Jens Axboeedafcce2019-01-09 09:16:05 -07004395static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
4396{
4397 int i, j;
4398
4399 if (!ctx->user_bufs)
4400 return -ENXIO;
4401
4402 for (i = 0; i < ctx->nr_user_bufs; i++) {
4403 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
4404
4405 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbard27c4d3a2019-08-04 19:32:06 -07004406 put_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07004407
4408 if (ctx->account_mem)
4409 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004410 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07004411 imu->nr_bvecs = 0;
4412 }
4413
4414 kfree(ctx->user_bufs);
4415 ctx->user_bufs = NULL;
4416 ctx->nr_user_bufs = 0;
4417 return 0;
4418}
4419
4420static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
4421 void __user *arg, unsigned index)
4422{
4423 struct iovec __user *src;
4424
4425#ifdef CONFIG_COMPAT
4426 if (ctx->compat) {
4427 struct compat_iovec __user *ciovs;
4428 struct compat_iovec ciov;
4429
4430 ciovs = (struct compat_iovec __user *) arg;
4431 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
4432 return -EFAULT;
4433
4434 dst->iov_base = (void __user *) (unsigned long) ciov.iov_base;
4435 dst->iov_len = ciov.iov_len;
4436 return 0;
4437 }
4438#endif
4439 src = (struct iovec __user *) arg;
4440 if (copy_from_user(dst, &src[index], sizeof(*dst)))
4441 return -EFAULT;
4442 return 0;
4443}
4444
4445static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
4446 unsigned nr_args)
4447{
4448 struct vm_area_struct **vmas = NULL;
4449 struct page **pages = NULL;
4450 int i, j, got_pages = 0;
4451 int ret = -EINVAL;
4452
4453 if (ctx->user_bufs)
4454 return -EBUSY;
4455 if (!nr_args || nr_args > UIO_MAXIOV)
4456 return -EINVAL;
4457
4458 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
4459 GFP_KERNEL);
4460 if (!ctx->user_bufs)
4461 return -ENOMEM;
4462
4463 for (i = 0; i < nr_args; i++) {
4464 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
4465 unsigned long off, start, end, ubuf;
4466 int pret, nr_pages;
4467 struct iovec iov;
4468 size_t size;
4469
4470 ret = io_copy_iov(ctx, &iov, arg, i);
4471 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03004472 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07004473
4474 /*
4475 * Don't impose further limits on the size and buffer
4476 * constraints here, we'll -EINVAL later when IO is
4477 * submitted if they are wrong.
4478 */
4479 ret = -EFAULT;
4480 if (!iov.iov_base || !iov.iov_len)
4481 goto err;
4482
4483 /* arbitrary limit, but we need something */
4484 if (iov.iov_len > SZ_1G)
4485 goto err;
4486
4487 ubuf = (unsigned long) iov.iov_base;
4488 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
4489 start = ubuf >> PAGE_SHIFT;
4490 nr_pages = end - start;
4491
4492 if (ctx->account_mem) {
4493 ret = io_account_mem(ctx->user, nr_pages);
4494 if (ret)
4495 goto err;
4496 }
4497
4498 ret = 0;
4499 if (!pages || nr_pages > got_pages) {
4500 kfree(vmas);
4501 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004502 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07004503 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004504 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07004505 sizeof(struct vm_area_struct *),
4506 GFP_KERNEL);
4507 if (!pages || !vmas) {
4508 ret = -ENOMEM;
4509 if (ctx->account_mem)
4510 io_unaccount_mem(ctx->user, nr_pages);
4511 goto err;
4512 }
4513 got_pages = nr_pages;
4514 }
4515
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004516 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07004517 GFP_KERNEL);
4518 ret = -ENOMEM;
4519 if (!imu->bvec) {
4520 if (ctx->account_mem)
4521 io_unaccount_mem(ctx->user, nr_pages);
4522 goto err;
4523 }
4524
4525 ret = 0;
4526 down_read(&current->mm->mmap_sem);
Ira Weiny932f4a62019-05-13 17:17:03 -07004527 pret = get_user_pages(ubuf, nr_pages,
4528 FOLL_WRITE | FOLL_LONGTERM,
4529 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07004530 if (pret == nr_pages) {
4531 /* don't support file backed memory */
4532 for (j = 0; j < nr_pages; j++) {
4533 struct vm_area_struct *vma = vmas[j];
4534
4535 if (vma->vm_file &&
4536 !is_file_hugepages(vma->vm_file)) {
4537 ret = -EOPNOTSUPP;
4538 break;
4539 }
4540 }
4541 } else {
4542 ret = pret < 0 ? pret : -EFAULT;
4543 }
4544 up_read(&current->mm->mmap_sem);
4545 if (ret) {
4546 /*
4547 * if we did partial map, or found file backed vmas,
4548 * release any pages we did get
4549 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07004550 if (pret > 0)
4551 put_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07004552 if (ctx->account_mem)
4553 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004554 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07004555 goto err;
4556 }
4557
4558 off = ubuf & ~PAGE_MASK;
4559 size = iov.iov_len;
4560 for (j = 0; j < nr_pages; j++) {
4561 size_t vec_len;
4562
4563 vec_len = min_t(size_t, size, PAGE_SIZE - off);
4564 imu->bvec[j].bv_page = pages[j];
4565 imu->bvec[j].bv_len = vec_len;
4566 imu->bvec[j].bv_offset = off;
4567 off = 0;
4568 size -= vec_len;
4569 }
4570 /* store original address for later verification */
4571 imu->ubuf = ubuf;
4572 imu->len = iov.iov_len;
4573 imu->nr_bvecs = nr_pages;
4574
4575 ctx->nr_user_bufs++;
4576 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004577 kvfree(pages);
4578 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07004579 return 0;
4580err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004581 kvfree(pages);
4582 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07004583 io_sqe_buffer_unregister(ctx);
4584 return ret;
4585}
4586
Jens Axboe9b402842019-04-11 11:45:41 -06004587static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
4588{
4589 __s32 __user *fds = arg;
4590 int fd;
4591
4592 if (ctx->cq_ev_fd)
4593 return -EBUSY;
4594
4595 if (copy_from_user(&fd, fds, sizeof(*fds)))
4596 return -EFAULT;
4597
4598 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
4599 if (IS_ERR(ctx->cq_ev_fd)) {
4600 int ret = PTR_ERR(ctx->cq_ev_fd);
4601 ctx->cq_ev_fd = NULL;
4602 return ret;
4603 }
4604
4605 return 0;
4606}
4607
4608static int io_eventfd_unregister(struct io_ring_ctx *ctx)
4609{
4610 if (ctx->cq_ev_fd) {
4611 eventfd_ctx_put(ctx->cq_ev_fd);
4612 ctx->cq_ev_fd = NULL;
4613 return 0;
4614 }
4615
4616 return -ENXIO;
4617}
4618
Jens Axboe2b188cc2019-01-07 10:46:33 -07004619static void io_ring_ctx_free(struct io_ring_ctx *ctx)
4620{
Jens Axboe6b063142019-01-10 22:13:58 -07004621 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004622 if (ctx->sqo_mm)
4623 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07004624
4625 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07004626 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07004627 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06004628 io_eventfd_unregister(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07004629
Jens Axboe2b188cc2019-01-07 10:46:33 -07004630#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07004631 if (ctx->ring_sock) {
4632 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004633 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07004634 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004635#endif
4636
Hristo Venev75b28af2019-08-26 17:23:46 +00004637 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004638 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004639
4640 percpu_ref_exit(&ctx->refs);
4641 if (ctx->account_mem)
4642 io_unaccount_mem(ctx->user,
4643 ring_pages(ctx->sq_entries, ctx->cq_entries));
4644 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07004645 put_cred(ctx->creds);
Jens Axboe206aefd2019-11-07 18:27:42 -07004646 kfree(ctx->completions);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07004647 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004648 kfree(ctx);
4649}
4650
4651static __poll_t io_uring_poll(struct file *file, poll_table *wait)
4652{
4653 struct io_ring_ctx *ctx = file->private_data;
4654 __poll_t mask = 0;
4655
4656 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02004657 /*
4658 * synchronizes with barrier from wq_has_sleeper call in
4659 * io_commit_cqring
4660 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004661 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00004662 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
4663 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004664 mask |= EPOLLOUT | EPOLLWRNORM;
yangerkundaa5de52019-09-24 20:53:34 +08004665 if (READ_ONCE(ctx->rings->cq.head) != ctx->cached_cq_tail)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004666 mask |= EPOLLIN | EPOLLRDNORM;
4667
4668 return mask;
4669}
4670
4671static int io_uring_fasync(int fd, struct file *file, int on)
4672{
4673 struct io_ring_ctx *ctx = file->private_data;
4674
4675 return fasync_helper(fd, file, on, &ctx->cq_fasync);
4676}
4677
4678static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
4679{
4680 mutex_lock(&ctx->uring_lock);
4681 percpu_ref_kill(&ctx->refs);
4682 mutex_unlock(&ctx->uring_lock);
4683
Jens Axboe5262f562019-09-17 12:26:57 -06004684 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004685 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06004686
4687 if (ctx->io_wq)
4688 io_wq_cancel_all(ctx->io_wq);
4689
Jens Axboedef596e2019-01-09 08:59:42 -07004690 io_iopoll_reap_events(ctx);
Jens Axboe15dff282019-11-13 09:09:23 -07004691 /* if we failed setting up the ctx, we might not have any rings */
4692 if (ctx->rings)
4693 io_cqring_overflow_flush(ctx, true);
Jens Axboe206aefd2019-11-07 18:27:42 -07004694 wait_for_completion(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004695 io_ring_ctx_free(ctx);
4696}
4697
4698static int io_uring_release(struct inode *inode, struct file *file)
4699{
4700 struct io_ring_ctx *ctx = file->private_data;
4701
4702 file->private_data = NULL;
4703 io_ring_ctx_wait_and_kill(ctx);
4704 return 0;
4705}
4706
Jens Axboefcb323c2019-10-24 12:39:47 -06004707static void io_uring_cancel_files(struct io_ring_ctx *ctx,
4708 struct files_struct *files)
4709{
4710 struct io_kiocb *req;
4711 DEFINE_WAIT(wait);
4712
4713 while (!list_empty_careful(&ctx->inflight_list)) {
Jens Axboe768134d2019-11-10 20:30:53 -07004714 struct io_kiocb *cancel_req = NULL;
Jens Axboefcb323c2019-10-24 12:39:47 -06004715
4716 spin_lock_irq(&ctx->inflight_lock);
4717 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07004718 if (req->work.files != files)
4719 continue;
4720 /* req is being completed, ignore */
4721 if (!refcount_inc_not_zero(&req->refs))
4722 continue;
4723 cancel_req = req;
4724 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06004725 }
Jens Axboe768134d2019-11-10 20:30:53 -07004726 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06004727 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07004728 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06004729 spin_unlock_irq(&ctx->inflight_lock);
4730
Jens Axboe768134d2019-11-10 20:30:53 -07004731 /* We need to keep going until we don't find a matching req */
4732 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06004733 break;
Bob Liu2f6d9b92019-11-13 18:06:24 +08004734
4735 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
4736 io_put_req(cancel_req);
Jens Axboefcb323c2019-10-24 12:39:47 -06004737 schedule();
4738 }
Jens Axboe768134d2019-11-10 20:30:53 -07004739 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06004740}
4741
4742static int io_uring_flush(struct file *file, void *data)
4743{
4744 struct io_ring_ctx *ctx = file->private_data;
4745
4746 io_uring_cancel_files(ctx, data);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004747 if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) {
4748 io_cqring_overflow_flush(ctx, true);
Jens Axboefcb323c2019-10-24 12:39:47 -06004749 io_wq_cancel_all(ctx->io_wq);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004750 }
Jens Axboefcb323c2019-10-24 12:39:47 -06004751 return 0;
4752}
4753
Roman Penyaev6c5c2402019-11-28 12:53:22 +01004754static void *io_uring_validate_mmap_request(struct file *file,
4755 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004756{
Jens Axboe2b188cc2019-01-07 10:46:33 -07004757 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01004758 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004759 struct page *page;
4760 void *ptr;
4761
4762 switch (offset) {
4763 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00004764 case IORING_OFF_CQ_RING:
4765 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004766 break;
4767 case IORING_OFF_SQES:
4768 ptr = ctx->sq_sqes;
4769 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004770 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01004771 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004772 }
4773
4774 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07004775 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01004776 return ERR_PTR(-EINVAL);
4777
4778 return ptr;
4779}
4780
4781#ifdef CONFIG_MMU
4782
4783static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
4784{
4785 size_t sz = vma->vm_end - vma->vm_start;
4786 unsigned long pfn;
4787 void *ptr;
4788
4789 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
4790 if (IS_ERR(ptr))
4791 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004792
4793 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
4794 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
4795}
4796
Roman Penyaev6c5c2402019-11-28 12:53:22 +01004797#else /* !CONFIG_MMU */
4798
4799static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
4800{
4801 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
4802}
4803
4804static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
4805{
4806 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
4807}
4808
4809static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
4810 unsigned long addr, unsigned long len,
4811 unsigned long pgoff, unsigned long flags)
4812{
4813 void *ptr;
4814
4815 ptr = io_uring_validate_mmap_request(file, pgoff, len);
4816 if (IS_ERR(ptr))
4817 return PTR_ERR(ptr);
4818
4819 return (unsigned long) ptr;
4820}
4821
4822#endif /* !CONFIG_MMU */
4823
Jens Axboe2b188cc2019-01-07 10:46:33 -07004824SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
4825 u32, min_complete, u32, flags, const sigset_t __user *, sig,
4826 size_t, sigsz)
4827{
4828 struct io_ring_ctx *ctx;
4829 long ret = -EBADF;
4830 int submitted = 0;
4831 struct fd f;
4832
Jens Axboe6c271ce2019-01-10 11:22:30 -07004833 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07004834 return -EINVAL;
4835
4836 f = fdget(fd);
4837 if (!f.file)
4838 return -EBADF;
4839
4840 ret = -EOPNOTSUPP;
4841 if (f.file->f_op != &io_uring_fops)
4842 goto out_fput;
4843
4844 ret = -ENXIO;
4845 ctx = f.file->private_data;
4846 if (!percpu_ref_tryget(&ctx->refs))
4847 goto out_fput;
4848
Jens Axboe6c271ce2019-01-10 11:22:30 -07004849 /*
4850 * For SQ polling, the thread will do all submissions and completions.
4851 * Just return the requested submit count, and wake the thread if
4852 * we were asked to.
4853 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06004854 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004855 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07004856 if (!list_empty_careful(&ctx->cq_overflow_list))
4857 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004858 if (flags & IORING_ENTER_SQ_WAKEUP)
4859 wake_up(&ctx->sqo_wait);
4860 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06004861 } else if (to_submit) {
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004862 struct mm_struct *cur_mm;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004863
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004864 to_submit = min(to_submit, ctx->sq_entries);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004865 mutex_lock(&ctx->uring_lock);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004866 /* already have mm, so io_submit_sqes() won't try to grab it */
4867 cur_mm = ctx->sqo_mm;
4868 submitted = io_submit_sqes(ctx, to_submit, f.file, fd,
4869 &cur_mm, false);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004870 mutex_unlock(&ctx->uring_lock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004871 }
4872 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07004873 unsigned nr_events = 0;
4874
Jens Axboe2b188cc2019-01-07 10:46:33 -07004875 min_complete = min(min_complete, ctx->cq_entries);
4876
Jens Axboedef596e2019-01-09 08:59:42 -07004877 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07004878 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07004879 } else {
4880 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
4881 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004882 }
4883
Pavel Begunkov6805b322019-10-08 02:18:42 +03004884 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004885out_fput:
4886 fdput(f);
4887 return submitted ? submitted : ret;
4888}
4889
4890static const struct file_operations io_uring_fops = {
4891 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06004892 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07004893 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01004894#ifndef CONFIG_MMU
4895 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
4896 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
4897#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07004898 .poll = io_uring_poll,
4899 .fasync = io_uring_fasync,
4900};
4901
4902static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
4903 struct io_uring_params *p)
4904{
Hristo Venev75b28af2019-08-26 17:23:46 +00004905 struct io_rings *rings;
4906 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004907
Hristo Venev75b28af2019-08-26 17:23:46 +00004908 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
4909 if (size == SIZE_MAX)
4910 return -EOVERFLOW;
4911
4912 rings = io_mem_alloc(size);
4913 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004914 return -ENOMEM;
4915
Hristo Venev75b28af2019-08-26 17:23:46 +00004916 ctx->rings = rings;
4917 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
4918 rings->sq_ring_mask = p->sq_entries - 1;
4919 rings->cq_ring_mask = p->cq_entries - 1;
4920 rings->sq_ring_entries = p->sq_entries;
4921 rings->cq_ring_entries = p->cq_entries;
4922 ctx->sq_mask = rings->sq_ring_mask;
4923 ctx->cq_mask = rings->cq_ring_mask;
4924 ctx->sq_entries = rings->sq_ring_entries;
4925 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004926
4927 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07004928 if (size == SIZE_MAX) {
4929 io_mem_free(ctx->rings);
4930 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004931 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07004932 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004933
4934 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07004935 if (!ctx->sq_sqes) {
4936 io_mem_free(ctx->rings);
4937 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004938 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07004939 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004940
Jens Axboe2b188cc2019-01-07 10:46:33 -07004941 return 0;
4942}
4943
4944/*
4945 * Allocate an anonymous fd, this is what constitutes the application
4946 * visible backing of an io_uring instance. The application mmaps this
4947 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
4948 * we have to tie this fd to a socket for file garbage collection purposes.
4949 */
4950static int io_uring_get_fd(struct io_ring_ctx *ctx)
4951{
4952 struct file *file;
4953 int ret;
4954
4955#if defined(CONFIG_UNIX)
4956 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
4957 &ctx->ring_sock);
4958 if (ret)
4959 return ret;
4960#endif
4961
4962 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
4963 if (ret < 0)
4964 goto err;
4965
4966 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
4967 O_RDWR | O_CLOEXEC);
4968 if (IS_ERR(file)) {
4969 put_unused_fd(ret);
4970 ret = PTR_ERR(file);
4971 goto err;
4972 }
4973
4974#if defined(CONFIG_UNIX)
4975 ctx->ring_sock->file = file;
Jens Axboe6b063142019-01-10 22:13:58 -07004976 ctx->ring_sock->sk->sk_user_data = ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004977#endif
4978 fd_install(ret, file);
4979 return ret;
4980err:
4981#if defined(CONFIG_UNIX)
4982 sock_release(ctx->ring_sock);
4983 ctx->ring_sock = NULL;
4984#endif
4985 return ret;
4986}
4987
4988static int io_uring_create(unsigned entries, struct io_uring_params *p)
4989{
4990 struct user_struct *user = NULL;
4991 struct io_ring_ctx *ctx;
4992 bool account_mem;
4993 int ret;
4994
4995 if (!entries || entries > IORING_MAX_ENTRIES)
4996 return -EINVAL;
4997
4998 /*
4999 * Use twice as many entries for the CQ ring. It's possible for the
5000 * application to drive a higher depth than the size of the SQ ring,
5001 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06005002 * some flexibility in overcommitting a bit. If the application has
5003 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
5004 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07005005 */
5006 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06005007 if (p->flags & IORING_SETUP_CQSIZE) {
5008 /*
5009 * If IORING_SETUP_CQSIZE is set, we do the same roundup
5010 * to a power-of-two, if it isn't already. We do NOT impose
5011 * any cq vs sq ring sizing.
5012 */
5013 if (p->cq_entries < p->sq_entries || p->cq_entries > IORING_MAX_CQ_ENTRIES)
5014 return -EINVAL;
5015 p->cq_entries = roundup_pow_of_two(p->cq_entries);
5016 } else {
5017 p->cq_entries = 2 * p->sq_entries;
5018 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07005019
5020 user = get_uid(current_user());
5021 account_mem = !capable(CAP_IPC_LOCK);
5022
5023 if (account_mem) {
5024 ret = io_account_mem(user,
5025 ring_pages(p->sq_entries, p->cq_entries));
5026 if (ret) {
5027 free_uid(user);
5028 return ret;
5029 }
5030 }
5031
5032 ctx = io_ring_ctx_alloc(p);
5033 if (!ctx) {
5034 if (account_mem)
5035 io_unaccount_mem(user, ring_pages(p->sq_entries,
5036 p->cq_entries));
5037 free_uid(user);
5038 return -ENOMEM;
5039 }
5040 ctx->compat = in_compat_syscall();
5041 ctx->account_mem = account_mem;
5042 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07005043 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07005044
5045 ret = io_allocate_scq_urings(ctx, p);
5046 if (ret)
5047 goto err;
5048
Jens Axboe6c271ce2019-01-10 11:22:30 -07005049 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005050 if (ret)
5051 goto err;
5052
Jens Axboe2b188cc2019-01-07 10:46:33 -07005053 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00005054 p->sq_off.head = offsetof(struct io_rings, sq.head);
5055 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
5056 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
5057 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
5058 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
5059 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
5060 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005061
5062 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00005063 p->cq_off.head = offsetof(struct io_rings, cq.head);
5064 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
5065 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
5066 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
5067 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
5068 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06005069
Jens Axboe044c1ab2019-10-28 09:15:33 -06005070 /*
5071 * Install ring fd as the very last thing, so we don't risk someone
5072 * having closed it before we finish setup
5073 */
5074 ret = io_uring_get_fd(ctx);
5075 if (ret < 0)
5076 goto err;
5077
Jens Axboeda8c9692019-12-02 18:51:26 -07005078 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
5079 IORING_FEAT_SUBMIT_STABLE;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02005080 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005081 return ret;
5082err:
5083 io_ring_ctx_wait_and_kill(ctx);
5084 return ret;
5085}
5086
5087/*
5088 * Sets up an aio uring context, and returns the fd. Applications asks for a
5089 * ring size, we return the actual sq/cq ring sizes (among other things) in the
5090 * params structure passed in.
5091 */
5092static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
5093{
5094 struct io_uring_params p;
5095 long ret;
5096 int i;
5097
5098 if (copy_from_user(&p, params, sizeof(p)))
5099 return -EFAULT;
5100 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
5101 if (p.resv[i])
5102 return -EINVAL;
5103 }
5104
Jens Axboe6c271ce2019-01-10 11:22:30 -07005105 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe33a107f2019-10-04 12:10:03 -06005106 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE))
Jens Axboe2b188cc2019-01-07 10:46:33 -07005107 return -EINVAL;
5108
5109 ret = io_uring_create(entries, &p);
5110 if (ret < 0)
5111 return ret;
5112
5113 if (copy_to_user(params, &p, sizeof(p)))
5114 return -EFAULT;
5115
5116 return ret;
5117}
5118
5119SYSCALL_DEFINE2(io_uring_setup, u32, entries,
5120 struct io_uring_params __user *, params)
5121{
5122 return io_uring_setup(entries, params);
5123}
5124
Jens Axboeedafcce2019-01-09 09:16:05 -07005125static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
5126 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06005127 __releases(ctx->uring_lock)
5128 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07005129{
5130 int ret;
5131
Jens Axboe35fa71a2019-04-22 10:23:23 -06005132 /*
5133 * We're inside the ring mutex, if the ref is already dying, then
5134 * someone else killed the ctx or is already going through
5135 * io_uring_register().
5136 */
5137 if (percpu_ref_is_dying(&ctx->refs))
5138 return -ENXIO;
5139
Jens Axboeedafcce2019-01-09 09:16:05 -07005140 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06005141
5142 /*
5143 * Drop uring mutex before waiting for references to exit. If another
5144 * thread is currently inside io_uring_enter() it might need to grab
5145 * the uring_lock to make progress. If we hold it here across the drain
5146 * wait, then we can deadlock. It's safe to drop the mutex here, since
5147 * no new references will come in after we've killed the percpu ref.
5148 */
5149 mutex_unlock(&ctx->uring_lock);
Jens Axboe206aefd2019-11-07 18:27:42 -07005150 wait_for_completion(&ctx->completions[0]);
Jens Axboeb19062a2019-04-15 10:49:38 -06005151 mutex_lock(&ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07005152
5153 switch (opcode) {
5154 case IORING_REGISTER_BUFFERS:
5155 ret = io_sqe_buffer_register(ctx, arg, nr_args);
5156 break;
5157 case IORING_UNREGISTER_BUFFERS:
5158 ret = -EINVAL;
5159 if (arg || nr_args)
5160 break;
5161 ret = io_sqe_buffer_unregister(ctx);
5162 break;
Jens Axboe6b063142019-01-10 22:13:58 -07005163 case IORING_REGISTER_FILES:
5164 ret = io_sqe_files_register(ctx, arg, nr_args);
5165 break;
5166 case IORING_UNREGISTER_FILES:
5167 ret = -EINVAL;
5168 if (arg || nr_args)
5169 break;
5170 ret = io_sqe_files_unregister(ctx);
5171 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06005172 case IORING_REGISTER_FILES_UPDATE:
5173 ret = io_sqe_files_update(ctx, arg, nr_args);
5174 break;
Jens Axboe9b402842019-04-11 11:45:41 -06005175 case IORING_REGISTER_EVENTFD:
5176 ret = -EINVAL;
5177 if (nr_args != 1)
5178 break;
5179 ret = io_eventfd_register(ctx, arg);
5180 break;
5181 case IORING_UNREGISTER_EVENTFD:
5182 ret = -EINVAL;
5183 if (arg || nr_args)
5184 break;
5185 ret = io_eventfd_unregister(ctx);
5186 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07005187 default:
5188 ret = -EINVAL;
5189 break;
5190 }
5191
5192 /* bring the ctx back to life */
Jens Axboe206aefd2019-11-07 18:27:42 -07005193 reinit_completion(&ctx->completions[0]);
Jens Axboeedafcce2019-01-09 09:16:05 -07005194 percpu_ref_reinit(&ctx->refs);
5195 return ret;
5196}
5197
5198SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
5199 void __user *, arg, unsigned int, nr_args)
5200{
5201 struct io_ring_ctx *ctx;
5202 long ret = -EBADF;
5203 struct fd f;
5204
5205 f = fdget(fd);
5206 if (!f.file)
5207 return -EBADF;
5208
5209 ret = -EOPNOTSUPP;
5210 if (f.file->f_op != &io_uring_fops)
5211 goto out_fput;
5212
5213 ctx = f.file->private_data;
5214
5215 mutex_lock(&ctx->uring_lock);
5216 ret = __io_uring_register(ctx, opcode, arg, nr_args);
5217 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02005218 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
5219 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07005220out_fput:
5221 fdput(f);
5222 return ret;
5223}
5224
Jens Axboe2b188cc2019-01-07 10:46:33 -07005225static int __init io_uring_init(void)
5226{
5227 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
5228 return 0;
5229};
5230__initcall(io_uring_init);