blob: 7a4e00ef02beb84be94990dfafdb4573a13afe25 [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;
Jens Axboed63d1b52019-12-10 10:38:56 -0700322 int mode;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700323};
324
Jens Axboefbf23842019-12-17 18:45:56 -0700325struct io_cancel {
326 struct file *file;
327 u64 addr;
328};
329
Jens Axboeb29472e2019-12-17 18:50:29 -0700330struct io_timeout {
331 struct file *file;
332 u64 addr;
333 int flags;
Jens Axboe26a61672019-12-20 09:02:01 -0700334 unsigned count;
Jens Axboeb29472e2019-12-17 18:50:29 -0700335};
336
Jens Axboe9adbd452019-12-20 08:45:55 -0700337struct io_rw {
338 /* NOTE: kiocb has the file as the first member, so don't do it here */
339 struct kiocb kiocb;
340 u64 addr;
341 u64 len;
342};
343
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700344struct io_connect {
345 struct file *file;
346 struct sockaddr __user *addr;
347 int addr_len;
348};
349
Jens Axboee47293f2019-12-20 08:58:21 -0700350struct io_sr_msg {
351 struct file *file;
352 struct user_msghdr __user *msg;
353 int msg_flags;
354};
355
Jens Axboef499a022019-12-02 16:28:46 -0700356struct io_async_connect {
357 struct sockaddr_storage address;
358};
359
Jens Axboe03b12302019-12-02 18:50:25 -0700360struct io_async_msghdr {
361 struct iovec fast_iov[UIO_FASTIOV];
362 struct iovec *iov;
363 struct sockaddr __user *uaddr;
364 struct msghdr msg;
365};
366
Jens Axboef67676d2019-12-02 11:03:47 -0700367struct io_async_rw {
368 struct iovec fast_iov[UIO_FASTIOV];
369 struct iovec *iov;
370 ssize_t nr_segs;
371 ssize_t size;
372};
373
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700374struct io_async_ctx {
Jens Axboef67676d2019-12-02 11:03:47 -0700375 union {
376 struct io_async_rw rw;
Jens Axboe03b12302019-12-02 18:50:25 -0700377 struct io_async_msghdr msg;
Jens Axboef499a022019-12-02 16:28:46 -0700378 struct io_async_connect connect;
Jens Axboe2d283902019-12-04 11:08:05 -0700379 struct io_timeout_data timeout;
Jens Axboef67676d2019-12-02 11:03:47 -0700380 };
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700381};
382
Jens Axboe09bb8392019-03-13 12:39:28 -0600383/*
384 * NOTE! Each of the iocb union members has the file pointer
385 * as the first entry in their struct definition. So you can
386 * access the file pointer through any of the sub-structs,
387 * or directly as just 'ki_filp' in this struct.
388 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700389struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700390 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600391 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700392 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700393 struct io_poll_iocb poll;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700394 struct io_accept accept;
395 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700396 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700397 struct io_timeout timeout;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700398 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700399 struct io_sr_msg sr_msg;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700400 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700401
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700402 struct io_async_ctx *io;
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300403 struct file *ring_file;
404 int ring_fd;
405 bool has_user;
406 bool in_async;
407 bool needs_fixed_file;
Jens Axboed625c6e2019-12-17 19:53:05 -0700408 u8 opcode;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700409
410 struct io_ring_ctx *ctx;
Jens Axboeeac406c2019-11-14 12:09:58 -0700411 union {
412 struct list_head list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700413 struct hlist_node hash_node;
Jens Axboeeac406c2019-11-14 12:09:58 -0700414 };
Jens Axboe9e645e112019-05-10 16:07:28 -0600415 struct list_head link_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700416 unsigned int flags;
Jens Axboec16361c2019-01-17 08:39:48 -0700417 refcount_t refs;
Stefan Bühler8449eed2019-04-27 20:34:19 +0200418#define REQ_F_NOWAIT 1 /* must not punt to workers */
Jens Axboedef596e2019-01-09 08:59:42 -0700419#define REQ_F_IOPOLL_COMPLETED 2 /* polled IO has completed */
Jens Axboe6b063142019-01-10 22:13:58 -0700420#define REQ_F_FIXED_FILE 4 /* ctx owns file */
Jens Axboe4d7dd462019-11-20 13:03:52 -0700421#define REQ_F_LINK_NEXT 8 /* already grabbed next link */
Stefan Bühlere2033e32019-05-11 19:08:01 +0200422#define REQ_F_IO_DRAIN 16 /* drain existing IO first */
423#define REQ_F_IO_DRAINED 32 /* drain done */
Jens Axboe9e645e112019-05-10 16:07:28 -0600424#define REQ_F_LINK 64 /* linked sqes */
Jens Axboe2665abf2019-11-05 12:40:47 -0700425#define REQ_F_LINK_TIMEOUT 128 /* has linked timeout */
Zhengyuan Liuf7b76ac2019-07-16 23:26:14 +0800426#define REQ_F_FAIL_LINK 256 /* fail rest of links */
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +0300427#define REQ_F_DRAIN_LINK 512 /* link should be fully drained */
Jens Axboe5262f562019-09-17 12:26:57 -0600428#define REQ_F_TIMEOUT 1024 /* timeout request */
Jens Axboe491381ce2019-10-17 09:20:46 -0600429#define REQ_F_ISREG 2048 /* regular file */
430#define REQ_F_MUST_PUNT 4096 /* must be punted even for NONBLOCK */
Jens Axboe93bd25b2019-11-11 23:34:31 -0700431#define REQ_F_TIMEOUT_NOSEQ 8192 /* no timeout sequence */
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800432#define REQ_F_INFLIGHT 16384 /* on inflight list */
433#define REQ_F_COMP_LOCKED 32768 /* completion under lock */
Jens Axboe4e88d6e2019-12-07 20:59:47 -0700434#define REQ_F_HARDLINK 65536 /* doesn't sever on completion < 0 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700435 u64 user_data;
Jens Axboe9e645e112019-05-10 16:07:28 -0600436 u32 result;
Jens Axboede0617e2019-04-06 21:51:27 -0600437 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700438
Jens Axboefcb323c2019-10-24 12:39:47 -0600439 struct list_head inflight_entry;
440
Jens Axboe561fb042019-10-24 07:25:42 -0600441 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700442};
443
444#define IO_PLUG_THRESHOLD 2
Jens Axboedef596e2019-01-09 08:59:42 -0700445#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700446
Jens Axboe9a56a232019-01-09 09:06:50 -0700447struct io_submit_state {
448 struct blk_plug plug;
449
450 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700451 * io_kiocb alloc cache
452 */
453 void *reqs[IO_IOPOLL_BATCH];
454 unsigned int free_reqs;
455 unsigned int cur_req;
456
457 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700458 * File reference cache
459 */
460 struct file *file;
461 unsigned int fd;
462 unsigned int has_refs;
463 unsigned int used_refs;
464 unsigned int ios_left;
465};
466
Jens Axboe561fb042019-10-24 07:25:42 -0600467static void io_wq_submit_work(struct io_wq_work **workptr);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700468static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liu4fe2c962019-09-09 20:50:40 +0800469static void __io_free_req(struct io_kiocb *req);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800470static void io_put_req(struct io_kiocb *req);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700471static void io_double_put_req(struct io_kiocb *req);
Jens Axboe978db572019-11-14 22:39:04 -0700472static void __io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700473static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
474static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboede0617e2019-04-06 21:51:27 -0600475
Jens Axboe2b188cc2019-01-07 10:46:33 -0700476static struct kmem_cache *req_cachep;
477
478static const struct file_operations io_uring_fops;
479
480struct sock *io_uring_get_socket(struct file *file)
481{
482#if defined(CONFIG_UNIX)
483 if (file->f_op == &io_uring_fops) {
484 struct io_ring_ctx *ctx = file->private_data;
485
486 return ctx->ring_sock->sk;
487 }
488#endif
489 return NULL;
490}
491EXPORT_SYMBOL(io_uring_get_socket);
492
493static void io_ring_ctx_ref_free(struct percpu_ref *ref)
494{
495 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
496
Jens Axboe206aefd2019-11-07 18:27:42 -0700497 complete(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700498}
499
500static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
501{
502 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -0700503 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700504
505 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
506 if (!ctx)
507 return NULL;
508
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700509 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
510 if (!ctx->fallback_req)
511 goto err;
512
Jens Axboe206aefd2019-11-07 18:27:42 -0700513 ctx->completions = kmalloc(2 * sizeof(struct completion), GFP_KERNEL);
514 if (!ctx->completions)
515 goto err;
516
Jens Axboe78076bb2019-12-04 19:56:40 -0700517 /*
518 * Use 5 bits less than the max cq entries, that should give us around
519 * 32 entries per hash list if totally full and uniformly spread.
520 */
521 hash_bits = ilog2(p->cq_entries);
522 hash_bits -= 5;
523 if (hash_bits <= 0)
524 hash_bits = 1;
525 ctx->cancel_hash_bits = hash_bits;
526 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
527 GFP_KERNEL);
528 if (!ctx->cancel_hash)
529 goto err;
530 __hash_init(ctx->cancel_hash, 1U << hash_bits);
531
Roman Gushchin21482892019-05-07 10:01:48 -0700532 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -0700533 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
534 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700535
536 ctx->flags = p->flags;
537 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700538 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe206aefd2019-11-07 18:27:42 -0700539 init_completion(&ctx->completions[0]);
540 init_completion(&ctx->completions[1]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700541 mutex_init(&ctx->uring_lock);
542 init_waitqueue_head(&ctx->wait);
543 spin_lock_init(&ctx->completion_lock);
Jens Axboedef596e2019-01-09 08:59:42 -0700544 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboede0617e2019-04-06 21:51:27 -0600545 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -0600546 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -0600547 init_waitqueue_head(&ctx->inflight_wait);
548 spin_lock_init(&ctx->inflight_lock);
549 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700550 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -0700551err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700552 if (ctx->fallback_req)
553 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe206aefd2019-11-07 18:27:42 -0700554 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -0700555 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -0700556 kfree(ctx);
557 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700558}
559
Bob Liu9d858b22019-11-13 18:06:25 +0800560static inline bool __req_need_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -0600561{
Jackie Liua197f662019-11-08 08:09:12 -0700562 struct io_ring_ctx *ctx = req->ctx;
563
Jens Axboe498ccd92019-10-25 10:04:25 -0600564 return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped
565 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboede0617e2019-04-06 21:51:27 -0600566}
567
Bob Liu9d858b22019-11-13 18:06:25 +0800568static inline bool req_need_defer(struct io_kiocb *req)
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600569{
Bob Liu9d858b22019-11-13 18:06:25 +0800570 if ((req->flags & (REQ_F_IO_DRAIN|REQ_F_IO_DRAINED)) == REQ_F_IO_DRAIN)
571 return __req_need_defer(req);
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600572
Bob Liu9d858b22019-11-13 18:06:25 +0800573 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600574}
575
576static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -0600577{
578 struct io_kiocb *req;
579
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600580 req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list);
Bob Liu9d858b22019-11-13 18:06:25 +0800581 if (req && !req_need_defer(req)) {
Jens Axboede0617e2019-04-06 21:51:27 -0600582 list_del_init(&req->list);
583 return req;
584 }
585
586 return NULL;
587}
588
Jens Axboe5262f562019-09-17 12:26:57 -0600589static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx)
590{
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600591 struct io_kiocb *req;
592
593 req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list);
Jens Axboe93bd25b2019-11-11 23:34:31 -0700594 if (req) {
595 if (req->flags & REQ_F_TIMEOUT_NOSEQ)
596 return NULL;
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800597 if (!__req_need_defer(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -0700598 list_del_init(&req->list);
599 return req;
600 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600601 }
602
603 return NULL;
Jens Axboe5262f562019-09-17 12:26:57 -0600604}
605
Jens Axboede0617e2019-04-06 21:51:27 -0600606static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700607{
Hristo Venev75b28af2019-08-26 17:23:46 +0000608 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700609
Hristo Venev75b28af2019-08-26 17:23:46 +0000610 if (ctx->cached_cq_tail != READ_ONCE(rings->cq.tail)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700611 /* order cqe stores with ring update */
Hristo Venev75b28af2019-08-26 17:23:46 +0000612 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700613
Jens Axboe2b188cc2019-01-07 10:46:33 -0700614 if (wq_has_sleeper(&ctx->cq_wait)) {
615 wake_up_interruptible(&ctx->cq_wait);
616 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
617 }
618 }
619}
620
Jens Axboed625c6e2019-12-17 19:53:05 -0700621static inline bool io_req_needs_user(struct io_kiocb *req)
Jens Axboe18d9be12019-09-10 09:13:05 -0600622{
Jens Axboed625c6e2019-12-17 19:53:05 -0700623 return !(req->opcode == IORING_OP_READ_FIXED ||
624 req->opcode == IORING_OP_WRITE_FIXED);
Jens Axboe561fb042019-10-24 07:25:42 -0600625}
626
Jens Axboe94ae5e72019-11-14 19:39:52 -0700627static inline bool io_prep_async_work(struct io_kiocb *req,
628 struct io_kiocb **link)
Jens Axboe561fb042019-10-24 07:25:42 -0600629{
630 bool do_hashed = false;
Jens Axboe54a91f32019-09-10 09:15:04 -0600631
Jens Axboe3529d8c2019-12-19 18:24:38 -0700632 switch (req->opcode) {
633 case IORING_OP_WRITEV:
634 case IORING_OP_WRITE_FIXED:
635 /* only regular files should be hashed for writes */
636 if (req->flags & REQ_F_ISREG)
637 do_hashed = true;
638 /* fall-through */
639 case IORING_OP_READV:
640 case IORING_OP_READ_FIXED:
641 case IORING_OP_SENDMSG:
642 case IORING_OP_RECVMSG:
643 case IORING_OP_ACCEPT:
644 case IORING_OP_POLL_ADD:
645 case IORING_OP_CONNECT:
646 /*
647 * We know REQ_F_ISREG is not set on some of these
648 * opcodes, but this enables us to keep the check in
649 * just one place.
650 */
651 if (!(req->flags & REQ_F_ISREG))
652 req->work.flags |= IO_WQ_WORK_UNBOUND;
653 break;
Jens Axboe54a91f32019-09-10 09:15:04 -0600654 }
Jens Axboe3529d8c2019-12-19 18:24:38 -0700655 if (io_req_needs_user(req))
656 req->work.flags |= IO_WQ_WORK_NEEDS_USER;
Jens Axboe54a91f32019-09-10 09:15:04 -0600657
Jens Axboe94ae5e72019-11-14 19:39:52 -0700658 *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -0600659 return do_hashed;
660}
661
Jackie Liua197f662019-11-08 08:09:12 -0700662static inline void io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -0600663{
Jackie Liua197f662019-11-08 08:09:12 -0700664 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe94ae5e72019-11-14 19:39:52 -0700665 struct io_kiocb *link;
666 bool do_hashed;
667
668 do_hashed = io_prep_async_work(req, &link);
Jens Axboe561fb042019-10-24 07:25:42 -0600669
670 trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work,
671 req->flags);
672 if (!do_hashed) {
673 io_wq_enqueue(ctx->io_wq, &req->work);
674 } else {
675 io_wq_enqueue_hashed(ctx->io_wq, &req->work,
676 file_inode(req->file));
677 }
Jens Axboe94ae5e72019-11-14 19:39:52 -0700678
679 if (link)
680 io_queue_linked_timeout(link);
Jens Axboe18d9be12019-09-10 09:13:05 -0600681}
682
Jens Axboe5262f562019-09-17 12:26:57 -0600683static void io_kill_timeout(struct io_kiocb *req)
684{
685 int ret;
686
Jens Axboe2d283902019-12-04 11:08:05 -0700687 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe5262f562019-09-17 12:26:57 -0600688 if (ret != -1) {
689 atomic_inc(&req->ctx->cq_timeouts);
Jens Axboe842f9612019-10-29 12:34:10 -0600690 list_del_init(&req->list);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700691 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800692 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -0600693 }
694}
695
696static void io_kill_timeouts(struct io_ring_ctx *ctx)
697{
698 struct io_kiocb *req, *tmp;
699
700 spin_lock_irq(&ctx->completion_lock);
701 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
702 io_kill_timeout(req);
703 spin_unlock_irq(&ctx->completion_lock);
704}
705
Jens Axboede0617e2019-04-06 21:51:27 -0600706static void io_commit_cqring(struct io_ring_ctx *ctx)
707{
708 struct io_kiocb *req;
709
Jens Axboe5262f562019-09-17 12:26:57 -0600710 while ((req = io_get_timeout_req(ctx)) != NULL)
711 io_kill_timeout(req);
712
Jens Axboede0617e2019-04-06 21:51:27 -0600713 __io_commit_cqring(ctx);
714
715 while ((req = io_get_deferred_req(ctx)) != NULL) {
716 req->flags |= REQ_F_IO_DRAINED;
Jackie Liua197f662019-11-08 08:09:12 -0700717 io_queue_async_work(req);
Jens Axboede0617e2019-04-06 21:51:27 -0600718 }
719}
720
Jens Axboe2b188cc2019-01-07 10:46:33 -0700721static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
722{
Hristo Venev75b28af2019-08-26 17:23:46 +0000723 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700724 unsigned tail;
725
726 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +0200727 /*
728 * writes to the cq entry need to come after reading head; the
729 * control dependency is enough as we're using WRITE_ONCE to
730 * fill the cq entry
731 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000732 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700733 return NULL;
734
735 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +0000736 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -0700737}
738
Jens Axboe8c838782019-03-12 15:48:16 -0600739static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
740{
741 if (waitqueue_active(&ctx->wait))
742 wake_up(&ctx->wait);
743 if (waitqueue_active(&ctx->sqo_wait))
744 wake_up(&ctx->sqo_wait);
Jens Axboe9b402842019-04-11 11:45:41 -0600745 if (ctx->cq_ev_fd)
746 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -0600747}
748
Jens Axboec4a2ed72019-11-21 21:01:26 -0700749/* Returns true if there are no backlogged entries after the flush */
750static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700751{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700752 struct io_rings *rings = ctx->rings;
753 struct io_uring_cqe *cqe;
754 struct io_kiocb *req;
755 unsigned long flags;
756 LIST_HEAD(list);
757
758 if (!force) {
759 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -0700760 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700761 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
762 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -0700763 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700764 }
765
766 spin_lock_irqsave(&ctx->completion_lock, flags);
767
768 /* if force is set, the ring is going away. always drop after that */
769 if (force)
770 ctx->cq_overflow_flushed = true;
771
Jens Axboec4a2ed72019-11-21 21:01:26 -0700772 cqe = NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700773 while (!list_empty(&ctx->cq_overflow_list)) {
774 cqe = io_get_cqring(ctx);
775 if (!cqe && !force)
776 break;
777
778 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
779 list);
780 list_move(&req->list, &list);
781 if (cqe) {
782 WRITE_ONCE(cqe->user_data, req->user_data);
783 WRITE_ONCE(cqe->res, req->result);
784 WRITE_ONCE(cqe->flags, 0);
785 } else {
786 WRITE_ONCE(ctx->rings->cq_overflow,
787 atomic_inc_return(&ctx->cached_cq_overflow));
788 }
789 }
790
791 io_commit_cqring(ctx);
792 spin_unlock_irqrestore(&ctx->completion_lock, flags);
793 io_cqring_ev_posted(ctx);
794
795 while (!list_empty(&list)) {
796 req = list_first_entry(&list, struct io_kiocb, list);
797 list_del(&req->list);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800798 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700799 }
Jens Axboec4a2ed72019-11-21 21:01:26 -0700800
801 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700802}
803
Jens Axboe78e19bb2019-11-06 15:21:34 -0700804static void io_cqring_fill_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700805{
Jens Axboe78e19bb2019-11-06 15:21:34 -0700806 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700807 struct io_uring_cqe *cqe;
808
Jens Axboe78e19bb2019-11-06 15:21:34 -0700809 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -0700810
Jens Axboe2b188cc2019-01-07 10:46:33 -0700811 /*
812 * If we can't get a cq entry, userspace overflowed the
813 * submission (by quite a lot). Increment the overflow count in
814 * the ring.
815 */
816 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700817 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -0700818 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700819 WRITE_ONCE(cqe->res, res);
820 WRITE_ONCE(cqe->flags, 0);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700821 } else if (ctx->cq_overflow_flushed) {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700822 WRITE_ONCE(ctx->rings->cq_overflow,
823 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700824 } else {
825 refcount_inc(&req->refs);
826 req->result = res;
827 list_add_tail(&req->list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700828 }
829}
830
Jens Axboe78e19bb2019-11-06 15:21:34 -0700831static void io_cqring_add_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700832{
Jens Axboe78e19bb2019-11-06 15:21:34 -0700833 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700834 unsigned long flags;
835
836 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700837 io_cqring_fill_event(req, res);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700838 io_commit_cqring(ctx);
839 spin_unlock_irqrestore(&ctx->completion_lock, flags);
840
Jens Axboe8c838782019-03-12 15:48:16 -0600841 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700842}
843
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700844static inline bool io_is_fallback_req(struct io_kiocb *req)
845{
846 return req == (struct io_kiocb *)
847 ((unsigned long) req->ctx->fallback_req & ~1UL);
848}
849
850static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
851{
852 struct io_kiocb *req;
853
854 req = ctx->fallback_req;
855 if (!test_and_set_bit_lock(0, (unsigned long *) ctx->fallback_req))
856 return req;
857
858 return NULL;
859}
860
Jens Axboe2579f912019-01-09 09:10:43 -0700861static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
862 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700863{
Jens Axboefd6fab22019-03-14 16:30:06 -0600864 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700865 struct io_kiocb *req;
866
867 if (!percpu_ref_tryget(&ctx->refs))
868 return NULL;
869
Jens Axboe2579f912019-01-09 09:10:43 -0700870 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -0600871 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -0700872 if (unlikely(!req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700873 goto fallback;
Jens Axboe2579f912019-01-09 09:10:43 -0700874 } else if (!state->free_reqs) {
875 size_t sz;
876 int ret;
877
878 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -0600879 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
880
881 /*
882 * Bulk alloc is all-or-nothing. If we fail to get a batch,
883 * retry single alloc to be on the safe side.
884 */
885 if (unlikely(ret <= 0)) {
886 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
887 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700888 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -0600889 ret = 1;
890 }
Jens Axboe2579f912019-01-09 09:10:43 -0700891 state->free_reqs = ret - 1;
892 state->cur_req = 1;
893 req = state->reqs[0];
894 } else {
895 req = state->reqs[state->cur_req];
896 state->free_reqs--;
897 state->cur_req++;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700898 }
899
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700900got_it:
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700901 req->io = NULL;
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300902 req->ring_file = NULL;
Jens Axboe60c112b2019-06-21 10:20:18 -0600903 req->file = NULL;
Jens Axboe2579f912019-01-09 09:10:43 -0700904 req->ctx = ctx;
905 req->flags = 0;
Jens Axboee65ef562019-03-12 10:16:44 -0600906 /* one is dropped after submission, the other at completion */
907 refcount_set(&req->refs, 2);
Jens Axboe9e645e112019-05-10 16:07:28 -0600908 req->result = 0;
Jens Axboe561fb042019-10-24 07:25:42 -0600909 INIT_IO_WORK(&req->work, io_wq_submit_work);
Jens Axboe2579f912019-01-09 09:10:43 -0700910 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700911fallback:
912 req = io_get_fallback_req(ctx);
913 if (req)
914 goto got_it;
Pavel Begunkov6805b322019-10-08 02:18:42 +0300915 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700916 return NULL;
917}
918
Jens Axboedef596e2019-01-09 08:59:42 -0700919static void io_free_req_many(struct io_ring_ctx *ctx, void **reqs, int *nr)
920{
921 if (*nr) {
922 kmem_cache_free_bulk(req_cachep, *nr, reqs);
Pavel Begunkov6805b322019-10-08 02:18:42 +0300923 percpu_ref_put_many(&ctx->refs, *nr);
Jens Axboedef596e2019-01-09 08:59:42 -0700924 *nr = 0;
925 }
926}
927
Jens Axboe9e645e112019-05-10 16:07:28 -0600928static void __io_free_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700929{
Jens Axboefcb323c2019-10-24 12:39:47 -0600930 struct io_ring_ctx *ctx = req->ctx;
931
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700932 if (req->io)
933 kfree(req->io);
Jens Axboe09bb8392019-03-13 12:39:28 -0600934 if (req->file && !(req->flags & REQ_F_FIXED_FILE))
935 fput(req->file);
Jens Axboefcb323c2019-10-24 12:39:47 -0600936 if (req->flags & REQ_F_INFLIGHT) {
937 unsigned long flags;
938
939 spin_lock_irqsave(&ctx->inflight_lock, flags);
940 list_del(&req->inflight_entry);
941 if (waitqueue_active(&ctx->inflight_wait))
942 wake_up(&ctx->inflight_wait);
943 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
944 }
945 percpu_ref_put(&ctx->refs);
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700946 if (likely(!io_is_fallback_req(req)))
947 kmem_cache_free(req_cachep, req);
948 else
949 clear_bit_unlock(0, (unsigned long *) ctx->fallback_req);
Jens Axboee65ef562019-03-12 10:16:44 -0600950}
951
Jackie Liua197f662019-11-08 08:09:12 -0700952static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -0600953{
Jackie Liua197f662019-11-08 08:09:12 -0700954 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -0700955 int ret;
956
Jens Axboe2d283902019-12-04 11:08:05 -0700957 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe2665abf2019-11-05 12:40:47 -0700958 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -0700959 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -0700960 io_commit_cqring(ctx);
961 req->flags &= ~REQ_F_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +0800962 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -0700963 return true;
964 }
965
966 return false;
967}
968
Jens Axboeba816ad2019-09-28 11:36:45 -0600969static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -0600970{
Jens Axboe2665abf2019-11-05 12:40:47 -0700971 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -0700972 bool wake_ev = false;
Jens Axboe9e645e112019-05-10 16:07:28 -0600973
Jens Axboe4d7dd462019-11-20 13:03:52 -0700974 /* Already got next link */
975 if (req->flags & REQ_F_LINK_NEXT)
976 return;
977
Jens Axboe9e645e112019-05-10 16:07:28 -0600978 /*
979 * The list should never be empty when we are called here. But could
980 * potentially happen if the chain is messed up, check to be on the
981 * safe side.
982 */
Pavel Begunkov44932332019-12-05 16:16:35 +0300983 while (!list_empty(&req->link_list)) {
984 struct io_kiocb *nxt = list_first_entry(&req->link_list,
985 struct io_kiocb, link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700986
Pavel Begunkov44932332019-12-05 16:16:35 +0300987 if (unlikely((req->flags & REQ_F_LINK_TIMEOUT) &&
988 (nxt->flags & REQ_F_TIMEOUT))) {
989 list_del_init(&nxt->link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700990 wake_ev |= io_link_cancel_timeout(nxt);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700991 req->flags &= ~REQ_F_LINK_TIMEOUT;
992 continue;
993 }
Jens Axboe9e645e112019-05-10 16:07:28 -0600994
Pavel Begunkov44932332019-12-05 16:16:35 +0300995 list_del_init(&req->link_list);
996 if (!list_empty(&nxt->link_list))
997 nxt->flags |= REQ_F_LINK;
Pavel Begunkovb18fdf72019-11-21 23:21:02 +0300998 *nxtptr = nxt;
Jens Axboe94ae5e72019-11-14 19:39:52 -0700999 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06001000 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001001
Jens Axboe4d7dd462019-11-20 13:03:52 -07001002 req->flags |= REQ_F_LINK_NEXT;
Jens Axboe2665abf2019-11-05 12:40:47 -07001003 if (wake_ev)
1004 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001005}
1006
1007/*
1008 * Called if REQ_F_LINK is set, and we fail the head request
1009 */
1010static void io_fail_links(struct io_kiocb *req)
1011{
Jens Axboe2665abf2019-11-05 12:40:47 -07001012 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001013 unsigned long flags;
1014
1015 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06001016
1017 while (!list_empty(&req->link_list)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03001018 struct io_kiocb *link = list_first_entry(&req->link_list,
1019 struct io_kiocb, link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06001020
Pavel Begunkov44932332019-12-05 16:16:35 +03001021 list_del_init(&link->link_list);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001022 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001023
1024 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
Jens Axboed625c6e2019-12-17 19:53:05 -07001025 link->opcode == IORING_OP_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -07001026 io_link_cancel_timeout(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001027 } else {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001028 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe978db572019-11-14 22:39:04 -07001029 __io_double_put_req(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001030 }
Jens Axboe5d960722019-11-19 15:31:28 -07001031 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -06001032 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001033
1034 io_commit_cqring(ctx);
1035 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1036 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001037}
1038
Jens Axboe4d7dd462019-11-20 13:03:52 -07001039static void io_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -06001040{
Jens Axboe4d7dd462019-11-20 13:03:52 -07001041 if (likely(!(req->flags & REQ_F_LINK)))
Jens Axboe2665abf2019-11-05 12:40:47 -07001042 return;
Jens Axboe2665abf2019-11-05 12:40:47 -07001043
Jens Axboe9e645e112019-05-10 16:07:28 -06001044 /*
1045 * If LINK is set, we have dependent requests in this chain. If we
1046 * didn't fail this request, queue the first one up, moving any other
1047 * dependencies to the next request. In case of failure, fail the rest
1048 * of the chain.
1049 */
Jens Axboe2665abf2019-11-05 12:40:47 -07001050 if (req->flags & REQ_F_FAIL_LINK) {
1051 io_fail_links(req);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001052 } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) ==
1053 REQ_F_LINK_TIMEOUT) {
Jens Axboe2665abf2019-11-05 12:40:47 -07001054 struct io_ring_ctx *ctx = req->ctx;
1055 unsigned long flags;
1056
1057 /*
1058 * If this is a timeout link, we could be racing with the
1059 * timeout timer. Grab the completion lock for this case to
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001060 * protect against that.
Jens Axboe2665abf2019-11-05 12:40:47 -07001061 */
1062 spin_lock_irqsave(&ctx->completion_lock, flags);
1063 io_req_link_next(req, nxt);
1064 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1065 } else {
1066 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -06001067 }
Jens Axboe4d7dd462019-11-20 13:03:52 -07001068}
Jens Axboe9e645e112019-05-10 16:07:28 -06001069
Jackie Liuc69f8db2019-11-09 11:00:08 +08001070static void io_free_req(struct io_kiocb *req)
1071{
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001072 struct io_kiocb *nxt = NULL;
1073
1074 io_req_find_next(req, &nxt);
Pavel Begunkov70cf9f32019-11-21 23:21:00 +03001075 __io_free_req(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001076
1077 if (nxt)
1078 io_queue_async_work(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08001079}
1080
Jens Axboeba816ad2019-09-28 11:36:45 -06001081/*
1082 * Drop reference to request, return next in chain (if there is one) if this
1083 * was the last reference to this request.
1084 */
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001085__attribute__((nonnull))
Jackie Liuec9c02a2019-11-08 23:50:36 +08001086static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboee65ef562019-03-12 10:16:44 -06001087{
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001088 io_req_find_next(req, nxtptr);
Jens Axboe4d7dd462019-11-20 13:03:52 -07001089
Jens Axboee65ef562019-03-12 10:16:44 -06001090 if (refcount_dec_and_test(&req->refs))
Jens Axboe4d7dd462019-11-20 13:03:52 -07001091 __io_free_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001092}
1093
Jens Axboe2b188cc2019-01-07 10:46:33 -07001094static void io_put_req(struct io_kiocb *req)
1095{
Jens Axboedef596e2019-01-09 08:59:42 -07001096 if (refcount_dec_and_test(&req->refs))
1097 io_free_req(req);
1098}
1099
Jens Axboe978db572019-11-14 22:39:04 -07001100/*
1101 * Must only be used if we don't need to care about links, usually from
1102 * within the completion handling itself.
1103 */
1104static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06001105{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001106 /* drop both submit and complete references */
1107 if (refcount_sub_and_test(2, &req->refs))
1108 __io_free_req(req);
1109}
1110
Jens Axboe978db572019-11-14 22:39:04 -07001111static void io_double_put_req(struct io_kiocb *req)
1112{
1113 /* drop both submit and complete references */
1114 if (refcount_sub_and_test(2, &req->refs))
1115 io_free_req(req);
1116}
1117
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001118static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06001119{
Jens Axboe84f97dc2019-11-06 11:27:53 -07001120 struct io_rings *rings = ctx->rings;
1121
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001122 /*
1123 * noflush == true is from the waitqueue handler, just ensure we wake
1124 * up the task, and the next invocation will flush the entries. We
1125 * cannot safely to it from here.
1126 */
1127 if (noflush && !list_empty(&ctx->cq_overflow_list))
1128 return -1U;
1129
1130 io_cqring_overflow_flush(ctx, false);
1131
Jens Axboea3a0e432019-08-20 11:03:11 -06001132 /* See comment at the top of this file */
1133 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00001134 return READ_ONCE(rings->cq.tail) - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06001135}
1136
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03001137static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1138{
1139 struct io_rings *rings = ctx->rings;
1140
1141 /* make sure SQ entry isn't read before tail */
1142 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1143}
1144
Jens Axboedef596e2019-01-09 08:59:42 -07001145/*
1146 * Find and free completed poll iocbs
1147 */
1148static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
1149 struct list_head *done)
1150{
1151 void *reqs[IO_IOPOLL_BATCH];
1152 struct io_kiocb *req;
Jens Axboe09bb8392019-03-13 12:39:28 -06001153 int to_free;
Jens Axboedef596e2019-01-09 08:59:42 -07001154
Jens Axboe09bb8392019-03-13 12:39:28 -06001155 to_free = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001156 while (!list_empty(done)) {
1157 req = list_first_entry(done, struct io_kiocb, list);
1158 list_del(&req->list);
1159
Jens Axboe78e19bb2019-11-06 15:21:34 -07001160 io_cqring_fill_event(req, req->result);
Jens Axboedef596e2019-01-09 08:59:42 -07001161 (*nr_events)++;
1162
Jens Axboe09bb8392019-03-13 12:39:28 -06001163 if (refcount_dec_and_test(&req->refs)) {
1164 /* If we're not using fixed files, we have to pair the
1165 * completion part with the file put. Use regular
1166 * completions for those, only batch free for fixed
Jens Axboe9e645e112019-05-10 16:07:28 -06001167 * file and non-linked commands.
Jens Axboe09bb8392019-03-13 12:39:28 -06001168 */
Jens Axboe1a6b74f2019-12-02 10:33:15 -07001169 if (((req->flags & (REQ_F_FIXED_FILE|REQ_F_LINK)) ==
1170 REQ_F_FIXED_FILE) && !io_is_fallback_req(req) &&
1171 !req->io) {
Jens Axboe09bb8392019-03-13 12:39:28 -06001172 reqs[to_free++] = req;
1173 if (to_free == ARRAY_SIZE(reqs))
1174 io_free_req_many(ctx, reqs, &to_free);
Jens Axboe6b063142019-01-10 22:13:58 -07001175 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001176 io_free_req(req);
Jens Axboe6b063142019-01-10 22:13:58 -07001177 }
Jens Axboe9a56a232019-01-09 09:06:50 -07001178 }
Jens Axboedef596e2019-01-09 08:59:42 -07001179 }
Jens Axboedef596e2019-01-09 08:59:42 -07001180
Jens Axboe09bb8392019-03-13 12:39:28 -06001181 io_commit_cqring(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07001182 io_free_req_many(ctx, reqs, &to_free);
1183}
1184
1185static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1186 long min)
1187{
1188 struct io_kiocb *req, *tmp;
1189 LIST_HEAD(done);
1190 bool spin;
1191 int ret;
1192
1193 /*
1194 * Only spin for completions if we don't have multiple devices hanging
1195 * off our complete list, and we're under the requested amount.
1196 */
1197 spin = !ctx->poll_multi_file && *nr_events < min;
1198
1199 ret = 0;
1200 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
Jens Axboe9adbd452019-12-20 08:45:55 -07001201 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07001202
1203 /*
1204 * Move completed entries to our local list. If we find a
1205 * request that requires polling, break out and complete
1206 * the done list first, if we have entries there.
1207 */
1208 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
1209 list_move_tail(&req->list, &done);
1210 continue;
1211 }
1212 if (!list_empty(&done))
1213 break;
1214
1215 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1216 if (ret < 0)
1217 break;
1218
1219 if (ret && spin)
1220 spin = false;
1221 ret = 0;
1222 }
1223
1224 if (!list_empty(&done))
1225 io_iopoll_complete(ctx, nr_events, &done);
1226
1227 return ret;
1228}
1229
1230/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08001231 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07001232 * non-spinning poll check - we'll still enter the driver poll loop, but only
1233 * as a non-spinning completion check.
1234 */
1235static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
1236 long min)
1237{
Jens Axboe08f54392019-08-21 22:19:11 -06001238 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07001239 int ret;
1240
1241 ret = io_do_iopoll(ctx, nr_events, min);
1242 if (ret < 0)
1243 return ret;
1244 if (!min || *nr_events >= min)
1245 return 0;
1246 }
1247
1248 return 1;
1249}
1250
1251/*
1252 * We can't just wait for polled events to come to us, we have to actively
1253 * find and complete them.
1254 */
1255static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
1256{
1257 if (!(ctx->flags & IORING_SETUP_IOPOLL))
1258 return;
1259
1260 mutex_lock(&ctx->uring_lock);
1261 while (!list_empty(&ctx->poll_list)) {
1262 unsigned int nr_events = 0;
1263
1264 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -06001265
1266 /*
1267 * Ensure we allow local-to-the-cpu processing to take place,
1268 * in this case we need to ensure that we reap all events.
1269 */
1270 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -07001271 }
1272 mutex_unlock(&ctx->uring_lock);
1273}
1274
Jens Axboe2b2ed972019-10-25 10:06:15 -06001275static int __io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1276 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07001277{
Jens Axboe2b2ed972019-10-25 10:06:15 -06001278 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001279
1280 do {
1281 int tmin = 0;
1282
Jens Axboe500f9fb2019-08-19 12:15:59 -06001283 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06001284 * Don't enter poll loop if we already have events pending.
1285 * If we do, we can potentially be spinning for commands that
1286 * already triggered a CQE (eg in error).
1287 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001288 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06001289 break;
1290
1291 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06001292 * If a submit got punted to a workqueue, we can have the
1293 * application entering polling for a command before it gets
1294 * issued. That app will hold the uring_lock for the duration
1295 * of the poll right here, so we need to take a breather every
1296 * now and then to ensure that the issue has a chance to add
1297 * the poll to the issued list. Otherwise we can spin here
1298 * forever, while the workqueue is stuck trying to acquire the
1299 * very same mutex.
1300 */
1301 if (!(++iters & 7)) {
1302 mutex_unlock(&ctx->uring_lock);
1303 mutex_lock(&ctx->uring_lock);
1304 }
1305
Jens Axboedef596e2019-01-09 08:59:42 -07001306 if (*nr_events < min)
1307 tmin = min - *nr_events;
1308
1309 ret = io_iopoll_getevents(ctx, nr_events, tmin);
1310 if (ret <= 0)
1311 break;
1312 ret = 0;
1313 } while (min && !*nr_events && !need_resched());
1314
Jens Axboe2b2ed972019-10-25 10:06:15 -06001315 return ret;
1316}
1317
1318static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1319 long min)
1320{
1321 int ret;
1322
1323 /*
1324 * We disallow the app entering submit/complete with polling, but we
1325 * still need to lock the ring to prevent racing with polled issue
1326 * that got punted to a workqueue.
1327 */
1328 mutex_lock(&ctx->uring_lock);
1329 ret = __io_iopoll_check(ctx, nr_events, min);
Jens Axboe500f9fb2019-08-19 12:15:59 -06001330 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001331 return ret;
1332}
1333
Jens Axboe491381ce2019-10-17 09:20:46 -06001334static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001335{
Jens Axboe491381ce2019-10-17 09:20:46 -06001336 /*
1337 * Tell lockdep we inherited freeze protection from submission
1338 * thread.
1339 */
1340 if (req->flags & REQ_F_ISREG) {
1341 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001342
Jens Axboe491381ce2019-10-17 09:20:46 -06001343 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001344 }
Jens Axboe491381ce2019-10-17 09:20:46 -06001345 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001346}
1347
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001348static inline void req_set_fail_links(struct io_kiocb *req)
1349{
1350 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1351 req->flags |= REQ_F_FAIL_LINK;
1352}
1353
Jens Axboeba816ad2019-09-28 11:36:45 -06001354static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001355{
Jens Axboe9adbd452019-12-20 08:45:55 -07001356 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001357
Jens Axboe491381ce2019-10-17 09:20:46 -06001358 if (kiocb->ki_flags & IOCB_WRITE)
1359 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001360
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001361 if (res != req->result)
1362 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001363 io_cqring_add_event(req, res);
Jens Axboeba816ad2019-09-28 11:36:45 -06001364}
1365
1366static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1367{
Jens Axboe9adbd452019-12-20 08:45:55 -07001368 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06001369
1370 io_complete_rw_common(kiocb, res);
Jens Axboee65ef562019-03-12 10:16:44 -06001371 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001372}
1373
Jens Axboeba816ad2019-09-28 11:36:45 -06001374static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res)
1375{
Jens Axboe9adbd452019-12-20 08:45:55 -07001376 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001377 struct io_kiocb *nxt = NULL;
Jens Axboeba816ad2019-09-28 11:36:45 -06001378
1379 io_complete_rw_common(kiocb, res);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001380 io_put_req_find_next(req, &nxt);
1381
1382 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001383}
1384
Jens Axboedef596e2019-01-09 08:59:42 -07001385static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1386{
Jens Axboe9adbd452019-12-20 08:45:55 -07001387 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07001388
Jens Axboe491381ce2019-10-17 09:20:46 -06001389 if (kiocb->ki_flags & IOCB_WRITE)
1390 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001391
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001392 if (res != req->result)
1393 req_set_fail_links(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06001394 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -07001395 if (res != -EAGAIN)
1396 req->flags |= REQ_F_IOPOLL_COMPLETED;
1397}
1398
1399/*
1400 * After the iocb has been issued, it's safe to be found on the poll list.
1401 * Adding the kiocb to the list AFTER submission ensures that we don't
1402 * find it from a io_iopoll_getevents() thread before the issuer is done
1403 * accessing the kiocb cookie.
1404 */
1405static void io_iopoll_req_issued(struct io_kiocb *req)
1406{
1407 struct io_ring_ctx *ctx = req->ctx;
1408
1409 /*
1410 * Track whether we have multiple files in our lists. This will impact
1411 * how we do polling eventually, not spinning if we're on potentially
1412 * different devices.
1413 */
1414 if (list_empty(&ctx->poll_list)) {
1415 ctx->poll_multi_file = false;
1416 } else if (!ctx->poll_multi_file) {
1417 struct io_kiocb *list_req;
1418
1419 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1420 list);
Jens Axboe9adbd452019-12-20 08:45:55 -07001421 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07001422 ctx->poll_multi_file = true;
1423 }
1424
1425 /*
1426 * For fast devices, IO may have already completed. If it has, add
1427 * it to the front so we find it first.
1428 */
1429 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1430 list_add(&req->list, &ctx->poll_list);
1431 else
1432 list_add_tail(&req->list, &ctx->poll_list);
1433}
1434
Jens Axboe3d6770f2019-04-13 11:50:54 -06001435static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07001436{
Jens Axboe3d6770f2019-04-13 11:50:54 -06001437 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -07001438 int diff = state->has_refs - state->used_refs;
1439
1440 if (diff)
1441 fput_many(state->file, diff);
1442 state->file = NULL;
1443 }
1444}
1445
1446/*
1447 * Get as many references to a file as we have IOs left in this submission,
1448 * assuming most submissions are for one file, or at least that each file
1449 * has more than one submission.
1450 */
1451static struct file *io_file_get(struct io_submit_state *state, int fd)
1452{
1453 if (!state)
1454 return fget(fd);
1455
1456 if (state->file) {
1457 if (state->fd == fd) {
1458 state->used_refs++;
1459 state->ios_left--;
1460 return state->file;
1461 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06001462 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07001463 }
1464 state->file = fget_many(fd, state->ios_left);
1465 if (!state->file)
1466 return NULL;
1467
1468 state->fd = fd;
1469 state->has_refs = state->ios_left;
1470 state->used_refs = 1;
1471 state->ios_left--;
1472 return state->file;
1473}
1474
Jens Axboe2b188cc2019-01-07 10:46:33 -07001475/*
1476 * If we tracked the file through the SCM inflight mechanism, we could support
1477 * any file. For now, just ensure that anything potentially problematic is done
1478 * inline.
1479 */
1480static bool io_file_supports_async(struct file *file)
1481{
1482 umode_t mode = file_inode(file)->i_mode;
1483
Jens Axboe10d59342019-12-09 20:16:22 -07001484 if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07001485 return true;
1486 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
1487 return true;
1488
1489 return false;
1490}
1491
Jens Axboe3529d8c2019-12-19 18:24:38 -07001492static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1493 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001494{
Jens Axboedef596e2019-01-09 08:59:42 -07001495 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07001496 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06001497 unsigned ioprio;
1498 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001499
Jens Axboe09bb8392019-03-13 12:39:28 -06001500 if (!req->file)
1501 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001502
Jens Axboe491381ce2019-10-17 09:20:46 -06001503 if (S_ISREG(file_inode(req->file)->i_mode))
1504 req->flags |= REQ_F_ISREG;
1505
Jens Axboe2b188cc2019-01-07 10:46:33 -07001506 kiocb->ki_pos = READ_ONCE(sqe->off);
1507 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
1508 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
1509
1510 ioprio = READ_ONCE(sqe->ioprio);
1511 if (ioprio) {
1512 ret = ioprio_check_cap(ioprio);
1513 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06001514 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001515
1516 kiocb->ki_ioprio = ioprio;
1517 } else
1518 kiocb->ki_ioprio = get_current_ioprio();
1519
1520 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
1521 if (unlikely(ret))
Jens Axboe09bb8392019-03-13 12:39:28 -06001522 return ret;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001523
1524 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06001525 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
1526 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02001527 req->flags |= REQ_F_NOWAIT;
1528
1529 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001530 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001531
Jens Axboedef596e2019-01-09 08:59:42 -07001532 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07001533 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
1534 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06001535 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001536
Jens Axboedef596e2019-01-09 08:59:42 -07001537 kiocb->ki_flags |= IOCB_HIPRI;
1538 kiocb->ki_complete = io_complete_rw_iopoll;
Jens Axboe6873e0b2019-10-30 13:53:09 -06001539 req->result = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001540 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001541 if (kiocb->ki_flags & IOCB_HIPRI)
1542 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07001543 kiocb->ki_complete = io_complete_rw;
1544 }
Jens Axboe9adbd452019-12-20 08:45:55 -07001545
Jens Axboe3529d8c2019-12-19 18:24:38 -07001546 req->rw.addr = READ_ONCE(sqe->addr);
1547 req->rw.len = READ_ONCE(sqe->len);
Jens Axboe9adbd452019-12-20 08:45:55 -07001548 /* we own ->private, reuse it for the buffer index */
1549 req->rw.kiocb.private = (void *) (unsigned long)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001550 READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001551 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001552}
1553
1554static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
1555{
1556 switch (ret) {
1557 case -EIOCBQUEUED:
1558 break;
1559 case -ERESTARTSYS:
1560 case -ERESTARTNOINTR:
1561 case -ERESTARTNOHAND:
1562 case -ERESTART_RESTARTBLOCK:
1563 /*
1564 * We can't just restart the syscall, since previously
1565 * submitted sqes may already be in progress. Just fail this
1566 * IO with EINTR.
1567 */
1568 ret = -EINTR;
1569 /* fall through */
1570 default:
1571 kiocb->ki_complete(kiocb, ret, 0);
1572 }
1573}
1574
Jens Axboeba816ad2019-09-28 11:36:45 -06001575static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt,
1576 bool in_async)
1577{
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001578 if (in_async && ret >= 0 && kiocb->ki_complete == io_complete_rw)
Jens Axboeba816ad2019-09-28 11:36:45 -06001579 *nxt = __io_complete_rw(kiocb, ret);
1580 else
1581 io_rw_done(kiocb, ret);
1582}
1583
Jens Axboe9adbd452019-12-20 08:45:55 -07001584static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03001585 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07001586{
Jens Axboe9adbd452019-12-20 08:45:55 -07001587 struct io_ring_ctx *ctx = req->ctx;
1588 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001589 struct io_mapped_ubuf *imu;
1590 unsigned index, buf_index;
1591 size_t offset;
1592 u64 buf_addr;
1593
1594 /* attempt to use fixed buffers without having provided iovecs */
1595 if (unlikely(!ctx->user_bufs))
1596 return -EFAULT;
1597
Jens Axboe9adbd452019-12-20 08:45:55 -07001598 buf_index = (unsigned long) req->rw.kiocb.private;
Jens Axboeedafcce2019-01-09 09:16:05 -07001599 if (unlikely(buf_index >= ctx->nr_user_bufs))
1600 return -EFAULT;
1601
1602 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
1603 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07001604 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07001605
1606 /* overflow */
1607 if (buf_addr + len < buf_addr)
1608 return -EFAULT;
1609 /* not inside the mapped region */
1610 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
1611 return -EFAULT;
1612
1613 /*
1614 * May not be a start of buffer, set size appropriately
1615 * and advance us to the beginning.
1616 */
1617 offset = buf_addr - imu->ubuf;
1618 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06001619
1620 if (offset) {
1621 /*
1622 * Don't use iov_iter_advance() here, as it's really slow for
1623 * using the latter parts of a big fixed buffer - it iterates
1624 * over each segment manually. We can cheat a bit here, because
1625 * we know that:
1626 *
1627 * 1) it's a BVEC iter, we set it up
1628 * 2) all bvecs are PAGE_SIZE in size, except potentially the
1629 * first and last bvec
1630 *
1631 * So just find our index, and adjust the iterator afterwards.
1632 * If the offset is within the first bvec (or the whole first
1633 * bvec, just use iov_iter_advance(). This makes it easier
1634 * since we can just skip the first segment, which may not
1635 * be PAGE_SIZE aligned.
1636 */
1637 const struct bio_vec *bvec = imu->bvec;
1638
1639 if (offset <= bvec->bv_len) {
1640 iov_iter_advance(iter, offset);
1641 } else {
1642 unsigned long seg_skip;
1643
1644 /* skip first vec */
1645 offset -= bvec->bv_len;
1646 seg_skip = 1 + (offset >> PAGE_SHIFT);
1647
1648 iter->bvec = bvec + seg_skip;
1649 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02001650 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001651 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001652 }
1653 }
1654
Jens Axboe5e559562019-11-13 16:12:46 -07001655 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001656}
1657
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001658static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
1659 struct iovec **iovec, struct iov_iter *iter)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001660{
Jens Axboe9adbd452019-12-20 08:45:55 -07001661 void __user *buf = u64_to_user_ptr(req->rw.addr);
1662 size_t sqe_len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001663 u8 opcode;
1664
Jens Axboed625c6e2019-12-17 19:53:05 -07001665 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03001666 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07001667 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07001668 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07001669 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001670
Jens Axboe9adbd452019-12-20 08:45:55 -07001671 /* buffer index only valid with fixed read/write */
1672 if (req->rw.kiocb.private)
1673 return -EINVAL;
1674
Jens Axboef67676d2019-12-02 11:03:47 -07001675 if (req->io) {
1676 struct io_async_rw *iorw = &req->io->rw;
1677
1678 *iovec = iorw->iov;
1679 iov_iter_init(iter, rw, *iovec, iorw->nr_segs, iorw->size);
1680 if (iorw->iov == iorw->fast_iov)
1681 *iovec = NULL;
1682 return iorw->size;
1683 }
1684
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001685 if (!req->has_user)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001686 return -EFAULT;
1687
1688#ifdef CONFIG_COMPAT
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001689 if (req->ctx->compat)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001690 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
1691 iovec, iter);
1692#endif
1693
1694 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
1695}
1696
Jens Axboe32960612019-09-23 11:05:34 -06001697/*
1698 * For files that don't have ->read_iter() and ->write_iter(), handle them
1699 * by looping over ->read() or ->write() manually.
1700 */
1701static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
1702 struct iov_iter *iter)
1703{
1704 ssize_t ret = 0;
1705
1706 /*
1707 * Don't support polled IO through this interface, and we can't
1708 * support non-blocking either. For the latter, this just causes
1709 * the kiocb to be handled from an async context.
1710 */
1711 if (kiocb->ki_flags & IOCB_HIPRI)
1712 return -EOPNOTSUPP;
1713 if (kiocb->ki_flags & IOCB_NOWAIT)
1714 return -EAGAIN;
1715
1716 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03001717 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06001718 ssize_t nr;
1719
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03001720 if (!iov_iter_is_bvec(iter)) {
1721 iovec = iov_iter_iovec(iter);
1722 } else {
1723 /* fixed buffers import bvec */
1724 iovec.iov_base = kmap(iter->bvec->bv_page)
1725 + iter->iov_offset;
1726 iovec.iov_len = min(iter->count,
1727 iter->bvec->bv_len - iter->iov_offset);
1728 }
1729
Jens Axboe32960612019-09-23 11:05:34 -06001730 if (rw == READ) {
1731 nr = file->f_op->read(file, iovec.iov_base,
1732 iovec.iov_len, &kiocb->ki_pos);
1733 } else {
1734 nr = file->f_op->write(file, iovec.iov_base,
1735 iovec.iov_len, &kiocb->ki_pos);
1736 }
1737
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03001738 if (iov_iter_is_bvec(iter))
1739 kunmap(iter->bvec->bv_page);
1740
Jens Axboe32960612019-09-23 11:05:34 -06001741 if (nr < 0) {
1742 if (!ret)
1743 ret = nr;
1744 break;
1745 }
1746 ret += nr;
1747 if (nr != iovec.iov_len)
1748 break;
1749 iov_iter_advance(iter, nr);
1750 }
1751
1752 return ret;
1753}
1754
Jens Axboeb7bb4f72019-12-15 22:13:43 -07001755static void io_req_map_rw(struct io_kiocb *req, ssize_t io_size,
Jens Axboef67676d2019-12-02 11:03:47 -07001756 struct iovec *iovec, struct iovec *fast_iov,
1757 struct iov_iter *iter)
1758{
1759 req->io->rw.nr_segs = iter->nr_segs;
1760 req->io->rw.size = io_size;
1761 req->io->rw.iov = iovec;
1762 if (!req->io->rw.iov) {
1763 req->io->rw.iov = req->io->rw.fast_iov;
1764 memcpy(req->io->rw.iov, fast_iov,
1765 sizeof(struct iovec) * iter->nr_segs);
1766 }
1767}
1768
Jens Axboeb7bb4f72019-12-15 22:13:43 -07001769static int io_alloc_async_ctx(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07001770{
1771 req->io = kmalloc(sizeof(*req->io), GFP_KERNEL);
Jens Axboe06b76d42019-12-19 14:44:26 -07001772 return req->io == NULL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07001773}
1774
1775static void io_rw_async(struct io_wq_work **workptr)
1776{
1777 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
1778 struct iovec *iov = NULL;
1779
1780 if (req->io->rw.iov != req->io->rw.fast_iov)
1781 iov = req->io->rw.iov;
1782 io_wq_submit_work(workptr);
1783 kfree(iov);
1784}
1785
1786static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size,
1787 struct iovec *iovec, struct iovec *fast_iov,
1788 struct iov_iter *iter)
1789{
Jens Axboe74566df2020-01-13 19:23:24 -07001790 if (req->opcode == IORING_OP_READ_FIXED ||
1791 req->opcode == IORING_OP_WRITE_FIXED)
1792 return 0;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07001793 if (!req->io && io_alloc_async_ctx(req))
1794 return -ENOMEM;
1795
1796 io_req_map_rw(req, io_size, iovec, fast_iov, iter);
1797 req->work.func = io_rw_async;
1798 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07001799}
1800
Jens Axboe3529d8c2019-12-19 18:24:38 -07001801static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1802 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07001803{
Jens Axboe3529d8c2019-12-19 18:24:38 -07001804 struct io_async_ctx *io;
1805 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07001806 ssize_t ret;
1807
Jens Axboe3529d8c2019-12-19 18:24:38 -07001808 ret = io_prep_rw(req, sqe, force_nonblock);
1809 if (ret)
1810 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07001811
Jens Axboe3529d8c2019-12-19 18:24:38 -07001812 if (unlikely(!(req->file->f_mode & FMODE_READ)))
1813 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07001814
Jens Axboe3529d8c2019-12-19 18:24:38 -07001815 if (!req->io)
1816 return 0;
1817
1818 io = req->io;
1819 io->rw.iov = io->rw.fast_iov;
1820 req->io = NULL;
1821 ret = io_import_iovec(READ, req, &io->rw.iov, &iter);
1822 req->io = io;
1823 if (ret < 0)
1824 return ret;
1825
1826 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
1827 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07001828}
1829
Pavel Begunkov267bc902019-11-07 01:41:08 +03001830static int io_read(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001831 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001832{
1833 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07001834 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001835 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07001836 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07001837 ssize_t io_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001838
Jens Axboe3529d8c2019-12-19 18:24:38 -07001839 ret = io_import_iovec(READ, req, &iovec, &iter);
Jens Axboe06b76d42019-12-19 14:44:26 -07001840 if (ret < 0)
1841 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001842
Jens Axboefd6c2e42019-12-18 12:19:41 -07001843 /* Ensure we clear previously set non-block flag */
1844 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07001845 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07001846
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08001847 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07001848 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06001849 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07001850 req->result = io_size;
1851
1852 /*
1853 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
1854 * we know to async punt it even if it was opened O_NONBLOCK
1855 */
Jens Axboe9adbd452019-12-20 08:45:55 -07001856 if (force_nonblock && !io_file_supports_async(req->file)) {
Jens Axboef67676d2019-12-02 11:03:47 -07001857 req->flags |= REQ_F_MUST_PUNT;
1858 goto copy_iov;
1859 }
Jens Axboe9e645e112019-05-10 16:07:28 -06001860
Jens Axboe31b51512019-01-18 22:56:34 -07001861 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07001862 ret = rw_verify_area(READ, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001863 if (!ret) {
1864 ssize_t ret2;
1865
Jens Axboe9adbd452019-12-20 08:45:55 -07001866 if (req->file->f_op->read_iter)
1867 ret2 = call_read_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06001868 else
Jens Axboe9adbd452019-12-20 08:45:55 -07001869 ret2 = loop_rw_iter(READ, req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06001870
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001871 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboef67676d2019-12-02 11:03:47 -07001872 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001873 kiocb_done(kiocb, ret2, nxt, req->in_async);
Jens Axboef67676d2019-12-02 11:03:47 -07001874 } else {
1875copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07001876 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07001877 inline_vecs, &iter);
1878 if (ret)
1879 goto out_free;
1880 return -EAGAIN;
1881 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001882 }
Jens Axboef67676d2019-12-02 11:03:47 -07001883out_free:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07001884 if (!io_wq_current_is_worker())
1885 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001886 return ret;
1887}
1888
Jens Axboe3529d8c2019-12-19 18:24:38 -07001889static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1890 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07001891{
Jens Axboe3529d8c2019-12-19 18:24:38 -07001892 struct io_async_ctx *io;
1893 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07001894 ssize_t ret;
1895
Jens Axboe3529d8c2019-12-19 18:24:38 -07001896 ret = io_prep_rw(req, sqe, force_nonblock);
1897 if (ret)
1898 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07001899
Jens Axboe3529d8c2019-12-19 18:24:38 -07001900 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
1901 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07001902
Jens Axboe3529d8c2019-12-19 18:24:38 -07001903 if (!req->io)
1904 return 0;
1905
1906 io = req->io;
1907 io->rw.iov = io->rw.fast_iov;
1908 req->io = NULL;
1909 ret = io_import_iovec(WRITE, req, &io->rw.iov, &iter);
1910 req->io = io;
1911 if (ret < 0)
1912 return ret;
1913
1914 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
1915 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07001916}
1917
Pavel Begunkov267bc902019-11-07 01:41:08 +03001918static int io_write(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001919 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001920{
1921 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07001922 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001923 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07001924 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07001925 ssize_t ret, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001926
Jens Axboe3529d8c2019-12-19 18:24:38 -07001927 ret = io_import_iovec(WRITE, req, &iovec, &iter);
Jens Axboe06b76d42019-12-19 14:44:26 -07001928 if (ret < 0)
1929 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001930
Jens Axboefd6c2e42019-12-18 12:19:41 -07001931 /* Ensure we clear previously set non-block flag */
1932 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07001933 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07001934
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08001935 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07001936 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06001937 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07001938 req->result = io_size;
1939
1940 /*
1941 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
1942 * we know to async punt it even if it was opened O_NONBLOCK
1943 */
1944 if (force_nonblock && !io_file_supports_async(req->file)) {
1945 req->flags |= REQ_F_MUST_PUNT;
1946 goto copy_iov;
1947 }
1948
Jens Axboe10d59342019-12-09 20:16:22 -07001949 /* file path doesn't support NOWAIT for non-direct_IO */
1950 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
1951 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07001952 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06001953
Jens Axboe31b51512019-01-18 22:56:34 -07001954 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07001955 ret = rw_verify_area(WRITE, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001956 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01001957 ssize_t ret2;
1958
Jens Axboe2b188cc2019-01-07 10:46:33 -07001959 /*
1960 * Open-code file_start_write here to grab freeze protection,
1961 * which will be released by another thread in
1962 * io_complete_rw(). Fool lockdep by telling it the lock got
1963 * released so that it doesn't complain about the held lock when
1964 * we return to userspace.
1965 */
Jens Axboe491381ce2019-10-17 09:20:46 -06001966 if (req->flags & REQ_F_ISREG) {
Jens Axboe9adbd452019-12-20 08:45:55 -07001967 __sb_start_write(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07001968 SB_FREEZE_WRITE, true);
Jens Axboe9adbd452019-12-20 08:45:55 -07001969 __sb_writers_release(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07001970 SB_FREEZE_WRITE);
1971 }
1972 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01001973
Jens Axboe9adbd452019-12-20 08:45:55 -07001974 if (req->file->f_op->write_iter)
1975 ret2 = call_write_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06001976 else
Jens Axboe9adbd452019-12-20 08:45:55 -07001977 ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter);
Jens Axboef67676d2019-12-02 11:03:47 -07001978 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001979 kiocb_done(kiocb, ret2, nxt, req->in_async);
Jens Axboef67676d2019-12-02 11:03:47 -07001980 } else {
1981copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07001982 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07001983 inline_vecs, &iter);
1984 if (ret)
1985 goto out_free;
1986 return -EAGAIN;
1987 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001988 }
Jens Axboe31b51512019-01-18 22:56:34 -07001989out_free:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07001990 if (!io_wq_current_is_worker())
1991 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001992 return ret;
1993}
1994
1995/*
1996 * IORING_OP_NOP just posts a completion event, nothing else.
1997 */
Jens Axboe78e19bb2019-11-06 15:21:34 -07001998static int io_nop(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001999{
2000 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002001
Jens Axboedef596e2019-01-09 08:59:42 -07002002 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2003 return -EINVAL;
2004
Jens Axboe78e19bb2019-11-06 15:21:34 -07002005 io_cqring_add_event(req, 0);
Jens Axboee65ef562019-03-12 10:16:44 -06002006 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002007 return 0;
2008}
2009
Jens Axboe3529d8c2019-12-19 18:24:38 -07002010static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002011{
Jens Axboe6b063142019-01-10 22:13:58 -07002012 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002013
Jens Axboe09bb8392019-03-13 12:39:28 -06002014 if (!req->file)
2015 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002016
Jens Axboe6b063142019-01-10 22:13:58 -07002017 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07002018 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07002019 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002020 return -EINVAL;
2021
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002022 req->sync.flags = READ_ONCE(sqe->fsync_flags);
2023 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
2024 return -EINVAL;
2025
2026 req->sync.off = READ_ONCE(sqe->off);
2027 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002028 return 0;
2029}
2030
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002031static bool io_req_cancelled(struct io_kiocb *req)
2032{
2033 if (req->work.flags & IO_WQ_WORK_CANCEL) {
2034 req_set_fail_links(req);
2035 io_cqring_add_event(req, -ECANCELED);
2036 io_put_req(req);
2037 return true;
2038 }
2039
2040 return false;
2041}
2042
Jens Axboe78912932020-01-14 22:09:06 -07002043static void io_link_work_cb(struct io_wq_work **workptr)
2044{
2045 struct io_wq_work *work = *workptr;
2046 struct io_kiocb *link = work->data;
2047
2048 io_queue_linked_timeout(link);
2049 work->func = io_wq_submit_work;
2050}
2051
2052static void io_wq_assign_next(struct io_wq_work **workptr, struct io_kiocb *nxt)
2053{
2054 struct io_kiocb *link;
2055
2056 io_prep_async_work(nxt, &link);
2057 *workptr = &nxt->work;
2058 if (link) {
2059 nxt->work.flags |= IO_WQ_WORK_CB;
2060 nxt->work.func = io_link_work_cb;
2061 nxt->work.data = link;
2062 }
2063}
2064
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002065static void io_fsync_finish(struct io_wq_work **workptr)
2066{
2067 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2068 loff_t end = req->sync.off + req->sync.len;
2069 struct io_kiocb *nxt = NULL;
2070 int ret;
2071
2072 if (io_req_cancelled(req))
2073 return;
2074
Jens Axboe9adbd452019-12-20 08:45:55 -07002075 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002076 end > 0 ? end : LLONG_MAX,
2077 req->sync.flags & IORING_FSYNC_DATASYNC);
2078 if (ret < 0)
2079 req_set_fail_links(req);
2080 io_cqring_add_event(req, ret);
2081 io_put_req_find_next(req, &nxt);
2082 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07002083 io_wq_assign_next(workptr, nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002084}
2085
Jens Axboefc4df992019-12-10 14:38:45 -07002086static int io_fsync(struct io_kiocb *req, struct io_kiocb **nxt,
2087 bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002088{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002089 struct io_wq_work *work, *old_work;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002090
2091 /* fsync always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002092 if (force_nonblock) {
2093 io_put_req(req);
2094 req->work.func = io_fsync_finish;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002095 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002096 }
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002097
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002098 work = old_work = &req->work;
2099 io_fsync_finish(&work);
2100 if (work && work != old_work)
2101 *nxt = container_of(work, struct io_kiocb, work);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002102 return 0;
2103}
2104
Jens Axboed63d1b52019-12-10 10:38:56 -07002105static void io_fallocate_finish(struct io_wq_work **workptr)
2106{
2107 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2108 struct io_kiocb *nxt = NULL;
2109 int ret;
2110
2111 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
2112 req->sync.len);
2113 if (ret < 0)
2114 req_set_fail_links(req);
2115 io_cqring_add_event(req, ret);
2116 io_put_req_find_next(req, &nxt);
2117 if (nxt)
2118 io_wq_assign_next(workptr, nxt);
2119}
2120
2121static int io_fallocate_prep(struct io_kiocb *req,
2122 const struct io_uring_sqe *sqe)
2123{
2124 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
2125 return -EINVAL;
2126
2127 req->sync.off = READ_ONCE(sqe->off);
2128 req->sync.len = READ_ONCE(sqe->addr);
2129 req->sync.mode = READ_ONCE(sqe->len);
2130 return 0;
2131}
2132
2133static int io_fallocate(struct io_kiocb *req, struct io_kiocb **nxt,
2134 bool force_nonblock)
2135{
2136 struct io_wq_work *work, *old_work;
2137
2138 /* fallocate always requiring blocking context */
2139 if (force_nonblock) {
2140 io_put_req(req);
2141 req->work.func = io_fallocate_finish;
2142 return -EAGAIN;
2143 }
2144
2145 work = old_work = &req->work;
2146 io_fallocate_finish(&work);
2147 if (work && work != old_work)
2148 *nxt = container_of(work, struct io_kiocb, work);
2149
2150 return 0;
2151}
2152
Jens Axboe3529d8c2019-12-19 18:24:38 -07002153static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002154{
2155 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002156
2157 if (!req->file)
2158 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002159
2160 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2161 return -EINVAL;
2162 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
2163 return -EINVAL;
2164
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002165 req->sync.off = READ_ONCE(sqe->off);
2166 req->sync.len = READ_ONCE(sqe->len);
2167 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002168 return 0;
2169}
2170
2171static void io_sync_file_range_finish(struct io_wq_work **workptr)
2172{
2173 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2174 struct io_kiocb *nxt = NULL;
2175 int ret;
2176
2177 if (io_req_cancelled(req))
2178 return;
2179
Jens Axboe9adbd452019-12-20 08:45:55 -07002180 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002181 req->sync.flags);
2182 if (ret < 0)
2183 req_set_fail_links(req);
2184 io_cqring_add_event(req, ret);
2185 io_put_req_find_next(req, &nxt);
2186 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07002187 io_wq_assign_next(workptr, nxt);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002188}
2189
Jens Axboefc4df992019-12-10 14:38:45 -07002190static int io_sync_file_range(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002191 bool force_nonblock)
2192{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002193 struct io_wq_work *work, *old_work;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002194
2195 /* sync_file_range always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002196 if (force_nonblock) {
2197 io_put_req(req);
2198 req->work.func = io_sync_file_range_finish;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002199 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002200 }
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002201
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002202 work = old_work = &req->work;
2203 io_sync_file_range_finish(&work);
2204 if (work && work != old_work)
2205 *nxt = container_of(work, struct io_kiocb, work);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002206 return 0;
2207}
2208
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002209#if defined(CONFIG_NET)
2210static void io_sendrecv_async(struct io_wq_work **workptr)
2211{
2212 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2213 struct iovec *iov = NULL;
2214
2215 if (req->io->rw.iov != req->io->rw.fast_iov)
2216 iov = req->io->msg.iov;
2217 io_wq_submit_work(workptr);
2218 kfree(iov);
2219}
2220#endif
2221
Jens Axboe3529d8c2019-12-19 18:24:38 -07002222static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06002223{
Jens Axboe03b12302019-12-02 18:50:25 -07002224#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07002225 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002226 struct io_async_ctx *io = req->io;
Jens Axboe03b12302019-12-02 18:50:25 -07002227
Jens Axboee47293f2019-12-20 08:58:21 -07002228 sr->msg_flags = READ_ONCE(sqe->msg_flags);
2229 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe3529d8c2019-12-19 18:24:38 -07002230
2231 if (!io)
2232 return 0;
2233
Jens Axboed9688562019-12-09 19:35:20 -07002234 io->msg.iov = io->msg.fast_iov;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002235 return sendmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07002236 &io->msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002237#else
Jens Axboee47293f2019-12-20 08:58:21 -07002238 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07002239#endif
2240}
2241
Jens Axboefc4df992019-12-10 14:38:45 -07002242static int io_sendmsg(struct io_kiocb *req, struct io_kiocb **nxt,
2243 bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07002244{
2245#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07002246 struct io_async_msghdr *kmsg = NULL;
Jens Axboe03b12302019-12-02 18:50:25 -07002247 struct socket *sock;
2248 int ret;
2249
2250 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2251 return -EINVAL;
2252
2253 sock = sock_from_file(req->file, &ret);
2254 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002255 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07002256 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -07002257 unsigned flags;
2258
Jens Axboe03b12302019-12-02 18:50:25 -07002259 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07002260 kmsg = &req->io->msg;
2261 kmsg->msg.msg_name = &addr;
2262 /* if iov is set, it's allocated already */
2263 if (!kmsg->iov)
2264 kmsg->iov = kmsg->fast_iov;
2265 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07002266 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07002267 struct io_sr_msg *sr = &req->sr_msg;
2268
Jens Axboe0b416c32019-12-15 10:57:46 -07002269 kmsg = &io.msg;
2270 kmsg->msg.msg_name = &addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002271
2272 io.msg.iov = io.msg.fast_iov;
2273 ret = sendmsg_copy_msghdr(&io.msg.msg, sr->msg,
2274 sr->msg_flags, &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002275 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002276 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07002277 }
2278
Jens Axboee47293f2019-12-20 08:58:21 -07002279 flags = req->sr_msg.msg_flags;
2280 if (flags & MSG_DONTWAIT)
2281 req->flags |= REQ_F_NOWAIT;
2282 else if (force_nonblock)
2283 flags |= MSG_DONTWAIT;
2284
Jens Axboe0b416c32019-12-15 10:57:46 -07002285 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Jens Axboe03b12302019-12-02 18:50:25 -07002286 if (force_nonblock && ret == -EAGAIN) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002287 if (req->io)
2288 return -EAGAIN;
2289 if (io_alloc_async_ctx(req))
2290 return -ENOMEM;
2291 memcpy(&req->io->msg, &io.msg, sizeof(io.msg));
2292 req->work.func = io_sendrecv_async;
Jens Axboe0b416c32019-12-15 10:57:46 -07002293 return -EAGAIN;
Jens Axboe03b12302019-12-02 18:50:25 -07002294 }
2295 if (ret == -ERESTARTSYS)
2296 ret = -EINTR;
2297 }
2298
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002299 if (!io_wq_current_is_worker() && kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07002300 kfree(kmsg->iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002301 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002302 if (ret < 0)
2303 req_set_fail_links(req);
Jens Axboe03b12302019-12-02 18:50:25 -07002304 io_put_req_find_next(req, nxt);
2305 return 0;
2306#else
2307 return -EOPNOTSUPP;
2308#endif
2309}
2310
Jens Axboe3529d8c2019-12-19 18:24:38 -07002311static int io_recvmsg_prep(struct io_kiocb *req,
2312 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07002313{
2314#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07002315 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002316 struct io_async_ctx *io = req->io;
Jens Axboe06b76d42019-12-19 14:44:26 -07002317
Jens Axboe3529d8c2019-12-19 18:24:38 -07002318 sr->msg_flags = READ_ONCE(sqe->msg_flags);
2319 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
2320
2321 if (!io)
Jens Axboe06b76d42019-12-19 14:44:26 -07002322 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07002323
Jens Axboed9688562019-12-09 19:35:20 -07002324 io->msg.iov = io->msg.fast_iov;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002325 return recvmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07002326 &io->msg.uaddr, &io->msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002327#else
Jens Axboee47293f2019-12-20 08:58:21 -07002328 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07002329#endif
2330}
2331
Jens Axboefc4df992019-12-10 14:38:45 -07002332static int io_recvmsg(struct io_kiocb *req, struct io_kiocb **nxt,
2333 bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07002334{
2335#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07002336 struct io_async_msghdr *kmsg = NULL;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002337 struct socket *sock;
2338 int ret;
2339
2340 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2341 return -EINVAL;
2342
2343 sock = sock_from_file(req->file, &ret);
2344 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002345 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07002346 struct sockaddr_storage addr;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002347 unsigned flags;
2348
Jens Axboe03b12302019-12-02 18:50:25 -07002349 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07002350 kmsg = &req->io->msg;
2351 kmsg->msg.msg_name = &addr;
2352 /* if iov is set, it's allocated already */
2353 if (!kmsg->iov)
2354 kmsg->iov = kmsg->fast_iov;
2355 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07002356 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07002357 struct io_sr_msg *sr = &req->sr_msg;
2358
Jens Axboe0b416c32019-12-15 10:57:46 -07002359 kmsg = &io.msg;
2360 kmsg->msg.msg_name = &addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002361
2362 io.msg.iov = io.msg.fast_iov;
2363 ret = recvmsg_copy_msghdr(&io.msg.msg, sr->msg,
2364 sr->msg_flags, &io.msg.uaddr,
2365 &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002366 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002367 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07002368 }
Jens Axboe0fa03c62019-04-19 13:34:07 -06002369
Jens Axboee47293f2019-12-20 08:58:21 -07002370 flags = req->sr_msg.msg_flags;
2371 if (flags & MSG_DONTWAIT)
2372 req->flags |= REQ_F_NOWAIT;
2373 else if (force_nonblock)
2374 flags |= MSG_DONTWAIT;
2375
2376 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.msg,
2377 kmsg->uaddr, flags);
Jens Axboe03b12302019-12-02 18:50:25 -07002378 if (force_nonblock && ret == -EAGAIN) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002379 if (req->io)
2380 return -EAGAIN;
2381 if (io_alloc_async_ctx(req))
2382 return -ENOMEM;
2383 memcpy(&req->io->msg, &io.msg, sizeof(io.msg));
2384 req->work.func = io_sendrecv_async;
Jens Axboe0b416c32019-12-15 10:57:46 -07002385 return -EAGAIN;
Jens Axboe03b12302019-12-02 18:50:25 -07002386 }
Jens Axboe441cdbd2019-12-02 18:49:10 -07002387 if (ret == -ERESTARTSYS)
2388 ret = -EINTR;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002389 }
2390
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002391 if (!io_wq_current_is_worker() && kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07002392 kfree(kmsg->iov);
Jens Axboe78e19bb2019-11-06 15:21:34 -07002393 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002394 if (ret < 0)
2395 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08002396 io_put_req_find_next(req, nxt);
Jens Axboe0fa03c62019-04-19 13:34:07 -06002397 return 0;
2398#else
2399 return -EOPNOTSUPP;
2400#endif
2401}
2402
Jens Axboe3529d8c2019-12-19 18:24:38 -07002403static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06002404{
2405#if defined(CONFIG_NET)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002406 struct io_accept *accept = &req->accept;
2407
Jens Axboe17f2fe32019-10-17 14:42:58 -06002408 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
2409 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05002410 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06002411 return -EINVAL;
2412
Jens Axboed55e5f52019-12-11 16:12:15 -07002413 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
2414 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002415 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002416 return 0;
2417#else
2418 return -EOPNOTSUPP;
2419#endif
2420}
Jens Axboe17f2fe32019-10-17 14:42:58 -06002421
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002422#if defined(CONFIG_NET)
2423static int __io_accept(struct io_kiocb *req, struct io_kiocb **nxt,
2424 bool force_nonblock)
2425{
2426 struct io_accept *accept = &req->accept;
2427 unsigned file_flags;
2428 int ret;
2429
2430 file_flags = force_nonblock ? O_NONBLOCK : 0;
2431 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
2432 accept->addr_len, accept->flags);
2433 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06002434 return -EAGAIN;
Jens Axboe8e3cca12019-11-09 19:52:33 -07002435 if (ret == -ERESTARTSYS)
2436 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002437 if (ret < 0)
2438 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07002439 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08002440 io_put_req_find_next(req, nxt);
Jens Axboe17f2fe32019-10-17 14:42:58 -06002441 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002442}
2443
2444static void io_accept_finish(struct io_wq_work **workptr)
2445{
2446 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2447 struct io_kiocb *nxt = NULL;
2448
2449 if (io_req_cancelled(req))
2450 return;
2451 __io_accept(req, &nxt, false);
2452 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07002453 io_wq_assign_next(workptr, nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002454}
2455#endif
2456
2457static int io_accept(struct io_kiocb *req, struct io_kiocb **nxt,
2458 bool force_nonblock)
2459{
2460#if defined(CONFIG_NET)
2461 int ret;
2462
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002463 ret = __io_accept(req, nxt, force_nonblock);
2464 if (ret == -EAGAIN && force_nonblock) {
2465 req->work.func = io_accept_finish;
2466 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
2467 io_put_req(req);
2468 return -EAGAIN;
2469 }
2470 return 0;
Jens Axboe17f2fe32019-10-17 14:42:58 -06002471#else
2472 return -EOPNOTSUPP;
2473#endif
2474}
2475
Jens Axboe3529d8c2019-12-19 18:24:38 -07002476static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07002477{
2478#if defined(CONFIG_NET)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002479 struct io_connect *conn = &req->connect;
2480 struct io_async_ctx *io = req->io;
Jens Axboef499a022019-12-02 16:28:46 -07002481
Jens Axboe3fbb51c2019-12-20 08:51:52 -07002482 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
2483 return -EINVAL;
2484 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
2485 return -EINVAL;
2486
Jens Axboe3529d8c2019-12-19 18:24:38 -07002487 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
2488 conn->addr_len = READ_ONCE(sqe->addr2);
2489
2490 if (!io)
2491 return 0;
2492
2493 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07002494 &io->connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07002495#else
Jens Axboe3fbb51c2019-12-20 08:51:52 -07002496 return -EOPNOTSUPP;
Jens Axboef499a022019-12-02 16:28:46 -07002497#endif
2498}
2499
Jens Axboefc4df992019-12-10 14:38:45 -07002500static int io_connect(struct io_kiocb *req, struct io_kiocb **nxt,
2501 bool force_nonblock)
Jens Axboef8e85cf2019-11-23 14:24:24 -07002502{
2503#if defined(CONFIG_NET)
Jens Axboef499a022019-12-02 16:28:46 -07002504 struct io_async_ctx __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07002505 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07002506 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07002507
Jens Axboef499a022019-12-02 16:28:46 -07002508 if (req->io) {
2509 io = req->io;
2510 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07002511 ret = move_addr_to_kernel(req->connect.addr,
2512 req->connect.addr_len,
2513 &__io.connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07002514 if (ret)
2515 goto out;
2516 io = &__io;
2517 }
2518
Jens Axboe3fbb51c2019-12-20 08:51:52 -07002519 file_flags = force_nonblock ? O_NONBLOCK : 0;
2520
2521 ret = __sys_connect_file(req->file, &io->connect.address,
2522 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07002523 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002524 if (req->io)
2525 return -EAGAIN;
2526 if (io_alloc_async_ctx(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07002527 ret = -ENOMEM;
2528 goto out;
2529 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002530 memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect));
Jens Axboef8e85cf2019-11-23 14:24:24 -07002531 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07002532 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07002533 if (ret == -ERESTARTSYS)
2534 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07002535out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002536 if (ret < 0)
2537 req_set_fail_links(req);
Jens Axboef8e85cf2019-11-23 14:24:24 -07002538 io_cqring_add_event(req, ret);
2539 io_put_req_find_next(req, nxt);
2540 return 0;
2541#else
2542 return -EOPNOTSUPP;
2543#endif
2544}
2545
Jens Axboe221c5eb2019-01-17 09:41:58 -07002546static void io_poll_remove_one(struct io_kiocb *req)
2547{
2548 struct io_poll_iocb *poll = &req->poll;
2549
2550 spin_lock(&poll->head->lock);
2551 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07002552 if (!list_empty(&poll->wait.entry)) {
2553 list_del_init(&poll->wait.entry);
Jackie Liua197f662019-11-08 08:09:12 -07002554 io_queue_async_work(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002555 }
2556 spin_unlock(&poll->head->lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07002557 hash_del(&req->hash_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002558}
2559
2560static void io_poll_remove_all(struct io_ring_ctx *ctx)
2561{
Jens Axboe78076bb2019-12-04 19:56:40 -07002562 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002563 struct io_kiocb *req;
Jens Axboe78076bb2019-12-04 19:56:40 -07002564 int i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002565
2566 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07002567 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
2568 struct hlist_head *list;
2569
2570 list = &ctx->cancel_hash[i];
2571 hlist_for_each_entry_safe(req, tmp, list, hash_node)
2572 io_poll_remove_one(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002573 }
2574 spin_unlock_irq(&ctx->completion_lock);
2575}
2576
Jens Axboe47f46762019-11-09 17:43:02 -07002577static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
2578{
Jens Axboe78076bb2019-12-04 19:56:40 -07002579 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07002580 struct io_kiocb *req;
2581
Jens Axboe78076bb2019-12-04 19:56:40 -07002582 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
2583 hlist_for_each_entry(req, list, hash_node) {
2584 if (sqe_addr == req->user_data) {
Jens Axboeeac406c2019-11-14 12:09:58 -07002585 io_poll_remove_one(req);
2586 return 0;
2587 }
Jens Axboe47f46762019-11-09 17:43:02 -07002588 }
2589
2590 return -ENOENT;
2591}
2592
Jens Axboe3529d8c2019-12-19 18:24:38 -07002593static int io_poll_remove_prep(struct io_kiocb *req,
2594 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002595{
Jens Axboe221c5eb2019-01-17 09:41:58 -07002596 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2597 return -EINVAL;
2598 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
2599 sqe->poll_events)
2600 return -EINVAL;
2601
Jens Axboe0969e782019-12-17 18:40:57 -07002602 req->poll.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07002603 return 0;
2604}
2605
2606/*
2607 * Find a running poll command that matches one specified in sqe->addr,
2608 * and remove it if found.
2609 */
2610static int io_poll_remove(struct io_kiocb *req)
2611{
2612 struct io_ring_ctx *ctx = req->ctx;
2613 u64 addr;
2614 int ret;
2615
Jens Axboe0969e782019-12-17 18:40:57 -07002616 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002617 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07002618 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002619 spin_unlock_irq(&ctx->completion_lock);
2620
Jens Axboe78e19bb2019-11-06 15:21:34 -07002621 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002622 if (ret < 0)
2623 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06002624 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002625 return 0;
2626}
2627
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002628static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002629{
Jackie Liua197f662019-11-08 08:09:12 -07002630 struct io_ring_ctx *ctx = req->ctx;
2631
Jens Axboe8c838782019-03-12 15:48:16 -06002632 req->poll.done = true;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002633 if (error)
2634 io_cqring_fill_event(req, error);
2635 else
2636 io_cqring_fill_event(req, mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06002637 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002638}
2639
Jens Axboe561fb042019-10-24 07:25:42 -06002640static void io_poll_complete_work(struct io_wq_work **workptr)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002641{
Jens Axboe561fb042019-10-24 07:25:42 -06002642 struct io_wq_work *work = *workptr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002643 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
2644 struct io_poll_iocb *poll = &req->poll;
2645 struct poll_table_struct pt = { ._key = poll->events };
2646 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe89723d02019-11-05 15:32:58 -07002647 struct io_kiocb *nxt = NULL;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002648 __poll_t mask = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002649 int ret = 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002650
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002651 if (work->flags & IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06002652 WRITE_ONCE(poll->canceled, true);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002653 ret = -ECANCELED;
2654 } else if (READ_ONCE(poll->canceled)) {
2655 ret = -ECANCELED;
2656 }
Jens Axboe561fb042019-10-24 07:25:42 -06002657
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002658 if (ret != -ECANCELED)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002659 mask = vfs_poll(poll->file, &pt) & poll->events;
2660
2661 /*
2662 * Note that ->ki_cancel callers also delete iocb from active_reqs after
2663 * calling ->ki_cancel. We need the ctx_lock roundtrip here to
2664 * synchronize with them. In the cancellation case the list_del_init
2665 * itself is not actually needed, but harmless so we keep it in to
2666 * avoid further branches in the fast path.
2667 */
2668 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002669 if (!mask && ret != -ECANCELED) {
Jens Axboe392edb42019-12-09 17:52:20 -07002670 add_wait_queue(poll->head, &poll->wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002671 spin_unlock_irq(&ctx->completion_lock);
2672 return;
2673 }
Jens Axboe78076bb2019-12-04 19:56:40 -07002674 hash_del(&req->hash_node);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002675 io_poll_complete(req, mask, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002676 spin_unlock_irq(&ctx->completion_lock);
2677
Jens Axboe8c838782019-03-12 15:48:16 -06002678 io_cqring_ev_posted(ctx);
Jens Axboe89723d02019-11-05 15:32:58 -07002679
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002680 if (ret < 0)
2681 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08002682 io_put_req_find_next(req, &nxt);
Jens Axboe89723d02019-11-05 15:32:58 -07002683 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07002684 io_wq_assign_next(workptr, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002685}
2686
2687static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
2688 void *key)
2689{
Jens Axboee9444752019-11-26 15:02:04 -07002690 struct io_poll_iocb *poll = wait->private;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002691 struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
2692 struct io_ring_ctx *ctx = req->ctx;
2693 __poll_t mask = key_to_poll(key);
Jens Axboe8c838782019-03-12 15:48:16 -06002694 unsigned long flags;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002695
2696 /* for instances that support it check for an event match first: */
Jens Axboe8c838782019-03-12 15:48:16 -06002697 if (mask && !(mask & poll->events))
2698 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002699
Jens Axboe392edb42019-12-09 17:52:20 -07002700 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06002701
Jens Axboe7c9e7f02019-11-12 08:15:53 -07002702 /*
2703 * Run completion inline if we can. We're using trylock here because
2704 * we are violating the completion_lock -> poll wq lock ordering.
2705 * If we have a link timeout we're going to need the completion_lock
2706 * for finalizing the request, mark us as having grabbed that already.
2707 */
Jens Axboe8c838782019-03-12 15:48:16 -06002708 if (mask && spin_trylock_irqsave(&ctx->completion_lock, flags)) {
Jens Axboe78076bb2019-12-04 19:56:40 -07002709 hash_del(&req->hash_node);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002710 io_poll_complete(req, mask, 0);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07002711 req->flags |= REQ_F_COMP_LOCKED;
2712 io_put_req(req);
Jens Axboe8c838782019-03-12 15:48:16 -06002713 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2714
2715 io_cqring_ev_posted(ctx);
Jens Axboe8c838782019-03-12 15:48:16 -06002716 } else {
Jackie Liua197f662019-11-08 08:09:12 -07002717 io_queue_async_work(req);
Jens Axboe8c838782019-03-12 15:48:16 -06002718 }
2719
Jens Axboe221c5eb2019-01-17 09:41:58 -07002720 return 1;
2721}
2722
2723struct io_poll_table {
2724 struct poll_table_struct pt;
2725 struct io_kiocb *req;
2726 int error;
2727};
2728
2729static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
2730 struct poll_table_struct *p)
2731{
2732 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
2733
2734 if (unlikely(pt->req->poll.head)) {
2735 pt->error = -EINVAL;
2736 return;
2737 }
2738
2739 pt->error = 0;
2740 pt->req->poll.head = head;
Jens Axboe392edb42019-12-09 17:52:20 -07002741 add_wait_queue(head, &pt->req->poll.wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002742}
2743
Jens Axboeeac406c2019-11-14 12:09:58 -07002744static void io_poll_req_insert(struct io_kiocb *req)
2745{
2746 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07002747 struct hlist_head *list;
Jens Axboeeac406c2019-11-14 12:09:58 -07002748
Jens Axboe78076bb2019-12-04 19:56:40 -07002749 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
2750 hlist_add_head(&req->hash_node, list);
Jens Axboeeac406c2019-11-14 12:09:58 -07002751}
2752
Jens Axboe3529d8c2019-12-19 18:24:38 -07002753static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002754{
2755 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002756 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002757
2758 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2759 return -EINVAL;
2760 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
2761 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06002762 if (!poll->file)
2763 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002764
Jens Axboe221c5eb2019-01-17 09:41:58 -07002765 events = READ_ONCE(sqe->poll_events);
2766 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
Jens Axboe0969e782019-12-17 18:40:57 -07002767 return 0;
2768}
2769
2770static int io_poll_add(struct io_kiocb *req, struct io_kiocb **nxt)
2771{
2772 struct io_poll_iocb *poll = &req->poll;
2773 struct io_ring_ctx *ctx = req->ctx;
2774 struct io_poll_table ipt;
2775 bool cancel = false;
2776 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07002777
2778 INIT_IO_WORK(&req->work, io_poll_complete_work);
Jens Axboe78076bb2019-12-04 19:56:40 -07002779 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002780
Jens Axboe221c5eb2019-01-17 09:41:58 -07002781 poll->head = NULL;
Jens Axboe8c838782019-03-12 15:48:16 -06002782 poll->done = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002783 poll->canceled = false;
2784
2785 ipt.pt._qproc = io_poll_queue_proc;
2786 ipt.pt._key = poll->events;
2787 ipt.req = req;
2788 ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
2789
2790 /* initialized the list so that we can do list_empty checks */
Jens Axboe392edb42019-12-09 17:52:20 -07002791 INIT_LIST_HEAD(&poll->wait.entry);
2792 init_waitqueue_func_entry(&poll->wait, io_poll_wake);
2793 poll->wait.private = poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002794
Jens Axboe36703242019-07-25 10:20:18 -06002795 INIT_LIST_HEAD(&req->list);
2796
Jens Axboe221c5eb2019-01-17 09:41:58 -07002797 mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002798
2799 spin_lock_irq(&ctx->completion_lock);
Jens Axboe8c838782019-03-12 15:48:16 -06002800 if (likely(poll->head)) {
2801 spin_lock(&poll->head->lock);
Jens Axboe392edb42019-12-09 17:52:20 -07002802 if (unlikely(list_empty(&poll->wait.entry))) {
Jens Axboe8c838782019-03-12 15:48:16 -06002803 if (ipt.error)
2804 cancel = true;
2805 ipt.error = 0;
2806 mask = 0;
2807 }
2808 if (mask || ipt.error)
Jens Axboe392edb42019-12-09 17:52:20 -07002809 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06002810 else if (cancel)
2811 WRITE_ONCE(poll->canceled, true);
2812 else if (!poll->done) /* actually waiting for an event */
Jens Axboeeac406c2019-11-14 12:09:58 -07002813 io_poll_req_insert(req);
Jens Axboe8c838782019-03-12 15:48:16 -06002814 spin_unlock(&poll->head->lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002815 }
Jens Axboe8c838782019-03-12 15:48:16 -06002816 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06002817 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002818 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06002819 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07002820 spin_unlock_irq(&ctx->completion_lock);
2821
Jens Axboe8c838782019-03-12 15:48:16 -06002822 if (mask) {
2823 io_cqring_ev_posted(ctx);
Jackie Liuec9c02a2019-11-08 23:50:36 +08002824 io_put_req_find_next(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002825 }
Jens Axboe8c838782019-03-12 15:48:16 -06002826 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002827}
2828
Jens Axboe5262f562019-09-17 12:26:57 -06002829static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
2830{
Jens Axboead8a48a2019-11-15 08:49:11 -07002831 struct io_timeout_data *data = container_of(timer,
2832 struct io_timeout_data, timer);
2833 struct io_kiocb *req = data->req;
2834 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06002835 unsigned long flags;
2836
Jens Axboe5262f562019-09-17 12:26:57 -06002837 atomic_inc(&ctx->cq_timeouts);
2838
2839 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08002840 /*
Jens Axboe11365042019-10-16 09:08:32 -06002841 * We could be racing with timeout deletion. If the list is empty,
2842 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08002843 */
Jens Axboe842f9612019-10-29 12:34:10 -06002844 if (!list_empty(&req->list)) {
Jens Axboe11365042019-10-16 09:08:32 -06002845 struct io_kiocb *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06002846
Jens Axboe11365042019-10-16 09:08:32 -06002847 /*
2848 * Adjust the reqs sequence before the current one because it
Brian Gianforcarod195a662019-12-13 03:09:50 -08002849 * will consume a slot in the cq_ring and the cq_tail
Jens Axboe11365042019-10-16 09:08:32 -06002850 * pointer will be increased, otherwise other timeout reqs may
2851 * return in advance without waiting for enough wait_nr.
2852 */
2853 prev = req;
2854 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
2855 prev->sequence++;
Jens Axboe11365042019-10-16 09:08:32 -06002856 list_del_init(&req->list);
Jens Axboe11365042019-10-16 09:08:32 -06002857 }
Jens Axboe842f9612019-10-29 12:34:10 -06002858
Jens Axboe78e19bb2019-11-06 15:21:34 -07002859 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06002860 io_commit_cqring(ctx);
2861 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2862
2863 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002864 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06002865 io_put_req(req);
2866 return HRTIMER_NORESTART;
2867}
2868
Jens Axboe47f46762019-11-09 17:43:02 -07002869static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
2870{
2871 struct io_kiocb *req;
2872 int ret = -ENOENT;
2873
2874 list_for_each_entry(req, &ctx->timeout_list, list) {
2875 if (user_data == req->user_data) {
2876 list_del_init(&req->list);
2877 ret = 0;
2878 break;
2879 }
2880 }
2881
2882 if (ret == -ENOENT)
2883 return ret;
2884
Jens Axboe2d283902019-12-04 11:08:05 -07002885 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe47f46762019-11-09 17:43:02 -07002886 if (ret == -1)
2887 return -EALREADY;
2888
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002889 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07002890 io_cqring_fill_event(req, -ECANCELED);
2891 io_put_req(req);
2892 return 0;
2893}
2894
Jens Axboe3529d8c2019-12-19 18:24:38 -07002895static int io_timeout_remove_prep(struct io_kiocb *req,
2896 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07002897{
Jens Axboeb29472e2019-12-17 18:50:29 -07002898 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2899 return -EINVAL;
2900 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
2901 return -EINVAL;
2902
2903 req->timeout.addr = READ_ONCE(sqe->addr);
2904 req->timeout.flags = READ_ONCE(sqe->timeout_flags);
2905 if (req->timeout.flags)
2906 return -EINVAL;
2907
Jens Axboeb29472e2019-12-17 18:50:29 -07002908 return 0;
2909}
2910
Jens Axboe11365042019-10-16 09:08:32 -06002911/*
2912 * Remove or update an existing timeout command
2913 */
Jens Axboefc4df992019-12-10 14:38:45 -07002914static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06002915{
2916 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07002917 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06002918
Jens Axboe11365042019-10-16 09:08:32 -06002919 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb29472e2019-12-17 18:50:29 -07002920 ret = io_timeout_cancel(ctx, req->timeout.addr);
Jens Axboe11365042019-10-16 09:08:32 -06002921
Jens Axboe47f46762019-11-09 17:43:02 -07002922 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06002923 io_commit_cqring(ctx);
2924 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06002925 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002926 if (ret < 0)
2927 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08002928 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06002929 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06002930}
2931
Jens Axboe3529d8c2019-12-19 18:24:38 -07002932static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07002933 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06002934{
Jens Axboead8a48a2019-11-15 08:49:11 -07002935 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06002936 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06002937
Jens Axboead8a48a2019-11-15 08:49:11 -07002938 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06002939 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07002940 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06002941 return -EINVAL;
Jens Axboe2d283902019-12-04 11:08:05 -07002942 if (sqe->off && is_timeout_link)
2943 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06002944 flags = READ_ONCE(sqe->timeout_flags);
2945 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06002946 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06002947
Jens Axboe26a61672019-12-20 09:02:01 -07002948 req->timeout.count = READ_ONCE(sqe->off);
2949
Jens Axboe3529d8c2019-12-19 18:24:38 -07002950 if (!req->io && io_alloc_async_ctx(req))
Jens Axboe26a61672019-12-20 09:02:01 -07002951 return -ENOMEM;
2952
2953 data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07002954 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07002955 req->flags |= REQ_F_TIMEOUT;
2956
2957 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06002958 return -EFAULT;
2959
Jens Axboe11365042019-10-16 09:08:32 -06002960 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07002961 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06002962 else
Jens Axboead8a48a2019-11-15 08:49:11 -07002963 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06002964
Jens Axboead8a48a2019-11-15 08:49:11 -07002965 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
2966 return 0;
2967}
2968
Jens Axboefc4df992019-12-10 14:38:45 -07002969static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07002970{
2971 unsigned count;
2972 struct io_ring_ctx *ctx = req->ctx;
2973 struct io_timeout_data *data;
2974 struct list_head *entry;
2975 unsigned span = 0;
Jens Axboead8a48a2019-11-15 08:49:11 -07002976
Jens Axboe2d283902019-12-04 11:08:05 -07002977 data = &req->io->timeout;
Jens Axboe93bd25b2019-11-11 23:34:31 -07002978
Jens Axboe5262f562019-09-17 12:26:57 -06002979 /*
2980 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07002981 * timeout event to be satisfied. If it isn't set, then this is
2982 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06002983 */
Jens Axboe26a61672019-12-20 09:02:01 -07002984 count = req->timeout.count;
Jens Axboe93bd25b2019-11-11 23:34:31 -07002985 if (!count) {
2986 req->flags |= REQ_F_TIMEOUT_NOSEQ;
2987 spin_lock_irq(&ctx->completion_lock);
2988 entry = ctx->timeout_list.prev;
2989 goto add;
2990 }
Jens Axboe5262f562019-09-17 12:26:57 -06002991
2992 req->sequence = ctx->cached_sq_head + count - 1;
Jens Axboe2d283902019-12-04 11:08:05 -07002993 data->seq_offset = count;
Jens Axboe5262f562019-09-17 12:26:57 -06002994
2995 /*
2996 * Insertion sort, ensuring the first entry in the list is always
2997 * the one we need first.
2998 */
Jens Axboe5262f562019-09-17 12:26:57 -06002999 spin_lock_irq(&ctx->completion_lock);
3000 list_for_each_prev(entry, &ctx->timeout_list) {
3001 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
yangerkun5da0fb12019-10-15 21:59:29 +08003002 unsigned nxt_sq_head;
3003 long long tmp, tmp_nxt;
Jens Axboe2d283902019-12-04 11:08:05 -07003004 u32 nxt_offset = nxt->io->timeout.seq_offset;
Jens Axboe5262f562019-09-17 12:26:57 -06003005
Jens Axboe93bd25b2019-11-11 23:34:31 -07003006 if (nxt->flags & REQ_F_TIMEOUT_NOSEQ)
3007 continue;
3008
yangerkun5da0fb12019-10-15 21:59:29 +08003009 /*
3010 * Since cached_sq_head + count - 1 can overflow, use type long
3011 * long to store it.
3012 */
3013 tmp = (long long)ctx->cached_sq_head + count - 1;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +03003014 nxt_sq_head = nxt->sequence - nxt_offset + 1;
3015 tmp_nxt = (long long)nxt_sq_head + nxt_offset - 1;
yangerkun5da0fb12019-10-15 21:59:29 +08003016
3017 /*
3018 * cached_sq_head may overflow, and it will never overflow twice
3019 * once there is some timeout req still be valid.
3020 */
3021 if (ctx->cached_sq_head < nxt_sq_head)
yangerkun8b07a652019-10-17 12:12:35 +08003022 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08003023
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003024 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06003025 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003026
3027 /*
3028 * Sequence of reqs after the insert one and itself should
3029 * be adjusted because each timeout req consumes a slot.
3030 */
3031 span++;
3032 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06003033 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003034 req->sequence -= span;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003035add:
Jens Axboe5262f562019-09-17 12:26:57 -06003036 list_add(&req->list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07003037 data->timer.function = io_timeout_fn;
3038 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06003039 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06003040 return 0;
3041}
3042
Jens Axboe62755e32019-10-28 21:49:21 -06003043static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06003044{
Jens Axboe62755e32019-10-28 21:49:21 -06003045 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06003046
Jens Axboe62755e32019-10-28 21:49:21 -06003047 return req->user_data == (unsigned long) data;
3048}
3049
Jens Axboee977d6d2019-11-05 12:39:45 -07003050static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06003051{
Jens Axboe62755e32019-10-28 21:49:21 -06003052 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06003053 int ret = 0;
3054
Jens Axboe62755e32019-10-28 21:49:21 -06003055 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
3056 switch (cancel_ret) {
3057 case IO_WQ_CANCEL_OK:
3058 ret = 0;
3059 break;
3060 case IO_WQ_CANCEL_RUNNING:
3061 ret = -EALREADY;
3062 break;
3063 case IO_WQ_CANCEL_NOTFOUND:
3064 ret = -ENOENT;
3065 break;
3066 }
3067
Jens Axboee977d6d2019-11-05 12:39:45 -07003068 return ret;
3069}
3070
Jens Axboe47f46762019-11-09 17:43:02 -07003071static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
3072 struct io_kiocb *req, __u64 sqe_addr,
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003073 struct io_kiocb **nxt, int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07003074{
3075 unsigned long flags;
3076 int ret;
3077
3078 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
3079 if (ret != -ENOENT) {
3080 spin_lock_irqsave(&ctx->completion_lock, flags);
3081 goto done;
3082 }
3083
3084 spin_lock_irqsave(&ctx->completion_lock, flags);
3085 ret = io_timeout_cancel(ctx, sqe_addr);
3086 if (ret != -ENOENT)
3087 goto done;
3088 ret = io_poll_cancel(ctx, sqe_addr);
3089done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003090 if (!ret)
3091 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07003092 io_cqring_fill_event(req, ret);
3093 io_commit_cqring(ctx);
3094 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3095 io_cqring_ev_posted(ctx);
3096
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003097 if (ret < 0)
3098 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07003099 io_put_req_find_next(req, nxt);
3100}
3101
Jens Axboe3529d8c2019-12-19 18:24:38 -07003102static int io_async_cancel_prep(struct io_kiocb *req,
3103 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07003104{
Jens Axboefbf23842019-12-17 18:45:56 -07003105 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07003106 return -EINVAL;
3107 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
3108 sqe->cancel_flags)
3109 return -EINVAL;
3110
Jens Axboefbf23842019-12-17 18:45:56 -07003111 req->cancel.addr = READ_ONCE(sqe->addr);
3112 return 0;
3113}
3114
3115static int io_async_cancel(struct io_kiocb *req, struct io_kiocb **nxt)
3116{
3117 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07003118
3119 io_async_find_and_cancel(ctx, req, req->cancel.addr, nxt, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06003120 return 0;
3121}
3122
Jens Axboe3529d8c2019-12-19 18:24:38 -07003123static int io_req_defer_prep(struct io_kiocb *req,
3124 const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003125{
Jens Axboee7815732019-12-17 19:45:06 -07003126 ssize_t ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003127
Jens Axboed625c6e2019-12-17 19:53:05 -07003128 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07003129 case IORING_OP_NOP:
3130 break;
Jens Axboef67676d2019-12-02 11:03:47 -07003131 case IORING_OP_READV:
3132 case IORING_OP_READ_FIXED:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003133 ret = io_read_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07003134 break;
3135 case IORING_OP_WRITEV:
3136 case IORING_OP_WRITE_FIXED:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003137 ret = io_write_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07003138 break;
Jens Axboe0969e782019-12-17 18:40:57 -07003139 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003140 ret = io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07003141 break;
3142 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003143 ret = io_poll_remove_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07003144 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003145 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003146 ret = io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003147 break;
3148 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003149 ret = io_prep_sfr(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003150 break;
Jens Axboe03b12302019-12-02 18:50:25 -07003151 case IORING_OP_SENDMSG:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003152 ret = io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07003153 break;
3154 case IORING_OP_RECVMSG:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003155 ret = io_recvmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07003156 break;
Jens Axboef499a022019-12-02 16:28:46 -07003157 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003158 ret = io_connect_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07003159 break;
Jens Axboe2d283902019-12-04 11:08:05 -07003160 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003161 ret = io_timeout_prep(req, sqe, false);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003162 break;
Jens Axboeb29472e2019-12-17 18:50:29 -07003163 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003164 ret = io_timeout_remove_prep(req, sqe);
Jens Axboeb29472e2019-12-17 18:50:29 -07003165 break;
Jens Axboefbf23842019-12-17 18:45:56 -07003166 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003167 ret = io_async_cancel_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07003168 break;
Jens Axboe2d283902019-12-04 11:08:05 -07003169 case IORING_OP_LINK_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003170 ret = io_timeout_prep(req, sqe, true);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003171 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003172 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003173 ret = io_accept_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003174 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07003175 case IORING_OP_FALLOCATE:
3176 ret = io_fallocate_prep(req, sqe);
3177 break;
Jens Axboef67676d2019-12-02 11:03:47 -07003178 default:
Jens Axboee7815732019-12-17 19:45:06 -07003179 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
3180 req->opcode);
3181 ret = -EINVAL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003182 break;
Jens Axboef67676d2019-12-02 11:03:47 -07003183 }
3184
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003185 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003186}
3187
Jens Axboe3529d8c2019-12-19 18:24:38 -07003188static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboede0617e2019-04-06 21:51:27 -06003189{
Jackie Liua197f662019-11-08 08:09:12 -07003190 struct io_ring_ctx *ctx = req->ctx;
Jens Axboef67676d2019-12-02 11:03:47 -07003191 int ret;
Jens Axboede0617e2019-04-06 21:51:27 -06003192
Bob Liu9d858b22019-11-13 18:06:25 +08003193 /* Still need defer if there is pending req in defer list. */
3194 if (!req_need_defer(req) && list_empty(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06003195 return 0;
3196
Jens Axboe3529d8c2019-12-19 18:24:38 -07003197 if (!req->io && io_alloc_async_ctx(req))
Jens Axboede0617e2019-04-06 21:51:27 -06003198 return -EAGAIN;
3199
Jens Axboe3529d8c2019-12-19 18:24:38 -07003200 ret = io_req_defer_prep(req, sqe);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003201 if (ret < 0)
Jens Axboe2d283902019-12-04 11:08:05 -07003202 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07003203
Jens Axboede0617e2019-04-06 21:51:27 -06003204 spin_lock_irq(&ctx->completion_lock);
Bob Liu9d858b22019-11-13 18:06:25 +08003205 if (!req_need_defer(req) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06003206 spin_unlock_irq(&ctx->completion_lock);
Jens Axboede0617e2019-04-06 21:51:27 -06003207 return 0;
3208 }
3209
Jens Axboe915967f2019-11-21 09:01:20 -07003210 trace_io_uring_defer(ctx, req, req->user_data);
Jens Axboede0617e2019-04-06 21:51:27 -06003211 list_add_tail(&req->list, &ctx->defer_list);
3212 spin_unlock_irq(&ctx->completion_lock);
3213 return -EIOCBQUEUED;
3214}
3215
Jens Axboe3529d8c2019-12-19 18:24:38 -07003216static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
3217 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003218{
Jackie Liua197f662019-11-08 08:09:12 -07003219 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07003220 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003221
Jens Axboed625c6e2019-12-17 19:53:05 -07003222 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07003223 case IORING_OP_NOP:
Jens Axboe78e19bb2019-11-06 15:21:34 -07003224 ret = io_nop(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003225 break;
3226 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003227 case IORING_OP_READ_FIXED:
3228 if (sqe) {
3229 ret = io_read_prep(req, sqe, force_nonblock);
3230 if (ret < 0)
3231 break;
3232 }
Pavel Begunkov267bc902019-11-07 01:41:08 +03003233 ret = io_read(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003234 break;
3235 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07003236 case IORING_OP_WRITE_FIXED:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003237 if (sqe) {
3238 ret = io_write_prep(req, sqe, force_nonblock);
3239 if (ret < 0)
3240 break;
3241 }
Pavel Begunkov267bc902019-11-07 01:41:08 +03003242 ret = io_write(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003243 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003244 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003245 if (sqe) {
3246 ret = io_prep_fsync(req, sqe);
3247 if (ret < 0)
3248 break;
3249 }
Jens Axboefc4df992019-12-10 14:38:45 -07003250 ret = io_fsync(req, nxt, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003251 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003252 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003253 if (sqe) {
3254 ret = io_poll_add_prep(req, sqe);
3255 if (ret)
3256 break;
3257 }
Jens Axboefc4df992019-12-10 14:38:45 -07003258 ret = io_poll_add(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003259 break;
3260 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003261 if (sqe) {
3262 ret = io_poll_remove_prep(req, sqe);
3263 if (ret < 0)
3264 break;
3265 }
Jens Axboefc4df992019-12-10 14:38:45 -07003266 ret = io_poll_remove(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003267 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003268 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003269 if (sqe) {
3270 ret = io_prep_sfr(req, sqe);
3271 if (ret < 0)
3272 break;
3273 }
Jens Axboefc4df992019-12-10 14:38:45 -07003274 ret = io_sync_file_range(req, nxt, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003275 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003276 case IORING_OP_SENDMSG:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003277 if (sqe) {
3278 ret = io_sendmsg_prep(req, sqe);
3279 if (ret < 0)
3280 break;
3281 }
Jens Axboefc4df992019-12-10 14:38:45 -07003282 ret = io_sendmsg(req, nxt, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06003283 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06003284 case IORING_OP_RECVMSG:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003285 if (sqe) {
3286 ret = io_recvmsg_prep(req, sqe);
3287 if (ret)
3288 break;
3289 }
Jens Axboefc4df992019-12-10 14:38:45 -07003290 ret = io_recvmsg(req, nxt, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06003291 break;
Jens Axboe5262f562019-09-17 12:26:57 -06003292 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003293 if (sqe) {
3294 ret = io_timeout_prep(req, sqe, false);
3295 if (ret)
3296 break;
3297 }
Jens Axboefc4df992019-12-10 14:38:45 -07003298 ret = io_timeout(req);
Jens Axboe5262f562019-09-17 12:26:57 -06003299 break;
Jens Axboe11365042019-10-16 09:08:32 -06003300 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003301 if (sqe) {
3302 ret = io_timeout_remove_prep(req, sqe);
3303 if (ret)
3304 break;
3305 }
Jens Axboefc4df992019-12-10 14:38:45 -07003306 ret = io_timeout_remove(req);
Jens Axboe11365042019-10-16 09:08:32 -06003307 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06003308 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003309 if (sqe) {
3310 ret = io_accept_prep(req, sqe);
3311 if (ret)
3312 break;
3313 }
Jens Axboefc4df992019-12-10 14:38:45 -07003314 ret = io_accept(req, nxt, force_nonblock);
Jens Axboe17f2fe32019-10-17 14:42:58 -06003315 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003316 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003317 if (sqe) {
3318 ret = io_connect_prep(req, sqe);
3319 if (ret)
3320 break;
3321 }
Jens Axboefc4df992019-12-10 14:38:45 -07003322 ret = io_connect(req, nxt, force_nonblock);
Jens Axboef8e85cf2019-11-23 14:24:24 -07003323 break;
Jens Axboe62755e32019-10-28 21:49:21 -06003324 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003325 if (sqe) {
3326 ret = io_async_cancel_prep(req, sqe);
3327 if (ret)
3328 break;
3329 }
Jens Axboefc4df992019-12-10 14:38:45 -07003330 ret = io_async_cancel(req, nxt);
Jens Axboe62755e32019-10-28 21:49:21 -06003331 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07003332 case IORING_OP_FALLOCATE:
3333 if (sqe) {
3334 ret = io_fallocate_prep(req, sqe);
3335 if (ret)
3336 break;
3337 }
3338 ret = io_fallocate(req, nxt, force_nonblock);
3339 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003340 default:
3341 ret = -EINVAL;
3342 break;
3343 }
3344
Jens Axboedef596e2019-01-09 08:59:42 -07003345 if (ret)
3346 return ret;
3347
3348 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe11ba8202020-01-15 21:51:17 -07003349 const bool in_async = io_wq_current_is_worker();
3350
Jens Axboe9e645e112019-05-10 16:07:28 -06003351 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07003352 return -EAGAIN;
3353
Jens Axboe11ba8202020-01-15 21:51:17 -07003354 /* workqueue context doesn't hold uring_lock, grab it now */
3355 if (in_async)
3356 mutex_lock(&ctx->uring_lock);
3357
Jens Axboedef596e2019-01-09 08:59:42 -07003358 io_iopoll_req_issued(req);
Jens Axboe11ba8202020-01-15 21:51:17 -07003359
3360 if (in_async)
3361 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07003362 }
3363
3364 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003365}
3366
Jens Axboe561fb042019-10-24 07:25:42 -06003367static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboe31b51512019-01-18 22:56:34 -07003368{
Jens Axboe561fb042019-10-24 07:25:42 -06003369 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003370 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe561fb042019-10-24 07:25:42 -06003371 struct io_kiocb *nxt = NULL;
3372 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003373
Jens Axboe561fb042019-10-24 07:25:42 -06003374 if (work->flags & IO_WQ_WORK_CANCEL)
3375 ret = -ECANCELED;
Jens Axboe31b51512019-01-18 22:56:34 -07003376
Jens Axboe561fb042019-10-24 07:25:42 -06003377 if (!ret) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003378 req->has_user = (work->flags & IO_WQ_WORK_HAS_MM) != 0;
3379 req->in_async = true;
Jens Axboe561fb042019-10-24 07:25:42 -06003380 do {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003381 ret = io_issue_sqe(req, NULL, &nxt, false);
Jens Axboe561fb042019-10-24 07:25:42 -06003382 /*
3383 * We can get EAGAIN for polled IO even though we're
3384 * forcing a sync submission from here, since we can't
3385 * wait for request slots on the block side.
3386 */
3387 if (ret != -EAGAIN)
3388 break;
3389 cond_resched();
3390 } while (1);
3391 }
Jens Axboe31b51512019-01-18 22:56:34 -07003392
Jens Axboe561fb042019-10-24 07:25:42 -06003393 /* drop submission reference */
Jackie Liuec9c02a2019-11-08 23:50:36 +08003394 io_put_req(req);
Jens Axboe817869d2019-04-30 14:44:05 -06003395
Jens Axboe561fb042019-10-24 07:25:42 -06003396 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003397 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07003398 io_cqring_add_event(req, ret);
Jens Axboe817869d2019-04-30 14:44:05 -06003399 io_put_req(req);
Jens Axboeedafcce2019-01-09 09:16:05 -07003400 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003401
Jens Axboe561fb042019-10-24 07:25:42 -06003402 /* if a dependent link is ready, pass it back */
Jens Axboe78912932020-01-14 22:09:06 -07003403 if (!ret && nxt)
3404 io_wq_assign_next(workptr, nxt);
Jens Axboe31b51512019-01-18 22:56:34 -07003405}
Jens Axboe2b188cc2019-01-07 10:46:33 -07003406
Jens Axboe9e3aa612019-12-11 15:55:43 -07003407static bool io_req_op_valid(int op)
3408{
3409 return op >= IORING_OP_NOP && op < IORING_OP_LAST;
3410}
3411
Jens Axboed625c6e2019-12-17 19:53:05 -07003412static int io_req_needs_file(struct io_kiocb *req)
Jens Axboe09bb8392019-03-13 12:39:28 -06003413{
Jens Axboed625c6e2019-12-17 19:53:05 -07003414 switch (req->opcode) {
Jens Axboe09bb8392019-03-13 12:39:28 -06003415 case IORING_OP_NOP:
3416 case IORING_OP_POLL_REMOVE:
Pavel Begunkov5683e542019-11-14 00:59:19 +03003417 case IORING_OP_TIMEOUT:
Pavel Begunkova320e9f2019-11-14 00:11:01 +03003418 case IORING_OP_TIMEOUT_REMOVE:
3419 case IORING_OP_ASYNC_CANCEL:
3420 case IORING_OP_LINK_TIMEOUT:
Jens Axboe9e3aa612019-12-11 15:55:43 -07003421 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06003422 default:
Jens Axboed625c6e2019-12-17 19:53:05 -07003423 if (io_req_op_valid(req->opcode))
Jens Axboe9e3aa612019-12-11 15:55:43 -07003424 return 1;
3425 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06003426 }
3427}
3428
Jens Axboe65e19f52019-10-26 07:20:21 -06003429static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
3430 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06003431{
Jens Axboe65e19f52019-10-26 07:20:21 -06003432 struct fixed_file_table *table;
3433
3434 table = &ctx->file_table[index >> IORING_FILE_TABLE_SHIFT];
3435 return table->files[index & IORING_FILE_TABLE_MASK];
3436}
3437
Jens Axboe3529d8c2019-12-19 18:24:38 -07003438static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
3439 const struct io_uring_sqe *sqe)
Jens Axboe09bb8392019-03-13 12:39:28 -06003440{
Jackie Liua197f662019-11-08 08:09:12 -07003441 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe09bb8392019-03-13 12:39:28 -06003442 unsigned flags;
Jens Axboe9e3aa612019-12-11 15:55:43 -07003443 int fd, ret;
Jens Axboe09bb8392019-03-13 12:39:28 -06003444
Jens Axboe3529d8c2019-12-19 18:24:38 -07003445 flags = READ_ONCE(sqe->flags);
3446 fd = READ_ONCE(sqe->fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06003447
Jackie Liu4fe2c962019-09-09 20:50:40 +08003448 if (flags & IOSQE_IO_DRAIN)
Jens Axboede0617e2019-04-06 21:51:27 -06003449 req->flags |= REQ_F_IO_DRAIN;
Jens Axboede0617e2019-04-06 21:51:27 -06003450
Jens Axboed625c6e2019-12-17 19:53:05 -07003451 ret = io_req_needs_file(req);
Jens Axboe9e3aa612019-12-11 15:55:43 -07003452 if (ret <= 0)
3453 return ret;
Jens Axboe09bb8392019-03-13 12:39:28 -06003454
3455 if (flags & IOSQE_FIXED_FILE) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003456 if (unlikely(!ctx->file_table ||
Jens Axboe09bb8392019-03-13 12:39:28 -06003457 (unsigned) fd >= ctx->nr_user_files))
3458 return -EBADF;
Jens Axboeb7620122019-10-26 07:22:55 -06003459 fd = array_index_nospec(fd, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06003460 req->file = io_file_from_index(ctx, fd);
3461 if (!req->file)
Jens Axboe08a45172019-10-03 08:11:03 -06003462 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06003463 req->flags |= REQ_F_FIXED_FILE;
3464 } else {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003465 if (req->needs_fixed_file)
Jens Axboe09bb8392019-03-13 12:39:28 -06003466 return -EBADF;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02003467 trace_io_uring_file_get(ctx, fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06003468 req->file = io_file_get(state, fd);
3469 if (unlikely(!req->file))
3470 return -EBADF;
3471 }
3472
3473 return 0;
3474}
3475
Jackie Liua197f662019-11-08 08:09:12 -07003476static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003477{
Jens Axboefcb323c2019-10-24 12:39:47 -06003478 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07003479 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06003480
3481 rcu_read_lock();
3482 spin_lock_irq(&ctx->inflight_lock);
3483 /*
3484 * We use the f_ops->flush() handler to ensure that we can flush
3485 * out work accessing these files if the fd is closed. Check if
3486 * the fd has changed since we started down this path, and disallow
3487 * this operation if it has.
3488 */
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003489 if (fcheck(req->ring_fd) == req->ring_file) {
Jens Axboefcb323c2019-10-24 12:39:47 -06003490 list_add(&req->inflight_entry, &ctx->inflight_list);
3491 req->flags |= REQ_F_INFLIGHT;
3492 req->work.files = current->files;
3493 ret = 0;
3494 }
3495 spin_unlock_irq(&ctx->inflight_lock);
3496 rcu_read_unlock();
3497
3498 return ret;
3499}
3500
Jens Axboe2665abf2019-11-05 12:40:47 -07003501static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
3502{
Jens Axboead8a48a2019-11-15 08:49:11 -07003503 struct io_timeout_data *data = container_of(timer,
3504 struct io_timeout_data, timer);
3505 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07003506 struct io_ring_ctx *ctx = req->ctx;
3507 struct io_kiocb *prev = NULL;
3508 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07003509
3510 spin_lock_irqsave(&ctx->completion_lock, flags);
3511
3512 /*
3513 * We don't expect the list to be empty, that will only happen if we
3514 * race with the completion of the linked work.
3515 */
Pavel Begunkov44932332019-12-05 16:16:35 +03003516 if (!list_empty(&req->link_list)) {
3517 prev = list_entry(req->link_list.prev, struct io_kiocb,
3518 link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07003519 if (refcount_inc_not_zero(&prev->refs)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03003520 list_del_init(&req->link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07003521 prev->flags &= ~REQ_F_LINK_TIMEOUT;
3522 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07003523 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07003524 }
3525
3526 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3527
3528 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003529 req_set_fail_links(prev);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003530 io_async_find_and_cancel(ctx, req, prev->user_data, NULL,
3531 -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07003532 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07003533 } else {
3534 io_cqring_add_event(req, -ETIME);
3535 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07003536 }
Jens Axboe2665abf2019-11-05 12:40:47 -07003537 return HRTIMER_NORESTART;
3538}
3539
Jens Axboead8a48a2019-11-15 08:49:11 -07003540static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07003541{
Jens Axboe76a46e02019-11-10 23:34:16 -07003542 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07003543
Jens Axboe76a46e02019-11-10 23:34:16 -07003544 /*
3545 * If the list is now empty, then our linked request finished before
3546 * we got a chance to setup the timer
3547 */
3548 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov44932332019-12-05 16:16:35 +03003549 if (!list_empty(&req->link_list)) {
Jens Axboe2d283902019-12-04 11:08:05 -07003550 struct io_timeout_data *data = &req->io->timeout;
Jens Axboe94ae5e72019-11-14 19:39:52 -07003551
Jens Axboead8a48a2019-11-15 08:49:11 -07003552 data->timer.function = io_link_timeout_fn;
3553 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
3554 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07003555 }
Jens Axboe76a46e02019-11-10 23:34:16 -07003556 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07003557
Jens Axboe2665abf2019-11-05 12:40:47 -07003558 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07003559 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07003560}
3561
Jens Axboead8a48a2019-11-15 08:49:11 -07003562static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07003563{
3564 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003565
Jens Axboe2665abf2019-11-05 12:40:47 -07003566 if (!(req->flags & REQ_F_LINK))
3567 return NULL;
3568
Pavel Begunkov44932332019-12-05 16:16:35 +03003569 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
3570 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07003571 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07003572 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07003573
Jens Axboe76a46e02019-11-10 23:34:16 -07003574 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07003575 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07003576}
3577
Jens Axboe3529d8c2019-12-19 18:24:38 -07003578static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003579{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07003580 struct io_kiocb *linked_timeout;
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03003581 struct io_kiocb *nxt = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003582 int ret;
3583
Jens Axboe4a0a7a12019-12-09 20:01:01 -07003584again:
3585 linked_timeout = io_prep_linked_timeout(req);
3586
Jens Axboe3529d8c2019-12-19 18:24:38 -07003587 ret = io_issue_sqe(req, sqe, &nxt, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06003588
3589 /*
3590 * We async punt it if the file wasn't marked NOWAIT, or if the file
3591 * doesn't support non-blocking read/write attempts
3592 */
3593 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
3594 (req->flags & REQ_F_MUST_PUNT))) {
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03003595 if (req->work.flags & IO_WQ_WORK_NEEDS_FILES) {
3596 ret = io_grab_files(req);
3597 if (ret)
3598 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003599 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03003600
3601 /*
3602 * Queued up for async execution, worker will release
3603 * submit reference when the iocb is actually submitted.
3604 */
3605 io_queue_async_work(req);
Jens Axboe4a0a7a12019-12-09 20:01:01 -07003606 goto done_req;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003607 }
Jens Axboee65ef562019-03-12 10:16:44 -06003608
Jens Axboefcb323c2019-10-24 12:39:47 -06003609err:
Jens Axboee65ef562019-03-12 10:16:44 -06003610 /* drop submission reference */
3611 io_put_req(req);
3612
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03003613 if (linked_timeout) {
Jens Axboe76a46e02019-11-10 23:34:16 -07003614 if (!ret)
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03003615 io_queue_linked_timeout(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07003616 else
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03003617 io_put_req(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07003618 }
3619
Jens Axboee65ef562019-03-12 10:16:44 -06003620 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06003621 if (ret) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07003622 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003623 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06003624 io_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06003625 }
Jens Axboe4a0a7a12019-12-09 20:01:01 -07003626done_req:
3627 if (nxt) {
3628 req = nxt;
3629 nxt = NULL;
3630 goto again;
3631 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003632}
3633
Jens Axboe3529d8c2019-12-19 18:24:38 -07003634static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jackie Liu4fe2c962019-09-09 20:50:40 +08003635{
3636 int ret;
3637
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003638 if (unlikely(req->ctx->drain_next)) {
3639 req->flags |= REQ_F_IO_DRAIN;
3640 req->ctx->drain_next = false;
3641 }
3642 req->ctx->drain_next = (req->flags & REQ_F_DRAIN_LINK);
3643
Jens Axboe3529d8c2019-12-19 18:24:38 -07003644 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08003645 if (ret) {
3646 if (ret != -EIOCBQUEUED) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07003647 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003648 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07003649 io_double_put_req(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08003650 }
Jens Axboe0e0702d2019-11-14 21:42:10 -07003651 } else
Jens Axboe3529d8c2019-12-19 18:24:38 -07003652 __io_queue_sqe(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08003653}
3654
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003655static inline void io_queue_link_head(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08003656{
Jens Axboe94ae5e72019-11-14 19:39:52 -07003657 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003658 io_cqring_add_event(req, -ECANCELED);
3659 io_double_put_req(req);
3660 } else
Jens Axboe3529d8c2019-12-19 18:24:38 -07003661 io_queue_sqe(req, NULL);
Jackie Liu4fe2c962019-09-09 20:50:40 +08003662}
3663
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003664#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
3665 IOSQE_IO_HARDLINK)
Jens Axboe9e645e112019-05-10 16:07:28 -06003666
Jens Axboe3529d8c2019-12-19 18:24:38 -07003667static bool io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
3668 struct io_submit_state *state, struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06003669{
Jackie Liua197f662019-11-08 08:09:12 -07003670 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -06003671 int ret;
3672
3673 /* enforce forwards compatibility on users */
Jens Axboe3529d8c2019-12-19 18:24:38 -07003674 if (unlikely(sqe->flags & ~SQE_VALID_FLAGS)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06003675 ret = -EINVAL;
Pavel Begunkov196be952019-11-07 01:41:06 +03003676 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06003677 }
3678
Jens Axboe3529d8c2019-12-19 18:24:38 -07003679 ret = io_req_set_file(state, req, sqe);
Jens Axboe9e645e112019-05-10 16:07:28 -06003680 if (unlikely(ret)) {
3681err_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07003682 io_cqring_add_event(req, ret);
3683 io_double_put_req(req);
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03003684 return false;
Jens Axboe9e645e112019-05-10 16:07:28 -06003685 }
3686
Jens Axboe9e645e112019-05-10 16:07:28 -06003687 /*
3688 * If we already have a head request, queue this one for async
3689 * submittal once the head completes. If we don't have a head but
3690 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
3691 * submitted sync once the chain is complete. If none of those
3692 * conditions are true (normal request), then just queue it.
3693 */
3694 if (*link) {
3695 struct io_kiocb *prev = *link;
3696
Jens Axboe3529d8c2019-12-19 18:24:38 -07003697 if (sqe->flags & IOSQE_IO_DRAIN)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003698 (*link)->flags |= REQ_F_DRAIN_LINK | REQ_F_IO_DRAIN;
3699
Jens Axboe3529d8c2019-12-19 18:24:38 -07003700 if (sqe->flags & IOSQE_IO_HARDLINK)
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003701 req->flags |= REQ_F_HARDLINK;
3702
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003703 if (io_alloc_async_ctx(req)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06003704 ret = -EAGAIN;
3705 goto err_req;
3706 }
3707
Jens Axboe3529d8c2019-12-19 18:24:38 -07003708 ret = io_req_defer_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07003709 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003710 /* fail even hard links since we don't submit */
Jens Axboe2d283902019-12-04 11:08:05 -07003711 prev->flags |= REQ_F_FAIL_LINK;
Jens Axboef67676d2019-12-02 11:03:47 -07003712 goto err_req;
Jens Axboe2d283902019-12-04 11:08:05 -07003713 }
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02003714 trace_io_uring_link(ctx, req, prev);
Pavel Begunkov44932332019-12-05 16:16:35 +03003715 list_add_tail(&req->link_list, &prev->link_list);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003716 } else if (sqe->flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06003717 req->flags |= REQ_F_LINK;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003718 if (sqe->flags & IOSQE_IO_HARDLINK)
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003719 req->flags |= REQ_F_HARDLINK;
Jens Axboe9e645e112019-05-10 16:07:28 -06003720
Jens Axboe9e645e112019-05-10 16:07:28 -06003721 INIT_LIST_HEAD(&req->link_list);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003722 ret = io_req_defer_prep(req, sqe);
3723 if (ret)
3724 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe9e645e112019-05-10 16:07:28 -06003725 *link = req;
3726 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003727 io_queue_sqe(req, sqe);
Jens Axboe9e645e112019-05-10 16:07:28 -06003728 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03003729
3730 return true;
Jens Axboe9e645e112019-05-10 16:07:28 -06003731}
3732
Jens Axboe9a56a232019-01-09 09:06:50 -07003733/*
3734 * Batched submission is done, ensure local IO is flushed out.
3735 */
3736static void io_submit_state_end(struct io_submit_state *state)
3737{
3738 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06003739 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07003740 if (state->free_reqs)
3741 kmem_cache_free_bulk(req_cachep, state->free_reqs,
3742 &state->reqs[state->cur_req]);
Jens Axboe9a56a232019-01-09 09:06:50 -07003743}
3744
3745/*
3746 * Start submission side cache.
3747 */
3748static void io_submit_state_start(struct io_submit_state *state,
Jackie Liu22efde52019-12-02 17:14:52 +08003749 unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07003750{
3751 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07003752 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07003753 state->file = NULL;
3754 state->ios_left = max_ios;
3755}
3756
Jens Axboe2b188cc2019-01-07 10:46:33 -07003757static void io_commit_sqring(struct io_ring_ctx *ctx)
3758{
Hristo Venev75b28af2019-08-26 17:23:46 +00003759 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003760
Hristo Venev75b28af2019-08-26 17:23:46 +00003761 if (ctx->cached_sq_head != READ_ONCE(rings->sq.head)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07003762 /*
3763 * Ensure any loads from the SQEs are done at this point,
3764 * since once we write the new head, the application could
3765 * write new data to them.
3766 */
Hristo Venev75b28af2019-08-26 17:23:46 +00003767 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003768 }
3769}
3770
3771/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07003772 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07003773 * that is mapped by userspace. This means that care needs to be taken to
3774 * ensure that reads are stable, as we cannot rely on userspace always
3775 * being a good citizen. If members of the sqe are validated and then later
3776 * used, it's important that those reads are done through READ_ONCE() to
3777 * prevent a re-load down the line.
3778 */
Jens Axboe3529d8c2019-12-19 18:24:38 -07003779static bool io_get_sqring(struct io_ring_ctx *ctx, struct io_kiocb *req,
3780 const struct io_uring_sqe **sqe_ptr)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003781{
Hristo Venev75b28af2019-08-26 17:23:46 +00003782 struct io_rings *rings = ctx->rings;
3783 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003784 unsigned head;
3785
3786 /*
3787 * The cached sq head (or cq tail) serves two purposes:
3788 *
3789 * 1) allows us to batch the cost of updating the user visible
3790 * head updates.
3791 * 2) allows the kernel side to track the head on its own, even
3792 * though the application is the one updating it.
3793 */
3794 head = ctx->cached_sq_head;
Stefan Bühlere523a292019-04-19 11:57:44 +02003795 /* make sure SQ entry isn't read before tail */
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03003796 if (unlikely(head == smp_load_acquire(&rings->sq.tail)))
Jens Axboe2b188cc2019-01-07 10:46:33 -07003797 return false;
3798
Hristo Venev75b28af2019-08-26 17:23:46 +00003799 head = READ_ONCE(sq_array[head & ctx->sq_mask]);
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03003800 if (likely(head < ctx->sq_entries)) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003801 /*
3802 * All io need record the previous position, if LINK vs DARIN,
3803 * it can be used to mark the position of the first IO in the
3804 * link list.
3805 */
3806 req->sequence = ctx->cached_sq_head;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003807 *sqe_ptr = &ctx->sq_sqes[head];
3808 req->opcode = READ_ONCE((*sqe_ptr)->opcode);
3809 req->user_data = READ_ONCE((*sqe_ptr)->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003810 ctx->cached_sq_head++;
3811 return true;
3812 }
3813
3814 /* drop invalid entries */
3815 ctx->cached_sq_head++;
Jens Axboe498ccd92019-10-25 10:04:25 -06003816 ctx->cached_sq_dropped++;
3817 WRITE_ONCE(rings->sq_dropped, ctx->cached_sq_dropped);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003818 return false;
3819}
3820
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003821static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkovae9428c2019-11-06 00:22:14 +03003822 struct file *ring_file, int ring_fd,
3823 struct mm_struct **mm, bool async)
Jens Axboe6c271ce2019-01-10 11:22:30 -07003824{
3825 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06003826 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06003827 int i, submitted = 0;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03003828 bool mm_fault = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003829
Jens Axboec4a2ed72019-11-21 21:01:26 -07003830 /* if we have a backlog and couldn't flush it all, return BUSY */
3831 if (!list_empty(&ctx->cq_overflow_list) &&
3832 !io_cqring_overflow_flush(ctx, false))
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003833 return -EBUSY;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003834
3835 if (nr > IO_PLUG_THRESHOLD) {
Jackie Liu22efde52019-12-02 17:14:52 +08003836 io_submit_state_start(&state, nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003837 statep = &state;
3838 }
3839
3840 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003841 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03003842 struct io_kiocb *req;
Pavel Begunkov50585b92019-11-07 01:41:07 +03003843 unsigned int sqe_flags;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003844
Pavel Begunkov196be952019-11-07 01:41:06 +03003845 req = io_get_req(ctx, statep);
3846 if (unlikely(!req)) {
3847 if (!submitted)
3848 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003849 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06003850 }
Jens Axboe3529d8c2019-12-19 18:24:38 -07003851 if (!io_get_sqring(ctx, req, &sqe)) {
Pavel Begunkov196be952019-11-07 01:41:06 +03003852 __io_free_req(req);
3853 break;
3854 }
Jens Axboe9e645e112019-05-10 16:07:28 -06003855
Jens Axboed625c6e2019-12-17 19:53:05 -07003856 if (io_req_needs_user(req) && !*mm) {
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03003857 mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm);
3858 if (!mm_fault) {
3859 use_mm(ctx->sqo_mm);
3860 *mm = ctx->sqo_mm;
3861 }
3862 }
3863
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03003864 submitted++;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003865 sqe_flags = sqe->flags;
Pavel Begunkov50585b92019-11-07 01:41:07 +03003866
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003867 req->ring_file = ring_file;
3868 req->ring_fd = ring_fd;
3869 req->has_user = *mm != NULL;
3870 req->in_async = async;
3871 req->needs_fixed_file = async;
Jens Axboed625c6e2019-12-17 19:53:05 -07003872 trace_io_uring_submit_sqe(ctx, req->user_data, true, async);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003873 if (!io_submit_sqe(req, sqe, statep, &link))
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03003874 break;
Pavel Begunkove5eb6362019-11-06 00:22:15 +03003875 /*
3876 * If previous wasn't linked and we have a linked command,
3877 * that's the end of the chain. Submit the previous link.
3878 */
Pavel Begunkovffbb8d62019-12-17 20:57:05 +03003879 if (!(sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK)) && link) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003880 io_queue_link_head(link);
Pavel Begunkove5eb6362019-11-06 00:22:15 +03003881 link = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003882 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07003883 }
3884
Jens Axboe9e645e112019-05-10 16:07:28 -06003885 if (link)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003886 io_queue_link_head(link);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003887 if (statep)
3888 io_submit_state_end(&state);
3889
Pavel Begunkovae9428c2019-11-06 00:22:14 +03003890 /* Commit SQ ring head once we've consumed and submitted all SQEs */
3891 io_commit_sqring(ctx);
3892
Jens Axboe6c271ce2019-01-10 11:22:30 -07003893 return submitted;
3894}
3895
3896static int io_sq_thread(void *data)
3897{
Jens Axboe6c271ce2019-01-10 11:22:30 -07003898 struct io_ring_ctx *ctx = data;
3899 struct mm_struct *cur_mm = NULL;
Jens Axboe181e4482019-11-25 08:52:30 -07003900 const struct cred *old_cred;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003901 mm_segment_t old_fs;
3902 DEFINE_WAIT(wait);
3903 unsigned inflight;
3904 unsigned long timeout;
Jens Axboec1edbf52019-11-10 16:56:04 -07003905 int ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003906
Jens Axboe206aefd2019-11-07 18:27:42 -07003907 complete(&ctx->completions[1]);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08003908
Jens Axboe6c271ce2019-01-10 11:22:30 -07003909 old_fs = get_fs();
3910 set_fs(USER_DS);
Jens Axboe181e4482019-11-25 08:52:30 -07003911 old_cred = override_creds(ctx->creds);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003912
Jens Axboec1edbf52019-11-10 16:56:04 -07003913 ret = timeout = inflight = 0;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003914 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003915 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003916
3917 if (inflight) {
3918 unsigned nr_events = 0;
3919
3920 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe2b2ed972019-10-25 10:06:15 -06003921 /*
3922 * inflight is the count of the maximum possible
3923 * entries we submitted, but it can be smaller
3924 * if we dropped some of them. If we don't have
3925 * poll entries available, then we know that we
3926 * have nothing left to poll for. Reset the
3927 * inflight count to zero in that case.
3928 */
3929 mutex_lock(&ctx->uring_lock);
3930 if (!list_empty(&ctx->poll_list))
3931 __io_iopoll_check(ctx, &nr_events, 0);
3932 else
3933 inflight = 0;
3934 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003935 } else {
3936 /*
3937 * Normal IO, just pretend everything completed.
3938 * We don't have to poll completions for that.
3939 */
3940 nr_events = inflight;
3941 }
3942
3943 inflight -= nr_events;
3944 if (!inflight)
3945 timeout = jiffies + ctx->sq_thread_idle;
3946 }
3947
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003948 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07003949
3950 /*
3951 * If submit got -EBUSY, flag us as needing the application
3952 * to enter the kernel to reap and flush events.
3953 */
3954 if (!to_submit || ret == -EBUSY) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07003955 /*
3956 * We're polling. If we're within the defined idle
3957 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07003958 * to sleep. The exception is if we got EBUSY doing
3959 * more IO, we should wait for the application to
3960 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07003961 */
Jens Axboec1edbf52019-11-10 16:56:04 -07003962 if (inflight ||
3963 (!time_after(jiffies, timeout) && ret != -EBUSY)) {
Jens Axboe9831a902019-09-19 09:48:55 -06003964 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07003965 continue;
3966 }
3967
3968 /*
3969 * Drop cur_mm before scheduling, we can't hold it for
3970 * long periods (or over schedule()). Do this before
3971 * adding ourselves to the waitqueue, as the unuse/drop
3972 * may sleep.
3973 */
3974 if (cur_mm) {
3975 unuse_mm(cur_mm);
3976 mmput(cur_mm);
3977 cur_mm = NULL;
3978 }
3979
3980 prepare_to_wait(&ctx->sqo_wait, &wait,
3981 TASK_INTERRUPTIBLE);
3982
3983 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00003984 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02003985 /* make sure to read SQ tail after writing flags */
3986 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07003987
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003988 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07003989 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003990 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07003991 finish_wait(&ctx->sqo_wait, &wait);
3992 break;
3993 }
3994 if (signal_pending(current))
3995 flush_signals(current);
3996 schedule();
3997 finish_wait(&ctx->sqo_wait, &wait);
3998
Hristo Venev75b28af2019-08-26 17:23:46 +00003999 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004000 continue;
4001 }
4002 finish_wait(&ctx->sqo_wait, &wait);
4003
Hristo Venev75b28af2019-08-26 17:23:46 +00004004 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004005 }
4006
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004007 to_submit = min(to_submit, ctx->sq_entries);
Jens Axboe8a4955f2019-12-09 14:52:35 -07004008 mutex_lock(&ctx->uring_lock);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004009 ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true);
Jens Axboe8a4955f2019-12-09 14:52:35 -07004010 mutex_unlock(&ctx->uring_lock);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004011 if (ret > 0)
4012 inflight += ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004013 }
4014
4015 set_fs(old_fs);
4016 if (cur_mm) {
4017 unuse_mm(cur_mm);
4018 mmput(cur_mm);
4019 }
Jens Axboe181e4482019-11-25 08:52:30 -07004020 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06004021
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02004022 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06004023
Jens Axboe6c271ce2019-01-10 11:22:30 -07004024 return 0;
4025}
4026
Jens Axboebda52162019-09-24 13:47:15 -06004027struct io_wait_queue {
4028 struct wait_queue_entry wq;
4029 struct io_ring_ctx *ctx;
4030 unsigned to_wait;
4031 unsigned nr_timeouts;
4032};
4033
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004034static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06004035{
4036 struct io_ring_ctx *ctx = iowq->ctx;
4037
4038 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08004039 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06004040 * started waiting. For timeouts, we always want to return to userspace,
4041 * regardless of event count.
4042 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004043 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06004044 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
4045}
4046
4047static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
4048 int wake_flags, void *key)
4049{
4050 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
4051 wq);
4052
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004053 /* use noflush == true, as we can't safely rely on locking context */
4054 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06004055 return -1;
4056
4057 return autoremove_wake_function(curr, mode, wake_flags, key);
4058}
4059
Jens Axboe2b188cc2019-01-07 10:46:33 -07004060/*
4061 * Wait until events become available, if we don't already have some. The
4062 * application must reap them itself, as they reside on the shared cq ring.
4063 */
4064static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
4065 const sigset_t __user *sig, size_t sigsz)
4066{
Jens Axboebda52162019-09-24 13:47:15 -06004067 struct io_wait_queue iowq = {
4068 .wq = {
4069 .private = current,
4070 .func = io_wake_function,
4071 .entry = LIST_HEAD_INIT(iowq.wq.entry),
4072 },
4073 .ctx = ctx,
4074 .to_wait = min_events,
4075 };
Hristo Venev75b28af2019-08-26 17:23:46 +00004076 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08004077 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004078
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004079 if (io_cqring_events(ctx, false) >= min_events)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004080 return 0;
4081
4082 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01004083#ifdef CONFIG_COMPAT
4084 if (in_compat_syscall())
4085 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07004086 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01004087 else
4088#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07004089 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01004090
Jens Axboe2b188cc2019-01-07 10:46:33 -07004091 if (ret)
4092 return ret;
4093 }
4094
Jens Axboebda52162019-09-24 13:47:15 -06004095 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004096 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06004097 do {
4098 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
4099 TASK_INTERRUPTIBLE);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004100 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06004101 break;
4102 schedule();
4103 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08004104 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06004105 break;
4106 }
4107 } while (1);
4108 finish_wait(&ctx->wait, &iowq.wq);
4109
Jackie Liue9ffa5c2019-10-29 11:16:42 +08004110 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004111
Hristo Venev75b28af2019-08-26 17:23:46 +00004112 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004113}
4114
Jens Axboe6b063142019-01-10 22:13:58 -07004115static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
4116{
4117#if defined(CONFIG_UNIX)
4118 if (ctx->ring_sock) {
4119 struct sock *sock = ctx->ring_sock->sk;
4120 struct sk_buff *skb;
4121
4122 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
4123 kfree_skb(skb);
4124 }
4125#else
4126 int i;
4127
Jens Axboe65e19f52019-10-26 07:20:21 -06004128 for (i = 0; i < ctx->nr_user_files; i++) {
4129 struct file *file;
4130
4131 file = io_file_from_index(ctx, i);
4132 if (file)
4133 fput(file);
4134 }
Jens Axboe6b063142019-01-10 22:13:58 -07004135#endif
4136}
4137
4138static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
4139{
Jens Axboe65e19f52019-10-26 07:20:21 -06004140 unsigned nr_tables, i;
4141
4142 if (!ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07004143 return -ENXIO;
4144
4145 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06004146 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
4147 for (i = 0; i < nr_tables; i++)
4148 kfree(ctx->file_table[i].files);
4149 kfree(ctx->file_table);
4150 ctx->file_table = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07004151 ctx->nr_user_files = 0;
4152 return 0;
4153}
4154
Jens Axboe6c271ce2019-01-10 11:22:30 -07004155static void io_sq_thread_stop(struct io_ring_ctx *ctx)
4156{
4157 if (ctx->sqo_thread) {
Jens Axboe206aefd2019-11-07 18:27:42 -07004158 wait_for_completion(&ctx->completions[1]);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02004159 /*
4160 * The park is a bit of a work-around, without it we get
4161 * warning spews on shutdown with SQPOLL set and affinity
4162 * set to a single CPU.
4163 */
Jens Axboe06058632019-04-13 09:26:03 -06004164 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004165 kthread_stop(ctx->sqo_thread);
4166 ctx->sqo_thread = NULL;
4167 }
4168}
4169
Jens Axboe6b063142019-01-10 22:13:58 -07004170static void io_finish_async(struct io_ring_ctx *ctx)
4171{
Jens Axboe6c271ce2019-01-10 11:22:30 -07004172 io_sq_thread_stop(ctx);
4173
Jens Axboe561fb042019-10-24 07:25:42 -06004174 if (ctx->io_wq) {
4175 io_wq_destroy(ctx->io_wq);
4176 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07004177 }
4178}
4179
4180#if defined(CONFIG_UNIX)
4181static void io_destruct_skb(struct sk_buff *skb)
4182{
4183 struct io_ring_ctx *ctx = skb->sk->sk_user_data;
4184
Jens Axboe561fb042019-10-24 07:25:42 -06004185 if (ctx->io_wq)
4186 io_wq_flush(ctx->io_wq);
Jens Axboe8a997342019-10-09 14:40:13 -06004187
Jens Axboe6b063142019-01-10 22:13:58 -07004188 unix_destruct_scm(skb);
4189}
4190
4191/*
4192 * Ensure the UNIX gc is aware of our file set, so we are certain that
4193 * the io_uring can be safely unregistered on process exit, even if we have
4194 * loops in the file referencing.
4195 */
4196static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
4197{
4198 struct sock *sk = ctx->ring_sock->sk;
4199 struct scm_fp_list *fpl;
4200 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06004201 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07004202
4203 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
4204 unsigned long inflight = ctx->user->unix_inflight + nr;
4205
4206 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
4207 return -EMFILE;
4208 }
4209
4210 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
4211 if (!fpl)
4212 return -ENOMEM;
4213
4214 skb = alloc_skb(0, GFP_KERNEL);
4215 if (!skb) {
4216 kfree(fpl);
4217 return -ENOMEM;
4218 }
4219
4220 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07004221
Jens Axboe08a45172019-10-03 08:11:03 -06004222 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07004223 fpl->user = get_uid(ctx->user);
4224 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06004225 struct file *file = io_file_from_index(ctx, i + offset);
4226
4227 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06004228 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06004229 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06004230 unix_inflight(fpl->user, fpl->fp[nr_files]);
4231 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07004232 }
4233
Jens Axboe08a45172019-10-03 08:11:03 -06004234 if (nr_files) {
4235 fpl->max = SCM_MAX_FD;
4236 fpl->count = nr_files;
4237 UNIXCB(skb).fp = fpl;
4238 skb->destructor = io_destruct_skb;
4239 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
4240 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07004241
Jens Axboe08a45172019-10-03 08:11:03 -06004242 for (i = 0; i < nr_files; i++)
4243 fput(fpl->fp[i]);
4244 } else {
4245 kfree_skb(skb);
4246 kfree(fpl);
4247 }
Jens Axboe6b063142019-01-10 22:13:58 -07004248
4249 return 0;
4250}
4251
4252/*
4253 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
4254 * causes regular reference counting to break down. We rely on the UNIX
4255 * garbage collection to take care of this problem for us.
4256 */
4257static int io_sqe_files_scm(struct io_ring_ctx *ctx)
4258{
4259 unsigned left, total;
4260 int ret = 0;
4261
4262 total = 0;
4263 left = ctx->nr_user_files;
4264 while (left) {
4265 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07004266
4267 ret = __io_sqe_files_scm(ctx, this_files, total);
4268 if (ret)
4269 break;
4270 left -= this_files;
4271 total += this_files;
4272 }
4273
4274 if (!ret)
4275 return 0;
4276
4277 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06004278 struct file *file = io_file_from_index(ctx, total);
4279
4280 if (file)
4281 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07004282 total++;
4283 }
4284
4285 return ret;
4286}
4287#else
4288static int io_sqe_files_scm(struct io_ring_ctx *ctx)
4289{
4290 return 0;
4291}
4292#endif
4293
Jens Axboe65e19f52019-10-26 07:20:21 -06004294static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
4295 unsigned nr_files)
4296{
4297 int i;
4298
4299 for (i = 0; i < nr_tables; i++) {
4300 struct fixed_file_table *table = &ctx->file_table[i];
4301 unsigned this_files;
4302
4303 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
4304 table->files = kcalloc(this_files, sizeof(struct file *),
4305 GFP_KERNEL);
4306 if (!table->files)
4307 break;
4308 nr_files -= this_files;
4309 }
4310
4311 if (i == nr_tables)
4312 return 0;
4313
4314 for (i = 0; i < nr_tables; i++) {
4315 struct fixed_file_table *table = &ctx->file_table[i];
4316 kfree(table->files);
4317 }
4318 return 1;
4319}
4320
Jens Axboe6b063142019-01-10 22:13:58 -07004321static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
4322 unsigned nr_args)
4323{
4324 __s32 __user *fds = (__s32 __user *) arg;
Jens Axboe65e19f52019-10-26 07:20:21 -06004325 unsigned nr_tables;
Jens Axboe6b063142019-01-10 22:13:58 -07004326 int fd, ret = 0;
4327 unsigned i;
4328
Jens Axboe65e19f52019-10-26 07:20:21 -06004329 if (ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07004330 return -EBUSY;
4331 if (!nr_args)
4332 return -EINVAL;
4333 if (nr_args > IORING_MAX_FIXED_FILES)
4334 return -EMFILE;
4335
Jens Axboe65e19f52019-10-26 07:20:21 -06004336 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
4337 ctx->file_table = kcalloc(nr_tables, sizeof(struct fixed_file_table),
4338 GFP_KERNEL);
4339 if (!ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07004340 return -ENOMEM;
4341
Jens Axboe65e19f52019-10-26 07:20:21 -06004342 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
4343 kfree(ctx->file_table);
Jens Axboe46568e92019-11-10 08:40:53 -07004344 ctx->file_table = NULL;
Jens Axboe65e19f52019-10-26 07:20:21 -06004345 return -ENOMEM;
4346 }
4347
Jens Axboe08a45172019-10-03 08:11:03 -06004348 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06004349 struct fixed_file_table *table;
4350 unsigned index;
4351
Jens Axboe6b063142019-01-10 22:13:58 -07004352 ret = -EFAULT;
4353 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
4354 break;
Jens Axboe08a45172019-10-03 08:11:03 -06004355 /* allow sparse sets */
4356 if (fd == -1) {
4357 ret = 0;
4358 continue;
4359 }
Jens Axboe6b063142019-01-10 22:13:58 -07004360
Jens Axboe65e19f52019-10-26 07:20:21 -06004361 table = &ctx->file_table[i >> IORING_FILE_TABLE_SHIFT];
4362 index = i & IORING_FILE_TABLE_MASK;
4363 table->files[index] = fget(fd);
Jens Axboe6b063142019-01-10 22:13:58 -07004364
4365 ret = -EBADF;
Jens Axboe65e19f52019-10-26 07:20:21 -06004366 if (!table->files[index])
Jens Axboe6b063142019-01-10 22:13:58 -07004367 break;
4368 /*
4369 * Don't allow io_uring instances to be registered. If UNIX
4370 * isn't enabled, then this causes a reference cycle and this
4371 * instance can never get freed. If UNIX is enabled we'll
4372 * handle it just fine, but there's still no point in allowing
4373 * a ring fd as it doesn't support regular read/write anyway.
4374 */
Jens Axboe65e19f52019-10-26 07:20:21 -06004375 if (table->files[index]->f_op == &io_uring_fops) {
4376 fput(table->files[index]);
Jens Axboe6b063142019-01-10 22:13:58 -07004377 break;
4378 }
Jens Axboe6b063142019-01-10 22:13:58 -07004379 ret = 0;
4380 }
4381
4382 if (ret) {
Jens Axboe65e19f52019-10-26 07:20:21 -06004383 for (i = 0; i < ctx->nr_user_files; i++) {
4384 struct file *file;
Jens Axboe6b063142019-01-10 22:13:58 -07004385
Jens Axboe65e19f52019-10-26 07:20:21 -06004386 file = io_file_from_index(ctx, i);
4387 if (file)
4388 fput(file);
4389 }
4390 for (i = 0; i < nr_tables; i++)
4391 kfree(ctx->file_table[i].files);
4392
4393 kfree(ctx->file_table);
4394 ctx->file_table = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07004395 ctx->nr_user_files = 0;
4396 return ret;
4397 }
4398
4399 ret = io_sqe_files_scm(ctx);
4400 if (ret)
4401 io_sqe_files_unregister(ctx);
4402
4403 return ret;
4404}
4405
Jens Axboec3a31e62019-10-03 13:59:56 -06004406static void io_sqe_file_unregister(struct io_ring_ctx *ctx, int index)
4407{
4408#if defined(CONFIG_UNIX)
Jens Axboe65e19f52019-10-26 07:20:21 -06004409 struct file *file = io_file_from_index(ctx, index);
Jens Axboec3a31e62019-10-03 13:59:56 -06004410 struct sock *sock = ctx->ring_sock->sk;
4411 struct sk_buff_head list, *head = &sock->sk_receive_queue;
4412 struct sk_buff *skb;
4413 int i;
4414
4415 __skb_queue_head_init(&list);
4416
4417 /*
4418 * Find the skb that holds this file in its SCM_RIGHTS. When found,
4419 * remove this entry and rearrange the file array.
4420 */
4421 skb = skb_dequeue(head);
4422 while (skb) {
4423 struct scm_fp_list *fp;
4424
4425 fp = UNIXCB(skb).fp;
4426 for (i = 0; i < fp->count; i++) {
4427 int left;
4428
4429 if (fp->fp[i] != file)
4430 continue;
4431
4432 unix_notinflight(fp->user, fp->fp[i]);
4433 left = fp->count - 1 - i;
4434 if (left) {
4435 memmove(&fp->fp[i], &fp->fp[i + 1],
4436 left * sizeof(struct file *));
4437 }
4438 fp->count--;
4439 if (!fp->count) {
4440 kfree_skb(skb);
4441 skb = NULL;
4442 } else {
4443 __skb_queue_tail(&list, skb);
4444 }
4445 fput(file);
4446 file = NULL;
4447 break;
4448 }
4449
4450 if (!file)
4451 break;
4452
4453 __skb_queue_tail(&list, skb);
4454
4455 skb = skb_dequeue(head);
4456 }
4457
4458 if (skb_peek(&list)) {
4459 spin_lock_irq(&head->lock);
4460 while ((skb = __skb_dequeue(&list)) != NULL)
4461 __skb_queue_tail(head, skb);
4462 spin_unlock_irq(&head->lock);
4463 }
4464#else
Jens Axboe65e19f52019-10-26 07:20:21 -06004465 fput(io_file_from_index(ctx, index));
Jens Axboec3a31e62019-10-03 13:59:56 -06004466#endif
4467}
4468
4469static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
4470 int index)
4471{
4472#if defined(CONFIG_UNIX)
4473 struct sock *sock = ctx->ring_sock->sk;
4474 struct sk_buff_head *head = &sock->sk_receive_queue;
4475 struct sk_buff *skb;
4476
4477 /*
4478 * See if we can merge this file into an existing skb SCM_RIGHTS
4479 * file set. If there's no room, fall back to allocating a new skb
4480 * and filling it in.
4481 */
4482 spin_lock_irq(&head->lock);
4483 skb = skb_peek(head);
4484 if (skb) {
4485 struct scm_fp_list *fpl = UNIXCB(skb).fp;
4486
4487 if (fpl->count < SCM_MAX_FD) {
4488 __skb_unlink(skb, head);
4489 spin_unlock_irq(&head->lock);
4490 fpl->fp[fpl->count] = get_file(file);
4491 unix_inflight(fpl->user, fpl->fp[fpl->count]);
4492 fpl->count++;
4493 spin_lock_irq(&head->lock);
4494 __skb_queue_head(head, skb);
4495 } else {
4496 skb = NULL;
4497 }
4498 }
4499 spin_unlock_irq(&head->lock);
4500
4501 if (skb) {
4502 fput(file);
4503 return 0;
4504 }
4505
4506 return __io_sqe_files_scm(ctx, 1, index);
4507#else
4508 return 0;
4509#endif
4510}
4511
4512static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
4513 unsigned nr_args)
4514{
4515 struct io_uring_files_update up;
4516 __s32 __user *fds;
4517 int fd, i, err;
4518 __u32 done;
4519
Jens Axboe65e19f52019-10-26 07:20:21 -06004520 if (!ctx->file_table)
Jens Axboec3a31e62019-10-03 13:59:56 -06004521 return -ENXIO;
4522 if (!nr_args)
4523 return -EINVAL;
4524 if (copy_from_user(&up, arg, sizeof(up)))
4525 return -EFAULT;
Eugene Syromiatnikov1292e972020-01-15 17:35:38 +01004526 if (up.resv)
4527 return -EINVAL;
Jens Axboec3a31e62019-10-03 13:59:56 -06004528 if (check_add_overflow(up.offset, nr_args, &done))
4529 return -EOVERFLOW;
4530 if (done > ctx->nr_user_files)
4531 return -EINVAL;
4532
4533 done = 0;
Eugene Syromiatnikov1292e972020-01-15 17:35:38 +01004534 fds = u64_to_user_ptr(up.fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06004535 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06004536 struct fixed_file_table *table;
4537 unsigned index;
4538
Jens Axboec3a31e62019-10-03 13:59:56 -06004539 err = 0;
4540 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
4541 err = -EFAULT;
4542 break;
4543 }
4544 i = array_index_nospec(up.offset, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06004545 table = &ctx->file_table[i >> IORING_FILE_TABLE_SHIFT];
4546 index = i & IORING_FILE_TABLE_MASK;
4547 if (table->files[index]) {
Jens Axboec3a31e62019-10-03 13:59:56 -06004548 io_sqe_file_unregister(ctx, i);
Jens Axboe65e19f52019-10-26 07:20:21 -06004549 table->files[index] = NULL;
Jens Axboec3a31e62019-10-03 13:59:56 -06004550 }
4551 if (fd != -1) {
4552 struct file *file;
4553
4554 file = fget(fd);
4555 if (!file) {
4556 err = -EBADF;
4557 break;
4558 }
4559 /*
4560 * Don't allow io_uring instances to be registered. If
4561 * UNIX isn't enabled, then this causes a reference
4562 * cycle and this instance can never get freed. If UNIX
4563 * is enabled we'll handle it just fine, but there's
4564 * still no point in allowing a ring fd as it doesn't
4565 * support regular read/write anyway.
4566 */
4567 if (file->f_op == &io_uring_fops) {
4568 fput(file);
4569 err = -EBADF;
4570 break;
4571 }
Jens Axboe65e19f52019-10-26 07:20:21 -06004572 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06004573 err = io_sqe_file_register(ctx, file, i);
4574 if (err)
4575 break;
4576 }
4577 nr_args--;
4578 done++;
4579 up.offset++;
4580 }
4581
4582 return done ? done : err;
4583}
4584
Jens Axboe7d723062019-11-12 22:31:31 -07004585static void io_put_work(struct io_wq_work *work)
4586{
4587 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
4588
4589 io_put_req(req);
4590}
4591
4592static void io_get_work(struct io_wq_work *work)
4593{
4594 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
4595
4596 refcount_inc(&req->refs);
4597}
4598
Jens Axboe6c271ce2019-01-10 11:22:30 -07004599static int io_sq_offload_start(struct io_ring_ctx *ctx,
4600 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004601{
Jens Axboe576a3472019-11-25 08:49:20 -07004602 struct io_wq_data data;
Jens Axboe561fb042019-10-24 07:25:42 -06004603 unsigned concurrency;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004604 int ret;
4605
Jens Axboe6c271ce2019-01-10 11:22:30 -07004606 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004607 mmgrab(current->mm);
4608 ctx->sqo_mm = current->mm;
4609
Jens Axboe6c271ce2019-01-10 11:22:30 -07004610 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06004611 ret = -EPERM;
4612 if (!capable(CAP_SYS_ADMIN))
4613 goto err;
4614
Jens Axboe917257d2019-04-13 09:28:55 -06004615 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
4616 if (!ctx->sq_thread_idle)
4617 ctx->sq_thread_idle = HZ;
4618
Jens Axboe6c271ce2019-01-10 11:22:30 -07004619 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06004620 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004621
Jens Axboe917257d2019-04-13 09:28:55 -06004622 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06004623 if (cpu >= nr_cpu_ids)
4624 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08004625 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06004626 goto err;
4627
Jens Axboe6c271ce2019-01-10 11:22:30 -07004628 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
4629 ctx, cpu,
4630 "io_uring-sq");
4631 } else {
4632 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
4633 "io_uring-sq");
4634 }
4635 if (IS_ERR(ctx->sqo_thread)) {
4636 ret = PTR_ERR(ctx->sqo_thread);
4637 ctx->sqo_thread = NULL;
4638 goto err;
4639 }
4640 wake_up_process(ctx->sqo_thread);
4641 } else if (p->flags & IORING_SETUP_SQ_AFF) {
4642 /* Can't have SQ_AFF without SQPOLL */
4643 ret = -EINVAL;
4644 goto err;
4645 }
4646
Jens Axboe576a3472019-11-25 08:49:20 -07004647 data.mm = ctx->sqo_mm;
4648 data.user = ctx->user;
Jens Axboe181e4482019-11-25 08:52:30 -07004649 data.creds = ctx->creds;
Jens Axboe576a3472019-11-25 08:49:20 -07004650 data.get_work = io_get_work;
4651 data.put_work = io_put_work;
4652
Jens Axboe561fb042019-10-24 07:25:42 -06004653 /* Do QD, or 4 * CPUS, whatever is smallest */
4654 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
Jens Axboe576a3472019-11-25 08:49:20 -07004655 ctx->io_wq = io_wq_create(concurrency, &data);
Jens Axboe975c99a52019-10-30 08:42:56 -06004656 if (IS_ERR(ctx->io_wq)) {
4657 ret = PTR_ERR(ctx->io_wq);
4658 ctx->io_wq = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004659 goto err;
4660 }
4661
4662 return 0;
4663err:
Jens Axboe54a91f32019-09-10 09:15:04 -06004664 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004665 mmdrop(ctx->sqo_mm);
4666 ctx->sqo_mm = NULL;
4667 return ret;
4668}
4669
4670static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
4671{
4672 atomic_long_sub(nr_pages, &user->locked_vm);
4673}
4674
4675static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
4676{
4677 unsigned long page_limit, cur_pages, new_pages;
4678
4679 /* Don't allow more pages than we can safely lock */
4680 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
4681
4682 do {
4683 cur_pages = atomic_long_read(&user->locked_vm);
4684 new_pages = cur_pages + nr_pages;
4685 if (new_pages > page_limit)
4686 return -ENOMEM;
4687 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
4688 new_pages) != cur_pages);
4689
4690 return 0;
4691}
4692
4693static void io_mem_free(void *ptr)
4694{
Mark Rutland52e04ef2019-04-30 17:30:21 +01004695 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004696
Mark Rutland52e04ef2019-04-30 17:30:21 +01004697 if (!ptr)
4698 return;
4699
4700 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004701 if (put_page_testzero(page))
4702 free_compound_page(page);
4703}
4704
4705static void *io_mem_alloc(size_t size)
4706{
4707 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
4708 __GFP_NORETRY;
4709
4710 return (void *) __get_free_pages(gfp_flags, get_order(size));
4711}
4712
Hristo Venev75b28af2019-08-26 17:23:46 +00004713static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
4714 size_t *sq_offset)
4715{
4716 struct io_rings *rings;
4717 size_t off, sq_array_size;
4718
4719 off = struct_size(rings, cqes, cq_entries);
4720 if (off == SIZE_MAX)
4721 return SIZE_MAX;
4722
4723#ifdef CONFIG_SMP
4724 off = ALIGN(off, SMP_CACHE_BYTES);
4725 if (off == 0)
4726 return SIZE_MAX;
4727#endif
4728
4729 sq_array_size = array_size(sizeof(u32), sq_entries);
4730 if (sq_array_size == SIZE_MAX)
4731 return SIZE_MAX;
4732
4733 if (check_add_overflow(off, sq_array_size, &off))
4734 return SIZE_MAX;
4735
4736 if (sq_offset)
4737 *sq_offset = off;
4738
4739 return off;
4740}
4741
Jens Axboe2b188cc2019-01-07 10:46:33 -07004742static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
4743{
Hristo Venev75b28af2019-08-26 17:23:46 +00004744 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004745
Hristo Venev75b28af2019-08-26 17:23:46 +00004746 pages = (size_t)1 << get_order(
4747 rings_size(sq_entries, cq_entries, NULL));
4748 pages += (size_t)1 << get_order(
4749 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07004750
Hristo Venev75b28af2019-08-26 17:23:46 +00004751 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004752}
4753
Jens Axboeedafcce2019-01-09 09:16:05 -07004754static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
4755{
4756 int i, j;
4757
4758 if (!ctx->user_bufs)
4759 return -ENXIO;
4760
4761 for (i = 0; i < ctx->nr_user_bufs; i++) {
4762 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
4763
4764 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbard27c4d3a2019-08-04 19:32:06 -07004765 put_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07004766
4767 if (ctx->account_mem)
4768 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004769 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07004770 imu->nr_bvecs = 0;
4771 }
4772
4773 kfree(ctx->user_bufs);
4774 ctx->user_bufs = NULL;
4775 ctx->nr_user_bufs = 0;
4776 return 0;
4777}
4778
4779static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
4780 void __user *arg, unsigned index)
4781{
4782 struct iovec __user *src;
4783
4784#ifdef CONFIG_COMPAT
4785 if (ctx->compat) {
4786 struct compat_iovec __user *ciovs;
4787 struct compat_iovec ciov;
4788
4789 ciovs = (struct compat_iovec __user *) arg;
4790 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
4791 return -EFAULT;
4792
Jens Axboed55e5f52019-12-11 16:12:15 -07004793 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07004794 dst->iov_len = ciov.iov_len;
4795 return 0;
4796 }
4797#endif
4798 src = (struct iovec __user *) arg;
4799 if (copy_from_user(dst, &src[index], sizeof(*dst)))
4800 return -EFAULT;
4801 return 0;
4802}
4803
4804static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
4805 unsigned nr_args)
4806{
4807 struct vm_area_struct **vmas = NULL;
4808 struct page **pages = NULL;
4809 int i, j, got_pages = 0;
4810 int ret = -EINVAL;
4811
4812 if (ctx->user_bufs)
4813 return -EBUSY;
4814 if (!nr_args || nr_args > UIO_MAXIOV)
4815 return -EINVAL;
4816
4817 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
4818 GFP_KERNEL);
4819 if (!ctx->user_bufs)
4820 return -ENOMEM;
4821
4822 for (i = 0; i < nr_args; i++) {
4823 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
4824 unsigned long off, start, end, ubuf;
4825 int pret, nr_pages;
4826 struct iovec iov;
4827 size_t size;
4828
4829 ret = io_copy_iov(ctx, &iov, arg, i);
4830 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03004831 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07004832
4833 /*
4834 * Don't impose further limits on the size and buffer
4835 * constraints here, we'll -EINVAL later when IO is
4836 * submitted if they are wrong.
4837 */
4838 ret = -EFAULT;
4839 if (!iov.iov_base || !iov.iov_len)
4840 goto err;
4841
4842 /* arbitrary limit, but we need something */
4843 if (iov.iov_len > SZ_1G)
4844 goto err;
4845
4846 ubuf = (unsigned long) iov.iov_base;
4847 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
4848 start = ubuf >> PAGE_SHIFT;
4849 nr_pages = end - start;
4850
4851 if (ctx->account_mem) {
4852 ret = io_account_mem(ctx->user, nr_pages);
4853 if (ret)
4854 goto err;
4855 }
4856
4857 ret = 0;
4858 if (!pages || nr_pages > got_pages) {
4859 kfree(vmas);
4860 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004861 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07004862 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004863 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07004864 sizeof(struct vm_area_struct *),
4865 GFP_KERNEL);
4866 if (!pages || !vmas) {
4867 ret = -ENOMEM;
4868 if (ctx->account_mem)
4869 io_unaccount_mem(ctx->user, nr_pages);
4870 goto err;
4871 }
4872 got_pages = nr_pages;
4873 }
4874
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004875 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07004876 GFP_KERNEL);
4877 ret = -ENOMEM;
4878 if (!imu->bvec) {
4879 if (ctx->account_mem)
4880 io_unaccount_mem(ctx->user, nr_pages);
4881 goto err;
4882 }
4883
4884 ret = 0;
4885 down_read(&current->mm->mmap_sem);
Ira Weiny932f4a62019-05-13 17:17:03 -07004886 pret = get_user_pages(ubuf, nr_pages,
4887 FOLL_WRITE | FOLL_LONGTERM,
4888 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07004889 if (pret == nr_pages) {
4890 /* don't support file backed memory */
4891 for (j = 0; j < nr_pages; j++) {
4892 struct vm_area_struct *vma = vmas[j];
4893
4894 if (vma->vm_file &&
4895 !is_file_hugepages(vma->vm_file)) {
4896 ret = -EOPNOTSUPP;
4897 break;
4898 }
4899 }
4900 } else {
4901 ret = pret < 0 ? pret : -EFAULT;
4902 }
4903 up_read(&current->mm->mmap_sem);
4904 if (ret) {
4905 /*
4906 * if we did partial map, or found file backed vmas,
4907 * release any pages we did get
4908 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07004909 if (pret > 0)
4910 put_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07004911 if (ctx->account_mem)
4912 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004913 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07004914 goto err;
4915 }
4916
4917 off = ubuf & ~PAGE_MASK;
4918 size = iov.iov_len;
4919 for (j = 0; j < nr_pages; j++) {
4920 size_t vec_len;
4921
4922 vec_len = min_t(size_t, size, PAGE_SIZE - off);
4923 imu->bvec[j].bv_page = pages[j];
4924 imu->bvec[j].bv_len = vec_len;
4925 imu->bvec[j].bv_offset = off;
4926 off = 0;
4927 size -= vec_len;
4928 }
4929 /* store original address for later verification */
4930 imu->ubuf = ubuf;
4931 imu->len = iov.iov_len;
4932 imu->nr_bvecs = nr_pages;
4933
4934 ctx->nr_user_bufs++;
4935 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004936 kvfree(pages);
4937 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07004938 return 0;
4939err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004940 kvfree(pages);
4941 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07004942 io_sqe_buffer_unregister(ctx);
4943 return ret;
4944}
4945
Jens Axboe9b402842019-04-11 11:45:41 -06004946static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
4947{
4948 __s32 __user *fds = arg;
4949 int fd;
4950
4951 if (ctx->cq_ev_fd)
4952 return -EBUSY;
4953
4954 if (copy_from_user(&fd, fds, sizeof(*fds)))
4955 return -EFAULT;
4956
4957 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
4958 if (IS_ERR(ctx->cq_ev_fd)) {
4959 int ret = PTR_ERR(ctx->cq_ev_fd);
4960 ctx->cq_ev_fd = NULL;
4961 return ret;
4962 }
4963
4964 return 0;
4965}
4966
4967static int io_eventfd_unregister(struct io_ring_ctx *ctx)
4968{
4969 if (ctx->cq_ev_fd) {
4970 eventfd_ctx_put(ctx->cq_ev_fd);
4971 ctx->cq_ev_fd = NULL;
4972 return 0;
4973 }
4974
4975 return -ENXIO;
4976}
4977
Jens Axboe2b188cc2019-01-07 10:46:33 -07004978static void io_ring_ctx_free(struct io_ring_ctx *ctx)
4979{
Jens Axboe6b063142019-01-10 22:13:58 -07004980 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004981 if (ctx->sqo_mm)
4982 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07004983
4984 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07004985 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07004986 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06004987 io_eventfd_unregister(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07004988
Jens Axboe2b188cc2019-01-07 10:46:33 -07004989#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07004990 if (ctx->ring_sock) {
4991 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004992 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07004993 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004994#endif
4995
Hristo Venev75b28af2019-08-26 17:23:46 +00004996 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004997 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004998
4999 percpu_ref_exit(&ctx->refs);
5000 if (ctx->account_mem)
5001 io_unaccount_mem(ctx->user,
5002 ring_pages(ctx->sq_entries, ctx->cq_entries));
5003 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07005004 put_cred(ctx->creds);
Jens Axboe206aefd2019-11-07 18:27:42 -07005005 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -07005006 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07005007 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005008 kfree(ctx);
5009}
5010
5011static __poll_t io_uring_poll(struct file *file, poll_table *wait)
5012{
5013 struct io_ring_ctx *ctx = file->private_data;
5014 __poll_t mask = 0;
5015
5016 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02005017 /*
5018 * synchronizes with barrier from wq_has_sleeper call in
5019 * io_commit_cqring
5020 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07005021 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00005022 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
5023 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005024 mask |= EPOLLOUT | EPOLLWRNORM;
yangerkundaa5de52019-09-24 20:53:34 +08005025 if (READ_ONCE(ctx->rings->cq.head) != ctx->cached_cq_tail)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005026 mask |= EPOLLIN | EPOLLRDNORM;
5027
5028 return mask;
5029}
5030
5031static int io_uring_fasync(int fd, struct file *file, int on)
5032{
5033 struct io_ring_ctx *ctx = file->private_data;
5034
5035 return fasync_helper(fd, file, on, &ctx->cq_fasync);
5036}
5037
5038static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
5039{
5040 mutex_lock(&ctx->uring_lock);
5041 percpu_ref_kill(&ctx->refs);
5042 mutex_unlock(&ctx->uring_lock);
5043
Jens Axboe5262f562019-09-17 12:26:57 -06005044 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07005045 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06005046
5047 if (ctx->io_wq)
5048 io_wq_cancel_all(ctx->io_wq);
5049
Jens Axboedef596e2019-01-09 08:59:42 -07005050 io_iopoll_reap_events(ctx);
Jens Axboe15dff282019-11-13 09:09:23 -07005051 /* if we failed setting up the ctx, we might not have any rings */
5052 if (ctx->rings)
5053 io_cqring_overflow_flush(ctx, true);
Jens Axboe206aefd2019-11-07 18:27:42 -07005054 wait_for_completion(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005055 io_ring_ctx_free(ctx);
5056}
5057
5058static int io_uring_release(struct inode *inode, struct file *file)
5059{
5060 struct io_ring_ctx *ctx = file->private_data;
5061
5062 file->private_data = NULL;
5063 io_ring_ctx_wait_and_kill(ctx);
5064 return 0;
5065}
5066
Jens Axboefcb323c2019-10-24 12:39:47 -06005067static void io_uring_cancel_files(struct io_ring_ctx *ctx,
5068 struct files_struct *files)
5069{
5070 struct io_kiocb *req;
5071 DEFINE_WAIT(wait);
5072
5073 while (!list_empty_careful(&ctx->inflight_list)) {
Jens Axboe768134d2019-11-10 20:30:53 -07005074 struct io_kiocb *cancel_req = NULL;
Jens Axboefcb323c2019-10-24 12:39:47 -06005075
5076 spin_lock_irq(&ctx->inflight_lock);
5077 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07005078 if (req->work.files != files)
5079 continue;
5080 /* req is being completed, ignore */
5081 if (!refcount_inc_not_zero(&req->refs))
5082 continue;
5083 cancel_req = req;
5084 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06005085 }
Jens Axboe768134d2019-11-10 20:30:53 -07005086 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06005087 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07005088 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06005089 spin_unlock_irq(&ctx->inflight_lock);
5090
Jens Axboe768134d2019-11-10 20:30:53 -07005091 /* We need to keep going until we don't find a matching req */
5092 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06005093 break;
Bob Liu2f6d9b92019-11-13 18:06:24 +08005094
5095 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
5096 io_put_req(cancel_req);
Jens Axboefcb323c2019-10-24 12:39:47 -06005097 schedule();
5098 }
Jens Axboe768134d2019-11-10 20:30:53 -07005099 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06005100}
5101
5102static int io_uring_flush(struct file *file, void *data)
5103{
5104 struct io_ring_ctx *ctx = file->private_data;
5105
5106 io_uring_cancel_files(ctx, data);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005107 if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) {
5108 io_cqring_overflow_flush(ctx, true);
Jens Axboefcb323c2019-10-24 12:39:47 -06005109 io_wq_cancel_all(ctx->io_wq);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005110 }
Jens Axboefcb323c2019-10-24 12:39:47 -06005111 return 0;
5112}
5113
Roman Penyaev6c5c2402019-11-28 12:53:22 +01005114static void *io_uring_validate_mmap_request(struct file *file,
5115 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005116{
Jens Axboe2b188cc2019-01-07 10:46:33 -07005117 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01005118 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005119 struct page *page;
5120 void *ptr;
5121
5122 switch (offset) {
5123 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00005124 case IORING_OFF_CQ_RING:
5125 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005126 break;
5127 case IORING_OFF_SQES:
5128 ptr = ctx->sq_sqes;
5129 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005130 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01005131 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005132 }
5133
5134 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07005135 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01005136 return ERR_PTR(-EINVAL);
5137
5138 return ptr;
5139}
5140
5141#ifdef CONFIG_MMU
5142
5143static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
5144{
5145 size_t sz = vma->vm_end - vma->vm_start;
5146 unsigned long pfn;
5147 void *ptr;
5148
5149 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
5150 if (IS_ERR(ptr))
5151 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005152
5153 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
5154 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
5155}
5156
Roman Penyaev6c5c2402019-11-28 12:53:22 +01005157#else /* !CONFIG_MMU */
5158
5159static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
5160{
5161 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
5162}
5163
5164static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
5165{
5166 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
5167}
5168
5169static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
5170 unsigned long addr, unsigned long len,
5171 unsigned long pgoff, unsigned long flags)
5172{
5173 void *ptr;
5174
5175 ptr = io_uring_validate_mmap_request(file, pgoff, len);
5176 if (IS_ERR(ptr))
5177 return PTR_ERR(ptr);
5178
5179 return (unsigned long) ptr;
5180}
5181
5182#endif /* !CONFIG_MMU */
5183
Jens Axboe2b188cc2019-01-07 10:46:33 -07005184SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
5185 u32, min_complete, u32, flags, const sigset_t __user *, sig,
5186 size_t, sigsz)
5187{
5188 struct io_ring_ctx *ctx;
5189 long ret = -EBADF;
5190 int submitted = 0;
5191 struct fd f;
5192
Jens Axboe6c271ce2019-01-10 11:22:30 -07005193 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07005194 return -EINVAL;
5195
5196 f = fdget(fd);
5197 if (!f.file)
5198 return -EBADF;
5199
5200 ret = -EOPNOTSUPP;
5201 if (f.file->f_op != &io_uring_fops)
5202 goto out_fput;
5203
5204 ret = -ENXIO;
5205 ctx = f.file->private_data;
5206 if (!percpu_ref_tryget(&ctx->refs))
5207 goto out_fput;
5208
Jens Axboe6c271ce2019-01-10 11:22:30 -07005209 /*
5210 * For SQ polling, the thread will do all submissions and completions.
5211 * Just return the requested submit count, and wake the thread if
5212 * we were asked to.
5213 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06005214 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005215 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07005216 if (!list_empty_careful(&ctx->cq_overflow_list))
5217 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005218 if (flags & IORING_ENTER_SQ_WAKEUP)
5219 wake_up(&ctx->sqo_wait);
5220 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06005221 } else if (to_submit) {
Pavel Begunkovae9428c2019-11-06 00:22:14 +03005222 struct mm_struct *cur_mm;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005223
Jens Axboe44d28272020-01-16 19:00:24 -07005224 if (current->mm != ctx->sqo_mm ||
5225 current_cred() != ctx->creds) {
5226 ret = -EPERM;
5227 goto out;
5228 }
5229
Pavel Begunkovae9428c2019-11-06 00:22:14 +03005230 to_submit = min(to_submit, ctx->sq_entries);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005231 mutex_lock(&ctx->uring_lock);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03005232 /* already have mm, so io_submit_sqes() won't try to grab it */
5233 cur_mm = ctx->sqo_mm;
5234 submitted = io_submit_sqes(ctx, to_submit, f.file, fd,
5235 &cur_mm, false);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005236 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03005237
5238 if (submitted != to_submit)
5239 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005240 }
5241 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07005242 unsigned nr_events = 0;
5243
Jens Axboe2b188cc2019-01-07 10:46:33 -07005244 min_complete = min(min_complete, ctx->cq_entries);
5245
Jens Axboedef596e2019-01-09 08:59:42 -07005246 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07005247 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07005248 } else {
5249 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
5250 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07005251 }
5252
Pavel Begunkov7c504e652019-12-18 19:53:45 +03005253out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03005254 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005255out_fput:
5256 fdput(f);
5257 return submitted ? submitted : ret;
5258}
5259
5260static const struct file_operations io_uring_fops = {
5261 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06005262 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07005263 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01005264#ifndef CONFIG_MMU
5265 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
5266 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
5267#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07005268 .poll = io_uring_poll,
5269 .fasync = io_uring_fasync,
5270};
5271
5272static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
5273 struct io_uring_params *p)
5274{
Hristo Venev75b28af2019-08-26 17:23:46 +00005275 struct io_rings *rings;
5276 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005277
Hristo Venev75b28af2019-08-26 17:23:46 +00005278 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
5279 if (size == SIZE_MAX)
5280 return -EOVERFLOW;
5281
5282 rings = io_mem_alloc(size);
5283 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005284 return -ENOMEM;
5285
Hristo Venev75b28af2019-08-26 17:23:46 +00005286 ctx->rings = rings;
5287 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
5288 rings->sq_ring_mask = p->sq_entries - 1;
5289 rings->cq_ring_mask = p->cq_entries - 1;
5290 rings->sq_ring_entries = p->sq_entries;
5291 rings->cq_ring_entries = p->cq_entries;
5292 ctx->sq_mask = rings->sq_ring_mask;
5293 ctx->cq_mask = rings->cq_ring_mask;
5294 ctx->sq_entries = rings->sq_ring_entries;
5295 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005296
5297 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07005298 if (size == SIZE_MAX) {
5299 io_mem_free(ctx->rings);
5300 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005301 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07005302 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07005303
5304 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07005305 if (!ctx->sq_sqes) {
5306 io_mem_free(ctx->rings);
5307 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005308 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07005309 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07005310
Jens Axboe2b188cc2019-01-07 10:46:33 -07005311 return 0;
5312}
5313
5314/*
5315 * Allocate an anonymous fd, this is what constitutes the application
5316 * visible backing of an io_uring instance. The application mmaps this
5317 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
5318 * we have to tie this fd to a socket for file garbage collection purposes.
5319 */
5320static int io_uring_get_fd(struct io_ring_ctx *ctx)
5321{
5322 struct file *file;
5323 int ret;
5324
5325#if defined(CONFIG_UNIX)
5326 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
5327 &ctx->ring_sock);
5328 if (ret)
5329 return ret;
5330#endif
5331
5332 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
5333 if (ret < 0)
5334 goto err;
5335
5336 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
5337 O_RDWR | O_CLOEXEC);
5338 if (IS_ERR(file)) {
5339 put_unused_fd(ret);
5340 ret = PTR_ERR(file);
5341 goto err;
5342 }
5343
5344#if defined(CONFIG_UNIX)
5345 ctx->ring_sock->file = file;
Jens Axboe6b063142019-01-10 22:13:58 -07005346 ctx->ring_sock->sk->sk_user_data = ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005347#endif
5348 fd_install(ret, file);
5349 return ret;
5350err:
5351#if defined(CONFIG_UNIX)
5352 sock_release(ctx->ring_sock);
5353 ctx->ring_sock = NULL;
5354#endif
5355 return ret;
5356}
5357
5358static int io_uring_create(unsigned entries, struct io_uring_params *p)
5359{
5360 struct user_struct *user = NULL;
5361 struct io_ring_ctx *ctx;
5362 bool account_mem;
5363 int ret;
5364
5365 if (!entries || entries > IORING_MAX_ENTRIES)
5366 return -EINVAL;
5367
5368 /*
5369 * Use twice as many entries for the CQ ring. It's possible for the
5370 * application to drive a higher depth than the size of the SQ ring,
5371 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06005372 * some flexibility in overcommitting a bit. If the application has
5373 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
5374 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07005375 */
5376 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06005377 if (p->flags & IORING_SETUP_CQSIZE) {
5378 /*
5379 * If IORING_SETUP_CQSIZE is set, we do the same roundup
5380 * to a power-of-two, if it isn't already. We do NOT impose
5381 * any cq vs sq ring sizing.
5382 */
5383 if (p->cq_entries < p->sq_entries || p->cq_entries > IORING_MAX_CQ_ENTRIES)
5384 return -EINVAL;
5385 p->cq_entries = roundup_pow_of_two(p->cq_entries);
5386 } else {
5387 p->cq_entries = 2 * p->sq_entries;
5388 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07005389
5390 user = get_uid(current_user());
5391 account_mem = !capable(CAP_IPC_LOCK);
5392
5393 if (account_mem) {
5394 ret = io_account_mem(user,
5395 ring_pages(p->sq_entries, p->cq_entries));
5396 if (ret) {
5397 free_uid(user);
5398 return ret;
5399 }
5400 }
5401
5402 ctx = io_ring_ctx_alloc(p);
5403 if (!ctx) {
5404 if (account_mem)
5405 io_unaccount_mem(user, ring_pages(p->sq_entries,
5406 p->cq_entries));
5407 free_uid(user);
5408 return -ENOMEM;
5409 }
5410 ctx->compat = in_compat_syscall();
5411 ctx->account_mem = account_mem;
5412 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07005413 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07005414
5415 ret = io_allocate_scq_urings(ctx, p);
5416 if (ret)
5417 goto err;
5418
Jens Axboe6c271ce2019-01-10 11:22:30 -07005419 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005420 if (ret)
5421 goto err;
5422
Jens Axboe2b188cc2019-01-07 10:46:33 -07005423 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00005424 p->sq_off.head = offsetof(struct io_rings, sq.head);
5425 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
5426 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
5427 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
5428 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
5429 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
5430 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005431
5432 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00005433 p->cq_off.head = offsetof(struct io_rings, cq.head);
5434 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
5435 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
5436 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
5437 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
5438 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06005439
Jens Axboe044c1ab2019-10-28 09:15:33 -06005440 /*
5441 * Install ring fd as the very last thing, so we don't risk someone
5442 * having closed it before we finish setup
5443 */
5444 ret = io_uring_get_fd(ctx);
5445 if (ret < 0)
5446 goto err;
5447
Jens Axboeda8c9692019-12-02 18:51:26 -07005448 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
5449 IORING_FEAT_SUBMIT_STABLE;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02005450 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005451 return ret;
5452err:
5453 io_ring_ctx_wait_and_kill(ctx);
5454 return ret;
5455}
5456
5457/*
5458 * Sets up an aio uring context, and returns the fd. Applications asks for a
5459 * ring size, we return the actual sq/cq ring sizes (among other things) in the
5460 * params structure passed in.
5461 */
5462static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
5463{
5464 struct io_uring_params p;
5465 long ret;
5466 int i;
5467
5468 if (copy_from_user(&p, params, sizeof(p)))
5469 return -EFAULT;
5470 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
5471 if (p.resv[i])
5472 return -EINVAL;
5473 }
5474
Jens Axboe6c271ce2019-01-10 11:22:30 -07005475 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe33a107f2019-10-04 12:10:03 -06005476 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE))
Jens Axboe2b188cc2019-01-07 10:46:33 -07005477 return -EINVAL;
5478
5479 ret = io_uring_create(entries, &p);
5480 if (ret < 0)
5481 return ret;
5482
5483 if (copy_to_user(params, &p, sizeof(p)))
5484 return -EFAULT;
5485
5486 return ret;
5487}
5488
5489SYSCALL_DEFINE2(io_uring_setup, u32, entries,
5490 struct io_uring_params __user *, params)
5491{
5492 return io_uring_setup(entries, params);
5493}
5494
Jens Axboeedafcce2019-01-09 09:16:05 -07005495static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
5496 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06005497 __releases(ctx->uring_lock)
5498 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07005499{
5500 int ret;
5501
Jens Axboe35fa71a2019-04-22 10:23:23 -06005502 /*
5503 * We're inside the ring mutex, if the ref is already dying, then
5504 * someone else killed the ctx or is already going through
5505 * io_uring_register().
5506 */
5507 if (percpu_ref_is_dying(&ctx->refs))
5508 return -ENXIO;
5509
Jens Axboeedafcce2019-01-09 09:16:05 -07005510 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06005511
5512 /*
5513 * Drop uring mutex before waiting for references to exit. If another
5514 * thread is currently inside io_uring_enter() it might need to grab
5515 * the uring_lock to make progress. If we hold it here across the drain
5516 * wait, then we can deadlock. It's safe to drop the mutex here, since
5517 * no new references will come in after we've killed the percpu ref.
5518 */
5519 mutex_unlock(&ctx->uring_lock);
Jens Axboe206aefd2019-11-07 18:27:42 -07005520 wait_for_completion(&ctx->completions[0]);
Jens Axboeb19062a2019-04-15 10:49:38 -06005521 mutex_lock(&ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07005522
5523 switch (opcode) {
5524 case IORING_REGISTER_BUFFERS:
5525 ret = io_sqe_buffer_register(ctx, arg, nr_args);
5526 break;
5527 case IORING_UNREGISTER_BUFFERS:
5528 ret = -EINVAL;
5529 if (arg || nr_args)
5530 break;
5531 ret = io_sqe_buffer_unregister(ctx);
5532 break;
Jens Axboe6b063142019-01-10 22:13:58 -07005533 case IORING_REGISTER_FILES:
5534 ret = io_sqe_files_register(ctx, arg, nr_args);
5535 break;
5536 case IORING_UNREGISTER_FILES:
5537 ret = -EINVAL;
5538 if (arg || nr_args)
5539 break;
5540 ret = io_sqe_files_unregister(ctx);
5541 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06005542 case IORING_REGISTER_FILES_UPDATE:
5543 ret = io_sqe_files_update(ctx, arg, nr_args);
5544 break;
Jens Axboe9b402842019-04-11 11:45:41 -06005545 case IORING_REGISTER_EVENTFD:
5546 ret = -EINVAL;
5547 if (nr_args != 1)
5548 break;
5549 ret = io_eventfd_register(ctx, arg);
5550 break;
5551 case IORING_UNREGISTER_EVENTFD:
5552 ret = -EINVAL;
5553 if (arg || nr_args)
5554 break;
5555 ret = io_eventfd_unregister(ctx);
5556 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07005557 default:
5558 ret = -EINVAL;
5559 break;
5560 }
5561
5562 /* bring the ctx back to life */
Jens Axboe206aefd2019-11-07 18:27:42 -07005563 reinit_completion(&ctx->completions[0]);
Jens Axboeedafcce2019-01-09 09:16:05 -07005564 percpu_ref_reinit(&ctx->refs);
5565 return ret;
5566}
5567
5568SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
5569 void __user *, arg, unsigned int, nr_args)
5570{
5571 struct io_ring_ctx *ctx;
5572 long ret = -EBADF;
5573 struct fd f;
5574
5575 f = fdget(fd);
5576 if (!f.file)
5577 return -EBADF;
5578
5579 ret = -EOPNOTSUPP;
5580 if (f.file->f_op != &io_uring_fops)
5581 goto out_fput;
5582
5583 ctx = f.file->private_data;
5584
5585 mutex_lock(&ctx->uring_lock);
5586 ret = __io_uring_register(ctx, opcode, arg, nr_args);
5587 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02005588 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
5589 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07005590out_fput:
5591 fdput(f);
5592 return ret;
5593}
5594
Jens Axboe2b188cc2019-01-07 10:46:33 -07005595static int __init io_uring_init(void)
5596{
5597 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
5598 return 0;
5599};
5600__initcall(io_uring_init);