blob: 2a173f54ec8ea42d7fe36fdfcbea4bf4c34ab7e3 [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 Axboe9adbd452019-12-20 08:45:55 -0700335struct io_rw {
336 /* NOTE: kiocb has the file as the first member, so don't do it here */
337 struct kiocb kiocb;
338 u64 addr;
339 u64 len;
340};
341
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700342struct io_connect {
343 struct file *file;
344 struct sockaddr __user *addr;
345 int addr_len;
346};
347
Jens Axboef499a022019-12-02 16:28:46 -0700348struct io_async_connect {
349 struct sockaddr_storage address;
350};
351
Jens Axboe03b12302019-12-02 18:50:25 -0700352struct io_async_msghdr {
353 struct iovec fast_iov[UIO_FASTIOV];
354 struct iovec *iov;
355 struct sockaddr __user *uaddr;
356 struct msghdr msg;
357};
358
Jens Axboef67676d2019-12-02 11:03:47 -0700359struct io_async_rw {
360 struct iovec fast_iov[UIO_FASTIOV];
361 struct iovec *iov;
362 ssize_t nr_segs;
363 ssize_t size;
364};
365
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700366struct io_async_ctx {
367 struct io_uring_sqe sqe;
Jens Axboef67676d2019-12-02 11:03:47 -0700368 union {
369 struct io_async_rw rw;
Jens Axboe03b12302019-12-02 18:50:25 -0700370 struct io_async_msghdr msg;
Jens Axboef499a022019-12-02 16:28:46 -0700371 struct io_async_connect connect;
Jens Axboe2d283902019-12-04 11:08:05 -0700372 struct io_timeout_data timeout;
Jens Axboef67676d2019-12-02 11:03:47 -0700373 };
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700374};
375
Jens Axboe09bb8392019-03-13 12:39:28 -0600376/*
377 * NOTE! Each of the iocb union members has the file pointer
378 * as the first entry in their struct definition. So you can
379 * access the file pointer through any of the sub-structs,
380 * or directly as just 'ki_filp' in this struct.
381 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700382struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700383 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600384 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700385 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700386 struct io_poll_iocb poll;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700387 struct io_accept accept;
388 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700389 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700390 struct io_timeout timeout;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700391 struct io_connect connect;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700392 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700393
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300394 const struct io_uring_sqe *sqe;
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700395 struct io_async_ctx *io;
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300396 struct file *ring_file;
397 int ring_fd;
398 bool has_user;
399 bool in_async;
400 bool needs_fixed_file;
Jens Axboed625c6e2019-12-17 19:53:05 -0700401 u8 opcode;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700402
403 struct io_ring_ctx *ctx;
Jens Axboeeac406c2019-11-14 12:09:58 -0700404 union {
405 struct list_head list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700406 struct hlist_node hash_node;
Jens Axboeeac406c2019-11-14 12:09:58 -0700407 };
Jens Axboe9e645e112019-05-10 16:07:28 -0600408 struct list_head link_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700409 unsigned int flags;
Jens Axboec16361c2019-01-17 08:39:48 -0700410 refcount_t refs;
Stefan Bühler8449eed2019-04-27 20:34:19 +0200411#define REQ_F_NOWAIT 1 /* must not punt to workers */
Jens Axboedef596e2019-01-09 08:59:42 -0700412#define REQ_F_IOPOLL_COMPLETED 2 /* polled IO has completed */
Jens Axboe6b063142019-01-10 22:13:58 -0700413#define REQ_F_FIXED_FILE 4 /* ctx owns file */
Jens Axboe4d7dd462019-11-20 13:03:52 -0700414#define REQ_F_LINK_NEXT 8 /* already grabbed next link */
Stefan Bühlere2033e32019-05-11 19:08:01 +0200415#define REQ_F_IO_DRAIN 16 /* drain existing IO first */
416#define REQ_F_IO_DRAINED 32 /* drain done */
Jens Axboe9e645e112019-05-10 16:07:28 -0600417#define REQ_F_LINK 64 /* linked sqes */
Jens Axboe2665abf2019-11-05 12:40:47 -0700418#define REQ_F_LINK_TIMEOUT 128 /* has linked timeout */
Zhengyuan Liuf7b76ac2019-07-16 23:26:14 +0800419#define REQ_F_FAIL_LINK 256 /* fail rest of links */
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +0300420#define REQ_F_DRAIN_LINK 512 /* link should be fully drained */
Jens Axboe5262f562019-09-17 12:26:57 -0600421#define REQ_F_TIMEOUT 1024 /* timeout request */
Jens Axboe491381ce2019-10-17 09:20:46 -0600422#define REQ_F_ISREG 2048 /* regular file */
423#define REQ_F_MUST_PUNT 4096 /* must be punted even for NONBLOCK */
Jens Axboe93bd25b2019-11-11 23:34:31 -0700424#define REQ_F_TIMEOUT_NOSEQ 8192 /* no timeout sequence */
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800425#define REQ_F_INFLIGHT 16384 /* on inflight list */
426#define REQ_F_COMP_LOCKED 32768 /* completion under lock */
Jens Axboe4e88d6e2019-12-07 20:59:47 -0700427#define REQ_F_HARDLINK 65536 /* doesn't sever on completion < 0 */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700428#define REQ_F_PREPPED 131072 /* request already opcode prepared */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700429 u64 user_data;
Jens Axboe9e645e112019-05-10 16:07:28 -0600430 u32 result;
Jens Axboede0617e2019-04-06 21:51:27 -0600431 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700432
Jens Axboefcb323c2019-10-24 12:39:47 -0600433 struct list_head inflight_entry;
434
Jens Axboe561fb042019-10-24 07:25:42 -0600435 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700436};
437
438#define IO_PLUG_THRESHOLD 2
Jens Axboedef596e2019-01-09 08:59:42 -0700439#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700440
Jens Axboe9a56a232019-01-09 09:06:50 -0700441struct io_submit_state {
442 struct blk_plug plug;
443
444 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700445 * io_kiocb alloc cache
446 */
447 void *reqs[IO_IOPOLL_BATCH];
448 unsigned int free_reqs;
449 unsigned int cur_req;
450
451 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700452 * File reference cache
453 */
454 struct file *file;
455 unsigned int fd;
456 unsigned int has_refs;
457 unsigned int used_refs;
458 unsigned int ios_left;
459};
460
Jens Axboe561fb042019-10-24 07:25:42 -0600461static void io_wq_submit_work(struct io_wq_work **workptr);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700462static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liu4fe2c962019-09-09 20:50:40 +0800463static void __io_free_req(struct io_kiocb *req);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800464static void io_put_req(struct io_kiocb *req);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700465static void io_double_put_req(struct io_kiocb *req);
Jens Axboe978db572019-11-14 22:39:04 -0700466static void __io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700467static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
468static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboede0617e2019-04-06 21:51:27 -0600469
Jens Axboe2b188cc2019-01-07 10:46:33 -0700470static struct kmem_cache *req_cachep;
471
472static const struct file_operations io_uring_fops;
473
474struct sock *io_uring_get_socket(struct file *file)
475{
476#if defined(CONFIG_UNIX)
477 if (file->f_op == &io_uring_fops) {
478 struct io_ring_ctx *ctx = file->private_data;
479
480 return ctx->ring_sock->sk;
481 }
482#endif
483 return NULL;
484}
485EXPORT_SYMBOL(io_uring_get_socket);
486
487static void io_ring_ctx_ref_free(struct percpu_ref *ref)
488{
489 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
490
Jens Axboe206aefd2019-11-07 18:27:42 -0700491 complete(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700492}
493
494static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
495{
496 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -0700497 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700498
499 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
500 if (!ctx)
501 return NULL;
502
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700503 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
504 if (!ctx->fallback_req)
505 goto err;
506
Jens Axboe206aefd2019-11-07 18:27:42 -0700507 ctx->completions = kmalloc(2 * sizeof(struct completion), GFP_KERNEL);
508 if (!ctx->completions)
509 goto err;
510
Jens Axboe78076bb2019-12-04 19:56:40 -0700511 /*
512 * Use 5 bits less than the max cq entries, that should give us around
513 * 32 entries per hash list if totally full and uniformly spread.
514 */
515 hash_bits = ilog2(p->cq_entries);
516 hash_bits -= 5;
517 if (hash_bits <= 0)
518 hash_bits = 1;
519 ctx->cancel_hash_bits = hash_bits;
520 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
521 GFP_KERNEL);
522 if (!ctx->cancel_hash)
523 goto err;
524 __hash_init(ctx->cancel_hash, 1U << hash_bits);
525
Roman Gushchin21482892019-05-07 10:01:48 -0700526 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -0700527 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
528 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700529
530 ctx->flags = p->flags;
531 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700532 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe206aefd2019-11-07 18:27:42 -0700533 init_completion(&ctx->completions[0]);
534 init_completion(&ctx->completions[1]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700535 mutex_init(&ctx->uring_lock);
536 init_waitqueue_head(&ctx->wait);
537 spin_lock_init(&ctx->completion_lock);
Jens Axboedef596e2019-01-09 08:59:42 -0700538 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboede0617e2019-04-06 21:51:27 -0600539 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -0600540 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -0600541 init_waitqueue_head(&ctx->inflight_wait);
542 spin_lock_init(&ctx->inflight_lock);
543 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700544 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -0700545err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700546 if (ctx->fallback_req)
547 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe206aefd2019-11-07 18:27:42 -0700548 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -0700549 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -0700550 kfree(ctx);
551 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700552}
553
Bob Liu9d858b22019-11-13 18:06:25 +0800554static inline bool __req_need_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -0600555{
Jackie Liua197f662019-11-08 08:09:12 -0700556 struct io_ring_ctx *ctx = req->ctx;
557
Jens Axboe498ccd92019-10-25 10:04:25 -0600558 return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped
559 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboede0617e2019-04-06 21:51:27 -0600560}
561
Bob Liu9d858b22019-11-13 18:06:25 +0800562static inline bool req_need_defer(struct io_kiocb *req)
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600563{
Bob Liu9d858b22019-11-13 18:06:25 +0800564 if ((req->flags & (REQ_F_IO_DRAIN|REQ_F_IO_DRAINED)) == REQ_F_IO_DRAIN)
565 return __req_need_defer(req);
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600566
Bob Liu9d858b22019-11-13 18:06:25 +0800567 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600568}
569
570static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -0600571{
572 struct io_kiocb *req;
573
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600574 req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list);
Bob Liu9d858b22019-11-13 18:06:25 +0800575 if (req && !req_need_defer(req)) {
Jens Axboede0617e2019-04-06 21:51:27 -0600576 list_del_init(&req->list);
577 return req;
578 }
579
580 return NULL;
581}
582
Jens Axboe5262f562019-09-17 12:26:57 -0600583static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx)
584{
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600585 struct io_kiocb *req;
586
587 req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list);
Jens Axboe93bd25b2019-11-11 23:34:31 -0700588 if (req) {
589 if (req->flags & REQ_F_TIMEOUT_NOSEQ)
590 return NULL;
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800591 if (!__req_need_defer(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -0700592 list_del_init(&req->list);
593 return req;
594 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600595 }
596
597 return NULL;
Jens Axboe5262f562019-09-17 12:26:57 -0600598}
599
Jens Axboede0617e2019-04-06 21:51:27 -0600600static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700601{
Hristo Venev75b28af2019-08-26 17:23:46 +0000602 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700603
Hristo Venev75b28af2019-08-26 17:23:46 +0000604 if (ctx->cached_cq_tail != READ_ONCE(rings->cq.tail)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700605 /* order cqe stores with ring update */
Hristo Venev75b28af2019-08-26 17:23:46 +0000606 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700607
Jens Axboe2b188cc2019-01-07 10:46:33 -0700608 if (wq_has_sleeper(&ctx->cq_wait)) {
609 wake_up_interruptible(&ctx->cq_wait);
610 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
611 }
612 }
613}
614
Jens Axboed625c6e2019-12-17 19:53:05 -0700615static inline bool io_req_needs_user(struct io_kiocb *req)
Jens Axboe18d9be12019-09-10 09:13:05 -0600616{
Jens Axboed625c6e2019-12-17 19:53:05 -0700617 return !(req->opcode == IORING_OP_READ_FIXED ||
618 req->opcode == IORING_OP_WRITE_FIXED);
Jens Axboe561fb042019-10-24 07:25:42 -0600619}
620
Jens Axboe94ae5e72019-11-14 19:39:52 -0700621static inline bool io_prep_async_work(struct io_kiocb *req,
622 struct io_kiocb **link)
Jens Axboe561fb042019-10-24 07:25:42 -0600623{
624 bool do_hashed = false;
Jens Axboe54a91f32019-09-10 09:15:04 -0600625
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300626 if (req->sqe) {
Jens Axboed625c6e2019-12-17 19:53:05 -0700627 switch (req->opcode) {
Jens Axboe6cc47d12019-09-18 11:18:23 -0600628 case IORING_OP_WRITEV:
629 case IORING_OP_WRITE_FIXED:
Jens Axboe53108d42019-12-09 20:12:38 -0700630 /* only regular files should be hashed for writes */
631 if (req->flags & REQ_F_ISREG)
632 do_hashed = true;
Jens Axboe5f8fd2d32019-11-07 10:57:36 -0700633 /* fall-through */
634 case IORING_OP_READV:
635 case IORING_OP_READ_FIXED:
636 case IORING_OP_SENDMSG:
637 case IORING_OP_RECVMSG:
638 case IORING_OP_ACCEPT:
639 case IORING_OP_POLL_ADD:
Jens Axboef8e85cf2019-11-23 14:24:24 -0700640 case IORING_OP_CONNECT:
Jens Axboe5f8fd2d32019-11-07 10:57:36 -0700641 /*
642 * We know REQ_F_ISREG is not set on some of these
643 * opcodes, but this enables us to keep the check in
644 * just one place.
645 */
646 if (!(req->flags & REQ_F_ISREG))
647 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe6cc47d12019-09-18 11:18:23 -0600648 break;
649 }
Jens Axboed625c6e2019-12-17 19:53:05 -0700650 if (io_req_needs_user(req))
Jens Axboe561fb042019-10-24 07:25:42 -0600651 req->work.flags |= IO_WQ_WORK_NEEDS_USER;
Jens Axboe54a91f32019-09-10 09:15:04 -0600652 }
653
Jens Axboe94ae5e72019-11-14 19:39:52 -0700654 *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -0600655 return do_hashed;
656}
657
Jackie Liua197f662019-11-08 08:09:12 -0700658static inline void io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -0600659{
Jackie Liua197f662019-11-08 08:09:12 -0700660 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe94ae5e72019-11-14 19:39:52 -0700661 struct io_kiocb *link;
662 bool do_hashed;
663
664 do_hashed = io_prep_async_work(req, &link);
Jens Axboe561fb042019-10-24 07:25:42 -0600665
666 trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work,
667 req->flags);
668 if (!do_hashed) {
669 io_wq_enqueue(ctx->io_wq, &req->work);
670 } else {
671 io_wq_enqueue_hashed(ctx->io_wq, &req->work,
672 file_inode(req->file));
673 }
Jens Axboe94ae5e72019-11-14 19:39:52 -0700674
675 if (link)
676 io_queue_linked_timeout(link);
Jens Axboe18d9be12019-09-10 09:13:05 -0600677}
678
Jens Axboe5262f562019-09-17 12:26:57 -0600679static void io_kill_timeout(struct io_kiocb *req)
680{
681 int ret;
682
Jens Axboe2d283902019-12-04 11:08:05 -0700683 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe5262f562019-09-17 12:26:57 -0600684 if (ret != -1) {
685 atomic_inc(&req->ctx->cq_timeouts);
Jens Axboe842f9612019-10-29 12:34:10 -0600686 list_del_init(&req->list);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700687 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800688 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -0600689 }
690}
691
692static void io_kill_timeouts(struct io_ring_ctx *ctx)
693{
694 struct io_kiocb *req, *tmp;
695
696 spin_lock_irq(&ctx->completion_lock);
697 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
698 io_kill_timeout(req);
699 spin_unlock_irq(&ctx->completion_lock);
700}
701
Jens Axboede0617e2019-04-06 21:51:27 -0600702static void io_commit_cqring(struct io_ring_ctx *ctx)
703{
704 struct io_kiocb *req;
705
Jens Axboe5262f562019-09-17 12:26:57 -0600706 while ((req = io_get_timeout_req(ctx)) != NULL)
707 io_kill_timeout(req);
708
Jens Axboede0617e2019-04-06 21:51:27 -0600709 __io_commit_cqring(ctx);
710
711 while ((req = io_get_deferred_req(ctx)) != NULL) {
712 req->flags |= REQ_F_IO_DRAINED;
Jackie Liua197f662019-11-08 08:09:12 -0700713 io_queue_async_work(req);
Jens Axboede0617e2019-04-06 21:51:27 -0600714 }
715}
716
Jens Axboe2b188cc2019-01-07 10:46:33 -0700717static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
718{
Hristo Venev75b28af2019-08-26 17:23:46 +0000719 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700720 unsigned tail;
721
722 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +0200723 /*
724 * writes to the cq entry need to come after reading head; the
725 * control dependency is enough as we're using WRITE_ONCE to
726 * fill the cq entry
727 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000728 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700729 return NULL;
730
731 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +0000732 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -0700733}
734
Jens Axboe8c838782019-03-12 15:48:16 -0600735static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
736{
737 if (waitqueue_active(&ctx->wait))
738 wake_up(&ctx->wait);
739 if (waitqueue_active(&ctx->sqo_wait))
740 wake_up(&ctx->sqo_wait);
Jens Axboe9b402842019-04-11 11:45:41 -0600741 if (ctx->cq_ev_fd)
742 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -0600743}
744
Jens Axboec4a2ed72019-11-21 21:01:26 -0700745/* Returns true if there are no backlogged entries after the flush */
746static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700747{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700748 struct io_rings *rings = ctx->rings;
749 struct io_uring_cqe *cqe;
750 struct io_kiocb *req;
751 unsigned long flags;
752 LIST_HEAD(list);
753
754 if (!force) {
755 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -0700756 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700757 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
758 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -0700759 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700760 }
761
762 spin_lock_irqsave(&ctx->completion_lock, flags);
763
764 /* if force is set, the ring is going away. always drop after that */
765 if (force)
766 ctx->cq_overflow_flushed = true;
767
Jens Axboec4a2ed72019-11-21 21:01:26 -0700768 cqe = NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700769 while (!list_empty(&ctx->cq_overflow_list)) {
770 cqe = io_get_cqring(ctx);
771 if (!cqe && !force)
772 break;
773
774 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
775 list);
776 list_move(&req->list, &list);
777 if (cqe) {
778 WRITE_ONCE(cqe->user_data, req->user_data);
779 WRITE_ONCE(cqe->res, req->result);
780 WRITE_ONCE(cqe->flags, 0);
781 } else {
782 WRITE_ONCE(ctx->rings->cq_overflow,
783 atomic_inc_return(&ctx->cached_cq_overflow));
784 }
785 }
786
787 io_commit_cqring(ctx);
788 spin_unlock_irqrestore(&ctx->completion_lock, flags);
789 io_cqring_ev_posted(ctx);
790
791 while (!list_empty(&list)) {
792 req = list_first_entry(&list, struct io_kiocb, list);
793 list_del(&req->list);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800794 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700795 }
Jens Axboec4a2ed72019-11-21 21:01:26 -0700796
797 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700798}
799
Jens Axboe78e19bb2019-11-06 15:21:34 -0700800static void io_cqring_fill_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700801{
Jens Axboe78e19bb2019-11-06 15:21:34 -0700802 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700803 struct io_uring_cqe *cqe;
804
Jens Axboe78e19bb2019-11-06 15:21:34 -0700805 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -0700806
Jens Axboe2b188cc2019-01-07 10:46:33 -0700807 /*
808 * If we can't get a cq entry, userspace overflowed the
809 * submission (by quite a lot). Increment the overflow count in
810 * the ring.
811 */
812 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700813 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -0700814 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700815 WRITE_ONCE(cqe->res, res);
816 WRITE_ONCE(cqe->flags, 0);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700817 } else if (ctx->cq_overflow_flushed) {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700818 WRITE_ONCE(ctx->rings->cq_overflow,
819 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700820 } else {
821 refcount_inc(&req->refs);
822 req->result = res;
823 list_add_tail(&req->list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700824 }
825}
826
Jens Axboe78e19bb2019-11-06 15:21:34 -0700827static void io_cqring_add_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700828{
Jens Axboe78e19bb2019-11-06 15:21:34 -0700829 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700830 unsigned long flags;
831
832 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700833 io_cqring_fill_event(req, res);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700834 io_commit_cqring(ctx);
835 spin_unlock_irqrestore(&ctx->completion_lock, flags);
836
Jens Axboe8c838782019-03-12 15:48:16 -0600837 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700838}
839
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700840static inline bool io_is_fallback_req(struct io_kiocb *req)
841{
842 return req == (struct io_kiocb *)
843 ((unsigned long) req->ctx->fallback_req & ~1UL);
844}
845
846static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
847{
848 struct io_kiocb *req;
849
850 req = ctx->fallback_req;
851 if (!test_and_set_bit_lock(0, (unsigned long *) ctx->fallback_req))
852 return req;
853
854 return NULL;
855}
856
Jens Axboe2579f912019-01-09 09:10:43 -0700857static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
858 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700859{
Jens Axboefd6fab22019-03-14 16:30:06 -0600860 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700861 struct io_kiocb *req;
862
863 if (!percpu_ref_tryget(&ctx->refs))
864 return NULL;
865
Jens Axboe2579f912019-01-09 09:10:43 -0700866 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -0600867 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -0700868 if (unlikely(!req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700869 goto fallback;
Jens Axboe2579f912019-01-09 09:10:43 -0700870 } else if (!state->free_reqs) {
871 size_t sz;
872 int ret;
873
874 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -0600875 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
876
877 /*
878 * Bulk alloc is all-or-nothing. If we fail to get a batch,
879 * retry single alloc to be on the safe side.
880 */
881 if (unlikely(ret <= 0)) {
882 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
883 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700884 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -0600885 ret = 1;
886 }
Jens Axboe2579f912019-01-09 09:10:43 -0700887 state->free_reqs = ret - 1;
888 state->cur_req = 1;
889 req = state->reqs[0];
890 } else {
891 req = state->reqs[state->cur_req];
892 state->free_reqs--;
893 state->cur_req++;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700894 }
895
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700896got_it:
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700897 req->io = NULL;
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300898 req->ring_file = NULL;
Jens Axboe60c112b2019-06-21 10:20:18 -0600899 req->file = NULL;
Jens Axboe2579f912019-01-09 09:10:43 -0700900 req->ctx = ctx;
901 req->flags = 0;
Jens Axboee65ef562019-03-12 10:16:44 -0600902 /* one is dropped after submission, the other at completion */
903 refcount_set(&req->refs, 2);
Jens Axboe9e645e112019-05-10 16:07:28 -0600904 req->result = 0;
Jens Axboe561fb042019-10-24 07:25:42 -0600905 INIT_IO_WORK(&req->work, io_wq_submit_work);
Jens Axboe2579f912019-01-09 09:10:43 -0700906 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700907fallback:
908 req = io_get_fallback_req(ctx);
909 if (req)
910 goto got_it;
Pavel Begunkov6805b322019-10-08 02:18:42 +0300911 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700912 return NULL;
913}
914
Jens Axboedef596e2019-01-09 08:59:42 -0700915static void io_free_req_many(struct io_ring_ctx *ctx, void **reqs, int *nr)
916{
917 if (*nr) {
918 kmem_cache_free_bulk(req_cachep, *nr, reqs);
Pavel Begunkov6805b322019-10-08 02:18:42 +0300919 percpu_ref_put_many(&ctx->refs, *nr);
Jens Axboedef596e2019-01-09 08:59:42 -0700920 *nr = 0;
921 }
922}
923
Jens Axboe9e645e112019-05-10 16:07:28 -0600924static void __io_free_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700925{
Jens Axboefcb323c2019-10-24 12:39:47 -0600926 struct io_ring_ctx *ctx = req->ctx;
927
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700928 if (req->io)
929 kfree(req->io);
Jens Axboe09bb8392019-03-13 12:39:28 -0600930 if (req->file && !(req->flags & REQ_F_FIXED_FILE))
931 fput(req->file);
Jens Axboefcb323c2019-10-24 12:39:47 -0600932 if (req->flags & REQ_F_INFLIGHT) {
933 unsigned long flags;
934
935 spin_lock_irqsave(&ctx->inflight_lock, flags);
936 list_del(&req->inflight_entry);
937 if (waitqueue_active(&ctx->inflight_wait))
938 wake_up(&ctx->inflight_wait);
939 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
940 }
941 percpu_ref_put(&ctx->refs);
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700942 if (likely(!io_is_fallback_req(req)))
943 kmem_cache_free(req_cachep, req);
944 else
945 clear_bit_unlock(0, (unsigned long *) ctx->fallback_req);
Jens Axboee65ef562019-03-12 10:16:44 -0600946}
947
Jackie Liua197f662019-11-08 08:09:12 -0700948static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -0600949{
Jackie Liua197f662019-11-08 08:09:12 -0700950 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -0700951 int ret;
952
Jens Axboe2d283902019-12-04 11:08:05 -0700953 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe2665abf2019-11-05 12:40:47 -0700954 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -0700955 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -0700956 io_commit_cqring(ctx);
957 req->flags &= ~REQ_F_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +0800958 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -0700959 return true;
960 }
961
962 return false;
963}
964
Jens Axboeba816ad2019-09-28 11:36:45 -0600965static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -0600966{
Jens Axboe2665abf2019-11-05 12:40:47 -0700967 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -0700968 bool wake_ev = false;
Jens Axboe9e645e112019-05-10 16:07:28 -0600969
Jens Axboe4d7dd462019-11-20 13:03:52 -0700970 /* Already got next link */
971 if (req->flags & REQ_F_LINK_NEXT)
972 return;
973
Jens Axboe9e645e112019-05-10 16:07:28 -0600974 /*
975 * The list should never be empty when we are called here. But could
976 * potentially happen if the chain is messed up, check to be on the
977 * safe side.
978 */
Pavel Begunkov44932332019-12-05 16:16:35 +0300979 while (!list_empty(&req->link_list)) {
980 struct io_kiocb *nxt = list_first_entry(&req->link_list,
981 struct io_kiocb, link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700982
Pavel Begunkov44932332019-12-05 16:16:35 +0300983 if (unlikely((req->flags & REQ_F_LINK_TIMEOUT) &&
984 (nxt->flags & REQ_F_TIMEOUT))) {
985 list_del_init(&nxt->link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700986 wake_ev |= io_link_cancel_timeout(nxt);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700987 req->flags &= ~REQ_F_LINK_TIMEOUT;
988 continue;
989 }
Jens Axboe9e645e112019-05-10 16:07:28 -0600990
Pavel Begunkov44932332019-12-05 16:16:35 +0300991 list_del_init(&req->link_list);
992 if (!list_empty(&nxt->link_list))
993 nxt->flags |= REQ_F_LINK;
Pavel Begunkovb18fdf72019-11-21 23:21:02 +0300994 *nxtptr = nxt;
Jens Axboe94ae5e72019-11-14 19:39:52 -0700995 break;
Jens Axboe9e645e112019-05-10 16:07:28 -0600996 }
Jens Axboe2665abf2019-11-05 12:40:47 -0700997
Jens Axboe4d7dd462019-11-20 13:03:52 -0700998 req->flags |= REQ_F_LINK_NEXT;
Jens Axboe2665abf2019-11-05 12:40:47 -0700999 if (wake_ev)
1000 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001001}
1002
1003/*
1004 * Called if REQ_F_LINK is set, and we fail the head request
1005 */
1006static void io_fail_links(struct io_kiocb *req)
1007{
Jens Axboe2665abf2019-11-05 12:40:47 -07001008 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001009 unsigned long flags;
1010
1011 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06001012
1013 while (!list_empty(&req->link_list)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03001014 struct io_kiocb *link = list_first_entry(&req->link_list,
1015 struct io_kiocb, link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06001016
Pavel Begunkov44932332019-12-05 16:16:35 +03001017 list_del_init(&link->link_list);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001018 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001019
1020 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
Jens Axboed625c6e2019-12-17 19:53:05 -07001021 link->opcode == IORING_OP_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -07001022 io_link_cancel_timeout(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001023 } else {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001024 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe978db572019-11-14 22:39:04 -07001025 __io_double_put_req(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001026 }
Jens Axboe5d960722019-11-19 15:31:28 -07001027 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -06001028 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001029
1030 io_commit_cqring(ctx);
1031 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1032 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001033}
1034
Jens Axboe4d7dd462019-11-20 13:03:52 -07001035static void io_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -06001036{
Jens Axboe4d7dd462019-11-20 13:03:52 -07001037 if (likely(!(req->flags & REQ_F_LINK)))
Jens Axboe2665abf2019-11-05 12:40:47 -07001038 return;
Jens Axboe2665abf2019-11-05 12:40:47 -07001039
Jens Axboe9e645e112019-05-10 16:07:28 -06001040 /*
1041 * If LINK is set, we have dependent requests in this chain. If we
1042 * didn't fail this request, queue the first one up, moving any other
1043 * dependencies to the next request. In case of failure, fail the rest
1044 * of the chain.
1045 */
Jens Axboe2665abf2019-11-05 12:40:47 -07001046 if (req->flags & REQ_F_FAIL_LINK) {
1047 io_fail_links(req);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001048 } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) ==
1049 REQ_F_LINK_TIMEOUT) {
Jens Axboe2665abf2019-11-05 12:40:47 -07001050 struct io_ring_ctx *ctx = req->ctx;
1051 unsigned long flags;
1052
1053 /*
1054 * If this is a timeout link, we could be racing with the
1055 * timeout timer. Grab the completion lock for this case to
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001056 * protect against that.
Jens Axboe2665abf2019-11-05 12:40:47 -07001057 */
1058 spin_lock_irqsave(&ctx->completion_lock, flags);
1059 io_req_link_next(req, nxt);
1060 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1061 } else {
1062 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -06001063 }
Jens Axboe4d7dd462019-11-20 13:03:52 -07001064}
Jens Axboe9e645e112019-05-10 16:07:28 -06001065
Jackie Liuc69f8db2019-11-09 11:00:08 +08001066static void io_free_req(struct io_kiocb *req)
1067{
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001068 struct io_kiocb *nxt = NULL;
1069
1070 io_req_find_next(req, &nxt);
Pavel Begunkov70cf9f32019-11-21 23:21:00 +03001071 __io_free_req(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001072
1073 if (nxt)
1074 io_queue_async_work(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08001075}
1076
Jens Axboeba816ad2019-09-28 11:36:45 -06001077/*
1078 * Drop reference to request, return next in chain (if there is one) if this
1079 * was the last reference to this request.
1080 */
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001081__attribute__((nonnull))
Jackie Liuec9c02a2019-11-08 23:50:36 +08001082static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboee65ef562019-03-12 10:16:44 -06001083{
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001084 io_req_find_next(req, nxtptr);
Jens Axboe4d7dd462019-11-20 13:03:52 -07001085
Jens Axboee65ef562019-03-12 10:16:44 -06001086 if (refcount_dec_and_test(&req->refs))
Jens Axboe4d7dd462019-11-20 13:03:52 -07001087 __io_free_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001088}
1089
Jens Axboe2b188cc2019-01-07 10:46:33 -07001090static void io_put_req(struct io_kiocb *req)
1091{
Jens Axboedef596e2019-01-09 08:59:42 -07001092 if (refcount_dec_and_test(&req->refs))
1093 io_free_req(req);
1094}
1095
Jens Axboe978db572019-11-14 22:39:04 -07001096/*
1097 * Must only be used if we don't need to care about links, usually from
1098 * within the completion handling itself.
1099 */
1100static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06001101{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001102 /* drop both submit and complete references */
1103 if (refcount_sub_and_test(2, &req->refs))
1104 __io_free_req(req);
1105}
1106
Jens Axboe978db572019-11-14 22:39:04 -07001107static void io_double_put_req(struct io_kiocb *req)
1108{
1109 /* drop both submit and complete references */
1110 if (refcount_sub_and_test(2, &req->refs))
1111 io_free_req(req);
1112}
1113
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001114static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06001115{
Jens Axboe84f97dc2019-11-06 11:27:53 -07001116 struct io_rings *rings = ctx->rings;
1117
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001118 /*
1119 * noflush == true is from the waitqueue handler, just ensure we wake
1120 * up the task, and the next invocation will flush the entries. We
1121 * cannot safely to it from here.
1122 */
1123 if (noflush && !list_empty(&ctx->cq_overflow_list))
1124 return -1U;
1125
1126 io_cqring_overflow_flush(ctx, false);
1127
Jens Axboea3a0e432019-08-20 11:03:11 -06001128 /* See comment at the top of this file */
1129 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00001130 return READ_ONCE(rings->cq.tail) - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06001131}
1132
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03001133static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1134{
1135 struct io_rings *rings = ctx->rings;
1136
1137 /* make sure SQ entry isn't read before tail */
1138 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1139}
1140
Jens Axboedef596e2019-01-09 08:59:42 -07001141/*
1142 * Find and free completed poll iocbs
1143 */
1144static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
1145 struct list_head *done)
1146{
1147 void *reqs[IO_IOPOLL_BATCH];
1148 struct io_kiocb *req;
Jens Axboe09bb8392019-03-13 12:39:28 -06001149 int to_free;
Jens Axboedef596e2019-01-09 08:59:42 -07001150
Jens Axboe09bb8392019-03-13 12:39:28 -06001151 to_free = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001152 while (!list_empty(done)) {
1153 req = list_first_entry(done, struct io_kiocb, list);
1154 list_del(&req->list);
1155
Jens Axboe78e19bb2019-11-06 15:21:34 -07001156 io_cqring_fill_event(req, req->result);
Jens Axboedef596e2019-01-09 08:59:42 -07001157 (*nr_events)++;
1158
Jens Axboe09bb8392019-03-13 12:39:28 -06001159 if (refcount_dec_and_test(&req->refs)) {
1160 /* If we're not using fixed files, we have to pair the
1161 * completion part with the file put. Use regular
1162 * completions for those, only batch free for fixed
Jens Axboe9e645e112019-05-10 16:07:28 -06001163 * file and non-linked commands.
Jens Axboe09bb8392019-03-13 12:39:28 -06001164 */
Jens Axboe1a6b74f2019-12-02 10:33:15 -07001165 if (((req->flags & (REQ_F_FIXED_FILE|REQ_F_LINK)) ==
1166 REQ_F_FIXED_FILE) && !io_is_fallback_req(req) &&
1167 !req->io) {
Jens Axboe09bb8392019-03-13 12:39:28 -06001168 reqs[to_free++] = req;
1169 if (to_free == ARRAY_SIZE(reqs))
1170 io_free_req_many(ctx, reqs, &to_free);
Jens Axboe6b063142019-01-10 22:13:58 -07001171 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001172 io_free_req(req);
Jens Axboe6b063142019-01-10 22:13:58 -07001173 }
Jens Axboe9a56a232019-01-09 09:06:50 -07001174 }
Jens Axboedef596e2019-01-09 08:59:42 -07001175 }
Jens Axboedef596e2019-01-09 08:59:42 -07001176
Jens Axboe09bb8392019-03-13 12:39:28 -06001177 io_commit_cqring(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07001178 io_free_req_many(ctx, reqs, &to_free);
1179}
1180
1181static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1182 long min)
1183{
1184 struct io_kiocb *req, *tmp;
1185 LIST_HEAD(done);
1186 bool spin;
1187 int ret;
1188
1189 /*
1190 * Only spin for completions if we don't have multiple devices hanging
1191 * off our complete list, and we're under the requested amount.
1192 */
1193 spin = !ctx->poll_multi_file && *nr_events < min;
1194
1195 ret = 0;
1196 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
Jens Axboe9adbd452019-12-20 08:45:55 -07001197 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07001198
1199 /*
1200 * Move completed entries to our local list. If we find a
1201 * request that requires polling, break out and complete
1202 * the done list first, if we have entries there.
1203 */
1204 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
1205 list_move_tail(&req->list, &done);
1206 continue;
1207 }
1208 if (!list_empty(&done))
1209 break;
1210
1211 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1212 if (ret < 0)
1213 break;
1214
1215 if (ret && spin)
1216 spin = false;
1217 ret = 0;
1218 }
1219
1220 if (!list_empty(&done))
1221 io_iopoll_complete(ctx, nr_events, &done);
1222
1223 return ret;
1224}
1225
1226/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08001227 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07001228 * non-spinning poll check - we'll still enter the driver poll loop, but only
1229 * as a non-spinning completion check.
1230 */
1231static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
1232 long min)
1233{
Jens Axboe08f54392019-08-21 22:19:11 -06001234 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07001235 int ret;
1236
1237 ret = io_do_iopoll(ctx, nr_events, min);
1238 if (ret < 0)
1239 return ret;
1240 if (!min || *nr_events >= min)
1241 return 0;
1242 }
1243
1244 return 1;
1245}
1246
1247/*
1248 * We can't just wait for polled events to come to us, we have to actively
1249 * find and complete them.
1250 */
1251static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
1252{
1253 if (!(ctx->flags & IORING_SETUP_IOPOLL))
1254 return;
1255
1256 mutex_lock(&ctx->uring_lock);
1257 while (!list_empty(&ctx->poll_list)) {
1258 unsigned int nr_events = 0;
1259
1260 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -06001261
1262 /*
1263 * Ensure we allow local-to-the-cpu processing to take place,
1264 * in this case we need to ensure that we reap all events.
1265 */
1266 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -07001267 }
1268 mutex_unlock(&ctx->uring_lock);
1269}
1270
Jens Axboe2b2ed972019-10-25 10:06:15 -06001271static int __io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1272 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07001273{
Jens Axboe2b2ed972019-10-25 10:06:15 -06001274 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001275
1276 do {
1277 int tmin = 0;
1278
Jens Axboe500f9fb2019-08-19 12:15:59 -06001279 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06001280 * Don't enter poll loop if we already have events pending.
1281 * If we do, we can potentially be spinning for commands that
1282 * already triggered a CQE (eg in error).
1283 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001284 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06001285 break;
1286
1287 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06001288 * If a submit got punted to a workqueue, we can have the
1289 * application entering polling for a command before it gets
1290 * issued. That app will hold the uring_lock for the duration
1291 * of the poll right here, so we need to take a breather every
1292 * now and then to ensure that the issue has a chance to add
1293 * the poll to the issued list. Otherwise we can spin here
1294 * forever, while the workqueue is stuck trying to acquire the
1295 * very same mutex.
1296 */
1297 if (!(++iters & 7)) {
1298 mutex_unlock(&ctx->uring_lock);
1299 mutex_lock(&ctx->uring_lock);
1300 }
1301
Jens Axboedef596e2019-01-09 08:59:42 -07001302 if (*nr_events < min)
1303 tmin = min - *nr_events;
1304
1305 ret = io_iopoll_getevents(ctx, nr_events, tmin);
1306 if (ret <= 0)
1307 break;
1308 ret = 0;
1309 } while (min && !*nr_events && !need_resched());
1310
Jens Axboe2b2ed972019-10-25 10:06:15 -06001311 return ret;
1312}
1313
1314static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1315 long min)
1316{
1317 int ret;
1318
1319 /*
1320 * We disallow the app entering submit/complete with polling, but we
1321 * still need to lock the ring to prevent racing with polled issue
1322 * that got punted to a workqueue.
1323 */
1324 mutex_lock(&ctx->uring_lock);
1325 ret = __io_iopoll_check(ctx, nr_events, min);
Jens Axboe500f9fb2019-08-19 12:15:59 -06001326 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001327 return ret;
1328}
1329
Jens Axboe491381ce2019-10-17 09:20:46 -06001330static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001331{
Jens Axboe491381ce2019-10-17 09:20:46 -06001332 /*
1333 * Tell lockdep we inherited freeze protection from submission
1334 * thread.
1335 */
1336 if (req->flags & REQ_F_ISREG) {
1337 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001338
Jens Axboe491381ce2019-10-17 09:20:46 -06001339 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001340 }
Jens Axboe491381ce2019-10-17 09:20:46 -06001341 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001342}
1343
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001344static inline void req_set_fail_links(struct io_kiocb *req)
1345{
1346 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1347 req->flags |= REQ_F_FAIL_LINK;
1348}
1349
Jens Axboeba816ad2019-09-28 11:36:45 -06001350static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001351{
Jens Axboe9adbd452019-12-20 08:45:55 -07001352 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001353
Jens Axboe491381ce2019-10-17 09:20:46 -06001354 if (kiocb->ki_flags & IOCB_WRITE)
1355 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001356
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001357 if (res != req->result)
1358 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001359 io_cqring_add_event(req, res);
Jens Axboeba816ad2019-09-28 11:36:45 -06001360}
1361
1362static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1363{
Jens Axboe9adbd452019-12-20 08:45:55 -07001364 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06001365
1366 io_complete_rw_common(kiocb, res);
Jens Axboee65ef562019-03-12 10:16:44 -06001367 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001368}
1369
Jens Axboeba816ad2019-09-28 11:36:45 -06001370static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res)
1371{
Jens Axboe9adbd452019-12-20 08:45:55 -07001372 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001373 struct io_kiocb *nxt = NULL;
Jens Axboeba816ad2019-09-28 11:36:45 -06001374
1375 io_complete_rw_common(kiocb, res);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001376 io_put_req_find_next(req, &nxt);
1377
1378 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001379}
1380
Jens Axboedef596e2019-01-09 08:59:42 -07001381static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1382{
Jens Axboe9adbd452019-12-20 08:45:55 -07001383 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07001384
Jens Axboe491381ce2019-10-17 09:20:46 -06001385 if (kiocb->ki_flags & IOCB_WRITE)
1386 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001387
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001388 if (res != req->result)
1389 req_set_fail_links(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06001390 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -07001391 if (res != -EAGAIN)
1392 req->flags |= REQ_F_IOPOLL_COMPLETED;
1393}
1394
1395/*
1396 * After the iocb has been issued, it's safe to be found on the poll list.
1397 * Adding the kiocb to the list AFTER submission ensures that we don't
1398 * find it from a io_iopoll_getevents() thread before the issuer is done
1399 * accessing the kiocb cookie.
1400 */
1401static void io_iopoll_req_issued(struct io_kiocb *req)
1402{
1403 struct io_ring_ctx *ctx = req->ctx;
1404
1405 /*
1406 * Track whether we have multiple files in our lists. This will impact
1407 * how we do polling eventually, not spinning if we're on potentially
1408 * different devices.
1409 */
1410 if (list_empty(&ctx->poll_list)) {
1411 ctx->poll_multi_file = false;
1412 } else if (!ctx->poll_multi_file) {
1413 struct io_kiocb *list_req;
1414
1415 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1416 list);
Jens Axboe9adbd452019-12-20 08:45:55 -07001417 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07001418 ctx->poll_multi_file = true;
1419 }
1420
1421 /*
1422 * For fast devices, IO may have already completed. If it has, add
1423 * it to the front so we find it first.
1424 */
1425 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1426 list_add(&req->list, &ctx->poll_list);
1427 else
1428 list_add_tail(&req->list, &ctx->poll_list);
1429}
1430
Jens Axboe3d6770f2019-04-13 11:50:54 -06001431static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07001432{
Jens Axboe3d6770f2019-04-13 11:50:54 -06001433 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -07001434 int diff = state->has_refs - state->used_refs;
1435
1436 if (diff)
1437 fput_many(state->file, diff);
1438 state->file = NULL;
1439 }
1440}
1441
1442/*
1443 * Get as many references to a file as we have IOs left in this submission,
1444 * assuming most submissions are for one file, or at least that each file
1445 * has more than one submission.
1446 */
1447static struct file *io_file_get(struct io_submit_state *state, int fd)
1448{
1449 if (!state)
1450 return fget(fd);
1451
1452 if (state->file) {
1453 if (state->fd == fd) {
1454 state->used_refs++;
1455 state->ios_left--;
1456 return state->file;
1457 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06001458 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07001459 }
1460 state->file = fget_many(fd, state->ios_left);
1461 if (!state->file)
1462 return NULL;
1463
1464 state->fd = fd;
1465 state->has_refs = state->ios_left;
1466 state->used_refs = 1;
1467 state->ios_left--;
1468 return state->file;
1469}
1470
Jens Axboe2b188cc2019-01-07 10:46:33 -07001471/*
1472 * If we tracked the file through the SCM inflight mechanism, we could support
1473 * any file. For now, just ensure that anything potentially problematic is done
1474 * inline.
1475 */
1476static bool io_file_supports_async(struct file *file)
1477{
1478 umode_t mode = file_inode(file)->i_mode;
1479
Jens Axboe10d59342019-12-09 20:16:22 -07001480 if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07001481 return true;
1482 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
1483 return true;
1484
1485 return false;
1486}
1487
Pavel Begunkov267bc902019-11-07 01:41:08 +03001488static int io_prep_rw(struct io_kiocb *req, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001489{
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001490 const struct io_uring_sqe *sqe = req->sqe;
Jens Axboedef596e2019-01-09 08:59:42 -07001491 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07001492 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06001493 unsigned ioprio;
1494 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001495
Jens Axboe09bb8392019-03-13 12:39:28 -06001496 if (!req->file)
1497 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001498
Jens Axboe491381ce2019-10-17 09:20:46 -06001499 if (S_ISREG(file_inode(req->file)->i_mode))
1500 req->flags |= REQ_F_ISREG;
1501
Jens Axboe2b188cc2019-01-07 10:46:33 -07001502 kiocb->ki_pos = READ_ONCE(sqe->off);
1503 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
1504 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
1505
1506 ioprio = READ_ONCE(sqe->ioprio);
1507 if (ioprio) {
1508 ret = ioprio_check_cap(ioprio);
1509 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06001510 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001511
1512 kiocb->ki_ioprio = ioprio;
1513 } else
1514 kiocb->ki_ioprio = get_current_ioprio();
1515
1516 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
1517 if (unlikely(ret))
Jens Axboe09bb8392019-03-13 12:39:28 -06001518 return ret;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001519
1520 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06001521 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
1522 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02001523 req->flags |= REQ_F_NOWAIT;
1524
1525 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001526 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001527
Jens Axboedef596e2019-01-09 08:59:42 -07001528 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07001529 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
1530 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06001531 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001532
Jens Axboedef596e2019-01-09 08:59:42 -07001533 kiocb->ki_flags |= IOCB_HIPRI;
1534 kiocb->ki_complete = io_complete_rw_iopoll;
Jens Axboe6873e0b2019-10-30 13:53:09 -06001535 req->result = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001536 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001537 if (kiocb->ki_flags & IOCB_HIPRI)
1538 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07001539 kiocb->ki_complete = io_complete_rw;
1540 }
Jens Axboe9adbd452019-12-20 08:45:55 -07001541
1542 req->rw.addr = READ_ONCE(req->sqe->addr);
1543 req->rw.len = READ_ONCE(req->sqe->len);
1544 /* we own ->private, reuse it for the buffer index */
1545 req->rw.kiocb.private = (void *) (unsigned long)
1546 READ_ONCE(req->sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001547 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001548}
1549
1550static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
1551{
1552 switch (ret) {
1553 case -EIOCBQUEUED:
1554 break;
1555 case -ERESTARTSYS:
1556 case -ERESTARTNOINTR:
1557 case -ERESTARTNOHAND:
1558 case -ERESTART_RESTARTBLOCK:
1559 /*
1560 * We can't just restart the syscall, since previously
1561 * submitted sqes may already be in progress. Just fail this
1562 * IO with EINTR.
1563 */
1564 ret = -EINTR;
1565 /* fall through */
1566 default:
1567 kiocb->ki_complete(kiocb, ret, 0);
1568 }
1569}
1570
Jens Axboeba816ad2019-09-28 11:36:45 -06001571static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt,
1572 bool in_async)
1573{
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001574 if (in_async && ret >= 0 && kiocb->ki_complete == io_complete_rw)
Jens Axboeba816ad2019-09-28 11:36:45 -06001575 *nxt = __io_complete_rw(kiocb, ret);
1576 else
1577 io_rw_done(kiocb, ret);
1578}
1579
Jens Axboe9adbd452019-12-20 08:45:55 -07001580static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03001581 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07001582{
Jens Axboe9adbd452019-12-20 08:45:55 -07001583 struct io_ring_ctx *ctx = req->ctx;
1584 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001585 struct io_mapped_ubuf *imu;
1586 unsigned index, buf_index;
1587 size_t offset;
1588 u64 buf_addr;
1589
1590 /* attempt to use fixed buffers without having provided iovecs */
1591 if (unlikely(!ctx->user_bufs))
1592 return -EFAULT;
1593
Jens Axboe9adbd452019-12-20 08:45:55 -07001594 buf_index = (unsigned long) req->rw.kiocb.private;
Jens Axboeedafcce2019-01-09 09:16:05 -07001595 if (unlikely(buf_index >= ctx->nr_user_bufs))
1596 return -EFAULT;
1597
1598 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
1599 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07001600 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07001601
1602 /* overflow */
1603 if (buf_addr + len < buf_addr)
1604 return -EFAULT;
1605 /* not inside the mapped region */
1606 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
1607 return -EFAULT;
1608
1609 /*
1610 * May not be a start of buffer, set size appropriately
1611 * and advance us to the beginning.
1612 */
1613 offset = buf_addr - imu->ubuf;
1614 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06001615
1616 if (offset) {
1617 /*
1618 * Don't use iov_iter_advance() here, as it's really slow for
1619 * using the latter parts of a big fixed buffer - it iterates
1620 * over each segment manually. We can cheat a bit here, because
1621 * we know that:
1622 *
1623 * 1) it's a BVEC iter, we set it up
1624 * 2) all bvecs are PAGE_SIZE in size, except potentially the
1625 * first and last bvec
1626 *
1627 * So just find our index, and adjust the iterator afterwards.
1628 * If the offset is within the first bvec (or the whole first
1629 * bvec, just use iov_iter_advance(). This makes it easier
1630 * since we can just skip the first segment, which may not
1631 * be PAGE_SIZE aligned.
1632 */
1633 const struct bio_vec *bvec = imu->bvec;
1634
1635 if (offset <= bvec->bv_len) {
1636 iov_iter_advance(iter, offset);
1637 } else {
1638 unsigned long seg_skip;
1639
1640 /* skip first vec */
1641 offset -= bvec->bv_len;
1642 seg_skip = 1 + (offset >> PAGE_SHIFT);
1643
1644 iter->bvec = bvec + seg_skip;
1645 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02001646 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001647 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001648 }
1649 }
1650
Jens Axboe5e559562019-11-13 16:12:46 -07001651 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001652}
1653
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001654static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
1655 struct iovec **iovec, struct iov_iter *iter)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001656{
Jens Axboe9adbd452019-12-20 08:45:55 -07001657 void __user *buf = u64_to_user_ptr(req->rw.addr);
1658 size_t sqe_len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001659 u8 opcode;
1660
Jens Axboed625c6e2019-12-17 19:53:05 -07001661 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03001662 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07001663 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07001664 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07001665 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001666
Jens Axboe9adbd452019-12-20 08:45:55 -07001667 /* buffer index only valid with fixed read/write */
1668 if (req->rw.kiocb.private)
1669 return -EINVAL;
1670
Jens Axboef67676d2019-12-02 11:03:47 -07001671 if (req->io) {
1672 struct io_async_rw *iorw = &req->io->rw;
1673
1674 *iovec = iorw->iov;
1675 iov_iter_init(iter, rw, *iovec, iorw->nr_segs, iorw->size);
1676 if (iorw->iov == iorw->fast_iov)
1677 *iovec = NULL;
1678 return iorw->size;
1679 }
1680
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001681 if (!req->has_user)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001682 return -EFAULT;
1683
1684#ifdef CONFIG_COMPAT
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001685 if (req->ctx->compat)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001686 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
1687 iovec, iter);
1688#endif
1689
1690 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
1691}
1692
Jens Axboe32960612019-09-23 11:05:34 -06001693/*
1694 * For files that don't have ->read_iter() and ->write_iter(), handle them
1695 * by looping over ->read() or ->write() manually.
1696 */
1697static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
1698 struct iov_iter *iter)
1699{
1700 ssize_t ret = 0;
1701
1702 /*
1703 * Don't support polled IO through this interface, and we can't
1704 * support non-blocking either. For the latter, this just causes
1705 * the kiocb to be handled from an async context.
1706 */
1707 if (kiocb->ki_flags & IOCB_HIPRI)
1708 return -EOPNOTSUPP;
1709 if (kiocb->ki_flags & IOCB_NOWAIT)
1710 return -EAGAIN;
1711
1712 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03001713 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06001714 ssize_t nr;
1715
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03001716 if (!iov_iter_is_bvec(iter)) {
1717 iovec = iov_iter_iovec(iter);
1718 } else {
1719 /* fixed buffers import bvec */
1720 iovec.iov_base = kmap(iter->bvec->bv_page)
1721 + iter->iov_offset;
1722 iovec.iov_len = min(iter->count,
1723 iter->bvec->bv_len - iter->iov_offset);
1724 }
1725
Jens Axboe32960612019-09-23 11:05:34 -06001726 if (rw == READ) {
1727 nr = file->f_op->read(file, iovec.iov_base,
1728 iovec.iov_len, &kiocb->ki_pos);
1729 } else {
1730 nr = file->f_op->write(file, iovec.iov_base,
1731 iovec.iov_len, &kiocb->ki_pos);
1732 }
1733
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03001734 if (iov_iter_is_bvec(iter))
1735 kunmap(iter->bvec->bv_page);
1736
Jens Axboe32960612019-09-23 11:05:34 -06001737 if (nr < 0) {
1738 if (!ret)
1739 ret = nr;
1740 break;
1741 }
1742 ret += nr;
1743 if (nr != iovec.iov_len)
1744 break;
1745 iov_iter_advance(iter, nr);
1746 }
1747
1748 return ret;
1749}
1750
Jens Axboeb7bb4f72019-12-15 22:13:43 -07001751static void io_req_map_rw(struct io_kiocb *req, ssize_t io_size,
Jens Axboef67676d2019-12-02 11:03:47 -07001752 struct iovec *iovec, struct iovec *fast_iov,
1753 struct iov_iter *iter)
1754{
1755 req->io->rw.nr_segs = iter->nr_segs;
1756 req->io->rw.size = io_size;
1757 req->io->rw.iov = iovec;
1758 if (!req->io->rw.iov) {
1759 req->io->rw.iov = req->io->rw.fast_iov;
1760 memcpy(req->io->rw.iov, fast_iov,
1761 sizeof(struct iovec) * iter->nr_segs);
1762 }
1763}
1764
Jens Axboeb7bb4f72019-12-15 22:13:43 -07001765static int io_alloc_async_ctx(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07001766{
1767 req->io = kmalloc(sizeof(*req->io), GFP_KERNEL);
1768 if (req->io) {
Jens Axboef67676d2019-12-02 11:03:47 -07001769 memcpy(&req->io->sqe, req->sqe, sizeof(req->io->sqe));
1770 req->sqe = &req->io->sqe;
1771 return 0;
1772 }
1773
Jens Axboeb7bb4f72019-12-15 22:13:43 -07001774 return 1;
1775}
1776
1777static void io_rw_async(struct io_wq_work **workptr)
1778{
1779 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
1780 struct iovec *iov = NULL;
1781
1782 if (req->io->rw.iov != req->io->rw.fast_iov)
1783 iov = req->io->rw.iov;
1784 io_wq_submit_work(workptr);
1785 kfree(iov);
1786}
1787
1788static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size,
1789 struct iovec *iovec, struct iovec *fast_iov,
1790 struct iov_iter *iter)
1791{
1792 if (!req->io && io_alloc_async_ctx(req))
1793 return -ENOMEM;
1794
1795 io_req_map_rw(req, io_size, iovec, fast_iov, iter);
1796 req->work.func = io_rw_async;
1797 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07001798}
1799
1800static int io_read_prep(struct io_kiocb *req, struct iovec **iovec,
1801 struct iov_iter *iter, bool force_nonblock)
1802{
1803 ssize_t ret;
1804
1805 ret = io_prep_rw(req, force_nonblock);
1806 if (ret)
1807 return ret;
1808
1809 if (unlikely(!(req->file->f_mode & FMODE_READ)))
1810 return -EBADF;
1811
1812 return io_import_iovec(READ, req, iovec, iter);
1813}
1814
Pavel Begunkov267bc902019-11-07 01:41:08 +03001815static int io_read(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001816 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001817{
1818 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07001819 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001820 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07001821 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07001822 ssize_t io_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001823
Jens Axboef67676d2019-12-02 11:03:47 -07001824 if (!req->io) {
1825 ret = io_read_prep(req, &iovec, &iter, force_nonblock);
1826 if (ret < 0)
1827 return ret;
1828 } else {
1829 ret = io_import_iovec(READ, req, &iovec, &iter);
1830 if (ret < 0)
1831 return ret;
1832 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001833
Jens Axboefd6c2e42019-12-18 12:19:41 -07001834 /* Ensure we clear previously set non-block flag */
1835 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07001836 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07001837
Jens Axboef67676d2019-12-02 11:03:47 -07001838 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06001839 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07001840 req->result = io_size;
1841
1842 /*
1843 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
1844 * we know to async punt it even if it was opened O_NONBLOCK
1845 */
Jens Axboe9adbd452019-12-20 08:45:55 -07001846 if (force_nonblock && !io_file_supports_async(req->file)) {
Jens Axboef67676d2019-12-02 11:03:47 -07001847 req->flags |= REQ_F_MUST_PUNT;
1848 goto copy_iov;
1849 }
Jens Axboe9e645e112019-05-10 16:07:28 -06001850
Jens Axboe31b51512019-01-18 22:56:34 -07001851 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07001852 ret = rw_verify_area(READ, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001853 if (!ret) {
1854 ssize_t ret2;
1855
Jens Axboe9adbd452019-12-20 08:45:55 -07001856 if (req->file->f_op->read_iter)
1857 ret2 = call_read_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06001858 else
Jens Axboe9adbd452019-12-20 08:45:55 -07001859 ret2 = loop_rw_iter(READ, req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06001860
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001861 /*
1862 * In case of a short read, punt to async. This can happen
1863 * if we have data partially cached. Alternatively we can
1864 * return the short read, in which case the application will
1865 * need to issue another SQE and wait for it. That SQE will
1866 * need async punt anyway, so it's more efficient to do it
1867 * here.
1868 */
Jens Axboe491381ce2019-10-17 09:20:46 -06001869 if (force_nonblock && !(req->flags & REQ_F_NOWAIT) &&
1870 (req->flags & REQ_F_ISREG) &&
Jens Axboef67676d2019-12-02 11:03:47 -07001871 ret2 > 0 && ret2 < io_size)
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001872 ret2 = -EAGAIN;
1873 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboef67676d2019-12-02 11:03:47 -07001874 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001875 kiocb_done(kiocb, ret2, nxt, req->in_async);
Jens Axboef67676d2019-12-02 11:03:47 -07001876 } else {
1877copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07001878 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07001879 inline_vecs, &iter);
1880 if (ret)
1881 goto out_free;
1882 return -EAGAIN;
1883 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001884 }
Jens Axboef67676d2019-12-02 11:03:47 -07001885out_free:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07001886 if (!io_wq_current_is_worker())
1887 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001888 return ret;
1889}
1890
Jens Axboef67676d2019-12-02 11:03:47 -07001891static int io_write_prep(struct io_kiocb *req, struct iovec **iovec,
1892 struct iov_iter *iter, bool force_nonblock)
1893{
1894 ssize_t ret;
1895
1896 ret = io_prep_rw(req, force_nonblock);
1897 if (ret)
1898 return ret;
1899
1900 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
1901 return -EBADF;
1902
1903 return io_import_iovec(WRITE, req, iovec, iter);
1904}
1905
Pavel Begunkov267bc902019-11-07 01:41:08 +03001906static int io_write(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001907 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001908{
1909 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07001910 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001911 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07001912 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07001913 ssize_t ret, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001914
Jens Axboef67676d2019-12-02 11:03:47 -07001915 if (!req->io) {
1916 ret = io_write_prep(req, &iovec, &iter, force_nonblock);
1917 if (ret < 0)
1918 return ret;
1919 } else {
1920 ret = io_import_iovec(WRITE, req, &iovec, &iter);
1921 if (ret < 0)
1922 return ret;
1923 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001924
Jens Axboefd6c2e42019-12-18 12:19:41 -07001925 /* Ensure we clear previously set non-block flag */
1926 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07001927 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07001928
Jens Axboef67676d2019-12-02 11:03:47 -07001929 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06001930 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07001931 req->result = io_size;
1932
1933 /*
1934 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
1935 * we know to async punt it even if it was opened O_NONBLOCK
1936 */
1937 if (force_nonblock && !io_file_supports_async(req->file)) {
1938 req->flags |= REQ_F_MUST_PUNT;
1939 goto copy_iov;
1940 }
1941
Jens Axboe10d59342019-12-09 20:16:22 -07001942 /* file path doesn't support NOWAIT for non-direct_IO */
1943 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
1944 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07001945 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06001946
Jens Axboe31b51512019-01-18 22:56:34 -07001947 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07001948 ret = rw_verify_area(WRITE, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001949 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01001950 ssize_t ret2;
1951
Jens Axboe2b188cc2019-01-07 10:46:33 -07001952 /*
1953 * Open-code file_start_write here to grab freeze protection,
1954 * which will be released by another thread in
1955 * io_complete_rw(). Fool lockdep by telling it the lock got
1956 * released so that it doesn't complain about the held lock when
1957 * we return to userspace.
1958 */
Jens Axboe491381ce2019-10-17 09:20:46 -06001959 if (req->flags & REQ_F_ISREG) {
Jens Axboe9adbd452019-12-20 08:45:55 -07001960 __sb_start_write(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07001961 SB_FREEZE_WRITE, true);
Jens Axboe9adbd452019-12-20 08:45:55 -07001962 __sb_writers_release(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07001963 SB_FREEZE_WRITE);
1964 }
1965 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01001966
Jens Axboe9adbd452019-12-20 08:45:55 -07001967 if (req->file->f_op->write_iter)
1968 ret2 = call_write_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06001969 else
Jens Axboe9adbd452019-12-20 08:45:55 -07001970 ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter);
Jens Axboef67676d2019-12-02 11:03:47 -07001971 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001972 kiocb_done(kiocb, ret2, nxt, req->in_async);
Jens Axboef67676d2019-12-02 11:03:47 -07001973 } else {
1974copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07001975 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07001976 inline_vecs, &iter);
1977 if (ret)
1978 goto out_free;
1979 return -EAGAIN;
1980 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001981 }
Jens Axboe31b51512019-01-18 22:56:34 -07001982out_free:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07001983 if (!io_wq_current_is_worker())
1984 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001985 return ret;
1986}
1987
1988/*
1989 * IORING_OP_NOP just posts a completion event, nothing else.
1990 */
Jens Axboe78e19bb2019-11-06 15:21:34 -07001991static int io_nop(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001992{
1993 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001994
Jens Axboedef596e2019-01-09 08:59:42 -07001995 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
1996 return -EINVAL;
1997
Jens Axboe78e19bb2019-11-06 15:21:34 -07001998 io_cqring_add_event(req, 0);
Jens Axboee65ef562019-03-12 10:16:44 -06001999 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002000 return 0;
2001}
2002
Jens Axboefc4df992019-12-10 14:38:45 -07002003static int io_prep_fsync(struct io_kiocb *req)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002004{
Jens Axboefc4df992019-12-10 14:38:45 -07002005 const struct io_uring_sqe *sqe = req->sqe;
Jens Axboe6b063142019-01-10 22:13:58 -07002006 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002007
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002008 if (req->flags & REQ_F_PREPPED)
2009 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06002010 if (!req->file)
2011 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002012
Jens Axboe6b063142019-01-10 22:13:58 -07002013 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07002014 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07002015 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002016 return -EINVAL;
2017
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002018 req->sync.flags = READ_ONCE(sqe->fsync_flags);
2019 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
2020 return -EINVAL;
2021
2022 req->sync.off = READ_ONCE(sqe->off);
2023 req->sync.len = READ_ONCE(sqe->len);
2024 req->flags |= REQ_F_PREPPED;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002025 return 0;
2026}
2027
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002028static bool io_req_cancelled(struct io_kiocb *req)
2029{
2030 if (req->work.flags & IO_WQ_WORK_CANCEL) {
2031 req_set_fail_links(req);
2032 io_cqring_add_event(req, -ECANCELED);
2033 io_put_req(req);
2034 return true;
2035 }
2036
2037 return false;
2038}
2039
2040static void io_fsync_finish(struct io_wq_work **workptr)
2041{
2042 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2043 loff_t end = req->sync.off + req->sync.len;
2044 struct io_kiocb *nxt = NULL;
2045 int ret;
2046
2047 if (io_req_cancelled(req))
2048 return;
2049
Jens Axboe9adbd452019-12-20 08:45:55 -07002050 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002051 end > 0 ? end : LLONG_MAX,
2052 req->sync.flags & IORING_FSYNC_DATASYNC);
2053 if (ret < 0)
2054 req_set_fail_links(req);
2055 io_cqring_add_event(req, ret);
2056 io_put_req_find_next(req, &nxt);
2057 if (nxt)
2058 *workptr = &nxt->work;
2059}
2060
Jens Axboefc4df992019-12-10 14:38:45 -07002061static int io_fsync(struct io_kiocb *req, struct io_kiocb **nxt,
2062 bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002063{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002064 struct io_wq_work *work, *old_work;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002065 int ret;
2066
Jens Axboefc4df992019-12-10 14:38:45 -07002067 ret = io_prep_fsync(req);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002068 if (ret)
2069 return ret;
2070
2071 /* fsync always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002072 if (force_nonblock) {
2073 io_put_req(req);
2074 req->work.func = io_fsync_finish;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002075 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002076 }
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002077
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002078 work = old_work = &req->work;
2079 io_fsync_finish(&work);
2080 if (work && work != old_work)
2081 *nxt = container_of(work, struct io_kiocb, work);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002082 return 0;
2083}
2084
Jens Axboefc4df992019-12-10 14:38:45 -07002085static int io_prep_sfr(struct io_kiocb *req)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002086{
Jens Axboefc4df992019-12-10 14:38:45 -07002087 const struct io_uring_sqe *sqe = req->sqe;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002088 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002089
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002090 if (req->flags & REQ_F_PREPPED)
2091 return 0;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002092 if (!req->file)
2093 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002094
2095 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2096 return -EINVAL;
2097 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
2098 return -EINVAL;
2099
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002100 req->sync.off = READ_ONCE(sqe->off);
2101 req->sync.len = READ_ONCE(sqe->len);
2102 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
2103 req->flags |= REQ_F_PREPPED;
2104 return 0;
2105}
2106
2107static void io_sync_file_range_finish(struct io_wq_work **workptr)
2108{
2109 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2110 struct io_kiocb *nxt = NULL;
2111 int ret;
2112
2113 if (io_req_cancelled(req))
2114 return;
2115
Jens Axboe9adbd452019-12-20 08:45:55 -07002116 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002117 req->sync.flags);
2118 if (ret < 0)
2119 req_set_fail_links(req);
2120 io_cqring_add_event(req, ret);
2121 io_put_req_find_next(req, &nxt);
2122 if (nxt)
2123 *workptr = &nxt->work;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002124}
2125
Jens Axboefc4df992019-12-10 14:38:45 -07002126static int io_sync_file_range(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002127 bool force_nonblock)
2128{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002129 struct io_wq_work *work, *old_work;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002130 int ret;
2131
Jens Axboefc4df992019-12-10 14:38:45 -07002132 ret = io_prep_sfr(req);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002133 if (ret)
2134 return ret;
2135
2136 /* sync_file_range always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002137 if (force_nonblock) {
2138 io_put_req(req);
2139 req->work.func = io_sync_file_range_finish;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002140 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002141 }
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002142
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002143 work = old_work = &req->work;
2144 io_sync_file_range_finish(&work);
2145 if (work && work != old_work)
2146 *nxt = container_of(work, struct io_kiocb, work);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002147 return 0;
2148}
2149
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002150#if defined(CONFIG_NET)
2151static void io_sendrecv_async(struct io_wq_work **workptr)
2152{
2153 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2154 struct iovec *iov = NULL;
2155
2156 if (req->io->rw.iov != req->io->rw.fast_iov)
2157 iov = req->io->msg.iov;
2158 io_wq_submit_work(workptr);
2159 kfree(iov);
2160}
2161#endif
2162
Jens Axboe03b12302019-12-02 18:50:25 -07002163static int io_sendmsg_prep(struct io_kiocb *req, struct io_async_ctx *io)
Jens Axboeaa1fa282019-04-19 13:38:09 -06002164{
Jens Axboe03b12302019-12-02 18:50:25 -07002165#if defined(CONFIG_NET)
2166 const struct io_uring_sqe *sqe = req->sqe;
2167 struct user_msghdr __user *msg;
2168 unsigned flags;
2169
2170 flags = READ_ONCE(sqe->msg_flags);
Jens Axboed55e5f52019-12-11 16:12:15 -07002171 msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboed9688562019-12-09 19:35:20 -07002172 io->msg.iov = io->msg.fast_iov;
Jens Axboe03b12302019-12-02 18:50:25 -07002173 return sendmsg_copy_msghdr(&io->msg.msg, msg, flags, &io->msg.iov);
2174#else
2175 return 0;
2176#endif
2177}
2178
Jens Axboefc4df992019-12-10 14:38:45 -07002179static int io_sendmsg(struct io_kiocb *req, struct io_kiocb **nxt,
2180 bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07002181{
2182#if defined(CONFIG_NET)
Jens Axboefc4df992019-12-10 14:38:45 -07002183 const struct io_uring_sqe *sqe = req->sqe;
Jens Axboe0b416c32019-12-15 10:57:46 -07002184 struct io_async_msghdr *kmsg = NULL;
Jens Axboe03b12302019-12-02 18:50:25 -07002185 struct socket *sock;
2186 int ret;
2187
2188 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2189 return -EINVAL;
2190
2191 sock = sock_from_file(req->file, &ret);
2192 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002193 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07002194 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -07002195 unsigned flags;
2196
2197 flags = READ_ONCE(sqe->msg_flags);
2198 if (flags & MSG_DONTWAIT)
2199 req->flags |= REQ_F_NOWAIT;
2200 else if (force_nonblock)
2201 flags |= MSG_DONTWAIT;
2202
2203 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07002204 kmsg = &req->io->msg;
2205 kmsg->msg.msg_name = &addr;
2206 /* if iov is set, it's allocated already */
2207 if (!kmsg->iov)
2208 kmsg->iov = kmsg->fast_iov;
2209 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07002210 } else {
Jens Axboe0b416c32019-12-15 10:57:46 -07002211 kmsg = &io.msg;
2212 kmsg->msg.msg_name = &addr;
Jens Axboe03b12302019-12-02 18:50:25 -07002213 ret = io_sendmsg_prep(req, &io);
2214 if (ret)
2215 goto out;
2216 }
2217
Jens Axboe0b416c32019-12-15 10:57:46 -07002218 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Jens Axboe03b12302019-12-02 18:50:25 -07002219 if (force_nonblock && ret == -EAGAIN) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002220 if (req->io)
2221 return -EAGAIN;
2222 if (io_alloc_async_ctx(req))
2223 return -ENOMEM;
2224 memcpy(&req->io->msg, &io.msg, sizeof(io.msg));
2225 req->work.func = io_sendrecv_async;
Jens Axboe0b416c32019-12-15 10:57:46 -07002226 return -EAGAIN;
Jens Axboe03b12302019-12-02 18:50:25 -07002227 }
2228 if (ret == -ERESTARTSYS)
2229 ret = -EINTR;
2230 }
2231
2232out:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002233 if (!io_wq_current_is_worker() && kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07002234 kfree(kmsg->iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002235 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002236 if (ret < 0)
2237 req_set_fail_links(req);
Jens Axboe03b12302019-12-02 18:50:25 -07002238 io_put_req_find_next(req, nxt);
2239 return 0;
2240#else
2241 return -EOPNOTSUPP;
2242#endif
2243}
2244
2245static int io_recvmsg_prep(struct io_kiocb *req, struct io_async_ctx *io)
2246{
2247#if defined(CONFIG_NET)
2248 const struct io_uring_sqe *sqe = req->sqe;
2249 struct user_msghdr __user *msg;
2250 unsigned flags;
2251
2252 flags = READ_ONCE(sqe->msg_flags);
Jens Axboed55e5f52019-12-11 16:12:15 -07002253 msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboed9688562019-12-09 19:35:20 -07002254 io->msg.iov = io->msg.fast_iov;
Jens Axboe03b12302019-12-02 18:50:25 -07002255 return recvmsg_copy_msghdr(&io->msg.msg, msg, flags, &io->msg.uaddr,
2256 &io->msg.iov);
2257#else
2258 return 0;
2259#endif
2260}
2261
Jens Axboefc4df992019-12-10 14:38:45 -07002262static int io_recvmsg(struct io_kiocb *req, struct io_kiocb **nxt,
2263 bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07002264{
2265#if defined(CONFIG_NET)
Jens Axboefc4df992019-12-10 14:38:45 -07002266 const struct io_uring_sqe *sqe = req->sqe;
Jens Axboe0b416c32019-12-15 10:57:46 -07002267 struct io_async_msghdr *kmsg = NULL;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002268 struct socket *sock;
2269 int ret;
2270
2271 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2272 return -EINVAL;
2273
2274 sock = sock_from_file(req->file, &ret);
2275 if (sock) {
2276 struct user_msghdr __user *msg;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002277 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07002278 struct sockaddr_storage addr;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002279 unsigned flags;
2280
2281 flags = READ_ONCE(sqe->msg_flags);
2282 if (flags & MSG_DONTWAIT)
2283 req->flags |= REQ_F_NOWAIT;
2284 else if (force_nonblock)
2285 flags |= MSG_DONTWAIT;
2286
Jens Axboed55e5f52019-12-11 16:12:15 -07002287 msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe03b12302019-12-02 18:50:25 -07002288 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07002289 kmsg = &req->io->msg;
2290 kmsg->msg.msg_name = &addr;
2291 /* if iov is set, it's allocated already */
2292 if (!kmsg->iov)
2293 kmsg->iov = kmsg->fast_iov;
2294 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07002295 } else {
Jens Axboe0b416c32019-12-15 10:57:46 -07002296 kmsg = &io.msg;
2297 kmsg->msg.msg_name = &addr;
Jens Axboe03b12302019-12-02 18:50:25 -07002298 ret = io_recvmsg_prep(req, &io);
2299 if (ret)
2300 goto out;
2301 }
Jens Axboe0fa03c62019-04-19 13:34:07 -06002302
Jens Axboe0b416c32019-12-15 10:57:46 -07002303 ret = __sys_recvmsg_sock(sock, &kmsg->msg, msg, kmsg->uaddr, flags);
Jens Axboe03b12302019-12-02 18:50:25 -07002304 if (force_nonblock && ret == -EAGAIN) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002305 if (req->io)
2306 return -EAGAIN;
2307 if (io_alloc_async_ctx(req))
2308 return -ENOMEM;
2309 memcpy(&req->io->msg, &io.msg, sizeof(io.msg));
2310 req->work.func = io_sendrecv_async;
Jens Axboe0b416c32019-12-15 10:57:46 -07002311 return -EAGAIN;
Jens Axboe03b12302019-12-02 18:50:25 -07002312 }
Jens Axboe441cdbd2019-12-02 18:49:10 -07002313 if (ret == -ERESTARTSYS)
2314 ret = -EINTR;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002315 }
2316
Jens Axboe03b12302019-12-02 18:50:25 -07002317out:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002318 if (!io_wq_current_is_worker() && kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07002319 kfree(kmsg->iov);
Jens Axboe78e19bb2019-11-06 15:21:34 -07002320 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002321 if (ret < 0)
2322 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08002323 io_put_req_find_next(req, nxt);
Jens Axboe0fa03c62019-04-19 13:34:07 -06002324 return 0;
2325#else
2326 return -EOPNOTSUPP;
2327#endif
2328}
2329
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002330static int io_accept_prep(struct io_kiocb *req)
Jens Axboe17f2fe32019-10-17 14:42:58 -06002331{
2332#if defined(CONFIG_NET)
Jens Axboefc4df992019-12-10 14:38:45 -07002333 const struct io_uring_sqe *sqe = req->sqe;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002334 struct io_accept *accept = &req->accept;
2335
2336 if (req->flags & REQ_F_PREPPED)
2337 return 0;
Jens Axboe17f2fe32019-10-17 14:42:58 -06002338
2339 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
2340 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05002341 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06002342 return -EINVAL;
2343
Jens Axboed55e5f52019-12-11 16:12:15 -07002344 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
2345 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002346 accept->flags = READ_ONCE(sqe->accept_flags);
2347 req->flags |= REQ_F_PREPPED;
2348 return 0;
2349#else
2350 return -EOPNOTSUPP;
2351#endif
2352}
Jens Axboe17f2fe32019-10-17 14:42:58 -06002353
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002354#if defined(CONFIG_NET)
2355static int __io_accept(struct io_kiocb *req, struct io_kiocb **nxt,
2356 bool force_nonblock)
2357{
2358 struct io_accept *accept = &req->accept;
2359 unsigned file_flags;
2360 int ret;
2361
2362 file_flags = force_nonblock ? O_NONBLOCK : 0;
2363 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
2364 accept->addr_len, accept->flags);
2365 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06002366 return -EAGAIN;
Jens Axboe8e3cca12019-11-09 19:52:33 -07002367 if (ret == -ERESTARTSYS)
2368 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002369 if (ret < 0)
2370 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07002371 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08002372 io_put_req_find_next(req, nxt);
Jens Axboe17f2fe32019-10-17 14:42:58 -06002373 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002374}
2375
2376static void io_accept_finish(struct io_wq_work **workptr)
2377{
2378 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2379 struct io_kiocb *nxt = NULL;
2380
2381 if (io_req_cancelled(req))
2382 return;
2383 __io_accept(req, &nxt, false);
2384 if (nxt)
2385 *workptr = &nxt->work;
2386}
2387#endif
2388
2389static int io_accept(struct io_kiocb *req, struct io_kiocb **nxt,
2390 bool force_nonblock)
2391{
2392#if defined(CONFIG_NET)
2393 int ret;
2394
2395 ret = io_accept_prep(req);
2396 if (ret)
2397 return ret;
2398
2399 ret = __io_accept(req, nxt, force_nonblock);
2400 if (ret == -EAGAIN && force_nonblock) {
2401 req->work.func = io_accept_finish;
2402 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
2403 io_put_req(req);
2404 return -EAGAIN;
2405 }
2406 return 0;
Jens Axboe17f2fe32019-10-17 14:42:58 -06002407#else
2408 return -EOPNOTSUPP;
2409#endif
2410}
2411
Jens Axboef499a022019-12-02 16:28:46 -07002412static int io_connect_prep(struct io_kiocb *req, struct io_async_ctx *io)
2413{
2414#if defined(CONFIG_NET)
2415 const struct io_uring_sqe *sqe = req->sqe;
Jens Axboef499a022019-12-02 16:28:46 -07002416
Jens Axboe3fbb51c2019-12-20 08:51:52 -07002417 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
2418 return -EINVAL;
2419 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
2420 return -EINVAL;
2421
2422 req->connect.addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
2423 req->connect.addr_len = READ_ONCE(sqe->addr2);
2424 return move_addr_to_kernel(req->connect.addr, req->connect.addr_len,
2425 &io->connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07002426#else
Jens Axboe3fbb51c2019-12-20 08:51:52 -07002427 return -EOPNOTSUPP;
Jens Axboef499a022019-12-02 16:28:46 -07002428#endif
2429}
2430
Jens Axboefc4df992019-12-10 14:38:45 -07002431static int io_connect(struct io_kiocb *req, struct io_kiocb **nxt,
2432 bool force_nonblock)
Jens Axboef8e85cf2019-11-23 14:24:24 -07002433{
2434#if defined(CONFIG_NET)
Jens Axboef499a022019-12-02 16:28:46 -07002435 struct io_async_ctx __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07002436 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07002437 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07002438
Jens Axboef499a022019-12-02 16:28:46 -07002439 if (req->io) {
2440 io = req->io;
2441 } else {
2442 ret = io_connect_prep(req, &__io);
2443 if (ret)
2444 goto out;
2445 io = &__io;
2446 }
2447
Jens Axboe3fbb51c2019-12-20 08:51:52 -07002448 file_flags = force_nonblock ? O_NONBLOCK : 0;
2449
2450 ret = __sys_connect_file(req->file, &io->connect.address,
2451 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07002452 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002453 if (req->io)
2454 return -EAGAIN;
2455 if (io_alloc_async_ctx(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07002456 ret = -ENOMEM;
2457 goto out;
2458 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002459 memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect));
Jens Axboef8e85cf2019-11-23 14:24:24 -07002460 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07002461 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07002462 if (ret == -ERESTARTSYS)
2463 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07002464out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002465 if (ret < 0)
2466 req_set_fail_links(req);
Jens Axboef8e85cf2019-11-23 14:24:24 -07002467 io_cqring_add_event(req, ret);
2468 io_put_req_find_next(req, nxt);
2469 return 0;
2470#else
2471 return -EOPNOTSUPP;
2472#endif
2473}
2474
Jens Axboe221c5eb2019-01-17 09:41:58 -07002475static void io_poll_remove_one(struct io_kiocb *req)
2476{
2477 struct io_poll_iocb *poll = &req->poll;
2478
2479 spin_lock(&poll->head->lock);
2480 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07002481 if (!list_empty(&poll->wait.entry)) {
2482 list_del_init(&poll->wait.entry);
Jackie Liua197f662019-11-08 08:09:12 -07002483 io_queue_async_work(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002484 }
2485 spin_unlock(&poll->head->lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07002486 hash_del(&req->hash_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002487}
2488
2489static void io_poll_remove_all(struct io_ring_ctx *ctx)
2490{
Jens Axboe78076bb2019-12-04 19:56:40 -07002491 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002492 struct io_kiocb *req;
Jens Axboe78076bb2019-12-04 19:56:40 -07002493 int i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002494
2495 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07002496 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
2497 struct hlist_head *list;
2498
2499 list = &ctx->cancel_hash[i];
2500 hlist_for_each_entry_safe(req, tmp, list, hash_node)
2501 io_poll_remove_one(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002502 }
2503 spin_unlock_irq(&ctx->completion_lock);
2504}
2505
Jens Axboe47f46762019-11-09 17:43:02 -07002506static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
2507{
Jens Axboe78076bb2019-12-04 19:56:40 -07002508 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07002509 struct io_kiocb *req;
2510
Jens Axboe78076bb2019-12-04 19:56:40 -07002511 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
2512 hlist_for_each_entry(req, list, hash_node) {
2513 if (sqe_addr == req->user_data) {
Jens Axboeeac406c2019-11-14 12:09:58 -07002514 io_poll_remove_one(req);
2515 return 0;
2516 }
Jens Axboe47f46762019-11-09 17:43:02 -07002517 }
2518
2519 return -ENOENT;
2520}
2521
Jens Axboe0969e782019-12-17 18:40:57 -07002522static int io_poll_remove_prep(struct io_kiocb *req)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002523{
Jens Axboefc4df992019-12-10 14:38:45 -07002524 const struct io_uring_sqe *sqe = req->sqe;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002525
Jens Axboe0969e782019-12-17 18:40:57 -07002526 if (req->flags & REQ_F_PREPPED)
2527 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002528 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2529 return -EINVAL;
2530 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
2531 sqe->poll_events)
2532 return -EINVAL;
2533
Jens Axboe0969e782019-12-17 18:40:57 -07002534 req->poll.addr = READ_ONCE(sqe->addr);
2535 req->flags |= REQ_F_PREPPED;
2536 return 0;
2537}
2538
2539/*
2540 * Find a running poll command that matches one specified in sqe->addr,
2541 * and remove it if found.
2542 */
2543static int io_poll_remove(struct io_kiocb *req)
2544{
2545 struct io_ring_ctx *ctx = req->ctx;
2546 u64 addr;
2547 int ret;
2548
2549 ret = io_poll_remove_prep(req);
2550 if (ret)
2551 return ret;
2552
2553 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002554 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07002555 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002556 spin_unlock_irq(&ctx->completion_lock);
2557
Jens Axboe78e19bb2019-11-06 15:21:34 -07002558 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002559 if (ret < 0)
2560 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06002561 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002562 return 0;
2563}
2564
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002565static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002566{
Jackie Liua197f662019-11-08 08:09:12 -07002567 struct io_ring_ctx *ctx = req->ctx;
2568
Jens Axboe8c838782019-03-12 15:48:16 -06002569 req->poll.done = true;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002570 if (error)
2571 io_cqring_fill_event(req, error);
2572 else
2573 io_cqring_fill_event(req, mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06002574 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002575}
2576
Jens Axboe561fb042019-10-24 07:25:42 -06002577static void io_poll_complete_work(struct io_wq_work **workptr)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002578{
Jens Axboe561fb042019-10-24 07:25:42 -06002579 struct io_wq_work *work = *workptr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002580 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
2581 struct io_poll_iocb *poll = &req->poll;
2582 struct poll_table_struct pt = { ._key = poll->events };
2583 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe89723d02019-11-05 15:32:58 -07002584 struct io_kiocb *nxt = NULL;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002585 __poll_t mask = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002586 int ret = 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002587
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002588 if (work->flags & IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06002589 WRITE_ONCE(poll->canceled, true);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002590 ret = -ECANCELED;
2591 } else if (READ_ONCE(poll->canceled)) {
2592 ret = -ECANCELED;
2593 }
Jens Axboe561fb042019-10-24 07:25:42 -06002594
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002595 if (ret != -ECANCELED)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002596 mask = vfs_poll(poll->file, &pt) & poll->events;
2597
2598 /*
2599 * Note that ->ki_cancel callers also delete iocb from active_reqs after
2600 * calling ->ki_cancel. We need the ctx_lock roundtrip here to
2601 * synchronize with them. In the cancellation case the list_del_init
2602 * itself is not actually needed, but harmless so we keep it in to
2603 * avoid further branches in the fast path.
2604 */
2605 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002606 if (!mask && ret != -ECANCELED) {
Jens Axboe392edb42019-12-09 17:52:20 -07002607 add_wait_queue(poll->head, &poll->wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002608 spin_unlock_irq(&ctx->completion_lock);
2609 return;
2610 }
Jens Axboe78076bb2019-12-04 19:56:40 -07002611 hash_del(&req->hash_node);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002612 io_poll_complete(req, mask, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002613 spin_unlock_irq(&ctx->completion_lock);
2614
Jens Axboe8c838782019-03-12 15:48:16 -06002615 io_cqring_ev_posted(ctx);
Jens Axboe89723d02019-11-05 15:32:58 -07002616
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002617 if (ret < 0)
2618 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08002619 io_put_req_find_next(req, &nxt);
Jens Axboe89723d02019-11-05 15:32:58 -07002620 if (nxt)
2621 *workptr = &nxt->work;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002622}
2623
2624static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
2625 void *key)
2626{
Jens Axboee9444752019-11-26 15:02:04 -07002627 struct io_poll_iocb *poll = wait->private;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002628 struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
2629 struct io_ring_ctx *ctx = req->ctx;
2630 __poll_t mask = key_to_poll(key);
Jens Axboe8c838782019-03-12 15:48:16 -06002631 unsigned long flags;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002632
2633 /* for instances that support it check for an event match first: */
Jens Axboe8c838782019-03-12 15:48:16 -06002634 if (mask && !(mask & poll->events))
2635 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002636
Jens Axboe392edb42019-12-09 17:52:20 -07002637 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06002638
Jens Axboe7c9e7f02019-11-12 08:15:53 -07002639 /*
2640 * Run completion inline if we can. We're using trylock here because
2641 * we are violating the completion_lock -> poll wq lock ordering.
2642 * If we have a link timeout we're going to need the completion_lock
2643 * for finalizing the request, mark us as having grabbed that already.
2644 */
Jens Axboe8c838782019-03-12 15:48:16 -06002645 if (mask && spin_trylock_irqsave(&ctx->completion_lock, flags)) {
Jens Axboe78076bb2019-12-04 19:56:40 -07002646 hash_del(&req->hash_node);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002647 io_poll_complete(req, mask, 0);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07002648 req->flags |= REQ_F_COMP_LOCKED;
2649 io_put_req(req);
Jens Axboe8c838782019-03-12 15:48:16 -06002650 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2651
2652 io_cqring_ev_posted(ctx);
Jens Axboe8c838782019-03-12 15:48:16 -06002653 } else {
Jackie Liua197f662019-11-08 08:09:12 -07002654 io_queue_async_work(req);
Jens Axboe8c838782019-03-12 15:48:16 -06002655 }
2656
Jens Axboe221c5eb2019-01-17 09:41:58 -07002657 return 1;
2658}
2659
2660struct io_poll_table {
2661 struct poll_table_struct pt;
2662 struct io_kiocb *req;
2663 int error;
2664};
2665
2666static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
2667 struct poll_table_struct *p)
2668{
2669 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
2670
2671 if (unlikely(pt->req->poll.head)) {
2672 pt->error = -EINVAL;
2673 return;
2674 }
2675
2676 pt->error = 0;
2677 pt->req->poll.head = head;
Jens Axboe392edb42019-12-09 17:52:20 -07002678 add_wait_queue(head, &pt->req->poll.wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002679}
2680
Jens Axboeeac406c2019-11-14 12:09:58 -07002681static void io_poll_req_insert(struct io_kiocb *req)
2682{
2683 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07002684 struct hlist_head *list;
Jens Axboeeac406c2019-11-14 12:09:58 -07002685
Jens Axboe78076bb2019-12-04 19:56:40 -07002686 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
2687 hlist_add_head(&req->hash_node, list);
Jens Axboeeac406c2019-11-14 12:09:58 -07002688}
2689
Jens Axboe0969e782019-12-17 18:40:57 -07002690static int io_poll_add_prep(struct io_kiocb *req)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002691{
Jens Axboefc4df992019-12-10 14:38:45 -07002692 const struct io_uring_sqe *sqe = req->sqe;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002693 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002694 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002695
Jens Axboe0969e782019-12-17 18:40:57 -07002696 if (req->flags & REQ_F_PREPPED)
2697 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002698 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2699 return -EINVAL;
2700 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
2701 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06002702 if (!poll->file)
2703 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002704
Jens Axboe0969e782019-12-17 18:40:57 -07002705 req->flags |= REQ_F_PREPPED;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002706 events = READ_ONCE(sqe->poll_events);
2707 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
Jens Axboe0969e782019-12-17 18:40:57 -07002708 return 0;
2709}
2710
2711static int io_poll_add(struct io_kiocb *req, struct io_kiocb **nxt)
2712{
2713 struct io_poll_iocb *poll = &req->poll;
2714 struct io_ring_ctx *ctx = req->ctx;
2715 struct io_poll_table ipt;
2716 bool cancel = false;
2717 __poll_t mask;
2718 int ret;
2719
2720 ret = io_poll_add_prep(req);
2721 if (ret)
2722 return ret;
2723
2724 INIT_IO_WORK(&req->work, io_poll_complete_work);
Jens Axboe78076bb2019-12-04 19:56:40 -07002725 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002726
Jens Axboe221c5eb2019-01-17 09:41:58 -07002727 poll->head = NULL;
Jens Axboe8c838782019-03-12 15:48:16 -06002728 poll->done = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002729 poll->canceled = false;
2730
2731 ipt.pt._qproc = io_poll_queue_proc;
2732 ipt.pt._key = poll->events;
2733 ipt.req = req;
2734 ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
2735
2736 /* initialized the list so that we can do list_empty checks */
Jens Axboe392edb42019-12-09 17:52:20 -07002737 INIT_LIST_HEAD(&poll->wait.entry);
2738 init_waitqueue_func_entry(&poll->wait, io_poll_wake);
2739 poll->wait.private = poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002740
Jens Axboe36703242019-07-25 10:20:18 -06002741 INIT_LIST_HEAD(&req->list);
2742
Jens Axboe221c5eb2019-01-17 09:41:58 -07002743 mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002744
2745 spin_lock_irq(&ctx->completion_lock);
Jens Axboe8c838782019-03-12 15:48:16 -06002746 if (likely(poll->head)) {
2747 spin_lock(&poll->head->lock);
Jens Axboe392edb42019-12-09 17:52:20 -07002748 if (unlikely(list_empty(&poll->wait.entry))) {
Jens Axboe8c838782019-03-12 15:48:16 -06002749 if (ipt.error)
2750 cancel = true;
2751 ipt.error = 0;
2752 mask = 0;
2753 }
2754 if (mask || ipt.error)
Jens Axboe392edb42019-12-09 17:52:20 -07002755 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06002756 else if (cancel)
2757 WRITE_ONCE(poll->canceled, true);
2758 else if (!poll->done) /* actually waiting for an event */
Jens Axboeeac406c2019-11-14 12:09:58 -07002759 io_poll_req_insert(req);
Jens Axboe8c838782019-03-12 15:48:16 -06002760 spin_unlock(&poll->head->lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002761 }
Jens Axboe8c838782019-03-12 15:48:16 -06002762 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06002763 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002764 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06002765 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07002766 spin_unlock_irq(&ctx->completion_lock);
2767
Jens Axboe8c838782019-03-12 15:48:16 -06002768 if (mask) {
2769 io_cqring_ev_posted(ctx);
Jackie Liuec9c02a2019-11-08 23:50:36 +08002770 io_put_req_find_next(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002771 }
Jens Axboe8c838782019-03-12 15:48:16 -06002772 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002773}
2774
Jens Axboe5262f562019-09-17 12:26:57 -06002775static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
2776{
Jens Axboead8a48a2019-11-15 08:49:11 -07002777 struct io_timeout_data *data = container_of(timer,
2778 struct io_timeout_data, timer);
2779 struct io_kiocb *req = data->req;
2780 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06002781 unsigned long flags;
2782
Jens Axboe5262f562019-09-17 12:26:57 -06002783 atomic_inc(&ctx->cq_timeouts);
2784
2785 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08002786 /*
Jens Axboe11365042019-10-16 09:08:32 -06002787 * We could be racing with timeout deletion. If the list is empty,
2788 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08002789 */
Jens Axboe842f9612019-10-29 12:34:10 -06002790 if (!list_empty(&req->list)) {
Jens Axboe11365042019-10-16 09:08:32 -06002791 struct io_kiocb *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06002792
Jens Axboe11365042019-10-16 09:08:32 -06002793 /*
2794 * Adjust the reqs sequence before the current one because it
Brian Gianforcarod195a662019-12-13 03:09:50 -08002795 * will consume a slot in the cq_ring and the cq_tail
Jens Axboe11365042019-10-16 09:08:32 -06002796 * pointer will be increased, otherwise other timeout reqs may
2797 * return in advance without waiting for enough wait_nr.
2798 */
2799 prev = req;
2800 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
2801 prev->sequence++;
Jens Axboe11365042019-10-16 09:08:32 -06002802 list_del_init(&req->list);
Jens Axboe11365042019-10-16 09:08:32 -06002803 }
Jens Axboe842f9612019-10-29 12:34:10 -06002804
Jens Axboe78e19bb2019-11-06 15:21:34 -07002805 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06002806 io_commit_cqring(ctx);
2807 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2808
2809 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002810 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06002811 io_put_req(req);
2812 return HRTIMER_NORESTART;
2813}
2814
Jens Axboe47f46762019-11-09 17:43:02 -07002815static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
2816{
2817 struct io_kiocb *req;
2818 int ret = -ENOENT;
2819
2820 list_for_each_entry(req, &ctx->timeout_list, list) {
2821 if (user_data == req->user_data) {
2822 list_del_init(&req->list);
2823 ret = 0;
2824 break;
2825 }
2826 }
2827
2828 if (ret == -ENOENT)
2829 return ret;
2830
Jens Axboe2d283902019-12-04 11:08:05 -07002831 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe47f46762019-11-09 17:43:02 -07002832 if (ret == -1)
2833 return -EALREADY;
2834
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002835 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07002836 io_cqring_fill_event(req, -ECANCELED);
2837 io_put_req(req);
2838 return 0;
2839}
2840
Jens Axboeb29472e2019-12-17 18:50:29 -07002841static int io_timeout_remove_prep(struct io_kiocb *req)
2842{
2843 const struct io_uring_sqe *sqe = req->sqe;
2844
2845 if (req->flags & REQ_F_PREPPED)
2846 return 0;
2847 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2848 return -EINVAL;
2849 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
2850 return -EINVAL;
2851
2852 req->timeout.addr = READ_ONCE(sqe->addr);
2853 req->timeout.flags = READ_ONCE(sqe->timeout_flags);
2854 if (req->timeout.flags)
2855 return -EINVAL;
2856
2857 req->flags |= REQ_F_PREPPED;
2858 return 0;
2859}
2860
Jens Axboe11365042019-10-16 09:08:32 -06002861/*
2862 * Remove or update an existing timeout command
2863 */
Jens Axboefc4df992019-12-10 14:38:45 -07002864static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06002865{
2866 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07002867 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06002868
Jens Axboeb29472e2019-12-17 18:50:29 -07002869 ret = io_timeout_remove_prep(req);
2870 if (ret)
2871 return ret;
Jens Axboe11365042019-10-16 09:08:32 -06002872
Jens Axboe11365042019-10-16 09:08:32 -06002873 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb29472e2019-12-17 18:50:29 -07002874 ret = io_timeout_cancel(ctx, req->timeout.addr);
Jens Axboe11365042019-10-16 09:08:32 -06002875
Jens Axboe47f46762019-11-09 17:43:02 -07002876 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06002877 io_commit_cqring(ctx);
2878 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06002879 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002880 if (ret < 0)
2881 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08002882 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06002883 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06002884}
2885
Jens Axboe2d283902019-12-04 11:08:05 -07002886static int io_timeout_prep(struct io_kiocb *req, struct io_async_ctx *io,
2887 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06002888{
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002889 const struct io_uring_sqe *sqe = req->sqe;
Jens Axboead8a48a2019-11-15 08:49:11 -07002890 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06002891 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06002892
Jens Axboead8a48a2019-11-15 08:49:11 -07002893 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06002894 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07002895 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06002896 return -EINVAL;
Jens Axboe2d283902019-12-04 11:08:05 -07002897 if (sqe->off && is_timeout_link)
2898 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06002899 flags = READ_ONCE(sqe->timeout_flags);
2900 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06002901 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06002902
Jens Axboe2d283902019-12-04 11:08:05 -07002903 data = &io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07002904 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07002905 req->flags |= REQ_F_TIMEOUT;
2906
2907 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06002908 return -EFAULT;
2909
Jens Axboe11365042019-10-16 09:08:32 -06002910 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07002911 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06002912 else
Jens Axboead8a48a2019-11-15 08:49:11 -07002913 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06002914
Jens Axboead8a48a2019-11-15 08:49:11 -07002915 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
2916 return 0;
2917}
2918
Jens Axboefc4df992019-12-10 14:38:45 -07002919static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07002920{
Jens Axboefc4df992019-12-10 14:38:45 -07002921 const struct io_uring_sqe *sqe = req->sqe;
Jens Axboead8a48a2019-11-15 08:49:11 -07002922 unsigned count;
2923 struct io_ring_ctx *ctx = req->ctx;
2924 struct io_timeout_data *data;
2925 struct list_head *entry;
2926 unsigned span = 0;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002927 int ret;
Jens Axboead8a48a2019-11-15 08:49:11 -07002928
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002929 if (!req->io) {
2930 if (io_alloc_async_ctx(req))
Jens Axboe2d283902019-12-04 11:08:05 -07002931 return -ENOMEM;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002932 ret = io_timeout_prep(req, req->io, false);
2933 if (ret)
Jens Axboe2d283902019-12-04 11:08:05 -07002934 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07002935 }
2936 data = &req->io->timeout;
Jens Axboe93bd25b2019-11-11 23:34:31 -07002937
Jens Axboe5262f562019-09-17 12:26:57 -06002938 /*
2939 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07002940 * timeout event to be satisfied. If it isn't set, then this is
2941 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06002942 */
2943 count = READ_ONCE(sqe->off);
Jens Axboe93bd25b2019-11-11 23:34:31 -07002944 if (!count) {
2945 req->flags |= REQ_F_TIMEOUT_NOSEQ;
2946 spin_lock_irq(&ctx->completion_lock);
2947 entry = ctx->timeout_list.prev;
2948 goto add;
2949 }
Jens Axboe5262f562019-09-17 12:26:57 -06002950
2951 req->sequence = ctx->cached_sq_head + count - 1;
Jens Axboe2d283902019-12-04 11:08:05 -07002952 data->seq_offset = count;
Jens Axboe5262f562019-09-17 12:26:57 -06002953
2954 /*
2955 * Insertion sort, ensuring the first entry in the list is always
2956 * the one we need first.
2957 */
Jens Axboe5262f562019-09-17 12:26:57 -06002958 spin_lock_irq(&ctx->completion_lock);
2959 list_for_each_prev(entry, &ctx->timeout_list) {
2960 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
yangerkun5da0fb12019-10-15 21:59:29 +08002961 unsigned nxt_sq_head;
2962 long long tmp, tmp_nxt;
Jens Axboe2d283902019-12-04 11:08:05 -07002963 u32 nxt_offset = nxt->io->timeout.seq_offset;
Jens Axboe5262f562019-09-17 12:26:57 -06002964
Jens Axboe93bd25b2019-11-11 23:34:31 -07002965 if (nxt->flags & REQ_F_TIMEOUT_NOSEQ)
2966 continue;
2967
yangerkun5da0fb12019-10-15 21:59:29 +08002968 /*
2969 * Since cached_sq_head + count - 1 can overflow, use type long
2970 * long to store it.
2971 */
2972 tmp = (long long)ctx->cached_sq_head + count - 1;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +03002973 nxt_sq_head = nxt->sequence - nxt_offset + 1;
2974 tmp_nxt = (long long)nxt_sq_head + nxt_offset - 1;
yangerkun5da0fb12019-10-15 21:59:29 +08002975
2976 /*
2977 * cached_sq_head may overflow, and it will never overflow twice
2978 * once there is some timeout req still be valid.
2979 */
2980 if (ctx->cached_sq_head < nxt_sq_head)
yangerkun8b07a652019-10-17 12:12:35 +08002981 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08002982
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002983 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06002984 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002985
2986 /*
2987 * Sequence of reqs after the insert one and itself should
2988 * be adjusted because each timeout req consumes a slot.
2989 */
2990 span++;
2991 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06002992 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002993 req->sequence -= span;
Jens Axboe93bd25b2019-11-11 23:34:31 -07002994add:
Jens Axboe5262f562019-09-17 12:26:57 -06002995 list_add(&req->list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07002996 data->timer.function = io_timeout_fn;
2997 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06002998 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06002999 return 0;
3000}
3001
Jens Axboe62755e32019-10-28 21:49:21 -06003002static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06003003{
Jens Axboe62755e32019-10-28 21:49:21 -06003004 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06003005
Jens Axboe62755e32019-10-28 21:49:21 -06003006 return req->user_data == (unsigned long) data;
3007}
3008
Jens Axboee977d6d2019-11-05 12:39:45 -07003009static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06003010{
Jens Axboe62755e32019-10-28 21:49:21 -06003011 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06003012 int ret = 0;
3013
Jens Axboe62755e32019-10-28 21:49:21 -06003014 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
3015 switch (cancel_ret) {
3016 case IO_WQ_CANCEL_OK:
3017 ret = 0;
3018 break;
3019 case IO_WQ_CANCEL_RUNNING:
3020 ret = -EALREADY;
3021 break;
3022 case IO_WQ_CANCEL_NOTFOUND:
3023 ret = -ENOENT;
3024 break;
3025 }
3026
Jens Axboee977d6d2019-11-05 12:39:45 -07003027 return ret;
3028}
3029
Jens Axboe47f46762019-11-09 17:43:02 -07003030static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
3031 struct io_kiocb *req, __u64 sqe_addr,
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003032 struct io_kiocb **nxt, int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07003033{
3034 unsigned long flags;
3035 int ret;
3036
3037 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
3038 if (ret != -ENOENT) {
3039 spin_lock_irqsave(&ctx->completion_lock, flags);
3040 goto done;
3041 }
3042
3043 spin_lock_irqsave(&ctx->completion_lock, flags);
3044 ret = io_timeout_cancel(ctx, sqe_addr);
3045 if (ret != -ENOENT)
3046 goto done;
3047 ret = io_poll_cancel(ctx, sqe_addr);
3048done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003049 if (!ret)
3050 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07003051 io_cqring_fill_event(req, ret);
3052 io_commit_cqring(ctx);
3053 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3054 io_cqring_ev_posted(ctx);
3055
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003056 if (ret < 0)
3057 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07003058 io_put_req_find_next(req, nxt);
3059}
3060
Jens Axboefbf23842019-12-17 18:45:56 -07003061static int io_async_cancel_prep(struct io_kiocb *req)
Jens Axboee977d6d2019-11-05 12:39:45 -07003062{
Jens Axboefc4df992019-12-10 14:38:45 -07003063 const struct io_uring_sqe *sqe = req->sqe;
Jens Axboee977d6d2019-11-05 12:39:45 -07003064
Jens Axboefbf23842019-12-17 18:45:56 -07003065 if (req->flags & REQ_F_PREPPED)
3066 return 0;
3067 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07003068 return -EINVAL;
3069 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
3070 sqe->cancel_flags)
3071 return -EINVAL;
3072
Jens Axboefbf23842019-12-17 18:45:56 -07003073 req->flags |= REQ_F_PREPPED;
3074 req->cancel.addr = READ_ONCE(sqe->addr);
3075 return 0;
3076}
3077
3078static int io_async_cancel(struct io_kiocb *req, struct io_kiocb **nxt)
3079{
3080 struct io_ring_ctx *ctx = req->ctx;
3081 int ret;
3082
3083 ret = io_async_cancel_prep(req);
3084 if (ret)
3085 return ret;
3086
3087 io_async_find_and_cancel(ctx, req, req->cancel.addr, nxt, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06003088 return 0;
3089}
3090
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003091static int io_req_defer_prep(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07003092{
3093 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003094 struct io_async_ctx *io = req->io;
Jens Axboef67676d2019-12-02 11:03:47 -07003095 struct iov_iter iter;
Jens Axboee7815732019-12-17 19:45:06 -07003096 ssize_t ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003097
Jens Axboed625c6e2019-12-17 19:53:05 -07003098 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07003099 case IORING_OP_NOP:
3100 break;
Jens Axboef67676d2019-12-02 11:03:47 -07003101 case IORING_OP_READV:
3102 case IORING_OP_READ_FIXED:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003103 /* ensure prep does right import */
3104 req->io = NULL;
Jens Axboef67676d2019-12-02 11:03:47 -07003105 ret = io_read_prep(req, &iovec, &iter, true);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003106 req->io = io;
3107 if (ret < 0)
3108 break;
3109 io_req_map_rw(req, ret, iovec, inline_vecs, &iter);
3110 ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003111 break;
3112 case IORING_OP_WRITEV:
3113 case IORING_OP_WRITE_FIXED:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003114 /* ensure prep does right import */
3115 req->io = NULL;
Jens Axboef67676d2019-12-02 11:03:47 -07003116 ret = io_write_prep(req, &iovec, &iter, true);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003117 req->io = io;
3118 if (ret < 0)
3119 break;
3120 io_req_map_rw(req, ret, iovec, inline_vecs, &iter);
3121 ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003122 break;
Jens Axboe0969e782019-12-17 18:40:57 -07003123 case IORING_OP_POLL_ADD:
3124 ret = io_poll_add_prep(req);
3125 break;
3126 case IORING_OP_POLL_REMOVE:
3127 ret = io_poll_remove_prep(req);
3128 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003129 case IORING_OP_FSYNC:
3130 ret = io_prep_fsync(req);
3131 break;
3132 case IORING_OP_SYNC_FILE_RANGE:
3133 ret = io_prep_sfr(req);
3134 break;
Jens Axboe03b12302019-12-02 18:50:25 -07003135 case IORING_OP_SENDMSG:
3136 ret = io_sendmsg_prep(req, io);
3137 break;
3138 case IORING_OP_RECVMSG:
3139 ret = io_recvmsg_prep(req, io);
3140 break;
Jens Axboef499a022019-12-02 16:28:46 -07003141 case IORING_OP_CONNECT:
3142 ret = io_connect_prep(req, io);
3143 break;
Jens Axboe2d283902019-12-04 11:08:05 -07003144 case IORING_OP_TIMEOUT:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003145 ret = io_timeout_prep(req, io, false);
3146 break;
Jens Axboeb29472e2019-12-17 18:50:29 -07003147 case IORING_OP_TIMEOUT_REMOVE:
3148 ret = io_timeout_remove_prep(req);
3149 break;
Jens Axboefbf23842019-12-17 18:45:56 -07003150 case IORING_OP_ASYNC_CANCEL:
3151 ret = io_async_cancel_prep(req);
3152 break;
Jens Axboe2d283902019-12-04 11:08:05 -07003153 case IORING_OP_LINK_TIMEOUT:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003154 ret = io_timeout_prep(req, io, true);
3155 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003156 case IORING_OP_ACCEPT:
3157 ret = io_accept_prep(req);
3158 break;
Jens Axboef67676d2019-12-02 11:03:47 -07003159 default:
Jens Axboee7815732019-12-17 19:45:06 -07003160 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
3161 req->opcode);
3162 ret = -EINVAL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003163 break;
Jens Axboef67676d2019-12-02 11:03:47 -07003164 }
3165
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003166 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003167}
3168
Jackie Liua197f662019-11-08 08:09:12 -07003169static int io_req_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -06003170{
Jackie Liua197f662019-11-08 08:09:12 -07003171 struct io_ring_ctx *ctx = req->ctx;
Jens Axboef67676d2019-12-02 11:03:47 -07003172 int ret;
Jens Axboede0617e2019-04-06 21:51:27 -06003173
Bob Liu9d858b22019-11-13 18:06:25 +08003174 /* Still need defer if there is pending req in defer list. */
3175 if (!req_need_defer(req) && list_empty(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06003176 return 0;
3177
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003178 if (io_alloc_async_ctx(req))
Jens Axboede0617e2019-04-06 21:51:27 -06003179 return -EAGAIN;
3180
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003181 ret = io_req_defer_prep(req);
3182 if (ret < 0)
Jens Axboe2d283902019-12-04 11:08:05 -07003183 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07003184
Jens Axboede0617e2019-04-06 21:51:27 -06003185 spin_lock_irq(&ctx->completion_lock);
Bob Liu9d858b22019-11-13 18:06:25 +08003186 if (!req_need_defer(req) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06003187 spin_unlock_irq(&ctx->completion_lock);
Jens Axboede0617e2019-04-06 21:51:27 -06003188 return 0;
3189 }
3190
Jens Axboe915967f2019-11-21 09:01:20 -07003191 trace_io_uring_defer(ctx, req, req->user_data);
Jens Axboede0617e2019-04-06 21:51:27 -06003192 list_add_tail(&req->list, &ctx->defer_list);
3193 spin_unlock_irq(&ctx->completion_lock);
3194 return -EIOCBQUEUED;
3195}
3196
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03003197__attribute__((nonnull))
Pavel Begunkovd7324472019-11-21 21:24:36 +03003198static int io_issue_sqe(struct io_kiocb *req, struct io_kiocb **nxt,
3199 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003200{
Jackie Liua197f662019-11-08 08:09:12 -07003201 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07003202 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003203
Jens Axboed625c6e2019-12-17 19:53:05 -07003204 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07003205 case IORING_OP_NOP:
Jens Axboe78e19bb2019-11-06 15:21:34 -07003206 ret = io_nop(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003207 break;
3208 case IORING_OP_READV:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003209 if (unlikely(req->sqe->buf_index))
Jens Axboeedafcce2019-01-09 09:16:05 -07003210 return -EINVAL;
Pavel Begunkov267bc902019-11-07 01:41:08 +03003211 ret = io_read(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003212 break;
3213 case IORING_OP_WRITEV:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003214 if (unlikely(req->sqe->buf_index))
Jens Axboeedafcce2019-01-09 09:16:05 -07003215 return -EINVAL;
Pavel Begunkov267bc902019-11-07 01:41:08 +03003216 ret = io_write(req, nxt, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07003217 break;
3218 case IORING_OP_READ_FIXED:
Pavel Begunkov267bc902019-11-07 01:41:08 +03003219 ret = io_read(req, nxt, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07003220 break;
3221 case IORING_OP_WRITE_FIXED:
Pavel Begunkov267bc902019-11-07 01:41:08 +03003222 ret = io_write(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003223 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003224 case IORING_OP_FSYNC:
Jens Axboefc4df992019-12-10 14:38:45 -07003225 ret = io_fsync(req, nxt, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003226 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003227 case IORING_OP_POLL_ADD:
Jens Axboefc4df992019-12-10 14:38:45 -07003228 ret = io_poll_add(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003229 break;
3230 case IORING_OP_POLL_REMOVE:
Jens Axboefc4df992019-12-10 14:38:45 -07003231 ret = io_poll_remove(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003232 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003233 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboefc4df992019-12-10 14:38:45 -07003234 ret = io_sync_file_range(req, nxt, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003235 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003236 case IORING_OP_SENDMSG:
Jens Axboefc4df992019-12-10 14:38:45 -07003237 ret = io_sendmsg(req, nxt, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06003238 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06003239 case IORING_OP_RECVMSG:
Jens Axboefc4df992019-12-10 14:38:45 -07003240 ret = io_recvmsg(req, nxt, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06003241 break;
Jens Axboe5262f562019-09-17 12:26:57 -06003242 case IORING_OP_TIMEOUT:
Jens Axboefc4df992019-12-10 14:38:45 -07003243 ret = io_timeout(req);
Jens Axboe5262f562019-09-17 12:26:57 -06003244 break;
Jens Axboe11365042019-10-16 09:08:32 -06003245 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboefc4df992019-12-10 14:38:45 -07003246 ret = io_timeout_remove(req);
Jens Axboe11365042019-10-16 09:08:32 -06003247 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06003248 case IORING_OP_ACCEPT:
Jens Axboefc4df992019-12-10 14:38:45 -07003249 ret = io_accept(req, nxt, force_nonblock);
Jens Axboe17f2fe32019-10-17 14:42:58 -06003250 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003251 case IORING_OP_CONNECT:
Jens Axboefc4df992019-12-10 14:38:45 -07003252 ret = io_connect(req, nxt, force_nonblock);
Jens Axboef8e85cf2019-11-23 14:24:24 -07003253 break;
Jens Axboe62755e32019-10-28 21:49:21 -06003254 case IORING_OP_ASYNC_CANCEL:
Jens Axboefc4df992019-12-10 14:38:45 -07003255 ret = io_async_cancel(req, nxt);
Jens Axboe62755e32019-10-28 21:49:21 -06003256 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003257 default:
3258 ret = -EINVAL;
3259 break;
3260 }
3261
Jens Axboedef596e2019-01-09 08:59:42 -07003262 if (ret)
3263 return ret;
3264
3265 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe9e645e112019-05-10 16:07:28 -06003266 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07003267 return -EAGAIN;
3268
Jens Axboedef596e2019-01-09 08:59:42 -07003269 io_iopoll_req_issued(req);
Jens Axboedef596e2019-01-09 08:59:42 -07003270 }
3271
3272 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003273}
3274
Jens Axboeb76da702019-11-20 13:05:32 -07003275static void io_link_work_cb(struct io_wq_work **workptr)
3276{
3277 struct io_wq_work *work = *workptr;
3278 struct io_kiocb *link = work->data;
3279
3280 io_queue_linked_timeout(link);
3281 work->func = io_wq_submit_work;
3282}
3283
Jens Axboe561fb042019-10-24 07:25:42 -06003284static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboe31b51512019-01-18 22:56:34 -07003285{
Jens Axboe561fb042019-10-24 07:25:42 -06003286 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003287 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe561fb042019-10-24 07:25:42 -06003288 struct io_kiocb *nxt = NULL;
3289 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003290
Jens Axboe561fb042019-10-24 07:25:42 -06003291 if (work->flags & IO_WQ_WORK_CANCEL)
3292 ret = -ECANCELED;
Jens Axboe31b51512019-01-18 22:56:34 -07003293
Jens Axboe561fb042019-10-24 07:25:42 -06003294 if (!ret) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003295 req->has_user = (work->flags & IO_WQ_WORK_HAS_MM) != 0;
3296 req->in_async = true;
Jens Axboe561fb042019-10-24 07:25:42 -06003297 do {
Pavel Begunkovd7324472019-11-21 21:24:36 +03003298 ret = io_issue_sqe(req, &nxt, false);
Jens Axboe561fb042019-10-24 07:25:42 -06003299 /*
3300 * We can get EAGAIN for polled IO even though we're
3301 * forcing a sync submission from here, since we can't
3302 * wait for request slots on the block side.
3303 */
3304 if (ret != -EAGAIN)
3305 break;
3306 cond_resched();
3307 } while (1);
3308 }
Jens Axboe31b51512019-01-18 22:56:34 -07003309
Jens Axboe561fb042019-10-24 07:25:42 -06003310 /* drop submission reference */
Jackie Liuec9c02a2019-11-08 23:50:36 +08003311 io_put_req(req);
Jens Axboe817869d2019-04-30 14:44:05 -06003312
Jens Axboe561fb042019-10-24 07:25:42 -06003313 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003314 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07003315 io_cqring_add_event(req, ret);
Jens Axboe817869d2019-04-30 14:44:05 -06003316 io_put_req(req);
Jens Axboeedafcce2019-01-09 09:16:05 -07003317 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003318
Jens Axboe561fb042019-10-24 07:25:42 -06003319 /* if a dependent link is ready, pass it back */
3320 if (!ret && nxt) {
Jens Axboe94ae5e72019-11-14 19:39:52 -07003321 struct io_kiocb *link;
3322
3323 io_prep_async_work(nxt, &link);
Jens Axboe561fb042019-10-24 07:25:42 -06003324 *workptr = &nxt->work;
Jens Axboeb76da702019-11-20 13:05:32 -07003325 if (link) {
3326 nxt->work.flags |= IO_WQ_WORK_CB;
3327 nxt->work.func = io_link_work_cb;
3328 nxt->work.data = link;
3329 }
Jens Axboeedafcce2019-01-09 09:16:05 -07003330 }
Jens Axboe31b51512019-01-18 22:56:34 -07003331}
Jens Axboe2b188cc2019-01-07 10:46:33 -07003332
Jens Axboe9e3aa612019-12-11 15:55:43 -07003333static bool io_req_op_valid(int op)
3334{
3335 return op >= IORING_OP_NOP && op < IORING_OP_LAST;
3336}
3337
Jens Axboed625c6e2019-12-17 19:53:05 -07003338static int io_req_needs_file(struct io_kiocb *req)
Jens Axboe09bb8392019-03-13 12:39:28 -06003339{
Jens Axboed625c6e2019-12-17 19:53:05 -07003340 switch (req->opcode) {
Jens Axboe09bb8392019-03-13 12:39:28 -06003341 case IORING_OP_NOP:
3342 case IORING_OP_POLL_REMOVE:
Pavel Begunkov5683e542019-11-14 00:59:19 +03003343 case IORING_OP_TIMEOUT:
Pavel Begunkova320e9f2019-11-14 00:11:01 +03003344 case IORING_OP_TIMEOUT_REMOVE:
3345 case IORING_OP_ASYNC_CANCEL:
3346 case IORING_OP_LINK_TIMEOUT:
Jens Axboe9e3aa612019-12-11 15:55:43 -07003347 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06003348 default:
Jens Axboed625c6e2019-12-17 19:53:05 -07003349 if (io_req_op_valid(req->opcode))
Jens Axboe9e3aa612019-12-11 15:55:43 -07003350 return 1;
3351 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06003352 }
3353}
3354
Jens Axboe65e19f52019-10-26 07:20:21 -06003355static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
3356 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06003357{
Jens Axboe65e19f52019-10-26 07:20:21 -06003358 struct fixed_file_table *table;
3359
3360 table = &ctx->file_table[index >> IORING_FILE_TABLE_SHIFT];
3361 return table->files[index & IORING_FILE_TABLE_MASK];
3362}
3363
Jackie Liua197f662019-11-08 08:09:12 -07003364static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req)
Jens Axboe09bb8392019-03-13 12:39:28 -06003365{
Jackie Liua197f662019-11-08 08:09:12 -07003366 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe09bb8392019-03-13 12:39:28 -06003367 unsigned flags;
Jens Axboe9e3aa612019-12-11 15:55:43 -07003368 int fd, ret;
Jens Axboe09bb8392019-03-13 12:39:28 -06003369
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003370 flags = READ_ONCE(req->sqe->flags);
3371 fd = READ_ONCE(req->sqe->fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06003372
Jackie Liu4fe2c962019-09-09 20:50:40 +08003373 if (flags & IOSQE_IO_DRAIN)
Jens Axboede0617e2019-04-06 21:51:27 -06003374 req->flags |= REQ_F_IO_DRAIN;
Jens Axboede0617e2019-04-06 21:51:27 -06003375
Jens Axboed625c6e2019-12-17 19:53:05 -07003376 ret = io_req_needs_file(req);
Jens Axboe9e3aa612019-12-11 15:55:43 -07003377 if (ret <= 0)
3378 return ret;
Jens Axboe09bb8392019-03-13 12:39:28 -06003379
3380 if (flags & IOSQE_FIXED_FILE) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003381 if (unlikely(!ctx->file_table ||
Jens Axboe09bb8392019-03-13 12:39:28 -06003382 (unsigned) fd >= ctx->nr_user_files))
3383 return -EBADF;
Jens Axboeb7620122019-10-26 07:22:55 -06003384 fd = array_index_nospec(fd, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06003385 req->file = io_file_from_index(ctx, fd);
3386 if (!req->file)
Jens Axboe08a45172019-10-03 08:11:03 -06003387 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06003388 req->flags |= REQ_F_FIXED_FILE;
3389 } else {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003390 if (req->needs_fixed_file)
Jens Axboe09bb8392019-03-13 12:39:28 -06003391 return -EBADF;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02003392 trace_io_uring_file_get(ctx, fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06003393 req->file = io_file_get(state, fd);
3394 if (unlikely(!req->file))
3395 return -EBADF;
3396 }
3397
3398 return 0;
3399}
3400
Jackie Liua197f662019-11-08 08:09:12 -07003401static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003402{
Jens Axboefcb323c2019-10-24 12:39:47 -06003403 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07003404 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06003405
3406 rcu_read_lock();
3407 spin_lock_irq(&ctx->inflight_lock);
3408 /*
3409 * We use the f_ops->flush() handler to ensure that we can flush
3410 * out work accessing these files if the fd is closed. Check if
3411 * the fd has changed since we started down this path, and disallow
3412 * this operation if it has.
3413 */
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003414 if (fcheck(req->ring_fd) == req->ring_file) {
Jens Axboefcb323c2019-10-24 12:39:47 -06003415 list_add(&req->inflight_entry, &ctx->inflight_list);
3416 req->flags |= REQ_F_INFLIGHT;
3417 req->work.files = current->files;
3418 ret = 0;
3419 }
3420 spin_unlock_irq(&ctx->inflight_lock);
3421 rcu_read_unlock();
3422
3423 return ret;
3424}
3425
Jens Axboe2665abf2019-11-05 12:40:47 -07003426static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
3427{
Jens Axboead8a48a2019-11-15 08:49:11 -07003428 struct io_timeout_data *data = container_of(timer,
3429 struct io_timeout_data, timer);
3430 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07003431 struct io_ring_ctx *ctx = req->ctx;
3432 struct io_kiocb *prev = NULL;
3433 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07003434
3435 spin_lock_irqsave(&ctx->completion_lock, flags);
3436
3437 /*
3438 * We don't expect the list to be empty, that will only happen if we
3439 * race with the completion of the linked work.
3440 */
Pavel Begunkov44932332019-12-05 16:16:35 +03003441 if (!list_empty(&req->link_list)) {
3442 prev = list_entry(req->link_list.prev, struct io_kiocb,
3443 link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07003444 if (refcount_inc_not_zero(&prev->refs)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03003445 list_del_init(&req->link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07003446 prev->flags &= ~REQ_F_LINK_TIMEOUT;
3447 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07003448 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07003449 }
3450
3451 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3452
3453 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003454 req_set_fail_links(prev);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003455 io_async_find_and_cancel(ctx, req, prev->user_data, NULL,
3456 -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07003457 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07003458 } else {
3459 io_cqring_add_event(req, -ETIME);
3460 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07003461 }
Jens Axboe2665abf2019-11-05 12:40:47 -07003462 return HRTIMER_NORESTART;
3463}
3464
Jens Axboead8a48a2019-11-15 08:49:11 -07003465static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07003466{
Jens Axboe76a46e02019-11-10 23:34:16 -07003467 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07003468
Jens Axboe76a46e02019-11-10 23:34:16 -07003469 /*
3470 * If the list is now empty, then our linked request finished before
3471 * we got a chance to setup the timer
3472 */
3473 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov44932332019-12-05 16:16:35 +03003474 if (!list_empty(&req->link_list)) {
Jens Axboe2d283902019-12-04 11:08:05 -07003475 struct io_timeout_data *data = &req->io->timeout;
Jens Axboe94ae5e72019-11-14 19:39:52 -07003476
Jens Axboead8a48a2019-11-15 08:49:11 -07003477 data->timer.function = io_link_timeout_fn;
3478 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
3479 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07003480 }
Jens Axboe76a46e02019-11-10 23:34:16 -07003481 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07003482
Jens Axboe2665abf2019-11-05 12:40:47 -07003483 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07003484 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07003485}
3486
Jens Axboead8a48a2019-11-15 08:49:11 -07003487static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07003488{
3489 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003490
Jens Axboe2665abf2019-11-05 12:40:47 -07003491 if (!(req->flags & REQ_F_LINK))
3492 return NULL;
3493
Pavel Begunkov44932332019-12-05 16:16:35 +03003494 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
3495 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07003496 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07003497 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07003498
Jens Axboe76a46e02019-11-10 23:34:16 -07003499 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07003500 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07003501}
3502
Jens Axboe0e0702d2019-11-14 21:42:10 -07003503static void __io_queue_sqe(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003504{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07003505 struct io_kiocb *linked_timeout;
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03003506 struct io_kiocb *nxt = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003507 int ret;
3508
Jens Axboe4a0a7a12019-12-09 20:01:01 -07003509again:
3510 linked_timeout = io_prep_linked_timeout(req);
3511
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03003512 ret = io_issue_sqe(req, &nxt, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06003513
3514 /*
3515 * We async punt it if the file wasn't marked NOWAIT, or if the file
3516 * doesn't support non-blocking read/write attempts
3517 */
3518 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
3519 (req->flags & REQ_F_MUST_PUNT))) {
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03003520 if (req->work.flags & IO_WQ_WORK_NEEDS_FILES) {
3521 ret = io_grab_files(req);
3522 if (ret)
3523 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003524 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03003525
3526 /*
3527 * Queued up for async execution, worker will release
3528 * submit reference when the iocb is actually submitted.
3529 */
3530 io_queue_async_work(req);
Jens Axboe4a0a7a12019-12-09 20:01:01 -07003531 goto done_req;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003532 }
Jens Axboee65ef562019-03-12 10:16:44 -06003533
Jens Axboefcb323c2019-10-24 12:39:47 -06003534err:
Jens Axboee65ef562019-03-12 10:16:44 -06003535 /* drop submission reference */
3536 io_put_req(req);
3537
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03003538 if (linked_timeout) {
Jens Axboe76a46e02019-11-10 23:34:16 -07003539 if (!ret)
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03003540 io_queue_linked_timeout(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07003541 else
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03003542 io_put_req(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07003543 }
3544
Jens Axboee65ef562019-03-12 10:16:44 -06003545 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06003546 if (ret) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07003547 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003548 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06003549 io_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06003550 }
Jens Axboe4a0a7a12019-12-09 20:01:01 -07003551done_req:
3552 if (nxt) {
3553 req = nxt;
3554 nxt = NULL;
3555 goto again;
3556 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003557}
3558
Jens Axboe0e0702d2019-11-14 21:42:10 -07003559static void io_queue_sqe(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08003560{
3561 int ret;
3562
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003563 if (unlikely(req->ctx->drain_next)) {
3564 req->flags |= REQ_F_IO_DRAIN;
3565 req->ctx->drain_next = false;
3566 }
3567 req->ctx->drain_next = (req->flags & REQ_F_DRAIN_LINK);
3568
Jackie Liua197f662019-11-08 08:09:12 -07003569 ret = io_req_defer(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08003570 if (ret) {
3571 if (ret != -EIOCBQUEUED) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07003572 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003573 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07003574 io_double_put_req(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08003575 }
Jens Axboe0e0702d2019-11-14 21:42:10 -07003576 } else
3577 __io_queue_sqe(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08003578}
3579
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003580static inline void io_queue_link_head(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08003581{
Jens Axboe94ae5e72019-11-14 19:39:52 -07003582 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003583 io_cqring_add_event(req, -ECANCELED);
3584 io_double_put_req(req);
3585 } else
Jens Axboe0e0702d2019-11-14 21:42:10 -07003586 io_queue_sqe(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08003587}
3588
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003589#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
3590 IOSQE_IO_HARDLINK)
Jens Axboe9e645e112019-05-10 16:07:28 -06003591
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03003592static bool io_submit_sqe(struct io_kiocb *req, struct io_submit_state *state,
Jackie Liua197f662019-11-08 08:09:12 -07003593 struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06003594{
Jackie Liua197f662019-11-08 08:09:12 -07003595 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -06003596 int ret;
3597
3598 /* enforce forwards compatibility on users */
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003599 if (unlikely(req->sqe->flags & ~SQE_VALID_FLAGS)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06003600 ret = -EINVAL;
Pavel Begunkov196be952019-11-07 01:41:06 +03003601 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06003602 }
3603
Jackie Liua197f662019-11-08 08:09:12 -07003604 ret = io_req_set_file(state, req);
Jens Axboe9e645e112019-05-10 16:07:28 -06003605 if (unlikely(ret)) {
3606err_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07003607 io_cqring_add_event(req, ret);
3608 io_double_put_req(req);
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03003609 return false;
Jens Axboe9e645e112019-05-10 16:07:28 -06003610 }
3611
Jens Axboe9e645e112019-05-10 16:07:28 -06003612 /*
3613 * If we already have a head request, queue this one for async
3614 * submittal once the head completes. If we don't have a head but
3615 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
3616 * submitted sync once the chain is complete. If none of those
3617 * conditions are true (normal request), then just queue it.
3618 */
3619 if (*link) {
3620 struct io_kiocb *prev = *link;
3621
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003622 if (req->sqe->flags & IOSQE_IO_DRAIN)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003623 (*link)->flags |= REQ_F_DRAIN_LINK | REQ_F_IO_DRAIN;
3624
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003625 if (req->sqe->flags & IOSQE_IO_HARDLINK)
3626 req->flags |= REQ_F_HARDLINK;
3627
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003628 if (io_alloc_async_ctx(req)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06003629 ret = -EAGAIN;
3630 goto err_req;
3631 }
3632
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003633 ret = io_req_defer_prep(req);
Jens Axboe2d283902019-12-04 11:08:05 -07003634 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003635 /* fail even hard links since we don't submit */
Jens Axboe2d283902019-12-04 11:08:05 -07003636 prev->flags |= REQ_F_FAIL_LINK;
Jens Axboef67676d2019-12-02 11:03:47 -07003637 goto err_req;
Jens Axboe2d283902019-12-04 11:08:05 -07003638 }
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02003639 trace_io_uring_link(ctx, req, prev);
Pavel Begunkov44932332019-12-05 16:16:35 +03003640 list_add_tail(&req->link_list, &prev->link_list);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003641 } else if (req->sqe->flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06003642 req->flags |= REQ_F_LINK;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003643 if (req->sqe->flags & IOSQE_IO_HARDLINK)
3644 req->flags |= REQ_F_HARDLINK;
Jens Axboe9e645e112019-05-10 16:07:28 -06003645
Jens Axboe9e645e112019-05-10 16:07:28 -06003646 INIT_LIST_HEAD(&req->link_list);
3647 *link = req;
3648 } else {
Jackie Liua197f662019-11-08 08:09:12 -07003649 io_queue_sqe(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06003650 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03003651
3652 return true;
Jens Axboe9e645e112019-05-10 16:07:28 -06003653}
3654
Jens Axboe9a56a232019-01-09 09:06:50 -07003655/*
3656 * Batched submission is done, ensure local IO is flushed out.
3657 */
3658static void io_submit_state_end(struct io_submit_state *state)
3659{
3660 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06003661 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07003662 if (state->free_reqs)
3663 kmem_cache_free_bulk(req_cachep, state->free_reqs,
3664 &state->reqs[state->cur_req]);
Jens Axboe9a56a232019-01-09 09:06:50 -07003665}
3666
3667/*
3668 * Start submission side cache.
3669 */
3670static void io_submit_state_start(struct io_submit_state *state,
Jackie Liu22efde52019-12-02 17:14:52 +08003671 unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07003672{
3673 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07003674 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07003675 state->file = NULL;
3676 state->ios_left = max_ios;
3677}
3678
Jens Axboe2b188cc2019-01-07 10:46:33 -07003679static void io_commit_sqring(struct io_ring_ctx *ctx)
3680{
Hristo Venev75b28af2019-08-26 17:23:46 +00003681 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003682
Hristo Venev75b28af2019-08-26 17:23:46 +00003683 if (ctx->cached_sq_head != READ_ONCE(rings->sq.head)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07003684 /*
3685 * Ensure any loads from the SQEs are done at this point,
3686 * since once we write the new head, the application could
3687 * write new data to them.
3688 */
Hristo Venev75b28af2019-08-26 17:23:46 +00003689 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003690 }
3691}
3692
3693/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08003694 * Fetch an sqe, if one is available. Note that req->sqe will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07003695 * that is mapped by userspace. This means that care needs to be taken to
3696 * ensure that reads are stable, as we cannot rely on userspace always
3697 * being a good citizen. If members of the sqe are validated and then later
3698 * used, it's important that those reads are done through READ_ONCE() to
3699 * prevent a re-load down the line.
3700 */
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003701static bool io_get_sqring(struct io_ring_ctx *ctx, struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003702{
Hristo Venev75b28af2019-08-26 17:23:46 +00003703 struct io_rings *rings = ctx->rings;
3704 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003705 unsigned head;
3706
3707 /*
3708 * The cached sq head (or cq tail) serves two purposes:
3709 *
3710 * 1) allows us to batch the cost of updating the user visible
3711 * head updates.
3712 * 2) allows the kernel side to track the head on its own, even
3713 * though the application is the one updating it.
3714 */
3715 head = ctx->cached_sq_head;
Stefan Bühlere523a292019-04-19 11:57:44 +02003716 /* make sure SQ entry isn't read before tail */
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03003717 if (unlikely(head == smp_load_acquire(&rings->sq.tail)))
Jens Axboe2b188cc2019-01-07 10:46:33 -07003718 return false;
3719
Hristo Venev75b28af2019-08-26 17:23:46 +00003720 head = READ_ONCE(sq_array[head & ctx->sq_mask]);
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03003721 if (likely(head < ctx->sq_entries)) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003722 /*
3723 * All io need record the previous position, if LINK vs DARIN,
3724 * it can be used to mark the position of the first IO in the
3725 * link list.
3726 */
3727 req->sequence = ctx->cached_sq_head;
3728 req->sqe = &ctx->sq_sqes[head];
Jens Axboed625c6e2019-12-17 19:53:05 -07003729 req->opcode = READ_ONCE(req->sqe->opcode);
3730 req->user_data = READ_ONCE(req->sqe->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003731 ctx->cached_sq_head++;
3732 return true;
3733 }
3734
3735 /* drop invalid entries */
3736 ctx->cached_sq_head++;
Jens Axboe498ccd92019-10-25 10:04:25 -06003737 ctx->cached_sq_dropped++;
3738 WRITE_ONCE(rings->sq_dropped, ctx->cached_sq_dropped);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003739 return false;
3740}
3741
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003742static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkovae9428c2019-11-06 00:22:14 +03003743 struct file *ring_file, int ring_fd,
3744 struct mm_struct **mm, bool async)
Jens Axboe6c271ce2019-01-10 11:22:30 -07003745{
3746 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06003747 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06003748 int i, submitted = 0;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03003749 bool mm_fault = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003750
Jens Axboec4a2ed72019-11-21 21:01:26 -07003751 /* if we have a backlog and couldn't flush it all, return BUSY */
3752 if (!list_empty(&ctx->cq_overflow_list) &&
3753 !io_cqring_overflow_flush(ctx, false))
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003754 return -EBUSY;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003755
3756 if (nr > IO_PLUG_THRESHOLD) {
Jackie Liu22efde52019-12-02 17:14:52 +08003757 io_submit_state_start(&state, nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003758 statep = &state;
3759 }
3760
3761 for (i = 0; i < nr; i++) {
Pavel Begunkov196be952019-11-07 01:41:06 +03003762 struct io_kiocb *req;
Pavel Begunkov50585b92019-11-07 01:41:07 +03003763 unsigned int sqe_flags;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003764
Pavel Begunkov196be952019-11-07 01:41:06 +03003765 req = io_get_req(ctx, statep);
3766 if (unlikely(!req)) {
3767 if (!submitted)
3768 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003769 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06003770 }
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003771 if (!io_get_sqring(ctx, req)) {
Pavel Begunkov196be952019-11-07 01:41:06 +03003772 __io_free_req(req);
3773 break;
3774 }
Jens Axboe9e645e112019-05-10 16:07:28 -06003775
Jens Axboed625c6e2019-12-17 19:53:05 -07003776 if (io_req_needs_user(req) && !*mm) {
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03003777 mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm);
3778 if (!mm_fault) {
3779 use_mm(ctx->sqo_mm);
3780 *mm = ctx->sqo_mm;
3781 }
3782 }
3783
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03003784 submitted++;
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003785 sqe_flags = req->sqe->flags;
Pavel Begunkov50585b92019-11-07 01:41:07 +03003786
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003787 req->ring_file = ring_file;
3788 req->ring_fd = ring_fd;
3789 req->has_user = *mm != NULL;
3790 req->in_async = async;
3791 req->needs_fixed_file = async;
Jens Axboed625c6e2019-12-17 19:53:05 -07003792 trace_io_uring_submit_sqe(ctx, req->user_data, true, async);
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03003793 if (!io_submit_sqe(req, statep, &link))
3794 break;
Pavel Begunkove5eb6362019-11-06 00:22:15 +03003795 /*
3796 * If previous wasn't linked and we have a linked command,
3797 * that's the end of the chain. Submit the previous link.
3798 */
Pavel Begunkovffbb8d62019-12-17 20:57:05 +03003799 if (!(sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK)) && link) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003800 io_queue_link_head(link);
Pavel Begunkove5eb6362019-11-06 00:22:15 +03003801 link = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003802 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07003803 }
3804
Jens Axboe9e645e112019-05-10 16:07:28 -06003805 if (link)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003806 io_queue_link_head(link);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003807 if (statep)
3808 io_submit_state_end(&state);
3809
Pavel Begunkovae9428c2019-11-06 00:22:14 +03003810 /* Commit SQ ring head once we've consumed and submitted all SQEs */
3811 io_commit_sqring(ctx);
3812
Jens Axboe6c271ce2019-01-10 11:22:30 -07003813 return submitted;
3814}
3815
3816static int io_sq_thread(void *data)
3817{
Jens Axboe6c271ce2019-01-10 11:22:30 -07003818 struct io_ring_ctx *ctx = data;
3819 struct mm_struct *cur_mm = NULL;
Jens Axboe181e4482019-11-25 08:52:30 -07003820 const struct cred *old_cred;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003821 mm_segment_t old_fs;
3822 DEFINE_WAIT(wait);
3823 unsigned inflight;
3824 unsigned long timeout;
Jens Axboec1edbf52019-11-10 16:56:04 -07003825 int ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003826
Jens Axboe206aefd2019-11-07 18:27:42 -07003827 complete(&ctx->completions[1]);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08003828
Jens Axboe6c271ce2019-01-10 11:22:30 -07003829 old_fs = get_fs();
3830 set_fs(USER_DS);
Jens Axboe181e4482019-11-25 08:52:30 -07003831 old_cred = override_creds(ctx->creds);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003832
Jens Axboec1edbf52019-11-10 16:56:04 -07003833 ret = timeout = inflight = 0;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003834 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003835 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003836
3837 if (inflight) {
3838 unsigned nr_events = 0;
3839
3840 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe2b2ed972019-10-25 10:06:15 -06003841 /*
3842 * inflight is the count of the maximum possible
3843 * entries we submitted, but it can be smaller
3844 * if we dropped some of them. If we don't have
3845 * poll entries available, then we know that we
3846 * have nothing left to poll for. Reset the
3847 * inflight count to zero in that case.
3848 */
3849 mutex_lock(&ctx->uring_lock);
3850 if (!list_empty(&ctx->poll_list))
3851 __io_iopoll_check(ctx, &nr_events, 0);
3852 else
3853 inflight = 0;
3854 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003855 } else {
3856 /*
3857 * Normal IO, just pretend everything completed.
3858 * We don't have to poll completions for that.
3859 */
3860 nr_events = inflight;
3861 }
3862
3863 inflight -= nr_events;
3864 if (!inflight)
3865 timeout = jiffies + ctx->sq_thread_idle;
3866 }
3867
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003868 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07003869
3870 /*
3871 * If submit got -EBUSY, flag us as needing the application
3872 * to enter the kernel to reap and flush events.
3873 */
3874 if (!to_submit || ret == -EBUSY) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07003875 /*
3876 * We're polling. If we're within the defined idle
3877 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07003878 * to sleep. The exception is if we got EBUSY doing
3879 * more IO, we should wait for the application to
3880 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07003881 */
Jens Axboec1edbf52019-11-10 16:56:04 -07003882 if (inflight ||
3883 (!time_after(jiffies, timeout) && ret != -EBUSY)) {
Jens Axboe9831a902019-09-19 09:48:55 -06003884 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07003885 continue;
3886 }
3887
3888 /*
3889 * Drop cur_mm before scheduling, we can't hold it for
3890 * long periods (or over schedule()). Do this before
3891 * adding ourselves to the waitqueue, as the unuse/drop
3892 * may sleep.
3893 */
3894 if (cur_mm) {
3895 unuse_mm(cur_mm);
3896 mmput(cur_mm);
3897 cur_mm = NULL;
3898 }
3899
3900 prepare_to_wait(&ctx->sqo_wait, &wait,
3901 TASK_INTERRUPTIBLE);
3902
3903 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00003904 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02003905 /* make sure to read SQ tail after writing flags */
3906 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07003907
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003908 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07003909 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003910 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07003911 finish_wait(&ctx->sqo_wait, &wait);
3912 break;
3913 }
3914 if (signal_pending(current))
3915 flush_signals(current);
3916 schedule();
3917 finish_wait(&ctx->sqo_wait, &wait);
3918
Hristo Venev75b28af2019-08-26 17:23:46 +00003919 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003920 continue;
3921 }
3922 finish_wait(&ctx->sqo_wait, &wait);
3923
Hristo Venev75b28af2019-08-26 17:23:46 +00003924 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003925 }
3926
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003927 to_submit = min(to_submit, ctx->sq_entries);
Jens Axboe8a4955f2019-12-09 14:52:35 -07003928 mutex_lock(&ctx->uring_lock);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003929 ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true);
Jens Axboe8a4955f2019-12-09 14:52:35 -07003930 mutex_unlock(&ctx->uring_lock);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003931 if (ret > 0)
3932 inflight += ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003933 }
3934
3935 set_fs(old_fs);
3936 if (cur_mm) {
3937 unuse_mm(cur_mm);
3938 mmput(cur_mm);
3939 }
Jens Axboe181e4482019-11-25 08:52:30 -07003940 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06003941
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003942 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06003943
Jens Axboe6c271ce2019-01-10 11:22:30 -07003944 return 0;
3945}
3946
Jens Axboebda52162019-09-24 13:47:15 -06003947struct io_wait_queue {
3948 struct wait_queue_entry wq;
3949 struct io_ring_ctx *ctx;
3950 unsigned to_wait;
3951 unsigned nr_timeouts;
3952};
3953
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003954static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06003955{
3956 struct io_ring_ctx *ctx = iowq->ctx;
3957
3958 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08003959 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06003960 * started waiting. For timeouts, we always want to return to userspace,
3961 * regardless of event count.
3962 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003963 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06003964 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
3965}
3966
3967static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
3968 int wake_flags, void *key)
3969{
3970 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
3971 wq);
3972
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003973 /* use noflush == true, as we can't safely rely on locking context */
3974 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06003975 return -1;
3976
3977 return autoremove_wake_function(curr, mode, wake_flags, key);
3978}
3979
Jens Axboe2b188cc2019-01-07 10:46:33 -07003980/*
3981 * Wait until events become available, if we don't already have some. The
3982 * application must reap them itself, as they reside on the shared cq ring.
3983 */
3984static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
3985 const sigset_t __user *sig, size_t sigsz)
3986{
Jens Axboebda52162019-09-24 13:47:15 -06003987 struct io_wait_queue iowq = {
3988 .wq = {
3989 .private = current,
3990 .func = io_wake_function,
3991 .entry = LIST_HEAD_INIT(iowq.wq.entry),
3992 },
3993 .ctx = ctx,
3994 .to_wait = min_events,
3995 };
Hristo Venev75b28af2019-08-26 17:23:46 +00003996 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08003997 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003998
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003999 if (io_cqring_events(ctx, false) >= min_events)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004000 return 0;
4001
4002 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01004003#ifdef CONFIG_COMPAT
4004 if (in_compat_syscall())
4005 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07004006 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01004007 else
4008#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07004009 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01004010
Jens Axboe2b188cc2019-01-07 10:46:33 -07004011 if (ret)
4012 return ret;
4013 }
4014
Jens Axboebda52162019-09-24 13:47:15 -06004015 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004016 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06004017 do {
4018 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
4019 TASK_INTERRUPTIBLE);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004020 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06004021 break;
4022 schedule();
4023 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08004024 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06004025 break;
4026 }
4027 } while (1);
4028 finish_wait(&ctx->wait, &iowq.wq);
4029
Jackie Liue9ffa5c2019-10-29 11:16:42 +08004030 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004031
Hristo Venev75b28af2019-08-26 17:23:46 +00004032 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004033}
4034
Jens Axboe6b063142019-01-10 22:13:58 -07004035static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
4036{
4037#if defined(CONFIG_UNIX)
4038 if (ctx->ring_sock) {
4039 struct sock *sock = ctx->ring_sock->sk;
4040 struct sk_buff *skb;
4041
4042 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
4043 kfree_skb(skb);
4044 }
4045#else
4046 int i;
4047
Jens Axboe65e19f52019-10-26 07:20:21 -06004048 for (i = 0; i < ctx->nr_user_files; i++) {
4049 struct file *file;
4050
4051 file = io_file_from_index(ctx, i);
4052 if (file)
4053 fput(file);
4054 }
Jens Axboe6b063142019-01-10 22:13:58 -07004055#endif
4056}
4057
4058static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
4059{
Jens Axboe65e19f52019-10-26 07:20:21 -06004060 unsigned nr_tables, i;
4061
4062 if (!ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07004063 return -ENXIO;
4064
4065 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06004066 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
4067 for (i = 0; i < nr_tables; i++)
4068 kfree(ctx->file_table[i].files);
4069 kfree(ctx->file_table);
4070 ctx->file_table = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07004071 ctx->nr_user_files = 0;
4072 return 0;
4073}
4074
Jens Axboe6c271ce2019-01-10 11:22:30 -07004075static void io_sq_thread_stop(struct io_ring_ctx *ctx)
4076{
4077 if (ctx->sqo_thread) {
Jens Axboe206aefd2019-11-07 18:27:42 -07004078 wait_for_completion(&ctx->completions[1]);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02004079 /*
4080 * The park is a bit of a work-around, without it we get
4081 * warning spews on shutdown with SQPOLL set and affinity
4082 * set to a single CPU.
4083 */
Jens Axboe06058632019-04-13 09:26:03 -06004084 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004085 kthread_stop(ctx->sqo_thread);
4086 ctx->sqo_thread = NULL;
4087 }
4088}
4089
Jens Axboe6b063142019-01-10 22:13:58 -07004090static void io_finish_async(struct io_ring_ctx *ctx)
4091{
Jens Axboe6c271ce2019-01-10 11:22:30 -07004092 io_sq_thread_stop(ctx);
4093
Jens Axboe561fb042019-10-24 07:25:42 -06004094 if (ctx->io_wq) {
4095 io_wq_destroy(ctx->io_wq);
4096 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07004097 }
4098}
4099
4100#if defined(CONFIG_UNIX)
4101static void io_destruct_skb(struct sk_buff *skb)
4102{
4103 struct io_ring_ctx *ctx = skb->sk->sk_user_data;
4104
Jens Axboe561fb042019-10-24 07:25:42 -06004105 if (ctx->io_wq)
4106 io_wq_flush(ctx->io_wq);
Jens Axboe8a997342019-10-09 14:40:13 -06004107
Jens Axboe6b063142019-01-10 22:13:58 -07004108 unix_destruct_scm(skb);
4109}
4110
4111/*
4112 * Ensure the UNIX gc is aware of our file set, so we are certain that
4113 * the io_uring can be safely unregistered on process exit, even if we have
4114 * loops in the file referencing.
4115 */
4116static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
4117{
4118 struct sock *sk = ctx->ring_sock->sk;
4119 struct scm_fp_list *fpl;
4120 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06004121 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07004122
4123 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
4124 unsigned long inflight = ctx->user->unix_inflight + nr;
4125
4126 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
4127 return -EMFILE;
4128 }
4129
4130 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
4131 if (!fpl)
4132 return -ENOMEM;
4133
4134 skb = alloc_skb(0, GFP_KERNEL);
4135 if (!skb) {
4136 kfree(fpl);
4137 return -ENOMEM;
4138 }
4139
4140 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07004141
Jens Axboe08a45172019-10-03 08:11:03 -06004142 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07004143 fpl->user = get_uid(ctx->user);
4144 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06004145 struct file *file = io_file_from_index(ctx, i + offset);
4146
4147 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06004148 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06004149 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06004150 unix_inflight(fpl->user, fpl->fp[nr_files]);
4151 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07004152 }
4153
Jens Axboe08a45172019-10-03 08:11:03 -06004154 if (nr_files) {
4155 fpl->max = SCM_MAX_FD;
4156 fpl->count = nr_files;
4157 UNIXCB(skb).fp = fpl;
4158 skb->destructor = io_destruct_skb;
4159 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
4160 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07004161
Jens Axboe08a45172019-10-03 08:11:03 -06004162 for (i = 0; i < nr_files; i++)
4163 fput(fpl->fp[i]);
4164 } else {
4165 kfree_skb(skb);
4166 kfree(fpl);
4167 }
Jens Axboe6b063142019-01-10 22:13:58 -07004168
4169 return 0;
4170}
4171
4172/*
4173 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
4174 * causes regular reference counting to break down. We rely on the UNIX
4175 * garbage collection to take care of this problem for us.
4176 */
4177static int io_sqe_files_scm(struct io_ring_ctx *ctx)
4178{
4179 unsigned left, total;
4180 int ret = 0;
4181
4182 total = 0;
4183 left = ctx->nr_user_files;
4184 while (left) {
4185 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07004186
4187 ret = __io_sqe_files_scm(ctx, this_files, total);
4188 if (ret)
4189 break;
4190 left -= this_files;
4191 total += this_files;
4192 }
4193
4194 if (!ret)
4195 return 0;
4196
4197 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06004198 struct file *file = io_file_from_index(ctx, total);
4199
4200 if (file)
4201 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07004202 total++;
4203 }
4204
4205 return ret;
4206}
4207#else
4208static int io_sqe_files_scm(struct io_ring_ctx *ctx)
4209{
4210 return 0;
4211}
4212#endif
4213
Jens Axboe65e19f52019-10-26 07:20:21 -06004214static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
4215 unsigned nr_files)
4216{
4217 int i;
4218
4219 for (i = 0; i < nr_tables; i++) {
4220 struct fixed_file_table *table = &ctx->file_table[i];
4221 unsigned this_files;
4222
4223 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
4224 table->files = kcalloc(this_files, sizeof(struct file *),
4225 GFP_KERNEL);
4226 if (!table->files)
4227 break;
4228 nr_files -= this_files;
4229 }
4230
4231 if (i == nr_tables)
4232 return 0;
4233
4234 for (i = 0; i < nr_tables; i++) {
4235 struct fixed_file_table *table = &ctx->file_table[i];
4236 kfree(table->files);
4237 }
4238 return 1;
4239}
4240
Jens Axboe6b063142019-01-10 22:13:58 -07004241static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
4242 unsigned nr_args)
4243{
4244 __s32 __user *fds = (__s32 __user *) arg;
Jens Axboe65e19f52019-10-26 07:20:21 -06004245 unsigned nr_tables;
Jens Axboe6b063142019-01-10 22:13:58 -07004246 int fd, ret = 0;
4247 unsigned i;
4248
Jens Axboe65e19f52019-10-26 07:20:21 -06004249 if (ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07004250 return -EBUSY;
4251 if (!nr_args)
4252 return -EINVAL;
4253 if (nr_args > IORING_MAX_FIXED_FILES)
4254 return -EMFILE;
4255
Jens Axboe65e19f52019-10-26 07:20:21 -06004256 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
4257 ctx->file_table = kcalloc(nr_tables, sizeof(struct fixed_file_table),
4258 GFP_KERNEL);
4259 if (!ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07004260 return -ENOMEM;
4261
Jens Axboe65e19f52019-10-26 07:20:21 -06004262 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
4263 kfree(ctx->file_table);
Jens Axboe46568e92019-11-10 08:40:53 -07004264 ctx->file_table = NULL;
Jens Axboe65e19f52019-10-26 07:20:21 -06004265 return -ENOMEM;
4266 }
4267
Jens Axboe08a45172019-10-03 08:11:03 -06004268 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06004269 struct fixed_file_table *table;
4270 unsigned index;
4271
Jens Axboe6b063142019-01-10 22:13:58 -07004272 ret = -EFAULT;
4273 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
4274 break;
Jens Axboe08a45172019-10-03 08:11:03 -06004275 /* allow sparse sets */
4276 if (fd == -1) {
4277 ret = 0;
4278 continue;
4279 }
Jens Axboe6b063142019-01-10 22:13:58 -07004280
Jens Axboe65e19f52019-10-26 07:20:21 -06004281 table = &ctx->file_table[i >> IORING_FILE_TABLE_SHIFT];
4282 index = i & IORING_FILE_TABLE_MASK;
4283 table->files[index] = fget(fd);
Jens Axboe6b063142019-01-10 22:13:58 -07004284
4285 ret = -EBADF;
Jens Axboe65e19f52019-10-26 07:20:21 -06004286 if (!table->files[index])
Jens Axboe6b063142019-01-10 22:13:58 -07004287 break;
4288 /*
4289 * Don't allow io_uring instances to be registered. If UNIX
4290 * isn't enabled, then this causes a reference cycle and this
4291 * instance can never get freed. If UNIX is enabled we'll
4292 * handle it just fine, but there's still no point in allowing
4293 * a ring fd as it doesn't support regular read/write anyway.
4294 */
Jens Axboe65e19f52019-10-26 07:20:21 -06004295 if (table->files[index]->f_op == &io_uring_fops) {
4296 fput(table->files[index]);
Jens Axboe6b063142019-01-10 22:13:58 -07004297 break;
4298 }
Jens Axboe6b063142019-01-10 22:13:58 -07004299 ret = 0;
4300 }
4301
4302 if (ret) {
Jens Axboe65e19f52019-10-26 07:20:21 -06004303 for (i = 0; i < ctx->nr_user_files; i++) {
4304 struct file *file;
Jens Axboe6b063142019-01-10 22:13:58 -07004305
Jens Axboe65e19f52019-10-26 07:20:21 -06004306 file = io_file_from_index(ctx, i);
4307 if (file)
4308 fput(file);
4309 }
4310 for (i = 0; i < nr_tables; i++)
4311 kfree(ctx->file_table[i].files);
4312
4313 kfree(ctx->file_table);
4314 ctx->file_table = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07004315 ctx->nr_user_files = 0;
4316 return ret;
4317 }
4318
4319 ret = io_sqe_files_scm(ctx);
4320 if (ret)
4321 io_sqe_files_unregister(ctx);
4322
4323 return ret;
4324}
4325
Jens Axboec3a31e62019-10-03 13:59:56 -06004326static void io_sqe_file_unregister(struct io_ring_ctx *ctx, int index)
4327{
4328#if defined(CONFIG_UNIX)
Jens Axboe65e19f52019-10-26 07:20:21 -06004329 struct file *file = io_file_from_index(ctx, index);
Jens Axboec3a31e62019-10-03 13:59:56 -06004330 struct sock *sock = ctx->ring_sock->sk;
4331 struct sk_buff_head list, *head = &sock->sk_receive_queue;
4332 struct sk_buff *skb;
4333 int i;
4334
4335 __skb_queue_head_init(&list);
4336
4337 /*
4338 * Find the skb that holds this file in its SCM_RIGHTS. When found,
4339 * remove this entry and rearrange the file array.
4340 */
4341 skb = skb_dequeue(head);
4342 while (skb) {
4343 struct scm_fp_list *fp;
4344
4345 fp = UNIXCB(skb).fp;
4346 for (i = 0; i < fp->count; i++) {
4347 int left;
4348
4349 if (fp->fp[i] != file)
4350 continue;
4351
4352 unix_notinflight(fp->user, fp->fp[i]);
4353 left = fp->count - 1 - i;
4354 if (left) {
4355 memmove(&fp->fp[i], &fp->fp[i + 1],
4356 left * sizeof(struct file *));
4357 }
4358 fp->count--;
4359 if (!fp->count) {
4360 kfree_skb(skb);
4361 skb = NULL;
4362 } else {
4363 __skb_queue_tail(&list, skb);
4364 }
4365 fput(file);
4366 file = NULL;
4367 break;
4368 }
4369
4370 if (!file)
4371 break;
4372
4373 __skb_queue_tail(&list, skb);
4374
4375 skb = skb_dequeue(head);
4376 }
4377
4378 if (skb_peek(&list)) {
4379 spin_lock_irq(&head->lock);
4380 while ((skb = __skb_dequeue(&list)) != NULL)
4381 __skb_queue_tail(head, skb);
4382 spin_unlock_irq(&head->lock);
4383 }
4384#else
Jens Axboe65e19f52019-10-26 07:20:21 -06004385 fput(io_file_from_index(ctx, index));
Jens Axboec3a31e62019-10-03 13:59:56 -06004386#endif
4387}
4388
4389static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
4390 int index)
4391{
4392#if defined(CONFIG_UNIX)
4393 struct sock *sock = ctx->ring_sock->sk;
4394 struct sk_buff_head *head = &sock->sk_receive_queue;
4395 struct sk_buff *skb;
4396
4397 /*
4398 * See if we can merge this file into an existing skb SCM_RIGHTS
4399 * file set. If there's no room, fall back to allocating a new skb
4400 * and filling it in.
4401 */
4402 spin_lock_irq(&head->lock);
4403 skb = skb_peek(head);
4404 if (skb) {
4405 struct scm_fp_list *fpl = UNIXCB(skb).fp;
4406
4407 if (fpl->count < SCM_MAX_FD) {
4408 __skb_unlink(skb, head);
4409 spin_unlock_irq(&head->lock);
4410 fpl->fp[fpl->count] = get_file(file);
4411 unix_inflight(fpl->user, fpl->fp[fpl->count]);
4412 fpl->count++;
4413 spin_lock_irq(&head->lock);
4414 __skb_queue_head(head, skb);
4415 } else {
4416 skb = NULL;
4417 }
4418 }
4419 spin_unlock_irq(&head->lock);
4420
4421 if (skb) {
4422 fput(file);
4423 return 0;
4424 }
4425
4426 return __io_sqe_files_scm(ctx, 1, index);
4427#else
4428 return 0;
4429#endif
4430}
4431
4432static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
4433 unsigned nr_args)
4434{
4435 struct io_uring_files_update up;
4436 __s32 __user *fds;
4437 int fd, i, err;
4438 __u32 done;
4439
Jens Axboe65e19f52019-10-26 07:20:21 -06004440 if (!ctx->file_table)
Jens Axboec3a31e62019-10-03 13:59:56 -06004441 return -ENXIO;
4442 if (!nr_args)
4443 return -EINVAL;
4444 if (copy_from_user(&up, arg, sizeof(up)))
4445 return -EFAULT;
4446 if (check_add_overflow(up.offset, nr_args, &done))
4447 return -EOVERFLOW;
4448 if (done > ctx->nr_user_files)
4449 return -EINVAL;
4450
4451 done = 0;
4452 fds = (__s32 __user *) up.fds;
4453 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06004454 struct fixed_file_table *table;
4455 unsigned index;
4456
Jens Axboec3a31e62019-10-03 13:59:56 -06004457 err = 0;
4458 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
4459 err = -EFAULT;
4460 break;
4461 }
4462 i = array_index_nospec(up.offset, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06004463 table = &ctx->file_table[i >> IORING_FILE_TABLE_SHIFT];
4464 index = i & IORING_FILE_TABLE_MASK;
4465 if (table->files[index]) {
Jens Axboec3a31e62019-10-03 13:59:56 -06004466 io_sqe_file_unregister(ctx, i);
Jens Axboe65e19f52019-10-26 07:20:21 -06004467 table->files[index] = NULL;
Jens Axboec3a31e62019-10-03 13:59:56 -06004468 }
4469 if (fd != -1) {
4470 struct file *file;
4471
4472 file = fget(fd);
4473 if (!file) {
4474 err = -EBADF;
4475 break;
4476 }
4477 /*
4478 * Don't allow io_uring instances to be registered. If
4479 * UNIX isn't enabled, then this causes a reference
4480 * cycle and this instance can never get freed. If UNIX
4481 * is enabled we'll handle it just fine, but there's
4482 * still no point in allowing a ring fd as it doesn't
4483 * support regular read/write anyway.
4484 */
4485 if (file->f_op == &io_uring_fops) {
4486 fput(file);
4487 err = -EBADF;
4488 break;
4489 }
Jens Axboe65e19f52019-10-26 07:20:21 -06004490 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06004491 err = io_sqe_file_register(ctx, file, i);
4492 if (err)
4493 break;
4494 }
4495 nr_args--;
4496 done++;
4497 up.offset++;
4498 }
4499
4500 return done ? done : err;
4501}
4502
Jens Axboe7d723062019-11-12 22:31:31 -07004503static void io_put_work(struct io_wq_work *work)
4504{
4505 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
4506
4507 io_put_req(req);
4508}
4509
4510static void io_get_work(struct io_wq_work *work)
4511{
4512 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
4513
4514 refcount_inc(&req->refs);
4515}
4516
Jens Axboe6c271ce2019-01-10 11:22:30 -07004517static int io_sq_offload_start(struct io_ring_ctx *ctx,
4518 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004519{
Jens Axboe576a3472019-11-25 08:49:20 -07004520 struct io_wq_data data;
Jens Axboe561fb042019-10-24 07:25:42 -06004521 unsigned concurrency;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004522 int ret;
4523
Jens Axboe6c271ce2019-01-10 11:22:30 -07004524 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004525 mmgrab(current->mm);
4526 ctx->sqo_mm = current->mm;
4527
Jens Axboe6c271ce2019-01-10 11:22:30 -07004528 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06004529 ret = -EPERM;
4530 if (!capable(CAP_SYS_ADMIN))
4531 goto err;
4532
Jens Axboe917257d2019-04-13 09:28:55 -06004533 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
4534 if (!ctx->sq_thread_idle)
4535 ctx->sq_thread_idle = HZ;
4536
Jens Axboe6c271ce2019-01-10 11:22:30 -07004537 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06004538 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004539
Jens Axboe917257d2019-04-13 09:28:55 -06004540 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06004541 if (cpu >= nr_cpu_ids)
4542 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08004543 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06004544 goto err;
4545
Jens Axboe6c271ce2019-01-10 11:22:30 -07004546 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
4547 ctx, cpu,
4548 "io_uring-sq");
4549 } else {
4550 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
4551 "io_uring-sq");
4552 }
4553 if (IS_ERR(ctx->sqo_thread)) {
4554 ret = PTR_ERR(ctx->sqo_thread);
4555 ctx->sqo_thread = NULL;
4556 goto err;
4557 }
4558 wake_up_process(ctx->sqo_thread);
4559 } else if (p->flags & IORING_SETUP_SQ_AFF) {
4560 /* Can't have SQ_AFF without SQPOLL */
4561 ret = -EINVAL;
4562 goto err;
4563 }
4564
Jens Axboe576a3472019-11-25 08:49:20 -07004565 data.mm = ctx->sqo_mm;
4566 data.user = ctx->user;
Jens Axboe181e4482019-11-25 08:52:30 -07004567 data.creds = ctx->creds;
Jens Axboe576a3472019-11-25 08:49:20 -07004568 data.get_work = io_get_work;
4569 data.put_work = io_put_work;
4570
Jens Axboe561fb042019-10-24 07:25:42 -06004571 /* Do QD, or 4 * CPUS, whatever is smallest */
4572 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
Jens Axboe576a3472019-11-25 08:49:20 -07004573 ctx->io_wq = io_wq_create(concurrency, &data);
Jens Axboe975c99a52019-10-30 08:42:56 -06004574 if (IS_ERR(ctx->io_wq)) {
4575 ret = PTR_ERR(ctx->io_wq);
4576 ctx->io_wq = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004577 goto err;
4578 }
4579
4580 return 0;
4581err:
Jens Axboe54a91f32019-09-10 09:15:04 -06004582 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004583 mmdrop(ctx->sqo_mm);
4584 ctx->sqo_mm = NULL;
4585 return ret;
4586}
4587
4588static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
4589{
4590 atomic_long_sub(nr_pages, &user->locked_vm);
4591}
4592
4593static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
4594{
4595 unsigned long page_limit, cur_pages, new_pages;
4596
4597 /* Don't allow more pages than we can safely lock */
4598 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
4599
4600 do {
4601 cur_pages = atomic_long_read(&user->locked_vm);
4602 new_pages = cur_pages + nr_pages;
4603 if (new_pages > page_limit)
4604 return -ENOMEM;
4605 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
4606 new_pages) != cur_pages);
4607
4608 return 0;
4609}
4610
4611static void io_mem_free(void *ptr)
4612{
Mark Rutland52e04ef2019-04-30 17:30:21 +01004613 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004614
Mark Rutland52e04ef2019-04-30 17:30:21 +01004615 if (!ptr)
4616 return;
4617
4618 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004619 if (put_page_testzero(page))
4620 free_compound_page(page);
4621}
4622
4623static void *io_mem_alloc(size_t size)
4624{
4625 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
4626 __GFP_NORETRY;
4627
4628 return (void *) __get_free_pages(gfp_flags, get_order(size));
4629}
4630
Hristo Venev75b28af2019-08-26 17:23:46 +00004631static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
4632 size_t *sq_offset)
4633{
4634 struct io_rings *rings;
4635 size_t off, sq_array_size;
4636
4637 off = struct_size(rings, cqes, cq_entries);
4638 if (off == SIZE_MAX)
4639 return SIZE_MAX;
4640
4641#ifdef CONFIG_SMP
4642 off = ALIGN(off, SMP_CACHE_BYTES);
4643 if (off == 0)
4644 return SIZE_MAX;
4645#endif
4646
4647 sq_array_size = array_size(sizeof(u32), sq_entries);
4648 if (sq_array_size == SIZE_MAX)
4649 return SIZE_MAX;
4650
4651 if (check_add_overflow(off, sq_array_size, &off))
4652 return SIZE_MAX;
4653
4654 if (sq_offset)
4655 *sq_offset = off;
4656
4657 return off;
4658}
4659
Jens Axboe2b188cc2019-01-07 10:46:33 -07004660static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
4661{
Hristo Venev75b28af2019-08-26 17:23:46 +00004662 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004663
Hristo Venev75b28af2019-08-26 17:23:46 +00004664 pages = (size_t)1 << get_order(
4665 rings_size(sq_entries, cq_entries, NULL));
4666 pages += (size_t)1 << get_order(
4667 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07004668
Hristo Venev75b28af2019-08-26 17:23:46 +00004669 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004670}
4671
Jens Axboeedafcce2019-01-09 09:16:05 -07004672static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
4673{
4674 int i, j;
4675
4676 if (!ctx->user_bufs)
4677 return -ENXIO;
4678
4679 for (i = 0; i < ctx->nr_user_bufs; i++) {
4680 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
4681
4682 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbard27c4d3a2019-08-04 19:32:06 -07004683 put_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07004684
4685 if (ctx->account_mem)
4686 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004687 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07004688 imu->nr_bvecs = 0;
4689 }
4690
4691 kfree(ctx->user_bufs);
4692 ctx->user_bufs = NULL;
4693 ctx->nr_user_bufs = 0;
4694 return 0;
4695}
4696
4697static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
4698 void __user *arg, unsigned index)
4699{
4700 struct iovec __user *src;
4701
4702#ifdef CONFIG_COMPAT
4703 if (ctx->compat) {
4704 struct compat_iovec __user *ciovs;
4705 struct compat_iovec ciov;
4706
4707 ciovs = (struct compat_iovec __user *) arg;
4708 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
4709 return -EFAULT;
4710
Jens Axboed55e5f52019-12-11 16:12:15 -07004711 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07004712 dst->iov_len = ciov.iov_len;
4713 return 0;
4714 }
4715#endif
4716 src = (struct iovec __user *) arg;
4717 if (copy_from_user(dst, &src[index], sizeof(*dst)))
4718 return -EFAULT;
4719 return 0;
4720}
4721
4722static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
4723 unsigned nr_args)
4724{
4725 struct vm_area_struct **vmas = NULL;
4726 struct page **pages = NULL;
4727 int i, j, got_pages = 0;
4728 int ret = -EINVAL;
4729
4730 if (ctx->user_bufs)
4731 return -EBUSY;
4732 if (!nr_args || nr_args > UIO_MAXIOV)
4733 return -EINVAL;
4734
4735 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
4736 GFP_KERNEL);
4737 if (!ctx->user_bufs)
4738 return -ENOMEM;
4739
4740 for (i = 0; i < nr_args; i++) {
4741 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
4742 unsigned long off, start, end, ubuf;
4743 int pret, nr_pages;
4744 struct iovec iov;
4745 size_t size;
4746
4747 ret = io_copy_iov(ctx, &iov, arg, i);
4748 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03004749 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07004750
4751 /*
4752 * Don't impose further limits on the size and buffer
4753 * constraints here, we'll -EINVAL later when IO is
4754 * submitted if they are wrong.
4755 */
4756 ret = -EFAULT;
4757 if (!iov.iov_base || !iov.iov_len)
4758 goto err;
4759
4760 /* arbitrary limit, but we need something */
4761 if (iov.iov_len > SZ_1G)
4762 goto err;
4763
4764 ubuf = (unsigned long) iov.iov_base;
4765 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
4766 start = ubuf >> PAGE_SHIFT;
4767 nr_pages = end - start;
4768
4769 if (ctx->account_mem) {
4770 ret = io_account_mem(ctx->user, nr_pages);
4771 if (ret)
4772 goto err;
4773 }
4774
4775 ret = 0;
4776 if (!pages || nr_pages > got_pages) {
4777 kfree(vmas);
4778 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004779 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07004780 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004781 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07004782 sizeof(struct vm_area_struct *),
4783 GFP_KERNEL);
4784 if (!pages || !vmas) {
4785 ret = -ENOMEM;
4786 if (ctx->account_mem)
4787 io_unaccount_mem(ctx->user, nr_pages);
4788 goto err;
4789 }
4790 got_pages = nr_pages;
4791 }
4792
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004793 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07004794 GFP_KERNEL);
4795 ret = -ENOMEM;
4796 if (!imu->bvec) {
4797 if (ctx->account_mem)
4798 io_unaccount_mem(ctx->user, nr_pages);
4799 goto err;
4800 }
4801
4802 ret = 0;
4803 down_read(&current->mm->mmap_sem);
Ira Weiny932f4a62019-05-13 17:17:03 -07004804 pret = get_user_pages(ubuf, nr_pages,
4805 FOLL_WRITE | FOLL_LONGTERM,
4806 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07004807 if (pret == nr_pages) {
4808 /* don't support file backed memory */
4809 for (j = 0; j < nr_pages; j++) {
4810 struct vm_area_struct *vma = vmas[j];
4811
4812 if (vma->vm_file &&
4813 !is_file_hugepages(vma->vm_file)) {
4814 ret = -EOPNOTSUPP;
4815 break;
4816 }
4817 }
4818 } else {
4819 ret = pret < 0 ? pret : -EFAULT;
4820 }
4821 up_read(&current->mm->mmap_sem);
4822 if (ret) {
4823 /*
4824 * if we did partial map, or found file backed vmas,
4825 * release any pages we did get
4826 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07004827 if (pret > 0)
4828 put_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07004829 if (ctx->account_mem)
4830 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004831 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07004832 goto err;
4833 }
4834
4835 off = ubuf & ~PAGE_MASK;
4836 size = iov.iov_len;
4837 for (j = 0; j < nr_pages; j++) {
4838 size_t vec_len;
4839
4840 vec_len = min_t(size_t, size, PAGE_SIZE - off);
4841 imu->bvec[j].bv_page = pages[j];
4842 imu->bvec[j].bv_len = vec_len;
4843 imu->bvec[j].bv_offset = off;
4844 off = 0;
4845 size -= vec_len;
4846 }
4847 /* store original address for later verification */
4848 imu->ubuf = ubuf;
4849 imu->len = iov.iov_len;
4850 imu->nr_bvecs = nr_pages;
4851
4852 ctx->nr_user_bufs++;
4853 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004854 kvfree(pages);
4855 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07004856 return 0;
4857err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004858 kvfree(pages);
4859 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07004860 io_sqe_buffer_unregister(ctx);
4861 return ret;
4862}
4863
Jens Axboe9b402842019-04-11 11:45:41 -06004864static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
4865{
4866 __s32 __user *fds = arg;
4867 int fd;
4868
4869 if (ctx->cq_ev_fd)
4870 return -EBUSY;
4871
4872 if (copy_from_user(&fd, fds, sizeof(*fds)))
4873 return -EFAULT;
4874
4875 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
4876 if (IS_ERR(ctx->cq_ev_fd)) {
4877 int ret = PTR_ERR(ctx->cq_ev_fd);
4878 ctx->cq_ev_fd = NULL;
4879 return ret;
4880 }
4881
4882 return 0;
4883}
4884
4885static int io_eventfd_unregister(struct io_ring_ctx *ctx)
4886{
4887 if (ctx->cq_ev_fd) {
4888 eventfd_ctx_put(ctx->cq_ev_fd);
4889 ctx->cq_ev_fd = NULL;
4890 return 0;
4891 }
4892
4893 return -ENXIO;
4894}
4895
Jens Axboe2b188cc2019-01-07 10:46:33 -07004896static void io_ring_ctx_free(struct io_ring_ctx *ctx)
4897{
Jens Axboe6b063142019-01-10 22:13:58 -07004898 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004899 if (ctx->sqo_mm)
4900 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07004901
4902 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07004903 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07004904 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06004905 io_eventfd_unregister(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07004906
Jens Axboe2b188cc2019-01-07 10:46:33 -07004907#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07004908 if (ctx->ring_sock) {
4909 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004910 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07004911 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004912#endif
4913
Hristo Venev75b28af2019-08-26 17:23:46 +00004914 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004915 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004916
4917 percpu_ref_exit(&ctx->refs);
4918 if (ctx->account_mem)
4919 io_unaccount_mem(ctx->user,
4920 ring_pages(ctx->sq_entries, ctx->cq_entries));
4921 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07004922 put_cred(ctx->creds);
Jens Axboe206aefd2019-11-07 18:27:42 -07004923 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -07004924 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07004925 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004926 kfree(ctx);
4927}
4928
4929static __poll_t io_uring_poll(struct file *file, poll_table *wait)
4930{
4931 struct io_ring_ctx *ctx = file->private_data;
4932 __poll_t mask = 0;
4933
4934 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02004935 /*
4936 * synchronizes with barrier from wq_has_sleeper call in
4937 * io_commit_cqring
4938 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004939 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00004940 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
4941 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004942 mask |= EPOLLOUT | EPOLLWRNORM;
yangerkundaa5de52019-09-24 20:53:34 +08004943 if (READ_ONCE(ctx->rings->cq.head) != ctx->cached_cq_tail)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004944 mask |= EPOLLIN | EPOLLRDNORM;
4945
4946 return mask;
4947}
4948
4949static int io_uring_fasync(int fd, struct file *file, int on)
4950{
4951 struct io_ring_ctx *ctx = file->private_data;
4952
4953 return fasync_helper(fd, file, on, &ctx->cq_fasync);
4954}
4955
4956static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
4957{
4958 mutex_lock(&ctx->uring_lock);
4959 percpu_ref_kill(&ctx->refs);
4960 mutex_unlock(&ctx->uring_lock);
4961
Jens Axboe5262f562019-09-17 12:26:57 -06004962 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004963 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06004964
4965 if (ctx->io_wq)
4966 io_wq_cancel_all(ctx->io_wq);
4967
Jens Axboedef596e2019-01-09 08:59:42 -07004968 io_iopoll_reap_events(ctx);
Jens Axboe15dff282019-11-13 09:09:23 -07004969 /* if we failed setting up the ctx, we might not have any rings */
4970 if (ctx->rings)
4971 io_cqring_overflow_flush(ctx, true);
Jens Axboe206aefd2019-11-07 18:27:42 -07004972 wait_for_completion(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004973 io_ring_ctx_free(ctx);
4974}
4975
4976static int io_uring_release(struct inode *inode, struct file *file)
4977{
4978 struct io_ring_ctx *ctx = file->private_data;
4979
4980 file->private_data = NULL;
4981 io_ring_ctx_wait_and_kill(ctx);
4982 return 0;
4983}
4984
Jens Axboefcb323c2019-10-24 12:39:47 -06004985static void io_uring_cancel_files(struct io_ring_ctx *ctx,
4986 struct files_struct *files)
4987{
4988 struct io_kiocb *req;
4989 DEFINE_WAIT(wait);
4990
4991 while (!list_empty_careful(&ctx->inflight_list)) {
Jens Axboe768134d2019-11-10 20:30:53 -07004992 struct io_kiocb *cancel_req = NULL;
Jens Axboefcb323c2019-10-24 12:39:47 -06004993
4994 spin_lock_irq(&ctx->inflight_lock);
4995 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07004996 if (req->work.files != files)
4997 continue;
4998 /* req is being completed, ignore */
4999 if (!refcount_inc_not_zero(&req->refs))
5000 continue;
5001 cancel_req = req;
5002 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06005003 }
Jens Axboe768134d2019-11-10 20:30:53 -07005004 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06005005 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07005006 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06005007 spin_unlock_irq(&ctx->inflight_lock);
5008
Jens Axboe768134d2019-11-10 20:30:53 -07005009 /* We need to keep going until we don't find a matching req */
5010 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06005011 break;
Bob Liu2f6d9b92019-11-13 18:06:24 +08005012
5013 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
5014 io_put_req(cancel_req);
Jens Axboefcb323c2019-10-24 12:39:47 -06005015 schedule();
5016 }
Jens Axboe768134d2019-11-10 20:30:53 -07005017 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06005018}
5019
5020static int io_uring_flush(struct file *file, void *data)
5021{
5022 struct io_ring_ctx *ctx = file->private_data;
5023
5024 io_uring_cancel_files(ctx, data);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005025 if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) {
5026 io_cqring_overflow_flush(ctx, true);
Jens Axboefcb323c2019-10-24 12:39:47 -06005027 io_wq_cancel_all(ctx->io_wq);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005028 }
Jens Axboefcb323c2019-10-24 12:39:47 -06005029 return 0;
5030}
5031
Roman Penyaev6c5c2402019-11-28 12:53:22 +01005032static void *io_uring_validate_mmap_request(struct file *file,
5033 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005034{
Jens Axboe2b188cc2019-01-07 10:46:33 -07005035 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01005036 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005037 struct page *page;
5038 void *ptr;
5039
5040 switch (offset) {
5041 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00005042 case IORING_OFF_CQ_RING:
5043 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005044 break;
5045 case IORING_OFF_SQES:
5046 ptr = ctx->sq_sqes;
5047 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005048 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01005049 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005050 }
5051
5052 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07005053 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01005054 return ERR_PTR(-EINVAL);
5055
5056 return ptr;
5057}
5058
5059#ifdef CONFIG_MMU
5060
5061static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
5062{
5063 size_t sz = vma->vm_end - vma->vm_start;
5064 unsigned long pfn;
5065 void *ptr;
5066
5067 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
5068 if (IS_ERR(ptr))
5069 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005070
5071 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
5072 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
5073}
5074
Roman Penyaev6c5c2402019-11-28 12:53:22 +01005075#else /* !CONFIG_MMU */
5076
5077static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
5078{
5079 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
5080}
5081
5082static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
5083{
5084 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
5085}
5086
5087static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
5088 unsigned long addr, unsigned long len,
5089 unsigned long pgoff, unsigned long flags)
5090{
5091 void *ptr;
5092
5093 ptr = io_uring_validate_mmap_request(file, pgoff, len);
5094 if (IS_ERR(ptr))
5095 return PTR_ERR(ptr);
5096
5097 return (unsigned long) ptr;
5098}
5099
5100#endif /* !CONFIG_MMU */
5101
Jens Axboe2b188cc2019-01-07 10:46:33 -07005102SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
5103 u32, min_complete, u32, flags, const sigset_t __user *, sig,
5104 size_t, sigsz)
5105{
5106 struct io_ring_ctx *ctx;
5107 long ret = -EBADF;
5108 int submitted = 0;
5109 struct fd f;
5110
Jens Axboe6c271ce2019-01-10 11:22:30 -07005111 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07005112 return -EINVAL;
5113
5114 f = fdget(fd);
5115 if (!f.file)
5116 return -EBADF;
5117
5118 ret = -EOPNOTSUPP;
5119 if (f.file->f_op != &io_uring_fops)
5120 goto out_fput;
5121
5122 ret = -ENXIO;
5123 ctx = f.file->private_data;
5124 if (!percpu_ref_tryget(&ctx->refs))
5125 goto out_fput;
5126
Jens Axboe6c271ce2019-01-10 11:22:30 -07005127 /*
5128 * For SQ polling, the thread will do all submissions and completions.
5129 * Just return the requested submit count, and wake the thread if
5130 * we were asked to.
5131 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06005132 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005133 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07005134 if (!list_empty_careful(&ctx->cq_overflow_list))
5135 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005136 if (flags & IORING_ENTER_SQ_WAKEUP)
5137 wake_up(&ctx->sqo_wait);
5138 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06005139 } else if (to_submit) {
Pavel Begunkovae9428c2019-11-06 00:22:14 +03005140 struct mm_struct *cur_mm;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005141
Pavel Begunkovae9428c2019-11-06 00:22:14 +03005142 to_submit = min(to_submit, ctx->sq_entries);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005143 mutex_lock(&ctx->uring_lock);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03005144 /* already have mm, so io_submit_sqes() won't try to grab it */
5145 cur_mm = ctx->sqo_mm;
5146 submitted = io_submit_sqes(ctx, to_submit, f.file, fd,
5147 &cur_mm, false);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005148 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03005149
5150 if (submitted != to_submit)
5151 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005152 }
5153 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07005154 unsigned nr_events = 0;
5155
Jens Axboe2b188cc2019-01-07 10:46:33 -07005156 min_complete = min(min_complete, ctx->cq_entries);
5157
Jens Axboedef596e2019-01-09 08:59:42 -07005158 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07005159 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07005160 } else {
5161 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
5162 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07005163 }
5164
Pavel Begunkov7c504e652019-12-18 19:53:45 +03005165out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03005166 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005167out_fput:
5168 fdput(f);
5169 return submitted ? submitted : ret;
5170}
5171
5172static const struct file_operations io_uring_fops = {
5173 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06005174 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07005175 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01005176#ifndef CONFIG_MMU
5177 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
5178 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
5179#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07005180 .poll = io_uring_poll,
5181 .fasync = io_uring_fasync,
5182};
5183
5184static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
5185 struct io_uring_params *p)
5186{
Hristo Venev75b28af2019-08-26 17:23:46 +00005187 struct io_rings *rings;
5188 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005189
Hristo Venev75b28af2019-08-26 17:23:46 +00005190 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
5191 if (size == SIZE_MAX)
5192 return -EOVERFLOW;
5193
5194 rings = io_mem_alloc(size);
5195 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005196 return -ENOMEM;
5197
Hristo Venev75b28af2019-08-26 17:23:46 +00005198 ctx->rings = rings;
5199 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
5200 rings->sq_ring_mask = p->sq_entries - 1;
5201 rings->cq_ring_mask = p->cq_entries - 1;
5202 rings->sq_ring_entries = p->sq_entries;
5203 rings->cq_ring_entries = p->cq_entries;
5204 ctx->sq_mask = rings->sq_ring_mask;
5205 ctx->cq_mask = rings->cq_ring_mask;
5206 ctx->sq_entries = rings->sq_ring_entries;
5207 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005208
5209 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07005210 if (size == SIZE_MAX) {
5211 io_mem_free(ctx->rings);
5212 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005213 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07005214 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07005215
5216 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07005217 if (!ctx->sq_sqes) {
5218 io_mem_free(ctx->rings);
5219 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005220 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07005221 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07005222
Jens Axboe2b188cc2019-01-07 10:46:33 -07005223 return 0;
5224}
5225
5226/*
5227 * Allocate an anonymous fd, this is what constitutes the application
5228 * visible backing of an io_uring instance. The application mmaps this
5229 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
5230 * we have to tie this fd to a socket for file garbage collection purposes.
5231 */
5232static int io_uring_get_fd(struct io_ring_ctx *ctx)
5233{
5234 struct file *file;
5235 int ret;
5236
5237#if defined(CONFIG_UNIX)
5238 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
5239 &ctx->ring_sock);
5240 if (ret)
5241 return ret;
5242#endif
5243
5244 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
5245 if (ret < 0)
5246 goto err;
5247
5248 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
5249 O_RDWR | O_CLOEXEC);
5250 if (IS_ERR(file)) {
5251 put_unused_fd(ret);
5252 ret = PTR_ERR(file);
5253 goto err;
5254 }
5255
5256#if defined(CONFIG_UNIX)
5257 ctx->ring_sock->file = file;
Jens Axboe6b063142019-01-10 22:13:58 -07005258 ctx->ring_sock->sk->sk_user_data = ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005259#endif
5260 fd_install(ret, file);
5261 return ret;
5262err:
5263#if defined(CONFIG_UNIX)
5264 sock_release(ctx->ring_sock);
5265 ctx->ring_sock = NULL;
5266#endif
5267 return ret;
5268}
5269
5270static int io_uring_create(unsigned entries, struct io_uring_params *p)
5271{
5272 struct user_struct *user = NULL;
5273 struct io_ring_ctx *ctx;
5274 bool account_mem;
5275 int ret;
5276
5277 if (!entries || entries > IORING_MAX_ENTRIES)
5278 return -EINVAL;
5279
5280 /*
5281 * Use twice as many entries for the CQ ring. It's possible for the
5282 * application to drive a higher depth than the size of the SQ ring,
5283 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06005284 * some flexibility in overcommitting a bit. If the application has
5285 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
5286 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07005287 */
5288 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06005289 if (p->flags & IORING_SETUP_CQSIZE) {
5290 /*
5291 * If IORING_SETUP_CQSIZE is set, we do the same roundup
5292 * to a power-of-two, if it isn't already. We do NOT impose
5293 * any cq vs sq ring sizing.
5294 */
5295 if (p->cq_entries < p->sq_entries || p->cq_entries > IORING_MAX_CQ_ENTRIES)
5296 return -EINVAL;
5297 p->cq_entries = roundup_pow_of_two(p->cq_entries);
5298 } else {
5299 p->cq_entries = 2 * p->sq_entries;
5300 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07005301
5302 user = get_uid(current_user());
5303 account_mem = !capable(CAP_IPC_LOCK);
5304
5305 if (account_mem) {
5306 ret = io_account_mem(user,
5307 ring_pages(p->sq_entries, p->cq_entries));
5308 if (ret) {
5309 free_uid(user);
5310 return ret;
5311 }
5312 }
5313
5314 ctx = io_ring_ctx_alloc(p);
5315 if (!ctx) {
5316 if (account_mem)
5317 io_unaccount_mem(user, ring_pages(p->sq_entries,
5318 p->cq_entries));
5319 free_uid(user);
5320 return -ENOMEM;
5321 }
5322 ctx->compat = in_compat_syscall();
5323 ctx->account_mem = account_mem;
5324 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07005325 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07005326
5327 ret = io_allocate_scq_urings(ctx, p);
5328 if (ret)
5329 goto err;
5330
Jens Axboe6c271ce2019-01-10 11:22:30 -07005331 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005332 if (ret)
5333 goto err;
5334
Jens Axboe2b188cc2019-01-07 10:46:33 -07005335 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00005336 p->sq_off.head = offsetof(struct io_rings, sq.head);
5337 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
5338 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
5339 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
5340 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
5341 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
5342 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005343
5344 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00005345 p->cq_off.head = offsetof(struct io_rings, cq.head);
5346 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
5347 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
5348 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
5349 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
5350 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06005351
Jens Axboe044c1ab2019-10-28 09:15:33 -06005352 /*
5353 * Install ring fd as the very last thing, so we don't risk someone
5354 * having closed it before we finish setup
5355 */
5356 ret = io_uring_get_fd(ctx);
5357 if (ret < 0)
5358 goto err;
5359
Jens Axboeda8c9692019-12-02 18:51:26 -07005360 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
5361 IORING_FEAT_SUBMIT_STABLE;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02005362 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005363 return ret;
5364err:
5365 io_ring_ctx_wait_and_kill(ctx);
5366 return ret;
5367}
5368
5369/*
5370 * Sets up an aio uring context, and returns the fd. Applications asks for a
5371 * ring size, we return the actual sq/cq ring sizes (among other things) in the
5372 * params structure passed in.
5373 */
5374static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
5375{
5376 struct io_uring_params p;
5377 long ret;
5378 int i;
5379
5380 if (copy_from_user(&p, params, sizeof(p)))
5381 return -EFAULT;
5382 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
5383 if (p.resv[i])
5384 return -EINVAL;
5385 }
5386
Jens Axboe6c271ce2019-01-10 11:22:30 -07005387 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe33a107f2019-10-04 12:10:03 -06005388 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE))
Jens Axboe2b188cc2019-01-07 10:46:33 -07005389 return -EINVAL;
5390
5391 ret = io_uring_create(entries, &p);
5392 if (ret < 0)
5393 return ret;
5394
5395 if (copy_to_user(params, &p, sizeof(p)))
5396 return -EFAULT;
5397
5398 return ret;
5399}
5400
5401SYSCALL_DEFINE2(io_uring_setup, u32, entries,
5402 struct io_uring_params __user *, params)
5403{
5404 return io_uring_setup(entries, params);
5405}
5406
Jens Axboeedafcce2019-01-09 09:16:05 -07005407static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
5408 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06005409 __releases(ctx->uring_lock)
5410 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07005411{
5412 int ret;
5413
Jens Axboe35fa71a2019-04-22 10:23:23 -06005414 /*
5415 * We're inside the ring mutex, if the ref is already dying, then
5416 * someone else killed the ctx or is already going through
5417 * io_uring_register().
5418 */
5419 if (percpu_ref_is_dying(&ctx->refs))
5420 return -ENXIO;
5421
Jens Axboeedafcce2019-01-09 09:16:05 -07005422 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06005423
5424 /*
5425 * Drop uring mutex before waiting for references to exit. If another
5426 * thread is currently inside io_uring_enter() it might need to grab
5427 * the uring_lock to make progress. If we hold it here across the drain
5428 * wait, then we can deadlock. It's safe to drop the mutex here, since
5429 * no new references will come in after we've killed the percpu ref.
5430 */
5431 mutex_unlock(&ctx->uring_lock);
Jens Axboe206aefd2019-11-07 18:27:42 -07005432 wait_for_completion(&ctx->completions[0]);
Jens Axboeb19062a2019-04-15 10:49:38 -06005433 mutex_lock(&ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07005434
5435 switch (opcode) {
5436 case IORING_REGISTER_BUFFERS:
5437 ret = io_sqe_buffer_register(ctx, arg, nr_args);
5438 break;
5439 case IORING_UNREGISTER_BUFFERS:
5440 ret = -EINVAL;
5441 if (arg || nr_args)
5442 break;
5443 ret = io_sqe_buffer_unregister(ctx);
5444 break;
Jens Axboe6b063142019-01-10 22:13:58 -07005445 case IORING_REGISTER_FILES:
5446 ret = io_sqe_files_register(ctx, arg, nr_args);
5447 break;
5448 case IORING_UNREGISTER_FILES:
5449 ret = -EINVAL;
5450 if (arg || nr_args)
5451 break;
5452 ret = io_sqe_files_unregister(ctx);
5453 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06005454 case IORING_REGISTER_FILES_UPDATE:
5455 ret = io_sqe_files_update(ctx, arg, nr_args);
5456 break;
Jens Axboe9b402842019-04-11 11:45:41 -06005457 case IORING_REGISTER_EVENTFD:
5458 ret = -EINVAL;
5459 if (nr_args != 1)
5460 break;
5461 ret = io_eventfd_register(ctx, arg);
5462 break;
5463 case IORING_UNREGISTER_EVENTFD:
5464 ret = -EINVAL;
5465 if (arg || nr_args)
5466 break;
5467 ret = io_eventfd_unregister(ctx);
5468 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07005469 default:
5470 ret = -EINVAL;
5471 break;
5472 }
5473
5474 /* bring the ctx back to life */
Jens Axboe206aefd2019-11-07 18:27:42 -07005475 reinit_completion(&ctx->completions[0]);
Jens Axboeedafcce2019-01-09 09:16:05 -07005476 percpu_ref_reinit(&ctx->refs);
5477 return ret;
5478}
5479
5480SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
5481 void __user *, arg, unsigned int, nr_args)
5482{
5483 struct io_ring_ctx *ctx;
5484 long ret = -EBADF;
5485 struct fd f;
5486
5487 f = fdget(fd);
5488 if (!f.file)
5489 return -EBADF;
5490
5491 ret = -EOPNOTSUPP;
5492 if (f.file->f_op != &io_uring_fops)
5493 goto out_fput;
5494
5495 ctx = f.file->private_data;
5496
5497 mutex_lock(&ctx->uring_lock);
5498 ret = __io_uring_register(ctx, opcode, arg, nr_args);
5499 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02005500 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
5501 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07005502out_fput:
5503 fdput(f);
5504 return ret;
5505}
5506
Jens Axboe2b188cc2019-01-07 10:46:33 -07005507static int __init io_uring_init(void)
5508{
5509 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
5510 return 0;
5511};
5512__initcall(io_uring_init);