blob: 1d6a5083f37f127818c9bfe6b147e7bf61689820 [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
LimingWu0b4295b2019-12-05 20:18:18 +0800148 * there are not more requests pending than there is space in
Stefan Bühler1e84b972019-04-24 23:54:16 +0200149 * 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 Axboe78076bb2019-12-04 19:56:40 -0700278 struct hlist_head *cancel_hash;
279 unsigned cancel_hash_bits;
Jens Axboefcb323c2019-10-24 12:39:47 -0600280
281 spinlock_t inflight_lock;
282 struct list_head inflight_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700283 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700284};
285
Jens Axboe09bb8392019-03-13 12:39:28 -0600286/*
287 * First field must be the file pointer in all the
288 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
289 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700290struct io_poll_iocb {
291 struct file *file;
Jens Axboe0969e782019-12-17 18:40:57 -0700292 union {
293 struct wait_queue_head *head;
294 u64 addr;
295 };
Jens Axboe221c5eb2019-01-17 09:41:58 -0700296 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600297 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700298 bool canceled;
Jens Axboe392edb42019-12-09 17:52:20 -0700299 struct wait_queue_entry wait;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700300};
301
Jens Axboead8a48a2019-11-15 08:49:11 -0700302struct io_timeout_data {
303 struct io_kiocb *req;
304 struct hrtimer timer;
305 struct timespec64 ts;
306 enum hrtimer_mode mode;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +0300307 u32 seq_offset;
Jens Axboead8a48a2019-11-15 08:49:11 -0700308};
309
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700310struct io_accept {
311 struct file *file;
312 struct sockaddr __user *addr;
313 int __user *addr_len;
314 int flags;
315};
316
317struct io_sync {
318 struct file *file;
319 loff_t len;
320 loff_t off;
321 int flags;
322};
323
Jens Axboefbf23842019-12-17 18:45:56 -0700324struct io_cancel {
325 struct file *file;
326 u64 addr;
327};
328
Jens Axboef499a022019-12-02 16:28:46 -0700329struct io_async_connect {
330 struct sockaddr_storage address;
331};
332
Jens Axboe03b12302019-12-02 18:50:25 -0700333struct io_async_msghdr {
334 struct iovec fast_iov[UIO_FASTIOV];
335 struct iovec *iov;
336 struct sockaddr __user *uaddr;
337 struct msghdr msg;
338};
339
Jens Axboef67676d2019-12-02 11:03:47 -0700340struct io_async_rw {
341 struct iovec fast_iov[UIO_FASTIOV];
342 struct iovec *iov;
343 ssize_t nr_segs;
344 ssize_t size;
345};
346
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700347struct io_async_ctx {
348 struct io_uring_sqe sqe;
Jens Axboef67676d2019-12-02 11:03:47 -0700349 union {
350 struct io_async_rw rw;
Jens Axboe03b12302019-12-02 18:50:25 -0700351 struct io_async_msghdr msg;
Jens Axboef499a022019-12-02 16:28:46 -0700352 struct io_async_connect connect;
Jens Axboe2d283902019-12-04 11:08:05 -0700353 struct io_timeout_data timeout;
Jens Axboef67676d2019-12-02 11:03:47 -0700354 };
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700355};
356
Jens Axboe09bb8392019-03-13 12:39:28 -0600357/*
358 * NOTE! Each of the iocb union members has the file pointer
359 * as the first entry in their struct definition. So you can
360 * access the file pointer through any of the sub-structs,
361 * or directly as just 'ki_filp' in this struct.
362 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700363struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700364 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600365 struct file *file;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700366 struct kiocb rw;
367 struct io_poll_iocb poll;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700368 struct io_accept accept;
369 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700370 struct io_cancel cancel;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700371 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700372
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300373 const struct io_uring_sqe *sqe;
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700374 struct io_async_ctx *io;
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300375 struct file *ring_file;
376 int ring_fd;
377 bool has_user;
378 bool in_async;
379 bool needs_fixed_file;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700380
381 struct io_ring_ctx *ctx;
Jens Axboeeac406c2019-11-14 12:09:58 -0700382 union {
383 struct list_head list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700384 struct hlist_node hash_node;
Jens Axboeeac406c2019-11-14 12:09:58 -0700385 };
Jens Axboe9e645e112019-05-10 16:07:28 -0600386 struct list_head link_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700387 unsigned int flags;
Jens Axboec16361c2019-01-17 08:39:48 -0700388 refcount_t refs;
Stefan Bühler8449eed2019-04-27 20:34:19 +0200389#define REQ_F_NOWAIT 1 /* must not punt to workers */
Jens Axboedef596e2019-01-09 08:59:42 -0700390#define REQ_F_IOPOLL_COMPLETED 2 /* polled IO has completed */
Jens Axboe6b063142019-01-10 22:13:58 -0700391#define REQ_F_FIXED_FILE 4 /* ctx owns file */
Jens Axboe4d7dd462019-11-20 13:03:52 -0700392#define REQ_F_LINK_NEXT 8 /* already grabbed next link */
Stefan Bühlere2033e32019-05-11 19:08:01 +0200393#define REQ_F_IO_DRAIN 16 /* drain existing IO first */
394#define REQ_F_IO_DRAINED 32 /* drain done */
Jens Axboe9e645e112019-05-10 16:07:28 -0600395#define REQ_F_LINK 64 /* linked sqes */
Jens Axboe2665abf2019-11-05 12:40:47 -0700396#define REQ_F_LINK_TIMEOUT 128 /* has linked timeout */
Zhengyuan Liuf7b76ac2019-07-16 23:26:14 +0800397#define REQ_F_FAIL_LINK 256 /* fail rest of links */
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +0300398#define REQ_F_DRAIN_LINK 512 /* link should be fully drained */
Jens Axboe5262f562019-09-17 12:26:57 -0600399#define REQ_F_TIMEOUT 1024 /* timeout request */
Jens Axboe491381ce2019-10-17 09:20:46 -0600400#define REQ_F_ISREG 2048 /* regular file */
401#define REQ_F_MUST_PUNT 4096 /* must be punted even for NONBLOCK */
Jens Axboe93bd25b2019-11-11 23:34:31 -0700402#define REQ_F_TIMEOUT_NOSEQ 8192 /* no timeout sequence */
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800403#define REQ_F_INFLIGHT 16384 /* on inflight list */
404#define REQ_F_COMP_LOCKED 32768 /* completion under lock */
Jens Axboe4e88d6e2019-12-07 20:59:47 -0700405#define REQ_F_HARDLINK 65536 /* doesn't sever on completion < 0 */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700406#define REQ_F_PREPPED 131072 /* request already opcode prepared */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700407 u64 user_data;
Jens Axboe9e645e112019-05-10 16:07:28 -0600408 u32 result;
Jens Axboede0617e2019-04-06 21:51:27 -0600409 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700410
Jens Axboefcb323c2019-10-24 12:39:47 -0600411 struct list_head inflight_entry;
412
Jens Axboe561fb042019-10-24 07:25:42 -0600413 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700414};
415
416#define IO_PLUG_THRESHOLD 2
Jens Axboedef596e2019-01-09 08:59:42 -0700417#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700418
Jens Axboe9a56a232019-01-09 09:06:50 -0700419struct io_submit_state {
420 struct blk_plug plug;
421
422 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700423 * io_kiocb alloc cache
424 */
425 void *reqs[IO_IOPOLL_BATCH];
426 unsigned int free_reqs;
427 unsigned int cur_req;
428
429 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700430 * File reference cache
431 */
432 struct file *file;
433 unsigned int fd;
434 unsigned int has_refs;
435 unsigned int used_refs;
436 unsigned int ios_left;
437};
438
Jens Axboe561fb042019-10-24 07:25:42 -0600439static void io_wq_submit_work(struct io_wq_work **workptr);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700440static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liu4fe2c962019-09-09 20:50:40 +0800441static void __io_free_req(struct io_kiocb *req);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800442static void io_put_req(struct io_kiocb *req);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700443static void io_double_put_req(struct io_kiocb *req);
Jens Axboe978db572019-11-14 22:39:04 -0700444static void __io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700445static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
446static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboede0617e2019-04-06 21:51:27 -0600447
Jens Axboe2b188cc2019-01-07 10:46:33 -0700448static struct kmem_cache *req_cachep;
449
450static const struct file_operations io_uring_fops;
451
452struct sock *io_uring_get_socket(struct file *file)
453{
454#if defined(CONFIG_UNIX)
455 if (file->f_op == &io_uring_fops) {
456 struct io_ring_ctx *ctx = file->private_data;
457
458 return ctx->ring_sock->sk;
459 }
460#endif
461 return NULL;
462}
463EXPORT_SYMBOL(io_uring_get_socket);
464
465static void io_ring_ctx_ref_free(struct percpu_ref *ref)
466{
467 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
468
Jens Axboe206aefd2019-11-07 18:27:42 -0700469 complete(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700470}
471
472static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
473{
474 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -0700475 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700476
477 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
478 if (!ctx)
479 return NULL;
480
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700481 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
482 if (!ctx->fallback_req)
483 goto err;
484
Jens Axboe206aefd2019-11-07 18:27:42 -0700485 ctx->completions = kmalloc(2 * sizeof(struct completion), GFP_KERNEL);
486 if (!ctx->completions)
487 goto err;
488
Jens Axboe78076bb2019-12-04 19:56:40 -0700489 /*
490 * Use 5 bits less than the max cq entries, that should give us around
491 * 32 entries per hash list if totally full and uniformly spread.
492 */
493 hash_bits = ilog2(p->cq_entries);
494 hash_bits -= 5;
495 if (hash_bits <= 0)
496 hash_bits = 1;
497 ctx->cancel_hash_bits = hash_bits;
498 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
499 GFP_KERNEL);
500 if (!ctx->cancel_hash)
501 goto err;
502 __hash_init(ctx->cancel_hash, 1U << hash_bits);
503
Roman Gushchin21482892019-05-07 10:01:48 -0700504 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -0700505 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
506 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700507
508 ctx->flags = p->flags;
509 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700510 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe206aefd2019-11-07 18:27:42 -0700511 init_completion(&ctx->completions[0]);
512 init_completion(&ctx->completions[1]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700513 mutex_init(&ctx->uring_lock);
514 init_waitqueue_head(&ctx->wait);
515 spin_lock_init(&ctx->completion_lock);
Jens Axboedef596e2019-01-09 08:59:42 -0700516 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboede0617e2019-04-06 21:51:27 -0600517 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -0600518 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -0600519 init_waitqueue_head(&ctx->inflight_wait);
520 spin_lock_init(&ctx->inflight_lock);
521 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700522 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -0700523err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700524 if (ctx->fallback_req)
525 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe206aefd2019-11-07 18:27:42 -0700526 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -0700527 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -0700528 kfree(ctx);
529 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700530}
531
Bob Liu9d858b22019-11-13 18:06:25 +0800532static inline bool __req_need_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -0600533{
Jackie Liua197f662019-11-08 08:09:12 -0700534 struct io_ring_ctx *ctx = req->ctx;
535
Jens Axboe498ccd92019-10-25 10:04:25 -0600536 return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped
537 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboede0617e2019-04-06 21:51:27 -0600538}
539
Bob Liu9d858b22019-11-13 18:06:25 +0800540static inline bool req_need_defer(struct io_kiocb *req)
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600541{
Bob Liu9d858b22019-11-13 18:06:25 +0800542 if ((req->flags & (REQ_F_IO_DRAIN|REQ_F_IO_DRAINED)) == REQ_F_IO_DRAIN)
543 return __req_need_defer(req);
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600544
Bob Liu9d858b22019-11-13 18:06:25 +0800545 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600546}
547
548static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -0600549{
550 struct io_kiocb *req;
551
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600552 req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list);
Bob Liu9d858b22019-11-13 18:06:25 +0800553 if (req && !req_need_defer(req)) {
Jens Axboede0617e2019-04-06 21:51:27 -0600554 list_del_init(&req->list);
555 return req;
556 }
557
558 return NULL;
559}
560
Jens Axboe5262f562019-09-17 12:26:57 -0600561static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx)
562{
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600563 struct io_kiocb *req;
564
565 req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list);
Jens Axboe93bd25b2019-11-11 23:34:31 -0700566 if (req) {
567 if (req->flags & REQ_F_TIMEOUT_NOSEQ)
568 return NULL;
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800569 if (!__req_need_defer(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -0700570 list_del_init(&req->list);
571 return req;
572 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600573 }
574
575 return NULL;
Jens Axboe5262f562019-09-17 12:26:57 -0600576}
577
Jens Axboede0617e2019-04-06 21:51:27 -0600578static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700579{
Hristo Venev75b28af2019-08-26 17:23:46 +0000580 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700581
Hristo Venev75b28af2019-08-26 17:23:46 +0000582 if (ctx->cached_cq_tail != READ_ONCE(rings->cq.tail)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700583 /* order cqe stores with ring update */
Hristo Venev75b28af2019-08-26 17:23:46 +0000584 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700585
Jens Axboe2b188cc2019-01-07 10:46:33 -0700586 if (wq_has_sleeper(&ctx->cq_wait)) {
587 wake_up_interruptible(&ctx->cq_wait);
588 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
589 }
590 }
591}
592
Jens Axboe561fb042019-10-24 07:25:42 -0600593static inline bool io_sqe_needs_user(const struct io_uring_sqe *sqe)
Jens Axboe18d9be12019-09-10 09:13:05 -0600594{
Jens Axboe561fb042019-10-24 07:25:42 -0600595 u8 opcode = READ_ONCE(sqe->opcode);
596
597 return !(opcode == IORING_OP_READ_FIXED ||
598 opcode == IORING_OP_WRITE_FIXED);
599}
600
Jens Axboe94ae5e72019-11-14 19:39:52 -0700601static inline bool io_prep_async_work(struct io_kiocb *req,
602 struct io_kiocb **link)
Jens Axboe561fb042019-10-24 07:25:42 -0600603{
604 bool do_hashed = false;
Jens Axboe54a91f32019-09-10 09:15:04 -0600605
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300606 if (req->sqe) {
607 switch (req->sqe->opcode) {
Jens Axboe6cc47d12019-09-18 11:18:23 -0600608 case IORING_OP_WRITEV:
609 case IORING_OP_WRITE_FIXED:
Jens Axboe53108d42019-12-09 20:12:38 -0700610 /* only regular files should be hashed for writes */
611 if (req->flags & REQ_F_ISREG)
612 do_hashed = true;
Jens Axboe5f8fd2d32019-11-07 10:57:36 -0700613 /* fall-through */
614 case IORING_OP_READV:
615 case IORING_OP_READ_FIXED:
616 case IORING_OP_SENDMSG:
617 case IORING_OP_RECVMSG:
618 case IORING_OP_ACCEPT:
619 case IORING_OP_POLL_ADD:
Jens Axboef8e85cf2019-11-23 14:24:24 -0700620 case IORING_OP_CONNECT:
Jens Axboe5f8fd2d32019-11-07 10:57:36 -0700621 /*
622 * We know REQ_F_ISREG is not set on some of these
623 * opcodes, but this enables us to keep the check in
624 * just one place.
625 */
626 if (!(req->flags & REQ_F_ISREG))
627 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe6cc47d12019-09-18 11:18:23 -0600628 break;
629 }
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300630 if (io_sqe_needs_user(req->sqe))
Jens Axboe561fb042019-10-24 07:25:42 -0600631 req->work.flags |= IO_WQ_WORK_NEEDS_USER;
Jens Axboe54a91f32019-09-10 09:15:04 -0600632 }
633
Jens Axboe94ae5e72019-11-14 19:39:52 -0700634 *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -0600635 return do_hashed;
636}
637
Jackie Liua197f662019-11-08 08:09:12 -0700638static inline void io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -0600639{
Jackie Liua197f662019-11-08 08:09:12 -0700640 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe94ae5e72019-11-14 19:39:52 -0700641 struct io_kiocb *link;
642 bool do_hashed;
643
644 do_hashed = io_prep_async_work(req, &link);
Jens Axboe561fb042019-10-24 07:25:42 -0600645
646 trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work,
647 req->flags);
648 if (!do_hashed) {
649 io_wq_enqueue(ctx->io_wq, &req->work);
650 } else {
651 io_wq_enqueue_hashed(ctx->io_wq, &req->work,
652 file_inode(req->file));
653 }
Jens Axboe94ae5e72019-11-14 19:39:52 -0700654
655 if (link)
656 io_queue_linked_timeout(link);
Jens Axboe18d9be12019-09-10 09:13:05 -0600657}
658
Jens Axboe5262f562019-09-17 12:26:57 -0600659static void io_kill_timeout(struct io_kiocb *req)
660{
661 int ret;
662
Jens Axboe2d283902019-12-04 11:08:05 -0700663 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe5262f562019-09-17 12:26:57 -0600664 if (ret != -1) {
665 atomic_inc(&req->ctx->cq_timeouts);
Jens Axboe842f9612019-10-29 12:34:10 -0600666 list_del_init(&req->list);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700667 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800668 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -0600669 }
670}
671
672static void io_kill_timeouts(struct io_ring_ctx *ctx)
673{
674 struct io_kiocb *req, *tmp;
675
676 spin_lock_irq(&ctx->completion_lock);
677 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
678 io_kill_timeout(req);
679 spin_unlock_irq(&ctx->completion_lock);
680}
681
Jens Axboede0617e2019-04-06 21:51:27 -0600682static void io_commit_cqring(struct io_ring_ctx *ctx)
683{
684 struct io_kiocb *req;
685
Jens Axboe5262f562019-09-17 12:26:57 -0600686 while ((req = io_get_timeout_req(ctx)) != NULL)
687 io_kill_timeout(req);
688
Jens Axboede0617e2019-04-06 21:51:27 -0600689 __io_commit_cqring(ctx);
690
691 while ((req = io_get_deferred_req(ctx)) != NULL) {
692 req->flags |= REQ_F_IO_DRAINED;
Jackie Liua197f662019-11-08 08:09:12 -0700693 io_queue_async_work(req);
Jens Axboede0617e2019-04-06 21:51:27 -0600694 }
695}
696
Jens Axboe2b188cc2019-01-07 10:46:33 -0700697static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
698{
Hristo Venev75b28af2019-08-26 17:23:46 +0000699 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700700 unsigned tail;
701
702 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +0200703 /*
704 * writes to the cq entry need to come after reading head; the
705 * control dependency is enough as we're using WRITE_ONCE to
706 * fill the cq entry
707 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000708 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700709 return NULL;
710
711 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +0000712 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -0700713}
714
Jens Axboe8c838782019-03-12 15:48:16 -0600715static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
716{
717 if (waitqueue_active(&ctx->wait))
718 wake_up(&ctx->wait);
719 if (waitqueue_active(&ctx->sqo_wait))
720 wake_up(&ctx->sqo_wait);
Jens Axboe9b402842019-04-11 11:45:41 -0600721 if (ctx->cq_ev_fd)
722 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -0600723}
724
Jens Axboec4a2ed72019-11-21 21:01:26 -0700725/* Returns true if there are no backlogged entries after the flush */
726static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700727{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700728 struct io_rings *rings = ctx->rings;
729 struct io_uring_cqe *cqe;
730 struct io_kiocb *req;
731 unsigned long flags;
732 LIST_HEAD(list);
733
734 if (!force) {
735 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -0700736 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700737 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
738 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -0700739 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700740 }
741
742 spin_lock_irqsave(&ctx->completion_lock, flags);
743
744 /* if force is set, the ring is going away. always drop after that */
745 if (force)
746 ctx->cq_overflow_flushed = true;
747
Jens Axboec4a2ed72019-11-21 21:01:26 -0700748 cqe = NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700749 while (!list_empty(&ctx->cq_overflow_list)) {
750 cqe = io_get_cqring(ctx);
751 if (!cqe && !force)
752 break;
753
754 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
755 list);
756 list_move(&req->list, &list);
757 if (cqe) {
758 WRITE_ONCE(cqe->user_data, req->user_data);
759 WRITE_ONCE(cqe->res, req->result);
760 WRITE_ONCE(cqe->flags, 0);
761 } else {
762 WRITE_ONCE(ctx->rings->cq_overflow,
763 atomic_inc_return(&ctx->cached_cq_overflow));
764 }
765 }
766
767 io_commit_cqring(ctx);
768 spin_unlock_irqrestore(&ctx->completion_lock, flags);
769 io_cqring_ev_posted(ctx);
770
771 while (!list_empty(&list)) {
772 req = list_first_entry(&list, struct io_kiocb, list);
773 list_del(&req->list);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800774 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700775 }
Jens Axboec4a2ed72019-11-21 21:01:26 -0700776
777 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700778}
779
Jens Axboe78e19bb2019-11-06 15:21:34 -0700780static void io_cqring_fill_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700781{
Jens Axboe78e19bb2019-11-06 15:21:34 -0700782 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700783 struct io_uring_cqe *cqe;
784
Jens Axboe78e19bb2019-11-06 15:21:34 -0700785 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -0700786
Jens Axboe2b188cc2019-01-07 10:46:33 -0700787 /*
788 * If we can't get a cq entry, userspace overflowed the
789 * submission (by quite a lot). Increment the overflow count in
790 * the ring.
791 */
792 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700793 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -0700794 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700795 WRITE_ONCE(cqe->res, res);
796 WRITE_ONCE(cqe->flags, 0);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700797 } else if (ctx->cq_overflow_flushed) {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700798 WRITE_ONCE(ctx->rings->cq_overflow,
799 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700800 } else {
801 refcount_inc(&req->refs);
802 req->result = res;
803 list_add_tail(&req->list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700804 }
805}
806
Jens Axboe78e19bb2019-11-06 15:21:34 -0700807static void io_cqring_add_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700808{
Jens Axboe78e19bb2019-11-06 15:21:34 -0700809 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700810 unsigned long flags;
811
812 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700813 io_cqring_fill_event(req, res);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700814 io_commit_cqring(ctx);
815 spin_unlock_irqrestore(&ctx->completion_lock, flags);
816
Jens Axboe8c838782019-03-12 15:48:16 -0600817 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700818}
819
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700820static inline bool io_is_fallback_req(struct io_kiocb *req)
821{
822 return req == (struct io_kiocb *)
823 ((unsigned long) req->ctx->fallback_req & ~1UL);
824}
825
826static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
827{
828 struct io_kiocb *req;
829
830 req = ctx->fallback_req;
831 if (!test_and_set_bit_lock(0, (unsigned long *) ctx->fallback_req))
832 return req;
833
834 return NULL;
835}
836
Jens Axboe2579f912019-01-09 09:10:43 -0700837static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
838 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700839{
Jens Axboefd6fab22019-03-14 16:30:06 -0600840 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700841 struct io_kiocb *req;
842
843 if (!percpu_ref_tryget(&ctx->refs))
844 return NULL;
845
Jens Axboe2579f912019-01-09 09:10:43 -0700846 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -0600847 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -0700848 if (unlikely(!req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700849 goto fallback;
Jens Axboe2579f912019-01-09 09:10:43 -0700850 } else if (!state->free_reqs) {
851 size_t sz;
852 int ret;
853
854 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -0600855 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
856
857 /*
858 * Bulk alloc is all-or-nothing. If we fail to get a batch,
859 * retry single alloc to be on the safe side.
860 */
861 if (unlikely(ret <= 0)) {
862 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
863 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700864 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -0600865 ret = 1;
866 }
Jens Axboe2579f912019-01-09 09:10:43 -0700867 state->free_reqs = ret - 1;
868 state->cur_req = 1;
869 req = state->reqs[0];
870 } else {
871 req = state->reqs[state->cur_req];
872 state->free_reqs--;
873 state->cur_req++;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700874 }
875
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700876got_it:
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700877 req->io = NULL;
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300878 req->ring_file = NULL;
Jens Axboe60c112b2019-06-21 10:20:18 -0600879 req->file = NULL;
Jens Axboe2579f912019-01-09 09:10:43 -0700880 req->ctx = ctx;
881 req->flags = 0;
Jens Axboee65ef562019-03-12 10:16:44 -0600882 /* one is dropped after submission, the other at completion */
883 refcount_set(&req->refs, 2);
Jens Axboe9e645e112019-05-10 16:07:28 -0600884 req->result = 0;
Jens Axboe561fb042019-10-24 07:25:42 -0600885 INIT_IO_WORK(&req->work, io_wq_submit_work);
Jens Axboe2579f912019-01-09 09:10:43 -0700886 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700887fallback:
888 req = io_get_fallback_req(ctx);
889 if (req)
890 goto got_it;
Pavel Begunkov6805b322019-10-08 02:18:42 +0300891 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700892 return NULL;
893}
894
Jens Axboedef596e2019-01-09 08:59:42 -0700895static void io_free_req_many(struct io_ring_ctx *ctx, void **reqs, int *nr)
896{
897 if (*nr) {
898 kmem_cache_free_bulk(req_cachep, *nr, reqs);
Pavel Begunkov6805b322019-10-08 02:18:42 +0300899 percpu_ref_put_many(&ctx->refs, *nr);
Jens Axboedef596e2019-01-09 08:59:42 -0700900 *nr = 0;
901 }
902}
903
Jens Axboe9e645e112019-05-10 16:07:28 -0600904static void __io_free_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700905{
Jens Axboefcb323c2019-10-24 12:39:47 -0600906 struct io_ring_ctx *ctx = req->ctx;
907
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700908 if (req->io)
909 kfree(req->io);
Jens Axboe09bb8392019-03-13 12:39:28 -0600910 if (req->file && !(req->flags & REQ_F_FIXED_FILE))
911 fput(req->file);
Jens Axboefcb323c2019-10-24 12:39:47 -0600912 if (req->flags & REQ_F_INFLIGHT) {
913 unsigned long flags;
914
915 spin_lock_irqsave(&ctx->inflight_lock, flags);
916 list_del(&req->inflight_entry);
917 if (waitqueue_active(&ctx->inflight_wait))
918 wake_up(&ctx->inflight_wait);
919 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
920 }
921 percpu_ref_put(&ctx->refs);
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700922 if (likely(!io_is_fallback_req(req)))
923 kmem_cache_free(req_cachep, req);
924 else
925 clear_bit_unlock(0, (unsigned long *) ctx->fallback_req);
Jens Axboee65ef562019-03-12 10:16:44 -0600926}
927
Jackie Liua197f662019-11-08 08:09:12 -0700928static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -0600929{
Jackie Liua197f662019-11-08 08:09:12 -0700930 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -0700931 int ret;
932
Jens Axboe2d283902019-12-04 11:08:05 -0700933 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe2665abf2019-11-05 12:40:47 -0700934 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -0700935 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -0700936 io_commit_cqring(ctx);
937 req->flags &= ~REQ_F_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +0800938 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -0700939 return true;
940 }
941
942 return false;
943}
944
Jens Axboeba816ad2019-09-28 11:36:45 -0600945static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -0600946{
Jens Axboe2665abf2019-11-05 12:40:47 -0700947 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -0700948 bool wake_ev = false;
Jens Axboe9e645e112019-05-10 16:07:28 -0600949
Jens Axboe4d7dd462019-11-20 13:03:52 -0700950 /* Already got next link */
951 if (req->flags & REQ_F_LINK_NEXT)
952 return;
953
Jens Axboe9e645e112019-05-10 16:07:28 -0600954 /*
955 * The list should never be empty when we are called here. But could
956 * potentially happen if the chain is messed up, check to be on the
957 * safe side.
958 */
Pavel Begunkov44932332019-12-05 16:16:35 +0300959 while (!list_empty(&req->link_list)) {
960 struct io_kiocb *nxt = list_first_entry(&req->link_list,
961 struct io_kiocb, link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700962
Pavel Begunkov44932332019-12-05 16:16:35 +0300963 if (unlikely((req->flags & REQ_F_LINK_TIMEOUT) &&
964 (nxt->flags & REQ_F_TIMEOUT))) {
965 list_del_init(&nxt->link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700966 wake_ev |= io_link_cancel_timeout(nxt);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700967 req->flags &= ~REQ_F_LINK_TIMEOUT;
968 continue;
969 }
Jens Axboe9e645e112019-05-10 16:07:28 -0600970
Pavel Begunkov44932332019-12-05 16:16:35 +0300971 list_del_init(&req->link_list);
972 if (!list_empty(&nxt->link_list))
973 nxt->flags |= REQ_F_LINK;
Pavel Begunkovb18fdf72019-11-21 23:21:02 +0300974 *nxtptr = nxt;
Jens Axboe94ae5e72019-11-14 19:39:52 -0700975 break;
Jens Axboe9e645e112019-05-10 16:07:28 -0600976 }
Jens Axboe2665abf2019-11-05 12:40:47 -0700977
Jens Axboe4d7dd462019-11-20 13:03:52 -0700978 req->flags |= REQ_F_LINK_NEXT;
Jens Axboe2665abf2019-11-05 12:40:47 -0700979 if (wake_ev)
980 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -0600981}
982
983/*
984 * Called if REQ_F_LINK is set, and we fail the head request
985 */
986static void io_fail_links(struct io_kiocb *req)
987{
Jens Axboe2665abf2019-11-05 12:40:47 -0700988 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -0700989 unsigned long flags;
990
991 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -0600992
993 while (!list_empty(&req->link_list)) {
Pavel Begunkov44932332019-12-05 16:16:35 +0300994 struct io_kiocb *link = list_first_entry(&req->link_list,
995 struct io_kiocb, link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -0600996
Pavel Begunkov44932332019-12-05 16:16:35 +0300997 list_del_init(&link->link_list);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +0200998 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -0700999
1000 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001001 link->sqe->opcode == IORING_OP_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -07001002 io_link_cancel_timeout(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001003 } else {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001004 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe978db572019-11-14 22:39:04 -07001005 __io_double_put_req(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001006 }
Jens Axboe5d960722019-11-19 15:31:28 -07001007 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -06001008 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001009
1010 io_commit_cqring(ctx);
1011 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1012 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001013}
1014
Jens Axboe4d7dd462019-11-20 13:03:52 -07001015static void io_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -06001016{
Jens Axboe4d7dd462019-11-20 13:03:52 -07001017 if (likely(!(req->flags & REQ_F_LINK)))
Jens Axboe2665abf2019-11-05 12:40:47 -07001018 return;
Jens Axboe2665abf2019-11-05 12:40:47 -07001019
Jens Axboe9e645e112019-05-10 16:07:28 -06001020 /*
1021 * If LINK is set, we have dependent requests in this chain. If we
1022 * didn't fail this request, queue the first one up, moving any other
1023 * dependencies to the next request. In case of failure, fail the rest
1024 * of the chain.
1025 */
Jens Axboe2665abf2019-11-05 12:40:47 -07001026 if (req->flags & REQ_F_FAIL_LINK) {
1027 io_fail_links(req);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001028 } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) ==
1029 REQ_F_LINK_TIMEOUT) {
Jens Axboe2665abf2019-11-05 12:40:47 -07001030 struct io_ring_ctx *ctx = req->ctx;
1031 unsigned long flags;
1032
1033 /*
1034 * If this is a timeout link, we could be racing with the
1035 * timeout timer. Grab the completion lock for this case to
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001036 * protect against that.
Jens Axboe2665abf2019-11-05 12:40:47 -07001037 */
1038 spin_lock_irqsave(&ctx->completion_lock, flags);
1039 io_req_link_next(req, nxt);
1040 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1041 } else {
1042 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -06001043 }
Jens Axboe4d7dd462019-11-20 13:03:52 -07001044}
Jens Axboe9e645e112019-05-10 16:07:28 -06001045
Jackie Liuc69f8db2019-11-09 11:00:08 +08001046static void io_free_req(struct io_kiocb *req)
1047{
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001048 struct io_kiocb *nxt = NULL;
1049
1050 io_req_find_next(req, &nxt);
Pavel Begunkov70cf9f32019-11-21 23:21:00 +03001051 __io_free_req(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001052
1053 if (nxt)
1054 io_queue_async_work(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08001055}
1056
Jens Axboeba816ad2019-09-28 11:36:45 -06001057/*
1058 * Drop reference to request, return next in chain (if there is one) if this
1059 * was the last reference to this request.
1060 */
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001061__attribute__((nonnull))
Jackie Liuec9c02a2019-11-08 23:50:36 +08001062static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboee65ef562019-03-12 10:16:44 -06001063{
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001064 io_req_find_next(req, nxtptr);
Jens Axboe4d7dd462019-11-20 13:03:52 -07001065
Jens Axboee65ef562019-03-12 10:16:44 -06001066 if (refcount_dec_and_test(&req->refs))
Jens Axboe4d7dd462019-11-20 13:03:52 -07001067 __io_free_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001068}
1069
Jens Axboe2b188cc2019-01-07 10:46:33 -07001070static void io_put_req(struct io_kiocb *req)
1071{
Jens Axboedef596e2019-01-09 08:59:42 -07001072 if (refcount_dec_and_test(&req->refs))
1073 io_free_req(req);
1074}
1075
Jens Axboe978db572019-11-14 22:39:04 -07001076/*
1077 * Must only be used if we don't need to care about links, usually from
1078 * within the completion handling itself.
1079 */
1080static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06001081{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001082 /* drop both submit and complete references */
1083 if (refcount_sub_and_test(2, &req->refs))
1084 __io_free_req(req);
1085}
1086
Jens Axboe978db572019-11-14 22:39:04 -07001087static void io_double_put_req(struct io_kiocb *req)
1088{
1089 /* drop both submit and complete references */
1090 if (refcount_sub_and_test(2, &req->refs))
1091 io_free_req(req);
1092}
1093
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001094static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06001095{
Jens Axboe84f97dc2019-11-06 11:27:53 -07001096 struct io_rings *rings = ctx->rings;
1097
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001098 /*
1099 * noflush == true is from the waitqueue handler, just ensure we wake
1100 * up the task, and the next invocation will flush the entries. We
1101 * cannot safely to it from here.
1102 */
1103 if (noflush && !list_empty(&ctx->cq_overflow_list))
1104 return -1U;
1105
1106 io_cqring_overflow_flush(ctx, false);
1107
Jens Axboea3a0e432019-08-20 11:03:11 -06001108 /* See comment at the top of this file */
1109 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00001110 return READ_ONCE(rings->cq.tail) - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06001111}
1112
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03001113static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1114{
1115 struct io_rings *rings = ctx->rings;
1116
1117 /* make sure SQ entry isn't read before tail */
1118 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1119}
1120
Jens Axboedef596e2019-01-09 08:59:42 -07001121/*
1122 * Find and free completed poll iocbs
1123 */
1124static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
1125 struct list_head *done)
1126{
1127 void *reqs[IO_IOPOLL_BATCH];
1128 struct io_kiocb *req;
Jens Axboe09bb8392019-03-13 12:39:28 -06001129 int to_free;
Jens Axboedef596e2019-01-09 08:59:42 -07001130
Jens Axboe09bb8392019-03-13 12:39:28 -06001131 to_free = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001132 while (!list_empty(done)) {
1133 req = list_first_entry(done, struct io_kiocb, list);
1134 list_del(&req->list);
1135
Jens Axboe78e19bb2019-11-06 15:21:34 -07001136 io_cqring_fill_event(req, req->result);
Jens Axboedef596e2019-01-09 08:59:42 -07001137 (*nr_events)++;
1138
Jens Axboe09bb8392019-03-13 12:39:28 -06001139 if (refcount_dec_and_test(&req->refs)) {
1140 /* If we're not using fixed files, we have to pair the
1141 * completion part with the file put. Use regular
1142 * completions for those, only batch free for fixed
Jens Axboe9e645e112019-05-10 16:07:28 -06001143 * file and non-linked commands.
Jens Axboe09bb8392019-03-13 12:39:28 -06001144 */
Jens Axboe1a6b74f2019-12-02 10:33:15 -07001145 if (((req->flags & (REQ_F_FIXED_FILE|REQ_F_LINK)) ==
1146 REQ_F_FIXED_FILE) && !io_is_fallback_req(req) &&
1147 !req->io) {
Jens Axboe09bb8392019-03-13 12:39:28 -06001148 reqs[to_free++] = req;
1149 if (to_free == ARRAY_SIZE(reqs))
1150 io_free_req_many(ctx, reqs, &to_free);
Jens Axboe6b063142019-01-10 22:13:58 -07001151 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001152 io_free_req(req);
Jens Axboe6b063142019-01-10 22:13:58 -07001153 }
Jens Axboe9a56a232019-01-09 09:06:50 -07001154 }
Jens Axboedef596e2019-01-09 08:59:42 -07001155 }
Jens Axboedef596e2019-01-09 08:59:42 -07001156
Jens Axboe09bb8392019-03-13 12:39:28 -06001157 io_commit_cqring(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07001158 io_free_req_many(ctx, reqs, &to_free);
1159}
1160
1161static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1162 long min)
1163{
1164 struct io_kiocb *req, *tmp;
1165 LIST_HEAD(done);
1166 bool spin;
1167 int ret;
1168
1169 /*
1170 * Only spin for completions if we don't have multiple devices hanging
1171 * off our complete list, and we're under the requested amount.
1172 */
1173 spin = !ctx->poll_multi_file && *nr_events < min;
1174
1175 ret = 0;
1176 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
1177 struct kiocb *kiocb = &req->rw;
1178
1179 /*
1180 * Move completed entries to our local list. If we find a
1181 * request that requires polling, break out and complete
1182 * the done list first, if we have entries there.
1183 */
1184 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
1185 list_move_tail(&req->list, &done);
1186 continue;
1187 }
1188 if (!list_empty(&done))
1189 break;
1190
1191 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1192 if (ret < 0)
1193 break;
1194
1195 if (ret && spin)
1196 spin = false;
1197 ret = 0;
1198 }
1199
1200 if (!list_empty(&done))
1201 io_iopoll_complete(ctx, nr_events, &done);
1202
1203 return ret;
1204}
1205
1206/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08001207 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07001208 * non-spinning poll check - we'll still enter the driver poll loop, but only
1209 * as a non-spinning completion check.
1210 */
1211static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
1212 long min)
1213{
Jens Axboe08f54392019-08-21 22:19:11 -06001214 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07001215 int ret;
1216
1217 ret = io_do_iopoll(ctx, nr_events, min);
1218 if (ret < 0)
1219 return ret;
1220 if (!min || *nr_events >= min)
1221 return 0;
1222 }
1223
1224 return 1;
1225}
1226
1227/*
1228 * We can't just wait for polled events to come to us, we have to actively
1229 * find and complete them.
1230 */
1231static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
1232{
1233 if (!(ctx->flags & IORING_SETUP_IOPOLL))
1234 return;
1235
1236 mutex_lock(&ctx->uring_lock);
1237 while (!list_empty(&ctx->poll_list)) {
1238 unsigned int nr_events = 0;
1239
1240 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -06001241
1242 /*
1243 * Ensure we allow local-to-the-cpu processing to take place,
1244 * in this case we need to ensure that we reap all events.
1245 */
1246 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -07001247 }
1248 mutex_unlock(&ctx->uring_lock);
1249}
1250
Jens Axboe2b2ed972019-10-25 10:06:15 -06001251static int __io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1252 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07001253{
Jens Axboe2b2ed972019-10-25 10:06:15 -06001254 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001255
1256 do {
1257 int tmin = 0;
1258
Jens Axboe500f9fb2019-08-19 12:15:59 -06001259 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06001260 * Don't enter poll loop if we already have events pending.
1261 * If we do, we can potentially be spinning for commands that
1262 * already triggered a CQE (eg in error).
1263 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001264 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06001265 break;
1266
1267 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06001268 * If a submit got punted to a workqueue, we can have the
1269 * application entering polling for a command before it gets
1270 * issued. That app will hold the uring_lock for the duration
1271 * of the poll right here, so we need to take a breather every
1272 * now and then to ensure that the issue has a chance to add
1273 * the poll to the issued list. Otherwise we can spin here
1274 * forever, while the workqueue is stuck trying to acquire the
1275 * very same mutex.
1276 */
1277 if (!(++iters & 7)) {
1278 mutex_unlock(&ctx->uring_lock);
1279 mutex_lock(&ctx->uring_lock);
1280 }
1281
Jens Axboedef596e2019-01-09 08:59:42 -07001282 if (*nr_events < min)
1283 tmin = min - *nr_events;
1284
1285 ret = io_iopoll_getevents(ctx, nr_events, tmin);
1286 if (ret <= 0)
1287 break;
1288 ret = 0;
1289 } while (min && !*nr_events && !need_resched());
1290
Jens Axboe2b2ed972019-10-25 10:06:15 -06001291 return ret;
1292}
1293
1294static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1295 long min)
1296{
1297 int ret;
1298
1299 /*
1300 * We disallow the app entering submit/complete with polling, but we
1301 * still need to lock the ring to prevent racing with polled issue
1302 * that got punted to a workqueue.
1303 */
1304 mutex_lock(&ctx->uring_lock);
1305 ret = __io_iopoll_check(ctx, nr_events, min);
Jens Axboe500f9fb2019-08-19 12:15:59 -06001306 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001307 return ret;
1308}
1309
Jens Axboe491381ce2019-10-17 09:20:46 -06001310static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001311{
Jens Axboe491381ce2019-10-17 09:20:46 -06001312 /*
1313 * Tell lockdep we inherited freeze protection from submission
1314 * thread.
1315 */
1316 if (req->flags & REQ_F_ISREG) {
1317 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001318
Jens Axboe491381ce2019-10-17 09:20:46 -06001319 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001320 }
Jens Axboe491381ce2019-10-17 09:20:46 -06001321 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001322}
1323
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001324static inline void req_set_fail_links(struct io_kiocb *req)
1325{
1326 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1327 req->flags |= REQ_F_FAIL_LINK;
1328}
1329
Jens Axboeba816ad2019-09-28 11:36:45 -06001330static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001331{
1332 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1333
Jens Axboe491381ce2019-10-17 09:20:46 -06001334 if (kiocb->ki_flags & IOCB_WRITE)
1335 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001336
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001337 if (res != req->result)
1338 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001339 io_cqring_add_event(req, res);
Jens Axboeba816ad2019-09-28 11:36:45 -06001340}
1341
1342static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1343{
1344 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1345
1346 io_complete_rw_common(kiocb, res);
Jens Axboee65ef562019-03-12 10:16:44 -06001347 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001348}
1349
Jens Axboeba816ad2019-09-28 11:36:45 -06001350static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res)
1351{
1352 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001353 struct io_kiocb *nxt = NULL;
Jens Axboeba816ad2019-09-28 11:36:45 -06001354
1355 io_complete_rw_common(kiocb, res);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001356 io_put_req_find_next(req, &nxt);
1357
1358 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001359}
1360
Jens Axboedef596e2019-01-09 08:59:42 -07001361static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1362{
1363 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1364
Jens Axboe491381ce2019-10-17 09:20:46 -06001365 if (kiocb->ki_flags & IOCB_WRITE)
1366 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001367
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001368 if (res != req->result)
1369 req_set_fail_links(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06001370 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -07001371 if (res != -EAGAIN)
1372 req->flags |= REQ_F_IOPOLL_COMPLETED;
1373}
1374
1375/*
1376 * After the iocb has been issued, it's safe to be found on the poll list.
1377 * Adding the kiocb to the list AFTER submission ensures that we don't
1378 * find it from a io_iopoll_getevents() thread before the issuer is done
1379 * accessing the kiocb cookie.
1380 */
1381static void io_iopoll_req_issued(struct io_kiocb *req)
1382{
1383 struct io_ring_ctx *ctx = req->ctx;
1384
1385 /*
1386 * Track whether we have multiple files in our lists. This will impact
1387 * how we do polling eventually, not spinning if we're on potentially
1388 * different devices.
1389 */
1390 if (list_empty(&ctx->poll_list)) {
1391 ctx->poll_multi_file = false;
1392 } else if (!ctx->poll_multi_file) {
1393 struct io_kiocb *list_req;
1394
1395 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1396 list);
1397 if (list_req->rw.ki_filp != req->rw.ki_filp)
1398 ctx->poll_multi_file = true;
1399 }
1400
1401 /*
1402 * For fast devices, IO may have already completed. If it has, add
1403 * it to the front so we find it first.
1404 */
1405 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1406 list_add(&req->list, &ctx->poll_list);
1407 else
1408 list_add_tail(&req->list, &ctx->poll_list);
1409}
1410
Jens Axboe3d6770f2019-04-13 11:50:54 -06001411static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07001412{
Jens Axboe3d6770f2019-04-13 11:50:54 -06001413 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -07001414 int diff = state->has_refs - state->used_refs;
1415
1416 if (diff)
1417 fput_many(state->file, diff);
1418 state->file = NULL;
1419 }
1420}
1421
1422/*
1423 * Get as many references to a file as we have IOs left in this submission,
1424 * assuming most submissions are for one file, or at least that each file
1425 * has more than one submission.
1426 */
1427static struct file *io_file_get(struct io_submit_state *state, int fd)
1428{
1429 if (!state)
1430 return fget(fd);
1431
1432 if (state->file) {
1433 if (state->fd == fd) {
1434 state->used_refs++;
1435 state->ios_left--;
1436 return state->file;
1437 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06001438 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07001439 }
1440 state->file = fget_many(fd, state->ios_left);
1441 if (!state->file)
1442 return NULL;
1443
1444 state->fd = fd;
1445 state->has_refs = state->ios_left;
1446 state->used_refs = 1;
1447 state->ios_left--;
1448 return state->file;
1449}
1450
Jens Axboe2b188cc2019-01-07 10:46:33 -07001451/*
1452 * If we tracked the file through the SCM inflight mechanism, we could support
1453 * any file. For now, just ensure that anything potentially problematic is done
1454 * inline.
1455 */
1456static bool io_file_supports_async(struct file *file)
1457{
1458 umode_t mode = file_inode(file)->i_mode;
1459
Jens Axboe10d59342019-12-09 20:16:22 -07001460 if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07001461 return true;
1462 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
1463 return true;
1464
1465 return false;
1466}
1467
Pavel Begunkov267bc902019-11-07 01:41:08 +03001468static int io_prep_rw(struct io_kiocb *req, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001469{
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001470 const struct io_uring_sqe *sqe = req->sqe;
Jens Axboedef596e2019-01-09 08:59:42 -07001471 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001472 struct kiocb *kiocb = &req->rw;
Jens Axboe09bb8392019-03-13 12:39:28 -06001473 unsigned ioprio;
1474 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001475
Jens Axboe09bb8392019-03-13 12:39:28 -06001476 if (!req->file)
1477 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001478
Jens Axboe491381ce2019-10-17 09:20:46 -06001479 if (S_ISREG(file_inode(req->file)->i_mode))
1480 req->flags |= REQ_F_ISREG;
1481
Jens Axboe2b188cc2019-01-07 10:46:33 -07001482 kiocb->ki_pos = READ_ONCE(sqe->off);
1483 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
1484 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
1485
1486 ioprio = READ_ONCE(sqe->ioprio);
1487 if (ioprio) {
1488 ret = ioprio_check_cap(ioprio);
1489 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06001490 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001491
1492 kiocb->ki_ioprio = ioprio;
1493 } else
1494 kiocb->ki_ioprio = get_current_ioprio();
1495
1496 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
1497 if (unlikely(ret))
Jens Axboe09bb8392019-03-13 12:39:28 -06001498 return ret;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001499
1500 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06001501 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
1502 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02001503 req->flags |= REQ_F_NOWAIT;
1504
1505 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001506 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001507
Jens Axboedef596e2019-01-09 08:59:42 -07001508 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07001509 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
1510 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06001511 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001512
Jens Axboedef596e2019-01-09 08:59:42 -07001513 kiocb->ki_flags |= IOCB_HIPRI;
1514 kiocb->ki_complete = io_complete_rw_iopoll;
Jens Axboe6873e0b2019-10-30 13:53:09 -06001515 req->result = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001516 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001517 if (kiocb->ki_flags & IOCB_HIPRI)
1518 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07001519 kiocb->ki_complete = io_complete_rw;
1520 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001521 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001522}
1523
1524static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
1525{
1526 switch (ret) {
1527 case -EIOCBQUEUED:
1528 break;
1529 case -ERESTARTSYS:
1530 case -ERESTARTNOINTR:
1531 case -ERESTARTNOHAND:
1532 case -ERESTART_RESTARTBLOCK:
1533 /*
1534 * We can't just restart the syscall, since previously
1535 * submitted sqes may already be in progress. Just fail this
1536 * IO with EINTR.
1537 */
1538 ret = -EINTR;
1539 /* fall through */
1540 default:
1541 kiocb->ki_complete(kiocb, ret, 0);
1542 }
1543}
1544
Jens Axboeba816ad2019-09-28 11:36:45 -06001545static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt,
1546 bool in_async)
1547{
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001548 if (in_async && ret >= 0 && kiocb->ki_complete == io_complete_rw)
Jens Axboeba816ad2019-09-28 11:36:45 -06001549 *nxt = __io_complete_rw(kiocb, ret);
1550 else
1551 io_rw_done(kiocb, ret);
1552}
1553
Pavel Begunkov7d009162019-11-25 23:14:40 +03001554static ssize_t io_import_fixed(struct io_ring_ctx *ctx, int rw,
1555 const struct io_uring_sqe *sqe,
1556 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07001557{
1558 size_t len = READ_ONCE(sqe->len);
1559 struct io_mapped_ubuf *imu;
1560 unsigned index, buf_index;
1561 size_t offset;
1562 u64 buf_addr;
1563
1564 /* attempt to use fixed buffers without having provided iovecs */
1565 if (unlikely(!ctx->user_bufs))
1566 return -EFAULT;
1567
1568 buf_index = READ_ONCE(sqe->buf_index);
1569 if (unlikely(buf_index >= ctx->nr_user_bufs))
1570 return -EFAULT;
1571
1572 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
1573 imu = &ctx->user_bufs[index];
1574 buf_addr = READ_ONCE(sqe->addr);
1575
1576 /* overflow */
1577 if (buf_addr + len < buf_addr)
1578 return -EFAULT;
1579 /* not inside the mapped region */
1580 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
1581 return -EFAULT;
1582
1583 /*
1584 * May not be a start of buffer, set size appropriately
1585 * and advance us to the beginning.
1586 */
1587 offset = buf_addr - imu->ubuf;
1588 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06001589
1590 if (offset) {
1591 /*
1592 * Don't use iov_iter_advance() here, as it's really slow for
1593 * using the latter parts of a big fixed buffer - it iterates
1594 * over each segment manually. We can cheat a bit here, because
1595 * we know that:
1596 *
1597 * 1) it's a BVEC iter, we set it up
1598 * 2) all bvecs are PAGE_SIZE in size, except potentially the
1599 * first and last bvec
1600 *
1601 * So just find our index, and adjust the iterator afterwards.
1602 * If the offset is within the first bvec (or the whole first
1603 * bvec, just use iov_iter_advance(). This makes it easier
1604 * since we can just skip the first segment, which may not
1605 * be PAGE_SIZE aligned.
1606 */
1607 const struct bio_vec *bvec = imu->bvec;
1608
1609 if (offset <= bvec->bv_len) {
1610 iov_iter_advance(iter, offset);
1611 } else {
1612 unsigned long seg_skip;
1613
1614 /* skip first vec */
1615 offset -= bvec->bv_len;
1616 seg_skip = 1 + (offset >> PAGE_SHIFT);
1617
1618 iter->bvec = bvec + seg_skip;
1619 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02001620 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001621 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001622 }
1623 }
1624
Jens Axboe5e559562019-11-13 16:12:46 -07001625 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001626}
1627
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001628static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
1629 struct iovec **iovec, struct iov_iter *iter)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001630{
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001631 const struct io_uring_sqe *sqe = req->sqe;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001632 void __user *buf = u64_to_user_ptr(READ_ONCE(sqe->addr));
1633 size_t sqe_len = READ_ONCE(sqe->len);
Jens Axboeedafcce2019-01-09 09:16:05 -07001634 u8 opcode;
1635
1636 /*
1637 * We're reading ->opcode for the second time, but the first read
1638 * doesn't care whether it's _FIXED or not, so it doesn't matter
1639 * whether ->opcode changes concurrently. The first read does care
1640 * about whether it is a READ or a WRITE, so we don't trust this read
1641 * for that purpose and instead let the caller pass in the read/write
1642 * flag.
1643 */
1644 opcode = READ_ONCE(sqe->opcode);
Pavel Begunkov7d009162019-11-25 23:14:40 +03001645 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07001646 *iovec = NULL;
Pavel Begunkov7d009162019-11-25 23:14:40 +03001647 return io_import_fixed(req->ctx, rw, sqe, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07001648 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001649
Jens Axboef67676d2019-12-02 11:03:47 -07001650 if (req->io) {
1651 struct io_async_rw *iorw = &req->io->rw;
1652
1653 *iovec = iorw->iov;
1654 iov_iter_init(iter, rw, *iovec, iorw->nr_segs, iorw->size);
1655 if (iorw->iov == iorw->fast_iov)
1656 *iovec = NULL;
1657 return iorw->size;
1658 }
1659
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001660 if (!req->has_user)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001661 return -EFAULT;
1662
1663#ifdef CONFIG_COMPAT
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001664 if (req->ctx->compat)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001665 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
1666 iovec, iter);
1667#endif
1668
1669 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
1670}
1671
Jens Axboe32960612019-09-23 11:05:34 -06001672/*
1673 * For files that don't have ->read_iter() and ->write_iter(), handle them
1674 * by looping over ->read() or ->write() manually.
1675 */
1676static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
1677 struct iov_iter *iter)
1678{
1679 ssize_t ret = 0;
1680
1681 /*
1682 * Don't support polled IO through this interface, and we can't
1683 * support non-blocking either. For the latter, this just causes
1684 * the kiocb to be handled from an async context.
1685 */
1686 if (kiocb->ki_flags & IOCB_HIPRI)
1687 return -EOPNOTSUPP;
1688 if (kiocb->ki_flags & IOCB_NOWAIT)
1689 return -EAGAIN;
1690
1691 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03001692 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06001693 ssize_t nr;
1694
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03001695 if (!iov_iter_is_bvec(iter)) {
1696 iovec = iov_iter_iovec(iter);
1697 } else {
1698 /* fixed buffers import bvec */
1699 iovec.iov_base = kmap(iter->bvec->bv_page)
1700 + iter->iov_offset;
1701 iovec.iov_len = min(iter->count,
1702 iter->bvec->bv_len - iter->iov_offset);
1703 }
1704
Jens Axboe32960612019-09-23 11:05:34 -06001705 if (rw == READ) {
1706 nr = file->f_op->read(file, iovec.iov_base,
1707 iovec.iov_len, &kiocb->ki_pos);
1708 } else {
1709 nr = file->f_op->write(file, iovec.iov_base,
1710 iovec.iov_len, &kiocb->ki_pos);
1711 }
1712
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03001713 if (iov_iter_is_bvec(iter))
1714 kunmap(iter->bvec->bv_page);
1715
Jens Axboe32960612019-09-23 11:05:34 -06001716 if (nr < 0) {
1717 if (!ret)
1718 ret = nr;
1719 break;
1720 }
1721 ret += nr;
1722 if (nr != iovec.iov_len)
1723 break;
1724 iov_iter_advance(iter, nr);
1725 }
1726
1727 return ret;
1728}
1729
Jens Axboeb7bb4f72019-12-15 22:13:43 -07001730static void io_req_map_rw(struct io_kiocb *req, ssize_t io_size,
Jens Axboef67676d2019-12-02 11:03:47 -07001731 struct iovec *iovec, struct iovec *fast_iov,
1732 struct iov_iter *iter)
1733{
1734 req->io->rw.nr_segs = iter->nr_segs;
1735 req->io->rw.size = io_size;
1736 req->io->rw.iov = iovec;
1737 if (!req->io->rw.iov) {
1738 req->io->rw.iov = req->io->rw.fast_iov;
1739 memcpy(req->io->rw.iov, fast_iov,
1740 sizeof(struct iovec) * iter->nr_segs);
1741 }
1742}
1743
Jens Axboeb7bb4f72019-12-15 22:13:43 -07001744static int io_alloc_async_ctx(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07001745{
1746 req->io = kmalloc(sizeof(*req->io), GFP_KERNEL);
1747 if (req->io) {
Jens Axboef67676d2019-12-02 11:03:47 -07001748 memcpy(&req->io->sqe, req->sqe, sizeof(req->io->sqe));
1749 req->sqe = &req->io->sqe;
1750 return 0;
1751 }
1752
Jens Axboeb7bb4f72019-12-15 22:13:43 -07001753 return 1;
1754}
1755
1756static void io_rw_async(struct io_wq_work **workptr)
1757{
1758 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
1759 struct iovec *iov = NULL;
1760
1761 if (req->io->rw.iov != req->io->rw.fast_iov)
1762 iov = req->io->rw.iov;
1763 io_wq_submit_work(workptr);
1764 kfree(iov);
1765}
1766
1767static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size,
1768 struct iovec *iovec, struct iovec *fast_iov,
1769 struct iov_iter *iter)
1770{
1771 if (!req->io && io_alloc_async_ctx(req))
1772 return -ENOMEM;
1773
1774 io_req_map_rw(req, io_size, iovec, fast_iov, iter);
1775 req->work.func = io_rw_async;
1776 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07001777}
1778
1779static int io_read_prep(struct io_kiocb *req, struct iovec **iovec,
1780 struct iov_iter *iter, bool force_nonblock)
1781{
1782 ssize_t ret;
1783
1784 ret = io_prep_rw(req, force_nonblock);
1785 if (ret)
1786 return ret;
1787
1788 if (unlikely(!(req->file->f_mode & FMODE_READ)))
1789 return -EBADF;
1790
1791 return io_import_iovec(READ, req, iovec, iter);
1792}
1793
Pavel Begunkov267bc902019-11-07 01:41:08 +03001794static int io_read(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001795 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001796{
1797 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1798 struct kiocb *kiocb = &req->rw;
1799 struct iov_iter iter;
1800 struct file *file;
Jens Axboe31b51512019-01-18 22:56:34 -07001801 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07001802 ssize_t io_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001803
Jens Axboef67676d2019-12-02 11:03:47 -07001804 if (!req->io) {
1805 ret = io_read_prep(req, &iovec, &iter, force_nonblock);
1806 if (ret < 0)
1807 return ret;
1808 } else {
1809 ret = io_import_iovec(READ, req, &iovec, &iter);
1810 if (ret < 0)
1811 return ret;
1812 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001813
Jens Axboef67676d2019-12-02 11:03:47 -07001814 file = req->file;
1815 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06001816 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07001817 req->result = io_size;
1818
1819 /*
1820 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
1821 * we know to async punt it even if it was opened O_NONBLOCK
1822 */
1823 if (force_nonblock && !io_file_supports_async(file)) {
1824 req->flags |= REQ_F_MUST_PUNT;
1825 goto copy_iov;
1826 }
Jens Axboe9e645e112019-05-10 16:07:28 -06001827
Jens Axboe31b51512019-01-18 22:56:34 -07001828 iov_count = iov_iter_count(&iter);
1829 ret = rw_verify_area(READ, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001830 if (!ret) {
1831 ssize_t ret2;
1832
Jens Axboe32960612019-09-23 11:05:34 -06001833 if (file->f_op->read_iter)
1834 ret2 = call_read_iter(file, kiocb, &iter);
1835 else
1836 ret2 = loop_rw_iter(READ, file, kiocb, &iter);
1837
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001838 /*
1839 * In case of a short read, punt to async. This can happen
1840 * if we have data partially cached. Alternatively we can
1841 * return the short read, in which case the application will
1842 * need to issue another SQE and wait for it. That SQE will
1843 * need async punt anyway, so it's more efficient to do it
1844 * here.
1845 */
Jens Axboe491381ce2019-10-17 09:20:46 -06001846 if (force_nonblock && !(req->flags & REQ_F_NOWAIT) &&
1847 (req->flags & REQ_F_ISREG) &&
Jens Axboef67676d2019-12-02 11:03:47 -07001848 ret2 > 0 && ret2 < io_size)
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001849 ret2 = -EAGAIN;
1850 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboef67676d2019-12-02 11:03:47 -07001851 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001852 kiocb_done(kiocb, ret2, nxt, req->in_async);
Jens Axboef67676d2019-12-02 11:03:47 -07001853 } else {
1854copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07001855 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07001856 inline_vecs, &iter);
1857 if (ret)
1858 goto out_free;
1859 return -EAGAIN;
1860 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001861 }
Jens Axboef67676d2019-12-02 11:03:47 -07001862out_free:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07001863 if (!io_wq_current_is_worker())
1864 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001865 return ret;
1866}
1867
Jens Axboef67676d2019-12-02 11:03:47 -07001868static int io_write_prep(struct io_kiocb *req, struct iovec **iovec,
1869 struct iov_iter *iter, bool force_nonblock)
1870{
1871 ssize_t ret;
1872
1873 ret = io_prep_rw(req, force_nonblock);
1874 if (ret)
1875 return ret;
1876
1877 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
1878 return -EBADF;
1879
1880 return io_import_iovec(WRITE, req, iovec, iter);
1881}
1882
Pavel Begunkov267bc902019-11-07 01:41:08 +03001883static int io_write(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001884 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001885{
1886 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1887 struct kiocb *kiocb = &req->rw;
1888 struct iov_iter iter;
1889 struct file *file;
Jens Axboe31b51512019-01-18 22:56:34 -07001890 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07001891 ssize_t ret, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001892
Jens Axboef67676d2019-12-02 11:03:47 -07001893 if (!req->io) {
1894 ret = io_write_prep(req, &iovec, &iter, force_nonblock);
1895 if (ret < 0)
1896 return ret;
1897 } else {
1898 ret = io_import_iovec(WRITE, req, &iovec, &iter);
1899 if (ret < 0)
1900 return ret;
1901 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001902
Jens Axboe2b188cc2019-01-07 10:46:33 -07001903 file = kiocb->ki_filp;
Jens Axboef67676d2019-12-02 11:03:47 -07001904 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06001905 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07001906 req->result = io_size;
1907
1908 /*
1909 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
1910 * we know to async punt it even if it was opened O_NONBLOCK
1911 */
1912 if (force_nonblock && !io_file_supports_async(req->file)) {
1913 req->flags |= REQ_F_MUST_PUNT;
1914 goto copy_iov;
1915 }
1916
Jens Axboe10d59342019-12-09 20:16:22 -07001917 /* file path doesn't support NOWAIT for non-direct_IO */
1918 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
1919 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07001920 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06001921
Jens Axboe31b51512019-01-18 22:56:34 -07001922 iov_count = iov_iter_count(&iter);
Jens Axboe31b51512019-01-18 22:56:34 -07001923 ret = rw_verify_area(WRITE, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001924 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01001925 ssize_t ret2;
1926
Jens Axboe2b188cc2019-01-07 10:46:33 -07001927 /*
1928 * Open-code file_start_write here to grab freeze protection,
1929 * which will be released by another thread in
1930 * io_complete_rw(). Fool lockdep by telling it the lock got
1931 * released so that it doesn't complain about the held lock when
1932 * we return to userspace.
1933 */
Jens Axboe491381ce2019-10-17 09:20:46 -06001934 if (req->flags & REQ_F_ISREG) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001935 __sb_start_write(file_inode(file)->i_sb,
1936 SB_FREEZE_WRITE, true);
1937 __sb_writers_release(file_inode(file)->i_sb,
1938 SB_FREEZE_WRITE);
1939 }
1940 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01001941
Jens Axboe32960612019-09-23 11:05:34 -06001942 if (file->f_op->write_iter)
1943 ret2 = call_write_iter(file, kiocb, &iter);
1944 else
1945 ret2 = loop_rw_iter(WRITE, file, kiocb, &iter);
Jens Axboef67676d2019-12-02 11:03:47 -07001946 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001947 kiocb_done(kiocb, ret2, nxt, req->in_async);
Jens Axboef67676d2019-12-02 11:03:47 -07001948 } else {
1949copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07001950 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07001951 inline_vecs, &iter);
1952 if (ret)
1953 goto out_free;
1954 return -EAGAIN;
1955 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001956 }
Jens Axboe31b51512019-01-18 22:56:34 -07001957out_free:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07001958 if (!io_wq_current_is_worker())
1959 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001960 return ret;
1961}
1962
1963/*
1964 * IORING_OP_NOP just posts a completion event, nothing else.
1965 */
Jens Axboe78e19bb2019-11-06 15:21:34 -07001966static int io_nop(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001967{
1968 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001969
Jens Axboedef596e2019-01-09 08:59:42 -07001970 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1971 return -EINVAL;
1972
Jens Axboe78e19bb2019-11-06 15:21:34 -07001973 io_cqring_add_event(req, 0);
Jens Axboee65ef562019-03-12 10:16:44 -06001974 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001975 return 0;
1976}
1977
Jens Axboefc4df992019-12-10 14:38:45 -07001978static int io_prep_fsync(struct io_kiocb *req)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001979{
Jens Axboefc4df992019-12-10 14:38:45 -07001980 const struct io_uring_sqe *sqe = req->sqe;
Jens Axboe6b063142019-01-10 22:13:58 -07001981 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001982
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07001983 if (req->flags & REQ_F_PREPPED)
1984 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06001985 if (!req->file)
1986 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001987
Jens Axboe6b063142019-01-10 22:13:58 -07001988 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07001989 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07001990 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001991 return -EINVAL;
1992
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07001993 req->sync.flags = READ_ONCE(sqe->fsync_flags);
1994 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
1995 return -EINVAL;
1996
1997 req->sync.off = READ_ONCE(sqe->off);
1998 req->sync.len = READ_ONCE(sqe->len);
1999 req->flags |= REQ_F_PREPPED;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002000 return 0;
2001}
2002
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002003static bool io_req_cancelled(struct io_kiocb *req)
2004{
2005 if (req->work.flags & IO_WQ_WORK_CANCEL) {
2006 req_set_fail_links(req);
2007 io_cqring_add_event(req, -ECANCELED);
2008 io_put_req(req);
2009 return true;
2010 }
2011
2012 return false;
2013}
2014
2015static void io_fsync_finish(struct io_wq_work **workptr)
2016{
2017 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2018 loff_t end = req->sync.off + req->sync.len;
2019 struct io_kiocb *nxt = NULL;
2020 int ret;
2021
2022 if (io_req_cancelled(req))
2023 return;
2024
2025 ret = vfs_fsync_range(req->rw.ki_filp, req->sync.off,
2026 end > 0 ? end : LLONG_MAX,
2027 req->sync.flags & IORING_FSYNC_DATASYNC);
2028 if (ret < 0)
2029 req_set_fail_links(req);
2030 io_cqring_add_event(req, ret);
2031 io_put_req_find_next(req, &nxt);
2032 if (nxt)
2033 *workptr = &nxt->work;
2034}
2035
Jens Axboefc4df992019-12-10 14:38:45 -07002036static int io_fsync(struct io_kiocb *req, struct io_kiocb **nxt,
2037 bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002038{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002039 struct io_wq_work *work, *old_work;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002040 int ret;
2041
Jens Axboefc4df992019-12-10 14:38:45 -07002042 ret = io_prep_fsync(req);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002043 if (ret)
2044 return ret;
2045
2046 /* fsync always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002047 if (force_nonblock) {
2048 io_put_req(req);
2049 req->work.func = io_fsync_finish;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002050 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002051 }
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002052
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002053 work = old_work = &req->work;
2054 io_fsync_finish(&work);
2055 if (work && work != old_work)
2056 *nxt = container_of(work, struct io_kiocb, work);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002057 return 0;
2058}
2059
Jens Axboefc4df992019-12-10 14:38:45 -07002060static int io_prep_sfr(struct io_kiocb *req)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002061{
Jens Axboefc4df992019-12-10 14:38:45 -07002062 const struct io_uring_sqe *sqe = req->sqe;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002063 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002064
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002065 if (req->flags & REQ_F_PREPPED)
2066 return 0;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002067 if (!req->file)
2068 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002069
2070 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2071 return -EINVAL;
2072 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
2073 return -EINVAL;
2074
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002075 req->sync.off = READ_ONCE(sqe->off);
2076 req->sync.len = READ_ONCE(sqe->len);
2077 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
2078 req->flags |= REQ_F_PREPPED;
2079 return 0;
2080}
2081
2082static void io_sync_file_range_finish(struct io_wq_work **workptr)
2083{
2084 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2085 struct io_kiocb *nxt = NULL;
2086 int ret;
2087
2088 if (io_req_cancelled(req))
2089 return;
2090
2091 ret = sync_file_range(req->rw.ki_filp, req->sync.off, req->sync.len,
2092 req->sync.flags);
2093 if (ret < 0)
2094 req_set_fail_links(req);
2095 io_cqring_add_event(req, ret);
2096 io_put_req_find_next(req, &nxt);
2097 if (nxt)
2098 *workptr = &nxt->work;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002099}
2100
Jens Axboefc4df992019-12-10 14:38:45 -07002101static int io_sync_file_range(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002102 bool force_nonblock)
2103{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002104 struct io_wq_work *work, *old_work;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002105 int ret;
2106
Jens Axboefc4df992019-12-10 14:38:45 -07002107 ret = io_prep_sfr(req);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002108 if (ret)
2109 return ret;
2110
2111 /* sync_file_range always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002112 if (force_nonblock) {
2113 io_put_req(req);
2114 req->work.func = io_sync_file_range_finish;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002115 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002116 }
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002117
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002118 work = old_work = &req->work;
2119 io_sync_file_range_finish(&work);
2120 if (work && work != old_work)
2121 *nxt = container_of(work, struct io_kiocb, work);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002122 return 0;
2123}
2124
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002125#if defined(CONFIG_NET)
2126static void io_sendrecv_async(struct io_wq_work **workptr)
2127{
2128 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2129 struct iovec *iov = NULL;
2130
2131 if (req->io->rw.iov != req->io->rw.fast_iov)
2132 iov = req->io->msg.iov;
2133 io_wq_submit_work(workptr);
2134 kfree(iov);
2135}
2136#endif
2137
Jens Axboe03b12302019-12-02 18:50:25 -07002138static int io_sendmsg_prep(struct io_kiocb *req, struct io_async_ctx *io)
Jens Axboeaa1fa282019-04-19 13:38:09 -06002139{
Jens Axboe03b12302019-12-02 18:50:25 -07002140#if defined(CONFIG_NET)
2141 const struct io_uring_sqe *sqe = req->sqe;
2142 struct user_msghdr __user *msg;
2143 unsigned flags;
2144
2145 flags = READ_ONCE(sqe->msg_flags);
2146 msg = (struct user_msghdr __user *)(unsigned long) READ_ONCE(sqe->addr);
Jens Axboed9688562019-12-09 19:35:20 -07002147 io->msg.iov = io->msg.fast_iov;
Jens Axboe03b12302019-12-02 18:50:25 -07002148 return sendmsg_copy_msghdr(&io->msg.msg, msg, flags, &io->msg.iov);
2149#else
2150 return 0;
2151#endif
2152}
2153
Jens Axboefc4df992019-12-10 14:38:45 -07002154static int io_sendmsg(struct io_kiocb *req, struct io_kiocb **nxt,
2155 bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07002156{
2157#if defined(CONFIG_NET)
Jens Axboefc4df992019-12-10 14:38:45 -07002158 const struct io_uring_sqe *sqe = req->sqe;
Jens Axboe0b416c32019-12-15 10:57:46 -07002159 struct io_async_msghdr *kmsg = NULL;
Jens Axboe03b12302019-12-02 18:50:25 -07002160 struct socket *sock;
2161 int ret;
2162
2163 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2164 return -EINVAL;
2165
2166 sock = sock_from_file(req->file, &ret);
2167 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002168 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07002169 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -07002170 unsigned flags;
2171
2172 flags = READ_ONCE(sqe->msg_flags);
2173 if (flags & MSG_DONTWAIT)
2174 req->flags |= REQ_F_NOWAIT;
2175 else if (force_nonblock)
2176 flags |= MSG_DONTWAIT;
2177
2178 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07002179 kmsg = &req->io->msg;
2180 kmsg->msg.msg_name = &addr;
2181 /* if iov is set, it's allocated already */
2182 if (!kmsg->iov)
2183 kmsg->iov = kmsg->fast_iov;
2184 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07002185 } else {
Jens Axboe0b416c32019-12-15 10:57:46 -07002186 kmsg = &io.msg;
2187 kmsg->msg.msg_name = &addr;
Jens Axboe03b12302019-12-02 18:50:25 -07002188 ret = io_sendmsg_prep(req, &io);
2189 if (ret)
2190 goto out;
2191 }
2192
Jens Axboe0b416c32019-12-15 10:57:46 -07002193 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Jens Axboe03b12302019-12-02 18:50:25 -07002194 if (force_nonblock && ret == -EAGAIN) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002195 if (req->io)
2196 return -EAGAIN;
2197 if (io_alloc_async_ctx(req))
2198 return -ENOMEM;
2199 memcpy(&req->io->msg, &io.msg, sizeof(io.msg));
2200 req->work.func = io_sendrecv_async;
Jens Axboe0b416c32019-12-15 10:57:46 -07002201 return -EAGAIN;
Jens Axboe03b12302019-12-02 18:50:25 -07002202 }
2203 if (ret == -ERESTARTSYS)
2204 ret = -EINTR;
2205 }
2206
2207out:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002208 if (!io_wq_current_is_worker() && kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07002209 kfree(kmsg->iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002210 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002211 if (ret < 0)
2212 req_set_fail_links(req);
Jens Axboe03b12302019-12-02 18:50:25 -07002213 io_put_req_find_next(req, nxt);
2214 return 0;
2215#else
2216 return -EOPNOTSUPP;
2217#endif
2218}
2219
2220static int io_recvmsg_prep(struct io_kiocb *req, struct io_async_ctx *io)
2221{
2222#if defined(CONFIG_NET)
2223 const struct io_uring_sqe *sqe = req->sqe;
2224 struct user_msghdr __user *msg;
2225 unsigned flags;
2226
2227 flags = READ_ONCE(sqe->msg_flags);
2228 msg = (struct user_msghdr __user *)(unsigned long) READ_ONCE(sqe->addr);
Jens Axboed9688562019-12-09 19:35:20 -07002229 io->msg.iov = io->msg.fast_iov;
Jens Axboe03b12302019-12-02 18:50:25 -07002230 return recvmsg_copy_msghdr(&io->msg.msg, msg, flags, &io->msg.uaddr,
2231 &io->msg.iov);
2232#else
2233 return 0;
2234#endif
2235}
2236
Jens Axboefc4df992019-12-10 14:38:45 -07002237static int io_recvmsg(struct io_kiocb *req, struct io_kiocb **nxt,
2238 bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07002239{
2240#if defined(CONFIG_NET)
Jens Axboefc4df992019-12-10 14:38:45 -07002241 const struct io_uring_sqe *sqe = req->sqe;
Jens Axboe0b416c32019-12-15 10:57:46 -07002242 struct io_async_msghdr *kmsg = NULL;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002243 struct socket *sock;
2244 int ret;
2245
2246 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2247 return -EINVAL;
2248
2249 sock = sock_from_file(req->file, &ret);
2250 if (sock) {
2251 struct user_msghdr __user *msg;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002252 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07002253 struct sockaddr_storage addr;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002254 unsigned flags;
2255
2256 flags = READ_ONCE(sqe->msg_flags);
2257 if (flags & MSG_DONTWAIT)
2258 req->flags |= REQ_F_NOWAIT;
2259 else if (force_nonblock)
2260 flags |= MSG_DONTWAIT;
2261
2262 msg = (struct user_msghdr __user *) (unsigned long)
2263 READ_ONCE(sqe->addr);
Jens Axboe03b12302019-12-02 18:50:25 -07002264 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07002265 kmsg = &req->io->msg;
2266 kmsg->msg.msg_name = &addr;
2267 /* if iov is set, it's allocated already */
2268 if (!kmsg->iov)
2269 kmsg->iov = kmsg->fast_iov;
2270 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07002271 } else {
Jens Axboe0b416c32019-12-15 10:57:46 -07002272 kmsg = &io.msg;
2273 kmsg->msg.msg_name = &addr;
Jens Axboe03b12302019-12-02 18:50:25 -07002274 ret = io_recvmsg_prep(req, &io);
2275 if (ret)
2276 goto out;
2277 }
Jens Axboe0fa03c62019-04-19 13:34:07 -06002278
Jens Axboe0b416c32019-12-15 10:57:46 -07002279 ret = __sys_recvmsg_sock(sock, &kmsg->msg, msg, kmsg->uaddr, flags);
Jens Axboe03b12302019-12-02 18:50:25 -07002280 if (force_nonblock && ret == -EAGAIN) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002281 if (req->io)
2282 return -EAGAIN;
2283 if (io_alloc_async_ctx(req))
2284 return -ENOMEM;
2285 memcpy(&req->io->msg, &io.msg, sizeof(io.msg));
2286 req->work.func = io_sendrecv_async;
Jens Axboe0b416c32019-12-15 10:57:46 -07002287 return -EAGAIN;
Jens Axboe03b12302019-12-02 18:50:25 -07002288 }
Jens Axboe441cdbd2019-12-02 18:49:10 -07002289 if (ret == -ERESTARTSYS)
2290 ret = -EINTR;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002291 }
2292
Jens Axboe03b12302019-12-02 18:50:25 -07002293out:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002294 if (!io_wq_current_is_worker() && kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07002295 kfree(kmsg->iov);
Jens Axboe78e19bb2019-11-06 15:21:34 -07002296 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002297 if (ret < 0)
2298 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08002299 io_put_req_find_next(req, nxt);
Jens Axboe0fa03c62019-04-19 13:34:07 -06002300 return 0;
2301#else
2302 return -EOPNOTSUPP;
2303#endif
2304}
2305
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002306static int io_accept_prep(struct io_kiocb *req)
Jens Axboe17f2fe32019-10-17 14:42:58 -06002307{
2308#if defined(CONFIG_NET)
Jens Axboefc4df992019-12-10 14:38:45 -07002309 const struct io_uring_sqe *sqe = req->sqe;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002310 struct io_accept *accept = &req->accept;
2311
2312 if (req->flags & REQ_F_PREPPED)
2313 return 0;
Jens Axboe17f2fe32019-10-17 14:42:58 -06002314
2315 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
2316 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05002317 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06002318 return -EINVAL;
2319
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002320 accept->addr = (struct sockaddr __user *)
2321 (unsigned long) READ_ONCE(sqe->addr);
2322 accept->addr_len = (int __user *) (unsigned long) READ_ONCE(sqe->addr2);
2323 accept->flags = READ_ONCE(sqe->accept_flags);
2324 req->flags |= REQ_F_PREPPED;
2325 return 0;
2326#else
2327 return -EOPNOTSUPP;
2328#endif
2329}
Jens Axboe17f2fe32019-10-17 14:42:58 -06002330
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002331#if defined(CONFIG_NET)
2332static int __io_accept(struct io_kiocb *req, struct io_kiocb **nxt,
2333 bool force_nonblock)
2334{
2335 struct io_accept *accept = &req->accept;
2336 unsigned file_flags;
2337 int ret;
2338
2339 file_flags = force_nonblock ? O_NONBLOCK : 0;
2340 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
2341 accept->addr_len, accept->flags);
2342 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06002343 return -EAGAIN;
Jens Axboe8e3cca12019-11-09 19:52:33 -07002344 if (ret == -ERESTARTSYS)
2345 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002346 if (ret < 0)
2347 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07002348 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08002349 io_put_req_find_next(req, nxt);
Jens Axboe17f2fe32019-10-17 14:42:58 -06002350 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002351}
2352
2353static void io_accept_finish(struct io_wq_work **workptr)
2354{
2355 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2356 struct io_kiocb *nxt = NULL;
2357
2358 if (io_req_cancelled(req))
2359 return;
2360 __io_accept(req, &nxt, false);
2361 if (nxt)
2362 *workptr = &nxt->work;
2363}
2364#endif
2365
2366static int io_accept(struct io_kiocb *req, struct io_kiocb **nxt,
2367 bool force_nonblock)
2368{
2369#if defined(CONFIG_NET)
2370 int ret;
2371
2372 ret = io_accept_prep(req);
2373 if (ret)
2374 return ret;
2375
2376 ret = __io_accept(req, nxt, force_nonblock);
2377 if (ret == -EAGAIN && force_nonblock) {
2378 req->work.func = io_accept_finish;
2379 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
2380 io_put_req(req);
2381 return -EAGAIN;
2382 }
2383 return 0;
Jens Axboe17f2fe32019-10-17 14:42:58 -06002384#else
2385 return -EOPNOTSUPP;
2386#endif
2387}
2388
Jens Axboef499a022019-12-02 16:28:46 -07002389static int io_connect_prep(struct io_kiocb *req, struct io_async_ctx *io)
2390{
2391#if defined(CONFIG_NET)
2392 const struct io_uring_sqe *sqe = req->sqe;
2393 struct sockaddr __user *addr;
2394 int addr_len;
2395
2396 addr = (struct sockaddr __user *) (unsigned long) READ_ONCE(sqe->addr);
2397 addr_len = READ_ONCE(sqe->addr2);
2398 return move_addr_to_kernel(addr, addr_len, &io->connect.address);
2399#else
2400 return 0;
2401#endif
2402}
2403
Jens Axboefc4df992019-12-10 14:38:45 -07002404static int io_connect(struct io_kiocb *req, struct io_kiocb **nxt,
2405 bool force_nonblock)
Jens Axboef8e85cf2019-11-23 14:24:24 -07002406{
2407#if defined(CONFIG_NET)
Jens Axboefc4df992019-12-10 14:38:45 -07002408 const struct io_uring_sqe *sqe = req->sqe;
Jens Axboef499a022019-12-02 16:28:46 -07002409 struct io_async_ctx __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07002410 unsigned file_flags;
2411 int addr_len, ret;
2412
2413 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
2414 return -EINVAL;
2415 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
2416 return -EINVAL;
2417
Jens Axboef8e85cf2019-11-23 14:24:24 -07002418 addr_len = READ_ONCE(sqe->addr2);
2419 file_flags = force_nonblock ? O_NONBLOCK : 0;
2420
Jens Axboef499a022019-12-02 16:28:46 -07002421 if (req->io) {
2422 io = req->io;
2423 } else {
2424 ret = io_connect_prep(req, &__io);
2425 if (ret)
2426 goto out;
2427 io = &__io;
2428 }
2429
2430 ret = __sys_connect_file(req->file, &io->connect.address, addr_len,
2431 file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07002432 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002433 if (req->io)
2434 return -EAGAIN;
2435 if (io_alloc_async_ctx(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07002436 ret = -ENOMEM;
2437 goto out;
2438 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002439 memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect));
Jens Axboef8e85cf2019-11-23 14:24:24 -07002440 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07002441 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07002442 if (ret == -ERESTARTSYS)
2443 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07002444out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002445 if (ret < 0)
2446 req_set_fail_links(req);
Jens Axboef8e85cf2019-11-23 14:24:24 -07002447 io_cqring_add_event(req, ret);
2448 io_put_req_find_next(req, nxt);
2449 return 0;
2450#else
2451 return -EOPNOTSUPP;
2452#endif
2453}
2454
Jens Axboe221c5eb2019-01-17 09:41:58 -07002455static void io_poll_remove_one(struct io_kiocb *req)
2456{
2457 struct io_poll_iocb *poll = &req->poll;
2458
2459 spin_lock(&poll->head->lock);
2460 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07002461 if (!list_empty(&poll->wait.entry)) {
2462 list_del_init(&poll->wait.entry);
Jackie Liua197f662019-11-08 08:09:12 -07002463 io_queue_async_work(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002464 }
2465 spin_unlock(&poll->head->lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07002466 hash_del(&req->hash_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002467}
2468
2469static void io_poll_remove_all(struct io_ring_ctx *ctx)
2470{
Jens Axboe78076bb2019-12-04 19:56:40 -07002471 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002472 struct io_kiocb *req;
Jens Axboe78076bb2019-12-04 19:56:40 -07002473 int i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002474
2475 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07002476 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
2477 struct hlist_head *list;
2478
2479 list = &ctx->cancel_hash[i];
2480 hlist_for_each_entry_safe(req, tmp, list, hash_node)
2481 io_poll_remove_one(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002482 }
2483 spin_unlock_irq(&ctx->completion_lock);
2484}
2485
Jens Axboe47f46762019-11-09 17:43:02 -07002486static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
2487{
Jens Axboe78076bb2019-12-04 19:56:40 -07002488 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07002489 struct io_kiocb *req;
2490
Jens Axboe78076bb2019-12-04 19:56:40 -07002491 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
2492 hlist_for_each_entry(req, list, hash_node) {
2493 if (sqe_addr == req->user_data) {
Jens Axboeeac406c2019-11-14 12:09:58 -07002494 io_poll_remove_one(req);
2495 return 0;
2496 }
Jens Axboe47f46762019-11-09 17:43:02 -07002497 }
2498
2499 return -ENOENT;
2500}
2501
Jens Axboe0969e782019-12-17 18:40:57 -07002502static int io_poll_remove_prep(struct io_kiocb *req)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002503{
Jens Axboefc4df992019-12-10 14:38:45 -07002504 const struct io_uring_sqe *sqe = req->sqe;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002505
Jens Axboe0969e782019-12-17 18:40:57 -07002506 if (req->flags & REQ_F_PREPPED)
2507 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002508 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2509 return -EINVAL;
2510 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
2511 sqe->poll_events)
2512 return -EINVAL;
2513
Jens Axboe0969e782019-12-17 18:40:57 -07002514 req->poll.addr = READ_ONCE(sqe->addr);
2515 req->flags |= REQ_F_PREPPED;
2516 return 0;
2517}
2518
2519/*
2520 * Find a running poll command that matches one specified in sqe->addr,
2521 * and remove it if found.
2522 */
2523static int io_poll_remove(struct io_kiocb *req)
2524{
2525 struct io_ring_ctx *ctx = req->ctx;
2526 u64 addr;
2527 int ret;
2528
2529 ret = io_poll_remove_prep(req);
2530 if (ret)
2531 return ret;
2532
2533 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002534 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07002535 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002536 spin_unlock_irq(&ctx->completion_lock);
2537
Jens Axboe78e19bb2019-11-06 15:21:34 -07002538 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002539 if (ret < 0)
2540 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06002541 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002542 return 0;
2543}
2544
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002545static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002546{
Jackie Liua197f662019-11-08 08:09:12 -07002547 struct io_ring_ctx *ctx = req->ctx;
2548
Jens Axboe8c838782019-03-12 15:48:16 -06002549 req->poll.done = true;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002550 if (error)
2551 io_cqring_fill_event(req, error);
2552 else
2553 io_cqring_fill_event(req, mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06002554 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002555}
2556
Jens Axboe561fb042019-10-24 07:25:42 -06002557static void io_poll_complete_work(struct io_wq_work **workptr)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002558{
Jens Axboe561fb042019-10-24 07:25:42 -06002559 struct io_wq_work *work = *workptr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002560 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
2561 struct io_poll_iocb *poll = &req->poll;
2562 struct poll_table_struct pt = { ._key = poll->events };
2563 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe89723d02019-11-05 15:32:58 -07002564 struct io_kiocb *nxt = NULL;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002565 __poll_t mask = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002566 int ret = 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002567
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002568 if (work->flags & IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06002569 WRITE_ONCE(poll->canceled, true);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002570 ret = -ECANCELED;
2571 } else if (READ_ONCE(poll->canceled)) {
2572 ret = -ECANCELED;
2573 }
Jens Axboe561fb042019-10-24 07:25:42 -06002574
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002575 if (ret != -ECANCELED)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002576 mask = vfs_poll(poll->file, &pt) & poll->events;
2577
2578 /*
2579 * Note that ->ki_cancel callers also delete iocb from active_reqs after
2580 * calling ->ki_cancel. We need the ctx_lock roundtrip here to
2581 * synchronize with them. In the cancellation case the list_del_init
2582 * itself is not actually needed, but harmless so we keep it in to
2583 * avoid further branches in the fast path.
2584 */
2585 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002586 if (!mask && ret != -ECANCELED) {
Jens Axboe392edb42019-12-09 17:52:20 -07002587 add_wait_queue(poll->head, &poll->wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002588 spin_unlock_irq(&ctx->completion_lock);
2589 return;
2590 }
Jens Axboe78076bb2019-12-04 19:56:40 -07002591 hash_del(&req->hash_node);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002592 io_poll_complete(req, mask, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002593 spin_unlock_irq(&ctx->completion_lock);
2594
Jens Axboe8c838782019-03-12 15:48:16 -06002595 io_cqring_ev_posted(ctx);
Jens Axboe89723d02019-11-05 15:32:58 -07002596
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002597 if (ret < 0)
2598 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08002599 io_put_req_find_next(req, &nxt);
Jens Axboe89723d02019-11-05 15:32:58 -07002600 if (nxt)
2601 *workptr = &nxt->work;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002602}
2603
2604static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
2605 void *key)
2606{
Jens Axboee9444752019-11-26 15:02:04 -07002607 struct io_poll_iocb *poll = wait->private;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002608 struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
2609 struct io_ring_ctx *ctx = req->ctx;
2610 __poll_t mask = key_to_poll(key);
Jens Axboe8c838782019-03-12 15:48:16 -06002611 unsigned long flags;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002612
2613 /* for instances that support it check for an event match first: */
Jens Axboe8c838782019-03-12 15:48:16 -06002614 if (mask && !(mask & poll->events))
2615 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002616
Jens Axboe392edb42019-12-09 17:52:20 -07002617 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06002618
Jens Axboe7c9e7f02019-11-12 08:15:53 -07002619 /*
2620 * Run completion inline if we can. We're using trylock here because
2621 * we are violating the completion_lock -> poll wq lock ordering.
2622 * If we have a link timeout we're going to need the completion_lock
2623 * for finalizing the request, mark us as having grabbed that already.
2624 */
Jens Axboe8c838782019-03-12 15:48:16 -06002625 if (mask && spin_trylock_irqsave(&ctx->completion_lock, flags)) {
Jens Axboe78076bb2019-12-04 19:56:40 -07002626 hash_del(&req->hash_node);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002627 io_poll_complete(req, mask, 0);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07002628 req->flags |= REQ_F_COMP_LOCKED;
2629 io_put_req(req);
Jens Axboe8c838782019-03-12 15:48:16 -06002630 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2631
2632 io_cqring_ev_posted(ctx);
Jens Axboe8c838782019-03-12 15:48:16 -06002633 } else {
Jackie Liua197f662019-11-08 08:09:12 -07002634 io_queue_async_work(req);
Jens Axboe8c838782019-03-12 15:48:16 -06002635 }
2636
Jens Axboe221c5eb2019-01-17 09:41:58 -07002637 return 1;
2638}
2639
2640struct io_poll_table {
2641 struct poll_table_struct pt;
2642 struct io_kiocb *req;
2643 int error;
2644};
2645
2646static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
2647 struct poll_table_struct *p)
2648{
2649 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
2650
2651 if (unlikely(pt->req->poll.head)) {
2652 pt->error = -EINVAL;
2653 return;
2654 }
2655
2656 pt->error = 0;
2657 pt->req->poll.head = head;
Jens Axboe392edb42019-12-09 17:52:20 -07002658 add_wait_queue(head, &pt->req->poll.wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002659}
2660
Jens Axboeeac406c2019-11-14 12:09:58 -07002661static void io_poll_req_insert(struct io_kiocb *req)
2662{
2663 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07002664 struct hlist_head *list;
Jens Axboeeac406c2019-11-14 12:09:58 -07002665
Jens Axboe78076bb2019-12-04 19:56:40 -07002666 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
2667 hlist_add_head(&req->hash_node, list);
Jens Axboeeac406c2019-11-14 12:09:58 -07002668}
2669
Jens Axboe0969e782019-12-17 18:40:57 -07002670static int io_poll_add_prep(struct io_kiocb *req)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002671{
Jens Axboefc4df992019-12-10 14:38:45 -07002672 const struct io_uring_sqe *sqe = req->sqe;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002673 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002674 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002675
Jens Axboe0969e782019-12-17 18:40:57 -07002676 if (req->flags & REQ_F_PREPPED)
2677 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002678 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2679 return -EINVAL;
2680 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
2681 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06002682 if (!poll->file)
2683 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002684
Jens Axboe0969e782019-12-17 18:40:57 -07002685 req->flags |= REQ_F_PREPPED;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002686 events = READ_ONCE(sqe->poll_events);
2687 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
Jens Axboe0969e782019-12-17 18:40:57 -07002688 return 0;
2689}
2690
2691static int io_poll_add(struct io_kiocb *req, struct io_kiocb **nxt)
2692{
2693 struct io_poll_iocb *poll = &req->poll;
2694 struct io_ring_ctx *ctx = req->ctx;
2695 struct io_poll_table ipt;
2696 bool cancel = false;
2697 __poll_t mask;
2698 int ret;
2699
2700 ret = io_poll_add_prep(req);
2701 if (ret)
2702 return ret;
2703
2704 INIT_IO_WORK(&req->work, io_poll_complete_work);
Jens Axboe78076bb2019-12-04 19:56:40 -07002705 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002706
Jens Axboe221c5eb2019-01-17 09:41:58 -07002707 poll->head = NULL;
Jens Axboe8c838782019-03-12 15:48:16 -06002708 poll->done = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002709 poll->canceled = false;
2710
2711 ipt.pt._qproc = io_poll_queue_proc;
2712 ipt.pt._key = poll->events;
2713 ipt.req = req;
2714 ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
2715
2716 /* initialized the list so that we can do list_empty checks */
Jens Axboe392edb42019-12-09 17:52:20 -07002717 INIT_LIST_HEAD(&poll->wait.entry);
2718 init_waitqueue_func_entry(&poll->wait, io_poll_wake);
2719 poll->wait.private = poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002720
Jens Axboe36703242019-07-25 10:20:18 -06002721 INIT_LIST_HEAD(&req->list);
2722
Jens Axboe221c5eb2019-01-17 09:41:58 -07002723 mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002724
2725 spin_lock_irq(&ctx->completion_lock);
Jens Axboe8c838782019-03-12 15:48:16 -06002726 if (likely(poll->head)) {
2727 spin_lock(&poll->head->lock);
Jens Axboe392edb42019-12-09 17:52:20 -07002728 if (unlikely(list_empty(&poll->wait.entry))) {
Jens Axboe8c838782019-03-12 15:48:16 -06002729 if (ipt.error)
2730 cancel = true;
2731 ipt.error = 0;
2732 mask = 0;
2733 }
2734 if (mask || ipt.error)
Jens Axboe392edb42019-12-09 17:52:20 -07002735 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06002736 else if (cancel)
2737 WRITE_ONCE(poll->canceled, true);
2738 else if (!poll->done) /* actually waiting for an event */
Jens Axboeeac406c2019-11-14 12:09:58 -07002739 io_poll_req_insert(req);
Jens Axboe8c838782019-03-12 15:48:16 -06002740 spin_unlock(&poll->head->lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002741 }
Jens Axboe8c838782019-03-12 15:48:16 -06002742 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06002743 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002744 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06002745 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07002746 spin_unlock_irq(&ctx->completion_lock);
2747
Jens Axboe8c838782019-03-12 15:48:16 -06002748 if (mask) {
2749 io_cqring_ev_posted(ctx);
Jackie Liuec9c02a2019-11-08 23:50:36 +08002750 io_put_req_find_next(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002751 }
Jens Axboe8c838782019-03-12 15:48:16 -06002752 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002753}
2754
Jens Axboe5262f562019-09-17 12:26:57 -06002755static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
2756{
Jens Axboead8a48a2019-11-15 08:49:11 -07002757 struct io_timeout_data *data = container_of(timer,
2758 struct io_timeout_data, timer);
2759 struct io_kiocb *req = data->req;
2760 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06002761 unsigned long flags;
2762
Jens Axboe5262f562019-09-17 12:26:57 -06002763 atomic_inc(&ctx->cq_timeouts);
2764
2765 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08002766 /*
Jens Axboe11365042019-10-16 09:08:32 -06002767 * We could be racing with timeout deletion. If the list is empty,
2768 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08002769 */
Jens Axboe842f9612019-10-29 12:34:10 -06002770 if (!list_empty(&req->list)) {
Jens Axboe11365042019-10-16 09:08:32 -06002771 struct io_kiocb *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06002772
Jens Axboe11365042019-10-16 09:08:32 -06002773 /*
2774 * Adjust the reqs sequence before the current one because it
Brian Gianforcarod195a662019-12-13 03:09:50 -08002775 * will consume a slot in the cq_ring and the cq_tail
Jens Axboe11365042019-10-16 09:08:32 -06002776 * pointer will be increased, otherwise other timeout reqs may
2777 * return in advance without waiting for enough wait_nr.
2778 */
2779 prev = req;
2780 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
2781 prev->sequence++;
Jens Axboe11365042019-10-16 09:08:32 -06002782 list_del_init(&req->list);
Jens Axboe11365042019-10-16 09:08:32 -06002783 }
Jens Axboe842f9612019-10-29 12:34:10 -06002784
Jens Axboe78e19bb2019-11-06 15:21:34 -07002785 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06002786 io_commit_cqring(ctx);
2787 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2788
2789 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002790 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06002791 io_put_req(req);
2792 return HRTIMER_NORESTART;
2793}
2794
Jens Axboe47f46762019-11-09 17:43:02 -07002795static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
2796{
2797 struct io_kiocb *req;
2798 int ret = -ENOENT;
2799
2800 list_for_each_entry(req, &ctx->timeout_list, list) {
2801 if (user_data == req->user_data) {
2802 list_del_init(&req->list);
2803 ret = 0;
2804 break;
2805 }
2806 }
2807
2808 if (ret == -ENOENT)
2809 return ret;
2810
Jens Axboe2d283902019-12-04 11:08:05 -07002811 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe47f46762019-11-09 17:43:02 -07002812 if (ret == -1)
2813 return -EALREADY;
2814
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002815 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07002816 io_cqring_fill_event(req, -ECANCELED);
2817 io_put_req(req);
2818 return 0;
2819}
2820
Jens Axboe11365042019-10-16 09:08:32 -06002821/*
2822 * Remove or update an existing timeout command
2823 */
Jens Axboefc4df992019-12-10 14:38:45 -07002824static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06002825{
Jens Axboefc4df992019-12-10 14:38:45 -07002826 const struct io_uring_sqe *sqe = req->sqe;
Jens Axboe11365042019-10-16 09:08:32 -06002827 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe11365042019-10-16 09:08:32 -06002828 unsigned flags;
Jens Axboe47f46762019-11-09 17:43:02 -07002829 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06002830
2831 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2832 return -EINVAL;
2833 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
2834 return -EINVAL;
2835 flags = READ_ONCE(sqe->timeout_flags);
2836 if (flags)
2837 return -EINVAL;
2838
Jens Axboe11365042019-10-16 09:08:32 -06002839 spin_lock_irq(&ctx->completion_lock);
Jens Axboe47f46762019-11-09 17:43:02 -07002840 ret = io_timeout_cancel(ctx, READ_ONCE(sqe->addr));
Jens Axboe11365042019-10-16 09:08:32 -06002841
Jens Axboe47f46762019-11-09 17:43:02 -07002842 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06002843 io_commit_cqring(ctx);
2844 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06002845 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002846 if (ret < 0)
2847 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08002848 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06002849 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06002850}
2851
Jens Axboe2d283902019-12-04 11:08:05 -07002852static int io_timeout_prep(struct io_kiocb *req, struct io_async_ctx *io,
2853 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06002854{
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002855 const struct io_uring_sqe *sqe = req->sqe;
Jens Axboead8a48a2019-11-15 08:49:11 -07002856 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06002857 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06002858
Jens Axboead8a48a2019-11-15 08:49:11 -07002859 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06002860 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07002861 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06002862 return -EINVAL;
Jens Axboe2d283902019-12-04 11:08:05 -07002863 if (sqe->off && is_timeout_link)
2864 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06002865 flags = READ_ONCE(sqe->timeout_flags);
2866 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06002867 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06002868
Jens Axboe2d283902019-12-04 11:08:05 -07002869 data = &io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07002870 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07002871 req->flags |= REQ_F_TIMEOUT;
2872
2873 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06002874 return -EFAULT;
2875
Jens Axboe11365042019-10-16 09:08:32 -06002876 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07002877 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06002878 else
Jens Axboead8a48a2019-11-15 08:49:11 -07002879 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06002880
Jens Axboead8a48a2019-11-15 08:49:11 -07002881 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
2882 return 0;
2883}
2884
Jens Axboefc4df992019-12-10 14:38:45 -07002885static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07002886{
Jens Axboefc4df992019-12-10 14:38:45 -07002887 const struct io_uring_sqe *sqe = req->sqe;
Jens Axboead8a48a2019-11-15 08:49:11 -07002888 unsigned count;
2889 struct io_ring_ctx *ctx = req->ctx;
2890 struct io_timeout_data *data;
2891 struct list_head *entry;
2892 unsigned span = 0;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002893 int ret;
Jens Axboead8a48a2019-11-15 08:49:11 -07002894
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002895 if (!req->io) {
2896 if (io_alloc_async_ctx(req))
Jens Axboe2d283902019-12-04 11:08:05 -07002897 return -ENOMEM;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002898 ret = io_timeout_prep(req, req->io, false);
2899 if (ret)
Jens Axboe2d283902019-12-04 11:08:05 -07002900 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07002901 }
2902 data = &req->io->timeout;
Jens Axboe93bd25b2019-11-11 23:34:31 -07002903
Jens Axboe5262f562019-09-17 12:26:57 -06002904 /*
2905 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07002906 * timeout event to be satisfied. If it isn't set, then this is
2907 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06002908 */
2909 count = READ_ONCE(sqe->off);
Jens Axboe93bd25b2019-11-11 23:34:31 -07002910 if (!count) {
2911 req->flags |= REQ_F_TIMEOUT_NOSEQ;
2912 spin_lock_irq(&ctx->completion_lock);
2913 entry = ctx->timeout_list.prev;
2914 goto add;
2915 }
Jens Axboe5262f562019-09-17 12:26:57 -06002916
2917 req->sequence = ctx->cached_sq_head + count - 1;
Jens Axboe2d283902019-12-04 11:08:05 -07002918 data->seq_offset = count;
Jens Axboe5262f562019-09-17 12:26:57 -06002919
2920 /*
2921 * Insertion sort, ensuring the first entry in the list is always
2922 * the one we need first.
2923 */
Jens Axboe5262f562019-09-17 12:26:57 -06002924 spin_lock_irq(&ctx->completion_lock);
2925 list_for_each_prev(entry, &ctx->timeout_list) {
2926 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
yangerkun5da0fb12019-10-15 21:59:29 +08002927 unsigned nxt_sq_head;
2928 long long tmp, tmp_nxt;
Jens Axboe2d283902019-12-04 11:08:05 -07002929 u32 nxt_offset = nxt->io->timeout.seq_offset;
Jens Axboe5262f562019-09-17 12:26:57 -06002930
Jens Axboe93bd25b2019-11-11 23:34:31 -07002931 if (nxt->flags & REQ_F_TIMEOUT_NOSEQ)
2932 continue;
2933
yangerkun5da0fb12019-10-15 21:59:29 +08002934 /*
2935 * Since cached_sq_head + count - 1 can overflow, use type long
2936 * long to store it.
2937 */
2938 tmp = (long long)ctx->cached_sq_head + count - 1;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +03002939 nxt_sq_head = nxt->sequence - nxt_offset + 1;
2940 tmp_nxt = (long long)nxt_sq_head + nxt_offset - 1;
yangerkun5da0fb12019-10-15 21:59:29 +08002941
2942 /*
2943 * cached_sq_head may overflow, and it will never overflow twice
2944 * once there is some timeout req still be valid.
2945 */
2946 if (ctx->cached_sq_head < nxt_sq_head)
yangerkun8b07a652019-10-17 12:12:35 +08002947 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08002948
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002949 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06002950 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002951
2952 /*
2953 * Sequence of reqs after the insert one and itself should
2954 * be adjusted because each timeout req consumes a slot.
2955 */
2956 span++;
2957 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06002958 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002959 req->sequence -= span;
Jens Axboe93bd25b2019-11-11 23:34:31 -07002960add:
Jens Axboe5262f562019-09-17 12:26:57 -06002961 list_add(&req->list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07002962 data->timer.function = io_timeout_fn;
2963 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06002964 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06002965 return 0;
2966}
2967
Jens Axboe62755e32019-10-28 21:49:21 -06002968static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06002969{
Jens Axboe62755e32019-10-28 21:49:21 -06002970 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06002971
Jens Axboe62755e32019-10-28 21:49:21 -06002972 return req->user_data == (unsigned long) data;
2973}
2974
Jens Axboee977d6d2019-11-05 12:39:45 -07002975static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06002976{
Jens Axboe62755e32019-10-28 21:49:21 -06002977 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06002978 int ret = 0;
2979
Jens Axboe62755e32019-10-28 21:49:21 -06002980 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
2981 switch (cancel_ret) {
2982 case IO_WQ_CANCEL_OK:
2983 ret = 0;
2984 break;
2985 case IO_WQ_CANCEL_RUNNING:
2986 ret = -EALREADY;
2987 break;
2988 case IO_WQ_CANCEL_NOTFOUND:
2989 ret = -ENOENT;
2990 break;
2991 }
2992
Jens Axboee977d6d2019-11-05 12:39:45 -07002993 return ret;
2994}
2995
Jens Axboe47f46762019-11-09 17:43:02 -07002996static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
2997 struct io_kiocb *req, __u64 sqe_addr,
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002998 struct io_kiocb **nxt, int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07002999{
3000 unsigned long flags;
3001 int ret;
3002
3003 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
3004 if (ret != -ENOENT) {
3005 spin_lock_irqsave(&ctx->completion_lock, flags);
3006 goto done;
3007 }
3008
3009 spin_lock_irqsave(&ctx->completion_lock, flags);
3010 ret = io_timeout_cancel(ctx, sqe_addr);
3011 if (ret != -ENOENT)
3012 goto done;
3013 ret = io_poll_cancel(ctx, sqe_addr);
3014done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003015 if (!ret)
3016 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07003017 io_cqring_fill_event(req, ret);
3018 io_commit_cqring(ctx);
3019 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3020 io_cqring_ev_posted(ctx);
3021
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003022 if (ret < 0)
3023 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07003024 io_put_req_find_next(req, nxt);
3025}
3026
Jens Axboefbf23842019-12-17 18:45:56 -07003027static int io_async_cancel_prep(struct io_kiocb *req)
Jens Axboee977d6d2019-11-05 12:39:45 -07003028{
Jens Axboefc4df992019-12-10 14:38:45 -07003029 const struct io_uring_sqe *sqe = req->sqe;
Jens Axboee977d6d2019-11-05 12:39:45 -07003030
Jens Axboefbf23842019-12-17 18:45:56 -07003031 if (req->flags & REQ_F_PREPPED)
3032 return 0;
3033 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07003034 return -EINVAL;
3035 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
3036 sqe->cancel_flags)
3037 return -EINVAL;
3038
Jens Axboefbf23842019-12-17 18:45:56 -07003039 req->flags |= REQ_F_PREPPED;
3040 req->cancel.addr = READ_ONCE(sqe->addr);
3041 return 0;
3042}
3043
3044static int io_async_cancel(struct io_kiocb *req, struct io_kiocb **nxt)
3045{
3046 struct io_ring_ctx *ctx = req->ctx;
3047 int ret;
3048
3049 ret = io_async_cancel_prep(req);
3050 if (ret)
3051 return ret;
3052
3053 io_async_find_and_cancel(ctx, req, req->cancel.addr, nxt, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06003054 return 0;
3055}
3056
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003057static int io_req_defer_prep(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07003058{
3059 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003060 struct io_async_ctx *io = req->io;
Jens Axboef67676d2019-12-02 11:03:47 -07003061 struct iov_iter iter;
3062 ssize_t ret;
3063
Jens Axboef67676d2019-12-02 11:03:47 -07003064 switch (io->sqe.opcode) {
3065 case IORING_OP_READV:
3066 case IORING_OP_READ_FIXED:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003067 /* ensure prep does right import */
3068 req->io = NULL;
Jens Axboef67676d2019-12-02 11:03:47 -07003069 ret = io_read_prep(req, &iovec, &iter, true);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003070 req->io = io;
3071 if (ret < 0)
3072 break;
3073 io_req_map_rw(req, ret, iovec, inline_vecs, &iter);
3074 ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003075 break;
3076 case IORING_OP_WRITEV:
3077 case IORING_OP_WRITE_FIXED:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003078 /* ensure prep does right import */
3079 req->io = NULL;
Jens Axboef67676d2019-12-02 11:03:47 -07003080 ret = io_write_prep(req, &iovec, &iter, true);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003081 req->io = io;
3082 if (ret < 0)
3083 break;
3084 io_req_map_rw(req, ret, iovec, inline_vecs, &iter);
3085 ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003086 break;
Jens Axboe0969e782019-12-17 18:40:57 -07003087 case IORING_OP_POLL_ADD:
3088 ret = io_poll_add_prep(req);
3089 break;
3090 case IORING_OP_POLL_REMOVE:
3091 ret = io_poll_remove_prep(req);
3092 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003093 case IORING_OP_FSYNC:
3094 ret = io_prep_fsync(req);
3095 break;
3096 case IORING_OP_SYNC_FILE_RANGE:
3097 ret = io_prep_sfr(req);
3098 break;
Jens Axboe03b12302019-12-02 18:50:25 -07003099 case IORING_OP_SENDMSG:
3100 ret = io_sendmsg_prep(req, io);
3101 break;
3102 case IORING_OP_RECVMSG:
3103 ret = io_recvmsg_prep(req, io);
3104 break;
Jens Axboef499a022019-12-02 16:28:46 -07003105 case IORING_OP_CONNECT:
3106 ret = io_connect_prep(req, io);
3107 break;
Jens Axboe2d283902019-12-04 11:08:05 -07003108 case IORING_OP_TIMEOUT:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003109 ret = io_timeout_prep(req, io, false);
3110 break;
Jens Axboefbf23842019-12-17 18:45:56 -07003111 case IORING_OP_ASYNC_CANCEL:
3112 ret = io_async_cancel_prep(req);
3113 break;
Jens Axboe2d283902019-12-04 11:08:05 -07003114 case IORING_OP_LINK_TIMEOUT:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003115 ret = io_timeout_prep(req, io, true);
3116 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003117 case IORING_OP_ACCEPT:
3118 ret = io_accept_prep(req);
3119 break;
Jens Axboef67676d2019-12-02 11:03:47 -07003120 default:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003121 ret = 0;
3122 break;
Jens Axboef67676d2019-12-02 11:03:47 -07003123 }
3124
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003125 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003126}
3127
Jackie Liua197f662019-11-08 08:09:12 -07003128static int io_req_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -06003129{
Jackie Liua197f662019-11-08 08:09:12 -07003130 struct io_ring_ctx *ctx = req->ctx;
Jens Axboef67676d2019-12-02 11:03:47 -07003131 int ret;
Jens Axboede0617e2019-04-06 21:51:27 -06003132
Bob Liu9d858b22019-11-13 18:06:25 +08003133 /* Still need defer if there is pending req in defer list. */
3134 if (!req_need_defer(req) && list_empty(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06003135 return 0;
3136
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003137 if (io_alloc_async_ctx(req))
Jens Axboede0617e2019-04-06 21:51:27 -06003138 return -EAGAIN;
3139
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003140 ret = io_req_defer_prep(req);
3141 if (ret < 0)
Jens Axboe2d283902019-12-04 11:08:05 -07003142 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07003143
Jens Axboede0617e2019-04-06 21:51:27 -06003144 spin_lock_irq(&ctx->completion_lock);
Bob Liu9d858b22019-11-13 18:06:25 +08003145 if (!req_need_defer(req) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06003146 spin_unlock_irq(&ctx->completion_lock);
Jens Axboede0617e2019-04-06 21:51:27 -06003147 return 0;
3148 }
3149
Jens Axboe915967f2019-11-21 09:01:20 -07003150 trace_io_uring_defer(ctx, req, req->user_data);
Jens Axboede0617e2019-04-06 21:51:27 -06003151 list_add_tail(&req->list, &ctx->defer_list);
3152 spin_unlock_irq(&ctx->completion_lock);
3153 return -EIOCBQUEUED;
3154}
3155
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03003156__attribute__((nonnull))
Pavel Begunkovd7324472019-11-21 21:24:36 +03003157static int io_issue_sqe(struct io_kiocb *req, struct io_kiocb **nxt,
3158 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003159{
Jens Axboee0c5c572019-03-12 10:18:47 -06003160 int ret, opcode;
Jackie Liua197f662019-11-08 08:09:12 -07003161 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003162
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003163 opcode = READ_ONCE(req->sqe->opcode);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003164 switch (opcode) {
3165 case IORING_OP_NOP:
Jens Axboe78e19bb2019-11-06 15:21:34 -07003166 ret = io_nop(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003167 break;
3168 case IORING_OP_READV:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003169 if (unlikely(req->sqe->buf_index))
Jens Axboeedafcce2019-01-09 09:16:05 -07003170 return -EINVAL;
Pavel Begunkov267bc902019-11-07 01:41:08 +03003171 ret = io_read(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003172 break;
3173 case IORING_OP_WRITEV:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003174 if (unlikely(req->sqe->buf_index))
Jens Axboeedafcce2019-01-09 09:16:05 -07003175 return -EINVAL;
Pavel Begunkov267bc902019-11-07 01:41:08 +03003176 ret = io_write(req, nxt, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07003177 break;
3178 case IORING_OP_READ_FIXED:
Pavel Begunkov267bc902019-11-07 01:41:08 +03003179 ret = io_read(req, nxt, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07003180 break;
3181 case IORING_OP_WRITE_FIXED:
Pavel Begunkov267bc902019-11-07 01:41:08 +03003182 ret = io_write(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003183 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003184 case IORING_OP_FSYNC:
Jens Axboefc4df992019-12-10 14:38:45 -07003185 ret = io_fsync(req, nxt, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003186 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003187 case IORING_OP_POLL_ADD:
Jens Axboefc4df992019-12-10 14:38:45 -07003188 ret = io_poll_add(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003189 break;
3190 case IORING_OP_POLL_REMOVE:
Jens Axboefc4df992019-12-10 14:38:45 -07003191 ret = io_poll_remove(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003192 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003193 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboefc4df992019-12-10 14:38:45 -07003194 ret = io_sync_file_range(req, nxt, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003195 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003196 case IORING_OP_SENDMSG:
Jens Axboefc4df992019-12-10 14:38:45 -07003197 ret = io_sendmsg(req, nxt, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06003198 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06003199 case IORING_OP_RECVMSG:
Jens Axboefc4df992019-12-10 14:38:45 -07003200 ret = io_recvmsg(req, nxt, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06003201 break;
Jens Axboe5262f562019-09-17 12:26:57 -06003202 case IORING_OP_TIMEOUT:
Jens Axboefc4df992019-12-10 14:38:45 -07003203 ret = io_timeout(req);
Jens Axboe5262f562019-09-17 12:26:57 -06003204 break;
Jens Axboe11365042019-10-16 09:08:32 -06003205 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboefc4df992019-12-10 14:38:45 -07003206 ret = io_timeout_remove(req);
Jens Axboe11365042019-10-16 09:08:32 -06003207 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06003208 case IORING_OP_ACCEPT:
Jens Axboefc4df992019-12-10 14:38:45 -07003209 ret = io_accept(req, nxt, force_nonblock);
Jens Axboe17f2fe32019-10-17 14:42:58 -06003210 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003211 case IORING_OP_CONNECT:
Jens Axboefc4df992019-12-10 14:38:45 -07003212 ret = io_connect(req, nxt, force_nonblock);
Jens Axboef8e85cf2019-11-23 14:24:24 -07003213 break;
Jens Axboe62755e32019-10-28 21:49:21 -06003214 case IORING_OP_ASYNC_CANCEL:
Jens Axboefc4df992019-12-10 14:38:45 -07003215 ret = io_async_cancel(req, nxt);
Jens Axboe62755e32019-10-28 21:49:21 -06003216 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003217 default:
3218 ret = -EINVAL;
3219 break;
3220 }
3221
Jens Axboedef596e2019-01-09 08:59:42 -07003222 if (ret)
3223 return ret;
3224
3225 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe9e645e112019-05-10 16:07:28 -06003226 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07003227 return -EAGAIN;
3228
Jens Axboedef596e2019-01-09 08:59:42 -07003229 io_iopoll_req_issued(req);
Jens Axboedef596e2019-01-09 08:59:42 -07003230 }
3231
3232 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003233}
3234
Jens Axboeb76da702019-11-20 13:05:32 -07003235static void io_link_work_cb(struct io_wq_work **workptr)
3236{
3237 struct io_wq_work *work = *workptr;
3238 struct io_kiocb *link = work->data;
3239
3240 io_queue_linked_timeout(link);
3241 work->func = io_wq_submit_work;
3242}
3243
Jens Axboe561fb042019-10-24 07:25:42 -06003244static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboe31b51512019-01-18 22:56:34 -07003245{
Jens Axboe561fb042019-10-24 07:25:42 -06003246 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003247 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe561fb042019-10-24 07:25:42 -06003248 struct io_kiocb *nxt = NULL;
3249 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003250
Jens Axboe561fb042019-10-24 07:25:42 -06003251 /* Ensure we clear previously set non-block flag */
3252 req->rw.ki_flags &= ~IOCB_NOWAIT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003253
Jens Axboe561fb042019-10-24 07:25:42 -06003254 if (work->flags & IO_WQ_WORK_CANCEL)
3255 ret = -ECANCELED;
Jens Axboe31b51512019-01-18 22:56:34 -07003256
Jens Axboe561fb042019-10-24 07:25:42 -06003257 if (!ret) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003258 req->has_user = (work->flags & IO_WQ_WORK_HAS_MM) != 0;
3259 req->in_async = true;
Jens Axboe561fb042019-10-24 07:25:42 -06003260 do {
Pavel Begunkovd7324472019-11-21 21:24:36 +03003261 ret = io_issue_sqe(req, &nxt, false);
Jens Axboe561fb042019-10-24 07:25:42 -06003262 /*
3263 * We can get EAGAIN for polled IO even though we're
3264 * forcing a sync submission from here, since we can't
3265 * wait for request slots on the block side.
3266 */
3267 if (ret != -EAGAIN)
3268 break;
3269 cond_resched();
3270 } while (1);
3271 }
Jens Axboe31b51512019-01-18 22:56:34 -07003272
Jens Axboe561fb042019-10-24 07:25:42 -06003273 /* drop submission reference */
Jackie Liuec9c02a2019-11-08 23:50:36 +08003274 io_put_req(req);
Jens Axboe817869d2019-04-30 14:44:05 -06003275
Jens Axboe561fb042019-10-24 07:25:42 -06003276 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003277 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07003278 io_cqring_add_event(req, ret);
Jens Axboe817869d2019-04-30 14:44:05 -06003279 io_put_req(req);
Jens Axboeedafcce2019-01-09 09:16:05 -07003280 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003281
Jens Axboe561fb042019-10-24 07:25:42 -06003282 /* if a dependent link is ready, pass it back */
3283 if (!ret && nxt) {
Jens Axboe94ae5e72019-11-14 19:39:52 -07003284 struct io_kiocb *link;
3285
3286 io_prep_async_work(nxt, &link);
Jens Axboe561fb042019-10-24 07:25:42 -06003287 *workptr = &nxt->work;
Jens Axboeb76da702019-11-20 13:05:32 -07003288 if (link) {
3289 nxt->work.flags |= IO_WQ_WORK_CB;
3290 nxt->work.func = io_link_work_cb;
3291 nxt->work.data = link;
3292 }
Jens Axboeedafcce2019-01-09 09:16:05 -07003293 }
Jens Axboe31b51512019-01-18 22:56:34 -07003294}
Jens Axboe2b188cc2019-01-07 10:46:33 -07003295
Jens Axboe9e3aa612019-12-11 15:55:43 -07003296static bool io_req_op_valid(int op)
3297{
3298 return op >= IORING_OP_NOP && op < IORING_OP_LAST;
3299}
3300
3301static int io_op_needs_file(const struct io_uring_sqe *sqe)
Jens Axboe09bb8392019-03-13 12:39:28 -06003302{
3303 int op = READ_ONCE(sqe->opcode);
3304
3305 switch (op) {
3306 case IORING_OP_NOP:
3307 case IORING_OP_POLL_REMOVE:
Pavel Begunkov5683e542019-11-14 00:59:19 +03003308 case IORING_OP_TIMEOUT:
Pavel Begunkova320e9f2019-11-14 00:11:01 +03003309 case IORING_OP_TIMEOUT_REMOVE:
3310 case IORING_OP_ASYNC_CANCEL:
3311 case IORING_OP_LINK_TIMEOUT:
Jens Axboe9e3aa612019-12-11 15:55:43 -07003312 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06003313 default:
Jens Axboe9e3aa612019-12-11 15:55:43 -07003314 if (io_req_op_valid(op))
3315 return 1;
3316 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06003317 }
3318}
3319
Jens Axboe65e19f52019-10-26 07:20:21 -06003320static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
3321 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06003322{
Jens Axboe65e19f52019-10-26 07:20:21 -06003323 struct fixed_file_table *table;
3324
3325 table = &ctx->file_table[index >> IORING_FILE_TABLE_SHIFT];
3326 return table->files[index & IORING_FILE_TABLE_MASK];
3327}
3328
Jackie Liua197f662019-11-08 08:09:12 -07003329static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req)
Jens Axboe09bb8392019-03-13 12:39:28 -06003330{
Jackie Liua197f662019-11-08 08:09:12 -07003331 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe09bb8392019-03-13 12:39:28 -06003332 unsigned flags;
Jens Axboe9e3aa612019-12-11 15:55:43 -07003333 int fd, ret;
Jens Axboe09bb8392019-03-13 12:39:28 -06003334
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003335 flags = READ_ONCE(req->sqe->flags);
3336 fd = READ_ONCE(req->sqe->fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06003337
Jackie Liu4fe2c962019-09-09 20:50:40 +08003338 if (flags & IOSQE_IO_DRAIN)
Jens Axboede0617e2019-04-06 21:51:27 -06003339 req->flags |= REQ_F_IO_DRAIN;
Jens Axboede0617e2019-04-06 21:51:27 -06003340
Jens Axboe9e3aa612019-12-11 15:55:43 -07003341 ret = io_op_needs_file(req->sqe);
3342 if (ret <= 0)
3343 return ret;
Jens Axboe09bb8392019-03-13 12:39:28 -06003344
3345 if (flags & IOSQE_FIXED_FILE) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003346 if (unlikely(!ctx->file_table ||
Jens Axboe09bb8392019-03-13 12:39:28 -06003347 (unsigned) fd >= ctx->nr_user_files))
3348 return -EBADF;
Jens Axboeb7620122019-10-26 07:22:55 -06003349 fd = array_index_nospec(fd, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06003350 req->file = io_file_from_index(ctx, fd);
3351 if (!req->file)
Jens Axboe08a45172019-10-03 08:11:03 -06003352 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06003353 req->flags |= REQ_F_FIXED_FILE;
3354 } else {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003355 if (req->needs_fixed_file)
Jens Axboe09bb8392019-03-13 12:39:28 -06003356 return -EBADF;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02003357 trace_io_uring_file_get(ctx, fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06003358 req->file = io_file_get(state, fd);
3359 if (unlikely(!req->file))
3360 return -EBADF;
3361 }
3362
3363 return 0;
3364}
3365
Jackie Liua197f662019-11-08 08:09:12 -07003366static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003367{
Jens Axboefcb323c2019-10-24 12:39:47 -06003368 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07003369 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06003370
3371 rcu_read_lock();
3372 spin_lock_irq(&ctx->inflight_lock);
3373 /*
3374 * We use the f_ops->flush() handler to ensure that we can flush
3375 * out work accessing these files if the fd is closed. Check if
3376 * the fd has changed since we started down this path, and disallow
3377 * this operation if it has.
3378 */
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003379 if (fcheck(req->ring_fd) == req->ring_file) {
Jens Axboefcb323c2019-10-24 12:39:47 -06003380 list_add(&req->inflight_entry, &ctx->inflight_list);
3381 req->flags |= REQ_F_INFLIGHT;
3382 req->work.files = current->files;
3383 ret = 0;
3384 }
3385 spin_unlock_irq(&ctx->inflight_lock);
3386 rcu_read_unlock();
3387
3388 return ret;
3389}
3390
Jens Axboe2665abf2019-11-05 12:40:47 -07003391static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
3392{
Jens Axboead8a48a2019-11-15 08:49:11 -07003393 struct io_timeout_data *data = container_of(timer,
3394 struct io_timeout_data, timer);
3395 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07003396 struct io_ring_ctx *ctx = req->ctx;
3397 struct io_kiocb *prev = NULL;
3398 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07003399
3400 spin_lock_irqsave(&ctx->completion_lock, flags);
3401
3402 /*
3403 * We don't expect the list to be empty, that will only happen if we
3404 * race with the completion of the linked work.
3405 */
Pavel Begunkov44932332019-12-05 16:16:35 +03003406 if (!list_empty(&req->link_list)) {
3407 prev = list_entry(req->link_list.prev, struct io_kiocb,
3408 link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07003409 if (refcount_inc_not_zero(&prev->refs)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03003410 list_del_init(&req->link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07003411 prev->flags &= ~REQ_F_LINK_TIMEOUT;
3412 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07003413 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07003414 }
3415
3416 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3417
3418 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003419 req_set_fail_links(prev);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003420 io_async_find_and_cancel(ctx, req, prev->user_data, NULL,
3421 -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07003422 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07003423 } else {
3424 io_cqring_add_event(req, -ETIME);
3425 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07003426 }
Jens Axboe2665abf2019-11-05 12:40:47 -07003427 return HRTIMER_NORESTART;
3428}
3429
Jens Axboead8a48a2019-11-15 08:49:11 -07003430static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07003431{
Jens Axboe76a46e02019-11-10 23:34:16 -07003432 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07003433
Jens Axboe76a46e02019-11-10 23:34:16 -07003434 /*
3435 * If the list is now empty, then our linked request finished before
3436 * we got a chance to setup the timer
3437 */
3438 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov44932332019-12-05 16:16:35 +03003439 if (!list_empty(&req->link_list)) {
Jens Axboe2d283902019-12-04 11:08:05 -07003440 struct io_timeout_data *data = &req->io->timeout;
Jens Axboe94ae5e72019-11-14 19:39:52 -07003441
Jens Axboead8a48a2019-11-15 08:49:11 -07003442 data->timer.function = io_link_timeout_fn;
3443 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
3444 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07003445 }
Jens Axboe76a46e02019-11-10 23:34:16 -07003446 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07003447
Jens Axboe2665abf2019-11-05 12:40:47 -07003448 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07003449 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07003450}
3451
Jens Axboead8a48a2019-11-15 08:49:11 -07003452static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07003453{
3454 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003455
Jens Axboe2665abf2019-11-05 12:40:47 -07003456 if (!(req->flags & REQ_F_LINK))
3457 return NULL;
3458
Pavel Begunkov44932332019-12-05 16:16:35 +03003459 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
3460 link_list);
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003461 if (!nxt || nxt->sqe->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07003462 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07003463
Jens Axboe76a46e02019-11-10 23:34:16 -07003464 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07003465 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07003466}
3467
Jens Axboe0e0702d2019-11-14 21:42:10 -07003468static void __io_queue_sqe(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003469{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07003470 struct io_kiocb *linked_timeout;
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03003471 struct io_kiocb *nxt = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003472 int ret;
3473
Jens Axboe4a0a7a12019-12-09 20:01:01 -07003474again:
3475 linked_timeout = io_prep_linked_timeout(req);
3476
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03003477 ret = io_issue_sqe(req, &nxt, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06003478
3479 /*
3480 * We async punt it if the file wasn't marked NOWAIT, or if the file
3481 * doesn't support non-blocking read/write attempts
3482 */
3483 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
3484 (req->flags & REQ_F_MUST_PUNT))) {
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03003485 if (req->work.flags & IO_WQ_WORK_NEEDS_FILES) {
3486 ret = io_grab_files(req);
3487 if (ret)
3488 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003489 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03003490
3491 /*
3492 * Queued up for async execution, worker will release
3493 * submit reference when the iocb is actually submitted.
3494 */
3495 io_queue_async_work(req);
Jens Axboe4a0a7a12019-12-09 20:01:01 -07003496 goto done_req;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003497 }
Jens Axboee65ef562019-03-12 10:16:44 -06003498
Jens Axboefcb323c2019-10-24 12:39:47 -06003499err:
Jens Axboee65ef562019-03-12 10:16:44 -06003500 /* drop submission reference */
3501 io_put_req(req);
3502
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03003503 if (linked_timeout) {
Jens Axboe76a46e02019-11-10 23:34:16 -07003504 if (!ret)
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03003505 io_queue_linked_timeout(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07003506 else
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03003507 io_put_req(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07003508 }
3509
Jens Axboee65ef562019-03-12 10:16:44 -06003510 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06003511 if (ret) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07003512 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003513 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06003514 io_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06003515 }
Jens Axboe4a0a7a12019-12-09 20:01:01 -07003516done_req:
3517 if (nxt) {
3518 req = nxt;
3519 nxt = NULL;
3520 goto again;
3521 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003522}
3523
Jens Axboe0e0702d2019-11-14 21:42:10 -07003524static void io_queue_sqe(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08003525{
3526 int ret;
3527
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003528 if (unlikely(req->ctx->drain_next)) {
3529 req->flags |= REQ_F_IO_DRAIN;
3530 req->ctx->drain_next = false;
3531 }
3532 req->ctx->drain_next = (req->flags & REQ_F_DRAIN_LINK);
3533
Jackie Liua197f662019-11-08 08:09:12 -07003534 ret = io_req_defer(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08003535 if (ret) {
3536 if (ret != -EIOCBQUEUED) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07003537 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003538 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07003539 io_double_put_req(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08003540 }
Jens Axboe0e0702d2019-11-14 21:42:10 -07003541 } else
3542 __io_queue_sqe(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08003543}
3544
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003545static inline void io_queue_link_head(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08003546{
Jens Axboe94ae5e72019-11-14 19:39:52 -07003547 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003548 io_cqring_add_event(req, -ECANCELED);
3549 io_double_put_req(req);
3550 } else
Jens Axboe0e0702d2019-11-14 21:42:10 -07003551 io_queue_sqe(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08003552}
3553
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003554#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
3555 IOSQE_IO_HARDLINK)
Jens Axboe9e645e112019-05-10 16:07:28 -06003556
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03003557static bool io_submit_sqe(struct io_kiocb *req, struct io_submit_state *state,
Jackie Liua197f662019-11-08 08:09:12 -07003558 struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06003559{
Jackie Liua197f662019-11-08 08:09:12 -07003560 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -06003561 int ret;
3562
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003563 req->user_data = req->sqe->user_data;
Jens Axboe78e19bb2019-11-06 15:21:34 -07003564
Jens Axboe9e645e112019-05-10 16:07:28 -06003565 /* enforce forwards compatibility on users */
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003566 if (unlikely(req->sqe->flags & ~SQE_VALID_FLAGS)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06003567 ret = -EINVAL;
Pavel Begunkov196be952019-11-07 01:41:06 +03003568 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06003569 }
3570
Jackie Liua197f662019-11-08 08:09:12 -07003571 ret = io_req_set_file(state, req);
Jens Axboe9e645e112019-05-10 16:07:28 -06003572 if (unlikely(ret)) {
3573err_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07003574 io_cqring_add_event(req, ret);
3575 io_double_put_req(req);
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03003576 return false;
Jens Axboe9e645e112019-05-10 16:07:28 -06003577 }
3578
Jens Axboe9e645e112019-05-10 16:07:28 -06003579 /*
3580 * If we already have a head request, queue this one for async
3581 * submittal once the head completes. If we don't have a head but
3582 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
3583 * submitted sync once the chain is complete. If none of those
3584 * conditions are true (normal request), then just queue it.
3585 */
3586 if (*link) {
3587 struct io_kiocb *prev = *link;
3588
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003589 if (req->sqe->flags & IOSQE_IO_DRAIN)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003590 (*link)->flags |= REQ_F_DRAIN_LINK | REQ_F_IO_DRAIN;
3591
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003592 if (req->sqe->flags & IOSQE_IO_HARDLINK)
3593 req->flags |= REQ_F_HARDLINK;
3594
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003595 if (io_alloc_async_ctx(req)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06003596 ret = -EAGAIN;
3597 goto err_req;
3598 }
3599
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003600 ret = io_req_defer_prep(req);
Jens Axboe2d283902019-12-04 11:08:05 -07003601 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003602 /* fail even hard links since we don't submit */
Jens Axboe2d283902019-12-04 11:08:05 -07003603 prev->flags |= REQ_F_FAIL_LINK;
Jens Axboef67676d2019-12-02 11:03:47 -07003604 goto err_req;
Jens Axboe2d283902019-12-04 11:08:05 -07003605 }
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02003606 trace_io_uring_link(ctx, req, prev);
Pavel Begunkov44932332019-12-05 16:16:35 +03003607 list_add_tail(&req->link_list, &prev->link_list);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003608 } else if (req->sqe->flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06003609 req->flags |= REQ_F_LINK;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003610 if (req->sqe->flags & IOSQE_IO_HARDLINK)
3611 req->flags |= REQ_F_HARDLINK;
Jens Axboe9e645e112019-05-10 16:07:28 -06003612
Jens Axboe9e645e112019-05-10 16:07:28 -06003613 INIT_LIST_HEAD(&req->link_list);
3614 *link = req;
3615 } else {
Jackie Liua197f662019-11-08 08:09:12 -07003616 io_queue_sqe(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06003617 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03003618
3619 return true;
Jens Axboe9e645e112019-05-10 16:07:28 -06003620}
3621
Jens Axboe9a56a232019-01-09 09:06:50 -07003622/*
3623 * Batched submission is done, ensure local IO is flushed out.
3624 */
3625static void io_submit_state_end(struct io_submit_state *state)
3626{
3627 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06003628 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07003629 if (state->free_reqs)
3630 kmem_cache_free_bulk(req_cachep, state->free_reqs,
3631 &state->reqs[state->cur_req]);
Jens Axboe9a56a232019-01-09 09:06:50 -07003632}
3633
3634/*
3635 * Start submission side cache.
3636 */
3637static void io_submit_state_start(struct io_submit_state *state,
Jackie Liu22efde52019-12-02 17:14:52 +08003638 unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07003639{
3640 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07003641 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07003642 state->file = NULL;
3643 state->ios_left = max_ios;
3644}
3645
Jens Axboe2b188cc2019-01-07 10:46:33 -07003646static void io_commit_sqring(struct io_ring_ctx *ctx)
3647{
Hristo Venev75b28af2019-08-26 17:23:46 +00003648 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003649
Hristo Venev75b28af2019-08-26 17:23:46 +00003650 if (ctx->cached_sq_head != READ_ONCE(rings->sq.head)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07003651 /*
3652 * Ensure any loads from the SQEs are done at this point,
3653 * since once we write the new head, the application could
3654 * write new data to them.
3655 */
Hristo Venev75b28af2019-08-26 17:23:46 +00003656 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003657 }
3658}
3659
3660/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08003661 * Fetch an sqe, if one is available. Note that req->sqe will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07003662 * that is mapped by userspace. This means that care needs to be taken to
3663 * ensure that reads are stable, as we cannot rely on userspace always
3664 * being a good citizen. If members of the sqe are validated and then later
3665 * used, it's important that those reads are done through READ_ONCE() to
3666 * prevent a re-load down the line.
3667 */
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003668static bool io_get_sqring(struct io_ring_ctx *ctx, struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003669{
Hristo Venev75b28af2019-08-26 17:23:46 +00003670 struct io_rings *rings = ctx->rings;
3671 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003672 unsigned head;
3673
3674 /*
3675 * The cached sq head (or cq tail) serves two purposes:
3676 *
3677 * 1) allows us to batch the cost of updating the user visible
3678 * head updates.
3679 * 2) allows the kernel side to track the head on its own, even
3680 * though the application is the one updating it.
3681 */
3682 head = ctx->cached_sq_head;
Stefan Bühlere523a292019-04-19 11:57:44 +02003683 /* make sure SQ entry isn't read before tail */
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03003684 if (unlikely(head == smp_load_acquire(&rings->sq.tail)))
Jens Axboe2b188cc2019-01-07 10:46:33 -07003685 return false;
3686
Hristo Venev75b28af2019-08-26 17:23:46 +00003687 head = READ_ONCE(sq_array[head & ctx->sq_mask]);
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03003688 if (likely(head < ctx->sq_entries)) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003689 /*
3690 * All io need record the previous position, if LINK vs DARIN,
3691 * it can be used to mark the position of the first IO in the
3692 * link list.
3693 */
3694 req->sequence = ctx->cached_sq_head;
3695 req->sqe = &ctx->sq_sqes[head];
Jens Axboe2b188cc2019-01-07 10:46:33 -07003696 ctx->cached_sq_head++;
3697 return true;
3698 }
3699
3700 /* drop invalid entries */
3701 ctx->cached_sq_head++;
Jens Axboe498ccd92019-10-25 10:04:25 -06003702 ctx->cached_sq_dropped++;
3703 WRITE_ONCE(rings->sq_dropped, ctx->cached_sq_dropped);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003704 return false;
3705}
3706
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003707static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkovae9428c2019-11-06 00:22:14 +03003708 struct file *ring_file, int ring_fd,
3709 struct mm_struct **mm, bool async)
Jens Axboe6c271ce2019-01-10 11:22:30 -07003710{
3711 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06003712 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06003713 int i, submitted = 0;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03003714 bool mm_fault = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003715
Jens Axboec4a2ed72019-11-21 21:01:26 -07003716 /* if we have a backlog and couldn't flush it all, return BUSY */
3717 if (!list_empty(&ctx->cq_overflow_list) &&
3718 !io_cqring_overflow_flush(ctx, false))
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003719 return -EBUSY;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003720
3721 if (nr > IO_PLUG_THRESHOLD) {
Jackie Liu22efde52019-12-02 17:14:52 +08003722 io_submit_state_start(&state, nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003723 statep = &state;
3724 }
3725
3726 for (i = 0; i < nr; i++) {
Pavel Begunkov196be952019-11-07 01:41:06 +03003727 struct io_kiocb *req;
Pavel Begunkov50585b92019-11-07 01:41:07 +03003728 unsigned int sqe_flags;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003729
Pavel Begunkov196be952019-11-07 01:41:06 +03003730 req = io_get_req(ctx, statep);
3731 if (unlikely(!req)) {
3732 if (!submitted)
3733 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003734 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06003735 }
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003736 if (!io_get_sqring(ctx, req)) {
Pavel Begunkov196be952019-11-07 01:41:06 +03003737 __io_free_req(req);
3738 break;
3739 }
Jens Axboe9e645e112019-05-10 16:07:28 -06003740
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003741 if (io_sqe_needs_user(req->sqe) && !*mm) {
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03003742 mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm);
3743 if (!mm_fault) {
3744 use_mm(ctx->sqo_mm);
3745 *mm = ctx->sqo_mm;
3746 }
3747 }
3748
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03003749 submitted++;
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003750 sqe_flags = req->sqe->flags;
Pavel Begunkov50585b92019-11-07 01:41:07 +03003751
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003752 req->ring_file = ring_file;
3753 req->ring_fd = ring_fd;
3754 req->has_user = *mm != NULL;
3755 req->in_async = async;
3756 req->needs_fixed_file = async;
3757 trace_io_uring_submit_sqe(ctx, req->sqe->user_data,
Pavel Begunkov50585b92019-11-07 01:41:07 +03003758 true, async);
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03003759 if (!io_submit_sqe(req, statep, &link))
3760 break;
Pavel Begunkove5eb6362019-11-06 00:22:15 +03003761 /*
3762 * If previous wasn't linked and we have a linked command,
3763 * that's the end of the chain. Submit the previous link.
3764 */
Pavel Begunkovffbb8d62019-12-17 20:57:05 +03003765 if (!(sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK)) && link) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003766 io_queue_link_head(link);
Pavel Begunkove5eb6362019-11-06 00:22:15 +03003767 link = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003768 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07003769 }
3770
Jens Axboe9e645e112019-05-10 16:07:28 -06003771 if (link)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003772 io_queue_link_head(link);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003773 if (statep)
3774 io_submit_state_end(&state);
3775
Pavel Begunkovae9428c2019-11-06 00:22:14 +03003776 /* Commit SQ ring head once we've consumed and submitted all SQEs */
3777 io_commit_sqring(ctx);
3778
Jens Axboe6c271ce2019-01-10 11:22:30 -07003779 return submitted;
3780}
3781
3782static int io_sq_thread(void *data)
3783{
Jens Axboe6c271ce2019-01-10 11:22:30 -07003784 struct io_ring_ctx *ctx = data;
3785 struct mm_struct *cur_mm = NULL;
Jens Axboe181e4482019-11-25 08:52:30 -07003786 const struct cred *old_cred;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003787 mm_segment_t old_fs;
3788 DEFINE_WAIT(wait);
3789 unsigned inflight;
3790 unsigned long timeout;
Jens Axboec1edbf52019-11-10 16:56:04 -07003791 int ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003792
Jens Axboe206aefd2019-11-07 18:27:42 -07003793 complete(&ctx->completions[1]);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08003794
Jens Axboe6c271ce2019-01-10 11:22:30 -07003795 old_fs = get_fs();
3796 set_fs(USER_DS);
Jens Axboe181e4482019-11-25 08:52:30 -07003797 old_cred = override_creds(ctx->creds);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003798
Jens Axboec1edbf52019-11-10 16:56:04 -07003799 ret = timeout = inflight = 0;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003800 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003801 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003802
3803 if (inflight) {
3804 unsigned nr_events = 0;
3805
3806 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe2b2ed972019-10-25 10:06:15 -06003807 /*
3808 * inflight is the count of the maximum possible
3809 * entries we submitted, but it can be smaller
3810 * if we dropped some of them. If we don't have
3811 * poll entries available, then we know that we
3812 * have nothing left to poll for. Reset the
3813 * inflight count to zero in that case.
3814 */
3815 mutex_lock(&ctx->uring_lock);
3816 if (!list_empty(&ctx->poll_list))
3817 __io_iopoll_check(ctx, &nr_events, 0);
3818 else
3819 inflight = 0;
3820 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003821 } else {
3822 /*
3823 * Normal IO, just pretend everything completed.
3824 * We don't have to poll completions for that.
3825 */
3826 nr_events = inflight;
3827 }
3828
3829 inflight -= nr_events;
3830 if (!inflight)
3831 timeout = jiffies + ctx->sq_thread_idle;
3832 }
3833
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003834 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07003835
3836 /*
3837 * If submit got -EBUSY, flag us as needing the application
3838 * to enter the kernel to reap and flush events.
3839 */
3840 if (!to_submit || ret == -EBUSY) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07003841 /*
3842 * We're polling. If we're within the defined idle
3843 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07003844 * to sleep. The exception is if we got EBUSY doing
3845 * more IO, we should wait for the application to
3846 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07003847 */
Jens Axboec1edbf52019-11-10 16:56:04 -07003848 if (inflight ||
3849 (!time_after(jiffies, timeout) && ret != -EBUSY)) {
Jens Axboe9831a902019-09-19 09:48:55 -06003850 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07003851 continue;
3852 }
3853
3854 /*
3855 * Drop cur_mm before scheduling, we can't hold it for
3856 * long periods (or over schedule()). Do this before
3857 * adding ourselves to the waitqueue, as the unuse/drop
3858 * may sleep.
3859 */
3860 if (cur_mm) {
3861 unuse_mm(cur_mm);
3862 mmput(cur_mm);
3863 cur_mm = NULL;
3864 }
3865
3866 prepare_to_wait(&ctx->sqo_wait, &wait,
3867 TASK_INTERRUPTIBLE);
3868
3869 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00003870 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02003871 /* make sure to read SQ tail after writing flags */
3872 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07003873
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003874 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07003875 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003876 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07003877 finish_wait(&ctx->sqo_wait, &wait);
3878 break;
3879 }
3880 if (signal_pending(current))
3881 flush_signals(current);
3882 schedule();
3883 finish_wait(&ctx->sqo_wait, &wait);
3884
Hristo Venev75b28af2019-08-26 17:23:46 +00003885 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003886 continue;
3887 }
3888 finish_wait(&ctx->sqo_wait, &wait);
3889
Hristo Venev75b28af2019-08-26 17:23:46 +00003890 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003891 }
3892
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003893 to_submit = min(to_submit, ctx->sq_entries);
Jens Axboe8a4955f2019-12-09 14:52:35 -07003894 mutex_lock(&ctx->uring_lock);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003895 ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true);
Jens Axboe8a4955f2019-12-09 14:52:35 -07003896 mutex_unlock(&ctx->uring_lock);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003897 if (ret > 0)
3898 inflight += ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003899 }
3900
3901 set_fs(old_fs);
3902 if (cur_mm) {
3903 unuse_mm(cur_mm);
3904 mmput(cur_mm);
3905 }
Jens Axboe181e4482019-11-25 08:52:30 -07003906 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06003907
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003908 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06003909
Jens Axboe6c271ce2019-01-10 11:22:30 -07003910 return 0;
3911}
3912
Jens Axboebda52162019-09-24 13:47:15 -06003913struct io_wait_queue {
3914 struct wait_queue_entry wq;
3915 struct io_ring_ctx *ctx;
3916 unsigned to_wait;
3917 unsigned nr_timeouts;
3918};
3919
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003920static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06003921{
3922 struct io_ring_ctx *ctx = iowq->ctx;
3923
3924 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08003925 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06003926 * started waiting. For timeouts, we always want to return to userspace,
3927 * regardless of event count.
3928 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003929 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06003930 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
3931}
3932
3933static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
3934 int wake_flags, void *key)
3935{
3936 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
3937 wq);
3938
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003939 /* use noflush == true, as we can't safely rely on locking context */
3940 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06003941 return -1;
3942
3943 return autoremove_wake_function(curr, mode, wake_flags, key);
3944}
3945
Jens Axboe2b188cc2019-01-07 10:46:33 -07003946/*
3947 * Wait until events become available, if we don't already have some. The
3948 * application must reap them itself, as they reside on the shared cq ring.
3949 */
3950static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
3951 const sigset_t __user *sig, size_t sigsz)
3952{
Jens Axboebda52162019-09-24 13:47:15 -06003953 struct io_wait_queue iowq = {
3954 .wq = {
3955 .private = current,
3956 .func = io_wake_function,
3957 .entry = LIST_HEAD_INIT(iowq.wq.entry),
3958 },
3959 .ctx = ctx,
3960 .to_wait = min_events,
3961 };
Hristo Venev75b28af2019-08-26 17:23:46 +00003962 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08003963 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003964
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003965 if (io_cqring_events(ctx, false) >= min_events)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003966 return 0;
3967
3968 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01003969#ifdef CONFIG_COMPAT
3970 if (in_compat_syscall())
3971 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07003972 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01003973 else
3974#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07003975 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01003976
Jens Axboe2b188cc2019-01-07 10:46:33 -07003977 if (ret)
3978 return ret;
3979 }
3980
Jens Axboebda52162019-09-24 13:47:15 -06003981 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02003982 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06003983 do {
3984 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
3985 TASK_INTERRUPTIBLE);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003986 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06003987 break;
3988 schedule();
3989 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08003990 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06003991 break;
3992 }
3993 } while (1);
3994 finish_wait(&ctx->wait, &iowq.wq);
3995
Jackie Liue9ffa5c2019-10-29 11:16:42 +08003996 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003997
Hristo Venev75b28af2019-08-26 17:23:46 +00003998 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003999}
4000
Jens Axboe6b063142019-01-10 22:13:58 -07004001static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
4002{
4003#if defined(CONFIG_UNIX)
4004 if (ctx->ring_sock) {
4005 struct sock *sock = ctx->ring_sock->sk;
4006 struct sk_buff *skb;
4007
4008 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
4009 kfree_skb(skb);
4010 }
4011#else
4012 int i;
4013
Jens Axboe65e19f52019-10-26 07:20:21 -06004014 for (i = 0; i < ctx->nr_user_files; i++) {
4015 struct file *file;
4016
4017 file = io_file_from_index(ctx, i);
4018 if (file)
4019 fput(file);
4020 }
Jens Axboe6b063142019-01-10 22:13:58 -07004021#endif
4022}
4023
4024static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
4025{
Jens Axboe65e19f52019-10-26 07:20:21 -06004026 unsigned nr_tables, i;
4027
4028 if (!ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07004029 return -ENXIO;
4030
4031 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06004032 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
4033 for (i = 0; i < nr_tables; i++)
4034 kfree(ctx->file_table[i].files);
4035 kfree(ctx->file_table);
4036 ctx->file_table = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07004037 ctx->nr_user_files = 0;
4038 return 0;
4039}
4040
Jens Axboe6c271ce2019-01-10 11:22:30 -07004041static void io_sq_thread_stop(struct io_ring_ctx *ctx)
4042{
4043 if (ctx->sqo_thread) {
Jens Axboe206aefd2019-11-07 18:27:42 -07004044 wait_for_completion(&ctx->completions[1]);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02004045 /*
4046 * The park is a bit of a work-around, without it we get
4047 * warning spews on shutdown with SQPOLL set and affinity
4048 * set to a single CPU.
4049 */
Jens Axboe06058632019-04-13 09:26:03 -06004050 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004051 kthread_stop(ctx->sqo_thread);
4052 ctx->sqo_thread = NULL;
4053 }
4054}
4055
Jens Axboe6b063142019-01-10 22:13:58 -07004056static void io_finish_async(struct io_ring_ctx *ctx)
4057{
Jens Axboe6c271ce2019-01-10 11:22:30 -07004058 io_sq_thread_stop(ctx);
4059
Jens Axboe561fb042019-10-24 07:25:42 -06004060 if (ctx->io_wq) {
4061 io_wq_destroy(ctx->io_wq);
4062 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07004063 }
4064}
4065
4066#if defined(CONFIG_UNIX)
4067static void io_destruct_skb(struct sk_buff *skb)
4068{
4069 struct io_ring_ctx *ctx = skb->sk->sk_user_data;
4070
Jens Axboe561fb042019-10-24 07:25:42 -06004071 if (ctx->io_wq)
4072 io_wq_flush(ctx->io_wq);
Jens Axboe8a997342019-10-09 14:40:13 -06004073
Jens Axboe6b063142019-01-10 22:13:58 -07004074 unix_destruct_scm(skb);
4075}
4076
4077/*
4078 * Ensure the UNIX gc is aware of our file set, so we are certain that
4079 * the io_uring can be safely unregistered on process exit, even if we have
4080 * loops in the file referencing.
4081 */
4082static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
4083{
4084 struct sock *sk = ctx->ring_sock->sk;
4085 struct scm_fp_list *fpl;
4086 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06004087 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07004088
4089 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
4090 unsigned long inflight = ctx->user->unix_inflight + nr;
4091
4092 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
4093 return -EMFILE;
4094 }
4095
4096 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
4097 if (!fpl)
4098 return -ENOMEM;
4099
4100 skb = alloc_skb(0, GFP_KERNEL);
4101 if (!skb) {
4102 kfree(fpl);
4103 return -ENOMEM;
4104 }
4105
4106 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07004107
Jens Axboe08a45172019-10-03 08:11:03 -06004108 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07004109 fpl->user = get_uid(ctx->user);
4110 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06004111 struct file *file = io_file_from_index(ctx, i + offset);
4112
4113 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06004114 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06004115 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06004116 unix_inflight(fpl->user, fpl->fp[nr_files]);
4117 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07004118 }
4119
Jens Axboe08a45172019-10-03 08:11:03 -06004120 if (nr_files) {
4121 fpl->max = SCM_MAX_FD;
4122 fpl->count = nr_files;
4123 UNIXCB(skb).fp = fpl;
4124 skb->destructor = io_destruct_skb;
4125 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
4126 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07004127
Jens Axboe08a45172019-10-03 08:11:03 -06004128 for (i = 0; i < nr_files; i++)
4129 fput(fpl->fp[i]);
4130 } else {
4131 kfree_skb(skb);
4132 kfree(fpl);
4133 }
Jens Axboe6b063142019-01-10 22:13:58 -07004134
4135 return 0;
4136}
4137
4138/*
4139 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
4140 * causes regular reference counting to break down. We rely on the UNIX
4141 * garbage collection to take care of this problem for us.
4142 */
4143static int io_sqe_files_scm(struct io_ring_ctx *ctx)
4144{
4145 unsigned left, total;
4146 int ret = 0;
4147
4148 total = 0;
4149 left = ctx->nr_user_files;
4150 while (left) {
4151 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07004152
4153 ret = __io_sqe_files_scm(ctx, this_files, total);
4154 if (ret)
4155 break;
4156 left -= this_files;
4157 total += this_files;
4158 }
4159
4160 if (!ret)
4161 return 0;
4162
4163 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06004164 struct file *file = io_file_from_index(ctx, total);
4165
4166 if (file)
4167 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07004168 total++;
4169 }
4170
4171 return ret;
4172}
4173#else
4174static int io_sqe_files_scm(struct io_ring_ctx *ctx)
4175{
4176 return 0;
4177}
4178#endif
4179
Jens Axboe65e19f52019-10-26 07:20:21 -06004180static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
4181 unsigned nr_files)
4182{
4183 int i;
4184
4185 for (i = 0; i < nr_tables; i++) {
4186 struct fixed_file_table *table = &ctx->file_table[i];
4187 unsigned this_files;
4188
4189 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
4190 table->files = kcalloc(this_files, sizeof(struct file *),
4191 GFP_KERNEL);
4192 if (!table->files)
4193 break;
4194 nr_files -= this_files;
4195 }
4196
4197 if (i == nr_tables)
4198 return 0;
4199
4200 for (i = 0; i < nr_tables; i++) {
4201 struct fixed_file_table *table = &ctx->file_table[i];
4202 kfree(table->files);
4203 }
4204 return 1;
4205}
4206
Jens Axboe6b063142019-01-10 22:13:58 -07004207static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
4208 unsigned nr_args)
4209{
4210 __s32 __user *fds = (__s32 __user *) arg;
Jens Axboe65e19f52019-10-26 07:20:21 -06004211 unsigned nr_tables;
Jens Axboe6b063142019-01-10 22:13:58 -07004212 int fd, ret = 0;
4213 unsigned i;
4214
Jens Axboe65e19f52019-10-26 07:20:21 -06004215 if (ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07004216 return -EBUSY;
4217 if (!nr_args)
4218 return -EINVAL;
4219 if (nr_args > IORING_MAX_FIXED_FILES)
4220 return -EMFILE;
4221
Jens Axboe65e19f52019-10-26 07:20:21 -06004222 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
4223 ctx->file_table = kcalloc(nr_tables, sizeof(struct fixed_file_table),
4224 GFP_KERNEL);
4225 if (!ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07004226 return -ENOMEM;
4227
Jens Axboe65e19f52019-10-26 07:20:21 -06004228 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
4229 kfree(ctx->file_table);
Jens Axboe46568e92019-11-10 08:40:53 -07004230 ctx->file_table = NULL;
Jens Axboe65e19f52019-10-26 07:20:21 -06004231 return -ENOMEM;
4232 }
4233
Jens Axboe08a45172019-10-03 08:11:03 -06004234 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06004235 struct fixed_file_table *table;
4236 unsigned index;
4237
Jens Axboe6b063142019-01-10 22:13:58 -07004238 ret = -EFAULT;
4239 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
4240 break;
Jens Axboe08a45172019-10-03 08:11:03 -06004241 /* allow sparse sets */
4242 if (fd == -1) {
4243 ret = 0;
4244 continue;
4245 }
Jens Axboe6b063142019-01-10 22:13:58 -07004246
Jens Axboe65e19f52019-10-26 07:20:21 -06004247 table = &ctx->file_table[i >> IORING_FILE_TABLE_SHIFT];
4248 index = i & IORING_FILE_TABLE_MASK;
4249 table->files[index] = fget(fd);
Jens Axboe6b063142019-01-10 22:13:58 -07004250
4251 ret = -EBADF;
Jens Axboe65e19f52019-10-26 07:20:21 -06004252 if (!table->files[index])
Jens Axboe6b063142019-01-10 22:13:58 -07004253 break;
4254 /*
4255 * Don't allow io_uring instances to be registered. If UNIX
4256 * isn't enabled, then this causes a reference cycle and this
4257 * instance can never get freed. If UNIX is enabled we'll
4258 * handle it just fine, but there's still no point in allowing
4259 * a ring fd as it doesn't support regular read/write anyway.
4260 */
Jens Axboe65e19f52019-10-26 07:20:21 -06004261 if (table->files[index]->f_op == &io_uring_fops) {
4262 fput(table->files[index]);
Jens Axboe6b063142019-01-10 22:13:58 -07004263 break;
4264 }
Jens Axboe6b063142019-01-10 22:13:58 -07004265 ret = 0;
4266 }
4267
4268 if (ret) {
Jens Axboe65e19f52019-10-26 07:20:21 -06004269 for (i = 0; i < ctx->nr_user_files; i++) {
4270 struct file *file;
Jens Axboe6b063142019-01-10 22:13:58 -07004271
Jens Axboe65e19f52019-10-26 07:20:21 -06004272 file = io_file_from_index(ctx, i);
4273 if (file)
4274 fput(file);
4275 }
4276 for (i = 0; i < nr_tables; i++)
4277 kfree(ctx->file_table[i].files);
4278
4279 kfree(ctx->file_table);
4280 ctx->file_table = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07004281 ctx->nr_user_files = 0;
4282 return ret;
4283 }
4284
4285 ret = io_sqe_files_scm(ctx);
4286 if (ret)
4287 io_sqe_files_unregister(ctx);
4288
4289 return ret;
4290}
4291
Jens Axboec3a31e62019-10-03 13:59:56 -06004292static void io_sqe_file_unregister(struct io_ring_ctx *ctx, int index)
4293{
4294#if defined(CONFIG_UNIX)
Jens Axboe65e19f52019-10-26 07:20:21 -06004295 struct file *file = io_file_from_index(ctx, index);
Jens Axboec3a31e62019-10-03 13:59:56 -06004296 struct sock *sock = ctx->ring_sock->sk;
4297 struct sk_buff_head list, *head = &sock->sk_receive_queue;
4298 struct sk_buff *skb;
4299 int i;
4300
4301 __skb_queue_head_init(&list);
4302
4303 /*
4304 * Find the skb that holds this file in its SCM_RIGHTS. When found,
4305 * remove this entry and rearrange the file array.
4306 */
4307 skb = skb_dequeue(head);
4308 while (skb) {
4309 struct scm_fp_list *fp;
4310
4311 fp = UNIXCB(skb).fp;
4312 for (i = 0; i < fp->count; i++) {
4313 int left;
4314
4315 if (fp->fp[i] != file)
4316 continue;
4317
4318 unix_notinflight(fp->user, fp->fp[i]);
4319 left = fp->count - 1 - i;
4320 if (left) {
4321 memmove(&fp->fp[i], &fp->fp[i + 1],
4322 left * sizeof(struct file *));
4323 }
4324 fp->count--;
4325 if (!fp->count) {
4326 kfree_skb(skb);
4327 skb = NULL;
4328 } else {
4329 __skb_queue_tail(&list, skb);
4330 }
4331 fput(file);
4332 file = NULL;
4333 break;
4334 }
4335
4336 if (!file)
4337 break;
4338
4339 __skb_queue_tail(&list, skb);
4340
4341 skb = skb_dequeue(head);
4342 }
4343
4344 if (skb_peek(&list)) {
4345 spin_lock_irq(&head->lock);
4346 while ((skb = __skb_dequeue(&list)) != NULL)
4347 __skb_queue_tail(head, skb);
4348 spin_unlock_irq(&head->lock);
4349 }
4350#else
Jens Axboe65e19f52019-10-26 07:20:21 -06004351 fput(io_file_from_index(ctx, index));
Jens Axboec3a31e62019-10-03 13:59:56 -06004352#endif
4353}
4354
4355static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
4356 int index)
4357{
4358#if defined(CONFIG_UNIX)
4359 struct sock *sock = ctx->ring_sock->sk;
4360 struct sk_buff_head *head = &sock->sk_receive_queue;
4361 struct sk_buff *skb;
4362
4363 /*
4364 * See if we can merge this file into an existing skb SCM_RIGHTS
4365 * file set. If there's no room, fall back to allocating a new skb
4366 * and filling it in.
4367 */
4368 spin_lock_irq(&head->lock);
4369 skb = skb_peek(head);
4370 if (skb) {
4371 struct scm_fp_list *fpl = UNIXCB(skb).fp;
4372
4373 if (fpl->count < SCM_MAX_FD) {
4374 __skb_unlink(skb, head);
4375 spin_unlock_irq(&head->lock);
4376 fpl->fp[fpl->count] = get_file(file);
4377 unix_inflight(fpl->user, fpl->fp[fpl->count]);
4378 fpl->count++;
4379 spin_lock_irq(&head->lock);
4380 __skb_queue_head(head, skb);
4381 } else {
4382 skb = NULL;
4383 }
4384 }
4385 spin_unlock_irq(&head->lock);
4386
4387 if (skb) {
4388 fput(file);
4389 return 0;
4390 }
4391
4392 return __io_sqe_files_scm(ctx, 1, index);
4393#else
4394 return 0;
4395#endif
4396}
4397
4398static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
4399 unsigned nr_args)
4400{
4401 struct io_uring_files_update up;
4402 __s32 __user *fds;
4403 int fd, i, err;
4404 __u32 done;
4405
Jens Axboe65e19f52019-10-26 07:20:21 -06004406 if (!ctx->file_table)
Jens Axboec3a31e62019-10-03 13:59:56 -06004407 return -ENXIO;
4408 if (!nr_args)
4409 return -EINVAL;
4410 if (copy_from_user(&up, arg, sizeof(up)))
4411 return -EFAULT;
4412 if (check_add_overflow(up.offset, nr_args, &done))
4413 return -EOVERFLOW;
4414 if (done > ctx->nr_user_files)
4415 return -EINVAL;
4416
4417 done = 0;
4418 fds = (__s32 __user *) up.fds;
4419 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06004420 struct fixed_file_table *table;
4421 unsigned index;
4422
Jens Axboec3a31e62019-10-03 13:59:56 -06004423 err = 0;
4424 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
4425 err = -EFAULT;
4426 break;
4427 }
4428 i = array_index_nospec(up.offset, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06004429 table = &ctx->file_table[i >> IORING_FILE_TABLE_SHIFT];
4430 index = i & IORING_FILE_TABLE_MASK;
4431 if (table->files[index]) {
Jens Axboec3a31e62019-10-03 13:59:56 -06004432 io_sqe_file_unregister(ctx, i);
Jens Axboe65e19f52019-10-26 07:20:21 -06004433 table->files[index] = NULL;
Jens Axboec3a31e62019-10-03 13:59:56 -06004434 }
4435 if (fd != -1) {
4436 struct file *file;
4437
4438 file = fget(fd);
4439 if (!file) {
4440 err = -EBADF;
4441 break;
4442 }
4443 /*
4444 * Don't allow io_uring instances to be registered. If
4445 * UNIX isn't enabled, then this causes a reference
4446 * cycle and this instance can never get freed. If UNIX
4447 * is enabled we'll handle it just fine, but there's
4448 * still no point in allowing a ring fd as it doesn't
4449 * support regular read/write anyway.
4450 */
4451 if (file->f_op == &io_uring_fops) {
4452 fput(file);
4453 err = -EBADF;
4454 break;
4455 }
Jens Axboe65e19f52019-10-26 07:20:21 -06004456 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06004457 err = io_sqe_file_register(ctx, file, i);
4458 if (err)
4459 break;
4460 }
4461 nr_args--;
4462 done++;
4463 up.offset++;
4464 }
4465
4466 return done ? done : err;
4467}
4468
Jens Axboe7d723062019-11-12 22:31:31 -07004469static void io_put_work(struct io_wq_work *work)
4470{
4471 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
4472
4473 io_put_req(req);
4474}
4475
4476static void io_get_work(struct io_wq_work *work)
4477{
4478 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
4479
4480 refcount_inc(&req->refs);
4481}
4482
Jens Axboe6c271ce2019-01-10 11:22:30 -07004483static int io_sq_offload_start(struct io_ring_ctx *ctx,
4484 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004485{
Jens Axboe576a3472019-11-25 08:49:20 -07004486 struct io_wq_data data;
Jens Axboe561fb042019-10-24 07:25:42 -06004487 unsigned concurrency;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004488 int ret;
4489
Jens Axboe6c271ce2019-01-10 11:22:30 -07004490 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004491 mmgrab(current->mm);
4492 ctx->sqo_mm = current->mm;
4493
Jens Axboe6c271ce2019-01-10 11:22:30 -07004494 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06004495 ret = -EPERM;
4496 if (!capable(CAP_SYS_ADMIN))
4497 goto err;
4498
Jens Axboe917257d2019-04-13 09:28:55 -06004499 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
4500 if (!ctx->sq_thread_idle)
4501 ctx->sq_thread_idle = HZ;
4502
Jens Axboe6c271ce2019-01-10 11:22:30 -07004503 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06004504 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004505
Jens Axboe917257d2019-04-13 09:28:55 -06004506 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06004507 if (cpu >= nr_cpu_ids)
4508 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08004509 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06004510 goto err;
4511
Jens Axboe6c271ce2019-01-10 11:22:30 -07004512 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
4513 ctx, cpu,
4514 "io_uring-sq");
4515 } else {
4516 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
4517 "io_uring-sq");
4518 }
4519 if (IS_ERR(ctx->sqo_thread)) {
4520 ret = PTR_ERR(ctx->sqo_thread);
4521 ctx->sqo_thread = NULL;
4522 goto err;
4523 }
4524 wake_up_process(ctx->sqo_thread);
4525 } else if (p->flags & IORING_SETUP_SQ_AFF) {
4526 /* Can't have SQ_AFF without SQPOLL */
4527 ret = -EINVAL;
4528 goto err;
4529 }
4530
Jens Axboe576a3472019-11-25 08:49:20 -07004531 data.mm = ctx->sqo_mm;
4532 data.user = ctx->user;
Jens Axboe181e4482019-11-25 08:52:30 -07004533 data.creds = ctx->creds;
Jens Axboe576a3472019-11-25 08:49:20 -07004534 data.get_work = io_get_work;
4535 data.put_work = io_put_work;
4536
Jens Axboe561fb042019-10-24 07:25:42 -06004537 /* Do QD, or 4 * CPUS, whatever is smallest */
4538 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
Jens Axboe576a3472019-11-25 08:49:20 -07004539 ctx->io_wq = io_wq_create(concurrency, &data);
Jens Axboe975c99a52019-10-30 08:42:56 -06004540 if (IS_ERR(ctx->io_wq)) {
4541 ret = PTR_ERR(ctx->io_wq);
4542 ctx->io_wq = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004543 goto err;
4544 }
4545
4546 return 0;
4547err:
Jens Axboe54a91f32019-09-10 09:15:04 -06004548 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004549 mmdrop(ctx->sqo_mm);
4550 ctx->sqo_mm = NULL;
4551 return ret;
4552}
4553
4554static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
4555{
4556 atomic_long_sub(nr_pages, &user->locked_vm);
4557}
4558
4559static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
4560{
4561 unsigned long page_limit, cur_pages, new_pages;
4562
4563 /* Don't allow more pages than we can safely lock */
4564 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
4565
4566 do {
4567 cur_pages = atomic_long_read(&user->locked_vm);
4568 new_pages = cur_pages + nr_pages;
4569 if (new_pages > page_limit)
4570 return -ENOMEM;
4571 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
4572 new_pages) != cur_pages);
4573
4574 return 0;
4575}
4576
4577static void io_mem_free(void *ptr)
4578{
Mark Rutland52e04ef2019-04-30 17:30:21 +01004579 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004580
Mark Rutland52e04ef2019-04-30 17:30:21 +01004581 if (!ptr)
4582 return;
4583
4584 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004585 if (put_page_testzero(page))
4586 free_compound_page(page);
4587}
4588
4589static void *io_mem_alloc(size_t size)
4590{
4591 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
4592 __GFP_NORETRY;
4593
4594 return (void *) __get_free_pages(gfp_flags, get_order(size));
4595}
4596
Hristo Venev75b28af2019-08-26 17:23:46 +00004597static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
4598 size_t *sq_offset)
4599{
4600 struct io_rings *rings;
4601 size_t off, sq_array_size;
4602
4603 off = struct_size(rings, cqes, cq_entries);
4604 if (off == SIZE_MAX)
4605 return SIZE_MAX;
4606
4607#ifdef CONFIG_SMP
4608 off = ALIGN(off, SMP_CACHE_BYTES);
4609 if (off == 0)
4610 return SIZE_MAX;
4611#endif
4612
4613 sq_array_size = array_size(sizeof(u32), sq_entries);
4614 if (sq_array_size == SIZE_MAX)
4615 return SIZE_MAX;
4616
4617 if (check_add_overflow(off, sq_array_size, &off))
4618 return SIZE_MAX;
4619
4620 if (sq_offset)
4621 *sq_offset = off;
4622
4623 return off;
4624}
4625
Jens Axboe2b188cc2019-01-07 10:46:33 -07004626static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
4627{
Hristo Venev75b28af2019-08-26 17:23:46 +00004628 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004629
Hristo Venev75b28af2019-08-26 17:23:46 +00004630 pages = (size_t)1 << get_order(
4631 rings_size(sq_entries, cq_entries, NULL));
4632 pages += (size_t)1 << get_order(
4633 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07004634
Hristo Venev75b28af2019-08-26 17:23:46 +00004635 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004636}
4637
Jens Axboeedafcce2019-01-09 09:16:05 -07004638static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
4639{
4640 int i, j;
4641
4642 if (!ctx->user_bufs)
4643 return -ENXIO;
4644
4645 for (i = 0; i < ctx->nr_user_bufs; i++) {
4646 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
4647
4648 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbard27c4d3a2019-08-04 19:32:06 -07004649 put_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07004650
4651 if (ctx->account_mem)
4652 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004653 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07004654 imu->nr_bvecs = 0;
4655 }
4656
4657 kfree(ctx->user_bufs);
4658 ctx->user_bufs = NULL;
4659 ctx->nr_user_bufs = 0;
4660 return 0;
4661}
4662
4663static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
4664 void __user *arg, unsigned index)
4665{
4666 struct iovec __user *src;
4667
4668#ifdef CONFIG_COMPAT
4669 if (ctx->compat) {
4670 struct compat_iovec __user *ciovs;
4671 struct compat_iovec ciov;
4672
4673 ciovs = (struct compat_iovec __user *) arg;
4674 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
4675 return -EFAULT;
4676
4677 dst->iov_base = (void __user *) (unsigned long) ciov.iov_base;
4678 dst->iov_len = ciov.iov_len;
4679 return 0;
4680 }
4681#endif
4682 src = (struct iovec __user *) arg;
4683 if (copy_from_user(dst, &src[index], sizeof(*dst)))
4684 return -EFAULT;
4685 return 0;
4686}
4687
4688static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
4689 unsigned nr_args)
4690{
4691 struct vm_area_struct **vmas = NULL;
4692 struct page **pages = NULL;
4693 int i, j, got_pages = 0;
4694 int ret = -EINVAL;
4695
4696 if (ctx->user_bufs)
4697 return -EBUSY;
4698 if (!nr_args || nr_args > UIO_MAXIOV)
4699 return -EINVAL;
4700
4701 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
4702 GFP_KERNEL);
4703 if (!ctx->user_bufs)
4704 return -ENOMEM;
4705
4706 for (i = 0; i < nr_args; i++) {
4707 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
4708 unsigned long off, start, end, ubuf;
4709 int pret, nr_pages;
4710 struct iovec iov;
4711 size_t size;
4712
4713 ret = io_copy_iov(ctx, &iov, arg, i);
4714 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03004715 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07004716
4717 /*
4718 * Don't impose further limits on the size and buffer
4719 * constraints here, we'll -EINVAL later when IO is
4720 * submitted if they are wrong.
4721 */
4722 ret = -EFAULT;
4723 if (!iov.iov_base || !iov.iov_len)
4724 goto err;
4725
4726 /* arbitrary limit, but we need something */
4727 if (iov.iov_len > SZ_1G)
4728 goto err;
4729
4730 ubuf = (unsigned long) iov.iov_base;
4731 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
4732 start = ubuf >> PAGE_SHIFT;
4733 nr_pages = end - start;
4734
4735 if (ctx->account_mem) {
4736 ret = io_account_mem(ctx->user, nr_pages);
4737 if (ret)
4738 goto err;
4739 }
4740
4741 ret = 0;
4742 if (!pages || nr_pages > got_pages) {
4743 kfree(vmas);
4744 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004745 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07004746 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004747 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07004748 sizeof(struct vm_area_struct *),
4749 GFP_KERNEL);
4750 if (!pages || !vmas) {
4751 ret = -ENOMEM;
4752 if (ctx->account_mem)
4753 io_unaccount_mem(ctx->user, nr_pages);
4754 goto err;
4755 }
4756 got_pages = nr_pages;
4757 }
4758
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004759 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07004760 GFP_KERNEL);
4761 ret = -ENOMEM;
4762 if (!imu->bvec) {
4763 if (ctx->account_mem)
4764 io_unaccount_mem(ctx->user, nr_pages);
4765 goto err;
4766 }
4767
4768 ret = 0;
4769 down_read(&current->mm->mmap_sem);
Ira Weiny932f4a62019-05-13 17:17:03 -07004770 pret = get_user_pages(ubuf, nr_pages,
4771 FOLL_WRITE | FOLL_LONGTERM,
4772 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07004773 if (pret == nr_pages) {
4774 /* don't support file backed memory */
4775 for (j = 0; j < nr_pages; j++) {
4776 struct vm_area_struct *vma = vmas[j];
4777
4778 if (vma->vm_file &&
4779 !is_file_hugepages(vma->vm_file)) {
4780 ret = -EOPNOTSUPP;
4781 break;
4782 }
4783 }
4784 } else {
4785 ret = pret < 0 ? pret : -EFAULT;
4786 }
4787 up_read(&current->mm->mmap_sem);
4788 if (ret) {
4789 /*
4790 * if we did partial map, or found file backed vmas,
4791 * release any pages we did get
4792 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07004793 if (pret > 0)
4794 put_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07004795 if (ctx->account_mem)
4796 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004797 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07004798 goto err;
4799 }
4800
4801 off = ubuf & ~PAGE_MASK;
4802 size = iov.iov_len;
4803 for (j = 0; j < nr_pages; j++) {
4804 size_t vec_len;
4805
4806 vec_len = min_t(size_t, size, PAGE_SIZE - off);
4807 imu->bvec[j].bv_page = pages[j];
4808 imu->bvec[j].bv_len = vec_len;
4809 imu->bvec[j].bv_offset = off;
4810 off = 0;
4811 size -= vec_len;
4812 }
4813 /* store original address for later verification */
4814 imu->ubuf = ubuf;
4815 imu->len = iov.iov_len;
4816 imu->nr_bvecs = nr_pages;
4817
4818 ctx->nr_user_bufs++;
4819 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004820 kvfree(pages);
4821 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07004822 return 0;
4823err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004824 kvfree(pages);
4825 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07004826 io_sqe_buffer_unregister(ctx);
4827 return ret;
4828}
4829
Jens Axboe9b402842019-04-11 11:45:41 -06004830static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
4831{
4832 __s32 __user *fds = arg;
4833 int fd;
4834
4835 if (ctx->cq_ev_fd)
4836 return -EBUSY;
4837
4838 if (copy_from_user(&fd, fds, sizeof(*fds)))
4839 return -EFAULT;
4840
4841 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
4842 if (IS_ERR(ctx->cq_ev_fd)) {
4843 int ret = PTR_ERR(ctx->cq_ev_fd);
4844 ctx->cq_ev_fd = NULL;
4845 return ret;
4846 }
4847
4848 return 0;
4849}
4850
4851static int io_eventfd_unregister(struct io_ring_ctx *ctx)
4852{
4853 if (ctx->cq_ev_fd) {
4854 eventfd_ctx_put(ctx->cq_ev_fd);
4855 ctx->cq_ev_fd = NULL;
4856 return 0;
4857 }
4858
4859 return -ENXIO;
4860}
4861
Jens Axboe2b188cc2019-01-07 10:46:33 -07004862static void io_ring_ctx_free(struct io_ring_ctx *ctx)
4863{
Jens Axboe6b063142019-01-10 22:13:58 -07004864 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004865 if (ctx->sqo_mm)
4866 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07004867
4868 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07004869 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07004870 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06004871 io_eventfd_unregister(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07004872
Jens Axboe2b188cc2019-01-07 10:46:33 -07004873#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07004874 if (ctx->ring_sock) {
4875 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004876 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07004877 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004878#endif
4879
Hristo Venev75b28af2019-08-26 17:23:46 +00004880 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004881 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004882
4883 percpu_ref_exit(&ctx->refs);
4884 if (ctx->account_mem)
4885 io_unaccount_mem(ctx->user,
4886 ring_pages(ctx->sq_entries, ctx->cq_entries));
4887 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07004888 put_cred(ctx->creds);
Jens Axboe206aefd2019-11-07 18:27:42 -07004889 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -07004890 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07004891 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004892 kfree(ctx);
4893}
4894
4895static __poll_t io_uring_poll(struct file *file, poll_table *wait)
4896{
4897 struct io_ring_ctx *ctx = file->private_data;
4898 __poll_t mask = 0;
4899
4900 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02004901 /*
4902 * synchronizes with barrier from wq_has_sleeper call in
4903 * io_commit_cqring
4904 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004905 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00004906 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
4907 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004908 mask |= EPOLLOUT | EPOLLWRNORM;
yangerkundaa5de52019-09-24 20:53:34 +08004909 if (READ_ONCE(ctx->rings->cq.head) != ctx->cached_cq_tail)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004910 mask |= EPOLLIN | EPOLLRDNORM;
4911
4912 return mask;
4913}
4914
4915static int io_uring_fasync(int fd, struct file *file, int on)
4916{
4917 struct io_ring_ctx *ctx = file->private_data;
4918
4919 return fasync_helper(fd, file, on, &ctx->cq_fasync);
4920}
4921
4922static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
4923{
4924 mutex_lock(&ctx->uring_lock);
4925 percpu_ref_kill(&ctx->refs);
4926 mutex_unlock(&ctx->uring_lock);
4927
Jens Axboe5262f562019-09-17 12:26:57 -06004928 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004929 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06004930
4931 if (ctx->io_wq)
4932 io_wq_cancel_all(ctx->io_wq);
4933
Jens Axboedef596e2019-01-09 08:59:42 -07004934 io_iopoll_reap_events(ctx);
Jens Axboe15dff282019-11-13 09:09:23 -07004935 /* if we failed setting up the ctx, we might not have any rings */
4936 if (ctx->rings)
4937 io_cqring_overflow_flush(ctx, true);
Jens Axboe206aefd2019-11-07 18:27:42 -07004938 wait_for_completion(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004939 io_ring_ctx_free(ctx);
4940}
4941
4942static int io_uring_release(struct inode *inode, struct file *file)
4943{
4944 struct io_ring_ctx *ctx = file->private_data;
4945
4946 file->private_data = NULL;
4947 io_ring_ctx_wait_and_kill(ctx);
4948 return 0;
4949}
4950
Jens Axboefcb323c2019-10-24 12:39:47 -06004951static void io_uring_cancel_files(struct io_ring_ctx *ctx,
4952 struct files_struct *files)
4953{
4954 struct io_kiocb *req;
4955 DEFINE_WAIT(wait);
4956
4957 while (!list_empty_careful(&ctx->inflight_list)) {
Jens Axboe768134d2019-11-10 20:30:53 -07004958 struct io_kiocb *cancel_req = NULL;
Jens Axboefcb323c2019-10-24 12:39:47 -06004959
4960 spin_lock_irq(&ctx->inflight_lock);
4961 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07004962 if (req->work.files != files)
4963 continue;
4964 /* req is being completed, ignore */
4965 if (!refcount_inc_not_zero(&req->refs))
4966 continue;
4967 cancel_req = req;
4968 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06004969 }
Jens Axboe768134d2019-11-10 20:30:53 -07004970 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06004971 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07004972 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06004973 spin_unlock_irq(&ctx->inflight_lock);
4974
Jens Axboe768134d2019-11-10 20:30:53 -07004975 /* We need to keep going until we don't find a matching req */
4976 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06004977 break;
Bob Liu2f6d9b92019-11-13 18:06:24 +08004978
4979 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
4980 io_put_req(cancel_req);
Jens Axboefcb323c2019-10-24 12:39:47 -06004981 schedule();
4982 }
Jens Axboe768134d2019-11-10 20:30:53 -07004983 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06004984}
4985
4986static int io_uring_flush(struct file *file, void *data)
4987{
4988 struct io_ring_ctx *ctx = file->private_data;
4989
4990 io_uring_cancel_files(ctx, data);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004991 if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) {
4992 io_cqring_overflow_flush(ctx, true);
Jens Axboefcb323c2019-10-24 12:39:47 -06004993 io_wq_cancel_all(ctx->io_wq);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004994 }
Jens Axboefcb323c2019-10-24 12:39:47 -06004995 return 0;
4996}
4997
Roman Penyaev6c5c2402019-11-28 12:53:22 +01004998static void *io_uring_validate_mmap_request(struct file *file,
4999 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005000{
Jens Axboe2b188cc2019-01-07 10:46:33 -07005001 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01005002 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005003 struct page *page;
5004 void *ptr;
5005
5006 switch (offset) {
5007 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00005008 case IORING_OFF_CQ_RING:
5009 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005010 break;
5011 case IORING_OFF_SQES:
5012 ptr = ctx->sq_sqes;
5013 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005014 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01005015 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005016 }
5017
5018 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07005019 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01005020 return ERR_PTR(-EINVAL);
5021
5022 return ptr;
5023}
5024
5025#ifdef CONFIG_MMU
5026
5027static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
5028{
5029 size_t sz = vma->vm_end - vma->vm_start;
5030 unsigned long pfn;
5031 void *ptr;
5032
5033 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
5034 if (IS_ERR(ptr))
5035 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005036
5037 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
5038 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
5039}
5040
Roman Penyaev6c5c2402019-11-28 12:53:22 +01005041#else /* !CONFIG_MMU */
5042
5043static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
5044{
5045 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
5046}
5047
5048static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
5049{
5050 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
5051}
5052
5053static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
5054 unsigned long addr, unsigned long len,
5055 unsigned long pgoff, unsigned long flags)
5056{
5057 void *ptr;
5058
5059 ptr = io_uring_validate_mmap_request(file, pgoff, len);
5060 if (IS_ERR(ptr))
5061 return PTR_ERR(ptr);
5062
5063 return (unsigned long) ptr;
5064}
5065
5066#endif /* !CONFIG_MMU */
5067
Jens Axboe2b188cc2019-01-07 10:46:33 -07005068SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
5069 u32, min_complete, u32, flags, const sigset_t __user *, sig,
5070 size_t, sigsz)
5071{
5072 struct io_ring_ctx *ctx;
5073 long ret = -EBADF;
5074 int submitted = 0;
5075 struct fd f;
5076
Jens Axboe6c271ce2019-01-10 11:22:30 -07005077 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07005078 return -EINVAL;
5079
5080 f = fdget(fd);
5081 if (!f.file)
5082 return -EBADF;
5083
5084 ret = -EOPNOTSUPP;
5085 if (f.file->f_op != &io_uring_fops)
5086 goto out_fput;
5087
5088 ret = -ENXIO;
5089 ctx = f.file->private_data;
5090 if (!percpu_ref_tryget(&ctx->refs))
5091 goto out_fput;
5092
Jens Axboe6c271ce2019-01-10 11:22:30 -07005093 /*
5094 * For SQ polling, the thread will do all submissions and completions.
5095 * Just return the requested submit count, and wake the thread if
5096 * we were asked to.
5097 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06005098 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005099 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07005100 if (!list_empty_careful(&ctx->cq_overflow_list))
5101 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005102 if (flags & IORING_ENTER_SQ_WAKEUP)
5103 wake_up(&ctx->sqo_wait);
5104 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06005105 } else if (to_submit) {
Pavel Begunkovae9428c2019-11-06 00:22:14 +03005106 struct mm_struct *cur_mm;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005107
Pavel Begunkovae9428c2019-11-06 00:22:14 +03005108 to_submit = min(to_submit, ctx->sq_entries);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005109 mutex_lock(&ctx->uring_lock);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03005110 /* already have mm, so io_submit_sqes() won't try to grab it */
5111 cur_mm = ctx->sqo_mm;
5112 submitted = io_submit_sqes(ctx, to_submit, f.file, fd,
5113 &cur_mm, false);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005114 mutex_unlock(&ctx->uring_lock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005115 }
5116 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07005117 unsigned nr_events = 0;
5118
Jens Axboe2b188cc2019-01-07 10:46:33 -07005119 min_complete = min(min_complete, ctx->cq_entries);
5120
Jens Axboedef596e2019-01-09 08:59:42 -07005121 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07005122 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07005123 } else {
5124 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
5125 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07005126 }
5127
Pavel Begunkov6805b322019-10-08 02:18:42 +03005128 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005129out_fput:
5130 fdput(f);
5131 return submitted ? submitted : ret;
5132}
5133
5134static const struct file_operations io_uring_fops = {
5135 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06005136 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07005137 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01005138#ifndef CONFIG_MMU
5139 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
5140 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
5141#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07005142 .poll = io_uring_poll,
5143 .fasync = io_uring_fasync,
5144};
5145
5146static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
5147 struct io_uring_params *p)
5148{
Hristo Venev75b28af2019-08-26 17:23:46 +00005149 struct io_rings *rings;
5150 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005151
Hristo Venev75b28af2019-08-26 17:23:46 +00005152 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
5153 if (size == SIZE_MAX)
5154 return -EOVERFLOW;
5155
5156 rings = io_mem_alloc(size);
5157 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005158 return -ENOMEM;
5159
Hristo Venev75b28af2019-08-26 17:23:46 +00005160 ctx->rings = rings;
5161 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
5162 rings->sq_ring_mask = p->sq_entries - 1;
5163 rings->cq_ring_mask = p->cq_entries - 1;
5164 rings->sq_ring_entries = p->sq_entries;
5165 rings->cq_ring_entries = p->cq_entries;
5166 ctx->sq_mask = rings->sq_ring_mask;
5167 ctx->cq_mask = rings->cq_ring_mask;
5168 ctx->sq_entries = rings->sq_ring_entries;
5169 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005170
5171 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07005172 if (size == SIZE_MAX) {
5173 io_mem_free(ctx->rings);
5174 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005175 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07005176 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07005177
5178 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07005179 if (!ctx->sq_sqes) {
5180 io_mem_free(ctx->rings);
5181 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005182 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07005183 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07005184
Jens Axboe2b188cc2019-01-07 10:46:33 -07005185 return 0;
5186}
5187
5188/*
5189 * Allocate an anonymous fd, this is what constitutes the application
5190 * visible backing of an io_uring instance. The application mmaps this
5191 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
5192 * we have to tie this fd to a socket for file garbage collection purposes.
5193 */
5194static int io_uring_get_fd(struct io_ring_ctx *ctx)
5195{
5196 struct file *file;
5197 int ret;
5198
5199#if defined(CONFIG_UNIX)
5200 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
5201 &ctx->ring_sock);
5202 if (ret)
5203 return ret;
5204#endif
5205
5206 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
5207 if (ret < 0)
5208 goto err;
5209
5210 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
5211 O_RDWR | O_CLOEXEC);
5212 if (IS_ERR(file)) {
5213 put_unused_fd(ret);
5214 ret = PTR_ERR(file);
5215 goto err;
5216 }
5217
5218#if defined(CONFIG_UNIX)
5219 ctx->ring_sock->file = file;
Jens Axboe6b063142019-01-10 22:13:58 -07005220 ctx->ring_sock->sk->sk_user_data = ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005221#endif
5222 fd_install(ret, file);
5223 return ret;
5224err:
5225#if defined(CONFIG_UNIX)
5226 sock_release(ctx->ring_sock);
5227 ctx->ring_sock = NULL;
5228#endif
5229 return ret;
5230}
5231
5232static int io_uring_create(unsigned entries, struct io_uring_params *p)
5233{
5234 struct user_struct *user = NULL;
5235 struct io_ring_ctx *ctx;
5236 bool account_mem;
5237 int ret;
5238
5239 if (!entries || entries > IORING_MAX_ENTRIES)
5240 return -EINVAL;
5241
5242 /*
5243 * Use twice as many entries for the CQ ring. It's possible for the
5244 * application to drive a higher depth than the size of the SQ ring,
5245 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06005246 * some flexibility in overcommitting a bit. If the application has
5247 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
5248 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07005249 */
5250 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06005251 if (p->flags & IORING_SETUP_CQSIZE) {
5252 /*
5253 * If IORING_SETUP_CQSIZE is set, we do the same roundup
5254 * to a power-of-two, if it isn't already. We do NOT impose
5255 * any cq vs sq ring sizing.
5256 */
5257 if (p->cq_entries < p->sq_entries || p->cq_entries > IORING_MAX_CQ_ENTRIES)
5258 return -EINVAL;
5259 p->cq_entries = roundup_pow_of_two(p->cq_entries);
5260 } else {
5261 p->cq_entries = 2 * p->sq_entries;
5262 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07005263
5264 user = get_uid(current_user());
5265 account_mem = !capable(CAP_IPC_LOCK);
5266
5267 if (account_mem) {
5268 ret = io_account_mem(user,
5269 ring_pages(p->sq_entries, p->cq_entries));
5270 if (ret) {
5271 free_uid(user);
5272 return ret;
5273 }
5274 }
5275
5276 ctx = io_ring_ctx_alloc(p);
5277 if (!ctx) {
5278 if (account_mem)
5279 io_unaccount_mem(user, ring_pages(p->sq_entries,
5280 p->cq_entries));
5281 free_uid(user);
5282 return -ENOMEM;
5283 }
5284 ctx->compat = in_compat_syscall();
5285 ctx->account_mem = account_mem;
5286 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07005287 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07005288
5289 ret = io_allocate_scq_urings(ctx, p);
5290 if (ret)
5291 goto err;
5292
Jens Axboe6c271ce2019-01-10 11:22:30 -07005293 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005294 if (ret)
5295 goto err;
5296
Jens Axboe2b188cc2019-01-07 10:46:33 -07005297 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00005298 p->sq_off.head = offsetof(struct io_rings, sq.head);
5299 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
5300 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
5301 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
5302 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
5303 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
5304 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005305
5306 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00005307 p->cq_off.head = offsetof(struct io_rings, cq.head);
5308 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
5309 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
5310 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
5311 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
5312 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06005313
Jens Axboe044c1ab2019-10-28 09:15:33 -06005314 /*
5315 * Install ring fd as the very last thing, so we don't risk someone
5316 * having closed it before we finish setup
5317 */
5318 ret = io_uring_get_fd(ctx);
5319 if (ret < 0)
5320 goto err;
5321
Jens Axboeda8c9692019-12-02 18:51:26 -07005322 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
5323 IORING_FEAT_SUBMIT_STABLE;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02005324 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005325 return ret;
5326err:
5327 io_ring_ctx_wait_and_kill(ctx);
5328 return ret;
5329}
5330
5331/*
5332 * Sets up an aio uring context, and returns the fd. Applications asks for a
5333 * ring size, we return the actual sq/cq ring sizes (among other things) in the
5334 * params structure passed in.
5335 */
5336static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
5337{
5338 struct io_uring_params p;
5339 long ret;
5340 int i;
5341
5342 if (copy_from_user(&p, params, sizeof(p)))
5343 return -EFAULT;
5344 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
5345 if (p.resv[i])
5346 return -EINVAL;
5347 }
5348
Jens Axboe6c271ce2019-01-10 11:22:30 -07005349 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe33a107f2019-10-04 12:10:03 -06005350 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE))
Jens Axboe2b188cc2019-01-07 10:46:33 -07005351 return -EINVAL;
5352
5353 ret = io_uring_create(entries, &p);
5354 if (ret < 0)
5355 return ret;
5356
5357 if (copy_to_user(params, &p, sizeof(p)))
5358 return -EFAULT;
5359
5360 return ret;
5361}
5362
5363SYSCALL_DEFINE2(io_uring_setup, u32, entries,
5364 struct io_uring_params __user *, params)
5365{
5366 return io_uring_setup(entries, params);
5367}
5368
Jens Axboeedafcce2019-01-09 09:16:05 -07005369static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
5370 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06005371 __releases(ctx->uring_lock)
5372 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07005373{
5374 int ret;
5375
Jens Axboe35fa71a2019-04-22 10:23:23 -06005376 /*
5377 * We're inside the ring mutex, if the ref is already dying, then
5378 * someone else killed the ctx or is already going through
5379 * io_uring_register().
5380 */
5381 if (percpu_ref_is_dying(&ctx->refs))
5382 return -ENXIO;
5383
Jens Axboeedafcce2019-01-09 09:16:05 -07005384 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06005385
5386 /*
5387 * Drop uring mutex before waiting for references to exit. If another
5388 * thread is currently inside io_uring_enter() it might need to grab
5389 * the uring_lock to make progress. If we hold it here across the drain
5390 * wait, then we can deadlock. It's safe to drop the mutex here, since
5391 * no new references will come in after we've killed the percpu ref.
5392 */
5393 mutex_unlock(&ctx->uring_lock);
Jens Axboe206aefd2019-11-07 18:27:42 -07005394 wait_for_completion(&ctx->completions[0]);
Jens Axboeb19062a2019-04-15 10:49:38 -06005395 mutex_lock(&ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07005396
5397 switch (opcode) {
5398 case IORING_REGISTER_BUFFERS:
5399 ret = io_sqe_buffer_register(ctx, arg, nr_args);
5400 break;
5401 case IORING_UNREGISTER_BUFFERS:
5402 ret = -EINVAL;
5403 if (arg || nr_args)
5404 break;
5405 ret = io_sqe_buffer_unregister(ctx);
5406 break;
Jens Axboe6b063142019-01-10 22:13:58 -07005407 case IORING_REGISTER_FILES:
5408 ret = io_sqe_files_register(ctx, arg, nr_args);
5409 break;
5410 case IORING_UNREGISTER_FILES:
5411 ret = -EINVAL;
5412 if (arg || nr_args)
5413 break;
5414 ret = io_sqe_files_unregister(ctx);
5415 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06005416 case IORING_REGISTER_FILES_UPDATE:
5417 ret = io_sqe_files_update(ctx, arg, nr_args);
5418 break;
Jens Axboe9b402842019-04-11 11:45:41 -06005419 case IORING_REGISTER_EVENTFD:
5420 ret = -EINVAL;
5421 if (nr_args != 1)
5422 break;
5423 ret = io_eventfd_register(ctx, arg);
5424 break;
5425 case IORING_UNREGISTER_EVENTFD:
5426 ret = -EINVAL;
5427 if (arg || nr_args)
5428 break;
5429 ret = io_eventfd_unregister(ctx);
5430 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07005431 default:
5432 ret = -EINVAL;
5433 break;
5434 }
5435
5436 /* bring the ctx back to life */
Jens Axboe206aefd2019-11-07 18:27:42 -07005437 reinit_completion(&ctx->completions[0]);
Jens Axboeedafcce2019-01-09 09:16:05 -07005438 percpu_ref_reinit(&ctx->refs);
5439 return ret;
5440}
5441
5442SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
5443 void __user *, arg, unsigned int, nr_args)
5444{
5445 struct io_ring_ctx *ctx;
5446 long ret = -EBADF;
5447 struct fd f;
5448
5449 f = fdget(fd);
5450 if (!f.file)
5451 return -EBADF;
5452
5453 ret = -EOPNOTSUPP;
5454 if (f.file->f_op != &io_uring_fops)
5455 goto out_fput;
5456
5457 ctx = f.file->private_data;
5458
5459 mutex_lock(&ctx->uring_lock);
5460 ret = __io_uring_register(ctx, opcode, arg, nr_args);
5461 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02005462 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
5463 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07005464out_fput:
5465 fdput(f);
5466 return ret;
5467}
5468
Jens Axboe2b188cc2019-01-07 10:46:33 -07005469static int __init io_uring_init(void)
5470{
5471 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
5472 return 0;
5473};
5474__initcall(io_uring_init);