blob: 3a57ea98fe3ae92c1491bd0a4f005f3a9d375645 [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 struct filename *filename;
Jens Axboeeddc7ef2019-12-13 21:18:10 -0700396 struct statx __user *buffer;
Jens Axboec12cedf2020-01-08 17:41:21 -0700397 struct open_how how;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700398};
399
Jens Axboe05f3fb32019-12-09 11:22:50 -0700400struct io_files_update {
401 struct file *file;
402 u64 arg;
403 u32 nr_args;
404 u32 offset;
405};
406
Jens Axboe4840e412019-12-25 22:03:45 -0700407struct io_fadvise {
408 struct file *file;
409 u64 offset;
410 u32 len;
411 u32 advice;
412};
413
Jens Axboec1ca7572019-12-25 22:18:28 -0700414struct io_madvise {
415 struct file *file;
416 u64 addr;
417 u32 len;
418 u32 advice;
419};
420
Jens Axboef499a022019-12-02 16:28:46 -0700421struct io_async_connect {
422 struct sockaddr_storage address;
423};
424
Jens Axboe03b12302019-12-02 18:50:25 -0700425struct io_async_msghdr {
426 struct iovec fast_iov[UIO_FASTIOV];
427 struct iovec *iov;
428 struct sockaddr __user *uaddr;
429 struct msghdr msg;
430};
431
Jens Axboef67676d2019-12-02 11:03:47 -0700432struct io_async_rw {
433 struct iovec fast_iov[UIO_FASTIOV];
434 struct iovec *iov;
435 ssize_t nr_segs;
436 ssize_t size;
437};
438
Jens Axboe15b71ab2019-12-11 11:20:36 -0700439struct io_async_open {
440 struct filename *filename;
441};
442
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700443struct io_async_ctx {
Jens Axboef67676d2019-12-02 11:03:47 -0700444 union {
445 struct io_async_rw rw;
Jens Axboe03b12302019-12-02 18:50:25 -0700446 struct io_async_msghdr msg;
Jens Axboef499a022019-12-02 16:28:46 -0700447 struct io_async_connect connect;
Jens Axboe2d283902019-12-04 11:08:05 -0700448 struct io_timeout_data timeout;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700449 struct io_async_open open;
Jens Axboef67676d2019-12-02 11:03:47 -0700450 };
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700451};
452
Jens Axboe09bb8392019-03-13 12:39:28 -0600453/*
454 * NOTE! Each of the iocb union members has the file pointer
455 * as the first entry in their struct definition. So you can
456 * access the file pointer through any of the sub-structs,
457 * or directly as just 'ki_filp' in this struct.
458 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700459struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700460 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600461 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700462 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700463 struct io_poll_iocb poll;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700464 struct io_accept accept;
465 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700466 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700467 struct io_timeout timeout;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700468 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700469 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700470 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700471 struct io_close close;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700472 struct io_files_update files_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700473 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700474 struct io_madvise madvise;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700475 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700476
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700477 struct io_async_ctx *io;
Jens Axboee94f1412019-12-19 12:06:02 -0700478 union {
479 /*
480 * ring_file is only used in the submission path, and
481 * llist_node is only used for poll deferred completions
482 */
483 struct file *ring_file;
484 struct llist_node llist_node;
485 };
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300486 int ring_fd;
487 bool has_user;
488 bool in_async;
489 bool needs_fixed_file;
Jens Axboed625c6e2019-12-17 19:53:05 -0700490 u8 opcode;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700491
492 struct io_ring_ctx *ctx;
Jens Axboeeac406c2019-11-14 12:09:58 -0700493 union {
494 struct list_head list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700495 struct hlist_node hash_node;
Jens Axboeeac406c2019-11-14 12:09:58 -0700496 };
Jens Axboe9e645e112019-05-10 16:07:28 -0600497 struct list_head link_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700498 unsigned int flags;
Jens Axboec16361c2019-01-17 08:39:48 -0700499 refcount_t refs;
Stefan Bühler8449eed2019-04-27 20:34:19 +0200500#define REQ_F_NOWAIT 1 /* must not punt to workers */
Jens Axboedef596e2019-01-09 08:59:42 -0700501#define REQ_F_IOPOLL_COMPLETED 2 /* polled IO has completed */
Jens Axboe6b063142019-01-10 22:13:58 -0700502#define REQ_F_FIXED_FILE 4 /* ctx owns file */
Jens Axboe4d7dd462019-11-20 13:03:52 -0700503#define REQ_F_LINK_NEXT 8 /* already grabbed next link */
Stefan Bühlere2033e32019-05-11 19:08:01 +0200504#define REQ_F_IO_DRAIN 16 /* drain existing IO first */
505#define REQ_F_IO_DRAINED 32 /* drain done */
Jens Axboe9e645e112019-05-10 16:07:28 -0600506#define REQ_F_LINK 64 /* linked sqes */
Jens Axboe2665abf2019-11-05 12:40:47 -0700507#define REQ_F_LINK_TIMEOUT 128 /* has linked timeout */
Zhengyuan Liuf7b76ac2019-07-16 23:26:14 +0800508#define REQ_F_FAIL_LINK 256 /* fail rest of links */
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +0300509#define REQ_F_DRAIN_LINK 512 /* link should be fully drained */
Jens Axboe5262f562019-09-17 12:26:57 -0600510#define REQ_F_TIMEOUT 1024 /* timeout request */
Jens Axboe491381ce2019-10-17 09:20:46 -0600511#define REQ_F_ISREG 2048 /* regular file */
512#define REQ_F_MUST_PUNT 4096 /* must be punted even for NONBLOCK */
Jens Axboe93bd25b2019-11-11 23:34:31 -0700513#define REQ_F_TIMEOUT_NOSEQ 8192 /* no timeout sequence */
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800514#define REQ_F_INFLIGHT 16384 /* on inflight list */
515#define REQ_F_COMP_LOCKED 32768 /* completion under lock */
Jens Axboe4e88d6e2019-12-07 20:59:47 -0700516#define REQ_F_HARDLINK 65536 /* doesn't sever on completion < 0 */
Jens Axboece35a472019-12-17 08:04:44 -0700517#define REQ_F_FORCE_ASYNC 131072 /* IOSQE_ASYNC */
Jens Axboeba042912019-12-25 16:33:42 -0700518#define REQ_F_CUR_POS 262144 /* read/write uses file position */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700519 u64 user_data;
Jens Axboe9e645e112019-05-10 16:07:28 -0600520 u32 result;
Jens Axboede0617e2019-04-06 21:51:27 -0600521 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700522
Jens Axboefcb323c2019-10-24 12:39:47 -0600523 struct list_head inflight_entry;
524
Jens Axboe561fb042019-10-24 07:25:42 -0600525 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700526};
527
528#define IO_PLUG_THRESHOLD 2
Jens Axboedef596e2019-01-09 08:59:42 -0700529#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700530
Jens Axboe9a56a232019-01-09 09:06:50 -0700531struct io_submit_state {
532 struct blk_plug plug;
533
534 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700535 * io_kiocb alloc cache
536 */
537 void *reqs[IO_IOPOLL_BATCH];
538 unsigned int free_reqs;
539 unsigned int cur_req;
540
541 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700542 * File reference cache
543 */
544 struct file *file;
545 unsigned int fd;
546 unsigned int has_refs;
547 unsigned int used_refs;
548 unsigned int ios_left;
549};
550
Jens Axboed3656342019-12-18 09:50:26 -0700551struct io_op_def {
552 /* needs req->io allocated for deferral/async */
553 unsigned async_ctx : 1;
554 /* needs current->mm setup, does mm access */
555 unsigned needs_mm : 1;
556 /* needs req->file assigned */
557 unsigned needs_file : 1;
558 /* needs req->file assigned IFF fd is >= 0 */
559 unsigned fd_non_neg : 1;
560 /* hash wq insertion if file is a regular file */
561 unsigned hash_reg_file : 1;
562 /* unbound wq insertion if file is a non-regular file */
563 unsigned unbound_nonreg_file : 1;
564};
565
566static const struct io_op_def io_op_defs[] = {
567 {
568 /* IORING_OP_NOP */
569 },
570 {
571 /* IORING_OP_READV */
572 .async_ctx = 1,
573 .needs_mm = 1,
574 .needs_file = 1,
575 .unbound_nonreg_file = 1,
576 },
577 {
578 /* IORING_OP_WRITEV */
579 .async_ctx = 1,
580 .needs_mm = 1,
581 .needs_file = 1,
582 .hash_reg_file = 1,
583 .unbound_nonreg_file = 1,
584 },
585 {
586 /* IORING_OP_FSYNC */
587 .needs_file = 1,
588 },
589 {
590 /* IORING_OP_READ_FIXED */
591 .needs_file = 1,
592 .unbound_nonreg_file = 1,
593 },
594 {
595 /* IORING_OP_WRITE_FIXED */
596 .needs_file = 1,
597 .hash_reg_file = 1,
598 .unbound_nonreg_file = 1,
599 },
600 {
601 /* IORING_OP_POLL_ADD */
602 .needs_file = 1,
603 .unbound_nonreg_file = 1,
604 },
605 {
606 /* IORING_OP_POLL_REMOVE */
607 },
608 {
609 /* IORING_OP_SYNC_FILE_RANGE */
610 .needs_file = 1,
611 },
612 {
613 /* IORING_OP_SENDMSG */
614 .async_ctx = 1,
615 .needs_mm = 1,
616 .needs_file = 1,
617 .unbound_nonreg_file = 1,
618 },
619 {
620 /* IORING_OP_RECVMSG */
621 .async_ctx = 1,
622 .needs_mm = 1,
623 .needs_file = 1,
624 .unbound_nonreg_file = 1,
625 },
626 {
627 /* IORING_OP_TIMEOUT */
628 .async_ctx = 1,
629 .needs_mm = 1,
630 },
631 {
632 /* IORING_OP_TIMEOUT_REMOVE */
633 },
634 {
635 /* IORING_OP_ACCEPT */
636 .needs_mm = 1,
637 .needs_file = 1,
638 .unbound_nonreg_file = 1,
639 },
640 {
641 /* IORING_OP_ASYNC_CANCEL */
642 },
643 {
644 /* IORING_OP_LINK_TIMEOUT */
645 .async_ctx = 1,
646 .needs_mm = 1,
647 },
648 {
649 /* IORING_OP_CONNECT */
650 .async_ctx = 1,
651 .needs_mm = 1,
652 .needs_file = 1,
653 .unbound_nonreg_file = 1,
654 },
655 {
656 /* IORING_OP_FALLOCATE */
657 .needs_file = 1,
658 },
659 {
660 /* IORING_OP_OPENAT */
661 .needs_file = 1,
662 .fd_non_neg = 1,
663 },
664 {
665 /* IORING_OP_CLOSE */
666 .needs_file = 1,
667 },
668 {
669 /* IORING_OP_FILES_UPDATE */
670 .needs_mm = 1,
671 },
672 {
673 /* IORING_OP_STATX */
674 .needs_mm = 1,
675 .needs_file = 1,
676 .fd_non_neg = 1,
677 },
Jens Axboe3a6820f2019-12-22 15:19:35 -0700678 {
679 /* IORING_OP_READ */
680 .needs_mm = 1,
681 .needs_file = 1,
682 .unbound_nonreg_file = 1,
683 },
684 {
685 /* IORING_OP_WRITE */
686 .needs_mm = 1,
687 .needs_file = 1,
688 .unbound_nonreg_file = 1,
689 },
Jens Axboe4840e412019-12-25 22:03:45 -0700690 {
691 /* IORING_OP_FADVISE */
692 .needs_file = 1,
693 },
Jens Axboec1ca7572019-12-25 22:18:28 -0700694 {
695 /* IORING_OP_MADVISE */
696 .needs_mm = 1,
697 },
Jens Axboefddafac2020-01-04 20:19:44 -0700698 {
699 /* IORING_OP_SEND */
700 .needs_mm = 1,
701 .needs_file = 1,
702 .unbound_nonreg_file = 1,
703 },
704 {
705 /* IORING_OP_RECV */
706 .needs_mm = 1,
707 .needs_file = 1,
708 .unbound_nonreg_file = 1,
709 },
Jens Axboed3656342019-12-18 09:50:26 -0700710};
711
Jens Axboe561fb042019-10-24 07:25:42 -0600712static void io_wq_submit_work(struct io_wq_work **workptr);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700713static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800714static void io_put_req(struct io_kiocb *req);
Jens Axboe978db572019-11-14 22:39:04 -0700715static void __io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700716static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
717static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -0700718static int __io_sqe_files_update(struct io_ring_ctx *ctx,
719 struct io_uring_files_update *ip,
720 unsigned nr_args);
Jens Axboede0617e2019-04-06 21:51:27 -0600721
Jens Axboe2b188cc2019-01-07 10:46:33 -0700722static struct kmem_cache *req_cachep;
723
724static const struct file_operations io_uring_fops;
725
726struct sock *io_uring_get_socket(struct file *file)
727{
728#if defined(CONFIG_UNIX)
729 if (file->f_op == &io_uring_fops) {
730 struct io_ring_ctx *ctx = file->private_data;
731
732 return ctx->ring_sock->sk;
733 }
734#endif
735 return NULL;
736}
737EXPORT_SYMBOL(io_uring_get_socket);
738
739static void io_ring_ctx_ref_free(struct percpu_ref *ref)
740{
741 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
742
Jens Axboe206aefd2019-11-07 18:27:42 -0700743 complete(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700744}
745
746static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
747{
748 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -0700749 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700750
751 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
752 if (!ctx)
753 return NULL;
754
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700755 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
756 if (!ctx->fallback_req)
757 goto err;
758
Jens Axboe206aefd2019-11-07 18:27:42 -0700759 ctx->completions = kmalloc(2 * sizeof(struct completion), GFP_KERNEL);
760 if (!ctx->completions)
761 goto err;
762
Jens Axboe78076bb2019-12-04 19:56:40 -0700763 /*
764 * Use 5 bits less than the max cq entries, that should give us around
765 * 32 entries per hash list if totally full and uniformly spread.
766 */
767 hash_bits = ilog2(p->cq_entries);
768 hash_bits -= 5;
769 if (hash_bits <= 0)
770 hash_bits = 1;
771 ctx->cancel_hash_bits = hash_bits;
772 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
773 GFP_KERNEL);
774 if (!ctx->cancel_hash)
775 goto err;
776 __hash_init(ctx->cancel_hash, 1U << hash_bits);
777
Roman Gushchin21482892019-05-07 10:01:48 -0700778 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -0700779 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
780 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700781
782 ctx->flags = p->flags;
783 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700784 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe206aefd2019-11-07 18:27:42 -0700785 init_completion(&ctx->completions[0]);
786 init_completion(&ctx->completions[1]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700787 mutex_init(&ctx->uring_lock);
788 init_waitqueue_head(&ctx->wait);
789 spin_lock_init(&ctx->completion_lock);
Jens Axboee94f1412019-12-19 12:06:02 -0700790 init_llist_head(&ctx->poll_llist);
Jens Axboedef596e2019-01-09 08:59:42 -0700791 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboede0617e2019-04-06 21:51:27 -0600792 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -0600793 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -0600794 init_waitqueue_head(&ctx->inflight_wait);
795 spin_lock_init(&ctx->inflight_lock);
796 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700797 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -0700798err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700799 if (ctx->fallback_req)
800 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe206aefd2019-11-07 18:27:42 -0700801 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -0700802 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -0700803 kfree(ctx);
804 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700805}
806
Bob Liu9d858b22019-11-13 18:06:25 +0800807static inline bool __req_need_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -0600808{
Jackie Liua197f662019-11-08 08:09:12 -0700809 struct io_ring_ctx *ctx = req->ctx;
810
Jens Axboe498ccd92019-10-25 10:04:25 -0600811 return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped
812 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboede0617e2019-04-06 21:51:27 -0600813}
814
Bob Liu9d858b22019-11-13 18:06:25 +0800815static inline bool req_need_defer(struct io_kiocb *req)
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600816{
Bob Liu9d858b22019-11-13 18:06:25 +0800817 if ((req->flags & (REQ_F_IO_DRAIN|REQ_F_IO_DRAINED)) == REQ_F_IO_DRAIN)
818 return __req_need_defer(req);
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600819
Bob Liu9d858b22019-11-13 18:06:25 +0800820 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600821}
822
823static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -0600824{
825 struct io_kiocb *req;
826
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600827 req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list);
Bob Liu9d858b22019-11-13 18:06:25 +0800828 if (req && !req_need_defer(req)) {
Jens Axboede0617e2019-04-06 21:51:27 -0600829 list_del_init(&req->list);
830 return req;
831 }
832
833 return NULL;
834}
835
Jens Axboe5262f562019-09-17 12:26:57 -0600836static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx)
837{
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600838 struct io_kiocb *req;
839
840 req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list);
Jens Axboe93bd25b2019-11-11 23:34:31 -0700841 if (req) {
842 if (req->flags & REQ_F_TIMEOUT_NOSEQ)
843 return NULL;
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800844 if (!__req_need_defer(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -0700845 list_del_init(&req->list);
846 return req;
847 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600848 }
849
850 return NULL;
Jens Axboe5262f562019-09-17 12:26:57 -0600851}
852
Jens Axboede0617e2019-04-06 21:51:27 -0600853static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700854{
Hristo Venev75b28af2019-08-26 17:23:46 +0000855 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700856
Hristo Venev75b28af2019-08-26 17:23:46 +0000857 if (ctx->cached_cq_tail != READ_ONCE(rings->cq.tail)) {
Jens Axboe2b188cc2019-01-07 10:46:33 -0700858 /* order cqe stores with ring update */
Hristo Venev75b28af2019-08-26 17:23:46 +0000859 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700860
Jens Axboe2b188cc2019-01-07 10:46:33 -0700861 if (wq_has_sleeper(&ctx->cq_wait)) {
862 wake_up_interruptible(&ctx->cq_wait);
863 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
864 }
865 }
866}
867
Jens Axboe94ae5e72019-11-14 19:39:52 -0700868static inline bool io_prep_async_work(struct io_kiocb *req,
869 struct io_kiocb **link)
Jens Axboe561fb042019-10-24 07:25:42 -0600870{
Jens Axboed3656342019-12-18 09:50:26 -0700871 const struct io_op_def *def = &io_op_defs[req->opcode];
Jens Axboe561fb042019-10-24 07:25:42 -0600872 bool do_hashed = false;
Jens Axboe54a91f32019-09-10 09:15:04 -0600873
Jens Axboed3656342019-12-18 09:50:26 -0700874 if (req->flags & REQ_F_ISREG) {
875 if (def->hash_reg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -0700876 do_hashed = true;
Jens Axboed3656342019-12-18 09:50:26 -0700877 } else {
878 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -0700879 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -0600880 }
Jens Axboed3656342019-12-18 09:50:26 -0700881 if (def->needs_mm)
Jens Axboe3529d8c2019-12-19 18:24:38 -0700882 req->work.flags |= IO_WQ_WORK_NEEDS_USER;
Jens Axboe54a91f32019-09-10 09:15:04 -0600883
Jens Axboe94ae5e72019-11-14 19:39:52 -0700884 *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -0600885 return do_hashed;
886}
887
Jackie Liua197f662019-11-08 08:09:12 -0700888static inline void io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -0600889{
Jackie Liua197f662019-11-08 08:09:12 -0700890 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe94ae5e72019-11-14 19:39:52 -0700891 struct io_kiocb *link;
892 bool do_hashed;
893
894 do_hashed = io_prep_async_work(req, &link);
Jens Axboe561fb042019-10-24 07:25:42 -0600895
896 trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work,
897 req->flags);
898 if (!do_hashed) {
899 io_wq_enqueue(ctx->io_wq, &req->work);
900 } else {
901 io_wq_enqueue_hashed(ctx->io_wq, &req->work,
902 file_inode(req->file));
903 }
Jens Axboe94ae5e72019-11-14 19:39:52 -0700904
905 if (link)
906 io_queue_linked_timeout(link);
Jens Axboe18d9be12019-09-10 09:13:05 -0600907}
908
Jens Axboe5262f562019-09-17 12:26:57 -0600909static void io_kill_timeout(struct io_kiocb *req)
910{
911 int ret;
912
Jens Axboe2d283902019-12-04 11:08:05 -0700913 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe5262f562019-09-17 12:26:57 -0600914 if (ret != -1) {
915 atomic_inc(&req->ctx->cq_timeouts);
Jens Axboe842f9612019-10-29 12:34:10 -0600916 list_del_init(&req->list);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700917 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800918 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -0600919 }
920}
921
922static void io_kill_timeouts(struct io_ring_ctx *ctx)
923{
924 struct io_kiocb *req, *tmp;
925
926 spin_lock_irq(&ctx->completion_lock);
927 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
928 io_kill_timeout(req);
929 spin_unlock_irq(&ctx->completion_lock);
930}
931
Jens Axboede0617e2019-04-06 21:51:27 -0600932static void io_commit_cqring(struct io_ring_ctx *ctx)
933{
934 struct io_kiocb *req;
935
Jens Axboe5262f562019-09-17 12:26:57 -0600936 while ((req = io_get_timeout_req(ctx)) != NULL)
937 io_kill_timeout(req);
938
Jens Axboede0617e2019-04-06 21:51:27 -0600939 __io_commit_cqring(ctx);
940
941 while ((req = io_get_deferred_req(ctx)) != NULL) {
942 req->flags |= REQ_F_IO_DRAINED;
Jackie Liua197f662019-11-08 08:09:12 -0700943 io_queue_async_work(req);
Jens Axboede0617e2019-04-06 21:51:27 -0600944 }
945}
946
Jens Axboe2b188cc2019-01-07 10:46:33 -0700947static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
948{
Hristo Venev75b28af2019-08-26 17:23:46 +0000949 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700950 unsigned tail;
951
952 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +0200953 /*
954 * writes to the cq entry need to come after reading head; the
955 * control dependency is enough as we're using WRITE_ONCE to
956 * fill the cq entry
957 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000958 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700959 return NULL;
960
961 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +0000962 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -0700963}
964
Jens Axboef2842ab2020-01-08 11:04:00 -0700965static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
966{
967 if (!ctx->eventfd_async)
968 return true;
969 return io_wq_current_is_worker() || in_interrupt();
970}
971
Jens Axboe8c838782019-03-12 15:48:16 -0600972static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
973{
974 if (waitqueue_active(&ctx->wait))
975 wake_up(&ctx->wait);
976 if (waitqueue_active(&ctx->sqo_wait))
977 wake_up(&ctx->sqo_wait);
Jens Axboef2842ab2020-01-08 11:04:00 -0700978 if (ctx->cq_ev_fd && io_should_trigger_evfd(ctx))
Jens Axboe9b402842019-04-11 11:45:41 -0600979 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -0600980}
981
Jens Axboec4a2ed72019-11-21 21:01:26 -0700982/* Returns true if there are no backlogged entries after the flush */
983static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700984{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700985 struct io_rings *rings = ctx->rings;
986 struct io_uring_cqe *cqe;
987 struct io_kiocb *req;
988 unsigned long flags;
989 LIST_HEAD(list);
990
991 if (!force) {
992 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -0700993 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700994 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
995 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -0700996 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700997 }
998
999 spin_lock_irqsave(&ctx->completion_lock, flags);
1000
1001 /* if force is set, the ring is going away. always drop after that */
1002 if (force)
Jens Axboe69b3e542020-01-08 11:01:46 -07001003 ctx->cq_overflow_flushed = 1;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001004
Jens Axboec4a2ed72019-11-21 21:01:26 -07001005 cqe = NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001006 while (!list_empty(&ctx->cq_overflow_list)) {
1007 cqe = io_get_cqring(ctx);
1008 if (!cqe && !force)
1009 break;
1010
1011 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
1012 list);
1013 list_move(&req->list, &list);
1014 if (cqe) {
1015 WRITE_ONCE(cqe->user_data, req->user_data);
1016 WRITE_ONCE(cqe->res, req->result);
1017 WRITE_ONCE(cqe->flags, 0);
1018 } else {
1019 WRITE_ONCE(ctx->rings->cq_overflow,
1020 atomic_inc_return(&ctx->cached_cq_overflow));
1021 }
1022 }
1023
1024 io_commit_cqring(ctx);
Jens Axboead3eb2c2019-12-18 17:12:20 -07001025 if (cqe) {
1026 clear_bit(0, &ctx->sq_check_overflow);
1027 clear_bit(0, &ctx->cq_check_overflow);
1028 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001029 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1030 io_cqring_ev_posted(ctx);
1031
1032 while (!list_empty(&list)) {
1033 req = list_first_entry(&list, struct io_kiocb, list);
1034 list_del(&req->list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001035 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001036 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001037
1038 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001039}
1040
Jens Axboe78e19bb2019-11-06 15:21:34 -07001041static void io_cqring_fill_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001042{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001043 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001044 struct io_uring_cqe *cqe;
1045
Jens Axboe78e19bb2019-11-06 15:21:34 -07001046 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001047
Jens Axboe2b188cc2019-01-07 10:46:33 -07001048 /*
1049 * If we can't get a cq entry, userspace overflowed the
1050 * submission (by quite a lot). Increment the overflow count in
1051 * the ring.
1052 */
1053 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001054 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001055 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001056 WRITE_ONCE(cqe->res, res);
1057 WRITE_ONCE(cqe->flags, 0);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001058 } else if (ctx->cq_overflow_flushed) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001059 WRITE_ONCE(ctx->rings->cq_overflow,
1060 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001061 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001062 if (list_empty(&ctx->cq_overflow_list)) {
1063 set_bit(0, &ctx->sq_check_overflow);
1064 set_bit(0, &ctx->cq_check_overflow);
1065 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001066 refcount_inc(&req->refs);
1067 req->result = res;
1068 list_add_tail(&req->list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001069 }
1070}
1071
Jens Axboe78e19bb2019-11-06 15:21:34 -07001072static void io_cqring_add_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001073{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001074 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001075 unsigned long flags;
1076
1077 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001078 io_cqring_fill_event(req, res);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001079 io_commit_cqring(ctx);
1080 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1081
Jens Axboe8c838782019-03-12 15:48:16 -06001082 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001083}
1084
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001085static inline bool io_is_fallback_req(struct io_kiocb *req)
1086{
1087 return req == (struct io_kiocb *)
1088 ((unsigned long) req->ctx->fallback_req & ~1UL);
1089}
1090
1091static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
1092{
1093 struct io_kiocb *req;
1094
1095 req = ctx->fallback_req;
1096 if (!test_and_set_bit_lock(0, (unsigned long *) ctx->fallback_req))
1097 return req;
1098
1099 return NULL;
1100}
1101
Jens Axboe2579f912019-01-09 09:10:43 -07001102static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
1103 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001104{
Jens Axboefd6fab22019-03-14 16:30:06 -06001105 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001106 struct io_kiocb *req;
1107
Jens Axboe2579f912019-01-09 09:10:43 -07001108 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -06001109 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -07001110 if (unlikely(!req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001111 goto fallback;
Jens Axboe2579f912019-01-09 09:10:43 -07001112 } else if (!state->free_reqs) {
1113 size_t sz;
1114 int ret;
1115
1116 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -06001117 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
1118
1119 /*
1120 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1121 * retry single alloc to be on the safe side.
1122 */
1123 if (unlikely(ret <= 0)) {
1124 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1125 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001126 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -06001127 ret = 1;
1128 }
Jens Axboe2579f912019-01-09 09:10:43 -07001129 state->free_reqs = ret - 1;
1130 state->cur_req = 1;
1131 req = state->reqs[0];
1132 } else {
1133 req = state->reqs[state->cur_req];
1134 state->free_reqs--;
1135 state->cur_req++;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001136 }
1137
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001138got_it:
Jens Axboe1a6b74f2019-12-02 10:33:15 -07001139 req->io = NULL;
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001140 req->ring_file = NULL;
Jens Axboe60c112b2019-06-21 10:20:18 -06001141 req->file = NULL;
Jens Axboe2579f912019-01-09 09:10:43 -07001142 req->ctx = ctx;
1143 req->flags = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06001144 /* one is dropped after submission, the other at completion */
1145 refcount_set(&req->refs, 2);
Jens Axboe9e645e112019-05-10 16:07:28 -06001146 req->result = 0;
Jens Axboe561fb042019-10-24 07:25:42 -06001147 INIT_IO_WORK(&req->work, io_wq_submit_work);
Jens Axboe2579f912019-01-09 09:10:43 -07001148 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001149fallback:
1150 req = io_get_fallback_req(ctx);
1151 if (req)
1152 goto got_it;
Pavel Begunkov6805b322019-10-08 02:18:42 +03001153 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001154 return NULL;
1155}
1156
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001157static void __io_req_do_free(struct io_kiocb *req)
1158{
1159 if (likely(!io_is_fallback_req(req)))
1160 kmem_cache_free(req_cachep, req);
1161 else
1162 clear_bit_unlock(0, (unsigned long *) req->ctx->fallback_req);
1163}
1164
Jens Axboec6ca97b302019-12-28 12:11:08 -07001165static void __io_req_aux_free(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001166{
Jens Axboefcb323c2019-10-24 12:39:47 -06001167 struct io_ring_ctx *ctx = req->ctx;
1168
YueHaibing96fd84d2020-01-07 22:22:44 +08001169 kfree(req->io);
Jens Axboe05f3fb32019-12-09 11:22:50 -07001170 if (req->file) {
1171 if (req->flags & REQ_F_FIXED_FILE)
1172 percpu_ref_put(&ctx->file_data->refs);
1173 else
1174 fput(req->file);
1175 }
Jens Axboec6ca97b302019-12-28 12:11:08 -07001176}
1177
1178static void __io_free_req(struct io_kiocb *req)
1179{
1180 __io_req_aux_free(req);
1181
Jens Axboefcb323c2019-10-24 12:39:47 -06001182 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001183 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06001184 unsigned long flags;
1185
1186 spin_lock_irqsave(&ctx->inflight_lock, flags);
1187 list_del(&req->inflight_entry);
1188 if (waitqueue_active(&ctx->inflight_wait))
1189 wake_up(&ctx->inflight_wait);
1190 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1191 }
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001192
1193 percpu_ref_put(&req->ctx->refs);
1194 __io_req_do_free(req);
Jens Axboee65ef562019-03-12 10:16:44 -06001195}
1196
Jens Axboec6ca97b302019-12-28 12:11:08 -07001197struct req_batch {
1198 void *reqs[IO_IOPOLL_BATCH];
1199 int to_free;
1200 int need_iter;
1201};
1202
1203static void io_free_req_many(struct io_ring_ctx *ctx, struct req_batch *rb)
1204{
1205 if (!rb->to_free)
1206 return;
1207 if (rb->need_iter) {
1208 int i, inflight = 0;
1209 unsigned long flags;
1210
1211 for (i = 0; i < rb->to_free; i++) {
1212 struct io_kiocb *req = rb->reqs[i];
1213
1214 if (req->flags & REQ_F_FIXED_FILE)
1215 req->file = NULL;
1216 if (req->flags & REQ_F_INFLIGHT)
1217 inflight++;
1218 else
1219 rb->reqs[i] = NULL;
1220 __io_req_aux_free(req);
1221 }
1222 if (!inflight)
1223 goto do_free;
1224
1225 spin_lock_irqsave(&ctx->inflight_lock, flags);
1226 for (i = 0; i < rb->to_free; i++) {
1227 struct io_kiocb *req = rb->reqs[i];
1228
1229 if (req) {
1230 list_del(&req->inflight_entry);
1231 if (!--inflight)
1232 break;
1233 }
1234 }
1235 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1236
1237 if (waitqueue_active(&ctx->inflight_wait))
1238 wake_up(&ctx->inflight_wait);
1239 }
1240do_free:
1241 kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs);
1242 percpu_ref_put_many(&ctx->refs, rb->to_free);
1243 percpu_ref_put_many(&ctx->file_data->refs, rb->to_free);
1244 rb->to_free = rb->need_iter = 0;
1245}
1246
Jackie Liua197f662019-11-08 08:09:12 -07001247static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001248{
Jackie Liua197f662019-11-08 08:09:12 -07001249 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001250 int ret;
1251
Jens Axboe2d283902019-12-04 11:08:05 -07001252 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe2665abf2019-11-05 12:40:47 -07001253 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001254 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -07001255 io_commit_cqring(ctx);
1256 req->flags &= ~REQ_F_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001257 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001258 return true;
1259 }
1260
1261 return false;
1262}
1263
Jens Axboeba816ad2019-09-28 11:36:45 -06001264static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -06001265{
Jens Axboe2665abf2019-11-05 12:40:47 -07001266 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001267 bool wake_ev = false;
Jens Axboe9e645e112019-05-10 16:07:28 -06001268
Jens Axboe4d7dd462019-11-20 13:03:52 -07001269 /* Already got next link */
1270 if (req->flags & REQ_F_LINK_NEXT)
1271 return;
1272
Jens Axboe9e645e112019-05-10 16:07:28 -06001273 /*
1274 * The list should never be empty when we are called here. But could
1275 * potentially happen if the chain is messed up, check to be on the
1276 * safe side.
1277 */
Pavel Begunkov44932332019-12-05 16:16:35 +03001278 while (!list_empty(&req->link_list)) {
1279 struct io_kiocb *nxt = list_first_entry(&req->link_list,
1280 struct io_kiocb, link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001281
Pavel Begunkov44932332019-12-05 16:16:35 +03001282 if (unlikely((req->flags & REQ_F_LINK_TIMEOUT) &&
1283 (nxt->flags & REQ_F_TIMEOUT))) {
1284 list_del_init(&nxt->link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001285 wake_ev |= io_link_cancel_timeout(nxt);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001286 req->flags &= ~REQ_F_LINK_TIMEOUT;
1287 continue;
1288 }
Jens Axboe9e645e112019-05-10 16:07:28 -06001289
Pavel Begunkov44932332019-12-05 16:16:35 +03001290 list_del_init(&req->link_list);
1291 if (!list_empty(&nxt->link_list))
1292 nxt->flags |= REQ_F_LINK;
Pavel Begunkovb18fdf72019-11-21 23:21:02 +03001293 *nxtptr = nxt;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001294 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06001295 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001296
Jens Axboe4d7dd462019-11-20 13:03:52 -07001297 req->flags |= REQ_F_LINK_NEXT;
Jens Axboe2665abf2019-11-05 12:40:47 -07001298 if (wake_ev)
1299 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001300}
1301
1302/*
1303 * Called if REQ_F_LINK is set, and we fail the head request
1304 */
1305static void io_fail_links(struct io_kiocb *req)
1306{
Jens Axboe2665abf2019-11-05 12:40:47 -07001307 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001308 unsigned long flags;
1309
1310 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06001311
1312 while (!list_empty(&req->link_list)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03001313 struct io_kiocb *link = list_first_entry(&req->link_list,
1314 struct io_kiocb, link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06001315
Pavel Begunkov44932332019-12-05 16:16:35 +03001316 list_del_init(&link->link_list);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001317 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001318
1319 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
Jens Axboed625c6e2019-12-17 19:53:05 -07001320 link->opcode == IORING_OP_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -07001321 io_link_cancel_timeout(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001322 } else {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001323 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe978db572019-11-14 22:39:04 -07001324 __io_double_put_req(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001325 }
Jens Axboe5d960722019-11-19 15:31:28 -07001326 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -06001327 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001328
1329 io_commit_cqring(ctx);
1330 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1331 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001332}
1333
Jens Axboe4d7dd462019-11-20 13:03:52 -07001334static void io_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -06001335{
Jens Axboe4d7dd462019-11-20 13:03:52 -07001336 if (likely(!(req->flags & REQ_F_LINK)))
Jens Axboe2665abf2019-11-05 12:40:47 -07001337 return;
Jens Axboe2665abf2019-11-05 12:40:47 -07001338
Jens Axboe9e645e112019-05-10 16:07:28 -06001339 /*
1340 * If LINK is set, we have dependent requests in this chain. If we
1341 * didn't fail this request, queue the first one up, moving any other
1342 * dependencies to the next request. In case of failure, fail the rest
1343 * of the chain.
1344 */
Jens Axboe2665abf2019-11-05 12:40:47 -07001345 if (req->flags & REQ_F_FAIL_LINK) {
1346 io_fail_links(req);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001347 } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) ==
1348 REQ_F_LINK_TIMEOUT) {
Jens Axboe2665abf2019-11-05 12:40:47 -07001349 struct io_ring_ctx *ctx = req->ctx;
1350 unsigned long flags;
1351
1352 /*
1353 * If this is a timeout link, we could be racing with the
1354 * timeout timer. Grab the completion lock for this case to
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001355 * protect against that.
Jens Axboe2665abf2019-11-05 12:40:47 -07001356 */
1357 spin_lock_irqsave(&ctx->completion_lock, flags);
1358 io_req_link_next(req, nxt);
1359 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1360 } else {
1361 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -06001362 }
Jens Axboe4d7dd462019-11-20 13:03:52 -07001363}
Jens Axboe9e645e112019-05-10 16:07:28 -06001364
Jackie Liuc69f8db2019-11-09 11:00:08 +08001365static void io_free_req(struct io_kiocb *req)
1366{
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001367 struct io_kiocb *nxt = NULL;
1368
1369 io_req_find_next(req, &nxt);
Pavel Begunkov70cf9f32019-11-21 23:21:00 +03001370 __io_free_req(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001371
1372 if (nxt)
1373 io_queue_async_work(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08001374}
1375
Jens Axboeba816ad2019-09-28 11:36:45 -06001376/*
1377 * Drop reference to request, return next in chain (if there is one) if this
1378 * was the last reference to this request.
1379 */
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001380__attribute__((nonnull))
Jackie Liuec9c02a2019-11-08 23:50:36 +08001381static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboee65ef562019-03-12 10:16:44 -06001382{
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001383 io_req_find_next(req, nxtptr);
Jens Axboe4d7dd462019-11-20 13:03:52 -07001384
Jens Axboee65ef562019-03-12 10:16:44 -06001385 if (refcount_dec_and_test(&req->refs))
Jens Axboe4d7dd462019-11-20 13:03:52 -07001386 __io_free_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001387}
1388
Jens Axboe2b188cc2019-01-07 10:46:33 -07001389static void io_put_req(struct io_kiocb *req)
1390{
Jens Axboedef596e2019-01-09 08:59:42 -07001391 if (refcount_dec_and_test(&req->refs))
1392 io_free_req(req);
1393}
1394
Jens Axboe978db572019-11-14 22:39:04 -07001395/*
1396 * Must only be used if we don't need to care about links, usually from
1397 * within the completion handling itself.
1398 */
1399static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06001400{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001401 /* drop both submit and complete references */
1402 if (refcount_sub_and_test(2, &req->refs))
1403 __io_free_req(req);
1404}
1405
Jens Axboe978db572019-11-14 22:39:04 -07001406static void io_double_put_req(struct io_kiocb *req)
1407{
1408 /* drop both submit and complete references */
1409 if (refcount_sub_and_test(2, &req->refs))
1410 io_free_req(req);
1411}
1412
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001413static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06001414{
Jens Axboe84f97dc2019-11-06 11:27:53 -07001415 struct io_rings *rings = ctx->rings;
1416
Jens Axboead3eb2c2019-12-18 17:12:20 -07001417 if (test_bit(0, &ctx->cq_check_overflow)) {
1418 /*
1419 * noflush == true is from the waitqueue handler, just ensure
1420 * we wake up the task, and the next invocation will flush the
1421 * entries. We cannot safely to it from here.
1422 */
1423 if (noflush && !list_empty(&ctx->cq_overflow_list))
1424 return -1U;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001425
Jens Axboead3eb2c2019-12-18 17:12:20 -07001426 io_cqring_overflow_flush(ctx, false);
1427 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001428
Jens Axboea3a0e432019-08-20 11:03:11 -06001429 /* See comment at the top of this file */
1430 smp_rmb();
Jens Axboead3eb2c2019-12-18 17:12:20 -07001431 return ctx->cached_cq_tail - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06001432}
1433
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03001434static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1435{
1436 struct io_rings *rings = ctx->rings;
1437
1438 /* make sure SQ entry isn't read before tail */
1439 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1440}
1441
Jens Axboe8237e042019-12-28 10:48:22 -07001442static inline bool io_req_multi_free(struct req_batch *rb, struct io_kiocb *req)
Jens Axboee94f1412019-12-19 12:06:02 -07001443{
Jens Axboec6ca97b302019-12-28 12:11:08 -07001444 if ((req->flags & REQ_F_LINK) || io_is_fallback_req(req))
1445 return false;
Jens Axboee94f1412019-12-19 12:06:02 -07001446
Jens Axboec6ca97b302019-12-28 12:11:08 -07001447 if (!(req->flags & REQ_F_FIXED_FILE) || req->io)
1448 rb->need_iter++;
1449
1450 rb->reqs[rb->to_free++] = req;
1451 if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
1452 io_free_req_many(req->ctx, rb);
1453 return true;
Jens Axboee94f1412019-12-19 12:06:02 -07001454}
1455
Jens Axboedef596e2019-01-09 08:59:42 -07001456/*
1457 * Find and free completed poll iocbs
1458 */
1459static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
1460 struct list_head *done)
1461{
Jens Axboe8237e042019-12-28 10:48:22 -07001462 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07001463 struct io_kiocb *req;
Jens Axboedef596e2019-01-09 08:59:42 -07001464
Jens Axboec6ca97b302019-12-28 12:11:08 -07001465 rb.to_free = rb.need_iter = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001466 while (!list_empty(done)) {
1467 req = list_first_entry(done, struct io_kiocb, list);
1468 list_del(&req->list);
1469
Jens Axboe78e19bb2019-11-06 15:21:34 -07001470 io_cqring_fill_event(req, req->result);
Jens Axboedef596e2019-01-09 08:59:42 -07001471 (*nr_events)++;
1472
Jens Axboe8237e042019-12-28 10:48:22 -07001473 if (refcount_dec_and_test(&req->refs) &&
1474 !io_req_multi_free(&rb, req))
1475 io_free_req(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001476 }
Jens Axboedef596e2019-01-09 08:59:42 -07001477
Jens Axboe09bb8392019-03-13 12:39:28 -06001478 io_commit_cqring(ctx);
Jens Axboe8237e042019-12-28 10:48:22 -07001479 io_free_req_many(ctx, &rb);
Jens Axboedef596e2019-01-09 08:59:42 -07001480}
1481
1482static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1483 long min)
1484{
1485 struct io_kiocb *req, *tmp;
1486 LIST_HEAD(done);
1487 bool spin;
1488 int ret;
1489
1490 /*
1491 * Only spin for completions if we don't have multiple devices hanging
1492 * off our complete list, and we're under the requested amount.
1493 */
1494 spin = !ctx->poll_multi_file && *nr_events < min;
1495
1496 ret = 0;
1497 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
Jens Axboe9adbd452019-12-20 08:45:55 -07001498 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07001499
1500 /*
1501 * Move completed entries to our local list. If we find a
1502 * request that requires polling, break out and complete
1503 * the done list first, if we have entries there.
1504 */
1505 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
1506 list_move_tail(&req->list, &done);
1507 continue;
1508 }
1509 if (!list_empty(&done))
1510 break;
1511
1512 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1513 if (ret < 0)
1514 break;
1515
1516 if (ret && spin)
1517 spin = false;
1518 ret = 0;
1519 }
1520
1521 if (!list_empty(&done))
1522 io_iopoll_complete(ctx, nr_events, &done);
1523
1524 return ret;
1525}
1526
1527/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08001528 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07001529 * non-spinning poll check - we'll still enter the driver poll loop, but only
1530 * as a non-spinning completion check.
1531 */
1532static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
1533 long min)
1534{
Jens Axboe08f54392019-08-21 22:19:11 -06001535 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07001536 int ret;
1537
1538 ret = io_do_iopoll(ctx, nr_events, min);
1539 if (ret < 0)
1540 return ret;
1541 if (!min || *nr_events >= min)
1542 return 0;
1543 }
1544
1545 return 1;
1546}
1547
1548/*
1549 * We can't just wait for polled events to come to us, we have to actively
1550 * find and complete them.
1551 */
1552static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
1553{
1554 if (!(ctx->flags & IORING_SETUP_IOPOLL))
1555 return;
1556
1557 mutex_lock(&ctx->uring_lock);
1558 while (!list_empty(&ctx->poll_list)) {
1559 unsigned int nr_events = 0;
1560
1561 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -06001562
1563 /*
1564 * Ensure we allow local-to-the-cpu processing to take place,
1565 * in this case we need to ensure that we reap all events.
1566 */
1567 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -07001568 }
1569 mutex_unlock(&ctx->uring_lock);
1570}
1571
Jens Axboe2b2ed972019-10-25 10:06:15 -06001572static int __io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1573 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07001574{
Jens Axboe2b2ed972019-10-25 10:06:15 -06001575 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001576
1577 do {
1578 int tmin = 0;
1579
Jens Axboe500f9fb2019-08-19 12:15:59 -06001580 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06001581 * Don't enter poll loop if we already have events pending.
1582 * If we do, we can potentially be spinning for commands that
1583 * already triggered a CQE (eg in error).
1584 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001585 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06001586 break;
1587
1588 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06001589 * If a submit got punted to a workqueue, we can have the
1590 * application entering polling for a command before it gets
1591 * issued. That app will hold the uring_lock for the duration
1592 * of the poll right here, so we need to take a breather every
1593 * now and then to ensure that the issue has a chance to add
1594 * the poll to the issued list. Otherwise we can spin here
1595 * forever, while the workqueue is stuck trying to acquire the
1596 * very same mutex.
1597 */
1598 if (!(++iters & 7)) {
1599 mutex_unlock(&ctx->uring_lock);
1600 mutex_lock(&ctx->uring_lock);
1601 }
1602
Jens Axboedef596e2019-01-09 08:59:42 -07001603 if (*nr_events < min)
1604 tmin = min - *nr_events;
1605
1606 ret = io_iopoll_getevents(ctx, nr_events, tmin);
1607 if (ret <= 0)
1608 break;
1609 ret = 0;
1610 } while (min && !*nr_events && !need_resched());
1611
Jens Axboe2b2ed972019-10-25 10:06:15 -06001612 return ret;
1613}
1614
1615static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1616 long min)
1617{
1618 int ret;
1619
1620 /*
1621 * We disallow the app entering submit/complete with polling, but we
1622 * still need to lock the ring to prevent racing with polled issue
1623 * that got punted to a workqueue.
1624 */
1625 mutex_lock(&ctx->uring_lock);
1626 ret = __io_iopoll_check(ctx, nr_events, min);
Jens Axboe500f9fb2019-08-19 12:15:59 -06001627 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001628 return ret;
1629}
1630
Jens Axboe491381ce2019-10-17 09:20:46 -06001631static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001632{
Jens Axboe491381ce2019-10-17 09:20:46 -06001633 /*
1634 * Tell lockdep we inherited freeze protection from submission
1635 * thread.
1636 */
1637 if (req->flags & REQ_F_ISREG) {
1638 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001639
Jens Axboe491381ce2019-10-17 09:20:46 -06001640 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001641 }
Jens Axboe491381ce2019-10-17 09:20:46 -06001642 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001643}
1644
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001645static inline void req_set_fail_links(struct io_kiocb *req)
1646{
1647 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1648 req->flags |= REQ_F_FAIL_LINK;
1649}
1650
Jens Axboeba816ad2019-09-28 11:36:45 -06001651static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001652{
Jens Axboe9adbd452019-12-20 08:45:55 -07001653 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001654
Jens Axboe491381ce2019-10-17 09:20:46 -06001655 if (kiocb->ki_flags & IOCB_WRITE)
1656 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001657
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001658 if (res != req->result)
1659 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001660 io_cqring_add_event(req, res);
Jens Axboeba816ad2019-09-28 11:36:45 -06001661}
1662
1663static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1664{
Jens Axboe9adbd452019-12-20 08:45:55 -07001665 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06001666
1667 io_complete_rw_common(kiocb, res);
Jens Axboee65ef562019-03-12 10:16:44 -06001668 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001669}
1670
Jens Axboeba816ad2019-09-28 11:36:45 -06001671static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res)
1672{
Jens Axboe9adbd452019-12-20 08:45:55 -07001673 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001674 struct io_kiocb *nxt = NULL;
Jens Axboeba816ad2019-09-28 11:36:45 -06001675
1676 io_complete_rw_common(kiocb, res);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001677 io_put_req_find_next(req, &nxt);
1678
1679 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001680}
1681
Jens Axboedef596e2019-01-09 08:59:42 -07001682static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1683{
Jens Axboe9adbd452019-12-20 08:45:55 -07001684 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07001685
Jens Axboe491381ce2019-10-17 09:20:46 -06001686 if (kiocb->ki_flags & IOCB_WRITE)
1687 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001688
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001689 if (res != req->result)
1690 req_set_fail_links(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06001691 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -07001692 if (res != -EAGAIN)
1693 req->flags |= REQ_F_IOPOLL_COMPLETED;
1694}
1695
1696/*
1697 * After the iocb has been issued, it's safe to be found on the poll list.
1698 * Adding the kiocb to the list AFTER submission ensures that we don't
1699 * find it from a io_iopoll_getevents() thread before the issuer is done
1700 * accessing the kiocb cookie.
1701 */
1702static void io_iopoll_req_issued(struct io_kiocb *req)
1703{
1704 struct io_ring_ctx *ctx = req->ctx;
1705
1706 /*
1707 * Track whether we have multiple files in our lists. This will impact
1708 * how we do polling eventually, not spinning if we're on potentially
1709 * different devices.
1710 */
1711 if (list_empty(&ctx->poll_list)) {
1712 ctx->poll_multi_file = false;
1713 } else if (!ctx->poll_multi_file) {
1714 struct io_kiocb *list_req;
1715
1716 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1717 list);
Jens Axboe9adbd452019-12-20 08:45:55 -07001718 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07001719 ctx->poll_multi_file = true;
1720 }
1721
1722 /*
1723 * For fast devices, IO may have already completed. If it has, add
1724 * it to the front so we find it first.
1725 */
1726 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1727 list_add(&req->list, &ctx->poll_list);
1728 else
1729 list_add_tail(&req->list, &ctx->poll_list);
1730}
1731
Jens Axboe3d6770f2019-04-13 11:50:54 -06001732static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07001733{
Jens Axboe3d6770f2019-04-13 11:50:54 -06001734 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -07001735 int diff = state->has_refs - state->used_refs;
1736
1737 if (diff)
1738 fput_many(state->file, diff);
1739 state->file = NULL;
1740 }
1741}
1742
1743/*
1744 * Get as many references to a file as we have IOs left in this submission,
1745 * assuming most submissions are for one file, or at least that each file
1746 * has more than one submission.
1747 */
1748static struct file *io_file_get(struct io_submit_state *state, int fd)
1749{
1750 if (!state)
1751 return fget(fd);
1752
1753 if (state->file) {
1754 if (state->fd == fd) {
1755 state->used_refs++;
1756 state->ios_left--;
1757 return state->file;
1758 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06001759 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07001760 }
1761 state->file = fget_many(fd, state->ios_left);
1762 if (!state->file)
1763 return NULL;
1764
1765 state->fd = fd;
1766 state->has_refs = state->ios_left;
1767 state->used_refs = 1;
1768 state->ios_left--;
1769 return state->file;
1770}
1771
Jens Axboe2b188cc2019-01-07 10:46:33 -07001772/*
1773 * If we tracked the file through the SCM inflight mechanism, we could support
1774 * any file. For now, just ensure that anything potentially problematic is done
1775 * inline.
1776 */
1777static bool io_file_supports_async(struct file *file)
1778{
1779 umode_t mode = file_inode(file)->i_mode;
1780
Jens Axboe10d59342019-12-09 20:16:22 -07001781 if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07001782 return true;
1783 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
1784 return true;
1785
1786 return false;
1787}
1788
Jens Axboe3529d8c2019-12-19 18:24:38 -07001789static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1790 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001791{
Jens Axboedef596e2019-01-09 08:59:42 -07001792 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07001793 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06001794 unsigned ioprio;
1795 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001796
Jens Axboe09bb8392019-03-13 12:39:28 -06001797 if (!req->file)
1798 return -EBADF;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001799
Jens Axboe491381ce2019-10-17 09:20:46 -06001800 if (S_ISREG(file_inode(req->file)->i_mode))
1801 req->flags |= REQ_F_ISREG;
1802
Jens Axboe2b188cc2019-01-07 10:46:33 -07001803 kiocb->ki_pos = READ_ONCE(sqe->off);
Jens Axboeba042912019-12-25 16:33:42 -07001804 if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) {
1805 req->flags |= REQ_F_CUR_POS;
1806 kiocb->ki_pos = req->file->f_pos;
1807 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001808 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
1809 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
1810
1811 ioprio = READ_ONCE(sqe->ioprio);
1812 if (ioprio) {
1813 ret = ioprio_check_cap(ioprio);
1814 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06001815 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001816
1817 kiocb->ki_ioprio = ioprio;
1818 } else
1819 kiocb->ki_ioprio = get_current_ioprio();
1820
1821 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
1822 if (unlikely(ret))
Jens Axboe09bb8392019-03-13 12:39:28 -06001823 return ret;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001824
1825 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06001826 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
1827 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02001828 req->flags |= REQ_F_NOWAIT;
1829
1830 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001831 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001832
Jens Axboedef596e2019-01-09 08:59:42 -07001833 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07001834 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
1835 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06001836 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001837
Jens Axboedef596e2019-01-09 08:59:42 -07001838 kiocb->ki_flags |= IOCB_HIPRI;
1839 kiocb->ki_complete = io_complete_rw_iopoll;
Jens Axboe6873e0b2019-10-30 13:53:09 -06001840 req->result = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001841 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001842 if (kiocb->ki_flags & IOCB_HIPRI)
1843 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07001844 kiocb->ki_complete = io_complete_rw;
1845 }
Jens Axboe9adbd452019-12-20 08:45:55 -07001846
Jens Axboe3529d8c2019-12-19 18:24:38 -07001847 req->rw.addr = READ_ONCE(sqe->addr);
1848 req->rw.len = READ_ONCE(sqe->len);
Jens Axboe9adbd452019-12-20 08:45:55 -07001849 /* we own ->private, reuse it for the buffer index */
1850 req->rw.kiocb.private = (void *) (unsigned long)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001851 READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001852 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001853}
1854
1855static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
1856{
1857 switch (ret) {
1858 case -EIOCBQUEUED:
1859 break;
1860 case -ERESTARTSYS:
1861 case -ERESTARTNOINTR:
1862 case -ERESTARTNOHAND:
1863 case -ERESTART_RESTARTBLOCK:
1864 /*
1865 * We can't just restart the syscall, since previously
1866 * submitted sqes may already be in progress. Just fail this
1867 * IO with EINTR.
1868 */
1869 ret = -EINTR;
1870 /* fall through */
1871 default:
1872 kiocb->ki_complete(kiocb, ret, 0);
1873 }
1874}
1875
Jens Axboeba816ad2019-09-28 11:36:45 -06001876static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt,
1877 bool in_async)
1878{
Jens Axboeba042912019-12-25 16:33:42 -07001879 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
1880
1881 if (req->flags & REQ_F_CUR_POS)
1882 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001883 if (in_async && ret >= 0 && kiocb->ki_complete == io_complete_rw)
Jens Axboeba816ad2019-09-28 11:36:45 -06001884 *nxt = __io_complete_rw(kiocb, ret);
1885 else
1886 io_rw_done(kiocb, ret);
1887}
1888
Jens Axboe9adbd452019-12-20 08:45:55 -07001889static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03001890 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07001891{
Jens Axboe9adbd452019-12-20 08:45:55 -07001892 struct io_ring_ctx *ctx = req->ctx;
1893 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001894 struct io_mapped_ubuf *imu;
1895 unsigned index, buf_index;
1896 size_t offset;
1897 u64 buf_addr;
1898
1899 /* attempt to use fixed buffers without having provided iovecs */
1900 if (unlikely(!ctx->user_bufs))
1901 return -EFAULT;
1902
Jens Axboe9adbd452019-12-20 08:45:55 -07001903 buf_index = (unsigned long) req->rw.kiocb.private;
Jens Axboeedafcce2019-01-09 09:16:05 -07001904 if (unlikely(buf_index >= ctx->nr_user_bufs))
1905 return -EFAULT;
1906
1907 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
1908 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07001909 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07001910
1911 /* overflow */
1912 if (buf_addr + len < buf_addr)
1913 return -EFAULT;
1914 /* not inside the mapped region */
1915 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
1916 return -EFAULT;
1917
1918 /*
1919 * May not be a start of buffer, set size appropriately
1920 * and advance us to the beginning.
1921 */
1922 offset = buf_addr - imu->ubuf;
1923 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06001924
1925 if (offset) {
1926 /*
1927 * Don't use iov_iter_advance() here, as it's really slow for
1928 * using the latter parts of a big fixed buffer - it iterates
1929 * over each segment manually. We can cheat a bit here, because
1930 * we know that:
1931 *
1932 * 1) it's a BVEC iter, we set it up
1933 * 2) all bvecs are PAGE_SIZE in size, except potentially the
1934 * first and last bvec
1935 *
1936 * So just find our index, and adjust the iterator afterwards.
1937 * If the offset is within the first bvec (or the whole first
1938 * bvec, just use iov_iter_advance(). This makes it easier
1939 * since we can just skip the first segment, which may not
1940 * be PAGE_SIZE aligned.
1941 */
1942 const struct bio_vec *bvec = imu->bvec;
1943
1944 if (offset <= bvec->bv_len) {
1945 iov_iter_advance(iter, offset);
1946 } else {
1947 unsigned long seg_skip;
1948
1949 /* skip first vec */
1950 offset -= bvec->bv_len;
1951 seg_skip = 1 + (offset >> PAGE_SHIFT);
1952
1953 iter->bvec = bvec + seg_skip;
1954 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02001955 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001956 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06001957 }
1958 }
1959
Jens Axboe5e559562019-11-13 16:12:46 -07001960 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001961}
1962
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001963static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
1964 struct iovec **iovec, struct iov_iter *iter)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001965{
Jens Axboe9adbd452019-12-20 08:45:55 -07001966 void __user *buf = u64_to_user_ptr(req->rw.addr);
1967 size_t sqe_len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001968 u8 opcode;
1969
Jens Axboed625c6e2019-12-17 19:53:05 -07001970 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03001971 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07001972 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07001973 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07001974 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001975
Jens Axboe9adbd452019-12-20 08:45:55 -07001976 /* buffer index only valid with fixed read/write */
1977 if (req->rw.kiocb.private)
1978 return -EINVAL;
1979
Jens Axboe3a6820f2019-12-22 15:19:35 -07001980 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
1981 ssize_t ret;
1982 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
1983 *iovec = NULL;
1984 return ret;
1985 }
1986
Jens Axboef67676d2019-12-02 11:03:47 -07001987 if (req->io) {
1988 struct io_async_rw *iorw = &req->io->rw;
1989
1990 *iovec = iorw->iov;
1991 iov_iter_init(iter, rw, *iovec, iorw->nr_segs, iorw->size);
1992 if (iorw->iov == iorw->fast_iov)
1993 *iovec = NULL;
1994 return iorw->size;
1995 }
1996
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03001997 if (!req->has_user)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001998 return -EFAULT;
1999
2000#ifdef CONFIG_COMPAT
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002001 if (req->ctx->compat)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002002 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
2003 iovec, iter);
2004#endif
2005
2006 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
2007}
2008
Jens Axboe32960612019-09-23 11:05:34 -06002009/*
2010 * For files that don't have ->read_iter() and ->write_iter(), handle them
2011 * by looping over ->read() or ->write() manually.
2012 */
2013static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
2014 struct iov_iter *iter)
2015{
2016 ssize_t ret = 0;
2017
2018 /*
2019 * Don't support polled IO through this interface, and we can't
2020 * support non-blocking either. For the latter, this just causes
2021 * the kiocb to be handled from an async context.
2022 */
2023 if (kiocb->ki_flags & IOCB_HIPRI)
2024 return -EOPNOTSUPP;
2025 if (kiocb->ki_flags & IOCB_NOWAIT)
2026 return -EAGAIN;
2027
2028 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002029 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06002030 ssize_t nr;
2031
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002032 if (!iov_iter_is_bvec(iter)) {
2033 iovec = iov_iter_iovec(iter);
2034 } else {
2035 /* fixed buffers import bvec */
2036 iovec.iov_base = kmap(iter->bvec->bv_page)
2037 + iter->iov_offset;
2038 iovec.iov_len = min(iter->count,
2039 iter->bvec->bv_len - iter->iov_offset);
2040 }
2041
Jens Axboe32960612019-09-23 11:05:34 -06002042 if (rw == READ) {
2043 nr = file->f_op->read(file, iovec.iov_base,
2044 iovec.iov_len, &kiocb->ki_pos);
2045 } else {
2046 nr = file->f_op->write(file, iovec.iov_base,
2047 iovec.iov_len, &kiocb->ki_pos);
2048 }
2049
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002050 if (iov_iter_is_bvec(iter))
2051 kunmap(iter->bvec->bv_page);
2052
Jens Axboe32960612019-09-23 11:05:34 -06002053 if (nr < 0) {
2054 if (!ret)
2055 ret = nr;
2056 break;
2057 }
2058 ret += nr;
2059 if (nr != iovec.iov_len)
2060 break;
2061 iov_iter_advance(iter, nr);
2062 }
2063
2064 return ret;
2065}
2066
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002067static void io_req_map_rw(struct io_kiocb *req, ssize_t io_size,
Jens Axboef67676d2019-12-02 11:03:47 -07002068 struct iovec *iovec, struct iovec *fast_iov,
2069 struct iov_iter *iter)
2070{
2071 req->io->rw.nr_segs = iter->nr_segs;
2072 req->io->rw.size = io_size;
2073 req->io->rw.iov = iovec;
2074 if (!req->io->rw.iov) {
2075 req->io->rw.iov = req->io->rw.fast_iov;
2076 memcpy(req->io->rw.iov, fast_iov,
2077 sizeof(struct iovec) * iter->nr_segs);
2078 }
2079}
2080
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002081static int io_alloc_async_ctx(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07002082{
Jens Axboed3656342019-12-18 09:50:26 -07002083 if (!io_op_defs[req->opcode].async_ctx)
2084 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002085 req->io = kmalloc(sizeof(*req->io), GFP_KERNEL);
Jens Axboe06b76d42019-12-19 14:44:26 -07002086 return req->io == NULL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002087}
2088
2089static void io_rw_async(struct io_wq_work **workptr)
2090{
2091 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2092 struct iovec *iov = NULL;
2093
2094 if (req->io->rw.iov != req->io->rw.fast_iov)
2095 iov = req->io->rw.iov;
2096 io_wq_submit_work(workptr);
2097 kfree(iov);
2098}
2099
2100static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size,
2101 struct iovec *iovec, struct iovec *fast_iov,
2102 struct iov_iter *iter)
2103{
Jens Axboe74566df2020-01-13 19:23:24 -07002104 if (req->opcode == IORING_OP_READ_FIXED ||
2105 req->opcode == IORING_OP_WRITE_FIXED)
2106 return 0;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002107 if (!req->io && io_alloc_async_ctx(req))
2108 return -ENOMEM;
2109
2110 io_req_map_rw(req, io_size, iovec, fast_iov, iter);
2111 req->work.func = io_rw_async;
2112 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002113}
2114
Jens Axboe3529d8c2019-12-19 18:24:38 -07002115static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2116 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002117{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002118 struct io_async_ctx *io;
2119 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002120 ssize_t ret;
2121
Jens Axboe3529d8c2019-12-19 18:24:38 -07002122 ret = io_prep_rw(req, sqe, force_nonblock);
2123 if (ret)
2124 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002125
Jens Axboe3529d8c2019-12-19 18:24:38 -07002126 if (unlikely(!(req->file->f_mode & FMODE_READ)))
2127 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002128
Jens Axboe3529d8c2019-12-19 18:24:38 -07002129 if (!req->io)
2130 return 0;
2131
2132 io = req->io;
2133 io->rw.iov = io->rw.fast_iov;
2134 req->io = NULL;
2135 ret = io_import_iovec(READ, req, &io->rw.iov, &iter);
2136 req->io = io;
2137 if (ret < 0)
2138 return ret;
2139
2140 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2141 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002142}
2143
Pavel Begunkov267bc902019-11-07 01:41:08 +03002144static int io_read(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06002145 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002146{
2147 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002148 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002149 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002150 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002151 ssize_t io_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002152
Jens Axboe3529d8c2019-12-19 18:24:38 -07002153 ret = io_import_iovec(READ, req, &iovec, &iter);
Jens Axboe06b76d42019-12-19 14:44:26 -07002154 if (ret < 0)
2155 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002156
Jens Axboefd6c2e42019-12-18 12:19:41 -07002157 /* Ensure we clear previously set non-block flag */
2158 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07002159 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002160
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002161 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002162 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06002163 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07002164 req->result = io_size;
2165
2166 /*
2167 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2168 * we know to async punt it even if it was opened O_NONBLOCK
2169 */
Jens Axboe9adbd452019-12-20 08:45:55 -07002170 if (force_nonblock && !io_file_supports_async(req->file)) {
Jens Axboef67676d2019-12-02 11:03:47 -07002171 req->flags |= REQ_F_MUST_PUNT;
2172 goto copy_iov;
2173 }
Jens Axboe9e645e112019-05-10 16:07:28 -06002174
Jens Axboe31b51512019-01-18 22:56:34 -07002175 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002176 ret = rw_verify_area(READ, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002177 if (!ret) {
2178 ssize_t ret2;
2179
Jens Axboe9adbd452019-12-20 08:45:55 -07002180 if (req->file->f_op->read_iter)
2181 ret2 = call_read_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002182 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002183 ret2 = loop_rw_iter(READ, req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002184
Jens Axboe9d93a3f2019-05-15 13:53:07 -06002185 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboef67676d2019-12-02 11:03:47 -07002186 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002187 kiocb_done(kiocb, ret2, nxt, req->in_async);
Jens Axboef67676d2019-12-02 11:03:47 -07002188 } else {
2189copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002190 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002191 inline_vecs, &iter);
2192 if (ret)
2193 goto out_free;
2194 return -EAGAIN;
2195 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002196 }
Jens Axboef67676d2019-12-02 11:03:47 -07002197out_free:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002198 if (!io_wq_current_is_worker())
2199 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002200 return ret;
2201}
2202
Jens Axboe3529d8c2019-12-19 18:24:38 -07002203static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2204 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002205{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002206 struct io_async_ctx *io;
2207 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002208 ssize_t ret;
2209
Jens Axboe3529d8c2019-12-19 18:24:38 -07002210 ret = io_prep_rw(req, sqe, force_nonblock);
2211 if (ret)
2212 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002213
Jens Axboe3529d8c2019-12-19 18:24:38 -07002214 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
2215 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002216
Jens Axboe3529d8c2019-12-19 18:24:38 -07002217 if (!req->io)
2218 return 0;
2219
2220 io = req->io;
2221 io->rw.iov = io->rw.fast_iov;
2222 req->io = NULL;
2223 ret = io_import_iovec(WRITE, req, &io->rw.iov, &iter);
2224 req->io = io;
2225 if (ret < 0)
2226 return ret;
2227
2228 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2229 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002230}
2231
Pavel Begunkov267bc902019-11-07 01:41:08 +03002232static int io_write(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06002233 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002234{
2235 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002236 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002237 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002238 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002239 ssize_t ret, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002240
Jens Axboe3529d8c2019-12-19 18:24:38 -07002241 ret = io_import_iovec(WRITE, req, &iovec, &iter);
Jens Axboe06b76d42019-12-19 14:44:26 -07002242 if (ret < 0)
2243 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002244
Jens Axboefd6c2e42019-12-18 12:19:41 -07002245 /* Ensure we clear previously set non-block flag */
2246 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07002247 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002248
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002249 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002250 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06002251 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07002252 req->result = io_size;
2253
2254 /*
2255 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2256 * we know to async punt it even if it was opened O_NONBLOCK
2257 */
2258 if (force_nonblock && !io_file_supports_async(req->file)) {
2259 req->flags |= REQ_F_MUST_PUNT;
2260 goto copy_iov;
2261 }
2262
Jens Axboe10d59342019-12-09 20:16:22 -07002263 /* file path doesn't support NOWAIT for non-direct_IO */
2264 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
2265 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07002266 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06002267
Jens Axboe31b51512019-01-18 22:56:34 -07002268 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002269 ret = rw_verify_area(WRITE, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002270 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01002271 ssize_t ret2;
2272
Jens Axboe2b188cc2019-01-07 10:46:33 -07002273 /*
2274 * Open-code file_start_write here to grab freeze protection,
2275 * which will be released by another thread in
2276 * io_complete_rw(). Fool lockdep by telling it the lock got
2277 * released so that it doesn't complain about the held lock when
2278 * we return to userspace.
2279 */
Jens Axboe491381ce2019-10-17 09:20:46 -06002280 if (req->flags & REQ_F_ISREG) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002281 __sb_start_write(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002282 SB_FREEZE_WRITE, true);
Jens Axboe9adbd452019-12-20 08:45:55 -07002283 __sb_writers_release(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002284 SB_FREEZE_WRITE);
2285 }
2286 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01002287
Jens Axboe9adbd452019-12-20 08:45:55 -07002288 if (req->file->f_op->write_iter)
2289 ret2 = call_write_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002290 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002291 ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter);
Jens Axboef67676d2019-12-02 11:03:47 -07002292 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002293 kiocb_done(kiocb, ret2, nxt, req->in_async);
Jens Axboef67676d2019-12-02 11:03:47 -07002294 } else {
2295copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002296 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002297 inline_vecs, &iter);
2298 if (ret)
2299 goto out_free;
2300 return -EAGAIN;
2301 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002302 }
Jens Axboe31b51512019-01-18 22:56:34 -07002303out_free:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002304 if (!io_wq_current_is_worker())
2305 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002306 return ret;
2307}
2308
2309/*
2310 * IORING_OP_NOP just posts a completion event, nothing else.
2311 */
Jens Axboe78e19bb2019-11-06 15:21:34 -07002312static int io_nop(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002313{
2314 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002315
Jens Axboedef596e2019-01-09 08:59:42 -07002316 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2317 return -EINVAL;
2318
Jens Axboe78e19bb2019-11-06 15:21:34 -07002319 io_cqring_add_event(req, 0);
Jens Axboee65ef562019-03-12 10:16:44 -06002320 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002321 return 0;
2322}
2323
Jens Axboe3529d8c2019-12-19 18:24:38 -07002324static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002325{
Jens Axboe6b063142019-01-10 22:13:58 -07002326 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002327
Jens Axboe09bb8392019-03-13 12:39:28 -06002328 if (!req->file)
2329 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002330
Jens Axboe6b063142019-01-10 22:13:58 -07002331 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07002332 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07002333 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002334 return -EINVAL;
2335
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002336 req->sync.flags = READ_ONCE(sqe->fsync_flags);
2337 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
2338 return -EINVAL;
2339
2340 req->sync.off = READ_ONCE(sqe->off);
2341 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002342 return 0;
2343}
2344
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002345static bool io_req_cancelled(struct io_kiocb *req)
2346{
2347 if (req->work.flags & IO_WQ_WORK_CANCEL) {
2348 req_set_fail_links(req);
2349 io_cqring_add_event(req, -ECANCELED);
2350 io_put_req(req);
2351 return true;
2352 }
2353
2354 return false;
2355}
2356
Jens Axboe78912932020-01-14 22:09:06 -07002357static void io_link_work_cb(struct io_wq_work **workptr)
2358{
2359 struct io_wq_work *work = *workptr;
2360 struct io_kiocb *link = work->data;
2361
2362 io_queue_linked_timeout(link);
2363 work->func = io_wq_submit_work;
2364}
2365
2366static void io_wq_assign_next(struct io_wq_work **workptr, struct io_kiocb *nxt)
2367{
2368 struct io_kiocb *link;
2369
2370 io_prep_async_work(nxt, &link);
2371 *workptr = &nxt->work;
2372 if (link) {
2373 nxt->work.flags |= IO_WQ_WORK_CB;
2374 nxt->work.func = io_link_work_cb;
2375 nxt->work.data = link;
2376 }
2377}
2378
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002379static void io_fsync_finish(struct io_wq_work **workptr)
2380{
2381 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2382 loff_t end = req->sync.off + req->sync.len;
2383 struct io_kiocb *nxt = NULL;
2384 int ret;
2385
2386 if (io_req_cancelled(req))
2387 return;
2388
Jens Axboe9adbd452019-12-20 08:45:55 -07002389 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002390 end > 0 ? end : LLONG_MAX,
2391 req->sync.flags & IORING_FSYNC_DATASYNC);
2392 if (ret < 0)
2393 req_set_fail_links(req);
2394 io_cqring_add_event(req, ret);
2395 io_put_req_find_next(req, &nxt);
2396 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07002397 io_wq_assign_next(workptr, nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002398}
2399
Jens Axboefc4df992019-12-10 14:38:45 -07002400static int io_fsync(struct io_kiocb *req, struct io_kiocb **nxt,
2401 bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002402{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002403 struct io_wq_work *work, *old_work;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002404
2405 /* fsync always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002406 if (force_nonblock) {
2407 io_put_req(req);
2408 req->work.func = io_fsync_finish;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002409 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002410 }
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002411
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002412 work = old_work = &req->work;
2413 io_fsync_finish(&work);
2414 if (work && work != old_work)
2415 *nxt = container_of(work, struct io_kiocb, work);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002416 return 0;
2417}
2418
Jens Axboed63d1b52019-12-10 10:38:56 -07002419static void io_fallocate_finish(struct io_wq_work **workptr)
2420{
2421 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2422 struct io_kiocb *nxt = NULL;
2423 int ret;
2424
2425 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
2426 req->sync.len);
2427 if (ret < 0)
2428 req_set_fail_links(req);
2429 io_cqring_add_event(req, ret);
2430 io_put_req_find_next(req, &nxt);
2431 if (nxt)
2432 io_wq_assign_next(workptr, nxt);
2433}
2434
2435static int io_fallocate_prep(struct io_kiocb *req,
2436 const struct io_uring_sqe *sqe)
2437{
2438 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
2439 return -EINVAL;
2440
2441 req->sync.off = READ_ONCE(sqe->off);
2442 req->sync.len = READ_ONCE(sqe->addr);
2443 req->sync.mode = READ_ONCE(sqe->len);
2444 return 0;
2445}
2446
2447static int io_fallocate(struct io_kiocb *req, struct io_kiocb **nxt,
2448 bool force_nonblock)
2449{
2450 struct io_wq_work *work, *old_work;
2451
2452 /* fallocate always requiring blocking context */
2453 if (force_nonblock) {
2454 io_put_req(req);
2455 req->work.func = io_fallocate_finish;
2456 return -EAGAIN;
2457 }
2458
2459 work = old_work = &req->work;
2460 io_fallocate_finish(&work);
2461 if (work && work != old_work)
2462 *nxt = container_of(work, struct io_kiocb, work);
2463
2464 return 0;
2465}
2466
Jens Axboe15b71ab2019-12-11 11:20:36 -07002467static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2468{
Jens Axboef8748882020-01-08 17:47:02 -07002469 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002470 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 Axboef8748882020-01-08 17:47:02 -07002477 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
Jens Axboef8748882020-01-08 17:47:02 -07002480 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002481 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{
Jens Axboef8748882020-01-08 17:47:02 -07002596 const char __user *fname;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002597 unsigned lookup_flags;
2598 int ret;
2599
2600 if (sqe->ioprio || sqe->buf_index)
2601 return -EINVAL;
2602
2603 req->open.dfd = READ_ONCE(sqe->fd);
2604 req->open.mask = READ_ONCE(sqe->len);
Jens Axboef8748882020-01-08 17:47:02 -07002605 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002606 req->open.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboec12cedf2020-01-08 17:41:21 -07002607 req->open.how.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002608
Jens Axboec12cedf2020-01-08 17:41:21 -07002609 if (vfs_stat_set_lookup_flags(&lookup_flags, req->open.how.flags))
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002610 return -EINVAL;
2611
Jens Axboef8748882020-01-08 17:47:02 -07002612 req->open.filename = getname_flags(fname, lookup_flags, NULL);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002613 if (IS_ERR(req->open.filename)) {
2614 ret = PTR_ERR(req->open.filename);
2615 req->open.filename = NULL;
2616 return ret;
2617 }
2618
2619 return 0;
2620}
2621
2622static int io_statx(struct io_kiocb *req, struct io_kiocb **nxt,
2623 bool force_nonblock)
2624{
2625 struct io_open *ctx = &req->open;
2626 unsigned lookup_flags;
2627 struct path path;
2628 struct kstat stat;
2629 int ret;
2630
2631 if (force_nonblock)
2632 return -EAGAIN;
2633
Jens Axboec12cedf2020-01-08 17:41:21 -07002634 if (vfs_stat_set_lookup_flags(&lookup_flags, ctx->how.flags))
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002635 return -EINVAL;
2636
2637retry:
2638 /* filename_lookup() drops it, keep a reference */
2639 ctx->filename->refcnt++;
2640
2641 ret = filename_lookup(ctx->dfd, ctx->filename, lookup_flags, &path,
2642 NULL);
2643 if (ret)
2644 goto err;
2645
Jens Axboec12cedf2020-01-08 17:41:21 -07002646 ret = vfs_getattr(&path, &stat, ctx->mask, ctx->how.flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002647 path_put(&path);
2648 if (retry_estale(ret, lookup_flags)) {
2649 lookup_flags |= LOOKUP_REVAL;
2650 goto retry;
2651 }
2652 if (!ret)
2653 ret = cp_statx(&stat, ctx->buffer);
2654err:
2655 putname(ctx->filename);
2656 if (ret < 0)
2657 req_set_fail_links(req);
2658 io_cqring_add_event(req, ret);
2659 io_put_req_find_next(req, nxt);
2660 return 0;
2661}
2662
Jens Axboeb5dba592019-12-11 14:02:38 -07002663static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2664{
2665 /*
2666 * If we queue this for async, it must not be cancellable. That would
2667 * leave the 'file' in an undeterminate state.
2668 */
2669 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
2670
2671 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
2672 sqe->rw_flags || sqe->buf_index)
2673 return -EINVAL;
2674 if (sqe->flags & IOSQE_FIXED_FILE)
2675 return -EINVAL;
2676
2677 req->close.fd = READ_ONCE(sqe->fd);
2678 if (req->file->f_op == &io_uring_fops ||
2679 req->close.fd == req->ring_fd)
2680 return -EBADF;
2681
2682 return 0;
2683}
2684
2685static void io_close_finish(struct io_wq_work **workptr)
2686{
2687 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2688 struct io_kiocb *nxt = NULL;
2689
2690 /* Invoked with files, we need to do the close */
2691 if (req->work.files) {
2692 int ret;
2693
2694 ret = filp_close(req->close.put_file, req->work.files);
2695 if (ret < 0) {
2696 req_set_fail_links(req);
2697 }
2698 io_cqring_add_event(req, ret);
2699 }
2700
2701 fput(req->close.put_file);
2702
2703 /* we bypassed the re-issue, drop the submission reference */
2704 io_put_req(req);
2705 io_put_req_find_next(req, &nxt);
2706 if (nxt)
2707 io_wq_assign_next(workptr, nxt);
2708}
2709
2710static int io_close(struct io_kiocb *req, struct io_kiocb **nxt,
2711 bool force_nonblock)
2712{
2713 int ret;
2714
2715 req->close.put_file = NULL;
2716 ret = __close_fd_get_file(req->close.fd, &req->close.put_file);
2717 if (ret < 0)
2718 return ret;
2719
2720 /* if the file has a flush method, be safe and punt to async */
2721 if (req->close.put_file->f_op->flush && !io_wq_current_is_worker()) {
2722 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
2723 goto eagain;
2724 }
2725
2726 /*
2727 * No ->flush(), safely close from here and just punt the
2728 * fput() to async context.
2729 */
2730 ret = filp_close(req->close.put_file, current->files);
2731
2732 if (ret < 0)
2733 req_set_fail_links(req);
2734 io_cqring_add_event(req, ret);
2735
2736 if (io_wq_current_is_worker()) {
2737 struct io_wq_work *old_work, *work;
2738
2739 old_work = work = &req->work;
2740 io_close_finish(&work);
2741 if (work && work != old_work)
2742 *nxt = container_of(work, struct io_kiocb, work);
2743 return 0;
2744 }
2745
2746eagain:
2747 req->work.func = io_close_finish;
2748 return -EAGAIN;
2749}
2750
Jens Axboe3529d8c2019-12-19 18:24:38 -07002751static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002752{
2753 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002754
2755 if (!req->file)
2756 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002757
2758 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2759 return -EINVAL;
2760 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
2761 return -EINVAL;
2762
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002763 req->sync.off = READ_ONCE(sqe->off);
2764 req->sync.len = READ_ONCE(sqe->len);
2765 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002766 return 0;
2767}
2768
2769static void io_sync_file_range_finish(struct io_wq_work **workptr)
2770{
2771 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2772 struct io_kiocb *nxt = NULL;
2773 int ret;
2774
2775 if (io_req_cancelled(req))
2776 return;
2777
Jens Axboe9adbd452019-12-20 08:45:55 -07002778 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002779 req->sync.flags);
2780 if (ret < 0)
2781 req_set_fail_links(req);
2782 io_cqring_add_event(req, ret);
2783 io_put_req_find_next(req, &nxt);
2784 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07002785 io_wq_assign_next(workptr, nxt);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002786}
2787
Jens Axboefc4df992019-12-10 14:38:45 -07002788static int io_sync_file_range(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002789 bool force_nonblock)
2790{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002791 struct io_wq_work *work, *old_work;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002792
2793 /* sync_file_range always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002794 if (force_nonblock) {
2795 io_put_req(req);
2796 req->work.func = io_sync_file_range_finish;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002797 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002798 }
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002799
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002800 work = old_work = &req->work;
2801 io_sync_file_range_finish(&work);
2802 if (work && work != old_work)
2803 *nxt = container_of(work, struct io_kiocb, work);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002804 return 0;
2805}
2806
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002807#if defined(CONFIG_NET)
2808static void io_sendrecv_async(struct io_wq_work **workptr)
2809{
2810 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2811 struct iovec *iov = NULL;
2812
2813 if (req->io->rw.iov != req->io->rw.fast_iov)
2814 iov = req->io->msg.iov;
2815 io_wq_submit_work(workptr);
2816 kfree(iov);
2817}
2818#endif
2819
Jens Axboe3529d8c2019-12-19 18:24:38 -07002820static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06002821{
Jens Axboe03b12302019-12-02 18:50:25 -07002822#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07002823 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002824 struct io_async_ctx *io = req->io;
Jens Axboe03b12302019-12-02 18:50:25 -07002825
Jens Axboee47293f2019-12-20 08:58:21 -07002826 sr->msg_flags = READ_ONCE(sqe->msg_flags);
2827 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07002828 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07002829
Jens Axboefddafac2020-01-04 20:19:44 -07002830 if (!io || req->opcode == IORING_OP_SEND)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002831 return 0;
2832
Jens Axboed9688562019-12-09 19:35:20 -07002833 io->msg.iov = io->msg.fast_iov;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002834 return sendmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07002835 &io->msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002836#else
Jens Axboee47293f2019-12-20 08:58:21 -07002837 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07002838#endif
2839}
2840
Jens Axboefc4df992019-12-10 14:38:45 -07002841static int io_sendmsg(struct io_kiocb *req, struct io_kiocb **nxt,
2842 bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07002843{
2844#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07002845 struct io_async_msghdr *kmsg = NULL;
Jens Axboe03b12302019-12-02 18:50:25 -07002846 struct socket *sock;
2847 int ret;
2848
2849 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2850 return -EINVAL;
2851
2852 sock = sock_from_file(req->file, &ret);
2853 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002854 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07002855 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -07002856 unsigned flags;
2857
Jens Axboe03b12302019-12-02 18:50:25 -07002858 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07002859 kmsg = &req->io->msg;
2860 kmsg->msg.msg_name = &addr;
2861 /* if iov is set, it's allocated already */
2862 if (!kmsg->iov)
2863 kmsg->iov = kmsg->fast_iov;
2864 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07002865 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07002866 struct io_sr_msg *sr = &req->sr_msg;
2867
Jens Axboe0b416c32019-12-15 10:57:46 -07002868 kmsg = &io.msg;
2869 kmsg->msg.msg_name = &addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002870
2871 io.msg.iov = io.msg.fast_iov;
2872 ret = sendmsg_copy_msghdr(&io.msg.msg, sr->msg,
2873 sr->msg_flags, &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002874 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002875 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07002876 }
2877
Jens Axboee47293f2019-12-20 08:58:21 -07002878 flags = req->sr_msg.msg_flags;
2879 if (flags & MSG_DONTWAIT)
2880 req->flags |= REQ_F_NOWAIT;
2881 else if (force_nonblock)
2882 flags |= MSG_DONTWAIT;
2883
Jens Axboe0b416c32019-12-15 10:57:46 -07002884 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Jens Axboe03b12302019-12-02 18:50:25 -07002885 if (force_nonblock && ret == -EAGAIN) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002886 if (req->io)
2887 return -EAGAIN;
2888 if (io_alloc_async_ctx(req))
2889 return -ENOMEM;
2890 memcpy(&req->io->msg, &io.msg, sizeof(io.msg));
2891 req->work.func = io_sendrecv_async;
Jens Axboe0b416c32019-12-15 10:57:46 -07002892 return -EAGAIN;
Jens Axboe03b12302019-12-02 18:50:25 -07002893 }
2894 if (ret == -ERESTARTSYS)
2895 ret = -EINTR;
2896 }
2897
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002898 if (!io_wq_current_is_worker() && kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07002899 kfree(kmsg->iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002900 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07002901 if (ret < 0)
2902 req_set_fail_links(req);
Jens Axboe03b12302019-12-02 18:50:25 -07002903 io_put_req_find_next(req, nxt);
2904 return 0;
2905#else
2906 return -EOPNOTSUPP;
2907#endif
2908}
2909
Jens Axboefddafac2020-01-04 20:19:44 -07002910static int io_send(struct io_kiocb *req, struct io_kiocb **nxt,
2911 bool force_nonblock)
2912{
2913#if defined(CONFIG_NET)
2914 struct socket *sock;
2915 int ret;
2916
2917 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2918 return -EINVAL;
2919
2920 sock = sock_from_file(req->file, &ret);
2921 if (sock) {
2922 struct io_sr_msg *sr = &req->sr_msg;
2923 struct msghdr msg;
2924 struct iovec iov;
2925 unsigned flags;
2926
2927 ret = import_single_range(WRITE, sr->buf, sr->len, &iov,
2928 &msg.msg_iter);
2929 if (ret)
2930 return ret;
2931
2932 msg.msg_name = NULL;
2933 msg.msg_control = NULL;
2934 msg.msg_controllen = 0;
2935 msg.msg_namelen = 0;
2936
2937 flags = req->sr_msg.msg_flags;
2938 if (flags & MSG_DONTWAIT)
2939 req->flags |= REQ_F_NOWAIT;
2940 else if (force_nonblock)
2941 flags |= MSG_DONTWAIT;
2942
2943 ret = __sys_sendmsg_sock(sock, &msg, flags);
2944 if (force_nonblock && ret == -EAGAIN)
2945 return -EAGAIN;
2946 if (ret == -ERESTARTSYS)
2947 ret = -EINTR;
2948 }
2949
2950 io_cqring_add_event(req, ret);
2951 if (ret < 0)
2952 req_set_fail_links(req);
2953 io_put_req_find_next(req, nxt);
2954 return 0;
2955#else
2956 return -EOPNOTSUPP;
2957#endif
2958}
2959
Jens Axboe3529d8c2019-12-19 18:24:38 -07002960static int io_recvmsg_prep(struct io_kiocb *req,
2961 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07002962{
2963#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07002964 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002965 struct io_async_ctx *io = req->io;
Jens Axboe06b76d42019-12-19 14:44:26 -07002966
Jens Axboe3529d8c2019-12-19 18:24:38 -07002967 sr->msg_flags = READ_ONCE(sqe->msg_flags);
2968 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
2969
Jens Axboefddafac2020-01-04 20:19:44 -07002970 if (!io || req->opcode == IORING_OP_RECV)
Jens Axboe06b76d42019-12-19 14:44:26 -07002971 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07002972
Jens Axboed9688562019-12-09 19:35:20 -07002973 io->msg.iov = io->msg.fast_iov;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002974 return recvmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07002975 &io->msg.uaddr, &io->msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002976#else
Jens Axboee47293f2019-12-20 08:58:21 -07002977 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07002978#endif
2979}
2980
Jens Axboefc4df992019-12-10 14:38:45 -07002981static int io_recvmsg(struct io_kiocb *req, struct io_kiocb **nxt,
2982 bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07002983{
2984#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07002985 struct io_async_msghdr *kmsg = NULL;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002986 struct socket *sock;
2987 int ret;
2988
2989 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2990 return -EINVAL;
2991
2992 sock = sock_from_file(req->file, &ret);
2993 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002994 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07002995 struct sockaddr_storage addr;
Jens Axboe0fa03c62019-04-19 13:34:07 -06002996 unsigned flags;
2997
Jens Axboe03b12302019-12-02 18:50:25 -07002998 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07002999 kmsg = &req->io->msg;
3000 kmsg->msg.msg_name = &addr;
3001 /* if iov is set, it's allocated already */
3002 if (!kmsg->iov)
3003 kmsg->iov = kmsg->fast_iov;
3004 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07003005 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003006 struct io_sr_msg *sr = &req->sr_msg;
3007
Jens Axboe0b416c32019-12-15 10:57:46 -07003008 kmsg = &io.msg;
3009 kmsg->msg.msg_name = &addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003010
3011 io.msg.iov = io.msg.fast_iov;
3012 ret = recvmsg_copy_msghdr(&io.msg.msg, sr->msg,
3013 sr->msg_flags, &io.msg.uaddr,
3014 &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07003015 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003016 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003017 }
Jens Axboe0fa03c62019-04-19 13:34:07 -06003018
Jens Axboee47293f2019-12-20 08:58:21 -07003019 flags = req->sr_msg.msg_flags;
3020 if (flags & MSG_DONTWAIT)
3021 req->flags |= REQ_F_NOWAIT;
3022 else if (force_nonblock)
3023 flags |= MSG_DONTWAIT;
3024
3025 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.msg,
3026 kmsg->uaddr, flags);
Jens Axboe03b12302019-12-02 18:50:25 -07003027 if (force_nonblock && ret == -EAGAIN) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003028 if (req->io)
3029 return -EAGAIN;
3030 if (io_alloc_async_ctx(req))
3031 return -ENOMEM;
3032 memcpy(&req->io->msg, &io.msg, sizeof(io.msg));
3033 req->work.func = io_sendrecv_async;
Jens Axboe0b416c32019-12-15 10:57:46 -07003034 return -EAGAIN;
Jens Axboe03b12302019-12-02 18:50:25 -07003035 }
Jens Axboe441cdbd2019-12-02 18:49:10 -07003036 if (ret == -ERESTARTSYS)
3037 ret = -EINTR;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003038 }
3039
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003040 if (!io_wq_current_is_worker() && kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07003041 kfree(kmsg->iov);
Jens Axboe78e19bb2019-11-06 15:21:34 -07003042 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003043 if (ret < 0)
3044 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003045 io_put_req_find_next(req, nxt);
Jens Axboe0fa03c62019-04-19 13:34:07 -06003046 return 0;
3047#else
3048 return -EOPNOTSUPP;
3049#endif
3050}
3051
Jens Axboefddafac2020-01-04 20:19:44 -07003052static int io_recv(struct io_kiocb *req, struct io_kiocb **nxt,
3053 bool force_nonblock)
3054{
3055#if defined(CONFIG_NET)
3056 struct socket *sock;
3057 int ret;
3058
3059 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3060 return -EINVAL;
3061
3062 sock = sock_from_file(req->file, &ret);
3063 if (sock) {
3064 struct io_sr_msg *sr = &req->sr_msg;
3065 struct msghdr msg;
3066 struct iovec iov;
3067 unsigned flags;
3068
3069 ret = import_single_range(READ, sr->buf, sr->len, &iov,
3070 &msg.msg_iter);
3071 if (ret)
3072 return ret;
3073
3074 msg.msg_name = NULL;
3075 msg.msg_control = NULL;
3076 msg.msg_controllen = 0;
3077 msg.msg_namelen = 0;
3078 msg.msg_iocb = NULL;
3079 msg.msg_flags = 0;
3080
3081 flags = req->sr_msg.msg_flags;
3082 if (flags & MSG_DONTWAIT)
3083 req->flags |= REQ_F_NOWAIT;
3084 else if (force_nonblock)
3085 flags |= MSG_DONTWAIT;
3086
3087 ret = __sys_recvmsg_sock(sock, &msg, NULL, NULL, flags);
3088 if (force_nonblock && ret == -EAGAIN)
3089 return -EAGAIN;
3090 if (ret == -ERESTARTSYS)
3091 ret = -EINTR;
3092 }
3093
3094 io_cqring_add_event(req, ret);
3095 if (ret < 0)
3096 req_set_fail_links(req);
3097 io_put_req_find_next(req, nxt);
3098 return 0;
3099#else
3100 return -EOPNOTSUPP;
3101#endif
3102}
3103
3104
Jens Axboe3529d8c2019-12-19 18:24:38 -07003105static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003106{
3107#if defined(CONFIG_NET)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003108 struct io_accept *accept = &req->accept;
3109
Jens Axboe17f2fe32019-10-17 14:42:58 -06003110 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3111 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05003112 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003113 return -EINVAL;
3114
Jens Axboed55e5f52019-12-11 16:12:15 -07003115 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3116 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003117 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003118 return 0;
3119#else
3120 return -EOPNOTSUPP;
3121#endif
3122}
Jens Axboe17f2fe32019-10-17 14:42:58 -06003123
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003124#if defined(CONFIG_NET)
3125static int __io_accept(struct io_kiocb *req, struct io_kiocb **nxt,
3126 bool force_nonblock)
3127{
3128 struct io_accept *accept = &req->accept;
3129 unsigned file_flags;
3130 int ret;
3131
3132 file_flags = force_nonblock ? O_NONBLOCK : 0;
3133 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
3134 accept->addr_len, accept->flags);
3135 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003136 return -EAGAIN;
Jens Axboe8e3cca12019-11-09 19:52:33 -07003137 if (ret == -ERESTARTSYS)
3138 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003139 if (ret < 0)
3140 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07003141 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003142 io_put_req_find_next(req, nxt);
Jens Axboe17f2fe32019-10-17 14:42:58 -06003143 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003144}
3145
3146static void io_accept_finish(struct io_wq_work **workptr)
3147{
3148 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3149 struct io_kiocb *nxt = NULL;
3150
3151 if (io_req_cancelled(req))
3152 return;
3153 __io_accept(req, &nxt, false);
3154 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07003155 io_wq_assign_next(workptr, nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003156}
3157#endif
3158
3159static int io_accept(struct io_kiocb *req, struct io_kiocb **nxt,
3160 bool force_nonblock)
3161{
3162#if defined(CONFIG_NET)
3163 int ret;
3164
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003165 ret = __io_accept(req, nxt, force_nonblock);
3166 if (ret == -EAGAIN && force_nonblock) {
3167 req->work.func = io_accept_finish;
3168 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
3169 io_put_req(req);
3170 return -EAGAIN;
3171 }
3172 return 0;
Jens Axboe17f2fe32019-10-17 14:42:58 -06003173#else
3174 return -EOPNOTSUPP;
3175#endif
3176}
3177
Jens Axboe3529d8c2019-12-19 18:24:38 -07003178static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07003179{
3180#if defined(CONFIG_NET)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003181 struct io_connect *conn = &req->connect;
3182 struct io_async_ctx *io = req->io;
Jens Axboef499a022019-12-02 16:28:46 -07003183
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003184 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3185 return -EINVAL;
3186 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
3187 return -EINVAL;
3188
Jens Axboe3529d8c2019-12-19 18:24:38 -07003189 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3190 conn->addr_len = READ_ONCE(sqe->addr2);
3191
3192 if (!io)
3193 return 0;
3194
3195 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003196 &io->connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07003197#else
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003198 return -EOPNOTSUPP;
Jens Axboef499a022019-12-02 16:28:46 -07003199#endif
3200}
3201
Jens Axboefc4df992019-12-10 14:38:45 -07003202static int io_connect(struct io_kiocb *req, struct io_kiocb **nxt,
3203 bool force_nonblock)
Jens Axboef8e85cf2019-11-23 14:24:24 -07003204{
3205#if defined(CONFIG_NET)
Jens Axboef499a022019-12-02 16:28:46 -07003206 struct io_async_ctx __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003207 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003208 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003209
Jens Axboef499a022019-12-02 16:28:46 -07003210 if (req->io) {
3211 io = req->io;
3212 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003213 ret = move_addr_to_kernel(req->connect.addr,
3214 req->connect.addr_len,
3215 &__io.connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07003216 if (ret)
3217 goto out;
3218 io = &__io;
3219 }
3220
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003221 file_flags = force_nonblock ? O_NONBLOCK : 0;
3222
3223 ret = __sys_connect_file(req->file, &io->connect.address,
3224 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07003225 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003226 if (req->io)
3227 return -EAGAIN;
3228 if (io_alloc_async_ctx(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07003229 ret = -ENOMEM;
3230 goto out;
3231 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003232 memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect));
Jens Axboef8e85cf2019-11-23 14:24:24 -07003233 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07003234 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07003235 if (ret == -ERESTARTSYS)
3236 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07003237out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003238 if (ret < 0)
3239 req_set_fail_links(req);
Jens Axboef8e85cf2019-11-23 14:24:24 -07003240 io_cqring_add_event(req, ret);
3241 io_put_req_find_next(req, nxt);
3242 return 0;
3243#else
3244 return -EOPNOTSUPP;
3245#endif
3246}
3247
Jens Axboe221c5eb2019-01-17 09:41:58 -07003248static void io_poll_remove_one(struct io_kiocb *req)
3249{
3250 struct io_poll_iocb *poll = &req->poll;
3251
3252 spin_lock(&poll->head->lock);
3253 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07003254 if (!list_empty(&poll->wait.entry)) {
3255 list_del_init(&poll->wait.entry);
Jackie Liua197f662019-11-08 08:09:12 -07003256 io_queue_async_work(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003257 }
3258 spin_unlock(&poll->head->lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07003259 hash_del(&req->hash_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003260}
3261
3262static void io_poll_remove_all(struct io_ring_ctx *ctx)
3263{
Jens Axboe78076bb2019-12-04 19:56:40 -07003264 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003265 struct io_kiocb *req;
Jens Axboe78076bb2019-12-04 19:56:40 -07003266 int i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003267
3268 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07003269 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
3270 struct hlist_head *list;
3271
3272 list = &ctx->cancel_hash[i];
3273 hlist_for_each_entry_safe(req, tmp, list, hash_node)
3274 io_poll_remove_one(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003275 }
3276 spin_unlock_irq(&ctx->completion_lock);
3277}
3278
Jens Axboe47f46762019-11-09 17:43:02 -07003279static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
3280{
Jens Axboe78076bb2019-12-04 19:56:40 -07003281 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07003282 struct io_kiocb *req;
3283
Jens Axboe78076bb2019-12-04 19:56:40 -07003284 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
3285 hlist_for_each_entry(req, list, hash_node) {
3286 if (sqe_addr == req->user_data) {
Jens Axboeeac406c2019-11-14 12:09:58 -07003287 io_poll_remove_one(req);
3288 return 0;
3289 }
Jens Axboe47f46762019-11-09 17:43:02 -07003290 }
3291
3292 return -ENOENT;
3293}
3294
Jens Axboe3529d8c2019-12-19 18:24:38 -07003295static int io_poll_remove_prep(struct io_kiocb *req,
3296 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003297{
Jens Axboe221c5eb2019-01-17 09:41:58 -07003298 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3299 return -EINVAL;
3300 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
3301 sqe->poll_events)
3302 return -EINVAL;
3303
Jens Axboe0969e782019-12-17 18:40:57 -07003304 req->poll.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07003305 return 0;
3306}
3307
3308/*
3309 * Find a running poll command that matches one specified in sqe->addr,
3310 * and remove it if found.
3311 */
3312static int io_poll_remove(struct io_kiocb *req)
3313{
3314 struct io_ring_ctx *ctx = req->ctx;
3315 u64 addr;
3316 int ret;
3317
Jens Axboe0969e782019-12-17 18:40:57 -07003318 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003319 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07003320 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003321 spin_unlock_irq(&ctx->completion_lock);
3322
Jens Axboe78e19bb2019-11-06 15:21:34 -07003323 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003324 if (ret < 0)
3325 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06003326 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003327 return 0;
3328}
3329
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003330static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003331{
Jackie Liua197f662019-11-08 08:09:12 -07003332 struct io_ring_ctx *ctx = req->ctx;
3333
Jens Axboe8c838782019-03-12 15:48:16 -06003334 req->poll.done = true;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003335 if (error)
3336 io_cqring_fill_event(req, error);
3337 else
3338 io_cqring_fill_event(req, mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06003339 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003340}
3341
Jens Axboe561fb042019-10-24 07:25:42 -06003342static void io_poll_complete_work(struct io_wq_work **workptr)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003343{
Jens Axboe561fb042019-10-24 07:25:42 -06003344 struct io_wq_work *work = *workptr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003345 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
3346 struct io_poll_iocb *poll = &req->poll;
3347 struct poll_table_struct pt = { ._key = poll->events };
3348 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe89723d02019-11-05 15:32:58 -07003349 struct io_kiocb *nxt = NULL;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003350 __poll_t mask = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003351 int ret = 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003352
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003353 if (work->flags & IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06003354 WRITE_ONCE(poll->canceled, true);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003355 ret = -ECANCELED;
3356 } else if (READ_ONCE(poll->canceled)) {
3357 ret = -ECANCELED;
3358 }
Jens Axboe561fb042019-10-24 07:25:42 -06003359
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003360 if (ret != -ECANCELED)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003361 mask = vfs_poll(poll->file, &pt) & poll->events;
3362
3363 /*
3364 * Note that ->ki_cancel callers also delete iocb from active_reqs after
3365 * calling ->ki_cancel. We need the ctx_lock roundtrip here to
3366 * synchronize with them. In the cancellation case the list_del_init
3367 * itself is not actually needed, but harmless so we keep it in to
3368 * avoid further branches in the fast path.
3369 */
3370 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003371 if (!mask && ret != -ECANCELED) {
Jens Axboe392edb42019-12-09 17:52:20 -07003372 add_wait_queue(poll->head, &poll->wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003373 spin_unlock_irq(&ctx->completion_lock);
3374 return;
3375 }
Jens Axboe78076bb2019-12-04 19:56:40 -07003376 hash_del(&req->hash_node);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003377 io_poll_complete(req, mask, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003378 spin_unlock_irq(&ctx->completion_lock);
3379
Jens Axboe8c838782019-03-12 15:48:16 -06003380 io_cqring_ev_posted(ctx);
Jens Axboe89723d02019-11-05 15:32:58 -07003381
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003382 if (ret < 0)
3383 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003384 io_put_req_find_next(req, &nxt);
Jens Axboe89723d02019-11-05 15:32:58 -07003385 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07003386 io_wq_assign_next(workptr, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003387}
3388
Jens Axboee94f1412019-12-19 12:06:02 -07003389static void __io_poll_flush(struct io_ring_ctx *ctx, struct llist_node *nodes)
3390{
Jens Axboee94f1412019-12-19 12:06:02 -07003391 struct io_kiocb *req, *tmp;
Jens Axboe8237e042019-12-28 10:48:22 -07003392 struct req_batch rb;
Jens Axboee94f1412019-12-19 12:06:02 -07003393
Jens Axboec6ca97b302019-12-28 12:11:08 -07003394 rb.to_free = rb.need_iter = 0;
Jens Axboee94f1412019-12-19 12:06:02 -07003395 spin_lock_irq(&ctx->completion_lock);
3396 llist_for_each_entry_safe(req, tmp, nodes, llist_node) {
3397 hash_del(&req->hash_node);
3398 io_poll_complete(req, req->result, 0);
3399
Jens Axboe8237e042019-12-28 10:48:22 -07003400 if (refcount_dec_and_test(&req->refs) &&
3401 !io_req_multi_free(&rb, req)) {
3402 req->flags |= REQ_F_COMP_LOCKED;
3403 io_free_req(req);
Jens Axboee94f1412019-12-19 12:06:02 -07003404 }
3405 }
3406 spin_unlock_irq(&ctx->completion_lock);
3407
3408 io_cqring_ev_posted(ctx);
Jens Axboe8237e042019-12-28 10:48:22 -07003409 io_free_req_many(ctx, &rb);
Jens Axboee94f1412019-12-19 12:06:02 -07003410}
3411
3412static void io_poll_flush(struct io_wq_work **workptr)
3413{
3414 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3415 struct llist_node *nodes;
3416
3417 nodes = llist_del_all(&req->ctx->poll_llist);
3418 if (nodes)
3419 __io_poll_flush(req->ctx, nodes);
3420}
3421
Jens Axboe221c5eb2019-01-17 09:41:58 -07003422static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
3423 void *key)
3424{
Jens Axboee9444752019-11-26 15:02:04 -07003425 struct io_poll_iocb *poll = wait->private;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003426 struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
3427 struct io_ring_ctx *ctx = req->ctx;
3428 __poll_t mask = key_to_poll(key);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003429
3430 /* for instances that support it check for an event match first: */
Jens Axboe8c838782019-03-12 15:48:16 -06003431 if (mask && !(mask & poll->events))
3432 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003433
Jens Axboe392edb42019-12-09 17:52:20 -07003434 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06003435
Jens Axboe7c9e7f02019-11-12 08:15:53 -07003436 /*
3437 * Run completion inline if we can. We're using trylock here because
3438 * we are violating the completion_lock -> poll wq lock ordering.
3439 * If we have a link timeout we're going to need the completion_lock
3440 * for finalizing the request, mark us as having grabbed that already.
3441 */
Jens Axboee94f1412019-12-19 12:06:02 -07003442 if (mask) {
3443 unsigned long flags;
Jens Axboe8c838782019-03-12 15:48:16 -06003444
Jens Axboee94f1412019-12-19 12:06:02 -07003445 if (llist_empty(&ctx->poll_llist) &&
3446 spin_trylock_irqsave(&ctx->completion_lock, flags)) {
3447 hash_del(&req->hash_node);
3448 io_poll_complete(req, mask, 0);
3449 req->flags |= REQ_F_COMP_LOCKED;
3450 io_put_req(req);
3451 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3452
3453 io_cqring_ev_posted(ctx);
3454 req = NULL;
3455 } else {
3456 req->result = mask;
3457 req->llist_node.next = NULL;
3458 /* if the list wasn't empty, we're done */
3459 if (!llist_add(&req->llist_node, &ctx->poll_llist))
3460 req = NULL;
3461 else
3462 req->work.func = io_poll_flush;
3463 }
Jens Axboe8c838782019-03-12 15:48:16 -06003464 }
Jens Axboee94f1412019-12-19 12:06:02 -07003465 if (req)
3466 io_queue_async_work(req);
Jens Axboe8c838782019-03-12 15:48:16 -06003467
Jens Axboe221c5eb2019-01-17 09:41:58 -07003468 return 1;
3469}
3470
3471struct io_poll_table {
3472 struct poll_table_struct pt;
3473 struct io_kiocb *req;
3474 int error;
3475};
3476
3477static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
3478 struct poll_table_struct *p)
3479{
3480 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
3481
3482 if (unlikely(pt->req->poll.head)) {
3483 pt->error = -EINVAL;
3484 return;
3485 }
3486
3487 pt->error = 0;
3488 pt->req->poll.head = head;
Jens Axboe392edb42019-12-09 17:52:20 -07003489 add_wait_queue(head, &pt->req->poll.wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003490}
3491
Jens Axboeeac406c2019-11-14 12:09:58 -07003492static void io_poll_req_insert(struct io_kiocb *req)
3493{
3494 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07003495 struct hlist_head *list;
Jens Axboeeac406c2019-11-14 12:09:58 -07003496
Jens Axboe78076bb2019-12-04 19:56:40 -07003497 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
3498 hlist_add_head(&req->hash_node, list);
Jens Axboeeac406c2019-11-14 12:09:58 -07003499}
3500
Jens Axboe3529d8c2019-12-19 18:24:38 -07003501static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003502{
3503 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003504 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003505
3506 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3507 return -EINVAL;
3508 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
3509 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06003510 if (!poll->file)
3511 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003512
Jens Axboe221c5eb2019-01-17 09:41:58 -07003513 events = READ_ONCE(sqe->poll_events);
3514 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
Jens Axboe0969e782019-12-17 18:40:57 -07003515 return 0;
3516}
3517
3518static int io_poll_add(struct io_kiocb *req, struct io_kiocb **nxt)
3519{
3520 struct io_poll_iocb *poll = &req->poll;
3521 struct io_ring_ctx *ctx = req->ctx;
3522 struct io_poll_table ipt;
3523 bool cancel = false;
3524 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07003525
3526 INIT_IO_WORK(&req->work, io_poll_complete_work);
Jens Axboe78076bb2019-12-04 19:56:40 -07003527 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003528
Jens Axboe221c5eb2019-01-17 09:41:58 -07003529 poll->head = NULL;
Jens Axboe8c838782019-03-12 15:48:16 -06003530 poll->done = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003531 poll->canceled = false;
3532
3533 ipt.pt._qproc = io_poll_queue_proc;
3534 ipt.pt._key = poll->events;
3535 ipt.req = req;
3536 ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
3537
3538 /* initialized the list so that we can do list_empty checks */
Jens Axboe392edb42019-12-09 17:52:20 -07003539 INIT_LIST_HEAD(&poll->wait.entry);
3540 init_waitqueue_func_entry(&poll->wait, io_poll_wake);
3541 poll->wait.private = poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003542
Jens Axboe36703242019-07-25 10:20:18 -06003543 INIT_LIST_HEAD(&req->list);
3544
Jens Axboe221c5eb2019-01-17 09:41:58 -07003545 mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003546
3547 spin_lock_irq(&ctx->completion_lock);
Jens Axboe8c838782019-03-12 15:48:16 -06003548 if (likely(poll->head)) {
3549 spin_lock(&poll->head->lock);
Jens Axboe392edb42019-12-09 17:52:20 -07003550 if (unlikely(list_empty(&poll->wait.entry))) {
Jens Axboe8c838782019-03-12 15:48:16 -06003551 if (ipt.error)
3552 cancel = true;
3553 ipt.error = 0;
3554 mask = 0;
3555 }
3556 if (mask || ipt.error)
Jens Axboe392edb42019-12-09 17:52:20 -07003557 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06003558 else if (cancel)
3559 WRITE_ONCE(poll->canceled, true);
3560 else if (!poll->done) /* actually waiting for an event */
Jens Axboeeac406c2019-11-14 12:09:58 -07003561 io_poll_req_insert(req);
Jens Axboe8c838782019-03-12 15:48:16 -06003562 spin_unlock(&poll->head->lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003563 }
Jens Axboe8c838782019-03-12 15:48:16 -06003564 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06003565 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003566 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06003567 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07003568 spin_unlock_irq(&ctx->completion_lock);
3569
Jens Axboe8c838782019-03-12 15:48:16 -06003570 if (mask) {
3571 io_cqring_ev_posted(ctx);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003572 io_put_req_find_next(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003573 }
Jens Axboe8c838782019-03-12 15:48:16 -06003574 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003575}
3576
Jens Axboe5262f562019-09-17 12:26:57 -06003577static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
3578{
Jens Axboead8a48a2019-11-15 08:49:11 -07003579 struct io_timeout_data *data = container_of(timer,
3580 struct io_timeout_data, timer);
3581 struct io_kiocb *req = data->req;
3582 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06003583 unsigned long flags;
3584
Jens Axboe5262f562019-09-17 12:26:57 -06003585 atomic_inc(&ctx->cq_timeouts);
3586
3587 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08003588 /*
Jens Axboe11365042019-10-16 09:08:32 -06003589 * We could be racing with timeout deletion. If the list is empty,
3590 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08003591 */
Jens Axboe842f9612019-10-29 12:34:10 -06003592 if (!list_empty(&req->list)) {
Jens Axboe11365042019-10-16 09:08:32 -06003593 struct io_kiocb *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06003594
Jens Axboe11365042019-10-16 09:08:32 -06003595 /*
3596 * Adjust the reqs sequence before the current one because it
Brian Gianforcarod195a662019-12-13 03:09:50 -08003597 * will consume a slot in the cq_ring and the cq_tail
Jens Axboe11365042019-10-16 09:08:32 -06003598 * pointer will be increased, otherwise other timeout reqs may
3599 * return in advance without waiting for enough wait_nr.
3600 */
3601 prev = req;
3602 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
3603 prev->sequence++;
Jens Axboe11365042019-10-16 09:08:32 -06003604 list_del_init(&req->list);
Jens Axboe11365042019-10-16 09:08:32 -06003605 }
Jens Axboe842f9612019-10-29 12:34:10 -06003606
Jens Axboe78e19bb2019-11-06 15:21:34 -07003607 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06003608 io_commit_cqring(ctx);
3609 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3610
3611 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003612 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06003613 io_put_req(req);
3614 return HRTIMER_NORESTART;
3615}
3616
Jens Axboe47f46762019-11-09 17:43:02 -07003617static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
3618{
3619 struct io_kiocb *req;
3620 int ret = -ENOENT;
3621
3622 list_for_each_entry(req, &ctx->timeout_list, list) {
3623 if (user_data == req->user_data) {
3624 list_del_init(&req->list);
3625 ret = 0;
3626 break;
3627 }
3628 }
3629
3630 if (ret == -ENOENT)
3631 return ret;
3632
Jens Axboe2d283902019-12-04 11:08:05 -07003633 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe47f46762019-11-09 17:43:02 -07003634 if (ret == -1)
3635 return -EALREADY;
3636
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003637 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07003638 io_cqring_fill_event(req, -ECANCELED);
3639 io_put_req(req);
3640 return 0;
3641}
3642
Jens Axboe3529d8c2019-12-19 18:24:38 -07003643static int io_timeout_remove_prep(struct io_kiocb *req,
3644 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07003645{
Jens Axboeb29472e2019-12-17 18:50:29 -07003646 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3647 return -EINVAL;
3648 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
3649 return -EINVAL;
3650
3651 req->timeout.addr = READ_ONCE(sqe->addr);
3652 req->timeout.flags = READ_ONCE(sqe->timeout_flags);
3653 if (req->timeout.flags)
3654 return -EINVAL;
3655
Jens Axboeb29472e2019-12-17 18:50:29 -07003656 return 0;
3657}
3658
Jens Axboe11365042019-10-16 09:08:32 -06003659/*
3660 * Remove or update an existing timeout command
3661 */
Jens Axboefc4df992019-12-10 14:38:45 -07003662static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06003663{
3664 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07003665 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06003666
Jens Axboe11365042019-10-16 09:08:32 -06003667 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb29472e2019-12-17 18:50:29 -07003668 ret = io_timeout_cancel(ctx, req->timeout.addr);
Jens Axboe11365042019-10-16 09:08:32 -06003669
Jens Axboe47f46762019-11-09 17:43:02 -07003670 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06003671 io_commit_cqring(ctx);
3672 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06003673 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003674 if (ret < 0)
3675 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003676 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06003677 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06003678}
3679
Jens Axboe3529d8c2019-12-19 18:24:38 -07003680static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07003681 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06003682{
Jens Axboead8a48a2019-11-15 08:49:11 -07003683 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06003684 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06003685
Jens Axboead8a48a2019-11-15 08:49:11 -07003686 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06003687 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07003688 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06003689 return -EINVAL;
Jens Axboe2d283902019-12-04 11:08:05 -07003690 if (sqe->off && is_timeout_link)
3691 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06003692 flags = READ_ONCE(sqe->timeout_flags);
3693 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06003694 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06003695
Jens Axboe26a61672019-12-20 09:02:01 -07003696 req->timeout.count = READ_ONCE(sqe->off);
3697
Jens Axboe3529d8c2019-12-19 18:24:38 -07003698 if (!req->io && io_alloc_async_ctx(req))
Jens Axboe26a61672019-12-20 09:02:01 -07003699 return -ENOMEM;
3700
3701 data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07003702 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07003703 req->flags |= REQ_F_TIMEOUT;
3704
3705 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06003706 return -EFAULT;
3707
Jens Axboe11365042019-10-16 09:08:32 -06003708 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07003709 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06003710 else
Jens Axboead8a48a2019-11-15 08:49:11 -07003711 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06003712
Jens Axboead8a48a2019-11-15 08:49:11 -07003713 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
3714 return 0;
3715}
3716
Jens Axboefc4df992019-12-10 14:38:45 -07003717static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07003718{
3719 unsigned count;
3720 struct io_ring_ctx *ctx = req->ctx;
3721 struct io_timeout_data *data;
3722 struct list_head *entry;
3723 unsigned span = 0;
Jens Axboead8a48a2019-11-15 08:49:11 -07003724
Jens Axboe2d283902019-12-04 11:08:05 -07003725 data = &req->io->timeout;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003726
Jens Axboe5262f562019-09-17 12:26:57 -06003727 /*
3728 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07003729 * timeout event to be satisfied. If it isn't set, then this is
3730 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06003731 */
Jens Axboe26a61672019-12-20 09:02:01 -07003732 count = req->timeout.count;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003733 if (!count) {
3734 req->flags |= REQ_F_TIMEOUT_NOSEQ;
3735 spin_lock_irq(&ctx->completion_lock);
3736 entry = ctx->timeout_list.prev;
3737 goto add;
3738 }
Jens Axboe5262f562019-09-17 12:26:57 -06003739
3740 req->sequence = ctx->cached_sq_head + count - 1;
Jens Axboe2d283902019-12-04 11:08:05 -07003741 data->seq_offset = count;
Jens Axboe5262f562019-09-17 12:26:57 -06003742
3743 /*
3744 * Insertion sort, ensuring the first entry in the list is always
3745 * the one we need first.
3746 */
Jens Axboe5262f562019-09-17 12:26:57 -06003747 spin_lock_irq(&ctx->completion_lock);
3748 list_for_each_prev(entry, &ctx->timeout_list) {
3749 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
yangerkun5da0fb12019-10-15 21:59:29 +08003750 unsigned nxt_sq_head;
3751 long long tmp, tmp_nxt;
Jens Axboe2d283902019-12-04 11:08:05 -07003752 u32 nxt_offset = nxt->io->timeout.seq_offset;
Jens Axboe5262f562019-09-17 12:26:57 -06003753
Jens Axboe93bd25b2019-11-11 23:34:31 -07003754 if (nxt->flags & REQ_F_TIMEOUT_NOSEQ)
3755 continue;
3756
yangerkun5da0fb12019-10-15 21:59:29 +08003757 /*
3758 * Since cached_sq_head + count - 1 can overflow, use type long
3759 * long to store it.
3760 */
3761 tmp = (long long)ctx->cached_sq_head + count - 1;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +03003762 nxt_sq_head = nxt->sequence - nxt_offset + 1;
3763 tmp_nxt = (long long)nxt_sq_head + nxt_offset - 1;
yangerkun5da0fb12019-10-15 21:59:29 +08003764
3765 /*
3766 * cached_sq_head may overflow, and it will never overflow twice
3767 * once there is some timeout req still be valid.
3768 */
3769 if (ctx->cached_sq_head < nxt_sq_head)
yangerkun8b07a652019-10-17 12:12:35 +08003770 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08003771
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003772 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06003773 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003774
3775 /*
3776 * Sequence of reqs after the insert one and itself should
3777 * be adjusted because each timeout req consumes a slot.
3778 */
3779 span++;
3780 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06003781 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003782 req->sequence -= span;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003783add:
Jens Axboe5262f562019-09-17 12:26:57 -06003784 list_add(&req->list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07003785 data->timer.function = io_timeout_fn;
3786 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06003787 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06003788 return 0;
3789}
3790
Jens Axboe62755e32019-10-28 21:49:21 -06003791static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06003792{
Jens Axboe62755e32019-10-28 21:49:21 -06003793 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06003794
Jens Axboe62755e32019-10-28 21:49:21 -06003795 return req->user_data == (unsigned long) data;
3796}
3797
Jens Axboee977d6d2019-11-05 12:39:45 -07003798static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06003799{
Jens Axboe62755e32019-10-28 21:49:21 -06003800 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06003801 int ret = 0;
3802
Jens Axboe62755e32019-10-28 21:49:21 -06003803 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
3804 switch (cancel_ret) {
3805 case IO_WQ_CANCEL_OK:
3806 ret = 0;
3807 break;
3808 case IO_WQ_CANCEL_RUNNING:
3809 ret = -EALREADY;
3810 break;
3811 case IO_WQ_CANCEL_NOTFOUND:
3812 ret = -ENOENT;
3813 break;
3814 }
3815
Jens Axboee977d6d2019-11-05 12:39:45 -07003816 return ret;
3817}
3818
Jens Axboe47f46762019-11-09 17:43:02 -07003819static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
3820 struct io_kiocb *req, __u64 sqe_addr,
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003821 struct io_kiocb **nxt, int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07003822{
3823 unsigned long flags;
3824 int ret;
3825
3826 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
3827 if (ret != -ENOENT) {
3828 spin_lock_irqsave(&ctx->completion_lock, flags);
3829 goto done;
3830 }
3831
3832 spin_lock_irqsave(&ctx->completion_lock, flags);
3833 ret = io_timeout_cancel(ctx, sqe_addr);
3834 if (ret != -ENOENT)
3835 goto done;
3836 ret = io_poll_cancel(ctx, sqe_addr);
3837done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003838 if (!ret)
3839 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07003840 io_cqring_fill_event(req, ret);
3841 io_commit_cqring(ctx);
3842 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3843 io_cqring_ev_posted(ctx);
3844
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003845 if (ret < 0)
3846 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07003847 io_put_req_find_next(req, nxt);
3848}
3849
Jens Axboe3529d8c2019-12-19 18:24:38 -07003850static int io_async_cancel_prep(struct io_kiocb *req,
3851 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07003852{
Jens Axboefbf23842019-12-17 18:45:56 -07003853 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07003854 return -EINVAL;
3855 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
3856 sqe->cancel_flags)
3857 return -EINVAL;
3858
Jens Axboefbf23842019-12-17 18:45:56 -07003859 req->cancel.addr = READ_ONCE(sqe->addr);
3860 return 0;
3861}
3862
3863static int io_async_cancel(struct io_kiocb *req, struct io_kiocb **nxt)
3864{
3865 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07003866
3867 io_async_find_and_cancel(ctx, req, req->cancel.addr, nxt, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06003868 return 0;
3869}
3870
Jens Axboe05f3fb32019-12-09 11:22:50 -07003871static int io_files_update_prep(struct io_kiocb *req,
3872 const struct io_uring_sqe *sqe)
3873{
3874 if (sqe->flags || sqe->ioprio || sqe->rw_flags)
3875 return -EINVAL;
3876
3877 req->files_update.offset = READ_ONCE(sqe->off);
3878 req->files_update.nr_args = READ_ONCE(sqe->len);
3879 if (!req->files_update.nr_args)
3880 return -EINVAL;
3881 req->files_update.arg = READ_ONCE(sqe->addr);
3882 return 0;
3883}
3884
3885static int io_files_update(struct io_kiocb *req, bool force_nonblock)
3886{
3887 struct io_ring_ctx *ctx = req->ctx;
3888 struct io_uring_files_update up;
3889 int ret;
3890
3891 if (force_nonblock) {
3892 req->work.flags |= IO_WQ_WORK_NEEDS_FILES;
3893 return -EAGAIN;
3894 }
3895
3896 up.offset = req->files_update.offset;
3897 up.fds = req->files_update.arg;
3898
3899 mutex_lock(&ctx->uring_lock);
3900 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
3901 mutex_unlock(&ctx->uring_lock);
3902
3903 if (ret < 0)
3904 req_set_fail_links(req);
3905 io_cqring_add_event(req, ret);
3906 io_put_req(req);
3907 return 0;
3908}
3909
Jens Axboe3529d8c2019-12-19 18:24:38 -07003910static int io_req_defer_prep(struct io_kiocb *req,
3911 const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07003912{
Jens Axboee7815732019-12-17 19:45:06 -07003913 ssize_t ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07003914
Jens Axboed625c6e2019-12-17 19:53:05 -07003915 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07003916 case IORING_OP_NOP:
3917 break;
Jens Axboef67676d2019-12-02 11:03:47 -07003918 case IORING_OP_READV:
3919 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07003920 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003921 ret = io_read_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07003922 break;
3923 case IORING_OP_WRITEV:
3924 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07003925 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003926 ret = io_write_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07003927 break;
Jens Axboe0969e782019-12-17 18:40:57 -07003928 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003929 ret = io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07003930 break;
3931 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003932 ret = io_poll_remove_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07003933 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003934 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003935 ret = io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003936 break;
3937 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003938 ret = io_prep_sfr(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003939 break;
Jens Axboe03b12302019-12-02 18:50:25 -07003940 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07003941 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003942 ret = io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07003943 break;
3944 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07003945 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003946 ret = io_recvmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07003947 break;
Jens Axboef499a022019-12-02 16:28:46 -07003948 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003949 ret = io_connect_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07003950 break;
Jens Axboe2d283902019-12-04 11:08:05 -07003951 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003952 ret = io_timeout_prep(req, sqe, false);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003953 break;
Jens Axboeb29472e2019-12-17 18:50:29 -07003954 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003955 ret = io_timeout_remove_prep(req, sqe);
Jens Axboeb29472e2019-12-17 18:50:29 -07003956 break;
Jens Axboefbf23842019-12-17 18:45:56 -07003957 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003958 ret = io_async_cancel_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07003959 break;
Jens Axboe2d283902019-12-04 11:08:05 -07003960 case IORING_OP_LINK_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003961 ret = io_timeout_prep(req, sqe, true);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003962 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003963 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07003964 ret = io_accept_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003965 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07003966 case IORING_OP_FALLOCATE:
3967 ret = io_fallocate_prep(req, sqe);
3968 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07003969 case IORING_OP_OPENAT:
3970 ret = io_openat_prep(req, sqe);
3971 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07003972 case IORING_OP_CLOSE:
3973 ret = io_close_prep(req, sqe);
3974 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07003975 case IORING_OP_FILES_UPDATE:
3976 ret = io_files_update_prep(req, sqe);
3977 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07003978 case IORING_OP_STATX:
3979 ret = io_statx_prep(req, sqe);
3980 break;
Jens Axboe4840e412019-12-25 22:03:45 -07003981 case IORING_OP_FADVISE:
3982 ret = io_fadvise_prep(req, sqe);
3983 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07003984 case IORING_OP_MADVISE:
3985 ret = io_madvise_prep(req, sqe);
3986 break;
Jens Axboef67676d2019-12-02 11:03:47 -07003987 default:
Jens Axboee7815732019-12-17 19:45:06 -07003988 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
3989 req->opcode);
3990 ret = -EINVAL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003991 break;
Jens Axboef67676d2019-12-02 11:03:47 -07003992 }
3993
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003994 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07003995}
3996
Jens Axboe3529d8c2019-12-19 18:24:38 -07003997static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboede0617e2019-04-06 21:51:27 -06003998{
Jackie Liua197f662019-11-08 08:09:12 -07003999 struct io_ring_ctx *ctx = req->ctx;
Jens Axboef67676d2019-12-02 11:03:47 -07004000 int ret;
Jens Axboede0617e2019-04-06 21:51:27 -06004001
Bob Liu9d858b22019-11-13 18:06:25 +08004002 /* Still need defer if there is pending req in defer list. */
4003 if (!req_need_defer(req) && list_empty(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06004004 return 0;
4005
Jens Axboe3529d8c2019-12-19 18:24:38 -07004006 if (!req->io && io_alloc_async_ctx(req))
Jens Axboede0617e2019-04-06 21:51:27 -06004007 return -EAGAIN;
4008
Jens Axboe3529d8c2019-12-19 18:24:38 -07004009 ret = io_req_defer_prep(req, sqe);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004010 if (ret < 0)
Jens Axboe2d283902019-12-04 11:08:05 -07004011 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07004012
Jens Axboede0617e2019-04-06 21:51:27 -06004013 spin_lock_irq(&ctx->completion_lock);
Bob Liu9d858b22019-11-13 18:06:25 +08004014 if (!req_need_defer(req) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06004015 spin_unlock_irq(&ctx->completion_lock);
Jens Axboede0617e2019-04-06 21:51:27 -06004016 return 0;
4017 }
4018
Jens Axboe915967f2019-11-21 09:01:20 -07004019 trace_io_uring_defer(ctx, req, req->user_data);
Jens Axboede0617e2019-04-06 21:51:27 -06004020 list_add_tail(&req->list, &ctx->defer_list);
4021 spin_unlock_irq(&ctx->completion_lock);
4022 return -EIOCBQUEUED;
4023}
4024
Jens Axboe3529d8c2019-12-19 18:24:38 -07004025static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
4026 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004027{
Jackie Liua197f662019-11-08 08:09:12 -07004028 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07004029 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004030
Jens Axboed625c6e2019-12-17 19:53:05 -07004031 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07004032 case IORING_OP_NOP:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004033 ret = io_nop(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004034 break;
4035 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004036 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004037 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004038 if (sqe) {
4039 ret = io_read_prep(req, sqe, force_nonblock);
4040 if (ret < 0)
4041 break;
4042 }
Pavel Begunkov267bc902019-11-07 01:41:08 +03004043 ret = io_read(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004044 break;
4045 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07004046 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004047 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004048 if (sqe) {
4049 ret = io_write_prep(req, sqe, force_nonblock);
4050 if (ret < 0)
4051 break;
4052 }
Pavel Begunkov267bc902019-11-07 01:41:08 +03004053 ret = io_write(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004054 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004055 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004056 if (sqe) {
4057 ret = io_prep_fsync(req, sqe);
4058 if (ret < 0)
4059 break;
4060 }
Jens Axboefc4df992019-12-10 14:38:45 -07004061 ret = io_fsync(req, nxt, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004062 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004063 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004064 if (sqe) {
4065 ret = io_poll_add_prep(req, sqe);
4066 if (ret)
4067 break;
4068 }
Jens Axboefc4df992019-12-10 14:38:45 -07004069 ret = io_poll_add(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004070 break;
4071 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004072 if (sqe) {
4073 ret = io_poll_remove_prep(req, sqe);
4074 if (ret < 0)
4075 break;
4076 }
Jens Axboefc4df992019-12-10 14:38:45 -07004077 ret = io_poll_remove(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004078 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004079 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004080 if (sqe) {
4081 ret = io_prep_sfr(req, sqe);
4082 if (ret < 0)
4083 break;
4084 }
Jens Axboefc4df992019-12-10 14:38:45 -07004085 ret = io_sync_file_range(req, nxt, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004086 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004087 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004088 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004089 if (sqe) {
4090 ret = io_sendmsg_prep(req, sqe);
4091 if (ret < 0)
4092 break;
4093 }
Jens Axboefddafac2020-01-04 20:19:44 -07004094 if (req->opcode == IORING_OP_SENDMSG)
4095 ret = io_sendmsg(req, nxt, force_nonblock);
4096 else
4097 ret = io_send(req, nxt, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004098 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06004099 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004100 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004101 if (sqe) {
4102 ret = io_recvmsg_prep(req, sqe);
4103 if (ret)
4104 break;
4105 }
Jens Axboefddafac2020-01-04 20:19:44 -07004106 if (req->opcode == IORING_OP_RECVMSG)
4107 ret = io_recvmsg(req, nxt, force_nonblock);
4108 else
4109 ret = io_recv(req, nxt, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06004110 break;
Jens Axboe5262f562019-09-17 12:26:57 -06004111 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004112 if (sqe) {
4113 ret = io_timeout_prep(req, sqe, false);
4114 if (ret)
4115 break;
4116 }
Jens Axboefc4df992019-12-10 14:38:45 -07004117 ret = io_timeout(req);
Jens Axboe5262f562019-09-17 12:26:57 -06004118 break;
Jens Axboe11365042019-10-16 09:08:32 -06004119 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004120 if (sqe) {
4121 ret = io_timeout_remove_prep(req, sqe);
4122 if (ret)
4123 break;
4124 }
Jens Axboefc4df992019-12-10 14:38:45 -07004125 ret = io_timeout_remove(req);
Jens Axboe11365042019-10-16 09:08:32 -06004126 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06004127 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004128 if (sqe) {
4129 ret = io_accept_prep(req, sqe);
4130 if (ret)
4131 break;
4132 }
Jens Axboefc4df992019-12-10 14:38:45 -07004133 ret = io_accept(req, nxt, force_nonblock);
Jens Axboe17f2fe32019-10-17 14:42:58 -06004134 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004135 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004136 if (sqe) {
4137 ret = io_connect_prep(req, sqe);
4138 if (ret)
4139 break;
4140 }
Jens Axboefc4df992019-12-10 14:38:45 -07004141 ret = io_connect(req, nxt, force_nonblock);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004142 break;
Jens Axboe62755e32019-10-28 21:49:21 -06004143 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004144 if (sqe) {
4145 ret = io_async_cancel_prep(req, sqe);
4146 if (ret)
4147 break;
4148 }
Jens Axboefc4df992019-12-10 14:38:45 -07004149 ret = io_async_cancel(req, nxt);
Jens Axboe62755e32019-10-28 21:49:21 -06004150 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07004151 case IORING_OP_FALLOCATE:
4152 if (sqe) {
4153 ret = io_fallocate_prep(req, sqe);
4154 if (ret)
4155 break;
4156 }
4157 ret = io_fallocate(req, nxt, force_nonblock);
4158 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004159 case IORING_OP_OPENAT:
4160 if (sqe) {
4161 ret = io_openat_prep(req, sqe);
4162 if (ret)
4163 break;
4164 }
4165 ret = io_openat(req, nxt, force_nonblock);
4166 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07004167 case IORING_OP_CLOSE:
4168 if (sqe) {
4169 ret = io_close_prep(req, sqe);
4170 if (ret)
4171 break;
4172 }
4173 ret = io_close(req, nxt, force_nonblock);
4174 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004175 case IORING_OP_FILES_UPDATE:
4176 if (sqe) {
4177 ret = io_files_update_prep(req, sqe);
4178 if (ret)
4179 break;
4180 }
4181 ret = io_files_update(req, force_nonblock);
4182 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004183 case IORING_OP_STATX:
4184 if (sqe) {
4185 ret = io_statx_prep(req, sqe);
4186 if (ret)
4187 break;
4188 }
4189 ret = io_statx(req, nxt, force_nonblock);
4190 break;
Jens Axboe4840e412019-12-25 22:03:45 -07004191 case IORING_OP_FADVISE:
4192 if (sqe) {
4193 ret = io_fadvise_prep(req, sqe);
4194 if (ret)
4195 break;
4196 }
4197 ret = io_fadvise(req, nxt, force_nonblock);
4198 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07004199 case IORING_OP_MADVISE:
4200 if (sqe) {
4201 ret = io_madvise_prep(req, sqe);
4202 if (ret)
4203 break;
4204 }
4205 ret = io_madvise(req, nxt, force_nonblock);
4206 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004207 default:
4208 ret = -EINVAL;
4209 break;
4210 }
4211
Jens Axboedef596e2019-01-09 08:59:42 -07004212 if (ret)
4213 return ret;
4214
4215 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe11ba8202020-01-15 21:51:17 -07004216 const bool in_async = io_wq_current_is_worker();
4217
Jens Axboe9e645e112019-05-10 16:07:28 -06004218 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07004219 return -EAGAIN;
4220
Jens Axboe11ba8202020-01-15 21:51:17 -07004221 /* workqueue context doesn't hold uring_lock, grab it now */
4222 if (in_async)
4223 mutex_lock(&ctx->uring_lock);
4224
Jens Axboedef596e2019-01-09 08:59:42 -07004225 io_iopoll_req_issued(req);
Jens Axboe11ba8202020-01-15 21:51:17 -07004226
4227 if (in_async)
4228 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07004229 }
4230
4231 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004232}
4233
Jens Axboe561fb042019-10-24 07:25:42 -06004234static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboe31b51512019-01-18 22:56:34 -07004235{
Jens Axboe561fb042019-10-24 07:25:42 -06004236 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004237 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe561fb042019-10-24 07:25:42 -06004238 struct io_kiocb *nxt = NULL;
4239 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004240
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07004241 /* if NO_CANCEL is set, we must still run the work */
4242 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
4243 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06004244 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07004245 }
Jens Axboe31b51512019-01-18 22:56:34 -07004246
Jens Axboe561fb042019-10-24 07:25:42 -06004247 if (!ret) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004248 req->has_user = (work->flags & IO_WQ_WORK_HAS_MM) != 0;
4249 req->in_async = true;
Jens Axboe561fb042019-10-24 07:25:42 -06004250 do {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004251 ret = io_issue_sqe(req, NULL, &nxt, false);
Jens Axboe561fb042019-10-24 07:25:42 -06004252 /*
4253 * We can get EAGAIN for polled IO even though we're
4254 * forcing a sync submission from here, since we can't
4255 * wait for request slots on the block side.
4256 */
4257 if (ret != -EAGAIN)
4258 break;
4259 cond_resched();
4260 } while (1);
4261 }
Jens Axboe31b51512019-01-18 22:56:34 -07004262
Jens Axboe561fb042019-10-24 07:25:42 -06004263 /* drop submission reference */
Jackie Liuec9c02a2019-11-08 23:50:36 +08004264 io_put_req(req);
Jens Axboe817869d2019-04-30 14:44:05 -06004265
Jens Axboe561fb042019-10-24 07:25:42 -06004266 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004267 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07004268 io_cqring_add_event(req, ret);
Jens Axboe817869d2019-04-30 14:44:05 -06004269 io_put_req(req);
Jens Axboeedafcce2019-01-09 09:16:05 -07004270 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004271
Jens Axboe561fb042019-10-24 07:25:42 -06004272 /* if a dependent link is ready, pass it back */
Jens Axboe78912932020-01-14 22:09:06 -07004273 if (!ret && nxt)
4274 io_wq_assign_next(workptr, nxt);
Jens Axboe31b51512019-01-18 22:56:34 -07004275}
Jens Axboe2b188cc2019-01-07 10:46:33 -07004276
Jens Axboe15b71ab2019-12-11 11:20:36 -07004277static int io_req_needs_file(struct io_kiocb *req, int fd)
Jens Axboe09bb8392019-03-13 12:39:28 -06004278{
Jens Axboed3656342019-12-18 09:50:26 -07004279 if (!io_op_defs[req->opcode].needs_file)
Jens Axboe9e3aa612019-12-11 15:55:43 -07004280 return 0;
Jens Axboed3656342019-12-18 09:50:26 -07004281 if (fd == -1 && io_op_defs[req->opcode].fd_non_neg)
4282 return 0;
4283 return 1;
Jens Axboe09bb8392019-03-13 12:39:28 -06004284}
4285
Jens Axboe65e19f52019-10-26 07:20:21 -06004286static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
4287 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06004288{
Jens Axboe65e19f52019-10-26 07:20:21 -06004289 struct fixed_file_table *table;
4290
Jens Axboe05f3fb32019-12-09 11:22:50 -07004291 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
4292 return table->files[index & IORING_FILE_TABLE_MASK];;
Jens Axboe65e19f52019-10-26 07:20:21 -06004293}
4294
Jens Axboe3529d8c2019-12-19 18:24:38 -07004295static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
4296 const struct io_uring_sqe *sqe)
Jens Axboe09bb8392019-03-13 12:39:28 -06004297{
Jackie Liua197f662019-11-08 08:09:12 -07004298 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe09bb8392019-03-13 12:39:28 -06004299 unsigned flags;
Jens Axboed3656342019-12-18 09:50:26 -07004300 int fd;
Jens Axboe09bb8392019-03-13 12:39:28 -06004301
Jens Axboe3529d8c2019-12-19 18:24:38 -07004302 flags = READ_ONCE(sqe->flags);
4303 fd = READ_ONCE(sqe->fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06004304
Jackie Liu4fe2c962019-09-09 20:50:40 +08004305 if (flags & IOSQE_IO_DRAIN)
Jens Axboede0617e2019-04-06 21:51:27 -06004306 req->flags |= REQ_F_IO_DRAIN;
Jens Axboede0617e2019-04-06 21:51:27 -06004307
Jens Axboed3656342019-12-18 09:50:26 -07004308 if (!io_req_needs_file(req, fd))
4309 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06004310
4311 if (flags & IOSQE_FIXED_FILE) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07004312 if (unlikely(!ctx->file_data ||
Jens Axboe09bb8392019-03-13 12:39:28 -06004313 (unsigned) fd >= ctx->nr_user_files))
4314 return -EBADF;
Jens Axboeb7620122019-10-26 07:22:55 -06004315 fd = array_index_nospec(fd, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06004316 req->file = io_file_from_index(ctx, fd);
4317 if (!req->file)
Jens Axboe08a45172019-10-03 08:11:03 -06004318 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06004319 req->flags |= REQ_F_FIXED_FILE;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004320 percpu_ref_get(&ctx->file_data->refs);
Jens Axboe09bb8392019-03-13 12:39:28 -06004321 } else {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004322 if (req->needs_fixed_file)
Jens Axboe09bb8392019-03-13 12:39:28 -06004323 return -EBADF;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004324 trace_io_uring_file_get(ctx, fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06004325 req->file = io_file_get(state, fd);
4326 if (unlikely(!req->file))
4327 return -EBADF;
4328 }
4329
4330 return 0;
4331}
4332
Jackie Liua197f662019-11-08 08:09:12 -07004333static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004334{
Jens Axboefcb323c2019-10-24 12:39:47 -06004335 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07004336 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06004337
Jens Axboeb5dba592019-12-11 14:02:38 -07004338 if (!req->ring_file)
4339 return -EBADF;
4340
Jens Axboefcb323c2019-10-24 12:39:47 -06004341 rcu_read_lock();
4342 spin_lock_irq(&ctx->inflight_lock);
4343 /*
4344 * We use the f_ops->flush() handler to ensure that we can flush
4345 * out work accessing these files if the fd is closed. Check if
4346 * the fd has changed since we started down this path, and disallow
4347 * this operation if it has.
4348 */
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004349 if (fcheck(req->ring_fd) == req->ring_file) {
Jens Axboefcb323c2019-10-24 12:39:47 -06004350 list_add(&req->inflight_entry, &ctx->inflight_list);
4351 req->flags |= REQ_F_INFLIGHT;
4352 req->work.files = current->files;
4353 ret = 0;
4354 }
4355 spin_unlock_irq(&ctx->inflight_lock);
4356 rcu_read_unlock();
4357
4358 return ret;
4359}
4360
Jens Axboe2665abf2019-11-05 12:40:47 -07004361static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
4362{
Jens Axboead8a48a2019-11-15 08:49:11 -07004363 struct io_timeout_data *data = container_of(timer,
4364 struct io_timeout_data, timer);
4365 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07004366 struct io_ring_ctx *ctx = req->ctx;
4367 struct io_kiocb *prev = NULL;
4368 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07004369
4370 spin_lock_irqsave(&ctx->completion_lock, flags);
4371
4372 /*
4373 * We don't expect the list to be empty, that will only happen if we
4374 * race with the completion of the linked work.
4375 */
Pavel Begunkov44932332019-12-05 16:16:35 +03004376 if (!list_empty(&req->link_list)) {
4377 prev = list_entry(req->link_list.prev, struct io_kiocb,
4378 link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07004379 if (refcount_inc_not_zero(&prev->refs)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03004380 list_del_init(&req->link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07004381 prev->flags &= ~REQ_F_LINK_TIMEOUT;
4382 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07004383 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07004384 }
4385
4386 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4387
4388 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004389 req_set_fail_links(prev);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004390 io_async_find_and_cancel(ctx, req, prev->user_data, NULL,
4391 -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07004392 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07004393 } else {
4394 io_cqring_add_event(req, -ETIME);
4395 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07004396 }
Jens Axboe2665abf2019-11-05 12:40:47 -07004397 return HRTIMER_NORESTART;
4398}
4399
Jens Axboead8a48a2019-11-15 08:49:11 -07004400static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07004401{
Jens Axboe76a46e02019-11-10 23:34:16 -07004402 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07004403
Jens Axboe76a46e02019-11-10 23:34:16 -07004404 /*
4405 * If the list is now empty, then our linked request finished before
4406 * we got a chance to setup the timer
4407 */
4408 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov44932332019-12-05 16:16:35 +03004409 if (!list_empty(&req->link_list)) {
Jens Axboe2d283902019-12-04 11:08:05 -07004410 struct io_timeout_data *data = &req->io->timeout;
Jens Axboe94ae5e72019-11-14 19:39:52 -07004411
Jens Axboead8a48a2019-11-15 08:49:11 -07004412 data->timer.function = io_link_timeout_fn;
4413 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
4414 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07004415 }
Jens Axboe76a46e02019-11-10 23:34:16 -07004416 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07004417
Jens Axboe2665abf2019-11-05 12:40:47 -07004418 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07004419 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07004420}
4421
Jens Axboead8a48a2019-11-15 08:49:11 -07004422static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07004423{
4424 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004425
Jens Axboe2665abf2019-11-05 12:40:47 -07004426 if (!(req->flags & REQ_F_LINK))
4427 return NULL;
4428
Pavel Begunkov44932332019-12-05 16:16:35 +03004429 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
4430 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07004431 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07004432 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07004433
Jens Axboe76a46e02019-11-10 23:34:16 -07004434 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07004435 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07004436}
4437
Jens Axboe3529d8c2019-12-19 18:24:38 -07004438static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004439{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004440 struct io_kiocb *linked_timeout;
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004441 struct io_kiocb *nxt = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004442 int ret;
4443
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004444again:
4445 linked_timeout = io_prep_linked_timeout(req);
4446
Jens Axboe3529d8c2019-12-19 18:24:38 -07004447 ret = io_issue_sqe(req, sqe, &nxt, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06004448
4449 /*
4450 * We async punt it if the file wasn't marked NOWAIT, or if the file
4451 * doesn't support non-blocking read/write attempts
4452 */
4453 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
4454 (req->flags & REQ_F_MUST_PUNT))) {
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03004455 if (req->work.flags & IO_WQ_WORK_NEEDS_FILES) {
4456 ret = io_grab_files(req);
4457 if (ret)
4458 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004459 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03004460
4461 /*
4462 * Queued up for async execution, worker will release
4463 * submit reference when the iocb is actually submitted.
4464 */
4465 io_queue_async_work(req);
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004466 goto done_req;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004467 }
Jens Axboee65ef562019-03-12 10:16:44 -06004468
Jens Axboefcb323c2019-10-24 12:39:47 -06004469err:
Jens Axboee65ef562019-03-12 10:16:44 -06004470 /* drop submission reference */
4471 io_put_req(req);
4472
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004473 if (linked_timeout) {
Jens Axboe76a46e02019-11-10 23:34:16 -07004474 if (!ret)
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004475 io_queue_linked_timeout(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07004476 else
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004477 io_put_req(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07004478 }
4479
Jens Axboee65ef562019-03-12 10:16:44 -06004480 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06004481 if (ret) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07004482 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004483 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06004484 io_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06004485 }
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004486done_req:
4487 if (nxt) {
4488 req = nxt;
4489 nxt = NULL;
4490 goto again;
4491 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004492}
4493
Jens Axboe3529d8c2019-12-19 18:24:38 -07004494static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jackie Liu4fe2c962019-09-09 20:50:40 +08004495{
4496 int ret;
4497
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004498 if (unlikely(req->ctx->drain_next)) {
4499 req->flags |= REQ_F_IO_DRAIN;
Jens Axboe69b3e542020-01-08 11:01:46 -07004500 req->ctx->drain_next = 0;
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004501 }
Jens Axboe69b3e542020-01-08 11:01:46 -07004502 req->ctx->drain_next = (req->flags & REQ_F_DRAIN_LINK) != 0;
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004503
Jens Axboe3529d8c2019-12-19 18:24:38 -07004504 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004505 if (ret) {
4506 if (ret != -EIOCBQUEUED) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07004507 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004508 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07004509 io_double_put_req(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004510 }
Pavel Begunkov25508782019-12-30 21:24:47 +03004511 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Jens Axboece35a472019-12-17 08:04:44 -07004512 /*
4513 * Never try inline submit of IOSQE_ASYNC is set, go straight
4514 * to async execution.
4515 */
4516 req->work.flags |= IO_WQ_WORK_CONCURRENT;
4517 io_queue_async_work(req);
4518 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004519 __io_queue_sqe(req, sqe);
Jens Axboece35a472019-12-17 08:04:44 -07004520 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08004521}
4522
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004523static inline void io_queue_link_head(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08004524{
Jens Axboe94ae5e72019-11-14 19:39:52 -07004525 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004526 io_cqring_add_event(req, -ECANCELED);
4527 io_double_put_req(req);
4528 } else
Jens Axboe3529d8c2019-12-19 18:24:38 -07004529 io_queue_sqe(req, NULL);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004530}
4531
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004532#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
Jens Axboece35a472019-12-17 08:04:44 -07004533 IOSQE_IO_HARDLINK | IOSQE_ASYNC)
Jens Axboe9e645e112019-05-10 16:07:28 -06004534
Jens Axboe3529d8c2019-12-19 18:24:38 -07004535static bool io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
4536 struct io_submit_state *state, struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06004537{
Jackie Liua197f662019-11-08 08:09:12 -07004538 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004539 unsigned int sqe_flags;
Jens Axboe9e645e112019-05-10 16:07:28 -06004540 int ret;
4541
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004542 sqe_flags = READ_ONCE(sqe->flags);
4543
Jens Axboe9e645e112019-05-10 16:07:28 -06004544 /* enforce forwards compatibility on users */
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004545 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06004546 ret = -EINVAL;
Pavel Begunkov196be952019-11-07 01:41:06 +03004547 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06004548 }
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004549 if (sqe_flags & IOSQE_ASYNC)
Jens Axboece35a472019-12-17 08:04:44 -07004550 req->flags |= REQ_F_FORCE_ASYNC;
Jens Axboe9e645e112019-05-10 16:07:28 -06004551
Jens Axboe3529d8c2019-12-19 18:24:38 -07004552 ret = io_req_set_file(state, req, sqe);
Jens Axboe9e645e112019-05-10 16:07:28 -06004553 if (unlikely(ret)) {
4554err_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004555 io_cqring_add_event(req, ret);
4556 io_double_put_req(req);
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004557 return false;
Jens Axboe9e645e112019-05-10 16:07:28 -06004558 }
4559
Jens Axboe9e645e112019-05-10 16:07:28 -06004560 /*
4561 * If we already have a head request, queue this one for async
4562 * submittal once the head completes. If we don't have a head but
4563 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
4564 * submitted sync once the chain is complete. If none of those
4565 * conditions are true (normal request), then just queue it.
4566 */
4567 if (*link) {
Pavel Begunkov9d763772019-12-17 02:22:07 +03004568 struct io_kiocb *head = *link;
Jens Axboe9e645e112019-05-10 16:07:28 -06004569
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004570 if (sqe_flags & IOSQE_IO_DRAIN)
Pavel Begunkov9d763772019-12-17 02:22:07 +03004571 head->flags |= REQ_F_DRAIN_LINK | REQ_F_IO_DRAIN;
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004572
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004573 if (sqe_flags & IOSQE_IO_HARDLINK)
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004574 req->flags |= REQ_F_HARDLINK;
4575
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004576 if (io_alloc_async_ctx(req)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06004577 ret = -EAGAIN;
4578 goto err_req;
4579 }
4580
Jens Axboe3529d8c2019-12-19 18:24:38 -07004581 ret = io_req_defer_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07004582 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004583 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03004584 head->flags |= REQ_F_FAIL_LINK;
Jens Axboef67676d2019-12-02 11:03:47 -07004585 goto err_req;
Jens Axboe2d283902019-12-04 11:08:05 -07004586 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03004587 trace_io_uring_link(ctx, req, head);
4588 list_add_tail(&req->link_list, &head->link_list);
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004589
4590 /* last request of a link, enqueue the link */
4591 if (!(sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK))) {
4592 io_queue_link_head(head);
4593 *link = NULL;
4594 }
4595 } else if (sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06004596 req->flags |= REQ_F_LINK;
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004597 if (sqe_flags & IOSQE_IO_HARDLINK)
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004598 req->flags |= REQ_F_HARDLINK;
Jens Axboe9e645e112019-05-10 16:07:28 -06004599
Jens Axboe9e645e112019-05-10 16:07:28 -06004600 INIT_LIST_HEAD(&req->link_list);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004601 ret = io_req_defer_prep(req, sqe);
4602 if (ret)
4603 req->flags |= REQ_F_FAIL_LINK;
Jens Axboe9e645e112019-05-10 16:07:28 -06004604 *link = req;
4605 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004606 io_queue_sqe(req, sqe);
Jens Axboe9e645e112019-05-10 16:07:28 -06004607 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004608
4609 return true;
Jens Axboe9e645e112019-05-10 16:07:28 -06004610}
4611
Jens Axboe9a56a232019-01-09 09:06:50 -07004612/*
4613 * Batched submission is done, ensure local IO is flushed out.
4614 */
4615static void io_submit_state_end(struct io_submit_state *state)
4616{
4617 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06004618 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07004619 if (state->free_reqs)
4620 kmem_cache_free_bulk(req_cachep, state->free_reqs,
4621 &state->reqs[state->cur_req]);
Jens Axboe9a56a232019-01-09 09:06:50 -07004622}
4623
4624/*
4625 * Start submission side cache.
4626 */
4627static void io_submit_state_start(struct io_submit_state *state,
Jackie Liu22efde52019-12-02 17:14:52 +08004628 unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07004629{
4630 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07004631 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07004632 state->file = NULL;
4633 state->ios_left = max_ios;
4634}
4635
Jens Axboe2b188cc2019-01-07 10:46:33 -07004636static void io_commit_sqring(struct io_ring_ctx *ctx)
4637{
Hristo Venev75b28af2019-08-26 17:23:46 +00004638 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004639
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03004640 /*
4641 * Ensure any loads from the SQEs are done at this point,
4642 * since once we write the new head, the application could
4643 * write new data to them.
4644 */
4645 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004646}
4647
4648/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07004649 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07004650 * that is mapped by userspace. This means that care needs to be taken to
4651 * ensure that reads are stable, as we cannot rely on userspace always
4652 * being a good citizen. If members of the sqe are validated and then later
4653 * used, it's important that those reads are done through READ_ONCE() to
4654 * prevent a re-load down the line.
4655 */
Jens Axboe3529d8c2019-12-19 18:24:38 -07004656static bool io_get_sqring(struct io_ring_ctx *ctx, struct io_kiocb *req,
4657 const struct io_uring_sqe **sqe_ptr)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004658{
Hristo Venev75b28af2019-08-26 17:23:46 +00004659 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004660 unsigned head;
4661
4662 /*
4663 * The cached sq head (or cq tail) serves two purposes:
4664 *
4665 * 1) allows us to batch the cost of updating the user visible
4666 * head updates.
4667 * 2) allows the kernel side to track the head on its own, even
4668 * though the application is the one updating it.
4669 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03004670 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03004671 if (likely(head < ctx->sq_entries)) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004672 /*
4673 * All io need record the previous position, if LINK vs DARIN,
4674 * it can be used to mark the position of the first IO in the
4675 * link list.
4676 */
4677 req->sequence = ctx->cached_sq_head;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004678 *sqe_ptr = &ctx->sq_sqes[head];
4679 req->opcode = READ_ONCE((*sqe_ptr)->opcode);
4680 req->user_data = READ_ONCE((*sqe_ptr)->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004681 ctx->cached_sq_head++;
4682 return true;
4683 }
4684
4685 /* drop invalid entries */
4686 ctx->cached_sq_head++;
Jens Axboe498ccd92019-10-25 10:04:25 -06004687 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03004688 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004689 return false;
4690}
4691
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004692static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004693 struct file *ring_file, int ring_fd,
4694 struct mm_struct **mm, bool async)
Jens Axboe6c271ce2019-01-10 11:22:30 -07004695{
4696 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06004697 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06004698 int i, submitted = 0;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03004699 bool mm_fault = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004700
Jens Axboec4a2ed72019-11-21 21:01:26 -07004701 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07004702 if (test_bit(0, &ctx->sq_check_overflow)) {
4703 if (!list_empty(&ctx->cq_overflow_list) &&
4704 !io_cqring_overflow_flush(ctx, false))
4705 return -EBUSY;
4706 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07004707
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03004708 /* make sure SQ entry isn't read before tail */
4709 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03004710
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03004711 if (!percpu_ref_tryget_many(&ctx->refs, nr))
4712 return -EAGAIN;
4713
Jens Axboe6c271ce2019-01-10 11:22:30 -07004714 if (nr > IO_PLUG_THRESHOLD) {
Jackie Liu22efde52019-12-02 17:14:52 +08004715 io_submit_state_start(&state, nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004716 statep = &state;
4717 }
4718
4719 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004720 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03004721 struct io_kiocb *req;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004722
Pavel Begunkov196be952019-11-07 01:41:06 +03004723 req = io_get_req(ctx, statep);
4724 if (unlikely(!req)) {
4725 if (!submitted)
4726 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004727 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06004728 }
Jens Axboe3529d8c2019-12-19 18:24:38 -07004729 if (!io_get_sqring(ctx, req, &sqe)) {
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03004730 __io_req_do_free(req);
Pavel Begunkov196be952019-11-07 01:41:06 +03004731 break;
4732 }
Jens Axboe9e645e112019-05-10 16:07:28 -06004733
Jens Axboed3656342019-12-18 09:50:26 -07004734 /* will complete beyond this point, count as submitted */
4735 submitted++;
4736
4737 if (unlikely(req->opcode >= IORING_OP_LAST)) {
4738 io_cqring_add_event(req, -EINVAL);
4739 io_double_put_req(req);
4740 break;
4741 }
4742
4743 if (io_op_defs[req->opcode].needs_mm && !*mm) {
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03004744 mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm);
4745 if (!mm_fault) {
4746 use_mm(ctx->sqo_mm);
4747 *mm = ctx->sqo_mm;
4748 }
4749 }
4750
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004751 req->ring_file = ring_file;
4752 req->ring_fd = ring_fd;
4753 req->has_user = *mm != NULL;
4754 req->in_async = async;
4755 req->needs_fixed_file = async;
Jens Axboed625c6e2019-12-17 19:53:05 -07004756 trace_io_uring_submit_sqe(ctx, req->user_data, true, async);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004757 if (!io_submit_sqe(req, sqe, statep, &link))
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004758 break;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004759 }
4760
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03004761 if (submitted != nr)
4762 percpu_ref_put_many(&ctx->refs, nr - submitted);
Jens Axboe9e645e112019-05-10 16:07:28 -06004763 if (link)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004764 io_queue_link_head(link);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004765 if (statep)
4766 io_submit_state_end(&state);
4767
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004768 /* Commit SQ ring head once we've consumed and submitted all SQEs */
4769 io_commit_sqring(ctx);
4770
Jens Axboe6c271ce2019-01-10 11:22:30 -07004771 return submitted;
4772}
4773
4774static int io_sq_thread(void *data)
4775{
Jens Axboe6c271ce2019-01-10 11:22:30 -07004776 struct io_ring_ctx *ctx = data;
4777 struct mm_struct *cur_mm = NULL;
Jens Axboe181e4482019-11-25 08:52:30 -07004778 const struct cred *old_cred;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004779 mm_segment_t old_fs;
4780 DEFINE_WAIT(wait);
4781 unsigned inflight;
4782 unsigned long timeout;
Jens Axboec1edbf52019-11-10 16:56:04 -07004783 int ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004784
Jens Axboe206aefd2019-11-07 18:27:42 -07004785 complete(&ctx->completions[1]);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08004786
Jens Axboe6c271ce2019-01-10 11:22:30 -07004787 old_fs = get_fs();
4788 set_fs(USER_DS);
Jens Axboe181e4482019-11-25 08:52:30 -07004789 old_cred = override_creds(ctx->creds);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004790
Jens Axboec1edbf52019-11-10 16:56:04 -07004791 ret = timeout = inflight = 0;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02004792 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004793 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004794
4795 if (inflight) {
4796 unsigned nr_events = 0;
4797
4798 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe2b2ed972019-10-25 10:06:15 -06004799 /*
4800 * inflight is the count of the maximum possible
4801 * entries we submitted, but it can be smaller
4802 * if we dropped some of them. If we don't have
4803 * poll entries available, then we know that we
4804 * have nothing left to poll for. Reset the
4805 * inflight count to zero in that case.
4806 */
4807 mutex_lock(&ctx->uring_lock);
4808 if (!list_empty(&ctx->poll_list))
4809 __io_iopoll_check(ctx, &nr_events, 0);
4810 else
4811 inflight = 0;
4812 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004813 } else {
4814 /*
4815 * Normal IO, just pretend everything completed.
4816 * We don't have to poll completions for that.
4817 */
4818 nr_events = inflight;
4819 }
4820
4821 inflight -= nr_events;
4822 if (!inflight)
4823 timeout = jiffies + ctx->sq_thread_idle;
4824 }
4825
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004826 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07004827
4828 /*
4829 * If submit got -EBUSY, flag us as needing the application
4830 * to enter the kernel to reap and flush events.
4831 */
4832 if (!to_submit || ret == -EBUSY) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07004833 /*
4834 * We're polling. If we're within the defined idle
4835 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07004836 * to sleep. The exception is if we got EBUSY doing
4837 * more IO, we should wait for the application to
4838 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07004839 */
Jens Axboec1edbf52019-11-10 16:56:04 -07004840 if (inflight ||
4841 (!time_after(jiffies, timeout) && ret != -EBUSY)) {
Jens Axboe9831a902019-09-19 09:48:55 -06004842 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07004843 continue;
4844 }
4845
4846 /*
4847 * Drop cur_mm before scheduling, we can't hold it for
4848 * long periods (or over schedule()). Do this before
4849 * adding ourselves to the waitqueue, as the unuse/drop
4850 * may sleep.
4851 */
4852 if (cur_mm) {
4853 unuse_mm(cur_mm);
4854 mmput(cur_mm);
4855 cur_mm = NULL;
4856 }
4857
4858 prepare_to_wait(&ctx->sqo_wait, &wait,
4859 TASK_INTERRUPTIBLE);
4860
4861 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00004862 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02004863 /* make sure to read SQ tail after writing flags */
4864 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07004865
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004866 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07004867 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02004868 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07004869 finish_wait(&ctx->sqo_wait, &wait);
4870 break;
4871 }
4872 if (signal_pending(current))
4873 flush_signals(current);
4874 schedule();
4875 finish_wait(&ctx->sqo_wait, &wait);
4876
Hristo Venev75b28af2019-08-26 17:23:46 +00004877 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004878 continue;
4879 }
4880 finish_wait(&ctx->sqo_wait, &wait);
4881
Hristo Venev75b28af2019-08-26 17:23:46 +00004882 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004883 }
4884
Jens Axboe8a4955f2019-12-09 14:52:35 -07004885 mutex_lock(&ctx->uring_lock);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004886 ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true);
Jens Axboe8a4955f2019-12-09 14:52:35 -07004887 mutex_unlock(&ctx->uring_lock);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004888 if (ret > 0)
4889 inflight += ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004890 }
4891
4892 set_fs(old_fs);
4893 if (cur_mm) {
4894 unuse_mm(cur_mm);
4895 mmput(cur_mm);
4896 }
Jens Axboe181e4482019-11-25 08:52:30 -07004897 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06004898
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02004899 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06004900
Jens Axboe6c271ce2019-01-10 11:22:30 -07004901 return 0;
4902}
4903
Jens Axboebda52162019-09-24 13:47:15 -06004904struct io_wait_queue {
4905 struct wait_queue_entry wq;
4906 struct io_ring_ctx *ctx;
4907 unsigned to_wait;
4908 unsigned nr_timeouts;
4909};
4910
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004911static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06004912{
4913 struct io_ring_ctx *ctx = iowq->ctx;
4914
4915 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08004916 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06004917 * started waiting. For timeouts, we always want to return to userspace,
4918 * regardless of event count.
4919 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004920 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06004921 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
4922}
4923
4924static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
4925 int wake_flags, void *key)
4926{
4927 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
4928 wq);
4929
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004930 /* use noflush == true, as we can't safely rely on locking context */
4931 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06004932 return -1;
4933
4934 return autoremove_wake_function(curr, mode, wake_flags, key);
4935}
4936
Jens Axboe2b188cc2019-01-07 10:46:33 -07004937/*
4938 * Wait until events become available, if we don't already have some. The
4939 * application must reap them itself, as they reside on the shared cq ring.
4940 */
4941static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
4942 const sigset_t __user *sig, size_t sigsz)
4943{
Jens Axboebda52162019-09-24 13:47:15 -06004944 struct io_wait_queue iowq = {
4945 .wq = {
4946 .private = current,
4947 .func = io_wake_function,
4948 .entry = LIST_HEAD_INIT(iowq.wq.entry),
4949 },
4950 .ctx = ctx,
4951 .to_wait = min_events,
4952 };
Hristo Venev75b28af2019-08-26 17:23:46 +00004953 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08004954 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004955
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004956 if (io_cqring_events(ctx, false) >= min_events)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004957 return 0;
4958
4959 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01004960#ifdef CONFIG_COMPAT
4961 if (in_compat_syscall())
4962 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07004963 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01004964 else
4965#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07004966 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01004967
Jens Axboe2b188cc2019-01-07 10:46:33 -07004968 if (ret)
4969 return ret;
4970 }
4971
Jens Axboebda52162019-09-24 13:47:15 -06004972 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004973 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06004974 do {
4975 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
4976 TASK_INTERRUPTIBLE);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07004977 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06004978 break;
4979 schedule();
4980 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08004981 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06004982 break;
4983 }
4984 } while (1);
4985 finish_wait(&ctx->wait, &iowq.wq);
4986
Jackie Liue9ffa5c2019-10-29 11:16:42 +08004987 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004988
Hristo Venev75b28af2019-08-26 17:23:46 +00004989 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004990}
4991
Jens Axboe6b063142019-01-10 22:13:58 -07004992static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
4993{
4994#if defined(CONFIG_UNIX)
4995 if (ctx->ring_sock) {
4996 struct sock *sock = ctx->ring_sock->sk;
4997 struct sk_buff *skb;
4998
4999 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
5000 kfree_skb(skb);
5001 }
5002#else
5003 int i;
5004
Jens Axboe65e19f52019-10-26 07:20:21 -06005005 for (i = 0; i < ctx->nr_user_files; i++) {
5006 struct file *file;
5007
5008 file = io_file_from_index(ctx, i);
5009 if (file)
5010 fput(file);
5011 }
Jens Axboe6b063142019-01-10 22:13:58 -07005012#endif
5013}
5014
Jens Axboe05f3fb32019-12-09 11:22:50 -07005015static void io_file_ref_kill(struct percpu_ref *ref)
5016{
5017 struct fixed_file_data *data;
5018
5019 data = container_of(ref, struct fixed_file_data, refs);
5020 complete(&data->done);
5021}
5022
Jens Axboe6b063142019-01-10 22:13:58 -07005023static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
5024{
Jens Axboe05f3fb32019-12-09 11:22:50 -07005025 struct fixed_file_data *data = ctx->file_data;
Jens Axboe65e19f52019-10-26 07:20:21 -06005026 unsigned nr_tables, i;
5027
Jens Axboe05f3fb32019-12-09 11:22:50 -07005028 if (!data)
Jens Axboe6b063142019-01-10 22:13:58 -07005029 return -ENXIO;
5030
Jens Axboe05f3fb32019-12-09 11:22:50 -07005031 /* protect against inflight atomic switch, which drops the ref */
5032 flush_work(&data->ref_work);
5033 percpu_ref_get(&data->refs);
5034 percpu_ref_kill_and_confirm(&data->refs, io_file_ref_kill);
5035 wait_for_completion(&data->done);
5036 percpu_ref_put(&data->refs);
5037 percpu_ref_exit(&data->refs);
5038
Jens Axboe6b063142019-01-10 22:13:58 -07005039 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06005040 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
5041 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005042 kfree(data->table[i].files);
5043 kfree(data->table);
5044 kfree(data);
5045 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07005046 ctx->nr_user_files = 0;
5047 return 0;
5048}
5049
Jens Axboe6c271ce2019-01-10 11:22:30 -07005050static void io_sq_thread_stop(struct io_ring_ctx *ctx)
5051{
5052 if (ctx->sqo_thread) {
Jens Axboe206aefd2019-11-07 18:27:42 -07005053 wait_for_completion(&ctx->completions[1]);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005054 /*
5055 * The park is a bit of a work-around, without it we get
5056 * warning spews on shutdown with SQPOLL set and affinity
5057 * set to a single CPU.
5058 */
Jens Axboe06058632019-04-13 09:26:03 -06005059 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005060 kthread_stop(ctx->sqo_thread);
5061 ctx->sqo_thread = NULL;
5062 }
5063}
5064
Jens Axboe6b063142019-01-10 22:13:58 -07005065static void io_finish_async(struct io_ring_ctx *ctx)
5066{
Jens Axboe6c271ce2019-01-10 11:22:30 -07005067 io_sq_thread_stop(ctx);
5068
Jens Axboe561fb042019-10-24 07:25:42 -06005069 if (ctx->io_wq) {
5070 io_wq_destroy(ctx->io_wq);
5071 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07005072 }
5073}
5074
5075#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07005076/*
5077 * Ensure the UNIX gc is aware of our file set, so we are certain that
5078 * the io_uring can be safely unregistered on process exit, even if we have
5079 * loops in the file referencing.
5080 */
5081static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
5082{
5083 struct sock *sk = ctx->ring_sock->sk;
5084 struct scm_fp_list *fpl;
5085 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06005086 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07005087
5088 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
5089 unsigned long inflight = ctx->user->unix_inflight + nr;
5090
5091 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
5092 return -EMFILE;
5093 }
5094
5095 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
5096 if (!fpl)
5097 return -ENOMEM;
5098
5099 skb = alloc_skb(0, GFP_KERNEL);
5100 if (!skb) {
5101 kfree(fpl);
5102 return -ENOMEM;
5103 }
5104
5105 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07005106
Jens Axboe08a45172019-10-03 08:11:03 -06005107 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07005108 fpl->user = get_uid(ctx->user);
5109 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005110 struct file *file = io_file_from_index(ctx, i + offset);
5111
5112 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06005113 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06005114 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06005115 unix_inflight(fpl->user, fpl->fp[nr_files]);
5116 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07005117 }
5118
Jens Axboe08a45172019-10-03 08:11:03 -06005119 if (nr_files) {
5120 fpl->max = SCM_MAX_FD;
5121 fpl->count = nr_files;
5122 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005123 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06005124 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
5125 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07005126
Jens Axboe08a45172019-10-03 08:11:03 -06005127 for (i = 0; i < nr_files; i++)
5128 fput(fpl->fp[i]);
5129 } else {
5130 kfree_skb(skb);
5131 kfree(fpl);
5132 }
Jens Axboe6b063142019-01-10 22:13:58 -07005133
5134 return 0;
5135}
5136
5137/*
5138 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
5139 * causes regular reference counting to break down. We rely on the UNIX
5140 * garbage collection to take care of this problem for us.
5141 */
5142static int io_sqe_files_scm(struct io_ring_ctx *ctx)
5143{
5144 unsigned left, total;
5145 int ret = 0;
5146
5147 total = 0;
5148 left = ctx->nr_user_files;
5149 while (left) {
5150 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07005151
5152 ret = __io_sqe_files_scm(ctx, this_files, total);
5153 if (ret)
5154 break;
5155 left -= this_files;
5156 total += this_files;
5157 }
5158
5159 if (!ret)
5160 return 0;
5161
5162 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005163 struct file *file = io_file_from_index(ctx, total);
5164
5165 if (file)
5166 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07005167 total++;
5168 }
5169
5170 return ret;
5171}
5172#else
5173static int io_sqe_files_scm(struct io_ring_ctx *ctx)
5174{
5175 return 0;
5176}
5177#endif
5178
Jens Axboe65e19f52019-10-26 07:20:21 -06005179static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
5180 unsigned nr_files)
5181{
5182 int i;
5183
5184 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005185 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06005186 unsigned this_files;
5187
5188 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
5189 table->files = kcalloc(this_files, sizeof(struct file *),
5190 GFP_KERNEL);
5191 if (!table->files)
5192 break;
5193 nr_files -= this_files;
5194 }
5195
5196 if (i == nr_tables)
5197 return 0;
5198
5199 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005200 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06005201 kfree(table->files);
5202 }
5203 return 1;
5204}
5205
Jens Axboe05f3fb32019-12-09 11:22:50 -07005206static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
Jens Axboec3a31e62019-10-03 13:59:56 -06005207{
5208#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06005209 struct sock *sock = ctx->ring_sock->sk;
5210 struct sk_buff_head list, *head = &sock->sk_receive_queue;
5211 struct sk_buff *skb;
5212 int i;
5213
5214 __skb_queue_head_init(&list);
5215
5216 /*
5217 * Find the skb that holds this file in its SCM_RIGHTS. When found,
5218 * remove this entry and rearrange the file array.
5219 */
5220 skb = skb_dequeue(head);
5221 while (skb) {
5222 struct scm_fp_list *fp;
5223
5224 fp = UNIXCB(skb).fp;
5225 for (i = 0; i < fp->count; i++) {
5226 int left;
5227
5228 if (fp->fp[i] != file)
5229 continue;
5230
5231 unix_notinflight(fp->user, fp->fp[i]);
5232 left = fp->count - 1 - i;
5233 if (left) {
5234 memmove(&fp->fp[i], &fp->fp[i + 1],
5235 left * sizeof(struct file *));
5236 }
5237 fp->count--;
5238 if (!fp->count) {
5239 kfree_skb(skb);
5240 skb = NULL;
5241 } else {
5242 __skb_queue_tail(&list, skb);
5243 }
5244 fput(file);
5245 file = NULL;
5246 break;
5247 }
5248
5249 if (!file)
5250 break;
5251
5252 __skb_queue_tail(&list, skb);
5253
5254 skb = skb_dequeue(head);
5255 }
5256
5257 if (skb_peek(&list)) {
5258 spin_lock_irq(&head->lock);
5259 while ((skb = __skb_dequeue(&list)) != NULL)
5260 __skb_queue_tail(head, skb);
5261 spin_unlock_irq(&head->lock);
5262 }
5263#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07005264 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06005265#endif
5266}
5267
Jens Axboe05f3fb32019-12-09 11:22:50 -07005268struct io_file_put {
5269 struct llist_node llist;
5270 struct file *file;
5271 struct completion *done;
5272};
5273
5274static void io_ring_file_ref_switch(struct work_struct *work)
5275{
5276 struct io_file_put *pfile, *tmp;
5277 struct fixed_file_data *data;
5278 struct llist_node *node;
5279
5280 data = container_of(work, struct fixed_file_data, ref_work);
5281
5282 while ((node = llist_del_all(&data->put_llist)) != NULL) {
5283 llist_for_each_entry_safe(pfile, tmp, node, llist) {
5284 io_ring_file_put(data->ctx, pfile->file);
5285 if (pfile->done)
5286 complete(pfile->done);
5287 else
5288 kfree(pfile);
5289 }
5290 }
5291
5292 percpu_ref_get(&data->refs);
5293 percpu_ref_switch_to_percpu(&data->refs);
5294}
5295
5296static void io_file_data_ref_zero(struct percpu_ref *ref)
5297{
5298 struct fixed_file_data *data;
5299
5300 data = container_of(ref, struct fixed_file_data, refs);
5301
5302 /* we can't safely switch from inside this context, punt to wq */
5303 queue_work(system_wq, &data->ref_work);
5304}
5305
5306static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
5307 unsigned nr_args)
5308{
5309 __s32 __user *fds = (__s32 __user *) arg;
5310 unsigned nr_tables;
5311 struct file *file;
5312 int fd, ret = 0;
5313 unsigned i;
5314
5315 if (ctx->file_data)
5316 return -EBUSY;
5317 if (!nr_args)
5318 return -EINVAL;
5319 if (nr_args > IORING_MAX_FIXED_FILES)
5320 return -EMFILE;
5321
5322 ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
5323 if (!ctx->file_data)
5324 return -ENOMEM;
5325 ctx->file_data->ctx = ctx;
5326 init_completion(&ctx->file_data->done);
5327
5328 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
5329 ctx->file_data->table = kcalloc(nr_tables,
5330 sizeof(struct fixed_file_table),
5331 GFP_KERNEL);
5332 if (!ctx->file_data->table) {
5333 kfree(ctx->file_data);
5334 ctx->file_data = NULL;
5335 return -ENOMEM;
5336 }
5337
5338 if (percpu_ref_init(&ctx->file_data->refs, io_file_data_ref_zero,
5339 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
5340 kfree(ctx->file_data->table);
5341 kfree(ctx->file_data);
5342 ctx->file_data = NULL;
5343 return -ENOMEM;
5344 }
5345 ctx->file_data->put_llist.first = NULL;
5346 INIT_WORK(&ctx->file_data->ref_work, io_ring_file_ref_switch);
5347
5348 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
5349 percpu_ref_exit(&ctx->file_data->refs);
5350 kfree(ctx->file_data->table);
5351 kfree(ctx->file_data);
5352 ctx->file_data = NULL;
5353 return -ENOMEM;
5354 }
5355
5356 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
5357 struct fixed_file_table *table;
5358 unsigned index;
5359
5360 ret = -EFAULT;
5361 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
5362 break;
5363 /* allow sparse sets */
5364 if (fd == -1) {
5365 ret = 0;
5366 continue;
5367 }
5368
5369 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
5370 index = i & IORING_FILE_TABLE_MASK;
5371 file = fget(fd);
5372
5373 ret = -EBADF;
5374 if (!file)
5375 break;
5376
5377 /*
5378 * Don't allow io_uring instances to be registered. If UNIX
5379 * isn't enabled, then this causes a reference cycle and this
5380 * instance can never get freed. If UNIX is enabled we'll
5381 * handle it just fine, but there's still no point in allowing
5382 * a ring fd as it doesn't support regular read/write anyway.
5383 */
5384 if (file->f_op == &io_uring_fops) {
5385 fput(file);
5386 break;
5387 }
5388 ret = 0;
5389 table->files[index] = file;
5390 }
5391
5392 if (ret) {
5393 for (i = 0; i < ctx->nr_user_files; i++) {
5394 file = io_file_from_index(ctx, i);
5395 if (file)
5396 fput(file);
5397 }
5398 for (i = 0; i < nr_tables; i++)
5399 kfree(ctx->file_data->table[i].files);
5400
5401 kfree(ctx->file_data->table);
5402 kfree(ctx->file_data);
5403 ctx->file_data = NULL;
5404 ctx->nr_user_files = 0;
5405 return ret;
5406 }
5407
5408 ret = io_sqe_files_scm(ctx);
5409 if (ret)
5410 io_sqe_files_unregister(ctx);
5411
5412 return ret;
5413}
5414
Jens Axboec3a31e62019-10-03 13:59:56 -06005415static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
5416 int index)
5417{
5418#if defined(CONFIG_UNIX)
5419 struct sock *sock = ctx->ring_sock->sk;
5420 struct sk_buff_head *head = &sock->sk_receive_queue;
5421 struct sk_buff *skb;
5422
5423 /*
5424 * See if we can merge this file into an existing skb SCM_RIGHTS
5425 * file set. If there's no room, fall back to allocating a new skb
5426 * and filling it in.
5427 */
5428 spin_lock_irq(&head->lock);
5429 skb = skb_peek(head);
5430 if (skb) {
5431 struct scm_fp_list *fpl = UNIXCB(skb).fp;
5432
5433 if (fpl->count < SCM_MAX_FD) {
5434 __skb_unlink(skb, head);
5435 spin_unlock_irq(&head->lock);
5436 fpl->fp[fpl->count] = get_file(file);
5437 unix_inflight(fpl->user, fpl->fp[fpl->count]);
5438 fpl->count++;
5439 spin_lock_irq(&head->lock);
5440 __skb_queue_head(head, skb);
5441 } else {
5442 skb = NULL;
5443 }
5444 }
5445 spin_unlock_irq(&head->lock);
5446
5447 if (skb) {
5448 fput(file);
5449 return 0;
5450 }
5451
5452 return __io_sqe_files_scm(ctx, 1, index);
5453#else
5454 return 0;
5455#endif
5456}
5457
Jens Axboe05f3fb32019-12-09 11:22:50 -07005458static void io_atomic_switch(struct percpu_ref *ref)
Jens Axboec3a31e62019-10-03 13:59:56 -06005459{
Jens Axboe05f3fb32019-12-09 11:22:50 -07005460 struct fixed_file_data *data;
5461
5462 data = container_of(ref, struct fixed_file_data, refs);
5463 clear_bit(FFD_F_ATOMIC, &data->state);
5464}
5465
5466static bool io_queue_file_removal(struct fixed_file_data *data,
5467 struct file *file)
5468{
5469 struct io_file_put *pfile, pfile_stack;
5470 DECLARE_COMPLETION_ONSTACK(done);
5471
5472 /*
5473 * If we fail allocating the struct we need for doing async reomval
5474 * of this file, just punt to sync and wait for it.
5475 */
5476 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
5477 if (!pfile) {
5478 pfile = &pfile_stack;
5479 pfile->done = &done;
5480 }
5481
5482 pfile->file = file;
5483 llist_add(&pfile->llist, &data->put_llist);
5484
5485 if (pfile == &pfile_stack) {
5486 if (!test_and_set_bit(FFD_F_ATOMIC, &data->state)) {
5487 percpu_ref_put(&data->refs);
5488 percpu_ref_switch_to_atomic(&data->refs,
5489 io_atomic_switch);
5490 }
5491 wait_for_completion(&done);
5492 flush_work(&data->ref_work);
5493 return false;
5494 }
5495
5496 return true;
5497}
5498
5499static int __io_sqe_files_update(struct io_ring_ctx *ctx,
5500 struct io_uring_files_update *up,
5501 unsigned nr_args)
5502{
5503 struct fixed_file_data *data = ctx->file_data;
5504 bool ref_switch = false;
5505 struct file *file;
Jens Axboec3a31e62019-10-03 13:59:56 -06005506 __s32 __user *fds;
5507 int fd, i, err;
5508 __u32 done;
5509
Jens Axboe05f3fb32019-12-09 11:22:50 -07005510 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06005511 return -EOVERFLOW;
5512 if (done > ctx->nr_user_files)
5513 return -EINVAL;
5514
5515 done = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005516 fds = u64_to_user_ptr(up->fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06005517 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005518 struct fixed_file_table *table;
5519 unsigned index;
5520
Jens Axboec3a31e62019-10-03 13:59:56 -06005521 err = 0;
5522 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
5523 err = -EFAULT;
5524 break;
5525 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07005526 i = array_index_nospec(up->offset, ctx->nr_user_files);
5527 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
Jens Axboe65e19f52019-10-26 07:20:21 -06005528 index = i & IORING_FILE_TABLE_MASK;
5529 if (table->files[index]) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005530 file = io_file_from_index(ctx, index);
Jens Axboe65e19f52019-10-26 07:20:21 -06005531 table->files[index] = NULL;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005532 if (io_queue_file_removal(data, file))
5533 ref_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06005534 }
5535 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06005536 file = fget(fd);
5537 if (!file) {
5538 err = -EBADF;
5539 break;
5540 }
5541 /*
5542 * Don't allow io_uring instances to be registered. If
5543 * UNIX isn't enabled, then this causes a reference
5544 * cycle and this instance can never get freed. If UNIX
5545 * is enabled we'll handle it just fine, but there's
5546 * still no point in allowing a ring fd as it doesn't
5547 * support regular read/write anyway.
5548 */
5549 if (file->f_op == &io_uring_fops) {
5550 fput(file);
5551 err = -EBADF;
5552 break;
5553 }
Jens Axboe65e19f52019-10-26 07:20:21 -06005554 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06005555 err = io_sqe_file_register(ctx, file, i);
5556 if (err)
5557 break;
5558 }
5559 nr_args--;
5560 done++;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005561 up->offset++;
5562 }
5563
5564 if (ref_switch && !test_and_set_bit(FFD_F_ATOMIC, &data->state)) {
5565 percpu_ref_put(&data->refs);
5566 percpu_ref_switch_to_atomic(&data->refs, io_atomic_switch);
Jens Axboec3a31e62019-10-03 13:59:56 -06005567 }
5568
5569 return done ? done : err;
5570}
Jens Axboe05f3fb32019-12-09 11:22:50 -07005571static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
5572 unsigned nr_args)
5573{
5574 struct io_uring_files_update up;
5575
5576 if (!ctx->file_data)
5577 return -ENXIO;
5578 if (!nr_args)
5579 return -EINVAL;
5580 if (copy_from_user(&up, arg, sizeof(up)))
5581 return -EFAULT;
5582 if (up.resv)
5583 return -EINVAL;
5584
5585 return __io_sqe_files_update(ctx, &up, nr_args);
5586}
Jens Axboec3a31e62019-10-03 13:59:56 -06005587
Jens Axboe7d723062019-11-12 22:31:31 -07005588static void io_put_work(struct io_wq_work *work)
5589{
5590 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
5591
5592 io_put_req(req);
5593}
5594
5595static void io_get_work(struct io_wq_work *work)
5596{
5597 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
5598
5599 refcount_inc(&req->refs);
5600}
5601
Jens Axboe6c271ce2019-01-10 11:22:30 -07005602static int io_sq_offload_start(struct io_ring_ctx *ctx,
5603 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005604{
Jens Axboe576a3472019-11-25 08:49:20 -07005605 struct io_wq_data data;
Jens Axboe561fb042019-10-24 07:25:42 -06005606 unsigned concurrency;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005607 int ret;
5608
Jens Axboe6c271ce2019-01-10 11:22:30 -07005609 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005610 mmgrab(current->mm);
5611 ctx->sqo_mm = current->mm;
5612
Jens Axboe6c271ce2019-01-10 11:22:30 -07005613 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06005614 ret = -EPERM;
5615 if (!capable(CAP_SYS_ADMIN))
5616 goto err;
5617
Jens Axboe917257d2019-04-13 09:28:55 -06005618 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
5619 if (!ctx->sq_thread_idle)
5620 ctx->sq_thread_idle = HZ;
5621
Jens Axboe6c271ce2019-01-10 11:22:30 -07005622 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06005623 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005624
Jens Axboe917257d2019-04-13 09:28:55 -06005625 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06005626 if (cpu >= nr_cpu_ids)
5627 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08005628 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06005629 goto err;
5630
Jens Axboe6c271ce2019-01-10 11:22:30 -07005631 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
5632 ctx, cpu,
5633 "io_uring-sq");
5634 } else {
5635 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
5636 "io_uring-sq");
5637 }
5638 if (IS_ERR(ctx->sqo_thread)) {
5639 ret = PTR_ERR(ctx->sqo_thread);
5640 ctx->sqo_thread = NULL;
5641 goto err;
5642 }
5643 wake_up_process(ctx->sqo_thread);
5644 } else if (p->flags & IORING_SETUP_SQ_AFF) {
5645 /* Can't have SQ_AFF without SQPOLL */
5646 ret = -EINVAL;
5647 goto err;
5648 }
5649
Jens Axboe576a3472019-11-25 08:49:20 -07005650 data.mm = ctx->sqo_mm;
5651 data.user = ctx->user;
Jens Axboe181e4482019-11-25 08:52:30 -07005652 data.creds = ctx->creds;
Jens Axboe576a3472019-11-25 08:49:20 -07005653 data.get_work = io_get_work;
5654 data.put_work = io_put_work;
5655
Jens Axboe561fb042019-10-24 07:25:42 -06005656 /* Do QD, or 4 * CPUS, whatever is smallest */
5657 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
Jens Axboe576a3472019-11-25 08:49:20 -07005658 ctx->io_wq = io_wq_create(concurrency, &data);
Jens Axboe975c99a52019-10-30 08:42:56 -06005659 if (IS_ERR(ctx->io_wq)) {
5660 ret = PTR_ERR(ctx->io_wq);
5661 ctx->io_wq = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005662 goto err;
5663 }
5664
5665 return 0;
5666err:
Jens Axboe54a91f32019-09-10 09:15:04 -06005667 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005668 mmdrop(ctx->sqo_mm);
5669 ctx->sqo_mm = NULL;
5670 return ret;
5671}
5672
5673static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
5674{
5675 atomic_long_sub(nr_pages, &user->locked_vm);
5676}
5677
5678static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
5679{
5680 unsigned long page_limit, cur_pages, new_pages;
5681
5682 /* Don't allow more pages than we can safely lock */
5683 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
5684
5685 do {
5686 cur_pages = atomic_long_read(&user->locked_vm);
5687 new_pages = cur_pages + nr_pages;
5688 if (new_pages > page_limit)
5689 return -ENOMEM;
5690 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
5691 new_pages) != cur_pages);
5692
5693 return 0;
5694}
5695
5696static void io_mem_free(void *ptr)
5697{
Mark Rutland52e04ef2019-04-30 17:30:21 +01005698 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005699
Mark Rutland52e04ef2019-04-30 17:30:21 +01005700 if (!ptr)
5701 return;
5702
5703 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005704 if (put_page_testzero(page))
5705 free_compound_page(page);
5706}
5707
5708static void *io_mem_alloc(size_t size)
5709{
5710 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
5711 __GFP_NORETRY;
5712
5713 return (void *) __get_free_pages(gfp_flags, get_order(size));
5714}
5715
Hristo Venev75b28af2019-08-26 17:23:46 +00005716static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
5717 size_t *sq_offset)
5718{
5719 struct io_rings *rings;
5720 size_t off, sq_array_size;
5721
5722 off = struct_size(rings, cqes, cq_entries);
5723 if (off == SIZE_MAX)
5724 return SIZE_MAX;
5725
5726#ifdef CONFIG_SMP
5727 off = ALIGN(off, SMP_CACHE_BYTES);
5728 if (off == 0)
5729 return SIZE_MAX;
5730#endif
5731
5732 sq_array_size = array_size(sizeof(u32), sq_entries);
5733 if (sq_array_size == SIZE_MAX)
5734 return SIZE_MAX;
5735
5736 if (check_add_overflow(off, sq_array_size, &off))
5737 return SIZE_MAX;
5738
5739 if (sq_offset)
5740 *sq_offset = off;
5741
5742 return off;
5743}
5744
Jens Axboe2b188cc2019-01-07 10:46:33 -07005745static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
5746{
Hristo Venev75b28af2019-08-26 17:23:46 +00005747 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005748
Hristo Venev75b28af2019-08-26 17:23:46 +00005749 pages = (size_t)1 << get_order(
5750 rings_size(sq_entries, cq_entries, NULL));
5751 pages += (size_t)1 << get_order(
5752 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07005753
Hristo Venev75b28af2019-08-26 17:23:46 +00005754 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005755}
5756
Jens Axboeedafcce2019-01-09 09:16:05 -07005757static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
5758{
5759 int i, j;
5760
5761 if (!ctx->user_bufs)
5762 return -ENXIO;
5763
5764 for (i = 0; i < ctx->nr_user_bufs; i++) {
5765 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
5766
5767 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbard27c4d3a2019-08-04 19:32:06 -07005768 put_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07005769
5770 if (ctx->account_mem)
5771 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005772 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07005773 imu->nr_bvecs = 0;
5774 }
5775
5776 kfree(ctx->user_bufs);
5777 ctx->user_bufs = NULL;
5778 ctx->nr_user_bufs = 0;
5779 return 0;
5780}
5781
5782static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
5783 void __user *arg, unsigned index)
5784{
5785 struct iovec __user *src;
5786
5787#ifdef CONFIG_COMPAT
5788 if (ctx->compat) {
5789 struct compat_iovec __user *ciovs;
5790 struct compat_iovec ciov;
5791
5792 ciovs = (struct compat_iovec __user *) arg;
5793 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
5794 return -EFAULT;
5795
Jens Axboed55e5f52019-12-11 16:12:15 -07005796 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07005797 dst->iov_len = ciov.iov_len;
5798 return 0;
5799 }
5800#endif
5801 src = (struct iovec __user *) arg;
5802 if (copy_from_user(dst, &src[index], sizeof(*dst)))
5803 return -EFAULT;
5804 return 0;
5805}
5806
5807static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
5808 unsigned nr_args)
5809{
5810 struct vm_area_struct **vmas = NULL;
5811 struct page **pages = NULL;
5812 int i, j, got_pages = 0;
5813 int ret = -EINVAL;
5814
5815 if (ctx->user_bufs)
5816 return -EBUSY;
5817 if (!nr_args || nr_args > UIO_MAXIOV)
5818 return -EINVAL;
5819
5820 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
5821 GFP_KERNEL);
5822 if (!ctx->user_bufs)
5823 return -ENOMEM;
5824
5825 for (i = 0; i < nr_args; i++) {
5826 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
5827 unsigned long off, start, end, ubuf;
5828 int pret, nr_pages;
5829 struct iovec iov;
5830 size_t size;
5831
5832 ret = io_copy_iov(ctx, &iov, arg, i);
5833 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03005834 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07005835
5836 /*
5837 * Don't impose further limits on the size and buffer
5838 * constraints here, we'll -EINVAL later when IO is
5839 * submitted if they are wrong.
5840 */
5841 ret = -EFAULT;
5842 if (!iov.iov_base || !iov.iov_len)
5843 goto err;
5844
5845 /* arbitrary limit, but we need something */
5846 if (iov.iov_len > SZ_1G)
5847 goto err;
5848
5849 ubuf = (unsigned long) iov.iov_base;
5850 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
5851 start = ubuf >> PAGE_SHIFT;
5852 nr_pages = end - start;
5853
5854 if (ctx->account_mem) {
5855 ret = io_account_mem(ctx->user, nr_pages);
5856 if (ret)
5857 goto err;
5858 }
5859
5860 ret = 0;
5861 if (!pages || nr_pages > got_pages) {
5862 kfree(vmas);
5863 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005864 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07005865 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005866 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07005867 sizeof(struct vm_area_struct *),
5868 GFP_KERNEL);
5869 if (!pages || !vmas) {
5870 ret = -ENOMEM;
5871 if (ctx->account_mem)
5872 io_unaccount_mem(ctx->user, nr_pages);
5873 goto err;
5874 }
5875 got_pages = nr_pages;
5876 }
5877
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005878 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07005879 GFP_KERNEL);
5880 ret = -ENOMEM;
5881 if (!imu->bvec) {
5882 if (ctx->account_mem)
5883 io_unaccount_mem(ctx->user, nr_pages);
5884 goto err;
5885 }
5886
5887 ret = 0;
5888 down_read(&current->mm->mmap_sem);
Ira Weiny932f4a62019-05-13 17:17:03 -07005889 pret = get_user_pages(ubuf, nr_pages,
5890 FOLL_WRITE | FOLL_LONGTERM,
5891 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07005892 if (pret == nr_pages) {
5893 /* don't support file backed memory */
5894 for (j = 0; j < nr_pages; j++) {
5895 struct vm_area_struct *vma = vmas[j];
5896
5897 if (vma->vm_file &&
5898 !is_file_hugepages(vma->vm_file)) {
5899 ret = -EOPNOTSUPP;
5900 break;
5901 }
5902 }
5903 } else {
5904 ret = pret < 0 ? pret : -EFAULT;
5905 }
5906 up_read(&current->mm->mmap_sem);
5907 if (ret) {
5908 /*
5909 * if we did partial map, or found file backed vmas,
5910 * release any pages we did get
5911 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07005912 if (pret > 0)
5913 put_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07005914 if (ctx->account_mem)
5915 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005916 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07005917 goto err;
5918 }
5919
5920 off = ubuf & ~PAGE_MASK;
5921 size = iov.iov_len;
5922 for (j = 0; j < nr_pages; j++) {
5923 size_t vec_len;
5924
5925 vec_len = min_t(size_t, size, PAGE_SIZE - off);
5926 imu->bvec[j].bv_page = pages[j];
5927 imu->bvec[j].bv_len = vec_len;
5928 imu->bvec[j].bv_offset = off;
5929 off = 0;
5930 size -= vec_len;
5931 }
5932 /* store original address for later verification */
5933 imu->ubuf = ubuf;
5934 imu->len = iov.iov_len;
5935 imu->nr_bvecs = nr_pages;
5936
5937 ctx->nr_user_bufs++;
5938 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005939 kvfree(pages);
5940 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07005941 return 0;
5942err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01005943 kvfree(pages);
5944 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07005945 io_sqe_buffer_unregister(ctx);
5946 return ret;
5947}
5948
Jens Axboe9b402842019-04-11 11:45:41 -06005949static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
5950{
5951 __s32 __user *fds = arg;
5952 int fd;
5953
5954 if (ctx->cq_ev_fd)
5955 return -EBUSY;
5956
5957 if (copy_from_user(&fd, fds, sizeof(*fds)))
5958 return -EFAULT;
5959
5960 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
5961 if (IS_ERR(ctx->cq_ev_fd)) {
5962 int ret = PTR_ERR(ctx->cq_ev_fd);
5963 ctx->cq_ev_fd = NULL;
5964 return ret;
5965 }
5966
5967 return 0;
5968}
5969
5970static int io_eventfd_unregister(struct io_ring_ctx *ctx)
5971{
5972 if (ctx->cq_ev_fd) {
5973 eventfd_ctx_put(ctx->cq_ev_fd);
5974 ctx->cq_ev_fd = NULL;
5975 return 0;
5976 }
5977
5978 return -ENXIO;
5979}
5980
Jens Axboe2b188cc2019-01-07 10:46:33 -07005981static void io_ring_ctx_free(struct io_ring_ctx *ctx)
5982{
Jens Axboe6b063142019-01-10 22:13:58 -07005983 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005984 if (ctx->sqo_mm)
5985 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07005986
5987 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07005988 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07005989 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06005990 io_eventfd_unregister(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07005991
Jens Axboe2b188cc2019-01-07 10:46:33 -07005992#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07005993 if (ctx->ring_sock) {
5994 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07005995 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07005996 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07005997#endif
5998
Hristo Venev75b28af2019-08-26 17:23:46 +00005999 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006000 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006001
6002 percpu_ref_exit(&ctx->refs);
6003 if (ctx->account_mem)
6004 io_unaccount_mem(ctx->user,
6005 ring_pages(ctx->sq_entries, ctx->cq_entries));
6006 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07006007 put_cred(ctx->creds);
Jens Axboe206aefd2019-11-07 18:27:42 -07006008 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -07006009 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07006010 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006011 kfree(ctx);
6012}
6013
6014static __poll_t io_uring_poll(struct file *file, poll_table *wait)
6015{
6016 struct io_ring_ctx *ctx = file->private_data;
6017 __poll_t mask = 0;
6018
6019 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02006020 /*
6021 * synchronizes with barrier from wq_has_sleeper call in
6022 * io_commit_cqring
6023 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07006024 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00006025 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
6026 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006027 mask |= EPOLLOUT | EPOLLWRNORM;
yangerkundaa5de52019-09-24 20:53:34 +08006028 if (READ_ONCE(ctx->rings->cq.head) != ctx->cached_cq_tail)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006029 mask |= EPOLLIN | EPOLLRDNORM;
6030
6031 return mask;
6032}
6033
6034static int io_uring_fasync(int fd, struct file *file, int on)
6035{
6036 struct io_ring_ctx *ctx = file->private_data;
6037
6038 return fasync_helper(fd, file, on, &ctx->cq_fasync);
6039}
6040
6041static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
6042{
6043 mutex_lock(&ctx->uring_lock);
6044 percpu_ref_kill(&ctx->refs);
6045 mutex_unlock(&ctx->uring_lock);
6046
Jens Axboe5262f562019-09-17 12:26:57 -06006047 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07006048 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06006049
6050 if (ctx->io_wq)
6051 io_wq_cancel_all(ctx->io_wq);
6052
Jens Axboedef596e2019-01-09 08:59:42 -07006053 io_iopoll_reap_events(ctx);
Jens Axboe15dff282019-11-13 09:09:23 -07006054 /* if we failed setting up the ctx, we might not have any rings */
6055 if (ctx->rings)
6056 io_cqring_overflow_flush(ctx, true);
Jens Axboe206aefd2019-11-07 18:27:42 -07006057 wait_for_completion(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006058 io_ring_ctx_free(ctx);
6059}
6060
6061static int io_uring_release(struct inode *inode, struct file *file)
6062{
6063 struct io_ring_ctx *ctx = file->private_data;
6064
6065 file->private_data = NULL;
6066 io_ring_ctx_wait_and_kill(ctx);
6067 return 0;
6068}
6069
Jens Axboefcb323c2019-10-24 12:39:47 -06006070static void io_uring_cancel_files(struct io_ring_ctx *ctx,
6071 struct files_struct *files)
6072{
6073 struct io_kiocb *req;
6074 DEFINE_WAIT(wait);
6075
6076 while (!list_empty_careful(&ctx->inflight_list)) {
Jens Axboe768134d2019-11-10 20:30:53 -07006077 struct io_kiocb *cancel_req = NULL;
Jens Axboefcb323c2019-10-24 12:39:47 -06006078
6079 spin_lock_irq(&ctx->inflight_lock);
6080 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07006081 if (req->work.files != files)
6082 continue;
6083 /* req is being completed, ignore */
6084 if (!refcount_inc_not_zero(&req->refs))
6085 continue;
6086 cancel_req = req;
6087 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06006088 }
Jens Axboe768134d2019-11-10 20:30:53 -07006089 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06006090 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07006091 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06006092 spin_unlock_irq(&ctx->inflight_lock);
6093
Jens Axboe768134d2019-11-10 20:30:53 -07006094 /* We need to keep going until we don't find a matching req */
6095 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06006096 break;
Bob Liu2f6d9b92019-11-13 18:06:24 +08006097
6098 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
6099 io_put_req(cancel_req);
Jens Axboefcb323c2019-10-24 12:39:47 -06006100 schedule();
6101 }
Jens Axboe768134d2019-11-10 20:30:53 -07006102 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06006103}
6104
6105static int io_uring_flush(struct file *file, void *data)
6106{
6107 struct io_ring_ctx *ctx = file->private_data;
6108
6109 io_uring_cancel_files(ctx, data);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006110 if (fatal_signal_pending(current) || (current->flags & PF_EXITING)) {
6111 io_cqring_overflow_flush(ctx, true);
Jens Axboefcb323c2019-10-24 12:39:47 -06006112 io_wq_cancel_all(ctx->io_wq);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07006113 }
Jens Axboefcb323c2019-10-24 12:39:47 -06006114 return 0;
6115}
6116
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006117static void *io_uring_validate_mmap_request(struct file *file,
6118 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006119{
Jens Axboe2b188cc2019-01-07 10:46:33 -07006120 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006121 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006122 struct page *page;
6123 void *ptr;
6124
6125 switch (offset) {
6126 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00006127 case IORING_OFF_CQ_RING:
6128 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006129 break;
6130 case IORING_OFF_SQES:
6131 ptr = ctx->sq_sqes;
6132 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006133 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006134 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006135 }
6136
6137 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07006138 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006139 return ERR_PTR(-EINVAL);
6140
6141 return ptr;
6142}
6143
6144#ifdef CONFIG_MMU
6145
6146static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
6147{
6148 size_t sz = vma->vm_end - vma->vm_start;
6149 unsigned long pfn;
6150 void *ptr;
6151
6152 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
6153 if (IS_ERR(ptr))
6154 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006155
6156 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
6157 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
6158}
6159
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006160#else /* !CONFIG_MMU */
6161
6162static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
6163{
6164 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
6165}
6166
6167static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
6168{
6169 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
6170}
6171
6172static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
6173 unsigned long addr, unsigned long len,
6174 unsigned long pgoff, unsigned long flags)
6175{
6176 void *ptr;
6177
6178 ptr = io_uring_validate_mmap_request(file, pgoff, len);
6179 if (IS_ERR(ptr))
6180 return PTR_ERR(ptr);
6181
6182 return (unsigned long) ptr;
6183}
6184
6185#endif /* !CONFIG_MMU */
6186
Jens Axboe2b188cc2019-01-07 10:46:33 -07006187SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
6188 u32, min_complete, u32, flags, const sigset_t __user *, sig,
6189 size_t, sigsz)
6190{
6191 struct io_ring_ctx *ctx;
6192 long ret = -EBADF;
6193 int submitted = 0;
6194 struct fd f;
6195
Jens Axboe6c271ce2019-01-10 11:22:30 -07006196 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006197 return -EINVAL;
6198
6199 f = fdget(fd);
6200 if (!f.file)
6201 return -EBADF;
6202
6203 ret = -EOPNOTSUPP;
6204 if (f.file->f_op != &io_uring_fops)
6205 goto out_fput;
6206
6207 ret = -ENXIO;
6208 ctx = f.file->private_data;
6209 if (!percpu_ref_tryget(&ctx->refs))
6210 goto out_fput;
6211
Jens Axboe6c271ce2019-01-10 11:22:30 -07006212 /*
6213 * For SQ polling, the thread will do all submissions and completions.
6214 * Just return the requested submit count, and wake the thread if
6215 * we were asked to.
6216 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06006217 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006218 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07006219 if (!list_empty_careful(&ctx->cq_overflow_list))
6220 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006221 if (flags & IORING_ENTER_SQ_WAKEUP)
6222 wake_up(&ctx->sqo_wait);
6223 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06006224 } else if (to_submit) {
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006225 struct mm_struct *cur_mm;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006226
Jens Axboe44d28272020-01-16 19:00:24 -07006227 if (current->mm != ctx->sqo_mm ||
6228 current_cred() != ctx->creds) {
6229 ret = -EPERM;
6230 goto out;
6231 }
6232
Jens Axboe2b188cc2019-01-07 10:46:33 -07006233 mutex_lock(&ctx->uring_lock);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006234 /* already have mm, so io_submit_sqes() won't try to grab it */
6235 cur_mm = ctx->sqo_mm;
6236 submitted = io_submit_sqes(ctx, to_submit, f.file, fd,
6237 &cur_mm, false);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006238 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03006239
6240 if (submitted != to_submit)
6241 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006242 }
6243 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07006244 unsigned nr_events = 0;
6245
Jens Axboe2b188cc2019-01-07 10:46:33 -07006246 min_complete = min(min_complete, ctx->cq_entries);
6247
Jens Axboedef596e2019-01-09 08:59:42 -07006248 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07006249 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07006250 } else {
6251 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
6252 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006253 }
6254
Pavel Begunkov7c504e652019-12-18 19:53:45 +03006255out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03006256 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006257out_fput:
6258 fdput(f);
6259 return submitted ? submitted : ret;
6260}
6261
6262static const struct file_operations io_uring_fops = {
6263 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06006264 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07006265 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006266#ifndef CONFIG_MMU
6267 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
6268 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
6269#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07006270 .poll = io_uring_poll,
6271 .fasync = io_uring_fasync,
6272};
6273
6274static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
6275 struct io_uring_params *p)
6276{
Hristo Venev75b28af2019-08-26 17:23:46 +00006277 struct io_rings *rings;
6278 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006279
Hristo Venev75b28af2019-08-26 17:23:46 +00006280 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
6281 if (size == SIZE_MAX)
6282 return -EOVERFLOW;
6283
6284 rings = io_mem_alloc(size);
6285 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006286 return -ENOMEM;
6287
Hristo Venev75b28af2019-08-26 17:23:46 +00006288 ctx->rings = rings;
6289 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
6290 rings->sq_ring_mask = p->sq_entries - 1;
6291 rings->cq_ring_mask = p->cq_entries - 1;
6292 rings->sq_ring_entries = p->sq_entries;
6293 rings->cq_ring_entries = p->cq_entries;
6294 ctx->sq_mask = rings->sq_ring_mask;
6295 ctx->cq_mask = rings->cq_ring_mask;
6296 ctx->sq_entries = rings->sq_ring_entries;
6297 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006298
6299 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07006300 if (size == SIZE_MAX) {
6301 io_mem_free(ctx->rings);
6302 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006303 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07006304 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006305
6306 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07006307 if (!ctx->sq_sqes) {
6308 io_mem_free(ctx->rings);
6309 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006310 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07006311 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006312
Jens Axboe2b188cc2019-01-07 10:46:33 -07006313 return 0;
6314}
6315
6316/*
6317 * Allocate an anonymous fd, this is what constitutes the application
6318 * visible backing of an io_uring instance. The application mmaps this
6319 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
6320 * we have to tie this fd to a socket for file garbage collection purposes.
6321 */
6322static int io_uring_get_fd(struct io_ring_ctx *ctx)
6323{
6324 struct file *file;
6325 int ret;
6326
6327#if defined(CONFIG_UNIX)
6328 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
6329 &ctx->ring_sock);
6330 if (ret)
6331 return ret;
6332#endif
6333
6334 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
6335 if (ret < 0)
6336 goto err;
6337
6338 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
6339 O_RDWR | O_CLOEXEC);
6340 if (IS_ERR(file)) {
6341 put_unused_fd(ret);
6342 ret = PTR_ERR(file);
6343 goto err;
6344 }
6345
6346#if defined(CONFIG_UNIX)
6347 ctx->ring_sock->file = file;
6348#endif
6349 fd_install(ret, file);
6350 return ret;
6351err:
6352#if defined(CONFIG_UNIX)
6353 sock_release(ctx->ring_sock);
6354 ctx->ring_sock = NULL;
6355#endif
6356 return ret;
6357}
6358
6359static int io_uring_create(unsigned entries, struct io_uring_params *p)
6360{
6361 struct user_struct *user = NULL;
6362 struct io_ring_ctx *ctx;
6363 bool account_mem;
6364 int ret;
6365
Jens Axboe8110c1a2019-12-28 15:39:54 -07006366 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006367 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07006368 if (entries > IORING_MAX_ENTRIES) {
6369 if (!(p->flags & IORING_SETUP_CLAMP))
6370 return -EINVAL;
6371 entries = IORING_MAX_ENTRIES;
6372 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006373
6374 /*
6375 * Use twice as many entries for the CQ ring. It's possible for the
6376 * application to drive a higher depth than the size of the SQ ring,
6377 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06006378 * some flexibility in overcommitting a bit. If the application has
6379 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
6380 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07006381 */
6382 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06006383 if (p->flags & IORING_SETUP_CQSIZE) {
6384 /*
6385 * If IORING_SETUP_CQSIZE is set, we do the same roundup
6386 * to a power-of-two, if it isn't already. We do NOT impose
6387 * any cq vs sq ring sizing.
6388 */
Jens Axboe8110c1a2019-12-28 15:39:54 -07006389 if (p->cq_entries < p->sq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06006390 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07006391 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
6392 if (!(p->flags & IORING_SETUP_CLAMP))
6393 return -EINVAL;
6394 p->cq_entries = IORING_MAX_CQ_ENTRIES;
6395 }
Jens Axboe33a107f2019-10-04 12:10:03 -06006396 p->cq_entries = roundup_pow_of_two(p->cq_entries);
6397 } else {
6398 p->cq_entries = 2 * p->sq_entries;
6399 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006400
6401 user = get_uid(current_user());
6402 account_mem = !capable(CAP_IPC_LOCK);
6403
6404 if (account_mem) {
6405 ret = io_account_mem(user,
6406 ring_pages(p->sq_entries, p->cq_entries));
6407 if (ret) {
6408 free_uid(user);
6409 return ret;
6410 }
6411 }
6412
6413 ctx = io_ring_ctx_alloc(p);
6414 if (!ctx) {
6415 if (account_mem)
6416 io_unaccount_mem(user, ring_pages(p->sq_entries,
6417 p->cq_entries));
6418 free_uid(user);
6419 return -ENOMEM;
6420 }
6421 ctx->compat = in_compat_syscall();
6422 ctx->account_mem = account_mem;
6423 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07006424 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07006425
6426 ret = io_allocate_scq_urings(ctx, p);
6427 if (ret)
6428 goto err;
6429
Jens Axboe6c271ce2019-01-10 11:22:30 -07006430 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006431 if (ret)
6432 goto err;
6433
Jens Axboe2b188cc2019-01-07 10:46:33 -07006434 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00006435 p->sq_off.head = offsetof(struct io_rings, sq.head);
6436 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
6437 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
6438 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
6439 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
6440 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
6441 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006442
6443 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00006444 p->cq_off.head = offsetof(struct io_rings, cq.head);
6445 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
6446 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
6447 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
6448 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
6449 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06006450
Jens Axboe044c1ab2019-10-28 09:15:33 -06006451 /*
6452 * Install ring fd as the very last thing, so we don't risk someone
6453 * having closed it before we finish setup
6454 */
6455 ret = io_uring_get_fd(ctx);
6456 if (ret < 0)
6457 goto err;
6458
Jens Axboeda8c9692019-12-02 18:51:26 -07006459 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
Jens Axboeba042912019-12-25 16:33:42 -07006460 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006461 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006462 return ret;
6463err:
6464 io_ring_ctx_wait_and_kill(ctx);
6465 return ret;
6466}
6467
6468/*
6469 * Sets up an aio uring context, and returns the fd. Applications asks for a
6470 * ring size, we return the actual sq/cq ring sizes (among other things) in the
6471 * params structure passed in.
6472 */
6473static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
6474{
6475 struct io_uring_params p;
6476 long ret;
6477 int i;
6478
6479 if (copy_from_user(&p, params, sizeof(p)))
6480 return -EFAULT;
6481 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
6482 if (p.resv[i])
6483 return -EINVAL;
6484 }
6485
Jens Axboe6c271ce2019-01-10 11:22:30 -07006486 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07006487 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
6488 IORING_SETUP_CLAMP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006489 return -EINVAL;
6490
6491 ret = io_uring_create(entries, &p);
6492 if (ret < 0)
6493 return ret;
6494
6495 if (copy_to_user(params, &p, sizeof(p)))
6496 return -EFAULT;
6497
6498 return ret;
6499}
6500
6501SYSCALL_DEFINE2(io_uring_setup, u32, entries,
6502 struct io_uring_params __user *, params)
6503{
6504 return io_uring_setup(entries, params);
6505}
6506
Jens Axboeedafcce2019-01-09 09:16:05 -07006507static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
6508 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06006509 __releases(ctx->uring_lock)
6510 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07006511{
6512 int ret;
6513
Jens Axboe35fa71a2019-04-22 10:23:23 -06006514 /*
6515 * We're inside the ring mutex, if the ref is already dying, then
6516 * someone else killed the ctx or is already going through
6517 * io_uring_register().
6518 */
6519 if (percpu_ref_is_dying(&ctx->refs))
6520 return -ENXIO;
6521
Jens Axboe05f3fb32019-12-09 11:22:50 -07006522 if (opcode != IORING_UNREGISTER_FILES &&
6523 opcode != IORING_REGISTER_FILES_UPDATE) {
6524 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06006525
Jens Axboe05f3fb32019-12-09 11:22:50 -07006526 /*
6527 * Drop uring mutex before waiting for references to exit. If
6528 * another thread is currently inside io_uring_enter() it might
6529 * need to grab the uring_lock to make progress. If we hold it
6530 * here across the drain wait, then we can deadlock. It's safe
6531 * to drop the mutex here, since no new references will come in
6532 * after we've killed the percpu ref.
6533 */
6534 mutex_unlock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07006535 ret = wait_for_completion_interruptible(&ctx->completions[0]);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006536 mutex_lock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07006537 if (ret) {
6538 percpu_ref_resurrect(&ctx->refs);
6539 ret = -EINTR;
6540 goto out;
6541 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07006542 }
Jens Axboeedafcce2019-01-09 09:16:05 -07006543
6544 switch (opcode) {
6545 case IORING_REGISTER_BUFFERS:
6546 ret = io_sqe_buffer_register(ctx, arg, nr_args);
6547 break;
6548 case IORING_UNREGISTER_BUFFERS:
6549 ret = -EINVAL;
6550 if (arg || nr_args)
6551 break;
6552 ret = io_sqe_buffer_unregister(ctx);
6553 break;
Jens Axboe6b063142019-01-10 22:13:58 -07006554 case IORING_REGISTER_FILES:
6555 ret = io_sqe_files_register(ctx, arg, nr_args);
6556 break;
6557 case IORING_UNREGISTER_FILES:
6558 ret = -EINVAL;
6559 if (arg || nr_args)
6560 break;
6561 ret = io_sqe_files_unregister(ctx);
6562 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06006563 case IORING_REGISTER_FILES_UPDATE:
6564 ret = io_sqe_files_update(ctx, arg, nr_args);
6565 break;
Jens Axboe9b402842019-04-11 11:45:41 -06006566 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -07006567 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -06006568 ret = -EINVAL;
6569 if (nr_args != 1)
6570 break;
6571 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -07006572 if (ret)
6573 break;
6574 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
6575 ctx->eventfd_async = 1;
6576 else
6577 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -06006578 break;
6579 case IORING_UNREGISTER_EVENTFD:
6580 ret = -EINVAL;
6581 if (arg || nr_args)
6582 break;
6583 ret = io_eventfd_unregister(ctx);
6584 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07006585 default:
6586 ret = -EINVAL;
6587 break;
6588 }
6589
Jens Axboe05f3fb32019-12-09 11:22:50 -07006590
6591 if (opcode != IORING_UNREGISTER_FILES &&
6592 opcode != IORING_REGISTER_FILES_UPDATE) {
6593 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -07006594 percpu_ref_reinit(&ctx->refs);
Jens Axboec1503682020-01-08 08:26:07 -07006595out:
6596 reinit_completion(&ctx->completions[0]);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006597 }
Jens Axboeedafcce2019-01-09 09:16:05 -07006598 return ret;
6599}
6600
6601SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
6602 void __user *, arg, unsigned int, nr_args)
6603{
6604 struct io_ring_ctx *ctx;
6605 long ret = -EBADF;
6606 struct fd f;
6607
6608 f = fdget(fd);
6609 if (!f.file)
6610 return -EBADF;
6611
6612 ret = -EOPNOTSUPP;
6613 if (f.file->f_op != &io_uring_fops)
6614 goto out_fput;
6615
6616 ctx = f.file->private_data;
6617
6618 mutex_lock(&ctx->uring_lock);
6619 ret = __io_uring_register(ctx, opcode, arg, nr_args);
6620 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006621 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
6622 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07006623out_fput:
6624 fdput(f);
6625 return ret;
6626}
6627
Jens Axboe2b188cc2019-01-07 10:46:33 -07006628static int __init io_uring_init(void)
6629{
Jens Axboed3656342019-12-18 09:50:26 -07006630 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006631 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
6632 return 0;
6633};
6634__initcall(io_uring_init);