blob: 5f2c0afefae1f3bb738d0a3b12652518b0d0cda7 [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 Axboeff002b32020-02-07 16:05:21 -070078#include <linux/fs_struct.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070079
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020080#define CREATE_TRACE_POINTS
81#include <trace/events/io_uring.h>
82
Jens Axboe2b188cc2019-01-07 10:46:33 -070083#include <uapi/linux/io_uring.h>
84
85#include "internal.h"
Jens Axboe561fb042019-10-24 07:25:42 -060086#include "io-wq.h"
Jens Axboe2b188cc2019-01-07 10:46:33 -070087
Daniel Xu5277dea2019-09-14 14:23:45 -070088#define IORING_MAX_ENTRIES 32768
Jens Axboe33a107f2019-10-04 12:10:03 -060089#define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES)
Jens Axboe65e19f52019-10-26 07:20:21 -060090
91/*
92 * Shift of 9 is 512 entries, or exactly one page on 64-bit archs
93 */
94#define IORING_FILE_TABLE_SHIFT 9
95#define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT)
96#define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1)
97#define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE)
Jens Axboe2b188cc2019-01-07 10:46:33 -070098
99struct io_uring {
100 u32 head ____cacheline_aligned_in_smp;
101 u32 tail ____cacheline_aligned_in_smp;
102};
103
Stefan Bühler1e84b972019-04-24 23:54:16 +0200104/*
Hristo Venev75b28af2019-08-26 17:23:46 +0000105 * This data is shared with the application through the mmap at offsets
106 * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200107 *
108 * The offsets to the member fields are published through struct
109 * io_sqring_offsets when calling io_uring_setup.
110 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000111struct io_rings {
Stefan Bühler1e84b972019-04-24 23:54:16 +0200112 /*
113 * Head and tail offsets into the ring; the offsets need to be
114 * masked to get valid indices.
115 *
Hristo Venev75b28af2019-08-26 17:23:46 +0000116 * The kernel controls head of the sq ring and the tail of the cq ring,
117 * and the application controls tail of the sq ring and the head of the
118 * cq ring.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200119 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000120 struct io_uring sq, cq;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200121 /*
Hristo Venev75b28af2019-08-26 17:23:46 +0000122 * Bitmasks to apply to head and tail offsets (constant, equals
Stefan Bühler1e84b972019-04-24 23:54:16 +0200123 * ring_entries - 1)
124 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000125 u32 sq_ring_mask, cq_ring_mask;
126 /* Ring sizes (constant, power of 2) */
127 u32 sq_ring_entries, cq_ring_entries;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200128 /*
129 * Number of invalid entries dropped by the kernel due to
130 * invalid index stored in array
131 *
132 * Written by the kernel, shouldn't be modified by the
133 * application (i.e. get number of "new events" by comparing to
134 * cached value).
135 *
136 * After a new SQ head value was read by the application this
137 * counter includes all submissions that were dropped reaching
138 * the new SQ head (and possibly more).
139 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000140 u32 sq_dropped;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200141 /*
142 * Runtime flags
143 *
144 * Written by the kernel, shouldn't be modified by the
145 * application.
146 *
147 * The application needs a full memory barrier before checking
148 * for IORING_SQ_NEED_WAKEUP after updating the sq tail.
149 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000150 u32 sq_flags;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200151 /*
152 * Number of completion events lost because the queue was full;
153 * this should be avoided by the application by making sure
LimingWu0b4295b2019-12-05 20:18:18 +0800154 * there are not more requests pending than there is space in
Stefan Bühler1e84b972019-04-24 23:54:16 +0200155 * the completion queue.
156 *
157 * Written by the kernel, shouldn't be modified by the
158 * application (i.e. get number of "new events" by comparing to
159 * cached value).
160 *
161 * As completion events come in out of order this counter is not
162 * ordered with any other data.
163 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000164 u32 cq_overflow;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200165 /*
166 * Ring buffer of completion events.
167 *
168 * The kernel writes completion events fresh every time they are
169 * produced, so the application is allowed to modify pending
170 * entries.
171 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000172 struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700173};
174
Jens Axboeedafcce2019-01-09 09:16:05 -0700175struct io_mapped_ubuf {
176 u64 ubuf;
177 size_t len;
178 struct bio_vec *bvec;
179 unsigned int nr_bvecs;
180};
181
Jens Axboe65e19f52019-10-26 07:20:21 -0600182struct fixed_file_table {
183 struct file **files;
Jens Axboe31b51512019-01-18 22:56:34 -0700184};
185
Jens Axboe05f3fb32019-12-09 11:22:50 -0700186struct fixed_file_data {
187 struct fixed_file_table *table;
188 struct io_ring_ctx *ctx;
189
190 struct percpu_ref refs;
191 struct llist_head put_llist;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700192 struct work_struct ref_work;
193 struct completion done;
194};
195
Jens Axboe2b188cc2019-01-07 10:46:33 -0700196struct io_ring_ctx {
197 struct {
198 struct percpu_ref refs;
199 } ____cacheline_aligned_in_smp;
200
201 struct {
202 unsigned int flags;
Randy Dunlape1d85332020-02-05 20:57:10 -0800203 unsigned int compat: 1;
204 unsigned int account_mem: 1;
205 unsigned int cq_overflow_flushed: 1;
206 unsigned int drain_next: 1;
207 unsigned int eventfd_async: 1;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700208
Hristo Venev75b28af2019-08-26 17:23:46 +0000209 /*
210 * Ring buffer of indices into array of io_uring_sqe, which is
211 * mmapped by the application using the IORING_OFF_SQES offset.
212 *
213 * This indirection could e.g. be used to assign fixed
214 * io_uring_sqe entries to operations and only submit them to
215 * the queue when needed.
216 *
217 * The kernel modifies neither the indices array nor the entries
218 * array.
219 */
220 u32 *sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700221 unsigned cached_sq_head;
222 unsigned sq_entries;
223 unsigned sq_mask;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700224 unsigned sq_thread_idle;
Jens Axboe498ccd92019-10-25 10:04:25 -0600225 unsigned cached_sq_dropped;
Jens Axboe206aefd2019-11-07 18:27:42 -0700226 atomic_t cached_cq_overflow;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700227 unsigned long sq_check_overflow;
Jens Axboede0617e2019-04-06 21:51:27 -0600228
229 struct list_head defer_list;
Jens Axboe5262f562019-09-17 12:26:57 -0600230 struct list_head timeout_list;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700231 struct list_head cq_overflow_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700232
Jens Axboefcb323c2019-10-24 12:39:47 -0600233 wait_queue_head_t inflight_wait;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700234 struct io_uring_sqe *sq_sqes;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700235 } ____cacheline_aligned_in_smp;
236
Hristo Venev75b28af2019-08-26 17:23:46 +0000237 struct io_rings *rings;
238
Jens Axboe2b188cc2019-01-07 10:46:33 -0700239 /* IO offload */
Jens Axboe561fb042019-10-24 07:25:42 -0600240 struct io_wq *io_wq;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700241 struct task_struct *sqo_thread; /* if using sq thread polling */
242 struct mm_struct *sqo_mm;
243 wait_queue_head_t sqo_wait;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700244
Jens Axboe6b063142019-01-10 22:13:58 -0700245 /*
246 * If used, fixed file set. Writers must ensure that ->refs is dead,
247 * readers must ensure that ->refs is alive as long as the file* is
248 * used. Only updated through io_uring_register(2).
249 */
Jens Axboe05f3fb32019-12-09 11:22:50 -0700250 struct fixed_file_data *file_data;
Jens Axboe6b063142019-01-10 22:13:58 -0700251 unsigned nr_user_files;
Pavel Begunkovb14cca02020-01-17 04:45:59 +0300252 int ring_fd;
253 struct file *ring_file;
Jens Axboe6b063142019-01-10 22:13:58 -0700254
Jens Axboeedafcce2019-01-09 09:16:05 -0700255 /* if used, fixed mapped user buffers */
256 unsigned nr_user_bufs;
257 struct io_mapped_ubuf *user_bufs;
258
Jens Axboe2b188cc2019-01-07 10:46:33 -0700259 struct user_struct *user;
260
Jens Axboe0b8c0ec2019-12-02 08:50:00 -0700261 const struct cred *creds;
Jens Axboe181e4482019-11-25 08:52:30 -0700262
Jens Axboe206aefd2019-11-07 18:27:42 -0700263 /* 0 is for ctx quiesce/reinit/free, 1 is for sqo_thread started */
264 struct completion *completions;
265
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700266 /* if all else fails... */
267 struct io_kiocb *fallback_req;
268
Jens Axboe206aefd2019-11-07 18:27:42 -0700269#if defined(CONFIG_UNIX)
270 struct socket *ring_sock;
271#endif
272
Jens Axboe071698e2020-01-28 10:04:42 -0700273 struct idr personality_idr;
274
Jens Axboe206aefd2019-11-07 18:27:42 -0700275 struct {
276 unsigned cached_cq_tail;
277 unsigned cq_entries;
278 unsigned cq_mask;
279 atomic_t cq_timeouts;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700280 unsigned long cq_check_overflow;
Jens Axboe206aefd2019-11-07 18:27:42 -0700281 struct wait_queue_head cq_wait;
282 struct fasync_struct *cq_fasync;
283 struct eventfd_ctx *cq_ev_fd;
284 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700285
286 struct {
287 struct mutex uring_lock;
288 wait_queue_head_t wait;
289 } ____cacheline_aligned_in_smp;
290
291 struct {
292 spinlock_t completion_lock;
Jens Axboee94f1412019-12-19 12:06:02 -0700293 struct llist_head poll_llist;
294
Jens Axboedef596e2019-01-09 08:59:42 -0700295 /*
296 * ->poll_list is protected by the ctx->uring_lock for
297 * io_uring instances that don't use IORING_SETUP_SQPOLL.
298 * For SQPOLL, only the single threaded io_sq_thread() will
299 * manipulate the list, hence no extra locking is needed there.
300 */
301 struct list_head poll_list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700302 struct hlist_head *cancel_hash;
303 unsigned cancel_hash_bits;
Jens Axboee94f1412019-12-19 12:06:02 -0700304 bool poll_multi_file;
Jens Axboefcb323c2019-10-24 12:39:47 -0600305
306 spinlock_t inflight_lock;
307 struct list_head inflight_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700308 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700309};
310
Jens Axboe09bb8392019-03-13 12:39:28 -0600311/*
312 * First field must be the file pointer in all the
313 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
314 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700315struct io_poll_iocb {
316 struct file *file;
Jens Axboe0969e782019-12-17 18:40:57 -0700317 union {
318 struct wait_queue_head *head;
319 u64 addr;
320 };
Jens Axboe221c5eb2019-01-17 09:41:58 -0700321 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600322 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700323 bool canceled;
Jens Axboe392edb42019-12-09 17:52:20 -0700324 struct wait_queue_entry wait;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700325};
326
Jens Axboeb5dba592019-12-11 14:02:38 -0700327struct io_close {
328 struct file *file;
329 struct file *put_file;
330 int fd;
331};
332
Jens Axboead8a48a2019-11-15 08:49:11 -0700333struct io_timeout_data {
334 struct io_kiocb *req;
335 struct hrtimer timer;
336 struct timespec64 ts;
337 enum hrtimer_mode mode;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +0300338 u32 seq_offset;
Jens Axboead8a48a2019-11-15 08:49:11 -0700339};
340
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700341struct io_accept {
342 struct file *file;
343 struct sockaddr __user *addr;
344 int __user *addr_len;
345 int flags;
346};
347
348struct io_sync {
349 struct file *file;
350 loff_t len;
351 loff_t off;
352 int flags;
Jens Axboed63d1b52019-12-10 10:38:56 -0700353 int mode;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700354};
355
Jens Axboefbf23842019-12-17 18:45:56 -0700356struct io_cancel {
357 struct file *file;
358 u64 addr;
359};
360
Jens Axboeb29472e2019-12-17 18:50:29 -0700361struct io_timeout {
362 struct file *file;
363 u64 addr;
364 int flags;
Jens Axboe26a61672019-12-20 09:02:01 -0700365 unsigned count;
Jens Axboeb29472e2019-12-17 18:50:29 -0700366};
367
Jens Axboe9adbd452019-12-20 08:45:55 -0700368struct io_rw {
369 /* NOTE: kiocb has the file as the first member, so don't do it here */
370 struct kiocb kiocb;
371 u64 addr;
372 u64 len;
373};
374
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700375struct io_connect {
376 struct file *file;
377 struct sockaddr __user *addr;
378 int addr_len;
379};
380
Jens Axboee47293f2019-12-20 08:58:21 -0700381struct io_sr_msg {
382 struct file *file;
Jens Axboefddafac2020-01-04 20:19:44 -0700383 union {
384 struct user_msghdr __user *msg;
385 void __user *buf;
386 };
Jens Axboee47293f2019-12-20 08:58:21 -0700387 int msg_flags;
Jens Axboefddafac2020-01-04 20:19:44 -0700388 size_t len;
Jens Axboee47293f2019-12-20 08:58:21 -0700389};
390
Jens Axboe15b71ab2019-12-11 11:20:36 -0700391struct io_open {
392 struct file *file;
393 int dfd;
Jens Axboeeddc7ef2019-12-13 21:18:10 -0700394 union {
Jens Axboeeddc7ef2019-12-13 21:18:10 -0700395 unsigned mask;
396 };
Jens Axboe15b71ab2019-12-11 11:20:36 -0700397 struct filename *filename;
Jens Axboeeddc7ef2019-12-13 21:18:10 -0700398 struct statx __user *buffer;
Jens Axboec12cedf2020-01-08 17:41:21 -0700399 struct open_how how;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700400};
401
Jens Axboe05f3fb32019-12-09 11:22:50 -0700402struct io_files_update {
403 struct file *file;
404 u64 arg;
405 u32 nr_args;
406 u32 offset;
407};
408
Jens Axboe4840e412019-12-25 22:03:45 -0700409struct io_fadvise {
410 struct file *file;
411 u64 offset;
412 u32 len;
413 u32 advice;
414};
415
Jens Axboec1ca7572019-12-25 22:18:28 -0700416struct io_madvise {
417 struct file *file;
418 u64 addr;
419 u32 len;
420 u32 advice;
421};
422
Jens Axboe3e4827b2020-01-08 15:18:09 -0700423struct io_epoll {
424 struct file *file;
425 int epfd;
426 int op;
427 int fd;
428 struct epoll_event event;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700429};
430
Jens Axboef499a022019-12-02 16:28:46 -0700431struct io_async_connect {
432 struct sockaddr_storage address;
433};
434
Jens Axboe03b12302019-12-02 18:50:25 -0700435struct io_async_msghdr {
436 struct iovec fast_iov[UIO_FASTIOV];
437 struct iovec *iov;
438 struct sockaddr __user *uaddr;
439 struct msghdr msg;
Jens Axboeb5379162020-02-09 11:29:15 -0700440 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -0700441};
442
Jens Axboef67676d2019-12-02 11:03:47 -0700443struct io_async_rw {
444 struct iovec fast_iov[UIO_FASTIOV];
445 struct iovec *iov;
446 ssize_t nr_segs;
447 ssize_t size;
448};
449
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700450struct io_async_ctx {
Jens Axboef67676d2019-12-02 11:03:47 -0700451 union {
452 struct io_async_rw rw;
Jens Axboe03b12302019-12-02 18:50:25 -0700453 struct io_async_msghdr msg;
Jens Axboef499a022019-12-02 16:28:46 -0700454 struct io_async_connect connect;
Jens Axboe2d283902019-12-04 11:08:05 -0700455 struct io_timeout_data timeout;
Jens Axboef67676d2019-12-02 11:03:47 -0700456 };
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700457};
458
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300459enum {
460 REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT,
461 REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT,
462 REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT,
463 REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT,
464 REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT,
465
466 REQ_F_LINK_NEXT_BIT,
467 REQ_F_FAIL_LINK_BIT,
468 REQ_F_INFLIGHT_BIT,
469 REQ_F_CUR_POS_BIT,
470 REQ_F_NOWAIT_BIT,
471 REQ_F_IOPOLL_COMPLETED_BIT,
472 REQ_F_LINK_TIMEOUT_BIT,
473 REQ_F_TIMEOUT_BIT,
474 REQ_F_ISREG_BIT,
475 REQ_F_MUST_PUNT_BIT,
476 REQ_F_TIMEOUT_NOSEQ_BIT,
477 REQ_F_COMP_LOCKED_BIT,
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300478 REQ_F_NEED_CLEANUP_BIT,
Jens Axboe2ca10252020-02-13 17:17:35 -0700479 REQ_F_OVERFLOW_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300480};
481
482enum {
483 /* ctx owns file */
484 REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT),
485 /* drain existing IO first */
486 REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT),
487 /* linked sqes */
488 REQ_F_LINK = BIT(REQ_F_LINK_BIT),
489 /* doesn't sever on completion < 0 */
490 REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT),
491 /* IOSQE_ASYNC */
492 REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT),
493
494 /* already grabbed next link */
495 REQ_F_LINK_NEXT = BIT(REQ_F_LINK_NEXT_BIT),
496 /* fail rest of links */
497 REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT),
498 /* on inflight list */
499 REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT),
500 /* read/write uses file position */
501 REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT),
502 /* must not punt to workers */
503 REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
504 /* polled IO has completed */
505 REQ_F_IOPOLL_COMPLETED = BIT(REQ_F_IOPOLL_COMPLETED_BIT),
506 /* has linked timeout */
507 REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT),
508 /* timeout request */
509 REQ_F_TIMEOUT = BIT(REQ_F_TIMEOUT_BIT),
510 /* regular file */
511 REQ_F_ISREG = BIT(REQ_F_ISREG_BIT),
512 /* must be punted even for NONBLOCK */
513 REQ_F_MUST_PUNT = BIT(REQ_F_MUST_PUNT_BIT),
514 /* no timeout sequence */
515 REQ_F_TIMEOUT_NOSEQ = BIT(REQ_F_TIMEOUT_NOSEQ_BIT),
516 /* completion under lock */
517 REQ_F_COMP_LOCKED = BIT(REQ_F_COMP_LOCKED_BIT),
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300518 /* needs cleanup */
519 REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT),
Jens Axboe2ca10252020-02-13 17:17:35 -0700520 /* in overflow list */
521 REQ_F_OVERFLOW = BIT(REQ_F_OVERFLOW_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300522};
523
Jens Axboe09bb8392019-03-13 12:39:28 -0600524/*
525 * NOTE! Each of the iocb union members has the file pointer
526 * as the first entry in their struct definition. So you can
527 * access the file pointer through any of the sub-structs,
528 * or directly as just 'ki_filp' in this struct.
529 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700530struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700531 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600532 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700533 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700534 struct io_poll_iocb poll;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700535 struct io_accept accept;
536 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700537 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700538 struct io_timeout timeout;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700539 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700540 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700541 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700542 struct io_close close;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700543 struct io_files_update files_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700544 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700545 struct io_madvise madvise;
Jens Axboe3e4827b2020-01-08 15:18:09 -0700546 struct io_epoll epoll;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700547 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700548
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700549 struct io_async_ctx *io;
Pavel Begunkovb14cca02020-01-17 04:45:59 +0300550 /*
551 * llist_node is only used for poll deferred completions
552 */
553 struct llist_node llist_node;
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300554 bool needs_fixed_file;
Jens Axboed625c6e2019-12-17 19:53:05 -0700555 u8 opcode;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700556
557 struct io_ring_ctx *ctx;
Jens Axboeeac406c2019-11-14 12:09:58 -0700558 union {
559 struct list_head list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700560 struct hlist_node hash_node;
Jens Axboeeac406c2019-11-14 12:09:58 -0700561 };
Jens Axboe9e645e112019-05-10 16:07:28 -0600562 struct list_head link_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700563 unsigned int flags;
Jens Axboec16361c2019-01-17 08:39:48 -0700564 refcount_t refs;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700565 u64 user_data;
Jens Axboe9e645e112019-05-10 16:07:28 -0600566 u32 result;
Jens Axboede0617e2019-04-06 21:51:27 -0600567 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700568
Jens Axboefcb323c2019-10-24 12:39:47 -0600569 struct list_head inflight_entry;
570
Jens Axboe561fb042019-10-24 07:25:42 -0600571 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700572};
573
574#define IO_PLUG_THRESHOLD 2
Jens Axboedef596e2019-01-09 08:59:42 -0700575#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700576
Jens Axboe9a56a232019-01-09 09:06:50 -0700577struct io_submit_state {
578 struct blk_plug plug;
579
580 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700581 * io_kiocb alloc cache
582 */
583 void *reqs[IO_IOPOLL_BATCH];
Pavel Begunkov6c8a3132020-02-01 03:58:00 +0300584 unsigned int free_reqs;
Jens Axboe2579f912019-01-09 09:10:43 -0700585
586 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700587 * File reference cache
588 */
589 struct file *file;
590 unsigned int fd;
591 unsigned int has_refs;
592 unsigned int used_refs;
593 unsigned int ios_left;
594};
595
Jens Axboed3656342019-12-18 09:50:26 -0700596struct io_op_def {
597 /* needs req->io allocated for deferral/async */
598 unsigned async_ctx : 1;
599 /* needs current->mm setup, does mm access */
600 unsigned needs_mm : 1;
601 /* needs req->file assigned */
602 unsigned needs_file : 1;
603 /* needs req->file assigned IFF fd is >= 0 */
604 unsigned fd_non_neg : 1;
605 /* hash wq insertion if file is a regular file */
606 unsigned hash_reg_file : 1;
607 /* unbound wq insertion if file is a non-regular file */
608 unsigned unbound_nonreg_file : 1;
Jens Axboe66f4af92020-01-16 15:36:52 -0700609 /* opcode is not supported by this kernel */
610 unsigned not_supported : 1;
Jens Axboef86cd202020-01-29 13:46:44 -0700611 /* needs file table */
612 unsigned file_table : 1;
Jens Axboeff002b32020-02-07 16:05:21 -0700613 /* needs ->fs */
614 unsigned needs_fs : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700615};
616
617static const struct io_op_def io_op_defs[] = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300618 [IORING_OP_NOP] = {},
619 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700620 .async_ctx = 1,
621 .needs_mm = 1,
622 .needs_file = 1,
623 .unbound_nonreg_file = 1,
624 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300625 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700626 .async_ctx = 1,
627 .needs_mm = 1,
628 .needs_file = 1,
629 .hash_reg_file = 1,
630 .unbound_nonreg_file = 1,
631 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300632 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700633 .needs_file = 1,
634 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300635 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700636 .needs_file = 1,
637 .unbound_nonreg_file = 1,
638 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300639 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700640 .needs_file = 1,
641 .hash_reg_file = 1,
642 .unbound_nonreg_file = 1,
643 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300644 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700645 .needs_file = 1,
646 .unbound_nonreg_file = 1,
647 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300648 [IORING_OP_POLL_REMOVE] = {},
649 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700650 .needs_file = 1,
651 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300652 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700653 .async_ctx = 1,
654 .needs_mm = 1,
655 .needs_file = 1,
656 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700657 .needs_fs = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700658 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300659 [IORING_OP_RECVMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700660 .async_ctx = 1,
661 .needs_mm = 1,
662 .needs_file = 1,
663 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700664 .needs_fs = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700665 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300666 [IORING_OP_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700667 .async_ctx = 1,
668 .needs_mm = 1,
669 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300670 [IORING_OP_TIMEOUT_REMOVE] = {},
671 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700672 .needs_mm = 1,
673 .needs_file = 1,
674 .unbound_nonreg_file = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700675 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700676 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300677 [IORING_OP_ASYNC_CANCEL] = {},
678 [IORING_OP_LINK_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700679 .async_ctx = 1,
680 .needs_mm = 1,
681 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300682 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700683 .async_ctx = 1,
684 .needs_mm = 1,
685 .needs_file = 1,
686 .unbound_nonreg_file = 1,
687 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300688 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700689 .needs_file = 1,
690 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300691 [IORING_OP_OPENAT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700692 .needs_file = 1,
693 .fd_non_neg = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700694 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700695 .needs_fs = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700696 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300697 [IORING_OP_CLOSE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700698 .needs_file = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700699 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700700 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300701 [IORING_OP_FILES_UPDATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700702 .needs_mm = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700703 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700704 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300705 [IORING_OP_STATX] = {
Jens Axboed3656342019-12-18 09:50:26 -0700706 .needs_mm = 1,
707 .needs_file = 1,
708 .fd_non_neg = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700709 .needs_fs = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700710 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300711 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700712 .needs_mm = 1,
713 .needs_file = 1,
714 .unbound_nonreg_file = 1,
715 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300716 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700717 .needs_mm = 1,
718 .needs_file = 1,
719 .unbound_nonreg_file = 1,
720 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300721 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -0700722 .needs_file = 1,
723 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300724 [IORING_OP_MADVISE] = {
Jens Axboec1ca7572019-12-25 22:18:28 -0700725 .needs_mm = 1,
726 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300727 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700728 .needs_mm = 1,
729 .needs_file = 1,
730 .unbound_nonreg_file = 1,
731 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300732 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700733 .needs_mm = 1,
734 .needs_file = 1,
735 .unbound_nonreg_file = 1,
736 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300737 [IORING_OP_OPENAT2] = {
Jens Axboecebdb982020-01-08 17:59:24 -0700738 .needs_file = 1,
739 .fd_non_neg = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700740 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700741 .needs_fs = 1,
Jens Axboecebdb982020-01-08 17:59:24 -0700742 },
Jens Axboe3e4827b2020-01-08 15:18:09 -0700743 [IORING_OP_EPOLL_CTL] = {
744 .unbound_nonreg_file = 1,
745 .file_table = 1,
746 },
Jens Axboed3656342019-12-18 09:50:26 -0700747};
748
Jens Axboe561fb042019-10-24 07:25:42 -0600749static void io_wq_submit_work(struct io_wq_work **workptr);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700750static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800751static void io_put_req(struct io_kiocb *req);
Jens Axboe978db572019-11-14 22:39:04 -0700752static void __io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700753static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
754static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -0700755static int __io_sqe_files_update(struct io_ring_ctx *ctx,
756 struct io_uring_files_update *ip,
757 unsigned nr_args);
Jens Axboef86cd202020-01-29 13:46:44 -0700758static int io_grab_files(struct io_kiocb *req);
Jens Axboe2faf8522020-02-04 19:54:55 -0700759static void io_ring_file_ref_flush(struct fixed_file_data *data);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300760static void io_cleanup_req(struct io_kiocb *req);
Jens Axboede0617e2019-04-06 21:51:27 -0600761
Jens Axboe2b188cc2019-01-07 10:46:33 -0700762static struct kmem_cache *req_cachep;
763
764static const struct file_operations io_uring_fops;
765
766struct sock *io_uring_get_socket(struct file *file)
767{
768#if defined(CONFIG_UNIX)
769 if (file->f_op == &io_uring_fops) {
770 struct io_ring_ctx *ctx = file->private_data;
771
772 return ctx->ring_sock->sk;
773 }
774#endif
775 return NULL;
776}
777EXPORT_SYMBOL(io_uring_get_socket);
778
779static void io_ring_ctx_ref_free(struct percpu_ref *ref)
780{
781 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
782
Jens Axboe206aefd2019-11-07 18:27:42 -0700783 complete(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700784}
785
786static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
787{
788 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -0700789 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700790
791 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
792 if (!ctx)
793 return NULL;
794
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700795 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
796 if (!ctx->fallback_req)
797 goto err;
798
Jens Axboe206aefd2019-11-07 18:27:42 -0700799 ctx->completions = kmalloc(2 * sizeof(struct completion), GFP_KERNEL);
800 if (!ctx->completions)
801 goto err;
802
Jens Axboe78076bb2019-12-04 19:56:40 -0700803 /*
804 * Use 5 bits less than the max cq entries, that should give us around
805 * 32 entries per hash list if totally full and uniformly spread.
806 */
807 hash_bits = ilog2(p->cq_entries);
808 hash_bits -= 5;
809 if (hash_bits <= 0)
810 hash_bits = 1;
811 ctx->cancel_hash_bits = hash_bits;
812 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
813 GFP_KERNEL);
814 if (!ctx->cancel_hash)
815 goto err;
816 __hash_init(ctx->cancel_hash, 1U << hash_bits);
817
Roman Gushchin21482892019-05-07 10:01:48 -0700818 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -0700819 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
820 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700821
822 ctx->flags = p->flags;
823 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700824 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe206aefd2019-11-07 18:27:42 -0700825 init_completion(&ctx->completions[0]);
826 init_completion(&ctx->completions[1]);
Jens Axboe071698e2020-01-28 10:04:42 -0700827 idr_init(&ctx->personality_idr);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700828 mutex_init(&ctx->uring_lock);
829 init_waitqueue_head(&ctx->wait);
830 spin_lock_init(&ctx->completion_lock);
Jens Axboee94f1412019-12-19 12:06:02 -0700831 init_llist_head(&ctx->poll_llist);
Jens Axboedef596e2019-01-09 08:59:42 -0700832 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboede0617e2019-04-06 21:51:27 -0600833 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -0600834 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -0600835 init_waitqueue_head(&ctx->inflight_wait);
836 spin_lock_init(&ctx->inflight_lock);
837 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700838 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -0700839err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700840 if (ctx->fallback_req)
841 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe206aefd2019-11-07 18:27:42 -0700842 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -0700843 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -0700844 kfree(ctx);
845 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700846}
847
Bob Liu9d858b22019-11-13 18:06:25 +0800848static inline bool __req_need_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -0600849{
Jackie Liua197f662019-11-08 08:09:12 -0700850 struct io_ring_ctx *ctx = req->ctx;
851
Jens Axboe498ccd92019-10-25 10:04:25 -0600852 return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped
853 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboede0617e2019-04-06 21:51:27 -0600854}
855
Bob Liu9d858b22019-11-13 18:06:25 +0800856static inline bool req_need_defer(struct io_kiocb *req)
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600857{
Pavel Begunkov87987892020-01-18 01:22:30 +0300858 if (unlikely(req->flags & REQ_F_IO_DRAIN))
Bob Liu9d858b22019-11-13 18:06:25 +0800859 return __req_need_defer(req);
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600860
Bob Liu9d858b22019-11-13 18:06:25 +0800861 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600862}
863
864static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -0600865{
866 struct io_kiocb *req;
867
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600868 req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list);
Bob Liu9d858b22019-11-13 18:06:25 +0800869 if (req && !req_need_defer(req)) {
Jens Axboede0617e2019-04-06 21:51:27 -0600870 list_del_init(&req->list);
871 return req;
872 }
873
874 return NULL;
875}
876
Jens Axboe5262f562019-09-17 12:26:57 -0600877static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx)
878{
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600879 struct io_kiocb *req;
880
881 req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list);
Jens Axboe93bd25b2019-11-11 23:34:31 -0700882 if (req) {
883 if (req->flags & REQ_F_TIMEOUT_NOSEQ)
884 return NULL;
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800885 if (!__req_need_defer(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -0700886 list_del_init(&req->list);
887 return req;
888 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600889 }
890
891 return NULL;
Jens Axboe5262f562019-09-17 12:26:57 -0600892}
893
Jens Axboede0617e2019-04-06 21:51:27 -0600894static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700895{
Hristo Venev75b28af2019-08-26 17:23:46 +0000896 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700897
Pavel Begunkov07910152020-01-17 03:52:46 +0300898 /* order cqe stores with ring update */
899 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700900
Pavel Begunkov07910152020-01-17 03:52:46 +0300901 if (wq_has_sleeper(&ctx->cq_wait)) {
902 wake_up_interruptible(&ctx->cq_wait);
903 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700904 }
905}
906
Jens Axboecccf0ee2020-01-27 16:34:48 -0700907static inline void io_req_work_grab_env(struct io_kiocb *req,
908 const struct io_op_def *def)
Jens Axboe18d9be12019-09-10 09:13:05 -0600909{
Jens Axboecccf0ee2020-01-27 16:34:48 -0700910 if (!req->work.mm && def->needs_mm) {
911 mmgrab(current->mm);
912 req->work.mm = current->mm;
913 }
914 if (!req->work.creds)
915 req->work.creds = get_current_cred();
Jens Axboeff002b32020-02-07 16:05:21 -0700916 if (!req->work.fs && def->needs_fs) {
917 spin_lock(&current->fs->lock);
918 if (!current->fs->in_exec) {
919 req->work.fs = current->fs;
920 req->work.fs->users++;
921 } else {
922 req->work.flags |= IO_WQ_WORK_CANCEL;
923 }
924 spin_unlock(&current->fs->lock);
925 }
Jens Axboe6ab23142020-02-08 20:23:59 -0700926 if (!req->work.task_pid)
927 req->work.task_pid = task_pid_vnr(current);
Jens Axboecccf0ee2020-01-27 16:34:48 -0700928}
929
930static inline void io_req_work_drop_env(struct io_kiocb *req)
931{
932 if (req->work.mm) {
933 mmdrop(req->work.mm);
934 req->work.mm = NULL;
935 }
936 if (req->work.creds) {
937 put_cred(req->work.creds);
938 req->work.creds = NULL;
939 }
Jens Axboeff002b32020-02-07 16:05:21 -0700940 if (req->work.fs) {
941 struct fs_struct *fs = req->work.fs;
942
943 spin_lock(&req->work.fs->lock);
944 if (--fs->users)
945 fs = NULL;
946 spin_unlock(&req->work.fs->lock);
947 if (fs)
948 free_fs_struct(fs);
949 }
Jens Axboe561fb042019-10-24 07:25:42 -0600950}
951
Pavel Begunkovdeb6dc02020-02-24 11:30:17 +0300952static inline void io_prep_next_work(struct io_kiocb *req,
953 struct io_kiocb **link)
954{
955 const struct io_op_def *def = &io_op_defs[req->opcode];
956
957 if (!(req->flags & REQ_F_ISREG) && def->unbound_nonreg_file)
958 req->work.flags |= IO_WQ_WORK_UNBOUND;
959
960 *link = io_prep_linked_timeout(req);
961}
962
Jens Axboe94ae5e72019-11-14 19:39:52 -0700963static inline bool io_prep_async_work(struct io_kiocb *req,
964 struct io_kiocb **link)
Jens Axboe561fb042019-10-24 07:25:42 -0600965{
Jens Axboed3656342019-12-18 09:50:26 -0700966 const struct io_op_def *def = &io_op_defs[req->opcode];
Jens Axboe561fb042019-10-24 07:25:42 -0600967 bool do_hashed = false;
Jens Axboe54a91f32019-09-10 09:15:04 -0600968
Jens Axboed3656342019-12-18 09:50:26 -0700969 if (req->flags & REQ_F_ISREG) {
970 if (def->hash_reg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -0700971 do_hashed = true;
Jens Axboed3656342019-12-18 09:50:26 -0700972 } else {
973 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -0700974 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -0600975 }
Jens Axboecccf0ee2020-01-27 16:34:48 -0700976
977 io_req_work_grab_env(req, def);
Jens Axboe54a91f32019-09-10 09:15:04 -0600978
Jens Axboe94ae5e72019-11-14 19:39:52 -0700979 *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -0600980 return do_hashed;
981}
982
Jackie Liua197f662019-11-08 08:09:12 -0700983static inline void io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -0600984{
Jackie Liua197f662019-11-08 08:09:12 -0700985 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe94ae5e72019-11-14 19:39:52 -0700986 struct io_kiocb *link;
987 bool do_hashed;
988
989 do_hashed = io_prep_async_work(req, &link);
Jens Axboe561fb042019-10-24 07:25:42 -0600990
991 trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work,
992 req->flags);
993 if (!do_hashed) {
994 io_wq_enqueue(ctx->io_wq, &req->work);
995 } else {
996 io_wq_enqueue_hashed(ctx->io_wq, &req->work,
997 file_inode(req->file));
998 }
Jens Axboe94ae5e72019-11-14 19:39:52 -0700999
1000 if (link)
1001 io_queue_linked_timeout(link);
Jens Axboe18d9be12019-09-10 09:13:05 -06001002}
1003
Jens Axboe5262f562019-09-17 12:26:57 -06001004static void io_kill_timeout(struct io_kiocb *req)
1005{
1006 int ret;
1007
Jens Axboe2d283902019-12-04 11:08:05 -07001008 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe5262f562019-09-17 12:26:57 -06001009 if (ret != -1) {
1010 atomic_inc(&req->ctx->cq_timeouts);
Jens Axboe842f9612019-10-29 12:34:10 -06001011 list_del_init(&req->list);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001012 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001013 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -06001014 }
1015}
1016
1017static void io_kill_timeouts(struct io_ring_ctx *ctx)
1018{
1019 struct io_kiocb *req, *tmp;
1020
1021 spin_lock_irq(&ctx->completion_lock);
1022 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
1023 io_kill_timeout(req);
1024 spin_unlock_irq(&ctx->completion_lock);
1025}
1026
Jens Axboede0617e2019-04-06 21:51:27 -06001027static void io_commit_cqring(struct io_ring_ctx *ctx)
1028{
1029 struct io_kiocb *req;
1030
Jens Axboe5262f562019-09-17 12:26:57 -06001031 while ((req = io_get_timeout_req(ctx)) != NULL)
1032 io_kill_timeout(req);
1033
Jens Axboede0617e2019-04-06 21:51:27 -06001034 __io_commit_cqring(ctx);
1035
Pavel Begunkov87987892020-01-18 01:22:30 +03001036 while ((req = io_get_deferred_req(ctx)) != NULL)
Jackie Liua197f662019-11-08 08:09:12 -07001037 io_queue_async_work(req);
Jens Axboede0617e2019-04-06 21:51:27 -06001038}
1039
Jens Axboe2b188cc2019-01-07 10:46:33 -07001040static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
1041{
Hristo Venev75b28af2019-08-26 17:23:46 +00001042 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001043 unsigned tail;
1044
1045 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +02001046 /*
1047 * writes to the cq entry need to come after reading head; the
1048 * control dependency is enough as we're using WRITE_ONCE to
1049 * fill the cq entry
1050 */
Hristo Venev75b28af2019-08-26 17:23:46 +00001051 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001052 return NULL;
1053
1054 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +00001055 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001056}
1057
Jens Axboef2842ab2020-01-08 11:04:00 -07001058static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1059{
Jens Axboef0b493e2020-02-01 21:30:11 -07001060 if (!ctx->cq_ev_fd)
1061 return false;
Jens Axboef2842ab2020-01-08 11:04:00 -07001062 if (!ctx->eventfd_async)
1063 return true;
1064 return io_wq_current_is_worker() || in_interrupt();
1065}
1066
Jens Axboef0b493e2020-02-01 21:30:11 -07001067static void __io_cqring_ev_posted(struct io_ring_ctx *ctx, bool trigger_ev)
Jens Axboe8c838782019-03-12 15:48:16 -06001068{
1069 if (waitqueue_active(&ctx->wait))
1070 wake_up(&ctx->wait);
1071 if (waitqueue_active(&ctx->sqo_wait))
1072 wake_up(&ctx->sqo_wait);
Jens Axboef0b493e2020-02-01 21:30:11 -07001073 if (trigger_ev)
Jens Axboe9b402842019-04-11 11:45:41 -06001074 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -06001075}
1076
Jens Axboef0b493e2020-02-01 21:30:11 -07001077static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
1078{
1079 __io_cqring_ev_posted(ctx, io_should_trigger_evfd(ctx));
1080}
1081
Jens Axboec4a2ed72019-11-21 21:01:26 -07001082/* Returns true if there are no backlogged entries after the flush */
1083static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001084{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001085 struct io_rings *rings = ctx->rings;
1086 struct io_uring_cqe *cqe;
1087 struct io_kiocb *req;
1088 unsigned long flags;
1089 LIST_HEAD(list);
1090
1091 if (!force) {
1092 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001093 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001094 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
1095 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001096 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001097 }
1098
1099 spin_lock_irqsave(&ctx->completion_lock, flags);
1100
1101 /* if force is set, the ring is going away. always drop after that */
1102 if (force)
Jens Axboe69b3e542020-01-08 11:01:46 -07001103 ctx->cq_overflow_flushed = 1;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001104
Jens Axboec4a2ed72019-11-21 21:01:26 -07001105 cqe = NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001106 while (!list_empty(&ctx->cq_overflow_list)) {
1107 cqe = io_get_cqring(ctx);
1108 if (!cqe && !force)
1109 break;
1110
1111 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
1112 list);
1113 list_move(&req->list, &list);
Jens Axboe2ca10252020-02-13 17:17:35 -07001114 req->flags &= ~REQ_F_OVERFLOW;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001115 if (cqe) {
1116 WRITE_ONCE(cqe->user_data, req->user_data);
1117 WRITE_ONCE(cqe->res, req->result);
1118 WRITE_ONCE(cqe->flags, 0);
1119 } else {
1120 WRITE_ONCE(ctx->rings->cq_overflow,
1121 atomic_inc_return(&ctx->cached_cq_overflow));
1122 }
1123 }
1124
1125 io_commit_cqring(ctx);
Jens Axboead3eb2c2019-12-18 17:12:20 -07001126 if (cqe) {
1127 clear_bit(0, &ctx->sq_check_overflow);
1128 clear_bit(0, &ctx->cq_check_overflow);
1129 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001130 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1131 io_cqring_ev_posted(ctx);
1132
1133 while (!list_empty(&list)) {
1134 req = list_first_entry(&list, struct io_kiocb, list);
1135 list_del(&req->list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001136 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001137 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001138
1139 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001140}
1141
Jens Axboe78e19bb2019-11-06 15:21:34 -07001142static void io_cqring_fill_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001143{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001144 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001145 struct io_uring_cqe *cqe;
1146
Jens Axboe78e19bb2019-11-06 15:21:34 -07001147 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001148
Jens Axboe2b188cc2019-01-07 10:46:33 -07001149 /*
1150 * If we can't get a cq entry, userspace overflowed the
1151 * submission (by quite a lot). Increment the overflow count in
1152 * the ring.
1153 */
1154 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001155 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001156 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001157 WRITE_ONCE(cqe->res, res);
1158 WRITE_ONCE(cqe->flags, 0);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001159 } else if (ctx->cq_overflow_flushed) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001160 WRITE_ONCE(ctx->rings->cq_overflow,
1161 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001162 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001163 if (list_empty(&ctx->cq_overflow_list)) {
1164 set_bit(0, &ctx->sq_check_overflow);
1165 set_bit(0, &ctx->cq_check_overflow);
1166 }
Jens Axboe2ca10252020-02-13 17:17:35 -07001167 req->flags |= REQ_F_OVERFLOW;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001168 refcount_inc(&req->refs);
1169 req->result = res;
1170 list_add_tail(&req->list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001171 }
1172}
1173
Jens Axboe78e19bb2019-11-06 15:21:34 -07001174static void io_cqring_add_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001175{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001176 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001177 unsigned long flags;
1178
1179 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001180 io_cqring_fill_event(req, res);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001181 io_commit_cqring(ctx);
1182 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1183
Jens Axboe8c838782019-03-12 15:48:16 -06001184 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001185}
1186
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001187static inline bool io_is_fallback_req(struct io_kiocb *req)
1188{
1189 return req == (struct io_kiocb *)
1190 ((unsigned long) req->ctx->fallback_req & ~1UL);
1191}
1192
1193static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
1194{
1195 struct io_kiocb *req;
1196
1197 req = ctx->fallback_req;
1198 if (!test_and_set_bit_lock(0, (unsigned long *) ctx->fallback_req))
1199 return req;
1200
1201 return NULL;
1202}
1203
Jens Axboe2579f912019-01-09 09:10:43 -07001204static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
1205 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001206{
Jens Axboefd6fab22019-03-14 16:30:06 -06001207 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001208 struct io_kiocb *req;
1209
Jens Axboe2579f912019-01-09 09:10:43 -07001210 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -06001211 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -07001212 if (unlikely(!req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001213 goto fallback;
Jens Axboe2579f912019-01-09 09:10:43 -07001214 } else if (!state->free_reqs) {
1215 size_t sz;
1216 int ret;
1217
1218 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -06001219 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
1220
1221 /*
1222 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1223 * retry single alloc to be on the safe side.
1224 */
1225 if (unlikely(ret <= 0)) {
1226 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1227 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001228 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -06001229 ret = 1;
1230 }
Jens Axboe2579f912019-01-09 09:10:43 -07001231 state->free_reqs = ret - 1;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001232 req = state->reqs[ret - 1];
Jens Axboe2579f912019-01-09 09:10:43 -07001233 } else {
Jens Axboe2579f912019-01-09 09:10:43 -07001234 state->free_reqs--;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001235 req = state->reqs[state->free_reqs];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001236 }
1237
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001238got_it:
Jens Axboe1a6b74f2019-12-02 10:33:15 -07001239 req->io = NULL;
Jens Axboe60c112b2019-06-21 10:20:18 -06001240 req->file = NULL;
Jens Axboe2579f912019-01-09 09:10:43 -07001241 req->ctx = ctx;
1242 req->flags = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06001243 /* one is dropped after submission, the other at completion */
1244 refcount_set(&req->refs, 2);
Jens Axboe9e645e112019-05-10 16:07:28 -06001245 req->result = 0;
Jens Axboe561fb042019-10-24 07:25:42 -06001246 INIT_IO_WORK(&req->work, io_wq_submit_work);
Jens Axboe2579f912019-01-09 09:10:43 -07001247 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001248fallback:
1249 req = io_get_fallback_req(ctx);
1250 if (req)
1251 goto got_it;
Pavel Begunkov6805b322019-10-08 02:18:42 +03001252 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001253 return NULL;
1254}
1255
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001256static void __io_req_do_free(struct io_kiocb *req)
Jens Axboedef596e2019-01-09 08:59:42 -07001257{
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001258 if (likely(!io_is_fallback_req(req)))
1259 kmem_cache_free(req_cachep, req);
1260 else
1261 clear_bit_unlock(0, (unsigned long *) req->ctx->fallback_req);
1262}
1263
Jens Axboec6ca97b302019-12-28 12:11:08 -07001264static void __io_req_aux_free(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001265{
Jens Axboefcb323c2019-10-24 12:39:47 -06001266 struct io_ring_ctx *ctx = req->ctx;
1267
Pavel Begunkov929a3af2020-02-19 00:19:09 +03001268 if (req->flags & REQ_F_NEED_CLEANUP)
1269 io_cleanup_req(req);
1270
YueHaibing96fd84d2020-01-07 22:22:44 +08001271 kfree(req->io);
Jens Axboe05f3fb32019-12-09 11:22:50 -07001272 if (req->file) {
1273 if (req->flags & REQ_F_FIXED_FILE)
1274 percpu_ref_put(&ctx->file_data->refs);
1275 else
1276 fput(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001277 }
Jens Axboecccf0ee2020-01-27 16:34:48 -07001278
1279 io_req_work_drop_env(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001280}
1281
1282static void __io_free_req(struct io_kiocb *req)
1283{
Jens Axboec6ca97b302019-12-28 12:11:08 -07001284 __io_req_aux_free(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001285
Jens Axboefcb323c2019-10-24 12:39:47 -06001286 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001287 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06001288 unsigned long flags;
1289
1290 spin_lock_irqsave(&ctx->inflight_lock, flags);
1291 list_del(&req->inflight_entry);
1292 if (waitqueue_active(&ctx->inflight_wait))
1293 wake_up(&ctx->inflight_wait);
1294 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1295 }
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001296
1297 percpu_ref_put(&req->ctx->refs);
1298 __io_req_do_free(req);
Jens Axboee65ef562019-03-12 10:16:44 -06001299}
1300
Jens Axboec6ca97b302019-12-28 12:11:08 -07001301struct req_batch {
1302 void *reqs[IO_IOPOLL_BATCH];
1303 int to_free;
1304 int need_iter;
1305};
1306
1307static void io_free_req_many(struct io_ring_ctx *ctx, struct req_batch *rb)
1308{
Jens Axboe10fef4b2020-01-09 07:52:28 -07001309 int fixed_refs = rb->to_free;
1310
Jens Axboec6ca97b302019-12-28 12:11:08 -07001311 if (!rb->to_free)
1312 return;
1313 if (rb->need_iter) {
1314 int i, inflight = 0;
1315 unsigned long flags;
1316
Jens Axboe10fef4b2020-01-09 07:52:28 -07001317 fixed_refs = 0;
Jens Axboec6ca97b302019-12-28 12:11:08 -07001318 for (i = 0; i < rb->to_free; i++) {
1319 struct io_kiocb *req = rb->reqs[i];
1320
Jens Axboe10fef4b2020-01-09 07:52:28 -07001321 if (req->flags & REQ_F_FIXED_FILE) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001322 req->file = NULL;
Jens Axboe10fef4b2020-01-09 07:52:28 -07001323 fixed_refs++;
1324 }
Jens Axboec6ca97b302019-12-28 12:11:08 -07001325 if (req->flags & REQ_F_INFLIGHT)
1326 inflight++;
Jens Axboec6ca97b302019-12-28 12:11:08 -07001327 __io_req_aux_free(req);
1328 }
1329 if (!inflight)
1330 goto do_free;
1331
1332 spin_lock_irqsave(&ctx->inflight_lock, flags);
1333 for (i = 0; i < rb->to_free; i++) {
1334 struct io_kiocb *req = rb->reqs[i];
1335
Jens Axboe10fef4b2020-01-09 07:52:28 -07001336 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001337 list_del(&req->inflight_entry);
1338 if (!--inflight)
1339 break;
1340 }
1341 }
1342 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1343
1344 if (waitqueue_active(&ctx->inflight_wait))
1345 wake_up(&ctx->inflight_wait);
1346 }
1347do_free:
1348 kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs);
Jens Axboe10fef4b2020-01-09 07:52:28 -07001349 if (fixed_refs)
1350 percpu_ref_put_many(&ctx->file_data->refs, fixed_refs);
Jens Axboec6ca97b302019-12-28 12:11:08 -07001351 percpu_ref_put_many(&ctx->refs, rb->to_free);
Jens Axboec6ca97b302019-12-28 12:11:08 -07001352 rb->to_free = rb->need_iter = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06001353}
1354
Jackie Liua197f662019-11-08 08:09:12 -07001355static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001356{
Jackie Liua197f662019-11-08 08:09:12 -07001357 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001358 int ret;
1359
Jens Axboe2d283902019-12-04 11:08:05 -07001360 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe2665abf2019-11-05 12:40:47 -07001361 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001362 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -07001363 io_commit_cqring(ctx);
1364 req->flags &= ~REQ_F_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001365 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001366 return true;
1367 }
1368
1369 return false;
1370}
1371
Jens Axboeba816ad2019-09-28 11:36:45 -06001372static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -06001373{
Jens Axboe2665abf2019-11-05 12:40:47 -07001374 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001375 bool wake_ev = false;
Jens Axboe9e645e112019-05-10 16:07:28 -06001376
Jens Axboe4d7dd462019-11-20 13:03:52 -07001377 /* Already got next link */
1378 if (req->flags & REQ_F_LINK_NEXT)
1379 return;
1380
Jens Axboe9e645e112019-05-10 16:07:28 -06001381 /*
1382 * The list should never be empty when we are called here. But could
1383 * potentially happen if the chain is messed up, check to be on the
1384 * safe side.
1385 */
Pavel Begunkov44932332019-12-05 16:16:35 +03001386 while (!list_empty(&req->link_list)) {
1387 struct io_kiocb *nxt = list_first_entry(&req->link_list,
1388 struct io_kiocb, link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001389
Pavel Begunkov44932332019-12-05 16:16:35 +03001390 if (unlikely((req->flags & REQ_F_LINK_TIMEOUT) &&
1391 (nxt->flags & REQ_F_TIMEOUT))) {
1392 list_del_init(&nxt->link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001393 wake_ev |= io_link_cancel_timeout(nxt);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001394 req->flags &= ~REQ_F_LINK_TIMEOUT;
1395 continue;
1396 }
Jens Axboe9e645e112019-05-10 16:07:28 -06001397
Pavel Begunkov44932332019-12-05 16:16:35 +03001398 list_del_init(&req->link_list);
1399 if (!list_empty(&nxt->link_list))
1400 nxt->flags |= REQ_F_LINK;
Pavel Begunkovb18fdf72019-11-21 23:21:02 +03001401 *nxtptr = nxt;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001402 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06001403 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001404
Jens Axboe4d7dd462019-11-20 13:03:52 -07001405 req->flags |= REQ_F_LINK_NEXT;
Jens Axboe2665abf2019-11-05 12:40:47 -07001406 if (wake_ev)
1407 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001408}
1409
1410/*
1411 * Called if REQ_F_LINK is set, and we fail the head request
1412 */
1413static void io_fail_links(struct io_kiocb *req)
1414{
Jens Axboe2665abf2019-11-05 12:40:47 -07001415 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001416 unsigned long flags;
1417
1418 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06001419
1420 while (!list_empty(&req->link_list)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03001421 struct io_kiocb *link = list_first_entry(&req->link_list,
1422 struct io_kiocb, link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06001423
Pavel Begunkov44932332019-12-05 16:16:35 +03001424 list_del_init(&link->link_list);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001425 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001426
1427 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
Jens Axboed625c6e2019-12-17 19:53:05 -07001428 link->opcode == IORING_OP_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -07001429 io_link_cancel_timeout(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001430 } else {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001431 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe978db572019-11-14 22:39:04 -07001432 __io_double_put_req(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001433 }
Jens Axboe5d960722019-11-19 15:31:28 -07001434 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -06001435 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001436
1437 io_commit_cqring(ctx);
1438 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1439 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001440}
1441
Jens Axboe4d7dd462019-11-20 13:03:52 -07001442static void io_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -06001443{
Jens Axboe4d7dd462019-11-20 13:03:52 -07001444 if (likely(!(req->flags & REQ_F_LINK)))
Jens Axboe2665abf2019-11-05 12:40:47 -07001445 return;
Jens Axboe2665abf2019-11-05 12:40:47 -07001446
Jens Axboe9e645e112019-05-10 16:07:28 -06001447 /*
1448 * If LINK is set, we have dependent requests in this chain. If we
1449 * didn't fail this request, queue the first one up, moving any other
1450 * dependencies to the next request. In case of failure, fail the rest
1451 * of the chain.
1452 */
Jens Axboe2665abf2019-11-05 12:40:47 -07001453 if (req->flags & REQ_F_FAIL_LINK) {
1454 io_fail_links(req);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001455 } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) ==
1456 REQ_F_LINK_TIMEOUT) {
Jens Axboe2665abf2019-11-05 12:40:47 -07001457 struct io_ring_ctx *ctx = req->ctx;
1458 unsigned long flags;
1459
1460 /*
1461 * If this is a timeout link, we could be racing with the
1462 * timeout timer. Grab the completion lock for this case to
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001463 * protect against that.
Jens Axboe2665abf2019-11-05 12:40:47 -07001464 */
1465 spin_lock_irqsave(&ctx->completion_lock, flags);
1466 io_req_link_next(req, nxt);
1467 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1468 } else {
1469 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -06001470 }
Jens Axboe4d7dd462019-11-20 13:03:52 -07001471}
Jens Axboe9e645e112019-05-10 16:07:28 -06001472
Jackie Liuc69f8db2019-11-09 11:00:08 +08001473static void io_free_req(struct io_kiocb *req)
1474{
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001475 struct io_kiocb *nxt = NULL;
1476
1477 io_req_find_next(req, &nxt);
Pavel Begunkov70cf9f32019-11-21 23:21:00 +03001478 __io_free_req(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001479
1480 if (nxt)
1481 io_queue_async_work(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08001482}
1483
Jens Axboeba816ad2019-09-28 11:36:45 -06001484/*
1485 * Drop reference to request, return next in chain (if there is one) if this
1486 * was the last reference to this request.
1487 */
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001488__attribute__((nonnull))
Jackie Liuec9c02a2019-11-08 23:50:36 +08001489static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboee65ef562019-03-12 10:16:44 -06001490{
Jens Axboe2a44f462020-02-25 13:25:41 -07001491 if (refcount_dec_and_test(&req->refs)) {
1492 io_req_find_next(req, nxtptr);
Jens Axboe4d7dd462019-11-20 13:03:52 -07001493 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07001494 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001495}
1496
Jens Axboe2b188cc2019-01-07 10:46:33 -07001497static void io_put_req(struct io_kiocb *req)
1498{
Jens Axboedef596e2019-01-09 08:59:42 -07001499 if (refcount_dec_and_test(&req->refs))
1500 io_free_req(req);
1501}
1502
Jens Axboe978db572019-11-14 22:39:04 -07001503/*
1504 * Must only be used if we don't need to care about links, usually from
1505 * within the completion handling itself.
1506 */
1507static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06001508{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001509 /* drop both submit and complete references */
1510 if (refcount_sub_and_test(2, &req->refs))
1511 __io_free_req(req);
1512}
1513
Jens Axboe978db572019-11-14 22:39:04 -07001514static void io_double_put_req(struct io_kiocb *req)
1515{
1516 /* drop both submit and complete references */
1517 if (refcount_sub_and_test(2, &req->refs))
1518 io_free_req(req);
1519}
1520
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001521static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06001522{
Jens Axboe84f97dc2019-11-06 11:27:53 -07001523 struct io_rings *rings = ctx->rings;
1524
Jens Axboead3eb2c2019-12-18 17:12:20 -07001525 if (test_bit(0, &ctx->cq_check_overflow)) {
1526 /*
1527 * noflush == true is from the waitqueue handler, just ensure
1528 * we wake up the task, and the next invocation will flush the
1529 * entries. We cannot safely to it from here.
1530 */
1531 if (noflush && !list_empty(&ctx->cq_overflow_list))
1532 return -1U;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001533
Jens Axboead3eb2c2019-12-18 17:12:20 -07001534 io_cqring_overflow_flush(ctx, false);
1535 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001536
Jens Axboea3a0e432019-08-20 11:03:11 -06001537 /* See comment at the top of this file */
1538 smp_rmb();
Jens Axboead3eb2c2019-12-18 17:12:20 -07001539 return ctx->cached_cq_tail - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06001540}
1541
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03001542static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1543{
1544 struct io_rings *rings = ctx->rings;
1545
1546 /* make sure SQ entry isn't read before tail */
1547 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1548}
1549
Jens Axboe8237e042019-12-28 10:48:22 -07001550static inline bool io_req_multi_free(struct req_batch *rb, struct io_kiocb *req)
Jens Axboee94f1412019-12-19 12:06:02 -07001551{
Jens Axboec6ca97b302019-12-28 12:11:08 -07001552 if ((req->flags & REQ_F_LINK) || io_is_fallback_req(req))
1553 return false;
Jens Axboee94f1412019-12-19 12:06:02 -07001554
Jens Axboec6ca97b302019-12-28 12:11:08 -07001555 if (!(req->flags & REQ_F_FIXED_FILE) || req->io)
1556 rb->need_iter++;
1557
1558 rb->reqs[rb->to_free++] = req;
1559 if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
1560 io_free_req_many(req->ctx, rb);
1561 return true;
Jens Axboee94f1412019-12-19 12:06:02 -07001562}
1563
Jens Axboedef596e2019-01-09 08:59:42 -07001564/*
1565 * Find and free completed poll iocbs
1566 */
1567static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
1568 struct list_head *done)
1569{
Jens Axboe8237e042019-12-28 10:48:22 -07001570 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07001571 struct io_kiocb *req;
Jens Axboedef596e2019-01-09 08:59:42 -07001572
Jens Axboec6ca97b302019-12-28 12:11:08 -07001573 rb.to_free = rb.need_iter = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001574 while (!list_empty(done)) {
1575 req = list_first_entry(done, struct io_kiocb, list);
1576 list_del(&req->list);
1577
Jens Axboe78e19bb2019-11-06 15:21:34 -07001578 io_cqring_fill_event(req, req->result);
Jens Axboedef596e2019-01-09 08:59:42 -07001579 (*nr_events)++;
1580
Jens Axboe8237e042019-12-28 10:48:22 -07001581 if (refcount_dec_and_test(&req->refs) &&
1582 !io_req_multi_free(&rb, req))
1583 io_free_req(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001584 }
Jens Axboedef596e2019-01-09 08:59:42 -07001585
Jens Axboe09bb8392019-03-13 12:39:28 -06001586 io_commit_cqring(ctx);
Jens Axboe8237e042019-12-28 10:48:22 -07001587 io_free_req_many(ctx, &rb);
Jens Axboedef596e2019-01-09 08:59:42 -07001588}
1589
1590static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1591 long min)
1592{
1593 struct io_kiocb *req, *tmp;
1594 LIST_HEAD(done);
1595 bool spin;
1596 int ret;
1597
1598 /*
1599 * Only spin for completions if we don't have multiple devices hanging
1600 * off our complete list, and we're under the requested amount.
1601 */
1602 spin = !ctx->poll_multi_file && *nr_events < min;
1603
1604 ret = 0;
1605 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
Jens Axboe9adbd452019-12-20 08:45:55 -07001606 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07001607
1608 /*
1609 * Move completed entries to our local list. If we find a
1610 * request that requires polling, break out and complete
1611 * the done list first, if we have entries there.
1612 */
1613 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
1614 list_move_tail(&req->list, &done);
1615 continue;
1616 }
1617 if (!list_empty(&done))
1618 break;
1619
1620 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1621 if (ret < 0)
1622 break;
1623
1624 if (ret && spin)
1625 spin = false;
1626 ret = 0;
1627 }
1628
1629 if (!list_empty(&done))
1630 io_iopoll_complete(ctx, nr_events, &done);
1631
1632 return ret;
1633}
1634
1635/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08001636 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07001637 * non-spinning poll check - we'll still enter the driver poll loop, but only
1638 * as a non-spinning completion check.
1639 */
1640static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
1641 long min)
1642{
Jens Axboe08f54392019-08-21 22:19:11 -06001643 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07001644 int ret;
1645
1646 ret = io_do_iopoll(ctx, nr_events, min);
1647 if (ret < 0)
1648 return ret;
1649 if (!min || *nr_events >= min)
1650 return 0;
1651 }
1652
1653 return 1;
1654}
1655
1656/*
1657 * We can't just wait for polled events to come to us, we have to actively
1658 * find and complete them.
1659 */
1660static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
1661{
1662 if (!(ctx->flags & IORING_SETUP_IOPOLL))
1663 return;
1664
1665 mutex_lock(&ctx->uring_lock);
1666 while (!list_empty(&ctx->poll_list)) {
1667 unsigned int nr_events = 0;
1668
1669 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -06001670
1671 /*
1672 * Ensure we allow local-to-the-cpu processing to take place,
1673 * in this case we need to ensure that we reap all events.
1674 */
1675 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -07001676 }
1677 mutex_unlock(&ctx->uring_lock);
1678}
1679
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08001680static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1681 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07001682{
Jens Axboe2b2ed972019-10-25 10:06:15 -06001683 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001684
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08001685 /*
1686 * We disallow the app entering submit/complete with polling, but we
1687 * still need to lock the ring to prevent racing with polled issue
1688 * that got punted to a workqueue.
1689 */
1690 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001691 do {
1692 int tmin = 0;
1693
Jens Axboe500f9fb2019-08-19 12:15:59 -06001694 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06001695 * Don't enter poll loop if we already have events pending.
1696 * If we do, we can potentially be spinning for commands that
1697 * already triggered a CQE (eg in error).
1698 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001699 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06001700 break;
1701
1702 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06001703 * If a submit got punted to a workqueue, we can have the
1704 * application entering polling for a command before it gets
1705 * issued. That app will hold the uring_lock for the duration
1706 * of the poll right here, so we need to take a breather every
1707 * now and then to ensure that the issue has a chance to add
1708 * the poll to the issued list. Otherwise we can spin here
1709 * forever, while the workqueue is stuck trying to acquire the
1710 * very same mutex.
1711 */
1712 if (!(++iters & 7)) {
1713 mutex_unlock(&ctx->uring_lock);
1714 mutex_lock(&ctx->uring_lock);
1715 }
1716
Jens Axboedef596e2019-01-09 08:59:42 -07001717 if (*nr_events < min)
1718 tmin = min - *nr_events;
1719
1720 ret = io_iopoll_getevents(ctx, nr_events, tmin);
1721 if (ret <= 0)
1722 break;
1723 ret = 0;
1724 } while (min && !*nr_events && !need_resched());
1725
Jens Axboe500f9fb2019-08-19 12:15:59 -06001726 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001727 return ret;
1728}
1729
Jens Axboe491381ce2019-10-17 09:20:46 -06001730static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001731{
Jens Axboe491381ce2019-10-17 09:20:46 -06001732 /*
1733 * Tell lockdep we inherited freeze protection from submission
1734 * thread.
1735 */
1736 if (req->flags & REQ_F_ISREG) {
1737 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001738
Jens Axboe491381ce2019-10-17 09:20:46 -06001739 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001740 }
Jens Axboe491381ce2019-10-17 09:20:46 -06001741 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001742}
1743
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001744static inline void req_set_fail_links(struct io_kiocb *req)
1745{
1746 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1747 req->flags |= REQ_F_FAIL_LINK;
1748}
1749
Jens Axboeba816ad2019-09-28 11:36:45 -06001750static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001751{
Jens Axboe9adbd452019-12-20 08:45:55 -07001752 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001753
Jens Axboe491381ce2019-10-17 09:20:46 -06001754 if (kiocb->ki_flags & IOCB_WRITE)
1755 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001756
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001757 if (res != req->result)
1758 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001759 io_cqring_add_event(req, res);
Jens Axboeba816ad2019-09-28 11:36:45 -06001760}
1761
1762static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1763{
Jens Axboe9adbd452019-12-20 08:45:55 -07001764 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06001765
1766 io_complete_rw_common(kiocb, res);
Jens Axboee65ef562019-03-12 10:16:44 -06001767 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001768}
1769
Jens Axboeba816ad2019-09-28 11:36:45 -06001770static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res)
1771{
Jens Axboe9adbd452019-12-20 08:45:55 -07001772 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001773 struct io_kiocb *nxt = NULL;
Jens Axboeba816ad2019-09-28 11:36:45 -06001774
1775 io_complete_rw_common(kiocb, res);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001776 io_put_req_find_next(req, &nxt);
1777
1778 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001779}
1780
Jens Axboedef596e2019-01-09 08:59:42 -07001781static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1782{
Jens Axboe9adbd452019-12-20 08:45:55 -07001783 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07001784
Jens Axboe491381ce2019-10-17 09:20:46 -06001785 if (kiocb->ki_flags & IOCB_WRITE)
1786 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001787
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001788 if (res != req->result)
1789 req_set_fail_links(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06001790 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -07001791 if (res != -EAGAIN)
1792 req->flags |= REQ_F_IOPOLL_COMPLETED;
1793}
1794
1795/*
1796 * After the iocb has been issued, it's safe to be found on the poll list.
1797 * Adding the kiocb to the list AFTER submission ensures that we don't
1798 * find it from a io_iopoll_getevents() thread before the issuer is done
1799 * accessing the kiocb cookie.
1800 */
1801static void io_iopoll_req_issued(struct io_kiocb *req)
1802{
1803 struct io_ring_ctx *ctx = req->ctx;
1804
1805 /*
1806 * Track whether we have multiple files in our lists. This will impact
1807 * how we do polling eventually, not spinning if we're on potentially
1808 * different devices.
1809 */
1810 if (list_empty(&ctx->poll_list)) {
1811 ctx->poll_multi_file = false;
1812 } else if (!ctx->poll_multi_file) {
1813 struct io_kiocb *list_req;
1814
1815 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1816 list);
Jens Axboe9adbd452019-12-20 08:45:55 -07001817 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07001818 ctx->poll_multi_file = true;
1819 }
1820
1821 /*
1822 * For fast devices, IO may have already completed. If it has, add
1823 * it to the front so we find it first.
1824 */
1825 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1826 list_add(&req->list, &ctx->poll_list);
1827 else
1828 list_add_tail(&req->list, &ctx->poll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08001829
1830 if ((ctx->flags & IORING_SETUP_SQPOLL) &&
1831 wq_has_sleeper(&ctx->sqo_wait))
1832 wake_up(&ctx->sqo_wait);
Jens Axboedef596e2019-01-09 08:59:42 -07001833}
1834
Jens Axboe3d6770f2019-04-13 11:50:54 -06001835static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07001836{
Jens Axboe3d6770f2019-04-13 11:50:54 -06001837 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -07001838 int diff = state->has_refs - state->used_refs;
1839
1840 if (diff)
1841 fput_many(state->file, diff);
1842 state->file = NULL;
1843 }
1844}
1845
1846/*
1847 * Get as many references to a file as we have IOs left in this submission,
1848 * assuming most submissions are for one file, or at least that each file
1849 * has more than one submission.
1850 */
1851static struct file *io_file_get(struct io_submit_state *state, int fd)
1852{
1853 if (!state)
1854 return fget(fd);
1855
1856 if (state->file) {
1857 if (state->fd == fd) {
1858 state->used_refs++;
1859 state->ios_left--;
1860 return state->file;
1861 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06001862 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07001863 }
1864 state->file = fget_many(fd, state->ios_left);
1865 if (!state->file)
1866 return NULL;
1867
1868 state->fd = fd;
1869 state->has_refs = state->ios_left;
1870 state->used_refs = 1;
1871 state->ios_left--;
1872 return state->file;
1873}
1874
Jens Axboe2b188cc2019-01-07 10:46:33 -07001875/*
1876 * If we tracked the file through the SCM inflight mechanism, we could support
1877 * any file. For now, just ensure that anything potentially problematic is done
1878 * inline.
1879 */
1880static bool io_file_supports_async(struct file *file)
1881{
1882 umode_t mode = file_inode(file)->i_mode;
1883
Jens Axboe10d59342019-12-09 20:16:22 -07001884 if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07001885 return true;
1886 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
1887 return true;
1888
1889 return false;
1890}
1891
Jens Axboe3529d8c2019-12-19 18:24:38 -07001892static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1893 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001894{
Jens Axboedef596e2019-01-09 08:59:42 -07001895 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07001896 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06001897 unsigned ioprio;
1898 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001899
Jens Axboe491381ce2019-10-17 09:20:46 -06001900 if (S_ISREG(file_inode(req->file)->i_mode))
1901 req->flags |= REQ_F_ISREG;
1902
Jens Axboe2b188cc2019-01-07 10:46:33 -07001903 kiocb->ki_pos = READ_ONCE(sqe->off);
Jens Axboeba042912019-12-25 16:33:42 -07001904 if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) {
1905 req->flags |= REQ_F_CUR_POS;
1906 kiocb->ki_pos = req->file->f_pos;
1907 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001908 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03001909 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
1910 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
1911 if (unlikely(ret))
1912 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001913
1914 ioprio = READ_ONCE(sqe->ioprio);
1915 if (ioprio) {
1916 ret = ioprio_check_cap(ioprio);
1917 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06001918 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001919
1920 kiocb->ki_ioprio = ioprio;
1921 } else
1922 kiocb->ki_ioprio = get_current_ioprio();
1923
Stefan Bühler8449eed2019-04-27 20:34:19 +02001924 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06001925 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
1926 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02001927 req->flags |= REQ_F_NOWAIT;
1928
1929 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001930 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001931
Jens Axboedef596e2019-01-09 08:59:42 -07001932 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07001933 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
1934 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06001935 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001936
Jens Axboedef596e2019-01-09 08:59:42 -07001937 kiocb->ki_flags |= IOCB_HIPRI;
1938 kiocb->ki_complete = io_complete_rw_iopoll;
Jens Axboe6873e0b2019-10-30 13:53:09 -06001939 req->result = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001940 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001941 if (kiocb->ki_flags & IOCB_HIPRI)
1942 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07001943 kiocb->ki_complete = io_complete_rw;
1944 }
Jens Axboe9adbd452019-12-20 08:45:55 -07001945
Jens Axboe3529d8c2019-12-19 18:24:38 -07001946 req->rw.addr = READ_ONCE(sqe->addr);
1947 req->rw.len = READ_ONCE(sqe->len);
Jens Axboe9adbd452019-12-20 08:45:55 -07001948 /* we own ->private, reuse it for the buffer index */
1949 req->rw.kiocb.private = (void *) (unsigned long)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001950 READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001951 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001952}
1953
1954static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
1955{
1956 switch (ret) {
1957 case -EIOCBQUEUED:
1958 break;
1959 case -ERESTARTSYS:
1960 case -ERESTARTNOINTR:
1961 case -ERESTARTNOHAND:
1962 case -ERESTART_RESTARTBLOCK:
1963 /*
1964 * We can't just restart the syscall, since previously
1965 * submitted sqes may already be in progress. Just fail this
1966 * IO with EINTR.
1967 */
1968 ret = -EINTR;
1969 /* fall through */
1970 default:
1971 kiocb->ki_complete(kiocb, ret, 0);
1972 }
1973}
1974
Pavel Begunkovbcaec082020-02-24 11:30:18 +03001975static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt)
Jens Axboeba816ad2019-09-28 11:36:45 -06001976{
Jens Axboeba042912019-12-25 16:33:42 -07001977 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
1978
1979 if (req->flags & REQ_F_CUR_POS)
1980 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovbcaec082020-02-24 11:30:18 +03001981 if (ret >= 0 && kiocb->ki_complete == io_complete_rw)
Jens Axboeba816ad2019-09-28 11:36:45 -06001982 *nxt = __io_complete_rw(kiocb, ret);
1983 else
1984 io_rw_done(kiocb, ret);
1985}
1986
Jens Axboe9adbd452019-12-20 08:45:55 -07001987static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03001988 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07001989{
Jens Axboe9adbd452019-12-20 08:45:55 -07001990 struct io_ring_ctx *ctx = req->ctx;
1991 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001992 struct io_mapped_ubuf *imu;
1993 unsigned index, buf_index;
1994 size_t offset;
1995 u64 buf_addr;
1996
1997 /* attempt to use fixed buffers without having provided iovecs */
1998 if (unlikely(!ctx->user_bufs))
1999 return -EFAULT;
2000
Jens Axboe9adbd452019-12-20 08:45:55 -07002001 buf_index = (unsigned long) req->rw.kiocb.private;
Jens Axboeedafcce2019-01-09 09:16:05 -07002002 if (unlikely(buf_index >= ctx->nr_user_bufs))
2003 return -EFAULT;
2004
2005 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
2006 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07002007 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07002008
2009 /* overflow */
2010 if (buf_addr + len < buf_addr)
2011 return -EFAULT;
2012 /* not inside the mapped region */
2013 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
2014 return -EFAULT;
2015
2016 /*
2017 * May not be a start of buffer, set size appropriately
2018 * and advance us to the beginning.
2019 */
2020 offset = buf_addr - imu->ubuf;
2021 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06002022
2023 if (offset) {
2024 /*
2025 * Don't use iov_iter_advance() here, as it's really slow for
2026 * using the latter parts of a big fixed buffer - it iterates
2027 * over each segment manually. We can cheat a bit here, because
2028 * we know that:
2029 *
2030 * 1) it's a BVEC iter, we set it up
2031 * 2) all bvecs are PAGE_SIZE in size, except potentially the
2032 * first and last bvec
2033 *
2034 * So just find our index, and adjust the iterator afterwards.
2035 * If the offset is within the first bvec (or the whole first
2036 * bvec, just use iov_iter_advance(). This makes it easier
2037 * since we can just skip the first segment, which may not
2038 * be PAGE_SIZE aligned.
2039 */
2040 const struct bio_vec *bvec = imu->bvec;
2041
2042 if (offset <= bvec->bv_len) {
2043 iov_iter_advance(iter, offset);
2044 } else {
2045 unsigned long seg_skip;
2046
2047 /* skip first vec */
2048 offset -= bvec->bv_len;
2049 seg_skip = 1 + (offset >> PAGE_SHIFT);
2050
2051 iter->bvec = bvec + seg_skip;
2052 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02002053 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002054 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002055 }
2056 }
2057
Jens Axboe5e559562019-11-13 16:12:46 -07002058 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002059}
2060
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002061static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
2062 struct iovec **iovec, struct iov_iter *iter)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002063{
Jens Axboe9adbd452019-12-20 08:45:55 -07002064 void __user *buf = u64_to_user_ptr(req->rw.addr);
2065 size_t sqe_len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002066 u8 opcode;
2067
Jens Axboed625c6e2019-12-17 19:53:05 -07002068 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03002069 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07002070 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07002071 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07002072 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002073
Jens Axboe9adbd452019-12-20 08:45:55 -07002074 /* buffer index only valid with fixed read/write */
2075 if (req->rw.kiocb.private)
2076 return -EINVAL;
2077
Jens Axboe3a6820f2019-12-22 15:19:35 -07002078 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
2079 ssize_t ret;
2080 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
2081 *iovec = NULL;
Jens Axboe3a901592020-02-25 17:48:55 -07002082 return ret < 0 ? ret : sqe_len;
Jens Axboe3a6820f2019-12-22 15:19:35 -07002083 }
2084
Jens Axboef67676d2019-12-02 11:03:47 -07002085 if (req->io) {
2086 struct io_async_rw *iorw = &req->io->rw;
2087
2088 *iovec = iorw->iov;
2089 iov_iter_init(iter, rw, *iovec, iorw->nr_segs, iorw->size);
2090 if (iorw->iov == iorw->fast_iov)
2091 *iovec = NULL;
2092 return iorw->size;
2093 }
2094
Jens Axboe2b188cc2019-01-07 10:46:33 -07002095#ifdef CONFIG_COMPAT
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002096 if (req->ctx->compat)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002097 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
2098 iovec, iter);
2099#endif
2100
2101 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
2102}
2103
Jens Axboe32960612019-09-23 11:05:34 -06002104/*
2105 * For files that don't have ->read_iter() and ->write_iter(), handle them
2106 * by looping over ->read() or ->write() manually.
2107 */
2108static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
2109 struct iov_iter *iter)
2110{
2111 ssize_t ret = 0;
2112
2113 /*
2114 * Don't support polled IO through this interface, and we can't
2115 * support non-blocking either. For the latter, this just causes
2116 * the kiocb to be handled from an async context.
2117 */
2118 if (kiocb->ki_flags & IOCB_HIPRI)
2119 return -EOPNOTSUPP;
2120 if (kiocb->ki_flags & IOCB_NOWAIT)
2121 return -EAGAIN;
2122
2123 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002124 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06002125 ssize_t nr;
2126
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002127 if (!iov_iter_is_bvec(iter)) {
2128 iovec = iov_iter_iovec(iter);
2129 } else {
2130 /* fixed buffers import bvec */
2131 iovec.iov_base = kmap(iter->bvec->bv_page)
2132 + iter->iov_offset;
2133 iovec.iov_len = min(iter->count,
2134 iter->bvec->bv_len - iter->iov_offset);
2135 }
2136
Jens Axboe32960612019-09-23 11:05:34 -06002137 if (rw == READ) {
2138 nr = file->f_op->read(file, iovec.iov_base,
2139 iovec.iov_len, &kiocb->ki_pos);
2140 } else {
2141 nr = file->f_op->write(file, iovec.iov_base,
2142 iovec.iov_len, &kiocb->ki_pos);
2143 }
2144
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002145 if (iov_iter_is_bvec(iter))
2146 kunmap(iter->bvec->bv_page);
2147
Jens Axboe32960612019-09-23 11:05:34 -06002148 if (nr < 0) {
2149 if (!ret)
2150 ret = nr;
2151 break;
2152 }
2153 ret += nr;
2154 if (nr != iovec.iov_len)
2155 break;
2156 iov_iter_advance(iter, nr);
2157 }
2158
2159 return ret;
2160}
2161
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002162static void io_req_map_rw(struct io_kiocb *req, ssize_t io_size,
Jens Axboef67676d2019-12-02 11:03:47 -07002163 struct iovec *iovec, struct iovec *fast_iov,
2164 struct iov_iter *iter)
2165{
2166 req->io->rw.nr_segs = iter->nr_segs;
2167 req->io->rw.size = io_size;
2168 req->io->rw.iov = iovec;
2169 if (!req->io->rw.iov) {
2170 req->io->rw.iov = req->io->rw.fast_iov;
2171 memcpy(req->io->rw.iov, fast_iov,
2172 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002173 } else {
2174 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07002175 }
2176}
2177
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002178static int io_alloc_async_ctx(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07002179{
Jens Axboed3656342019-12-18 09:50:26 -07002180 if (!io_op_defs[req->opcode].async_ctx)
2181 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002182 req->io = kmalloc(sizeof(*req->io), GFP_KERNEL);
Jens Axboe06b76d42019-12-19 14:44:26 -07002183 return req->io == NULL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002184}
2185
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002186static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size,
2187 struct iovec *iovec, struct iovec *fast_iov,
2188 struct iov_iter *iter)
2189{
Jens Axboe980ad262020-01-24 23:08:54 -07002190 if (!io_op_defs[req->opcode].async_ctx)
Jens Axboe74566df2020-01-13 19:23:24 -07002191 return 0;
Jens Axboe5d204bc2020-01-31 12:06:52 -07002192 if (!req->io) {
2193 if (io_alloc_async_ctx(req))
2194 return -ENOMEM;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002195
Jens Axboe5d204bc2020-01-31 12:06:52 -07002196 io_req_map_rw(req, io_size, iovec, fast_iov, iter);
2197 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002198 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002199}
2200
Jens Axboe3529d8c2019-12-19 18:24:38 -07002201static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2202 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002203{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002204 struct io_async_ctx *io;
2205 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002206 ssize_t ret;
2207
Jens Axboe3529d8c2019-12-19 18:24:38 -07002208 ret = io_prep_rw(req, sqe, force_nonblock);
2209 if (ret)
2210 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002211
Jens Axboe3529d8c2019-12-19 18:24:38 -07002212 if (unlikely(!(req->file->f_mode & FMODE_READ)))
2213 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002214
Pavel Begunkov5f798be2020-02-08 13:28:02 +03002215 /* either don't need iovec imported or already have it */
2216 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002217 return 0;
2218
2219 io = req->io;
2220 io->rw.iov = io->rw.fast_iov;
2221 req->io = NULL;
2222 ret = io_import_iovec(READ, req, &io->rw.iov, &iter);
2223 req->io = io;
2224 if (ret < 0)
2225 return ret;
2226
2227 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2228 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002229}
2230
Pavel Begunkov267bc902019-11-07 01:41:08 +03002231static int io_read(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06002232 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002233{
2234 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002235 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002236 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002237 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002238 ssize_t io_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002239
Jens Axboe3529d8c2019-12-19 18:24:38 -07002240 ret = io_import_iovec(READ, req, &iovec, &iter);
Jens Axboe06b76d42019-12-19 14:44:26 -07002241 if (ret < 0)
2242 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002243
Jens Axboefd6c2e42019-12-18 12:19:41 -07002244 /* Ensure we clear previously set non-block flag */
2245 if (!force_nonblock)
Jens Axboe29de5f62020-02-20 09:56:08 -07002246 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002247
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002248 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002249 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06002250 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07002251 req->result = io_size;
2252
2253 /*
2254 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2255 * we know to async punt it even if it was opened O_NONBLOCK
2256 */
Jens Axboe29de5f62020-02-20 09:56:08 -07002257 if (force_nonblock && !io_file_supports_async(req->file))
Jens Axboef67676d2019-12-02 11:03:47 -07002258 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06002259
Jens Axboe31b51512019-01-18 22:56:34 -07002260 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002261 ret = rw_verify_area(READ, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002262 if (!ret) {
2263 ssize_t ret2;
2264
Jens Axboe9adbd452019-12-20 08:45:55 -07002265 if (req->file->f_op->read_iter)
2266 ret2 = call_read_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002267 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002268 ret2 = loop_rw_iter(READ, req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002269
Jens Axboe9d93a3f2019-05-15 13:53:07 -06002270 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboef67676d2019-12-02 11:03:47 -07002271 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovbcaec082020-02-24 11:30:18 +03002272 kiocb_done(kiocb, ret2, nxt);
Jens Axboef67676d2019-12-02 11:03:47 -07002273 } else {
2274copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002275 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002276 inline_vecs, &iter);
2277 if (ret)
2278 goto out_free;
Jens Axboe29de5f62020-02-20 09:56:08 -07002279 /* any defer here is final, must blocking retry */
2280 if (!(req->flags & REQ_F_NOWAIT))
2281 req->flags |= REQ_F_MUST_PUNT;
Jens Axboef67676d2019-12-02 11:03:47 -07002282 return -EAGAIN;
2283 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002284 }
Jens Axboef67676d2019-12-02 11:03:47 -07002285out_free:
Pavel Begunkov1e950812020-02-06 19:51:16 +03002286 kfree(iovec);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002287 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002288 return ret;
2289}
2290
Jens Axboe3529d8c2019-12-19 18:24:38 -07002291static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2292 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002293{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002294 struct io_async_ctx *io;
2295 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002296 ssize_t ret;
2297
Jens Axboe3529d8c2019-12-19 18:24:38 -07002298 ret = io_prep_rw(req, sqe, force_nonblock);
2299 if (ret)
2300 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002301
Jens Axboe3529d8c2019-12-19 18:24:38 -07002302 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
2303 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002304
Pavel Begunkov5f798be2020-02-08 13:28:02 +03002305 /* either don't need iovec imported or already have it */
2306 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002307 return 0;
2308
2309 io = req->io;
2310 io->rw.iov = io->rw.fast_iov;
2311 req->io = NULL;
2312 ret = io_import_iovec(WRITE, req, &io->rw.iov, &iter);
2313 req->io = io;
2314 if (ret < 0)
2315 return ret;
2316
2317 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2318 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002319}
2320
Pavel Begunkov267bc902019-11-07 01:41:08 +03002321static int io_write(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06002322 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002323{
2324 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002325 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002326 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002327 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002328 ssize_t ret, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002329
Jens Axboe3529d8c2019-12-19 18:24:38 -07002330 ret = io_import_iovec(WRITE, req, &iovec, &iter);
Jens Axboe06b76d42019-12-19 14:44:26 -07002331 if (ret < 0)
2332 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002333
Jens Axboefd6c2e42019-12-18 12:19:41 -07002334 /* Ensure we clear previously set non-block flag */
2335 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07002336 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002337
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002338 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002339 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06002340 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07002341 req->result = io_size;
2342
2343 /*
2344 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2345 * we know to async punt it even if it was opened O_NONBLOCK
2346 */
Jens Axboe29de5f62020-02-20 09:56:08 -07002347 if (force_nonblock && !io_file_supports_async(req->file))
Jens Axboef67676d2019-12-02 11:03:47 -07002348 goto copy_iov;
Jens Axboef67676d2019-12-02 11:03:47 -07002349
Jens Axboe10d59342019-12-09 20:16:22 -07002350 /* file path doesn't support NOWAIT for non-direct_IO */
2351 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
2352 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07002353 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06002354
Jens Axboe31b51512019-01-18 22:56:34 -07002355 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002356 ret = rw_verify_area(WRITE, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002357 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01002358 ssize_t ret2;
2359
Jens Axboe2b188cc2019-01-07 10:46:33 -07002360 /*
2361 * Open-code file_start_write here to grab freeze protection,
2362 * which will be released by another thread in
2363 * io_complete_rw(). Fool lockdep by telling it the lock got
2364 * released so that it doesn't complain about the held lock when
2365 * we return to userspace.
2366 */
Jens Axboe491381ce2019-10-17 09:20:46 -06002367 if (req->flags & REQ_F_ISREG) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002368 __sb_start_write(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002369 SB_FREEZE_WRITE, true);
Jens Axboe9adbd452019-12-20 08:45:55 -07002370 __sb_writers_release(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002371 SB_FREEZE_WRITE);
2372 }
2373 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01002374
Jens Axboe9adbd452019-12-20 08:45:55 -07002375 if (req->file->f_op->write_iter)
2376 ret2 = call_write_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002377 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002378 ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter);
Jens Axboefaac9962020-02-07 15:45:22 -07002379 /*
2380 * Raw bdev writes will -EOPNOTSUPP for IOCB_NOWAIT. Just
2381 * retry them without IOCB_NOWAIT.
2382 */
2383 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
2384 ret2 = -EAGAIN;
Jens Axboef67676d2019-12-02 11:03:47 -07002385 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovbcaec082020-02-24 11:30:18 +03002386 kiocb_done(kiocb, ret2, nxt);
Jens Axboef67676d2019-12-02 11:03:47 -07002387 } else {
2388copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002389 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002390 inline_vecs, &iter);
2391 if (ret)
2392 goto out_free;
Jens Axboe29de5f62020-02-20 09:56:08 -07002393 /* any defer here is final, must blocking retry */
2394 req->flags |= REQ_F_MUST_PUNT;
Jens Axboef67676d2019-12-02 11:03:47 -07002395 return -EAGAIN;
2396 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002397 }
Jens Axboe31b51512019-01-18 22:56:34 -07002398out_free:
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002399 req->flags &= ~REQ_F_NEED_CLEANUP;
Pavel Begunkov1e950812020-02-06 19:51:16 +03002400 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002401 return ret;
2402}
2403
2404/*
2405 * IORING_OP_NOP just posts a completion event, nothing else.
2406 */
Jens Axboe78e19bb2019-11-06 15:21:34 -07002407static int io_nop(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002408{
2409 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002410
Jens Axboedef596e2019-01-09 08:59:42 -07002411 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2412 return -EINVAL;
2413
Jens Axboe78e19bb2019-11-06 15:21:34 -07002414 io_cqring_add_event(req, 0);
Jens Axboee65ef562019-03-12 10:16:44 -06002415 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002416 return 0;
2417}
2418
Jens Axboe3529d8c2019-12-19 18:24:38 -07002419static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002420{
Jens Axboe6b063142019-01-10 22:13:58 -07002421 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002422
Jens Axboe09bb8392019-03-13 12:39:28 -06002423 if (!req->file)
2424 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002425
Jens Axboe6b063142019-01-10 22:13:58 -07002426 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07002427 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07002428 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002429 return -EINVAL;
2430
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002431 req->sync.flags = READ_ONCE(sqe->fsync_flags);
2432 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
2433 return -EINVAL;
2434
2435 req->sync.off = READ_ONCE(sqe->off);
2436 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002437 return 0;
2438}
2439
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002440static bool io_req_cancelled(struct io_kiocb *req)
2441{
2442 if (req->work.flags & IO_WQ_WORK_CANCEL) {
2443 req_set_fail_links(req);
2444 io_cqring_add_event(req, -ECANCELED);
2445 io_put_req(req);
2446 return true;
2447 }
2448
2449 return false;
2450}
2451
Jens Axboe78912932020-01-14 22:09:06 -07002452static void io_link_work_cb(struct io_wq_work **workptr)
2453{
2454 struct io_wq_work *work = *workptr;
2455 struct io_kiocb *link = work->data;
2456
2457 io_queue_linked_timeout(link);
2458 work->func = io_wq_submit_work;
2459}
2460
2461static void io_wq_assign_next(struct io_wq_work **workptr, struct io_kiocb *nxt)
2462{
2463 struct io_kiocb *link;
2464
Pavel Begunkovdeb6dc02020-02-24 11:30:17 +03002465 io_prep_next_work(nxt, &link);
Jens Axboe78912932020-01-14 22:09:06 -07002466 *workptr = &nxt->work;
2467 if (link) {
2468 nxt->work.flags |= IO_WQ_WORK_CB;
2469 nxt->work.func = io_link_work_cb;
2470 nxt->work.data = link;
2471 }
2472}
2473
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002474static void __io_fsync(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002475{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002476 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002477 int ret;
2478
Jens Axboe9adbd452019-12-20 08:45:55 -07002479 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002480 end > 0 ? end : LLONG_MAX,
2481 req->sync.flags & IORING_FSYNC_DATASYNC);
2482 if (ret < 0)
2483 req_set_fail_links(req);
2484 io_cqring_add_event(req, ret);
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002485 io_put_req_find_next(req, nxt);
2486}
2487
2488static void io_fsync_finish(struct io_wq_work **workptr)
2489{
2490 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2491 struct io_kiocb *nxt = NULL;
2492
2493 if (io_req_cancelled(req))
2494 return;
2495 __io_fsync(req, &nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002496 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07002497 io_wq_assign_next(workptr, nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002498}
2499
Jens Axboefc4df992019-12-10 14:38:45 -07002500static int io_fsync(struct io_kiocb *req, struct io_kiocb **nxt,
2501 bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002502{
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002503 /* fsync always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002504 if (force_nonblock) {
2505 io_put_req(req);
2506 req->work.func = io_fsync_finish;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002507 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002508 }
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002509 __io_fsync(req, nxt);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002510 return 0;
2511}
2512
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002513static void __io_fallocate(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboed63d1b52019-12-10 10:38:56 -07002514{
Jens Axboed63d1b52019-12-10 10:38:56 -07002515 int ret;
2516
Pavel Begunkov7fbeb952020-02-16 01:01:18 +03002517 if (io_req_cancelled(req))
2518 return;
2519
Jens Axboed63d1b52019-12-10 10:38:56 -07002520 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
2521 req->sync.len);
2522 if (ret < 0)
2523 req_set_fail_links(req);
2524 io_cqring_add_event(req, ret);
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002525 io_put_req_find_next(req, nxt);
2526}
2527
2528static void io_fallocate_finish(struct io_wq_work **workptr)
2529{
2530 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2531 struct io_kiocb *nxt = NULL;
2532
2533 __io_fallocate(req, &nxt);
Jens Axboed63d1b52019-12-10 10:38:56 -07002534 if (nxt)
2535 io_wq_assign_next(workptr, nxt);
2536}
2537
2538static int io_fallocate_prep(struct io_kiocb *req,
2539 const struct io_uring_sqe *sqe)
2540{
2541 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
2542 return -EINVAL;
2543
2544 req->sync.off = READ_ONCE(sqe->off);
2545 req->sync.len = READ_ONCE(sqe->addr);
2546 req->sync.mode = READ_ONCE(sqe->len);
2547 return 0;
2548}
2549
2550static int io_fallocate(struct io_kiocb *req, struct io_kiocb **nxt,
2551 bool force_nonblock)
2552{
Jens Axboed63d1b52019-12-10 10:38:56 -07002553 /* fallocate always requiring blocking context */
2554 if (force_nonblock) {
2555 io_put_req(req);
2556 req->work.func = io_fallocate_finish;
2557 return -EAGAIN;
2558 }
2559
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002560 __io_fallocate(req, nxt);
Jens Axboed63d1b52019-12-10 10:38:56 -07002561 return 0;
2562}
2563
Jens Axboe15b71ab2019-12-11 11:20:36 -07002564static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2565{
Jens Axboef8748882020-01-08 17:47:02 -07002566 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002567 int ret;
2568
2569 if (sqe->ioprio || sqe->buf_index)
2570 return -EINVAL;
Jens Axboecf3040c2020-02-06 21:31:40 -07002571 if (sqe->flags & IOSQE_FIXED_FILE)
2572 return -EBADF;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03002573 if (req->flags & REQ_F_NEED_CLEANUP)
2574 return 0;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002575
2576 req->open.dfd = READ_ONCE(sqe->fd);
Jens Axboec12cedf2020-01-08 17:41:21 -07002577 req->open.how.mode = READ_ONCE(sqe->len);
Jens Axboef8748882020-01-08 17:47:02 -07002578 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboec12cedf2020-01-08 17:41:21 -07002579 req->open.how.flags = READ_ONCE(sqe->open_flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002580
Jens Axboef8748882020-01-08 17:47:02 -07002581 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002582 if (IS_ERR(req->open.filename)) {
2583 ret = PTR_ERR(req->open.filename);
2584 req->open.filename = NULL;
2585 return ret;
2586 }
2587
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002588 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002589 return 0;
2590}
2591
Jens Axboecebdb982020-01-08 17:59:24 -07002592static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2593{
2594 struct open_how __user *how;
2595 const char __user *fname;
2596 size_t len;
2597 int ret;
2598
2599 if (sqe->ioprio || sqe->buf_index)
2600 return -EINVAL;
Jens Axboecf3040c2020-02-06 21:31:40 -07002601 if (sqe->flags & IOSQE_FIXED_FILE)
2602 return -EBADF;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03002603 if (req->flags & REQ_F_NEED_CLEANUP)
2604 return 0;
Jens Axboecebdb982020-01-08 17:59:24 -07002605
2606 req->open.dfd = READ_ONCE(sqe->fd);
2607 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
2608 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
2609 len = READ_ONCE(sqe->len);
2610
2611 if (len < OPEN_HOW_SIZE_VER0)
2612 return -EINVAL;
2613
2614 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
2615 len);
2616 if (ret)
2617 return ret;
2618
2619 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
2620 req->open.how.flags |= O_LARGEFILE;
2621
2622 req->open.filename = getname(fname);
2623 if (IS_ERR(req->open.filename)) {
2624 ret = PTR_ERR(req->open.filename);
2625 req->open.filename = NULL;
2626 return ret;
2627 }
2628
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002629 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboecebdb982020-01-08 17:59:24 -07002630 return 0;
2631}
2632
2633static int io_openat2(struct io_kiocb *req, struct io_kiocb **nxt,
2634 bool force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07002635{
2636 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002637 struct file *file;
2638 int ret;
2639
Jens Axboef86cd202020-01-29 13:46:44 -07002640 if (force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07002641 return -EAGAIN;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002642
Jens Axboecebdb982020-01-08 17:59:24 -07002643 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002644 if (ret)
2645 goto err;
2646
Jens Axboecebdb982020-01-08 17:59:24 -07002647 ret = get_unused_fd_flags(req->open.how.flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002648 if (ret < 0)
2649 goto err;
2650
2651 file = do_filp_open(req->open.dfd, req->open.filename, &op);
2652 if (IS_ERR(file)) {
2653 put_unused_fd(ret);
2654 ret = PTR_ERR(file);
2655 } else {
2656 fsnotify_open(file);
2657 fd_install(ret, file);
2658 }
2659err:
2660 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002661 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002662 if (ret < 0)
2663 req_set_fail_links(req);
2664 io_cqring_add_event(req, ret);
2665 io_put_req_find_next(req, nxt);
2666 return 0;
2667}
2668
Jens Axboecebdb982020-01-08 17:59:24 -07002669static int io_openat(struct io_kiocb *req, struct io_kiocb **nxt,
2670 bool force_nonblock)
2671{
2672 req->open.how = build_open_how(req->open.how.flags, req->open.how.mode);
2673 return io_openat2(req, nxt, force_nonblock);
2674}
2675
Jens Axboe3e4827b2020-01-08 15:18:09 -07002676static int io_epoll_ctl_prep(struct io_kiocb *req,
2677 const struct io_uring_sqe *sqe)
2678{
2679#if defined(CONFIG_EPOLL)
2680 if (sqe->ioprio || sqe->buf_index)
2681 return -EINVAL;
2682
2683 req->epoll.epfd = READ_ONCE(sqe->fd);
2684 req->epoll.op = READ_ONCE(sqe->len);
2685 req->epoll.fd = READ_ONCE(sqe->off);
2686
2687 if (ep_op_has_event(req->epoll.op)) {
2688 struct epoll_event __user *ev;
2689
2690 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
2691 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
2692 return -EFAULT;
2693 }
2694
2695 return 0;
2696#else
2697 return -EOPNOTSUPP;
2698#endif
2699}
2700
2701static int io_epoll_ctl(struct io_kiocb *req, struct io_kiocb **nxt,
2702 bool force_nonblock)
2703{
2704#if defined(CONFIG_EPOLL)
2705 struct io_epoll *ie = &req->epoll;
2706 int ret;
2707
2708 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
2709 if (force_nonblock && ret == -EAGAIN)
2710 return -EAGAIN;
2711
2712 if (ret < 0)
2713 req_set_fail_links(req);
2714 io_cqring_add_event(req, ret);
2715 io_put_req_find_next(req, nxt);
2716 return 0;
2717#else
2718 return -EOPNOTSUPP;
2719#endif
2720}
2721
Jens Axboec1ca7572019-12-25 22:18:28 -07002722static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2723{
2724#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
2725 if (sqe->ioprio || sqe->buf_index || sqe->off)
2726 return -EINVAL;
2727
2728 req->madvise.addr = READ_ONCE(sqe->addr);
2729 req->madvise.len = READ_ONCE(sqe->len);
2730 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
2731 return 0;
2732#else
2733 return -EOPNOTSUPP;
2734#endif
2735}
2736
2737static int io_madvise(struct io_kiocb *req, struct io_kiocb **nxt,
2738 bool force_nonblock)
2739{
2740#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
2741 struct io_madvise *ma = &req->madvise;
2742 int ret;
2743
2744 if (force_nonblock)
2745 return -EAGAIN;
2746
2747 ret = do_madvise(ma->addr, ma->len, ma->advice);
2748 if (ret < 0)
2749 req_set_fail_links(req);
2750 io_cqring_add_event(req, ret);
2751 io_put_req_find_next(req, nxt);
2752 return 0;
2753#else
2754 return -EOPNOTSUPP;
2755#endif
2756}
2757
Jens Axboe4840e412019-12-25 22:03:45 -07002758static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2759{
2760 if (sqe->ioprio || sqe->buf_index || sqe->addr)
2761 return -EINVAL;
2762
2763 req->fadvise.offset = READ_ONCE(sqe->off);
2764 req->fadvise.len = READ_ONCE(sqe->len);
2765 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
2766 return 0;
2767}
2768
2769static int io_fadvise(struct io_kiocb *req, struct io_kiocb **nxt,
2770 bool force_nonblock)
2771{
2772 struct io_fadvise *fa = &req->fadvise;
2773 int ret;
2774
Jens Axboe3e694262020-02-01 09:22:49 -07002775 if (force_nonblock) {
2776 switch (fa->advice) {
2777 case POSIX_FADV_NORMAL:
2778 case POSIX_FADV_RANDOM:
2779 case POSIX_FADV_SEQUENTIAL:
2780 break;
2781 default:
2782 return -EAGAIN;
2783 }
2784 }
Jens Axboe4840e412019-12-25 22:03:45 -07002785
2786 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
2787 if (ret < 0)
2788 req_set_fail_links(req);
2789 io_cqring_add_event(req, ret);
2790 io_put_req_find_next(req, nxt);
2791 return 0;
2792}
2793
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002794static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2795{
Jens Axboef8748882020-01-08 17:47:02 -07002796 const char __user *fname;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002797 unsigned lookup_flags;
2798 int ret;
2799
2800 if (sqe->ioprio || sqe->buf_index)
2801 return -EINVAL;
Jens Axboecf3040c2020-02-06 21:31:40 -07002802 if (sqe->flags & IOSQE_FIXED_FILE)
2803 return -EBADF;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03002804 if (req->flags & REQ_F_NEED_CLEANUP)
2805 return 0;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002806
2807 req->open.dfd = READ_ONCE(sqe->fd);
2808 req->open.mask = READ_ONCE(sqe->len);
Jens Axboef8748882020-01-08 17:47:02 -07002809 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002810 req->open.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboec12cedf2020-01-08 17:41:21 -07002811 req->open.how.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002812
Jens Axboec12cedf2020-01-08 17:41:21 -07002813 if (vfs_stat_set_lookup_flags(&lookup_flags, req->open.how.flags))
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002814 return -EINVAL;
2815
Jens Axboef8748882020-01-08 17:47:02 -07002816 req->open.filename = getname_flags(fname, lookup_flags, NULL);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002817 if (IS_ERR(req->open.filename)) {
2818 ret = PTR_ERR(req->open.filename);
2819 req->open.filename = NULL;
2820 return ret;
2821 }
2822
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002823 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002824 return 0;
2825}
2826
2827static int io_statx(struct io_kiocb *req, struct io_kiocb **nxt,
2828 bool force_nonblock)
2829{
2830 struct io_open *ctx = &req->open;
2831 unsigned lookup_flags;
2832 struct path path;
2833 struct kstat stat;
2834 int ret;
2835
2836 if (force_nonblock)
2837 return -EAGAIN;
2838
Jens Axboec12cedf2020-01-08 17:41:21 -07002839 if (vfs_stat_set_lookup_flags(&lookup_flags, ctx->how.flags))
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002840 return -EINVAL;
2841
2842retry:
2843 /* filename_lookup() drops it, keep a reference */
2844 ctx->filename->refcnt++;
2845
2846 ret = filename_lookup(ctx->dfd, ctx->filename, lookup_flags, &path,
2847 NULL);
2848 if (ret)
2849 goto err;
2850
Jens Axboec12cedf2020-01-08 17:41:21 -07002851 ret = vfs_getattr(&path, &stat, ctx->mask, ctx->how.flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002852 path_put(&path);
2853 if (retry_estale(ret, lookup_flags)) {
2854 lookup_flags |= LOOKUP_REVAL;
2855 goto retry;
2856 }
2857 if (!ret)
2858 ret = cp_statx(&stat, ctx->buffer);
2859err:
2860 putname(ctx->filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002861 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002862 if (ret < 0)
2863 req_set_fail_links(req);
2864 io_cqring_add_event(req, ret);
2865 io_put_req_find_next(req, nxt);
2866 return 0;
2867}
2868
Jens Axboeb5dba592019-12-11 14:02:38 -07002869static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2870{
2871 /*
2872 * If we queue this for async, it must not be cancellable. That would
2873 * leave the 'file' in an undeterminate state.
2874 */
2875 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
2876
2877 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
2878 sqe->rw_flags || sqe->buf_index)
2879 return -EINVAL;
2880 if (sqe->flags & IOSQE_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07002881 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07002882
2883 req->close.fd = READ_ONCE(sqe->fd);
2884 if (req->file->f_op == &io_uring_fops ||
Pavel Begunkovb14cca02020-01-17 04:45:59 +03002885 req->close.fd == req->ctx->ring_fd)
Jens Axboeb5dba592019-12-11 14:02:38 -07002886 return -EBADF;
2887
2888 return 0;
2889}
2890
Pavel Begunkova93b3332020-02-08 14:04:34 +03002891/* only called when __close_fd_get_file() is done */
2892static void __io_close_finish(struct io_kiocb *req, struct io_kiocb **nxt)
2893{
2894 int ret;
2895
2896 ret = filp_close(req->close.put_file, req->work.files);
2897 if (ret < 0)
2898 req_set_fail_links(req);
2899 io_cqring_add_event(req, ret);
2900 fput(req->close.put_file);
2901 io_put_req_find_next(req, nxt);
2902}
2903
Jens Axboeb5dba592019-12-11 14:02:38 -07002904static void io_close_finish(struct io_wq_work **workptr)
2905{
2906 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2907 struct io_kiocb *nxt = NULL;
2908
Pavel Begunkov7fbeb952020-02-16 01:01:18 +03002909 /* not cancellable, don't do io_req_cancelled() */
Pavel Begunkova93b3332020-02-08 14:04:34 +03002910 __io_close_finish(req, &nxt);
Jens Axboeb5dba592019-12-11 14:02:38 -07002911 if (nxt)
2912 io_wq_assign_next(workptr, nxt);
2913}
2914
2915static int io_close(struct io_kiocb *req, struct io_kiocb **nxt,
2916 bool force_nonblock)
2917{
2918 int ret;
2919
2920 req->close.put_file = NULL;
2921 ret = __close_fd_get_file(req->close.fd, &req->close.put_file);
2922 if (ret < 0)
2923 return ret;
2924
2925 /* if the file has a flush method, be safe and punt to async */
Jens Axboef86cd202020-01-29 13:46:44 -07002926 if (req->close.put_file->f_op->flush && !io_wq_current_is_worker())
Jens Axboeb5dba592019-12-11 14:02:38 -07002927 goto eagain;
Jens Axboeb5dba592019-12-11 14:02:38 -07002928
2929 /*
2930 * No ->flush(), safely close from here and just punt the
2931 * fput() to async context.
2932 */
Pavel Begunkova93b3332020-02-08 14:04:34 +03002933 __io_close_finish(req, nxt);
2934 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07002935eagain:
2936 req->work.func = io_close_finish;
Jens Axboe1a417f42020-01-31 17:16:48 -07002937 /*
2938 * Do manual async queue here to avoid grabbing files - we don't
2939 * need the files, and it'll cause io_close_finish() to close
2940 * the file again and cause a double CQE entry for this request
2941 */
2942 io_queue_async_work(req);
2943 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07002944}
2945
Jens Axboe3529d8c2019-12-19 18:24:38 -07002946static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002947{
2948 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002949
2950 if (!req->file)
2951 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002952
2953 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2954 return -EINVAL;
2955 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
2956 return -EINVAL;
2957
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002958 req->sync.off = READ_ONCE(sqe->off);
2959 req->sync.len = READ_ONCE(sqe->len);
2960 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002961 return 0;
2962}
2963
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002964static void __io_sync_file_range(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002965{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002966 int ret;
2967
Jens Axboe9adbd452019-12-20 08:45:55 -07002968 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002969 req->sync.flags);
2970 if (ret < 0)
2971 req_set_fail_links(req);
2972 io_cqring_add_event(req, ret);
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002973 io_put_req_find_next(req, nxt);
2974}
2975
2976
2977static void io_sync_file_range_finish(struct io_wq_work **workptr)
2978{
2979 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2980 struct io_kiocb *nxt = NULL;
2981
2982 if (io_req_cancelled(req))
2983 return;
2984 __io_sync_file_range(req, &nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002985 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07002986 io_wq_assign_next(workptr, nxt);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002987}
2988
Jens Axboefc4df992019-12-10 14:38:45 -07002989static int io_sync_file_range(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002990 bool force_nonblock)
2991{
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002992 /* sync_file_range always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002993 if (force_nonblock) {
2994 io_put_req(req);
2995 req->work.func = io_sync_file_range_finish;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002996 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002997 }
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002998
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002999 __io_sync_file_range(req, nxt);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003000 return 0;
3001}
3002
Jens Axboe3529d8c2019-12-19 18:24:38 -07003003static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06003004{
Jens Axboe03b12302019-12-02 18:50:25 -07003005#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07003006 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003007 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003008 int ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003009
Jens Axboee47293f2019-12-20 08:58:21 -07003010 sr->msg_flags = READ_ONCE(sqe->msg_flags);
3011 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07003012 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003013
Jens Axboed8768362020-02-27 14:17:49 -07003014#ifdef CONFIG_COMPAT
3015 if (req->ctx->compat)
3016 sr->msg_flags |= MSG_CMSG_COMPAT;
3017#endif
3018
Jens Axboefddafac2020-01-04 20:19:44 -07003019 if (!io || req->opcode == IORING_OP_SEND)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003020 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003021 /* iovec is already imported */
3022 if (req->flags & REQ_F_NEED_CLEANUP)
3023 return 0;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003024
Jens Axboed9688562019-12-09 19:35:20 -07003025 io->msg.iov = io->msg.fast_iov;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003026 ret = sendmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07003027 &io->msg.iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003028 if (!ret)
3029 req->flags |= REQ_F_NEED_CLEANUP;
3030 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003031#else
Jens Axboee47293f2019-12-20 08:58:21 -07003032 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07003033#endif
3034}
3035
Jens Axboefc4df992019-12-10 14:38:45 -07003036static int io_sendmsg(struct io_kiocb *req, struct io_kiocb **nxt,
3037 bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07003038{
3039#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07003040 struct io_async_msghdr *kmsg = NULL;
Jens Axboe03b12302019-12-02 18:50:25 -07003041 struct socket *sock;
3042 int ret;
3043
3044 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3045 return -EINVAL;
3046
3047 sock = sock_from_file(req->file, &ret);
3048 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003049 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07003050 unsigned flags;
3051
Jens Axboe03b12302019-12-02 18:50:25 -07003052 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07003053 kmsg = &req->io->msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003054 kmsg->msg.msg_name = &req->io->msg.addr;
Jens Axboe0b416c32019-12-15 10:57:46 -07003055 /* if iov is set, it's allocated already */
3056 if (!kmsg->iov)
3057 kmsg->iov = kmsg->fast_iov;
3058 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07003059 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003060 struct io_sr_msg *sr = &req->sr_msg;
3061
Jens Axboe0b416c32019-12-15 10:57:46 -07003062 kmsg = &io.msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003063 kmsg->msg.msg_name = &io.msg.addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003064
3065 io.msg.iov = io.msg.fast_iov;
3066 ret = sendmsg_copy_msghdr(&io.msg.msg, sr->msg,
3067 sr->msg_flags, &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07003068 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003069 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003070 }
3071
Jens Axboee47293f2019-12-20 08:58:21 -07003072 flags = req->sr_msg.msg_flags;
3073 if (flags & MSG_DONTWAIT)
3074 req->flags |= REQ_F_NOWAIT;
3075 else if (force_nonblock)
3076 flags |= MSG_DONTWAIT;
3077
Jens Axboe0b416c32019-12-15 10:57:46 -07003078 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Jens Axboe03b12302019-12-02 18:50:25 -07003079 if (force_nonblock && ret == -EAGAIN) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003080 if (req->io)
3081 return -EAGAIN;
Pavel Begunkov1e950812020-02-06 19:51:16 +03003082 if (io_alloc_async_ctx(req)) {
Dan Carpenter297a31e2020-02-17 17:39:45 +03003083 if (kmsg->iov != kmsg->fast_iov)
Pavel Begunkov1e950812020-02-06 19:51:16 +03003084 kfree(kmsg->iov);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003085 return -ENOMEM;
Pavel Begunkov1e950812020-02-06 19:51:16 +03003086 }
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003087 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003088 memcpy(&req->io->msg, &io.msg, sizeof(io.msg));
Jens Axboe0b416c32019-12-15 10:57:46 -07003089 return -EAGAIN;
Jens Axboe03b12302019-12-02 18:50:25 -07003090 }
3091 if (ret == -ERESTARTSYS)
3092 ret = -EINTR;
3093 }
3094
Pavel Begunkov1e950812020-02-06 19:51:16 +03003095 if (kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07003096 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003097 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe03b12302019-12-02 18:50:25 -07003098 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003099 if (ret < 0)
3100 req_set_fail_links(req);
Jens Axboe03b12302019-12-02 18:50:25 -07003101 io_put_req_find_next(req, nxt);
3102 return 0;
3103#else
3104 return -EOPNOTSUPP;
3105#endif
3106}
3107
Jens Axboefddafac2020-01-04 20:19:44 -07003108static int io_send(struct io_kiocb *req, struct io_kiocb **nxt,
3109 bool force_nonblock)
3110{
3111#if defined(CONFIG_NET)
3112 struct socket *sock;
3113 int ret;
3114
3115 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3116 return -EINVAL;
3117
3118 sock = sock_from_file(req->file, &ret);
3119 if (sock) {
3120 struct io_sr_msg *sr = &req->sr_msg;
3121 struct msghdr msg;
3122 struct iovec iov;
3123 unsigned flags;
3124
3125 ret = import_single_range(WRITE, sr->buf, sr->len, &iov,
3126 &msg.msg_iter);
3127 if (ret)
3128 return ret;
3129
3130 msg.msg_name = NULL;
3131 msg.msg_control = NULL;
3132 msg.msg_controllen = 0;
3133 msg.msg_namelen = 0;
3134
3135 flags = req->sr_msg.msg_flags;
3136 if (flags & MSG_DONTWAIT)
3137 req->flags |= REQ_F_NOWAIT;
3138 else if (force_nonblock)
3139 flags |= MSG_DONTWAIT;
3140
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003141 msg.msg_flags = flags;
3142 ret = sock_sendmsg(sock, &msg);
Jens Axboefddafac2020-01-04 20:19:44 -07003143 if (force_nonblock && ret == -EAGAIN)
3144 return -EAGAIN;
3145 if (ret == -ERESTARTSYS)
3146 ret = -EINTR;
3147 }
3148
3149 io_cqring_add_event(req, ret);
3150 if (ret < 0)
3151 req_set_fail_links(req);
3152 io_put_req_find_next(req, nxt);
3153 return 0;
3154#else
3155 return -EOPNOTSUPP;
3156#endif
3157}
3158
Jens Axboe3529d8c2019-12-19 18:24:38 -07003159static int io_recvmsg_prep(struct io_kiocb *req,
3160 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07003161{
3162#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07003163 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003164 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003165 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07003166
Jens Axboe3529d8c2019-12-19 18:24:38 -07003167 sr->msg_flags = READ_ONCE(sqe->msg_flags);
3168 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003169 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003170
Jens Axboed8768362020-02-27 14:17:49 -07003171#ifdef CONFIG_COMPAT
3172 if (req->ctx->compat)
3173 sr->msg_flags |= MSG_CMSG_COMPAT;
3174#endif
3175
Jens Axboefddafac2020-01-04 20:19:44 -07003176 if (!io || req->opcode == IORING_OP_RECV)
Jens Axboe06b76d42019-12-19 14:44:26 -07003177 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003178 /* iovec is already imported */
3179 if (req->flags & REQ_F_NEED_CLEANUP)
3180 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07003181
Jens Axboed9688562019-12-09 19:35:20 -07003182 io->msg.iov = io->msg.fast_iov;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003183 ret = recvmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07003184 &io->msg.uaddr, &io->msg.iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003185 if (!ret)
3186 req->flags |= REQ_F_NEED_CLEANUP;
3187 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003188#else
Jens Axboee47293f2019-12-20 08:58:21 -07003189 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07003190#endif
3191}
3192
Jens Axboefc4df992019-12-10 14:38:45 -07003193static int io_recvmsg(struct io_kiocb *req, struct io_kiocb **nxt,
3194 bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07003195{
3196#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07003197 struct io_async_msghdr *kmsg = NULL;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003198 struct socket *sock;
3199 int ret;
3200
3201 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3202 return -EINVAL;
3203
3204 sock = sock_from_file(req->file, &ret);
3205 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003206 struct io_async_ctx io;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003207 unsigned flags;
3208
Jens Axboe03b12302019-12-02 18:50:25 -07003209 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07003210 kmsg = &req->io->msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003211 kmsg->msg.msg_name = &req->io->msg.addr;
Jens Axboe0b416c32019-12-15 10:57:46 -07003212 /* if iov is set, it's allocated already */
3213 if (!kmsg->iov)
3214 kmsg->iov = kmsg->fast_iov;
3215 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07003216 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003217 struct io_sr_msg *sr = &req->sr_msg;
3218
Jens Axboe0b416c32019-12-15 10:57:46 -07003219 kmsg = &io.msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003220 kmsg->msg.msg_name = &io.msg.addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003221
3222 io.msg.iov = io.msg.fast_iov;
3223 ret = recvmsg_copy_msghdr(&io.msg.msg, sr->msg,
3224 sr->msg_flags, &io.msg.uaddr,
3225 &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07003226 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003227 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003228 }
Jens Axboe0fa03c62019-04-19 13:34:07 -06003229
Jens Axboee47293f2019-12-20 08:58:21 -07003230 flags = req->sr_msg.msg_flags;
3231 if (flags & MSG_DONTWAIT)
3232 req->flags |= REQ_F_NOWAIT;
3233 else if (force_nonblock)
3234 flags |= MSG_DONTWAIT;
3235
3236 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.msg,
3237 kmsg->uaddr, flags);
Jens Axboe03b12302019-12-02 18:50:25 -07003238 if (force_nonblock && ret == -EAGAIN) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003239 if (req->io)
3240 return -EAGAIN;
Pavel Begunkov1e950812020-02-06 19:51:16 +03003241 if (io_alloc_async_ctx(req)) {
Dan Carpenter297a31e2020-02-17 17:39:45 +03003242 if (kmsg->iov != kmsg->fast_iov)
Pavel Begunkov1e950812020-02-06 19:51:16 +03003243 kfree(kmsg->iov);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003244 return -ENOMEM;
Pavel Begunkov1e950812020-02-06 19:51:16 +03003245 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003246 memcpy(&req->io->msg, &io.msg, sizeof(io.msg));
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003247 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe0b416c32019-12-15 10:57:46 -07003248 return -EAGAIN;
Jens Axboe03b12302019-12-02 18:50:25 -07003249 }
Jens Axboe441cdbd2019-12-02 18:49:10 -07003250 if (ret == -ERESTARTSYS)
3251 ret = -EINTR;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003252 }
3253
Pavel Begunkov1e950812020-02-06 19:51:16 +03003254 if (kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07003255 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003256 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe78e19bb2019-11-06 15:21:34 -07003257 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003258 if (ret < 0)
3259 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003260 io_put_req_find_next(req, nxt);
Jens Axboe0fa03c62019-04-19 13:34:07 -06003261 return 0;
3262#else
3263 return -EOPNOTSUPP;
3264#endif
3265}
3266
Jens Axboefddafac2020-01-04 20:19:44 -07003267static int io_recv(struct io_kiocb *req, struct io_kiocb **nxt,
3268 bool force_nonblock)
3269{
3270#if defined(CONFIG_NET)
3271 struct socket *sock;
3272 int ret;
3273
3274 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3275 return -EINVAL;
3276
3277 sock = sock_from_file(req->file, &ret);
3278 if (sock) {
3279 struct io_sr_msg *sr = &req->sr_msg;
3280 struct msghdr msg;
3281 struct iovec iov;
3282 unsigned flags;
3283
3284 ret = import_single_range(READ, sr->buf, sr->len, &iov,
3285 &msg.msg_iter);
3286 if (ret)
3287 return ret;
3288
3289 msg.msg_name = NULL;
3290 msg.msg_control = NULL;
3291 msg.msg_controllen = 0;
3292 msg.msg_namelen = 0;
3293 msg.msg_iocb = NULL;
3294 msg.msg_flags = 0;
3295
3296 flags = req->sr_msg.msg_flags;
3297 if (flags & MSG_DONTWAIT)
3298 req->flags |= REQ_F_NOWAIT;
3299 else if (force_nonblock)
3300 flags |= MSG_DONTWAIT;
3301
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003302 ret = sock_recvmsg(sock, &msg, flags);
Jens Axboefddafac2020-01-04 20:19:44 -07003303 if (force_nonblock && ret == -EAGAIN)
3304 return -EAGAIN;
3305 if (ret == -ERESTARTSYS)
3306 ret = -EINTR;
3307 }
3308
3309 io_cqring_add_event(req, ret);
3310 if (ret < 0)
3311 req_set_fail_links(req);
3312 io_put_req_find_next(req, nxt);
3313 return 0;
3314#else
3315 return -EOPNOTSUPP;
3316#endif
3317}
3318
3319
Jens Axboe3529d8c2019-12-19 18:24:38 -07003320static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003321{
3322#if defined(CONFIG_NET)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003323 struct io_accept *accept = &req->accept;
3324
Jens Axboe17f2fe32019-10-17 14:42:58 -06003325 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3326 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05003327 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003328 return -EINVAL;
3329
Jens Axboed55e5f52019-12-11 16:12:15 -07003330 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3331 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003332 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003333 return 0;
3334#else
3335 return -EOPNOTSUPP;
3336#endif
3337}
Jens Axboe17f2fe32019-10-17 14:42:58 -06003338
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003339#if defined(CONFIG_NET)
3340static int __io_accept(struct io_kiocb *req, struct io_kiocb **nxt,
3341 bool force_nonblock)
3342{
3343 struct io_accept *accept = &req->accept;
3344 unsigned file_flags;
3345 int ret;
3346
3347 file_flags = force_nonblock ? O_NONBLOCK : 0;
3348 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
3349 accept->addr_len, accept->flags);
3350 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003351 return -EAGAIN;
Jens Axboe8e3cca12019-11-09 19:52:33 -07003352 if (ret == -ERESTARTSYS)
3353 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003354 if (ret < 0)
3355 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07003356 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003357 io_put_req_find_next(req, nxt);
Jens Axboe17f2fe32019-10-17 14:42:58 -06003358 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003359}
3360
3361static void io_accept_finish(struct io_wq_work **workptr)
3362{
3363 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3364 struct io_kiocb *nxt = NULL;
3365
Jens Axboee441d1c2020-02-20 09:59:02 -07003366 io_put_req(req);
3367
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003368 if (io_req_cancelled(req))
3369 return;
3370 __io_accept(req, &nxt, false);
3371 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07003372 io_wq_assign_next(workptr, nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003373}
3374#endif
3375
3376static int io_accept(struct io_kiocb *req, struct io_kiocb **nxt,
3377 bool force_nonblock)
3378{
3379#if defined(CONFIG_NET)
3380 int ret;
3381
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003382 ret = __io_accept(req, nxt, force_nonblock);
3383 if (ret == -EAGAIN && force_nonblock) {
3384 req->work.func = io_accept_finish;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003385 return -EAGAIN;
3386 }
3387 return 0;
Jens Axboe17f2fe32019-10-17 14:42:58 -06003388#else
3389 return -EOPNOTSUPP;
3390#endif
3391}
3392
Jens Axboe3529d8c2019-12-19 18:24:38 -07003393static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07003394{
3395#if defined(CONFIG_NET)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003396 struct io_connect *conn = &req->connect;
3397 struct io_async_ctx *io = req->io;
Jens Axboef499a022019-12-02 16:28:46 -07003398
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003399 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3400 return -EINVAL;
3401 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
3402 return -EINVAL;
3403
Jens Axboe3529d8c2019-12-19 18:24:38 -07003404 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3405 conn->addr_len = READ_ONCE(sqe->addr2);
3406
3407 if (!io)
3408 return 0;
3409
3410 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003411 &io->connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07003412#else
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003413 return -EOPNOTSUPP;
Jens Axboef499a022019-12-02 16:28:46 -07003414#endif
3415}
3416
Jens Axboefc4df992019-12-10 14:38:45 -07003417static int io_connect(struct io_kiocb *req, struct io_kiocb **nxt,
3418 bool force_nonblock)
Jens Axboef8e85cf2019-11-23 14:24:24 -07003419{
3420#if defined(CONFIG_NET)
Jens Axboef499a022019-12-02 16:28:46 -07003421 struct io_async_ctx __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003422 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003423 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003424
Jens Axboef499a022019-12-02 16:28:46 -07003425 if (req->io) {
3426 io = req->io;
3427 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003428 ret = move_addr_to_kernel(req->connect.addr,
3429 req->connect.addr_len,
3430 &__io.connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07003431 if (ret)
3432 goto out;
3433 io = &__io;
3434 }
3435
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003436 file_flags = force_nonblock ? O_NONBLOCK : 0;
3437
3438 ret = __sys_connect_file(req->file, &io->connect.address,
3439 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07003440 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003441 if (req->io)
3442 return -EAGAIN;
3443 if (io_alloc_async_ctx(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07003444 ret = -ENOMEM;
3445 goto out;
3446 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003447 memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect));
Jens Axboef8e85cf2019-11-23 14:24:24 -07003448 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07003449 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07003450 if (ret == -ERESTARTSYS)
3451 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07003452out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003453 if (ret < 0)
3454 req_set_fail_links(req);
Jens Axboef8e85cf2019-11-23 14:24:24 -07003455 io_cqring_add_event(req, ret);
3456 io_put_req_find_next(req, nxt);
3457 return 0;
3458#else
3459 return -EOPNOTSUPP;
3460#endif
3461}
3462
Jens Axboe221c5eb2019-01-17 09:41:58 -07003463static void io_poll_remove_one(struct io_kiocb *req)
3464{
3465 struct io_poll_iocb *poll = &req->poll;
3466
3467 spin_lock(&poll->head->lock);
3468 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07003469 if (!list_empty(&poll->wait.entry)) {
3470 list_del_init(&poll->wait.entry);
Jackie Liua197f662019-11-08 08:09:12 -07003471 io_queue_async_work(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003472 }
3473 spin_unlock(&poll->head->lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07003474 hash_del(&req->hash_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003475}
3476
3477static void io_poll_remove_all(struct io_ring_ctx *ctx)
3478{
Jens Axboe78076bb2019-12-04 19:56:40 -07003479 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003480 struct io_kiocb *req;
Jens Axboe78076bb2019-12-04 19:56:40 -07003481 int i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003482
3483 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07003484 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
3485 struct hlist_head *list;
3486
3487 list = &ctx->cancel_hash[i];
3488 hlist_for_each_entry_safe(req, tmp, list, hash_node)
3489 io_poll_remove_one(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003490 }
3491 spin_unlock_irq(&ctx->completion_lock);
3492}
3493
Jens Axboe47f46762019-11-09 17:43:02 -07003494static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
3495{
Jens Axboe78076bb2019-12-04 19:56:40 -07003496 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07003497 struct io_kiocb *req;
3498
Jens Axboe78076bb2019-12-04 19:56:40 -07003499 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
3500 hlist_for_each_entry(req, list, hash_node) {
3501 if (sqe_addr == req->user_data) {
Jens Axboeeac406c2019-11-14 12:09:58 -07003502 io_poll_remove_one(req);
3503 return 0;
3504 }
Jens Axboe47f46762019-11-09 17:43:02 -07003505 }
3506
3507 return -ENOENT;
3508}
3509
Jens Axboe3529d8c2019-12-19 18:24:38 -07003510static int io_poll_remove_prep(struct io_kiocb *req,
3511 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003512{
Jens Axboe221c5eb2019-01-17 09:41:58 -07003513 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3514 return -EINVAL;
3515 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
3516 sqe->poll_events)
3517 return -EINVAL;
3518
Jens Axboe0969e782019-12-17 18:40:57 -07003519 req->poll.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07003520 return 0;
3521}
3522
3523/*
3524 * Find a running poll command that matches one specified in sqe->addr,
3525 * and remove it if found.
3526 */
3527static int io_poll_remove(struct io_kiocb *req)
3528{
3529 struct io_ring_ctx *ctx = req->ctx;
3530 u64 addr;
3531 int ret;
3532
Jens Axboe0969e782019-12-17 18:40:57 -07003533 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003534 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07003535 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003536 spin_unlock_irq(&ctx->completion_lock);
3537
Jens Axboe78e19bb2019-11-06 15:21:34 -07003538 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003539 if (ret < 0)
3540 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06003541 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003542 return 0;
3543}
3544
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003545static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003546{
Jackie Liua197f662019-11-08 08:09:12 -07003547 struct io_ring_ctx *ctx = req->ctx;
3548
Jens Axboe8c838782019-03-12 15:48:16 -06003549 req->poll.done = true;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003550 if (error)
3551 io_cqring_fill_event(req, error);
3552 else
3553 io_cqring_fill_event(req, mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06003554 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003555}
3556
Jens Axboe561fb042019-10-24 07:25:42 -06003557static void io_poll_complete_work(struct io_wq_work **workptr)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003558{
Jens Axboe561fb042019-10-24 07:25:42 -06003559 struct io_wq_work *work = *workptr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003560 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
3561 struct io_poll_iocb *poll = &req->poll;
3562 struct poll_table_struct pt = { ._key = poll->events };
3563 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe89723d02019-11-05 15:32:58 -07003564 struct io_kiocb *nxt = NULL;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003565 __poll_t mask = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003566 int ret = 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003567
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003568 if (work->flags & IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06003569 WRITE_ONCE(poll->canceled, true);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003570 ret = -ECANCELED;
3571 } else if (READ_ONCE(poll->canceled)) {
3572 ret = -ECANCELED;
3573 }
Jens Axboe561fb042019-10-24 07:25:42 -06003574
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003575 if (ret != -ECANCELED)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003576 mask = vfs_poll(poll->file, &pt) & poll->events;
3577
3578 /*
3579 * Note that ->ki_cancel callers also delete iocb from active_reqs after
3580 * calling ->ki_cancel. We need the ctx_lock roundtrip here to
3581 * synchronize with them. In the cancellation case the list_del_init
3582 * itself is not actually needed, but harmless so we keep it in to
3583 * avoid further branches in the fast path.
3584 */
3585 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003586 if (!mask && ret != -ECANCELED) {
Jens Axboe392edb42019-12-09 17:52:20 -07003587 add_wait_queue(poll->head, &poll->wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003588 spin_unlock_irq(&ctx->completion_lock);
3589 return;
3590 }
Jens Axboe78076bb2019-12-04 19:56:40 -07003591 hash_del(&req->hash_node);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003592 io_poll_complete(req, mask, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003593 spin_unlock_irq(&ctx->completion_lock);
3594
Jens Axboe8c838782019-03-12 15:48:16 -06003595 io_cqring_ev_posted(ctx);
Jens Axboe89723d02019-11-05 15:32:58 -07003596
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003597 if (ret < 0)
3598 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003599 io_put_req_find_next(req, &nxt);
Jens Axboe89723d02019-11-05 15:32:58 -07003600 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07003601 io_wq_assign_next(workptr, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003602}
3603
Jens Axboee94f1412019-12-19 12:06:02 -07003604static void __io_poll_flush(struct io_ring_ctx *ctx, struct llist_node *nodes)
3605{
Jens Axboee94f1412019-12-19 12:06:02 -07003606 struct io_kiocb *req, *tmp;
Jens Axboe8237e042019-12-28 10:48:22 -07003607 struct req_batch rb;
Jens Axboee94f1412019-12-19 12:06:02 -07003608
Jens Axboec6ca97b302019-12-28 12:11:08 -07003609 rb.to_free = rb.need_iter = 0;
Jens Axboee94f1412019-12-19 12:06:02 -07003610 spin_lock_irq(&ctx->completion_lock);
3611 llist_for_each_entry_safe(req, tmp, nodes, llist_node) {
3612 hash_del(&req->hash_node);
3613 io_poll_complete(req, req->result, 0);
3614
Jens Axboe8237e042019-12-28 10:48:22 -07003615 if (refcount_dec_and_test(&req->refs) &&
3616 !io_req_multi_free(&rb, req)) {
3617 req->flags |= REQ_F_COMP_LOCKED;
3618 io_free_req(req);
Jens Axboee94f1412019-12-19 12:06:02 -07003619 }
3620 }
3621 spin_unlock_irq(&ctx->completion_lock);
3622
3623 io_cqring_ev_posted(ctx);
Jens Axboe8237e042019-12-28 10:48:22 -07003624 io_free_req_many(ctx, &rb);
Jens Axboee94f1412019-12-19 12:06:02 -07003625}
3626
3627static void io_poll_flush(struct io_wq_work **workptr)
3628{
3629 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3630 struct llist_node *nodes;
3631
3632 nodes = llist_del_all(&req->ctx->poll_llist);
3633 if (nodes)
3634 __io_poll_flush(req->ctx, nodes);
3635}
3636
Jens Axboef0b493e2020-02-01 21:30:11 -07003637static void io_poll_trigger_evfd(struct io_wq_work **workptr)
3638{
3639 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3640
3641 eventfd_signal(req->ctx->cq_ev_fd, 1);
3642 io_put_req(req);
3643}
3644
Jens Axboe221c5eb2019-01-17 09:41:58 -07003645static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
3646 void *key)
3647{
Jens Axboee9444752019-11-26 15:02:04 -07003648 struct io_poll_iocb *poll = wait->private;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003649 struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
3650 struct io_ring_ctx *ctx = req->ctx;
3651 __poll_t mask = key_to_poll(key);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003652
3653 /* for instances that support it check for an event match first: */
Jens Axboe8c838782019-03-12 15:48:16 -06003654 if (mask && !(mask & poll->events))
3655 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003656
Jens Axboe392edb42019-12-09 17:52:20 -07003657 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06003658
Jens Axboe7c9e7f02019-11-12 08:15:53 -07003659 /*
3660 * Run completion inline if we can. We're using trylock here because
3661 * we are violating the completion_lock -> poll wq lock ordering.
3662 * If we have a link timeout we're going to need the completion_lock
3663 * for finalizing the request, mark us as having grabbed that already.
3664 */
Jens Axboee94f1412019-12-19 12:06:02 -07003665 if (mask) {
3666 unsigned long flags;
Jens Axboe8c838782019-03-12 15:48:16 -06003667
Jens Axboee94f1412019-12-19 12:06:02 -07003668 if (llist_empty(&ctx->poll_llist) &&
3669 spin_trylock_irqsave(&ctx->completion_lock, flags)) {
Jens Axboef0b493e2020-02-01 21:30:11 -07003670 bool trigger_ev;
3671
Jens Axboee94f1412019-12-19 12:06:02 -07003672 hash_del(&req->hash_node);
3673 io_poll_complete(req, mask, 0);
Jens Axboee94f1412019-12-19 12:06:02 -07003674
Jens Axboef0b493e2020-02-01 21:30:11 -07003675 trigger_ev = io_should_trigger_evfd(ctx);
3676 if (trigger_ev && eventfd_signal_count()) {
3677 trigger_ev = false;
3678 req->work.func = io_poll_trigger_evfd;
3679 } else {
3680 req->flags |= REQ_F_COMP_LOCKED;
3681 io_put_req(req);
3682 req = NULL;
3683 }
3684 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3685 __io_cqring_ev_posted(ctx, trigger_ev);
Jens Axboee94f1412019-12-19 12:06:02 -07003686 } else {
3687 req->result = mask;
3688 req->llist_node.next = NULL;
3689 /* if the list wasn't empty, we're done */
3690 if (!llist_add(&req->llist_node, &ctx->poll_llist))
3691 req = NULL;
3692 else
3693 req->work.func = io_poll_flush;
3694 }
Jens Axboe8c838782019-03-12 15:48:16 -06003695 }
Jens Axboee94f1412019-12-19 12:06:02 -07003696 if (req)
3697 io_queue_async_work(req);
Jens Axboe8c838782019-03-12 15:48:16 -06003698
Jens Axboe221c5eb2019-01-17 09:41:58 -07003699 return 1;
3700}
3701
3702struct io_poll_table {
3703 struct poll_table_struct pt;
3704 struct io_kiocb *req;
3705 int error;
3706};
3707
3708static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
3709 struct poll_table_struct *p)
3710{
3711 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
3712
3713 if (unlikely(pt->req->poll.head)) {
3714 pt->error = -EINVAL;
3715 return;
3716 }
3717
3718 pt->error = 0;
3719 pt->req->poll.head = head;
Jens Axboe392edb42019-12-09 17:52:20 -07003720 add_wait_queue(head, &pt->req->poll.wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003721}
3722
Jens Axboeeac406c2019-11-14 12:09:58 -07003723static void io_poll_req_insert(struct io_kiocb *req)
3724{
3725 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07003726 struct hlist_head *list;
Jens Axboeeac406c2019-11-14 12:09:58 -07003727
Jens Axboe78076bb2019-12-04 19:56:40 -07003728 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
3729 hlist_add_head(&req->hash_node, list);
Jens Axboeeac406c2019-11-14 12:09:58 -07003730}
3731
Jens Axboe3529d8c2019-12-19 18:24:38 -07003732static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003733{
3734 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003735 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003736
3737 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3738 return -EINVAL;
3739 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
3740 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06003741 if (!poll->file)
3742 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003743
Jens Axboe221c5eb2019-01-17 09:41:58 -07003744 events = READ_ONCE(sqe->poll_events);
3745 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
Jens Axboe0969e782019-12-17 18:40:57 -07003746 return 0;
3747}
3748
3749static int io_poll_add(struct io_kiocb *req, struct io_kiocb **nxt)
3750{
3751 struct io_poll_iocb *poll = &req->poll;
3752 struct io_ring_ctx *ctx = req->ctx;
3753 struct io_poll_table ipt;
3754 bool cancel = false;
3755 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07003756
3757 INIT_IO_WORK(&req->work, io_poll_complete_work);
Jens Axboe78076bb2019-12-04 19:56:40 -07003758 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003759
Jens Axboe221c5eb2019-01-17 09:41:58 -07003760 poll->head = NULL;
Jens Axboe8c838782019-03-12 15:48:16 -06003761 poll->done = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003762 poll->canceled = false;
3763
3764 ipt.pt._qproc = io_poll_queue_proc;
3765 ipt.pt._key = poll->events;
3766 ipt.req = req;
3767 ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
3768
3769 /* initialized the list so that we can do list_empty checks */
Jens Axboe392edb42019-12-09 17:52:20 -07003770 INIT_LIST_HEAD(&poll->wait.entry);
3771 init_waitqueue_func_entry(&poll->wait, io_poll_wake);
3772 poll->wait.private = poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003773
Jens Axboe36703242019-07-25 10:20:18 -06003774 INIT_LIST_HEAD(&req->list);
3775
Jens Axboe221c5eb2019-01-17 09:41:58 -07003776 mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003777
3778 spin_lock_irq(&ctx->completion_lock);
Jens Axboe8c838782019-03-12 15:48:16 -06003779 if (likely(poll->head)) {
3780 spin_lock(&poll->head->lock);
Jens Axboe392edb42019-12-09 17:52:20 -07003781 if (unlikely(list_empty(&poll->wait.entry))) {
Jens Axboe8c838782019-03-12 15:48:16 -06003782 if (ipt.error)
3783 cancel = true;
3784 ipt.error = 0;
3785 mask = 0;
3786 }
3787 if (mask || ipt.error)
Jens Axboe392edb42019-12-09 17:52:20 -07003788 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06003789 else if (cancel)
3790 WRITE_ONCE(poll->canceled, true);
3791 else if (!poll->done) /* actually waiting for an event */
Jens Axboeeac406c2019-11-14 12:09:58 -07003792 io_poll_req_insert(req);
Jens Axboe8c838782019-03-12 15:48:16 -06003793 spin_unlock(&poll->head->lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003794 }
Jens Axboe8c838782019-03-12 15:48:16 -06003795 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06003796 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003797 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06003798 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07003799 spin_unlock_irq(&ctx->completion_lock);
3800
Jens Axboe8c838782019-03-12 15:48:16 -06003801 if (mask) {
3802 io_cqring_ev_posted(ctx);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003803 io_put_req_find_next(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003804 }
Jens Axboe8c838782019-03-12 15:48:16 -06003805 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003806}
3807
Jens Axboe5262f562019-09-17 12:26:57 -06003808static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
3809{
Jens Axboead8a48a2019-11-15 08:49:11 -07003810 struct io_timeout_data *data = container_of(timer,
3811 struct io_timeout_data, timer);
3812 struct io_kiocb *req = data->req;
3813 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06003814 unsigned long flags;
3815
Jens Axboe5262f562019-09-17 12:26:57 -06003816 atomic_inc(&ctx->cq_timeouts);
3817
3818 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08003819 /*
Jens Axboe11365042019-10-16 09:08:32 -06003820 * We could be racing with timeout deletion. If the list is empty,
3821 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08003822 */
Jens Axboe842f9612019-10-29 12:34:10 -06003823 if (!list_empty(&req->list)) {
Jens Axboe11365042019-10-16 09:08:32 -06003824 struct io_kiocb *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06003825
Jens Axboe11365042019-10-16 09:08:32 -06003826 /*
3827 * Adjust the reqs sequence before the current one because it
Brian Gianforcarod195a662019-12-13 03:09:50 -08003828 * will consume a slot in the cq_ring and the cq_tail
Jens Axboe11365042019-10-16 09:08:32 -06003829 * pointer will be increased, otherwise other timeout reqs may
3830 * return in advance without waiting for enough wait_nr.
3831 */
3832 prev = req;
3833 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
3834 prev->sequence++;
Jens Axboe11365042019-10-16 09:08:32 -06003835 list_del_init(&req->list);
Jens Axboe11365042019-10-16 09:08:32 -06003836 }
Jens Axboe842f9612019-10-29 12:34:10 -06003837
Jens Axboe78e19bb2019-11-06 15:21:34 -07003838 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06003839 io_commit_cqring(ctx);
3840 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3841
3842 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003843 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06003844 io_put_req(req);
3845 return HRTIMER_NORESTART;
3846}
3847
Jens Axboe47f46762019-11-09 17:43:02 -07003848static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
3849{
3850 struct io_kiocb *req;
3851 int ret = -ENOENT;
3852
3853 list_for_each_entry(req, &ctx->timeout_list, list) {
3854 if (user_data == req->user_data) {
3855 list_del_init(&req->list);
3856 ret = 0;
3857 break;
3858 }
3859 }
3860
3861 if (ret == -ENOENT)
3862 return ret;
3863
Jens Axboe2d283902019-12-04 11:08:05 -07003864 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe47f46762019-11-09 17:43:02 -07003865 if (ret == -1)
3866 return -EALREADY;
3867
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003868 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07003869 io_cqring_fill_event(req, -ECANCELED);
3870 io_put_req(req);
3871 return 0;
3872}
3873
Jens Axboe3529d8c2019-12-19 18:24:38 -07003874static int io_timeout_remove_prep(struct io_kiocb *req,
3875 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07003876{
Jens Axboeb29472e2019-12-17 18:50:29 -07003877 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3878 return -EINVAL;
3879 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
3880 return -EINVAL;
3881
3882 req->timeout.addr = READ_ONCE(sqe->addr);
3883 req->timeout.flags = READ_ONCE(sqe->timeout_flags);
3884 if (req->timeout.flags)
3885 return -EINVAL;
3886
Jens Axboeb29472e2019-12-17 18:50:29 -07003887 return 0;
3888}
3889
Jens Axboe11365042019-10-16 09:08:32 -06003890/*
3891 * Remove or update an existing timeout command
3892 */
Jens Axboefc4df992019-12-10 14:38:45 -07003893static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06003894{
3895 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07003896 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06003897
Jens Axboe11365042019-10-16 09:08:32 -06003898 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb29472e2019-12-17 18:50:29 -07003899 ret = io_timeout_cancel(ctx, req->timeout.addr);
Jens Axboe11365042019-10-16 09:08:32 -06003900
Jens Axboe47f46762019-11-09 17:43:02 -07003901 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06003902 io_commit_cqring(ctx);
3903 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06003904 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003905 if (ret < 0)
3906 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003907 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06003908 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06003909}
3910
Jens Axboe3529d8c2019-12-19 18:24:38 -07003911static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07003912 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06003913{
Jens Axboead8a48a2019-11-15 08:49:11 -07003914 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06003915 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06003916
Jens Axboead8a48a2019-11-15 08:49:11 -07003917 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06003918 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07003919 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06003920 return -EINVAL;
Jens Axboe2d283902019-12-04 11:08:05 -07003921 if (sqe->off && is_timeout_link)
3922 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06003923 flags = READ_ONCE(sqe->timeout_flags);
3924 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06003925 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06003926
Jens Axboe26a61672019-12-20 09:02:01 -07003927 req->timeout.count = READ_ONCE(sqe->off);
3928
Jens Axboe3529d8c2019-12-19 18:24:38 -07003929 if (!req->io && io_alloc_async_ctx(req))
Jens Axboe26a61672019-12-20 09:02:01 -07003930 return -ENOMEM;
3931
3932 data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07003933 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07003934 req->flags |= REQ_F_TIMEOUT;
3935
3936 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06003937 return -EFAULT;
3938
Jens Axboe11365042019-10-16 09:08:32 -06003939 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07003940 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06003941 else
Jens Axboead8a48a2019-11-15 08:49:11 -07003942 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06003943
Jens Axboead8a48a2019-11-15 08:49:11 -07003944 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
3945 return 0;
3946}
3947
Jens Axboefc4df992019-12-10 14:38:45 -07003948static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07003949{
3950 unsigned count;
3951 struct io_ring_ctx *ctx = req->ctx;
3952 struct io_timeout_data *data;
3953 struct list_head *entry;
3954 unsigned span = 0;
Jens Axboead8a48a2019-11-15 08:49:11 -07003955
Jens Axboe2d283902019-12-04 11:08:05 -07003956 data = &req->io->timeout;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003957
Jens Axboe5262f562019-09-17 12:26:57 -06003958 /*
3959 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07003960 * timeout event to be satisfied. If it isn't set, then this is
3961 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06003962 */
Jens Axboe26a61672019-12-20 09:02:01 -07003963 count = req->timeout.count;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003964 if (!count) {
3965 req->flags |= REQ_F_TIMEOUT_NOSEQ;
3966 spin_lock_irq(&ctx->completion_lock);
3967 entry = ctx->timeout_list.prev;
3968 goto add;
3969 }
Jens Axboe5262f562019-09-17 12:26:57 -06003970
3971 req->sequence = ctx->cached_sq_head + count - 1;
Jens Axboe2d283902019-12-04 11:08:05 -07003972 data->seq_offset = count;
Jens Axboe5262f562019-09-17 12:26:57 -06003973
3974 /*
3975 * Insertion sort, ensuring the first entry in the list is always
3976 * the one we need first.
3977 */
Jens Axboe5262f562019-09-17 12:26:57 -06003978 spin_lock_irq(&ctx->completion_lock);
3979 list_for_each_prev(entry, &ctx->timeout_list) {
3980 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
yangerkun5da0fb12019-10-15 21:59:29 +08003981 unsigned nxt_sq_head;
3982 long long tmp, tmp_nxt;
Jens Axboe2d283902019-12-04 11:08:05 -07003983 u32 nxt_offset = nxt->io->timeout.seq_offset;
Jens Axboe5262f562019-09-17 12:26:57 -06003984
Jens Axboe93bd25b2019-11-11 23:34:31 -07003985 if (nxt->flags & REQ_F_TIMEOUT_NOSEQ)
3986 continue;
3987
yangerkun5da0fb12019-10-15 21:59:29 +08003988 /*
3989 * Since cached_sq_head + count - 1 can overflow, use type long
3990 * long to store it.
3991 */
3992 tmp = (long long)ctx->cached_sq_head + count - 1;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +03003993 nxt_sq_head = nxt->sequence - nxt_offset + 1;
3994 tmp_nxt = (long long)nxt_sq_head + nxt_offset - 1;
yangerkun5da0fb12019-10-15 21:59:29 +08003995
3996 /*
3997 * cached_sq_head may overflow, and it will never overflow twice
3998 * once there is some timeout req still be valid.
3999 */
4000 if (ctx->cached_sq_head < nxt_sq_head)
yangerkun8b07a652019-10-17 12:12:35 +08004001 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08004002
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08004003 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06004004 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08004005
4006 /*
4007 * Sequence of reqs after the insert one and itself should
4008 * be adjusted because each timeout req consumes a slot.
4009 */
4010 span++;
4011 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06004012 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08004013 req->sequence -= span;
Jens Axboe93bd25b2019-11-11 23:34:31 -07004014add:
Jens Axboe5262f562019-09-17 12:26:57 -06004015 list_add(&req->list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07004016 data->timer.function = io_timeout_fn;
4017 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06004018 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06004019 return 0;
4020}
4021
Jens Axboe62755e32019-10-28 21:49:21 -06004022static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06004023{
Jens Axboe62755e32019-10-28 21:49:21 -06004024 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06004025
Jens Axboe62755e32019-10-28 21:49:21 -06004026 return req->user_data == (unsigned long) data;
4027}
4028
Jens Axboee977d6d2019-11-05 12:39:45 -07004029static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06004030{
Jens Axboe62755e32019-10-28 21:49:21 -06004031 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06004032 int ret = 0;
4033
Jens Axboe62755e32019-10-28 21:49:21 -06004034 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
4035 switch (cancel_ret) {
4036 case IO_WQ_CANCEL_OK:
4037 ret = 0;
4038 break;
4039 case IO_WQ_CANCEL_RUNNING:
4040 ret = -EALREADY;
4041 break;
4042 case IO_WQ_CANCEL_NOTFOUND:
4043 ret = -ENOENT;
4044 break;
4045 }
4046
Jens Axboee977d6d2019-11-05 12:39:45 -07004047 return ret;
4048}
4049
Jens Axboe47f46762019-11-09 17:43:02 -07004050static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
4051 struct io_kiocb *req, __u64 sqe_addr,
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004052 struct io_kiocb **nxt, int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07004053{
4054 unsigned long flags;
4055 int ret;
4056
4057 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
4058 if (ret != -ENOENT) {
4059 spin_lock_irqsave(&ctx->completion_lock, flags);
4060 goto done;
4061 }
4062
4063 spin_lock_irqsave(&ctx->completion_lock, flags);
4064 ret = io_timeout_cancel(ctx, sqe_addr);
4065 if (ret != -ENOENT)
4066 goto done;
4067 ret = io_poll_cancel(ctx, sqe_addr);
4068done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004069 if (!ret)
4070 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07004071 io_cqring_fill_event(req, ret);
4072 io_commit_cqring(ctx);
4073 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4074 io_cqring_ev_posted(ctx);
4075
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004076 if (ret < 0)
4077 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07004078 io_put_req_find_next(req, nxt);
4079}
4080
Jens Axboe3529d8c2019-12-19 18:24:38 -07004081static int io_async_cancel_prep(struct io_kiocb *req,
4082 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07004083{
Jens Axboefbf23842019-12-17 18:45:56 -07004084 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07004085 return -EINVAL;
4086 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
4087 sqe->cancel_flags)
4088 return -EINVAL;
4089
Jens Axboefbf23842019-12-17 18:45:56 -07004090 req->cancel.addr = READ_ONCE(sqe->addr);
4091 return 0;
4092}
4093
4094static int io_async_cancel(struct io_kiocb *req, struct io_kiocb **nxt)
4095{
4096 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07004097
4098 io_async_find_and_cancel(ctx, req, req->cancel.addr, nxt, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06004099 return 0;
4100}
4101
Jens Axboe05f3fb32019-12-09 11:22:50 -07004102static int io_files_update_prep(struct io_kiocb *req,
4103 const struct io_uring_sqe *sqe)
4104{
4105 if (sqe->flags || sqe->ioprio || sqe->rw_flags)
4106 return -EINVAL;
4107
4108 req->files_update.offset = READ_ONCE(sqe->off);
4109 req->files_update.nr_args = READ_ONCE(sqe->len);
4110 if (!req->files_update.nr_args)
4111 return -EINVAL;
4112 req->files_update.arg = READ_ONCE(sqe->addr);
4113 return 0;
4114}
4115
4116static int io_files_update(struct io_kiocb *req, bool force_nonblock)
4117{
4118 struct io_ring_ctx *ctx = req->ctx;
4119 struct io_uring_files_update up;
4120 int ret;
4121
Jens Axboef86cd202020-01-29 13:46:44 -07004122 if (force_nonblock)
Jens Axboe05f3fb32019-12-09 11:22:50 -07004123 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004124
4125 up.offset = req->files_update.offset;
4126 up.fds = req->files_update.arg;
4127
4128 mutex_lock(&ctx->uring_lock);
4129 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
4130 mutex_unlock(&ctx->uring_lock);
4131
4132 if (ret < 0)
4133 req_set_fail_links(req);
4134 io_cqring_add_event(req, ret);
4135 io_put_req(req);
4136 return 0;
4137}
4138
Jens Axboe3529d8c2019-12-19 18:24:38 -07004139static int io_req_defer_prep(struct io_kiocb *req,
4140 const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07004141{
Jens Axboee7815732019-12-17 19:45:06 -07004142 ssize_t ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07004143
Jens Axboef86cd202020-01-29 13:46:44 -07004144 if (io_op_defs[req->opcode].file_table) {
4145 ret = io_grab_files(req);
4146 if (unlikely(ret))
4147 return ret;
4148 }
4149
Jens Axboecccf0ee2020-01-27 16:34:48 -07004150 io_req_work_grab_env(req, &io_op_defs[req->opcode]);
4151
Jens Axboed625c6e2019-12-17 19:53:05 -07004152 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07004153 case IORING_OP_NOP:
4154 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004155 case IORING_OP_READV:
4156 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004157 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004158 ret = io_read_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07004159 break;
4160 case IORING_OP_WRITEV:
4161 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004162 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004163 ret = io_write_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07004164 break;
Jens Axboe0969e782019-12-17 18:40:57 -07004165 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004166 ret = io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07004167 break;
4168 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004169 ret = io_poll_remove_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07004170 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004171 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004172 ret = io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004173 break;
4174 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004175 ret = io_prep_sfr(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004176 break;
Jens Axboe03b12302019-12-02 18:50:25 -07004177 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004178 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004179 ret = io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07004180 break;
4181 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004182 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004183 ret = io_recvmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07004184 break;
Jens Axboef499a022019-12-02 16:28:46 -07004185 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004186 ret = io_connect_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07004187 break;
Jens Axboe2d283902019-12-04 11:08:05 -07004188 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004189 ret = io_timeout_prep(req, sqe, false);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004190 break;
Jens Axboeb29472e2019-12-17 18:50:29 -07004191 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004192 ret = io_timeout_remove_prep(req, sqe);
Jens Axboeb29472e2019-12-17 18:50:29 -07004193 break;
Jens Axboefbf23842019-12-17 18:45:56 -07004194 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004195 ret = io_async_cancel_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07004196 break;
Jens Axboe2d283902019-12-04 11:08:05 -07004197 case IORING_OP_LINK_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004198 ret = io_timeout_prep(req, sqe, true);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004199 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004200 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004201 ret = io_accept_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004202 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07004203 case IORING_OP_FALLOCATE:
4204 ret = io_fallocate_prep(req, sqe);
4205 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004206 case IORING_OP_OPENAT:
4207 ret = io_openat_prep(req, sqe);
4208 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07004209 case IORING_OP_CLOSE:
4210 ret = io_close_prep(req, sqe);
4211 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004212 case IORING_OP_FILES_UPDATE:
4213 ret = io_files_update_prep(req, sqe);
4214 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004215 case IORING_OP_STATX:
4216 ret = io_statx_prep(req, sqe);
4217 break;
Jens Axboe4840e412019-12-25 22:03:45 -07004218 case IORING_OP_FADVISE:
4219 ret = io_fadvise_prep(req, sqe);
4220 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07004221 case IORING_OP_MADVISE:
4222 ret = io_madvise_prep(req, sqe);
4223 break;
Jens Axboecebdb982020-01-08 17:59:24 -07004224 case IORING_OP_OPENAT2:
4225 ret = io_openat2_prep(req, sqe);
4226 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004227 case IORING_OP_EPOLL_CTL:
4228 ret = io_epoll_ctl_prep(req, sqe);
4229 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004230 default:
Jens Axboee7815732019-12-17 19:45:06 -07004231 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
4232 req->opcode);
4233 ret = -EINVAL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004234 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004235 }
4236
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004237 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07004238}
4239
Jens Axboe3529d8c2019-12-19 18:24:38 -07004240static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboede0617e2019-04-06 21:51:27 -06004241{
Jackie Liua197f662019-11-08 08:09:12 -07004242 struct io_ring_ctx *ctx = req->ctx;
Jens Axboef67676d2019-12-02 11:03:47 -07004243 int ret;
Jens Axboede0617e2019-04-06 21:51:27 -06004244
Bob Liu9d858b22019-11-13 18:06:25 +08004245 /* Still need defer if there is pending req in defer list. */
4246 if (!req_need_defer(req) && list_empty(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06004247 return 0;
4248
Jens Axboe3529d8c2019-12-19 18:24:38 -07004249 if (!req->io && io_alloc_async_ctx(req))
Jens Axboede0617e2019-04-06 21:51:27 -06004250 return -EAGAIN;
4251
Jens Axboe3529d8c2019-12-19 18:24:38 -07004252 ret = io_req_defer_prep(req, sqe);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004253 if (ret < 0)
Jens Axboe2d283902019-12-04 11:08:05 -07004254 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07004255
Jens Axboede0617e2019-04-06 21:51:27 -06004256 spin_lock_irq(&ctx->completion_lock);
Bob Liu9d858b22019-11-13 18:06:25 +08004257 if (!req_need_defer(req) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06004258 spin_unlock_irq(&ctx->completion_lock);
Jens Axboede0617e2019-04-06 21:51:27 -06004259 return 0;
4260 }
4261
Jens Axboe915967f2019-11-21 09:01:20 -07004262 trace_io_uring_defer(ctx, req, req->user_data);
Jens Axboede0617e2019-04-06 21:51:27 -06004263 list_add_tail(&req->list, &ctx->defer_list);
4264 spin_unlock_irq(&ctx->completion_lock);
4265 return -EIOCBQUEUED;
4266}
4267
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004268static void io_cleanup_req(struct io_kiocb *req)
4269{
4270 struct io_async_ctx *io = req->io;
4271
4272 switch (req->opcode) {
4273 case IORING_OP_READV:
4274 case IORING_OP_READ_FIXED:
4275 case IORING_OP_READ:
4276 case IORING_OP_WRITEV:
4277 case IORING_OP_WRITE_FIXED:
4278 case IORING_OP_WRITE:
4279 if (io->rw.iov != io->rw.fast_iov)
4280 kfree(io->rw.iov);
4281 break;
4282 case IORING_OP_SENDMSG:
4283 case IORING_OP_RECVMSG:
4284 if (io->msg.iov != io->msg.fast_iov)
4285 kfree(io->msg.iov);
4286 break;
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03004287 case IORING_OP_OPENAT:
4288 case IORING_OP_OPENAT2:
4289 case IORING_OP_STATX:
4290 putname(req->open.filename);
4291 break;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004292 }
4293
4294 req->flags &= ~REQ_F_NEED_CLEANUP;
4295}
4296
Jens Axboe3529d8c2019-12-19 18:24:38 -07004297static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
4298 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004299{
Jackie Liua197f662019-11-08 08:09:12 -07004300 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07004301 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004302
Jens Axboed625c6e2019-12-17 19:53:05 -07004303 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07004304 case IORING_OP_NOP:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004305 ret = io_nop(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004306 break;
4307 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004308 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004309 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004310 if (sqe) {
4311 ret = io_read_prep(req, sqe, force_nonblock);
4312 if (ret < 0)
4313 break;
4314 }
Pavel Begunkov267bc902019-11-07 01:41:08 +03004315 ret = io_read(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004316 break;
4317 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07004318 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004319 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004320 if (sqe) {
4321 ret = io_write_prep(req, sqe, force_nonblock);
4322 if (ret < 0)
4323 break;
4324 }
Pavel Begunkov267bc902019-11-07 01:41:08 +03004325 ret = io_write(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004326 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004327 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004328 if (sqe) {
4329 ret = io_prep_fsync(req, sqe);
4330 if (ret < 0)
4331 break;
4332 }
Jens Axboefc4df992019-12-10 14:38:45 -07004333 ret = io_fsync(req, nxt, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004334 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004335 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004336 if (sqe) {
4337 ret = io_poll_add_prep(req, sqe);
4338 if (ret)
4339 break;
4340 }
Jens Axboefc4df992019-12-10 14:38:45 -07004341 ret = io_poll_add(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004342 break;
4343 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004344 if (sqe) {
4345 ret = io_poll_remove_prep(req, sqe);
4346 if (ret < 0)
4347 break;
4348 }
Jens Axboefc4df992019-12-10 14:38:45 -07004349 ret = io_poll_remove(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004350 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004351 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004352 if (sqe) {
4353 ret = io_prep_sfr(req, sqe);
4354 if (ret < 0)
4355 break;
4356 }
Jens Axboefc4df992019-12-10 14:38:45 -07004357 ret = io_sync_file_range(req, nxt, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004358 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004359 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004360 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004361 if (sqe) {
4362 ret = io_sendmsg_prep(req, sqe);
4363 if (ret < 0)
4364 break;
4365 }
Jens Axboefddafac2020-01-04 20:19:44 -07004366 if (req->opcode == IORING_OP_SENDMSG)
4367 ret = io_sendmsg(req, nxt, force_nonblock);
4368 else
4369 ret = io_send(req, nxt, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004370 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06004371 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004372 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004373 if (sqe) {
4374 ret = io_recvmsg_prep(req, sqe);
4375 if (ret)
4376 break;
4377 }
Jens Axboefddafac2020-01-04 20:19:44 -07004378 if (req->opcode == IORING_OP_RECVMSG)
4379 ret = io_recvmsg(req, nxt, force_nonblock);
4380 else
4381 ret = io_recv(req, nxt, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06004382 break;
Jens Axboe5262f562019-09-17 12:26:57 -06004383 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004384 if (sqe) {
4385 ret = io_timeout_prep(req, sqe, false);
4386 if (ret)
4387 break;
4388 }
Jens Axboefc4df992019-12-10 14:38:45 -07004389 ret = io_timeout(req);
Jens Axboe5262f562019-09-17 12:26:57 -06004390 break;
Jens Axboe11365042019-10-16 09:08:32 -06004391 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004392 if (sqe) {
4393 ret = io_timeout_remove_prep(req, sqe);
4394 if (ret)
4395 break;
4396 }
Jens Axboefc4df992019-12-10 14:38:45 -07004397 ret = io_timeout_remove(req);
Jens Axboe11365042019-10-16 09:08:32 -06004398 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06004399 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004400 if (sqe) {
4401 ret = io_accept_prep(req, sqe);
4402 if (ret)
4403 break;
4404 }
Jens Axboefc4df992019-12-10 14:38:45 -07004405 ret = io_accept(req, nxt, force_nonblock);
Jens Axboe17f2fe32019-10-17 14:42:58 -06004406 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004407 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004408 if (sqe) {
4409 ret = io_connect_prep(req, sqe);
4410 if (ret)
4411 break;
4412 }
Jens Axboefc4df992019-12-10 14:38:45 -07004413 ret = io_connect(req, nxt, force_nonblock);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004414 break;
Jens Axboe62755e32019-10-28 21:49:21 -06004415 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004416 if (sqe) {
4417 ret = io_async_cancel_prep(req, sqe);
4418 if (ret)
4419 break;
4420 }
Jens Axboefc4df992019-12-10 14:38:45 -07004421 ret = io_async_cancel(req, nxt);
Jens Axboe62755e32019-10-28 21:49:21 -06004422 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07004423 case IORING_OP_FALLOCATE:
4424 if (sqe) {
4425 ret = io_fallocate_prep(req, sqe);
4426 if (ret)
4427 break;
4428 }
4429 ret = io_fallocate(req, nxt, force_nonblock);
4430 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004431 case IORING_OP_OPENAT:
4432 if (sqe) {
4433 ret = io_openat_prep(req, sqe);
4434 if (ret)
4435 break;
4436 }
4437 ret = io_openat(req, nxt, force_nonblock);
4438 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07004439 case IORING_OP_CLOSE:
4440 if (sqe) {
4441 ret = io_close_prep(req, sqe);
4442 if (ret)
4443 break;
4444 }
4445 ret = io_close(req, nxt, force_nonblock);
4446 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004447 case IORING_OP_FILES_UPDATE:
4448 if (sqe) {
4449 ret = io_files_update_prep(req, sqe);
4450 if (ret)
4451 break;
4452 }
4453 ret = io_files_update(req, force_nonblock);
4454 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004455 case IORING_OP_STATX:
4456 if (sqe) {
4457 ret = io_statx_prep(req, sqe);
4458 if (ret)
4459 break;
4460 }
4461 ret = io_statx(req, nxt, force_nonblock);
4462 break;
Jens Axboe4840e412019-12-25 22:03:45 -07004463 case IORING_OP_FADVISE:
4464 if (sqe) {
4465 ret = io_fadvise_prep(req, sqe);
4466 if (ret)
4467 break;
4468 }
4469 ret = io_fadvise(req, nxt, force_nonblock);
4470 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07004471 case IORING_OP_MADVISE:
4472 if (sqe) {
4473 ret = io_madvise_prep(req, sqe);
4474 if (ret)
4475 break;
4476 }
4477 ret = io_madvise(req, nxt, force_nonblock);
4478 break;
Jens Axboecebdb982020-01-08 17:59:24 -07004479 case IORING_OP_OPENAT2:
4480 if (sqe) {
4481 ret = io_openat2_prep(req, sqe);
4482 if (ret)
4483 break;
4484 }
4485 ret = io_openat2(req, nxt, force_nonblock);
4486 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004487 case IORING_OP_EPOLL_CTL:
4488 if (sqe) {
4489 ret = io_epoll_ctl_prep(req, sqe);
4490 if (ret)
4491 break;
4492 }
4493 ret = io_epoll_ctl(req, nxt, force_nonblock);
4494 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004495 default:
4496 ret = -EINVAL;
4497 break;
4498 }
4499
Jens Axboedef596e2019-01-09 08:59:42 -07004500 if (ret)
4501 return ret;
4502
4503 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe11ba8202020-01-15 21:51:17 -07004504 const bool in_async = io_wq_current_is_worker();
4505
Jens Axboe9e645e112019-05-10 16:07:28 -06004506 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07004507 return -EAGAIN;
4508
Jens Axboe11ba8202020-01-15 21:51:17 -07004509 /* workqueue context doesn't hold uring_lock, grab it now */
4510 if (in_async)
4511 mutex_lock(&ctx->uring_lock);
4512
Jens Axboedef596e2019-01-09 08:59:42 -07004513 io_iopoll_req_issued(req);
Jens Axboe11ba8202020-01-15 21:51:17 -07004514
4515 if (in_async)
4516 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07004517 }
4518
4519 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004520}
4521
Jens Axboe561fb042019-10-24 07:25:42 -06004522static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboe31b51512019-01-18 22:56:34 -07004523{
Jens Axboe561fb042019-10-24 07:25:42 -06004524 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004525 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe561fb042019-10-24 07:25:42 -06004526 struct io_kiocb *nxt = NULL;
4527 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004528
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07004529 /* if NO_CANCEL is set, we must still run the work */
4530 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
4531 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06004532 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07004533 }
Jens Axboe31b51512019-01-18 22:56:34 -07004534
Jens Axboe561fb042019-10-24 07:25:42 -06004535 if (!ret) {
Jens Axboe561fb042019-10-24 07:25:42 -06004536 do {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004537 ret = io_issue_sqe(req, NULL, &nxt, false);
Jens Axboe561fb042019-10-24 07:25:42 -06004538 /*
4539 * We can get EAGAIN for polled IO even though we're
4540 * forcing a sync submission from here, since we can't
4541 * wait for request slots on the block side.
4542 */
4543 if (ret != -EAGAIN)
4544 break;
4545 cond_resched();
4546 } while (1);
4547 }
Jens Axboe31b51512019-01-18 22:56:34 -07004548
Jens Axboe561fb042019-10-24 07:25:42 -06004549 /* drop submission reference */
Jackie Liuec9c02a2019-11-08 23:50:36 +08004550 io_put_req(req);
Jens Axboe817869d2019-04-30 14:44:05 -06004551
Jens Axboe561fb042019-10-24 07:25:42 -06004552 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004553 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07004554 io_cqring_add_event(req, ret);
Jens Axboe817869d2019-04-30 14:44:05 -06004555 io_put_req(req);
Jens Axboeedafcce2019-01-09 09:16:05 -07004556 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004557
Jens Axboe561fb042019-10-24 07:25:42 -06004558 /* if a dependent link is ready, pass it back */
Jens Axboe78912932020-01-14 22:09:06 -07004559 if (!ret && nxt)
4560 io_wq_assign_next(workptr, nxt);
Jens Axboe31b51512019-01-18 22:56:34 -07004561}
Jens Axboe2b188cc2019-01-07 10:46:33 -07004562
Jens Axboe15b71ab2019-12-11 11:20:36 -07004563static int io_req_needs_file(struct io_kiocb *req, int fd)
Jens Axboe9e3aa612019-12-11 15:55:43 -07004564{
Jens Axboed3656342019-12-18 09:50:26 -07004565 if (!io_op_defs[req->opcode].needs_file)
Jens Axboe9e3aa612019-12-11 15:55:43 -07004566 return 0;
Jens Axboe0b5faf62020-02-06 21:42:51 -07004567 if ((fd == -1 || fd == AT_FDCWD) && io_op_defs[req->opcode].fd_non_neg)
Jens Axboed3656342019-12-18 09:50:26 -07004568 return 0;
4569 return 1;
Jens Axboe09bb8392019-03-13 12:39:28 -06004570}
4571
Jens Axboe65e19f52019-10-26 07:20:21 -06004572static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
4573 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06004574{
Jens Axboe65e19f52019-10-26 07:20:21 -06004575 struct fixed_file_table *table;
4576
Jens Axboe05f3fb32019-12-09 11:22:50 -07004577 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
4578 return table->files[index & IORING_FILE_TABLE_MASK];;
Jens Axboe65e19f52019-10-26 07:20:21 -06004579}
4580
Jens Axboe3529d8c2019-12-19 18:24:38 -07004581static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
4582 const struct io_uring_sqe *sqe)
Jens Axboe09bb8392019-03-13 12:39:28 -06004583{
Jackie Liua197f662019-11-08 08:09:12 -07004584 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe09bb8392019-03-13 12:39:28 -06004585 unsigned flags;
Jens Axboed3656342019-12-18 09:50:26 -07004586 int fd;
Jens Axboe09bb8392019-03-13 12:39:28 -06004587
Jens Axboe3529d8c2019-12-19 18:24:38 -07004588 flags = READ_ONCE(sqe->flags);
4589 fd = READ_ONCE(sqe->fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06004590
Jens Axboed3656342019-12-18 09:50:26 -07004591 if (!io_req_needs_file(req, fd))
4592 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06004593
4594 if (flags & IOSQE_FIXED_FILE) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07004595 if (unlikely(!ctx->file_data ||
Jens Axboe09bb8392019-03-13 12:39:28 -06004596 (unsigned) fd >= ctx->nr_user_files))
4597 return -EBADF;
Jens Axboeb7620122019-10-26 07:22:55 -06004598 fd = array_index_nospec(fd, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06004599 req->file = io_file_from_index(ctx, fd);
4600 if (!req->file)
Jens Axboe08a45172019-10-03 08:11:03 -06004601 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06004602 req->flags |= REQ_F_FIXED_FILE;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004603 percpu_ref_get(&ctx->file_data->refs);
Jens Axboe09bb8392019-03-13 12:39:28 -06004604 } else {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004605 if (req->needs_fixed_file)
Jens Axboe09bb8392019-03-13 12:39:28 -06004606 return -EBADF;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004607 trace_io_uring_file_get(ctx, fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06004608 req->file = io_file_get(state, fd);
4609 if (unlikely(!req->file))
4610 return -EBADF;
4611 }
4612
4613 return 0;
4614}
4615
Jackie Liua197f662019-11-08 08:09:12 -07004616static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004617{
Jens Axboefcb323c2019-10-24 12:39:47 -06004618 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07004619 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06004620
Jens Axboef86cd202020-01-29 13:46:44 -07004621 if (req->work.files)
4622 return 0;
Pavel Begunkovb14cca02020-01-17 04:45:59 +03004623 if (!ctx->ring_file)
Jens Axboeb5dba592019-12-11 14:02:38 -07004624 return -EBADF;
4625
Jens Axboefcb323c2019-10-24 12:39:47 -06004626 rcu_read_lock();
4627 spin_lock_irq(&ctx->inflight_lock);
4628 /*
4629 * We use the f_ops->flush() handler to ensure that we can flush
4630 * out work accessing these files if the fd is closed. Check if
4631 * the fd has changed since we started down this path, and disallow
4632 * this operation if it has.
4633 */
Pavel Begunkovb14cca02020-01-17 04:45:59 +03004634 if (fcheck(ctx->ring_fd) == ctx->ring_file) {
Jens Axboefcb323c2019-10-24 12:39:47 -06004635 list_add(&req->inflight_entry, &ctx->inflight_list);
4636 req->flags |= REQ_F_INFLIGHT;
4637 req->work.files = current->files;
4638 ret = 0;
4639 }
4640 spin_unlock_irq(&ctx->inflight_lock);
4641 rcu_read_unlock();
4642
4643 return ret;
4644}
4645
Jens Axboe2665abf2019-11-05 12:40:47 -07004646static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
4647{
Jens Axboead8a48a2019-11-15 08:49:11 -07004648 struct io_timeout_data *data = container_of(timer,
4649 struct io_timeout_data, timer);
4650 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07004651 struct io_ring_ctx *ctx = req->ctx;
4652 struct io_kiocb *prev = NULL;
4653 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07004654
4655 spin_lock_irqsave(&ctx->completion_lock, flags);
4656
4657 /*
4658 * We don't expect the list to be empty, that will only happen if we
4659 * race with the completion of the linked work.
4660 */
Pavel Begunkov44932332019-12-05 16:16:35 +03004661 if (!list_empty(&req->link_list)) {
4662 prev = list_entry(req->link_list.prev, struct io_kiocb,
4663 link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07004664 if (refcount_inc_not_zero(&prev->refs)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03004665 list_del_init(&req->link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07004666 prev->flags &= ~REQ_F_LINK_TIMEOUT;
4667 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07004668 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07004669 }
4670
4671 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4672
4673 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004674 req_set_fail_links(prev);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004675 io_async_find_and_cancel(ctx, req, prev->user_data, NULL,
4676 -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07004677 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07004678 } else {
4679 io_cqring_add_event(req, -ETIME);
4680 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07004681 }
Jens Axboe2665abf2019-11-05 12:40:47 -07004682 return HRTIMER_NORESTART;
4683}
4684
Jens Axboead8a48a2019-11-15 08:49:11 -07004685static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07004686{
Jens Axboe76a46e02019-11-10 23:34:16 -07004687 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07004688
Jens Axboe76a46e02019-11-10 23:34:16 -07004689 /*
4690 * If the list is now empty, then our linked request finished before
4691 * we got a chance to setup the timer
4692 */
4693 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov44932332019-12-05 16:16:35 +03004694 if (!list_empty(&req->link_list)) {
Jens Axboe2d283902019-12-04 11:08:05 -07004695 struct io_timeout_data *data = &req->io->timeout;
Jens Axboe94ae5e72019-11-14 19:39:52 -07004696
Jens Axboead8a48a2019-11-15 08:49:11 -07004697 data->timer.function = io_link_timeout_fn;
4698 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
4699 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07004700 }
Jens Axboe76a46e02019-11-10 23:34:16 -07004701 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07004702
Jens Axboe2665abf2019-11-05 12:40:47 -07004703 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07004704 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07004705}
4706
Jens Axboead8a48a2019-11-15 08:49:11 -07004707static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07004708{
4709 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004710
Jens Axboe2665abf2019-11-05 12:40:47 -07004711 if (!(req->flags & REQ_F_LINK))
4712 return NULL;
4713
Pavel Begunkov44932332019-12-05 16:16:35 +03004714 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
4715 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07004716 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07004717 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07004718
Jens Axboe76a46e02019-11-10 23:34:16 -07004719 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07004720 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07004721}
4722
Jens Axboe3529d8c2019-12-19 18:24:38 -07004723static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004724{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004725 struct io_kiocb *linked_timeout;
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004726 struct io_kiocb *nxt = NULL;
Jens Axboe193155c2020-02-22 23:22:19 -07004727 const struct cred *old_creds = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004728 int ret;
4729
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004730again:
4731 linked_timeout = io_prep_linked_timeout(req);
4732
Jens Axboe193155c2020-02-22 23:22:19 -07004733 if (req->work.creds && req->work.creds != current_cred()) {
4734 if (old_creds)
4735 revert_creds(old_creds);
4736 if (old_creds == req->work.creds)
4737 old_creds = NULL; /* restored original creds */
4738 else
4739 old_creds = override_creds(req->work.creds);
4740 }
4741
Jens Axboe3529d8c2019-12-19 18:24:38 -07004742 ret = io_issue_sqe(req, sqe, &nxt, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06004743
4744 /*
4745 * We async punt it if the file wasn't marked NOWAIT, or if the file
4746 * doesn't support non-blocking read/write attempts
4747 */
4748 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
4749 (req->flags & REQ_F_MUST_PUNT))) {
Pavel Begunkov86a761f2020-01-22 23:09:36 +03004750punt:
Jens Axboef86cd202020-01-29 13:46:44 -07004751 if (io_op_defs[req->opcode].file_table) {
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03004752 ret = io_grab_files(req);
4753 if (ret)
4754 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004755 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03004756
4757 /*
4758 * Queued up for async execution, worker will release
4759 * submit reference when the iocb is actually submitted.
4760 */
4761 io_queue_async_work(req);
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004762 goto done_req;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004763 }
Jens Axboee65ef562019-03-12 10:16:44 -06004764
Jens Axboefcb323c2019-10-24 12:39:47 -06004765err:
Jens Axboee65ef562019-03-12 10:16:44 -06004766 /* drop submission reference */
Jens Axboe2a44f462020-02-25 13:25:41 -07004767 io_put_req_find_next(req, &nxt);
Jens Axboee65ef562019-03-12 10:16:44 -06004768
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004769 if (linked_timeout) {
Jens Axboe76a46e02019-11-10 23:34:16 -07004770 if (!ret)
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004771 io_queue_linked_timeout(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07004772 else
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004773 io_put_req(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07004774 }
4775
Jens Axboee65ef562019-03-12 10:16:44 -06004776 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06004777 if (ret) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07004778 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004779 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06004780 io_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06004781 }
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004782done_req:
4783 if (nxt) {
4784 req = nxt;
4785 nxt = NULL;
Pavel Begunkov86a761f2020-01-22 23:09:36 +03004786
4787 if (req->flags & REQ_F_FORCE_ASYNC)
4788 goto punt;
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004789 goto again;
4790 }
Jens Axboe193155c2020-02-22 23:22:19 -07004791 if (old_creds)
4792 revert_creds(old_creds);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004793}
4794
Jens Axboe3529d8c2019-12-19 18:24:38 -07004795static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jackie Liu4fe2c962019-09-09 20:50:40 +08004796{
4797 int ret;
4798
Jens Axboe3529d8c2019-12-19 18:24:38 -07004799 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004800 if (ret) {
4801 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03004802fail_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004803 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004804 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07004805 io_double_put_req(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004806 }
Pavel Begunkov25508782019-12-30 21:24:47 +03004807 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Pavel Begunkov11185912020-01-22 23:09:35 +03004808 ret = io_req_defer_prep(req, sqe);
4809 if (unlikely(ret < 0))
4810 goto fail_req;
Jens Axboece35a472019-12-17 08:04:44 -07004811 /*
4812 * Never try inline submit of IOSQE_ASYNC is set, go straight
4813 * to async execution.
4814 */
4815 req->work.flags |= IO_WQ_WORK_CONCURRENT;
4816 io_queue_async_work(req);
4817 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004818 __io_queue_sqe(req, sqe);
Jens Axboece35a472019-12-17 08:04:44 -07004819 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08004820}
4821
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004822static inline void io_queue_link_head(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08004823{
Jens Axboe94ae5e72019-11-14 19:39:52 -07004824 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004825 io_cqring_add_event(req, -ECANCELED);
4826 io_double_put_req(req);
4827 } else
Jens Axboe3529d8c2019-12-19 18:24:38 -07004828 io_queue_sqe(req, NULL);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004829}
4830
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004831#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
Jens Axboece35a472019-12-17 08:04:44 -07004832 IOSQE_IO_HARDLINK | IOSQE_ASYNC)
Jens Axboe9e645e112019-05-10 16:07:28 -06004833
Jens Axboe3529d8c2019-12-19 18:24:38 -07004834static bool io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
4835 struct io_submit_state *state, struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06004836{
Jackie Liua197f662019-11-08 08:09:12 -07004837 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004838 unsigned int sqe_flags;
Jens Axboe75c6a032020-01-28 10:15:23 -07004839 int ret, id;
Jens Axboe9e645e112019-05-10 16:07:28 -06004840
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004841 sqe_flags = READ_ONCE(sqe->flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06004842
4843 /* enforce forwards compatibility on users */
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004844 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06004845 ret = -EINVAL;
Pavel Begunkov196be952019-11-07 01:41:06 +03004846 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06004847 }
4848
Jens Axboe75c6a032020-01-28 10:15:23 -07004849 id = READ_ONCE(sqe->personality);
4850 if (id) {
Jens Axboe193155c2020-02-22 23:22:19 -07004851 req->work.creds = idr_find(&ctx->personality_idr, id);
4852 if (unlikely(!req->work.creds)) {
Jens Axboe75c6a032020-01-28 10:15:23 -07004853 ret = -EINVAL;
4854 goto err_req;
4855 }
Jens Axboe193155c2020-02-22 23:22:19 -07004856 get_cred(req->work.creds);
Jens Axboe75c6a032020-01-28 10:15:23 -07004857 }
4858
Pavel Begunkov6b47ee62020-01-18 20:22:41 +03004859 /* same numerical values with corresponding REQ_F_*, safe to copy */
4860 req->flags |= sqe_flags & (IOSQE_IO_DRAIN|IOSQE_IO_HARDLINK|
4861 IOSQE_ASYNC);
Jens Axboe9e645e112019-05-10 16:07:28 -06004862
Jens Axboe3529d8c2019-12-19 18:24:38 -07004863 ret = io_req_set_file(state, req, sqe);
Jens Axboe9e645e112019-05-10 16:07:28 -06004864 if (unlikely(ret)) {
4865err_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004866 io_cqring_add_event(req, ret);
4867 io_double_put_req(req);
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004868 return false;
Jens Axboe9e645e112019-05-10 16:07:28 -06004869 }
4870
Jens Axboe9e645e112019-05-10 16:07:28 -06004871 /*
4872 * If we already have a head request, queue this one for async
4873 * submittal once the head completes. If we don't have a head but
4874 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
4875 * submitted sync once the chain is complete. If none of those
4876 * conditions are true (normal request), then just queue it.
4877 */
4878 if (*link) {
Pavel Begunkov9d763772019-12-17 02:22:07 +03004879 struct io_kiocb *head = *link;
Jens Axboe9e645e112019-05-10 16:07:28 -06004880
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03004881 /*
4882 * Taking sequential execution of a link, draining both sides
4883 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
4884 * requests in the link. So, it drains the head and the
4885 * next after the link request. The last one is done via
4886 * drain_next flag to persist the effect across calls.
4887 */
Pavel Begunkov711be032020-01-17 03:57:59 +03004888 if (sqe_flags & IOSQE_IO_DRAIN) {
4889 head->flags |= REQ_F_IO_DRAIN;
4890 ctx->drain_next = 1;
4891 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004892 if (io_alloc_async_ctx(req)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06004893 ret = -EAGAIN;
4894 goto err_req;
4895 }
4896
Jens Axboe3529d8c2019-12-19 18:24:38 -07004897 ret = io_req_defer_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07004898 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004899 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03004900 head->flags |= REQ_F_FAIL_LINK;
Jens Axboef67676d2019-12-02 11:03:47 -07004901 goto err_req;
Jens Axboe2d283902019-12-04 11:08:05 -07004902 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03004903 trace_io_uring_link(ctx, req, head);
4904 list_add_tail(&req->link_list, &head->link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06004905
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004906 /* last request of a link, enqueue the link */
4907 if (!(sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK))) {
4908 io_queue_link_head(head);
4909 *link = NULL;
4910 }
Jens Axboe9e645e112019-05-10 16:07:28 -06004911 } else {
Pavel Begunkov711be032020-01-17 03:57:59 +03004912 if (unlikely(ctx->drain_next)) {
4913 req->flags |= REQ_F_IO_DRAIN;
4914 req->ctx->drain_next = 0;
4915 }
4916 if (sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK)) {
4917 req->flags |= REQ_F_LINK;
Pavel Begunkov711be032020-01-17 03:57:59 +03004918 INIT_LIST_HEAD(&req->link_list);
4919 ret = io_req_defer_prep(req, sqe);
4920 if (ret)
4921 req->flags |= REQ_F_FAIL_LINK;
4922 *link = req;
4923 } else {
4924 io_queue_sqe(req, sqe);
4925 }
Jens Axboe9e645e112019-05-10 16:07:28 -06004926 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004927
4928 return true;
Jens Axboe9e645e112019-05-10 16:07:28 -06004929}
4930
Jens Axboe9a56a232019-01-09 09:06:50 -07004931/*
4932 * Batched submission is done, ensure local IO is flushed out.
4933 */
4934static void io_submit_state_end(struct io_submit_state *state)
4935{
4936 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06004937 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07004938 if (state->free_reqs)
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03004939 kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs);
Jens Axboe9a56a232019-01-09 09:06:50 -07004940}
4941
4942/*
4943 * Start submission side cache.
4944 */
4945static void io_submit_state_start(struct io_submit_state *state,
Jackie Liu22efde52019-12-02 17:14:52 +08004946 unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07004947{
4948 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07004949 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07004950 state->file = NULL;
4951 state->ios_left = max_ios;
4952}
4953
Jens Axboe2b188cc2019-01-07 10:46:33 -07004954static void io_commit_sqring(struct io_ring_ctx *ctx)
4955{
Hristo Venev75b28af2019-08-26 17:23:46 +00004956 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004957
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03004958 /*
4959 * Ensure any loads from the SQEs are done at this point,
4960 * since once we write the new head, the application could
4961 * write new data to them.
4962 */
4963 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004964}
4965
4966/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07004967 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07004968 * that is mapped by userspace. This means that care needs to be taken to
4969 * ensure that reads are stable, as we cannot rely on userspace always
4970 * being a good citizen. If members of the sqe are validated and then later
4971 * used, it's important that those reads are done through READ_ONCE() to
4972 * prevent a re-load down the line.
4973 */
Jens Axboe3529d8c2019-12-19 18:24:38 -07004974static bool io_get_sqring(struct io_ring_ctx *ctx, struct io_kiocb *req,
4975 const struct io_uring_sqe **sqe_ptr)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004976{
Hristo Venev75b28af2019-08-26 17:23:46 +00004977 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004978 unsigned head;
4979
4980 /*
4981 * The cached sq head (or cq tail) serves two purposes:
4982 *
4983 * 1) allows us to batch the cost of updating the user visible
4984 * head updates.
4985 * 2) allows the kernel side to track the head on its own, even
4986 * though the application is the one updating it.
4987 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03004988 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03004989 if (likely(head < ctx->sq_entries)) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004990 /*
4991 * All io need record the previous position, if LINK vs DARIN,
4992 * it can be used to mark the position of the first IO in the
4993 * link list.
4994 */
4995 req->sequence = ctx->cached_sq_head;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004996 *sqe_ptr = &ctx->sq_sqes[head];
4997 req->opcode = READ_ONCE((*sqe_ptr)->opcode);
4998 req->user_data = READ_ONCE((*sqe_ptr)->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004999 ctx->cached_sq_head++;
5000 return true;
5001 }
5002
5003 /* drop invalid entries */
5004 ctx->cached_sq_head++;
Jens Axboe498ccd92019-10-25 10:04:25 -06005005 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03005006 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005007 return false;
5008}
5009
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005010static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkovae9428c2019-11-06 00:22:14 +03005011 struct file *ring_file, int ring_fd,
5012 struct mm_struct **mm, bool async)
Jens Axboe6c271ce2019-01-10 11:22:30 -07005013{
5014 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06005015 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06005016 int i, submitted = 0;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03005017 bool mm_fault = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005018
Jens Axboec4a2ed72019-11-21 21:01:26 -07005019 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07005020 if (test_bit(0, &ctx->sq_check_overflow)) {
5021 if (!list_empty(&ctx->cq_overflow_list) &&
5022 !io_cqring_overflow_flush(ctx, false))
5023 return -EBUSY;
5024 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07005025
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03005026 /* make sure SQ entry isn't read before tail */
5027 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03005028
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03005029 if (!percpu_ref_tryget_many(&ctx->refs, nr))
5030 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005031
5032 if (nr > IO_PLUG_THRESHOLD) {
Jackie Liu22efde52019-12-02 17:14:52 +08005033 io_submit_state_start(&state, nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005034 statep = &state;
5035 }
5036
Pavel Begunkovb14cca02020-01-17 04:45:59 +03005037 ctx->ring_fd = ring_fd;
5038 ctx->ring_file = ring_file;
5039
Jens Axboe6c271ce2019-01-10 11:22:30 -07005040 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07005041 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03005042 struct io_kiocb *req;
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005043 int err;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005044
Pavel Begunkov196be952019-11-07 01:41:06 +03005045 req = io_get_req(ctx, statep);
5046 if (unlikely(!req)) {
5047 if (!submitted)
5048 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005049 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06005050 }
Jens Axboe3529d8c2019-12-19 18:24:38 -07005051 if (!io_get_sqring(ctx, req, &sqe)) {
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03005052 __io_req_do_free(req);
Pavel Begunkov196be952019-11-07 01:41:06 +03005053 break;
5054 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005055
Jens Axboed3656342019-12-18 09:50:26 -07005056 /* will complete beyond this point, count as submitted */
5057 submitted++;
5058
5059 if (unlikely(req->opcode >= IORING_OP_LAST)) {
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005060 err = -EINVAL;
5061fail_req:
5062 io_cqring_add_event(req, err);
Jens Axboed3656342019-12-18 09:50:26 -07005063 io_double_put_req(req);
5064 break;
5065 }
5066
5067 if (io_op_defs[req->opcode].needs_mm && !*mm) {
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03005068 mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm);
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005069 if (unlikely(mm_fault)) {
5070 err = -EFAULT;
5071 goto fail_req;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03005072 }
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005073 use_mm(ctx->sqo_mm);
5074 *mm = ctx->sqo_mm;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03005075 }
5076
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03005077 req->needs_fixed_file = async;
Jens Axboe354420f2020-01-08 18:55:15 -07005078 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
5079 true, async);
Jens Axboe3529d8c2019-12-19 18:24:38 -07005080 if (!io_submit_sqe(req, sqe, statep, &link))
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03005081 break;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005082 }
5083
Pavel Begunkov9466f432020-01-25 22:34:01 +03005084 if (unlikely(submitted != nr)) {
5085 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
5086
5087 percpu_ref_put_many(&ctx->refs, nr - ref_used);
5088 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005089 if (link)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03005090 io_queue_link_head(link);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005091 if (statep)
5092 io_submit_state_end(&state);
5093
Pavel Begunkovae9428c2019-11-06 00:22:14 +03005094 /* Commit SQ ring head once we've consumed and submitted all SQEs */
5095 io_commit_sqring(ctx);
5096
Jens Axboe6c271ce2019-01-10 11:22:30 -07005097 return submitted;
5098}
5099
5100static int io_sq_thread(void *data)
5101{
Jens Axboe6c271ce2019-01-10 11:22:30 -07005102 struct io_ring_ctx *ctx = data;
5103 struct mm_struct *cur_mm = NULL;
Jens Axboe181e4482019-11-25 08:52:30 -07005104 const struct cred *old_cred;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005105 mm_segment_t old_fs;
5106 DEFINE_WAIT(wait);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005107 unsigned long timeout;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005108 int ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005109
Jens Axboe206aefd2019-11-07 18:27:42 -07005110 complete(&ctx->completions[1]);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08005111
Jens Axboe6c271ce2019-01-10 11:22:30 -07005112 old_fs = get_fs();
5113 set_fs(USER_DS);
Jens Axboe181e4482019-11-25 08:52:30 -07005114 old_cred = override_creds(ctx->creds);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005115
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005116 timeout = jiffies + ctx->sq_thread_idle;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005117 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005118 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005119
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005120 if (!list_empty(&ctx->poll_list)) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07005121 unsigned nr_events = 0;
5122
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005123 mutex_lock(&ctx->uring_lock);
5124 if (!list_empty(&ctx->poll_list))
5125 io_iopoll_getevents(ctx, &nr_events, 0);
5126 else
Jens Axboe6c271ce2019-01-10 11:22:30 -07005127 timeout = jiffies + ctx->sq_thread_idle;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005128 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005129 }
5130
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005131 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07005132
5133 /*
5134 * If submit got -EBUSY, flag us as needing the application
5135 * to enter the kernel to reap and flush events.
5136 */
5137 if (!to_submit || ret == -EBUSY) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07005138 /*
Stefano Garzarella7143b5a2020-02-21 16:42:16 +01005139 * Drop cur_mm before scheduling, we can't hold it for
5140 * long periods (or over schedule()). Do this before
5141 * adding ourselves to the waitqueue, as the unuse/drop
5142 * may sleep.
5143 */
5144 if (cur_mm) {
5145 unuse_mm(cur_mm);
5146 mmput(cur_mm);
5147 cur_mm = NULL;
5148 }
5149
5150 /*
Jens Axboe6c271ce2019-01-10 11:22:30 -07005151 * We're polling. If we're within the defined idle
5152 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07005153 * to sleep. The exception is if we got EBUSY doing
5154 * more IO, we should wait for the application to
5155 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07005156 */
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005157 if (!list_empty(&ctx->poll_list) ||
Jens Axboedf069d82020-02-04 16:48:34 -07005158 (!time_after(jiffies, timeout) && ret != -EBUSY &&
5159 !percpu_ref_is_dying(&ctx->refs))) {
Jens Axboe9831a902019-09-19 09:48:55 -06005160 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07005161 continue;
5162 }
5163
Jens Axboe6c271ce2019-01-10 11:22:30 -07005164 prepare_to_wait(&ctx->sqo_wait, &wait,
5165 TASK_INTERRUPTIBLE);
5166
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005167 /*
5168 * While doing polled IO, before going to sleep, we need
5169 * to check if there are new reqs added to poll_list, it
5170 * is because reqs may have been punted to io worker and
5171 * will be added to poll_list later, hence check the
5172 * poll_list again.
5173 */
5174 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
5175 !list_empty_careful(&ctx->poll_list)) {
5176 finish_wait(&ctx->sqo_wait, &wait);
5177 continue;
5178 }
5179
Jens Axboe6c271ce2019-01-10 11:22:30 -07005180 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00005181 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02005182 /* make sure to read SQ tail after writing flags */
5183 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07005184
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005185 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07005186 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005187 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07005188 finish_wait(&ctx->sqo_wait, &wait);
5189 break;
5190 }
5191 if (signal_pending(current))
5192 flush_signals(current);
5193 schedule();
5194 finish_wait(&ctx->sqo_wait, &wait);
5195
Hristo Venev75b28af2019-08-26 17:23:46 +00005196 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005197 continue;
5198 }
5199 finish_wait(&ctx->sqo_wait, &wait);
5200
Hristo Venev75b28af2019-08-26 17:23:46 +00005201 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005202 }
5203
Jens Axboe8a4955f2019-12-09 14:52:35 -07005204 mutex_lock(&ctx->uring_lock);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005205 ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true);
Jens Axboe8a4955f2019-12-09 14:52:35 -07005206 mutex_unlock(&ctx->uring_lock);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005207 timeout = jiffies + ctx->sq_thread_idle;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005208 }
5209
5210 set_fs(old_fs);
5211 if (cur_mm) {
5212 unuse_mm(cur_mm);
5213 mmput(cur_mm);
5214 }
Jens Axboe181e4482019-11-25 08:52:30 -07005215 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06005216
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005217 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06005218
Jens Axboe6c271ce2019-01-10 11:22:30 -07005219 return 0;
5220}
5221
Jens Axboebda52162019-09-24 13:47:15 -06005222struct io_wait_queue {
5223 struct wait_queue_entry wq;
5224 struct io_ring_ctx *ctx;
5225 unsigned to_wait;
5226 unsigned nr_timeouts;
5227};
5228
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005229static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06005230{
5231 struct io_ring_ctx *ctx = iowq->ctx;
5232
5233 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08005234 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06005235 * started waiting. For timeouts, we always want to return to userspace,
5236 * regardless of event count.
5237 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005238 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06005239 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
5240}
5241
5242static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
5243 int wake_flags, void *key)
5244{
5245 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
5246 wq);
5247
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005248 /* use noflush == true, as we can't safely rely on locking context */
5249 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06005250 return -1;
5251
5252 return autoremove_wake_function(curr, mode, wake_flags, key);
5253}
5254
Jens Axboe2b188cc2019-01-07 10:46:33 -07005255/*
5256 * Wait until events become available, if we don't already have some. The
5257 * application must reap them itself, as they reside on the shared cq ring.
5258 */
5259static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
5260 const sigset_t __user *sig, size_t sigsz)
5261{
Jens Axboebda52162019-09-24 13:47:15 -06005262 struct io_wait_queue iowq = {
5263 .wq = {
5264 .private = current,
5265 .func = io_wake_function,
5266 .entry = LIST_HEAD_INIT(iowq.wq.entry),
5267 },
5268 .ctx = ctx,
5269 .to_wait = min_events,
5270 };
Hristo Venev75b28af2019-08-26 17:23:46 +00005271 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005272 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005273
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005274 if (io_cqring_events(ctx, false) >= min_events)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005275 return 0;
5276
5277 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005278#ifdef CONFIG_COMPAT
5279 if (in_compat_syscall())
5280 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07005281 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005282 else
5283#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07005284 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005285
Jens Axboe2b188cc2019-01-07 10:46:33 -07005286 if (ret)
5287 return ret;
5288 }
5289
Jens Axboebda52162019-09-24 13:47:15 -06005290 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02005291 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06005292 do {
5293 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
5294 TASK_INTERRUPTIBLE);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005295 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06005296 break;
5297 schedule();
5298 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005299 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06005300 break;
5301 }
5302 } while (1);
5303 finish_wait(&ctx->wait, &iowq.wq);
5304
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005305 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005306
Hristo Venev75b28af2019-08-26 17:23:46 +00005307 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005308}
5309
Jens Axboe6b063142019-01-10 22:13:58 -07005310static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
5311{
5312#if defined(CONFIG_UNIX)
5313 if (ctx->ring_sock) {
5314 struct sock *sock = ctx->ring_sock->sk;
5315 struct sk_buff *skb;
5316
5317 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
5318 kfree_skb(skb);
5319 }
5320#else
5321 int i;
5322
Jens Axboe65e19f52019-10-26 07:20:21 -06005323 for (i = 0; i < ctx->nr_user_files; i++) {
5324 struct file *file;
5325
5326 file = io_file_from_index(ctx, i);
5327 if (file)
5328 fput(file);
5329 }
Jens Axboe6b063142019-01-10 22:13:58 -07005330#endif
5331}
5332
Jens Axboe05f3fb32019-12-09 11:22:50 -07005333static void io_file_ref_kill(struct percpu_ref *ref)
5334{
5335 struct fixed_file_data *data;
5336
5337 data = container_of(ref, struct fixed_file_data, refs);
5338 complete(&data->done);
5339}
5340
Jens Axboe6b063142019-01-10 22:13:58 -07005341static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
5342{
Jens Axboe05f3fb32019-12-09 11:22:50 -07005343 struct fixed_file_data *data = ctx->file_data;
Jens Axboe65e19f52019-10-26 07:20:21 -06005344 unsigned nr_tables, i;
5345
Jens Axboe05f3fb32019-12-09 11:22:50 -07005346 if (!data)
Jens Axboe6b063142019-01-10 22:13:58 -07005347 return -ENXIO;
5348
Jens Axboe05f3fb32019-12-09 11:22:50 -07005349 percpu_ref_kill_and_confirm(&data->refs, io_file_ref_kill);
Jens Axboee46a7952020-01-17 11:15:34 -07005350 flush_work(&data->ref_work);
Jens Axboe2faf8522020-02-04 19:54:55 -07005351 wait_for_completion(&data->done);
5352 io_ring_file_ref_flush(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005353 percpu_ref_exit(&data->refs);
5354
Jens Axboe6b063142019-01-10 22:13:58 -07005355 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06005356 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
5357 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005358 kfree(data->table[i].files);
5359 kfree(data->table);
5360 kfree(data);
5361 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07005362 ctx->nr_user_files = 0;
5363 return 0;
5364}
5365
Jens Axboe6c271ce2019-01-10 11:22:30 -07005366static void io_sq_thread_stop(struct io_ring_ctx *ctx)
5367{
5368 if (ctx->sqo_thread) {
Jens Axboe206aefd2019-11-07 18:27:42 -07005369 wait_for_completion(&ctx->completions[1]);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005370 /*
5371 * The park is a bit of a work-around, without it we get
5372 * warning spews on shutdown with SQPOLL set and affinity
5373 * set to a single CPU.
5374 */
Jens Axboe06058632019-04-13 09:26:03 -06005375 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005376 kthread_stop(ctx->sqo_thread);
5377 ctx->sqo_thread = NULL;
5378 }
5379}
5380
Jens Axboe6b063142019-01-10 22:13:58 -07005381static void io_finish_async(struct io_ring_ctx *ctx)
5382{
Jens Axboe6c271ce2019-01-10 11:22:30 -07005383 io_sq_thread_stop(ctx);
5384
Jens Axboe561fb042019-10-24 07:25:42 -06005385 if (ctx->io_wq) {
5386 io_wq_destroy(ctx->io_wq);
5387 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07005388 }
5389}
5390
5391#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07005392/*
5393 * Ensure the UNIX gc is aware of our file set, so we are certain that
5394 * the io_uring can be safely unregistered on process exit, even if we have
5395 * loops in the file referencing.
5396 */
5397static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
5398{
5399 struct sock *sk = ctx->ring_sock->sk;
5400 struct scm_fp_list *fpl;
5401 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06005402 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07005403
5404 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
5405 unsigned long inflight = ctx->user->unix_inflight + nr;
5406
5407 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
5408 return -EMFILE;
5409 }
5410
5411 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
5412 if (!fpl)
5413 return -ENOMEM;
5414
5415 skb = alloc_skb(0, GFP_KERNEL);
5416 if (!skb) {
5417 kfree(fpl);
5418 return -ENOMEM;
5419 }
5420
5421 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07005422
Jens Axboe08a45172019-10-03 08:11:03 -06005423 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07005424 fpl->user = get_uid(ctx->user);
5425 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005426 struct file *file = io_file_from_index(ctx, i + offset);
5427
5428 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06005429 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06005430 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06005431 unix_inflight(fpl->user, fpl->fp[nr_files]);
5432 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07005433 }
5434
Jens Axboe08a45172019-10-03 08:11:03 -06005435 if (nr_files) {
5436 fpl->max = SCM_MAX_FD;
5437 fpl->count = nr_files;
5438 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005439 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06005440 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
5441 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07005442
Jens Axboe08a45172019-10-03 08:11:03 -06005443 for (i = 0; i < nr_files; i++)
5444 fput(fpl->fp[i]);
5445 } else {
5446 kfree_skb(skb);
5447 kfree(fpl);
5448 }
Jens Axboe6b063142019-01-10 22:13:58 -07005449
5450 return 0;
5451}
5452
5453/*
5454 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
5455 * causes regular reference counting to break down. We rely on the UNIX
5456 * garbage collection to take care of this problem for us.
5457 */
5458static int io_sqe_files_scm(struct io_ring_ctx *ctx)
5459{
5460 unsigned left, total;
5461 int ret = 0;
5462
5463 total = 0;
5464 left = ctx->nr_user_files;
5465 while (left) {
5466 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07005467
5468 ret = __io_sqe_files_scm(ctx, this_files, total);
5469 if (ret)
5470 break;
5471 left -= this_files;
5472 total += this_files;
5473 }
5474
5475 if (!ret)
5476 return 0;
5477
5478 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005479 struct file *file = io_file_from_index(ctx, total);
5480
5481 if (file)
5482 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07005483 total++;
5484 }
5485
5486 return ret;
5487}
5488#else
5489static int io_sqe_files_scm(struct io_ring_ctx *ctx)
5490{
5491 return 0;
5492}
5493#endif
5494
Jens Axboe65e19f52019-10-26 07:20:21 -06005495static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
5496 unsigned nr_files)
5497{
5498 int i;
5499
5500 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005501 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06005502 unsigned this_files;
5503
5504 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
5505 table->files = kcalloc(this_files, sizeof(struct file *),
5506 GFP_KERNEL);
5507 if (!table->files)
5508 break;
5509 nr_files -= this_files;
5510 }
5511
5512 if (i == nr_tables)
5513 return 0;
5514
5515 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005516 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06005517 kfree(table->files);
5518 }
5519 return 1;
5520}
5521
Jens Axboe05f3fb32019-12-09 11:22:50 -07005522static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
Jens Axboec3a31e62019-10-03 13:59:56 -06005523{
5524#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06005525 struct sock *sock = ctx->ring_sock->sk;
5526 struct sk_buff_head list, *head = &sock->sk_receive_queue;
5527 struct sk_buff *skb;
5528 int i;
5529
5530 __skb_queue_head_init(&list);
5531
5532 /*
5533 * Find the skb that holds this file in its SCM_RIGHTS. When found,
5534 * remove this entry and rearrange the file array.
5535 */
5536 skb = skb_dequeue(head);
5537 while (skb) {
5538 struct scm_fp_list *fp;
5539
5540 fp = UNIXCB(skb).fp;
5541 for (i = 0; i < fp->count; i++) {
5542 int left;
5543
5544 if (fp->fp[i] != file)
5545 continue;
5546
5547 unix_notinflight(fp->user, fp->fp[i]);
5548 left = fp->count - 1 - i;
5549 if (left) {
5550 memmove(&fp->fp[i], &fp->fp[i + 1],
5551 left * sizeof(struct file *));
5552 }
5553 fp->count--;
5554 if (!fp->count) {
5555 kfree_skb(skb);
5556 skb = NULL;
5557 } else {
5558 __skb_queue_tail(&list, skb);
5559 }
5560 fput(file);
5561 file = NULL;
5562 break;
5563 }
5564
5565 if (!file)
5566 break;
5567
5568 __skb_queue_tail(&list, skb);
5569
5570 skb = skb_dequeue(head);
5571 }
5572
5573 if (skb_peek(&list)) {
5574 spin_lock_irq(&head->lock);
5575 while ((skb = __skb_dequeue(&list)) != NULL)
5576 __skb_queue_tail(head, skb);
5577 spin_unlock_irq(&head->lock);
5578 }
5579#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07005580 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06005581#endif
5582}
5583
Jens Axboe05f3fb32019-12-09 11:22:50 -07005584struct io_file_put {
5585 struct llist_node llist;
5586 struct file *file;
5587 struct completion *done;
5588};
5589
Jens Axboe2faf8522020-02-04 19:54:55 -07005590static void io_ring_file_ref_flush(struct fixed_file_data *data)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005591{
5592 struct io_file_put *pfile, *tmp;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005593 struct llist_node *node;
5594
Jens Axboe05f3fb32019-12-09 11:22:50 -07005595 while ((node = llist_del_all(&data->put_llist)) != NULL) {
5596 llist_for_each_entry_safe(pfile, tmp, node, llist) {
5597 io_ring_file_put(data->ctx, pfile->file);
5598 if (pfile->done)
5599 complete(pfile->done);
5600 else
5601 kfree(pfile);
5602 }
5603 }
Jens Axboe2faf8522020-02-04 19:54:55 -07005604}
Jens Axboe05f3fb32019-12-09 11:22:50 -07005605
Jens Axboe2faf8522020-02-04 19:54:55 -07005606static void io_ring_file_ref_switch(struct work_struct *work)
5607{
5608 struct fixed_file_data *data;
5609
5610 data = container_of(work, struct fixed_file_data, ref_work);
5611 io_ring_file_ref_flush(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005612 percpu_ref_switch_to_percpu(&data->refs);
5613}
5614
5615static void io_file_data_ref_zero(struct percpu_ref *ref)
5616{
5617 struct fixed_file_data *data;
5618
5619 data = container_of(ref, struct fixed_file_data, refs);
5620
Jens Axboe2faf8522020-02-04 19:54:55 -07005621 /*
5622 * We can't safely switch from inside this context, punt to wq. If
5623 * the table ref is going away, the table is being unregistered.
5624 * Don't queue up the async work for that case, the caller will
5625 * handle it.
5626 */
5627 if (!percpu_ref_is_dying(&data->refs))
5628 queue_work(system_wq, &data->ref_work);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005629}
5630
5631static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
5632 unsigned nr_args)
5633{
5634 __s32 __user *fds = (__s32 __user *) arg;
5635 unsigned nr_tables;
5636 struct file *file;
5637 int fd, ret = 0;
5638 unsigned i;
5639
5640 if (ctx->file_data)
5641 return -EBUSY;
5642 if (!nr_args)
5643 return -EINVAL;
5644 if (nr_args > IORING_MAX_FIXED_FILES)
5645 return -EMFILE;
5646
5647 ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
5648 if (!ctx->file_data)
5649 return -ENOMEM;
5650 ctx->file_data->ctx = ctx;
5651 init_completion(&ctx->file_data->done);
5652
5653 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
5654 ctx->file_data->table = kcalloc(nr_tables,
5655 sizeof(struct fixed_file_table),
5656 GFP_KERNEL);
5657 if (!ctx->file_data->table) {
5658 kfree(ctx->file_data);
5659 ctx->file_data = NULL;
5660 return -ENOMEM;
5661 }
5662
5663 if (percpu_ref_init(&ctx->file_data->refs, io_file_data_ref_zero,
5664 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
5665 kfree(ctx->file_data->table);
5666 kfree(ctx->file_data);
5667 ctx->file_data = NULL;
5668 return -ENOMEM;
5669 }
5670 ctx->file_data->put_llist.first = NULL;
5671 INIT_WORK(&ctx->file_data->ref_work, io_ring_file_ref_switch);
5672
5673 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
5674 percpu_ref_exit(&ctx->file_data->refs);
5675 kfree(ctx->file_data->table);
5676 kfree(ctx->file_data);
5677 ctx->file_data = NULL;
5678 return -ENOMEM;
5679 }
5680
5681 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
5682 struct fixed_file_table *table;
5683 unsigned index;
5684
5685 ret = -EFAULT;
5686 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
5687 break;
5688 /* allow sparse sets */
5689 if (fd == -1) {
5690 ret = 0;
5691 continue;
5692 }
5693
5694 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
5695 index = i & IORING_FILE_TABLE_MASK;
5696 file = fget(fd);
5697
5698 ret = -EBADF;
5699 if (!file)
5700 break;
5701
5702 /*
5703 * Don't allow io_uring instances to be registered. If UNIX
5704 * isn't enabled, then this causes a reference cycle and this
5705 * instance can never get freed. If UNIX is enabled we'll
5706 * handle it just fine, but there's still no point in allowing
5707 * a ring fd as it doesn't support regular read/write anyway.
5708 */
5709 if (file->f_op == &io_uring_fops) {
5710 fput(file);
5711 break;
5712 }
5713 ret = 0;
5714 table->files[index] = file;
5715 }
5716
5717 if (ret) {
5718 for (i = 0; i < ctx->nr_user_files; i++) {
5719 file = io_file_from_index(ctx, i);
5720 if (file)
5721 fput(file);
5722 }
5723 for (i = 0; i < nr_tables; i++)
5724 kfree(ctx->file_data->table[i].files);
5725
5726 kfree(ctx->file_data->table);
5727 kfree(ctx->file_data);
5728 ctx->file_data = NULL;
5729 ctx->nr_user_files = 0;
5730 return ret;
5731 }
5732
5733 ret = io_sqe_files_scm(ctx);
5734 if (ret)
5735 io_sqe_files_unregister(ctx);
5736
5737 return ret;
5738}
5739
Jens Axboec3a31e62019-10-03 13:59:56 -06005740static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
5741 int index)
5742{
5743#if defined(CONFIG_UNIX)
5744 struct sock *sock = ctx->ring_sock->sk;
5745 struct sk_buff_head *head = &sock->sk_receive_queue;
5746 struct sk_buff *skb;
5747
5748 /*
5749 * See if we can merge this file into an existing skb SCM_RIGHTS
5750 * file set. If there's no room, fall back to allocating a new skb
5751 * and filling it in.
5752 */
5753 spin_lock_irq(&head->lock);
5754 skb = skb_peek(head);
5755 if (skb) {
5756 struct scm_fp_list *fpl = UNIXCB(skb).fp;
5757
5758 if (fpl->count < SCM_MAX_FD) {
5759 __skb_unlink(skb, head);
5760 spin_unlock_irq(&head->lock);
5761 fpl->fp[fpl->count] = get_file(file);
5762 unix_inflight(fpl->user, fpl->fp[fpl->count]);
5763 fpl->count++;
5764 spin_lock_irq(&head->lock);
5765 __skb_queue_head(head, skb);
5766 } else {
5767 skb = NULL;
5768 }
5769 }
5770 spin_unlock_irq(&head->lock);
5771
5772 if (skb) {
5773 fput(file);
5774 return 0;
5775 }
5776
5777 return __io_sqe_files_scm(ctx, 1, index);
5778#else
5779 return 0;
5780#endif
5781}
5782
Jens Axboe05f3fb32019-12-09 11:22:50 -07005783static void io_atomic_switch(struct percpu_ref *ref)
Jens Axboec3a31e62019-10-03 13:59:56 -06005784{
Jens Axboe05f3fb32019-12-09 11:22:50 -07005785 struct fixed_file_data *data;
5786
Jens Axboedd3db2a2020-02-26 10:23:43 -07005787 /*
5788 * Juggle reference to ensure we hit zero, if needed, so we can
5789 * switch back to percpu mode
5790 */
Jens Axboe05f3fb32019-12-09 11:22:50 -07005791 data = container_of(ref, struct fixed_file_data, refs);
Jens Axboedd3db2a2020-02-26 10:23:43 -07005792 percpu_ref_put(&data->refs);
5793 percpu_ref_get(&data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005794}
5795
5796static bool io_queue_file_removal(struct fixed_file_data *data,
5797 struct file *file)
5798{
5799 struct io_file_put *pfile, pfile_stack;
5800 DECLARE_COMPLETION_ONSTACK(done);
5801
5802 /*
5803 * If we fail allocating the struct we need for doing async reomval
5804 * of this file, just punt to sync and wait for it.
5805 */
5806 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
5807 if (!pfile) {
5808 pfile = &pfile_stack;
5809 pfile->done = &done;
5810 }
5811
5812 pfile->file = file;
5813 llist_add(&pfile->llist, &data->put_llist);
5814
5815 if (pfile == &pfile_stack) {
Jens Axboedd3db2a2020-02-26 10:23:43 -07005816 percpu_ref_switch_to_atomic(&data->refs, io_atomic_switch);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005817 wait_for_completion(&done);
5818 flush_work(&data->ref_work);
5819 return false;
5820 }
5821
5822 return true;
5823}
5824
5825static int __io_sqe_files_update(struct io_ring_ctx *ctx,
5826 struct io_uring_files_update *up,
5827 unsigned nr_args)
5828{
5829 struct fixed_file_data *data = ctx->file_data;
5830 bool ref_switch = false;
5831 struct file *file;
Jens Axboec3a31e62019-10-03 13:59:56 -06005832 __s32 __user *fds;
5833 int fd, i, err;
5834 __u32 done;
5835
Jens Axboe05f3fb32019-12-09 11:22:50 -07005836 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06005837 return -EOVERFLOW;
5838 if (done > ctx->nr_user_files)
5839 return -EINVAL;
5840
5841 done = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005842 fds = u64_to_user_ptr(up->fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06005843 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005844 struct fixed_file_table *table;
5845 unsigned index;
5846
Jens Axboec3a31e62019-10-03 13:59:56 -06005847 err = 0;
5848 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
5849 err = -EFAULT;
5850 break;
5851 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07005852 i = array_index_nospec(up->offset, ctx->nr_user_files);
5853 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
Jens Axboe65e19f52019-10-26 07:20:21 -06005854 index = i & IORING_FILE_TABLE_MASK;
5855 if (table->files[index]) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005856 file = io_file_from_index(ctx, index);
Jens Axboe65e19f52019-10-26 07:20:21 -06005857 table->files[index] = NULL;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005858 if (io_queue_file_removal(data, file))
5859 ref_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06005860 }
5861 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06005862 file = fget(fd);
5863 if (!file) {
5864 err = -EBADF;
5865 break;
5866 }
5867 /*
5868 * Don't allow io_uring instances to be registered. If
5869 * UNIX isn't enabled, then this causes a reference
5870 * cycle and this instance can never get freed. If UNIX
5871 * is enabled we'll handle it just fine, but there's
5872 * still no point in allowing a ring fd as it doesn't
5873 * support regular read/write anyway.
5874 */
5875 if (file->f_op == &io_uring_fops) {
5876 fput(file);
5877 err = -EBADF;
5878 break;
5879 }
Jens Axboe65e19f52019-10-26 07:20:21 -06005880 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06005881 err = io_sqe_file_register(ctx, file, i);
5882 if (err)
5883 break;
5884 }
5885 nr_args--;
5886 done++;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005887 up->offset++;
5888 }
5889
Jens Axboedd3db2a2020-02-26 10:23:43 -07005890 if (ref_switch)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005891 percpu_ref_switch_to_atomic(&data->refs, io_atomic_switch);
Jens Axboec3a31e62019-10-03 13:59:56 -06005892
5893 return done ? done : err;
5894}
Jens Axboe05f3fb32019-12-09 11:22:50 -07005895static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
5896 unsigned nr_args)
5897{
5898 struct io_uring_files_update up;
5899
5900 if (!ctx->file_data)
5901 return -ENXIO;
5902 if (!nr_args)
5903 return -EINVAL;
5904 if (copy_from_user(&up, arg, sizeof(up)))
5905 return -EFAULT;
5906 if (up.resv)
5907 return -EINVAL;
5908
5909 return __io_sqe_files_update(ctx, &up, nr_args);
5910}
Jens Axboec3a31e62019-10-03 13:59:56 -06005911
Jens Axboe7d723062019-11-12 22:31:31 -07005912static void io_put_work(struct io_wq_work *work)
5913{
5914 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
5915
5916 io_put_req(req);
5917}
5918
5919static void io_get_work(struct io_wq_work *work)
5920{
5921 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
5922
5923 refcount_inc(&req->refs);
5924}
5925
Pavel Begunkov24369c22020-01-28 03:15:48 +03005926static int io_init_wq_offload(struct io_ring_ctx *ctx,
5927 struct io_uring_params *p)
5928{
5929 struct io_wq_data data;
5930 struct fd f;
5931 struct io_ring_ctx *ctx_attach;
5932 unsigned int concurrency;
5933 int ret = 0;
5934
5935 data.user = ctx->user;
5936 data.get_work = io_get_work;
5937 data.put_work = io_put_work;
5938
5939 if (!(p->flags & IORING_SETUP_ATTACH_WQ)) {
5940 /* Do QD, or 4 * CPUS, whatever is smallest */
5941 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
5942
5943 ctx->io_wq = io_wq_create(concurrency, &data);
5944 if (IS_ERR(ctx->io_wq)) {
5945 ret = PTR_ERR(ctx->io_wq);
5946 ctx->io_wq = NULL;
5947 }
5948 return ret;
5949 }
5950
5951 f = fdget(p->wq_fd);
5952 if (!f.file)
5953 return -EBADF;
5954
5955 if (f.file->f_op != &io_uring_fops) {
5956 ret = -EINVAL;
5957 goto out_fput;
5958 }
5959
5960 ctx_attach = f.file->private_data;
5961 /* @io_wq is protected by holding the fd */
5962 if (!io_wq_get(ctx_attach->io_wq, &data)) {
5963 ret = -EINVAL;
5964 goto out_fput;
5965 }
5966
5967 ctx->io_wq = ctx_attach->io_wq;
5968out_fput:
5969 fdput(f);
5970 return ret;
5971}
5972
Jens Axboe6c271ce2019-01-10 11:22:30 -07005973static int io_sq_offload_start(struct io_ring_ctx *ctx,
5974 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005975{
5976 int ret;
5977
Jens Axboe6c271ce2019-01-10 11:22:30 -07005978 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005979 mmgrab(current->mm);
5980 ctx->sqo_mm = current->mm;
5981
Jens Axboe6c271ce2019-01-10 11:22:30 -07005982 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06005983 ret = -EPERM;
5984 if (!capable(CAP_SYS_ADMIN))
5985 goto err;
5986
Jens Axboe917257d2019-04-13 09:28:55 -06005987 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
5988 if (!ctx->sq_thread_idle)
5989 ctx->sq_thread_idle = HZ;
5990
Jens Axboe6c271ce2019-01-10 11:22:30 -07005991 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06005992 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005993
Jens Axboe917257d2019-04-13 09:28:55 -06005994 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06005995 if (cpu >= nr_cpu_ids)
5996 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08005997 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06005998 goto err;
5999
Jens Axboe6c271ce2019-01-10 11:22:30 -07006000 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
6001 ctx, cpu,
6002 "io_uring-sq");
6003 } else {
6004 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
6005 "io_uring-sq");
6006 }
6007 if (IS_ERR(ctx->sqo_thread)) {
6008 ret = PTR_ERR(ctx->sqo_thread);
6009 ctx->sqo_thread = NULL;
6010 goto err;
6011 }
6012 wake_up_process(ctx->sqo_thread);
6013 } else if (p->flags & IORING_SETUP_SQ_AFF) {
6014 /* Can't have SQ_AFF without SQPOLL */
6015 ret = -EINVAL;
6016 goto err;
6017 }
6018
Pavel Begunkov24369c22020-01-28 03:15:48 +03006019 ret = io_init_wq_offload(ctx, p);
6020 if (ret)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006021 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006022
6023 return 0;
6024err:
Jens Axboe54a91f32019-09-10 09:15:04 -06006025 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006026 mmdrop(ctx->sqo_mm);
6027 ctx->sqo_mm = NULL;
6028 return ret;
6029}
6030
6031static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
6032{
6033 atomic_long_sub(nr_pages, &user->locked_vm);
6034}
6035
6036static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
6037{
6038 unsigned long page_limit, cur_pages, new_pages;
6039
6040 /* Don't allow more pages than we can safely lock */
6041 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
6042
6043 do {
6044 cur_pages = atomic_long_read(&user->locked_vm);
6045 new_pages = cur_pages + nr_pages;
6046 if (new_pages > page_limit)
6047 return -ENOMEM;
6048 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
6049 new_pages) != cur_pages);
6050
6051 return 0;
6052}
6053
6054static void io_mem_free(void *ptr)
6055{
Mark Rutland52e04ef2019-04-30 17:30:21 +01006056 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006057
Mark Rutland52e04ef2019-04-30 17:30:21 +01006058 if (!ptr)
6059 return;
6060
6061 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006062 if (put_page_testzero(page))
6063 free_compound_page(page);
6064}
6065
6066static void *io_mem_alloc(size_t size)
6067{
6068 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
6069 __GFP_NORETRY;
6070
6071 return (void *) __get_free_pages(gfp_flags, get_order(size));
6072}
6073
Hristo Venev75b28af2019-08-26 17:23:46 +00006074static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
6075 size_t *sq_offset)
6076{
6077 struct io_rings *rings;
6078 size_t off, sq_array_size;
6079
6080 off = struct_size(rings, cqes, cq_entries);
6081 if (off == SIZE_MAX)
6082 return SIZE_MAX;
6083
6084#ifdef CONFIG_SMP
6085 off = ALIGN(off, SMP_CACHE_BYTES);
6086 if (off == 0)
6087 return SIZE_MAX;
6088#endif
6089
6090 sq_array_size = array_size(sizeof(u32), sq_entries);
6091 if (sq_array_size == SIZE_MAX)
6092 return SIZE_MAX;
6093
6094 if (check_add_overflow(off, sq_array_size, &off))
6095 return SIZE_MAX;
6096
6097 if (sq_offset)
6098 *sq_offset = off;
6099
6100 return off;
6101}
6102
Jens Axboe2b188cc2019-01-07 10:46:33 -07006103static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
6104{
Hristo Venev75b28af2019-08-26 17:23:46 +00006105 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006106
Hristo Venev75b28af2019-08-26 17:23:46 +00006107 pages = (size_t)1 << get_order(
6108 rings_size(sq_entries, cq_entries, NULL));
6109 pages += (size_t)1 << get_order(
6110 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07006111
Hristo Venev75b28af2019-08-26 17:23:46 +00006112 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006113}
6114
Jens Axboeedafcce2019-01-09 09:16:05 -07006115static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
6116{
6117 int i, j;
6118
6119 if (!ctx->user_bufs)
6120 return -ENXIO;
6121
6122 for (i = 0; i < ctx->nr_user_bufs; i++) {
6123 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
6124
6125 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08006126 unpin_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07006127
6128 if (ctx->account_mem)
6129 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006130 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07006131 imu->nr_bvecs = 0;
6132 }
6133
6134 kfree(ctx->user_bufs);
6135 ctx->user_bufs = NULL;
6136 ctx->nr_user_bufs = 0;
6137 return 0;
6138}
6139
6140static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
6141 void __user *arg, unsigned index)
6142{
6143 struct iovec __user *src;
6144
6145#ifdef CONFIG_COMPAT
6146 if (ctx->compat) {
6147 struct compat_iovec __user *ciovs;
6148 struct compat_iovec ciov;
6149
6150 ciovs = (struct compat_iovec __user *) arg;
6151 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
6152 return -EFAULT;
6153
Jens Axboed55e5f52019-12-11 16:12:15 -07006154 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07006155 dst->iov_len = ciov.iov_len;
6156 return 0;
6157 }
6158#endif
6159 src = (struct iovec __user *) arg;
6160 if (copy_from_user(dst, &src[index], sizeof(*dst)))
6161 return -EFAULT;
6162 return 0;
6163}
6164
6165static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
6166 unsigned nr_args)
6167{
6168 struct vm_area_struct **vmas = NULL;
6169 struct page **pages = NULL;
6170 int i, j, got_pages = 0;
6171 int ret = -EINVAL;
6172
6173 if (ctx->user_bufs)
6174 return -EBUSY;
6175 if (!nr_args || nr_args > UIO_MAXIOV)
6176 return -EINVAL;
6177
6178 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
6179 GFP_KERNEL);
6180 if (!ctx->user_bufs)
6181 return -ENOMEM;
6182
6183 for (i = 0; i < nr_args; i++) {
6184 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
6185 unsigned long off, start, end, ubuf;
6186 int pret, nr_pages;
6187 struct iovec iov;
6188 size_t size;
6189
6190 ret = io_copy_iov(ctx, &iov, arg, i);
6191 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03006192 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07006193
6194 /*
6195 * Don't impose further limits on the size and buffer
6196 * constraints here, we'll -EINVAL later when IO is
6197 * submitted if they are wrong.
6198 */
6199 ret = -EFAULT;
6200 if (!iov.iov_base || !iov.iov_len)
6201 goto err;
6202
6203 /* arbitrary limit, but we need something */
6204 if (iov.iov_len > SZ_1G)
6205 goto err;
6206
6207 ubuf = (unsigned long) iov.iov_base;
6208 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
6209 start = ubuf >> PAGE_SHIFT;
6210 nr_pages = end - start;
6211
6212 if (ctx->account_mem) {
6213 ret = io_account_mem(ctx->user, nr_pages);
6214 if (ret)
6215 goto err;
6216 }
6217
6218 ret = 0;
6219 if (!pages || nr_pages > got_pages) {
6220 kfree(vmas);
6221 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006222 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07006223 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006224 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07006225 sizeof(struct vm_area_struct *),
6226 GFP_KERNEL);
6227 if (!pages || !vmas) {
6228 ret = -ENOMEM;
6229 if (ctx->account_mem)
6230 io_unaccount_mem(ctx->user, nr_pages);
6231 goto err;
6232 }
6233 got_pages = nr_pages;
6234 }
6235
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006236 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07006237 GFP_KERNEL);
6238 ret = -ENOMEM;
6239 if (!imu->bvec) {
6240 if (ctx->account_mem)
6241 io_unaccount_mem(ctx->user, nr_pages);
6242 goto err;
6243 }
6244
6245 ret = 0;
6246 down_read(&current->mm->mmap_sem);
John Hubbard2113b052020-01-30 22:13:13 -08006247 pret = pin_user_pages(ubuf, nr_pages,
Ira Weiny932f4a62019-05-13 17:17:03 -07006248 FOLL_WRITE | FOLL_LONGTERM,
6249 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07006250 if (pret == nr_pages) {
6251 /* don't support file backed memory */
6252 for (j = 0; j < nr_pages; j++) {
6253 struct vm_area_struct *vma = vmas[j];
6254
6255 if (vma->vm_file &&
6256 !is_file_hugepages(vma->vm_file)) {
6257 ret = -EOPNOTSUPP;
6258 break;
6259 }
6260 }
6261 } else {
6262 ret = pret < 0 ? pret : -EFAULT;
6263 }
6264 up_read(&current->mm->mmap_sem);
6265 if (ret) {
6266 /*
6267 * if we did partial map, or found file backed vmas,
6268 * release any pages we did get
6269 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07006270 if (pret > 0)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08006271 unpin_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07006272 if (ctx->account_mem)
6273 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006274 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07006275 goto err;
6276 }
6277
6278 off = ubuf & ~PAGE_MASK;
6279 size = iov.iov_len;
6280 for (j = 0; j < nr_pages; j++) {
6281 size_t vec_len;
6282
6283 vec_len = min_t(size_t, size, PAGE_SIZE - off);
6284 imu->bvec[j].bv_page = pages[j];
6285 imu->bvec[j].bv_len = vec_len;
6286 imu->bvec[j].bv_offset = off;
6287 off = 0;
6288 size -= vec_len;
6289 }
6290 /* store original address for later verification */
6291 imu->ubuf = ubuf;
6292 imu->len = iov.iov_len;
6293 imu->nr_bvecs = nr_pages;
6294
6295 ctx->nr_user_bufs++;
6296 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006297 kvfree(pages);
6298 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07006299 return 0;
6300err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006301 kvfree(pages);
6302 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07006303 io_sqe_buffer_unregister(ctx);
6304 return ret;
6305}
6306
Jens Axboe9b402842019-04-11 11:45:41 -06006307static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
6308{
6309 __s32 __user *fds = arg;
6310 int fd;
6311
6312 if (ctx->cq_ev_fd)
6313 return -EBUSY;
6314
6315 if (copy_from_user(&fd, fds, sizeof(*fds)))
6316 return -EFAULT;
6317
6318 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
6319 if (IS_ERR(ctx->cq_ev_fd)) {
6320 int ret = PTR_ERR(ctx->cq_ev_fd);
6321 ctx->cq_ev_fd = NULL;
6322 return ret;
6323 }
6324
6325 return 0;
6326}
6327
6328static int io_eventfd_unregister(struct io_ring_ctx *ctx)
6329{
6330 if (ctx->cq_ev_fd) {
6331 eventfd_ctx_put(ctx->cq_ev_fd);
6332 ctx->cq_ev_fd = NULL;
6333 return 0;
6334 }
6335
6336 return -ENXIO;
6337}
6338
Jens Axboe2b188cc2019-01-07 10:46:33 -07006339static void io_ring_ctx_free(struct io_ring_ctx *ctx)
6340{
Jens Axboe6b063142019-01-10 22:13:58 -07006341 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006342 if (ctx->sqo_mm)
6343 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07006344
6345 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07006346 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07006347 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06006348 io_eventfd_unregister(ctx);
Jens Axboe41726c92020-02-23 13:11:42 -07006349 idr_destroy(&ctx->personality_idr);
Jens Axboedef596e2019-01-09 08:59:42 -07006350
Jens Axboe2b188cc2019-01-07 10:46:33 -07006351#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07006352 if (ctx->ring_sock) {
6353 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07006354 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07006355 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006356#endif
6357
Hristo Venev75b28af2019-08-26 17:23:46 +00006358 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006359 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006360
6361 percpu_ref_exit(&ctx->refs);
6362 if (ctx->account_mem)
6363 io_unaccount_mem(ctx->user,
6364 ring_pages(ctx->sq_entries, ctx->cq_entries));
6365 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07006366 put_cred(ctx->creds);
Jens Axboe206aefd2019-11-07 18:27:42 -07006367 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -07006368 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07006369 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006370 kfree(ctx);
6371}
6372
6373static __poll_t io_uring_poll(struct file *file, poll_table *wait)
6374{
6375 struct io_ring_ctx *ctx = file->private_data;
6376 __poll_t mask = 0;
6377
6378 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02006379 /*
6380 * synchronizes with barrier from wq_has_sleeper call in
6381 * io_commit_cqring
6382 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07006383 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00006384 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
6385 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006386 mask |= EPOLLOUT | EPOLLWRNORM;
Stefano Garzarella63e5d812020-02-07 13:18:28 +01006387 if (io_cqring_events(ctx, false))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006388 mask |= EPOLLIN | EPOLLRDNORM;
6389
6390 return mask;
6391}
6392
6393static int io_uring_fasync(int fd, struct file *file, int on)
6394{
6395 struct io_ring_ctx *ctx = file->private_data;
6396
6397 return fasync_helper(fd, file, on, &ctx->cq_fasync);
6398}
6399
Jens Axboe071698e2020-01-28 10:04:42 -07006400static int io_remove_personalities(int id, void *p, void *data)
6401{
6402 struct io_ring_ctx *ctx = data;
6403 const struct cred *cred;
6404
6405 cred = idr_remove(&ctx->personality_idr, id);
6406 if (cred)
6407 put_cred(cred);
6408 return 0;
6409}
6410
Jens Axboe2b188cc2019-01-07 10:46:33 -07006411static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
6412{
6413 mutex_lock(&ctx->uring_lock);
6414 percpu_ref_kill(&ctx->refs);
6415 mutex_unlock(&ctx->uring_lock);
6416
Jens Axboedf069d82020-02-04 16:48:34 -07006417 /*
6418 * Wait for sq thread to idle, if we have one. It won't spin on new
6419 * work after we've killed the ctx ref above. This is important to do
6420 * before we cancel existing commands, as the thread could otherwise
6421 * be queueing new work post that. If that's work we need to cancel,
6422 * it could cause shutdown to hang.
6423 */
6424 while (ctx->sqo_thread && !wq_has_sleeper(&ctx->sqo_wait))
6425 cpu_relax();
6426
Jens Axboe5262f562019-09-17 12:26:57 -06006427 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07006428 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06006429
6430 if (ctx->io_wq)
6431 io_wq_cancel_all(ctx->io_wq);
6432
Jens Axboedef596e2019-01-09 08:59:42 -07006433 io_iopoll_reap_events(ctx);
Jens Axboe15dff282019-11-13 09:09:23 -07006434 /* if we failed setting up the ctx, we might not have any rings */
6435 if (ctx->rings)
6436 io_cqring_overflow_flush(ctx, true);
Jens Axboe071698e2020-01-28 10:04:42 -07006437 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
Jens Axboe206aefd2019-11-07 18:27:42 -07006438 wait_for_completion(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006439 io_ring_ctx_free(ctx);
6440}
6441
6442static int io_uring_release(struct inode *inode, struct file *file)
6443{
6444 struct io_ring_ctx *ctx = file->private_data;
6445
6446 file->private_data = NULL;
6447 io_ring_ctx_wait_and_kill(ctx);
6448 return 0;
6449}
6450
Jens Axboefcb323c2019-10-24 12:39:47 -06006451static void io_uring_cancel_files(struct io_ring_ctx *ctx,
6452 struct files_struct *files)
6453{
6454 struct io_kiocb *req;
6455 DEFINE_WAIT(wait);
6456
6457 while (!list_empty_careful(&ctx->inflight_list)) {
Jens Axboe768134d2019-11-10 20:30:53 -07006458 struct io_kiocb *cancel_req = NULL;
Jens Axboefcb323c2019-10-24 12:39:47 -06006459
6460 spin_lock_irq(&ctx->inflight_lock);
6461 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07006462 if (req->work.files != files)
6463 continue;
6464 /* req is being completed, ignore */
6465 if (!refcount_inc_not_zero(&req->refs))
6466 continue;
6467 cancel_req = req;
6468 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06006469 }
Jens Axboe768134d2019-11-10 20:30:53 -07006470 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06006471 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07006472 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06006473 spin_unlock_irq(&ctx->inflight_lock);
6474
Jens Axboe768134d2019-11-10 20:30:53 -07006475 /* We need to keep going until we don't find a matching req */
6476 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06006477 break;
Bob Liu2f6d9b92019-11-13 18:06:24 +08006478
Jens Axboe2ca10252020-02-13 17:17:35 -07006479 if (cancel_req->flags & REQ_F_OVERFLOW) {
6480 spin_lock_irq(&ctx->completion_lock);
6481 list_del(&cancel_req->list);
6482 cancel_req->flags &= ~REQ_F_OVERFLOW;
6483 if (list_empty(&ctx->cq_overflow_list)) {
6484 clear_bit(0, &ctx->sq_check_overflow);
6485 clear_bit(0, &ctx->cq_check_overflow);
6486 }
6487 spin_unlock_irq(&ctx->completion_lock);
6488
6489 WRITE_ONCE(ctx->rings->cq_overflow,
6490 atomic_inc_return(&ctx->cached_cq_overflow));
6491
6492 /*
6493 * Put inflight ref and overflow ref. If that's
6494 * all we had, then we're done with this request.
6495 */
6496 if (refcount_sub_and_test(2, &cancel_req->refs)) {
6497 io_put_req(cancel_req);
6498 continue;
6499 }
6500 }
6501
Bob Liu2f6d9b92019-11-13 18:06:24 +08006502 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
6503 io_put_req(cancel_req);
Jens Axboefcb323c2019-10-24 12:39:47 -06006504 schedule();
6505 }
Jens Axboe768134d2019-11-10 20:30:53 -07006506 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06006507}
6508
6509static int io_uring_flush(struct file *file, void *data)
6510{
6511 struct io_ring_ctx *ctx = file->private_data;
6512
6513 io_uring_cancel_files(ctx, data);
Jens Axboe6ab23142020-02-08 20:23:59 -07006514
6515 /*
6516 * If the task is going away, cancel work it may have pending
6517 */
6518 if (fatal_signal_pending(current) || (current->flags & PF_EXITING))
6519 io_wq_cancel_pid(ctx->io_wq, task_pid_vnr(current));
6520
Jens Axboefcb323c2019-10-24 12:39:47 -06006521 return 0;
6522}
6523
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006524static void *io_uring_validate_mmap_request(struct file *file,
6525 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006526{
Jens Axboe2b188cc2019-01-07 10:46:33 -07006527 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006528 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006529 struct page *page;
6530 void *ptr;
6531
6532 switch (offset) {
6533 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00006534 case IORING_OFF_CQ_RING:
6535 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006536 break;
6537 case IORING_OFF_SQES:
6538 ptr = ctx->sq_sqes;
6539 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006540 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006541 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006542 }
6543
6544 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07006545 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006546 return ERR_PTR(-EINVAL);
6547
6548 return ptr;
6549}
6550
6551#ifdef CONFIG_MMU
6552
6553static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
6554{
6555 size_t sz = vma->vm_end - vma->vm_start;
6556 unsigned long pfn;
6557 void *ptr;
6558
6559 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
6560 if (IS_ERR(ptr))
6561 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006562
6563 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
6564 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
6565}
6566
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006567#else /* !CONFIG_MMU */
6568
6569static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
6570{
6571 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
6572}
6573
6574static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
6575{
6576 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
6577}
6578
6579static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
6580 unsigned long addr, unsigned long len,
6581 unsigned long pgoff, unsigned long flags)
6582{
6583 void *ptr;
6584
6585 ptr = io_uring_validate_mmap_request(file, pgoff, len);
6586 if (IS_ERR(ptr))
6587 return PTR_ERR(ptr);
6588
6589 return (unsigned long) ptr;
6590}
6591
6592#endif /* !CONFIG_MMU */
6593
Jens Axboe2b188cc2019-01-07 10:46:33 -07006594SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
6595 u32, min_complete, u32, flags, const sigset_t __user *, sig,
6596 size_t, sigsz)
6597{
6598 struct io_ring_ctx *ctx;
6599 long ret = -EBADF;
6600 int submitted = 0;
6601 struct fd f;
6602
Jens Axboe6c271ce2019-01-10 11:22:30 -07006603 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006604 return -EINVAL;
6605
6606 f = fdget(fd);
6607 if (!f.file)
6608 return -EBADF;
6609
6610 ret = -EOPNOTSUPP;
6611 if (f.file->f_op != &io_uring_fops)
6612 goto out_fput;
6613
6614 ret = -ENXIO;
6615 ctx = f.file->private_data;
6616 if (!percpu_ref_tryget(&ctx->refs))
6617 goto out_fput;
6618
Jens Axboe6c271ce2019-01-10 11:22:30 -07006619 /*
6620 * For SQ polling, the thread will do all submissions and completions.
6621 * Just return the requested submit count, and wake the thread if
6622 * we were asked to.
6623 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06006624 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006625 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07006626 if (!list_empty_careful(&ctx->cq_overflow_list))
6627 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006628 if (flags & IORING_ENTER_SQ_WAKEUP)
6629 wake_up(&ctx->sqo_wait);
6630 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06006631 } else if (to_submit) {
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006632 struct mm_struct *cur_mm;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006633
6634 mutex_lock(&ctx->uring_lock);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006635 /* already have mm, so io_submit_sqes() won't try to grab it */
6636 cur_mm = ctx->sqo_mm;
6637 submitted = io_submit_sqes(ctx, to_submit, f.file, fd,
6638 &cur_mm, false);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006639 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03006640
6641 if (submitted != to_submit)
6642 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006643 }
6644 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07006645 unsigned nr_events = 0;
6646
Jens Axboe2b188cc2019-01-07 10:46:33 -07006647 min_complete = min(min_complete, ctx->cq_entries);
6648
Jens Axboedef596e2019-01-09 08:59:42 -07006649 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07006650 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07006651 } else {
6652 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
6653 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006654 }
6655
Pavel Begunkov7c504e652019-12-18 19:53:45 +03006656out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03006657 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006658out_fput:
6659 fdput(f);
6660 return submitted ? submitted : ret;
6661}
6662
Tobias Klauserbebdb652020-02-26 18:38:32 +01006663#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07006664static int io_uring_show_cred(int id, void *p, void *data)
6665{
6666 const struct cred *cred = p;
6667 struct seq_file *m = data;
6668 struct user_namespace *uns = seq_user_ns(m);
6669 struct group_info *gi;
6670 kernel_cap_t cap;
6671 unsigned __capi;
6672 int g;
6673
6674 seq_printf(m, "%5d\n", id);
6675 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
6676 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
6677 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
6678 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
6679 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
6680 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
6681 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
6682 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
6683 seq_puts(m, "\n\tGroups:\t");
6684 gi = cred->group_info;
6685 for (g = 0; g < gi->ngroups; g++) {
6686 seq_put_decimal_ull(m, g ? " " : "",
6687 from_kgid_munged(uns, gi->gid[g]));
6688 }
6689 seq_puts(m, "\n\tCapEff:\t");
6690 cap = cred->cap_effective;
6691 CAP_FOR_EACH_U32(__capi)
6692 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
6693 seq_putc(m, '\n');
6694 return 0;
6695}
6696
6697static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
6698{
6699 int i;
6700
6701 mutex_lock(&ctx->uring_lock);
6702 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
6703 for (i = 0; i < ctx->nr_user_files; i++) {
6704 struct fixed_file_table *table;
6705 struct file *f;
6706
6707 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
6708 f = table->files[i & IORING_FILE_TABLE_MASK];
6709 if (f)
6710 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
6711 else
6712 seq_printf(m, "%5u: <none>\n", i);
6713 }
6714 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
6715 for (i = 0; i < ctx->nr_user_bufs; i++) {
6716 struct io_mapped_ubuf *buf = &ctx->user_bufs[i];
6717
6718 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf,
6719 (unsigned int) buf->len);
6720 }
6721 if (!idr_is_empty(&ctx->personality_idr)) {
6722 seq_printf(m, "Personalities:\n");
6723 idr_for_each(&ctx->personality_idr, io_uring_show_cred, m);
6724 }
6725 mutex_unlock(&ctx->uring_lock);
6726}
6727
6728static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
6729{
6730 struct io_ring_ctx *ctx = f->private_data;
6731
6732 if (percpu_ref_tryget(&ctx->refs)) {
6733 __io_uring_show_fdinfo(ctx, m);
6734 percpu_ref_put(&ctx->refs);
6735 }
6736}
Tobias Klauserbebdb652020-02-26 18:38:32 +01006737#endif
Jens Axboe87ce9552020-01-30 08:25:34 -07006738
Jens Axboe2b188cc2019-01-07 10:46:33 -07006739static const struct file_operations io_uring_fops = {
6740 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06006741 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07006742 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006743#ifndef CONFIG_MMU
6744 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
6745 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
6746#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07006747 .poll = io_uring_poll,
6748 .fasync = io_uring_fasync,
Tobias Klauserbebdb652020-02-26 18:38:32 +01006749#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07006750 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +01006751#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07006752};
6753
6754static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
6755 struct io_uring_params *p)
6756{
Hristo Venev75b28af2019-08-26 17:23:46 +00006757 struct io_rings *rings;
6758 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006759
Hristo Venev75b28af2019-08-26 17:23:46 +00006760 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
6761 if (size == SIZE_MAX)
6762 return -EOVERFLOW;
6763
6764 rings = io_mem_alloc(size);
6765 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006766 return -ENOMEM;
6767
Hristo Venev75b28af2019-08-26 17:23:46 +00006768 ctx->rings = rings;
6769 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
6770 rings->sq_ring_mask = p->sq_entries - 1;
6771 rings->cq_ring_mask = p->cq_entries - 1;
6772 rings->sq_ring_entries = p->sq_entries;
6773 rings->cq_ring_entries = p->cq_entries;
6774 ctx->sq_mask = rings->sq_ring_mask;
6775 ctx->cq_mask = rings->cq_ring_mask;
6776 ctx->sq_entries = rings->sq_ring_entries;
6777 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006778
6779 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07006780 if (size == SIZE_MAX) {
6781 io_mem_free(ctx->rings);
6782 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006783 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07006784 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006785
6786 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07006787 if (!ctx->sq_sqes) {
6788 io_mem_free(ctx->rings);
6789 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006790 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07006791 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006792
Jens Axboe2b188cc2019-01-07 10:46:33 -07006793 return 0;
6794}
6795
6796/*
6797 * Allocate an anonymous fd, this is what constitutes the application
6798 * visible backing of an io_uring instance. The application mmaps this
6799 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
6800 * we have to tie this fd to a socket for file garbage collection purposes.
6801 */
6802static int io_uring_get_fd(struct io_ring_ctx *ctx)
6803{
6804 struct file *file;
6805 int ret;
6806
6807#if defined(CONFIG_UNIX)
6808 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
6809 &ctx->ring_sock);
6810 if (ret)
6811 return ret;
6812#endif
6813
6814 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
6815 if (ret < 0)
6816 goto err;
6817
6818 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
6819 O_RDWR | O_CLOEXEC);
6820 if (IS_ERR(file)) {
6821 put_unused_fd(ret);
6822 ret = PTR_ERR(file);
6823 goto err;
6824 }
6825
6826#if defined(CONFIG_UNIX)
6827 ctx->ring_sock->file = file;
6828#endif
6829 fd_install(ret, file);
6830 return ret;
6831err:
6832#if defined(CONFIG_UNIX)
6833 sock_release(ctx->ring_sock);
6834 ctx->ring_sock = NULL;
6835#endif
6836 return ret;
6837}
6838
6839static int io_uring_create(unsigned entries, struct io_uring_params *p)
6840{
6841 struct user_struct *user = NULL;
6842 struct io_ring_ctx *ctx;
6843 bool account_mem;
6844 int ret;
6845
Jens Axboe8110c1a2019-12-28 15:39:54 -07006846 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006847 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07006848 if (entries > IORING_MAX_ENTRIES) {
6849 if (!(p->flags & IORING_SETUP_CLAMP))
6850 return -EINVAL;
6851 entries = IORING_MAX_ENTRIES;
6852 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006853
6854 /*
6855 * Use twice as many entries for the CQ ring. It's possible for the
6856 * application to drive a higher depth than the size of the SQ ring,
6857 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06006858 * some flexibility in overcommitting a bit. If the application has
6859 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
6860 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07006861 */
6862 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06006863 if (p->flags & IORING_SETUP_CQSIZE) {
6864 /*
6865 * If IORING_SETUP_CQSIZE is set, we do the same roundup
6866 * to a power-of-two, if it isn't already. We do NOT impose
6867 * any cq vs sq ring sizing.
6868 */
Jens Axboe8110c1a2019-12-28 15:39:54 -07006869 if (p->cq_entries < p->sq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06006870 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07006871 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
6872 if (!(p->flags & IORING_SETUP_CLAMP))
6873 return -EINVAL;
6874 p->cq_entries = IORING_MAX_CQ_ENTRIES;
6875 }
Jens Axboe33a107f2019-10-04 12:10:03 -06006876 p->cq_entries = roundup_pow_of_two(p->cq_entries);
6877 } else {
6878 p->cq_entries = 2 * p->sq_entries;
6879 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006880
6881 user = get_uid(current_user());
6882 account_mem = !capable(CAP_IPC_LOCK);
6883
6884 if (account_mem) {
6885 ret = io_account_mem(user,
6886 ring_pages(p->sq_entries, p->cq_entries));
6887 if (ret) {
6888 free_uid(user);
6889 return ret;
6890 }
6891 }
6892
6893 ctx = io_ring_ctx_alloc(p);
6894 if (!ctx) {
6895 if (account_mem)
6896 io_unaccount_mem(user, ring_pages(p->sq_entries,
6897 p->cq_entries));
6898 free_uid(user);
6899 return -ENOMEM;
6900 }
6901 ctx->compat = in_compat_syscall();
6902 ctx->account_mem = account_mem;
6903 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07006904 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07006905
6906 ret = io_allocate_scq_urings(ctx, p);
6907 if (ret)
6908 goto err;
6909
Jens Axboe6c271ce2019-01-10 11:22:30 -07006910 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006911 if (ret)
6912 goto err;
6913
Jens Axboe2b188cc2019-01-07 10:46:33 -07006914 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00006915 p->sq_off.head = offsetof(struct io_rings, sq.head);
6916 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
6917 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
6918 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
6919 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
6920 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
6921 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006922
6923 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00006924 p->cq_off.head = offsetof(struct io_rings, cq.head);
6925 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
6926 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
6927 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
6928 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
6929 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06006930
Jens Axboe044c1ab2019-10-28 09:15:33 -06006931 /*
6932 * Install ring fd as the very last thing, so we don't risk someone
6933 * having closed it before we finish setup
6934 */
6935 ret = io_uring_get_fd(ctx);
6936 if (ret < 0)
6937 goto err;
6938
Jens Axboeda8c9692019-12-02 18:51:26 -07006939 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
Jens Axboecccf0ee2020-01-27 16:34:48 -07006940 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
6941 IORING_FEAT_CUR_PERSONALITY;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006942 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006943 return ret;
6944err:
6945 io_ring_ctx_wait_and_kill(ctx);
6946 return ret;
6947}
6948
6949/*
6950 * Sets up an aio uring context, and returns the fd. Applications asks for a
6951 * ring size, we return the actual sq/cq ring sizes (among other things) in the
6952 * params structure passed in.
6953 */
6954static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
6955{
6956 struct io_uring_params p;
6957 long ret;
6958 int i;
6959
6960 if (copy_from_user(&p, params, sizeof(p)))
6961 return -EFAULT;
6962 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
6963 if (p.resv[i])
6964 return -EINVAL;
6965 }
6966
Jens Axboe6c271ce2019-01-10 11:22:30 -07006967 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07006968 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Pavel Begunkov24369c22020-01-28 03:15:48 +03006969 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006970 return -EINVAL;
6971
6972 ret = io_uring_create(entries, &p);
6973 if (ret < 0)
6974 return ret;
6975
6976 if (copy_to_user(params, &p, sizeof(p)))
6977 return -EFAULT;
6978
6979 return ret;
6980}
6981
6982SYSCALL_DEFINE2(io_uring_setup, u32, entries,
6983 struct io_uring_params __user *, params)
6984{
6985 return io_uring_setup(entries, params);
6986}
6987
Jens Axboe66f4af92020-01-16 15:36:52 -07006988static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
6989{
6990 struct io_uring_probe *p;
6991 size_t size;
6992 int i, ret;
6993
6994 size = struct_size(p, ops, nr_args);
6995 if (size == SIZE_MAX)
6996 return -EOVERFLOW;
6997 p = kzalloc(size, GFP_KERNEL);
6998 if (!p)
6999 return -ENOMEM;
7000
7001 ret = -EFAULT;
7002 if (copy_from_user(p, arg, size))
7003 goto out;
7004 ret = -EINVAL;
7005 if (memchr_inv(p, 0, size))
7006 goto out;
7007
7008 p->last_op = IORING_OP_LAST - 1;
7009 if (nr_args > IORING_OP_LAST)
7010 nr_args = IORING_OP_LAST;
7011
7012 for (i = 0; i < nr_args; i++) {
7013 p->ops[i].op = i;
7014 if (!io_op_defs[i].not_supported)
7015 p->ops[i].flags = IO_URING_OP_SUPPORTED;
7016 }
7017 p->ops_len = i;
7018
7019 ret = 0;
7020 if (copy_to_user(arg, p, size))
7021 ret = -EFAULT;
7022out:
7023 kfree(p);
7024 return ret;
7025}
7026
Jens Axboe071698e2020-01-28 10:04:42 -07007027static int io_register_personality(struct io_ring_ctx *ctx)
7028{
7029 const struct cred *creds = get_current_cred();
7030 int id;
7031
7032 id = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1,
7033 USHRT_MAX, GFP_KERNEL);
7034 if (id < 0)
7035 put_cred(creds);
7036 return id;
7037}
7038
7039static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
7040{
7041 const struct cred *old_creds;
7042
7043 old_creds = idr_remove(&ctx->personality_idr, id);
7044 if (old_creds) {
7045 put_cred(old_creds);
7046 return 0;
7047 }
7048
7049 return -EINVAL;
7050}
7051
7052static bool io_register_op_must_quiesce(int op)
7053{
7054 switch (op) {
7055 case IORING_UNREGISTER_FILES:
7056 case IORING_REGISTER_FILES_UPDATE:
7057 case IORING_REGISTER_PROBE:
7058 case IORING_REGISTER_PERSONALITY:
7059 case IORING_UNREGISTER_PERSONALITY:
7060 return false;
7061 default:
7062 return true;
7063 }
7064}
7065
Jens Axboeedafcce2019-01-09 09:16:05 -07007066static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
7067 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06007068 __releases(ctx->uring_lock)
7069 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07007070{
7071 int ret;
7072
Jens Axboe35fa71a2019-04-22 10:23:23 -06007073 /*
7074 * We're inside the ring mutex, if the ref is already dying, then
7075 * someone else killed the ctx or is already going through
7076 * io_uring_register().
7077 */
7078 if (percpu_ref_is_dying(&ctx->refs))
7079 return -ENXIO;
7080
Jens Axboe071698e2020-01-28 10:04:42 -07007081 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007082 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06007083
Jens Axboe05f3fb32019-12-09 11:22:50 -07007084 /*
7085 * Drop uring mutex before waiting for references to exit. If
7086 * another thread is currently inside io_uring_enter() it might
7087 * need to grab the uring_lock to make progress. If we hold it
7088 * here across the drain wait, then we can deadlock. It's safe
7089 * to drop the mutex here, since no new references will come in
7090 * after we've killed the percpu ref.
7091 */
7092 mutex_unlock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07007093 ret = wait_for_completion_interruptible(&ctx->completions[0]);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007094 mutex_lock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07007095 if (ret) {
7096 percpu_ref_resurrect(&ctx->refs);
7097 ret = -EINTR;
7098 goto out;
7099 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007100 }
Jens Axboeedafcce2019-01-09 09:16:05 -07007101
7102 switch (opcode) {
7103 case IORING_REGISTER_BUFFERS:
7104 ret = io_sqe_buffer_register(ctx, arg, nr_args);
7105 break;
7106 case IORING_UNREGISTER_BUFFERS:
7107 ret = -EINVAL;
7108 if (arg || nr_args)
7109 break;
7110 ret = io_sqe_buffer_unregister(ctx);
7111 break;
Jens Axboe6b063142019-01-10 22:13:58 -07007112 case IORING_REGISTER_FILES:
7113 ret = io_sqe_files_register(ctx, arg, nr_args);
7114 break;
7115 case IORING_UNREGISTER_FILES:
7116 ret = -EINVAL;
7117 if (arg || nr_args)
7118 break;
7119 ret = io_sqe_files_unregister(ctx);
7120 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06007121 case IORING_REGISTER_FILES_UPDATE:
7122 ret = io_sqe_files_update(ctx, arg, nr_args);
7123 break;
Jens Axboe9b402842019-04-11 11:45:41 -06007124 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -07007125 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -06007126 ret = -EINVAL;
7127 if (nr_args != 1)
7128 break;
7129 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -07007130 if (ret)
7131 break;
7132 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
7133 ctx->eventfd_async = 1;
7134 else
7135 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -06007136 break;
7137 case IORING_UNREGISTER_EVENTFD:
7138 ret = -EINVAL;
7139 if (arg || nr_args)
7140 break;
7141 ret = io_eventfd_unregister(ctx);
7142 break;
Jens Axboe66f4af92020-01-16 15:36:52 -07007143 case IORING_REGISTER_PROBE:
7144 ret = -EINVAL;
7145 if (!arg || nr_args > 256)
7146 break;
7147 ret = io_probe(ctx, arg, nr_args);
7148 break;
Jens Axboe071698e2020-01-28 10:04:42 -07007149 case IORING_REGISTER_PERSONALITY:
7150 ret = -EINVAL;
7151 if (arg || nr_args)
7152 break;
7153 ret = io_register_personality(ctx);
7154 break;
7155 case IORING_UNREGISTER_PERSONALITY:
7156 ret = -EINVAL;
7157 if (arg)
7158 break;
7159 ret = io_unregister_personality(ctx, nr_args);
7160 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07007161 default:
7162 ret = -EINVAL;
7163 break;
7164 }
7165
Jens Axboe071698e2020-01-28 10:04:42 -07007166 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007167 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -07007168 percpu_ref_reinit(&ctx->refs);
Jens Axboec1503682020-01-08 08:26:07 -07007169out:
7170 reinit_completion(&ctx->completions[0]);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007171 }
Jens Axboeedafcce2019-01-09 09:16:05 -07007172 return ret;
7173}
7174
7175SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
7176 void __user *, arg, unsigned int, nr_args)
7177{
7178 struct io_ring_ctx *ctx;
7179 long ret = -EBADF;
7180 struct fd f;
7181
7182 f = fdget(fd);
7183 if (!f.file)
7184 return -EBADF;
7185
7186 ret = -EOPNOTSUPP;
7187 if (f.file->f_op != &io_uring_fops)
7188 goto out_fput;
7189
7190 ctx = f.file->private_data;
7191
7192 mutex_lock(&ctx->uring_lock);
7193 ret = __io_uring_register(ctx, opcode, arg, nr_args);
7194 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02007195 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
7196 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07007197out_fput:
7198 fdput(f);
7199 return ret;
7200}
7201
Jens Axboe2b188cc2019-01-07 10:46:33 -07007202static int __init io_uring_init(void)
7203{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01007204#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
7205 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
7206 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
7207} while (0)
7208
7209#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
7210 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
7211 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
7212 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
7213 BUILD_BUG_SQE_ELEM(1, __u8, flags);
7214 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
7215 BUILD_BUG_SQE_ELEM(4, __s32, fd);
7216 BUILD_BUG_SQE_ELEM(8, __u64, off);
7217 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
7218 BUILD_BUG_SQE_ELEM(16, __u64, addr);
7219 BUILD_BUG_SQE_ELEM(24, __u32, len);
7220 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
7221 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
7222 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
7223 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
7224 BUILD_BUG_SQE_ELEM(28, __u16, poll_events);
7225 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
7226 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
7227 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
7228 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
7229 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
7230 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
7231 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
7232 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
7233 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
7234 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
7235 BUILD_BUG_SQE_ELEM(42, __u16, personality);
7236
Jens Axboed3656342019-12-18 09:50:26 -07007237 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007238 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
7239 return 0;
7240};
7241__initcall(io_uring_init);