blob: 2700382ebcc765868937d5b213e144d19b4d2331 [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 Axboe5262f562019-09-17 12:26:57 -0600306struct io_timeout {
307 struct file *file;
Jens Axboead8a48a2019-11-15 08:49:11 -0700308 struct io_timeout_data *data;
Jens Axboe5262f562019-09-17 12:26:57 -0600309};
310
Jens Axboe03b12302019-12-02 18:50:25 -0700311struct io_async_msghdr {
312 struct iovec fast_iov[UIO_FASTIOV];
313 struct iovec *iov;
314 struct sockaddr __user *uaddr;
315 struct msghdr msg;
316};
317
Jens Axboef67676d2019-12-02 11:03:47 -0700318struct io_async_rw {
319 struct iovec fast_iov[UIO_FASTIOV];
320 struct iovec *iov;
321 ssize_t nr_segs;
322 ssize_t size;
323};
324
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700325struct io_async_ctx {
326 struct io_uring_sqe sqe;
Jens Axboef67676d2019-12-02 11:03:47 -0700327 union {
328 struct io_async_rw rw;
Jens Axboe03b12302019-12-02 18:50:25 -0700329 struct io_async_msghdr msg;
Jens Axboef67676d2019-12-02 11:03:47 -0700330 };
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700331};
332
Jens Axboe09bb8392019-03-13 12:39:28 -0600333/*
334 * NOTE! Each of the iocb union members has the file pointer
335 * as the first entry in their struct definition. So you can
336 * access the file pointer through any of the sub-structs,
337 * or directly as just 'ki_filp' in this struct.
338 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700339struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700340 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600341 struct file *file;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700342 struct kiocb rw;
343 struct io_poll_iocb poll;
Jens Axboe5262f562019-09-17 12:26:57 -0600344 struct io_timeout timeout;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700345 };
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 Axboead8a48a2019-11-15 08:49:11 -0700617 ret = hrtimer_try_to_cancel(&req->timeout.data->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 }
Jens Axboead8a48a2019-11-15 08:49:11 -0700875 if (req->flags & REQ_F_TIMEOUT)
876 kfree(req->timeout.data);
Jens Axboefcb323c2019-10-24 12:39:47 -0600877 percpu_ref_put(&ctx->refs);
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700878 if (likely(!io_is_fallback_req(req)))
879 kmem_cache_free(req_cachep, req);
880 else
881 clear_bit_unlock(0, (unsigned long *) ctx->fallback_req);
Jens Axboee65ef562019-03-12 10:16:44 -0600882}
883
Jackie Liua197f662019-11-08 08:09:12 -0700884static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -0600885{
Jackie Liua197f662019-11-08 08:09:12 -0700886 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -0700887 int ret;
888
Jens Axboead8a48a2019-11-15 08:49:11 -0700889 ret = hrtimer_try_to_cancel(&req->timeout.data->timer);
Jens Axboe2665abf2019-11-05 12:40:47 -0700890 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -0700891 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -0700892 io_commit_cqring(ctx);
893 req->flags &= ~REQ_F_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +0800894 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -0700895 return true;
896 }
897
898 return false;
899}
900
Jens Axboeba816ad2019-09-28 11:36:45 -0600901static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -0600902{
Jens Axboe2665abf2019-11-05 12:40:47 -0700903 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -0600904 struct io_kiocb *nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -0700905 bool wake_ev = false;
Jens Axboe9e645e112019-05-10 16:07:28 -0600906
Jens Axboe4d7dd462019-11-20 13:03:52 -0700907 /* Already got next link */
908 if (req->flags & REQ_F_LINK_NEXT)
909 return;
910
Jens Axboe9e645e112019-05-10 16:07:28 -0600911 /*
912 * The list should never be empty when we are called here. But could
913 * potentially happen if the chain is messed up, check to be on the
914 * safe side.
915 */
916 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, list);
Jens Axboe2665abf2019-11-05 12:40:47 -0700917 while (nxt) {
Jens Axboe76a46e02019-11-10 23:34:16 -0700918 list_del_init(&nxt->list);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700919
920 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
921 (nxt->flags & REQ_F_TIMEOUT)) {
922 wake_ev |= io_link_cancel_timeout(nxt);
923 nxt = list_first_entry_or_null(&req->link_list,
924 struct io_kiocb, list);
925 req->flags &= ~REQ_F_LINK_TIMEOUT;
926 continue;
927 }
Jens Axboe9e645e112019-05-10 16:07:28 -0600928 if (!list_empty(&req->link_list)) {
929 INIT_LIST_HEAD(&nxt->link_list);
930 list_splice(&req->link_list, &nxt->link_list);
931 nxt->flags |= REQ_F_LINK;
932 }
933
Pavel Begunkovb18fdf72019-11-21 23:21:02 +0300934 *nxtptr = nxt;
Jens Axboe94ae5e72019-11-14 19:39:52 -0700935 break;
Jens Axboe9e645e112019-05-10 16:07:28 -0600936 }
Jens Axboe2665abf2019-11-05 12:40:47 -0700937
Jens Axboe4d7dd462019-11-20 13:03:52 -0700938 req->flags |= REQ_F_LINK_NEXT;
Jens Axboe2665abf2019-11-05 12:40:47 -0700939 if (wake_ev)
940 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -0600941}
942
943/*
944 * Called if REQ_F_LINK is set, and we fail the head request
945 */
946static void io_fail_links(struct io_kiocb *req)
947{
Jens Axboe2665abf2019-11-05 12:40:47 -0700948 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -0600949 struct io_kiocb *link;
Jens Axboe2665abf2019-11-05 12:40:47 -0700950 unsigned long flags;
951
952 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -0600953
954 while (!list_empty(&req->link_list)) {
955 link = list_first_entry(&req->link_list, struct io_kiocb, list);
Jens Axboe2665abf2019-11-05 12:40:47 -0700956 list_del_init(&link->list);
Jens Axboe9e645e112019-05-10 16:07:28 -0600957
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +0200958 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -0700959
960 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300961 link->sqe->opcode == IORING_OP_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -0700962 io_link_cancel_timeout(link);
Jens Axboe2665abf2019-11-05 12:40:47 -0700963 } else {
Jens Axboe78e19bb2019-11-06 15:21:34 -0700964 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe978db572019-11-14 22:39:04 -0700965 __io_double_put_req(link);
Jens Axboe2665abf2019-11-05 12:40:47 -0700966 }
Jens Axboe5d960722019-11-19 15:31:28 -0700967 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -0600968 }
Jens Axboe2665abf2019-11-05 12:40:47 -0700969
970 io_commit_cqring(ctx);
971 spin_unlock_irqrestore(&ctx->completion_lock, flags);
972 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -0600973}
974
Jens Axboe4d7dd462019-11-20 13:03:52 -0700975static void io_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -0600976{
Jens Axboe4d7dd462019-11-20 13:03:52 -0700977 if (likely(!(req->flags & REQ_F_LINK)))
Jens Axboe2665abf2019-11-05 12:40:47 -0700978 return;
Jens Axboe2665abf2019-11-05 12:40:47 -0700979
Jens Axboe9e645e112019-05-10 16:07:28 -0600980 /*
981 * If LINK is set, we have dependent requests in this chain. If we
982 * didn't fail this request, queue the first one up, moving any other
983 * dependencies to the next request. In case of failure, fail the rest
984 * of the chain.
985 */
Jens Axboe2665abf2019-11-05 12:40:47 -0700986 if (req->flags & REQ_F_FAIL_LINK) {
987 io_fail_links(req);
Jens Axboe7c9e7f02019-11-12 08:15:53 -0700988 } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) ==
989 REQ_F_LINK_TIMEOUT) {
Jens Axboe2665abf2019-11-05 12:40:47 -0700990 struct io_ring_ctx *ctx = req->ctx;
991 unsigned long flags;
992
993 /*
994 * If this is a timeout link, we could be racing with the
995 * timeout timer. Grab the completion lock for this case to
Jens Axboe7c9e7f02019-11-12 08:15:53 -0700996 * protect against that.
Jens Axboe2665abf2019-11-05 12:40:47 -0700997 */
998 spin_lock_irqsave(&ctx->completion_lock, flags);
999 io_req_link_next(req, nxt);
1000 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1001 } else {
1002 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -06001003 }
Jens Axboe4d7dd462019-11-20 13:03:52 -07001004}
Jens Axboe9e645e112019-05-10 16:07:28 -06001005
Jackie Liuc69f8db2019-11-09 11:00:08 +08001006static void io_free_req(struct io_kiocb *req)
1007{
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001008 struct io_kiocb *nxt = NULL;
1009
1010 io_req_find_next(req, &nxt);
Pavel Begunkov70cf9f32019-11-21 23:21:00 +03001011 __io_free_req(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001012
1013 if (nxt)
1014 io_queue_async_work(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08001015}
1016
Jens Axboeba816ad2019-09-28 11:36:45 -06001017/*
1018 * Drop reference to request, return next in chain (if there is one) if this
1019 * was the last reference to this request.
1020 */
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001021__attribute__((nonnull))
Jackie Liuec9c02a2019-11-08 23:50:36 +08001022static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboee65ef562019-03-12 10:16:44 -06001023{
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001024 io_req_find_next(req, nxtptr);
Jens Axboe4d7dd462019-11-20 13:03:52 -07001025
Jens Axboee65ef562019-03-12 10:16:44 -06001026 if (refcount_dec_and_test(&req->refs))
Jens Axboe4d7dd462019-11-20 13:03:52 -07001027 __io_free_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001028}
1029
Jens Axboe2b188cc2019-01-07 10:46:33 -07001030static void io_put_req(struct io_kiocb *req)
1031{
Jens Axboedef596e2019-01-09 08:59:42 -07001032 if (refcount_dec_and_test(&req->refs))
1033 io_free_req(req);
1034}
1035
Jens Axboe978db572019-11-14 22:39:04 -07001036/*
1037 * Must only be used if we don't need to care about links, usually from
1038 * within the completion handling itself.
1039 */
1040static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06001041{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001042 /* drop both submit and complete references */
1043 if (refcount_sub_and_test(2, &req->refs))
1044 __io_free_req(req);
1045}
1046
Jens Axboe978db572019-11-14 22:39:04 -07001047static void io_double_put_req(struct io_kiocb *req)
1048{
1049 /* drop both submit and complete references */
1050 if (refcount_sub_and_test(2, &req->refs))
1051 io_free_req(req);
1052}
1053
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001054static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06001055{
Jens Axboe84f97dc2019-11-06 11:27:53 -07001056 struct io_rings *rings = ctx->rings;
1057
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001058 /*
1059 * noflush == true is from the waitqueue handler, just ensure we wake
1060 * up the task, and the next invocation will flush the entries. We
1061 * cannot safely to it from here.
1062 */
1063 if (noflush && !list_empty(&ctx->cq_overflow_list))
1064 return -1U;
1065
1066 io_cqring_overflow_flush(ctx, false);
1067
Jens Axboea3a0e432019-08-20 11:03:11 -06001068 /* See comment at the top of this file */
1069 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00001070 return READ_ONCE(rings->cq.tail) - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06001071}
1072
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03001073static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1074{
1075 struct io_rings *rings = ctx->rings;
1076
1077 /* make sure SQ entry isn't read before tail */
1078 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1079}
1080
Jens Axboedef596e2019-01-09 08:59:42 -07001081/*
1082 * Find and free completed poll iocbs
1083 */
1084static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
1085 struct list_head *done)
1086{
1087 void *reqs[IO_IOPOLL_BATCH];
1088 struct io_kiocb *req;
Jens Axboe09bb8392019-03-13 12:39:28 -06001089 int to_free;
Jens Axboedef596e2019-01-09 08:59:42 -07001090
Jens Axboe09bb8392019-03-13 12:39:28 -06001091 to_free = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001092 while (!list_empty(done)) {
1093 req = list_first_entry(done, struct io_kiocb, list);
1094 list_del(&req->list);
1095
Jens Axboe78e19bb2019-11-06 15:21:34 -07001096 io_cqring_fill_event(req, req->result);
Jens Axboedef596e2019-01-09 08:59:42 -07001097 (*nr_events)++;
1098
Jens Axboe09bb8392019-03-13 12:39:28 -06001099 if (refcount_dec_and_test(&req->refs)) {
1100 /* If we're not using fixed files, we have to pair the
1101 * completion part with the file put. Use regular
1102 * completions for those, only batch free for fixed
Jens Axboe9e645e112019-05-10 16:07:28 -06001103 * file and non-linked commands.
Jens Axboe09bb8392019-03-13 12:39:28 -06001104 */
Jens Axboe1a6b74f2019-12-02 10:33:15 -07001105 if (((req->flags & (REQ_F_FIXED_FILE|REQ_F_LINK)) ==
1106 REQ_F_FIXED_FILE) && !io_is_fallback_req(req) &&
1107 !req->io) {
Jens Axboe09bb8392019-03-13 12:39:28 -06001108 reqs[to_free++] = req;
1109 if (to_free == ARRAY_SIZE(reqs))
1110 io_free_req_many(ctx, reqs, &to_free);
Jens Axboe6b063142019-01-10 22:13:58 -07001111 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001112 io_free_req(req);
Jens Axboe6b063142019-01-10 22:13:58 -07001113 }
Jens Axboe9a56a232019-01-09 09:06:50 -07001114 }
Jens Axboedef596e2019-01-09 08:59:42 -07001115 }
Jens Axboedef596e2019-01-09 08:59:42 -07001116
Jens Axboe09bb8392019-03-13 12:39:28 -06001117 io_commit_cqring(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07001118 io_free_req_many(ctx, reqs, &to_free);
1119}
1120
1121static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1122 long min)
1123{
1124 struct io_kiocb *req, *tmp;
1125 LIST_HEAD(done);
1126 bool spin;
1127 int ret;
1128
1129 /*
1130 * Only spin for completions if we don't have multiple devices hanging
1131 * off our complete list, and we're under the requested amount.
1132 */
1133 spin = !ctx->poll_multi_file && *nr_events < min;
1134
1135 ret = 0;
1136 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
1137 struct kiocb *kiocb = &req->rw;
1138
1139 /*
1140 * Move completed entries to our local list. If we find a
1141 * request that requires polling, break out and complete
1142 * the done list first, if we have entries there.
1143 */
1144 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
1145 list_move_tail(&req->list, &done);
1146 continue;
1147 }
1148 if (!list_empty(&done))
1149 break;
1150
1151 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1152 if (ret < 0)
1153 break;
1154
1155 if (ret && spin)
1156 spin = false;
1157 ret = 0;
1158 }
1159
1160 if (!list_empty(&done))
1161 io_iopoll_complete(ctx, nr_events, &done);
1162
1163 return ret;
1164}
1165
1166/*
1167 * Poll for a mininum of 'min' events. Note that if min == 0 we consider that a
1168 * non-spinning poll check - we'll still enter the driver poll loop, but only
1169 * as a non-spinning completion check.
1170 */
1171static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
1172 long min)
1173{
Jens Axboe08f54392019-08-21 22:19:11 -06001174 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07001175 int ret;
1176
1177 ret = io_do_iopoll(ctx, nr_events, min);
1178 if (ret < 0)
1179 return ret;
1180 if (!min || *nr_events >= min)
1181 return 0;
1182 }
1183
1184 return 1;
1185}
1186
1187/*
1188 * We can't just wait for polled events to come to us, we have to actively
1189 * find and complete them.
1190 */
1191static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
1192{
1193 if (!(ctx->flags & IORING_SETUP_IOPOLL))
1194 return;
1195
1196 mutex_lock(&ctx->uring_lock);
1197 while (!list_empty(&ctx->poll_list)) {
1198 unsigned int nr_events = 0;
1199
1200 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -06001201
1202 /*
1203 * Ensure we allow local-to-the-cpu processing to take place,
1204 * in this case we need to ensure that we reap all events.
1205 */
1206 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -07001207 }
1208 mutex_unlock(&ctx->uring_lock);
1209}
1210
Jens Axboe2b2ed972019-10-25 10:06:15 -06001211static int __io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1212 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07001213{
Jens Axboe2b2ed972019-10-25 10:06:15 -06001214 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001215
1216 do {
1217 int tmin = 0;
1218
Jens Axboe500f9fb2019-08-19 12:15:59 -06001219 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06001220 * Don't enter poll loop if we already have events pending.
1221 * If we do, we can potentially be spinning for commands that
1222 * already triggered a CQE (eg in error).
1223 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001224 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06001225 break;
1226
1227 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06001228 * If a submit got punted to a workqueue, we can have the
1229 * application entering polling for a command before it gets
1230 * issued. That app will hold the uring_lock for the duration
1231 * of the poll right here, so we need to take a breather every
1232 * now and then to ensure that the issue has a chance to add
1233 * the poll to the issued list. Otherwise we can spin here
1234 * forever, while the workqueue is stuck trying to acquire the
1235 * very same mutex.
1236 */
1237 if (!(++iters & 7)) {
1238 mutex_unlock(&ctx->uring_lock);
1239 mutex_lock(&ctx->uring_lock);
1240 }
1241
Jens Axboedef596e2019-01-09 08:59:42 -07001242 if (*nr_events < min)
1243 tmin = min - *nr_events;
1244
1245 ret = io_iopoll_getevents(ctx, nr_events, tmin);
1246 if (ret <= 0)
1247 break;
1248 ret = 0;
1249 } while (min && !*nr_events && !need_resched());
1250
Jens Axboe2b2ed972019-10-25 10:06:15 -06001251 return ret;
1252}
1253
1254static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1255 long min)
1256{
1257 int ret;
1258
1259 /*
1260 * We disallow the app entering submit/complete with polling, but we
1261 * still need to lock the ring to prevent racing with polled issue
1262 * that got punted to a workqueue.
1263 */
1264 mutex_lock(&ctx->uring_lock);
1265 ret = __io_iopoll_check(ctx, nr_events, min);
Jens Axboe500f9fb2019-08-19 12:15:59 -06001266 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001267 return ret;
1268}
1269
Jens Axboe491381ce2019-10-17 09:20:46 -06001270static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001271{
Jens Axboe491381ce2019-10-17 09:20:46 -06001272 /*
1273 * Tell lockdep we inherited freeze protection from submission
1274 * thread.
1275 */
1276 if (req->flags & REQ_F_ISREG) {
1277 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001278
Jens Axboe491381ce2019-10-17 09:20:46 -06001279 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001280 }
Jens Axboe491381ce2019-10-17 09:20:46 -06001281 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001282}
1283
Jens Axboeba816ad2019-09-28 11:36:45 -06001284static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001285{
1286 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1287
Jens Axboe491381ce2019-10-17 09:20:46 -06001288 if (kiocb->ki_flags & IOCB_WRITE)
1289 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001290
Jens Axboe9e645e112019-05-10 16:07:28 -06001291 if ((req->flags & REQ_F_LINK) && res != req->result)
1292 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001293 io_cqring_add_event(req, res);
Jens Axboeba816ad2019-09-28 11:36:45 -06001294}
1295
1296static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1297{
1298 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1299
1300 io_complete_rw_common(kiocb, res);
Jens Axboee65ef562019-03-12 10:16:44 -06001301 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001302}
1303
Jens Axboeba816ad2019-09-28 11:36:45 -06001304static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res)
1305{
1306 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001307 struct io_kiocb *nxt = NULL;
Jens Axboeba816ad2019-09-28 11:36:45 -06001308
1309 io_complete_rw_common(kiocb, res);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001310 io_put_req_find_next(req, &nxt);
1311
1312 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001313}
1314
Jens Axboedef596e2019-01-09 08:59:42 -07001315static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1316{
1317 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1318
Jens Axboe491381ce2019-10-17 09:20:46 -06001319 if (kiocb->ki_flags & IOCB_WRITE)
1320 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001321
Jens Axboe9e645e112019-05-10 16:07:28 -06001322 if ((req->flags & REQ_F_LINK) && res != req->result)
1323 req->flags |= REQ_F_FAIL_LINK;
1324 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -07001325 if (res != -EAGAIN)
1326 req->flags |= REQ_F_IOPOLL_COMPLETED;
1327}
1328
1329/*
1330 * After the iocb has been issued, it's safe to be found on the poll list.
1331 * Adding the kiocb to the list AFTER submission ensures that we don't
1332 * find it from a io_iopoll_getevents() thread before the issuer is done
1333 * accessing the kiocb cookie.
1334 */
1335static void io_iopoll_req_issued(struct io_kiocb *req)
1336{
1337 struct io_ring_ctx *ctx = req->ctx;
1338
1339 /*
1340 * Track whether we have multiple files in our lists. This will impact
1341 * how we do polling eventually, not spinning if we're on potentially
1342 * different devices.
1343 */
1344 if (list_empty(&ctx->poll_list)) {
1345 ctx->poll_multi_file = false;
1346 } else if (!ctx->poll_multi_file) {
1347 struct io_kiocb *list_req;
1348
1349 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1350 list);
1351 if (list_req->rw.ki_filp != req->rw.ki_filp)
1352 ctx->poll_multi_file = true;
1353 }
1354
1355 /*
1356 * For fast devices, IO may have already completed. If it has, add
1357 * it to the front so we find it first.
1358 */
1359 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1360 list_add(&req->list, &ctx->poll_list);
1361 else
1362 list_add_tail(&req->list, &ctx->poll_list);
1363}
1364
Jens Axboe3d6770f2019-04-13 11:50:54 -06001365static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07001366{
Jens Axboe3d6770f2019-04-13 11:50:54 -06001367 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -07001368 int diff = state->has_refs - state->used_refs;
1369
1370 if (diff)
1371 fput_many(state->file, diff);
1372 state->file = NULL;
1373 }
1374}
1375
1376/*
1377 * Get as many references to a file as we have IOs left in this submission,
1378 * assuming most submissions are for one file, or at least that each file
1379 * has more than one submission.
1380 */
1381static struct file *io_file_get(struct io_submit_state *state, int fd)
1382{
1383 if (!state)
1384 return fget(fd);
1385
1386 if (state->file) {
1387 if (state->fd == fd) {
1388 state->used_refs++;
1389 state->ios_left--;
1390 return state->file;
1391 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06001392 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07001393 }
1394 state->file = fget_many(fd, state->ios_left);
1395 if (!state->file)
1396 return NULL;
1397
1398 state->fd = fd;
1399 state->has_refs = state->ios_left;
1400 state->used_refs = 1;
1401 state->ios_left--;
1402 return state->file;
1403}
1404
Jens Axboe2b188cc2019-01-07 10:46:33 -07001405/*
1406 * If we tracked the file through the SCM inflight mechanism, we could support
1407 * any file. For now, just ensure that anything potentially problematic is done
1408 * inline.
1409 */
1410static bool io_file_supports_async(struct file *file)
1411{
1412 umode_t mode = file_inode(file)->i_mode;
1413
1414 if (S_ISBLK(mode) || S_ISCHR(mode))
1415 return true;
1416 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
1417 return true;
1418
1419 return false;
1420}
1421
Pavel Begunkov267bc902019-11-07 01:41:08 +03001422static int io_prep_rw(struct io_kiocb *req, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001423{
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001424 const struct io_uring_sqe *sqe = req->sqe;
Jens Axboedef596e2019-01-09 08:59:42 -07001425 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001426 struct kiocb *kiocb = &req->rw;
Jens Axboe09bb8392019-03-13 12:39:28 -06001427 unsigned ioprio;
1428 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001429
Jens Axboe09bb8392019-03-13 12:39:28 -06001430 if (!req->file)
1431 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001432
Jens Axboe491381ce2019-10-17 09:20:46 -06001433 if (S_ISREG(file_inode(req->file)->i_mode))
1434 req->flags |= REQ_F_ISREG;
1435
Jens Axboe2b188cc2019-01-07 10:46:33 -07001436 kiocb->ki_pos = READ_ONCE(sqe->off);
1437 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
1438 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
1439
1440 ioprio = READ_ONCE(sqe->ioprio);
1441 if (ioprio) {
1442 ret = ioprio_check_cap(ioprio);
1443 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06001444 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001445
1446 kiocb->ki_ioprio = ioprio;
1447 } else
1448 kiocb->ki_ioprio = get_current_ioprio();
1449
1450 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
1451 if (unlikely(ret))
Jens Axboe09bb8392019-03-13 12:39:28 -06001452 return ret;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001453
1454 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06001455 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
1456 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02001457 req->flags |= REQ_F_NOWAIT;
1458
1459 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001460 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001461
Jens Axboedef596e2019-01-09 08:59:42 -07001462 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07001463 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
1464 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06001465 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001466
Jens Axboedef596e2019-01-09 08:59:42 -07001467 kiocb->ki_flags |= IOCB_HIPRI;
1468 kiocb->ki_complete = io_complete_rw_iopoll;
Jens Axboe6873e0b2019-10-30 13:53:09 -06001469 req->result = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001470 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001471 if (kiocb->ki_flags & IOCB_HIPRI)
1472 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07001473 kiocb->ki_complete = io_complete_rw;
1474 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001475 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001476}
1477
1478static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
1479{
1480 switch (ret) {
1481 case -EIOCBQUEUED:
1482 break;
1483 case -ERESTARTSYS:
1484 case -ERESTARTNOINTR:
1485 case -ERESTARTNOHAND:
1486 case -ERESTART_RESTARTBLOCK:
1487 /*
1488 * We can't just restart the syscall, since previously
1489 * submitted sqes may already be in progress. Just fail this
1490 * IO with EINTR.
1491 */
1492 ret = -EINTR;
1493 /* fall through */
1494 default:
1495 kiocb->ki_complete(kiocb, ret, 0);
1496 }
1497}
1498
Jens Axboeba816ad2019-09-28 11:36:45 -06001499static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt,
1500 bool in_async)
1501{
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001502 if (in_async && ret >= 0 && kiocb->ki_complete == io_complete_rw)
Jens Axboeba816ad2019-09-28 11:36:45 -06001503 *nxt = __io_complete_rw(kiocb, ret);
1504 else
1505 io_rw_done(kiocb, ret);
1506}
1507
Pavel Begunkov7d009162019-11-25 23:14:40 +03001508static ssize_t io_import_fixed(struct io_ring_ctx *ctx, int rw,
1509 const struct io_uring_sqe *sqe,
1510 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07001511{
1512 size_t len = READ_ONCE(sqe->len);
1513 struct io_mapped_ubuf *imu;
1514 unsigned index, buf_index;
1515 size_t offset;
1516 u64 buf_addr;
1517
1518 /* attempt to use fixed buffers without having provided iovecs */
1519 if (unlikely(!ctx->user_bufs))
1520 return -EFAULT;
1521
1522 buf_index = READ_ONCE(sqe->buf_index);
1523 if (unlikely(buf_index >= ctx->nr_user_bufs))
1524 return -EFAULT;
1525
1526 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
1527 imu = &ctx->user_bufs[index];
1528 buf_addr = READ_ONCE(sqe->addr);
1529
1530 /* overflow */
1531 if (buf_addr + len < buf_addr)
1532 return -EFAULT;
1533 /* not inside the mapped region */
1534 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
1535 return -EFAULT;
1536
1537 /*
1538 * May not be a start of buffer, set size appropriately
1539 * and advance us to the beginning.
1540 */
1541 offset = buf_addr - imu->ubuf;
1542 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06001543
1544 if (offset) {
1545 /*
1546 * Don't use iov_iter_advance() here, as it's really slow for
1547 * using the latter parts of a big fixed buffer - it iterates
1548 * over each segment manually. We can cheat a bit here, because
1549 * we know that:
1550 *
1551 * 1) it's a BVEC iter, we set it up
1552 * 2) all bvecs are PAGE_SIZE in size, except potentially the
1553 * first and last bvec
1554 *
1555 * So just find our index, and adjust the iterator afterwards.
1556 * If the offset is within the first bvec (or the whole first
1557 * bvec, just use iov_iter_advance(). This makes it easier
1558 * since we can just skip the first segment, which may not
1559 * be PAGE_SIZE aligned.
1560 */
1561 const struct bio_vec *bvec = imu->bvec;
1562
1563 if (offset <= bvec->bv_len) {
1564 iov_iter_advance(iter, offset);
1565 } else {
1566 unsigned long seg_skip;
1567
1568 /* skip first vec */
1569 offset -= bvec->bv_len;
1570 seg_skip = 1 + (offset >> PAGE_SHIFT);
1571
1572 iter->bvec = bvec + seg_skip;
1573 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02001574 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001575 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001576 }
1577 }
1578
Jens Axboe5e559562019-11-13 16:12:46 -07001579 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001580}
1581
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001582static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
1583 struct iovec **iovec, struct iov_iter *iter)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001584{
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001585 const struct io_uring_sqe *sqe = req->sqe;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001586 void __user *buf = u64_to_user_ptr(READ_ONCE(sqe->addr));
1587 size_t sqe_len = READ_ONCE(sqe->len);
Jens Axboeedafcce2019-01-09 09:16:05 -07001588 u8 opcode;
1589
1590 /*
1591 * We're reading ->opcode for the second time, but the first read
1592 * doesn't care whether it's _FIXED or not, so it doesn't matter
1593 * whether ->opcode changes concurrently. The first read does care
1594 * about whether it is a READ or a WRITE, so we don't trust this read
1595 * for that purpose and instead let the caller pass in the read/write
1596 * flag.
1597 */
1598 opcode = READ_ONCE(sqe->opcode);
Pavel Begunkov7d009162019-11-25 23:14:40 +03001599 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07001600 *iovec = NULL;
Pavel Begunkov7d009162019-11-25 23:14:40 +03001601 return io_import_fixed(req->ctx, rw, sqe, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07001602 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001603
Jens Axboef67676d2019-12-02 11:03:47 -07001604 if (req->io) {
1605 struct io_async_rw *iorw = &req->io->rw;
1606
1607 *iovec = iorw->iov;
1608 iov_iter_init(iter, rw, *iovec, iorw->nr_segs, iorw->size);
1609 if (iorw->iov == iorw->fast_iov)
1610 *iovec = NULL;
1611 return iorw->size;
1612 }
1613
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001614 if (!req->has_user)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001615 return -EFAULT;
1616
1617#ifdef CONFIG_COMPAT
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001618 if (req->ctx->compat)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001619 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
1620 iovec, iter);
1621#endif
1622
1623 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
1624}
1625
Jens Axboe32960612019-09-23 11:05:34 -06001626/*
1627 * For files that don't have ->read_iter() and ->write_iter(), handle them
1628 * by looping over ->read() or ->write() manually.
1629 */
1630static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
1631 struct iov_iter *iter)
1632{
1633 ssize_t ret = 0;
1634
1635 /*
1636 * Don't support polled IO through this interface, and we can't
1637 * support non-blocking either. For the latter, this just causes
1638 * the kiocb to be handled from an async context.
1639 */
1640 if (kiocb->ki_flags & IOCB_HIPRI)
1641 return -EOPNOTSUPP;
1642 if (kiocb->ki_flags & IOCB_NOWAIT)
1643 return -EAGAIN;
1644
1645 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03001646 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06001647 ssize_t nr;
1648
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03001649 if (!iov_iter_is_bvec(iter)) {
1650 iovec = iov_iter_iovec(iter);
1651 } else {
1652 /* fixed buffers import bvec */
1653 iovec.iov_base = kmap(iter->bvec->bv_page)
1654 + iter->iov_offset;
1655 iovec.iov_len = min(iter->count,
1656 iter->bvec->bv_len - iter->iov_offset);
1657 }
1658
Jens Axboe32960612019-09-23 11:05:34 -06001659 if (rw == READ) {
1660 nr = file->f_op->read(file, iovec.iov_base,
1661 iovec.iov_len, &kiocb->ki_pos);
1662 } else {
1663 nr = file->f_op->write(file, iovec.iov_base,
1664 iovec.iov_len, &kiocb->ki_pos);
1665 }
1666
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03001667 if (iov_iter_is_bvec(iter))
1668 kunmap(iter->bvec->bv_page);
1669
Jens Axboe32960612019-09-23 11:05:34 -06001670 if (nr < 0) {
1671 if (!ret)
1672 ret = nr;
1673 break;
1674 }
1675 ret += nr;
1676 if (nr != iovec.iov_len)
1677 break;
1678 iov_iter_advance(iter, nr);
1679 }
1680
1681 return ret;
1682}
1683
Jens Axboef67676d2019-12-02 11:03:47 -07001684static void io_req_map_io(struct io_kiocb *req, ssize_t io_size,
1685 struct iovec *iovec, struct iovec *fast_iov,
1686 struct iov_iter *iter)
1687{
1688 req->io->rw.nr_segs = iter->nr_segs;
1689 req->io->rw.size = io_size;
1690 req->io->rw.iov = iovec;
1691 if (!req->io->rw.iov) {
1692 req->io->rw.iov = req->io->rw.fast_iov;
1693 memcpy(req->io->rw.iov, fast_iov,
1694 sizeof(struct iovec) * iter->nr_segs);
1695 }
1696}
1697
1698static int io_setup_async_io(struct io_kiocb *req, ssize_t io_size,
1699 struct iovec *iovec, struct iovec *fast_iov,
1700 struct iov_iter *iter)
1701{
1702 req->io = kmalloc(sizeof(*req->io), GFP_KERNEL);
1703 if (req->io) {
1704 io_req_map_io(req, io_size, iovec, fast_iov, iter);
1705 memcpy(&req->io->sqe, req->sqe, sizeof(req->io->sqe));
1706 req->sqe = &req->io->sqe;
1707 return 0;
1708 }
1709
1710 return -ENOMEM;
1711}
1712
1713static int io_read_prep(struct io_kiocb *req, struct iovec **iovec,
1714 struct iov_iter *iter, bool force_nonblock)
1715{
1716 ssize_t ret;
1717
1718 ret = io_prep_rw(req, force_nonblock);
1719 if (ret)
1720 return ret;
1721
1722 if (unlikely(!(req->file->f_mode & FMODE_READ)))
1723 return -EBADF;
1724
1725 return io_import_iovec(READ, req, iovec, iter);
1726}
1727
Pavel Begunkov267bc902019-11-07 01:41:08 +03001728static int io_read(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001729 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001730{
1731 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1732 struct kiocb *kiocb = &req->rw;
1733 struct iov_iter iter;
1734 struct file *file;
Jens Axboe31b51512019-01-18 22:56:34 -07001735 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07001736 ssize_t io_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001737
Jens Axboef67676d2019-12-02 11:03:47 -07001738 if (!req->io) {
1739 ret = io_read_prep(req, &iovec, &iter, force_nonblock);
1740 if (ret < 0)
1741 return ret;
1742 } else {
1743 ret = io_import_iovec(READ, req, &iovec, &iter);
1744 if (ret < 0)
1745 return ret;
1746 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001747
Jens Axboef67676d2019-12-02 11:03:47 -07001748 file = req->file;
1749 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06001750 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07001751 req->result = io_size;
1752
1753 /*
1754 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
1755 * we know to async punt it even if it was opened O_NONBLOCK
1756 */
1757 if (force_nonblock && !io_file_supports_async(file)) {
1758 req->flags |= REQ_F_MUST_PUNT;
1759 goto copy_iov;
1760 }
Jens Axboe9e645e112019-05-10 16:07:28 -06001761
Jens Axboe31b51512019-01-18 22:56:34 -07001762 iov_count = iov_iter_count(&iter);
1763 ret = rw_verify_area(READ, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001764 if (!ret) {
1765 ssize_t ret2;
1766
Jens Axboe32960612019-09-23 11:05:34 -06001767 if (file->f_op->read_iter)
1768 ret2 = call_read_iter(file, kiocb, &iter);
1769 else
1770 ret2 = loop_rw_iter(READ, file, kiocb, &iter);
1771
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001772 /*
1773 * In case of a short read, punt to async. This can happen
1774 * if we have data partially cached. Alternatively we can
1775 * return the short read, in which case the application will
1776 * need to issue another SQE and wait for it. That SQE will
1777 * need async punt anyway, so it's more efficient to do it
1778 * here.
1779 */
Jens Axboe491381ce2019-10-17 09:20:46 -06001780 if (force_nonblock && !(req->flags & REQ_F_NOWAIT) &&
1781 (req->flags & REQ_F_ISREG) &&
Jens Axboef67676d2019-12-02 11:03:47 -07001782 ret2 > 0 && ret2 < io_size)
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001783 ret2 = -EAGAIN;
1784 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboef67676d2019-12-02 11:03:47 -07001785 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001786 kiocb_done(kiocb, ret2, nxt, req->in_async);
Jens Axboef67676d2019-12-02 11:03:47 -07001787 } else {
1788copy_iov:
1789 ret = io_setup_async_io(req, io_size, iovec,
1790 inline_vecs, &iter);
1791 if (ret)
1792 goto out_free;
1793 return -EAGAIN;
1794 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001795 }
Jens Axboef67676d2019-12-02 11:03:47 -07001796out_free:
Jens Axboe2b188cc2019-01-07 10:46:33 -07001797 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001798 return ret;
1799}
1800
Jens Axboef67676d2019-12-02 11:03:47 -07001801static int io_write_prep(struct io_kiocb *req, struct iovec **iovec,
1802 struct iov_iter *iter, bool force_nonblock)
1803{
1804 ssize_t ret;
1805
1806 ret = io_prep_rw(req, force_nonblock);
1807 if (ret)
1808 return ret;
1809
1810 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
1811 return -EBADF;
1812
1813 return io_import_iovec(WRITE, req, iovec, iter);
1814}
1815
Pavel Begunkov267bc902019-11-07 01:41:08 +03001816static int io_write(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001817 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001818{
1819 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1820 struct kiocb *kiocb = &req->rw;
1821 struct iov_iter iter;
1822 struct file *file;
Jens Axboe31b51512019-01-18 22:56:34 -07001823 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07001824 ssize_t ret, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001825
Jens Axboef67676d2019-12-02 11:03:47 -07001826 if (!req->io) {
1827 ret = io_write_prep(req, &iovec, &iter, force_nonblock);
1828 if (ret < 0)
1829 return ret;
1830 } else {
1831 ret = io_import_iovec(WRITE, req, &iovec, &iter);
1832 if (ret < 0)
1833 return ret;
1834 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001835
Jens Axboe2b188cc2019-01-07 10:46:33 -07001836 file = kiocb->ki_filp;
Jens Axboef67676d2019-12-02 11:03:47 -07001837 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06001838 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07001839 req->result = io_size;
1840
1841 /*
1842 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
1843 * we know to async punt it even if it was opened O_NONBLOCK
1844 */
1845 if (force_nonblock && !io_file_supports_async(req->file)) {
1846 req->flags |= REQ_F_MUST_PUNT;
1847 goto copy_iov;
1848 }
1849
1850 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT))
1851 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06001852
Jens Axboe31b51512019-01-18 22:56:34 -07001853 iov_count = iov_iter_count(&iter);
Jens Axboe31b51512019-01-18 22:56:34 -07001854 ret = rw_verify_area(WRITE, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001855 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01001856 ssize_t ret2;
1857
Jens Axboe2b188cc2019-01-07 10:46:33 -07001858 /*
1859 * Open-code file_start_write here to grab freeze protection,
1860 * which will be released by another thread in
1861 * io_complete_rw(). Fool lockdep by telling it the lock got
1862 * released so that it doesn't complain about the held lock when
1863 * we return to userspace.
1864 */
Jens Axboe491381ce2019-10-17 09:20:46 -06001865 if (req->flags & REQ_F_ISREG) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001866 __sb_start_write(file_inode(file)->i_sb,
1867 SB_FREEZE_WRITE, true);
1868 __sb_writers_release(file_inode(file)->i_sb,
1869 SB_FREEZE_WRITE);
1870 }
1871 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01001872
Jens Axboe32960612019-09-23 11:05:34 -06001873 if (file->f_op->write_iter)
1874 ret2 = call_write_iter(file, kiocb, &iter);
1875 else
1876 ret2 = loop_rw_iter(WRITE, file, kiocb, &iter);
Jens Axboef67676d2019-12-02 11:03:47 -07001877 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001878 kiocb_done(kiocb, ret2, nxt, req->in_async);
Jens Axboef67676d2019-12-02 11:03:47 -07001879 } else {
1880copy_iov:
1881 ret = io_setup_async_io(req, io_size, iovec,
1882 inline_vecs, &iter);
1883 if (ret)
1884 goto out_free;
1885 return -EAGAIN;
1886 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001887 }
Jens Axboe31b51512019-01-18 22:56:34 -07001888out_free:
Jens Axboe2b188cc2019-01-07 10:46:33 -07001889 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001890 return ret;
1891}
1892
1893/*
1894 * IORING_OP_NOP just posts a completion event, nothing else.
1895 */
Jens Axboe78e19bb2019-11-06 15:21:34 -07001896static int io_nop(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001897{
1898 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001899
Jens Axboedef596e2019-01-09 08:59:42 -07001900 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1901 return -EINVAL;
1902
Jens Axboe78e19bb2019-11-06 15:21:34 -07001903 io_cqring_add_event(req, 0);
Jens Axboee65ef562019-03-12 10:16:44 -06001904 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001905 return 0;
1906}
1907
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001908static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1909{
Jens Axboe6b063142019-01-10 22:13:58 -07001910 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001911
Jens Axboe09bb8392019-03-13 12:39:28 -06001912 if (!req->file)
1913 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001914
Jens Axboe6b063142019-01-10 22:13:58 -07001915 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07001916 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07001917 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001918 return -EINVAL;
1919
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001920 return 0;
1921}
1922
1923static int io_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001924 struct io_kiocb **nxt, bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001925{
1926 loff_t sqe_off = READ_ONCE(sqe->off);
1927 loff_t sqe_len = READ_ONCE(sqe->len);
1928 loff_t end = sqe_off + sqe_len;
1929 unsigned fsync_flags;
1930 int ret;
1931
1932 fsync_flags = READ_ONCE(sqe->fsync_flags);
1933 if (unlikely(fsync_flags & ~IORING_FSYNC_DATASYNC))
1934 return -EINVAL;
1935
1936 ret = io_prep_fsync(req, sqe);
1937 if (ret)
1938 return ret;
1939
1940 /* fsync always requires a blocking context */
1941 if (force_nonblock)
1942 return -EAGAIN;
1943
1944 ret = vfs_fsync_range(req->rw.ki_filp, sqe_off,
1945 end > 0 ? end : LLONG_MAX,
1946 fsync_flags & IORING_FSYNC_DATASYNC);
1947
Jens Axboe9e645e112019-05-10 16:07:28 -06001948 if (ret < 0 && (req->flags & REQ_F_LINK))
1949 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001950 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001951 io_put_req_find_next(req, nxt);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001952 return 0;
1953}
1954
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001955static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1956{
1957 struct io_ring_ctx *ctx = req->ctx;
1958 int ret = 0;
1959
1960 if (!req->file)
1961 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001962
1963 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1964 return -EINVAL;
1965 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
1966 return -EINVAL;
1967
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001968 return ret;
1969}
1970
1971static int io_sync_file_range(struct io_kiocb *req,
1972 const struct io_uring_sqe *sqe,
Jens Axboeba816ad2019-09-28 11:36:45 -06001973 struct io_kiocb **nxt,
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001974 bool force_nonblock)
1975{
1976 loff_t sqe_off;
1977 loff_t sqe_len;
1978 unsigned flags;
1979 int ret;
1980
1981 ret = io_prep_sfr(req, sqe);
1982 if (ret)
1983 return ret;
1984
1985 /* sync_file_range always requires a blocking context */
1986 if (force_nonblock)
1987 return -EAGAIN;
1988
1989 sqe_off = READ_ONCE(sqe->off);
1990 sqe_len = READ_ONCE(sqe->len);
1991 flags = READ_ONCE(sqe->sync_range_flags);
1992
1993 ret = sync_file_range(req->rw.ki_filp, sqe_off, sqe_len, flags);
1994
Jens Axboe9e645e112019-05-10 16:07:28 -06001995 if (ret < 0 && (req->flags & REQ_F_LINK))
1996 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001997 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001998 io_put_req_find_next(req, nxt);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06001999 return 0;
2000}
2001
Jens Axboe03b12302019-12-02 18:50:25 -07002002static int io_sendmsg_prep(struct io_kiocb *req, struct io_async_ctx *io)
Jens Axboeaa1fa282019-04-19 13:38:09 -06002003{
Jens Axboe03b12302019-12-02 18:50:25 -07002004#if defined(CONFIG_NET)
2005 const struct io_uring_sqe *sqe = req->sqe;
2006 struct user_msghdr __user *msg;
2007 unsigned flags;
2008
2009 flags = READ_ONCE(sqe->msg_flags);
2010 msg = (struct user_msghdr __user *)(unsigned long) READ_ONCE(sqe->addr);
2011 return sendmsg_copy_msghdr(&io->msg.msg, msg, flags, &io->msg.iov);
2012#else
2013 return 0;
2014#endif
2015}
2016
2017static int io_sendmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2018 struct io_kiocb **nxt, bool force_nonblock)
2019{
2020#if defined(CONFIG_NET)
2021 struct socket *sock;
2022 int ret;
2023
2024 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2025 return -EINVAL;
2026
2027 sock = sock_from_file(req->file, &ret);
2028 if (sock) {
2029 struct io_async_ctx io, *copy;
2030 struct sockaddr_storage addr;
2031 struct msghdr *kmsg;
2032 unsigned flags;
2033
2034 flags = READ_ONCE(sqe->msg_flags);
2035 if (flags & MSG_DONTWAIT)
2036 req->flags |= REQ_F_NOWAIT;
2037 else if (force_nonblock)
2038 flags |= MSG_DONTWAIT;
2039
2040 if (req->io) {
2041 kmsg = &req->io->msg.msg;
2042 kmsg->msg_name = &addr;
2043 } else {
2044 kmsg = &io.msg.msg;
2045 kmsg->msg_name = &addr;
2046 io.msg.iov = io.msg.fast_iov;
2047 ret = io_sendmsg_prep(req, &io);
2048 if (ret)
2049 goto out;
2050 }
2051
2052 ret = __sys_sendmsg_sock(sock, kmsg, flags);
2053 if (force_nonblock && ret == -EAGAIN) {
2054 copy = kmalloc(sizeof(*copy), GFP_KERNEL);
2055 if (!copy) {
2056 ret = -ENOMEM;
2057 goto out;
2058 }
2059 memcpy(&copy->msg, &io.msg, sizeof(copy->msg));
2060 req->io = copy;
2061 memcpy(&req->io->sqe, req->sqe, sizeof(*req->sqe));
2062 req->sqe = &req->io->sqe;
2063 return ret;
2064 }
2065 if (ret == -ERESTARTSYS)
2066 ret = -EINTR;
2067 }
2068
2069out:
2070 io_cqring_add_event(req, ret);
2071 if (ret < 0 && (req->flags & REQ_F_LINK))
2072 req->flags |= REQ_F_FAIL_LINK;
2073 io_put_req_find_next(req, nxt);
2074 return 0;
2075#else
2076 return -EOPNOTSUPP;
2077#endif
2078}
2079
2080static int io_recvmsg_prep(struct io_kiocb *req, struct io_async_ctx *io)
2081{
2082#if defined(CONFIG_NET)
2083 const struct io_uring_sqe *sqe = req->sqe;
2084 struct user_msghdr __user *msg;
2085 unsigned flags;
2086
2087 flags = READ_ONCE(sqe->msg_flags);
2088 msg = (struct user_msghdr __user *)(unsigned long) READ_ONCE(sqe->addr);
2089 return recvmsg_copy_msghdr(&io->msg.msg, msg, flags, &io->msg.uaddr,
2090 &io->msg.iov);
2091#else
2092 return 0;
2093#endif
2094}
2095
2096static int io_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2097 struct io_kiocb **nxt, bool force_nonblock)
2098{
2099#if defined(CONFIG_NET)
Jens Axboe0fa03c62019-04-19 13:34:07 -06002100 struct socket *sock;
2101 int ret;
2102
2103 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2104 return -EINVAL;
2105
2106 sock = sock_from_file(req->file, &ret);
2107 if (sock) {
2108 struct user_msghdr __user *msg;
Jens Axboe03b12302019-12-02 18:50:25 -07002109 struct io_async_ctx io, *copy;
2110 struct sockaddr_storage addr;
2111 struct msghdr *kmsg;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002112 unsigned flags;
2113
2114 flags = READ_ONCE(sqe->msg_flags);
2115 if (flags & MSG_DONTWAIT)
2116 req->flags |= REQ_F_NOWAIT;
2117 else if (force_nonblock)
2118 flags |= MSG_DONTWAIT;
2119
2120 msg = (struct user_msghdr __user *) (unsigned long)
2121 READ_ONCE(sqe->addr);
Jens Axboe03b12302019-12-02 18:50:25 -07002122 if (req->io) {
2123 kmsg = &req->io->msg.msg;
2124 kmsg->msg_name = &addr;
2125 } else {
2126 kmsg = &io.msg.msg;
2127 kmsg->msg_name = &addr;
2128 io.msg.iov = io.msg.fast_iov;
2129 ret = io_recvmsg_prep(req, &io);
2130 if (ret)
2131 goto out;
2132 }
Jens Axboe0fa03c62019-04-19 13:34:07 -06002133
Jens Axboe03b12302019-12-02 18:50:25 -07002134 ret = __sys_recvmsg_sock(sock, kmsg, msg, io.msg.uaddr, flags);
2135 if (force_nonblock && ret == -EAGAIN) {
2136 copy = kmalloc(sizeof(*copy), GFP_KERNEL);
2137 if (!copy) {
2138 ret = -ENOMEM;
2139 goto out;
2140 }
2141 memcpy(copy, &io, sizeof(*copy));
2142 req->io = copy;
2143 memcpy(&req->io->sqe, req->sqe, sizeof(*req->sqe));
2144 req->sqe = &req->io->sqe;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002145 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07002146 }
Jens Axboe441cdbd2019-12-02 18:49:10 -07002147 if (ret == -ERESTARTSYS)
2148 ret = -EINTR;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002149 }
2150
Jens Axboe03b12302019-12-02 18:50:25 -07002151out:
Jens Axboe78e19bb2019-11-06 15:21:34 -07002152 io_cqring_add_event(req, ret);
Jens Axboef1f40852019-11-05 20:33:16 -07002153 if (ret < 0 && (req->flags & REQ_F_LINK))
2154 req->flags |= REQ_F_FAIL_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08002155 io_put_req_find_next(req, nxt);
Jens Axboe0fa03c62019-04-19 13:34:07 -06002156 return 0;
2157#else
2158 return -EOPNOTSUPP;
2159#endif
2160}
2161
Jens Axboe17f2fe32019-10-17 14:42:58 -06002162static int io_accept(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2163 struct io_kiocb **nxt, bool force_nonblock)
2164{
2165#if defined(CONFIG_NET)
2166 struct sockaddr __user *addr;
2167 int __user *addr_len;
2168 unsigned file_flags;
2169 int flags, ret;
2170
2171 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
2172 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05002173 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06002174 return -EINVAL;
2175
2176 addr = (struct sockaddr __user *) (unsigned long) READ_ONCE(sqe->addr);
2177 addr_len = (int __user *) (unsigned long) READ_ONCE(sqe->addr2);
2178 flags = READ_ONCE(sqe->accept_flags);
2179 file_flags = force_nonblock ? O_NONBLOCK : 0;
2180
2181 ret = __sys_accept4_file(req->file, file_flags, addr, addr_len, flags);
2182 if (ret == -EAGAIN && force_nonblock) {
2183 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
2184 return -EAGAIN;
2185 }
Jens Axboe8e3cca12019-11-09 19:52:33 -07002186 if (ret == -ERESTARTSYS)
2187 ret = -EINTR;
Jens Axboe17f2fe32019-10-17 14:42:58 -06002188 if (ret < 0 && (req->flags & REQ_F_LINK))
2189 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07002190 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08002191 io_put_req_find_next(req, nxt);
Jens Axboe17f2fe32019-10-17 14:42:58 -06002192 return 0;
2193#else
2194 return -EOPNOTSUPP;
2195#endif
2196}
2197
Jens Axboef8e85cf2019-11-23 14:24:24 -07002198static int io_connect(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2199 struct io_kiocb **nxt, bool force_nonblock)
2200{
2201#if defined(CONFIG_NET)
2202 struct sockaddr __user *addr;
2203 unsigned file_flags;
2204 int addr_len, ret;
2205
2206 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
2207 return -EINVAL;
2208 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
2209 return -EINVAL;
2210
2211 addr = (struct sockaddr __user *) (unsigned long) READ_ONCE(sqe->addr);
2212 addr_len = READ_ONCE(sqe->addr2);
2213 file_flags = force_nonblock ? O_NONBLOCK : 0;
2214
2215 ret = __sys_connect_file(req->file, addr, addr_len, file_flags);
2216 if (ret == -EAGAIN && force_nonblock)
2217 return -EAGAIN;
2218 if (ret == -ERESTARTSYS)
2219 ret = -EINTR;
2220 if (ret < 0 && (req->flags & REQ_F_LINK))
2221 req->flags |= REQ_F_FAIL_LINK;
2222 io_cqring_add_event(req, ret);
2223 io_put_req_find_next(req, nxt);
2224 return 0;
2225#else
2226 return -EOPNOTSUPP;
2227#endif
2228}
2229
Jens Axboeeac406c2019-11-14 12:09:58 -07002230static inline void io_poll_remove_req(struct io_kiocb *req)
2231{
2232 if (!RB_EMPTY_NODE(&req->rb_node)) {
2233 rb_erase(&req->rb_node, &req->ctx->cancel_tree);
2234 RB_CLEAR_NODE(&req->rb_node);
2235 }
2236}
2237
Jens Axboe221c5eb2019-01-17 09:41:58 -07002238static void io_poll_remove_one(struct io_kiocb *req)
2239{
2240 struct io_poll_iocb *poll = &req->poll;
2241
2242 spin_lock(&poll->head->lock);
2243 WRITE_ONCE(poll->canceled, true);
Jens Axboee9444752019-11-26 15:02:04 -07002244 if (!list_empty(&poll->wait->entry)) {
2245 list_del_init(&poll->wait->entry);
Jackie Liua197f662019-11-08 08:09:12 -07002246 io_queue_async_work(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002247 }
2248 spin_unlock(&poll->head->lock);
Jens Axboeeac406c2019-11-14 12:09:58 -07002249 io_poll_remove_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002250}
2251
2252static void io_poll_remove_all(struct io_ring_ctx *ctx)
2253{
Jens Axboeeac406c2019-11-14 12:09:58 -07002254 struct rb_node *node;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002255 struct io_kiocb *req;
2256
2257 spin_lock_irq(&ctx->completion_lock);
Jens Axboeeac406c2019-11-14 12:09:58 -07002258 while ((node = rb_first(&ctx->cancel_tree)) != NULL) {
2259 req = rb_entry(node, struct io_kiocb, rb_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002260 io_poll_remove_one(req);
2261 }
2262 spin_unlock_irq(&ctx->completion_lock);
2263}
2264
Jens Axboe47f46762019-11-09 17:43:02 -07002265static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
2266{
Jens Axboeeac406c2019-11-14 12:09:58 -07002267 struct rb_node *p, *parent = NULL;
Jens Axboe47f46762019-11-09 17:43:02 -07002268 struct io_kiocb *req;
2269
Jens Axboeeac406c2019-11-14 12:09:58 -07002270 p = ctx->cancel_tree.rb_node;
2271 while (p) {
2272 parent = p;
2273 req = rb_entry(parent, struct io_kiocb, rb_node);
2274 if (sqe_addr < req->user_data) {
2275 p = p->rb_left;
2276 } else if (sqe_addr > req->user_data) {
2277 p = p->rb_right;
2278 } else {
2279 io_poll_remove_one(req);
2280 return 0;
2281 }
Jens Axboe47f46762019-11-09 17:43:02 -07002282 }
2283
2284 return -ENOENT;
2285}
2286
Jens Axboe221c5eb2019-01-17 09:41:58 -07002287/*
2288 * Find a running poll command that matches one specified in sqe->addr,
2289 * and remove it if found.
2290 */
2291static int io_poll_remove(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2292{
2293 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07002294 int ret;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002295
2296 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2297 return -EINVAL;
2298 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
2299 sqe->poll_events)
2300 return -EINVAL;
2301
2302 spin_lock_irq(&ctx->completion_lock);
Jens Axboe47f46762019-11-09 17:43:02 -07002303 ret = io_poll_cancel(ctx, READ_ONCE(sqe->addr));
Jens Axboe221c5eb2019-01-17 09:41:58 -07002304 spin_unlock_irq(&ctx->completion_lock);
2305
Jens Axboe78e19bb2019-11-06 15:21:34 -07002306 io_cqring_add_event(req, ret);
Jens Axboef1f40852019-11-05 20:33:16 -07002307 if (ret < 0 && (req->flags & REQ_F_LINK))
2308 req->flags |= REQ_F_FAIL_LINK;
Jens Axboee65ef562019-03-12 10:16:44 -06002309 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002310 return 0;
2311}
2312
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002313static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002314{
Jackie Liua197f662019-11-08 08:09:12 -07002315 struct io_ring_ctx *ctx = req->ctx;
2316
Jens Axboe8c838782019-03-12 15:48:16 -06002317 req->poll.done = true;
Jens Axboee9444752019-11-26 15:02:04 -07002318 kfree(req->poll.wait);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002319 if (error)
2320 io_cqring_fill_event(req, error);
2321 else
2322 io_cqring_fill_event(req, mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06002323 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002324}
2325
Jens Axboe561fb042019-10-24 07:25:42 -06002326static void io_poll_complete_work(struct io_wq_work **workptr)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002327{
Jens Axboe561fb042019-10-24 07:25:42 -06002328 struct io_wq_work *work = *workptr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002329 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
2330 struct io_poll_iocb *poll = &req->poll;
2331 struct poll_table_struct pt = { ._key = poll->events };
2332 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe89723d02019-11-05 15:32:58 -07002333 struct io_kiocb *nxt = NULL;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002334 __poll_t mask = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002335 int ret = 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002336
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002337 if (work->flags & IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06002338 WRITE_ONCE(poll->canceled, true);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002339 ret = -ECANCELED;
2340 } else if (READ_ONCE(poll->canceled)) {
2341 ret = -ECANCELED;
2342 }
Jens Axboe561fb042019-10-24 07:25:42 -06002343
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002344 if (ret != -ECANCELED)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002345 mask = vfs_poll(poll->file, &pt) & poll->events;
2346
2347 /*
2348 * Note that ->ki_cancel callers also delete iocb from active_reqs after
2349 * calling ->ki_cancel. We need the ctx_lock roundtrip here to
2350 * synchronize with them. In the cancellation case the list_del_init
2351 * itself is not actually needed, but harmless so we keep it in to
2352 * avoid further branches in the fast path.
2353 */
2354 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002355 if (!mask && ret != -ECANCELED) {
Jens Axboee9444752019-11-26 15:02:04 -07002356 add_wait_queue(poll->head, poll->wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002357 spin_unlock_irq(&ctx->completion_lock);
2358 return;
2359 }
Jens Axboeeac406c2019-11-14 12:09:58 -07002360 io_poll_remove_req(req);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002361 io_poll_complete(req, mask, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002362 spin_unlock_irq(&ctx->completion_lock);
2363
Jens Axboe8c838782019-03-12 15:48:16 -06002364 io_cqring_ev_posted(ctx);
Jens Axboe89723d02019-11-05 15:32:58 -07002365
Jens Axboefba38c22019-11-18 12:27:57 -07002366 if (ret < 0 && req->flags & REQ_F_LINK)
2367 req->flags |= REQ_F_FAIL_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08002368 io_put_req_find_next(req, &nxt);
Jens Axboe89723d02019-11-05 15:32:58 -07002369 if (nxt)
2370 *workptr = &nxt->work;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002371}
2372
2373static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
2374 void *key)
2375{
Jens Axboee9444752019-11-26 15:02:04 -07002376 struct io_poll_iocb *poll = wait->private;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002377 struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
2378 struct io_ring_ctx *ctx = req->ctx;
2379 __poll_t mask = key_to_poll(key);
Jens Axboe8c838782019-03-12 15:48:16 -06002380 unsigned long flags;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002381
2382 /* for instances that support it check for an event match first: */
Jens Axboe8c838782019-03-12 15:48:16 -06002383 if (mask && !(mask & poll->events))
2384 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002385
Jens Axboee9444752019-11-26 15:02:04 -07002386 list_del_init(&poll->wait->entry);
Jens Axboe8c838782019-03-12 15:48:16 -06002387
Jens Axboe7c9e7f02019-11-12 08:15:53 -07002388 /*
2389 * Run completion inline if we can. We're using trylock here because
2390 * we are violating the completion_lock -> poll wq lock ordering.
2391 * If we have a link timeout we're going to need the completion_lock
2392 * for finalizing the request, mark us as having grabbed that already.
2393 */
Jens Axboe8c838782019-03-12 15:48:16 -06002394 if (mask && spin_trylock_irqsave(&ctx->completion_lock, flags)) {
Jens Axboeeac406c2019-11-14 12:09:58 -07002395 io_poll_remove_req(req);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002396 io_poll_complete(req, mask, 0);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07002397 req->flags |= REQ_F_COMP_LOCKED;
2398 io_put_req(req);
Jens Axboe8c838782019-03-12 15:48:16 -06002399 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2400
2401 io_cqring_ev_posted(ctx);
Jens Axboe8c838782019-03-12 15:48:16 -06002402 } else {
Jackie Liua197f662019-11-08 08:09:12 -07002403 io_queue_async_work(req);
Jens Axboe8c838782019-03-12 15:48:16 -06002404 }
2405
Jens Axboe221c5eb2019-01-17 09:41:58 -07002406 return 1;
2407}
2408
2409struct io_poll_table {
2410 struct poll_table_struct pt;
2411 struct io_kiocb *req;
2412 int error;
2413};
2414
2415static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
2416 struct poll_table_struct *p)
2417{
2418 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
2419
2420 if (unlikely(pt->req->poll.head)) {
2421 pt->error = -EINVAL;
2422 return;
2423 }
2424
2425 pt->error = 0;
2426 pt->req->poll.head = head;
Jens Axboee9444752019-11-26 15:02:04 -07002427 add_wait_queue(head, pt->req->poll.wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002428}
2429
Jens Axboeeac406c2019-11-14 12:09:58 -07002430static void io_poll_req_insert(struct io_kiocb *req)
2431{
2432 struct io_ring_ctx *ctx = req->ctx;
2433 struct rb_node **p = &ctx->cancel_tree.rb_node;
2434 struct rb_node *parent = NULL;
2435 struct io_kiocb *tmp;
2436
2437 while (*p) {
2438 parent = *p;
2439 tmp = rb_entry(parent, struct io_kiocb, rb_node);
2440 if (req->user_data < tmp->user_data)
2441 p = &(*p)->rb_left;
2442 else
2443 p = &(*p)->rb_right;
2444 }
2445 rb_link_node(&req->rb_node, parent, p);
2446 rb_insert_color(&req->rb_node, &ctx->cancel_tree);
2447}
2448
Jens Axboe89723d02019-11-05 15:32:58 -07002449static int io_poll_add(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2450 struct io_kiocb **nxt)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002451{
2452 struct io_poll_iocb *poll = &req->poll;
2453 struct io_ring_ctx *ctx = req->ctx;
2454 struct io_poll_table ipt;
Jens Axboe8c838782019-03-12 15:48:16 -06002455 bool cancel = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002456 __poll_t mask;
2457 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002458
2459 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2460 return -EINVAL;
2461 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
2462 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06002463 if (!poll->file)
2464 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002465
Jens Axboee9444752019-11-26 15:02:04 -07002466 poll->wait = kmalloc(sizeof(*poll->wait), GFP_KERNEL);
2467 if (!poll->wait)
2468 return -ENOMEM;
2469
Jens Axboe1a6b74f2019-12-02 10:33:15 -07002470 req->io = NULL;
Jens Axboe561fb042019-10-24 07:25:42 -06002471 INIT_IO_WORK(&req->work, io_poll_complete_work);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002472 events = READ_ONCE(sqe->poll_events);
2473 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
Jens Axboeeac406c2019-11-14 12:09:58 -07002474 RB_CLEAR_NODE(&req->rb_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002475
Jens Axboe221c5eb2019-01-17 09:41:58 -07002476 poll->head = NULL;
Jens Axboe8c838782019-03-12 15:48:16 -06002477 poll->done = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002478 poll->canceled = false;
2479
2480 ipt.pt._qproc = io_poll_queue_proc;
2481 ipt.pt._key = poll->events;
2482 ipt.req = req;
2483 ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
2484
2485 /* initialized the list so that we can do list_empty checks */
Jens Axboee9444752019-11-26 15:02:04 -07002486 INIT_LIST_HEAD(&poll->wait->entry);
2487 init_waitqueue_func_entry(poll->wait, io_poll_wake);
2488 poll->wait->private = poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002489
Jens Axboe36703242019-07-25 10:20:18 -06002490 INIT_LIST_HEAD(&req->list);
2491
Jens Axboe221c5eb2019-01-17 09:41:58 -07002492 mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002493
2494 spin_lock_irq(&ctx->completion_lock);
Jens Axboe8c838782019-03-12 15:48:16 -06002495 if (likely(poll->head)) {
2496 spin_lock(&poll->head->lock);
Jens Axboee9444752019-11-26 15:02:04 -07002497 if (unlikely(list_empty(&poll->wait->entry))) {
Jens Axboe8c838782019-03-12 15:48:16 -06002498 if (ipt.error)
2499 cancel = true;
2500 ipt.error = 0;
2501 mask = 0;
2502 }
2503 if (mask || ipt.error)
Jens Axboee9444752019-11-26 15:02:04 -07002504 list_del_init(&poll->wait->entry);
Jens Axboe8c838782019-03-12 15:48:16 -06002505 else if (cancel)
2506 WRITE_ONCE(poll->canceled, true);
2507 else if (!poll->done) /* actually waiting for an event */
Jens Axboeeac406c2019-11-14 12:09:58 -07002508 io_poll_req_insert(req);
Jens Axboe8c838782019-03-12 15:48:16 -06002509 spin_unlock(&poll->head->lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002510 }
Jens Axboe8c838782019-03-12 15:48:16 -06002511 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06002512 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002513 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06002514 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07002515 spin_unlock_irq(&ctx->completion_lock);
2516
Jens Axboe8c838782019-03-12 15:48:16 -06002517 if (mask) {
2518 io_cqring_ev_posted(ctx);
Jackie Liuec9c02a2019-11-08 23:50:36 +08002519 io_put_req_find_next(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002520 }
Jens Axboe8c838782019-03-12 15:48:16 -06002521 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002522}
2523
Jens Axboe5262f562019-09-17 12:26:57 -06002524static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
2525{
Jens Axboead8a48a2019-11-15 08:49:11 -07002526 struct io_timeout_data *data = container_of(timer,
2527 struct io_timeout_data, timer);
2528 struct io_kiocb *req = data->req;
2529 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06002530 unsigned long flags;
2531
Jens Axboe5262f562019-09-17 12:26:57 -06002532 atomic_inc(&ctx->cq_timeouts);
2533
2534 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08002535 /*
Jens Axboe11365042019-10-16 09:08:32 -06002536 * We could be racing with timeout deletion. If the list is empty,
2537 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08002538 */
Jens Axboe842f9612019-10-29 12:34:10 -06002539 if (!list_empty(&req->list)) {
Jens Axboe11365042019-10-16 09:08:32 -06002540 struct io_kiocb *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06002541
Jens Axboe11365042019-10-16 09:08:32 -06002542 /*
2543 * Adjust the reqs sequence before the current one because it
2544 * will consume a slot in the cq_ring and the the cq_tail
2545 * pointer will be increased, otherwise other timeout reqs may
2546 * return in advance without waiting for enough wait_nr.
2547 */
2548 prev = req;
2549 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
2550 prev->sequence++;
Jens Axboe11365042019-10-16 09:08:32 -06002551 list_del_init(&req->list);
Jens Axboe11365042019-10-16 09:08:32 -06002552 }
Jens Axboe842f9612019-10-29 12:34:10 -06002553
Jens Axboe78e19bb2019-11-06 15:21:34 -07002554 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06002555 io_commit_cqring(ctx);
2556 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2557
2558 io_cqring_ev_posted(ctx);
Jens Axboef1f40852019-11-05 20:33:16 -07002559 if (req->flags & REQ_F_LINK)
2560 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe5262f562019-09-17 12:26:57 -06002561 io_put_req(req);
2562 return HRTIMER_NORESTART;
2563}
2564
Jens Axboe47f46762019-11-09 17:43:02 -07002565static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
2566{
2567 struct io_kiocb *req;
2568 int ret = -ENOENT;
2569
2570 list_for_each_entry(req, &ctx->timeout_list, list) {
2571 if (user_data == req->user_data) {
2572 list_del_init(&req->list);
2573 ret = 0;
2574 break;
2575 }
2576 }
2577
2578 if (ret == -ENOENT)
2579 return ret;
2580
Jens Axboead8a48a2019-11-15 08:49:11 -07002581 ret = hrtimer_try_to_cancel(&req->timeout.data->timer);
Jens Axboe47f46762019-11-09 17:43:02 -07002582 if (ret == -1)
2583 return -EALREADY;
2584
Jens Axboefba38c22019-11-18 12:27:57 -07002585 if (req->flags & REQ_F_LINK)
2586 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe47f46762019-11-09 17:43:02 -07002587 io_cqring_fill_event(req, -ECANCELED);
2588 io_put_req(req);
2589 return 0;
2590}
2591
Jens Axboe11365042019-10-16 09:08:32 -06002592/*
2593 * Remove or update an existing timeout command
2594 */
2595static int io_timeout_remove(struct io_kiocb *req,
2596 const struct io_uring_sqe *sqe)
2597{
2598 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe11365042019-10-16 09:08:32 -06002599 unsigned flags;
Jens Axboe47f46762019-11-09 17:43:02 -07002600 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06002601
2602 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2603 return -EINVAL;
2604 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
2605 return -EINVAL;
2606 flags = READ_ONCE(sqe->timeout_flags);
2607 if (flags)
2608 return -EINVAL;
2609
Jens Axboe11365042019-10-16 09:08:32 -06002610 spin_lock_irq(&ctx->completion_lock);
Jens Axboe47f46762019-11-09 17:43:02 -07002611 ret = io_timeout_cancel(ctx, READ_ONCE(sqe->addr));
Jens Axboe11365042019-10-16 09:08:32 -06002612
Jens Axboe47f46762019-11-09 17:43:02 -07002613 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06002614 io_commit_cqring(ctx);
2615 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06002616 io_cqring_ev_posted(ctx);
Jens Axboe47f46762019-11-09 17:43:02 -07002617 if (ret < 0 && req->flags & REQ_F_LINK)
2618 req->flags |= REQ_F_FAIL_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08002619 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06002620 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06002621}
2622
Jens Axboead8a48a2019-11-15 08:49:11 -07002623static int io_timeout_setup(struct io_kiocb *req)
Jens Axboe5262f562019-09-17 12:26:57 -06002624{
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002625 const struct io_uring_sqe *sqe = req->sqe;
Jens Axboead8a48a2019-11-15 08:49:11 -07002626 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06002627 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06002628
Jens Axboead8a48a2019-11-15 08:49:11 -07002629 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06002630 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07002631 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06002632 return -EINVAL;
2633 flags = READ_ONCE(sqe->timeout_flags);
2634 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06002635 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06002636
Jens Axboead8a48a2019-11-15 08:49:11 -07002637 data = kzalloc(sizeof(struct io_timeout_data), GFP_KERNEL);
2638 if (!data)
2639 return -ENOMEM;
2640 data->req = req;
2641 req->timeout.data = data;
2642 req->flags |= REQ_F_TIMEOUT;
2643
2644 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06002645 return -EFAULT;
2646
Jens Axboe11365042019-10-16 09:08:32 -06002647 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07002648 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06002649 else
Jens Axboead8a48a2019-11-15 08:49:11 -07002650 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06002651
Jens Axboead8a48a2019-11-15 08:49:11 -07002652 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
2653 return 0;
2654}
2655
2656static int io_timeout(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2657{
2658 unsigned count;
2659 struct io_ring_ctx *ctx = req->ctx;
2660 struct io_timeout_data *data;
2661 struct list_head *entry;
2662 unsigned span = 0;
2663 int ret;
2664
2665 ret = io_timeout_setup(req);
2666 /* common setup allows flags (like links) set, we don't */
2667 if (!ret && sqe->flags)
2668 ret = -EINVAL;
2669 if (ret)
2670 return ret;
Jens Axboe93bd25b2019-11-11 23:34:31 -07002671
Jens Axboe5262f562019-09-17 12:26:57 -06002672 /*
2673 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07002674 * timeout event to be satisfied. If it isn't set, then this is
2675 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06002676 */
2677 count = READ_ONCE(sqe->off);
Jens Axboe93bd25b2019-11-11 23:34:31 -07002678 if (!count) {
2679 req->flags |= REQ_F_TIMEOUT_NOSEQ;
2680 spin_lock_irq(&ctx->completion_lock);
2681 entry = ctx->timeout_list.prev;
2682 goto add;
2683 }
Jens Axboe5262f562019-09-17 12:26:57 -06002684
2685 req->sequence = ctx->cached_sq_head + count - 1;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +03002686 req->timeout.data->seq_offset = count;
Jens Axboe5262f562019-09-17 12:26:57 -06002687
2688 /*
2689 * Insertion sort, ensuring the first entry in the list is always
2690 * the one we need first.
2691 */
Jens Axboe5262f562019-09-17 12:26:57 -06002692 spin_lock_irq(&ctx->completion_lock);
2693 list_for_each_prev(entry, &ctx->timeout_list) {
2694 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
yangerkun5da0fb12019-10-15 21:59:29 +08002695 unsigned nxt_sq_head;
2696 long long tmp, tmp_nxt;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +03002697 u32 nxt_offset = nxt->timeout.data->seq_offset;
Jens Axboe5262f562019-09-17 12:26:57 -06002698
Jens Axboe93bd25b2019-11-11 23:34:31 -07002699 if (nxt->flags & REQ_F_TIMEOUT_NOSEQ)
2700 continue;
2701
yangerkun5da0fb12019-10-15 21:59:29 +08002702 /*
2703 * Since cached_sq_head + count - 1 can overflow, use type long
2704 * long to store it.
2705 */
2706 tmp = (long long)ctx->cached_sq_head + count - 1;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +03002707 nxt_sq_head = nxt->sequence - nxt_offset + 1;
2708 tmp_nxt = (long long)nxt_sq_head + nxt_offset - 1;
yangerkun5da0fb12019-10-15 21:59:29 +08002709
2710 /*
2711 * cached_sq_head may overflow, and it will never overflow twice
2712 * once there is some timeout req still be valid.
2713 */
2714 if (ctx->cached_sq_head < nxt_sq_head)
yangerkun8b07a652019-10-17 12:12:35 +08002715 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08002716
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002717 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06002718 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002719
2720 /*
2721 * Sequence of reqs after the insert one and itself should
2722 * be adjusted because each timeout req consumes a slot.
2723 */
2724 span++;
2725 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06002726 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002727 req->sequence -= span;
Jens Axboe93bd25b2019-11-11 23:34:31 -07002728add:
Jens Axboe5262f562019-09-17 12:26:57 -06002729 list_add(&req->list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07002730 data = req->timeout.data;
2731 data->timer.function = io_timeout_fn;
2732 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06002733 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06002734 return 0;
2735}
2736
Jens Axboe62755e32019-10-28 21:49:21 -06002737static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06002738{
Jens Axboe62755e32019-10-28 21:49:21 -06002739 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06002740
Jens Axboe62755e32019-10-28 21:49:21 -06002741 return req->user_data == (unsigned long) data;
2742}
2743
Jens Axboee977d6d2019-11-05 12:39:45 -07002744static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06002745{
Jens Axboe62755e32019-10-28 21:49:21 -06002746 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06002747 int ret = 0;
2748
Jens Axboe62755e32019-10-28 21:49:21 -06002749 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
2750 switch (cancel_ret) {
2751 case IO_WQ_CANCEL_OK:
2752 ret = 0;
2753 break;
2754 case IO_WQ_CANCEL_RUNNING:
2755 ret = -EALREADY;
2756 break;
2757 case IO_WQ_CANCEL_NOTFOUND:
2758 ret = -ENOENT;
2759 break;
2760 }
2761
Jens Axboee977d6d2019-11-05 12:39:45 -07002762 return ret;
2763}
2764
Jens Axboe47f46762019-11-09 17:43:02 -07002765static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
2766 struct io_kiocb *req, __u64 sqe_addr,
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002767 struct io_kiocb **nxt, int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07002768{
2769 unsigned long flags;
2770 int ret;
2771
2772 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
2773 if (ret != -ENOENT) {
2774 spin_lock_irqsave(&ctx->completion_lock, flags);
2775 goto done;
2776 }
2777
2778 spin_lock_irqsave(&ctx->completion_lock, flags);
2779 ret = io_timeout_cancel(ctx, sqe_addr);
2780 if (ret != -ENOENT)
2781 goto done;
2782 ret = io_poll_cancel(ctx, sqe_addr);
2783done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002784 if (!ret)
2785 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07002786 io_cqring_fill_event(req, ret);
2787 io_commit_cqring(ctx);
2788 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2789 io_cqring_ev_posted(ctx);
2790
2791 if (ret < 0 && (req->flags & REQ_F_LINK))
2792 req->flags |= REQ_F_FAIL_LINK;
2793 io_put_req_find_next(req, nxt);
2794}
2795
Jens Axboee977d6d2019-11-05 12:39:45 -07002796static int io_async_cancel(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2797 struct io_kiocb **nxt)
2798{
2799 struct io_ring_ctx *ctx = req->ctx;
Jens Axboee977d6d2019-11-05 12:39:45 -07002800
2801 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2802 return -EINVAL;
2803 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
2804 sqe->cancel_flags)
2805 return -EINVAL;
2806
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002807 io_async_find_and_cancel(ctx, req, READ_ONCE(sqe->addr), nxt, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06002808 return 0;
2809}
2810
Jens Axboef67676d2019-12-02 11:03:47 -07002811static int io_req_defer_prep(struct io_kiocb *req, struct io_async_ctx *io)
2812{
2813 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
2814 struct iov_iter iter;
2815 ssize_t ret;
2816
2817 memcpy(&io->sqe, req->sqe, sizeof(io->sqe));
2818 req->sqe = &io->sqe;
2819
2820 switch (io->sqe.opcode) {
2821 case IORING_OP_READV:
2822 case IORING_OP_READ_FIXED:
2823 ret = io_read_prep(req, &iovec, &iter, true);
2824 break;
2825 case IORING_OP_WRITEV:
2826 case IORING_OP_WRITE_FIXED:
2827 ret = io_write_prep(req, &iovec, &iter, true);
2828 break;
Jens Axboe03b12302019-12-02 18:50:25 -07002829 case IORING_OP_SENDMSG:
2830 ret = io_sendmsg_prep(req, io);
2831 break;
2832 case IORING_OP_RECVMSG:
2833 ret = io_recvmsg_prep(req, io);
2834 break;
Jens Axboef67676d2019-12-02 11:03:47 -07002835 default:
2836 req->io = io;
2837 return 0;
2838 }
2839
2840 if (ret < 0)
2841 return ret;
2842
2843 req->io = io;
2844 io_req_map_io(req, ret, iovec, inline_vecs, &iter);
2845 return 0;
2846}
2847
Jackie Liua197f662019-11-08 08:09:12 -07002848static int io_req_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -06002849{
Jackie Liua197f662019-11-08 08:09:12 -07002850 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe1a6b74f2019-12-02 10:33:15 -07002851 struct io_async_ctx *io;
Jens Axboef67676d2019-12-02 11:03:47 -07002852 int ret;
Jens Axboede0617e2019-04-06 21:51:27 -06002853
Bob Liu9d858b22019-11-13 18:06:25 +08002854 /* Still need defer if there is pending req in defer list. */
2855 if (!req_need_defer(req) && list_empty(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06002856 return 0;
2857
Jens Axboe1a6b74f2019-12-02 10:33:15 -07002858 io = kmalloc(sizeof(*io), GFP_KERNEL);
2859 if (!io)
Jens Axboede0617e2019-04-06 21:51:27 -06002860 return -EAGAIN;
2861
2862 spin_lock_irq(&ctx->completion_lock);
Bob Liu9d858b22019-11-13 18:06:25 +08002863 if (!req_need_defer(req) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06002864 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe1a6b74f2019-12-02 10:33:15 -07002865 kfree(io);
Jens Axboede0617e2019-04-06 21:51:27 -06002866 return 0;
2867 }
2868
Jens Axboef67676d2019-12-02 11:03:47 -07002869 ret = io_req_defer_prep(req, io);
2870 if (ret < 0)
2871 return ret;
Jens Axboede0617e2019-04-06 21:51:27 -06002872
Jens Axboe915967f2019-11-21 09:01:20 -07002873 trace_io_uring_defer(ctx, req, req->user_data);
Jens Axboede0617e2019-04-06 21:51:27 -06002874 list_add_tail(&req->list, &ctx->defer_list);
2875 spin_unlock_irq(&ctx->completion_lock);
2876 return -EIOCBQUEUED;
2877}
2878
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03002879__attribute__((nonnull))
Pavel Begunkovd7324472019-11-21 21:24:36 +03002880static int io_issue_sqe(struct io_kiocb *req, struct io_kiocb **nxt,
2881 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002882{
Jens Axboee0c5c572019-03-12 10:18:47 -06002883 int ret, opcode;
Jackie Liua197f662019-11-08 08:09:12 -07002884 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002885
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002886 opcode = READ_ONCE(req->sqe->opcode);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002887 switch (opcode) {
2888 case IORING_OP_NOP:
Jens Axboe78e19bb2019-11-06 15:21:34 -07002889 ret = io_nop(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002890 break;
2891 case IORING_OP_READV:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002892 if (unlikely(req->sqe->buf_index))
Jens Axboeedafcce2019-01-09 09:16:05 -07002893 return -EINVAL;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002894 ret = io_read(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002895 break;
2896 case IORING_OP_WRITEV:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002897 if (unlikely(req->sqe->buf_index))
Jens Axboeedafcce2019-01-09 09:16:05 -07002898 return -EINVAL;
Pavel Begunkov267bc902019-11-07 01:41:08 +03002899 ret = io_write(req, nxt, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07002900 break;
2901 case IORING_OP_READ_FIXED:
Pavel Begunkov267bc902019-11-07 01:41:08 +03002902 ret = io_read(req, nxt, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07002903 break;
2904 case IORING_OP_WRITE_FIXED:
Pavel Begunkov267bc902019-11-07 01:41:08 +03002905 ret = io_write(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002906 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002907 case IORING_OP_FSYNC:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002908 ret = io_fsync(req, req->sqe, nxt, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002909 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002910 case IORING_OP_POLL_ADD:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002911 ret = io_poll_add(req, req->sqe, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002912 break;
2913 case IORING_OP_POLL_REMOVE:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002914 ret = io_poll_remove(req, req->sqe);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002915 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002916 case IORING_OP_SYNC_FILE_RANGE:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002917 ret = io_sync_file_range(req, req->sqe, nxt, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002918 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002919 case IORING_OP_SENDMSG:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002920 ret = io_sendmsg(req, req->sqe, nxt, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06002921 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06002922 case IORING_OP_RECVMSG:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002923 ret = io_recvmsg(req, req->sqe, nxt, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06002924 break;
Jens Axboe5262f562019-09-17 12:26:57 -06002925 case IORING_OP_TIMEOUT:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002926 ret = io_timeout(req, req->sqe);
Jens Axboe5262f562019-09-17 12:26:57 -06002927 break;
Jens Axboe11365042019-10-16 09:08:32 -06002928 case IORING_OP_TIMEOUT_REMOVE:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002929 ret = io_timeout_remove(req, req->sqe);
Jens Axboe11365042019-10-16 09:08:32 -06002930 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06002931 case IORING_OP_ACCEPT:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002932 ret = io_accept(req, req->sqe, nxt, force_nonblock);
Jens Axboe17f2fe32019-10-17 14:42:58 -06002933 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07002934 case IORING_OP_CONNECT:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002935 ret = io_connect(req, req->sqe, nxt, force_nonblock);
Jens Axboef8e85cf2019-11-23 14:24:24 -07002936 break;
Jens Axboe62755e32019-10-28 21:49:21 -06002937 case IORING_OP_ASYNC_CANCEL:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002938 ret = io_async_cancel(req, req->sqe, nxt);
Jens Axboe62755e32019-10-28 21:49:21 -06002939 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002940 default:
2941 ret = -EINVAL;
2942 break;
2943 }
2944
Jens Axboedef596e2019-01-09 08:59:42 -07002945 if (ret)
2946 return ret;
2947
2948 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe9e645e112019-05-10 16:07:28 -06002949 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07002950 return -EAGAIN;
2951
2952 /* workqueue context doesn't hold uring_lock, grab it now */
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002953 if (req->in_async)
Jens Axboedef596e2019-01-09 08:59:42 -07002954 mutex_lock(&ctx->uring_lock);
2955 io_iopoll_req_issued(req);
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002956 if (req->in_async)
Jens Axboedef596e2019-01-09 08:59:42 -07002957 mutex_unlock(&ctx->uring_lock);
2958 }
2959
2960 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002961}
2962
Jens Axboeb76da702019-11-20 13:05:32 -07002963static void io_link_work_cb(struct io_wq_work **workptr)
2964{
2965 struct io_wq_work *work = *workptr;
2966 struct io_kiocb *link = work->data;
2967
2968 io_queue_linked_timeout(link);
2969 work->func = io_wq_submit_work;
2970}
2971
Jens Axboe561fb042019-10-24 07:25:42 -06002972static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboe31b51512019-01-18 22:56:34 -07002973{
Jens Axboe561fb042019-10-24 07:25:42 -06002974 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002975 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe561fb042019-10-24 07:25:42 -06002976 struct io_kiocb *nxt = NULL;
2977 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002978
Jens Axboe561fb042019-10-24 07:25:42 -06002979 /* Ensure we clear previously set non-block flag */
2980 req->rw.ki_flags &= ~IOCB_NOWAIT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002981
Jens Axboe561fb042019-10-24 07:25:42 -06002982 if (work->flags & IO_WQ_WORK_CANCEL)
2983 ret = -ECANCELED;
Jens Axboe31b51512019-01-18 22:56:34 -07002984
Jens Axboe561fb042019-10-24 07:25:42 -06002985 if (!ret) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002986 req->has_user = (work->flags & IO_WQ_WORK_HAS_MM) != 0;
2987 req->in_async = true;
Jens Axboe561fb042019-10-24 07:25:42 -06002988 do {
Pavel Begunkovd7324472019-11-21 21:24:36 +03002989 ret = io_issue_sqe(req, &nxt, false);
Jens Axboe561fb042019-10-24 07:25:42 -06002990 /*
2991 * We can get EAGAIN for polled IO even though we're
2992 * forcing a sync submission from here, since we can't
2993 * wait for request slots on the block side.
2994 */
2995 if (ret != -EAGAIN)
2996 break;
2997 cond_resched();
2998 } while (1);
2999 }
Jens Axboe31b51512019-01-18 22:56:34 -07003000
Jens Axboe561fb042019-10-24 07:25:42 -06003001 /* drop submission reference */
Jackie Liuec9c02a2019-11-08 23:50:36 +08003002 io_put_req(req);
Jens Axboe817869d2019-04-30 14:44:05 -06003003
Jens Axboe561fb042019-10-24 07:25:42 -06003004 if (ret) {
Jens Axboef1f40852019-11-05 20:33:16 -07003005 if (req->flags & REQ_F_LINK)
3006 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07003007 io_cqring_add_event(req, ret);
Jens Axboe817869d2019-04-30 14:44:05 -06003008 io_put_req(req);
Jens Axboeedafcce2019-01-09 09:16:05 -07003009 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003010
Jens Axboe561fb042019-10-24 07:25:42 -06003011 /* if a dependent link is ready, pass it back */
3012 if (!ret && nxt) {
Jens Axboe94ae5e72019-11-14 19:39:52 -07003013 struct io_kiocb *link;
3014
3015 io_prep_async_work(nxt, &link);
Jens Axboe561fb042019-10-24 07:25:42 -06003016 *workptr = &nxt->work;
Jens Axboeb76da702019-11-20 13:05:32 -07003017 if (link) {
3018 nxt->work.flags |= IO_WQ_WORK_CB;
3019 nxt->work.func = io_link_work_cb;
3020 nxt->work.data = link;
3021 }
Jens Axboeedafcce2019-01-09 09:16:05 -07003022 }
Jens Axboe31b51512019-01-18 22:56:34 -07003023}
Jens Axboe2b188cc2019-01-07 10:46:33 -07003024
Jens Axboe09bb8392019-03-13 12:39:28 -06003025static bool io_op_needs_file(const struct io_uring_sqe *sqe)
3026{
3027 int op = READ_ONCE(sqe->opcode);
3028
3029 switch (op) {
3030 case IORING_OP_NOP:
3031 case IORING_OP_POLL_REMOVE:
Pavel Begunkov5683e542019-11-14 00:59:19 +03003032 case IORING_OP_TIMEOUT:
Pavel Begunkova320e9f2019-11-14 00:11:01 +03003033 case IORING_OP_TIMEOUT_REMOVE:
3034 case IORING_OP_ASYNC_CANCEL:
3035 case IORING_OP_LINK_TIMEOUT:
Jens Axboe09bb8392019-03-13 12:39:28 -06003036 return false;
3037 default:
3038 return true;
3039 }
3040}
3041
Jens Axboe65e19f52019-10-26 07:20:21 -06003042static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
3043 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06003044{
Jens Axboe65e19f52019-10-26 07:20:21 -06003045 struct fixed_file_table *table;
3046
3047 table = &ctx->file_table[index >> IORING_FILE_TABLE_SHIFT];
3048 return table->files[index & IORING_FILE_TABLE_MASK];
3049}
3050
Jackie Liua197f662019-11-08 08:09:12 -07003051static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req)
Jens Axboe09bb8392019-03-13 12:39:28 -06003052{
Jackie Liua197f662019-11-08 08:09:12 -07003053 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe09bb8392019-03-13 12:39:28 -06003054 unsigned flags;
3055 int fd;
3056
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003057 flags = READ_ONCE(req->sqe->flags);
3058 fd = READ_ONCE(req->sqe->fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06003059
Jackie Liu4fe2c962019-09-09 20:50:40 +08003060 if (flags & IOSQE_IO_DRAIN)
Jens Axboede0617e2019-04-06 21:51:27 -06003061 req->flags |= REQ_F_IO_DRAIN;
Jens Axboede0617e2019-04-06 21:51:27 -06003062
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003063 if (!io_op_needs_file(req->sqe))
Jens Axboe09bb8392019-03-13 12:39:28 -06003064 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06003065
3066 if (flags & IOSQE_FIXED_FILE) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003067 if (unlikely(!ctx->file_table ||
Jens Axboe09bb8392019-03-13 12:39:28 -06003068 (unsigned) fd >= ctx->nr_user_files))
3069 return -EBADF;
Jens Axboeb7620122019-10-26 07:22:55 -06003070 fd = array_index_nospec(fd, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06003071 req->file = io_file_from_index(ctx, fd);
3072 if (!req->file)
Jens Axboe08a45172019-10-03 08:11:03 -06003073 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06003074 req->flags |= REQ_F_FIXED_FILE;
3075 } else {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003076 if (req->needs_fixed_file)
Jens Axboe09bb8392019-03-13 12:39:28 -06003077 return -EBADF;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02003078 trace_io_uring_file_get(ctx, fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06003079 req->file = io_file_get(state, fd);
3080 if (unlikely(!req->file))
3081 return -EBADF;
3082 }
3083
3084 return 0;
3085}
3086
Jackie Liua197f662019-11-08 08:09:12 -07003087static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003088{
Jens Axboefcb323c2019-10-24 12:39:47 -06003089 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07003090 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06003091
3092 rcu_read_lock();
3093 spin_lock_irq(&ctx->inflight_lock);
3094 /*
3095 * We use the f_ops->flush() handler to ensure that we can flush
3096 * out work accessing these files if the fd is closed. Check if
3097 * the fd has changed since we started down this path, and disallow
3098 * this operation if it has.
3099 */
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003100 if (fcheck(req->ring_fd) == req->ring_file) {
Jens Axboefcb323c2019-10-24 12:39:47 -06003101 list_add(&req->inflight_entry, &ctx->inflight_list);
3102 req->flags |= REQ_F_INFLIGHT;
3103 req->work.files = current->files;
3104 ret = 0;
3105 }
3106 spin_unlock_irq(&ctx->inflight_lock);
3107 rcu_read_unlock();
3108
3109 return ret;
3110}
3111
Jens Axboe2665abf2019-11-05 12:40:47 -07003112static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
3113{
Jens Axboead8a48a2019-11-15 08:49:11 -07003114 struct io_timeout_data *data = container_of(timer,
3115 struct io_timeout_data, timer);
3116 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07003117 struct io_ring_ctx *ctx = req->ctx;
3118 struct io_kiocb *prev = NULL;
3119 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07003120
3121 spin_lock_irqsave(&ctx->completion_lock, flags);
3122
3123 /*
3124 * We don't expect the list to be empty, that will only happen if we
3125 * race with the completion of the linked work.
3126 */
3127 if (!list_empty(&req->list)) {
3128 prev = list_entry(req->list.prev, struct io_kiocb, link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07003129 if (refcount_inc_not_zero(&prev->refs)) {
Jens Axboe76a46e02019-11-10 23:34:16 -07003130 list_del_init(&req->list);
Jens Axboe5d960722019-11-19 15:31:28 -07003131 prev->flags &= ~REQ_F_LINK_TIMEOUT;
3132 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07003133 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07003134 }
3135
3136 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3137
3138 if (prev) {
Jens Axboefba38c22019-11-18 12:27:57 -07003139 if (prev->flags & REQ_F_LINK)
3140 prev->flags |= REQ_F_FAIL_LINK;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003141 io_async_find_and_cancel(ctx, req, prev->user_data, NULL,
3142 -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07003143 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07003144 } else {
3145 io_cqring_add_event(req, -ETIME);
3146 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07003147 }
Jens Axboe2665abf2019-11-05 12:40:47 -07003148 return HRTIMER_NORESTART;
3149}
3150
Jens Axboead8a48a2019-11-15 08:49:11 -07003151static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07003152{
Jens Axboe76a46e02019-11-10 23:34:16 -07003153 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07003154
Jens Axboe76a46e02019-11-10 23:34:16 -07003155 /*
3156 * If the list is now empty, then our linked request finished before
3157 * we got a chance to setup the timer
3158 */
3159 spin_lock_irq(&ctx->completion_lock);
3160 if (!list_empty(&req->list)) {
Jens Axboe94ae5e72019-11-14 19:39:52 -07003161 struct io_timeout_data *data = req->timeout.data;
3162
Jens Axboead8a48a2019-11-15 08:49:11 -07003163 data->timer.function = io_link_timeout_fn;
3164 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
3165 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07003166 }
Jens Axboe76a46e02019-11-10 23:34:16 -07003167 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07003168
Jens Axboe2665abf2019-11-05 12:40:47 -07003169 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07003170 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07003171}
3172
Jens Axboead8a48a2019-11-15 08:49:11 -07003173static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07003174{
3175 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003176
Jens Axboe2665abf2019-11-05 12:40:47 -07003177 if (!(req->flags & REQ_F_LINK))
3178 return NULL;
3179
3180 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, list);
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003181 if (!nxt || nxt->sqe->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07003182 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07003183
Jens Axboe76a46e02019-11-10 23:34:16 -07003184 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07003185 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07003186}
3187
Jens Axboe0e0702d2019-11-14 21:42:10 -07003188static void __io_queue_sqe(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003189{
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03003190 struct io_kiocb *linked_timeout = io_prep_linked_timeout(req);
3191 struct io_kiocb *nxt = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003192 int ret;
3193
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03003194 ret = io_issue_sqe(req, &nxt, true);
3195 if (nxt)
3196 io_queue_async_work(nxt);
Jens Axboe491381ce2019-10-17 09:20:46 -06003197
3198 /*
3199 * We async punt it if the file wasn't marked NOWAIT, or if the file
3200 * doesn't support non-blocking read/write attempts
3201 */
3202 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
3203 (req->flags & REQ_F_MUST_PUNT))) {
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03003204 if (req->work.flags & IO_WQ_WORK_NEEDS_FILES) {
3205 ret = io_grab_files(req);
3206 if (ret)
3207 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003208 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03003209
3210 /*
3211 * Queued up for async execution, worker will release
3212 * submit reference when the iocb is actually submitted.
3213 */
3214 io_queue_async_work(req);
3215 return;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003216 }
Jens Axboee65ef562019-03-12 10:16:44 -06003217
Jens Axboefcb323c2019-10-24 12:39:47 -06003218err:
Jens Axboee65ef562019-03-12 10:16:44 -06003219 /* drop submission reference */
3220 io_put_req(req);
3221
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03003222 if (linked_timeout) {
Jens Axboe76a46e02019-11-10 23:34:16 -07003223 if (!ret)
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03003224 io_queue_linked_timeout(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07003225 else
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03003226 io_put_req(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07003227 }
3228
Jens Axboee65ef562019-03-12 10:16:44 -06003229 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06003230 if (ret) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07003231 io_cqring_add_event(req, ret);
Jens Axboe9e645e112019-05-10 16:07:28 -06003232 if (req->flags & REQ_F_LINK)
3233 req->flags |= REQ_F_FAIL_LINK;
Jens Axboee65ef562019-03-12 10:16:44 -06003234 io_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06003235 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003236}
3237
Jens Axboe0e0702d2019-11-14 21:42:10 -07003238static void io_queue_sqe(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08003239{
3240 int ret;
3241
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003242 if (unlikely(req->ctx->drain_next)) {
3243 req->flags |= REQ_F_IO_DRAIN;
3244 req->ctx->drain_next = false;
3245 }
3246 req->ctx->drain_next = (req->flags & REQ_F_DRAIN_LINK);
3247
Jackie Liua197f662019-11-08 08:09:12 -07003248 ret = io_req_defer(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08003249 if (ret) {
3250 if (ret != -EIOCBQUEUED) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07003251 io_cqring_add_event(req, ret);
Pavel Begunkovd3b357962019-11-19 23:32:48 +03003252 if (req->flags & REQ_F_LINK)
3253 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe78e19bb2019-11-06 15:21:34 -07003254 io_double_put_req(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08003255 }
Jens Axboe0e0702d2019-11-14 21:42:10 -07003256 } else
3257 __io_queue_sqe(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08003258}
3259
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003260static inline void io_queue_link_head(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08003261{
Jens Axboe94ae5e72019-11-14 19:39:52 -07003262 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003263 io_cqring_add_event(req, -ECANCELED);
3264 io_double_put_req(req);
3265 } else
Jens Axboe0e0702d2019-11-14 21:42:10 -07003266 io_queue_sqe(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08003267}
3268
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003269
Jens Axboe9e645e112019-05-10 16:07:28 -06003270#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK)
3271
Jackie Liua197f662019-11-08 08:09:12 -07003272static void io_submit_sqe(struct io_kiocb *req, struct io_submit_state *state,
3273 struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06003274{
Jackie Liua197f662019-11-08 08:09:12 -07003275 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -06003276 int ret;
3277
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003278 req->user_data = req->sqe->user_data;
Jens Axboe78e19bb2019-11-06 15:21:34 -07003279
Jens Axboe9e645e112019-05-10 16:07:28 -06003280 /* enforce forwards compatibility on users */
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003281 if (unlikely(req->sqe->flags & ~SQE_VALID_FLAGS)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06003282 ret = -EINVAL;
Pavel Begunkov196be952019-11-07 01:41:06 +03003283 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06003284 }
3285
Jackie Liua197f662019-11-08 08:09:12 -07003286 ret = io_req_set_file(state, req);
Jens Axboe9e645e112019-05-10 16:07:28 -06003287 if (unlikely(ret)) {
3288err_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07003289 io_cqring_add_event(req, ret);
3290 io_double_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06003291 return;
3292 }
3293
Jens Axboe9e645e112019-05-10 16:07:28 -06003294 /*
3295 * If we already have a head request, queue this one for async
3296 * submittal once the head completes. If we don't have a head but
3297 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
3298 * submitted sync once the chain is complete. If none of those
3299 * conditions are true (normal request), then just queue it.
3300 */
3301 if (*link) {
3302 struct io_kiocb *prev = *link;
Jens Axboe1a6b74f2019-12-02 10:33:15 -07003303 struct io_async_ctx *io;
Jens Axboe9e645e112019-05-10 16:07:28 -06003304
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003305 if (req->sqe->flags & IOSQE_IO_DRAIN)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003306 (*link)->flags |= REQ_F_DRAIN_LINK | REQ_F_IO_DRAIN;
3307
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003308 if (READ_ONCE(req->sqe->opcode) == IORING_OP_LINK_TIMEOUT) {
Jens Axboe94ae5e72019-11-14 19:39:52 -07003309 ret = io_timeout_setup(req);
3310 /* common setup allows offset being set, we don't */
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003311 if (!ret && req->sqe->off)
Jens Axboe94ae5e72019-11-14 19:39:52 -07003312 ret = -EINVAL;
3313 if (ret) {
3314 prev->flags |= REQ_F_FAIL_LINK;
3315 goto err_req;
3316 }
3317 }
3318
Jens Axboe1a6b74f2019-12-02 10:33:15 -07003319 io = kmalloc(sizeof(*io), GFP_KERNEL);
3320 if (!io) {
Jens Axboe9e645e112019-05-10 16:07:28 -06003321 ret = -EAGAIN;
3322 goto err_req;
3323 }
3324
Jens Axboef67676d2019-12-02 11:03:47 -07003325 ret = io_req_defer_prep(req, io);
3326 if (ret)
3327 goto err_req;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02003328 trace_io_uring_link(ctx, req, prev);
Jens Axboe9e645e112019-05-10 16:07:28 -06003329 list_add_tail(&req->list, &prev->link_list);
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003330 } else if (req->sqe->flags & IOSQE_IO_LINK) {
Jens Axboe9e645e112019-05-10 16:07:28 -06003331 req->flags |= REQ_F_LINK;
3332
Jens Axboe9e645e112019-05-10 16:07:28 -06003333 INIT_LIST_HEAD(&req->link_list);
3334 *link = req;
3335 } else {
Jackie Liua197f662019-11-08 08:09:12 -07003336 io_queue_sqe(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06003337 }
3338}
3339
Jens Axboe9a56a232019-01-09 09:06:50 -07003340/*
3341 * Batched submission is done, ensure local IO is flushed out.
3342 */
3343static void io_submit_state_end(struct io_submit_state *state)
3344{
3345 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06003346 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07003347 if (state->free_reqs)
3348 kmem_cache_free_bulk(req_cachep, state->free_reqs,
3349 &state->reqs[state->cur_req]);
Jens Axboe9a56a232019-01-09 09:06:50 -07003350}
3351
3352/*
3353 * Start submission side cache.
3354 */
3355static void io_submit_state_start(struct io_submit_state *state,
3356 struct io_ring_ctx *ctx, unsigned max_ios)
3357{
3358 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07003359 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07003360 state->file = NULL;
3361 state->ios_left = max_ios;
3362}
3363
Jens Axboe2b188cc2019-01-07 10:46:33 -07003364static void io_commit_sqring(struct io_ring_ctx *ctx)
3365{
Hristo Venev75b28af2019-08-26 17:23:46 +00003366 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003367
Hristo Venev75b28af2019-08-26 17:23:46 +00003368 if (ctx->cached_sq_head != READ_ONCE(rings->sq.head)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07003369 /*
3370 * Ensure any loads from the SQEs are done at this point,
3371 * since once we write the new head, the application could
3372 * write new data to them.
3373 */
Hristo Venev75b28af2019-08-26 17:23:46 +00003374 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003375 }
3376}
3377
3378/*
Jens Axboe2b188cc2019-01-07 10:46:33 -07003379 * Fetch an sqe, if one is available. Note that s->sqe will point to memory
3380 * that is mapped by userspace. This means that care needs to be taken to
3381 * ensure that reads are stable, as we cannot rely on userspace always
3382 * being a good citizen. If members of the sqe are validated and then later
3383 * used, it's important that those reads are done through READ_ONCE() to
3384 * prevent a re-load down the line.
3385 */
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003386static bool io_get_sqring(struct io_ring_ctx *ctx, struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003387{
Hristo Venev75b28af2019-08-26 17:23:46 +00003388 struct io_rings *rings = ctx->rings;
3389 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003390 unsigned head;
3391
3392 /*
3393 * The cached sq head (or cq tail) serves two purposes:
3394 *
3395 * 1) allows us to batch the cost of updating the user visible
3396 * head updates.
3397 * 2) allows the kernel side to track the head on its own, even
3398 * though the application is the one updating it.
3399 */
3400 head = ctx->cached_sq_head;
Stefan Bühlere523a292019-04-19 11:57:44 +02003401 /* make sure SQ entry isn't read before tail */
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03003402 if (unlikely(head == smp_load_acquire(&rings->sq.tail)))
Jens Axboe2b188cc2019-01-07 10:46:33 -07003403 return false;
3404
Hristo Venev75b28af2019-08-26 17:23:46 +00003405 head = READ_ONCE(sq_array[head & ctx->sq_mask]);
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03003406 if (likely(head < ctx->sq_entries)) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003407 /*
3408 * All io need record the previous position, if LINK vs DARIN,
3409 * it can be used to mark the position of the first IO in the
3410 * link list.
3411 */
3412 req->sequence = ctx->cached_sq_head;
3413 req->sqe = &ctx->sq_sqes[head];
Jens Axboe2b188cc2019-01-07 10:46:33 -07003414 ctx->cached_sq_head++;
3415 return true;
3416 }
3417
3418 /* drop invalid entries */
3419 ctx->cached_sq_head++;
Jens Axboe498ccd92019-10-25 10:04:25 -06003420 ctx->cached_sq_dropped++;
3421 WRITE_ONCE(rings->sq_dropped, ctx->cached_sq_dropped);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003422 return false;
3423}
3424
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003425static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkovae9428c2019-11-06 00:22:14 +03003426 struct file *ring_file, int ring_fd,
3427 struct mm_struct **mm, bool async)
Jens Axboe6c271ce2019-01-10 11:22:30 -07003428{
3429 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06003430 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06003431 int i, submitted = 0;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03003432 bool mm_fault = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003433
Jens Axboec4a2ed72019-11-21 21:01:26 -07003434 /* if we have a backlog and couldn't flush it all, return BUSY */
3435 if (!list_empty(&ctx->cq_overflow_list) &&
3436 !io_cqring_overflow_flush(ctx, false))
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003437 return -EBUSY;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003438
3439 if (nr > IO_PLUG_THRESHOLD) {
3440 io_submit_state_start(&state, ctx, nr);
3441 statep = &state;
3442 }
3443
3444 for (i = 0; i < nr; i++) {
Pavel Begunkov196be952019-11-07 01:41:06 +03003445 struct io_kiocb *req;
Pavel Begunkov50585b92019-11-07 01:41:07 +03003446 unsigned int sqe_flags;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003447
Pavel Begunkov196be952019-11-07 01:41:06 +03003448 req = io_get_req(ctx, statep);
3449 if (unlikely(!req)) {
3450 if (!submitted)
3451 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003452 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06003453 }
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003454 if (!io_get_sqring(ctx, req)) {
Pavel Begunkov196be952019-11-07 01:41:06 +03003455 __io_free_req(req);
3456 break;
3457 }
Jens Axboe9e645e112019-05-10 16:07:28 -06003458
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003459 if (io_sqe_needs_user(req->sqe) && !*mm) {
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03003460 mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm);
3461 if (!mm_fault) {
3462 use_mm(ctx->sqo_mm);
3463 *mm = ctx->sqo_mm;
3464 }
3465 }
3466
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003467 sqe_flags = req->sqe->flags;
Pavel Begunkov50585b92019-11-07 01:41:07 +03003468
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003469 req->ring_file = ring_file;
3470 req->ring_fd = ring_fd;
3471 req->has_user = *mm != NULL;
3472 req->in_async = async;
3473 req->needs_fixed_file = async;
3474 trace_io_uring_submit_sqe(ctx, req->sqe->user_data,
Pavel Begunkov50585b92019-11-07 01:41:07 +03003475 true, async);
Jackie Liua197f662019-11-08 08:09:12 -07003476 io_submit_sqe(req, statep, &link);
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03003477 submitted++;
Pavel Begunkove5eb6362019-11-06 00:22:15 +03003478
3479 /*
3480 * If previous wasn't linked and we have a linked command,
3481 * that's the end of the chain. Submit the previous link.
3482 */
Pavel Begunkov50585b92019-11-07 01:41:07 +03003483 if (!(sqe_flags & IOSQE_IO_LINK) && link) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003484 io_queue_link_head(link);
Pavel Begunkove5eb6362019-11-06 00:22:15 +03003485 link = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003486 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07003487 }
3488
Jens Axboe9e645e112019-05-10 16:07:28 -06003489 if (link)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003490 io_queue_link_head(link);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003491 if (statep)
3492 io_submit_state_end(&state);
3493
Pavel Begunkovae9428c2019-11-06 00:22:14 +03003494 /* Commit SQ ring head once we've consumed and submitted all SQEs */
3495 io_commit_sqring(ctx);
3496
Jens Axboe6c271ce2019-01-10 11:22:30 -07003497 return submitted;
3498}
3499
3500static int io_sq_thread(void *data)
3501{
Jens Axboe6c271ce2019-01-10 11:22:30 -07003502 struct io_ring_ctx *ctx = data;
3503 struct mm_struct *cur_mm = NULL;
Jens Axboe181e4482019-11-25 08:52:30 -07003504 const struct cred *old_cred;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003505 mm_segment_t old_fs;
3506 DEFINE_WAIT(wait);
3507 unsigned inflight;
3508 unsigned long timeout;
Jens Axboec1edbf52019-11-10 16:56:04 -07003509 int ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003510
Jens Axboe206aefd2019-11-07 18:27:42 -07003511 complete(&ctx->completions[1]);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08003512
Jens Axboe6c271ce2019-01-10 11:22:30 -07003513 old_fs = get_fs();
3514 set_fs(USER_DS);
Jens Axboe181e4482019-11-25 08:52:30 -07003515 old_cred = override_creds(ctx->creds);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003516
Jens Axboec1edbf52019-11-10 16:56:04 -07003517 ret = timeout = inflight = 0;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003518 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003519 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003520
3521 if (inflight) {
3522 unsigned nr_events = 0;
3523
3524 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe2b2ed972019-10-25 10:06:15 -06003525 /*
3526 * inflight is the count of the maximum possible
3527 * entries we submitted, but it can be smaller
3528 * if we dropped some of them. If we don't have
3529 * poll entries available, then we know that we
3530 * have nothing left to poll for. Reset the
3531 * inflight count to zero in that case.
3532 */
3533 mutex_lock(&ctx->uring_lock);
3534 if (!list_empty(&ctx->poll_list))
3535 __io_iopoll_check(ctx, &nr_events, 0);
3536 else
3537 inflight = 0;
3538 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003539 } else {
3540 /*
3541 * Normal IO, just pretend everything completed.
3542 * We don't have to poll completions for that.
3543 */
3544 nr_events = inflight;
3545 }
3546
3547 inflight -= nr_events;
3548 if (!inflight)
3549 timeout = jiffies + ctx->sq_thread_idle;
3550 }
3551
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003552 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07003553
3554 /*
3555 * If submit got -EBUSY, flag us as needing the application
3556 * to enter the kernel to reap and flush events.
3557 */
3558 if (!to_submit || ret == -EBUSY) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07003559 /*
3560 * We're polling. If we're within the defined idle
3561 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07003562 * to sleep. The exception is if we got EBUSY doing
3563 * more IO, we should wait for the application to
3564 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07003565 */
Jens Axboec1edbf52019-11-10 16:56:04 -07003566 if (inflight ||
3567 (!time_after(jiffies, timeout) && ret != -EBUSY)) {
Jens Axboe9831a902019-09-19 09:48:55 -06003568 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07003569 continue;
3570 }
3571
3572 /*
3573 * Drop cur_mm before scheduling, we can't hold it for
3574 * long periods (or over schedule()). Do this before
3575 * adding ourselves to the waitqueue, as the unuse/drop
3576 * may sleep.
3577 */
3578 if (cur_mm) {
3579 unuse_mm(cur_mm);
3580 mmput(cur_mm);
3581 cur_mm = NULL;
3582 }
3583
3584 prepare_to_wait(&ctx->sqo_wait, &wait,
3585 TASK_INTERRUPTIBLE);
3586
3587 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00003588 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02003589 /* make sure to read SQ tail after writing flags */
3590 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07003591
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003592 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07003593 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003594 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07003595 finish_wait(&ctx->sqo_wait, &wait);
3596 break;
3597 }
3598 if (signal_pending(current))
3599 flush_signals(current);
3600 schedule();
3601 finish_wait(&ctx->sqo_wait, &wait);
3602
Hristo Venev75b28af2019-08-26 17:23:46 +00003603 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003604 continue;
3605 }
3606 finish_wait(&ctx->sqo_wait, &wait);
3607
Hristo Venev75b28af2019-08-26 17:23:46 +00003608 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003609 }
3610
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003611 to_submit = min(to_submit, ctx->sq_entries);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003612 ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true);
3613 if (ret > 0)
3614 inflight += ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003615 }
3616
3617 set_fs(old_fs);
3618 if (cur_mm) {
3619 unuse_mm(cur_mm);
3620 mmput(cur_mm);
3621 }
Jens Axboe181e4482019-11-25 08:52:30 -07003622 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06003623
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003624 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06003625
Jens Axboe6c271ce2019-01-10 11:22:30 -07003626 return 0;
3627}
3628
Jens Axboebda52162019-09-24 13:47:15 -06003629struct io_wait_queue {
3630 struct wait_queue_entry wq;
3631 struct io_ring_ctx *ctx;
3632 unsigned to_wait;
3633 unsigned nr_timeouts;
3634};
3635
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003636static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06003637{
3638 struct io_ring_ctx *ctx = iowq->ctx;
3639
3640 /*
3641 * Wake up if we have enough events, or if a timeout occured since we
3642 * started waiting. For timeouts, we always want to return to userspace,
3643 * regardless of event count.
3644 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003645 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06003646 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
3647}
3648
3649static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
3650 int wake_flags, void *key)
3651{
3652 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
3653 wq);
3654
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003655 /* use noflush == true, as we can't safely rely on locking context */
3656 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06003657 return -1;
3658
3659 return autoremove_wake_function(curr, mode, wake_flags, key);
3660}
3661
Jens Axboe2b188cc2019-01-07 10:46:33 -07003662/*
3663 * Wait until events become available, if we don't already have some. The
3664 * application must reap them itself, as they reside on the shared cq ring.
3665 */
3666static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
3667 const sigset_t __user *sig, size_t sigsz)
3668{
Jens Axboebda52162019-09-24 13:47:15 -06003669 struct io_wait_queue iowq = {
3670 .wq = {
3671 .private = current,
3672 .func = io_wake_function,
3673 .entry = LIST_HEAD_INIT(iowq.wq.entry),
3674 },
3675 .ctx = ctx,
3676 .to_wait = min_events,
3677 };
Hristo Venev75b28af2019-08-26 17:23:46 +00003678 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08003679 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003680
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003681 if (io_cqring_events(ctx, false) >= min_events)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003682 return 0;
3683
3684 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01003685#ifdef CONFIG_COMPAT
3686 if (in_compat_syscall())
3687 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07003688 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01003689 else
3690#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07003691 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01003692
Jens Axboe2b188cc2019-01-07 10:46:33 -07003693 if (ret)
3694 return ret;
3695 }
3696
Jens Axboebda52162019-09-24 13:47:15 -06003697 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02003698 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06003699 do {
3700 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
3701 TASK_INTERRUPTIBLE);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003702 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06003703 break;
3704 schedule();
3705 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08003706 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06003707 break;
3708 }
3709 } while (1);
3710 finish_wait(&ctx->wait, &iowq.wq);
3711
Jackie Liue9ffa5c2019-10-29 11:16:42 +08003712 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003713
Hristo Venev75b28af2019-08-26 17:23:46 +00003714 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003715}
3716
Jens Axboe6b063142019-01-10 22:13:58 -07003717static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
3718{
3719#if defined(CONFIG_UNIX)
3720 if (ctx->ring_sock) {
3721 struct sock *sock = ctx->ring_sock->sk;
3722 struct sk_buff *skb;
3723
3724 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
3725 kfree_skb(skb);
3726 }
3727#else
3728 int i;
3729
Jens Axboe65e19f52019-10-26 07:20:21 -06003730 for (i = 0; i < ctx->nr_user_files; i++) {
3731 struct file *file;
3732
3733 file = io_file_from_index(ctx, i);
3734 if (file)
3735 fput(file);
3736 }
Jens Axboe6b063142019-01-10 22:13:58 -07003737#endif
3738}
3739
3740static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
3741{
Jens Axboe65e19f52019-10-26 07:20:21 -06003742 unsigned nr_tables, i;
3743
3744 if (!ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003745 return -ENXIO;
3746
3747 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06003748 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
3749 for (i = 0; i < nr_tables; i++)
3750 kfree(ctx->file_table[i].files);
3751 kfree(ctx->file_table);
3752 ctx->file_table = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003753 ctx->nr_user_files = 0;
3754 return 0;
3755}
3756
Jens Axboe6c271ce2019-01-10 11:22:30 -07003757static void io_sq_thread_stop(struct io_ring_ctx *ctx)
3758{
3759 if (ctx->sqo_thread) {
Jens Axboe206aefd2019-11-07 18:27:42 -07003760 wait_for_completion(&ctx->completions[1]);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003761 /*
3762 * The park is a bit of a work-around, without it we get
3763 * warning spews on shutdown with SQPOLL set and affinity
3764 * set to a single CPU.
3765 */
Jens Axboe06058632019-04-13 09:26:03 -06003766 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003767 kthread_stop(ctx->sqo_thread);
3768 ctx->sqo_thread = NULL;
3769 }
3770}
3771
Jens Axboe6b063142019-01-10 22:13:58 -07003772static void io_finish_async(struct io_ring_ctx *ctx)
3773{
Jens Axboe6c271ce2019-01-10 11:22:30 -07003774 io_sq_thread_stop(ctx);
3775
Jens Axboe561fb042019-10-24 07:25:42 -06003776 if (ctx->io_wq) {
3777 io_wq_destroy(ctx->io_wq);
3778 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003779 }
3780}
3781
3782#if defined(CONFIG_UNIX)
3783static void io_destruct_skb(struct sk_buff *skb)
3784{
3785 struct io_ring_ctx *ctx = skb->sk->sk_user_data;
3786
Jens Axboe561fb042019-10-24 07:25:42 -06003787 if (ctx->io_wq)
3788 io_wq_flush(ctx->io_wq);
Jens Axboe8a997342019-10-09 14:40:13 -06003789
Jens Axboe6b063142019-01-10 22:13:58 -07003790 unix_destruct_scm(skb);
3791}
3792
3793/*
3794 * Ensure the UNIX gc is aware of our file set, so we are certain that
3795 * the io_uring can be safely unregistered on process exit, even if we have
3796 * loops in the file referencing.
3797 */
3798static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
3799{
3800 struct sock *sk = ctx->ring_sock->sk;
3801 struct scm_fp_list *fpl;
3802 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06003803 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07003804
3805 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
3806 unsigned long inflight = ctx->user->unix_inflight + nr;
3807
3808 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
3809 return -EMFILE;
3810 }
3811
3812 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
3813 if (!fpl)
3814 return -ENOMEM;
3815
3816 skb = alloc_skb(0, GFP_KERNEL);
3817 if (!skb) {
3818 kfree(fpl);
3819 return -ENOMEM;
3820 }
3821
3822 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07003823
Jens Axboe08a45172019-10-03 08:11:03 -06003824 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07003825 fpl->user = get_uid(ctx->user);
3826 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003827 struct file *file = io_file_from_index(ctx, i + offset);
3828
3829 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06003830 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06003831 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06003832 unix_inflight(fpl->user, fpl->fp[nr_files]);
3833 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07003834 }
3835
Jens Axboe08a45172019-10-03 08:11:03 -06003836 if (nr_files) {
3837 fpl->max = SCM_MAX_FD;
3838 fpl->count = nr_files;
3839 UNIXCB(skb).fp = fpl;
3840 skb->destructor = io_destruct_skb;
3841 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
3842 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07003843
Jens Axboe08a45172019-10-03 08:11:03 -06003844 for (i = 0; i < nr_files; i++)
3845 fput(fpl->fp[i]);
3846 } else {
3847 kfree_skb(skb);
3848 kfree(fpl);
3849 }
Jens Axboe6b063142019-01-10 22:13:58 -07003850
3851 return 0;
3852}
3853
3854/*
3855 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
3856 * causes regular reference counting to break down. We rely on the UNIX
3857 * garbage collection to take care of this problem for us.
3858 */
3859static int io_sqe_files_scm(struct io_ring_ctx *ctx)
3860{
3861 unsigned left, total;
3862 int ret = 0;
3863
3864 total = 0;
3865 left = ctx->nr_user_files;
3866 while (left) {
3867 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07003868
3869 ret = __io_sqe_files_scm(ctx, this_files, total);
3870 if (ret)
3871 break;
3872 left -= this_files;
3873 total += this_files;
3874 }
3875
3876 if (!ret)
3877 return 0;
3878
3879 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003880 struct file *file = io_file_from_index(ctx, total);
3881
3882 if (file)
3883 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07003884 total++;
3885 }
3886
3887 return ret;
3888}
3889#else
3890static int io_sqe_files_scm(struct io_ring_ctx *ctx)
3891{
3892 return 0;
3893}
3894#endif
3895
Jens Axboe65e19f52019-10-26 07:20:21 -06003896static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
3897 unsigned nr_files)
3898{
3899 int i;
3900
3901 for (i = 0; i < nr_tables; i++) {
3902 struct fixed_file_table *table = &ctx->file_table[i];
3903 unsigned this_files;
3904
3905 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
3906 table->files = kcalloc(this_files, sizeof(struct file *),
3907 GFP_KERNEL);
3908 if (!table->files)
3909 break;
3910 nr_files -= this_files;
3911 }
3912
3913 if (i == nr_tables)
3914 return 0;
3915
3916 for (i = 0; i < nr_tables; i++) {
3917 struct fixed_file_table *table = &ctx->file_table[i];
3918 kfree(table->files);
3919 }
3920 return 1;
3921}
3922
Jens Axboe6b063142019-01-10 22:13:58 -07003923static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
3924 unsigned nr_args)
3925{
3926 __s32 __user *fds = (__s32 __user *) arg;
Jens Axboe65e19f52019-10-26 07:20:21 -06003927 unsigned nr_tables;
Jens Axboe6b063142019-01-10 22:13:58 -07003928 int fd, ret = 0;
3929 unsigned i;
3930
Jens Axboe65e19f52019-10-26 07:20:21 -06003931 if (ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003932 return -EBUSY;
3933 if (!nr_args)
3934 return -EINVAL;
3935 if (nr_args > IORING_MAX_FIXED_FILES)
3936 return -EMFILE;
3937
Jens Axboe65e19f52019-10-26 07:20:21 -06003938 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
3939 ctx->file_table = kcalloc(nr_tables, sizeof(struct fixed_file_table),
3940 GFP_KERNEL);
3941 if (!ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07003942 return -ENOMEM;
3943
Jens Axboe65e19f52019-10-26 07:20:21 -06003944 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
3945 kfree(ctx->file_table);
Jens Axboe46568e92019-11-10 08:40:53 -07003946 ctx->file_table = NULL;
Jens Axboe65e19f52019-10-26 07:20:21 -06003947 return -ENOMEM;
3948 }
3949
Jens Axboe08a45172019-10-03 08:11:03 -06003950 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003951 struct fixed_file_table *table;
3952 unsigned index;
3953
Jens Axboe6b063142019-01-10 22:13:58 -07003954 ret = -EFAULT;
3955 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
3956 break;
Jens Axboe08a45172019-10-03 08:11:03 -06003957 /* allow sparse sets */
3958 if (fd == -1) {
3959 ret = 0;
3960 continue;
3961 }
Jens Axboe6b063142019-01-10 22:13:58 -07003962
Jens Axboe65e19f52019-10-26 07:20:21 -06003963 table = &ctx->file_table[i >> IORING_FILE_TABLE_SHIFT];
3964 index = i & IORING_FILE_TABLE_MASK;
3965 table->files[index] = fget(fd);
Jens Axboe6b063142019-01-10 22:13:58 -07003966
3967 ret = -EBADF;
Jens Axboe65e19f52019-10-26 07:20:21 -06003968 if (!table->files[index])
Jens Axboe6b063142019-01-10 22:13:58 -07003969 break;
3970 /*
3971 * Don't allow io_uring instances to be registered. If UNIX
3972 * isn't enabled, then this causes a reference cycle and this
3973 * instance can never get freed. If UNIX is enabled we'll
3974 * handle it just fine, but there's still no point in allowing
3975 * a ring fd as it doesn't support regular read/write anyway.
3976 */
Jens Axboe65e19f52019-10-26 07:20:21 -06003977 if (table->files[index]->f_op == &io_uring_fops) {
3978 fput(table->files[index]);
Jens Axboe6b063142019-01-10 22:13:58 -07003979 break;
3980 }
Jens Axboe6b063142019-01-10 22:13:58 -07003981 ret = 0;
3982 }
3983
3984 if (ret) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003985 for (i = 0; i < ctx->nr_user_files; i++) {
3986 struct file *file;
Jens Axboe6b063142019-01-10 22:13:58 -07003987
Jens Axboe65e19f52019-10-26 07:20:21 -06003988 file = io_file_from_index(ctx, i);
3989 if (file)
3990 fput(file);
3991 }
3992 for (i = 0; i < nr_tables; i++)
3993 kfree(ctx->file_table[i].files);
3994
3995 kfree(ctx->file_table);
3996 ctx->file_table = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07003997 ctx->nr_user_files = 0;
3998 return ret;
3999 }
4000
4001 ret = io_sqe_files_scm(ctx);
4002 if (ret)
4003 io_sqe_files_unregister(ctx);
4004
4005 return ret;
4006}
4007
Jens Axboec3a31e62019-10-03 13:59:56 -06004008static void io_sqe_file_unregister(struct io_ring_ctx *ctx, int index)
4009{
4010#if defined(CONFIG_UNIX)
Jens Axboe65e19f52019-10-26 07:20:21 -06004011 struct file *file = io_file_from_index(ctx, index);
Jens Axboec3a31e62019-10-03 13:59:56 -06004012 struct sock *sock = ctx->ring_sock->sk;
4013 struct sk_buff_head list, *head = &sock->sk_receive_queue;
4014 struct sk_buff *skb;
4015 int i;
4016
4017 __skb_queue_head_init(&list);
4018
4019 /*
4020 * Find the skb that holds this file in its SCM_RIGHTS. When found,
4021 * remove this entry and rearrange the file array.
4022 */
4023 skb = skb_dequeue(head);
4024 while (skb) {
4025 struct scm_fp_list *fp;
4026
4027 fp = UNIXCB(skb).fp;
4028 for (i = 0; i < fp->count; i++) {
4029 int left;
4030
4031 if (fp->fp[i] != file)
4032 continue;
4033
4034 unix_notinflight(fp->user, fp->fp[i]);
4035 left = fp->count - 1 - i;
4036 if (left) {
4037 memmove(&fp->fp[i], &fp->fp[i + 1],
4038 left * sizeof(struct file *));
4039 }
4040 fp->count--;
4041 if (!fp->count) {
4042 kfree_skb(skb);
4043 skb = NULL;
4044 } else {
4045 __skb_queue_tail(&list, skb);
4046 }
4047 fput(file);
4048 file = NULL;
4049 break;
4050 }
4051
4052 if (!file)
4053 break;
4054
4055 __skb_queue_tail(&list, skb);
4056
4057 skb = skb_dequeue(head);
4058 }
4059
4060 if (skb_peek(&list)) {
4061 spin_lock_irq(&head->lock);
4062 while ((skb = __skb_dequeue(&list)) != NULL)
4063 __skb_queue_tail(head, skb);
4064 spin_unlock_irq(&head->lock);
4065 }
4066#else
Jens Axboe65e19f52019-10-26 07:20:21 -06004067 fput(io_file_from_index(ctx, index));
Jens Axboec3a31e62019-10-03 13:59:56 -06004068#endif
4069}
4070
4071static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
4072 int index)
4073{
4074#if defined(CONFIG_UNIX)
4075 struct sock *sock = ctx->ring_sock->sk;
4076 struct sk_buff_head *head = &sock->sk_receive_queue;
4077 struct sk_buff *skb;
4078
4079 /*
4080 * See if we can merge this file into an existing skb SCM_RIGHTS
4081 * file set. If there's no room, fall back to allocating a new skb
4082 * and filling it in.
4083 */
4084 spin_lock_irq(&head->lock);
4085 skb = skb_peek(head);
4086 if (skb) {
4087 struct scm_fp_list *fpl = UNIXCB(skb).fp;
4088
4089 if (fpl->count < SCM_MAX_FD) {
4090 __skb_unlink(skb, head);
4091 spin_unlock_irq(&head->lock);
4092 fpl->fp[fpl->count] = get_file(file);
4093 unix_inflight(fpl->user, fpl->fp[fpl->count]);
4094 fpl->count++;
4095 spin_lock_irq(&head->lock);
4096 __skb_queue_head(head, skb);
4097 } else {
4098 skb = NULL;
4099 }
4100 }
4101 spin_unlock_irq(&head->lock);
4102
4103 if (skb) {
4104 fput(file);
4105 return 0;
4106 }
4107
4108 return __io_sqe_files_scm(ctx, 1, index);
4109#else
4110 return 0;
4111#endif
4112}
4113
4114static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
4115 unsigned nr_args)
4116{
4117 struct io_uring_files_update up;
4118 __s32 __user *fds;
4119 int fd, i, err;
4120 __u32 done;
4121
Jens Axboe65e19f52019-10-26 07:20:21 -06004122 if (!ctx->file_table)
Jens Axboec3a31e62019-10-03 13:59:56 -06004123 return -ENXIO;
4124 if (!nr_args)
4125 return -EINVAL;
4126 if (copy_from_user(&up, arg, sizeof(up)))
4127 return -EFAULT;
4128 if (check_add_overflow(up.offset, nr_args, &done))
4129 return -EOVERFLOW;
4130 if (done > ctx->nr_user_files)
4131 return -EINVAL;
4132
4133 done = 0;
4134 fds = (__s32 __user *) up.fds;
4135 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06004136 struct fixed_file_table *table;
4137 unsigned index;
4138
Jens Axboec3a31e62019-10-03 13:59:56 -06004139 err = 0;
4140 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
4141 err = -EFAULT;
4142 break;
4143 }
4144 i = array_index_nospec(up.offset, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06004145 table = &ctx->file_table[i >> IORING_FILE_TABLE_SHIFT];
4146 index = i & IORING_FILE_TABLE_MASK;
4147 if (table->files[index]) {
Jens Axboec3a31e62019-10-03 13:59:56 -06004148 io_sqe_file_unregister(ctx, i);
Jens Axboe65e19f52019-10-26 07:20:21 -06004149 table->files[index] = NULL;
Jens Axboec3a31e62019-10-03 13:59:56 -06004150 }
4151 if (fd != -1) {
4152 struct file *file;
4153
4154 file = fget(fd);
4155 if (!file) {
4156 err = -EBADF;
4157 break;
4158 }
4159 /*
4160 * Don't allow io_uring instances to be registered. If
4161 * UNIX isn't enabled, then this causes a reference
4162 * cycle and this instance can never get freed. If UNIX
4163 * is enabled we'll handle it just fine, but there's
4164 * still no point in allowing a ring fd as it doesn't
4165 * support regular read/write anyway.
4166 */
4167 if (file->f_op == &io_uring_fops) {
4168 fput(file);
4169 err = -EBADF;
4170 break;
4171 }
Jens Axboe65e19f52019-10-26 07:20:21 -06004172 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06004173 err = io_sqe_file_register(ctx, file, i);
4174 if (err)
4175 break;
4176 }
4177 nr_args--;
4178 done++;
4179 up.offset++;
4180 }
4181
4182 return done ? done : err;
4183}
4184
Jens Axboe7d723062019-11-12 22:31:31 -07004185static void io_put_work(struct io_wq_work *work)
4186{
4187 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
4188
4189 io_put_req(req);
4190}
4191
4192static void io_get_work(struct io_wq_work *work)
4193{
4194 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
4195
4196 refcount_inc(&req->refs);
4197}
4198
Jens Axboe6c271ce2019-01-10 11:22:30 -07004199static int io_sq_offload_start(struct io_ring_ctx *ctx,
4200 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004201{
Jens Axboe576a3472019-11-25 08:49:20 -07004202 struct io_wq_data data;
Jens Axboe561fb042019-10-24 07:25:42 -06004203 unsigned concurrency;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004204 int ret;
4205
Jens Axboe6c271ce2019-01-10 11:22:30 -07004206 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004207 mmgrab(current->mm);
4208 ctx->sqo_mm = current->mm;
4209
Jens Axboe6c271ce2019-01-10 11:22:30 -07004210 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06004211 ret = -EPERM;
4212 if (!capable(CAP_SYS_ADMIN))
4213 goto err;
4214
Jens Axboe917257d2019-04-13 09:28:55 -06004215 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
4216 if (!ctx->sq_thread_idle)
4217 ctx->sq_thread_idle = HZ;
4218
Jens Axboe6c271ce2019-01-10 11:22:30 -07004219 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06004220 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004221
Jens Axboe917257d2019-04-13 09:28:55 -06004222 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06004223 if (cpu >= nr_cpu_ids)
4224 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08004225 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06004226 goto err;
4227
Jens Axboe6c271ce2019-01-10 11:22:30 -07004228 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
4229 ctx, cpu,
4230 "io_uring-sq");
4231 } else {
4232 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
4233 "io_uring-sq");
4234 }
4235 if (IS_ERR(ctx->sqo_thread)) {
4236 ret = PTR_ERR(ctx->sqo_thread);
4237 ctx->sqo_thread = NULL;
4238 goto err;
4239 }
4240 wake_up_process(ctx->sqo_thread);
4241 } else if (p->flags & IORING_SETUP_SQ_AFF) {
4242 /* Can't have SQ_AFF without SQPOLL */
4243 ret = -EINVAL;
4244 goto err;
4245 }
4246
Jens Axboe576a3472019-11-25 08:49:20 -07004247 data.mm = ctx->sqo_mm;
4248 data.user = ctx->user;
Jens Axboe181e4482019-11-25 08:52:30 -07004249 data.creds = ctx->creds;
Jens Axboe576a3472019-11-25 08:49:20 -07004250 data.get_work = io_get_work;
4251 data.put_work = io_put_work;
4252
Jens Axboe561fb042019-10-24 07:25:42 -06004253 /* Do QD, or 4 * CPUS, whatever is smallest */
4254 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
Jens Axboe576a3472019-11-25 08:49:20 -07004255 ctx->io_wq = io_wq_create(concurrency, &data);
Jens Axboe975c99a52019-10-30 08:42:56 -06004256 if (IS_ERR(ctx->io_wq)) {
4257 ret = PTR_ERR(ctx->io_wq);
4258 ctx->io_wq = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004259 goto err;
4260 }
4261
4262 return 0;
4263err:
Jens Axboe54a91f32019-09-10 09:15:04 -06004264 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004265 mmdrop(ctx->sqo_mm);
4266 ctx->sqo_mm = NULL;
4267 return ret;
4268}
4269
4270static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
4271{
4272 atomic_long_sub(nr_pages, &user->locked_vm);
4273}
4274
4275static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
4276{
4277 unsigned long page_limit, cur_pages, new_pages;
4278
4279 /* Don't allow more pages than we can safely lock */
4280 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
4281
4282 do {
4283 cur_pages = atomic_long_read(&user->locked_vm);
4284 new_pages = cur_pages + nr_pages;
4285 if (new_pages > page_limit)
4286 return -ENOMEM;
4287 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
4288 new_pages) != cur_pages);
4289
4290 return 0;
4291}
4292
4293static void io_mem_free(void *ptr)
4294{
Mark Rutland52e04ef2019-04-30 17:30:21 +01004295 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004296
Mark Rutland52e04ef2019-04-30 17:30:21 +01004297 if (!ptr)
4298 return;
4299
4300 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004301 if (put_page_testzero(page))
4302 free_compound_page(page);
4303}
4304
4305static void *io_mem_alloc(size_t size)
4306{
4307 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
4308 __GFP_NORETRY;
4309
4310 return (void *) __get_free_pages(gfp_flags, get_order(size));
4311}
4312
Hristo Venev75b28af2019-08-26 17:23:46 +00004313static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
4314 size_t *sq_offset)
4315{
4316 struct io_rings *rings;
4317 size_t off, sq_array_size;
4318
4319 off = struct_size(rings, cqes, cq_entries);
4320 if (off == SIZE_MAX)
4321 return SIZE_MAX;
4322
4323#ifdef CONFIG_SMP
4324 off = ALIGN(off, SMP_CACHE_BYTES);
4325 if (off == 0)
4326 return SIZE_MAX;
4327#endif
4328
4329 sq_array_size = array_size(sizeof(u32), sq_entries);
4330 if (sq_array_size == SIZE_MAX)
4331 return SIZE_MAX;
4332
4333 if (check_add_overflow(off, sq_array_size, &off))
4334 return SIZE_MAX;
4335
4336 if (sq_offset)
4337 *sq_offset = off;
4338
4339 return off;
4340}
4341
Jens Axboe2b188cc2019-01-07 10:46:33 -07004342static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
4343{
Hristo Venev75b28af2019-08-26 17:23:46 +00004344 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004345
Hristo Venev75b28af2019-08-26 17:23:46 +00004346 pages = (size_t)1 << get_order(
4347 rings_size(sq_entries, cq_entries, NULL));
4348 pages += (size_t)1 << get_order(
4349 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07004350
Hristo Venev75b28af2019-08-26 17:23:46 +00004351 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004352}
4353
Jens Axboeedafcce2019-01-09 09:16:05 -07004354static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
4355{
4356 int i, j;
4357
4358 if (!ctx->user_bufs)
4359 return -ENXIO;
4360
4361 for (i = 0; i < ctx->nr_user_bufs; i++) {
4362 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
4363
4364 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbard27c4d3a2019-08-04 19:32:06 -07004365 put_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07004366
4367 if (ctx->account_mem)
4368 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004369 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07004370 imu->nr_bvecs = 0;
4371 }
4372
4373 kfree(ctx->user_bufs);
4374 ctx->user_bufs = NULL;
4375 ctx->nr_user_bufs = 0;
4376 return 0;
4377}
4378
4379static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
4380 void __user *arg, unsigned index)
4381{
4382 struct iovec __user *src;
4383
4384#ifdef CONFIG_COMPAT
4385 if (ctx->compat) {
4386 struct compat_iovec __user *ciovs;
4387 struct compat_iovec ciov;
4388
4389 ciovs = (struct compat_iovec __user *) arg;
4390 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
4391 return -EFAULT;
4392
4393 dst->iov_base = (void __user *) (unsigned long) ciov.iov_base;
4394 dst->iov_len = ciov.iov_len;
4395 return 0;
4396 }
4397#endif
4398 src = (struct iovec __user *) arg;
4399 if (copy_from_user(dst, &src[index], sizeof(*dst)))
4400 return -EFAULT;
4401 return 0;
4402}
4403
4404static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
4405 unsigned nr_args)
4406{
4407 struct vm_area_struct **vmas = NULL;
4408 struct page **pages = NULL;
4409 int i, j, got_pages = 0;
4410 int ret = -EINVAL;
4411
4412 if (ctx->user_bufs)
4413 return -EBUSY;
4414 if (!nr_args || nr_args > UIO_MAXIOV)
4415 return -EINVAL;
4416
4417 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
4418 GFP_KERNEL);
4419 if (!ctx->user_bufs)
4420 return -ENOMEM;
4421
4422 for (i = 0; i < nr_args; i++) {
4423 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
4424 unsigned long off, start, end, ubuf;
4425 int pret, nr_pages;
4426 struct iovec iov;
4427 size_t size;
4428
4429 ret = io_copy_iov(ctx, &iov, arg, i);
4430 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03004431 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07004432
4433 /*
4434 * Don't impose further limits on the size and buffer
4435 * constraints here, we'll -EINVAL later when IO is
4436 * submitted if they are wrong.
4437 */
4438 ret = -EFAULT;
4439 if (!iov.iov_base || !iov.iov_len)
4440 goto err;
4441
4442 /* arbitrary limit, but we need something */
4443 if (iov.iov_len > SZ_1G)
4444 goto err;
4445
4446 ubuf = (unsigned long) iov.iov_base;
4447 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
4448 start = ubuf >> PAGE_SHIFT;
4449 nr_pages = end - start;
4450
4451 if (ctx->account_mem) {
4452 ret = io_account_mem(ctx->user, nr_pages);
4453 if (ret)
4454 goto err;
4455 }
4456
4457 ret = 0;
4458 if (!pages || nr_pages > got_pages) {
4459 kfree(vmas);
4460 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004461 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07004462 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004463 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07004464 sizeof(struct vm_area_struct *),
4465 GFP_KERNEL);
4466 if (!pages || !vmas) {
4467 ret = -ENOMEM;
4468 if (ctx->account_mem)
4469 io_unaccount_mem(ctx->user, nr_pages);
4470 goto err;
4471 }
4472 got_pages = nr_pages;
4473 }
4474
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004475 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07004476 GFP_KERNEL);
4477 ret = -ENOMEM;
4478 if (!imu->bvec) {
4479 if (ctx->account_mem)
4480 io_unaccount_mem(ctx->user, nr_pages);
4481 goto err;
4482 }
4483
4484 ret = 0;
4485 down_read(&current->mm->mmap_sem);
Ira Weiny932f4a62019-05-13 17:17:03 -07004486 pret = get_user_pages(ubuf, nr_pages,
4487 FOLL_WRITE | FOLL_LONGTERM,
4488 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07004489 if (pret == nr_pages) {
4490 /* don't support file backed memory */
4491 for (j = 0; j < nr_pages; j++) {
4492 struct vm_area_struct *vma = vmas[j];
4493
4494 if (vma->vm_file &&
4495 !is_file_hugepages(vma->vm_file)) {
4496 ret = -EOPNOTSUPP;
4497 break;
4498 }
4499 }
4500 } else {
4501 ret = pret < 0 ? pret : -EFAULT;
4502 }
4503 up_read(&current->mm->mmap_sem);
4504 if (ret) {
4505 /*
4506 * if we did partial map, or found file backed vmas,
4507 * release any pages we did get
4508 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07004509 if (pret > 0)
4510 put_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07004511 if (ctx->account_mem)
4512 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004513 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07004514 goto err;
4515 }
4516
4517 off = ubuf & ~PAGE_MASK;
4518 size = iov.iov_len;
4519 for (j = 0; j < nr_pages; j++) {
4520 size_t vec_len;
4521
4522 vec_len = min_t(size_t, size, PAGE_SIZE - off);
4523 imu->bvec[j].bv_page = pages[j];
4524 imu->bvec[j].bv_len = vec_len;
4525 imu->bvec[j].bv_offset = off;
4526 off = 0;
4527 size -= vec_len;
4528 }
4529 /* store original address for later verification */
4530 imu->ubuf = ubuf;
4531 imu->len = iov.iov_len;
4532 imu->nr_bvecs = nr_pages;
4533
4534 ctx->nr_user_bufs++;
4535 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004536 kvfree(pages);
4537 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07004538 return 0;
4539err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004540 kvfree(pages);
4541 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07004542 io_sqe_buffer_unregister(ctx);
4543 return ret;
4544}
4545
Jens Axboe9b402842019-04-11 11:45:41 -06004546static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
4547{
4548 __s32 __user *fds = arg;
4549 int fd;
4550
4551 if (ctx->cq_ev_fd)
4552 return -EBUSY;
4553
4554 if (copy_from_user(&fd, fds, sizeof(*fds)))
4555 return -EFAULT;
4556
4557 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
4558 if (IS_ERR(ctx->cq_ev_fd)) {
4559 int ret = PTR_ERR(ctx->cq_ev_fd);
4560 ctx->cq_ev_fd = NULL;
4561 return ret;
4562 }
4563
4564 return 0;
4565}
4566
4567static int io_eventfd_unregister(struct io_ring_ctx *ctx)
4568{
4569 if (ctx->cq_ev_fd) {
4570 eventfd_ctx_put(ctx->cq_ev_fd);
4571 ctx->cq_ev_fd = NULL;
4572 return 0;
4573 }
4574
4575 return -ENXIO;
4576}
4577
Jens Axboe2b188cc2019-01-07 10:46:33 -07004578static void io_ring_ctx_free(struct io_ring_ctx *ctx)
4579{
Jens Axboe6b063142019-01-10 22:13:58 -07004580 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004581 if (ctx->sqo_mm)
4582 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07004583
4584 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07004585 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07004586 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06004587 io_eventfd_unregister(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07004588
Jens Axboe2b188cc2019-01-07 10:46:33 -07004589#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07004590 if (ctx->ring_sock) {
4591 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004592 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07004593 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004594#endif
4595
Hristo Venev75b28af2019-08-26 17:23:46 +00004596 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004597 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004598
4599 percpu_ref_exit(&ctx->refs);
4600 if (ctx->account_mem)
4601 io_unaccount_mem(ctx->user,
4602 ring_pages(ctx->sq_entries, ctx->cq_entries));
4603 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07004604 put_cred(ctx->creds);
Jens Axboe206aefd2019-11-07 18:27:42 -07004605 kfree(ctx->completions);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07004606 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004607 kfree(ctx);
4608}
4609
4610static __poll_t io_uring_poll(struct file *file, poll_table *wait)
4611{
4612 struct io_ring_ctx *ctx = file->private_data;
4613 __poll_t mask = 0;
4614
4615 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02004616 /*
4617 * synchronizes with barrier from wq_has_sleeper call in
4618 * io_commit_cqring
4619 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004620 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00004621 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
4622 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004623 mask |= EPOLLOUT | EPOLLWRNORM;
yangerkundaa5de52019-09-24 20:53:34 +08004624 if (READ_ONCE(ctx->rings->cq.head) != ctx->cached_cq_tail)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004625 mask |= EPOLLIN | EPOLLRDNORM;
4626
4627 return mask;
4628}
4629
4630static int io_uring_fasync(int fd, struct file *file, int on)
4631{
4632 struct io_ring_ctx *ctx = file->private_data;
4633
4634 return fasync_helper(fd, file, on, &ctx->cq_fasync);
4635}
4636
4637static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
4638{
4639 mutex_lock(&ctx->uring_lock);
4640 percpu_ref_kill(&ctx->refs);
4641 mutex_unlock(&ctx->uring_lock);
4642
Jens Axboe5262f562019-09-17 12:26:57 -06004643 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004644 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06004645
4646 if (ctx->io_wq)
4647 io_wq_cancel_all(ctx->io_wq);
4648
Jens Axboedef596e2019-01-09 08:59:42 -07004649 io_iopoll_reap_events(ctx);
Jens Axboe15dff282019-11-13 09:09:23 -07004650 /* if we failed setting up the ctx, we might not have any rings */
4651 if (ctx->rings)
4652 io_cqring_overflow_flush(ctx, true);
Jens Axboe206aefd2019-11-07 18:27:42 -07004653 wait_for_completion(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004654 io_ring_ctx_free(ctx);
4655}
4656
4657static int io_uring_release(struct inode *inode, struct file *file)
4658{
4659 struct io_ring_ctx *ctx = file->private_data;
4660
4661 file->private_data = NULL;
4662 io_ring_ctx_wait_and_kill(ctx);
4663 return 0;
4664}
4665
Jens Axboefcb323c2019-10-24 12:39:47 -06004666static void io_uring_cancel_files(struct io_ring_ctx *ctx,
4667 struct files_struct *files)
4668{
4669 struct io_kiocb *req;
4670 DEFINE_WAIT(wait);
4671
4672 while (!list_empty_careful(&ctx->inflight_list)) {
Jens Axboe768134d2019-11-10 20:30:53 -07004673 struct io_kiocb *cancel_req = NULL;
Jens Axboefcb323c2019-10-24 12:39:47 -06004674
4675 spin_lock_irq(&ctx->inflight_lock);
4676 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07004677 if (req->work.files != files)
4678 continue;
4679 /* req is being completed, ignore */
4680 if (!refcount_inc_not_zero(&req->refs))
4681 continue;
4682 cancel_req = req;
4683 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06004684 }
Jens Axboe768134d2019-11-10 20:30:53 -07004685 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06004686 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07004687 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06004688 spin_unlock_irq(&ctx->inflight_lock);
4689
Jens Axboe768134d2019-11-10 20:30:53 -07004690 /* We need to keep going until we don't find a matching req */
4691 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06004692 break;
Bob Liu2f6d9b92019-11-13 18:06:24 +08004693
4694 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
4695 io_put_req(cancel_req);
Jens Axboefcb323c2019-10-24 12:39:47 -06004696 schedule();
4697 }
Jens Axboe768134d2019-11-10 20:30:53 -07004698 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06004699}
4700
4701static int io_uring_flush(struct file *file, void *data)
4702{
4703 struct io_ring_ctx *ctx = file->private_data;
4704
4705 io_uring_cancel_files(ctx, data);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004706 if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) {
4707 io_cqring_overflow_flush(ctx, true);
Jens Axboefcb323c2019-10-24 12:39:47 -06004708 io_wq_cancel_all(ctx->io_wq);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004709 }
Jens Axboefcb323c2019-10-24 12:39:47 -06004710 return 0;
4711}
4712
Roman Penyaev6c5c2402019-11-28 12:53:22 +01004713static void *io_uring_validate_mmap_request(struct file *file,
4714 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004715{
Jens Axboe2b188cc2019-01-07 10:46:33 -07004716 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01004717 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004718 struct page *page;
4719 void *ptr;
4720
4721 switch (offset) {
4722 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00004723 case IORING_OFF_CQ_RING:
4724 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004725 break;
4726 case IORING_OFF_SQES:
4727 ptr = ctx->sq_sqes;
4728 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004729 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01004730 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004731 }
4732
4733 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07004734 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01004735 return ERR_PTR(-EINVAL);
4736
4737 return ptr;
4738}
4739
4740#ifdef CONFIG_MMU
4741
4742static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
4743{
4744 size_t sz = vma->vm_end - vma->vm_start;
4745 unsigned long pfn;
4746 void *ptr;
4747
4748 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
4749 if (IS_ERR(ptr))
4750 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004751
4752 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
4753 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
4754}
4755
Roman Penyaev6c5c2402019-11-28 12:53:22 +01004756#else /* !CONFIG_MMU */
4757
4758static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
4759{
4760 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
4761}
4762
4763static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
4764{
4765 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
4766}
4767
4768static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
4769 unsigned long addr, unsigned long len,
4770 unsigned long pgoff, unsigned long flags)
4771{
4772 void *ptr;
4773
4774 ptr = io_uring_validate_mmap_request(file, pgoff, len);
4775 if (IS_ERR(ptr))
4776 return PTR_ERR(ptr);
4777
4778 return (unsigned long) ptr;
4779}
4780
4781#endif /* !CONFIG_MMU */
4782
Jens Axboe2b188cc2019-01-07 10:46:33 -07004783SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
4784 u32, min_complete, u32, flags, const sigset_t __user *, sig,
4785 size_t, sigsz)
4786{
4787 struct io_ring_ctx *ctx;
4788 long ret = -EBADF;
4789 int submitted = 0;
4790 struct fd f;
4791
Jens Axboe6c271ce2019-01-10 11:22:30 -07004792 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07004793 return -EINVAL;
4794
4795 f = fdget(fd);
4796 if (!f.file)
4797 return -EBADF;
4798
4799 ret = -EOPNOTSUPP;
4800 if (f.file->f_op != &io_uring_fops)
4801 goto out_fput;
4802
4803 ret = -ENXIO;
4804 ctx = f.file->private_data;
4805 if (!percpu_ref_tryget(&ctx->refs))
4806 goto out_fput;
4807
Jens Axboe6c271ce2019-01-10 11:22:30 -07004808 /*
4809 * For SQ polling, the thread will do all submissions and completions.
4810 * Just return the requested submit count, and wake the thread if
4811 * we were asked to.
4812 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06004813 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004814 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07004815 if (!list_empty_careful(&ctx->cq_overflow_list))
4816 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004817 if (flags & IORING_ENTER_SQ_WAKEUP)
4818 wake_up(&ctx->sqo_wait);
4819 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06004820 } else if (to_submit) {
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004821 struct mm_struct *cur_mm;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004822
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004823 to_submit = min(to_submit, ctx->sq_entries);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004824 mutex_lock(&ctx->uring_lock);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004825 /* already have mm, so io_submit_sqes() won't try to grab it */
4826 cur_mm = ctx->sqo_mm;
4827 submitted = io_submit_sqes(ctx, to_submit, f.file, fd,
4828 &cur_mm, false);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004829 mutex_unlock(&ctx->uring_lock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004830 }
4831 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07004832 unsigned nr_events = 0;
4833
Jens Axboe2b188cc2019-01-07 10:46:33 -07004834 min_complete = min(min_complete, ctx->cq_entries);
4835
Jens Axboedef596e2019-01-09 08:59:42 -07004836 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07004837 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07004838 } else {
4839 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
4840 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004841 }
4842
Pavel Begunkov6805b322019-10-08 02:18:42 +03004843 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004844out_fput:
4845 fdput(f);
4846 return submitted ? submitted : ret;
4847}
4848
4849static const struct file_operations io_uring_fops = {
4850 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06004851 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07004852 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01004853#ifndef CONFIG_MMU
4854 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
4855 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
4856#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07004857 .poll = io_uring_poll,
4858 .fasync = io_uring_fasync,
4859};
4860
4861static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
4862 struct io_uring_params *p)
4863{
Hristo Venev75b28af2019-08-26 17:23:46 +00004864 struct io_rings *rings;
4865 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004866
Hristo Venev75b28af2019-08-26 17:23:46 +00004867 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
4868 if (size == SIZE_MAX)
4869 return -EOVERFLOW;
4870
4871 rings = io_mem_alloc(size);
4872 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004873 return -ENOMEM;
4874
Hristo Venev75b28af2019-08-26 17:23:46 +00004875 ctx->rings = rings;
4876 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
4877 rings->sq_ring_mask = p->sq_entries - 1;
4878 rings->cq_ring_mask = p->cq_entries - 1;
4879 rings->sq_ring_entries = p->sq_entries;
4880 rings->cq_ring_entries = p->cq_entries;
4881 ctx->sq_mask = rings->sq_ring_mask;
4882 ctx->cq_mask = rings->cq_ring_mask;
4883 ctx->sq_entries = rings->sq_ring_entries;
4884 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004885
4886 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07004887 if (size == SIZE_MAX) {
4888 io_mem_free(ctx->rings);
4889 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004890 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07004891 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004892
4893 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07004894 if (!ctx->sq_sqes) {
4895 io_mem_free(ctx->rings);
4896 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004897 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07004898 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004899
Jens Axboe2b188cc2019-01-07 10:46:33 -07004900 return 0;
4901}
4902
4903/*
4904 * Allocate an anonymous fd, this is what constitutes the application
4905 * visible backing of an io_uring instance. The application mmaps this
4906 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
4907 * we have to tie this fd to a socket for file garbage collection purposes.
4908 */
4909static int io_uring_get_fd(struct io_ring_ctx *ctx)
4910{
4911 struct file *file;
4912 int ret;
4913
4914#if defined(CONFIG_UNIX)
4915 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
4916 &ctx->ring_sock);
4917 if (ret)
4918 return ret;
4919#endif
4920
4921 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
4922 if (ret < 0)
4923 goto err;
4924
4925 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
4926 O_RDWR | O_CLOEXEC);
4927 if (IS_ERR(file)) {
4928 put_unused_fd(ret);
4929 ret = PTR_ERR(file);
4930 goto err;
4931 }
4932
4933#if defined(CONFIG_UNIX)
4934 ctx->ring_sock->file = file;
Jens Axboe6b063142019-01-10 22:13:58 -07004935 ctx->ring_sock->sk->sk_user_data = ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004936#endif
4937 fd_install(ret, file);
4938 return ret;
4939err:
4940#if defined(CONFIG_UNIX)
4941 sock_release(ctx->ring_sock);
4942 ctx->ring_sock = NULL;
4943#endif
4944 return ret;
4945}
4946
4947static int io_uring_create(unsigned entries, struct io_uring_params *p)
4948{
4949 struct user_struct *user = NULL;
4950 struct io_ring_ctx *ctx;
4951 bool account_mem;
4952 int ret;
4953
4954 if (!entries || entries > IORING_MAX_ENTRIES)
4955 return -EINVAL;
4956
4957 /*
4958 * Use twice as many entries for the CQ ring. It's possible for the
4959 * application to drive a higher depth than the size of the SQ ring,
4960 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06004961 * some flexibility in overcommitting a bit. If the application has
4962 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
4963 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07004964 */
4965 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06004966 if (p->flags & IORING_SETUP_CQSIZE) {
4967 /*
4968 * If IORING_SETUP_CQSIZE is set, we do the same roundup
4969 * to a power-of-two, if it isn't already. We do NOT impose
4970 * any cq vs sq ring sizing.
4971 */
4972 if (p->cq_entries < p->sq_entries || p->cq_entries > IORING_MAX_CQ_ENTRIES)
4973 return -EINVAL;
4974 p->cq_entries = roundup_pow_of_two(p->cq_entries);
4975 } else {
4976 p->cq_entries = 2 * p->sq_entries;
4977 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004978
4979 user = get_uid(current_user());
4980 account_mem = !capable(CAP_IPC_LOCK);
4981
4982 if (account_mem) {
4983 ret = io_account_mem(user,
4984 ring_pages(p->sq_entries, p->cq_entries));
4985 if (ret) {
4986 free_uid(user);
4987 return ret;
4988 }
4989 }
4990
4991 ctx = io_ring_ctx_alloc(p);
4992 if (!ctx) {
4993 if (account_mem)
4994 io_unaccount_mem(user, ring_pages(p->sq_entries,
4995 p->cq_entries));
4996 free_uid(user);
4997 return -ENOMEM;
4998 }
4999 ctx->compat = in_compat_syscall();
5000 ctx->account_mem = account_mem;
5001 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07005002 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07005003
5004 ret = io_allocate_scq_urings(ctx, p);
5005 if (ret)
5006 goto err;
5007
Jens Axboe6c271ce2019-01-10 11:22:30 -07005008 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005009 if (ret)
5010 goto err;
5011
Jens Axboe2b188cc2019-01-07 10:46:33 -07005012 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00005013 p->sq_off.head = offsetof(struct io_rings, sq.head);
5014 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
5015 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
5016 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
5017 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
5018 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
5019 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005020
5021 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00005022 p->cq_off.head = offsetof(struct io_rings, cq.head);
5023 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
5024 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
5025 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
5026 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
5027 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06005028
Jens Axboe044c1ab2019-10-28 09:15:33 -06005029 /*
5030 * Install ring fd as the very last thing, so we don't risk someone
5031 * having closed it before we finish setup
5032 */
5033 ret = io_uring_get_fd(ctx);
5034 if (ret < 0)
5035 goto err;
5036
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005037 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02005038 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005039 return ret;
5040err:
5041 io_ring_ctx_wait_and_kill(ctx);
5042 return ret;
5043}
5044
5045/*
5046 * Sets up an aio uring context, and returns the fd. Applications asks for a
5047 * ring size, we return the actual sq/cq ring sizes (among other things) in the
5048 * params structure passed in.
5049 */
5050static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
5051{
5052 struct io_uring_params p;
5053 long ret;
5054 int i;
5055
5056 if (copy_from_user(&p, params, sizeof(p)))
5057 return -EFAULT;
5058 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
5059 if (p.resv[i])
5060 return -EINVAL;
5061 }
5062
Jens Axboe6c271ce2019-01-10 11:22:30 -07005063 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe33a107f2019-10-04 12:10:03 -06005064 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE))
Jens Axboe2b188cc2019-01-07 10:46:33 -07005065 return -EINVAL;
5066
5067 ret = io_uring_create(entries, &p);
5068 if (ret < 0)
5069 return ret;
5070
5071 if (copy_to_user(params, &p, sizeof(p)))
5072 return -EFAULT;
5073
5074 return ret;
5075}
5076
5077SYSCALL_DEFINE2(io_uring_setup, u32, entries,
5078 struct io_uring_params __user *, params)
5079{
5080 return io_uring_setup(entries, params);
5081}
5082
Jens Axboeedafcce2019-01-09 09:16:05 -07005083static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
5084 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06005085 __releases(ctx->uring_lock)
5086 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07005087{
5088 int ret;
5089
Jens Axboe35fa71a2019-04-22 10:23:23 -06005090 /*
5091 * We're inside the ring mutex, if the ref is already dying, then
5092 * someone else killed the ctx or is already going through
5093 * io_uring_register().
5094 */
5095 if (percpu_ref_is_dying(&ctx->refs))
5096 return -ENXIO;
5097
Jens Axboeedafcce2019-01-09 09:16:05 -07005098 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06005099
5100 /*
5101 * Drop uring mutex before waiting for references to exit. If another
5102 * thread is currently inside io_uring_enter() it might need to grab
5103 * the uring_lock to make progress. If we hold it here across the drain
5104 * wait, then we can deadlock. It's safe to drop the mutex here, since
5105 * no new references will come in after we've killed the percpu ref.
5106 */
5107 mutex_unlock(&ctx->uring_lock);
Jens Axboe206aefd2019-11-07 18:27:42 -07005108 wait_for_completion(&ctx->completions[0]);
Jens Axboeb19062a2019-04-15 10:49:38 -06005109 mutex_lock(&ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07005110
5111 switch (opcode) {
5112 case IORING_REGISTER_BUFFERS:
5113 ret = io_sqe_buffer_register(ctx, arg, nr_args);
5114 break;
5115 case IORING_UNREGISTER_BUFFERS:
5116 ret = -EINVAL;
5117 if (arg || nr_args)
5118 break;
5119 ret = io_sqe_buffer_unregister(ctx);
5120 break;
Jens Axboe6b063142019-01-10 22:13:58 -07005121 case IORING_REGISTER_FILES:
5122 ret = io_sqe_files_register(ctx, arg, nr_args);
5123 break;
5124 case IORING_UNREGISTER_FILES:
5125 ret = -EINVAL;
5126 if (arg || nr_args)
5127 break;
5128 ret = io_sqe_files_unregister(ctx);
5129 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06005130 case IORING_REGISTER_FILES_UPDATE:
5131 ret = io_sqe_files_update(ctx, arg, nr_args);
5132 break;
Jens Axboe9b402842019-04-11 11:45:41 -06005133 case IORING_REGISTER_EVENTFD:
5134 ret = -EINVAL;
5135 if (nr_args != 1)
5136 break;
5137 ret = io_eventfd_register(ctx, arg);
5138 break;
5139 case IORING_UNREGISTER_EVENTFD:
5140 ret = -EINVAL;
5141 if (arg || nr_args)
5142 break;
5143 ret = io_eventfd_unregister(ctx);
5144 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07005145 default:
5146 ret = -EINVAL;
5147 break;
5148 }
5149
5150 /* bring the ctx back to life */
Jens Axboe206aefd2019-11-07 18:27:42 -07005151 reinit_completion(&ctx->completions[0]);
Jens Axboeedafcce2019-01-09 09:16:05 -07005152 percpu_ref_reinit(&ctx->refs);
5153 return ret;
5154}
5155
5156SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
5157 void __user *, arg, unsigned int, nr_args)
5158{
5159 struct io_ring_ctx *ctx;
5160 long ret = -EBADF;
5161 struct fd f;
5162
5163 f = fdget(fd);
5164 if (!f.file)
5165 return -EBADF;
5166
5167 ret = -EOPNOTSUPP;
5168 if (f.file->f_op != &io_uring_fops)
5169 goto out_fput;
5170
5171 ctx = f.file->private_data;
5172
5173 mutex_lock(&ctx->uring_lock);
5174 ret = __io_uring_register(ctx, opcode, arg, nr_args);
5175 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02005176 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
5177 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07005178out_fput:
5179 fdput(f);
5180 return ret;
5181}
5182
Jens Axboe2b188cc2019-01-07 10:46:33 -07005183static int __init io_uring_init(void)
5184{
5185 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
5186 return 0;
5187};
5188__initcall(io_uring_init);