blob: 6f085215be13ac408c27923a89700bce528086fc [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 in_async;
555 bool needs_fixed_file;
Jens Axboed625c6e2019-12-17 19:53:05 -0700556 u8 opcode;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700557
558 struct io_ring_ctx *ctx;
Jens Axboeeac406c2019-11-14 12:09:58 -0700559 union {
560 struct list_head list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700561 struct hlist_node hash_node;
Jens Axboeeac406c2019-11-14 12:09:58 -0700562 };
Jens Axboe9e645e112019-05-10 16:07:28 -0600563 struct list_head link_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700564 unsigned int flags;
Jens Axboec16361c2019-01-17 08:39:48 -0700565 refcount_t refs;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700566 u64 user_data;
Jens Axboe9e645e112019-05-10 16:07:28 -0600567 u32 result;
Jens Axboede0617e2019-04-06 21:51:27 -0600568 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700569
Jens Axboefcb323c2019-10-24 12:39:47 -0600570 struct list_head inflight_entry;
571
Jens Axboe561fb042019-10-24 07:25:42 -0600572 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700573};
574
575#define IO_PLUG_THRESHOLD 2
Jens Axboedef596e2019-01-09 08:59:42 -0700576#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700577
Jens Axboe9a56a232019-01-09 09:06:50 -0700578struct io_submit_state {
579 struct blk_plug plug;
580
581 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700582 * io_kiocb alloc cache
583 */
584 void *reqs[IO_IOPOLL_BATCH];
Pavel Begunkov6c8a3132020-02-01 03:58:00 +0300585 unsigned int free_reqs;
Jens Axboe2579f912019-01-09 09:10:43 -0700586
587 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700588 * File reference cache
589 */
590 struct file *file;
591 unsigned int fd;
592 unsigned int has_refs;
593 unsigned int used_refs;
594 unsigned int ios_left;
595};
596
Jens Axboed3656342019-12-18 09:50:26 -0700597struct io_op_def {
598 /* needs req->io allocated for deferral/async */
599 unsigned async_ctx : 1;
600 /* needs current->mm setup, does mm access */
601 unsigned needs_mm : 1;
602 /* needs req->file assigned */
603 unsigned needs_file : 1;
604 /* needs req->file assigned IFF fd is >= 0 */
605 unsigned fd_non_neg : 1;
606 /* hash wq insertion if file is a regular file */
607 unsigned hash_reg_file : 1;
608 /* unbound wq insertion if file is a non-regular file */
609 unsigned unbound_nonreg_file : 1;
Jens Axboe66f4af92020-01-16 15:36:52 -0700610 /* opcode is not supported by this kernel */
611 unsigned not_supported : 1;
Jens Axboef86cd202020-01-29 13:46:44 -0700612 /* needs file table */
613 unsigned file_table : 1;
Jens Axboeff002b32020-02-07 16:05:21 -0700614 /* needs ->fs */
615 unsigned needs_fs : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700616};
617
618static const struct io_op_def io_op_defs[] = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300619 [IORING_OP_NOP] = {},
620 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700621 .async_ctx = 1,
622 .needs_mm = 1,
623 .needs_file = 1,
624 .unbound_nonreg_file = 1,
625 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300626 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700627 .async_ctx = 1,
628 .needs_mm = 1,
629 .needs_file = 1,
630 .hash_reg_file = 1,
631 .unbound_nonreg_file = 1,
632 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300633 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700634 .needs_file = 1,
635 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300636 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700637 .needs_file = 1,
638 .unbound_nonreg_file = 1,
639 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300640 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700641 .needs_file = 1,
642 .hash_reg_file = 1,
643 .unbound_nonreg_file = 1,
644 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300645 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700646 .needs_file = 1,
647 .unbound_nonreg_file = 1,
648 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300649 [IORING_OP_POLL_REMOVE] = {},
650 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700651 .needs_file = 1,
652 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300653 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700654 .async_ctx = 1,
655 .needs_mm = 1,
656 .needs_file = 1,
657 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700658 .needs_fs = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700659 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300660 [IORING_OP_RECVMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700661 .async_ctx = 1,
662 .needs_mm = 1,
663 .needs_file = 1,
664 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700665 .needs_fs = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700666 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300667 [IORING_OP_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700668 .async_ctx = 1,
669 .needs_mm = 1,
670 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300671 [IORING_OP_TIMEOUT_REMOVE] = {},
672 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700673 .needs_mm = 1,
674 .needs_file = 1,
675 .unbound_nonreg_file = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700676 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700677 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300678 [IORING_OP_ASYNC_CANCEL] = {},
679 [IORING_OP_LINK_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700680 .async_ctx = 1,
681 .needs_mm = 1,
682 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300683 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700684 .async_ctx = 1,
685 .needs_mm = 1,
686 .needs_file = 1,
687 .unbound_nonreg_file = 1,
688 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300689 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700690 .needs_file = 1,
691 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300692 [IORING_OP_OPENAT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700693 .needs_file = 1,
694 .fd_non_neg = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700695 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700696 .needs_fs = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700697 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300698 [IORING_OP_CLOSE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700699 .needs_file = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700700 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700701 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300702 [IORING_OP_FILES_UPDATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700703 .needs_mm = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700704 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700705 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300706 [IORING_OP_STATX] = {
Jens Axboed3656342019-12-18 09:50:26 -0700707 .needs_mm = 1,
708 .needs_file = 1,
709 .fd_non_neg = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700710 .needs_fs = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700711 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300712 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700713 .needs_mm = 1,
714 .needs_file = 1,
715 .unbound_nonreg_file = 1,
716 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300717 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700718 .needs_mm = 1,
719 .needs_file = 1,
720 .unbound_nonreg_file = 1,
721 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300722 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -0700723 .needs_file = 1,
724 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300725 [IORING_OP_MADVISE] = {
Jens Axboec1ca7572019-12-25 22:18:28 -0700726 .needs_mm = 1,
727 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300728 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700729 .needs_mm = 1,
730 .needs_file = 1,
731 .unbound_nonreg_file = 1,
732 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300733 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700734 .needs_mm = 1,
735 .needs_file = 1,
736 .unbound_nonreg_file = 1,
737 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300738 [IORING_OP_OPENAT2] = {
Jens Axboecebdb982020-01-08 17:59:24 -0700739 .needs_file = 1,
740 .fd_non_neg = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700741 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700742 .needs_fs = 1,
Jens Axboecebdb982020-01-08 17:59:24 -0700743 },
Jens Axboe3e4827b2020-01-08 15:18:09 -0700744 [IORING_OP_EPOLL_CTL] = {
745 .unbound_nonreg_file = 1,
746 .file_table = 1,
747 },
Jens Axboed3656342019-12-18 09:50:26 -0700748};
749
Jens Axboe561fb042019-10-24 07:25:42 -0600750static void io_wq_submit_work(struct io_wq_work **workptr);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700751static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800752static void io_put_req(struct io_kiocb *req);
Jens Axboe978db572019-11-14 22:39:04 -0700753static void __io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700754static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
755static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -0700756static int __io_sqe_files_update(struct io_ring_ctx *ctx,
757 struct io_uring_files_update *ip,
758 unsigned nr_args);
Jens Axboef86cd202020-01-29 13:46:44 -0700759static int io_grab_files(struct io_kiocb *req);
Jens Axboe2faf8522020-02-04 19:54:55 -0700760static void io_ring_file_ref_flush(struct fixed_file_data *data);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300761static void io_cleanup_req(struct io_kiocb *req);
Jens Axboede0617e2019-04-06 21:51:27 -0600762
Jens Axboe2b188cc2019-01-07 10:46:33 -0700763static struct kmem_cache *req_cachep;
764
765static const struct file_operations io_uring_fops;
766
767struct sock *io_uring_get_socket(struct file *file)
768{
769#if defined(CONFIG_UNIX)
770 if (file->f_op == &io_uring_fops) {
771 struct io_ring_ctx *ctx = file->private_data;
772
773 return ctx->ring_sock->sk;
774 }
775#endif
776 return NULL;
777}
778EXPORT_SYMBOL(io_uring_get_socket);
779
780static void io_ring_ctx_ref_free(struct percpu_ref *ref)
781{
782 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
783
Jens Axboe206aefd2019-11-07 18:27:42 -0700784 complete(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700785}
786
787static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
788{
789 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -0700790 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700791
792 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
793 if (!ctx)
794 return NULL;
795
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700796 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
797 if (!ctx->fallback_req)
798 goto err;
799
Jens Axboe206aefd2019-11-07 18:27:42 -0700800 ctx->completions = kmalloc(2 * sizeof(struct completion), GFP_KERNEL);
801 if (!ctx->completions)
802 goto err;
803
Jens Axboe78076bb2019-12-04 19:56:40 -0700804 /*
805 * Use 5 bits less than the max cq entries, that should give us around
806 * 32 entries per hash list if totally full and uniformly spread.
807 */
808 hash_bits = ilog2(p->cq_entries);
809 hash_bits -= 5;
810 if (hash_bits <= 0)
811 hash_bits = 1;
812 ctx->cancel_hash_bits = hash_bits;
813 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
814 GFP_KERNEL);
815 if (!ctx->cancel_hash)
816 goto err;
817 __hash_init(ctx->cancel_hash, 1U << hash_bits);
818
Roman Gushchin21482892019-05-07 10:01:48 -0700819 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -0700820 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
821 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700822
823 ctx->flags = p->flags;
824 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700825 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe206aefd2019-11-07 18:27:42 -0700826 init_completion(&ctx->completions[0]);
827 init_completion(&ctx->completions[1]);
Jens Axboe071698e2020-01-28 10:04:42 -0700828 idr_init(&ctx->personality_idr);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700829 mutex_init(&ctx->uring_lock);
830 init_waitqueue_head(&ctx->wait);
831 spin_lock_init(&ctx->completion_lock);
Jens Axboee94f1412019-12-19 12:06:02 -0700832 init_llist_head(&ctx->poll_llist);
Jens Axboedef596e2019-01-09 08:59:42 -0700833 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboede0617e2019-04-06 21:51:27 -0600834 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -0600835 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -0600836 init_waitqueue_head(&ctx->inflight_wait);
837 spin_lock_init(&ctx->inflight_lock);
838 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700839 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -0700840err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700841 if (ctx->fallback_req)
842 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe206aefd2019-11-07 18:27:42 -0700843 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -0700844 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -0700845 kfree(ctx);
846 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700847}
848
Bob Liu9d858b22019-11-13 18:06:25 +0800849static inline bool __req_need_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -0600850{
Jackie Liua197f662019-11-08 08:09:12 -0700851 struct io_ring_ctx *ctx = req->ctx;
852
Jens Axboe498ccd92019-10-25 10:04:25 -0600853 return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped
854 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboede0617e2019-04-06 21:51:27 -0600855}
856
Bob Liu9d858b22019-11-13 18:06:25 +0800857static inline bool req_need_defer(struct io_kiocb *req)
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600858{
Pavel Begunkov87987892020-01-18 01:22:30 +0300859 if (unlikely(req->flags & REQ_F_IO_DRAIN))
Bob Liu9d858b22019-11-13 18:06:25 +0800860 return __req_need_defer(req);
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600861
Bob Liu9d858b22019-11-13 18:06:25 +0800862 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600863}
864
865static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -0600866{
867 struct io_kiocb *req;
868
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600869 req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list);
Bob Liu9d858b22019-11-13 18:06:25 +0800870 if (req && !req_need_defer(req)) {
Jens Axboede0617e2019-04-06 21:51:27 -0600871 list_del_init(&req->list);
872 return req;
873 }
874
875 return NULL;
876}
877
Jens Axboe5262f562019-09-17 12:26:57 -0600878static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx)
879{
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600880 struct io_kiocb *req;
881
882 req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list);
Jens Axboe93bd25b2019-11-11 23:34:31 -0700883 if (req) {
884 if (req->flags & REQ_F_TIMEOUT_NOSEQ)
885 return NULL;
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800886 if (!__req_need_defer(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -0700887 list_del_init(&req->list);
888 return req;
889 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600890 }
891
892 return NULL;
Jens Axboe5262f562019-09-17 12:26:57 -0600893}
894
Jens Axboede0617e2019-04-06 21:51:27 -0600895static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700896{
Hristo Venev75b28af2019-08-26 17:23:46 +0000897 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700898
Pavel Begunkov07910152020-01-17 03:52:46 +0300899 /* order cqe stores with ring update */
900 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700901
Pavel Begunkov07910152020-01-17 03:52:46 +0300902 if (wq_has_sleeper(&ctx->cq_wait)) {
903 wake_up_interruptible(&ctx->cq_wait);
904 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700905 }
906}
907
Jens Axboecccf0ee2020-01-27 16:34:48 -0700908static inline void io_req_work_grab_env(struct io_kiocb *req,
909 const struct io_op_def *def)
Jens Axboe18d9be12019-09-10 09:13:05 -0600910{
Jens Axboecccf0ee2020-01-27 16:34:48 -0700911 if (!req->work.mm && def->needs_mm) {
912 mmgrab(current->mm);
913 req->work.mm = current->mm;
914 }
915 if (!req->work.creds)
916 req->work.creds = get_current_cred();
Jens Axboeff002b32020-02-07 16:05:21 -0700917 if (!req->work.fs && def->needs_fs) {
918 spin_lock(&current->fs->lock);
919 if (!current->fs->in_exec) {
920 req->work.fs = current->fs;
921 req->work.fs->users++;
922 } else {
923 req->work.flags |= IO_WQ_WORK_CANCEL;
924 }
925 spin_unlock(&current->fs->lock);
926 }
Jens Axboe6ab23142020-02-08 20:23:59 -0700927 if (!req->work.task_pid)
928 req->work.task_pid = task_pid_vnr(current);
Jens Axboecccf0ee2020-01-27 16:34:48 -0700929}
930
931static inline void io_req_work_drop_env(struct io_kiocb *req)
932{
933 if (req->work.mm) {
934 mmdrop(req->work.mm);
935 req->work.mm = NULL;
936 }
937 if (req->work.creds) {
938 put_cred(req->work.creds);
939 req->work.creds = NULL;
940 }
Jens Axboeff002b32020-02-07 16:05:21 -0700941 if (req->work.fs) {
942 struct fs_struct *fs = req->work.fs;
943
944 spin_lock(&req->work.fs->lock);
945 if (--fs->users)
946 fs = NULL;
947 spin_unlock(&req->work.fs->lock);
948 if (fs)
949 free_fs_struct(fs);
950 }
Jens Axboe561fb042019-10-24 07:25:42 -0600951}
952
Pavel Begunkovdeb6dc02020-02-24 11:30:17 +0300953static inline void io_prep_next_work(struct io_kiocb *req,
954 struct io_kiocb **link)
955{
956 const struct io_op_def *def = &io_op_defs[req->opcode];
957
958 if (!(req->flags & REQ_F_ISREG) && def->unbound_nonreg_file)
959 req->work.flags |= IO_WQ_WORK_UNBOUND;
960
961 *link = io_prep_linked_timeout(req);
962}
963
Jens Axboe94ae5e72019-11-14 19:39:52 -0700964static inline bool io_prep_async_work(struct io_kiocb *req,
965 struct io_kiocb **link)
Jens Axboe561fb042019-10-24 07:25:42 -0600966{
Jens Axboed3656342019-12-18 09:50:26 -0700967 const struct io_op_def *def = &io_op_defs[req->opcode];
Jens Axboe561fb042019-10-24 07:25:42 -0600968 bool do_hashed = false;
Jens Axboe54a91f32019-09-10 09:15:04 -0600969
Jens Axboed3656342019-12-18 09:50:26 -0700970 if (req->flags & REQ_F_ISREG) {
971 if (def->hash_reg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -0700972 do_hashed = true;
Jens Axboed3656342019-12-18 09:50:26 -0700973 } else {
974 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -0700975 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -0600976 }
Jens Axboecccf0ee2020-01-27 16:34:48 -0700977
978 io_req_work_grab_env(req, def);
Jens Axboe54a91f32019-09-10 09:15:04 -0600979
Jens Axboe94ae5e72019-11-14 19:39:52 -0700980 *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -0600981 return do_hashed;
982}
983
Jackie Liua197f662019-11-08 08:09:12 -0700984static inline void io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -0600985{
Jackie Liua197f662019-11-08 08:09:12 -0700986 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe94ae5e72019-11-14 19:39:52 -0700987 struct io_kiocb *link;
988 bool do_hashed;
989
990 do_hashed = io_prep_async_work(req, &link);
Jens Axboe561fb042019-10-24 07:25:42 -0600991
992 trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work,
993 req->flags);
994 if (!do_hashed) {
995 io_wq_enqueue(ctx->io_wq, &req->work);
996 } else {
997 io_wq_enqueue_hashed(ctx->io_wq, &req->work,
998 file_inode(req->file));
999 }
Jens Axboe94ae5e72019-11-14 19:39:52 -07001000
1001 if (link)
1002 io_queue_linked_timeout(link);
Jens Axboe18d9be12019-09-10 09:13:05 -06001003}
1004
Jens Axboe5262f562019-09-17 12:26:57 -06001005static void io_kill_timeout(struct io_kiocb *req)
1006{
1007 int ret;
1008
Jens Axboe2d283902019-12-04 11:08:05 -07001009 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe5262f562019-09-17 12:26:57 -06001010 if (ret != -1) {
1011 atomic_inc(&req->ctx->cq_timeouts);
Jens Axboe842f9612019-10-29 12:34:10 -06001012 list_del_init(&req->list);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001013 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001014 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -06001015 }
1016}
1017
1018static void io_kill_timeouts(struct io_ring_ctx *ctx)
1019{
1020 struct io_kiocb *req, *tmp;
1021
1022 spin_lock_irq(&ctx->completion_lock);
1023 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
1024 io_kill_timeout(req);
1025 spin_unlock_irq(&ctx->completion_lock);
1026}
1027
Jens Axboede0617e2019-04-06 21:51:27 -06001028static void io_commit_cqring(struct io_ring_ctx *ctx)
1029{
1030 struct io_kiocb *req;
1031
Jens Axboe5262f562019-09-17 12:26:57 -06001032 while ((req = io_get_timeout_req(ctx)) != NULL)
1033 io_kill_timeout(req);
1034
Jens Axboede0617e2019-04-06 21:51:27 -06001035 __io_commit_cqring(ctx);
1036
Pavel Begunkov87987892020-01-18 01:22:30 +03001037 while ((req = io_get_deferred_req(ctx)) != NULL)
Jackie Liua197f662019-11-08 08:09:12 -07001038 io_queue_async_work(req);
Jens Axboede0617e2019-04-06 21:51:27 -06001039}
1040
Jens Axboe2b188cc2019-01-07 10:46:33 -07001041static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
1042{
Hristo Venev75b28af2019-08-26 17:23:46 +00001043 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001044 unsigned tail;
1045
1046 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +02001047 /*
1048 * writes to the cq entry need to come after reading head; the
1049 * control dependency is enough as we're using WRITE_ONCE to
1050 * fill the cq entry
1051 */
Hristo Venev75b28af2019-08-26 17:23:46 +00001052 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001053 return NULL;
1054
1055 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +00001056 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001057}
1058
Jens Axboef2842ab2020-01-08 11:04:00 -07001059static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1060{
Jens Axboef0b493e2020-02-01 21:30:11 -07001061 if (!ctx->cq_ev_fd)
1062 return false;
Jens Axboef2842ab2020-01-08 11:04:00 -07001063 if (!ctx->eventfd_async)
1064 return true;
1065 return io_wq_current_is_worker() || in_interrupt();
1066}
1067
Jens Axboef0b493e2020-02-01 21:30:11 -07001068static void __io_cqring_ev_posted(struct io_ring_ctx *ctx, bool trigger_ev)
Jens Axboe8c838782019-03-12 15:48:16 -06001069{
1070 if (waitqueue_active(&ctx->wait))
1071 wake_up(&ctx->wait);
1072 if (waitqueue_active(&ctx->sqo_wait))
1073 wake_up(&ctx->sqo_wait);
Jens Axboef0b493e2020-02-01 21:30:11 -07001074 if (trigger_ev)
Jens Axboe9b402842019-04-11 11:45:41 -06001075 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -06001076}
1077
Jens Axboef0b493e2020-02-01 21:30:11 -07001078static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
1079{
1080 __io_cqring_ev_posted(ctx, io_should_trigger_evfd(ctx));
1081}
1082
Jens Axboec4a2ed72019-11-21 21:01:26 -07001083/* Returns true if there are no backlogged entries after the flush */
1084static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001085{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001086 struct io_rings *rings = ctx->rings;
1087 struct io_uring_cqe *cqe;
1088 struct io_kiocb *req;
1089 unsigned long flags;
1090 LIST_HEAD(list);
1091
1092 if (!force) {
1093 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001094 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001095 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
1096 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001097 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001098 }
1099
1100 spin_lock_irqsave(&ctx->completion_lock, flags);
1101
1102 /* if force is set, the ring is going away. always drop after that */
1103 if (force)
Jens Axboe69b3e542020-01-08 11:01:46 -07001104 ctx->cq_overflow_flushed = 1;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001105
Jens Axboec4a2ed72019-11-21 21:01:26 -07001106 cqe = NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001107 while (!list_empty(&ctx->cq_overflow_list)) {
1108 cqe = io_get_cqring(ctx);
1109 if (!cqe && !force)
1110 break;
1111
1112 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
1113 list);
1114 list_move(&req->list, &list);
Jens Axboe2ca10252020-02-13 17:17:35 -07001115 req->flags &= ~REQ_F_OVERFLOW;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001116 if (cqe) {
1117 WRITE_ONCE(cqe->user_data, req->user_data);
1118 WRITE_ONCE(cqe->res, req->result);
1119 WRITE_ONCE(cqe->flags, 0);
1120 } else {
1121 WRITE_ONCE(ctx->rings->cq_overflow,
1122 atomic_inc_return(&ctx->cached_cq_overflow));
1123 }
1124 }
1125
1126 io_commit_cqring(ctx);
Jens Axboead3eb2c2019-12-18 17:12:20 -07001127 if (cqe) {
1128 clear_bit(0, &ctx->sq_check_overflow);
1129 clear_bit(0, &ctx->cq_check_overflow);
1130 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001131 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1132 io_cqring_ev_posted(ctx);
1133
1134 while (!list_empty(&list)) {
1135 req = list_first_entry(&list, struct io_kiocb, list);
1136 list_del(&req->list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001137 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001138 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001139
1140 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001141}
1142
Jens Axboe78e19bb2019-11-06 15:21:34 -07001143static void io_cqring_fill_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001144{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001145 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001146 struct io_uring_cqe *cqe;
1147
Jens Axboe78e19bb2019-11-06 15:21:34 -07001148 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001149
Jens Axboe2b188cc2019-01-07 10:46:33 -07001150 /*
1151 * If we can't get a cq entry, userspace overflowed the
1152 * submission (by quite a lot). Increment the overflow count in
1153 * the ring.
1154 */
1155 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001156 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001157 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001158 WRITE_ONCE(cqe->res, res);
1159 WRITE_ONCE(cqe->flags, 0);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001160 } else if (ctx->cq_overflow_flushed) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001161 WRITE_ONCE(ctx->rings->cq_overflow,
1162 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001163 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001164 if (list_empty(&ctx->cq_overflow_list)) {
1165 set_bit(0, &ctx->sq_check_overflow);
1166 set_bit(0, &ctx->cq_check_overflow);
1167 }
Jens Axboe2ca10252020-02-13 17:17:35 -07001168 req->flags |= REQ_F_OVERFLOW;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001169 refcount_inc(&req->refs);
1170 req->result = res;
1171 list_add_tail(&req->list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001172 }
1173}
1174
Jens Axboe78e19bb2019-11-06 15:21:34 -07001175static void io_cqring_add_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001176{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001177 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001178 unsigned long flags;
1179
1180 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001181 io_cqring_fill_event(req, res);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001182 io_commit_cqring(ctx);
1183 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1184
Jens Axboe8c838782019-03-12 15:48:16 -06001185 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001186}
1187
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001188static inline bool io_is_fallback_req(struct io_kiocb *req)
1189{
1190 return req == (struct io_kiocb *)
1191 ((unsigned long) req->ctx->fallback_req & ~1UL);
1192}
1193
1194static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
1195{
1196 struct io_kiocb *req;
1197
1198 req = ctx->fallback_req;
1199 if (!test_and_set_bit_lock(0, (unsigned long *) ctx->fallback_req))
1200 return req;
1201
1202 return NULL;
1203}
1204
Jens Axboe2579f912019-01-09 09:10:43 -07001205static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
1206 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001207{
Jens Axboefd6fab22019-03-14 16:30:06 -06001208 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001209 struct io_kiocb *req;
1210
Jens Axboe2579f912019-01-09 09:10:43 -07001211 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -06001212 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -07001213 if (unlikely(!req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001214 goto fallback;
Jens Axboe2579f912019-01-09 09:10:43 -07001215 } else if (!state->free_reqs) {
1216 size_t sz;
1217 int ret;
1218
1219 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -06001220 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
1221
1222 /*
1223 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1224 * retry single alloc to be on the safe side.
1225 */
1226 if (unlikely(ret <= 0)) {
1227 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1228 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001229 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -06001230 ret = 1;
1231 }
Jens Axboe2579f912019-01-09 09:10:43 -07001232 state->free_reqs = ret - 1;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001233 req = state->reqs[ret - 1];
Jens Axboe2579f912019-01-09 09:10:43 -07001234 } else {
Jens Axboe2579f912019-01-09 09:10:43 -07001235 state->free_reqs--;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001236 req = state->reqs[state->free_reqs];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001237 }
1238
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001239got_it:
Jens Axboe1a6b74f2019-12-02 10:33:15 -07001240 req->io = NULL;
Jens Axboe60c112b2019-06-21 10:20:18 -06001241 req->file = NULL;
Jens Axboe2579f912019-01-09 09:10:43 -07001242 req->ctx = ctx;
1243 req->flags = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06001244 /* one is dropped after submission, the other at completion */
1245 refcount_set(&req->refs, 2);
Jens Axboe9e645e112019-05-10 16:07:28 -06001246 req->result = 0;
Jens Axboe561fb042019-10-24 07:25:42 -06001247 INIT_IO_WORK(&req->work, io_wq_submit_work);
Jens Axboe2579f912019-01-09 09:10:43 -07001248 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001249fallback:
1250 req = io_get_fallback_req(ctx);
1251 if (req)
1252 goto got_it;
Pavel Begunkov6805b322019-10-08 02:18:42 +03001253 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001254 return NULL;
1255}
1256
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001257static void __io_req_do_free(struct io_kiocb *req)
Jens Axboedef596e2019-01-09 08:59:42 -07001258{
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001259 if (likely(!io_is_fallback_req(req)))
1260 kmem_cache_free(req_cachep, req);
1261 else
1262 clear_bit_unlock(0, (unsigned long *) req->ctx->fallback_req);
1263}
1264
Jens Axboec6ca97b302019-12-28 12:11:08 -07001265static void __io_req_aux_free(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001266{
Jens Axboefcb323c2019-10-24 12:39:47 -06001267 struct io_ring_ctx *ctx = req->ctx;
1268
Pavel Begunkov929a3af2020-02-19 00:19:09 +03001269 if (req->flags & REQ_F_NEED_CLEANUP)
1270 io_cleanup_req(req);
1271
YueHaibing96fd84d2020-01-07 22:22:44 +08001272 kfree(req->io);
Jens Axboe05f3fb32019-12-09 11:22:50 -07001273 if (req->file) {
1274 if (req->flags & REQ_F_FIXED_FILE)
1275 percpu_ref_put(&ctx->file_data->refs);
1276 else
1277 fput(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001278 }
Jens Axboecccf0ee2020-01-27 16:34:48 -07001279
1280 io_req_work_drop_env(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001281}
1282
1283static void __io_free_req(struct io_kiocb *req)
1284{
Jens Axboec6ca97b302019-12-28 12:11:08 -07001285 __io_req_aux_free(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001286
Jens Axboefcb323c2019-10-24 12:39:47 -06001287 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001288 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06001289 unsigned long flags;
1290
1291 spin_lock_irqsave(&ctx->inflight_lock, flags);
1292 list_del(&req->inflight_entry);
1293 if (waitqueue_active(&ctx->inflight_wait))
1294 wake_up(&ctx->inflight_wait);
1295 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1296 }
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001297
1298 percpu_ref_put(&req->ctx->refs);
1299 __io_req_do_free(req);
Jens Axboee65ef562019-03-12 10:16:44 -06001300}
1301
Jens Axboec6ca97b302019-12-28 12:11:08 -07001302struct req_batch {
1303 void *reqs[IO_IOPOLL_BATCH];
1304 int to_free;
1305 int need_iter;
1306};
1307
1308static void io_free_req_many(struct io_ring_ctx *ctx, struct req_batch *rb)
1309{
Jens Axboe10fef4b2020-01-09 07:52:28 -07001310 int fixed_refs = rb->to_free;
1311
Jens Axboec6ca97b302019-12-28 12:11:08 -07001312 if (!rb->to_free)
1313 return;
1314 if (rb->need_iter) {
1315 int i, inflight = 0;
1316 unsigned long flags;
1317
Jens Axboe10fef4b2020-01-09 07:52:28 -07001318 fixed_refs = 0;
Jens Axboec6ca97b302019-12-28 12:11:08 -07001319 for (i = 0; i < rb->to_free; i++) {
1320 struct io_kiocb *req = rb->reqs[i];
1321
Jens Axboe10fef4b2020-01-09 07:52:28 -07001322 if (req->flags & REQ_F_FIXED_FILE) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001323 req->file = NULL;
Jens Axboe10fef4b2020-01-09 07:52:28 -07001324 fixed_refs++;
1325 }
Jens Axboec6ca97b302019-12-28 12:11:08 -07001326 if (req->flags & REQ_F_INFLIGHT)
1327 inflight++;
Jens Axboec6ca97b302019-12-28 12:11:08 -07001328 __io_req_aux_free(req);
1329 }
1330 if (!inflight)
1331 goto do_free;
1332
1333 spin_lock_irqsave(&ctx->inflight_lock, flags);
1334 for (i = 0; i < rb->to_free; i++) {
1335 struct io_kiocb *req = rb->reqs[i];
1336
Jens Axboe10fef4b2020-01-09 07:52:28 -07001337 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001338 list_del(&req->inflight_entry);
1339 if (!--inflight)
1340 break;
1341 }
1342 }
1343 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1344
1345 if (waitqueue_active(&ctx->inflight_wait))
1346 wake_up(&ctx->inflight_wait);
1347 }
1348do_free:
1349 kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs);
Jens Axboe10fef4b2020-01-09 07:52:28 -07001350 if (fixed_refs)
1351 percpu_ref_put_many(&ctx->file_data->refs, fixed_refs);
Jens Axboec6ca97b302019-12-28 12:11:08 -07001352 percpu_ref_put_many(&ctx->refs, rb->to_free);
Jens Axboec6ca97b302019-12-28 12:11:08 -07001353 rb->to_free = rb->need_iter = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06001354}
1355
Jackie Liua197f662019-11-08 08:09:12 -07001356static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001357{
Jackie Liua197f662019-11-08 08:09:12 -07001358 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001359 int ret;
1360
Jens Axboe2d283902019-12-04 11:08:05 -07001361 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe2665abf2019-11-05 12:40:47 -07001362 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001363 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -07001364 io_commit_cqring(ctx);
1365 req->flags &= ~REQ_F_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001366 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001367 return true;
1368 }
1369
1370 return false;
1371}
1372
Jens Axboeba816ad2019-09-28 11:36:45 -06001373static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -06001374{
Jens Axboe2665abf2019-11-05 12:40:47 -07001375 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001376 bool wake_ev = false;
Jens Axboe9e645e112019-05-10 16:07:28 -06001377
Jens Axboe4d7dd462019-11-20 13:03:52 -07001378 /* Already got next link */
1379 if (req->flags & REQ_F_LINK_NEXT)
1380 return;
1381
Jens Axboe9e645e112019-05-10 16:07:28 -06001382 /*
1383 * The list should never be empty when we are called here. But could
1384 * potentially happen if the chain is messed up, check to be on the
1385 * safe side.
1386 */
Pavel Begunkov44932332019-12-05 16:16:35 +03001387 while (!list_empty(&req->link_list)) {
1388 struct io_kiocb *nxt = list_first_entry(&req->link_list,
1389 struct io_kiocb, link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001390
Pavel Begunkov44932332019-12-05 16:16:35 +03001391 if (unlikely((req->flags & REQ_F_LINK_TIMEOUT) &&
1392 (nxt->flags & REQ_F_TIMEOUT))) {
1393 list_del_init(&nxt->link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001394 wake_ev |= io_link_cancel_timeout(nxt);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001395 req->flags &= ~REQ_F_LINK_TIMEOUT;
1396 continue;
1397 }
Jens Axboe9e645e112019-05-10 16:07:28 -06001398
Pavel Begunkov44932332019-12-05 16:16:35 +03001399 list_del_init(&req->link_list);
1400 if (!list_empty(&nxt->link_list))
1401 nxt->flags |= REQ_F_LINK;
Pavel Begunkovb18fdf72019-11-21 23:21:02 +03001402 *nxtptr = nxt;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001403 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06001404 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001405
Jens Axboe4d7dd462019-11-20 13:03:52 -07001406 req->flags |= REQ_F_LINK_NEXT;
Jens Axboe2665abf2019-11-05 12:40:47 -07001407 if (wake_ev)
1408 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001409}
1410
1411/*
1412 * Called if REQ_F_LINK is set, and we fail the head request
1413 */
1414static void io_fail_links(struct io_kiocb *req)
1415{
Jens Axboe2665abf2019-11-05 12:40:47 -07001416 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001417 unsigned long flags;
1418
1419 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06001420
1421 while (!list_empty(&req->link_list)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03001422 struct io_kiocb *link = list_first_entry(&req->link_list,
1423 struct io_kiocb, link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06001424
Pavel Begunkov44932332019-12-05 16:16:35 +03001425 list_del_init(&link->link_list);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001426 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001427
1428 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
Jens Axboed625c6e2019-12-17 19:53:05 -07001429 link->opcode == IORING_OP_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -07001430 io_link_cancel_timeout(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001431 } else {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001432 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe978db572019-11-14 22:39:04 -07001433 __io_double_put_req(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001434 }
Jens Axboe5d960722019-11-19 15:31:28 -07001435 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -06001436 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001437
1438 io_commit_cqring(ctx);
1439 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1440 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001441}
1442
Jens Axboe4d7dd462019-11-20 13:03:52 -07001443static void io_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -06001444{
Jens Axboe4d7dd462019-11-20 13:03:52 -07001445 if (likely(!(req->flags & REQ_F_LINK)))
Jens Axboe2665abf2019-11-05 12:40:47 -07001446 return;
Jens Axboe2665abf2019-11-05 12:40:47 -07001447
Jens Axboe9e645e112019-05-10 16:07:28 -06001448 /*
1449 * If LINK is set, we have dependent requests in this chain. If we
1450 * didn't fail this request, queue the first one up, moving any other
1451 * dependencies to the next request. In case of failure, fail the rest
1452 * of the chain.
1453 */
Jens Axboe2665abf2019-11-05 12:40:47 -07001454 if (req->flags & REQ_F_FAIL_LINK) {
1455 io_fail_links(req);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001456 } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) ==
1457 REQ_F_LINK_TIMEOUT) {
Jens Axboe2665abf2019-11-05 12:40:47 -07001458 struct io_ring_ctx *ctx = req->ctx;
1459 unsigned long flags;
1460
1461 /*
1462 * If this is a timeout link, we could be racing with the
1463 * timeout timer. Grab the completion lock for this case to
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001464 * protect against that.
Jens Axboe2665abf2019-11-05 12:40:47 -07001465 */
1466 spin_lock_irqsave(&ctx->completion_lock, flags);
1467 io_req_link_next(req, nxt);
1468 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1469 } else {
1470 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -06001471 }
Jens Axboe4d7dd462019-11-20 13:03:52 -07001472}
Jens Axboe9e645e112019-05-10 16:07:28 -06001473
Jackie Liuc69f8db2019-11-09 11:00:08 +08001474static void io_free_req(struct io_kiocb *req)
1475{
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001476 struct io_kiocb *nxt = NULL;
1477
1478 io_req_find_next(req, &nxt);
Pavel Begunkov70cf9f32019-11-21 23:21:00 +03001479 __io_free_req(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001480
1481 if (nxt)
1482 io_queue_async_work(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08001483}
1484
Jens Axboeba816ad2019-09-28 11:36:45 -06001485/*
1486 * Drop reference to request, return next in chain (if there is one) if this
1487 * was the last reference to this request.
1488 */
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001489__attribute__((nonnull))
Jackie Liuec9c02a2019-11-08 23:50:36 +08001490static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboee65ef562019-03-12 10:16:44 -06001491{
Jens Axboe2a44f462020-02-25 13:25:41 -07001492 if (refcount_dec_and_test(&req->refs)) {
1493 io_req_find_next(req, nxtptr);
Jens Axboe4d7dd462019-11-20 13:03:52 -07001494 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07001495 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001496}
1497
Jens Axboe2b188cc2019-01-07 10:46:33 -07001498static void io_put_req(struct io_kiocb *req)
1499{
Jens Axboedef596e2019-01-09 08:59:42 -07001500 if (refcount_dec_and_test(&req->refs))
1501 io_free_req(req);
1502}
1503
Jens Axboe978db572019-11-14 22:39:04 -07001504/*
1505 * Must only be used if we don't need to care about links, usually from
1506 * within the completion handling itself.
1507 */
1508static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06001509{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001510 /* drop both submit and complete references */
1511 if (refcount_sub_and_test(2, &req->refs))
1512 __io_free_req(req);
1513}
1514
Jens Axboe978db572019-11-14 22:39:04 -07001515static void io_double_put_req(struct io_kiocb *req)
1516{
1517 /* drop both submit and complete references */
1518 if (refcount_sub_and_test(2, &req->refs))
1519 io_free_req(req);
1520}
1521
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001522static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06001523{
Jens Axboe84f97dc2019-11-06 11:27:53 -07001524 struct io_rings *rings = ctx->rings;
1525
Jens Axboead3eb2c2019-12-18 17:12:20 -07001526 if (test_bit(0, &ctx->cq_check_overflow)) {
1527 /*
1528 * noflush == true is from the waitqueue handler, just ensure
1529 * we wake up the task, and the next invocation will flush the
1530 * entries. We cannot safely to it from here.
1531 */
1532 if (noflush && !list_empty(&ctx->cq_overflow_list))
1533 return -1U;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001534
Jens Axboead3eb2c2019-12-18 17:12:20 -07001535 io_cqring_overflow_flush(ctx, false);
1536 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001537
Jens Axboea3a0e432019-08-20 11:03:11 -06001538 /* See comment at the top of this file */
1539 smp_rmb();
Jens Axboead3eb2c2019-12-18 17:12:20 -07001540 return ctx->cached_cq_tail - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06001541}
1542
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03001543static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1544{
1545 struct io_rings *rings = ctx->rings;
1546
1547 /* make sure SQ entry isn't read before tail */
1548 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1549}
1550
Jens Axboe8237e042019-12-28 10:48:22 -07001551static inline bool io_req_multi_free(struct req_batch *rb, struct io_kiocb *req)
Jens Axboee94f1412019-12-19 12:06:02 -07001552{
Jens Axboec6ca97b302019-12-28 12:11:08 -07001553 if ((req->flags & REQ_F_LINK) || io_is_fallback_req(req))
1554 return false;
Jens Axboee94f1412019-12-19 12:06:02 -07001555
Jens Axboec6ca97b302019-12-28 12:11:08 -07001556 if (!(req->flags & REQ_F_FIXED_FILE) || req->io)
1557 rb->need_iter++;
1558
1559 rb->reqs[rb->to_free++] = req;
1560 if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
1561 io_free_req_many(req->ctx, rb);
1562 return true;
Jens Axboee94f1412019-12-19 12:06:02 -07001563}
1564
Jens Axboedef596e2019-01-09 08:59:42 -07001565/*
1566 * Find and free completed poll iocbs
1567 */
1568static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
1569 struct list_head *done)
1570{
Jens Axboe8237e042019-12-28 10:48:22 -07001571 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07001572 struct io_kiocb *req;
Jens Axboedef596e2019-01-09 08:59:42 -07001573
Jens Axboec6ca97b302019-12-28 12:11:08 -07001574 rb.to_free = rb.need_iter = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001575 while (!list_empty(done)) {
1576 req = list_first_entry(done, struct io_kiocb, list);
1577 list_del(&req->list);
1578
Jens Axboe78e19bb2019-11-06 15:21:34 -07001579 io_cqring_fill_event(req, req->result);
Jens Axboedef596e2019-01-09 08:59:42 -07001580 (*nr_events)++;
1581
Jens Axboe8237e042019-12-28 10:48:22 -07001582 if (refcount_dec_and_test(&req->refs) &&
1583 !io_req_multi_free(&rb, req))
1584 io_free_req(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001585 }
Jens Axboedef596e2019-01-09 08:59:42 -07001586
Jens Axboe09bb8392019-03-13 12:39:28 -06001587 io_commit_cqring(ctx);
Jens Axboe8237e042019-12-28 10:48:22 -07001588 io_free_req_many(ctx, &rb);
Jens Axboedef596e2019-01-09 08:59:42 -07001589}
1590
1591static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1592 long min)
1593{
1594 struct io_kiocb *req, *tmp;
1595 LIST_HEAD(done);
1596 bool spin;
1597 int ret;
1598
1599 /*
1600 * Only spin for completions if we don't have multiple devices hanging
1601 * off our complete list, and we're under the requested amount.
1602 */
1603 spin = !ctx->poll_multi_file && *nr_events < min;
1604
1605 ret = 0;
1606 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
Jens Axboe9adbd452019-12-20 08:45:55 -07001607 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07001608
1609 /*
1610 * Move completed entries to our local list. If we find a
1611 * request that requires polling, break out and complete
1612 * the done list first, if we have entries there.
1613 */
1614 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
1615 list_move_tail(&req->list, &done);
1616 continue;
1617 }
1618 if (!list_empty(&done))
1619 break;
1620
1621 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1622 if (ret < 0)
1623 break;
1624
1625 if (ret && spin)
1626 spin = false;
1627 ret = 0;
1628 }
1629
1630 if (!list_empty(&done))
1631 io_iopoll_complete(ctx, nr_events, &done);
1632
1633 return ret;
1634}
1635
1636/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08001637 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07001638 * non-spinning poll check - we'll still enter the driver poll loop, but only
1639 * as a non-spinning completion check.
1640 */
1641static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
1642 long min)
1643{
Jens Axboe08f54392019-08-21 22:19:11 -06001644 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07001645 int ret;
1646
1647 ret = io_do_iopoll(ctx, nr_events, min);
1648 if (ret < 0)
1649 return ret;
1650 if (!min || *nr_events >= min)
1651 return 0;
1652 }
1653
1654 return 1;
1655}
1656
1657/*
1658 * We can't just wait for polled events to come to us, we have to actively
1659 * find and complete them.
1660 */
1661static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
1662{
1663 if (!(ctx->flags & IORING_SETUP_IOPOLL))
1664 return;
1665
1666 mutex_lock(&ctx->uring_lock);
1667 while (!list_empty(&ctx->poll_list)) {
1668 unsigned int nr_events = 0;
1669
1670 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -06001671
1672 /*
1673 * Ensure we allow local-to-the-cpu processing to take place,
1674 * in this case we need to ensure that we reap all events.
1675 */
1676 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -07001677 }
1678 mutex_unlock(&ctx->uring_lock);
1679}
1680
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08001681static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1682 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07001683{
Jens Axboe2b2ed972019-10-25 10:06:15 -06001684 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001685
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08001686 /*
1687 * We disallow the app entering submit/complete with polling, but we
1688 * still need to lock the ring to prevent racing with polled issue
1689 * that got punted to a workqueue.
1690 */
1691 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001692 do {
1693 int tmin = 0;
1694
Jens Axboe500f9fb2019-08-19 12:15:59 -06001695 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06001696 * Don't enter poll loop if we already have events pending.
1697 * If we do, we can potentially be spinning for commands that
1698 * already triggered a CQE (eg in error).
1699 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001700 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06001701 break;
1702
1703 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06001704 * If a submit got punted to a workqueue, we can have the
1705 * application entering polling for a command before it gets
1706 * issued. That app will hold the uring_lock for the duration
1707 * of the poll right here, so we need to take a breather every
1708 * now and then to ensure that the issue has a chance to add
1709 * the poll to the issued list. Otherwise we can spin here
1710 * forever, while the workqueue is stuck trying to acquire the
1711 * very same mutex.
1712 */
1713 if (!(++iters & 7)) {
1714 mutex_unlock(&ctx->uring_lock);
1715 mutex_lock(&ctx->uring_lock);
1716 }
1717
Jens Axboedef596e2019-01-09 08:59:42 -07001718 if (*nr_events < min)
1719 tmin = min - *nr_events;
1720
1721 ret = io_iopoll_getevents(ctx, nr_events, tmin);
1722 if (ret <= 0)
1723 break;
1724 ret = 0;
1725 } while (min && !*nr_events && !need_resched());
1726
Jens Axboe500f9fb2019-08-19 12:15:59 -06001727 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001728 return ret;
1729}
1730
Jens Axboe491381ce2019-10-17 09:20:46 -06001731static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001732{
Jens Axboe491381ce2019-10-17 09:20:46 -06001733 /*
1734 * Tell lockdep we inherited freeze protection from submission
1735 * thread.
1736 */
1737 if (req->flags & REQ_F_ISREG) {
1738 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001739
Jens Axboe491381ce2019-10-17 09:20:46 -06001740 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001741 }
Jens Axboe491381ce2019-10-17 09:20:46 -06001742 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001743}
1744
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001745static inline void req_set_fail_links(struct io_kiocb *req)
1746{
1747 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1748 req->flags |= REQ_F_FAIL_LINK;
1749}
1750
Jens Axboeba816ad2019-09-28 11:36:45 -06001751static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001752{
Jens Axboe9adbd452019-12-20 08:45:55 -07001753 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001754
Jens Axboe491381ce2019-10-17 09:20:46 -06001755 if (kiocb->ki_flags & IOCB_WRITE)
1756 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001757
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001758 if (res != req->result)
1759 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001760 io_cqring_add_event(req, res);
Jens Axboeba816ad2019-09-28 11:36:45 -06001761}
1762
1763static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1764{
Jens Axboe9adbd452019-12-20 08:45:55 -07001765 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06001766
1767 io_complete_rw_common(kiocb, res);
Jens Axboee65ef562019-03-12 10:16:44 -06001768 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001769}
1770
Jens Axboeba816ad2019-09-28 11:36:45 -06001771static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res)
1772{
Jens Axboe9adbd452019-12-20 08:45:55 -07001773 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001774 struct io_kiocb *nxt = NULL;
Jens Axboeba816ad2019-09-28 11:36:45 -06001775
1776 io_complete_rw_common(kiocb, res);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001777 io_put_req_find_next(req, &nxt);
1778
1779 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001780}
1781
Jens Axboedef596e2019-01-09 08:59:42 -07001782static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1783{
Jens Axboe9adbd452019-12-20 08:45:55 -07001784 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07001785
Jens Axboe491381ce2019-10-17 09:20:46 -06001786 if (kiocb->ki_flags & IOCB_WRITE)
1787 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001788
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001789 if (res != req->result)
1790 req_set_fail_links(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06001791 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -07001792 if (res != -EAGAIN)
1793 req->flags |= REQ_F_IOPOLL_COMPLETED;
1794}
1795
1796/*
1797 * After the iocb has been issued, it's safe to be found on the poll list.
1798 * Adding the kiocb to the list AFTER submission ensures that we don't
1799 * find it from a io_iopoll_getevents() thread before the issuer is done
1800 * accessing the kiocb cookie.
1801 */
1802static void io_iopoll_req_issued(struct io_kiocb *req)
1803{
1804 struct io_ring_ctx *ctx = req->ctx;
1805
1806 /*
1807 * Track whether we have multiple files in our lists. This will impact
1808 * how we do polling eventually, not spinning if we're on potentially
1809 * different devices.
1810 */
1811 if (list_empty(&ctx->poll_list)) {
1812 ctx->poll_multi_file = false;
1813 } else if (!ctx->poll_multi_file) {
1814 struct io_kiocb *list_req;
1815
1816 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1817 list);
Jens Axboe9adbd452019-12-20 08:45:55 -07001818 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07001819 ctx->poll_multi_file = true;
1820 }
1821
1822 /*
1823 * For fast devices, IO may have already completed. If it has, add
1824 * it to the front so we find it first.
1825 */
1826 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1827 list_add(&req->list, &ctx->poll_list);
1828 else
1829 list_add_tail(&req->list, &ctx->poll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08001830
1831 if ((ctx->flags & IORING_SETUP_SQPOLL) &&
1832 wq_has_sleeper(&ctx->sqo_wait))
1833 wake_up(&ctx->sqo_wait);
Jens Axboedef596e2019-01-09 08:59:42 -07001834}
1835
Jens Axboe3d6770f2019-04-13 11:50:54 -06001836static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07001837{
Jens Axboe3d6770f2019-04-13 11:50:54 -06001838 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -07001839 int diff = state->has_refs - state->used_refs;
1840
1841 if (diff)
1842 fput_many(state->file, diff);
1843 state->file = NULL;
1844 }
1845}
1846
1847/*
1848 * Get as many references to a file as we have IOs left in this submission,
1849 * assuming most submissions are for one file, or at least that each file
1850 * has more than one submission.
1851 */
1852static struct file *io_file_get(struct io_submit_state *state, int fd)
1853{
1854 if (!state)
1855 return fget(fd);
1856
1857 if (state->file) {
1858 if (state->fd == fd) {
1859 state->used_refs++;
1860 state->ios_left--;
1861 return state->file;
1862 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06001863 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07001864 }
1865 state->file = fget_many(fd, state->ios_left);
1866 if (!state->file)
1867 return NULL;
1868
1869 state->fd = fd;
1870 state->has_refs = state->ios_left;
1871 state->used_refs = 1;
1872 state->ios_left--;
1873 return state->file;
1874}
1875
Jens Axboe2b188cc2019-01-07 10:46:33 -07001876/*
1877 * If we tracked the file through the SCM inflight mechanism, we could support
1878 * any file. For now, just ensure that anything potentially problematic is done
1879 * inline.
1880 */
1881static bool io_file_supports_async(struct file *file)
1882{
1883 umode_t mode = file_inode(file)->i_mode;
1884
Jens Axboe10d59342019-12-09 20:16:22 -07001885 if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07001886 return true;
1887 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
1888 return true;
1889
1890 return false;
1891}
1892
Jens Axboe3529d8c2019-12-19 18:24:38 -07001893static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1894 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001895{
Jens Axboedef596e2019-01-09 08:59:42 -07001896 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07001897 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06001898 unsigned ioprio;
1899 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001900
Jens Axboe491381ce2019-10-17 09:20:46 -06001901 if (S_ISREG(file_inode(req->file)->i_mode))
1902 req->flags |= REQ_F_ISREG;
1903
Jens Axboe2b188cc2019-01-07 10:46:33 -07001904 kiocb->ki_pos = READ_ONCE(sqe->off);
Jens Axboeba042912019-12-25 16:33:42 -07001905 if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) {
1906 req->flags |= REQ_F_CUR_POS;
1907 kiocb->ki_pos = req->file->f_pos;
1908 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001909 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03001910 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
1911 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
1912 if (unlikely(ret))
1913 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001914
1915 ioprio = READ_ONCE(sqe->ioprio);
1916 if (ioprio) {
1917 ret = ioprio_check_cap(ioprio);
1918 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06001919 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001920
1921 kiocb->ki_ioprio = ioprio;
1922 } else
1923 kiocb->ki_ioprio = get_current_ioprio();
1924
Stefan Bühler8449eed2019-04-27 20:34:19 +02001925 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06001926 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
1927 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02001928 req->flags |= REQ_F_NOWAIT;
1929
1930 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001931 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001932
Jens Axboedef596e2019-01-09 08:59:42 -07001933 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07001934 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
1935 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06001936 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001937
Jens Axboedef596e2019-01-09 08:59:42 -07001938 kiocb->ki_flags |= IOCB_HIPRI;
1939 kiocb->ki_complete = io_complete_rw_iopoll;
Jens Axboe6873e0b2019-10-30 13:53:09 -06001940 req->result = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001941 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001942 if (kiocb->ki_flags & IOCB_HIPRI)
1943 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07001944 kiocb->ki_complete = io_complete_rw;
1945 }
Jens Axboe9adbd452019-12-20 08:45:55 -07001946
Jens Axboe3529d8c2019-12-19 18:24:38 -07001947 req->rw.addr = READ_ONCE(sqe->addr);
1948 req->rw.len = READ_ONCE(sqe->len);
Jens Axboe9adbd452019-12-20 08:45:55 -07001949 /* we own ->private, reuse it for the buffer index */
1950 req->rw.kiocb.private = (void *) (unsigned long)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001951 READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001952 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001953}
1954
1955static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
1956{
1957 switch (ret) {
1958 case -EIOCBQUEUED:
1959 break;
1960 case -ERESTARTSYS:
1961 case -ERESTARTNOINTR:
1962 case -ERESTARTNOHAND:
1963 case -ERESTART_RESTARTBLOCK:
1964 /*
1965 * We can't just restart the syscall, since previously
1966 * submitted sqes may already be in progress. Just fail this
1967 * IO with EINTR.
1968 */
1969 ret = -EINTR;
1970 /* fall through */
1971 default:
1972 kiocb->ki_complete(kiocb, ret, 0);
1973 }
1974}
1975
Jens Axboeba816ad2019-09-28 11:36:45 -06001976static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt,
1977 bool in_async)
1978{
Jens Axboeba042912019-12-25 16:33:42 -07001979 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
1980
1981 if (req->flags & REQ_F_CUR_POS)
1982 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001983 if (in_async && ret >= 0 && kiocb->ki_complete == io_complete_rw)
Jens Axboeba816ad2019-09-28 11:36:45 -06001984 *nxt = __io_complete_rw(kiocb, ret);
1985 else
1986 io_rw_done(kiocb, ret);
1987}
1988
Jens Axboe9adbd452019-12-20 08:45:55 -07001989static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03001990 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07001991{
Jens Axboe9adbd452019-12-20 08:45:55 -07001992 struct io_ring_ctx *ctx = req->ctx;
1993 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001994 struct io_mapped_ubuf *imu;
1995 unsigned index, buf_index;
1996 size_t offset;
1997 u64 buf_addr;
1998
1999 /* attempt to use fixed buffers without having provided iovecs */
2000 if (unlikely(!ctx->user_bufs))
2001 return -EFAULT;
2002
Jens Axboe9adbd452019-12-20 08:45:55 -07002003 buf_index = (unsigned long) req->rw.kiocb.private;
Jens Axboeedafcce2019-01-09 09:16:05 -07002004 if (unlikely(buf_index >= ctx->nr_user_bufs))
2005 return -EFAULT;
2006
2007 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
2008 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07002009 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07002010
2011 /* overflow */
2012 if (buf_addr + len < buf_addr)
2013 return -EFAULT;
2014 /* not inside the mapped region */
2015 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
2016 return -EFAULT;
2017
2018 /*
2019 * May not be a start of buffer, set size appropriately
2020 * and advance us to the beginning.
2021 */
2022 offset = buf_addr - imu->ubuf;
2023 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06002024
2025 if (offset) {
2026 /*
2027 * Don't use iov_iter_advance() here, as it's really slow for
2028 * using the latter parts of a big fixed buffer - it iterates
2029 * over each segment manually. We can cheat a bit here, because
2030 * we know that:
2031 *
2032 * 1) it's a BVEC iter, we set it up
2033 * 2) all bvecs are PAGE_SIZE in size, except potentially the
2034 * first and last bvec
2035 *
2036 * So just find our index, and adjust the iterator afterwards.
2037 * If the offset is within the first bvec (or the whole first
2038 * bvec, just use iov_iter_advance(). This makes it easier
2039 * since we can just skip the first segment, which may not
2040 * be PAGE_SIZE aligned.
2041 */
2042 const struct bio_vec *bvec = imu->bvec;
2043
2044 if (offset <= bvec->bv_len) {
2045 iov_iter_advance(iter, offset);
2046 } else {
2047 unsigned long seg_skip;
2048
2049 /* skip first vec */
2050 offset -= bvec->bv_len;
2051 seg_skip = 1 + (offset >> PAGE_SHIFT);
2052
2053 iter->bvec = bvec + seg_skip;
2054 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02002055 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002056 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002057 }
2058 }
2059
Jens Axboe5e559562019-11-13 16:12:46 -07002060 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002061}
2062
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002063static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
2064 struct iovec **iovec, struct iov_iter *iter)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002065{
Jens Axboe9adbd452019-12-20 08:45:55 -07002066 void __user *buf = u64_to_user_ptr(req->rw.addr);
2067 size_t sqe_len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002068 u8 opcode;
2069
Jens Axboed625c6e2019-12-17 19:53:05 -07002070 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03002071 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07002072 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07002073 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07002074 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002075
Jens Axboe9adbd452019-12-20 08:45:55 -07002076 /* buffer index only valid with fixed read/write */
2077 if (req->rw.kiocb.private)
2078 return -EINVAL;
2079
Jens Axboe3a6820f2019-12-22 15:19:35 -07002080 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
2081 ssize_t ret;
2082 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
2083 *iovec = NULL;
Jens Axboe3a901592020-02-25 17:48:55 -07002084 return ret < 0 ? ret : sqe_len;
Jens Axboe3a6820f2019-12-22 15:19:35 -07002085 }
2086
Jens Axboef67676d2019-12-02 11:03:47 -07002087 if (req->io) {
2088 struct io_async_rw *iorw = &req->io->rw;
2089
2090 *iovec = iorw->iov;
2091 iov_iter_init(iter, rw, *iovec, iorw->nr_segs, iorw->size);
2092 if (iorw->iov == iorw->fast_iov)
2093 *iovec = NULL;
2094 return iorw->size;
2095 }
2096
Jens Axboe2b188cc2019-01-07 10:46:33 -07002097#ifdef CONFIG_COMPAT
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002098 if (req->ctx->compat)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002099 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
2100 iovec, iter);
2101#endif
2102
2103 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
2104}
2105
Jens Axboe32960612019-09-23 11:05:34 -06002106/*
2107 * For files that don't have ->read_iter() and ->write_iter(), handle them
2108 * by looping over ->read() or ->write() manually.
2109 */
2110static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
2111 struct iov_iter *iter)
2112{
2113 ssize_t ret = 0;
2114
2115 /*
2116 * Don't support polled IO through this interface, and we can't
2117 * support non-blocking either. For the latter, this just causes
2118 * the kiocb to be handled from an async context.
2119 */
2120 if (kiocb->ki_flags & IOCB_HIPRI)
2121 return -EOPNOTSUPP;
2122 if (kiocb->ki_flags & IOCB_NOWAIT)
2123 return -EAGAIN;
2124
2125 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002126 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06002127 ssize_t nr;
2128
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002129 if (!iov_iter_is_bvec(iter)) {
2130 iovec = iov_iter_iovec(iter);
2131 } else {
2132 /* fixed buffers import bvec */
2133 iovec.iov_base = kmap(iter->bvec->bv_page)
2134 + iter->iov_offset;
2135 iovec.iov_len = min(iter->count,
2136 iter->bvec->bv_len - iter->iov_offset);
2137 }
2138
Jens Axboe32960612019-09-23 11:05:34 -06002139 if (rw == READ) {
2140 nr = file->f_op->read(file, iovec.iov_base,
2141 iovec.iov_len, &kiocb->ki_pos);
2142 } else {
2143 nr = file->f_op->write(file, iovec.iov_base,
2144 iovec.iov_len, &kiocb->ki_pos);
2145 }
2146
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002147 if (iov_iter_is_bvec(iter))
2148 kunmap(iter->bvec->bv_page);
2149
Jens Axboe32960612019-09-23 11:05:34 -06002150 if (nr < 0) {
2151 if (!ret)
2152 ret = nr;
2153 break;
2154 }
2155 ret += nr;
2156 if (nr != iovec.iov_len)
2157 break;
2158 iov_iter_advance(iter, nr);
2159 }
2160
2161 return ret;
2162}
2163
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002164static void io_req_map_rw(struct io_kiocb *req, ssize_t io_size,
Jens Axboef67676d2019-12-02 11:03:47 -07002165 struct iovec *iovec, struct iovec *fast_iov,
2166 struct iov_iter *iter)
2167{
2168 req->io->rw.nr_segs = iter->nr_segs;
2169 req->io->rw.size = io_size;
2170 req->io->rw.iov = iovec;
2171 if (!req->io->rw.iov) {
2172 req->io->rw.iov = req->io->rw.fast_iov;
2173 memcpy(req->io->rw.iov, fast_iov,
2174 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002175 } else {
2176 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07002177 }
2178}
2179
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002180static int io_alloc_async_ctx(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07002181{
Jens Axboed3656342019-12-18 09:50:26 -07002182 if (!io_op_defs[req->opcode].async_ctx)
2183 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002184 req->io = kmalloc(sizeof(*req->io), GFP_KERNEL);
Jens Axboe06b76d42019-12-19 14:44:26 -07002185 return req->io == NULL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002186}
2187
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002188static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size,
2189 struct iovec *iovec, struct iovec *fast_iov,
2190 struct iov_iter *iter)
2191{
Jens Axboe980ad262020-01-24 23:08:54 -07002192 if (!io_op_defs[req->opcode].async_ctx)
Jens Axboe74566df2020-01-13 19:23:24 -07002193 return 0;
Jens Axboe5d204bc2020-01-31 12:06:52 -07002194 if (!req->io) {
2195 if (io_alloc_async_ctx(req))
2196 return -ENOMEM;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002197
Jens Axboe5d204bc2020-01-31 12:06:52 -07002198 io_req_map_rw(req, io_size, iovec, fast_iov, iter);
2199 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002200 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002201}
2202
Jens Axboe3529d8c2019-12-19 18:24:38 -07002203static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2204 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002205{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002206 struct io_async_ctx *io;
2207 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002208 ssize_t ret;
2209
Jens Axboe3529d8c2019-12-19 18:24:38 -07002210 ret = io_prep_rw(req, sqe, force_nonblock);
2211 if (ret)
2212 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002213
Jens Axboe3529d8c2019-12-19 18:24:38 -07002214 if (unlikely(!(req->file->f_mode & FMODE_READ)))
2215 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002216
Pavel Begunkov5f798be2020-02-08 13:28:02 +03002217 /* either don't need iovec imported or already have it */
2218 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002219 return 0;
2220
2221 io = req->io;
2222 io->rw.iov = io->rw.fast_iov;
2223 req->io = NULL;
2224 ret = io_import_iovec(READ, req, &io->rw.iov, &iter);
2225 req->io = io;
2226 if (ret < 0)
2227 return ret;
2228
2229 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2230 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002231}
2232
Pavel Begunkov267bc902019-11-07 01:41:08 +03002233static int io_read(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06002234 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002235{
2236 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002237 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002238 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002239 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002240 ssize_t io_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002241
Jens Axboe3529d8c2019-12-19 18:24:38 -07002242 ret = io_import_iovec(READ, req, &iovec, &iter);
Jens Axboe06b76d42019-12-19 14:44:26 -07002243 if (ret < 0)
2244 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002245
Jens Axboefd6c2e42019-12-18 12:19:41 -07002246 /* Ensure we clear previously set non-block flag */
2247 if (!force_nonblock)
Jens Axboe29de5f62020-02-20 09:56:08 -07002248 kiocb->ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002249
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002250 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002251 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06002252 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07002253 req->result = io_size;
2254
2255 /*
2256 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2257 * we know to async punt it even if it was opened O_NONBLOCK
2258 */
Jens Axboe29de5f62020-02-20 09:56:08 -07002259 if (force_nonblock && !io_file_supports_async(req->file))
Jens Axboef67676d2019-12-02 11:03:47 -07002260 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06002261
Jens Axboe31b51512019-01-18 22:56:34 -07002262 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002263 ret = rw_verify_area(READ, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002264 if (!ret) {
2265 ssize_t ret2;
2266
Jens Axboe9adbd452019-12-20 08:45:55 -07002267 if (req->file->f_op->read_iter)
2268 ret2 = call_read_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002269 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002270 ret2 = loop_rw_iter(READ, req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002271
Jens Axboe9d93a3f2019-05-15 13:53:07 -06002272 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboef67676d2019-12-02 11:03:47 -07002273 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002274 kiocb_done(kiocb, ret2, nxt, req->in_async);
Jens Axboef67676d2019-12-02 11:03:47 -07002275 } else {
2276copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002277 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002278 inline_vecs, &iter);
2279 if (ret)
2280 goto out_free;
Jens Axboe29de5f62020-02-20 09:56:08 -07002281 /* any defer here is final, must blocking retry */
2282 if (!(req->flags & REQ_F_NOWAIT))
2283 req->flags |= REQ_F_MUST_PUNT;
Jens Axboef67676d2019-12-02 11:03:47 -07002284 return -EAGAIN;
2285 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002286 }
Jens Axboef67676d2019-12-02 11:03:47 -07002287out_free:
Pavel Begunkov1e950812020-02-06 19:51:16 +03002288 kfree(iovec);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002289 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002290 return ret;
2291}
2292
Jens Axboe3529d8c2019-12-19 18:24:38 -07002293static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2294 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002295{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002296 struct io_async_ctx *io;
2297 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002298 ssize_t ret;
2299
Jens Axboe3529d8c2019-12-19 18:24:38 -07002300 ret = io_prep_rw(req, sqe, force_nonblock);
2301 if (ret)
2302 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002303
Jens Axboe3529d8c2019-12-19 18:24:38 -07002304 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
2305 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002306
Pavel Begunkov5f798be2020-02-08 13:28:02 +03002307 /* either don't need iovec imported or already have it */
2308 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002309 return 0;
2310
2311 io = req->io;
2312 io->rw.iov = io->rw.fast_iov;
2313 req->io = NULL;
2314 ret = io_import_iovec(WRITE, req, &io->rw.iov, &iter);
2315 req->io = io;
2316 if (ret < 0)
2317 return ret;
2318
2319 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2320 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002321}
2322
Pavel Begunkov267bc902019-11-07 01:41:08 +03002323static int io_write(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06002324 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002325{
2326 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002327 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002328 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002329 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002330 ssize_t ret, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002331
Jens Axboe3529d8c2019-12-19 18:24:38 -07002332 ret = io_import_iovec(WRITE, req, &iovec, &iter);
Jens Axboe06b76d42019-12-19 14:44:26 -07002333 if (ret < 0)
2334 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002335
Jens Axboefd6c2e42019-12-18 12:19:41 -07002336 /* Ensure we clear previously set non-block flag */
2337 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07002338 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002339
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002340 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002341 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06002342 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07002343 req->result = io_size;
2344
2345 /*
2346 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2347 * we know to async punt it even if it was opened O_NONBLOCK
2348 */
Jens Axboe29de5f62020-02-20 09:56:08 -07002349 if (force_nonblock && !io_file_supports_async(req->file))
Jens Axboef67676d2019-12-02 11:03:47 -07002350 goto copy_iov;
Jens Axboef67676d2019-12-02 11:03:47 -07002351
Jens Axboe10d59342019-12-09 20:16:22 -07002352 /* file path doesn't support NOWAIT for non-direct_IO */
2353 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
2354 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07002355 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06002356
Jens Axboe31b51512019-01-18 22:56:34 -07002357 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002358 ret = rw_verify_area(WRITE, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002359 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01002360 ssize_t ret2;
2361
Jens Axboe2b188cc2019-01-07 10:46:33 -07002362 /*
2363 * Open-code file_start_write here to grab freeze protection,
2364 * which will be released by another thread in
2365 * io_complete_rw(). Fool lockdep by telling it the lock got
2366 * released so that it doesn't complain about the held lock when
2367 * we return to userspace.
2368 */
Jens Axboe491381ce2019-10-17 09:20:46 -06002369 if (req->flags & REQ_F_ISREG) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002370 __sb_start_write(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002371 SB_FREEZE_WRITE, true);
Jens Axboe9adbd452019-12-20 08:45:55 -07002372 __sb_writers_release(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002373 SB_FREEZE_WRITE);
2374 }
2375 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01002376
Jens Axboe9adbd452019-12-20 08:45:55 -07002377 if (req->file->f_op->write_iter)
2378 ret2 = call_write_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002379 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002380 ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter);
Jens Axboefaac9962020-02-07 15:45:22 -07002381 /*
2382 * Raw bdev writes will -EOPNOTSUPP for IOCB_NOWAIT. Just
2383 * retry them without IOCB_NOWAIT.
2384 */
2385 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
2386 ret2 = -EAGAIN;
Jens Axboef67676d2019-12-02 11:03:47 -07002387 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002388 kiocb_done(kiocb, ret2, nxt, req->in_async);
Jens Axboef67676d2019-12-02 11:03:47 -07002389 } else {
2390copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002391 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002392 inline_vecs, &iter);
2393 if (ret)
2394 goto out_free;
Jens Axboe29de5f62020-02-20 09:56:08 -07002395 /* any defer here is final, must blocking retry */
2396 req->flags |= REQ_F_MUST_PUNT;
Jens Axboef67676d2019-12-02 11:03:47 -07002397 return -EAGAIN;
2398 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002399 }
Jens Axboe31b51512019-01-18 22:56:34 -07002400out_free:
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002401 req->flags &= ~REQ_F_NEED_CLEANUP;
Pavel Begunkov1e950812020-02-06 19:51:16 +03002402 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002403 return ret;
2404}
2405
2406/*
2407 * IORING_OP_NOP just posts a completion event, nothing else.
2408 */
Jens Axboe78e19bb2019-11-06 15:21:34 -07002409static int io_nop(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002410{
2411 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002412
Jens Axboedef596e2019-01-09 08:59:42 -07002413 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2414 return -EINVAL;
2415
Jens Axboe78e19bb2019-11-06 15:21:34 -07002416 io_cqring_add_event(req, 0);
Jens Axboee65ef562019-03-12 10:16:44 -06002417 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002418 return 0;
2419}
2420
Jens Axboe3529d8c2019-12-19 18:24:38 -07002421static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002422{
Jens Axboe6b063142019-01-10 22:13:58 -07002423 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002424
Jens Axboe09bb8392019-03-13 12:39:28 -06002425 if (!req->file)
2426 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002427
Jens Axboe6b063142019-01-10 22:13:58 -07002428 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07002429 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07002430 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002431 return -EINVAL;
2432
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002433 req->sync.flags = READ_ONCE(sqe->fsync_flags);
2434 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
2435 return -EINVAL;
2436
2437 req->sync.off = READ_ONCE(sqe->off);
2438 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002439 return 0;
2440}
2441
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002442static bool io_req_cancelled(struct io_kiocb *req)
2443{
2444 if (req->work.flags & IO_WQ_WORK_CANCEL) {
2445 req_set_fail_links(req);
2446 io_cqring_add_event(req, -ECANCELED);
2447 io_put_req(req);
2448 return true;
2449 }
2450
2451 return false;
2452}
2453
Jens Axboe78912932020-01-14 22:09:06 -07002454static void io_link_work_cb(struct io_wq_work **workptr)
2455{
2456 struct io_wq_work *work = *workptr;
2457 struct io_kiocb *link = work->data;
2458
2459 io_queue_linked_timeout(link);
2460 work->func = io_wq_submit_work;
2461}
2462
2463static void io_wq_assign_next(struct io_wq_work **workptr, struct io_kiocb *nxt)
2464{
2465 struct io_kiocb *link;
2466
Pavel Begunkovdeb6dc02020-02-24 11:30:17 +03002467 io_prep_next_work(nxt, &link);
Jens Axboe78912932020-01-14 22:09:06 -07002468 *workptr = &nxt->work;
2469 if (link) {
2470 nxt->work.flags |= IO_WQ_WORK_CB;
2471 nxt->work.func = io_link_work_cb;
2472 nxt->work.data = link;
2473 }
2474}
2475
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002476static void __io_fsync(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002477{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002478 loff_t end = req->sync.off + req->sync.len;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002479 int ret;
2480
Jens Axboe9adbd452019-12-20 08:45:55 -07002481 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002482 end > 0 ? end : LLONG_MAX,
2483 req->sync.flags & IORING_FSYNC_DATASYNC);
2484 if (ret < 0)
2485 req_set_fail_links(req);
2486 io_cqring_add_event(req, ret);
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002487 io_put_req_find_next(req, nxt);
2488}
2489
2490static void io_fsync_finish(struct io_wq_work **workptr)
2491{
2492 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2493 struct io_kiocb *nxt = NULL;
2494
2495 if (io_req_cancelled(req))
2496 return;
2497 __io_fsync(req, &nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002498 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07002499 io_wq_assign_next(workptr, nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002500}
2501
Jens Axboefc4df992019-12-10 14:38:45 -07002502static int io_fsync(struct io_kiocb *req, struct io_kiocb **nxt,
2503 bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002504{
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002505 /* fsync always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002506 if (force_nonblock) {
2507 io_put_req(req);
2508 req->work.func = io_fsync_finish;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002509 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002510 }
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002511 __io_fsync(req, nxt);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002512 return 0;
2513}
2514
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002515static void __io_fallocate(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboed63d1b52019-12-10 10:38:56 -07002516{
Jens Axboed63d1b52019-12-10 10:38:56 -07002517 int ret;
2518
Pavel Begunkov7fbeb952020-02-16 01:01:18 +03002519 if (io_req_cancelled(req))
2520 return;
2521
Jens Axboed63d1b52019-12-10 10:38:56 -07002522 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
2523 req->sync.len);
2524 if (ret < 0)
2525 req_set_fail_links(req);
2526 io_cqring_add_event(req, ret);
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002527 io_put_req_find_next(req, nxt);
2528}
2529
2530static void io_fallocate_finish(struct io_wq_work **workptr)
2531{
2532 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2533 struct io_kiocb *nxt = NULL;
2534
2535 __io_fallocate(req, &nxt);
Jens Axboed63d1b52019-12-10 10:38:56 -07002536 if (nxt)
2537 io_wq_assign_next(workptr, nxt);
2538}
2539
2540static int io_fallocate_prep(struct io_kiocb *req,
2541 const struct io_uring_sqe *sqe)
2542{
2543 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
2544 return -EINVAL;
2545
2546 req->sync.off = READ_ONCE(sqe->off);
2547 req->sync.len = READ_ONCE(sqe->addr);
2548 req->sync.mode = READ_ONCE(sqe->len);
2549 return 0;
2550}
2551
2552static int io_fallocate(struct io_kiocb *req, struct io_kiocb **nxt,
2553 bool force_nonblock)
2554{
Jens Axboed63d1b52019-12-10 10:38:56 -07002555 /* fallocate always requiring blocking context */
2556 if (force_nonblock) {
2557 io_put_req(req);
2558 req->work.func = io_fallocate_finish;
2559 return -EAGAIN;
2560 }
2561
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002562 __io_fallocate(req, nxt);
Jens Axboed63d1b52019-12-10 10:38:56 -07002563 return 0;
2564}
2565
Jens Axboe15b71ab2019-12-11 11:20:36 -07002566static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2567{
Jens Axboef8748882020-01-08 17:47:02 -07002568 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002569 int ret;
2570
2571 if (sqe->ioprio || sqe->buf_index)
2572 return -EINVAL;
Jens Axboecf3040c2020-02-06 21:31:40 -07002573 if (sqe->flags & IOSQE_FIXED_FILE)
2574 return -EBADF;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03002575 if (req->flags & REQ_F_NEED_CLEANUP)
2576 return 0;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002577
2578 req->open.dfd = READ_ONCE(sqe->fd);
Jens Axboec12cedf2020-01-08 17:41:21 -07002579 req->open.how.mode = READ_ONCE(sqe->len);
Jens Axboef8748882020-01-08 17:47:02 -07002580 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboec12cedf2020-01-08 17:41:21 -07002581 req->open.how.flags = READ_ONCE(sqe->open_flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002582
Jens Axboef8748882020-01-08 17:47:02 -07002583 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002584 if (IS_ERR(req->open.filename)) {
2585 ret = PTR_ERR(req->open.filename);
2586 req->open.filename = NULL;
2587 return ret;
2588 }
2589
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002590 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002591 return 0;
2592}
2593
Jens Axboecebdb982020-01-08 17:59:24 -07002594static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2595{
2596 struct open_how __user *how;
2597 const char __user *fname;
2598 size_t len;
2599 int ret;
2600
2601 if (sqe->ioprio || sqe->buf_index)
2602 return -EINVAL;
Jens Axboecf3040c2020-02-06 21:31:40 -07002603 if (sqe->flags & IOSQE_FIXED_FILE)
2604 return -EBADF;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03002605 if (req->flags & REQ_F_NEED_CLEANUP)
2606 return 0;
Jens Axboecebdb982020-01-08 17:59:24 -07002607
2608 req->open.dfd = READ_ONCE(sqe->fd);
2609 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
2610 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
2611 len = READ_ONCE(sqe->len);
2612
2613 if (len < OPEN_HOW_SIZE_VER0)
2614 return -EINVAL;
2615
2616 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
2617 len);
2618 if (ret)
2619 return ret;
2620
2621 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
2622 req->open.how.flags |= O_LARGEFILE;
2623
2624 req->open.filename = getname(fname);
2625 if (IS_ERR(req->open.filename)) {
2626 ret = PTR_ERR(req->open.filename);
2627 req->open.filename = NULL;
2628 return ret;
2629 }
2630
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002631 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboecebdb982020-01-08 17:59:24 -07002632 return 0;
2633}
2634
2635static int io_openat2(struct io_kiocb *req, struct io_kiocb **nxt,
2636 bool force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07002637{
2638 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002639 struct file *file;
2640 int ret;
2641
Jens Axboef86cd202020-01-29 13:46:44 -07002642 if (force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07002643 return -EAGAIN;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002644
Jens Axboecebdb982020-01-08 17:59:24 -07002645 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002646 if (ret)
2647 goto err;
2648
Jens Axboecebdb982020-01-08 17:59:24 -07002649 ret = get_unused_fd_flags(req->open.how.flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002650 if (ret < 0)
2651 goto err;
2652
2653 file = do_filp_open(req->open.dfd, req->open.filename, &op);
2654 if (IS_ERR(file)) {
2655 put_unused_fd(ret);
2656 ret = PTR_ERR(file);
2657 } else {
2658 fsnotify_open(file);
2659 fd_install(ret, file);
2660 }
2661err:
2662 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002663 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002664 if (ret < 0)
2665 req_set_fail_links(req);
2666 io_cqring_add_event(req, ret);
2667 io_put_req_find_next(req, nxt);
2668 return 0;
2669}
2670
Jens Axboecebdb982020-01-08 17:59:24 -07002671static int io_openat(struct io_kiocb *req, struct io_kiocb **nxt,
2672 bool force_nonblock)
2673{
2674 req->open.how = build_open_how(req->open.how.flags, req->open.how.mode);
2675 return io_openat2(req, nxt, force_nonblock);
2676}
2677
Jens Axboe3e4827b2020-01-08 15:18:09 -07002678static int io_epoll_ctl_prep(struct io_kiocb *req,
2679 const struct io_uring_sqe *sqe)
2680{
2681#if defined(CONFIG_EPOLL)
2682 if (sqe->ioprio || sqe->buf_index)
2683 return -EINVAL;
2684
2685 req->epoll.epfd = READ_ONCE(sqe->fd);
2686 req->epoll.op = READ_ONCE(sqe->len);
2687 req->epoll.fd = READ_ONCE(sqe->off);
2688
2689 if (ep_op_has_event(req->epoll.op)) {
2690 struct epoll_event __user *ev;
2691
2692 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
2693 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
2694 return -EFAULT;
2695 }
2696
2697 return 0;
2698#else
2699 return -EOPNOTSUPP;
2700#endif
2701}
2702
2703static int io_epoll_ctl(struct io_kiocb *req, struct io_kiocb **nxt,
2704 bool force_nonblock)
2705{
2706#if defined(CONFIG_EPOLL)
2707 struct io_epoll *ie = &req->epoll;
2708 int ret;
2709
2710 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
2711 if (force_nonblock && ret == -EAGAIN)
2712 return -EAGAIN;
2713
2714 if (ret < 0)
2715 req_set_fail_links(req);
2716 io_cqring_add_event(req, ret);
2717 io_put_req_find_next(req, nxt);
2718 return 0;
2719#else
2720 return -EOPNOTSUPP;
2721#endif
2722}
2723
Jens Axboec1ca7572019-12-25 22:18:28 -07002724static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2725{
2726#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
2727 if (sqe->ioprio || sqe->buf_index || sqe->off)
2728 return -EINVAL;
2729
2730 req->madvise.addr = READ_ONCE(sqe->addr);
2731 req->madvise.len = READ_ONCE(sqe->len);
2732 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
2733 return 0;
2734#else
2735 return -EOPNOTSUPP;
2736#endif
2737}
2738
2739static int io_madvise(struct io_kiocb *req, struct io_kiocb **nxt,
2740 bool force_nonblock)
2741{
2742#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
2743 struct io_madvise *ma = &req->madvise;
2744 int ret;
2745
2746 if (force_nonblock)
2747 return -EAGAIN;
2748
2749 ret = do_madvise(ma->addr, ma->len, ma->advice);
2750 if (ret < 0)
2751 req_set_fail_links(req);
2752 io_cqring_add_event(req, ret);
2753 io_put_req_find_next(req, nxt);
2754 return 0;
2755#else
2756 return -EOPNOTSUPP;
2757#endif
2758}
2759
Jens Axboe4840e412019-12-25 22:03:45 -07002760static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2761{
2762 if (sqe->ioprio || sqe->buf_index || sqe->addr)
2763 return -EINVAL;
2764
2765 req->fadvise.offset = READ_ONCE(sqe->off);
2766 req->fadvise.len = READ_ONCE(sqe->len);
2767 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
2768 return 0;
2769}
2770
2771static int io_fadvise(struct io_kiocb *req, struct io_kiocb **nxt,
2772 bool force_nonblock)
2773{
2774 struct io_fadvise *fa = &req->fadvise;
2775 int ret;
2776
Jens Axboe3e694262020-02-01 09:22:49 -07002777 if (force_nonblock) {
2778 switch (fa->advice) {
2779 case POSIX_FADV_NORMAL:
2780 case POSIX_FADV_RANDOM:
2781 case POSIX_FADV_SEQUENTIAL:
2782 break;
2783 default:
2784 return -EAGAIN;
2785 }
2786 }
Jens Axboe4840e412019-12-25 22:03:45 -07002787
2788 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
2789 if (ret < 0)
2790 req_set_fail_links(req);
2791 io_cqring_add_event(req, ret);
2792 io_put_req_find_next(req, nxt);
2793 return 0;
2794}
2795
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002796static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2797{
Jens Axboef8748882020-01-08 17:47:02 -07002798 const char __user *fname;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002799 unsigned lookup_flags;
2800 int ret;
2801
2802 if (sqe->ioprio || sqe->buf_index)
2803 return -EINVAL;
Jens Axboecf3040c2020-02-06 21:31:40 -07002804 if (sqe->flags & IOSQE_FIXED_FILE)
2805 return -EBADF;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03002806 if (req->flags & REQ_F_NEED_CLEANUP)
2807 return 0;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002808
2809 req->open.dfd = READ_ONCE(sqe->fd);
2810 req->open.mask = READ_ONCE(sqe->len);
Jens Axboef8748882020-01-08 17:47:02 -07002811 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002812 req->open.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboec12cedf2020-01-08 17:41:21 -07002813 req->open.how.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002814
Jens Axboec12cedf2020-01-08 17:41:21 -07002815 if (vfs_stat_set_lookup_flags(&lookup_flags, req->open.how.flags))
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002816 return -EINVAL;
2817
Jens Axboef8748882020-01-08 17:47:02 -07002818 req->open.filename = getname_flags(fname, lookup_flags, NULL);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002819 if (IS_ERR(req->open.filename)) {
2820 ret = PTR_ERR(req->open.filename);
2821 req->open.filename = NULL;
2822 return ret;
2823 }
2824
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002825 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002826 return 0;
2827}
2828
2829static int io_statx(struct io_kiocb *req, struct io_kiocb **nxt,
2830 bool force_nonblock)
2831{
2832 struct io_open *ctx = &req->open;
2833 unsigned lookup_flags;
2834 struct path path;
2835 struct kstat stat;
2836 int ret;
2837
2838 if (force_nonblock)
2839 return -EAGAIN;
2840
Jens Axboec12cedf2020-01-08 17:41:21 -07002841 if (vfs_stat_set_lookup_flags(&lookup_flags, ctx->how.flags))
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002842 return -EINVAL;
2843
2844retry:
2845 /* filename_lookup() drops it, keep a reference */
2846 ctx->filename->refcnt++;
2847
2848 ret = filename_lookup(ctx->dfd, ctx->filename, lookup_flags, &path,
2849 NULL);
2850 if (ret)
2851 goto err;
2852
Jens Axboec12cedf2020-01-08 17:41:21 -07002853 ret = vfs_getattr(&path, &stat, ctx->mask, ctx->how.flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002854 path_put(&path);
2855 if (retry_estale(ret, lookup_flags)) {
2856 lookup_flags |= LOOKUP_REVAL;
2857 goto retry;
2858 }
2859 if (!ret)
2860 ret = cp_statx(&stat, ctx->buffer);
2861err:
2862 putname(ctx->filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002863 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002864 if (ret < 0)
2865 req_set_fail_links(req);
2866 io_cqring_add_event(req, ret);
2867 io_put_req_find_next(req, nxt);
2868 return 0;
2869}
2870
Jens Axboeb5dba592019-12-11 14:02:38 -07002871static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2872{
2873 /*
2874 * If we queue this for async, it must not be cancellable. That would
2875 * leave the 'file' in an undeterminate state.
2876 */
2877 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
2878
2879 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
2880 sqe->rw_flags || sqe->buf_index)
2881 return -EINVAL;
2882 if (sqe->flags & IOSQE_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07002883 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07002884
2885 req->close.fd = READ_ONCE(sqe->fd);
2886 if (req->file->f_op == &io_uring_fops ||
Pavel Begunkovb14cca02020-01-17 04:45:59 +03002887 req->close.fd == req->ctx->ring_fd)
Jens Axboeb5dba592019-12-11 14:02:38 -07002888 return -EBADF;
2889
2890 return 0;
2891}
2892
Pavel Begunkova93b3332020-02-08 14:04:34 +03002893/* only called when __close_fd_get_file() is done */
2894static void __io_close_finish(struct io_kiocb *req, struct io_kiocb **nxt)
2895{
2896 int ret;
2897
2898 ret = filp_close(req->close.put_file, req->work.files);
2899 if (ret < 0)
2900 req_set_fail_links(req);
2901 io_cqring_add_event(req, ret);
2902 fput(req->close.put_file);
2903 io_put_req_find_next(req, nxt);
2904}
2905
Jens Axboeb5dba592019-12-11 14:02:38 -07002906static void io_close_finish(struct io_wq_work **workptr)
2907{
2908 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2909 struct io_kiocb *nxt = NULL;
2910
Pavel Begunkov7fbeb952020-02-16 01:01:18 +03002911 /* not cancellable, don't do io_req_cancelled() */
Pavel Begunkova93b3332020-02-08 14:04:34 +03002912 __io_close_finish(req, &nxt);
Jens Axboeb5dba592019-12-11 14:02:38 -07002913 if (nxt)
2914 io_wq_assign_next(workptr, nxt);
2915}
2916
2917static int io_close(struct io_kiocb *req, struct io_kiocb **nxt,
2918 bool force_nonblock)
2919{
2920 int ret;
2921
2922 req->close.put_file = NULL;
2923 ret = __close_fd_get_file(req->close.fd, &req->close.put_file);
2924 if (ret < 0)
2925 return ret;
2926
2927 /* if the file has a flush method, be safe and punt to async */
Jens Axboef86cd202020-01-29 13:46:44 -07002928 if (req->close.put_file->f_op->flush && !io_wq_current_is_worker())
Jens Axboeb5dba592019-12-11 14:02:38 -07002929 goto eagain;
Jens Axboeb5dba592019-12-11 14:02:38 -07002930
2931 /*
2932 * No ->flush(), safely close from here and just punt the
2933 * fput() to async context.
2934 */
Pavel Begunkova93b3332020-02-08 14:04:34 +03002935 __io_close_finish(req, nxt);
2936 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07002937eagain:
2938 req->work.func = io_close_finish;
Jens Axboe1a417f42020-01-31 17:16:48 -07002939 /*
2940 * Do manual async queue here to avoid grabbing files - we don't
2941 * need the files, and it'll cause io_close_finish() to close
2942 * the file again and cause a double CQE entry for this request
2943 */
2944 io_queue_async_work(req);
2945 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07002946}
2947
Jens Axboe3529d8c2019-12-19 18:24:38 -07002948static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002949{
2950 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002951
2952 if (!req->file)
2953 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002954
2955 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2956 return -EINVAL;
2957 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
2958 return -EINVAL;
2959
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002960 req->sync.off = READ_ONCE(sqe->off);
2961 req->sync.len = READ_ONCE(sqe->len);
2962 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002963 return 0;
2964}
2965
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002966static void __io_sync_file_range(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002967{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002968 int ret;
2969
Jens Axboe9adbd452019-12-20 08:45:55 -07002970 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002971 req->sync.flags);
2972 if (ret < 0)
2973 req_set_fail_links(req);
2974 io_cqring_add_event(req, ret);
Pavel Begunkov5ea62162020-02-24 11:30:16 +03002975 io_put_req_find_next(req, nxt);
2976}
2977
2978
2979static void io_sync_file_range_finish(struct io_wq_work **workptr)
2980{
2981 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2982 struct io_kiocb *nxt = NULL;
2983
2984 if (io_req_cancelled(req))
2985 return;
2986 __io_sync_file_range(req, &nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002987 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07002988 io_wq_assign_next(workptr, nxt);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002989}
2990
Jens Axboefc4df992019-12-10 14:38:45 -07002991static int io_sync_file_range(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002992 bool force_nonblock)
2993{
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002994 /* sync_file_range always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002995 if (force_nonblock) {
2996 io_put_req(req);
2997 req->work.func = io_sync_file_range_finish;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002998 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002999 }
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003000
Pavel Begunkov5ea62162020-02-24 11:30:16 +03003001 __io_sync_file_range(req, nxt);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06003002 return 0;
3003}
3004
Jens Axboe3529d8c2019-12-19 18:24:38 -07003005static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06003006{
Jens Axboe03b12302019-12-02 18:50:25 -07003007#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07003008 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003009 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003010 int ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003011
Jens Axboee47293f2019-12-20 08:58:21 -07003012 sr->msg_flags = READ_ONCE(sqe->msg_flags);
3013 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07003014 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003015
Jens Axboed8768362020-02-27 14:17:49 -07003016#ifdef CONFIG_COMPAT
3017 if (req->ctx->compat)
3018 sr->msg_flags |= MSG_CMSG_COMPAT;
3019#endif
3020
Jens Axboefddafac2020-01-04 20:19:44 -07003021 if (!io || req->opcode == IORING_OP_SEND)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003022 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003023 /* iovec is already imported */
3024 if (req->flags & REQ_F_NEED_CLEANUP)
3025 return 0;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003026
Jens Axboed9688562019-12-09 19:35:20 -07003027 io->msg.iov = io->msg.fast_iov;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003028 ret = sendmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07003029 &io->msg.iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003030 if (!ret)
3031 req->flags |= REQ_F_NEED_CLEANUP;
3032 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003033#else
Jens Axboee47293f2019-12-20 08:58:21 -07003034 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07003035#endif
3036}
3037
Jens Axboefc4df992019-12-10 14:38:45 -07003038static int io_sendmsg(struct io_kiocb *req, struct io_kiocb **nxt,
3039 bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07003040{
3041#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07003042 struct io_async_msghdr *kmsg = NULL;
Jens Axboe03b12302019-12-02 18:50:25 -07003043 struct socket *sock;
3044 int ret;
3045
3046 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3047 return -EINVAL;
3048
3049 sock = sock_from_file(req->file, &ret);
3050 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003051 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07003052 unsigned flags;
3053
Jens Axboe03b12302019-12-02 18:50:25 -07003054 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07003055 kmsg = &req->io->msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003056 kmsg->msg.msg_name = &req->io->msg.addr;
Jens Axboe0b416c32019-12-15 10:57:46 -07003057 /* if iov is set, it's allocated already */
3058 if (!kmsg->iov)
3059 kmsg->iov = kmsg->fast_iov;
3060 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07003061 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003062 struct io_sr_msg *sr = &req->sr_msg;
3063
Jens Axboe0b416c32019-12-15 10:57:46 -07003064 kmsg = &io.msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003065 kmsg->msg.msg_name = &io.msg.addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003066
3067 io.msg.iov = io.msg.fast_iov;
3068 ret = sendmsg_copy_msghdr(&io.msg.msg, sr->msg,
3069 sr->msg_flags, &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07003070 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003071 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003072 }
3073
Jens Axboee47293f2019-12-20 08:58:21 -07003074 flags = req->sr_msg.msg_flags;
3075 if (flags & MSG_DONTWAIT)
3076 req->flags |= REQ_F_NOWAIT;
3077 else if (force_nonblock)
3078 flags |= MSG_DONTWAIT;
3079
Jens Axboe0b416c32019-12-15 10:57:46 -07003080 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Jens Axboe03b12302019-12-02 18:50:25 -07003081 if (force_nonblock && ret == -EAGAIN) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003082 if (req->io)
3083 return -EAGAIN;
Pavel Begunkov1e950812020-02-06 19:51:16 +03003084 if (io_alloc_async_ctx(req)) {
Dan Carpenter297a31e2020-02-17 17:39:45 +03003085 if (kmsg->iov != kmsg->fast_iov)
Pavel Begunkov1e950812020-02-06 19:51:16 +03003086 kfree(kmsg->iov);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003087 return -ENOMEM;
Pavel Begunkov1e950812020-02-06 19:51:16 +03003088 }
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003089 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003090 memcpy(&req->io->msg, &io.msg, sizeof(io.msg));
Jens Axboe0b416c32019-12-15 10:57:46 -07003091 return -EAGAIN;
Jens Axboe03b12302019-12-02 18:50:25 -07003092 }
3093 if (ret == -ERESTARTSYS)
3094 ret = -EINTR;
3095 }
3096
Pavel Begunkov1e950812020-02-06 19:51:16 +03003097 if (kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07003098 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003099 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe03b12302019-12-02 18:50:25 -07003100 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003101 if (ret < 0)
3102 req_set_fail_links(req);
Jens Axboe03b12302019-12-02 18:50:25 -07003103 io_put_req_find_next(req, nxt);
3104 return 0;
3105#else
3106 return -EOPNOTSUPP;
3107#endif
3108}
3109
Jens Axboefddafac2020-01-04 20:19:44 -07003110static int io_send(struct io_kiocb *req, struct io_kiocb **nxt,
3111 bool force_nonblock)
3112{
3113#if defined(CONFIG_NET)
3114 struct socket *sock;
3115 int ret;
3116
3117 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3118 return -EINVAL;
3119
3120 sock = sock_from_file(req->file, &ret);
3121 if (sock) {
3122 struct io_sr_msg *sr = &req->sr_msg;
3123 struct msghdr msg;
3124 struct iovec iov;
3125 unsigned flags;
3126
3127 ret = import_single_range(WRITE, sr->buf, sr->len, &iov,
3128 &msg.msg_iter);
3129 if (ret)
3130 return ret;
3131
3132 msg.msg_name = NULL;
3133 msg.msg_control = NULL;
3134 msg.msg_controllen = 0;
3135 msg.msg_namelen = 0;
3136
3137 flags = req->sr_msg.msg_flags;
3138 if (flags & MSG_DONTWAIT)
3139 req->flags |= REQ_F_NOWAIT;
3140 else if (force_nonblock)
3141 flags |= MSG_DONTWAIT;
3142
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003143 msg.msg_flags = flags;
3144 ret = sock_sendmsg(sock, &msg);
Jens Axboefddafac2020-01-04 20:19:44 -07003145 if (force_nonblock && ret == -EAGAIN)
3146 return -EAGAIN;
3147 if (ret == -ERESTARTSYS)
3148 ret = -EINTR;
3149 }
3150
3151 io_cqring_add_event(req, ret);
3152 if (ret < 0)
3153 req_set_fail_links(req);
3154 io_put_req_find_next(req, nxt);
3155 return 0;
3156#else
3157 return -EOPNOTSUPP;
3158#endif
3159}
3160
Jens Axboe3529d8c2019-12-19 18:24:38 -07003161static int io_recvmsg_prep(struct io_kiocb *req,
3162 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07003163{
3164#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07003165 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003166 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003167 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07003168
Jens Axboe3529d8c2019-12-19 18:24:38 -07003169 sr->msg_flags = READ_ONCE(sqe->msg_flags);
3170 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003171 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003172
Jens Axboed8768362020-02-27 14:17:49 -07003173#ifdef CONFIG_COMPAT
3174 if (req->ctx->compat)
3175 sr->msg_flags |= MSG_CMSG_COMPAT;
3176#endif
3177
Jens Axboefddafac2020-01-04 20:19:44 -07003178 if (!io || req->opcode == IORING_OP_RECV)
Jens Axboe06b76d42019-12-19 14:44:26 -07003179 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003180 /* iovec is already imported */
3181 if (req->flags & REQ_F_NEED_CLEANUP)
3182 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07003183
Jens Axboed9688562019-12-09 19:35:20 -07003184 io->msg.iov = io->msg.fast_iov;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003185 ret = recvmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07003186 &io->msg.uaddr, &io->msg.iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003187 if (!ret)
3188 req->flags |= REQ_F_NEED_CLEANUP;
3189 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003190#else
Jens Axboee47293f2019-12-20 08:58:21 -07003191 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07003192#endif
3193}
3194
Jens Axboefc4df992019-12-10 14:38:45 -07003195static int io_recvmsg(struct io_kiocb *req, struct io_kiocb **nxt,
3196 bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07003197{
3198#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07003199 struct io_async_msghdr *kmsg = NULL;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003200 struct socket *sock;
3201 int ret;
3202
3203 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3204 return -EINVAL;
3205
3206 sock = sock_from_file(req->file, &ret);
3207 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003208 struct io_async_ctx io;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003209 unsigned flags;
3210
Jens Axboe03b12302019-12-02 18:50:25 -07003211 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07003212 kmsg = &req->io->msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003213 kmsg->msg.msg_name = &req->io->msg.addr;
Jens Axboe0b416c32019-12-15 10:57:46 -07003214 /* if iov is set, it's allocated already */
3215 if (!kmsg->iov)
3216 kmsg->iov = kmsg->fast_iov;
3217 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07003218 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003219 struct io_sr_msg *sr = &req->sr_msg;
3220
Jens Axboe0b416c32019-12-15 10:57:46 -07003221 kmsg = &io.msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003222 kmsg->msg.msg_name = &io.msg.addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003223
3224 io.msg.iov = io.msg.fast_iov;
3225 ret = recvmsg_copy_msghdr(&io.msg.msg, sr->msg,
3226 sr->msg_flags, &io.msg.uaddr,
3227 &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07003228 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003229 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003230 }
Jens Axboe0fa03c62019-04-19 13:34:07 -06003231
Jens Axboee47293f2019-12-20 08:58:21 -07003232 flags = req->sr_msg.msg_flags;
3233 if (flags & MSG_DONTWAIT)
3234 req->flags |= REQ_F_NOWAIT;
3235 else if (force_nonblock)
3236 flags |= MSG_DONTWAIT;
3237
3238 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.msg,
3239 kmsg->uaddr, flags);
Jens Axboe03b12302019-12-02 18:50:25 -07003240 if (force_nonblock && ret == -EAGAIN) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003241 if (req->io)
3242 return -EAGAIN;
Pavel Begunkov1e950812020-02-06 19:51:16 +03003243 if (io_alloc_async_ctx(req)) {
Dan Carpenter297a31e2020-02-17 17:39:45 +03003244 if (kmsg->iov != kmsg->fast_iov)
Pavel Begunkov1e950812020-02-06 19:51:16 +03003245 kfree(kmsg->iov);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003246 return -ENOMEM;
Pavel Begunkov1e950812020-02-06 19:51:16 +03003247 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003248 memcpy(&req->io->msg, &io.msg, sizeof(io.msg));
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003249 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe0b416c32019-12-15 10:57:46 -07003250 return -EAGAIN;
Jens Axboe03b12302019-12-02 18:50:25 -07003251 }
Jens Axboe441cdbd2019-12-02 18:49:10 -07003252 if (ret == -ERESTARTSYS)
3253 ret = -EINTR;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003254 }
3255
Pavel Begunkov1e950812020-02-06 19:51:16 +03003256 if (kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07003257 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003258 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe78e19bb2019-11-06 15:21:34 -07003259 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003260 if (ret < 0)
3261 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003262 io_put_req_find_next(req, nxt);
Jens Axboe0fa03c62019-04-19 13:34:07 -06003263 return 0;
3264#else
3265 return -EOPNOTSUPP;
3266#endif
3267}
3268
Jens Axboefddafac2020-01-04 20:19:44 -07003269static int io_recv(struct io_kiocb *req, struct io_kiocb **nxt,
3270 bool force_nonblock)
3271{
3272#if defined(CONFIG_NET)
3273 struct socket *sock;
3274 int ret;
3275
3276 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3277 return -EINVAL;
3278
3279 sock = sock_from_file(req->file, &ret);
3280 if (sock) {
3281 struct io_sr_msg *sr = &req->sr_msg;
3282 struct msghdr msg;
3283 struct iovec iov;
3284 unsigned flags;
3285
3286 ret = import_single_range(READ, sr->buf, sr->len, &iov,
3287 &msg.msg_iter);
3288 if (ret)
3289 return ret;
3290
3291 msg.msg_name = NULL;
3292 msg.msg_control = NULL;
3293 msg.msg_controllen = 0;
3294 msg.msg_namelen = 0;
3295 msg.msg_iocb = NULL;
3296 msg.msg_flags = 0;
3297
3298 flags = req->sr_msg.msg_flags;
3299 if (flags & MSG_DONTWAIT)
3300 req->flags |= REQ_F_NOWAIT;
3301 else if (force_nonblock)
3302 flags |= MSG_DONTWAIT;
3303
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003304 ret = sock_recvmsg(sock, &msg, flags);
Jens Axboefddafac2020-01-04 20:19:44 -07003305 if (force_nonblock && ret == -EAGAIN)
3306 return -EAGAIN;
3307 if (ret == -ERESTARTSYS)
3308 ret = -EINTR;
3309 }
3310
3311 io_cqring_add_event(req, ret);
3312 if (ret < 0)
3313 req_set_fail_links(req);
3314 io_put_req_find_next(req, nxt);
3315 return 0;
3316#else
3317 return -EOPNOTSUPP;
3318#endif
3319}
3320
3321
Jens Axboe3529d8c2019-12-19 18:24:38 -07003322static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003323{
3324#if defined(CONFIG_NET)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003325 struct io_accept *accept = &req->accept;
3326
Jens Axboe17f2fe32019-10-17 14:42:58 -06003327 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3328 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05003329 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003330 return -EINVAL;
3331
Jens Axboed55e5f52019-12-11 16:12:15 -07003332 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3333 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003334 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003335 return 0;
3336#else
3337 return -EOPNOTSUPP;
3338#endif
3339}
Jens Axboe17f2fe32019-10-17 14:42:58 -06003340
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003341#if defined(CONFIG_NET)
3342static int __io_accept(struct io_kiocb *req, struct io_kiocb **nxt,
3343 bool force_nonblock)
3344{
3345 struct io_accept *accept = &req->accept;
3346 unsigned file_flags;
3347 int ret;
3348
3349 file_flags = force_nonblock ? O_NONBLOCK : 0;
3350 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
3351 accept->addr_len, accept->flags);
3352 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003353 return -EAGAIN;
Jens Axboe8e3cca12019-11-09 19:52:33 -07003354 if (ret == -ERESTARTSYS)
3355 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003356 if (ret < 0)
3357 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07003358 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003359 io_put_req_find_next(req, nxt);
Jens Axboe17f2fe32019-10-17 14:42:58 -06003360 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003361}
3362
3363static void io_accept_finish(struct io_wq_work **workptr)
3364{
3365 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3366 struct io_kiocb *nxt = NULL;
3367
Jens Axboee441d1c2020-02-20 09:59:02 -07003368 io_put_req(req);
3369
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003370 if (io_req_cancelled(req))
3371 return;
3372 __io_accept(req, &nxt, false);
3373 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07003374 io_wq_assign_next(workptr, nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003375}
3376#endif
3377
3378static int io_accept(struct io_kiocb *req, struct io_kiocb **nxt,
3379 bool force_nonblock)
3380{
3381#if defined(CONFIG_NET)
3382 int ret;
3383
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003384 ret = __io_accept(req, nxt, force_nonblock);
3385 if (ret == -EAGAIN && force_nonblock) {
3386 req->work.func = io_accept_finish;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003387 return -EAGAIN;
3388 }
3389 return 0;
Jens Axboe17f2fe32019-10-17 14:42:58 -06003390#else
3391 return -EOPNOTSUPP;
3392#endif
3393}
3394
Jens Axboe3529d8c2019-12-19 18:24:38 -07003395static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07003396{
3397#if defined(CONFIG_NET)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003398 struct io_connect *conn = &req->connect;
3399 struct io_async_ctx *io = req->io;
Jens Axboef499a022019-12-02 16:28:46 -07003400
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003401 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3402 return -EINVAL;
3403 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
3404 return -EINVAL;
3405
Jens Axboe3529d8c2019-12-19 18:24:38 -07003406 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3407 conn->addr_len = READ_ONCE(sqe->addr2);
3408
3409 if (!io)
3410 return 0;
3411
3412 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003413 &io->connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07003414#else
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003415 return -EOPNOTSUPP;
Jens Axboef499a022019-12-02 16:28:46 -07003416#endif
3417}
3418
Jens Axboefc4df992019-12-10 14:38:45 -07003419static int io_connect(struct io_kiocb *req, struct io_kiocb **nxt,
3420 bool force_nonblock)
Jens Axboef8e85cf2019-11-23 14:24:24 -07003421{
3422#if defined(CONFIG_NET)
Jens Axboef499a022019-12-02 16:28:46 -07003423 struct io_async_ctx __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003424 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003425 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003426
Jens Axboef499a022019-12-02 16:28:46 -07003427 if (req->io) {
3428 io = req->io;
3429 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003430 ret = move_addr_to_kernel(req->connect.addr,
3431 req->connect.addr_len,
3432 &__io.connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07003433 if (ret)
3434 goto out;
3435 io = &__io;
3436 }
3437
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003438 file_flags = force_nonblock ? O_NONBLOCK : 0;
3439
3440 ret = __sys_connect_file(req->file, &io->connect.address,
3441 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07003442 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003443 if (req->io)
3444 return -EAGAIN;
3445 if (io_alloc_async_ctx(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07003446 ret = -ENOMEM;
3447 goto out;
3448 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003449 memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect));
Jens Axboef8e85cf2019-11-23 14:24:24 -07003450 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07003451 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07003452 if (ret == -ERESTARTSYS)
3453 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07003454out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003455 if (ret < 0)
3456 req_set_fail_links(req);
Jens Axboef8e85cf2019-11-23 14:24:24 -07003457 io_cqring_add_event(req, ret);
3458 io_put_req_find_next(req, nxt);
3459 return 0;
3460#else
3461 return -EOPNOTSUPP;
3462#endif
3463}
3464
Jens Axboe221c5eb2019-01-17 09:41:58 -07003465static void io_poll_remove_one(struct io_kiocb *req)
3466{
3467 struct io_poll_iocb *poll = &req->poll;
3468
3469 spin_lock(&poll->head->lock);
3470 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07003471 if (!list_empty(&poll->wait.entry)) {
3472 list_del_init(&poll->wait.entry);
Jackie Liua197f662019-11-08 08:09:12 -07003473 io_queue_async_work(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003474 }
3475 spin_unlock(&poll->head->lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07003476 hash_del(&req->hash_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003477}
3478
3479static void io_poll_remove_all(struct io_ring_ctx *ctx)
3480{
Jens Axboe78076bb2019-12-04 19:56:40 -07003481 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003482 struct io_kiocb *req;
Jens Axboe78076bb2019-12-04 19:56:40 -07003483 int i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003484
3485 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07003486 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
3487 struct hlist_head *list;
3488
3489 list = &ctx->cancel_hash[i];
3490 hlist_for_each_entry_safe(req, tmp, list, hash_node)
3491 io_poll_remove_one(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003492 }
3493 spin_unlock_irq(&ctx->completion_lock);
3494}
3495
Jens Axboe47f46762019-11-09 17:43:02 -07003496static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
3497{
Jens Axboe78076bb2019-12-04 19:56:40 -07003498 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07003499 struct io_kiocb *req;
3500
Jens Axboe78076bb2019-12-04 19:56:40 -07003501 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
3502 hlist_for_each_entry(req, list, hash_node) {
3503 if (sqe_addr == req->user_data) {
Jens Axboeeac406c2019-11-14 12:09:58 -07003504 io_poll_remove_one(req);
3505 return 0;
3506 }
Jens Axboe47f46762019-11-09 17:43:02 -07003507 }
3508
3509 return -ENOENT;
3510}
3511
Jens Axboe3529d8c2019-12-19 18:24:38 -07003512static int io_poll_remove_prep(struct io_kiocb *req,
3513 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003514{
Jens Axboe221c5eb2019-01-17 09:41:58 -07003515 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3516 return -EINVAL;
3517 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
3518 sqe->poll_events)
3519 return -EINVAL;
3520
Jens Axboe0969e782019-12-17 18:40:57 -07003521 req->poll.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07003522 return 0;
3523}
3524
3525/*
3526 * Find a running poll command that matches one specified in sqe->addr,
3527 * and remove it if found.
3528 */
3529static int io_poll_remove(struct io_kiocb *req)
3530{
3531 struct io_ring_ctx *ctx = req->ctx;
3532 u64 addr;
3533 int ret;
3534
Jens Axboe0969e782019-12-17 18:40:57 -07003535 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003536 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07003537 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003538 spin_unlock_irq(&ctx->completion_lock);
3539
Jens Axboe78e19bb2019-11-06 15:21:34 -07003540 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003541 if (ret < 0)
3542 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06003543 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003544 return 0;
3545}
3546
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003547static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003548{
Jackie Liua197f662019-11-08 08:09:12 -07003549 struct io_ring_ctx *ctx = req->ctx;
3550
Jens Axboe8c838782019-03-12 15:48:16 -06003551 req->poll.done = true;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003552 if (error)
3553 io_cqring_fill_event(req, error);
3554 else
3555 io_cqring_fill_event(req, mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06003556 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003557}
3558
Jens Axboe561fb042019-10-24 07:25:42 -06003559static void io_poll_complete_work(struct io_wq_work **workptr)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003560{
Jens Axboe561fb042019-10-24 07:25:42 -06003561 struct io_wq_work *work = *workptr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003562 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
3563 struct io_poll_iocb *poll = &req->poll;
3564 struct poll_table_struct pt = { ._key = poll->events };
3565 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe89723d02019-11-05 15:32:58 -07003566 struct io_kiocb *nxt = NULL;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003567 __poll_t mask = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003568 int ret = 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003569
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003570 if (work->flags & IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06003571 WRITE_ONCE(poll->canceled, true);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003572 ret = -ECANCELED;
3573 } else if (READ_ONCE(poll->canceled)) {
3574 ret = -ECANCELED;
3575 }
Jens Axboe561fb042019-10-24 07:25:42 -06003576
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003577 if (ret != -ECANCELED)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003578 mask = vfs_poll(poll->file, &pt) & poll->events;
3579
3580 /*
3581 * Note that ->ki_cancel callers also delete iocb from active_reqs after
3582 * calling ->ki_cancel. We need the ctx_lock roundtrip here to
3583 * synchronize with them. In the cancellation case the list_del_init
3584 * itself is not actually needed, but harmless so we keep it in to
3585 * avoid further branches in the fast path.
3586 */
3587 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003588 if (!mask && ret != -ECANCELED) {
Jens Axboe392edb42019-12-09 17:52:20 -07003589 add_wait_queue(poll->head, &poll->wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003590 spin_unlock_irq(&ctx->completion_lock);
3591 return;
3592 }
Jens Axboe78076bb2019-12-04 19:56:40 -07003593 hash_del(&req->hash_node);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003594 io_poll_complete(req, mask, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003595 spin_unlock_irq(&ctx->completion_lock);
3596
Jens Axboe8c838782019-03-12 15:48:16 -06003597 io_cqring_ev_posted(ctx);
Jens Axboe89723d02019-11-05 15:32:58 -07003598
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003599 if (ret < 0)
3600 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003601 io_put_req_find_next(req, &nxt);
Jens Axboe89723d02019-11-05 15:32:58 -07003602 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07003603 io_wq_assign_next(workptr, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003604}
3605
Jens Axboee94f1412019-12-19 12:06:02 -07003606static void __io_poll_flush(struct io_ring_ctx *ctx, struct llist_node *nodes)
3607{
Jens Axboee94f1412019-12-19 12:06:02 -07003608 struct io_kiocb *req, *tmp;
Jens Axboe8237e042019-12-28 10:48:22 -07003609 struct req_batch rb;
Jens Axboee94f1412019-12-19 12:06:02 -07003610
Jens Axboec6ca97b302019-12-28 12:11:08 -07003611 rb.to_free = rb.need_iter = 0;
Jens Axboee94f1412019-12-19 12:06:02 -07003612 spin_lock_irq(&ctx->completion_lock);
3613 llist_for_each_entry_safe(req, tmp, nodes, llist_node) {
3614 hash_del(&req->hash_node);
3615 io_poll_complete(req, req->result, 0);
3616
Jens Axboe8237e042019-12-28 10:48:22 -07003617 if (refcount_dec_and_test(&req->refs) &&
3618 !io_req_multi_free(&rb, req)) {
3619 req->flags |= REQ_F_COMP_LOCKED;
3620 io_free_req(req);
Jens Axboee94f1412019-12-19 12:06:02 -07003621 }
3622 }
3623 spin_unlock_irq(&ctx->completion_lock);
3624
3625 io_cqring_ev_posted(ctx);
Jens Axboe8237e042019-12-28 10:48:22 -07003626 io_free_req_many(ctx, &rb);
Jens Axboee94f1412019-12-19 12:06:02 -07003627}
3628
3629static void io_poll_flush(struct io_wq_work **workptr)
3630{
3631 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3632 struct llist_node *nodes;
3633
3634 nodes = llist_del_all(&req->ctx->poll_llist);
3635 if (nodes)
3636 __io_poll_flush(req->ctx, nodes);
3637}
3638
Jens Axboef0b493e2020-02-01 21:30:11 -07003639static void io_poll_trigger_evfd(struct io_wq_work **workptr)
3640{
3641 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3642
3643 eventfd_signal(req->ctx->cq_ev_fd, 1);
3644 io_put_req(req);
3645}
3646
Jens Axboe221c5eb2019-01-17 09:41:58 -07003647static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
3648 void *key)
3649{
Jens Axboee9444752019-11-26 15:02:04 -07003650 struct io_poll_iocb *poll = wait->private;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003651 struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
3652 struct io_ring_ctx *ctx = req->ctx;
3653 __poll_t mask = key_to_poll(key);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003654
3655 /* for instances that support it check for an event match first: */
Jens Axboe8c838782019-03-12 15:48:16 -06003656 if (mask && !(mask & poll->events))
3657 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003658
Jens Axboe392edb42019-12-09 17:52:20 -07003659 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06003660
Jens Axboe7c9e7f02019-11-12 08:15:53 -07003661 /*
3662 * Run completion inline if we can. We're using trylock here because
3663 * we are violating the completion_lock -> poll wq lock ordering.
3664 * If we have a link timeout we're going to need the completion_lock
3665 * for finalizing the request, mark us as having grabbed that already.
3666 */
Jens Axboee94f1412019-12-19 12:06:02 -07003667 if (mask) {
3668 unsigned long flags;
Jens Axboe8c838782019-03-12 15:48:16 -06003669
Jens Axboee94f1412019-12-19 12:06:02 -07003670 if (llist_empty(&ctx->poll_llist) &&
3671 spin_trylock_irqsave(&ctx->completion_lock, flags)) {
Jens Axboef0b493e2020-02-01 21:30:11 -07003672 bool trigger_ev;
3673
Jens Axboee94f1412019-12-19 12:06:02 -07003674 hash_del(&req->hash_node);
3675 io_poll_complete(req, mask, 0);
Jens Axboee94f1412019-12-19 12:06:02 -07003676
Jens Axboef0b493e2020-02-01 21:30:11 -07003677 trigger_ev = io_should_trigger_evfd(ctx);
3678 if (trigger_ev && eventfd_signal_count()) {
3679 trigger_ev = false;
3680 req->work.func = io_poll_trigger_evfd;
3681 } else {
3682 req->flags |= REQ_F_COMP_LOCKED;
3683 io_put_req(req);
3684 req = NULL;
3685 }
3686 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3687 __io_cqring_ev_posted(ctx, trigger_ev);
Jens Axboee94f1412019-12-19 12:06:02 -07003688 } else {
3689 req->result = mask;
3690 req->llist_node.next = NULL;
3691 /* if the list wasn't empty, we're done */
3692 if (!llist_add(&req->llist_node, &ctx->poll_llist))
3693 req = NULL;
3694 else
3695 req->work.func = io_poll_flush;
3696 }
Jens Axboe8c838782019-03-12 15:48:16 -06003697 }
Jens Axboee94f1412019-12-19 12:06:02 -07003698 if (req)
3699 io_queue_async_work(req);
Jens Axboe8c838782019-03-12 15:48:16 -06003700
Jens Axboe221c5eb2019-01-17 09:41:58 -07003701 return 1;
3702}
3703
3704struct io_poll_table {
3705 struct poll_table_struct pt;
3706 struct io_kiocb *req;
3707 int error;
3708};
3709
3710static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
3711 struct poll_table_struct *p)
3712{
3713 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
3714
3715 if (unlikely(pt->req->poll.head)) {
3716 pt->error = -EINVAL;
3717 return;
3718 }
3719
3720 pt->error = 0;
3721 pt->req->poll.head = head;
Jens Axboe392edb42019-12-09 17:52:20 -07003722 add_wait_queue(head, &pt->req->poll.wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003723}
3724
Jens Axboeeac406c2019-11-14 12:09:58 -07003725static void io_poll_req_insert(struct io_kiocb *req)
3726{
3727 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07003728 struct hlist_head *list;
Jens Axboeeac406c2019-11-14 12:09:58 -07003729
Jens Axboe78076bb2019-12-04 19:56:40 -07003730 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
3731 hlist_add_head(&req->hash_node, list);
Jens Axboeeac406c2019-11-14 12:09:58 -07003732}
3733
Jens Axboe3529d8c2019-12-19 18:24:38 -07003734static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003735{
3736 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003737 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003738
3739 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3740 return -EINVAL;
3741 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
3742 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06003743 if (!poll->file)
3744 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003745
Jens Axboe221c5eb2019-01-17 09:41:58 -07003746 events = READ_ONCE(sqe->poll_events);
3747 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
Jens Axboe0969e782019-12-17 18:40:57 -07003748 return 0;
3749}
3750
3751static int io_poll_add(struct io_kiocb *req, struct io_kiocb **nxt)
3752{
3753 struct io_poll_iocb *poll = &req->poll;
3754 struct io_ring_ctx *ctx = req->ctx;
3755 struct io_poll_table ipt;
3756 bool cancel = false;
3757 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07003758
3759 INIT_IO_WORK(&req->work, io_poll_complete_work);
Jens Axboe78076bb2019-12-04 19:56:40 -07003760 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003761
Jens Axboe221c5eb2019-01-17 09:41:58 -07003762 poll->head = NULL;
Jens Axboe8c838782019-03-12 15:48:16 -06003763 poll->done = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003764 poll->canceled = false;
3765
3766 ipt.pt._qproc = io_poll_queue_proc;
3767 ipt.pt._key = poll->events;
3768 ipt.req = req;
3769 ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
3770
3771 /* initialized the list so that we can do list_empty checks */
Jens Axboe392edb42019-12-09 17:52:20 -07003772 INIT_LIST_HEAD(&poll->wait.entry);
3773 init_waitqueue_func_entry(&poll->wait, io_poll_wake);
3774 poll->wait.private = poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003775
Jens Axboe36703242019-07-25 10:20:18 -06003776 INIT_LIST_HEAD(&req->list);
3777
Jens Axboe221c5eb2019-01-17 09:41:58 -07003778 mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003779
3780 spin_lock_irq(&ctx->completion_lock);
Jens Axboe8c838782019-03-12 15:48:16 -06003781 if (likely(poll->head)) {
3782 spin_lock(&poll->head->lock);
Jens Axboe392edb42019-12-09 17:52:20 -07003783 if (unlikely(list_empty(&poll->wait.entry))) {
Jens Axboe8c838782019-03-12 15:48:16 -06003784 if (ipt.error)
3785 cancel = true;
3786 ipt.error = 0;
3787 mask = 0;
3788 }
3789 if (mask || ipt.error)
Jens Axboe392edb42019-12-09 17:52:20 -07003790 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06003791 else if (cancel)
3792 WRITE_ONCE(poll->canceled, true);
3793 else if (!poll->done) /* actually waiting for an event */
Jens Axboeeac406c2019-11-14 12:09:58 -07003794 io_poll_req_insert(req);
Jens Axboe8c838782019-03-12 15:48:16 -06003795 spin_unlock(&poll->head->lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003796 }
Jens Axboe8c838782019-03-12 15:48:16 -06003797 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06003798 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003799 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06003800 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07003801 spin_unlock_irq(&ctx->completion_lock);
3802
Jens Axboe8c838782019-03-12 15:48:16 -06003803 if (mask) {
3804 io_cqring_ev_posted(ctx);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003805 io_put_req_find_next(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003806 }
Jens Axboe8c838782019-03-12 15:48:16 -06003807 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003808}
3809
Jens Axboe5262f562019-09-17 12:26:57 -06003810static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
3811{
Jens Axboead8a48a2019-11-15 08:49:11 -07003812 struct io_timeout_data *data = container_of(timer,
3813 struct io_timeout_data, timer);
3814 struct io_kiocb *req = data->req;
3815 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06003816 unsigned long flags;
3817
Jens Axboe5262f562019-09-17 12:26:57 -06003818 atomic_inc(&ctx->cq_timeouts);
3819
3820 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08003821 /*
Jens Axboe11365042019-10-16 09:08:32 -06003822 * We could be racing with timeout deletion. If the list is empty,
3823 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08003824 */
Jens Axboe842f9612019-10-29 12:34:10 -06003825 if (!list_empty(&req->list)) {
Jens Axboe11365042019-10-16 09:08:32 -06003826 struct io_kiocb *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06003827
Jens Axboe11365042019-10-16 09:08:32 -06003828 /*
3829 * Adjust the reqs sequence before the current one because it
Brian Gianforcarod195a662019-12-13 03:09:50 -08003830 * will consume a slot in the cq_ring and the cq_tail
Jens Axboe11365042019-10-16 09:08:32 -06003831 * pointer will be increased, otherwise other timeout reqs may
3832 * return in advance without waiting for enough wait_nr.
3833 */
3834 prev = req;
3835 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
3836 prev->sequence++;
Jens Axboe11365042019-10-16 09:08:32 -06003837 list_del_init(&req->list);
Jens Axboe11365042019-10-16 09:08:32 -06003838 }
Jens Axboe842f9612019-10-29 12:34:10 -06003839
Jens Axboe78e19bb2019-11-06 15:21:34 -07003840 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06003841 io_commit_cqring(ctx);
3842 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3843
3844 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003845 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06003846 io_put_req(req);
3847 return HRTIMER_NORESTART;
3848}
3849
Jens Axboe47f46762019-11-09 17:43:02 -07003850static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
3851{
3852 struct io_kiocb *req;
3853 int ret = -ENOENT;
3854
3855 list_for_each_entry(req, &ctx->timeout_list, list) {
3856 if (user_data == req->user_data) {
3857 list_del_init(&req->list);
3858 ret = 0;
3859 break;
3860 }
3861 }
3862
3863 if (ret == -ENOENT)
3864 return ret;
3865
Jens Axboe2d283902019-12-04 11:08:05 -07003866 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe47f46762019-11-09 17:43:02 -07003867 if (ret == -1)
3868 return -EALREADY;
3869
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003870 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07003871 io_cqring_fill_event(req, -ECANCELED);
3872 io_put_req(req);
3873 return 0;
3874}
3875
Jens Axboe3529d8c2019-12-19 18:24:38 -07003876static int io_timeout_remove_prep(struct io_kiocb *req,
3877 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07003878{
Jens Axboeb29472e2019-12-17 18:50:29 -07003879 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3880 return -EINVAL;
3881 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
3882 return -EINVAL;
3883
3884 req->timeout.addr = READ_ONCE(sqe->addr);
3885 req->timeout.flags = READ_ONCE(sqe->timeout_flags);
3886 if (req->timeout.flags)
3887 return -EINVAL;
3888
Jens Axboeb29472e2019-12-17 18:50:29 -07003889 return 0;
3890}
3891
Jens Axboe11365042019-10-16 09:08:32 -06003892/*
3893 * Remove or update an existing timeout command
3894 */
Jens Axboefc4df992019-12-10 14:38:45 -07003895static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06003896{
3897 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07003898 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06003899
Jens Axboe11365042019-10-16 09:08:32 -06003900 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb29472e2019-12-17 18:50:29 -07003901 ret = io_timeout_cancel(ctx, req->timeout.addr);
Jens Axboe11365042019-10-16 09:08:32 -06003902
Jens Axboe47f46762019-11-09 17:43:02 -07003903 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06003904 io_commit_cqring(ctx);
3905 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06003906 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003907 if (ret < 0)
3908 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003909 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06003910 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06003911}
3912
Jens Axboe3529d8c2019-12-19 18:24:38 -07003913static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07003914 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06003915{
Jens Axboead8a48a2019-11-15 08:49:11 -07003916 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06003917 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06003918
Jens Axboead8a48a2019-11-15 08:49:11 -07003919 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06003920 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07003921 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06003922 return -EINVAL;
Jens Axboe2d283902019-12-04 11:08:05 -07003923 if (sqe->off && is_timeout_link)
3924 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06003925 flags = READ_ONCE(sqe->timeout_flags);
3926 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06003927 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06003928
Jens Axboe26a61672019-12-20 09:02:01 -07003929 req->timeout.count = READ_ONCE(sqe->off);
3930
Jens Axboe3529d8c2019-12-19 18:24:38 -07003931 if (!req->io && io_alloc_async_ctx(req))
Jens Axboe26a61672019-12-20 09:02:01 -07003932 return -ENOMEM;
3933
3934 data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07003935 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07003936 req->flags |= REQ_F_TIMEOUT;
3937
3938 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06003939 return -EFAULT;
3940
Jens Axboe11365042019-10-16 09:08:32 -06003941 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07003942 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06003943 else
Jens Axboead8a48a2019-11-15 08:49:11 -07003944 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06003945
Jens Axboead8a48a2019-11-15 08:49:11 -07003946 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
3947 return 0;
3948}
3949
Jens Axboefc4df992019-12-10 14:38:45 -07003950static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07003951{
3952 unsigned count;
3953 struct io_ring_ctx *ctx = req->ctx;
3954 struct io_timeout_data *data;
3955 struct list_head *entry;
3956 unsigned span = 0;
Jens Axboead8a48a2019-11-15 08:49:11 -07003957
Jens Axboe2d283902019-12-04 11:08:05 -07003958 data = &req->io->timeout;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003959
Jens Axboe5262f562019-09-17 12:26:57 -06003960 /*
3961 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07003962 * timeout event to be satisfied. If it isn't set, then this is
3963 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06003964 */
Jens Axboe26a61672019-12-20 09:02:01 -07003965 count = req->timeout.count;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003966 if (!count) {
3967 req->flags |= REQ_F_TIMEOUT_NOSEQ;
3968 spin_lock_irq(&ctx->completion_lock);
3969 entry = ctx->timeout_list.prev;
3970 goto add;
3971 }
Jens Axboe5262f562019-09-17 12:26:57 -06003972
3973 req->sequence = ctx->cached_sq_head + count - 1;
Jens Axboe2d283902019-12-04 11:08:05 -07003974 data->seq_offset = count;
Jens Axboe5262f562019-09-17 12:26:57 -06003975
3976 /*
3977 * Insertion sort, ensuring the first entry in the list is always
3978 * the one we need first.
3979 */
Jens Axboe5262f562019-09-17 12:26:57 -06003980 spin_lock_irq(&ctx->completion_lock);
3981 list_for_each_prev(entry, &ctx->timeout_list) {
3982 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
yangerkun5da0fb12019-10-15 21:59:29 +08003983 unsigned nxt_sq_head;
3984 long long tmp, tmp_nxt;
Jens Axboe2d283902019-12-04 11:08:05 -07003985 u32 nxt_offset = nxt->io->timeout.seq_offset;
Jens Axboe5262f562019-09-17 12:26:57 -06003986
Jens Axboe93bd25b2019-11-11 23:34:31 -07003987 if (nxt->flags & REQ_F_TIMEOUT_NOSEQ)
3988 continue;
3989
yangerkun5da0fb12019-10-15 21:59:29 +08003990 /*
3991 * Since cached_sq_head + count - 1 can overflow, use type long
3992 * long to store it.
3993 */
3994 tmp = (long long)ctx->cached_sq_head + count - 1;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +03003995 nxt_sq_head = nxt->sequence - nxt_offset + 1;
3996 tmp_nxt = (long long)nxt_sq_head + nxt_offset - 1;
yangerkun5da0fb12019-10-15 21:59:29 +08003997
3998 /*
3999 * cached_sq_head may overflow, and it will never overflow twice
4000 * once there is some timeout req still be valid.
4001 */
4002 if (ctx->cached_sq_head < nxt_sq_head)
yangerkun8b07a652019-10-17 12:12:35 +08004003 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08004004
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08004005 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06004006 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08004007
4008 /*
4009 * Sequence of reqs after the insert one and itself should
4010 * be adjusted because each timeout req consumes a slot.
4011 */
4012 span++;
4013 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06004014 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08004015 req->sequence -= span;
Jens Axboe93bd25b2019-11-11 23:34:31 -07004016add:
Jens Axboe5262f562019-09-17 12:26:57 -06004017 list_add(&req->list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07004018 data->timer.function = io_timeout_fn;
4019 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06004020 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06004021 return 0;
4022}
4023
Jens Axboe62755e32019-10-28 21:49:21 -06004024static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06004025{
Jens Axboe62755e32019-10-28 21:49:21 -06004026 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06004027
Jens Axboe62755e32019-10-28 21:49:21 -06004028 return req->user_data == (unsigned long) data;
4029}
4030
Jens Axboee977d6d2019-11-05 12:39:45 -07004031static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06004032{
Jens Axboe62755e32019-10-28 21:49:21 -06004033 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06004034 int ret = 0;
4035
Jens Axboe62755e32019-10-28 21:49:21 -06004036 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
4037 switch (cancel_ret) {
4038 case IO_WQ_CANCEL_OK:
4039 ret = 0;
4040 break;
4041 case IO_WQ_CANCEL_RUNNING:
4042 ret = -EALREADY;
4043 break;
4044 case IO_WQ_CANCEL_NOTFOUND:
4045 ret = -ENOENT;
4046 break;
4047 }
4048
Jens Axboee977d6d2019-11-05 12:39:45 -07004049 return ret;
4050}
4051
Jens Axboe47f46762019-11-09 17:43:02 -07004052static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
4053 struct io_kiocb *req, __u64 sqe_addr,
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004054 struct io_kiocb **nxt, int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07004055{
4056 unsigned long flags;
4057 int ret;
4058
4059 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
4060 if (ret != -ENOENT) {
4061 spin_lock_irqsave(&ctx->completion_lock, flags);
4062 goto done;
4063 }
4064
4065 spin_lock_irqsave(&ctx->completion_lock, flags);
4066 ret = io_timeout_cancel(ctx, sqe_addr);
4067 if (ret != -ENOENT)
4068 goto done;
4069 ret = io_poll_cancel(ctx, sqe_addr);
4070done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004071 if (!ret)
4072 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07004073 io_cqring_fill_event(req, ret);
4074 io_commit_cqring(ctx);
4075 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4076 io_cqring_ev_posted(ctx);
4077
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004078 if (ret < 0)
4079 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07004080 io_put_req_find_next(req, nxt);
4081}
4082
Jens Axboe3529d8c2019-12-19 18:24:38 -07004083static int io_async_cancel_prep(struct io_kiocb *req,
4084 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07004085{
Jens Axboefbf23842019-12-17 18:45:56 -07004086 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07004087 return -EINVAL;
4088 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
4089 sqe->cancel_flags)
4090 return -EINVAL;
4091
Jens Axboefbf23842019-12-17 18:45:56 -07004092 req->cancel.addr = READ_ONCE(sqe->addr);
4093 return 0;
4094}
4095
4096static int io_async_cancel(struct io_kiocb *req, struct io_kiocb **nxt)
4097{
4098 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07004099
4100 io_async_find_and_cancel(ctx, req, req->cancel.addr, nxt, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06004101 return 0;
4102}
4103
Jens Axboe05f3fb32019-12-09 11:22:50 -07004104static int io_files_update_prep(struct io_kiocb *req,
4105 const struct io_uring_sqe *sqe)
4106{
4107 if (sqe->flags || sqe->ioprio || sqe->rw_flags)
4108 return -EINVAL;
4109
4110 req->files_update.offset = READ_ONCE(sqe->off);
4111 req->files_update.nr_args = READ_ONCE(sqe->len);
4112 if (!req->files_update.nr_args)
4113 return -EINVAL;
4114 req->files_update.arg = READ_ONCE(sqe->addr);
4115 return 0;
4116}
4117
4118static int io_files_update(struct io_kiocb *req, bool force_nonblock)
4119{
4120 struct io_ring_ctx *ctx = req->ctx;
4121 struct io_uring_files_update up;
4122 int ret;
4123
Jens Axboef86cd202020-01-29 13:46:44 -07004124 if (force_nonblock)
Jens Axboe05f3fb32019-12-09 11:22:50 -07004125 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004126
4127 up.offset = req->files_update.offset;
4128 up.fds = req->files_update.arg;
4129
4130 mutex_lock(&ctx->uring_lock);
4131 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
4132 mutex_unlock(&ctx->uring_lock);
4133
4134 if (ret < 0)
4135 req_set_fail_links(req);
4136 io_cqring_add_event(req, ret);
4137 io_put_req(req);
4138 return 0;
4139}
4140
Jens Axboe3529d8c2019-12-19 18:24:38 -07004141static int io_req_defer_prep(struct io_kiocb *req,
4142 const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07004143{
Jens Axboee7815732019-12-17 19:45:06 -07004144 ssize_t ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07004145
Jens Axboef86cd202020-01-29 13:46:44 -07004146 if (io_op_defs[req->opcode].file_table) {
4147 ret = io_grab_files(req);
4148 if (unlikely(ret))
4149 return ret;
4150 }
4151
Jens Axboecccf0ee2020-01-27 16:34:48 -07004152 io_req_work_grab_env(req, &io_op_defs[req->opcode]);
4153
Jens Axboed625c6e2019-12-17 19:53:05 -07004154 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07004155 case IORING_OP_NOP:
4156 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004157 case IORING_OP_READV:
4158 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004159 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004160 ret = io_read_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07004161 break;
4162 case IORING_OP_WRITEV:
4163 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004164 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004165 ret = io_write_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07004166 break;
Jens Axboe0969e782019-12-17 18:40:57 -07004167 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004168 ret = io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07004169 break;
4170 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004171 ret = io_poll_remove_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07004172 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004173 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004174 ret = io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004175 break;
4176 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004177 ret = io_prep_sfr(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004178 break;
Jens Axboe03b12302019-12-02 18:50:25 -07004179 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004180 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004181 ret = io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07004182 break;
4183 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004184 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004185 ret = io_recvmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07004186 break;
Jens Axboef499a022019-12-02 16:28:46 -07004187 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004188 ret = io_connect_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07004189 break;
Jens Axboe2d283902019-12-04 11:08:05 -07004190 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004191 ret = io_timeout_prep(req, sqe, false);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004192 break;
Jens Axboeb29472e2019-12-17 18:50:29 -07004193 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004194 ret = io_timeout_remove_prep(req, sqe);
Jens Axboeb29472e2019-12-17 18:50:29 -07004195 break;
Jens Axboefbf23842019-12-17 18:45:56 -07004196 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004197 ret = io_async_cancel_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07004198 break;
Jens Axboe2d283902019-12-04 11:08:05 -07004199 case IORING_OP_LINK_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004200 ret = io_timeout_prep(req, sqe, true);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004201 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004202 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004203 ret = io_accept_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004204 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07004205 case IORING_OP_FALLOCATE:
4206 ret = io_fallocate_prep(req, sqe);
4207 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004208 case IORING_OP_OPENAT:
4209 ret = io_openat_prep(req, sqe);
4210 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07004211 case IORING_OP_CLOSE:
4212 ret = io_close_prep(req, sqe);
4213 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004214 case IORING_OP_FILES_UPDATE:
4215 ret = io_files_update_prep(req, sqe);
4216 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004217 case IORING_OP_STATX:
4218 ret = io_statx_prep(req, sqe);
4219 break;
Jens Axboe4840e412019-12-25 22:03:45 -07004220 case IORING_OP_FADVISE:
4221 ret = io_fadvise_prep(req, sqe);
4222 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07004223 case IORING_OP_MADVISE:
4224 ret = io_madvise_prep(req, sqe);
4225 break;
Jens Axboecebdb982020-01-08 17:59:24 -07004226 case IORING_OP_OPENAT2:
4227 ret = io_openat2_prep(req, sqe);
4228 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004229 case IORING_OP_EPOLL_CTL:
4230 ret = io_epoll_ctl_prep(req, sqe);
4231 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004232 default:
Jens Axboee7815732019-12-17 19:45:06 -07004233 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
4234 req->opcode);
4235 ret = -EINVAL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004236 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004237 }
4238
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004239 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07004240}
4241
Jens Axboe3529d8c2019-12-19 18:24:38 -07004242static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboede0617e2019-04-06 21:51:27 -06004243{
Jackie Liua197f662019-11-08 08:09:12 -07004244 struct io_ring_ctx *ctx = req->ctx;
Jens Axboef67676d2019-12-02 11:03:47 -07004245 int ret;
Jens Axboede0617e2019-04-06 21:51:27 -06004246
Bob Liu9d858b22019-11-13 18:06:25 +08004247 /* Still need defer if there is pending req in defer list. */
4248 if (!req_need_defer(req) && list_empty(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06004249 return 0;
4250
Jens Axboe3529d8c2019-12-19 18:24:38 -07004251 if (!req->io && io_alloc_async_ctx(req))
Jens Axboede0617e2019-04-06 21:51:27 -06004252 return -EAGAIN;
4253
Jens Axboe3529d8c2019-12-19 18:24:38 -07004254 ret = io_req_defer_prep(req, sqe);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004255 if (ret < 0)
Jens Axboe2d283902019-12-04 11:08:05 -07004256 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07004257
Jens Axboede0617e2019-04-06 21:51:27 -06004258 spin_lock_irq(&ctx->completion_lock);
Bob Liu9d858b22019-11-13 18:06:25 +08004259 if (!req_need_defer(req) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06004260 spin_unlock_irq(&ctx->completion_lock);
Jens Axboede0617e2019-04-06 21:51:27 -06004261 return 0;
4262 }
4263
Jens Axboe915967f2019-11-21 09:01:20 -07004264 trace_io_uring_defer(ctx, req, req->user_data);
Jens Axboede0617e2019-04-06 21:51:27 -06004265 list_add_tail(&req->list, &ctx->defer_list);
4266 spin_unlock_irq(&ctx->completion_lock);
4267 return -EIOCBQUEUED;
4268}
4269
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004270static void io_cleanup_req(struct io_kiocb *req)
4271{
4272 struct io_async_ctx *io = req->io;
4273
4274 switch (req->opcode) {
4275 case IORING_OP_READV:
4276 case IORING_OP_READ_FIXED:
4277 case IORING_OP_READ:
4278 case IORING_OP_WRITEV:
4279 case IORING_OP_WRITE_FIXED:
4280 case IORING_OP_WRITE:
4281 if (io->rw.iov != io->rw.fast_iov)
4282 kfree(io->rw.iov);
4283 break;
4284 case IORING_OP_SENDMSG:
4285 case IORING_OP_RECVMSG:
4286 if (io->msg.iov != io->msg.fast_iov)
4287 kfree(io->msg.iov);
4288 break;
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03004289 case IORING_OP_OPENAT:
4290 case IORING_OP_OPENAT2:
4291 case IORING_OP_STATX:
4292 putname(req->open.filename);
4293 break;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004294 }
4295
4296 req->flags &= ~REQ_F_NEED_CLEANUP;
4297}
4298
Jens Axboe3529d8c2019-12-19 18:24:38 -07004299static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
4300 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004301{
Jackie Liua197f662019-11-08 08:09:12 -07004302 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07004303 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004304
Jens Axboed625c6e2019-12-17 19:53:05 -07004305 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07004306 case IORING_OP_NOP:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004307 ret = io_nop(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004308 break;
4309 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004310 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004311 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004312 if (sqe) {
4313 ret = io_read_prep(req, sqe, force_nonblock);
4314 if (ret < 0)
4315 break;
4316 }
Pavel Begunkov267bc902019-11-07 01:41:08 +03004317 ret = io_read(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004318 break;
4319 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07004320 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004321 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004322 if (sqe) {
4323 ret = io_write_prep(req, sqe, force_nonblock);
4324 if (ret < 0)
4325 break;
4326 }
Pavel Begunkov267bc902019-11-07 01:41:08 +03004327 ret = io_write(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004328 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004329 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004330 if (sqe) {
4331 ret = io_prep_fsync(req, sqe);
4332 if (ret < 0)
4333 break;
4334 }
Jens Axboefc4df992019-12-10 14:38:45 -07004335 ret = io_fsync(req, nxt, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004336 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004337 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004338 if (sqe) {
4339 ret = io_poll_add_prep(req, sqe);
4340 if (ret)
4341 break;
4342 }
Jens Axboefc4df992019-12-10 14:38:45 -07004343 ret = io_poll_add(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004344 break;
4345 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004346 if (sqe) {
4347 ret = io_poll_remove_prep(req, sqe);
4348 if (ret < 0)
4349 break;
4350 }
Jens Axboefc4df992019-12-10 14:38:45 -07004351 ret = io_poll_remove(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004352 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004353 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004354 if (sqe) {
4355 ret = io_prep_sfr(req, sqe);
4356 if (ret < 0)
4357 break;
4358 }
Jens Axboefc4df992019-12-10 14:38:45 -07004359 ret = io_sync_file_range(req, nxt, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004360 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004361 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004362 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004363 if (sqe) {
4364 ret = io_sendmsg_prep(req, sqe);
4365 if (ret < 0)
4366 break;
4367 }
Jens Axboefddafac2020-01-04 20:19:44 -07004368 if (req->opcode == IORING_OP_SENDMSG)
4369 ret = io_sendmsg(req, nxt, force_nonblock);
4370 else
4371 ret = io_send(req, nxt, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004372 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06004373 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004374 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004375 if (sqe) {
4376 ret = io_recvmsg_prep(req, sqe);
4377 if (ret)
4378 break;
4379 }
Jens Axboefddafac2020-01-04 20:19:44 -07004380 if (req->opcode == IORING_OP_RECVMSG)
4381 ret = io_recvmsg(req, nxt, force_nonblock);
4382 else
4383 ret = io_recv(req, nxt, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06004384 break;
Jens Axboe5262f562019-09-17 12:26:57 -06004385 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004386 if (sqe) {
4387 ret = io_timeout_prep(req, sqe, false);
4388 if (ret)
4389 break;
4390 }
Jens Axboefc4df992019-12-10 14:38:45 -07004391 ret = io_timeout(req);
Jens Axboe5262f562019-09-17 12:26:57 -06004392 break;
Jens Axboe11365042019-10-16 09:08:32 -06004393 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004394 if (sqe) {
4395 ret = io_timeout_remove_prep(req, sqe);
4396 if (ret)
4397 break;
4398 }
Jens Axboefc4df992019-12-10 14:38:45 -07004399 ret = io_timeout_remove(req);
Jens Axboe11365042019-10-16 09:08:32 -06004400 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06004401 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004402 if (sqe) {
4403 ret = io_accept_prep(req, sqe);
4404 if (ret)
4405 break;
4406 }
Jens Axboefc4df992019-12-10 14:38:45 -07004407 ret = io_accept(req, nxt, force_nonblock);
Jens Axboe17f2fe32019-10-17 14:42:58 -06004408 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004409 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004410 if (sqe) {
4411 ret = io_connect_prep(req, sqe);
4412 if (ret)
4413 break;
4414 }
Jens Axboefc4df992019-12-10 14:38:45 -07004415 ret = io_connect(req, nxt, force_nonblock);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004416 break;
Jens Axboe62755e32019-10-28 21:49:21 -06004417 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004418 if (sqe) {
4419 ret = io_async_cancel_prep(req, sqe);
4420 if (ret)
4421 break;
4422 }
Jens Axboefc4df992019-12-10 14:38:45 -07004423 ret = io_async_cancel(req, nxt);
Jens Axboe62755e32019-10-28 21:49:21 -06004424 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07004425 case IORING_OP_FALLOCATE:
4426 if (sqe) {
4427 ret = io_fallocate_prep(req, sqe);
4428 if (ret)
4429 break;
4430 }
4431 ret = io_fallocate(req, nxt, force_nonblock);
4432 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004433 case IORING_OP_OPENAT:
4434 if (sqe) {
4435 ret = io_openat_prep(req, sqe);
4436 if (ret)
4437 break;
4438 }
4439 ret = io_openat(req, nxt, force_nonblock);
4440 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07004441 case IORING_OP_CLOSE:
4442 if (sqe) {
4443 ret = io_close_prep(req, sqe);
4444 if (ret)
4445 break;
4446 }
4447 ret = io_close(req, nxt, force_nonblock);
4448 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004449 case IORING_OP_FILES_UPDATE:
4450 if (sqe) {
4451 ret = io_files_update_prep(req, sqe);
4452 if (ret)
4453 break;
4454 }
4455 ret = io_files_update(req, force_nonblock);
4456 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004457 case IORING_OP_STATX:
4458 if (sqe) {
4459 ret = io_statx_prep(req, sqe);
4460 if (ret)
4461 break;
4462 }
4463 ret = io_statx(req, nxt, force_nonblock);
4464 break;
Jens Axboe4840e412019-12-25 22:03:45 -07004465 case IORING_OP_FADVISE:
4466 if (sqe) {
4467 ret = io_fadvise_prep(req, sqe);
4468 if (ret)
4469 break;
4470 }
4471 ret = io_fadvise(req, nxt, force_nonblock);
4472 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07004473 case IORING_OP_MADVISE:
4474 if (sqe) {
4475 ret = io_madvise_prep(req, sqe);
4476 if (ret)
4477 break;
4478 }
4479 ret = io_madvise(req, nxt, force_nonblock);
4480 break;
Jens Axboecebdb982020-01-08 17:59:24 -07004481 case IORING_OP_OPENAT2:
4482 if (sqe) {
4483 ret = io_openat2_prep(req, sqe);
4484 if (ret)
4485 break;
4486 }
4487 ret = io_openat2(req, nxt, force_nonblock);
4488 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004489 case IORING_OP_EPOLL_CTL:
4490 if (sqe) {
4491 ret = io_epoll_ctl_prep(req, sqe);
4492 if (ret)
4493 break;
4494 }
4495 ret = io_epoll_ctl(req, nxt, force_nonblock);
4496 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004497 default:
4498 ret = -EINVAL;
4499 break;
4500 }
4501
Jens Axboedef596e2019-01-09 08:59:42 -07004502 if (ret)
4503 return ret;
4504
4505 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe11ba8202020-01-15 21:51:17 -07004506 const bool in_async = io_wq_current_is_worker();
4507
Jens Axboe9e645e112019-05-10 16:07:28 -06004508 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07004509 return -EAGAIN;
4510
Jens Axboe11ba8202020-01-15 21:51:17 -07004511 /* workqueue context doesn't hold uring_lock, grab it now */
4512 if (in_async)
4513 mutex_lock(&ctx->uring_lock);
4514
Jens Axboedef596e2019-01-09 08:59:42 -07004515 io_iopoll_req_issued(req);
Jens Axboe11ba8202020-01-15 21:51:17 -07004516
4517 if (in_async)
4518 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07004519 }
4520
4521 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004522}
4523
Jens Axboe561fb042019-10-24 07:25:42 -06004524static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboe31b51512019-01-18 22:56:34 -07004525{
Jens Axboe561fb042019-10-24 07:25:42 -06004526 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004527 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe561fb042019-10-24 07:25:42 -06004528 struct io_kiocb *nxt = NULL;
4529 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004530
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07004531 /* if NO_CANCEL is set, we must still run the work */
4532 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
4533 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06004534 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07004535 }
Jens Axboe31b51512019-01-18 22:56:34 -07004536
Jens Axboe561fb042019-10-24 07:25:42 -06004537 if (!ret) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004538 req->in_async = true;
Jens Axboe561fb042019-10-24 07:25:42 -06004539 do {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004540 ret = io_issue_sqe(req, NULL, &nxt, false);
Jens Axboe561fb042019-10-24 07:25:42 -06004541 /*
4542 * We can get EAGAIN for polled IO even though we're
4543 * forcing a sync submission from here, since we can't
4544 * wait for request slots on the block side.
4545 */
4546 if (ret != -EAGAIN)
4547 break;
4548 cond_resched();
4549 } while (1);
4550 }
Jens Axboe31b51512019-01-18 22:56:34 -07004551
Jens Axboe561fb042019-10-24 07:25:42 -06004552 /* drop submission reference */
Jackie Liuec9c02a2019-11-08 23:50:36 +08004553 io_put_req(req);
Jens Axboe817869d2019-04-30 14:44:05 -06004554
Jens Axboe561fb042019-10-24 07:25:42 -06004555 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004556 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07004557 io_cqring_add_event(req, ret);
Jens Axboe817869d2019-04-30 14:44:05 -06004558 io_put_req(req);
Jens Axboeedafcce2019-01-09 09:16:05 -07004559 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004560
Jens Axboe561fb042019-10-24 07:25:42 -06004561 /* if a dependent link is ready, pass it back */
Jens Axboe78912932020-01-14 22:09:06 -07004562 if (!ret && nxt)
4563 io_wq_assign_next(workptr, nxt);
Jens Axboe31b51512019-01-18 22:56:34 -07004564}
Jens Axboe2b188cc2019-01-07 10:46:33 -07004565
Jens Axboe15b71ab2019-12-11 11:20:36 -07004566static int io_req_needs_file(struct io_kiocb *req, int fd)
Jens Axboe9e3aa612019-12-11 15:55:43 -07004567{
Jens Axboed3656342019-12-18 09:50:26 -07004568 if (!io_op_defs[req->opcode].needs_file)
Jens Axboe9e3aa612019-12-11 15:55:43 -07004569 return 0;
Jens Axboe0b5faf62020-02-06 21:42:51 -07004570 if ((fd == -1 || fd == AT_FDCWD) && io_op_defs[req->opcode].fd_non_neg)
Jens Axboed3656342019-12-18 09:50:26 -07004571 return 0;
4572 return 1;
Jens Axboe09bb8392019-03-13 12:39:28 -06004573}
4574
Jens Axboe65e19f52019-10-26 07:20:21 -06004575static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
4576 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06004577{
Jens Axboe65e19f52019-10-26 07:20:21 -06004578 struct fixed_file_table *table;
4579
Jens Axboe05f3fb32019-12-09 11:22:50 -07004580 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
4581 return table->files[index & IORING_FILE_TABLE_MASK];;
Jens Axboe65e19f52019-10-26 07:20:21 -06004582}
4583
Jens Axboe3529d8c2019-12-19 18:24:38 -07004584static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
4585 const struct io_uring_sqe *sqe)
Jens Axboe09bb8392019-03-13 12:39:28 -06004586{
Jackie Liua197f662019-11-08 08:09:12 -07004587 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe09bb8392019-03-13 12:39:28 -06004588 unsigned flags;
Jens Axboed3656342019-12-18 09:50:26 -07004589 int fd;
Jens Axboe09bb8392019-03-13 12:39:28 -06004590
Jens Axboe3529d8c2019-12-19 18:24:38 -07004591 flags = READ_ONCE(sqe->flags);
4592 fd = READ_ONCE(sqe->fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06004593
Jens Axboed3656342019-12-18 09:50:26 -07004594 if (!io_req_needs_file(req, fd))
4595 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06004596
4597 if (flags & IOSQE_FIXED_FILE) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07004598 if (unlikely(!ctx->file_data ||
Jens Axboe09bb8392019-03-13 12:39:28 -06004599 (unsigned) fd >= ctx->nr_user_files))
4600 return -EBADF;
Jens Axboeb7620122019-10-26 07:22:55 -06004601 fd = array_index_nospec(fd, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06004602 req->file = io_file_from_index(ctx, fd);
4603 if (!req->file)
Jens Axboe08a45172019-10-03 08:11:03 -06004604 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06004605 req->flags |= REQ_F_FIXED_FILE;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004606 percpu_ref_get(&ctx->file_data->refs);
Jens Axboe09bb8392019-03-13 12:39:28 -06004607 } else {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004608 if (req->needs_fixed_file)
Jens Axboe09bb8392019-03-13 12:39:28 -06004609 return -EBADF;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004610 trace_io_uring_file_get(ctx, fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06004611 req->file = io_file_get(state, fd);
4612 if (unlikely(!req->file))
4613 return -EBADF;
4614 }
4615
4616 return 0;
4617}
4618
Jackie Liua197f662019-11-08 08:09:12 -07004619static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004620{
Jens Axboefcb323c2019-10-24 12:39:47 -06004621 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07004622 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06004623
Jens Axboef86cd202020-01-29 13:46:44 -07004624 if (req->work.files)
4625 return 0;
Pavel Begunkovb14cca02020-01-17 04:45:59 +03004626 if (!ctx->ring_file)
Jens Axboeb5dba592019-12-11 14:02:38 -07004627 return -EBADF;
4628
Jens Axboefcb323c2019-10-24 12:39:47 -06004629 rcu_read_lock();
4630 spin_lock_irq(&ctx->inflight_lock);
4631 /*
4632 * We use the f_ops->flush() handler to ensure that we can flush
4633 * out work accessing these files if the fd is closed. Check if
4634 * the fd has changed since we started down this path, and disallow
4635 * this operation if it has.
4636 */
Pavel Begunkovb14cca02020-01-17 04:45:59 +03004637 if (fcheck(ctx->ring_fd) == ctx->ring_file) {
Jens Axboefcb323c2019-10-24 12:39:47 -06004638 list_add(&req->inflight_entry, &ctx->inflight_list);
4639 req->flags |= REQ_F_INFLIGHT;
4640 req->work.files = current->files;
4641 ret = 0;
4642 }
4643 spin_unlock_irq(&ctx->inflight_lock);
4644 rcu_read_unlock();
4645
4646 return ret;
4647}
4648
Jens Axboe2665abf2019-11-05 12:40:47 -07004649static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
4650{
Jens Axboead8a48a2019-11-15 08:49:11 -07004651 struct io_timeout_data *data = container_of(timer,
4652 struct io_timeout_data, timer);
4653 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07004654 struct io_ring_ctx *ctx = req->ctx;
4655 struct io_kiocb *prev = NULL;
4656 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07004657
4658 spin_lock_irqsave(&ctx->completion_lock, flags);
4659
4660 /*
4661 * We don't expect the list to be empty, that will only happen if we
4662 * race with the completion of the linked work.
4663 */
Pavel Begunkov44932332019-12-05 16:16:35 +03004664 if (!list_empty(&req->link_list)) {
4665 prev = list_entry(req->link_list.prev, struct io_kiocb,
4666 link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07004667 if (refcount_inc_not_zero(&prev->refs)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03004668 list_del_init(&req->link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07004669 prev->flags &= ~REQ_F_LINK_TIMEOUT;
4670 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07004671 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07004672 }
4673
4674 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4675
4676 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004677 req_set_fail_links(prev);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004678 io_async_find_and_cancel(ctx, req, prev->user_data, NULL,
4679 -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07004680 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07004681 } else {
4682 io_cqring_add_event(req, -ETIME);
4683 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07004684 }
Jens Axboe2665abf2019-11-05 12:40:47 -07004685 return HRTIMER_NORESTART;
4686}
4687
Jens Axboead8a48a2019-11-15 08:49:11 -07004688static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07004689{
Jens Axboe76a46e02019-11-10 23:34:16 -07004690 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07004691
Jens Axboe76a46e02019-11-10 23:34:16 -07004692 /*
4693 * If the list is now empty, then our linked request finished before
4694 * we got a chance to setup the timer
4695 */
4696 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov44932332019-12-05 16:16:35 +03004697 if (!list_empty(&req->link_list)) {
Jens Axboe2d283902019-12-04 11:08:05 -07004698 struct io_timeout_data *data = &req->io->timeout;
Jens Axboe94ae5e72019-11-14 19:39:52 -07004699
Jens Axboead8a48a2019-11-15 08:49:11 -07004700 data->timer.function = io_link_timeout_fn;
4701 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
4702 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07004703 }
Jens Axboe76a46e02019-11-10 23:34:16 -07004704 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07004705
Jens Axboe2665abf2019-11-05 12:40:47 -07004706 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07004707 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07004708}
4709
Jens Axboead8a48a2019-11-15 08:49:11 -07004710static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07004711{
4712 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004713
Jens Axboe2665abf2019-11-05 12:40:47 -07004714 if (!(req->flags & REQ_F_LINK))
4715 return NULL;
4716
Pavel Begunkov44932332019-12-05 16:16:35 +03004717 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
4718 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07004719 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07004720 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07004721
Jens Axboe76a46e02019-11-10 23:34:16 -07004722 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07004723 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07004724}
4725
Jens Axboe3529d8c2019-12-19 18:24:38 -07004726static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004727{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004728 struct io_kiocb *linked_timeout;
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004729 struct io_kiocb *nxt = NULL;
Jens Axboe193155c2020-02-22 23:22:19 -07004730 const struct cred *old_creds = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004731 int ret;
4732
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004733again:
4734 linked_timeout = io_prep_linked_timeout(req);
4735
Jens Axboe193155c2020-02-22 23:22:19 -07004736 if (req->work.creds && req->work.creds != current_cred()) {
4737 if (old_creds)
4738 revert_creds(old_creds);
4739 if (old_creds == req->work.creds)
4740 old_creds = NULL; /* restored original creds */
4741 else
4742 old_creds = override_creds(req->work.creds);
4743 }
4744
Jens Axboe3529d8c2019-12-19 18:24:38 -07004745 ret = io_issue_sqe(req, sqe, &nxt, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06004746
4747 /*
4748 * We async punt it if the file wasn't marked NOWAIT, or if the file
4749 * doesn't support non-blocking read/write attempts
4750 */
4751 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
4752 (req->flags & REQ_F_MUST_PUNT))) {
Pavel Begunkov86a761f2020-01-22 23:09:36 +03004753punt:
Jens Axboef86cd202020-01-29 13:46:44 -07004754 if (io_op_defs[req->opcode].file_table) {
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03004755 ret = io_grab_files(req);
4756 if (ret)
4757 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004758 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03004759
4760 /*
4761 * Queued up for async execution, worker will release
4762 * submit reference when the iocb is actually submitted.
4763 */
4764 io_queue_async_work(req);
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004765 goto done_req;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004766 }
Jens Axboee65ef562019-03-12 10:16:44 -06004767
Jens Axboefcb323c2019-10-24 12:39:47 -06004768err:
Jens Axboee65ef562019-03-12 10:16:44 -06004769 /* drop submission reference */
Jens Axboe2a44f462020-02-25 13:25:41 -07004770 io_put_req_find_next(req, &nxt);
Jens Axboee65ef562019-03-12 10:16:44 -06004771
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004772 if (linked_timeout) {
Jens Axboe76a46e02019-11-10 23:34:16 -07004773 if (!ret)
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004774 io_queue_linked_timeout(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07004775 else
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004776 io_put_req(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07004777 }
4778
Jens Axboee65ef562019-03-12 10:16:44 -06004779 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06004780 if (ret) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07004781 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004782 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06004783 io_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06004784 }
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004785done_req:
4786 if (nxt) {
4787 req = nxt;
4788 nxt = NULL;
Pavel Begunkov86a761f2020-01-22 23:09:36 +03004789
4790 if (req->flags & REQ_F_FORCE_ASYNC)
4791 goto punt;
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004792 goto again;
4793 }
Jens Axboe193155c2020-02-22 23:22:19 -07004794 if (old_creds)
4795 revert_creds(old_creds);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004796}
4797
Jens Axboe3529d8c2019-12-19 18:24:38 -07004798static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jackie Liu4fe2c962019-09-09 20:50:40 +08004799{
4800 int ret;
4801
Jens Axboe3529d8c2019-12-19 18:24:38 -07004802 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004803 if (ret) {
4804 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03004805fail_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004806 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004807 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07004808 io_double_put_req(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004809 }
Pavel Begunkov25508782019-12-30 21:24:47 +03004810 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Pavel Begunkov11185912020-01-22 23:09:35 +03004811 ret = io_req_defer_prep(req, sqe);
4812 if (unlikely(ret < 0))
4813 goto fail_req;
Jens Axboece35a472019-12-17 08:04:44 -07004814 /*
4815 * Never try inline submit of IOSQE_ASYNC is set, go straight
4816 * to async execution.
4817 */
4818 req->work.flags |= IO_WQ_WORK_CONCURRENT;
4819 io_queue_async_work(req);
4820 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004821 __io_queue_sqe(req, sqe);
Jens Axboece35a472019-12-17 08:04:44 -07004822 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08004823}
4824
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004825static inline void io_queue_link_head(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08004826{
Jens Axboe94ae5e72019-11-14 19:39:52 -07004827 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004828 io_cqring_add_event(req, -ECANCELED);
4829 io_double_put_req(req);
4830 } else
Jens Axboe3529d8c2019-12-19 18:24:38 -07004831 io_queue_sqe(req, NULL);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004832}
4833
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004834#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
Jens Axboece35a472019-12-17 08:04:44 -07004835 IOSQE_IO_HARDLINK | IOSQE_ASYNC)
Jens Axboe9e645e112019-05-10 16:07:28 -06004836
Jens Axboe3529d8c2019-12-19 18:24:38 -07004837static bool io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
4838 struct io_submit_state *state, struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06004839{
Jackie Liua197f662019-11-08 08:09:12 -07004840 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004841 unsigned int sqe_flags;
Jens Axboe75c6a032020-01-28 10:15:23 -07004842 int ret, id;
Jens Axboe9e645e112019-05-10 16:07:28 -06004843
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004844 sqe_flags = READ_ONCE(sqe->flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06004845
4846 /* enforce forwards compatibility on users */
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004847 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06004848 ret = -EINVAL;
Pavel Begunkov196be952019-11-07 01:41:06 +03004849 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06004850 }
4851
Jens Axboe75c6a032020-01-28 10:15:23 -07004852 id = READ_ONCE(sqe->personality);
4853 if (id) {
Jens Axboe193155c2020-02-22 23:22:19 -07004854 req->work.creds = idr_find(&ctx->personality_idr, id);
4855 if (unlikely(!req->work.creds)) {
Jens Axboe75c6a032020-01-28 10:15:23 -07004856 ret = -EINVAL;
4857 goto err_req;
4858 }
Jens Axboe193155c2020-02-22 23:22:19 -07004859 get_cred(req->work.creds);
Jens Axboe75c6a032020-01-28 10:15:23 -07004860 }
4861
Pavel Begunkov6b47ee62020-01-18 20:22:41 +03004862 /* same numerical values with corresponding REQ_F_*, safe to copy */
4863 req->flags |= sqe_flags & (IOSQE_IO_DRAIN|IOSQE_IO_HARDLINK|
4864 IOSQE_ASYNC);
Jens Axboe9e645e112019-05-10 16:07:28 -06004865
Jens Axboe3529d8c2019-12-19 18:24:38 -07004866 ret = io_req_set_file(state, req, sqe);
Jens Axboe9e645e112019-05-10 16:07:28 -06004867 if (unlikely(ret)) {
4868err_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004869 io_cqring_add_event(req, ret);
4870 io_double_put_req(req);
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004871 return false;
Jens Axboe9e645e112019-05-10 16:07:28 -06004872 }
4873
Jens Axboe9e645e112019-05-10 16:07:28 -06004874 /*
4875 * If we already have a head request, queue this one for async
4876 * submittal once the head completes. If we don't have a head but
4877 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
4878 * submitted sync once the chain is complete. If none of those
4879 * conditions are true (normal request), then just queue it.
4880 */
4881 if (*link) {
Pavel Begunkov9d763772019-12-17 02:22:07 +03004882 struct io_kiocb *head = *link;
Jens Axboe9e645e112019-05-10 16:07:28 -06004883
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03004884 /*
4885 * Taking sequential execution of a link, draining both sides
4886 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
4887 * requests in the link. So, it drains the head and the
4888 * next after the link request. The last one is done via
4889 * drain_next flag to persist the effect across calls.
4890 */
Pavel Begunkov711be032020-01-17 03:57:59 +03004891 if (sqe_flags & IOSQE_IO_DRAIN) {
4892 head->flags |= REQ_F_IO_DRAIN;
4893 ctx->drain_next = 1;
4894 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004895 if (io_alloc_async_ctx(req)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06004896 ret = -EAGAIN;
4897 goto err_req;
4898 }
4899
Jens Axboe3529d8c2019-12-19 18:24:38 -07004900 ret = io_req_defer_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07004901 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004902 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03004903 head->flags |= REQ_F_FAIL_LINK;
Jens Axboef67676d2019-12-02 11:03:47 -07004904 goto err_req;
Jens Axboe2d283902019-12-04 11:08:05 -07004905 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03004906 trace_io_uring_link(ctx, req, head);
4907 list_add_tail(&req->link_list, &head->link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06004908
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004909 /* last request of a link, enqueue the link */
4910 if (!(sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK))) {
4911 io_queue_link_head(head);
4912 *link = NULL;
4913 }
Jens Axboe9e645e112019-05-10 16:07:28 -06004914 } else {
Pavel Begunkov711be032020-01-17 03:57:59 +03004915 if (unlikely(ctx->drain_next)) {
4916 req->flags |= REQ_F_IO_DRAIN;
4917 req->ctx->drain_next = 0;
4918 }
4919 if (sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK)) {
4920 req->flags |= REQ_F_LINK;
Pavel Begunkov711be032020-01-17 03:57:59 +03004921 INIT_LIST_HEAD(&req->link_list);
4922 ret = io_req_defer_prep(req, sqe);
4923 if (ret)
4924 req->flags |= REQ_F_FAIL_LINK;
4925 *link = req;
4926 } else {
4927 io_queue_sqe(req, sqe);
4928 }
Jens Axboe9e645e112019-05-10 16:07:28 -06004929 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004930
4931 return true;
Jens Axboe9e645e112019-05-10 16:07:28 -06004932}
4933
Jens Axboe9a56a232019-01-09 09:06:50 -07004934/*
4935 * Batched submission is done, ensure local IO is flushed out.
4936 */
4937static void io_submit_state_end(struct io_submit_state *state)
4938{
4939 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06004940 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07004941 if (state->free_reqs)
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03004942 kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs);
Jens Axboe9a56a232019-01-09 09:06:50 -07004943}
4944
4945/*
4946 * Start submission side cache.
4947 */
4948static void io_submit_state_start(struct io_submit_state *state,
Jackie Liu22efde52019-12-02 17:14:52 +08004949 unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07004950{
4951 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07004952 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07004953 state->file = NULL;
4954 state->ios_left = max_ios;
4955}
4956
Jens Axboe2b188cc2019-01-07 10:46:33 -07004957static void io_commit_sqring(struct io_ring_ctx *ctx)
4958{
Hristo Venev75b28af2019-08-26 17:23:46 +00004959 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004960
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03004961 /*
4962 * Ensure any loads from the SQEs are done at this point,
4963 * since once we write the new head, the application could
4964 * write new data to them.
4965 */
4966 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004967}
4968
4969/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07004970 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07004971 * that is mapped by userspace. This means that care needs to be taken to
4972 * ensure that reads are stable, as we cannot rely on userspace always
4973 * being a good citizen. If members of the sqe are validated and then later
4974 * used, it's important that those reads are done through READ_ONCE() to
4975 * prevent a re-load down the line.
4976 */
Jens Axboe3529d8c2019-12-19 18:24:38 -07004977static bool io_get_sqring(struct io_ring_ctx *ctx, struct io_kiocb *req,
4978 const struct io_uring_sqe **sqe_ptr)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004979{
Hristo Venev75b28af2019-08-26 17:23:46 +00004980 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004981 unsigned head;
4982
4983 /*
4984 * The cached sq head (or cq tail) serves two purposes:
4985 *
4986 * 1) allows us to batch the cost of updating the user visible
4987 * head updates.
4988 * 2) allows the kernel side to track the head on its own, even
4989 * though the application is the one updating it.
4990 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03004991 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03004992 if (likely(head < ctx->sq_entries)) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004993 /*
4994 * All io need record the previous position, if LINK vs DARIN,
4995 * it can be used to mark the position of the first IO in the
4996 * link list.
4997 */
4998 req->sequence = ctx->cached_sq_head;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004999 *sqe_ptr = &ctx->sq_sqes[head];
5000 req->opcode = READ_ONCE((*sqe_ptr)->opcode);
5001 req->user_data = READ_ONCE((*sqe_ptr)->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005002 ctx->cached_sq_head++;
5003 return true;
5004 }
5005
5006 /* drop invalid entries */
5007 ctx->cached_sq_head++;
Jens Axboe498ccd92019-10-25 10:04:25 -06005008 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03005009 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005010 return false;
5011}
5012
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005013static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkovae9428c2019-11-06 00:22:14 +03005014 struct file *ring_file, int ring_fd,
5015 struct mm_struct **mm, bool async)
Jens Axboe6c271ce2019-01-10 11:22:30 -07005016{
5017 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06005018 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06005019 int i, submitted = 0;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03005020 bool mm_fault = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005021
Jens Axboec4a2ed72019-11-21 21:01:26 -07005022 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07005023 if (test_bit(0, &ctx->sq_check_overflow)) {
5024 if (!list_empty(&ctx->cq_overflow_list) &&
5025 !io_cqring_overflow_flush(ctx, false))
5026 return -EBUSY;
5027 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07005028
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03005029 /* make sure SQ entry isn't read before tail */
5030 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03005031
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03005032 if (!percpu_ref_tryget_many(&ctx->refs, nr))
5033 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005034
5035 if (nr > IO_PLUG_THRESHOLD) {
Jackie Liu22efde52019-12-02 17:14:52 +08005036 io_submit_state_start(&state, nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005037 statep = &state;
5038 }
5039
Pavel Begunkovb14cca02020-01-17 04:45:59 +03005040 ctx->ring_fd = ring_fd;
5041 ctx->ring_file = ring_file;
5042
Jens Axboe6c271ce2019-01-10 11:22:30 -07005043 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07005044 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03005045 struct io_kiocb *req;
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005046 int err;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005047
Pavel Begunkov196be952019-11-07 01:41:06 +03005048 req = io_get_req(ctx, statep);
5049 if (unlikely(!req)) {
5050 if (!submitted)
5051 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005052 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06005053 }
Jens Axboe3529d8c2019-12-19 18:24:38 -07005054 if (!io_get_sqring(ctx, req, &sqe)) {
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03005055 __io_req_do_free(req);
Pavel Begunkov196be952019-11-07 01:41:06 +03005056 break;
5057 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005058
Jens Axboed3656342019-12-18 09:50:26 -07005059 /* will complete beyond this point, count as submitted */
5060 submitted++;
5061
5062 if (unlikely(req->opcode >= IORING_OP_LAST)) {
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005063 err = -EINVAL;
5064fail_req:
5065 io_cqring_add_event(req, err);
Jens Axboed3656342019-12-18 09:50:26 -07005066 io_double_put_req(req);
5067 break;
5068 }
5069
5070 if (io_op_defs[req->opcode].needs_mm && !*mm) {
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03005071 mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm);
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005072 if (unlikely(mm_fault)) {
5073 err = -EFAULT;
5074 goto fail_req;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03005075 }
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005076 use_mm(ctx->sqo_mm);
5077 *mm = ctx->sqo_mm;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03005078 }
5079
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03005080 req->in_async = async;
5081 req->needs_fixed_file = async;
Jens Axboe354420f2020-01-08 18:55:15 -07005082 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
5083 true, async);
Jens Axboe3529d8c2019-12-19 18:24:38 -07005084 if (!io_submit_sqe(req, sqe, statep, &link))
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03005085 break;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005086 }
5087
Pavel Begunkov9466f432020-01-25 22:34:01 +03005088 if (unlikely(submitted != nr)) {
5089 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
5090
5091 percpu_ref_put_many(&ctx->refs, nr - ref_used);
5092 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005093 if (link)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03005094 io_queue_link_head(link);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005095 if (statep)
5096 io_submit_state_end(&state);
5097
Pavel Begunkovae9428c2019-11-06 00:22:14 +03005098 /* Commit SQ ring head once we've consumed and submitted all SQEs */
5099 io_commit_sqring(ctx);
5100
Jens Axboe6c271ce2019-01-10 11:22:30 -07005101 return submitted;
5102}
5103
5104static int io_sq_thread(void *data)
5105{
Jens Axboe6c271ce2019-01-10 11:22:30 -07005106 struct io_ring_ctx *ctx = data;
5107 struct mm_struct *cur_mm = NULL;
Jens Axboe181e4482019-11-25 08:52:30 -07005108 const struct cred *old_cred;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005109 mm_segment_t old_fs;
5110 DEFINE_WAIT(wait);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005111 unsigned long timeout;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005112 int ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005113
Jens Axboe206aefd2019-11-07 18:27:42 -07005114 complete(&ctx->completions[1]);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08005115
Jens Axboe6c271ce2019-01-10 11:22:30 -07005116 old_fs = get_fs();
5117 set_fs(USER_DS);
Jens Axboe181e4482019-11-25 08:52:30 -07005118 old_cred = override_creds(ctx->creds);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005119
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005120 timeout = jiffies + ctx->sq_thread_idle;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005121 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005122 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005123
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005124 if (!list_empty(&ctx->poll_list)) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07005125 unsigned nr_events = 0;
5126
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005127 mutex_lock(&ctx->uring_lock);
5128 if (!list_empty(&ctx->poll_list))
5129 io_iopoll_getevents(ctx, &nr_events, 0);
5130 else
Jens Axboe6c271ce2019-01-10 11:22:30 -07005131 timeout = jiffies + ctx->sq_thread_idle;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005132 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005133 }
5134
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005135 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07005136
5137 /*
5138 * If submit got -EBUSY, flag us as needing the application
5139 * to enter the kernel to reap and flush events.
5140 */
5141 if (!to_submit || ret == -EBUSY) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07005142 /*
Stefano Garzarella7143b5a2020-02-21 16:42:16 +01005143 * Drop cur_mm before scheduling, we can't hold it for
5144 * long periods (or over schedule()). Do this before
5145 * adding ourselves to the waitqueue, as the unuse/drop
5146 * may sleep.
5147 */
5148 if (cur_mm) {
5149 unuse_mm(cur_mm);
5150 mmput(cur_mm);
5151 cur_mm = NULL;
5152 }
5153
5154 /*
Jens Axboe6c271ce2019-01-10 11:22:30 -07005155 * We're polling. If we're within the defined idle
5156 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07005157 * to sleep. The exception is if we got EBUSY doing
5158 * more IO, we should wait for the application to
5159 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07005160 */
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005161 if (!list_empty(&ctx->poll_list) ||
Jens Axboedf069d82020-02-04 16:48:34 -07005162 (!time_after(jiffies, timeout) && ret != -EBUSY &&
5163 !percpu_ref_is_dying(&ctx->refs))) {
Jens Axboe9831a902019-09-19 09:48:55 -06005164 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07005165 continue;
5166 }
5167
Jens Axboe6c271ce2019-01-10 11:22:30 -07005168 prepare_to_wait(&ctx->sqo_wait, &wait,
5169 TASK_INTERRUPTIBLE);
5170
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005171 /*
5172 * While doing polled IO, before going to sleep, we need
5173 * to check if there are new reqs added to poll_list, it
5174 * is because reqs may have been punted to io worker and
5175 * will be added to poll_list later, hence check the
5176 * poll_list again.
5177 */
5178 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
5179 !list_empty_careful(&ctx->poll_list)) {
5180 finish_wait(&ctx->sqo_wait, &wait);
5181 continue;
5182 }
5183
Jens Axboe6c271ce2019-01-10 11:22:30 -07005184 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00005185 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02005186 /* make sure to read SQ tail after writing flags */
5187 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07005188
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005189 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07005190 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005191 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07005192 finish_wait(&ctx->sqo_wait, &wait);
5193 break;
5194 }
5195 if (signal_pending(current))
5196 flush_signals(current);
5197 schedule();
5198 finish_wait(&ctx->sqo_wait, &wait);
5199
Hristo Venev75b28af2019-08-26 17:23:46 +00005200 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005201 continue;
5202 }
5203 finish_wait(&ctx->sqo_wait, &wait);
5204
Hristo Venev75b28af2019-08-26 17:23:46 +00005205 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005206 }
5207
Jens Axboe8a4955f2019-12-09 14:52:35 -07005208 mutex_lock(&ctx->uring_lock);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005209 ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true);
Jens Axboe8a4955f2019-12-09 14:52:35 -07005210 mutex_unlock(&ctx->uring_lock);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005211 timeout = jiffies + ctx->sq_thread_idle;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005212 }
5213
5214 set_fs(old_fs);
5215 if (cur_mm) {
5216 unuse_mm(cur_mm);
5217 mmput(cur_mm);
5218 }
Jens Axboe181e4482019-11-25 08:52:30 -07005219 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06005220
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005221 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06005222
Jens Axboe6c271ce2019-01-10 11:22:30 -07005223 return 0;
5224}
5225
Jens Axboebda52162019-09-24 13:47:15 -06005226struct io_wait_queue {
5227 struct wait_queue_entry wq;
5228 struct io_ring_ctx *ctx;
5229 unsigned to_wait;
5230 unsigned nr_timeouts;
5231};
5232
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005233static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06005234{
5235 struct io_ring_ctx *ctx = iowq->ctx;
5236
5237 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08005238 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06005239 * started waiting. For timeouts, we always want to return to userspace,
5240 * regardless of event count.
5241 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005242 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06005243 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
5244}
5245
5246static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
5247 int wake_flags, void *key)
5248{
5249 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
5250 wq);
5251
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005252 /* use noflush == true, as we can't safely rely on locking context */
5253 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06005254 return -1;
5255
5256 return autoremove_wake_function(curr, mode, wake_flags, key);
5257}
5258
Jens Axboe2b188cc2019-01-07 10:46:33 -07005259/*
5260 * Wait until events become available, if we don't already have some. The
5261 * application must reap them itself, as they reside on the shared cq ring.
5262 */
5263static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
5264 const sigset_t __user *sig, size_t sigsz)
5265{
Jens Axboebda52162019-09-24 13:47:15 -06005266 struct io_wait_queue iowq = {
5267 .wq = {
5268 .private = current,
5269 .func = io_wake_function,
5270 .entry = LIST_HEAD_INIT(iowq.wq.entry),
5271 },
5272 .ctx = ctx,
5273 .to_wait = min_events,
5274 };
Hristo Venev75b28af2019-08-26 17:23:46 +00005275 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005276 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005277
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005278 if (io_cqring_events(ctx, false) >= min_events)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005279 return 0;
5280
5281 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005282#ifdef CONFIG_COMPAT
5283 if (in_compat_syscall())
5284 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07005285 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005286 else
5287#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07005288 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005289
Jens Axboe2b188cc2019-01-07 10:46:33 -07005290 if (ret)
5291 return ret;
5292 }
5293
Jens Axboebda52162019-09-24 13:47:15 -06005294 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02005295 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06005296 do {
5297 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
5298 TASK_INTERRUPTIBLE);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005299 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06005300 break;
5301 schedule();
5302 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005303 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06005304 break;
5305 }
5306 } while (1);
5307 finish_wait(&ctx->wait, &iowq.wq);
5308
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005309 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005310
Hristo Venev75b28af2019-08-26 17:23:46 +00005311 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005312}
5313
Jens Axboe6b063142019-01-10 22:13:58 -07005314static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
5315{
5316#if defined(CONFIG_UNIX)
5317 if (ctx->ring_sock) {
5318 struct sock *sock = ctx->ring_sock->sk;
5319 struct sk_buff *skb;
5320
5321 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
5322 kfree_skb(skb);
5323 }
5324#else
5325 int i;
5326
Jens Axboe65e19f52019-10-26 07:20:21 -06005327 for (i = 0; i < ctx->nr_user_files; i++) {
5328 struct file *file;
5329
5330 file = io_file_from_index(ctx, i);
5331 if (file)
5332 fput(file);
5333 }
Jens Axboe6b063142019-01-10 22:13:58 -07005334#endif
5335}
5336
Jens Axboe05f3fb32019-12-09 11:22:50 -07005337static void io_file_ref_kill(struct percpu_ref *ref)
5338{
5339 struct fixed_file_data *data;
5340
5341 data = container_of(ref, struct fixed_file_data, refs);
5342 complete(&data->done);
5343}
5344
Jens Axboe6b063142019-01-10 22:13:58 -07005345static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
5346{
Jens Axboe05f3fb32019-12-09 11:22:50 -07005347 struct fixed_file_data *data = ctx->file_data;
Jens Axboe65e19f52019-10-26 07:20:21 -06005348 unsigned nr_tables, i;
5349
Jens Axboe05f3fb32019-12-09 11:22:50 -07005350 if (!data)
Jens Axboe6b063142019-01-10 22:13:58 -07005351 return -ENXIO;
5352
Jens Axboe05f3fb32019-12-09 11:22:50 -07005353 percpu_ref_kill_and_confirm(&data->refs, io_file_ref_kill);
Jens Axboee46a7952020-01-17 11:15:34 -07005354 flush_work(&data->ref_work);
Jens Axboe2faf8522020-02-04 19:54:55 -07005355 wait_for_completion(&data->done);
5356 io_ring_file_ref_flush(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005357 percpu_ref_exit(&data->refs);
5358
Jens Axboe6b063142019-01-10 22:13:58 -07005359 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06005360 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
5361 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005362 kfree(data->table[i].files);
5363 kfree(data->table);
5364 kfree(data);
5365 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07005366 ctx->nr_user_files = 0;
5367 return 0;
5368}
5369
Jens Axboe6c271ce2019-01-10 11:22:30 -07005370static void io_sq_thread_stop(struct io_ring_ctx *ctx)
5371{
5372 if (ctx->sqo_thread) {
Jens Axboe206aefd2019-11-07 18:27:42 -07005373 wait_for_completion(&ctx->completions[1]);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005374 /*
5375 * The park is a bit of a work-around, without it we get
5376 * warning spews on shutdown with SQPOLL set and affinity
5377 * set to a single CPU.
5378 */
Jens Axboe06058632019-04-13 09:26:03 -06005379 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005380 kthread_stop(ctx->sqo_thread);
5381 ctx->sqo_thread = NULL;
5382 }
5383}
5384
Jens Axboe6b063142019-01-10 22:13:58 -07005385static void io_finish_async(struct io_ring_ctx *ctx)
5386{
Jens Axboe6c271ce2019-01-10 11:22:30 -07005387 io_sq_thread_stop(ctx);
5388
Jens Axboe561fb042019-10-24 07:25:42 -06005389 if (ctx->io_wq) {
5390 io_wq_destroy(ctx->io_wq);
5391 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07005392 }
5393}
5394
5395#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07005396/*
5397 * Ensure the UNIX gc is aware of our file set, so we are certain that
5398 * the io_uring can be safely unregistered on process exit, even if we have
5399 * loops in the file referencing.
5400 */
5401static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
5402{
5403 struct sock *sk = ctx->ring_sock->sk;
5404 struct scm_fp_list *fpl;
5405 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06005406 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07005407
5408 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
5409 unsigned long inflight = ctx->user->unix_inflight + nr;
5410
5411 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
5412 return -EMFILE;
5413 }
5414
5415 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
5416 if (!fpl)
5417 return -ENOMEM;
5418
5419 skb = alloc_skb(0, GFP_KERNEL);
5420 if (!skb) {
5421 kfree(fpl);
5422 return -ENOMEM;
5423 }
5424
5425 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07005426
Jens Axboe08a45172019-10-03 08:11:03 -06005427 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07005428 fpl->user = get_uid(ctx->user);
5429 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005430 struct file *file = io_file_from_index(ctx, i + offset);
5431
5432 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06005433 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06005434 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06005435 unix_inflight(fpl->user, fpl->fp[nr_files]);
5436 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07005437 }
5438
Jens Axboe08a45172019-10-03 08:11:03 -06005439 if (nr_files) {
5440 fpl->max = SCM_MAX_FD;
5441 fpl->count = nr_files;
5442 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005443 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06005444 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
5445 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07005446
Jens Axboe08a45172019-10-03 08:11:03 -06005447 for (i = 0; i < nr_files; i++)
5448 fput(fpl->fp[i]);
5449 } else {
5450 kfree_skb(skb);
5451 kfree(fpl);
5452 }
Jens Axboe6b063142019-01-10 22:13:58 -07005453
5454 return 0;
5455}
5456
5457/*
5458 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
5459 * causes regular reference counting to break down. We rely on the UNIX
5460 * garbage collection to take care of this problem for us.
5461 */
5462static int io_sqe_files_scm(struct io_ring_ctx *ctx)
5463{
5464 unsigned left, total;
5465 int ret = 0;
5466
5467 total = 0;
5468 left = ctx->nr_user_files;
5469 while (left) {
5470 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07005471
5472 ret = __io_sqe_files_scm(ctx, this_files, total);
5473 if (ret)
5474 break;
5475 left -= this_files;
5476 total += this_files;
5477 }
5478
5479 if (!ret)
5480 return 0;
5481
5482 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005483 struct file *file = io_file_from_index(ctx, total);
5484
5485 if (file)
5486 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07005487 total++;
5488 }
5489
5490 return ret;
5491}
5492#else
5493static int io_sqe_files_scm(struct io_ring_ctx *ctx)
5494{
5495 return 0;
5496}
5497#endif
5498
Jens Axboe65e19f52019-10-26 07:20:21 -06005499static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
5500 unsigned nr_files)
5501{
5502 int i;
5503
5504 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005505 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06005506 unsigned this_files;
5507
5508 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
5509 table->files = kcalloc(this_files, sizeof(struct file *),
5510 GFP_KERNEL);
5511 if (!table->files)
5512 break;
5513 nr_files -= this_files;
5514 }
5515
5516 if (i == nr_tables)
5517 return 0;
5518
5519 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005520 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06005521 kfree(table->files);
5522 }
5523 return 1;
5524}
5525
Jens Axboe05f3fb32019-12-09 11:22:50 -07005526static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
Jens Axboec3a31e62019-10-03 13:59:56 -06005527{
5528#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06005529 struct sock *sock = ctx->ring_sock->sk;
5530 struct sk_buff_head list, *head = &sock->sk_receive_queue;
5531 struct sk_buff *skb;
5532 int i;
5533
5534 __skb_queue_head_init(&list);
5535
5536 /*
5537 * Find the skb that holds this file in its SCM_RIGHTS. When found,
5538 * remove this entry and rearrange the file array.
5539 */
5540 skb = skb_dequeue(head);
5541 while (skb) {
5542 struct scm_fp_list *fp;
5543
5544 fp = UNIXCB(skb).fp;
5545 for (i = 0; i < fp->count; i++) {
5546 int left;
5547
5548 if (fp->fp[i] != file)
5549 continue;
5550
5551 unix_notinflight(fp->user, fp->fp[i]);
5552 left = fp->count - 1 - i;
5553 if (left) {
5554 memmove(&fp->fp[i], &fp->fp[i + 1],
5555 left * sizeof(struct file *));
5556 }
5557 fp->count--;
5558 if (!fp->count) {
5559 kfree_skb(skb);
5560 skb = NULL;
5561 } else {
5562 __skb_queue_tail(&list, skb);
5563 }
5564 fput(file);
5565 file = NULL;
5566 break;
5567 }
5568
5569 if (!file)
5570 break;
5571
5572 __skb_queue_tail(&list, skb);
5573
5574 skb = skb_dequeue(head);
5575 }
5576
5577 if (skb_peek(&list)) {
5578 spin_lock_irq(&head->lock);
5579 while ((skb = __skb_dequeue(&list)) != NULL)
5580 __skb_queue_tail(head, skb);
5581 spin_unlock_irq(&head->lock);
5582 }
5583#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07005584 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06005585#endif
5586}
5587
Jens Axboe05f3fb32019-12-09 11:22:50 -07005588struct io_file_put {
5589 struct llist_node llist;
5590 struct file *file;
5591 struct completion *done;
5592};
5593
Jens Axboe2faf8522020-02-04 19:54:55 -07005594static void io_ring_file_ref_flush(struct fixed_file_data *data)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005595{
5596 struct io_file_put *pfile, *tmp;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005597 struct llist_node *node;
5598
Jens Axboe05f3fb32019-12-09 11:22:50 -07005599 while ((node = llist_del_all(&data->put_llist)) != NULL) {
5600 llist_for_each_entry_safe(pfile, tmp, node, llist) {
5601 io_ring_file_put(data->ctx, pfile->file);
5602 if (pfile->done)
5603 complete(pfile->done);
5604 else
5605 kfree(pfile);
5606 }
5607 }
Jens Axboe2faf8522020-02-04 19:54:55 -07005608}
Jens Axboe05f3fb32019-12-09 11:22:50 -07005609
Jens Axboe2faf8522020-02-04 19:54:55 -07005610static void io_ring_file_ref_switch(struct work_struct *work)
5611{
5612 struct fixed_file_data *data;
5613
5614 data = container_of(work, struct fixed_file_data, ref_work);
5615 io_ring_file_ref_flush(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005616 percpu_ref_switch_to_percpu(&data->refs);
5617}
5618
5619static void io_file_data_ref_zero(struct percpu_ref *ref)
5620{
5621 struct fixed_file_data *data;
5622
5623 data = container_of(ref, struct fixed_file_data, refs);
5624
Jens Axboe2faf8522020-02-04 19:54:55 -07005625 /*
5626 * We can't safely switch from inside this context, punt to wq. If
5627 * the table ref is going away, the table is being unregistered.
5628 * Don't queue up the async work for that case, the caller will
5629 * handle it.
5630 */
5631 if (!percpu_ref_is_dying(&data->refs))
5632 queue_work(system_wq, &data->ref_work);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005633}
5634
5635static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
5636 unsigned nr_args)
5637{
5638 __s32 __user *fds = (__s32 __user *) arg;
5639 unsigned nr_tables;
5640 struct file *file;
5641 int fd, ret = 0;
5642 unsigned i;
5643
5644 if (ctx->file_data)
5645 return -EBUSY;
5646 if (!nr_args)
5647 return -EINVAL;
5648 if (nr_args > IORING_MAX_FIXED_FILES)
5649 return -EMFILE;
5650
5651 ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
5652 if (!ctx->file_data)
5653 return -ENOMEM;
5654 ctx->file_data->ctx = ctx;
5655 init_completion(&ctx->file_data->done);
5656
5657 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
5658 ctx->file_data->table = kcalloc(nr_tables,
5659 sizeof(struct fixed_file_table),
5660 GFP_KERNEL);
5661 if (!ctx->file_data->table) {
5662 kfree(ctx->file_data);
5663 ctx->file_data = NULL;
5664 return -ENOMEM;
5665 }
5666
5667 if (percpu_ref_init(&ctx->file_data->refs, io_file_data_ref_zero,
5668 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
5669 kfree(ctx->file_data->table);
5670 kfree(ctx->file_data);
5671 ctx->file_data = NULL;
5672 return -ENOMEM;
5673 }
5674 ctx->file_data->put_llist.first = NULL;
5675 INIT_WORK(&ctx->file_data->ref_work, io_ring_file_ref_switch);
5676
5677 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
5678 percpu_ref_exit(&ctx->file_data->refs);
5679 kfree(ctx->file_data->table);
5680 kfree(ctx->file_data);
5681 ctx->file_data = NULL;
5682 return -ENOMEM;
5683 }
5684
5685 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
5686 struct fixed_file_table *table;
5687 unsigned index;
5688
5689 ret = -EFAULT;
5690 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
5691 break;
5692 /* allow sparse sets */
5693 if (fd == -1) {
5694 ret = 0;
5695 continue;
5696 }
5697
5698 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
5699 index = i & IORING_FILE_TABLE_MASK;
5700 file = fget(fd);
5701
5702 ret = -EBADF;
5703 if (!file)
5704 break;
5705
5706 /*
5707 * Don't allow io_uring instances to be registered. If UNIX
5708 * isn't enabled, then this causes a reference cycle and this
5709 * instance can never get freed. If UNIX is enabled we'll
5710 * handle it just fine, but there's still no point in allowing
5711 * a ring fd as it doesn't support regular read/write anyway.
5712 */
5713 if (file->f_op == &io_uring_fops) {
5714 fput(file);
5715 break;
5716 }
5717 ret = 0;
5718 table->files[index] = file;
5719 }
5720
5721 if (ret) {
5722 for (i = 0; i < ctx->nr_user_files; i++) {
5723 file = io_file_from_index(ctx, i);
5724 if (file)
5725 fput(file);
5726 }
5727 for (i = 0; i < nr_tables; i++)
5728 kfree(ctx->file_data->table[i].files);
5729
5730 kfree(ctx->file_data->table);
5731 kfree(ctx->file_data);
5732 ctx->file_data = NULL;
5733 ctx->nr_user_files = 0;
5734 return ret;
5735 }
5736
5737 ret = io_sqe_files_scm(ctx);
5738 if (ret)
5739 io_sqe_files_unregister(ctx);
5740
5741 return ret;
5742}
5743
Jens Axboec3a31e62019-10-03 13:59:56 -06005744static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
5745 int index)
5746{
5747#if defined(CONFIG_UNIX)
5748 struct sock *sock = ctx->ring_sock->sk;
5749 struct sk_buff_head *head = &sock->sk_receive_queue;
5750 struct sk_buff *skb;
5751
5752 /*
5753 * See if we can merge this file into an existing skb SCM_RIGHTS
5754 * file set. If there's no room, fall back to allocating a new skb
5755 * and filling it in.
5756 */
5757 spin_lock_irq(&head->lock);
5758 skb = skb_peek(head);
5759 if (skb) {
5760 struct scm_fp_list *fpl = UNIXCB(skb).fp;
5761
5762 if (fpl->count < SCM_MAX_FD) {
5763 __skb_unlink(skb, head);
5764 spin_unlock_irq(&head->lock);
5765 fpl->fp[fpl->count] = get_file(file);
5766 unix_inflight(fpl->user, fpl->fp[fpl->count]);
5767 fpl->count++;
5768 spin_lock_irq(&head->lock);
5769 __skb_queue_head(head, skb);
5770 } else {
5771 skb = NULL;
5772 }
5773 }
5774 spin_unlock_irq(&head->lock);
5775
5776 if (skb) {
5777 fput(file);
5778 return 0;
5779 }
5780
5781 return __io_sqe_files_scm(ctx, 1, index);
5782#else
5783 return 0;
5784#endif
5785}
5786
Jens Axboe05f3fb32019-12-09 11:22:50 -07005787static void io_atomic_switch(struct percpu_ref *ref)
Jens Axboec3a31e62019-10-03 13:59:56 -06005788{
Jens Axboe05f3fb32019-12-09 11:22:50 -07005789 struct fixed_file_data *data;
5790
Jens Axboedd3db2a2020-02-26 10:23:43 -07005791 /*
5792 * Juggle reference to ensure we hit zero, if needed, so we can
5793 * switch back to percpu mode
5794 */
Jens Axboe05f3fb32019-12-09 11:22:50 -07005795 data = container_of(ref, struct fixed_file_data, refs);
Jens Axboedd3db2a2020-02-26 10:23:43 -07005796 percpu_ref_put(&data->refs);
5797 percpu_ref_get(&data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005798}
5799
5800static bool io_queue_file_removal(struct fixed_file_data *data,
5801 struct file *file)
5802{
5803 struct io_file_put *pfile, pfile_stack;
5804 DECLARE_COMPLETION_ONSTACK(done);
5805
5806 /*
5807 * If we fail allocating the struct we need for doing async reomval
5808 * of this file, just punt to sync and wait for it.
5809 */
5810 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
5811 if (!pfile) {
5812 pfile = &pfile_stack;
5813 pfile->done = &done;
5814 }
5815
5816 pfile->file = file;
5817 llist_add(&pfile->llist, &data->put_llist);
5818
5819 if (pfile == &pfile_stack) {
Jens Axboedd3db2a2020-02-26 10:23:43 -07005820 percpu_ref_switch_to_atomic(&data->refs, io_atomic_switch);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005821 wait_for_completion(&done);
5822 flush_work(&data->ref_work);
5823 return false;
5824 }
5825
5826 return true;
5827}
5828
5829static int __io_sqe_files_update(struct io_ring_ctx *ctx,
5830 struct io_uring_files_update *up,
5831 unsigned nr_args)
5832{
5833 struct fixed_file_data *data = ctx->file_data;
5834 bool ref_switch = false;
5835 struct file *file;
Jens Axboec3a31e62019-10-03 13:59:56 -06005836 __s32 __user *fds;
5837 int fd, i, err;
5838 __u32 done;
5839
Jens Axboe05f3fb32019-12-09 11:22:50 -07005840 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06005841 return -EOVERFLOW;
5842 if (done > ctx->nr_user_files)
5843 return -EINVAL;
5844
5845 done = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005846 fds = u64_to_user_ptr(up->fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06005847 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005848 struct fixed_file_table *table;
5849 unsigned index;
5850
Jens Axboec3a31e62019-10-03 13:59:56 -06005851 err = 0;
5852 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
5853 err = -EFAULT;
5854 break;
5855 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07005856 i = array_index_nospec(up->offset, ctx->nr_user_files);
5857 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
Jens Axboe65e19f52019-10-26 07:20:21 -06005858 index = i & IORING_FILE_TABLE_MASK;
5859 if (table->files[index]) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005860 file = io_file_from_index(ctx, index);
Jens Axboe65e19f52019-10-26 07:20:21 -06005861 table->files[index] = NULL;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005862 if (io_queue_file_removal(data, file))
5863 ref_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06005864 }
5865 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06005866 file = fget(fd);
5867 if (!file) {
5868 err = -EBADF;
5869 break;
5870 }
5871 /*
5872 * Don't allow io_uring instances to be registered. If
5873 * UNIX isn't enabled, then this causes a reference
5874 * cycle and this instance can never get freed. If UNIX
5875 * is enabled we'll handle it just fine, but there's
5876 * still no point in allowing a ring fd as it doesn't
5877 * support regular read/write anyway.
5878 */
5879 if (file->f_op == &io_uring_fops) {
5880 fput(file);
5881 err = -EBADF;
5882 break;
5883 }
Jens Axboe65e19f52019-10-26 07:20:21 -06005884 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06005885 err = io_sqe_file_register(ctx, file, i);
5886 if (err)
5887 break;
5888 }
5889 nr_args--;
5890 done++;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005891 up->offset++;
5892 }
5893
Jens Axboedd3db2a2020-02-26 10:23:43 -07005894 if (ref_switch)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005895 percpu_ref_switch_to_atomic(&data->refs, io_atomic_switch);
Jens Axboec3a31e62019-10-03 13:59:56 -06005896
5897 return done ? done : err;
5898}
Jens Axboe05f3fb32019-12-09 11:22:50 -07005899static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
5900 unsigned nr_args)
5901{
5902 struct io_uring_files_update up;
5903
5904 if (!ctx->file_data)
5905 return -ENXIO;
5906 if (!nr_args)
5907 return -EINVAL;
5908 if (copy_from_user(&up, arg, sizeof(up)))
5909 return -EFAULT;
5910 if (up.resv)
5911 return -EINVAL;
5912
5913 return __io_sqe_files_update(ctx, &up, nr_args);
5914}
Jens Axboec3a31e62019-10-03 13:59:56 -06005915
Jens Axboe7d723062019-11-12 22:31:31 -07005916static void io_put_work(struct io_wq_work *work)
5917{
5918 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
5919
5920 io_put_req(req);
5921}
5922
5923static void io_get_work(struct io_wq_work *work)
5924{
5925 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
5926
5927 refcount_inc(&req->refs);
5928}
5929
Pavel Begunkov24369c22020-01-28 03:15:48 +03005930static int io_init_wq_offload(struct io_ring_ctx *ctx,
5931 struct io_uring_params *p)
5932{
5933 struct io_wq_data data;
5934 struct fd f;
5935 struct io_ring_ctx *ctx_attach;
5936 unsigned int concurrency;
5937 int ret = 0;
5938
5939 data.user = ctx->user;
5940 data.get_work = io_get_work;
5941 data.put_work = io_put_work;
5942
5943 if (!(p->flags & IORING_SETUP_ATTACH_WQ)) {
5944 /* Do QD, or 4 * CPUS, whatever is smallest */
5945 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
5946
5947 ctx->io_wq = io_wq_create(concurrency, &data);
5948 if (IS_ERR(ctx->io_wq)) {
5949 ret = PTR_ERR(ctx->io_wq);
5950 ctx->io_wq = NULL;
5951 }
5952 return ret;
5953 }
5954
5955 f = fdget(p->wq_fd);
5956 if (!f.file)
5957 return -EBADF;
5958
5959 if (f.file->f_op != &io_uring_fops) {
5960 ret = -EINVAL;
5961 goto out_fput;
5962 }
5963
5964 ctx_attach = f.file->private_data;
5965 /* @io_wq is protected by holding the fd */
5966 if (!io_wq_get(ctx_attach->io_wq, &data)) {
5967 ret = -EINVAL;
5968 goto out_fput;
5969 }
5970
5971 ctx->io_wq = ctx_attach->io_wq;
5972out_fput:
5973 fdput(f);
5974 return ret;
5975}
5976
Jens Axboe6c271ce2019-01-10 11:22:30 -07005977static int io_sq_offload_start(struct io_ring_ctx *ctx,
5978 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005979{
5980 int ret;
5981
Jens Axboe6c271ce2019-01-10 11:22:30 -07005982 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005983 mmgrab(current->mm);
5984 ctx->sqo_mm = current->mm;
5985
Jens Axboe6c271ce2019-01-10 11:22:30 -07005986 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06005987 ret = -EPERM;
5988 if (!capable(CAP_SYS_ADMIN))
5989 goto err;
5990
Jens Axboe917257d2019-04-13 09:28:55 -06005991 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
5992 if (!ctx->sq_thread_idle)
5993 ctx->sq_thread_idle = HZ;
5994
Jens Axboe6c271ce2019-01-10 11:22:30 -07005995 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06005996 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005997
Jens Axboe917257d2019-04-13 09:28:55 -06005998 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06005999 if (cpu >= nr_cpu_ids)
6000 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08006001 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06006002 goto err;
6003
Jens Axboe6c271ce2019-01-10 11:22:30 -07006004 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
6005 ctx, cpu,
6006 "io_uring-sq");
6007 } else {
6008 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
6009 "io_uring-sq");
6010 }
6011 if (IS_ERR(ctx->sqo_thread)) {
6012 ret = PTR_ERR(ctx->sqo_thread);
6013 ctx->sqo_thread = NULL;
6014 goto err;
6015 }
6016 wake_up_process(ctx->sqo_thread);
6017 } else if (p->flags & IORING_SETUP_SQ_AFF) {
6018 /* Can't have SQ_AFF without SQPOLL */
6019 ret = -EINVAL;
6020 goto err;
6021 }
6022
Pavel Begunkov24369c22020-01-28 03:15:48 +03006023 ret = io_init_wq_offload(ctx, p);
6024 if (ret)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006025 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006026
6027 return 0;
6028err:
Jens Axboe54a91f32019-09-10 09:15:04 -06006029 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006030 mmdrop(ctx->sqo_mm);
6031 ctx->sqo_mm = NULL;
6032 return ret;
6033}
6034
6035static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
6036{
6037 atomic_long_sub(nr_pages, &user->locked_vm);
6038}
6039
6040static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
6041{
6042 unsigned long page_limit, cur_pages, new_pages;
6043
6044 /* Don't allow more pages than we can safely lock */
6045 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
6046
6047 do {
6048 cur_pages = atomic_long_read(&user->locked_vm);
6049 new_pages = cur_pages + nr_pages;
6050 if (new_pages > page_limit)
6051 return -ENOMEM;
6052 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
6053 new_pages) != cur_pages);
6054
6055 return 0;
6056}
6057
6058static void io_mem_free(void *ptr)
6059{
Mark Rutland52e04ef2019-04-30 17:30:21 +01006060 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006061
Mark Rutland52e04ef2019-04-30 17:30:21 +01006062 if (!ptr)
6063 return;
6064
6065 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006066 if (put_page_testzero(page))
6067 free_compound_page(page);
6068}
6069
6070static void *io_mem_alloc(size_t size)
6071{
6072 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
6073 __GFP_NORETRY;
6074
6075 return (void *) __get_free_pages(gfp_flags, get_order(size));
6076}
6077
Hristo Venev75b28af2019-08-26 17:23:46 +00006078static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
6079 size_t *sq_offset)
6080{
6081 struct io_rings *rings;
6082 size_t off, sq_array_size;
6083
6084 off = struct_size(rings, cqes, cq_entries);
6085 if (off == SIZE_MAX)
6086 return SIZE_MAX;
6087
6088#ifdef CONFIG_SMP
6089 off = ALIGN(off, SMP_CACHE_BYTES);
6090 if (off == 0)
6091 return SIZE_MAX;
6092#endif
6093
6094 sq_array_size = array_size(sizeof(u32), sq_entries);
6095 if (sq_array_size == SIZE_MAX)
6096 return SIZE_MAX;
6097
6098 if (check_add_overflow(off, sq_array_size, &off))
6099 return SIZE_MAX;
6100
6101 if (sq_offset)
6102 *sq_offset = off;
6103
6104 return off;
6105}
6106
Jens Axboe2b188cc2019-01-07 10:46:33 -07006107static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
6108{
Hristo Venev75b28af2019-08-26 17:23:46 +00006109 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006110
Hristo Venev75b28af2019-08-26 17:23:46 +00006111 pages = (size_t)1 << get_order(
6112 rings_size(sq_entries, cq_entries, NULL));
6113 pages += (size_t)1 << get_order(
6114 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07006115
Hristo Venev75b28af2019-08-26 17:23:46 +00006116 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006117}
6118
Jens Axboeedafcce2019-01-09 09:16:05 -07006119static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
6120{
6121 int i, j;
6122
6123 if (!ctx->user_bufs)
6124 return -ENXIO;
6125
6126 for (i = 0; i < ctx->nr_user_bufs; i++) {
6127 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
6128
6129 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08006130 unpin_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07006131
6132 if (ctx->account_mem)
6133 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006134 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07006135 imu->nr_bvecs = 0;
6136 }
6137
6138 kfree(ctx->user_bufs);
6139 ctx->user_bufs = NULL;
6140 ctx->nr_user_bufs = 0;
6141 return 0;
6142}
6143
6144static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
6145 void __user *arg, unsigned index)
6146{
6147 struct iovec __user *src;
6148
6149#ifdef CONFIG_COMPAT
6150 if (ctx->compat) {
6151 struct compat_iovec __user *ciovs;
6152 struct compat_iovec ciov;
6153
6154 ciovs = (struct compat_iovec __user *) arg;
6155 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
6156 return -EFAULT;
6157
Jens Axboed55e5f52019-12-11 16:12:15 -07006158 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07006159 dst->iov_len = ciov.iov_len;
6160 return 0;
6161 }
6162#endif
6163 src = (struct iovec __user *) arg;
6164 if (copy_from_user(dst, &src[index], sizeof(*dst)))
6165 return -EFAULT;
6166 return 0;
6167}
6168
6169static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
6170 unsigned nr_args)
6171{
6172 struct vm_area_struct **vmas = NULL;
6173 struct page **pages = NULL;
6174 int i, j, got_pages = 0;
6175 int ret = -EINVAL;
6176
6177 if (ctx->user_bufs)
6178 return -EBUSY;
6179 if (!nr_args || nr_args > UIO_MAXIOV)
6180 return -EINVAL;
6181
6182 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
6183 GFP_KERNEL);
6184 if (!ctx->user_bufs)
6185 return -ENOMEM;
6186
6187 for (i = 0; i < nr_args; i++) {
6188 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
6189 unsigned long off, start, end, ubuf;
6190 int pret, nr_pages;
6191 struct iovec iov;
6192 size_t size;
6193
6194 ret = io_copy_iov(ctx, &iov, arg, i);
6195 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03006196 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07006197
6198 /*
6199 * Don't impose further limits on the size and buffer
6200 * constraints here, we'll -EINVAL later when IO is
6201 * submitted if they are wrong.
6202 */
6203 ret = -EFAULT;
6204 if (!iov.iov_base || !iov.iov_len)
6205 goto err;
6206
6207 /* arbitrary limit, but we need something */
6208 if (iov.iov_len > SZ_1G)
6209 goto err;
6210
6211 ubuf = (unsigned long) iov.iov_base;
6212 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
6213 start = ubuf >> PAGE_SHIFT;
6214 nr_pages = end - start;
6215
6216 if (ctx->account_mem) {
6217 ret = io_account_mem(ctx->user, nr_pages);
6218 if (ret)
6219 goto err;
6220 }
6221
6222 ret = 0;
6223 if (!pages || nr_pages > got_pages) {
6224 kfree(vmas);
6225 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006226 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07006227 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006228 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07006229 sizeof(struct vm_area_struct *),
6230 GFP_KERNEL);
6231 if (!pages || !vmas) {
6232 ret = -ENOMEM;
6233 if (ctx->account_mem)
6234 io_unaccount_mem(ctx->user, nr_pages);
6235 goto err;
6236 }
6237 got_pages = nr_pages;
6238 }
6239
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006240 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07006241 GFP_KERNEL);
6242 ret = -ENOMEM;
6243 if (!imu->bvec) {
6244 if (ctx->account_mem)
6245 io_unaccount_mem(ctx->user, nr_pages);
6246 goto err;
6247 }
6248
6249 ret = 0;
6250 down_read(&current->mm->mmap_sem);
John Hubbard2113b052020-01-30 22:13:13 -08006251 pret = pin_user_pages(ubuf, nr_pages,
Ira Weiny932f4a62019-05-13 17:17:03 -07006252 FOLL_WRITE | FOLL_LONGTERM,
6253 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07006254 if (pret == nr_pages) {
6255 /* don't support file backed memory */
6256 for (j = 0; j < nr_pages; j++) {
6257 struct vm_area_struct *vma = vmas[j];
6258
6259 if (vma->vm_file &&
6260 !is_file_hugepages(vma->vm_file)) {
6261 ret = -EOPNOTSUPP;
6262 break;
6263 }
6264 }
6265 } else {
6266 ret = pret < 0 ? pret : -EFAULT;
6267 }
6268 up_read(&current->mm->mmap_sem);
6269 if (ret) {
6270 /*
6271 * if we did partial map, or found file backed vmas,
6272 * release any pages we did get
6273 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07006274 if (pret > 0)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08006275 unpin_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07006276 if (ctx->account_mem)
6277 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006278 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07006279 goto err;
6280 }
6281
6282 off = ubuf & ~PAGE_MASK;
6283 size = iov.iov_len;
6284 for (j = 0; j < nr_pages; j++) {
6285 size_t vec_len;
6286
6287 vec_len = min_t(size_t, size, PAGE_SIZE - off);
6288 imu->bvec[j].bv_page = pages[j];
6289 imu->bvec[j].bv_len = vec_len;
6290 imu->bvec[j].bv_offset = off;
6291 off = 0;
6292 size -= vec_len;
6293 }
6294 /* store original address for later verification */
6295 imu->ubuf = ubuf;
6296 imu->len = iov.iov_len;
6297 imu->nr_bvecs = nr_pages;
6298
6299 ctx->nr_user_bufs++;
6300 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006301 kvfree(pages);
6302 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07006303 return 0;
6304err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006305 kvfree(pages);
6306 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07006307 io_sqe_buffer_unregister(ctx);
6308 return ret;
6309}
6310
Jens Axboe9b402842019-04-11 11:45:41 -06006311static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
6312{
6313 __s32 __user *fds = arg;
6314 int fd;
6315
6316 if (ctx->cq_ev_fd)
6317 return -EBUSY;
6318
6319 if (copy_from_user(&fd, fds, sizeof(*fds)))
6320 return -EFAULT;
6321
6322 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
6323 if (IS_ERR(ctx->cq_ev_fd)) {
6324 int ret = PTR_ERR(ctx->cq_ev_fd);
6325 ctx->cq_ev_fd = NULL;
6326 return ret;
6327 }
6328
6329 return 0;
6330}
6331
6332static int io_eventfd_unregister(struct io_ring_ctx *ctx)
6333{
6334 if (ctx->cq_ev_fd) {
6335 eventfd_ctx_put(ctx->cq_ev_fd);
6336 ctx->cq_ev_fd = NULL;
6337 return 0;
6338 }
6339
6340 return -ENXIO;
6341}
6342
Jens Axboe2b188cc2019-01-07 10:46:33 -07006343static void io_ring_ctx_free(struct io_ring_ctx *ctx)
6344{
Jens Axboe6b063142019-01-10 22:13:58 -07006345 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006346 if (ctx->sqo_mm)
6347 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07006348
6349 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07006350 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07006351 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06006352 io_eventfd_unregister(ctx);
Jens Axboe41726c92020-02-23 13:11:42 -07006353 idr_destroy(&ctx->personality_idr);
Jens Axboedef596e2019-01-09 08:59:42 -07006354
Jens Axboe2b188cc2019-01-07 10:46:33 -07006355#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07006356 if (ctx->ring_sock) {
6357 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07006358 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07006359 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006360#endif
6361
Hristo Venev75b28af2019-08-26 17:23:46 +00006362 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006363 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006364
6365 percpu_ref_exit(&ctx->refs);
6366 if (ctx->account_mem)
6367 io_unaccount_mem(ctx->user,
6368 ring_pages(ctx->sq_entries, ctx->cq_entries));
6369 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07006370 put_cred(ctx->creds);
Jens Axboe206aefd2019-11-07 18:27:42 -07006371 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -07006372 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07006373 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006374 kfree(ctx);
6375}
6376
6377static __poll_t io_uring_poll(struct file *file, poll_table *wait)
6378{
6379 struct io_ring_ctx *ctx = file->private_data;
6380 __poll_t mask = 0;
6381
6382 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02006383 /*
6384 * synchronizes with barrier from wq_has_sleeper call in
6385 * io_commit_cqring
6386 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07006387 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00006388 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
6389 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006390 mask |= EPOLLOUT | EPOLLWRNORM;
Stefano Garzarella63e5d812020-02-07 13:18:28 +01006391 if (io_cqring_events(ctx, false))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006392 mask |= EPOLLIN | EPOLLRDNORM;
6393
6394 return mask;
6395}
6396
6397static int io_uring_fasync(int fd, struct file *file, int on)
6398{
6399 struct io_ring_ctx *ctx = file->private_data;
6400
6401 return fasync_helper(fd, file, on, &ctx->cq_fasync);
6402}
6403
Jens Axboe071698e2020-01-28 10:04:42 -07006404static int io_remove_personalities(int id, void *p, void *data)
6405{
6406 struct io_ring_ctx *ctx = data;
6407 const struct cred *cred;
6408
6409 cred = idr_remove(&ctx->personality_idr, id);
6410 if (cred)
6411 put_cred(cred);
6412 return 0;
6413}
6414
Jens Axboe2b188cc2019-01-07 10:46:33 -07006415static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
6416{
6417 mutex_lock(&ctx->uring_lock);
6418 percpu_ref_kill(&ctx->refs);
6419 mutex_unlock(&ctx->uring_lock);
6420
Jens Axboedf069d82020-02-04 16:48:34 -07006421 /*
6422 * Wait for sq thread to idle, if we have one. It won't spin on new
6423 * work after we've killed the ctx ref above. This is important to do
6424 * before we cancel existing commands, as the thread could otherwise
6425 * be queueing new work post that. If that's work we need to cancel,
6426 * it could cause shutdown to hang.
6427 */
6428 while (ctx->sqo_thread && !wq_has_sleeper(&ctx->sqo_wait))
6429 cpu_relax();
6430
Jens Axboe5262f562019-09-17 12:26:57 -06006431 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07006432 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06006433
6434 if (ctx->io_wq)
6435 io_wq_cancel_all(ctx->io_wq);
6436
Jens Axboedef596e2019-01-09 08:59:42 -07006437 io_iopoll_reap_events(ctx);
Jens Axboe15dff282019-11-13 09:09:23 -07006438 /* if we failed setting up the ctx, we might not have any rings */
6439 if (ctx->rings)
6440 io_cqring_overflow_flush(ctx, true);
Jens Axboe071698e2020-01-28 10:04:42 -07006441 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
Jens Axboe206aefd2019-11-07 18:27:42 -07006442 wait_for_completion(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006443 io_ring_ctx_free(ctx);
6444}
6445
6446static int io_uring_release(struct inode *inode, struct file *file)
6447{
6448 struct io_ring_ctx *ctx = file->private_data;
6449
6450 file->private_data = NULL;
6451 io_ring_ctx_wait_and_kill(ctx);
6452 return 0;
6453}
6454
Jens Axboefcb323c2019-10-24 12:39:47 -06006455static void io_uring_cancel_files(struct io_ring_ctx *ctx,
6456 struct files_struct *files)
6457{
6458 struct io_kiocb *req;
6459 DEFINE_WAIT(wait);
6460
6461 while (!list_empty_careful(&ctx->inflight_list)) {
Jens Axboe768134d2019-11-10 20:30:53 -07006462 struct io_kiocb *cancel_req = NULL;
Jens Axboefcb323c2019-10-24 12:39:47 -06006463
6464 spin_lock_irq(&ctx->inflight_lock);
6465 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07006466 if (req->work.files != files)
6467 continue;
6468 /* req is being completed, ignore */
6469 if (!refcount_inc_not_zero(&req->refs))
6470 continue;
6471 cancel_req = req;
6472 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06006473 }
Jens Axboe768134d2019-11-10 20:30:53 -07006474 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06006475 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07006476 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06006477 spin_unlock_irq(&ctx->inflight_lock);
6478
Jens Axboe768134d2019-11-10 20:30:53 -07006479 /* We need to keep going until we don't find a matching req */
6480 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06006481 break;
Bob Liu2f6d9b92019-11-13 18:06:24 +08006482
Jens Axboe2ca10252020-02-13 17:17:35 -07006483 if (cancel_req->flags & REQ_F_OVERFLOW) {
6484 spin_lock_irq(&ctx->completion_lock);
6485 list_del(&cancel_req->list);
6486 cancel_req->flags &= ~REQ_F_OVERFLOW;
6487 if (list_empty(&ctx->cq_overflow_list)) {
6488 clear_bit(0, &ctx->sq_check_overflow);
6489 clear_bit(0, &ctx->cq_check_overflow);
6490 }
6491 spin_unlock_irq(&ctx->completion_lock);
6492
6493 WRITE_ONCE(ctx->rings->cq_overflow,
6494 atomic_inc_return(&ctx->cached_cq_overflow));
6495
6496 /*
6497 * Put inflight ref and overflow ref. If that's
6498 * all we had, then we're done with this request.
6499 */
6500 if (refcount_sub_and_test(2, &cancel_req->refs)) {
6501 io_put_req(cancel_req);
6502 continue;
6503 }
6504 }
6505
Bob Liu2f6d9b92019-11-13 18:06:24 +08006506 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
6507 io_put_req(cancel_req);
Jens Axboefcb323c2019-10-24 12:39:47 -06006508 schedule();
6509 }
Jens Axboe768134d2019-11-10 20:30:53 -07006510 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06006511}
6512
6513static int io_uring_flush(struct file *file, void *data)
6514{
6515 struct io_ring_ctx *ctx = file->private_data;
6516
6517 io_uring_cancel_files(ctx, data);
Jens Axboe6ab23142020-02-08 20:23:59 -07006518
6519 /*
6520 * If the task is going away, cancel work it may have pending
6521 */
6522 if (fatal_signal_pending(current) || (current->flags & PF_EXITING))
6523 io_wq_cancel_pid(ctx->io_wq, task_pid_vnr(current));
6524
Jens Axboefcb323c2019-10-24 12:39:47 -06006525 return 0;
6526}
6527
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006528static void *io_uring_validate_mmap_request(struct file *file,
6529 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006530{
Jens Axboe2b188cc2019-01-07 10:46:33 -07006531 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006532 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006533 struct page *page;
6534 void *ptr;
6535
6536 switch (offset) {
6537 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00006538 case IORING_OFF_CQ_RING:
6539 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006540 break;
6541 case IORING_OFF_SQES:
6542 ptr = ctx->sq_sqes;
6543 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006544 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006545 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006546 }
6547
6548 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07006549 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006550 return ERR_PTR(-EINVAL);
6551
6552 return ptr;
6553}
6554
6555#ifdef CONFIG_MMU
6556
6557static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
6558{
6559 size_t sz = vma->vm_end - vma->vm_start;
6560 unsigned long pfn;
6561 void *ptr;
6562
6563 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
6564 if (IS_ERR(ptr))
6565 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006566
6567 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
6568 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
6569}
6570
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006571#else /* !CONFIG_MMU */
6572
6573static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
6574{
6575 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
6576}
6577
6578static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
6579{
6580 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
6581}
6582
6583static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
6584 unsigned long addr, unsigned long len,
6585 unsigned long pgoff, unsigned long flags)
6586{
6587 void *ptr;
6588
6589 ptr = io_uring_validate_mmap_request(file, pgoff, len);
6590 if (IS_ERR(ptr))
6591 return PTR_ERR(ptr);
6592
6593 return (unsigned long) ptr;
6594}
6595
6596#endif /* !CONFIG_MMU */
6597
Jens Axboe2b188cc2019-01-07 10:46:33 -07006598SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
6599 u32, min_complete, u32, flags, const sigset_t __user *, sig,
6600 size_t, sigsz)
6601{
6602 struct io_ring_ctx *ctx;
6603 long ret = -EBADF;
6604 int submitted = 0;
6605 struct fd f;
6606
Jens Axboe6c271ce2019-01-10 11:22:30 -07006607 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006608 return -EINVAL;
6609
6610 f = fdget(fd);
6611 if (!f.file)
6612 return -EBADF;
6613
6614 ret = -EOPNOTSUPP;
6615 if (f.file->f_op != &io_uring_fops)
6616 goto out_fput;
6617
6618 ret = -ENXIO;
6619 ctx = f.file->private_data;
6620 if (!percpu_ref_tryget(&ctx->refs))
6621 goto out_fput;
6622
Jens Axboe6c271ce2019-01-10 11:22:30 -07006623 /*
6624 * For SQ polling, the thread will do all submissions and completions.
6625 * Just return the requested submit count, and wake the thread if
6626 * we were asked to.
6627 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06006628 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006629 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07006630 if (!list_empty_careful(&ctx->cq_overflow_list))
6631 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006632 if (flags & IORING_ENTER_SQ_WAKEUP)
6633 wake_up(&ctx->sqo_wait);
6634 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06006635 } else if (to_submit) {
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006636 struct mm_struct *cur_mm;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006637
6638 mutex_lock(&ctx->uring_lock);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006639 /* already have mm, so io_submit_sqes() won't try to grab it */
6640 cur_mm = ctx->sqo_mm;
6641 submitted = io_submit_sqes(ctx, to_submit, f.file, fd,
6642 &cur_mm, false);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006643 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03006644
6645 if (submitted != to_submit)
6646 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006647 }
6648 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07006649 unsigned nr_events = 0;
6650
Jens Axboe2b188cc2019-01-07 10:46:33 -07006651 min_complete = min(min_complete, ctx->cq_entries);
6652
Jens Axboedef596e2019-01-09 08:59:42 -07006653 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07006654 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07006655 } else {
6656 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
6657 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006658 }
6659
Pavel Begunkov7c504e652019-12-18 19:53:45 +03006660out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03006661 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006662out_fput:
6663 fdput(f);
6664 return submitted ? submitted : ret;
6665}
6666
Tobias Klauserbebdb652020-02-26 18:38:32 +01006667#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07006668static int io_uring_show_cred(int id, void *p, void *data)
6669{
6670 const struct cred *cred = p;
6671 struct seq_file *m = data;
6672 struct user_namespace *uns = seq_user_ns(m);
6673 struct group_info *gi;
6674 kernel_cap_t cap;
6675 unsigned __capi;
6676 int g;
6677
6678 seq_printf(m, "%5d\n", id);
6679 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
6680 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
6681 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
6682 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
6683 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
6684 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
6685 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
6686 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
6687 seq_puts(m, "\n\tGroups:\t");
6688 gi = cred->group_info;
6689 for (g = 0; g < gi->ngroups; g++) {
6690 seq_put_decimal_ull(m, g ? " " : "",
6691 from_kgid_munged(uns, gi->gid[g]));
6692 }
6693 seq_puts(m, "\n\tCapEff:\t");
6694 cap = cred->cap_effective;
6695 CAP_FOR_EACH_U32(__capi)
6696 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
6697 seq_putc(m, '\n');
6698 return 0;
6699}
6700
6701static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
6702{
6703 int i;
6704
6705 mutex_lock(&ctx->uring_lock);
6706 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
6707 for (i = 0; i < ctx->nr_user_files; i++) {
6708 struct fixed_file_table *table;
6709 struct file *f;
6710
6711 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
6712 f = table->files[i & IORING_FILE_TABLE_MASK];
6713 if (f)
6714 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
6715 else
6716 seq_printf(m, "%5u: <none>\n", i);
6717 }
6718 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
6719 for (i = 0; i < ctx->nr_user_bufs; i++) {
6720 struct io_mapped_ubuf *buf = &ctx->user_bufs[i];
6721
6722 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf,
6723 (unsigned int) buf->len);
6724 }
6725 if (!idr_is_empty(&ctx->personality_idr)) {
6726 seq_printf(m, "Personalities:\n");
6727 idr_for_each(&ctx->personality_idr, io_uring_show_cred, m);
6728 }
6729 mutex_unlock(&ctx->uring_lock);
6730}
6731
6732static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
6733{
6734 struct io_ring_ctx *ctx = f->private_data;
6735
6736 if (percpu_ref_tryget(&ctx->refs)) {
6737 __io_uring_show_fdinfo(ctx, m);
6738 percpu_ref_put(&ctx->refs);
6739 }
6740}
Tobias Klauserbebdb652020-02-26 18:38:32 +01006741#endif
Jens Axboe87ce9552020-01-30 08:25:34 -07006742
Jens Axboe2b188cc2019-01-07 10:46:33 -07006743static const struct file_operations io_uring_fops = {
6744 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06006745 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07006746 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006747#ifndef CONFIG_MMU
6748 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
6749 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
6750#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07006751 .poll = io_uring_poll,
6752 .fasync = io_uring_fasync,
Tobias Klauserbebdb652020-02-26 18:38:32 +01006753#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07006754 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +01006755#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07006756};
6757
6758static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
6759 struct io_uring_params *p)
6760{
Hristo Venev75b28af2019-08-26 17:23:46 +00006761 struct io_rings *rings;
6762 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006763
Hristo Venev75b28af2019-08-26 17:23:46 +00006764 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
6765 if (size == SIZE_MAX)
6766 return -EOVERFLOW;
6767
6768 rings = io_mem_alloc(size);
6769 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006770 return -ENOMEM;
6771
Hristo Venev75b28af2019-08-26 17:23:46 +00006772 ctx->rings = rings;
6773 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
6774 rings->sq_ring_mask = p->sq_entries - 1;
6775 rings->cq_ring_mask = p->cq_entries - 1;
6776 rings->sq_ring_entries = p->sq_entries;
6777 rings->cq_ring_entries = p->cq_entries;
6778 ctx->sq_mask = rings->sq_ring_mask;
6779 ctx->cq_mask = rings->cq_ring_mask;
6780 ctx->sq_entries = rings->sq_ring_entries;
6781 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006782
6783 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07006784 if (size == SIZE_MAX) {
6785 io_mem_free(ctx->rings);
6786 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006787 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07006788 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006789
6790 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07006791 if (!ctx->sq_sqes) {
6792 io_mem_free(ctx->rings);
6793 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006794 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07006795 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006796
Jens Axboe2b188cc2019-01-07 10:46:33 -07006797 return 0;
6798}
6799
6800/*
6801 * Allocate an anonymous fd, this is what constitutes the application
6802 * visible backing of an io_uring instance. The application mmaps this
6803 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
6804 * we have to tie this fd to a socket for file garbage collection purposes.
6805 */
6806static int io_uring_get_fd(struct io_ring_ctx *ctx)
6807{
6808 struct file *file;
6809 int ret;
6810
6811#if defined(CONFIG_UNIX)
6812 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
6813 &ctx->ring_sock);
6814 if (ret)
6815 return ret;
6816#endif
6817
6818 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
6819 if (ret < 0)
6820 goto err;
6821
6822 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
6823 O_RDWR | O_CLOEXEC);
6824 if (IS_ERR(file)) {
6825 put_unused_fd(ret);
6826 ret = PTR_ERR(file);
6827 goto err;
6828 }
6829
6830#if defined(CONFIG_UNIX)
6831 ctx->ring_sock->file = file;
6832#endif
6833 fd_install(ret, file);
6834 return ret;
6835err:
6836#if defined(CONFIG_UNIX)
6837 sock_release(ctx->ring_sock);
6838 ctx->ring_sock = NULL;
6839#endif
6840 return ret;
6841}
6842
6843static int io_uring_create(unsigned entries, struct io_uring_params *p)
6844{
6845 struct user_struct *user = NULL;
6846 struct io_ring_ctx *ctx;
6847 bool account_mem;
6848 int ret;
6849
Jens Axboe8110c1a2019-12-28 15:39:54 -07006850 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006851 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07006852 if (entries > IORING_MAX_ENTRIES) {
6853 if (!(p->flags & IORING_SETUP_CLAMP))
6854 return -EINVAL;
6855 entries = IORING_MAX_ENTRIES;
6856 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006857
6858 /*
6859 * Use twice as many entries for the CQ ring. It's possible for the
6860 * application to drive a higher depth than the size of the SQ ring,
6861 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06006862 * some flexibility in overcommitting a bit. If the application has
6863 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
6864 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07006865 */
6866 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06006867 if (p->flags & IORING_SETUP_CQSIZE) {
6868 /*
6869 * If IORING_SETUP_CQSIZE is set, we do the same roundup
6870 * to a power-of-two, if it isn't already. We do NOT impose
6871 * any cq vs sq ring sizing.
6872 */
Jens Axboe8110c1a2019-12-28 15:39:54 -07006873 if (p->cq_entries < p->sq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06006874 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07006875 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
6876 if (!(p->flags & IORING_SETUP_CLAMP))
6877 return -EINVAL;
6878 p->cq_entries = IORING_MAX_CQ_ENTRIES;
6879 }
Jens Axboe33a107f2019-10-04 12:10:03 -06006880 p->cq_entries = roundup_pow_of_two(p->cq_entries);
6881 } else {
6882 p->cq_entries = 2 * p->sq_entries;
6883 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006884
6885 user = get_uid(current_user());
6886 account_mem = !capable(CAP_IPC_LOCK);
6887
6888 if (account_mem) {
6889 ret = io_account_mem(user,
6890 ring_pages(p->sq_entries, p->cq_entries));
6891 if (ret) {
6892 free_uid(user);
6893 return ret;
6894 }
6895 }
6896
6897 ctx = io_ring_ctx_alloc(p);
6898 if (!ctx) {
6899 if (account_mem)
6900 io_unaccount_mem(user, ring_pages(p->sq_entries,
6901 p->cq_entries));
6902 free_uid(user);
6903 return -ENOMEM;
6904 }
6905 ctx->compat = in_compat_syscall();
6906 ctx->account_mem = account_mem;
6907 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07006908 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07006909
6910 ret = io_allocate_scq_urings(ctx, p);
6911 if (ret)
6912 goto err;
6913
Jens Axboe6c271ce2019-01-10 11:22:30 -07006914 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006915 if (ret)
6916 goto err;
6917
Jens Axboe2b188cc2019-01-07 10:46:33 -07006918 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00006919 p->sq_off.head = offsetof(struct io_rings, sq.head);
6920 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
6921 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
6922 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
6923 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
6924 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
6925 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006926
6927 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00006928 p->cq_off.head = offsetof(struct io_rings, cq.head);
6929 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
6930 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
6931 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
6932 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
6933 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06006934
Jens Axboe044c1ab2019-10-28 09:15:33 -06006935 /*
6936 * Install ring fd as the very last thing, so we don't risk someone
6937 * having closed it before we finish setup
6938 */
6939 ret = io_uring_get_fd(ctx);
6940 if (ret < 0)
6941 goto err;
6942
Jens Axboeda8c9692019-12-02 18:51:26 -07006943 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
Jens Axboecccf0ee2020-01-27 16:34:48 -07006944 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
6945 IORING_FEAT_CUR_PERSONALITY;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006946 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006947 return ret;
6948err:
6949 io_ring_ctx_wait_and_kill(ctx);
6950 return ret;
6951}
6952
6953/*
6954 * Sets up an aio uring context, and returns the fd. Applications asks for a
6955 * ring size, we return the actual sq/cq ring sizes (among other things) in the
6956 * params structure passed in.
6957 */
6958static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
6959{
6960 struct io_uring_params p;
6961 long ret;
6962 int i;
6963
6964 if (copy_from_user(&p, params, sizeof(p)))
6965 return -EFAULT;
6966 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
6967 if (p.resv[i])
6968 return -EINVAL;
6969 }
6970
Jens Axboe6c271ce2019-01-10 11:22:30 -07006971 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07006972 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Pavel Begunkov24369c22020-01-28 03:15:48 +03006973 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006974 return -EINVAL;
6975
6976 ret = io_uring_create(entries, &p);
6977 if (ret < 0)
6978 return ret;
6979
6980 if (copy_to_user(params, &p, sizeof(p)))
6981 return -EFAULT;
6982
6983 return ret;
6984}
6985
6986SYSCALL_DEFINE2(io_uring_setup, u32, entries,
6987 struct io_uring_params __user *, params)
6988{
6989 return io_uring_setup(entries, params);
6990}
6991
Jens Axboe66f4af92020-01-16 15:36:52 -07006992static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
6993{
6994 struct io_uring_probe *p;
6995 size_t size;
6996 int i, ret;
6997
6998 size = struct_size(p, ops, nr_args);
6999 if (size == SIZE_MAX)
7000 return -EOVERFLOW;
7001 p = kzalloc(size, GFP_KERNEL);
7002 if (!p)
7003 return -ENOMEM;
7004
7005 ret = -EFAULT;
7006 if (copy_from_user(p, arg, size))
7007 goto out;
7008 ret = -EINVAL;
7009 if (memchr_inv(p, 0, size))
7010 goto out;
7011
7012 p->last_op = IORING_OP_LAST - 1;
7013 if (nr_args > IORING_OP_LAST)
7014 nr_args = IORING_OP_LAST;
7015
7016 for (i = 0; i < nr_args; i++) {
7017 p->ops[i].op = i;
7018 if (!io_op_defs[i].not_supported)
7019 p->ops[i].flags = IO_URING_OP_SUPPORTED;
7020 }
7021 p->ops_len = i;
7022
7023 ret = 0;
7024 if (copy_to_user(arg, p, size))
7025 ret = -EFAULT;
7026out:
7027 kfree(p);
7028 return ret;
7029}
7030
Jens Axboe071698e2020-01-28 10:04:42 -07007031static int io_register_personality(struct io_ring_ctx *ctx)
7032{
7033 const struct cred *creds = get_current_cred();
7034 int id;
7035
7036 id = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1,
7037 USHRT_MAX, GFP_KERNEL);
7038 if (id < 0)
7039 put_cred(creds);
7040 return id;
7041}
7042
7043static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
7044{
7045 const struct cred *old_creds;
7046
7047 old_creds = idr_remove(&ctx->personality_idr, id);
7048 if (old_creds) {
7049 put_cred(old_creds);
7050 return 0;
7051 }
7052
7053 return -EINVAL;
7054}
7055
7056static bool io_register_op_must_quiesce(int op)
7057{
7058 switch (op) {
7059 case IORING_UNREGISTER_FILES:
7060 case IORING_REGISTER_FILES_UPDATE:
7061 case IORING_REGISTER_PROBE:
7062 case IORING_REGISTER_PERSONALITY:
7063 case IORING_UNREGISTER_PERSONALITY:
7064 return false;
7065 default:
7066 return true;
7067 }
7068}
7069
Jens Axboeedafcce2019-01-09 09:16:05 -07007070static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
7071 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06007072 __releases(ctx->uring_lock)
7073 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07007074{
7075 int ret;
7076
Jens Axboe35fa71a2019-04-22 10:23:23 -06007077 /*
7078 * We're inside the ring mutex, if the ref is already dying, then
7079 * someone else killed the ctx or is already going through
7080 * io_uring_register().
7081 */
7082 if (percpu_ref_is_dying(&ctx->refs))
7083 return -ENXIO;
7084
Jens Axboe071698e2020-01-28 10:04:42 -07007085 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007086 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06007087
Jens Axboe05f3fb32019-12-09 11:22:50 -07007088 /*
7089 * Drop uring mutex before waiting for references to exit. If
7090 * another thread is currently inside io_uring_enter() it might
7091 * need to grab the uring_lock to make progress. If we hold it
7092 * here across the drain wait, then we can deadlock. It's safe
7093 * to drop the mutex here, since no new references will come in
7094 * after we've killed the percpu ref.
7095 */
7096 mutex_unlock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07007097 ret = wait_for_completion_interruptible(&ctx->completions[0]);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007098 mutex_lock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07007099 if (ret) {
7100 percpu_ref_resurrect(&ctx->refs);
7101 ret = -EINTR;
7102 goto out;
7103 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007104 }
Jens Axboeedafcce2019-01-09 09:16:05 -07007105
7106 switch (opcode) {
7107 case IORING_REGISTER_BUFFERS:
7108 ret = io_sqe_buffer_register(ctx, arg, nr_args);
7109 break;
7110 case IORING_UNREGISTER_BUFFERS:
7111 ret = -EINVAL;
7112 if (arg || nr_args)
7113 break;
7114 ret = io_sqe_buffer_unregister(ctx);
7115 break;
Jens Axboe6b063142019-01-10 22:13:58 -07007116 case IORING_REGISTER_FILES:
7117 ret = io_sqe_files_register(ctx, arg, nr_args);
7118 break;
7119 case IORING_UNREGISTER_FILES:
7120 ret = -EINVAL;
7121 if (arg || nr_args)
7122 break;
7123 ret = io_sqe_files_unregister(ctx);
7124 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06007125 case IORING_REGISTER_FILES_UPDATE:
7126 ret = io_sqe_files_update(ctx, arg, nr_args);
7127 break;
Jens Axboe9b402842019-04-11 11:45:41 -06007128 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -07007129 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -06007130 ret = -EINVAL;
7131 if (nr_args != 1)
7132 break;
7133 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -07007134 if (ret)
7135 break;
7136 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
7137 ctx->eventfd_async = 1;
7138 else
7139 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -06007140 break;
7141 case IORING_UNREGISTER_EVENTFD:
7142 ret = -EINVAL;
7143 if (arg || nr_args)
7144 break;
7145 ret = io_eventfd_unregister(ctx);
7146 break;
Jens Axboe66f4af92020-01-16 15:36:52 -07007147 case IORING_REGISTER_PROBE:
7148 ret = -EINVAL;
7149 if (!arg || nr_args > 256)
7150 break;
7151 ret = io_probe(ctx, arg, nr_args);
7152 break;
Jens Axboe071698e2020-01-28 10:04:42 -07007153 case IORING_REGISTER_PERSONALITY:
7154 ret = -EINVAL;
7155 if (arg || nr_args)
7156 break;
7157 ret = io_register_personality(ctx);
7158 break;
7159 case IORING_UNREGISTER_PERSONALITY:
7160 ret = -EINVAL;
7161 if (arg)
7162 break;
7163 ret = io_unregister_personality(ctx, nr_args);
7164 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07007165 default:
7166 ret = -EINVAL;
7167 break;
7168 }
7169
Jens Axboe071698e2020-01-28 10:04:42 -07007170 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007171 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -07007172 percpu_ref_reinit(&ctx->refs);
Jens Axboec1503682020-01-08 08:26:07 -07007173out:
7174 reinit_completion(&ctx->completions[0]);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007175 }
Jens Axboeedafcce2019-01-09 09:16:05 -07007176 return ret;
7177}
7178
7179SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
7180 void __user *, arg, unsigned int, nr_args)
7181{
7182 struct io_ring_ctx *ctx;
7183 long ret = -EBADF;
7184 struct fd f;
7185
7186 f = fdget(fd);
7187 if (!f.file)
7188 return -EBADF;
7189
7190 ret = -EOPNOTSUPP;
7191 if (f.file->f_op != &io_uring_fops)
7192 goto out_fput;
7193
7194 ctx = f.file->private_data;
7195
7196 mutex_lock(&ctx->uring_lock);
7197 ret = __io_uring_register(ctx, opcode, arg, nr_args);
7198 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02007199 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
7200 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07007201out_fput:
7202 fdput(f);
7203 return ret;
7204}
7205
Jens Axboe2b188cc2019-01-07 10:46:33 -07007206static int __init io_uring_init(void)
7207{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01007208#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
7209 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
7210 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
7211} while (0)
7212
7213#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
7214 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
7215 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
7216 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
7217 BUILD_BUG_SQE_ELEM(1, __u8, flags);
7218 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
7219 BUILD_BUG_SQE_ELEM(4, __s32, fd);
7220 BUILD_BUG_SQE_ELEM(8, __u64, off);
7221 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
7222 BUILD_BUG_SQE_ELEM(16, __u64, addr);
7223 BUILD_BUG_SQE_ELEM(24, __u32, len);
7224 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
7225 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
7226 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
7227 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
7228 BUILD_BUG_SQE_ELEM(28, __u16, poll_events);
7229 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
7230 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
7231 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
7232 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
7233 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
7234 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
7235 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
7236 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
7237 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
7238 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
7239 BUILD_BUG_SQE_ELEM(42, __u16, personality);
7240
Jens Axboed3656342019-12-18 09:50:26 -07007241 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007242 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
7243 return 0;
7244};
7245__initcall(io_uring_init);