blob: 77f22c3da30f59108cb207aca8c78c3f23b6720d [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;
Jens Axboe69b3e542020-01-08 11:01:46 -0700207 int compat: 1;
208 int account_mem: 1;
209 int cq_overflow_flushed: 1;
210 int drain_next: 1;
Jens Axboef2842ab2020-01-08 11:04:00 -0700211 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 has_user;
557 bool in_async;
558 bool needs_fixed_file;
Jens Axboed625c6e2019-12-17 19:53:05 -0700559 u8 opcode;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700560
561 struct io_ring_ctx *ctx;
Jens Axboeeac406c2019-11-14 12:09:58 -0700562 union {
563 struct list_head list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700564 struct hlist_node hash_node;
Jens Axboeeac406c2019-11-14 12:09:58 -0700565 };
Jens Axboe9e645e112019-05-10 16:07:28 -0600566 struct list_head link_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700567 unsigned int flags;
Jens Axboec16361c2019-01-17 08:39:48 -0700568 refcount_t refs;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700569 u64 user_data;
Jens Axboe9e645e112019-05-10 16:07:28 -0600570 u32 result;
Jens Axboede0617e2019-04-06 21:51:27 -0600571 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700572
Jens Axboefcb323c2019-10-24 12:39:47 -0600573 struct list_head inflight_entry;
574
Jens Axboe561fb042019-10-24 07:25:42 -0600575 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700576};
577
578#define IO_PLUG_THRESHOLD 2
Jens Axboedef596e2019-01-09 08:59:42 -0700579#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700580
Jens Axboe9a56a232019-01-09 09:06:50 -0700581struct io_submit_state {
582 struct blk_plug plug;
583
584 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700585 * io_kiocb alloc cache
586 */
587 void *reqs[IO_IOPOLL_BATCH];
Pavel Begunkov6c8a3132020-02-01 03:58:00 +0300588 unsigned int free_reqs;
Jens Axboe2579f912019-01-09 09:10:43 -0700589
590 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700591 * File reference cache
592 */
593 struct file *file;
594 unsigned int fd;
595 unsigned int has_refs;
596 unsigned int used_refs;
597 unsigned int ios_left;
598};
599
Jens Axboed3656342019-12-18 09:50:26 -0700600struct io_op_def {
601 /* needs req->io allocated for deferral/async */
602 unsigned async_ctx : 1;
603 /* needs current->mm setup, does mm access */
604 unsigned needs_mm : 1;
605 /* needs req->file assigned */
606 unsigned needs_file : 1;
607 /* needs req->file assigned IFF fd is >= 0 */
608 unsigned fd_non_neg : 1;
609 /* hash wq insertion if file is a regular file */
610 unsigned hash_reg_file : 1;
611 /* unbound wq insertion if file is a non-regular file */
612 unsigned unbound_nonreg_file : 1;
Jens Axboe66f4af92020-01-16 15:36:52 -0700613 /* opcode is not supported by this kernel */
614 unsigned not_supported : 1;
Jens Axboef86cd202020-01-29 13:46:44 -0700615 /* needs file table */
616 unsigned file_table : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700617};
618
619static const struct io_op_def io_op_defs[] = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300620 [IORING_OP_NOP] = {},
621 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700622 .async_ctx = 1,
623 .needs_mm = 1,
624 .needs_file = 1,
625 .unbound_nonreg_file = 1,
626 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300627 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700628 .async_ctx = 1,
629 .needs_mm = 1,
630 .needs_file = 1,
631 .hash_reg_file = 1,
632 .unbound_nonreg_file = 1,
633 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300634 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700635 .needs_file = 1,
636 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300637 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700638 .needs_file = 1,
639 .unbound_nonreg_file = 1,
640 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300641 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700642 .needs_file = 1,
643 .hash_reg_file = 1,
644 .unbound_nonreg_file = 1,
645 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300646 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700647 .needs_file = 1,
648 .unbound_nonreg_file = 1,
649 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300650 [IORING_OP_POLL_REMOVE] = {},
651 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700652 .needs_file = 1,
653 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300654 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700655 .async_ctx = 1,
656 .needs_mm = 1,
657 .needs_file = 1,
658 .unbound_nonreg_file = 1,
659 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300660 [IORING_OP_RECVMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700661 .async_ctx = 1,
662 .needs_mm = 1,
663 .needs_file = 1,
664 .unbound_nonreg_file = 1,
665 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300666 [IORING_OP_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700667 .async_ctx = 1,
668 .needs_mm = 1,
669 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300670 [IORING_OP_TIMEOUT_REMOVE] = {},
671 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700672 .needs_mm = 1,
673 .needs_file = 1,
674 .unbound_nonreg_file = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700675 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700676 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300677 [IORING_OP_ASYNC_CANCEL] = {},
678 [IORING_OP_LINK_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700679 .async_ctx = 1,
680 .needs_mm = 1,
681 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300682 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700683 .async_ctx = 1,
684 .needs_mm = 1,
685 .needs_file = 1,
686 .unbound_nonreg_file = 1,
687 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300688 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700689 .needs_file = 1,
690 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300691 [IORING_OP_OPENAT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700692 .needs_file = 1,
693 .fd_non_neg = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700694 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700695 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300696 [IORING_OP_CLOSE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700697 .needs_file = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700698 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700699 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300700 [IORING_OP_FILES_UPDATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700701 .needs_mm = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700702 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700703 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300704 [IORING_OP_STATX] = {
Jens Axboed3656342019-12-18 09:50:26 -0700705 .needs_mm = 1,
706 .needs_file = 1,
707 .fd_non_neg = 1,
708 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300709 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700710 .needs_mm = 1,
711 .needs_file = 1,
712 .unbound_nonreg_file = 1,
713 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300714 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700715 .needs_mm = 1,
716 .needs_file = 1,
717 .unbound_nonreg_file = 1,
718 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300719 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -0700720 .needs_file = 1,
721 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300722 [IORING_OP_MADVISE] = {
Jens Axboec1ca7572019-12-25 22:18:28 -0700723 .needs_mm = 1,
724 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300725 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700726 .needs_mm = 1,
727 .needs_file = 1,
728 .unbound_nonreg_file = 1,
729 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300730 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700731 .needs_mm = 1,
732 .needs_file = 1,
733 .unbound_nonreg_file = 1,
734 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300735 [IORING_OP_OPENAT2] = {
Jens Axboecebdb982020-01-08 17:59:24 -0700736 .needs_file = 1,
737 .fd_non_neg = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700738 .file_table = 1,
Jens Axboecebdb982020-01-08 17:59:24 -0700739 },
Jens Axboe3e4827b2020-01-08 15:18:09 -0700740 [IORING_OP_EPOLL_CTL] = {
741 .unbound_nonreg_file = 1,
742 .file_table = 1,
743 },
Jens Axboed3656342019-12-18 09:50:26 -0700744};
745
Jens Axboe561fb042019-10-24 07:25:42 -0600746static void io_wq_submit_work(struct io_wq_work **workptr);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700747static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800748static void io_put_req(struct io_kiocb *req);
Jens Axboe978db572019-11-14 22:39:04 -0700749static void __io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700750static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
751static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -0700752static int __io_sqe_files_update(struct io_ring_ctx *ctx,
753 struct io_uring_files_update *ip,
754 unsigned nr_args);
Jens Axboef86cd202020-01-29 13:46:44 -0700755static int io_grab_files(struct io_kiocb *req);
Jens Axboe2faf8522020-02-04 19:54:55 -0700756static void io_ring_file_ref_flush(struct fixed_file_data *data);
Jens Axboede0617e2019-04-06 21:51:27 -0600757
Jens Axboe2b188cc2019-01-07 10:46:33 -0700758static struct kmem_cache *req_cachep;
759
760static const struct file_operations io_uring_fops;
761
762struct sock *io_uring_get_socket(struct file *file)
763{
764#if defined(CONFIG_UNIX)
765 if (file->f_op == &io_uring_fops) {
766 struct io_ring_ctx *ctx = file->private_data;
767
768 return ctx->ring_sock->sk;
769 }
770#endif
771 return NULL;
772}
773EXPORT_SYMBOL(io_uring_get_socket);
774
775static void io_ring_ctx_ref_free(struct percpu_ref *ref)
776{
777 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
778
Jens Axboe206aefd2019-11-07 18:27:42 -0700779 complete(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700780}
781
782static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
783{
784 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -0700785 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700786
787 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
788 if (!ctx)
789 return NULL;
790
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700791 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
792 if (!ctx->fallback_req)
793 goto err;
794
Jens Axboe206aefd2019-11-07 18:27:42 -0700795 ctx->completions = kmalloc(2 * sizeof(struct completion), GFP_KERNEL);
796 if (!ctx->completions)
797 goto err;
798
Jens Axboe78076bb2019-12-04 19:56:40 -0700799 /*
800 * Use 5 bits less than the max cq entries, that should give us around
801 * 32 entries per hash list if totally full and uniformly spread.
802 */
803 hash_bits = ilog2(p->cq_entries);
804 hash_bits -= 5;
805 if (hash_bits <= 0)
806 hash_bits = 1;
807 ctx->cancel_hash_bits = hash_bits;
808 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
809 GFP_KERNEL);
810 if (!ctx->cancel_hash)
811 goto err;
812 __hash_init(ctx->cancel_hash, 1U << hash_bits);
813
Roman Gushchin21482892019-05-07 10:01:48 -0700814 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -0700815 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
816 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700817
818 ctx->flags = p->flags;
819 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700820 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe206aefd2019-11-07 18:27:42 -0700821 init_completion(&ctx->completions[0]);
822 init_completion(&ctx->completions[1]);
Jens Axboe071698e2020-01-28 10:04:42 -0700823 idr_init(&ctx->personality_idr);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700824 mutex_init(&ctx->uring_lock);
825 init_waitqueue_head(&ctx->wait);
826 spin_lock_init(&ctx->completion_lock);
Jens Axboee94f1412019-12-19 12:06:02 -0700827 init_llist_head(&ctx->poll_llist);
Jens Axboedef596e2019-01-09 08:59:42 -0700828 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboede0617e2019-04-06 21:51:27 -0600829 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -0600830 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -0600831 init_waitqueue_head(&ctx->inflight_wait);
832 spin_lock_init(&ctx->inflight_lock);
833 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700834 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -0700835err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700836 if (ctx->fallback_req)
837 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe206aefd2019-11-07 18:27:42 -0700838 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -0700839 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -0700840 kfree(ctx);
841 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700842}
843
Bob Liu9d858b22019-11-13 18:06:25 +0800844static inline bool __req_need_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -0600845{
Jackie Liua197f662019-11-08 08:09:12 -0700846 struct io_ring_ctx *ctx = req->ctx;
847
Jens Axboe498ccd92019-10-25 10:04:25 -0600848 return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped
849 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboede0617e2019-04-06 21:51:27 -0600850}
851
Bob Liu9d858b22019-11-13 18:06:25 +0800852static inline bool req_need_defer(struct io_kiocb *req)
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600853{
Pavel Begunkov87987892020-01-18 01:22:30 +0300854 if (unlikely(req->flags & REQ_F_IO_DRAIN))
Bob Liu9d858b22019-11-13 18:06:25 +0800855 return __req_need_defer(req);
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600856
Bob Liu9d858b22019-11-13 18:06:25 +0800857 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600858}
859
860static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -0600861{
862 struct io_kiocb *req;
863
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600864 req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list);
Bob Liu9d858b22019-11-13 18:06:25 +0800865 if (req && !req_need_defer(req)) {
Jens Axboede0617e2019-04-06 21:51:27 -0600866 list_del_init(&req->list);
867 return req;
868 }
869
870 return NULL;
871}
872
Jens Axboe5262f562019-09-17 12:26:57 -0600873static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx)
874{
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600875 struct io_kiocb *req;
876
877 req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list);
Jens Axboe93bd25b2019-11-11 23:34:31 -0700878 if (req) {
879 if (req->flags & REQ_F_TIMEOUT_NOSEQ)
880 return NULL;
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800881 if (!__req_need_defer(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -0700882 list_del_init(&req->list);
883 return req;
884 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600885 }
886
887 return NULL;
Jens Axboe5262f562019-09-17 12:26:57 -0600888}
889
Jens Axboede0617e2019-04-06 21:51:27 -0600890static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700891{
Hristo Venev75b28af2019-08-26 17:23:46 +0000892 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700893
Pavel Begunkov07910152020-01-17 03:52:46 +0300894 /* order cqe stores with ring update */
895 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700896
Pavel Begunkov07910152020-01-17 03:52:46 +0300897 if (wq_has_sleeper(&ctx->cq_wait)) {
898 wake_up_interruptible(&ctx->cq_wait);
899 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700900 }
901}
902
Jens Axboecccf0ee2020-01-27 16:34:48 -0700903static inline void io_req_work_grab_env(struct io_kiocb *req,
904 const struct io_op_def *def)
Jens Axboe18d9be12019-09-10 09:13:05 -0600905{
Jens Axboecccf0ee2020-01-27 16:34:48 -0700906 if (!req->work.mm && def->needs_mm) {
907 mmgrab(current->mm);
908 req->work.mm = current->mm;
909 }
910 if (!req->work.creds)
911 req->work.creds = get_current_cred();
912}
913
914static inline void io_req_work_drop_env(struct io_kiocb *req)
915{
916 if (req->work.mm) {
917 mmdrop(req->work.mm);
918 req->work.mm = NULL;
919 }
920 if (req->work.creds) {
921 put_cred(req->work.creds);
922 req->work.creds = NULL;
923 }
Jens Axboe561fb042019-10-24 07:25:42 -0600924}
925
Jens Axboe94ae5e72019-11-14 19:39:52 -0700926static inline bool io_prep_async_work(struct io_kiocb *req,
927 struct io_kiocb **link)
Jens Axboe561fb042019-10-24 07:25:42 -0600928{
Jens Axboed3656342019-12-18 09:50:26 -0700929 const struct io_op_def *def = &io_op_defs[req->opcode];
Jens Axboe561fb042019-10-24 07:25:42 -0600930 bool do_hashed = false;
Jens Axboe54a91f32019-09-10 09:15:04 -0600931
Jens Axboed3656342019-12-18 09:50:26 -0700932 if (req->flags & REQ_F_ISREG) {
933 if (def->hash_reg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -0700934 do_hashed = true;
Jens Axboed3656342019-12-18 09:50:26 -0700935 } else {
936 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -0700937 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -0600938 }
Jens Axboecccf0ee2020-01-27 16:34:48 -0700939
940 io_req_work_grab_env(req, def);
Jens Axboe54a91f32019-09-10 09:15:04 -0600941
Jens Axboe94ae5e72019-11-14 19:39:52 -0700942 *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -0600943 return do_hashed;
944}
945
Jackie Liua197f662019-11-08 08:09:12 -0700946static inline void io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -0600947{
Jackie Liua197f662019-11-08 08:09:12 -0700948 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe94ae5e72019-11-14 19:39:52 -0700949 struct io_kiocb *link;
950 bool do_hashed;
951
952 do_hashed = io_prep_async_work(req, &link);
Jens Axboe561fb042019-10-24 07:25:42 -0600953
954 trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work,
955 req->flags);
956 if (!do_hashed) {
957 io_wq_enqueue(ctx->io_wq, &req->work);
958 } else {
959 io_wq_enqueue_hashed(ctx->io_wq, &req->work,
960 file_inode(req->file));
961 }
Jens Axboe94ae5e72019-11-14 19:39:52 -0700962
963 if (link)
964 io_queue_linked_timeout(link);
Jens Axboe18d9be12019-09-10 09:13:05 -0600965}
966
Jens Axboe5262f562019-09-17 12:26:57 -0600967static void io_kill_timeout(struct io_kiocb *req)
968{
969 int ret;
970
Jens Axboe2d283902019-12-04 11:08:05 -0700971 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe5262f562019-09-17 12:26:57 -0600972 if (ret != -1) {
973 atomic_inc(&req->ctx->cq_timeouts);
Jens Axboe842f9612019-10-29 12:34:10 -0600974 list_del_init(&req->list);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700975 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800976 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -0600977 }
978}
979
980static void io_kill_timeouts(struct io_ring_ctx *ctx)
981{
982 struct io_kiocb *req, *tmp;
983
984 spin_lock_irq(&ctx->completion_lock);
985 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
986 io_kill_timeout(req);
987 spin_unlock_irq(&ctx->completion_lock);
988}
989
Jens Axboede0617e2019-04-06 21:51:27 -0600990static void io_commit_cqring(struct io_ring_ctx *ctx)
991{
992 struct io_kiocb *req;
993
Jens Axboe5262f562019-09-17 12:26:57 -0600994 while ((req = io_get_timeout_req(ctx)) != NULL)
995 io_kill_timeout(req);
996
Jens Axboede0617e2019-04-06 21:51:27 -0600997 __io_commit_cqring(ctx);
998
Pavel Begunkov87987892020-01-18 01:22:30 +0300999 while ((req = io_get_deferred_req(ctx)) != NULL)
Jackie Liua197f662019-11-08 08:09:12 -07001000 io_queue_async_work(req);
Jens Axboede0617e2019-04-06 21:51:27 -06001001}
1002
Jens Axboe2b188cc2019-01-07 10:46:33 -07001003static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
1004{
Hristo Venev75b28af2019-08-26 17:23:46 +00001005 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001006 unsigned tail;
1007
1008 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +02001009 /*
1010 * writes to the cq entry need to come after reading head; the
1011 * control dependency is enough as we're using WRITE_ONCE to
1012 * fill the cq entry
1013 */
Hristo Venev75b28af2019-08-26 17:23:46 +00001014 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001015 return NULL;
1016
1017 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +00001018 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001019}
1020
Jens Axboef2842ab2020-01-08 11:04:00 -07001021static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1022{
Jens Axboef0b493e2020-02-01 21:30:11 -07001023 if (!ctx->cq_ev_fd)
1024 return false;
Jens Axboef2842ab2020-01-08 11:04:00 -07001025 if (!ctx->eventfd_async)
1026 return true;
1027 return io_wq_current_is_worker() || in_interrupt();
1028}
1029
Jens Axboef0b493e2020-02-01 21:30:11 -07001030static void __io_cqring_ev_posted(struct io_ring_ctx *ctx, bool trigger_ev)
Jens Axboe8c838782019-03-12 15:48:16 -06001031{
1032 if (waitqueue_active(&ctx->wait))
1033 wake_up(&ctx->wait);
1034 if (waitqueue_active(&ctx->sqo_wait))
1035 wake_up(&ctx->sqo_wait);
Jens Axboef0b493e2020-02-01 21:30:11 -07001036 if (trigger_ev)
Jens Axboe9b402842019-04-11 11:45:41 -06001037 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -06001038}
1039
Jens Axboef0b493e2020-02-01 21:30:11 -07001040static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
1041{
1042 __io_cqring_ev_posted(ctx, io_should_trigger_evfd(ctx));
1043}
1044
Jens Axboec4a2ed72019-11-21 21:01:26 -07001045/* Returns true if there are no backlogged entries after the flush */
1046static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001047{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001048 struct io_rings *rings = ctx->rings;
1049 struct io_uring_cqe *cqe;
1050 struct io_kiocb *req;
1051 unsigned long flags;
1052 LIST_HEAD(list);
1053
1054 if (!force) {
1055 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001056 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001057 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
1058 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001059 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001060 }
1061
1062 spin_lock_irqsave(&ctx->completion_lock, flags);
1063
1064 /* if force is set, the ring is going away. always drop after that */
1065 if (force)
Jens Axboe69b3e542020-01-08 11:01:46 -07001066 ctx->cq_overflow_flushed = 1;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001067
Jens Axboec4a2ed72019-11-21 21:01:26 -07001068 cqe = NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001069 while (!list_empty(&ctx->cq_overflow_list)) {
1070 cqe = io_get_cqring(ctx);
1071 if (!cqe && !force)
1072 break;
1073
1074 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
1075 list);
1076 list_move(&req->list, &list);
1077 if (cqe) {
1078 WRITE_ONCE(cqe->user_data, req->user_data);
1079 WRITE_ONCE(cqe->res, req->result);
1080 WRITE_ONCE(cqe->flags, 0);
1081 } else {
1082 WRITE_ONCE(ctx->rings->cq_overflow,
1083 atomic_inc_return(&ctx->cached_cq_overflow));
1084 }
1085 }
1086
1087 io_commit_cqring(ctx);
Jens Axboead3eb2c2019-12-18 17:12:20 -07001088 if (cqe) {
1089 clear_bit(0, &ctx->sq_check_overflow);
1090 clear_bit(0, &ctx->cq_check_overflow);
1091 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001092 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1093 io_cqring_ev_posted(ctx);
1094
1095 while (!list_empty(&list)) {
1096 req = list_first_entry(&list, struct io_kiocb, list);
1097 list_del(&req->list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001098 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001099 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001100
1101 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001102}
1103
Jens Axboe78e19bb2019-11-06 15:21:34 -07001104static void io_cqring_fill_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001105{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001106 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001107 struct io_uring_cqe *cqe;
1108
Jens Axboe78e19bb2019-11-06 15:21:34 -07001109 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001110
Jens Axboe2b188cc2019-01-07 10:46:33 -07001111 /*
1112 * If we can't get a cq entry, userspace overflowed the
1113 * submission (by quite a lot). Increment the overflow count in
1114 * the ring.
1115 */
1116 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001117 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001118 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001119 WRITE_ONCE(cqe->res, res);
1120 WRITE_ONCE(cqe->flags, 0);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001121 } else if (ctx->cq_overflow_flushed) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001122 WRITE_ONCE(ctx->rings->cq_overflow,
1123 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001124 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001125 if (list_empty(&ctx->cq_overflow_list)) {
1126 set_bit(0, &ctx->sq_check_overflow);
1127 set_bit(0, &ctx->cq_check_overflow);
1128 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001129 refcount_inc(&req->refs);
1130 req->result = res;
1131 list_add_tail(&req->list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001132 }
1133}
1134
Jens Axboe78e19bb2019-11-06 15:21:34 -07001135static void io_cqring_add_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001136{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001137 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001138 unsigned long flags;
1139
1140 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001141 io_cqring_fill_event(req, res);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001142 io_commit_cqring(ctx);
1143 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1144
Jens Axboe8c838782019-03-12 15:48:16 -06001145 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001146}
1147
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001148static inline bool io_is_fallback_req(struct io_kiocb *req)
1149{
1150 return req == (struct io_kiocb *)
1151 ((unsigned long) req->ctx->fallback_req & ~1UL);
1152}
1153
1154static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
1155{
1156 struct io_kiocb *req;
1157
1158 req = ctx->fallback_req;
1159 if (!test_and_set_bit_lock(0, (unsigned long *) ctx->fallback_req))
1160 return req;
1161
1162 return NULL;
1163}
1164
Jens Axboe2579f912019-01-09 09:10:43 -07001165static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
1166 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001167{
Jens Axboefd6fab22019-03-14 16:30:06 -06001168 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001169 struct io_kiocb *req;
1170
Jens Axboe2579f912019-01-09 09:10:43 -07001171 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -06001172 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -07001173 if (unlikely(!req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001174 goto fallback;
Jens Axboe2579f912019-01-09 09:10:43 -07001175 } else if (!state->free_reqs) {
1176 size_t sz;
1177 int ret;
1178
1179 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -06001180 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
1181
1182 /*
1183 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1184 * retry single alloc to be on the safe side.
1185 */
1186 if (unlikely(ret <= 0)) {
1187 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1188 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001189 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -06001190 ret = 1;
1191 }
Jens Axboe2579f912019-01-09 09:10:43 -07001192 state->free_reqs = ret - 1;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001193 req = state->reqs[ret - 1];
Jens Axboe2579f912019-01-09 09:10:43 -07001194 } else {
Jens Axboe2579f912019-01-09 09:10:43 -07001195 state->free_reqs--;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001196 req = state->reqs[state->free_reqs];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001197 }
1198
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001199got_it:
Jens Axboe1a6b74f2019-12-02 10:33:15 -07001200 req->io = NULL;
Jens Axboe60c112b2019-06-21 10:20:18 -06001201 req->file = NULL;
Jens Axboe2579f912019-01-09 09:10:43 -07001202 req->ctx = ctx;
1203 req->flags = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06001204 /* one is dropped after submission, the other at completion */
1205 refcount_set(&req->refs, 2);
Jens Axboe9e645e112019-05-10 16:07:28 -06001206 req->result = 0;
Jens Axboe561fb042019-10-24 07:25:42 -06001207 INIT_IO_WORK(&req->work, io_wq_submit_work);
Jens Axboe2579f912019-01-09 09:10:43 -07001208 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001209fallback:
1210 req = io_get_fallback_req(ctx);
1211 if (req)
1212 goto got_it;
Pavel Begunkov6805b322019-10-08 02:18:42 +03001213 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001214 return NULL;
1215}
1216
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001217static void __io_req_do_free(struct io_kiocb *req)
Jens Axboedef596e2019-01-09 08:59:42 -07001218{
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001219 if (likely(!io_is_fallback_req(req)))
1220 kmem_cache_free(req_cachep, req);
1221 else
1222 clear_bit_unlock(0, (unsigned long *) req->ctx->fallback_req);
1223}
1224
Jens Axboec6ca97b302019-12-28 12:11:08 -07001225static void __io_req_aux_free(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001226{
Jens Axboefcb323c2019-10-24 12:39:47 -06001227 struct io_ring_ctx *ctx = req->ctx;
1228
YueHaibing96fd84d2020-01-07 22:22:44 +08001229 kfree(req->io);
Jens Axboe05f3fb32019-12-09 11:22:50 -07001230 if (req->file) {
1231 if (req->flags & REQ_F_FIXED_FILE)
1232 percpu_ref_put(&ctx->file_data->refs);
1233 else
1234 fput(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001235 }
Jens Axboecccf0ee2020-01-27 16:34:48 -07001236
1237 io_req_work_drop_env(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001238}
1239
1240static void __io_free_req(struct io_kiocb *req)
1241{
Jens Axboec6ca97b302019-12-28 12:11:08 -07001242 __io_req_aux_free(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001243
Jens Axboefcb323c2019-10-24 12:39:47 -06001244 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001245 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06001246 unsigned long flags;
1247
1248 spin_lock_irqsave(&ctx->inflight_lock, flags);
1249 list_del(&req->inflight_entry);
1250 if (waitqueue_active(&ctx->inflight_wait))
1251 wake_up(&ctx->inflight_wait);
1252 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1253 }
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001254
1255 percpu_ref_put(&req->ctx->refs);
1256 __io_req_do_free(req);
Jens Axboee65ef562019-03-12 10:16:44 -06001257}
1258
Jens Axboec6ca97b302019-12-28 12:11:08 -07001259struct req_batch {
1260 void *reqs[IO_IOPOLL_BATCH];
1261 int to_free;
1262 int need_iter;
1263};
1264
1265static void io_free_req_many(struct io_ring_ctx *ctx, struct req_batch *rb)
1266{
Jens Axboe10fef4b2020-01-09 07:52:28 -07001267 int fixed_refs = rb->to_free;
1268
Jens Axboec6ca97b302019-12-28 12:11:08 -07001269 if (!rb->to_free)
1270 return;
1271 if (rb->need_iter) {
1272 int i, inflight = 0;
1273 unsigned long flags;
1274
Jens Axboe10fef4b2020-01-09 07:52:28 -07001275 fixed_refs = 0;
Jens Axboec6ca97b302019-12-28 12:11:08 -07001276 for (i = 0; i < rb->to_free; i++) {
1277 struct io_kiocb *req = rb->reqs[i];
1278
Jens Axboe10fef4b2020-01-09 07:52:28 -07001279 if (req->flags & REQ_F_FIXED_FILE) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001280 req->file = NULL;
Jens Axboe10fef4b2020-01-09 07:52:28 -07001281 fixed_refs++;
1282 }
Jens Axboec6ca97b302019-12-28 12:11:08 -07001283 if (req->flags & REQ_F_INFLIGHT)
1284 inflight++;
Jens Axboec6ca97b302019-12-28 12:11:08 -07001285 __io_req_aux_free(req);
1286 }
1287 if (!inflight)
1288 goto do_free;
1289
1290 spin_lock_irqsave(&ctx->inflight_lock, flags);
1291 for (i = 0; i < rb->to_free; i++) {
1292 struct io_kiocb *req = rb->reqs[i];
1293
Jens Axboe10fef4b2020-01-09 07:52:28 -07001294 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001295 list_del(&req->inflight_entry);
1296 if (!--inflight)
1297 break;
1298 }
1299 }
1300 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1301
1302 if (waitqueue_active(&ctx->inflight_wait))
1303 wake_up(&ctx->inflight_wait);
1304 }
1305do_free:
1306 kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs);
Jens Axboe10fef4b2020-01-09 07:52:28 -07001307 if (fixed_refs)
1308 percpu_ref_put_many(&ctx->file_data->refs, fixed_refs);
Jens Axboec6ca97b302019-12-28 12:11:08 -07001309 percpu_ref_put_many(&ctx->refs, rb->to_free);
Jens Axboec6ca97b302019-12-28 12:11:08 -07001310 rb->to_free = rb->need_iter = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06001311}
1312
Jackie Liua197f662019-11-08 08:09:12 -07001313static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001314{
Jackie Liua197f662019-11-08 08:09:12 -07001315 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001316 int ret;
1317
Jens Axboe2d283902019-12-04 11:08:05 -07001318 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe2665abf2019-11-05 12:40:47 -07001319 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001320 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -07001321 io_commit_cqring(ctx);
1322 req->flags &= ~REQ_F_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001323 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001324 return true;
1325 }
1326
1327 return false;
1328}
1329
Jens Axboeba816ad2019-09-28 11:36:45 -06001330static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -06001331{
Jens Axboe2665abf2019-11-05 12:40:47 -07001332 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001333 bool wake_ev = false;
Jens Axboe9e645e112019-05-10 16:07:28 -06001334
Jens Axboe4d7dd462019-11-20 13:03:52 -07001335 /* Already got next link */
1336 if (req->flags & REQ_F_LINK_NEXT)
1337 return;
1338
Jens Axboe9e645e112019-05-10 16:07:28 -06001339 /*
1340 * The list should never be empty when we are called here. But could
1341 * potentially happen if the chain is messed up, check to be on the
1342 * safe side.
1343 */
Pavel Begunkov44932332019-12-05 16:16:35 +03001344 while (!list_empty(&req->link_list)) {
1345 struct io_kiocb *nxt = list_first_entry(&req->link_list,
1346 struct io_kiocb, link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001347
Pavel Begunkov44932332019-12-05 16:16:35 +03001348 if (unlikely((req->flags & REQ_F_LINK_TIMEOUT) &&
1349 (nxt->flags & REQ_F_TIMEOUT))) {
1350 list_del_init(&nxt->link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001351 wake_ev |= io_link_cancel_timeout(nxt);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001352 req->flags &= ~REQ_F_LINK_TIMEOUT;
1353 continue;
1354 }
Jens Axboe9e645e112019-05-10 16:07:28 -06001355
Pavel Begunkov44932332019-12-05 16:16:35 +03001356 list_del_init(&req->link_list);
1357 if (!list_empty(&nxt->link_list))
1358 nxt->flags |= REQ_F_LINK;
Pavel Begunkovb18fdf72019-11-21 23:21:02 +03001359 *nxtptr = nxt;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001360 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06001361 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001362
Jens Axboe4d7dd462019-11-20 13:03:52 -07001363 req->flags |= REQ_F_LINK_NEXT;
Jens Axboe2665abf2019-11-05 12:40:47 -07001364 if (wake_ev)
1365 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001366}
1367
1368/*
1369 * Called if REQ_F_LINK is set, and we fail the head request
1370 */
1371static void io_fail_links(struct io_kiocb *req)
1372{
Jens Axboe2665abf2019-11-05 12:40:47 -07001373 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001374 unsigned long flags;
1375
1376 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06001377
1378 while (!list_empty(&req->link_list)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03001379 struct io_kiocb *link = list_first_entry(&req->link_list,
1380 struct io_kiocb, link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06001381
Pavel Begunkov44932332019-12-05 16:16:35 +03001382 list_del_init(&link->link_list);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001383 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001384
1385 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
Jens Axboed625c6e2019-12-17 19:53:05 -07001386 link->opcode == IORING_OP_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -07001387 io_link_cancel_timeout(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001388 } else {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001389 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe978db572019-11-14 22:39:04 -07001390 __io_double_put_req(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001391 }
Jens Axboe5d960722019-11-19 15:31:28 -07001392 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -06001393 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001394
1395 io_commit_cqring(ctx);
1396 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1397 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001398}
1399
Jens Axboe4d7dd462019-11-20 13:03:52 -07001400static void io_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -06001401{
Jens Axboe4d7dd462019-11-20 13:03:52 -07001402 if (likely(!(req->flags & REQ_F_LINK)))
Jens Axboe2665abf2019-11-05 12:40:47 -07001403 return;
Jens Axboe2665abf2019-11-05 12:40:47 -07001404
Jens Axboe9e645e112019-05-10 16:07:28 -06001405 /*
1406 * If LINK is set, we have dependent requests in this chain. If we
1407 * didn't fail this request, queue the first one up, moving any other
1408 * dependencies to the next request. In case of failure, fail the rest
1409 * of the chain.
1410 */
Jens Axboe2665abf2019-11-05 12:40:47 -07001411 if (req->flags & REQ_F_FAIL_LINK) {
1412 io_fail_links(req);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001413 } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) ==
1414 REQ_F_LINK_TIMEOUT) {
Jens Axboe2665abf2019-11-05 12:40:47 -07001415 struct io_ring_ctx *ctx = req->ctx;
1416 unsigned long flags;
1417
1418 /*
1419 * If this is a timeout link, we could be racing with the
1420 * timeout timer. Grab the completion lock for this case to
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001421 * protect against that.
Jens Axboe2665abf2019-11-05 12:40:47 -07001422 */
1423 spin_lock_irqsave(&ctx->completion_lock, flags);
1424 io_req_link_next(req, nxt);
1425 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1426 } else {
1427 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -06001428 }
Jens Axboe4d7dd462019-11-20 13:03:52 -07001429}
Jens Axboe9e645e112019-05-10 16:07:28 -06001430
Jackie Liuc69f8db2019-11-09 11:00:08 +08001431static void io_free_req(struct io_kiocb *req)
1432{
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001433 struct io_kiocb *nxt = NULL;
1434
1435 io_req_find_next(req, &nxt);
Pavel Begunkov70cf9f32019-11-21 23:21:00 +03001436 __io_free_req(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001437
1438 if (nxt)
1439 io_queue_async_work(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08001440}
1441
Jens Axboeba816ad2019-09-28 11:36:45 -06001442/*
1443 * Drop reference to request, return next in chain (if there is one) if this
1444 * was the last reference to this request.
1445 */
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001446__attribute__((nonnull))
Jackie Liuec9c02a2019-11-08 23:50:36 +08001447static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboee65ef562019-03-12 10:16:44 -06001448{
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001449 io_req_find_next(req, nxtptr);
Jens Axboe4d7dd462019-11-20 13:03:52 -07001450
Jens Axboee65ef562019-03-12 10:16:44 -06001451 if (refcount_dec_and_test(&req->refs))
Jens Axboe4d7dd462019-11-20 13:03:52 -07001452 __io_free_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001453}
1454
Jens Axboe2b188cc2019-01-07 10:46:33 -07001455static void io_put_req(struct io_kiocb *req)
1456{
Jens Axboedef596e2019-01-09 08:59:42 -07001457 if (refcount_dec_and_test(&req->refs))
1458 io_free_req(req);
1459}
1460
Jens Axboe978db572019-11-14 22:39:04 -07001461/*
1462 * Must only be used if we don't need to care about links, usually from
1463 * within the completion handling itself.
1464 */
1465static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06001466{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001467 /* drop both submit and complete references */
1468 if (refcount_sub_and_test(2, &req->refs))
1469 __io_free_req(req);
1470}
1471
Jens Axboe978db572019-11-14 22:39:04 -07001472static void io_double_put_req(struct io_kiocb *req)
1473{
1474 /* drop both submit and complete references */
1475 if (refcount_sub_and_test(2, &req->refs))
1476 io_free_req(req);
1477}
1478
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001479static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06001480{
Jens Axboe84f97dc2019-11-06 11:27:53 -07001481 struct io_rings *rings = ctx->rings;
1482
Jens Axboead3eb2c2019-12-18 17:12:20 -07001483 if (test_bit(0, &ctx->cq_check_overflow)) {
1484 /*
1485 * noflush == true is from the waitqueue handler, just ensure
1486 * we wake up the task, and the next invocation will flush the
1487 * entries. We cannot safely to it from here.
1488 */
1489 if (noflush && !list_empty(&ctx->cq_overflow_list))
1490 return -1U;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001491
Jens Axboead3eb2c2019-12-18 17:12:20 -07001492 io_cqring_overflow_flush(ctx, false);
1493 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001494
Jens Axboea3a0e432019-08-20 11:03:11 -06001495 /* See comment at the top of this file */
1496 smp_rmb();
Jens Axboead3eb2c2019-12-18 17:12:20 -07001497 return ctx->cached_cq_tail - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06001498}
1499
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03001500static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1501{
1502 struct io_rings *rings = ctx->rings;
1503
1504 /* make sure SQ entry isn't read before tail */
1505 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1506}
1507
Jens Axboe8237e042019-12-28 10:48:22 -07001508static inline bool io_req_multi_free(struct req_batch *rb, struct io_kiocb *req)
Jens Axboee94f1412019-12-19 12:06:02 -07001509{
Jens Axboec6ca97b302019-12-28 12:11:08 -07001510 if ((req->flags & REQ_F_LINK) || io_is_fallback_req(req))
1511 return false;
Jens Axboee94f1412019-12-19 12:06:02 -07001512
Jens Axboec6ca97b302019-12-28 12:11:08 -07001513 if (!(req->flags & REQ_F_FIXED_FILE) || req->io)
1514 rb->need_iter++;
1515
1516 rb->reqs[rb->to_free++] = req;
1517 if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
1518 io_free_req_many(req->ctx, rb);
1519 return true;
Jens Axboee94f1412019-12-19 12:06:02 -07001520}
1521
Jens Axboedef596e2019-01-09 08:59:42 -07001522/*
1523 * Find and free completed poll iocbs
1524 */
1525static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
1526 struct list_head *done)
1527{
Jens Axboe8237e042019-12-28 10:48:22 -07001528 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07001529 struct io_kiocb *req;
Jens Axboedef596e2019-01-09 08:59:42 -07001530
Jens Axboec6ca97b302019-12-28 12:11:08 -07001531 rb.to_free = rb.need_iter = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001532 while (!list_empty(done)) {
1533 req = list_first_entry(done, struct io_kiocb, list);
1534 list_del(&req->list);
1535
Jens Axboe78e19bb2019-11-06 15:21:34 -07001536 io_cqring_fill_event(req, req->result);
Jens Axboedef596e2019-01-09 08:59:42 -07001537 (*nr_events)++;
1538
Jens Axboe8237e042019-12-28 10:48:22 -07001539 if (refcount_dec_and_test(&req->refs) &&
1540 !io_req_multi_free(&rb, req))
1541 io_free_req(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001542 }
Jens Axboedef596e2019-01-09 08:59:42 -07001543
Jens Axboe09bb8392019-03-13 12:39:28 -06001544 io_commit_cqring(ctx);
Jens Axboe8237e042019-12-28 10:48:22 -07001545 io_free_req_many(ctx, &rb);
Jens Axboedef596e2019-01-09 08:59:42 -07001546}
1547
1548static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1549 long min)
1550{
1551 struct io_kiocb *req, *tmp;
1552 LIST_HEAD(done);
1553 bool spin;
1554 int ret;
1555
1556 /*
1557 * Only spin for completions if we don't have multiple devices hanging
1558 * off our complete list, and we're under the requested amount.
1559 */
1560 spin = !ctx->poll_multi_file && *nr_events < min;
1561
1562 ret = 0;
1563 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
Jens Axboe9adbd452019-12-20 08:45:55 -07001564 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07001565
1566 /*
1567 * Move completed entries to our local list. If we find a
1568 * request that requires polling, break out and complete
1569 * the done list first, if we have entries there.
1570 */
1571 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
1572 list_move_tail(&req->list, &done);
1573 continue;
1574 }
1575 if (!list_empty(&done))
1576 break;
1577
1578 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1579 if (ret < 0)
1580 break;
1581
1582 if (ret && spin)
1583 spin = false;
1584 ret = 0;
1585 }
1586
1587 if (!list_empty(&done))
1588 io_iopoll_complete(ctx, nr_events, &done);
1589
1590 return ret;
1591}
1592
1593/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08001594 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07001595 * non-spinning poll check - we'll still enter the driver poll loop, but only
1596 * as a non-spinning completion check.
1597 */
1598static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
1599 long min)
1600{
Jens Axboe08f54392019-08-21 22:19:11 -06001601 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07001602 int ret;
1603
1604 ret = io_do_iopoll(ctx, nr_events, min);
1605 if (ret < 0)
1606 return ret;
1607 if (!min || *nr_events >= min)
1608 return 0;
1609 }
1610
1611 return 1;
1612}
1613
1614/*
1615 * We can't just wait for polled events to come to us, we have to actively
1616 * find and complete them.
1617 */
1618static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
1619{
1620 if (!(ctx->flags & IORING_SETUP_IOPOLL))
1621 return;
1622
1623 mutex_lock(&ctx->uring_lock);
1624 while (!list_empty(&ctx->poll_list)) {
1625 unsigned int nr_events = 0;
1626
1627 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -06001628
1629 /*
1630 * Ensure we allow local-to-the-cpu processing to take place,
1631 * in this case we need to ensure that we reap all events.
1632 */
1633 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -07001634 }
1635 mutex_unlock(&ctx->uring_lock);
1636}
1637
Jens Axboe2b2ed972019-10-25 10:06:15 -06001638static int __io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1639 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07001640{
Jens Axboe2b2ed972019-10-25 10:06:15 -06001641 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001642
1643 do {
1644 int tmin = 0;
1645
Jens Axboe500f9fb2019-08-19 12:15:59 -06001646 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06001647 * Don't enter poll loop if we already have events pending.
1648 * If we do, we can potentially be spinning for commands that
1649 * already triggered a CQE (eg in error).
1650 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001651 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06001652 break;
1653
1654 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06001655 * If a submit got punted to a workqueue, we can have the
1656 * application entering polling for a command before it gets
1657 * issued. That app will hold the uring_lock for the duration
1658 * of the poll right here, so we need to take a breather every
1659 * now and then to ensure that the issue has a chance to add
1660 * the poll to the issued list. Otherwise we can spin here
1661 * forever, while the workqueue is stuck trying to acquire the
1662 * very same mutex.
1663 */
1664 if (!(++iters & 7)) {
1665 mutex_unlock(&ctx->uring_lock);
1666 mutex_lock(&ctx->uring_lock);
1667 }
1668
Jens Axboedef596e2019-01-09 08:59:42 -07001669 if (*nr_events < min)
1670 tmin = min - *nr_events;
1671
1672 ret = io_iopoll_getevents(ctx, nr_events, tmin);
1673 if (ret <= 0)
1674 break;
1675 ret = 0;
1676 } while (min && !*nr_events && !need_resched());
1677
Jens Axboe2b2ed972019-10-25 10:06:15 -06001678 return ret;
1679}
1680
1681static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1682 long min)
1683{
1684 int ret;
1685
1686 /*
1687 * We disallow the app entering submit/complete with polling, but we
1688 * still need to lock the ring to prevent racing with polled issue
1689 * that got punted to a workqueue.
1690 */
1691 mutex_lock(&ctx->uring_lock);
1692 ret = __io_iopoll_check(ctx, nr_events, min);
Jens Axboe500f9fb2019-08-19 12:15:59 -06001693 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001694 return ret;
1695}
1696
Jens Axboe491381ce2019-10-17 09:20:46 -06001697static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001698{
Jens Axboe491381ce2019-10-17 09:20:46 -06001699 /*
1700 * Tell lockdep we inherited freeze protection from submission
1701 * thread.
1702 */
1703 if (req->flags & REQ_F_ISREG) {
1704 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001705
Jens Axboe491381ce2019-10-17 09:20:46 -06001706 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001707 }
Jens Axboe491381ce2019-10-17 09:20:46 -06001708 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001709}
1710
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001711static inline void req_set_fail_links(struct io_kiocb *req)
1712{
1713 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1714 req->flags |= REQ_F_FAIL_LINK;
1715}
1716
Jens Axboeba816ad2019-09-28 11:36:45 -06001717static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001718{
Jens Axboe9adbd452019-12-20 08:45:55 -07001719 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001720
Jens Axboe491381ce2019-10-17 09:20:46 -06001721 if (kiocb->ki_flags & IOCB_WRITE)
1722 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001723
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001724 if (res != req->result)
1725 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001726 io_cqring_add_event(req, res);
Jens Axboeba816ad2019-09-28 11:36:45 -06001727}
1728
1729static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1730{
Jens Axboe9adbd452019-12-20 08:45:55 -07001731 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06001732
1733 io_complete_rw_common(kiocb, res);
Jens Axboee65ef562019-03-12 10:16:44 -06001734 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001735}
1736
Jens Axboeba816ad2019-09-28 11:36:45 -06001737static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res)
1738{
Jens Axboe9adbd452019-12-20 08:45:55 -07001739 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001740 struct io_kiocb *nxt = NULL;
Jens Axboeba816ad2019-09-28 11:36:45 -06001741
1742 io_complete_rw_common(kiocb, res);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001743 io_put_req_find_next(req, &nxt);
1744
1745 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001746}
1747
Jens Axboedef596e2019-01-09 08:59:42 -07001748static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1749{
Jens Axboe9adbd452019-12-20 08:45:55 -07001750 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07001751
Jens Axboe491381ce2019-10-17 09:20:46 -06001752 if (kiocb->ki_flags & IOCB_WRITE)
1753 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001754
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001755 if (res != req->result)
1756 req_set_fail_links(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06001757 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -07001758 if (res != -EAGAIN)
1759 req->flags |= REQ_F_IOPOLL_COMPLETED;
1760}
1761
1762/*
1763 * After the iocb has been issued, it's safe to be found on the poll list.
1764 * Adding the kiocb to the list AFTER submission ensures that we don't
1765 * find it from a io_iopoll_getevents() thread before the issuer is done
1766 * accessing the kiocb cookie.
1767 */
1768static void io_iopoll_req_issued(struct io_kiocb *req)
1769{
1770 struct io_ring_ctx *ctx = req->ctx;
1771
1772 /*
1773 * Track whether we have multiple files in our lists. This will impact
1774 * how we do polling eventually, not spinning if we're on potentially
1775 * different devices.
1776 */
1777 if (list_empty(&ctx->poll_list)) {
1778 ctx->poll_multi_file = false;
1779 } else if (!ctx->poll_multi_file) {
1780 struct io_kiocb *list_req;
1781
1782 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1783 list);
Jens Axboe9adbd452019-12-20 08:45:55 -07001784 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07001785 ctx->poll_multi_file = true;
1786 }
1787
1788 /*
1789 * For fast devices, IO may have already completed. If it has, add
1790 * it to the front so we find it first.
1791 */
1792 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1793 list_add(&req->list, &ctx->poll_list);
1794 else
1795 list_add_tail(&req->list, &ctx->poll_list);
1796}
1797
Jens Axboe3d6770f2019-04-13 11:50:54 -06001798static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07001799{
Jens Axboe3d6770f2019-04-13 11:50:54 -06001800 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -07001801 int diff = state->has_refs - state->used_refs;
1802
1803 if (diff)
1804 fput_many(state->file, diff);
1805 state->file = NULL;
1806 }
1807}
1808
1809/*
1810 * Get as many references to a file as we have IOs left in this submission,
1811 * assuming most submissions are for one file, or at least that each file
1812 * has more than one submission.
1813 */
1814static struct file *io_file_get(struct io_submit_state *state, int fd)
1815{
1816 if (!state)
1817 return fget(fd);
1818
1819 if (state->file) {
1820 if (state->fd == fd) {
1821 state->used_refs++;
1822 state->ios_left--;
1823 return state->file;
1824 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06001825 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07001826 }
1827 state->file = fget_many(fd, state->ios_left);
1828 if (!state->file)
1829 return NULL;
1830
1831 state->fd = fd;
1832 state->has_refs = state->ios_left;
1833 state->used_refs = 1;
1834 state->ios_left--;
1835 return state->file;
1836}
1837
Jens Axboe2b188cc2019-01-07 10:46:33 -07001838/*
1839 * If we tracked the file through the SCM inflight mechanism, we could support
1840 * any file. For now, just ensure that anything potentially problematic is done
1841 * inline.
1842 */
1843static bool io_file_supports_async(struct file *file)
1844{
1845 umode_t mode = file_inode(file)->i_mode;
1846
Jens Axboe10d59342019-12-09 20:16:22 -07001847 if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07001848 return true;
1849 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
1850 return true;
1851
1852 return false;
1853}
1854
Jens Axboe3529d8c2019-12-19 18:24:38 -07001855static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1856 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001857{
Jens Axboedef596e2019-01-09 08:59:42 -07001858 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07001859 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06001860 unsigned ioprio;
1861 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001862
Jens Axboe491381ce2019-10-17 09:20:46 -06001863 if (S_ISREG(file_inode(req->file)->i_mode))
1864 req->flags |= REQ_F_ISREG;
1865
Jens Axboe2b188cc2019-01-07 10:46:33 -07001866 kiocb->ki_pos = READ_ONCE(sqe->off);
Jens Axboeba042912019-12-25 16:33:42 -07001867 if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) {
1868 req->flags |= REQ_F_CUR_POS;
1869 kiocb->ki_pos = req->file->f_pos;
1870 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001871 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03001872 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
1873 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
1874 if (unlikely(ret))
1875 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001876
1877 ioprio = READ_ONCE(sqe->ioprio);
1878 if (ioprio) {
1879 ret = ioprio_check_cap(ioprio);
1880 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06001881 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001882
1883 kiocb->ki_ioprio = ioprio;
1884 } else
1885 kiocb->ki_ioprio = get_current_ioprio();
1886
Stefan Bühler8449eed2019-04-27 20:34:19 +02001887 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06001888 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
1889 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02001890 req->flags |= REQ_F_NOWAIT;
1891
1892 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001893 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001894
Jens Axboedef596e2019-01-09 08:59:42 -07001895 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07001896 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
1897 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06001898 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001899
Jens Axboedef596e2019-01-09 08:59:42 -07001900 kiocb->ki_flags |= IOCB_HIPRI;
1901 kiocb->ki_complete = io_complete_rw_iopoll;
Jens Axboe6873e0b2019-10-30 13:53:09 -06001902 req->result = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001903 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001904 if (kiocb->ki_flags & IOCB_HIPRI)
1905 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07001906 kiocb->ki_complete = io_complete_rw;
1907 }
Jens Axboe9adbd452019-12-20 08:45:55 -07001908
Jens Axboe3529d8c2019-12-19 18:24:38 -07001909 req->rw.addr = READ_ONCE(sqe->addr);
1910 req->rw.len = READ_ONCE(sqe->len);
Jens Axboe9adbd452019-12-20 08:45:55 -07001911 /* we own ->private, reuse it for the buffer index */
1912 req->rw.kiocb.private = (void *) (unsigned long)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001913 READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001914 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001915}
1916
1917static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
1918{
1919 switch (ret) {
1920 case -EIOCBQUEUED:
1921 break;
1922 case -ERESTARTSYS:
1923 case -ERESTARTNOINTR:
1924 case -ERESTARTNOHAND:
1925 case -ERESTART_RESTARTBLOCK:
1926 /*
1927 * We can't just restart the syscall, since previously
1928 * submitted sqes may already be in progress. Just fail this
1929 * IO with EINTR.
1930 */
1931 ret = -EINTR;
1932 /* fall through */
1933 default:
1934 kiocb->ki_complete(kiocb, ret, 0);
1935 }
1936}
1937
Jens Axboeba816ad2019-09-28 11:36:45 -06001938static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt,
1939 bool in_async)
1940{
Jens Axboeba042912019-12-25 16:33:42 -07001941 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
1942
1943 if (req->flags & REQ_F_CUR_POS)
1944 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001945 if (in_async && ret >= 0 && kiocb->ki_complete == io_complete_rw)
Jens Axboeba816ad2019-09-28 11:36:45 -06001946 *nxt = __io_complete_rw(kiocb, ret);
1947 else
1948 io_rw_done(kiocb, ret);
1949}
1950
Jens Axboe9adbd452019-12-20 08:45:55 -07001951static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03001952 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07001953{
Jens Axboe9adbd452019-12-20 08:45:55 -07001954 struct io_ring_ctx *ctx = req->ctx;
1955 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001956 struct io_mapped_ubuf *imu;
1957 unsigned index, buf_index;
1958 size_t offset;
1959 u64 buf_addr;
1960
1961 /* attempt to use fixed buffers without having provided iovecs */
1962 if (unlikely(!ctx->user_bufs))
1963 return -EFAULT;
1964
Jens Axboe9adbd452019-12-20 08:45:55 -07001965 buf_index = (unsigned long) req->rw.kiocb.private;
Jens Axboeedafcce2019-01-09 09:16:05 -07001966 if (unlikely(buf_index >= ctx->nr_user_bufs))
1967 return -EFAULT;
1968
1969 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
1970 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07001971 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07001972
1973 /* overflow */
1974 if (buf_addr + len < buf_addr)
1975 return -EFAULT;
1976 /* not inside the mapped region */
1977 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
1978 return -EFAULT;
1979
1980 /*
1981 * May not be a start of buffer, set size appropriately
1982 * and advance us to the beginning.
1983 */
1984 offset = buf_addr - imu->ubuf;
1985 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06001986
1987 if (offset) {
1988 /*
1989 * Don't use iov_iter_advance() here, as it's really slow for
1990 * using the latter parts of a big fixed buffer - it iterates
1991 * over each segment manually. We can cheat a bit here, because
1992 * we know that:
1993 *
1994 * 1) it's a BVEC iter, we set it up
1995 * 2) all bvecs are PAGE_SIZE in size, except potentially the
1996 * first and last bvec
1997 *
1998 * So just find our index, and adjust the iterator afterwards.
1999 * If the offset is within the first bvec (or the whole first
2000 * bvec, just use iov_iter_advance(). This makes it easier
2001 * since we can just skip the first segment, which may not
2002 * be PAGE_SIZE aligned.
2003 */
2004 const struct bio_vec *bvec = imu->bvec;
2005
2006 if (offset <= bvec->bv_len) {
2007 iov_iter_advance(iter, offset);
2008 } else {
2009 unsigned long seg_skip;
2010
2011 /* skip first vec */
2012 offset -= bvec->bv_len;
2013 seg_skip = 1 + (offset >> PAGE_SHIFT);
2014
2015 iter->bvec = bvec + seg_skip;
2016 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02002017 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002018 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002019 }
2020 }
2021
Jens Axboe5e559562019-11-13 16:12:46 -07002022 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002023}
2024
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002025static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
2026 struct iovec **iovec, struct iov_iter *iter)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002027{
Jens Axboe9adbd452019-12-20 08:45:55 -07002028 void __user *buf = u64_to_user_ptr(req->rw.addr);
2029 size_t sqe_len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002030 u8 opcode;
2031
Jens Axboed625c6e2019-12-17 19:53:05 -07002032 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03002033 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07002034 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07002035 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07002036 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002037
Jens Axboe9adbd452019-12-20 08:45:55 -07002038 /* buffer index only valid with fixed read/write */
2039 if (req->rw.kiocb.private)
2040 return -EINVAL;
2041
Jens Axboe3a6820f2019-12-22 15:19:35 -07002042 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
2043 ssize_t ret;
2044 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
2045 *iovec = NULL;
2046 return ret;
2047 }
2048
Jens Axboef67676d2019-12-02 11:03:47 -07002049 if (req->io) {
2050 struct io_async_rw *iorw = &req->io->rw;
2051
2052 *iovec = iorw->iov;
2053 iov_iter_init(iter, rw, *iovec, iorw->nr_segs, iorw->size);
2054 if (iorw->iov == iorw->fast_iov)
2055 *iovec = NULL;
2056 return iorw->size;
2057 }
2058
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002059 if (!req->has_user)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002060 return -EFAULT;
2061
2062#ifdef CONFIG_COMPAT
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002063 if (req->ctx->compat)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002064 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
2065 iovec, iter);
2066#endif
2067
2068 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
2069}
2070
Jens Axboe32960612019-09-23 11:05:34 -06002071/*
2072 * For files that don't have ->read_iter() and ->write_iter(), handle them
2073 * by looping over ->read() or ->write() manually.
2074 */
2075static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
2076 struct iov_iter *iter)
2077{
2078 ssize_t ret = 0;
2079
2080 /*
2081 * Don't support polled IO through this interface, and we can't
2082 * support non-blocking either. For the latter, this just causes
2083 * the kiocb to be handled from an async context.
2084 */
2085 if (kiocb->ki_flags & IOCB_HIPRI)
2086 return -EOPNOTSUPP;
2087 if (kiocb->ki_flags & IOCB_NOWAIT)
2088 return -EAGAIN;
2089
2090 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002091 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06002092 ssize_t nr;
2093
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002094 if (!iov_iter_is_bvec(iter)) {
2095 iovec = iov_iter_iovec(iter);
2096 } else {
2097 /* fixed buffers import bvec */
2098 iovec.iov_base = kmap(iter->bvec->bv_page)
2099 + iter->iov_offset;
2100 iovec.iov_len = min(iter->count,
2101 iter->bvec->bv_len - iter->iov_offset);
2102 }
2103
Jens Axboe32960612019-09-23 11:05:34 -06002104 if (rw == READ) {
2105 nr = file->f_op->read(file, iovec.iov_base,
2106 iovec.iov_len, &kiocb->ki_pos);
2107 } else {
2108 nr = file->f_op->write(file, iovec.iov_base,
2109 iovec.iov_len, &kiocb->ki_pos);
2110 }
2111
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002112 if (iov_iter_is_bvec(iter))
2113 kunmap(iter->bvec->bv_page);
2114
Jens Axboe32960612019-09-23 11:05:34 -06002115 if (nr < 0) {
2116 if (!ret)
2117 ret = nr;
2118 break;
2119 }
2120 ret += nr;
2121 if (nr != iovec.iov_len)
2122 break;
2123 iov_iter_advance(iter, nr);
2124 }
2125
2126 return ret;
2127}
2128
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002129static void io_req_map_rw(struct io_kiocb *req, ssize_t io_size,
Jens Axboef67676d2019-12-02 11:03:47 -07002130 struct iovec *iovec, struct iovec *fast_iov,
2131 struct iov_iter *iter)
2132{
2133 req->io->rw.nr_segs = iter->nr_segs;
2134 req->io->rw.size = io_size;
2135 req->io->rw.iov = iovec;
2136 if (!req->io->rw.iov) {
2137 req->io->rw.iov = req->io->rw.fast_iov;
2138 memcpy(req->io->rw.iov, fast_iov,
2139 sizeof(struct iovec) * iter->nr_segs);
2140 }
2141}
2142
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002143static int io_alloc_async_ctx(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07002144{
Jens Axboed3656342019-12-18 09:50:26 -07002145 if (!io_op_defs[req->opcode].async_ctx)
2146 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002147 req->io = kmalloc(sizeof(*req->io), GFP_KERNEL);
Jens Axboe06b76d42019-12-19 14:44:26 -07002148 return req->io == NULL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002149}
2150
2151static void io_rw_async(struct io_wq_work **workptr)
2152{
2153 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2154 struct iovec *iov = NULL;
2155
2156 if (req->io->rw.iov != req->io->rw.fast_iov)
2157 iov = req->io->rw.iov;
2158 io_wq_submit_work(workptr);
2159 kfree(iov);
2160}
2161
2162static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size,
2163 struct iovec *iovec, struct iovec *fast_iov,
2164 struct iov_iter *iter)
2165{
Jens Axboe980ad262020-01-24 23:08:54 -07002166 if (!io_op_defs[req->opcode].async_ctx)
Jens Axboe74566df2020-01-13 19:23:24 -07002167 return 0;
Jens Axboe5d204bc2020-01-31 12:06:52 -07002168 if (!req->io) {
2169 if (io_alloc_async_ctx(req))
2170 return -ENOMEM;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002171
Jens Axboe5d204bc2020-01-31 12:06:52 -07002172 io_req_map_rw(req, io_size, iovec, fast_iov, iter);
2173 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002174 req->work.func = io_rw_async;
2175 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002176}
2177
Jens Axboe3529d8c2019-12-19 18:24:38 -07002178static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2179 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002180{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002181 struct io_async_ctx *io;
2182 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002183 ssize_t ret;
2184
Jens Axboe3529d8c2019-12-19 18:24:38 -07002185 ret = io_prep_rw(req, sqe, force_nonblock);
2186 if (ret)
2187 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002188
Jens Axboe3529d8c2019-12-19 18:24:38 -07002189 if (unlikely(!(req->file->f_mode & FMODE_READ)))
2190 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002191
Jens Axboe3529d8c2019-12-19 18:24:38 -07002192 if (!req->io)
2193 return 0;
2194
2195 io = req->io;
2196 io->rw.iov = io->rw.fast_iov;
2197 req->io = NULL;
2198 ret = io_import_iovec(READ, req, &io->rw.iov, &iter);
2199 req->io = io;
2200 if (ret < 0)
2201 return ret;
2202
2203 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2204 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002205}
2206
Pavel Begunkov267bc902019-11-07 01:41:08 +03002207static int io_read(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06002208 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002209{
2210 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002211 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002212 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002213 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002214 ssize_t io_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002215
Jens Axboe3529d8c2019-12-19 18:24:38 -07002216 ret = io_import_iovec(READ, req, &iovec, &iter);
Jens Axboe06b76d42019-12-19 14:44:26 -07002217 if (ret < 0)
2218 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002219
Jens Axboefd6c2e42019-12-18 12:19:41 -07002220 /* Ensure we clear previously set non-block flag */
2221 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07002222 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002223
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002224 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002225 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06002226 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07002227 req->result = io_size;
2228
2229 /*
2230 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2231 * we know to async punt it even if it was opened O_NONBLOCK
2232 */
Jens Axboe9adbd452019-12-20 08:45:55 -07002233 if (force_nonblock && !io_file_supports_async(req->file)) {
Jens Axboef67676d2019-12-02 11:03:47 -07002234 req->flags |= REQ_F_MUST_PUNT;
2235 goto copy_iov;
2236 }
Jens Axboe9e645e112019-05-10 16:07:28 -06002237
Jens Axboe31b51512019-01-18 22:56:34 -07002238 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002239 ret = rw_verify_area(READ, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002240 if (!ret) {
2241 ssize_t ret2;
2242
Jens Axboe9adbd452019-12-20 08:45:55 -07002243 if (req->file->f_op->read_iter)
2244 ret2 = call_read_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002245 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002246 ret2 = loop_rw_iter(READ, req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002247
Jens Axboe9d93a3f2019-05-15 13:53:07 -06002248 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboef67676d2019-12-02 11:03:47 -07002249 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002250 kiocb_done(kiocb, ret2, nxt, req->in_async);
Jens Axboef67676d2019-12-02 11:03:47 -07002251 } else {
2252copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002253 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002254 inline_vecs, &iter);
2255 if (ret)
2256 goto out_free;
2257 return -EAGAIN;
2258 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002259 }
Jens Axboef67676d2019-12-02 11:03:47 -07002260out_free:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002261 if (!io_wq_current_is_worker())
2262 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002263 return ret;
2264}
2265
Jens Axboe3529d8c2019-12-19 18:24:38 -07002266static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2267 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002268{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002269 struct io_async_ctx *io;
2270 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002271 ssize_t ret;
2272
Jens Axboe3529d8c2019-12-19 18:24:38 -07002273 ret = io_prep_rw(req, sqe, force_nonblock);
2274 if (ret)
2275 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002276
Jens Axboe3529d8c2019-12-19 18:24:38 -07002277 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
2278 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002279
Jens Axboe3529d8c2019-12-19 18:24:38 -07002280 if (!req->io)
2281 return 0;
2282
2283 io = req->io;
2284 io->rw.iov = io->rw.fast_iov;
2285 req->io = NULL;
2286 ret = io_import_iovec(WRITE, req, &io->rw.iov, &iter);
2287 req->io = io;
2288 if (ret < 0)
2289 return ret;
2290
2291 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2292 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002293}
2294
Pavel Begunkov267bc902019-11-07 01:41:08 +03002295static int io_write(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06002296 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002297{
2298 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002299 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002300 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002301 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002302 ssize_t ret, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002303
Jens Axboe3529d8c2019-12-19 18:24:38 -07002304 ret = io_import_iovec(WRITE, req, &iovec, &iter);
Jens Axboe06b76d42019-12-19 14:44:26 -07002305 if (ret < 0)
2306 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002307
Jens Axboefd6c2e42019-12-18 12:19:41 -07002308 /* Ensure we clear previously set non-block flag */
2309 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07002310 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002311
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002312 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002313 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06002314 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07002315 req->result = io_size;
2316
2317 /*
2318 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2319 * we know to async punt it even if it was opened O_NONBLOCK
2320 */
2321 if (force_nonblock && !io_file_supports_async(req->file)) {
2322 req->flags |= REQ_F_MUST_PUNT;
2323 goto copy_iov;
2324 }
2325
Jens Axboe10d59342019-12-09 20:16:22 -07002326 /* file path doesn't support NOWAIT for non-direct_IO */
2327 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
2328 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07002329 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06002330
Jens Axboe31b51512019-01-18 22:56:34 -07002331 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002332 ret = rw_verify_area(WRITE, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002333 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01002334 ssize_t ret2;
2335
Jens Axboe2b188cc2019-01-07 10:46:33 -07002336 /*
2337 * Open-code file_start_write here to grab freeze protection,
2338 * which will be released by another thread in
2339 * io_complete_rw(). Fool lockdep by telling it the lock got
2340 * released so that it doesn't complain about the held lock when
2341 * we return to userspace.
2342 */
Jens Axboe491381ce2019-10-17 09:20:46 -06002343 if (req->flags & REQ_F_ISREG) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002344 __sb_start_write(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002345 SB_FREEZE_WRITE, true);
Jens Axboe9adbd452019-12-20 08:45:55 -07002346 __sb_writers_release(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002347 SB_FREEZE_WRITE);
2348 }
2349 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01002350
Jens Axboe9adbd452019-12-20 08:45:55 -07002351 if (req->file->f_op->write_iter)
2352 ret2 = call_write_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002353 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002354 ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter);
Jens Axboef67676d2019-12-02 11:03:47 -07002355 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002356 kiocb_done(kiocb, ret2, nxt, req->in_async);
Jens Axboef67676d2019-12-02 11:03:47 -07002357 } else {
2358copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002359 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002360 inline_vecs, &iter);
2361 if (ret)
2362 goto out_free;
2363 return -EAGAIN;
2364 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002365 }
Jens Axboe31b51512019-01-18 22:56:34 -07002366out_free:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002367 if (!io_wq_current_is_worker())
2368 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002369 return ret;
2370}
2371
2372/*
2373 * IORING_OP_NOP just posts a completion event, nothing else.
2374 */
Jens Axboe78e19bb2019-11-06 15:21:34 -07002375static int io_nop(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002376{
2377 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002378
Jens Axboedef596e2019-01-09 08:59:42 -07002379 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2380 return -EINVAL;
2381
Jens Axboe78e19bb2019-11-06 15:21:34 -07002382 io_cqring_add_event(req, 0);
Jens Axboee65ef562019-03-12 10:16:44 -06002383 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002384 return 0;
2385}
2386
Jens Axboe3529d8c2019-12-19 18:24:38 -07002387static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002388{
Jens Axboe6b063142019-01-10 22:13:58 -07002389 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002390
Jens Axboe09bb8392019-03-13 12:39:28 -06002391 if (!req->file)
2392 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002393
Jens Axboe6b063142019-01-10 22:13:58 -07002394 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07002395 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07002396 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002397 return -EINVAL;
2398
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002399 req->sync.flags = READ_ONCE(sqe->fsync_flags);
2400 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
2401 return -EINVAL;
2402
2403 req->sync.off = READ_ONCE(sqe->off);
2404 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002405 return 0;
2406}
2407
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002408static bool io_req_cancelled(struct io_kiocb *req)
2409{
2410 if (req->work.flags & IO_WQ_WORK_CANCEL) {
2411 req_set_fail_links(req);
2412 io_cqring_add_event(req, -ECANCELED);
2413 io_put_req(req);
2414 return true;
2415 }
2416
2417 return false;
2418}
2419
Jens Axboe78912932020-01-14 22:09:06 -07002420static void io_link_work_cb(struct io_wq_work **workptr)
2421{
2422 struct io_wq_work *work = *workptr;
2423 struct io_kiocb *link = work->data;
2424
2425 io_queue_linked_timeout(link);
2426 work->func = io_wq_submit_work;
2427}
2428
2429static void io_wq_assign_next(struct io_wq_work **workptr, struct io_kiocb *nxt)
2430{
2431 struct io_kiocb *link;
2432
2433 io_prep_async_work(nxt, &link);
2434 *workptr = &nxt->work;
2435 if (link) {
2436 nxt->work.flags |= IO_WQ_WORK_CB;
2437 nxt->work.func = io_link_work_cb;
2438 nxt->work.data = link;
2439 }
2440}
2441
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002442static void io_fsync_finish(struct io_wq_work **workptr)
2443{
2444 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2445 loff_t end = req->sync.off + req->sync.len;
2446 struct io_kiocb *nxt = NULL;
2447 int ret;
2448
2449 if (io_req_cancelled(req))
2450 return;
2451
Jens Axboe9adbd452019-12-20 08:45:55 -07002452 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002453 end > 0 ? end : LLONG_MAX,
2454 req->sync.flags & IORING_FSYNC_DATASYNC);
2455 if (ret < 0)
2456 req_set_fail_links(req);
2457 io_cqring_add_event(req, ret);
2458 io_put_req_find_next(req, &nxt);
2459 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07002460 io_wq_assign_next(workptr, nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002461}
2462
Jens Axboefc4df992019-12-10 14:38:45 -07002463static int io_fsync(struct io_kiocb *req, struct io_kiocb **nxt,
2464 bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002465{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002466 struct io_wq_work *work, *old_work;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002467
2468 /* fsync always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002469 if (force_nonblock) {
2470 io_put_req(req);
2471 req->work.func = io_fsync_finish;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002472 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002473 }
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002474
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002475 work = old_work = &req->work;
2476 io_fsync_finish(&work);
2477 if (work && work != old_work)
2478 *nxt = container_of(work, struct io_kiocb, work);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002479 return 0;
2480}
2481
Jens Axboed63d1b52019-12-10 10:38:56 -07002482static void io_fallocate_finish(struct io_wq_work **workptr)
2483{
2484 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2485 struct io_kiocb *nxt = NULL;
2486 int ret;
2487
2488 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
2489 req->sync.len);
2490 if (ret < 0)
2491 req_set_fail_links(req);
2492 io_cqring_add_event(req, ret);
2493 io_put_req_find_next(req, &nxt);
2494 if (nxt)
2495 io_wq_assign_next(workptr, nxt);
2496}
2497
2498static int io_fallocate_prep(struct io_kiocb *req,
2499 const struct io_uring_sqe *sqe)
2500{
2501 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
2502 return -EINVAL;
2503
2504 req->sync.off = READ_ONCE(sqe->off);
2505 req->sync.len = READ_ONCE(sqe->addr);
2506 req->sync.mode = READ_ONCE(sqe->len);
2507 return 0;
2508}
2509
2510static int io_fallocate(struct io_kiocb *req, struct io_kiocb **nxt,
2511 bool force_nonblock)
2512{
2513 struct io_wq_work *work, *old_work;
2514
2515 /* fallocate always requiring blocking context */
2516 if (force_nonblock) {
2517 io_put_req(req);
2518 req->work.func = io_fallocate_finish;
2519 return -EAGAIN;
2520 }
2521
2522 work = old_work = &req->work;
2523 io_fallocate_finish(&work);
2524 if (work && work != old_work)
2525 *nxt = container_of(work, struct io_kiocb, work);
2526
2527 return 0;
2528}
2529
Jens Axboe15b71ab2019-12-11 11:20:36 -07002530static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2531{
Jens Axboef8748882020-01-08 17:47:02 -07002532 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002533 int ret;
2534
2535 if (sqe->ioprio || sqe->buf_index)
2536 return -EINVAL;
2537
2538 req->open.dfd = READ_ONCE(sqe->fd);
Jens Axboec12cedf2020-01-08 17:41:21 -07002539 req->open.how.mode = READ_ONCE(sqe->len);
Jens Axboef8748882020-01-08 17:47:02 -07002540 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboec12cedf2020-01-08 17:41:21 -07002541 req->open.how.flags = READ_ONCE(sqe->open_flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002542
Jens Axboef8748882020-01-08 17:47:02 -07002543 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002544 if (IS_ERR(req->open.filename)) {
2545 ret = PTR_ERR(req->open.filename);
2546 req->open.filename = NULL;
2547 return ret;
2548 }
2549
2550 return 0;
2551}
2552
Jens Axboecebdb982020-01-08 17:59:24 -07002553static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2554{
2555 struct open_how __user *how;
2556 const char __user *fname;
2557 size_t len;
2558 int ret;
2559
2560 if (sqe->ioprio || sqe->buf_index)
2561 return -EINVAL;
2562
2563 req->open.dfd = READ_ONCE(sqe->fd);
2564 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
2565 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
2566 len = READ_ONCE(sqe->len);
2567
2568 if (len < OPEN_HOW_SIZE_VER0)
2569 return -EINVAL;
2570
2571 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
2572 len);
2573 if (ret)
2574 return ret;
2575
2576 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
2577 req->open.how.flags |= O_LARGEFILE;
2578
2579 req->open.filename = getname(fname);
2580 if (IS_ERR(req->open.filename)) {
2581 ret = PTR_ERR(req->open.filename);
2582 req->open.filename = NULL;
2583 return ret;
2584 }
2585
2586 return 0;
2587}
2588
2589static int io_openat2(struct io_kiocb *req, struct io_kiocb **nxt,
2590 bool force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07002591{
2592 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002593 struct file *file;
2594 int ret;
2595
Jens Axboef86cd202020-01-29 13:46:44 -07002596 if (force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07002597 return -EAGAIN;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002598
Jens Axboecebdb982020-01-08 17:59:24 -07002599 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002600 if (ret)
2601 goto err;
2602
Jens Axboecebdb982020-01-08 17:59:24 -07002603 ret = get_unused_fd_flags(req->open.how.flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002604 if (ret < 0)
2605 goto err;
2606
2607 file = do_filp_open(req->open.dfd, req->open.filename, &op);
2608 if (IS_ERR(file)) {
2609 put_unused_fd(ret);
2610 ret = PTR_ERR(file);
2611 } else {
2612 fsnotify_open(file);
2613 fd_install(ret, file);
2614 }
2615err:
2616 putname(req->open.filename);
2617 if (ret < 0)
2618 req_set_fail_links(req);
2619 io_cqring_add_event(req, ret);
2620 io_put_req_find_next(req, nxt);
2621 return 0;
2622}
2623
Jens Axboecebdb982020-01-08 17:59:24 -07002624static int io_openat(struct io_kiocb *req, struct io_kiocb **nxt,
2625 bool force_nonblock)
2626{
2627 req->open.how = build_open_how(req->open.how.flags, req->open.how.mode);
2628 return io_openat2(req, nxt, force_nonblock);
2629}
2630
Jens Axboe3e4827b2020-01-08 15:18:09 -07002631static int io_epoll_ctl_prep(struct io_kiocb *req,
2632 const struct io_uring_sqe *sqe)
2633{
2634#if defined(CONFIG_EPOLL)
2635 if (sqe->ioprio || sqe->buf_index)
2636 return -EINVAL;
2637
2638 req->epoll.epfd = READ_ONCE(sqe->fd);
2639 req->epoll.op = READ_ONCE(sqe->len);
2640 req->epoll.fd = READ_ONCE(sqe->off);
2641
2642 if (ep_op_has_event(req->epoll.op)) {
2643 struct epoll_event __user *ev;
2644
2645 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
2646 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
2647 return -EFAULT;
2648 }
2649
2650 return 0;
2651#else
2652 return -EOPNOTSUPP;
2653#endif
2654}
2655
2656static int io_epoll_ctl(struct io_kiocb *req, struct io_kiocb **nxt,
2657 bool force_nonblock)
2658{
2659#if defined(CONFIG_EPOLL)
2660 struct io_epoll *ie = &req->epoll;
2661 int ret;
2662
2663 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
2664 if (force_nonblock && ret == -EAGAIN)
2665 return -EAGAIN;
2666
2667 if (ret < 0)
2668 req_set_fail_links(req);
2669 io_cqring_add_event(req, ret);
2670 io_put_req_find_next(req, nxt);
2671 return 0;
2672#else
2673 return -EOPNOTSUPP;
2674#endif
2675}
2676
Jens Axboec1ca7572019-12-25 22:18:28 -07002677static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2678{
2679#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
2680 if (sqe->ioprio || sqe->buf_index || sqe->off)
2681 return -EINVAL;
2682
2683 req->madvise.addr = READ_ONCE(sqe->addr);
2684 req->madvise.len = READ_ONCE(sqe->len);
2685 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
2686 return 0;
2687#else
2688 return -EOPNOTSUPP;
2689#endif
2690}
2691
2692static int io_madvise(struct io_kiocb *req, struct io_kiocb **nxt,
2693 bool force_nonblock)
2694{
2695#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
2696 struct io_madvise *ma = &req->madvise;
2697 int ret;
2698
2699 if (force_nonblock)
2700 return -EAGAIN;
2701
2702 ret = do_madvise(ma->addr, ma->len, ma->advice);
2703 if (ret < 0)
2704 req_set_fail_links(req);
2705 io_cqring_add_event(req, ret);
2706 io_put_req_find_next(req, nxt);
2707 return 0;
2708#else
2709 return -EOPNOTSUPP;
2710#endif
2711}
2712
Jens Axboe4840e412019-12-25 22:03:45 -07002713static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2714{
2715 if (sqe->ioprio || sqe->buf_index || sqe->addr)
2716 return -EINVAL;
2717
2718 req->fadvise.offset = READ_ONCE(sqe->off);
2719 req->fadvise.len = READ_ONCE(sqe->len);
2720 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
2721 return 0;
2722}
2723
2724static int io_fadvise(struct io_kiocb *req, struct io_kiocb **nxt,
2725 bool force_nonblock)
2726{
2727 struct io_fadvise *fa = &req->fadvise;
2728 int ret;
2729
Jens Axboe3e694262020-02-01 09:22:49 -07002730 if (force_nonblock) {
2731 switch (fa->advice) {
2732 case POSIX_FADV_NORMAL:
2733 case POSIX_FADV_RANDOM:
2734 case POSIX_FADV_SEQUENTIAL:
2735 break;
2736 default:
2737 return -EAGAIN;
2738 }
2739 }
Jens Axboe4840e412019-12-25 22:03:45 -07002740
2741 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
2742 if (ret < 0)
2743 req_set_fail_links(req);
2744 io_cqring_add_event(req, ret);
2745 io_put_req_find_next(req, nxt);
2746 return 0;
2747}
2748
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002749static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2750{
Jens Axboef8748882020-01-08 17:47:02 -07002751 const char __user *fname;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002752 unsigned lookup_flags;
2753 int ret;
2754
2755 if (sqe->ioprio || sqe->buf_index)
2756 return -EINVAL;
2757
2758 req->open.dfd = READ_ONCE(sqe->fd);
2759 req->open.mask = READ_ONCE(sqe->len);
Jens Axboef8748882020-01-08 17:47:02 -07002760 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002761 req->open.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboec12cedf2020-01-08 17:41:21 -07002762 req->open.how.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002763
Jens Axboec12cedf2020-01-08 17:41:21 -07002764 if (vfs_stat_set_lookup_flags(&lookup_flags, req->open.how.flags))
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002765 return -EINVAL;
2766
Jens Axboef8748882020-01-08 17:47:02 -07002767 req->open.filename = getname_flags(fname, lookup_flags, NULL);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002768 if (IS_ERR(req->open.filename)) {
2769 ret = PTR_ERR(req->open.filename);
2770 req->open.filename = NULL;
2771 return ret;
2772 }
2773
2774 return 0;
2775}
2776
2777static int io_statx(struct io_kiocb *req, struct io_kiocb **nxt,
2778 bool force_nonblock)
2779{
2780 struct io_open *ctx = &req->open;
2781 unsigned lookup_flags;
2782 struct path path;
2783 struct kstat stat;
2784 int ret;
2785
2786 if (force_nonblock)
2787 return -EAGAIN;
2788
Jens Axboec12cedf2020-01-08 17:41:21 -07002789 if (vfs_stat_set_lookup_flags(&lookup_flags, ctx->how.flags))
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002790 return -EINVAL;
2791
2792retry:
2793 /* filename_lookup() drops it, keep a reference */
2794 ctx->filename->refcnt++;
2795
2796 ret = filename_lookup(ctx->dfd, ctx->filename, lookup_flags, &path,
2797 NULL);
2798 if (ret)
2799 goto err;
2800
Jens Axboec12cedf2020-01-08 17:41:21 -07002801 ret = vfs_getattr(&path, &stat, ctx->mask, ctx->how.flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002802 path_put(&path);
2803 if (retry_estale(ret, lookup_flags)) {
2804 lookup_flags |= LOOKUP_REVAL;
2805 goto retry;
2806 }
2807 if (!ret)
2808 ret = cp_statx(&stat, ctx->buffer);
2809err:
2810 putname(ctx->filename);
2811 if (ret < 0)
2812 req_set_fail_links(req);
2813 io_cqring_add_event(req, ret);
2814 io_put_req_find_next(req, nxt);
2815 return 0;
2816}
2817
Jens Axboeb5dba592019-12-11 14:02:38 -07002818static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2819{
2820 /*
2821 * If we queue this for async, it must not be cancellable. That would
2822 * leave the 'file' in an undeterminate state.
2823 */
2824 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
2825
2826 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
2827 sqe->rw_flags || sqe->buf_index)
2828 return -EINVAL;
2829 if (sqe->flags & IOSQE_FIXED_FILE)
2830 return -EINVAL;
2831
2832 req->close.fd = READ_ONCE(sqe->fd);
2833 if (req->file->f_op == &io_uring_fops ||
Pavel Begunkovb14cca02020-01-17 04:45:59 +03002834 req->close.fd == req->ctx->ring_fd)
Jens Axboeb5dba592019-12-11 14:02:38 -07002835 return -EBADF;
2836
2837 return 0;
2838}
2839
2840static void io_close_finish(struct io_wq_work **workptr)
2841{
2842 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2843 struct io_kiocb *nxt = NULL;
2844
2845 /* Invoked with files, we need to do the close */
2846 if (req->work.files) {
2847 int ret;
2848
2849 ret = filp_close(req->close.put_file, req->work.files);
Jens Axboe1a417f42020-01-31 17:16:48 -07002850 if (ret < 0)
Jens Axboeb5dba592019-12-11 14:02:38 -07002851 req_set_fail_links(req);
Jens Axboeb5dba592019-12-11 14:02:38 -07002852 io_cqring_add_event(req, ret);
2853 }
2854
2855 fput(req->close.put_file);
2856
Jens Axboeb5dba592019-12-11 14:02:38 -07002857 io_put_req_find_next(req, &nxt);
2858 if (nxt)
2859 io_wq_assign_next(workptr, nxt);
2860}
2861
2862static int io_close(struct io_kiocb *req, struct io_kiocb **nxt,
2863 bool force_nonblock)
2864{
2865 int ret;
2866
2867 req->close.put_file = NULL;
2868 ret = __close_fd_get_file(req->close.fd, &req->close.put_file);
2869 if (ret < 0)
2870 return ret;
2871
2872 /* if the file has a flush method, be safe and punt to async */
Jens Axboef86cd202020-01-29 13:46:44 -07002873 if (req->close.put_file->f_op->flush && !io_wq_current_is_worker())
Jens Axboeb5dba592019-12-11 14:02:38 -07002874 goto eagain;
Jens Axboeb5dba592019-12-11 14:02:38 -07002875
2876 /*
2877 * No ->flush(), safely close from here and just punt the
2878 * fput() to async context.
2879 */
2880 ret = filp_close(req->close.put_file, current->files);
2881
2882 if (ret < 0)
2883 req_set_fail_links(req);
2884 io_cqring_add_event(req, ret);
2885
2886 if (io_wq_current_is_worker()) {
2887 struct io_wq_work *old_work, *work;
2888
2889 old_work = work = &req->work;
2890 io_close_finish(&work);
2891 if (work && work != old_work)
2892 *nxt = container_of(work, struct io_kiocb, work);
2893 return 0;
2894 }
2895
2896eagain:
2897 req->work.func = io_close_finish;
Jens Axboe1a417f42020-01-31 17:16:48 -07002898 /*
2899 * Do manual async queue here to avoid grabbing files - we don't
2900 * need the files, and it'll cause io_close_finish() to close
2901 * the file again and cause a double CQE entry for this request
2902 */
2903 io_queue_async_work(req);
2904 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07002905}
2906
Jens Axboe3529d8c2019-12-19 18:24:38 -07002907static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002908{
2909 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002910
2911 if (!req->file)
2912 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002913
2914 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2915 return -EINVAL;
2916 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
2917 return -EINVAL;
2918
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002919 req->sync.off = READ_ONCE(sqe->off);
2920 req->sync.len = READ_ONCE(sqe->len);
2921 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002922 return 0;
2923}
2924
2925static void io_sync_file_range_finish(struct io_wq_work **workptr)
2926{
2927 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2928 struct io_kiocb *nxt = NULL;
2929 int ret;
2930
2931 if (io_req_cancelled(req))
2932 return;
2933
Jens Axboe9adbd452019-12-20 08:45:55 -07002934 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002935 req->sync.flags);
2936 if (ret < 0)
2937 req_set_fail_links(req);
2938 io_cqring_add_event(req, ret);
2939 io_put_req_find_next(req, &nxt);
2940 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07002941 io_wq_assign_next(workptr, nxt);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002942}
2943
Jens Axboefc4df992019-12-10 14:38:45 -07002944static int io_sync_file_range(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002945 bool force_nonblock)
2946{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002947 struct io_wq_work *work, *old_work;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002948
2949 /* sync_file_range always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002950 if (force_nonblock) {
2951 io_put_req(req);
2952 req->work.func = io_sync_file_range_finish;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002953 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002954 }
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002955
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002956 work = old_work = &req->work;
2957 io_sync_file_range_finish(&work);
2958 if (work && work != old_work)
2959 *nxt = container_of(work, struct io_kiocb, work);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002960 return 0;
2961}
2962
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002963#if defined(CONFIG_NET)
2964static void io_sendrecv_async(struct io_wq_work **workptr)
2965{
2966 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2967 struct iovec *iov = NULL;
2968
2969 if (req->io->rw.iov != req->io->rw.fast_iov)
2970 iov = req->io->msg.iov;
2971 io_wq_submit_work(workptr);
2972 kfree(iov);
2973}
2974#endif
2975
Jens Axboe3529d8c2019-12-19 18:24:38 -07002976static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06002977{
Jens Axboe03b12302019-12-02 18:50:25 -07002978#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07002979 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002980 struct io_async_ctx *io = req->io;
Jens Axboe03b12302019-12-02 18:50:25 -07002981
Jens Axboee47293f2019-12-20 08:58:21 -07002982 sr->msg_flags = READ_ONCE(sqe->msg_flags);
2983 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07002984 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07002985
Jens Axboefddafac2020-01-04 20:19:44 -07002986 if (!io || req->opcode == IORING_OP_SEND)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002987 return 0;
2988
Jens Axboed9688562019-12-09 19:35:20 -07002989 io->msg.iov = io->msg.fast_iov;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002990 return sendmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07002991 &io->msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002992#else
Jens Axboee47293f2019-12-20 08:58:21 -07002993 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07002994#endif
2995}
2996
Jens Axboefc4df992019-12-10 14:38:45 -07002997static int io_sendmsg(struct io_kiocb *req, struct io_kiocb **nxt,
2998 bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07002999{
3000#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07003001 struct io_async_msghdr *kmsg = NULL;
Jens Axboe03b12302019-12-02 18:50:25 -07003002 struct socket *sock;
3003 int ret;
3004
3005 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3006 return -EINVAL;
3007
3008 sock = sock_from_file(req->file, &ret);
3009 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003010 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07003011 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -07003012 unsigned flags;
3013
Jens Axboe03b12302019-12-02 18:50:25 -07003014 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07003015 kmsg = &req->io->msg;
3016 kmsg->msg.msg_name = &addr;
3017 /* if iov is set, it's allocated already */
3018 if (!kmsg->iov)
3019 kmsg->iov = kmsg->fast_iov;
3020 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07003021 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003022 struct io_sr_msg *sr = &req->sr_msg;
3023
Jens Axboe0b416c32019-12-15 10:57:46 -07003024 kmsg = &io.msg;
3025 kmsg->msg.msg_name = &addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003026
3027 io.msg.iov = io.msg.fast_iov;
3028 ret = sendmsg_copy_msghdr(&io.msg.msg, sr->msg,
3029 sr->msg_flags, &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07003030 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003031 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003032 }
3033
Jens Axboee47293f2019-12-20 08:58:21 -07003034 flags = req->sr_msg.msg_flags;
3035 if (flags & MSG_DONTWAIT)
3036 req->flags |= REQ_F_NOWAIT;
3037 else if (force_nonblock)
3038 flags |= MSG_DONTWAIT;
3039
Jens Axboe0b416c32019-12-15 10:57:46 -07003040 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Jens Axboe03b12302019-12-02 18:50:25 -07003041 if (force_nonblock && ret == -EAGAIN) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003042 if (req->io)
3043 return -EAGAIN;
3044 if (io_alloc_async_ctx(req))
3045 return -ENOMEM;
3046 memcpy(&req->io->msg, &io.msg, sizeof(io.msg));
3047 req->work.func = io_sendrecv_async;
Jens Axboe0b416c32019-12-15 10:57:46 -07003048 return -EAGAIN;
Jens Axboe03b12302019-12-02 18:50:25 -07003049 }
3050 if (ret == -ERESTARTSYS)
3051 ret = -EINTR;
3052 }
3053
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003054 if (!io_wq_current_is_worker() && kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07003055 kfree(kmsg->iov);
Jens Axboe03b12302019-12-02 18:50:25 -07003056 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003057 if (ret < 0)
3058 req_set_fail_links(req);
Jens Axboe03b12302019-12-02 18:50:25 -07003059 io_put_req_find_next(req, nxt);
3060 return 0;
3061#else
3062 return -EOPNOTSUPP;
3063#endif
3064}
3065
Jens Axboefddafac2020-01-04 20:19:44 -07003066static int io_send(struct io_kiocb *req, struct io_kiocb **nxt,
3067 bool force_nonblock)
3068{
3069#if defined(CONFIG_NET)
3070 struct socket *sock;
3071 int ret;
3072
3073 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3074 return -EINVAL;
3075
3076 sock = sock_from_file(req->file, &ret);
3077 if (sock) {
3078 struct io_sr_msg *sr = &req->sr_msg;
3079 struct msghdr msg;
3080 struct iovec iov;
3081 unsigned flags;
3082
3083 ret = import_single_range(WRITE, sr->buf, sr->len, &iov,
3084 &msg.msg_iter);
3085 if (ret)
3086 return ret;
3087
3088 msg.msg_name = NULL;
3089 msg.msg_control = NULL;
3090 msg.msg_controllen = 0;
3091 msg.msg_namelen = 0;
3092
3093 flags = req->sr_msg.msg_flags;
3094 if (flags & MSG_DONTWAIT)
3095 req->flags |= REQ_F_NOWAIT;
3096 else if (force_nonblock)
3097 flags |= MSG_DONTWAIT;
3098
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003099 msg.msg_flags = flags;
3100 ret = sock_sendmsg(sock, &msg);
Jens Axboefddafac2020-01-04 20:19:44 -07003101 if (force_nonblock && ret == -EAGAIN)
3102 return -EAGAIN;
3103 if (ret == -ERESTARTSYS)
3104 ret = -EINTR;
3105 }
3106
3107 io_cqring_add_event(req, ret);
3108 if (ret < 0)
3109 req_set_fail_links(req);
3110 io_put_req_find_next(req, nxt);
3111 return 0;
3112#else
3113 return -EOPNOTSUPP;
3114#endif
3115}
3116
Jens Axboe3529d8c2019-12-19 18:24:38 -07003117static int io_recvmsg_prep(struct io_kiocb *req,
3118 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07003119{
3120#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07003121 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003122 struct io_async_ctx *io = req->io;
Jens Axboe06b76d42019-12-19 14:44:26 -07003123
Jens Axboe3529d8c2019-12-19 18:24:38 -07003124 sr->msg_flags = READ_ONCE(sqe->msg_flags);
3125 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003126 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003127
Jens Axboefddafac2020-01-04 20:19:44 -07003128 if (!io || req->opcode == IORING_OP_RECV)
Jens Axboe06b76d42019-12-19 14:44:26 -07003129 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07003130
Jens Axboed9688562019-12-09 19:35:20 -07003131 io->msg.iov = io->msg.fast_iov;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003132 return recvmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07003133 &io->msg.uaddr, &io->msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07003134#else
Jens Axboee47293f2019-12-20 08:58:21 -07003135 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07003136#endif
3137}
3138
Jens Axboefc4df992019-12-10 14:38:45 -07003139static int io_recvmsg(struct io_kiocb *req, struct io_kiocb **nxt,
3140 bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07003141{
3142#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07003143 struct io_async_msghdr *kmsg = NULL;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003144 struct socket *sock;
3145 int ret;
3146
3147 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3148 return -EINVAL;
3149
3150 sock = sock_from_file(req->file, &ret);
3151 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003152 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07003153 struct sockaddr_storage addr;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003154 unsigned flags;
3155
Jens Axboe03b12302019-12-02 18:50:25 -07003156 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07003157 kmsg = &req->io->msg;
3158 kmsg->msg.msg_name = &addr;
3159 /* if iov is set, it's allocated already */
3160 if (!kmsg->iov)
3161 kmsg->iov = kmsg->fast_iov;
3162 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07003163 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003164 struct io_sr_msg *sr = &req->sr_msg;
3165
Jens Axboe0b416c32019-12-15 10:57:46 -07003166 kmsg = &io.msg;
3167 kmsg->msg.msg_name = &addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003168
3169 io.msg.iov = io.msg.fast_iov;
3170 ret = recvmsg_copy_msghdr(&io.msg.msg, sr->msg,
3171 sr->msg_flags, &io.msg.uaddr,
3172 &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07003173 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003174 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003175 }
Jens Axboe0fa03c62019-04-19 13:34:07 -06003176
Jens Axboee47293f2019-12-20 08:58:21 -07003177 flags = req->sr_msg.msg_flags;
3178 if (flags & MSG_DONTWAIT)
3179 req->flags |= REQ_F_NOWAIT;
3180 else if (force_nonblock)
3181 flags |= MSG_DONTWAIT;
3182
3183 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.msg,
3184 kmsg->uaddr, flags);
Jens Axboe03b12302019-12-02 18:50:25 -07003185 if (force_nonblock && ret == -EAGAIN) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003186 if (req->io)
3187 return -EAGAIN;
3188 if (io_alloc_async_ctx(req))
3189 return -ENOMEM;
3190 memcpy(&req->io->msg, &io.msg, sizeof(io.msg));
3191 req->work.func = io_sendrecv_async;
Jens Axboe0b416c32019-12-15 10:57:46 -07003192 return -EAGAIN;
Jens Axboe03b12302019-12-02 18:50:25 -07003193 }
Jens Axboe441cdbd2019-12-02 18:49:10 -07003194 if (ret == -ERESTARTSYS)
3195 ret = -EINTR;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003196 }
3197
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003198 if (!io_wq_current_is_worker() && kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07003199 kfree(kmsg->iov);
Jens Axboe78e19bb2019-11-06 15:21:34 -07003200 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003201 if (ret < 0)
3202 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003203 io_put_req_find_next(req, nxt);
Jens Axboe0fa03c62019-04-19 13:34:07 -06003204 return 0;
3205#else
3206 return -EOPNOTSUPP;
3207#endif
3208}
3209
Jens Axboefddafac2020-01-04 20:19:44 -07003210static int io_recv(struct io_kiocb *req, struct io_kiocb **nxt,
3211 bool force_nonblock)
3212{
3213#if defined(CONFIG_NET)
3214 struct socket *sock;
3215 int ret;
3216
3217 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3218 return -EINVAL;
3219
3220 sock = sock_from_file(req->file, &ret);
3221 if (sock) {
3222 struct io_sr_msg *sr = &req->sr_msg;
3223 struct msghdr msg;
3224 struct iovec iov;
3225 unsigned flags;
3226
3227 ret = import_single_range(READ, sr->buf, sr->len, &iov,
3228 &msg.msg_iter);
3229 if (ret)
3230 return ret;
3231
3232 msg.msg_name = NULL;
3233 msg.msg_control = NULL;
3234 msg.msg_controllen = 0;
3235 msg.msg_namelen = 0;
3236 msg.msg_iocb = NULL;
3237 msg.msg_flags = 0;
3238
3239 flags = req->sr_msg.msg_flags;
3240 if (flags & MSG_DONTWAIT)
3241 req->flags |= REQ_F_NOWAIT;
3242 else if (force_nonblock)
3243 flags |= MSG_DONTWAIT;
3244
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003245 ret = sock_recvmsg(sock, &msg, flags);
Jens Axboefddafac2020-01-04 20:19:44 -07003246 if (force_nonblock && ret == -EAGAIN)
3247 return -EAGAIN;
3248 if (ret == -ERESTARTSYS)
3249 ret = -EINTR;
3250 }
3251
3252 io_cqring_add_event(req, ret);
3253 if (ret < 0)
3254 req_set_fail_links(req);
3255 io_put_req_find_next(req, nxt);
3256 return 0;
3257#else
3258 return -EOPNOTSUPP;
3259#endif
3260}
3261
3262
Jens Axboe3529d8c2019-12-19 18:24:38 -07003263static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003264{
3265#if defined(CONFIG_NET)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003266 struct io_accept *accept = &req->accept;
3267
Jens Axboe17f2fe32019-10-17 14:42:58 -06003268 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3269 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05003270 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003271 return -EINVAL;
3272
Jens Axboed55e5f52019-12-11 16:12:15 -07003273 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3274 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003275 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003276 return 0;
3277#else
3278 return -EOPNOTSUPP;
3279#endif
3280}
Jens Axboe17f2fe32019-10-17 14:42:58 -06003281
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003282#if defined(CONFIG_NET)
3283static int __io_accept(struct io_kiocb *req, struct io_kiocb **nxt,
3284 bool force_nonblock)
3285{
3286 struct io_accept *accept = &req->accept;
3287 unsigned file_flags;
3288 int ret;
3289
3290 file_flags = force_nonblock ? O_NONBLOCK : 0;
3291 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
3292 accept->addr_len, accept->flags);
3293 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003294 return -EAGAIN;
Jens Axboe8e3cca12019-11-09 19:52:33 -07003295 if (ret == -ERESTARTSYS)
3296 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003297 if (ret < 0)
3298 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07003299 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003300 io_put_req_find_next(req, nxt);
Jens Axboe17f2fe32019-10-17 14:42:58 -06003301 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003302}
3303
3304static void io_accept_finish(struct io_wq_work **workptr)
3305{
3306 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3307 struct io_kiocb *nxt = NULL;
3308
3309 if (io_req_cancelled(req))
3310 return;
3311 __io_accept(req, &nxt, false);
3312 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07003313 io_wq_assign_next(workptr, nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003314}
3315#endif
3316
3317static int io_accept(struct io_kiocb *req, struct io_kiocb **nxt,
3318 bool force_nonblock)
3319{
3320#if defined(CONFIG_NET)
3321 int ret;
3322
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003323 ret = __io_accept(req, nxt, force_nonblock);
3324 if (ret == -EAGAIN && force_nonblock) {
3325 req->work.func = io_accept_finish;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003326 io_put_req(req);
3327 return -EAGAIN;
3328 }
3329 return 0;
Jens Axboe17f2fe32019-10-17 14:42:58 -06003330#else
3331 return -EOPNOTSUPP;
3332#endif
3333}
3334
Jens Axboe3529d8c2019-12-19 18:24:38 -07003335static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07003336{
3337#if defined(CONFIG_NET)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003338 struct io_connect *conn = &req->connect;
3339 struct io_async_ctx *io = req->io;
Jens Axboef499a022019-12-02 16:28:46 -07003340
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003341 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3342 return -EINVAL;
3343 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
3344 return -EINVAL;
3345
Jens Axboe3529d8c2019-12-19 18:24:38 -07003346 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3347 conn->addr_len = READ_ONCE(sqe->addr2);
3348
3349 if (!io)
3350 return 0;
3351
3352 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003353 &io->connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07003354#else
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003355 return -EOPNOTSUPP;
Jens Axboef499a022019-12-02 16:28:46 -07003356#endif
3357}
3358
Jens Axboefc4df992019-12-10 14:38:45 -07003359static int io_connect(struct io_kiocb *req, struct io_kiocb **nxt,
3360 bool force_nonblock)
Jens Axboef8e85cf2019-11-23 14:24:24 -07003361{
3362#if defined(CONFIG_NET)
Jens Axboef499a022019-12-02 16:28:46 -07003363 struct io_async_ctx __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003364 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003365 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003366
Jens Axboef499a022019-12-02 16:28:46 -07003367 if (req->io) {
3368 io = req->io;
3369 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003370 ret = move_addr_to_kernel(req->connect.addr,
3371 req->connect.addr_len,
3372 &__io.connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07003373 if (ret)
3374 goto out;
3375 io = &__io;
3376 }
3377
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003378 file_flags = force_nonblock ? O_NONBLOCK : 0;
3379
3380 ret = __sys_connect_file(req->file, &io->connect.address,
3381 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07003382 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003383 if (req->io)
3384 return -EAGAIN;
3385 if (io_alloc_async_ctx(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07003386 ret = -ENOMEM;
3387 goto out;
3388 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003389 memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect));
Jens Axboef8e85cf2019-11-23 14:24:24 -07003390 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07003391 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07003392 if (ret == -ERESTARTSYS)
3393 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07003394out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003395 if (ret < 0)
3396 req_set_fail_links(req);
Jens Axboef8e85cf2019-11-23 14:24:24 -07003397 io_cqring_add_event(req, ret);
3398 io_put_req_find_next(req, nxt);
3399 return 0;
3400#else
3401 return -EOPNOTSUPP;
3402#endif
3403}
3404
Jens Axboe221c5eb2019-01-17 09:41:58 -07003405static void io_poll_remove_one(struct io_kiocb *req)
3406{
3407 struct io_poll_iocb *poll = &req->poll;
3408
3409 spin_lock(&poll->head->lock);
3410 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07003411 if (!list_empty(&poll->wait.entry)) {
3412 list_del_init(&poll->wait.entry);
Jackie Liua197f662019-11-08 08:09:12 -07003413 io_queue_async_work(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003414 }
3415 spin_unlock(&poll->head->lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07003416 hash_del(&req->hash_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003417}
3418
3419static void io_poll_remove_all(struct io_ring_ctx *ctx)
3420{
Jens Axboe78076bb2019-12-04 19:56:40 -07003421 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003422 struct io_kiocb *req;
Jens Axboe78076bb2019-12-04 19:56:40 -07003423 int i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003424
3425 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07003426 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
3427 struct hlist_head *list;
3428
3429 list = &ctx->cancel_hash[i];
3430 hlist_for_each_entry_safe(req, tmp, list, hash_node)
3431 io_poll_remove_one(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003432 }
3433 spin_unlock_irq(&ctx->completion_lock);
3434}
3435
Jens Axboe47f46762019-11-09 17:43:02 -07003436static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
3437{
Jens Axboe78076bb2019-12-04 19:56:40 -07003438 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07003439 struct io_kiocb *req;
3440
Jens Axboe78076bb2019-12-04 19:56:40 -07003441 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
3442 hlist_for_each_entry(req, list, hash_node) {
3443 if (sqe_addr == req->user_data) {
Jens Axboeeac406c2019-11-14 12:09:58 -07003444 io_poll_remove_one(req);
3445 return 0;
3446 }
Jens Axboe47f46762019-11-09 17:43:02 -07003447 }
3448
3449 return -ENOENT;
3450}
3451
Jens Axboe3529d8c2019-12-19 18:24:38 -07003452static int io_poll_remove_prep(struct io_kiocb *req,
3453 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003454{
Jens Axboe221c5eb2019-01-17 09:41:58 -07003455 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3456 return -EINVAL;
3457 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
3458 sqe->poll_events)
3459 return -EINVAL;
3460
Jens Axboe0969e782019-12-17 18:40:57 -07003461 req->poll.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07003462 return 0;
3463}
3464
3465/*
3466 * Find a running poll command that matches one specified in sqe->addr,
3467 * and remove it if found.
3468 */
3469static int io_poll_remove(struct io_kiocb *req)
3470{
3471 struct io_ring_ctx *ctx = req->ctx;
3472 u64 addr;
3473 int ret;
3474
Jens Axboe0969e782019-12-17 18:40:57 -07003475 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003476 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07003477 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003478 spin_unlock_irq(&ctx->completion_lock);
3479
Jens Axboe78e19bb2019-11-06 15:21:34 -07003480 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003481 if (ret < 0)
3482 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06003483 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003484 return 0;
3485}
3486
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003487static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003488{
Jackie Liua197f662019-11-08 08:09:12 -07003489 struct io_ring_ctx *ctx = req->ctx;
3490
Jens Axboe8c838782019-03-12 15:48:16 -06003491 req->poll.done = true;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003492 if (error)
3493 io_cqring_fill_event(req, error);
3494 else
3495 io_cqring_fill_event(req, mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06003496 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003497}
3498
Jens Axboe561fb042019-10-24 07:25:42 -06003499static void io_poll_complete_work(struct io_wq_work **workptr)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003500{
Jens Axboe561fb042019-10-24 07:25:42 -06003501 struct io_wq_work *work = *workptr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003502 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
3503 struct io_poll_iocb *poll = &req->poll;
3504 struct poll_table_struct pt = { ._key = poll->events };
3505 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe89723d02019-11-05 15:32:58 -07003506 struct io_kiocb *nxt = NULL;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003507 __poll_t mask = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003508 int ret = 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003509
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003510 if (work->flags & IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06003511 WRITE_ONCE(poll->canceled, true);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003512 ret = -ECANCELED;
3513 } else if (READ_ONCE(poll->canceled)) {
3514 ret = -ECANCELED;
3515 }
Jens Axboe561fb042019-10-24 07:25:42 -06003516
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003517 if (ret != -ECANCELED)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003518 mask = vfs_poll(poll->file, &pt) & poll->events;
3519
3520 /*
3521 * Note that ->ki_cancel callers also delete iocb from active_reqs after
3522 * calling ->ki_cancel. We need the ctx_lock roundtrip here to
3523 * synchronize with them. In the cancellation case the list_del_init
3524 * itself is not actually needed, but harmless so we keep it in to
3525 * avoid further branches in the fast path.
3526 */
3527 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003528 if (!mask && ret != -ECANCELED) {
Jens Axboe392edb42019-12-09 17:52:20 -07003529 add_wait_queue(poll->head, &poll->wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003530 spin_unlock_irq(&ctx->completion_lock);
3531 return;
3532 }
Jens Axboe78076bb2019-12-04 19:56:40 -07003533 hash_del(&req->hash_node);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003534 io_poll_complete(req, mask, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003535 spin_unlock_irq(&ctx->completion_lock);
3536
Jens Axboe8c838782019-03-12 15:48:16 -06003537 io_cqring_ev_posted(ctx);
Jens Axboe89723d02019-11-05 15:32:58 -07003538
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003539 if (ret < 0)
3540 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003541 io_put_req_find_next(req, &nxt);
Jens Axboe89723d02019-11-05 15:32:58 -07003542 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07003543 io_wq_assign_next(workptr, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003544}
3545
Jens Axboee94f1412019-12-19 12:06:02 -07003546static void __io_poll_flush(struct io_ring_ctx *ctx, struct llist_node *nodes)
3547{
Jens Axboee94f1412019-12-19 12:06:02 -07003548 struct io_kiocb *req, *tmp;
Jens Axboe8237e042019-12-28 10:48:22 -07003549 struct req_batch rb;
Jens Axboee94f1412019-12-19 12:06:02 -07003550
Jens Axboec6ca97b302019-12-28 12:11:08 -07003551 rb.to_free = rb.need_iter = 0;
Jens Axboee94f1412019-12-19 12:06:02 -07003552 spin_lock_irq(&ctx->completion_lock);
3553 llist_for_each_entry_safe(req, tmp, nodes, llist_node) {
3554 hash_del(&req->hash_node);
3555 io_poll_complete(req, req->result, 0);
3556
Jens Axboe8237e042019-12-28 10:48:22 -07003557 if (refcount_dec_and_test(&req->refs) &&
3558 !io_req_multi_free(&rb, req)) {
3559 req->flags |= REQ_F_COMP_LOCKED;
3560 io_free_req(req);
Jens Axboee94f1412019-12-19 12:06:02 -07003561 }
3562 }
3563 spin_unlock_irq(&ctx->completion_lock);
3564
3565 io_cqring_ev_posted(ctx);
Jens Axboe8237e042019-12-28 10:48:22 -07003566 io_free_req_many(ctx, &rb);
Jens Axboee94f1412019-12-19 12:06:02 -07003567}
3568
3569static void io_poll_flush(struct io_wq_work **workptr)
3570{
3571 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3572 struct llist_node *nodes;
3573
3574 nodes = llist_del_all(&req->ctx->poll_llist);
3575 if (nodes)
3576 __io_poll_flush(req->ctx, nodes);
3577}
3578
Jens Axboef0b493e2020-02-01 21:30:11 -07003579static void io_poll_trigger_evfd(struct io_wq_work **workptr)
3580{
3581 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3582
3583 eventfd_signal(req->ctx->cq_ev_fd, 1);
3584 io_put_req(req);
3585}
3586
Jens Axboe221c5eb2019-01-17 09:41:58 -07003587static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
3588 void *key)
3589{
Jens Axboee9444752019-11-26 15:02:04 -07003590 struct io_poll_iocb *poll = wait->private;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003591 struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
3592 struct io_ring_ctx *ctx = req->ctx;
3593 __poll_t mask = key_to_poll(key);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003594
3595 /* for instances that support it check for an event match first: */
Jens Axboe8c838782019-03-12 15:48:16 -06003596 if (mask && !(mask & poll->events))
3597 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003598
Jens Axboe392edb42019-12-09 17:52:20 -07003599 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06003600
Jens Axboe7c9e7f02019-11-12 08:15:53 -07003601 /*
3602 * Run completion inline if we can. We're using trylock here because
3603 * we are violating the completion_lock -> poll wq lock ordering.
3604 * If we have a link timeout we're going to need the completion_lock
3605 * for finalizing the request, mark us as having grabbed that already.
3606 */
Jens Axboee94f1412019-12-19 12:06:02 -07003607 if (mask) {
3608 unsigned long flags;
Jens Axboe8c838782019-03-12 15:48:16 -06003609
Jens Axboee94f1412019-12-19 12:06:02 -07003610 if (llist_empty(&ctx->poll_llist) &&
3611 spin_trylock_irqsave(&ctx->completion_lock, flags)) {
Jens Axboef0b493e2020-02-01 21:30:11 -07003612 bool trigger_ev;
3613
Jens Axboee94f1412019-12-19 12:06:02 -07003614 hash_del(&req->hash_node);
3615 io_poll_complete(req, mask, 0);
Jens Axboee94f1412019-12-19 12:06:02 -07003616
Jens Axboef0b493e2020-02-01 21:30:11 -07003617 trigger_ev = io_should_trigger_evfd(ctx);
3618 if (trigger_ev && eventfd_signal_count()) {
3619 trigger_ev = false;
3620 req->work.func = io_poll_trigger_evfd;
3621 } else {
3622 req->flags |= REQ_F_COMP_LOCKED;
3623 io_put_req(req);
3624 req = NULL;
3625 }
3626 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3627 __io_cqring_ev_posted(ctx, trigger_ev);
Jens Axboee94f1412019-12-19 12:06:02 -07003628 } else {
3629 req->result = mask;
3630 req->llist_node.next = NULL;
3631 /* if the list wasn't empty, we're done */
3632 if (!llist_add(&req->llist_node, &ctx->poll_llist))
3633 req = NULL;
3634 else
3635 req->work.func = io_poll_flush;
3636 }
Jens Axboe8c838782019-03-12 15:48:16 -06003637 }
Jens Axboee94f1412019-12-19 12:06:02 -07003638 if (req)
3639 io_queue_async_work(req);
Jens Axboe8c838782019-03-12 15:48:16 -06003640
Jens Axboe221c5eb2019-01-17 09:41:58 -07003641 return 1;
3642}
3643
3644struct io_poll_table {
3645 struct poll_table_struct pt;
3646 struct io_kiocb *req;
3647 int error;
3648};
3649
3650static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
3651 struct poll_table_struct *p)
3652{
3653 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
3654
3655 if (unlikely(pt->req->poll.head)) {
3656 pt->error = -EINVAL;
3657 return;
3658 }
3659
3660 pt->error = 0;
3661 pt->req->poll.head = head;
Jens Axboe392edb42019-12-09 17:52:20 -07003662 add_wait_queue(head, &pt->req->poll.wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003663}
3664
Jens Axboeeac406c2019-11-14 12:09:58 -07003665static void io_poll_req_insert(struct io_kiocb *req)
3666{
3667 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07003668 struct hlist_head *list;
Jens Axboeeac406c2019-11-14 12:09:58 -07003669
Jens Axboe78076bb2019-12-04 19:56:40 -07003670 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
3671 hlist_add_head(&req->hash_node, list);
Jens Axboeeac406c2019-11-14 12:09:58 -07003672}
3673
Jens Axboe3529d8c2019-12-19 18:24:38 -07003674static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003675{
3676 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003677 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003678
3679 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3680 return -EINVAL;
3681 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
3682 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06003683 if (!poll->file)
3684 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003685
Jens Axboe221c5eb2019-01-17 09:41:58 -07003686 events = READ_ONCE(sqe->poll_events);
3687 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
Jens Axboe0969e782019-12-17 18:40:57 -07003688 return 0;
3689}
3690
3691static int io_poll_add(struct io_kiocb *req, struct io_kiocb **nxt)
3692{
3693 struct io_poll_iocb *poll = &req->poll;
3694 struct io_ring_ctx *ctx = req->ctx;
3695 struct io_poll_table ipt;
3696 bool cancel = false;
3697 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07003698
3699 INIT_IO_WORK(&req->work, io_poll_complete_work);
Jens Axboe78076bb2019-12-04 19:56:40 -07003700 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003701
Jens Axboe221c5eb2019-01-17 09:41:58 -07003702 poll->head = NULL;
Jens Axboe8c838782019-03-12 15:48:16 -06003703 poll->done = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003704 poll->canceled = false;
3705
3706 ipt.pt._qproc = io_poll_queue_proc;
3707 ipt.pt._key = poll->events;
3708 ipt.req = req;
3709 ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
3710
3711 /* initialized the list so that we can do list_empty checks */
Jens Axboe392edb42019-12-09 17:52:20 -07003712 INIT_LIST_HEAD(&poll->wait.entry);
3713 init_waitqueue_func_entry(&poll->wait, io_poll_wake);
3714 poll->wait.private = poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003715
Jens Axboe36703242019-07-25 10:20:18 -06003716 INIT_LIST_HEAD(&req->list);
3717
Jens Axboe221c5eb2019-01-17 09:41:58 -07003718 mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003719
3720 spin_lock_irq(&ctx->completion_lock);
Jens Axboe8c838782019-03-12 15:48:16 -06003721 if (likely(poll->head)) {
3722 spin_lock(&poll->head->lock);
Jens Axboe392edb42019-12-09 17:52:20 -07003723 if (unlikely(list_empty(&poll->wait.entry))) {
Jens Axboe8c838782019-03-12 15:48:16 -06003724 if (ipt.error)
3725 cancel = true;
3726 ipt.error = 0;
3727 mask = 0;
3728 }
3729 if (mask || ipt.error)
Jens Axboe392edb42019-12-09 17:52:20 -07003730 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06003731 else if (cancel)
3732 WRITE_ONCE(poll->canceled, true);
3733 else if (!poll->done) /* actually waiting for an event */
Jens Axboeeac406c2019-11-14 12:09:58 -07003734 io_poll_req_insert(req);
Jens Axboe8c838782019-03-12 15:48:16 -06003735 spin_unlock(&poll->head->lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003736 }
Jens Axboe8c838782019-03-12 15:48:16 -06003737 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06003738 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003739 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06003740 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07003741 spin_unlock_irq(&ctx->completion_lock);
3742
Jens Axboe8c838782019-03-12 15:48:16 -06003743 if (mask) {
3744 io_cqring_ev_posted(ctx);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003745 io_put_req_find_next(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003746 }
Jens Axboe8c838782019-03-12 15:48:16 -06003747 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003748}
3749
Jens Axboe5262f562019-09-17 12:26:57 -06003750static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
3751{
Jens Axboead8a48a2019-11-15 08:49:11 -07003752 struct io_timeout_data *data = container_of(timer,
3753 struct io_timeout_data, timer);
3754 struct io_kiocb *req = data->req;
3755 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06003756 unsigned long flags;
3757
Jens Axboe5262f562019-09-17 12:26:57 -06003758 atomic_inc(&ctx->cq_timeouts);
3759
3760 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08003761 /*
Jens Axboe11365042019-10-16 09:08:32 -06003762 * We could be racing with timeout deletion. If the list is empty,
3763 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08003764 */
Jens Axboe842f9612019-10-29 12:34:10 -06003765 if (!list_empty(&req->list)) {
Jens Axboe11365042019-10-16 09:08:32 -06003766 struct io_kiocb *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06003767
Jens Axboe11365042019-10-16 09:08:32 -06003768 /*
3769 * Adjust the reqs sequence before the current one because it
Brian Gianforcarod195a662019-12-13 03:09:50 -08003770 * will consume a slot in the cq_ring and the cq_tail
Jens Axboe11365042019-10-16 09:08:32 -06003771 * pointer will be increased, otherwise other timeout reqs may
3772 * return in advance without waiting for enough wait_nr.
3773 */
3774 prev = req;
3775 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
3776 prev->sequence++;
Jens Axboe11365042019-10-16 09:08:32 -06003777 list_del_init(&req->list);
Jens Axboe11365042019-10-16 09:08:32 -06003778 }
Jens Axboe842f9612019-10-29 12:34:10 -06003779
Jens Axboe78e19bb2019-11-06 15:21:34 -07003780 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06003781 io_commit_cqring(ctx);
3782 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3783
3784 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003785 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06003786 io_put_req(req);
3787 return HRTIMER_NORESTART;
3788}
3789
Jens Axboe47f46762019-11-09 17:43:02 -07003790static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
3791{
3792 struct io_kiocb *req;
3793 int ret = -ENOENT;
3794
3795 list_for_each_entry(req, &ctx->timeout_list, list) {
3796 if (user_data == req->user_data) {
3797 list_del_init(&req->list);
3798 ret = 0;
3799 break;
3800 }
3801 }
3802
3803 if (ret == -ENOENT)
3804 return ret;
3805
Jens Axboe2d283902019-12-04 11:08:05 -07003806 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe47f46762019-11-09 17:43:02 -07003807 if (ret == -1)
3808 return -EALREADY;
3809
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003810 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07003811 io_cqring_fill_event(req, -ECANCELED);
3812 io_put_req(req);
3813 return 0;
3814}
3815
Jens Axboe3529d8c2019-12-19 18:24:38 -07003816static int io_timeout_remove_prep(struct io_kiocb *req,
3817 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07003818{
Jens Axboeb29472e2019-12-17 18:50:29 -07003819 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3820 return -EINVAL;
3821 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
3822 return -EINVAL;
3823
3824 req->timeout.addr = READ_ONCE(sqe->addr);
3825 req->timeout.flags = READ_ONCE(sqe->timeout_flags);
3826 if (req->timeout.flags)
3827 return -EINVAL;
3828
Jens Axboeb29472e2019-12-17 18:50:29 -07003829 return 0;
3830}
3831
Jens Axboe11365042019-10-16 09:08:32 -06003832/*
3833 * Remove or update an existing timeout command
3834 */
Jens Axboefc4df992019-12-10 14:38:45 -07003835static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06003836{
3837 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07003838 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06003839
Jens Axboe11365042019-10-16 09:08:32 -06003840 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb29472e2019-12-17 18:50:29 -07003841 ret = io_timeout_cancel(ctx, req->timeout.addr);
Jens Axboe11365042019-10-16 09:08:32 -06003842
Jens Axboe47f46762019-11-09 17:43:02 -07003843 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06003844 io_commit_cqring(ctx);
3845 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06003846 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003847 if (ret < 0)
3848 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003849 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06003850 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06003851}
3852
Jens Axboe3529d8c2019-12-19 18:24:38 -07003853static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07003854 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06003855{
Jens Axboead8a48a2019-11-15 08:49:11 -07003856 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06003857 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06003858
Jens Axboead8a48a2019-11-15 08:49:11 -07003859 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06003860 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07003861 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06003862 return -EINVAL;
Jens Axboe2d283902019-12-04 11:08:05 -07003863 if (sqe->off && is_timeout_link)
3864 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06003865 flags = READ_ONCE(sqe->timeout_flags);
3866 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06003867 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06003868
Jens Axboe26a61672019-12-20 09:02:01 -07003869 req->timeout.count = READ_ONCE(sqe->off);
3870
Jens Axboe3529d8c2019-12-19 18:24:38 -07003871 if (!req->io && io_alloc_async_ctx(req))
Jens Axboe26a61672019-12-20 09:02:01 -07003872 return -ENOMEM;
3873
3874 data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07003875 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07003876 req->flags |= REQ_F_TIMEOUT;
3877
3878 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06003879 return -EFAULT;
3880
Jens Axboe11365042019-10-16 09:08:32 -06003881 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07003882 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06003883 else
Jens Axboead8a48a2019-11-15 08:49:11 -07003884 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06003885
Jens Axboead8a48a2019-11-15 08:49:11 -07003886 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
3887 return 0;
3888}
3889
Jens Axboefc4df992019-12-10 14:38:45 -07003890static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07003891{
3892 unsigned count;
3893 struct io_ring_ctx *ctx = req->ctx;
3894 struct io_timeout_data *data;
3895 struct list_head *entry;
3896 unsigned span = 0;
Jens Axboead8a48a2019-11-15 08:49:11 -07003897
Jens Axboe2d283902019-12-04 11:08:05 -07003898 data = &req->io->timeout;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003899
Jens Axboe5262f562019-09-17 12:26:57 -06003900 /*
3901 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07003902 * timeout event to be satisfied. If it isn't set, then this is
3903 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06003904 */
Jens Axboe26a61672019-12-20 09:02:01 -07003905 count = req->timeout.count;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003906 if (!count) {
3907 req->flags |= REQ_F_TIMEOUT_NOSEQ;
3908 spin_lock_irq(&ctx->completion_lock);
3909 entry = ctx->timeout_list.prev;
3910 goto add;
3911 }
Jens Axboe5262f562019-09-17 12:26:57 -06003912
3913 req->sequence = ctx->cached_sq_head + count - 1;
Jens Axboe2d283902019-12-04 11:08:05 -07003914 data->seq_offset = count;
Jens Axboe5262f562019-09-17 12:26:57 -06003915
3916 /*
3917 * Insertion sort, ensuring the first entry in the list is always
3918 * the one we need first.
3919 */
Jens Axboe5262f562019-09-17 12:26:57 -06003920 spin_lock_irq(&ctx->completion_lock);
3921 list_for_each_prev(entry, &ctx->timeout_list) {
3922 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
yangerkun5da0fb12019-10-15 21:59:29 +08003923 unsigned nxt_sq_head;
3924 long long tmp, tmp_nxt;
Jens Axboe2d283902019-12-04 11:08:05 -07003925 u32 nxt_offset = nxt->io->timeout.seq_offset;
Jens Axboe5262f562019-09-17 12:26:57 -06003926
Jens Axboe93bd25b2019-11-11 23:34:31 -07003927 if (nxt->flags & REQ_F_TIMEOUT_NOSEQ)
3928 continue;
3929
yangerkun5da0fb12019-10-15 21:59:29 +08003930 /*
3931 * Since cached_sq_head + count - 1 can overflow, use type long
3932 * long to store it.
3933 */
3934 tmp = (long long)ctx->cached_sq_head + count - 1;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +03003935 nxt_sq_head = nxt->sequence - nxt_offset + 1;
3936 tmp_nxt = (long long)nxt_sq_head + nxt_offset - 1;
yangerkun5da0fb12019-10-15 21:59:29 +08003937
3938 /*
3939 * cached_sq_head may overflow, and it will never overflow twice
3940 * once there is some timeout req still be valid.
3941 */
3942 if (ctx->cached_sq_head < nxt_sq_head)
yangerkun8b07a652019-10-17 12:12:35 +08003943 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08003944
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003945 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06003946 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003947
3948 /*
3949 * Sequence of reqs after the insert one and itself should
3950 * be adjusted because each timeout req consumes a slot.
3951 */
3952 span++;
3953 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06003954 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003955 req->sequence -= span;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003956add:
Jens Axboe5262f562019-09-17 12:26:57 -06003957 list_add(&req->list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07003958 data->timer.function = io_timeout_fn;
3959 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06003960 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06003961 return 0;
3962}
3963
Jens Axboe62755e32019-10-28 21:49:21 -06003964static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06003965{
Jens Axboe62755e32019-10-28 21:49:21 -06003966 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06003967
Jens Axboe62755e32019-10-28 21:49:21 -06003968 return req->user_data == (unsigned long) data;
3969}
3970
Jens Axboee977d6d2019-11-05 12:39:45 -07003971static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06003972{
Jens Axboe62755e32019-10-28 21:49:21 -06003973 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06003974 int ret = 0;
3975
Jens Axboe62755e32019-10-28 21:49:21 -06003976 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
3977 switch (cancel_ret) {
3978 case IO_WQ_CANCEL_OK:
3979 ret = 0;
3980 break;
3981 case IO_WQ_CANCEL_RUNNING:
3982 ret = -EALREADY;
3983 break;
3984 case IO_WQ_CANCEL_NOTFOUND:
3985 ret = -ENOENT;
3986 break;
3987 }
3988
Jens Axboee977d6d2019-11-05 12:39:45 -07003989 return ret;
3990}
3991
Jens Axboe47f46762019-11-09 17:43:02 -07003992static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
3993 struct io_kiocb *req, __u64 sqe_addr,
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003994 struct io_kiocb **nxt, int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07003995{
3996 unsigned long flags;
3997 int ret;
3998
3999 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
4000 if (ret != -ENOENT) {
4001 spin_lock_irqsave(&ctx->completion_lock, flags);
4002 goto done;
4003 }
4004
4005 spin_lock_irqsave(&ctx->completion_lock, flags);
4006 ret = io_timeout_cancel(ctx, sqe_addr);
4007 if (ret != -ENOENT)
4008 goto done;
4009 ret = io_poll_cancel(ctx, sqe_addr);
4010done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004011 if (!ret)
4012 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07004013 io_cqring_fill_event(req, ret);
4014 io_commit_cqring(ctx);
4015 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4016 io_cqring_ev_posted(ctx);
4017
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004018 if (ret < 0)
4019 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07004020 io_put_req_find_next(req, nxt);
4021}
4022
Jens Axboe3529d8c2019-12-19 18:24:38 -07004023static int io_async_cancel_prep(struct io_kiocb *req,
4024 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07004025{
Jens Axboefbf23842019-12-17 18:45:56 -07004026 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07004027 return -EINVAL;
4028 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
4029 sqe->cancel_flags)
4030 return -EINVAL;
4031
Jens Axboefbf23842019-12-17 18:45:56 -07004032 req->cancel.addr = READ_ONCE(sqe->addr);
4033 return 0;
4034}
4035
4036static int io_async_cancel(struct io_kiocb *req, struct io_kiocb **nxt)
4037{
4038 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07004039
4040 io_async_find_and_cancel(ctx, req, req->cancel.addr, nxt, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06004041 return 0;
4042}
4043
Jens Axboe05f3fb32019-12-09 11:22:50 -07004044static int io_files_update_prep(struct io_kiocb *req,
4045 const struct io_uring_sqe *sqe)
4046{
4047 if (sqe->flags || sqe->ioprio || sqe->rw_flags)
4048 return -EINVAL;
4049
4050 req->files_update.offset = READ_ONCE(sqe->off);
4051 req->files_update.nr_args = READ_ONCE(sqe->len);
4052 if (!req->files_update.nr_args)
4053 return -EINVAL;
4054 req->files_update.arg = READ_ONCE(sqe->addr);
4055 return 0;
4056}
4057
4058static int io_files_update(struct io_kiocb *req, bool force_nonblock)
4059{
4060 struct io_ring_ctx *ctx = req->ctx;
4061 struct io_uring_files_update up;
4062 int ret;
4063
Jens Axboef86cd202020-01-29 13:46:44 -07004064 if (force_nonblock)
Jens Axboe05f3fb32019-12-09 11:22:50 -07004065 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004066
4067 up.offset = req->files_update.offset;
4068 up.fds = req->files_update.arg;
4069
4070 mutex_lock(&ctx->uring_lock);
4071 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
4072 mutex_unlock(&ctx->uring_lock);
4073
4074 if (ret < 0)
4075 req_set_fail_links(req);
4076 io_cqring_add_event(req, ret);
4077 io_put_req(req);
4078 return 0;
4079}
4080
Jens Axboe3529d8c2019-12-19 18:24:38 -07004081static int io_req_defer_prep(struct io_kiocb *req,
4082 const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07004083{
Jens Axboee7815732019-12-17 19:45:06 -07004084 ssize_t ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07004085
Jens Axboef86cd202020-01-29 13:46:44 -07004086 if (io_op_defs[req->opcode].file_table) {
4087 ret = io_grab_files(req);
4088 if (unlikely(ret))
4089 return ret;
4090 }
4091
Jens Axboecccf0ee2020-01-27 16:34:48 -07004092 io_req_work_grab_env(req, &io_op_defs[req->opcode]);
4093
Jens Axboed625c6e2019-12-17 19:53:05 -07004094 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07004095 case IORING_OP_NOP:
4096 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004097 case IORING_OP_READV:
4098 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004099 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004100 ret = io_read_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07004101 break;
4102 case IORING_OP_WRITEV:
4103 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004104 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004105 ret = io_write_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07004106 break;
Jens Axboe0969e782019-12-17 18:40:57 -07004107 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004108 ret = io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07004109 break;
4110 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004111 ret = io_poll_remove_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07004112 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004113 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004114 ret = io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004115 break;
4116 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004117 ret = io_prep_sfr(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004118 break;
Jens Axboe03b12302019-12-02 18:50:25 -07004119 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004120 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004121 ret = io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07004122 break;
4123 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004124 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004125 ret = io_recvmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07004126 break;
Jens Axboef499a022019-12-02 16:28:46 -07004127 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004128 ret = io_connect_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07004129 break;
Jens Axboe2d283902019-12-04 11:08:05 -07004130 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004131 ret = io_timeout_prep(req, sqe, false);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004132 break;
Jens Axboeb29472e2019-12-17 18:50:29 -07004133 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004134 ret = io_timeout_remove_prep(req, sqe);
Jens Axboeb29472e2019-12-17 18:50:29 -07004135 break;
Jens Axboefbf23842019-12-17 18:45:56 -07004136 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004137 ret = io_async_cancel_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07004138 break;
Jens Axboe2d283902019-12-04 11:08:05 -07004139 case IORING_OP_LINK_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004140 ret = io_timeout_prep(req, sqe, true);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004141 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004142 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004143 ret = io_accept_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004144 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07004145 case IORING_OP_FALLOCATE:
4146 ret = io_fallocate_prep(req, sqe);
4147 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004148 case IORING_OP_OPENAT:
4149 ret = io_openat_prep(req, sqe);
4150 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07004151 case IORING_OP_CLOSE:
4152 ret = io_close_prep(req, sqe);
4153 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004154 case IORING_OP_FILES_UPDATE:
4155 ret = io_files_update_prep(req, sqe);
4156 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004157 case IORING_OP_STATX:
4158 ret = io_statx_prep(req, sqe);
4159 break;
Jens Axboe4840e412019-12-25 22:03:45 -07004160 case IORING_OP_FADVISE:
4161 ret = io_fadvise_prep(req, sqe);
4162 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07004163 case IORING_OP_MADVISE:
4164 ret = io_madvise_prep(req, sqe);
4165 break;
Jens Axboecebdb982020-01-08 17:59:24 -07004166 case IORING_OP_OPENAT2:
4167 ret = io_openat2_prep(req, sqe);
4168 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004169 case IORING_OP_EPOLL_CTL:
4170 ret = io_epoll_ctl_prep(req, sqe);
4171 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004172 default:
Jens Axboee7815732019-12-17 19:45:06 -07004173 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
4174 req->opcode);
4175 ret = -EINVAL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004176 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004177 }
4178
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004179 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07004180}
4181
Jens Axboe3529d8c2019-12-19 18:24:38 -07004182static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboede0617e2019-04-06 21:51:27 -06004183{
Jackie Liua197f662019-11-08 08:09:12 -07004184 struct io_ring_ctx *ctx = req->ctx;
Jens Axboef67676d2019-12-02 11:03:47 -07004185 int ret;
Jens Axboede0617e2019-04-06 21:51:27 -06004186
Bob Liu9d858b22019-11-13 18:06:25 +08004187 /* Still need defer if there is pending req in defer list. */
4188 if (!req_need_defer(req) && list_empty(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06004189 return 0;
4190
Jens Axboe3529d8c2019-12-19 18:24:38 -07004191 if (!req->io && io_alloc_async_ctx(req))
Jens Axboede0617e2019-04-06 21:51:27 -06004192 return -EAGAIN;
4193
Jens Axboe3529d8c2019-12-19 18:24:38 -07004194 ret = io_req_defer_prep(req, sqe);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004195 if (ret < 0)
Jens Axboe2d283902019-12-04 11:08:05 -07004196 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07004197
Jens Axboede0617e2019-04-06 21:51:27 -06004198 spin_lock_irq(&ctx->completion_lock);
Bob Liu9d858b22019-11-13 18:06:25 +08004199 if (!req_need_defer(req) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06004200 spin_unlock_irq(&ctx->completion_lock);
Jens Axboede0617e2019-04-06 21:51:27 -06004201 return 0;
4202 }
4203
Jens Axboe915967f2019-11-21 09:01:20 -07004204 trace_io_uring_defer(ctx, req, req->user_data);
Jens Axboede0617e2019-04-06 21:51:27 -06004205 list_add_tail(&req->list, &ctx->defer_list);
4206 spin_unlock_irq(&ctx->completion_lock);
4207 return -EIOCBQUEUED;
4208}
4209
Jens Axboe3529d8c2019-12-19 18:24:38 -07004210static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
4211 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004212{
Jackie Liua197f662019-11-08 08:09:12 -07004213 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07004214 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004215
Jens Axboed625c6e2019-12-17 19:53:05 -07004216 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07004217 case IORING_OP_NOP:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004218 ret = io_nop(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004219 break;
4220 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004221 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004222 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004223 if (sqe) {
4224 ret = io_read_prep(req, sqe, force_nonblock);
4225 if (ret < 0)
4226 break;
4227 }
Pavel Begunkov267bc902019-11-07 01:41:08 +03004228 ret = io_read(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004229 break;
4230 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07004231 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004232 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004233 if (sqe) {
4234 ret = io_write_prep(req, sqe, force_nonblock);
4235 if (ret < 0)
4236 break;
4237 }
Pavel Begunkov267bc902019-11-07 01:41:08 +03004238 ret = io_write(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004239 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004240 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004241 if (sqe) {
4242 ret = io_prep_fsync(req, sqe);
4243 if (ret < 0)
4244 break;
4245 }
Jens Axboefc4df992019-12-10 14:38:45 -07004246 ret = io_fsync(req, nxt, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004247 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004248 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004249 if (sqe) {
4250 ret = io_poll_add_prep(req, sqe);
4251 if (ret)
4252 break;
4253 }
Jens Axboefc4df992019-12-10 14:38:45 -07004254 ret = io_poll_add(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004255 break;
4256 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004257 if (sqe) {
4258 ret = io_poll_remove_prep(req, sqe);
4259 if (ret < 0)
4260 break;
4261 }
Jens Axboefc4df992019-12-10 14:38:45 -07004262 ret = io_poll_remove(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004263 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004264 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004265 if (sqe) {
4266 ret = io_prep_sfr(req, sqe);
4267 if (ret < 0)
4268 break;
4269 }
Jens Axboefc4df992019-12-10 14:38:45 -07004270 ret = io_sync_file_range(req, nxt, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004271 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004272 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004273 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004274 if (sqe) {
4275 ret = io_sendmsg_prep(req, sqe);
4276 if (ret < 0)
4277 break;
4278 }
Jens Axboefddafac2020-01-04 20:19:44 -07004279 if (req->opcode == IORING_OP_SENDMSG)
4280 ret = io_sendmsg(req, nxt, force_nonblock);
4281 else
4282 ret = io_send(req, nxt, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004283 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06004284 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004285 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004286 if (sqe) {
4287 ret = io_recvmsg_prep(req, sqe);
4288 if (ret)
4289 break;
4290 }
Jens Axboefddafac2020-01-04 20:19:44 -07004291 if (req->opcode == IORING_OP_RECVMSG)
4292 ret = io_recvmsg(req, nxt, force_nonblock);
4293 else
4294 ret = io_recv(req, nxt, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06004295 break;
Jens Axboe5262f562019-09-17 12:26:57 -06004296 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004297 if (sqe) {
4298 ret = io_timeout_prep(req, sqe, false);
4299 if (ret)
4300 break;
4301 }
Jens Axboefc4df992019-12-10 14:38:45 -07004302 ret = io_timeout(req);
Jens Axboe5262f562019-09-17 12:26:57 -06004303 break;
Jens Axboe11365042019-10-16 09:08:32 -06004304 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004305 if (sqe) {
4306 ret = io_timeout_remove_prep(req, sqe);
4307 if (ret)
4308 break;
4309 }
Jens Axboefc4df992019-12-10 14:38:45 -07004310 ret = io_timeout_remove(req);
Jens Axboe11365042019-10-16 09:08:32 -06004311 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06004312 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004313 if (sqe) {
4314 ret = io_accept_prep(req, sqe);
4315 if (ret)
4316 break;
4317 }
Jens Axboefc4df992019-12-10 14:38:45 -07004318 ret = io_accept(req, nxt, force_nonblock);
Jens Axboe17f2fe32019-10-17 14:42:58 -06004319 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004320 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004321 if (sqe) {
4322 ret = io_connect_prep(req, sqe);
4323 if (ret)
4324 break;
4325 }
Jens Axboefc4df992019-12-10 14:38:45 -07004326 ret = io_connect(req, nxt, force_nonblock);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004327 break;
Jens Axboe62755e32019-10-28 21:49:21 -06004328 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004329 if (sqe) {
4330 ret = io_async_cancel_prep(req, sqe);
4331 if (ret)
4332 break;
4333 }
Jens Axboefc4df992019-12-10 14:38:45 -07004334 ret = io_async_cancel(req, nxt);
Jens Axboe62755e32019-10-28 21:49:21 -06004335 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07004336 case IORING_OP_FALLOCATE:
4337 if (sqe) {
4338 ret = io_fallocate_prep(req, sqe);
4339 if (ret)
4340 break;
4341 }
4342 ret = io_fallocate(req, nxt, force_nonblock);
4343 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004344 case IORING_OP_OPENAT:
4345 if (sqe) {
4346 ret = io_openat_prep(req, sqe);
4347 if (ret)
4348 break;
4349 }
4350 ret = io_openat(req, nxt, force_nonblock);
4351 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07004352 case IORING_OP_CLOSE:
4353 if (sqe) {
4354 ret = io_close_prep(req, sqe);
4355 if (ret)
4356 break;
4357 }
4358 ret = io_close(req, nxt, force_nonblock);
4359 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004360 case IORING_OP_FILES_UPDATE:
4361 if (sqe) {
4362 ret = io_files_update_prep(req, sqe);
4363 if (ret)
4364 break;
4365 }
4366 ret = io_files_update(req, force_nonblock);
4367 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004368 case IORING_OP_STATX:
4369 if (sqe) {
4370 ret = io_statx_prep(req, sqe);
4371 if (ret)
4372 break;
4373 }
4374 ret = io_statx(req, nxt, force_nonblock);
4375 break;
Jens Axboe4840e412019-12-25 22:03:45 -07004376 case IORING_OP_FADVISE:
4377 if (sqe) {
4378 ret = io_fadvise_prep(req, sqe);
4379 if (ret)
4380 break;
4381 }
4382 ret = io_fadvise(req, nxt, force_nonblock);
4383 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07004384 case IORING_OP_MADVISE:
4385 if (sqe) {
4386 ret = io_madvise_prep(req, sqe);
4387 if (ret)
4388 break;
4389 }
4390 ret = io_madvise(req, nxt, force_nonblock);
4391 break;
Jens Axboecebdb982020-01-08 17:59:24 -07004392 case IORING_OP_OPENAT2:
4393 if (sqe) {
4394 ret = io_openat2_prep(req, sqe);
4395 if (ret)
4396 break;
4397 }
4398 ret = io_openat2(req, nxt, force_nonblock);
4399 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004400 case IORING_OP_EPOLL_CTL:
4401 if (sqe) {
4402 ret = io_epoll_ctl_prep(req, sqe);
4403 if (ret)
4404 break;
4405 }
4406 ret = io_epoll_ctl(req, nxt, force_nonblock);
4407 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004408 default:
4409 ret = -EINVAL;
4410 break;
4411 }
4412
Jens Axboedef596e2019-01-09 08:59:42 -07004413 if (ret)
4414 return ret;
4415
4416 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe11ba8202020-01-15 21:51:17 -07004417 const bool in_async = io_wq_current_is_worker();
4418
Jens Axboe9e645e112019-05-10 16:07:28 -06004419 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07004420 return -EAGAIN;
4421
Jens Axboe11ba8202020-01-15 21:51:17 -07004422 /* workqueue context doesn't hold uring_lock, grab it now */
4423 if (in_async)
4424 mutex_lock(&ctx->uring_lock);
4425
Jens Axboedef596e2019-01-09 08:59:42 -07004426 io_iopoll_req_issued(req);
Jens Axboe11ba8202020-01-15 21:51:17 -07004427
4428 if (in_async)
4429 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07004430 }
4431
4432 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004433}
4434
Jens Axboe561fb042019-10-24 07:25:42 -06004435static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboe31b51512019-01-18 22:56:34 -07004436{
Jens Axboe561fb042019-10-24 07:25:42 -06004437 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004438 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe561fb042019-10-24 07:25:42 -06004439 struct io_kiocb *nxt = NULL;
4440 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004441
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07004442 /* if NO_CANCEL is set, we must still run the work */
4443 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
4444 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06004445 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07004446 }
Jens Axboe31b51512019-01-18 22:56:34 -07004447
Jens Axboe561fb042019-10-24 07:25:42 -06004448 if (!ret) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004449 req->has_user = (work->flags & IO_WQ_WORK_HAS_MM) != 0;
4450 req->in_async = true;
Jens Axboe561fb042019-10-24 07:25:42 -06004451 do {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004452 ret = io_issue_sqe(req, NULL, &nxt, false);
Jens Axboe561fb042019-10-24 07:25:42 -06004453 /*
4454 * We can get EAGAIN for polled IO even though we're
4455 * forcing a sync submission from here, since we can't
4456 * wait for request slots on the block side.
4457 */
4458 if (ret != -EAGAIN)
4459 break;
4460 cond_resched();
4461 } while (1);
4462 }
Jens Axboe31b51512019-01-18 22:56:34 -07004463
Jens Axboe561fb042019-10-24 07:25:42 -06004464 /* drop submission reference */
Jackie Liuec9c02a2019-11-08 23:50:36 +08004465 io_put_req(req);
Jens Axboe817869d2019-04-30 14:44:05 -06004466
Jens Axboe561fb042019-10-24 07:25:42 -06004467 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004468 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07004469 io_cqring_add_event(req, ret);
Jens Axboe817869d2019-04-30 14:44:05 -06004470 io_put_req(req);
Jens Axboeedafcce2019-01-09 09:16:05 -07004471 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004472
Jens Axboe561fb042019-10-24 07:25:42 -06004473 /* if a dependent link is ready, pass it back */
Jens Axboe78912932020-01-14 22:09:06 -07004474 if (!ret && nxt)
4475 io_wq_assign_next(workptr, nxt);
Jens Axboe31b51512019-01-18 22:56:34 -07004476}
Jens Axboe2b188cc2019-01-07 10:46:33 -07004477
Jens Axboe15b71ab2019-12-11 11:20:36 -07004478static int io_req_needs_file(struct io_kiocb *req, int fd)
Jens Axboe9e3aa612019-12-11 15:55:43 -07004479{
Jens Axboed3656342019-12-18 09:50:26 -07004480 if (!io_op_defs[req->opcode].needs_file)
Jens Axboe9e3aa612019-12-11 15:55:43 -07004481 return 0;
Jens Axboed3656342019-12-18 09:50:26 -07004482 if (fd == -1 && io_op_defs[req->opcode].fd_non_neg)
4483 return 0;
4484 return 1;
Jens Axboe09bb8392019-03-13 12:39:28 -06004485}
4486
Jens Axboe65e19f52019-10-26 07:20:21 -06004487static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
4488 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06004489{
Jens Axboe65e19f52019-10-26 07:20:21 -06004490 struct fixed_file_table *table;
4491
Jens Axboe05f3fb32019-12-09 11:22:50 -07004492 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
4493 return table->files[index & IORING_FILE_TABLE_MASK];;
Jens Axboe65e19f52019-10-26 07:20:21 -06004494}
4495
Jens Axboe3529d8c2019-12-19 18:24:38 -07004496static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
4497 const struct io_uring_sqe *sqe)
Jens Axboe09bb8392019-03-13 12:39:28 -06004498{
Jackie Liua197f662019-11-08 08:09:12 -07004499 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe09bb8392019-03-13 12:39:28 -06004500 unsigned flags;
Jens Axboed3656342019-12-18 09:50:26 -07004501 int fd;
Jens Axboe09bb8392019-03-13 12:39:28 -06004502
Jens Axboe3529d8c2019-12-19 18:24:38 -07004503 flags = READ_ONCE(sqe->flags);
4504 fd = READ_ONCE(sqe->fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06004505
Jens Axboed3656342019-12-18 09:50:26 -07004506 if (!io_req_needs_file(req, fd))
4507 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06004508
4509 if (flags & IOSQE_FIXED_FILE) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07004510 if (unlikely(!ctx->file_data ||
Jens Axboe09bb8392019-03-13 12:39:28 -06004511 (unsigned) fd >= ctx->nr_user_files))
4512 return -EBADF;
Jens Axboeb7620122019-10-26 07:22:55 -06004513 fd = array_index_nospec(fd, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06004514 req->file = io_file_from_index(ctx, fd);
4515 if (!req->file)
Jens Axboe08a45172019-10-03 08:11:03 -06004516 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06004517 req->flags |= REQ_F_FIXED_FILE;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004518 percpu_ref_get(&ctx->file_data->refs);
Jens Axboe09bb8392019-03-13 12:39:28 -06004519 } else {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004520 if (req->needs_fixed_file)
Jens Axboe09bb8392019-03-13 12:39:28 -06004521 return -EBADF;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004522 trace_io_uring_file_get(ctx, fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06004523 req->file = io_file_get(state, fd);
4524 if (unlikely(!req->file))
4525 return -EBADF;
4526 }
4527
4528 return 0;
4529}
4530
Jackie Liua197f662019-11-08 08:09:12 -07004531static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004532{
Jens Axboefcb323c2019-10-24 12:39:47 -06004533 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07004534 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06004535
Jens Axboef86cd202020-01-29 13:46:44 -07004536 if (req->work.files)
4537 return 0;
Pavel Begunkovb14cca02020-01-17 04:45:59 +03004538 if (!ctx->ring_file)
Jens Axboeb5dba592019-12-11 14:02:38 -07004539 return -EBADF;
4540
Jens Axboefcb323c2019-10-24 12:39:47 -06004541 rcu_read_lock();
4542 spin_lock_irq(&ctx->inflight_lock);
4543 /*
4544 * We use the f_ops->flush() handler to ensure that we can flush
4545 * out work accessing these files if the fd is closed. Check if
4546 * the fd has changed since we started down this path, and disallow
4547 * this operation if it has.
4548 */
Pavel Begunkovb14cca02020-01-17 04:45:59 +03004549 if (fcheck(ctx->ring_fd) == ctx->ring_file) {
Jens Axboefcb323c2019-10-24 12:39:47 -06004550 list_add(&req->inflight_entry, &ctx->inflight_list);
4551 req->flags |= REQ_F_INFLIGHT;
4552 req->work.files = current->files;
4553 ret = 0;
4554 }
4555 spin_unlock_irq(&ctx->inflight_lock);
4556 rcu_read_unlock();
4557
4558 return ret;
4559}
4560
Jens Axboe2665abf2019-11-05 12:40:47 -07004561static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
4562{
Jens Axboead8a48a2019-11-15 08:49:11 -07004563 struct io_timeout_data *data = container_of(timer,
4564 struct io_timeout_data, timer);
4565 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07004566 struct io_ring_ctx *ctx = req->ctx;
4567 struct io_kiocb *prev = NULL;
4568 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07004569
4570 spin_lock_irqsave(&ctx->completion_lock, flags);
4571
4572 /*
4573 * We don't expect the list to be empty, that will only happen if we
4574 * race with the completion of the linked work.
4575 */
Pavel Begunkov44932332019-12-05 16:16:35 +03004576 if (!list_empty(&req->link_list)) {
4577 prev = list_entry(req->link_list.prev, struct io_kiocb,
4578 link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07004579 if (refcount_inc_not_zero(&prev->refs)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03004580 list_del_init(&req->link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07004581 prev->flags &= ~REQ_F_LINK_TIMEOUT;
4582 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07004583 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07004584 }
4585
4586 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4587
4588 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004589 req_set_fail_links(prev);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004590 io_async_find_and_cancel(ctx, req, prev->user_data, NULL,
4591 -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07004592 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07004593 } else {
4594 io_cqring_add_event(req, -ETIME);
4595 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07004596 }
Jens Axboe2665abf2019-11-05 12:40:47 -07004597 return HRTIMER_NORESTART;
4598}
4599
Jens Axboead8a48a2019-11-15 08:49:11 -07004600static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07004601{
Jens Axboe76a46e02019-11-10 23:34:16 -07004602 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07004603
Jens Axboe76a46e02019-11-10 23:34:16 -07004604 /*
4605 * If the list is now empty, then our linked request finished before
4606 * we got a chance to setup the timer
4607 */
4608 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov44932332019-12-05 16:16:35 +03004609 if (!list_empty(&req->link_list)) {
Jens Axboe2d283902019-12-04 11:08:05 -07004610 struct io_timeout_data *data = &req->io->timeout;
Jens Axboe94ae5e72019-11-14 19:39:52 -07004611
Jens Axboead8a48a2019-11-15 08:49:11 -07004612 data->timer.function = io_link_timeout_fn;
4613 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
4614 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07004615 }
Jens Axboe76a46e02019-11-10 23:34:16 -07004616 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07004617
Jens Axboe2665abf2019-11-05 12:40:47 -07004618 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07004619 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07004620}
4621
Jens Axboead8a48a2019-11-15 08:49:11 -07004622static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07004623{
4624 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004625
Jens Axboe2665abf2019-11-05 12:40:47 -07004626 if (!(req->flags & REQ_F_LINK))
4627 return NULL;
4628
Pavel Begunkov44932332019-12-05 16:16:35 +03004629 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
4630 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07004631 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07004632 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07004633
Jens Axboe76a46e02019-11-10 23:34:16 -07004634 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07004635 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07004636}
4637
Jens Axboe3529d8c2019-12-19 18:24:38 -07004638static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004639{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004640 struct io_kiocb *linked_timeout;
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004641 struct io_kiocb *nxt = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004642 int ret;
4643
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004644again:
4645 linked_timeout = io_prep_linked_timeout(req);
4646
Jens Axboe3529d8c2019-12-19 18:24:38 -07004647 ret = io_issue_sqe(req, sqe, &nxt, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06004648
4649 /*
4650 * We async punt it if the file wasn't marked NOWAIT, or if the file
4651 * doesn't support non-blocking read/write attempts
4652 */
4653 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
4654 (req->flags & REQ_F_MUST_PUNT))) {
Pavel Begunkov86a761f2020-01-22 23:09:36 +03004655punt:
Jens Axboef86cd202020-01-29 13:46:44 -07004656 if (io_op_defs[req->opcode].file_table) {
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03004657 ret = io_grab_files(req);
4658 if (ret)
4659 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004660 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03004661
4662 /*
4663 * Queued up for async execution, worker will release
4664 * submit reference when the iocb is actually submitted.
4665 */
4666 io_queue_async_work(req);
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004667 goto done_req;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004668 }
Jens Axboee65ef562019-03-12 10:16:44 -06004669
Jens Axboefcb323c2019-10-24 12:39:47 -06004670err:
Jens Axboee65ef562019-03-12 10:16:44 -06004671 /* drop submission reference */
4672 io_put_req(req);
4673
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004674 if (linked_timeout) {
Jens Axboe76a46e02019-11-10 23:34:16 -07004675 if (!ret)
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004676 io_queue_linked_timeout(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07004677 else
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004678 io_put_req(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07004679 }
4680
Jens Axboee65ef562019-03-12 10:16:44 -06004681 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06004682 if (ret) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07004683 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004684 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06004685 io_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06004686 }
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004687done_req:
4688 if (nxt) {
4689 req = nxt;
4690 nxt = NULL;
Pavel Begunkov86a761f2020-01-22 23:09:36 +03004691
4692 if (req->flags & REQ_F_FORCE_ASYNC)
4693 goto punt;
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004694 goto again;
4695 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004696}
4697
Jens Axboe3529d8c2019-12-19 18:24:38 -07004698static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jackie Liu4fe2c962019-09-09 20:50:40 +08004699{
4700 int ret;
4701
Jens Axboe3529d8c2019-12-19 18:24:38 -07004702 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004703 if (ret) {
4704 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03004705fail_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004706 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004707 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07004708 io_double_put_req(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004709 }
Pavel Begunkov25508782019-12-30 21:24:47 +03004710 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Pavel Begunkov11185912020-01-22 23:09:35 +03004711 ret = io_req_defer_prep(req, sqe);
4712 if (unlikely(ret < 0))
4713 goto fail_req;
Jens Axboece35a472019-12-17 08:04:44 -07004714 /*
4715 * Never try inline submit of IOSQE_ASYNC is set, go straight
4716 * to async execution.
4717 */
4718 req->work.flags |= IO_WQ_WORK_CONCURRENT;
4719 io_queue_async_work(req);
4720 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004721 __io_queue_sqe(req, sqe);
Jens Axboece35a472019-12-17 08:04:44 -07004722 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08004723}
4724
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004725static inline void io_queue_link_head(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08004726{
Jens Axboe94ae5e72019-11-14 19:39:52 -07004727 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004728 io_cqring_add_event(req, -ECANCELED);
4729 io_double_put_req(req);
4730 } else
Jens Axboe3529d8c2019-12-19 18:24:38 -07004731 io_queue_sqe(req, NULL);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004732}
4733
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004734#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
Jens Axboece35a472019-12-17 08:04:44 -07004735 IOSQE_IO_HARDLINK | IOSQE_ASYNC)
Jens Axboe9e645e112019-05-10 16:07:28 -06004736
Jens Axboe3529d8c2019-12-19 18:24:38 -07004737static bool io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
4738 struct io_submit_state *state, struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06004739{
Jens Axboe75c6a032020-01-28 10:15:23 -07004740 const struct cred *old_creds = NULL;
Jackie Liua197f662019-11-08 08:09:12 -07004741 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004742 unsigned int sqe_flags;
Jens Axboe75c6a032020-01-28 10:15:23 -07004743 int ret, id;
Jens Axboe9e645e112019-05-10 16:07:28 -06004744
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004745 sqe_flags = READ_ONCE(sqe->flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06004746
4747 /* enforce forwards compatibility on users */
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004748 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06004749 ret = -EINVAL;
Pavel Begunkov196be952019-11-07 01:41:06 +03004750 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06004751 }
4752
Jens Axboe75c6a032020-01-28 10:15:23 -07004753 id = READ_ONCE(sqe->personality);
4754 if (id) {
4755 const struct cred *personality_creds;
4756
4757 personality_creds = idr_find(&ctx->personality_idr, id);
4758 if (unlikely(!personality_creds)) {
4759 ret = -EINVAL;
4760 goto err_req;
4761 }
4762 old_creds = override_creds(personality_creds);
4763 }
4764
Pavel Begunkov6b47ee62020-01-18 20:22:41 +03004765 /* same numerical values with corresponding REQ_F_*, safe to copy */
4766 req->flags |= sqe_flags & (IOSQE_IO_DRAIN|IOSQE_IO_HARDLINK|
4767 IOSQE_ASYNC);
Jens Axboe9e645e112019-05-10 16:07:28 -06004768
Jens Axboe3529d8c2019-12-19 18:24:38 -07004769 ret = io_req_set_file(state, req, sqe);
Jens Axboe9e645e112019-05-10 16:07:28 -06004770 if (unlikely(ret)) {
4771err_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004772 io_cqring_add_event(req, ret);
4773 io_double_put_req(req);
Jens Axboe75c6a032020-01-28 10:15:23 -07004774 if (old_creds)
4775 revert_creds(old_creds);
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004776 return false;
Jens Axboe9e645e112019-05-10 16:07:28 -06004777 }
4778
Jens Axboe9e645e112019-05-10 16:07:28 -06004779 /*
4780 * If we already have a head request, queue this one for async
4781 * submittal once the head completes. If we don't have a head but
4782 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
4783 * submitted sync once the chain is complete. If none of those
4784 * conditions are true (normal request), then just queue it.
4785 */
4786 if (*link) {
Pavel Begunkov9d763772019-12-17 02:22:07 +03004787 struct io_kiocb *head = *link;
Jens Axboe9e645e112019-05-10 16:07:28 -06004788
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03004789 /*
4790 * Taking sequential execution of a link, draining both sides
4791 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
4792 * requests in the link. So, it drains the head and the
4793 * next after the link request. The last one is done via
4794 * drain_next flag to persist the effect across calls.
4795 */
Pavel Begunkov711be032020-01-17 03:57:59 +03004796 if (sqe_flags & IOSQE_IO_DRAIN) {
4797 head->flags |= REQ_F_IO_DRAIN;
4798 ctx->drain_next = 1;
4799 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004800 if (io_alloc_async_ctx(req)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06004801 ret = -EAGAIN;
4802 goto err_req;
4803 }
4804
Jens Axboe3529d8c2019-12-19 18:24:38 -07004805 ret = io_req_defer_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07004806 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004807 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03004808 head->flags |= REQ_F_FAIL_LINK;
Jens Axboef67676d2019-12-02 11:03:47 -07004809 goto err_req;
Jens Axboe2d283902019-12-04 11:08:05 -07004810 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03004811 trace_io_uring_link(ctx, req, head);
4812 list_add_tail(&req->link_list, &head->link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06004813
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004814 /* last request of a link, enqueue the link */
4815 if (!(sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK))) {
4816 io_queue_link_head(head);
4817 *link = NULL;
4818 }
Jens Axboe9e645e112019-05-10 16:07:28 -06004819 } else {
Pavel Begunkov711be032020-01-17 03:57:59 +03004820 if (unlikely(ctx->drain_next)) {
4821 req->flags |= REQ_F_IO_DRAIN;
4822 req->ctx->drain_next = 0;
4823 }
4824 if (sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK)) {
4825 req->flags |= REQ_F_LINK;
Pavel Begunkov711be032020-01-17 03:57:59 +03004826 INIT_LIST_HEAD(&req->link_list);
4827 ret = io_req_defer_prep(req, sqe);
4828 if (ret)
4829 req->flags |= REQ_F_FAIL_LINK;
4830 *link = req;
4831 } else {
4832 io_queue_sqe(req, sqe);
4833 }
Jens Axboe9e645e112019-05-10 16:07:28 -06004834 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004835
Jens Axboe75c6a032020-01-28 10:15:23 -07004836 if (old_creds)
4837 revert_creds(old_creds);
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004838 return true;
Jens Axboe9e645e112019-05-10 16:07:28 -06004839}
4840
Jens Axboe9a56a232019-01-09 09:06:50 -07004841/*
4842 * Batched submission is done, ensure local IO is flushed out.
4843 */
4844static void io_submit_state_end(struct io_submit_state *state)
4845{
4846 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06004847 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07004848 if (state->free_reqs)
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03004849 kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs);
Jens Axboe9a56a232019-01-09 09:06:50 -07004850}
4851
4852/*
4853 * Start submission side cache.
4854 */
4855static void io_submit_state_start(struct io_submit_state *state,
Jackie Liu22efde52019-12-02 17:14:52 +08004856 unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07004857{
4858 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07004859 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07004860 state->file = NULL;
4861 state->ios_left = max_ios;
4862}
4863
Jens Axboe2b188cc2019-01-07 10:46:33 -07004864static void io_commit_sqring(struct io_ring_ctx *ctx)
4865{
Hristo Venev75b28af2019-08-26 17:23:46 +00004866 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004867
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03004868 /*
4869 * Ensure any loads from the SQEs are done at this point,
4870 * since once we write the new head, the application could
4871 * write new data to them.
4872 */
4873 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004874}
4875
4876/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07004877 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07004878 * that is mapped by userspace. This means that care needs to be taken to
4879 * ensure that reads are stable, as we cannot rely on userspace always
4880 * being a good citizen. If members of the sqe are validated and then later
4881 * used, it's important that those reads are done through READ_ONCE() to
4882 * prevent a re-load down the line.
4883 */
Jens Axboe3529d8c2019-12-19 18:24:38 -07004884static bool io_get_sqring(struct io_ring_ctx *ctx, struct io_kiocb *req,
4885 const struct io_uring_sqe **sqe_ptr)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004886{
Hristo Venev75b28af2019-08-26 17:23:46 +00004887 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004888 unsigned head;
4889
4890 /*
4891 * The cached sq head (or cq tail) serves two purposes:
4892 *
4893 * 1) allows us to batch the cost of updating the user visible
4894 * head updates.
4895 * 2) allows the kernel side to track the head on its own, even
4896 * though the application is the one updating it.
4897 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03004898 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03004899 if (likely(head < ctx->sq_entries)) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004900 /*
4901 * All io need record the previous position, if LINK vs DARIN,
4902 * it can be used to mark the position of the first IO in the
4903 * link list.
4904 */
4905 req->sequence = ctx->cached_sq_head;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004906 *sqe_ptr = &ctx->sq_sqes[head];
4907 req->opcode = READ_ONCE((*sqe_ptr)->opcode);
4908 req->user_data = READ_ONCE((*sqe_ptr)->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004909 ctx->cached_sq_head++;
4910 return true;
4911 }
4912
4913 /* drop invalid entries */
4914 ctx->cached_sq_head++;
Jens Axboe498ccd92019-10-25 10:04:25 -06004915 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03004916 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004917 return false;
4918}
4919
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004920static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004921 struct file *ring_file, int ring_fd,
4922 struct mm_struct **mm, bool async)
Jens Axboe6c271ce2019-01-10 11:22:30 -07004923{
4924 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06004925 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06004926 int i, submitted = 0;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03004927 bool mm_fault = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004928
Jens Axboec4a2ed72019-11-21 21:01:26 -07004929 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07004930 if (test_bit(0, &ctx->sq_check_overflow)) {
4931 if (!list_empty(&ctx->cq_overflow_list) &&
4932 !io_cqring_overflow_flush(ctx, false))
4933 return -EBUSY;
4934 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07004935
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03004936 /* make sure SQ entry isn't read before tail */
4937 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03004938
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03004939 if (!percpu_ref_tryget_many(&ctx->refs, nr))
4940 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004941
4942 if (nr > IO_PLUG_THRESHOLD) {
Jackie Liu22efde52019-12-02 17:14:52 +08004943 io_submit_state_start(&state, nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004944 statep = &state;
4945 }
4946
Pavel Begunkovb14cca02020-01-17 04:45:59 +03004947 ctx->ring_fd = ring_fd;
4948 ctx->ring_file = ring_file;
4949
Jens Axboe6c271ce2019-01-10 11:22:30 -07004950 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004951 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03004952 struct io_kiocb *req;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004953
Pavel Begunkov196be952019-11-07 01:41:06 +03004954 req = io_get_req(ctx, statep);
4955 if (unlikely(!req)) {
4956 if (!submitted)
4957 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004958 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06004959 }
Jens Axboe3529d8c2019-12-19 18:24:38 -07004960 if (!io_get_sqring(ctx, req, &sqe)) {
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03004961 __io_req_do_free(req);
Pavel Begunkov196be952019-11-07 01:41:06 +03004962 break;
4963 }
Jens Axboe9e645e112019-05-10 16:07:28 -06004964
Jens Axboed3656342019-12-18 09:50:26 -07004965 /* will complete beyond this point, count as submitted */
4966 submitted++;
4967
4968 if (unlikely(req->opcode >= IORING_OP_LAST)) {
4969 io_cqring_add_event(req, -EINVAL);
4970 io_double_put_req(req);
4971 break;
4972 }
4973
4974 if (io_op_defs[req->opcode].needs_mm && !*mm) {
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03004975 mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm);
4976 if (!mm_fault) {
4977 use_mm(ctx->sqo_mm);
4978 *mm = ctx->sqo_mm;
4979 }
4980 }
4981
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004982 req->has_user = *mm != NULL;
4983 req->in_async = async;
4984 req->needs_fixed_file = async;
Jens Axboe354420f2020-01-08 18:55:15 -07004985 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
4986 true, async);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004987 if (!io_submit_sqe(req, sqe, statep, &link))
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004988 break;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004989 }
4990
Pavel Begunkov9466f432020-01-25 22:34:01 +03004991 if (unlikely(submitted != nr)) {
4992 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
4993
4994 percpu_ref_put_many(&ctx->refs, nr - ref_used);
4995 }
Jens Axboe9e645e112019-05-10 16:07:28 -06004996 if (link)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004997 io_queue_link_head(link);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004998 if (statep)
4999 io_submit_state_end(&state);
5000
Pavel Begunkovae9428c2019-11-06 00:22:14 +03005001 /* Commit SQ ring head once we've consumed and submitted all SQEs */
5002 io_commit_sqring(ctx);
5003
Jens Axboe6c271ce2019-01-10 11:22:30 -07005004 return submitted;
5005}
5006
5007static int io_sq_thread(void *data)
5008{
Jens Axboe6c271ce2019-01-10 11:22:30 -07005009 struct io_ring_ctx *ctx = data;
5010 struct mm_struct *cur_mm = NULL;
Jens Axboe181e4482019-11-25 08:52:30 -07005011 const struct cred *old_cred;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005012 mm_segment_t old_fs;
5013 DEFINE_WAIT(wait);
5014 unsigned inflight;
5015 unsigned long timeout;
Jens Axboec1edbf52019-11-10 16:56:04 -07005016 int ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005017
Jens Axboe206aefd2019-11-07 18:27:42 -07005018 complete(&ctx->completions[1]);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08005019
Jens Axboe6c271ce2019-01-10 11:22:30 -07005020 old_fs = get_fs();
5021 set_fs(USER_DS);
Jens Axboe181e4482019-11-25 08:52:30 -07005022 old_cred = override_creds(ctx->creds);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005023
Jens Axboec1edbf52019-11-10 16:56:04 -07005024 ret = timeout = inflight = 0;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005025 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005026 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005027
5028 if (inflight) {
5029 unsigned nr_events = 0;
5030
5031 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe2b2ed972019-10-25 10:06:15 -06005032 /*
5033 * inflight is the count of the maximum possible
5034 * entries we submitted, but it can be smaller
5035 * if we dropped some of them. If we don't have
5036 * poll entries available, then we know that we
5037 * have nothing left to poll for. Reset the
5038 * inflight count to zero in that case.
5039 */
5040 mutex_lock(&ctx->uring_lock);
5041 if (!list_empty(&ctx->poll_list))
5042 __io_iopoll_check(ctx, &nr_events, 0);
5043 else
5044 inflight = 0;
5045 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005046 } else {
5047 /*
5048 * Normal IO, just pretend everything completed.
5049 * We don't have to poll completions for that.
5050 */
5051 nr_events = inflight;
5052 }
5053
5054 inflight -= nr_events;
5055 if (!inflight)
5056 timeout = jiffies + ctx->sq_thread_idle;
5057 }
5058
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005059 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07005060
5061 /*
5062 * If submit got -EBUSY, flag us as needing the application
5063 * to enter the kernel to reap and flush events.
5064 */
5065 if (!to_submit || ret == -EBUSY) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07005066 /*
5067 * We're polling. If we're within the defined idle
5068 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07005069 * to sleep. The exception is if we got EBUSY doing
5070 * more IO, we should wait for the application to
5071 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07005072 */
Jens Axboec1edbf52019-11-10 16:56:04 -07005073 if (inflight ||
Jens Axboedf069d82020-02-04 16:48:34 -07005074 (!time_after(jiffies, timeout) && ret != -EBUSY &&
5075 !percpu_ref_is_dying(&ctx->refs))) {
Jens Axboe9831a902019-09-19 09:48:55 -06005076 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07005077 continue;
5078 }
5079
5080 /*
5081 * Drop cur_mm before scheduling, we can't hold it for
5082 * long periods (or over schedule()). Do this before
5083 * adding ourselves to the waitqueue, as the unuse/drop
5084 * may sleep.
5085 */
5086 if (cur_mm) {
5087 unuse_mm(cur_mm);
5088 mmput(cur_mm);
5089 cur_mm = NULL;
5090 }
5091
5092 prepare_to_wait(&ctx->sqo_wait, &wait,
5093 TASK_INTERRUPTIBLE);
5094
5095 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00005096 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02005097 /* make sure to read SQ tail after writing flags */
5098 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07005099
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005100 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07005101 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005102 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07005103 finish_wait(&ctx->sqo_wait, &wait);
5104 break;
5105 }
5106 if (signal_pending(current))
5107 flush_signals(current);
5108 schedule();
5109 finish_wait(&ctx->sqo_wait, &wait);
5110
Hristo Venev75b28af2019-08-26 17:23:46 +00005111 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005112 continue;
5113 }
5114 finish_wait(&ctx->sqo_wait, &wait);
5115
Hristo Venev75b28af2019-08-26 17:23:46 +00005116 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005117 }
5118
Jens Axboe8a4955f2019-12-09 14:52:35 -07005119 mutex_lock(&ctx->uring_lock);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005120 ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true);
Jens Axboe8a4955f2019-12-09 14:52:35 -07005121 mutex_unlock(&ctx->uring_lock);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005122 if (ret > 0)
5123 inflight += ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005124 }
5125
5126 set_fs(old_fs);
5127 if (cur_mm) {
5128 unuse_mm(cur_mm);
5129 mmput(cur_mm);
5130 }
Jens Axboe181e4482019-11-25 08:52:30 -07005131 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06005132
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005133 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06005134
Jens Axboe6c271ce2019-01-10 11:22:30 -07005135 return 0;
5136}
5137
Jens Axboebda52162019-09-24 13:47:15 -06005138struct io_wait_queue {
5139 struct wait_queue_entry wq;
5140 struct io_ring_ctx *ctx;
5141 unsigned to_wait;
5142 unsigned nr_timeouts;
5143};
5144
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005145static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06005146{
5147 struct io_ring_ctx *ctx = iowq->ctx;
5148
5149 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08005150 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06005151 * started waiting. For timeouts, we always want to return to userspace,
5152 * regardless of event count.
5153 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005154 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06005155 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
5156}
5157
5158static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
5159 int wake_flags, void *key)
5160{
5161 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
5162 wq);
5163
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005164 /* use noflush == true, as we can't safely rely on locking context */
5165 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06005166 return -1;
5167
5168 return autoremove_wake_function(curr, mode, wake_flags, key);
5169}
5170
Jens Axboe2b188cc2019-01-07 10:46:33 -07005171/*
5172 * Wait until events become available, if we don't already have some. The
5173 * application must reap them itself, as they reside on the shared cq ring.
5174 */
5175static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
5176 const sigset_t __user *sig, size_t sigsz)
5177{
Jens Axboebda52162019-09-24 13:47:15 -06005178 struct io_wait_queue iowq = {
5179 .wq = {
5180 .private = current,
5181 .func = io_wake_function,
5182 .entry = LIST_HEAD_INIT(iowq.wq.entry),
5183 },
5184 .ctx = ctx,
5185 .to_wait = min_events,
5186 };
Hristo Venev75b28af2019-08-26 17:23:46 +00005187 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005188 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005189
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005190 if (io_cqring_events(ctx, false) >= min_events)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005191 return 0;
5192
5193 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005194#ifdef CONFIG_COMPAT
5195 if (in_compat_syscall())
5196 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07005197 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005198 else
5199#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07005200 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005201
Jens Axboe2b188cc2019-01-07 10:46:33 -07005202 if (ret)
5203 return ret;
5204 }
5205
Jens Axboebda52162019-09-24 13:47:15 -06005206 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02005207 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06005208 do {
5209 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
5210 TASK_INTERRUPTIBLE);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005211 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06005212 break;
5213 schedule();
5214 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005215 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06005216 break;
5217 }
5218 } while (1);
5219 finish_wait(&ctx->wait, &iowq.wq);
5220
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005221 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005222
Hristo Venev75b28af2019-08-26 17:23:46 +00005223 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005224}
5225
Jens Axboe6b063142019-01-10 22:13:58 -07005226static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
5227{
5228#if defined(CONFIG_UNIX)
5229 if (ctx->ring_sock) {
5230 struct sock *sock = ctx->ring_sock->sk;
5231 struct sk_buff *skb;
5232
5233 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
5234 kfree_skb(skb);
5235 }
5236#else
5237 int i;
5238
Jens Axboe65e19f52019-10-26 07:20:21 -06005239 for (i = 0; i < ctx->nr_user_files; i++) {
5240 struct file *file;
5241
5242 file = io_file_from_index(ctx, i);
5243 if (file)
5244 fput(file);
5245 }
Jens Axboe6b063142019-01-10 22:13:58 -07005246#endif
5247}
5248
Jens Axboe05f3fb32019-12-09 11:22:50 -07005249static void io_file_ref_kill(struct percpu_ref *ref)
5250{
5251 struct fixed_file_data *data;
5252
5253 data = container_of(ref, struct fixed_file_data, refs);
5254 complete(&data->done);
5255}
5256
Jens Axboe6b063142019-01-10 22:13:58 -07005257static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
5258{
Jens Axboe05f3fb32019-12-09 11:22:50 -07005259 struct fixed_file_data *data = ctx->file_data;
Jens Axboe65e19f52019-10-26 07:20:21 -06005260 unsigned nr_tables, i;
5261
Jens Axboe05f3fb32019-12-09 11:22:50 -07005262 if (!data)
Jens Axboe6b063142019-01-10 22:13:58 -07005263 return -ENXIO;
5264
Jens Axboe05f3fb32019-12-09 11:22:50 -07005265 percpu_ref_kill_and_confirm(&data->refs, io_file_ref_kill);
Jens Axboee46a7952020-01-17 11:15:34 -07005266 flush_work(&data->ref_work);
Jens Axboe2faf8522020-02-04 19:54:55 -07005267 wait_for_completion(&data->done);
5268 io_ring_file_ref_flush(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005269 percpu_ref_exit(&data->refs);
5270
Jens Axboe6b063142019-01-10 22:13:58 -07005271 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06005272 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
5273 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005274 kfree(data->table[i].files);
5275 kfree(data->table);
5276 kfree(data);
5277 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07005278 ctx->nr_user_files = 0;
5279 return 0;
5280}
5281
Jens Axboe6c271ce2019-01-10 11:22:30 -07005282static void io_sq_thread_stop(struct io_ring_ctx *ctx)
5283{
5284 if (ctx->sqo_thread) {
Jens Axboe206aefd2019-11-07 18:27:42 -07005285 wait_for_completion(&ctx->completions[1]);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005286 /*
5287 * The park is a bit of a work-around, without it we get
5288 * warning spews on shutdown with SQPOLL set and affinity
5289 * set to a single CPU.
5290 */
Jens Axboe06058632019-04-13 09:26:03 -06005291 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005292 kthread_stop(ctx->sqo_thread);
5293 ctx->sqo_thread = NULL;
5294 }
5295}
5296
Jens Axboe6b063142019-01-10 22:13:58 -07005297static void io_finish_async(struct io_ring_ctx *ctx)
5298{
Jens Axboe6c271ce2019-01-10 11:22:30 -07005299 io_sq_thread_stop(ctx);
5300
Jens Axboe561fb042019-10-24 07:25:42 -06005301 if (ctx->io_wq) {
5302 io_wq_destroy(ctx->io_wq);
5303 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07005304 }
5305}
5306
5307#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07005308/*
5309 * Ensure the UNIX gc is aware of our file set, so we are certain that
5310 * the io_uring can be safely unregistered on process exit, even if we have
5311 * loops in the file referencing.
5312 */
5313static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
5314{
5315 struct sock *sk = ctx->ring_sock->sk;
5316 struct scm_fp_list *fpl;
5317 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06005318 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07005319
5320 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
5321 unsigned long inflight = ctx->user->unix_inflight + nr;
5322
5323 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
5324 return -EMFILE;
5325 }
5326
5327 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
5328 if (!fpl)
5329 return -ENOMEM;
5330
5331 skb = alloc_skb(0, GFP_KERNEL);
5332 if (!skb) {
5333 kfree(fpl);
5334 return -ENOMEM;
5335 }
5336
5337 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07005338
Jens Axboe08a45172019-10-03 08:11:03 -06005339 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07005340 fpl->user = get_uid(ctx->user);
5341 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005342 struct file *file = io_file_from_index(ctx, i + offset);
5343
5344 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06005345 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06005346 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06005347 unix_inflight(fpl->user, fpl->fp[nr_files]);
5348 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07005349 }
5350
Jens Axboe08a45172019-10-03 08:11:03 -06005351 if (nr_files) {
5352 fpl->max = SCM_MAX_FD;
5353 fpl->count = nr_files;
5354 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005355 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06005356 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
5357 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07005358
Jens Axboe08a45172019-10-03 08:11:03 -06005359 for (i = 0; i < nr_files; i++)
5360 fput(fpl->fp[i]);
5361 } else {
5362 kfree_skb(skb);
5363 kfree(fpl);
5364 }
Jens Axboe6b063142019-01-10 22:13:58 -07005365
5366 return 0;
5367}
5368
5369/*
5370 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
5371 * causes regular reference counting to break down. We rely on the UNIX
5372 * garbage collection to take care of this problem for us.
5373 */
5374static int io_sqe_files_scm(struct io_ring_ctx *ctx)
5375{
5376 unsigned left, total;
5377 int ret = 0;
5378
5379 total = 0;
5380 left = ctx->nr_user_files;
5381 while (left) {
5382 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07005383
5384 ret = __io_sqe_files_scm(ctx, this_files, total);
5385 if (ret)
5386 break;
5387 left -= this_files;
5388 total += this_files;
5389 }
5390
5391 if (!ret)
5392 return 0;
5393
5394 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005395 struct file *file = io_file_from_index(ctx, total);
5396
5397 if (file)
5398 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07005399 total++;
5400 }
5401
5402 return ret;
5403}
5404#else
5405static int io_sqe_files_scm(struct io_ring_ctx *ctx)
5406{
5407 return 0;
5408}
5409#endif
5410
Jens Axboe65e19f52019-10-26 07:20:21 -06005411static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
5412 unsigned nr_files)
5413{
5414 int i;
5415
5416 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005417 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06005418 unsigned this_files;
5419
5420 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
5421 table->files = kcalloc(this_files, sizeof(struct file *),
5422 GFP_KERNEL);
5423 if (!table->files)
5424 break;
5425 nr_files -= this_files;
5426 }
5427
5428 if (i == nr_tables)
5429 return 0;
5430
5431 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005432 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06005433 kfree(table->files);
5434 }
5435 return 1;
5436}
5437
Jens Axboe05f3fb32019-12-09 11:22:50 -07005438static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
Jens Axboec3a31e62019-10-03 13:59:56 -06005439{
5440#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06005441 struct sock *sock = ctx->ring_sock->sk;
5442 struct sk_buff_head list, *head = &sock->sk_receive_queue;
5443 struct sk_buff *skb;
5444 int i;
5445
5446 __skb_queue_head_init(&list);
5447
5448 /*
5449 * Find the skb that holds this file in its SCM_RIGHTS. When found,
5450 * remove this entry and rearrange the file array.
5451 */
5452 skb = skb_dequeue(head);
5453 while (skb) {
5454 struct scm_fp_list *fp;
5455
5456 fp = UNIXCB(skb).fp;
5457 for (i = 0; i < fp->count; i++) {
5458 int left;
5459
5460 if (fp->fp[i] != file)
5461 continue;
5462
5463 unix_notinflight(fp->user, fp->fp[i]);
5464 left = fp->count - 1 - i;
5465 if (left) {
5466 memmove(&fp->fp[i], &fp->fp[i + 1],
5467 left * sizeof(struct file *));
5468 }
5469 fp->count--;
5470 if (!fp->count) {
5471 kfree_skb(skb);
5472 skb = NULL;
5473 } else {
5474 __skb_queue_tail(&list, skb);
5475 }
5476 fput(file);
5477 file = NULL;
5478 break;
5479 }
5480
5481 if (!file)
5482 break;
5483
5484 __skb_queue_tail(&list, skb);
5485
5486 skb = skb_dequeue(head);
5487 }
5488
5489 if (skb_peek(&list)) {
5490 spin_lock_irq(&head->lock);
5491 while ((skb = __skb_dequeue(&list)) != NULL)
5492 __skb_queue_tail(head, skb);
5493 spin_unlock_irq(&head->lock);
5494 }
5495#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07005496 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06005497#endif
5498}
5499
Jens Axboe05f3fb32019-12-09 11:22:50 -07005500struct io_file_put {
5501 struct llist_node llist;
5502 struct file *file;
5503 struct completion *done;
5504};
5505
Jens Axboe2faf8522020-02-04 19:54:55 -07005506static void io_ring_file_ref_flush(struct fixed_file_data *data)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005507{
5508 struct io_file_put *pfile, *tmp;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005509 struct llist_node *node;
5510
Jens Axboe05f3fb32019-12-09 11:22:50 -07005511 while ((node = llist_del_all(&data->put_llist)) != NULL) {
5512 llist_for_each_entry_safe(pfile, tmp, node, llist) {
5513 io_ring_file_put(data->ctx, pfile->file);
5514 if (pfile->done)
5515 complete(pfile->done);
5516 else
5517 kfree(pfile);
5518 }
5519 }
Jens Axboe2faf8522020-02-04 19:54:55 -07005520}
Jens Axboe05f3fb32019-12-09 11:22:50 -07005521
Jens Axboe2faf8522020-02-04 19:54:55 -07005522static void io_ring_file_ref_switch(struct work_struct *work)
5523{
5524 struct fixed_file_data *data;
5525
5526 data = container_of(work, struct fixed_file_data, ref_work);
5527 io_ring_file_ref_flush(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005528 percpu_ref_get(&data->refs);
5529 percpu_ref_switch_to_percpu(&data->refs);
5530}
5531
5532static void io_file_data_ref_zero(struct percpu_ref *ref)
5533{
5534 struct fixed_file_data *data;
5535
5536 data = container_of(ref, struct fixed_file_data, refs);
5537
Jens Axboe2faf8522020-02-04 19:54:55 -07005538 /*
5539 * We can't safely switch from inside this context, punt to wq. If
5540 * the table ref is going away, the table is being unregistered.
5541 * Don't queue up the async work for that case, the caller will
5542 * handle it.
5543 */
5544 if (!percpu_ref_is_dying(&data->refs))
5545 queue_work(system_wq, &data->ref_work);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005546}
5547
5548static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
5549 unsigned nr_args)
5550{
5551 __s32 __user *fds = (__s32 __user *) arg;
5552 unsigned nr_tables;
5553 struct file *file;
5554 int fd, ret = 0;
5555 unsigned i;
5556
5557 if (ctx->file_data)
5558 return -EBUSY;
5559 if (!nr_args)
5560 return -EINVAL;
5561 if (nr_args > IORING_MAX_FIXED_FILES)
5562 return -EMFILE;
5563
5564 ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
5565 if (!ctx->file_data)
5566 return -ENOMEM;
5567 ctx->file_data->ctx = ctx;
5568 init_completion(&ctx->file_data->done);
5569
5570 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
5571 ctx->file_data->table = kcalloc(nr_tables,
5572 sizeof(struct fixed_file_table),
5573 GFP_KERNEL);
5574 if (!ctx->file_data->table) {
5575 kfree(ctx->file_data);
5576 ctx->file_data = NULL;
5577 return -ENOMEM;
5578 }
5579
5580 if (percpu_ref_init(&ctx->file_data->refs, io_file_data_ref_zero,
5581 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
5582 kfree(ctx->file_data->table);
5583 kfree(ctx->file_data);
5584 ctx->file_data = NULL;
5585 return -ENOMEM;
5586 }
5587 ctx->file_data->put_llist.first = NULL;
5588 INIT_WORK(&ctx->file_data->ref_work, io_ring_file_ref_switch);
5589
5590 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
5591 percpu_ref_exit(&ctx->file_data->refs);
5592 kfree(ctx->file_data->table);
5593 kfree(ctx->file_data);
5594 ctx->file_data = NULL;
5595 return -ENOMEM;
5596 }
5597
5598 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
5599 struct fixed_file_table *table;
5600 unsigned index;
5601
5602 ret = -EFAULT;
5603 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
5604 break;
5605 /* allow sparse sets */
5606 if (fd == -1) {
5607 ret = 0;
5608 continue;
5609 }
5610
5611 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
5612 index = i & IORING_FILE_TABLE_MASK;
5613 file = fget(fd);
5614
5615 ret = -EBADF;
5616 if (!file)
5617 break;
5618
5619 /*
5620 * Don't allow io_uring instances to be registered. If UNIX
5621 * isn't enabled, then this causes a reference cycle and this
5622 * instance can never get freed. If UNIX is enabled we'll
5623 * handle it just fine, but there's still no point in allowing
5624 * a ring fd as it doesn't support regular read/write anyway.
5625 */
5626 if (file->f_op == &io_uring_fops) {
5627 fput(file);
5628 break;
5629 }
5630 ret = 0;
5631 table->files[index] = file;
5632 }
5633
5634 if (ret) {
5635 for (i = 0; i < ctx->nr_user_files; i++) {
5636 file = io_file_from_index(ctx, i);
5637 if (file)
5638 fput(file);
5639 }
5640 for (i = 0; i < nr_tables; i++)
5641 kfree(ctx->file_data->table[i].files);
5642
5643 kfree(ctx->file_data->table);
5644 kfree(ctx->file_data);
5645 ctx->file_data = NULL;
5646 ctx->nr_user_files = 0;
5647 return ret;
5648 }
5649
5650 ret = io_sqe_files_scm(ctx);
5651 if (ret)
5652 io_sqe_files_unregister(ctx);
5653
5654 return ret;
5655}
5656
Jens Axboec3a31e62019-10-03 13:59:56 -06005657static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
5658 int index)
5659{
5660#if defined(CONFIG_UNIX)
5661 struct sock *sock = ctx->ring_sock->sk;
5662 struct sk_buff_head *head = &sock->sk_receive_queue;
5663 struct sk_buff *skb;
5664
5665 /*
5666 * See if we can merge this file into an existing skb SCM_RIGHTS
5667 * file set. If there's no room, fall back to allocating a new skb
5668 * and filling it in.
5669 */
5670 spin_lock_irq(&head->lock);
5671 skb = skb_peek(head);
5672 if (skb) {
5673 struct scm_fp_list *fpl = UNIXCB(skb).fp;
5674
5675 if (fpl->count < SCM_MAX_FD) {
5676 __skb_unlink(skb, head);
5677 spin_unlock_irq(&head->lock);
5678 fpl->fp[fpl->count] = get_file(file);
5679 unix_inflight(fpl->user, fpl->fp[fpl->count]);
5680 fpl->count++;
5681 spin_lock_irq(&head->lock);
5682 __skb_queue_head(head, skb);
5683 } else {
5684 skb = NULL;
5685 }
5686 }
5687 spin_unlock_irq(&head->lock);
5688
5689 if (skb) {
5690 fput(file);
5691 return 0;
5692 }
5693
5694 return __io_sqe_files_scm(ctx, 1, index);
5695#else
5696 return 0;
5697#endif
5698}
5699
Jens Axboe05f3fb32019-12-09 11:22:50 -07005700static void io_atomic_switch(struct percpu_ref *ref)
Jens Axboec3a31e62019-10-03 13:59:56 -06005701{
Jens Axboe05f3fb32019-12-09 11:22:50 -07005702 struct fixed_file_data *data;
5703
5704 data = container_of(ref, struct fixed_file_data, refs);
5705 clear_bit(FFD_F_ATOMIC, &data->state);
5706}
5707
5708static bool io_queue_file_removal(struct fixed_file_data *data,
5709 struct file *file)
5710{
5711 struct io_file_put *pfile, pfile_stack;
5712 DECLARE_COMPLETION_ONSTACK(done);
5713
5714 /*
5715 * If we fail allocating the struct we need for doing async reomval
5716 * of this file, just punt to sync and wait for it.
5717 */
5718 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
5719 if (!pfile) {
5720 pfile = &pfile_stack;
5721 pfile->done = &done;
5722 }
5723
5724 pfile->file = file;
5725 llist_add(&pfile->llist, &data->put_llist);
5726
5727 if (pfile == &pfile_stack) {
5728 if (!test_and_set_bit(FFD_F_ATOMIC, &data->state)) {
5729 percpu_ref_put(&data->refs);
5730 percpu_ref_switch_to_atomic(&data->refs,
5731 io_atomic_switch);
5732 }
5733 wait_for_completion(&done);
5734 flush_work(&data->ref_work);
5735 return false;
5736 }
5737
5738 return true;
5739}
5740
5741static int __io_sqe_files_update(struct io_ring_ctx *ctx,
5742 struct io_uring_files_update *up,
5743 unsigned nr_args)
5744{
5745 struct fixed_file_data *data = ctx->file_data;
5746 bool ref_switch = false;
5747 struct file *file;
Jens Axboec3a31e62019-10-03 13:59:56 -06005748 __s32 __user *fds;
5749 int fd, i, err;
5750 __u32 done;
5751
Jens Axboe05f3fb32019-12-09 11:22:50 -07005752 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06005753 return -EOVERFLOW;
5754 if (done > ctx->nr_user_files)
5755 return -EINVAL;
5756
5757 done = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005758 fds = u64_to_user_ptr(up->fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06005759 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005760 struct fixed_file_table *table;
5761 unsigned index;
5762
Jens Axboec3a31e62019-10-03 13:59:56 -06005763 err = 0;
5764 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
5765 err = -EFAULT;
5766 break;
5767 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07005768 i = array_index_nospec(up->offset, ctx->nr_user_files);
5769 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
Jens Axboe65e19f52019-10-26 07:20:21 -06005770 index = i & IORING_FILE_TABLE_MASK;
5771 if (table->files[index]) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005772 file = io_file_from_index(ctx, index);
Jens Axboe65e19f52019-10-26 07:20:21 -06005773 table->files[index] = NULL;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005774 if (io_queue_file_removal(data, file))
5775 ref_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06005776 }
5777 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06005778 file = fget(fd);
5779 if (!file) {
5780 err = -EBADF;
5781 break;
5782 }
5783 /*
5784 * Don't allow io_uring instances to be registered. If
5785 * UNIX isn't enabled, then this causes a reference
5786 * cycle and this instance can never get freed. If UNIX
5787 * is enabled we'll handle it just fine, but there's
5788 * still no point in allowing a ring fd as it doesn't
5789 * support regular read/write anyway.
5790 */
5791 if (file->f_op == &io_uring_fops) {
5792 fput(file);
5793 err = -EBADF;
5794 break;
5795 }
Jens Axboe65e19f52019-10-26 07:20:21 -06005796 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06005797 err = io_sqe_file_register(ctx, file, i);
5798 if (err)
5799 break;
5800 }
5801 nr_args--;
5802 done++;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005803 up->offset++;
5804 }
5805
5806 if (ref_switch && !test_and_set_bit(FFD_F_ATOMIC, &data->state)) {
5807 percpu_ref_put(&data->refs);
5808 percpu_ref_switch_to_atomic(&data->refs, io_atomic_switch);
Jens Axboec3a31e62019-10-03 13:59:56 -06005809 }
5810
5811 return done ? done : err;
5812}
Jens Axboe05f3fb32019-12-09 11:22:50 -07005813static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
5814 unsigned nr_args)
5815{
5816 struct io_uring_files_update up;
5817
5818 if (!ctx->file_data)
5819 return -ENXIO;
5820 if (!nr_args)
5821 return -EINVAL;
5822 if (copy_from_user(&up, arg, sizeof(up)))
5823 return -EFAULT;
5824 if (up.resv)
5825 return -EINVAL;
5826
5827 return __io_sqe_files_update(ctx, &up, nr_args);
5828}
Jens Axboec3a31e62019-10-03 13:59:56 -06005829
Jens Axboe7d723062019-11-12 22:31:31 -07005830static void io_put_work(struct io_wq_work *work)
5831{
5832 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
5833
5834 io_put_req(req);
5835}
5836
5837static void io_get_work(struct io_wq_work *work)
5838{
5839 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
5840
5841 refcount_inc(&req->refs);
5842}
5843
Pavel Begunkov24369c22020-01-28 03:15:48 +03005844static int io_init_wq_offload(struct io_ring_ctx *ctx,
5845 struct io_uring_params *p)
5846{
5847 struct io_wq_data data;
5848 struct fd f;
5849 struct io_ring_ctx *ctx_attach;
5850 unsigned int concurrency;
5851 int ret = 0;
5852
5853 data.user = ctx->user;
5854 data.get_work = io_get_work;
5855 data.put_work = io_put_work;
5856
5857 if (!(p->flags & IORING_SETUP_ATTACH_WQ)) {
5858 /* Do QD, or 4 * CPUS, whatever is smallest */
5859 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
5860
5861 ctx->io_wq = io_wq_create(concurrency, &data);
5862 if (IS_ERR(ctx->io_wq)) {
5863 ret = PTR_ERR(ctx->io_wq);
5864 ctx->io_wq = NULL;
5865 }
5866 return ret;
5867 }
5868
5869 f = fdget(p->wq_fd);
5870 if (!f.file)
5871 return -EBADF;
5872
5873 if (f.file->f_op != &io_uring_fops) {
5874 ret = -EINVAL;
5875 goto out_fput;
5876 }
5877
5878 ctx_attach = f.file->private_data;
5879 /* @io_wq is protected by holding the fd */
5880 if (!io_wq_get(ctx_attach->io_wq, &data)) {
5881 ret = -EINVAL;
5882 goto out_fput;
5883 }
5884
5885 ctx->io_wq = ctx_attach->io_wq;
5886out_fput:
5887 fdput(f);
5888 return ret;
5889}
5890
Jens Axboe6c271ce2019-01-10 11:22:30 -07005891static int io_sq_offload_start(struct io_ring_ctx *ctx,
5892 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005893{
5894 int ret;
5895
Jens Axboe6c271ce2019-01-10 11:22:30 -07005896 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005897 mmgrab(current->mm);
5898 ctx->sqo_mm = current->mm;
5899
Jens Axboe6c271ce2019-01-10 11:22:30 -07005900 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06005901 ret = -EPERM;
5902 if (!capable(CAP_SYS_ADMIN))
5903 goto err;
5904
Jens Axboe917257d2019-04-13 09:28:55 -06005905 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
5906 if (!ctx->sq_thread_idle)
5907 ctx->sq_thread_idle = HZ;
5908
Jens Axboe6c271ce2019-01-10 11:22:30 -07005909 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06005910 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005911
Jens Axboe917257d2019-04-13 09:28:55 -06005912 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06005913 if (cpu >= nr_cpu_ids)
5914 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08005915 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06005916 goto err;
5917
Jens Axboe6c271ce2019-01-10 11:22:30 -07005918 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
5919 ctx, cpu,
5920 "io_uring-sq");
5921 } else {
5922 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
5923 "io_uring-sq");
5924 }
5925 if (IS_ERR(ctx->sqo_thread)) {
5926 ret = PTR_ERR(ctx->sqo_thread);
5927 ctx->sqo_thread = NULL;
5928 goto err;
5929 }
5930 wake_up_process(ctx->sqo_thread);
5931 } else if (p->flags & IORING_SETUP_SQ_AFF) {
5932 /* Can't have SQ_AFF without SQPOLL */
5933 ret = -EINVAL;
5934 goto err;
5935 }
5936
Pavel Begunkov24369c22020-01-28 03:15:48 +03005937 ret = io_init_wq_offload(ctx, p);
5938 if (ret)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005939 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005940
5941 return 0;
5942err:
Jens Axboe54a91f32019-09-10 09:15:04 -06005943 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005944 mmdrop(ctx->sqo_mm);
5945 ctx->sqo_mm = NULL;
5946 return ret;
5947}
5948
5949static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
5950{
5951 atomic_long_sub(nr_pages, &user->locked_vm);
5952}
5953
5954static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
5955{
5956 unsigned long page_limit, cur_pages, new_pages;
5957
5958 /* Don't allow more pages than we can safely lock */
5959 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
5960
5961 do {
5962 cur_pages = atomic_long_read(&user->locked_vm);
5963 new_pages = cur_pages + nr_pages;
5964 if (new_pages > page_limit)
5965 return -ENOMEM;
5966 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
5967 new_pages) != cur_pages);
5968
5969 return 0;
5970}
5971
5972static void io_mem_free(void *ptr)
5973{
Mark Rutland52e04ef2019-04-30 17:30:21 +01005974 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005975
Mark Rutland52e04ef2019-04-30 17:30:21 +01005976 if (!ptr)
5977 return;
5978
5979 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005980 if (put_page_testzero(page))
5981 free_compound_page(page);
5982}
5983
5984static void *io_mem_alloc(size_t size)
5985{
5986 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
5987 __GFP_NORETRY;
5988
5989 return (void *) __get_free_pages(gfp_flags, get_order(size));
5990}
5991
Hristo Venev75b28af2019-08-26 17:23:46 +00005992static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
5993 size_t *sq_offset)
5994{
5995 struct io_rings *rings;
5996 size_t off, sq_array_size;
5997
5998 off = struct_size(rings, cqes, cq_entries);
5999 if (off == SIZE_MAX)
6000 return SIZE_MAX;
6001
6002#ifdef CONFIG_SMP
6003 off = ALIGN(off, SMP_CACHE_BYTES);
6004 if (off == 0)
6005 return SIZE_MAX;
6006#endif
6007
6008 sq_array_size = array_size(sizeof(u32), sq_entries);
6009 if (sq_array_size == SIZE_MAX)
6010 return SIZE_MAX;
6011
6012 if (check_add_overflow(off, sq_array_size, &off))
6013 return SIZE_MAX;
6014
6015 if (sq_offset)
6016 *sq_offset = off;
6017
6018 return off;
6019}
6020
Jens Axboe2b188cc2019-01-07 10:46:33 -07006021static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
6022{
Hristo Venev75b28af2019-08-26 17:23:46 +00006023 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006024
Hristo Venev75b28af2019-08-26 17:23:46 +00006025 pages = (size_t)1 << get_order(
6026 rings_size(sq_entries, cq_entries, NULL));
6027 pages += (size_t)1 << get_order(
6028 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07006029
Hristo Venev75b28af2019-08-26 17:23:46 +00006030 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006031}
6032
Jens Axboeedafcce2019-01-09 09:16:05 -07006033static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
6034{
6035 int i, j;
6036
6037 if (!ctx->user_bufs)
6038 return -ENXIO;
6039
6040 for (i = 0; i < ctx->nr_user_bufs; i++) {
6041 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
6042
6043 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08006044 unpin_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07006045
6046 if (ctx->account_mem)
6047 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006048 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07006049 imu->nr_bvecs = 0;
6050 }
6051
6052 kfree(ctx->user_bufs);
6053 ctx->user_bufs = NULL;
6054 ctx->nr_user_bufs = 0;
6055 return 0;
6056}
6057
6058static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
6059 void __user *arg, unsigned index)
6060{
6061 struct iovec __user *src;
6062
6063#ifdef CONFIG_COMPAT
6064 if (ctx->compat) {
6065 struct compat_iovec __user *ciovs;
6066 struct compat_iovec ciov;
6067
6068 ciovs = (struct compat_iovec __user *) arg;
6069 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
6070 return -EFAULT;
6071
Jens Axboed55e5f52019-12-11 16:12:15 -07006072 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07006073 dst->iov_len = ciov.iov_len;
6074 return 0;
6075 }
6076#endif
6077 src = (struct iovec __user *) arg;
6078 if (copy_from_user(dst, &src[index], sizeof(*dst)))
6079 return -EFAULT;
6080 return 0;
6081}
6082
6083static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
6084 unsigned nr_args)
6085{
6086 struct vm_area_struct **vmas = NULL;
6087 struct page **pages = NULL;
6088 int i, j, got_pages = 0;
6089 int ret = -EINVAL;
6090
6091 if (ctx->user_bufs)
6092 return -EBUSY;
6093 if (!nr_args || nr_args > UIO_MAXIOV)
6094 return -EINVAL;
6095
6096 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
6097 GFP_KERNEL);
6098 if (!ctx->user_bufs)
6099 return -ENOMEM;
6100
6101 for (i = 0; i < nr_args; i++) {
6102 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
6103 unsigned long off, start, end, ubuf;
6104 int pret, nr_pages;
6105 struct iovec iov;
6106 size_t size;
6107
6108 ret = io_copy_iov(ctx, &iov, arg, i);
6109 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03006110 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07006111
6112 /*
6113 * Don't impose further limits on the size and buffer
6114 * constraints here, we'll -EINVAL later when IO is
6115 * submitted if they are wrong.
6116 */
6117 ret = -EFAULT;
6118 if (!iov.iov_base || !iov.iov_len)
6119 goto err;
6120
6121 /* arbitrary limit, but we need something */
6122 if (iov.iov_len > SZ_1G)
6123 goto err;
6124
6125 ubuf = (unsigned long) iov.iov_base;
6126 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
6127 start = ubuf >> PAGE_SHIFT;
6128 nr_pages = end - start;
6129
6130 if (ctx->account_mem) {
6131 ret = io_account_mem(ctx->user, nr_pages);
6132 if (ret)
6133 goto err;
6134 }
6135
6136 ret = 0;
6137 if (!pages || nr_pages > got_pages) {
6138 kfree(vmas);
6139 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006140 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07006141 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006142 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07006143 sizeof(struct vm_area_struct *),
6144 GFP_KERNEL);
6145 if (!pages || !vmas) {
6146 ret = -ENOMEM;
6147 if (ctx->account_mem)
6148 io_unaccount_mem(ctx->user, nr_pages);
6149 goto err;
6150 }
6151 got_pages = nr_pages;
6152 }
6153
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006154 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07006155 GFP_KERNEL);
6156 ret = -ENOMEM;
6157 if (!imu->bvec) {
6158 if (ctx->account_mem)
6159 io_unaccount_mem(ctx->user, nr_pages);
6160 goto err;
6161 }
6162
6163 ret = 0;
6164 down_read(&current->mm->mmap_sem);
John Hubbard2113b052020-01-30 22:13:13 -08006165 pret = pin_user_pages(ubuf, nr_pages,
Ira Weiny932f4a62019-05-13 17:17:03 -07006166 FOLL_WRITE | FOLL_LONGTERM,
6167 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07006168 if (pret == nr_pages) {
6169 /* don't support file backed memory */
6170 for (j = 0; j < nr_pages; j++) {
6171 struct vm_area_struct *vma = vmas[j];
6172
6173 if (vma->vm_file &&
6174 !is_file_hugepages(vma->vm_file)) {
6175 ret = -EOPNOTSUPP;
6176 break;
6177 }
6178 }
6179 } else {
6180 ret = pret < 0 ? pret : -EFAULT;
6181 }
6182 up_read(&current->mm->mmap_sem);
6183 if (ret) {
6184 /*
6185 * if we did partial map, or found file backed vmas,
6186 * release any pages we did get
6187 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07006188 if (pret > 0)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08006189 unpin_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07006190 if (ctx->account_mem)
6191 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006192 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07006193 goto err;
6194 }
6195
6196 off = ubuf & ~PAGE_MASK;
6197 size = iov.iov_len;
6198 for (j = 0; j < nr_pages; j++) {
6199 size_t vec_len;
6200
6201 vec_len = min_t(size_t, size, PAGE_SIZE - off);
6202 imu->bvec[j].bv_page = pages[j];
6203 imu->bvec[j].bv_len = vec_len;
6204 imu->bvec[j].bv_offset = off;
6205 off = 0;
6206 size -= vec_len;
6207 }
6208 /* store original address for later verification */
6209 imu->ubuf = ubuf;
6210 imu->len = iov.iov_len;
6211 imu->nr_bvecs = nr_pages;
6212
6213 ctx->nr_user_bufs++;
6214 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006215 kvfree(pages);
6216 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07006217 return 0;
6218err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006219 kvfree(pages);
6220 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07006221 io_sqe_buffer_unregister(ctx);
6222 return ret;
6223}
6224
Jens Axboe9b402842019-04-11 11:45:41 -06006225static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
6226{
6227 __s32 __user *fds = arg;
6228 int fd;
6229
6230 if (ctx->cq_ev_fd)
6231 return -EBUSY;
6232
6233 if (copy_from_user(&fd, fds, sizeof(*fds)))
6234 return -EFAULT;
6235
6236 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
6237 if (IS_ERR(ctx->cq_ev_fd)) {
6238 int ret = PTR_ERR(ctx->cq_ev_fd);
6239 ctx->cq_ev_fd = NULL;
6240 return ret;
6241 }
6242
6243 return 0;
6244}
6245
6246static int io_eventfd_unregister(struct io_ring_ctx *ctx)
6247{
6248 if (ctx->cq_ev_fd) {
6249 eventfd_ctx_put(ctx->cq_ev_fd);
6250 ctx->cq_ev_fd = NULL;
6251 return 0;
6252 }
6253
6254 return -ENXIO;
6255}
6256
Jens Axboe2b188cc2019-01-07 10:46:33 -07006257static void io_ring_ctx_free(struct io_ring_ctx *ctx)
6258{
Jens Axboe6b063142019-01-10 22:13:58 -07006259 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006260 if (ctx->sqo_mm)
6261 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07006262
6263 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07006264 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07006265 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06006266 io_eventfd_unregister(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07006267
Jens Axboe2b188cc2019-01-07 10:46:33 -07006268#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07006269 if (ctx->ring_sock) {
6270 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07006271 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07006272 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006273#endif
6274
Hristo Venev75b28af2019-08-26 17:23:46 +00006275 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006276 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006277
6278 percpu_ref_exit(&ctx->refs);
6279 if (ctx->account_mem)
6280 io_unaccount_mem(ctx->user,
6281 ring_pages(ctx->sq_entries, ctx->cq_entries));
6282 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07006283 put_cred(ctx->creds);
Jens Axboe206aefd2019-11-07 18:27:42 -07006284 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -07006285 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07006286 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006287 kfree(ctx);
6288}
6289
6290static __poll_t io_uring_poll(struct file *file, poll_table *wait)
6291{
6292 struct io_ring_ctx *ctx = file->private_data;
6293 __poll_t mask = 0;
6294
6295 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02006296 /*
6297 * synchronizes with barrier from wq_has_sleeper call in
6298 * io_commit_cqring
6299 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07006300 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00006301 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
6302 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006303 mask |= EPOLLOUT | EPOLLWRNORM;
yangerkundaa5de52019-09-24 20:53:34 +08006304 if (READ_ONCE(ctx->rings->cq.head) != ctx->cached_cq_tail)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006305 mask |= EPOLLIN | EPOLLRDNORM;
6306
6307 return mask;
6308}
6309
6310static int io_uring_fasync(int fd, struct file *file, int on)
6311{
6312 struct io_ring_ctx *ctx = file->private_data;
6313
6314 return fasync_helper(fd, file, on, &ctx->cq_fasync);
6315}
6316
Jens Axboe071698e2020-01-28 10:04:42 -07006317static int io_remove_personalities(int id, void *p, void *data)
6318{
6319 struct io_ring_ctx *ctx = data;
6320 const struct cred *cred;
6321
6322 cred = idr_remove(&ctx->personality_idr, id);
6323 if (cred)
6324 put_cred(cred);
6325 return 0;
6326}
6327
Jens Axboe2b188cc2019-01-07 10:46:33 -07006328static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
6329{
6330 mutex_lock(&ctx->uring_lock);
6331 percpu_ref_kill(&ctx->refs);
6332 mutex_unlock(&ctx->uring_lock);
6333
Jens Axboedf069d82020-02-04 16:48:34 -07006334 /*
6335 * Wait for sq thread to idle, if we have one. It won't spin on new
6336 * work after we've killed the ctx ref above. This is important to do
6337 * before we cancel existing commands, as the thread could otherwise
6338 * be queueing new work post that. If that's work we need to cancel,
6339 * it could cause shutdown to hang.
6340 */
6341 while (ctx->sqo_thread && !wq_has_sleeper(&ctx->sqo_wait))
6342 cpu_relax();
6343
Jens Axboe5262f562019-09-17 12:26:57 -06006344 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07006345 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06006346
6347 if (ctx->io_wq)
6348 io_wq_cancel_all(ctx->io_wq);
6349
Jens Axboedef596e2019-01-09 08:59:42 -07006350 io_iopoll_reap_events(ctx);
Jens Axboe15dff282019-11-13 09:09:23 -07006351 /* if we failed setting up the ctx, we might not have any rings */
6352 if (ctx->rings)
6353 io_cqring_overflow_flush(ctx, true);
Jens Axboe071698e2020-01-28 10:04:42 -07006354 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
Jens Axboe206aefd2019-11-07 18:27:42 -07006355 wait_for_completion(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006356 io_ring_ctx_free(ctx);
6357}
6358
6359static int io_uring_release(struct inode *inode, struct file *file)
6360{
6361 struct io_ring_ctx *ctx = file->private_data;
6362
6363 file->private_data = NULL;
6364 io_ring_ctx_wait_and_kill(ctx);
6365 return 0;
6366}
6367
Jens Axboefcb323c2019-10-24 12:39:47 -06006368static void io_uring_cancel_files(struct io_ring_ctx *ctx,
6369 struct files_struct *files)
6370{
6371 struct io_kiocb *req;
6372 DEFINE_WAIT(wait);
6373
6374 while (!list_empty_careful(&ctx->inflight_list)) {
Jens Axboe768134d2019-11-10 20:30:53 -07006375 struct io_kiocb *cancel_req = NULL;
Jens Axboefcb323c2019-10-24 12:39:47 -06006376
6377 spin_lock_irq(&ctx->inflight_lock);
6378 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07006379 if (req->work.files != files)
6380 continue;
6381 /* req is being completed, ignore */
6382 if (!refcount_inc_not_zero(&req->refs))
6383 continue;
6384 cancel_req = req;
6385 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06006386 }
Jens Axboe768134d2019-11-10 20:30:53 -07006387 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06006388 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07006389 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06006390 spin_unlock_irq(&ctx->inflight_lock);
6391
Jens Axboe768134d2019-11-10 20:30:53 -07006392 /* We need to keep going until we don't find a matching req */
6393 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06006394 break;
Bob Liu2f6d9b92019-11-13 18:06:24 +08006395
6396 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
6397 io_put_req(cancel_req);
Jens Axboefcb323c2019-10-24 12:39:47 -06006398 schedule();
6399 }
Jens Axboe768134d2019-11-10 20:30:53 -07006400 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06006401}
6402
6403static int io_uring_flush(struct file *file, void *data)
6404{
6405 struct io_ring_ctx *ctx = file->private_data;
6406
6407 io_uring_cancel_files(ctx, data);
Jens Axboefcb323c2019-10-24 12:39:47 -06006408 return 0;
6409}
6410
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006411static void *io_uring_validate_mmap_request(struct file *file,
6412 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006413{
Jens Axboe2b188cc2019-01-07 10:46:33 -07006414 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006415 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006416 struct page *page;
6417 void *ptr;
6418
6419 switch (offset) {
6420 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00006421 case IORING_OFF_CQ_RING:
6422 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006423 break;
6424 case IORING_OFF_SQES:
6425 ptr = ctx->sq_sqes;
6426 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006427 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006428 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006429 }
6430
6431 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07006432 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006433 return ERR_PTR(-EINVAL);
6434
6435 return ptr;
6436}
6437
6438#ifdef CONFIG_MMU
6439
6440static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
6441{
6442 size_t sz = vma->vm_end - vma->vm_start;
6443 unsigned long pfn;
6444 void *ptr;
6445
6446 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
6447 if (IS_ERR(ptr))
6448 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006449
6450 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
6451 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
6452}
6453
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006454#else /* !CONFIG_MMU */
6455
6456static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
6457{
6458 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
6459}
6460
6461static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
6462{
6463 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
6464}
6465
6466static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
6467 unsigned long addr, unsigned long len,
6468 unsigned long pgoff, unsigned long flags)
6469{
6470 void *ptr;
6471
6472 ptr = io_uring_validate_mmap_request(file, pgoff, len);
6473 if (IS_ERR(ptr))
6474 return PTR_ERR(ptr);
6475
6476 return (unsigned long) ptr;
6477}
6478
6479#endif /* !CONFIG_MMU */
6480
Jens Axboe2b188cc2019-01-07 10:46:33 -07006481SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
6482 u32, min_complete, u32, flags, const sigset_t __user *, sig,
6483 size_t, sigsz)
6484{
6485 struct io_ring_ctx *ctx;
6486 long ret = -EBADF;
6487 int submitted = 0;
6488 struct fd f;
6489
Jens Axboe6c271ce2019-01-10 11:22:30 -07006490 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006491 return -EINVAL;
6492
6493 f = fdget(fd);
6494 if (!f.file)
6495 return -EBADF;
6496
6497 ret = -EOPNOTSUPP;
6498 if (f.file->f_op != &io_uring_fops)
6499 goto out_fput;
6500
6501 ret = -ENXIO;
6502 ctx = f.file->private_data;
6503 if (!percpu_ref_tryget(&ctx->refs))
6504 goto out_fput;
6505
Jens Axboe6c271ce2019-01-10 11:22:30 -07006506 /*
6507 * For SQ polling, the thread will do all submissions and completions.
6508 * Just return the requested submit count, and wake the thread if
6509 * we were asked to.
6510 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06006511 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006512 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07006513 if (!list_empty_careful(&ctx->cq_overflow_list))
6514 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006515 if (flags & IORING_ENTER_SQ_WAKEUP)
6516 wake_up(&ctx->sqo_wait);
6517 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06006518 } else if (to_submit) {
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006519 struct mm_struct *cur_mm;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006520
6521 mutex_lock(&ctx->uring_lock);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006522 /* already have mm, so io_submit_sqes() won't try to grab it */
6523 cur_mm = ctx->sqo_mm;
6524 submitted = io_submit_sqes(ctx, to_submit, f.file, fd,
6525 &cur_mm, false);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006526 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03006527
6528 if (submitted != to_submit)
6529 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006530 }
6531 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07006532 unsigned nr_events = 0;
6533
Jens Axboe2b188cc2019-01-07 10:46:33 -07006534 min_complete = min(min_complete, ctx->cq_entries);
6535
Jens Axboedef596e2019-01-09 08:59:42 -07006536 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07006537 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07006538 } else {
6539 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
6540 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006541 }
6542
Pavel Begunkov7c504e652019-12-18 19:53:45 +03006543out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03006544 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006545out_fput:
6546 fdput(f);
6547 return submitted ? submitted : ret;
6548}
6549
Jens Axboe87ce9552020-01-30 08:25:34 -07006550static int io_uring_show_cred(int id, void *p, void *data)
6551{
6552 const struct cred *cred = p;
6553 struct seq_file *m = data;
6554 struct user_namespace *uns = seq_user_ns(m);
6555 struct group_info *gi;
6556 kernel_cap_t cap;
6557 unsigned __capi;
6558 int g;
6559
6560 seq_printf(m, "%5d\n", id);
6561 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
6562 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
6563 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
6564 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
6565 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
6566 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
6567 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
6568 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
6569 seq_puts(m, "\n\tGroups:\t");
6570 gi = cred->group_info;
6571 for (g = 0; g < gi->ngroups; g++) {
6572 seq_put_decimal_ull(m, g ? " " : "",
6573 from_kgid_munged(uns, gi->gid[g]));
6574 }
6575 seq_puts(m, "\n\tCapEff:\t");
6576 cap = cred->cap_effective;
6577 CAP_FOR_EACH_U32(__capi)
6578 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
6579 seq_putc(m, '\n');
6580 return 0;
6581}
6582
6583static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
6584{
6585 int i;
6586
6587 mutex_lock(&ctx->uring_lock);
6588 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
6589 for (i = 0; i < ctx->nr_user_files; i++) {
6590 struct fixed_file_table *table;
6591 struct file *f;
6592
6593 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
6594 f = table->files[i & IORING_FILE_TABLE_MASK];
6595 if (f)
6596 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
6597 else
6598 seq_printf(m, "%5u: <none>\n", i);
6599 }
6600 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
6601 for (i = 0; i < ctx->nr_user_bufs; i++) {
6602 struct io_mapped_ubuf *buf = &ctx->user_bufs[i];
6603
6604 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf,
6605 (unsigned int) buf->len);
6606 }
6607 if (!idr_is_empty(&ctx->personality_idr)) {
6608 seq_printf(m, "Personalities:\n");
6609 idr_for_each(&ctx->personality_idr, io_uring_show_cred, m);
6610 }
6611 mutex_unlock(&ctx->uring_lock);
6612}
6613
6614static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
6615{
6616 struct io_ring_ctx *ctx = f->private_data;
6617
6618 if (percpu_ref_tryget(&ctx->refs)) {
6619 __io_uring_show_fdinfo(ctx, m);
6620 percpu_ref_put(&ctx->refs);
6621 }
6622}
6623
Jens Axboe2b188cc2019-01-07 10:46:33 -07006624static const struct file_operations io_uring_fops = {
6625 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06006626 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07006627 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006628#ifndef CONFIG_MMU
6629 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
6630 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
6631#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07006632 .poll = io_uring_poll,
6633 .fasync = io_uring_fasync,
Jens Axboe87ce9552020-01-30 08:25:34 -07006634 .show_fdinfo = io_uring_show_fdinfo,
Jens Axboe2b188cc2019-01-07 10:46:33 -07006635};
6636
6637static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
6638 struct io_uring_params *p)
6639{
Hristo Venev75b28af2019-08-26 17:23:46 +00006640 struct io_rings *rings;
6641 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006642
Hristo Venev75b28af2019-08-26 17:23:46 +00006643 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
6644 if (size == SIZE_MAX)
6645 return -EOVERFLOW;
6646
6647 rings = io_mem_alloc(size);
6648 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006649 return -ENOMEM;
6650
Hristo Venev75b28af2019-08-26 17:23:46 +00006651 ctx->rings = rings;
6652 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
6653 rings->sq_ring_mask = p->sq_entries - 1;
6654 rings->cq_ring_mask = p->cq_entries - 1;
6655 rings->sq_ring_entries = p->sq_entries;
6656 rings->cq_ring_entries = p->cq_entries;
6657 ctx->sq_mask = rings->sq_ring_mask;
6658 ctx->cq_mask = rings->cq_ring_mask;
6659 ctx->sq_entries = rings->sq_ring_entries;
6660 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006661
6662 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07006663 if (size == SIZE_MAX) {
6664 io_mem_free(ctx->rings);
6665 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006666 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07006667 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006668
6669 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07006670 if (!ctx->sq_sqes) {
6671 io_mem_free(ctx->rings);
6672 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006673 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07006674 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006675
Jens Axboe2b188cc2019-01-07 10:46:33 -07006676 return 0;
6677}
6678
6679/*
6680 * Allocate an anonymous fd, this is what constitutes the application
6681 * visible backing of an io_uring instance. The application mmaps this
6682 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
6683 * we have to tie this fd to a socket for file garbage collection purposes.
6684 */
6685static int io_uring_get_fd(struct io_ring_ctx *ctx)
6686{
6687 struct file *file;
6688 int ret;
6689
6690#if defined(CONFIG_UNIX)
6691 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
6692 &ctx->ring_sock);
6693 if (ret)
6694 return ret;
6695#endif
6696
6697 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
6698 if (ret < 0)
6699 goto err;
6700
6701 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
6702 O_RDWR | O_CLOEXEC);
6703 if (IS_ERR(file)) {
6704 put_unused_fd(ret);
6705 ret = PTR_ERR(file);
6706 goto err;
6707 }
6708
6709#if defined(CONFIG_UNIX)
6710 ctx->ring_sock->file = file;
6711#endif
6712 fd_install(ret, file);
6713 return ret;
6714err:
6715#if defined(CONFIG_UNIX)
6716 sock_release(ctx->ring_sock);
6717 ctx->ring_sock = NULL;
6718#endif
6719 return ret;
6720}
6721
6722static int io_uring_create(unsigned entries, struct io_uring_params *p)
6723{
6724 struct user_struct *user = NULL;
6725 struct io_ring_ctx *ctx;
6726 bool account_mem;
6727 int ret;
6728
Jens Axboe8110c1a2019-12-28 15:39:54 -07006729 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006730 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07006731 if (entries > IORING_MAX_ENTRIES) {
6732 if (!(p->flags & IORING_SETUP_CLAMP))
6733 return -EINVAL;
6734 entries = IORING_MAX_ENTRIES;
6735 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006736
6737 /*
6738 * Use twice as many entries for the CQ ring. It's possible for the
6739 * application to drive a higher depth than the size of the SQ ring,
6740 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06006741 * some flexibility in overcommitting a bit. If the application has
6742 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
6743 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07006744 */
6745 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06006746 if (p->flags & IORING_SETUP_CQSIZE) {
6747 /*
6748 * If IORING_SETUP_CQSIZE is set, we do the same roundup
6749 * to a power-of-two, if it isn't already. We do NOT impose
6750 * any cq vs sq ring sizing.
6751 */
Jens Axboe8110c1a2019-12-28 15:39:54 -07006752 if (p->cq_entries < p->sq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06006753 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07006754 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
6755 if (!(p->flags & IORING_SETUP_CLAMP))
6756 return -EINVAL;
6757 p->cq_entries = IORING_MAX_CQ_ENTRIES;
6758 }
Jens Axboe33a107f2019-10-04 12:10:03 -06006759 p->cq_entries = roundup_pow_of_two(p->cq_entries);
6760 } else {
6761 p->cq_entries = 2 * p->sq_entries;
6762 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006763
6764 user = get_uid(current_user());
6765 account_mem = !capable(CAP_IPC_LOCK);
6766
6767 if (account_mem) {
6768 ret = io_account_mem(user,
6769 ring_pages(p->sq_entries, p->cq_entries));
6770 if (ret) {
6771 free_uid(user);
6772 return ret;
6773 }
6774 }
6775
6776 ctx = io_ring_ctx_alloc(p);
6777 if (!ctx) {
6778 if (account_mem)
6779 io_unaccount_mem(user, ring_pages(p->sq_entries,
6780 p->cq_entries));
6781 free_uid(user);
6782 return -ENOMEM;
6783 }
6784 ctx->compat = in_compat_syscall();
6785 ctx->account_mem = account_mem;
6786 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07006787 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07006788
6789 ret = io_allocate_scq_urings(ctx, p);
6790 if (ret)
6791 goto err;
6792
Jens Axboe6c271ce2019-01-10 11:22:30 -07006793 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006794 if (ret)
6795 goto err;
6796
Jens Axboe2b188cc2019-01-07 10:46:33 -07006797 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00006798 p->sq_off.head = offsetof(struct io_rings, sq.head);
6799 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
6800 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
6801 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
6802 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
6803 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
6804 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006805
6806 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00006807 p->cq_off.head = offsetof(struct io_rings, cq.head);
6808 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
6809 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
6810 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
6811 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
6812 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06006813
Jens Axboe044c1ab2019-10-28 09:15:33 -06006814 /*
6815 * Install ring fd as the very last thing, so we don't risk someone
6816 * having closed it before we finish setup
6817 */
6818 ret = io_uring_get_fd(ctx);
6819 if (ret < 0)
6820 goto err;
6821
Jens Axboeda8c9692019-12-02 18:51:26 -07006822 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
Jens Axboecccf0ee2020-01-27 16:34:48 -07006823 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
6824 IORING_FEAT_CUR_PERSONALITY;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006825 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006826 return ret;
6827err:
6828 io_ring_ctx_wait_and_kill(ctx);
6829 return ret;
6830}
6831
6832/*
6833 * Sets up an aio uring context, and returns the fd. Applications asks for a
6834 * ring size, we return the actual sq/cq ring sizes (among other things) in the
6835 * params structure passed in.
6836 */
6837static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
6838{
6839 struct io_uring_params p;
6840 long ret;
6841 int i;
6842
6843 if (copy_from_user(&p, params, sizeof(p)))
6844 return -EFAULT;
6845 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
6846 if (p.resv[i])
6847 return -EINVAL;
6848 }
6849
Jens Axboe6c271ce2019-01-10 11:22:30 -07006850 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07006851 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Pavel Begunkov24369c22020-01-28 03:15:48 +03006852 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006853 return -EINVAL;
6854
6855 ret = io_uring_create(entries, &p);
6856 if (ret < 0)
6857 return ret;
6858
6859 if (copy_to_user(params, &p, sizeof(p)))
6860 return -EFAULT;
6861
6862 return ret;
6863}
6864
6865SYSCALL_DEFINE2(io_uring_setup, u32, entries,
6866 struct io_uring_params __user *, params)
6867{
6868 return io_uring_setup(entries, params);
6869}
6870
Jens Axboe66f4af92020-01-16 15:36:52 -07006871static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
6872{
6873 struct io_uring_probe *p;
6874 size_t size;
6875 int i, ret;
6876
6877 size = struct_size(p, ops, nr_args);
6878 if (size == SIZE_MAX)
6879 return -EOVERFLOW;
6880 p = kzalloc(size, GFP_KERNEL);
6881 if (!p)
6882 return -ENOMEM;
6883
6884 ret = -EFAULT;
6885 if (copy_from_user(p, arg, size))
6886 goto out;
6887 ret = -EINVAL;
6888 if (memchr_inv(p, 0, size))
6889 goto out;
6890
6891 p->last_op = IORING_OP_LAST - 1;
6892 if (nr_args > IORING_OP_LAST)
6893 nr_args = IORING_OP_LAST;
6894
6895 for (i = 0; i < nr_args; i++) {
6896 p->ops[i].op = i;
6897 if (!io_op_defs[i].not_supported)
6898 p->ops[i].flags = IO_URING_OP_SUPPORTED;
6899 }
6900 p->ops_len = i;
6901
6902 ret = 0;
6903 if (copy_to_user(arg, p, size))
6904 ret = -EFAULT;
6905out:
6906 kfree(p);
6907 return ret;
6908}
6909
Jens Axboe071698e2020-01-28 10:04:42 -07006910static int io_register_personality(struct io_ring_ctx *ctx)
6911{
6912 const struct cred *creds = get_current_cred();
6913 int id;
6914
6915 id = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1,
6916 USHRT_MAX, GFP_KERNEL);
6917 if (id < 0)
6918 put_cred(creds);
6919 return id;
6920}
6921
6922static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
6923{
6924 const struct cred *old_creds;
6925
6926 old_creds = idr_remove(&ctx->personality_idr, id);
6927 if (old_creds) {
6928 put_cred(old_creds);
6929 return 0;
6930 }
6931
6932 return -EINVAL;
6933}
6934
6935static bool io_register_op_must_quiesce(int op)
6936{
6937 switch (op) {
6938 case IORING_UNREGISTER_FILES:
6939 case IORING_REGISTER_FILES_UPDATE:
6940 case IORING_REGISTER_PROBE:
6941 case IORING_REGISTER_PERSONALITY:
6942 case IORING_UNREGISTER_PERSONALITY:
6943 return false;
6944 default:
6945 return true;
6946 }
6947}
6948
Jens Axboeedafcce2019-01-09 09:16:05 -07006949static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
6950 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06006951 __releases(ctx->uring_lock)
6952 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07006953{
6954 int ret;
6955
Jens Axboe35fa71a2019-04-22 10:23:23 -06006956 /*
6957 * We're inside the ring mutex, if the ref is already dying, then
6958 * someone else killed the ctx or is already going through
6959 * io_uring_register().
6960 */
6961 if (percpu_ref_is_dying(&ctx->refs))
6962 return -ENXIO;
6963
Jens Axboe071698e2020-01-28 10:04:42 -07006964 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006965 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06006966
Jens Axboe05f3fb32019-12-09 11:22:50 -07006967 /*
6968 * Drop uring mutex before waiting for references to exit. If
6969 * another thread is currently inside io_uring_enter() it might
6970 * need to grab the uring_lock to make progress. If we hold it
6971 * here across the drain wait, then we can deadlock. It's safe
6972 * to drop the mutex here, since no new references will come in
6973 * after we've killed the percpu ref.
6974 */
6975 mutex_unlock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07006976 ret = wait_for_completion_interruptible(&ctx->completions[0]);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006977 mutex_lock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07006978 if (ret) {
6979 percpu_ref_resurrect(&ctx->refs);
6980 ret = -EINTR;
6981 goto out;
6982 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07006983 }
Jens Axboeedafcce2019-01-09 09:16:05 -07006984
6985 switch (opcode) {
6986 case IORING_REGISTER_BUFFERS:
6987 ret = io_sqe_buffer_register(ctx, arg, nr_args);
6988 break;
6989 case IORING_UNREGISTER_BUFFERS:
6990 ret = -EINVAL;
6991 if (arg || nr_args)
6992 break;
6993 ret = io_sqe_buffer_unregister(ctx);
6994 break;
Jens Axboe6b063142019-01-10 22:13:58 -07006995 case IORING_REGISTER_FILES:
6996 ret = io_sqe_files_register(ctx, arg, nr_args);
6997 break;
6998 case IORING_UNREGISTER_FILES:
6999 ret = -EINVAL;
7000 if (arg || nr_args)
7001 break;
7002 ret = io_sqe_files_unregister(ctx);
7003 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06007004 case IORING_REGISTER_FILES_UPDATE:
7005 ret = io_sqe_files_update(ctx, arg, nr_args);
7006 break;
Jens Axboe9b402842019-04-11 11:45:41 -06007007 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -07007008 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -06007009 ret = -EINVAL;
7010 if (nr_args != 1)
7011 break;
7012 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -07007013 if (ret)
7014 break;
7015 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
7016 ctx->eventfd_async = 1;
7017 else
7018 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -06007019 break;
7020 case IORING_UNREGISTER_EVENTFD:
7021 ret = -EINVAL;
7022 if (arg || nr_args)
7023 break;
7024 ret = io_eventfd_unregister(ctx);
7025 break;
Jens Axboe66f4af92020-01-16 15:36:52 -07007026 case IORING_REGISTER_PROBE:
7027 ret = -EINVAL;
7028 if (!arg || nr_args > 256)
7029 break;
7030 ret = io_probe(ctx, arg, nr_args);
7031 break;
Jens Axboe071698e2020-01-28 10:04:42 -07007032 case IORING_REGISTER_PERSONALITY:
7033 ret = -EINVAL;
7034 if (arg || nr_args)
7035 break;
7036 ret = io_register_personality(ctx);
7037 break;
7038 case IORING_UNREGISTER_PERSONALITY:
7039 ret = -EINVAL;
7040 if (arg)
7041 break;
7042 ret = io_unregister_personality(ctx, nr_args);
7043 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07007044 default:
7045 ret = -EINVAL;
7046 break;
7047 }
7048
Jens Axboe071698e2020-01-28 10:04:42 -07007049 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007050 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -07007051 percpu_ref_reinit(&ctx->refs);
Jens Axboec1503682020-01-08 08:26:07 -07007052out:
7053 reinit_completion(&ctx->completions[0]);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007054 }
Jens Axboeedafcce2019-01-09 09:16:05 -07007055 return ret;
7056}
7057
7058SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
7059 void __user *, arg, unsigned int, nr_args)
7060{
7061 struct io_ring_ctx *ctx;
7062 long ret = -EBADF;
7063 struct fd f;
7064
7065 f = fdget(fd);
7066 if (!f.file)
7067 return -EBADF;
7068
7069 ret = -EOPNOTSUPP;
7070 if (f.file->f_op != &io_uring_fops)
7071 goto out_fput;
7072
7073 ctx = f.file->private_data;
7074
7075 mutex_lock(&ctx->uring_lock);
7076 ret = __io_uring_register(ctx, opcode, arg, nr_args);
7077 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02007078 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
7079 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07007080out_fput:
7081 fdput(f);
7082 return ret;
7083}
7084
Jens Axboe2b188cc2019-01-07 10:46:33 -07007085static int __init io_uring_init(void)
7086{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01007087#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
7088 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
7089 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
7090} while (0)
7091
7092#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
7093 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
7094 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
7095 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
7096 BUILD_BUG_SQE_ELEM(1, __u8, flags);
7097 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
7098 BUILD_BUG_SQE_ELEM(4, __s32, fd);
7099 BUILD_BUG_SQE_ELEM(8, __u64, off);
7100 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
7101 BUILD_BUG_SQE_ELEM(16, __u64, addr);
7102 BUILD_BUG_SQE_ELEM(24, __u32, len);
7103 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
7104 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
7105 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
7106 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
7107 BUILD_BUG_SQE_ELEM(28, __u16, poll_events);
7108 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
7109 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
7110 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
7111 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
7112 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
7113 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
7114 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
7115 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
7116 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
7117 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
7118 BUILD_BUG_SQE_ELEM(42, __u16, personality);
7119
Jens Axboed3656342019-12-18 09:50:26 -07007120 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007121 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
7122 return 0;
7123};
7124__initcall(io_uring_init);