blob: fcb4536a3c8ca7154dee1aedd021435f9798b22a [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>
Pavel Begunkov6b47ee62020-01-18 20:22:41 +030049#include <linux/bits.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070050
51#include <linux/sched/signal.h>
52#include <linux/fs.h>
53#include <linux/file.h>
54#include <linux/fdtable.h>
55#include <linux/mm.h>
56#include <linux/mman.h>
57#include <linux/mmu_context.h>
58#include <linux/percpu.h>
59#include <linux/slab.h>
Jens Axboe6c271ce2019-01-10 11:22:30 -070060#include <linux/kthread.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070061#include <linux/blkdev.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070062#include <linux/bvec.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070063#include <linux/net.h>
64#include <net/sock.h>
65#include <net/af_unix.h>
Jens Axboe6b063142019-01-10 22:13:58 -070066#include <net/scm.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070067#include <linux/anon_inodes.h>
68#include <linux/sched/mm.h>
69#include <linux/uaccess.h>
70#include <linux/nospec.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070071#include <linux/sizes.h>
72#include <linux/hugetlb.h>
Jens Axboeaa4c3962019-11-29 10:14:00 -070073#include <linux/highmem.h>
Jens Axboe15b71ab2019-12-11 11:20:36 -070074#include <linux/namei.h>
75#include <linux/fsnotify.h>
Jens Axboe4840e412019-12-25 22:03:45 -070076#include <linux/fadvise.h>
Jens Axboe3e4827b2020-01-08 15:18:09 -070077#include <linux/eventpoll.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070078
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020079#define CREATE_TRACE_POINTS
80#include <trace/events/io_uring.h>
81
Jens Axboe2b188cc2019-01-07 10:46:33 -070082#include <uapi/linux/io_uring.h>
83
84#include "internal.h"
Jens Axboe561fb042019-10-24 07:25:42 -060085#include "io-wq.h"
Jens Axboe2b188cc2019-01-07 10:46:33 -070086
Daniel Xu5277dea2019-09-14 14:23:45 -070087#define IORING_MAX_ENTRIES 32768
Jens Axboe33a107f2019-10-04 12:10:03 -060088#define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES)
Jens Axboe65e19f52019-10-26 07:20:21 -060089
90/*
91 * Shift of 9 is 512 entries, or exactly one page on 64-bit archs
92 */
93#define IORING_FILE_TABLE_SHIFT 9
94#define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT)
95#define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1)
96#define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE)
Jens Axboe2b188cc2019-01-07 10:46:33 -070097
98struct io_uring {
99 u32 head ____cacheline_aligned_in_smp;
100 u32 tail ____cacheline_aligned_in_smp;
101};
102
Stefan Bühler1e84b972019-04-24 23:54:16 +0200103/*
Hristo Venev75b28af2019-08-26 17:23:46 +0000104 * This data is shared with the application through the mmap at offsets
105 * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200106 *
107 * The offsets to the member fields are published through struct
108 * io_sqring_offsets when calling io_uring_setup.
109 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000110struct io_rings {
Stefan Bühler1e84b972019-04-24 23:54:16 +0200111 /*
112 * Head and tail offsets into the ring; the offsets need to be
113 * masked to get valid indices.
114 *
Hristo Venev75b28af2019-08-26 17:23:46 +0000115 * The kernel controls head of the sq ring and the tail of the cq ring,
116 * and the application controls tail of the sq ring and the head of the
117 * cq ring.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200118 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000119 struct io_uring sq, cq;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200120 /*
Hristo Venev75b28af2019-08-26 17:23:46 +0000121 * Bitmasks to apply to head and tail offsets (constant, equals
Stefan Bühler1e84b972019-04-24 23:54:16 +0200122 * ring_entries - 1)
123 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000124 u32 sq_ring_mask, cq_ring_mask;
125 /* Ring sizes (constant, power of 2) */
126 u32 sq_ring_entries, cq_ring_entries;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200127 /*
128 * Number of invalid entries dropped by the kernel due to
129 * invalid index stored in array
130 *
131 * Written by the kernel, shouldn't be modified by the
132 * application (i.e. get number of "new events" by comparing to
133 * cached value).
134 *
135 * After a new SQ head value was read by the application this
136 * counter includes all submissions that were dropped reaching
137 * the new SQ head (and possibly more).
138 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000139 u32 sq_dropped;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200140 /*
141 * Runtime flags
142 *
143 * Written by the kernel, shouldn't be modified by the
144 * application.
145 *
146 * The application needs a full memory barrier before checking
147 * for IORING_SQ_NEED_WAKEUP after updating the sq tail.
148 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000149 u32 sq_flags;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200150 /*
151 * Number of completion events lost because the queue was full;
152 * this should be avoided by the application by making sure
LimingWu0b4295b2019-12-05 20:18:18 +0800153 * there are not more requests pending than there is space in
Stefan Bühler1e84b972019-04-24 23:54:16 +0200154 * the completion queue.
155 *
156 * Written by the kernel, shouldn't be modified by the
157 * application (i.e. get number of "new events" by comparing to
158 * cached value).
159 *
160 * As completion events come in out of order this counter is not
161 * ordered with any other data.
162 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000163 u32 cq_overflow;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200164 /*
165 * Ring buffer of completion events.
166 *
167 * The kernel writes completion events fresh every time they are
168 * produced, so the application is allowed to modify pending
169 * entries.
170 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000171 struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700172};
173
Jens Axboeedafcce2019-01-09 09:16:05 -0700174struct io_mapped_ubuf {
175 u64 ubuf;
176 size_t len;
177 struct bio_vec *bvec;
178 unsigned int nr_bvecs;
179};
180
Jens Axboe65e19f52019-10-26 07:20:21 -0600181struct fixed_file_table {
182 struct file **files;
Jens Axboe31b51512019-01-18 22:56:34 -0700183};
184
Jens Axboe05f3fb32019-12-09 11:22:50 -0700185enum {
186 FFD_F_ATOMIC,
187};
188
189struct fixed_file_data {
190 struct fixed_file_table *table;
191 struct io_ring_ctx *ctx;
192
193 struct percpu_ref refs;
194 struct llist_head put_llist;
195 unsigned long state;
196 struct work_struct ref_work;
197 struct completion done;
198};
199
Jens Axboe2b188cc2019-01-07 10:46:33 -0700200struct io_ring_ctx {
201 struct {
202 struct percpu_ref refs;
203 } ____cacheline_aligned_in_smp;
204
205 struct {
206 unsigned int flags;
Randy Dunlape1d85332020-02-05 20:57:10 -0800207 unsigned int compat: 1;
208 unsigned int account_mem: 1;
209 unsigned int cq_overflow_flushed: 1;
210 unsigned int drain_next: 1;
211 unsigned int eventfd_async: 1;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700212
Hristo Venev75b28af2019-08-26 17:23:46 +0000213 /*
214 * Ring buffer of indices into array of io_uring_sqe, which is
215 * mmapped by the application using the IORING_OFF_SQES offset.
216 *
217 * This indirection could e.g. be used to assign fixed
218 * io_uring_sqe entries to operations and only submit them to
219 * the queue when needed.
220 *
221 * The kernel modifies neither the indices array nor the entries
222 * array.
223 */
224 u32 *sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700225 unsigned cached_sq_head;
226 unsigned sq_entries;
227 unsigned sq_mask;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700228 unsigned sq_thread_idle;
Jens Axboe498ccd92019-10-25 10:04:25 -0600229 unsigned cached_sq_dropped;
Jens Axboe206aefd2019-11-07 18:27:42 -0700230 atomic_t cached_cq_overflow;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700231 unsigned long sq_check_overflow;
Jens Axboede0617e2019-04-06 21:51:27 -0600232
233 struct list_head defer_list;
Jens Axboe5262f562019-09-17 12:26:57 -0600234 struct list_head timeout_list;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700235 struct list_head cq_overflow_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700236
Jens Axboefcb323c2019-10-24 12:39:47 -0600237 wait_queue_head_t inflight_wait;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700238 struct io_uring_sqe *sq_sqes;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700239 } ____cacheline_aligned_in_smp;
240
Hristo Venev75b28af2019-08-26 17:23:46 +0000241 struct io_rings *rings;
242
Jens Axboe2b188cc2019-01-07 10:46:33 -0700243 /* IO offload */
Jens Axboe561fb042019-10-24 07:25:42 -0600244 struct io_wq *io_wq;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700245 struct task_struct *sqo_thread; /* if using sq thread polling */
246 struct mm_struct *sqo_mm;
247 wait_queue_head_t sqo_wait;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700248
Jens Axboe6b063142019-01-10 22:13:58 -0700249 /*
250 * If used, fixed file set. Writers must ensure that ->refs is dead,
251 * readers must ensure that ->refs is alive as long as the file* is
252 * used. Only updated through io_uring_register(2).
253 */
Jens Axboe05f3fb32019-12-09 11:22:50 -0700254 struct fixed_file_data *file_data;
Jens Axboe6b063142019-01-10 22:13:58 -0700255 unsigned nr_user_files;
Pavel Begunkovb14cca02020-01-17 04:45:59 +0300256 int ring_fd;
257 struct file *ring_file;
Jens Axboe6b063142019-01-10 22:13:58 -0700258
Jens Axboeedafcce2019-01-09 09:16:05 -0700259 /* if used, fixed mapped user buffers */
260 unsigned nr_user_bufs;
261 struct io_mapped_ubuf *user_bufs;
262
Jens Axboe2b188cc2019-01-07 10:46:33 -0700263 struct user_struct *user;
264
Jens Axboe0b8c0ec2019-12-02 08:50:00 -0700265 const struct cred *creds;
Jens Axboe181e4482019-11-25 08:52:30 -0700266
Jens Axboe206aefd2019-11-07 18:27:42 -0700267 /* 0 is for ctx quiesce/reinit/free, 1 is for sqo_thread started */
268 struct completion *completions;
269
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700270 /* if all else fails... */
271 struct io_kiocb *fallback_req;
272
Jens Axboe206aefd2019-11-07 18:27:42 -0700273#if defined(CONFIG_UNIX)
274 struct socket *ring_sock;
275#endif
276
Jens Axboe071698e2020-01-28 10:04:42 -0700277 struct idr personality_idr;
278
Jens Axboe206aefd2019-11-07 18:27:42 -0700279 struct {
280 unsigned cached_cq_tail;
281 unsigned cq_entries;
282 unsigned cq_mask;
283 atomic_t cq_timeouts;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700284 unsigned long cq_check_overflow;
Jens Axboe206aefd2019-11-07 18:27:42 -0700285 struct wait_queue_head cq_wait;
286 struct fasync_struct *cq_fasync;
287 struct eventfd_ctx *cq_ev_fd;
288 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700289
290 struct {
291 struct mutex uring_lock;
292 wait_queue_head_t wait;
293 } ____cacheline_aligned_in_smp;
294
295 struct {
296 spinlock_t completion_lock;
Jens Axboee94f1412019-12-19 12:06:02 -0700297 struct llist_head poll_llist;
298
Jens Axboedef596e2019-01-09 08:59:42 -0700299 /*
300 * ->poll_list is protected by the ctx->uring_lock for
301 * io_uring instances that don't use IORING_SETUP_SQPOLL.
302 * For SQPOLL, only the single threaded io_sq_thread() will
303 * manipulate the list, hence no extra locking is needed there.
304 */
305 struct list_head poll_list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700306 struct hlist_head *cancel_hash;
307 unsigned cancel_hash_bits;
Jens Axboee94f1412019-12-19 12:06:02 -0700308 bool poll_multi_file;
Jens Axboefcb323c2019-10-24 12:39:47 -0600309
310 spinlock_t inflight_lock;
311 struct list_head inflight_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700312 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700313};
314
Jens Axboe09bb8392019-03-13 12:39:28 -0600315/*
316 * First field must be the file pointer in all the
317 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
318 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700319struct io_poll_iocb {
320 struct file *file;
Jens Axboe0969e782019-12-17 18:40:57 -0700321 union {
322 struct wait_queue_head *head;
323 u64 addr;
324 };
Jens Axboe221c5eb2019-01-17 09:41:58 -0700325 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600326 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700327 bool canceled;
Jens Axboe392edb42019-12-09 17:52:20 -0700328 struct wait_queue_entry wait;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700329};
330
Jens Axboeb5dba592019-12-11 14:02:38 -0700331struct io_close {
332 struct file *file;
333 struct file *put_file;
334 int fd;
335};
336
Jens Axboead8a48a2019-11-15 08:49:11 -0700337struct io_timeout_data {
338 struct io_kiocb *req;
339 struct hrtimer timer;
340 struct timespec64 ts;
341 enum hrtimer_mode mode;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +0300342 u32 seq_offset;
Jens Axboead8a48a2019-11-15 08:49:11 -0700343};
344
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700345struct io_accept {
346 struct file *file;
347 struct sockaddr __user *addr;
348 int __user *addr_len;
349 int flags;
350};
351
352struct io_sync {
353 struct file *file;
354 loff_t len;
355 loff_t off;
356 int flags;
Jens Axboed63d1b52019-12-10 10:38:56 -0700357 int mode;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700358};
359
Jens Axboefbf23842019-12-17 18:45:56 -0700360struct io_cancel {
361 struct file *file;
362 u64 addr;
363};
364
Jens Axboeb29472e2019-12-17 18:50:29 -0700365struct io_timeout {
366 struct file *file;
367 u64 addr;
368 int flags;
Jens Axboe26a61672019-12-20 09:02:01 -0700369 unsigned count;
Jens Axboeb29472e2019-12-17 18:50:29 -0700370};
371
Jens Axboe9adbd452019-12-20 08:45:55 -0700372struct io_rw {
373 /* NOTE: kiocb has the file as the first member, so don't do it here */
374 struct kiocb kiocb;
375 u64 addr;
376 u64 len;
377};
378
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700379struct io_connect {
380 struct file *file;
381 struct sockaddr __user *addr;
382 int addr_len;
383};
384
Jens Axboee47293f2019-12-20 08:58:21 -0700385struct io_sr_msg {
386 struct file *file;
Jens Axboefddafac2020-01-04 20:19:44 -0700387 union {
388 struct user_msghdr __user *msg;
389 void __user *buf;
390 };
Jens Axboee47293f2019-12-20 08:58:21 -0700391 int msg_flags;
Jens Axboefddafac2020-01-04 20:19:44 -0700392 size_t len;
Jens Axboee47293f2019-12-20 08:58:21 -0700393};
394
Jens Axboe15b71ab2019-12-11 11:20:36 -0700395struct io_open {
396 struct file *file;
397 int dfd;
Jens Axboeeddc7ef2019-12-13 21:18:10 -0700398 union {
Jens Axboeeddc7ef2019-12-13 21:18:10 -0700399 unsigned mask;
400 };
Jens Axboe15b71ab2019-12-11 11:20:36 -0700401 struct filename *filename;
Jens Axboeeddc7ef2019-12-13 21:18:10 -0700402 struct statx __user *buffer;
Jens Axboec12cedf2020-01-08 17:41:21 -0700403 struct open_how how;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700404};
405
Jens Axboe05f3fb32019-12-09 11:22:50 -0700406struct io_files_update {
407 struct file *file;
408 u64 arg;
409 u32 nr_args;
410 u32 offset;
411};
412
Jens Axboe4840e412019-12-25 22:03:45 -0700413struct io_fadvise {
414 struct file *file;
415 u64 offset;
416 u32 len;
417 u32 advice;
418};
419
Jens Axboec1ca7572019-12-25 22:18:28 -0700420struct io_madvise {
421 struct file *file;
422 u64 addr;
423 u32 len;
424 u32 advice;
425};
426
Jens Axboe3e4827b2020-01-08 15:18:09 -0700427struct io_epoll {
428 struct file *file;
429 int epfd;
430 int op;
431 int fd;
432 struct epoll_event event;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700433};
434
Jens Axboef499a022019-12-02 16:28:46 -0700435struct io_async_connect {
436 struct sockaddr_storage address;
437};
438
Jens Axboe03b12302019-12-02 18:50:25 -0700439struct io_async_msghdr {
440 struct iovec fast_iov[UIO_FASTIOV];
441 struct iovec *iov;
442 struct sockaddr __user *uaddr;
443 struct msghdr msg;
444};
445
Jens Axboef67676d2019-12-02 11:03:47 -0700446struct io_async_rw {
447 struct iovec fast_iov[UIO_FASTIOV];
448 struct iovec *iov;
449 ssize_t nr_segs;
450 ssize_t size;
451};
452
Jens Axboe15b71ab2019-12-11 11:20:36 -0700453struct io_async_open {
454 struct filename *filename;
455};
456
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700457struct io_async_ctx {
Jens Axboef67676d2019-12-02 11:03:47 -0700458 union {
459 struct io_async_rw rw;
Jens Axboe03b12302019-12-02 18:50:25 -0700460 struct io_async_msghdr msg;
Jens Axboef499a022019-12-02 16:28:46 -0700461 struct io_async_connect connect;
Jens Axboe2d283902019-12-04 11:08:05 -0700462 struct io_timeout_data timeout;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700463 struct io_async_open open;
Jens Axboef67676d2019-12-02 11:03:47 -0700464 };
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700465};
466
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300467enum {
468 REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT,
469 REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT,
470 REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT,
471 REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT,
472 REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT,
473
474 REQ_F_LINK_NEXT_BIT,
475 REQ_F_FAIL_LINK_BIT,
476 REQ_F_INFLIGHT_BIT,
477 REQ_F_CUR_POS_BIT,
478 REQ_F_NOWAIT_BIT,
479 REQ_F_IOPOLL_COMPLETED_BIT,
480 REQ_F_LINK_TIMEOUT_BIT,
481 REQ_F_TIMEOUT_BIT,
482 REQ_F_ISREG_BIT,
483 REQ_F_MUST_PUNT_BIT,
484 REQ_F_TIMEOUT_NOSEQ_BIT,
485 REQ_F_COMP_LOCKED_BIT,
486};
487
488enum {
489 /* ctx owns file */
490 REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT),
491 /* drain existing IO first */
492 REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT),
493 /* linked sqes */
494 REQ_F_LINK = BIT(REQ_F_LINK_BIT),
495 /* doesn't sever on completion < 0 */
496 REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT),
497 /* IOSQE_ASYNC */
498 REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT),
499
500 /* already grabbed next link */
501 REQ_F_LINK_NEXT = BIT(REQ_F_LINK_NEXT_BIT),
502 /* fail rest of links */
503 REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT),
504 /* on inflight list */
505 REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT),
506 /* read/write uses file position */
507 REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT),
508 /* must not punt to workers */
509 REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
510 /* polled IO has completed */
511 REQ_F_IOPOLL_COMPLETED = BIT(REQ_F_IOPOLL_COMPLETED_BIT),
512 /* has linked timeout */
513 REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT),
514 /* timeout request */
515 REQ_F_TIMEOUT = BIT(REQ_F_TIMEOUT_BIT),
516 /* regular file */
517 REQ_F_ISREG = BIT(REQ_F_ISREG_BIT),
518 /* must be punted even for NONBLOCK */
519 REQ_F_MUST_PUNT = BIT(REQ_F_MUST_PUNT_BIT),
520 /* no timeout sequence */
521 REQ_F_TIMEOUT_NOSEQ = BIT(REQ_F_TIMEOUT_NOSEQ_BIT),
522 /* completion under lock */
523 REQ_F_COMP_LOCKED = BIT(REQ_F_COMP_LOCKED_BIT),
524};
525
Jens Axboe09bb8392019-03-13 12:39:28 -0600526/*
527 * NOTE! Each of the iocb union members has the file pointer
528 * as the first entry in their struct definition. So you can
529 * access the file pointer through any of the sub-structs,
530 * or directly as just 'ki_filp' in this struct.
531 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700532struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700533 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600534 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700535 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700536 struct io_poll_iocb poll;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700537 struct io_accept accept;
538 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700539 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700540 struct io_timeout timeout;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700541 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700542 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700543 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700544 struct io_close close;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700545 struct io_files_update files_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700546 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700547 struct io_madvise madvise;
Jens Axboe3e4827b2020-01-08 15:18:09 -0700548 struct io_epoll epoll;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700549 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700550
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700551 struct io_async_ctx *io;
Pavel Begunkovb14cca02020-01-17 04:45:59 +0300552 /*
553 * llist_node is only used for poll deferred completions
554 */
555 struct llist_node llist_node;
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300556 bool in_async;
557 bool needs_fixed_file;
Jens Axboed625c6e2019-12-17 19:53:05 -0700558 u8 opcode;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700559
560 struct io_ring_ctx *ctx;
Jens Axboeeac406c2019-11-14 12:09:58 -0700561 union {
562 struct list_head list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700563 struct hlist_node hash_node;
Jens Axboeeac406c2019-11-14 12:09:58 -0700564 };
Jens Axboe9e645e112019-05-10 16:07:28 -0600565 struct list_head link_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700566 unsigned int flags;
Jens Axboec16361c2019-01-17 08:39:48 -0700567 refcount_t refs;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700568 u64 user_data;
Jens Axboe9e645e112019-05-10 16:07:28 -0600569 u32 result;
Jens Axboede0617e2019-04-06 21:51:27 -0600570 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700571
Jens Axboefcb323c2019-10-24 12:39:47 -0600572 struct list_head inflight_entry;
573
Jens Axboe561fb042019-10-24 07:25:42 -0600574 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700575};
576
577#define IO_PLUG_THRESHOLD 2
Jens Axboedef596e2019-01-09 08:59:42 -0700578#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700579
Jens Axboe9a56a232019-01-09 09:06:50 -0700580struct io_submit_state {
581 struct blk_plug plug;
582
583 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700584 * io_kiocb alloc cache
585 */
586 void *reqs[IO_IOPOLL_BATCH];
Pavel Begunkov6c8a3132020-02-01 03:58:00 +0300587 unsigned int free_reqs;
Jens Axboe2579f912019-01-09 09:10:43 -0700588
589 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700590 * File reference cache
591 */
592 struct file *file;
593 unsigned int fd;
594 unsigned int has_refs;
595 unsigned int used_refs;
596 unsigned int ios_left;
597};
598
Jens Axboed3656342019-12-18 09:50:26 -0700599struct io_op_def {
600 /* needs req->io allocated for deferral/async */
601 unsigned async_ctx : 1;
602 /* needs current->mm setup, does mm access */
603 unsigned needs_mm : 1;
604 /* needs req->file assigned */
605 unsigned needs_file : 1;
606 /* needs req->file assigned IFF fd is >= 0 */
607 unsigned fd_non_neg : 1;
608 /* hash wq insertion if file is a regular file */
609 unsigned hash_reg_file : 1;
610 /* unbound wq insertion if file is a non-regular file */
611 unsigned unbound_nonreg_file : 1;
Jens Axboe66f4af92020-01-16 15:36:52 -0700612 /* opcode is not supported by this kernel */
613 unsigned not_supported : 1;
Jens Axboef86cd202020-01-29 13:46:44 -0700614 /* needs file table */
615 unsigned file_table : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700616};
617
618static const struct io_op_def io_op_defs[] = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300619 [IORING_OP_NOP] = {},
620 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700621 .async_ctx = 1,
622 .needs_mm = 1,
623 .needs_file = 1,
624 .unbound_nonreg_file = 1,
625 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300626 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700627 .async_ctx = 1,
628 .needs_mm = 1,
629 .needs_file = 1,
630 .hash_reg_file = 1,
631 .unbound_nonreg_file = 1,
632 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300633 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700634 .needs_file = 1,
635 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300636 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700637 .needs_file = 1,
638 .unbound_nonreg_file = 1,
639 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300640 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700641 .needs_file = 1,
642 .hash_reg_file = 1,
643 .unbound_nonreg_file = 1,
644 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300645 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700646 .needs_file = 1,
647 .unbound_nonreg_file = 1,
648 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300649 [IORING_OP_POLL_REMOVE] = {},
650 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700651 .needs_file = 1,
652 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300653 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700654 .async_ctx = 1,
655 .needs_mm = 1,
656 .needs_file = 1,
657 .unbound_nonreg_file = 1,
658 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300659 [IORING_OP_RECVMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700660 .async_ctx = 1,
661 .needs_mm = 1,
662 .needs_file = 1,
663 .unbound_nonreg_file = 1,
664 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300665 [IORING_OP_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700666 .async_ctx = 1,
667 .needs_mm = 1,
668 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300669 [IORING_OP_TIMEOUT_REMOVE] = {},
670 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700671 .needs_mm = 1,
672 .needs_file = 1,
673 .unbound_nonreg_file = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700674 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700675 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300676 [IORING_OP_ASYNC_CANCEL] = {},
677 [IORING_OP_LINK_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700678 .async_ctx = 1,
679 .needs_mm = 1,
680 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300681 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700682 .async_ctx = 1,
683 .needs_mm = 1,
684 .needs_file = 1,
685 .unbound_nonreg_file = 1,
686 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300687 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700688 .needs_file = 1,
689 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300690 [IORING_OP_OPENAT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700691 .needs_file = 1,
692 .fd_non_neg = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700693 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700694 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300695 [IORING_OP_CLOSE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700696 .needs_file = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700697 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700698 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300699 [IORING_OP_FILES_UPDATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700700 .needs_mm = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700701 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700702 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300703 [IORING_OP_STATX] = {
Jens Axboed3656342019-12-18 09:50:26 -0700704 .needs_mm = 1,
705 .needs_file = 1,
706 .fd_non_neg = 1,
707 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300708 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700709 .needs_mm = 1,
710 .needs_file = 1,
711 .unbound_nonreg_file = 1,
712 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300713 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700714 .needs_mm = 1,
715 .needs_file = 1,
716 .unbound_nonreg_file = 1,
717 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300718 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -0700719 .needs_file = 1,
720 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300721 [IORING_OP_MADVISE] = {
Jens Axboec1ca7572019-12-25 22:18:28 -0700722 .needs_mm = 1,
723 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300724 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700725 .needs_mm = 1,
726 .needs_file = 1,
727 .unbound_nonreg_file = 1,
728 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300729 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700730 .needs_mm = 1,
731 .needs_file = 1,
732 .unbound_nonreg_file = 1,
733 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300734 [IORING_OP_OPENAT2] = {
Jens Axboecebdb982020-01-08 17:59:24 -0700735 .needs_file = 1,
736 .fd_non_neg = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700737 .file_table = 1,
Jens Axboecebdb982020-01-08 17:59:24 -0700738 },
Jens Axboe3e4827b2020-01-08 15:18:09 -0700739 [IORING_OP_EPOLL_CTL] = {
740 .unbound_nonreg_file = 1,
741 .file_table = 1,
742 },
Jens Axboed3656342019-12-18 09:50:26 -0700743};
744
Jens Axboe561fb042019-10-24 07:25:42 -0600745static void io_wq_submit_work(struct io_wq_work **workptr);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700746static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800747static void io_put_req(struct io_kiocb *req);
Jens Axboe978db572019-11-14 22:39:04 -0700748static void __io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700749static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
750static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -0700751static int __io_sqe_files_update(struct io_ring_ctx *ctx,
752 struct io_uring_files_update *ip,
753 unsigned nr_args);
Jens Axboef86cd202020-01-29 13:46:44 -0700754static int io_grab_files(struct io_kiocb *req);
Jens Axboe2faf8522020-02-04 19:54:55 -0700755static void io_ring_file_ref_flush(struct fixed_file_data *data);
Jens Axboede0617e2019-04-06 21:51:27 -0600756
Jens Axboe2b188cc2019-01-07 10:46:33 -0700757static struct kmem_cache *req_cachep;
758
759static const struct file_operations io_uring_fops;
760
761struct sock *io_uring_get_socket(struct file *file)
762{
763#if defined(CONFIG_UNIX)
764 if (file->f_op == &io_uring_fops) {
765 struct io_ring_ctx *ctx = file->private_data;
766
767 return ctx->ring_sock->sk;
768 }
769#endif
770 return NULL;
771}
772EXPORT_SYMBOL(io_uring_get_socket);
773
774static void io_ring_ctx_ref_free(struct percpu_ref *ref)
775{
776 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
777
Jens Axboe206aefd2019-11-07 18:27:42 -0700778 complete(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700779}
780
781static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
782{
783 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -0700784 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700785
786 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
787 if (!ctx)
788 return NULL;
789
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700790 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
791 if (!ctx->fallback_req)
792 goto err;
793
Jens Axboe206aefd2019-11-07 18:27:42 -0700794 ctx->completions = kmalloc(2 * sizeof(struct completion), GFP_KERNEL);
795 if (!ctx->completions)
796 goto err;
797
Jens Axboe78076bb2019-12-04 19:56:40 -0700798 /*
799 * Use 5 bits less than the max cq entries, that should give us around
800 * 32 entries per hash list if totally full and uniformly spread.
801 */
802 hash_bits = ilog2(p->cq_entries);
803 hash_bits -= 5;
804 if (hash_bits <= 0)
805 hash_bits = 1;
806 ctx->cancel_hash_bits = hash_bits;
807 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
808 GFP_KERNEL);
809 if (!ctx->cancel_hash)
810 goto err;
811 __hash_init(ctx->cancel_hash, 1U << hash_bits);
812
Roman Gushchin21482892019-05-07 10:01:48 -0700813 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -0700814 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
815 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700816
817 ctx->flags = p->flags;
818 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700819 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe206aefd2019-11-07 18:27:42 -0700820 init_completion(&ctx->completions[0]);
821 init_completion(&ctx->completions[1]);
Jens Axboe071698e2020-01-28 10:04:42 -0700822 idr_init(&ctx->personality_idr);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700823 mutex_init(&ctx->uring_lock);
824 init_waitqueue_head(&ctx->wait);
825 spin_lock_init(&ctx->completion_lock);
Jens Axboee94f1412019-12-19 12:06:02 -0700826 init_llist_head(&ctx->poll_llist);
Jens Axboedef596e2019-01-09 08:59:42 -0700827 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboede0617e2019-04-06 21:51:27 -0600828 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -0600829 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -0600830 init_waitqueue_head(&ctx->inflight_wait);
831 spin_lock_init(&ctx->inflight_lock);
832 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700833 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -0700834err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700835 if (ctx->fallback_req)
836 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe206aefd2019-11-07 18:27:42 -0700837 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -0700838 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -0700839 kfree(ctx);
840 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700841}
842
Bob Liu9d858b22019-11-13 18:06:25 +0800843static inline bool __req_need_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -0600844{
Jackie Liua197f662019-11-08 08:09:12 -0700845 struct io_ring_ctx *ctx = req->ctx;
846
Jens Axboe498ccd92019-10-25 10:04:25 -0600847 return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped
848 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboede0617e2019-04-06 21:51:27 -0600849}
850
Bob Liu9d858b22019-11-13 18:06:25 +0800851static inline bool req_need_defer(struct io_kiocb *req)
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600852{
Pavel Begunkov87987892020-01-18 01:22:30 +0300853 if (unlikely(req->flags & REQ_F_IO_DRAIN))
Bob Liu9d858b22019-11-13 18:06:25 +0800854 return __req_need_defer(req);
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600855
Bob Liu9d858b22019-11-13 18:06:25 +0800856 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600857}
858
859static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -0600860{
861 struct io_kiocb *req;
862
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600863 req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list);
Bob Liu9d858b22019-11-13 18:06:25 +0800864 if (req && !req_need_defer(req)) {
Jens Axboede0617e2019-04-06 21:51:27 -0600865 list_del_init(&req->list);
866 return req;
867 }
868
869 return NULL;
870}
871
Jens Axboe5262f562019-09-17 12:26:57 -0600872static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx)
873{
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600874 struct io_kiocb *req;
875
876 req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list);
Jens Axboe93bd25b2019-11-11 23:34:31 -0700877 if (req) {
878 if (req->flags & REQ_F_TIMEOUT_NOSEQ)
879 return NULL;
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800880 if (!__req_need_defer(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -0700881 list_del_init(&req->list);
882 return req;
883 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600884 }
885
886 return NULL;
Jens Axboe5262f562019-09-17 12:26:57 -0600887}
888
Jens Axboede0617e2019-04-06 21:51:27 -0600889static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700890{
Hristo Venev75b28af2019-08-26 17:23:46 +0000891 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700892
Pavel Begunkov07910152020-01-17 03:52:46 +0300893 /* order cqe stores with ring update */
894 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700895
Pavel Begunkov07910152020-01-17 03:52:46 +0300896 if (wq_has_sleeper(&ctx->cq_wait)) {
897 wake_up_interruptible(&ctx->cq_wait);
898 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700899 }
900}
901
Jens Axboecccf0ee2020-01-27 16:34:48 -0700902static inline void io_req_work_grab_env(struct io_kiocb *req,
903 const struct io_op_def *def)
Jens Axboe18d9be12019-09-10 09:13:05 -0600904{
Jens Axboecccf0ee2020-01-27 16:34:48 -0700905 if (!req->work.mm && def->needs_mm) {
906 mmgrab(current->mm);
907 req->work.mm = current->mm;
908 }
909 if (!req->work.creds)
910 req->work.creds = get_current_cred();
911}
912
913static inline void io_req_work_drop_env(struct io_kiocb *req)
914{
915 if (req->work.mm) {
916 mmdrop(req->work.mm);
917 req->work.mm = NULL;
918 }
919 if (req->work.creds) {
920 put_cred(req->work.creds);
921 req->work.creds = NULL;
922 }
Jens Axboe561fb042019-10-24 07:25:42 -0600923}
924
Jens Axboe94ae5e72019-11-14 19:39:52 -0700925static inline bool io_prep_async_work(struct io_kiocb *req,
926 struct io_kiocb **link)
Jens Axboe561fb042019-10-24 07:25:42 -0600927{
Jens Axboed3656342019-12-18 09:50:26 -0700928 const struct io_op_def *def = &io_op_defs[req->opcode];
Jens Axboe561fb042019-10-24 07:25:42 -0600929 bool do_hashed = false;
Jens Axboe54a91f32019-09-10 09:15:04 -0600930
Jens Axboed3656342019-12-18 09:50:26 -0700931 if (req->flags & REQ_F_ISREG) {
932 if (def->hash_reg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -0700933 do_hashed = true;
Jens Axboed3656342019-12-18 09:50:26 -0700934 } else {
935 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -0700936 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -0600937 }
Jens Axboecccf0ee2020-01-27 16:34:48 -0700938
939 io_req_work_grab_env(req, def);
Jens Axboe54a91f32019-09-10 09:15:04 -0600940
Jens Axboe94ae5e72019-11-14 19:39:52 -0700941 *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -0600942 return do_hashed;
943}
944
Jackie Liua197f662019-11-08 08:09:12 -0700945static inline void io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -0600946{
Jackie Liua197f662019-11-08 08:09:12 -0700947 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe94ae5e72019-11-14 19:39:52 -0700948 struct io_kiocb *link;
949 bool do_hashed;
950
951 do_hashed = io_prep_async_work(req, &link);
Jens Axboe561fb042019-10-24 07:25:42 -0600952
953 trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work,
954 req->flags);
955 if (!do_hashed) {
956 io_wq_enqueue(ctx->io_wq, &req->work);
957 } else {
958 io_wq_enqueue_hashed(ctx->io_wq, &req->work,
959 file_inode(req->file));
960 }
Jens Axboe94ae5e72019-11-14 19:39:52 -0700961
962 if (link)
963 io_queue_linked_timeout(link);
Jens Axboe18d9be12019-09-10 09:13:05 -0600964}
965
Jens Axboe5262f562019-09-17 12:26:57 -0600966static void io_kill_timeout(struct io_kiocb *req)
967{
968 int ret;
969
Jens Axboe2d283902019-12-04 11:08:05 -0700970 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe5262f562019-09-17 12:26:57 -0600971 if (ret != -1) {
972 atomic_inc(&req->ctx->cq_timeouts);
Jens Axboe842f9612019-10-29 12:34:10 -0600973 list_del_init(&req->list);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700974 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800975 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -0600976 }
977}
978
979static void io_kill_timeouts(struct io_ring_ctx *ctx)
980{
981 struct io_kiocb *req, *tmp;
982
983 spin_lock_irq(&ctx->completion_lock);
984 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
985 io_kill_timeout(req);
986 spin_unlock_irq(&ctx->completion_lock);
987}
988
Jens Axboede0617e2019-04-06 21:51:27 -0600989static void io_commit_cqring(struct io_ring_ctx *ctx)
990{
991 struct io_kiocb *req;
992
Jens Axboe5262f562019-09-17 12:26:57 -0600993 while ((req = io_get_timeout_req(ctx)) != NULL)
994 io_kill_timeout(req);
995
Jens Axboede0617e2019-04-06 21:51:27 -0600996 __io_commit_cqring(ctx);
997
Pavel Begunkov87987892020-01-18 01:22:30 +0300998 while ((req = io_get_deferred_req(ctx)) != NULL)
Jackie Liua197f662019-11-08 08:09:12 -0700999 io_queue_async_work(req);
Jens Axboede0617e2019-04-06 21:51:27 -06001000}
1001
Jens Axboe2b188cc2019-01-07 10:46:33 -07001002static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
1003{
Hristo Venev75b28af2019-08-26 17:23:46 +00001004 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001005 unsigned tail;
1006
1007 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +02001008 /*
1009 * writes to the cq entry need to come after reading head; the
1010 * control dependency is enough as we're using WRITE_ONCE to
1011 * fill the cq entry
1012 */
Hristo Venev75b28af2019-08-26 17:23:46 +00001013 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001014 return NULL;
1015
1016 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +00001017 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001018}
1019
Jens Axboef2842ab2020-01-08 11:04:00 -07001020static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1021{
Jens Axboef0b493e2020-02-01 21:30:11 -07001022 if (!ctx->cq_ev_fd)
1023 return false;
Jens Axboef2842ab2020-01-08 11:04:00 -07001024 if (!ctx->eventfd_async)
1025 return true;
1026 return io_wq_current_is_worker() || in_interrupt();
1027}
1028
Jens Axboef0b493e2020-02-01 21:30:11 -07001029static void __io_cqring_ev_posted(struct io_ring_ctx *ctx, bool trigger_ev)
Jens Axboe8c838782019-03-12 15:48:16 -06001030{
1031 if (waitqueue_active(&ctx->wait))
1032 wake_up(&ctx->wait);
1033 if (waitqueue_active(&ctx->sqo_wait))
1034 wake_up(&ctx->sqo_wait);
Jens Axboef0b493e2020-02-01 21:30:11 -07001035 if (trigger_ev)
Jens Axboe9b402842019-04-11 11:45:41 -06001036 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -06001037}
1038
Jens Axboef0b493e2020-02-01 21:30:11 -07001039static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
1040{
1041 __io_cqring_ev_posted(ctx, io_should_trigger_evfd(ctx));
1042}
1043
Jens Axboec4a2ed72019-11-21 21:01:26 -07001044/* Returns true if there are no backlogged entries after the flush */
1045static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001046{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001047 struct io_rings *rings = ctx->rings;
1048 struct io_uring_cqe *cqe;
1049 struct io_kiocb *req;
1050 unsigned long flags;
1051 LIST_HEAD(list);
1052
1053 if (!force) {
1054 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001055 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001056 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
1057 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001058 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001059 }
1060
1061 spin_lock_irqsave(&ctx->completion_lock, flags);
1062
1063 /* if force is set, the ring is going away. always drop after that */
1064 if (force)
Jens Axboe69b3e542020-01-08 11:01:46 -07001065 ctx->cq_overflow_flushed = 1;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001066
Jens Axboec4a2ed72019-11-21 21:01:26 -07001067 cqe = NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001068 while (!list_empty(&ctx->cq_overflow_list)) {
1069 cqe = io_get_cqring(ctx);
1070 if (!cqe && !force)
1071 break;
1072
1073 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
1074 list);
1075 list_move(&req->list, &list);
1076 if (cqe) {
1077 WRITE_ONCE(cqe->user_data, req->user_data);
1078 WRITE_ONCE(cqe->res, req->result);
1079 WRITE_ONCE(cqe->flags, 0);
1080 } else {
1081 WRITE_ONCE(ctx->rings->cq_overflow,
1082 atomic_inc_return(&ctx->cached_cq_overflow));
1083 }
1084 }
1085
1086 io_commit_cqring(ctx);
Jens Axboead3eb2c2019-12-18 17:12:20 -07001087 if (cqe) {
1088 clear_bit(0, &ctx->sq_check_overflow);
1089 clear_bit(0, &ctx->cq_check_overflow);
1090 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001091 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1092 io_cqring_ev_posted(ctx);
1093
1094 while (!list_empty(&list)) {
1095 req = list_first_entry(&list, struct io_kiocb, list);
1096 list_del(&req->list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001097 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001098 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001099
1100 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001101}
1102
Jens Axboe78e19bb2019-11-06 15:21:34 -07001103static void io_cqring_fill_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001104{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001105 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001106 struct io_uring_cqe *cqe;
1107
Jens Axboe78e19bb2019-11-06 15:21:34 -07001108 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001109
Jens Axboe2b188cc2019-01-07 10:46:33 -07001110 /*
1111 * If we can't get a cq entry, userspace overflowed the
1112 * submission (by quite a lot). Increment the overflow count in
1113 * the ring.
1114 */
1115 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001116 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001117 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001118 WRITE_ONCE(cqe->res, res);
1119 WRITE_ONCE(cqe->flags, 0);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001120 } else if (ctx->cq_overflow_flushed) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001121 WRITE_ONCE(ctx->rings->cq_overflow,
1122 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001123 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001124 if (list_empty(&ctx->cq_overflow_list)) {
1125 set_bit(0, &ctx->sq_check_overflow);
1126 set_bit(0, &ctx->cq_check_overflow);
1127 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001128 refcount_inc(&req->refs);
1129 req->result = res;
1130 list_add_tail(&req->list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001131 }
1132}
1133
Jens Axboe78e19bb2019-11-06 15:21:34 -07001134static void io_cqring_add_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001135{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001136 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001137 unsigned long flags;
1138
1139 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001140 io_cqring_fill_event(req, res);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001141 io_commit_cqring(ctx);
1142 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1143
Jens Axboe8c838782019-03-12 15:48:16 -06001144 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001145}
1146
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001147static inline bool io_is_fallback_req(struct io_kiocb *req)
1148{
1149 return req == (struct io_kiocb *)
1150 ((unsigned long) req->ctx->fallback_req & ~1UL);
1151}
1152
1153static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
1154{
1155 struct io_kiocb *req;
1156
1157 req = ctx->fallback_req;
1158 if (!test_and_set_bit_lock(0, (unsigned long *) ctx->fallback_req))
1159 return req;
1160
1161 return NULL;
1162}
1163
Jens Axboe2579f912019-01-09 09:10:43 -07001164static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
1165 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001166{
Jens Axboefd6fab22019-03-14 16:30:06 -06001167 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001168 struct io_kiocb *req;
1169
Jens Axboe2579f912019-01-09 09:10:43 -07001170 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -06001171 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -07001172 if (unlikely(!req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001173 goto fallback;
Jens Axboe2579f912019-01-09 09:10:43 -07001174 } else if (!state->free_reqs) {
1175 size_t sz;
1176 int ret;
1177
1178 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -06001179 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
1180
1181 /*
1182 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1183 * retry single alloc to be on the safe side.
1184 */
1185 if (unlikely(ret <= 0)) {
1186 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1187 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001188 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -06001189 ret = 1;
1190 }
Jens Axboe2579f912019-01-09 09:10:43 -07001191 state->free_reqs = ret - 1;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001192 req = state->reqs[ret - 1];
Jens Axboe2579f912019-01-09 09:10:43 -07001193 } else {
Jens Axboe2579f912019-01-09 09:10:43 -07001194 state->free_reqs--;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001195 req = state->reqs[state->free_reqs];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001196 }
1197
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001198got_it:
Jens Axboe1a6b74f2019-12-02 10:33:15 -07001199 req->io = NULL;
Jens Axboe60c112b2019-06-21 10:20:18 -06001200 req->file = NULL;
Jens Axboe2579f912019-01-09 09:10:43 -07001201 req->ctx = ctx;
1202 req->flags = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06001203 /* one is dropped after submission, the other at completion */
1204 refcount_set(&req->refs, 2);
Jens Axboe9e645e112019-05-10 16:07:28 -06001205 req->result = 0;
Jens Axboe561fb042019-10-24 07:25:42 -06001206 INIT_IO_WORK(&req->work, io_wq_submit_work);
Jens Axboe2579f912019-01-09 09:10:43 -07001207 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001208fallback:
1209 req = io_get_fallback_req(ctx);
1210 if (req)
1211 goto got_it;
Pavel Begunkov6805b322019-10-08 02:18:42 +03001212 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001213 return NULL;
1214}
1215
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001216static void __io_req_do_free(struct io_kiocb *req)
Jens Axboedef596e2019-01-09 08:59:42 -07001217{
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001218 if (likely(!io_is_fallback_req(req)))
1219 kmem_cache_free(req_cachep, req);
1220 else
1221 clear_bit_unlock(0, (unsigned long *) req->ctx->fallback_req);
1222}
1223
Jens Axboec6ca97b302019-12-28 12:11:08 -07001224static void __io_req_aux_free(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001225{
Jens Axboefcb323c2019-10-24 12:39:47 -06001226 struct io_ring_ctx *ctx = req->ctx;
1227
YueHaibing96fd84d2020-01-07 22:22:44 +08001228 kfree(req->io);
Jens Axboe05f3fb32019-12-09 11:22:50 -07001229 if (req->file) {
1230 if (req->flags & REQ_F_FIXED_FILE)
1231 percpu_ref_put(&ctx->file_data->refs);
1232 else
1233 fput(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001234 }
Jens Axboecccf0ee2020-01-27 16:34:48 -07001235
1236 io_req_work_drop_env(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001237}
1238
1239static void __io_free_req(struct io_kiocb *req)
1240{
Jens Axboec6ca97b302019-12-28 12:11:08 -07001241 __io_req_aux_free(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001242
Jens Axboefcb323c2019-10-24 12:39:47 -06001243 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001244 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06001245 unsigned long flags;
1246
1247 spin_lock_irqsave(&ctx->inflight_lock, flags);
1248 list_del(&req->inflight_entry);
1249 if (waitqueue_active(&ctx->inflight_wait))
1250 wake_up(&ctx->inflight_wait);
1251 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1252 }
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001253
1254 percpu_ref_put(&req->ctx->refs);
1255 __io_req_do_free(req);
Jens Axboee65ef562019-03-12 10:16:44 -06001256}
1257
Jens Axboec6ca97b302019-12-28 12:11:08 -07001258struct req_batch {
1259 void *reqs[IO_IOPOLL_BATCH];
1260 int to_free;
1261 int need_iter;
1262};
1263
1264static void io_free_req_many(struct io_ring_ctx *ctx, struct req_batch *rb)
1265{
Jens Axboe10fef4b2020-01-09 07:52:28 -07001266 int fixed_refs = rb->to_free;
1267
Jens Axboec6ca97b302019-12-28 12:11:08 -07001268 if (!rb->to_free)
1269 return;
1270 if (rb->need_iter) {
1271 int i, inflight = 0;
1272 unsigned long flags;
1273
Jens Axboe10fef4b2020-01-09 07:52:28 -07001274 fixed_refs = 0;
Jens Axboec6ca97b302019-12-28 12:11:08 -07001275 for (i = 0; i < rb->to_free; i++) {
1276 struct io_kiocb *req = rb->reqs[i];
1277
Jens Axboe10fef4b2020-01-09 07:52:28 -07001278 if (req->flags & REQ_F_FIXED_FILE) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001279 req->file = NULL;
Jens Axboe10fef4b2020-01-09 07:52:28 -07001280 fixed_refs++;
1281 }
Jens Axboec6ca97b302019-12-28 12:11:08 -07001282 if (req->flags & REQ_F_INFLIGHT)
1283 inflight++;
Jens Axboec6ca97b302019-12-28 12:11:08 -07001284 __io_req_aux_free(req);
1285 }
1286 if (!inflight)
1287 goto do_free;
1288
1289 spin_lock_irqsave(&ctx->inflight_lock, flags);
1290 for (i = 0; i < rb->to_free; i++) {
1291 struct io_kiocb *req = rb->reqs[i];
1292
Jens Axboe10fef4b2020-01-09 07:52:28 -07001293 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001294 list_del(&req->inflight_entry);
1295 if (!--inflight)
1296 break;
1297 }
1298 }
1299 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1300
1301 if (waitqueue_active(&ctx->inflight_wait))
1302 wake_up(&ctx->inflight_wait);
1303 }
1304do_free:
1305 kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs);
Jens Axboe10fef4b2020-01-09 07:52:28 -07001306 if (fixed_refs)
1307 percpu_ref_put_many(&ctx->file_data->refs, fixed_refs);
Jens Axboec6ca97b302019-12-28 12:11:08 -07001308 percpu_ref_put_many(&ctx->refs, rb->to_free);
Jens Axboec6ca97b302019-12-28 12:11:08 -07001309 rb->to_free = rb->need_iter = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06001310}
1311
Jackie Liua197f662019-11-08 08:09:12 -07001312static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001313{
Jackie Liua197f662019-11-08 08:09:12 -07001314 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001315 int ret;
1316
Jens Axboe2d283902019-12-04 11:08:05 -07001317 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe2665abf2019-11-05 12:40:47 -07001318 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001319 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -07001320 io_commit_cqring(ctx);
1321 req->flags &= ~REQ_F_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001322 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001323 return true;
1324 }
1325
1326 return false;
1327}
1328
Jens Axboeba816ad2019-09-28 11:36:45 -06001329static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -06001330{
Jens Axboe2665abf2019-11-05 12:40:47 -07001331 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001332 bool wake_ev = false;
Jens Axboe9e645e112019-05-10 16:07:28 -06001333
Jens Axboe4d7dd462019-11-20 13:03:52 -07001334 /* Already got next link */
1335 if (req->flags & REQ_F_LINK_NEXT)
1336 return;
1337
Jens Axboe9e645e112019-05-10 16:07:28 -06001338 /*
1339 * The list should never be empty when we are called here. But could
1340 * potentially happen if the chain is messed up, check to be on the
1341 * safe side.
1342 */
Pavel Begunkov44932332019-12-05 16:16:35 +03001343 while (!list_empty(&req->link_list)) {
1344 struct io_kiocb *nxt = list_first_entry(&req->link_list,
1345 struct io_kiocb, link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001346
Pavel Begunkov44932332019-12-05 16:16:35 +03001347 if (unlikely((req->flags & REQ_F_LINK_TIMEOUT) &&
1348 (nxt->flags & REQ_F_TIMEOUT))) {
1349 list_del_init(&nxt->link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001350 wake_ev |= io_link_cancel_timeout(nxt);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001351 req->flags &= ~REQ_F_LINK_TIMEOUT;
1352 continue;
1353 }
Jens Axboe9e645e112019-05-10 16:07:28 -06001354
Pavel Begunkov44932332019-12-05 16:16:35 +03001355 list_del_init(&req->link_list);
1356 if (!list_empty(&nxt->link_list))
1357 nxt->flags |= REQ_F_LINK;
Pavel Begunkovb18fdf72019-11-21 23:21:02 +03001358 *nxtptr = nxt;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001359 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06001360 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001361
Jens Axboe4d7dd462019-11-20 13:03:52 -07001362 req->flags |= REQ_F_LINK_NEXT;
Jens Axboe2665abf2019-11-05 12:40:47 -07001363 if (wake_ev)
1364 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001365}
1366
1367/*
1368 * Called if REQ_F_LINK is set, and we fail the head request
1369 */
1370static void io_fail_links(struct io_kiocb *req)
1371{
Jens Axboe2665abf2019-11-05 12:40:47 -07001372 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001373 unsigned long flags;
1374
1375 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06001376
1377 while (!list_empty(&req->link_list)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03001378 struct io_kiocb *link = list_first_entry(&req->link_list,
1379 struct io_kiocb, link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06001380
Pavel Begunkov44932332019-12-05 16:16:35 +03001381 list_del_init(&link->link_list);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001382 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001383
1384 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
Jens Axboed625c6e2019-12-17 19:53:05 -07001385 link->opcode == IORING_OP_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -07001386 io_link_cancel_timeout(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001387 } else {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001388 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe978db572019-11-14 22:39:04 -07001389 __io_double_put_req(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001390 }
Jens Axboe5d960722019-11-19 15:31:28 -07001391 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -06001392 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001393
1394 io_commit_cqring(ctx);
1395 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1396 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001397}
1398
Jens Axboe4d7dd462019-11-20 13:03:52 -07001399static void io_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -06001400{
Jens Axboe4d7dd462019-11-20 13:03:52 -07001401 if (likely(!(req->flags & REQ_F_LINK)))
Jens Axboe2665abf2019-11-05 12:40:47 -07001402 return;
Jens Axboe2665abf2019-11-05 12:40:47 -07001403
Jens Axboe9e645e112019-05-10 16:07:28 -06001404 /*
1405 * If LINK is set, we have dependent requests in this chain. If we
1406 * didn't fail this request, queue the first one up, moving any other
1407 * dependencies to the next request. In case of failure, fail the rest
1408 * of the chain.
1409 */
Jens Axboe2665abf2019-11-05 12:40:47 -07001410 if (req->flags & REQ_F_FAIL_LINK) {
1411 io_fail_links(req);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001412 } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) ==
1413 REQ_F_LINK_TIMEOUT) {
Jens Axboe2665abf2019-11-05 12:40:47 -07001414 struct io_ring_ctx *ctx = req->ctx;
1415 unsigned long flags;
1416
1417 /*
1418 * If this is a timeout link, we could be racing with the
1419 * timeout timer. Grab the completion lock for this case to
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001420 * protect against that.
Jens Axboe2665abf2019-11-05 12:40:47 -07001421 */
1422 spin_lock_irqsave(&ctx->completion_lock, flags);
1423 io_req_link_next(req, nxt);
1424 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1425 } else {
1426 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -06001427 }
Jens Axboe4d7dd462019-11-20 13:03:52 -07001428}
Jens Axboe9e645e112019-05-10 16:07:28 -06001429
Jackie Liuc69f8db2019-11-09 11:00:08 +08001430static void io_free_req(struct io_kiocb *req)
1431{
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001432 struct io_kiocb *nxt = NULL;
1433
1434 io_req_find_next(req, &nxt);
Pavel Begunkov70cf9f32019-11-21 23:21:00 +03001435 __io_free_req(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001436
1437 if (nxt)
1438 io_queue_async_work(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08001439}
1440
Jens Axboeba816ad2019-09-28 11:36:45 -06001441/*
1442 * Drop reference to request, return next in chain (if there is one) if this
1443 * was the last reference to this request.
1444 */
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001445__attribute__((nonnull))
Jackie Liuec9c02a2019-11-08 23:50:36 +08001446static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboee65ef562019-03-12 10:16:44 -06001447{
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001448 io_req_find_next(req, nxtptr);
Jens Axboe4d7dd462019-11-20 13:03:52 -07001449
Jens Axboee65ef562019-03-12 10:16:44 -06001450 if (refcount_dec_and_test(&req->refs))
Jens Axboe4d7dd462019-11-20 13:03:52 -07001451 __io_free_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001452}
1453
Jens Axboe2b188cc2019-01-07 10:46:33 -07001454static void io_put_req(struct io_kiocb *req)
1455{
Jens Axboedef596e2019-01-09 08:59:42 -07001456 if (refcount_dec_and_test(&req->refs))
1457 io_free_req(req);
1458}
1459
Jens Axboe978db572019-11-14 22:39:04 -07001460/*
1461 * Must only be used if we don't need to care about links, usually from
1462 * within the completion handling itself.
1463 */
1464static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06001465{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001466 /* drop both submit and complete references */
1467 if (refcount_sub_and_test(2, &req->refs))
1468 __io_free_req(req);
1469}
1470
Jens Axboe978db572019-11-14 22:39:04 -07001471static void io_double_put_req(struct io_kiocb *req)
1472{
1473 /* drop both submit and complete references */
1474 if (refcount_sub_and_test(2, &req->refs))
1475 io_free_req(req);
1476}
1477
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001478static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06001479{
Jens Axboe84f97dc2019-11-06 11:27:53 -07001480 struct io_rings *rings = ctx->rings;
1481
Jens Axboead3eb2c2019-12-18 17:12:20 -07001482 if (test_bit(0, &ctx->cq_check_overflow)) {
1483 /*
1484 * noflush == true is from the waitqueue handler, just ensure
1485 * we wake up the task, and the next invocation will flush the
1486 * entries. We cannot safely to it from here.
1487 */
1488 if (noflush && !list_empty(&ctx->cq_overflow_list))
1489 return -1U;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001490
Jens Axboead3eb2c2019-12-18 17:12:20 -07001491 io_cqring_overflow_flush(ctx, false);
1492 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001493
Jens Axboea3a0e432019-08-20 11:03:11 -06001494 /* See comment at the top of this file */
1495 smp_rmb();
Jens Axboead3eb2c2019-12-18 17:12:20 -07001496 return ctx->cached_cq_tail - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06001497}
1498
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03001499static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1500{
1501 struct io_rings *rings = ctx->rings;
1502
1503 /* make sure SQ entry isn't read before tail */
1504 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1505}
1506
Jens Axboe8237e042019-12-28 10:48:22 -07001507static inline bool io_req_multi_free(struct req_batch *rb, struct io_kiocb *req)
Jens Axboee94f1412019-12-19 12:06:02 -07001508{
Jens Axboec6ca97b302019-12-28 12:11:08 -07001509 if ((req->flags & REQ_F_LINK) || io_is_fallback_req(req))
1510 return false;
Jens Axboee94f1412019-12-19 12:06:02 -07001511
Jens Axboec6ca97b302019-12-28 12:11:08 -07001512 if (!(req->flags & REQ_F_FIXED_FILE) || req->io)
1513 rb->need_iter++;
1514
1515 rb->reqs[rb->to_free++] = req;
1516 if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
1517 io_free_req_many(req->ctx, rb);
1518 return true;
Jens Axboee94f1412019-12-19 12:06:02 -07001519}
1520
Jens Axboedef596e2019-01-09 08:59:42 -07001521/*
1522 * Find and free completed poll iocbs
1523 */
1524static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
1525 struct list_head *done)
1526{
Jens Axboe8237e042019-12-28 10:48:22 -07001527 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07001528 struct io_kiocb *req;
Jens Axboedef596e2019-01-09 08:59:42 -07001529
Jens Axboec6ca97b302019-12-28 12:11:08 -07001530 rb.to_free = rb.need_iter = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001531 while (!list_empty(done)) {
1532 req = list_first_entry(done, struct io_kiocb, list);
1533 list_del(&req->list);
1534
Jens Axboe78e19bb2019-11-06 15:21:34 -07001535 io_cqring_fill_event(req, req->result);
Jens Axboedef596e2019-01-09 08:59:42 -07001536 (*nr_events)++;
1537
Jens Axboe8237e042019-12-28 10:48:22 -07001538 if (refcount_dec_and_test(&req->refs) &&
1539 !io_req_multi_free(&rb, req))
1540 io_free_req(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001541 }
Jens Axboedef596e2019-01-09 08:59:42 -07001542
Jens Axboe09bb8392019-03-13 12:39:28 -06001543 io_commit_cqring(ctx);
Jens Axboe8237e042019-12-28 10:48:22 -07001544 io_free_req_many(ctx, &rb);
Jens Axboedef596e2019-01-09 08:59:42 -07001545}
1546
1547static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1548 long min)
1549{
1550 struct io_kiocb *req, *tmp;
1551 LIST_HEAD(done);
1552 bool spin;
1553 int ret;
1554
1555 /*
1556 * Only spin for completions if we don't have multiple devices hanging
1557 * off our complete list, and we're under the requested amount.
1558 */
1559 spin = !ctx->poll_multi_file && *nr_events < min;
1560
1561 ret = 0;
1562 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
Jens Axboe9adbd452019-12-20 08:45:55 -07001563 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07001564
1565 /*
1566 * Move completed entries to our local list. If we find a
1567 * request that requires polling, break out and complete
1568 * the done list first, if we have entries there.
1569 */
1570 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
1571 list_move_tail(&req->list, &done);
1572 continue;
1573 }
1574 if (!list_empty(&done))
1575 break;
1576
1577 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1578 if (ret < 0)
1579 break;
1580
1581 if (ret && spin)
1582 spin = false;
1583 ret = 0;
1584 }
1585
1586 if (!list_empty(&done))
1587 io_iopoll_complete(ctx, nr_events, &done);
1588
1589 return ret;
1590}
1591
1592/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08001593 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07001594 * non-spinning poll check - we'll still enter the driver poll loop, but only
1595 * as a non-spinning completion check.
1596 */
1597static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
1598 long min)
1599{
Jens Axboe08f54392019-08-21 22:19:11 -06001600 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07001601 int ret;
1602
1603 ret = io_do_iopoll(ctx, nr_events, min);
1604 if (ret < 0)
1605 return ret;
1606 if (!min || *nr_events >= min)
1607 return 0;
1608 }
1609
1610 return 1;
1611}
1612
1613/*
1614 * We can't just wait for polled events to come to us, we have to actively
1615 * find and complete them.
1616 */
1617static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
1618{
1619 if (!(ctx->flags & IORING_SETUP_IOPOLL))
1620 return;
1621
1622 mutex_lock(&ctx->uring_lock);
1623 while (!list_empty(&ctx->poll_list)) {
1624 unsigned int nr_events = 0;
1625
1626 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -06001627
1628 /*
1629 * Ensure we allow local-to-the-cpu processing to take place,
1630 * in this case we need to ensure that we reap all events.
1631 */
1632 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -07001633 }
1634 mutex_unlock(&ctx->uring_lock);
1635}
1636
Jens Axboe2b2ed972019-10-25 10:06:15 -06001637static int __io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1638 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07001639{
Jens Axboe2b2ed972019-10-25 10:06:15 -06001640 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001641
1642 do {
1643 int tmin = 0;
1644
Jens Axboe500f9fb2019-08-19 12:15:59 -06001645 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06001646 * Don't enter poll loop if we already have events pending.
1647 * If we do, we can potentially be spinning for commands that
1648 * already triggered a CQE (eg in error).
1649 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001650 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06001651 break;
1652
1653 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06001654 * If a submit got punted to a workqueue, we can have the
1655 * application entering polling for a command before it gets
1656 * issued. That app will hold the uring_lock for the duration
1657 * of the poll right here, so we need to take a breather every
1658 * now and then to ensure that the issue has a chance to add
1659 * the poll to the issued list. Otherwise we can spin here
1660 * forever, while the workqueue is stuck trying to acquire the
1661 * very same mutex.
1662 */
1663 if (!(++iters & 7)) {
1664 mutex_unlock(&ctx->uring_lock);
1665 mutex_lock(&ctx->uring_lock);
1666 }
1667
Jens Axboedef596e2019-01-09 08:59:42 -07001668 if (*nr_events < min)
1669 tmin = min - *nr_events;
1670
1671 ret = io_iopoll_getevents(ctx, nr_events, tmin);
1672 if (ret <= 0)
1673 break;
1674 ret = 0;
1675 } while (min && !*nr_events && !need_resched());
1676
Jens Axboe2b2ed972019-10-25 10:06:15 -06001677 return ret;
1678}
1679
1680static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1681 long min)
1682{
1683 int ret;
1684
1685 /*
1686 * We disallow the app entering submit/complete with polling, but we
1687 * still need to lock the ring to prevent racing with polled issue
1688 * that got punted to a workqueue.
1689 */
1690 mutex_lock(&ctx->uring_lock);
1691 ret = __io_iopoll_check(ctx, nr_events, min);
Jens Axboe500f9fb2019-08-19 12:15:59 -06001692 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001693 return ret;
1694}
1695
Jens Axboe491381ce2019-10-17 09:20:46 -06001696static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001697{
Jens Axboe491381ce2019-10-17 09:20:46 -06001698 /*
1699 * Tell lockdep we inherited freeze protection from submission
1700 * thread.
1701 */
1702 if (req->flags & REQ_F_ISREG) {
1703 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001704
Jens Axboe491381ce2019-10-17 09:20:46 -06001705 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001706 }
Jens Axboe491381ce2019-10-17 09:20:46 -06001707 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001708}
1709
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001710static inline void req_set_fail_links(struct io_kiocb *req)
1711{
1712 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1713 req->flags |= REQ_F_FAIL_LINK;
1714}
1715
Jens Axboeba816ad2019-09-28 11:36:45 -06001716static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001717{
Jens Axboe9adbd452019-12-20 08:45:55 -07001718 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001719
Jens Axboe491381ce2019-10-17 09:20:46 -06001720 if (kiocb->ki_flags & IOCB_WRITE)
1721 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001722
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001723 if (res != req->result)
1724 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001725 io_cqring_add_event(req, res);
Jens Axboeba816ad2019-09-28 11:36:45 -06001726}
1727
1728static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1729{
Jens Axboe9adbd452019-12-20 08:45:55 -07001730 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06001731
1732 io_complete_rw_common(kiocb, res);
Jens Axboee65ef562019-03-12 10:16:44 -06001733 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001734}
1735
Jens Axboeba816ad2019-09-28 11:36:45 -06001736static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res)
1737{
Jens Axboe9adbd452019-12-20 08:45:55 -07001738 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001739 struct io_kiocb *nxt = NULL;
Jens Axboeba816ad2019-09-28 11:36:45 -06001740
1741 io_complete_rw_common(kiocb, res);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001742 io_put_req_find_next(req, &nxt);
1743
1744 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001745}
1746
Jens Axboedef596e2019-01-09 08:59:42 -07001747static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1748{
Jens Axboe9adbd452019-12-20 08:45:55 -07001749 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07001750
Jens Axboe491381ce2019-10-17 09:20:46 -06001751 if (kiocb->ki_flags & IOCB_WRITE)
1752 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001753
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001754 if (res != req->result)
1755 req_set_fail_links(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06001756 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -07001757 if (res != -EAGAIN)
1758 req->flags |= REQ_F_IOPOLL_COMPLETED;
1759}
1760
1761/*
1762 * After the iocb has been issued, it's safe to be found on the poll list.
1763 * Adding the kiocb to the list AFTER submission ensures that we don't
1764 * find it from a io_iopoll_getevents() thread before the issuer is done
1765 * accessing the kiocb cookie.
1766 */
1767static void io_iopoll_req_issued(struct io_kiocb *req)
1768{
1769 struct io_ring_ctx *ctx = req->ctx;
1770
1771 /*
1772 * Track whether we have multiple files in our lists. This will impact
1773 * how we do polling eventually, not spinning if we're on potentially
1774 * different devices.
1775 */
1776 if (list_empty(&ctx->poll_list)) {
1777 ctx->poll_multi_file = false;
1778 } else if (!ctx->poll_multi_file) {
1779 struct io_kiocb *list_req;
1780
1781 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1782 list);
Jens Axboe9adbd452019-12-20 08:45:55 -07001783 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07001784 ctx->poll_multi_file = true;
1785 }
1786
1787 /*
1788 * For fast devices, IO may have already completed. If it has, add
1789 * it to the front so we find it first.
1790 */
1791 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1792 list_add(&req->list, &ctx->poll_list);
1793 else
1794 list_add_tail(&req->list, &ctx->poll_list);
1795}
1796
Jens Axboe3d6770f2019-04-13 11:50:54 -06001797static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07001798{
Jens Axboe3d6770f2019-04-13 11:50:54 -06001799 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -07001800 int diff = state->has_refs - state->used_refs;
1801
1802 if (diff)
1803 fput_many(state->file, diff);
1804 state->file = NULL;
1805 }
1806}
1807
1808/*
1809 * Get as many references to a file as we have IOs left in this submission,
1810 * assuming most submissions are for one file, or at least that each file
1811 * has more than one submission.
1812 */
1813static struct file *io_file_get(struct io_submit_state *state, int fd)
1814{
1815 if (!state)
1816 return fget(fd);
1817
1818 if (state->file) {
1819 if (state->fd == fd) {
1820 state->used_refs++;
1821 state->ios_left--;
1822 return state->file;
1823 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06001824 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07001825 }
1826 state->file = fget_many(fd, state->ios_left);
1827 if (!state->file)
1828 return NULL;
1829
1830 state->fd = fd;
1831 state->has_refs = state->ios_left;
1832 state->used_refs = 1;
1833 state->ios_left--;
1834 return state->file;
1835}
1836
Jens Axboe2b188cc2019-01-07 10:46:33 -07001837/*
1838 * If we tracked the file through the SCM inflight mechanism, we could support
1839 * any file. For now, just ensure that anything potentially problematic is done
1840 * inline.
1841 */
1842static bool io_file_supports_async(struct file *file)
1843{
1844 umode_t mode = file_inode(file)->i_mode;
1845
Jens Axboe10d59342019-12-09 20:16:22 -07001846 if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07001847 return true;
1848 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
1849 return true;
1850
1851 return false;
1852}
1853
Jens Axboe3529d8c2019-12-19 18:24:38 -07001854static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1855 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001856{
Jens Axboedef596e2019-01-09 08:59:42 -07001857 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07001858 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06001859 unsigned ioprio;
1860 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001861
Jens Axboe491381ce2019-10-17 09:20:46 -06001862 if (S_ISREG(file_inode(req->file)->i_mode))
1863 req->flags |= REQ_F_ISREG;
1864
Jens Axboe2b188cc2019-01-07 10:46:33 -07001865 kiocb->ki_pos = READ_ONCE(sqe->off);
Jens Axboeba042912019-12-25 16:33:42 -07001866 if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) {
1867 req->flags |= REQ_F_CUR_POS;
1868 kiocb->ki_pos = req->file->f_pos;
1869 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001870 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03001871 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
1872 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
1873 if (unlikely(ret))
1874 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001875
1876 ioprio = READ_ONCE(sqe->ioprio);
1877 if (ioprio) {
1878 ret = ioprio_check_cap(ioprio);
1879 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06001880 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001881
1882 kiocb->ki_ioprio = ioprio;
1883 } else
1884 kiocb->ki_ioprio = get_current_ioprio();
1885
Stefan Bühler8449eed2019-04-27 20:34:19 +02001886 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06001887 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
1888 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02001889 req->flags |= REQ_F_NOWAIT;
1890
1891 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001892 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001893
Jens Axboedef596e2019-01-09 08:59:42 -07001894 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07001895 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
1896 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06001897 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001898
Jens Axboedef596e2019-01-09 08:59:42 -07001899 kiocb->ki_flags |= IOCB_HIPRI;
1900 kiocb->ki_complete = io_complete_rw_iopoll;
Jens Axboe6873e0b2019-10-30 13:53:09 -06001901 req->result = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001902 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001903 if (kiocb->ki_flags & IOCB_HIPRI)
1904 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07001905 kiocb->ki_complete = io_complete_rw;
1906 }
Jens Axboe9adbd452019-12-20 08:45:55 -07001907
Jens Axboe3529d8c2019-12-19 18:24:38 -07001908 req->rw.addr = READ_ONCE(sqe->addr);
1909 req->rw.len = READ_ONCE(sqe->len);
Jens Axboe9adbd452019-12-20 08:45:55 -07001910 /* we own ->private, reuse it for the buffer index */
1911 req->rw.kiocb.private = (void *) (unsigned long)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001912 READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001913 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001914}
1915
1916static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
1917{
1918 switch (ret) {
1919 case -EIOCBQUEUED:
1920 break;
1921 case -ERESTARTSYS:
1922 case -ERESTARTNOINTR:
1923 case -ERESTARTNOHAND:
1924 case -ERESTART_RESTARTBLOCK:
1925 /*
1926 * We can't just restart the syscall, since previously
1927 * submitted sqes may already be in progress. Just fail this
1928 * IO with EINTR.
1929 */
1930 ret = -EINTR;
1931 /* fall through */
1932 default:
1933 kiocb->ki_complete(kiocb, ret, 0);
1934 }
1935}
1936
Jens Axboeba816ad2019-09-28 11:36:45 -06001937static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt,
1938 bool in_async)
1939{
Jens Axboeba042912019-12-25 16:33:42 -07001940 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
1941
1942 if (req->flags & REQ_F_CUR_POS)
1943 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001944 if (in_async && ret >= 0 && kiocb->ki_complete == io_complete_rw)
Jens Axboeba816ad2019-09-28 11:36:45 -06001945 *nxt = __io_complete_rw(kiocb, ret);
1946 else
1947 io_rw_done(kiocb, ret);
1948}
1949
Jens Axboe9adbd452019-12-20 08:45:55 -07001950static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03001951 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07001952{
Jens Axboe9adbd452019-12-20 08:45:55 -07001953 struct io_ring_ctx *ctx = req->ctx;
1954 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001955 struct io_mapped_ubuf *imu;
1956 unsigned index, buf_index;
1957 size_t offset;
1958 u64 buf_addr;
1959
1960 /* attempt to use fixed buffers without having provided iovecs */
1961 if (unlikely(!ctx->user_bufs))
1962 return -EFAULT;
1963
Jens Axboe9adbd452019-12-20 08:45:55 -07001964 buf_index = (unsigned long) req->rw.kiocb.private;
Jens Axboeedafcce2019-01-09 09:16:05 -07001965 if (unlikely(buf_index >= ctx->nr_user_bufs))
1966 return -EFAULT;
1967
1968 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
1969 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07001970 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07001971
1972 /* overflow */
1973 if (buf_addr + len < buf_addr)
1974 return -EFAULT;
1975 /* not inside the mapped region */
1976 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
1977 return -EFAULT;
1978
1979 /*
1980 * May not be a start of buffer, set size appropriately
1981 * and advance us to the beginning.
1982 */
1983 offset = buf_addr - imu->ubuf;
1984 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06001985
1986 if (offset) {
1987 /*
1988 * Don't use iov_iter_advance() here, as it's really slow for
1989 * using the latter parts of a big fixed buffer - it iterates
1990 * over each segment manually. We can cheat a bit here, because
1991 * we know that:
1992 *
1993 * 1) it's a BVEC iter, we set it up
1994 * 2) all bvecs are PAGE_SIZE in size, except potentially the
1995 * first and last bvec
1996 *
1997 * So just find our index, and adjust the iterator afterwards.
1998 * If the offset is within the first bvec (or the whole first
1999 * bvec, just use iov_iter_advance(). This makes it easier
2000 * since we can just skip the first segment, which may not
2001 * be PAGE_SIZE aligned.
2002 */
2003 const struct bio_vec *bvec = imu->bvec;
2004
2005 if (offset <= bvec->bv_len) {
2006 iov_iter_advance(iter, offset);
2007 } else {
2008 unsigned long seg_skip;
2009
2010 /* skip first vec */
2011 offset -= bvec->bv_len;
2012 seg_skip = 1 + (offset >> PAGE_SHIFT);
2013
2014 iter->bvec = bvec + seg_skip;
2015 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02002016 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002017 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002018 }
2019 }
2020
Jens Axboe5e559562019-11-13 16:12:46 -07002021 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002022}
2023
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002024static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
2025 struct iovec **iovec, struct iov_iter *iter)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002026{
Jens Axboe9adbd452019-12-20 08:45:55 -07002027 void __user *buf = u64_to_user_ptr(req->rw.addr);
2028 size_t sqe_len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002029 u8 opcode;
2030
Jens Axboed625c6e2019-12-17 19:53:05 -07002031 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03002032 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07002033 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07002034 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07002035 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002036
Jens Axboe9adbd452019-12-20 08:45:55 -07002037 /* buffer index only valid with fixed read/write */
2038 if (req->rw.kiocb.private)
2039 return -EINVAL;
2040
Jens Axboe3a6820f2019-12-22 15:19:35 -07002041 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
2042 ssize_t ret;
2043 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
2044 *iovec = NULL;
2045 return ret;
2046 }
2047
Jens Axboef67676d2019-12-02 11:03:47 -07002048 if (req->io) {
2049 struct io_async_rw *iorw = &req->io->rw;
2050
2051 *iovec = iorw->iov;
2052 iov_iter_init(iter, rw, *iovec, iorw->nr_segs, iorw->size);
2053 if (iorw->iov == iorw->fast_iov)
2054 *iovec = NULL;
2055 return iorw->size;
2056 }
2057
Jens Axboe2b188cc2019-01-07 10:46:33 -07002058#ifdef CONFIG_COMPAT
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002059 if (req->ctx->compat)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002060 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
2061 iovec, iter);
2062#endif
2063
2064 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
2065}
2066
Jens Axboe32960612019-09-23 11:05:34 -06002067/*
2068 * For files that don't have ->read_iter() and ->write_iter(), handle them
2069 * by looping over ->read() or ->write() manually.
2070 */
2071static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
2072 struct iov_iter *iter)
2073{
2074 ssize_t ret = 0;
2075
2076 /*
2077 * Don't support polled IO through this interface, and we can't
2078 * support non-blocking either. For the latter, this just causes
2079 * the kiocb to be handled from an async context.
2080 */
2081 if (kiocb->ki_flags & IOCB_HIPRI)
2082 return -EOPNOTSUPP;
2083 if (kiocb->ki_flags & IOCB_NOWAIT)
2084 return -EAGAIN;
2085
2086 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002087 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06002088 ssize_t nr;
2089
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002090 if (!iov_iter_is_bvec(iter)) {
2091 iovec = iov_iter_iovec(iter);
2092 } else {
2093 /* fixed buffers import bvec */
2094 iovec.iov_base = kmap(iter->bvec->bv_page)
2095 + iter->iov_offset;
2096 iovec.iov_len = min(iter->count,
2097 iter->bvec->bv_len - iter->iov_offset);
2098 }
2099
Jens Axboe32960612019-09-23 11:05:34 -06002100 if (rw == READ) {
2101 nr = file->f_op->read(file, iovec.iov_base,
2102 iovec.iov_len, &kiocb->ki_pos);
2103 } else {
2104 nr = file->f_op->write(file, iovec.iov_base,
2105 iovec.iov_len, &kiocb->ki_pos);
2106 }
2107
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002108 if (iov_iter_is_bvec(iter))
2109 kunmap(iter->bvec->bv_page);
2110
Jens Axboe32960612019-09-23 11:05:34 -06002111 if (nr < 0) {
2112 if (!ret)
2113 ret = nr;
2114 break;
2115 }
2116 ret += nr;
2117 if (nr != iovec.iov_len)
2118 break;
2119 iov_iter_advance(iter, nr);
2120 }
2121
2122 return ret;
2123}
2124
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002125static void io_req_map_rw(struct io_kiocb *req, ssize_t io_size,
Jens Axboef67676d2019-12-02 11:03:47 -07002126 struct iovec *iovec, struct iovec *fast_iov,
2127 struct iov_iter *iter)
2128{
2129 req->io->rw.nr_segs = iter->nr_segs;
2130 req->io->rw.size = io_size;
2131 req->io->rw.iov = iovec;
2132 if (!req->io->rw.iov) {
2133 req->io->rw.iov = req->io->rw.fast_iov;
2134 memcpy(req->io->rw.iov, fast_iov,
2135 sizeof(struct iovec) * iter->nr_segs);
2136 }
2137}
2138
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002139static int io_alloc_async_ctx(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07002140{
Jens Axboed3656342019-12-18 09:50:26 -07002141 if (!io_op_defs[req->opcode].async_ctx)
2142 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002143 req->io = kmalloc(sizeof(*req->io), GFP_KERNEL);
Jens Axboe06b76d42019-12-19 14:44:26 -07002144 return req->io == NULL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002145}
2146
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002147static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size,
2148 struct iovec *iovec, struct iovec *fast_iov,
2149 struct iov_iter *iter)
2150{
Jens Axboe980ad262020-01-24 23:08:54 -07002151 if (!io_op_defs[req->opcode].async_ctx)
Jens Axboe74566df2020-01-13 19:23:24 -07002152 return 0;
Jens Axboe5d204bc2020-01-31 12:06:52 -07002153 if (!req->io) {
2154 if (io_alloc_async_ctx(req))
2155 return -ENOMEM;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002156
Jens Axboe5d204bc2020-01-31 12:06:52 -07002157 io_req_map_rw(req, io_size, iovec, fast_iov, iter);
2158 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002159 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002160}
2161
Jens Axboe3529d8c2019-12-19 18:24:38 -07002162static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2163 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002164{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002165 struct io_async_ctx *io;
2166 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002167 ssize_t ret;
2168
Jens Axboe3529d8c2019-12-19 18:24:38 -07002169 ret = io_prep_rw(req, sqe, force_nonblock);
2170 if (ret)
2171 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002172
Jens Axboe3529d8c2019-12-19 18:24:38 -07002173 if (unlikely(!(req->file->f_mode & FMODE_READ)))
2174 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002175
Jens Axboe3529d8c2019-12-19 18:24:38 -07002176 if (!req->io)
2177 return 0;
2178
2179 io = req->io;
2180 io->rw.iov = io->rw.fast_iov;
2181 req->io = NULL;
2182 ret = io_import_iovec(READ, req, &io->rw.iov, &iter);
2183 req->io = io;
2184 if (ret < 0)
2185 return ret;
2186
2187 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2188 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002189}
2190
Pavel Begunkov267bc902019-11-07 01:41:08 +03002191static int io_read(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06002192 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002193{
2194 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002195 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002196 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002197 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002198 ssize_t io_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002199
Jens Axboe3529d8c2019-12-19 18:24:38 -07002200 ret = io_import_iovec(READ, req, &iovec, &iter);
Jens Axboe06b76d42019-12-19 14:44:26 -07002201 if (ret < 0)
2202 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002203
Jens Axboefd6c2e42019-12-18 12:19:41 -07002204 /* Ensure we clear previously set non-block flag */
2205 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07002206 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002207
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002208 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002209 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06002210 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07002211 req->result = io_size;
2212
2213 /*
2214 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2215 * we know to async punt it even if it was opened O_NONBLOCK
2216 */
Jens Axboe9adbd452019-12-20 08:45:55 -07002217 if (force_nonblock && !io_file_supports_async(req->file)) {
Jens Axboef67676d2019-12-02 11:03:47 -07002218 req->flags |= REQ_F_MUST_PUNT;
2219 goto copy_iov;
2220 }
Jens Axboe9e645e112019-05-10 16:07:28 -06002221
Jens Axboe31b51512019-01-18 22:56:34 -07002222 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002223 ret = rw_verify_area(READ, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002224 if (!ret) {
2225 ssize_t ret2;
2226
Jens Axboe9adbd452019-12-20 08:45:55 -07002227 if (req->file->f_op->read_iter)
2228 ret2 = call_read_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002229 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002230 ret2 = loop_rw_iter(READ, req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002231
Jens Axboe9d93a3f2019-05-15 13:53:07 -06002232 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboef67676d2019-12-02 11:03:47 -07002233 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002234 kiocb_done(kiocb, ret2, nxt, req->in_async);
Jens Axboef67676d2019-12-02 11:03:47 -07002235 } else {
2236copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002237 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002238 inline_vecs, &iter);
2239 if (ret)
2240 goto out_free;
2241 return -EAGAIN;
2242 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002243 }
Jens Axboef67676d2019-12-02 11:03:47 -07002244out_free:
Pavel Begunkov1e950812020-02-06 19:51:16 +03002245 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002246 return ret;
2247}
2248
Jens Axboe3529d8c2019-12-19 18:24:38 -07002249static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2250 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002251{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002252 struct io_async_ctx *io;
2253 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002254 ssize_t ret;
2255
Jens Axboe3529d8c2019-12-19 18:24:38 -07002256 ret = io_prep_rw(req, sqe, force_nonblock);
2257 if (ret)
2258 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002259
Jens Axboe3529d8c2019-12-19 18:24:38 -07002260 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
2261 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002262
Jens Axboe3529d8c2019-12-19 18:24:38 -07002263 if (!req->io)
2264 return 0;
2265
2266 io = req->io;
2267 io->rw.iov = io->rw.fast_iov;
2268 req->io = NULL;
2269 ret = io_import_iovec(WRITE, req, &io->rw.iov, &iter);
2270 req->io = io;
2271 if (ret < 0)
2272 return ret;
2273
2274 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2275 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002276}
2277
Pavel Begunkov267bc902019-11-07 01:41:08 +03002278static int io_write(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06002279 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002280{
2281 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002282 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002283 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002284 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002285 ssize_t ret, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002286
Jens Axboe3529d8c2019-12-19 18:24:38 -07002287 ret = io_import_iovec(WRITE, req, &iovec, &iter);
Jens Axboe06b76d42019-12-19 14:44:26 -07002288 if (ret < 0)
2289 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002290
Jens Axboefd6c2e42019-12-18 12:19:41 -07002291 /* Ensure we clear previously set non-block flag */
2292 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07002293 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002294
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002295 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002296 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06002297 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07002298 req->result = io_size;
2299
2300 /*
2301 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2302 * we know to async punt it even if it was opened O_NONBLOCK
2303 */
2304 if (force_nonblock && !io_file_supports_async(req->file)) {
2305 req->flags |= REQ_F_MUST_PUNT;
2306 goto copy_iov;
2307 }
2308
Jens Axboe10d59342019-12-09 20:16:22 -07002309 /* file path doesn't support NOWAIT for non-direct_IO */
2310 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
2311 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07002312 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06002313
Jens Axboe31b51512019-01-18 22:56:34 -07002314 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002315 ret = rw_verify_area(WRITE, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002316 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01002317 ssize_t ret2;
2318
Jens Axboe2b188cc2019-01-07 10:46:33 -07002319 /*
2320 * Open-code file_start_write here to grab freeze protection,
2321 * which will be released by another thread in
2322 * io_complete_rw(). Fool lockdep by telling it the lock got
2323 * released so that it doesn't complain about the held lock when
2324 * we return to userspace.
2325 */
Jens Axboe491381ce2019-10-17 09:20:46 -06002326 if (req->flags & REQ_F_ISREG) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002327 __sb_start_write(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002328 SB_FREEZE_WRITE, true);
Jens Axboe9adbd452019-12-20 08:45:55 -07002329 __sb_writers_release(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002330 SB_FREEZE_WRITE);
2331 }
2332 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01002333
Jens Axboe9adbd452019-12-20 08:45:55 -07002334 if (req->file->f_op->write_iter)
2335 ret2 = call_write_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002336 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002337 ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter);
Jens Axboef67676d2019-12-02 11:03:47 -07002338 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002339 kiocb_done(kiocb, ret2, nxt, req->in_async);
Jens Axboef67676d2019-12-02 11:03:47 -07002340 } else {
2341copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002342 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002343 inline_vecs, &iter);
2344 if (ret)
2345 goto out_free;
2346 return -EAGAIN;
2347 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002348 }
Jens Axboe31b51512019-01-18 22:56:34 -07002349out_free:
Pavel Begunkov1e950812020-02-06 19:51:16 +03002350 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002351 return ret;
2352}
2353
2354/*
2355 * IORING_OP_NOP just posts a completion event, nothing else.
2356 */
Jens Axboe78e19bb2019-11-06 15:21:34 -07002357static int io_nop(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002358{
2359 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002360
Jens Axboedef596e2019-01-09 08:59:42 -07002361 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2362 return -EINVAL;
2363
Jens Axboe78e19bb2019-11-06 15:21:34 -07002364 io_cqring_add_event(req, 0);
Jens Axboee65ef562019-03-12 10:16:44 -06002365 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002366 return 0;
2367}
2368
Jens Axboe3529d8c2019-12-19 18:24:38 -07002369static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002370{
Jens Axboe6b063142019-01-10 22:13:58 -07002371 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002372
Jens Axboe09bb8392019-03-13 12:39:28 -06002373 if (!req->file)
2374 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002375
Jens Axboe6b063142019-01-10 22:13:58 -07002376 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07002377 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07002378 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002379 return -EINVAL;
2380
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002381 req->sync.flags = READ_ONCE(sqe->fsync_flags);
2382 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
2383 return -EINVAL;
2384
2385 req->sync.off = READ_ONCE(sqe->off);
2386 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002387 return 0;
2388}
2389
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002390static bool io_req_cancelled(struct io_kiocb *req)
2391{
2392 if (req->work.flags & IO_WQ_WORK_CANCEL) {
2393 req_set_fail_links(req);
2394 io_cqring_add_event(req, -ECANCELED);
2395 io_put_req(req);
2396 return true;
2397 }
2398
2399 return false;
2400}
2401
Jens Axboe78912932020-01-14 22:09:06 -07002402static void io_link_work_cb(struct io_wq_work **workptr)
2403{
2404 struct io_wq_work *work = *workptr;
2405 struct io_kiocb *link = work->data;
2406
2407 io_queue_linked_timeout(link);
2408 work->func = io_wq_submit_work;
2409}
2410
2411static void io_wq_assign_next(struct io_wq_work **workptr, struct io_kiocb *nxt)
2412{
2413 struct io_kiocb *link;
2414
2415 io_prep_async_work(nxt, &link);
2416 *workptr = &nxt->work;
2417 if (link) {
2418 nxt->work.flags |= IO_WQ_WORK_CB;
2419 nxt->work.func = io_link_work_cb;
2420 nxt->work.data = link;
2421 }
2422}
2423
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002424static void io_fsync_finish(struct io_wq_work **workptr)
2425{
2426 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2427 loff_t end = req->sync.off + req->sync.len;
2428 struct io_kiocb *nxt = NULL;
2429 int ret;
2430
2431 if (io_req_cancelled(req))
2432 return;
2433
Jens Axboe9adbd452019-12-20 08:45:55 -07002434 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002435 end > 0 ? end : LLONG_MAX,
2436 req->sync.flags & IORING_FSYNC_DATASYNC);
2437 if (ret < 0)
2438 req_set_fail_links(req);
2439 io_cqring_add_event(req, ret);
2440 io_put_req_find_next(req, &nxt);
2441 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07002442 io_wq_assign_next(workptr, nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002443}
2444
Jens Axboefc4df992019-12-10 14:38:45 -07002445static int io_fsync(struct io_kiocb *req, struct io_kiocb **nxt,
2446 bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002447{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002448 struct io_wq_work *work, *old_work;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002449
2450 /* fsync always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002451 if (force_nonblock) {
2452 io_put_req(req);
2453 req->work.func = io_fsync_finish;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002454 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002455 }
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002456
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002457 work = old_work = &req->work;
2458 io_fsync_finish(&work);
2459 if (work && work != old_work)
2460 *nxt = container_of(work, struct io_kiocb, work);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002461 return 0;
2462}
2463
Jens Axboed63d1b52019-12-10 10:38:56 -07002464static void io_fallocate_finish(struct io_wq_work **workptr)
2465{
2466 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2467 struct io_kiocb *nxt = NULL;
2468 int ret;
2469
2470 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
2471 req->sync.len);
2472 if (ret < 0)
2473 req_set_fail_links(req);
2474 io_cqring_add_event(req, ret);
2475 io_put_req_find_next(req, &nxt);
2476 if (nxt)
2477 io_wq_assign_next(workptr, nxt);
2478}
2479
2480static int io_fallocate_prep(struct io_kiocb *req,
2481 const struct io_uring_sqe *sqe)
2482{
2483 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
2484 return -EINVAL;
2485
2486 req->sync.off = READ_ONCE(sqe->off);
2487 req->sync.len = READ_ONCE(sqe->addr);
2488 req->sync.mode = READ_ONCE(sqe->len);
2489 return 0;
2490}
2491
2492static int io_fallocate(struct io_kiocb *req, struct io_kiocb **nxt,
2493 bool force_nonblock)
2494{
2495 struct io_wq_work *work, *old_work;
2496
2497 /* fallocate always requiring blocking context */
2498 if (force_nonblock) {
2499 io_put_req(req);
2500 req->work.func = io_fallocate_finish;
2501 return -EAGAIN;
2502 }
2503
2504 work = old_work = &req->work;
2505 io_fallocate_finish(&work);
2506 if (work && work != old_work)
2507 *nxt = container_of(work, struct io_kiocb, work);
2508
2509 return 0;
2510}
2511
Jens Axboe15b71ab2019-12-11 11:20:36 -07002512static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2513{
Jens Axboef8748882020-01-08 17:47:02 -07002514 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002515 int ret;
2516
2517 if (sqe->ioprio || sqe->buf_index)
2518 return -EINVAL;
2519
2520 req->open.dfd = READ_ONCE(sqe->fd);
Jens Axboec12cedf2020-01-08 17:41:21 -07002521 req->open.how.mode = READ_ONCE(sqe->len);
Jens Axboef8748882020-01-08 17:47:02 -07002522 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboec12cedf2020-01-08 17:41:21 -07002523 req->open.how.flags = READ_ONCE(sqe->open_flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002524
Jens Axboef8748882020-01-08 17:47:02 -07002525 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002526 if (IS_ERR(req->open.filename)) {
2527 ret = PTR_ERR(req->open.filename);
2528 req->open.filename = NULL;
2529 return ret;
2530 }
2531
2532 return 0;
2533}
2534
Jens Axboecebdb982020-01-08 17:59:24 -07002535static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2536{
2537 struct open_how __user *how;
2538 const char __user *fname;
2539 size_t len;
2540 int ret;
2541
2542 if (sqe->ioprio || sqe->buf_index)
2543 return -EINVAL;
2544
2545 req->open.dfd = READ_ONCE(sqe->fd);
2546 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
2547 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
2548 len = READ_ONCE(sqe->len);
2549
2550 if (len < OPEN_HOW_SIZE_VER0)
2551 return -EINVAL;
2552
2553 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
2554 len);
2555 if (ret)
2556 return ret;
2557
2558 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
2559 req->open.how.flags |= O_LARGEFILE;
2560
2561 req->open.filename = getname(fname);
2562 if (IS_ERR(req->open.filename)) {
2563 ret = PTR_ERR(req->open.filename);
2564 req->open.filename = NULL;
2565 return ret;
2566 }
2567
2568 return 0;
2569}
2570
2571static int io_openat2(struct io_kiocb *req, struct io_kiocb **nxt,
2572 bool force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07002573{
2574 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002575 struct file *file;
2576 int ret;
2577
Jens Axboef86cd202020-01-29 13:46:44 -07002578 if (force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07002579 return -EAGAIN;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002580
Jens Axboecebdb982020-01-08 17:59:24 -07002581 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002582 if (ret)
2583 goto err;
2584
Jens Axboecebdb982020-01-08 17:59:24 -07002585 ret = get_unused_fd_flags(req->open.how.flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002586 if (ret < 0)
2587 goto err;
2588
2589 file = do_filp_open(req->open.dfd, req->open.filename, &op);
2590 if (IS_ERR(file)) {
2591 put_unused_fd(ret);
2592 ret = PTR_ERR(file);
2593 } else {
2594 fsnotify_open(file);
2595 fd_install(ret, file);
2596 }
2597err:
2598 putname(req->open.filename);
2599 if (ret < 0)
2600 req_set_fail_links(req);
2601 io_cqring_add_event(req, ret);
2602 io_put_req_find_next(req, nxt);
2603 return 0;
2604}
2605
Jens Axboecebdb982020-01-08 17:59:24 -07002606static int io_openat(struct io_kiocb *req, struct io_kiocb **nxt,
2607 bool force_nonblock)
2608{
2609 req->open.how = build_open_how(req->open.how.flags, req->open.how.mode);
2610 return io_openat2(req, nxt, force_nonblock);
2611}
2612
Jens Axboe3e4827b2020-01-08 15:18:09 -07002613static int io_epoll_ctl_prep(struct io_kiocb *req,
2614 const struct io_uring_sqe *sqe)
2615{
2616#if defined(CONFIG_EPOLL)
2617 if (sqe->ioprio || sqe->buf_index)
2618 return -EINVAL;
2619
2620 req->epoll.epfd = READ_ONCE(sqe->fd);
2621 req->epoll.op = READ_ONCE(sqe->len);
2622 req->epoll.fd = READ_ONCE(sqe->off);
2623
2624 if (ep_op_has_event(req->epoll.op)) {
2625 struct epoll_event __user *ev;
2626
2627 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
2628 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
2629 return -EFAULT;
2630 }
2631
2632 return 0;
2633#else
2634 return -EOPNOTSUPP;
2635#endif
2636}
2637
2638static int io_epoll_ctl(struct io_kiocb *req, struct io_kiocb **nxt,
2639 bool force_nonblock)
2640{
2641#if defined(CONFIG_EPOLL)
2642 struct io_epoll *ie = &req->epoll;
2643 int ret;
2644
2645 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
2646 if (force_nonblock && ret == -EAGAIN)
2647 return -EAGAIN;
2648
2649 if (ret < 0)
2650 req_set_fail_links(req);
2651 io_cqring_add_event(req, ret);
2652 io_put_req_find_next(req, nxt);
2653 return 0;
2654#else
2655 return -EOPNOTSUPP;
2656#endif
2657}
2658
Jens Axboec1ca7572019-12-25 22:18:28 -07002659static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2660{
2661#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
2662 if (sqe->ioprio || sqe->buf_index || sqe->off)
2663 return -EINVAL;
2664
2665 req->madvise.addr = READ_ONCE(sqe->addr);
2666 req->madvise.len = READ_ONCE(sqe->len);
2667 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
2668 return 0;
2669#else
2670 return -EOPNOTSUPP;
2671#endif
2672}
2673
2674static int io_madvise(struct io_kiocb *req, struct io_kiocb **nxt,
2675 bool force_nonblock)
2676{
2677#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
2678 struct io_madvise *ma = &req->madvise;
2679 int ret;
2680
2681 if (force_nonblock)
2682 return -EAGAIN;
2683
2684 ret = do_madvise(ma->addr, ma->len, ma->advice);
2685 if (ret < 0)
2686 req_set_fail_links(req);
2687 io_cqring_add_event(req, ret);
2688 io_put_req_find_next(req, nxt);
2689 return 0;
2690#else
2691 return -EOPNOTSUPP;
2692#endif
2693}
2694
Jens Axboe4840e412019-12-25 22:03:45 -07002695static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2696{
2697 if (sqe->ioprio || sqe->buf_index || sqe->addr)
2698 return -EINVAL;
2699
2700 req->fadvise.offset = READ_ONCE(sqe->off);
2701 req->fadvise.len = READ_ONCE(sqe->len);
2702 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
2703 return 0;
2704}
2705
2706static int io_fadvise(struct io_kiocb *req, struct io_kiocb **nxt,
2707 bool force_nonblock)
2708{
2709 struct io_fadvise *fa = &req->fadvise;
2710 int ret;
2711
Jens Axboe3e694262020-02-01 09:22:49 -07002712 if (force_nonblock) {
2713 switch (fa->advice) {
2714 case POSIX_FADV_NORMAL:
2715 case POSIX_FADV_RANDOM:
2716 case POSIX_FADV_SEQUENTIAL:
2717 break;
2718 default:
2719 return -EAGAIN;
2720 }
2721 }
Jens Axboe4840e412019-12-25 22:03:45 -07002722
2723 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
2724 if (ret < 0)
2725 req_set_fail_links(req);
2726 io_cqring_add_event(req, ret);
2727 io_put_req_find_next(req, nxt);
2728 return 0;
2729}
2730
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002731static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2732{
Jens Axboef8748882020-01-08 17:47:02 -07002733 const char __user *fname;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002734 unsigned lookup_flags;
2735 int ret;
2736
2737 if (sqe->ioprio || sqe->buf_index)
2738 return -EINVAL;
2739
2740 req->open.dfd = READ_ONCE(sqe->fd);
2741 req->open.mask = READ_ONCE(sqe->len);
Jens Axboef8748882020-01-08 17:47:02 -07002742 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002743 req->open.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboec12cedf2020-01-08 17:41:21 -07002744 req->open.how.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002745
Jens Axboec12cedf2020-01-08 17:41:21 -07002746 if (vfs_stat_set_lookup_flags(&lookup_flags, req->open.how.flags))
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002747 return -EINVAL;
2748
Jens Axboef8748882020-01-08 17:47:02 -07002749 req->open.filename = getname_flags(fname, lookup_flags, NULL);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002750 if (IS_ERR(req->open.filename)) {
2751 ret = PTR_ERR(req->open.filename);
2752 req->open.filename = NULL;
2753 return ret;
2754 }
2755
2756 return 0;
2757}
2758
2759static int io_statx(struct io_kiocb *req, struct io_kiocb **nxt,
2760 bool force_nonblock)
2761{
2762 struct io_open *ctx = &req->open;
2763 unsigned lookup_flags;
2764 struct path path;
2765 struct kstat stat;
2766 int ret;
2767
2768 if (force_nonblock)
2769 return -EAGAIN;
2770
Jens Axboec12cedf2020-01-08 17:41:21 -07002771 if (vfs_stat_set_lookup_flags(&lookup_flags, ctx->how.flags))
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002772 return -EINVAL;
2773
2774retry:
2775 /* filename_lookup() drops it, keep a reference */
2776 ctx->filename->refcnt++;
2777
2778 ret = filename_lookup(ctx->dfd, ctx->filename, lookup_flags, &path,
2779 NULL);
2780 if (ret)
2781 goto err;
2782
Jens Axboec12cedf2020-01-08 17:41:21 -07002783 ret = vfs_getattr(&path, &stat, ctx->mask, ctx->how.flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002784 path_put(&path);
2785 if (retry_estale(ret, lookup_flags)) {
2786 lookup_flags |= LOOKUP_REVAL;
2787 goto retry;
2788 }
2789 if (!ret)
2790 ret = cp_statx(&stat, ctx->buffer);
2791err:
2792 putname(ctx->filename);
2793 if (ret < 0)
2794 req_set_fail_links(req);
2795 io_cqring_add_event(req, ret);
2796 io_put_req_find_next(req, nxt);
2797 return 0;
2798}
2799
Jens Axboeb5dba592019-12-11 14:02:38 -07002800static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2801{
2802 /*
2803 * If we queue this for async, it must not be cancellable. That would
2804 * leave the 'file' in an undeterminate state.
2805 */
2806 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
2807
2808 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
2809 sqe->rw_flags || sqe->buf_index)
2810 return -EINVAL;
2811 if (sqe->flags & IOSQE_FIXED_FILE)
2812 return -EINVAL;
2813
2814 req->close.fd = READ_ONCE(sqe->fd);
2815 if (req->file->f_op == &io_uring_fops ||
Pavel Begunkovb14cca02020-01-17 04:45:59 +03002816 req->close.fd == req->ctx->ring_fd)
Jens Axboeb5dba592019-12-11 14:02:38 -07002817 return -EBADF;
2818
2819 return 0;
2820}
2821
2822static void io_close_finish(struct io_wq_work **workptr)
2823{
2824 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2825 struct io_kiocb *nxt = NULL;
2826
2827 /* Invoked with files, we need to do the close */
2828 if (req->work.files) {
2829 int ret;
2830
2831 ret = filp_close(req->close.put_file, req->work.files);
Jens Axboe1a417f42020-01-31 17:16:48 -07002832 if (ret < 0)
Jens Axboeb5dba592019-12-11 14:02:38 -07002833 req_set_fail_links(req);
Jens Axboeb5dba592019-12-11 14:02:38 -07002834 io_cqring_add_event(req, ret);
2835 }
2836
2837 fput(req->close.put_file);
2838
Jens Axboeb5dba592019-12-11 14:02:38 -07002839 io_put_req_find_next(req, &nxt);
2840 if (nxt)
2841 io_wq_assign_next(workptr, nxt);
2842}
2843
2844static int io_close(struct io_kiocb *req, struct io_kiocb **nxt,
2845 bool force_nonblock)
2846{
2847 int ret;
2848
2849 req->close.put_file = NULL;
2850 ret = __close_fd_get_file(req->close.fd, &req->close.put_file);
2851 if (ret < 0)
2852 return ret;
2853
2854 /* if the file has a flush method, be safe and punt to async */
Jens Axboef86cd202020-01-29 13:46:44 -07002855 if (req->close.put_file->f_op->flush && !io_wq_current_is_worker())
Jens Axboeb5dba592019-12-11 14:02:38 -07002856 goto eagain;
Jens Axboeb5dba592019-12-11 14:02:38 -07002857
2858 /*
2859 * No ->flush(), safely close from here and just punt the
2860 * fput() to async context.
2861 */
2862 ret = filp_close(req->close.put_file, current->files);
2863
2864 if (ret < 0)
2865 req_set_fail_links(req);
2866 io_cqring_add_event(req, ret);
2867
2868 if (io_wq_current_is_worker()) {
2869 struct io_wq_work *old_work, *work;
2870
2871 old_work = work = &req->work;
2872 io_close_finish(&work);
2873 if (work && work != old_work)
2874 *nxt = container_of(work, struct io_kiocb, work);
2875 return 0;
2876 }
2877
2878eagain:
2879 req->work.func = io_close_finish;
Jens Axboe1a417f42020-01-31 17:16:48 -07002880 /*
2881 * Do manual async queue here to avoid grabbing files - we don't
2882 * need the files, and it'll cause io_close_finish() to close
2883 * the file again and cause a double CQE entry for this request
2884 */
2885 io_queue_async_work(req);
2886 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07002887}
2888
Jens Axboe3529d8c2019-12-19 18:24:38 -07002889static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002890{
2891 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002892
2893 if (!req->file)
2894 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002895
2896 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2897 return -EINVAL;
2898 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
2899 return -EINVAL;
2900
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002901 req->sync.off = READ_ONCE(sqe->off);
2902 req->sync.len = READ_ONCE(sqe->len);
2903 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002904 return 0;
2905}
2906
2907static void io_sync_file_range_finish(struct io_wq_work **workptr)
2908{
2909 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2910 struct io_kiocb *nxt = NULL;
2911 int ret;
2912
2913 if (io_req_cancelled(req))
2914 return;
2915
Jens Axboe9adbd452019-12-20 08:45:55 -07002916 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002917 req->sync.flags);
2918 if (ret < 0)
2919 req_set_fail_links(req);
2920 io_cqring_add_event(req, ret);
2921 io_put_req_find_next(req, &nxt);
2922 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07002923 io_wq_assign_next(workptr, nxt);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002924}
2925
Jens Axboefc4df992019-12-10 14:38:45 -07002926static int io_sync_file_range(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002927 bool force_nonblock)
2928{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002929 struct io_wq_work *work, *old_work;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002930
2931 /* sync_file_range always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002932 if (force_nonblock) {
2933 io_put_req(req);
2934 req->work.func = io_sync_file_range_finish;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002935 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002936 }
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002937
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002938 work = old_work = &req->work;
2939 io_sync_file_range_finish(&work);
2940 if (work && work != old_work)
2941 *nxt = container_of(work, struct io_kiocb, work);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002942 return 0;
2943}
2944
Jens Axboe3529d8c2019-12-19 18:24:38 -07002945static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06002946{
Jens Axboe03b12302019-12-02 18:50:25 -07002947#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07002948 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002949 struct io_async_ctx *io = req->io;
Jens Axboe03b12302019-12-02 18:50:25 -07002950
Jens Axboee47293f2019-12-20 08:58:21 -07002951 sr->msg_flags = READ_ONCE(sqe->msg_flags);
2952 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07002953 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07002954
Jens Axboefddafac2020-01-04 20:19:44 -07002955 if (!io || req->opcode == IORING_OP_SEND)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002956 return 0;
2957
Jens Axboed9688562019-12-09 19:35:20 -07002958 io->msg.iov = io->msg.fast_iov;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002959 return sendmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07002960 &io->msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002961#else
Jens Axboee47293f2019-12-20 08:58:21 -07002962 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07002963#endif
2964}
2965
Jens Axboefc4df992019-12-10 14:38:45 -07002966static int io_sendmsg(struct io_kiocb *req, struct io_kiocb **nxt,
2967 bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07002968{
2969#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07002970 struct io_async_msghdr *kmsg = NULL;
Jens Axboe03b12302019-12-02 18:50:25 -07002971 struct socket *sock;
2972 int ret;
2973
2974 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2975 return -EINVAL;
2976
2977 sock = sock_from_file(req->file, &ret);
2978 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002979 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07002980 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -07002981 unsigned flags;
2982
Jens Axboe03b12302019-12-02 18:50:25 -07002983 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07002984 kmsg = &req->io->msg;
2985 kmsg->msg.msg_name = &addr;
2986 /* if iov is set, it's allocated already */
2987 if (!kmsg->iov)
2988 kmsg->iov = kmsg->fast_iov;
2989 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07002990 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07002991 struct io_sr_msg *sr = &req->sr_msg;
2992
Jens Axboe0b416c32019-12-15 10:57:46 -07002993 kmsg = &io.msg;
2994 kmsg->msg.msg_name = &addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002995
2996 io.msg.iov = io.msg.fast_iov;
2997 ret = sendmsg_copy_msghdr(&io.msg.msg, sr->msg,
2998 sr->msg_flags, &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002999 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003000 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003001 }
3002
Jens Axboee47293f2019-12-20 08:58:21 -07003003 flags = req->sr_msg.msg_flags;
3004 if (flags & MSG_DONTWAIT)
3005 req->flags |= REQ_F_NOWAIT;
3006 else if (force_nonblock)
3007 flags |= MSG_DONTWAIT;
3008
Jens Axboe0b416c32019-12-15 10:57:46 -07003009 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Jens Axboe03b12302019-12-02 18:50:25 -07003010 if (force_nonblock && ret == -EAGAIN) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003011 if (req->io)
3012 return -EAGAIN;
Pavel Begunkov1e950812020-02-06 19:51:16 +03003013 if (io_alloc_async_ctx(req)) {
3014 if (kmsg && kmsg->iov != kmsg->fast_iov)
3015 kfree(kmsg->iov);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003016 return -ENOMEM;
Pavel Begunkov1e950812020-02-06 19:51:16 +03003017 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003018 memcpy(&req->io->msg, &io.msg, sizeof(io.msg));
Jens Axboe0b416c32019-12-15 10:57:46 -07003019 return -EAGAIN;
Jens Axboe03b12302019-12-02 18:50:25 -07003020 }
3021 if (ret == -ERESTARTSYS)
3022 ret = -EINTR;
3023 }
3024
Pavel Begunkov1e950812020-02-06 19:51:16 +03003025 if (kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07003026 kfree(kmsg->iov);
Jens Axboe03b12302019-12-02 18:50:25 -07003027 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003028 if (ret < 0)
3029 req_set_fail_links(req);
Jens Axboe03b12302019-12-02 18:50:25 -07003030 io_put_req_find_next(req, nxt);
3031 return 0;
3032#else
3033 return -EOPNOTSUPP;
3034#endif
3035}
3036
Jens Axboefddafac2020-01-04 20:19:44 -07003037static int io_send(struct io_kiocb *req, struct io_kiocb **nxt,
3038 bool force_nonblock)
3039{
3040#if defined(CONFIG_NET)
3041 struct socket *sock;
3042 int ret;
3043
3044 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3045 return -EINVAL;
3046
3047 sock = sock_from_file(req->file, &ret);
3048 if (sock) {
3049 struct io_sr_msg *sr = &req->sr_msg;
3050 struct msghdr msg;
3051 struct iovec iov;
3052 unsigned flags;
3053
3054 ret = import_single_range(WRITE, sr->buf, sr->len, &iov,
3055 &msg.msg_iter);
3056 if (ret)
3057 return ret;
3058
3059 msg.msg_name = NULL;
3060 msg.msg_control = NULL;
3061 msg.msg_controllen = 0;
3062 msg.msg_namelen = 0;
3063
3064 flags = req->sr_msg.msg_flags;
3065 if (flags & MSG_DONTWAIT)
3066 req->flags |= REQ_F_NOWAIT;
3067 else if (force_nonblock)
3068 flags |= MSG_DONTWAIT;
3069
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003070 msg.msg_flags = flags;
3071 ret = sock_sendmsg(sock, &msg);
Jens Axboefddafac2020-01-04 20:19:44 -07003072 if (force_nonblock && ret == -EAGAIN)
3073 return -EAGAIN;
3074 if (ret == -ERESTARTSYS)
3075 ret = -EINTR;
3076 }
3077
3078 io_cqring_add_event(req, ret);
3079 if (ret < 0)
3080 req_set_fail_links(req);
3081 io_put_req_find_next(req, nxt);
3082 return 0;
3083#else
3084 return -EOPNOTSUPP;
3085#endif
3086}
3087
Jens Axboe3529d8c2019-12-19 18:24:38 -07003088static int io_recvmsg_prep(struct io_kiocb *req,
3089 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07003090{
3091#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07003092 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003093 struct io_async_ctx *io = req->io;
Jens Axboe06b76d42019-12-19 14:44:26 -07003094
Jens Axboe3529d8c2019-12-19 18:24:38 -07003095 sr->msg_flags = READ_ONCE(sqe->msg_flags);
3096 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003097 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003098
Jens Axboefddafac2020-01-04 20:19:44 -07003099 if (!io || req->opcode == IORING_OP_RECV)
Jens Axboe06b76d42019-12-19 14:44:26 -07003100 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07003101
Jens Axboed9688562019-12-09 19:35:20 -07003102 io->msg.iov = io->msg.fast_iov;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003103 return recvmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07003104 &io->msg.uaddr, &io->msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07003105#else
Jens Axboee47293f2019-12-20 08:58:21 -07003106 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07003107#endif
3108}
3109
Jens Axboefc4df992019-12-10 14:38:45 -07003110static int io_recvmsg(struct io_kiocb *req, struct io_kiocb **nxt,
3111 bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07003112{
3113#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07003114 struct io_async_msghdr *kmsg = NULL;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003115 struct socket *sock;
3116 int ret;
3117
3118 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3119 return -EINVAL;
3120
3121 sock = sock_from_file(req->file, &ret);
3122 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003123 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07003124 struct sockaddr_storage addr;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003125 unsigned flags;
3126
Jens Axboe03b12302019-12-02 18:50:25 -07003127 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07003128 kmsg = &req->io->msg;
3129 kmsg->msg.msg_name = &addr;
3130 /* if iov is set, it's allocated already */
3131 if (!kmsg->iov)
3132 kmsg->iov = kmsg->fast_iov;
3133 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07003134 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003135 struct io_sr_msg *sr = &req->sr_msg;
3136
Jens Axboe0b416c32019-12-15 10:57:46 -07003137 kmsg = &io.msg;
3138 kmsg->msg.msg_name = &addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003139
3140 io.msg.iov = io.msg.fast_iov;
3141 ret = recvmsg_copy_msghdr(&io.msg.msg, sr->msg,
3142 sr->msg_flags, &io.msg.uaddr,
3143 &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07003144 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003145 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003146 }
Jens Axboe0fa03c62019-04-19 13:34:07 -06003147
Jens Axboee47293f2019-12-20 08:58:21 -07003148 flags = req->sr_msg.msg_flags;
3149 if (flags & MSG_DONTWAIT)
3150 req->flags |= REQ_F_NOWAIT;
3151 else if (force_nonblock)
3152 flags |= MSG_DONTWAIT;
3153
3154 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.msg,
3155 kmsg->uaddr, flags);
Jens Axboe03b12302019-12-02 18:50:25 -07003156 if (force_nonblock && ret == -EAGAIN) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003157 if (req->io)
3158 return -EAGAIN;
Pavel Begunkov1e950812020-02-06 19:51:16 +03003159 if (io_alloc_async_ctx(req)) {
3160 if (kmsg && kmsg->iov != kmsg->fast_iov)
3161 kfree(kmsg->iov);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003162 return -ENOMEM;
Pavel Begunkov1e950812020-02-06 19:51:16 +03003163 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003164 memcpy(&req->io->msg, &io.msg, sizeof(io.msg));
Jens Axboe0b416c32019-12-15 10:57:46 -07003165 return -EAGAIN;
Jens Axboe03b12302019-12-02 18:50:25 -07003166 }
Jens Axboe441cdbd2019-12-02 18:49:10 -07003167 if (ret == -ERESTARTSYS)
3168 ret = -EINTR;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003169 }
3170
Pavel Begunkov1e950812020-02-06 19:51:16 +03003171 if (kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07003172 kfree(kmsg->iov);
Jens Axboe78e19bb2019-11-06 15:21:34 -07003173 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003174 if (ret < 0)
3175 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003176 io_put_req_find_next(req, nxt);
Jens Axboe0fa03c62019-04-19 13:34:07 -06003177 return 0;
3178#else
3179 return -EOPNOTSUPP;
3180#endif
3181}
3182
Jens Axboefddafac2020-01-04 20:19:44 -07003183static int io_recv(struct io_kiocb *req, struct io_kiocb **nxt,
3184 bool force_nonblock)
3185{
3186#if defined(CONFIG_NET)
3187 struct socket *sock;
3188 int ret;
3189
3190 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3191 return -EINVAL;
3192
3193 sock = sock_from_file(req->file, &ret);
3194 if (sock) {
3195 struct io_sr_msg *sr = &req->sr_msg;
3196 struct msghdr msg;
3197 struct iovec iov;
3198 unsigned flags;
3199
3200 ret = import_single_range(READ, sr->buf, sr->len, &iov,
3201 &msg.msg_iter);
3202 if (ret)
3203 return ret;
3204
3205 msg.msg_name = NULL;
3206 msg.msg_control = NULL;
3207 msg.msg_controllen = 0;
3208 msg.msg_namelen = 0;
3209 msg.msg_iocb = NULL;
3210 msg.msg_flags = 0;
3211
3212 flags = req->sr_msg.msg_flags;
3213 if (flags & MSG_DONTWAIT)
3214 req->flags |= REQ_F_NOWAIT;
3215 else if (force_nonblock)
3216 flags |= MSG_DONTWAIT;
3217
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003218 ret = sock_recvmsg(sock, &msg, flags);
Jens Axboefddafac2020-01-04 20:19:44 -07003219 if (force_nonblock && ret == -EAGAIN)
3220 return -EAGAIN;
3221 if (ret == -ERESTARTSYS)
3222 ret = -EINTR;
3223 }
3224
3225 io_cqring_add_event(req, ret);
3226 if (ret < 0)
3227 req_set_fail_links(req);
3228 io_put_req_find_next(req, nxt);
3229 return 0;
3230#else
3231 return -EOPNOTSUPP;
3232#endif
3233}
3234
3235
Jens Axboe3529d8c2019-12-19 18:24:38 -07003236static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003237{
3238#if defined(CONFIG_NET)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003239 struct io_accept *accept = &req->accept;
3240
Jens Axboe17f2fe32019-10-17 14:42:58 -06003241 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3242 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05003243 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003244 return -EINVAL;
3245
Jens Axboed55e5f52019-12-11 16:12:15 -07003246 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3247 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003248 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003249 return 0;
3250#else
3251 return -EOPNOTSUPP;
3252#endif
3253}
Jens Axboe17f2fe32019-10-17 14:42:58 -06003254
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003255#if defined(CONFIG_NET)
3256static int __io_accept(struct io_kiocb *req, struct io_kiocb **nxt,
3257 bool force_nonblock)
3258{
3259 struct io_accept *accept = &req->accept;
3260 unsigned file_flags;
3261 int ret;
3262
3263 file_flags = force_nonblock ? O_NONBLOCK : 0;
3264 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
3265 accept->addr_len, accept->flags);
3266 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003267 return -EAGAIN;
Jens Axboe8e3cca12019-11-09 19:52:33 -07003268 if (ret == -ERESTARTSYS)
3269 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003270 if (ret < 0)
3271 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07003272 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003273 io_put_req_find_next(req, nxt);
Jens Axboe17f2fe32019-10-17 14:42:58 -06003274 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003275}
3276
3277static void io_accept_finish(struct io_wq_work **workptr)
3278{
3279 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3280 struct io_kiocb *nxt = NULL;
3281
3282 if (io_req_cancelled(req))
3283 return;
3284 __io_accept(req, &nxt, false);
3285 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07003286 io_wq_assign_next(workptr, nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003287}
3288#endif
3289
3290static int io_accept(struct io_kiocb *req, struct io_kiocb **nxt,
3291 bool force_nonblock)
3292{
3293#if defined(CONFIG_NET)
3294 int ret;
3295
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003296 ret = __io_accept(req, nxt, force_nonblock);
3297 if (ret == -EAGAIN && force_nonblock) {
3298 req->work.func = io_accept_finish;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003299 io_put_req(req);
3300 return -EAGAIN;
3301 }
3302 return 0;
Jens Axboe17f2fe32019-10-17 14:42:58 -06003303#else
3304 return -EOPNOTSUPP;
3305#endif
3306}
3307
Jens Axboe3529d8c2019-12-19 18:24:38 -07003308static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07003309{
3310#if defined(CONFIG_NET)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003311 struct io_connect *conn = &req->connect;
3312 struct io_async_ctx *io = req->io;
Jens Axboef499a022019-12-02 16:28:46 -07003313
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003314 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3315 return -EINVAL;
3316 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
3317 return -EINVAL;
3318
Jens Axboe3529d8c2019-12-19 18:24:38 -07003319 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3320 conn->addr_len = READ_ONCE(sqe->addr2);
3321
3322 if (!io)
3323 return 0;
3324
3325 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003326 &io->connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07003327#else
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003328 return -EOPNOTSUPP;
Jens Axboef499a022019-12-02 16:28:46 -07003329#endif
3330}
3331
Jens Axboefc4df992019-12-10 14:38:45 -07003332static int io_connect(struct io_kiocb *req, struct io_kiocb **nxt,
3333 bool force_nonblock)
Jens Axboef8e85cf2019-11-23 14:24:24 -07003334{
3335#if defined(CONFIG_NET)
Jens Axboef499a022019-12-02 16:28:46 -07003336 struct io_async_ctx __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003337 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003338 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003339
Jens Axboef499a022019-12-02 16:28:46 -07003340 if (req->io) {
3341 io = req->io;
3342 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003343 ret = move_addr_to_kernel(req->connect.addr,
3344 req->connect.addr_len,
3345 &__io.connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07003346 if (ret)
3347 goto out;
3348 io = &__io;
3349 }
3350
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003351 file_flags = force_nonblock ? O_NONBLOCK : 0;
3352
3353 ret = __sys_connect_file(req->file, &io->connect.address,
3354 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07003355 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003356 if (req->io)
3357 return -EAGAIN;
3358 if (io_alloc_async_ctx(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07003359 ret = -ENOMEM;
3360 goto out;
3361 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003362 memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect));
Jens Axboef8e85cf2019-11-23 14:24:24 -07003363 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07003364 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07003365 if (ret == -ERESTARTSYS)
3366 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07003367out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003368 if (ret < 0)
3369 req_set_fail_links(req);
Jens Axboef8e85cf2019-11-23 14:24:24 -07003370 io_cqring_add_event(req, ret);
3371 io_put_req_find_next(req, nxt);
3372 return 0;
3373#else
3374 return -EOPNOTSUPP;
3375#endif
3376}
3377
Jens Axboe221c5eb2019-01-17 09:41:58 -07003378static void io_poll_remove_one(struct io_kiocb *req)
3379{
3380 struct io_poll_iocb *poll = &req->poll;
3381
3382 spin_lock(&poll->head->lock);
3383 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07003384 if (!list_empty(&poll->wait.entry)) {
3385 list_del_init(&poll->wait.entry);
Jackie Liua197f662019-11-08 08:09:12 -07003386 io_queue_async_work(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003387 }
3388 spin_unlock(&poll->head->lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07003389 hash_del(&req->hash_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003390}
3391
3392static void io_poll_remove_all(struct io_ring_ctx *ctx)
3393{
Jens Axboe78076bb2019-12-04 19:56:40 -07003394 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003395 struct io_kiocb *req;
Jens Axboe78076bb2019-12-04 19:56:40 -07003396 int i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003397
3398 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07003399 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
3400 struct hlist_head *list;
3401
3402 list = &ctx->cancel_hash[i];
3403 hlist_for_each_entry_safe(req, tmp, list, hash_node)
3404 io_poll_remove_one(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003405 }
3406 spin_unlock_irq(&ctx->completion_lock);
3407}
3408
Jens Axboe47f46762019-11-09 17:43:02 -07003409static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
3410{
Jens Axboe78076bb2019-12-04 19:56:40 -07003411 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07003412 struct io_kiocb *req;
3413
Jens Axboe78076bb2019-12-04 19:56:40 -07003414 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
3415 hlist_for_each_entry(req, list, hash_node) {
3416 if (sqe_addr == req->user_data) {
Jens Axboeeac406c2019-11-14 12:09:58 -07003417 io_poll_remove_one(req);
3418 return 0;
3419 }
Jens Axboe47f46762019-11-09 17:43:02 -07003420 }
3421
3422 return -ENOENT;
3423}
3424
Jens Axboe3529d8c2019-12-19 18:24:38 -07003425static int io_poll_remove_prep(struct io_kiocb *req,
3426 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003427{
Jens Axboe221c5eb2019-01-17 09:41:58 -07003428 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3429 return -EINVAL;
3430 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
3431 sqe->poll_events)
3432 return -EINVAL;
3433
Jens Axboe0969e782019-12-17 18:40:57 -07003434 req->poll.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07003435 return 0;
3436}
3437
3438/*
3439 * Find a running poll command that matches one specified in sqe->addr,
3440 * and remove it if found.
3441 */
3442static int io_poll_remove(struct io_kiocb *req)
3443{
3444 struct io_ring_ctx *ctx = req->ctx;
3445 u64 addr;
3446 int ret;
3447
Jens Axboe0969e782019-12-17 18:40:57 -07003448 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003449 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07003450 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003451 spin_unlock_irq(&ctx->completion_lock);
3452
Jens Axboe78e19bb2019-11-06 15:21:34 -07003453 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003454 if (ret < 0)
3455 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06003456 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003457 return 0;
3458}
3459
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003460static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003461{
Jackie Liua197f662019-11-08 08:09:12 -07003462 struct io_ring_ctx *ctx = req->ctx;
3463
Jens Axboe8c838782019-03-12 15:48:16 -06003464 req->poll.done = true;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003465 if (error)
3466 io_cqring_fill_event(req, error);
3467 else
3468 io_cqring_fill_event(req, mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06003469 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003470}
3471
Jens Axboe561fb042019-10-24 07:25:42 -06003472static void io_poll_complete_work(struct io_wq_work **workptr)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003473{
Jens Axboe561fb042019-10-24 07:25:42 -06003474 struct io_wq_work *work = *workptr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003475 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
3476 struct io_poll_iocb *poll = &req->poll;
3477 struct poll_table_struct pt = { ._key = poll->events };
3478 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe89723d02019-11-05 15:32:58 -07003479 struct io_kiocb *nxt = NULL;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003480 __poll_t mask = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003481 int ret = 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003482
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003483 if (work->flags & IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06003484 WRITE_ONCE(poll->canceled, true);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003485 ret = -ECANCELED;
3486 } else if (READ_ONCE(poll->canceled)) {
3487 ret = -ECANCELED;
3488 }
Jens Axboe561fb042019-10-24 07:25:42 -06003489
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003490 if (ret != -ECANCELED)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003491 mask = vfs_poll(poll->file, &pt) & poll->events;
3492
3493 /*
3494 * Note that ->ki_cancel callers also delete iocb from active_reqs after
3495 * calling ->ki_cancel. We need the ctx_lock roundtrip here to
3496 * synchronize with them. In the cancellation case the list_del_init
3497 * itself is not actually needed, but harmless so we keep it in to
3498 * avoid further branches in the fast path.
3499 */
3500 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003501 if (!mask && ret != -ECANCELED) {
Jens Axboe392edb42019-12-09 17:52:20 -07003502 add_wait_queue(poll->head, &poll->wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003503 spin_unlock_irq(&ctx->completion_lock);
3504 return;
3505 }
Jens Axboe78076bb2019-12-04 19:56:40 -07003506 hash_del(&req->hash_node);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003507 io_poll_complete(req, mask, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003508 spin_unlock_irq(&ctx->completion_lock);
3509
Jens Axboe8c838782019-03-12 15:48:16 -06003510 io_cqring_ev_posted(ctx);
Jens Axboe89723d02019-11-05 15:32:58 -07003511
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003512 if (ret < 0)
3513 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003514 io_put_req_find_next(req, &nxt);
Jens Axboe89723d02019-11-05 15:32:58 -07003515 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07003516 io_wq_assign_next(workptr, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003517}
3518
Jens Axboee94f1412019-12-19 12:06:02 -07003519static void __io_poll_flush(struct io_ring_ctx *ctx, struct llist_node *nodes)
3520{
Jens Axboee94f1412019-12-19 12:06:02 -07003521 struct io_kiocb *req, *tmp;
Jens Axboe8237e042019-12-28 10:48:22 -07003522 struct req_batch rb;
Jens Axboee94f1412019-12-19 12:06:02 -07003523
Jens Axboec6ca97b302019-12-28 12:11:08 -07003524 rb.to_free = rb.need_iter = 0;
Jens Axboee94f1412019-12-19 12:06:02 -07003525 spin_lock_irq(&ctx->completion_lock);
3526 llist_for_each_entry_safe(req, tmp, nodes, llist_node) {
3527 hash_del(&req->hash_node);
3528 io_poll_complete(req, req->result, 0);
3529
Jens Axboe8237e042019-12-28 10:48:22 -07003530 if (refcount_dec_and_test(&req->refs) &&
3531 !io_req_multi_free(&rb, req)) {
3532 req->flags |= REQ_F_COMP_LOCKED;
3533 io_free_req(req);
Jens Axboee94f1412019-12-19 12:06:02 -07003534 }
3535 }
3536 spin_unlock_irq(&ctx->completion_lock);
3537
3538 io_cqring_ev_posted(ctx);
Jens Axboe8237e042019-12-28 10:48:22 -07003539 io_free_req_many(ctx, &rb);
Jens Axboee94f1412019-12-19 12:06:02 -07003540}
3541
3542static void io_poll_flush(struct io_wq_work **workptr)
3543{
3544 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3545 struct llist_node *nodes;
3546
3547 nodes = llist_del_all(&req->ctx->poll_llist);
3548 if (nodes)
3549 __io_poll_flush(req->ctx, nodes);
3550}
3551
Jens Axboef0b493e2020-02-01 21:30:11 -07003552static void io_poll_trigger_evfd(struct io_wq_work **workptr)
3553{
3554 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3555
3556 eventfd_signal(req->ctx->cq_ev_fd, 1);
3557 io_put_req(req);
3558}
3559
Jens Axboe221c5eb2019-01-17 09:41:58 -07003560static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
3561 void *key)
3562{
Jens Axboee9444752019-11-26 15:02:04 -07003563 struct io_poll_iocb *poll = wait->private;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003564 struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
3565 struct io_ring_ctx *ctx = req->ctx;
3566 __poll_t mask = key_to_poll(key);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003567
3568 /* for instances that support it check for an event match first: */
Jens Axboe8c838782019-03-12 15:48:16 -06003569 if (mask && !(mask & poll->events))
3570 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003571
Jens Axboe392edb42019-12-09 17:52:20 -07003572 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06003573
Jens Axboe7c9e7f02019-11-12 08:15:53 -07003574 /*
3575 * Run completion inline if we can. We're using trylock here because
3576 * we are violating the completion_lock -> poll wq lock ordering.
3577 * If we have a link timeout we're going to need the completion_lock
3578 * for finalizing the request, mark us as having grabbed that already.
3579 */
Jens Axboee94f1412019-12-19 12:06:02 -07003580 if (mask) {
3581 unsigned long flags;
Jens Axboe8c838782019-03-12 15:48:16 -06003582
Jens Axboee94f1412019-12-19 12:06:02 -07003583 if (llist_empty(&ctx->poll_llist) &&
3584 spin_trylock_irqsave(&ctx->completion_lock, flags)) {
Jens Axboef0b493e2020-02-01 21:30:11 -07003585 bool trigger_ev;
3586
Jens Axboee94f1412019-12-19 12:06:02 -07003587 hash_del(&req->hash_node);
3588 io_poll_complete(req, mask, 0);
Jens Axboee94f1412019-12-19 12:06:02 -07003589
Jens Axboef0b493e2020-02-01 21:30:11 -07003590 trigger_ev = io_should_trigger_evfd(ctx);
3591 if (trigger_ev && eventfd_signal_count()) {
3592 trigger_ev = false;
3593 req->work.func = io_poll_trigger_evfd;
3594 } else {
3595 req->flags |= REQ_F_COMP_LOCKED;
3596 io_put_req(req);
3597 req = NULL;
3598 }
3599 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3600 __io_cqring_ev_posted(ctx, trigger_ev);
Jens Axboee94f1412019-12-19 12:06:02 -07003601 } else {
3602 req->result = mask;
3603 req->llist_node.next = NULL;
3604 /* if the list wasn't empty, we're done */
3605 if (!llist_add(&req->llist_node, &ctx->poll_llist))
3606 req = NULL;
3607 else
3608 req->work.func = io_poll_flush;
3609 }
Jens Axboe8c838782019-03-12 15:48:16 -06003610 }
Jens Axboee94f1412019-12-19 12:06:02 -07003611 if (req)
3612 io_queue_async_work(req);
Jens Axboe8c838782019-03-12 15:48:16 -06003613
Jens Axboe221c5eb2019-01-17 09:41:58 -07003614 return 1;
3615}
3616
3617struct io_poll_table {
3618 struct poll_table_struct pt;
3619 struct io_kiocb *req;
3620 int error;
3621};
3622
3623static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
3624 struct poll_table_struct *p)
3625{
3626 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
3627
3628 if (unlikely(pt->req->poll.head)) {
3629 pt->error = -EINVAL;
3630 return;
3631 }
3632
3633 pt->error = 0;
3634 pt->req->poll.head = head;
Jens Axboe392edb42019-12-09 17:52:20 -07003635 add_wait_queue(head, &pt->req->poll.wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003636}
3637
Jens Axboeeac406c2019-11-14 12:09:58 -07003638static void io_poll_req_insert(struct io_kiocb *req)
3639{
3640 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07003641 struct hlist_head *list;
Jens Axboeeac406c2019-11-14 12:09:58 -07003642
Jens Axboe78076bb2019-12-04 19:56:40 -07003643 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
3644 hlist_add_head(&req->hash_node, list);
Jens Axboeeac406c2019-11-14 12:09:58 -07003645}
3646
Jens Axboe3529d8c2019-12-19 18:24:38 -07003647static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003648{
3649 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003650 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003651
3652 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3653 return -EINVAL;
3654 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
3655 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06003656 if (!poll->file)
3657 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003658
Jens Axboe221c5eb2019-01-17 09:41:58 -07003659 events = READ_ONCE(sqe->poll_events);
3660 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
Jens Axboe0969e782019-12-17 18:40:57 -07003661 return 0;
3662}
3663
3664static int io_poll_add(struct io_kiocb *req, struct io_kiocb **nxt)
3665{
3666 struct io_poll_iocb *poll = &req->poll;
3667 struct io_ring_ctx *ctx = req->ctx;
3668 struct io_poll_table ipt;
3669 bool cancel = false;
3670 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07003671
3672 INIT_IO_WORK(&req->work, io_poll_complete_work);
Jens Axboe78076bb2019-12-04 19:56:40 -07003673 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003674
Jens Axboe221c5eb2019-01-17 09:41:58 -07003675 poll->head = NULL;
Jens Axboe8c838782019-03-12 15:48:16 -06003676 poll->done = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003677 poll->canceled = false;
3678
3679 ipt.pt._qproc = io_poll_queue_proc;
3680 ipt.pt._key = poll->events;
3681 ipt.req = req;
3682 ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
3683
3684 /* initialized the list so that we can do list_empty checks */
Jens Axboe392edb42019-12-09 17:52:20 -07003685 INIT_LIST_HEAD(&poll->wait.entry);
3686 init_waitqueue_func_entry(&poll->wait, io_poll_wake);
3687 poll->wait.private = poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003688
Jens Axboe36703242019-07-25 10:20:18 -06003689 INIT_LIST_HEAD(&req->list);
3690
Jens Axboe221c5eb2019-01-17 09:41:58 -07003691 mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003692
3693 spin_lock_irq(&ctx->completion_lock);
Jens Axboe8c838782019-03-12 15:48:16 -06003694 if (likely(poll->head)) {
3695 spin_lock(&poll->head->lock);
Jens Axboe392edb42019-12-09 17:52:20 -07003696 if (unlikely(list_empty(&poll->wait.entry))) {
Jens Axboe8c838782019-03-12 15:48:16 -06003697 if (ipt.error)
3698 cancel = true;
3699 ipt.error = 0;
3700 mask = 0;
3701 }
3702 if (mask || ipt.error)
Jens Axboe392edb42019-12-09 17:52:20 -07003703 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06003704 else if (cancel)
3705 WRITE_ONCE(poll->canceled, true);
3706 else if (!poll->done) /* actually waiting for an event */
Jens Axboeeac406c2019-11-14 12:09:58 -07003707 io_poll_req_insert(req);
Jens Axboe8c838782019-03-12 15:48:16 -06003708 spin_unlock(&poll->head->lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003709 }
Jens Axboe8c838782019-03-12 15:48:16 -06003710 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06003711 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003712 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06003713 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07003714 spin_unlock_irq(&ctx->completion_lock);
3715
Jens Axboe8c838782019-03-12 15:48:16 -06003716 if (mask) {
3717 io_cqring_ev_posted(ctx);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003718 io_put_req_find_next(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003719 }
Jens Axboe8c838782019-03-12 15:48:16 -06003720 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003721}
3722
Jens Axboe5262f562019-09-17 12:26:57 -06003723static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
3724{
Jens Axboead8a48a2019-11-15 08:49:11 -07003725 struct io_timeout_data *data = container_of(timer,
3726 struct io_timeout_data, timer);
3727 struct io_kiocb *req = data->req;
3728 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06003729 unsigned long flags;
3730
Jens Axboe5262f562019-09-17 12:26:57 -06003731 atomic_inc(&ctx->cq_timeouts);
3732
3733 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08003734 /*
Jens Axboe11365042019-10-16 09:08:32 -06003735 * We could be racing with timeout deletion. If the list is empty,
3736 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08003737 */
Jens Axboe842f9612019-10-29 12:34:10 -06003738 if (!list_empty(&req->list)) {
Jens Axboe11365042019-10-16 09:08:32 -06003739 struct io_kiocb *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06003740
Jens Axboe11365042019-10-16 09:08:32 -06003741 /*
3742 * Adjust the reqs sequence before the current one because it
Brian Gianforcarod195a662019-12-13 03:09:50 -08003743 * will consume a slot in the cq_ring and the cq_tail
Jens Axboe11365042019-10-16 09:08:32 -06003744 * pointer will be increased, otherwise other timeout reqs may
3745 * return in advance without waiting for enough wait_nr.
3746 */
3747 prev = req;
3748 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
3749 prev->sequence++;
Jens Axboe11365042019-10-16 09:08:32 -06003750 list_del_init(&req->list);
Jens Axboe11365042019-10-16 09:08:32 -06003751 }
Jens Axboe842f9612019-10-29 12:34:10 -06003752
Jens Axboe78e19bb2019-11-06 15:21:34 -07003753 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06003754 io_commit_cqring(ctx);
3755 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3756
3757 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003758 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06003759 io_put_req(req);
3760 return HRTIMER_NORESTART;
3761}
3762
Jens Axboe47f46762019-11-09 17:43:02 -07003763static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
3764{
3765 struct io_kiocb *req;
3766 int ret = -ENOENT;
3767
3768 list_for_each_entry(req, &ctx->timeout_list, list) {
3769 if (user_data == req->user_data) {
3770 list_del_init(&req->list);
3771 ret = 0;
3772 break;
3773 }
3774 }
3775
3776 if (ret == -ENOENT)
3777 return ret;
3778
Jens Axboe2d283902019-12-04 11:08:05 -07003779 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe47f46762019-11-09 17:43:02 -07003780 if (ret == -1)
3781 return -EALREADY;
3782
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003783 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07003784 io_cqring_fill_event(req, -ECANCELED);
3785 io_put_req(req);
3786 return 0;
3787}
3788
Jens Axboe3529d8c2019-12-19 18:24:38 -07003789static int io_timeout_remove_prep(struct io_kiocb *req,
3790 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07003791{
Jens Axboeb29472e2019-12-17 18:50:29 -07003792 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3793 return -EINVAL;
3794 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
3795 return -EINVAL;
3796
3797 req->timeout.addr = READ_ONCE(sqe->addr);
3798 req->timeout.flags = READ_ONCE(sqe->timeout_flags);
3799 if (req->timeout.flags)
3800 return -EINVAL;
3801
Jens Axboeb29472e2019-12-17 18:50:29 -07003802 return 0;
3803}
3804
Jens Axboe11365042019-10-16 09:08:32 -06003805/*
3806 * Remove or update an existing timeout command
3807 */
Jens Axboefc4df992019-12-10 14:38:45 -07003808static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06003809{
3810 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07003811 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06003812
Jens Axboe11365042019-10-16 09:08:32 -06003813 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb29472e2019-12-17 18:50:29 -07003814 ret = io_timeout_cancel(ctx, req->timeout.addr);
Jens Axboe11365042019-10-16 09:08:32 -06003815
Jens Axboe47f46762019-11-09 17:43:02 -07003816 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06003817 io_commit_cqring(ctx);
3818 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06003819 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003820 if (ret < 0)
3821 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003822 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06003823 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06003824}
3825
Jens Axboe3529d8c2019-12-19 18:24:38 -07003826static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07003827 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06003828{
Jens Axboead8a48a2019-11-15 08:49:11 -07003829 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06003830 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06003831
Jens Axboead8a48a2019-11-15 08:49:11 -07003832 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06003833 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07003834 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06003835 return -EINVAL;
Jens Axboe2d283902019-12-04 11:08:05 -07003836 if (sqe->off && is_timeout_link)
3837 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06003838 flags = READ_ONCE(sqe->timeout_flags);
3839 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06003840 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06003841
Jens Axboe26a61672019-12-20 09:02:01 -07003842 req->timeout.count = READ_ONCE(sqe->off);
3843
Jens Axboe3529d8c2019-12-19 18:24:38 -07003844 if (!req->io && io_alloc_async_ctx(req))
Jens Axboe26a61672019-12-20 09:02:01 -07003845 return -ENOMEM;
3846
3847 data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07003848 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07003849 req->flags |= REQ_F_TIMEOUT;
3850
3851 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06003852 return -EFAULT;
3853
Jens Axboe11365042019-10-16 09:08:32 -06003854 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07003855 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06003856 else
Jens Axboead8a48a2019-11-15 08:49:11 -07003857 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06003858
Jens Axboead8a48a2019-11-15 08:49:11 -07003859 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
3860 return 0;
3861}
3862
Jens Axboefc4df992019-12-10 14:38:45 -07003863static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07003864{
3865 unsigned count;
3866 struct io_ring_ctx *ctx = req->ctx;
3867 struct io_timeout_data *data;
3868 struct list_head *entry;
3869 unsigned span = 0;
Jens Axboead8a48a2019-11-15 08:49:11 -07003870
Jens Axboe2d283902019-12-04 11:08:05 -07003871 data = &req->io->timeout;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003872
Jens Axboe5262f562019-09-17 12:26:57 -06003873 /*
3874 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07003875 * timeout event to be satisfied. If it isn't set, then this is
3876 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06003877 */
Jens Axboe26a61672019-12-20 09:02:01 -07003878 count = req->timeout.count;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003879 if (!count) {
3880 req->flags |= REQ_F_TIMEOUT_NOSEQ;
3881 spin_lock_irq(&ctx->completion_lock);
3882 entry = ctx->timeout_list.prev;
3883 goto add;
3884 }
Jens Axboe5262f562019-09-17 12:26:57 -06003885
3886 req->sequence = ctx->cached_sq_head + count - 1;
Jens Axboe2d283902019-12-04 11:08:05 -07003887 data->seq_offset = count;
Jens Axboe5262f562019-09-17 12:26:57 -06003888
3889 /*
3890 * Insertion sort, ensuring the first entry in the list is always
3891 * the one we need first.
3892 */
Jens Axboe5262f562019-09-17 12:26:57 -06003893 spin_lock_irq(&ctx->completion_lock);
3894 list_for_each_prev(entry, &ctx->timeout_list) {
3895 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
yangerkun5da0fb12019-10-15 21:59:29 +08003896 unsigned nxt_sq_head;
3897 long long tmp, tmp_nxt;
Jens Axboe2d283902019-12-04 11:08:05 -07003898 u32 nxt_offset = nxt->io->timeout.seq_offset;
Jens Axboe5262f562019-09-17 12:26:57 -06003899
Jens Axboe93bd25b2019-11-11 23:34:31 -07003900 if (nxt->flags & REQ_F_TIMEOUT_NOSEQ)
3901 continue;
3902
yangerkun5da0fb12019-10-15 21:59:29 +08003903 /*
3904 * Since cached_sq_head + count - 1 can overflow, use type long
3905 * long to store it.
3906 */
3907 tmp = (long long)ctx->cached_sq_head + count - 1;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +03003908 nxt_sq_head = nxt->sequence - nxt_offset + 1;
3909 tmp_nxt = (long long)nxt_sq_head + nxt_offset - 1;
yangerkun5da0fb12019-10-15 21:59:29 +08003910
3911 /*
3912 * cached_sq_head may overflow, and it will never overflow twice
3913 * once there is some timeout req still be valid.
3914 */
3915 if (ctx->cached_sq_head < nxt_sq_head)
yangerkun8b07a652019-10-17 12:12:35 +08003916 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08003917
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003918 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06003919 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003920
3921 /*
3922 * Sequence of reqs after the insert one and itself should
3923 * be adjusted because each timeout req consumes a slot.
3924 */
3925 span++;
3926 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06003927 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003928 req->sequence -= span;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003929add:
Jens Axboe5262f562019-09-17 12:26:57 -06003930 list_add(&req->list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07003931 data->timer.function = io_timeout_fn;
3932 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06003933 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06003934 return 0;
3935}
3936
Jens Axboe62755e32019-10-28 21:49:21 -06003937static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06003938{
Jens Axboe62755e32019-10-28 21:49:21 -06003939 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06003940
Jens Axboe62755e32019-10-28 21:49:21 -06003941 return req->user_data == (unsigned long) data;
3942}
3943
Jens Axboee977d6d2019-11-05 12:39:45 -07003944static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06003945{
Jens Axboe62755e32019-10-28 21:49:21 -06003946 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06003947 int ret = 0;
3948
Jens Axboe62755e32019-10-28 21:49:21 -06003949 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
3950 switch (cancel_ret) {
3951 case IO_WQ_CANCEL_OK:
3952 ret = 0;
3953 break;
3954 case IO_WQ_CANCEL_RUNNING:
3955 ret = -EALREADY;
3956 break;
3957 case IO_WQ_CANCEL_NOTFOUND:
3958 ret = -ENOENT;
3959 break;
3960 }
3961
Jens Axboee977d6d2019-11-05 12:39:45 -07003962 return ret;
3963}
3964
Jens Axboe47f46762019-11-09 17:43:02 -07003965static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
3966 struct io_kiocb *req, __u64 sqe_addr,
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003967 struct io_kiocb **nxt, int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07003968{
3969 unsigned long flags;
3970 int ret;
3971
3972 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
3973 if (ret != -ENOENT) {
3974 spin_lock_irqsave(&ctx->completion_lock, flags);
3975 goto done;
3976 }
3977
3978 spin_lock_irqsave(&ctx->completion_lock, flags);
3979 ret = io_timeout_cancel(ctx, sqe_addr);
3980 if (ret != -ENOENT)
3981 goto done;
3982 ret = io_poll_cancel(ctx, sqe_addr);
3983done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003984 if (!ret)
3985 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07003986 io_cqring_fill_event(req, ret);
3987 io_commit_cqring(ctx);
3988 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3989 io_cqring_ev_posted(ctx);
3990
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003991 if (ret < 0)
3992 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07003993 io_put_req_find_next(req, nxt);
3994}
3995
Jens Axboe3529d8c2019-12-19 18:24:38 -07003996static int io_async_cancel_prep(struct io_kiocb *req,
3997 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07003998{
Jens Axboefbf23842019-12-17 18:45:56 -07003999 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07004000 return -EINVAL;
4001 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
4002 sqe->cancel_flags)
4003 return -EINVAL;
4004
Jens Axboefbf23842019-12-17 18:45:56 -07004005 req->cancel.addr = READ_ONCE(sqe->addr);
4006 return 0;
4007}
4008
4009static int io_async_cancel(struct io_kiocb *req, struct io_kiocb **nxt)
4010{
4011 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07004012
4013 io_async_find_and_cancel(ctx, req, req->cancel.addr, nxt, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06004014 return 0;
4015}
4016
Jens Axboe05f3fb32019-12-09 11:22:50 -07004017static int io_files_update_prep(struct io_kiocb *req,
4018 const struct io_uring_sqe *sqe)
4019{
4020 if (sqe->flags || sqe->ioprio || sqe->rw_flags)
4021 return -EINVAL;
4022
4023 req->files_update.offset = READ_ONCE(sqe->off);
4024 req->files_update.nr_args = READ_ONCE(sqe->len);
4025 if (!req->files_update.nr_args)
4026 return -EINVAL;
4027 req->files_update.arg = READ_ONCE(sqe->addr);
4028 return 0;
4029}
4030
4031static int io_files_update(struct io_kiocb *req, bool force_nonblock)
4032{
4033 struct io_ring_ctx *ctx = req->ctx;
4034 struct io_uring_files_update up;
4035 int ret;
4036
Jens Axboef86cd202020-01-29 13:46:44 -07004037 if (force_nonblock)
Jens Axboe05f3fb32019-12-09 11:22:50 -07004038 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004039
4040 up.offset = req->files_update.offset;
4041 up.fds = req->files_update.arg;
4042
4043 mutex_lock(&ctx->uring_lock);
4044 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
4045 mutex_unlock(&ctx->uring_lock);
4046
4047 if (ret < 0)
4048 req_set_fail_links(req);
4049 io_cqring_add_event(req, ret);
4050 io_put_req(req);
4051 return 0;
4052}
4053
Jens Axboe3529d8c2019-12-19 18:24:38 -07004054static int io_req_defer_prep(struct io_kiocb *req,
4055 const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07004056{
Jens Axboee7815732019-12-17 19:45:06 -07004057 ssize_t ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07004058
Jens Axboef86cd202020-01-29 13:46:44 -07004059 if (io_op_defs[req->opcode].file_table) {
4060 ret = io_grab_files(req);
4061 if (unlikely(ret))
4062 return ret;
4063 }
4064
Jens Axboecccf0ee2020-01-27 16:34:48 -07004065 io_req_work_grab_env(req, &io_op_defs[req->opcode]);
4066
Jens Axboed625c6e2019-12-17 19:53:05 -07004067 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07004068 case IORING_OP_NOP:
4069 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004070 case IORING_OP_READV:
4071 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004072 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004073 ret = io_read_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07004074 break;
4075 case IORING_OP_WRITEV:
4076 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004077 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004078 ret = io_write_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07004079 break;
Jens Axboe0969e782019-12-17 18:40:57 -07004080 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004081 ret = io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07004082 break;
4083 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004084 ret = io_poll_remove_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07004085 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004086 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004087 ret = io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004088 break;
4089 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004090 ret = io_prep_sfr(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004091 break;
Jens Axboe03b12302019-12-02 18:50:25 -07004092 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004093 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004094 ret = io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07004095 break;
4096 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004097 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004098 ret = io_recvmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07004099 break;
Jens Axboef499a022019-12-02 16:28:46 -07004100 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004101 ret = io_connect_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07004102 break;
Jens Axboe2d283902019-12-04 11:08:05 -07004103 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004104 ret = io_timeout_prep(req, sqe, false);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004105 break;
Jens Axboeb29472e2019-12-17 18:50:29 -07004106 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004107 ret = io_timeout_remove_prep(req, sqe);
Jens Axboeb29472e2019-12-17 18:50:29 -07004108 break;
Jens Axboefbf23842019-12-17 18:45:56 -07004109 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004110 ret = io_async_cancel_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07004111 break;
Jens Axboe2d283902019-12-04 11:08:05 -07004112 case IORING_OP_LINK_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004113 ret = io_timeout_prep(req, sqe, true);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004114 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004115 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004116 ret = io_accept_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004117 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07004118 case IORING_OP_FALLOCATE:
4119 ret = io_fallocate_prep(req, sqe);
4120 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004121 case IORING_OP_OPENAT:
4122 ret = io_openat_prep(req, sqe);
4123 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07004124 case IORING_OP_CLOSE:
4125 ret = io_close_prep(req, sqe);
4126 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004127 case IORING_OP_FILES_UPDATE:
4128 ret = io_files_update_prep(req, sqe);
4129 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004130 case IORING_OP_STATX:
4131 ret = io_statx_prep(req, sqe);
4132 break;
Jens Axboe4840e412019-12-25 22:03:45 -07004133 case IORING_OP_FADVISE:
4134 ret = io_fadvise_prep(req, sqe);
4135 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07004136 case IORING_OP_MADVISE:
4137 ret = io_madvise_prep(req, sqe);
4138 break;
Jens Axboecebdb982020-01-08 17:59:24 -07004139 case IORING_OP_OPENAT2:
4140 ret = io_openat2_prep(req, sqe);
4141 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004142 case IORING_OP_EPOLL_CTL:
4143 ret = io_epoll_ctl_prep(req, sqe);
4144 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004145 default:
Jens Axboee7815732019-12-17 19:45:06 -07004146 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
4147 req->opcode);
4148 ret = -EINVAL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004149 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004150 }
4151
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004152 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07004153}
4154
Jens Axboe3529d8c2019-12-19 18:24:38 -07004155static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboede0617e2019-04-06 21:51:27 -06004156{
Jackie Liua197f662019-11-08 08:09:12 -07004157 struct io_ring_ctx *ctx = req->ctx;
Jens Axboef67676d2019-12-02 11:03:47 -07004158 int ret;
Jens Axboede0617e2019-04-06 21:51:27 -06004159
Bob Liu9d858b22019-11-13 18:06:25 +08004160 /* Still need defer if there is pending req in defer list. */
4161 if (!req_need_defer(req) && list_empty(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06004162 return 0;
4163
Jens Axboe3529d8c2019-12-19 18:24:38 -07004164 if (!req->io && io_alloc_async_ctx(req))
Jens Axboede0617e2019-04-06 21:51:27 -06004165 return -EAGAIN;
4166
Jens Axboe3529d8c2019-12-19 18:24:38 -07004167 ret = io_req_defer_prep(req, sqe);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004168 if (ret < 0)
Jens Axboe2d283902019-12-04 11:08:05 -07004169 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07004170
Jens Axboede0617e2019-04-06 21:51:27 -06004171 spin_lock_irq(&ctx->completion_lock);
Bob Liu9d858b22019-11-13 18:06:25 +08004172 if (!req_need_defer(req) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06004173 spin_unlock_irq(&ctx->completion_lock);
Jens Axboede0617e2019-04-06 21:51:27 -06004174 return 0;
4175 }
4176
Jens Axboe915967f2019-11-21 09:01:20 -07004177 trace_io_uring_defer(ctx, req, req->user_data);
Jens Axboede0617e2019-04-06 21:51:27 -06004178 list_add_tail(&req->list, &ctx->defer_list);
4179 spin_unlock_irq(&ctx->completion_lock);
4180 return -EIOCBQUEUED;
4181}
4182
Jens Axboe3529d8c2019-12-19 18:24:38 -07004183static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
4184 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004185{
Jackie Liua197f662019-11-08 08:09:12 -07004186 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07004187 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004188
Jens Axboed625c6e2019-12-17 19:53:05 -07004189 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07004190 case IORING_OP_NOP:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004191 ret = io_nop(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004192 break;
4193 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004194 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004195 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004196 if (sqe) {
4197 ret = io_read_prep(req, sqe, force_nonblock);
4198 if (ret < 0)
4199 break;
4200 }
Pavel Begunkov267bc902019-11-07 01:41:08 +03004201 ret = io_read(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004202 break;
4203 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07004204 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004205 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004206 if (sqe) {
4207 ret = io_write_prep(req, sqe, force_nonblock);
4208 if (ret < 0)
4209 break;
4210 }
Pavel Begunkov267bc902019-11-07 01:41:08 +03004211 ret = io_write(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004212 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004213 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004214 if (sqe) {
4215 ret = io_prep_fsync(req, sqe);
4216 if (ret < 0)
4217 break;
4218 }
Jens Axboefc4df992019-12-10 14:38:45 -07004219 ret = io_fsync(req, nxt, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004220 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004221 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004222 if (sqe) {
4223 ret = io_poll_add_prep(req, sqe);
4224 if (ret)
4225 break;
4226 }
Jens Axboefc4df992019-12-10 14:38:45 -07004227 ret = io_poll_add(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004228 break;
4229 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004230 if (sqe) {
4231 ret = io_poll_remove_prep(req, sqe);
4232 if (ret < 0)
4233 break;
4234 }
Jens Axboefc4df992019-12-10 14:38:45 -07004235 ret = io_poll_remove(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004236 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004237 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004238 if (sqe) {
4239 ret = io_prep_sfr(req, sqe);
4240 if (ret < 0)
4241 break;
4242 }
Jens Axboefc4df992019-12-10 14:38:45 -07004243 ret = io_sync_file_range(req, nxt, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004244 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004245 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004246 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004247 if (sqe) {
4248 ret = io_sendmsg_prep(req, sqe);
4249 if (ret < 0)
4250 break;
4251 }
Jens Axboefddafac2020-01-04 20:19:44 -07004252 if (req->opcode == IORING_OP_SENDMSG)
4253 ret = io_sendmsg(req, nxt, force_nonblock);
4254 else
4255 ret = io_send(req, nxt, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004256 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06004257 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004258 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004259 if (sqe) {
4260 ret = io_recvmsg_prep(req, sqe);
4261 if (ret)
4262 break;
4263 }
Jens Axboefddafac2020-01-04 20:19:44 -07004264 if (req->opcode == IORING_OP_RECVMSG)
4265 ret = io_recvmsg(req, nxt, force_nonblock);
4266 else
4267 ret = io_recv(req, nxt, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06004268 break;
Jens Axboe5262f562019-09-17 12:26:57 -06004269 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004270 if (sqe) {
4271 ret = io_timeout_prep(req, sqe, false);
4272 if (ret)
4273 break;
4274 }
Jens Axboefc4df992019-12-10 14:38:45 -07004275 ret = io_timeout(req);
Jens Axboe5262f562019-09-17 12:26:57 -06004276 break;
Jens Axboe11365042019-10-16 09:08:32 -06004277 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004278 if (sqe) {
4279 ret = io_timeout_remove_prep(req, sqe);
4280 if (ret)
4281 break;
4282 }
Jens Axboefc4df992019-12-10 14:38:45 -07004283 ret = io_timeout_remove(req);
Jens Axboe11365042019-10-16 09:08:32 -06004284 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06004285 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004286 if (sqe) {
4287 ret = io_accept_prep(req, sqe);
4288 if (ret)
4289 break;
4290 }
Jens Axboefc4df992019-12-10 14:38:45 -07004291 ret = io_accept(req, nxt, force_nonblock);
Jens Axboe17f2fe32019-10-17 14:42:58 -06004292 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004293 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004294 if (sqe) {
4295 ret = io_connect_prep(req, sqe);
4296 if (ret)
4297 break;
4298 }
Jens Axboefc4df992019-12-10 14:38:45 -07004299 ret = io_connect(req, nxt, force_nonblock);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004300 break;
Jens Axboe62755e32019-10-28 21:49:21 -06004301 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004302 if (sqe) {
4303 ret = io_async_cancel_prep(req, sqe);
4304 if (ret)
4305 break;
4306 }
Jens Axboefc4df992019-12-10 14:38:45 -07004307 ret = io_async_cancel(req, nxt);
Jens Axboe62755e32019-10-28 21:49:21 -06004308 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07004309 case IORING_OP_FALLOCATE:
4310 if (sqe) {
4311 ret = io_fallocate_prep(req, sqe);
4312 if (ret)
4313 break;
4314 }
4315 ret = io_fallocate(req, nxt, force_nonblock);
4316 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004317 case IORING_OP_OPENAT:
4318 if (sqe) {
4319 ret = io_openat_prep(req, sqe);
4320 if (ret)
4321 break;
4322 }
4323 ret = io_openat(req, nxt, force_nonblock);
4324 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07004325 case IORING_OP_CLOSE:
4326 if (sqe) {
4327 ret = io_close_prep(req, sqe);
4328 if (ret)
4329 break;
4330 }
4331 ret = io_close(req, nxt, force_nonblock);
4332 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004333 case IORING_OP_FILES_UPDATE:
4334 if (sqe) {
4335 ret = io_files_update_prep(req, sqe);
4336 if (ret)
4337 break;
4338 }
4339 ret = io_files_update(req, force_nonblock);
4340 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004341 case IORING_OP_STATX:
4342 if (sqe) {
4343 ret = io_statx_prep(req, sqe);
4344 if (ret)
4345 break;
4346 }
4347 ret = io_statx(req, nxt, force_nonblock);
4348 break;
Jens Axboe4840e412019-12-25 22:03:45 -07004349 case IORING_OP_FADVISE:
4350 if (sqe) {
4351 ret = io_fadvise_prep(req, sqe);
4352 if (ret)
4353 break;
4354 }
4355 ret = io_fadvise(req, nxt, force_nonblock);
4356 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07004357 case IORING_OP_MADVISE:
4358 if (sqe) {
4359 ret = io_madvise_prep(req, sqe);
4360 if (ret)
4361 break;
4362 }
4363 ret = io_madvise(req, nxt, force_nonblock);
4364 break;
Jens Axboecebdb982020-01-08 17:59:24 -07004365 case IORING_OP_OPENAT2:
4366 if (sqe) {
4367 ret = io_openat2_prep(req, sqe);
4368 if (ret)
4369 break;
4370 }
4371 ret = io_openat2(req, nxt, force_nonblock);
4372 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004373 case IORING_OP_EPOLL_CTL:
4374 if (sqe) {
4375 ret = io_epoll_ctl_prep(req, sqe);
4376 if (ret)
4377 break;
4378 }
4379 ret = io_epoll_ctl(req, nxt, force_nonblock);
4380 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004381 default:
4382 ret = -EINVAL;
4383 break;
4384 }
4385
Jens Axboedef596e2019-01-09 08:59:42 -07004386 if (ret)
4387 return ret;
4388
4389 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe11ba8202020-01-15 21:51:17 -07004390 const bool in_async = io_wq_current_is_worker();
4391
Jens Axboe9e645e112019-05-10 16:07:28 -06004392 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07004393 return -EAGAIN;
4394
Jens Axboe11ba8202020-01-15 21:51:17 -07004395 /* workqueue context doesn't hold uring_lock, grab it now */
4396 if (in_async)
4397 mutex_lock(&ctx->uring_lock);
4398
Jens Axboedef596e2019-01-09 08:59:42 -07004399 io_iopoll_req_issued(req);
Jens Axboe11ba8202020-01-15 21:51:17 -07004400
4401 if (in_async)
4402 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07004403 }
4404
4405 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004406}
4407
Jens Axboe561fb042019-10-24 07:25:42 -06004408static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboe31b51512019-01-18 22:56:34 -07004409{
Jens Axboe561fb042019-10-24 07:25:42 -06004410 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004411 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe561fb042019-10-24 07:25:42 -06004412 struct io_kiocb *nxt = NULL;
4413 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004414
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07004415 /* if NO_CANCEL is set, we must still run the work */
4416 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
4417 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06004418 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07004419 }
Jens Axboe31b51512019-01-18 22:56:34 -07004420
Jens Axboe561fb042019-10-24 07:25:42 -06004421 if (!ret) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004422 req->in_async = true;
Jens Axboe561fb042019-10-24 07:25:42 -06004423 do {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004424 ret = io_issue_sqe(req, NULL, &nxt, false);
Jens Axboe561fb042019-10-24 07:25:42 -06004425 /*
4426 * We can get EAGAIN for polled IO even though we're
4427 * forcing a sync submission from here, since we can't
4428 * wait for request slots on the block side.
4429 */
4430 if (ret != -EAGAIN)
4431 break;
4432 cond_resched();
4433 } while (1);
4434 }
Jens Axboe31b51512019-01-18 22:56:34 -07004435
Jens Axboe561fb042019-10-24 07:25:42 -06004436 /* drop submission reference */
Jackie Liuec9c02a2019-11-08 23:50:36 +08004437 io_put_req(req);
Jens Axboe817869d2019-04-30 14:44:05 -06004438
Jens Axboe561fb042019-10-24 07:25:42 -06004439 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004440 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07004441 io_cqring_add_event(req, ret);
Jens Axboe817869d2019-04-30 14:44:05 -06004442 io_put_req(req);
Jens Axboeedafcce2019-01-09 09:16:05 -07004443 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004444
Jens Axboe561fb042019-10-24 07:25:42 -06004445 /* if a dependent link is ready, pass it back */
Jens Axboe78912932020-01-14 22:09:06 -07004446 if (!ret && nxt)
4447 io_wq_assign_next(workptr, nxt);
Jens Axboe31b51512019-01-18 22:56:34 -07004448}
Jens Axboe2b188cc2019-01-07 10:46:33 -07004449
Jens Axboe15b71ab2019-12-11 11:20:36 -07004450static int io_req_needs_file(struct io_kiocb *req, int fd)
Jens Axboe9e3aa612019-12-11 15:55:43 -07004451{
Jens Axboed3656342019-12-18 09:50:26 -07004452 if (!io_op_defs[req->opcode].needs_file)
Jens Axboe9e3aa612019-12-11 15:55:43 -07004453 return 0;
Jens Axboed3656342019-12-18 09:50:26 -07004454 if (fd == -1 && io_op_defs[req->opcode].fd_non_neg)
4455 return 0;
4456 return 1;
Jens Axboe09bb8392019-03-13 12:39:28 -06004457}
4458
Jens Axboe65e19f52019-10-26 07:20:21 -06004459static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
4460 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06004461{
Jens Axboe65e19f52019-10-26 07:20:21 -06004462 struct fixed_file_table *table;
4463
Jens Axboe05f3fb32019-12-09 11:22:50 -07004464 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
4465 return table->files[index & IORING_FILE_TABLE_MASK];;
Jens Axboe65e19f52019-10-26 07:20:21 -06004466}
4467
Jens Axboe3529d8c2019-12-19 18:24:38 -07004468static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
4469 const struct io_uring_sqe *sqe)
Jens Axboe09bb8392019-03-13 12:39:28 -06004470{
Jackie Liua197f662019-11-08 08:09:12 -07004471 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe09bb8392019-03-13 12:39:28 -06004472 unsigned flags;
Jens Axboed3656342019-12-18 09:50:26 -07004473 int fd;
Jens Axboe09bb8392019-03-13 12:39:28 -06004474
Jens Axboe3529d8c2019-12-19 18:24:38 -07004475 flags = READ_ONCE(sqe->flags);
4476 fd = READ_ONCE(sqe->fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06004477
Jens Axboed3656342019-12-18 09:50:26 -07004478 if (!io_req_needs_file(req, fd))
4479 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06004480
4481 if (flags & IOSQE_FIXED_FILE) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07004482 if (unlikely(!ctx->file_data ||
Jens Axboe09bb8392019-03-13 12:39:28 -06004483 (unsigned) fd >= ctx->nr_user_files))
4484 return -EBADF;
Jens Axboeb7620122019-10-26 07:22:55 -06004485 fd = array_index_nospec(fd, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06004486 req->file = io_file_from_index(ctx, fd);
4487 if (!req->file)
Jens Axboe08a45172019-10-03 08:11:03 -06004488 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06004489 req->flags |= REQ_F_FIXED_FILE;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004490 percpu_ref_get(&ctx->file_data->refs);
Jens Axboe09bb8392019-03-13 12:39:28 -06004491 } else {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004492 if (req->needs_fixed_file)
Jens Axboe09bb8392019-03-13 12:39:28 -06004493 return -EBADF;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004494 trace_io_uring_file_get(ctx, fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06004495 req->file = io_file_get(state, fd);
4496 if (unlikely(!req->file))
4497 return -EBADF;
4498 }
4499
4500 return 0;
4501}
4502
Jackie Liua197f662019-11-08 08:09:12 -07004503static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004504{
Jens Axboefcb323c2019-10-24 12:39:47 -06004505 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07004506 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06004507
Jens Axboef86cd202020-01-29 13:46:44 -07004508 if (req->work.files)
4509 return 0;
Pavel Begunkovb14cca02020-01-17 04:45:59 +03004510 if (!ctx->ring_file)
Jens Axboeb5dba592019-12-11 14:02:38 -07004511 return -EBADF;
4512
Jens Axboefcb323c2019-10-24 12:39:47 -06004513 rcu_read_lock();
4514 spin_lock_irq(&ctx->inflight_lock);
4515 /*
4516 * We use the f_ops->flush() handler to ensure that we can flush
4517 * out work accessing these files if the fd is closed. Check if
4518 * the fd has changed since we started down this path, and disallow
4519 * this operation if it has.
4520 */
Pavel Begunkovb14cca02020-01-17 04:45:59 +03004521 if (fcheck(ctx->ring_fd) == ctx->ring_file) {
Jens Axboefcb323c2019-10-24 12:39:47 -06004522 list_add(&req->inflight_entry, &ctx->inflight_list);
4523 req->flags |= REQ_F_INFLIGHT;
4524 req->work.files = current->files;
4525 ret = 0;
4526 }
4527 spin_unlock_irq(&ctx->inflight_lock);
4528 rcu_read_unlock();
4529
4530 return ret;
4531}
4532
Jens Axboe2665abf2019-11-05 12:40:47 -07004533static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
4534{
Jens Axboead8a48a2019-11-15 08:49:11 -07004535 struct io_timeout_data *data = container_of(timer,
4536 struct io_timeout_data, timer);
4537 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07004538 struct io_ring_ctx *ctx = req->ctx;
4539 struct io_kiocb *prev = NULL;
4540 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07004541
4542 spin_lock_irqsave(&ctx->completion_lock, flags);
4543
4544 /*
4545 * We don't expect the list to be empty, that will only happen if we
4546 * race with the completion of the linked work.
4547 */
Pavel Begunkov44932332019-12-05 16:16:35 +03004548 if (!list_empty(&req->link_list)) {
4549 prev = list_entry(req->link_list.prev, struct io_kiocb,
4550 link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07004551 if (refcount_inc_not_zero(&prev->refs)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03004552 list_del_init(&req->link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07004553 prev->flags &= ~REQ_F_LINK_TIMEOUT;
4554 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07004555 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07004556 }
4557
4558 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4559
4560 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004561 req_set_fail_links(prev);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004562 io_async_find_and_cancel(ctx, req, prev->user_data, NULL,
4563 -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07004564 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07004565 } else {
4566 io_cqring_add_event(req, -ETIME);
4567 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07004568 }
Jens Axboe2665abf2019-11-05 12:40:47 -07004569 return HRTIMER_NORESTART;
4570}
4571
Jens Axboead8a48a2019-11-15 08:49:11 -07004572static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07004573{
Jens Axboe76a46e02019-11-10 23:34:16 -07004574 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07004575
Jens Axboe76a46e02019-11-10 23:34:16 -07004576 /*
4577 * If the list is now empty, then our linked request finished before
4578 * we got a chance to setup the timer
4579 */
4580 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov44932332019-12-05 16:16:35 +03004581 if (!list_empty(&req->link_list)) {
Jens Axboe2d283902019-12-04 11:08:05 -07004582 struct io_timeout_data *data = &req->io->timeout;
Jens Axboe94ae5e72019-11-14 19:39:52 -07004583
Jens Axboead8a48a2019-11-15 08:49:11 -07004584 data->timer.function = io_link_timeout_fn;
4585 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
4586 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07004587 }
Jens Axboe76a46e02019-11-10 23:34:16 -07004588 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07004589
Jens Axboe2665abf2019-11-05 12:40:47 -07004590 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07004591 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07004592}
4593
Jens Axboead8a48a2019-11-15 08:49:11 -07004594static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07004595{
4596 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004597
Jens Axboe2665abf2019-11-05 12:40:47 -07004598 if (!(req->flags & REQ_F_LINK))
4599 return NULL;
4600
Pavel Begunkov44932332019-12-05 16:16:35 +03004601 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
4602 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07004603 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07004604 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07004605
Jens Axboe76a46e02019-11-10 23:34:16 -07004606 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07004607 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07004608}
4609
Jens Axboe3529d8c2019-12-19 18:24:38 -07004610static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004611{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004612 struct io_kiocb *linked_timeout;
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004613 struct io_kiocb *nxt = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004614 int ret;
4615
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004616again:
4617 linked_timeout = io_prep_linked_timeout(req);
4618
Jens Axboe3529d8c2019-12-19 18:24:38 -07004619 ret = io_issue_sqe(req, sqe, &nxt, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06004620
4621 /*
4622 * We async punt it if the file wasn't marked NOWAIT, or if the file
4623 * doesn't support non-blocking read/write attempts
4624 */
4625 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
4626 (req->flags & REQ_F_MUST_PUNT))) {
Pavel Begunkov86a761f2020-01-22 23:09:36 +03004627punt:
Jens Axboef86cd202020-01-29 13:46:44 -07004628 if (io_op_defs[req->opcode].file_table) {
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03004629 ret = io_grab_files(req);
4630 if (ret)
4631 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004632 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03004633
4634 /*
4635 * Queued up for async execution, worker will release
4636 * submit reference when the iocb is actually submitted.
4637 */
4638 io_queue_async_work(req);
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004639 goto done_req;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004640 }
Jens Axboee65ef562019-03-12 10:16:44 -06004641
Jens Axboefcb323c2019-10-24 12:39:47 -06004642err:
Jens Axboee65ef562019-03-12 10:16:44 -06004643 /* drop submission reference */
4644 io_put_req(req);
4645
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004646 if (linked_timeout) {
Jens Axboe76a46e02019-11-10 23:34:16 -07004647 if (!ret)
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004648 io_queue_linked_timeout(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07004649 else
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004650 io_put_req(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07004651 }
4652
Jens Axboee65ef562019-03-12 10:16:44 -06004653 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06004654 if (ret) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07004655 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004656 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06004657 io_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06004658 }
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004659done_req:
4660 if (nxt) {
4661 req = nxt;
4662 nxt = NULL;
Pavel Begunkov86a761f2020-01-22 23:09:36 +03004663
4664 if (req->flags & REQ_F_FORCE_ASYNC)
4665 goto punt;
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004666 goto again;
4667 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004668}
4669
Jens Axboe3529d8c2019-12-19 18:24:38 -07004670static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jackie Liu4fe2c962019-09-09 20:50:40 +08004671{
4672 int ret;
4673
Jens Axboe3529d8c2019-12-19 18:24:38 -07004674 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004675 if (ret) {
4676 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03004677fail_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004678 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004679 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07004680 io_double_put_req(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004681 }
Pavel Begunkov25508782019-12-30 21:24:47 +03004682 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Pavel Begunkov11185912020-01-22 23:09:35 +03004683 ret = io_req_defer_prep(req, sqe);
4684 if (unlikely(ret < 0))
4685 goto fail_req;
Jens Axboece35a472019-12-17 08:04:44 -07004686 /*
4687 * Never try inline submit of IOSQE_ASYNC is set, go straight
4688 * to async execution.
4689 */
4690 req->work.flags |= IO_WQ_WORK_CONCURRENT;
4691 io_queue_async_work(req);
4692 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004693 __io_queue_sqe(req, sqe);
Jens Axboece35a472019-12-17 08:04:44 -07004694 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08004695}
4696
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004697static inline void io_queue_link_head(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08004698{
Jens Axboe94ae5e72019-11-14 19:39:52 -07004699 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004700 io_cqring_add_event(req, -ECANCELED);
4701 io_double_put_req(req);
4702 } else
Jens Axboe3529d8c2019-12-19 18:24:38 -07004703 io_queue_sqe(req, NULL);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004704}
4705
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004706#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
Jens Axboece35a472019-12-17 08:04:44 -07004707 IOSQE_IO_HARDLINK | IOSQE_ASYNC)
Jens Axboe9e645e112019-05-10 16:07:28 -06004708
Jens Axboe3529d8c2019-12-19 18:24:38 -07004709static bool io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
4710 struct io_submit_state *state, struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06004711{
Jens Axboe75c6a032020-01-28 10:15:23 -07004712 const struct cred *old_creds = NULL;
Jackie Liua197f662019-11-08 08:09:12 -07004713 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004714 unsigned int sqe_flags;
Jens Axboe75c6a032020-01-28 10:15:23 -07004715 int ret, id;
Jens Axboe9e645e112019-05-10 16:07:28 -06004716
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004717 sqe_flags = READ_ONCE(sqe->flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06004718
4719 /* enforce forwards compatibility on users */
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004720 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06004721 ret = -EINVAL;
Pavel Begunkov196be952019-11-07 01:41:06 +03004722 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06004723 }
4724
Jens Axboe75c6a032020-01-28 10:15:23 -07004725 id = READ_ONCE(sqe->personality);
4726 if (id) {
4727 const struct cred *personality_creds;
4728
4729 personality_creds = idr_find(&ctx->personality_idr, id);
4730 if (unlikely(!personality_creds)) {
4731 ret = -EINVAL;
4732 goto err_req;
4733 }
4734 old_creds = override_creds(personality_creds);
4735 }
4736
Pavel Begunkov6b47ee62020-01-18 20:22:41 +03004737 /* same numerical values with corresponding REQ_F_*, safe to copy */
4738 req->flags |= sqe_flags & (IOSQE_IO_DRAIN|IOSQE_IO_HARDLINK|
4739 IOSQE_ASYNC);
Jens Axboe9e645e112019-05-10 16:07:28 -06004740
Jens Axboe3529d8c2019-12-19 18:24:38 -07004741 ret = io_req_set_file(state, req, sqe);
Jens Axboe9e645e112019-05-10 16:07:28 -06004742 if (unlikely(ret)) {
4743err_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004744 io_cqring_add_event(req, ret);
4745 io_double_put_req(req);
Jens Axboe75c6a032020-01-28 10:15:23 -07004746 if (old_creds)
4747 revert_creds(old_creds);
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004748 return false;
Jens Axboe9e645e112019-05-10 16:07:28 -06004749 }
4750
Jens Axboe9e645e112019-05-10 16:07:28 -06004751 /*
4752 * If we already have a head request, queue this one for async
4753 * submittal once the head completes. If we don't have a head but
4754 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
4755 * submitted sync once the chain is complete. If none of those
4756 * conditions are true (normal request), then just queue it.
4757 */
4758 if (*link) {
Pavel Begunkov9d763772019-12-17 02:22:07 +03004759 struct io_kiocb *head = *link;
Jens Axboe9e645e112019-05-10 16:07:28 -06004760
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03004761 /*
4762 * Taking sequential execution of a link, draining both sides
4763 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
4764 * requests in the link. So, it drains the head and the
4765 * next after the link request. The last one is done via
4766 * drain_next flag to persist the effect across calls.
4767 */
Pavel Begunkov711be032020-01-17 03:57:59 +03004768 if (sqe_flags & IOSQE_IO_DRAIN) {
4769 head->flags |= REQ_F_IO_DRAIN;
4770 ctx->drain_next = 1;
4771 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004772 if (io_alloc_async_ctx(req)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06004773 ret = -EAGAIN;
4774 goto err_req;
4775 }
4776
Jens Axboe3529d8c2019-12-19 18:24:38 -07004777 ret = io_req_defer_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07004778 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004779 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03004780 head->flags |= REQ_F_FAIL_LINK;
Jens Axboef67676d2019-12-02 11:03:47 -07004781 goto err_req;
Jens Axboe2d283902019-12-04 11:08:05 -07004782 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03004783 trace_io_uring_link(ctx, req, head);
4784 list_add_tail(&req->link_list, &head->link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06004785
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004786 /* last request of a link, enqueue the link */
4787 if (!(sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK))) {
4788 io_queue_link_head(head);
4789 *link = NULL;
4790 }
Jens Axboe9e645e112019-05-10 16:07:28 -06004791 } else {
Pavel Begunkov711be032020-01-17 03:57:59 +03004792 if (unlikely(ctx->drain_next)) {
4793 req->flags |= REQ_F_IO_DRAIN;
4794 req->ctx->drain_next = 0;
4795 }
4796 if (sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK)) {
4797 req->flags |= REQ_F_LINK;
Pavel Begunkov711be032020-01-17 03:57:59 +03004798 INIT_LIST_HEAD(&req->link_list);
4799 ret = io_req_defer_prep(req, sqe);
4800 if (ret)
4801 req->flags |= REQ_F_FAIL_LINK;
4802 *link = req;
4803 } else {
4804 io_queue_sqe(req, sqe);
4805 }
Jens Axboe9e645e112019-05-10 16:07:28 -06004806 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004807
Jens Axboe75c6a032020-01-28 10:15:23 -07004808 if (old_creds)
4809 revert_creds(old_creds);
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004810 return true;
Jens Axboe9e645e112019-05-10 16:07:28 -06004811}
4812
Jens Axboe9a56a232019-01-09 09:06:50 -07004813/*
4814 * Batched submission is done, ensure local IO is flushed out.
4815 */
4816static void io_submit_state_end(struct io_submit_state *state)
4817{
4818 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06004819 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07004820 if (state->free_reqs)
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03004821 kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs);
Jens Axboe9a56a232019-01-09 09:06:50 -07004822}
4823
4824/*
4825 * Start submission side cache.
4826 */
4827static void io_submit_state_start(struct io_submit_state *state,
Jackie Liu22efde52019-12-02 17:14:52 +08004828 unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07004829{
4830 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07004831 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07004832 state->file = NULL;
4833 state->ios_left = max_ios;
4834}
4835
Jens Axboe2b188cc2019-01-07 10:46:33 -07004836static void io_commit_sqring(struct io_ring_ctx *ctx)
4837{
Hristo Venev75b28af2019-08-26 17:23:46 +00004838 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004839
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03004840 /*
4841 * Ensure any loads from the SQEs are done at this point,
4842 * since once we write the new head, the application could
4843 * write new data to them.
4844 */
4845 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004846}
4847
4848/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07004849 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07004850 * that is mapped by userspace. This means that care needs to be taken to
4851 * ensure that reads are stable, as we cannot rely on userspace always
4852 * being a good citizen. If members of the sqe are validated and then later
4853 * used, it's important that those reads are done through READ_ONCE() to
4854 * prevent a re-load down the line.
4855 */
Jens Axboe3529d8c2019-12-19 18:24:38 -07004856static bool io_get_sqring(struct io_ring_ctx *ctx, struct io_kiocb *req,
4857 const struct io_uring_sqe **sqe_ptr)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004858{
Hristo Venev75b28af2019-08-26 17:23:46 +00004859 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004860 unsigned head;
4861
4862 /*
4863 * The cached sq head (or cq tail) serves two purposes:
4864 *
4865 * 1) allows us to batch the cost of updating the user visible
4866 * head updates.
4867 * 2) allows the kernel side to track the head on its own, even
4868 * though the application is the one updating it.
4869 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03004870 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03004871 if (likely(head < ctx->sq_entries)) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004872 /*
4873 * All io need record the previous position, if LINK vs DARIN,
4874 * it can be used to mark the position of the first IO in the
4875 * link list.
4876 */
4877 req->sequence = ctx->cached_sq_head;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004878 *sqe_ptr = &ctx->sq_sqes[head];
4879 req->opcode = READ_ONCE((*sqe_ptr)->opcode);
4880 req->user_data = READ_ONCE((*sqe_ptr)->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004881 ctx->cached_sq_head++;
4882 return true;
4883 }
4884
4885 /* drop invalid entries */
4886 ctx->cached_sq_head++;
Jens Axboe498ccd92019-10-25 10:04:25 -06004887 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03004888 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004889 return false;
4890}
4891
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004892static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004893 struct file *ring_file, int ring_fd,
4894 struct mm_struct **mm, bool async)
Jens Axboe6c271ce2019-01-10 11:22:30 -07004895{
4896 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06004897 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06004898 int i, submitted = 0;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03004899 bool mm_fault = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004900
Jens Axboec4a2ed72019-11-21 21:01:26 -07004901 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07004902 if (test_bit(0, &ctx->sq_check_overflow)) {
4903 if (!list_empty(&ctx->cq_overflow_list) &&
4904 !io_cqring_overflow_flush(ctx, false))
4905 return -EBUSY;
4906 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07004907
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03004908 /* make sure SQ entry isn't read before tail */
4909 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03004910
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03004911 if (!percpu_ref_tryget_many(&ctx->refs, nr))
4912 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004913
4914 if (nr > IO_PLUG_THRESHOLD) {
Jackie Liu22efde52019-12-02 17:14:52 +08004915 io_submit_state_start(&state, nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004916 statep = &state;
4917 }
4918
Pavel Begunkovb14cca02020-01-17 04:45:59 +03004919 ctx->ring_fd = ring_fd;
4920 ctx->ring_file = ring_file;
4921
Jens Axboe6c271ce2019-01-10 11:22:30 -07004922 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004923 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03004924 struct io_kiocb *req;
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03004925 int err;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004926
Pavel Begunkov196be952019-11-07 01:41:06 +03004927 req = io_get_req(ctx, statep);
4928 if (unlikely(!req)) {
4929 if (!submitted)
4930 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004931 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06004932 }
Jens Axboe3529d8c2019-12-19 18:24:38 -07004933 if (!io_get_sqring(ctx, req, &sqe)) {
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03004934 __io_req_do_free(req);
Pavel Begunkov196be952019-11-07 01:41:06 +03004935 break;
4936 }
Jens Axboe9e645e112019-05-10 16:07:28 -06004937
Jens Axboed3656342019-12-18 09:50:26 -07004938 /* will complete beyond this point, count as submitted */
4939 submitted++;
4940
4941 if (unlikely(req->opcode >= IORING_OP_LAST)) {
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03004942 err = -EINVAL;
4943fail_req:
4944 io_cqring_add_event(req, err);
Jens Axboed3656342019-12-18 09:50:26 -07004945 io_double_put_req(req);
4946 break;
4947 }
4948
4949 if (io_op_defs[req->opcode].needs_mm && !*mm) {
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03004950 mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm);
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03004951 if (unlikely(mm_fault)) {
4952 err = -EFAULT;
4953 goto fail_req;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03004954 }
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03004955 use_mm(ctx->sqo_mm);
4956 *mm = ctx->sqo_mm;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03004957 }
4958
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004959 req->in_async = async;
4960 req->needs_fixed_file = async;
Jens Axboe354420f2020-01-08 18:55:15 -07004961 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
4962 true, async);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004963 if (!io_submit_sqe(req, sqe, statep, &link))
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004964 break;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004965 }
4966
Pavel Begunkov9466f432020-01-25 22:34:01 +03004967 if (unlikely(submitted != nr)) {
4968 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
4969
4970 percpu_ref_put_many(&ctx->refs, nr - ref_used);
4971 }
Jens Axboe9e645e112019-05-10 16:07:28 -06004972 if (link)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004973 io_queue_link_head(link);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004974 if (statep)
4975 io_submit_state_end(&state);
4976
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004977 /* Commit SQ ring head once we've consumed and submitted all SQEs */
4978 io_commit_sqring(ctx);
4979
Jens Axboe6c271ce2019-01-10 11:22:30 -07004980 return submitted;
4981}
4982
4983static int io_sq_thread(void *data)
4984{
Jens Axboe6c271ce2019-01-10 11:22:30 -07004985 struct io_ring_ctx *ctx = data;
4986 struct mm_struct *cur_mm = NULL;
Jens Axboe181e4482019-11-25 08:52:30 -07004987 const struct cred *old_cred;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004988 mm_segment_t old_fs;
4989 DEFINE_WAIT(wait);
4990 unsigned inflight;
4991 unsigned long timeout;
Jens Axboec1edbf52019-11-10 16:56:04 -07004992 int ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004993
Jens Axboe206aefd2019-11-07 18:27:42 -07004994 complete(&ctx->completions[1]);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08004995
Jens Axboe6c271ce2019-01-10 11:22:30 -07004996 old_fs = get_fs();
4997 set_fs(USER_DS);
Jens Axboe181e4482019-11-25 08:52:30 -07004998 old_cred = override_creds(ctx->creds);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004999
Jens Axboec1edbf52019-11-10 16:56:04 -07005000 ret = timeout = inflight = 0;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005001 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005002 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005003
5004 if (inflight) {
5005 unsigned nr_events = 0;
5006
5007 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe2b2ed972019-10-25 10:06:15 -06005008 /*
5009 * inflight is the count of the maximum possible
5010 * entries we submitted, but it can be smaller
5011 * if we dropped some of them. If we don't have
5012 * poll entries available, then we know that we
5013 * have nothing left to poll for. Reset the
5014 * inflight count to zero in that case.
5015 */
5016 mutex_lock(&ctx->uring_lock);
5017 if (!list_empty(&ctx->poll_list))
5018 __io_iopoll_check(ctx, &nr_events, 0);
5019 else
5020 inflight = 0;
5021 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005022 } else {
5023 /*
5024 * Normal IO, just pretend everything completed.
5025 * We don't have to poll completions for that.
5026 */
5027 nr_events = inflight;
5028 }
5029
5030 inflight -= nr_events;
5031 if (!inflight)
5032 timeout = jiffies + ctx->sq_thread_idle;
5033 }
5034
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005035 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07005036
5037 /*
5038 * If submit got -EBUSY, flag us as needing the application
5039 * to enter the kernel to reap and flush events.
5040 */
5041 if (!to_submit || ret == -EBUSY) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07005042 /*
5043 * We're polling. If we're within the defined idle
5044 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07005045 * to sleep. The exception is if we got EBUSY doing
5046 * more IO, we should wait for the application to
5047 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07005048 */
Jens Axboec1edbf52019-11-10 16:56:04 -07005049 if (inflight ||
Jens Axboedf069d82020-02-04 16:48:34 -07005050 (!time_after(jiffies, timeout) && ret != -EBUSY &&
5051 !percpu_ref_is_dying(&ctx->refs))) {
Jens Axboe9831a902019-09-19 09:48:55 -06005052 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07005053 continue;
5054 }
5055
5056 /*
5057 * Drop cur_mm before scheduling, we can't hold it for
5058 * long periods (or over schedule()). Do this before
5059 * adding ourselves to the waitqueue, as the unuse/drop
5060 * may sleep.
5061 */
5062 if (cur_mm) {
5063 unuse_mm(cur_mm);
5064 mmput(cur_mm);
5065 cur_mm = NULL;
5066 }
5067
5068 prepare_to_wait(&ctx->sqo_wait, &wait,
5069 TASK_INTERRUPTIBLE);
5070
5071 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00005072 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02005073 /* make sure to read SQ tail after writing flags */
5074 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07005075
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005076 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07005077 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005078 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07005079 finish_wait(&ctx->sqo_wait, &wait);
5080 break;
5081 }
5082 if (signal_pending(current))
5083 flush_signals(current);
5084 schedule();
5085 finish_wait(&ctx->sqo_wait, &wait);
5086
Hristo Venev75b28af2019-08-26 17:23:46 +00005087 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005088 continue;
5089 }
5090 finish_wait(&ctx->sqo_wait, &wait);
5091
Hristo Venev75b28af2019-08-26 17:23:46 +00005092 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005093 }
5094
Jens Axboe8a4955f2019-12-09 14:52:35 -07005095 mutex_lock(&ctx->uring_lock);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005096 ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true);
Jens Axboe8a4955f2019-12-09 14:52:35 -07005097 mutex_unlock(&ctx->uring_lock);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005098 if (ret > 0)
5099 inflight += ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005100 }
5101
5102 set_fs(old_fs);
5103 if (cur_mm) {
5104 unuse_mm(cur_mm);
5105 mmput(cur_mm);
5106 }
Jens Axboe181e4482019-11-25 08:52:30 -07005107 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06005108
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005109 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06005110
Jens Axboe6c271ce2019-01-10 11:22:30 -07005111 return 0;
5112}
5113
Jens Axboebda52162019-09-24 13:47:15 -06005114struct io_wait_queue {
5115 struct wait_queue_entry wq;
5116 struct io_ring_ctx *ctx;
5117 unsigned to_wait;
5118 unsigned nr_timeouts;
5119};
5120
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005121static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06005122{
5123 struct io_ring_ctx *ctx = iowq->ctx;
5124
5125 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08005126 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06005127 * started waiting. For timeouts, we always want to return to userspace,
5128 * regardless of event count.
5129 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005130 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06005131 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
5132}
5133
5134static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
5135 int wake_flags, void *key)
5136{
5137 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
5138 wq);
5139
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005140 /* use noflush == true, as we can't safely rely on locking context */
5141 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06005142 return -1;
5143
5144 return autoremove_wake_function(curr, mode, wake_flags, key);
5145}
5146
Jens Axboe2b188cc2019-01-07 10:46:33 -07005147/*
5148 * Wait until events become available, if we don't already have some. The
5149 * application must reap them itself, as they reside on the shared cq ring.
5150 */
5151static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
5152 const sigset_t __user *sig, size_t sigsz)
5153{
Jens Axboebda52162019-09-24 13:47:15 -06005154 struct io_wait_queue iowq = {
5155 .wq = {
5156 .private = current,
5157 .func = io_wake_function,
5158 .entry = LIST_HEAD_INIT(iowq.wq.entry),
5159 },
5160 .ctx = ctx,
5161 .to_wait = min_events,
5162 };
Hristo Venev75b28af2019-08-26 17:23:46 +00005163 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005164 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005165
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005166 if (io_cqring_events(ctx, false) >= min_events)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005167 return 0;
5168
5169 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005170#ifdef CONFIG_COMPAT
5171 if (in_compat_syscall())
5172 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07005173 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005174 else
5175#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07005176 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005177
Jens Axboe2b188cc2019-01-07 10:46:33 -07005178 if (ret)
5179 return ret;
5180 }
5181
Jens Axboebda52162019-09-24 13:47:15 -06005182 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02005183 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06005184 do {
5185 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
5186 TASK_INTERRUPTIBLE);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005187 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06005188 break;
5189 schedule();
5190 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005191 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06005192 break;
5193 }
5194 } while (1);
5195 finish_wait(&ctx->wait, &iowq.wq);
5196
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005197 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005198
Hristo Venev75b28af2019-08-26 17:23:46 +00005199 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005200}
5201
Jens Axboe6b063142019-01-10 22:13:58 -07005202static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
5203{
5204#if defined(CONFIG_UNIX)
5205 if (ctx->ring_sock) {
5206 struct sock *sock = ctx->ring_sock->sk;
5207 struct sk_buff *skb;
5208
5209 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
5210 kfree_skb(skb);
5211 }
5212#else
5213 int i;
5214
Jens Axboe65e19f52019-10-26 07:20:21 -06005215 for (i = 0; i < ctx->nr_user_files; i++) {
5216 struct file *file;
5217
5218 file = io_file_from_index(ctx, i);
5219 if (file)
5220 fput(file);
5221 }
Jens Axboe6b063142019-01-10 22:13:58 -07005222#endif
5223}
5224
Jens Axboe05f3fb32019-12-09 11:22:50 -07005225static void io_file_ref_kill(struct percpu_ref *ref)
5226{
5227 struct fixed_file_data *data;
5228
5229 data = container_of(ref, struct fixed_file_data, refs);
5230 complete(&data->done);
5231}
5232
Jens Axboe6b063142019-01-10 22:13:58 -07005233static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
5234{
Jens Axboe05f3fb32019-12-09 11:22:50 -07005235 struct fixed_file_data *data = ctx->file_data;
Jens Axboe65e19f52019-10-26 07:20:21 -06005236 unsigned nr_tables, i;
5237
Jens Axboe05f3fb32019-12-09 11:22:50 -07005238 if (!data)
Jens Axboe6b063142019-01-10 22:13:58 -07005239 return -ENXIO;
5240
Jens Axboe05f3fb32019-12-09 11:22:50 -07005241 percpu_ref_kill_and_confirm(&data->refs, io_file_ref_kill);
Jens Axboee46a7952020-01-17 11:15:34 -07005242 flush_work(&data->ref_work);
Jens Axboe2faf8522020-02-04 19:54:55 -07005243 wait_for_completion(&data->done);
5244 io_ring_file_ref_flush(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005245 percpu_ref_exit(&data->refs);
5246
Jens Axboe6b063142019-01-10 22:13:58 -07005247 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06005248 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
5249 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005250 kfree(data->table[i].files);
5251 kfree(data->table);
5252 kfree(data);
5253 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07005254 ctx->nr_user_files = 0;
5255 return 0;
5256}
5257
Jens Axboe6c271ce2019-01-10 11:22:30 -07005258static void io_sq_thread_stop(struct io_ring_ctx *ctx)
5259{
5260 if (ctx->sqo_thread) {
Jens Axboe206aefd2019-11-07 18:27:42 -07005261 wait_for_completion(&ctx->completions[1]);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005262 /*
5263 * The park is a bit of a work-around, without it we get
5264 * warning spews on shutdown with SQPOLL set and affinity
5265 * set to a single CPU.
5266 */
Jens Axboe06058632019-04-13 09:26:03 -06005267 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005268 kthread_stop(ctx->sqo_thread);
5269 ctx->sqo_thread = NULL;
5270 }
5271}
5272
Jens Axboe6b063142019-01-10 22:13:58 -07005273static void io_finish_async(struct io_ring_ctx *ctx)
5274{
Jens Axboe6c271ce2019-01-10 11:22:30 -07005275 io_sq_thread_stop(ctx);
5276
Jens Axboe561fb042019-10-24 07:25:42 -06005277 if (ctx->io_wq) {
5278 io_wq_destroy(ctx->io_wq);
5279 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07005280 }
5281}
5282
5283#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07005284/*
5285 * Ensure the UNIX gc is aware of our file set, so we are certain that
5286 * the io_uring can be safely unregistered on process exit, even if we have
5287 * loops in the file referencing.
5288 */
5289static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
5290{
5291 struct sock *sk = ctx->ring_sock->sk;
5292 struct scm_fp_list *fpl;
5293 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06005294 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07005295
5296 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
5297 unsigned long inflight = ctx->user->unix_inflight + nr;
5298
5299 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
5300 return -EMFILE;
5301 }
5302
5303 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
5304 if (!fpl)
5305 return -ENOMEM;
5306
5307 skb = alloc_skb(0, GFP_KERNEL);
5308 if (!skb) {
5309 kfree(fpl);
5310 return -ENOMEM;
5311 }
5312
5313 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07005314
Jens Axboe08a45172019-10-03 08:11:03 -06005315 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07005316 fpl->user = get_uid(ctx->user);
5317 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005318 struct file *file = io_file_from_index(ctx, i + offset);
5319
5320 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06005321 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06005322 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06005323 unix_inflight(fpl->user, fpl->fp[nr_files]);
5324 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07005325 }
5326
Jens Axboe08a45172019-10-03 08:11:03 -06005327 if (nr_files) {
5328 fpl->max = SCM_MAX_FD;
5329 fpl->count = nr_files;
5330 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005331 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06005332 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
5333 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07005334
Jens Axboe08a45172019-10-03 08:11:03 -06005335 for (i = 0; i < nr_files; i++)
5336 fput(fpl->fp[i]);
5337 } else {
5338 kfree_skb(skb);
5339 kfree(fpl);
5340 }
Jens Axboe6b063142019-01-10 22:13:58 -07005341
5342 return 0;
5343}
5344
5345/*
5346 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
5347 * causes regular reference counting to break down. We rely on the UNIX
5348 * garbage collection to take care of this problem for us.
5349 */
5350static int io_sqe_files_scm(struct io_ring_ctx *ctx)
5351{
5352 unsigned left, total;
5353 int ret = 0;
5354
5355 total = 0;
5356 left = ctx->nr_user_files;
5357 while (left) {
5358 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07005359
5360 ret = __io_sqe_files_scm(ctx, this_files, total);
5361 if (ret)
5362 break;
5363 left -= this_files;
5364 total += this_files;
5365 }
5366
5367 if (!ret)
5368 return 0;
5369
5370 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005371 struct file *file = io_file_from_index(ctx, total);
5372
5373 if (file)
5374 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07005375 total++;
5376 }
5377
5378 return ret;
5379}
5380#else
5381static int io_sqe_files_scm(struct io_ring_ctx *ctx)
5382{
5383 return 0;
5384}
5385#endif
5386
Jens Axboe65e19f52019-10-26 07:20:21 -06005387static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
5388 unsigned nr_files)
5389{
5390 int i;
5391
5392 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005393 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06005394 unsigned this_files;
5395
5396 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
5397 table->files = kcalloc(this_files, sizeof(struct file *),
5398 GFP_KERNEL);
5399 if (!table->files)
5400 break;
5401 nr_files -= this_files;
5402 }
5403
5404 if (i == nr_tables)
5405 return 0;
5406
5407 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005408 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06005409 kfree(table->files);
5410 }
5411 return 1;
5412}
5413
Jens Axboe05f3fb32019-12-09 11:22:50 -07005414static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
Jens Axboec3a31e62019-10-03 13:59:56 -06005415{
5416#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06005417 struct sock *sock = ctx->ring_sock->sk;
5418 struct sk_buff_head list, *head = &sock->sk_receive_queue;
5419 struct sk_buff *skb;
5420 int i;
5421
5422 __skb_queue_head_init(&list);
5423
5424 /*
5425 * Find the skb that holds this file in its SCM_RIGHTS. When found,
5426 * remove this entry and rearrange the file array.
5427 */
5428 skb = skb_dequeue(head);
5429 while (skb) {
5430 struct scm_fp_list *fp;
5431
5432 fp = UNIXCB(skb).fp;
5433 for (i = 0; i < fp->count; i++) {
5434 int left;
5435
5436 if (fp->fp[i] != file)
5437 continue;
5438
5439 unix_notinflight(fp->user, fp->fp[i]);
5440 left = fp->count - 1 - i;
5441 if (left) {
5442 memmove(&fp->fp[i], &fp->fp[i + 1],
5443 left * sizeof(struct file *));
5444 }
5445 fp->count--;
5446 if (!fp->count) {
5447 kfree_skb(skb);
5448 skb = NULL;
5449 } else {
5450 __skb_queue_tail(&list, skb);
5451 }
5452 fput(file);
5453 file = NULL;
5454 break;
5455 }
5456
5457 if (!file)
5458 break;
5459
5460 __skb_queue_tail(&list, skb);
5461
5462 skb = skb_dequeue(head);
5463 }
5464
5465 if (skb_peek(&list)) {
5466 spin_lock_irq(&head->lock);
5467 while ((skb = __skb_dequeue(&list)) != NULL)
5468 __skb_queue_tail(head, skb);
5469 spin_unlock_irq(&head->lock);
5470 }
5471#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07005472 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06005473#endif
5474}
5475
Jens Axboe05f3fb32019-12-09 11:22:50 -07005476struct io_file_put {
5477 struct llist_node llist;
5478 struct file *file;
5479 struct completion *done;
5480};
5481
Jens Axboe2faf8522020-02-04 19:54:55 -07005482static void io_ring_file_ref_flush(struct fixed_file_data *data)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005483{
5484 struct io_file_put *pfile, *tmp;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005485 struct llist_node *node;
5486
Jens Axboe05f3fb32019-12-09 11:22:50 -07005487 while ((node = llist_del_all(&data->put_llist)) != NULL) {
5488 llist_for_each_entry_safe(pfile, tmp, node, llist) {
5489 io_ring_file_put(data->ctx, pfile->file);
5490 if (pfile->done)
5491 complete(pfile->done);
5492 else
5493 kfree(pfile);
5494 }
5495 }
Jens Axboe2faf8522020-02-04 19:54:55 -07005496}
Jens Axboe05f3fb32019-12-09 11:22:50 -07005497
Jens Axboe2faf8522020-02-04 19:54:55 -07005498static void io_ring_file_ref_switch(struct work_struct *work)
5499{
5500 struct fixed_file_data *data;
5501
5502 data = container_of(work, struct fixed_file_data, ref_work);
5503 io_ring_file_ref_flush(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005504 percpu_ref_get(&data->refs);
5505 percpu_ref_switch_to_percpu(&data->refs);
5506}
5507
5508static void io_file_data_ref_zero(struct percpu_ref *ref)
5509{
5510 struct fixed_file_data *data;
5511
5512 data = container_of(ref, struct fixed_file_data, refs);
5513
Jens Axboe2faf8522020-02-04 19:54:55 -07005514 /*
5515 * We can't safely switch from inside this context, punt to wq. If
5516 * the table ref is going away, the table is being unregistered.
5517 * Don't queue up the async work for that case, the caller will
5518 * handle it.
5519 */
5520 if (!percpu_ref_is_dying(&data->refs))
5521 queue_work(system_wq, &data->ref_work);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005522}
5523
5524static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
5525 unsigned nr_args)
5526{
5527 __s32 __user *fds = (__s32 __user *) arg;
5528 unsigned nr_tables;
5529 struct file *file;
5530 int fd, ret = 0;
5531 unsigned i;
5532
5533 if (ctx->file_data)
5534 return -EBUSY;
5535 if (!nr_args)
5536 return -EINVAL;
5537 if (nr_args > IORING_MAX_FIXED_FILES)
5538 return -EMFILE;
5539
5540 ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
5541 if (!ctx->file_data)
5542 return -ENOMEM;
5543 ctx->file_data->ctx = ctx;
5544 init_completion(&ctx->file_data->done);
5545
5546 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
5547 ctx->file_data->table = kcalloc(nr_tables,
5548 sizeof(struct fixed_file_table),
5549 GFP_KERNEL);
5550 if (!ctx->file_data->table) {
5551 kfree(ctx->file_data);
5552 ctx->file_data = NULL;
5553 return -ENOMEM;
5554 }
5555
5556 if (percpu_ref_init(&ctx->file_data->refs, io_file_data_ref_zero,
5557 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
5558 kfree(ctx->file_data->table);
5559 kfree(ctx->file_data);
5560 ctx->file_data = NULL;
5561 return -ENOMEM;
5562 }
5563 ctx->file_data->put_llist.first = NULL;
5564 INIT_WORK(&ctx->file_data->ref_work, io_ring_file_ref_switch);
5565
5566 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
5567 percpu_ref_exit(&ctx->file_data->refs);
5568 kfree(ctx->file_data->table);
5569 kfree(ctx->file_data);
5570 ctx->file_data = NULL;
5571 return -ENOMEM;
5572 }
5573
5574 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
5575 struct fixed_file_table *table;
5576 unsigned index;
5577
5578 ret = -EFAULT;
5579 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
5580 break;
5581 /* allow sparse sets */
5582 if (fd == -1) {
5583 ret = 0;
5584 continue;
5585 }
5586
5587 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
5588 index = i & IORING_FILE_TABLE_MASK;
5589 file = fget(fd);
5590
5591 ret = -EBADF;
5592 if (!file)
5593 break;
5594
5595 /*
5596 * Don't allow io_uring instances to be registered. If UNIX
5597 * isn't enabled, then this causes a reference cycle and this
5598 * instance can never get freed. If UNIX is enabled we'll
5599 * handle it just fine, but there's still no point in allowing
5600 * a ring fd as it doesn't support regular read/write anyway.
5601 */
5602 if (file->f_op == &io_uring_fops) {
5603 fput(file);
5604 break;
5605 }
5606 ret = 0;
5607 table->files[index] = file;
5608 }
5609
5610 if (ret) {
5611 for (i = 0; i < ctx->nr_user_files; i++) {
5612 file = io_file_from_index(ctx, i);
5613 if (file)
5614 fput(file);
5615 }
5616 for (i = 0; i < nr_tables; i++)
5617 kfree(ctx->file_data->table[i].files);
5618
5619 kfree(ctx->file_data->table);
5620 kfree(ctx->file_data);
5621 ctx->file_data = NULL;
5622 ctx->nr_user_files = 0;
5623 return ret;
5624 }
5625
5626 ret = io_sqe_files_scm(ctx);
5627 if (ret)
5628 io_sqe_files_unregister(ctx);
5629
5630 return ret;
5631}
5632
Jens Axboec3a31e62019-10-03 13:59:56 -06005633static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
5634 int index)
5635{
5636#if defined(CONFIG_UNIX)
5637 struct sock *sock = ctx->ring_sock->sk;
5638 struct sk_buff_head *head = &sock->sk_receive_queue;
5639 struct sk_buff *skb;
5640
5641 /*
5642 * See if we can merge this file into an existing skb SCM_RIGHTS
5643 * file set. If there's no room, fall back to allocating a new skb
5644 * and filling it in.
5645 */
5646 spin_lock_irq(&head->lock);
5647 skb = skb_peek(head);
5648 if (skb) {
5649 struct scm_fp_list *fpl = UNIXCB(skb).fp;
5650
5651 if (fpl->count < SCM_MAX_FD) {
5652 __skb_unlink(skb, head);
5653 spin_unlock_irq(&head->lock);
5654 fpl->fp[fpl->count] = get_file(file);
5655 unix_inflight(fpl->user, fpl->fp[fpl->count]);
5656 fpl->count++;
5657 spin_lock_irq(&head->lock);
5658 __skb_queue_head(head, skb);
5659 } else {
5660 skb = NULL;
5661 }
5662 }
5663 spin_unlock_irq(&head->lock);
5664
5665 if (skb) {
5666 fput(file);
5667 return 0;
5668 }
5669
5670 return __io_sqe_files_scm(ctx, 1, index);
5671#else
5672 return 0;
5673#endif
5674}
5675
Jens Axboe05f3fb32019-12-09 11:22:50 -07005676static void io_atomic_switch(struct percpu_ref *ref)
Jens Axboec3a31e62019-10-03 13:59:56 -06005677{
Jens Axboe05f3fb32019-12-09 11:22:50 -07005678 struct fixed_file_data *data;
5679
5680 data = container_of(ref, struct fixed_file_data, refs);
5681 clear_bit(FFD_F_ATOMIC, &data->state);
5682}
5683
5684static bool io_queue_file_removal(struct fixed_file_data *data,
5685 struct file *file)
5686{
5687 struct io_file_put *pfile, pfile_stack;
5688 DECLARE_COMPLETION_ONSTACK(done);
5689
5690 /*
5691 * If we fail allocating the struct we need for doing async reomval
5692 * of this file, just punt to sync and wait for it.
5693 */
5694 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
5695 if (!pfile) {
5696 pfile = &pfile_stack;
5697 pfile->done = &done;
5698 }
5699
5700 pfile->file = file;
5701 llist_add(&pfile->llist, &data->put_llist);
5702
5703 if (pfile == &pfile_stack) {
5704 if (!test_and_set_bit(FFD_F_ATOMIC, &data->state)) {
5705 percpu_ref_put(&data->refs);
5706 percpu_ref_switch_to_atomic(&data->refs,
5707 io_atomic_switch);
5708 }
5709 wait_for_completion(&done);
5710 flush_work(&data->ref_work);
5711 return false;
5712 }
5713
5714 return true;
5715}
5716
5717static int __io_sqe_files_update(struct io_ring_ctx *ctx,
5718 struct io_uring_files_update *up,
5719 unsigned nr_args)
5720{
5721 struct fixed_file_data *data = ctx->file_data;
5722 bool ref_switch = false;
5723 struct file *file;
Jens Axboec3a31e62019-10-03 13:59:56 -06005724 __s32 __user *fds;
5725 int fd, i, err;
5726 __u32 done;
5727
Jens Axboe05f3fb32019-12-09 11:22:50 -07005728 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06005729 return -EOVERFLOW;
5730 if (done > ctx->nr_user_files)
5731 return -EINVAL;
5732
5733 done = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005734 fds = u64_to_user_ptr(up->fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06005735 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005736 struct fixed_file_table *table;
5737 unsigned index;
5738
Jens Axboec3a31e62019-10-03 13:59:56 -06005739 err = 0;
5740 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
5741 err = -EFAULT;
5742 break;
5743 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07005744 i = array_index_nospec(up->offset, ctx->nr_user_files);
5745 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
Jens Axboe65e19f52019-10-26 07:20:21 -06005746 index = i & IORING_FILE_TABLE_MASK;
5747 if (table->files[index]) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005748 file = io_file_from_index(ctx, index);
Jens Axboe65e19f52019-10-26 07:20:21 -06005749 table->files[index] = NULL;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005750 if (io_queue_file_removal(data, file))
5751 ref_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06005752 }
5753 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06005754 file = fget(fd);
5755 if (!file) {
5756 err = -EBADF;
5757 break;
5758 }
5759 /*
5760 * Don't allow io_uring instances to be registered. If
5761 * UNIX isn't enabled, then this causes a reference
5762 * cycle and this instance can never get freed. If UNIX
5763 * is enabled we'll handle it just fine, but there's
5764 * still no point in allowing a ring fd as it doesn't
5765 * support regular read/write anyway.
5766 */
5767 if (file->f_op == &io_uring_fops) {
5768 fput(file);
5769 err = -EBADF;
5770 break;
5771 }
Jens Axboe65e19f52019-10-26 07:20:21 -06005772 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06005773 err = io_sqe_file_register(ctx, file, i);
5774 if (err)
5775 break;
5776 }
5777 nr_args--;
5778 done++;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005779 up->offset++;
5780 }
5781
5782 if (ref_switch && !test_and_set_bit(FFD_F_ATOMIC, &data->state)) {
5783 percpu_ref_put(&data->refs);
5784 percpu_ref_switch_to_atomic(&data->refs, io_atomic_switch);
Jens Axboec3a31e62019-10-03 13:59:56 -06005785 }
5786
5787 return done ? done : err;
5788}
Jens Axboe05f3fb32019-12-09 11:22:50 -07005789static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
5790 unsigned nr_args)
5791{
5792 struct io_uring_files_update up;
5793
5794 if (!ctx->file_data)
5795 return -ENXIO;
5796 if (!nr_args)
5797 return -EINVAL;
5798 if (copy_from_user(&up, arg, sizeof(up)))
5799 return -EFAULT;
5800 if (up.resv)
5801 return -EINVAL;
5802
5803 return __io_sqe_files_update(ctx, &up, nr_args);
5804}
Jens Axboec3a31e62019-10-03 13:59:56 -06005805
Jens Axboe7d723062019-11-12 22:31:31 -07005806static void io_put_work(struct io_wq_work *work)
5807{
5808 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
5809
5810 io_put_req(req);
5811}
5812
5813static void io_get_work(struct io_wq_work *work)
5814{
5815 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
5816
5817 refcount_inc(&req->refs);
5818}
5819
Pavel Begunkov24369c22020-01-28 03:15:48 +03005820static int io_init_wq_offload(struct io_ring_ctx *ctx,
5821 struct io_uring_params *p)
5822{
5823 struct io_wq_data data;
5824 struct fd f;
5825 struct io_ring_ctx *ctx_attach;
5826 unsigned int concurrency;
5827 int ret = 0;
5828
5829 data.user = ctx->user;
5830 data.get_work = io_get_work;
5831 data.put_work = io_put_work;
5832
5833 if (!(p->flags & IORING_SETUP_ATTACH_WQ)) {
5834 /* Do QD, or 4 * CPUS, whatever is smallest */
5835 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
5836
5837 ctx->io_wq = io_wq_create(concurrency, &data);
5838 if (IS_ERR(ctx->io_wq)) {
5839 ret = PTR_ERR(ctx->io_wq);
5840 ctx->io_wq = NULL;
5841 }
5842 return ret;
5843 }
5844
5845 f = fdget(p->wq_fd);
5846 if (!f.file)
5847 return -EBADF;
5848
5849 if (f.file->f_op != &io_uring_fops) {
5850 ret = -EINVAL;
5851 goto out_fput;
5852 }
5853
5854 ctx_attach = f.file->private_data;
5855 /* @io_wq is protected by holding the fd */
5856 if (!io_wq_get(ctx_attach->io_wq, &data)) {
5857 ret = -EINVAL;
5858 goto out_fput;
5859 }
5860
5861 ctx->io_wq = ctx_attach->io_wq;
5862out_fput:
5863 fdput(f);
5864 return ret;
5865}
5866
Jens Axboe6c271ce2019-01-10 11:22:30 -07005867static int io_sq_offload_start(struct io_ring_ctx *ctx,
5868 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005869{
5870 int ret;
5871
Jens Axboe6c271ce2019-01-10 11:22:30 -07005872 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005873 mmgrab(current->mm);
5874 ctx->sqo_mm = current->mm;
5875
Jens Axboe6c271ce2019-01-10 11:22:30 -07005876 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06005877 ret = -EPERM;
5878 if (!capable(CAP_SYS_ADMIN))
5879 goto err;
5880
Jens Axboe917257d2019-04-13 09:28:55 -06005881 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
5882 if (!ctx->sq_thread_idle)
5883 ctx->sq_thread_idle = HZ;
5884
Jens Axboe6c271ce2019-01-10 11:22:30 -07005885 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06005886 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005887
Jens Axboe917257d2019-04-13 09:28:55 -06005888 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06005889 if (cpu >= nr_cpu_ids)
5890 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08005891 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06005892 goto err;
5893
Jens Axboe6c271ce2019-01-10 11:22:30 -07005894 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
5895 ctx, cpu,
5896 "io_uring-sq");
5897 } else {
5898 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
5899 "io_uring-sq");
5900 }
5901 if (IS_ERR(ctx->sqo_thread)) {
5902 ret = PTR_ERR(ctx->sqo_thread);
5903 ctx->sqo_thread = NULL;
5904 goto err;
5905 }
5906 wake_up_process(ctx->sqo_thread);
5907 } else if (p->flags & IORING_SETUP_SQ_AFF) {
5908 /* Can't have SQ_AFF without SQPOLL */
5909 ret = -EINVAL;
5910 goto err;
5911 }
5912
Pavel Begunkov24369c22020-01-28 03:15:48 +03005913 ret = io_init_wq_offload(ctx, p);
5914 if (ret)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005915 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005916
5917 return 0;
5918err:
Jens Axboe54a91f32019-09-10 09:15:04 -06005919 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005920 mmdrop(ctx->sqo_mm);
5921 ctx->sqo_mm = NULL;
5922 return ret;
5923}
5924
5925static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
5926{
5927 atomic_long_sub(nr_pages, &user->locked_vm);
5928}
5929
5930static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
5931{
5932 unsigned long page_limit, cur_pages, new_pages;
5933
5934 /* Don't allow more pages than we can safely lock */
5935 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
5936
5937 do {
5938 cur_pages = atomic_long_read(&user->locked_vm);
5939 new_pages = cur_pages + nr_pages;
5940 if (new_pages > page_limit)
5941 return -ENOMEM;
5942 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
5943 new_pages) != cur_pages);
5944
5945 return 0;
5946}
5947
5948static void io_mem_free(void *ptr)
5949{
Mark Rutland52e04ef2019-04-30 17:30:21 +01005950 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005951
Mark Rutland52e04ef2019-04-30 17:30:21 +01005952 if (!ptr)
5953 return;
5954
5955 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005956 if (put_page_testzero(page))
5957 free_compound_page(page);
5958}
5959
5960static void *io_mem_alloc(size_t size)
5961{
5962 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
5963 __GFP_NORETRY;
5964
5965 return (void *) __get_free_pages(gfp_flags, get_order(size));
5966}
5967
Hristo Venev75b28af2019-08-26 17:23:46 +00005968static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
5969 size_t *sq_offset)
5970{
5971 struct io_rings *rings;
5972 size_t off, sq_array_size;
5973
5974 off = struct_size(rings, cqes, cq_entries);
5975 if (off == SIZE_MAX)
5976 return SIZE_MAX;
5977
5978#ifdef CONFIG_SMP
5979 off = ALIGN(off, SMP_CACHE_BYTES);
5980 if (off == 0)
5981 return SIZE_MAX;
5982#endif
5983
5984 sq_array_size = array_size(sizeof(u32), sq_entries);
5985 if (sq_array_size == SIZE_MAX)
5986 return SIZE_MAX;
5987
5988 if (check_add_overflow(off, sq_array_size, &off))
5989 return SIZE_MAX;
5990
5991 if (sq_offset)
5992 *sq_offset = off;
5993
5994 return off;
5995}
5996
Jens Axboe2b188cc2019-01-07 10:46:33 -07005997static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
5998{
Hristo Venev75b28af2019-08-26 17:23:46 +00005999 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006000
Hristo Venev75b28af2019-08-26 17:23:46 +00006001 pages = (size_t)1 << get_order(
6002 rings_size(sq_entries, cq_entries, NULL));
6003 pages += (size_t)1 << get_order(
6004 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07006005
Hristo Venev75b28af2019-08-26 17:23:46 +00006006 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006007}
6008
Jens Axboeedafcce2019-01-09 09:16:05 -07006009static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
6010{
6011 int i, j;
6012
6013 if (!ctx->user_bufs)
6014 return -ENXIO;
6015
6016 for (i = 0; i < ctx->nr_user_bufs; i++) {
6017 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
6018
6019 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08006020 unpin_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07006021
6022 if (ctx->account_mem)
6023 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006024 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07006025 imu->nr_bvecs = 0;
6026 }
6027
6028 kfree(ctx->user_bufs);
6029 ctx->user_bufs = NULL;
6030 ctx->nr_user_bufs = 0;
6031 return 0;
6032}
6033
6034static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
6035 void __user *arg, unsigned index)
6036{
6037 struct iovec __user *src;
6038
6039#ifdef CONFIG_COMPAT
6040 if (ctx->compat) {
6041 struct compat_iovec __user *ciovs;
6042 struct compat_iovec ciov;
6043
6044 ciovs = (struct compat_iovec __user *) arg;
6045 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
6046 return -EFAULT;
6047
Jens Axboed55e5f52019-12-11 16:12:15 -07006048 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07006049 dst->iov_len = ciov.iov_len;
6050 return 0;
6051 }
6052#endif
6053 src = (struct iovec __user *) arg;
6054 if (copy_from_user(dst, &src[index], sizeof(*dst)))
6055 return -EFAULT;
6056 return 0;
6057}
6058
6059static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
6060 unsigned nr_args)
6061{
6062 struct vm_area_struct **vmas = NULL;
6063 struct page **pages = NULL;
6064 int i, j, got_pages = 0;
6065 int ret = -EINVAL;
6066
6067 if (ctx->user_bufs)
6068 return -EBUSY;
6069 if (!nr_args || nr_args > UIO_MAXIOV)
6070 return -EINVAL;
6071
6072 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
6073 GFP_KERNEL);
6074 if (!ctx->user_bufs)
6075 return -ENOMEM;
6076
6077 for (i = 0; i < nr_args; i++) {
6078 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
6079 unsigned long off, start, end, ubuf;
6080 int pret, nr_pages;
6081 struct iovec iov;
6082 size_t size;
6083
6084 ret = io_copy_iov(ctx, &iov, arg, i);
6085 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03006086 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07006087
6088 /*
6089 * Don't impose further limits on the size and buffer
6090 * constraints here, we'll -EINVAL later when IO is
6091 * submitted if they are wrong.
6092 */
6093 ret = -EFAULT;
6094 if (!iov.iov_base || !iov.iov_len)
6095 goto err;
6096
6097 /* arbitrary limit, but we need something */
6098 if (iov.iov_len > SZ_1G)
6099 goto err;
6100
6101 ubuf = (unsigned long) iov.iov_base;
6102 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
6103 start = ubuf >> PAGE_SHIFT;
6104 nr_pages = end - start;
6105
6106 if (ctx->account_mem) {
6107 ret = io_account_mem(ctx->user, nr_pages);
6108 if (ret)
6109 goto err;
6110 }
6111
6112 ret = 0;
6113 if (!pages || nr_pages > got_pages) {
6114 kfree(vmas);
6115 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006116 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07006117 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006118 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07006119 sizeof(struct vm_area_struct *),
6120 GFP_KERNEL);
6121 if (!pages || !vmas) {
6122 ret = -ENOMEM;
6123 if (ctx->account_mem)
6124 io_unaccount_mem(ctx->user, nr_pages);
6125 goto err;
6126 }
6127 got_pages = nr_pages;
6128 }
6129
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006130 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07006131 GFP_KERNEL);
6132 ret = -ENOMEM;
6133 if (!imu->bvec) {
6134 if (ctx->account_mem)
6135 io_unaccount_mem(ctx->user, nr_pages);
6136 goto err;
6137 }
6138
6139 ret = 0;
6140 down_read(&current->mm->mmap_sem);
John Hubbard2113b052020-01-30 22:13:13 -08006141 pret = pin_user_pages(ubuf, nr_pages,
Ira Weiny932f4a62019-05-13 17:17:03 -07006142 FOLL_WRITE | FOLL_LONGTERM,
6143 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07006144 if (pret == nr_pages) {
6145 /* don't support file backed memory */
6146 for (j = 0; j < nr_pages; j++) {
6147 struct vm_area_struct *vma = vmas[j];
6148
6149 if (vma->vm_file &&
6150 !is_file_hugepages(vma->vm_file)) {
6151 ret = -EOPNOTSUPP;
6152 break;
6153 }
6154 }
6155 } else {
6156 ret = pret < 0 ? pret : -EFAULT;
6157 }
6158 up_read(&current->mm->mmap_sem);
6159 if (ret) {
6160 /*
6161 * if we did partial map, or found file backed vmas,
6162 * release any pages we did get
6163 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07006164 if (pret > 0)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08006165 unpin_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07006166 if (ctx->account_mem)
6167 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006168 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07006169 goto err;
6170 }
6171
6172 off = ubuf & ~PAGE_MASK;
6173 size = iov.iov_len;
6174 for (j = 0; j < nr_pages; j++) {
6175 size_t vec_len;
6176
6177 vec_len = min_t(size_t, size, PAGE_SIZE - off);
6178 imu->bvec[j].bv_page = pages[j];
6179 imu->bvec[j].bv_len = vec_len;
6180 imu->bvec[j].bv_offset = off;
6181 off = 0;
6182 size -= vec_len;
6183 }
6184 /* store original address for later verification */
6185 imu->ubuf = ubuf;
6186 imu->len = iov.iov_len;
6187 imu->nr_bvecs = nr_pages;
6188
6189 ctx->nr_user_bufs++;
6190 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006191 kvfree(pages);
6192 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07006193 return 0;
6194err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006195 kvfree(pages);
6196 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07006197 io_sqe_buffer_unregister(ctx);
6198 return ret;
6199}
6200
Jens Axboe9b402842019-04-11 11:45:41 -06006201static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
6202{
6203 __s32 __user *fds = arg;
6204 int fd;
6205
6206 if (ctx->cq_ev_fd)
6207 return -EBUSY;
6208
6209 if (copy_from_user(&fd, fds, sizeof(*fds)))
6210 return -EFAULT;
6211
6212 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
6213 if (IS_ERR(ctx->cq_ev_fd)) {
6214 int ret = PTR_ERR(ctx->cq_ev_fd);
6215 ctx->cq_ev_fd = NULL;
6216 return ret;
6217 }
6218
6219 return 0;
6220}
6221
6222static int io_eventfd_unregister(struct io_ring_ctx *ctx)
6223{
6224 if (ctx->cq_ev_fd) {
6225 eventfd_ctx_put(ctx->cq_ev_fd);
6226 ctx->cq_ev_fd = NULL;
6227 return 0;
6228 }
6229
6230 return -ENXIO;
6231}
6232
Jens Axboe2b188cc2019-01-07 10:46:33 -07006233static void io_ring_ctx_free(struct io_ring_ctx *ctx)
6234{
Jens Axboe6b063142019-01-10 22:13:58 -07006235 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006236 if (ctx->sqo_mm)
6237 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07006238
6239 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07006240 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07006241 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06006242 io_eventfd_unregister(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07006243
Jens Axboe2b188cc2019-01-07 10:46:33 -07006244#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07006245 if (ctx->ring_sock) {
6246 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07006247 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07006248 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006249#endif
6250
Hristo Venev75b28af2019-08-26 17:23:46 +00006251 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006252 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006253
6254 percpu_ref_exit(&ctx->refs);
6255 if (ctx->account_mem)
6256 io_unaccount_mem(ctx->user,
6257 ring_pages(ctx->sq_entries, ctx->cq_entries));
6258 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07006259 put_cred(ctx->creds);
Jens Axboe206aefd2019-11-07 18:27:42 -07006260 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -07006261 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07006262 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006263 kfree(ctx);
6264}
6265
6266static __poll_t io_uring_poll(struct file *file, poll_table *wait)
6267{
6268 struct io_ring_ctx *ctx = file->private_data;
6269 __poll_t mask = 0;
6270
6271 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02006272 /*
6273 * synchronizes with barrier from wq_has_sleeper call in
6274 * io_commit_cqring
6275 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07006276 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00006277 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
6278 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006279 mask |= EPOLLOUT | EPOLLWRNORM;
yangerkundaa5de52019-09-24 20:53:34 +08006280 if (READ_ONCE(ctx->rings->cq.head) != ctx->cached_cq_tail)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006281 mask |= EPOLLIN | EPOLLRDNORM;
6282
6283 return mask;
6284}
6285
6286static int io_uring_fasync(int fd, struct file *file, int on)
6287{
6288 struct io_ring_ctx *ctx = file->private_data;
6289
6290 return fasync_helper(fd, file, on, &ctx->cq_fasync);
6291}
6292
Jens Axboe071698e2020-01-28 10:04:42 -07006293static int io_remove_personalities(int id, void *p, void *data)
6294{
6295 struct io_ring_ctx *ctx = data;
6296 const struct cred *cred;
6297
6298 cred = idr_remove(&ctx->personality_idr, id);
6299 if (cred)
6300 put_cred(cred);
6301 return 0;
6302}
6303
Jens Axboe2b188cc2019-01-07 10:46:33 -07006304static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
6305{
6306 mutex_lock(&ctx->uring_lock);
6307 percpu_ref_kill(&ctx->refs);
6308 mutex_unlock(&ctx->uring_lock);
6309
Jens Axboedf069d82020-02-04 16:48:34 -07006310 /*
6311 * Wait for sq thread to idle, if we have one. It won't spin on new
6312 * work after we've killed the ctx ref above. This is important to do
6313 * before we cancel existing commands, as the thread could otherwise
6314 * be queueing new work post that. If that's work we need to cancel,
6315 * it could cause shutdown to hang.
6316 */
6317 while (ctx->sqo_thread && !wq_has_sleeper(&ctx->sqo_wait))
6318 cpu_relax();
6319
Jens Axboe5262f562019-09-17 12:26:57 -06006320 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07006321 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06006322
6323 if (ctx->io_wq)
6324 io_wq_cancel_all(ctx->io_wq);
6325
Jens Axboedef596e2019-01-09 08:59:42 -07006326 io_iopoll_reap_events(ctx);
Jens Axboe15dff282019-11-13 09:09:23 -07006327 /* if we failed setting up the ctx, we might not have any rings */
6328 if (ctx->rings)
6329 io_cqring_overflow_flush(ctx, true);
Jens Axboe071698e2020-01-28 10:04:42 -07006330 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
Jens Axboe206aefd2019-11-07 18:27:42 -07006331 wait_for_completion(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006332 io_ring_ctx_free(ctx);
6333}
6334
6335static int io_uring_release(struct inode *inode, struct file *file)
6336{
6337 struct io_ring_ctx *ctx = file->private_data;
6338
6339 file->private_data = NULL;
6340 io_ring_ctx_wait_and_kill(ctx);
6341 return 0;
6342}
6343
Jens Axboefcb323c2019-10-24 12:39:47 -06006344static void io_uring_cancel_files(struct io_ring_ctx *ctx,
6345 struct files_struct *files)
6346{
6347 struct io_kiocb *req;
6348 DEFINE_WAIT(wait);
6349
6350 while (!list_empty_careful(&ctx->inflight_list)) {
Jens Axboe768134d2019-11-10 20:30:53 -07006351 struct io_kiocb *cancel_req = NULL;
Jens Axboefcb323c2019-10-24 12:39:47 -06006352
6353 spin_lock_irq(&ctx->inflight_lock);
6354 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07006355 if (req->work.files != files)
6356 continue;
6357 /* req is being completed, ignore */
6358 if (!refcount_inc_not_zero(&req->refs))
6359 continue;
6360 cancel_req = req;
6361 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06006362 }
Jens Axboe768134d2019-11-10 20:30:53 -07006363 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06006364 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07006365 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06006366 spin_unlock_irq(&ctx->inflight_lock);
6367
Jens Axboe768134d2019-11-10 20:30:53 -07006368 /* We need to keep going until we don't find a matching req */
6369 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06006370 break;
Bob Liu2f6d9b92019-11-13 18:06:24 +08006371
6372 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
6373 io_put_req(cancel_req);
Jens Axboefcb323c2019-10-24 12:39:47 -06006374 schedule();
6375 }
Jens Axboe768134d2019-11-10 20:30:53 -07006376 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06006377}
6378
6379static int io_uring_flush(struct file *file, void *data)
6380{
6381 struct io_ring_ctx *ctx = file->private_data;
6382
6383 io_uring_cancel_files(ctx, data);
Jens Axboefcb323c2019-10-24 12:39:47 -06006384 return 0;
6385}
6386
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006387static void *io_uring_validate_mmap_request(struct file *file,
6388 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006389{
Jens Axboe2b188cc2019-01-07 10:46:33 -07006390 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006391 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006392 struct page *page;
6393 void *ptr;
6394
6395 switch (offset) {
6396 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00006397 case IORING_OFF_CQ_RING:
6398 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006399 break;
6400 case IORING_OFF_SQES:
6401 ptr = ctx->sq_sqes;
6402 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006403 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006404 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006405 }
6406
6407 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07006408 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006409 return ERR_PTR(-EINVAL);
6410
6411 return ptr;
6412}
6413
6414#ifdef CONFIG_MMU
6415
6416static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
6417{
6418 size_t sz = vma->vm_end - vma->vm_start;
6419 unsigned long pfn;
6420 void *ptr;
6421
6422 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
6423 if (IS_ERR(ptr))
6424 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006425
6426 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
6427 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
6428}
6429
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006430#else /* !CONFIG_MMU */
6431
6432static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
6433{
6434 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
6435}
6436
6437static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
6438{
6439 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
6440}
6441
6442static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
6443 unsigned long addr, unsigned long len,
6444 unsigned long pgoff, unsigned long flags)
6445{
6446 void *ptr;
6447
6448 ptr = io_uring_validate_mmap_request(file, pgoff, len);
6449 if (IS_ERR(ptr))
6450 return PTR_ERR(ptr);
6451
6452 return (unsigned long) ptr;
6453}
6454
6455#endif /* !CONFIG_MMU */
6456
Jens Axboe2b188cc2019-01-07 10:46:33 -07006457SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
6458 u32, min_complete, u32, flags, const sigset_t __user *, sig,
6459 size_t, sigsz)
6460{
6461 struct io_ring_ctx *ctx;
6462 long ret = -EBADF;
6463 int submitted = 0;
6464 struct fd f;
6465
Jens Axboe6c271ce2019-01-10 11:22:30 -07006466 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006467 return -EINVAL;
6468
6469 f = fdget(fd);
6470 if (!f.file)
6471 return -EBADF;
6472
6473 ret = -EOPNOTSUPP;
6474 if (f.file->f_op != &io_uring_fops)
6475 goto out_fput;
6476
6477 ret = -ENXIO;
6478 ctx = f.file->private_data;
6479 if (!percpu_ref_tryget(&ctx->refs))
6480 goto out_fput;
6481
Jens Axboe6c271ce2019-01-10 11:22:30 -07006482 /*
6483 * For SQ polling, the thread will do all submissions and completions.
6484 * Just return the requested submit count, and wake the thread if
6485 * we were asked to.
6486 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06006487 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006488 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07006489 if (!list_empty_careful(&ctx->cq_overflow_list))
6490 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006491 if (flags & IORING_ENTER_SQ_WAKEUP)
6492 wake_up(&ctx->sqo_wait);
6493 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06006494 } else if (to_submit) {
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006495 struct mm_struct *cur_mm;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006496
6497 mutex_lock(&ctx->uring_lock);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006498 /* already have mm, so io_submit_sqes() won't try to grab it */
6499 cur_mm = ctx->sqo_mm;
6500 submitted = io_submit_sqes(ctx, to_submit, f.file, fd,
6501 &cur_mm, false);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006502 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03006503
6504 if (submitted != to_submit)
6505 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006506 }
6507 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07006508 unsigned nr_events = 0;
6509
Jens Axboe2b188cc2019-01-07 10:46:33 -07006510 min_complete = min(min_complete, ctx->cq_entries);
6511
Jens Axboedef596e2019-01-09 08:59:42 -07006512 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07006513 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07006514 } else {
6515 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
6516 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006517 }
6518
Pavel Begunkov7c504e652019-12-18 19:53:45 +03006519out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03006520 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006521out_fput:
6522 fdput(f);
6523 return submitted ? submitted : ret;
6524}
6525
Jens Axboe87ce9552020-01-30 08:25:34 -07006526static int io_uring_show_cred(int id, void *p, void *data)
6527{
6528 const struct cred *cred = p;
6529 struct seq_file *m = data;
6530 struct user_namespace *uns = seq_user_ns(m);
6531 struct group_info *gi;
6532 kernel_cap_t cap;
6533 unsigned __capi;
6534 int g;
6535
6536 seq_printf(m, "%5d\n", id);
6537 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
6538 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
6539 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
6540 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
6541 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
6542 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
6543 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
6544 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
6545 seq_puts(m, "\n\tGroups:\t");
6546 gi = cred->group_info;
6547 for (g = 0; g < gi->ngroups; g++) {
6548 seq_put_decimal_ull(m, g ? " " : "",
6549 from_kgid_munged(uns, gi->gid[g]));
6550 }
6551 seq_puts(m, "\n\tCapEff:\t");
6552 cap = cred->cap_effective;
6553 CAP_FOR_EACH_U32(__capi)
6554 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
6555 seq_putc(m, '\n');
6556 return 0;
6557}
6558
6559static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
6560{
6561 int i;
6562
6563 mutex_lock(&ctx->uring_lock);
6564 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
6565 for (i = 0; i < ctx->nr_user_files; i++) {
6566 struct fixed_file_table *table;
6567 struct file *f;
6568
6569 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
6570 f = table->files[i & IORING_FILE_TABLE_MASK];
6571 if (f)
6572 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
6573 else
6574 seq_printf(m, "%5u: <none>\n", i);
6575 }
6576 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
6577 for (i = 0; i < ctx->nr_user_bufs; i++) {
6578 struct io_mapped_ubuf *buf = &ctx->user_bufs[i];
6579
6580 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf,
6581 (unsigned int) buf->len);
6582 }
6583 if (!idr_is_empty(&ctx->personality_idr)) {
6584 seq_printf(m, "Personalities:\n");
6585 idr_for_each(&ctx->personality_idr, io_uring_show_cred, m);
6586 }
6587 mutex_unlock(&ctx->uring_lock);
6588}
6589
6590static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
6591{
6592 struct io_ring_ctx *ctx = f->private_data;
6593
6594 if (percpu_ref_tryget(&ctx->refs)) {
6595 __io_uring_show_fdinfo(ctx, m);
6596 percpu_ref_put(&ctx->refs);
6597 }
6598}
6599
Jens Axboe2b188cc2019-01-07 10:46:33 -07006600static const struct file_operations io_uring_fops = {
6601 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06006602 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07006603 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006604#ifndef CONFIG_MMU
6605 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
6606 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
6607#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07006608 .poll = io_uring_poll,
6609 .fasync = io_uring_fasync,
Jens Axboe87ce9552020-01-30 08:25:34 -07006610 .show_fdinfo = io_uring_show_fdinfo,
Jens Axboe2b188cc2019-01-07 10:46:33 -07006611};
6612
6613static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
6614 struct io_uring_params *p)
6615{
Hristo Venev75b28af2019-08-26 17:23:46 +00006616 struct io_rings *rings;
6617 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006618
Hristo Venev75b28af2019-08-26 17:23:46 +00006619 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
6620 if (size == SIZE_MAX)
6621 return -EOVERFLOW;
6622
6623 rings = io_mem_alloc(size);
6624 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006625 return -ENOMEM;
6626
Hristo Venev75b28af2019-08-26 17:23:46 +00006627 ctx->rings = rings;
6628 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
6629 rings->sq_ring_mask = p->sq_entries - 1;
6630 rings->cq_ring_mask = p->cq_entries - 1;
6631 rings->sq_ring_entries = p->sq_entries;
6632 rings->cq_ring_entries = p->cq_entries;
6633 ctx->sq_mask = rings->sq_ring_mask;
6634 ctx->cq_mask = rings->cq_ring_mask;
6635 ctx->sq_entries = rings->sq_ring_entries;
6636 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006637
6638 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07006639 if (size == SIZE_MAX) {
6640 io_mem_free(ctx->rings);
6641 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006642 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07006643 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006644
6645 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07006646 if (!ctx->sq_sqes) {
6647 io_mem_free(ctx->rings);
6648 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006649 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07006650 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006651
Jens Axboe2b188cc2019-01-07 10:46:33 -07006652 return 0;
6653}
6654
6655/*
6656 * Allocate an anonymous fd, this is what constitutes the application
6657 * visible backing of an io_uring instance. The application mmaps this
6658 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
6659 * we have to tie this fd to a socket for file garbage collection purposes.
6660 */
6661static int io_uring_get_fd(struct io_ring_ctx *ctx)
6662{
6663 struct file *file;
6664 int ret;
6665
6666#if defined(CONFIG_UNIX)
6667 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
6668 &ctx->ring_sock);
6669 if (ret)
6670 return ret;
6671#endif
6672
6673 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
6674 if (ret < 0)
6675 goto err;
6676
6677 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
6678 O_RDWR | O_CLOEXEC);
6679 if (IS_ERR(file)) {
6680 put_unused_fd(ret);
6681 ret = PTR_ERR(file);
6682 goto err;
6683 }
6684
6685#if defined(CONFIG_UNIX)
6686 ctx->ring_sock->file = file;
6687#endif
6688 fd_install(ret, file);
6689 return ret;
6690err:
6691#if defined(CONFIG_UNIX)
6692 sock_release(ctx->ring_sock);
6693 ctx->ring_sock = NULL;
6694#endif
6695 return ret;
6696}
6697
6698static int io_uring_create(unsigned entries, struct io_uring_params *p)
6699{
6700 struct user_struct *user = NULL;
6701 struct io_ring_ctx *ctx;
6702 bool account_mem;
6703 int ret;
6704
Jens Axboe8110c1a2019-12-28 15:39:54 -07006705 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006706 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07006707 if (entries > IORING_MAX_ENTRIES) {
6708 if (!(p->flags & IORING_SETUP_CLAMP))
6709 return -EINVAL;
6710 entries = IORING_MAX_ENTRIES;
6711 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006712
6713 /*
6714 * Use twice as many entries for the CQ ring. It's possible for the
6715 * application to drive a higher depth than the size of the SQ ring,
6716 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06006717 * some flexibility in overcommitting a bit. If the application has
6718 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
6719 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07006720 */
6721 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06006722 if (p->flags & IORING_SETUP_CQSIZE) {
6723 /*
6724 * If IORING_SETUP_CQSIZE is set, we do the same roundup
6725 * to a power-of-two, if it isn't already. We do NOT impose
6726 * any cq vs sq ring sizing.
6727 */
Jens Axboe8110c1a2019-12-28 15:39:54 -07006728 if (p->cq_entries < p->sq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06006729 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07006730 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
6731 if (!(p->flags & IORING_SETUP_CLAMP))
6732 return -EINVAL;
6733 p->cq_entries = IORING_MAX_CQ_ENTRIES;
6734 }
Jens Axboe33a107f2019-10-04 12:10:03 -06006735 p->cq_entries = roundup_pow_of_two(p->cq_entries);
6736 } else {
6737 p->cq_entries = 2 * p->sq_entries;
6738 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006739
6740 user = get_uid(current_user());
6741 account_mem = !capable(CAP_IPC_LOCK);
6742
6743 if (account_mem) {
6744 ret = io_account_mem(user,
6745 ring_pages(p->sq_entries, p->cq_entries));
6746 if (ret) {
6747 free_uid(user);
6748 return ret;
6749 }
6750 }
6751
6752 ctx = io_ring_ctx_alloc(p);
6753 if (!ctx) {
6754 if (account_mem)
6755 io_unaccount_mem(user, ring_pages(p->sq_entries,
6756 p->cq_entries));
6757 free_uid(user);
6758 return -ENOMEM;
6759 }
6760 ctx->compat = in_compat_syscall();
6761 ctx->account_mem = account_mem;
6762 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07006763 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07006764
6765 ret = io_allocate_scq_urings(ctx, p);
6766 if (ret)
6767 goto err;
6768
Jens Axboe6c271ce2019-01-10 11:22:30 -07006769 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006770 if (ret)
6771 goto err;
6772
Jens Axboe2b188cc2019-01-07 10:46:33 -07006773 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00006774 p->sq_off.head = offsetof(struct io_rings, sq.head);
6775 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
6776 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
6777 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
6778 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
6779 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
6780 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006781
6782 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00006783 p->cq_off.head = offsetof(struct io_rings, cq.head);
6784 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
6785 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
6786 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
6787 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
6788 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06006789
Jens Axboe044c1ab2019-10-28 09:15:33 -06006790 /*
6791 * Install ring fd as the very last thing, so we don't risk someone
6792 * having closed it before we finish setup
6793 */
6794 ret = io_uring_get_fd(ctx);
6795 if (ret < 0)
6796 goto err;
6797
Jens Axboeda8c9692019-12-02 18:51:26 -07006798 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
Jens Axboecccf0ee2020-01-27 16:34:48 -07006799 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
6800 IORING_FEAT_CUR_PERSONALITY;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006801 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006802 return ret;
6803err:
6804 io_ring_ctx_wait_and_kill(ctx);
6805 return ret;
6806}
6807
6808/*
6809 * Sets up an aio uring context, and returns the fd. Applications asks for a
6810 * ring size, we return the actual sq/cq ring sizes (among other things) in the
6811 * params structure passed in.
6812 */
6813static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
6814{
6815 struct io_uring_params p;
6816 long ret;
6817 int i;
6818
6819 if (copy_from_user(&p, params, sizeof(p)))
6820 return -EFAULT;
6821 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
6822 if (p.resv[i])
6823 return -EINVAL;
6824 }
6825
Jens Axboe6c271ce2019-01-10 11:22:30 -07006826 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07006827 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Pavel Begunkov24369c22020-01-28 03:15:48 +03006828 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006829 return -EINVAL;
6830
6831 ret = io_uring_create(entries, &p);
6832 if (ret < 0)
6833 return ret;
6834
6835 if (copy_to_user(params, &p, sizeof(p)))
6836 return -EFAULT;
6837
6838 return ret;
6839}
6840
6841SYSCALL_DEFINE2(io_uring_setup, u32, entries,
6842 struct io_uring_params __user *, params)
6843{
6844 return io_uring_setup(entries, params);
6845}
6846
Jens Axboe66f4af92020-01-16 15:36:52 -07006847static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
6848{
6849 struct io_uring_probe *p;
6850 size_t size;
6851 int i, ret;
6852
6853 size = struct_size(p, ops, nr_args);
6854 if (size == SIZE_MAX)
6855 return -EOVERFLOW;
6856 p = kzalloc(size, GFP_KERNEL);
6857 if (!p)
6858 return -ENOMEM;
6859
6860 ret = -EFAULT;
6861 if (copy_from_user(p, arg, size))
6862 goto out;
6863 ret = -EINVAL;
6864 if (memchr_inv(p, 0, size))
6865 goto out;
6866
6867 p->last_op = IORING_OP_LAST - 1;
6868 if (nr_args > IORING_OP_LAST)
6869 nr_args = IORING_OP_LAST;
6870
6871 for (i = 0; i < nr_args; i++) {
6872 p->ops[i].op = i;
6873 if (!io_op_defs[i].not_supported)
6874 p->ops[i].flags = IO_URING_OP_SUPPORTED;
6875 }
6876 p->ops_len = i;
6877
6878 ret = 0;
6879 if (copy_to_user(arg, p, size))
6880 ret = -EFAULT;
6881out:
6882 kfree(p);
6883 return ret;
6884}
6885
Jens Axboe071698e2020-01-28 10:04:42 -07006886static int io_register_personality(struct io_ring_ctx *ctx)
6887{
6888 const struct cred *creds = get_current_cred();
6889 int id;
6890
6891 id = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1,
6892 USHRT_MAX, GFP_KERNEL);
6893 if (id < 0)
6894 put_cred(creds);
6895 return id;
6896}
6897
6898static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
6899{
6900 const struct cred *old_creds;
6901
6902 old_creds = idr_remove(&ctx->personality_idr, id);
6903 if (old_creds) {
6904 put_cred(old_creds);
6905 return 0;
6906 }
6907
6908 return -EINVAL;
6909}
6910
6911static bool io_register_op_must_quiesce(int op)
6912{
6913 switch (op) {
6914 case IORING_UNREGISTER_FILES:
6915 case IORING_REGISTER_FILES_UPDATE:
6916 case IORING_REGISTER_PROBE:
6917 case IORING_REGISTER_PERSONALITY:
6918 case IORING_UNREGISTER_PERSONALITY:
6919 return false;
6920 default:
6921 return true;
6922 }
6923}
6924
Jens Axboeedafcce2019-01-09 09:16:05 -07006925static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
6926 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06006927 __releases(ctx->uring_lock)
6928 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07006929{
6930 int ret;
6931
Jens Axboe35fa71a2019-04-22 10:23:23 -06006932 /*
6933 * We're inside the ring mutex, if the ref is already dying, then
6934 * someone else killed the ctx or is already going through
6935 * io_uring_register().
6936 */
6937 if (percpu_ref_is_dying(&ctx->refs))
6938 return -ENXIO;
6939
Jens Axboe071698e2020-01-28 10:04:42 -07006940 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006941 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06006942
Jens Axboe05f3fb32019-12-09 11:22:50 -07006943 /*
6944 * Drop uring mutex before waiting for references to exit. If
6945 * another thread is currently inside io_uring_enter() it might
6946 * need to grab the uring_lock to make progress. If we hold it
6947 * here across the drain wait, then we can deadlock. It's safe
6948 * to drop the mutex here, since no new references will come in
6949 * after we've killed the percpu ref.
6950 */
6951 mutex_unlock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07006952 ret = wait_for_completion_interruptible(&ctx->completions[0]);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006953 mutex_lock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07006954 if (ret) {
6955 percpu_ref_resurrect(&ctx->refs);
6956 ret = -EINTR;
6957 goto out;
6958 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07006959 }
Jens Axboeedafcce2019-01-09 09:16:05 -07006960
6961 switch (opcode) {
6962 case IORING_REGISTER_BUFFERS:
6963 ret = io_sqe_buffer_register(ctx, arg, nr_args);
6964 break;
6965 case IORING_UNREGISTER_BUFFERS:
6966 ret = -EINVAL;
6967 if (arg || nr_args)
6968 break;
6969 ret = io_sqe_buffer_unregister(ctx);
6970 break;
Jens Axboe6b063142019-01-10 22:13:58 -07006971 case IORING_REGISTER_FILES:
6972 ret = io_sqe_files_register(ctx, arg, nr_args);
6973 break;
6974 case IORING_UNREGISTER_FILES:
6975 ret = -EINVAL;
6976 if (arg || nr_args)
6977 break;
6978 ret = io_sqe_files_unregister(ctx);
6979 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06006980 case IORING_REGISTER_FILES_UPDATE:
6981 ret = io_sqe_files_update(ctx, arg, nr_args);
6982 break;
Jens Axboe9b402842019-04-11 11:45:41 -06006983 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -07006984 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -06006985 ret = -EINVAL;
6986 if (nr_args != 1)
6987 break;
6988 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -07006989 if (ret)
6990 break;
6991 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
6992 ctx->eventfd_async = 1;
6993 else
6994 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -06006995 break;
6996 case IORING_UNREGISTER_EVENTFD:
6997 ret = -EINVAL;
6998 if (arg || nr_args)
6999 break;
7000 ret = io_eventfd_unregister(ctx);
7001 break;
Jens Axboe66f4af92020-01-16 15:36:52 -07007002 case IORING_REGISTER_PROBE:
7003 ret = -EINVAL;
7004 if (!arg || nr_args > 256)
7005 break;
7006 ret = io_probe(ctx, arg, nr_args);
7007 break;
Jens Axboe071698e2020-01-28 10:04:42 -07007008 case IORING_REGISTER_PERSONALITY:
7009 ret = -EINVAL;
7010 if (arg || nr_args)
7011 break;
7012 ret = io_register_personality(ctx);
7013 break;
7014 case IORING_UNREGISTER_PERSONALITY:
7015 ret = -EINVAL;
7016 if (arg)
7017 break;
7018 ret = io_unregister_personality(ctx, nr_args);
7019 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07007020 default:
7021 ret = -EINVAL;
7022 break;
7023 }
7024
Jens Axboe071698e2020-01-28 10:04:42 -07007025 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007026 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -07007027 percpu_ref_reinit(&ctx->refs);
Jens Axboec1503682020-01-08 08:26:07 -07007028out:
7029 reinit_completion(&ctx->completions[0]);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007030 }
Jens Axboeedafcce2019-01-09 09:16:05 -07007031 return ret;
7032}
7033
7034SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
7035 void __user *, arg, unsigned int, nr_args)
7036{
7037 struct io_ring_ctx *ctx;
7038 long ret = -EBADF;
7039 struct fd f;
7040
7041 f = fdget(fd);
7042 if (!f.file)
7043 return -EBADF;
7044
7045 ret = -EOPNOTSUPP;
7046 if (f.file->f_op != &io_uring_fops)
7047 goto out_fput;
7048
7049 ctx = f.file->private_data;
7050
7051 mutex_lock(&ctx->uring_lock);
7052 ret = __io_uring_register(ctx, opcode, arg, nr_args);
7053 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02007054 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
7055 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07007056out_fput:
7057 fdput(f);
7058 return ret;
7059}
7060
Jens Axboe2b188cc2019-01-07 10:46:33 -07007061static int __init io_uring_init(void)
7062{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01007063#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
7064 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
7065 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
7066} while (0)
7067
7068#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
7069 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
7070 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
7071 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
7072 BUILD_BUG_SQE_ELEM(1, __u8, flags);
7073 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
7074 BUILD_BUG_SQE_ELEM(4, __s32, fd);
7075 BUILD_BUG_SQE_ELEM(8, __u64, off);
7076 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
7077 BUILD_BUG_SQE_ELEM(16, __u64, addr);
7078 BUILD_BUG_SQE_ELEM(24, __u32, len);
7079 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
7080 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
7081 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
7082 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
7083 BUILD_BUG_SQE_ELEM(28, __u16, poll_events);
7084 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
7085 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
7086 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
7087 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
7088 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
7089 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
7090 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
7091 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
7092 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
7093 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
7094 BUILD_BUG_SQE_ELEM(42, __u16, personality);
7095
Jens Axboed3656342019-12-18 09:50:26 -07007096 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007097 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
7098 return 0;
7099};
7100__initcall(io_uring_init);