blob: 7e8d2875005322e8a104c47b6dde8f19f7160c3c [file] [log] [blame]
Jens Axboe2b188cc2019-01-07 10:46:33 -07001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Shared application/kernel submission and completion ring pairs, for
4 * supporting fast/efficient IO.
5 *
6 * A note on the read/write ordering memory barriers that are matched between
Stefan Bühler1e84b972019-04-24 23:54:16 +02007 * the application and kernel side.
8 *
9 * After the application reads the CQ ring tail, it must use an
10 * appropriate smp_rmb() to pair with the smp_wmb() the kernel uses
11 * before writing the tail (using smp_load_acquire to read the tail will
12 * do). It also needs a smp_mb() before updating CQ head (ordering the
13 * entry load(s) with the head store), pairing with an implicit barrier
14 * through a control-dependency in io_get_cqring (smp_store_release to
15 * store head will do). Failure to do so could lead to reading invalid
16 * CQ entries.
17 *
18 * Likewise, the application must use an appropriate smp_wmb() before
19 * writing the SQ tail (ordering SQ entry stores with the tail store),
20 * which pairs with smp_load_acquire in io_get_sqring (smp_store_release
21 * to store the tail will do). And it needs a barrier ordering the SQ
22 * head load before writing new SQ entries (smp_load_acquire to read
23 * head will do).
24 *
25 * When using the SQ poll thread (IORING_SETUP_SQPOLL), the application
26 * needs to check the SQ flags for IORING_SQ_NEED_WAKEUP *after*
27 * updating the SQ tail; a full memory barrier smp_mb() is needed
28 * between.
Jens Axboe2b188cc2019-01-07 10:46:33 -070029 *
30 * Also see the examples in the liburing library:
31 *
32 * git://git.kernel.dk/liburing
33 *
34 * io_uring also uses READ/WRITE_ONCE() for _any_ store or load that happens
35 * from data shared between the kernel and application. This is done both
36 * for ordering purposes, but also to ensure that once a value is loaded from
37 * data that the application could potentially modify, it remains stable.
38 *
39 * Copyright (C) 2018-2019 Jens Axboe
Christoph Hellwigc992fe22019-01-11 09:43:02 -070040 * Copyright (c) 2018-2019 Christoph Hellwig
Jens Axboe2b188cc2019-01-07 10:46:33 -070041 */
42#include <linux/kernel.h>
43#include <linux/init.h>
44#include <linux/errno.h>
45#include <linux/syscalls.h>
46#include <linux/compat.h>
47#include <linux/refcount.h>
48#include <linux/uio.h>
49
50#include <linux/sched/signal.h>
51#include <linux/fs.h>
52#include <linux/file.h>
53#include <linux/fdtable.h>
54#include <linux/mm.h>
55#include <linux/mman.h>
56#include <linux/mmu_context.h>
57#include <linux/percpu.h>
58#include <linux/slab.h>
Jens Axboe6c271ce2019-01-10 11:22:30 -070059#include <linux/kthread.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070060#include <linux/blkdev.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070061#include <linux/bvec.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070062#include <linux/net.h>
63#include <net/sock.h>
64#include <net/af_unix.h>
Jens Axboe6b063142019-01-10 22:13:58 -070065#include <net/scm.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070066#include <linux/anon_inodes.h>
67#include <linux/sched/mm.h>
68#include <linux/uaccess.h>
69#include <linux/nospec.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070070#include <linux/sizes.h>
71#include <linux/hugetlb.h>
Jens Axboeaa4c3962019-11-29 10:14:00 -070072#include <linux/highmem.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070073
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020074#define CREATE_TRACE_POINTS
75#include <trace/events/io_uring.h>
76
Jens Axboe2b188cc2019-01-07 10:46:33 -070077#include <uapi/linux/io_uring.h>
78
79#include "internal.h"
Jens Axboe561fb042019-10-24 07:25:42 -060080#include "io-wq.h"
Jens Axboe2b188cc2019-01-07 10:46:33 -070081
Daniel Xu5277dea2019-09-14 14:23:45 -070082#define IORING_MAX_ENTRIES 32768
Jens Axboe33a107f2019-10-04 12:10:03 -060083#define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES)
Jens Axboe65e19f52019-10-26 07:20:21 -060084
85/*
86 * Shift of 9 is 512 entries, or exactly one page on 64-bit archs
87 */
88#define IORING_FILE_TABLE_SHIFT 9
89#define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT)
90#define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1)
91#define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE)
Jens Axboe2b188cc2019-01-07 10:46:33 -070092
93struct io_uring {
94 u32 head ____cacheline_aligned_in_smp;
95 u32 tail ____cacheline_aligned_in_smp;
96};
97
Stefan Bühler1e84b972019-04-24 23:54:16 +020098/*
Hristo Venev75b28af2019-08-26 17:23:46 +000099 * This data is shared with the application through the mmap at offsets
100 * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200101 *
102 * The offsets to the member fields are published through struct
103 * io_sqring_offsets when calling io_uring_setup.
104 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000105struct io_rings {
Stefan Bühler1e84b972019-04-24 23:54:16 +0200106 /*
107 * Head and tail offsets into the ring; the offsets need to be
108 * masked to get valid indices.
109 *
Hristo Venev75b28af2019-08-26 17:23:46 +0000110 * The kernel controls head of the sq ring and the tail of the cq ring,
111 * and the application controls tail of the sq ring and the head of the
112 * cq ring.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200113 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000114 struct io_uring sq, cq;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200115 /*
Hristo Venev75b28af2019-08-26 17:23:46 +0000116 * Bitmasks to apply to head and tail offsets (constant, equals
Stefan Bühler1e84b972019-04-24 23:54:16 +0200117 * ring_entries - 1)
118 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000119 u32 sq_ring_mask, cq_ring_mask;
120 /* Ring sizes (constant, power of 2) */
121 u32 sq_ring_entries, cq_ring_entries;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200122 /*
123 * Number of invalid entries dropped by the kernel due to
124 * invalid index stored in array
125 *
126 * Written by the kernel, shouldn't be modified by the
127 * application (i.e. get number of "new events" by comparing to
128 * cached value).
129 *
130 * After a new SQ head value was read by the application this
131 * counter includes all submissions that were dropped reaching
132 * the new SQ head (and possibly more).
133 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000134 u32 sq_dropped;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200135 /*
136 * Runtime flags
137 *
138 * Written by the kernel, shouldn't be modified by the
139 * application.
140 *
141 * The application needs a full memory barrier before checking
142 * for IORING_SQ_NEED_WAKEUP after updating the sq tail.
143 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000144 u32 sq_flags;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200145 /*
146 * Number of completion events lost because the queue was full;
147 * this should be avoided by the application by making sure
LimingWu0b4295b2019-12-05 20:18:18 +0800148 * there are not more requests pending than there is space in
Stefan Bühler1e84b972019-04-24 23:54:16 +0200149 * the completion queue.
150 *
151 * Written by the kernel, shouldn't be modified by the
152 * application (i.e. get number of "new events" by comparing to
153 * cached value).
154 *
155 * As completion events come in out of order this counter is not
156 * ordered with any other data.
157 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000158 u32 cq_overflow;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200159 /*
160 * Ring buffer of completion events.
161 *
162 * The kernel writes completion events fresh every time they are
163 * produced, so the application is allowed to modify pending
164 * entries.
165 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000166 struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700167};
168
Jens Axboeedafcce2019-01-09 09:16:05 -0700169struct io_mapped_ubuf {
170 u64 ubuf;
171 size_t len;
172 struct bio_vec *bvec;
173 unsigned int nr_bvecs;
174};
175
Jens Axboe65e19f52019-10-26 07:20:21 -0600176struct fixed_file_table {
177 struct file **files;
Jens Axboe31b51512019-01-18 22:56:34 -0700178};
179
Jens Axboe2b188cc2019-01-07 10:46:33 -0700180struct io_ring_ctx {
181 struct {
182 struct percpu_ref refs;
183 } ____cacheline_aligned_in_smp;
184
185 struct {
186 unsigned int flags;
187 bool compat;
188 bool account_mem;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700189 bool cq_overflow_flushed;
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +0300190 bool drain_next;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700191
Hristo Venev75b28af2019-08-26 17:23:46 +0000192 /*
193 * Ring buffer of indices into array of io_uring_sqe, which is
194 * mmapped by the application using the IORING_OFF_SQES offset.
195 *
196 * This indirection could e.g. be used to assign fixed
197 * io_uring_sqe entries to operations and only submit them to
198 * the queue when needed.
199 *
200 * The kernel modifies neither the indices array nor the entries
201 * array.
202 */
203 u32 *sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700204 unsigned cached_sq_head;
205 unsigned sq_entries;
206 unsigned sq_mask;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700207 unsigned sq_thread_idle;
Jens Axboe498ccd92019-10-25 10:04:25 -0600208 unsigned cached_sq_dropped;
Jens Axboe206aefd2019-11-07 18:27:42 -0700209 atomic_t cached_cq_overflow;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700210 struct io_uring_sqe *sq_sqes;
Jens Axboede0617e2019-04-06 21:51:27 -0600211
212 struct list_head defer_list;
Jens Axboe5262f562019-09-17 12:26:57 -0600213 struct list_head timeout_list;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700214 struct list_head cq_overflow_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700215
Jens Axboefcb323c2019-10-24 12:39:47 -0600216 wait_queue_head_t inflight_wait;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700217 } ____cacheline_aligned_in_smp;
218
Hristo Venev75b28af2019-08-26 17:23:46 +0000219 struct io_rings *rings;
220
Jens Axboe2b188cc2019-01-07 10:46:33 -0700221 /* IO offload */
Jens Axboe561fb042019-10-24 07:25:42 -0600222 struct io_wq *io_wq;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700223 struct task_struct *sqo_thread; /* if using sq thread polling */
224 struct mm_struct *sqo_mm;
225 wait_queue_head_t sqo_wait;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700226
Jens Axboe6b063142019-01-10 22:13:58 -0700227 /*
228 * If used, fixed file set. Writers must ensure that ->refs is dead,
229 * readers must ensure that ->refs is alive as long as the file* is
230 * used. Only updated through io_uring_register(2).
231 */
Jens Axboe65e19f52019-10-26 07:20:21 -0600232 struct fixed_file_table *file_table;
Jens Axboe6b063142019-01-10 22:13:58 -0700233 unsigned nr_user_files;
234
Jens Axboeedafcce2019-01-09 09:16:05 -0700235 /* if used, fixed mapped user buffers */
236 unsigned nr_user_bufs;
237 struct io_mapped_ubuf *user_bufs;
238
Jens Axboe2b188cc2019-01-07 10:46:33 -0700239 struct user_struct *user;
240
Jens Axboe0b8c0ec2019-12-02 08:50:00 -0700241 const struct cred *creds;
Jens Axboe181e4482019-11-25 08:52:30 -0700242
Jens Axboe206aefd2019-11-07 18:27:42 -0700243 /* 0 is for ctx quiesce/reinit/free, 1 is for sqo_thread started */
244 struct completion *completions;
245
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700246 /* if all else fails... */
247 struct io_kiocb *fallback_req;
248
Jens Axboe206aefd2019-11-07 18:27:42 -0700249#if defined(CONFIG_UNIX)
250 struct socket *ring_sock;
251#endif
252
253 struct {
254 unsigned cached_cq_tail;
255 unsigned cq_entries;
256 unsigned cq_mask;
257 atomic_t cq_timeouts;
258 struct wait_queue_head cq_wait;
259 struct fasync_struct *cq_fasync;
260 struct eventfd_ctx *cq_ev_fd;
261 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700262
263 struct {
264 struct mutex uring_lock;
265 wait_queue_head_t wait;
266 } ____cacheline_aligned_in_smp;
267
268 struct {
269 spinlock_t completion_lock;
Jens Axboedef596e2019-01-09 08:59:42 -0700270 bool poll_multi_file;
271 /*
272 * ->poll_list is protected by the ctx->uring_lock for
273 * io_uring instances that don't use IORING_SETUP_SQPOLL.
274 * For SQPOLL, only the single threaded io_sq_thread() will
275 * manipulate the list, hence no extra locking is needed there.
276 */
277 struct list_head poll_list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700278 struct hlist_head *cancel_hash;
279 unsigned cancel_hash_bits;
Jens Axboefcb323c2019-10-24 12:39:47 -0600280
281 spinlock_t inflight_lock;
282 struct list_head inflight_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700283 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700284};
285
Jens Axboe09bb8392019-03-13 12:39:28 -0600286/*
287 * First field must be the file pointer in all the
288 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
289 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700290struct io_poll_iocb {
291 struct file *file;
Jens Axboe0969e782019-12-17 18:40:57 -0700292 union {
293 struct wait_queue_head *head;
294 u64 addr;
295 };
Jens Axboe221c5eb2019-01-17 09:41:58 -0700296 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600297 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700298 bool canceled;
Jens Axboe392edb42019-12-09 17:52:20 -0700299 struct wait_queue_entry wait;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700300};
301
Jens Axboead8a48a2019-11-15 08:49:11 -0700302struct io_timeout_data {
303 struct io_kiocb *req;
304 struct hrtimer timer;
305 struct timespec64 ts;
306 enum hrtimer_mode mode;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +0300307 u32 seq_offset;
Jens Axboead8a48a2019-11-15 08:49:11 -0700308};
309
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700310struct io_accept {
311 struct file *file;
312 struct sockaddr __user *addr;
313 int __user *addr_len;
314 int flags;
315};
316
317struct io_sync {
318 struct file *file;
319 loff_t len;
320 loff_t off;
321 int flags;
322};
323
Jens Axboefbf23842019-12-17 18:45:56 -0700324struct io_cancel {
325 struct file *file;
326 u64 addr;
327};
328
Jens Axboeb29472e2019-12-17 18:50:29 -0700329struct io_timeout {
330 struct file *file;
331 u64 addr;
332 int flags;
Jens Axboe26a61672019-12-20 09:02:01 -0700333 unsigned count;
Jens Axboeb29472e2019-12-17 18:50:29 -0700334};
335
Jens Axboe9adbd452019-12-20 08:45:55 -0700336struct io_rw {
337 /* NOTE: kiocb has the file as the first member, so don't do it here */
338 struct kiocb kiocb;
339 u64 addr;
340 u64 len;
341};
342
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700343struct io_connect {
344 struct file *file;
345 struct sockaddr __user *addr;
346 int addr_len;
347};
348
Jens Axboee47293f2019-12-20 08:58:21 -0700349struct io_sr_msg {
350 struct file *file;
351 struct user_msghdr __user *msg;
352 int msg_flags;
353};
354
Jens Axboef499a022019-12-02 16:28:46 -0700355struct io_async_connect {
356 struct sockaddr_storage address;
357};
358
Jens Axboe03b12302019-12-02 18:50:25 -0700359struct io_async_msghdr {
360 struct iovec fast_iov[UIO_FASTIOV];
361 struct iovec *iov;
362 struct sockaddr __user *uaddr;
363 struct msghdr msg;
364};
365
Jens Axboef67676d2019-12-02 11:03:47 -0700366struct io_async_rw {
367 struct iovec fast_iov[UIO_FASTIOV];
368 struct iovec *iov;
369 ssize_t nr_segs;
370 ssize_t size;
371};
372
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700373struct io_async_ctx {
374 struct io_uring_sqe sqe;
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
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300402 const struct io_uring_sqe *sqe;
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700403 struct io_async_ctx *io;
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300404 struct file *ring_file;
405 int ring_fd;
406 bool has_user;
407 bool in_async;
408 bool needs_fixed_file;
Jens Axboed625c6e2019-12-17 19:53:05 -0700409 u8 opcode;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700410
411 struct io_ring_ctx *ctx;
Jens Axboeeac406c2019-11-14 12:09:58 -0700412 union {
413 struct list_head list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700414 struct hlist_node hash_node;
Jens Axboeeac406c2019-11-14 12:09:58 -0700415 };
Jens Axboe9e645e112019-05-10 16:07:28 -0600416 struct list_head link_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700417 unsigned int flags;
Jens Axboec16361c2019-01-17 08:39:48 -0700418 refcount_t refs;
Stefan Bühler8449eed2019-04-27 20:34:19 +0200419#define REQ_F_NOWAIT 1 /* must not punt to workers */
Jens Axboedef596e2019-01-09 08:59:42 -0700420#define REQ_F_IOPOLL_COMPLETED 2 /* polled IO has completed */
Jens Axboe6b063142019-01-10 22:13:58 -0700421#define REQ_F_FIXED_FILE 4 /* ctx owns file */
Jens Axboe4d7dd462019-11-20 13:03:52 -0700422#define REQ_F_LINK_NEXT 8 /* already grabbed next link */
Stefan Bühlere2033e32019-05-11 19:08:01 +0200423#define REQ_F_IO_DRAIN 16 /* drain existing IO first */
424#define REQ_F_IO_DRAINED 32 /* drain done */
Jens Axboe9e645e112019-05-10 16:07:28 -0600425#define REQ_F_LINK 64 /* linked sqes */
Jens Axboe2665abf2019-11-05 12:40:47 -0700426#define REQ_F_LINK_TIMEOUT 128 /* has linked timeout */
Zhengyuan Liuf7b76ac2019-07-16 23:26:14 +0800427#define REQ_F_FAIL_LINK 256 /* fail rest of links */
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +0300428#define REQ_F_DRAIN_LINK 512 /* link should be fully drained */
Jens Axboe5262f562019-09-17 12:26:57 -0600429#define REQ_F_TIMEOUT 1024 /* timeout request */
Jens Axboe491381ce2019-10-17 09:20:46 -0600430#define REQ_F_ISREG 2048 /* regular file */
431#define REQ_F_MUST_PUNT 4096 /* must be punted even for NONBLOCK */
Jens Axboe93bd25b2019-11-11 23:34:31 -0700432#define REQ_F_TIMEOUT_NOSEQ 8192 /* no timeout sequence */
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800433#define REQ_F_INFLIGHT 16384 /* on inflight list */
434#define REQ_F_COMP_LOCKED 32768 /* completion under lock */
Jens Axboe4e88d6e2019-12-07 20:59:47 -0700435#define REQ_F_HARDLINK 65536 /* doesn't sever on completion < 0 */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700436#define REQ_F_PREPPED 131072 /* request already opcode prepared */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700437 u64 user_data;
Jens Axboe9e645e112019-05-10 16:07:28 -0600438 u32 result;
Jens Axboede0617e2019-04-06 21:51:27 -0600439 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700440
Jens Axboefcb323c2019-10-24 12:39:47 -0600441 struct list_head inflight_entry;
442
Jens Axboe561fb042019-10-24 07:25:42 -0600443 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700444};
445
446#define IO_PLUG_THRESHOLD 2
Jens Axboedef596e2019-01-09 08:59:42 -0700447#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700448
Jens Axboe9a56a232019-01-09 09:06:50 -0700449struct io_submit_state {
450 struct blk_plug plug;
451
452 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700453 * io_kiocb alloc cache
454 */
455 void *reqs[IO_IOPOLL_BATCH];
456 unsigned int free_reqs;
457 unsigned int cur_req;
458
459 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700460 * File reference cache
461 */
462 struct file *file;
463 unsigned int fd;
464 unsigned int has_refs;
465 unsigned int used_refs;
466 unsigned int ios_left;
467};
468
Jens Axboe561fb042019-10-24 07:25:42 -0600469static void io_wq_submit_work(struct io_wq_work **workptr);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700470static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liu4fe2c962019-09-09 20:50:40 +0800471static void __io_free_req(struct io_kiocb *req);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800472static void io_put_req(struct io_kiocb *req);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700473static void io_double_put_req(struct io_kiocb *req);
Jens Axboe978db572019-11-14 22:39:04 -0700474static void __io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700475static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
476static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboede0617e2019-04-06 21:51:27 -0600477
Jens Axboe2b188cc2019-01-07 10:46:33 -0700478static struct kmem_cache *req_cachep;
479
480static const struct file_operations io_uring_fops;
481
482struct sock *io_uring_get_socket(struct file *file)
483{
484#if defined(CONFIG_UNIX)
485 if (file->f_op == &io_uring_fops) {
486 struct io_ring_ctx *ctx = file->private_data;
487
488 return ctx->ring_sock->sk;
489 }
490#endif
491 return NULL;
492}
493EXPORT_SYMBOL(io_uring_get_socket);
494
495static void io_ring_ctx_ref_free(struct percpu_ref *ref)
496{
497 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
498
Jens Axboe206aefd2019-11-07 18:27:42 -0700499 complete(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700500}
501
502static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
503{
504 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -0700505 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700506
507 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
508 if (!ctx)
509 return NULL;
510
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700511 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
512 if (!ctx->fallback_req)
513 goto err;
514
Jens Axboe206aefd2019-11-07 18:27:42 -0700515 ctx->completions = kmalloc(2 * sizeof(struct completion), GFP_KERNEL);
516 if (!ctx->completions)
517 goto err;
518
Jens Axboe78076bb2019-12-04 19:56:40 -0700519 /*
520 * Use 5 bits less than the max cq entries, that should give us around
521 * 32 entries per hash list if totally full and uniformly spread.
522 */
523 hash_bits = ilog2(p->cq_entries);
524 hash_bits -= 5;
525 if (hash_bits <= 0)
526 hash_bits = 1;
527 ctx->cancel_hash_bits = hash_bits;
528 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
529 GFP_KERNEL);
530 if (!ctx->cancel_hash)
531 goto err;
532 __hash_init(ctx->cancel_hash, 1U << hash_bits);
533
Roman Gushchin21482892019-05-07 10:01:48 -0700534 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -0700535 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
536 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700537
538 ctx->flags = p->flags;
539 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700540 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe206aefd2019-11-07 18:27:42 -0700541 init_completion(&ctx->completions[0]);
542 init_completion(&ctx->completions[1]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700543 mutex_init(&ctx->uring_lock);
544 init_waitqueue_head(&ctx->wait);
545 spin_lock_init(&ctx->completion_lock);
Jens Axboedef596e2019-01-09 08:59:42 -0700546 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboede0617e2019-04-06 21:51:27 -0600547 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -0600548 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -0600549 init_waitqueue_head(&ctx->inflight_wait);
550 spin_lock_init(&ctx->inflight_lock);
551 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700552 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -0700553err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700554 if (ctx->fallback_req)
555 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe206aefd2019-11-07 18:27:42 -0700556 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -0700557 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -0700558 kfree(ctx);
559 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700560}
561
Bob Liu9d858b22019-11-13 18:06:25 +0800562static inline bool __req_need_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -0600563{
Jackie Liua197f662019-11-08 08:09:12 -0700564 struct io_ring_ctx *ctx = req->ctx;
565
Jens Axboe498ccd92019-10-25 10:04:25 -0600566 return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped
567 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboede0617e2019-04-06 21:51:27 -0600568}
569
Bob Liu9d858b22019-11-13 18:06:25 +0800570static inline bool req_need_defer(struct io_kiocb *req)
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600571{
Bob Liu9d858b22019-11-13 18:06:25 +0800572 if ((req->flags & (REQ_F_IO_DRAIN|REQ_F_IO_DRAINED)) == REQ_F_IO_DRAIN)
573 return __req_need_defer(req);
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600574
Bob Liu9d858b22019-11-13 18:06:25 +0800575 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600576}
577
578static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -0600579{
580 struct io_kiocb *req;
581
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600582 req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list);
Bob Liu9d858b22019-11-13 18:06:25 +0800583 if (req && !req_need_defer(req)) {
Jens Axboede0617e2019-04-06 21:51:27 -0600584 list_del_init(&req->list);
585 return req;
586 }
587
588 return NULL;
589}
590
Jens Axboe5262f562019-09-17 12:26:57 -0600591static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx)
592{
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600593 struct io_kiocb *req;
594
595 req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list);
Jens Axboe93bd25b2019-11-11 23:34:31 -0700596 if (req) {
597 if (req->flags & REQ_F_TIMEOUT_NOSEQ)
598 return NULL;
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800599 if (!__req_need_defer(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -0700600 list_del_init(&req->list);
601 return req;
602 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600603 }
604
605 return NULL;
Jens Axboe5262f562019-09-17 12:26:57 -0600606}
607
Jens Axboede0617e2019-04-06 21:51:27 -0600608static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700609{
Hristo Venev75b28af2019-08-26 17:23:46 +0000610 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700611
Hristo Venev75b28af2019-08-26 17:23:46 +0000612 if (ctx->cached_cq_tail != READ_ONCE(rings->cq.tail)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700613 /* order cqe stores with ring update */
Hristo Venev75b28af2019-08-26 17:23:46 +0000614 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700615
Jens Axboe2b188cc2019-01-07 10:46:33 -0700616 if (wq_has_sleeper(&ctx->cq_wait)) {
617 wake_up_interruptible(&ctx->cq_wait);
618 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
619 }
620 }
621}
622
Jens Axboed625c6e2019-12-17 19:53:05 -0700623static inline bool io_req_needs_user(struct io_kiocb *req)
Jens Axboe18d9be12019-09-10 09:13:05 -0600624{
Jens Axboed625c6e2019-12-17 19:53:05 -0700625 return !(req->opcode == IORING_OP_READ_FIXED ||
626 req->opcode == IORING_OP_WRITE_FIXED);
Jens Axboe561fb042019-10-24 07:25:42 -0600627}
628
Jens Axboe94ae5e72019-11-14 19:39:52 -0700629static inline bool io_prep_async_work(struct io_kiocb *req,
630 struct io_kiocb **link)
Jens Axboe561fb042019-10-24 07:25:42 -0600631{
632 bool do_hashed = false;
Jens Axboe54a91f32019-09-10 09:15:04 -0600633
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300634 if (req->sqe) {
Jens Axboed625c6e2019-12-17 19:53:05 -0700635 switch (req->opcode) {
Jens Axboe6cc47d12019-09-18 11:18:23 -0600636 case IORING_OP_WRITEV:
637 case IORING_OP_WRITE_FIXED:
Jens Axboe53108d42019-12-09 20:12:38 -0700638 /* only regular files should be hashed for writes */
639 if (req->flags & REQ_F_ISREG)
640 do_hashed = true;
Jens Axboe5f8fd2d32019-11-07 10:57:36 -0700641 /* fall-through */
642 case IORING_OP_READV:
643 case IORING_OP_READ_FIXED:
644 case IORING_OP_SENDMSG:
645 case IORING_OP_RECVMSG:
646 case IORING_OP_ACCEPT:
647 case IORING_OP_POLL_ADD:
Jens Axboef8e85cf2019-11-23 14:24:24 -0700648 case IORING_OP_CONNECT:
Jens Axboe5f8fd2d32019-11-07 10:57:36 -0700649 /*
650 * We know REQ_F_ISREG is not set on some of these
651 * opcodes, but this enables us to keep the check in
652 * just one place.
653 */
654 if (!(req->flags & REQ_F_ISREG))
655 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe6cc47d12019-09-18 11:18:23 -0600656 break;
657 }
Jens Axboed625c6e2019-12-17 19:53:05 -0700658 if (io_req_needs_user(req))
Jens Axboe561fb042019-10-24 07:25:42 -0600659 req->work.flags |= IO_WQ_WORK_NEEDS_USER;
Jens Axboe54a91f32019-09-10 09:15:04 -0600660 }
661
Jens Axboe94ae5e72019-11-14 19:39:52 -0700662 *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -0600663 return do_hashed;
664}
665
Jackie Liua197f662019-11-08 08:09:12 -0700666static inline void io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -0600667{
Jackie Liua197f662019-11-08 08:09:12 -0700668 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe94ae5e72019-11-14 19:39:52 -0700669 struct io_kiocb *link;
670 bool do_hashed;
671
672 do_hashed = io_prep_async_work(req, &link);
Jens Axboe561fb042019-10-24 07:25:42 -0600673
674 trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work,
675 req->flags);
676 if (!do_hashed) {
677 io_wq_enqueue(ctx->io_wq, &req->work);
678 } else {
679 io_wq_enqueue_hashed(ctx->io_wq, &req->work,
680 file_inode(req->file));
681 }
Jens Axboe94ae5e72019-11-14 19:39:52 -0700682
683 if (link)
684 io_queue_linked_timeout(link);
Jens Axboe18d9be12019-09-10 09:13:05 -0600685}
686
Jens Axboe5262f562019-09-17 12:26:57 -0600687static void io_kill_timeout(struct io_kiocb *req)
688{
689 int ret;
690
Jens Axboe2d283902019-12-04 11:08:05 -0700691 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe5262f562019-09-17 12:26:57 -0600692 if (ret != -1) {
693 atomic_inc(&req->ctx->cq_timeouts);
Jens Axboe842f9612019-10-29 12:34:10 -0600694 list_del_init(&req->list);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700695 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800696 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -0600697 }
698}
699
700static void io_kill_timeouts(struct io_ring_ctx *ctx)
701{
702 struct io_kiocb *req, *tmp;
703
704 spin_lock_irq(&ctx->completion_lock);
705 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
706 io_kill_timeout(req);
707 spin_unlock_irq(&ctx->completion_lock);
708}
709
Jens Axboede0617e2019-04-06 21:51:27 -0600710static void io_commit_cqring(struct io_ring_ctx *ctx)
711{
712 struct io_kiocb *req;
713
Jens Axboe5262f562019-09-17 12:26:57 -0600714 while ((req = io_get_timeout_req(ctx)) != NULL)
715 io_kill_timeout(req);
716
Jens Axboede0617e2019-04-06 21:51:27 -0600717 __io_commit_cqring(ctx);
718
719 while ((req = io_get_deferred_req(ctx)) != NULL) {
720 req->flags |= REQ_F_IO_DRAINED;
Jackie Liua197f662019-11-08 08:09:12 -0700721 io_queue_async_work(req);
Jens Axboede0617e2019-04-06 21:51:27 -0600722 }
723}
724
Jens Axboe2b188cc2019-01-07 10:46:33 -0700725static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
726{
Hristo Venev75b28af2019-08-26 17:23:46 +0000727 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700728 unsigned tail;
729
730 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +0200731 /*
732 * writes to the cq entry need to come after reading head; the
733 * control dependency is enough as we're using WRITE_ONCE to
734 * fill the cq entry
735 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000736 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700737 return NULL;
738
739 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +0000740 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -0700741}
742
Jens Axboe8c838782019-03-12 15:48:16 -0600743static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
744{
745 if (waitqueue_active(&ctx->wait))
746 wake_up(&ctx->wait);
747 if (waitqueue_active(&ctx->sqo_wait))
748 wake_up(&ctx->sqo_wait);
Jens Axboe9b402842019-04-11 11:45:41 -0600749 if (ctx->cq_ev_fd)
750 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -0600751}
752
Jens Axboec4a2ed72019-11-21 21:01:26 -0700753/* Returns true if there are no backlogged entries after the flush */
754static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700755{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700756 struct io_rings *rings = ctx->rings;
757 struct io_uring_cqe *cqe;
758 struct io_kiocb *req;
759 unsigned long flags;
760 LIST_HEAD(list);
761
762 if (!force) {
763 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -0700764 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700765 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
766 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -0700767 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700768 }
769
770 spin_lock_irqsave(&ctx->completion_lock, flags);
771
772 /* if force is set, the ring is going away. always drop after that */
773 if (force)
774 ctx->cq_overflow_flushed = true;
775
Jens Axboec4a2ed72019-11-21 21:01:26 -0700776 cqe = NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700777 while (!list_empty(&ctx->cq_overflow_list)) {
778 cqe = io_get_cqring(ctx);
779 if (!cqe && !force)
780 break;
781
782 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
783 list);
784 list_move(&req->list, &list);
785 if (cqe) {
786 WRITE_ONCE(cqe->user_data, req->user_data);
787 WRITE_ONCE(cqe->res, req->result);
788 WRITE_ONCE(cqe->flags, 0);
789 } else {
790 WRITE_ONCE(ctx->rings->cq_overflow,
791 atomic_inc_return(&ctx->cached_cq_overflow));
792 }
793 }
794
795 io_commit_cqring(ctx);
796 spin_unlock_irqrestore(&ctx->completion_lock, flags);
797 io_cqring_ev_posted(ctx);
798
799 while (!list_empty(&list)) {
800 req = list_first_entry(&list, struct io_kiocb, list);
801 list_del(&req->list);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800802 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700803 }
Jens Axboec4a2ed72019-11-21 21:01:26 -0700804
805 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700806}
807
Jens Axboe78e19bb2019-11-06 15:21:34 -0700808static void io_cqring_fill_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700809{
Jens Axboe78e19bb2019-11-06 15:21:34 -0700810 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700811 struct io_uring_cqe *cqe;
812
Jens Axboe78e19bb2019-11-06 15:21:34 -0700813 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -0700814
Jens Axboe2b188cc2019-01-07 10:46:33 -0700815 /*
816 * If we can't get a cq entry, userspace overflowed the
817 * submission (by quite a lot). Increment the overflow count in
818 * the ring.
819 */
820 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700821 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -0700822 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700823 WRITE_ONCE(cqe->res, res);
824 WRITE_ONCE(cqe->flags, 0);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700825 } else if (ctx->cq_overflow_flushed) {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700826 WRITE_ONCE(ctx->rings->cq_overflow,
827 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700828 } else {
829 refcount_inc(&req->refs);
830 req->result = res;
831 list_add_tail(&req->list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700832 }
833}
834
Jens Axboe78e19bb2019-11-06 15:21:34 -0700835static void io_cqring_add_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700836{
Jens Axboe78e19bb2019-11-06 15:21:34 -0700837 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700838 unsigned long flags;
839
840 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700841 io_cqring_fill_event(req, res);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700842 io_commit_cqring(ctx);
843 spin_unlock_irqrestore(&ctx->completion_lock, flags);
844
Jens Axboe8c838782019-03-12 15:48:16 -0600845 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700846}
847
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700848static inline bool io_is_fallback_req(struct io_kiocb *req)
849{
850 return req == (struct io_kiocb *)
851 ((unsigned long) req->ctx->fallback_req & ~1UL);
852}
853
854static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
855{
856 struct io_kiocb *req;
857
858 req = ctx->fallback_req;
859 if (!test_and_set_bit_lock(0, (unsigned long *) ctx->fallback_req))
860 return req;
861
862 return NULL;
863}
864
Jens Axboe2579f912019-01-09 09:10:43 -0700865static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
866 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700867{
Jens Axboefd6fab22019-03-14 16:30:06 -0600868 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700869 struct io_kiocb *req;
870
871 if (!percpu_ref_tryget(&ctx->refs))
872 return NULL;
873
Jens Axboe2579f912019-01-09 09:10:43 -0700874 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -0600875 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -0700876 if (unlikely(!req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700877 goto fallback;
Jens Axboe2579f912019-01-09 09:10:43 -0700878 } else if (!state->free_reqs) {
879 size_t sz;
880 int ret;
881
882 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -0600883 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
884
885 /*
886 * Bulk alloc is all-or-nothing. If we fail to get a batch,
887 * retry single alloc to be on the safe side.
888 */
889 if (unlikely(ret <= 0)) {
890 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
891 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700892 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -0600893 ret = 1;
894 }
Jens Axboe2579f912019-01-09 09:10:43 -0700895 state->free_reqs = ret - 1;
896 state->cur_req = 1;
897 req = state->reqs[0];
898 } else {
899 req = state->reqs[state->cur_req];
900 state->free_reqs--;
901 state->cur_req++;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700902 }
903
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700904got_it:
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700905 req->io = NULL;
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300906 req->ring_file = NULL;
Jens Axboe60c112b2019-06-21 10:20:18 -0600907 req->file = NULL;
Jens Axboe2579f912019-01-09 09:10:43 -0700908 req->ctx = ctx;
909 req->flags = 0;
Jens Axboee65ef562019-03-12 10:16:44 -0600910 /* one is dropped after submission, the other at completion */
911 refcount_set(&req->refs, 2);
Jens Axboe9e645e112019-05-10 16:07:28 -0600912 req->result = 0;
Jens Axboe561fb042019-10-24 07:25:42 -0600913 INIT_IO_WORK(&req->work, io_wq_submit_work);
Jens Axboe2579f912019-01-09 09:10:43 -0700914 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700915fallback:
916 req = io_get_fallback_req(ctx);
917 if (req)
918 goto got_it;
Pavel Begunkov6805b322019-10-08 02:18:42 +0300919 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700920 return NULL;
921}
922
Jens Axboedef596e2019-01-09 08:59:42 -0700923static void io_free_req_many(struct io_ring_ctx *ctx, void **reqs, int *nr)
924{
925 if (*nr) {
926 kmem_cache_free_bulk(req_cachep, *nr, reqs);
Pavel Begunkov6805b322019-10-08 02:18:42 +0300927 percpu_ref_put_many(&ctx->refs, *nr);
Jens Axboedef596e2019-01-09 08:59:42 -0700928 *nr = 0;
929 }
930}
931
Jens Axboe9e645e112019-05-10 16:07:28 -0600932static void __io_free_req(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700933{
Jens Axboefcb323c2019-10-24 12:39:47 -0600934 struct io_ring_ctx *ctx = req->ctx;
935
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700936 if (req->io)
937 kfree(req->io);
Jens Axboe09bb8392019-03-13 12:39:28 -0600938 if (req->file && !(req->flags & REQ_F_FIXED_FILE))
939 fput(req->file);
Jens Axboefcb323c2019-10-24 12:39:47 -0600940 if (req->flags & REQ_F_INFLIGHT) {
941 unsigned long flags;
942
943 spin_lock_irqsave(&ctx->inflight_lock, flags);
944 list_del(&req->inflight_entry);
945 if (waitqueue_active(&ctx->inflight_wait))
946 wake_up(&ctx->inflight_wait);
947 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
948 }
949 percpu_ref_put(&ctx->refs);
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700950 if (likely(!io_is_fallback_req(req)))
951 kmem_cache_free(req_cachep, req);
952 else
953 clear_bit_unlock(0, (unsigned long *) ctx->fallback_req);
Jens Axboee65ef562019-03-12 10:16:44 -0600954}
955
Jackie Liua197f662019-11-08 08:09:12 -0700956static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -0600957{
Jackie Liua197f662019-11-08 08:09:12 -0700958 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -0700959 int ret;
960
Jens Axboe2d283902019-12-04 11:08:05 -0700961 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe2665abf2019-11-05 12:40:47 -0700962 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -0700963 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -0700964 io_commit_cqring(ctx);
965 req->flags &= ~REQ_F_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +0800966 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -0700967 return true;
968 }
969
970 return false;
971}
972
Jens Axboeba816ad2019-09-28 11:36:45 -0600973static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -0600974{
Jens Axboe2665abf2019-11-05 12:40:47 -0700975 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -0700976 bool wake_ev = false;
Jens Axboe9e645e112019-05-10 16:07:28 -0600977
Jens Axboe4d7dd462019-11-20 13:03:52 -0700978 /* Already got next link */
979 if (req->flags & REQ_F_LINK_NEXT)
980 return;
981
Jens Axboe9e645e112019-05-10 16:07:28 -0600982 /*
983 * The list should never be empty when we are called here. But could
984 * potentially happen if the chain is messed up, check to be on the
985 * safe side.
986 */
Pavel Begunkov44932332019-12-05 16:16:35 +0300987 while (!list_empty(&req->link_list)) {
988 struct io_kiocb *nxt = list_first_entry(&req->link_list,
989 struct io_kiocb, link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700990
Pavel Begunkov44932332019-12-05 16:16:35 +0300991 if (unlikely((req->flags & REQ_F_LINK_TIMEOUT) &&
992 (nxt->flags & REQ_F_TIMEOUT))) {
993 list_del_init(&nxt->link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700994 wake_ev |= io_link_cancel_timeout(nxt);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700995 req->flags &= ~REQ_F_LINK_TIMEOUT;
996 continue;
997 }
Jens Axboe9e645e112019-05-10 16:07:28 -0600998
Pavel Begunkov44932332019-12-05 16:16:35 +0300999 list_del_init(&req->link_list);
1000 if (!list_empty(&nxt->link_list))
1001 nxt->flags |= REQ_F_LINK;
Pavel Begunkovb18fdf72019-11-21 23:21:02 +03001002 *nxtptr = nxt;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001003 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06001004 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001005
Jens Axboe4d7dd462019-11-20 13:03:52 -07001006 req->flags |= REQ_F_LINK_NEXT;
Jens Axboe2665abf2019-11-05 12:40:47 -07001007 if (wake_ev)
1008 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001009}
1010
1011/*
1012 * Called if REQ_F_LINK is set, and we fail the head request
1013 */
1014static void io_fail_links(struct io_kiocb *req)
1015{
Jens Axboe2665abf2019-11-05 12:40:47 -07001016 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001017 unsigned long flags;
1018
1019 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06001020
1021 while (!list_empty(&req->link_list)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03001022 struct io_kiocb *link = list_first_entry(&req->link_list,
1023 struct io_kiocb, link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06001024
Pavel Begunkov44932332019-12-05 16:16:35 +03001025 list_del_init(&link->link_list);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001026 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001027
1028 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
Jens Axboed625c6e2019-12-17 19:53:05 -07001029 link->opcode == IORING_OP_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -07001030 io_link_cancel_timeout(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001031 } else {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001032 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe978db572019-11-14 22:39:04 -07001033 __io_double_put_req(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001034 }
Jens Axboe5d960722019-11-19 15:31:28 -07001035 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -06001036 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001037
1038 io_commit_cqring(ctx);
1039 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1040 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001041}
1042
Jens Axboe4d7dd462019-11-20 13:03:52 -07001043static void io_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -06001044{
Jens Axboe4d7dd462019-11-20 13:03:52 -07001045 if (likely(!(req->flags & REQ_F_LINK)))
Jens Axboe2665abf2019-11-05 12:40:47 -07001046 return;
Jens Axboe2665abf2019-11-05 12:40:47 -07001047
Jens Axboe9e645e112019-05-10 16:07:28 -06001048 /*
1049 * If LINK is set, we have dependent requests in this chain. If we
1050 * didn't fail this request, queue the first one up, moving any other
1051 * dependencies to the next request. In case of failure, fail the rest
1052 * of the chain.
1053 */
Jens Axboe2665abf2019-11-05 12:40:47 -07001054 if (req->flags & REQ_F_FAIL_LINK) {
1055 io_fail_links(req);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001056 } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) ==
1057 REQ_F_LINK_TIMEOUT) {
Jens Axboe2665abf2019-11-05 12:40:47 -07001058 struct io_ring_ctx *ctx = req->ctx;
1059 unsigned long flags;
1060
1061 /*
1062 * If this is a timeout link, we could be racing with the
1063 * timeout timer. Grab the completion lock for this case to
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001064 * protect against that.
Jens Axboe2665abf2019-11-05 12:40:47 -07001065 */
1066 spin_lock_irqsave(&ctx->completion_lock, flags);
1067 io_req_link_next(req, nxt);
1068 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1069 } else {
1070 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -06001071 }
Jens Axboe4d7dd462019-11-20 13:03:52 -07001072}
Jens Axboe9e645e112019-05-10 16:07:28 -06001073
Jackie Liuc69f8db2019-11-09 11:00:08 +08001074static void io_free_req(struct io_kiocb *req)
1075{
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001076 struct io_kiocb *nxt = NULL;
1077
1078 io_req_find_next(req, &nxt);
Pavel Begunkov70cf9f32019-11-21 23:21:00 +03001079 __io_free_req(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001080
1081 if (nxt)
1082 io_queue_async_work(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08001083}
1084
Jens Axboeba816ad2019-09-28 11:36:45 -06001085/*
1086 * Drop reference to request, return next in chain (if there is one) if this
1087 * was the last reference to this request.
1088 */
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001089__attribute__((nonnull))
Jackie Liuec9c02a2019-11-08 23:50:36 +08001090static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboee65ef562019-03-12 10:16:44 -06001091{
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001092 io_req_find_next(req, nxtptr);
Jens Axboe4d7dd462019-11-20 13:03:52 -07001093
Jens Axboee65ef562019-03-12 10:16:44 -06001094 if (refcount_dec_and_test(&req->refs))
Jens Axboe4d7dd462019-11-20 13:03:52 -07001095 __io_free_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001096}
1097
Jens Axboe2b188cc2019-01-07 10:46:33 -07001098static void io_put_req(struct io_kiocb *req)
1099{
Jens Axboedef596e2019-01-09 08:59:42 -07001100 if (refcount_dec_and_test(&req->refs))
1101 io_free_req(req);
1102}
1103
Jens Axboe978db572019-11-14 22:39:04 -07001104/*
1105 * Must only be used if we don't need to care about links, usually from
1106 * within the completion handling itself.
1107 */
1108static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06001109{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001110 /* drop both submit and complete references */
1111 if (refcount_sub_and_test(2, &req->refs))
1112 __io_free_req(req);
1113}
1114
Jens Axboe978db572019-11-14 22:39:04 -07001115static void io_double_put_req(struct io_kiocb *req)
1116{
1117 /* drop both submit and complete references */
1118 if (refcount_sub_and_test(2, &req->refs))
1119 io_free_req(req);
1120}
1121
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001122static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06001123{
Jens Axboe84f97dc2019-11-06 11:27:53 -07001124 struct io_rings *rings = ctx->rings;
1125
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001126 /*
1127 * noflush == true is from the waitqueue handler, just ensure we wake
1128 * up the task, and the next invocation will flush the entries. We
1129 * cannot safely to it from here.
1130 */
1131 if (noflush && !list_empty(&ctx->cq_overflow_list))
1132 return -1U;
1133
1134 io_cqring_overflow_flush(ctx, false);
1135
Jens Axboea3a0e432019-08-20 11:03:11 -06001136 /* See comment at the top of this file */
1137 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00001138 return READ_ONCE(rings->cq.tail) - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06001139}
1140
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03001141static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1142{
1143 struct io_rings *rings = ctx->rings;
1144
1145 /* make sure SQ entry isn't read before tail */
1146 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1147}
1148
Jens Axboedef596e2019-01-09 08:59:42 -07001149/*
1150 * Find and free completed poll iocbs
1151 */
1152static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
1153 struct list_head *done)
1154{
1155 void *reqs[IO_IOPOLL_BATCH];
1156 struct io_kiocb *req;
Jens Axboe09bb8392019-03-13 12:39:28 -06001157 int to_free;
Jens Axboedef596e2019-01-09 08:59:42 -07001158
Jens Axboe09bb8392019-03-13 12:39:28 -06001159 to_free = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001160 while (!list_empty(done)) {
1161 req = list_first_entry(done, struct io_kiocb, list);
1162 list_del(&req->list);
1163
Jens Axboe78e19bb2019-11-06 15:21:34 -07001164 io_cqring_fill_event(req, req->result);
Jens Axboedef596e2019-01-09 08:59:42 -07001165 (*nr_events)++;
1166
Jens Axboe09bb8392019-03-13 12:39:28 -06001167 if (refcount_dec_and_test(&req->refs)) {
1168 /* If we're not using fixed files, we have to pair the
1169 * completion part with the file put. Use regular
1170 * completions for those, only batch free for fixed
Jens Axboe9e645e112019-05-10 16:07:28 -06001171 * file and non-linked commands.
Jens Axboe09bb8392019-03-13 12:39:28 -06001172 */
Jens Axboe1a6b74f2019-12-02 10:33:15 -07001173 if (((req->flags & (REQ_F_FIXED_FILE|REQ_F_LINK)) ==
1174 REQ_F_FIXED_FILE) && !io_is_fallback_req(req) &&
1175 !req->io) {
Jens Axboe09bb8392019-03-13 12:39:28 -06001176 reqs[to_free++] = req;
1177 if (to_free == ARRAY_SIZE(reqs))
1178 io_free_req_many(ctx, reqs, &to_free);
Jens Axboe6b063142019-01-10 22:13:58 -07001179 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001180 io_free_req(req);
Jens Axboe6b063142019-01-10 22:13:58 -07001181 }
Jens Axboe9a56a232019-01-09 09:06:50 -07001182 }
Jens Axboedef596e2019-01-09 08:59:42 -07001183 }
Jens Axboedef596e2019-01-09 08:59:42 -07001184
Jens Axboe09bb8392019-03-13 12:39:28 -06001185 io_commit_cqring(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07001186 io_free_req_many(ctx, reqs, &to_free);
1187}
1188
1189static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1190 long min)
1191{
1192 struct io_kiocb *req, *tmp;
1193 LIST_HEAD(done);
1194 bool spin;
1195 int ret;
1196
1197 /*
1198 * Only spin for completions if we don't have multiple devices hanging
1199 * off our complete list, and we're under the requested amount.
1200 */
1201 spin = !ctx->poll_multi_file && *nr_events < min;
1202
1203 ret = 0;
1204 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
Jens Axboe9adbd452019-12-20 08:45:55 -07001205 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07001206
1207 /*
1208 * Move completed entries to our local list. If we find a
1209 * request that requires polling, break out and complete
1210 * the done list first, if we have entries there.
1211 */
1212 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
1213 list_move_tail(&req->list, &done);
1214 continue;
1215 }
1216 if (!list_empty(&done))
1217 break;
1218
1219 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1220 if (ret < 0)
1221 break;
1222
1223 if (ret && spin)
1224 spin = false;
1225 ret = 0;
1226 }
1227
1228 if (!list_empty(&done))
1229 io_iopoll_complete(ctx, nr_events, &done);
1230
1231 return ret;
1232}
1233
1234/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08001235 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07001236 * non-spinning poll check - we'll still enter the driver poll loop, but only
1237 * as a non-spinning completion check.
1238 */
1239static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
1240 long min)
1241{
Jens Axboe08f54392019-08-21 22:19:11 -06001242 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07001243 int ret;
1244
1245 ret = io_do_iopoll(ctx, nr_events, min);
1246 if (ret < 0)
1247 return ret;
1248 if (!min || *nr_events >= min)
1249 return 0;
1250 }
1251
1252 return 1;
1253}
1254
1255/*
1256 * We can't just wait for polled events to come to us, we have to actively
1257 * find and complete them.
1258 */
1259static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
1260{
1261 if (!(ctx->flags & IORING_SETUP_IOPOLL))
1262 return;
1263
1264 mutex_lock(&ctx->uring_lock);
1265 while (!list_empty(&ctx->poll_list)) {
1266 unsigned int nr_events = 0;
1267
1268 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -06001269
1270 /*
1271 * Ensure we allow local-to-the-cpu processing to take place,
1272 * in this case we need to ensure that we reap all events.
1273 */
1274 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -07001275 }
1276 mutex_unlock(&ctx->uring_lock);
1277}
1278
Jens Axboe2b2ed972019-10-25 10:06:15 -06001279static int __io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1280 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07001281{
Jens Axboe2b2ed972019-10-25 10:06:15 -06001282 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001283
1284 do {
1285 int tmin = 0;
1286
Jens Axboe500f9fb2019-08-19 12:15:59 -06001287 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06001288 * Don't enter poll loop if we already have events pending.
1289 * If we do, we can potentially be spinning for commands that
1290 * already triggered a CQE (eg in error).
1291 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001292 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06001293 break;
1294
1295 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06001296 * If a submit got punted to a workqueue, we can have the
1297 * application entering polling for a command before it gets
1298 * issued. That app will hold the uring_lock for the duration
1299 * of the poll right here, so we need to take a breather every
1300 * now and then to ensure that the issue has a chance to add
1301 * the poll to the issued list. Otherwise we can spin here
1302 * forever, while the workqueue is stuck trying to acquire the
1303 * very same mutex.
1304 */
1305 if (!(++iters & 7)) {
1306 mutex_unlock(&ctx->uring_lock);
1307 mutex_lock(&ctx->uring_lock);
1308 }
1309
Jens Axboedef596e2019-01-09 08:59:42 -07001310 if (*nr_events < min)
1311 tmin = min - *nr_events;
1312
1313 ret = io_iopoll_getevents(ctx, nr_events, tmin);
1314 if (ret <= 0)
1315 break;
1316 ret = 0;
1317 } while (min && !*nr_events && !need_resched());
1318
Jens Axboe2b2ed972019-10-25 10:06:15 -06001319 return ret;
1320}
1321
1322static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1323 long min)
1324{
1325 int ret;
1326
1327 /*
1328 * We disallow the app entering submit/complete with polling, but we
1329 * still need to lock the ring to prevent racing with polled issue
1330 * that got punted to a workqueue.
1331 */
1332 mutex_lock(&ctx->uring_lock);
1333 ret = __io_iopoll_check(ctx, nr_events, min);
Jens Axboe500f9fb2019-08-19 12:15:59 -06001334 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001335 return ret;
1336}
1337
Jens Axboe491381ce2019-10-17 09:20:46 -06001338static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001339{
Jens Axboe491381ce2019-10-17 09:20:46 -06001340 /*
1341 * Tell lockdep we inherited freeze protection from submission
1342 * thread.
1343 */
1344 if (req->flags & REQ_F_ISREG) {
1345 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001346
Jens Axboe491381ce2019-10-17 09:20:46 -06001347 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001348 }
Jens Axboe491381ce2019-10-17 09:20:46 -06001349 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001350}
1351
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001352static inline void req_set_fail_links(struct io_kiocb *req)
1353{
1354 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1355 req->flags |= REQ_F_FAIL_LINK;
1356}
1357
Jens Axboeba816ad2019-09-28 11:36:45 -06001358static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001359{
Jens Axboe9adbd452019-12-20 08:45:55 -07001360 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001361
Jens Axboe491381ce2019-10-17 09:20:46 -06001362 if (kiocb->ki_flags & IOCB_WRITE)
1363 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001364
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001365 if (res != req->result)
1366 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001367 io_cqring_add_event(req, res);
Jens Axboeba816ad2019-09-28 11:36:45 -06001368}
1369
1370static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1371{
Jens Axboe9adbd452019-12-20 08:45:55 -07001372 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06001373
1374 io_complete_rw_common(kiocb, res);
Jens Axboee65ef562019-03-12 10:16:44 -06001375 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001376}
1377
Jens Axboeba816ad2019-09-28 11:36:45 -06001378static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res)
1379{
Jens Axboe9adbd452019-12-20 08:45:55 -07001380 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001381 struct io_kiocb *nxt = NULL;
Jens Axboeba816ad2019-09-28 11:36:45 -06001382
1383 io_complete_rw_common(kiocb, res);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001384 io_put_req_find_next(req, &nxt);
1385
1386 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001387}
1388
Jens Axboedef596e2019-01-09 08:59:42 -07001389static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1390{
Jens Axboe9adbd452019-12-20 08:45:55 -07001391 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07001392
Jens Axboe491381ce2019-10-17 09:20:46 -06001393 if (kiocb->ki_flags & IOCB_WRITE)
1394 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001395
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001396 if (res != req->result)
1397 req_set_fail_links(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06001398 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -07001399 if (res != -EAGAIN)
1400 req->flags |= REQ_F_IOPOLL_COMPLETED;
1401}
1402
1403/*
1404 * After the iocb has been issued, it's safe to be found on the poll list.
1405 * Adding the kiocb to the list AFTER submission ensures that we don't
1406 * find it from a io_iopoll_getevents() thread before the issuer is done
1407 * accessing the kiocb cookie.
1408 */
1409static void io_iopoll_req_issued(struct io_kiocb *req)
1410{
1411 struct io_ring_ctx *ctx = req->ctx;
1412
1413 /*
1414 * Track whether we have multiple files in our lists. This will impact
1415 * how we do polling eventually, not spinning if we're on potentially
1416 * different devices.
1417 */
1418 if (list_empty(&ctx->poll_list)) {
1419 ctx->poll_multi_file = false;
1420 } else if (!ctx->poll_multi_file) {
1421 struct io_kiocb *list_req;
1422
1423 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1424 list);
Jens Axboe9adbd452019-12-20 08:45:55 -07001425 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07001426 ctx->poll_multi_file = true;
1427 }
1428
1429 /*
1430 * For fast devices, IO may have already completed. If it has, add
1431 * it to the front so we find it first.
1432 */
1433 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1434 list_add(&req->list, &ctx->poll_list);
1435 else
1436 list_add_tail(&req->list, &ctx->poll_list);
1437}
1438
Jens Axboe3d6770f2019-04-13 11:50:54 -06001439static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07001440{
Jens Axboe3d6770f2019-04-13 11:50:54 -06001441 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -07001442 int diff = state->has_refs - state->used_refs;
1443
1444 if (diff)
1445 fput_many(state->file, diff);
1446 state->file = NULL;
1447 }
1448}
1449
1450/*
1451 * Get as many references to a file as we have IOs left in this submission,
1452 * assuming most submissions are for one file, or at least that each file
1453 * has more than one submission.
1454 */
1455static struct file *io_file_get(struct io_submit_state *state, int fd)
1456{
1457 if (!state)
1458 return fget(fd);
1459
1460 if (state->file) {
1461 if (state->fd == fd) {
1462 state->used_refs++;
1463 state->ios_left--;
1464 return state->file;
1465 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06001466 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07001467 }
1468 state->file = fget_many(fd, state->ios_left);
1469 if (!state->file)
1470 return NULL;
1471
1472 state->fd = fd;
1473 state->has_refs = state->ios_left;
1474 state->used_refs = 1;
1475 state->ios_left--;
1476 return state->file;
1477}
1478
Jens Axboe2b188cc2019-01-07 10:46:33 -07001479/*
1480 * If we tracked the file through the SCM inflight mechanism, we could support
1481 * any file. For now, just ensure that anything potentially problematic is done
1482 * inline.
1483 */
1484static bool io_file_supports_async(struct file *file)
1485{
1486 umode_t mode = file_inode(file)->i_mode;
1487
Jens Axboe10d59342019-12-09 20:16:22 -07001488 if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07001489 return true;
1490 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
1491 return true;
1492
1493 return false;
1494}
1495
Pavel Begunkov267bc902019-11-07 01:41:08 +03001496static int io_prep_rw(struct io_kiocb *req, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001497{
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001498 const struct io_uring_sqe *sqe = req->sqe;
Jens Axboedef596e2019-01-09 08:59:42 -07001499 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07001500 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06001501 unsigned ioprio;
1502 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001503
Jens Axboe09bb8392019-03-13 12:39:28 -06001504 if (!req->file)
1505 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001506
Jens Axboe491381ce2019-10-17 09:20:46 -06001507 if (S_ISREG(file_inode(req->file)->i_mode))
1508 req->flags |= REQ_F_ISREG;
1509
Jens Axboe2b188cc2019-01-07 10:46:33 -07001510 kiocb->ki_pos = READ_ONCE(sqe->off);
1511 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
1512 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
1513
1514 ioprio = READ_ONCE(sqe->ioprio);
1515 if (ioprio) {
1516 ret = ioprio_check_cap(ioprio);
1517 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06001518 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001519
1520 kiocb->ki_ioprio = ioprio;
1521 } else
1522 kiocb->ki_ioprio = get_current_ioprio();
1523
1524 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
1525 if (unlikely(ret))
Jens Axboe09bb8392019-03-13 12:39:28 -06001526 return ret;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001527
1528 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06001529 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
1530 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02001531 req->flags |= REQ_F_NOWAIT;
1532
1533 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001534 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001535
Jens Axboedef596e2019-01-09 08:59:42 -07001536 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07001537 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
1538 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06001539 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001540
Jens Axboedef596e2019-01-09 08:59:42 -07001541 kiocb->ki_flags |= IOCB_HIPRI;
1542 kiocb->ki_complete = io_complete_rw_iopoll;
Jens Axboe6873e0b2019-10-30 13:53:09 -06001543 req->result = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001544 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001545 if (kiocb->ki_flags & IOCB_HIPRI)
1546 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07001547 kiocb->ki_complete = io_complete_rw;
1548 }
Jens Axboe9adbd452019-12-20 08:45:55 -07001549
1550 req->rw.addr = READ_ONCE(req->sqe->addr);
1551 req->rw.len = READ_ONCE(req->sqe->len);
1552 /* we own ->private, reuse it for the buffer index */
1553 req->rw.kiocb.private = (void *) (unsigned long)
1554 READ_ONCE(req->sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001555 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001556}
1557
1558static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
1559{
1560 switch (ret) {
1561 case -EIOCBQUEUED:
1562 break;
1563 case -ERESTARTSYS:
1564 case -ERESTARTNOINTR:
1565 case -ERESTARTNOHAND:
1566 case -ERESTART_RESTARTBLOCK:
1567 /*
1568 * We can't just restart the syscall, since previously
1569 * submitted sqes may already be in progress. Just fail this
1570 * IO with EINTR.
1571 */
1572 ret = -EINTR;
1573 /* fall through */
1574 default:
1575 kiocb->ki_complete(kiocb, ret, 0);
1576 }
1577}
1578
Jens Axboeba816ad2019-09-28 11:36:45 -06001579static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt,
1580 bool in_async)
1581{
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001582 if (in_async && ret >= 0 && kiocb->ki_complete == io_complete_rw)
Jens Axboeba816ad2019-09-28 11:36:45 -06001583 *nxt = __io_complete_rw(kiocb, ret);
1584 else
1585 io_rw_done(kiocb, ret);
1586}
1587
Jens Axboe9adbd452019-12-20 08:45:55 -07001588static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03001589 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07001590{
Jens Axboe9adbd452019-12-20 08:45:55 -07001591 struct io_ring_ctx *ctx = req->ctx;
1592 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001593 struct io_mapped_ubuf *imu;
1594 unsigned index, buf_index;
1595 size_t offset;
1596 u64 buf_addr;
1597
1598 /* attempt to use fixed buffers without having provided iovecs */
1599 if (unlikely(!ctx->user_bufs))
1600 return -EFAULT;
1601
Jens Axboe9adbd452019-12-20 08:45:55 -07001602 buf_index = (unsigned long) req->rw.kiocb.private;
Jens Axboeedafcce2019-01-09 09:16:05 -07001603 if (unlikely(buf_index >= ctx->nr_user_bufs))
1604 return -EFAULT;
1605
1606 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
1607 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07001608 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07001609
1610 /* overflow */
1611 if (buf_addr + len < buf_addr)
1612 return -EFAULT;
1613 /* not inside the mapped region */
1614 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
1615 return -EFAULT;
1616
1617 /*
1618 * May not be a start of buffer, set size appropriately
1619 * and advance us to the beginning.
1620 */
1621 offset = buf_addr - imu->ubuf;
1622 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06001623
1624 if (offset) {
1625 /*
1626 * Don't use iov_iter_advance() here, as it's really slow for
1627 * using the latter parts of a big fixed buffer - it iterates
1628 * over each segment manually. We can cheat a bit here, because
1629 * we know that:
1630 *
1631 * 1) it's a BVEC iter, we set it up
1632 * 2) all bvecs are PAGE_SIZE in size, except potentially the
1633 * first and last bvec
1634 *
1635 * So just find our index, and adjust the iterator afterwards.
1636 * If the offset is within the first bvec (or the whole first
1637 * bvec, just use iov_iter_advance(). This makes it easier
1638 * since we can just skip the first segment, which may not
1639 * be PAGE_SIZE aligned.
1640 */
1641 const struct bio_vec *bvec = imu->bvec;
1642
1643 if (offset <= bvec->bv_len) {
1644 iov_iter_advance(iter, offset);
1645 } else {
1646 unsigned long seg_skip;
1647
1648 /* skip first vec */
1649 offset -= bvec->bv_len;
1650 seg_skip = 1 + (offset >> PAGE_SHIFT);
1651
1652 iter->bvec = bvec + seg_skip;
1653 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02001654 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001655 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001656 }
1657 }
1658
Jens Axboe5e559562019-11-13 16:12:46 -07001659 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001660}
1661
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001662static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
1663 struct iovec **iovec, struct iov_iter *iter)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001664{
Jens Axboe9adbd452019-12-20 08:45:55 -07001665 void __user *buf = u64_to_user_ptr(req->rw.addr);
1666 size_t sqe_len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001667 u8 opcode;
1668
Jens Axboed625c6e2019-12-17 19:53:05 -07001669 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03001670 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07001671 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07001672 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07001673 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001674
Jens Axboe9adbd452019-12-20 08:45:55 -07001675 /* buffer index only valid with fixed read/write */
1676 if (req->rw.kiocb.private)
1677 return -EINVAL;
1678
Jens Axboef67676d2019-12-02 11:03:47 -07001679 if (req->io) {
1680 struct io_async_rw *iorw = &req->io->rw;
1681
1682 *iovec = iorw->iov;
1683 iov_iter_init(iter, rw, *iovec, iorw->nr_segs, iorw->size);
1684 if (iorw->iov == iorw->fast_iov)
1685 *iovec = NULL;
1686 return iorw->size;
1687 }
1688
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001689 if (!req->has_user)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001690 return -EFAULT;
1691
1692#ifdef CONFIG_COMPAT
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001693 if (req->ctx->compat)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001694 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
1695 iovec, iter);
1696#endif
1697
1698 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
1699}
1700
Jens Axboe32960612019-09-23 11:05:34 -06001701/*
1702 * For files that don't have ->read_iter() and ->write_iter(), handle them
1703 * by looping over ->read() or ->write() manually.
1704 */
1705static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
1706 struct iov_iter *iter)
1707{
1708 ssize_t ret = 0;
1709
1710 /*
1711 * Don't support polled IO through this interface, and we can't
1712 * support non-blocking either. For the latter, this just causes
1713 * the kiocb to be handled from an async context.
1714 */
1715 if (kiocb->ki_flags & IOCB_HIPRI)
1716 return -EOPNOTSUPP;
1717 if (kiocb->ki_flags & IOCB_NOWAIT)
1718 return -EAGAIN;
1719
1720 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03001721 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06001722 ssize_t nr;
1723
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03001724 if (!iov_iter_is_bvec(iter)) {
1725 iovec = iov_iter_iovec(iter);
1726 } else {
1727 /* fixed buffers import bvec */
1728 iovec.iov_base = kmap(iter->bvec->bv_page)
1729 + iter->iov_offset;
1730 iovec.iov_len = min(iter->count,
1731 iter->bvec->bv_len - iter->iov_offset);
1732 }
1733
Jens Axboe32960612019-09-23 11:05:34 -06001734 if (rw == READ) {
1735 nr = file->f_op->read(file, iovec.iov_base,
1736 iovec.iov_len, &kiocb->ki_pos);
1737 } else {
1738 nr = file->f_op->write(file, iovec.iov_base,
1739 iovec.iov_len, &kiocb->ki_pos);
1740 }
1741
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03001742 if (iov_iter_is_bvec(iter))
1743 kunmap(iter->bvec->bv_page);
1744
Jens Axboe32960612019-09-23 11:05:34 -06001745 if (nr < 0) {
1746 if (!ret)
1747 ret = nr;
1748 break;
1749 }
1750 ret += nr;
1751 if (nr != iovec.iov_len)
1752 break;
1753 iov_iter_advance(iter, nr);
1754 }
1755
1756 return ret;
1757}
1758
Jens Axboeb7bb4f72019-12-15 22:13:43 -07001759static void io_req_map_rw(struct io_kiocb *req, ssize_t io_size,
Jens Axboef67676d2019-12-02 11:03:47 -07001760 struct iovec *iovec, struct iovec *fast_iov,
1761 struct iov_iter *iter)
1762{
1763 req->io->rw.nr_segs = iter->nr_segs;
1764 req->io->rw.size = io_size;
1765 req->io->rw.iov = iovec;
1766 if (!req->io->rw.iov) {
1767 req->io->rw.iov = req->io->rw.fast_iov;
1768 memcpy(req->io->rw.iov, fast_iov,
1769 sizeof(struct iovec) * iter->nr_segs);
1770 }
1771}
1772
Jens Axboeb7bb4f72019-12-15 22:13:43 -07001773static int io_alloc_async_ctx(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07001774{
1775 req->io = kmalloc(sizeof(*req->io), GFP_KERNEL);
1776 if (req->io) {
Jens Axboef67676d2019-12-02 11:03:47 -07001777 memcpy(&req->io->sqe, req->sqe, sizeof(req->io->sqe));
1778 req->sqe = &req->io->sqe;
1779 return 0;
1780 }
1781
Jens Axboeb7bb4f72019-12-15 22:13:43 -07001782 return 1;
1783}
1784
1785static void io_rw_async(struct io_wq_work **workptr)
1786{
1787 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
1788 struct iovec *iov = NULL;
1789
1790 if (req->io->rw.iov != req->io->rw.fast_iov)
1791 iov = req->io->rw.iov;
1792 io_wq_submit_work(workptr);
1793 kfree(iov);
1794}
1795
1796static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size,
1797 struct iovec *iovec, struct iovec *fast_iov,
1798 struct iov_iter *iter)
1799{
1800 if (!req->io && io_alloc_async_ctx(req))
1801 return -ENOMEM;
1802
1803 io_req_map_rw(req, io_size, iovec, fast_iov, iter);
1804 req->work.func = io_rw_async;
1805 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07001806}
1807
1808static int io_read_prep(struct io_kiocb *req, struct iovec **iovec,
1809 struct iov_iter *iter, bool force_nonblock)
1810{
1811 ssize_t ret;
1812
1813 ret = io_prep_rw(req, force_nonblock);
1814 if (ret)
1815 return ret;
1816
1817 if (unlikely(!(req->file->f_mode & FMODE_READ)))
1818 return -EBADF;
1819
1820 return io_import_iovec(READ, req, iovec, iter);
1821}
1822
Pavel Begunkov267bc902019-11-07 01:41:08 +03001823static int io_read(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001824 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001825{
1826 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07001827 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001828 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07001829 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07001830 ssize_t io_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001831
Jens Axboef67676d2019-12-02 11:03:47 -07001832 if (!req->io) {
1833 ret = io_read_prep(req, &iovec, &iter, force_nonblock);
1834 if (ret < 0)
1835 return ret;
1836 } else {
1837 ret = io_import_iovec(READ, req, &iovec, &iter);
1838 if (ret < 0)
1839 return ret;
1840 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001841
Jens Axboefd6c2e42019-12-18 12:19:41 -07001842 /* Ensure we clear previously set non-block flag */
1843 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07001844 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07001845
Jens Axboef67676d2019-12-02 11:03:47 -07001846 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06001847 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07001848 req->result = io_size;
1849
1850 /*
1851 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
1852 * we know to async punt it even if it was opened O_NONBLOCK
1853 */
Jens Axboe9adbd452019-12-20 08:45:55 -07001854 if (force_nonblock && !io_file_supports_async(req->file)) {
Jens Axboef67676d2019-12-02 11:03:47 -07001855 req->flags |= REQ_F_MUST_PUNT;
1856 goto copy_iov;
1857 }
Jens Axboe9e645e112019-05-10 16:07:28 -06001858
Jens Axboe31b51512019-01-18 22:56:34 -07001859 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07001860 ret = rw_verify_area(READ, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001861 if (!ret) {
1862 ssize_t ret2;
1863
Jens Axboe9adbd452019-12-20 08:45:55 -07001864 if (req->file->f_op->read_iter)
1865 ret2 = call_read_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06001866 else
Jens Axboe9adbd452019-12-20 08:45:55 -07001867 ret2 = loop_rw_iter(READ, req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06001868
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001869 /*
1870 * In case of a short read, punt to async. This can happen
1871 * if we have data partially cached. Alternatively we can
1872 * return the short read, in which case the application will
1873 * need to issue another SQE and wait for it. That SQE will
1874 * need async punt anyway, so it's more efficient to do it
1875 * here.
1876 */
Jens Axboe491381ce2019-10-17 09:20:46 -06001877 if (force_nonblock && !(req->flags & REQ_F_NOWAIT) &&
1878 (req->flags & REQ_F_ISREG) &&
Jens Axboef67676d2019-12-02 11:03:47 -07001879 ret2 > 0 && ret2 < io_size)
Jens Axboe9d93a3f2019-05-15 13:53:07 -06001880 ret2 = -EAGAIN;
1881 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboef67676d2019-12-02 11:03:47 -07001882 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001883 kiocb_done(kiocb, ret2, nxt, req->in_async);
Jens Axboef67676d2019-12-02 11:03:47 -07001884 } else {
1885copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07001886 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07001887 inline_vecs, &iter);
1888 if (ret)
1889 goto out_free;
1890 return -EAGAIN;
1891 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001892 }
Jens Axboef67676d2019-12-02 11:03:47 -07001893out_free:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07001894 if (!io_wq_current_is_worker())
1895 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001896 return ret;
1897}
1898
Jens Axboef67676d2019-12-02 11:03:47 -07001899static int io_write_prep(struct io_kiocb *req, struct iovec **iovec,
1900 struct iov_iter *iter, bool force_nonblock)
1901{
1902 ssize_t ret;
1903
1904 ret = io_prep_rw(req, force_nonblock);
1905 if (ret)
1906 return ret;
1907
1908 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
1909 return -EBADF;
1910
1911 return io_import_iovec(WRITE, req, iovec, iter);
1912}
1913
Pavel Begunkov267bc902019-11-07 01:41:08 +03001914static int io_write(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06001915 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001916{
1917 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07001918 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001919 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07001920 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07001921 ssize_t ret, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001922
Jens Axboef67676d2019-12-02 11:03:47 -07001923 if (!req->io) {
1924 ret = io_write_prep(req, &iovec, &iter, force_nonblock);
1925 if (ret < 0)
1926 return ret;
1927 } else {
1928 ret = io_import_iovec(WRITE, req, &iovec, &iter);
1929 if (ret < 0)
1930 return ret;
1931 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001932
Jens Axboefd6c2e42019-12-18 12:19:41 -07001933 /* Ensure we clear previously set non-block flag */
1934 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07001935 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07001936
Jens Axboef67676d2019-12-02 11:03:47 -07001937 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06001938 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07001939 req->result = io_size;
1940
1941 /*
1942 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
1943 * we know to async punt it even if it was opened O_NONBLOCK
1944 */
1945 if (force_nonblock && !io_file_supports_async(req->file)) {
1946 req->flags |= REQ_F_MUST_PUNT;
1947 goto copy_iov;
1948 }
1949
Jens Axboe10d59342019-12-09 20:16:22 -07001950 /* file path doesn't support NOWAIT for non-direct_IO */
1951 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
1952 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07001953 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06001954
Jens Axboe31b51512019-01-18 22:56:34 -07001955 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07001956 ret = rw_verify_area(WRITE, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001957 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01001958 ssize_t ret2;
1959
Jens Axboe2b188cc2019-01-07 10:46:33 -07001960 /*
1961 * Open-code file_start_write here to grab freeze protection,
1962 * which will be released by another thread in
1963 * io_complete_rw(). Fool lockdep by telling it the lock got
1964 * released so that it doesn't complain about the held lock when
1965 * we return to userspace.
1966 */
Jens Axboe491381ce2019-10-17 09:20:46 -06001967 if (req->flags & REQ_F_ISREG) {
Jens Axboe9adbd452019-12-20 08:45:55 -07001968 __sb_start_write(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07001969 SB_FREEZE_WRITE, true);
Jens Axboe9adbd452019-12-20 08:45:55 -07001970 __sb_writers_release(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07001971 SB_FREEZE_WRITE);
1972 }
1973 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01001974
Jens Axboe9adbd452019-12-20 08:45:55 -07001975 if (req->file->f_op->write_iter)
1976 ret2 = call_write_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06001977 else
Jens Axboe9adbd452019-12-20 08:45:55 -07001978 ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter);
Jens Axboef67676d2019-12-02 11:03:47 -07001979 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001980 kiocb_done(kiocb, ret2, nxt, req->in_async);
Jens Axboef67676d2019-12-02 11:03:47 -07001981 } else {
1982copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07001983 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07001984 inline_vecs, &iter);
1985 if (ret)
1986 goto out_free;
1987 return -EAGAIN;
1988 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001989 }
Jens Axboe31b51512019-01-18 22:56:34 -07001990out_free:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07001991 if (!io_wq_current_is_worker())
1992 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001993 return ret;
1994}
1995
1996/*
1997 * IORING_OP_NOP just posts a completion event, nothing else.
1998 */
Jens Axboe78e19bb2019-11-06 15:21:34 -07001999static int io_nop(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002000{
2001 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002002
Jens Axboedef596e2019-01-09 08:59:42 -07002003 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2004 return -EINVAL;
2005
Jens Axboe78e19bb2019-11-06 15:21:34 -07002006 io_cqring_add_event(req, 0);
Jens Axboee65ef562019-03-12 10:16:44 -06002007 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002008 return 0;
2009}
2010
Jens Axboefc4df992019-12-10 14:38:45 -07002011static int io_prep_fsync(struct io_kiocb *req)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002012{
Jens Axboefc4df992019-12-10 14:38:45 -07002013 const struct io_uring_sqe *sqe = req->sqe;
Jens Axboe6b063142019-01-10 22:13:58 -07002014 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002015
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002016 if (req->flags & REQ_F_PREPPED)
2017 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06002018 if (!req->file)
2019 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002020
Jens Axboe6b063142019-01-10 22:13:58 -07002021 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07002022 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07002023 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002024 return -EINVAL;
2025
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002026 req->sync.flags = READ_ONCE(sqe->fsync_flags);
2027 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
2028 return -EINVAL;
2029
2030 req->sync.off = READ_ONCE(sqe->off);
2031 req->sync.len = READ_ONCE(sqe->len);
2032 req->flags |= REQ_F_PREPPED;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002033 return 0;
2034}
2035
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002036static bool io_req_cancelled(struct io_kiocb *req)
2037{
2038 if (req->work.flags & IO_WQ_WORK_CANCEL) {
2039 req_set_fail_links(req);
2040 io_cqring_add_event(req, -ECANCELED);
2041 io_put_req(req);
2042 return true;
2043 }
2044
2045 return false;
2046}
2047
2048static void io_fsync_finish(struct io_wq_work **workptr)
2049{
2050 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2051 loff_t end = req->sync.off + req->sync.len;
2052 struct io_kiocb *nxt = NULL;
2053 int ret;
2054
2055 if (io_req_cancelled(req))
2056 return;
2057
Jens Axboe9adbd452019-12-20 08:45:55 -07002058 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002059 end > 0 ? end : LLONG_MAX,
2060 req->sync.flags & IORING_FSYNC_DATASYNC);
2061 if (ret < 0)
2062 req_set_fail_links(req);
2063 io_cqring_add_event(req, ret);
2064 io_put_req_find_next(req, &nxt);
2065 if (nxt)
2066 *workptr = &nxt->work;
2067}
2068
Jens Axboefc4df992019-12-10 14:38:45 -07002069static int io_fsync(struct io_kiocb *req, struct io_kiocb **nxt,
2070 bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002071{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002072 struct io_wq_work *work, *old_work;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002073 int ret;
2074
Jens Axboefc4df992019-12-10 14:38:45 -07002075 ret = io_prep_fsync(req);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002076 if (ret)
2077 return ret;
2078
2079 /* fsync always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002080 if (force_nonblock) {
2081 io_put_req(req);
2082 req->work.func = io_fsync_finish;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002083 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002084 }
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002085
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002086 work = old_work = &req->work;
2087 io_fsync_finish(&work);
2088 if (work && work != old_work)
2089 *nxt = container_of(work, struct io_kiocb, work);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002090 return 0;
2091}
2092
Jens Axboefc4df992019-12-10 14:38:45 -07002093static int io_prep_sfr(struct io_kiocb *req)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002094{
Jens Axboefc4df992019-12-10 14:38:45 -07002095 const struct io_uring_sqe *sqe = req->sqe;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002096 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002097
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002098 if (req->flags & REQ_F_PREPPED)
2099 return 0;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002100 if (!req->file)
2101 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002102
2103 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2104 return -EINVAL;
2105 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
2106 return -EINVAL;
2107
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002108 req->sync.off = READ_ONCE(sqe->off);
2109 req->sync.len = READ_ONCE(sqe->len);
2110 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
2111 req->flags |= REQ_F_PREPPED;
2112 return 0;
2113}
2114
2115static void io_sync_file_range_finish(struct io_wq_work **workptr)
2116{
2117 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2118 struct io_kiocb *nxt = NULL;
2119 int ret;
2120
2121 if (io_req_cancelled(req))
2122 return;
2123
Jens Axboe9adbd452019-12-20 08:45:55 -07002124 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002125 req->sync.flags);
2126 if (ret < 0)
2127 req_set_fail_links(req);
2128 io_cqring_add_event(req, ret);
2129 io_put_req_find_next(req, &nxt);
2130 if (nxt)
2131 *workptr = &nxt->work;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002132}
2133
Jens Axboefc4df992019-12-10 14:38:45 -07002134static int io_sync_file_range(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002135 bool force_nonblock)
2136{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002137 struct io_wq_work *work, *old_work;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002138 int ret;
2139
Jens Axboefc4df992019-12-10 14:38:45 -07002140 ret = io_prep_sfr(req);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002141 if (ret)
2142 return ret;
2143
2144 /* sync_file_range always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002145 if (force_nonblock) {
2146 io_put_req(req);
2147 req->work.func = io_sync_file_range_finish;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002148 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002149 }
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002150
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002151 work = old_work = &req->work;
2152 io_sync_file_range_finish(&work);
2153 if (work && work != old_work)
2154 *nxt = container_of(work, struct io_kiocb, work);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002155 return 0;
2156}
2157
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002158#if defined(CONFIG_NET)
2159static void io_sendrecv_async(struct io_wq_work **workptr)
2160{
2161 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2162 struct iovec *iov = NULL;
2163
2164 if (req->io->rw.iov != req->io->rw.fast_iov)
2165 iov = req->io->msg.iov;
2166 io_wq_submit_work(workptr);
2167 kfree(iov);
2168}
2169#endif
2170
Jens Axboe03b12302019-12-02 18:50:25 -07002171static int io_sendmsg_prep(struct io_kiocb *req, struct io_async_ctx *io)
Jens Axboeaa1fa282019-04-19 13:38:09 -06002172{
Jens Axboe03b12302019-12-02 18:50:25 -07002173#if defined(CONFIG_NET)
2174 const struct io_uring_sqe *sqe = req->sqe;
Jens Axboee47293f2019-12-20 08:58:21 -07002175 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe03b12302019-12-02 18:50:25 -07002176
Jens Axboee47293f2019-12-20 08:58:21 -07002177 sr->msg_flags = READ_ONCE(sqe->msg_flags);
2178 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboed9688562019-12-09 19:35:20 -07002179 io->msg.iov = io->msg.fast_iov;
Jens Axboee47293f2019-12-20 08:58:21 -07002180 return sendmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
2181 &io->msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002182#else
Jens Axboee47293f2019-12-20 08:58:21 -07002183 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07002184#endif
2185}
2186
Jens Axboefc4df992019-12-10 14:38:45 -07002187static int io_sendmsg(struct io_kiocb *req, struct io_kiocb **nxt,
2188 bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07002189{
2190#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07002191 struct io_async_msghdr *kmsg = NULL;
Jens Axboe03b12302019-12-02 18:50:25 -07002192 struct socket *sock;
2193 int ret;
2194
2195 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2196 return -EINVAL;
2197
2198 sock = sock_from_file(req->file, &ret);
2199 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002200 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07002201 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -07002202 unsigned flags;
2203
Jens Axboe03b12302019-12-02 18:50:25 -07002204 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07002205 kmsg = &req->io->msg;
2206 kmsg->msg.msg_name = &addr;
2207 /* if iov is set, it's allocated already */
2208 if (!kmsg->iov)
2209 kmsg->iov = kmsg->fast_iov;
2210 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07002211 } else {
Jens Axboe0b416c32019-12-15 10:57:46 -07002212 kmsg = &io.msg;
2213 kmsg->msg.msg_name = &addr;
Jens Axboe03b12302019-12-02 18:50:25 -07002214 ret = io_sendmsg_prep(req, &io);
2215 if (ret)
2216 goto out;
2217 }
2218
Jens Axboee47293f2019-12-20 08:58:21 -07002219 flags = req->sr_msg.msg_flags;
2220 if (flags & MSG_DONTWAIT)
2221 req->flags |= REQ_F_NOWAIT;
2222 else if (force_nonblock)
2223 flags |= MSG_DONTWAIT;
2224
Jens Axboe0b416c32019-12-15 10:57:46 -07002225 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Jens Axboe03b12302019-12-02 18:50:25 -07002226 if (force_nonblock && ret == -EAGAIN) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002227 if (req->io)
2228 return -EAGAIN;
2229 if (io_alloc_async_ctx(req))
2230 return -ENOMEM;
2231 memcpy(&req->io->msg, &io.msg, sizeof(io.msg));
2232 req->work.func = io_sendrecv_async;
Jens Axboe0b416c32019-12-15 10:57:46 -07002233 return -EAGAIN;
Jens Axboe03b12302019-12-02 18:50:25 -07002234 }
2235 if (ret == -ERESTARTSYS)
2236 ret = -EINTR;
2237 }
2238
2239out:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002240 if (!io_wq_current_is_worker() && kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07002241 kfree(kmsg->iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002242 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002243 if (ret < 0)
2244 req_set_fail_links(req);
Jens Axboe03b12302019-12-02 18:50:25 -07002245 io_put_req_find_next(req, nxt);
2246 return 0;
2247#else
2248 return -EOPNOTSUPP;
2249#endif
2250}
2251
2252static int io_recvmsg_prep(struct io_kiocb *req, struct io_async_ctx *io)
2253{
2254#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07002255 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe03b12302019-12-02 18:50:25 -07002256
Jens Axboee47293f2019-12-20 08:58:21 -07002257 sr->msg_flags = READ_ONCE(req->sqe->msg_flags);
2258 sr->msg = u64_to_user_ptr(READ_ONCE(req->sqe->addr));
Jens Axboed9688562019-12-09 19:35:20 -07002259 io->msg.iov = io->msg.fast_iov;
Jens Axboee47293f2019-12-20 08:58:21 -07002260 return recvmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
2261 &io->msg.uaddr, &io->msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002262#else
Jens Axboee47293f2019-12-20 08:58:21 -07002263 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07002264#endif
2265}
2266
Jens Axboefc4df992019-12-10 14:38:45 -07002267static int io_recvmsg(struct io_kiocb *req, struct io_kiocb **nxt,
2268 bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07002269{
2270#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07002271 struct io_async_msghdr *kmsg = NULL;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002272 struct socket *sock;
2273 int ret;
2274
2275 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2276 return -EINVAL;
2277
2278 sock = sock_from_file(req->file, &ret);
2279 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002280 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07002281 struct sockaddr_storage addr;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002282 unsigned flags;
2283
Jens Axboe03b12302019-12-02 18:50:25 -07002284 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07002285 kmsg = &req->io->msg;
2286 kmsg->msg.msg_name = &addr;
2287 /* if iov is set, it's allocated already */
2288 if (!kmsg->iov)
2289 kmsg->iov = kmsg->fast_iov;
2290 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07002291 } else {
Jens Axboe0b416c32019-12-15 10:57:46 -07002292 kmsg = &io.msg;
2293 kmsg->msg.msg_name = &addr;
Jens Axboe03b12302019-12-02 18:50:25 -07002294 ret = io_recvmsg_prep(req, &io);
2295 if (ret)
2296 goto out;
2297 }
Jens Axboe0fa03c62019-04-19 13:34:07 -06002298
Jens Axboee47293f2019-12-20 08:58:21 -07002299 flags = req->sr_msg.msg_flags;
2300 if (flags & MSG_DONTWAIT)
2301 req->flags |= REQ_F_NOWAIT;
2302 else if (force_nonblock)
2303 flags |= MSG_DONTWAIT;
2304
2305 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.msg,
2306 kmsg->uaddr, flags);
Jens Axboe03b12302019-12-02 18:50:25 -07002307 if (force_nonblock && ret == -EAGAIN) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002308 if (req->io)
2309 return -EAGAIN;
2310 if (io_alloc_async_ctx(req))
2311 return -ENOMEM;
2312 memcpy(&req->io->msg, &io.msg, sizeof(io.msg));
2313 req->work.func = io_sendrecv_async;
Jens Axboe0b416c32019-12-15 10:57:46 -07002314 return -EAGAIN;
Jens Axboe03b12302019-12-02 18:50:25 -07002315 }
Jens Axboe441cdbd2019-12-02 18:49:10 -07002316 if (ret == -ERESTARTSYS)
2317 ret = -EINTR;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002318 }
2319
Jens Axboe03b12302019-12-02 18:50:25 -07002320out:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002321 if (!io_wq_current_is_worker() && kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07002322 kfree(kmsg->iov);
Jens Axboe78e19bb2019-11-06 15:21:34 -07002323 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002324 if (ret < 0)
2325 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08002326 io_put_req_find_next(req, nxt);
Jens Axboe0fa03c62019-04-19 13:34:07 -06002327 return 0;
2328#else
2329 return -EOPNOTSUPP;
2330#endif
2331}
2332
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002333static int io_accept_prep(struct io_kiocb *req)
Jens Axboe17f2fe32019-10-17 14:42:58 -06002334{
2335#if defined(CONFIG_NET)
Jens Axboefc4df992019-12-10 14:38:45 -07002336 const struct io_uring_sqe *sqe = req->sqe;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002337 struct io_accept *accept = &req->accept;
2338
2339 if (req->flags & REQ_F_PREPPED)
2340 return 0;
Jens Axboe17f2fe32019-10-17 14:42:58 -06002341
2342 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
2343 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05002344 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06002345 return -EINVAL;
2346
Jens Axboed55e5f52019-12-11 16:12:15 -07002347 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
2348 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002349 accept->flags = READ_ONCE(sqe->accept_flags);
2350 req->flags |= REQ_F_PREPPED;
2351 return 0;
2352#else
2353 return -EOPNOTSUPP;
2354#endif
2355}
Jens Axboe17f2fe32019-10-17 14:42:58 -06002356
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002357#if defined(CONFIG_NET)
2358static int __io_accept(struct io_kiocb *req, struct io_kiocb **nxt,
2359 bool force_nonblock)
2360{
2361 struct io_accept *accept = &req->accept;
2362 unsigned file_flags;
2363 int ret;
2364
2365 file_flags = force_nonblock ? O_NONBLOCK : 0;
2366 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
2367 accept->addr_len, accept->flags);
2368 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06002369 return -EAGAIN;
Jens Axboe8e3cca12019-11-09 19:52:33 -07002370 if (ret == -ERESTARTSYS)
2371 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002372 if (ret < 0)
2373 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07002374 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08002375 io_put_req_find_next(req, nxt);
Jens Axboe17f2fe32019-10-17 14:42:58 -06002376 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002377}
2378
2379static void io_accept_finish(struct io_wq_work **workptr)
2380{
2381 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2382 struct io_kiocb *nxt = NULL;
2383
2384 if (io_req_cancelled(req))
2385 return;
2386 __io_accept(req, &nxt, false);
2387 if (nxt)
2388 *workptr = &nxt->work;
2389}
2390#endif
2391
2392static int io_accept(struct io_kiocb *req, struct io_kiocb **nxt,
2393 bool force_nonblock)
2394{
2395#if defined(CONFIG_NET)
2396 int ret;
2397
2398 ret = io_accept_prep(req);
2399 if (ret)
2400 return ret;
2401
2402 ret = __io_accept(req, nxt, force_nonblock);
2403 if (ret == -EAGAIN && force_nonblock) {
2404 req->work.func = io_accept_finish;
2405 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
2406 io_put_req(req);
2407 return -EAGAIN;
2408 }
2409 return 0;
Jens Axboe17f2fe32019-10-17 14:42:58 -06002410#else
2411 return -EOPNOTSUPP;
2412#endif
2413}
2414
Jens Axboef499a022019-12-02 16:28:46 -07002415static int io_connect_prep(struct io_kiocb *req, struct io_async_ctx *io)
2416{
2417#if defined(CONFIG_NET)
2418 const struct io_uring_sqe *sqe = req->sqe;
Jens Axboef499a022019-12-02 16:28:46 -07002419
Jens Axboe3fbb51c2019-12-20 08:51:52 -07002420 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
2421 return -EINVAL;
2422 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
2423 return -EINVAL;
2424
2425 req->connect.addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
2426 req->connect.addr_len = READ_ONCE(sqe->addr2);
2427 return move_addr_to_kernel(req->connect.addr, req->connect.addr_len,
2428 &io->connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07002429#else
Jens Axboe3fbb51c2019-12-20 08:51:52 -07002430 return -EOPNOTSUPP;
Jens Axboef499a022019-12-02 16:28:46 -07002431#endif
2432}
2433
Jens Axboefc4df992019-12-10 14:38:45 -07002434static int io_connect(struct io_kiocb *req, struct io_kiocb **nxt,
2435 bool force_nonblock)
Jens Axboef8e85cf2019-11-23 14:24:24 -07002436{
2437#if defined(CONFIG_NET)
Jens Axboef499a022019-12-02 16:28:46 -07002438 struct io_async_ctx __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07002439 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07002440 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07002441
Jens Axboef499a022019-12-02 16:28:46 -07002442 if (req->io) {
2443 io = req->io;
2444 } else {
2445 ret = io_connect_prep(req, &__io);
2446 if (ret)
2447 goto out;
2448 io = &__io;
2449 }
2450
Jens Axboe3fbb51c2019-12-20 08:51:52 -07002451 file_flags = force_nonblock ? O_NONBLOCK : 0;
2452
2453 ret = __sys_connect_file(req->file, &io->connect.address,
2454 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07002455 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002456 if (req->io)
2457 return -EAGAIN;
2458 if (io_alloc_async_ctx(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07002459 ret = -ENOMEM;
2460 goto out;
2461 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002462 memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect));
Jens Axboef8e85cf2019-11-23 14:24:24 -07002463 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07002464 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07002465 if (ret == -ERESTARTSYS)
2466 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07002467out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002468 if (ret < 0)
2469 req_set_fail_links(req);
Jens Axboef8e85cf2019-11-23 14:24:24 -07002470 io_cqring_add_event(req, ret);
2471 io_put_req_find_next(req, nxt);
2472 return 0;
2473#else
2474 return -EOPNOTSUPP;
2475#endif
2476}
2477
Jens Axboe221c5eb2019-01-17 09:41:58 -07002478static void io_poll_remove_one(struct io_kiocb *req)
2479{
2480 struct io_poll_iocb *poll = &req->poll;
2481
2482 spin_lock(&poll->head->lock);
2483 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07002484 if (!list_empty(&poll->wait.entry)) {
2485 list_del_init(&poll->wait.entry);
Jackie Liua197f662019-11-08 08:09:12 -07002486 io_queue_async_work(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002487 }
2488 spin_unlock(&poll->head->lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07002489 hash_del(&req->hash_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002490}
2491
2492static void io_poll_remove_all(struct io_ring_ctx *ctx)
2493{
Jens Axboe78076bb2019-12-04 19:56:40 -07002494 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002495 struct io_kiocb *req;
Jens Axboe78076bb2019-12-04 19:56:40 -07002496 int i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002497
2498 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07002499 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
2500 struct hlist_head *list;
2501
2502 list = &ctx->cancel_hash[i];
2503 hlist_for_each_entry_safe(req, tmp, list, hash_node)
2504 io_poll_remove_one(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002505 }
2506 spin_unlock_irq(&ctx->completion_lock);
2507}
2508
Jens Axboe47f46762019-11-09 17:43:02 -07002509static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
2510{
Jens Axboe78076bb2019-12-04 19:56:40 -07002511 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07002512 struct io_kiocb *req;
2513
Jens Axboe78076bb2019-12-04 19:56:40 -07002514 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
2515 hlist_for_each_entry(req, list, hash_node) {
2516 if (sqe_addr == req->user_data) {
Jens Axboeeac406c2019-11-14 12:09:58 -07002517 io_poll_remove_one(req);
2518 return 0;
2519 }
Jens Axboe47f46762019-11-09 17:43:02 -07002520 }
2521
2522 return -ENOENT;
2523}
2524
Jens Axboe0969e782019-12-17 18:40:57 -07002525static int io_poll_remove_prep(struct io_kiocb *req)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002526{
Jens Axboefc4df992019-12-10 14:38:45 -07002527 const struct io_uring_sqe *sqe = req->sqe;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002528
Jens Axboe0969e782019-12-17 18:40:57 -07002529 if (req->flags & REQ_F_PREPPED)
2530 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002531 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2532 return -EINVAL;
2533 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
2534 sqe->poll_events)
2535 return -EINVAL;
2536
Jens Axboe0969e782019-12-17 18:40:57 -07002537 req->poll.addr = READ_ONCE(sqe->addr);
2538 req->flags |= REQ_F_PREPPED;
2539 return 0;
2540}
2541
2542/*
2543 * Find a running poll command that matches one specified in sqe->addr,
2544 * and remove it if found.
2545 */
2546static int io_poll_remove(struct io_kiocb *req)
2547{
2548 struct io_ring_ctx *ctx = req->ctx;
2549 u64 addr;
2550 int ret;
2551
2552 ret = io_poll_remove_prep(req);
2553 if (ret)
2554 return ret;
2555
2556 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002557 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07002558 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002559 spin_unlock_irq(&ctx->completion_lock);
2560
Jens Axboe78e19bb2019-11-06 15:21:34 -07002561 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002562 if (ret < 0)
2563 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06002564 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002565 return 0;
2566}
2567
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002568static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002569{
Jackie Liua197f662019-11-08 08:09:12 -07002570 struct io_ring_ctx *ctx = req->ctx;
2571
Jens Axboe8c838782019-03-12 15:48:16 -06002572 req->poll.done = true;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002573 if (error)
2574 io_cqring_fill_event(req, error);
2575 else
2576 io_cqring_fill_event(req, mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06002577 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002578}
2579
Jens Axboe561fb042019-10-24 07:25:42 -06002580static void io_poll_complete_work(struct io_wq_work **workptr)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002581{
Jens Axboe561fb042019-10-24 07:25:42 -06002582 struct io_wq_work *work = *workptr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002583 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
2584 struct io_poll_iocb *poll = &req->poll;
2585 struct poll_table_struct pt = { ._key = poll->events };
2586 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe89723d02019-11-05 15:32:58 -07002587 struct io_kiocb *nxt = NULL;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002588 __poll_t mask = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002589 int ret = 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002590
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002591 if (work->flags & IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06002592 WRITE_ONCE(poll->canceled, true);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002593 ret = -ECANCELED;
2594 } else if (READ_ONCE(poll->canceled)) {
2595 ret = -ECANCELED;
2596 }
Jens Axboe561fb042019-10-24 07:25:42 -06002597
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002598 if (ret != -ECANCELED)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002599 mask = vfs_poll(poll->file, &pt) & poll->events;
2600
2601 /*
2602 * Note that ->ki_cancel callers also delete iocb from active_reqs after
2603 * calling ->ki_cancel. We need the ctx_lock roundtrip here to
2604 * synchronize with them. In the cancellation case the list_del_init
2605 * itself is not actually needed, but harmless so we keep it in to
2606 * avoid further branches in the fast path.
2607 */
2608 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002609 if (!mask && ret != -ECANCELED) {
Jens Axboe392edb42019-12-09 17:52:20 -07002610 add_wait_queue(poll->head, &poll->wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002611 spin_unlock_irq(&ctx->completion_lock);
2612 return;
2613 }
Jens Axboe78076bb2019-12-04 19:56:40 -07002614 hash_del(&req->hash_node);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002615 io_poll_complete(req, mask, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002616 spin_unlock_irq(&ctx->completion_lock);
2617
Jens Axboe8c838782019-03-12 15:48:16 -06002618 io_cqring_ev_posted(ctx);
Jens Axboe89723d02019-11-05 15:32:58 -07002619
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002620 if (ret < 0)
2621 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08002622 io_put_req_find_next(req, &nxt);
Jens Axboe89723d02019-11-05 15:32:58 -07002623 if (nxt)
2624 *workptr = &nxt->work;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002625}
2626
2627static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
2628 void *key)
2629{
Jens Axboee9444752019-11-26 15:02:04 -07002630 struct io_poll_iocb *poll = wait->private;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002631 struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
2632 struct io_ring_ctx *ctx = req->ctx;
2633 __poll_t mask = key_to_poll(key);
Jens Axboe8c838782019-03-12 15:48:16 -06002634 unsigned long flags;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002635
2636 /* for instances that support it check for an event match first: */
Jens Axboe8c838782019-03-12 15:48:16 -06002637 if (mask && !(mask & poll->events))
2638 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002639
Jens Axboe392edb42019-12-09 17:52:20 -07002640 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06002641
Jens Axboe7c9e7f02019-11-12 08:15:53 -07002642 /*
2643 * Run completion inline if we can. We're using trylock here because
2644 * we are violating the completion_lock -> poll wq lock ordering.
2645 * If we have a link timeout we're going to need the completion_lock
2646 * for finalizing the request, mark us as having grabbed that already.
2647 */
Jens Axboe8c838782019-03-12 15:48:16 -06002648 if (mask && spin_trylock_irqsave(&ctx->completion_lock, flags)) {
Jens Axboe78076bb2019-12-04 19:56:40 -07002649 hash_del(&req->hash_node);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002650 io_poll_complete(req, mask, 0);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07002651 req->flags |= REQ_F_COMP_LOCKED;
2652 io_put_req(req);
Jens Axboe8c838782019-03-12 15:48:16 -06002653 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2654
2655 io_cqring_ev_posted(ctx);
Jens Axboe8c838782019-03-12 15:48:16 -06002656 } else {
Jackie Liua197f662019-11-08 08:09:12 -07002657 io_queue_async_work(req);
Jens Axboe8c838782019-03-12 15:48:16 -06002658 }
2659
Jens Axboe221c5eb2019-01-17 09:41:58 -07002660 return 1;
2661}
2662
2663struct io_poll_table {
2664 struct poll_table_struct pt;
2665 struct io_kiocb *req;
2666 int error;
2667};
2668
2669static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
2670 struct poll_table_struct *p)
2671{
2672 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
2673
2674 if (unlikely(pt->req->poll.head)) {
2675 pt->error = -EINVAL;
2676 return;
2677 }
2678
2679 pt->error = 0;
2680 pt->req->poll.head = head;
Jens Axboe392edb42019-12-09 17:52:20 -07002681 add_wait_queue(head, &pt->req->poll.wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002682}
2683
Jens Axboeeac406c2019-11-14 12:09:58 -07002684static void io_poll_req_insert(struct io_kiocb *req)
2685{
2686 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07002687 struct hlist_head *list;
Jens Axboeeac406c2019-11-14 12:09:58 -07002688
Jens Axboe78076bb2019-12-04 19:56:40 -07002689 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
2690 hlist_add_head(&req->hash_node, list);
Jens Axboeeac406c2019-11-14 12:09:58 -07002691}
2692
Jens Axboe0969e782019-12-17 18:40:57 -07002693static int io_poll_add_prep(struct io_kiocb *req)
Jens Axboe221c5eb2019-01-17 09:41:58 -07002694{
Jens Axboefc4df992019-12-10 14:38:45 -07002695 const struct io_uring_sqe *sqe = req->sqe;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002696 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002697 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002698
Jens Axboe0969e782019-12-17 18:40:57 -07002699 if (req->flags & REQ_F_PREPPED)
2700 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002701 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2702 return -EINVAL;
2703 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
2704 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06002705 if (!poll->file)
2706 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002707
Jens Axboe0969e782019-12-17 18:40:57 -07002708 req->flags |= REQ_F_PREPPED;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002709 events = READ_ONCE(sqe->poll_events);
2710 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
Jens Axboe0969e782019-12-17 18:40:57 -07002711 return 0;
2712}
2713
2714static int io_poll_add(struct io_kiocb *req, struct io_kiocb **nxt)
2715{
2716 struct io_poll_iocb *poll = &req->poll;
2717 struct io_ring_ctx *ctx = req->ctx;
2718 struct io_poll_table ipt;
2719 bool cancel = false;
2720 __poll_t mask;
2721 int ret;
2722
2723 ret = io_poll_add_prep(req);
2724 if (ret)
2725 return ret;
2726
2727 INIT_IO_WORK(&req->work, io_poll_complete_work);
Jens Axboe78076bb2019-12-04 19:56:40 -07002728 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002729
Jens Axboe221c5eb2019-01-17 09:41:58 -07002730 poll->head = NULL;
Jens Axboe8c838782019-03-12 15:48:16 -06002731 poll->done = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002732 poll->canceled = false;
2733
2734 ipt.pt._qproc = io_poll_queue_proc;
2735 ipt.pt._key = poll->events;
2736 ipt.req = req;
2737 ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
2738
2739 /* initialized the list so that we can do list_empty checks */
Jens Axboe392edb42019-12-09 17:52:20 -07002740 INIT_LIST_HEAD(&poll->wait.entry);
2741 init_waitqueue_func_entry(&poll->wait, io_poll_wake);
2742 poll->wait.private = poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002743
Jens Axboe36703242019-07-25 10:20:18 -06002744 INIT_LIST_HEAD(&req->list);
2745
Jens Axboe221c5eb2019-01-17 09:41:58 -07002746 mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002747
2748 spin_lock_irq(&ctx->completion_lock);
Jens Axboe8c838782019-03-12 15:48:16 -06002749 if (likely(poll->head)) {
2750 spin_lock(&poll->head->lock);
Jens Axboe392edb42019-12-09 17:52:20 -07002751 if (unlikely(list_empty(&poll->wait.entry))) {
Jens Axboe8c838782019-03-12 15:48:16 -06002752 if (ipt.error)
2753 cancel = true;
2754 ipt.error = 0;
2755 mask = 0;
2756 }
2757 if (mask || ipt.error)
Jens Axboe392edb42019-12-09 17:52:20 -07002758 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06002759 else if (cancel)
2760 WRITE_ONCE(poll->canceled, true);
2761 else if (!poll->done) /* actually waiting for an event */
Jens Axboeeac406c2019-11-14 12:09:58 -07002762 io_poll_req_insert(req);
Jens Axboe8c838782019-03-12 15:48:16 -06002763 spin_unlock(&poll->head->lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002764 }
Jens Axboe8c838782019-03-12 15:48:16 -06002765 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06002766 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07002767 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06002768 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07002769 spin_unlock_irq(&ctx->completion_lock);
2770
Jens Axboe8c838782019-03-12 15:48:16 -06002771 if (mask) {
2772 io_cqring_ev_posted(ctx);
Jackie Liuec9c02a2019-11-08 23:50:36 +08002773 io_put_req_find_next(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07002774 }
Jens Axboe8c838782019-03-12 15:48:16 -06002775 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07002776}
2777
Jens Axboe5262f562019-09-17 12:26:57 -06002778static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
2779{
Jens Axboead8a48a2019-11-15 08:49:11 -07002780 struct io_timeout_data *data = container_of(timer,
2781 struct io_timeout_data, timer);
2782 struct io_kiocb *req = data->req;
2783 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06002784 unsigned long flags;
2785
Jens Axboe5262f562019-09-17 12:26:57 -06002786 atomic_inc(&ctx->cq_timeouts);
2787
2788 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08002789 /*
Jens Axboe11365042019-10-16 09:08:32 -06002790 * We could be racing with timeout deletion. If the list is empty,
2791 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08002792 */
Jens Axboe842f9612019-10-29 12:34:10 -06002793 if (!list_empty(&req->list)) {
Jens Axboe11365042019-10-16 09:08:32 -06002794 struct io_kiocb *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06002795
Jens Axboe11365042019-10-16 09:08:32 -06002796 /*
2797 * Adjust the reqs sequence before the current one because it
Brian Gianforcarod195a662019-12-13 03:09:50 -08002798 * will consume a slot in the cq_ring and the cq_tail
Jens Axboe11365042019-10-16 09:08:32 -06002799 * pointer will be increased, otherwise other timeout reqs may
2800 * return in advance without waiting for enough wait_nr.
2801 */
2802 prev = req;
2803 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
2804 prev->sequence++;
Jens Axboe11365042019-10-16 09:08:32 -06002805 list_del_init(&req->list);
Jens Axboe11365042019-10-16 09:08:32 -06002806 }
Jens Axboe842f9612019-10-29 12:34:10 -06002807
Jens Axboe78e19bb2019-11-06 15:21:34 -07002808 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06002809 io_commit_cqring(ctx);
2810 spin_unlock_irqrestore(&ctx->completion_lock, flags);
2811
2812 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002813 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06002814 io_put_req(req);
2815 return HRTIMER_NORESTART;
2816}
2817
Jens Axboe47f46762019-11-09 17:43:02 -07002818static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
2819{
2820 struct io_kiocb *req;
2821 int ret = -ENOENT;
2822
2823 list_for_each_entry(req, &ctx->timeout_list, list) {
2824 if (user_data == req->user_data) {
2825 list_del_init(&req->list);
2826 ret = 0;
2827 break;
2828 }
2829 }
2830
2831 if (ret == -ENOENT)
2832 return ret;
2833
Jens Axboe2d283902019-12-04 11:08:05 -07002834 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe47f46762019-11-09 17:43:02 -07002835 if (ret == -1)
2836 return -EALREADY;
2837
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002838 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07002839 io_cqring_fill_event(req, -ECANCELED);
2840 io_put_req(req);
2841 return 0;
2842}
2843
Jens Axboeb29472e2019-12-17 18:50:29 -07002844static int io_timeout_remove_prep(struct io_kiocb *req)
2845{
2846 const struct io_uring_sqe *sqe = req->sqe;
2847
2848 if (req->flags & REQ_F_PREPPED)
2849 return 0;
2850 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2851 return -EINVAL;
2852 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
2853 return -EINVAL;
2854
2855 req->timeout.addr = READ_ONCE(sqe->addr);
2856 req->timeout.flags = READ_ONCE(sqe->timeout_flags);
2857 if (req->timeout.flags)
2858 return -EINVAL;
2859
2860 req->flags |= REQ_F_PREPPED;
2861 return 0;
2862}
2863
Jens Axboe11365042019-10-16 09:08:32 -06002864/*
2865 * Remove or update an existing timeout command
2866 */
Jens Axboefc4df992019-12-10 14:38:45 -07002867static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06002868{
2869 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07002870 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06002871
Jens Axboeb29472e2019-12-17 18:50:29 -07002872 ret = io_timeout_remove_prep(req);
2873 if (ret)
2874 return ret;
Jens Axboe11365042019-10-16 09:08:32 -06002875
Jens Axboe11365042019-10-16 09:08:32 -06002876 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb29472e2019-12-17 18:50:29 -07002877 ret = io_timeout_cancel(ctx, req->timeout.addr);
Jens Axboe11365042019-10-16 09:08:32 -06002878
Jens Axboe47f46762019-11-09 17:43:02 -07002879 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06002880 io_commit_cqring(ctx);
2881 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06002882 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002883 if (ret < 0)
2884 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08002885 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06002886 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06002887}
2888
Jens Axboe2d283902019-12-04 11:08:05 -07002889static int io_timeout_prep(struct io_kiocb *req, struct io_async_ctx *io,
2890 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06002891{
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002892 const struct io_uring_sqe *sqe = req->sqe;
Jens Axboead8a48a2019-11-15 08:49:11 -07002893 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06002894 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06002895
Jens Axboead8a48a2019-11-15 08:49:11 -07002896 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06002897 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07002898 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06002899 return -EINVAL;
Jens Axboe2d283902019-12-04 11:08:05 -07002900 if (sqe->off && is_timeout_link)
2901 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06002902 flags = READ_ONCE(sqe->timeout_flags);
2903 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06002904 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06002905
Jens Axboe26a61672019-12-20 09:02:01 -07002906 req->timeout.count = READ_ONCE(sqe->off);
2907
2908 if (!io && io_alloc_async_ctx(req))
2909 return -ENOMEM;
2910
2911 data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07002912 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07002913 req->flags |= REQ_F_TIMEOUT;
2914
2915 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06002916 return -EFAULT;
2917
Jens Axboe11365042019-10-16 09:08:32 -06002918 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07002919 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06002920 else
Jens Axboead8a48a2019-11-15 08:49:11 -07002921 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06002922
Jens Axboead8a48a2019-11-15 08:49:11 -07002923 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
2924 return 0;
2925}
2926
Jens Axboefc4df992019-12-10 14:38:45 -07002927static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07002928{
2929 unsigned count;
2930 struct io_ring_ctx *ctx = req->ctx;
2931 struct io_timeout_data *data;
2932 struct list_head *entry;
2933 unsigned span = 0;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002934 int ret;
Jens Axboead8a48a2019-11-15 08:49:11 -07002935
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002936 if (!req->io) {
2937 if (io_alloc_async_ctx(req))
Jens Axboe2d283902019-12-04 11:08:05 -07002938 return -ENOMEM;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002939 ret = io_timeout_prep(req, req->io, false);
2940 if (ret)
Jens Axboe2d283902019-12-04 11:08:05 -07002941 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07002942 }
2943 data = &req->io->timeout;
Jens Axboe93bd25b2019-11-11 23:34:31 -07002944
Jens Axboe5262f562019-09-17 12:26:57 -06002945 /*
2946 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07002947 * timeout event to be satisfied. If it isn't set, then this is
2948 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06002949 */
Jens Axboe26a61672019-12-20 09:02:01 -07002950 count = req->timeout.count;
Jens Axboe93bd25b2019-11-11 23:34:31 -07002951 if (!count) {
2952 req->flags |= REQ_F_TIMEOUT_NOSEQ;
2953 spin_lock_irq(&ctx->completion_lock);
2954 entry = ctx->timeout_list.prev;
2955 goto add;
2956 }
Jens Axboe5262f562019-09-17 12:26:57 -06002957
2958 req->sequence = ctx->cached_sq_head + count - 1;
Jens Axboe2d283902019-12-04 11:08:05 -07002959 data->seq_offset = count;
Jens Axboe5262f562019-09-17 12:26:57 -06002960
2961 /*
2962 * Insertion sort, ensuring the first entry in the list is always
2963 * the one we need first.
2964 */
Jens Axboe5262f562019-09-17 12:26:57 -06002965 spin_lock_irq(&ctx->completion_lock);
2966 list_for_each_prev(entry, &ctx->timeout_list) {
2967 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
yangerkun5da0fb12019-10-15 21:59:29 +08002968 unsigned nxt_sq_head;
2969 long long tmp, tmp_nxt;
Jens Axboe2d283902019-12-04 11:08:05 -07002970 u32 nxt_offset = nxt->io->timeout.seq_offset;
Jens Axboe5262f562019-09-17 12:26:57 -06002971
Jens Axboe93bd25b2019-11-11 23:34:31 -07002972 if (nxt->flags & REQ_F_TIMEOUT_NOSEQ)
2973 continue;
2974
yangerkun5da0fb12019-10-15 21:59:29 +08002975 /*
2976 * Since cached_sq_head + count - 1 can overflow, use type long
2977 * long to store it.
2978 */
2979 tmp = (long long)ctx->cached_sq_head + count - 1;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +03002980 nxt_sq_head = nxt->sequence - nxt_offset + 1;
2981 tmp_nxt = (long long)nxt_sq_head + nxt_offset - 1;
yangerkun5da0fb12019-10-15 21:59:29 +08002982
2983 /*
2984 * cached_sq_head may overflow, and it will never overflow twice
2985 * once there is some timeout req still be valid.
2986 */
2987 if (ctx->cached_sq_head < nxt_sq_head)
yangerkun8b07a652019-10-17 12:12:35 +08002988 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08002989
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002990 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06002991 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08002992
2993 /*
2994 * Sequence of reqs after the insert one and itself should
2995 * be adjusted because each timeout req consumes a slot.
2996 */
2997 span++;
2998 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06002999 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003000 req->sequence -= span;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003001add:
Jens Axboe5262f562019-09-17 12:26:57 -06003002 list_add(&req->list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07003003 data->timer.function = io_timeout_fn;
3004 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06003005 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06003006 return 0;
3007}
3008
Jens Axboe62755e32019-10-28 21:49:21 -06003009static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06003010{
Jens Axboe62755e32019-10-28 21:49:21 -06003011 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06003012
Jens Axboe62755e32019-10-28 21:49:21 -06003013 return req->user_data == (unsigned long) data;
3014}
3015
Jens Axboee977d6d2019-11-05 12:39:45 -07003016static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06003017{
Jens Axboe62755e32019-10-28 21:49:21 -06003018 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06003019 int ret = 0;
3020
Jens Axboe62755e32019-10-28 21:49:21 -06003021 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
3022 switch (cancel_ret) {
3023 case IO_WQ_CANCEL_OK:
3024 ret = 0;
3025 break;
3026 case IO_WQ_CANCEL_RUNNING:
3027 ret = -EALREADY;
3028 break;
3029 case IO_WQ_CANCEL_NOTFOUND:
3030 ret = -ENOENT;
3031 break;
3032 }
3033
Jens Axboee977d6d2019-11-05 12:39:45 -07003034 return ret;
3035}
3036
Jens Axboe47f46762019-11-09 17:43:02 -07003037static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
3038 struct io_kiocb *req, __u64 sqe_addr,
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003039 struct io_kiocb **nxt, int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07003040{
3041 unsigned long flags;
3042 int ret;
3043
3044 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
3045 if (ret != -ENOENT) {
3046 spin_lock_irqsave(&ctx->completion_lock, flags);
3047 goto done;
3048 }
3049
3050 spin_lock_irqsave(&ctx->completion_lock, flags);
3051 ret = io_timeout_cancel(ctx, sqe_addr);
3052 if (ret != -ENOENT)
3053 goto done;
3054 ret = io_poll_cancel(ctx, sqe_addr);
3055done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003056 if (!ret)
3057 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07003058 io_cqring_fill_event(req, ret);
3059 io_commit_cqring(ctx);
3060 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3061 io_cqring_ev_posted(ctx);
3062
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003063 if (ret < 0)
3064 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07003065 io_put_req_find_next(req, nxt);
3066}
3067
Jens Axboefbf23842019-12-17 18:45:56 -07003068static int io_async_cancel_prep(struct io_kiocb *req)
Jens Axboee977d6d2019-11-05 12:39:45 -07003069{
Jens Axboefc4df992019-12-10 14:38:45 -07003070 const struct io_uring_sqe *sqe = req->sqe;
Jens Axboee977d6d2019-11-05 12:39:45 -07003071
Jens Axboefbf23842019-12-17 18:45:56 -07003072 if (req->flags & REQ_F_PREPPED)
3073 return 0;
3074 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07003075 return -EINVAL;
3076 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
3077 sqe->cancel_flags)
3078 return -EINVAL;
3079
Jens Axboefbf23842019-12-17 18:45:56 -07003080 req->flags |= REQ_F_PREPPED;
3081 req->cancel.addr = READ_ONCE(sqe->addr);
3082 return 0;
3083}
3084
3085static int io_async_cancel(struct io_kiocb *req, struct io_kiocb **nxt)
3086{
3087 struct io_ring_ctx *ctx = req->ctx;
3088 int ret;
3089
3090 ret = io_async_cancel_prep(req);
3091 if (ret)
3092 return ret;
3093
3094 io_async_find_and_cancel(ctx, req, req->cancel.addr, nxt, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06003095 return 0;
3096}
3097
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003098static int io_req_defer_prep(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07003099{
3100 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003101 struct io_async_ctx *io = req->io;
Jens Axboef67676d2019-12-02 11:03:47 -07003102 struct iov_iter iter;
Jens Axboee7815732019-12-17 19:45:06 -07003103 ssize_t ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003104
Jens Axboed625c6e2019-12-17 19:53:05 -07003105 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07003106 case IORING_OP_NOP:
3107 break;
Jens Axboef67676d2019-12-02 11:03:47 -07003108 case IORING_OP_READV:
3109 case IORING_OP_READ_FIXED:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003110 /* ensure prep does right import */
3111 req->io = NULL;
Jens Axboef67676d2019-12-02 11:03:47 -07003112 ret = io_read_prep(req, &iovec, &iter, true);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003113 req->io = io;
3114 if (ret < 0)
3115 break;
3116 io_req_map_rw(req, ret, iovec, inline_vecs, &iter);
3117 ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003118 break;
3119 case IORING_OP_WRITEV:
3120 case IORING_OP_WRITE_FIXED:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003121 /* ensure prep does right import */
3122 req->io = NULL;
Jens Axboef67676d2019-12-02 11:03:47 -07003123 ret = io_write_prep(req, &iovec, &iter, true);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003124 req->io = io;
3125 if (ret < 0)
3126 break;
3127 io_req_map_rw(req, ret, iovec, inline_vecs, &iter);
3128 ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003129 break;
Jens Axboe0969e782019-12-17 18:40:57 -07003130 case IORING_OP_POLL_ADD:
3131 ret = io_poll_add_prep(req);
3132 break;
3133 case IORING_OP_POLL_REMOVE:
3134 ret = io_poll_remove_prep(req);
3135 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003136 case IORING_OP_FSYNC:
3137 ret = io_prep_fsync(req);
3138 break;
3139 case IORING_OP_SYNC_FILE_RANGE:
3140 ret = io_prep_sfr(req);
3141 break;
Jens Axboe03b12302019-12-02 18:50:25 -07003142 case IORING_OP_SENDMSG:
3143 ret = io_sendmsg_prep(req, io);
3144 break;
3145 case IORING_OP_RECVMSG:
3146 ret = io_recvmsg_prep(req, io);
3147 break;
Jens Axboef499a022019-12-02 16:28:46 -07003148 case IORING_OP_CONNECT:
3149 ret = io_connect_prep(req, io);
3150 break;
Jens Axboe2d283902019-12-04 11:08:05 -07003151 case IORING_OP_TIMEOUT:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003152 ret = io_timeout_prep(req, io, false);
3153 break;
Jens Axboeb29472e2019-12-17 18:50:29 -07003154 case IORING_OP_TIMEOUT_REMOVE:
3155 ret = io_timeout_remove_prep(req);
3156 break;
Jens Axboefbf23842019-12-17 18:45:56 -07003157 case IORING_OP_ASYNC_CANCEL:
3158 ret = io_async_cancel_prep(req);
3159 break;
Jens Axboe2d283902019-12-04 11:08:05 -07003160 case IORING_OP_LINK_TIMEOUT:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003161 ret = io_timeout_prep(req, io, true);
3162 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003163 case IORING_OP_ACCEPT:
3164 ret = io_accept_prep(req);
3165 break;
Jens Axboef67676d2019-12-02 11:03:47 -07003166 default:
Jens Axboee7815732019-12-17 19:45:06 -07003167 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
3168 req->opcode);
3169 ret = -EINVAL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003170 break;
Jens Axboef67676d2019-12-02 11:03:47 -07003171 }
3172
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003173 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003174}
3175
Jackie Liua197f662019-11-08 08:09:12 -07003176static int io_req_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -06003177{
Jackie Liua197f662019-11-08 08:09:12 -07003178 struct io_ring_ctx *ctx = req->ctx;
Jens Axboef67676d2019-12-02 11:03:47 -07003179 int ret;
Jens Axboede0617e2019-04-06 21:51:27 -06003180
Bob Liu9d858b22019-11-13 18:06:25 +08003181 /* Still need defer if there is pending req in defer list. */
3182 if (!req_need_defer(req) && list_empty(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06003183 return 0;
3184
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003185 if (io_alloc_async_ctx(req))
Jens Axboede0617e2019-04-06 21:51:27 -06003186 return -EAGAIN;
3187
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003188 ret = io_req_defer_prep(req);
3189 if (ret < 0)
Jens Axboe2d283902019-12-04 11:08:05 -07003190 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07003191
Jens Axboede0617e2019-04-06 21:51:27 -06003192 spin_lock_irq(&ctx->completion_lock);
Bob Liu9d858b22019-11-13 18:06:25 +08003193 if (!req_need_defer(req) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06003194 spin_unlock_irq(&ctx->completion_lock);
Jens Axboede0617e2019-04-06 21:51:27 -06003195 return 0;
3196 }
3197
Jens Axboe915967f2019-11-21 09:01:20 -07003198 trace_io_uring_defer(ctx, req, req->user_data);
Jens Axboede0617e2019-04-06 21:51:27 -06003199 list_add_tail(&req->list, &ctx->defer_list);
3200 spin_unlock_irq(&ctx->completion_lock);
3201 return -EIOCBQUEUED;
3202}
3203
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03003204__attribute__((nonnull))
Pavel Begunkovd7324472019-11-21 21:24:36 +03003205static int io_issue_sqe(struct io_kiocb *req, struct io_kiocb **nxt,
3206 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003207{
Jackie Liua197f662019-11-08 08:09:12 -07003208 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07003209 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003210
Jens Axboed625c6e2019-12-17 19:53:05 -07003211 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07003212 case IORING_OP_NOP:
Jens Axboe78e19bb2019-11-06 15:21:34 -07003213 ret = io_nop(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003214 break;
3215 case IORING_OP_READV:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003216 if (unlikely(req->sqe->buf_index))
Jens Axboeedafcce2019-01-09 09:16:05 -07003217 return -EINVAL;
Pavel Begunkov267bc902019-11-07 01:41:08 +03003218 ret = io_read(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003219 break;
3220 case IORING_OP_WRITEV:
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003221 if (unlikely(req->sqe->buf_index))
Jens Axboeedafcce2019-01-09 09:16:05 -07003222 return -EINVAL;
Pavel Begunkov267bc902019-11-07 01:41:08 +03003223 ret = io_write(req, nxt, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07003224 break;
3225 case IORING_OP_READ_FIXED:
Pavel Begunkov267bc902019-11-07 01:41:08 +03003226 ret = io_read(req, nxt, force_nonblock);
Jens Axboeedafcce2019-01-09 09:16:05 -07003227 break;
3228 case IORING_OP_WRITE_FIXED:
Pavel Begunkov267bc902019-11-07 01:41:08 +03003229 ret = io_write(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003230 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003231 case IORING_OP_FSYNC:
Jens Axboefc4df992019-12-10 14:38:45 -07003232 ret = io_fsync(req, nxt, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07003233 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003234 case IORING_OP_POLL_ADD:
Jens Axboefc4df992019-12-10 14:38:45 -07003235 ret = io_poll_add(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003236 break;
3237 case IORING_OP_POLL_REMOVE:
Jens Axboefc4df992019-12-10 14:38:45 -07003238 ret = io_poll_remove(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003239 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003240 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboefc4df992019-12-10 14:38:45 -07003241 ret = io_sync_file_range(req, nxt, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003242 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003243 case IORING_OP_SENDMSG:
Jens Axboefc4df992019-12-10 14:38:45 -07003244 ret = io_sendmsg(req, nxt, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06003245 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06003246 case IORING_OP_RECVMSG:
Jens Axboefc4df992019-12-10 14:38:45 -07003247 ret = io_recvmsg(req, nxt, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06003248 break;
Jens Axboe5262f562019-09-17 12:26:57 -06003249 case IORING_OP_TIMEOUT:
Jens Axboefc4df992019-12-10 14:38:45 -07003250 ret = io_timeout(req);
Jens Axboe5262f562019-09-17 12:26:57 -06003251 break;
Jens Axboe11365042019-10-16 09:08:32 -06003252 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboefc4df992019-12-10 14:38:45 -07003253 ret = io_timeout_remove(req);
Jens Axboe11365042019-10-16 09:08:32 -06003254 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06003255 case IORING_OP_ACCEPT:
Jens Axboefc4df992019-12-10 14:38:45 -07003256 ret = io_accept(req, nxt, force_nonblock);
Jens Axboe17f2fe32019-10-17 14:42:58 -06003257 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003258 case IORING_OP_CONNECT:
Jens Axboefc4df992019-12-10 14:38:45 -07003259 ret = io_connect(req, nxt, force_nonblock);
Jens Axboef8e85cf2019-11-23 14:24:24 -07003260 break;
Jens Axboe62755e32019-10-28 21:49:21 -06003261 case IORING_OP_ASYNC_CANCEL:
Jens Axboefc4df992019-12-10 14:38:45 -07003262 ret = io_async_cancel(req, nxt);
Jens Axboe62755e32019-10-28 21:49:21 -06003263 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003264 default:
3265 ret = -EINVAL;
3266 break;
3267 }
3268
Jens Axboedef596e2019-01-09 08:59:42 -07003269 if (ret)
3270 return ret;
3271
3272 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe9e645e112019-05-10 16:07:28 -06003273 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07003274 return -EAGAIN;
3275
Jens Axboedef596e2019-01-09 08:59:42 -07003276 io_iopoll_req_issued(req);
Jens Axboedef596e2019-01-09 08:59:42 -07003277 }
3278
3279 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003280}
3281
Jens Axboeb76da702019-11-20 13:05:32 -07003282static void io_link_work_cb(struct io_wq_work **workptr)
3283{
3284 struct io_wq_work *work = *workptr;
3285 struct io_kiocb *link = work->data;
3286
3287 io_queue_linked_timeout(link);
3288 work->func = io_wq_submit_work;
3289}
3290
Jens Axboe561fb042019-10-24 07:25:42 -06003291static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboe31b51512019-01-18 22:56:34 -07003292{
Jens Axboe561fb042019-10-24 07:25:42 -06003293 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003294 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe561fb042019-10-24 07:25:42 -06003295 struct io_kiocb *nxt = NULL;
3296 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003297
Jens Axboe561fb042019-10-24 07:25:42 -06003298 if (work->flags & IO_WQ_WORK_CANCEL)
3299 ret = -ECANCELED;
Jens Axboe31b51512019-01-18 22:56:34 -07003300
Jens Axboe561fb042019-10-24 07:25:42 -06003301 if (!ret) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003302 req->has_user = (work->flags & IO_WQ_WORK_HAS_MM) != 0;
3303 req->in_async = true;
Jens Axboe561fb042019-10-24 07:25:42 -06003304 do {
Pavel Begunkovd7324472019-11-21 21:24:36 +03003305 ret = io_issue_sqe(req, &nxt, false);
Jens Axboe561fb042019-10-24 07:25:42 -06003306 /*
3307 * We can get EAGAIN for polled IO even though we're
3308 * forcing a sync submission from here, since we can't
3309 * wait for request slots on the block side.
3310 */
3311 if (ret != -EAGAIN)
3312 break;
3313 cond_resched();
3314 } while (1);
3315 }
Jens Axboe31b51512019-01-18 22:56:34 -07003316
Jens Axboe561fb042019-10-24 07:25:42 -06003317 /* drop submission reference */
Jackie Liuec9c02a2019-11-08 23:50:36 +08003318 io_put_req(req);
Jens Axboe817869d2019-04-30 14:44:05 -06003319
Jens Axboe561fb042019-10-24 07:25:42 -06003320 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003321 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07003322 io_cqring_add_event(req, ret);
Jens Axboe817869d2019-04-30 14:44:05 -06003323 io_put_req(req);
Jens Axboeedafcce2019-01-09 09:16:05 -07003324 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003325
Jens Axboe561fb042019-10-24 07:25:42 -06003326 /* if a dependent link is ready, pass it back */
3327 if (!ret && nxt) {
Jens Axboe94ae5e72019-11-14 19:39:52 -07003328 struct io_kiocb *link;
3329
3330 io_prep_async_work(nxt, &link);
Jens Axboe561fb042019-10-24 07:25:42 -06003331 *workptr = &nxt->work;
Jens Axboeb76da702019-11-20 13:05:32 -07003332 if (link) {
3333 nxt->work.flags |= IO_WQ_WORK_CB;
3334 nxt->work.func = io_link_work_cb;
3335 nxt->work.data = link;
3336 }
Jens Axboeedafcce2019-01-09 09:16:05 -07003337 }
Jens Axboe31b51512019-01-18 22:56:34 -07003338}
Jens Axboe2b188cc2019-01-07 10:46:33 -07003339
Jens Axboe9e3aa612019-12-11 15:55:43 -07003340static bool io_req_op_valid(int op)
3341{
3342 return op >= IORING_OP_NOP && op < IORING_OP_LAST;
3343}
3344
Jens Axboed625c6e2019-12-17 19:53:05 -07003345static int io_req_needs_file(struct io_kiocb *req)
Jens Axboe09bb8392019-03-13 12:39:28 -06003346{
Jens Axboed625c6e2019-12-17 19:53:05 -07003347 switch (req->opcode) {
Jens Axboe09bb8392019-03-13 12:39:28 -06003348 case IORING_OP_NOP:
3349 case IORING_OP_POLL_REMOVE:
Pavel Begunkov5683e542019-11-14 00:59:19 +03003350 case IORING_OP_TIMEOUT:
Pavel Begunkova320e9f2019-11-14 00:11:01 +03003351 case IORING_OP_TIMEOUT_REMOVE:
3352 case IORING_OP_ASYNC_CANCEL:
3353 case IORING_OP_LINK_TIMEOUT:
Jens Axboe9e3aa612019-12-11 15:55:43 -07003354 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06003355 default:
Jens Axboed625c6e2019-12-17 19:53:05 -07003356 if (io_req_op_valid(req->opcode))
Jens Axboe9e3aa612019-12-11 15:55:43 -07003357 return 1;
3358 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06003359 }
3360}
3361
Jens Axboe65e19f52019-10-26 07:20:21 -06003362static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
3363 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06003364{
Jens Axboe65e19f52019-10-26 07:20:21 -06003365 struct fixed_file_table *table;
3366
3367 table = &ctx->file_table[index >> IORING_FILE_TABLE_SHIFT];
3368 return table->files[index & IORING_FILE_TABLE_MASK];
3369}
3370
Jackie Liua197f662019-11-08 08:09:12 -07003371static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req)
Jens Axboe09bb8392019-03-13 12:39:28 -06003372{
Jackie Liua197f662019-11-08 08:09:12 -07003373 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe09bb8392019-03-13 12:39:28 -06003374 unsigned flags;
Jens Axboe9e3aa612019-12-11 15:55:43 -07003375 int fd, ret;
Jens Axboe09bb8392019-03-13 12:39:28 -06003376
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003377 flags = READ_ONCE(req->sqe->flags);
3378 fd = READ_ONCE(req->sqe->fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06003379
Jackie Liu4fe2c962019-09-09 20:50:40 +08003380 if (flags & IOSQE_IO_DRAIN)
Jens Axboede0617e2019-04-06 21:51:27 -06003381 req->flags |= REQ_F_IO_DRAIN;
Jens Axboede0617e2019-04-06 21:51:27 -06003382
Jens Axboed625c6e2019-12-17 19:53:05 -07003383 ret = io_req_needs_file(req);
Jens Axboe9e3aa612019-12-11 15:55:43 -07003384 if (ret <= 0)
3385 return ret;
Jens Axboe09bb8392019-03-13 12:39:28 -06003386
3387 if (flags & IOSQE_FIXED_FILE) {
Jens Axboe65e19f52019-10-26 07:20:21 -06003388 if (unlikely(!ctx->file_table ||
Jens Axboe09bb8392019-03-13 12:39:28 -06003389 (unsigned) fd >= ctx->nr_user_files))
3390 return -EBADF;
Jens Axboeb7620122019-10-26 07:22:55 -06003391 fd = array_index_nospec(fd, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06003392 req->file = io_file_from_index(ctx, fd);
3393 if (!req->file)
Jens Axboe08a45172019-10-03 08:11:03 -06003394 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06003395 req->flags |= REQ_F_FIXED_FILE;
3396 } else {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003397 if (req->needs_fixed_file)
Jens Axboe09bb8392019-03-13 12:39:28 -06003398 return -EBADF;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02003399 trace_io_uring_file_get(ctx, fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06003400 req->file = io_file_get(state, fd);
3401 if (unlikely(!req->file))
3402 return -EBADF;
3403 }
3404
3405 return 0;
3406}
3407
Jackie Liua197f662019-11-08 08:09:12 -07003408static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003409{
Jens Axboefcb323c2019-10-24 12:39:47 -06003410 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07003411 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06003412
3413 rcu_read_lock();
3414 spin_lock_irq(&ctx->inflight_lock);
3415 /*
3416 * We use the f_ops->flush() handler to ensure that we can flush
3417 * out work accessing these files if the fd is closed. Check if
3418 * the fd has changed since we started down this path, and disallow
3419 * this operation if it has.
3420 */
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003421 if (fcheck(req->ring_fd) == req->ring_file) {
Jens Axboefcb323c2019-10-24 12:39:47 -06003422 list_add(&req->inflight_entry, &ctx->inflight_list);
3423 req->flags |= REQ_F_INFLIGHT;
3424 req->work.files = current->files;
3425 ret = 0;
3426 }
3427 spin_unlock_irq(&ctx->inflight_lock);
3428 rcu_read_unlock();
3429
3430 return ret;
3431}
3432
Jens Axboe2665abf2019-11-05 12:40:47 -07003433static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
3434{
Jens Axboead8a48a2019-11-15 08:49:11 -07003435 struct io_timeout_data *data = container_of(timer,
3436 struct io_timeout_data, timer);
3437 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07003438 struct io_ring_ctx *ctx = req->ctx;
3439 struct io_kiocb *prev = NULL;
3440 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07003441
3442 spin_lock_irqsave(&ctx->completion_lock, flags);
3443
3444 /*
3445 * We don't expect the list to be empty, that will only happen if we
3446 * race with the completion of the linked work.
3447 */
Pavel Begunkov44932332019-12-05 16:16:35 +03003448 if (!list_empty(&req->link_list)) {
3449 prev = list_entry(req->link_list.prev, struct io_kiocb,
3450 link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07003451 if (refcount_inc_not_zero(&prev->refs)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03003452 list_del_init(&req->link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07003453 prev->flags &= ~REQ_F_LINK_TIMEOUT;
3454 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07003455 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07003456 }
3457
3458 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3459
3460 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003461 req_set_fail_links(prev);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003462 io_async_find_and_cancel(ctx, req, prev->user_data, NULL,
3463 -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07003464 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07003465 } else {
3466 io_cqring_add_event(req, -ETIME);
3467 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07003468 }
Jens Axboe2665abf2019-11-05 12:40:47 -07003469 return HRTIMER_NORESTART;
3470}
3471
Jens Axboead8a48a2019-11-15 08:49:11 -07003472static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07003473{
Jens Axboe76a46e02019-11-10 23:34:16 -07003474 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07003475
Jens Axboe76a46e02019-11-10 23:34:16 -07003476 /*
3477 * If the list is now empty, then our linked request finished before
3478 * we got a chance to setup the timer
3479 */
3480 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov44932332019-12-05 16:16:35 +03003481 if (!list_empty(&req->link_list)) {
Jens Axboe2d283902019-12-04 11:08:05 -07003482 struct io_timeout_data *data = &req->io->timeout;
Jens Axboe94ae5e72019-11-14 19:39:52 -07003483
Jens Axboead8a48a2019-11-15 08:49:11 -07003484 data->timer.function = io_link_timeout_fn;
3485 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
3486 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07003487 }
Jens Axboe76a46e02019-11-10 23:34:16 -07003488 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07003489
Jens Axboe2665abf2019-11-05 12:40:47 -07003490 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07003491 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07003492}
3493
Jens Axboead8a48a2019-11-15 08:49:11 -07003494static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07003495{
3496 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003497
Jens Axboe2665abf2019-11-05 12:40:47 -07003498 if (!(req->flags & REQ_F_LINK))
3499 return NULL;
3500
Pavel Begunkov44932332019-12-05 16:16:35 +03003501 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
3502 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07003503 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07003504 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07003505
Jens Axboe76a46e02019-11-10 23:34:16 -07003506 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07003507 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07003508}
3509
Jens Axboe0e0702d2019-11-14 21:42:10 -07003510static void __io_queue_sqe(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003511{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07003512 struct io_kiocb *linked_timeout;
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03003513 struct io_kiocb *nxt = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003514 int ret;
3515
Jens Axboe4a0a7a12019-12-09 20:01:01 -07003516again:
3517 linked_timeout = io_prep_linked_timeout(req);
3518
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03003519 ret = io_issue_sqe(req, &nxt, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06003520
3521 /*
3522 * We async punt it if the file wasn't marked NOWAIT, or if the file
3523 * doesn't support non-blocking read/write attempts
3524 */
3525 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
3526 (req->flags & REQ_F_MUST_PUNT))) {
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03003527 if (req->work.flags & IO_WQ_WORK_NEEDS_FILES) {
3528 ret = io_grab_files(req);
3529 if (ret)
3530 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003531 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03003532
3533 /*
3534 * Queued up for async execution, worker will release
3535 * submit reference when the iocb is actually submitted.
3536 */
3537 io_queue_async_work(req);
Jens Axboe4a0a7a12019-12-09 20:01:01 -07003538 goto done_req;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003539 }
Jens Axboee65ef562019-03-12 10:16:44 -06003540
Jens Axboefcb323c2019-10-24 12:39:47 -06003541err:
Jens Axboee65ef562019-03-12 10:16:44 -06003542 /* drop submission reference */
3543 io_put_req(req);
3544
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03003545 if (linked_timeout) {
Jens Axboe76a46e02019-11-10 23:34:16 -07003546 if (!ret)
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03003547 io_queue_linked_timeout(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07003548 else
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03003549 io_put_req(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07003550 }
3551
Jens Axboee65ef562019-03-12 10:16:44 -06003552 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06003553 if (ret) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07003554 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003555 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06003556 io_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06003557 }
Jens Axboe4a0a7a12019-12-09 20:01:01 -07003558done_req:
3559 if (nxt) {
3560 req = nxt;
3561 nxt = NULL;
3562 goto again;
3563 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07003564}
3565
Jens Axboe0e0702d2019-11-14 21:42:10 -07003566static void io_queue_sqe(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08003567{
3568 int ret;
3569
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003570 if (unlikely(req->ctx->drain_next)) {
3571 req->flags |= REQ_F_IO_DRAIN;
3572 req->ctx->drain_next = false;
3573 }
3574 req->ctx->drain_next = (req->flags & REQ_F_DRAIN_LINK);
3575
Jackie Liua197f662019-11-08 08:09:12 -07003576 ret = io_req_defer(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08003577 if (ret) {
3578 if (ret != -EIOCBQUEUED) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07003579 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003580 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07003581 io_double_put_req(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08003582 }
Jens Axboe0e0702d2019-11-14 21:42:10 -07003583 } else
3584 __io_queue_sqe(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08003585}
3586
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003587static inline void io_queue_link_head(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08003588{
Jens Axboe94ae5e72019-11-14 19:39:52 -07003589 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003590 io_cqring_add_event(req, -ECANCELED);
3591 io_double_put_req(req);
3592 } else
Jens Axboe0e0702d2019-11-14 21:42:10 -07003593 io_queue_sqe(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08003594}
3595
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003596#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
3597 IOSQE_IO_HARDLINK)
Jens Axboe9e645e112019-05-10 16:07:28 -06003598
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03003599static bool io_submit_sqe(struct io_kiocb *req, struct io_submit_state *state,
Jackie Liua197f662019-11-08 08:09:12 -07003600 struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06003601{
Jackie Liua197f662019-11-08 08:09:12 -07003602 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9e645e112019-05-10 16:07:28 -06003603 int ret;
3604
3605 /* enforce forwards compatibility on users */
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003606 if (unlikely(req->sqe->flags & ~SQE_VALID_FLAGS)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06003607 ret = -EINVAL;
Pavel Begunkov196be952019-11-07 01:41:06 +03003608 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06003609 }
3610
Jackie Liua197f662019-11-08 08:09:12 -07003611 ret = io_req_set_file(state, req);
Jens Axboe9e645e112019-05-10 16:07:28 -06003612 if (unlikely(ret)) {
3613err_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07003614 io_cqring_add_event(req, ret);
3615 io_double_put_req(req);
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03003616 return false;
Jens Axboe9e645e112019-05-10 16:07:28 -06003617 }
3618
Jens Axboe9e645e112019-05-10 16:07:28 -06003619 /*
3620 * If we already have a head request, queue this one for async
3621 * submittal once the head completes. If we don't have a head but
3622 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
3623 * submitted sync once the chain is complete. If none of those
3624 * conditions are true (normal request), then just queue it.
3625 */
3626 if (*link) {
3627 struct io_kiocb *prev = *link;
3628
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003629 if (req->sqe->flags & IOSQE_IO_DRAIN)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003630 (*link)->flags |= REQ_F_DRAIN_LINK | REQ_F_IO_DRAIN;
3631
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003632 if (req->sqe->flags & IOSQE_IO_HARDLINK)
3633 req->flags |= REQ_F_HARDLINK;
3634
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003635 if (io_alloc_async_ctx(req)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06003636 ret = -EAGAIN;
3637 goto err_req;
3638 }
3639
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003640 ret = io_req_defer_prep(req);
Jens Axboe2d283902019-12-04 11:08:05 -07003641 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003642 /* fail even hard links since we don't submit */
Jens Axboe2d283902019-12-04 11:08:05 -07003643 prev->flags |= REQ_F_FAIL_LINK;
Jens Axboef67676d2019-12-02 11:03:47 -07003644 goto err_req;
Jens Axboe2d283902019-12-04 11:08:05 -07003645 }
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02003646 trace_io_uring_link(ctx, req, prev);
Pavel Begunkov44932332019-12-05 16:16:35 +03003647 list_add_tail(&req->link_list, &prev->link_list);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003648 } else if (req->sqe->flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06003649 req->flags |= REQ_F_LINK;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003650 if (req->sqe->flags & IOSQE_IO_HARDLINK)
3651 req->flags |= REQ_F_HARDLINK;
Jens Axboe9e645e112019-05-10 16:07:28 -06003652
Jens Axboe9e645e112019-05-10 16:07:28 -06003653 INIT_LIST_HEAD(&req->link_list);
3654 *link = req;
3655 } else {
Jackie Liua197f662019-11-08 08:09:12 -07003656 io_queue_sqe(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06003657 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03003658
3659 return true;
Jens Axboe9e645e112019-05-10 16:07:28 -06003660}
3661
Jens Axboe9a56a232019-01-09 09:06:50 -07003662/*
3663 * Batched submission is done, ensure local IO is flushed out.
3664 */
3665static void io_submit_state_end(struct io_submit_state *state)
3666{
3667 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06003668 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07003669 if (state->free_reqs)
3670 kmem_cache_free_bulk(req_cachep, state->free_reqs,
3671 &state->reqs[state->cur_req]);
Jens Axboe9a56a232019-01-09 09:06:50 -07003672}
3673
3674/*
3675 * Start submission side cache.
3676 */
3677static void io_submit_state_start(struct io_submit_state *state,
Jackie Liu22efde52019-12-02 17:14:52 +08003678 unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07003679{
3680 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07003681 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07003682 state->file = NULL;
3683 state->ios_left = max_ios;
3684}
3685
Jens Axboe2b188cc2019-01-07 10:46:33 -07003686static void io_commit_sqring(struct io_ring_ctx *ctx)
3687{
Hristo Venev75b28af2019-08-26 17:23:46 +00003688 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003689
Hristo Venev75b28af2019-08-26 17:23:46 +00003690 if (ctx->cached_sq_head != READ_ONCE(rings->sq.head)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07003691 /*
3692 * Ensure any loads from the SQEs are done at this point,
3693 * since once we write the new head, the application could
3694 * write new data to them.
3695 */
Hristo Venev75b28af2019-08-26 17:23:46 +00003696 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003697 }
3698}
3699
3700/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08003701 * Fetch an sqe, if one is available. Note that req->sqe will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07003702 * that is mapped by userspace. This means that care needs to be taken to
3703 * ensure that reads are stable, as we cannot rely on userspace always
3704 * being a good citizen. If members of the sqe are validated and then later
3705 * used, it's important that those reads are done through READ_ONCE() to
3706 * prevent a re-load down the line.
3707 */
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003708static bool io_get_sqring(struct io_ring_ctx *ctx, struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07003709{
Hristo Venev75b28af2019-08-26 17:23:46 +00003710 struct io_rings *rings = ctx->rings;
3711 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07003712 unsigned head;
3713
3714 /*
3715 * The cached sq head (or cq tail) serves two purposes:
3716 *
3717 * 1) allows us to batch the cost of updating the user visible
3718 * head updates.
3719 * 2) allows the kernel side to track the head on its own, even
3720 * though the application is the one updating it.
3721 */
3722 head = ctx->cached_sq_head;
Stefan Bühlere523a292019-04-19 11:57:44 +02003723 /* make sure SQ entry isn't read before tail */
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03003724 if (unlikely(head == smp_load_acquire(&rings->sq.tail)))
Jens Axboe2b188cc2019-01-07 10:46:33 -07003725 return false;
3726
Hristo Venev75b28af2019-08-26 17:23:46 +00003727 head = READ_ONCE(sq_array[head & ctx->sq_mask]);
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03003728 if (likely(head < ctx->sq_entries)) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003729 /*
3730 * All io need record the previous position, if LINK vs DARIN,
3731 * it can be used to mark the position of the first IO in the
3732 * link list.
3733 */
3734 req->sequence = ctx->cached_sq_head;
3735 req->sqe = &ctx->sq_sqes[head];
Jens Axboed625c6e2019-12-17 19:53:05 -07003736 req->opcode = READ_ONCE(req->sqe->opcode);
3737 req->user_data = READ_ONCE(req->sqe->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003738 ctx->cached_sq_head++;
3739 return true;
3740 }
3741
3742 /* drop invalid entries */
3743 ctx->cached_sq_head++;
Jens Axboe498ccd92019-10-25 10:04:25 -06003744 ctx->cached_sq_dropped++;
3745 WRITE_ONCE(rings->sq_dropped, ctx->cached_sq_dropped);
Jens Axboe2b188cc2019-01-07 10:46:33 -07003746 return false;
3747}
3748
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003749static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkovae9428c2019-11-06 00:22:14 +03003750 struct file *ring_file, int ring_fd,
3751 struct mm_struct **mm, bool async)
Jens Axboe6c271ce2019-01-10 11:22:30 -07003752{
3753 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06003754 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06003755 int i, submitted = 0;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03003756 bool mm_fault = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003757
Jens Axboec4a2ed72019-11-21 21:01:26 -07003758 /* if we have a backlog and couldn't flush it all, return BUSY */
3759 if (!list_empty(&ctx->cq_overflow_list) &&
3760 !io_cqring_overflow_flush(ctx, false))
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003761 return -EBUSY;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003762
3763 if (nr > IO_PLUG_THRESHOLD) {
Jackie Liu22efde52019-12-02 17:14:52 +08003764 io_submit_state_start(&state, nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003765 statep = &state;
3766 }
3767
3768 for (i = 0; i < nr; i++) {
Pavel Begunkov196be952019-11-07 01:41:06 +03003769 struct io_kiocb *req;
Pavel Begunkov50585b92019-11-07 01:41:07 +03003770 unsigned int sqe_flags;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003771
Pavel Begunkov196be952019-11-07 01:41:06 +03003772 req = io_get_req(ctx, statep);
3773 if (unlikely(!req)) {
3774 if (!submitted)
3775 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003776 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06003777 }
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003778 if (!io_get_sqring(ctx, req)) {
Pavel Begunkov196be952019-11-07 01:41:06 +03003779 __io_free_req(req);
3780 break;
3781 }
Jens Axboe9e645e112019-05-10 16:07:28 -06003782
Jens Axboed625c6e2019-12-17 19:53:05 -07003783 if (io_req_needs_user(req) && !*mm) {
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03003784 mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm);
3785 if (!mm_fault) {
3786 use_mm(ctx->sqo_mm);
3787 *mm = ctx->sqo_mm;
3788 }
3789 }
3790
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03003791 submitted++;
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003792 sqe_flags = req->sqe->flags;
Pavel Begunkov50585b92019-11-07 01:41:07 +03003793
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03003794 req->ring_file = ring_file;
3795 req->ring_fd = ring_fd;
3796 req->has_user = *mm != NULL;
3797 req->in_async = async;
3798 req->needs_fixed_file = async;
Jens Axboed625c6e2019-12-17 19:53:05 -07003799 trace_io_uring_submit_sqe(ctx, req->user_data, true, async);
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03003800 if (!io_submit_sqe(req, statep, &link))
3801 break;
Pavel Begunkove5eb6362019-11-06 00:22:15 +03003802 /*
3803 * If previous wasn't linked and we have a linked command,
3804 * that's the end of the chain. Submit the previous link.
3805 */
Pavel Begunkovffbb8d62019-12-17 20:57:05 +03003806 if (!(sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK)) && link) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003807 io_queue_link_head(link);
Pavel Begunkove5eb6362019-11-06 00:22:15 +03003808 link = NULL;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003809 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07003810 }
3811
Jens Axboe9e645e112019-05-10 16:07:28 -06003812 if (link)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03003813 io_queue_link_head(link);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003814 if (statep)
3815 io_submit_state_end(&state);
3816
Pavel Begunkovae9428c2019-11-06 00:22:14 +03003817 /* Commit SQ ring head once we've consumed and submitted all SQEs */
3818 io_commit_sqring(ctx);
3819
Jens Axboe6c271ce2019-01-10 11:22:30 -07003820 return submitted;
3821}
3822
3823static int io_sq_thread(void *data)
3824{
Jens Axboe6c271ce2019-01-10 11:22:30 -07003825 struct io_ring_ctx *ctx = data;
3826 struct mm_struct *cur_mm = NULL;
Jens Axboe181e4482019-11-25 08:52:30 -07003827 const struct cred *old_cred;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003828 mm_segment_t old_fs;
3829 DEFINE_WAIT(wait);
3830 unsigned inflight;
3831 unsigned long timeout;
Jens Axboec1edbf52019-11-10 16:56:04 -07003832 int ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003833
Jens Axboe206aefd2019-11-07 18:27:42 -07003834 complete(&ctx->completions[1]);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08003835
Jens Axboe6c271ce2019-01-10 11:22:30 -07003836 old_fs = get_fs();
3837 set_fs(USER_DS);
Jens Axboe181e4482019-11-25 08:52:30 -07003838 old_cred = override_creds(ctx->creds);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003839
Jens Axboec1edbf52019-11-10 16:56:04 -07003840 ret = timeout = inflight = 0;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003841 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003842 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003843
3844 if (inflight) {
3845 unsigned nr_events = 0;
3846
3847 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe2b2ed972019-10-25 10:06:15 -06003848 /*
3849 * inflight is the count of the maximum possible
3850 * entries we submitted, but it can be smaller
3851 * if we dropped some of them. If we don't have
3852 * poll entries available, then we know that we
3853 * have nothing left to poll for. Reset the
3854 * inflight count to zero in that case.
3855 */
3856 mutex_lock(&ctx->uring_lock);
3857 if (!list_empty(&ctx->poll_list))
3858 __io_iopoll_check(ctx, &nr_events, 0);
3859 else
3860 inflight = 0;
3861 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07003862 } else {
3863 /*
3864 * Normal IO, just pretend everything completed.
3865 * We don't have to poll completions for that.
3866 */
3867 nr_events = inflight;
3868 }
3869
3870 inflight -= nr_events;
3871 if (!inflight)
3872 timeout = jiffies + ctx->sq_thread_idle;
3873 }
3874
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003875 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07003876
3877 /*
3878 * If submit got -EBUSY, flag us as needing the application
3879 * to enter the kernel to reap and flush events.
3880 */
3881 if (!to_submit || ret == -EBUSY) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07003882 /*
3883 * We're polling. If we're within the defined idle
3884 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07003885 * to sleep. The exception is if we got EBUSY doing
3886 * more IO, we should wait for the application to
3887 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07003888 */
Jens Axboec1edbf52019-11-10 16:56:04 -07003889 if (inflight ||
3890 (!time_after(jiffies, timeout) && ret != -EBUSY)) {
Jens Axboe9831a902019-09-19 09:48:55 -06003891 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07003892 continue;
3893 }
3894
3895 /*
3896 * Drop cur_mm before scheduling, we can't hold it for
3897 * long periods (or over schedule()). Do this before
3898 * adding ourselves to the waitqueue, as the unuse/drop
3899 * may sleep.
3900 */
3901 if (cur_mm) {
3902 unuse_mm(cur_mm);
3903 mmput(cur_mm);
3904 cur_mm = NULL;
3905 }
3906
3907 prepare_to_wait(&ctx->sqo_wait, &wait,
3908 TASK_INTERRUPTIBLE);
3909
3910 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00003911 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02003912 /* make sure to read SQ tail after writing flags */
3913 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07003914
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003915 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07003916 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003917 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07003918 finish_wait(&ctx->sqo_wait, &wait);
3919 break;
3920 }
3921 if (signal_pending(current))
3922 flush_signals(current);
3923 schedule();
3924 finish_wait(&ctx->sqo_wait, &wait);
3925
Hristo Venev75b28af2019-08-26 17:23:46 +00003926 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003927 continue;
3928 }
3929 finish_wait(&ctx->sqo_wait, &wait);
3930
Hristo Venev75b28af2019-08-26 17:23:46 +00003931 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003932 }
3933
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03003934 to_submit = min(to_submit, ctx->sq_entries);
Jens Axboe8a4955f2019-12-09 14:52:35 -07003935 mutex_lock(&ctx->uring_lock);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003936 ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true);
Jens Axboe8a4955f2019-12-09 14:52:35 -07003937 mutex_unlock(&ctx->uring_lock);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003938 if (ret > 0)
3939 inflight += ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07003940 }
3941
3942 set_fs(old_fs);
3943 if (cur_mm) {
3944 unuse_mm(cur_mm);
3945 mmput(cur_mm);
3946 }
Jens Axboe181e4482019-11-25 08:52:30 -07003947 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06003948
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02003949 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06003950
Jens Axboe6c271ce2019-01-10 11:22:30 -07003951 return 0;
3952}
3953
Jens Axboebda52162019-09-24 13:47:15 -06003954struct io_wait_queue {
3955 struct wait_queue_entry wq;
3956 struct io_ring_ctx *ctx;
3957 unsigned to_wait;
3958 unsigned nr_timeouts;
3959};
3960
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003961static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06003962{
3963 struct io_ring_ctx *ctx = iowq->ctx;
3964
3965 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08003966 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06003967 * started waiting. For timeouts, we always want to return to userspace,
3968 * regardless of event count.
3969 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003970 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06003971 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
3972}
3973
3974static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
3975 int wake_flags, void *key)
3976{
3977 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
3978 wq);
3979
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07003980 /* use noflush == true, as we can't safely rely on locking context */
3981 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06003982 return -1;
3983
3984 return autoremove_wake_function(curr, mode, wake_flags, key);
3985}
3986
Jens Axboe2b188cc2019-01-07 10:46:33 -07003987/*
3988 * Wait until events become available, if we don't already have some. The
3989 * application must reap them itself, as they reside on the shared cq ring.
3990 */
3991static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
3992 const sigset_t __user *sig, size_t sigsz)
3993{
Jens Axboebda52162019-09-24 13:47:15 -06003994 struct io_wait_queue iowq = {
3995 .wq = {
3996 .private = current,
3997 .func = io_wake_function,
3998 .entry = LIST_HEAD_INIT(iowq.wq.entry),
3999 },
4000 .ctx = ctx,
4001 .to_wait = min_events,
4002 };
Hristo Venev75b28af2019-08-26 17:23:46 +00004003 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08004004 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004005
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004006 if (io_cqring_events(ctx, false) >= min_events)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004007 return 0;
4008
4009 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01004010#ifdef CONFIG_COMPAT
4011 if (in_compat_syscall())
4012 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07004013 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01004014 else
4015#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07004016 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01004017
Jens Axboe2b188cc2019-01-07 10:46:33 -07004018 if (ret)
4019 return ret;
4020 }
4021
Jens Axboebda52162019-09-24 13:47:15 -06004022 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004023 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06004024 do {
4025 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
4026 TASK_INTERRUPTIBLE);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004027 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06004028 break;
4029 schedule();
4030 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08004031 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06004032 break;
4033 }
4034 } while (1);
4035 finish_wait(&ctx->wait, &iowq.wq);
4036
Jackie Liue9ffa5c2019-10-29 11:16:42 +08004037 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004038
Hristo Venev75b28af2019-08-26 17:23:46 +00004039 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004040}
4041
Jens Axboe6b063142019-01-10 22:13:58 -07004042static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
4043{
4044#if defined(CONFIG_UNIX)
4045 if (ctx->ring_sock) {
4046 struct sock *sock = ctx->ring_sock->sk;
4047 struct sk_buff *skb;
4048
4049 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
4050 kfree_skb(skb);
4051 }
4052#else
4053 int i;
4054
Jens Axboe65e19f52019-10-26 07:20:21 -06004055 for (i = 0; i < ctx->nr_user_files; i++) {
4056 struct file *file;
4057
4058 file = io_file_from_index(ctx, i);
4059 if (file)
4060 fput(file);
4061 }
Jens Axboe6b063142019-01-10 22:13:58 -07004062#endif
4063}
4064
4065static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
4066{
Jens Axboe65e19f52019-10-26 07:20:21 -06004067 unsigned nr_tables, i;
4068
4069 if (!ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07004070 return -ENXIO;
4071
4072 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06004073 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
4074 for (i = 0; i < nr_tables; i++)
4075 kfree(ctx->file_table[i].files);
4076 kfree(ctx->file_table);
4077 ctx->file_table = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07004078 ctx->nr_user_files = 0;
4079 return 0;
4080}
4081
Jens Axboe6c271ce2019-01-10 11:22:30 -07004082static void io_sq_thread_stop(struct io_ring_ctx *ctx)
4083{
4084 if (ctx->sqo_thread) {
Jens Axboe206aefd2019-11-07 18:27:42 -07004085 wait_for_completion(&ctx->completions[1]);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02004086 /*
4087 * The park is a bit of a work-around, without it we get
4088 * warning spews on shutdown with SQPOLL set and affinity
4089 * set to a single CPU.
4090 */
Jens Axboe06058632019-04-13 09:26:03 -06004091 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004092 kthread_stop(ctx->sqo_thread);
4093 ctx->sqo_thread = NULL;
4094 }
4095}
4096
Jens Axboe6b063142019-01-10 22:13:58 -07004097static void io_finish_async(struct io_ring_ctx *ctx)
4098{
Jens Axboe6c271ce2019-01-10 11:22:30 -07004099 io_sq_thread_stop(ctx);
4100
Jens Axboe561fb042019-10-24 07:25:42 -06004101 if (ctx->io_wq) {
4102 io_wq_destroy(ctx->io_wq);
4103 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07004104 }
4105}
4106
4107#if defined(CONFIG_UNIX)
4108static void io_destruct_skb(struct sk_buff *skb)
4109{
4110 struct io_ring_ctx *ctx = skb->sk->sk_user_data;
4111
Jens Axboe561fb042019-10-24 07:25:42 -06004112 if (ctx->io_wq)
4113 io_wq_flush(ctx->io_wq);
Jens Axboe8a997342019-10-09 14:40:13 -06004114
Jens Axboe6b063142019-01-10 22:13:58 -07004115 unix_destruct_scm(skb);
4116}
4117
4118/*
4119 * Ensure the UNIX gc is aware of our file set, so we are certain that
4120 * the io_uring can be safely unregistered on process exit, even if we have
4121 * loops in the file referencing.
4122 */
4123static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
4124{
4125 struct sock *sk = ctx->ring_sock->sk;
4126 struct scm_fp_list *fpl;
4127 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06004128 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07004129
4130 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
4131 unsigned long inflight = ctx->user->unix_inflight + nr;
4132
4133 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
4134 return -EMFILE;
4135 }
4136
4137 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
4138 if (!fpl)
4139 return -ENOMEM;
4140
4141 skb = alloc_skb(0, GFP_KERNEL);
4142 if (!skb) {
4143 kfree(fpl);
4144 return -ENOMEM;
4145 }
4146
4147 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07004148
Jens Axboe08a45172019-10-03 08:11:03 -06004149 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07004150 fpl->user = get_uid(ctx->user);
4151 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06004152 struct file *file = io_file_from_index(ctx, i + offset);
4153
4154 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06004155 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06004156 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06004157 unix_inflight(fpl->user, fpl->fp[nr_files]);
4158 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07004159 }
4160
Jens Axboe08a45172019-10-03 08:11:03 -06004161 if (nr_files) {
4162 fpl->max = SCM_MAX_FD;
4163 fpl->count = nr_files;
4164 UNIXCB(skb).fp = fpl;
4165 skb->destructor = io_destruct_skb;
4166 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
4167 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07004168
Jens Axboe08a45172019-10-03 08:11:03 -06004169 for (i = 0; i < nr_files; i++)
4170 fput(fpl->fp[i]);
4171 } else {
4172 kfree_skb(skb);
4173 kfree(fpl);
4174 }
Jens Axboe6b063142019-01-10 22:13:58 -07004175
4176 return 0;
4177}
4178
4179/*
4180 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
4181 * causes regular reference counting to break down. We rely on the UNIX
4182 * garbage collection to take care of this problem for us.
4183 */
4184static int io_sqe_files_scm(struct io_ring_ctx *ctx)
4185{
4186 unsigned left, total;
4187 int ret = 0;
4188
4189 total = 0;
4190 left = ctx->nr_user_files;
4191 while (left) {
4192 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07004193
4194 ret = __io_sqe_files_scm(ctx, this_files, total);
4195 if (ret)
4196 break;
4197 left -= this_files;
4198 total += this_files;
4199 }
4200
4201 if (!ret)
4202 return 0;
4203
4204 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06004205 struct file *file = io_file_from_index(ctx, total);
4206
4207 if (file)
4208 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07004209 total++;
4210 }
4211
4212 return ret;
4213}
4214#else
4215static int io_sqe_files_scm(struct io_ring_ctx *ctx)
4216{
4217 return 0;
4218}
4219#endif
4220
Jens Axboe65e19f52019-10-26 07:20:21 -06004221static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
4222 unsigned nr_files)
4223{
4224 int i;
4225
4226 for (i = 0; i < nr_tables; i++) {
4227 struct fixed_file_table *table = &ctx->file_table[i];
4228 unsigned this_files;
4229
4230 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
4231 table->files = kcalloc(this_files, sizeof(struct file *),
4232 GFP_KERNEL);
4233 if (!table->files)
4234 break;
4235 nr_files -= this_files;
4236 }
4237
4238 if (i == nr_tables)
4239 return 0;
4240
4241 for (i = 0; i < nr_tables; i++) {
4242 struct fixed_file_table *table = &ctx->file_table[i];
4243 kfree(table->files);
4244 }
4245 return 1;
4246}
4247
Jens Axboe6b063142019-01-10 22:13:58 -07004248static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
4249 unsigned nr_args)
4250{
4251 __s32 __user *fds = (__s32 __user *) arg;
Jens Axboe65e19f52019-10-26 07:20:21 -06004252 unsigned nr_tables;
Jens Axboe6b063142019-01-10 22:13:58 -07004253 int fd, ret = 0;
4254 unsigned i;
4255
Jens Axboe65e19f52019-10-26 07:20:21 -06004256 if (ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07004257 return -EBUSY;
4258 if (!nr_args)
4259 return -EINVAL;
4260 if (nr_args > IORING_MAX_FIXED_FILES)
4261 return -EMFILE;
4262
Jens Axboe65e19f52019-10-26 07:20:21 -06004263 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
4264 ctx->file_table = kcalloc(nr_tables, sizeof(struct fixed_file_table),
4265 GFP_KERNEL);
4266 if (!ctx->file_table)
Jens Axboe6b063142019-01-10 22:13:58 -07004267 return -ENOMEM;
4268
Jens Axboe65e19f52019-10-26 07:20:21 -06004269 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
4270 kfree(ctx->file_table);
Jens Axboe46568e92019-11-10 08:40:53 -07004271 ctx->file_table = NULL;
Jens Axboe65e19f52019-10-26 07:20:21 -06004272 return -ENOMEM;
4273 }
4274
Jens Axboe08a45172019-10-03 08:11:03 -06004275 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06004276 struct fixed_file_table *table;
4277 unsigned index;
4278
Jens Axboe6b063142019-01-10 22:13:58 -07004279 ret = -EFAULT;
4280 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
4281 break;
Jens Axboe08a45172019-10-03 08:11:03 -06004282 /* allow sparse sets */
4283 if (fd == -1) {
4284 ret = 0;
4285 continue;
4286 }
Jens Axboe6b063142019-01-10 22:13:58 -07004287
Jens Axboe65e19f52019-10-26 07:20:21 -06004288 table = &ctx->file_table[i >> IORING_FILE_TABLE_SHIFT];
4289 index = i & IORING_FILE_TABLE_MASK;
4290 table->files[index] = fget(fd);
Jens Axboe6b063142019-01-10 22:13:58 -07004291
4292 ret = -EBADF;
Jens Axboe65e19f52019-10-26 07:20:21 -06004293 if (!table->files[index])
Jens Axboe6b063142019-01-10 22:13:58 -07004294 break;
4295 /*
4296 * Don't allow io_uring instances to be registered. If UNIX
4297 * isn't enabled, then this causes a reference cycle and this
4298 * instance can never get freed. If UNIX is enabled we'll
4299 * handle it just fine, but there's still no point in allowing
4300 * a ring fd as it doesn't support regular read/write anyway.
4301 */
Jens Axboe65e19f52019-10-26 07:20:21 -06004302 if (table->files[index]->f_op == &io_uring_fops) {
4303 fput(table->files[index]);
Jens Axboe6b063142019-01-10 22:13:58 -07004304 break;
4305 }
Jens Axboe6b063142019-01-10 22:13:58 -07004306 ret = 0;
4307 }
4308
4309 if (ret) {
Jens Axboe65e19f52019-10-26 07:20:21 -06004310 for (i = 0; i < ctx->nr_user_files; i++) {
4311 struct file *file;
Jens Axboe6b063142019-01-10 22:13:58 -07004312
Jens Axboe65e19f52019-10-26 07:20:21 -06004313 file = io_file_from_index(ctx, i);
4314 if (file)
4315 fput(file);
4316 }
4317 for (i = 0; i < nr_tables; i++)
4318 kfree(ctx->file_table[i].files);
4319
4320 kfree(ctx->file_table);
4321 ctx->file_table = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07004322 ctx->nr_user_files = 0;
4323 return ret;
4324 }
4325
4326 ret = io_sqe_files_scm(ctx);
4327 if (ret)
4328 io_sqe_files_unregister(ctx);
4329
4330 return ret;
4331}
4332
Jens Axboec3a31e62019-10-03 13:59:56 -06004333static void io_sqe_file_unregister(struct io_ring_ctx *ctx, int index)
4334{
4335#if defined(CONFIG_UNIX)
Jens Axboe65e19f52019-10-26 07:20:21 -06004336 struct file *file = io_file_from_index(ctx, index);
Jens Axboec3a31e62019-10-03 13:59:56 -06004337 struct sock *sock = ctx->ring_sock->sk;
4338 struct sk_buff_head list, *head = &sock->sk_receive_queue;
4339 struct sk_buff *skb;
4340 int i;
4341
4342 __skb_queue_head_init(&list);
4343
4344 /*
4345 * Find the skb that holds this file in its SCM_RIGHTS. When found,
4346 * remove this entry and rearrange the file array.
4347 */
4348 skb = skb_dequeue(head);
4349 while (skb) {
4350 struct scm_fp_list *fp;
4351
4352 fp = UNIXCB(skb).fp;
4353 for (i = 0; i < fp->count; i++) {
4354 int left;
4355
4356 if (fp->fp[i] != file)
4357 continue;
4358
4359 unix_notinflight(fp->user, fp->fp[i]);
4360 left = fp->count - 1 - i;
4361 if (left) {
4362 memmove(&fp->fp[i], &fp->fp[i + 1],
4363 left * sizeof(struct file *));
4364 }
4365 fp->count--;
4366 if (!fp->count) {
4367 kfree_skb(skb);
4368 skb = NULL;
4369 } else {
4370 __skb_queue_tail(&list, skb);
4371 }
4372 fput(file);
4373 file = NULL;
4374 break;
4375 }
4376
4377 if (!file)
4378 break;
4379
4380 __skb_queue_tail(&list, skb);
4381
4382 skb = skb_dequeue(head);
4383 }
4384
4385 if (skb_peek(&list)) {
4386 spin_lock_irq(&head->lock);
4387 while ((skb = __skb_dequeue(&list)) != NULL)
4388 __skb_queue_tail(head, skb);
4389 spin_unlock_irq(&head->lock);
4390 }
4391#else
Jens Axboe65e19f52019-10-26 07:20:21 -06004392 fput(io_file_from_index(ctx, index));
Jens Axboec3a31e62019-10-03 13:59:56 -06004393#endif
4394}
4395
4396static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
4397 int index)
4398{
4399#if defined(CONFIG_UNIX)
4400 struct sock *sock = ctx->ring_sock->sk;
4401 struct sk_buff_head *head = &sock->sk_receive_queue;
4402 struct sk_buff *skb;
4403
4404 /*
4405 * See if we can merge this file into an existing skb SCM_RIGHTS
4406 * file set. If there's no room, fall back to allocating a new skb
4407 * and filling it in.
4408 */
4409 spin_lock_irq(&head->lock);
4410 skb = skb_peek(head);
4411 if (skb) {
4412 struct scm_fp_list *fpl = UNIXCB(skb).fp;
4413
4414 if (fpl->count < SCM_MAX_FD) {
4415 __skb_unlink(skb, head);
4416 spin_unlock_irq(&head->lock);
4417 fpl->fp[fpl->count] = get_file(file);
4418 unix_inflight(fpl->user, fpl->fp[fpl->count]);
4419 fpl->count++;
4420 spin_lock_irq(&head->lock);
4421 __skb_queue_head(head, skb);
4422 } else {
4423 skb = NULL;
4424 }
4425 }
4426 spin_unlock_irq(&head->lock);
4427
4428 if (skb) {
4429 fput(file);
4430 return 0;
4431 }
4432
4433 return __io_sqe_files_scm(ctx, 1, index);
4434#else
4435 return 0;
4436#endif
4437}
4438
4439static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
4440 unsigned nr_args)
4441{
4442 struct io_uring_files_update up;
4443 __s32 __user *fds;
4444 int fd, i, err;
4445 __u32 done;
4446
Jens Axboe65e19f52019-10-26 07:20:21 -06004447 if (!ctx->file_table)
Jens Axboec3a31e62019-10-03 13:59:56 -06004448 return -ENXIO;
4449 if (!nr_args)
4450 return -EINVAL;
4451 if (copy_from_user(&up, arg, sizeof(up)))
4452 return -EFAULT;
4453 if (check_add_overflow(up.offset, nr_args, &done))
4454 return -EOVERFLOW;
4455 if (done > ctx->nr_user_files)
4456 return -EINVAL;
4457
4458 done = 0;
4459 fds = (__s32 __user *) up.fds;
4460 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06004461 struct fixed_file_table *table;
4462 unsigned index;
4463
Jens Axboec3a31e62019-10-03 13:59:56 -06004464 err = 0;
4465 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
4466 err = -EFAULT;
4467 break;
4468 }
4469 i = array_index_nospec(up.offset, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06004470 table = &ctx->file_table[i >> IORING_FILE_TABLE_SHIFT];
4471 index = i & IORING_FILE_TABLE_MASK;
4472 if (table->files[index]) {
Jens Axboec3a31e62019-10-03 13:59:56 -06004473 io_sqe_file_unregister(ctx, i);
Jens Axboe65e19f52019-10-26 07:20:21 -06004474 table->files[index] = NULL;
Jens Axboec3a31e62019-10-03 13:59:56 -06004475 }
4476 if (fd != -1) {
4477 struct file *file;
4478
4479 file = fget(fd);
4480 if (!file) {
4481 err = -EBADF;
4482 break;
4483 }
4484 /*
4485 * Don't allow io_uring instances to be registered. If
4486 * UNIX isn't enabled, then this causes a reference
4487 * cycle and this instance can never get freed. If UNIX
4488 * is enabled we'll handle it just fine, but there's
4489 * still no point in allowing a ring fd as it doesn't
4490 * support regular read/write anyway.
4491 */
4492 if (file->f_op == &io_uring_fops) {
4493 fput(file);
4494 err = -EBADF;
4495 break;
4496 }
Jens Axboe65e19f52019-10-26 07:20:21 -06004497 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06004498 err = io_sqe_file_register(ctx, file, i);
4499 if (err)
4500 break;
4501 }
4502 nr_args--;
4503 done++;
4504 up.offset++;
4505 }
4506
4507 return done ? done : err;
4508}
4509
Jens Axboe7d723062019-11-12 22:31:31 -07004510static void io_put_work(struct io_wq_work *work)
4511{
4512 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
4513
4514 io_put_req(req);
4515}
4516
4517static void io_get_work(struct io_wq_work *work)
4518{
4519 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
4520
4521 refcount_inc(&req->refs);
4522}
4523
Jens Axboe6c271ce2019-01-10 11:22:30 -07004524static int io_sq_offload_start(struct io_ring_ctx *ctx,
4525 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004526{
Jens Axboe576a3472019-11-25 08:49:20 -07004527 struct io_wq_data data;
Jens Axboe561fb042019-10-24 07:25:42 -06004528 unsigned concurrency;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004529 int ret;
4530
Jens Axboe6c271ce2019-01-10 11:22:30 -07004531 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004532 mmgrab(current->mm);
4533 ctx->sqo_mm = current->mm;
4534
Jens Axboe6c271ce2019-01-10 11:22:30 -07004535 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06004536 ret = -EPERM;
4537 if (!capable(CAP_SYS_ADMIN))
4538 goto err;
4539
Jens Axboe917257d2019-04-13 09:28:55 -06004540 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
4541 if (!ctx->sq_thread_idle)
4542 ctx->sq_thread_idle = HZ;
4543
Jens Axboe6c271ce2019-01-10 11:22:30 -07004544 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06004545 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004546
Jens Axboe917257d2019-04-13 09:28:55 -06004547 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06004548 if (cpu >= nr_cpu_ids)
4549 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08004550 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06004551 goto err;
4552
Jens Axboe6c271ce2019-01-10 11:22:30 -07004553 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
4554 ctx, cpu,
4555 "io_uring-sq");
4556 } else {
4557 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
4558 "io_uring-sq");
4559 }
4560 if (IS_ERR(ctx->sqo_thread)) {
4561 ret = PTR_ERR(ctx->sqo_thread);
4562 ctx->sqo_thread = NULL;
4563 goto err;
4564 }
4565 wake_up_process(ctx->sqo_thread);
4566 } else if (p->flags & IORING_SETUP_SQ_AFF) {
4567 /* Can't have SQ_AFF without SQPOLL */
4568 ret = -EINVAL;
4569 goto err;
4570 }
4571
Jens Axboe576a3472019-11-25 08:49:20 -07004572 data.mm = ctx->sqo_mm;
4573 data.user = ctx->user;
Jens Axboe181e4482019-11-25 08:52:30 -07004574 data.creds = ctx->creds;
Jens Axboe576a3472019-11-25 08:49:20 -07004575 data.get_work = io_get_work;
4576 data.put_work = io_put_work;
4577
Jens Axboe561fb042019-10-24 07:25:42 -06004578 /* Do QD, or 4 * CPUS, whatever is smallest */
4579 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
Jens Axboe576a3472019-11-25 08:49:20 -07004580 ctx->io_wq = io_wq_create(concurrency, &data);
Jens Axboe975c99a52019-10-30 08:42:56 -06004581 if (IS_ERR(ctx->io_wq)) {
4582 ret = PTR_ERR(ctx->io_wq);
4583 ctx->io_wq = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004584 goto err;
4585 }
4586
4587 return 0;
4588err:
Jens Axboe54a91f32019-09-10 09:15:04 -06004589 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004590 mmdrop(ctx->sqo_mm);
4591 ctx->sqo_mm = NULL;
4592 return ret;
4593}
4594
4595static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
4596{
4597 atomic_long_sub(nr_pages, &user->locked_vm);
4598}
4599
4600static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
4601{
4602 unsigned long page_limit, cur_pages, new_pages;
4603
4604 /* Don't allow more pages than we can safely lock */
4605 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
4606
4607 do {
4608 cur_pages = atomic_long_read(&user->locked_vm);
4609 new_pages = cur_pages + nr_pages;
4610 if (new_pages > page_limit)
4611 return -ENOMEM;
4612 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
4613 new_pages) != cur_pages);
4614
4615 return 0;
4616}
4617
4618static void io_mem_free(void *ptr)
4619{
Mark Rutland52e04ef2019-04-30 17:30:21 +01004620 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004621
Mark Rutland52e04ef2019-04-30 17:30:21 +01004622 if (!ptr)
4623 return;
4624
4625 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004626 if (put_page_testzero(page))
4627 free_compound_page(page);
4628}
4629
4630static void *io_mem_alloc(size_t size)
4631{
4632 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
4633 __GFP_NORETRY;
4634
4635 return (void *) __get_free_pages(gfp_flags, get_order(size));
4636}
4637
Hristo Venev75b28af2019-08-26 17:23:46 +00004638static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
4639 size_t *sq_offset)
4640{
4641 struct io_rings *rings;
4642 size_t off, sq_array_size;
4643
4644 off = struct_size(rings, cqes, cq_entries);
4645 if (off == SIZE_MAX)
4646 return SIZE_MAX;
4647
4648#ifdef CONFIG_SMP
4649 off = ALIGN(off, SMP_CACHE_BYTES);
4650 if (off == 0)
4651 return SIZE_MAX;
4652#endif
4653
4654 sq_array_size = array_size(sizeof(u32), sq_entries);
4655 if (sq_array_size == SIZE_MAX)
4656 return SIZE_MAX;
4657
4658 if (check_add_overflow(off, sq_array_size, &off))
4659 return SIZE_MAX;
4660
4661 if (sq_offset)
4662 *sq_offset = off;
4663
4664 return off;
4665}
4666
Jens Axboe2b188cc2019-01-07 10:46:33 -07004667static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
4668{
Hristo Venev75b28af2019-08-26 17:23:46 +00004669 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004670
Hristo Venev75b28af2019-08-26 17:23:46 +00004671 pages = (size_t)1 << get_order(
4672 rings_size(sq_entries, cq_entries, NULL));
4673 pages += (size_t)1 << get_order(
4674 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07004675
Hristo Venev75b28af2019-08-26 17:23:46 +00004676 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004677}
4678
Jens Axboeedafcce2019-01-09 09:16:05 -07004679static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
4680{
4681 int i, j;
4682
4683 if (!ctx->user_bufs)
4684 return -ENXIO;
4685
4686 for (i = 0; i < ctx->nr_user_bufs; i++) {
4687 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
4688
4689 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbard27c4d3a2019-08-04 19:32:06 -07004690 put_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07004691
4692 if (ctx->account_mem)
4693 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004694 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07004695 imu->nr_bvecs = 0;
4696 }
4697
4698 kfree(ctx->user_bufs);
4699 ctx->user_bufs = NULL;
4700 ctx->nr_user_bufs = 0;
4701 return 0;
4702}
4703
4704static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
4705 void __user *arg, unsigned index)
4706{
4707 struct iovec __user *src;
4708
4709#ifdef CONFIG_COMPAT
4710 if (ctx->compat) {
4711 struct compat_iovec __user *ciovs;
4712 struct compat_iovec ciov;
4713
4714 ciovs = (struct compat_iovec __user *) arg;
4715 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
4716 return -EFAULT;
4717
Jens Axboed55e5f52019-12-11 16:12:15 -07004718 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07004719 dst->iov_len = ciov.iov_len;
4720 return 0;
4721 }
4722#endif
4723 src = (struct iovec __user *) arg;
4724 if (copy_from_user(dst, &src[index], sizeof(*dst)))
4725 return -EFAULT;
4726 return 0;
4727}
4728
4729static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
4730 unsigned nr_args)
4731{
4732 struct vm_area_struct **vmas = NULL;
4733 struct page **pages = NULL;
4734 int i, j, got_pages = 0;
4735 int ret = -EINVAL;
4736
4737 if (ctx->user_bufs)
4738 return -EBUSY;
4739 if (!nr_args || nr_args > UIO_MAXIOV)
4740 return -EINVAL;
4741
4742 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
4743 GFP_KERNEL);
4744 if (!ctx->user_bufs)
4745 return -ENOMEM;
4746
4747 for (i = 0; i < nr_args; i++) {
4748 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
4749 unsigned long off, start, end, ubuf;
4750 int pret, nr_pages;
4751 struct iovec iov;
4752 size_t size;
4753
4754 ret = io_copy_iov(ctx, &iov, arg, i);
4755 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03004756 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07004757
4758 /*
4759 * Don't impose further limits on the size and buffer
4760 * constraints here, we'll -EINVAL later when IO is
4761 * submitted if they are wrong.
4762 */
4763 ret = -EFAULT;
4764 if (!iov.iov_base || !iov.iov_len)
4765 goto err;
4766
4767 /* arbitrary limit, but we need something */
4768 if (iov.iov_len > SZ_1G)
4769 goto err;
4770
4771 ubuf = (unsigned long) iov.iov_base;
4772 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
4773 start = ubuf >> PAGE_SHIFT;
4774 nr_pages = end - start;
4775
4776 if (ctx->account_mem) {
4777 ret = io_account_mem(ctx->user, nr_pages);
4778 if (ret)
4779 goto err;
4780 }
4781
4782 ret = 0;
4783 if (!pages || nr_pages > got_pages) {
4784 kfree(vmas);
4785 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004786 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07004787 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004788 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07004789 sizeof(struct vm_area_struct *),
4790 GFP_KERNEL);
4791 if (!pages || !vmas) {
4792 ret = -ENOMEM;
4793 if (ctx->account_mem)
4794 io_unaccount_mem(ctx->user, nr_pages);
4795 goto err;
4796 }
4797 got_pages = nr_pages;
4798 }
4799
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004800 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07004801 GFP_KERNEL);
4802 ret = -ENOMEM;
4803 if (!imu->bvec) {
4804 if (ctx->account_mem)
4805 io_unaccount_mem(ctx->user, nr_pages);
4806 goto err;
4807 }
4808
4809 ret = 0;
4810 down_read(&current->mm->mmap_sem);
Ira Weiny932f4a62019-05-13 17:17:03 -07004811 pret = get_user_pages(ubuf, nr_pages,
4812 FOLL_WRITE | FOLL_LONGTERM,
4813 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07004814 if (pret == nr_pages) {
4815 /* don't support file backed memory */
4816 for (j = 0; j < nr_pages; j++) {
4817 struct vm_area_struct *vma = vmas[j];
4818
4819 if (vma->vm_file &&
4820 !is_file_hugepages(vma->vm_file)) {
4821 ret = -EOPNOTSUPP;
4822 break;
4823 }
4824 }
4825 } else {
4826 ret = pret < 0 ? pret : -EFAULT;
4827 }
4828 up_read(&current->mm->mmap_sem);
4829 if (ret) {
4830 /*
4831 * if we did partial map, or found file backed vmas,
4832 * release any pages we did get
4833 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07004834 if (pret > 0)
4835 put_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07004836 if (ctx->account_mem)
4837 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004838 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07004839 goto err;
4840 }
4841
4842 off = ubuf & ~PAGE_MASK;
4843 size = iov.iov_len;
4844 for (j = 0; j < nr_pages; j++) {
4845 size_t vec_len;
4846
4847 vec_len = min_t(size_t, size, PAGE_SIZE - off);
4848 imu->bvec[j].bv_page = pages[j];
4849 imu->bvec[j].bv_len = vec_len;
4850 imu->bvec[j].bv_offset = off;
4851 off = 0;
4852 size -= vec_len;
4853 }
4854 /* store original address for later verification */
4855 imu->ubuf = ubuf;
4856 imu->len = iov.iov_len;
4857 imu->nr_bvecs = nr_pages;
4858
4859 ctx->nr_user_bufs++;
4860 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004861 kvfree(pages);
4862 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07004863 return 0;
4864err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01004865 kvfree(pages);
4866 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07004867 io_sqe_buffer_unregister(ctx);
4868 return ret;
4869}
4870
Jens Axboe9b402842019-04-11 11:45:41 -06004871static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
4872{
4873 __s32 __user *fds = arg;
4874 int fd;
4875
4876 if (ctx->cq_ev_fd)
4877 return -EBUSY;
4878
4879 if (copy_from_user(&fd, fds, sizeof(*fds)))
4880 return -EFAULT;
4881
4882 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
4883 if (IS_ERR(ctx->cq_ev_fd)) {
4884 int ret = PTR_ERR(ctx->cq_ev_fd);
4885 ctx->cq_ev_fd = NULL;
4886 return ret;
4887 }
4888
4889 return 0;
4890}
4891
4892static int io_eventfd_unregister(struct io_ring_ctx *ctx)
4893{
4894 if (ctx->cq_ev_fd) {
4895 eventfd_ctx_put(ctx->cq_ev_fd);
4896 ctx->cq_ev_fd = NULL;
4897 return 0;
4898 }
4899
4900 return -ENXIO;
4901}
4902
Jens Axboe2b188cc2019-01-07 10:46:33 -07004903static void io_ring_ctx_free(struct io_ring_ctx *ctx)
4904{
Jens Axboe6b063142019-01-10 22:13:58 -07004905 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004906 if (ctx->sqo_mm)
4907 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07004908
4909 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07004910 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07004911 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06004912 io_eventfd_unregister(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07004913
Jens Axboe2b188cc2019-01-07 10:46:33 -07004914#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07004915 if (ctx->ring_sock) {
4916 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004917 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07004918 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004919#endif
4920
Hristo Venev75b28af2019-08-26 17:23:46 +00004921 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004922 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004923
4924 percpu_ref_exit(&ctx->refs);
4925 if (ctx->account_mem)
4926 io_unaccount_mem(ctx->user,
4927 ring_pages(ctx->sq_entries, ctx->cq_entries));
4928 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07004929 put_cred(ctx->creds);
Jens Axboe206aefd2019-11-07 18:27:42 -07004930 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -07004931 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07004932 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004933 kfree(ctx);
4934}
4935
4936static __poll_t io_uring_poll(struct file *file, poll_table *wait)
4937{
4938 struct io_ring_ctx *ctx = file->private_data;
4939 __poll_t mask = 0;
4940
4941 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02004942 /*
4943 * synchronizes with barrier from wq_has_sleeper call in
4944 * io_commit_cqring
4945 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07004946 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00004947 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
4948 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004949 mask |= EPOLLOUT | EPOLLWRNORM;
yangerkundaa5de52019-09-24 20:53:34 +08004950 if (READ_ONCE(ctx->rings->cq.head) != ctx->cached_cq_tail)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004951 mask |= EPOLLIN | EPOLLRDNORM;
4952
4953 return mask;
4954}
4955
4956static int io_uring_fasync(int fd, struct file *file, int on)
4957{
4958 struct io_ring_ctx *ctx = file->private_data;
4959
4960 return fasync_helper(fd, file, on, &ctx->cq_fasync);
4961}
4962
4963static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
4964{
4965 mutex_lock(&ctx->uring_lock);
4966 percpu_ref_kill(&ctx->refs);
4967 mutex_unlock(&ctx->uring_lock);
4968
Jens Axboe5262f562019-09-17 12:26:57 -06004969 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004970 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06004971
4972 if (ctx->io_wq)
4973 io_wq_cancel_all(ctx->io_wq);
4974
Jens Axboedef596e2019-01-09 08:59:42 -07004975 io_iopoll_reap_events(ctx);
Jens Axboe15dff282019-11-13 09:09:23 -07004976 /* if we failed setting up the ctx, we might not have any rings */
4977 if (ctx->rings)
4978 io_cqring_overflow_flush(ctx, true);
Jens Axboe206aefd2019-11-07 18:27:42 -07004979 wait_for_completion(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004980 io_ring_ctx_free(ctx);
4981}
4982
4983static int io_uring_release(struct inode *inode, struct file *file)
4984{
4985 struct io_ring_ctx *ctx = file->private_data;
4986
4987 file->private_data = NULL;
4988 io_ring_ctx_wait_and_kill(ctx);
4989 return 0;
4990}
4991
Jens Axboefcb323c2019-10-24 12:39:47 -06004992static void io_uring_cancel_files(struct io_ring_ctx *ctx,
4993 struct files_struct *files)
4994{
4995 struct io_kiocb *req;
4996 DEFINE_WAIT(wait);
4997
4998 while (!list_empty_careful(&ctx->inflight_list)) {
Jens Axboe768134d2019-11-10 20:30:53 -07004999 struct io_kiocb *cancel_req = NULL;
Jens Axboefcb323c2019-10-24 12:39:47 -06005000
5001 spin_lock_irq(&ctx->inflight_lock);
5002 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07005003 if (req->work.files != files)
5004 continue;
5005 /* req is being completed, ignore */
5006 if (!refcount_inc_not_zero(&req->refs))
5007 continue;
5008 cancel_req = req;
5009 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06005010 }
Jens Axboe768134d2019-11-10 20:30:53 -07005011 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06005012 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07005013 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06005014 spin_unlock_irq(&ctx->inflight_lock);
5015
Jens Axboe768134d2019-11-10 20:30:53 -07005016 /* We need to keep going until we don't find a matching req */
5017 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06005018 break;
Bob Liu2f6d9b92019-11-13 18:06:24 +08005019
5020 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
5021 io_put_req(cancel_req);
Jens Axboefcb323c2019-10-24 12:39:47 -06005022 schedule();
5023 }
Jens Axboe768134d2019-11-10 20:30:53 -07005024 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06005025}
5026
5027static int io_uring_flush(struct file *file, void *data)
5028{
5029 struct io_ring_ctx *ctx = file->private_data;
5030
5031 io_uring_cancel_files(ctx, data);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005032 if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) {
5033 io_cqring_overflow_flush(ctx, true);
Jens Axboefcb323c2019-10-24 12:39:47 -06005034 io_wq_cancel_all(ctx->io_wq);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005035 }
Jens Axboefcb323c2019-10-24 12:39:47 -06005036 return 0;
5037}
5038
Roman Penyaev6c5c2402019-11-28 12:53:22 +01005039static void *io_uring_validate_mmap_request(struct file *file,
5040 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005041{
Jens Axboe2b188cc2019-01-07 10:46:33 -07005042 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01005043 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005044 struct page *page;
5045 void *ptr;
5046
5047 switch (offset) {
5048 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00005049 case IORING_OFF_CQ_RING:
5050 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005051 break;
5052 case IORING_OFF_SQES:
5053 ptr = ctx->sq_sqes;
5054 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005055 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01005056 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005057 }
5058
5059 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07005060 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01005061 return ERR_PTR(-EINVAL);
5062
5063 return ptr;
5064}
5065
5066#ifdef CONFIG_MMU
5067
5068static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
5069{
5070 size_t sz = vma->vm_end - vma->vm_start;
5071 unsigned long pfn;
5072 void *ptr;
5073
5074 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
5075 if (IS_ERR(ptr))
5076 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005077
5078 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
5079 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
5080}
5081
Roman Penyaev6c5c2402019-11-28 12:53:22 +01005082#else /* !CONFIG_MMU */
5083
5084static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
5085{
5086 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
5087}
5088
5089static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
5090{
5091 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
5092}
5093
5094static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
5095 unsigned long addr, unsigned long len,
5096 unsigned long pgoff, unsigned long flags)
5097{
5098 void *ptr;
5099
5100 ptr = io_uring_validate_mmap_request(file, pgoff, len);
5101 if (IS_ERR(ptr))
5102 return PTR_ERR(ptr);
5103
5104 return (unsigned long) ptr;
5105}
5106
5107#endif /* !CONFIG_MMU */
5108
Jens Axboe2b188cc2019-01-07 10:46:33 -07005109SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
5110 u32, min_complete, u32, flags, const sigset_t __user *, sig,
5111 size_t, sigsz)
5112{
5113 struct io_ring_ctx *ctx;
5114 long ret = -EBADF;
5115 int submitted = 0;
5116 struct fd f;
5117
Jens Axboe6c271ce2019-01-10 11:22:30 -07005118 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07005119 return -EINVAL;
5120
5121 f = fdget(fd);
5122 if (!f.file)
5123 return -EBADF;
5124
5125 ret = -EOPNOTSUPP;
5126 if (f.file->f_op != &io_uring_fops)
5127 goto out_fput;
5128
5129 ret = -ENXIO;
5130 ctx = f.file->private_data;
5131 if (!percpu_ref_tryget(&ctx->refs))
5132 goto out_fput;
5133
Jens Axboe6c271ce2019-01-10 11:22:30 -07005134 /*
5135 * For SQ polling, the thread will do all submissions and completions.
5136 * Just return the requested submit count, and wake the thread if
5137 * we were asked to.
5138 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06005139 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005140 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07005141 if (!list_empty_careful(&ctx->cq_overflow_list))
5142 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005143 if (flags & IORING_ENTER_SQ_WAKEUP)
5144 wake_up(&ctx->sqo_wait);
5145 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06005146 } else if (to_submit) {
Pavel Begunkovae9428c2019-11-06 00:22:14 +03005147 struct mm_struct *cur_mm;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005148
Pavel Begunkovae9428c2019-11-06 00:22:14 +03005149 to_submit = min(to_submit, ctx->sq_entries);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005150 mutex_lock(&ctx->uring_lock);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03005151 /* already have mm, so io_submit_sqes() won't try to grab it */
5152 cur_mm = ctx->sqo_mm;
5153 submitted = io_submit_sqes(ctx, to_submit, f.file, fd,
5154 &cur_mm, false);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005155 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03005156
5157 if (submitted != to_submit)
5158 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005159 }
5160 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07005161 unsigned nr_events = 0;
5162
Jens Axboe2b188cc2019-01-07 10:46:33 -07005163 min_complete = min(min_complete, ctx->cq_entries);
5164
Jens Axboedef596e2019-01-09 08:59:42 -07005165 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07005166 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07005167 } else {
5168 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
5169 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07005170 }
5171
Pavel Begunkov7c504e652019-12-18 19:53:45 +03005172out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03005173 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005174out_fput:
5175 fdput(f);
5176 return submitted ? submitted : ret;
5177}
5178
5179static const struct file_operations io_uring_fops = {
5180 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06005181 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07005182 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01005183#ifndef CONFIG_MMU
5184 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
5185 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
5186#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07005187 .poll = io_uring_poll,
5188 .fasync = io_uring_fasync,
5189};
5190
5191static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
5192 struct io_uring_params *p)
5193{
Hristo Venev75b28af2019-08-26 17:23:46 +00005194 struct io_rings *rings;
5195 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005196
Hristo Venev75b28af2019-08-26 17:23:46 +00005197 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
5198 if (size == SIZE_MAX)
5199 return -EOVERFLOW;
5200
5201 rings = io_mem_alloc(size);
5202 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005203 return -ENOMEM;
5204
Hristo Venev75b28af2019-08-26 17:23:46 +00005205 ctx->rings = rings;
5206 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
5207 rings->sq_ring_mask = p->sq_entries - 1;
5208 rings->cq_ring_mask = p->cq_entries - 1;
5209 rings->sq_ring_entries = p->sq_entries;
5210 rings->cq_ring_entries = p->cq_entries;
5211 ctx->sq_mask = rings->sq_ring_mask;
5212 ctx->cq_mask = rings->cq_ring_mask;
5213 ctx->sq_entries = rings->sq_ring_entries;
5214 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005215
5216 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07005217 if (size == SIZE_MAX) {
5218 io_mem_free(ctx->rings);
5219 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005220 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07005221 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07005222
5223 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07005224 if (!ctx->sq_sqes) {
5225 io_mem_free(ctx->rings);
5226 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005227 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07005228 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07005229
Jens Axboe2b188cc2019-01-07 10:46:33 -07005230 return 0;
5231}
5232
5233/*
5234 * Allocate an anonymous fd, this is what constitutes the application
5235 * visible backing of an io_uring instance. The application mmaps this
5236 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
5237 * we have to tie this fd to a socket for file garbage collection purposes.
5238 */
5239static int io_uring_get_fd(struct io_ring_ctx *ctx)
5240{
5241 struct file *file;
5242 int ret;
5243
5244#if defined(CONFIG_UNIX)
5245 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
5246 &ctx->ring_sock);
5247 if (ret)
5248 return ret;
5249#endif
5250
5251 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
5252 if (ret < 0)
5253 goto err;
5254
5255 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
5256 O_RDWR | O_CLOEXEC);
5257 if (IS_ERR(file)) {
5258 put_unused_fd(ret);
5259 ret = PTR_ERR(file);
5260 goto err;
5261 }
5262
5263#if defined(CONFIG_UNIX)
5264 ctx->ring_sock->file = file;
Jens Axboe6b063142019-01-10 22:13:58 -07005265 ctx->ring_sock->sk->sk_user_data = ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005266#endif
5267 fd_install(ret, file);
5268 return ret;
5269err:
5270#if defined(CONFIG_UNIX)
5271 sock_release(ctx->ring_sock);
5272 ctx->ring_sock = NULL;
5273#endif
5274 return ret;
5275}
5276
5277static int io_uring_create(unsigned entries, struct io_uring_params *p)
5278{
5279 struct user_struct *user = NULL;
5280 struct io_ring_ctx *ctx;
5281 bool account_mem;
5282 int ret;
5283
5284 if (!entries || entries > IORING_MAX_ENTRIES)
5285 return -EINVAL;
5286
5287 /*
5288 * Use twice as many entries for the CQ ring. It's possible for the
5289 * application to drive a higher depth than the size of the SQ ring,
5290 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06005291 * some flexibility in overcommitting a bit. If the application has
5292 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
5293 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07005294 */
5295 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06005296 if (p->flags & IORING_SETUP_CQSIZE) {
5297 /*
5298 * If IORING_SETUP_CQSIZE is set, we do the same roundup
5299 * to a power-of-two, if it isn't already. We do NOT impose
5300 * any cq vs sq ring sizing.
5301 */
5302 if (p->cq_entries < p->sq_entries || p->cq_entries > IORING_MAX_CQ_ENTRIES)
5303 return -EINVAL;
5304 p->cq_entries = roundup_pow_of_two(p->cq_entries);
5305 } else {
5306 p->cq_entries = 2 * p->sq_entries;
5307 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07005308
5309 user = get_uid(current_user());
5310 account_mem = !capable(CAP_IPC_LOCK);
5311
5312 if (account_mem) {
5313 ret = io_account_mem(user,
5314 ring_pages(p->sq_entries, p->cq_entries));
5315 if (ret) {
5316 free_uid(user);
5317 return ret;
5318 }
5319 }
5320
5321 ctx = io_ring_ctx_alloc(p);
5322 if (!ctx) {
5323 if (account_mem)
5324 io_unaccount_mem(user, ring_pages(p->sq_entries,
5325 p->cq_entries));
5326 free_uid(user);
5327 return -ENOMEM;
5328 }
5329 ctx->compat = in_compat_syscall();
5330 ctx->account_mem = account_mem;
5331 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07005332 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07005333
5334 ret = io_allocate_scq_urings(ctx, p);
5335 if (ret)
5336 goto err;
5337
Jens Axboe6c271ce2019-01-10 11:22:30 -07005338 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005339 if (ret)
5340 goto err;
5341
Jens Axboe2b188cc2019-01-07 10:46:33 -07005342 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00005343 p->sq_off.head = offsetof(struct io_rings, sq.head);
5344 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
5345 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
5346 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
5347 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
5348 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
5349 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005350
5351 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00005352 p->cq_off.head = offsetof(struct io_rings, cq.head);
5353 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
5354 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
5355 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
5356 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
5357 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06005358
Jens Axboe044c1ab2019-10-28 09:15:33 -06005359 /*
5360 * Install ring fd as the very last thing, so we don't risk someone
5361 * having closed it before we finish setup
5362 */
5363 ret = io_uring_get_fd(ctx);
5364 if (ret < 0)
5365 goto err;
5366
Jens Axboeda8c9692019-12-02 18:51:26 -07005367 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
5368 IORING_FEAT_SUBMIT_STABLE;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02005369 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005370 return ret;
5371err:
5372 io_ring_ctx_wait_and_kill(ctx);
5373 return ret;
5374}
5375
5376/*
5377 * Sets up an aio uring context, and returns the fd. Applications asks for a
5378 * ring size, we return the actual sq/cq ring sizes (among other things) in the
5379 * params structure passed in.
5380 */
5381static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
5382{
5383 struct io_uring_params p;
5384 long ret;
5385 int i;
5386
5387 if (copy_from_user(&p, params, sizeof(p)))
5388 return -EFAULT;
5389 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
5390 if (p.resv[i])
5391 return -EINVAL;
5392 }
5393
Jens Axboe6c271ce2019-01-10 11:22:30 -07005394 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe33a107f2019-10-04 12:10:03 -06005395 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE))
Jens Axboe2b188cc2019-01-07 10:46:33 -07005396 return -EINVAL;
5397
5398 ret = io_uring_create(entries, &p);
5399 if (ret < 0)
5400 return ret;
5401
5402 if (copy_to_user(params, &p, sizeof(p)))
5403 return -EFAULT;
5404
5405 return ret;
5406}
5407
5408SYSCALL_DEFINE2(io_uring_setup, u32, entries,
5409 struct io_uring_params __user *, params)
5410{
5411 return io_uring_setup(entries, params);
5412}
5413
Jens Axboeedafcce2019-01-09 09:16:05 -07005414static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
5415 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06005416 __releases(ctx->uring_lock)
5417 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07005418{
5419 int ret;
5420
Jens Axboe35fa71a2019-04-22 10:23:23 -06005421 /*
5422 * We're inside the ring mutex, if the ref is already dying, then
5423 * someone else killed the ctx or is already going through
5424 * io_uring_register().
5425 */
5426 if (percpu_ref_is_dying(&ctx->refs))
5427 return -ENXIO;
5428
Jens Axboeedafcce2019-01-09 09:16:05 -07005429 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06005430
5431 /*
5432 * Drop uring mutex before waiting for references to exit. If another
5433 * thread is currently inside io_uring_enter() it might need to grab
5434 * the uring_lock to make progress. If we hold it here across the drain
5435 * wait, then we can deadlock. It's safe to drop the mutex here, since
5436 * no new references will come in after we've killed the percpu ref.
5437 */
5438 mutex_unlock(&ctx->uring_lock);
Jens Axboe206aefd2019-11-07 18:27:42 -07005439 wait_for_completion(&ctx->completions[0]);
Jens Axboeb19062a2019-04-15 10:49:38 -06005440 mutex_lock(&ctx->uring_lock);
Jens Axboeedafcce2019-01-09 09:16:05 -07005441
5442 switch (opcode) {
5443 case IORING_REGISTER_BUFFERS:
5444 ret = io_sqe_buffer_register(ctx, arg, nr_args);
5445 break;
5446 case IORING_UNREGISTER_BUFFERS:
5447 ret = -EINVAL;
5448 if (arg || nr_args)
5449 break;
5450 ret = io_sqe_buffer_unregister(ctx);
5451 break;
Jens Axboe6b063142019-01-10 22:13:58 -07005452 case IORING_REGISTER_FILES:
5453 ret = io_sqe_files_register(ctx, arg, nr_args);
5454 break;
5455 case IORING_UNREGISTER_FILES:
5456 ret = -EINVAL;
5457 if (arg || nr_args)
5458 break;
5459 ret = io_sqe_files_unregister(ctx);
5460 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06005461 case IORING_REGISTER_FILES_UPDATE:
5462 ret = io_sqe_files_update(ctx, arg, nr_args);
5463 break;
Jens Axboe9b402842019-04-11 11:45:41 -06005464 case IORING_REGISTER_EVENTFD:
5465 ret = -EINVAL;
5466 if (nr_args != 1)
5467 break;
5468 ret = io_eventfd_register(ctx, arg);
5469 break;
5470 case IORING_UNREGISTER_EVENTFD:
5471 ret = -EINVAL;
5472 if (arg || nr_args)
5473 break;
5474 ret = io_eventfd_unregister(ctx);
5475 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07005476 default:
5477 ret = -EINVAL;
5478 break;
5479 }
5480
5481 /* bring the ctx back to life */
Jens Axboe206aefd2019-11-07 18:27:42 -07005482 reinit_completion(&ctx->completions[0]);
Jens Axboeedafcce2019-01-09 09:16:05 -07005483 percpu_ref_reinit(&ctx->refs);
5484 return ret;
5485}
5486
5487SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
5488 void __user *, arg, unsigned int, nr_args)
5489{
5490 struct io_ring_ctx *ctx;
5491 long ret = -EBADF;
5492 struct fd f;
5493
5494 f = fdget(fd);
5495 if (!f.file)
5496 return -EBADF;
5497
5498 ret = -EOPNOTSUPP;
5499 if (f.file->f_op != &io_uring_fops)
5500 goto out_fput;
5501
5502 ctx = f.file->private_data;
5503
5504 mutex_lock(&ctx->uring_lock);
5505 ret = __io_uring_register(ctx, opcode, arg, nr_args);
5506 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02005507 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
5508 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07005509out_fput:
5510 fdput(f);
5511 return ret;
5512}
5513
Jens Axboe2b188cc2019-01-07 10:46:33 -07005514static int __init io_uring_init(void)
5515{
5516 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
5517 return 0;
5518};
5519__initcall(io_uring_init);