blob: 9d4f8274ee1eb847c4857ce82742f9083010b8c9 [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 Axboeb29472e2019-12-17 18:50:29 -0700329struct io_timeout {
330 struct file *file;
331 u64 addr;
332 int flags;
333};
334
Jens Axboef499a022019-12-02 16:28:46 -0700335struct io_async_connect {
336 struct sockaddr_storage address;
337};
338
Jens Axboe03b12302019-12-02 18:50:25 -0700339struct io_async_msghdr {
340 struct iovec fast_iov[UIO_FASTIOV];
341 struct iovec *iov;
342 struct sockaddr __user *uaddr;
343 struct msghdr msg;
344};
345
Jens Axboef67676d2019-12-02 11:03:47 -0700346struct io_async_rw {
347 struct iovec fast_iov[UIO_FASTIOV];
348 struct iovec *iov;
349 ssize_t nr_segs;
350 ssize_t size;
351};
352
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700353struct io_async_ctx {
354 struct io_uring_sqe sqe;
Jens Axboef67676d2019-12-02 11:03:47 -0700355 union {
356 struct io_async_rw rw;
Jens Axboe03b12302019-12-02 18:50:25 -0700357 struct io_async_msghdr msg;
Jens Axboef499a022019-12-02 16:28:46 -0700358 struct io_async_connect connect;
Jens Axboe2d283902019-12-04 11:08:05 -0700359 struct io_timeout_data timeout;
Jens Axboef67676d2019-12-02 11:03:47 -0700360 };
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700361};
362
Jens Axboe09bb8392019-03-13 12:39:28 -0600363/*
364 * NOTE! Each of the iocb union members has the file pointer
365 * as the first entry in their struct definition. So you can
366 * access the file pointer through any of the sub-structs,
367 * or directly as just 'ki_filp' in this struct.
368 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700369struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700370 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600371 struct file *file;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700372 struct kiocb rw;
373 struct io_poll_iocb poll;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700374 struct io_accept accept;
375 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700376 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700377 struct io_timeout timeout;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700378 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700379
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300380 const struct io_uring_sqe *sqe;
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700381 struct io_async_ctx *io;
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300382 struct file *ring_file;
383 int ring_fd;
384 bool has_user;
385 bool in_async;
386 bool needs_fixed_file;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700387
388 struct io_ring_ctx *ctx;
Jens Axboeeac406c2019-11-14 12:09:58 -0700389 union {
390 struct list_head list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700391 struct hlist_node hash_node;
Jens Axboeeac406c2019-11-14 12:09:58 -0700392 };
Jens Axboe9e645e112019-05-10 16:07:28 -0600393 struct list_head link_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700394 unsigned int flags;
Jens Axboec16361c2019-01-17 08:39:48 -0700395 refcount_t refs;
Stefan Bühler8449eed2019-04-27 20:34:19 +0200396#define REQ_F_NOWAIT 1 /* must not punt to workers */
Jens Axboedef596e2019-01-09 08:59:42 -0700397#define REQ_F_IOPOLL_COMPLETED 2 /* polled IO has completed */
Jens Axboe6b063142019-01-10 22:13:58 -0700398#define REQ_F_FIXED_FILE 4 /* ctx owns file */
Jens Axboe4d7dd462019-11-20 13:03:52 -0700399#define REQ_F_LINK_NEXT 8 /* already grabbed next link */
Stefan Bühlere2033e32019-05-11 19:08:01 +0200400#define REQ_F_IO_DRAIN 16 /* drain existing IO first */
401#define REQ_F_IO_DRAINED 32 /* drain done */
Jens Axboe9e645e112019-05-10 16:07:28 -0600402#define REQ_F_LINK 64 /* linked sqes */
Jens Axboe2665abf2019-11-05 12:40:47 -0700403#define REQ_F_LINK_TIMEOUT 128 /* has linked timeout */
Zhengyuan Liuf7b76ac2019-07-16 23:26:14 +0800404#define REQ_F_FAIL_LINK 256 /* fail rest of links */
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +0300405#define REQ_F_DRAIN_LINK 512 /* link should be fully drained */
Jens Axboe5262f562019-09-17 12:26:57 -0600406#define REQ_F_TIMEOUT 1024 /* timeout request */
Jens Axboe491381ce2019-10-17 09:20:46 -0600407#define REQ_F_ISREG 2048 /* regular file */
408#define REQ_F_MUST_PUNT 4096 /* must be punted even for NONBLOCK */
Jens Axboe93bd25b2019-11-11 23:34:31 -0700409#define REQ_F_TIMEOUT_NOSEQ 8192 /* no timeout sequence */
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800410#define REQ_F_INFLIGHT 16384 /* on inflight list */
411#define REQ_F_COMP_LOCKED 32768 /* completion under lock */
Jens Axboe4e88d6e2019-12-07 20:59:47 -0700412#define REQ_F_HARDLINK 65536 /* doesn't sever on completion < 0 */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700413#define REQ_F_PREPPED 131072 /* request already opcode prepared */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700414 u64 user_data;
Jens Axboe9e645e112019-05-10 16:07:28 -0600415 u32 result;
Jens Axboede0617e2019-04-06 21:51:27 -0600416 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700417
Jens Axboefcb323c2019-10-24 12:39:47 -0600418 struct list_head inflight_entry;
419
Jens Axboe561fb042019-10-24 07:25:42 -0600420 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700421};
422
423#define IO_PLUG_THRESHOLD 2
Jens Axboedef596e2019-01-09 08:59:42 -0700424#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700425
Jens Axboe9a56a232019-01-09 09:06:50 -0700426struct io_submit_state {
427 struct blk_plug plug;
428
429 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700430 * io_kiocb alloc cache
431 */
432 void *reqs[IO_IOPOLL_BATCH];
433 unsigned int free_reqs;
434 unsigned int cur_req;
435
436 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700437 * File reference cache
438 */
439 struct file *file;
440 unsigned int fd;
441 unsigned int has_refs;
442 unsigned int used_refs;
443 unsigned int ios_left;
444};
445
Jens Axboe561fb042019-10-24 07:25:42 -0600446static void io_wq_submit_work(struct io_wq_work **workptr);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700447static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liu4fe2c962019-09-09 20:50:40 +0800448static void __io_free_req(struct io_kiocb *req);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800449static void io_put_req(struct io_kiocb *req);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700450static void io_double_put_req(struct io_kiocb *req);
Jens Axboe978db572019-11-14 22:39:04 -0700451static void __io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700452static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
453static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboede0617e2019-04-06 21:51:27 -0600454
Jens Axboe2b188cc2019-01-07 10:46:33 -0700455static struct kmem_cache *req_cachep;
456
457static const struct file_operations io_uring_fops;
458
459struct sock *io_uring_get_socket(struct file *file)
460{
461#if defined(CONFIG_UNIX)
462 if (file->f_op == &io_uring_fops) {
463 struct io_ring_ctx *ctx = file->private_data;
464
465 return ctx->ring_sock->sk;
466 }
467#endif
468 return NULL;
469}
470EXPORT_SYMBOL(io_uring_get_socket);
471
472static void io_ring_ctx_ref_free(struct percpu_ref *ref)
473{
474 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
475
Jens Axboe206aefd2019-11-07 18:27:42 -0700476 complete(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700477}
478
479static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
480{
481 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -0700482 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700483
484 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
485 if (!ctx)
486 return NULL;
487
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700488 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
489 if (!ctx->fallback_req)
490 goto err;
491
Jens Axboe206aefd2019-11-07 18:27:42 -0700492 ctx->completions = kmalloc(2 * sizeof(struct completion), GFP_KERNEL);
493 if (!ctx->completions)
494 goto err;
495
Jens Axboe78076bb2019-12-04 19:56:40 -0700496 /*
497 * Use 5 bits less than the max cq entries, that should give us around
498 * 32 entries per hash list if totally full and uniformly spread.
499 */
500 hash_bits = ilog2(p->cq_entries);
501 hash_bits -= 5;
502 if (hash_bits <= 0)
503 hash_bits = 1;
504 ctx->cancel_hash_bits = hash_bits;
505 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
506 GFP_KERNEL);
507 if (!ctx->cancel_hash)
508 goto err;
509 __hash_init(ctx->cancel_hash, 1U << hash_bits);
510
Roman Gushchin21482892019-05-07 10:01:48 -0700511 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -0700512 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
513 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700514
515 ctx->flags = p->flags;
516 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700517 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe206aefd2019-11-07 18:27:42 -0700518 init_completion(&ctx->completions[0]);
519 init_completion(&ctx->completions[1]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700520 mutex_init(&ctx->uring_lock);
521 init_waitqueue_head(&ctx->wait);
522 spin_lock_init(&ctx->completion_lock);
Jens Axboedef596e2019-01-09 08:59:42 -0700523 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboede0617e2019-04-06 21:51:27 -0600524 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -0600525 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -0600526 init_waitqueue_head(&ctx->inflight_wait);
527 spin_lock_init(&ctx->inflight_lock);
528 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700529 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -0700530err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700531 if (ctx->fallback_req)
532 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe206aefd2019-11-07 18:27:42 -0700533 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -0700534 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -0700535 kfree(ctx);
536 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700537}
538
Bob Liu9d858b22019-11-13 18:06:25 +0800539static inline bool __req_need_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -0600540{
Jackie Liua197f662019-11-08 08:09:12 -0700541 struct io_ring_ctx *ctx = req->ctx;
542
Jens Axboe498ccd92019-10-25 10:04:25 -0600543 return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped
544 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboede0617e2019-04-06 21:51:27 -0600545}
546
Bob Liu9d858b22019-11-13 18:06:25 +0800547static inline bool req_need_defer(struct io_kiocb *req)
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600548{
Bob Liu9d858b22019-11-13 18:06:25 +0800549 if ((req->flags & (REQ_F_IO_DRAIN|REQ_F_IO_DRAINED)) == REQ_F_IO_DRAIN)
550 return __req_need_defer(req);
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600551
Bob Liu9d858b22019-11-13 18:06:25 +0800552 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600553}
554
555static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -0600556{
557 struct io_kiocb *req;
558
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600559 req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list);
Bob Liu9d858b22019-11-13 18:06:25 +0800560 if (req && !req_need_defer(req)) {
Jens Axboede0617e2019-04-06 21:51:27 -0600561 list_del_init(&req->list);
562 return req;
563 }
564
565 return NULL;
566}
567
Jens Axboe5262f562019-09-17 12:26:57 -0600568static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx)
569{
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600570 struct io_kiocb *req;
571
572 req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list);
Jens Axboe93bd25b2019-11-11 23:34:31 -0700573 if (req) {
574 if (req->flags & REQ_F_TIMEOUT_NOSEQ)
575 return NULL;
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800576 if (!__req_need_defer(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -0700577 list_del_init(&req->list);
578 return req;
579 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600580 }
581
582 return NULL;
Jens Axboe5262f562019-09-17 12:26:57 -0600583}
584
Jens Axboede0617e2019-04-06 21:51:27 -0600585static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700586{
Hristo Venev75b28af2019-08-26 17:23:46 +0000587 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700588
Hristo Venev75b28af2019-08-26 17:23:46 +0000589 if (ctx->cached_cq_tail != READ_ONCE(rings->cq.tail)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700590 /* order cqe stores with ring update */
Hristo Venev75b28af2019-08-26 17:23:46 +0000591 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700592
Jens Axboe2b188cc2019-01-07 10:46:33 -0700593 if (wq_has_sleeper(&ctx->cq_wait)) {
594 wake_up_interruptible(&ctx->cq_wait);
595 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
596 }
597 }
598}
599
Jens Axboe561fb042019-10-24 07:25:42 -0600600static inline bool io_sqe_needs_user(const struct io_uring_sqe *sqe)
Jens Axboe18d9be12019-09-10 09:13:05 -0600601{
Jens Axboe561fb042019-10-24 07:25:42 -0600602 u8 opcode = READ_ONCE(sqe->opcode);
603
604 return !(opcode == IORING_OP_READ_FIXED ||
605 opcode == IORING_OP_WRITE_FIXED);
606}
607
Jens Axboe94ae5e72019-11-14 19:39:52 -0700608static inline bool io_prep_async_work(struct io_kiocb *req,
609 struct io_kiocb **link)
Jens Axboe561fb042019-10-24 07:25:42 -0600610{
611 bool do_hashed = false;
Jens Axboe54a91f32019-09-10 09:15:04 -0600612
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300613 if (req->sqe) {
614 switch (req->sqe->opcode) {
Jens Axboe6cc47d12019-09-18 11:18:23 -0600615 case IORING_OP_WRITEV:
616 case IORING_OP_WRITE_FIXED:
Jens Axboe53108d42019-12-09 20:12:38 -0700617 /* only regular files should be hashed for writes */
618 if (req->flags & REQ_F_ISREG)
619 do_hashed = true;
Jens Axboe5f8fd2d32019-11-07 10:57:36 -0700620 /* fall-through */
621 case IORING_OP_READV:
622 case IORING_OP_READ_FIXED:
623 case IORING_OP_SENDMSG:
624 case IORING_OP_RECVMSG:
625 case IORING_OP_ACCEPT:
626 case IORING_OP_POLL_ADD:
Jens Axboef8e85cf2019-11-23 14:24:24 -0700627 case IORING_OP_CONNECT:
Jens Axboe5f8fd2d32019-11-07 10:57:36 -0700628 /*
629 * We know REQ_F_ISREG is not set on some of these
630 * opcodes, but this enables us to keep the check in
631 * just one place.
632 */
633 if (!(req->flags & REQ_F_ISREG))
634 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe6cc47d12019-09-18 11:18:23 -0600635 break;
636 }
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300637 if (io_sqe_needs_user(req->sqe))
Jens Axboe561fb042019-10-24 07:25:42 -0600638 req->work.flags |= IO_WQ_WORK_NEEDS_USER;
Jens Axboe54a91f32019-09-10 09:15:04 -0600639 }
640
Jens Axboe94ae5e72019-11-14 19:39:52 -0700641 *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -0600642 return do_hashed;
643}
644
Jackie Liua197f662019-11-08 08:09:12 -0700645static inline void io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -0600646{
Jackie Liua197f662019-11-08 08:09:12 -0700647 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe94ae5e72019-11-14 19:39:52 -0700648 struct io_kiocb *link;
649 bool do_hashed;
650
651 do_hashed = io_prep_async_work(req, &link);
Jens Axboe561fb042019-10-24 07:25:42 -0600652
653 trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work,
654 req->flags);
655 if (!do_hashed) {
656 io_wq_enqueue(ctx->io_wq, &req->work);
657 } else {
658 io_wq_enqueue_hashed(ctx->io_wq, &req->work,
659 file_inode(req->file));
660 }
Jens Axboe94ae5e72019-11-14 19:39:52 -0700661
662 if (link)
663 io_queue_linked_timeout(link);
Jens Axboe18d9be12019-09-10 09:13:05 -0600664}
665
Jens Axboe5262f562019-09-17 12:26:57 -0600666static void io_kill_timeout(struct io_kiocb *req)
667{
668 int ret;
669
Jens Axboe2d283902019-12-04 11:08:05 -0700670 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe5262f562019-09-17 12:26:57 -0600671 if (ret != -1) {
672 atomic_inc(&req->ctx->cq_timeouts);
Jens Axboe842f9612019-10-29 12:34:10 -0600673 list_del_init(&req->list);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700674 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800675 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -0600676 }
677}
678
679static void io_kill_timeouts(struct io_ring_ctx *ctx)
680{
681 struct io_kiocb *req, *tmp;
682
683 spin_lock_irq(&ctx->completion_lock);
684 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
685 io_kill_timeout(req);
686 spin_unlock_irq(&ctx->completion_lock);
687}
688
Jens Axboede0617e2019-04-06 21:51:27 -0600689static void io_commit_cqring(struct io_ring_ctx *ctx)
690{
691 struct io_kiocb *req;
692
Jens Axboe5262f562019-09-17 12:26:57 -0600693 while ((req = io_get_timeout_req(ctx)) != NULL)
694 io_kill_timeout(req);
695
Jens Axboede0617e2019-04-06 21:51:27 -0600696 __io_commit_cqring(ctx);
697
698 while ((req = io_get_deferred_req(ctx)) != NULL) {
699 req->flags |= REQ_F_IO_DRAINED;
Jackie Liua197f662019-11-08 08:09:12 -0700700 io_queue_async_work(req);
Jens Axboede0617e2019-04-06 21:51:27 -0600701 }
702}
703
Jens Axboe2b188cc2019-01-07 10:46:33 -0700704static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
705{
Hristo Venev75b28af2019-08-26 17:23:46 +0000706 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700707 unsigned tail;
708
709 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +0200710 /*
711 * writes to the cq entry need to come after reading head; the
712 * control dependency is enough as we're using WRITE_ONCE to
713 * fill the cq entry
714 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000715 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700716 return NULL;
717
718 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +0000719 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -0700720}
721
Jens Axboe8c838782019-03-12 15:48:16 -0600722static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
723{
724 if (waitqueue_active(&ctx->wait))
725 wake_up(&ctx->wait);
726 if (waitqueue_active(&ctx->sqo_wait))
727 wake_up(&ctx->sqo_wait);
Jens Axboe9b402842019-04-11 11:45:41 -0600728 if (ctx->cq_ev_fd)
729 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -0600730}
731
Jens Axboec4a2ed72019-11-21 21:01:26 -0700732/* Returns true if there are no backlogged entries after the flush */
733static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700734{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700735 struct io_rings *rings = ctx->rings;
736 struct io_uring_cqe *cqe;
737 struct io_kiocb *req;
738 unsigned long flags;
739 LIST_HEAD(list);
740
741 if (!force) {
742 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -0700743 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700744 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
745 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -0700746 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700747 }
748
749 spin_lock_irqsave(&ctx->completion_lock, flags);
750
751 /* if force is set, the ring is going away. always drop after that */
752 if (force)
753 ctx->cq_overflow_flushed = true;
754
Jens Axboec4a2ed72019-11-21 21:01:26 -0700755 cqe = NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700756 while (!list_empty(&ctx->cq_overflow_list)) {
757 cqe = io_get_cqring(ctx);
758 if (!cqe && !force)
759 break;
760
761 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
762 list);
763 list_move(&req->list, &list);
764 if (cqe) {
765 WRITE_ONCE(cqe->user_data, req->user_data);
766 WRITE_ONCE(cqe->res, req->result);
767 WRITE_ONCE(cqe->flags, 0);
768 } else {
769 WRITE_ONCE(ctx->rings->cq_overflow,
770 atomic_inc_return(&ctx->cached_cq_overflow));
771 }
772 }
773
774 io_commit_cqring(ctx);
775 spin_unlock_irqrestore(&ctx->completion_lock, flags);
776 io_cqring_ev_posted(ctx);
777
778 while (!list_empty(&list)) {
779 req = list_first_entry(&list, struct io_kiocb, list);
780 list_del(&req->list);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800781 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700782 }
Jens Axboec4a2ed72019-11-21 21:01:26 -0700783
784 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700785}
786
Jens Axboe78e19bb2019-11-06 15:21:34 -0700787static void io_cqring_fill_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700788{
Jens Axboe78e19bb2019-11-06 15:21:34 -0700789 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700790 struct io_uring_cqe *cqe;
791
Jens Axboe78e19bb2019-11-06 15:21:34 -0700792 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -0700793
Jens Axboe2b188cc2019-01-07 10:46:33 -0700794 /*
795 * If we can't get a cq entry, userspace overflowed the
796 * submission (by quite a lot). Increment the overflow count in
797 * the ring.
798 */
799 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700800 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -0700801 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700802 WRITE_ONCE(cqe->res, res);
803 WRITE_ONCE(cqe->flags, 0);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700804 } else if (ctx->cq_overflow_flushed) {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700805 WRITE_ONCE(ctx->rings->cq_overflow,
806 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700807 } else {
808 refcount_inc(&req->refs);
809 req->result = res;
810 list_add_tail(&req->list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700811 }
812}
813
Jens Axboe78e19bb2019-11-06 15:21:34 -0700814static void io_cqring_add_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700815{
Jens Axboe78e19bb2019-11-06 15:21:34 -0700816 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700817 unsigned long flags;
818
819 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700820 io_cqring_fill_event(req, res);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700821 io_commit_cqring(ctx);
822 spin_unlock_irqrestore(&ctx->completion_lock, flags);
823
Jens Axboe8c838782019-03-12 15:48:16 -0600824 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700825}
826
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700827static inline bool io_is_fallback_req(struct io_kiocb *req)
828{
829 return req == (struct io_kiocb *)
830 ((unsigned long) req->ctx->fallback_req & ~1UL);
831}
832
833static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
834{
835 struct io_kiocb *req;
836
837 req = ctx->fallback_req;
838 if (!test_and_set_bit_lock(0, (unsigned long *) ctx->fallback_req))
839 return req;
840
841 return NULL;
842}
843
Jens Axboe2579f912019-01-09 09:10:43 -0700844static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
845 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700846{
Jens Axboefd6fab22019-03-14 16:30:06 -0600847 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700848 struct io_kiocb *req;
849
850 if (!percpu_ref_tryget(&ctx->refs))
851 return NULL;
852
Jens Axboe2579f912019-01-09 09:10:43 -0700853 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -0600854 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -0700855 if (unlikely(!req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700856 goto fallback;
Jens Axboe2579f912019-01-09 09:10:43 -0700857 } else if (!state->free_reqs) {
858 size_t sz;
859 int ret;
860
861 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -0600862 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
863
864 /*
865 * Bulk alloc is all-or-nothing. If we fail to get a batch,
866 * retry single alloc to be on the safe side.
867 */
868 if (unlikely(ret <= 0)) {
869 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
870 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700871 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -0600872 ret = 1;
873 }
Jens Axboe2579f912019-01-09 09:10:43 -0700874 state->free_reqs = ret - 1;
875 state->cur_req = 1;
876 req = state->reqs[0];
877 } else {
878 req = state->reqs[state->cur_req];
879 state->free_reqs--;
880 state->cur_req++;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700881 }
882
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700883got_it:
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700884 req->io = NULL;
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300885 req->ring_file = NULL;
Jens Axboe60c112b2019-06-21 10:20:18 -0600886 req->file = NULL;
Jens Axboe2579f912019-01-09 09:10:43 -0700887 req->ctx = ctx;
888 req->flags = 0;
Jens Axboee65ef562019-03-12 10:16:44 -0600889 /* one is dropped after submission, the other at completion */
890 refcount_set(&req->refs, 2);
Jens Axboe9e645e112019-05-10 16:07:28 -0600891 req->result = 0;
Jens Axboe561fb042019-10-24 07:25:42 -0600892 INIT_IO_WORK(&req->work, io_wq_submit_work);
Jens Axboe2579f912019-01-09 09:10:43 -0700893 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700894fallback:
895 req = io_get_fallback_req(ctx);
896 if (req)
897 goto got_it;
Pavel Begunkov6805b322019-10-08 02:18:42 +0300898 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700899 return NULL;
900}
901
Jens Axboedef596e2019-01-09 08:59:42 -0700902static void io_free_req_many(struct io_ring_ctx *ctx, void **reqs, int *nr)
903{
904 if (*nr) {
905 kmem_cache_free_bulk(req_cachep, *nr, reqs);
Pavel Begunkov6805b322019-10-08 02:18:42 +0300906 percpu_ref_put_many(&ctx->refs, *nr);
Jens Axboedef596e2019-01-09 08:59:42 -0700907 *nr = 0;
908 }
909}
910
Jens Axboe9e645e112019-05-10 16:07:28 -0600911static void __io_free_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700912{
Jens Axboefcb323c2019-10-24 12:39:47 -0600913 struct io_ring_ctx *ctx = req->ctx;
914
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700915 if (req->io)
916 kfree(req->io);
Jens Axboe09bb8392019-03-13 12:39:28 -0600917 if (req->file && !(req->flags & REQ_F_FIXED_FILE))
918 fput(req->file);
Jens Axboefcb323c2019-10-24 12:39:47 -0600919 if (req->flags & REQ_F_INFLIGHT) {
920 unsigned long flags;
921
922 spin_lock_irqsave(&ctx->inflight_lock, flags);
923 list_del(&req->inflight_entry);
924 if (waitqueue_active(&ctx->inflight_wait))
925 wake_up(&ctx->inflight_wait);
926 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
927 }
928 percpu_ref_put(&ctx->refs);
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700929 if (likely(!io_is_fallback_req(req)))
930 kmem_cache_free(req_cachep, req);
931 else
932 clear_bit_unlock(0, (unsigned long *) ctx->fallback_req);
Jens Axboee65ef562019-03-12 10:16:44 -0600933}
934
Jackie Liua197f662019-11-08 08:09:12 -0700935static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -0600936{
Jackie Liua197f662019-11-08 08:09:12 -0700937 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -0700938 int ret;
939
Jens Axboe2d283902019-12-04 11:08:05 -0700940 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe2665abf2019-11-05 12:40:47 -0700941 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -0700942 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -0700943 io_commit_cqring(ctx);
944 req->flags &= ~REQ_F_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +0800945 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -0700946 return true;
947 }
948
949 return false;
950}
951
Jens Axboeba816ad2019-09-28 11:36:45 -0600952static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -0600953{
Jens Axboe2665abf2019-11-05 12:40:47 -0700954 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -0700955 bool wake_ev = false;
Jens Axboe9e645e112019-05-10 16:07:28 -0600956
Jens Axboe4d7dd462019-11-20 13:03:52 -0700957 /* Already got next link */
958 if (req->flags & REQ_F_LINK_NEXT)
959 return;
960
Jens Axboe9e645e112019-05-10 16:07:28 -0600961 /*
962 * The list should never be empty when we are called here. But could
963 * potentially happen if the chain is messed up, check to be on the
964 * safe side.
965 */
Pavel Begunkov44932332019-12-05 16:16:35 +0300966 while (!list_empty(&req->link_list)) {
967 struct io_kiocb *nxt = list_first_entry(&req->link_list,
968 struct io_kiocb, link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700969
Pavel Begunkov44932332019-12-05 16:16:35 +0300970 if (unlikely((req->flags & REQ_F_LINK_TIMEOUT) &&
971 (nxt->flags & REQ_F_TIMEOUT))) {
972 list_del_init(&nxt->link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700973 wake_ev |= io_link_cancel_timeout(nxt);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700974 req->flags &= ~REQ_F_LINK_TIMEOUT;
975 continue;
976 }
Jens Axboe9e645e112019-05-10 16:07:28 -0600977
Pavel Begunkov44932332019-12-05 16:16:35 +0300978 list_del_init(&req->link_list);
979 if (!list_empty(&nxt->link_list))
980 nxt->flags |= REQ_F_LINK;
Pavel Begunkovb18fdf72019-11-21 23:21:02 +0300981 *nxtptr = nxt;
Jens Axboe94ae5e72019-11-14 19:39:52 -0700982 break;
Jens Axboe9e645e112019-05-10 16:07:28 -0600983 }
Jens Axboe2665abf2019-11-05 12:40:47 -0700984
Jens Axboe4d7dd462019-11-20 13:03:52 -0700985 req->flags |= REQ_F_LINK_NEXT;
Jens Axboe2665abf2019-11-05 12:40:47 -0700986 if (wake_ev)
987 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -0600988}
989
990/*
991 * Called if REQ_F_LINK is set, and we fail the head request
992 */
993static void io_fail_links(struct io_kiocb *req)
994{
Jens Axboe2665abf2019-11-05 12:40:47 -0700995 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -0700996 unsigned long flags;
997
998 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -0600999
1000 while (!list_empty(&req->link_list)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03001001 struct io_kiocb *link = list_first_entry(&req->link_list,
1002 struct io_kiocb, link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06001003
Pavel Begunkov44932332019-12-05 16:16:35 +03001004 list_del_init(&link->link_list);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001005 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001006
1007 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001008 link->sqe->opcode == IORING_OP_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -07001009 io_link_cancel_timeout(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001010 } else {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001011 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe978db572019-11-14 22:39:04 -07001012 __io_double_put_req(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001013 }
Jens Axboe5d960722019-11-19 15:31:28 -07001014 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -06001015 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001016
1017 io_commit_cqring(ctx);
1018 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1019 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001020}
1021
Jens Axboe4d7dd462019-11-20 13:03:52 -07001022static void io_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -06001023{
Jens Axboe4d7dd462019-11-20 13:03:52 -07001024 if (likely(!(req->flags & REQ_F_LINK)))
Jens Axboe2665abf2019-11-05 12:40:47 -07001025 return;
Jens Axboe2665abf2019-11-05 12:40:47 -07001026
Jens Axboe9e645e112019-05-10 16:07:28 -06001027 /*
1028 * If LINK is set, we have dependent requests in this chain. If we
1029 * didn't fail this request, queue the first one up, moving any other
1030 * dependencies to the next request. In case of failure, fail the rest
1031 * of the chain.
1032 */
Jens Axboe2665abf2019-11-05 12:40:47 -07001033 if (req->flags & REQ_F_FAIL_LINK) {
1034 io_fail_links(req);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001035 } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) ==
1036 REQ_F_LINK_TIMEOUT) {
Jens Axboe2665abf2019-11-05 12:40:47 -07001037 struct io_ring_ctx *ctx = req->ctx;
1038 unsigned long flags;
1039
1040 /*
1041 * If this is a timeout link, we could be racing with the
1042 * timeout timer. Grab the completion lock for this case to
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001043 * protect against that.
Jens Axboe2665abf2019-11-05 12:40:47 -07001044 */
1045 spin_lock_irqsave(&ctx->completion_lock, flags);
1046 io_req_link_next(req, nxt);
1047 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1048 } else {
1049 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -06001050 }
Jens Axboe4d7dd462019-11-20 13:03:52 -07001051}
Jens Axboe9e645e112019-05-10 16:07:28 -06001052
Jackie Liuc69f8db2019-11-09 11:00:08 +08001053static void io_free_req(struct io_kiocb *req)
1054{
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001055 struct io_kiocb *nxt = NULL;
1056
1057 io_req_find_next(req, &nxt);
Pavel Begunkov70cf9f32019-11-21 23:21:00 +03001058 __io_free_req(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001059
1060 if (nxt)
1061 io_queue_async_work(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08001062}
1063
Jens Axboeba816ad2019-09-28 11:36:45 -06001064/*
1065 * Drop reference to request, return next in chain (if there is one) if this
1066 * was the last reference to this request.
1067 */
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001068__attribute__((nonnull))
Jackie Liuec9c02a2019-11-08 23:50:36 +08001069static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboee65ef562019-03-12 10:16:44 -06001070{
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001071 io_req_find_next(req, nxtptr);
Jens Axboe4d7dd462019-11-20 13:03:52 -07001072
Jens Axboee65ef562019-03-12 10:16:44 -06001073 if (refcount_dec_and_test(&req->refs))
Jens Axboe4d7dd462019-11-20 13:03:52 -07001074 __io_free_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001075}
1076
Jens Axboe2b188cc2019-01-07 10:46:33 -07001077static void io_put_req(struct io_kiocb *req)
1078{
Jens Axboedef596e2019-01-09 08:59:42 -07001079 if (refcount_dec_and_test(&req->refs))
1080 io_free_req(req);
1081}
1082
Jens Axboe978db572019-11-14 22:39:04 -07001083/*
1084 * Must only be used if we don't need to care about links, usually from
1085 * within the completion handling itself.
1086 */
1087static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06001088{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001089 /* drop both submit and complete references */
1090 if (refcount_sub_and_test(2, &req->refs))
1091 __io_free_req(req);
1092}
1093
Jens Axboe978db572019-11-14 22:39:04 -07001094static void io_double_put_req(struct io_kiocb *req)
1095{
1096 /* drop both submit and complete references */
1097 if (refcount_sub_and_test(2, &req->refs))
1098 io_free_req(req);
1099}
1100
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001101static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06001102{
Jens Axboe84f97dc2019-11-06 11:27:53 -07001103 struct io_rings *rings = ctx->rings;
1104
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001105 /*
1106 * noflush == true is from the waitqueue handler, just ensure we wake
1107 * up the task, and the next invocation will flush the entries. We
1108 * cannot safely to it from here.
1109 */
1110 if (noflush && !list_empty(&ctx->cq_overflow_list))
1111 return -1U;
1112
1113 io_cqring_overflow_flush(ctx, false);
1114
Jens Axboea3a0e432019-08-20 11:03:11 -06001115 /* See comment at the top of this file */
1116 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00001117 return READ_ONCE(rings->cq.tail) - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06001118}
1119
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03001120static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1121{
1122 struct io_rings *rings = ctx->rings;
1123
1124 /* make sure SQ entry isn't read before tail */
1125 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1126}
1127
Jens Axboedef596e2019-01-09 08:59:42 -07001128/*
1129 * Find and free completed poll iocbs
1130 */
1131static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
1132 struct list_head *done)
1133{
1134 void *reqs[IO_IOPOLL_BATCH];
1135 struct io_kiocb *req;
Jens Axboe09bb8392019-03-13 12:39:28 -06001136 int to_free;
Jens Axboedef596e2019-01-09 08:59:42 -07001137
Jens Axboe09bb8392019-03-13 12:39:28 -06001138 to_free = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001139 while (!list_empty(done)) {
1140 req = list_first_entry(done, struct io_kiocb, list);
1141 list_del(&req->list);
1142
Jens Axboe78e19bb2019-11-06 15:21:34 -07001143 io_cqring_fill_event(req, req->result);
Jens Axboedef596e2019-01-09 08:59:42 -07001144 (*nr_events)++;
1145
Jens Axboe09bb8392019-03-13 12:39:28 -06001146 if (refcount_dec_and_test(&req->refs)) {
1147 /* If we're not using fixed files, we have to pair the
1148 * completion part with the file put. Use regular
1149 * completions for those, only batch free for fixed
Jens Axboe9e645e112019-05-10 16:07:28 -06001150 * file and non-linked commands.
Jens Axboe09bb8392019-03-13 12:39:28 -06001151 */
Jens Axboe1a6b74f2019-12-02 10:33:15 -07001152 if (((req->flags & (REQ_F_FIXED_FILE|REQ_F_LINK)) ==
1153 REQ_F_FIXED_FILE) && !io_is_fallback_req(req) &&
1154 !req->io) {
Jens Axboe09bb8392019-03-13 12:39:28 -06001155 reqs[to_free++] = req;
1156 if (to_free == ARRAY_SIZE(reqs))
1157 io_free_req_many(ctx, reqs, &to_free);
Jens Axboe6b063142019-01-10 22:13:58 -07001158 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001159 io_free_req(req);
Jens Axboe6b063142019-01-10 22:13:58 -07001160 }
Jens Axboe9a56a232019-01-09 09:06:50 -07001161 }
Jens Axboedef596e2019-01-09 08:59:42 -07001162 }
Jens Axboedef596e2019-01-09 08:59:42 -07001163
Jens Axboe09bb8392019-03-13 12:39:28 -06001164 io_commit_cqring(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07001165 io_free_req_many(ctx, reqs, &to_free);
1166}
1167
1168static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1169 long min)
1170{
1171 struct io_kiocb *req, *tmp;
1172 LIST_HEAD(done);
1173 bool spin;
1174 int ret;
1175
1176 /*
1177 * Only spin for completions if we don't have multiple devices hanging
1178 * off our complete list, and we're under the requested amount.
1179 */
1180 spin = !ctx->poll_multi_file && *nr_events < min;
1181
1182 ret = 0;
1183 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
1184 struct kiocb *kiocb = &req->rw;
1185
1186 /*
1187 * Move completed entries to our local list. If we find a
1188 * request that requires polling, break out and complete
1189 * the done list first, if we have entries there.
1190 */
1191 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
1192 list_move_tail(&req->list, &done);
1193 continue;
1194 }
1195 if (!list_empty(&done))
1196 break;
1197
1198 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1199 if (ret < 0)
1200 break;
1201
1202 if (ret && spin)
1203 spin = false;
1204 ret = 0;
1205 }
1206
1207 if (!list_empty(&done))
1208 io_iopoll_complete(ctx, nr_events, &done);
1209
1210 return ret;
1211}
1212
1213/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08001214 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07001215 * non-spinning poll check - we'll still enter the driver poll loop, but only
1216 * as a non-spinning completion check.
1217 */
1218static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
1219 long min)
1220{
Jens Axboe08f54392019-08-21 22:19:11 -06001221 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07001222 int ret;
1223
1224 ret = io_do_iopoll(ctx, nr_events, min);
1225 if (ret < 0)
1226 return ret;
1227 if (!min || *nr_events >= min)
1228 return 0;
1229 }
1230
1231 return 1;
1232}
1233
1234/*
1235 * We can't just wait for polled events to come to us, we have to actively
1236 * find and complete them.
1237 */
1238static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
1239{
1240 if (!(ctx->flags & IORING_SETUP_IOPOLL))
1241 return;
1242
1243 mutex_lock(&ctx->uring_lock);
1244 while (!list_empty(&ctx->poll_list)) {
1245 unsigned int nr_events = 0;
1246
1247 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -06001248
1249 /*
1250 * Ensure we allow local-to-the-cpu processing to take place,
1251 * in this case we need to ensure that we reap all events.
1252 */
1253 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -07001254 }
1255 mutex_unlock(&ctx->uring_lock);
1256}
1257
Jens Axboe2b2ed972019-10-25 10:06:15 -06001258static int __io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1259 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07001260{
Jens Axboe2b2ed972019-10-25 10:06:15 -06001261 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001262
1263 do {
1264 int tmin = 0;
1265
Jens Axboe500f9fb2019-08-19 12:15:59 -06001266 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06001267 * Don't enter poll loop if we already have events pending.
1268 * If we do, we can potentially be spinning for commands that
1269 * already triggered a CQE (eg in error).
1270 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001271 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06001272 break;
1273
1274 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06001275 * If a submit got punted to a workqueue, we can have the
1276 * application entering polling for a command before it gets
1277 * issued. That app will hold the uring_lock for the duration
1278 * of the poll right here, so we need to take a breather every
1279 * now and then to ensure that the issue has a chance to add
1280 * the poll to the issued list. Otherwise we can spin here
1281 * forever, while the workqueue is stuck trying to acquire the
1282 * very same mutex.
1283 */
1284 if (!(++iters & 7)) {
1285 mutex_unlock(&ctx->uring_lock);
1286 mutex_lock(&ctx->uring_lock);
1287 }
1288
Jens Axboedef596e2019-01-09 08:59:42 -07001289 if (*nr_events < min)
1290 tmin = min - *nr_events;
1291
1292 ret = io_iopoll_getevents(ctx, nr_events, tmin);
1293 if (ret <= 0)
1294 break;
1295 ret = 0;
1296 } while (min && !*nr_events && !need_resched());
1297
Jens Axboe2b2ed972019-10-25 10:06:15 -06001298 return ret;
1299}
1300
1301static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1302 long min)
1303{
1304 int ret;
1305
1306 /*
1307 * We disallow the app entering submit/complete with polling, but we
1308 * still need to lock the ring to prevent racing with polled issue
1309 * that got punted to a workqueue.
1310 */
1311 mutex_lock(&ctx->uring_lock);
1312 ret = __io_iopoll_check(ctx, nr_events, min);
Jens Axboe500f9fb2019-08-19 12:15:59 -06001313 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001314 return ret;
1315}
1316
Jens Axboe491381ce2019-10-17 09:20:46 -06001317static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001318{
Jens Axboe491381ce2019-10-17 09:20:46 -06001319 /*
1320 * Tell lockdep we inherited freeze protection from submission
1321 * thread.
1322 */
1323 if (req->flags & REQ_F_ISREG) {
1324 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001325
Jens Axboe491381ce2019-10-17 09:20:46 -06001326 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001327 }
Jens Axboe491381ce2019-10-17 09:20:46 -06001328 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001329}
1330
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001331static inline void req_set_fail_links(struct io_kiocb *req)
1332{
1333 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1334 req->flags |= REQ_F_FAIL_LINK;
1335}
1336
Jens Axboeba816ad2019-09-28 11:36:45 -06001337static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001338{
1339 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1340
Jens Axboe491381ce2019-10-17 09:20:46 -06001341 if (kiocb->ki_flags & IOCB_WRITE)
1342 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001343
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001344 if (res != req->result)
1345 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001346 io_cqring_add_event(req, res);
Jens Axboeba816ad2019-09-28 11:36:45 -06001347}
1348
1349static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1350{
1351 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1352
1353 io_complete_rw_common(kiocb, res);
Jens Axboee65ef562019-03-12 10:16:44 -06001354 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001355}
1356
Jens Axboeba816ad2019-09-28 11:36:45 -06001357static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res)
1358{
1359 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001360 struct io_kiocb *nxt = NULL;
Jens Axboeba816ad2019-09-28 11:36:45 -06001361
1362 io_complete_rw_common(kiocb, res);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001363 io_put_req_find_next(req, &nxt);
1364
1365 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001366}
1367
Jens Axboedef596e2019-01-09 08:59:42 -07001368static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1369{
1370 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw);
1371
Jens Axboe491381ce2019-10-17 09:20:46 -06001372 if (kiocb->ki_flags & IOCB_WRITE)
1373 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001374
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001375 if (res != req->result)
1376 req_set_fail_links(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06001377 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -07001378 if (res != -EAGAIN)
1379 req->flags |= REQ_F_IOPOLL_COMPLETED;
1380}
1381
1382/*
1383 * After the iocb has been issued, it's safe to be found on the poll list.
1384 * Adding the kiocb to the list AFTER submission ensures that we don't
1385 * find it from a io_iopoll_getevents() thread before the issuer is done
1386 * accessing the kiocb cookie.
1387 */
1388static void io_iopoll_req_issued(struct io_kiocb *req)
1389{
1390 struct io_ring_ctx *ctx = req->ctx;
1391
1392 /*
1393 * Track whether we have multiple files in our lists. This will impact
1394 * how we do polling eventually, not spinning if we're on potentially
1395 * different devices.
1396 */
1397 if (list_empty(&ctx->poll_list)) {
1398 ctx->poll_multi_file = false;
1399 } else if (!ctx->poll_multi_file) {
1400 struct io_kiocb *list_req;
1401
1402 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1403 list);
1404 if (list_req->rw.ki_filp != req->rw.ki_filp)
1405 ctx->poll_multi_file = true;
1406 }
1407
1408 /*
1409 * For fast devices, IO may have already completed. If it has, add
1410 * it to the front so we find it first.
1411 */
1412 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1413 list_add(&req->list, &ctx->poll_list);
1414 else
1415 list_add_tail(&req->list, &ctx->poll_list);
1416}
1417
Jens Axboe3d6770f2019-04-13 11:50:54 -06001418static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07001419{
Jens Axboe3d6770f2019-04-13 11:50:54 -06001420 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -07001421 int diff = state->has_refs - state->used_refs;
1422
1423 if (diff)
1424 fput_many(state->file, diff);
1425 state->file = NULL;
1426 }
1427}
1428
1429/*
1430 * Get as many references to a file as we have IOs left in this submission,
1431 * assuming most submissions are for one file, or at least that each file
1432 * has more than one submission.
1433 */
1434static struct file *io_file_get(struct io_submit_state *state, int fd)
1435{
1436 if (!state)
1437 return fget(fd);
1438
1439 if (state->file) {
1440 if (state->fd == fd) {
1441 state->used_refs++;
1442 state->ios_left--;
1443 return state->file;
1444 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06001445 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07001446 }
1447 state->file = fget_many(fd, state->ios_left);
1448 if (!state->file)
1449 return NULL;
1450
1451 state->fd = fd;
1452 state->has_refs = state->ios_left;
1453 state->used_refs = 1;
1454 state->ios_left--;
1455 return state->file;
1456}
1457
Jens Axboe2b188cc2019-01-07 10:46:33 -07001458/*
1459 * If we tracked the file through the SCM inflight mechanism, we could support
1460 * any file. For now, just ensure that anything potentially problematic is done
1461 * inline.
1462 */
1463static bool io_file_supports_async(struct file *file)
1464{
1465 umode_t mode = file_inode(file)->i_mode;
1466
Jens Axboe10d59342019-12-09 20:16:22 -07001467 if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07001468 return true;
1469 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
1470 return true;
1471
1472 return false;
1473}
1474
Pavel Begunkov267bc902019-11-07 01:41:08 +03001475static int io_prep_rw(struct io_kiocb *req, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001476{
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001477 const struct io_uring_sqe *sqe = req->sqe;
Jens Axboedef596e2019-01-09 08:59:42 -07001478 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001479 struct kiocb *kiocb = &req->rw;
Jens Axboe09bb8392019-03-13 12:39:28 -06001480 unsigned ioprio;
1481 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001482
Jens Axboe09bb8392019-03-13 12:39:28 -06001483 if (!req->file)
1484 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001485
Jens Axboe491381ce2019-10-17 09:20:46 -06001486 if (S_ISREG(file_inode(req->file)->i_mode))
1487 req->flags |= REQ_F_ISREG;
1488
Jens Axboe2b188cc2019-01-07 10:46:33 -07001489 kiocb->ki_pos = READ_ONCE(sqe->off);
1490 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
1491 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
1492
1493 ioprio = READ_ONCE(sqe->ioprio);
1494 if (ioprio) {
1495 ret = ioprio_check_cap(ioprio);
1496 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06001497 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001498
1499 kiocb->ki_ioprio = ioprio;
1500 } else
1501 kiocb->ki_ioprio = get_current_ioprio();
1502
1503 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
1504 if (unlikely(ret))
Jens Axboe09bb8392019-03-13 12:39:28 -06001505 return ret;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001506
1507 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06001508 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
1509 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02001510 req->flags |= REQ_F_NOWAIT;
1511
1512 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001513 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001514
Jens Axboedef596e2019-01-09 08:59:42 -07001515 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07001516 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
1517 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06001518 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001519
Jens Axboedef596e2019-01-09 08:59:42 -07001520 kiocb->ki_flags |= IOCB_HIPRI;
1521 kiocb->ki_complete = io_complete_rw_iopoll;
Jens Axboe6873e0b2019-10-30 13:53:09 -06001522 req->result = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001523 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001524 if (kiocb->ki_flags & IOCB_HIPRI)
1525 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07001526 kiocb->ki_complete = io_complete_rw;
1527 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001528 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001529}
1530
1531static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
1532{
1533 switch (ret) {
1534 case -EIOCBQUEUED:
1535 break;
1536 case -ERESTARTSYS:
1537 case -ERESTARTNOINTR:
1538 case -ERESTARTNOHAND:
1539 case -ERESTART_RESTARTBLOCK:
1540 /*
1541 * We can't just restart the syscall, since previously
1542 * submitted sqes may already be in progress. Just fail this
1543 * IO with EINTR.
1544 */
1545 ret = -EINTR;
1546 /* fall through */
1547 default:
1548 kiocb->ki_complete(kiocb, ret, 0);
1549 }
1550}
1551
Jens Axboeba816ad2019-09-28 11:36:45 -06001552static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt,
1553 bool in_async)
1554{
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001555 if (in_async && ret >= 0 && kiocb->ki_complete == io_complete_rw)
Jens Axboeba816ad2019-09-28 11:36:45 -06001556 *nxt = __io_complete_rw(kiocb, ret);
1557 else
1558 io_rw_done(kiocb, ret);
1559}
1560
Pavel Begunkov7d009162019-11-25 23:14:40 +03001561static ssize_t io_import_fixed(struct io_ring_ctx *ctx, int rw,
1562 const struct io_uring_sqe *sqe,
1563 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07001564{
1565 size_t len = READ_ONCE(sqe->len);
1566 struct io_mapped_ubuf *imu;
1567 unsigned index, buf_index;
1568 size_t offset;
1569 u64 buf_addr;
1570
1571 /* attempt to use fixed buffers without having provided iovecs */
1572 if (unlikely(!ctx->user_bufs))
1573 return -EFAULT;
1574
1575 buf_index = READ_ONCE(sqe->buf_index);
1576 if (unlikely(buf_index >= ctx->nr_user_bufs))
1577 return -EFAULT;
1578
1579 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
1580 imu = &ctx->user_bufs[index];
1581 buf_addr = READ_ONCE(sqe->addr);
1582
1583 /* overflow */
1584 if (buf_addr + len < buf_addr)
1585 return -EFAULT;
1586 /* not inside the mapped region */
1587 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
1588 return -EFAULT;
1589
1590 /*
1591 * May not be a start of buffer, set size appropriately
1592 * and advance us to the beginning.
1593 */
1594 offset = buf_addr - imu->ubuf;
1595 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06001596
1597 if (offset) {
1598 /*
1599 * Don't use iov_iter_advance() here, as it's really slow for
1600 * using the latter parts of a big fixed buffer - it iterates
1601 * over each segment manually. We can cheat a bit here, because
1602 * we know that:
1603 *
1604 * 1) it's a BVEC iter, we set it up
1605 * 2) all bvecs are PAGE_SIZE in size, except potentially the
1606 * first and last bvec
1607 *
1608 * So just find our index, and adjust the iterator afterwards.
1609 * If the offset is within the first bvec (or the whole first
1610 * bvec, just use iov_iter_advance(). This makes it easier
1611 * since we can just skip the first segment, which may not
1612 * be PAGE_SIZE aligned.
1613 */
1614 const struct bio_vec *bvec = imu->bvec;
1615
1616 if (offset <= bvec->bv_len) {
1617 iov_iter_advance(iter, offset);
1618 } else {
1619 unsigned long seg_skip;
1620
1621 /* skip first vec */
1622 offset -= bvec->bv_len;
1623 seg_skip = 1 + (offset >> PAGE_SHIFT);
1624
1625 iter->bvec = bvec + seg_skip;
1626 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02001627 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001628 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001629 }
1630 }
1631
Jens Axboe5e559562019-11-13 16:12:46 -07001632 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001633}
1634
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001635static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
1636 struct iovec **iovec, struct iov_iter *iter)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001637{
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001638 const struct io_uring_sqe *sqe = req->sqe;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001639 void __user *buf = u64_to_user_ptr(READ_ONCE(sqe->addr));
1640 size_t sqe_len = READ_ONCE(sqe->len);
Jens Axboeedafcce2019-01-09 09:16:05 -07001641 u8 opcode;
1642
1643 /*
1644 * We're reading ->opcode for the second time, but the first read
1645 * doesn't care whether it's _FIXED or not, so it doesn't matter
1646 * whether ->opcode changes concurrently. The first read does care
1647 * about whether it is a READ or a WRITE, so we don't trust this read
1648 * for that purpose and instead let the caller pass in the read/write
1649 * flag.
1650 */
1651 opcode = READ_ONCE(sqe->opcode);
Pavel Begunkov7d009162019-11-25 23:14:40 +03001652 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07001653 *iovec = NULL;
Pavel Begunkov7d009162019-11-25 23:14:40 +03001654 return io_import_fixed(req->ctx, rw, sqe, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07001655 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001656
Jens Axboef67676d2019-12-02 11:03:47 -07001657 if (req->io) {
1658 struct io_async_rw *iorw = &req->io->rw;
1659
1660 *iovec = iorw->iov;
1661 iov_iter_init(iter, rw, *iovec, iorw->nr_segs, iorw->size);
1662 if (iorw->iov == iorw->fast_iov)
1663 *iovec = NULL;
1664 return iorw->size;
1665 }
1666
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001667 if (!req->has_user)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001668 return -EFAULT;
1669
1670#ifdef CONFIG_COMPAT
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001671 if (req->ctx->compat)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001672 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
1673 iovec, iter);
1674#endif
1675
1676 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
1677}
1678
Jens Axboe32960612019-09-23 11:05:34 -06001679/*
1680 * For files that don't have ->read_iter() and ->write_iter(), handle them
1681 * by looping over ->read() or ->write() manually.
1682 */
1683static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
1684 struct iov_iter *iter)
1685{
1686 ssize_t ret = 0;
1687
1688 /*
1689 * Don't support polled IO through this interface, and we can't
1690 * support non-blocking either. For the latter, this just causes
1691 * the kiocb to be handled from an async context.
1692 */
1693 if (kiocb->ki_flags & IOCB_HIPRI)
1694 return -EOPNOTSUPP;
1695 if (kiocb->ki_flags & IOCB_NOWAIT)
1696 return -EAGAIN;
1697
1698 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03001699 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06001700 ssize_t nr;
1701
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03001702 if (!iov_iter_is_bvec(iter)) {
1703 iovec = iov_iter_iovec(iter);
1704 } else {
1705 /* fixed buffers import bvec */
1706 iovec.iov_base = kmap(iter->bvec->bv_page)
1707 + iter->iov_offset;
1708 iovec.iov_len = min(iter->count,
1709 iter->bvec->bv_len - iter->iov_offset);
1710 }
1711
Jens Axboe32960612019-09-23 11:05:34 -06001712 if (rw == READ) {
1713 nr = file->f_op->read(file, iovec.iov_base,
1714 iovec.iov_len, &kiocb->ki_pos);
1715 } else {
1716 nr = file->f_op->write(file, iovec.iov_base,
1717 iovec.iov_len, &kiocb->ki_pos);
1718 }
1719
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03001720 if (iov_iter_is_bvec(iter))
1721 kunmap(iter->bvec->bv_page);
1722
Jens Axboe32960612019-09-23 11:05:34 -06001723 if (nr < 0) {
1724 if (!ret)
1725 ret = nr;
1726 break;
1727 }
1728 ret += nr;
1729 if (nr != iovec.iov_len)
1730 break;
1731 iov_iter_advance(iter, nr);
1732 }
1733
1734 return ret;
1735}
1736
Jens Axboeb7bb4f72019-12-15 22:13:43 -07001737static void io_req_map_rw(struct io_kiocb *req, ssize_t io_size,
Jens Axboef67676d2019-12-02 11:03:47 -07001738 struct iovec *iovec, struct iovec *fast_iov,
1739 struct iov_iter *iter)
1740{
1741 req->io->rw.nr_segs = iter->nr_segs;
1742 req->io->rw.size = io_size;
1743 req->io->rw.iov = iovec;
1744 if (!req->io->rw.iov) {
1745 req->io->rw.iov = req->io->rw.fast_iov;
1746 memcpy(req->io->rw.iov, fast_iov,
1747 sizeof(struct iovec) * iter->nr_segs);
1748 }
1749}
1750
Jens Axboeb7bb4f72019-12-15 22:13:43 -07001751static int io_alloc_async_ctx(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07001752{
1753 req->io = kmalloc(sizeof(*req->io), GFP_KERNEL);
1754 if (req->io) {
Jens Axboef67676d2019-12-02 11:03:47 -07001755 memcpy(&req->io->sqe, req->sqe, sizeof(req->io->sqe));
1756 req->sqe = &req->io->sqe;
1757 return 0;
1758 }
1759
Jens Axboeb7bb4f72019-12-15 22:13:43 -07001760 return 1;
1761}
1762
1763static void io_rw_async(struct io_wq_work **workptr)
1764{
1765 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
1766 struct iovec *iov = NULL;
1767
1768 if (req->io->rw.iov != req->io->rw.fast_iov)
1769 iov = req->io->rw.iov;
1770 io_wq_submit_work(workptr);
1771 kfree(iov);
1772}
1773
1774static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size,
1775 struct iovec *iovec, struct iovec *fast_iov,
1776 struct iov_iter *iter)
1777{
1778 if (!req->io && io_alloc_async_ctx(req))
1779 return -ENOMEM;
1780
1781 io_req_map_rw(req, io_size, iovec, fast_iov, iter);
1782 req->work.func = io_rw_async;
1783 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07001784}
1785
1786static int io_read_prep(struct io_kiocb *req, struct iovec **iovec,
1787 struct iov_iter *iter, bool force_nonblock)
1788{
1789 ssize_t ret;
1790
1791 ret = io_prep_rw(req, force_nonblock);
1792 if (ret)
1793 return ret;
1794
1795 if (unlikely(!(req->file->f_mode & FMODE_READ)))
1796 return -EBADF;
1797
1798 return io_import_iovec(READ, req, iovec, iter);
1799}
1800
Pavel Begunkov267bc902019-11-07 01:41:08 +03001801static int io_read(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001802 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001803{
1804 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1805 struct kiocb *kiocb = &req->rw;
1806 struct iov_iter iter;
1807 struct file *file;
Jens Axboe31b51512019-01-18 22:56:34 -07001808 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07001809 ssize_t io_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001810
Jens Axboef67676d2019-12-02 11:03:47 -07001811 if (!req->io) {
1812 ret = io_read_prep(req, &iovec, &iter, force_nonblock);
1813 if (ret < 0)
1814 return ret;
1815 } else {
1816 ret = io_import_iovec(READ, req, &iovec, &iter);
1817 if (ret < 0)
1818 return ret;
1819 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001820
Jens Axboef67676d2019-12-02 11:03:47 -07001821 file = req->file;
1822 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06001823 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07001824 req->result = io_size;
1825
1826 /*
1827 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
1828 * we know to async punt it even if it was opened O_NONBLOCK
1829 */
1830 if (force_nonblock && !io_file_supports_async(file)) {
1831 req->flags |= REQ_F_MUST_PUNT;
1832 goto copy_iov;
1833 }
Jens Axboe9e645e112019-05-10 16:07:28 -06001834
Jens Axboe31b51512019-01-18 22:56:34 -07001835 iov_count = iov_iter_count(&iter);
1836 ret = rw_verify_area(READ, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001837 if (!ret) {
1838 ssize_t ret2;
1839
Jens Axboe32960612019-09-23 11:05:34 -06001840 if (file->f_op->read_iter)
1841 ret2 = call_read_iter(file, kiocb, &iter);
1842 else
1843 ret2 = loop_rw_iter(READ, file, kiocb, &iter);
1844
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001845 /*
1846 * In case of a short read, punt to async. This can happen
1847 * if we have data partially cached. Alternatively we can
1848 * return the short read, in which case the application will
1849 * need to issue another SQE and wait for it. That SQE will
1850 * need async punt anyway, so it's more efficient to do it
1851 * here.
1852 */
Jens Axboe491381ce2019-10-17 09:20:46 -06001853 if (force_nonblock && !(req->flags & REQ_F_NOWAIT) &&
1854 (req->flags & REQ_F_ISREG) &&
Jens Axboef67676d2019-12-02 11:03:47 -07001855 ret2 > 0 && ret2 < io_size)
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001856 ret2 = -EAGAIN;
1857 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboef67676d2019-12-02 11:03:47 -07001858 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001859 kiocb_done(kiocb, ret2, nxt, req->in_async);
Jens Axboef67676d2019-12-02 11:03:47 -07001860 } else {
1861copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07001862 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07001863 inline_vecs, &iter);
1864 if (ret)
1865 goto out_free;
1866 return -EAGAIN;
1867 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001868 }
Jens Axboef67676d2019-12-02 11:03:47 -07001869out_free:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07001870 if (!io_wq_current_is_worker())
1871 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001872 return ret;
1873}
1874
Jens Axboef67676d2019-12-02 11:03:47 -07001875static int io_write_prep(struct io_kiocb *req, struct iovec **iovec,
1876 struct iov_iter *iter, bool force_nonblock)
1877{
1878 ssize_t ret;
1879
1880 ret = io_prep_rw(req, force_nonblock);
1881 if (ret)
1882 return ret;
1883
1884 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
1885 return -EBADF;
1886
1887 return io_import_iovec(WRITE, req, iovec, iter);
1888}
1889
Pavel Begunkov267bc902019-11-07 01:41:08 +03001890static int io_write(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001891 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001892{
1893 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
1894 struct kiocb *kiocb = &req->rw;
1895 struct iov_iter iter;
1896 struct file *file;
Jens Axboe31b51512019-01-18 22:56:34 -07001897 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07001898 ssize_t ret, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001899
Jens Axboef67676d2019-12-02 11:03:47 -07001900 if (!req->io) {
1901 ret = io_write_prep(req, &iovec, &iter, force_nonblock);
1902 if (ret < 0)
1903 return ret;
1904 } else {
1905 ret = io_import_iovec(WRITE, req, &iovec, &iter);
1906 if (ret < 0)
1907 return ret;
1908 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001909
Jens Axboe2b188cc2019-01-07 10:46:33 -07001910 file = kiocb->ki_filp;
Jens Axboef67676d2019-12-02 11:03:47 -07001911 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06001912 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07001913 req->result = io_size;
1914
1915 /*
1916 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
1917 * we know to async punt it even if it was opened O_NONBLOCK
1918 */
1919 if (force_nonblock && !io_file_supports_async(req->file)) {
1920 req->flags |= REQ_F_MUST_PUNT;
1921 goto copy_iov;
1922 }
1923
Jens Axboe10d59342019-12-09 20:16:22 -07001924 /* file path doesn't support NOWAIT for non-direct_IO */
1925 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
1926 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07001927 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06001928
Jens Axboe31b51512019-01-18 22:56:34 -07001929 iov_count = iov_iter_count(&iter);
Jens Axboe31b51512019-01-18 22:56:34 -07001930 ret = rw_verify_area(WRITE, file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001931 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01001932 ssize_t ret2;
1933
Jens Axboe2b188cc2019-01-07 10:46:33 -07001934 /*
1935 * Open-code file_start_write here to grab freeze protection,
1936 * which will be released by another thread in
1937 * io_complete_rw(). Fool lockdep by telling it the lock got
1938 * released so that it doesn't complain about the held lock when
1939 * we return to userspace.
1940 */
Jens Axboe491381ce2019-10-17 09:20:46 -06001941 if (req->flags & REQ_F_ISREG) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001942 __sb_start_write(file_inode(file)->i_sb,
1943 SB_FREEZE_WRITE, true);
1944 __sb_writers_release(file_inode(file)->i_sb,
1945 SB_FREEZE_WRITE);
1946 }
1947 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01001948
Jens Axboe32960612019-09-23 11:05:34 -06001949 if (file->f_op->write_iter)
1950 ret2 = call_write_iter(file, kiocb, &iter);
1951 else
1952 ret2 = loop_rw_iter(WRITE, file, kiocb, &iter);
Jens Axboef67676d2019-12-02 11:03:47 -07001953 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001954 kiocb_done(kiocb, ret2, nxt, req->in_async);
Jens Axboef67676d2019-12-02 11:03:47 -07001955 } else {
1956copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07001957 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07001958 inline_vecs, &iter);
1959 if (ret)
1960 goto out_free;
1961 return -EAGAIN;
1962 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001963 }
Jens Axboe31b51512019-01-18 22:56:34 -07001964out_free:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07001965 if (!io_wq_current_is_worker())
1966 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001967 return ret;
1968}
1969
1970/*
1971 * IORING_OP_NOP just posts a completion event, nothing else.
1972 */
Jens Axboe78e19bb2019-11-06 15:21:34 -07001973static int io_nop(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001974{
1975 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001976
Jens Axboedef596e2019-01-09 08:59:42 -07001977 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1978 return -EINVAL;
1979
Jens Axboe78e19bb2019-11-06 15:21:34 -07001980 io_cqring_add_event(req, 0);
Jens Axboee65ef562019-03-12 10:16:44 -06001981 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001982 return 0;
1983}
1984
Jens Axboefc4df992019-12-10 14:38:45 -07001985static int io_prep_fsync(struct io_kiocb *req)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001986{
Jens Axboefc4df992019-12-10 14:38:45 -07001987 const struct io_uring_sqe *sqe = req->sqe;
Jens Axboe6b063142019-01-10 22:13:58 -07001988 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001989
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07001990 if (req->flags & REQ_F_PREPPED)
1991 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06001992 if (!req->file)
1993 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001994
Jens Axboe6b063142019-01-10 22:13:58 -07001995 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07001996 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07001997 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07001998 return -EINVAL;
1999
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002000 req->sync.flags = READ_ONCE(sqe->fsync_flags);
2001 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
2002 return -EINVAL;
2003
2004 req->sync.off = READ_ONCE(sqe->off);
2005 req->sync.len = READ_ONCE(sqe->len);
2006 req->flags |= REQ_F_PREPPED;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002007 return 0;
2008}
2009
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002010static bool io_req_cancelled(struct io_kiocb *req)
2011{
2012 if (req->work.flags & IO_WQ_WORK_CANCEL) {
2013 req_set_fail_links(req);
2014 io_cqring_add_event(req, -ECANCELED);
2015 io_put_req(req);
2016 return true;
2017 }
2018
2019 return false;
2020}
2021
2022static void io_fsync_finish(struct io_wq_work **workptr)
2023{
2024 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2025 loff_t end = req->sync.off + req->sync.len;
2026 struct io_kiocb *nxt = NULL;
2027 int ret;
2028
2029 if (io_req_cancelled(req))
2030 return;
2031
2032 ret = vfs_fsync_range(req->rw.ki_filp, req->sync.off,
2033 end > 0 ? end : LLONG_MAX,
2034 req->sync.flags & IORING_FSYNC_DATASYNC);
2035 if (ret < 0)
2036 req_set_fail_links(req);
2037 io_cqring_add_event(req, ret);
2038 io_put_req_find_next(req, &nxt);
2039 if (nxt)
2040 *workptr = &nxt->work;
2041}
2042
Jens Axboefc4df992019-12-10 14:38:45 -07002043static int io_fsync(struct io_kiocb *req, struct io_kiocb **nxt,
2044 bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002045{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002046 struct io_wq_work *work, *old_work;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002047 int ret;
2048
Jens Axboefc4df992019-12-10 14:38:45 -07002049 ret = io_prep_fsync(req);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002050 if (ret)
2051 return ret;
2052
2053 /* fsync always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002054 if (force_nonblock) {
2055 io_put_req(req);
2056 req->work.func = io_fsync_finish;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002057 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002058 }
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002059
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002060 work = old_work = &req->work;
2061 io_fsync_finish(&work);
2062 if (work && work != old_work)
2063 *nxt = container_of(work, struct io_kiocb, work);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002064 return 0;
2065}
2066
Jens Axboefc4df992019-12-10 14:38:45 -07002067static int io_prep_sfr(struct io_kiocb *req)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002068{
Jens Axboefc4df992019-12-10 14:38:45 -07002069 const struct io_uring_sqe *sqe = req->sqe;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002070 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002071
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002072 if (req->flags & REQ_F_PREPPED)
2073 return 0;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002074 if (!req->file)
2075 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002076
2077 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2078 return -EINVAL;
2079 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
2080 return -EINVAL;
2081
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002082 req->sync.off = READ_ONCE(sqe->off);
2083 req->sync.len = READ_ONCE(sqe->len);
2084 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
2085 req->flags |= REQ_F_PREPPED;
2086 return 0;
2087}
2088
2089static void io_sync_file_range_finish(struct io_wq_work **workptr)
2090{
2091 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2092 struct io_kiocb *nxt = NULL;
2093 int ret;
2094
2095 if (io_req_cancelled(req))
2096 return;
2097
2098 ret = sync_file_range(req->rw.ki_filp, req->sync.off, req->sync.len,
2099 req->sync.flags);
2100 if (ret < 0)
2101 req_set_fail_links(req);
2102 io_cqring_add_event(req, ret);
2103 io_put_req_find_next(req, &nxt);
2104 if (nxt)
2105 *workptr = &nxt->work;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002106}
2107
Jens Axboefc4df992019-12-10 14:38:45 -07002108static int io_sync_file_range(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002109 bool force_nonblock)
2110{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002111 struct io_wq_work *work, *old_work;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002112 int ret;
2113
Jens Axboefc4df992019-12-10 14:38:45 -07002114 ret = io_prep_sfr(req);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002115 if (ret)
2116 return ret;
2117
2118 /* sync_file_range always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002119 if (force_nonblock) {
2120 io_put_req(req);
2121 req->work.func = io_sync_file_range_finish;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002122 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002123 }
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002124
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002125 work = old_work = &req->work;
2126 io_sync_file_range_finish(&work);
2127 if (work && work != old_work)
2128 *nxt = container_of(work, struct io_kiocb, work);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002129 return 0;
2130}
2131
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002132#if defined(CONFIG_NET)
2133static void io_sendrecv_async(struct io_wq_work **workptr)
2134{
2135 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2136 struct iovec *iov = NULL;
2137
2138 if (req->io->rw.iov != req->io->rw.fast_iov)
2139 iov = req->io->msg.iov;
2140 io_wq_submit_work(workptr);
2141 kfree(iov);
2142}
2143#endif
2144
Jens Axboe03b12302019-12-02 18:50:25 -07002145static int io_sendmsg_prep(struct io_kiocb *req, struct io_async_ctx *io)
Jens Axboeaa1fa282019-04-19 13:38:09 -06002146{
Jens Axboe03b12302019-12-02 18:50:25 -07002147#if defined(CONFIG_NET)
2148 const struct io_uring_sqe *sqe = req->sqe;
2149 struct user_msghdr __user *msg;
2150 unsigned flags;
2151
2152 flags = READ_ONCE(sqe->msg_flags);
2153 msg = (struct user_msghdr __user *)(unsigned long) READ_ONCE(sqe->addr);
Jens Axboed9688562019-12-09 19:35:20 -07002154 io->msg.iov = io->msg.fast_iov;
Jens Axboe03b12302019-12-02 18:50:25 -07002155 return sendmsg_copy_msghdr(&io->msg.msg, msg, flags, &io->msg.iov);
2156#else
2157 return 0;
2158#endif
2159}
2160
Jens Axboefc4df992019-12-10 14:38:45 -07002161static int io_sendmsg(struct io_kiocb *req, struct io_kiocb **nxt,
2162 bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07002163{
2164#if defined(CONFIG_NET)
Jens Axboefc4df992019-12-10 14:38:45 -07002165 const struct io_uring_sqe *sqe = req->sqe;
Jens Axboe0b416c32019-12-15 10:57:46 -07002166 struct io_async_msghdr *kmsg = NULL;
Jens Axboe03b12302019-12-02 18:50:25 -07002167 struct socket *sock;
2168 int ret;
2169
2170 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2171 return -EINVAL;
2172
2173 sock = sock_from_file(req->file, &ret);
2174 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002175 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07002176 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -07002177 unsigned flags;
2178
2179 flags = READ_ONCE(sqe->msg_flags);
2180 if (flags & MSG_DONTWAIT)
2181 req->flags |= REQ_F_NOWAIT;
2182 else if (force_nonblock)
2183 flags |= MSG_DONTWAIT;
2184
2185 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07002186 kmsg = &req->io->msg;
2187 kmsg->msg.msg_name = &addr;
2188 /* if iov is set, it's allocated already */
2189 if (!kmsg->iov)
2190 kmsg->iov = kmsg->fast_iov;
2191 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07002192 } else {
Jens Axboe0b416c32019-12-15 10:57:46 -07002193 kmsg = &io.msg;
2194 kmsg->msg.msg_name = &addr;
Jens Axboe03b12302019-12-02 18:50:25 -07002195 ret = io_sendmsg_prep(req, &io);
2196 if (ret)
2197 goto out;
2198 }
2199
Jens Axboe0b416c32019-12-15 10:57:46 -07002200 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Jens Axboe03b12302019-12-02 18:50:25 -07002201 if (force_nonblock && ret == -EAGAIN) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002202 if (req->io)
2203 return -EAGAIN;
2204 if (io_alloc_async_ctx(req))
2205 return -ENOMEM;
2206 memcpy(&req->io->msg, &io.msg, sizeof(io.msg));
2207 req->work.func = io_sendrecv_async;
Jens Axboe0b416c32019-12-15 10:57:46 -07002208 return -EAGAIN;
Jens Axboe03b12302019-12-02 18:50:25 -07002209 }
2210 if (ret == -ERESTARTSYS)
2211 ret = -EINTR;
2212 }
2213
2214out:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002215 if (!io_wq_current_is_worker() && kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07002216 kfree(kmsg->iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002217 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002218 if (ret < 0)
2219 req_set_fail_links(req);
Jens Axboe03b12302019-12-02 18:50:25 -07002220 io_put_req_find_next(req, nxt);
2221 return 0;
2222#else
2223 return -EOPNOTSUPP;
2224#endif
2225}
2226
2227static int io_recvmsg_prep(struct io_kiocb *req, struct io_async_ctx *io)
2228{
2229#if defined(CONFIG_NET)
2230 const struct io_uring_sqe *sqe = req->sqe;
2231 struct user_msghdr __user *msg;
2232 unsigned flags;
2233
2234 flags = READ_ONCE(sqe->msg_flags);
2235 msg = (struct user_msghdr __user *)(unsigned long) READ_ONCE(sqe->addr);
Jens Axboed9688562019-12-09 19:35:20 -07002236 io->msg.iov = io->msg.fast_iov;
Jens Axboe03b12302019-12-02 18:50:25 -07002237 return recvmsg_copy_msghdr(&io->msg.msg, msg, flags, &io->msg.uaddr,
2238 &io->msg.iov);
2239#else
2240 return 0;
2241#endif
2242}
2243
Jens Axboefc4df992019-12-10 14:38:45 -07002244static int io_recvmsg(struct io_kiocb *req, struct io_kiocb **nxt,
2245 bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07002246{
2247#if defined(CONFIG_NET)
Jens Axboefc4df992019-12-10 14:38:45 -07002248 const struct io_uring_sqe *sqe = req->sqe;
Jens Axboe0b416c32019-12-15 10:57:46 -07002249 struct io_async_msghdr *kmsg = NULL;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002250 struct socket *sock;
2251 int ret;
2252
2253 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2254 return -EINVAL;
2255
2256 sock = sock_from_file(req->file, &ret);
2257 if (sock) {
2258 struct user_msghdr __user *msg;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002259 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07002260 struct sockaddr_storage addr;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002261 unsigned flags;
2262
2263 flags = READ_ONCE(sqe->msg_flags);
2264 if (flags & MSG_DONTWAIT)
2265 req->flags |= REQ_F_NOWAIT;
2266 else if (force_nonblock)
2267 flags |= MSG_DONTWAIT;
2268
2269 msg = (struct user_msghdr __user *) (unsigned long)
2270 READ_ONCE(sqe->addr);
Jens Axboe03b12302019-12-02 18:50:25 -07002271 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07002272 kmsg = &req->io->msg;
2273 kmsg->msg.msg_name = &addr;
2274 /* if iov is set, it's allocated already */
2275 if (!kmsg->iov)
2276 kmsg->iov = kmsg->fast_iov;
2277 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07002278 } else {
Jens Axboe0b416c32019-12-15 10:57:46 -07002279 kmsg = &io.msg;
2280 kmsg->msg.msg_name = &addr;
Jens Axboe03b12302019-12-02 18:50:25 -07002281 ret = io_recvmsg_prep(req, &io);
2282 if (ret)
2283 goto out;
2284 }
Jens Axboe0fa03c62019-04-19 13:34:07 -06002285
Jens Axboe0b416c32019-12-15 10:57:46 -07002286 ret = __sys_recvmsg_sock(sock, &kmsg->msg, msg, kmsg->uaddr, flags);
Jens Axboe03b12302019-12-02 18:50:25 -07002287 if (force_nonblock && ret == -EAGAIN) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002288 if (req->io)
2289 return -EAGAIN;
2290 if (io_alloc_async_ctx(req))
2291 return -ENOMEM;
2292 memcpy(&req->io->msg, &io.msg, sizeof(io.msg));
2293 req->work.func = io_sendrecv_async;
Jens Axboe0b416c32019-12-15 10:57:46 -07002294 return -EAGAIN;
Jens Axboe03b12302019-12-02 18:50:25 -07002295 }
Jens Axboe441cdbd2019-12-02 18:49:10 -07002296 if (ret == -ERESTARTSYS)
2297 ret = -EINTR;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002298 }
2299
Jens Axboe03b12302019-12-02 18:50:25 -07002300out:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002301 if (!io_wq_current_is_worker() && kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07002302 kfree(kmsg->iov);
Jens Axboe78e19bb2019-11-06 15:21:34 -07002303 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002304 if (ret < 0)
2305 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08002306 io_put_req_find_next(req, nxt);
Jens Axboe0fa03c62019-04-19 13:34:07 -06002307 return 0;
2308#else
2309 return -EOPNOTSUPP;
2310#endif
2311}
2312
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002313static int io_accept_prep(struct io_kiocb *req)
Jens Axboe17f2fe32019-10-17 14:42:58 -06002314{
2315#if defined(CONFIG_NET)
Jens Axboefc4df992019-12-10 14:38:45 -07002316 const struct io_uring_sqe *sqe = req->sqe;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002317 struct io_accept *accept = &req->accept;
2318
2319 if (req->flags & REQ_F_PREPPED)
2320 return 0;
Jens Axboe17f2fe32019-10-17 14:42:58 -06002321
2322 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
2323 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05002324 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06002325 return -EINVAL;
2326
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002327 accept->addr = (struct sockaddr __user *)
2328 (unsigned long) READ_ONCE(sqe->addr);
2329 accept->addr_len = (int __user *) (unsigned long) READ_ONCE(sqe->addr2);
2330 accept->flags = READ_ONCE(sqe->accept_flags);
2331 req->flags |= REQ_F_PREPPED;
2332 return 0;
2333#else
2334 return -EOPNOTSUPP;
2335#endif
2336}
Jens Axboe17f2fe32019-10-17 14:42:58 -06002337
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002338#if defined(CONFIG_NET)
2339static int __io_accept(struct io_kiocb *req, struct io_kiocb **nxt,
2340 bool force_nonblock)
2341{
2342 struct io_accept *accept = &req->accept;
2343 unsigned file_flags;
2344 int ret;
2345
2346 file_flags = force_nonblock ? O_NONBLOCK : 0;
2347 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
2348 accept->addr_len, accept->flags);
2349 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06002350 return -EAGAIN;
Jens Axboe8e3cca12019-11-09 19:52:33 -07002351 if (ret == -ERESTARTSYS)
2352 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002353 if (ret < 0)
2354 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07002355 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08002356 io_put_req_find_next(req, nxt);
Jens Axboe17f2fe32019-10-17 14:42:58 -06002357 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002358}
2359
2360static void io_accept_finish(struct io_wq_work **workptr)
2361{
2362 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2363 struct io_kiocb *nxt = NULL;
2364
2365 if (io_req_cancelled(req))
2366 return;
2367 __io_accept(req, &nxt, false);
2368 if (nxt)
2369 *workptr = &nxt->work;
2370}
2371#endif
2372
2373static int io_accept(struct io_kiocb *req, struct io_kiocb **nxt,
2374 bool force_nonblock)
2375{
2376#if defined(CONFIG_NET)
2377 int ret;
2378
2379 ret = io_accept_prep(req);
2380 if (ret)
2381 return ret;
2382
2383 ret = __io_accept(req, nxt, force_nonblock);
2384 if (ret == -EAGAIN && force_nonblock) {
2385 req->work.func = io_accept_finish;
2386 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
2387 io_put_req(req);
2388 return -EAGAIN;
2389 }
2390 return 0;
Jens Axboe17f2fe32019-10-17 14:42:58 -06002391#else
2392 return -EOPNOTSUPP;
2393#endif
2394}
2395
Jens Axboef499a022019-12-02 16:28:46 -07002396static int io_connect_prep(struct io_kiocb *req, struct io_async_ctx *io)
2397{
2398#if defined(CONFIG_NET)
2399 const struct io_uring_sqe *sqe = req->sqe;
2400 struct sockaddr __user *addr;
2401 int addr_len;
2402
2403 addr = (struct sockaddr __user *) (unsigned long) READ_ONCE(sqe->addr);
2404 addr_len = READ_ONCE(sqe->addr2);
2405 return move_addr_to_kernel(addr, addr_len, &io->connect.address);
2406#else
2407 return 0;
2408#endif
2409}
2410
Jens Axboefc4df992019-12-10 14:38:45 -07002411static int io_connect(struct io_kiocb *req, struct io_kiocb **nxt,
2412 bool force_nonblock)
Jens Axboef8e85cf2019-11-23 14:24:24 -07002413{
2414#if defined(CONFIG_NET)
Jens Axboefc4df992019-12-10 14:38:45 -07002415 const struct io_uring_sqe *sqe = req->sqe;
Jens Axboef499a022019-12-02 16:28:46 -07002416 struct io_async_ctx __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07002417 unsigned file_flags;
2418 int addr_len, ret;
2419
2420 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
2421 return -EINVAL;
2422 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
2423 return -EINVAL;
2424
Jens Axboef8e85cf2019-11-23 14:24:24 -07002425 addr_len = READ_ONCE(sqe->addr2);
2426 file_flags = force_nonblock ? O_NONBLOCK : 0;
2427
Jens Axboef499a022019-12-02 16:28:46 -07002428 if (req->io) {
2429 io = req->io;
2430 } else {
2431 ret = io_connect_prep(req, &__io);
2432 if (ret)
2433 goto out;
2434 io = &__io;
2435 }
2436
2437 ret = __sys_connect_file(req->file, &io->connect.address, addr_len,
2438 file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07002439 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002440 if (req->io)
2441 return -EAGAIN;
2442 if (io_alloc_async_ctx(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07002443 ret = -ENOMEM;
2444 goto out;
2445 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002446 memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect));
Jens Axboef8e85cf2019-11-23 14:24:24 -07002447 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07002448 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07002449 if (ret == -ERESTARTSYS)
2450 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07002451out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002452 if (ret < 0)
2453 req_set_fail_links(req);
Jens Axboef8e85cf2019-11-23 14:24:24 -07002454 io_cqring_add_event(req, ret);
2455 io_put_req_find_next(req, nxt);
2456 return 0;
2457#else
2458 return -EOPNOTSUPP;
2459#endif
2460}
2461
Jens Axboe221c5eb2019-01-17 09:41:58 -07002462static void io_poll_remove_one(struct io_kiocb *req)
2463{
2464 struct io_poll_iocb *poll = &req->poll;
2465
2466 spin_lock(&poll->head->lock);
2467 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07002468 if (!list_empty(&poll->wait.entry)) {
2469 list_del_init(&poll->wait.entry);
Jackie Liua197f662019-11-08 08:09:12 -07002470 io_queue_async_work(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002471 }
2472 spin_unlock(&poll->head->lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07002473 hash_del(&req->hash_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002474}
2475
2476static void io_poll_remove_all(struct io_ring_ctx *ctx)
2477{
Jens Axboe78076bb2019-12-04 19:56:40 -07002478 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002479 struct io_kiocb *req;
Jens Axboe78076bb2019-12-04 19:56:40 -07002480 int i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002481
2482 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07002483 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
2484 struct hlist_head *list;
2485
2486 list = &ctx->cancel_hash[i];
2487 hlist_for_each_entry_safe(req, tmp, list, hash_node)
2488 io_poll_remove_one(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002489 }
2490 spin_unlock_irq(&ctx->completion_lock);
2491}
2492
Jens Axboe47f46762019-11-09 17:43:02 -07002493static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
2494{
Jens Axboe78076bb2019-12-04 19:56:40 -07002495 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07002496 struct io_kiocb *req;
2497
Jens Axboe78076bb2019-12-04 19:56:40 -07002498 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
2499 hlist_for_each_entry(req, list, hash_node) {
2500 if (sqe_addr == req->user_data) {
Jens Axboeeac406c2019-11-14 12:09:58 -07002501 io_poll_remove_one(req);
2502 return 0;
2503 }
Jens Axboe47f46762019-11-09 17:43:02 -07002504 }
2505
2506 return -ENOENT;
2507}
2508
Jens Axboe0969e782019-12-17 18:40:57 -07002509static int io_poll_remove_prep(struct io_kiocb *req)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002510{
Jens Axboefc4df992019-12-10 14:38:45 -07002511 const struct io_uring_sqe *sqe = req->sqe;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002512
Jens Axboe0969e782019-12-17 18:40:57 -07002513 if (req->flags & REQ_F_PREPPED)
2514 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002515 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2516 return -EINVAL;
2517 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
2518 sqe->poll_events)
2519 return -EINVAL;
2520
Jens Axboe0969e782019-12-17 18:40:57 -07002521 req->poll.addr = READ_ONCE(sqe->addr);
2522 req->flags |= REQ_F_PREPPED;
2523 return 0;
2524}
2525
2526/*
2527 * Find a running poll command that matches one specified in sqe->addr,
2528 * and remove it if found.
2529 */
2530static int io_poll_remove(struct io_kiocb *req)
2531{
2532 struct io_ring_ctx *ctx = req->ctx;
2533 u64 addr;
2534 int ret;
2535
2536 ret = io_poll_remove_prep(req);
2537 if (ret)
2538 return ret;
2539
2540 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002541 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07002542 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002543 spin_unlock_irq(&ctx->completion_lock);
2544
Jens Axboe78e19bb2019-11-06 15:21:34 -07002545 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002546 if (ret < 0)
2547 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06002548 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002549 return 0;
2550}
2551
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002552static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002553{
Jackie Liua197f662019-11-08 08:09:12 -07002554 struct io_ring_ctx *ctx = req->ctx;
2555
Jens Axboe8c838782019-03-12 15:48:16 -06002556 req->poll.done = true;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002557 if (error)
2558 io_cqring_fill_event(req, error);
2559 else
2560 io_cqring_fill_event(req, mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06002561 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002562}
2563
Jens Axboe561fb042019-10-24 07:25:42 -06002564static void io_poll_complete_work(struct io_wq_work **workptr)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002565{
Jens Axboe561fb042019-10-24 07:25:42 -06002566 struct io_wq_work *work = *workptr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002567 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
2568 struct io_poll_iocb *poll = &req->poll;
2569 struct poll_table_struct pt = { ._key = poll->events };
2570 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe89723d02019-11-05 15:32:58 -07002571 struct io_kiocb *nxt = NULL;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002572 __poll_t mask = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002573 int ret = 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002574
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002575 if (work->flags & IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06002576 WRITE_ONCE(poll->canceled, true);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002577 ret = -ECANCELED;
2578 } else if (READ_ONCE(poll->canceled)) {
2579 ret = -ECANCELED;
2580 }
Jens Axboe561fb042019-10-24 07:25:42 -06002581
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002582 if (ret != -ECANCELED)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002583 mask = vfs_poll(poll->file, &pt) & poll->events;
2584
2585 /*
2586 * Note that ->ki_cancel callers also delete iocb from active_reqs after
2587 * calling ->ki_cancel. We need the ctx_lock roundtrip here to
2588 * synchronize with them. In the cancellation case the list_del_init
2589 * itself is not actually needed, but harmless so we keep it in to
2590 * avoid further branches in the fast path.
2591 */
2592 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002593 if (!mask && ret != -ECANCELED) {
Jens Axboe392edb42019-12-09 17:52:20 -07002594 add_wait_queue(poll->head, &poll->wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002595 spin_unlock_irq(&ctx->completion_lock);
2596 return;
2597 }
Jens Axboe78076bb2019-12-04 19:56:40 -07002598 hash_del(&req->hash_node);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002599 io_poll_complete(req, mask, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002600 spin_unlock_irq(&ctx->completion_lock);
2601
Jens Axboe8c838782019-03-12 15:48:16 -06002602 io_cqring_ev_posted(ctx);
Jens Axboe89723d02019-11-05 15:32:58 -07002603
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002604 if (ret < 0)
2605 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08002606 io_put_req_find_next(req, &nxt);
Jens Axboe89723d02019-11-05 15:32:58 -07002607 if (nxt)
2608 *workptr = &nxt->work;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002609}
2610
2611static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
2612 void *key)
2613{
Jens Axboee9444752019-11-26 15:02:04 -07002614 struct io_poll_iocb *poll = wait->private;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002615 struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
2616 struct io_ring_ctx *ctx = req->ctx;
2617 __poll_t mask = key_to_poll(key);
Jens Axboe8c838782019-03-12 15:48:16 -06002618 unsigned long flags;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002619
2620 /* for instances that support it check for an event match first: */
Jens Axboe8c838782019-03-12 15:48:16 -06002621 if (mask && !(mask & poll->events))
2622 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002623
Jens Axboe392edb42019-12-09 17:52:20 -07002624 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06002625
Jens Axboe7c9e7f02019-11-12 08:15:53 -07002626 /*
2627 * Run completion inline if we can. We're using trylock here because
2628 * we are violating the completion_lock -> poll wq lock ordering.
2629 * If we have a link timeout we're going to need the completion_lock
2630 * for finalizing the request, mark us as having grabbed that already.
2631 */
Jens Axboe8c838782019-03-12 15:48:16 -06002632 if (mask && spin_trylock_irqsave(&ctx->completion_lock, flags)) {
Jens Axboe78076bb2019-12-04 19:56:40 -07002633 hash_del(&req->hash_node);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002634 io_poll_complete(req, mask, 0);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07002635 req->flags |= REQ_F_COMP_LOCKED;
2636 io_put_req(req);
Jens Axboe8c838782019-03-12 15:48:16 -06002637 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2638
2639 io_cqring_ev_posted(ctx);
Jens Axboe8c838782019-03-12 15:48:16 -06002640 } else {
Jackie Liua197f662019-11-08 08:09:12 -07002641 io_queue_async_work(req);
Jens Axboe8c838782019-03-12 15:48:16 -06002642 }
2643
Jens Axboe221c5eb2019-01-17 09:41:58 -07002644 return 1;
2645}
2646
2647struct io_poll_table {
2648 struct poll_table_struct pt;
2649 struct io_kiocb *req;
2650 int error;
2651};
2652
2653static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
2654 struct poll_table_struct *p)
2655{
2656 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
2657
2658 if (unlikely(pt->req->poll.head)) {
2659 pt->error = -EINVAL;
2660 return;
2661 }
2662
2663 pt->error = 0;
2664 pt->req->poll.head = head;
Jens Axboe392edb42019-12-09 17:52:20 -07002665 add_wait_queue(head, &pt->req->poll.wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002666}
2667
Jens Axboeeac406c2019-11-14 12:09:58 -07002668static void io_poll_req_insert(struct io_kiocb *req)
2669{
2670 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07002671 struct hlist_head *list;
Jens Axboeeac406c2019-11-14 12:09:58 -07002672
Jens Axboe78076bb2019-12-04 19:56:40 -07002673 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
2674 hlist_add_head(&req->hash_node, list);
Jens Axboeeac406c2019-11-14 12:09:58 -07002675}
2676
Jens Axboe0969e782019-12-17 18:40:57 -07002677static int io_poll_add_prep(struct io_kiocb *req)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002678{
Jens Axboefc4df992019-12-10 14:38:45 -07002679 const struct io_uring_sqe *sqe = req->sqe;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002680 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002681 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002682
Jens Axboe0969e782019-12-17 18:40:57 -07002683 if (req->flags & REQ_F_PREPPED)
2684 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002685 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2686 return -EINVAL;
2687 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
2688 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06002689 if (!poll->file)
2690 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002691
Jens Axboe0969e782019-12-17 18:40:57 -07002692 req->flags |= REQ_F_PREPPED;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002693 events = READ_ONCE(sqe->poll_events);
2694 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
Jens Axboe0969e782019-12-17 18:40:57 -07002695 return 0;
2696}
2697
2698static int io_poll_add(struct io_kiocb *req, struct io_kiocb **nxt)
2699{
2700 struct io_poll_iocb *poll = &req->poll;
2701 struct io_ring_ctx *ctx = req->ctx;
2702 struct io_poll_table ipt;
2703 bool cancel = false;
2704 __poll_t mask;
2705 int ret;
2706
2707 ret = io_poll_add_prep(req);
2708 if (ret)
2709 return ret;
2710
2711 INIT_IO_WORK(&req->work, io_poll_complete_work);
Jens Axboe78076bb2019-12-04 19:56:40 -07002712 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002713
Jens Axboe221c5eb2019-01-17 09:41:58 -07002714 poll->head = NULL;
Jens Axboe8c838782019-03-12 15:48:16 -06002715 poll->done = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002716 poll->canceled = false;
2717
2718 ipt.pt._qproc = io_poll_queue_proc;
2719 ipt.pt._key = poll->events;
2720 ipt.req = req;
2721 ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
2722
2723 /* initialized the list so that we can do list_empty checks */
Jens Axboe392edb42019-12-09 17:52:20 -07002724 INIT_LIST_HEAD(&poll->wait.entry);
2725 init_waitqueue_func_entry(&poll->wait, io_poll_wake);
2726 poll->wait.private = poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002727
Jens Axboe36703242019-07-25 10:20:18 -06002728 INIT_LIST_HEAD(&req->list);
2729
Jens Axboe221c5eb2019-01-17 09:41:58 -07002730 mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002731
2732 spin_lock_irq(&ctx->completion_lock);
Jens Axboe8c838782019-03-12 15:48:16 -06002733 if (likely(poll->head)) {
2734 spin_lock(&poll->head->lock);
Jens Axboe392edb42019-12-09 17:52:20 -07002735 if (unlikely(list_empty(&poll->wait.entry))) {
Jens Axboe8c838782019-03-12 15:48:16 -06002736 if (ipt.error)
2737 cancel = true;
2738 ipt.error = 0;
2739 mask = 0;
2740 }
2741 if (mask || ipt.error)
Jens Axboe392edb42019-12-09 17:52:20 -07002742 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06002743 else if (cancel)
2744 WRITE_ONCE(poll->canceled, true);
2745 else if (!poll->done) /* actually waiting for an event */
Jens Axboeeac406c2019-11-14 12:09:58 -07002746 io_poll_req_insert(req);
Jens Axboe8c838782019-03-12 15:48:16 -06002747 spin_unlock(&poll->head->lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002748 }
Jens Axboe8c838782019-03-12 15:48:16 -06002749 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06002750 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002751 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06002752 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07002753 spin_unlock_irq(&ctx->completion_lock);
2754
Jens Axboe8c838782019-03-12 15:48:16 -06002755 if (mask) {
2756 io_cqring_ev_posted(ctx);
Jackie Liuec9c02a2019-11-08 23:50:36 +08002757 io_put_req_find_next(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002758 }
Jens Axboe8c838782019-03-12 15:48:16 -06002759 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002760}
2761
Jens Axboe5262f562019-09-17 12:26:57 -06002762static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
2763{
Jens Axboead8a48a2019-11-15 08:49:11 -07002764 struct io_timeout_data *data = container_of(timer,
2765 struct io_timeout_data, timer);
2766 struct io_kiocb *req = data->req;
2767 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06002768 unsigned long flags;
2769
Jens Axboe5262f562019-09-17 12:26:57 -06002770 atomic_inc(&ctx->cq_timeouts);
2771
2772 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08002773 /*
Jens Axboe11365042019-10-16 09:08:32 -06002774 * We could be racing with timeout deletion. If the list is empty,
2775 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08002776 */
Jens Axboe842f9612019-10-29 12:34:10 -06002777 if (!list_empty(&req->list)) {
Jens Axboe11365042019-10-16 09:08:32 -06002778 struct io_kiocb *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06002779
Jens Axboe11365042019-10-16 09:08:32 -06002780 /*
2781 * Adjust the reqs sequence before the current one because it
Brian Gianforcarod195a662019-12-13 03:09:50 -08002782 * will consume a slot in the cq_ring and the cq_tail
Jens Axboe11365042019-10-16 09:08:32 -06002783 * pointer will be increased, otherwise other timeout reqs may
2784 * return in advance without waiting for enough wait_nr.
2785 */
2786 prev = req;
2787 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
2788 prev->sequence++;
Jens Axboe11365042019-10-16 09:08:32 -06002789 list_del_init(&req->list);
Jens Axboe11365042019-10-16 09:08:32 -06002790 }
Jens Axboe842f9612019-10-29 12:34:10 -06002791
Jens Axboe78e19bb2019-11-06 15:21:34 -07002792 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06002793 io_commit_cqring(ctx);
2794 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2795
2796 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002797 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06002798 io_put_req(req);
2799 return HRTIMER_NORESTART;
2800}
2801
Jens Axboe47f46762019-11-09 17:43:02 -07002802static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
2803{
2804 struct io_kiocb *req;
2805 int ret = -ENOENT;
2806
2807 list_for_each_entry(req, &ctx->timeout_list, list) {
2808 if (user_data == req->user_data) {
2809 list_del_init(&req->list);
2810 ret = 0;
2811 break;
2812 }
2813 }
2814
2815 if (ret == -ENOENT)
2816 return ret;
2817
Jens Axboe2d283902019-12-04 11:08:05 -07002818 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe47f46762019-11-09 17:43:02 -07002819 if (ret == -1)
2820 return -EALREADY;
2821
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002822 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07002823 io_cqring_fill_event(req, -ECANCELED);
2824 io_put_req(req);
2825 return 0;
2826}
2827
Jens Axboeb29472e2019-12-17 18:50:29 -07002828static int io_timeout_remove_prep(struct io_kiocb *req)
2829{
2830 const struct io_uring_sqe *sqe = req->sqe;
2831
2832 if (req->flags & REQ_F_PREPPED)
2833 return 0;
2834 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2835 return -EINVAL;
2836 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
2837 return -EINVAL;
2838
2839 req->timeout.addr = READ_ONCE(sqe->addr);
2840 req->timeout.flags = READ_ONCE(sqe->timeout_flags);
2841 if (req->timeout.flags)
2842 return -EINVAL;
2843
2844 req->flags |= REQ_F_PREPPED;
2845 return 0;
2846}
2847
Jens Axboe11365042019-10-16 09:08:32 -06002848/*
2849 * Remove or update an existing timeout command
2850 */
Jens Axboefc4df992019-12-10 14:38:45 -07002851static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06002852{
2853 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07002854 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06002855
Jens Axboeb29472e2019-12-17 18:50:29 -07002856 ret = io_timeout_remove_prep(req);
2857 if (ret)
2858 return ret;
Jens Axboe11365042019-10-16 09:08:32 -06002859
Jens Axboe11365042019-10-16 09:08:32 -06002860 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb29472e2019-12-17 18:50:29 -07002861 ret = io_timeout_cancel(ctx, req->timeout.addr);
Jens Axboe11365042019-10-16 09:08:32 -06002862
Jens Axboe47f46762019-11-09 17:43:02 -07002863 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06002864 io_commit_cqring(ctx);
2865 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06002866 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002867 if (ret < 0)
2868 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08002869 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06002870 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06002871}
2872
Jens Axboe2d283902019-12-04 11:08:05 -07002873static int io_timeout_prep(struct io_kiocb *req, struct io_async_ctx *io,
2874 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06002875{
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002876 const struct io_uring_sqe *sqe = req->sqe;
Jens Axboead8a48a2019-11-15 08:49:11 -07002877 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06002878 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06002879
Jens Axboead8a48a2019-11-15 08:49:11 -07002880 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06002881 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07002882 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06002883 return -EINVAL;
Jens Axboe2d283902019-12-04 11:08:05 -07002884 if (sqe->off && is_timeout_link)
2885 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06002886 flags = READ_ONCE(sqe->timeout_flags);
2887 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06002888 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06002889
Jens Axboe2d283902019-12-04 11:08:05 -07002890 data = &io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07002891 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07002892 req->flags |= REQ_F_TIMEOUT;
2893
2894 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06002895 return -EFAULT;
2896
Jens Axboe11365042019-10-16 09:08:32 -06002897 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07002898 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06002899 else
Jens Axboead8a48a2019-11-15 08:49:11 -07002900 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06002901
Jens Axboead8a48a2019-11-15 08:49:11 -07002902 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
2903 return 0;
2904}
2905
Jens Axboefc4df992019-12-10 14:38:45 -07002906static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07002907{
Jens Axboefc4df992019-12-10 14:38:45 -07002908 const struct io_uring_sqe *sqe = req->sqe;
Jens Axboead8a48a2019-11-15 08:49:11 -07002909 unsigned count;
2910 struct io_ring_ctx *ctx = req->ctx;
2911 struct io_timeout_data *data;
2912 struct list_head *entry;
2913 unsigned span = 0;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002914 int ret;
Jens Axboead8a48a2019-11-15 08:49:11 -07002915
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002916 if (!req->io) {
2917 if (io_alloc_async_ctx(req))
Jens Axboe2d283902019-12-04 11:08:05 -07002918 return -ENOMEM;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002919 ret = io_timeout_prep(req, req->io, false);
2920 if (ret)
Jens Axboe2d283902019-12-04 11:08:05 -07002921 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07002922 }
2923 data = &req->io->timeout;
Jens Axboe93bd25b2019-11-11 23:34:31 -07002924
Jens Axboe5262f562019-09-17 12:26:57 -06002925 /*
2926 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07002927 * timeout event to be satisfied. If it isn't set, then this is
2928 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06002929 */
2930 count = READ_ONCE(sqe->off);
Jens Axboe93bd25b2019-11-11 23:34:31 -07002931 if (!count) {
2932 req->flags |= REQ_F_TIMEOUT_NOSEQ;
2933 spin_lock_irq(&ctx->completion_lock);
2934 entry = ctx->timeout_list.prev;
2935 goto add;
2936 }
Jens Axboe5262f562019-09-17 12:26:57 -06002937
2938 req->sequence = ctx->cached_sq_head + count - 1;
Jens Axboe2d283902019-12-04 11:08:05 -07002939 data->seq_offset = count;
Jens Axboe5262f562019-09-17 12:26:57 -06002940
2941 /*
2942 * Insertion sort, ensuring the first entry in the list is always
2943 * the one we need first.
2944 */
Jens Axboe5262f562019-09-17 12:26:57 -06002945 spin_lock_irq(&ctx->completion_lock);
2946 list_for_each_prev(entry, &ctx->timeout_list) {
2947 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
yangerkun5da0fb12019-10-15 21:59:29 +08002948 unsigned nxt_sq_head;
2949 long long tmp, tmp_nxt;
Jens Axboe2d283902019-12-04 11:08:05 -07002950 u32 nxt_offset = nxt->io->timeout.seq_offset;
Jens Axboe5262f562019-09-17 12:26:57 -06002951
Jens Axboe93bd25b2019-11-11 23:34:31 -07002952 if (nxt->flags & REQ_F_TIMEOUT_NOSEQ)
2953 continue;
2954
yangerkun5da0fb12019-10-15 21:59:29 +08002955 /*
2956 * Since cached_sq_head + count - 1 can overflow, use type long
2957 * long to store it.
2958 */
2959 tmp = (long long)ctx->cached_sq_head + count - 1;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +03002960 nxt_sq_head = nxt->sequence - nxt_offset + 1;
2961 tmp_nxt = (long long)nxt_sq_head + nxt_offset - 1;
yangerkun5da0fb12019-10-15 21:59:29 +08002962
2963 /*
2964 * cached_sq_head may overflow, and it will never overflow twice
2965 * once there is some timeout req still be valid.
2966 */
2967 if (ctx->cached_sq_head < nxt_sq_head)
yangerkun8b07a652019-10-17 12:12:35 +08002968 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08002969
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002970 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06002971 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002972
2973 /*
2974 * Sequence of reqs after the insert one and itself should
2975 * be adjusted because each timeout req consumes a slot.
2976 */
2977 span++;
2978 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06002979 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002980 req->sequence -= span;
Jens Axboe93bd25b2019-11-11 23:34:31 -07002981add:
Jens Axboe5262f562019-09-17 12:26:57 -06002982 list_add(&req->list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07002983 data->timer.function = io_timeout_fn;
2984 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06002985 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06002986 return 0;
2987}
2988
Jens Axboe62755e32019-10-28 21:49:21 -06002989static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06002990{
Jens Axboe62755e32019-10-28 21:49:21 -06002991 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06002992
Jens Axboe62755e32019-10-28 21:49:21 -06002993 return req->user_data == (unsigned long) data;
2994}
2995
Jens Axboee977d6d2019-11-05 12:39:45 -07002996static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06002997{
Jens Axboe62755e32019-10-28 21:49:21 -06002998 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06002999 int ret = 0;
3000
Jens Axboe62755e32019-10-28 21:49:21 -06003001 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
3002 switch (cancel_ret) {
3003 case IO_WQ_CANCEL_OK:
3004 ret = 0;
3005 break;
3006 case IO_WQ_CANCEL_RUNNING:
3007 ret = -EALREADY;
3008 break;
3009 case IO_WQ_CANCEL_NOTFOUND:
3010 ret = -ENOENT;
3011 break;
3012 }
3013
Jens Axboee977d6d2019-11-05 12:39:45 -07003014 return ret;
3015}
3016
Jens Axboe47f46762019-11-09 17:43:02 -07003017static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
3018 struct io_kiocb *req, __u64 sqe_addr,
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003019 struct io_kiocb **nxt, int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07003020{
3021 unsigned long flags;
3022 int ret;
3023
3024 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
3025 if (ret != -ENOENT) {
3026 spin_lock_irqsave(&ctx->completion_lock, flags);
3027 goto done;
3028 }
3029
3030 spin_lock_irqsave(&ctx->completion_lock, flags);
3031 ret = io_timeout_cancel(ctx, sqe_addr);
3032 if (ret != -ENOENT)
3033 goto done;
3034 ret = io_poll_cancel(ctx, sqe_addr);
3035done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003036 if (!ret)
3037 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07003038 io_cqring_fill_event(req, ret);
3039 io_commit_cqring(ctx);
3040 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3041 io_cqring_ev_posted(ctx);
3042
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003043 if (ret < 0)
3044 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07003045 io_put_req_find_next(req, nxt);
3046}
3047
Jens Axboefbf23842019-12-17 18:45:56 -07003048static int io_async_cancel_prep(struct io_kiocb *req)
Jens Axboee977d6d2019-11-05 12:39:45 -07003049{
Jens Axboefc4df992019-12-10 14:38:45 -07003050 const struct io_uring_sqe *sqe = req->sqe;
Jens Axboee977d6d2019-11-05 12:39:45 -07003051
Jens Axboefbf23842019-12-17 18:45:56 -07003052 if (req->flags & REQ_F_PREPPED)
3053 return 0;
3054 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07003055 return -EINVAL;
3056 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
3057 sqe->cancel_flags)
3058 return -EINVAL;
3059
Jens Axboefbf23842019-12-17 18:45:56 -07003060 req->flags |= REQ_F_PREPPED;
3061 req->cancel.addr = READ_ONCE(sqe->addr);
3062 return 0;
3063}
3064
3065static int io_async_cancel(struct io_kiocb *req, struct io_kiocb **nxt)
3066{
3067 struct io_ring_ctx *ctx = req->ctx;
3068 int ret;
3069
3070 ret = io_async_cancel_prep(req);
3071 if (ret)
3072 return ret;
3073
3074 io_async_find_and_cancel(ctx, req, req->cancel.addr, nxt, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06003075 return 0;
3076}
3077
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003078static int io_req_defer_prep(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07003079{
3080 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003081 struct io_async_ctx *io = req->io;
Jens Axboef67676d2019-12-02 11:03:47 -07003082 struct iov_iter iter;
3083 ssize_t ret;
3084
Jens Axboef67676d2019-12-02 11:03:47 -07003085 switch (io->sqe.opcode) {
3086 case IORING_OP_READV:
3087 case IORING_OP_READ_FIXED:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003088 /* ensure prep does right import */
3089 req->io = NULL;
Jens Axboef67676d2019-12-02 11:03:47 -07003090 ret = io_read_prep(req, &iovec, &iter, true);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003091 req->io = io;
3092 if (ret < 0)
3093 break;
3094 io_req_map_rw(req, ret, iovec, inline_vecs, &iter);
3095 ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003096 break;
3097 case IORING_OP_WRITEV:
3098 case IORING_OP_WRITE_FIXED:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003099 /* ensure prep does right import */
3100 req->io = NULL;
Jens Axboef67676d2019-12-02 11:03:47 -07003101 ret = io_write_prep(req, &iovec, &iter, true);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003102 req->io = io;
3103 if (ret < 0)
3104 break;
3105 io_req_map_rw(req, ret, iovec, inline_vecs, &iter);
3106 ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003107 break;
Jens Axboe0969e782019-12-17 18:40:57 -07003108 case IORING_OP_POLL_ADD:
3109 ret = io_poll_add_prep(req);
3110 break;
3111 case IORING_OP_POLL_REMOVE:
3112 ret = io_poll_remove_prep(req);
3113 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003114 case IORING_OP_FSYNC:
3115 ret = io_prep_fsync(req);
3116 break;
3117 case IORING_OP_SYNC_FILE_RANGE:
3118 ret = io_prep_sfr(req);
3119 break;
Jens Axboe03b12302019-12-02 18:50:25 -07003120 case IORING_OP_SENDMSG:
3121 ret = io_sendmsg_prep(req, io);
3122 break;
3123 case IORING_OP_RECVMSG:
3124 ret = io_recvmsg_prep(req, io);
3125 break;
Jens Axboef499a022019-12-02 16:28:46 -07003126 case IORING_OP_CONNECT:
3127 ret = io_connect_prep(req, io);
3128 break;
Jens Axboe2d283902019-12-04 11:08:05 -07003129 case IORING_OP_TIMEOUT:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003130 ret = io_timeout_prep(req, io, false);
3131 break;
Jens Axboeb29472e2019-12-17 18:50:29 -07003132 case IORING_OP_TIMEOUT_REMOVE:
3133 ret = io_timeout_remove_prep(req);
3134 break;
Jens Axboefbf23842019-12-17 18:45:56 -07003135 case IORING_OP_ASYNC_CANCEL:
3136 ret = io_async_cancel_prep(req);
3137 break;
Jens Axboe2d283902019-12-04 11:08:05 -07003138 case IORING_OP_LINK_TIMEOUT:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003139 ret = io_timeout_prep(req, io, true);
3140 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003141 case IORING_OP_ACCEPT:
3142 ret = io_accept_prep(req);
3143 break;
Jens Axboef67676d2019-12-02 11:03:47 -07003144 default:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003145 ret = 0;
3146 break;
Jens Axboef67676d2019-12-02 11:03:47 -07003147 }
3148
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003149 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003150}
3151
Jackie Liua197f662019-11-08 08:09:12 -07003152static int io_req_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -06003153{
Jackie Liua197f662019-11-08 08:09:12 -07003154 struct io_ring_ctx *ctx = req->ctx;
Jens Axboef67676d2019-12-02 11:03:47 -07003155 int ret;
Jens Axboede0617e2019-04-06 21:51:27 -06003156
Bob Liu9d858b22019-11-13 18:06:25 +08003157 /* Still need defer if there is pending req in defer list. */
3158 if (!req_need_defer(req) && list_empty(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06003159 return 0;
3160
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003161 if (io_alloc_async_ctx(req))
Jens Axboede0617e2019-04-06 21:51:27 -06003162 return -EAGAIN;
3163
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003164 ret = io_req_defer_prep(req);
3165 if (ret < 0)
Jens Axboe2d283902019-12-04 11:08:05 -07003166 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07003167
Jens Axboede0617e2019-04-06 21:51:27 -06003168 spin_lock_irq(&ctx->completion_lock);
Bob Liu9d858b22019-11-13 18:06:25 +08003169 if (!req_need_defer(req) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06003170 spin_unlock_irq(&ctx->completion_lock);
Jens Axboede0617e2019-04-06 21:51:27 -06003171 return 0;
3172 }
3173
Jens Axboe915967f2019-11-21 09:01:20 -07003174 trace_io_uring_defer(ctx, req, req->user_data);
Jens Axboede0617e2019-04-06 21:51:27 -06003175 list_add_tail(&req->list, &ctx->defer_list);
3176 spin_unlock_irq(&ctx->completion_lock);
3177 return -EIOCBQUEUED;
3178}
3179
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03003180__attribute__((nonnull))
Pavel Begunkovd7324472019-11-21 21:24:36 +03003181static int io_issue_sqe(struct io_kiocb *req, struct io_kiocb **nxt,
3182 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003183{
Jens Axboee0c5c572019-03-12 10:18:47 -06003184 int ret, opcode;
Jackie Liua197f662019-11-08 08:09:12 -07003185 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003186
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003187 opcode = READ_ONCE(req->sqe->opcode);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003188 switch (opcode) {
3189 case IORING_OP_NOP:
Jens Axboe78e19bb2019-11-06 15:21:34 -07003190 ret = io_nop(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003191 break;
3192 case IORING_OP_READV:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003193 if (unlikely(req->sqe->buf_index))
Jens Axboeedafcce2019-01-09 09:16:05 -07003194 return -EINVAL;
Pavel Begunkov267bc902019-11-07 01:41:08 +03003195 ret = io_read(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003196 break;
3197 case IORING_OP_WRITEV:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003198 if (unlikely(req->sqe->buf_index))
Jens Axboeedafcce2019-01-09 09:16:05 -07003199 return -EINVAL;
Pavel Begunkov267bc902019-11-07 01:41:08 +03003200 ret = io_write(req, nxt, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07003201 break;
3202 case IORING_OP_READ_FIXED:
Pavel Begunkov267bc902019-11-07 01:41:08 +03003203 ret = io_read(req, nxt, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07003204 break;
3205 case IORING_OP_WRITE_FIXED:
Pavel Begunkov267bc902019-11-07 01:41:08 +03003206 ret = io_write(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003207 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003208 case IORING_OP_FSYNC:
Jens Axboefc4df992019-12-10 14:38:45 -07003209 ret = io_fsync(req, nxt, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003210 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003211 case IORING_OP_POLL_ADD:
Jens Axboefc4df992019-12-10 14:38:45 -07003212 ret = io_poll_add(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003213 break;
3214 case IORING_OP_POLL_REMOVE:
Jens Axboefc4df992019-12-10 14:38:45 -07003215 ret = io_poll_remove(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003216 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003217 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboefc4df992019-12-10 14:38:45 -07003218 ret = io_sync_file_range(req, nxt, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003219 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003220 case IORING_OP_SENDMSG:
Jens Axboefc4df992019-12-10 14:38:45 -07003221 ret = io_sendmsg(req, nxt, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06003222 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06003223 case IORING_OP_RECVMSG:
Jens Axboefc4df992019-12-10 14:38:45 -07003224 ret = io_recvmsg(req, nxt, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06003225 break;
Jens Axboe5262f562019-09-17 12:26:57 -06003226 case IORING_OP_TIMEOUT:
Jens Axboefc4df992019-12-10 14:38:45 -07003227 ret = io_timeout(req);
Jens Axboe5262f562019-09-17 12:26:57 -06003228 break;
Jens Axboe11365042019-10-16 09:08:32 -06003229 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboefc4df992019-12-10 14:38:45 -07003230 ret = io_timeout_remove(req);
Jens Axboe11365042019-10-16 09:08:32 -06003231 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06003232 case IORING_OP_ACCEPT:
Jens Axboefc4df992019-12-10 14:38:45 -07003233 ret = io_accept(req, nxt, force_nonblock);
Jens Axboe17f2fe32019-10-17 14:42:58 -06003234 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003235 case IORING_OP_CONNECT:
Jens Axboefc4df992019-12-10 14:38:45 -07003236 ret = io_connect(req, nxt, force_nonblock);
Jens Axboef8e85cf2019-11-23 14:24:24 -07003237 break;
Jens Axboe62755e32019-10-28 21:49:21 -06003238 case IORING_OP_ASYNC_CANCEL:
Jens Axboefc4df992019-12-10 14:38:45 -07003239 ret = io_async_cancel(req, nxt);
Jens Axboe62755e32019-10-28 21:49:21 -06003240 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003241 default:
3242 ret = -EINVAL;
3243 break;
3244 }
3245
Jens Axboedef596e2019-01-09 08:59:42 -07003246 if (ret)
3247 return ret;
3248
3249 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe9e645e112019-05-10 16:07:28 -06003250 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07003251 return -EAGAIN;
3252
Jens Axboedef596e2019-01-09 08:59:42 -07003253 io_iopoll_req_issued(req);
Jens Axboedef596e2019-01-09 08:59:42 -07003254 }
3255
3256 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003257}
3258
Jens Axboeb76da702019-11-20 13:05:32 -07003259static void io_link_work_cb(struct io_wq_work **workptr)
3260{
3261 struct io_wq_work *work = *workptr;
3262 struct io_kiocb *link = work->data;
3263
3264 io_queue_linked_timeout(link);
3265 work->func = io_wq_submit_work;
3266}
3267
Jens Axboe561fb042019-10-24 07:25:42 -06003268static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboe31b51512019-01-18 22:56:34 -07003269{
Jens Axboe561fb042019-10-24 07:25:42 -06003270 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003271 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe561fb042019-10-24 07:25:42 -06003272 struct io_kiocb *nxt = NULL;
3273 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003274
Jens Axboe561fb042019-10-24 07:25:42 -06003275 /* Ensure we clear previously set non-block flag */
3276 req->rw.ki_flags &= ~IOCB_NOWAIT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003277
Jens Axboe561fb042019-10-24 07:25:42 -06003278 if (work->flags & IO_WQ_WORK_CANCEL)
3279 ret = -ECANCELED;
Jens Axboe31b51512019-01-18 22:56:34 -07003280
Jens Axboe561fb042019-10-24 07:25:42 -06003281 if (!ret) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003282 req->has_user = (work->flags & IO_WQ_WORK_HAS_MM) != 0;
3283 req->in_async = true;
Jens Axboe561fb042019-10-24 07:25:42 -06003284 do {
Pavel Begunkovd7324472019-11-21 21:24:36 +03003285 ret = io_issue_sqe(req, &nxt, false);
Jens Axboe561fb042019-10-24 07:25:42 -06003286 /*
3287 * We can get EAGAIN for polled IO even though we're
3288 * forcing a sync submission from here, since we can't
3289 * wait for request slots on the block side.
3290 */
3291 if (ret != -EAGAIN)
3292 break;
3293 cond_resched();
3294 } while (1);
3295 }
Jens Axboe31b51512019-01-18 22:56:34 -07003296
Jens Axboe561fb042019-10-24 07:25:42 -06003297 /* drop submission reference */
Jackie Liuec9c02a2019-11-08 23:50:36 +08003298 io_put_req(req);
Jens Axboe817869d2019-04-30 14:44:05 -06003299
Jens Axboe561fb042019-10-24 07:25:42 -06003300 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003301 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07003302 io_cqring_add_event(req, ret);
Jens Axboe817869d2019-04-30 14:44:05 -06003303 io_put_req(req);
Jens Axboeedafcce2019-01-09 09:16:05 -07003304 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003305
Jens Axboe561fb042019-10-24 07:25:42 -06003306 /* if a dependent link is ready, pass it back */
3307 if (!ret && nxt) {
Jens Axboe94ae5e72019-11-14 19:39:52 -07003308 struct io_kiocb *link;
3309
3310 io_prep_async_work(nxt, &link);
Jens Axboe561fb042019-10-24 07:25:42 -06003311 *workptr = &nxt->work;
Jens Axboeb76da702019-11-20 13:05:32 -07003312 if (link) {
3313 nxt->work.flags |= IO_WQ_WORK_CB;
3314 nxt->work.func = io_link_work_cb;
3315 nxt->work.data = link;
3316 }
Jens Axboeedafcce2019-01-09 09:16:05 -07003317 }
Jens Axboe31b51512019-01-18 22:56:34 -07003318}
Jens Axboe2b188cc2019-01-07 10:46:33 -07003319
Jens Axboe9e3aa612019-12-11 15:55:43 -07003320static bool io_req_op_valid(int op)
3321{
3322 return op >= IORING_OP_NOP && op < IORING_OP_LAST;
3323}
3324
3325static int io_op_needs_file(const struct io_uring_sqe *sqe)
Jens Axboe09bb8392019-03-13 12:39:28 -06003326{
3327 int op = READ_ONCE(sqe->opcode);
3328
3329 switch (op) {
3330 case IORING_OP_NOP:
3331 case IORING_OP_POLL_REMOVE:
Pavel Begunkov5683e542019-11-14 00:59:19 +03003332 case IORING_OP_TIMEOUT:
Pavel Begunkova320e9f2019-11-14 00:11:01 +03003333 case IORING_OP_TIMEOUT_REMOVE:
3334 case IORING_OP_ASYNC_CANCEL:
3335 case IORING_OP_LINK_TIMEOUT:
Jens Axboe9e3aa612019-12-11 15:55:43 -07003336 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06003337 default:
Jens Axboe9e3aa612019-12-11 15:55:43 -07003338 if (io_req_op_valid(op))
3339 return 1;
3340 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06003341 }
3342}
3343
Jens Axboe65e19f52019-10-26 07:20:21 -06003344static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
3345 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06003346{
Jens Axboe65e19f52019-10-26 07:20:21 -06003347 struct fixed_file_table *table;
3348
3349 table = &ctx->file_table[index >> IORING_FILE_TABLE_SHIFT];
3350 return table->files[index & IORING_FILE_TABLE_MASK];
3351}
3352
Jackie Liua197f662019-11-08 08:09:12 -07003353static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req)
Jens Axboe09bb8392019-03-13 12:39:28 -06003354{
Jackie Liua197f662019-11-08 08:09:12 -07003355 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe09bb8392019-03-13 12:39:28 -06003356 unsigned flags;
Jens Axboe9e3aa612019-12-11 15:55:43 -07003357 int fd, ret;
Jens Axboe09bb8392019-03-13 12:39:28 -06003358
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003359 flags = READ_ONCE(req->sqe->flags);
3360 fd = READ_ONCE(req->sqe->fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06003361
Jackie Liu4fe2c962019-09-09 20:50:40 +08003362 if (flags & IOSQE_IO_DRAIN)
Jens Axboede0617e2019-04-06 21:51:27 -06003363 req->flags |= REQ_F_IO_DRAIN;
Jens Axboede0617e2019-04-06 21:51:27 -06003364
Jens Axboe9e3aa612019-12-11 15:55:43 -07003365 ret = io_op_needs_file(req->sqe);
3366 if (ret <= 0)
3367 return ret;
Jens Axboe09bb8392019-03-13 12:39:28 -06003368
3369 if (flags & IOSQE_FIXED_FILE) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003370 if (unlikely(!ctx->file_table ||
Jens Axboe09bb8392019-03-13 12:39:28 -06003371 (unsigned) fd >= ctx->nr_user_files))
3372 return -EBADF;
Jens Axboeb7620122019-10-26 07:22:55 -06003373 fd = array_index_nospec(fd, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06003374 req->file = io_file_from_index(ctx, fd);
3375 if (!req->file)
Jens Axboe08a45172019-10-03 08:11:03 -06003376 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06003377 req->flags |= REQ_F_FIXED_FILE;
3378 } else {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003379 if (req->needs_fixed_file)
Jens Axboe09bb8392019-03-13 12:39:28 -06003380 return -EBADF;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02003381 trace_io_uring_file_get(ctx, fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06003382 req->file = io_file_get(state, fd);
3383 if (unlikely(!req->file))
3384 return -EBADF;
3385 }
3386
3387 return 0;
3388}
3389
Jackie Liua197f662019-11-08 08:09:12 -07003390static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003391{
Jens Axboefcb323c2019-10-24 12:39:47 -06003392 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07003393 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06003394
3395 rcu_read_lock();
3396 spin_lock_irq(&ctx->inflight_lock);
3397 /*
3398 * We use the f_ops->flush() handler to ensure that we can flush
3399 * out work accessing these files if the fd is closed. Check if
3400 * the fd has changed since we started down this path, and disallow
3401 * this operation if it has.
3402 */
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003403 if (fcheck(req->ring_fd) == req->ring_file) {
Jens Axboefcb323c2019-10-24 12:39:47 -06003404 list_add(&req->inflight_entry, &ctx->inflight_list);
3405 req->flags |= REQ_F_INFLIGHT;
3406 req->work.files = current->files;
3407 ret = 0;
3408 }
3409 spin_unlock_irq(&ctx->inflight_lock);
3410 rcu_read_unlock();
3411
3412 return ret;
3413}
3414
Jens Axboe2665abf2019-11-05 12:40:47 -07003415static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
3416{
Jens Axboead8a48a2019-11-15 08:49:11 -07003417 struct io_timeout_data *data = container_of(timer,
3418 struct io_timeout_data, timer);
3419 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07003420 struct io_ring_ctx *ctx = req->ctx;
3421 struct io_kiocb *prev = NULL;
3422 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07003423
3424 spin_lock_irqsave(&ctx->completion_lock, flags);
3425
3426 /*
3427 * We don't expect the list to be empty, that will only happen if we
3428 * race with the completion of the linked work.
3429 */
Pavel Begunkov44932332019-12-05 16:16:35 +03003430 if (!list_empty(&req->link_list)) {
3431 prev = list_entry(req->link_list.prev, struct io_kiocb,
3432 link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07003433 if (refcount_inc_not_zero(&prev->refs)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03003434 list_del_init(&req->link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07003435 prev->flags &= ~REQ_F_LINK_TIMEOUT;
3436 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07003437 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07003438 }
3439
3440 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3441
3442 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003443 req_set_fail_links(prev);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003444 io_async_find_and_cancel(ctx, req, prev->user_data, NULL,
3445 -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07003446 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07003447 } else {
3448 io_cqring_add_event(req, -ETIME);
3449 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07003450 }
Jens Axboe2665abf2019-11-05 12:40:47 -07003451 return HRTIMER_NORESTART;
3452}
3453
Jens Axboead8a48a2019-11-15 08:49:11 -07003454static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07003455{
Jens Axboe76a46e02019-11-10 23:34:16 -07003456 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07003457
Jens Axboe76a46e02019-11-10 23:34:16 -07003458 /*
3459 * If the list is now empty, then our linked request finished before
3460 * we got a chance to setup the timer
3461 */
3462 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov44932332019-12-05 16:16:35 +03003463 if (!list_empty(&req->link_list)) {
Jens Axboe2d283902019-12-04 11:08:05 -07003464 struct io_timeout_data *data = &req->io->timeout;
Jens Axboe94ae5e72019-11-14 19:39:52 -07003465
Jens Axboead8a48a2019-11-15 08:49:11 -07003466 data->timer.function = io_link_timeout_fn;
3467 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
3468 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07003469 }
Jens Axboe76a46e02019-11-10 23:34:16 -07003470 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07003471
Jens Axboe2665abf2019-11-05 12:40:47 -07003472 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07003473 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07003474}
3475
Jens Axboead8a48a2019-11-15 08:49:11 -07003476static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07003477{
3478 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003479
Jens Axboe2665abf2019-11-05 12:40:47 -07003480 if (!(req->flags & REQ_F_LINK))
3481 return NULL;
3482
Pavel Begunkov44932332019-12-05 16:16:35 +03003483 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
3484 link_list);
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003485 if (!nxt || nxt->sqe->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07003486 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07003487
Jens Axboe76a46e02019-11-10 23:34:16 -07003488 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07003489 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07003490}
3491
Jens Axboe0e0702d2019-11-14 21:42:10 -07003492static void __io_queue_sqe(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003493{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07003494 struct io_kiocb *linked_timeout;
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03003495 struct io_kiocb *nxt = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003496 int ret;
3497
Jens Axboe4a0a7a12019-12-09 20:01:01 -07003498again:
3499 linked_timeout = io_prep_linked_timeout(req);
3500
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03003501 ret = io_issue_sqe(req, &nxt, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06003502
3503 /*
3504 * We async punt it if the file wasn't marked NOWAIT, or if the file
3505 * doesn't support non-blocking read/write attempts
3506 */
3507 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
3508 (req->flags & REQ_F_MUST_PUNT))) {
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03003509 if (req->work.flags & IO_WQ_WORK_NEEDS_FILES) {
3510 ret = io_grab_files(req);
3511 if (ret)
3512 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003513 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03003514
3515 /*
3516 * Queued up for async execution, worker will release
3517 * submit reference when the iocb is actually submitted.
3518 */
3519 io_queue_async_work(req);
Jens Axboe4a0a7a12019-12-09 20:01:01 -07003520 goto done_req;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003521 }
Jens Axboee65ef562019-03-12 10:16:44 -06003522
Jens Axboefcb323c2019-10-24 12:39:47 -06003523err:
Jens Axboee65ef562019-03-12 10:16:44 -06003524 /* drop submission reference */
3525 io_put_req(req);
3526
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03003527 if (linked_timeout) {
Jens Axboe76a46e02019-11-10 23:34:16 -07003528 if (!ret)
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03003529 io_queue_linked_timeout(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07003530 else
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03003531 io_put_req(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07003532 }
3533
Jens Axboee65ef562019-03-12 10:16:44 -06003534 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06003535 if (ret) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07003536 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003537 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06003538 io_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06003539 }
Jens Axboe4a0a7a12019-12-09 20:01:01 -07003540done_req:
3541 if (nxt) {
3542 req = nxt;
3543 nxt = NULL;
3544 goto again;
3545 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003546}
3547
Jens Axboe0e0702d2019-11-14 21:42:10 -07003548static void io_queue_sqe(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08003549{
3550 int ret;
3551
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003552 if (unlikely(req->ctx->drain_next)) {
3553 req->flags |= REQ_F_IO_DRAIN;
3554 req->ctx->drain_next = false;
3555 }
3556 req->ctx->drain_next = (req->flags & REQ_F_DRAIN_LINK);
3557
Jackie Liua197f662019-11-08 08:09:12 -07003558 ret = io_req_defer(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08003559 if (ret) {
3560 if (ret != -EIOCBQUEUED) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07003561 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003562 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07003563 io_double_put_req(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08003564 }
Jens Axboe0e0702d2019-11-14 21:42:10 -07003565 } else
3566 __io_queue_sqe(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08003567}
3568
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003569static inline void io_queue_link_head(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08003570{
Jens Axboe94ae5e72019-11-14 19:39:52 -07003571 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003572 io_cqring_add_event(req, -ECANCELED);
3573 io_double_put_req(req);
3574 } else
Jens Axboe0e0702d2019-11-14 21:42:10 -07003575 io_queue_sqe(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08003576}
3577
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003578#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
3579 IOSQE_IO_HARDLINK)
Jens Axboe9e645e112019-05-10 16:07:28 -06003580
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03003581static bool io_submit_sqe(struct io_kiocb *req, struct io_submit_state *state,
Jackie Liua197f662019-11-08 08:09:12 -07003582 struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06003583{
Jackie Liua197f662019-11-08 08:09:12 -07003584 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -06003585 int ret;
3586
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003587 req->user_data = req->sqe->user_data;
Jens Axboe78e19bb2019-11-06 15:21:34 -07003588
Jens Axboe9e645e112019-05-10 16:07:28 -06003589 /* enforce forwards compatibility on users */
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003590 if (unlikely(req->sqe->flags & ~SQE_VALID_FLAGS)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06003591 ret = -EINVAL;
Pavel Begunkov196be952019-11-07 01:41:06 +03003592 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06003593 }
3594
Jackie Liua197f662019-11-08 08:09:12 -07003595 ret = io_req_set_file(state, req);
Jens Axboe9e645e112019-05-10 16:07:28 -06003596 if (unlikely(ret)) {
3597err_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07003598 io_cqring_add_event(req, ret);
3599 io_double_put_req(req);
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03003600 return false;
Jens Axboe9e645e112019-05-10 16:07:28 -06003601 }
3602
Jens Axboe9e645e112019-05-10 16:07:28 -06003603 /*
3604 * If we already have a head request, queue this one for async
3605 * submittal once the head completes. If we don't have a head but
3606 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
3607 * submitted sync once the chain is complete. If none of those
3608 * conditions are true (normal request), then just queue it.
3609 */
3610 if (*link) {
3611 struct io_kiocb *prev = *link;
3612
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003613 if (req->sqe->flags & IOSQE_IO_DRAIN)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003614 (*link)->flags |= REQ_F_DRAIN_LINK | REQ_F_IO_DRAIN;
3615
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003616 if (req->sqe->flags & IOSQE_IO_HARDLINK)
3617 req->flags |= REQ_F_HARDLINK;
3618
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003619 if (io_alloc_async_ctx(req)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06003620 ret = -EAGAIN;
3621 goto err_req;
3622 }
3623
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003624 ret = io_req_defer_prep(req);
Jens Axboe2d283902019-12-04 11:08:05 -07003625 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003626 /* fail even hard links since we don't submit */
Jens Axboe2d283902019-12-04 11:08:05 -07003627 prev->flags |= REQ_F_FAIL_LINK;
Jens Axboef67676d2019-12-02 11:03:47 -07003628 goto err_req;
Jens Axboe2d283902019-12-04 11:08:05 -07003629 }
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02003630 trace_io_uring_link(ctx, req, prev);
Pavel Begunkov44932332019-12-05 16:16:35 +03003631 list_add_tail(&req->link_list, &prev->link_list);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003632 } else if (req->sqe->flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06003633 req->flags |= REQ_F_LINK;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003634 if (req->sqe->flags & IOSQE_IO_HARDLINK)
3635 req->flags |= REQ_F_HARDLINK;
Jens Axboe9e645e112019-05-10 16:07:28 -06003636
Jens Axboe9e645e112019-05-10 16:07:28 -06003637 INIT_LIST_HEAD(&req->link_list);
3638 *link = req;
3639 } else {
Jackie Liua197f662019-11-08 08:09:12 -07003640 io_queue_sqe(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06003641 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03003642
3643 return true;
Jens Axboe9e645e112019-05-10 16:07:28 -06003644}
3645
Jens Axboe9a56a232019-01-09 09:06:50 -07003646/*
3647 * Batched submission is done, ensure local IO is flushed out.
3648 */
3649static void io_submit_state_end(struct io_submit_state *state)
3650{
3651 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06003652 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07003653 if (state->free_reqs)
3654 kmem_cache_free_bulk(req_cachep, state->free_reqs,
3655 &state->reqs[state->cur_req]);
Jens Axboe9a56a232019-01-09 09:06:50 -07003656}
3657
3658/*
3659 * Start submission side cache.
3660 */
3661static void io_submit_state_start(struct io_submit_state *state,
Jackie Liu22efde52019-12-02 17:14:52 +08003662 unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07003663{
3664 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07003665 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07003666 state->file = NULL;
3667 state->ios_left = max_ios;
3668}
3669
Jens Axboe2b188cc2019-01-07 10:46:33 -07003670static void io_commit_sqring(struct io_ring_ctx *ctx)
3671{
Hristo Venev75b28af2019-08-26 17:23:46 +00003672 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003673
Hristo Venev75b28af2019-08-26 17:23:46 +00003674 if (ctx->cached_sq_head != READ_ONCE(rings->sq.head)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07003675 /*
3676 * Ensure any loads from the SQEs are done at this point,
3677 * since once we write the new head, the application could
3678 * write new data to them.
3679 */
Hristo Venev75b28af2019-08-26 17:23:46 +00003680 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003681 }
3682}
3683
3684/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08003685 * Fetch an sqe, if one is available. Note that req->sqe will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07003686 * that is mapped by userspace. This means that care needs to be taken to
3687 * ensure that reads are stable, as we cannot rely on userspace always
3688 * being a good citizen. If members of the sqe are validated and then later
3689 * used, it's important that those reads are done through READ_ONCE() to
3690 * prevent a re-load down the line.
3691 */
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003692static bool io_get_sqring(struct io_ring_ctx *ctx, struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003693{
Hristo Venev75b28af2019-08-26 17:23:46 +00003694 struct io_rings *rings = ctx->rings;
3695 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003696 unsigned head;
3697
3698 /*
3699 * The cached sq head (or cq tail) serves two purposes:
3700 *
3701 * 1) allows us to batch the cost of updating the user visible
3702 * head updates.
3703 * 2) allows the kernel side to track the head on its own, even
3704 * though the application is the one updating it.
3705 */
3706 head = ctx->cached_sq_head;
Stefan Bühlere523a292019-04-19 11:57:44 +02003707 /* make sure SQ entry isn't read before tail */
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03003708 if (unlikely(head == smp_load_acquire(&rings->sq.tail)))
Jens Axboe2b188cc2019-01-07 10:46:33 -07003709 return false;
3710
Hristo Venev75b28af2019-08-26 17:23:46 +00003711 head = READ_ONCE(sq_array[head & ctx->sq_mask]);
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03003712 if (likely(head < ctx->sq_entries)) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003713 /*
3714 * All io need record the previous position, if LINK vs DARIN,
3715 * it can be used to mark the position of the first IO in the
3716 * link list.
3717 */
3718 req->sequence = ctx->cached_sq_head;
3719 req->sqe = &ctx->sq_sqes[head];
Jens Axboe2b188cc2019-01-07 10:46:33 -07003720 ctx->cached_sq_head++;
3721 return true;
3722 }
3723
3724 /* drop invalid entries */
3725 ctx->cached_sq_head++;
Jens Axboe498ccd92019-10-25 10:04:25 -06003726 ctx->cached_sq_dropped++;
3727 WRITE_ONCE(rings->sq_dropped, ctx->cached_sq_dropped);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003728 return false;
3729}
3730
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003731static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkovae9428c2019-11-06 00:22:14 +03003732 struct file *ring_file, int ring_fd,
3733 struct mm_struct **mm, bool async)
Jens Axboe6c271ce2019-01-10 11:22:30 -07003734{
3735 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06003736 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06003737 int i, submitted = 0;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03003738 bool mm_fault = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003739
Jens Axboec4a2ed72019-11-21 21:01:26 -07003740 /* if we have a backlog and couldn't flush it all, return BUSY */
3741 if (!list_empty(&ctx->cq_overflow_list) &&
3742 !io_cqring_overflow_flush(ctx, false))
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003743 return -EBUSY;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003744
3745 if (nr > IO_PLUG_THRESHOLD) {
Jackie Liu22efde52019-12-02 17:14:52 +08003746 io_submit_state_start(&state, nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003747 statep = &state;
3748 }
3749
3750 for (i = 0; i < nr; i++) {
Pavel Begunkov196be952019-11-07 01:41:06 +03003751 struct io_kiocb *req;
Pavel Begunkov50585b92019-11-07 01:41:07 +03003752 unsigned int sqe_flags;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003753
Pavel Begunkov196be952019-11-07 01:41:06 +03003754 req = io_get_req(ctx, statep);
3755 if (unlikely(!req)) {
3756 if (!submitted)
3757 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003758 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06003759 }
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003760 if (!io_get_sqring(ctx, req)) {
Pavel Begunkov196be952019-11-07 01:41:06 +03003761 __io_free_req(req);
3762 break;
3763 }
Jens Axboe9e645e112019-05-10 16:07:28 -06003764
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003765 if (io_sqe_needs_user(req->sqe) && !*mm) {
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03003766 mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm);
3767 if (!mm_fault) {
3768 use_mm(ctx->sqo_mm);
3769 *mm = ctx->sqo_mm;
3770 }
3771 }
3772
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03003773 submitted++;
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003774 sqe_flags = req->sqe->flags;
Pavel Begunkov50585b92019-11-07 01:41:07 +03003775
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003776 req->ring_file = ring_file;
3777 req->ring_fd = ring_fd;
3778 req->has_user = *mm != NULL;
3779 req->in_async = async;
3780 req->needs_fixed_file = async;
3781 trace_io_uring_submit_sqe(ctx, req->sqe->user_data,
Pavel Begunkov50585b92019-11-07 01:41:07 +03003782 true, async);
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03003783 if (!io_submit_sqe(req, statep, &link))
3784 break;
Pavel Begunkove5eb6362019-11-06 00:22:15 +03003785 /*
3786 * If previous wasn't linked and we have a linked command,
3787 * that's the end of the chain. Submit the previous link.
3788 */
Pavel Begunkovffbb8d62019-12-17 20:57:05 +03003789 if (!(sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK)) && link) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003790 io_queue_link_head(link);
Pavel Begunkove5eb6362019-11-06 00:22:15 +03003791 link = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003792 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07003793 }
3794
Jens Axboe9e645e112019-05-10 16:07:28 -06003795 if (link)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003796 io_queue_link_head(link);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003797 if (statep)
3798 io_submit_state_end(&state);
3799
Pavel Begunkovae9428c2019-11-06 00:22:14 +03003800 /* Commit SQ ring head once we've consumed and submitted all SQEs */
3801 io_commit_sqring(ctx);
3802
Jens Axboe6c271ce2019-01-10 11:22:30 -07003803 return submitted;
3804}
3805
3806static int io_sq_thread(void *data)
3807{
Jens Axboe6c271ce2019-01-10 11:22:30 -07003808 struct io_ring_ctx *ctx = data;
3809 struct mm_struct *cur_mm = NULL;
Jens Axboe181e4482019-11-25 08:52:30 -07003810 const struct cred *old_cred;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003811 mm_segment_t old_fs;
3812 DEFINE_WAIT(wait);
3813 unsigned inflight;
3814 unsigned long timeout;
Jens Axboec1edbf52019-11-10 16:56:04 -07003815 int ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003816
Jens Axboe206aefd2019-11-07 18:27:42 -07003817 complete(&ctx->completions[1]);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08003818
Jens Axboe6c271ce2019-01-10 11:22:30 -07003819 old_fs = get_fs();
3820 set_fs(USER_DS);
Jens Axboe181e4482019-11-25 08:52:30 -07003821 old_cred = override_creds(ctx->creds);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003822
Jens Axboec1edbf52019-11-10 16:56:04 -07003823 ret = timeout = inflight = 0;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003824 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003825 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003826
3827 if (inflight) {
3828 unsigned nr_events = 0;
3829
3830 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe2b2ed972019-10-25 10:06:15 -06003831 /*
3832 * inflight is the count of the maximum possible
3833 * entries we submitted, but it can be smaller
3834 * if we dropped some of them. If we don't have
3835 * poll entries available, then we know that we
3836 * have nothing left to poll for. Reset the
3837 * inflight count to zero in that case.
3838 */
3839 mutex_lock(&ctx->uring_lock);
3840 if (!list_empty(&ctx->poll_list))
3841 __io_iopoll_check(ctx, &nr_events, 0);
3842 else
3843 inflight = 0;
3844 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003845 } else {
3846 /*
3847 * Normal IO, just pretend everything completed.
3848 * We don't have to poll completions for that.
3849 */
3850 nr_events = inflight;
3851 }
3852
3853 inflight -= nr_events;
3854 if (!inflight)
3855 timeout = jiffies + ctx->sq_thread_idle;
3856 }
3857
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003858 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07003859
3860 /*
3861 * If submit got -EBUSY, flag us as needing the application
3862 * to enter the kernel to reap and flush events.
3863 */
3864 if (!to_submit || ret == -EBUSY) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07003865 /*
3866 * We're polling. If we're within the defined idle
3867 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07003868 * to sleep. The exception is if we got EBUSY doing
3869 * more IO, we should wait for the application to
3870 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07003871 */
Jens Axboec1edbf52019-11-10 16:56:04 -07003872 if (inflight ||
3873 (!time_after(jiffies, timeout) && ret != -EBUSY)) {
Jens Axboe9831a902019-09-19 09:48:55 -06003874 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07003875 continue;
3876 }
3877
3878 /*
3879 * Drop cur_mm before scheduling, we can't hold it for
3880 * long periods (or over schedule()). Do this before
3881 * adding ourselves to the waitqueue, as the unuse/drop
3882 * may sleep.
3883 */
3884 if (cur_mm) {
3885 unuse_mm(cur_mm);
3886 mmput(cur_mm);
3887 cur_mm = NULL;
3888 }
3889
3890 prepare_to_wait(&ctx->sqo_wait, &wait,
3891 TASK_INTERRUPTIBLE);
3892
3893 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00003894 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02003895 /* make sure to read SQ tail after writing flags */
3896 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07003897
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003898 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07003899 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003900 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07003901 finish_wait(&ctx->sqo_wait, &wait);
3902 break;
3903 }
3904 if (signal_pending(current))
3905 flush_signals(current);
3906 schedule();
3907 finish_wait(&ctx->sqo_wait, &wait);
3908
Hristo Venev75b28af2019-08-26 17:23:46 +00003909 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003910 continue;
3911 }
3912 finish_wait(&ctx->sqo_wait, &wait);
3913
Hristo Venev75b28af2019-08-26 17:23:46 +00003914 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003915 }
3916
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003917 to_submit = min(to_submit, ctx->sq_entries);
Jens Axboe8a4955f2019-12-09 14:52:35 -07003918 mutex_lock(&ctx->uring_lock);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003919 ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true);
Jens Axboe8a4955f2019-12-09 14:52:35 -07003920 mutex_unlock(&ctx->uring_lock);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003921 if (ret > 0)
3922 inflight += ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003923 }
3924
3925 set_fs(old_fs);
3926 if (cur_mm) {
3927 unuse_mm(cur_mm);
3928 mmput(cur_mm);
3929 }
Jens Axboe181e4482019-11-25 08:52:30 -07003930 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06003931
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003932 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06003933
Jens Axboe6c271ce2019-01-10 11:22:30 -07003934 return 0;
3935}
3936
Jens Axboebda52162019-09-24 13:47:15 -06003937struct io_wait_queue {
3938 struct wait_queue_entry wq;
3939 struct io_ring_ctx *ctx;
3940 unsigned to_wait;
3941 unsigned nr_timeouts;
3942};
3943
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003944static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06003945{
3946 struct io_ring_ctx *ctx = iowq->ctx;
3947
3948 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08003949 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06003950 * started waiting. For timeouts, we always want to return to userspace,
3951 * regardless of event count.
3952 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003953 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06003954 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
3955}
3956
3957static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
3958 int wake_flags, void *key)
3959{
3960 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
3961 wq);
3962
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003963 /* use noflush == true, as we can't safely rely on locking context */
3964 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06003965 return -1;
3966
3967 return autoremove_wake_function(curr, mode, wake_flags, key);
3968}
3969
Jens Axboe2b188cc2019-01-07 10:46:33 -07003970/*
3971 * Wait until events become available, if we don't already have some. The
3972 * application must reap them itself, as they reside on the shared cq ring.
3973 */
3974static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
3975 const sigset_t __user *sig, size_t sigsz)
3976{
Jens Axboebda52162019-09-24 13:47:15 -06003977 struct io_wait_queue iowq = {
3978 .wq = {
3979 .private = current,
3980 .func = io_wake_function,
3981 .entry = LIST_HEAD_INIT(iowq.wq.entry),
3982 },
3983 .ctx = ctx,
3984 .to_wait = min_events,
3985 };
Hristo Venev75b28af2019-08-26 17:23:46 +00003986 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08003987 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003988
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003989 if (io_cqring_events(ctx, false) >= min_events)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003990 return 0;
3991
3992 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01003993#ifdef CONFIG_COMPAT
3994 if (in_compat_syscall())
3995 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07003996 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01003997 else
3998#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07003999 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01004000
Jens Axboe2b188cc2019-01-07 10:46:33 -07004001 if (ret)
4002 return ret;
4003 }
4004
Jens Axboebda52162019-09-24 13:47:15 -06004005 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004006 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06004007 do {
4008 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
4009 TASK_INTERRUPTIBLE);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004010 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06004011 break;
4012 schedule();
4013 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08004014 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06004015 break;
4016 }
4017 } while (1);
4018 finish_wait(&ctx->wait, &iowq.wq);
4019
Jackie Liue9ffa5c2019-10-29 11:16:42 +08004020 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004021
Hristo Venev75b28af2019-08-26 17:23:46 +00004022 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004023}
4024
Jens Axboe6b063142019-01-10 22:13:58 -07004025static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
4026{
4027#if defined(CONFIG_UNIX)
4028 if (ctx->ring_sock) {
4029 struct sock *sock = ctx->ring_sock->sk;
4030 struct sk_buff *skb;
4031
4032 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
4033 kfree_skb(skb);
4034 }
4035#else
4036 int i;
4037
Jens Axboe65e19f52019-10-26 07:20:21 -06004038 for (i = 0; i < ctx->nr_user_files; i++) {
4039 struct file *file;
4040
4041 file = io_file_from_index(ctx, i);
4042 if (file)
4043 fput(file);
4044 }
Jens Axboe6b063142019-01-10 22:13:58 -07004045#endif
4046}
4047
4048static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
4049{
Jens Axboe65e19f52019-10-26 07:20:21 -06004050 unsigned nr_tables, i;
4051
4052 if (!ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07004053 return -ENXIO;
4054
4055 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06004056 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
4057 for (i = 0; i < nr_tables; i++)
4058 kfree(ctx->file_table[i].files);
4059 kfree(ctx->file_table);
4060 ctx->file_table = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07004061 ctx->nr_user_files = 0;
4062 return 0;
4063}
4064
Jens Axboe6c271ce2019-01-10 11:22:30 -07004065static void io_sq_thread_stop(struct io_ring_ctx *ctx)
4066{
4067 if (ctx->sqo_thread) {
Jens Axboe206aefd2019-11-07 18:27:42 -07004068 wait_for_completion(&ctx->completions[1]);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02004069 /*
4070 * The park is a bit of a work-around, without it we get
4071 * warning spews on shutdown with SQPOLL set and affinity
4072 * set to a single CPU.
4073 */
Jens Axboe06058632019-04-13 09:26:03 -06004074 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004075 kthread_stop(ctx->sqo_thread);
4076 ctx->sqo_thread = NULL;
4077 }
4078}
4079
Jens Axboe6b063142019-01-10 22:13:58 -07004080static void io_finish_async(struct io_ring_ctx *ctx)
4081{
Jens Axboe6c271ce2019-01-10 11:22:30 -07004082 io_sq_thread_stop(ctx);
4083
Jens Axboe561fb042019-10-24 07:25:42 -06004084 if (ctx->io_wq) {
4085 io_wq_destroy(ctx->io_wq);
4086 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07004087 }
4088}
4089
4090#if defined(CONFIG_UNIX)
4091static void io_destruct_skb(struct sk_buff *skb)
4092{
4093 struct io_ring_ctx *ctx = skb->sk->sk_user_data;
4094
Jens Axboe561fb042019-10-24 07:25:42 -06004095 if (ctx->io_wq)
4096 io_wq_flush(ctx->io_wq);
Jens Axboe8a997342019-10-09 14:40:13 -06004097
Jens Axboe6b063142019-01-10 22:13:58 -07004098 unix_destruct_scm(skb);
4099}
4100
4101/*
4102 * Ensure the UNIX gc is aware of our file set, so we are certain that
4103 * the io_uring can be safely unregistered on process exit, even if we have
4104 * loops in the file referencing.
4105 */
4106static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
4107{
4108 struct sock *sk = ctx->ring_sock->sk;
4109 struct scm_fp_list *fpl;
4110 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06004111 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07004112
4113 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
4114 unsigned long inflight = ctx->user->unix_inflight + nr;
4115
4116 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
4117 return -EMFILE;
4118 }
4119
4120 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
4121 if (!fpl)
4122 return -ENOMEM;
4123
4124 skb = alloc_skb(0, GFP_KERNEL);
4125 if (!skb) {
4126 kfree(fpl);
4127 return -ENOMEM;
4128 }
4129
4130 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07004131
Jens Axboe08a45172019-10-03 08:11:03 -06004132 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07004133 fpl->user = get_uid(ctx->user);
4134 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06004135 struct file *file = io_file_from_index(ctx, i + offset);
4136
4137 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06004138 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06004139 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06004140 unix_inflight(fpl->user, fpl->fp[nr_files]);
4141 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07004142 }
4143
Jens Axboe08a45172019-10-03 08:11:03 -06004144 if (nr_files) {
4145 fpl->max = SCM_MAX_FD;
4146 fpl->count = nr_files;
4147 UNIXCB(skb).fp = fpl;
4148 skb->destructor = io_destruct_skb;
4149 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
4150 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07004151
Jens Axboe08a45172019-10-03 08:11:03 -06004152 for (i = 0; i < nr_files; i++)
4153 fput(fpl->fp[i]);
4154 } else {
4155 kfree_skb(skb);
4156 kfree(fpl);
4157 }
Jens Axboe6b063142019-01-10 22:13:58 -07004158
4159 return 0;
4160}
4161
4162/*
4163 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
4164 * causes regular reference counting to break down. We rely on the UNIX
4165 * garbage collection to take care of this problem for us.
4166 */
4167static int io_sqe_files_scm(struct io_ring_ctx *ctx)
4168{
4169 unsigned left, total;
4170 int ret = 0;
4171
4172 total = 0;
4173 left = ctx->nr_user_files;
4174 while (left) {
4175 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07004176
4177 ret = __io_sqe_files_scm(ctx, this_files, total);
4178 if (ret)
4179 break;
4180 left -= this_files;
4181 total += this_files;
4182 }
4183
4184 if (!ret)
4185 return 0;
4186
4187 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06004188 struct file *file = io_file_from_index(ctx, total);
4189
4190 if (file)
4191 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07004192 total++;
4193 }
4194
4195 return ret;
4196}
4197#else
4198static int io_sqe_files_scm(struct io_ring_ctx *ctx)
4199{
4200 return 0;
4201}
4202#endif
4203
Jens Axboe65e19f52019-10-26 07:20:21 -06004204static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
4205 unsigned nr_files)
4206{
4207 int i;
4208
4209 for (i = 0; i < nr_tables; i++) {
4210 struct fixed_file_table *table = &ctx->file_table[i];
4211 unsigned this_files;
4212
4213 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
4214 table->files = kcalloc(this_files, sizeof(struct file *),
4215 GFP_KERNEL);
4216 if (!table->files)
4217 break;
4218 nr_files -= this_files;
4219 }
4220
4221 if (i == nr_tables)
4222 return 0;
4223
4224 for (i = 0; i < nr_tables; i++) {
4225 struct fixed_file_table *table = &ctx->file_table[i];
4226 kfree(table->files);
4227 }
4228 return 1;
4229}
4230
Jens Axboe6b063142019-01-10 22:13:58 -07004231static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
4232 unsigned nr_args)
4233{
4234 __s32 __user *fds = (__s32 __user *) arg;
Jens Axboe65e19f52019-10-26 07:20:21 -06004235 unsigned nr_tables;
Jens Axboe6b063142019-01-10 22:13:58 -07004236 int fd, ret = 0;
4237 unsigned i;
4238
Jens Axboe65e19f52019-10-26 07:20:21 -06004239 if (ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07004240 return -EBUSY;
4241 if (!nr_args)
4242 return -EINVAL;
4243 if (nr_args > IORING_MAX_FIXED_FILES)
4244 return -EMFILE;
4245
Jens Axboe65e19f52019-10-26 07:20:21 -06004246 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
4247 ctx->file_table = kcalloc(nr_tables, sizeof(struct fixed_file_table),
4248 GFP_KERNEL);
4249 if (!ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07004250 return -ENOMEM;
4251
Jens Axboe65e19f52019-10-26 07:20:21 -06004252 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
4253 kfree(ctx->file_table);
Jens Axboe46568e92019-11-10 08:40:53 -07004254 ctx->file_table = NULL;
Jens Axboe65e19f52019-10-26 07:20:21 -06004255 return -ENOMEM;
4256 }
4257
Jens Axboe08a45172019-10-03 08:11:03 -06004258 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06004259 struct fixed_file_table *table;
4260 unsigned index;
4261
Jens Axboe6b063142019-01-10 22:13:58 -07004262 ret = -EFAULT;
4263 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
4264 break;
Jens Axboe08a45172019-10-03 08:11:03 -06004265 /* allow sparse sets */
4266 if (fd == -1) {
4267 ret = 0;
4268 continue;
4269 }
Jens Axboe6b063142019-01-10 22:13:58 -07004270
Jens Axboe65e19f52019-10-26 07:20:21 -06004271 table = &ctx->file_table[i >> IORING_FILE_TABLE_SHIFT];
4272 index = i & IORING_FILE_TABLE_MASK;
4273 table->files[index] = fget(fd);
Jens Axboe6b063142019-01-10 22:13:58 -07004274
4275 ret = -EBADF;
Jens Axboe65e19f52019-10-26 07:20:21 -06004276 if (!table->files[index])
Jens Axboe6b063142019-01-10 22:13:58 -07004277 break;
4278 /*
4279 * Don't allow io_uring instances to be registered. If UNIX
4280 * isn't enabled, then this causes a reference cycle and this
4281 * instance can never get freed. If UNIX is enabled we'll
4282 * handle it just fine, but there's still no point in allowing
4283 * a ring fd as it doesn't support regular read/write anyway.
4284 */
Jens Axboe65e19f52019-10-26 07:20:21 -06004285 if (table->files[index]->f_op == &io_uring_fops) {
4286 fput(table->files[index]);
Jens Axboe6b063142019-01-10 22:13:58 -07004287 break;
4288 }
Jens Axboe6b063142019-01-10 22:13:58 -07004289 ret = 0;
4290 }
4291
4292 if (ret) {
Jens Axboe65e19f52019-10-26 07:20:21 -06004293 for (i = 0; i < ctx->nr_user_files; i++) {
4294 struct file *file;
Jens Axboe6b063142019-01-10 22:13:58 -07004295
Jens Axboe65e19f52019-10-26 07:20:21 -06004296 file = io_file_from_index(ctx, i);
4297 if (file)
4298 fput(file);
4299 }
4300 for (i = 0; i < nr_tables; i++)
4301 kfree(ctx->file_table[i].files);
4302
4303 kfree(ctx->file_table);
4304 ctx->file_table = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07004305 ctx->nr_user_files = 0;
4306 return ret;
4307 }
4308
4309 ret = io_sqe_files_scm(ctx);
4310 if (ret)
4311 io_sqe_files_unregister(ctx);
4312
4313 return ret;
4314}
4315
Jens Axboec3a31e62019-10-03 13:59:56 -06004316static void io_sqe_file_unregister(struct io_ring_ctx *ctx, int index)
4317{
4318#if defined(CONFIG_UNIX)
Jens Axboe65e19f52019-10-26 07:20:21 -06004319 struct file *file = io_file_from_index(ctx, index);
Jens Axboec3a31e62019-10-03 13:59:56 -06004320 struct sock *sock = ctx->ring_sock->sk;
4321 struct sk_buff_head list, *head = &sock->sk_receive_queue;
4322 struct sk_buff *skb;
4323 int i;
4324
4325 __skb_queue_head_init(&list);
4326
4327 /*
4328 * Find the skb that holds this file in its SCM_RIGHTS. When found,
4329 * remove this entry and rearrange the file array.
4330 */
4331 skb = skb_dequeue(head);
4332 while (skb) {
4333 struct scm_fp_list *fp;
4334
4335 fp = UNIXCB(skb).fp;
4336 for (i = 0; i < fp->count; i++) {
4337 int left;
4338
4339 if (fp->fp[i] != file)
4340 continue;
4341
4342 unix_notinflight(fp->user, fp->fp[i]);
4343 left = fp->count - 1 - i;
4344 if (left) {
4345 memmove(&fp->fp[i], &fp->fp[i + 1],
4346 left * sizeof(struct file *));
4347 }
4348 fp->count--;
4349 if (!fp->count) {
4350 kfree_skb(skb);
4351 skb = NULL;
4352 } else {
4353 __skb_queue_tail(&list, skb);
4354 }
4355 fput(file);
4356 file = NULL;
4357 break;
4358 }
4359
4360 if (!file)
4361 break;
4362
4363 __skb_queue_tail(&list, skb);
4364
4365 skb = skb_dequeue(head);
4366 }
4367
4368 if (skb_peek(&list)) {
4369 spin_lock_irq(&head->lock);
4370 while ((skb = __skb_dequeue(&list)) != NULL)
4371 __skb_queue_tail(head, skb);
4372 spin_unlock_irq(&head->lock);
4373 }
4374#else
Jens Axboe65e19f52019-10-26 07:20:21 -06004375 fput(io_file_from_index(ctx, index));
Jens Axboec3a31e62019-10-03 13:59:56 -06004376#endif
4377}
4378
4379static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
4380 int index)
4381{
4382#if defined(CONFIG_UNIX)
4383 struct sock *sock = ctx->ring_sock->sk;
4384 struct sk_buff_head *head = &sock->sk_receive_queue;
4385 struct sk_buff *skb;
4386
4387 /*
4388 * See if we can merge this file into an existing skb SCM_RIGHTS
4389 * file set. If there's no room, fall back to allocating a new skb
4390 * and filling it in.
4391 */
4392 spin_lock_irq(&head->lock);
4393 skb = skb_peek(head);
4394 if (skb) {
4395 struct scm_fp_list *fpl = UNIXCB(skb).fp;
4396
4397 if (fpl->count < SCM_MAX_FD) {
4398 __skb_unlink(skb, head);
4399 spin_unlock_irq(&head->lock);
4400 fpl->fp[fpl->count] = get_file(file);
4401 unix_inflight(fpl->user, fpl->fp[fpl->count]);
4402 fpl->count++;
4403 spin_lock_irq(&head->lock);
4404 __skb_queue_head(head, skb);
4405 } else {
4406 skb = NULL;
4407 }
4408 }
4409 spin_unlock_irq(&head->lock);
4410
4411 if (skb) {
4412 fput(file);
4413 return 0;
4414 }
4415
4416 return __io_sqe_files_scm(ctx, 1, index);
4417#else
4418 return 0;
4419#endif
4420}
4421
4422static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
4423 unsigned nr_args)
4424{
4425 struct io_uring_files_update up;
4426 __s32 __user *fds;
4427 int fd, i, err;
4428 __u32 done;
4429
Jens Axboe65e19f52019-10-26 07:20:21 -06004430 if (!ctx->file_table)
Jens Axboec3a31e62019-10-03 13:59:56 -06004431 return -ENXIO;
4432 if (!nr_args)
4433 return -EINVAL;
4434 if (copy_from_user(&up, arg, sizeof(up)))
4435 return -EFAULT;
4436 if (check_add_overflow(up.offset, nr_args, &done))
4437 return -EOVERFLOW;
4438 if (done > ctx->nr_user_files)
4439 return -EINVAL;
4440
4441 done = 0;
4442 fds = (__s32 __user *) up.fds;
4443 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06004444 struct fixed_file_table *table;
4445 unsigned index;
4446
Jens Axboec3a31e62019-10-03 13:59:56 -06004447 err = 0;
4448 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
4449 err = -EFAULT;
4450 break;
4451 }
4452 i = array_index_nospec(up.offset, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06004453 table = &ctx->file_table[i >> IORING_FILE_TABLE_SHIFT];
4454 index = i & IORING_FILE_TABLE_MASK;
4455 if (table->files[index]) {
Jens Axboec3a31e62019-10-03 13:59:56 -06004456 io_sqe_file_unregister(ctx, i);
Jens Axboe65e19f52019-10-26 07:20:21 -06004457 table->files[index] = NULL;
Jens Axboec3a31e62019-10-03 13:59:56 -06004458 }
4459 if (fd != -1) {
4460 struct file *file;
4461
4462 file = fget(fd);
4463 if (!file) {
4464 err = -EBADF;
4465 break;
4466 }
4467 /*
4468 * Don't allow io_uring instances to be registered. If
4469 * UNIX isn't enabled, then this causes a reference
4470 * cycle and this instance can never get freed. If UNIX
4471 * is enabled we'll handle it just fine, but there's
4472 * still no point in allowing a ring fd as it doesn't
4473 * support regular read/write anyway.
4474 */
4475 if (file->f_op == &io_uring_fops) {
4476 fput(file);
4477 err = -EBADF;
4478 break;
4479 }
Jens Axboe65e19f52019-10-26 07:20:21 -06004480 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06004481 err = io_sqe_file_register(ctx, file, i);
4482 if (err)
4483 break;
4484 }
4485 nr_args--;
4486 done++;
4487 up.offset++;
4488 }
4489
4490 return done ? done : err;
4491}
4492
Jens Axboe7d723062019-11-12 22:31:31 -07004493static void io_put_work(struct io_wq_work *work)
4494{
4495 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
4496
4497 io_put_req(req);
4498}
4499
4500static void io_get_work(struct io_wq_work *work)
4501{
4502 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
4503
4504 refcount_inc(&req->refs);
4505}
4506
Jens Axboe6c271ce2019-01-10 11:22:30 -07004507static int io_sq_offload_start(struct io_ring_ctx *ctx,
4508 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004509{
Jens Axboe576a3472019-11-25 08:49:20 -07004510 struct io_wq_data data;
Jens Axboe561fb042019-10-24 07:25:42 -06004511 unsigned concurrency;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004512 int ret;
4513
Jens Axboe6c271ce2019-01-10 11:22:30 -07004514 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004515 mmgrab(current->mm);
4516 ctx->sqo_mm = current->mm;
4517
Jens Axboe6c271ce2019-01-10 11:22:30 -07004518 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06004519 ret = -EPERM;
4520 if (!capable(CAP_SYS_ADMIN))
4521 goto err;
4522
Jens Axboe917257d2019-04-13 09:28:55 -06004523 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
4524 if (!ctx->sq_thread_idle)
4525 ctx->sq_thread_idle = HZ;
4526
Jens Axboe6c271ce2019-01-10 11:22:30 -07004527 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06004528 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004529
Jens Axboe917257d2019-04-13 09:28:55 -06004530 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06004531 if (cpu >= nr_cpu_ids)
4532 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08004533 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06004534 goto err;
4535
Jens Axboe6c271ce2019-01-10 11:22:30 -07004536 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
4537 ctx, cpu,
4538 "io_uring-sq");
4539 } else {
4540 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
4541 "io_uring-sq");
4542 }
4543 if (IS_ERR(ctx->sqo_thread)) {
4544 ret = PTR_ERR(ctx->sqo_thread);
4545 ctx->sqo_thread = NULL;
4546 goto err;
4547 }
4548 wake_up_process(ctx->sqo_thread);
4549 } else if (p->flags & IORING_SETUP_SQ_AFF) {
4550 /* Can't have SQ_AFF without SQPOLL */
4551 ret = -EINVAL;
4552 goto err;
4553 }
4554
Jens Axboe576a3472019-11-25 08:49:20 -07004555 data.mm = ctx->sqo_mm;
4556 data.user = ctx->user;
Jens Axboe181e4482019-11-25 08:52:30 -07004557 data.creds = ctx->creds;
Jens Axboe576a3472019-11-25 08:49:20 -07004558 data.get_work = io_get_work;
4559 data.put_work = io_put_work;
4560
Jens Axboe561fb042019-10-24 07:25:42 -06004561 /* Do QD, or 4 * CPUS, whatever is smallest */
4562 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
Jens Axboe576a3472019-11-25 08:49:20 -07004563 ctx->io_wq = io_wq_create(concurrency, &data);
Jens Axboe975c99a52019-10-30 08:42:56 -06004564 if (IS_ERR(ctx->io_wq)) {
4565 ret = PTR_ERR(ctx->io_wq);
4566 ctx->io_wq = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004567 goto err;
4568 }
4569
4570 return 0;
4571err:
Jens Axboe54a91f32019-09-10 09:15:04 -06004572 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004573 mmdrop(ctx->sqo_mm);
4574 ctx->sqo_mm = NULL;
4575 return ret;
4576}
4577
4578static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
4579{
4580 atomic_long_sub(nr_pages, &user->locked_vm);
4581}
4582
4583static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
4584{
4585 unsigned long page_limit, cur_pages, new_pages;
4586
4587 /* Don't allow more pages than we can safely lock */
4588 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
4589
4590 do {
4591 cur_pages = atomic_long_read(&user->locked_vm);
4592 new_pages = cur_pages + nr_pages;
4593 if (new_pages > page_limit)
4594 return -ENOMEM;
4595 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
4596 new_pages) != cur_pages);
4597
4598 return 0;
4599}
4600
4601static void io_mem_free(void *ptr)
4602{
Mark Rutland52e04ef2019-04-30 17:30:21 +01004603 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004604
Mark Rutland52e04ef2019-04-30 17:30:21 +01004605 if (!ptr)
4606 return;
4607
4608 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004609 if (put_page_testzero(page))
4610 free_compound_page(page);
4611}
4612
4613static void *io_mem_alloc(size_t size)
4614{
4615 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
4616 __GFP_NORETRY;
4617
4618 return (void *) __get_free_pages(gfp_flags, get_order(size));
4619}
4620
Hristo Venev75b28af2019-08-26 17:23:46 +00004621static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
4622 size_t *sq_offset)
4623{
4624 struct io_rings *rings;
4625 size_t off, sq_array_size;
4626
4627 off = struct_size(rings, cqes, cq_entries);
4628 if (off == SIZE_MAX)
4629 return SIZE_MAX;
4630
4631#ifdef CONFIG_SMP
4632 off = ALIGN(off, SMP_CACHE_BYTES);
4633 if (off == 0)
4634 return SIZE_MAX;
4635#endif
4636
4637 sq_array_size = array_size(sizeof(u32), sq_entries);
4638 if (sq_array_size == SIZE_MAX)
4639 return SIZE_MAX;
4640
4641 if (check_add_overflow(off, sq_array_size, &off))
4642 return SIZE_MAX;
4643
4644 if (sq_offset)
4645 *sq_offset = off;
4646
4647 return off;
4648}
4649
Jens Axboe2b188cc2019-01-07 10:46:33 -07004650static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
4651{
Hristo Venev75b28af2019-08-26 17:23:46 +00004652 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004653
Hristo Venev75b28af2019-08-26 17:23:46 +00004654 pages = (size_t)1 << get_order(
4655 rings_size(sq_entries, cq_entries, NULL));
4656 pages += (size_t)1 << get_order(
4657 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07004658
Hristo Venev75b28af2019-08-26 17:23:46 +00004659 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004660}
4661
Jens Axboeedafcce2019-01-09 09:16:05 -07004662static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
4663{
4664 int i, j;
4665
4666 if (!ctx->user_bufs)
4667 return -ENXIO;
4668
4669 for (i = 0; i < ctx->nr_user_bufs; i++) {
4670 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
4671
4672 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbard27c4d3a2019-08-04 19:32:06 -07004673 put_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07004674
4675 if (ctx->account_mem)
4676 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004677 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07004678 imu->nr_bvecs = 0;
4679 }
4680
4681 kfree(ctx->user_bufs);
4682 ctx->user_bufs = NULL;
4683 ctx->nr_user_bufs = 0;
4684 return 0;
4685}
4686
4687static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
4688 void __user *arg, unsigned index)
4689{
4690 struct iovec __user *src;
4691
4692#ifdef CONFIG_COMPAT
4693 if (ctx->compat) {
4694 struct compat_iovec __user *ciovs;
4695 struct compat_iovec ciov;
4696
4697 ciovs = (struct compat_iovec __user *) arg;
4698 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
4699 return -EFAULT;
4700
4701 dst->iov_base = (void __user *) (unsigned long) ciov.iov_base;
4702 dst->iov_len = ciov.iov_len;
4703 return 0;
4704 }
4705#endif
4706 src = (struct iovec __user *) arg;
4707 if (copy_from_user(dst, &src[index], sizeof(*dst)))
4708 return -EFAULT;
4709 return 0;
4710}
4711
4712static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
4713 unsigned nr_args)
4714{
4715 struct vm_area_struct **vmas = NULL;
4716 struct page **pages = NULL;
4717 int i, j, got_pages = 0;
4718 int ret = -EINVAL;
4719
4720 if (ctx->user_bufs)
4721 return -EBUSY;
4722 if (!nr_args || nr_args > UIO_MAXIOV)
4723 return -EINVAL;
4724
4725 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
4726 GFP_KERNEL);
4727 if (!ctx->user_bufs)
4728 return -ENOMEM;
4729
4730 for (i = 0; i < nr_args; i++) {
4731 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
4732 unsigned long off, start, end, ubuf;
4733 int pret, nr_pages;
4734 struct iovec iov;
4735 size_t size;
4736
4737 ret = io_copy_iov(ctx, &iov, arg, i);
4738 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03004739 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07004740
4741 /*
4742 * Don't impose further limits on the size and buffer
4743 * constraints here, we'll -EINVAL later when IO is
4744 * submitted if they are wrong.
4745 */
4746 ret = -EFAULT;
4747 if (!iov.iov_base || !iov.iov_len)
4748 goto err;
4749
4750 /* arbitrary limit, but we need something */
4751 if (iov.iov_len > SZ_1G)
4752 goto err;
4753
4754 ubuf = (unsigned long) iov.iov_base;
4755 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
4756 start = ubuf >> PAGE_SHIFT;
4757 nr_pages = end - start;
4758
4759 if (ctx->account_mem) {
4760 ret = io_account_mem(ctx->user, nr_pages);
4761 if (ret)
4762 goto err;
4763 }
4764
4765 ret = 0;
4766 if (!pages || nr_pages > got_pages) {
4767 kfree(vmas);
4768 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004769 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07004770 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004771 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07004772 sizeof(struct vm_area_struct *),
4773 GFP_KERNEL);
4774 if (!pages || !vmas) {
4775 ret = -ENOMEM;
4776 if (ctx->account_mem)
4777 io_unaccount_mem(ctx->user, nr_pages);
4778 goto err;
4779 }
4780 got_pages = nr_pages;
4781 }
4782
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004783 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07004784 GFP_KERNEL);
4785 ret = -ENOMEM;
4786 if (!imu->bvec) {
4787 if (ctx->account_mem)
4788 io_unaccount_mem(ctx->user, nr_pages);
4789 goto err;
4790 }
4791
4792 ret = 0;
4793 down_read(&current->mm->mmap_sem);
Ira Weiny932f4a62019-05-13 17:17:03 -07004794 pret = get_user_pages(ubuf, nr_pages,
4795 FOLL_WRITE | FOLL_LONGTERM,
4796 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07004797 if (pret == nr_pages) {
4798 /* don't support file backed memory */
4799 for (j = 0; j < nr_pages; j++) {
4800 struct vm_area_struct *vma = vmas[j];
4801
4802 if (vma->vm_file &&
4803 !is_file_hugepages(vma->vm_file)) {
4804 ret = -EOPNOTSUPP;
4805 break;
4806 }
4807 }
4808 } else {
4809 ret = pret < 0 ? pret : -EFAULT;
4810 }
4811 up_read(&current->mm->mmap_sem);
4812 if (ret) {
4813 /*
4814 * if we did partial map, or found file backed vmas,
4815 * release any pages we did get
4816 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07004817 if (pret > 0)
4818 put_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07004819 if (ctx->account_mem)
4820 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004821 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07004822 goto err;
4823 }
4824
4825 off = ubuf & ~PAGE_MASK;
4826 size = iov.iov_len;
4827 for (j = 0; j < nr_pages; j++) {
4828 size_t vec_len;
4829
4830 vec_len = min_t(size_t, size, PAGE_SIZE - off);
4831 imu->bvec[j].bv_page = pages[j];
4832 imu->bvec[j].bv_len = vec_len;
4833 imu->bvec[j].bv_offset = off;
4834 off = 0;
4835 size -= vec_len;
4836 }
4837 /* store original address for later verification */
4838 imu->ubuf = ubuf;
4839 imu->len = iov.iov_len;
4840 imu->nr_bvecs = nr_pages;
4841
4842 ctx->nr_user_bufs++;
4843 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004844 kvfree(pages);
4845 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07004846 return 0;
4847err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004848 kvfree(pages);
4849 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07004850 io_sqe_buffer_unregister(ctx);
4851 return ret;
4852}
4853
Jens Axboe9b402842019-04-11 11:45:41 -06004854static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
4855{
4856 __s32 __user *fds = arg;
4857 int fd;
4858
4859 if (ctx->cq_ev_fd)
4860 return -EBUSY;
4861
4862 if (copy_from_user(&fd, fds, sizeof(*fds)))
4863 return -EFAULT;
4864
4865 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
4866 if (IS_ERR(ctx->cq_ev_fd)) {
4867 int ret = PTR_ERR(ctx->cq_ev_fd);
4868 ctx->cq_ev_fd = NULL;
4869 return ret;
4870 }
4871
4872 return 0;
4873}
4874
4875static int io_eventfd_unregister(struct io_ring_ctx *ctx)
4876{
4877 if (ctx->cq_ev_fd) {
4878 eventfd_ctx_put(ctx->cq_ev_fd);
4879 ctx->cq_ev_fd = NULL;
4880 return 0;
4881 }
4882
4883 return -ENXIO;
4884}
4885
Jens Axboe2b188cc2019-01-07 10:46:33 -07004886static void io_ring_ctx_free(struct io_ring_ctx *ctx)
4887{
Jens Axboe6b063142019-01-10 22:13:58 -07004888 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004889 if (ctx->sqo_mm)
4890 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07004891
4892 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07004893 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07004894 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06004895 io_eventfd_unregister(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07004896
Jens Axboe2b188cc2019-01-07 10:46:33 -07004897#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07004898 if (ctx->ring_sock) {
4899 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004900 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07004901 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004902#endif
4903
Hristo Venev75b28af2019-08-26 17:23:46 +00004904 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004905 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004906
4907 percpu_ref_exit(&ctx->refs);
4908 if (ctx->account_mem)
4909 io_unaccount_mem(ctx->user,
4910 ring_pages(ctx->sq_entries, ctx->cq_entries));
4911 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07004912 put_cred(ctx->creds);
Jens Axboe206aefd2019-11-07 18:27:42 -07004913 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -07004914 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07004915 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004916 kfree(ctx);
4917}
4918
4919static __poll_t io_uring_poll(struct file *file, poll_table *wait)
4920{
4921 struct io_ring_ctx *ctx = file->private_data;
4922 __poll_t mask = 0;
4923
4924 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02004925 /*
4926 * synchronizes with barrier from wq_has_sleeper call in
4927 * io_commit_cqring
4928 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004929 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00004930 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
4931 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004932 mask |= EPOLLOUT | EPOLLWRNORM;
yangerkundaa5de52019-09-24 20:53:34 +08004933 if (READ_ONCE(ctx->rings->cq.head) != ctx->cached_cq_tail)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004934 mask |= EPOLLIN | EPOLLRDNORM;
4935
4936 return mask;
4937}
4938
4939static int io_uring_fasync(int fd, struct file *file, int on)
4940{
4941 struct io_ring_ctx *ctx = file->private_data;
4942
4943 return fasync_helper(fd, file, on, &ctx->cq_fasync);
4944}
4945
4946static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
4947{
4948 mutex_lock(&ctx->uring_lock);
4949 percpu_ref_kill(&ctx->refs);
4950 mutex_unlock(&ctx->uring_lock);
4951
Jens Axboe5262f562019-09-17 12:26:57 -06004952 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004953 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06004954
4955 if (ctx->io_wq)
4956 io_wq_cancel_all(ctx->io_wq);
4957
Jens Axboedef596e2019-01-09 08:59:42 -07004958 io_iopoll_reap_events(ctx);
Jens Axboe15dff282019-11-13 09:09:23 -07004959 /* if we failed setting up the ctx, we might not have any rings */
4960 if (ctx->rings)
4961 io_cqring_overflow_flush(ctx, true);
Jens Axboe206aefd2019-11-07 18:27:42 -07004962 wait_for_completion(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004963 io_ring_ctx_free(ctx);
4964}
4965
4966static int io_uring_release(struct inode *inode, struct file *file)
4967{
4968 struct io_ring_ctx *ctx = file->private_data;
4969
4970 file->private_data = NULL;
4971 io_ring_ctx_wait_and_kill(ctx);
4972 return 0;
4973}
4974
Jens Axboefcb323c2019-10-24 12:39:47 -06004975static void io_uring_cancel_files(struct io_ring_ctx *ctx,
4976 struct files_struct *files)
4977{
4978 struct io_kiocb *req;
4979 DEFINE_WAIT(wait);
4980
4981 while (!list_empty_careful(&ctx->inflight_list)) {
Jens Axboe768134d2019-11-10 20:30:53 -07004982 struct io_kiocb *cancel_req = NULL;
Jens Axboefcb323c2019-10-24 12:39:47 -06004983
4984 spin_lock_irq(&ctx->inflight_lock);
4985 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07004986 if (req->work.files != files)
4987 continue;
4988 /* req is being completed, ignore */
4989 if (!refcount_inc_not_zero(&req->refs))
4990 continue;
4991 cancel_req = req;
4992 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06004993 }
Jens Axboe768134d2019-11-10 20:30:53 -07004994 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06004995 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07004996 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06004997 spin_unlock_irq(&ctx->inflight_lock);
4998
Jens Axboe768134d2019-11-10 20:30:53 -07004999 /* We need to keep going until we don't find a matching req */
5000 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06005001 break;
Bob Liu2f6d9b92019-11-13 18:06:24 +08005002
5003 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
5004 io_put_req(cancel_req);
Jens Axboefcb323c2019-10-24 12:39:47 -06005005 schedule();
5006 }
Jens Axboe768134d2019-11-10 20:30:53 -07005007 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06005008}
5009
5010static int io_uring_flush(struct file *file, void *data)
5011{
5012 struct io_ring_ctx *ctx = file->private_data;
5013
5014 io_uring_cancel_files(ctx, data);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005015 if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) {
5016 io_cqring_overflow_flush(ctx, true);
Jens Axboefcb323c2019-10-24 12:39:47 -06005017 io_wq_cancel_all(ctx->io_wq);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005018 }
Jens Axboefcb323c2019-10-24 12:39:47 -06005019 return 0;
5020}
5021
Roman Penyaev6c5c2402019-11-28 12:53:22 +01005022static void *io_uring_validate_mmap_request(struct file *file,
5023 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005024{
Jens Axboe2b188cc2019-01-07 10:46:33 -07005025 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01005026 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005027 struct page *page;
5028 void *ptr;
5029
5030 switch (offset) {
5031 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00005032 case IORING_OFF_CQ_RING:
5033 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005034 break;
5035 case IORING_OFF_SQES:
5036 ptr = ctx->sq_sqes;
5037 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005038 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01005039 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005040 }
5041
5042 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07005043 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01005044 return ERR_PTR(-EINVAL);
5045
5046 return ptr;
5047}
5048
5049#ifdef CONFIG_MMU
5050
5051static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
5052{
5053 size_t sz = vma->vm_end - vma->vm_start;
5054 unsigned long pfn;
5055 void *ptr;
5056
5057 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
5058 if (IS_ERR(ptr))
5059 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005060
5061 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
5062 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
5063}
5064
Roman Penyaev6c5c2402019-11-28 12:53:22 +01005065#else /* !CONFIG_MMU */
5066
5067static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
5068{
5069 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
5070}
5071
5072static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
5073{
5074 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
5075}
5076
5077static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
5078 unsigned long addr, unsigned long len,
5079 unsigned long pgoff, unsigned long flags)
5080{
5081 void *ptr;
5082
5083 ptr = io_uring_validate_mmap_request(file, pgoff, len);
5084 if (IS_ERR(ptr))
5085 return PTR_ERR(ptr);
5086
5087 return (unsigned long) ptr;
5088}
5089
5090#endif /* !CONFIG_MMU */
5091
Jens Axboe2b188cc2019-01-07 10:46:33 -07005092SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
5093 u32, min_complete, u32, flags, const sigset_t __user *, sig,
5094 size_t, sigsz)
5095{
5096 struct io_ring_ctx *ctx;
5097 long ret = -EBADF;
5098 int submitted = 0;
5099 struct fd f;
5100
Jens Axboe6c271ce2019-01-10 11:22:30 -07005101 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07005102 return -EINVAL;
5103
5104 f = fdget(fd);
5105 if (!f.file)
5106 return -EBADF;
5107
5108 ret = -EOPNOTSUPP;
5109 if (f.file->f_op != &io_uring_fops)
5110 goto out_fput;
5111
5112 ret = -ENXIO;
5113 ctx = f.file->private_data;
5114 if (!percpu_ref_tryget(&ctx->refs))
5115 goto out_fput;
5116
Jens Axboe6c271ce2019-01-10 11:22:30 -07005117 /*
5118 * For SQ polling, the thread will do all submissions and completions.
5119 * Just return the requested submit count, and wake the thread if
5120 * we were asked to.
5121 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06005122 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005123 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07005124 if (!list_empty_careful(&ctx->cq_overflow_list))
5125 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005126 if (flags & IORING_ENTER_SQ_WAKEUP)
5127 wake_up(&ctx->sqo_wait);
5128 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06005129 } else if (to_submit) {
Pavel Begunkovae9428c2019-11-06 00:22:14 +03005130 struct mm_struct *cur_mm;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005131
Pavel Begunkovae9428c2019-11-06 00:22:14 +03005132 to_submit = min(to_submit, ctx->sq_entries);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005133 mutex_lock(&ctx->uring_lock);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03005134 /* already have mm, so io_submit_sqes() won't try to grab it */
5135 cur_mm = ctx->sqo_mm;
5136 submitted = io_submit_sqes(ctx, to_submit, f.file, fd,
5137 &cur_mm, false);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005138 mutex_unlock(&ctx->uring_lock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005139 }
5140 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07005141 unsigned nr_events = 0;
5142
Jens Axboe2b188cc2019-01-07 10:46:33 -07005143 min_complete = min(min_complete, ctx->cq_entries);
5144
Jens Axboedef596e2019-01-09 08:59:42 -07005145 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07005146 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07005147 } else {
5148 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
5149 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07005150 }
5151
Pavel Begunkov6805b322019-10-08 02:18:42 +03005152 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005153out_fput:
5154 fdput(f);
5155 return submitted ? submitted : ret;
5156}
5157
5158static const struct file_operations io_uring_fops = {
5159 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06005160 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07005161 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01005162#ifndef CONFIG_MMU
5163 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
5164 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
5165#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07005166 .poll = io_uring_poll,
5167 .fasync = io_uring_fasync,
5168};
5169
5170static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
5171 struct io_uring_params *p)
5172{
Hristo Venev75b28af2019-08-26 17:23:46 +00005173 struct io_rings *rings;
5174 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005175
Hristo Venev75b28af2019-08-26 17:23:46 +00005176 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
5177 if (size == SIZE_MAX)
5178 return -EOVERFLOW;
5179
5180 rings = io_mem_alloc(size);
5181 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005182 return -ENOMEM;
5183
Hristo Venev75b28af2019-08-26 17:23:46 +00005184 ctx->rings = rings;
5185 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
5186 rings->sq_ring_mask = p->sq_entries - 1;
5187 rings->cq_ring_mask = p->cq_entries - 1;
5188 rings->sq_ring_entries = p->sq_entries;
5189 rings->cq_ring_entries = p->cq_entries;
5190 ctx->sq_mask = rings->sq_ring_mask;
5191 ctx->cq_mask = rings->cq_ring_mask;
5192 ctx->sq_entries = rings->sq_ring_entries;
5193 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005194
5195 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07005196 if (size == SIZE_MAX) {
5197 io_mem_free(ctx->rings);
5198 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005199 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07005200 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07005201
5202 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07005203 if (!ctx->sq_sqes) {
5204 io_mem_free(ctx->rings);
5205 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005206 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07005207 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07005208
Jens Axboe2b188cc2019-01-07 10:46:33 -07005209 return 0;
5210}
5211
5212/*
5213 * Allocate an anonymous fd, this is what constitutes the application
5214 * visible backing of an io_uring instance. The application mmaps this
5215 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
5216 * we have to tie this fd to a socket for file garbage collection purposes.
5217 */
5218static int io_uring_get_fd(struct io_ring_ctx *ctx)
5219{
5220 struct file *file;
5221 int ret;
5222
5223#if defined(CONFIG_UNIX)
5224 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
5225 &ctx->ring_sock);
5226 if (ret)
5227 return ret;
5228#endif
5229
5230 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
5231 if (ret < 0)
5232 goto err;
5233
5234 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
5235 O_RDWR | O_CLOEXEC);
5236 if (IS_ERR(file)) {
5237 put_unused_fd(ret);
5238 ret = PTR_ERR(file);
5239 goto err;
5240 }
5241
5242#if defined(CONFIG_UNIX)
5243 ctx->ring_sock->file = file;
Jens Axboe6b063142019-01-10 22:13:58 -07005244 ctx->ring_sock->sk->sk_user_data = ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005245#endif
5246 fd_install(ret, file);
5247 return ret;
5248err:
5249#if defined(CONFIG_UNIX)
5250 sock_release(ctx->ring_sock);
5251 ctx->ring_sock = NULL;
5252#endif
5253 return ret;
5254}
5255
5256static int io_uring_create(unsigned entries, struct io_uring_params *p)
5257{
5258 struct user_struct *user = NULL;
5259 struct io_ring_ctx *ctx;
5260 bool account_mem;
5261 int ret;
5262
5263 if (!entries || entries > IORING_MAX_ENTRIES)
5264 return -EINVAL;
5265
5266 /*
5267 * Use twice as many entries for the CQ ring. It's possible for the
5268 * application to drive a higher depth than the size of the SQ ring,
5269 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06005270 * some flexibility in overcommitting a bit. If the application has
5271 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
5272 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07005273 */
5274 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06005275 if (p->flags & IORING_SETUP_CQSIZE) {
5276 /*
5277 * If IORING_SETUP_CQSIZE is set, we do the same roundup
5278 * to a power-of-two, if it isn't already. We do NOT impose
5279 * any cq vs sq ring sizing.
5280 */
5281 if (p->cq_entries < p->sq_entries || p->cq_entries > IORING_MAX_CQ_ENTRIES)
5282 return -EINVAL;
5283 p->cq_entries = roundup_pow_of_two(p->cq_entries);
5284 } else {
5285 p->cq_entries = 2 * p->sq_entries;
5286 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07005287
5288 user = get_uid(current_user());
5289 account_mem = !capable(CAP_IPC_LOCK);
5290
5291 if (account_mem) {
5292 ret = io_account_mem(user,
5293 ring_pages(p->sq_entries, p->cq_entries));
5294 if (ret) {
5295 free_uid(user);
5296 return ret;
5297 }
5298 }
5299
5300 ctx = io_ring_ctx_alloc(p);
5301 if (!ctx) {
5302 if (account_mem)
5303 io_unaccount_mem(user, ring_pages(p->sq_entries,
5304 p->cq_entries));
5305 free_uid(user);
5306 return -ENOMEM;
5307 }
5308 ctx->compat = in_compat_syscall();
5309 ctx->account_mem = account_mem;
5310 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07005311 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07005312
5313 ret = io_allocate_scq_urings(ctx, p);
5314 if (ret)
5315 goto err;
5316
Jens Axboe6c271ce2019-01-10 11:22:30 -07005317 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005318 if (ret)
5319 goto err;
5320
Jens Axboe2b188cc2019-01-07 10:46:33 -07005321 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00005322 p->sq_off.head = offsetof(struct io_rings, sq.head);
5323 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
5324 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
5325 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
5326 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
5327 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
5328 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005329
5330 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00005331 p->cq_off.head = offsetof(struct io_rings, cq.head);
5332 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
5333 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
5334 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
5335 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
5336 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06005337
Jens Axboe044c1ab2019-10-28 09:15:33 -06005338 /*
5339 * Install ring fd as the very last thing, so we don't risk someone
5340 * having closed it before we finish setup
5341 */
5342 ret = io_uring_get_fd(ctx);
5343 if (ret < 0)
5344 goto err;
5345
Jens Axboeda8c9692019-12-02 18:51:26 -07005346 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
5347 IORING_FEAT_SUBMIT_STABLE;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02005348 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005349 return ret;
5350err:
5351 io_ring_ctx_wait_and_kill(ctx);
5352 return ret;
5353}
5354
5355/*
5356 * Sets up an aio uring context, and returns the fd. Applications asks for a
5357 * ring size, we return the actual sq/cq ring sizes (among other things) in the
5358 * params structure passed in.
5359 */
5360static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
5361{
5362 struct io_uring_params p;
5363 long ret;
5364 int i;
5365
5366 if (copy_from_user(&p, params, sizeof(p)))
5367 return -EFAULT;
5368 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
5369 if (p.resv[i])
5370 return -EINVAL;
5371 }
5372
Jens Axboe6c271ce2019-01-10 11:22:30 -07005373 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe33a107f2019-10-04 12:10:03 -06005374 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE))
Jens Axboe2b188cc2019-01-07 10:46:33 -07005375 return -EINVAL;
5376
5377 ret = io_uring_create(entries, &p);
5378 if (ret < 0)
5379 return ret;
5380
5381 if (copy_to_user(params, &p, sizeof(p)))
5382 return -EFAULT;
5383
5384 return ret;
5385}
5386
5387SYSCALL_DEFINE2(io_uring_setup, u32, entries,
5388 struct io_uring_params __user *, params)
5389{
5390 return io_uring_setup(entries, params);
5391}
5392
Jens Axboeedafcce2019-01-09 09:16:05 -07005393static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
5394 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06005395 __releases(ctx->uring_lock)
5396 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07005397{
5398 int ret;
5399
Jens Axboe35fa71a2019-04-22 10:23:23 -06005400 /*
5401 * We're inside the ring mutex, if the ref is already dying, then
5402 * someone else killed the ctx or is already going through
5403 * io_uring_register().
5404 */
5405 if (percpu_ref_is_dying(&ctx->refs))
5406 return -ENXIO;
5407
Jens Axboeedafcce2019-01-09 09:16:05 -07005408 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06005409
5410 /*
5411 * Drop uring mutex before waiting for references to exit. If another
5412 * thread is currently inside io_uring_enter() it might need to grab
5413 * the uring_lock to make progress. If we hold it here across the drain
5414 * wait, then we can deadlock. It's safe to drop the mutex here, since
5415 * no new references will come in after we've killed the percpu ref.
5416 */
5417 mutex_unlock(&ctx->uring_lock);
Jens Axboe206aefd2019-11-07 18:27:42 -07005418 wait_for_completion(&ctx->completions[0]);
Jens Axboeb19062a2019-04-15 10:49:38 -06005419 mutex_lock(&ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07005420
5421 switch (opcode) {
5422 case IORING_REGISTER_BUFFERS:
5423 ret = io_sqe_buffer_register(ctx, arg, nr_args);
5424 break;
5425 case IORING_UNREGISTER_BUFFERS:
5426 ret = -EINVAL;
5427 if (arg || nr_args)
5428 break;
5429 ret = io_sqe_buffer_unregister(ctx);
5430 break;
Jens Axboe6b063142019-01-10 22:13:58 -07005431 case IORING_REGISTER_FILES:
5432 ret = io_sqe_files_register(ctx, arg, nr_args);
5433 break;
5434 case IORING_UNREGISTER_FILES:
5435 ret = -EINVAL;
5436 if (arg || nr_args)
5437 break;
5438 ret = io_sqe_files_unregister(ctx);
5439 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06005440 case IORING_REGISTER_FILES_UPDATE:
5441 ret = io_sqe_files_update(ctx, arg, nr_args);
5442 break;
Jens Axboe9b402842019-04-11 11:45:41 -06005443 case IORING_REGISTER_EVENTFD:
5444 ret = -EINVAL;
5445 if (nr_args != 1)
5446 break;
5447 ret = io_eventfd_register(ctx, arg);
5448 break;
5449 case IORING_UNREGISTER_EVENTFD:
5450 ret = -EINVAL;
5451 if (arg || nr_args)
5452 break;
5453 ret = io_eventfd_unregister(ctx);
5454 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07005455 default:
5456 ret = -EINVAL;
5457 break;
5458 }
5459
5460 /* bring the ctx back to life */
Jens Axboe206aefd2019-11-07 18:27:42 -07005461 reinit_completion(&ctx->completions[0]);
Jens Axboeedafcce2019-01-09 09:16:05 -07005462 percpu_ref_reinit(&ctx->refs);
5463 return ret;
5464}
5465
5466SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
5467 void __user *, arg, unsigned int, nr_args)
5468{
5469 struct io_ring_ctx *ctx;
5470 long ret = -EBADF;
5471 struct fd f;
5472
5473 f = fdget(fd);
5474 if (!f.file)
5475 return -EBADF;
5476
5477 ret = -EOPNOTSUPP;
5478 if (f.file->f_op != &io_uring_fops)
5479 goto out_fput;
5480
5481 ctx = f.file->private_data;
5482
5483 mutex_lock(&ctx->uring_lock);
5484 ret = __io_uring_register(ctx, opcode, arg, nr_args);
5485 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02005486 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
5487 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07005488out_fput:
5489 fdput(f);
5490 return ret;
5491}
5492
Jens Axboe2b188cc2019-01-07 10:46:33 -07005493static int __init io_uring_init(void)
5494{
5495 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
5496 return 0;
5497};
5498__initcall(io_uring_init);