blob: d8d252b4c9be6f042ab3c145a5fb24545406428f [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 Axboe15b71ab2019-12-11 11:20:36 -070073#include <linux/namei.h>
74#include <linux/fsnotify.h>
Jens Axboe4840e412019-12-25 22:03:45 -070075#include <linux/fadvise.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070076
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020077#define CREATE_TRACE_POINTS
78#include <trace/events/io_uring.h>
79
Jens Axboe2b188cc2019-01-07 10:46:33 -070080#include <uapi/linux/io_uring.h>
81
82#include "internal.h"
Jens Axboe561fb042019-10-24 07:25:42 -060083#include "io-wq.h"
Jens Axboe2b188cc2019-01-07 10:46:33 -070084
Daniel Xu5277dea2019-09-14 14:23:45 -070085#define IORING_MAX_ENTRIES 32768
Jens Axboe33a107f2019-10-04 12:10:03 -060086#define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES)
Jens Axboe65e19f52019-10-26 07:20:21 -060087
88/*
89 * Shift of 9 is 512 entries, or exactly one page on 64-bit archs
90 */
91#define IORING_FILE_TABLE_SHIFT 9
92#define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT)
93#define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1)
94#define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE)
Jens Axboe2b188cc2019-01-07 10:46:33 -070095
96struct io_uring {
97 u32 head ____cacheline_aligned_in_smp;
98 u32 tail ____cacheline_aligned_in_smp;
99};
100
Stefan Bühler1e84b972019-04-24 23:54:16 +0200101/*
Hristo Venev75b28af2019-08-26 17:23:46 +0000102 * This data is shared with the application through the mmap at offsets
103 * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200104 *
105 * The offsets to the member fields are published through struct
106 * io_sqring_offsets when calling io_uring_setup.
107 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000108struct io_rings {
Stefan Bühler1e84b972019-04-24 23:54:16 +0200109 /*
110 * Head and tail offsets into the ring; the offsets need to be
111 * masked to get valid indices.
112 *
Hristo Venev75b28af2019-08-26 17:23:46 +0000113 * The kernel controls head of the sq ring and the tail of the cq ring,
114 * and the application controls tail of the sq ring and the head of the
115 * cq ring.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200116 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000117 struct io_uring sq, cq;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200118 /*
Hristo Venev75b28af2019-08-26 17:23:46 +0000119 * Bitmasks to apply to head and tail offsets (constant, equals
Stefan Bühler1e84b972019-04-24 23:54:16 +0200120 * ring_entries - 1)
121 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000122 u32 sq_ring_mask, cq_ring_mask;
123 /* Ring sizes (constant, power of 2) */
124 u32 sq_ring_entries, cq_ring_entries;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200125 /*
126 * Number of invalid entries dropped by the kernel due to
127 * invalid index stored in array
128 *
129 * Written by the kernel, shouldn't be modified by the
130 * application (i.e. get number of "new events" by comparing to
131 * cached value).
132 *
133 * After a new SQ head value was read by the application this
134 * counter includes all submissions that were dropped reaching
135 * the new SQ head (and possibly more).
136 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000137 u32 sq_dropped;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200138 /*
139 * Runtime flags
140 *
141 * Written by the kernel, shouldn't be modified by the
142 * application.
143 *
144 * The application needs a full memory barrier before checking
145 * for IORING_SQ_NEED_WAKEUP after updating the sq tail.
146 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000147 u32 sq_flags;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200148 /*
149 * Number of completion events lost because the queue was full;
150 * this should be avoided by the application by making sure
LimingWu0b4295b2019-12-05 20:18:18 +0800151 * there are not more requests pending than there is space in
Stefan Bühler1e84b972019-04-24 23:54:16 +0200152 * the completion queue.
153 *
154 * Written by the kernel, shouldn't be modified by the
155 * application (i.e. get number of "new events" by comparing to
156 * cached value).
157 *
158 * As completion events come in out of order this counter is not
159 * ordered with any other data.
160 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000161 u32 cq_overflow;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200162 /*
163 * Ring buffer of completion events.
164 *
165 * The kernel writes completion events fresh every time they are
166 * produced, so the application is allowed to modify pending
167 * entries.
168 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000169 struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700170};
171
Jens Axboeedafcce2019-01-09 09:16:05 -0700172struct io_mapped_ubuf {
173 u64 ubuf;
174 size_t len;
175 struct bio_vec *bvec;
176 unsigned int nr_bvecs;
177};
178
Jens Axboe65e19f52019-10-26 07:20:21 -0600179struct fixed_file_table {
180 struct file **files;
Jens Axboe31b51512019-01-18 22:56:34 -0700181};
182
Jens Axboe05f3fb32019-12-09 11:22:50 -0700183enum {
184 FFD_F_ATOMIC,
185};
186
187struct fixed_file_data {
188 struct fixed_file_table *table;
189 struct io_ring_ctx *ctx;
190
191 struct percpu_ref refs;
192 struct llist_head put_llist;
193 unsigned long state;
194 struct work_struct ref_work;
195 struct completion done;
196};
197
Jens Axboe2b188cc2019-01-07 10:46:33 -0700198struct io_ring_ctx {
199 struct {
200 struct percpu_ref refs;
201 } ____cacheline_aligned_in_smp;
202
203 struct {
204 unsigned int flags;
Jens Axboe69b3e542020-01-08 11:01:46 -0700205 int compat: 1;
206 int account_mem: 1;
207 int cq_overflow_flushed: 1;
208 int drain_next: 1;
Jens Axboef2842ab2020-01-08 11:04:00 -0700209 int eventfd_async: 1;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700210
Hristo Venev75b28af2019-08-26 17:23:46 +0000211 /*
212 * Ring buffer of indices into array of io_uring_sqe, which is
213 * mmapped by the application using the IORING_OFF_SQES offset.
214 *
215 * This indirection could e.g. be used to assign fixed
216 * io_uring_sqe entries to operations and only submit them to
217 * the queue when needed.
218 *
219 * The kernel modifies neither the indices array nor the entries
220 * array.
221 */
222 u32 *sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700223 unsigned cached_sq_head;
224 unsigned sq_entries;
225 unsigned sq_mask;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700226 unsigned sq_thread_idle;
Jens Axboe498ccd92019-10-25 10:04:25 -0600227 unsigned cached_sq_dropped;
Jens Axboe206aefd2019-11-07 18:27:42 -0700228 atomic_t cached_cq_overflow;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700229 unsigned long sq_check_overflow;
Jens Axboede0617e2019-04-06 21:51:27 -0600230
231 struct list_head defer_list;
Jens Axboe5262f562019-09-17 12:26:57 -0600232 struct list_head timeout_list;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700233 struct list_head cq_overflow_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700234
Jens Axboefcb323c2019-10-24 12:39:47 -0600235 wait_queue_head_t inflight_wait;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700236 struct io_uring_sqe *sq_sqes;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700237 } ____cacheline_aligned_in_smp;
238
Hristo Venev75b28af2019-08-26 17:23:46 +0000239 struct io_rings *rings;
240
Jens Axboe2b188cc2019-01-07 10:46:33 -0700241 /* IO offload */
Jens Axboe561fb042019-10-24 07:25:42 -0600242 struct io_wq *io_wq;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700243 struct task_struct *sqo_thread; /* if using sq thread polling */
244 struct mm_struct *sqo_mm;
245 wait_queue_head_t sqo_wait;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700246
Jens Axboe6b063142019-01-10 22:13:58 -0700247 /*
248 * If used, fixed file set. Writers must ensure that ->refs is dead,
249 * readers must ensure that ->refs is alive as long as the file* is
250 * used. Only updated through io_uring_register(2).
251 */
Jens Axboe05f3fb32019-12-09 11:22:50 -0700252 struct fixed_file_data *file_data;
Jens Axboe6b063142019-01-10 22:13:58 -0700253 unsigned nr_user_files;
254
Jens Axboeedafcce2019-01-09 09:16:05 -0700255 /* if used, fixed mapped user buffers */
256 unsigned nr_user_bufs;
257 struct io_mapped_ubuf *user_bufs;
258
Jens Axboe2b188cc2019-01-07 10:46:33 -0700259 struct user_struct *user;
260
Jens Axboe0b8c0ec2019-12-02 08:50:00 -0700261 const struct cred *creds;
Jens Axboe181e4482019-11-25 08:52:30 -0700262
Jens Axboe206aefd2019-11-07 18:27:42 -0700263 /* 0 is for ctx quiesce/reinit/free, 1 is for sqo_thread started */
264 struct completion *completions;
265
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700266 /* if all else fails... */
267 struct io_kiocb *fallback_req;
268
Jens Axboe206aefd2019-11-07 18:27:42 -0700269#if defined(CONFIG_UNIX)
270 struct socket *ring_sock;
271#endif
272
273 struct {
274 unsigned cached_cq_tail;
275 unsigned cq_entries;
276 unsigned cq_mask;
277 atomic_t cq_timeouts;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700278 unsigned long cq_check_overflow;
Jens Axboe206aefd2019-11-07 18:27:42 -0700279 struct wait_queue_head cq_wait;
280 struct fasync_struct *cq_fasync;
281 struct eventfd_ctx *cq_ev_fd;
282 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700283
284 struct {
285 struct mutex uring_lock;
286 wait_queue_head_t wait;
287 } ____cacheline_aligned_in_smp;
288
289 struct {
290 spinlock_t completion_lock;
Jens Axboee94f1412019-12-19 12:06:02 -0700291 struct llist_head poll_llist;
292
Jens Axboedef596e2019-01-09 08:59:42 -0700293 /*
294 * ->poll_list is protected by the ctx->uring_lock for
295 * io_uring instances that don't use IORING_SETUP_SQPOLL.
296 * For SQPOLL, only the single threaded io_sq_thread() will
297 * manipulate the list, hence no extra locking is needed there.
298 */
299 struct list_head poll_list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700300 struct hlist_head *cancel_hash;
301 unsigned cancel_hash_bits;
Jens Axboee94f1412019-12-19 12:06:02 -0700302 bool poll_multi_file;
Jens Axboefcb323c2019-10-24 12:39:47 -0600303
304 spinlock_t inflight_lock;
305 struct list_head inflight_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700306 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700307};
308
Jens Axboe09bb8392019-03-13 12:39:28 -0600309/*
310 * First field must be the file pointer in all the
311 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
312 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700313struct io_poll_iocb {
314 struct file *file;
Jens Axboe0969e782019-12-17 18:40:57 -0700315 union {
316 struct wait_queue_head *head;
317 u64 addr;
318 };
Jens Axboe221c5eb2019-01-17 09:41:58 -0700319 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600320 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700321 bool canceled;
Jens Axboe392edb42019-12-09 17:52:20 -0700322 struct wait_queue_entry wait;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700323};
324
Jens Axboeb5dba592019-12-11 14:02:38 -0700325struct io_close {
326 struct file *file;
327 struct file *put_file;
328 int fd;
329};
330
Jens Axboead8a48a2019-11-15 08:49:11 -0700331struct io_timeout_data {
332 struct io_kiocb *req;
333 struct hrtimer timer;
334 struct timespec64 ts;
335 enum hrtimer_mode mode;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +0300336 u32 seq_offset;
Jens Axboead8a48a2019-11-15 08:49:11 -0700337};
338
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700339struct io_accept {
340 struct file *file;
341 struct sockaddr __user *addr;
342 int __user *addr_len;
343 int flags;
344};
345
346struct io_sync {
347 struct file *file;
348 loff_t len;
349 loff_t off;
350 int flags;
Jens Axboed63d1b52019-12-10 10:38:56 -0700351 int mode;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700352};
353
Jens Axboefbf23842019-12-17 18:45:56 -0700354struct io_cancel {
355 struct file *file;
356 u64 addr;
357};
358
Jens Axboeb29472e2019-12-17 18:50:29 -0700359struct io_timeout {
360 struct file *file;
361 u64 addr;
362 int flags;
Jens Axboe26a61672019-12-20 09:02:01 -0700363 unsigned count;
Jens Axboeb29472e2019-12-17 18:50:29 -0700364};
365
Jens Axboe9adbd452019-12-20 08:45:55 -0700366struct io_rw {
367 /* NOTE: kiocb has the file as the first member, so don't do it here */
368 struct kiocb kiocb;
369 u64 addr;
370 u64 len;
371};
372
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700373struct io_connect {
374 struct file *file;
375 struct sockaddr __user *addr;
376 int addr_len;
377};
378
Jens Axboee47293f2019-12-20 08:58:21 -0700379struct io_sr_msg {
380 struct file *file;
Jens Axboefddafac2020-01-04 20:19:44 -0700381 union {
382 struct user_msghdr __user *msg;
383 void __user *buf;
384 };
Jens Axboee47293f2019-12-20 08:58:21 -0700385 int msg_flags;
Jens Axboefddafac2020-01-04 20:19:44 -0700386 size_t len;
Jens Axboee47293f2019-12-20 08:58:21 -0700387};
388
Jens Axboe15b71ab2019-12-11 11:20:36 -0700389struct io_open {
390 struct file *file;
391 int dfd;
Jens Axboeeddc7ef2019-12-13 21:18:10 -0700392 union {
Jens Axboeeddc7ef2019-12-13 21:18:10 -0700393 unsigned mask;
394 };
Jens Axboe15b71ab2019-12-11 11:20:36 -0700395 const char __user *fname;
396 struct filename *filename;
Jens Axboeeddc7ef2019-12-13 21:18:10 -0700397 struct statx __user *buffer;
Jens Axboec12cedf2020-01-08 17:41:21 -0700398 struct open_how how;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700399};
400
Jens Axboe05f3fb32019-12-09 11:22:50 -0700401struct io_files_update {
402 struct file *file;
403 u64 arg;
404 u32 nr_args;
405 u32 offset;
406};
407
Jens Axboe4840e412019-12-25 22:03:45 -0700408struct io_fadvise {
409 struct file *file;
410 u64 offset;
411 u32 len;
412 u32 advice;
413};
414
Jens Axboec1ca7572019-12-25 22:18:28 -0700415struct io_madvise {
416 struct file *file;
417 u64 addr;
418 u32 len;
419 u32 advice;
420};
421
Jens Axboef499a022019-12-02 16:28:46 -0700422struct io_async_connect {
423 struct sockaddr_storage address;
424};
425
Jens Axboe03b12302019-12-02 18:50:25 -0700426struct io_async_msghdr {
427 struct iovec fast_iov[UIO_FASTIOV];
428 struct iovec *iov;
429 struct sockaddr __user *uaddr;
430 struct msghdr msg;
431};
432
Jens Axboef67676d2019-12-02 11:03:47 -0700433struct io_async_rw {
434 struct iovec fast_iov[UIO_FASTIOV];
435 struct iovec *iov;
436 ssize_t nr_segs;
437 ssize_t size;
438};
439
Jens Axboe15b71ab2019-12-11 11:20:36 -0700440struct io_async_open {
441 struct filename *filename;
442};
443
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700444struct io_async_ctx {
Jens Axboef67676d2019-12-02 11:03:47 -0700445 union {
446 struct io_async_rw rw;
Jens Axboe03b12302019-12-02 18:50:25 -0700447 struct io_async_msghdr msg;
Jens Axboef499a022019-12-02 16:28:46 -0700448 struct io_async_connect connect;
Jens Axboe2d283902019-12-04 11:08:05 -0700449 struct io_timeout_data timeout;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700450 struct io_async_open open;
Jens Axboef67676d2019-12-02 11:03:47 -0700451 };
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700452};
453
Jens Axboe09bb8392019-03-13 12:39:28 -0600454/*
455 * NOTE! Each of the iocb union members has the file pointer
456 * as the first entry in their struct definition. So you can
457 * access the file pointer through any of the sub-structs,
458 * or directly as just 'ki_filp' in this struct.
459 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700460struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700461 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600462 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700463 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700464 struct io_poll_iocb poll;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700465 struct io_accept accept;
466 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700467 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700468 struct io_timeout timeout;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700469 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700470 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700471 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700472 struct io_close close;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700473 struct io_files_update files_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700474 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700475 struct io_madvise madvise;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700476 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700477
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700478 struct io_async_ctx *io;
Jens Axboee94f1412019-12-19 12:06:02 -0700479 union {
480 /*
481 * ring_file is only used in the submission path, and
482 * llist_node is only used for poll deferred completions
483 */
484 struct file *ring_file;
485 struct llist_node llist_node;
486 };
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300487 int ring_fd;
488 bool has_user;
489 bool in_async;
490 bool needs_fixed_file;
Jens Axboed625c6e2019-12-17 19:53:05 -0700491 u8 opcode;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700492
493 struct io_ring_ctx *ctx;
Jens Axboeeac406c2019-11-14 12:09:58 -0700494 union {
495 struct list_head list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700496 struct hlist_node hash_node;
Jens Axboeeac406c2019-11-14 12:09:58 -0700497 };
Jens Axboe9e645e112019-05-10 16:07:28 -0600498 struct list_head link_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700499 unsigned int flags;
Jens Axboec16361c2019-01-17 08:39:48 -0700500 refcount_t refs;
Stefan Bühler8449eed2019-04-27 20:34:19 +0200501#define REQ_F_NOWAIT 1 /* must not punt to workers */
Jens Axboedef596e2019-01-09 08:59:42 -0700502#define REQ_F_IOPOLL_COMPLETED 2 /* polled IO has completed */
Jens Axboe6b063142019-01-10 22:13:58 -0700503#define REQ_F_FIXED_FILE 4 /* ctx owns file */
Jens Axboe4d7dd462019-11-20 13:03:52 -0700504#define REQ_F_LINK_NEXT 8 /* already grabbed next link */
Stefan Bühlere2033e32019-05-11 19:08:01 +0200505#define REQ_F_IO_DRAIN 16 /* drain existing IO first */
506#define REQ_F_IO_DRAINED 32 /* drain done */
Jens Axboe9e645e112019-05-10 16:07:28 -0600507#define REQ_F_LINK 64 /* linked sqes */
Jens Axboe2665abf2019-11-05 12:40:47 -0700508#define REQ_F_LINK_TIMEOUT 128 /* has linked timeout */
Zhengyuan Liuf7b76ac2019-07-16 23:26:14 +0800509#define REQ_F_FAIL_LINK 256 /* fail rest of links */
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +0300510#define REQ_F_DRAIN_LINK 512 /* link should be fully drained */
Jens Axboe5262f562019-09-17 12:26:57 -0600511#define REQ_F_TIMEOUT 1024 /* timeout request */
Jens Axboe491381ce2019-10-17 09:20:46 -0600512#define REQ_F_ISREG 2048 /* regular file */
513#define REQ_F_MUST_PUNT 4096 /* must be punted even for NONBLOCK */
Jens Axboe93bd25b2019-11-11 23:34:31 -0700514#define REQ_F_TIMEOUT_NOSEQ 8192 /* no timeout sequence */
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800515#define REQ_F_INFLIGHT 16384 /* on inflight list */
516#define REQ_F_COMP_LOCKED 32768 /* completion under lock */
Jens Axboe4e88d6e2019-12-07 20:59:47 -0700517#define REQ_F_HARDLINK 65536 /* doesn't sever on completion < 0 */
Jens Axboece35a472019-12-17 08:04:44 -0700518#define REQ_F_FORCE_ASYNC 131072 /* IOSQE_ASYNC */
Jens Axboeba042912019-12-25 16:33:42 -0700519#define REQ_F_CUR_POS 262144 /* read/write uses file position */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700520 u64 user_data;
Jens Axboe9e645e112019-05-10 16:07:28 -0600521 u32 result;
Jens Axboede0617e2019-04-06 21:51:27 -0600522 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700523
Jens Axboefcb323c2019-10-24 12:39:47 -0600524 struct list_head inflight_entry;
525
Jens Axboe561fb042019-10-24 07:25:42 -0600526 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700527};
528
529#define IO_PLUG_THRESHOLD 2
Jens Axboedef596e2019-01-09 08:59:42 -0700530#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700531
Jens Axboe9a56a232019-01-09 09:06:50 -0700532struct io_submit_state {
533 struct blk_plug plug;
534
535 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700536 * io_kiocb alloc cache
537 */
538 void *reqs[IO_IOPOLL_BATCH];
539 unsigned int free_reqs;
540 unsigned int cur_req;
541
542 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700543 * File reference cache
544 */
545 struct file *file;
546 unsigned int fd;
547 unsigned int has_refs;
548 unsigned int used_refs;
549 unsigned int ios_left;
550};
551
Jens Axboed3656342019-12-18 09:50:26 -0700552struct io_op_def {
553 /* needs req->io allocated for deferral/async */
554 unsigned async_ctx : 1;
555 /* needs current->mm setup, does mm access */
556 unsigned needs_mm : 1;
557 /* needs req->file assigned */
558 unsigned needs_file : 1;
559 /* needs req->file assigned IFF fd is >= 0 */
560 unsigned fd_non_neg : 1;
561 /* hash wq insertion if file is a regular file */
562 unsigned hash_reg_file : 1;
563 /* unbound wq insertion if file is a non-regular file */
564 unsigned unbound_nonreg_file : 1;
565};
566
567static const struct io_op_def io_op_defs[] = {
568 {
569 /* IORING_OP_NOP */
570 },
571 {
572 /* IORING_OP_READV */
573 .async_ctx = 1,
574 .needs_mm = 1,
575 .needs_file = 1,
576 .unbound_nonreg_file = 1,
577 },
578 {
579 /* IORING_OP_WRITEV */
580 .async_ctx = 1,
581 .needs_mm = 1,
582 .needs_file = 1,
583 .hash_reg_file = 1,
584 .unbound_nonreg_file = 1,
585 },
586 {
587 /* IORING_OP_FSYNC */
588 .needs_file = 1,
589 },
590 {
591 /* IORING_OP_READ_FIXED */
592 .needs_file = 1,
593 .unbound_nonreg_file = 1,
594 },
595 {
596 /* IORING_OP_WRITE_FIXED */
597 .needs_file = 1,
598 .hash_reg_file = 1,
599 .unbound_nonreg_file = 1,
600 },
601 {
602 /* IORING_OP_POLL_ADD */
603 .needs_file = 1,
604 .unbound_nonreg_file = 1,
605 },
606 {
607 /* IORING_OP_POLL_REMOVE */
608 },
609 {
610 /* IORING_OP_SYNC_FILE_RANGE */
611 .needs_file = 1,
612 },
613 {
614 /* IORING_OP_SENDMSG */
615 .async_ctx = 1,
616 .needs_mm = 1,
617 .needs_file = 1,
618 .unbound_nonreg_file = 1,
619 },
620 {
621 /* IORING_OP_RECVMSG */
622 .async_ctx = 1,
623 .needs_mm = 1,
624 .needs_file = 1,
625 .unbound_nonreg_file = 1,
626 },
627 {
628 /* IORING_OP_TIMEOUT */
629 .async_ctx = 1,
630 .needs_mm = 1,
631 },
632 {
633 /* IORING_OP_TIMEOUT_REMOVE */
634 },
635 {
636 /* IORING_OP_ACCEPT */
637 .needs_mm = 1,
638 .needs_file = 1,
639 .unbound_nonreg_file = 1,
640 },
641 {
642 /* IORING_OP_ASYNC_CANCEL */
643 },
644 {
645 /* IORING_OP_LINK_TIMEOUT */
646 .async_ctx = 1,
647 .needs_mm = 1,
648 },
649 {
650 /* IORING_OP_CONNECT */
651 .async_ctx = 1,
652 .needs_mm = 1,
653 .needs_file = 1,
654 .unbound_nonreg_file = 1,
655 },
656 {
657 /* IORING_OP_FALLOCATE */
658 .needs_file = 1,
659 },
660 {
661 /* IORING_OP_OPENAT */
662 .needs_file = 1,
663 .fd_non_neg = 1,
664 },
665 {
666 /* IORING_OP_CLOSE */
667 .needs_file = 1,
668 },
669 {
670 /* IORING_OP_FILES_UPDATE */
671 .needs_mm = 1,
672 },
673 {
674 /* IORING_OP_STATX */
675 .needs_mm = 1,
676 .needs_file = 1,
677 .fd_non_neg = 1,
678 },
Jens Axboe3a6820f2019-12-22 15:19:35 -0700679 {
680 /* IORING_OP_READ */
681 .needs_mm = 1,
682 .needs_file = 1,
683 .unbound_nonreg_file = 1,
684 },
685 {
686 /* IORING_OP_WRITE */
687 .needs_mm = 1,
688 .needs_file = 1,
689 .unbound_nonreg_file = 1,
690 },
Jens Axboe4840e412019-12-25 22:03:45 -0700691 {
692 /* IORING_OP_FADVISE */
693 .needs_file = 1,
694 },
Jens Axboec1ca7572019-12-25 22:18:28 -0700695 {
696 /* IORING_OP_MADVISE */
697 .needs_mm = 1,
698 },
Jens Axboefddafac2020-01-04 20:19:44 -0700699 {
700 /* IORING_OP_SEND */
701 .needs_mm = 1,
702 .needs_file = 1,
703 .unbound_nonreg_file = 1,
704 },
705 {
706 /* IORING_OP_RECV */
707 .needs_mm = 1,
708 .needs_file = 1,
709 .unbound_nonreg_file = 1,
710 },
Jens Axboed3656342019-12-18 09:50:26 -0700711};
712
Jens Axboe561fb042019-10-24 07:25:42 -0600713static void io_wq_submit_work(struct io_wq_work **workptr);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700714static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800715static void io_put_req(struct io_kiocb *req);
Jens Axboe978db572019-11-14 22:39:04 -0700716static void __io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700717static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
718static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -0700719static int __io_sqe_files_update(struct io_ring_ctx *ctx,
720 struct io_uring_files_update *ip,
721 unsigned nr_args);
Jens Axboede0617e2019-04-06 21:51:27 -0600722
Jens Axboe2b188cc2019-01-07 10:46:33 -0700723static struct kmem_cache *req_cachep;
724
725static const struct file_operations io_uring_fops;
726
727struct sock *io_uring_get_socket(struct file *file)
728{
729#if defined(CONFIG_UNIX)
730 if (file->f_op == &io_uring_fops) {
731 struct io_ring_ctx *ctx = file->private_data;
732
733 return ctx->ring_sock->sk;
734 }
735#endif
736 return NULL;
737}
738EXPORT_SYMBOL(io_uring_get_socket);
739
740static void io_ring_ctx_ref_free(struct percpu_ref *ref)
741{
742 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
743
Jens Axboe206aefd2019-11-07 18:27:42 -0700744 complete(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700745}
746
747static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
748{
749 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -0700750 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700751
752 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
753 if (!ctx)
754 return NULL;
755
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700756 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
757 if (!ctx->fallback_req)
758 goto err;
759
Jens Axboe206aefd2019-11-07 18:27:42 -0700760 ctx->completions = kmalloc(2 * sizeof(struct completion), GFP_KERNEL);
761 if (!ctx->completions)
762 goto err;
763
Jens Axboe78076bb2019-12-04 19:56:40 -0700764 /*
765 * Use 5 bits less than the max cq entries, that should give us around
766 * 32 entries per hash list if totally full and uniformly spread.
767 */
768 hash_bits = ilog2(p->cq_entries);
769 hash_bits -= 5;
770 if (hash_bits <= 0)
771 hash_bits = 1;
772 ctx->cancel_hash_bits = hash_bits;
773 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
774 GFP_KERNEL);
775 if (!ctx->cancel_hash)
776 goto err;
777 __hash_init(ctx->cancel_hash, 1U << hash_bits);
778
Roman Gushchin21482892019-05-07 10:01:48 -0700779 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -0700780 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
781 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700782
783 ctx->flags = p->flags;
784 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700785 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe206aefd2019-11-07 18:27:42 -0700786 init_completion(&ctx->completions[0]);
787 init_completion(&ctx->completions[1]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700788 mutex_init(&ctx->uring_lock);
789 init_waitqueue_head(&ctx->wait);
790 spin_lock_init(&ctx->completion_lock);
Jens Axboee94f1412019-12-19 12:06:02 -0700791 init_llist_head(&ctx->poll_llist);
Jens Axboedef596e2019-01-09 08:59:42 -0700792 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboede0617e2019-04-06 21:51:27 -0600793 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -0600794 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -0600795 init_waitqueue_head(&ctx->inflight_wait);
796 spin_lock_init(&ctx->inflight_lock);
797 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700798 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -0700799err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700800 if (ctx->fallback_req)
801 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe206aefd2019-11-07 18:27:42 -0700802 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -0700803 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -0700804 kfree(ctx);
805 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700806}
807
Bob Liu9d858b22019-11-13 18:06:25 +0800808static inline bool __req_need_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -0600809{
Jackie Liua197f662019-11-08 08:09:12 -0700810 struct io_ring_ctx *ctx = req->ctx;
811
Jens Axboe498ccd92019-10-25 10:04:25 -0600812 return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped
813 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboede0617e2019-04-06 21:51:27 -0600814}
815
Bob Liu9d858b22019-11-13 18:06:25 +0800816static inline bool req_need_defer(struct io_kiocb *req)
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600817{
Bob Liu9d858b22019-11-13 18:06:25 +0800818 if ((req->flags & (REQ_F_IO_DRAIN|REQ_F_IO_DRAINED)) == REQ_F_IO_DRAIN)
819 return __req_need_defer(req);
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600820
Bob Liu9d858b22019-11-13 18:06:25 +0800821 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600822}
823
824static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -0600825{
826 struct io_kiocb *req;
827
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600828 req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list);
Bob Liu9d858b22019-11-13 18:06:25 +0800829 if (req && !req_need_defer(req)) {
Jens Axboede0617e2019-04-06 21:51:27 -0600830 list_del_init(&req->list);
831 return req;
832 }
833
834 return NULL;
835}
836
Jens Axboe5262f562019-09-17 12:26:57 -0600837static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx)
838{
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600839 struct io_kiocb *req;
840
841 req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list);
Jens Axboe93bd25b2019-11-11 23:34:31 -0700842 if (req) {
843 if (req->flags & REQ_F_TIMEOUT_NOSEQ)
844 return NULL;
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800845 if (!__req_need_defer(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -0700846 list_del_init(&req->list);
847 return req;
848 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600849 }
850
851 return NULL;
Jens Axboe5262f562019-09-17 12:26:57 -0600852}
853
Jens Axboede0617e2019-04-06 21:51:27 -0600854static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700855{
Hristo Venev75b28af2019-08-26 17:23:46 +0000856 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700857
Hristo Venev75b28af2019-08-26 17:23:46 +0000858 if (ctx->cached_cq_tail != READ_ONCE(rings->cq.tail)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700859 /* order cqe stores with ring update */
Hristo Venev75b28af2019-08-26 17:23:46 +0000860 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700861
Jens Axboe2b188cc2019-01-07 10:46:33 -0700862 if (wq_has_sleeper(&ctx->cq_wait)) {
863 wake_up_interruptible(&ctx->cq_wait);
864 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
865 }
866 }
867}
868
Jens Axboe94ae5e72019-11-14 19:39:52 -0700869static inline bool io_prep_async_work(struct io_kiocb *req,
870 struct io_kiocb **link)
Jens Axboe561fb042019-10-24 07:25:42 -0600871{
Jens Axboed3656342019-12-18 09:50:26 -0700872 const struct io_op_def *def = &io_op_defs[req->opcode];
Jens Axboe561fb042019-10-24 07:25:42 -0600873 bool do_hashed = false;
Jens Axboe54a91f32019-09-10 09:15:04 -0600874
Jens Axboed3656342019-12-18 09:50:26 -0700875 if (req->flags & REQ_F_ISREG) {
876 if (def->hash_reg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -0700877 do_hashed = true;
Jens Axboed3656342019-12-18 09:50:26 -0700878 } else {
879 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -0700880 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -0600881 }
Jens Axboed3656342019-12-18 09:50:26 -0700882 if (def->needs_mm)
Jens Axboe3529d8c2019-12-19 18:24:38 -0700883 req->work.flags |= IO_WQ_WORK_NEEDS_USER;
Jens Axboe54a91f32019-09-10 09:15:04 -0600884
Jens Axboe94ae5e72019-11-14 19:39:52 -0700885 *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -0600886 return do_hashed;
887}
888
Jackie Liua197f662019-11-08 08:09:12 -0700889static inline void io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -0600890{
Jackie Liua197f662019-11-08 08:09:12 -0700891 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe94ae5e72019-11-14 19:39:52 -0700892 struct io_kiocb *link;
893 bool do_hashed;
894
895 do_hashed = io_prep_async_work(req, &link);
Jens Axboe561fb042019-10-24 07:25:42 -0600896
897 trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work,
898 req->flags);
899 if (!do_hashed) {
900 io_wq_enqueue(ctx->io_wq, &req->work);
901 } else {
902 io_wq_enqueue_hashed(ctx->io_wq, &req->work,
903 file_inode(req->file));
904 }
Jens Axboe94ae5e72019-11-14 19:39:52 -0700905
906 if (link)
907 io_queue_linked_timeout(link);
Jens Axboe18d9be12019-09-10 09:13:05 -0600908}
909
Jens Axboe5262f562019-09-17 12:26:57 -0600910static void io_kill_timeout(struct io_kiocb *req)
911{
912 int ret;
913
Jens Axboe2d283902019-12-04 11:08:05 -0700914 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe5262f562019-09-17 12:26:57 -0600915 if (ret != -1) {
916 atomic_inc(&req->ctx->cq_timeouts);
Jens Axboe842f9612019-10-29 12:34:10 -0600917 list_del_init(&req->list);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700918 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800919 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -0600920 }
921}
922
923static void io_kill_timeouts(struct io_ring_ctx *ctx)
924{
925 struct io_kiocb *req, *tmp;
926
927 spin_lock_irq(&ctx->completion_lock);
928 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
929 io_kill_timeout(req);
930 spin_unlock_irq(&ctx->completion_lock);
931}
932
Jens Axboede0617e2019-04-06 21:51:27 -0600933static void io_commit_cqring(struct io_ring_ctx *ctx)
934{
935 struct io_kiocb *req;
936
Jens Axboe5262f562019-09-17 12:26:57 -0600937 while ((req = io_get_timeout_req(ctx)) != NULL)
938 io_kill_timeout(req);
939
Jens Axboede0617e2019-04-06 21:51:27 -0600940 __io_commit_cqring(ctx);
941
942 while ((req = io_get_deferred_req(ctx)) != NULL) {
943 req->flags |= REQ_F_IO_DRAINED;
Jackie Liua197f662019-11-08 08:09:12 -0700944 io_queue_async_work(req);
Jens Axboede0617e2019-04-06 21:51:27 -0600945 }
946}
947
Jens Axboe2b188cc2019-01-07 10:46:33 -0700948static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
949{
Hristo Venev75b28af2019-08-26 17:23:46 +0000950 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700951 unsigned tail;
952
953 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +0200954 /*
955 * writes to the cq entry need to come after reading head; the
956 * control dependency is enough as we're using WRITE_ONCE to
957 * fill the cq entry
958 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000959 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700960 return NULL;
961
962 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +0000963 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -0700964}
965
Jens Axboef2842ab2020-01-08 11:04:00 -0700966static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
967{
968 if (!ctx->eventfd_async)
969 return true;
970 return io_wq_current_is_worker() || in_interrupt();
971}
972
Jens Axboe8c838782019-03-12 15:48:16 -0600973static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
974{
975 if (waitqueue_active(&ctx->wait))
976 wake_up(&ctx->wait);
977 if (waitqueue_active(&ctx->sqo_wait))
978 wake_up(&ctx->sqo_wait);
Jens Axboef2842ab2020-01-08 11:04:00 -0700979 if (ctx->cq_ev_fd && io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -0600980 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -0600981}
982
Jens Axboec4a2ed72019-11-21 21:01:26 -0700983/* Returns true if there are no backlogged entries after the flush */
984static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700985{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700986 struct io_rings *rings = ctx->rings;
987 struct io_uring_cqe *cqe;
988 struct io_kiocb *req;
989 unsigned long flags;
990 LIST_HEAD(list);
991
992 if (!force) {
993 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -0700994 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700995 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
996 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -0700997 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700998 }
999
1000 spin_lock_irqsave(&ctx->completion_lock, flags);
1001
1002 /* if force is set, the ring is going away. always drop after that */
1003 if (force)
Jens Axboe69b3e542020-01-08 11:01:46 -07001004 ctx->cq_overflow_flushed = 1;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001005
Jens Axboec4a2ed72019-11-21 21:01:26 -07001006 cqe = NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001007 while (!list_empty(&ctx->cq_overflow_list)) {
1008 cqe = io_get_cqring(ctx);
1009 if (!cqe && !force)
1010 break;
1011
1012 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
1013 list);
1014 list_move(&req->list, &list);
1015 if (cqe) {
1016 WRITE_ONCE(cqe->user_data, req->user_data);
1017 WRITE_ONCE(cqe->res, req->result);
1018 WRITE_ONCE(cqe->flags, 0);
1019 } else {
1020 WRITE_ONCE(ctx->rings->cq_overflow,
1021 atomic_inc_return(&ctx->cached_cq_overflow));
1022 }
1023 }
1024
1025 io_commit_cqring(ctx);
Jens Axboead3eb2c2019-12-18 17:12:20 -07001026 if (cqe) {
1027 clear_bit(0, &ctx->sq_check_overflow);
1028 clear_bit(0, &ctx->cq_check_overflow);
1029 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001030 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1031 io_cqring_ev_posted(ctx);
1032
1033 while (!list_empty(&list)) {
1034 req = list_first_entry(&list, struct io_kiocb, list);
1035 list_del(&req->list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001036 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001037 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001038
1039 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001040}
1041
Jens Axboe78e19bb2019-11-06 15:21:34 -07001042static void io_cqring_fill_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001043{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001044 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001045 struct io_uring_cqe *cqe;
1046
Jens Axboe78e19bb2019-11-06 15:21:34 -07001047 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001048
Jens Axboe2b188cc2019-01-07 10:46:33 -07001049 /*
1050 * If we can't get a cq entry, userspace overflowed the
1051 * submission (by quite a lot). Increment the overflow count in
1052 * the ring.
1053 */
1054 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001055 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001056 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001057 WRITE_ONCE(cqe->res, res);
1058 WRITE_ONCE(cqe->flags, 0);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001059 } else if (ctx->cq_overflow_flushed) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001060 WRITE_ONCE(ctx->rings->cq_overflow,
1061 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001062 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001063 if (list_empty(&ctx->cq_overflow_list)) {
1064 set_bit(0, &ctx->sq_check_overflow);
1065 set_bit(0, &ctx->cq_check_overflow);
1066 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001067 refcount_inc(&req->refs);
1068 req->result = res;
1069 list_add_tail(&req->list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001070 }
1071}
1072
Jens Axboe78e19bb2019-11-06 15:21:34 -07001073static void io_cqring_add_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001074{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001075 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001076 unsigned long flags;
1077
1078 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001079 io_cqring_fill_event(req, res);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001080 io_commit_cqring(ctx);
1081 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1082
Jens Axboe8c838782019-03-12 15:48:16 -06001083 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001084}
1085
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001086static inline bool io_is_fallback_req(struct io_kiocb *req)
1087{
1088 return req == (struct io_kiocb *)
1089 ((unsigned long) req->ctx->fallback_req & ~1UL);
1090}
1091
1092static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
1093{
1094 struct io_kiocb *req;
1095
1096 req = ctx->fallback_req;
1097 if (!test_and_set_bit_lock(0, (unsigned long *) ctx->fallback_req))
1098 return req;
1099
1100 return NULL;
1101}
1102
Jens Axboe2579f912019-01-09 09:10:43 -07001103static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
1104 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001105{
Jens Axboefd6fab22019-03-14 16:30:06 -06001106 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001107 struct io_kiocb *req;
1108
Jens Axboe2579f912019-01-09 09:10:43 -07001109 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -06001110 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -07001111 if (unlikely(!req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001112 goto fallback;
Jens Axboe2579f912019-01-09 09:10:43 -07001113 } else if (!state->free_reqs) {
1114 size_t sz;
1115 int ret;
1116
1117 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -06001118 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
1119
1120 /*
1121 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1122 * retry single alloc to be on the safe side.
1123 */
1124 if (unlikely(ret <= 0)) {
1125 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1126 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001127 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -06001128 ret = 1;
1129 }
Jens Axboe2579f912019-01-09 09:10:43 -07001130 state->free_reqs = ret - 1;
1131 state->cur_req = 1;
1132 req = state->reqs[0];
1133 } else {
1134 req = state->reqs[state->cur_req];
1135 state->free_reqs--;
1136 state->cur_req++;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001137 }
1138
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001139got_it:
Jens Axboe1a6b74f2019-12-02 10:33:15 -07001140 req->io = NULL;
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001141 req->ring_file = NULL;
Jens Axboe60c112b2019-06-21 10:20:18 -06001142 req->file = NULL;
Jens Axboe2579f912019-01-09 09:10:43 -07001143 req->ctx = ctx;
1144 req->flags = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06001145 /* one is dropped after submission, the other at completion */
1146 refcount_set(&req->refs, 2);
Jens Axboe9e645e112019-05-10 16:07:28 -06001147 req->result = 0;
Jens Axboe561fb042019-10-24 07:25:42 -06001148 INIT_IO_WORK(&req->work, io_wq_submit_work);
Jens Axboe2579f912019-01-09 09:10:43 -07001149 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001150fallback:
1151 req = io_get_fallback_req(ctx);
1152 if (req)
1153 goto got_it;
Pavel Begunkov6805b322019-10-08 02:18:42 +03001154 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001155 return NULL;
1156}
1157
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001158static void __io_req_do_free(struct io_kiocb *req)
1159{
1160 if (likely(!io_is_fallback_req(req)))
1161 kmem_cache_free(req_cachep, req);
1162 else
1163 clear_bit_unlock(0, (unsigned long *) req->ctx->fallback_req);
1164}
1165
Jens Axboec6ca97b302019-12-28 12:11:08 -07001166static void __io_req_aux_free(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001167{
Jens Axboefcb323c2019-10-24 12:39:47 -06001168 struct io_ring_ctx *ctx = req->ctx;
1169
YueHaibing96fd84d2020-01-07 22:22:44 +08001170 kfree(req->io);
Jens Axboe05f3fb32019-12-09 11:22:50 -07001171 if (req->file) {
1172 if (req->flags & REQ_F_FIXED_FILE)
1173 percpu_ref_put(&ctx->file_data->refs);
1174 else
1175 fput(req->file);
1176 }
Jens Axboec6ca97b302019-12-28 12:11:08 -07001177}
1178
1179static void __io_free_req(struct io_kiocb *req)
1180{
1181 __io_req_aux_free(req);
1182
Jens Axboefcb323c2019-10-24 12:39:47 -06001183 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001184 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06001185 unsigned long flags;
1186
1187 spin_lock_irqsave(&ctx->inflight_lock, flags);
1188 list_del(&req->inflight_entry);
1189 if (waitqueue_active(&ctx->inflight_wait))
1190 wake_up(&ctx->inflight_wait);
1191 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1192 }
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001193
1194 percpu_ref_put(&req->ctx->refs);
1195 __io_req_do_free(req);
Jens Axboee65ef562019-03-12 10:16:44 -06001196}
1197
Jens Axboec6ca97b302019-12-28 12:11:08 -07001198struct req_batch {
1199 void *reqs[IO_IOPOLL_BATCH];
1200 int to_free;
1201 int need_iter;
1202};
1203
1204static void io_free_req_many(struct io_ring_ctx *ctx, struct req_batch *rb)
1205{
1206 if (!rb->to_free)
1207 return;
1208 if (rb->need_iter) {
1209 int i, inflight = 0;
1210 unsigned long flags;
1211
1212 for (i = 0; i < rb->to_free; i++) {
1213 struct io_kiocb *req = rb->reqs[i];
1214
1215 if (req->flags & REQ_F_FIXED_FILE)
1216 req->file = NULL;
1217 if (req->flags & REQ_F_INFLIGHT)
1218 inflight++;
1219 else
1220 rb->reqs[i] = NULL;
1221 __io_req_aux_free(req);
1222 }
1223 if (!inflight)
1224 goto do_free;
1225
1226 spin_lock_irqsave(&ctx->inflight_lock, flags);
1227 for (i = 0; i < rb->to_free; i++) {
1228 struct io_kiocb *req = rb->reqs[i];
1229
1230 if (req) {
1231 list_del(&req->inflight_entry);
1232 if (!--inflight)
1233 break;
1234 }
1235 }
1236 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1237
1238 if (waitqueue_active(&ctx->inflight_wait))
1239 wake_up(&ctx->inflight_wait);
1240 }
1241do_free:
1242 kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs);
1243 percpu_ref_put_many(&ctx->refs, rb->to_free);
1244 percpu_ref_put_many(&ctx->file_data->refs, rb->to_free);
1245 rb->to_free = rb->need_iter = 0;
1246}
1247
Jackie Liua197f662019-11-08 08:09:12 -07001248static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001249{
Jackie Liua197f662019-11-08 08:09:12 -07001250 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001251 int ret;
1252
Jens Axboe2d283902019-12-04 11:08:05 -07001253 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe2665abf2019-11-05 12:40:47 -07001254 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001255 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -07001256 io_commit_cqring(ctx);
1257 req->flags &= ~REQ_F_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001258 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001259 return true;
1260 }
1261
1262 return false;
1263}
1264
Jens Axboeba816ad2019-09-28 11:36:45 -06001265static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -06001266{
Jens Axboe2665abf2019-11-05 12:40:47 -07001267 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001268 bool wake_ev = false;
Jens Axboe9e645e112019-05-10 16:07:28 -06001269
Jens Axboe4d7dd462019-11-20 13:03:52 -07001270 /* Already got next link */
1271 if (req->flags & REQ_F_LINK_NEXT)
1272 return;
1273
Jens Axboe9e645e112019-05-10 16:07:28 -06001274 /*
1275 * The list should never be empty when we are called here. But could
1276 * potentially happen if the chain is messed up, check to be on the
1277 * safe side.
1278 */
Pavel Begunkov44932332019-12-05 16:16:35 +03001279 while (!list_empty(&req->link_list)) {
1280 struct io_kiocb *nxt = list_first_entry(&req->link_list,
1281 struct io_kiocb, link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001282
Pavel Begunkov44932332019-12-05 16:16:35 +03001283 if (unlikely((req->flags & REQ_F_LINK_TIMEOUT) &&
1284 (nxt->flags & REQ_F_TIMEOUT))) {
1285 list_del_init(&nxt->link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001286 wake_ev |= io_link_cancel_timeout(nxt);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001287 req->flags &= ~REQ_F_LINK_TIMEOUT;
1288 continue;
1289 }
Jens Axboe9e645e112019-05-10 16:07:28 -06001290
Pavel Begunkov44932332019-12-05 16:16:35 +03001291 list_del_init(&req->link_list);
1292 if (!list_empty(&nxt->link_list))
1293 nxt->flags |= REQ_F_LINK;
Pavel Begunkovb18fdf72019-11-21 23:21:02 +03001294 *nxtptr = nxt;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001295 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06001296 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001297
Jens Axboe4d7dd462019-11-20 13:03:52 -07001298 req->flags |= REQ_F_LINK_NEXT;
Jens Axboe2665abf2019-11-05 12:40:47 -07001299 if (wake_ev)
1300 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001301}
1302
1303/*
1304 * Called if REQ_F_LINK is set, and we fail the head request
1305 */
1306static void io_fail_links(struct io_kiocb *req)
1307{
Jens Axboe2665abf2019-11-05 12:40:47 -07001308 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001309 unsigned long flags;
1310
1311 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06001312
1313 while (!list_empty(&req->link_list)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03001314 struct io_kiocb *link = list_first_entry(&req->link_list,
1315 struct io_kiocb, link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06001316
Pavel Begunkov44932332019-12-05 16:16:35 +03001317 list_del_init(&link->link_list);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001318 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001319
1320 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
Jens Axboed625c6e2019-12-17 19:53:05 -07001321 link->opcode == IORING_OP_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -07001322 io_link_cancel_timeout(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001323 } else {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001324 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe978db572019-11-14 22:39:04 -07001325 __io_double_put_req(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001326 }
Jens Axboe5d960722019-11-19 15:31:28 -07001327 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -06001328 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001329
1330 io_commit_cqring(ctx);
1331 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1332 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001333}
1334
Jens Axboe4d7dd462019-11-20 13:03:52 -07001335static void io_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -06001336{
Jens Axboe4d7dd462019-11-20 13:03:52 -07001337 if (likely(!(req->flags & REQ_F_LINK)))
Jens Axboe2665abf2019-11-05 12:40:47 -07001338 return;
Jens Axboe2665abf2019-11-05 12:40:47 -07001339
Jens Axboe9e645e112019-05-10 16:07:28 -06001340 /*
1341 * If LINK is set, we have dependent requests in this chain. If we
1342 * didn't fail this request, queue the first one up, moving any other
1343 * dependencies to the next request. In case of failure, fail the rest
1344 * of the chain.
1345 */
Jens Axboe2665abf2019-11-05 12:40:47 -07001346 if (req->flags & REQ_F_FAIL_LINK) {
1347 io_fail_links(req);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001348 } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) ==
1349 REQ_F_LINK_TIMEOUT) {
Jens Axboe2665abf2019-11-05 12:40:47 -07001350 struct io_ring_ctx *ctx = req->ctx;
1351 unsigned long flags;
1352
1353 /*
1354 * If this is a timeout link, we could be racing with the
1355 * timeout timer. Grab the completion lock for this case to
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001356 * protect against that.
Jens Axboe2665abf2019-11-05 12:40:47 -07001357 */
1358 spin_lock_irqsave(&ctx->completion_lock, flags);
1359 io_req_link_next(req, nxt);
1360 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1361 } else {
1362 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -06001363 }
Jens Axboe4d7dd462019-11-20 13:03:52 -07001364}
Jens Axboe9e645e112019-05-10 16:07:28 -06001365
Jackie Liuc69f8db2019-11-09 11:00:08 +08001366static void io_free_req(struct io_kiocb *req)
1367{
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001368 struct io_kiocb *nxt = NULL;
1369
1370 io_req_find_next(req, &nxt);
Pavel Begunkov70cf9f32019-11-21 23:21:00 +03001371 __io_free_req(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001372
1373 if (nxt)
1374 io_queue_async_work(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08001375}
1376
Jens Axboeba816ad2019-09-28 11:36:45 -06001377/*
1378 * Drop reference to request, return next in chain (if there is one) if this
1379 * was the last reference to this request.
1380 */
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001381__attribute__((nonnull))
Jackie Liuec9c02a2019-11-08 23:50:36 +08001382static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboee65ef562019-03-12 10:16:44 -06001383{
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001384 io_req_find_next(req, nxtptr);
Jens Axboe4d7dd462019-11-20 13:03:52 -07001385
Jens Axboee65ef562019-03-12 10:16:44 -06001386 if (refcount_dec_and_test(&req->refs))
Jens Axboe4d7dd462019-11-20 13:03:52 -07001387 __io_free_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001388}
1389
Jens Axboe2b188cc2019-01-07 10:46:33 -07001390static void io_put_req(struct io_kiocb *req)
1391{
Jens Axboedef596e2019-01-09 08:59:42 -07001392 if (refcount_dec_and_test(&req->refs))
1393 io_free_req(req);
1394}
1395
Jens Axboe978db572019-11-14 22:39:04 -07001396/*
1397 * Must only be used if we don't need to care about links, usually from
1398 * within the completion handling itself.
1399 */
1400static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06001401{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001402 /* drop both submit and complete references */
1403 if (refcount_sub_and_test(2, &req->refs))
1404 __io_free_req(req);
1405}
1406
Jens Axboe978db572019-11-14 22:39:04 -07001407static void io_double_put_req(struct io_kiocb *req)
1408{
1409 /* drop both submit and complete references */
1410 if (refcount_sub_and_test(2, &req->refs))
1411 io_free_req(req);
1412}
1413
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001414static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06001415{
Jens Axboe84f97dc2019-11-06 11:27:53 -07001416 struct io_rings *rings = ctx->rings;
1417
Jens Axboead3eb2c2019-12-18 17:12:20 -07001418 if (test_bit(0, &ctx->cq_check_overflow)) {
1419 /*
1420 * noflush == true is from the waitqueue handler, just ensure
1421 * we wake up the task, and the next invocation will flush the
1422 * entries. We cannot safely to it from here.
1423 */
1424 if (noflush && !list_empty(&ctx->cq_overflow_list))
1425 return -1U;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001426
Jens Axboead3eb2c2019-12-18 17:12:20 -07001427 io_cqring_overflow_flush(ctx, false);
1428 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001429
Jens Axboea3a0e432019-08-20 11:03:11 -06001430 /* See comment at the top of this file */
1431 smp_rmb();
Jens Axboead3eb2c2019-12-18 17:12:20 -07001432 return ctx->cached_cq_tail - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06001433}
1434
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03001435static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1436{
1437 struct io_rings *rings = ctx->rings;
1438
1439 /* make sure SQ entry isn't read before tail */
1440 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1441}
1442
Jens Axboe8237e042019-12-28 10:48:22 -07001443static inline bool io_req_multi_free(struct req_batch *rb, struct io_kiocb *req)
Jens Axboee94f1412019-12-19 12:06:02 -07001444{
Jens Axboec6ca97b302019-12-28 12:11:08 -07001445 if ((req->flags & REQ_F_LINK) || io_is_fallback_req(req))
1446 return false;
Jens Axboee94f1412019-12-19 12:06:02 -07001447
Jens Axboec6ca97b302019-12-28 12:11:08 -07001448 if (!(req->flags & REQ_F_FIXED_FILE) || req->io)
1449 rb->need_iter++;
1450
1451 rb->reqs[rb->to_free++] = req;
1452 if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
1453 io_free_req_many(req->ctx, rb);
1454 return true;
Jens Axboee94f1412019-12-19 12:06:02 -07001455}
1456
Jens Axboedef596e2019-01-09 08:59:42 -07001457/*
1458 * Find and free completed poll iocbs
1459 */
1460static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
1461 struct list_head *done)
1462{
Jens Axboe8237e042019-12-28 10:48:22 -07001463 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07001464 struct io_kiocb *req;
Jens Axboedef596e2019-01-09 08:59:42 -07001465
Jens Axboec6ca97b302019-12-28 12:11:08 -07001466 rb.to_free = rb.need_iter = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001467 while (!list_empty(done)) {
1468 req = list_first_entry(done, struct io_kiocb, list);
1469 list_del(&req->list);
1470
Jens Axboe78e19bb2019-11-06 15:21:34 -07001471 io_cqring_fill_event(req, req->result);
Jens Axboedef596e2019-01-09 08:59:42 -07001472 (*nr_events)++;
1473
Jens Axboe8237e042019-12-28 10:48:22 -07001474 if (refcount_dec_and_test(&req->refs) &&
1475 !io_req_multi_free(&rb, req))
1476 io_free_req(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001477 }
Jens Axboedef596e2019-01-09 08:59:42 -07001478
Jens Axboe09bb8392019-03-13 12:39:28 -06001479 io_commit_cqring(ctx);
Jens Axboe8237e042019-12-28 10:48:22 -07001480 io_free_req_many(ctx, &rb);
Jens Axboedef596e2019-01-09 08:59:42 -07001481}
1482
1483static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1484 long min)
1485{
1486 struct io_kiocb *req, *tmp;
1487 LIST_HEAD(done);
1488 bool spin;
1489 int ret;
1490
1491 /*
1492 * Only spin for completions if we don't have multiple devices hanging
1493 * off our complete list, and we're under the requested amount.
1494 */
1495 spin = !ctx->poll_multi_file && *nr_events < min;
1496
1497 ret = 0;
1498 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
Jens Axboe9adbd452019-12-20 08:45:55 -07001499 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07001500
1501 /*
1502 * Move completed entries to our local list. If we find a
1503 * request that requires polling, break out and complete
1504 * the done list first, if we have entries there.
1505 */
1506 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
1507 list_move_tail(&req->list, &done);
1508 continue;
1509 }
1510 if (!list_empty(&done))
1511 break;
1512
1513 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1514 if (ret < 0)
1515 break;
1516
1517 if (ret && spin)
1518 spin = false;
1519 ret = 0;
1520 }
1521
1522 if (!list_empty(&done))
1523 io_iopoll_complete(ctx, nr_events, &done);
1524
1525 return ret;
1526}
1527
1528/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08001529 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07001530 * non-spinning poll check - we'll still enter the driver poll loop, but only
1531 * as a non-spinning completion check.
1532 */
1533static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
1534 long min)
1535{
Jens Axboe08f54392019-08-21 22:19:11 -06001536 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07001537 int ret;
1538
1539 ret = io_do_iopoll(ctx, nr_events, min);
1540 if (ret < 0)
1541 return ret;
1542 if (!min || *nr_events >= min)
1543 return 0;
1544 }
1545
1546 return 1;
1547}
1548
1549/*
1550 * We can't just wait for polled events to come to us, we have to actively
1551 * find and complete them.
1552 */
1553static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
1554{
1555 if (!(ctx->flags & IORING_SETUP_IOPOLL))
1556 return;
1557
1558 mutex_lock(&ctx->uring_lock);
1559 while (!list_empty(&ctx->poll_list)) {
1560 unsigned int nr_events = 0;
1561
1562 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -06001563
1564 /*
1565 * Ensure we allow local-to-the-cpu processing to take place,
1566 * in this case we need to ensure that we reap all events.
1567 */
1568 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -07001569 }
1570 mutex_unlock(&ctx->uring_lock);
1571}
1572
Jens Axboe2b2ed972019-10-25 10:06:15 -06001573static int __io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1574 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07001575{
Jens Axboe2b2ed972019-10-25 10:06:15 -06001576 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001577
1578 do {
1579 int tmin = 0;
1580
Jens Axboe500f9fb2019-08-19 12:15:59 -06001581 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06001582 * Don't enter poll loop if we already have events pending.
1583 * If we do, we can potentially be spinning for commands that
1584 * already triggered a CQE (eg in error).
1585 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001586 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06001587 break;
1588
1589 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06001590 * If a submit got punted to a workqueue, we can have the
1591 * application entering polling for a command before it gets
1592 * issued. That app will hold the uring_lock for the duration
1593 * of the poll right here, so we need to take a breather every
1594 * now and then to ensure that the issue has a chance to add
1595 * the poll to the issued list. Otherwise we can spin here
1596 * forever, while the workqueue is stuck trying to acquire the
1597 * very same mutex.
1598 */
1599 if (!(++iters & 7)) {
1600 mutex_unlock(&ctx->uring_lock);
1601 mutex_lock(&ctx->uring_lock);
1602 }
1603
Jens Axboedef596e2019-01-09 08:59:42 -07001604 if (*nr_events < min)
1605 tmin = min - *nr_events;
1606
1607 ret = io_iopoll_getevents(ctx, nr_events, tmin);
1608 if (ret <= 0)
1609 break;
1610 ret = 0;
1611 } while (min && !*nr_events && !need_resched());
1612
Jens Axboe2b2ed972019-10-25 10:06:15 -06001613 return ret;
1614}
1615
1616static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1617 long min)
1618{
1619 int ret;
1620
1621 /*
1622 * We disallow the app entering submit/complete with polling, but we
1623 * still need to lock the ring to prevent racing with polled issue
1624 * that got punted to a workqueue.
1625 */
1626 mutex_lock(&ctx->uring_lock);
1627 ret = __io_iopoll_check(ctx, nr_events, min);
Jens Axboe500f9fb2019-08-19 12:15:59 -06001628 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001629 return ret;
1630}
1631
Jens Axboe491381ce2019-10-17 09:20:46 -06001632static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001633{
Jens Axboe491381ce2019-10-17 09:20:46 -06001634 /*
1635 * Tell lockdep we inherited freeze protection from submission
1636 * thread.
1637 */
1638 if (req->flags & REQ_F_ISREG) {
1639 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001640
Jens Axboe491381ce2019-10-17 09:20:46 -06001641 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001642 }
Jens Axboe491381ce2019-10-17 09:20:46 -06001643 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001644}
1645
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001646static inline void req_set_fail_links(struct io_kiocb *req)
1647{
1648 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1649 req->flags |= REQ_F_FAIL_LINK;
1650}
1651
Jens Axboeba816ad2019-09-28 11:36:45 -06001652static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001653{
Jens Axboe9adbd452019-12-20 08:45:55 -07001654 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001655
Jens Axboe491381ce2019-10-17 09:20:46 -06001656 if (kiocb->ki_flags & IOCB_WRITE)
1657 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001658
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001659 if (res != req->result)
1660 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001661 io_cqring_add_event(req, res);
Jens Axboeba816ad2019-09-28 11:36:45 -06001662}
1663
1664static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1665{
Jens Axboe9adbd452019-12-20 08:45:55 -07001666 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06001667
1668 io_complete_rw_common(kiocb, res);
Jens Axboee65ef562019-03-12 10:16:44 -06001669 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001670}
1671
Jens Axboeba816ad2019-09-28 11:36:45 -06001672static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res)
1673{
Jens Axboe9adbd452019-12-20 08:45:55 -07001674 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001675 struct io_kiocb *nxt = NULL;
Jens Axboeba816ad2019-09-28 11:36:45 -06001676
1677 io_complete_rw_common(kiocb, res);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001678 io_put_req_find_next(req, &nxt);
1679
1680 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001681}
1682
Jens Axboedef596e2019-01-09 08:59:42 -07001683static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1684{
Jens Axboe9adbd452019-12-20 08:45:55 -07001685 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07001686
Jens Axboe491381ce2019-10-17 09:20:46 -06001687 if (kiocb->ki_flags & IOCB_WRITE)
1688 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001689
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001690 if (res != req->result)
1691 req_set_fail_links(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06001692 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -07001693 if (res != -EAGAIN)
1694 req->flags |= REQ_F_IOPOLL_COMPLETED;
1695}
1696
1697/*
1698 * After the iocb has been issued, it's safe to be found on the poll list.
1699 * Adding the kiocb to the list AFTER submission ensures that we don't
1700 * find it from a io_iopoll_getevents() thread before the issuer is done
1701 * accessing the kiocb cookie.
1702 */
1703static void io_iopoll_req_issued(struct io_kiocb *req)
1704{
1705 struct io_ring_ctx *ctx = req->ctx;
1706
1707 /*
1708 * Track whether we have multiple files in our lists. This will impact
1709 * how we do polling eventually, not spinning if we're on potentially
1710 * different devices.
1711 */
1712 if (list_empty(&ctx->poll_list)) {
1713 ctx->poll_multi_file = false;
1714 } else if (!ctx->poll_multi_file) {
1715 struct io_kiocb *list_req;
1716
1717 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1718 list);
Jens Axboe9adbd452019-12-20 08:45:55 -07001719 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07001720 ctx->poll_multi_file = true;
1721 }
1722
1723 /*
1724 * For fast devices, IO may have already completed. If it has, add
1725 * it to the front so we find it first.
1726 */
1727 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1728 list_add(&req->list, &ctx->poll_list);
1729 else
1730 list_add_tail(&req->list, &ctx->poll_list);
1731}
1732
Jens Axboe3d6770f2019-04-13 11:50:54 -06001733static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07001734{
Jens Axboe3d6770f2019-04-13 11:50:54 -06001735 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -07001736 int diff = state->has_refs - state->used_refs;
1737
1738 if (diff)
1739 fput_many(state->file, diff);
1740 state->file = NULL;
1741 }
1742}
1743
1744/*
1745 * Get as many references to a file as we have IOs left in this submission,
1746 * assuming most submissions are for one file, or at least that each file
1747 * has more than one submission.
1748 */
1749static struct file *io_file_get(struct io_submit_state *state, int fd)
1750{
1751 if (!state)
1752 return fget(fd);
1753
1754 if (state->file) {
1755 if (state->fd == fd) {
1756 state->used_refs++;
1757 state->ios_left--;
1758 return state->file;
1759 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06001760 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07001761 }
1762 state->file = fget_many(fd, state->ios_left);
1763 if (!state->file)
1764 return NULL;
1765
1766 state->fd = fd;
1767 state->has_refs = state->ios_left;
1768 state->used_refs = 1;
1769 state->ios_left--;
1770 return state->file;
1771}
1772
Jens Axboe2b188cc2019-01-07 10:46:33 -07001773/*
1774 * If we tracked the file through the SCM inflight mechanism, we could support
1775 * any file. For now, just ensure that anything potentially problematic is done
1776 * inline.
1777 */
1778static bool io_file_supports_async(struct file *file)
1779{
1780 umode_t mode = file_inode(file)->i_mode;
1781
Jens Axboe10d59342019-12-09 20:16:22 -07001782 if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07001783 return true;
1784 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
1785 return true;
1786
1787 return false;
1788}
1789
Jens Axboe3529d8c2019-12-19 18:24:38 -07001790static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1791 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001792{
Jens Axboedef596e2019-01-09 08:59:42 -07001793 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07001794 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06001795 unsigned ioprio;
1796 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001797
Jens Axboe09bb8392019-03-13 12:39:28 -06001798 if (!req->file)
1799 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001800
Jens Axboe491381ce2019-10-17 09:20:46 -06001801 if (S_ISREG(file_inode(req->file)->i_mode))
1802 req->flags |= REQ_F_ISREG;
1803
Jens Axboe2b188cc2019-01-07 10:46:33 -07001804 kiocb->ki_pos = READ_ONCE(sqe->off);
Jens Axboeba042912019-12-25 16:33:42 -07001805 if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) {
1806 req->flags |= REQ_F_CUR_POS;
1807 kiocb->ki_pos = req->file->f_pos;
1808 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001809 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
1810 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
1811
1812 ioprio = READ_ONCE(sqe->ioprio);
1813 if (ioprio) {
1814 ret = ioprio_check_cap(ioprio);
1815 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06001816 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001817
1818 kiocb->ki_ioprio = ioprio;
1819 } else
1820 kiocb->ki_ioprio = get_current_ioprio();
1821
1822 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
1823 if (unlikely(ret))
Jens Axboe09bb8392019-03-13 12:39:28 -06001824 return ret;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001825
1826 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06001827 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
1828 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02001829 req->flags |= REQ_F_NOWAIT;
1830
1831 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001832 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001833
Jens Axboedef596e2019-01-09 08:59:42 -07001834 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07001835 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
1836 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06001837 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001838
Jens Axboedef596e2019-01-09 08:59:42 -07001839 kiocb->ki_flags |= IOCB_HIPRI;
1840 kiocb->ki_complete = io_complete_rw_iopoll;
Jens Axboe6873e0b2019-10-30 13:53:09 -06001841 req->result = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001842 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001843 if (kiocb->ki_flags & IOCB_HIPRI)
1844 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07001845 kiocb->ki_complete = io_complete_rw;
1846 }
Jens Axboe9adbd452019-12-20 08:45:55 -07001847
Jens Axboe3529d8c2019-12-19 18:24:38 -07001848 req->rw.addr = READ_ONCE(sqe->addr);
1849 req->rw.len = READ_ONCE(sqe->len);
Jens Axboe9adbd452019-12-20 08:45:55 -07001850 /* we own ->private, reuse it for the buffer index */
1851 req->rw.kiocb.private = (void *) (unsigned long)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001852 READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001853 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001854}
1855
1856static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
1857{
1858 switch (ret) {
1859 case -EIOCBQUEUED:
1860 break;
1861 case -ERESTARTSYS:
1862 case -ERESTARTNOINTR:
1863 case -ERESTARTNOHAND:
1864 case -ERESTART_RESTARTBLOCK:
1865 /*
1866 * We can't just restart the syscall, since previously
1867 * submitted sqes may already be in progress. Just fail this
1868 * IO with EINTR.
1869 */
1870 ret = -EINTR;
1871 /* fall through */
1872 default:
1873 kiocb->ki_complete(kiocb, ret, 0);
1874 }
1875}
1876
Jens Axboeba816ad2019-09-28 11:36:45 -06001877static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt,
1878 bool in_async)
1879{
Jens Axboeba042912019-12-25 16:33:42 -07001880 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
1881
1882 if (req->flags & REQ_F_CUR_POS)
1883 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001884 if (in_async && ret >= 0 && kiocb->ki_complete == io_complete_rw)
Jens Axboeba816ad2019-09-28 11:36:45 -06001885 *nxt = __io_complete_rw(kiocb, ret);
1886 else
1887 io_rw_done(kiocb, ret);
1888}
1889
Jens Axboe9adbd452019-12-20 08:45:55 -07001890static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03001891 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07001892{
Jens Axboe9adbd452019-12-20 08:45:55 -07001893 struct io_ring_ctx *ctx = req->ctx;
1894 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001895 struct io_mapped_ubuf *imu;
1896 unsigned index, buf_index;
1897 size_t offset;
1898 u64 buf_addr;
1899
1900 /* attempt to use fixed buffers without having provided iovecs */
1901 if (unlikely(!ctx->user_bufs))
1902 return -EFAULT;
1903
Jens Axboe9adbd452019-12-20 08:45:55 -07001904 buf_index = (unsigned long) req->rw.kiocb.private;
Jens Axboeedafcce2019-01-09 09:16:05 -07001905 if (unlikely(buf_index >= ctx->nr_user_bufs))
1906 return -EFAULT;
1907
1908 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
1909 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07001910 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07001911
1912 /* overflow */
1913 if (buf_addr + len < buf_addr)
1914 return -EFAULT;
1915 /* not inside the mapped region */
1916 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
1917 return -EFAULT;
1918
1919 /*
1920 * May not be a start of buffer, set size appropriately
1921 * and advance us to the beginning.
1922 */
1923 offset = buf_addr - imu->ubuf;
1924 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06001925
1926 if (offset) {
1927 /*
1928 * Don't use iov_iter_advance() here, as it's really slow for
1929 * using the latter parts of a big fixed buffer - it iterates
1930 * over each segment manually. We can cheat a bit here, because
1931 * we know that:
1932 *
1933 * 1) it's a BVEC iter, we set it up
1934 * 2) all bvecs are PAGE_SIZE in size, except potentially the
1935 * first and last bvec
1936 *
1937 * So just find our index, and adjust the iterator afterwards.
1938 * If the offset is within the first bvec (or the whole first
1939 * bvec, just use iov_iter_advance(). This makes it easier
1940 * since we can just skip the first segment, which may not
1941 * be PAGE_SIZE aligned.
1942 */
1943 const struct bio_vec *bvec = imu->bvec;
1944
1945 if (offset <= bvec->bv_len) {
1946 iov_iter_advance(iter, offset);
1947 } else {
1948 unsigned long seg_skip;
1949
1950 /* skip first vec */
1951 offset -= bvec->bv_len;
1952 seg_skip = 1 + (offset >> PAGE_SHIFT);
1953
1954 iter->bvec = bvec + seg_skip;
1955 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02001956 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001957 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001958 }
1959 }
1960
Jens Axboe5e559562019-11-13 16:12:46 -07001961 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001962}
1963
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001964static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
1965 struct iovec **iovec, struct iov_iter *iter)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001966{
Jens Axboe9adbd452019-12-20 08:45:55 -07001967 void __user *buf = u64_to_user_ptr(req->rw.addr);
1968 size_t sqe_len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001969 u8 opcode;
1970
Jens Axboed625c6e2019-12-17 19:53:05 -07001971 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03001972 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07001973 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07001974 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07001975 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001976
Jens Axboe9adbd452019-12-20 08:45:55 -07001977 /* buffer index only valid with fixed read/write */
1978 if (req->rw.kiocb.private)
1979 return -EINVAL;
1980
Jens Axboe3a6820f2019-12-22 15:19:35 -07001981 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
1982 ssize_t ret;
1983 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
1984 *iovec = NULL;
1985 return ret;
1986 }
1987
Jens Axboef67676d2019-12-02 11:03:47 -07001988 if (req->io) {
1989 struct io_async_rw *iorw = &req->io->rw;
1990
1991 *iovec = iorw->iov;
1992 iov_iter_init(iter, rw, *iovec, iorw->nr_segs, iorw->size);
1993 if (iorw->iov == iorw->fast_iov)
1994 *iovec = NULL;
1995 return iorw->size;
1996 }
1997
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001998 if (!req->has_user)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001999 return -EFAULT;
2000
2001#ifdef CONFIG_COMPAT
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002002 if (req->ctx->compat)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002003 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
2004 iovec, iter);
2005#endif
2006
2007 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
2008}
2009
Jens Axboe32960612019-09-23 11:05:34 -06002010/*
2011 * For files that don't have ->read_iter() and ->write_iter(), handle them
2012 * by looping over ->read() or ->write() manually.
2013 */
2014static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
2015 struct iov_iter *iter)
2016{
2017 ssize_t ret = 0;
2018
2019 /*
2020 * Don't support polled IO through this interface, and we can't
2021 * support non-blocking either. For the latter, this just causes
2022 * the kiocb to be handled from an async context.
2023 */
2024 if (kiocb->ki_flags & IOCB_HIPRI)
2025 return -EOPNOTSUPP;
2026 if (kiocb->ki_flags & IOCB_NOWAIT)
2027 return -EAGAIN;
2028
2029 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002030 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06002031 ssize_t nr;
2032
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002033 if (!iov_iter_is_bvec(iter)) {
2034 iovec = iov_iter_iovec(iter);
2035 } else {
2036 /* fixed buffers import bvec */
2037 iovec.iov_base = kmap(iter->bvec->bv_page)
2038 + iter->iov_offset;
2039 iovec.iov_len = min(iter->count,
2040 iter->bvec->bv_len - iter->iov_offset);
2041 }
2042
Jens Axboe32960612019-09-23 11:05:34 -06002043 if (rw == READ) {
2044 nr = file->f_op->read(file, iovec.iov_base,
2045 iovec.iov_len, &kiocb->ki_pos);
2046 } else {
2047 nr = file->f_op->write(file, iovec.iov_base,
2048 iovec.iov_len, &kiocb->ki_pos);
2049 }
2050
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002051 if (iov_iter_is_bvec(iter))
2052 kunmap(iter->bvec->bv_page);
2053
Jens Axboe32960612019-09-23 11:05:34 -06002054 if (nr < 0) {
2055 if (!ret)
2056 ret = nr;
2057 break;
2058 }
2059 ret += nr;
2060 if (nr != iovec.iov_len)
2061 break;
2062 iov_iter_advance(iter, nr);
2063 }
2064
2065 return ret;
2066}
2067
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002068static void io_req_map_rw(struct io_kiocb *req, ssize_t io_size,
Jens Axboef67676d2019-12-02 11:03:47 -07002069 struct iovec *iovec, struct iovec *fast_iov,
2070 struct iov_iter *iter)
2071{
2072 req->io->rw.nr_segs = iter->nr_segs;
2073 req->io->rw.size = io_size;
2074 req->io->rw.iov = iovec;
2075 if (!req->io->rw.iov) {
2076 req->io->rw.iov = req->io->rw.fast_iov;
2077 memcpy(req->io->rw.iov, fast_iov,
2078 sizeof(struct iovec) * iter->nr_segs);
2079 }
2080}
2081
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002082static int io_alloc_async_ctx(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07002083{
Jens Axboed3656342019-12-18 09:50:26 -07002084 if (!io_op_defs[req->opcode].async_ctx)
2085 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002086 req->io = kmalloc(sizeof(*req->io), GFP_KERNEL);
Jens Axboe06b76d42019-12-19 14:44:26 -07002087 return req->io == NULL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002088}
2089
2090static void io_rw_async(struct io_wq_work **workptr)
2091{
2092 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2093 struct iovec *iov = NULL;
2094
2095 if (req->io->rw.iov != req->io->rw.fast_iov)
2096 iov = req->io->rw.iov;
2097 io_wq_submit_work(workptr);
2098 kfree(iov);
2099}
2100
2101static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size,
2102 struct iovec *iovec, struct iovec *fast_iov,
2103 struct iov_iter *iter)
2104{
Jens Axboe74566df2020-01-13 19:23:24 -07002105 if (req->opcode == IORING_OP_READ_FIXED ||
2106 req->opcode == IORING_OP_WRITE_FIXED)
2107 return 0;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002108 if (!req->io && io_alloc_async_ctx(req))
2109 return -ENOMEM;
2110
2111 io_req_map_rw(req, io_size, iovec, fast_iov, iter);
2112 req->work.func = io_rw_async;
2113 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002114}
2115
Jens Axboe3529d8c2019-12-19 18:24:38 -07002116static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2117 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002118{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002119 struct io_async_ctx *io;
2120 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002121 ssize_t ret;
2122
Jens Axboe3529d8c2019-12-19 18:24:38 -07002123 ret = io_prep_rw(req, sqe, force_nonblock);
2124 if (ret)
2125 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002126
Jens Axboe3529d8c2019-12-19 18:24:38 -07002127 if (unlikely(!(req->file->f_mode & FMODE_READ)))
2128 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002129
Jens Axboe3529d8c2019-12-19 18:24:38 -07002130 if (!req->io)
2131 return 0;
2132
2133 io = req->io;
2134 io->rw.iov = io->rw.fast_iov;
2135 req->io = NULL;
2136 ret = io_import_iovec(READ, req, &io->rw.iov, &iter);
2137 req->io = io;
2138 if (ret < 0)
2139 return ret;
2140
2141 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2142 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002143}
2144
Pavel Begunkov267bc902019-11-07 01:41:08 +03002145static int io_read(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06002146 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002147{
2148 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002149 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002150 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002151 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002152 ssize_t io_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002153
Jens Axboe3529d8c2019-12-19 18:24:38 -07002154 ret = io_import_iovec(READ, req, &iovec, &iter);
Jens Axboe06b76d42019-12-19 14:44:26 -07002155 if (ret < 0)
2156 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002157
Jens Axboefd6c2e42019-12-18 12:19:41 -07002158 /* Ensure we clear previously set non-block flag */
2159 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07002160 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002161
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002162 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002163 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06002164 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07002165 req->result = io_size;
2166
2167 /*
2168 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2169 * we know to async punt it even if it was opened O_NONBLOCK
2170 */
Jens Axboe9adbd452019-12-20 08:45:55 -07002171 if (force_nonblock && !io_file_supports_async(req->file)) {
Jens Axboef67676d2019-12-02 11:03:47 -07002172 req->flags |= REQ_F_MUST_PUNT;
2173 goto copy_iov;
2174 }
Jens Axboe9e645e112019-05-10 16:07:28 -06002175
Jens Axboe31b51512019-01-18 22:56:34 -07002176 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002177 ret = rw_verify_area(READ, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002178 if (!ret) {
2179 ssize_t ret2;
2180
Jens Axboe9adbd452019-12-20 08:45:55 -07002181 if (req->file->f_op->read_iter)
2182 ret2 = call_read_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002183 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002184 ret2 = loop_rw_iter(READ, req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002185
Jens Axboe9d93a3f2019-05-15 13:53:07 -06002186 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboef67676d2019-12-02 11:03:47 -07002187 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002188 kiocb_done(kiocb, ret2, nxt, req->in_async);
Jens Axboef67676d2019-12-02 11:03:47 -07002189 } else {
2190copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002191 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002192 inline_vecs, &iter);
2193 if (ret)
2194 goto out_free;
2195 return -EAGAIN;
2196 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002197 }
Jens Axboef67676d2019-12-02 11:03:47 -07002198out_free:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002199 if (!io_wq_current_is_worker())
2200 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002201 return ret;
2202}
2203
Jens Axboe3529d8c2019-12-19 18:24:38 -07002204static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2205 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002206{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002207 struct io_async_ctx *io;
2208 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002209 ssize_t ret;
2210
Jens Axboe3529d8c2019-12-19 18:24:38 -07002211 ret = io_prep_rw(req, sqe, force_nonblock);
2212 if (ret)
2213 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002214
Jens Axboe3529d8c2019-12-19 18:24:38 -07002215 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
2216 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002217
Jens Axboe3529d8c2019-12-19 18:24:38 -07002218 if (!req->io)
2219 return 0;
2220
2221 io = req->io;
2222 io->rw.iov = io->rw.fast_iov;
2223 req->io = NULL;
2224 ret = io_import_iovec(WRITE, req, &io->rw.iov, &iter);
2225 req->io = io;
2226 if (ret < 0)
2227 return ret;
2228
2229 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2230 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002231}
2232
Pavel Begunkov267bc902019-11-07 01:41:08 +03002233static int io_write(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06002234 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002235{
2236 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002237 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002238 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002239 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002240 ssize_t ret, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002241
Jens Axboe3529d8c2019-12-19 18:24:38 -07002242 ret = io_import_iovec(WRITE, req, &iovec, &iter);
Jens Axboe06b76d42019-12-19 14:44:26 -07002243 if (ret < 0)
2244 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002245
Jens Axboefd6c2e42019-12-18 12:19:41 -07002246 /* Ensure we clear previously set non-block flag */
2247 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07002248 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002249
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002250 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002251 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06002252 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07002253 req->result = io_size;
2254
2255 /*
2256 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2257 * we know to async punt it even if it was opened O_NONBLOCK
2258 */
2259 if (force_nonblock && !io_file_supports_async(req->file)) {
2260 req->flags |= REQ_F_MUST_PUNT;
2261 goto copy_iov;
2262 }
2263
Jens Axboe10d59342019-12-09 20:16:22 -07002264 /* file path doesn't support NOWAIT for non-direct_IO */
2265 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
2266 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07002267 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06002268
Jens Axboe31b51512019-01-18 22:56:34 -07002269 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002270 ret = rw_verify_area(WRITE, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002271 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01002272 ssize_t ret2;
2273
Jens Axboe2b188cc2019-01-07 10:46:33 -07002274 /*
2275 * Open-code file_start_write here to grab freeze protection,
2276 * which will be released by another thread in
2277 * io_complete_rw(). Fool lockdep by telling it the lock got
2278 * released so that it doesn't complain about the held lock when
2279 * we return to userspace.
2280 */
Jens Axboe491381ce2019-10-17 09:20:46 -06002281 if (req->flags & REQ_F_ISREG) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002282 __sb_start_write(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002283 SB_FREEZE_WRITE, true);
Jens Axboe9adbd452019-12-20 08:45:55 -07002284 __sb_writers_release(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002285 SB_FREEZE_WRITE);
2286 }
2287 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01002288
Jens Axboe9adbd452019-12-20 08:45:55 -07002289 if (req->file->f_op->write_iter)
2290 ret2 = call_write_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002291 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002292 ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter);
Jens Axboef67676d2019-12-02 11:03:47 -07002293 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002294 kiocb_done(kiocb, ret2, nxt, req->in_async);
Jens Axboef67676d2019-12-02 11:03:47 -07002295 } else {
2296copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002297 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002298 inline_vecs, &iter);
2299 if (ret)
2300 goto out_free;
2301 return -EAGAIN;
2302 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002303 }
Jens Axboe31b51512019-01-18 22:56:34 -07002304out_free:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002305 if (!io_wq_current_is_worker())
2306 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002307 return ret;
2308}
2309
2310/*
2311 * IORING_OP_NOP just posts a completion event, nothing else.
2312 */
Jens Axboe78e19bb2019-11-06 15:21:34 -07002313static int io_nop(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002314{
2315 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002316
Jens Axboedef596e2019-01-09 08:59:42 -07002317 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2318 return -EINVAL;
2319
Jens Axboe78e19bb2019-11-06 15:21:34 -07002320 io_cqring_add_event(req, 0);
Jens Axboee65ef562019-03-12 10:16:44 -06002321 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002322 return 0;
2323}
2324
Jens Axboe3529d8c2019-12-19 18:24:38 -07002325static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002326{
Jens Axboe6b063142019-01-10 22:13:58 -07002327 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002328
Jens Axboe09bb8392019-03-13 12:39:28 -06002329 if (!req->file)
2330 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002331
Jens Axboe6b063142019-01-10 22:13:58 -07002332 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07002333 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07002334 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002335 return -EINVAL;
2336
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002337 req->sync.flags = READ_ONCE(sqe->fsync_flags);
2338 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
2339 return -EINVAL;
2340
2341 req->sync.off = READ_ONCE(sqe->off);
2342 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002343 return 0;
2344}
2345
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002346static bool io_req_cancelled(struct io_kiocb *req)
2347{
2348 if (req->work.flags & IO_WQ_WORK_CANCEL) {
2349 req_set_fail_links(req);
2350 io_cqring_add_event(req, -ECANCELED);
2351 io_put_req(req);
2352 return true;
2353 }
2354
2355 return false;
2356}
2357
Jens Axboe78912932020-01-14 22:09:06 -07002358static void io_link_work_cb(struct io_wq_work **workptr)
2359{
2360 struct io_wq_work *work = *workptr;
2361 struct io_kiocb *link = work->data;
2362
2363 io_queue_linked_timeout(link);
2364 work->func = io_wq_submit_work;
2365}
2366
2367static void io_wq_assign_next(struct io_wq_work **workptr, struct io_kiocb *nxt)
2368{
2369 struct io_kiocb *link;
2370
2371 io_prep_async_work(nxt, &link);
2372 *workptr = &nxt->work;
2373 if (link) {
2374 nxt->work.flags |= IO_WQ_WORK_CB;
2375 nxt->work.func = io_link_work_cb;
2376 nxt->work.data = link;
2377 }
2378}
2379
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002380static void io_fsync_finish(struct io_wq_work **workptr)
2381{
2382 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2383 loff_t end = req->sync.off + req->sync.len;
2384 struct io_kiocb *nxt = NULL;
2385 int ret;
2386
2387 if (io_req_cancelled(req))
2388 return;
2389
Jens Axboe9adbd452019-12-20 08:45:55 -07002390 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002391 end > 0 ? end : LLONG_MAX,
2392 req->sync.flags & IORING_FSYNC_DATASYNC);
2393 if (ret < 0)
2394 req_set_fail_links(req);
2395 io_cqring_add_event(req, ret);
2396 io_put_req_find_next(req, &nxt);
2397 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07002398 io_wq_assign_next(workptr, nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002399}
2400
Jens Axboefc4df992019-12-10 14:38:45 -07002401static int io_fsync(struct io_kiocb *req, struct io_kiocb **nxt,
2402 bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002403{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002404 struct io_wq_work *work, *old_work;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002405
2406 /* fsync always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002407 if (force_nonblock) {
2408 io_put_req(req);
2409 req->work.func = io_fsync_finish;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002410 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002411 }
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002412
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002413 work = old_work = &req->work;
2414 io_fsync_finish(&work);
2415 if (work && work != old_work)
2416 *nxt = container_of(work, struct io_kiocb, work);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002417 return 0;
2418}
2419
Jens Axboed63d1b52019-12-10 10:38:56 -07002420static void io_fallocate_finish(struct io_wq_work **workptr)
2421{
2422 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2423 struct io_kiocb *nxt = NULL;
2424 int ret;
2425
2426 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
2427 req->sync.len);
2428 if (ret < 0)
2429 req_set_fail_links(req);
2430 io_cqring_add_event(req, ret);
2431 io_put_req_find_next(req, &nxt);
2432 if (nxt)
2433 io_wq_assign_next(workptr, nxt);
2434}
2435
2436static int io_fallocate_prep(struct io_kiocb *req,
2437 const struct io_uring_sqe *sqe)
2438{
2439 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
2440 return -EINVAL;
2441
2442 req->sync.off = READ_ONCE(sqe->off);
2443 req->sync.len = READ_ONCE(sqe->addr);
2444 req->sync.mode = READ_ONCE(sqe->len);
2445 return 0;
2446}
2447
2448static int io_fallocate(struct io_kiocb *req, struct io_kiocb **nxt,
2449 bool force_nonblock)
2450{
2451 struct io_wq_work *work, *old_work;
2452
2453 /* fallocate always requiring blocking context */
2454 if (force_nonblock) {
2455 io_put_req(req);
2456 req->work.func = io_fallocate_finish;
2457 return -EAGAIN;
2458 }
2459
2460 work = old_work = &req->work;
2461 io_fallocate_finish(&work);
2462 if (work && work != old_work)
2463 *nxt = container_of(work, struct io_kiocb, work);
2464
2465 return 0;
2466}
2467
Jens Axboe15b71ab2019-12-11 11:20:36 -07002468static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2469{
2470 int ret;
2471
2472 if (sqe->ioprio || sqe->buf_index)
2473 return -EINVAL;
2474
2475 req->open.dfd = READ_ONCE(sqe->fd);
Jens Axboec12cedf2020-01-08 17:41:21 -07002476 req->open.how.mode = READ_ONCE(sqe->len);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002477 req->open.fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboec12cedf2020-01-08 17:41:21 -07002478 req->open.how.flags = READ_ONCE(sqe->open_flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002479
2480 req->open.filename = getname(req->open.fname);
2481 if (IS_ERR(req->open.filename)) {
2482 ret = PTR_ERR(req->open.filename);
2483 req->open.filename = NULL;
2484 return ret;
2485 }
2486
2487 return 0;
2488}
2489
2490static int io_openat(struct io_kiocb *req, struct io_kiocb **nxt,
2491 bool force_nonblock)
2492{
2493 struct open_flags op;
2494 struct open_how how;
2495 struct file *file;
2496 int ret;
2497
2498 if (force_nonblock) {
2499 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
2500 return -EAGAIN;
2501 }
2502
Jens Axboec12cedf2020-01-08 17:41:21 -07002503 how = build_open_how(req->open.how.flags, req->open.how.mode);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002504 ret = build_open_flags(&how, &op);
2505 if (ret)
2506 goto err;
2507
2508 ret = get_unused_fd_flags(how.flags);
2509 if (ret < 0)
2510 goto err;
2511
2512 file = do_filp_open(req->open.dfd, req->open.filename, &op);
2513 if (IS_ERR(file)) {
2514 put_unused_fd(ret);
2515 ret = PTR_ERR(file);
2516 } else {
2517 fsnotify_open(file);
2518 fd_install(ret, file);
2519 }
2520err:
2521 putname(req->open.filename);
2522 if (ret < 0)
2523 req_set_fail_links(req);
2524 io_cqring_add_event(req, ret);
2525 io_put_req_find_next(req, nxt);
2526 return 0;
2527}
2528
Jens Axboec1ca7572019-12-25 22:18:28 -07002529static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2530{
2531#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
2532 if (sqe->ioprio || sqe->buf_index || sqe->off)
2533 return -EINVAL;
2534
2535 req->madvise.addr = READ_ONCE(sqe->addr);
2536 req->madvise.len = READ_ONCE(sqe->len);
2537 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
2538 return 0;
2539#else
2540 return -EOPNOTSUPP;
2541#endif
2542}
2543
2544static int io_madvise(struct io_kiocb *req, struct io_kiocb **nxt,
2545 bool force_nonblock)
2546{
2547#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
2548 struct io_madvise *ma = &req->madvise;
2549 int ret;
2550
2551 if (force_nonblock)
2552 return -EAGAIN;
2553
2554 ret = do_madvise(ma->addr, ma->len, ma->advice);
2555 if (ret < 0)
2556 req_set_fail_links(req);
2557 io_cqring_add_event(req, ret);
2558 io_put_req_find_next(req, nxt);
2559 return 0;
2560#else
2561 return -EOPNOTSUPP;
2562#endif
2563}
2564
Jens Axboe4840e412019-12-25 22:03:45 -07002565static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2566{
2567 if (sqe->ioprio || sqe->buf_index || sqe->addr)
2568 return -EINVAL;
2569
2570 req->fadvise.offset = READ_ONCE(sqe->off);
2571 req->fadvise.len = READ_ONCE(sqe->len);
2572 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
2573 return 0;
2574}
2575
2576static int io_fadvise(struct io_kiocb *req, struct io_kiocb **nxt,
2577 bool force_nonblock)
2578{
2579 struct io_fadvise *fa = &req->fadvise;
2580 int ret;
2581
2582 /* DONTNEED may block, others _should_ not */
2583 if (fa->advice == POSIX_FADV_DONTNEED && force_nonblock)
2584 return -EAGAIN;
2585
2586 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
2587 if (ret < 0)
2588 req_set_fail_links(req);
2589 io_cqring_add_event(req, ret);
2590 io_put_req_find_next(req, nxt);
2591 return 0;
2592}
2593
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002594static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2595{
2596 unsigned lookup_flags;
2597 int ret;
2598
2599 if (sqe->ioprio || sqe->buf_index)
2600 return -EINVAL;
2601
2602 req->open.dfd = READ_ONCE(sqe->fd);
2603 req->open.mask = READ_ONCE(sqe->len);
2604 req->open.fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
2605 req->open.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboec12cedf2020-01-08 17:41:21 -07002606 req->open.how.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002607
Jens Axboec12cedf2020-01-08 17:41:21 -07002608 if (vfs_stat_set_lookup_flags(&lookup_flags, req->open.how.flags))
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002609 return -EINVAL;
2610
2611 req->open.filename = getname_flags(req->open.fname, lookup_flags, NULL);
2612 if (IS_ERR(req->open.filename)) {
2613 ret = PTR_ERR(req->open.filename);
2614 req->open.filename = NULL;
2615 return ret;
2616 }
2617
2618 return 0;
2619}
2620
2621static int io_statx(struct io_kiocb *req, struct io_kiocb **nxt,
2622 bool force_nonblock)
2623{
2624 struct io_open *ctx = &req->open;
2625 unsigned lookup_flags;
2626 struct path path;
2627 struct kstat stat;
2628 int ret;
2629
2630 if (force_nonblock)
2631 return -EAGAIN;
2632
Jens Axboec12cedf2020-01-08 17:41:21 -07002633 if (vfs_stat_set_lookup_flags(&lookup_flags, ctx->how.flags))
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002634 return -EINVAL;
2635
2636retry:
2637 /* filename_lookup() drops it, keep a reference */
2638 ctx->filename->refcnt++;
2639
2640 ret = filename_lookup(ctx->dfd, ctx->filename, lookup_flags, &path,
2641 NULL);
2642 if (ret)
2643 goto err;
2644
Jens Axboec12cedf2020-01-08 17:41:21 -07002645 ret = vfs_getattr(&path, &stat, ctx->mask, ctx->how.flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002646 path_put(&path);
2647 if (retry_estale(ret, lookup_flags)) {
2648 lookup_flags |= LOOKUP_REVAL;
2649 goto retry;
2650 }
2651 if (!ret)
2652 ret = cp_statx(&stat, ctx->buffer);
2653err:
2654 putname(ctx->filename);
2655 if (ret < 0)
2656 req_set_fail_links(req);
2657 io_cqring_add_event(req, ret);
2658 io_put_req_find_next(req, nxt);
2659 return 0;
2660}
2661
Jens Axboeb5dba592019-12-11 14:02:38 -07002662static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2663{
2664 /*
2665 * If we queue this for async, it must not be cancellable. That would
2666 * leave the 'file' in an undeterminate state.
2667 */
2668 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
2669
2670 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
2671 sqe->rw_flags || sqe->buf_index)
2672 return -EINVAL;
2673 if (sqe->flags & IOSQE_FIXED_FILE)
2674 return -EINVAL;
2675
2676 req->close.fd = READ_ONCE(sqe->fd);
2677 if (req->file->f_op == &io_uring_fops ||
2678 req->close.fd == req->ring_fd)
2679 return -EBADF;
2680
2681 return 0;
2682}
2683
2684static void io_close_finish(struct io_wq_work **workptr)
2685{
2686 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2687 struct io_kiocb *nxt = NULL;
2688
2689 /* Invoked with files, we need to do the close */
2690 if (req->work.files) {
2691 int ret;
2692
2693 ret = filp_close(req->close.put_file, req->work.files);
2694 if (ret < 0) {
2695 req_set_fail_links(req);
2696 }
2697 io_cqring_add_event(req, ret);
2698 }
2699
2700 fput(req->close.put_file);
2701
2702 /* we bypassed the re-issue, drop the submission reference */
2703 io_put_req(req);
2704 io_put_req_find_next(req, &nxt);
2705 if (nxt)
2706 io_wq_assign_next(workptr, nxt);
2707}
2708
2709static int io_close(struct io_kiocb *req, struct io_kiocb **nxt,
2710 bool force_nonblock)
2711{
2712 int ret;
2713
2714 req->close.put_file = NULL;
2715 ret = __close_fd_get_file(req->close.fd, &req->close.put_file);
2716 if (ret < 0)
2717 return ret;
2718
2719 /* if the file has a flush method, be safe and punt to async */
2720 if (req->close.put_file->f_op->flush && !io_wq_current_is_worker()) {
2721 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
2722 goto eagain;
2723 }
2724
2725 /*
2726 * No ->flush(), safely close from here and just punt the
2727 * fput() to async context.
2728 */
2729 ret = filp_close(req->close.put_file, current->files);
2730
2731 if (ret < 0)
2732 req_set_fail_links(req);
2733 io_cqring_add_event(req, ret);
2734
2735 if (io_wq_current_is_worker()) {
2736 struct io_wq_work *old_work, *work;
2737
2738 old_work = work = &req->work;
2739 io_close_finish(&work);
2740 if (work && work != old_work)
2741 *nxt = container_of(work, struct io_kiocb, work);
2742 return 0;
2743 }
2744
2745eagain:
2746 req->work.func = io_close_finish;
2747 return -EAGAIN;
2748}
2749
Jens Axboe3529d8c2019-12-19 18:24:38 -07002750static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002751{
2752 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002753
2754 if (!req->file)
2755 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002756
2757 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2758 return -EINVAL;
2759 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
2760 return -EINVAL;
2761
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002762 req->sync.off = READ_ONCE(sqe->off);
2763 req->sync.len = READ_ONCE(sqe->len);
2764 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002765 return 0;
2766}
2767
2768static void io_sync_file_range_finish(struct io_wq_work **workptr)
2769{
2770 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2771 struct io_kiocb *nxt = NULL;
2772 int ret;
2773
2774 if (io_req_cancelled(req))
2775 return;
2776
Jens Axboe9adbd452019-12-20 08:45:55 -07002777 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002778 req->sync.flags);
2779 if (ret < 0)
2780 req_set_fail_links(req);
2781 io_cqring_add_event(req, ret);
2782 io_put_req_find_next(req, &nxt);
2783 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07002784 io_wq_assign_next(workptr, nxt);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002785}
2786
Jens Axboefc4df992019-12-10 14:38:45 -07002787static int io_sync_file_range(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002788 bool force_nonblock)
2789{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002790 struct io_wq_work *work, *old_work;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002791
2792 /* sync_file_range always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002793 if (force_nonblock) {
2794 io_put_req(req);
2795 req->work.func = io_sync_file_range_finish;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002796 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002797 }
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002798
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002799 work = old_work = &req->work;
2800 io_sync_file_range_finish(&work);
2801 if (work && work != old_work)
2802 *nxt = container_of(work, struct io_kiocb, work);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002803 return 0;
2804}
2805
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002806#if defined(CONFIG_NET)
2807static void io_sendrecv_async(struct io_wq_work **workptr)
2808{
2809 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2810 struct iovec *iov = NULL;
2811
2812 if (req->io->rw.iov != req->io->rw.fast_iov)
2813 iov = req->io->msg.iov;
2814 io_wq_submit_work(workptr);
2815 kfree(iov);
2816}
2817#endif
2818
Jens Axboe3529d8c2019-12-19 18:24:38 -07002819static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06002820{
Jens Axboe03b12302019-12-02 18:50:25 -07002821#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07002822 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002823 struct io_async_ctx *io = req->io;
Jens Axboe03b12302019-12-02 18:50:25 -07002824
Jens Axboee47293f2019-12-20 08:58:21 -07002825 sr->msg_flags = READ_ONCE(sqe->msg_flags);
2826 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07002827 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07002828
Jens Axboefddafac2020-01-04 20:19:44 -07002829 if (!io || req->opcode == IORING_OP_SEND)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002830 return 0;
2831
Jens Axboed9688562019-12-09 19:35:20 -07002832 io->msg.iov = io->msg.fast_iov;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002833 return sendmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07002834 &io->msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002835#else
Jens Axboee47293f2019-12-20 08:58:21 -07002836 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07002837#endif
2838}
2839
Jens Axboefc4df992019-12-10 14:38:45 -07002840static int io_sendmsg(struct io_kiocb *req, struct io_kiocb **nxt,
2841 bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07002842{
2843#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07002844 struct io_async_msghdr *kmsg = NULL;
Jens Axboe03b12302019-12-02 18:50:25 -07002845 struct socket *sock;
2846 int ret;
2847
2848 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2849 return -EINVAL;
2850
2851 sock = sock_from_file(req->file, &ret);
2852 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002853 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07002854 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -07002855 unsigned flags;
2856
Jens Axboe03b12302019-12-02 18:50:25 -07002857 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07002858 kmsg = &req->io->msg;
2859 kmsg->msg.msg_name = &addr;
2860 /* if iov is set, it's allocated already */
2861 if (!kmsg->iov)
2862 kmsg->iov = kmsg->fast_iov;
2863 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07002864 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07002865 struct io_sr_msg *sr = &req->sr_msg;
2866
Jens Axboe0b416c32019-12-15 10:57:46 -07002867 kmsg = &io.msg;
2868 kmsg->msg.msg_name = &addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002869
2870 io.msg.iov = io.msg.fast_iov;
2871 ret = sendmsg_copy_msghdr(&io.msg.msg, sr->msg,
2872 sr->msg_flags, &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002873 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002874 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07002875 }
2876
Jens Axboee47293f2019-12-20 08:58:21 -07002877 flags = req->sr_msg.msg_flags;
2878 if (flags & MSG_DONTWAIT)
2879 req->flags |= REQ_F_NOWAIT;
2880 else if (force_nonblock)
2881 flags |= MSG_DONTWAIT;
2882
Jens Axboe0b416c32019-12-15 10:57:46 -07002883 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Jens Axboe03b12302019-12-02 18:50:25 -07002884 if (force_nonblock && ret == -EAGAIN) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002885 if (req->io)
2886 return -EAGAIN;
2887 if (io_alloc_async_ctx(req))
2888 return -ENOMEM;
2889 memcpy(&req->io->msg, &io.msg, sizeof(io.msg));
2890 req->work.func = io_sendrecv_async;
Jens Axboe0b416c32019-12-15 10:57:46 -07002891 return -EAGAIN;
Jens Axboe03b12302019-12-02 18:50:25 -07002892 }
2893 if (ret == -ERESTARTSYS)
2894 ret = -EINTR;
2895 }
2896
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002897 if (!io_wq_current_is_worker() && kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07002898 kfree(kmsg->iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002899 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002900 if (ret < 0)
2901 req_set_fail_links(req);
Jens Axboe03b12302019-12-02 18:50:25 -07002902 io_put_req_find_next(req, nxt);
2903 return 0;
2904#else
2905 return -EOPNOTSUPP;
2906#endif
2907}
2908
Jens Axboefddafac2020-01-04 20:19:44 -07002909static int io_send(struct io_kiocb *req, struct io_kiocb **nxt,
2910 bool force_nonblock)
2911{
2912#if defined(CONFIG_NET)
2913 struct socket *sock;
2914 int ret;
2915
2916 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2917 return -EINVAL;
2918
2919 sock = sock_from_file(req->file, &ret);
2920 if (sock) {
2921 struct io_sr_msg *sr = &req->sr_msg;
2922 struct msghdr msg;
2923 struct iovec iov;
2924 unsigned flags;
2925
2926 ret = import_single_range(WRITE, sr->buf, sr->len, &iov,
2927 &msg.msg_iter);
2928 if (ret)
2929 return ret;
2930
2931 msg.msg_name = NULL;
2932 msg.msg_control = NULL;
2933 msg.msg_controllen = 0;
2934 msg.msg_namelen = 0;
2935
2936 flags = req->sr_msg.msg_flags;
2937 if (flags & MSG_DONTWAIT)
2938 req->flags |= REQ_F_NOWAIT;
2939 else if (force_nonblock)
2940 flags |= MSG_DONTWAIT;
2941
2942 ret = __sys_sendmsg_sock(sock, &msg, flags);
2943 if (force_nonblock && ret == -EAGAIN)
2944 return -EAGAIN;
2945 if (ret == -ERESTARTSYS)
2946 ret = -EINTR;
2947 }
2948
2949 io_cqring_add_event(req, ret);
2950 if (ret < 0)
2951 req_set_fail_links(req);
2952 io_put_req_find_next(req, nxt);
2953 return 0;
2954#else
2955 return -EOPNOTSUPP;
2956#endif
2957}
2958
Jens Axboe3529d8c2019-12-19 18:24:38 -07002959static int io_recvmsg_prep(struct io_kiocb *req,
2960 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07002961{
2962#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07002963 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002964 struct io_async_ctx *io = req->io;
Jens Axboe06b76d42019-12-19 14:44:26 -07002965
Jens Axboe3529d8c2019-12-19 18:24:38 -07002966 sr->msg_flags = READ_ONCE(sqe->msg_flags);
2967 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
2968
Jens Axboefddafac2020-01-04 20:19:44 -07002969 if (!io || req->opcode == IORING_OP_RECV)
Jens Axboe06b76d42019-12-19 14:44:26 -07002970 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07002971
Jens Axboed9688562019-12-09 19:35:20 -07002972 io->msg.iov = io->msg.fast_iov;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002973 return recvmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07002974 &io->msg.uaddr, &io->msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002975#else
Jens Axboee47293f2019-12-20 08:58:21 -07002976 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07002977#endif
2978}
2979
Jens Axboefc4df992019-12-10 14:38:45 -07002980static int io_recvmsg(struct io_kiocb *req, struct io_kiocb **nxt,
2981 bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07002982{
2983#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07002984 struct io_async_msghdr *kmsg = NULL;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002985 struct socket *sock;
2986 int ret;
2987
2988 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2989 return -EINVAL;
2990
2991 sock = sock_from_file(req->file, &ret);
2992 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002993 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07002994 struct sockaddr_storage addr;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002995 unsigned flags;
2996
Jens Axboe03b12302019-12-02 18:50:25 -07002997 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07002998 kmsg = &req->io->msg;
2999 kmsg->msg.msg_name = &addr;
3000 /* if iov is set, it's allocated already */
3001 if (!kmsg->iov)
3002 kmsg->iov = kmsg->fast_iov;
3003 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07003004 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003005 struct io_sr_msg *sr = &req->sr_msg;
3006
Jens Axboe0b416c32019-12-15 10:57:46 -07003007 kmsg = &io.msg;
3008 kmsg->msg.msg_name = &addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003009
3010 io.msg.iov = io.msg.fast_iov;
3011 ret = recvmsg_copy_msghdr(&io.msg.msg, sr->msg,
3012 sr->msg_flags, &io.msg.uaddr,
3013 &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07003014 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003015 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003016 }
Jens Axboe0fa03c62019-04-19 13:34:07 -06003017
Jens Axboee47293f2019-12-20 08:58:21 -07003018 flags = req->sr_msg.msg_flags;
3019 if (flags & MSG_DONTWAIT)
3020 req->flags |= REQ_F_NOWAIT;
3021 else if (force_nonblock)
3022 flags |= MSG_DONTWAIT;
3023
3024 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.msg,
3025 kmsg->uaddr, flags);
Jens Axboe03b12302019-12-02 18:50:25 -07003026 if (force_nonblock && ret == -EAGAIN) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003027 if (req->io)
3028 return -EAGAIN;
3029 if (io_alloc_async_ctx(req))
3030 return -ENOMEM;
3031 memcpy(&req->io->msg, &io.msg, sizeof(io.msg));
3032 req->work.func = io_sendrecv_async;
Jens Axboe0b416c32019-12-15 10:57:46 -07003033 return -EAGAIN;
Jens Axboe03b12302019-12-02 18:50:25 -07003034 }
Jens Axboe441cdbd2019-12-02 18:49:10 -07003035 if (ret == -ERESTARTSYS)
3036 ret = -EINTR;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003037 }
3038
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003039 if (!io_wq_current_is_worker() && kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07003040 kfree(kmsg->iov);
Jens Axboe78e19bb2019-11-06 15:21:34 -07003041 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003042 if (ret < 0)
3043 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003044 io_put_req_find_next(req, nxt);
Jens Axboe0fa03c62019-04-19 13:34:07 -06003045 return 0;
3046#else
3047 return -EOPNOTSUPP;
3048#endif
3049}
3050
Jens Axboefddafac2020-01-04 20:19:44 -07003051static int io_recv(struct io_kiocb *req, struct io_kiocb **nxt,
3052 bool force_nonblock)
3053{
3054#if defined(CONFIG_NET)
3055 struct socket *sock;
3056 int ret;
3057
3058 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3059 return -EINVAL;
3060
3061 sock = sock_from_file(req->file, &ret);
3062 if (sock) {
3063 struct io_sr_msg *sr = &req->sr_msg;
3064 struct msghdr msg;
3065 struct iovec iov;
3066 unsigned flags;
3067
3068 ret = import_single_range(READ, sr->buf, sr->len, &iov,
3069 &msg.msg_iter);
3070 if (ret)
3071 return ret;
3072
3073 msg.msg_name = NULL;
3074 msg.msg_control = NULL;
3075 msg.msg_controllen = 0;
3076 msg.msg_namelen = 0;
3077 msg.msg_iocb = NULL;
3078 msg.msg_flags = 0;
3079
3080 flags = req->sr_msg.msg_flags;
3081 if (flags & MSG_DONTWAIT)
3082 req->flags |= REQ_F_NOWAIT;
3083 else if (force_nonblock)
3084 flags |= MSG_DONTWAIT;
3085
3086 ret = __sys_recvmsg_sock(sock, &msg, NULL, NULL, flags);
3087 if (force_nonblock && ret == -EAGAIN)
3088 return -EAGAIN;
3089 if (ret == -ERESTARTSYS)
3090 ret = -EINTR;
3091 }
3092
3093 io_cqring_add_event(req, ret);
3094 if (ret < 0)
3095 req_set_fail_links(req);
3096 io_put_req_find_next(req, nxt);
3097 return 0;
3098#else
3099 return -EOPNOTSUPP;
3100#endif
3101}
3102
3103
Jens Axboe3529d8c2019-12-19 18:24:38 -07003104static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003105{
3106#if defined(CONFIG_NET)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003107 struct io_accept *accept = &req->accept;
3108
Jens Axboe17f2fe32019-10-17 14:42:58 -06003109 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3110 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05003111 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003112 return -EINVAL;
3113
Jens Axboed55e5f52019-12-11 16:12:15 -07003114 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3115 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003116 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003117 return 0;
3118#else
3119 return -EOPNOTSUPP;
3120#endif
3121}
Jens Axboe17f2fe32019-10-17 14:42:58 -06003122
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003123#if defined(CONFIG_NET)
3124static int __io_accept(struct io_kiocb *req, struct io_kiocb **nxt,
3125 bool force_nonblock)
3126{
3127 struct io_accept *accept = &req->accept;
3128 unsigned file_flags;
3129 int ret;
3130
3131 file_flags = force_nonblock ? O_NONBLOCK : 0;
3132 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
3133 accept->addr_len, accept->flags);
3134 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003135 return -EAGAIN;
Jens Axboe8e3cca12019-11-09 19:52:33 -07003136 if (ret == -ERESTARTSYS)
3137 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003138 if (ret < 0)
3139 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07003140 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003141 io_put_req_find_next(req, nxt);
Jens Axboe17f2fe32019-10-17 14:42:58 -06003142 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003143}
3144
3145static void io_accept_finish(struct io_wq_work **workptr)
3146{
3147 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3148 struct io_kiocb *nxt = NULL;
3149
3150 if (io_req_cancelled(req))
3151 return;
3152 __io_accept(req, &nxt, false);
3153 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07003154 io_wq_assign_next(workptr, nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003155}
3156#endif
3157
3158static int io_accept(struct io_kiocb *req, struct io_kiocb **nxt,
3159 bool force_nonblock)
3160{
3161#if defined(CONFIG_NET)
3162 int ret;
3163
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003164 ret = __io_accept(req, nxt, force_nonblock);
3165 if (ret == -EAGAIN && force_nonblock) {
3166 req->work.func = io_accept_finish;
3167 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
3168 io_put_req(req);
3169 return -EAGAIN;
3170 }
3171 return 0;
Jens Axboe17f2fe32019-10-17 14:42:58 -06003172#else
3173 return -EOPNOTSUPP;
3174#endif
3175}
3176
Jens Axboe3529d8c2019-12-19 18:24:38 -07003177static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07003178{
3179#if defined(CONFIG_NET)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003180 struct io_connect *conn = &req->connect;
3181 struct io_async_ctx *io = req->io;
Jens Axboef499a022019-12-02 16:28:46 -07003182
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003183 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3184 return -EINVAL;
3185 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
3186 return -EINVAL;
3187
Jens Axboe3529d8c2019-12-19 18:24:38 -07003188 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3189 conn->addr_len = READ_ONCE(sqe->addr2);
3190
3191 if (!io)
3192 return 0;
3193
3194 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003195 &io->connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07003196#else
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003197 return -EOPNOTSUPP;
Jens Axboef499a022019-12-02 16:28:46 -07003198#endif
3199}
3200
Jens Axboefc4df992019-12-10 14:38:45 -07003201static int io_connect(struct io_kiocb *req, struct io_kiocb **nxt,
3202 bool force_nonblock)
Jens Axboef8e85cf2019-11-23 14:24:24 -07003203{
3204#if defined(CONFIG_NET)
Jens Axboef499a022019-12-02 16:28:46 -07003205 struct io_async_ctx __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003206 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003207 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003208
Jens Axboef499a022019-12-02 16:28:46 -07003209 if (req->io) {
3210 io = req->io;
3211 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003212 ret = move_addr_to_kernel(req->connect.addr,
3213 req->connect.addr_len,
3214 &__io.connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07003215 if (ret)
3216 goto out;
3217 io = &__io;
3218 }
3219
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003220 file_flags = force_nonblock ? O_NONBLOCK : 0;
3221
3222 ret = __sys_connect_file(req->file, &io->connect.address,
3223 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07003224 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003225 if (req->io)
3226 return -EAGAIN;
3227 if (io_alloc_async_ctx(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07003228 ret = -ENOMEM;
3229 goto out;
3230 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003231 memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect));
Jens Axboef8e85cf2019-11-23 14:24:24 -07003232 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07003233 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07003234 if (ret == -ERESTARTSYS)
3235 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07003236out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003237 if (ret < 0)
3238 req_set_fail_links(req);
Jens Axboef8e85cf2019-11-23 14:24:24 -07003239 io_cqring_add_event(req, ret);
3240 io_put_req_find_next(req, nxt);
3241 return 0;
3242#else
3243 return -EOPNOTSUPP;
3244#endif
3245}
3246
Jens Axboe221c5eb2019-01-17 09:41:58 -07003247static void io_poll_remove_one(struct io_kiocb *req)
3248{
3249 struct io_poll_iocb *poll = &req->poll;
3250
3251 spin_lock(&poll->head->lock);
3252 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07003253 if (!list_empty(&poll->wait.entry)) {
3254 list_del_init(&poll->wait.entry);
Jackie Liua197f662019-11-08 08:09:12 -07003255 io_queue_async_work(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003256 }
3257 spin_unlock(&poll->head->lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07003258 hash_del(&req->hash_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003259}
3260
3261static void io_poll_remove_all(struct io_ring_ctx *ctx)
3262{
Jens Axboe78076bb2019-12-04 19:56:40 -07003263 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003264 struct io_kiocb *req;
Jens Axboe78076bb2019-12-04 19:56:40 -07003265 int i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003266
3267 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07003268 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
3269 struct hlist_head *list;
3270
3271 list = &ctx->cancel_hash[i];
3272 hlist_for_each_entry_safe(req, tmp, list, hash_node)
3273 io_poll_remove_one(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003274 }
3275 spin_unlock_irq(&ctx->completion_lock);
3276}
3277
Jens Axboe47f46762019-11-09 17:43:02 -07003278static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
3279{
Jens Axboe78076bb2019-12-04 19:56:40 -07003280 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07003281 struct io_kiocb *req;
3282
Jens Axboe78076bb2019-12-04 19:56:40 -07003283 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
3284 hlist_for_each_entry(req, list, hash_node) {
3285 if (sqe_addr == req->user_data) {
Jens Axboeeac406c2019-11-14 12:09:58 -07003286 io_poll_remove_one(req);
3287 return 0;
3288 }
Jens Axboe47f46762019-11-09 17:43:02 -07003289 }
3290
3291 return -ENOENT;
3292}
3293
Jens Axboe3529d8c2019-12-19 18:24:38 -07003294static int io_poll_remove_prep(struct io_kiocb *req,
3295 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003296{
Jens Axboe221c5eb2019-01-17 09:41:58 -07003297 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3298 return -EINVAL;
3299 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
3300 sqe->poll_events)
3301 return -EINVAL;
3302
Jens Axboe0969e782019-12-17 18:40:57 -07003303 req->poll.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07003304 return 0;
3305}
3306
3307/*
3308 * Find a running poll command that matches one specified in sqe->addr,
3309 * and remove it if found.
3310 */
3311static int io_poll_remove(struct io_kiocb *req)
3312{
3313 struct io_ring_ctx *ctx = req->ctx;
3314 u64 addr;
3315 int ret;
3316
Jens Axboe0969e782019-12-17 18:40:57 -07003317 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003318 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07003319 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003320 spin_unlock_irq(&ctx->completion_lock);
3321
Jens Axboe78e19bb2019-11-06 15:21:34 -07003322 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003323 if (ret < 0)
3324 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06003325 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003326 return 0;
3327}
3328
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003329static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003330{
Jackie Liua197f662019-11-08 08:09:12 -07003331 struct io_ring_ctx *ctx = req->ctx;
3332
Jens Axboe8c838782019-03-12 15:48:16 -06003333 req->poll.done = true;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003334 if (error)
3335 io_cqring_fill_event(req, error);
3336 else
3337 io_cqring_fill_event(req, mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06003338 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003339}
3340
Jens Axboe561fb042019-10-24 07:25:42 -06003341static void io_poll_complete_work(struct io_wq_work **workptr)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003342{
Jens Axboe561fb042019-10-24 07:25:42 -06003343 struct io_wq_work *work = *workptr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003344 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
3345 struct io_poll_iocb *poll = &req->poll;
3346 struct poll_table_struct pt = { ._key = poll->events };
3347 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe89723d02019-11-05 15:32:58 -07003348 struct io_kiocb *nxt = NULL;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003349 __poll_t mask = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003350 int ret = 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003351
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003352 if (work->flags & IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06003353 WRITE_ONCE(poll->canceled, true);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003354 ret = -ECANCELED;
3355 } else if (READ_ONCE(poll->canceled)) {
3356 ret = -ECANCELED;
3357 }
Jens Axboe561fb042019-10-24 07:25:42 -06003358
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003359 if (ret != -ECANCELED)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003360 mask = vfs_poll(poll->file, &pt) & poll->events;
3361
3362 /*
3363 * Note that ->ki_cancel callers also delete iocb from active_reqs after
3364 * calling ->ki_cancel. We need the ctx_lock roundtrip here to
3365 * synchronize with them. In the cancellation case the list_del_init
3366 * itself is not actually needed, but harmless so we keep it in to
3367 * avoid further branches in the fast path.
3368 */
3369 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003370 if (!mask && ret != -ECANCELED) {
Jens Axboe392edb42019-12-09 17:52:20 -07003371 add_wait_queue(poll->head, &poll->wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003372 spin_unlock_irq(&ctx->completion_lock);
3373 return;
3374 }
Jens Axboe78076bb2019-12-04 19:56:40 -07003375 hash_del(&req->hash_node);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003376 io_poll_complete(req, mask, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003377 spin_unlock_irq(&ctx->completion_lock);
3378
Jens Axboe8c838782019-03-12 15:48:16 -06003379 io_cqring_ev_posted(ctx);
Jens Axboe89723d02019-11-05 15:32:58 -07003380
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003381 if (ret < 0)
3382 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003383 io_put_req_find_next(req, &nxt);
Jens Axboe89723d02019-11-05 15:32:58 -07003384 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07003385 io_wq_assign_next(workptr, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003386}
3387
Jens Axboee94f1412019-12-19 12:06:02 -07003388static void __io_poll_flush(struct io_ring_ctx *ctx, struct llist_node *nodes)
3389{
Jens Axboee94f1412019-12-19 12:06:02 -07003390 struct io_kiocb *req, *tmp;
Jens Axboe8237e042019-12-28 10:48:22 -07003391 struct req_batch rb;
Jens Axboee94f1412019-12-19 12:06:02 -07003392
Jens Axboec6ca97b302019-12-28 12:11:08 -07003393 rb.to_free = rb.need_iter = 0;
Jens Axboee94f1412019-12-19 12:06:02 -07003394 spin_lock_irq(&ctx->completion_lock);
3395 llist_for_each_entry_safe(req, tmp, nodes, llist_node) {
3396 hash_del(&req->hash_node);
3397 io_poll_complete(req, req->result, 0);
3398
Jens Axboe8237e042019-12-28 10:48:22 -07003399 if (refcount_dec_and_test(&req->refs) &&
3400 !io_req_multi_free(&rb, req)) {
3401 req->flags |= REQ_F_COMP_LOCKED;
3402 io_free_req(req);
Jens Axboee94f1412019-12-19 12:06:02 -07003403 }
3404 }
3405 spin_unlock_irq(&ctx->completion_lock);
3406
3407 io_cqring_ev_posted(ctx);
Jens Axboe8237e042019-12-28 10:48:22 -07003408 io_free_req_many(ctx, &rb);
Jens Axboee94f1412019-12-19 12:06:02 -07003409}
3410
3411static void io_poll_flush(struct io_wq_work **workptr)
3412{
3413 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3414 struct llist_node *nodes;
3415
3416 nodes = llist_del_all(&req->ctx->poll_llist);
3417 if (nodes)
3418 __io_poll_flush(req->ctx, nodes);
3419}
3420
Jens Axboe221c5eb2019-01-17 09:41:58 -07003421static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
3422 void *key)
3423{
Jens Axboee9444752019-11-26 15:02:04 -07003424 struct io_poll_iocb *poll = wait->private;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003425 struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
3426 struct io_ring_ctx *ctx = req->ctx;
3427 __poll_t mask = key_to_poll(key);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003428
3429 /* for instances that support it check for an event match first: */
Jens Axboe8c838782019-03-12 15:48:16 -06003430 if (mask && !(mask & poll->events))
3431 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003432
Jens Axboe392edb42019-12-09 17:52:20 -07003433 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06003434
Jens Axboe7c9e7f02019-11-12 08:15:53 -07003435 /*
3436 * Run completion inline if we can. We're using trylock here because
3437 * we are violating the completion_lock -> poll wq lock ordering.
3438 * If we have a link timeout we're going to need the completion_lock
3439 * for finalizing the request, mark us as having grabbed that already.
3440 */
Jens Axboee94f1412019-12-19 12:06:02 -07003441 if (mask) {
3442 unsigned long flags;
Jens Axboe8c838782019-03-12 15:48:16 -06003443
Jens Axboee94f1412019-12-19 12:06:02 -07003444 if (llist_empty(&ctx->poll_llist) &&
3445 spin_trylock_irqsave(&ctx->completion_lock, flags)) {
3446 hash_del(&req->hash_node);
3447 io_poll_complete(req, mask, 0);
3448 req->flags |= REQ_F_COMP_LOCKED;
3449 io_put_req(req);
3450 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3451
3452 io_cqring_ev_posted(ctx);
3453 req = NULL;
3454 } else {
3455 req->result = mask;
3456 req->llist_node.next = NULL;
3457 /* if the list wasn't empty, we're done */
3458 if (!llist_add(&req->llist_node, &ctx->poll_llist))
3459 req = NULL;
3460 else
3461 req->work.func = io_poll_flush;
3462 }
Jens Axboe8c838782019-03-12 15:48:16 -06003463 }
Jens Axboee94f1412019-12-19 12:06:02 -07003464 if (req)
3465 io_queue_async_work(req);
Jens Axboe8c838782019-03-12 15:48:16 -06003466
Jens Axboe221c5eb2019-01-17 09:41:58 -07003467 return 1;
3468}
3469
3470struct io_poll_table {
3471 struct poll_table_struct pt;
3472 struct io_kiocb *req;
3473 int error;
3474};
3475
3476static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
3477 struct poll_table_struct *p)
3478{
3479 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
3480
3481 if (unlikely(pt->req->poll.head)) {
3482 pt->error = -EINVAL;
3483 return;
3484 }
3485
3486 pt->error = 0;
3487 pt->req->poll.head = head;
Jens Axboe392edb42019-12-09 17:52:20 -07003488 add_wait_queue(head, &pt->req->poll.wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003489}
3490
Jens Axboeeac406c2019-11-14 12:09:58 -07003491static void io_poll_req_insert(struct io_kiocb *req)
3492{
3493 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07003494 struct hlist_head *list;
Jens Axboeeac406c2019-11-14 12:09:58 -07003495
Jens Axboe78076bb2019-12-04 19:56:40 -07003496 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
3497 hlist_add_head(&req->hash_node, list);
Jens Axboeeac406c2019-11-14 12:09:58 -07003498}
3499
Jens Axboe3529d8c2019-12-19 18:24:38 -07003500static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003501{
3502 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003503 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003504
3505 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3506 return -EINVAL;
3507 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
3508 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06003509 if (!poll->file)
3510 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003511
Jens Axboe221c5eb2019-01-17 09:41:58 -07003512 events = READ_ONCE(sqe->poll_events);
3513 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
Jens Axboe0969e782019-12-17 18:40:57 -07003514 return 0;
3515}
3516
3517static int io_poll_add(struct io_kiocb *req, struct io_kiocb **nxt)
3518{
3519 struct io_poll_iocb *poll = &req->poll;
3520 struct io_ring_ctx *ctx = req->ctx;
3521 struct io_poll_table ipt;
3522 bool cancel = false;
3523 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07003524
3525 INIT_IO_WORK(&req->work, io_poll_complete_work);
Jens Axboe78076bb2019-12-04 19:56:40 -07003526 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003527
Jens Axboe221c5eb2019-01-17 09:41:58 -07003528 poll->head = NULL;
Jens Axboe8c838782019-03-12 15:48:16 -06003529 poll->done = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003530 poll->canceled = false;
3531
3532 ipt.pt._qproc = io_poll_queue_proc;
3533 ipt.pt._key = poll->events;
3534 ipt.req = req;
3535 ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
3536
3537 /* initialized the list so that we can do list_empty checks */
Jens Axboe392edb42019-12-09 17:52:20 -07003538 INIT_LIST_HEAD(&poll->wait.entry);
3539 init_waitqueue_func_entry(&poll->wait, io_poll_wake);
3540 poll->wait.private = poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003541
Jens Axboe36703242019-07-25 10:20:18 -06003542 INIT_LIST_HEAD(&req->list);
3543
Jens Axboe221c5eb2019-01-17 09:41:58 -07003544 mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003545
3546 spin_lock_irq(&ctx->completion_lock);
Jens Axboe8c838782019-03-12 15:48:16 -06003547 if (likely(poll->head)) {
3548 spin_lock(&poll->head->lock);
Jens Axboe392edb42019-12-09 17:52:20 -07003549 if (unlikely(list_empty(&poll->wait.entry))) {
Jens Axboe8c838782019-03-12 15:48:16 -06003550 if (ipt.error)
3551 cancel = true;
3552 ipt.error = 0;
3553 mask = 0;
3554 }
3555 if (mask || ipt.error)
Jens Axboe392edb42019-12-09 17:52:20 -07003556 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06003557 else if (cancel)
3558 WRITE_ONCE(poll->canceled, true);
3559 else if (!poll->done) /* actually waiting for an event */
Jens Axboeeac406c2019-11-14 12:09:58 -07003560 io_poll_req_insert(req);
Jens Axboe8c838782019-03-12 15:48:16 -06003561 spin_unlock(&poll->head->lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003562 }
Jens Axboe8c838782019-03-12 15:48:16 -06003563 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06003564 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003565 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06003566 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07003567 spin_unlock_irq(&ctx->completion_lock);
3568
Jens Axboe8c838782019-03-12 15:48:16 -06003569 if (mask) {
3570 io_cqring_ev_posted(ctx);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003571 io_put_req_find_next(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003572 }
Jens Axboe8c838782019-03-12 15:48:16 -06003573 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003574}
3575
Jens Axboe5262f562019-09-17 12:26:57 -06003576static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
3577{
Jens Axboead8a48a2019-11-15 08:49:11 -07003578 struct io_timeout_data *data = container_of(timer,
3579 struct io_timeout_data, timer);
3580 struct io_kiocb *req = data->req;
3581 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06003582 unsigned long flags;
3583
Jens Axboe5262f562019-09-17 12:26:57 -06003584 atomic_inc(&ctx->cq_timeouts);
3585
3586 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08003587 /*
Jens Axboe11365042019-10-16 09:08:32 -06003588 * We could be racing with timeout deletion. If the list is empty,
3589 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08003590 */
Jens Axboe842f9612019-10-29 12:34:10 -06003591 if (!list_empty(&req->list)) {
Jens Axboe11365042019-10-16 09:08:32 -06003592 struct io_kiocb *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06003593
Jens Axboe11365042019-10-16 09:08:32 -06003594 /*
3595 * Adjust the reqs sequence before the current one because it
Brian Gianforcarod195a662019-12-13 03:09:50 -08003596 * will consume a slot in the cq_ring and the cq_tail
Jens Axboe11365042019-10-16 09:08:32 -06003597 * pointer will be increased, otherwise other timeout reqs may
3598 * return in advance without waiting for enough wait_nr.
3599 */
3600 prev = req;
3601 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
3602 prev->sequence++;
Jens Axboe11365042019-10-16 09:08:32 -06003603 list_del_init(&req->list);
Jens Axboe11365042019-10-16 09:08:32 -06003604 }
Jens Axboe842f9612019-10-29 12:34:10 -06003605
Jens Axboe78e19bb2019-11-06 15:21:34 -07003606 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06003607 io_commit_cqring(ctx);
3608 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3609
3610 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003611 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06003612 io_put_req(req);
3613 return HRTIMER_NORESTART;
3614}
3615
Jens Axboe47f46762019-11-09 17:43:02 -07003616static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
3617{
3618 struct io_kiocb *req;
3619 int ret = -ENOENT;
3620
3621 list_for_each_entry(req, &ctx->timeout_list, list) {
3622 if (user_data == req->user_data) {
3623 list_del_init(&req->list);
3624 ret = 0;
3625 break;
3626 }
3627 }
3628
3629 if (ret == -ENOENT)
3630 return ret;
3631
Jens Axboe2d283902019-12-04 11:08:05 -07003632 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe47f46762019-11-09 17:43:02 -07003633 if (ret == -1)
3634 return -EALREADY;
3635
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003636 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07003637 io_cqring_fill_event(req, -ECANCELED);
3638 io_put_req(req);
3639 return 0;
3640}
3641
Jens Axboe3529d8c2019-12-19 18:24:38 -07003642static int io_timeout_remove_prep(struct io_kiocb *req,
3643 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07003644{
Jens Axboeb29472e2019-12-17 18:50:29 -07003645 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3646 return -EINVAL;
3647 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
3648 return -EINVAL;
3649
3650 req->timeout.addr = READ_ONCE(sqe->addr);
3651 req->timeout.flags = READ_ONCE(sqe->timeout_flags);
3652 if (req->timeout.flags)
3653 return -EINVAL;
3654
Jens Axboeb29472e2019-12-17 18:50:29 -07003655 return 0;
3656}
3657
Jens Axboe11365042019-10-16 09:08:32 -06003658/*
3659 * Remove or update an existing timeout command
3660 */
Jens Axboefc4df992019-12-10 14:38:45 -07003661static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06003662{
3663 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07003664 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06003665
Jens Axboe11365042019-10-16 09:08:32 -06003666 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb29472e2019-12-17 18:50:29 -07003667 ret = io_timeout_cancel(ctx, req->timeout.addr);
Jens Axboe11365042019-10-16 09:08:32 -06003668
Jens Axboe47f46762019-11-09 17:43:02 -07003669 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06003670 io_commit_cqring(ctx);
3671 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06003672 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003673 if (ret < 0)
3674 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003675 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06003676 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06003677}
3678
Jens Axboe3529d8c2019-12-19 18:24:38 -07003679static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07003680 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06003681{
Jens Axboead8a48a2019-11-15 08:49:11 -07003682 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06003683 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06003684
Jens Axboead8a48a2019-11-15 08:49:11 -07003685 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06003686 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07003687 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06003688 return -EINVAL;
Jens Axboe2d283902019-12-04 11:08:05 -07003689 if (sqe->off && is_timeout_link)
3690 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06003691 flags = READ_ONCE(sqe->timeout_flags);
3692 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06003693 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06003694
Jens Axboe26a61672019-12-20 09:02:01 -07003695 req->timeout.count = READ_ONCE(sqe->off);
3696
Jens Axboe3529d8c2019-12-19 18:24:38 -07003697 if (!req->io && io_alloc_async_ctx(req))
Jens Axboe26a61672019-12-20 09:02:01 -07003698 return -ENOMEM;
3699
3700 data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07003701 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07003702 req->flags |= REQ_F_TIMEOUT;
3703
3704 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06003705 return -EFAULT;
3706
Jens Axboe11365042019-10-16 09:08:32 -06003707 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07003708 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06003709 else
Jens Axboead8a48a2019-11-15 08:49:11 -07003710 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06003711
Jens Axboead8a48a2019-11-15 08:49:11 -07003712 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
3713 return 0;
3714}
3715
Jens Axboefc4df992019-12-10 14:38:45 -07003716static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07003717{
3718 unsigned count;
3719 struct io_ring_ctx *ctx = req->ctx;
3720 struct io_timeout_data *data;
3721 struct list_head *entry;
3722 unsigned span = 0;
Jens Axboead8a48a2019-11-15 08:49:11 -07003723
Jens Axboe2d283902019-12-04 11:08:05 -07003724 data = &req->io->timeout;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003725
Jens Axboe5262f562019-09-17 12:26:57 -06003726 /*
3727 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07003728 * timeout event to be satisfied. If it isn't set, then this is
3729 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06003730 */
Jens Axboe26a61672019-12-20 09:02:01 -07003731 count = req->timeout.count;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003732 if (!count) {
3733 req->flags |= REQ_F_TIMEOUT_NOSEQ;
3734 spin_lock_irq(&ctx->completion_lock);
3735 entry = ctx->timeout_list.prev;
3736 goto add;
3737 }
Jens Axboe5262f562019-09-17 12:26:57 -06003738
3739 req->sequence = ctx->cached_sq_head + count - 1;
Jens Axboe2d283902019-12-04 11:08:05 -07003740 data->seq_offset = count;
Jens Axboe5262f562019-09-17 12:26:57 -06003741
3742 /*
3743 * Insertion sort, ensuring the first entry in the list is always
3744 * the one we need first.
3745 */
Jens Axboe5262f562019-09-17 12:26:57 -06003746 spin_lock_irq(&ctx->completion_lock);
3747 list_for_each_prev(entry, &ctx->timeout_list) {
3748 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
yangerkun5da0fb12019-10-15 21:59:29 +08003749 unsigned nxt_sq_head;
3750 long long tmp, tmp_nxt;
Jens Axboe2d283902019-12-04 11:08:05 -07003751 u32 nxt_offset = nxt->io->timeout.seq_offset;
Jens Axboe5262f562019-09-17 12:26:57 -06003752
Jens Axboe93bd25b2019-11-11 23:34:31 -07003753 if (nxt->flags & REQ_F_TIMEOUT_NOSEQ)
3754 continue;
3755
yangerkun5da0fb12019-10-15 21:59:29 +08003756 /*
3757 * Since cached_sq_head + count - 1 can overflow, use type long
3758 * long to store it.
3759 */
3760 tmp = (long long)ctx->cached_sq_head + count - 1;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +03003761 nxt_sq_head = nxt->sequence - nxt_offset + 1;
3762 tmp_nxt = (long long)nxt_sq_head + nxt_offset - 1;
yangerkun5da0fb12019-10-15 21:59:29 +08003763
3764 /*
3765 * cached_sq_head may overflow, and it will never overflow twice
3766 * once there is some timeout req still be valid.
3767 */
3768 if (ctx->cached_sq_head < nxt_sq_head)
yangerkun8b07a652019-10-17 12:12:35 +08003769 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08003770
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003771 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06003772 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003773
3774 /*
3775 * Sequence of reqs after the insert one and itself should
3776 * be adjusted because each timeout req consumes a slot.
3777 */
3778 span++;
3779 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06003780 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003781 req->sequence -= span;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003782add:
Jens Axboe5262f562019-09-17 12:26:57 -06003783 list_add(&req->list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07003784 data->timer.function = io_timeout_fn;
3785 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06003786 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06003787 return 0;
3788}
3789
Jens Axboe62755e32019-10-28 21:49:21 -06003790static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06003791{
Jens Axboe62755e32019-10-28 21:49:21 -06003792 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06003793
Jens Axboe62755e32019-10-28 21:49:21 -06003794 return req->user_data == (unsigned long) data;
3795}
3796
Jens Axboee977d6d2019-11-05 12:39:45 -07003797static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06003798{
Jens Axboe62755e32019-10-28 21:49:21 -06003799 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06003800 int ret = 0;
3801
Jens Axboe62755e32019-10-28 21:49:21 -06003802 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
3803 switch (cancel_ret) {
3804 case IO_WQ_CANCEL_OK:
3805 ret = 0;
3806 break;
3807 case IO_WQ_CANCEL_RUNNING:
3808 ret = -EALREADY;
3809 break;
3810 case IO_WQ_CANCEL_NOTFOUND:
3811 ret = -ENOENT;
3812 break;
3813 }
3814
Jens Axboee977d6d2019-11-05 12:39:45 -07003815 return ret;
3816}
3817
Jens Axboe47f46762019-11-09 17:43:02 -07003818static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
3819 struct io_kiocb *req, __u64 sqe_addr,
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003820 struct io_kiocb **nxt, int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07003821{
3822 unsigned long flags;
3823 int ret;
3824
3825 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
3826 if (ret != -ENOENT) {
3827 spin_lock_irqsave(&ctx->completion_lock, flags);
3828 goto done;
3829 }
3830
3831 spin_lock_irqsave(&ctx->completion_lock, flags);
3832 ret = io_timeout_cancel(ctx, sqe_addr);
3833 if (ret != -ENOENT)
3834 goto done;
3835 ret = io_poll_cancel(ctx, sqe_addr);
3836done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003837 if (!ret)
3838 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07003839 io_cqring_fill_event(req, ret);
3840 io_commit_cqring(ctx);
3841 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3842 io_cqring_ev_posted(ctx);
3843
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003844 if (ret < 0)
3845 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07003846 io_put_req_find_next(req, nxt);
3847}
3848
Jens Axboe3529d8c2019-12-19 18:24:38 -07003849static int io_async_cancel_prep(struct io_kiocb *req,
3850 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07003851{
Jens Axboefbf23842019-12-17 18:45:56 -07003852 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07003853 return -EINVAL;
3854 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
3855 sqe->cancel_flags)
3856 return -EINVAL;
3857
Jens Axboefbf23842019-12-17 18:45:56 -07003858 req->cancel.addr = READ_ONCE(sqe->addr);
3859 return 0;
3860}
3861
3862static int io_async_cancel(struct io_kiocb *req, struct io_kiocb **nxt)
3863{
3864 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07003865
3866 io_async_find_and_cancel(ctx, req, req->cancel.addr, nxt, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06003867 return 0;
3868}
3869
Jens Axboe05f3fb32019-12-09 11:22:50 -07003870static int io_files_update_prep(struct io_kiocb *req,
3871 const struct io_uring_sqe *sqe)
3872{
3873 if (sqe->flags || sqe->ioprio || sqe->rw_flags)
3874 return -EINVAL;
3875
3876 req->files_update.offset = READ_ONCE(sqe->off);
3877 req->files_update.nr_args = READ_ONCE(sqe->len);
3878 if (!req->files_update.nr_args)
3879 return -EINVAL;
3880 req->files_update.arg = READ_ONCE(sqe->addr);
3881 return 0;
3882}
3883
3884static int io_files_update(struct io_kiocb *req, bool force_nonblock)
3885{
3886 struct io_ring_ctx *ctx = req->ctx;
3887 struct io_uring_files_update up;
3888 int ret;
3889
3890 if (force_nonblock) {
3891 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
3892 return -EAGAIN;
3893 }
3894
3895 up.offset = req->files_update.offset;
3896 up.fds = req->files_update.arg;
3897
3898 mutex_lock(&ctx->uring_lock);
3899 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
3900 mutex_unlock(&ctx->uring_lock);
3901
3902 if (ret < 0)
3903 req_set_fail_links(req);
3904 io_cqring_add_event(req, ret);
3905 io_put_req(req);
3906 return 0;
3907}
3908
Jens Axboe3529d8c2019-12-19 18:24:38 -07003909static int io_req_defer_prep(struct io_kiocb *req,
3910 const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003911{
Jens Axboee7815732019-12-17 19:45:06 -07003912 ssize_t ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003913
Jens Axboed625c6e2019-12-17 19:53:05 -07003914 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07003915 case IORING_OP_NOP:
3916 break;
Jens Axboef67676d2019-12-02 11:03:47 -07003917 case IORING_OP_READV:
3918 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07003919 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003920 ret = io_read_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07003921 break;
3922 case IORING_OP_WRITEV:
3923 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07003924 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003925 ret = io_write_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07003926 break;
Jens Axboe0969e782019-12-17 18:40:57 -07003927 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003928 ret = io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07003929 break;
3930 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003931 ret = io_poll_remove_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07003932 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003933 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003934 ret = io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003935 break;
3936 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003937 ret = io_prep_sfr(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003938 break;
Jens Axboe03b12302019-12-02 18:50:25 -07003939 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07003940 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003941 ret = io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07003942 break;
3943 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07003944 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003945 ret = io_recvmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07003946 break;
Jens Axboef499a022019-12-02 16:28:46 -07003947 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003948 ret = io_connect_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07003949 break;
Jens Axboe2d283902019-12-04 11:08:05 -07003950 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003951 ret = io_timeout_prep(req, sqe, false);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003952 break;
Jens Axboeb29472e2019-12-17 18:50:29 -07003953 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003954 ret = io_timeout_remove_prep(req, sqe);
Jens Axboeb29472e2019-12-17 18:50:29 -07003955 break;
Jens Axboefbf23842019-12-17 18:45:56 -07003956 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003957 ret = io_async_cancel_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07003958 break;
Jens Axboe2d283902019-12-04 11:08:05 -07003959 case IORING_OP_LINK_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003960 ret = io_timeout_prep(req, sqe, true);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003961 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003962 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003963 ret = io_accept_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003964 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07003965 case IORING_OP_FALLOCATE:
3966 ret = io_fallocate_prep(req, sqe);
3967 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003968 case IORING_OP_OPENAT:
3969 ret = io_openat_prep(req, sqe);
3970 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07003971 case IORING_OP_CLOSE:
3972 ret = io_close_prep(req, sqe);
3973 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07003974 case IORING_OP_FILES_UPDATE:
3975 ret = io_files_update_prep(req, sqe);
3976 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003977 case IORING_OP_STATX:
3978 ret = io_statx_prep(req, sqe);
3979 break;
Jens Axboe4840e412019-12-25 22:03:45 -07003980 case IORING_OP_FADVISE:
3981 ret = io_fadvise_prep(req, sqe);
3982 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07003983 case IORING_OP_MADVISE:
3984 ret = io_madvise_prep(req, sqe);
3985 break;
Jens Axboef67676d2019-12-02 11:03:47 -07003986 default:
Jens Axboee7815732019-12-17 19:45:06 -07003987 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
3988 req->opcode);
3989 ret = -EINVAL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003990 break;
Jens Axboef67676d2019-12-02 11:03:47 -07003991 }
3992
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003993 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003994}
3995
Jens Axboe3529d8c2019-12-19 18:24:38 -07003996static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboede0617e2019-04-06 21:51:27 -06003997{
Jackie Liua197f662019-11-08 08:09:12 -07003998 struct io_ring_ctx *ctx = req->ctx;
Jens Axboef67676d2019-12-02 11:03:47 -07003999 int ret;
Jens Axboede0617e2019-04-06 21:51:27 -06004000
Bob Liu9d858b22019-11-13 18:06:25 +08004001 /* Still need defer if there is pending req in defer list. */
4002 if (!req_need_defer(req) && list_empty(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06004003 return 0;
4004
Jens Axboe3529d8c2019-12-19 18:24:38 -07004005 if (!req->io && io_alloc_async_ctx(req))
Jens Axboede0617e2019-04-06 21:51:27 -06004006 return -EAGAIN;
4007
Jens Axboe3529d8c2019-12-19 18:24:38 -07004008 ret = io_req_defer_prep(req, sqe);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004009 if (ret < 0)
Jens Axboe2d283902019-12-04 11:08:05 -07004010 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07004011
Jens Axboede0617e2019-04-06 21:51:27 -06004012 spin_lock_irq(&ctx->completion_lock);
Bob Liu9d858b22019-11-13 18:06:25 +08004013 if (!req_need_defer(req) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06004014 spin_unlock_irq(&ctx->completion_lock);
Jens Axboede0617e2019-04-06 21:51:27 -06004015 return 0;
4016 }
4017
Jens Axboe915967f2019-11-21 09:01:20 -07004018 trace_io_uring_defer(ctx, req, req->user_data);
Jens Axboede0617e2019-04-06 21:51:27 -06004019 list_add_tail(&req->list, &ctx->defer_list);
4020 spin_unlock_irq(&ctx->completion_lock);
4021 return -EIOCBQUEUED;
4022}
4023
Jens Axboe3529d8c2019-12-19 18:24:38 -07004024static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
4025 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004026{
Jackie Liua197f662019-11-08 08:09:12 -07004027 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07004028 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004029
Jens Axboed625c6e2019-12-17 19:53:05 -07004030 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07004031 case IORING_OP_NOP:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004032 ret = io_nop(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004033 break;
4034 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004035 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004036 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004037 if (sqe) {
4038 ret = io_read_prep(req, sqe, force_nonblock);
4039 if (ret < 0)
4040 break;
4041 }
Pavel Begunkov267bc902019-11-07 01:41:08 +03004042 ret = io_read(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004043 break;
4044 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07004045 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004046 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004047 if (sqe) {
4048 ret = io_write_prep(req, sqe, force_nonblock);
4049 if (ret < 0)
4050 break;
4051 }
Pavel Begunkov267bc902019-11-07 01:41:08 +03004052 ret = io_write(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004053 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004054 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004055 if (sqe) {
4056 ret = io_prep_fsync(req, sqe);
4057 if (ret < 0)
4058 break;
4059 }
Jens Axboefc4df992019-12-10 14:38:45 -07004060 ret = io_fsync(req, nxt, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004061 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004062 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004063 if (sqe) {
4064 ret = io_poll_add_prep(req, sqe);
4065 if (ret)
4066 break;
4067 }
Jens Axboefc4df992019-12-10 14:38:45 -07004068 ret = io_poll_add(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004069 break;
4070 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004071 if (sqe) {
4072 ret = io_poll_remove_prep(req, sqe);
4073 if (ret < 0)
4074 break;
4075 }
Jens Axboefc4df992019-12-10 14:38:45 -07004076 ret = io_poll_remove(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004077 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004078 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004079 if (sqe) {
4080 ret = io_prep_sfr(req, sqe);
4081 if (ret < 0)
4082 break;
4083 }
Jens Axboefc4df992019-12-10 14:38:45 -07004084 ret = io_sync_file_range(req, nxt, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004085 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004086 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004087 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004088 if (sqe) {
4089 ret = io_sendmsg_prep(req, sqe);
4090 if (ret < 0)
4091 break;
4092 }
Jens Axboefddafac2020-01-04 20:19:44 -07004093 if (req->opcode == IORING_OP_SENDMSG)
4094 ret = io_sendmsg(req, nxt, force_nonblock);
4095 else
4096 ret = io_send(req, nxt, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004097 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06004098 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004099 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004100 if (sqe) {
4101 ret = io_recvmsg_prep(req, sqe);
4102 if (ret)
4103 break;
4104 }
Jens Axboefddafac2020-01-04 20:19:44 -07004105 if (req->opcode == IORING_OP_RECVMSG)
4106 ret = io_recvmsg(req, nxt, force_nonblock);
4107 else
4108 ret = io_recv(req, nxt, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06004109 break;
Jens Axboe5262f562019-09-17 12:26:57 -06004110 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004111 if (sqe) {
4112 ret = io_timeout_prep(req, sqe, false);
4113 if (ret)
4114 break;
4115 }
Jens Axboefc4df992019-12-10 14:38:45 -07004116 ret = io_timeout(req);
Jens Axboe5262f562019-09-17 12:26:57 -06004117 break;
Jens Axboe11365042019-10-16 09:08:32 -06004118 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004119 if (sqe) {
4120 ret = io_timeout_remove_prep(req, sqe);
4121 if (ret)
4122 break;
4123 }
Jens Axboefc4df992019-12-10 14:38:45 -07004124 ret = io_timeout_remove(req);
Jens Axboe11365042019-10-16 09:08:32 -06004125 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06004126 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004127 if (sqe) {
4128 ret = io_accept_prep(req, sqe);
4129 if (ret)
4130 break;
4131 }
Jens Axboefc4df992019-12-10 14:38:45 -07004132 ret = io_accept(req, nxt, force_nonblock);
Jens Axboe17f2fe32019-10-17 14:42:58 -06004133 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004134 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004135 if (sqe) {
4136 ret = io_connect_prep(req, sqe);
4137 if (ret)
4138 break;
4139 }
Jens Axboefc4df992019-12-10 14:38:45 -07004140 ret = io_connect(req, nxt, force_nonblock);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004141 break;
Jens Axboe62755e32019-10-28 21:49:21 -06004142 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004143 if (sqe) {
4144 ret = io_async_cancel_prep(req, sqe);
4145 if (ret)
4146 break;
4147 }
Jens Axboefc4df992019-12-10 14:38:45 -07004148 ret = io_async_cancel(req, nxt);
Jens Axboe62755e32019-10-28 21:49:21 -06004149 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07004150 case IORING_OP_FALLOCATE:
4151 if (sqe) {
4152 ret = io_fallocate_prep(req, sqe);
4153 if (ret)
4154 break;
4155 }
4156 ret = io_fallocate(req, nxt, force_nonblock);
4157 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004158 case IORING_OP_OPENAT:
4159 if (sqe) {
4160 ret = io_openat_prep(req, sqe);
4161 if (ret)
4162 break;
4163 }
4164 ret = io_openat(req, nxt, force_nonblock);
4165 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07004166 case IORING_OP_CLOSE:
4167 if (sqe) {
4168 ret = io_close_prep(req, sqe);
4169 if (ret)
4170 break;
4171 }
4172 ret = io_close(req, nxt, force_nonblock);
4173 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004174 case IORING_OP_FILES_UPDATE:
4175 if (sqe) {
4176 ret = io_files_update_prep(req, sqe);
4177 if (ret)
4178 break;
4179 }
4180 ret = io_files_update(req, force_nonblock);
4181 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004182 case IORING_OP_STATX:
4183 if (sqe) {
4184 ret = io_statx_prep(req, sqe);
4185 if (ret)
4186 break;
4187 }
4188 ret = io_statx(req, nxt, force_nonblock);
4189 break;
Jens Axboe4840e412019-12-25 22:03:45 -07004190 case IORING_OP_FADVISE:
4191 if (sqe) {
4192 ret = io_fadvise_prep(req, sqe);
4193 if (ret)
4194 break;
4195 }
4196 ret = io_fadvise(req, nxt, force_nonblock);
4197 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07004198 case IORING_OP_MADVISE:
4199 if (sqe) {
4200 ret = io_madvise_prep(req, sqe);
4201 if (ret)
4202 break;
4203 }
4204 ret = io_madvise(req, nxt, force_nonblock);
4205 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004206 default:
4207 ret = -EINVAL;
4208 break;
4209 }
4210
Jens Axboedef596e2019-01-09 08:59:42 -07004211 if (ret)
4212 return ret;
4213
4214 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe11ba8202020-01-15 21:51:17 -07004215 const bool in_async = io_wq_current_is_worker();
4216
Jens Axboe9e645e112019-05-10 16:07:28 -06004217 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07004218 return -EAGAIN;
4219
Jens Axboe11ba8202020-01-15 21:51:17 -07004220 /* workqueue context doesn't hold uring_lock, grab it now */
4221 if (in_async)
4222 mutex_lock(&ctx->uring_lock);
4223
Jens Axboedef596e2019-01-09 08:59:42 -07004224 io_iopoll_req_issued(req);
Jens Axboe11ba8202020-01-15 21:51:17 -07004225
4226 if (in_async)
4227 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07004228 }
4229
4230 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004231}
4232
Jens Axboe561fb042019-10-24 07:25:42 -06004233static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboe31b51512019-01-18 22:56:34 -07004234{
Jens Axboe561fb042019-10-24 07:25:42 -06004235 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004236 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe561fb042019-10-24 07:25:42 -06004237 struct io_kiocb *nxt = NULL;
4238 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004239
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07004240 /* if NO_CANCEL is set, we must still run the work */
4241 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
4242 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06004243 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07004244 }
Jens Axboe31b51512019-01-18 22:56:34 -07004245
Jens Axboe561fb042019-10-24 07:25:42 -06004246 if (!ret) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004247 req->has_user = (work->flags & IO_WQ_WORK_HAS_MM) != 0;
4248 req->in_async = true;
Jens Axboe561fb042019-10-24 07:25:42 -06004249 do {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004250 ret = io_issue_sqe(req, NULL, &nxt, false);
Jens Axboe561fb042019-10-24 07:25:42 -06004251 /*
4252 * We can get EAGAIN for polled IO even though we're
4253 * forcing a sync submission from here, since we can't
4254 * wait for request slots on the block side.
4255 */
4256 if (ret != -EAGAIN)
4257 break;
4258 cond_resched();
4259 } while (1);
4260 }
Jens Axboe31b51512019-01-18 22:56:34 -07004261
Jens Axboe561fb042019-10-24 07:25:42 -06004262 /* drop submission reference */
Jackie Liuec9c02a2019-11-08 23:50:36 +08004263 io_put_req(req);
Jens Axboe817869d2019-04-30 14:44:05 -06004264
Jens Axboe561fb042019-10-24 07:25:42 -06004265 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004266 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07004267 io_cqring_add_event(req, ret);
Jens Axboe817869d2019-04-30 14:44:05 -06004268 io_put_req(req);
Jens Axboeedafcce2019-01-09 09:16:05 -07004269 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004270
Jens Axboe561fb042019-10-24 07:25:42 -06004271 /* if a dependent link is ready, pass it back */
Jens Axboe78912932020-01-14 22:09:06 -07004272 if (!ret && nxt)
4273 io_wq_assign_next(workptr, nxt);
Jens Axboe31b51512019-01-18 22:56:34 -07004274}
Jens Axboe2b188cc2019-01-07 10:46:33 -07004275
Jens Axboe15b71ab2019-12-11 11:20:36 -07004276static int io_req_needs_file(struct io_kiocb *req, int fd)
Jens Axboe09bb8392019-03-13 12:39:28 -06004277{
Jens Axboed3656342019-12-18 09:50:26 -07004278 if (!io_op_defs[req->opcode].needs_file)
Jens Axboe9e3aa612019-12-11 15:55:43 -07004279 return 0;
Jens Axboed3656342019-12-18 09:50:26 -07004280 if (fd == -1 && io_op_defs[req->opcode].fd_non_neg)
4281 return 0;
4282 return 1;
Jens Axboe09bb8392019-03-13 12:39:28 -06004283}
4284
Jens Axboe65e19f52019-10-26 07:20:21 -06004285static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
4286 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06004287{
Jens Axboe65e19f52019-10-26 07:20:21 -06004288 struct fixed_file_table *table;
4289
Jens Axboe05f3fb32019-12-09 11:22:50 -07004290 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
4291 return table->files[index & IORING_FILE_TABLE_MASK];;
Jens Axboe65e19f52019-10-26 07:20:21 -06004292}
4293
Jens Axboe3529d8c2019-12-19 18:24:38 -07004294static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
4295 const struct io_uring_sqe *sqe)
Jens Axboe09bb8392019-03-13 12:39:28 -06004296{
Jackie Liua197f662019-11-08 08:09:12 -07004297 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe09bb8392019-03-13 12:39:28 -06004298 unsigned flags;
Jens Axboed3656342019-12-18 09:50:26 -07004299 int fd;
Jens Axboe09bb8392019-03-13 12:39:28 -06004300
Jens Axboe3529d8c2019-12-19 18:24:38 -07004301 flags = READ_ONCE(sqe->flags);
4302 fd = READ_ONCE(sqe->fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06004303
Jackie Liu4fe2c962019-09-09 20:50:40 +08004304 if (flags & IOSQE_IO_DRAIN)
Jens Axboede0617e2019-04-06 21:51:27 -06004305 req->flags |= REQ_F_IO_DRAIN;
Jens Axboede0617e2019-04-06 21:51:27 -06004306
Jens Axboed3656342019-12-18 09:50:26 -07004307 if (!io_req_needs_file(req, fd))
4308 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06004309
4310 if (flags & IOSQE_FIXED_FILE) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07004311 if (unlikely(!ctx->file_data ||
Jens Axboe09bb8392019-03-13 12:39:28 -06004312 (unsigned) fd >= ctx->nr_user_files))
4313 return -EBADF;
Jens Axboeb7620122019-10-26 07:22:55 -06004314 fd = array_index_nospec(fd, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06004315 req->file = io_file_from_index(ctx, fd);
4316 if (!req->file)
Jens Axboe08a45172019-10-03 08:11:03 -06004317 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06004318 req->flags |= REQ_F_FIXED_FILE;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004319 percpu_ref_get(&ctx->file_data->refs);
Jens Axboe09bb8392019-03-13 12:39:28 -06004320 } else {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004321 if (req->needs_fixed_file)
Jens Axboe09bb8392019-03-13 12:39:28 -06004322 return -EBADF;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004323 trace_io_uring_file_get(ctx, fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06004324 req->file = io_file_get(state, fd);
4325 if (unlikely(!req->file))
4326 return -EBADF;
4327 }
4328
4329 return 0;
4330}
4331
Jackie Liua197f662019-11-08 08:09:12 -07004332static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004333{
Jens Axboefcb323c2019-10-24 12:39:47 -06004334 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07004335 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06004336
Jens Axboeb5dba592019-12-11 14:02:38 -07004337 if (!req->ring_file)
4338 return -EBADF;
4339
Jens Axboefcb323c2019-10-24 12:39:47 -06004340 rcu_read_lock();
4341 spin_lock_irq(&ctx->inflight_lock);
4342 /*
4343 * We use the f_ops->flush() handler to ensure that we can flush
4344 * out work accessing these files if the fd is closed. Check if
4345 * the fd has changed since we started down this path, and disallow
4346 * this operation if it has.
4347 */
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004348 if (fcheck(req->ring_fd) == req->ring_file) {
Jens Axboefcb323c2019-10-24 12:39:47 -06004349 list_add(&req->inflight_entry, &ctx->inflight_list);
4350 req->flags |= REQ_F_INFLIGHT;
4351 req->work.files = current->files;
4352 ret = 0;
4353 }
4354 spin_unlock_irq(&ctx->inflight_lock);
4355 rcu_read_unlock();
4356
4357 return ret;
4358}
4359
Jens Axboe2665abf2019-11-05 12:40:47 -07004360static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
4361{
Jens Axboead8a48a2019-11-15 08:49:11 -07004362 struct io_timeout_data *data = container_of(timer,
4363 struct io_timeout_data, timer);
4364 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07004365 struct io_ring_ctx *ctx = req->ctx;
4366 struct io_kiocb *prev = NULL;
4367 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07004368
4369 spin_lock_irqsave(&ctx->completion_lock, flags);
4370
4371 /*
4372 * We don't expect the list to be empty, that will only happen if we
4373 * race with the completion of the linked work.
4374 */
Pavel Begunkov44932332019-12-05 16:16:35 +03004375 if (!list_empty(&req->link_list)) {
4376 prev = list_entry(req->link_list.prev, struct io_kiocb,
4377 link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07004378 if (refcount_inc_not_zero(&prev->refs)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03004379 list_del_init(&req->link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07004380 prev->flags &= ~REQ_F_LINK_TIMEOUT;
4381 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07004382 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07004383 }
4384
4385 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4386
4387 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004388 req_set_fail_links(prev);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004389 io_async_find_and_cancel(ctx, req, prev->user_data, NULL,
4390 -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07004391 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07004392 } else {
4393 io_cqring_add_event(req, -ETIME);
4394 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07004395 }
Jens Axboe2665abf2019-11-05 12:40:47 -07004396 return HRTIMER_NORESTART;
4397}
4398
Jens Axboead8a48a2019-11-15 08:49:11 -07004399static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07004400{
Jens Axboe76a46e02019-11-10 23:34:16 -07004401 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07004402
Jens Axboe76a46e02019-11-10 23:34:16 -07004403 /*
4404 * If the list is now empty, then our linked request finished before
4405 * we got a chance to setup the timer
4406 */
4407 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov44932332019-12-05 16:16:35 +03004408 if (!list_empty(&req->link_list)) {
Jens Axboe2d283902019-12-04 11:08:05 -07004409 struct io_timeout_data *data = &req->io->timeout;
Jens Axboe94ae5e72019-11-14 19:39:52 -07004410
Jens Axboead8a48a2019-11-15 08:49:11 -07004411 data->timer.function = io_link_timeout_fn;
4412 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
4413 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07004414 }
Jens Axboe76a46e02019-11-10 23:34:16 -07004415 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07004416
Jens Axboe2665abf2019-11-05 12:40:47 -07004417 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07004418 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07004419}
4420
Jens Axboead8a48a2019-11-15 08:49:11 -07004421static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07004422{
4423 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004424
Jens Axboe2665abf2019-11-05 12:40:47 -07004425 if (!(req->flags & REQ_F_LINK))
4426 return NULL;
4427
Pavel Begunkov44932332019-12-05 16:16:35 +03004428 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
4429 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07004430 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07004431 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07004432
Jens Axboe76a46e02019-11-10 23:34:16 -07004433 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07004434 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07004435}
4436
Jens Axboe3529d8c2019-12-19 18:24:38 -07004437static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004438{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004439 struct io_kiocb *linked_timeout;
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004440 struct io_kiocb *nxt = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004441 int ret;
4442
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004443again:
4444 linked_timeout = io_prep_linked_timeout(req);
4445
Jens Axboe3529d8c2019-12-19 18:24:38 -07004446 ret = io_issue_sqe(req, sqe, &nxt, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06004447
4448 /*
4449 * We async punt it if the file wasn't marked NOWAIT, or if the file
4450 * doesn't support non-blocking read/write attempts
4451 */
4452 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
4453 (req->flags & REQ_F_MUST_PUNT))) {
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03004454 if (req->work.flags & IO_WQ_WORK_NEEDS_FILES) {
4455 ret = io_grab_files(req);
4456 if (ret)
4457 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004458 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03004459
4460 /*
4461 * Queued up for async execution, worker will release
4462 * submit reference when the iocb is actually submitted.
4463 */
4464 io_queue_async_work(req);
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004465 goto done_req;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004466 }
Jens Axboee65ef562019-03-12 10:16:44 -06004467
Jens Axboefcb323c2019-10-24 12:39:47 -06004468err:
Jens Axboee65ef562019-03-12 10:16:44 -06004469 /* drop submission reference */
4470 io_put_req(req);
4471
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004472 if (linked_timeout) {
Jens Axboe76a46e02019-11-10 23:34:16 -07004473 if (!ret)
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004474 io_queue_linked_timeout(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07004475 else
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004476 io_put_req(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07004477 }
4478
Jens Axboee65ef562019-03-12 10:16:44 -06004479 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06004480 if (ret) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07004481 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004482 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06004483 io_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06004484 }
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004485done_req:
4486 if (nxt) {
4487 req = nxt;
4488 nxt = NULL;
4489 goto again;
4490 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004491}
4492
Jens Axboe3529d8c2019-12-19 18:24:38 -07004493static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jackie Liu4fe2c962019-09-09 20:50:40 +08004494{
4495 int ret;
4496
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004497 if (unlikely(req->ctx->drain_next)) {
4498 req->flags |= REQ_F_IO_DRAIN;
Jens Axboe69b3e542020-01-08 11:01:46 -07004499 req->ctx->drain_next = 0;
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004500 }
Jens Axboe69b3e542020-01-08 11:01:46 -07004501 req->ctx->drain_next = (req->flags & REQ_F_DRAIN_LINK) != 0;
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004502
Jens Axboe3529d8c2019-12-19 18:24:38 -07004503 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004504 if (ret) {
4505 if (ret != -EIOCBQUEUED) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07004506 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004507 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07004508 io_double_put_req(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004509 }
Pavel Begunkov25508782019-12-30 21:24:47 +03004510 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Jens Axboece35a472019-12-17 08:04:44 -07004511 /*
4512 * Never try inline submit of IOSQE_ASYNC is set, go straight
4513 * to async execution.
4514 */
4515 req->work.flags |= IO_WQ_WORK_CONCURRENT;
4516 io_queue_async_work(req);
4517 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004518 __io_queue_sqe(req, sqe);
Jens Axboece35a472019-12-17 08:04:44 -07004519 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08004520}
4521
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004522static inline void io_queue_link_head(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08004523{
Jens Axboe94ae5e72019-11-14 19:39:52 -07004524 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004525 io_cqring_add_event(req, -ECANCELED);
4526 io_double_put_req(req);
4527 } else
Jens Axboe3529d8c2019-12-19 18:24:38 -07004528 io_queue_sqe(req, NULL);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004529}
4530
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004531#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
Jens Axboece35a472019-12-17 08:04:44 -07004532 IOSQE_IO_HARDLINK | IOSQE_ASYNC)
Jens Axboe9e645e112019-05-10 16:07:28 -06004533
Jens Axboe3529d8c2019-12-19 18:24:38 -07004534static bool io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
4535 struct io_submit_state *state, struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06004536{
Jackie Liua197f662019-11-08 08:09:12 -07004537 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004538 unsigned int sqe_flags;
Jens Axboe9e645e112019-05-10 16:07:28 -06004539 int ret;
4540
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004541 sqe_flags = READ_ONCE(sqe->flags);
4542
Jens Axboe9e645e112019-05-10 16:07:28 -06004543 /* enforce forwards compatibility on users */
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004544 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06004545 ret = -EINVAL;
Pavel Begunkov196be952019-11-07 01:41:06 +03004546 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06004547 }
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004548 if (sqe_flags & IOSQE_ASYNC)
Jens Axboece35a472019-12-17 08:04:44 -07004549 req->flags |= REQ_F_FORCE_ASYNC;
Jens Axboe9e645e112019-05-10 16:07:28 -06004550
Jens Axboe3529d8c2019-12-19 18:24:38 -07004551 ret = io_req_set_file(state, req, sqe);
Jens Axboe9e645e112019-05-10 16:07:28 -06004552 if (unlikely(ret)) {
4553err_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004554 io_cqring_add_event(req, ret);
4555 io_double_put_req(req);
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004556 return false;
Jens Axboe9e645e112019-05-10 16:07:28 -06004557 }
4558
Jens Axboe9e645e112019-05-10 16:07:28 -06004559 /*
4560 * If we already have a head request, queue this one for async
4561 * submittal once the head completes. If we don't have a head but
4562 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
4563 * submitted sync once the chain is complete. If none of those
4564 * conditions are true (normal request), then just queue it.
4565 */
4566 if (*link) {
Pavel Begunkov9d763772019-12-17 02:22:07 +03004567 struct io_kiocb *head = *link;
Jens Axboe9e645e112019-05-10 16:07:28 -06004568
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004569 if (sqe_flags & IOSQE_IO_DRAIN)
Pavel Begunkov9d763772019-12-17 02:22:07 +03004570 head->flags |= REQ_F_DRAIN_LINK | REQ_F_IO_DRAIN;
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004571
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004572 if (sqe_flags & IOSQE_IO_HARDLINK)
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004573 req->flags |= REQ_F_HARDLINK;
4574
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004575 if (io_alloc_async_ctx(req)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06004576 ret = -EAGAIN;
4577 goto err_req;
4578 }
4579
Jens Axboe3529d8c2019-12-19 18:24:38 -07004580 ret = io_req_defer_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07004581 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004582 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03004583 head->flags |= REQ_F_FAIL_LINK;
Jens Axboef67676d2019-12-02 11:03:47 -07004584 goto err_req;
Jens Axboe2d283902019-12-04 11:08:05 -07004585 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03004586 trace_io_uring_link(ctx, req, head);
4587 list_add_tail(&req->link_list, &head->link_list);
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004588
4589 /* last request of a link, enqueue the link */
4590 if (!(sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK))) {
4591 io_queue_link_head(head);
4592 *link = NULL;
4593 }
4594 } else if (sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06004595 req->flags |= REQ_F_LINK;
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004596 if (sqe_flags & IOSQE_IO_HARDLINK)
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004597 req->flags |= REQ_F_HARDLINK;
Jens Axboe9e645e112019-05-10 16:07:28 -06004598
Jens Axboe9e645e112019-05-10 16:07:28 -06004599 INIT_LIST_HEAD(&req->link_list);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004600 ret = io_req_defer_prep(req, sqe);
4601 if (ret)
4602 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe9e645e112019-05-10 16:07:28 -06004603 *link = req;
4604 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004605 io_queue_sqe(req, sqe);
Jens Axboe9e645e112019-05-10 16:07:28 -06004606 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004607
4608 return true;
Jens Axboe9e645e112019-05-10 16:07:28 -06004609}
4610
Jens Axboe9a56a232019-01-09 09:06:50 -07004611/*
4612 * Batched submission is done, ensure local IO is flushed out.
4613 */
4614static void io_submit_state_end(struct io_submit_state *state)
4615{
4616 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06004617 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07004618 if (state->free_reqs)
4619 kmem_cache_free_bulk(req_cachep, state->free_reqs,
4620 &state->reqs[state->cur_req]);
Jens Axboe9a56a232019-01-09 09:06:50 -07004621}
4622
4623/*
4624 * Start submission side cache.
4625 */
4626static void io_submit_state_start(struct io_submit_state *state,
Jackie Liu22efde52019-12-02 17:14:52 +08004627 unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07004628{
4629 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07004630 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07004631 state->file = NULL;
4632 state->ios_left = max_ios;
4633}
4634
Jens Axboe2b188cc2019-01-07 10:46:33 -07004635static void io_commit_sqring(struct io_ring_ctx *ctx)
4636{
Hristo Venev75b28af2019-08-26 17:23:46 +00004637 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004638
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03004639 /*
4640 * Ensure any loads from the SQEs are done at this point,
4641 * since once we write the new head, the application could
4642 * write new data to them.
4643 */
4644 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004645}
4646
4647/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07004648 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07004649 * that is mapped by userspace. This means that care needs to be taken to
4650 * ensure that reads are stable, as we cannot rely on userspace always
4651 * being a good citizen. If members of the sqe are validated and then later
4652 * used, it's important that those reads are done through READ_ONCE() to
4653 * prevent a re-load down the line.
4654 */
Jens Axboe3529d8c2019-12-19 18:24:38 -07004655static bool io_get_sqring(struct io_ring_ctx *ctx, struct io_kiocb *req,
4656 const struct io_uring_sqe **sqe_ptr)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004657{
Hristo Venev75b28af2019-08-26 17:23:46 +00004658 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004659 unsigned head;
4660
4661 /*
4662 * The cached sq head (or cq tail) serves two purposes:
4663 *
4664 * 1) allows us to batch the cost of updating the user visible
4665 * head updates.
4666 * 2) allows the kernel side to track the head on its own, even
4667 * though the application is the one updating it.
4668 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03004669 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03004670 if (likely(head < ctx->sq_entries)) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004671 /*
4672 * All io need record the previous position, if LINK vs DARIN,
4673 * it can be used to mark the position of the first IO in the
4674 * link list.
4675 */
4676 req->sequence = ctx->cached_sq_head;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004677 *sqe_ptr = &ctx->sq_sqes[head];
4678 req->opcode = READ_ONCE((*sqe_ptr)->opcode);
4679 req->user_data = READ_ONCE((*sqe_ptr)->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004680 ctx->cached_sq_head++;
4681 return true;
4682 }
4683
4684 /* drop invalid entries */
4685 ctx->cached_sq_head++;
Jens Axboe498ccd92019-10-25 10:04:25 -06004686 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03004687 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004688 return false;
4689}
4690
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004691static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004692 struct file *ring_file, int ring_fd,
4693 struct mm_struct **mm, bool async)
Jens Axboe6c271ce2019-01-10 11:22:30 -07004694{
4695 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06004696 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06004697 int i, submitted = 0;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03004698 bool mm_fault = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004699
Jens Axboec4a2ed72019-11-21 21:01:26 -07004700 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07004701 if (test_bit(0, &ctx->sq_check_overflow)) {
4702 if (!list_empty(&ctx->cq_overflow_list) &&
4703 !io_cqring_overflow_flush(ctx, false))
4704 return -EBUSY;
4705 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07004706
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03004707 /* make sure SQ entry isn't read before tail */
4708 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03004709
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03004710 if (!percpu_ref_tryget_many(&ctx->refs, nr))
4711 return -EAGAIN;
4712
Jens Axboe6c271ce2019-01-10 11:22:30 -07004713 if (nr > IO_PLUG_THRESHOLD) {
Jackie Liu22efde52019-12-02 17:14:52 +08004714 io_submit_state_start(&state, nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004715 statep = &state;
4716 }
4717
4718 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004719 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03004720 struct io_kiocb *req;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004721
Pavel Begunkov196be952019-11-07 01:41:06 +03004722 req = io_get_req(ctx, statep);
4723 if (unlikely(!req)) {
4724 if (!submitted)
4725 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004726 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06004727 }
Jens Axboe3529d8c2019-12-19 18:24:38 -07004728 if (!io_get_sqring(ctx, req, &sqe)) {
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03004729 __io_req_do_free(req);
Pavel Begunkov196be952019-11-07 01:41:06 +03004730 break;
4731 }
Jens Axboe9e645e112019-05-10 16:07:28 -06004732
Jens Axboed3656342019-12-18 09:50:26 -07004733 /* will complete beyond this point, count as submitted */
4734 submitted++;
4735
4736 if (unlikely(req->opcode >= IORING_OP_LAST)) {
4737 io_cqring_add_event(req, -EINVAL);
4738 io_double_put_req(req);
4739 break;
4740 }
4741
4742 if (io_op_defs[req->opcode].needs_mm && !*mm) {
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03004743 mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm);
4744 if (!mm_fault) {
4745 use_mm(ctx->sqo_mm);
4746 *mm = ctx->sqo_mm;
4747 }
4748 }
4749
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004750 req->ring_file = ring_file;
4751 req->ring_fd = ring_fd;
4752 req->has_user = *mm != NULL;
4753 req->in_async = async;
4754 req->needs_fixed_file = async;
Jens Axboed625c6e2019-12-17 19:53:05 -07004755 trace_io_uring_submit_sqe(ctx, req->user_data, true, async);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004756 if (!io_submit_sqe(req, sqe, statep, &link))
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004757 break;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004758 }
4759
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03004760 if (submitted != nr)
4761 percpu_ref_put_many(&ctx->refs, nr - submitted);
Jens Axboe9e645e112019-05-10 16:07:28 -06004762 if (link)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004763 io_queue_link_head(link);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004764 if (statep)
4765 io_submit_state_end(&state);
4766
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004767 /* Commit SQ ring head once we've consumed and submitted all SQEs */
4768 io_commit_sqring(ctx);
4769
Jens Axboe6c271ce2019-01-10 11:22:30 -07004770 return submitted;
4771}
4772
4773static int io_sq_thread(void *data)
4774{
Jens Axboe6c271ce2019-01-10 11:22:30 -07004775 struct io_ring_ctx *ctx = data;
4776 struct mm_struct *cur_mm = NULL;
Jens Axboe181e4482019-11-25 08:52:30 -07004777 const struct cred *old_cred;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004778 mm_segment_t old_fs;
4779 DEFINE_WAIT(wait);
4780 unsigned inflight;
4781 unsigned long timeout;
Jens Axboec1edbf52019-11-10 16:56:04 -07004782 int ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004783
Jens Axboe206aefd2019-11-07 18:27:42 -07004784 complete(&ctx->completions[1]);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08004785
Jens Axboe6c271ce2019-01-10 11:22:30 -07004786 old_fs = get_fs();
4787 set_fs(USER_DS);
Jens Axboe181e4482019-11-25 08:52:30 -07004788 old_cred = override_creds(ctx->creds);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004789
Jens Axboec1edbf52019-11-10 16:56:04 -07004790 ret = timeout = inflight = 0;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02004791 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004792 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004793
4794 if (inflight) {
4795 unsigned nr_events = 0;
4796
4797 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe2b2ed972019-10-25 10:06:15 -06004798 /*
4799 * inflight is the count of the maximum possible
4800 * entries we submitted, but it can be smaller
4801 * if we dropped some of them. If we don't have
4802 * poll entries available, then we know that we
4803 * have nothing left to poll for. Reset the
4804 * inflight count to zero in that case.
4805 */
4806 mutex_lock(&ctx->uring_lock);
4807 if (!list_empty(&ctx->poll_list))
4808 __io_iopoll_check(ctx, &nr_events, 0);
4809 else
4810 inflight = 0;
4811 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004812 } else {
4813 /*
4814 * Normal IO, just pretend everything completed.
4815 * We don't have to poll completions for that.
4816 */
4817 nr_events = inflight;
4818 }
4819
4820 inflight -= nr_events;
4821 if (!inflight)
4822 timeout = jiffies + ctx->sq_thread_idle;
4823 }
4824
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004825 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07004826
4827 /*
4828 * If submit got -EBUSY, flag us as needing the application
4829 * to enter the kernel to reap and flush events.
4830 */
4831 if (!to_submit || ret == -EBUSY) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07004832 /*
4833 * We're polling. If we're within the defined idle
4834 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07004835 * to sleep. The exception is if we got EBUSY doing
4836 * more IO, we should wait for the application to
4837 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07004838 */
Jens Axboec1edbf52019-11-10 16:56:04 -07004839 if (inflight ||
4840 (!time_after(jiffies, timeout) && ret != -EBUSY)) {
Jens Axboe9831a902019-09-19 09:48:55 -06004841 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07004842 continue;
4843 }
4844
4845 /*
4846 * Drop cur_mm before scheduling, we can't hold it for
4847 * long periods (or over schedule()). Do this before
4848 * adding ourselves to the waitqueue, as the unuse/drop
4849 * may sleep.
4850 */
4851 if (cur_mm) {
4852 unuse_mm(cur_mm);
4853 mmput(cur_mm);
4854 cur_mm = NULL;
4855 }
4856
4857 prepare_to_wait(&ctx->sqo_wait, &wait,
4858 TASK_INTERRUPTIBLE);
4859
4860 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00004861 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02004862 /* make sure to read SQ tail after writing flags */
4863 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07004864
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004865 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07004866 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02004867 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07004868 finish_wait(&ctx->sqo_wait, &wait);
4869 break;
4870 }
4871 if (signal_pending(current))
4872 flush_signals(current);
4873 schedule();
4874 finish_wait(&ctx->sqo_wait, &wait);
4875
Hristo Venev75b28af2019-08-26 17:23:46 +00004876 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004877 continue;
4878 }
4879 finish_wait(&ctx->sqo_wait, &wait);
4880
Hristo Venev75b28af2019-08-26 17:23:46 +00004881 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004882 }
4883
Jens Axboe8a4955f2019-12-09 14:52:35 -07004884 mutex_lock(&ctx->uring_lock);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004885 ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true);
Jens Axboe8a4955f2019-12-09 14:52:35 -07004886 mutex_unlock(&ctx->uring_lock);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004887 if (ret > 0)
4888 inflight += ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004889 }
4890
4891 set_fs(old_fs);
4892 if (cur_mm) {
4893 unuse_mm(cur_mm);
4894 mmput(cur_mm);
4895 }
Jens Axboe181e4482019-11-25 08:52:30 -07004896 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06004897
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02004898 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06004899
Jens Axboe6c271ce2019-01-10 11:22:30 -07004900 return 0;
4901}
4902
Jens Axboebda52162019-09-24 13:47:15 -06004903struct io_wait_queue {
4904 struct wait_queue_entry wq;
4905 struct io_ring_ctx *ctx;
4906 unsigned to_wait;
4907 unsigned nr_timeouts;
4908};
4909
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004910static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06004911{
4912 struct io_ring_ctx *ctx = iowq->ctx;
4913
4914 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08004915 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06004916 * started waiting. For timeouts, we always want to return to userspace,
4917 * regardless of event count.
4918 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004919 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06004920 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
4921}
4922
4923static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
4924 int wake_flags, void *key)
4925{
4926 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
4927 wq);
4928
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004929 /* use noflush == true, as we can't safely rely on locking context */
4930 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06004931 return -1;
4932
4933 return autoremove_wake_function(curr, mode, wake_flags, key);
4934}
4935
Jens Axboe2b188cc2019-01-07 10:46:33 -07004936/*
4937 * Wait until events become available, if we don't already have some. The
4938 * application must reap them itself, as they reside on the shared cq ring.
4939 */
4940static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
4941 const sigset_t __user *sig, size_t sigsz)
4942{
Jens Axboebda52162019-09-24 13:47:15 -06004943 struct io_wait_queue iowq = {
4944 .wq = {
4945 .private = current,
4946 .func = io_wake_function,
4947 .entry = LIST_HEAD_INIT(iowq.wq.entry),
4948 },
4949 .ctx = ctx,
4950 .to_wait = min_events,
4951 };
Hristo Venev75b28af2019-08-26 17:23:46 +00004952 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08004953 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004954
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004955 if (io_cqring_events(ctx, false) >= min_events)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004956 return 0;
4957
4958 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01004959#ifdef CONFIG_COMPAT
4960 if (in_compat_syscall())
4961 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07004962 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01004963 else
4964#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07004965 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01004966
Jens Axboe2b188cc2019-01-07 10:46:33 -07004967 if (ret)
4968 return ret;
4969 }
4970
Jens Axboebda52162019-09-24 13:47:15 -06004971 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004972 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06004973 do {
4974 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
4975 TASK_INTERRUPTIBLE);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004976 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06004977 break;
4978 schedule();
4979 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08004980 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06004981 break;
4982 }
4983 } while (1);
4984 finish_wait(&ctx->wait, &iowq.wq);
4985
Jackie Liue9ffa5c2019-10-29 11:16:42 +08004986 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004987
Hristo Venev75b28af2019-08-26 17:23:46 +00004988 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004989}
4990
Jens Axboe6b063142019-01-10 22:13:58 -07004991static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
4992{
4993#if defined(CONFIG_UNIX)
4994 if (ctx->ring_sock) {
4995 struct sock *sock = ctx->ring_sock->sk;
4996 struct sk_buff *skb;
4997
4998 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
4999 kfree_skb(skb);
5000 }
5001#else
5002 int i;
5003
Jens Axboe65e19f52019-10-26 07:20:21 -06005004 for (i = 0; i < ctx->nr_user_files; i++) {
5005 struct file *file;
5006
5007 file = io_file_from_index(ctx, i);
5008 if (file)
5009 fput(file);
5010 }
Jens Axboe6b063142019-01-10 22:13:58 -07005011#endif
5012}
5013
Jens Axboe05f3fb32019-12-09 11:22:50 -07005014static void io_file_ref_kill(struct percpu_ref *ref)
5015{
5016 struct fixed_file_data *data;
5017
5018 data = container_of(ref, struct fixed_file_data, refs);
5019 complete(&data->done);
5020}
5021
Jens Axboe6b063142019-01-10 22:13:58 -07005022static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
5023{
Jens Axboe05f3fb32019-12-09 11:22:50 -07005024 struct fixed_file_data *data = ctx->file_data;
Jens Axboe65e19f52019-10-26 07:20:21 -06005025 unsigned nr_tables, i;
5026
Jens Axboe05f3fb32019-12-09 11:22:50 -07005027 if (!data)
Jens Axboe6b063142019-01-10 22:13:58 -07005028 return -ENXIO;
5029
Jens Axboe05f3fb32019-12-09 11:22:50 -07005030 /* protect against inflight atomic switch, which drops the ref */
5031 flush_work(&data->ref_work);
5032 percpu_ref_get(&data->refs);
5033 percpu_ref_kill_and_confirm(&data->refs, io_file_ref_kill);
5034 wait_for_completion(&data->done);
5035 percpu_ref_put(&data->refs);
5036 percpu_ref_exit(&data->refs);
5037
Jens Axboe6b063142019-01-10 22:13:58 -07005038 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06005039 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
5040 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005041 kfree(data->table[i].files);
5042 kfree(data->table);
5043 kfree(data);
5044 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07005045 ctx->nr_user_files = 0;
5046 return 0;
5047}
5048
Jens Axboe6c271ce2019-01-10 11:22:30 -07005049static void io_sq_thread_stop(struct io_ring_ctx *ctx)
5050{
5051 if (ctx->sqo_thread) {
Jens Axboe206aefd2019-11-07 18:27:42 -07005052 wait_for_completion(&ctx->completions[1]);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005053 /*
5054 * The park is a bit of a work-around, without it we get
5055 * warning spews on shutdown with SQPOLL set and affinity
5056 * set to a single CPU.
5057 */
Jens Axboe06058632019-04-13 09:26:03 -06005058 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005059 kthread_stop(ctx->sqo_thread);
5060 ctx->sqo_thread = NULL;
5061 }
5062}
5063
Jens Axboe6b063142019-01-10 22:13:58 -07005064static void io_finish_async(struct io_ring_ctx *ctx)
5065{
Jens Axboe6c271ce2019-01-10 11:22:30 -07005066 io_sq_thread_stop(ctx);
5067
Jens Axboe561fb042019-10-24 07:25:42 -06005068 if (ctx->io_wq) {
5069 io_wq_destroy(ctx->io_wq);
5070 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07005071 }
5072}
5073
5074#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07005075/*
5076 * Ensure the UNIX gc is aware of our file set, so we are certain that
5077 * the io_uring can be safely unregistered on process exit, even if we have
5078 * loops in the file referencing.
5079 */
5080static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
5081{
5082 struct sock *sk = ctx->ring_sock->sk;
5083 struct scm_fp_list *fpl;
5084 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06005085 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07005086
5087 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
5088 unsigned long inflight = ctx->user->unix_inflight + nr;
5089
5090 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
5091 return -EMFILE;
5092 }
5093
5094 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
5095 if (!fpl)
5096 return -ENOMEM;
5097
5098 skb = alloc_skb(0, GFP_KERNEL);
5099 if (!skb) {
5100 kfree(fpl);
5101 return -ENOMEM;
5102 }
5103
5104 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07005105
Jens Axboe08a45172019-10-03 08:11:03 -06005106 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07005107 fpl->user = get_uid(ctx->user);
5108 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005109 struct file *file = io_file_from_index(ctx, i + offset);
5110
5111 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06005112 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06005113 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06005114 unix_inflight(fpl->user, fpl->fp[nr_files]);
5115 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07005116 }
5117
Jens Axboe08a45172019-10-03 08:11:03 -06005118 if (nr_files) {
5119 fpl->max = SCM_MAX_FD;
5120 fpl->count = nr_files;
5121 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005122 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06005123 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
5124 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07005125
Jens Axboe08a45172019-10-03 08:11:03 -06005126 for (i = 0; i < nr_files; i++)
5127 fput(fpl->fp[i]);
5128 } else {
5129 kfree_skb(skb);
5130 kfree(fpl);
5131 }
Jens Axboe6b063142019-01-10 22:13:58 -07005132
5133 return 0;
5134}
5135
5136/*
5137 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
5138 * causes regular reference counting to break down. We rely on the UNIX
5139 * garbage collection to take care of this problem for us.
5140 */
5141static int io_sqe_files_scm(struct io_ring_ctx *ctx)
5142{
5143 unsigned left, total;
5144 int ret = 0;
5145
5146 total = 0;
5147 left = ctx->nr_user_files;
5148 while (left) {
5149 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07005150
5151 ret = __io_sqe_files_scm(ctx, this_files, total);
5152 if (ret)
5153 break;
5154 left -= this_files;
5155 total += this_files;
5156 }
5157
5158 if (!ret)
5159 return 0;
5160
5161 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005162 struct file *file = io_file_from_index(ctx, total);
5163
5164 if (file)
5165 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07005166 total++;
5167 }
5168
5169 return ret;
5170}
5171#else
5172static int io_sqe_files_scm(struct io_ring_ctx *ctx)
5173{
5174 return 0;
5175}
5176#endif
5177
Jens Axboe65e19f52019-10-26 07:20:21 -06005178static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
5179 unsigned nr_files)
5180{
5181 int i;
5182
5183 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005184 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06005185 unsigned this_files;
5186
5187 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
5188 table->files = kcalloc(this_files, sizeof(struct file *),
5189 GFP_KERNEL);
5190 if (!table->files)
5191 break;
5192 nr_files -= this_files;
5193 }
5194
5195 if (i == nr_tables)
5196 return 0;
5197
5198 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005199 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06005200 kfree(table->files);
5201 }
5202 return 1;
5203}
5204
Jens Axboe05f3fb32019-12-09 11:22:50 -07005205static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
Jens Axboec3a31e62019-10-03 13:59:56 -06005206{
5207#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06005208 struct sock *sock = ctx->ring_sock->sk;
5209 struct sk_buff_head list, *head = &sock->sk_receive_queue;
5210 struct sk_buff *skb;
5211 int i;
5212
5213 __skb_queue_head_init(&list);
5214
5215 /*
5216 * Find the skb that holds this file in its SCM_RIGHTS. When found,
5217 * remove this entry and rearrange the file array.
5218 */
5219 skb = skb_dequeue(head);
5220 while (skb) {
5221 struct scm_fp_list *fp;
5222
5223 fp = UNIXCB(skb).fp;
5224 for (i = 0; i < fp->count; i++) {
5225 int left;
5226
5227 if (fp->fp[i] != file)
5228 continue;
5229
5230 unix_notinflight(fp->user, fp->fp[i]);
5231 left = fp->count - 1 - i;
5232 if (left) {
5233 memmove(&fp->fp[i], &fp->fp[i + 1],
5234 left * sizeof(struct file *));
5235 }
5236 fp->count--;
5237 if (!fp->count) {
5238 kfree_skb(skb);
5239 skb = NULL;
5240 } else {
5241 __skb_queue_tail(&list, skb);
5242 }
5243 fput(file);
5244 file = NULL;
5245 break;
5246 }
5247
5248 if (!file)
5249 break;
5250
5251 __skb_queue_tail(&list, skb);
5252
5253 skb = skb_dequeue(head);
5254 }
5255
5256 if (skb_peek(&list)) {
5257 spin_lock_irq(&head->lock);
5258 while ((skb = __skb_dequeue(&list)) != NULL)
5259 __skb_queue_tail(head, skb);
5260 spin_unlock_irq(&head->lock);
5261 }
5262#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07005263 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06005264#endif
5265}
5266
Jens Axboe05f3fb32019-12-09 11:22:50 -07005267struct io_file_put {
5268 struct llist_node llist;
5269 struct file *file;
5270 struct completion *done;
5271};
5272
5273static void io_ring_file_ref_switch(struct work_struct *work)
5274{
5275 struct io_file_put *pfile, *tmp;
5276 struct fixed_file_data *data;
5277 struct llist_node *node;
5278
5279 data = container_of(work, struct fixed_file_data, ref_work);
5280
5281 while ((node = llist_del_all(&data->put_llist)) != NULL) {
5282 llist_for_each_entry_safe(pfile, tmp, node, llist) {
5283 io_ring_file_put(data->ctx, pfile->file);
5284 if (pfile->done)
5285 complete(pfile->done);
5286 else
5287 kfree(pfile);
5288 }
5289 }
5290
5291 percpu_ref_get(&data->refs);
5292 percpu_ref_switch_to_percpu(&data->refs);
5293}
5294
5295static void io_file_data_ref_zero(struct percpu_ref *ref)
5296{
5297 struct fixed_file_data *data;
5298
5299 data = container_of(ref, struct fixed_file_data, refs);
5300
5301 /* we can't safely switch from inside this context, punt to wq */
5302 queue_work(system_wq, &data->ref_work);
5303}
5304
5305static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
5306 unsigned nr_args)
5307{
5308 __s32 __user *fds = (__s32 __user *) arg;
5309 unsigned nr_tables;
5310 struct file *file;
5311 int fd, ret = 0;
5312 unsigned i;
5313
5314 if (ctx->file_data)
5315 return -EBUSY;
5316 if (!nr_args)
5317 return -EINVAL;
5318 if (nr_args > IORING_MAX_FIXED_FILES)
5319 return -EMFILE;
5320
5321 ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
5322 if (!ctx->file_data)
5323 return -ENOMEM;
5324 ctx->file_data->ctx = ctx;
5325 init_completion(&ctx->file_data->done);
5326
5327 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
5328 ctx->file_data->table = kcalloc(nr_tables,
5329 sizeof(struct fixed_file_table),
5330 GFP_KERNEL);
5331 if (!ctx->file_data->table) {
5332 kfree(ctx->file_data);
5333 ctx->file_data = NULL;
5334 return -ENOMEM;
5335 }
5336
5337 if (percpu_ref_init(&ctx->file_data->refs, io_file_data_ref_zero,
5338 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
5339 kfree(ctx->file_data->table);
5340 kfree(ctx->file_data);
5341 ctx->file_data = NULL;
5342 return -ENOMEM;
5343 }
5344 ctx->file_data->put_llist.first = NULL;
5345 INIT_WORK(&ctx->file_data->ref_work, io_ring_file_ref_switch);
5346
5347 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
5348 percpu_ref_exit(&ctx->file_data->refs);
5349 kfree(ctx->file_data->table);
5350 kfree(ctx->file_data);
5351 ctx->file_data = NULL;
5352 return -ENOMEM;
5353 }
5354
5355 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
5356 struct fixed_file_table *table;
5357 unsigned index;
5358
5359 ret = -EFAULT;
5360 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
5361 break;
5362 /* allow sparse sets */
5363 if (fd == -1) {
5364 ret = 0;
5365 continue;
5366 }
5367
5368 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
5369 index = i & IORING_FILE_TABLE_MASK;
5370 file = fget(fd);
5371
5372 ret = -EBADF;
5373 if (!file)
5374 break;
5375
5376 /*
5377 * Don't allow io_uring instances to be registered. If UNIX
5378 * isn't enabled, then this causes a reference cycle and this
5379 * instance can never get freed. If UNIX is enabled we'll
5380 * handle it just fine, but there's still no point in allowing
5381 * a ring fd as it doesn't support regular read/write anyway.
5382 */
5383 if (file->f_op == &io_uring_fops) {
5384 fput(file);
5385 break;
5386 }
5387 ret = 0;
5388 table->files[index] = file;
5389 }
5390
5391 if (ret) {
5392 for (i = 0; i < ctx->nr_user_files; i++) {
5393 file = io_file_from_index(ctx, i);
5394 if (file)
5395 fput(file);
5396 }
5397 for (i = 0; i < nr_tables; i++)
5398 kfree(ctx->file_data->table[i].files);
5399
5400 kfree(ctx->file_data->table);
5401 kfree(ctx->file_data);
5402 ctx->file_data = NULL;
5403 ctx->nr_user_files = 0;
5404 return ret;
5405 }
5406
5407 ret = io_sqe_files_scm(ctx);
5408 if (ret)
5409 io_sqe_files_unregister(ctx);
5410
5411 return ret;
5412}
5413
Jens Axboec3a31e62019-10-03 13:59:56 -06005414static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
5415 int index)
5416{
5417#if defined(CONFIG_UNIX)
5418 struct sock *sock = ctx->ring_sock->sk;
5419 struct sk_buff_head *head = &sock->sk_receive_queue;
5420 struct sk_buff *skb;
5421
5422 /*
5423 * See if we can merge this file into an existing skb SCM_RIGHTS
5424 * file set. If there's no room, fall back to allocating a new skb
5425 * and filling it in.
5426 */
5427 spin_lock_irq(&head->lock);
5428 skb = skb_peek(head);
5429 if (skb) {
5430 struct scm_fp_list *fpl = UNIXCB(skb).fp;
5431
5432 if (fpl->count < SCM_MAX_FD) {
5433 __skb_unlink(skb, head);
5434 spin_unlock_irq(&head->lock);
5435 fpl->fp[fpl->count] = get_file(file);
5436 unix_inflight(fpl->user, fpl->fp[fpl->count]);
5437 fpl->count++;
5438 spin_lock_irq(&head->lock);
5439 __skb_queue_head(head, skb);
5440 } else {
5441 skb = NULL;
5442 }
5443 }
5444 spin_unlock_irq(&head->lock);
5445
5446 if (skb) {
5447 fput(file);
5448 return 0;
5449 }
5450
5451 return __io_sqe_files_scm(ctx, 1, index);
5452#else
5453 return 0;
5454#endif
5455}
5456
Jens Axboe05f3fb32019-12-09 11:22:50 -07005457static void io_atomic_switch(struct percpu_ref *ref)
Jens Axboec3a31e62019-10-03 13:59:56 -06005458{
Jens Axboe05f3fb32019-12-09 11:22:50 -07005459 struct fixed_file_data *data;
5460
5461 data = container_of(ref, struct fixed_file_data, refs);
5462 clear_bit(FFD_F_ATOMIC, &data->state);
5463}
5464
5465static bool io_queue_file_removal(struct fixed_file_data *data,
5466 struct file *file)
5467{
5468 struct io_file_put *pfile, pfile_stack;
5469 DECLARE_COMPLETION_ONSTACK(done);
5470
5471 /*
5472 * If we fail allocating the struct we need for doing async reomval
5473 * of this file, just punt to sync and wait for it.
5474 */
5475 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
5476 if (!pfile) {
5477 pfile = &pfile_stack;
5478 pfile->done = &done;
5479 }
5480
5481 pfile->file = file;
5482 llist_add(&pfile->llist, &data->put_llist);
5483
5484 if (pfile == &pfile_stack) {
5485 if (!test_and_set_bit(FFD_F_ATOMIC, &data->state)) {
5486 percpu_ref_put(&data->refs);
5487 percpu_ref_switch_to_atomic(&data->refs,
5488 io_atomic_switch);
5489 }
5490 wait_for_completion(&done);
5491 flush_work(&data->ref_work);
5492 return false;
5493 }
5494
5495 return true;
5496}
5497
5498static int __io_sqe_files_update(struct io_ring_ctx *ctx,
5499 struct io_uring_files_update *up,
5500 unsigned nr_args)
5501{
5502 struct fixed_file_data *data = ctx->file_data;
5503 bool ref_switch = false;
5504 struct file *file;
Jens Axboec3a31e62019-10-03 13:59:56 -06005505 __s32 __user *fds;
5506 int fd, i, err;
5507 __u32 done;
5508
Jens Axboe05f3fb32019-12-09 11:22:50 -07005509 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06005510 return -EOVERFLOW;
5511 if (done > ctx->nr_user_files)
5512 return -EINVAL;
5513
5514 done = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005515 fds = u64_to_user_ptr(up->fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06005516 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005517 struct fixed_file_table *table;
5518 unsigned index;
5519
Jens Axboec3a31e62019-10-03 13:59:56 -06005520 err = 0;
5521 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
5522 err = -EFAULT;
5523 break;
5524 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07005525 i = array_index_nospec(up->offset, ctx->nr_user_files);
5526 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
Jens Axboe65e19f52019-10-26 07:20:21 -06005527 index = i & IORING_FILE_TABLE_MASK;
5528 if (table->files[index]) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005529 file = io_file_from_index(ctx, index);
Jens Axboe65e19f52019-10-26 07:20:21 -06005530 table->files[index] = NULL;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005531 if (io_queue_file_removal(data, file))
5532 ref_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06005533 }
5534 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06005535 file = fget(fd);
5536 if (!file) {
5537 err = -EBADF;
5538 break;
5539 }
5540 /*
5541 * Don't allow io_uring instances to be registered. If
5542 * UNIX isn't enabled, then this causes a reference
5543 * cycle and this instance can never get freed. If UNIX
5544 * is enabled we'll handle it just fine, but there's
5545 * still no point in allowing a ring fd as it doesn't
5546 * support regular read/write anyway.
5547 */
5548 if (file->f_op == &io_uring_fops) {
5549 fput(file);
5550 err = -EBADF;
5551 break;
5552 }
Jens Axboe65e19f52019-10-26 07:20:21 -06005553 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06005554 err = io_sqe_file_register(ctx, file, i);
5555 if (err)
5556 break;
5557 }
5558 nr_args--;
5559 done++;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005560 up->offset++;
5561 }
5562
5563 if (ref_switch && !test_and_set_bit(FFD_F_ATOMIC, &data->state)) {
5564 percpu_ref_put(&data->refs);
5565 percpu_ref_switch_to_atomic(&data->refs, io_atomic_switch);
Jens Axboec3a31e62019-10-03 13:59:56 -06005566 }
5567
5568 return done ? done : err;
5569}
Jens Axboe05f3fb32019-12-09 11:22:50 -07005570static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
5571 unsigned nr_args)
5572{
5573 struct io_uring_files_update up;
5574
5575 if (!ctx->file_data)
5576 return -ENXIO;
5577 if (!nr_args)
5578 return -EINVAL;
5579 if (copy_from_user(&up, arg, sizeof(up)))
5580 return -EFAULT;
5581 if (up.resv)
5582 return -EINVAL;
5583
5584 return __io_sqe_files_update(ctx, &up, nr_args);
5585}
Jens Axboec3a31e62019-10-03 13:59:56 -06005586
Jens Axboe7d723062019-11-12 22:31:31 -07005587static void io_put_work(struct io_wq_work *work)
5588{
5589 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
5590
5591 io_put_req(req);
5592}
5593
5594static void io_get_work(struct io_wq_work *work)
5595{
5596 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
5597
5598 refcount_inc(&req->refs);
5599}
5600
Jens Axboe6c271ce2019-01-10 11:22:30 -07005601static int io_sq_offload_start(struct io_ring_ctx *ctx,
5602 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005603{
Jens Axboe576a3472019-11-25 08:49:20 -07005604 struct io_wq_data data;
Jens Axboe561fb042019-10-24 07:25:42 -06005605 unsigned concurrency;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005606 int ret;
5607
Jens Axboe6c271ce2019-01-10 11:22:30 -07005608 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005609 mmgrab(current->mm);
5610 ctx->sqo_mm = current->mm;
5611
Jens Axboe6c271ce2019-01-10 11:22:30 -07005612 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06005613 ret = -EPERM;
5614 if (!capable(CAP_SYS_ADMIN))
5615 goto err;
5616
Jens Axboe917257d2019-04-13 09:28:55 -06005617 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
5618 if (!ctx->sq_thread_idle)
5619 ctx->sq_thread_idle = HZ;
5620
Jens Axboe6c271ce2019-01-10 11:22:30 -07005621 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06005622 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005623
Jens Axboe917257d2019-04-13 09:28:55 -06005624 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06005625 if (cpu >= nr_cpu_ids)
5626 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08005627 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06005628 goto err;
5629
Jens Axboe6c271ce2019-01-10 11:22:30 -07005630 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
5631 ctx, cpu,
5632 "io_uring-sq");
5633 } else {
5634 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
5635 "io_uring-sq");
5636 }
5637 if (IS_ERR(ctx->sqo_thread)) {
5638 ret = PTR_ERR(ctx->sqo_thread);
5639 ctx->sqo_thread = NULL;
5640 goto err;
5641 }
5642 wake_up_process(ctx->sqo_thread);
5643 } else if (p->flags & IORING_SETUP_SQ_AFF) {
5644 /* Can't have SQ_AFF without SQPOLL */
5645 ret = -EINVAL;
5646 goto err;
5647 }
5648
Jens Axboe576a3472019-11-25 08:49:20 -07005649 data.mm = ctx->sqo_mm;
5650 data.user = ctx->user;
Jens Axboe181e4482019-11-25 08:52:30 -07005651 data.creds = ctx->creds;
Jens Axboe576a3472019-11-25 08:49:20 -07005652 data.get_work = io_get_work;
5653 data.put_work = io_put_work;
5654
Jens Axboe561fb042019-10-24 07:25:42 -06005655 /* Do QD, or 4 * CPUS, whatever is smallest */
5656 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
Jens Axboe576a3472019-11-25 08:49:20 -07005657 ctx->io_wq = io_wq_create(concurrency, &data);
Jens Axboe975c99a52019-10-30 08:42:56 -06005658 if (IS_ERR(ctx->io_wq)) {
5659 ret = PTR_ERR(ctx->io_wq);
5660 ctx->io_wq = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005661 goto err;
5662 }
5663
5664 return 0;
5665err:
Jens Axboe54a91f32019-09-10 09:15:04 -06005666 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005667 mmdrop(ctx->sqo_mm);
5668 ctx->sqo_mm = NULL;
5669 return ret;
5670}
5671
5672static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
5673{
5674 atomic_long_sub(nr_pages, &user->locked_vm);
5675}
5676
5677static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
5678{
5679 unsigned long page_limit, cur_pages, new_pages;
5680
5681 /* Don't allow more pages than we can safely lock */
5682 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
5683
5684 do {
5685 cur_pages = atomic_long_read(&user->locked_vm);
5686 new_pages = cur_pages + nr_pages;
5687 if (new_pages > page_limit)
5688 return -ENOMEM;
5689 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
5690 new_pages) != cur_pages);
5691
5692 return 0;
5693}
5694
5695static void io_mem_free(void *ptr)
5696{
Mark Rutland52e04ef2019-04-30 17:30:21 +01005697 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005698
Mark Rutland52e04ef2019-04-30 17:30:21 +01005699 if (!ptr)
5700 return;
5701
5702 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005703 if (put_page_testzero(page))
5704 free_compound_page(page);
5705}
5706
5707static void *io_mem_alloc(size_t size)
5708{
5709 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
5710 __GFP_NORETRY;
5711
5712 return (void *) __get_free_pages(gfp_flags, get_order(size));
5713}
5714
Hristo Venev75b28af2019-08-26 17:23:46 +00005715static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
5716 size_t *sq_offset)
5717{
5718 struct io_rings *rings;
5719 size_t off, sq_array_size;
5720
5721 off = struct_size(rings, cqes, cq_entries);
5722 if (off == SIZE_MAX)
5723 return SIZE_MAX;
5724
5725#ifdef CONFIG_SMP
5726 off = ALIGN(off, SMP_CACHE_BYTES);
5727 if (off == 0)
5728 return SIZE_MAX;
5729#endif
5730
5731 sq_array_size = array_size(sizeof(u32), sq_entries);
5732 if (sq_array_size == SIZE_MAX)
5733 return SIZE_MAX;
5734
5735 if (check_add_overflow(off, sq_array_size, &off))
5736 return SIZE_MAX;
5737
5738 if (sq_offset)
5739 *sq_offset = off;
5740
5741 return off;
5742}
5743
Jens Axboe2b188cc2019-01-07 10:46:33 -07005744static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
5745{
Hristo Venev75b28af2019-08-26 17:23:46 +00005746 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005747
Hristo Venev75b28af2019-08-26 17:23:46 +00005748 pages = (size_t)1 << get_order(
5749 rings_size(sq_entries, cq_entries, NULL));
5750 pages += (size_t)1 << get_order(
5751 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07005752
Hristo Venev75b28af2019-08-26 17:23:46 +00005753 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005754}
5755
Jens Axboeedafcce2019-01-09 09:16:05 -07005756static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
5757{
5758 int i, j;
5759
5760 if (!ctx->user_bufs)
5761 return -ENXIO;
5762
5763 for (i = 0; i < ctx->nr_user_bufs; i++) {
5764 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
5765
5766 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbard27c4d3a2019-08-04 19:32:06 -07005767 put_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07005768
5769 if (ctx->account_mem)
5770 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005771 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07005772 imu->nr_bvecs = 0;
5773 }
5774
5775 kfree(ctx->user_bufs);
5776 ctx->user_bufs = NULL;
5777 ctx->nr_user_bufs = 0;
5778 return 0;
5779}
5780
5781static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
5782 void __user *arg, unsigned index)
5783{
5784 struct iovec __user *src;
5785
5786#ifdef CONFIG_COMPAT
5787 if (ctx->compat) {
5788 struct compat_iovec __user *ciovs;
5789 struct compat_iovec ciov;
5790
5791 ciovs = (struct compat_iovec __user *) arg;
5792 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
5793 return -EFAULT;
5794
Jens Axboed55e5f52019-12-11 16:12:15 -07005795 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07005796 dst->iov_len = ciov.iov_len;
5797 return 0;
5798 }
5799#endif
5800 src = (struct iovec __user *) arg;
5801 if (copy_from_user(dst, &src[index], sizeof(*dst)))
5802 return -EFAULT;
5803 return 0;
5804}
5805
5806static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
5807 unsigned nr_args)
5808{
5809 struct vm_area_struct **vmas = NULL;
5810 struct page **pages = NULL;
5811 int i, j, got_pages = 0;
5812 int ret = -EINVAL;
5813
5814 if (ctx->user_bufs)
5815 return -EBUSY;
5816 if (!nr_args || nr_args > UIO_MAXIOV)
5817 return -EINVAL;
5818
5819 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
5820 GFP_KERNEL);
5821 if (!ctx->user_bufs)
5822 return -ENOMEM;
5823
5824 for (i = 0; i < nr_args; i++) {
5825 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
5826 unsigned long off, start, end, ubuf;
5827 int pret, nr_pages;
5828 struct iovec iov;
5829 size_t size;
5830
5831 ret = io_copy_iov(ctx, &iov, arg, i);
5832 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03005833 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07005834
5835 /*
5836 * Don't impose further limits on the size and buffer
5837 * constraints here, we'll -EINVAL later when IO is
5838 * submitted if they are wrong.
5839 */
5840 ret = -EFAULT;
5841 if (!iov.iov_base || !iov.iov_len)
5842 goto err;
5843
5844 /* arbitrary limit, but we need something */
5845 if (iov.iov_len > SZ_1G)
5846 goto err;
5847
5848 ubuf = (unsigned long) iov.iov_base;
5849 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
5850 start = ubuf >> PAGE_SHIFT;
5851 nr_pages = end - start;
5852
5853 if (ctx->account_mem) {
5854 ret = io_account_mem(ctx->user, nr_pages);
5855 if (ret)
5856 goto err;
5857 }
5858
5859 ret = 0;
5860 if (!pages || nr_pages > got_pages) {
5861 kfree(vmas);
5862 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005863 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07005864 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005865 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07005866 sizeof(struct vm_area_struct *),
5867 GFP_KERNEL);
5868 if (!pages || !vmas) {
5869 ret = -ENOMEM;
5870 if (ctx->account_mem)
5871 io_unaccount_mem(ctx->user, nr_pages);
5872 goto err;
5873 }
5874 got_pages = nr_pages;
5875 }
5876
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005877 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07005878 GFP_KERNEL);
5879 ret = -ENOMEM;
5880 if (!imu->bvec) {
5881 if (ctx->account_mem)
5882 io_unaccount_mem(ctx->user, nr_pages);
5883 goto err;
5884 }
5885
5886 ret = 0;
5887 down_read(&current->mm->mmap_sem);
Ira Weiny932f4a62019-05-13 17:17:03 -07005888 pret = get_user_pages(ubuf, nr_pages,
5889 FOLL_WRITE | FOLL_LONGTERM,
5890 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07005891 if (pret == nr_pages) {
5892 /* don't support file backed memory */
5893 for (j = 0; j < nr_pages; j++) {
5894 struct vm_area_struct *vma = vmas[j];
5895
5896 if (vma->vm_file &&
5897 !is_file_hugepages(vma->vm_file)) {
5898 ret = -EOPNOTSUPP;
5899 break;
5900 }
5901 }
5902 } else {
5903 ret = pret < 0 ? pret : -EFAULT;
5904 }
5905 up_read(&current->mm->mmap_sem);
5906 if (ret) {
5907 /*
5908 * if we did partial map, or found file backed vmas,
5909 * release any pages we did get
5910 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07005911 if (pret > 0)
5912 put_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07005913 if (ctx->account_mem)
5914 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005915 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07005916 goto err;
5917 }
5918
5919 off = ubuf & ~PAGE_MASK;
5920 size = iov.iov_len;
5921 for (j = 0; j < nr_pages; j++) {
5922 size_t vec_len;
5923
5924 vec_len = min_t(size_t, size, PAGE_SIZE - off);
5925 imu->bvec[j].bv_page = pages[j];
5926 imu->bvec[j].bv_len = vec_len;
5927 imu->bvec[j].bv_offset = off;
5928 off = 0;
5929 size -= vec_len;
5930 }
5931 /* store original address for later verification */
5932 imu->ubuf = ubuf;
5933 imu->len = iov.iov_len;
5934 imu->nr_bvecs = nr_pages;
5935
5936 ctx->nr_user_bufs++;
5937 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005938 kvfree(pages);
5939 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07005940 return 0;
5941err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005942 kvfree(pages);
5943 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07005944 io_sqe_buffer_unregister(ctx);
5945 return ret;
5946}
5947
Jens Axboe9b402842019-04-11 11:45:41 -06005948static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
5949{
5950 __s32 __user *fds = arg;
5951 int fd;
5952
5953 if (ctx->cq_ev_fd)
5954 return -EBUSY;
5955
5956 if (copy_from_user(&fd, fds, sizeof(*fds)))
5957 return -EFAULT;
5958
5959 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
5960 if (IS_ERR(ctx->cq_ev_fd)) {
5961 int ret = PTR_ERR(ctx->cq_ev_fd);
5962 ctx->cq_ev_fd = NULL;
5963 return ret;
5964 }
5965
5966 return 0;
5967}
5968
5969static int io_eventfd_unregister(struct io_ring_ctx *ctx)
5970{
5971 if (ctx->cq_ev_fd) {
5972 eventfd_ctx_put(ctx->cq_ev_fd);
5973 ctx->cq_ev_fd = NULL;
5974 return 0;
5975 }
5976
5977 return -ENXIO;
5978}
5979
Jens Axboe2b188cc2019-01-07 10:46:33 -07005980static void io_ring_ctx_free(struct io_ring_ctx *ctx)
5981{
Jens Axboe6b063142019-01-10 22:13:58 -07005982 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005983 if (ctx->sqo_mm)
5984 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07005985
5986 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07005987 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07005988 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06005989 io_eventfd_unregister(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07005990
Jens Axboe2b188cc2019-01-07 10:46:33 -07005991#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07005992 if (ctx->ring_sock) {
5993 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07005994 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07005995 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07005996#endif
5997
Hristo Venev75b28af2019-08-26 17:23:46 +00005998 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005999 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006000
6001 percpu_ref_exit(&ctx->refs);
6002 if (ctx->account_mem)
6003 io_unaccount_mem(ctx->user,
6004 ring_pages(ctx->sq_entries, ctx->cq_entries));
6005 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07006006 put_cred(ctx->creds);
Jens Axboe206aefd2019-11-07 18:27:42 -07006007 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -07006008 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07006009 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006010 kfree(ctx);
6011}
6012
6013static __poll_t io_uring_poll(struct file *file, poll_table *wait)
6014{
6015 struct io_ring_ctx *ctx = file->private_data;
6016 __poll_t mask = 0;
6017
6018 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02006019 /*
6020 * synchronizes with barrier from wq_has_sleeper call in
6021 * io_commit_cqring
6022 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07006023 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00006024 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
6025 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006026 mask |= EPOLLOUT | EPOLLWRNORM;
yangerkundaa5de52019-09-24 20:53:34 +08006027 if (READ_ONCE(ctx->rings->cq.head) != ctx->cached_cq_tail)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006028 mask |= EPOLLIN | EPOLLRDNORM;
6029
6030 return mask;
6031}
6032
6033static int io_uring_fasync(int fd, struct file *file, int on)
6034{
6035 struct io_ring_ctx *ctx = file->private_data;
6036
6037 return fasync_helper(fd, file, on, &ctx->cq_fasync);
6038}
6039
6040static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
6041{
6042 mutex_lock(&ctx->uring_lock);
6043 percpu_ref_kill(&ctx->refs);
6044 mutex_unlock(&ctx->uring_lock);
6045
Jens Axboe5262f562019-09-17 12:26:57 -06006046 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07006047 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06006048
6049 if (ctx->io_wq)
6050 io_wq_cancel_all(ctx->io_wq);
6051
Jens Axboedef596e2019-01-09 08:59:42 -07006052 io_iopoll_reap_events(ctx);
Jens Axboe15dff282019-11-13 09:09:23 -07006053 /* if we failed setting up the ctx, we might not have any rings */
6054 if (ctx->rings)
6055 io_cqring_overflow_flush(ctx, true);
Jens Axboe206aefd2019-11-07 18:27:42 -07006056 wait_for_completion(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006057 io_ring_ctx_free(ctx);
6058}
6059
6060static int io_uring_release(struct inode *inode, struct file *file)
6061{
6062 struct io_ring_ctx *ctx = file->private_data;
6063
6064 file->private_data = NULL;
6065 io_ring_ctx_wait_and_kill(ctx);
6066 return 0;
6067}
6068
Jens Axboefcb323c2019-10-24 12:39:47 -06006069static void io_uring_cancel_files(struct io_ring_ctx *ctx,
6070 struct files_struct *files)
6071{
6072 struct io_kiocb *req;
6073 DEFINE_WAIT(wait);
6074
6075 while (!list_empty_careful(&ctx->inflight_list)) {
Jens Axboe768134d2019-11-10 20:30:53 -07006076 struct io_kiocb *cancel_req = NULL;
Jens Axboefcb323c2019-10-24 12:39:47 -06006077
6078 spin_lock_irq(&ctx->inflight_lock);
6079 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07006080 if (req->work.files != files)
6081 continue;
6082 /* req is being completed, ignore */
6083 if (!refcount_inc_not_zero(&req->refs))
6084 continue;
6085 cancel_req = req;
6086 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06006087 }
Jens Axboe768134d2019-11-10 20:30:53 -07006088 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06006089 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07006090 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06006091 spin_unlock_irq(&ctx->inflight_lock);
6092
Jens Axboe768134d2019-11-10 20:30:53 -07006093 /* We need to keep going until we don't find a matching req */
6094 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06006095 break;
Bob Liu2f6d9b92019-11-13 18:06:24 +08006096
6097 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
6098 io_put_req(cancel_req);
Jens Axboefcb323c2019-10-24 12:39:47 -06006099 schedule();
6100 }
Jens Axboe768134d2019-11-10 20:30:53 -07006101 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06006102}
6103
6104static int io_uring_flush(struct file *file, void *data)
6105{
6106 struct io_ring_ctx *ctx = file->private_data;
6107
6108 io_uring_cancel_files(ctx, data);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006109 if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) {
6110 io_cqring_overflow_flush(ctx, true);
Jens Axboefcb323c2019-10-24 12:39:47 -06006111 io_wq_cancel_all(ctx->io_wq);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006112 }
Jens Axboefcb323c2019-10-24 12:39:47 -06006113 return 0;
6114}
6115
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006116static void *io_uring_validate_mmap_request(struct file *file,
6117 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006118{
Jens Axboe2b188cc2019-01-07 10:46:33 -07006119 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006120 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006121 struct page *page;
6122 void *ptr;
6123
6124 switch (offset) {
6125 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00006126 case IORING_OFF_CQ_RING:
6127 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006128 break;
6129 case IORING_OFF_SQES:
6130 ptr = ctx->sq_sqes;
6131 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006132 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006133 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006134 }
6135
6136 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07006137 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006138 return ERR_PTR(-EINVAL);
6139
6140 return ptr;
6141}
6142
6143#ifdef CONFIG_MMU
6144
6145static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
6146{
6147 size_t sz = vma->vm_end - vma->vm_start;
6148 unsigned long pfn;
6149 void *ptr;
6150
6151 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
6152 if (IS_ERR(ptr))
6153 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006154
6155 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
6156 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
6157}
6158
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006159#else /* !CONFIG_MMU */
6160
6161static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
6162{
6163 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
6164}
6165
6166static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
6167{
6168 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
6169}
6170
6171static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
6172 unsigned long addr, unsigned long len,
6173 unsigned long pgoff, unsigned long flags)
6174{
6175 void *ptr;
6176
6177 ptr = io_uring_validate_mmap_request(file, pgoff, len);
6178 if (IS_ERR(ptr))
6179 return PTR_ERR(ptr);
6180
6181 return (unsigned long) ptr;
6182}
6183
6184#endif /* !CONFIG_MMU */
6185
Jens Axboe2b188cc2019-01-07 10:46:33 -07006186SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
6187 u32, min_complete, u32, flags, const sigset_t __user *, sig,
6188 size_t, sigsz)
6189{
6190 struct io_ring_ctx *ctx;
6191 long ret = -EBADF;
6192 int submitted = 0;
6193 struct fd f;
6194
Jens Axboe6c271ce2019-01-10 11:22:30 -07006195 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006196 return -EINVAL;
6197
6198 f = fdget(fd);
6199 if (!f.file)
6200 return -EBADF;
6201
6202 ret = -EOPNOTSUPP;
6203 if (f.file->f_op != &io_uring_fops)
6204 goto out_fput;
6205
6206 ret = -ENXIO;
6207 ctx = f.file->private_data;
6208 if (!percpu_ref_tryget(&ctx->refs))
6209 goto out_fput;
6210
Jens Axboe6c271ce2019-01-10 11:22:30 -07006211 /*
6212 * For SQ polling, the thread will do all submissions and completions.
6213 * Just return the requested submit count, and wake the thread if
6214 * we were asked to.
6215 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06006216 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006217 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07006218 if (!list_empty_careful(&ctx->cq_overflow_list))
6219 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006220 if (flags & IORING_ENTER_SQ_WAKEUP)
6221 wake_up(&ctx->sqo_wait);
6222 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06006223 } else if (to_submit) {
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006224 struct mm_struct *cur_mm;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006225
Jens Axboe44d28272020-01-16 19:00:24 -07006226 if (current->mm != ctx->sqo_mm ||
6227 current_cred() != ctx->creds) {
6228 ret = -EPERM;
6229 goto out;
6230 }
6231
Jens Axboe2b188cc2019-01-07 10:46:33 -07006232 mutex_lock(&ctx->uring_lock);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006233 /* already have mm, so io_submit_sqes() won't try to grab it */
6234 cur_mm = ctx->sqo_mm;
6235 submitted = io_submit_sqes(ctx, to_submit, f.file, fd,
6236 &cur_mm, false);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006237 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03006238
6239 if (submitted != to_submit)
6240 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006241 }
6242 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07006243 unsigned nr_events = 0;
6244
Jens Axboe2b188cc2019-01-07 10:46:33 -07006245 min_complete = min(min_complete, ctx->cq_entries);
6246
Jens Axboedef596e2019-01-09 08:59:42 -07006247 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07006248 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07006249 } else {
6250 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
6251 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006252 }
6253
Pavel Begunkov7c504e652019-12-18 19:53:45 +03006254out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03006255 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006256out_fput:
6257 fdput(f);
6258 return submitted ? submitted : ret;
6259}
6260
6261static const struct file_operations io_uring_fops = {
6262 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06006263 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07006264 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006265#ifndef CONFIG_MMU
6266 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
6267 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
6268#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07006269 .poll = io_uring_poll,
6270 .fasync = io_uring_fasync,
6271};
6272
6273static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
6274 struct io_uring_params *p)
6275{
Hristo Venev75b28af2019-08-26 17:23:46 +00006276 struct io_rings *rings;
6277 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006278
Hristo Venev75b28af2019-08-26 17:23:46 +00006279 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
6280 if (size == SIZE_MAX)
6281 return -EOVERFLOW;
6282
6283 rings = io_mem_alloc(size);
6284 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006285 return -ENOMEM;
6286
Hristo Venev75b28af2019-08-26 17:23:46 +00006287 ctx->rings = rings;
6288 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
6289 rings->sq_ring_mask = p->sq_entries - 1;
6290 rings->cq_ring_mask = p->cq_entries - 1;
6291 rings->sq_ring_entries = p->sq_entries;
6292 rings->cq_ring_entries = p->cq_entries;
6293 ctx->sq_mask = rings->sq_ring_mask;
6294 ctx->cq_mask = rings->cq_ring_mask;
6295 ctx->sq_entries = rings->sq_ring_entries;
6296 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006297
6298 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07006299 if (size == SIZE_MAX) {
6300 io_mem_free(ctx->rings);
6301 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006302 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07006303 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006304
6305 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07006306 if (!ctx->sq_sqes) {
6307 io_mem_free(ctx->rings);
6308 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006309 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07006310 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006311
Jens Axboe2b188cc2019-01-07 10:46:33 -07006312 return 0;
6313}
6314
6315/*
6316 * Allocate an anonymous fd, this is what constitutes the application
6317 * visible backing of an io_uring instance. The application mmaps this
6318 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
6319 * we have to tie this fd to a socket for file garbage collection purposes.
6320 */
6321static int io_uring_get_fd(struct io_ring_ctx *ctx)
6322{
6323 struct file *file;
6324 int ret;
6325
6326#if defined(CONFIG_UNIX)
6327 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
6328 &ctx->ring_sock);
6329 if (ret)
6330 return ret;
6331#endif
6332
6333 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
6334 if (ret < 0)
6335 goto err;
6336
6337 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
6338 O_RDWR | O_CLOEXEC);
6339 if (IS_ERR(file)) {
6340 put_unused_fd(ret);
6341 ret = PTR_ERR(file);
6342 goto err;
6343 }
6344
6345#if defined(CONFIG_UNIX)
6346 ctx->ring_sock->file = file;
6347#endif
6348 fd_install(ret, file);
6349 return ret;
6350err:
6351#if defined(CONFIG_UNIX)
6352 sock_release(ctx->ring_sock);
6353 ctx->ring_sock = NULL;
6354#endif
6355 return ret;
6356}
6357
6358static int io_uring_create(unsigned entries, struct io_uring_params *p)
6359{
6360 struct user_struct *user = NULL;
6361 struct io_ring_ctx *ctx;
6362 bool account_mem;
6363 int ret;
6364
Jens Axboe8110c1a2019-12-28 15:39:54 -07006365 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006366 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07006367 if (entries > IORING_MAX_ENTRIES) {
6368 if (!(p->flags & IORING_SETUP_CLAMP))
6369 return -EINVAL;
6370 entries = IORING_MAX_ENTRIES;
6371 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006372
6373 /*
6374 * Use twice as many entries for the CQ ring. It's possible for the
6375 * application to drive a higher depth than the size of the SQ ring,
6376 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06006377 * some flexibility in overcommitting a bit. If the application has
6378 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
6379 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07006380 */
6381 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06006382 if (p->flags & IORING_SETUP_CQSIZE) {
6383 /*
6384 * If IORING_SETUP_CQSIZE is set, we do the same roundup
6385 * to a power-of-two, if it isn't already. We do NOT impose
6386 * any cq vs sq ring sizing.
6387 */
Jens Axboe8110c1a2019-12-28 15:39:54 -07006388 if (p->cq_entries < p->sq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06006389 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07006390 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
6391 if (!(p->flags & IORING_SETUP_CLAMP))
6392 return -EINVAL;
6393 p->cq_entries = IORING_MAX_CQ_ENTRIES;
6394 }
Jens Axboe33a107f2019-10-04 12:10:03 -06006395 p->cq_entries = roundup_pow_of_two(p->cq_entries);
6396 } else {
6397 p->cq_entries = 2 * p->sq_entries;
6398 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006399
6400 user = get_uid(current_user());
6401 account_mem = !capable(CAP_IPC_LOCK);
6402
6403 if (account_mem) {
6404 ret = io_account_mem(user,
6405 ring_pages(p->sq_entries, p->cq_entries));
6406 if (ret) {
6407 free_uid(user);
6408 return ret;
6409 }
6410 }
6411
6412 ctx = io_ring_ctx_alloc(p);
6413 if (!ctx) {
6414 if (account_mem)
6415 io_unaccount_mem(user, ring_pages(p->sq_entries,
6416 p->cq_entries));
6417 free_uid(user);
6418 return -ENOMEM;
6419 }
6420 ctx->compat = in_compat_syscall();
6421 ctx->account_mem = account_mem;
6422 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07006423 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07006424
6425 ret = io_allocate_scq_urings(ctx, p);
6426 if (ret)
6427 goto err;
6428
Jens Axboe6c271ce2019-01-10 11:22:30 -07006429 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006430 if (ret)
6431 goto err;
6432
Jens Axboe2b188cc2019-01-07 10:46:33 -07006433 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00006434 p->sq_off.head = offsetof(struct io_rings, sq.head);
6435 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
6436 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
6437 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
6438 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
6439 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
6440 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006441
6442 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00006443 p->cq_off.head = offsetof(struct io_rings, cq.head);
6444 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
6445 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
6446 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
6447 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
6448 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06006449
Jens Axboe044c1ab2019-10-28 09:15:33 -06006450 /*
6451 * Install ring fd as the very last thing, so we don't risk someone
6452 * having closed it before we finish setup
6453 */
6454 ret = io_uring_get_fd(ctx);
6455 if (ret < 0)
6456 goto err;
6457
Jens Axboeda8c9692019-12-02 18:51:26 -07006458 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
Jens Axboeba042912019-12-25 16:33:42 -07006459 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006460 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006461 return ret;
6462err:
6463 io_ring_ctx_wait_and_kill(ctx);
6464 return ret;
6465}
6466
6467/*
6468 * Sets up an aio uring context, and returns the fd. Applications asks for a
6469 * ring size, we return the actual sq/cq ring sizes (among other things) in the
6470 * params structure passed in.
6471 */
6472static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
6473{
6474 struct io_uring_params p;
6475 long ret;
6476 int i;
6477
6478 if (copy_from_user(&p, params, sizeof(p)))
6479 return -EFAULT;
6480 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
6481 if (p.resv[i])
6482 return -EINVAL;
6483 }
6484
Jens Axboe6c271ce2019-01-10 11:22:30 -07006485 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07006486 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
6487 IORING_SETUP_CLAMP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006488 return -EINVAL;
6489
6490 ret = io_uring_create(entries, &p);
6491 if (ret < 0)
6492 return ret;
6493
6494 if (copy_to_user(params, &p, sizeof(p)))
6495 return -EFAULT;
6496
6497 return ret;
6498}
6499
6500SYSCALL_DEFINE2(io_uring_setup, u32, entries,
6501 struct io_uring_params __user *, params)
6502{
6503 return io_uring_setup(entries, params);
6504}
6505
Jens Axboeedafcce2019-01-09 09:16:05 -07006506static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
6507 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06006508 __releases(ctx->uring_lock)
6509 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07006510{
6511 int ret;
6512
Jens Axboe35fa71a2019-04-22 10:23:23 -06006513 /*
6514 * We're inside the ring mutex, if the ref is already dying, then
6515 * someone else killed the ctx or is already going through
6516 * io_uring_register().
6517 */
6518 if (percpu_ref_is_dying(&ctx->refs))
6519 return -ENXIO;
6520
Jens Axboe05f3fb32019-12-09 11:22:50 -07006521 if (opcode != IORING_UNREGISTER_FILES &&
6522 opcode != IORING_REGISTER_FILES_UPDATE) {
6523 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06006524
Jens Axboe05f3fb32019-12-09 11:22:50 -07006525 /*
6526 * Drop uring mutex before waiting for references to exit. If
6527 * another thread is currently inside io_uring_enter() it might
6528 * need to grab the uring_lock to make progress. If we hold it
6529 * here across the drain wait, then we can deadlock. It's safe
6530 * to drop the mutex here, since no new references will come in
6531 * after we've killed the percpu ref.
6532 */
6533 mutex_unlock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07006534 ret = wait_for_completion_interruptible(&ctx->completions[0]);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006535 mutex_lock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07006536 if (ret) {
6537 percpu_ref_resurrect(&ctx->refs);
6538 ret = -EINTR;
6539 goto out;
6540 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07006541 }
Jens Axboeedafcce2019-01-09 09:16:05 -07006542
6543 switch (opcode) {
6544 case IORING_REGISTER_BUFFERS:
6545 ret = io_sqe_buffer_register(ctx, arg, nr_args);
6546 break;
6547 case IORING_UNREGISTER_BUFFERS:
6548 ret = -EINVAL;
6549 if (arg || nr_args)
6550 break;
6551 ret = io_sqe_buffer_unregister(ctx);
6552 break;
Jens Axboe6b063142019-01-10 22:13:58 -07006553 case IORING_REGISTER_FILES:
6554 ret = io_sqe_files_register(ctx, arg, nr_args);
6555 break;
6556 case IORING_UNREGISTER_FILES:
6557 ret = -EINVAL;
6558 if (arg || nr_args)
6559 break;
6560 ret = io_sqe_files_unregister(ctx);
6561 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06006562 case IORING_REGISTER_FILES_UPDATE:
6563 ret = io_sqe_files_update(ctx, arg, nr_args);
6564 break;
Jens Axboe9b402842019-04-11 11:45:41 -06006565 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -07006566 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -06006567 ret = -EINVAL;
6568 if (nr_args != 1)
6569 break;
6570 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -07006571 if (ret)
6572 break;
6573 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
6574 ctx->eventfd_async = 1;
6575 else
6576 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -06006577 break;
6578 case IORING_UNREGISTER_EVENTFD:
6579 ret = -EINVAL;
6580 if (arg || nr_args)
6581 break;
6582 ret = io_eventfd_unregister(ctx);
6583 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07006584 default:
6585 ret = -EINVAL;
6586 break;
6587 }
6588
Jens Axboe05f3fb32019-12-09 11:22:50 -07006589
6590 if (opcode != IORING_UNREGISTER_FILES &&
6591 opcode != IORING_REGISTER_FILES_UPDATE) {
6592 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -07006593 percpu_ref_reinit(&ctx->refs);
Jens Axboec1503682020-01-08 08:26:07 -07006594out:
6595 reinit_completion(&ctx->completions[0]);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006596 }
Jens Axboeedafcce2019-01-09 09:16:05 -07006597 return ret;
6598}
6599
6600SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
6601 void __user *, arg, unsigned int, nr_args)
6602{
6603 struct io_ring_ctx *ctx;
6604 long ret = -EBADF;
6605 struct fd f;
6606
6607 f = fdget(fd);
6608 if (!f.file)
6609 return -EBADF;
6610
6611 ret = -EOPNOTSUPP;
6612 if (f.file->f_op != &io_uring_fops)
6613 goto out_fput;
6614
6615 ctx = f.file->private_data;
6616
6617 mutex_lock(&ctx->uring_lock);
6618 ret = __io_uring_register(ctx, opcode, arg, nr_args);
6619 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006620 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
6621 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07006622out_fput:
6623 fdput(f);
6624 return ret;
6625}
6626
Jens Axboe2b188cc2019-01-07 10:46:33 -07006627static int __init io_uring_init(void)
6628{
Jens Axboed3656342019-12-18 09:50:26 -07006629 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006630 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
6631 return 0;
6632};
6633__initcall(io_uring_init);