blob: ebf3b43fb91bf61eeb3dad9edc26d5e73807d543 [file] [log] [blame]
Jens Axboe2b188cc2019-01-07 10:46:33 -07001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Shared application/kernel submission and completion ring pairs, for
4 * supporting fast/efficient IO.
5 *
6 * A note on the read/write ordering memory barriers that are matched between
Stefan Bühler1e84b972019-04-24 23:54:16 +02007 * the application and kernel side.
8 *
9 * After the application reads the CQ ring tail, it must use an
10 * appropriate smp_rmb() to pair with the smp_wmb() the kernel uses
11 * before writing the tail (using smp_load_acquire to read the tail will
12 * do). It also needs a smp_mb() before updating CQ head (ordering the
13 * entry load(s) with the head store), pairing with an implicit barrier
14 * through a control-dependency in io_get_cqring (smp_store_release to
15 * store head will do). Failure to do so could lead to reading invalid
16 * CQ entries.
17 *
18 * Likewise, the application must use an appropriate smp_wmb() before
19 * writing the SQ tail (ordering SQ entry stores with the tail store),
20 * which pairs with smp_load_acquire in io_get_sqring (smp_store_release
21 * to store the tail will do). And it needs a barrier ordering the SQ
22 * head load before writing new SQ entries (smp_load_acquire to read
23 * head will do).
24 *
25 * When using the SQ poll thread (IORING_SETUP_SQPOLL), the application
26 * needs to check the SQ flags for IORING_SQ_NEED_WAKEUP *after*
27 * updating the SQ tail; a full memory barrier smp_mb() is needed
28 * between.
Jens Axboe2b188cc2019-01-07 10:46:33 -070029 *
30 * Also see the examples in the liburing library:
31 *
32 * git://git.kernel.dk/liburing
33 *
34 * io_uring also uses READ/WRITE_ONCE() for _any_ store or load that happens
35 * from data shared between the kernel and application. This is done both
36 * for ordering purposes, but also to ensure that once a value is loaded from
37 * data that the application could potentially modify, it remains stable.
38 *
39 * Copyright (C) 2018-2019 Jens Axboe
Christoph Hellwigc992fe22019-01-11 09:43:02 -070040 * Copyright (c) 2018-2019 Christoph Hellwig
Jens Axboe2b188cc2019-01-07 10:46:33 -070041 */
42#include <linux/kernel.h>
43#include <linux/init.h>
44#include <linux/errno.h>
45#include <linux/syscalls.h>
46#include <linux/compat.h>
47#include <linux/refcount.h>
48#include <linux/uio.h>
Pavel Begunkov6b47ee62020-01-18 20:22:41 +030049#include <linux/bits.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070050
51#include <linux/sched/signal.h>
52#include <linux/fs.h>
53#include <linux/file.h>
54#include <linux/fdtable.h>
55#include <linux/mm.h>
56#include <linux/mman.h>
57#include <linux/mmu_context.h>
58#include <linux/percpu.h>
59#include <linux/slab.h>
Jens Axboe6c271ce2019-01-10 11:22:30 -070060#include <linux/kthread.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070061#include <linux/blkdev.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070062#include <linux/bvec.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070063#include <linux/net.h>
64#include <net/sock.h>
65#include <net/af_unix.h>
Jens Axboe6b063142019-01-10 22:13:58 -070066#include <net/scm.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070067#include <linux/anon_inodes.h>
68#include <linux/sched/mm.h>
69#include <linux/uaccess.h>
70#include <linux/nospec.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070071#include <linux/sizes.h>
72#include <linux/hugetlb.h>
Jens Axboeaa4c3962019-11-29 10:14:00 -070073#include <linux/highmem.h>
Jens Axboe15b71ab2019-12-11 11:20:36 -070074#include <linux/namei.h>
75#include <linux/fsnotify.h>
Jens Axboe4840e412019-12-25 22:03:45 -070076#include <linux/fadvise.h>
Jens Axboe3e4827b2020-01-08 15:18:09 -070077#include <linux/eventpoll.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070078
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020079#define CREATE_TRACE_POINTS
80#include <trace/events/io_uring.h>
81
Jens Axboe2b188cc2019-01-07 10:46:33 -070082#include <uapi/linux/io_uring.h>
83
84#include "internal.h"
Jens Axboe561fb042019-10-24 07:25:42 -060085#include "io-wq.h"
Jens Axboe2b188cc2019-01-07 10:46:33 -070086
Daniel Xu5277dea2019-09-14 14:23:45 -070087#define IORING_MAX_ENTRIES 32768
Jens Axboe33a107f2019-10-04 12:10:03 -060088#define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES)
Jens Axboe65e19f52019-10-26 07:20:21 -060089
90/*
91 * Shift of 9 is 512 entries, or exactly one page on 64-bit archs
92 */
93#define IORING_FILE_TABLE_SHIFT 9
94#define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT)
95#define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1)
96#define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE)
Jens Axboe2b188cc2019-01-07 10:46:33 -070097
98struct io_uring {
99 u32 head ____cacheline_aligned_in_smp;
100 u32 tail ____cacheline_aligned_in_smp;
101};
102
Stefan Bühler1e84b972019-04-24 23:54:16 +0200103/*
Hristo Venev75b28af2019-08-26 17:23:46 +0000104 * This data is shared with the application through the mmap at offsets
105 * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200106 *
107 * The offsets to the member fields are published through struct
108 * io_sqring_offsets when calling io_uring_setup.
109 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000110struct io_rings {
Stefan Bühler1e84b972019-04-24 23:54:16 +0200111 /*
112 * Head and tail offsets into the ring; the offsets need to be
113 * masked to get valid indices.
114 *
Hristo Venev75b28af2019-08-26 17:23:46 +0000115 * The kernel controls head of the sq ring and the tail of the cq ring,
116 * and the application controls tail of the sq ring and the head of the
117 * cq ring.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200118 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000119 struct io_uring sq, cq;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200120 /*
Hristo Venev75b28af2019-08-26 17:23:46 +0000121 * Bitmasks to apply to head and tail offsets (constant, equals
Stefan Bühler1e84b972019-04-24 23:54:16 +0200122 * ring_entries - 1)
123 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000124 u32 sq_ring_mask, cq_ring_mask;
125 /* Ring sizes (constant, power of 2) */
126 u32 sq_ring_entries, cq_ring_entries;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200127 /*
128 * Number of invalid entries dropped by the kernel due to
129 * invalid index stored in array
130 *
131 * Written by the kernel, shouldn't be modified by the
132 * application (i.e. get number of "new events" by comparing to
133 * cached value).
134 *
135 * After a new SQ head value was read by the application this
136 * counter includes all submissions that were dropped reaching
137 * the new SQ head (and possibly more).
138 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000139 u32 sq_dropped;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200140 /*
141 * Runtime flags
142 *
143 * Written by the kernel, shouldn't be modified by the
144 * application.
145 *
146 * The application needs a full memory barrier before checking
147 * for IORING_SQ_NEED_WAKEUP after updating the sq tail.
148 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000149 u32 sq_flags;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200150 /*
151 * Number of completion events lost because the queue was full;
152 * this should be avoided by the application by making sure
LimingWu0b4295b2019-12-05 20:18:18 +0800153 * there are not more requests pending than there is space in
Stefan Bühler1e84b972019-04-24 23:54:16 +0200154 * the completion queue.
155 *
156 * Written by the kernel, shouldn't be modified by the
157 * application (i.e. get number of "new events" by comparing to
158 * cached value).
159 *
160 * As completion events come in out of order this counter is not
161 * ordered with any other data.
162 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000163 u32 cq_overflow;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200164 /*
165 * Ring buffer of completion events.
166 *
167 * The kernel writes completion events fresh every time they are
168 * produced, so the application is allowed to modify pending
169 * entries.
170 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000171 struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700172};
173
Jens Axboeedafcce2019-01-09 09:16:05 -0700174struct io_mapped_ubuf {
175 u64 ubuf;
176 size_t len;
177 struct bio_vec *bvec;
178 unsigned int nr_bvecs;
179};
180
Jens Axboe65e19f52019-10-26 07:20:21 -0600181struct fixed_file_table {
182 struct file **files;
Jens Axboe31b51512019-01-18 22:56:34 -0700183};
184
Jens Axboe05f3fb32019-12-09 11:22:50 -0700185enum {
186 FFD_F_ATOMIC,
187};
188
189struct fixed_file_data {
190 struct fixed_file_table *table;
191 struct io_ring_ctx *ctx;
192
193 struct percpu_ref refs;
194 struct llist_head put_llist;
195 unsigned long state;
196 struct work_struct ref_work;
197 struct completion done;
198};
199
Jens Axboe2b188cc2019-01-07 10:46:33 -0700200struct io_ring_ctx {
201 struct {
202 struct percpu_ref refs;
203 } ____cacheline_aligned_in_smp;
204
205 struct {
206 unsigned int flags;
Randy Dunlape1d85332020-02-05 20:57:10 -0800207 unsigned int compat: 1;
208 unsigned int account_mem: 1;
209 unsigned int cq_overflow_flushed: 1;
210 unsigned int drain_next: 1;
211 unsigned int eventfd_async: 1;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700212
Hristo Venev75b28af2019-08-26 17:23:46 +0000213 /*
214 * Ring buffer of indices into array of io_uring_sqe, which is
215 * mmapped by the application using the IORING_OFF_SQES offset.
216 *
217 * This indirection could e.g. be used to assign fixed
218 * io_uring_sqe entries to operations and only submit them to
219 * the queue when needed.
220 *
221 * The kernel modifies neither the indices array nor the entries
222 * array.
223 */
224 u32 *sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700225 unsigned cached_sq_head;
226 unsigned sq_entries;
227 unsigned sq_mask;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700228 unsigned sq_thread_idle;
Jens Axboe498ccd92019-10-25 10:04:25 -0600229 unsigned cached_sq_dropped;
Jens Axboe206aefd2019-11-07 18:27:42 -0700230 atomic_t cached_cq_overflow;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700231 unsigned long sq_check_overflow;
Jens Axboede0617e2019-04-06 21:51:27 -0600232
233 struct list_head defer_list;
Jens Axboe5262f562019-09-17 12:26:57 -0600234 struct list_head timeout_list;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700235 struct list_head cq_overflow_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700236
Jens Axboefcb323c2019-10-24 12:39:47 -0600237 wait_queue_head_t inflight_wait;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700238 struct io_uring_sqe *sq_sqes;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700239 } ____cacheline_aligned_in_smp;
240
Hristo Venev75b28af2019-08-26 17:23:46 +0000241 struct io_rings *rings;
242
Jens Axboe2b188cc2019-01-07 10:46:33 -0700243 /* IO offload */
Jens Axboe561fb042019-10-24 07:25:42 -0600244 struct io_wq *io_wq;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700245 struct task_struct *sqo_thread; /* if using sq thread polling */
246 struct mm_struct *sqo_mm;
247 wait_queue_head_t sqo_wait;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700248
Jens Axboe6b063142019-01-10 22:13:58 -0700249 /*
250 * If used, fixed file set. Writers must ensure that ->refs is dead,
251 * readers must ensure that ->refs is alive as long as the file* is
252 * used. Only updated through io_uring_register(2).
253 */
Jens Axboe05f3fb32019-12-09 11:22:50 -0700254 struct fixed_file_data *file_data;
Jens Axboe6b063142019-01-10 22:13:58 -0700255 unsigned nr_user_files;
Pavel Begunkovb14cca02020-01-17 04:45:59 +0300256 int ring_fd;
257 struct file *ring_file;
Jens Axboe6b063142019-01-10 22:13:58 -0700258
Jens Axboeedafcce2019-01-09 09:16:05 -0700259 /* if used, fixed mapped user buffers */
260 unsigned nr_user_bufs;
261 struct io_mapped_ubuf *user_bufs;
262
Jens Axboe2b188cc2019-01-07 10:46:33 -0700263 struct user_struct *user;
264
Jens Axboe0b8c0ec2019-12-02 08:50:00 -0700265 const struct cred *creds;
Jens Axboe181e4482019-11-25 08:52:30 -0700266
Jens Axboe206aefd2019-11-07 18:27:42 -0700267 /* 0 is for ctx quiesce/reinit/free, 1 is for sqo_thread started */
268 struct completion *completions;
269
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700270 /* if all else fails... */
271 struct io_kiocb *fallback_req;
272
Jens Axboe206aefd2019-11-07 18:27:42 -0700273#if defined(CONFIG_UNIX)
274 struct socket *ring_sock;
275#endif
276
Jens Axboe071698e2020-01-28 10:04:42 -0700277 struct idr personality_idr;
278
Jens Axboe206aefd2019-11-07 18:27:42 -0700279 struct {
280 unsigned cached_cq_tail;
281 unsigned cq_entries;
282 unsigned cq_mask;
283 atomic_t cq_timeouts;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700284 unsigned long cq_check_overflow;
Jens Axboe206aefd2019-11-07 18:27:42 -0700285 struct wait_queue_head cq_wait;
286 struct fasync_struct *cq_fasync;
287 struct eventfd_ctx *cq_ev_fd;
288 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700289
290 struct {
291 struct mutex uring_lock;
292 wait_queue_head_t wait;
293 } ____cacheline_aligned_in_smp;
294
295 struct {
296 spinlock_t completion_lock;
Jens Axboee94f1412019-12-19 12:06:02 -0700297 struct llist_head poll_llist;
298
Jens Axboedef596e2019-01-09 08:59:42 -0700299 /*
300 * ->poll_list is protected by the ctx->uring_lock for
301 * io_uring instances that don't use IORING_SETUP_SQPOLL.
302 * For SQPOLL, only the single threaded io_sq_thread() will
303 * manipulate the list, hence no extra locking is needed there.
304 */
305 struct list_head poll_list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700306 struct hlist_head *cancel_hash;
307 unsigned cancel_hash_bits;
Jens Axboee94f1412019-12-19 12:06:02 -0700308 bool poll_multi_file;
Jens Axboefcb323c2019-10-24 12:39:47 -0600309
310 spinlock_t inflight_lock;
311 struct list_head inflight_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700312 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700313};
314
Jens Axboe09bb8392019-03-13 12:39:28 -0600315/*
316 * First field must be the file pointer in all the
317 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
318 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700319struct io_poll_iocb {
320 struct file *file;
Jens Axboe0969e782019-12-17 18:40:57 -0700321 union {
322 struct wait_queue_head *head;
323 u64 addr;
324 };
Jens Axboe221c5eb2019-01-17 09:41:58 -0700325 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600326 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700327 bool canceled;
Jens Axboe392edb42019-12-09 17:52:20 -0700328 struct wait_queue_entry wait;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700329};
330
Jens Axboeb5dba592019-12-11 14:02:38 -0700331struct io_close {
332 struct file *file;
333 struct file *put_file;
334 int fd;
335};
336
Jens Axboead8a48a2019-11-15 08:49:11 -0700337struct io_timeout_data {
338 struct io_kiocb *req;
339 struct hrtimer timer;
340 struct timespec64 ts;
341 enum hrtimer_mode mode;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +0300342 u32 seq_offset;
Jens Axboead8a48a2019-11-15 08:49:11 -0700343};
344
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700345struct io_accept {
346 struct file *file;
347 struct sockaddr __user *addr;
348 int __user *addr_len;
349 int flags;
350};
351
352struct io_sync {
353 struct file *file;
354 loff_t len;
355 loff_t off;
356 int flags;
Jens Axboed63d1b52019-12-10 10:38:56 -0700357 int mode;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700358};
359
Jens Axboefbf23842019-12-17 18:45:56 -0700360struct io_cancel {
361 struct file *file;
362 u64 addr;
363};
364
Jens Axboeb29472e2019-12-17 18:50:29 -0700365struct io_timeout {
366 struct file *file;
367 u64 addr;
368 int flags;
Jens Axboe26a61672019-12-20 09:02:01 -0700369 unsigned count;
Jens Axboeb29472e2019-12-17 18:50:29 -0700370};
371
Jens Axboe9adbd452019-12-20 08:45:55 -0700372struct io_rw {
373 /* NOTE: kiocb has the file as the first member, so don't do it here */
374 struct kiocb kiocb;
375 u64 addr;
376 u64 len;
377};
378
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700379struct io_connect {
380 struct file *file;
381 struct sockaddr __user *addr;
382 int addr_len;
383};
384
Jens Axboee47293f2019-12-20 08:58:21 -0700385struct io_sr_msg {
386 struct file *file;
Jens Axboefddafac2020-01-04 20:19:44 -0700387 union {
388 struct user_msghdr __user *msg;
389 void __user *buf;
390 };
Jens Axboee47293f2019-12-20 08:58:21 -0700391 int msg_flags;
Jens Axboefddafac2020-01-04 20:19:44 -0700392 size_t len;
Jens Axboee47293f2019-12-20 08:58:21 -0700393};
394
Jens Axboe15b71ab2019-12-11 11:20:36 -0700395struct io_open {
396 struct file *file;
397 int dfd;
Jens Axboeeddc7ef2019-12-13 21:18:10 -0700398 union {
Jens Axboeeddc7ef2019-12-13 21:18:10 -0700399 unsigned mask;
400 };
Jens Axboe15b71ab2019-12-11 11:20:36 -0700401 struct filename *filename;
Jens Axboeeddc7ef2019-12-13 21:18:10 -0700402 struct statx __user *buffer;
Jens Axboec12cedf2020-01-08 17:41:21 -0700403 struct open_how how;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700404};
405
Jens Axboe05f3fb32019-12-09 11:22:50 -0700406struct io_files_update {
407 struct file *file;
408 u64 arg;
409 u32 nr_args;
410 u32 offset;
411};
412
Jens Axboe4840e412019-12-25 22:03:45 -0700413struct io_fadvise {
414 struct file *file;
415 u64 offset;
416 u32 len;
417 u32 advice;
418};
419
Jens Axboec1ca7572019-12-25 22:18:28 -0700420struct io_madvise {
421 struct file *file;
422 u64 addr;
423 u32 len;
424 u32 advice;
425};
426
Jens Axboe3e4827b2020-01-08 15:18:09 -0700427struct io_epoll {
428 struct file *file;
429 int epfd;
430 int op;
431 int fd;
432 struct epoll_event event;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700433};
434
Jens Axboef499a022019-12-02 16:28:46 -0700435struct io_async_connect {
436 struct sockaddr_storage address;
437};
438
Jens Axboe03b12302019-12-02 18:50:25 -0700439struct io_async_msghdr {
440 struct iovec fast_iov[UIO_FASTIOV];
441 struct iovec *iov;
442 struct sockaddr __user *uaddr;
443 struct msghdr msg;
444};
445
Jens Axboef67676d2019-12-02 11:03:47 -0700446struct io_async_rw {
447 struct iovec fast_iov[UIO_FASTIOV];
448 struct iovec *iov;
449 ssize_t nr_segs;
450 ssize_t size;
451};
452
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700453struct io_async_ctx {
Jens Axboef67676d2019-12-02 11:03:47 -0700454 union {
455 struct io_async_rw rw;
Jens Axboe03b12302019-12-02 18:50:25 -0700456 struct io_async_msghdr msg;
Jens Axboef499a022019-12-02 16:28:46 -0700457 struct io_async_connect connect;
Jens Axboe2d283902019-12-04 11:08:05 -0700458 struct io_timeout_data timeout;
Jens Axboef67676d2019-12-02 11:03:47 -0700459 };
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700460};
461
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300462enum {
463 REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT,
464 REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT,
465 REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT,
466 REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT,
467 REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT,
468
469 REQ_F_LINK_NEXT_BIT,
470 REQ_F_FAIL_LINK_BIT,
471 REQ_F_INFLIGHT_BIT,
472 REQ_F_CUR_POS_BIT,
473 REQ_F_NOWAIT_BIT,
474 REQ_F_IOPOLL_COMPLETED_BIT,
475 REQ_F_LINK_TIMEOUT_BIT,
476 REQ_F_TIMEOUT_BIT,
477 REQ_F_ISREG_BIT,
478 REQ_F_MUST_PUNT_BIT,
479 REQ_F_TIMEOUT_NOSEQ_BIT,
480 REQ_F_COMP_LOCKED_BIT,
481};
482
483enum {
484 /* ctx owns file */
485 REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT),
486 /* drain existing IO first */
487 REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT),
488 /* linked sqes */
489 REQ_F_LINK = BIT(REQ_F_LINK_BIT),
490 /* doesn't sever on completion < 0 */
491 REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT),
492 /* IOSQE_ASYNC */
493 REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT),
494
495 /* already grabbed next link */
496 REQ_F_LINK_NEXT = BIT(REQ_F_LINK_NEXT_BIT),
497 /* fail rest of links */
498 REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT),
499 /* on inflight list */
500 REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT),
501 /* read/write uses file position */
502 REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT),
503 /* must not punt to workers */
504 REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
505 /* polled IO has completed */
506 REQ_F_IOPOLL_COMPLETED = BIT(REQ_F_IOPOLL_COMPLETED_BIT),
507 /* has linked timeout */
508 REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT),
509 /* timeout request */
510 REQ_F_TIMEOUT = BIT(REQ_F_TIMEOUT_BIT),
511 /* regular file */
512 REQ_F_ISREG = BIT(REQ_F_ISREG_BIT),
513 /* must be punted even for NONBLOCK */
514 REQ_F_MUST_PUNT = BIT(REQ_F_MUST_PUNT_BIT),
515 /* no timeout sequence */
516 REQ_F_TIMEOUT_NOSEQ = BIT(REQ_F_TIMEOUT_NOSEQ_BIT),
517 /* completion under lock */
518 REQ_F_COMP_LOCKED = BIT(REQ_F_COMP_LOCKED_BIT),
519};
520
Jens Axboe09bb8392019-03-13 12:39:28 -0600521/*
522 * NOTE! Each of the iocb union members has the file pointer
523 * as the first entry in their struct definition. So you can
524 * access the file pointer through any of the sub-structs,
525 * or directly as just 'ki_filp' in this struct.
526 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700527struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700528 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600529 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700530 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700531 struct io_poll_iocb poll;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700532 struct io_accept accept;
533 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700534 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700535 struct io_timeout timeout;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700536 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700537 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700538 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700539 struct io_close close;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700540 struct io_files_update files_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700541 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700542 struct io_madvise madvise;
Jens Axboe3e4827b2020-01-08 15:18:09 -0700543 struct io_epoll epoll;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700544 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700545
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700546 struct io_async_ctx *io;
Pavel Begunkovb14cca02020-01-17 04:45:59 +0300547 /*
548 * llist_node is only used for poll deferred completions
549 */
550 struct llist_node llist_node;
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300551 bool in_async;
552 bool needs_fixed_file;
Jens Axboed625c6e2019-12-17 19:53:05 -0700553 u8 opcode;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700554
555 struct io_ring_ctx *ctx;
Jens Axboeeac406c2019-11-14 12:09:58 -0700556 union {
557 struct list_head list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700558 struct hlist_node hash_node;
Jens Axboeeac406c2019-11-14 12:09:58 -0700559 };
Jens Axboe9e645e112019-05-10 16:07:28 -0600560 struct list_head link_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700561 unsigned int flags;
Jens Axboec16361c2019-01-17 08:39:48 -0700562 refcount_t refs;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700563 u64 user_data;
Jens Axboe9e645e112019-05-10 16:07:28 -0600564 u32 result;
Jens Axboede0617e2019-04-06 21:51:27 -0600565 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700566
Jens Axboefcb323c2019-10-24 12:39:47 -0600567 struct list_head inflight_entry;
568
Jens Axboe561fb042019-10-24 07:25:42 -0600569 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700570};
571
572#define IO_PLUG_THRESHOLD 2
Jens Axboedef596e2019-01-09 08:59:42 -0700573#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700574
Jens Axboe9a56a232019-01-09 09:06:50 -0700575struct io_submit_state {
576 struct blk_plug plug;
577
578 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700579 * io_kiocb alloc cache
580 */
581 void *reqs[IO_IOPOLL_BATCH];
Pavel Begunkov6c8a3132020-02-01 03:58:00 +0300582 unsigned int free_reqs;
Jens Axboe2579f912019-01-09 09:10:43 -0700583
584 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700585 * File reference cache
586 */
587 struct file *file;
588 unsigned int fd;
589 unsigned int has_refs;
590 unsigned int used_refs;
591 unsigned int ios_left;
592};
593
Jens Axboed3656342019-12-18 09:50:26 -0700594struct io_op_def {
595 /* needs req->io allocated for deferral/async */
596 unsigned async_ctx : 1;
597 /* needs current->mm setup, does mm access */
598 unsigned needs_mm : 1;
599 /* needs req->file assigned */
600 unsigned needs_file : 1;
601 /* needs req->file assigned IFF fd is >= 0 */
602 unsigned fd_non_neg : 1;
603 /* hash wq insertion if file is a regular file */
604 unsigned hash_reg_file : 1;
605 /* unbound wq insertion if file is a non-regular file */
606 unsigned unbound_nonreg_file : 1;
Jens Axboe66f4af92020-01-16 15:36:52 -0700607 /* opcode is not supported by this kernel */
608 unsigned not_supported : 1;
Jens Axboef86cd202020-01-29 13:46:44 -0700609 /* needs file table */
610 unsigned file_table : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700611};
612
613static const struct io_op_def io_op_defs[] = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300614 [IORING_OP_NOP] = {},
615 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700616 .async_ctx = 1,
617 .needs_mm = 1,
618 .needs_file = 1,
619 .unbound_nonreg_file = 1,
620 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300621 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700622 .async_ctx = 1,
623 .needs_mm = 1,
624 .needs_file = 1,
625 .hash_reg_file = 1,
626 .unbound_nonreg_file = 1,
627 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300628 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700629 .needs_file = 1,
630 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300631 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700632 .needs_file = 1,
633 .unbound_nonreg_file = 1,
634 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300635 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700636 .needs_file = 1,
637 .hash_reg_file = 1,
638 .unbound_nonreg_file = 1,
639 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300640 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700641 .needs_file = 1,
642 .unbound_nonreg_file = 1,
643 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300644 [IORING_OP_POLL_REMOVE] = {},
645 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700646 .needs_file = 1,
647 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300648 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700649 .async_ctx = 1,
650 .needs_mm = 1,
651 .needs_file = 1,
652 .unbound_nonreg_file = 1,
653 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300654 [IORING_OP_RECVMSG] = {
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_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700661 .async_ctx = 1,
662 .needs_mm = 1,
663 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300664 [IORING_OP_TIMEOUT_REMOVE] = {},
665 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700666 .needs_mm = 1,
667 .needs_file = 1,
668 .unbound_nonreg_file = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700669 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700670 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300671 [IORING_OP_ASYNC_CANCEL] = {},
672 [IORING_OP_LINK_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700673 .async_ctx = 1,
674 .needs_mm = 1,
675 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300676 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700677 .async_ctx = 1,
678 .needs_mm = 1,
679 .needs_file = 1,
680 .unbound_nonreg_file = 1,
681 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300682 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700683 .needs_file = 1,
684 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300685 [IORING_OP_OPENAT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700686 .needs_file = 1,
687 .fd_non_neg = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700688 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700689 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300690 [IORING_OP_CLOSE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700691 .needs_file = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700692 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700693 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300694 [IORING_OP_FILES_UPDATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700695 .needs_mm = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700696 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700697 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300698 [IORING_OP_STATX] = {
Jens Axboed3656342019-12-18 09:50:26 -0700699 .needs_mm = 1,
700 .needs_file = 1,
701 .fd_non_neg = 1,
702 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300703 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700704 .needs_mm = 1,
705 .needs_file = 1,
706 .unbound_nonreg_file = 1,
707 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300708 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700709 .needs_mm = 1,
710 .needs_file = 1,
711 .unbound_nonreg_file = 1,
712 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300713 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -0700714 .needs_file = 1,
715 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300716 [IORING_OP_MADVISE] = {
Jens Axboec1ca7572019-12-25 22:18:28 -0700717 .needs_mm = 1,
718 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300719 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700720 .needs_mm = 1,
721 .needs_file = 1,
722 .unbound_nonreg_file = 1,
723 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300724 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700725 .needs_mm = 1,
726 .needs_file = 1,
727 .unbound_nonreg_file = 1,
728 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300729 [IORING_OP_OPENAT2] = {
Jens Axboecebdb982020-01-08 17:59:24 -0700730 .needs_file = 1,
731 .fd_non_neg = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700732 .file_table = 1,
Jens Axboecebdb982020-01-08 17:59:24 -0700733 },
Jens Axboe3e4827b2020-01-08 15:18:09 -0700734 [IORING_OP_EPOLL_CTL] = {
735 .unbound_nonreg_file = 1,
736 .file_table = 1,
737 },
Jens Axboed3656342019-12-18 09:50:26 -0700738};
739
Jens Axboe561fb042019-10-24 07:25:42 -0600740static void io_wq_submit_work(struct io_wq_work **workptr);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700741static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800742static void io_put_req(struct io_kiocb *req);
Jens Axboe978db572019-11-14 22:39:04 -0700743static void __io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700744static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
745static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -0700746static int __io_sqe_files_update(struct io_ring_ctx *ctx,
747 struct io_uring_files_update *ip,
748 unsigned nr_args);
Jens Axboef86cd202020-01-29 13:46:44 -0700749static int io_grab_files(struct io_kiocb *req);
Jens Axboe2faf8522020-02-04 19:54:55 -0700750static void io_ring_file_ref_flush(struct fixed_file_data *data);
Jens Axboede0617e2019-04-06 21:51:27 -0600751
Jens Axboe2b188cc2019-01-07 10:46:33 -0700752static struct kmem_cache *req_cachep;
753
754static const struct file_operations io_uring_fops;
755
756struct sock *io_uring_get_socket(struct file *file)
757{
758#if defined(CONFIG_UNIX)
759 if (file->f_op == &io_uring_fops) {
760 struct io_ring_ctx *ctx = file->private_data;
761
762 return ctx->ring_sock->sk;
763 }
764#endif
765 return NULL;
766}
767EXPORT_SYMBOL(io_uring_get_socket);
768
769static void io_ring_ctx_ref_free(struct percpu_ref *ref)
770{
771 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
772
Jens Axboe206aefd2019-11-07 18:27:42 -0700773 complete(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700774}
775
776static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
777{
778 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -0700779 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700780
781 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
782 if (!ctx)
783 return NULL;
784
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700785 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
786 if (!ctx->fallback_req)
787 goto err;
788
Jens Axboe206aefd2019-11-07 18:27:42 -0700789 ctx->completions = kmalloc(2 * sizeof(struct completion), GFP_KERNEL);
790 if (!ctx->completions)
791 goto err;
792
Jens Axboe78076bb2019-12-04 19:56:40 -0700793 /*
794 * Use 5 bits less than the max cq entries, that should give us around
795 * 32 entries per hash list if totally full and uniformly spread.
796 */
797 hash_bits = ilog2(p->cq_entries);
798 hash_bits -= 5;
799 if (hash_bits <= 0)
800 hash_bits = 1;
801 ctx->cancel_hash_bits = hash_bits;
802 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
803 GFP_KERNEL);
804 if (!ctx->cancel_hash)
805 goto err;
806 __hash_init(ctx->cancel_hash, 1U << hash_bits);
807
Roman Gushchin21482892019-05-07 10:01:48 -0700808 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -0700809 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
810 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700811
812 ctx->flags = p->flags;
813 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700814 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe206aefd2019-11-07 18:27:42 -0700815 init_completion(&ctx->completions[0]);
816 init_completion(&ctx->completions[1]);
Jens Axboe071698e2020-01-28 10:04:42 -0700817 idr_init(&ctx->personality_idr);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700818 mutex_init(&ctx->uring_lock);
819 init_waitqueue_head(&ctx->wait);
820 spin_lock_init(&ctx->completion_lock);
Jens Axboee94f1412019-12-19 12:06:02 -0700821 init_llist_head(&ctx->poll_llist);
Jens Axboedef596e2019-01-09 08:59:42 -0700822 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboede0617e2019-04-06 21:51:27 -0600823 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -0600824 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -0600825 init_waitqueue_head(&ctx->inflight_wait);
826 spin_lock_init(&ctx->inflight_lock);
827 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700828 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -0700829err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700830 if (ctx->fallback_req)
831 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe206aefd2019-11-07 18:27:42 -0700832 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -0700833 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -0700834 kfree(ctx);
835 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700836}
837
Bob Liu9d858b22019-11-13 18:06:25 +0800838static inline bool __req_need_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -0600839{
Jackie Liua197f662019-11-08 08:09:12 -0700840 struct io_ring_ctx *ctx = req->ctx;
841
Jens Axboe498ccd92019-10-25 10:04:25 -0600842 return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped
843 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboede0617e2019-04-06 21:51:27 -0600844}
845
Bob Liu9d858b22019-11-13 18:06:25 +0800846static inline bool req_need_defer(struct io_kiocb *req)
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600847{
Pavel Begunkov87987892020-01-18 01:22:30 +0300848 if (unlikely(req->flags & REQ_F_IO_DRAIN))
Bob Liu9d858b22019-11-13 18:06:25 +0800849 return __req_need_defer(req);
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600850
Bob Liu9d858b22019-11-13 18:06:25 +0800851 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600852}
853
854static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -0600855{
856 struct io_kiocb *req;
857
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600858 req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list);
Bob Liu9d858b22019-11-13 18:06:25 +0800859 if (req && !req_need_defer(req)) {
Jens Axboede0617e2019-04-06 21:51:27 -0600860 list_del_init(&req->list);
861 return req;
862 }
863
864 return NULL;
865}
866
Jens Axboe5262f562019-09-17 12:26:57 -0600867static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx)
868{
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600869 struct io_kiocb *req;
870
871 req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list);
Jens Axboe93bd25b2019-11-11 23:34:31 -0700872 if (req) {
873 if (req->flags & REQ_F_TIMEOUT_NOSEQ)
874 return NULL;
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800875 if (!__req_need_defer(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -0700876 list_del_init(&req->list);
877 return req;
878 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600879 }
880
881 return NULL;
Jens Axboe5262f562019-09-17 12:26:57 -0600882}
883
Jens Axboede0617e2019-04-06 21:51:27 -0600884static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700885{
Hristo Venev75b28af2019-08-26 17:23:46 +0000886 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700887
Pavel Begunkov07910152020-01-17 03:52:46 +0300888 /* order cqe stores with ring update */
889 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700890
Pavel Begunkov07910152020-01-17 03:52:46 +0300891 if (wq_has_sleeper(&ctx->cq_wait)) {
892 wake_up_interruptible(&ctx->cq_wait);
893 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700894 }
895}
896
Jens Axboecccf0ee2020-01-27 16:34:48 -0700897static inline void io_req_work_grab_env(struct io_kiocb *req,
898 const struct io_op_def *def)
Jens Axboe18d9be12019-09-10 09:13:05 -0600899{
Jens Axboecccf0ee2020-01-27 16:34:48 -0700900 if (!req->work.mm && def->needs_mm) {
901 mmgrab(current->mm);
902 req->work.mm = current->mm;
903 }
904 if (!req->work.creds)
905 req->work.creds = get_current_cred();
906}
907
908static inline void io_req_work_drop_env(struct io_kiocb *req)
909{
910 if (req->work.mm) {
911 mmdrop(req->work.mm);
912 req->work.mm = NULL;
913 }
914 if (req->work.creds) {
915 put_cred(req->work.creds);
916 req->work.creds = NULL;
917 }
Jens Axboe561fb042019-10-24 07:25:42 -0600918}
919
Jens Axboe94ae5e72019-11-14 19:39:52 -0700920static inline bool io_prep_async_work(struct io_kiocb *req,
921 struct io_kiocb **link)
Jens Axboe561fb042019-10-24 07:25:42 -0600922{
Jens Axboed3656342019-12-18 09:50:26 -0700923 const struct io_op_def *def = &io_op_defs[req->opcode];
Jens Axboe561fb042019-10-24 07:25:42 -0600924 bool do_hashed = false;
Jens Axboe54a91f32019-09-10 09:15:04 -0600925
Jens Axboed3656342019-12-18 09:50:26 -0700926 if (req->flags & REQ_F_ISREG) {
927 if (def->hash_reg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -0700928 do_hashed = true;
Jens Axboed3656342019-12-18 09:50:26 -0700929 } else {
930 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -0700931 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -0600932 }
Jens Axboecccf0ee2020-01-27 16:34:48 -0700933
934 io_req_work_grab_env(req, def);
Jens Axboe54a91f32019-09-10 09:15:04 -0600935
Jens Axboe94ae5e72019-11-14 19:39:52 -0700936 *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -0600937 return do_hashed;
938}
939
Jackie Liua197f662019-11-08 08:09:12 -0700940static inline void io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -0600941{
Jackie Liua197f662019-11-08 08:09:12 -0700942 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe94ae5e72019-11-14 19:39:52 -0700943 struct io_kiocb *link;
944 bool do_hashed;
945
946 do_hashed = io_prep_async_work(req, &link);
Jens Axboe561fb042019-10-24 07:25:42 -0600947
948 trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work,
949 req->flags);
950 if (!do_hashed) {
951 io_wq_enqueue(ctx->io_wq, &req->work);
952 } else {
953 io_wq_enqueue_hashed(ctx->io_wq, &req->work,
954 file_inode(req->file));
955 }
Jens Axboe94ae5e72019-11-14 19:39:52 -0700956
957 if (link)
958 io_queue_linked_timeout(link);
Jens Axboe18d9be12019-09-10 09:13:05 -0600959}
960
Jens Axboe5262f562019-09-17 12:26:57 -0600961static void io_kill_timeout(struct io_kiocb *req)
962{
963 int ret;
964
Jens Axboe2d283902019-12-04 11:08:05 -0700965 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe5262f562019-09-17 12:26:57 -0600966 if (ret != -1) {
967 atomic_inc(&req->ctx->cq_timeouts);
Jens Axboe842f9612019-10-29 12:34:10 -0600968 list_del_init(&req->list);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700969 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800970 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -0600971 }
972}
973
974static void io_kill_timeouts(struct io_ring_ctx *ctx)
975{
976 struct io_kiocb *req, *tmp;
977
978 spin_lock_irq(&ctx->completion_lock);
979 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
980 io_kill_timeout(req);
981 spin_unlock_irq(&ctx->completion_lock);
982}
983
Jens Axboede0617e2019-04-06 21:51:27 -0600984static void io_commit_cqring(struct io_ring_ctx *ctx)
985{
986 struct io_kiocb *req;
987
Jens Axboe5262f562019-09-17 12:26:57 -0600988 while ((req = io_get_timeout_req(ctx)) != NULL)
989 io_kill_timeout(req);
990
Jens Axboede0617e2019-04-06 21:51:27 -0600991 __io_commit_cqring(ctx);
992
Pavel Begunkov87987892020-01-18 01:22:30 +0300993 while ((req = io_get_deferred_req(ctx)) != NULL)
Jackie Liua197f662019-11-08 08:09:12 -0700994 io_queue_async_work(req);
Jens Axboede0617e2019-04-06 21:51:27 -0600995}
996
Jens Axboe2b188cc2019-01-07 10:46:33 -0700997static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
998{
Hristo Venev75b28af2019-08-26 17:23:46 +0000999 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001000 unsigned tail;
1001
1002 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +02001003 /*
1004 * writes to the cq entry need to come after reading head; the
1005 * control dependency is enough as we're using WRITE_ONCE to
1006 * fill the cq entry
1007 */
Hristo Venev75b28af2019-08-26 17:23:46 +00001008 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001009 return NULL;
1010
1011 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +00001012 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001013}
1014
Jens Axboef2842ab2020-01-08 11:04:00 -07001015static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1016{
Jens Axboef0b493e2020-02-01 21:30:11 -07001017 if (!ctx->cq_ev_fd)
1018 return false;
Jens Axboef2842ab2020-01-08 11:04:00 -07001019 if (!ctx->eventfd_async)
1020 return true;
1021 return io_wq_current_is_worker() || in_interrupt();
1022}
1023
Jens Axboef0b493e2020-02-01 21:30:11 -07001024static void __io_cqring_ev_posted(struct io_ring_ctx *ctx, bool trigger_ev)
Jens Axboe8c838782019-03-12 15:48:16 -06001025{
1026 if (waitqueue_active(&ctx->wait))
1027 wake_up(&ctx->wait);
1028 if (waitqueue_active(&ctx->sqo_wait))
1029 wake_up(&ctx->sqo_wait);
Jens Axboef0b493e2020-02-01 21:30:11 -07001030 if (trigger_ev)
Jens Axboe9b402842019-04-11 11:45:41 -06001031 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -06001032}
1033
Jens Axboef0b493e2020-02-01 21:30:11 -07001034static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
1035{
1036 __io_cqring_ev_posted(ctx, io_should_trigger_evfd(ctx));
1037}
1038
Jens Axboec4a2ed72019-11-21 21:01:26 -07001039/* Returns true if there are no backlogged entries after the flush */
1040static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001041{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001042 struct io_rings *rings = ctx->rings;
1043 struct io_uring_cqe *cqe;
1044 struct io_kiocb *req;
1045 unsigned long flags;
1046 LIST_HEAD(list);
1047
1048 if (!force) {
1049 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001050 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001051 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
1052 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001053 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001054 }
1055
1056 spin_lock_irqsave(&ctx->completion_lock, flags);
1057
1058 /* if force is set, the ring is going away. always drop after that */
1059 if (force)
Jens Axboe69b3e542020-01-08 11:01:46 -07001060 ctx->cq_overflow_flushed = 1;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001061
Jens Axboec4a2ed72019-11-21 21:01:26 -07001062 cqe = NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001063 while (!list_empty(&ctx->cq_overflow_list)) {
1064 cqe = io_get_cqring(ctx);
1065 if (!cqe && !force)
1066 break;
1067
1068 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
1069 list);
1070 list_move(&req->list, &list);
1071 if (cqe) {
1072 WRITE_ONCE(cqe->user_data, req->user_data);
1073 WRITE_ONCE(cqe->res, req->result);
1074 WRITE_ONCE(cqe->flags, 0);
1075 } else {
1076 WRITE_ONCE(ctx->rings->cq_overflow,
1077 atomic_inc_return(&ctx->cached_cq_overflow));
1078 }
1079 }
1080
1081 io_commit_cqring(ctx);
Jens Axboead3eb2c2019-12-18 17:12:20 -07001082 if (cqe) {
1083 clear_bit(0, &ctx->sq_check_overflow);
1084 clear_bit(0, &ctx->cq_check_overflow);
1085 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001086 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1087 io_cqring_ev_posted(ctx);
1088
1089 while (!list_empty(&list)) {
1090 req = list_first_entry(&list, struct io_kiocb, list);
1091 list_del(&req->list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001092 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001093 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001094
1095 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001096}
1097
Jens Axboe78e19bb2019-11-06 15:21:34 -07001098static void io_cqring_fill_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001099{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001100 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001101 struct io_uring_cqe *cqe;
1102
Jens Axboe78e19bb2019-11-06 15:21:34 -07001103 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001104
Jens Axboe2b188cc2019-01-07 10:46:33 -07001105 /*
1106 * If we can't get a cq entry, userspace overflowed the
1107 * submission (by quite a lot). Increment the overflow count in
1108 * the ring.
1109 */
1110 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001111 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001112 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001113 WRITE_ONCE(cqe->res, res);
1114 WRITE_ONCE(cqe->flags, 0);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001115 } else if (ctx->cq_overflow_flushed) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001116 WRITE_ONCE(ctx->rings->cq_overflow,
1117 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001118 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001119 if (list_empty(&ctx->cq_overflow_list)) {
1120 set_bit(0, &ctx->sq_check_overflow);
1121 set_bit(0, &ctx->cq_check_overflow);
1122 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001123 refcount_inc(&req->refs);
1124 req->result = res;
1125 list_add_tail(&req->list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001126 }
1127}
1128
Jens Axboe78e19bb2019-11-06 15:21:34 -07001129static void io_cqring_add_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001130{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001131 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001132 unsigned long flags;
1133
1134 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001135 io_cqring_fill_event(req, res);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001136 io_commit_cqring(ctx);
1137 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1138
Jens Axboe8c838782019-03-12 15:48:16 -06001139 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001140}
1141
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001142static inline bool io_is_fallback_req(struct io_kiocb *req)
1143{
1144 return req == (struct io_kiocb *)
1145 ((unsigned long) req->ctx->fallback_req & ~1UL);
1146}
1147
1148static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
1149{
1150 struct io_kiocb *req;
1151
1152 req = ctx->fallback_req;
1153 if (!test_and_set_bit_lock(0, (unsigned long *) ctx->fallback_req))
1154 return req;
1155
1156 return NULL;
1157}
1158
Jens Axboe2579f912019-01-09 09:10:43 -07001159static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
1160 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001161{
Jens Axboefd6fab22019-03-14 16:30:06 -06001162 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001163 struct io_kiocb *req;
1164
Jens Axboe2579f912019-01-09 09:10:43 -07001165 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -06001166 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -07001167 if (unlikely(!req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001168 goto fallback;
Jens Axboe2579f912019-01-09 09:10:43 -07001169 } else if (!state->free_reqs) {
1170 size_t sz;
1171 int ret;
1172
1173 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -06001174 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
1175
1176 /*
1177 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1178 * retry single alloc to be on the safe side.
1179 */
1180 if (unlikely(ret <= 0)) {
1181 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1182 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001183 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -06001184 ret = 1;
1185 }
Jens Axboe2579f912019-01-09 09:10:43 -07001186 state->free_reqs = ret - 1;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001187 req = state->reqs[ret - 1];
Jens Axboe2579f912019-01-09 09:10:43 -07001188 } else {
Jens Axboe2579f912019-01-09 09:10:43 -07001189 state->free_reqs--;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001190 req = state->reqs[state->free_reqs];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001191 }
1192
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001193got_it:
Jens Axboe1a6b74f2019-12-02 10:33:15 -07001194 req->io = NULL;
Jens Axboe60c112b2019-06-21 10:20:18 -06001195 req->file = NULL;
Jens Axboe2579f912019-01-09 09:10:43 -07001196 req->ctx = ctx;
1197 req->flags = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06001198 /* one is dropped after submission, the other at completion */
1199 refcount_set(&req->refs, 2);
Jens Axboe9e645e112019-05-10 16:07:28 -06001200 req->result = 0;
Jens Axboe561fb042019-10-24 07:25:42 -06001201 INIT_IO_WORK(&req->work, io_wq_submit_work);
Jens Axboe2579f912019-01-09 09:10:43 -07001202 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001203fallback:
1204 req = io_get_fallback_req(ctx);
1205 if (req)
1206 goto got_it;
Pavel Begunkov6805b322019-10-08 02:18:42 +03001207 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001208 return NULL;
1209}
1210
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001211static void __io_req_do_free(struct io_kiocb *req)
Jens Axboedef596e2019-01-09 08:59:42 -07001212{
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001213 if (likely(!io_is_fallback_req(req)))
1214 kmem_cache_free(req_cachep, req);
1215 else
1216 clear_bit_unlock(0, (unsigned long *) req->ctx->fallback_req);
1217}
1218
Jens Axboec6ca97b302019-12-28 12:11:08 -07001219static void __io_req_aux_free(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001220{
Jens Axboefcb323c2019-10-24 12:39:47 -06001221 struct io_ring_ctx *ctx = req->ctx;
1222
YueHaibing96fd84d2020-01-07 22:22:44 +08001223 kfree(req->io);
Jens Axboe05f3fb32019-12-09 11:22:50 -07001224 if (req->file) {
1225 if (req->flags & REQ_F_FIXED_FILE)
1226 percpu_ref_put(&ctx->file_data->refs);
1227 else
1228 fput(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001229 }
Jens Axboecccf0ee2020-01-27 16:34:48 -07001230
1231 io_req_work_drop_env(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001232}
1233
1234static void __io_free_req(struct io_kiocb *req)
1235{
Jens Axboec6ca97b302019-12-28 12:11:08 -07001236 __io_req_aux_free(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001237
Jens Axboefcb323c2019-10-24 12:39:47 -06001238 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001239 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06001240 unsigned long flags;
1241
1242 spin_lock_irqsave(&ctx->inflight_lock, flags);
1243 list_del(&req->inflight_entry);
1244 if (waitqueue_active(&ctx->inflight_wait))
1245 wake_up(&ctx->inflight_wait);
1246 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1247 }
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001248
1249 percpu_ref_put(&req->ctx->refs);
1250 __io_req_do_free(req);
Jens Axboee65ef562019-03-12 10:16:44 -06001251}
1252
Jens Axboec6ca97b302019-12-28 12:11:08 -07001253struct req_batch {
1254 void *reqs[IO_IOPOLL_BATCH];
1255 int to_free;
1256 int need_iter;
1257};
1258
1259static void io_free_req_many(struct io_ring_ctx *ctx, struct req_batch *rb)
1260{
Jens Axboe10fef4b2020-01-09 07:52:28 -07001261 int fixed_refs = rb->to_free;
1262
Jens Axboec6ca97b302019-12-28 12:11:08 -07001263 if (!rb->to_free)
1264 return;
1265 if (rb->need_iter) {
1266 int i, inflight = 0;
1267 unsigned long flags;
1268
Jens Axboe10fef4b2020-01-09 07:52:28 -07001269 fixed_refs = 0;
Jens Axboec6ca97b302019-12-28 12:11:08 -07001270 for (i = 0; i < rb->to_free; i++) {
1271 struct io_kiocb *req = rb->reqs[i];
1272
Jens Axboe10fef4b2020-01-09 07:52:28 -07001273 if (req->flags & REQ_F_FIXED_FILE) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001274 req->file = NULL;
Jens Axboe10fef4b2020-01-09 07:52:28 -07001275 fixed_refs++;
1276 }
Jens Axboec6ca97b302019-12-28 12:11:08 -07001277 if (req->flags & REQ_F_INFLIGHT)
1278 inflight++;
Jens Axboec6ca97b302019-12-28 12:11:08 -07001279 __io_req_aux_free(req);
1280 }
1281 if (!inflight)
1282 goto do_free;
1283
1284 spin_lock_irqsave(&ctx->inflight_lock, flags);
1285 for (i = 0; i < rb->to_free; i++) {
1286 struct io_kiocb *req = rb->reqs[i];
1287
Jens Axboe10fef4b2020-01-09 07:52:28 -07001288 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001289 list_del(&req->inflight_entry);
1290 if (!--inflight)
1291 break;
1292 }
1293 }
1294 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1295
1296 if (waitqueue_active(&ctx->inflight_wait))
1297 wake_up(&ctx->inflight_wait);
1298 }
1299do_free:
1300 kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs);
Jens Axboe10fef4b2020-01-09 07:52:28 -07001301 if (fixed_refs)
1302 percpu_ref_put_many(&ctx->file_data->refs, fixed_refs);
Jens Axboec6ca97b302019-12-28 12:11:08 -07001303 percpu_ref_put_many(&ctx->refs, rb->to_free);
Jens Axboec6ca97b302019-12-28 12:11:08 -07001304 rb->to_free = rb->need_iter = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06001305}
1306
Jackie Liua197f662019-11-08 08:09:12 -07001307static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001308{
Jackie Liua197f662019-11-08 08:09:12 -07001309 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001310 int ret;
1311
Jens Axboe2d283902019-12-04 11:08:05 -07001312 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe2665abf2019-11-05 12:40:47 -07001313 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001314 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -07001315 io_commit_cqring(ctx);
1316 req->flags &= ~REQ_F_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001317 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001318 return true;
1319 }
1320
1321 return false;
1322}
1323
Jens Axboeba816ad2019-09-28 11:36:45 -06001324static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -06001325{
Jens Axboe2665abf2019-11-05 12:40:47 -07001326 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001327 bool wake_ev = false;
Jens Axboe9e645e112019-05-10 16:07:28 -06001328
Jens Axboe4d7dd462019-11-20 13:03:52 -07001329 /* Already got next link */
1330 if (req->flags & REQ_F_LINK_NEXT)
1331 return;
1332
Jens Axboe9e645e112019-05-10 16:07:28 -06001333 /*
1334 * The list should never be empty when we are called here. But could
1335 * potentially happen if the chain is messed up, check to be on the
1336 * safe side.
1337 */
Pavel Begunkov44932332019-12-05 16:16:35 +03001338 while (!list_empty(&req->link_list)) {
1339 struct io_kiocb *nxt = list_first_entry(&req->link_list,
1340 struct io_kiocb, link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001341
Pavel Begunkov44932332019-12-05 16:16:35 +03001342 if (unlikely((req->flags & REQ_F_LINK_TIMEOUT) &&
1343 (nxt->flags & REQ_F_TIMEOUT))) {
1344 list_del_init(&nxt->link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001345 wake_ev |= io_link_cancel_timeout(nxt);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001346 req->flags &= ~REQ_F_LINK_TIMEOUT;
1347 continue;
1348 }
Jens Axboe9e645e112019-05-10 16:07:28 -06001349
Pavel Begunkov44932332019-12-05 16:16:35 +03001350 list_del_init(&req->link_list);
1351 if (!list_empty(&nxt->link_list))
1352 nxt->flags |= REQ_F_LINK;
Pavel Begunkovb18fdf72019-11-21 23:21:02 +03001353 *nxtptr = nxt;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001354 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06001355 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001356
Jens Axboe4d7dd462019-11-20 13:03:52 -07001357 req->flags |= REQ_F_LINK_NEXT;
Jens Axboe2665abf2019-11-05 12:40:47 -07001358 if (wake_ev)
1359 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001360}
1361
1362/*
1363 * Called if REQ_F_LINK is set, and we fail the head request
1364 */
1365static void io_fail_links(struct io_kiocb *req)
1366{
Jens Axboe2665abf2019-11-05 12:40:47 -07001367 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001368 unsigned long flags;
1369
1370 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06001371
1372 while (!list_empty(&req->link_list)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03001373 struct io_kiocb *link = list_first_entry(&req->link_list,
1374 struct io_kiocb, link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06001375
Pavel Begunkov44932332019-12-05 16:16:35 +03001376 list_del_init(&link->link_list);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001377 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001378
1379 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
Jens Axboed625c6e2019-12-17 19:53:05 -07001380 link->opcode == IORING_OP_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -07001381 io_link_cancel_timeout(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001382 } else {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001383 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe978db572019-11-14 22:39:04 -07001384 __io_double_put_req(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001385 }
Jens Axboe5d960722019-11-19 15:31:28 -07001386 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -06001387 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001388
1389 io_commit_cqring(ctx);
1390 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1391 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001392}
1393
Jens Axboe4d7dd462019-11-20 13:03:52 -07001394static void io_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -06001395{
Jens Axboe4d7dd462019-11-20 13:03:52 -07001396 if (likely(!(req->flags & REQ_F_LINK)))
Jens Axboe2665abf2019-11-05 12:40:47 -07001397 return;
Jens Axboe2665abf2019-11-05 12:40:47 -07001398
Jens Axboe9e645e112019-05-10 16:07:28 -06001399 /*
1400 * If LINK is set, we have dependent requests in this chain. If we
1401 * didn't fail this request, queue the first one up, moving any other
1402 * dependencies to the next request. In case of failure, fail the rest
1403 * of the chain.
1404 */
Jens Axboe2665abf2019-11-05 12:40:47 -07001405 if (req->flags & REQ_F_FAIL_LINK) {
1406 io_fail_links(req);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001407 } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) ==
1408 REQ_F_LINK_TIMEOUT) {
Jens Axboe2665abf2019-11-05 12:40:47 -07001409 struct io_ring_ctx *ctx = req->ctx;
1410 unsigned long flags;
1411
1412 /*
1413 * If this is a timeout link, we could be racing with the
1414 * timeout timer. Grab the completion lock for this case to
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001415 * protect against that.
Jens Axboe2665abf2019-11-05 12:40:47 -07001416 */
1417 spin_lock_irqsave(&ctx->completion_lock, flags);
1418 io_req_link_next(req, nxt);
1419 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1420 } else {
1421 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -06001422 }
Jens Axboe4d7dd462019-11-20 13:03:52 -07001423}
Jens Axboe9e645e112019-05-10 16:07:28 -06001424
Jackie Liuc69f8db2019-11-09 11:00:08 +08001425static void io_free_req(struct io_kiocb *req)
1426{
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001427 struct io_kiocb *nxt = NULL;
1428
1429 io_req_find_next(req, &nxt);
Pavel Begunkov70cf9f32019-11-21 23:21:00 +03001430 __io_free_req(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001431
1432 if (nxt)
1433 io_queue_async_work(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08001434}
1435
Jens Axboeba816ad2019-09-28 11:36:45 -06001436/*
1437 * Drop reference to request, return next in chain (if there is one) if this
1438 * was the last reference to this request.
1439 */
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001440__attribute__((nonnull))
Jackie Liuec9c02a2019-11-08 23:50:36 +08001441static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboee65ef562019-03-12 10:16:44 -06001442{
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001443 io_req_find_next(req, nxtptr);
Jens Axboe4d7dd462019-11-20 13:03:52 -07001444
Jens Axboee65ef562019-03-12 10:16:44 -06001445 if (refcount_dec_and_test(&req->refs))
Jens Axboe4d7dd462019-11-20 13:03:52 -07001446 __io_free_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001447}
1448
Jens Axboe2b188cc2019-01-07 10:46:33 -07001449static void io_put_req(struct io_kiocb *req)
1450{
Jens Axboedef596e2019-01-09 08:59:42 -07001451 if (refcount_dec_and_test(&req->refs))
1452 io_free_req(req);
1453}
1454
Jens Axboe978db572019-11-14 22:39:04 -07001455/*
1456 * Must only be used if we don't need to care about links, usually from
1457 * within the completion handling itself.
1458 */
1459static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06001460{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001461 /* drop both submit and complete references */
1462 if (refcount_sub_and_test(2, &req->refs))
1463 __io_free_req(req);
1464}
1465
Jens Axboe978db572019-11-14 22:39:04 -07001466static void io_double_put_req(struct io_kiocb *req)
1467{
1468 /* drop both submit and complete references */
1469 if (refcount_sub_and_test(2, &req->refs))
1470 io_free_req(req);
1471}
1472
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001473static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06001474{
Jens Axboe84f97dc2019-11-06 11:27:53 -07001475 struct io_rings *rings = ctx->rings;
1476
Jens Axboead3eb2c2019-12-18 17:12:20 -07001477 if (test_bit(0, &ctx->cq_check_overflow)) {
1478 /*
1479 * noflush == true is from the waitqueue handler, just ensure
1480 * we wake up the task, and the next invocation will flush the
1481 * entries. We cannot safely to it from here.
1482 */
1483 if (noflush && !list_empty(&ctx->cq_overflow_list))
1484 return -1U;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001485
Jens Axboead3eb2c2019-12-18 17:12:20 -07001486 io_cqring_overflow_flush(ctx, false);
1487 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001488
Jens Axboea3a0e432019-08-20 11:03:11 -06001489 /* See comment at the top of this file */
1490 smp_rmb();
Jens Axboead3eb2c2019-12-18 17:12:20 -07001491 return ctx->cached_cq_tail - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06001492}
1493
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03001494static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1495{
1496 struct io_rings *rings = ctx->rings;
1497
1498 /* make sure SQ entry isn't read before tail */
1499 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1500}
1501
Jens Axboe8237e042019-12-28 10:48:22 -07001502static inline bool io_req_multi_free(struct req_batch *rb, struct io_kiocb *req)
Jens Axboee94f1412019-12-19 12:06:02 -07001503{
Jens Axboec6ca97b302019-12-28 12:11:08 -07001504 if ((req->flags & REQ_F_LINK) || io_is_fallback_req(req))
1505 return false;
Jens Axboee94f1412019-12-19 12:06:02 -07001506
Jens Axboec6ca97b302019-12-28 12:11:08 -07001507 if (!(req->flags & REQ_F_FIXED_FILE) || req->io)
1508 rb->need_iter++;
1509
1510 rb->reqs[rb->to_free++] = req;
1511 if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
1512 io_free_req_many(req->ctx, rb);
1513 return true;
Jens Axboee94f1412019-12-19 12:06:02 -07001514}
1515
Jens Axboedef596e2019-01-09 08:59:42 -07001516/*
1517 * Find and free completed poll iocbs
1518 */
1519static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
1520 struct list_head *done)
1521{
Jens Axboe8237e042019-12-28 10:48:22 -07001522 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07001523 struct io_kiocb *req;
Jens Axboedef596e2019-01-09 08:59:42 -07001524
Jens Axboec6ca97b302019-12-28 12:11:08 -07001525 rb.to_free = rb.need_iter = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001526 while (!list_empty(done)) {
1527 req = list_first_entry(done, struct io_kiocb, list);
1528 list_del(&req->list);
1529
Jens Axboe78e19bb2019-11-06 15:21:34 -07001530 io_cqring_fill_event(req, req->result);
Jens Axboedef596e2019-01-09 08:59:42 -07001531 (*nr_events)++;
1532
Jens Axboe8237e042019-12-28 10:48:22 -07001533 if (refcount_dec_and_test(&req->refs) &&
1534 !io_req_multi_free(&rb, req))
1535 io_free_req(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001536 }
Jens Axboedef596e2019-01-09 08:59:42 -07001537
Jens Axboe09bb8392019-03-13 12:39:28 -06001538 io_commit_cqring(ctx);
Jens Axboe8237e042019-12-28 10:48:22 -07001539 io_free_req_many(ctx, &rb);
Jens Axboedef596e2019-01-09 08:59:42 -07001540}
1541
1542static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1543 long min)
1544{
1545 struct io_kiocb *req, *tmp;
1546 LIST_HEAD(done);
1547 bool spin;
1548 int ret;
1549
1550 /*
1551 * Only spin for completions if we don't have multiple devices hanging
1552 * off our complete list, and we're under the requested amount.
1553 */
1554 spin = !ctx->poll_multi_file && *nr_events < min;
1555
1556 ret = 0;
1557 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
Jens Axboe9adbd452019-12-20 08:45:55 -07001558 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07001559
1560 /*
1561 * Move completed entries to our local list. If we find a
1562 * request that requires polling, break out and complete
1563 * the done list first, if we have entries there.
1564 */
1565 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
1566 list_move_tail(&req->list, &done);
1567 continue;
1568 }
1569 if (!list_empty(&done))
1570 break;
1571
1572 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1573 if (ret < 0)
1574 break;
1575
1576 if (ret && spin)
1577 spin = false;
1578 ret = 0;
1579 }
1580
1581 if (!list_empty(&done))
1582 io_iopoll_complete(ctx, nr_events, &done);
1583
1584 return ret;
1585}
1586
1587/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08001588 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07001589 * non-spinning poll check - we'll still enter the driver poll loop, but only
1590 * as a non-spinning completion check.
1591 */
1592static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
1593 long min)
1594{
Jens Axboe08f54392019-08-21 22:19:11 -06001595 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07001596 int ret;
1597
1598 ret = io_do_iopoll(ctx, nr_events, min);
1599 if (ret < 0)
1600 return ret;
1601 if (!min || *nr_events >= min)
1602 return 0;
1603 }
1604
1605 return 1;
1606}
1607
1608/*
1609 * We can't just wait for polled events to come to us, we have to actively
1610 * find and complete them.
1611 */
1612static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
1613{
1614 if (!(ctx->flags & IORING_SETUP_IOPOLL))
1615 return;
1616
1617 mutex_lock(&ctx->uring_lock);
1618 while (!list_empty(&ctx->poll_list)) {
1619 unsigned int nr_events = 0;
1620
1621 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -06001622
1623 /*
1624 * Ensure we allow local-to-the-cpu processing to take place,
1625 * in this case we need to ensure that we reap all events.
1626 */
1627 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -07001628 }
1629 mutex_unlock(&ctx->uring_lock);
1630}
1631
Jens Axboe2b2ed972019-10-25 10:06:15 -06001632static int __io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1633 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07001634{
Jens Axboe2b2ed972019-10-25 10:06:15 -06001635 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001636
1637 do {
1638 int tmin = 0;
1639
Jens Axboe500f9fb2019-08-19 12:15:59 -06001640 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06001641 * Don't enter poll loop if we already have events pending.
1642 * If we do, we can potentially be spinning for commands that
1643 * already triggered a CQE (eg in error).
1644 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001645 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06001646 break;
1647
1648 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06001649 * If a submit got punted to a workqueue, we can have the
1650 * application entering polling for a command before it gets
1651 * issued. That app will hold the uring_lock for the duration
1652 * of the poll right here, so we need to take a breather every
1653 * now and then to ensure that the issue has a chance to add
1654 * the poll to the issued list. Otherwise we can spin here
1655 * forever, while the workqueue is stuck trying to acquire the
1656 * very same mutex.
1657 */
1658 if (!(++iters & 7)) {
1659 mutex_unlock(&ctx->uring_lock);
1660 mutex_lock(&ctx->uring_lock);
1661 }
1662
Jens Axboedef596e2019-01-09 08:59:42 -07001663 if (*nr_events < min)
1664 tmin = min - *nr_events;
1665
1666 ret = io_iopoll_getevents(ctx, nr_events, tmin);
1667 if (ret <= 0)
1668 break;
1669 ret = 0;
1670 } while (min && !*nr_events && !need_resched());
1671
Jens Axboe2b2ed972019-10-25 10:06:15 -06001672 return ret;
1673}
1674
1675static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1676 long min)
1677{
1678 int ret;
1679
1680 /*
1681 * We disallow the app entering submit/complete with polling, but we
1682 * still need to lock the ring to prevent racing with polled issue
1683 * that got punted to a workqueue.
1684 */
1685 mutex_lock(&ctx->uring_lock);
1686 ret = __io_iopoll_check(ctx, nr_events, min);
Jens Axboe500f9fb2019-08-19 12:15:59 -06001687 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001688 return ret;
1689}
1690
Jens Axboe491381ce2019-10-17 09:20:46 -06001691static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001692{
Jens Axboe491381ce2019-10-17 09:20:46 -06001693 /*
1694 * Tell lockdep we inherited freeze protection from submission
1695 * thread.
1696 */
1697 if (req->flags & REQ_F_ISREG) {
1698 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001699
Jens Axboe491381ce2019-10-17 09:20:46 -06001700 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001701 }
Jens Axboe491381ce2019-10-17 09:20:46 -06001702 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001703}
1704
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001705static inline void req_set_fail_links(struct io_kiocb *req)
1706{
1707 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1708 req->flags |= REQ_F_FAIL_LINK;
1709}
1710
Jens Axboeba816ad2019-09-28 11:36:45 -06001711static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001712{
Jens Axboe9adbd452019-12-20 08:45:55 -07001713 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001714
Jens Axboe491381ce2019-10-17 09:20:46 -06001715 if (kiocb->ki_flags & IOCB_WRITE)
1716 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001717
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001718 if (res != req->result)
1719 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001720 io_cqring_add_event(req, res);
Jens Axboeba816ad2019-09-28 11:36:45 -06001721}
1722
1723static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1724{
Jens Axboe9adbd452019-12-20 08:45:55 -07001725 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06001726
1727 io_complete_rw_common(kiocb, res);
Jens Axboee65ef562019-03-12 10:16:44 -06001728 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001729}
1730
Jens Axboeba816ad2019-09-28 11:36:45 -06001731static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res)
1732{
Jens Axboe9adbd452019-12-20 08:45:55 -07001733 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001734 struct io_kiocb *nxt = NULL;
Jens Axboeba816ad2019-09-28 11:36:45 -06001735
1736 io_complete_rw_common(kiocb, res);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001737 io_put_req_find_next(req, &nxt);
1738
1739 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001740}
1741
Jens Axboedef596e2019-01-09 08:59:42 -07001742static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1743{
Jens Axboe9adbd452019-12-20 08:45:55 -07001744 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07001745
Jens Axboe491381ce2019-10-17 09:20:46 -06001746 if (kiocb->ki_flags & IOCB_WRITE)
1747 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001748
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001749 if (res != req->result)
1750 req_set_fail_links(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06001751 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -07001752 if (res != -EAGAIN)
1753 req->flags |= REQ_F_IOPOLL_COMPLETED;
1754}
1755
1756/*
1757 * After the iocb has been issued, it's safe to be found on the poll list.
1758 * Adding the kiocb to the list AFTER submission ensures that we don't
1759 * find it from a io_iopoll_getevents() thread before the issuer is done
1760 * accessing the kiocb cookie.
1761 */
1762static void io_iopoll_req_issued(struct io_kiocb *req)
1763{
1764 struct io_ring_ctx *ctx = req->ctx;
1765
1766 /*
1767 * Track whether we have multiple files in our lists. This will impact
1768 * how we do polling eventually, not spinning if we're on potentially
1769 * different devices.
1770 */
1771 if (list_empty(&ctx->poll_list)) {
1772 ctx->poll_multi_file = false;
1773 } else if (!ctx->poll_multi_file) {
1774 struct io_kiocb *list_req;
1775
1776 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1777 list);
Jens Axboe9adbd452019-12-20 08:45:55 -07001778 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07001779 ctx->poll_multi_file = true;
1780 }
1781
1782 /*
1783 * For fast devices, IO may have already completed. If it has, add
1784 * it to the front so we find it first.
1785 */
1786 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1787 list_add(&req->list, &ctx->poll_list);
1788 else
1789 list_add_tail(&req->list, &ctx->poll_list);
1790}
1791
Jens Axboe3d6770f2019-04-13 11:50:54 -06001792static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07001793{
Jens Axboe3d6770f2019-04-13 11:50:54 -06001794 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -07001795 int diff = state->has_refs - state->used_refs;
1796
1797 if (diff)
1798 fput_many(state->file, diff);
1799 state->file = NULL;
1800 }
1801}
1802
1803/*
1804 * Get as many references to a file as we have IOs left in this submission,
1805 * assuming most submissions are for one file, or at least that each file
1806 * has more than one submission.
1807 */
1808static struct file *io_file_get(struct io_submit_state *state, int fd)
1809{
1810 if (!state)
1811 return fget(fd);
1812
1813 if (state->file) {
1814 if (state->fd == fd) {
1815 state->used_refs++;
1816 state->ios_left--;
1817 return state->file;
1818 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06001819 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07001820 }
1821 state->file = fget_many(fd, state->ios_left);
1822 if (!state->file)
1823 return NULL;
1824
1825 state->fd = fd;
1826 state->has_refs = state->ios_left;
1827 state->used_refs = 1;
1828 state->ios_left--;
1829 return state->file;
1830}
1831
Jens Axboe2b188cc2019-01-07 10:46:33 -07001832/*
1833 * If we tracked the file through the SCM inflight mechanism, we could support
1834 * any file. For now, just ensure that anything potentially problematic is done
1835 * inline.
1836 */
1837static bool io_file_supports_async(struct file *file)
1838{
1839 umode_t mode = file_inode(file)->i_mode;
1840
Jens Axboe10d59342019-12-09 20:16:22 -07001841 if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07001842 return true;
1843 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
1844 return true;
1845
1846 return false;
1847}
1848
Jens Axboe3529d8c2019-12-19 18:24:38 -07001849static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1850 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001851{
Jens Axboedef596e2019-01-09 08:59:42 -07001852 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07001853 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06001854 unsigned ioprio;
1855 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001856
Jens Axboe491381ce2019-10-17 09:20:46 -06001857 if (S_ISREG(file_inode(req->file)->i_mode))
1858 req->flags |= REQ_F_ISREG;
1859
Jens Axboe2b188cc2019-01-07 10:46:33 -07001860 kiocb->ki_pos = READ_ONCE(sqe->off);
Jens Axboeba042912019-12-25 16:33:42 -07001861 if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) {
1862 req->flags |= REQ_F_CUR_POS;
1863 kiocb->ki_pos = req->file->f_pos;
1864 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001865 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03001866 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
1867 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
1868 if (unlikely(ret))
1869 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001870
1871 ioprio = READ_ONCE(sqe->ioprio);
1872 if (ioprio) {
1873 ret = ioprio_check_cap(ioprio);
1874 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06001875 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001876
1877 kiocb->ki_ioprio = ioprio;
1878 } else
1879 kiocb->ki_ioprio = get_current_ioprio();
1880
Stefan Bühler8449eed2019-04-27 20:34:19 +02001881 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06001882 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
1883 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02001884 req->flags |= REQ_F_NOWAIT;
1885
1886 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001887 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001888
Jens Axboedef596e2019-01-09 08:59:42 -07001889 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07001890 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
1891 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06001892 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001893
Jens Axboedef596e2019-01-09 08:59:42 -07001894 kiocb->ki_flags |= IOCB_HIPRI;
1895 kiocb->ki_complete = io_complete_rw_iopoll;
Jens Axboe6873e0b2019-10-30 13:53:09 -06001896 req->result = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001897 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001898 if (kiocb->ki_flags & IOCB_HIPRI)
1899 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07001900 kiocb->ki_complete = io_complete_rw;
1901 }
Jens Axboe9adbd452019-12-20 08:45:55 -07001902
Jens Axboe3529d8c2019-12-19 18:24:38 -07001903 req->rw.addr = READ_ONCE(sqe->addr);
1904 req->rw.len = READ_ONCE(sqe->len);
Jens Axboe9adbd452019-12-20 08:45:55 -07001905 /* we own ->private, reuse it for the buffer index */
1906 req->rw.kiocb.private = (void *) (unsigned long)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001907 READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001908 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001909}
1910
1911static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
1912{
1913 switch (ret) {
1914 case -EIOCBQUEUED:
1915 break;
1916 case -ERESTARTSYS:
1917 case -ERESTARTNOINTR:
1918 case -ERESTARTNOHAND:
1919 case -ERESTART_RESTARTBLOCK:
1920 /*
1921 * We can't just restart the syscall, since previously
1922 * submitted sqes may already be in progress. Just fail this
1923 * IO with EINTR.
1924 */
1925 ret = -EINTR;
1926 /* fall through */
1927 default:
1928 kiocb->ki_complete(kiocb, ret, 0);
1929 }
1930}
1931
Jens Axboeba816ad2019-09-28 11:36:45 -06001932static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt,
1933 bool in_async)
1934{
Jens Axboeba042912019-12-25 16:33:42 -07001935 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
1936
1937 if (req->flags & REQ_F_CUR_POS)
1938 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001939 if (in_async && ret >= 0 && kiocb->ki_complete == io_complete_rw)
Jens Axboeba816ad2019-09-28 11:36:45 -06001940 *nxt = __io_complete_rw(kiocb, ret);
1941 else
1942 io_rw_done(kiocb, ret);
1943}
1944
Jens Axboe9adbd452019-12-20 08:45:55 -07001945static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03001946 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07001947{
Jens Axboe9adbd452019-12-20 08:45:55 -07001948 struct io_ring_ctx *ctx = req->ctx;
1949 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001950 struct io_mapped_ubuf *imu;
1951 unsigned index, buf_index;
1952 size_t offset;
1953 u64 buf_addr;
1954
1955 /* attempt to use fixed buffers without having provided iovecs */
1956 if (unlikely(!ctx->user_bufs))
1957 return -EFAULT;
1958
Jens Axboe9adbd452019-12-20 08:45:55 -07001959 buf_index = (unsigned long) req->rw.kiocb.private;
Jens Axboeedafcce2019-01-09 09:16:05 -07001960 if (unlikely(buf_index >= ctx->nr_user_bufs))
1961 return -EFAULT;
1962
1963 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
1964 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07001965 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07001966
1967 /* overflow */
1968 if (buf_addr + len < buf_addr)
1969 return -EFAULT;
1970 /* not inside the mapped region */
1971 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
1972 return -EFAULT;
1973
1974 /*
1975 * May not be a start of buffer, set size appropriately
1976 * and advance us to the beginning.
1977 */
1978 offset = buf_addr - imu->ubuf;
1979 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06001980
1981 if (offset) {
1982 /*
1983 * Don't use iov_iter_advance() here, as it's really slow for
1984 * using the latter parts of a big fixed buffer - it iterates
1985 * over each segment manually. We can cheat a bit here, because
1986 * we know that:
1987 *
1988 * 1) it's a BVEC iter, we set it up
1989 * 2) all bvecs are PAGE_SIZE in size, except potentially the
1990 * first and last bvec
1991 *
1992 * So just find our index, and adjust the iterator afterwards.
1993 * If the offset is within the first bvec (or the whole first
1994 * bvec, just use iov_iter_advance(). This makes it easier
1995 * since we can just skip the first segment, which may not
1996 * be PAGE_SIZE aligned.
1997 */
1998 const struct bio_vec *bvec = imu->bvec;
1999
2000 if (offset <= bvec->bv_len) {
2001 iov_iter_advance(iter, offset);
2002 } else {
2003 unsigned long seg_skip;
2004
2005 /* skip first vec */
2006 offset -= bvec->bv_len;
2007 seg_skip = 1 + (offset >> PAGE_SHIFT);
2008
2009 iter->bvec = bvec + seg_skip;
2010 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02002011 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002012 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002013 }
2014 }
2015
Jens Axboe5e559562019-11-13 16:12:46 -07002016 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002017}
2018
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002019static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
2020 struct iovec **iovec, struct iov_iter *iter)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002021{
Jens Axboe9adbd452019-12-20 08:45:55 -07002022 void __user *buf = u64_to_user_ptr(req->rw.addr);
2023 size_t sqe_len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002024 u8 opcode;
2025
Jens Axboed625c6e2019-12-17 19:53:05 -07002026 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03002027 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07002028 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07002029 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07002030 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002031
Jens Axboe9adbd452019-12-20 08:45:55 -07002032 /* buffer index only valid with fixed read/write */
2033 if (req->rw.kiocb.private)
2034 return -EINVAL;
2035
Jens Axboe3a6820f2019-12-22 15:19:35 -07002036 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
2037 ssize_t ret;
2038 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
2039 *iovec = NULL;
2040 return ret;
2041 }
2042
Jens Axboef67676d2019-12-02 11:03:47 -07002043 if (req->io) {
2044 struct io_async_rw *iorw = &req->io->rw;
2045
2046 *iovec = iorw->iov;
2047 iov_iter_init(iter, rw, *iovec, iorw->nr_segs, iorw->size);
2048 if (iorw->iov == iorw->fast_iov)
2049 *iovec = NULL;
2050 return iorw->size;
2051 }
2052
Jens Axboe2b188cc2019-01-07 10:46:33 -07002053#ifdef CONFIG_COMPAT
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002054 if (req->ctx->compat)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002055 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
2056 iovec, iter);
2057#endif
2058
2059 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
2060}
2061
Jens Axboe32960612019-09-23 11:05:34 -06002062/*
2063 * For files that don't have ->read_iter() and ->write_iter(), handle them
2064 * by looping over ->read() or ->write() manually.
2065 */
2066static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
2067 struct iov_iter *iter)
2068{
2069 ssize_t ret = 0;
2070
2071 /*
2072 * Don't support polled IO through this interface, and we can't
2073 * support non-blocking either. For the latter, this just causes
2074 * the kiocb to be handled from an async context.
2075 */
2076 if (kiocb->ki_flags & IOCB_HIPRI)
2077 return -EOPNOTSUPP;
2078 if (kiocb->ki_flags & IOCB_NOWAIT)
2079 return -EAGAIN;
2080
2081 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002082 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06002083 ssize_t nr;
2084
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002085 if (!iov_iter_is_bvec(iter)) {
2086 iovec = iov_iter_iovec(iter);
2087 } else {
2088 /* fixed buffers import bvec */
2089 iovec.iov_base = kmap(iter->bvec->bv_page)
2090 + iter->iov_offset;
2091 iovec.iov_len = min(iter->count,
2092 iter->bvec->bv_len - iter->iov_offset);
2093 }
2094
Jens Axboe32960612019-09-23 11:05:34 -06002095 if (rw == READ) {
2096 nr = file->f_op->read(file, iovec.iov_base,
2097 iovec.iov_len, &kiocb->ki_pos);
2098 } else {
2099 nr = file->f_op->write(file, iovec.iov_base,
2100 iovec.iov_len, &kiocb->ki_pos);
2101 }
2102
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002103 if (iov_iter_is_bvec(iter))
2104 kunmap(iter->bvec->bv_page);
2105
Jens Axboe32960612019-09-23 11:05:34 -06002106 if (nr < 0) {
2107 if (!ret)
2108 ret = nr;
2109 break;
2110 }
2111 ret += nr;
2112 if (nr != iovec.iov_len)
2113 break;
2114 iov_iter_advance(iter, nr);
2115 }
2116
2117 return ret;
2118}
2119
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002120static void io_req_map_rw(struct io_kiocb *req, ssize_t io_size,
Jens Axboef67676d2019-12-02 11:03:47 -07002121 struct iovec *iovec, struct iovec *fast_iov,
2122 struct iov_iter *iter)
2123{
2124 req->io->rw.nr_segs = iter->nr_segs;
2125 req->io->rw.size = io_size;
2126 req->io->rw.iov = iovec;
2127 if (!req->io->rw.iov) {
2128 req->io->rw.iov = req->io->rw.fast_iov;
2129 memcpy(req->io->rw.iov, fast_iov,
2130 sizeof(struct iovec) * iter->nr_segs);
2131 }
2132}
2133
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002134static int io_alloc_async_ctx(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07002135{
Jens Axboed3656342019-12-18 09:50:26 -07002136 if (!io_op_defs[req->opcode].async_ctx)
2137 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002138 req->io = kmalloc(sizeof(*req->io), GFP_KERNEL);
Jens Axboe06b76d42019-12-19 14:44:26 -07002139 return req->io == NULL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002140}
2141
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002142static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size,
2143 struct iovec *iovec, struct iovec *fast_iov,
2144 struct iov_iter *iter)
2145{
Jens Axboe980ad262020-01-24 23:08:54 -07002146 if (!io_op_defs[req->opcode].async_ctx)
Jens Axboe74566df2020-01-13 19:23:24 -07002147 return 0;
Jens Axboe5d204bc2020-01-31 12:06:52 -07002148 if (!req->io) {
2149 if (io_alloc_async_ctx(req))
2150 return -ENOMEM;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002151
Jens Axboe5d204bc2020-01-31 12:06:52 -07002152 io_req_map_rw(req, io_size, iovec, fast_iov, iter);
2153 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002154 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002155}
2156
Jens Axboe3529d8c2019-12-19 18:24:38 -07002157static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2158 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002159{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002160 struct io_async_ctx *io;
2161 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002162 ssize_t ret;
2163
Jens Axboe3529d8c2019-12-19 18:24:38 -07002164 ret = io_prep_rw(req, sqe, force_nonblock);
2165 if (ret)
2166 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002167
Jens Axboe3529d8c2019-12-19 18:24:38 -07002168 if (unlikely(!(req->file->f_mode & FMODE_READ)))
2169 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002170
Jens Axboe3529d8c2019-12-19 18:24:38 -07002171 if (!req->io)
2172 return 0;
2173
2174 io = req->io;
2175 io->rw.iov = io->rw.fast_iov;
2176 req->io = NULL;
2177 ret = io_import_iovec(READ, req, &io->rw.iov, &iter);
2178 req->io = io;
2179 if (ret < 0)
2180 return ret;
2181
2182 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2183 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002184}
2185
Pavel Begunkov267bc902019-11-07 01:41:08 +03002186static int io_read(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06002187 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002188{
2189 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002190 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002191 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002192 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002193 ssize_t io_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002194
Jens Axboe3529d8c2019-12-19 18:24:38 -07002195 ret = io_import_iovec(READ, req, &iovec, &iter);
Jens Axboe06b76d42019-12-19 14:44:26 -07002196 if (ret < 0)
2197 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002198
Jens Axboefd6c2e42019-12-18 12:19:41 -07002199 /* Ensure we clear previously set non-block flag */
2200 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07002201 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002202
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002203 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002204 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06002205 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07002206 req->result = io_size;
2207
2208 /*
2209 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2210 * we know to async punt it even if it was opened O_NONBLOCK
2211 */
Jens Axboe9adbd452019-12-20 08:45:55 -07002212 if (force_nonblock && !io_file_supports_async(req->file)) {
Jens Axboef67676d2019-12-02 11:03:47 -07002213 req->flags |= REQ_F_MUST_PUNT;
2214 goto copy_iov;
2215 }
Jens Axboe9e645e112019-05-10 16:07:28 -06002216
Jens Axboe31b51512019-01-18 22:56:34 -07002217 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002218 ret = rw_verify_area(READ, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002219 if (!ret) {
2220 ssize_t ret2;
2221
Jens Axboe9adbd452019-12-20 08:45:55 -07002222 if (req->file->f_op->read_iter)
2223 ret2 = call_read_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002224 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002225 ret2 = loop_rw_iter(READ, req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002226
Jens Axboe9d93a3f2019-05-15 13:53:07 -06002227 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboef67676d2019-12-02 11:03:47 -07002228 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002229 kiocb_done(kiocb, ret2, nxt, req->in_async);
Jens Axboef67676d2019-12-02 11:03:47 -07002230 } else {
2231copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002232 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002233 inline_vecs, &iter);
2234 if (ret)
2235 goto out_free;
2236 return -EAGAIN;
2237 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002238 }
Jens Axboef67676d2019-12-02 11:03:47 -07002239out_free:
Pavel Begunkov1e950812020-02-06 19:51:16 +03002240 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002241 return ret;
2242}
2243
Jens Axboe3529d8c2019-12-19 18:24:38 -07002244static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2245 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002246{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002247 struct io_async_ctx *io;
2248 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002249 ssize_t ret;
2250
Jens Axboe3529d8c2019-12-19 18:24:38 -07002251 ret = io_prep_rw(req, sqe, force_nonblock);
2252 if (ret)
2253 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002254
Jens Axboe3529d8c2019-12-19 18:24:38 -07002255 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
2256 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002257
Jens Axboe3529d8c2019-12-19 18:24:38 -07002258 if (!req->io)
2259 return 0;
2260
2261 io = req->io;
2262 io->rw.iov = io->rw.fast_iov;
2263 req->io = NULL;
2264 ret = io_import_iovec(WRITE, req, &io->rw.iov, &iter);
2265 req->io = io;
2266 if (ret < 0)
2267 return ret;
2268
2269 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2270 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002271}
2272
Pavel Begunkov267bc902019-11-07 01:41:08 +03002273static int io_write(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06002274 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002275{
2276 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002277 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002278 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002279 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002280 ssize_t ret, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002281
Jens Axboe3529d8c2019-12-19 18:24:38 -07002282 ret = io_import_iovec(WRITE, req, &iovec, &iter);
Jens Axboe06b76d42019-12-19 14:44:26 -07002283 if (ret < 0)
2284 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002285
Jens Axboefd6c2e42019-12-18 12:19:41 -07002286 /* Ensure we clear previously set non-block flag */
2287 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07002288 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002289
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002290 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002291 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06002292 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07002293 req->result = io_size;
2294
2295 /*
2296 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2297 * we know to async punt it even if it was opened O_NONBLOCK
2298 */
2299 if (force_nonblock && !io_file_supports_async(req->file)) {
2300 req->flags |= REQ_F_MUST_PUNT;
2301 goto copy_iov;
2302 }
2303
Jens Axboe10d59342019-12-09 20:16:22 -07002304 /* file path doesn't support NOWAIT for non-direct_IO */
2305 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
2306 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07002307 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06002308
Jens Axboe31b51512019-01-18 22:56:34 -07002309 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002310 ret = rw_verify_area(WRITE, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002311 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01002312 ssize_t ret2;
2313
Jens Axboe2b188cc2019-01-07 10:46:33 -07002314 /*
2315 * Open-code file_start_write here to grab freeze protection,
2316 * which will be released by another thread in
2317 * io_complete_rw(). Fool lockdep by telling it the lock got
2318 * released so that it doesn't complain about the held lock when
2319 * we return to userspace.
2320 */
Jens Axboe491381ce2019-10-17 09:20:46 -06002321 if (req->flags & REQ_F_ISREG) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002322 __sb_start_write(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002323 SB_FREEZE_WRITE, true);
Jens Axboe9adbd452019-12-20 08:45:55 -07002324 __sb_writers_release(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002325 SB_FREEZE_WRITE);
2326 }
2327 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01002328
Jens Axboe9adbd452019-12-20 08:45:55 -07002329 if (req->file->f_op->write_iter)
2330 ret2 = call_write_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002331 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002332 ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter);
Jens Axboef67676d2019-12-02 11:03:47 -07002333 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002334 kiocb_done(kiocb, ret2, nxt, req->in_async);
Jens Axboef67676d2019-12-02 11:03:47 -07002335 } else {
2336copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002337 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002338 inline_vecs, &iter);
2339 if (ret)
2340 goto out_free;
2341 return -EAGAIN;
2342 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002343 }
Jens Axboe31b51512019-01-18 22:56:34 -07002344out_free:
Pavel Begunkov1e950812020-02-06 19:51:16 +03002345 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002346 return ret;
2347}
2348
2349/*
2350 * IORING_OP_NOP just posts a completion event, nothing else.
2351 */
Jens Axboe78e19bb2019-11-06 15:21:34 -07002352static int io_nop(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002353{
2354 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002355
Jens Axboedef596e2019-01-09 08:59:42 -07002356 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2357 return -EINVAL;
2358
Jens Axboe78e19bb2019-11-06 15:21:34 -07002359 io_cqring_add_event(req, 0);
Jens Axboee65ef562019-03-12 10:16:44 -06002360 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002361 return 0;
2362}
2363
Jens Axboe3529d8c2019-12-19 18:24:38 -07002364static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002365{
Jens Axboe6b063142019-01-10 22:13:58 -07002366 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002367
Jens Axboe09bb8392019-03-13 12:39:28 -06002368 if (!req->file)
2369 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002370
Jens Axboe6b063142019-01-10 22:13:58 -07002371 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07002372 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07002373 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002374 return -EINVAL;
2375
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002376 req->sync.flags = READ_ONCE(sqe->fsync_flags);
2377 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
2378 return -EINVAL;
2379
2380 req->sync.off = READ_ONCE(sqe->off);
2381 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002382 return 0;
2383}
2384
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002385static bool io_req_cancelled(struct io_kiocb *req)
2386{
2387 if (req->work.flags & IO_WQ_WORK_CANCEL) {
2388 req_set_fail_links(req);
2389 io_cqring_add_event(req, -ECANCELED);
2390 io_put_req(req);
2391 return true;
2392 }
2393
2394 return false;
2395}
2396
Jens Axboe78912932020-01-14 22:09:06 -07002397static void io_link_work_cb(struct io_wq_work **workptr)
2398{
2399 struct io_wq_work *work = *workptr;
2400 struct io_kiocb *link = work->data;
2401
2402 io_queue_linked_timeout(link);
2403 work->func = io_wq_submit_work;
2404}
2405
2406static void io_wq_assign_next(struct io_wq_work **workptr, struct io_kiocb *nxt)
2407{
2408 struct io_kiocb *link;
2409
2410 io_prep_async_work(nxt, &link);
2411 *workptr = &nxt->work;
2412 if (link) {
2413 nxt->work.flags |= IO_WQ_WORK_CB;
2414 nxt->work.func = io_link_work_cb;
2415 nxt->work.data = link;
2416 }
2417}
2418
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002419static void io_fsync_finish(struct io_wq_work **workptr)
2420{
2421 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2422 loff_t end = req->sync.off + req->sync.len;
2423 struct io_kiocb *nxt = NULL;
2424 int ret;
2425
2426 if (io_req_cancelled(req))
2427 return;
2428
Jens Axboe9adbd452019-12-20 08:45:55 -07002429 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002430 end > 0 ? end : LLONG_MAX,
2431 req->sync.flags & IORING_FSYNC_DATASYNC);
2432 if (ret < 0)
2433 req_set_fail_links(req);
2434 io_cqring_add_event(req, ret);
2435 io_put_req_find_next(req, &nxt);
2436 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07002437 io_wq_assign_next(workptr, nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002438}
2439
Jens Axboefc4df992019-12-10 14:38:45 -07002440static int io_fsync(struct io_kiocb *req, struct io_kiocb **nxt,
2441 bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002442{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002443 struct io_wq_work *work, *old_work;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002444
2445 /* fsync always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002446 if (force_nonblock) {
2447 io_put_req(req);
2448 req->work.func = io_fsync_finish;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002449 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002450 }
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002451
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002452 work = old_work = &req->work;
2453 io_fsync_finish(&work);
2454 if (work && work != old_work)
2455 *nxt = container_of(work, struct io_kiocb, work);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002456 return 0;
2457}
2458
Jens Axboed63d1b52019-12-10 10:38:56 -07002459static void io_fallocate_finish(struct io_wq_work **workptr)
2460{
2461 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2462 struct io_kiocb *nxt = NULL;
2463 int ret;
2464
2465 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
2466 req->sync.len);
2467 if (ret < 0)
2468 req_set_fail_links(req);
2469 io_cqring_add_event(req, ret);
2470 io_put_req_find_next(req, &nxt);
2471 if (nxt)
2472 io_wq_assign_next(workptr, nxt);
2473}
2474
2475static int io_fallocate_prep(struct io_kiocb *req,
2476 const struct io_uring_sqe *sqe)
2477{
2478 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
2479 return -EINVAL;
2480
2481 req->sync.off = READ_ONCE(sqe->off);
2482 req->sync.len = READ_ONCE(sqe->addr);
2483 req->sync.mode = READ_ONCE(sqe->len);
2484 return 0;
2485}
2486
2487static int io_fallocate(struct io_kiocb *req, struct io_kiocb **nxt,
2488 bool force_nonblock)
2489{
2490 struct io_wq_work *work, *old_work;
2491
2492 /* fallocate always requiring blocking context */
2493 if (force_nonblock) {
2494 io_put_req(req);
2495 req->work.func = io_fallocate_finish;
2496 return -EAGAIN;
2497 }
2498
2499 work = old_work = &req->work;
2500 io_fallocate_finish(&work);
2501 if (work && work != old_work)
2502 *nxt = container_of(work, struct io_kiocb, work);
2503
2504 return 0;
2505}
2506
Jens Axboe15b71ab2019-12-11 11:20:36 -07002507static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2508{
Jens Axboef8748882020-01-08 17:47:02 -07002509 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002510 int ret;
2511
2512 if (sqe->ioprio || sqe->buf_index)
2513 return -EINVAL;
Jens Axboecf3040c2020-02-06 21:31:40 -07002514 if (sqe->flags & IOSQE_FIXED_FILE)
2515 return -EBADF;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002516
2517 req->open.dfd = READ_ONCE(sqe->fd);
Jens Axboec12cedf2020-01-08 17:41:21 -07002518 req->open.how.mode = READ_ONCE(sqe->len);
Jens Axboef8748882020-01-08 17:47:02 -07002519 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboec12cedf2020-01-08 17:41:21 -07002520 req->open.how.flags = READ_ONCE(sqe->open_flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002521
Jens Axboef8748882020-01-08 17:47:02 -07002522 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002523 if (IS_ERR(req->open.filename)) {
2524 ret = PTR_ERR(req->open.filename);
2525 req->open.filename = NULL;
2526 return ret;
2527 }
2528
2529 return 0;
2530}
2531
Jens Axboecebdb982020-01-08 17:59:24 -07002532static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2533{
2534 struct open_how __user *how;
2535 const char __user *fname;
2536 size_t len;
2537 int ret;
2538
2539 if (sqe->ioprio || sqe->buf_index)
2540 return -EINVAL;
Jens Axboecf3040c2020-02-06 21:31:40 -07002541 if (sqe->flags & IOSQE_FIXED_FILE)
2542 return -EBADF;
Jens Axboecebdb982020-01-08 17:59:24 -07002543
2544 req->open.dfd = READ_ONCE(sqe->fd);
2545 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
2546 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
2547 len = READ_ONCE(sqe->len);
2548
2549 if (len < OPEN_HOW_SIZE_VER0)
2550 return -EINVAL;
2551
2552 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
2553 len);
2554 if (ret)
2555 return ret;
2556
2557 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
2558 req->open.how.flags |= O_LARGEFILE;
2559
2560 req->open.filename = getname(fname);
2561 if (IS_ERR(req->open.filename)) {
2562 ret = PTR_ERR(req->open.filename);
2563 req->open.filename = NULL;
2564 return ret;
2565 }
2566
2567 return 0;
2568}
2569
2570static int io_openat2(struct io_kiocb *req, struct io_kiocb **nxt,
2571 bool force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07002572{
2573 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002574 struct file *file;
2575 int ret;
2576
Jens Axboef86cd202020-01-29 13:46:44 -07002577 if (force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07002578 return -EAGAIN;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002579
Jens Axboecebdb982020-01-08 17:59:24 -07002580 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002581 if (ret)
2582 goto err;
2583
Jens Axboecebdb982020-01-08 17:59:24 -07002584 ret = get_unused_fd_flags(req->open.how.flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002585 if (ret < 0)
2586 goto err;
2587
2588 file = do_filp_open(req->open.dfd, req->open.filename, &op);
2589 if (IS_ERR(file)) {
2590 put_unused_fd(ret);
2591 ret = PTR_ERR(file);
2592 } else {
2593 fsnotify_open(file);
2594 fd_install(ret, file);
2595 }
2596err:
2597 putname(req->open.filename);
2598 if (ret < 0)
2599 req_set_fail_links(req);
2600 io_cqring_add_event(req, ret);
2601 io_put_req_find_next(req, nxt);
2602 return 0;
2603}
2604
Jens Axboecebdb982020-01-08 17:59:24 -07002605static int io_openat(struct io_kiocb *req, struct io_kiocb **nxt,
2606 bool force_nonblock)
2607{
2608 req->open.how = build_open_how(req->open.how.flags, req->open.how.mode);
2609 return io_openat2(req, nxt, force_nonblock);
2610}
2611
Jens Axboe3e4827b2020-01-08 15:18:09 -07002612static int io_epoll_ctl_prep(struct io_kiocb *req,
2613 const struct io_uring_sqe *sqe)
2614{
2615#if defined(CONFIG_EPOLL)
2616 if (sqe->ioprio || sqe->buf_index)
2617 return -EINVAL;
2618
2619 req->epoll.epfd = READ_ONCE(sqe->fd);
2620 req->epoll.op = READ_ONCE(sqe->len);
2621 req->epoll.fd = READ_ONCE(sqe->off);
2622
2623 if (ep_op_has_event(req->epoll.op)) {
2624 struct epoll_event __user *ev;
2625
2626 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
2627 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
2628 return -EFAULT;
2629 }
2630
2631 return 0;
2632#else
2633 return -EOPNOTSUPP;
2634#endif
2635}
2636
2637static int io_epoll_ctl(struct io_kiocb *req, struct io_kiocb **nxt,
2638 bool force_nonblock)
2639{
2640#if defined(CONFIG_EPOLL)
2641 struct io_epoll *ie = &req->epoll;
2642 int ret;
2643
2644 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
2645 if (force_nonblock && ret == -EAGAIN)
2646 return -EAGAIN;
2647
2648 if (ret < 0)
2649 req_set_fail_links(req);
2650 io_cqring_add_event(req, ret);
2651 io_put_req_find_next(req, nxt);
2652 return 0;
2653#else
2654 return -EOPNOTSUPP;
2655#endif
2656}
2657
Jens Axboec1ca7572019-12-25 22:18:28 -07002658static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2659{
2660#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
2661 if (sqe->ioprio || sqe->buf_index || sqe->off)
2662 return -EINVAL;
2663
2664 req->madvise.addr = READ_ONCE(sqe->addr);
2665 req->madvise.len = READ_ONCE(sqe->len);
2666 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
2667 return 0;
2668#else
2669 return -EOPNOTSUPP;
2670#endif
2671}
2672
2673static int io_madvise(struct io_kiocb *req, struct io_kiocb **nxt,
2674 bool force_nonblock)
2675{
2676#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
2677 struct io_madvise *ma = &req->madvise;
2678 int ret;
2679
2680 if (force_nonblock)
2681 return -EAGAIN;
2682
2683 ret = do_madvise(ma->addr, ma->len, ma->advice);
2684 if (ret < 0)
2685 req_set_fail_links(req);
2686 io_cqring_add_event(req, ret);
2687 io_put_req_find_next(req, nxt);
2688 return 0;
2689#else
2690 return -EOPNOTSUPP;
2691#endif
2692}
2693
Jens Axboe4840e412019-12-25 22:03:45 -07002694static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2695{
2696 if (sqe->ioprio || sqe->buf_index || sqe->addr)
2697 return -EINVAL;
2698
2699 req->fadvise.offset = READ_ONCE(sqe->off);
2700 req->fadvise.len = READ_ONCE(sqe->len);
2701 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
2702 return 0;
2703}
2704
2705static int io_fadvise(struct io_kiocb *req, struct io_kiocb **nxt,
2706 bool force_nonblock)
2707{
2708 struct io_fadvise *fa = &req->fadvise;
2709 int ret;
2710
Jens Axboe3e694262020-02-01 09:22:49 -07002711 if (force_nonblock) {
2712 switch (fa->advice) {
2713 case POSIX_FADV_NORMAL:
2714 case POSIX_FADV_RANDOM:
2715 case POSIX_FADV_SEQUENTIAL:
2716 break;
2717 default:
2718 return -EAGAIN;
2719 }
2720 }
Jens Axboe4840e412019-12-25 22:03:45 -07002721
2722 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
2723 if (ret < 0)
2724 req_set_fail_links(req);
2725 io_cqring_add_event(req, ret);
2726 io_put_req_find_next(req, nxt);
2727 return 0;
2728}
2729
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002730static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2731{
Jens Axboef8748882020-01-08 17:47:02 -07002732 const char __user *fname;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002733 unsigned lookup_flags;
2734 int ret;
2735
2736 if (sqe->ioprio || sqe->buf_index)
2737 return -EINVAL;
Jens Axboecf3040c2020-02-06 21:31:40 -07002738 if (sqe->flags & IOSQE_FIXED_FILE)
2739 return -EBADF;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002740
2741 req->open.dfd = READ_ONCE(sqe->fd);
2742 req->open.mask = READ_ONCE(sqe->len);
Jens Axboef8748882020-01-08 17:47:02 -07002743 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002744 req->open.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboec12cedf2020-01-08 17:41:21 -07002745 req->open.how.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002746
Jens Axboec12cedf2020-01-08 17:41:21 -07002747 if (vfs_stat_set_lookup_flags(&lookup_flags, req->open.how.flags))
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002748 return -EINVAL;
2749
Jens Axboef8748882020-01-08 17:47:02 -07002750 req->open.filename = getname_flags(fname, lookup_flags, NULL);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002751 if (IS_ERR(req->open.filename)) {
2752 ret = PTR_ERR(req->open.filename);
2753 req->open.filename = NULL;
2754 return ret;
2755 }
2756
2757 return 0;
2758}
2759
2760static int io_statx(struct io_kiocb *req, struct io_kiocb **nxt,
2761 bool force_nonblock)
2762{
2763 struct io_open *ctx = &req->open;
2764 unsigned lookup_flags;
2765 struct path path;
2766 struct kstat stat;
2767 int ret;
2768
2769 if (force_nonblock)
2770 return -EAGAIN;
2771
Jens Axboec12cedf2020-01-08 17:41:21 -07002772 if (vfs_stat_set_lookup_flags(&lookup_flags, ctx->how.flags))
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002773 return -EINVAL;
2774
2775retry:
2776 /* filename_lookup() drops it, keep a reference */
2777 ctx->filename->refcnt++;
2778
2779 ret = filename_lookup(ctx->dfd, ctx->filename, lookup_flags, &path,
2780 NULL);
2781 if (ret)
2782 goto err;
2783
Jens Axboec12cedf2020-01-08 17:41:21 -07002784 ret = vfs_getattr(&path, &stat, ctx->mask, ctx->how.flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002785 path_put(&path);
2786 if (retry_estale(ret, lookup_flags)) {
2787 lookup_flags |= LOOKUP_REVAL;
2788 goto retry;
2789 }
2790 if (!ret)
2791 ret = cp_statx(&stat, ctx->buffer);
2792err:
2793 putname(ctx->filename);
2794 if (ret < 0)
2795 req_set_fail_links(req);
2796 io_cqring_add_event(req, ret);
2797 io_put_req_find_next(req, nxt);
2798 return 0;
2799}
2800
Jens Axboeb5dba592019-12-11 14:02:38 -07002801static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2802{
2803 /*
2804 * If we queue this for async, it must not be cancellable. That would
2805 * leave the 'file' in an undeterminate state.
2806 */
2807 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
2808
2809 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
2810 sqe->rw_flags || sqe->buf_index)
2811 return -EINVAL;
2812 if (sqe->flags & IOSQE_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07002813 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07002814
2815 req->close.fd = READ_ONCE(sqe->fd);
2816 if (req->file->f_op == &io_uring_fops ||
Pavel Begunkovb14cca02020-01-17 04:45:59 +03002817 req->close.fd == req->ctx->ring_fd)
Jens Axboeb5dba592019-12-11 14:02:38 -07002818 return -EBADF;
2819
2820 return 0;
2821}
2822
2823static void io_close_finish(struct io_wq_work **workptr)
2824{
2825 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2826 struct io_kiocb *nxt = NULL;
2827
2828 /* Invoked with files, we need to do the close */
2829 if (req->work.files) {
2830 int ret;
2831
2832 ret = filp_close(req->close.put_file, req->work.files);
Jens Axboe1a417f42020-01-31 17:16:48 -07002833 if (ret < 0)
Jens Axboeb5dba592019-12-11 14:02:38 -07002834 req_set_fail_links(req);
Jens Axboeb5dba592019-12-11 14:02:38 -07002835 io_cqring_add_event(req, ret);
2836 }
2837
2838 fput(req->close.put_file);
2839
Jens Axboeb5dba592019-12-11 14:02:38 -07002840 io_put_req_find_next(req, &nxt);
2841 if (nxt)
2842 io_wq_assign_next(workptr, nxt);
2843}
2844
2845static int io_close(struct io_kiocb *req, struct io_kiocb **nxt,
2846 bool force_nonblock)
2847{
2848 int ret;
2849
2850 req->close.put_file = NULL;
2851 ret = __close_fd_get_file(req->close.fd, &req->close.put_file);
2852 if (ret < 0)
2853 return ret;
2854
2855 /* if the file has a flush method, be safe and punt to async */
Jens Axboef86cd202020-01-29 13:46:44 -07002856 if (req->close.put_file->f_op->flush && !io_wq_current_is_worker())
Jens Axboeb5dba592019-12-11 14:02:38 -07002857 goto eagain;
Jens Axboeb5dba592019-12-11 14:02:38 -07002858
2859 /*
2860 * No ->flush(), safely close from here and just punt the
2861 * fput() to async context.
2862 */
2863 ret = filp_close(req->close.put_file, current->files);
2864
2865 if (ret < 0)
2866 req_set_fail_links(req);
2867 io_cqring_add_event(req, ret);
2868
2869 if (io_wq_current_is_worker()) {
2870 struct io_wq_work *old_work, *work;
2871
2872 old_work = work = &req->work;
2873 io_close_finish(&work);
2874 if (work && work != old_work)
2875 *nxt = container_of(work, struct io_kiocb, work);
2876 return 0;
2877 }
2878
2879eagain:
2880 req->work.func = io_close_finish;
Jens Axboe1a417f42020-01-31 17:16:48 -07002881 /*
2882 * Do manual async queue here to avoid grabbing files - we don't
2883 * need the files, and it'll cause io_close_finish() to close
2884 * the file again and cause a double CQE entry for this request
2885 */
2886 io_queue_async_work(req);
2887 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07002888}
2889
Jens Axboe3529d8c2019-12-19 18:24:38 -07002890static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002891{
2892 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002893
2894 if (!req->file)
2895 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002896
2897 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2898 return -EINVAL;
2899 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
2900 return -EINVAL;
2901
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002902 req->sync.off = READ_ONCE(sqe->off);
2903 req->sync.len = READ_ONCE(sqe->len);
2904 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002905 return 0;
2906}
2907
2908static void io_sync_file_range_finish(struct io_wq_work **workptr)
2909{
2910 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2911 struct io_kiocb *nxt = NULL;
2912 int ret;
2913
2914 if (io_req_cancelled(req))
2915 return;
2916
Jens Axboe9adbd452019-12-20 08:45:55 -07002917 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002918 req->sync.flags);
2919 if (ret < 0)
2920 req_set_fail_links(req);
2921 io_cqring_add_event(req, ret);
2922 io_put_req_find_next(req, &nxt);
2923 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07002924 io_wq_assign_next(workptr, nxt);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002925}
2926
Jens Axboefc4df992019-12-10 14:38:45 -07002927static int io_sync_file_range(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002928 bool force_nonblock)
2929{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002930 struct io_wq_work *work, *old_work;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002931
2932 /* sync_file_range always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002933 if (force_nonblock) {
2934 io_put_req(req);
2935 req->work.func = io_sync_file_range_finish;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002936 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002937 }
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002938
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002939 work = old_work = &req->work;
2940 io_sync_file_range_finish(&work);
2941 if (work && work != old_work)
2942 *nxt = container_of(work, struct io_kiocb, work);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002943 return 0;
2944}
2945
Jens Axboe3529d8c2019-12-19 18:24:38 -07002946static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06002947{
Jens Axboe03b12302019-12-02 18:50:25 -07002948#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07002949 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002950 struct io_async_ctx *io = req->io;
Jens Axboe03b12302019-12-02 18:50:25 -07002951
Jens Axboee47293f2019-12-20 08:58:21 -07002952 sr->msg_flags = READ_ONCE(sqe->msg_flags);
2953 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07002954 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07002955
Jens Axboefddafac2020-01-04 20:19:44 -07002956 if (!io || req->opcode == IORING_OP_SEND)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002957 return 0;
2958
Jens Axboed9688562019-12-09 19:35:20 -07002959 io->msg.iov = io->msg.fast_iov;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002960 return sendmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07002961 &io->msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07002962#else
Jens Axboee47293f2019-12-20 08:58:21 -07002963 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07002964#endif
2965}
2966
Jens Axboefc4df992019-12-10 14:38:45 -07002967static int io_sendmsg(struct io_kiocb *req, struct io_kiocb **nxt,
2968 bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07002969{
2970#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07002971 struct io_async_msghdr *kmsg = NULL;
Jens Axboe03b12302019-12-02 18:50:25 -07002972 struct socket *sock;
2973 int ret;
2974
2975 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
2976 return -EINVAL;
2977
2978 sock = sock_from_file(req->file, &ret);
2979 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002980 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07002981 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -07002982 unsigned flags;
2983
Jens Axboe03b12302019-12-02 18:50:25 -07002984 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07002985 kmsg = &req->io->msg;
2986 kmsg->msg.msg_name = &addr;
2987 /* if iov is set, it's allocated already */
2988 if (!kmsg->iov)
2989 kmsg->iov = kmsg->fast_iov;
2990 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07002991 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07002992 struct io_sr_msg *sr = &req->sr_msg;
2993
Jens Axboe0b416c32019-12-15 10:57:46 -07002994 kmsg = &io.msg;
2995 kmsg->msg.msg_name = &addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002996
2997 io.msg.iov = io.msg.fast_iov;
2998 ret = sendmsg_copy_msghdr(&io.msg.msg, sr->msg,
2999 sr->msg_flags, &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07003000 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003001 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003002 }
3003
Jens Axboee47293f2019-12-20 08:58:21 -07003004 flags = req->sr_msg.msg_flags;
3005 if (flags & MSG_DONTWAIT)
3006 req->flags |= REQ_F_NOWAIT;
3007 else if (force_nonblock)
3008 flags |= MSG_DONTWAIT;
3009
Jens Axboe0b416c32019-12-15 10:57:46 -07003010 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Jens Axboe03b12302019-12-02 18:50:25 -07003011 if (force_nonblock && ret == -EAGAIN) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003012 if (req->io)
3013 return -EAGAIN;
Pavel Begunkov1e950812020-02-06 19:51:16 +03003014 if (io_alloc_async_ctx(req)) {
3015 if (kmsg && kmsg->iov != kmsg->fast_iov)
3016 kfree(kmsg->iov);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003017 return -ENOMEM;
Pavel Begunkov1e950812020-02-06 19:51:16 +03003018 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003019 memcpy(&req->io->msg, &io.msg, sizeof(io.msg));
Jens Axboe0b416c32019-12-15 10:57:46 -07003020 return -EAGAIN;
Jens Axboe03b12302019-12-02 18:50:25 -07003021 }
3022 if (ret == -ERESTARTSYS)
3023 ret = -EINTR;
3024 }
3025
Pavel Begunkov1e950812020-02-06 19:51:16 +03003026 if (kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07003027 kfree(kmsg->iov);
Jens Axboe03b12302019-12-02 18:50:25 -07003028 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003029 if (ret < 0)
3030 req_set_fail_links(req);
Jens Axboe03b12302019-12-02 18:50:25 -07003031 io_put_req_find_next(req, nxt);
3032 return 0;
3033#else
3034 return -EOPNOTSUPP;
3035#endif
3036}
3037
Jens Axboefddafac2020-01-04 20:19:44 -07003038static int io_send(struct io_kiocb *req, struct io_kiocb **nxt,
3039 bool force_nonblock)
3040{
3041#if defined(CONFIG_NET)
3042 struct socket *sock;
3043 int ret;
3044
3045 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3046 return -EINVAL;
3047
3048 sock = sock_from_file(req->file, &ret);
3049 if (sock) {
3050 struct io_sr_msg *sr = &req->sr_msg;
3051 struct msghdr msg;
3052 struct iovec iov;
3053 unsigned flags;
3054
3055 ret = import_single_range(WRITE, sr->buf, sr->len, &iov,
3056 &msg.msg_iter);
3057 if (ret)
3058 return ret;
3059
3060 msg.msg_name = NULL;
3061 msg.msg_control = NULL;
3062 msg.msg_controllen = 0;
3063 msg.msg_namelen = 0;
3064
3065 flags = req->sr_msg.msg_flags;
3066 if (flags & MSG_DONTWAIT)
3067 req->flags |= REQ_F_NOWAIT;
3068 else if (force_nonblock)
3069 flags |= MSG_DONTWAIT;
3070
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003071 msg.msg_flags = flags;
3072 ret = sock_sendmsg(sock, &msg);
Jens Axboefddafac2020-01-04 20:19:44 -07003073 if (force_nonblock && ret == -EAGAIN)
3074 return -EAGAIN;
3075 if (ret == -ERESTARTSYS)
3076 ret = -EINTR;
3077 }
3078
3079 io_cqring_add_event(req, ret);
3080 if (ret < 0)
3081 req_set_fail_links(req);
3082 io_put_req_find_next(req, nxt);
3083 return 0;
3084#else
3085 return -EOPNOTSUPP;
3086#endif
3087}
3088
Jens Axboe3529d8c2019-12-19 18:24:38 -07003089static int io_recvmsg_prep(struct io_kiocb *req,
3090 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07003091{
3092#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07003093 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003094 struct io_async_ctx *io = req->io;
Jens Axboe06b76d42019-12-19 14:44:26 -07003095
Jens Axboe3529d8c2019-12-19 18:24:38 -07003096 sr->msg_flags = READ_ONCE(sqe->msg_flags);
3097 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003098 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003099
Jens Axboefddafac2020-01-04 20:19:44 -07003100 if (!io || req->opcode == IORING_OP_RECV)
Jens Axboe06b76d42019-12-19 14:44:26 -07003101 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07003102
Jens Axboed9688562019-12-09 19:35:20 -07003103 io->msg.iov = io->msg.fast_iov;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003104 return recvmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07003105 &io->msg.uaddr, &io->msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07003106#else
Jens Axboee47293f2019-12-20 08:58:21 -07003107 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07003108#endif
3109}
3110
Jens Axboefc4df992019-12-10 14:38:45 -07003111static int io_recvmsg(struct io_kiocb *req, struct io_kiocb **nxt,
3112 bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07003113{
3114#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07003115 struct io_async_msghdr *kmsg = NULL;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003116 struct socket *sock;
3117 int ret;
3118
3119 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3120 return -EINVAL;
3121
3122 sock = sock_from_file(req->file, &ret);
3123 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003124 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07003125 struct sockaddr_storage addr;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003126 unsigned flags;
3127
Jens Axboe03b12302019-12-02 18:50:25 -07003128 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07003129 kmsg = &req->io->msg;
3130 kmsg->msg.msg_name = &addr;
3131 /* if iov is set, it's allocated already */
3132 if (!kmsg->iov)
3133 kmsg->iov = kmsg->fast_iov;
3134 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07003135 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003136 struct io_sr_msg *sr = &req->sr_msg;
3137
Jens Axboe0b416c32019-12-15 10:57:46 -07003138 kmsg = &io.msg;
3139 kmsg->msg.msg_name = &addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003140
3141 io.msg.iov = io.msg.fast_iov;
3142 ret = recvmsg_copy_msghdr(&io.msg.msg, sr->msg,
3143 sr->msg_flags, &io.msg.uaddr,
3144 &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07003145 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003146 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003147 }
Jens Axboe0fa03c62019-04-19 13:34:07 -06003148
Jens Axboee47293f2019-12-20 08:58:21 -07003149 flags = req->sr_msg.msg_flags;
3150 if (flags & MSG_DONTWAIT)
3151 req->flags |= REQ_F_NOWAIT;
3152 else if (force_nonblock)
3153 flags |= MSG_DONTWAIT;
3154
3155 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.msg,
3156 kmsg->uaddr, flags);
Jens Axboe03b12302019-12-02 18:50:25 -07003157 if (force_nonblock && ret == -EAGAIN) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003158 if (req->io)
3159 return -EAGAIN;
Pavel Begunkov1e950812020-02-06 19:51:16 +03003160 if (io_alloc_async_ctx(req)) {
3161 if (kmsg && kmsg->iov != kmsg->fast_iov)
3162 kfree(kmsg->iov);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003163 return -ENOMEM;
Pavel Begunkov1e950812020-02-06 19:51:16 +03003164 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003165 memcpy(&req->io->msg, &io.msg, sizeof(io.msg));
Jens Axboe0b416c32019-12-15 10:57:46 -07003166 return -EAGAIN;
Jens Axboe03b12302019-12-02 18:50:25 -07003167 }
Jens Axboe441cdbd2019-12-02 18:49:10 -07003168 if (ret == -ERESTARTSYS)
3169 ret = -EINTR;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003170 }
3171
Pavel Begunkov1e950812020-02-06 19:51:16 +03003172 if (kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07003173 kfree(kmsg->iov);
Jens Axboe78e19bb2019-11-06 15:21:34 -07003174 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003175 if (ret < 0)
3176 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003177 io_put_req_find_next(req, nxt);
Jens Axboe0fa03c62019-04-19 13:34:07 -06003178 return 0;
3179#else
3180 return -EOPNOTSUPP;
3181#endif
3182}
3183
Jens Axboefddafac2020-01-04 20:19:44 -07003184static int io_recv(struct io_kiocb *req, struct io_kiocb **nxt,
3185 bool force_nonblock)
3186{
3187#if defined(CONFIG_NET)
3188 struct socket *sock;
3189 int ret;
3190
3191 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3192 return -EINVAL;
3193
3194 sock = sock_from_file(req->file, &ret);
3195 if (sock) {
3196 struct io_sr_msg *sr = &req->sr_msg;
3197 struct msghdr msg;
3198 struct iovec iov;
3199 unsigned flags;
3200
3201 ret = import_single_range(READ, sr->buf, sr->len, &iov,
3202 &msg.msg_iter);
3203 if (ret)
3204 return ret;
3205
3206 msg.msg_name = NULL;
3207 msg.msg_control = NULL;
3208 msg.msg_controllen = 0;
3209 msg.msg_namelen = 0;
3210 msg.msg_iocb = NULL;
3211 msg.msg_flags = 0;
3212
3213 flags = req->sr_msg.msg_flags;
3214 if (flags & MSG_DONTWAIT)
3215 req->flags |= REQ_F_NOWAIT;
3216 else if (force_nonblock)
3217 flags |= MSG_DONTWAIT;
3218
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003219 ret = sock_recvmsg(sock, &msg, flags);
Jens Axboefddafac2020-01-04 20:19:44 -07003220 if (force_nonblock && ret == -EAGAIN)
3221 return -EAGAIN;
3222 if (ret == -ERESTARTSYS)
3223 ret = -EINTR;
3224 }
3225
3226 io_cqring_add_event(req, ret);
3227 if (ret < 0)
3228 req_set_fail_links(req);
3229 io_put_req_find_next(req, nxt);
3230 return 0;
3231#else
3232 return -EOPNOTSUPP;
3233#endif
3234}
3235
3236
Jens Axboe3529d8c2019-12-19 18:24:38 -07003237static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003238{
3239#if defined(CONFIG_NET)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003240 struct io_accept *accept = &req->accept;
3241
Jens Axboe17f2fe32019-10-17 14:42:58 -06003242 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3243 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05003244 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003245 return -EINVAL;
3246
Jens Axboed55e5f52019-12-11 16:12:15 -07003247 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3248 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003249 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003250 return 0;
3251#else
3252 return -EOPNOTSUPP;
3253#endif
3254}
Jens Axboe17f2fe32019-10-17 14:42:58 -06003255
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003256#if defined(CONFIG_NET)
3257static int __io_accept(struct io_kiocb *req, struct io_kiocb **nxt,
3258 bool force_nonblock)
3259{
3260 struct io_accept *accept = &req->accept;
3261 unsigned file_flags;
3262 int ret;
3263
3264 file_flags = force_nonblock ? O_NONBLOCK : 0;
3265 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
3266 accept->addr_len, accept->flags);
3267 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003268 return -EAGAIN;
Jens Axboe8e3cca12019-11-09 19:52:33 -07003269 if (ret == -ERESTARTSYS)
3270 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003271 if (ret < 0)
3272 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07003273 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003274 io_put_req_find_next(req, nxt);
Jens Axboe17f2fe32019-10-17 14:42:58 -06003275 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003276}
3277
3278static void io_accept_finish(struct io_wq_work **workptr)
3279{
3280 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3281 struct io_kiocb *nxt = NULL;
3282
3283 if (io_req_cancelled(req))
3284 return;
3285 __io_accept(req, &nxt, false);
3286 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07003287 io_wq_assign_next(workptr, nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003288}
3289#endif
3290
3291static int io_accept(struct io_kiocb *req, struct io_kiocb **nxt,
3292 bool force_nonblock)
3293{
3294#if defined(CONFIG_NET)
3295 int ret;
3296
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003297 ret = __io_accept(req, nxt, force_nonblock);
3298 if (ret == -EAGAIN && force_nonblock) {
3299 req->work.func = io_accept_finish;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003300 io_put_req(req);
3301 return -EAGAIN;
3302 }
3303 return 0;
Jens Axboe17f2fe32019-10-17 14:42:58 -06003304#else
3305 return -EOPNOTSUPP;
3306#endif
3307}
3308
Jens Axboe3529d8c2019-12-19 18:24:38 -07003309static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07003310{
3311#if defined(CONFIG_NET)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003312 struct io_connect *conn = &req->connect;
3313 struct io_async_ctx *io = req->io;
Jens Axboef499a022019-12-02 16:28:46 -07003314
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003315 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3316 return -EINVAL;
3317 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
3318 return -EINVAL;
3319
Jens Axboe3529d8c2019-12-19 18:24:38 -07003320 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3321 conn->addr_len = READ_ONCE(sqe->addr2);
3322
3323 if (!io)
3324 return 0;
3325
3326 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003327 &io->connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07003328#else
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003329 return -EOPNOTSUPP;
Jens Axboef499a022019-12-02 16:28:46 -07003330#endif
3331}
3332
Jens Axboefc4df992019-12-10 14:38:45 -07003333static int io_connect(struct io_kiocb *req, struct io_kiocb **nxt,
3334 bool force_nonblock)
Jens Axboef8e85cf2019-11-23 14:24:24 -07003335{
3336#if defined(CONFIG_NET)
Jens Axboef499a022019-12-02 16:28:46 -07003337 struct io_async_ctx __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003338 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003339 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003340
Jens Axboef499a022019-12-02 16:28:46 -07003341 if (req->io) {
3342 io = req->io;
3343 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003344 ret = move_addr_to_kernel(req->connect.addr,
3345 req->connect.addr_len,
3346 &__io.connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07003347 if (ret)
3348 goto out;
3349 io = &__io;
3350 }
3351
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003352 file_flags = force_nonblock ? O_NONBLOCK : 0;
3353
3354 ret = __sys_connect_file(req->file, &io->connect.address,
3355 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07003356 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003357 if (req->io)
3358 return -EAGAIN;
3359 if (io_alloc_async_ctx(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07003360 ret = -ENOMEM;
3361 goto out;
3362 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003363 memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect));
Jens Axboef8e85cf2019-11-23 14:24:24 -07003364 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07003365 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07003366 if (ret == -ERESTARTSYS)
3367 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07003368out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003369 if (ret < 0)
3370 req_set_fail_links(req);
Jens Axboef8e85cf2019-11-23 14:24:24 -07003371 io_cqring_add_event(req, ret);
3372 io_put_req_find_next(req, nxt);
3373 return 0;
3374#else
3375 return -EOPNOTSUPP;
3376#endif
3377}
3378
Jens Axboe221c5eb2019-01-17 09:41:58 -07003379static void io_poll_remove_one(struct io_kiocb *req)
3380{
3381 struct io_poll_iocb *poll = &req->poll;
3382
3383 spin_lock(&poll->head->lock);
3384 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07003385 if (!list_empty(&poll->wait.entry)) {
3386 list_del_init(&poll->wait.entry);
Jackie Liua197f662019-11-08 08:09:12 -07003387 io_queue_async_work(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003388 }
3389 spin_unlock(&poll->head->lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07003390 hash_del(&req->hash_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003391}
3392
3393static void io_poll_remove_all(struct io_ring_ctx *ctx)
3394{
Jens Axboe78076bb2019-12-04 19:56:40 -07003395 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003396 struct io_kiocb *req;
Jens Axboe78076bb2019-12-04 19:56:40 -07003397 int i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003398
3399 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07003400 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
3401 struct hlist_head *list;
3402
3403 list = &ctx->cancel_hash[i];
3404 hlist_for_each_entry_safe(req, tmp, list, hash_node)
3405 io_poll_remove_one(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003406 }
3407 spin_unlock_irq(&ctx->completion_lock);
3408}
3409
Jens Axboe47f46762019-11-09 17:43:02 -07003410static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
3411{
Jens Axboe78076bb2019-12-04 19:56:40 -07003412 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07003413 struct io_kiocb *req;
3414
Jens Axboe78076bb2019-12-04 19:56:40 -07003415 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
3416 hlist_for_each_entry(req, list, hash_node) {
3417 if (sqe_addr == req->user_data) {
Jens Axboeeac406c2019-11-14 12:09:58 -07003418 io_poll_remove_one(req);
3419 return 0;
3420 }
Jens Axboe47f46762019-11-09 17:43:02 -07003421 }
3422
3423 return -ENOENT;
3424}
3425
Jens Axboe3529d8c2019-12-19 18:24:38 -07003426static int io_poll_remove_prep(struct io_kiocb *req,
3427 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003428{
Jens Axboe221c5eb2019-01-17 09:41:58 -07003429 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3430 return -EINVAL;
3431 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
3432 sqe->poll_events)
3433 return -EINVAL;
3434
Jens Axboe0969e782019-12-17 18:40:57 -07003435 req->poll.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07003436 return 0;
3437}
3438
3439/*
3440 * Find a running poll command that matches one specified in sqe->addr,
3441 * and remove it if found.
3442 */
3443static int io_poll_remove(struct io_kiocb *req)
3444{
3445 struct io_ring_ctx *ctx = req->ctx;
3446 u64 addr;
3447 int ret;
3448
Jens Axboe0969e782019-12-17 18:40:57 -07003449 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003450 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07003451 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003452 spin_unlock_irq(&ctx->completion_lock);
3453
Jens Axboe78e19bb2019-11-06 15:21:34 -07003454 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003455 if (ret < 0)
3456 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06003457 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003458 return 0;
3459}
3460
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003461static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003462{
Jackie Liua197f662019-11-08 08:09:12 -07003463 struct io_ring_ctx *ctx = req->ctx;
3464
Jens Axboe8c838782019-03-12 15:48:16 -06003465 req->poll.done = true;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003466 if (error)
3467 io_cqring_fill_event(req, error);
3468 else
3469 io_cqring_fill_event(req, mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06003470 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003471}
3472
Jens Axboe561fb042019-10-24 07:25:42 -06003473static void io_poll_complete_work(struct io_wq_work **workptr)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003474{
Jens Axboe561fb042019-10-24 07:25:42 -06003475 struct io_wq_work *work = *workptr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003476 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
3477 struct io_poll_iocb *poll = &req->poll;
3478 struct poll_table_struct pt = { ._key = poll->events };
3479 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe89723d02019-11-05 15:32:58 -07003480 struct io_kiocb *nxt = NULL;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003481 __poll_t mask = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003482 int ret = 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003483
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003484 if (work->flags & IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06003485 WRITE_ONCE(poll->canceled, true);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003486 ret = -ECANCELED;
3487 } else if (READ_ONCE(poll->canceled)) {
3488 ret = -ECANCELED;
3489 }
Jens Axboe561fb042019-10-24 07:25:42 -06003490
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003491 if (ret != -ECANCELED)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003492 mask = vfs_poll(poll->file, &pt) & poll->events;
3493
3494 /*
3495 * Note that ->ki_cancel callers also delete iocb from active_reqs after
3496 * calling ->ki_cancel. We need the ctx_lock roundtrip here to
3497 * synchronize with them. In the cancellation case the list_del_init
3498 * itself is not actually needed, but harmless so we keep it in to
3499 * avoid further branches in the fast path.
3500 */
3501 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003502 if (!mask && ret != -ECANCELED) {
Jens Axboe392edb42019-12-09 17:52:20 -07003503 add_wait_queue(poll->head, &poll->wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003504 spin_unlock_irq(&ctx->completion_lock);
3505 return;
3506 }
Jens Axboe78076bb2019-12-04 19:56:40 -07003507 hash_del(&req->hash_node);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003508 io_poll_complete(req, mask, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003509 spin_unlock_irq(&ctx->completion_lock);
3510
Jens Axboe8c838782019-03-12 15:48:16 -06003511 io_cqring_ev_posted(ctx);
Jens Axboe89723d02019-11-05 15:32:58 -07003512
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003513 if (ret < 0)
3514 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003515 io_put_req_find_next(req, &nxt);
Jens Axboe89723d02019-11-05 15:32:58 -07003516 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07003517 io_wq_assign_next(workptr, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003518}
3519
Jens Axboee94f1412019-12-19 12:06:02 -07003520static void __io_poll_flush(struct io_ring_ctx *ctx, struct llist_node *nodes)
3521{
Jens Axboee94f1412019-12-19 12:06:02 -07003522 struct io_kiocb *req, *tmp;
Jens Axboe8237e042019-12-28 10:48:22 -07003523 struct req_batch rb;
Jens Axboee94f1412019-12-19 12:06:02 -07003524
Jens Axboec6ca97b302019-12-28 12:11:08 -07003525 rb.to_free = rb.need_iter = 0;
Jens Axboee94f1412019-12-19 12:06:02 -07003526 spin_lock_irq(&ctx->completion_lock);
3527 llist_for_each_entry_safe(req, tmp, nodes, llist_node) {
3528 hash_del(&req->hash_node);
3529 io_poll_complete(req, req->result, 0);
3530
Jens Axboe8237e042019-12-28 10:48:22 -07003531 if (refcount_dec_and_test(&req->refs) &&
3532 !io_req_multi_free(&rb, req)) {
3533 req->flags |= REQ_F_COMP_LOCKED;
3534 io_free_req(req);
Jens Axboee94f1412019-12-19 12:06:02 -07003535 }
3536 }
3537 spin_unlock_irq(&ctx->completion_lock);
3538
3539 io_cqring_ev_posted(ctx);
Jens Axboe8237e042019-12-28 10:48:22 -07003540 io_free_req_many(ctx, &rb);
Jens Axboee94f1412019-12-19 12:06:02 -07003541}
3542
3543static void io_poll_flush(struct io_wq_work **workptr)
3544{
3545 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3546 struct llist_node *nodes;
3547
3548 nodes = llist_del_all(&req->ctx->poll_llist);
3549 if (nodes)
3550 __io_poll_flush(req->ctx, nodes);
3551}
3552
Jens Axboef0b493e2020-02-01 21:30:11 -07003553static void io_poll_trigger_evfd(struct io_wq_work **workptr)
3554{
3555 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3556
3557 eventfd_signal(req->ctx->cq_ev_fd, 1);
3558 io_put_req(req);
3559}
3560
Jens Axboe221c5eb2019-01-17 09:41:58 -07003561static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
3562 void *key)
3563{
Jens Axboee9444752019-11-26 15:02:04 -07003564 struct io_poll_iocb *poll = wait->private;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003565 struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
3566 struct io_ring_ctx *ctx = req->ctx;
3567 __poll_t mask = key_to_poll(key);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003568
3569 /* for instances that support it check for an event match first: */
Jens Axboe8c838782019-03-12 15:48:16 -06003570 if (mask && !(mask & poll->events))
3571 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003572
Jens Axboe392edb42019-12-09 17:52:20 -07003573 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06003574
Jens Axboe7c9e7f02019-11-12 08:15:53 -07003575 /*
3576 * Run completion inline if we can. We're using trylock here because
3577 * we are violating the completion_lock -> poll wq lock ordering.
3578 * If we have a link timeout we're going to need the completion_lock
3579 * for finalizing the request, mark us as having grabbed that already.
3580 */
Jens Axboee94f1412019-12-19 12:06:02 -07003581 if (mask) {
3582 unsigned long flags;
Jens Axboe8c838782019-03-12 15:48:16 -06003583
Jens Axboee94f1412019-12-19 12:06:02 -07003584 if (llist_empty(&ctx->poll_llist) &&
3585 spin_trylock_irqsave(&ctx->completion_lock, flags)) {
Jens Axboef0b493e2020-02-01 21:30:11 -07003586 bool trigger_ev;
3587
Jens Axboee94f1412019-12-19 12:06:02 -07003588 hash_del(&req->hash_node);
3589 io_poll_complete(req, mask, 0);
Jens Axboee94f1412019-12-19 12:06:02 -07003590
Jens Axboef0b493e2020-02-01 21:30:11 -07003591 trigger_ev = io_should_trigger_evfd(ctx);
3592 if (trigger_ev && eventfd_signal_count()) {
3593 trigger_ev = false;
3594 req->work.func = io_poll_trigger_evfd;
3595 } else {
3596 req->flags |= REQ_F_COMP_LOCKED;
3597 io_put_req(req);
3598 req = NULL;
3599 }
3600 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3601 __io_cqring_ev_posted(ctx, trigger_ev);
Jens Axboee94f1412019-12-19 12:06:02 -07003602 } else {
3603 req->result = mask;
3604 req->llist_node.next = NULL;
3605 /* if the list wasn't empty, we're done */
3606 if (!llist_add(&req->llist_node, &ctx->poll_llist))
3607 req = NULL;
3608 else
3609 req->work.func = io_poll_flush;
3610 }
Jens Axboe8c838782019-03-12 15:48:16 -06003611 }
Jens Axboee94f1412019-12-19 12:06:02 -07003612 if (req)
3613 io_queue_async_work(req);
Jens Axboe8c838782019-03-12 15:48:16 -06003614
Jens Axboe221c5eb2019-01-17 09:41:58 -07003615 return 1;
3616}
3617
3618struct io_poll_table {
3619 struct poll_table_struct pt;
3620 struct io_kiocb *req;
3621 int error;
3622};
3623
3624static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
3625 struct poll_table_struct *p)
3626{
3627 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
3628
3629 if (unlikely(pt->req->poll.head)) {
3630 pt->error = -EINVAL;
3631 return;
3632 }
3633
3634 pt->error = 0;
3635 pt->req->poll.head = head;
Jens Axboe392edb42019-12-09 17:52:20 -07003636 add_wait_queue(head, &pt->req->poll.wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003637}
3638
Jens Axboeeac406c2019-11-14 12:09:58 -07003639static void io_poll_req_insert(struct io_kiocb *req)
3640{
3641 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07003642 struct hlist_head *list;
Jens Axboeeac406c2019-11-14 12:09:58 -07003643
Jens Axboe78076bb2019-12-04 19:56:40 -07003644 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
3645 hlist_add_head(&req->hash_node, list);
Jens Axboeeac406c2019-11-14 12:09:58 -07003646}
3647
Jens Axboe3529d8c2019-12-19 18:24:38 -07003648static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003649{
3650 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003651 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003652
3653 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3654 return -EINVAL;
3655 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
3656 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06003657 if (!poll->file)
3658 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003659
Jens Axboe221c5eb2019-01-17 09:41:58 -07003660 events = READ_ONCE(sqe->poll_events);
3661 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
Jens Axboe0969e782019-12-17 18:40:57 -07003662 return 0;
3663}
3664
3665static int io_poll_add(struct io_kiocb *req, struct io_kiocb **nxt)
3666{
3667 struct io_poll_iocb *poll = &req->poll;
3668 struct io_ring_ctx *ctx = req->ctx;
3669 struct io_poll_table ipt;
3670 bool cancel = false;
3671 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07003672
3673 INIT_IO_WORK(&req->work, io_poll_complete_work);
Jens Axboe78076bb2019-12-04 19:56:40 -07003674 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003675
Jens Axboe221c5eb2019-01-17 09:41:58 -07003676 poll->head = NULL;
Jens Axboe8c838782019-03-12 15:48:16 -06003677 poll->done = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003678 poll->canceled = false;
3679
3680 ipt.pt._qproc = io_poll_queue_proc;
3681 ipt.pt._key = poll->events;
3682 ipt.req = req;
3683 ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
3684
3685 /* initialized the list so that we can do list_empty checks */
Jens Axboe392edb42019-12-09 17:52:20 -07003686 INIT_LIST_HEAD(&poll->wait.entry);
3687 init_waitqueue_func_entry(&poll->wait, io_poll_wake);
3688 poll->wait.private = poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003689
Jens Axboe36703242019-07-25 10:20:18 -06003690 INIT_LIST_HEAD(&req->list);
3691
Jens Axboe221c5eb2019-01-17 09:41:58 -07003692 mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003693
3694 spin_lock_irq(&ctx->completion_lock);
Jens Axboe8c838782019-03-12 15:48:16 -06003695 if (likely(poll->head)) {
3696 spin_lock(&poll->head->lock);
Jens Axboe392edb42019-12-09 17:52:20 -07003697 if (unlikely(list_empty(&poll->wait.entry))) {
Jens Axboe8c838782019-03-12 15:48:16 -06003698 if (ipt.error)
3699 cancel = true;
3700 ipt.error = 0;
3701 mask = 0;
3702 }
3703 if (mask || ipt.error)
Jens Axboe392edb42019-12-09 17:52:20 -07003704 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06003705 else if (cancel)
3706 WRITE_ONCE(poll->canceled, true);
3707 else if (!poll->done) /* actually waiting for an event */
Jens Axboeeac406c2019-11-14 12:09:58 -07003708 io_poll_req_insert(req);
Jens Axboe8c838782019-03-12 15:48:16 -06003709 spin_unlock(&poll->head->lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003710 }
Jens Axboe8c838782019-03-12 15:48:16 -06003711 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06003712 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003713 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06003714 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07003715 spin_unlock_irq(&ctx->completion_lock);
3716
Jens Axboe8c838782019-03-12 15:48:16 -06003717 if (mask) {
3718 io_cqring_ev_posted(ctx);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003719 io_put_req_find_next(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003720 }
Jens Axboe8c838782019-03-12 15:48:16 -06003721 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003722}
3723
Jens Axboe5262f562019-09-17 12:26:57 -06003724static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
3725{
Jens Axboead8a48a2019-11-15 08:49:11 -07003726 struct io_timeout_data *data = container_of(timer,
3727 struct io_timeout_data, timer);
3728 struct io_kiocb *req = data->req;
3729 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06003730 unsigned long flags;
3731
Jens Axboe5262f562019-09-17 12:26:57 -06003732 atomic_inc(&ctx->cq_timeouts);
3733
3734 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08003735 /*
Jens Axboe11365042019-10-16 09:08:32 -06003736 * We could be racing with timeout deletion. If the list is empty,
3737 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08003738 */
Jens Axboe842f9612019-10-29 12:34:10 -06003739 if (!list_empty(&req->list)) {
Jens Axboe11365042019-10-16 09:08:32 -06003740 struct io_kiocb *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06003741
Jens Axboe11365042019-10-16 09:08:32 -06003742 /*
3743 * Adjust the reqs sequence before the current one because it
Brian Gianforcarod195a662019-12-13 03:09:50 -08003744 * will consume a slot in the cq_ring and the cq_tail
Jens Axboe11365042019-10-16 09:08:32 -06003745 * pointer will be increased, otherwise other timeout reqs may
3746 * return in advance without waiting for enough wait_nr.
3747 */
3748 prev = req;
3749 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
3750 prev->sequence++;
Jens Axboe11365042019-10-16 09:08:32 -06003751 list_del_init(&req->list);
Jens Axboe11365042019-10-16 09:08:32 -06003752 }
Jens Axboe842f9612019-10-29 12:34:10 -06003753
Jens Axboe78e19bb2019-11-06 15:21:34 -07003754 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06003755 io_commit_cqring(ctx);
3756 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3757
3758 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003759 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06003760 io_put_req(req);
3761 return HRTIMER_NORESTART;
3762}
3763
Jens Axboe47f46762019-11-09 17:43:02 -07003764static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
3765{
3766 struct io_kiocb *req;
3767 int ret = -ENOENT;
3768
3769 list_for_each_entry(req, &ctx->timeout_list, list) {
3770 if (user_data == req->user_data) {
3771 list_del_init(&req->list);
3772 ret = 0;
3773 break;
3774 }
3775 }
3776
3777 if (ret == -ENOENT)
3778 return ret;
3779
Jens Axboe2d283902019-12-04 11:08:05 -07003780 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe47f46762019-11-09 17:43:02 -07003781 if (ret == -1)
3782 return -EALREADY;
3783
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003784 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07003785 io_cqring_fill_event(req, -ECANCELED);
3786 io_put_req(req);
3787 return 0;
3788}
3789
Jens Axboe3529d8c2019-12-19 18:24:38 -07003790static int io_timeout_remove_prep(struct io_kiocb *req,
3791 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07003792{
Jens Axboeb29472e2019-12-17 18:50:29 -07003793 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3794 return -EINVAL;
3795 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
3796 return -EINVAL;
3797
3798 req->timeout.addr = READ_ONCE(sqe->addr);
3799 req->timeout.flags = READ_ONCE(sqe->timeout_flags);
3800 if (req->timeout.flags)
3801 return -EINVAL;
3802
Jens Axboeb29472e2019-12-17 18:50:29 -07003803 return 0;
3804}
3805
Jens Axboe11365042019-10-16 09:08:32 -06003806/*
3807 * Remove or update an existing timeout command
3808 */
Jens Axboefc4df992019-12-10 14:38:45 -07003809static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06003810{
3811 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07003812 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06003813
Jens Axboe11365042019-10-16 09:08:32 -06003814 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb29472e2019-12-17 18:50:29 -07003815 ret = io_timeout_cancel(ctx, req->timeout.addr);
Jens Axboe11365042019-10-16 09:08:32 -06003816
Jens Axboe47f46762019-11-09 17:43:02 -07003817 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06003818 io_commit_cqring(ctx);
3819 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06003820 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003821 if (ret < 0)
3822 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003823 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06003824 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06003825}
3826
Jens Axboe3529d8c2019-12-19 18:24:38 -07003827static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07003828 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06003829{
Jens Axboead8a48a2019-11-15 08:49:11 -07003830 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06003831 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06003832
Jens Axboead8a48a2019-11-15 08:49:11 -07003833 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06003834 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07003835 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06003836 return -EINVAL;
Jens Axboe2d283902019-12-04 11:08:05 -07003837 if (sqe->off && is_timeout_link)
3838 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06003839 flags = READ_ONCE(sqe->timeout_flags);
3840 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06003841 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06003842
Jens Axboe26a61672019-12-20 09:02:01 -07003843 req->timeout.count = READ_ONCE(sqe->off);
3844
Jens Axboe3529d8c2019-12-19 18:24:38 -07003845 if (!req->io && io_alloc_async_ctx(req))
Jens Axboe26a61672019-12-20 09:02:01 -07003846 return -ENOMEM;
3847
3848 data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07003849 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07003850 req->flags |= REQ_F_TIMEOUT;
3851
3852 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06003853 return -EFAULT;
3854
Jens Axboe11365042019-10-16 09:08:32 -06003855 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07003856 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06003857 else
Jens Axboead8a48a2019-11-15 08:49:11 -07003858 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06003859
Jens Axboead8a48a2019-11-15 08:49:11 -07003860 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
3861 return 0;
3862}
3863
Jens Axboefc4df992019-12-10 14:38:45 -07003864static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07003865{
3866 unsigned count;
3867 struct io_ring_ctx *ctx = req->ctx;
3868 struct io_timeout_data *data;
3869 struct list_head *entry;
3870 unsigned span = 0;
Jens Axboead8a48a2019-11-15 08:49:11 -07003871
Jens Axboe2d283902019-12-04 11:08:05 -07003872 data = &req->io->timeout;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003873
Jens Axboe5262f562019-09-17 12:26:57 -06003874 /*
3875 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07003876 * timeout event to be satisfied. If it isn't set, then this is
3877 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06003878 */
Jens Axboe26a61672019-12-20 09:02:01 -07003879 count = req->timeout.count;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003880 if (!count) {
3881 req->flags |= REQ_F_TIMEOUT_NOSEQ;
3882 spin_lock_irq(&ctx->completion_lock);
3883 entry = ctx->timeout_list.prev;
3884 goto add;
3885 }
Jens Axboe5262f562019-09-17 12:26:57 -06003886
3887 req->sequence = ctx->cached_sq_head + count - 1;
Jens Axboe2d283902019-12-04 11:08:05 -07003888 data->seq_offset = count;
Jens Axboe5262f562019-09-17 12:26:57 -06003889
3890 /*
3891 * Insertion sort, ensuring the first entry in the list is always
3892 * the one we need first.
3893 */
Jens Axboe5262f562019-09-17 12:26:57 -06003894 spin_lock_irq(&ctx->completion_lock);
3895 list_for_each_prev(entry, &ctx->timeout_list) {
3896 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
yangerkun5da0fb12019-10-15 21:59:29 +08003897 unsigned nxt_sq_head;
3898 long long tmp, tmp_nxt;
Jens Axboe2d283902019-12-04 11:08:05 -07003899 u32 nxt_offset = nxt->io->timeout.seq_offset;
Jens Axboe5262f562019-09-17 12:26:57 -06003900
Jens Axboe93bd25b2019-11-11 23:34:31 -07003901 if (nxt->flags & REQ_F_TIMEOUT_NOSEQ)
3902 continue;
3903
yangerkun5da0fb12019-10-15 21:59:29 +08003904 /*
3905 * Since cached_sq_head + count - 1 can overflow, use type long
3906 * long to store it.
3907 */
3908 tmp = (long long)ctx->cached_sq_head + count - 1;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +03003909 nxt_sq_head = nxt->sequence - nxt_offset + 1;
3910 tmp_nxt = (long long)nxt_sq_head + nxt_offset - 1;
yangerkun5da0fb12019-10-15 21:59:29 +08003911
3912 /*
3913 * cached_sq_head may overflow, and it will never overflow twice
3914 * once there is some timeout req still be valid.
3915 */
3916 if (ctx->cached_sq_head < nxt_sq_head)
yangerkun8b07a652019-10-17 12:12:35 +08003917 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08003918
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003919 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06003920 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003921
3922 /*
3923 * Sequence of reqs after the insert one and itself should
3924 * be adjusted because each timeout req consumes a slot.
3925 */
3926 span++;
3927 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06003928 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003929 req->sequence -= span;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003930add:
Jens Axboe5262f562019-09-17 12:26:57 -06003931 list_add(&req->list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07003932 data->timer.function = io_timeout_fn;
3933 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06003934 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06003935 return 0;
3936}
3937
Jens Axboe62755e32019-10-28 21:49:21 -06003938static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06003939{
Jens Axboe62755e32019-10-28 21:49:21 -06003940 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06003941
Jens Axboe62755e32019-10-28 21:49:21 -06003942 return req->user_data == (unsigned long) data;
3943}
3944
Jens Axboee977d6d2019-11-05 12:39:45 -07003945static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06003946{
Jens Axboe62755e32019-10-28 21:49:21 -06003947 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06003948 int ret = 0;
3949
Jens Axboe62755e32019-10-28 21:49:21 -06003950 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
3951 switch (cancel_ret) {
3952 case IO_WQ_CANCEL_OK:
3953 ret = 0;
3954 break;
3955 case IO_WQ_CANCEL_RUNNING:
3956 ret = -EALREADY;
3957 break;
3958 case IO_WQ_CANCEL_NOTFOUND:
3959 ret = -ENOENT;
3960 break;
3961 }
3962
Jens Axboee977d6d2019-11-05 12:39:45 -07003963 return ret;
3964}
3965
Jens Axboe47f46762019-11-09 17:43:02 -07003966static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
3967 struct io_kiocb *req, __u64 sqe_addr,
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003968 struct io_kiocb **nxt, int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07003969{
3970 unsigned long flags;
3971 int ret;
3972
3973 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
3974 if (ret != -ENOENT) {
3975 spin_lock_irqsave(&ctx->completion_lock, flags);
3976 goto done;
3977 }
3978
3979 spin_lock_irqsave(&ctx->completion_lock, flags);
3980 ret = io_timeout_cancel(ctx, sqe_addr);
3981 if (ret != -ENOENT)
3982 goto done;
3983 ret = io_poll_cancel(ctx, sqe_addr);
3984done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003985 if (!ret)
3986 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07003987 io_cqring_fill_event(req, ret);
3988 io_commit_cqring(ctx);
3989 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3990 io_cqring_ev_posted(ctx);
3991
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003992 if (ret < 0)
3993 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07003994 io_put_req_find_next(req, nxt);
3995}
3996
Jens Axboe3529d8c2019-12-19 18:24:38 -07003997static int io_async_cancel_prep(struct io_kiocb *req,
3998 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07003999{
Jens Axboefbf23842019-12-17 18:45:56 -07004000 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07004001 return -EINVAL;
4002 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
4003 sqe->cancel_flags)
4004 return -EINVAL;
4005
Jens Axboefbf23842019-12-17 18:45:56 -07004006 req->cancel.addr = READ_ONCE(sqe->addr);
4007 return 0;
4008}
4009
4010static int io_async_cancel(struct io_kiocb *req, struct io_kiocb **nxt)
4011{
4012 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07004013
4014 io_async_find_and_cancel(ctx, req, req->cancel.addr, nxt, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06004015 return 0;
4016}
4017
Jens Axboe05f3fb32019-12-09 11:22:50 -07004018static int io_files_update_prep(struct io_kiocb *req,
4019 const struct io_uring_sqe *sqe)
4020{
4021 if (sqe->flags || sqe->ioprio || sqe->rw_flags)
4022 return -EINVAL;
4023
4024 req->files_update.offset = READ_ONCE(sqe->off);
4025 req->files_update.nr_args = READ_ONCE(sqe->len);
4026 if (!req->files_update.nr_args)
4027 return -EINVAL;
4028 req->files_update.arg = READ_ONCE(sqe->addr);
4029 return 0;
4030}
4031
4032static int io_files_update(struct io_kiocb *req, bool force_nonblock)
4033{
4034 struct io_ring_ctx *ctx = req->ctx;
4035 struct io_uring_files_update up;
4036 int ret;
4037
Jens Axboef86cd202020-01-29 13:46:44 -07004038 if (force_nonblock)
Jens Axboe05f3fb32019-12-09 11:22:50 -07004039 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004040
4041 up.offset = req->files_update.offset;
4042 up.fds = req->files_update.arg;
4043
4044 mutex_lock(&ctx->uring_lock);
4045 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
4046 mutex_unlock(&ctx->uring_lock);
4047
4048 if (ret < 0)
4049 req_set_fail_links(req);
4050 io_cqring_add_event(req, ret);
4051 io_put_req(req);
4052 return 0;
4053}
4054
Jens Axboe3529d8c2019-12-19 18:24:38 -07004055static int io_req_defer_prep(struct io_kiocb *req,
4056 const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07004057{
Jens Axboee7815732019-12-17 19:45:06 -07004058 ssize_t ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07004059
Jens Axboef86cd202020-01-29 13:46:44 -07004060 if (io_op_defs[req->opcode].file_table) {
4061 ret = io_grab_files(req);
4062 if (unlikely(ret))
4063 return ret;
4064 }
4065
Jens Axboecccf0ee2020-01-27 16:34:48 -07004066 io_req_work_grab_env(req, &io_op_defs[req->opcode]);
4067
Jens Axboed625c6e2019-12-17 19:53:05 -07004068 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07004069 case IORING_OP_NOP:
4070 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004071 case IORING_OP_READV:
4072 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004073 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004074 ret = io_read_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07004075 break;
4076 case IORING_OP_WRITEV:
4077 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004078 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004079 ret = io_write_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07004080 break;
Jens Axboe0969e782019-12-17 18:40:57 -07004081 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004082 ret = io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07004083 break;
4084 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004085 ret = io_poll_remove_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07004086 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004087 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004088 ret = io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004089 break;
4090 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004091 ret = io_prep_sfr(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004092 break;
Jens Axboe03b12302019-12-02 18:50:25 -07004093 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004094 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004095 ret = io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07004096 break;
4097 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004098 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004099 ret = io_recvmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07004100 break;
Jens Axboef499a022019-12-02 16:28:46 -07004101 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004102 ret = io_connect_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07004103 break;
Jens Axboe2d283902019-12-04 11:08:05 -07004104 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004105 ret = io_timeout_prep(req, sqe, false);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004106 break;
Jens Axboeb29472e2019-12-17 18:50:29 -07004107 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004108 ret = io_timeout_remove_prep(req, sqe);
Jens Axboeb29472e2019-12-17 18:50:29 -07004109 break;
Jens Axboefbf23842019-12-17 18:45:56 -07004110 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004111 ret = io_async_cancel_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07004112 break;
Jens Axboe2d283902019-12-04 11:08:05 -07004113 case IORING_OP_LINK_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004114 ret = io_timeout_prep(req, sqe, true);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004115 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004116 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004117 ret = io_accept_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004118 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07004119 case IORING_OP_FALLOCATE:
4120 ret = io_fallocate_prep(req, sqe);
4121 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004122 case IORING_OP_OPENAT:
4123 ret = io_openat_prep(req, sqe);
4124 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07004125 case IORING_OP_CLOSE:
4126 ret = io_close_prep(req, sqe);
4127 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004128 case IORING_OP_FILES_UPDATE:
4129 ret = io_files_update_prep(req, sqe);
4130 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004131 case IORING_OP_STATX:
4132 ret = io_statx_prep(req, sqe);
4133 break;
Jens Axboe4840e412019-12-25 22:03:45 -07004134 case IORING_OP_FADVISE:
4135 ret = io_fadvise_prep(req, sqe);
4136 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07004137 case IORING_OP_MADVISE:
4138 ret = io_madvise_prep(req, sqe);
4139 break;
Jens Axboecebdb982020-01-08 17:59:24 -07004140 case IORING_OP_OPENAT2:
4141 ret = io_openat2_prep(req, sqe);
4142 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004143 case IORING_OP_EPOLL_CTL:
4144 ret = io_epoll_ctl_prep(req, sqe);
4145 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004146 default:
Jens Axboee7815732019-12-17 19:45:06 -07004147 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
4148 req->opcode);
4149 ret = -EINVAL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004150 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004151 }
4152
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004153 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07004154}
4155
Jens Axboe3529d8c2019-12-19 18:24:38 -07004156static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboede0617e2019-04-06 21:51:27 -06004157{
Jackie Liua197f662019-11-08 08:09:12 -07004158 struct io_ring_ctx *ctx = req->ctx;
Jens Axboef67676d2019-12-02 11:03:47 -07004159 int ret;
Jens Axboede0617e2019-04-06 21:51:27 -06004160
Bob Liu9d858b22019-11-13 18:06:25 +08004161 /* Still need defer if there is pending req in defer list. */
4162 if (!req_need_defer(req) && list_empty(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06004163 return 0;
4164
Jens Axboe3529d8c2019-12-19 18:24:38 -07004165 if (!req->io && io_alloc_async_ctx(req))
Jens Axboede0617e2019-04-06 21:51:27 -06004166 return -EAGAIN;
4167
Jens Axboe3529d8c2019-12-19 18:24:38 -07004168 ret = io_req_defer_prep(req, sqe);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004169 if (ret < 0)
Jens Axboe2d283902019-12-04 11:08:05 -07004170 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07004171
Jens Axboede0617e2019-04-06 21:51:27 -06004172 spin_lock_irq(&ctx->completion_lock);
Bob Liu9d858b22019-11-13 18:06:25 +08004173 if (!req_need_defer(req) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06004174 spin_unlock_irq(&ctx->completion_lock);
Jens Axboede0617e2019-04-06 21:51:27 -06004175 return 0;
4176 }
4177
Jens Axboe915967f2019-11-21 09:01:20 -07004178 trace_io_uring_defer(ctx, req, req->user_data);
Jens Axboede0617e2019-04-06 21:51:27 -06004179 list_add_tail(&req->list, &ctx->defer_list);
4180 spin_unlock_irq(&ctx->completion_lock);
4181 return -EIOCBQUEUED;
4182}
4183
Jens Axboe3529d8c2019-12-19 18:24:38 -07004184static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
4185 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004186{
Jackie Liua197f662019-11-08 08:09:12 -07004187 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07004188 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004189
Jens Axboed625c6e2019-12-17 19:53:05 -07004190 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07004191 case IORING_OP_NOP:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004192 ret = io_nop(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004193 break;
4194 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004195 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004196 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004197 if (sqe) {
4198 ret = io_read_prep(req, sqe, force_nonblock);
4199 if (ret < 0)
4200 break;
4201 }
Pavel Begunkov267bc902019-11-07 01:41:08 +03004202 ret = io_read(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004203 break;
4204 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07004205 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004206 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004207 if (sqe) {
4208 ret = io_write_prep(req, sqe, force_nonblock);
4209 if (ret < 0)
4210 break;
4211 }
Pavel Begunkov267bc902019-11-07 01:41:08 +03004212 ret = io_write(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004213 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004214 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004215 if (sqe) {
4216 ret = io_prep_fsync(req, sqe);
4217 if (ret < 0)
4218 break;
4219 }
Jens Axboefc4df992019-12-10 14:38:45 -07004220 ret = io_fsync(req, nxt, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004221 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004222 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004223 if (sqe) {
4224 ret = io_poll_add_prep(req, sqe);
4225 if (ret)
4226 break;
4227 }
Jens Axboefc4df992019-12-10 14:38:45 -07004228 ret = io_poll_add(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004229 break;
4230 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004231 if (sqe) {
4232 ret = io_poll_remove_prep(req, sqe);
4233 if (ret < 0)
4234 break;
4235 }
Jens Axboefc4df992019-12-10 14:38:45 -07004236 ret = io_poll_remove(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004237 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004238 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004239 if (sqe) {
4240 ret = io_prep_sfr(req, sqe);
4241 if (ret < 0)
4242 break;
4243 }
Jens Axboefc4df992019-12-10 14:38:45 -07004244 ret = io_sync_file_range(req, nxt, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004245 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004246 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004247 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004248 if (sqe) {
4249 ret = io_sendmsg_prep(req, sqe);
4250 if (ret < 0)
4251 break;
4252 }
Jens Axboefddafac2020-01-04 20:19:44 -07004253 if (req->opcode == IORING_OP_SENDMSG)
4254 ret = io_sendmsg(req, nxt, force_nonblock);
4255 else
4256 ret = io_send(req, nxt, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004257 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06004258 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004259 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004260 if (sqe) {
4261 ret = io_recvmsg_prep(req, sqe);
4262 if (ret)
4263 break;
4264 }
Jens Axboefddafac2020-01-04 20:19:44 -07004265 if (req->opcode == IORING_OP_RECVMSG)
4266 ret = io_recvmsg(req, nxt, force_nonblock);
4267 else
4268 ret = io_recv(req, nxt, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06004269 break;
Jens Axboe5262f562019-09-17 12:26:57 -06004270 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004271 if (sqe) {
4272 ret = io_timeout_prep(req, sqe, false);
4273 if (ret)
4274 break;
4275 }
Jens Axboefc4df992019-12-10 14:38:45 -07004276 ret = io_timeout(req);
Jens Axboe5262f562019-09-17 12:26:57 -06004277 break;
Jens Axboe11365042019-10-16 09:08:32 -06004278 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004279 if (sqe) {
4280 ret = io_timeout_remove_prep(req, sqe);
4281 if (ret)
4282 break;
4283 }
Jens Axboefc4df992019-12-10 14:38:45 -07004284 ret = io_timeout_remove(req);
Jens Axboe11365042019-10-16 09:08:32 -06004285 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06004286 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004287 if (sqe) {
4288 ret = io_accept_prep(req, sqe);
4289 if (ret)
4290 break;
4291 }
Jens Axboefc4df992019-12-10 14:38:45 -07004292 ret = io_accept(req, nxt, force_nonblock);
Jens Axboe17f2fe32019-10-17 14:42:58 -06004293 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004294 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004295 if (sqe) {
4296 ret = io_connect_prep(req, sqe);
4297 if (ret)
4298 break;
4299 }
Jens Axboefc4df992019-12-10 14:38:45 -07004300 ret = io_connect(req, nxt, force_nonblock);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004301 break;
Jens Axboe62755e32019-10-28 21:49:21 -06004302 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004303 if (sqe) {
4304 ret = io_async_cancel_prep(req, sqe);
4305 if (ret)
4306 break;
4307 }
Jens Axboefc4df992019-12-10 14:38:45 -07004308 ret = io_async_cancel(req, nxt);
Jens Axboe62755e32019-10-28 21:49:21 -06004309 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07004310 case IORING_OP_FALLOCATE:
4311 if (sqe) {
4312 ret = io_fallocate_prep(req, sqe);
4313 if (ret)
4314 break;
4315 }
4316 ret = io_fallocate(req, nxt, force_nonblock);
4317 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004318 case IORING_OP_OPENAT:
4319 if (sqe) {
4320 ret = io_openat_prep(req, sqe);
4321 if (ret)
4322 break;
4323 }
4324 ret = io_openat(req, nxt, force_nonblock);
4325 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07004326 case IORING_OP_CLOSE:
4327 if (sqe) {
4328 ret = io_close_prep(req, sqe);
4329 if (ret)
4330 break;
4331 }
4332 ret = io_close(req, nxt, force_nonblock);
4333 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004334 case IORING_OP_FILES_UPDATE:
4335 if (sqe) {
4336 ret = io_files_update_prep(req, sqe);
4337 if (ret)
4338 break;
4339 }
4340 ret = io_files_update(req, force_nonblock);
4341 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004342 case IORING_OP_STATX:
4343 if (sqe) {
4344 ret = io_statx_prep(req, sqe);
4345 if (ret)
4346 break;
4347 }
4348 ret = io_statx(req, nxt, force_nonblock);
4349 break;
Jens Axboe4840e412019-12-25 22:03:45 -07004350 case IORING_OP_FADVISE:
4351 if (sqe) {
4352 ret = io_fadvise_prep(req, sqe);
4353 if (ret)
4354 break;
4355 }
4356 ret = io_fadvise(req, nxt, force_nonblock);
4357 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07004358 case IORING_OP_MADVISE:
4359 if (sqe) {
4360 ret = io_madvise_prep(req, sqe);
4361 if (ret)
4362 break;
4363 }
4364 ret = io_madvise(req, nxt, force_nonblock);
4365 break;
Jens Axboecebdb982020-01-08 17:59:24 -07004366 case IORING_OP_OPENAT2:
4367 if (sqe) {
4368 ret = io_openat2_prep(req, sqe);
4369 if (ret)
4370 break;
4371 }
4372 ret = io_openat2(req, nxt, force_nonblock);
4373 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004374 case IORING_OP_EPOLL_CTL:
4375 if (sqe) {
4376 ret = io_epoll_ctl_prep(req, sqe);
4377 if (ret)
4378 break;
4379 }
4380 ret = io_epoll_ctl(req, nxt, force_nonblock);
4381 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004382 default:
4383 ret = -EINVAL;
4384 break;
4385 }
4386
Jens Axboedef596e2019-01-09 08:59:42 -07004387 if (ret)
4388 return ret;
4389
4390 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe11ba8202020-01-15 21:51:17 -07004391 const bool in_async = io_wq_current_is_worker();
4392
Jens Axboe9e645e112019-05-10 16:07:28 -06004393 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07004394 return -EAGAIN;
4395
Jens Axboe11ba8202020-01-15 21:51:17 -07004396 /* workqueue context doesn't hold uring_lock, grab it now */
4397 if (in_async)
4398 mutex_lock(&ctx->uring_lock);
4399
Jens Axboedef596e2019-01-09 08:59:42 -07004400 io_iopoll_req_issued(req);
Jens Axboe11ba8202020-01-15 21:51:17 -07004401
4402 if (in_async)
4403 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07004404 }
4405
4406 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004407}
4408
Jens Axboe561fb042019-10-24 07:25:42 -06004409static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboe31b51512019-01-18 22:56:34 -07004410{
Jens Axboe561fb042019-10-24 07:25:42 -06004411 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004412 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe561fb042019-10-24 07:25:42 -06004413 struct io_kiocb *nxt = NULL;
4414 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004415
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07004416 /* if NO_CANCEL is set, we must still run the work */
4417 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
4418 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06004419 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07004420 }
Jens Axboe31b51512019-01-18 22:56:34 -07004421
Jens Axboe561fb042019-10-24 07:25:42 -06004422 if (!ret) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004423 req->in_async = true;
Jens Axboe561fb042019-10-24 07:25:42 -06004424 do {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004425 ret = io_issue_sqe(req, NULL, &nxt, false);
Jens Axboe561fb042019-10-24 07:25:42 -06004426 /*
4427 * We can get EAGAIN for polled IO even though we're
4428 * forcing a sync submission from here, since we can't
4429 * wait for request slots on the block side.
4430 */
4431 if (ret != -EAGAIN)
4432 break;
4433 cond_resched();
4434 } while (1);
4435 }
Jens Axboe31b51512019-01-18 22:56:34 -07004436
Jens Axboe561fb042019-10-24 07:25:42 -06004437 /* drop submission reference */
Jackie Liuec9c02a2019-11-08 23:50:36 +08004438 io_put_req(req);
Jens Axboe817869d2019-04-30 14:44:05 -06004439
Jens Axboe561fb042019-10-24 07:25:42 -06004440 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004441 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07004442 io_cqring_add_event(req, ret);
Jens Axboe817869d2019-04-30 14:44:05 -06004443 io_put_req(req);
Jens Axboeedafcce2019-01-09 09:16:05 -07004444 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004445
Jens Axboe561fb042019-10-24 07:25:42 -06004446 /* if a dependent link is ready, pass it back */
Jens Axboe78912932020-01-14 22:09:06 -07004447 if (!ret && nxt)
4448 io_wq_assign_next(workptr, nxt);
Jens Axboe31b51512019-01-18 22:56:34 -07004449}
Jens Axboe2b188cc2019-01-07 10:46:33 -07004450
Jens Axboe15b71ab2019-12-11 11:20:36 -07004451static int io_req_needs_file(struct io_kiocb *req, int fd)
Jens Axboe9e3aa612019-12-11 15:55:43 -07004452{
Jens Axboed3656342019-12-18 09:50:26 -07004453 if (!io_op_defs[req->opcode].needs_file)
Jens Axboe9e3aa612019-12-11 15:55:43 -07004454 return 0;
Jens Axboed3656342019-12-18 09:50:26 -07004455 if (fd == -1 && io_op_defs[req->opcode].fd_non_neg)
4456 return 0;
4457 return 1;
Jens Axboe09bb8392019-03-13 12:39:28 -06004458}
4459
Jens Axboe65e19f52019-10-26 07:20:21 -06004460static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
4461 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06004462{
Jens Axboe65e19f52019-10-26 07:20:21 -06004463 struct fixed_file_table *table;
4464
Jens Axboe05f3fb32019-12-09 11:22:50 -07004465 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
4466 return table->files[index & IORING_FILE_TABLE_MASK];;
Jens Axboe65e19f52019-10-26 07:20:21 -06004467}
4468
Jens Axboe3529d8c2019-12-19 18:24:38 -07004469static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
4470 const struct io_uring_sqe *sqe)
Jens Axboe09bb8392019-03-13 12:39:28 -06004471{
Jackie Liua197f662019-11-08 08:09:12 -07004472 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe09bb8392019-03-13 12:39:28 -06004473 unsigned flags;
Jens Axboed3656342019-12-18 09:50:26 -07004474 int fd;
Jens Axboe09bb8392019-03-13 12:39:28 -06004475
Jens Axboe3529d8c2019-12-19 18:24:38 -07004476 flags = READ_ONCE(sqe->flags);
4477 fd = READ_ONCE(sqe->fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06004478
Jens Axboed3656342019-12-18 09:50:26 -07004479 if (!io_req_needs_file(req, fd))
4480 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06004481
4482 if (flags & IOSQE_FIXED_FILE) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07004483 if (unlikely(!ctx->file_data ||
Jens Axboe09bb8392019-03-13 12:39:28 -06004484 (unsigned) fd >= ctx->nr_user_files))
4485 return -EBADF;
Jens Axboeb7620122019-10-26 07:22:55 -06004486 fd = array_index_nospec(fd, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06004487 req->file = io_file_from_index(ctx, fd);
4488 if (!req->file)
Jens Axboe08a45172019-10-03 08:11:03 -06004489 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06004490 req->flags |= REQ_F_FIXED_FILE;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004491 percpu_ref_get(&ctx->file_data->refs);
Jens Axboe09bb8392019-03-13 12:39:28 -06004492 } else {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004493 if (req->needs_fixed_file)
Jens Axboe09bb8392019-03-13 12:39:28 -06004494 return -EBADF;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004495 trace_io_uring_file_get(ctx, fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06004496 req->file = io_file_get(state, fd);
4497 if (unlikely(!req->file))
4498 return -EBADF;
4499 }
4500
4501 return 0;
4502}
4503
Jackie Liua197f662019-11-08 08:09:12 -07004504static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004505{
Jens Axboefcb323c2019-10-24 12:39:47 -06004506 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07004507 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06004508
Jens Axboef86cd202020-01-29 13:46:44 -07004509 if (req->work.files)
4510 return 0;
Pavel Begunkovb14cca02020-01-17 04:45:59 +03004511 if (!ctx->ring_file)
Jens Axboeb5dba592019-12-11 14:02:38 -07004512 return -EBADF;
4513
Jens Axboefcb323c2019-10-24 12:39:47 -06004514 rcu_read_lock();
4515 spin_lock_irq(&ctx->inflight_lock);
4516 /*
4517 * We use the f_ops->flush() handler to ensure that we can flush
4518 * out work accessing these files if the fd is closed. Check if
4519 * the fd has changed since we started down this path, and disallow
4520 * this operation if it has.
4521 */
Pavel Begunkovb14cca02020-01-17 04:45:59 +03004522 if (fcheck(ctx->ring_fd) == ctx->ring_file) {
Jens Axboefcb323c2019-10-24 12:39:47 -06004523 list_add(&req->inflight_entry, &ctx->inflight_list);
4524 req->flags |= REQ_F_INFLIGHT;
4525 req->work.files = current->files;
4526 ret = 0;
4527 }
4528 spin_unlock_irq(&ctx->inflight_lock);
4529 rcu_read_unlock();
4530
4531 return ret;
4532}
4533
Jens Axboe2665abf2019-11-05 12:40:47 -07004534static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
4535{
Jens Axboead8a48a2019-11-15 08:49:11 -07004536 struct io_timeout_data *data = container_of(timer,
4537 struct io_timeout_data, timer);
4538 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07004539 struct io_ring_ctx *ctx = req->ctx;
4540 struct io_kiocb *prev = NULL;
4541 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07004542
4543 spin_lock_irqsave(&ctx->completion_lock, flags);
4544
4545 /*
4546 * We don't expect the list to be empty, that will only happen if we
4547 * race with the completion of the linked work.
4548 */
Pavel Begunkov44932332019-12-05 16:16:35 +03004549 if (!list_empty(&req->link_list)) {
4550 prev = list_entry(req->link_list.prev, struct io_kiocb,
4551 link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07004552 if (refcount_inc_not_zero(&prev->refs)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03004553 list_del_init(&req->link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07004554 prev->flags &= ~REQ_F_LINK_TIMEOUT;
4555 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07004556 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07004557 }
4558
4559 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4560
4561 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004562 req_set_fail_links(prev);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004563 io_async_find_and_cancel(ctx, req, prev->user_data, NULL,
4564 -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07004565 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07004566 } else {
4567 io_cqring_add_event(req, -ETIME);
4568 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07004569 }
Jens Axboe2665abf2019-11-05 12:40:47 -07004570 return HRTIMER_NORESTART;
4571}
4572
Jens Axboead8a48a2019-11-15 08:49:11 -07004573static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07004574{
Jens Axboe76a46e02019-11-10 23:34:16 -07004575 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07004576
Jens Axboe76a46e02019-11-10 23:34:16 -07004577 /*
4578 * If the list is now empty, then our linked request finished before
4579 * we got a chance to setup the timer
4580 */
4581 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov44932332019-12-05 16:16:35 +03004582 if (!list_empty(&req->link_list)) {
Jens Axboe2d283902019-12-04 11:08:05 -07004583 struct io_timeout_data *data = &req->io->timeout;
Jens Axboe94ae5e72019-11-14 19:39:52 -07004584
Jens Axboead8a48a2019-11-15 08:49:11 -07004585 data->timer.function = io_link_timeout_fn;
4586 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
4587 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07004588 }
Jens Axboe76a46e02019-11-10 23:34:16 -07004589 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07004590
Jens Axboe2665abf2019-11-05 12:40:47 -07004591 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07004592 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07004593}
4594
Jens Axboead8a48a2019-11-15 08:49:11 -07004595static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07004596{
4597 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004598
Jens Axboe2665abf2019-11-05 12:40:47 -07004599 if (!(req->flags & REQ_F_LINK))
4600 return NULL;
4601
Pavel Begunkov44932332019-12-05 16:16:35 +03004602 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
4603 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07004604 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07004605 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07004606
Jens Axboe76a46e02019-11-10 23:34:16 -07004607 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07004608 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07004609}
4610
Jens Axboe3529d8c2019-12-19 18:24:38 -07004611static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004612{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004613 struct io_kiocb *linked_timeout;
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004614 struct io_kiocb *nxt = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004615 int ret;
4616
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004617again:
4618 linked_timeout = io_prep_linked_timeout(req);
4619
Jens Axboe3529d8c2019-12-19 18:24:38 -07004620 ret = io_issue_sqe(req, sqe, &nxt, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06004621
4622 /*
4623 * We async punt it if the file wasn't marked NOWAIT, or if the file
4624 * doesn't support non-blocking read/write attempts
4625 */
4626 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
4627 (req->flags & REQ_F_MUST_PUNT))) {
Pavel Begunkov86a761f2020-01-22 23:09:36 +03004628punt:
Jens Axboef86cd202020-01-29 13:46:44 -07004629 if (io_op_defs[req->opcode].file_table) {
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03004630 ret = io_grab_files(req);
4631 if (ret)
4632 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004633 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03004634
4635 /*
4636 * Queued up for async execution, worker will release
4637 * submit reference when the iocb is actually submitted.
4638 */
4639 io_queue_async_work(req);
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004640 goto done_req;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004641 }
Jens Axboee65ef562019-03-12 10:16:44 -06004642
Jens Axboefcb323c2019-10-24 12:39:47 -06004643err:
Jens Axboee65ef562019-03-12 10:16:44 -06004644 /* drop submission reference */
4645 io_put_req(req);
4646
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004647 if (linked_timeout) {
Jens Axboe76a46e02019-11-10 23:34:16 -07004648 if (!ret)
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004649 io_queue_linked_timeout(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07004650 else
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004651 io_put_req(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07004652 }
4653
Jens Axboee65ef562019-03-12 10:16:44 -06004654 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06004655 if (ret) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07004656 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004657 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06004658 io_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06004659 }
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004660done_req:
4661 if (nxt) {
4662 req = nxt;
4663 nxt = NULL;
Pavel Begunkov86a761f2020-01-22 23:09:36 +03004664
4665 if (req->flags & REQ_F_FORCE_ASYNC)
4666 goto punt;
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004667 goto again;
4668 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004669}
4670
Jens Axboe3529d8c2019-12-19 18:24:38 -07004671static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jackie Liu4fe2c962019-09-09 20:50:40 +08004672{
4673 int ret;
4674
Jens Axboe3529d8c2019-12-19 18:24:38 -07004675 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004676 if (ret) {
4677 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03004678fail_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004679 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004680 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07004681 io_double_put_req(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004682 }
Pavel Begunkov25508782019-12-30 21:24:47 +03004683 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Pavel Begunkov11185912020-01-22 23:09:35 +03004684 ret = io_req_defer_prep(req, sqe);
4685 if (unlikely(ret < 0))
4686 goto fail_req;
Jens Axboece35a472019-12-17 08:04:44 -07004687 /*
4688 * Never try inline submit of IOSQE_ASYNC is set, go straight
4689 * to async execution.
4690 */
4691 req->work.flags |= IO_WQ_WORK_CONCURRENT;
4692 io_queue_async_work(req);
4693 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004694 __io_queue_sqe(req, sqe);
Jens Axboece35a472019-12-17 08:04:44 -07004695 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08004696}
4697
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004698static inline void io_queue_link_head(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08004699{
Jens Axboe94ae5e72019-11-14 19:39:52 -07004700 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004701 io_cqring_add_event(req, -ECANCELED);
4702 io_double_put_req(req);
4703 } else
Jens Axboe3529d8c2019-12-19 18:24:38 -07004704 io_queue_sqe(req, NULL);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004705}
4706
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004707#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
Jens Axboece35a472019-12-17 08:04:44 -07004708 IOSQE_IO_HARDLINK | IOSQE_ASYNC)
Jens Axboe9e645e112019-05-10 16:07:28 -06004709
Jens Axboe3529d8c2019-12-19 18:24:38 -07004710static bool io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
4711 struct io_submit_state *state, struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06004712{
Jens Axboe75c6a032020-01-28 10:15:23 -07004713 const struct cred *old_creds = NULL;
Jackie Liua197f662019-11-08 08:09:12 -07004714 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004715 unsigned int sqe_flags;
Jens Axboe75c6a032020-01-28 10:15:23 -07004716 int ret, id;
Jens Axboe9e645e112019-05-10 16:07:28 -06004717
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004718 sqe_flags = READ_ONCE(sqe->flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06004719
4720 /* enforce forwards compatibility on users */
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004721 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06004722 ret = -EINVAL;
Pavel Begunkov196be952019-11-07 01:41:06 +03004723 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06004724 }
4725
Jens Axboe75c6a032020-01-28 10:15:23 -07004726 id = READ_ONCE(sqe->personality);
4727 if (id) {
4728 const struct cred *personality_creds;
4729
4730 personality_creds = idr_find(&ctx->personality_idr, id);
4731 if (unlikely(!personality_creds)) {
4732 ret = -EINVAL;
4733 goto err_req;
4734 }
4735 old_creds = override_creds(personality_creds);
4736 }
4737
Pavel Begunkov6b47ee62020-01-18 20:22:41 +03004738 /* same numerical values with corresponding REQ_F_*, safe to copy */
4739 req->flags |= sqe_flags & (IOSQE_IO_DRAIN|IOSQE_IO_HARDLINK|
4740 IOSQE_ASYNC);
Jens Axboe9e645e112019-05-10 16:07:28 -06004741
Jens Axboe3529d8c2019-12-19 18:24:38 -07004742 ret = io_req_set_file(state, req, sqe);
Jens Axboe9e645e112019-05-10 16:07:28 -06004743 if (unlikely(ret)) {
4744err_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004745 io_cqring_add_event(req, ret);
4746 io_double_put_req(req);
Jens Axboe75c6a032020-01-28 10:15:23 -07004747 if (old_creds)
4748 revert_creds(old_creds);
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004749 return false;
Jens Axboe9e645e112019-05-10 16:07:28 -06004750 }
4751
Jens Axboe9e645e112019-05-10 16:07:28 -06004752 /*
4753 * If we already have a head request, queue this one for async
4754 * submittal once the head completes. If we don't have a head but
4755 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
4756 * submitted sync once the chain is complete. If none of those
4757 * conditions are true (normal request), then just queue it.
4758 */
4759 if (*link) {
Pavel Begunkov9d763772019-12-17 02:22:07 +03004760 struct io_kiocb *head = *link;
Jens Axboe9e645e112019-05-10 16:07:28 -06004761
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03004762 /*
4763 * Taking sequential execution of a link, draining both sides
4764 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
4765 * requests in the link. So, it drains the head and the
4766 * next after the link request. The last one is done via
4767 * drain_next flag to persist the effect across calls.
4768 */
Pavel Begunkov711be032020-01-17 03:57:59 +03004769 if (sqe_flags & IOSQE_IO_DRAIN) {
4770 head->flags |= REQ_F_IO_DRAIN;
4771 ctx->drain_next = 1;
4772 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004773 if (io_alloc_async_ctx(req)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06004774 ret = -EAGAIN;
4775 goto err_req;
4776 }
4777
Jens Axboe3529d8c2019-12-19 18:24:38 -07004778 ret = io_req_defer_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07004779 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004780 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03004781 head->flags |= REQ_F_FAIL_LINK;
Jens Axboef67676d2019-12-02 11:03:47 -07004782 goto err_req;
Jens Axboe2d283902019-12-04 11:08:05 -07004783 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03004784 trace_io_uring_link(ctx, req, head);
4785 list_add_tail(&req->link_list, &head->link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06004786
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004787 /* last request of a link, enqueue the link */
4788 if (!(sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK))) {
4789 io_queue_link_head(head);
4790 *link = NULL;
4791 }
Jens Axboe9e645e112019-05-10 16:07:28 -06004792 } else {
Pavel Begunkov711be032020-01-17 03:57:59 +03004793 if (unlikely(ctx->drain_next)) {
4794 req->flags |= REQ_F_IO_DRAIN;
4795 req->ctx->drain_next = 0;
4796 }
4797 if (sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK)) {
4798 req->flags |= REQ_F_LINK;
Pavel Begunkov711be032020-01-17 03:57:59 +03004799 INIT_LIST_HEAD(&req->link_list);
4800 ret = io_req_defer_prep(req, sqe);
4801 if (ret)
4802 req->flags |= REQ_F_FAIL_LINK;
4803 *link = req;
4804 } else {
4805 io_queue_sqe(req, sqe);
4806 }
Jens Axboe9e645e112019-05-10 16:07:28 -06004807 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004808
Jens Axboe75c6a032020-01-28 10:15:23 -07004809 if (old_creds)
4810 revert_creds(old_creds);
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004811 return true;
Jens Axboe9e645e112019-05-10 16:07:28 -06004812}
4813
Jens Axboe9a56a232019-01-09 09:06:50 -07004814/*
4815 * Batched submission is done, ensure local IO is flushed out.
4816 */
4817static void io_submit_state_end(struct io_submit_state *state)
4818{
4819 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06004820 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07004821 if (state->free_reqs)
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03004822 kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs);
Jens Axboe9a56a232019-01-09 09:06:50 -07004823}
4824
4825/*
4826 * Start submission side cache.
4827 */
4828static void io_submit_state_start(struct io_submit_state *state,
Jackie Liu22efde52019-12-02 17:14:52 +08004829 unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07004830{
4831 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07004832 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07004833 state->file = NULL;
4834 state->ios_left = max_ios;
4835}
4836
Jens Axboe2b188cc2019-01-07 10:46:33 -07004837static void io_commit_sqring(struct io_ring_ctx *ctx)
4838{
Hristo Venev75b28af2019-08-26 17:23:46 +00004839 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004840
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03004841 /*
4842 * Ensure any loads from the SQEs are done at this point,
4843 * since once we write the new head, the application could
4844 * write new data to them.
4845 */
4846 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004847}
4848
4849/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07004850 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07004851 * that is mapped by userspace. This means that care needs to be taken to
4852 * ensure that reads are stable, as we cannot rely on userspace always
4853 * being a good citizen. If members of the sqe are validated and then later
4854 * used, it's important that those reads are done through READ_ONCE() to
4855 * prevent a re-load down the line.
4856 */
Jens Axboe3529d8c2019-12-19 18:24:38 -07004857static bool io_get_sqring(struct io_ring_ctx *ctx, struct io_kiocb *req,
4858 const struct io_uring_sqe **sqe_ptr)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004859{
Hristo Venev75b28af2019-08-26 17:23:46 +00004860 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004861 unsigned head;
4862
4863 /*
4864 * The cached sq head (or cq tail) serves two purposes:
4865 *
4866 * 1) allows us to batch the cost of updating the user visible
4867 * head updates.
4868 * 2) allows the kernel side to track the head on its own, even
4869 * though the application is the one updating it.
4870 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03004871 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03004872 if (likely(head < ctx->sq_entries)) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004873 /*
4874 * All io need record the previous position, if LINK vs DARIN,
4875 * it can be used to mark the position of the first IO in the
4876 * link list.
4877 */
4878 req->sequence = ctx->cached_sq_head;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004879 *sqe_ptr = &ctx->sq_sqes[head];
4880 req->opcode = READ_ONCE((*sqe_ptr)->opcode);
4881 req->user_data = READ_ONCE((*sqe_ptr)->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004882 ctx->cached_sq_head++;
4883 return true;
4884 }
4885
4886 /* drop invalid entries */
4887 ctx->cached_sq_head++;
Jens Axboe498ccd92019-10-25 10:04:25 -06004888 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03004889 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004890 return false;
4891}
4892
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004893static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004894 struct file *ring_file, int ring_fd,
4895 struct mm_struct **mm, bool async)
Jens Axboe6c271ce2019-01-10 11:22:30 -07004896{
4897 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06004898 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06004899 int i, submitted = 0;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03004900 bool mm_fault = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004901
Jens Axboec4a2ed72019-11-21 21:01:26 -07004902 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07004903 if (test_bit(0, &ctx->sq_check_overflow)) {
4904 if (!list_empty(&ctx->cq_overflow_list) &&
4905 !io_cqring_overflow_flush(ctx, false))
4906 return -EBUSY;
4907 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07004908
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03004909 /* make sure SQ entry isn't read before tail */
4910 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03004911
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03004912 if (!percpu_ref_tryget_many(&ctx->refs, nr))
4913 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004914
4915 if (nr > IO_PLUG_THRESHOLD) {
Jackie Liu22efde52019-12-02 17:14:52 +08004916 io_submit_state_start(&state, nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004917 statep = &state;
4918 }
4919
Pavel Begunkovb14cca02020-01-17 04:45:59 +03004920 ctx->ring_fd = ring_fd;
4921 ctx->ring_file = ring_file;
4922
Jens Axboe6c271ce2019-01-10 11:22:30 -07004923 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004924 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03004925 struct io_kiocb *req;
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03004926 int err;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004927
Pavel Begunkov196be952019-11-07 01:41:06 +03004928 req = io_get_req(ctx, statep);
4929 if (unlikely(!req)) {
4930 if (!submitted)
4931 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004932 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06004933 }
Jens Axboe3529d8c2019-12-19 18:24:38 -07004934 if (!io_get_sqring(ctx, req, &sqe)) {
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03004935 __io_req_do_free(req);
Pavel Begunkov196be952019-11-07 01:41:06 +03004936 break;
4937 }
Jens Axboe9e645e112019-05-10 16:07:28 -06004938
Jens Axboed3656342019-12-18 09:50:26 -07004939 /* will complete beyond this point, count as submitted */
4940 submitted++;
4941
4942 if (unlikely(req->opcode >= IORING_OP_LAST)) {
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03004943 err = -EINVAL;
4944fail_req:
4945 io_cqring_add_event(req, err);
Jens Axboed3656342019-12-18 09:50:26 -07004946 io_double_put_req(req);
4947 break;
4948 }
4949
4950 if (io_op_defs[req->opcode].needs_mm && !*mm) {
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03004951 mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm);
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03004952 if (unlikely(mm_fault)) {
4953 err = -EFAULT;
4954 goto fail_req;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03004955 }
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03004956 use_mm(ctx->sqo_mm);
4957 *mm = ctx->sqo_mm;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03004958 }
4959
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004960 req->in_async = async;
4961 req->needs_fixed_file = async;
Jens Axboe354420f2020-01-08 18:55:15 -07004962 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
4963 true, async);
Jens Axboe3529d8c2019-12-19 18:24:38 -07004964 if (!io_submit_sqe(req, sqe, statep, &link))
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004965 break;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004966 }
4967
Pavel Begunkov9466f432020-01-25 22:34:01 +03004968 if (unlikely(submitted != nr)) {
4969 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
4970
4971 percpu_ref_put_many(&ctx->refs, nr - ref_used);
4972 }
Jens Axboe9e645e112019-05-10 16:07:28 -06004973 if (link)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004974 io_queue_link_head(link);
Jens Axboe6c271ce2019-01-10 11:22:30 -07004975 if (statep)
4976 io_submit_state_end(&state);
4977
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004978 /* Commit SQ ring head once we've consumed and submitted all SQEs */
4979 io_commit_sqring(ctx);
4980
Jens Axboe6c271ce2019-01-10 11:22:30 -07004981 return submitted;
4982}
4983
4984static int io_sq_thread(void *data)
4985{
Jens Axboe6c271ce2019-01-10 11:22:30 -07004986 struct io_ring_ctx *ctx = data;
4987 struct mm_struct *cur_mm = NULL;
Jens Axboe181e4482019-11-25 08:52:30 -07004988 const struct cred *old_cred;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004989 mm_segment_t old_fs;
4990 DEFINE_WAIT(wait);
4991 unsigned inflight;
4992 unsigned long timeout;
Jens Axboec1edbf52019-11-10 16:56:04 -07004993 int ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004994
Jens Axboe206aefd2019-11-07 18:27:42 -07004995 complete(&ctx->completions[1]);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08004996
Jens Axboe6c271ce2019-01-10 11:22:30 -07004997 old_fs = get_fs();
4998 set_fs(USER_DS);
Jens Axboe181e4482019-11-25 08:52:30 -07004999 old_cred = override_creds(ctx->creds);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005000
Jens Axboec1edbf52019-11-10 16:56:04 -07005001 ret = timeout = inflight = 0;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005002 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005003 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005004
5005 if (inflight) {
5006 unsigned nr_events = 0;
5007
5008 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe2b2ed972019-10-25 10:06:15 -06005009 /*
5010 * inflight is the count of the maximum possible
5011 * entries we submitted, but it can be smaller
5012 * if we dropped some of them. If we don't have
5013 * poll entries available, then we know that we
5014 * have nothing left to poll for. Reset the
5015 * inflight count to zero in that case.
5016 */
5017 mutex_lock(&ctx->uring_lock);
5018 if (!list_empty(&ctx->poll_list))
5019 __io_iopoll_check(ctx, &nr_events, 0);
5020 else
5021 inflight = 0;
5022 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005023 } else {
5024 /*
5025 * Normal IO, just pretend everything completed.
5026 * We don't have to poll completions for that.
5027 */
5028 nr_events = inflight;
5029 }
5030
5031 inflight -= nr_events;
5032 if (!inflight)
5033 timeout = jiffies + ctx->sq_thread_idle;
5034 }
5035
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005036 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07005037
5038 /*
5039 * If submit got -EBUSY, flag us as needing the application
5040 * to enter the kernel to reap and flush events.
5041 */
5042 if (!to_submit || ret == -EBUSY) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07005043 /*
5044 * We're polling. If we're within the defined idle
5045 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07005046 * to sleep. The exception is if we got EBUSY doing
5047 * more IO, we should wait for the application to
5048 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07005049 */
Jens Axboec1edbf52019-11-10 16:56:04 -07005050 if (inflight ||
Jens Axboedf069d82020-02-04 16:48:34 -07005051 (!time_after(jiffies, timeout) && ret != -EBUSY &&
5052 !percpu_ref_is_dying(&ctx->refs))) {
Jens Axboe9831a902019-09-19 09:48:55 -06005053 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07005054 continue;
5055 }
5056
5057 /*
5058 * Drop cur_mm before scheduling, we can't hold it for
5059 * long periods (or over schedule()). Do this before
5060 * adding ourselves to the waitqueue, as the unuse/drop
5061 * may sleep.
5062 */
5063 if (cur_mm) {
5064 unuse_mm(cur_mm);
5065 mmput(cur_mm);
5066 cur_mm = NULL;
5067 }
5068
5069 prepare_to_wait(&ctx->sqo_wait, &wait,
5070 TASK_INTERRUPTIBLE);
5071
5072 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00005073 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02005074 /* make sure to read SQ tail after writing flags */
5075 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07005076
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005077 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07005078 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005079 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07005080 finish_wait(&ctx->sqo_wait, &wait);
5081 break;
5082 }
5083 if (signal_pending(current))
5084 flush_signals(current);
5085 schedule();
5086 finish_wait(&ctx->sqo_wait, &wait);
5087
Hristo Venev75b28af2019-08-26 17:23:46 +00005088 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005089 continue;
5090 }
5091 finish_wait(&ctx->sqo_wait, &wait);
5092
Hristo Venev75b28af2019-08-26 17:23:46 +00005093 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005094 }
5095
Jens Axboe8a4955f2019-12-09 14:52:35 -07005096 mutex_lock(&ctx->uring_lock);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005097 ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true);
Jens Axboe8a4955f2019-12-09 14:52:35 -07005098 mutex_unlock(&ctx->uring_lock);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005099 if (ret > 0)
5100 inflight += ret;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005101 }
5102
5103 set_fs(old_fs);
5104 if (cur_mm) {
5105 unuse_mm(cur_mm);
5106 mmput(cur_mm);
5107 }
Jens Axboe181e4482019-11-25 08:52:30 -07005108 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06005109
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005110 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06005111
Jens Axboe6c271ce2019-01-10 11:22:30 -07005112 return 0;
5113}
5114
Jens Axboebda52162019-09-24 13:47:15 -06005115struct io_wait_queue {
5116 struct wait_queue_entry wq;
5117 struct io_ring_ctx *ctx;
5118 unsigned to_wait;
5119 unsigned nr_timeouts;
5120};
5121
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005122static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06005123{
5124 struct io_ring_ctx *ctx = iowq->ctx;
5125
5126 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08005127 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06005128 * started waiting. For timeouts, we always want to return to userspace,
5129 * regardless of event count.
5130 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005131 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06005132 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
5133}
5134
5135static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
5136 int wake_flags, void *key)
5137{
5138 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
5139 wq);
5140
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005141 /* use noflush == true, as we can't safely rely on locking context */
5142 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06005143 return -1;
5144
5145 return autoremove_wake_function(curr, mode, wake_flags, key);
5146}
5147
Jens Axboe2b188cc2019-01-07 10:46:33 -07005148/*
5149 * Wait until events become available, if we don't already have some. The
5150 * application must reap them itself, as they reside on the shared cq ring.
5151 */
5152static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
5153 const sigset_t __user *sig, size_t sigsz)
5154{
Jens Axboebda52162019-09-24 13:47:15 -06005155 struct io_wait_queue iowq = {
5156 .wq = {
5157 .private = current,
5158 .func = io_wake_function,
5159 .entry = LIST_HEAD_INIT(iowq.wq.entry),
5160 },
5161 .ctx = ctx,
5162 .to_wait = min_events,
5163 };
Hristo Venev75b28af2019-08-26 17:23:46 +00005164 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005165 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005166
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005167 if (io_cqring_events(ctx, false) >= min_events)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005168 return 0;
5169
5170 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005171#ifdef CONFIG_COMPAT
5172 if (in_compat_syscall())
5173 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07005174 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005175 else
5176#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07005177 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005178
Jens Axboe2b188cc2019-01-07 10:46:33 -07005179 if (ret)
5180 return ret;
5181 }
5182
Jens Axboebda52162019-09-24 13:47:15 -06005183 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02005184 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06005185 do {
5186 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
5187 TASK_INTERRUPTIBLE);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005188 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06005189 break;
5190 schedule();
5191 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005192 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06005193 break;
5194 }
5195 } while (1);
5196 finish_wait(&ctx->wait, &iowq.wq);
5197
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005198 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005199
Hristo Venev75b28af2019-08-26 17:23:46 +00005200 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005201}
5202
Jens Axboe6b063142019-01-10 22:13:58 -07005203static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
5204{
5205#if defined(CONFIG_UNIX)
5206 if (ctx->ring_sock) {
5207 struct sock *sock = ctx->ring_sock->sk;
5208 struct sk_buff *skb;
5209
5210 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
5211 kfree_skb(skb);
5212 }
5213#else
5214 int i;
5215
Jens Axboe65e19f52019-10-26 07:20:21 -06005216 for (i = 0; i < ctx->nr_user_files; i++) {
5217 struct file *file;
5218
5219 file = io_file_from_index(ctx, i);
5220 if (file)
5221 fput(file);
5222 }
Jens Axboe6b063142019-01-10 22:13:58 -07005223#endif
5224}
5225
Jens Axboe05f3fb32019-12-09 11:22:50 -07005226static void io_file_ref_kill(struct percpu_ref *ref)
5227{
5228 struct fixed_file_data *data;
5229
5230 data = container_of(ref, struct fixed_file_data, refs);
5231 complete(&data->done);
5232}
5233
Jens Axboe6b063142019-01-10 22:13:58 -07005234static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
5235{
Jens Axboe05f3fb32019-12-09 11:22:50 -07005236 struct fixed_file_data *data = ctx->file_data;
Jens Axboe65e19f52019-10-26 07:20:21 -06005237 unsigned nr_tables, i;
5238
Jens Axboe05f3fb32019-12-09 11:22:50 -07005239 if (!data)
Jens Axboe6b063142019-01-10 22:13:58 -07005240 return -ENXIO;
5241
Jens Axboe05f3fb32019-12-09 11:22:50 -07005242 percpu_ref_kill_and_confirm(&data->refs, io_file_ref_kill);
Jens Axboee46a7952020-01-17 11:15:34 -07005243 flush_work(&data->ref_work);
Jens Axboe2faf8522020-02-04 19:54:55 -07005244 wait_for_completion(&data->done);
5245 io_ring_file_ref_flush(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005246 percpu_ref_exit(&data->refs);
5247
Jens Axboe6b063142019-01-10 22:13:58 -07005248 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06005249 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
5250 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005251 kfree(data->table[i].files);
5252 kfree(data->table);
5253 kfree(data);
5254 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07005255 ctx->nr_user_files = 0;
5256 return 0;
5257}
5258
Jens Axboe6c271ce2019-01-10 11:22:30 -07005259static void io_sq_thread_stop(struct io_ring_ctx *ctx)
5260{
5261 if (ctx->sqo_thread) {
Jens Axboe206aefd2019-11-07 18:27:42 -07005262 wait_for_completion(&ctx->completions[1]);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005263 /*
5264 * The park is a bit of a work-around, without it we get
5265 * warning spews on shutdown with SQPOLL set and affinity
5266 * set to a single CPU.
5267 */
Jens Axboe06058632019-04-13 09:26:03 -06005268 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005269 kthread_stop(ctx->sqo_thread);
5270 ctx->sqo_thread = NULL;
5271 }
5272}
5273
Jens Axboe6b063142019-01-10 22:13:58 -07005274static void io_finish_async(struct io_ring_ctx *ctx)
5275{
Jens Axboe6c271ce2019-01-10 11:22:30 -07005276 io_sq_thread_stop(ctx);
5277
Jens Axboe561fb042019-10-24 07:25:42 -06005278 if (ctx->io_wq) {
5279 io_wq_destroy(ctx->io_wq);
5280 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07005281 }
5282}
5283
5284#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07005285/*
5286 * Ensure the UNIX gc is aware of our file set, so we are certain that
5287 * the io_uring can be safely unregistered on process exit, even if we have
5288 * loops in the file referencing.
5289 */
5290static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
5291{
5292 struct sock *sk = ctx->ring_sock->sk;
5293 struct scm_fp_list *fpl;
5294 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06005295 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07005296
5297 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
5298 unsigned long inflight = ctx->user->unix_inflight + nr;
5299
5300 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
5301 return -EMFILE;
5302 }
5303
5304 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
5305 if (!fpl)
5306 return -ENOMEM;
5307
5308 skb = alloc_skb(0, GFP_KERNEL);
5309 if (!skb) {
5310 kfree(fpl);
5311 return -ENOMEM;
5312 }
5313
5314 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07005315
Jens Axboe08a45172019-10-03 08:11:03 -06005316 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07005317 fpl->user = get_uid(ctx->user);
5318 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005319 struct file *file = io_file_from_index(ctx, i + offset);
5320
5321 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06005322 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06005323 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06005324 unix_inflight(fpl->user, fpl->fp[nr_files]);
5325 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07005326 }
5327
Jens Axboe08a45172019-10-03 08:11:03 -06005328 if (nr_files) {
5329 fpl->max = SCM_MAX_FD;
5330 fpl->count = nr_files;
5331 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005332 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06005333 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
5334 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07005335
Jens Axboe08a45172019-10-03 08:11:03 -06005336 for (i = 0; i < nr_files; i++)
5337 fput(fpl->fp[i]);
5338 } else {
5339 kfree_skb(skb);
5340 kfree(fpl);
5341 }
Jens Axboe6b063142019-01-10 22:13:58 -07005342
5343 return 0;
5344}
5345
5346/*
5347 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
5348 * causes regular reference counting to break down. We rely on the UNIX
5349 * garbage collection to take care of this problem for us.
5350 */
5351static int io_sqe_files_scm(struct io_ring_ctx *ctx)
5352{
5353 unsigned left, total;
5354 int ret = 0;
5355
5356 total = 0;
5357 left = ctx->nr_user_files;
5358 while (left) {
5359 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07005360
5361 ret = __io_sqe_files_scm(ctx, this_files, total);
5362 if (ret)
5363 break;
5364 left -= this_files;
5365 total += this_files;
5366 }
5367
5368 if (!ret)
5369 return 0;
5370
5371 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005372 struct file *file = io_file_from_index(ctx, total);
5373
5374 if (file)
5375 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07005376 total++;
5377 }
5378
5379 return ret;
5380}
5381#else
5382static int io_sqe_files_scm(struct io_ring_ctx *ctx)
5383{
5384 return 0;
5385}
5386#endif
5387
Jens Axboe65e19f52019-10-26 07:20:21 -06005388static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
5389 unsigned nr_files)
5390{
5391 int i;
5392
5393 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005394 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06005395 unsigned this_files;
5396
5397 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
5398 table->files = kcalloc(this_files, sizeof(struct file *),
5399 GFP_KERNEL);
5400 if (!table->files)
5401 break;
5402 nr_files -= this_files;
5403 }
5404
5405 if (i == nr_tables)
5406 return 0;
5407
5408 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005409 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06005410 kfree(table->files);
5411 }
5412 return 1;
5413}
5414
Jens Axboe05f3fb32019-12-09 11:22:50 -07005415static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
Jens Axboec3a31e62019-10-03 13:59:56 -06005416{
5417#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06005418 struct sock *sock = ctx->ring_sock->sk;
5419 struct sk_buff_head list, *head = &sock->sk_receive_queue;
5420 struct sk_buff *skb;
5421 int i;
5422
5423 __skb_queue_head_init(&list);
5424
5425 /*
5426 * Find the skb that holds this file in its SCM_RIGHTS. When found,
5427 * remove this entry and rearrange the file array.
5428 */
5429 skb = skb_dequeue(head);
5430 while (skb) {
5431 struct scm_fp_list *fp;
5432
5433 fp = UNIXCB(skb).fp;
5434 for (i = 0; i < fp->count; i++) {
5435 int left;
5436
5437 if (fp->fp[i] != file)
5438 continue;
5439
5440 unix_notinflight(fp->user, fp->fp[i]);
5441 left = fp->count - 1 - i;
5442 if (left) {
5443 memmove(&fp->fp[i], &fp->fp[i + 1],
5444 left * sizeof(struct file *));
5445 }
5446 fp->count--;
5447 if (!fp->count) {
5448 kfree_skb(skb);
5449 skb = NULL;
5450 } else {
5451 __skb_queue_tail(&list, skb);
5452 }
5453 fput(file);
5454 file = NULL;
5455 break;
5456 }
5457
5458 if (!file)
5459 break;
5460
5461 __skb_queue_tail(&list, skb);
5462
5463 skb = skb_dequeue(head);
5464 }
5465
5466 if (skb_peek(&list)) {
5467 spin_lock_irq(&head->lock);
5468 while ((skb = __skb_dequeue(&list)) != NULL)
5469 __skb_queue_tail(head, skb);
5470 spin_unlock_irq(&head->lock);
5471 }
5472#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07005473 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06005474#endif
5475}
5476
Jens Axboe05f3fb32019-12-09 11:22:50 -07005477struct io_file_put {
5478 struct llist_node llist;
5479 struct file *file;
5480 struct completion *done;
5481};
5482
Jens Axboe2faf8522020-02-04 19:54:55 -07005483static void io_ring_file_ref_flush(struct fixed_file_data *data)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005484{
5485 struct io_file_put *pfile, *tmp;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005486 struct llist_node *node;
5487
Jens Axboe05f3fb32019-12-09 11:22:50 -07005488 while ((node = llist_del_all(&data->put_llist)) != NULL) {
5489 llist_for_each_entry_safe(pfile, tmp, node, llist) {
5490 io_ring_file_put(data->ctx, pfile->file);
5491 if (pfile->done)
5492 complete(pfile->done);
5493 else
5494 kfree(pfile);
5495 }
5496 }
Jens Axboe2faf8522020-02-04 19:54:55 -07005497}
Jens Axboe05f3fb32019-12-09 11:22:50 -07005498
Jens Axboe2faf8522020-02-04 19:54:55 -07005499static void io_ring_file_ref_switch(struct work_struct *work)
5500{
5501 struct fixed_file_data *data;
5502
5503 data = container_of(work, struct fixed_file_data, ref_work);
5504 io_ring_file_ref_flush(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005505 percpu_ref_get(&data->refs);
5506 percpu_ref_switch_to_percpu(&data->refs);
5507}
5508
5509static void io_file_data_ref_zero(struct percpu_ref *ref)
5510{
5511 struct fixed_file_data *data;
5512
5513 data = container_of(ref, struct fixed_file_data, refs);
5514
Jens Axboe2faf8522020-02-04 19:54:55 -07005515 /*
5516 * We can't safely switch from inside this context, punt to wq. If
5517 * the table ref is going away, the table is being unregistered.
5518 * Don't queue up the async work for that case, the caller will
5519 * handle it.
5520 */
5521 if (!percpu_ref_is_dying(&data->refs))
5522 queue_work(system_wq, &data->ref_work);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005523}
5524
5525static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
5526 unsigned nr_args)
5527{
5528 __s32 __user *fds = (__s32 __user *) arg;
5529 unsigned nr_tables;
5530 struct file *file;
5531 int fd, ret = 0;
5532 unsigned i;
5533
5534 if (ctx->file_data)
5535 return -EBUSY;
5536 if (!nr_args)
5537 return -EINVAL;
5538 if (nr_args > IORING_MAX_FIXED_FILES)
5539 return -EMFILE;
5540
5541 ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
5542 if (!ctx->file_data)
5543 return -ENOMEM;
5544 ctx->file_data->ctx = ctx;
5545 init_completion(&ctx->file_data->done);
5546
5547 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
5548 ctx->file_data->table = kcalloc(nr_tables,
5549 sizeof(struct fixed_file_table),
5550 GFP_KERNEL);
5551 if (!ctx->file_data->table) {
5552 kfree(ctx->file_data);
5553 ctx->file_data = NULL;
5554 return -ENOMEM;
5555 }
5556
5557 if (percpu_ref_init(&ctx->file_data->refs, io_file_data_ref_zero,
5558 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
5559 kfree(ctx->file_data->table);
5560 kfree(ctx->file_data);
5561 ctx->file_data = NULL;
5562 return -ENOMEM;
5563 }
5564 ctx->file_data->put_llist.first = NULL;
5565 INIT_WORK(&ctx->file_data->ref_work, io_ring_file_ref_switch);
5566
5567 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
5568 percpu_ref_exit(&ctx->file_data->refs);
5569 kfree(ctx->file_data->table);
5570 kfree(ctx->file_data);
5571 ctx->file_data = NULL;
5572 return -ENOMEM;
5573 }
5574
5575 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
5576 struct fixed_file_table *table;
5577 unsigned index;
5578
5579 ret = -EFAULT;
5580 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
5581 break;
5582 /* allow sparse sets */
5583 if (fd == -1) {
5584 ret = 0;
5585 continue;
5586 }
5587
5588 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
5589 index = i & IORING_FILE_TABLE_MASK;
5590 file = fget(fd);
5591
5592 ret = -EBADF;
5593 if (!file)
5594 break;
5595
5596 /*
5597 * Don't allow io_uring instances to be registered. If UNIX
5598 * isn't enabled, then this causes a reference cycle and this
5599 * instance can never get freed. If UNIX is enabled we'll
5600 * handle it just fine, but there's still no point in allowing
5601 * a ring fd as it doesn't support regular read/write anyway.
5602 */
5603 if (file->f_op == &io_uring_fops) {
5604 fput(file);
5605 break;
5606 }
5607 ret = 0;
5608 table->files[index] = file;
5609 }
5610
5611 if (ret) {
5612 for (i = 0; i < ctx->nr_user_files; i++) {
5613 file = io_file_from_index(ctx, i);
5614 if (file)
5615 fput(file);
5616 }
5617 for (i = 0; i < nr_tables; i++)
5618 kfree(ctx->file_data->table[i].files);
5619
5620 kfree(ctx->file_data->table);
5621 kfree(ctx->file_data);
5622 ctx->file_data = NULL;
5623 ctx->nr_user_files = 0;
5624 return ret;
5625 }
5626
5627 ret = io_sqe_files_scm(ctx);
5628 if (ret)
5629 io_sqe_files_unregister(ctx);
5630
5631 return ret;
5632}
5633
Jens Axboec3a31e62019-10-03 13:59:56 -06005634static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
5635 int index)
5636{
5637#if defined(CONFIG_UNIX)
5638 struct sock *sock = ctx->ring_sock->sk;
5639 struct sk_buff_head *head = &sock->sk_receive_queue;
5640 struct sk_buff *skb;
5641
5642 /*
5643 * See if we can merge this file into an existing skb SCM_RIGHTS
5644 * file set. If there's no room, fall back to allocating a new skb
5645 * and filling it in.
5646 */
5647 spin_lock_irq(&head->lock);
5648 skb = skb_peek(head);
5649 if (skb) {
5650 struct scm_fp_list *fpl = UNIXCB(skb).fp;
5651
5652 if (fpl->count < SCM_MAX_FD) {
5653 __skb_unlink(skb, head);
5654 spin_unlock_irq(&head->lock);
5655 fpl->fp[fpl->count] = get_file(file);
5656 unix_inflight(fpl->user, fpl->fp[fpl->count]);
5657 fpl->count++;
5658 spin_lock_irq(&head->lock);
5659 __skb_queue_head(head, skb);
5660 } else {
5661 skb = NULL;
5662 }
5663 }
5664 spin_unlock_irq(&head->lock);
5665
5666 if (skb) {
5667 fput(file);
5668 return 0;
5669 }
5670
5671 return __io_sqe_files_scm(ctx, 1, index);
5672#else
5673 return 0;
5674#endif
5675}
5676
Jens Axboe05f3fb32019-12-09 11:22:50 -07005677static void io_atomic_switch(struct percpu_ref *ref)
Jens Axboec3a31e62019-10-03 13:59:56 -06005678{
Jens Axboe05f3fb32019-12-09 11:22:50 -07005679 struct fixed_file_data *data;
5680
5681 data = container_of(ref, struct fixed_file_data, refs);
5682 clear_bit(FFD_F_ATOMIC, &data->state);
5683}
5684
5685static bool io_queue_file_removal(struct fixed_file_data *data,
5686 struct file *file)
5687{
5688 struct io_file_put *pfile, pfile_stack;
5689 DECLARE_COMPLETION_ONSTACK(done);
5690
5691 /*
5692 * If we fail allocating the struct we need for doing async reomval
5693 * of this file, just punt to sync and wait for it.
5694 */
5695 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
5696 if (!pfile) {
5697 pfile = &pfile_stack;
5698 pfile->done = &done;
5699 }
5700
5701 pfile->file = file;
5702 llist_add(&pfile->llist, &data->put_llist);
5703
5704 if (pfile == &pfile_stack) {
5705 if (!test_and_set_bit(FFD_F_ATOMIC, &data->state)) {
5706 percpu_ref_put(&data->refs);
5707 percpu_ref_switch_to_atomic(&data->refs,
5708 io_atomic_switch);
5709 }
5710 wait_for_completion(&done);
5711 flush_work(&data->ref_work);
5712 return false;
5713 }
5714
5715 return true;
5716}
5717
5718static int __io_sqe_files_update(struct io_ring_ctx *ctx,
5719 struct io_uring_files_update *up,
5720 unsigned nr_args)
5721{
5722 struct fixed_file_data *data = ctx->file_data;
5723 bool ref_switch = false;
5724 struct file *file;
Jens Axboec3a31e62019-10-03 13:59:56 -06005725 __s32 __user *fds;
5726 int fd, i, err;
5727 __u32 done;
5728
Jens Axboe05f3fb32019-12-09 11:22:50 -07005729 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06005730 return -EOVERFLOW;
5731 if (done > ctx->nr_user_files)
5732 return -EINVAL;
5733
5734 done = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005735 fds = u64_to_user_ptr(up->fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06005736 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005737 struct fixed_file_table *table;
5738 unsigned index;
5739
Jens Axboec3a31e62019-10-03 13:59:56 -06005740 err = 0;
5741 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
5742 err = -EFAULT;
5743 break;
5744 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07005745 i = array_index_nospec(up->offset, ctx->nr_user_files);
5746 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
Jens Axboe65e19f52019-10-26 07:20:21 -06005747 index = i & IORING_FILE_TABLE_MASK;
5748 if (table->files[index]) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005749 file = io_file_from_index(ctx, index);
Jens Axboe65e19f52019-10-26 07:20:21 -06005750 table->files[index] = NULL;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005751 if (io_queue_file_removal(data, file))
5752 ref_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06005753 }
5754 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06005755 file = fget(fd);
5756 if (!file) {
5757 err = -EBADF;
5758 break;
5759 }
5760 /*
5761 * Don't allow io_uring instances to be registered. If
5762 * UNIX isn't enabled, then this causes a reference
5763 * cycle and this instance can never get freed. If UNIX
5764 * is enabled we'll handle it just fine, but there's
5765 * still no point in allowing a ring fd as it doesn't
5766 * support regular read/write anyway.
5767 */
5768 if (file->f_op == &io_uring_fops) {
5769 fput(file);
5770 err = -EBADF;
5771 break;
5772 }
Jens Axboe65e19f52019-10-26 07:20:21 -06005773 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06005774 err = io_sqe_file_register(ctx, file, i);
5775 if (err)
5776 break;
5777 }
5778 nr_args--;
5779 done++;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005780 up->offset++;
5781 }
5782
5783 if (ref_switch && !test_and_set_bit(FFD_F_ATOMIC, &data->state)) {
5784 percpu_ref_put(&data->refs);
5785 percpu_ref_switch_to_atomic(&data->refs, io_atomic_switch);
Jens Axboec3a31e62019-10-03 13:59:56 -06005786 }
5787
5788 return done ? done : err;
5789}
Jens Axboe05f3fb32019-12-09 11:22:50 -07005790static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
5791 unsigned nr_args)
5792{
5793 struct io_uring_files_update up;
5794
5795 if (!ctx->file_data)
5796 return -ENXIO;
5797 if (!nr_args)
5798 return -EINVAL;
5799 if (copy_from_user(&up, arg, sizeof(up)))
5800 return -EFAULT;
5801 if (up.resv)
5802 return -EINVAL;
5803
5804 return __io_sqe_files_update(ctx, &up, nr_args);
5805}
Jens Axboec3a31e62019-10-03 13:59:56 -06005806
Jens Axboe7d723062019-11-12 22:31:31 -07005807static void io_put_work(struct io_wq_work *work)
5808{
5809 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
5810
5811 io_put_req(req);
5812}
5813
5814static void io_get_work(struct io_wq_work *work)
5815{
5816 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
5817
5818 refcount_inc(&req->refs);
5819}
5820
Pavel Begunkov24369c22020-01-28 03:15:48 +03005821static int io_init_wq_offload(struct io_ring_ctx *ctx,
5822 struct io_uring_params *p)
5823{
5824 struct io_wq_data data;
5825 struct fd f;
5826 struct io_ring_ctx *ctx_attach;
5827 unsigned int concurrency;
5828 int ret = 0;
5829
5830 data.user = ctx->user;
5831 data.get_work = io_get_work;
5832 data.put_work = io_put_work;
5833
5834 if (!(p->flags & IORING_SETUP_ATTACH_WQ)) {
5835 /* Do QD, or 4 * CPUS, whatever is smallest */
5836 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
5837
5838 ctx->io_wq = io_wq_create(concurrency, &data);
5839 if (IS_ERR(ctx->io_wq)) {
5840 ret = PTR_ERR(ctx->io_wq);
5841 ctx->io_wq = NULL;
5842 }
5843 return ret;
5844 }
5845
5846 f = fdget(p->wq_fd);
5847 if (!f.file)
5848 return -EBADF;
5849
5850 if (f.file->f_op != &io_uring_fops) {
5851 ret = -EINVAL;
5852 goto out_fput;
5853 }
5854
5855 ctx_attach = f.file->private_data;
5856 /* @io_wq is protected by holding the fd */
5857 if (!io_wq_get(ctx_attach->io_wq, &data)) {
5858 ret = -EINVAL;
5859 goto out_fput;
5860 }
5861
5862 ctx->io_wq = ctx_attach->io_wq;
5863out_fput:
5864 fdput(f);
5865 return ret;
5866}
5867
Jens Axboe6c271ce2019-01-10 11:22:30 -07005868static int io_sq_offload_start(struct io_ring_ctx *ctx,
5869 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005870{
5871 int ret;
5872
Jens Axboe6c271ce2019-01-10 11:22:30 -07005873 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005874 mmgrab(current->mm);
5875 ctx->sqo_mm = current->mm;
5876
Jens Axboe6c271ce2019-01-10 11:22:30 -07005877 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06005878 ret = -EPERM;
5879 if (!capable(CAP_SYS_ADMIN))
5880 goto err;
5881
Jens Axboe917257d2019-04-13 09:28:55 -06005882 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
5883 if (!ctx->sq_thread_idle)
5884 ctx->sq_thread_idle = HZ;
5885
Jens Axboe6c271ce2019-01-10 11:22:30 -07005886 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06005887 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005888
Jens Axboe917257d2019-04-13 09:28:55 -06005889 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06005890 if (cpu >= nr_cpu_ids)
5891 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08005892 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06005893 goto err;
5894
Jens Axboe6c271ce2019-01-10 11:22:30 -07005895 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
5896 ctx, cpu,
5897 "io_uring-sq");
5898 } else {
5899 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
5900 "io_uring-sq");
5901 }
5902 if (IS_ERR(ctx->sqo_thread)) {
5903 ret = PTR_ERR(ctx->sqo_thread);
5904 ctx->sqo_thread = NULL;
5905 goto err;
5906 }
5907 wake_up_process(ctx->sqo_thread);
5908 } else if (p->flags & IORING_SETUP_SQ_AFF) {
5909 /* Can't have SQ_AFF without SQPOLL */
5910 ret = -EINVAL;
5911 goto err;
5912 }
5913
Pavel Begunkov24369c22020-01-28 03:15:48 +03005914 ret = io_init_wq_offload(ctx, p);
5915 if (ret)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005916 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005917
5918 return 0;
5919err:
Jens Axboe54a91f32019-09-10 09:15:04 -06005920 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005921 mmdrop(ctx->sqo_mm);
5922 ctx->sqo_mm = NULL;
5923 return ret;
5924}
5925
5926static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
5927{
5928 atomic_long_sub(nr_pages, &user->locked_vm);
5929}
5930
5931static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
5932{
5933 unsigned long page_limit, cur_pages, new_pages;
5934
5935 /* Don't allow more pages than we can safely lock */
5936 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
5937
5938 do {
5939 cur_pages = atomic_long_read(&user->locked_vm);
5940 new_pages = cur_pages + nr_pages;
5941 if (new_pages > page_limit)
5942 return -ENOMEM;
5943 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
5944 new_pages) != cur_pages);
5945
5946 return 0;
5947}
5948
5949static void io_mem_free(void *ptr)
5950{
Mark Rutland52e04ef2019-04-30 17:30:21 +01005951 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005952
Mark Rutland52e04ef2019-04-30 17:30:21 +01005953 if (!ptr)
5954 return;
5955
5956 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005957 if (put_page_testzero(page))
5958 free_compound_page(page);
5959}
5960
5961static void *io_mem_alloc(size_t size)
5962{
5963 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
5964 __GFP_NORETRY;
5965
5966 return (void *) __get_free_pages(gfp_flags, get_order(size));
5967}
5968
Hristo Venev75b28af2019-08-26 17:23:46 +00005969static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
5970 size_t *sq_offset)
5971{
5972 struct io_rings *rings;
5973 size_t off, sq_array_size;
5974
5975 off = struct_size(rings, cqes, cq_entries);
5976 if (off == SIZE_MAX)
5977 return SIZE_MAX;
5978
5979#ifdef CONFIG_SMP
5980 off = ALIGN(off, SMP_CACHE_BYTES);
5981 if (off == 0)
5982 return SIZE_MAX;
5983#endif
5984
5985 sq_array_size = array_size(sizeof(u32), sq_entries);
5986 if (sq_array_size == SIZE_MAX)
5987 return SIZE_MAX;
5988
5989 if (check_add_overflow(off, sq_array_size, &off))
5990 return SIZE_MAX;
5991
5992 if (sq_offset)
5993 *sq_offset = off;
5994
5995 return off;
5996}
5997
Jens Axboe2b188cc2019-01-07 10:46:33 -07005998static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
5999{
Hristo Venev75b28af2019-08-26 17:23:46 +00006000 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006001
Hristo Venev75b28af2019-08-26 17:23:46 +00006002 pages = (size_t)1 << get_order(
6003 rings_size(sq_entries, cq_entries, NULL));
6004 pages += (size_t)1 << get_order(
6005 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07006006
Hristo Venev75b28af2019-08-26 17:23:46 +00006007 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006008}
6009
Jens Axboeedafcce2019-01-09 09:16:05 -07006010static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
6011{
6012 int i, j;
6013
6014 if (!ctx->user_bufs)
6015 return -ENXIO;
6016
6017 for (i = 0; i < ctx->nr_user_bufs; i++) {
6018 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
6019
6020 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08006021 unpin_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07006022
6023 if (ctx->account_mem)
6024 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006025 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07006026 imu->nr_bvecs = 0;
6027 }
6028
6029 kfree(ctx->user_bufs);
6030 ctx->user_bufs = NULL;
6031 ctx->nr_user_bufs = 0;
6032 return 0;
6033}
6034
6035static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
6036 void __user *arg, unsigned index)
6037{
6038 struct iovec __user *src;
6039
6040#ifdef CONFIG_COMPAT
6041 if (ctx->compat) {
6042 struct compat_iovec __user *ciovs;
6043 struct compat_iovec ciov;
6044
6045 ciovs = (struct compat_iovec __user *) arg;
6046 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
6047 return -EFAULT;
6048
Jens Axboed55e5f52019-12-11 16:12:15 -07006049 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07006050 dst->iov_len = ciov.iov_len;
6051 return 0;
6052 }
6053#endif
6054 src = (struct iovec __user *) arg;
6055 if (copy_from_user(dst, &src[index], sizeof(*dst)))
6056 return -EFAULT;
6057 return 0;
6058}
6059
6060static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
6061 unsigned nr_args)
6062{
6063 struct vm_area_struct **vmas = NULL;
6064 struct page **pages = NULL;
6065 int i, j, got_pages = 0;
6066 int ret = -EINVAL;
6067
6068 if (ctx->user_bufs)
6069 return -EBUSY;
6070 if (!nr_args || nr_args > UIO_MAXIOV)
6071 return -EINVAL;
6072
6073 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
6074 GFP_KERNEL);
6075 if (!ctx->user_bufs)
6076 return -ENOMEM;
6077
6078 for (i = 0; i < nr_args; i++) {
6079 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
6080 unsigned long off, start, end, ubuf;
6081 int pret, nr_pages;
6082 struct iovec iov;
6083 size_t size;
6084
6085 ret = io_copy_iov(ctx, &iov, arg, i);
6086 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03006087 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07006088
6089 /*
6090 * Don't impose further limits on the size and buffer
6091 * constraints here, we'll -EINVAL later when IO is
6092 * submitted if they are wrong.
6093 */
6094 ret = -EFAULT;
6095 if (!iov.iov_base || !iov.iov_len)
6096 goto err;
6097
6098 /* arbitrary limit, but we need something */
6099 if (iov.iov_len > SZ_1G)
6100 goto err;
6101
6102 ubuf = (unsigned long) iov.iov_base;
6103 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
6104 start = ubuf >> PAGE_SHIFT;
6105 nr_pages = end - start;
6106
6107 if (ctx->account_mem) {
6108 ret = io_account_mem(ctx->user, nr_pages);
6109 if (ret)
6110 goto err;
6111 }
6112
6113 ret = 0;
6114 if (!pages || nr_pages > got_pages) {
6115 kfree(vmas);
6116 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006117 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07006118 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006119 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07006120 sizeof(struct vm_area_struct *),
6121 GFP_KERNEL);
6122 if (!pages || !vmas) {
6123 ret = -ENOMEM;
6124 if (ctx->account_mem)
6125 io_unaccount_mem(ctx->user, nr_pages);
6126 goto err;
6127 }
6128 got_pages = nr_pages;
6129 }
6130
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006131 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07006132 GFP_KERNEL);
6133 ret = -ENOMEM;
6134 if (!imu->bvec) {
6135 if (ctx->account_mem)
6136 io_unaccount_mem(ctx->user, nr_pages);
6137 goto err;
6138 }
6139
6140 ret = 0;
6141 down_read(&current->mm->mmap_sem);
John Hubbard2113b052020-01-30 22:13:13 -08006142 pret = pin_user_pages(ubuf, nr_pages,
Ira Weiny932f4a62019-05-13 17:17:03 -07006143 FOLL_WRITE | FOLL_LONGTERM,
6144 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07006145 if (pret == nr_pages) {
6146 /* don't support file backed memory */
6147 for (j = 0; j < nr_pages; j++) {
6148 struct vm_area_struct *vma = vmas[j];
6149
6150 if (vma->vm_file &&
6151 !is_file_hugepages(vma->vm_file)) {
6152 ret = -EOPNOTSUPP;
6153 break;
6154 }
6155 }
6156 } else {
6157 ret = pret < 0 ? pret : -EFAULT;
6158 }
6159 up_read(&current->mm->mmap_sem);
6160 if (ret) {
6161 /*
6162 * if we did partial map, or found file backed vmas,
6163 * release any pages we did get
6164 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07006165 if (pret > 0)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08006166 unpin_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07006167 if (ctx->account_mem)
6168 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006169 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07006170 goto err;
6171 }
6172
6173 off = ubuf & ~PAGE_MASK;
6174 size = iov.iov_len;
6175 for (j = 0; j < nr_pages; j++) {
6176 size_t vec_len;
6177
6178 vec_len = min_t(size_t, size, PAGE_SIZE - off);
6179 imu->bvec[j].bv_page = pages[j];
6180 imu->bvec[j].bv_len = vec_len;
6181 imu->bvec[j].bv_offset = off;
6182 off = 0;
6183 size -= vec_len;
6184 }
6185 /* store original address for later verification */
6186 imu->ubuf = ubuf;
6187 imu->len = iov.iov_len;
6188 imu->nr_bvecs = nr_pages;
6189
6190 ctx->nr_user_bufs++;
6191 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006192 kvfree(pages);
6193 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07006194 return 0;
6195err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006196 kvfree(pages);
6197 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07006198 io_sqe_buffer_unregister(ctx);
6199 return ret;
6200}
6201
Jens Axboe9b402842019-04-11 11:45:41 -06006202static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
6203{
6204 __s32 __user *fds = arg;
6205 int fd;
6206
6207 if (ctx->cq_ev_fd)
6208 return -EBUSY;
6209
6210 if (copy_from_user(&fd, fds, sizeof(*fds)))
6211 return -EFAULT;
6212
6213 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
6214 if (IS_ERR(ctx->cq_ev_fd)) {
6215 int ret = PTR_ERR(ctx->cq_ev_fd);
6216 ctx->cq_ev_fd = NULL;
6217 return ret;
6218 }
6219
6220 return 0;
6221}
6222
6223static int io_eventfd_unregister(struct io_ring_ctx *ctx)
6224{
6225 if (ctx->cq_ev_fd) {
6226 eventfd_ctx_put(ctx->cq_ev_fd);
6227 ctx->cq_ev_fd = NULL;
6228 return 0;
6229 }
6230
6231 return -ENXIO;
6232}
6233
Jens Axboe2b188cc2019-01-07 10:46:33 -07006234static void io_ring_ctx_free(struct io_ring_ctx *ctx)
6235{
Jens Axboe6b063142019-01-10 22:13:58 -07006236 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006237 if (ctx->sqo_mm)
6238 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07006239
6240 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07006241 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07006242 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06006243 io_eventfd_unregister(ctx);
Jens Axboedef596e2019-01-09 08:59:42 -07006244
Jens Axboe2b188cc2019-01-07 10:46:33 -07006245#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07006246 if (ctx->ring_sock) {
6247 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07006248 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07006249 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006250#endif
6251
Hristo Venev75b28af2019-08-26 17:23:46 +00006252 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006253 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006254
6255 percpu_ref_exit(&ctx->refs);
6256 if (ctx->account_mem)
6257 io_unaccount_mem(ctx->user,
6258 ring_pages(ctx->sq_entries, ctx->cq_entries));
6259 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07006260 put_cred(ctx->creds);
Jens Axboe206aefd2019-11-07 18:27:42 -07006261 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -07006262 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07006263 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006264 kfree(ctx);
6265}
6266
6267static __poll_t io_uring_poll(struct file *file, poll_table *wait)
6268{
6269 struct io_ring_ctx *ctx = file->private_data;
6270 __poll_t mask = 0;
6271
6272 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02006273 /*
6274 * synchronizes with barrier from wq_has_sleeper call in
6275 * io_commit_cqring
6276 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07006277 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00006278 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
6279 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006280 mask |= EPOLLOUT | EPOLLWRNORM;
Stefano Garzarella63e5d812020-02-07 13:18:28 +01006281 if (io_cqring_events(ctx, false))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006282 mask |= EPOLLIN | EPOLLRDNORM;
6283
6284 return mask;
6285}
6286
6287static int io_uring_fasync(int fd, struct file *file, int on)
6288{
6289 struct io_ring_ctx *ctx = file->private_data;
6290
6291 return fasync_helper(fd, file, on, &ctx->cq_fasync);
6292}
6293
Jens Axboe071698e2020-01-28 10:04:42 -07006294static int io_remove_personalities(int id, void *p, void *data)
6295{
6296 struct io_ring_ctx *ctx = data;
6297 const struct cred *cred;
6298
6299 cred = idr_remove(&ctx->personality_idr, id);
6300 if (cred)
6301 put_cred(cred);
6302 return 0;
6303}
6304
Jens Axboe2b188cc2019-01-07 10:46:33 -07006305static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
6306{
6307 mutex_lock(&ctx->uring_lock);
6308 percpu_ref_kill(&ctx->refs);
6309 mutex_unlock(&ctx->uring_lock);
6310
Jens Axboedf069d82020-02-04 16:48:34 -07006311 /*
6312 * Wait for sq thread to idle, if we have one. It won't spin on new
6313 * work after we've killed the ctx ref above. This is important to do
6314 * before we cancel existing commands, as the thread could otherwise
6315 * be queueing new work post that. If that's work we need to cancel,
6316 * it could cause shutdown to hang.
6317 */
6318 while (ctx->sqo_thread && !wq_has_sleeper(&ctx->sqo_wait))
6319 cpu_relax();
6320
Jens Axboe5262f562019-09-17 12:26:57 -06006321 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07006322 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06006323
6324 if (ctx->io_wq)
6325 io_wq_cancel_all(ctx->io_wq);
6326
Jens Axboedef596e2019-01-09 08:59:42 -07006327 io_iopoll_reap_events(ctx);
Jens Axboe15dff282019-11-13 09:09:23 -07006328 /* if we failed setting up the ctx, we might not have any rings */
6329 if (ctx->rings)
6330 io_cqring_overflow_flush(ctx, true);
Jens Axboe071698e2020-01-28 10:04:42 -07006331 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
Jens Axboe206aefd2019-11-07 18:27:42 -07006332 wait_for_completion(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006333 io_ring_ctx_free(ctx);
6334}
6335
6336static int io_uring_release(struct inode *inode, struct file *file)
6337{
6338 struct io_ring_ctx *ctx = file->private_data;
6339
6340 file->private_data = NULL;
6341 io_ring_ctx_wait_and_kill(ctx);
6342 return 0;
6343}
6344
Jens Axboefcb323c2019-10-24 12:39:47 -06006345static void io_uring_cancel_files(struct io_ring_ctx *ctx,
6346 struct files_struct *files)
6347{
6348 struct io_kiocb *req;
6349 DEFINE_WAIT(wait);
6350
6351 while (!list_empty_careful(&ctx->inflight_list)) {
Jens Axboe768134d2019-11-10 20:30:53 -07006352 struct io_kiocb *cancel_req = NULL;
Jens Axboefcb323c2019-10-24 12:39:47 -06006353
6354 spin_lock_irq(&ctx->inflight_lock);
6355 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07006356 if (req->work.files != files)
6357 continue;
6358 /* req is being completed, ignore */
6359 if (!refcount_inc_not_zero(&req->refs))
6360 continue;
6361 cancel_req = req;
6362 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06006363 }
Jens Axboe768134d2019-11-10 20:30:53 -07006364 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06006365 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07006366 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06006367 spin_unlock_irq(&ctx->inflight_lock);
6368
Jens Axboe768134d2019-11-10 20:30:53 -07006369 /* We need to keep going until we don't find a matching req */
6370 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06006371 break;
Bob Liu2f6d9b92019-11-13 18:06:24 +08006372
6373 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
6374 io_put_req(cancel_req);
Jens Axboefcb323c2019-10-24 12:39:47 -06006375 schedule();
6376 }
Jens Axboe768134d2019-11-10 20:30:53 -07006377 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06006378}
6379
6380static int io_uring_flush(struct file *file, void *data)
6381{
6382 struct io_ring_ctx *ctx = file->private_data;
6383
6384 io_uring_cancel_files(ctx, data);
Jens Axboefcb323c2019-10-24 12:39:47 -06006385 return 0;
6386}
6387
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006388static void *io_uring_validate_mmap_request(struct file *file,
6389 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006390{
Jens Axboe2b188cc2019-01-07 10:46:33 -07006391 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006392 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006393 struct page *page;
6394 void *ptr;
6395
6396 switch (offset) {
6397 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00006398 case IORING_OFF_CQ_RING:
6399 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006400 break;
6401 case IORING_OFF_SQES:
6402 ptr = ctx->sq_sqes;
6403 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006404 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006405 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006406 }
6407
6408 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07006409 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006410 return ERR_PTR(-EINVAL);
6411
6412 return ptr;
6413}
6414
6415#ifdef CONFIG_MMU
6416
6417static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
6418{
6419 size_t sz = vma->vm_end - vma->vm_start;
6420 unsigned long pfn;
6421 void *ptr;
6422
6423 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
6424 if (IS_ERR(ptr))
6425 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006426
6427 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
6428 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
6429}
6430
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006431#else /* !CONFIG_MMU */
6432
6433static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
6434{
6435 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
6436}
6437
6438static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
6439{
6440 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
6441}
6442
6443static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
6444 unsigned long addr, unsigned long len,
6445 unsigned long pgoff, unsigned long flags)
6446{
6447 void *ptr;
6448
6449 ptr = io_uring_validate_mmap_request(file, pgoff, len);
6450 if (IS_ERR(ptr))
6451 return PTR_ERR(ptr);
6452
6453 return (unsigned long) ptr;
6454}
6455
6456#endif /* !CONFIG_MMU */
6457
Jens Axboe2b188cc2019-01-07 10:46:33 -07006458SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
6459 u32, min_complete, u32, flags, const sigset_t __user *, sig,
6460 size_t, sigsz)
6461{
6462 struct io_ring_ctx *ctx;
6463 long ret = -EBADF;
6464 int submitted = 0;
6465 struct fd f;
6466
Jens Axboe6c271ce2019-01-10 11:22:30 -07006467 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006468 return -EINVAL;
6469
6470 f = fdget(fd);
6471 if (!f.file)
6472 return -EBADF;
6473
6474 ret = -EOPNOTSUPP;
6475 if (f.file->f_op != &io_uring_fops)
6476 goto out_fput;
6477
6478 ret = -ENXIO;
6479 ctx = f.file->private_data;
6480 if (!percpu_ref_tryget(&ctx->refs))
6481 goto out_fput;
6482
Jens Axboe6c271ce2019-01-10 11:22:30 -07006483 /*
6484 * For SQ polling, the thread will do all submissions and completions.
6485 * Just return the requested submit count, and wake the thread if
6486 * we were asked to.
6487 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06006488 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006489 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07006490 if (!list_empty_careful(&ctx->cq_overflow_list))
6491 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006492 if (flags & IORING_ENTER_SQ_WAKEUP)
6493 wake_up(&ctx->sqo_wait);
6494 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06006495 } else if (to_submit) {
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006496 struct mm_struct *cur_mm;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006497
6498 mutex_lock(&ctx->uring_lock);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006499 /* already have mm, so io_submit_sqes() won't try to grab it */
6500 cur_mm = ctx->sqo_mm;
6501 submitted = io_submit_sqes(ctx, to_submit, f.file, fd,
6502 &cur_mm, false);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006503 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03006504
6505 if (submitted != to_submit)
6506 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006507 }
6508 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07006509 unsigned nr_events = 0;
6510
Jens Axboe2b188cc2019-01-07 10:46:33 -07006511 min_complete = min(min_complete, ctx->cq_entries);
6512
Jens Axboedef596e2019-01-09 08:59:42 -07006513 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07006514 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07006515 } else {
6516 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
6517 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006518 }
6519
Pavel Begunkov7c504e652019-12-18 19:53:45 +03006520out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03006521 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006522out_fput:
6523 fdput(f);
6524 return submitted ? submitted : ret;
6525}
6526
Jens Axboe87ce9552020-01-30 08:25:34 -07006527static int io_uring_show_cred(int id, void *p, void *data)
6528{
6529 const struct cred *cred = p;
6530 struct seq_file *m = data;
6531 struct user_namespace *uns = seq_user_ns(m);
6532 struct group_info *gi;
6533 kernel_cap_t cap;
6534 unsigned __capi;
6535 int g;
6536
6537 seq_printf(m, "%5d\n", id);
6538 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
6539 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
6540 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
6541 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
6542 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
6543 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
6544 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
6545 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
6546 seq_puts(m, "\n\tGroups:\t");
6547 gi = cred->group_info;
6548 for (g = 0; g < gi->ngroups; g++) {
6549 seq_put_decimal_ull(m, g ? " " : "",
6550 from_kgid_munged(uns, gi->gid[g]));
6551 }
6552 seq_puts(m, "\n\tCapEff:\t");
6553 cap = cred->cap_effective;
6554 CAP_FOR_EACH_U32(__capi)
6555 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
6556 seq_putc(m, '\n');
6557 return 0;
6558}
6559
6560static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
6561{
6562 int i;
6563
6564 mutex_lock(&ctx->uring_lock);
6565 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
6566 for (i = 0; i < ctx->nr_user_files; i++) {
6567 struct fixed_file_table *table;
6568 struct file *f;
6569
6570 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
6571 f = table->files[i & IORING_FILE_TABLE_MASK];
6572 if (f)
6573 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
6574 else
6575 seq_printf(m, "%5u: <none>\n", i);
6576 }
6577 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
6578 for (i = 0; i < ctx->nr_user_bufs; i++) {
6579 struct io_mapped_ubuf *buf = &ctx->user_bufs[i];
6580
6581 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf,
6582 (unsigned int) buf->len);
6583 }
6584 if (!idr_is_empty(&ctx->personality_idr)) {
6585 seq_printf(m, "Personalities:\n");
6586 idr_for_each(&ctx->personality_idr, io_uring_show_cred, m);
6587 }
6588 mutex_unlock(&ctx->uring_lock);
6589}
6590
6591static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
6592{
6593 struct io_ring_ctx *ctx = f->private_data;
6594
6595 if (percpu_ref_tryget(&ctx->refs)) {
6596 __io_uring_show_fdinfo(ctx, m);
6597 percpu_ref_put(&ctx->refs);
6598 }
6599}
6600
Jens Axboe2b188cc2019-01-07 10:46:33 -07006601static const struct file_operations io_uring_fops = {
6602 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06006603 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07006604 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006605#ifndef CONFIG_MMU
6606 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
6607 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
6608#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07006609 .poll = io_uring_poll,
6610 .fasync = io_uring_fasync,
Jens Axboe87ce9552020-01-30 08:25:34 -07006611 .show_fdinfo = io_uring_show_fdinfo,
Jens Axboe2b188cc2019-01-07 10:46:33 -07006612};
6613
6614static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
6615 struct io_uring_params *p)
6616{
Hristo Venev75b28af2019-08-26 17:23:46 +00006617 struct io_rings *rings;
6618 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006619
Hristo Venev75b28af2019-08-26 17:23:46 +00006620 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
6621 if (size == SIZE_MAX)
6622 return -EOVERFLOW;
6623
6624 rings = io_mem_alloc(size);
6625 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006626 return -ENOMEM;
6627
Hristo Venev75b28af2019-08-26 17:23:46 +00006628 ctx->rings = rings;
6629 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
6630 rings->sq_ring_mask = p->sq_entries - 1;
6631 rings->cq_ring_mask = p->cq_entries - 1;
6632 rings->sq_ring_entries = p->sq_entries;
6633 rings->cq_ring_entries = p->cq_entries;
6634 ctx->sq_mask = rings->sq_ring_mask;
6635 ctx->cq_mask = rings->cq_ring_mask;
6636 ctx->sq_entries = rings->sq_ring_entries;
6637 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006638
6639 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07006640 if (size == SIZE_MAX) {
6641 io_mem_free(ctx->rings);
6642 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006643 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07006644 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006645
6646 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07006647 if (!ctx->sq_sqes) {
6648 io_mem_free(ctx->rings);
6649 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006650 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07006651 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006652
Jens Axboe2b188cc2019-01-07 10:46:33 -07006653 return 0;
6654}
6655
6656/*
6657 * Allocate an anonymous fd, this is what constitutes the application
6658 * visible backing of an io_uring instance. The application mmaps this
6659 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
6660 * we have to tie this fd to a socket for file garbage collection purposes.
6661 */
6662static int io_uring_get_fd(struct io_ring_ctx *ctx)
6663{
6664 struct file *file;
6665 int ret;
6666
6667#if defined(CONFIG_UNIX)
6668 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
6669 &ctx->ring_sock);
6670 if (ret)
6671 return ret;
6672#endif
6673
6674 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
6675 if (ret < 0)
6676 goto err;
6677
6678 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
6679 O_RDWR | O_CLOEXEC);
6680 if (IS_ERR(file)) {
6681 put_unused_fd(ret);
6682 ret = PTR_ERR(file);
6683 goto err;
6684 }
6685
6686#if defined(CONFIG_UNIX)
6687 ctx->ring_sock->file = file;
6688#endif
6689 fd_install(ret, file);
6690 return ret;
6691err:
6692#if defined(CONFIG_UNIX)
6693 sock_release(ctx->ring_sock);
6694 ctx->ring_sock = NULL;
6695#endif
6696 return ret;
6697}
6698
6699static int io_uring_create(unsigned entries, struct io_uring_params *p)
6700{
6701 struct user_struct *user = NULL;
6702 struct io_ring_ctx *ctx;
6703 bool account_mem;
6704 int ret;
6705
Jens Axboe8110c1a2019-12-28 15:39:54 -07006706 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006707 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07006708 if (entries > IORING_MAX_ENTRIES) {
6709 if (!(p->flags & IORING_SETUP_CLAMP))
6710 return -EINVAL;
6711 entries = IORING_MAX_ENTRIES;
6712 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006713
6714 /*
6715 * Use twice as many entries for the CQ ring. It's possible for the
6716 * application to drive a higher depth than the size of the SQ ring,
6717 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06006718 * some flexibility in overcommitting a bit. If the application has
6719 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
6720 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07006721 */
6722 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06006723 if (p->flags & IORING_SETUP_CQSIZE) {
6724 /*
6725 * If IORING_SETUP_CQSIZE is set, we do the same roundup
6726 * to a power-of-two, if it isn't already. We do NOT impose
6727 * any cq vs sq ring sizing.
6728 */
Jens Axboe8110c1a2019-12-28 15:39:54 -07006729 if (p->cq_entries < p->sq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06006730 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07006731 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
6732 if (!(p->flags & IORING_SETUP_CLAMP))
6733 return -EINVAL;
6734 p->cq_entries = IORING_MAX_CQ_ENTRIES;
6735 }
Jens Axboe33a107f2019-10-04 12:10:03 -06006736 p->cq_entries = roundup_pow_of_two(p->cq_entries);
6737 } else {
6738 p->cq_entries = 2 * p->sq_entries;
6739 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006740
6741 user = get_uid(current_user());
6742 account_mem = !capable(CAP_IPC_LOCK);
6743
6744 if (account_mem) {
6745 ret = io_account_mem(user,
6746 ring_pages(p->sq_entries, p->cq_entries));
6747 if (ret) {
6748 free_uid(user);
6749 return ret;
6750 }
6751 }
6752
6753 ctx = io_ring_ctx_alloc(p);
6754 if (!ctx) {
6755 if (account_mem)
6756 io_unaccount_mem(user, ring_pages(p->sq_entries,
6757 p->cq_entries));
6758 free_uid(user);
6759 return -ENOMEM;
6760 }
6761 ctx->compat = in_compat_syscall();
6762 ctx->account_mem = account_mem;
6763 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07006764 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07006765
6766 ret = io_allocate_scq_urings(ctx, p);
6767 if (ret)
6768 goto err;
6769
Jens Axboe6c271ce2019-01-10 11:22:30 -07006770 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006771 if (ret)
6772 goto err;
6773
Jens Axboe2b188cc2019-01-07 10:46:33 -07006774 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00006775 p->sq_off.head = offsetof(struct io_rings, sq.head);
6776 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
6777 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
6778 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
6779 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
6780 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
6781 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006782
6783 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00006784 p->cq_off.head = offsetof(struct io_rings, cq.head);
6785 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
6786 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
6787 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
6788 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
6789 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06006790
Jens Axboe044c1ab2019-10-28 09:15:33 -06006791 /*
6792 * Install ring fd as the very last thing, so we don't risk someone
6793 * having closed it before we finish setup
6794 */
6795 ret = io_uring_get_fd(ctx);
6796 if (ret < 0)
6797 goto err;
6798
Jens Axboeda8c9692019-12-02 18:51:26 -07006799 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
Jens Axboecccf0ee2020-01-27 16:34:48 -07006800 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
6801 IORING_FEAT_CUR_PERSONALITY;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006802 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006803 return ret;
6804err:
6805 io_ring_ctx_wait_and_kill(ctx);
6806 return ret;
6807}
6808
6809/*
6810 * Sets up an aio uring context, and returns the fd. Applications asks for a
6811 * ring size, we return the actual sq/cq ring sizes (among other things) in the
6812 * params structure passed in.
6813 */
6814static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
6815{
6816 struct io_uring_params p;
6817 long ret;
6818 int i;
6819
6820 if (copy_from_user(&p, params, sizeof(p)))
6821 return -EFAULT;
6822 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
6823 if (p.resv[i])
6824 return -EINVAL;
6825 }
6826
Jens Axboe6c271ce2019-01-10 11:22:30 -07006827 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07006828 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Pavel Begunkov24369c22020-01-28 03:15:48 +03006829 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006830 return -EINVAL;
6831
6832 ret = io_uring_create(entries, &p);
6833 if (ret < 0)
6834 return ret;
6835
6836 if (copy_to_user(params, &p, sizeof(p)))
6837 return -EFAULT;
6838
6839 return ret;
6840}
6841
6842SYSCALL_DEFINE2(io_uring_setup, u32, entries,
6843 struct io_uring_params __user *, params)
6844{
6845 return io_uring_setup(entries, params);
6846}
6847
Jens Axboe66f4af92020-01-16 15:36:52 -07006848static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
6849{
6850 struct io_uring_probe *p;
6851 size_t size;
6852 int i, ret;
6853
6854 size = struct_size(p, ops, nr_args);
6855 if (size == SIZE_MAX)
6856 return -EOVERFLOW;
6857 p = kzalloc(size, GFP_KERNEL);
6858 if (!p)
6859 return -ENOMEM;
6860
6861 ret = -EFAULT;
6862 if (copy_from_user(p, arg, size))
6863 goto out;
6864 ret = -EINVAL;
6865 if (memchr_inv(p, 0, size))
6866 goto out;
6867
6868 p->last_op = IORING_OP_LAST - 1;
6869 if (nr_args > IORING_OP_LAST)
6870 nr_args = IORING_OP_LAST;
6871
6872 for (i = 0; i < nr_args; i++) {
6873 p->ops[i].op = i;
6874 if (!io_op_defs[i].not_supported)
6875 p->ops[i].flags = IO_URING_OP_SUPPORTED;
6876 }
6877 p->ops_len = i;
6878
6879 ret = 0;
6880 if (copy_to_user(arg, p, size))
6881 ret = -EFAULT;
6882out:
6883 kfree(p);
6884 return ret;
6885}
6886
Jens Axboe071698e2020-01-28 10:04:42 -07006887static int io_register_personality(struct io_ring_ctx *ctx)
6888{
6889 const struct cred *creds = get_current_cred();
6890 int id;
6891
6892 id = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1,
6893 USHRT_MAX, GFP_KERNEL);
6894 if (id < 0)
6895 put_cred(creds);
6896 return id;
6897}
6898
6899static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
6900{
6901 const struct cred *old_creds;
6902
6903 old_creds = idr_remove(&ctx->personality_idr, id);
6904 if (old_creds) {
6905 put_cred(old_creds);
6906 return 0;
6907 }
6908
6909 return -EINVAL;
6910}
6911
6912static bool io_register_op_must_quiesce(int op)
6913{
6914 switch (op) {
6915 case IORING_UNREGISTER_FILES:
6916 case IORING_REGISTER_FILES_UPDATE:
6917 case IORING_REGISTER_PROBE:
6918 case IORING_REGISTER_PERSONALITY:
6919 case IORING_UNREGISTER_PERSONALITY:
6920 return false;
6921 default:
6922 return true;
6923 }
6924}
6925
Jens Axboeedafcce2019-01-09 09:16:05 -07006926static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
6927 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06006928 __releases(ctx->uring_lock)
6929 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07006930{
6931 int ret;
6932
Jens Axboe35fa71a2019-04-22 10:23:23 -06006933 /*
6934 * We're inside the ring mutex, if the ref is already dying, then
6935 * someone else killed the ctx or is already going through
6936 * io_uring_register().
6937 */
6938 if (percpu_ref_is_dying(&ctx->refs))
6939 return -ENXIO;
6940
Jens Axboe071698e2020-01-28 10:04:42 -07006941 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07006942 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06006943
Jens Axboe05f3fb32019-12-09 11:22:50 -07006944 /*
6945 * Drop uring mutex before waiting for references to exit. If
6946 * another thread is currently inside io_uring_enter() it might
6947 * need to grab the uring_lock to make progress. If we hold it
6948 * here across the drain wait, then we can deadlock. It's safe
6949 * to drop the mutex here, since no new references will come in
6950 * after we've killed the percpu ref.
6951 */
6952 mutex_unlock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07006953 ret = wait_for_completion_interruptible(&ctx->completions[0]);
Jens Axboe05f3fb32019-12-09 11:22:50 -07006954 mutex_lock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07006955 if (ret) {
6956 percpu_ref_resurrect(&ctx->refs);
6957 ret = -EINTR;
6958 goto out;
6959 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07006960 }
Jens Axboeedafcce2019-01-09 09:16:05 -07006961
6962 switch (opcode) {
6963 case IORING_REGISTER_BUFFERS:
6964 ret = io_sqe_buffer_register(ctx, arg, nr_args);
6965 break;
6966 case IORING_UNREGISTER_BUFFERS:
6967 ret = -EINVAL;
6968 if (arg || nr_args)
6969 break;
6970 ret = io_sqe_buffer_unregister(ctx);
6971 break;
Jens Axboe6b063142019-01-10 22:13:58 -07006972 case IORING_REGISTER_FILES:
6973 ret = io_sqe_files_register(ctx, arg, nr_args);
6974 break;
6975 case IORING_UNREGISTER_FILES:
6976 ret = -EINVAL;
6977 if (arg || nr_args)
6978 break;
6979 ret = io_sqe_files_unregister(ctx);
6980 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06006981 case IORING_REGISTER_FILES_UPDATE:
6982 ret = io_sqe_files_update(ctx, arg, nr_args);
6983 break;
Jens Axboe9b402842019-04-11 11:45:41 -06006984 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -07006985 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -06006986 ret = -EINVAL;
6987 if (nr_args != 1)
6988 break;
6989 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -07006990 if (ret)
6991 break;
6992 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
6993 ctx->eventfd_async = 1;
6994 else
6995 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -06006996 break;
6997 case IORING_UNREGISTER_EVENTFD:
6998 ret = -EINVAL;
6999 if (arg || nr_args)
7000 break;
7001 ret = io_eventfd_unregister(ctx);
7002 break;
Jens Axboe66f4af92020-01-16 15:36:52 -07007003 case IORING_REGISTER_PROBE:
7004 ret = -EINVAL;
7005 if (!arg || nr_args > 256)
7006 break;
7007 ret = io_probe(ctx, arg, nr_args);
7008 break;
Jens Axboe071698e2020-01-28 10:04:42 -07007009 case IORING_REGISTER_PERSONALITY:
7010 ret = -EINVAL;
7011 if (arg || nr_args)
7012 break;
7013 ret = io_register_personality(ctx);
7014 break;
7015 case IORING_UNREGISTER_PERSONALITY:
7016 ret = -EINVAL;
7017 if (arg)
7018 break;
7019 ret = io_unregister_personality(ctx, nr_args);
7020 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07007021 default:
7022 ret = -EINVAL;
7023 break;
7024 }
7025
Jens Axboe071698e2020-01-28 10:04:42 -07007026 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007027 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -07007028 percpu_ref_reinit(&ctx->refs);
Jens Axboec1503682020-01-08 08:26:07 -07007029out:
7030 reinit_completion(&ctx->completions[0]);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007031 }
Jens Axboeedafcce2019-01-09 09:16:05 -07007032 return ret;
7033}
7034
7035SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
7036 void __user *, arg, unsigned int, nr_args)
7037{
7038 struct io_ring_ctx *ctx;
7039 long ret = -EBADF;
7040 struct fd f;
7041
7042 f = fdget(fd);
7043 if (!f.file)
7044 return -EBADF;
7045
7046 ret = -EOPNOTSUPP;
7047 if (f.file->f_op != &io_uring_fops)
7048 goto out_fput;
7049
7050 ctx = f.file->private_data;
7051
7052 mutex_lock(&ctx->uring_lock);
7053 ret = __io_uring_register(ctx, opcode, arg, nr_args);
7054 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02007055 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
7056 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07007057out_fput:
7058 fdput(f);
7059 return ret;
7060}
7061
Jens Axboe2b188cc2019-01-07 10:46:33 -07007062static int __init io_uring_init(void)
7063{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01007064#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
7065 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
7066 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
7067} while (0)
7068
7069#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
7070 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
7071 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
7072 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
7073 BUILD_BUG_SQE_ELEM(1, __u8, flags);
7074 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
7075 BUILD_BUG_SQE_ELEM(4, __s32, fd);
7076 BUILD_BUG_SQE_ELEM(8, __u64, off);
7077 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
7078 BUILD_BUG_SQE_ELEM(16, __u64, addr);
7079 BUILD_BUG_SQE_ELEM(24, __u32, len);
7080 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
7081 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
7082 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
7083 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
7084 BUILD_BUG_SQE_ELEM(28, __u16, poll_events);
7085 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
7086 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
7087 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
7088 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
7089 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
7090 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
7091 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
7092 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
7093 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
7094 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
7095 BUILD_BUG_SQE_ELEM(42, __u16, personality);
7096
Jens Axboed3656342019-12-18 09:50:26 -07007097 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007098 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
7099 return 0;
7100};
7101__initcall(io_uring_init);