blob: fe5ded7c74ef9a8e7d7f2c76a26f86b4d2cacb16 [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 Axboe4022e7a2020-03-19 19:23:18 -0600400 unsigned long nofile;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700401};
402
Jens Axboe05f3fb32019-12-09 11:22:50 -0700403struct io_files_update {
404 struct file *file;
405 u64 arg;
406 u32 nr_args;
407 u32 offset;
408};
409
Jens Axboe4840e412019-12-25 22:03:45 -0700410struct io_fadvise {
411 struct file *file;
412 u64 offset;
413 u32 len;
414 u32 advice;
415};
416
Jens Axboec1ca7572019-12-25 22:18:28 -0700417struct io_madvise {
418 struct file *file;
419 u64 addr;
420 u32 len;
421 u32 advice;
422};
423
Jens Axboe3e4827b2020-01-08 15:18:09 -0700424struct io_epoll {
425 struct file *file;
426 int epfd;
427 int op;
428 int fd;
429 struct epoll_event event;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700430};
431
Jens Axboef499a022019-12-02 16:28:46 -0700432struct io_async_connect {
433 struct sockaddr_storage address;
434};
435
Jens Axboe03b12302019-12-02 18:50:25 -0700436struct io_async_msghdr {
437 struct iovec fast_iov[UIO_FASTIOV];
438 struct iovec *iov;
439 struct sockaddr __user *uaddr;
440 struct msghdr msg;
Jens Axboeb5379162020-02-09 11:29:15 -0700441 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -0700442};
443
Jens Axboef67676d2019-12-02 11:03:47 -0700444struct io_async_rw {
445 struct iovec fast_iov[UIO_FASTIOV];
446 struct iovec *iov;
447 ssize_t nr_segs;
448 ssize_t size;
449};
450
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700451struct io_async_ctx {
Jens Axboef67676d2019-12-02 11:03:47 -0700452 union {
453 struct io_async_rw rw;
Jens Axboe03b12302019-12-02 18:50:25 -0700454 struct io_async_msghdr msg;
Jens Axboef499a022019-12-02 16:28:46 -0700455 struct io_async_connect connect;
Jens Axboe2d283902019-12-04 11:08:05 -0700456 struct io_timeout_data timeout;
Jens Axboef67676d2019-12-02 11:03:47 -0700457 };
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700458};
459
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300460enum {
461 REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT,
462 REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT,
463 REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT,
464 REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT,
465 REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT,
466
467 REQ_F_LINK_NEXT_BIT,
468 REQ_F_FAIL_LINK_BIT,
469 REQ_F_INFLIGHT_BIT,
470 REQ_F_CUR_POS_BIT,
471 REQ_F_NOWAIT_BIT,
472 REQ_F_IOPOLL_COMPLETED_BIT,
473 REQ_F_LINK_TIMEOUT_BIT,
474 REQ_F_TIMEOUT_BIT,
475 REQ_F_ISREG_BIT,
476 REQ_F_MUST_PUNT_BIT,
477 REQ_F_TIMEOUT_NOSEQ_BIT,
478 REQ_F_COMP_LOCKED_BIT,
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300479 REQ_F_NEED_CLEANUP_BIT,
Jens Axboe2ca10252020-02-13 17:17:35 -0700480 REQ_F_OVERFLOW_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300481};
482
483enum {
484 /* ctx owns file */
485 REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT),
486 /* drain existing IO first */
487 REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT),
488 /* linked sqes */
489 REQ_F_LINK = BIT(REQ_F_LINK_BIT),
490 /* doesn't sever on completion < 0 */
491 REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT),
492 /* IOSQE_ASYNC */
493 REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT),
494
495 /* already grabbed next link */
496 REQ_F_LINK_NEXT = BIT(REQ_F_LINK_NEXT_BIT),
497 /* fail rest of links */
498 REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT),
499 /* on inflight list */
500 REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT),
501 /* read/write uses file position */
502 REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT),
503 /* must not punt to workers */
504 REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
505 /* polled IO has completed */
506 REQ_F_IOPOLL_COMPLETED = BIT(REQ_F_IOPOLL_COMPLETED_BIT),
507 /* has linked timeout */
508 REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT),
509 /* timeout request */
510 REQ_F_TIMEOUT = BIT(REQ_F_TIMEOUT_BIT),
511 /* regular file */
512 REQ_F_ISREG = BIT(REQ_F_ISREG_BIT),
513 /* must be punted even for NONBLOCK */
514 REQ_F_MUST_PUNT = BIT(REQ_F_MUST_PUNT_BIT),
515 /* no timeout sequence */
516 REQ_F_TIMEOUT_NOSEQ = BIT(REQ_F_TIMEOUT_NOSEQ_BIT),
517 /* completion under lock */
518 REQ_F_COMP_LOCKED = BIT(REQ_F_COMP_LOCKED_BIT),
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300519 /* needs cleanup */
520 REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT),
Jens Axboe2ca10252020-02-13 17:17:35 -0700521 /* in overflow list */
522 REQ_F_OVERFLOW = BIT(REQ_F_OVERFLOW_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300523};
524
Jens Axboe09bb8392019-03-13 12:39:28 -0600525/*
526 * NOTE! Each of the iocb union members has the file pointer
527 * as the first entry in their struct definition. So you can
528 * access the file pointer through any of the sub-structs,
529 * or directly as just 'ki_filp' in this struct.
530 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700531struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700532 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600533 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700534 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700535 struct io_poll_iocb poll;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700536 struct io_accept accept;
537 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700538 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700539 struct io_timeout timeout;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700540 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700541 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700542 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700543 struct io_close close;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700544 struct io_files_update files_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700545 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700546 struct io_madvise madvise;
Jens Axboe3e4827b2020-01-08 15:18:09 -0700547 struct io_epoll epoll;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700548 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700549
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700550 struct io_async_ctx *io;
Pavel Begunkovb14cca02020-01-17 04:45:59 +0300551 /*
552 * llist_node is only used for poll deferred completions
553 */
554 struct llist_node llist_node;
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300555 bool in_async;
556 bool needs_fixed_file;
Jens Axboed625c6e2019-12-17 19:53:05 -0700557 u8 opcode;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700558
559 struct io_ring_ctx *ctx;
Jens Axboeeac406c2019-11-14 12:09:58 -0700560 union {
561 struct list_head list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700562 struct hlist_node hash_node;
Jens Axboeeac406c2019-11-14 12:09:58 -0700563 };
Jens Axboe9e645e112019-05-10 16:07:28 -0600564 struct list_head link_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700565 unsigned int flags;
Jens Axboec16361c2019-01-17 08:39:48 -0700566 refcount_t refs;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700567 u64 user_data;
Jens Axboe9e645e112019-05-10 16:07:28 -0600568 u32 result;
Jens Axboede0617e2019-04-06 21:51:27 -0600569 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700570
Jens Axboefcb323c2019-10-24 12:39:47 -0600571 struct list_head inflight_entry;
572
Jens Axboe561fb042019-10-24 07:25:42 -0600573 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700574};
575
576#define IO_PLUG_THRESHOLD 2
Jens Axboedef596e2019-01-09 08:59:42 -0700577#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700578
Jens Axboe9a56a232019-01-09 09:06:50 -0700579struct io_submit_state {
580 struct blk_plug plug;
581
582 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700583 * io_kiocb alloc cache
584 */
585 void *reqs[IO_IOPOLL_BATCH];
Pavel Begunkov6c8a3132020-02-01 03:58:00 +0300586 unsigned int free_reqs;
Jens Axboe2579f912019-01-09 09:10:43 -0700587
588 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700589 * File reference cache
590 */
591 struct file *file;
592 unsigned int fd;
593 unsigned int has_refs;
594 unsigned int used_refs;
595 unsigned int ios_left;
596};
597
Jens Axboed3656342019-12-18 09:50:26 -0700598struct io_op_def {
599 /* needs req->io allocated for deferral/async */
600 unsigned async_ctx : 1;
601 /* needs current->mm setup, does mm access */
602 unsigned needs_mm : 1;
603 /* needs req->file assigned */
604 unsigned needs_file : 1;
605 /* needs req->file assigned IFF fd is >= 0 */
606 unsigned fd_non_neg : 1;
607 /* hash wq insertion if file is a regular file */
608 unsigned hash_reg_file : 1;
609 /* unbound wq insertion if file is a non-regular file */
610 unsigned unbound_nonreg_file : 1;
Jens Axboe66f4af92020-01-16 15:36:52 -0700611 /* opcode is not supported by this kernel */
612 unsigned not_supported : 1;
Jens Axboef86cd202020-01-29 13:46:44 -0700613 /* needs file table */
614 unsigned file_table : 1;
Jens Axboeff002b32020-02-07 16:05:21 -0700615 /* needs ->fs */
616 unsigned needs_fs : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700617};
618
619static const struct io_op_def io_op_defs[] = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300620 [IORING_OP_NOP] = {},
621 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700622 .async_ctx = 1,
623 .needs_mm = 1,
624 .needs_file = 1,
625 .unbound_nonreg_file = 1,
626 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300627 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700628 .async_ctx = 1,
629 .needs_mm = 1,
630 .needs_file = 1,
631 .hash_reg_file = 1,
632 .unbound_nonreg_file = 1,
633 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300634 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700635 .needs_file = 1,
636 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300637 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700638 .needs_file = 1,
639 .unbound_nonreg_file = 1,
640 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300641 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700642 .needs_file = 1,
643 .hash_reg_file = 1,
644 .unbound_nonreg_file = 1,
645 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300646 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700647 .needs_file = 1,
648 .unbound_nonreg_file = 1,
649 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300650 [IORING_OP_POLL_REMOVE] = {},
651 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700652 .needs_file = 1,
653 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300654 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700655 .async_ctx = 1,
656 .needs_mm = 1,
657 .needs_file = 1,
658 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700659 .needs_fs = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700660 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300661 [IORING_OP_RECVMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700662 .async_ctx = 1,
663 .needs_mm = 1,
664 .needs_file = 1,
665 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700666 .needs_fs = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700667 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300668 [IORING_OP_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700669 .async_ctx = 1,
670 .needs_mm = 1,
671 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300672 [IORING_OP_TIMEOUT_REMOVE] = {},
673 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700674 .needs_mm = 1,
675 .needs_file = 1,
676 .unbound_nonreg_file = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700677 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700678 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300679 [IORING_OP_ASYNC_CANCEL] = {},
680 [IORING_OP_LINK_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700681 .async_ctx = 1,
682 .needs_mm = 1,
683 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300684 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700685 .async_ctx = 1,
686 .needs_mm = 1,
687 .needs_file = 1,
688 .unbound_nonreg_file = 1,
689 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300690 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700691 .needs_file = 1,
692 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300693 [IORING_OP_OPENAT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700694 .needs_file = 1,
695 .fd_non_neg = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700696 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700697 .needs_fs = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700698 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300699 [IORING_OP_CLOSE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700700 .needs_file = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700701 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700702 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300703 [IORING_OP_FILES_UPDATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700704 .needs_mm = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700705 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700706 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300707 [IORING_OP_STATX] = {
Jens Axboed3656342019-12-18 09:50:26 -0700708 .needs_mm = 1,
709 .needs_file = 1,
710 .fd_non_neg = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700711 .needs_fs = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700712 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300713 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700714 .needs_mm = 1,
715 .needs_file = 1,
716 .unbound_nonreg_file = 1,
717 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300718 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700719 .needs_mm = 1,
720 .needs_file = 1,
721 .unbound_nonreg_file = 1,
722 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300723 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -0700724 .needs_file = 1,
725 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300726 [IORING_OP_MADVISE] = {
Jens Axboec1ca7572019-12-25 22:18:28 -0700727 .needs_mm = 1,
728 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300729 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700730 .needs_mm = 1,
731 .needs_file = 1,
732 .unbound_nonreg_file = 1,
733 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300734 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700735 .needs_mm = 1,
736 .needs_file = 1,
737 .unbound_nonreg_file = 1,
738 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300739 [IORING_OP_OPENAT2] = {
Jens Axboecebdb982020-01-08 17:59:24 -0700740 .needs_file = 1,
741 .fd_non_neg = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700742 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700743 .needs_fs = 1,
Jens Axboecebdb982020-01-08 17:59:24 -0700744 },
Jens Axboe3e4827b2020-01-08 15:18:09 -0700745 [IORING_OP_EPOLL_CTL] = {
746 .unbound_nonreg_file = 1,
747 .file_table = 1,
748 },
Jens Axboed3656342019-12-18 09:50:26 -0700749};
750
Jens Axboe561fb042019-10-24 07:25:42 -0600751static void io_wq_submit_work(struct io_wq_work **workptr);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700752static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800753static void io_put_req(struct io_kiocb *req);
Jens Axboe978db572019-11-14 22:39:04 -0700754static void __io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700755static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
756static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -0700757static int __io_sqe_files_update(struct io_ring_ctx *ctx,
758 struct io_uring_files_update *ip,
759 unsigned nr_args);
Jens Axboef86cd202020-01-29 13:46:44 -0700760static int io_grab_files(struct io_kiocb *req);
Jens Axboe2faf8522020-02-04 19:54:55 -0700761static void io_ring_file_ref_flush(struct fixed_file_data *data);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300762static void io_cleanup_req(struct io_kiocb *req);
Jens Axboede0617e2019-04-06 21:51:27 -0600763
Jens Axboe2b188cc2019-01-07 10:46:33 -0700764static struct kmem_cache *req_cachep;
765
766static const struct file_operations io_uring_fops;
767
768struct sock *io_uring_get_socket(struct file *file)
769{
770#if defined(CONFIG_UNIX)
771 if (file->f_op == &io_uring_fops) {
772 struct io_ring_ctx *ctx = file->private_data;
773
774 return ctx->ring_sock->sk;
775 }
776#endif
777 return NULL;
778}
779EXPORT_SYMBOL(io_uring_get_socket);
780
781static void io_ring_ctx_ref_free(struct percpu_ref *ref)
782{
783 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
784
Jens Axboe206aefd2019-11-07 18:27:42 -0700785 complete(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700786}
787
788static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
789{
790 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -0700791 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700792
793 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
794 if (!ctx)
795 return NULL;
796
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700797 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
798 if (!ctx->fallback_req)
799 goto err;
800
Jens Axboe206aefd2019-11-07 18:27:42 -0700801 ctx->completions = kmalloc(2 * sizeof(struct completion), GFP_KERNEL);
802 if (!ctx->completions)
803 goto err;
804
Jens Axboe78076bb2019-12-04 19:56:40 -0700805 /*
806 * Use 5 bits less than the max cq entries, that should give us around
807 * 32 entries per hash list if totally full and uniformly spread.
808 */
809 hash_bits = ilog2(p->cq_entries);
810 hash_bits -= 5;
811 if (hash_bits <= 0)
812 hash_bits = 1;
813 ctx->cancel_hash_bits = hash_bits;
814 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
815 GFP_KERNEL);
816 if (!ctx->cancel_hash)
817 goto err;
818 __hash_init(ctx->cancel_hash, 1U << hash_bits);
819
Roman Gushchin21482892019-05-07 10:01:48 -0700820 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -0700821 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
822 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700823
824 ctx->flags = p->flags;
825 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700826 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe206aefd2019-11-07 18:27:42 -0700827 init_completion(&ctx->completions[0]);
828 init_completion(&ctx->completions[1]);
Jens Axboe071698e2020-01-28 10:04:42 -0700829 idr_init(&ctx->personality_idr);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700830 mutex_init(&ctx->uring_lock);
831 init_waitqueue_head(&ctx->wait);
832 spin_lock_init(&ctx->completion_lock);
Jens Axboee94f1412019-12-19 12:06:02 -0700833 init_llist_head(&ctx->poll_llist);
Jens Axboedef596e2019-01-09 08:59:42 -0700834 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboede0617e2019-04-06 21:51:27 -0600835 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -0600836 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -0600837 init_waitqueue_head(&ctx->inflight_wait);
838 spin_lock_init(&ctx->inflight_lock);
839 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700840 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -0700841err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700842 if (ctx->fallback_req)
843 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe206aefd2019-11-07 18:27:42 -0700844 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -0700845 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -0700846 kfree(ctx);
847 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700848}
849
Bob Liu9d858b22019-11-13 18:06:25 +0800850static inline bool __req_need_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -0600851{
Jackie Liua197f662019-11-08 08:09:12 -0700852 struct io_ring_ctx *ctx = req->ctx;
853
Jens Axboe498ccd92019-10-25 10:04:25 -0600854 return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped
855 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboede0617e2019-04-06 21:51:27 -0600856}
857
Bob Liu9d858b22019-11-13 18:06:25 +0800858static inline bool req_need_defer(struct io_kiocb *req)
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600859{
Pavel Begunkov87987892020-01-18 01:22:30 +0300860 if (unlikely(req->flags & REQ_F_IO_DRAIN))
Bob Liu9d858b22019-11-13 18:06:25 +0800861 return __req_need_defer(req);
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600862
Bob Liu9d858b22019-11-13 18:06:25 +0800863 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600864}
865
866static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -0600867{
868 struct io_kiocb *req;
869
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600870 req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list);
Bob Liu9d858b22019-11-13 18:06:25 +0800871 if (req && !req_need_defer(req)) {
Jens Axboede0617e2019-04-06 21:51:27 -0600872 list_del_init(&req->list);
873 return req;
874 }
875
876 return NULL;
877}
878
Jens Axboe5262f562019-09-17 12:26:57 -0600879static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx)
880{
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600881 struct io_kiocb *req;
882
883 req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list);
Jens Axboe93bd25b2019-11-11 23:34:31 -0700884 if (req) {
885 if (req->flags & REQ_F_TIMEOUT_NOSEQ)
886 return NULL;
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800887 if (!__req_need_defer(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -0700888 list_del_init(&req->list);
889 return req;
890 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600891 }
892
893 return NULL;
Jens Axboe5262f562019-09-17 12:26:57 -0600894}
895
Jens Axboede0617e2019-04-06 21:51:27 -0600896static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700897{
Hristo Venev75b28af2019-08-26 17:23:46 +0000898 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700899
Pavel Begunkov07910152020-01-17 03:52:46 +0300900 /* order cqe stores with ring update */
901 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700902
Pavel Begunkov07910152020-01-17 03:52:46 +0300903 if (wq_has_sleeper(&ctx->cq_wait)) {
904 wake_up_interruptible(&ctx->cq_wait);
905 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700906 }
907}
908
Jens Axboecccf0ee2020-01-27 16:34:48 -0700909static inline void io_req_work_grab_env(struct io_kiocb *req,
910 const struct io_op_def *def)
Jens Axboe18d9be12019-09-10 09:13:05 -0600911{
Jens Axboecccf0ee2020-01-27 16:34:48 -0700912 if (!req->work.mm && def->needs_mm) {
913 mmgrab(current->mm);
914 req->work.mm = current->mm;
915 }
916 if (!req->work.creds)
917 req->work.creds = get_current_cred();
Jens Axboeff002b32020-02-07 16:05:21 -0700918 if (!req->work.fs && def->needs_fs) {
919 spin_lock(&current->fs->lock);
920 if (!current->fs->in_exec) {
921 req->work.fs = current->fs;
922 req->work.fs->users++;
923 } else {
924 req->work.flags |= IO_WQ_WORK_CANCEL;
925 }
926 spin_unlock(&current->fs->lock);
927 }
Jens Axboe6ab23142020-02-08 20:23:59 -0700928 if (!req->work.task_pid)
929 req->work.task_pid = task_pid_vnr(current);
Jens Axboecccf0ee2020-01-27 16:34:48 -0700930}
931
932static inline void io_req_work_drop_env(struct io_kiocb *req)
933{
934 if (req->work.mm) {
935 mmdrop(req->work.mm);
936 req->work.mm = NULL;
937 }
938 if (req->work.creds) {
939 put_cred(req->work.creds);
940 req->work.creds = NULL;
941 }
Jens Axboeff002b32020-02-07 16:05:21 -0700942 if (req->work.fs) {
943 struct fs_struct *fs = req->work.fs;
944
945 spin_lock(&req->work.fs->lock);
946 if (--fs->users)
947 fs = NULL;
948 spin_unlock(&req->work.fs->lock);
949 if (fs)
950 free_fs_struct(fs);
951 }
Jens Axboe561fb042019-10-24 07:25:42 -0600952}
953
Jens Axboe94ae5e72019-11-14 19:39:52 -0700954static inline bool io_prep_async_work(struct io_kiocb *req,
955 struct io_kiocb **link)
Jens Axboe561fb042019-10-24 07:25:42 -0600956{
Jens Axboed3656342019-12-18 09:50:26 -0700957 const struct io_op_def *def = &io_op_defs[req->opcode];
Jens Axboe561fb042019-10-24 07:25:42 -0600958 bool do_hashed = false;
Jens Axboe54a91f32019-09-10 09:15:04 -0600959
Jens Axboed3656342019-12-18 09:50:26 -0700960 if (req->flags & REQ_F_ISREG) {
961 if (def->hash_reg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -0700962 do_hashed = true;
Jens Axboed3656342019-12-18 09:50:26 -0700963 } else {
964 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -0700965 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -0600966 }
Jens Axboecccf0ee2020-01-27 16:34:48 -0700967
968 io_req_work_grab_env(req, def);
Jens Axboe54a91f32019-09-10 09:15:04 -0600969
Jens Axboe94ae5e72019-11-14 19:39:52 -0700970 *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -0600971 return do_hashed;
972}
973
Jackie Liua197f662019-11-08 08:09:12 -0700974static inline void io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -0600975{
Jackie Liua197f662019-11-08 08:09:12 -0700976 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe94ae5e72019-11-14 19:39:52 -0700977 struct io_kiocb *link;
978 bool do_hashed;
979
980 do_hashed = io_prep_async_work(req, &link);
Jens Axboe561fb042019-10-24 07:25:42 -0600981
982 trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work,
983 req->flags);
984 if (!do_hashed) {
985 io_wq_enqueue(ctx->io_wq, &req->work);
986 } else {
987 io_wq_enqueue_hashed(ctx->io_wq, &req->work,
988 file_inode(req->file));
989 }
Jens Axboe94ae5e72019-11-14 19:39:52 -0700990
991 if (link)
992 io_queue_linked_timeout(link);
Jens Axboe18d9be12019-09-10 09:13:05 -0600993}
994
Jens Axboe5262f562019-09-17 12:26:57 -0600995static void io_kill_timeout(struct io_kiocb *req)
996{
997 int ret;
998
Jens Axboe2d283902019-12-04 11:08:05 -0700999 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe5262f562019-09-17 12:26:57 -06001000 if (ret != -1) {
1001 atomic_inc(&req->ctx->cq_timeouts);
Jens Axboe842f9612019-10-29 12:34:10 -06001002 list_del_init(&req->list);
Pavel Begunkovf0e20b82020-03-07 01:15:22 +03001003 req->flags |= REQ_F_COMP_LOCKED;
Jens Axboe78e19bb2019-11-06 15:21:34 -07001004 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001005 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -06001006 }
1007}
1008
1009static void io_kill_timeouts(struct io_ring_ctx *ctx)
1010{
1011 struct io_kiocb *req, *tmp;
1012
1013 spin_lock_irq(&ctx->completion_lock);
1014 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
1015 io_kill_timeout(req);
1016 spin_unlock_irq(&ctx->completion_lock);
1017}
1018
Jens Axboede0617e2019-04-06 21:51:27 -06001019static void io_commit_cqring(struct io_ring_ctx *ctx)
1020{
1021 struct io_kiocb *req;
1022
Jens Axboe5262f562019-09-17 12:26:57 -06001023 while ((req = io_get_timeout_req(ctx)) != NULL)
1024 io_kill_timeout(req);
1025
Jens Axboede0617e2019-04-06 21:51:27 -06001026 __io_commit_cqring(ctx);
1027
Pavel Begunkov87987892020-01-18 01:22:30 +03001028 while ((req = io_get_deferred_req(ctx)) != NULL)
Jackie Liua197f662019-11-08 08:09:12 -07001029 io_queue_async_work(req);
Jens Axboede0617e2019-04-06 21:51:27 -06001030}
1031
Jens Axboe2b188cc2019-01-07 10:46:33 -07001032static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
1033{
Hristo Venev75b28af2019-08-26 17:23:46 +00001034 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001035 unsigned tail;
1036
1037 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +02001038 /*
1039 * writes to the cq entry need to come after reading head; the
1040 * control dependency is enough as we're using WRITE_ONCE to
1041 * fill the cq entry
1042 */
Hristo Venev75b28af2019-08-26 17:23:46 +00001043 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001044 return NULL;
1045
1046 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +00001047 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001048}
1049
Jens Axboef2842ab2020-01-08 11:04:00 -07001050static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1051{
Jens Axboef0b493e2020-02-01 21:30:11 -07001052 if (!ctx->cq_ev_fd)
1053 return false;
Jens Axboef2842ab2020-01-08 11:04:00 -07001054 if (!ctx->eventfd_async)
1055 return true;
1056 return io_wq_current_is_worker() || in_interrupt();
1057}
1058
Jens Axboef0b493e2020-02-01 21:30:11 -07001059static void __io_cqring_ev_posted(struct io_ring_ctx *ctx, bool trigger_ev)
Jens Axboe8c838782019-03-12 15:48:16 -06001060{
1061 if (waitqueue_active(&ctx->wait))
1062 wake_up(&ctx->wait);
1063 if (waitqueue_active(&ctx->sqo_wait))
1064 wake_up(&ctx->sqo_wait);
Jens Axboef0b493e2020-02-01 21:30:11 -07001065 if (trigger_ev)
Jens Axboe9b402842019-04-11 11:45:41 -06001066 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -06001067}
1068
Jens Axboef0b493e2020-02-01 21:30:11 -07001069static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
1070{
1071 __io_cqring_ev_posted(ctx, io_should_trigger_evfd(ctx));
1072}
1073
Jens Axboec4a2ed72019-11-21 21:01:26 -07001074/* Returns true if there are no backlogged entries after the flush */
1075static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001076{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001077 struct io_rings *rings = ctx->rings;
1078 struct io_uring_cqe *cqe;
1079 struct io_kiocb *req;
1080 unsigned long flags;
1081 LIST_HEAD(list);
1082
1083 if (!force) {
1084 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001085 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001086 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
1087 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001088 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001089 }
1090
1091 spin_lock_irqsave(&ctx->completion_lock, flags);
1092
1093 /* if force is set, the ring is going away. always drop after that */
1094 if (force)
Jens Axboe69b3e542020-01-08 11:01:46 -07001095 ctx->cq_overflow_flushed = 1;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001096
Jens Axboec4a2ed72019-11-21 21:01:26 -07001097 cqe = NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001098 while (!list_empty(&ctx->cq_overflow_list)) {
1099 cqe = io_get_cqring(ctx);
1100 if (!cqe && !force)
1101 break;
1102
1103 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
1104 list);
1105 list_move(&req->list, &list);
Jens Axboe2ca10252020-02-13 17:17:35 -07001106 req->flags &= ~REQ_F_OVERFLOW;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001107 if (cqe) {
1108 WRITE_ONCE(cqe->user_data, req->user_data);
1109 WRITE_ONCE(cqe->res, req->result);
1110 WRITE_ONCE(cqe->flags, 0);
1111 } else {
1112 WRITE_ONCE(ctx->rings->cq_overflow,
1113 atomic_inc_return(&ctx->cached_cq_overflow));
1114 }
1115 }
1116
1117 io_commit_cqring(ctx);
Jens Axboead3eb2c2019-12-18 17:12:20 -07001118 if (cqe) {
1119 clear_bit(0, &ctx->sq_check_overflow);
1120 clear_bit(0, &ctx->cq_check_overflow);
1121 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001122 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1123 io_cqring_ev_posted(ctx);
1124
1125 while (!list_empty(&list)) {
1126 req = list_first_entry(&list, struct io_kiocb, list);
1127 list_del(&req->list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001128 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001129 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001130
1131 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001132}
1133
Jens Axboe78e19bb2019-11-06 15:21:34 -07001134static void io_cqring_fill_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001135{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001136 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001137 struct io_uring_cqe *cqe;
1138
Jens Axboe78e19bb2019-11-06 15:21:34 -07001139 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001140
Jens Axboe2b188cc2019-01-07 10:46:33 -07001141 /*
1142 * If we can't get a cq entry, userspace overflowed the
1143 * submission (by quite a lot). Increment the overflow count in
1144 * the ring.
1145 */
1146 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001147 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001148 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001149 WRITE_ONCE(cqe->res, res);
1150 WRITE_ONCE(cqe->flags, 0);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001151 } else if (ctx->cq_overflow_flushed) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001152 WRITE_ONCE(ctx->rings->cq_overflow,
1153 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001154 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001155 if (list_empty(&ctx->cq_overflow_list)) {
1156 set_bit(0, &ctx->sq_check_overflow);
1157 set_bit(0, &ctx->cq_check_overflow);
1158 }
Jens Axboe2ca10252020-02-13 17:17:35 -07001159 req->flags |= REQ_F_OVERFLOW;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001160 refcount_inc(&req->refs);
1161 req->result = res;
1162 list_add_tail(&req->list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001163 }
1164}
1165
Jens Axboe78e19bb2019-11-06 15:21:34 -07001166static void io_cqring_add_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001167{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001168 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001169 unsigned long flags;
1170
1171 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001172 io_cqring_fill_event(req, res);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001173 io_commit_cqring(ctx);
1174 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1175
Jens Axboe8c838782019-03-12 15:48:16 -06001176 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001177}
1178
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001179static inline bool io_is_fallback_req(struct io_kiocb *req)
1180{
1181 return req == (struct io_kiocb *)
1182 ((unsigned long) req->ctx->fallback_req & ~1UL);
1183}
1184
1185static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
1186{
1187 struct io_kiocb *req;
1188
1189 req = ctx->fallback_req;
1190 if (!test_and_set_bit_lock(0, (unsigned long *) ctx->fallback_req))
1191 return req;
1192
1193 return NULL;
1194}
1195
Jens Axboe2579f912019-01-09 09:10:43 -07001196static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
1197 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001198{
Jens Axboefd6fab22019-03-14 16:30:06 -06001199 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001200 struct io_kiocb *req;
1201
Jens Axboe2579f912019-01-09 09:10:43 -07001202 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -06001203 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -07001204 if (unlikely(!req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001205 goto fallback;
Jens Axboe2579f912019-01-09 09:10:43 -07001206 } else if (!state->free_reqs) {
1207 size_t sz;
1208 int ret;
1209
1210 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -06001211 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
1212
1213 /*
1214 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1215 * retry single alloc to be on the safe side.
1216 */
1217 if (unlikely(ret <= 0)) {
1218 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1219 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001220 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -06001221 ret = 1;
1222 }
Jens Axboe2579f912019-01-09 09:10:43 -07001223 state->free_reqs = ret - 1;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001224 req = state->reqs[ret - 1];
Jens Axboe2579f912019-01-09 09:10:43 -07001225 } else {
Jens Axboe2579f912019-01-09 09:10:43 -07001226 state->free_reqs--;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001227 req = state->reqs[state->free_reqs];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001228 }
1229
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001230got_it:
Jens Axboe1a6b74f2019-12-02 10:33:15 -07001231 req->io = NULL;
Jens Axboe60c112b2019-06-21 10:20:18 -06001232 req->file = NULL;
Jens Axboe2579f912019-01-09 09:10:43 -07001233 req->ctx = ctx;
1234 req->flags = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06001235 /* one is dropped after submission, the other at completion */
1236 refcount_set(&req->refs, 2);
Jens Axboe9e645e112019-05-10 16:07:28 -06001237 req->result = 0;
Jens Axboe561fb042019-10-24 07:25:42 -06001238 INIT_IO_WORK(&req->work, io_wq_submit_work);
Jens Axboe2579f912019-01-09 09:10:43 -07001239 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001240fallback:
1241 req = io_get_fallback_req(ctx);
1242 if (req)
1243 goto got_it;
Pavel Begunkov6805b322019-10-08 02:18:42 +03001244 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001245 return NULL;
1246}
1247
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001248static void __io_req_do_free(struct io_kiocb *req)
Jens Axboedef596e2019-01-09 08:59:42 -07001249{
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001250 if (likely(!io_is_fallback_req(req)))
1251 kmem_cache_free(req_cachep, req);
1252 else
1253 clear_bit_unlock(0, (unsigned long *) req->ctx->fallback_req);
1254}
1255
Jens Axboec6ca97b302019-12-28 12:11:08 -07001256static void __io_req_aux_free(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001257{
Jens Axboefcb323c2019-10-24 12:39:47 -06001258 struct io_ring_ctx *ctx = req->ctx;
1259
Pavel Begunkov929a3af2020-02-19 00:19:09 +03001260 if (req->flags & REQ_F_NEED_CLEANUP)
1261 io_cleanup_req(req);
1262
YueHaibing96fd84d2020-01-07 22:22:44 +08001263 kfree(req->io);
Jens Axboe05f3fb32019-12-09 11:22:50 -07001264 if (req->file) {
1265 if (req->flags & REQ_F_FIXED_FILE)
1266 percpu_ref_put(&ctx->file_data->refs);
1267 else
1268 fput(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001269 }
Jens Axboecccf0ee2020-01-27 16:34:48 -07001270
1271 io_req_work_drop_env(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001272}
1273
1274static void __io_free_req(struct io_kiocb *req)
1275{
Jens Axboec6ca97b302019-12-28 12:11:08 -07001276 __io_req_aux_free(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001277
Jens Axboefcb323c2019-10-24 12:39:47 -06001278 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001279 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06001280 unsigned long flags;
1281
1282 spin_lock_irqsave(&ctx->inflight_lock, flags);
1283 list_del(&req->inflight_entry);
1284 if (waitqueue_active(&ctx->inflight_wait))
1285 wake_up(&ctx->inflight_wait);
1286 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1287 }
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001288
1289 percpu_ref_put(&req->ctx->refs);
1290 __io_req_do_free(req);
Jens Axboee65ef562019-03-12 10:16:44 -06001291}
1292
Jens Axboec6ca97b302019-12-28 12:11:08 -07001293struct req_batch {
1294 void *reqs[IO_IOPOLL_BATCH];
1295 int to_free;
1296 int need_iter;
1297};
1298
1299static void io_free_req_many(struct io_ring_ctx *ctx, struct req_batch *rb)
1300{
Jens Axboe10fef4b2020-01-09 07:52:28 -07001301 int fixed_refs = rb->to_free;
1302
Jens Axboec6ca97b302019-12-28 12:11:08 -07001303 if (!rb->to_free)
1304 return;
1305 if (rb->need_iter) {
1306 int i, inflight = 0;
1307 unsigned long flags;
1308
Jens Axboe10fef4b2020-01-09 07:52:28 -07001309 fixed_refs = 0;
Jens Axboec6ca97b302019-12-28 12:11:08 -07001310 for (i = 0; i < rb->to_free; i++) {
1311 struct io_kiocb *req = rb->reqs[i];
1312
Jens Axboe10fef4b2020-01-09 07:52:28 -07001313 if (req->flags & REQ_F_FIXED_FILE) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001314 req->file = NULL;
Jens Axboe10fef4b2020-01-09 07:52:28 -07001315 fixed_refs++;
1316 }
Jens Axboec6ca97b302019-12-28 12:11:08 -07001317 if (req->flags & REQ_F_INFLIGHT)
1318 inflight++;
Jens Axboec6ca97b302019-12-28 12:11:08 -07001319 __io_req_aux_free(req);
1320 }
1321 if (!inflight)
1322 goto do_free;
1323
1324 spin_lock_irqsave(&ctx->inflight_lock, flags);
1325 for (i = 0; i < rb->to_free; i++) {
1326 struct io_kiocb *req = rb->reqs[i];
1327
Jens Axboe10fef4b2020-01-09 07:52:28 -07001328 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001329 list_del(&req->inflight_entry);
1330 if (!--inflight)
1331 break;
1332 }
1333 }
1334 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1335
1336 if (waitqueue_active(&ctx->inflight_wait))
1337 wake_up(&ctx->inflight_wait);
1338 }
1339do_free:
1340 kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs);
Jens Axboe10fef4b2020-01-09 07:52:28 -07001341 if (fixed_refs)
1342 percpu_ref_put_many(&ctx->file_data->refs, fixed_refs);
Jens Axboec6ca97b302019-12-28 12:11:08 -07001343 percpu_ref_put_many(&ctx->refs, rb->to_free);
Jens Axboec6ca97b302019-12-28 12:11:08 -07001344 rb->to_free = rb->need_iter = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06001345}
1346
Jackie Liua197f662019-11-08 08:09:12 -07001347static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001348{
Jackie Liua197f662019-11-08 08:09:12 -07001349 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001350 int ret;
1351
Jens Axboe2d283902019-12-04 11:08:05 -07001352 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe2665abf2019-11-05 12:40:47 -07001353 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001354 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -07001355 io_commit_cqring(ctx);
1356 req->flags &= ~REQ_F_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001357 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001358 return true;
1359 }
1360
1361 return false;
1362}
1363
Jens Axboeba816ad2019-09-28 11:36:45 -06001364static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -06001365{
Jens Axboe2665abf2019-11-05 12:40:47 -07001366 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001367 bool wake_ev = false;
Jens Axboe9e645e112019-05-10 16:07:28 -06001368
Jens Axboe4d7dd462019-11-20 13:03:52 -07001369 /* Already got next link */
1370 if (req->flags & REQ_F_LINK_NEXT)
1371 return;
1372
Jens Axboe9e645e112019-05-10 16:07:28 -06001373 /*
1374 * The list should never be empty when we are called here. But could
1375 * potentially happen if the chain is messed up, check to be on the
1376 * safe side.
1377 */
Pavel Begunkov44932332019-12-05 16:16:35 +03001378 while (!list_empty(&req->link_list)) {
1379 struct io_kiocb *nxt = list_first_entry(&req->link_list,
1380 struct io_kiocb, link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001381
Pavel Begunkov44932332019-12-05 16:16:35 +03001382 if (unlikely((req->flags & REQ_F_LINK_TIMEOUT) &&
1383 (nxt->flags & REQ_F_TIMEOUT))) {
1384 list_del_init(&nxt->link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001385 wake_ev |= io_link_cancel_timeout(nxt);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001386 req->flags &= ~REQ_F_LINK_TIMEOUT;
1387 continue;
1388 }
Jens Axboe9e645e112019-05-10 16:07:28 -06001389
Pavel Begunkov44932332019-12-05 16:16:35 +03001390 list_del_init(&req->link_list);
1391 if (!list_empty(&nxt->link_list))
1392 nxt->flags |= REQ_F_LINK;
Pavel Begunkovb18fdf72019-11-21 23:21:02 +03001393 *nxtptr = nxt;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001394 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06001395 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001396
Jens Axboe4d7dd462019-11-20 13:03:52 -07001397 req->flags |= REQ_F_LINK_NEXT;
Jens Axboe2665abf2019-11-05 12:40:47 -07001398 if (wake_ev)
1399 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001400}
1401
1402/*
1403 * Called if REQ_F_LINK is set, and we fail the head request
1404 */
1405static void io_fail_links(struct io_kiocb *req)
1406{
Jens Axboe2665abf2019-11-05 12:40:47 -07001407 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001408 unsigned long flags;
1409
1410 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06001411
1412 while (!list_empty(&req->link_list)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03001413 struct io_kiocb *link = list_first_entry(&req->link_list,
1414 struct io_kiocb, link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06001415
Pavel Begunkov44932332019-12-05 16:16:35 +03001416 list_del_init(&link->link_list);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001417 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001418
1419 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
Jens Axboed625c6e2019-12-17 19:53:05 -07001420 link->opcode == IORING_OP_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -07001421 io_link_cancel_timeout(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001422 } else {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001423 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe978db572019-11-14 22:39:04 -07001424 __io_double_put_req(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001425 }
Jens Axboe5d960722019-11-19 15:31:28 -07001426 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -06001427 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001428
1429 io_commit_cqring(ctx);
1430 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1431 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001432}
1433
Jens Axboe4d7dd462019-11-20 13:03:52 -07001434static void io_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -06001435{
Jens Axboe4d7dd462019-11-20 13:03:52 -07001436 if (likely(!(req->flags & REQ_F_LINK)))
Jens Axboe2665abf2019-11-05 12:40:47 -07001437 return;
Jens Axboe2665abf2019-11-05 12:40:47 -07001438
Jens Axboe9e645e112019-05-10 16:07:28 -06001439 /*
1440 * If LINK is set, we have dependent requests in this chain. If we
1441 * didn't fail this request, queue the first one up, moving any other
1442 * dependencies to the next request. In case of failure, fail the rest
1443 * of the chain.
1444 */
Jens Axboe2665abf2019-11-05 12:40:47 -07001445 if (req->flags & REQ_F_FAIL_LINK) {
1446 io_fail_links(req);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001447 } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) ==
1448 REQ_F_LINK_TIMEOUT) {
Jens Axboe2665abf2019-11-05 12:40:47 -07001449 struct io_ring_ctx *ctx = req->ctx;
1450 unsigned long flags;
1451
1452 /*
1453 * If this is a timeout link, we could be racing with the
1454 * timeout timer. Grab the completion lock for this case to
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001455 * protect against that.
Jens Axboe2665abf2019-11-05 12:40:47 -07001456 */
1457 spin_lock_irqsave(&ctx->completion_lock, flags);
1458 io_req_link_next(req, nxt);
1459 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1460 } else {
1461 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -06001462 }
Jens Axboe4d7dd462019-11-20 13:03:52 -07001463}
Jens Axboe9e645e112019-05-10 16:07:28 -06001464
Jackie Liuc69f8db2019-11-09 11:00:08 +08001465static void io_free_req(struct io_kiocb *req)
1466{
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001467 struct io_kiocb *nxt = NULL;
1468
1469 io_req_find_next(req, &nxt);
Pavel Begunkov70cf9f32019-11-21 23:21:00 +03001470 __io_free_req(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001471
1472 if (nxt)
1473 io_queue_async_work(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08001474}
1475
Jens Axboeba816ad2019-09-28 11:36:45 -06001476/*
1477 * Drop reference to request, return next in chain (if there is one) if this
1478 * was the last reference to this request.
1479 */
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001480__attribute__((nonnull))
Jackie Liuec9c02a2019-11-08 23:50:36 +08001481static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboee65ef562019-03-12 10:16:44 -06001482{
Jens Axboe2a44f462020-02-25 13:25:41 -07001483 if (refcount_dec_and_test(&req->refs)) {
1484 io_req_find_next(req, nxtptr);
Jens Axboe4d7dd462019-11-20 13:03:52 -07001485 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07001486 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001487}
1488
Jens Axboe2b188cc2019-01-07 10:46:33 -07001489static void io_put_req(struct io_kiocb *req)
1490{
Jens Axboedef596e2019-01-09 08:59:42 -07001491 if (refcount_dec_and_test(&req->refs))
1492 io_free_req(req);
1493}
1494
Jens Axboe978db572019-11-14 22:39:04 -07001495/*
1496 * Must only be used if we don't need to care about links, usually from
1497 * within the completion handling itself.
1498 */
1499static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06001500{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001501 /* drop both submit and complete references */
1502 if (refcount_sub_and_test(2, &req->refs))
1503 __io_free_req(req);
1504}
1505
Jens Axboe978db572019-11-14 22:39:04 -07001506static void io_double_put_req(struct io_kiocb *req)
1507{
1508 /* drop both submit and complete references */
1509 if (refcount_sub_and_test(2, &req->refs))
1510 io_free_req(req);
1511}
1512
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001513static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06001514{
Jens Axboe84f97dc2019-11-06 11:27:53 -07001515 struct io_rings *rings = ctx->rings;
1516
Jens Axboead3eb2c2019-12-18 17:12:20 -07001517 if (test_bit(0, &ctx->cq_check_overflow)) {
1518 /*
1519 * noflush == true is from the waitqueue handler, just ensure
1520 * we wake up the task, and the next invocation will flush the
1521 * entries. We cannot safely to it from here.
1522 */
1523 if (noflush && !list_empty(&ctx->cq_overflow_list))
1524 return -1U;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001525
Jens Axboead3eb2c2019-12-18 17:12:20 -07001526 io_cqring_overflow_flush(ctx, false);
1527 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001528
Jens Axboea3a0e432019-08-20 11:03:11 -06001529 /* See comment at the top of this file */
1530 smp_rmb();
Jens Axboead3eb2c2019-12-18 17:12:20 -07001531 return ctx->cached_cq_tail - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06001532}
1533
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03001534static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1535{
1536 struct io_rings *rings = ctx->rings;
1537
1538 /* make sure SQ entry isn't read before tail */
1539 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1540}
1541
Jens Axboe8237e042019-12-28 10:48:22 -07001542static inline bool io_req_multi_free(struct req_batch *rb, struct io_kiocb *req)
Jens Axboee94f1412019-12-19 12:06:02 -07001543{
Jens Axboec6ca97b302019-12-28 12:11:08 -07001544 if ((req->flags & REQ_F_LINK) || io_is_fallback_req(req))
1545 return false;
Jens Axboee94f1412019-12-19 12:06:02 -07001546
Jens Axboec6ca97b302019-12-28 12:11:08 -07001547 if (!(req->flags & REQ_F_FIXED_FILE) || req->io)
1548 rb->need_iter++;
1549
1550 rb->reqs[rb->to_free++] = req;
1551 if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
1552 io_free_req_many(req->ctx, rb);
1553 return true;
Jens Axboee94f1412019-12-19 12:06:02 -07001554}
1555
Jens Axboedef596e2019-01-09 08:59:42 -07001556/*
1557 * Find and free completed poll iocbs
1558 */
1559static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
1560 struct list_head *done)
1561{
Jens Axboe8237e042019-12-28 10:48:22 -07001562 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07001563 struct io_kiocb *req;
Jens Axboedef596e2019-01-09 08:59:42 -07001564
Jens Axboec6ca97b302019-12-28 12:11:08 -07001565 rb.to_free = rb.need_iter = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001566 while (!list_empty(done)) {
1567 req = list_first_entry(done, struct io_kiocb, list);
1568 list_del(&req->list);
1569
Jens Axboe78e19bb2019-11-06 15:21:34 -07001570 io_cqring_fill_event(req, req->result);
Jens Axboedef596e2019-01-09 08:59:42 -07001571 (*nr_events)++;
1572
Jens Axboe8237e042019-12-28 10:48:22 -07001573 if (refcount_dec_and_test(&req->refs) &&
1574 !io_req_multi_free(&rb, req))
1575 io_free_req(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001576 }
Jens Axboedef596e2019-01-09 08:59:42 -07001577
Jens Axboe09bb8392019-03-13 12:39:28 -06001578 io_commit_cqring(ctx);
Jens Axboe8237e042019-12-28 10:48:22 -07001579 io_free_req_many(ctx, &rb);
Jens Axboedef596e2019-01-09 08:59:42 -07001580}
1581
1582static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1583 long min)
1584{
1585 struct io_kiocb *req, *tmp;
1586 LIST_HEAD(done);
1587 bool spin;
1588 int ret;
1589
1590 /*
1591 * Only spin for completions if we don't have multiple devices hanging
1592 * off our complete list, and we're under the requested amount.
1593 */
1594 spin = !ctx->poll_multi_file && *nr_events < min;
1595
1596 ret = 0;
1597 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
Jens Axboe9adbd452019-12-20 08:45:55 -07001598 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07001599
1600 /*
1601 * Move completed entries to our local list. If we find a
1602 * request that requires polling, break out and complete
1603 * the done list first, if we have entries there.
1604 */
1605 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
1606 list_move_tail(&req->list, &done);
1607 continue;
1608 }
1609 if (!list_empty(&done))
1610 break;
1611
1612 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1613 if (ret < 0)
1614 break;
1615
1616 if (ret && spin)
1617 spin = false;
1618 ret = 0;
1619 }
1620
1621 if (!list_empty(&done))
1622 io_iopoll_complete(ctx, nr_events, &done);
1623
1624 return ret;
1625}
1626
1627/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08001628 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07001629 * non-spinning poll check - we'll still enter the driver poll loop, but only
1630 * as a non-spinning completion check.
1631 */
1632static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
1633 long min)
1634{
Jens Axboe08f54392019-08-21 22:19:11 -06001635 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07001636 int ret;
1637
1638 ret = io_do_iopoll(ctx, nr_events, min);
1639 if (ret < 0)
1640 return ret;
1641 if (!min || *nr_events >= min)
1642 return 0;
1643 }
1644
1645 return 1;
1646}
1647
1648/*
1649 * We can't just wait for polled events to come to us, we have to actively
1650 * find and complete them.
1651 */
1652static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
1653{
1654 if (!(ctx->flags & IORING_SETUP_IOPOLL))
1655 return;
1656
1657 mutex_lock(&ctx->uring_lock);
1658 while (!list_empty(&ctx->poll_list)) {
1659 unsigned int nr_events = 0;
1660
1661 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -06001662
1663 /*
1664 * Ensure we allow local-to-the-cpu processing to take place,
1665 * in this case we need to ensure that we reap all events.
1666 */
1667 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -07001668 }
1669 mutex_unlock(&ctx->uring_lock);
1670}
1671
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08001672static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1673 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07001674{
Jens Axboe2b2ed972019-10-25 10:06:15 -06001675 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001676
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08001677 /*
1678 * We disallow the app entering submit/complete with polling, but we
1679 * still need to lock the ring to prevent racing with polled issue
1680 * that got punted to a workqueue.
1681 */
1682 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001683 do {
1684 int tmin = 0;
1685
Jens Axboe500f9fb2019-08-19 12:15:59 -06001686 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06001687 * Don't enter poll loop if we already have events pending.
1688 * If we do, we can potentially be spinning for commands that
1689 * already triggered a CQE (eg in error).
1690 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001691 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06001692 break;
1693
1694 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06001695 * If a submit got punted to a workqueue, we can have the
1696 * application entering polling for a command before it gets
1697 * issued. That app will hold the uring_lock for the duration
1698 * of the poll right here, so we need to take a breather every
1699 * now and then to ensure that the issue has a chance to add
1700 * the poll to the issued list. Otherwise we can spin here
1701 * forever, while the workqueue is stuck trying to acquire the
1702 * very same mutex.
1703 */
1704 if (!(++iters & 7)) {
1705 mutex_unlock(&ctx->uring_lock);
1706 mutex_lock(&ctx->uring_lock);
1707 }
1708
Jens Axboedef596e2019-01-09 08:59:42 -07001709 if (*nr_events < min)
1710 tmin = min - *nr_events;
1711
1712 ret = io_iopoll_getevents(ctx, nr_events, tmin);
1713 if (ret <= 0)
1714 break;
1715 ret = 0;
1716 } while (min && !*nr_events && !need_resched());
1717
Jens Axboe500f9fb2019-08-19 12:15:59 -06001718 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001719 return ret;
1720}
1721
Jens Axboe491381ce2019-10-17 09:20:46 -06001722static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001723{
Jens Axboe491381ce2019-10-17 09:20:46 -06001724 /*
1725 * Tell lockdep we inherited freeze protection from submission
1726 * thread.
1727 */
1728 if (req->flags & REQ_F_ISREG) {
1729 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001730
Jens Axboe491381ce2019-10-17 09:20:46 -06001731 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001732 }
Jens Axboe491381ce2019-10-17 09:20:46 -06001733 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001734}
1735
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001736static inline void req_set_fail_links(struct io_kiocb *req)
1737{
1738 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1739 req->flags |= REQ_F_FAIL_LINK;
1740}
1741
Jens Axboeba816ad2019-09-28 11:36:45 -06001742static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001743{
Jens Axboe9adbd452019-12-20 08:45:55 -07001744 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001745
Jens Axboe491381ce2019-10-17 09:20:46 -06001746 if (kiocb->ki_flags & IOCB_WRITE)
1747 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001748
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001749 if (res != req->result)
1750 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001751 io_cqring_add_event(req, res);
Jens Axboeba816ad2019-09-28 11:36:45 -06001752}
1753
1754static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1755{
Jens Axboe9adbd452019-12-20 08:45:55 -07001756 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06001757
1758 io_complete_rw_common(kiocb, res);
Jens Axboee65ef562019-03-12 10:16:44 -06001759 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001760}
1761
Jens Axboeba816ad2019-09-28 11:36:45 -06001762static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res)
1763{
Jens Axboe9adbd452019-12-20 08:45:55 -07001764 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001765 struct io_kiocb *nxt = NULL;
Jens Axboeba816ad2019-09-28 11:36:45 -06001766
1767 io_complete_rw_common(kiocb, res);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001768 io_put_req_find_next(req, &nxt);
1769
1770 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001771}
1772
Jens Axboedef596e2019-01-09 08:59:42 -07001773static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1774{
Jens Axboe9adbd452019-12-20 08:45:55 -07001775 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07001776
Jens Axboe491381ce2019-10-17 09:20:46 -06001777 if (kiocb->ki_flags & IOCB_WRITE)
1778 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001779
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001780 if (res != req->result)
1781 req_set_fail_links(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06001782 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -07001783 if (res != -EAGAIN)
1784 req->flags |= REQ_F_IOPOLL_COMPLETED;
1785}
1786
1787/*
1788 * After the iocb has been issued, it's safe to be found on the poll list.
1789 * Adding the kiocb to the list AFTER submission ensures that we don't
1790 * find it from a io_iopoll_getevents() thread before the issuer is done
1791 * accessing the kiocb cookie.
1792 */
1793static void io_iopoll_req_issued(struct io_kiocb *req)
1794{
1795 struct io_ring_ctx *ctx = req->ctx;
1796
1797 /*
1798 * Track whether we have multiple files in our lists. This will impact
1799 * how we do polling eventually, not spinning if we're on potentially
1800 * different devices.
1801 */
1802 if (list_empty(&ctx->poll_list)) {
1803 ctx->poll_multi_file = false;
1804 } else if (!ctx->poll_multi_file) {
1805 struct io_kiocb *list_req;
1806
1807 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1808 list);
Jens Axboe9adbd452019-12-20 08:45:55 -07001809 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07001810 ctx->poll_multi_file = true;
1811 }
1812
1813 /*
1814 * For fast devices, IO may have already completed. If it has, add
1815 * it to the front so we find it first.
1816 */
1817 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1818 list_add(&req->list, &ctx->poll_list);
1819 else
1820 list_add_tail(&req->list, &ctx->poll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08001821
1822 if ((ctx->flags & IORING_SETUP_SQPOLL) &&
1823 wq_has_sleeper(&ctx->sqo_wait))
1824 wake_up(&ctx->sqo_wait);
Jens Axboedef596e2019-01-09 08:59:42 -07001825}
1826
Jens Axboe3d6770f2019-04-13 11:50:54 -06001827static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07001828{
Jens Axboe3d6770f2019-04-13 11:50:54 -06001829 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -07001830 int diff = state->has_refs - state->used_refs;
1831
1832 if (diff)
1833 fput_many(state->file, diff);
1834 state->file = NULL;
1835 }
1836}
1837
1838/*
1839 * Get as many references to a file as we have IOs left in this submission,
1840 * assuming most submissions are for one file, or at least that each file
1841 * has more than one submission.
1842 */
1843static struct file *io_file_get(struct io_submit_state *state, int fd)
1844{
1845 if (!state)
1846 return fget(fd);
1847
1848 if (state->file) {
1849 if (state->fd == fd) {
1850 state->used_refs++;
1851 state->ios_left--;
1852 return state->file;
1853 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06001854 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07001855 }
1856 state->file = fget_many(fd, state->ios_left);
1857 if (!state->file)
1858 return NULL;
1859
1860 state->fd = fd;
1861 state->has_refs = state->ios_left;
1862 state->used_refs = 1;
1863 state->ios_left--;
1864 return state->file;
1865}
1866
Jens Axboe2b188cc2019-01-07 10:46:33 -07001867/*
1868 * If we tracked the file through the SCM inflight mechanism, we could support
1869 * any file. For now, just ensure that anything potentially problematic is done
1870 * inline.
1871 */
1872static bool io_file_supports_async(struct file *file)
1873{
1874 umode_t mode = file_inode(file)->i_mode;
1875
Jens Axboe10d59342019-12-09 20:16:22 -07001876 if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07001877 return true;
1878 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
1879 return true;
1880
1881 return false;
1882}
1883
Jens Axboe3529d8c2019-12-19 18:24:38 -07001884static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1885 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001886{
Jens Axboedef596e2019-01-09 08:59:42 -07001887 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07001888 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06001889 unsigned ioprio;
1890 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001891
Jens Axboe491381ce2019-10-17 09:20:46 -06001892 if (S_ISREG(file_inode(req->file)->i_mode))
1893 req->flags |= REQ_F_ISREG;
1894
Jens Axboe2b188cc2019-01-07 10:46:33 -07001895 kiocb->ki_pos = READ_ONCE(sqe->off);
Jens Axboeba042912019-12-25 16:33:42 -07001896 if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) {
1897 req->flags |= REQ_F_CUR_POS;
1898 kiocb->ki_pos = req->file->f_pos;
1899 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001900 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03001901 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
1902 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
1903 if (unlikely(ret))
1904 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001905
1906 ioprio = READ_ONCE(sqe->ioprio);
1907 if (ioprio) {
1908 ret = ioprio_check_cap(ioprio);
1909 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06001910 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001911
1912 kiocb->ki_ioprio = ioprio;
1913 } else
1914 kiocb->ki_ioprio = get_current_ioprio();
1915
Stefan Bühler8449eed2019-04-27 20:34:19 +02001916 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06001917 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
1918 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02001919 req->flags |= REQ_F_NOWAIT;
1920
1921 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001922 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001923
Jens Axboedef596e2019-01-09 08:59:42 -07001924 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07001925 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
1926 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06001927 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001928
Jens Axboedef596e2019-01-09 08:59:42 -07001929 kiocb->ki_flags |= IOCB_HIPRI;
1930 kiocb->ki_complete = io_complete_rw_iopoll;
Jens Axboe6873e0b2019-10-30 13:53:09 -06001931 req->result = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001932 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001933 if (kiocb->ki_flags & IOCB_HIPRI)
1934 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07001935 kiocb->ki_complete = io_complete_rw;
1936 }
Jens Axboe9adbd452019-12-20 08:45:55 -07001937
Jens Axboe3529d8c2019-12-19 18:24:38 -07001938 req->rw.addr = READ_ONCE(sqe->addr);
1939 req->rw.len = READ_ONCE(sqe->len);
Jens Axboe9adbd452019-12-20 08:45:55 -07001940 /* we own ->private, reuse it for the buffer index */
1941 req->rw.kiocb.private = (void *) (unsigned long)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001942 READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001943 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001944}
1945
1946static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
1947{
1948 switch (ret) {
1949 case -EIOCBQUEUED:
1950 break;
1951 case -ERESTARTSYS:
1952 case -ERESTARTNOINTR:
1953 case -ERESTARTNOHAND:
1954 case -ERESTART_RESTARTBLOCK:
1955 /*
1956 * We can't just restart the syscall, since previously
1957 * submitted sqes may already be in progress. Just fail this
1958 * IO with EINTR.
1959 */
1960 ret = -EINTR;
1961 /* fall through */
1962 default:
1963 kiocb->ki_complete(kiocb, ret, 0);
1964 }
1965}
1966
Jens Axboeba816ad2019-09-28 11:36:45 -06001967static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt,
1968 bool in_async)
1969{
Jens Axboeba042912019-12-25 16:33:42 -07001970 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
1971
1972 if (req->flags & REQ_F_CUR_POS)
1973 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001974 if (in_async && ret >= 0 && kiocb->ki_complete == io_complete_rw)
Jens Axboeba816ad2019-09-28 11:36:45 -06001975 *nxt = __io_complete_rw(kiocb, ret);
1976 else
1977 io_rw_done(kiocb, ret);
1978}
1979
Jens Axboe9adbd452019-12-20 08:45:55 -07001980static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03001981 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07001982{
Jens Axboe9adbd452019-12-20 08:45:55 -07001983 struct io_ring_ctx *ctx = req->ctx;
1984 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001985 struct io_mapped_ubuf *imu;
1986 unsigned index, buf_index;
1987 size_t offset;
1988 u64 buf_addr;
1989
1990 /* attempt to use fixed buffers without having provided iovecs */
1991 if (unlikely(!ctx->user_bufs))
1992 return -EFAULT;
1993
Jens Axboe9adbd452019-12-20 08:45:55 -07001994 buf_index = (unsigned long) req->rw.kiocb.private;
Jens Axboeedafcce2019-01-09 09:16:05 -07001995 if (unlikely(buf_index >= ctx->nr_user_bufs))
1996 return -EFAULT;
1997
1998 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
1999 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07002000 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07002001
2002 /* overflow */
2003 if (buf_addr + len < buf_addr)
2004 return -EFAULT;
2005 /* not inside the mapped region */
2006 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
2007 return -EFAULT;
2008
2009 /*
2010 * May not be a start of buffer, set size appropriately
2011 * and advance us to the beginning.
2012 */
2013 offset = buf_addr - imu->ubuf;
2014 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06002015
2016 if (offset) {
2017 /*
2018 * Don't use iov_iter_advance() here, as it's really slow for
2019 * using the latter parts of a big fixed buffer - it iterates
2020 * over each segment manually. We can cheat a bit here, because
2021 * we know that:
2022 *
2023 * 1) it's a BVEC iter, we set it up
2024 * 2) all bvecs are PAGE_SIZE in size, except potentially the
2025 * first and last bvec
2026 *
2027 * So just find our index, and adjust the iterator afterwards.
2028 * If the offset is within the first bvec (or the whole first
2029 * bvec, just use iov_iter_advance(). This makes it easier
2030 * since we can just skip the first segment, which may not
2031 * be PAGE_SIZE aligned.
2032 */
2033 const struct bio_vec *bvec = imu->bvec;
2034
2035 if (offset <= bvec->bv_len) {
2036 iov_iter_advance(iter, offset);
2037 } else {
2038 unsigned long seg_skip;
2039
2040 /* skip first vec */
2041 offset -= bvec->bv_len;
2042 seg_skip = 1 + (offset >> PAGE_SHIFT);
2043
2044 iter->bvec = bvec + seg_skip;
2045 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02002046 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002047 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002048 }
2049 }
2050
Jens Axboe5e559562019-11-13 16:12:46 -07002051 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002052}
2053
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002054static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
2055 struct iovec **iovec, struct iov_iter *iter)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002056{
Jens Axboe9adbd452019-12-20 08:45:55 -07002057 void __user *buf = u64_to_user_ptr(req->rw.addr);
2058 size_t sqe_len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002059 u8 opcode;
2060
Jens Axboed625c6e2019-12-17 19:53:05 -07002061 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03002062 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07002063 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07002064 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07002065 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002066
Jens Axboe9adbd452019-12-20 08:45:55 -07002067 /* buffer index only valid with fixed read/write */
2068 if (req->rw.kiocb.private)
2069 return -EINVAL;
2070
Jens Axboe3a6820f2019-12-22 15:19:35 -07002071 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
2072 ssize_t ret;
2073 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
2074 *iovec = NULL;
Jens Axboe3a901592020-02-25 17:48:55 -07002075 return ret < 0 ? ret : sqe_len;
Jens Axboe3a6820f2019-12-22 15:19:35 -07002076 }
2077
Jens Axboef67676d2019-12-02 11:03:47 -07002078 if (req->io) {
2079 struct io_async_rw *iorw = &req->io->rw;
2080
2081 *iovec = iorw->iov;
2082 iov_iter_init(iter, rw, *iovec, iorw->nr_segs, iorw->size);
2083 if (iorw->iov == iorw->fast_iov)
2084 *iovec = NULL;
2085 return iorw->size;
2086 }
2087
Jens Axboe2b188cc2019-01-07 10:46:33 -07002088#ifdef CONFIG_COMPAT
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002089 if (req->ctx->compat)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002090 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
2091 iovec, iter);
2092#endif
2093
2094 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
2095}
2096
Jens Axboe32960612019-09-23 11:05:34 -06002097/*
2098 * For files that don't have ->read_iter() and ->write_iter(), handle them
2099 * by looping over ->read() or ->write() manually.
2100 */
2101static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
2102 struct iov_iter *iter)
2103{
2104 ssize_t ret = 0;
2105
2106 /*
2107 * Don't support polled IO through this interface, and we can't
2108 * support non-blocking either. For the latter, this just causes
2109 * the kiocb to be handled from an async context.
2110 */
2111 if (kiocb->ki_flags & IOCB_HIPRI)
2112 return -EOPNOTSUPP;
2113 if (kiocb->ki_flags & IOCB_NOWAIT)
2114 return -EAGAIN;
2115
2116 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002117 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06002118 ssize_t nr;
2119
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002120 if (!iov_iter_is_bvec(iter)) {
2121 iovec = iov_iter_iovec(iter);
2122 } else {
2123 /* fixed buffers import bvec */
2124 iovec.iov_base = kmap(iter->bvec->bv_page)
2125 + iter->iov_offset;
2126 iovec.iov_len = min(iter->count,
2127 iter->bvec->bv_len - iter->iov_offset);
2128 }
2129
Jens Axboe32960612019-09-23 11:05:34 -06002130 if (rw == READ) {
2131 nr = file->f_op->read(file, iovec.iov_base,
2132 iovec.iov_len, &kiocb->ki_pos);
2133 } else {
2134 nr = file->f_op->write(file, iovec.iov_base,
2135 iovec.iov_len, &kiocb->ki_pos);
2136 }
2137
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002138 if (iov_iter_is_bvec(iter))
2139 kunmap(iter->bvec->bv_page);
2140
Jens Axboe32960612019-09-23 11:05:34 -06002141 if (nr < 0) {
2142 if (!ret)
2143 ret = nr;
2144 break;
2145 }
2146 ret += nr;
2147 if (nr != iovec.iov_len)
2148 break;
2149 iov_iter_advance(iter, nr);
2150 }
2151
2152 return ret;
2153}
2154
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002155static void io_req_map_rw(struct io_kiocb *req, ssize_t io_size,
Jens Axboef67676d2019-12-02 11:03:47 -07002156 struct iovec *iovec, struct iovec *fast_iov,
2157 struct iov_iter *iter)
2158{
2159 req->io->rw.nr_segs = iter->nr_segs;
2160 req->io->rw.size = io_size;
2161 req->io->rw.iov = iovec;
2162 if (!req->io->rw.iov) {
2163 req->io->rw.iov = req->io->rw.fast_iov;
2164 memcpy(req->io->rw.iov, fast_iov,
2165 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002166 } else {
2167 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07002168 }
2169}
2170
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002171static int io_alloc_async_ctx(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07002172{
Jens Axboed3656342019-12-18 09:50:26 -07002173 if (!io_op_defs[req->opcode].async_ctx)
2174 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002175 req->io = kmalloc(sizeof(*req->io), GFP_KERNEL);
Jens Axboe06b76d42019-12-19 14:44:26 -07002176 return req->io == NULL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002177}
2178
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002179static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size,
2180 struct iovec *iovec, struct iovec *fast_iov,
2181 struct iov_iter *iter)
2182{
Jens Axboe980ad262020-01-24 23:08:54 -07002183 if (!io_op_defs[req->opcode].async_ctx)
Jens Axboe74566df2020-01-13 19:23:24 -07002184 return 0;
Jens Axboe5d204bc2020-01-31 12:06:52 -07002185 if (!req->io) {
2186 if (io_alloc_async_ctx(req))
2187 return -ENOMEM;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002188
Jens Axboe5d204bc2020-01-31 12:06:52 -07002189 io_req_map_rw(req, io_size, iovec, fast_iov, iter);
2190 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002191 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002192}
2193
Jens Axboe3529d8c2019-12-19 18:24:38 -07002194static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2195 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002196{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002197 struct io_async_ctx *io;
2198 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002199 ssize_t ret;
2200
Jens Axboe3529d8c2019-12-19 18:24:38 -07002201 ret = io_prep_rw(req, sqe, force_nonblock);
2202 if (ret)
2203 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002204
Jens Axboe3529d8c2019-12-19 18:24:38 -07002205 if (unlikely(!(req->file->f_mode & FMODE_READ)))
2206 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002207
Pavel Begunkov5f798be2020-02-08 13:28:02 +03002208 /* either don't need iovec imported or already have it */
2209 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002210 return 0;
2211
2212 io = req->io;
2213 io->rw.iov = io->rw.fast_iov;
2214 req->io = NULL;
2215 ret = io_import_iovec(READ, req, &io->rw.iov, &iter);
2216 req->io = io;
2217 if (ret < 0)
2218 return ret;
2219
2220 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2221 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002222}
2223
Pavel Begunkov267bc902019-11-07 01:41:08 +03002224static int io_read(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06002225 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002226{
2227 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002228 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002229 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002230 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002231 ssize_t io_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002232
Jens Axboe3529d8c2019-12-19 18:24:38 -07002233 ret = io_import_iovec(READ, req, &iovec, &iter);
Jens Axboe06b76d42019-12-19 14:44:26 -07002234 if (ret < 0)
2235 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002236
Jens Axboefd6c2e42019-12-18 12:19:41 -07002237 /* Ensure we clear previously set non-block flag */
2238 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07002239 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002240
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002241 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002242 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06002243 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07002244 req->result = io_size;
2245
2246 /*
2247 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2248 * we know to async punt it even if it was opened O_NONBLOCK
2249 */
Jens Axboe9adbd452019-12-20 08:45:55 -07002250 if (force_nonblock && !io_file_supports_async(req->file)) {
Jens Axboef67676d2019-12-02 11:03:47 -07002251 req->flags |= REQ_F_MUST_PUNT;
2252 goto copy_iov;
2253 }
Jens Axboe9e645e112019-05-10 16:07:28 -06002254
Jens Axboe31b51512019-01-18 22:56:34 -07002255 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002256 ret = rw_verify_area(READ, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002257 if (!ret) {
2258 ssize_t ret2;
2259
Jens Axboe9adbd452019-12-20 08:45:55 -07002260 if (req->file->f_op->read_iter)
2261 ret2 = call_read_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002262 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002263 ret2 = loop_rw_iter(READ, req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002264
Jens Axboe9d93a3f2019-05-15 13:53:07 -06002265 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboef67676d2019-12-02 11:03:47 -07002266 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002267 kiocb_done(kiocb, ret2, nxt, req->in_async);
Jens Axboef67676d2019-12-02 11:03:47 -07002268 } else {
2269copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002270 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002271 inline_vecs, &iter);
2272 if (ret)
2273 goto out_free;
2274 return -EAGAIN;
2275 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002276 }
Jens Axboef67676d2019-12-02 11:03:47 -07002277out_free:
Pavel Begunkov1e950812020-02-06 19:51:16 +03002278 kfree(iovec);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002279 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002280 return ret;
2281}
2282
Jens Axboe3529d8c2019-12-19 18:24:38 -07002283static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2284 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002285{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002286 struct io_async_ctx *io;
2287 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002288 ssize_t ret;
2289
Jens Axboe3529d8c2019-12-19 18:24:38 -07002290 ret = io_prep_rw(req, sqe, force_nonblock);
2291 if (ret)
2292 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002293
Jens Axboe3529d8c2019-12-19 18:24:38 -07002294 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
2295 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002296
Pavel Begunkov5f798be2020-02-08 13:28:02 +03002297 /* either don't need iovec imported or already have it */
2298 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002299 return 0;
2300
2301 io = req->io;
2302 io->rw.iov = io->rw.fast_iov;
2303 req->io = NULL;
2304 ret = io_import_iovec(WRITE, req, &io->rw.iov, &iter);
2305 req->io = io;
2306 if (ret < 0)
2307 return ret;
2308
2309 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2310 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002311}
2312
Pavel Begunkov267bc902019-11-07 01:41:08 +03002313static int io_write(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06002314 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002315{
2316 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002317 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002318 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002319 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002320 ssize_t ret, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002321
Jens Axboe3529d8c2019-12-19 18:24:38 -07002322 ret = io_import_iovec(WRITE, req, &iovec, &iter);
Jens Axboe06b76d42019-12-19 14:44:26 -07002323 if (ret < 0)
2324 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002325
Jens Axboefd6c2e42019-12-18 12:19:41 -07002326 /* Ensure we clear previously set non-block flag */
2327 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07002328 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002329
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002330 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002331 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06002332 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07002333 req->result = io_size;
2334
2335 /*
2336 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2337 * we know to async punt it even if it was opened O_NONBLOCK
2338 */
2339 if (force_nonblock && !io_file_supports_async(req->file)) {
2340 req->flags |= REQ_F_MUST_PUNT;
2341 goto copy_iov;
2342 }
2343
Jens Axboe10d59342019-12-09 20:16:22 -07002344 /* file path doesn't support NOWAIT for non-direct_IO */
2345 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
2346 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07002347 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06002348
Jens Axboe31b51512019-01-18 22:56:34 -07002349 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002350 ret = rw_verify_area(WRITE, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002351 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01002352 ssize_t ret2;
2353
Jens Axboe2b188cc2019-01-07 10:46:33 -07002354 /*
2355 * Open-code file_start_write here to grab freeze protection,
2356 * which will be released by another thread in
2357 * io_complete_rw(). Fool lockdep by telling it the lock got
2358 * released so that it doesn't complain about the held lock when
2359 * we return to userspace.
2360 */
Jens Axboe491381ce2019-10-17 09:20:46 -06002361 if (req->flags & REQ_F_ISREG) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002362 __sb_start_write(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002363 SB_FREEZE_WRITE, true);
Jens Axboe9adbd452019-12-20 08:45:55 -07002364 __sb_writers_release(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002365 SB_FREEZE_WRITE);
2366 }
2367 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01002368
Jens Axboe9adbd452019-12-20 08:45:55 -07002369 if (req->file->f_op->write_iter)
2370 ret2 = call_write_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002371 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002372 ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter);
Jens Axboefaac9962020-02-07 15:45:22 -07002373 /*
2374 * Raw bdev writes will -EOPNOTSUPP for IOCB_NOWAIT. Just
2375 * retry them without IOCB_NOWAIT.
2376 */
2377 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
2378 ret2 = -EAGAIN;
Jens Axboef67676d2019-12-02 11:03:47 -07002379 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002380 kiocb_done(kiocb, ret2, nxt, req->in_async);
Jens Axboef67676d2019-12-02 11:03:47 -07002381 } else {
2382copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002383 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002384 inline_vecs, &iter);
2385 if (ret)
2386 goto out_free;
2387 return -EAGAIN;
2388 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002389 }
Jens Axboe31b51512019-01-18 22:56:34 -07002390out_free:
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002391 req->flags &= ~REQ_F_NEED_CLEANUP;
Pavel Begunkov1e950812020-02-06 19:51:16 +03002392 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002393 return ret;
2394}
2395
2396/*
2397 * IORING_OP_NOP just posts a completion event, nothing else.
2398 */
Jens Axboe78e19bb2019-11-06 15:21:34 -07002399static int io_nop(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002400{
2401 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002402
Jens Axboedef596e2019-01-09 08:59:42 -07002403 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2404 return -EINVAL;
2405
Jens Axboe78e19bb2019-11-06 15:21:34 -07002406 io_cqring_add_event(req, 0);
Jens Axboee65ef562019-03-12 10:16:44 -06002407 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002408 return 0;
2409}
2410
Jens Axboe3529d8c2019-12-19 18:24:38 -07002411static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002412{
Jens Axboe6b063142019-01-10 22:13:58 -07002413 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002414
Jens Axboe09bb8392019-03-13 12:39:28 -06002415 if (!req->file)
2416 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002417
Jens Axboe6b063142019-01-10 22:13:58 -07002418 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07002419 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07002420 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002421 return -EINVAL;
2422
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002423 req->sync.flags = READ_ONCE(sqe->fsync_flags);
2424 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
2425 return -EINVAL;
2426
2427 req->sync.off = READ_ONCE(sqe->off);
2428 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002429 return 0;
2430}
2431
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002432static bool io_req_cancelled(struct io_kiocb *req)
2433{
2434 if (req->work.flags & IO_WQ_WORK_CANCEL) {
2435 req_set_fail_links(req);
2436 io_cqring_add_event(req, -ECANCELED);
2437 io_put_req(req);
2438 return true;
2439 }
2440
2441 return false;
2442}
2443
Jens Axboe78912932020-01-14 22:09:06 -07002444static void io_link_work_cb(struct io_wq_work **workptr)
2445{
2446 struct io_wq_work *work = *workptr;
2447 struct io_kiocb *link = work->data;
2448
2449 io_queue_linked_timeout(link);
2450 work->func = io_wq_submit_work;
2451}
2452
2453static void io_wq_assign_next(struct io_wq_work **workptr, struct io_kiocb *nxt)
2454{
2455 struct io_kiocb *link;
2456
2457 io_prep_async_work(nxt, &link);
2458 *workptr = &nxt->work;
2459 if (link) {
2460 nxt->work.flags |= IO_WQ_WORK_CB;
2461 nxt->work.func = io_link_work_cb;
2462 nxt->work.data = link;
2463 }
2464}
2465
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002466static void io_fsync_finish(struct io_wq_work **workptr)
2467{
2468 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2469 loff_t end = req->sync.off + req->sync.len;
2470 struct io_kiocb *nxt = NULL;
2471 int ret;
2472
2473 if (io_req_cancelled(req))
2474 return;
2475
Jens Axboe9adbd452019-12-20 08:45:55 -07002476 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002477 end > 0 ? end : LLONG_MAX,
2478 req->sync.flags & IORING_FSYNC_DATASYNC);
2479 if (ret < 0)
2480 req_set_fail_links(req);
2481 io_cqring_add_event(req, ret);
2482 io_put_req_find_next(req, &nxt);
2483 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07002484 io_wq_assign_next(workptr, nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002485}
2486
Jens Axboefc4df992019-12-10 14:38:45 -07002487static int io_fsync(struct io_kiocb *req, struct io_kiocb **nxt,
2488 bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002489{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002490 struct io_wq_work *work, *old_work;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002491
2492 /* fsync always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002493 if (force_nonblock) {
2494 io_put_req(req);
2495 req->work.func = io_fsync_finish;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002496 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002497 }
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002498
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002499 work = old_work = &req->work;
2500 io_fsync_finish(&work);
2501 if (work && work != old_work)
2502 *nxt = container_of(work, struct io_kiocb, work);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002503 return 0;
2504}
2505
Jens Axboed63d1b52019-12-10 10:38:56 -07002506static void io_fallocate_finish(struct io_wq_work **workptr)
2507{
2508 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2509 struct io_kiocb *nxt = NULL;
2510 int ret;
2511
Pavel Begunkov7fbeb952020-02-16 01:01:18 +03002512 if (io_req_cancelled(req))
2513 return;
2514
Jens Axboed63d1b52019-12-10 10:38:56 -07002515 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
2516 req->sync.len);
2517 if (ret < 0)
2518 req_set_fail_links(req);
2519 io_cqring_add_event(req, ret);
2520 io_put_req_find_next(req, &nxt);
2521 if (nxt)
2522 io_wq_assign_next(workptr, nxt);
2523}
2524
2525static int io_fallocate_prep(struct io_kiocb *req,
2526 const struct io_uring_sqe *sqe)
2527{
2528 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
2529 return -EINVAL;
2530
2531 req->sync.off = READ_ONCE(sqe->off);
2532 req->sync.len = READ_ONCE(sqe->addr);
2533 req->sync.mode = READ_ONCE(sqe->len);
2534 return 0;
2535}
2536
2537static int io_fallocate(struct io_kiocb *req, struct io_kiocb **nxt,
2538 bool force_nonblock)
2539{
2540 struct io_wq_work *work, *old_work;
2541
2542 /* fallocate always requiring blocking context */
2543 if (force_nonblock) {
2544 io_put_req(req);
2545 req->work.func = io_fallocate_finish;
2546 return -EAGAIN;
2547 }
2548
2549 work = old_work = &req->work;
2550 io_fallocate_finish(&work);
2551 if (work && work != old_work)
2552 *nxt = container_of(work, struct io_kiocb, work);
2553
2554 return 0;
2555}
2556
Jens Axboe15b71ab2019-12-11 11:20:36 -07002557static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2558{
Jens Axboef8748882020-01-08 17:47:02 -07002559 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002560 int ret;
2561
2562 if (sqe->ioprio || sqe->buf_index)
2563 return -EINVAL;
Jens Axboecf3040c2020-02-06 21:31:40 -07002564 if (sqe->flags & IOSQE_FIXED_FILE)
2565 return -EBADF;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03002566 if (req->flags & REQ_F_NEED_CLEANUP)
2567 return 0;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002568
2569 req->open.dfd = READ_ONCE(sqe->fd);
Jens Axboec12cedf2020-01-08 17:41:21 -07002570 req->open.how.mode = READ_ONCE(sqe->len);
Jens Axboef8748882020-01-08 17:47:02 -07002571 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboec12cedf2020-01-08 17:41:21 -07002572 req->open.how.flags = READ_ONCE(sqe->open_flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002573
Jens Axboef8748882020-01-08 17:47:02 -07002574 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002575 if (IS_ERR(req->open.filename)) {
2576 ret = PTR_ERR(req->open.filename);
2577 req->open.filename = NULL;
2578 return ret;
2579 }
2580
Jens Axboe4022e7a2020-03-19 19:23:18 -06002581 req->open.nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002582 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002583 return 0;
2584}
2585
Jens Axboecebdb982020-01-08 17:59:24 -07002586static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2587{
2588 struct open_how __user *how;
2589 const char __user *fname;
2590 size_t len;
2591 int ret;
2592
2593 if (sqe->ioprio || sqe->buf_index)
2594 return -EINVAL;
Jens Axboecf3040c2020-02-06 21:31:40 -07002595 if (sqe->flags & IOSQE_FIXED_FILE)
2596 return -EBADF;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03002597 if (req->flags & REQ_F_NEED_CLEANUP)
2598 return 0;
Jens Axboecebdb982020-01-08 17:59:24 -07002599
2600 req->open.dfd = READ_ONCE(sqe->fd);
2601 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
2602 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
2603 len = READ_ONCE(sqe->len);
2604
2605 if (len < OPEN_HOW_SIZE_VER0)
2606 return -EINVAL;
2607
2608 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
2609 len);
2610 if (ret)
2611 return ret;
2612
2613 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
2614 req->open.how.flags |= O_LARGEFILE;
2615
2616 req->open.filename = getname(fname);
2617 if (IS_ERR(req->open.filename)) {
2618 ret = PTR_ERR(req->open.filename);
2619 req->open.filename = NULL;
2620 return ret;
2621 }
2622
Jens Axboe4022e7a2020-03-19 19:23:18 -06002623 req->open.nofile = rlimit(RLIMIT_NOFILE);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002624 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboecebdb982020-01-08 17:59:24 -07002625 return 0;
2626}
2627
2628static int io_openat2(struct io_kiocb *req, struct io_kiocb **nxt,
2629 bool force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07002630{
2631 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002632 struct file *file;
2633 int ret;
2634
Jens Axboef86cd202020-01-29 13:46:44 -07002635 if (force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07002636 return -EAGAIN;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002637
Jens Axboecebdb982020-01-08 17:59:24 -07002638 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002639 if (ret)
2640 goto err;
2641
Jens Axboe4022e7a2020-03-19 19:23:18 -06002642 ret = __get_unused_fd_flags(req->open.how.flags, req->open.nofile);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002643 if (ret < 0)
2644 goto err;
2645
2646 file = do_filp_open(req->open.dfd, req->open.filename, &op);
2647 if (IS_ERR(file)) {
2648 put_unused_fd(ret);
2649 ret = PTR_ERR(file);
2650 } else {
2651 fsnotify_open(file);
2652 fd_install(ret, file);
2653 }
2654err:
2655 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002656 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002657 if (ret < 0)
2658 req_set_fail_links(req);
2659 io_cqring_add_event(req, ret);
2660 io_put_req_find_next(req, nxt);
2661 return 0;
2662}
2663
Jens Axboecebdb982020-01-08 17:59:24 -07002664static int io_openat(struct io_kiocb *req, struct io_kiocb **nxt,
2665 bool force_nonblock)
2666{
2667 req->open.how = build_open_how(req->open.how.flags, req->open.how.mode);
2668 return io_openat2(req, nxt, force_nonblock);
2669}
2670
Jens Axboe3e4827b2020-01-08 15:18:09 -07002671static int io_epoll_ctl_prep(struct io_kiocb *req,
2672 const struct io_uring_sqe *sqe)
2673{
2674#if defined(CONFIG_EPOLL)
2675 if (sqe->ioprio || sqe->buf_index)
2676 return -EINVAL;
2677
2678 req->epoll.epfd = READ_ONCE(sqe->fd);
2679 req->epoll.op = READ_ONCE(sqe->len);
2680 req->epoll.fd = READ_ONCE(sqe->off);
2681
2682 if (ep_op_has_event(req->epoll.op)) {
2683 struct epoll_event __user *ev;
2684
2685 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
2686 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
2687 return -EFAULT;
2688 }
2689
2690 return 0;
2691#else
2692 return -EOPNOTSUPP;
2693#endif
2694}
2695
2696static int io_epoll_ctl(struct io_kiocb *req, struct io_kiocb **nxt,
2697 bool force_nonblock)
2698{
2699#if defined(CONFIG_EPOLL)
2700 struct io_epoll *ie = &req->epoll;
2701 int ret;
2702
2703 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
2704 if (force_nonblock && ret == -EAGAIN)
2705 return -EAGAIN;
2706
2707 if (ret < 0)
2708 req_set_fail_links(req);
2709 io_cqring_add_event(req, ret);
2710 io_put_req_find_next(req, nxt);
2711 return 0;
2712#else
2713 return -EOPNOTSUPP;
2714#endif
2715}
2716
Jens Axboec1ca7572019-12-25 22:18:28 -07002717static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2718{
2719#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
2720 if (sqe->ioprio || sqe->buf_index || sqe->off)
2721 return -EINVAL;
2722
2723 req->madvise.addr = READ_ONCE(sqe->addr);
2724 req->madvise.len = READ_ONCE(sqe->len);
2725 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
2726 return 0;
2727#else
2728 return -EOPNOTSUPP;
2729#endif
2730}
2731
2732static int io_madvise(struct io_kiocb *req, struct io_kiocb **nxt,
2733 bool force_nonblock)
2734{
2735#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
2736 struct io_madvise *ma = &req->madvise;
2737 int ret;
2738
2739 if (force_nonblock)
2740 return -EAGAIN;
2741
2742 ret = do_madvise(ma->addr, ma->len, ma->advice);
2743 if (ret < 0)
2744 req_set_fail_links(req);
2745 io_cqring_add_event(req, ret);
2746 io_put_req_find_next(req, nxt);
2747 return 0;
2748#else
2749 return -EOPNOTSUPP;
2750#endif
2751}
2752
Jens Axboe4840e412019-12-25 22:03:45 -07002753static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2754{
2755 if (sqe->ioprio || sqe->buf_index || sqe->addr)
2756 return -EINVAL;
2757
2758 req->fadvise.offset = READ_ONCE(sqe->off);
2759 req->fadvise.len = READ_ONCE(sqe->len);
2760 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
2761 return 0;
2762}
2763
2764static int io_fadvise(struct io_kiocb *req, struct io_kiocb **nxt,
2765 bool force_nonblock)
2766{
2767 struct io_fadvise *fa = &req->fadvise;
2768 int ret;
2769
Jens Axboe3e694262020-02-01 09:22:49 -07002770 if (force_nonblock) {
2771 switch (fa->advice) {
2772 case POSIX_FADV_NORMAL:
2773 case POSIX_FADV_RANDOM:
2774 case POSIX_FADV_SEQUENTIAL:
2775 break;
2776 default:
2777 return -EAGAIN;
2778 }
2779 }
Jens Axboe4840e412019-12-25 22:03:45 -07002780
2781 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
2782 if (ret < 0)
2783 req_set_fail_links(req);
2784 io_cqring_add_event(req, ret);
2785 io_put_req_find_next(req, nxt);
2786 return 0;
2787}
2788
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002789static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2790{
Jens Axboef8748882020-01-08 17:47:02 -07002791 const char __user *fname;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002792 unsigned lookup_flags;
2793 int ret;
2794
2795 if (sqe->ioprio || sqe->buf_index)
2796 return -EINVAL;
Jens Axboecf3040c2020-02-06 21:31:40 -07002797 if (sqe->flags & IOSQE_FIXED_FILE)
2798 return -EBADF;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03002799 if (req->flags & REQ_F_NEED_CLEANUP)
2800 return 0;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002801
2802 req->open.dfd = READ_ONCE(sqe->fd);
2803 req->open.mask = READ_ONCE(sqe->len);
Jens Axboef8748882020-01-08 17:47:02 -07002804 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002805 req->open.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboec12cedf2020-01-08 17:41:21 -07002806 req->open.how.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002807
Jens Axboec12cedf2020-01-08 17:41:21 -07002808 if (vfs_stat_set_lookup_flags(&lookup_flags, req->open.how.flags))
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002809 return -EINVAL;
2810
Jens Axboef8748882020-01-08 17:47:02 -07002811 req->open.filename = getname_flags(fname, lookup_flags, NULL);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002812 if (IS_ERR(req->open.filename)) {
2813 ret = PTR_ERR(req->open.filename);
2814 req->open.filename = NULL;
2815 return ret;
2816 }
2817
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002818 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002819 return 0;
2820}
2821
2822static int io_statx(struct io_kiocb *req, struct io_kiocb **nxt,
2823 bool force_nonblock)
2824{
2825 struct io_open *ctx = &req->open;
2826 unsigned lookup_flags;
2827 struct path path;
2828 struct kstat stat;
2829 int ret;
2830
2831 if (force_nonblock)
2832 return -EAGAIN;
2833
Jens Axboec12cedf2020-01-08 17:41:21 -07002834 if (vfs_stat_set_lookup_flags(&lookup_flags, ctx->how.flags))
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002835 return -EINVAL;
2836
2837retry:
2838 /* filename_lookup() drops it, keep a reference */
2839 ctx->filename->refcnt++;
2840
2841 ret = filename_lookup(ctx->dfd, ctx->filename, lookup_flags, &path,
2842 NULL);
2843 if (ret)
2844 goto err;
2845
Jens Axboec12cedf2020-01-08 17:41:21 -07002846 ret = vfs_getattr(&path, &stat, ctx->mask, ctx->how.flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002847 path_put(&path);
2848 if (retry_estale(ret, lookup_flags)) {
2849 lookup_flags |= LOOKUP_REVAL;
2850 goto retry;
2851 }
2852 if (!ret)
2853 ret = cp_statx(&stat, ctx->buffer);
2854err:
2855 putname(ctx->filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002856 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002857 if (ret < 0)
2858 req_set_fail_links(req);
2859 io_cqring_add_event(req, ret);
2860 io_put_req_find_next(req, nxt);
2861 return 0;
2862}
2863
Jens Axboeb5dba592019-12-11 14:02:38 -07002864static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2865{
2866 /*
2867 * If we queue this for async, it must not be cancellable. That would
2868 * leave the 'file' in an undeterminate state.
2869 */
2870 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
2871
2872 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
2873 sqe->rw_flags || sqe->buf_index)
2874 return -EINVAL;
2875 if (sqe->flags & IOSQE_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07002876 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07002877
2878 req->close.fd = READ_ONCE(sqe->fd);
2879 if (req->file->f_op == &io_uring_fops ||
Pavel Begunkovb14cca02020-01-17 04:45:59 +03002880 req->close.fd == req->ctx->ring_fd)
Jens Axboeb5dba592019-12-11 14:02:38 -07002881 return -EBADF;
2882
2883 return 0;
2884}
2885
Pavel Begunkova93b3332020-02-08 14:04:34 +03002886/* only called when __close_fd_get_file() is done */
2887static void __io_close_finish(struct io_kiocb *req, struct io_kiocb **nxt)
2888{
2889 int ret;
2890
2891 ret = filp_close(req->close.put_file, req->work.files);
2892 if (ret < 0)
2893 req_set_fail_links(req);
2894 io_cqring_add_event(req, ret);
2895 fput(req->close.put_file);
2896 io_put_req_find_next(req, nxt);
2897}
2898
Jens Axboeb5dba592019-12-11 14:02:38 -07002899static void io_close_finish(struct io_wq_work **workptr)
2900{
2901 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2902 struct io_kiocb *nxt = NULL;
2903
Pavel Begunkov7fbeb952020-02-16 01:01:18 +03002904 /* not cancellable, don't do io_req_cancelled() */
Pavel Begunkova93b3332020-02-08 14:04:34 +03002905 __io_close_finish(req, &nxt);
Jens Axboeb5dba592019-12-11 14:02:38 -07002906 if (nxt)
2907 io_wq_assign_next(workptr, nxt);
2908}
2909
2910static int io_close(struct io_kiocb *req, struct io_kiocb **nxt,
2911 bool force_nonblock)
2912{
2913 int ret;
2914
2915 req->close.put_file = NULL;
2916 ret = __close_fd_get_file(req->close.fd, &req->close.put_file);
2917 if (ret < 0)
2918 return ret;
2919
2920 /* if the file has a flush method, be safe and punt to async */
Jens Axboef86cd202020-01-29 13:46:44 -07002921 if (req->close.put_file->f_op->flush && !io_wq_current_is_worker())
Jens Axboeb5dba592019-12-11 14:02:38 -07002922 goto eagain;
Jens Axboeb5dba592019-12-11 14:02:38 -07002923
2924 /*
2925 * No ->flush(), safely close from here and just punt the
2926 * fput() to async context.
2927 */
Pavel Begunkova93b3332020-02-08 14:04:34 +03002928 __io_close_finish(req, nxt);
2929 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07002930eagain:
2931 req->work.func = io_close_finish;
Jens Axboe1a417f42020-01-31 17:16:48 -07002932 /*
2933 * Do manual async queue here to avoid grabbing files - we don't
2934 * need the files, and it'll cause io_close_finish() to close
2935 * the file again and cause a double CQE entry for this request
2936 */
2937 io_queue_async_work(req);
2938 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07002939}
2940
Jens Axboe3529d8c2019-12-19 18:24:38 -07002941static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002942{
2943 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002944
2945 if (!req->file)
2946 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002947
2948 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2949 return -EINVAL;
2950 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
2951 return -EINVAL;
2952
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002953 req->sync.off = READ_ONCE(sqe->off);
2954 req->sync.len = READ_ONCE(sqe->len);
2955 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002956 return 0;
2957}
2958
2959static void io_sync_file_range_finish(struct io_wq_work **workptr)
2960{
2961 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2962 struct io_kiocb *nxt = NULL;
2963 int ret;
2964
2965 if (io_req_cancelled(req))
2966 return;
2967
Jens Axboe9adbd452019-12-20 08:45:55 -07002968 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002969 req->sync.flags);
2970 if (ret < 0)
2971 req_set_fail_links(req);
2972 io_cqring_add_event(req, ret);
2973 io_put_req_find_next(req, &nxt);
2974 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07002975 io_wq_assign_next(workptr, nxt);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002976}
2977
Jens Axboefc4df992019-12-10 14:38:45 -07002978static int io_sync_file_range(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002979 bool force_nonblock)
2980{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002981 struct io_wq_work *work, *old_work;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002982
2983 /* sync_file_range always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002984 if (force_nonblock) {
2985 io_put_req(req);
2986 req->work.func = io_sync_file_range_finish;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002987 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002988 }
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002989
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002990 work = old_work = &req->work;
2991 io_sync_file_range_finish(&work);
2992 if (work && work != old_work)
2993 *nxt = container_of(work, struct io_kiocb, work);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002994 return 0;
2995}
2996
Jens Axboe3529d8c2019-12-19 18:24:38 -07002997static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06002998{
Jens Axboe03b12302019-12-02 18:50:25 -07002999#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07003000 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003001 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003002 int ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003003
Jens Axboee47293f2019-12-20 08:58:21 -07003004 sr->msg_flags = READ_ONCE(sqe->msg_flags);
3005 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07003006 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003007
Jens Axboed8768362020-02-27 14:17:49 -07003008#ifdef CONFIG_COMPAT
3009 if (req->ctx->compat)
3010 sr->msg_flags |= MSG_CMSG_COMPAT;
3011#endif
3012
Jens Axboefddafac2020-01-04 20:19:44 -07003013 if (!io || req->opcode == IORING_OP_SEND)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003014 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003015 /* iovec is already imported */
3016 if (req->flags & REQ_F_NEED_CLEANUP)
3017 return 0;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003018
Jens Axboed9688562019-12-09 19:35:20 -07003019 io->msg.iov = io->msg.fast_iov;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003020 ret = sendmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07003021 &io->msg.iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003022 if (!ret)
3023 req->flags |= REQ_F_NEED_CLEANUP;
3024 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003025#else
Jens Axboee47293f2019-12-20 08:58:21 -07003026 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07003027#endif
3028}
3029
Jens Axboefc4df992019-12-10 14:38:45 -07003030static int io_sendmsg(struct io_kiocb *req, struct io_kiocb **nxt,
3031 bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07003032{
3033#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07003034 struct io_async_msghdr *kmsg = NULL;
Jens Axboe03b12302019-12-02 18:50:25 -07003035 struct socket *sock;
3036 int ret;
3037
3038 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3039 return -EINVAL;
3040
3041 sock = sock_from_file(req->file, &ret);
3042 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003043 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07003044 unsigned flags;
3045
Jens Axboe03b12302019-12-02 18:50:25 -07003046 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07003047 kmsg = &req->io->msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003048 kmsg->msg.msg_name = &req->io->msg.addr;
Jens Axboe0b416c32019-12-15 10:57:46 -07003049 /* if iov is set, it's allocated already */
3050 if (!kmsg->iov)
3051 kmsg->iov = kmsg->fast_iov;
3052 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07003053 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003054 struct io_sr_msg *sr = &req->sr_msg;
3055
Jens Axboe0b416c32019-12-15 10:57:46 -07003056 kmsg = &io.msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003057 kmsg->msg.msg_name = &io.msg.addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003058
3059 io.msg.iov = io.msg.fast_iov;
3060 ret = sendmsg_copy_msghdr(&io.msg.msg, sr->msg,
3061 sr->msg_flags, &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07003062 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003063 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003064 }
3065
Jens Axboee47293f2019-12-20 08:58:21 -07003066 flags = req->sr_msg.msg_flags;
3067 if (flags & MSG_DONTWAIT)
3068 req->flags |= REQ_F_NOWAIT;
3069 else if (force_nonblock)
3070 flags |= MSG_DONTWAIT;
3071
Jens Axboe0b416c32019-12-15 10:57:46 -07003072 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Jens Axboe03b12302019-12-02 18:50:25 -07003073 if (force_nonblock && ret == -EAGAIN) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003074 if (req->io)
3075 return -EAGAIN;
Pavel Begunkov1e950812020-02-06 19:51:16 +03003076 if (io_alloc_async_ctx(req)) {
Dan Carpenter297a31e2020-02-17 17:39:45 +03003077 if (kmsg->iov != kmsg->fast_iov)
Pavel Begunkov1e950812020-02-06 19:51:16 +03003078 kfree(kmsg->iov);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003079 return -ENOMEM;
Pavel Begunkov1e950812020-02-06 19:51:16 +03003080 }
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003081 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003082 memcpy(&req->io->msg, &io.msg, sizeof(io.msg));
Jens Axboe0b416c32019-12-15 10:57:46 -07003083 return -EAGAIN;
Jens Axboe03b12302019-12-02 18:50:25 -07003084 }
3085 if (ret == -ERESTARTSYS)
3086 ret = -EINTR;
3087 }
3088
Pavel Begunkov1e950812020-02-06 19:51:16 +03003089 if (kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07003090 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003091 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe03b12302019-12-02 18:50:25 -07003092 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003093 if (ret < 0)
3094 req_set_fail_links(req);
Jens Axboe03b12302019-12-02 18:50:25 -07003095 io_put_req_find_next(req, nxt);
3096 return 0;
3097#else
3098 return -EOPNOTSUPP;
3099#endif
3100}
3101
Jens Axboefddafac2020-01-04 20:19:44 -07003102static int io_send(struct io_kiocb *req, struct io_kiocb **nxt,
3103 bool force_nonblock)
3104{
3105#if defined(CONFIG_NET)
3106 struct socket *sock;
3107 int ret;
3108
3109 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3110 return -EINVAL;
3111
3112 sock = sock_from_file(req->file, &ret);
3113 if (sock) {
3114 struct io_sr_msg *sr = &req->sr_msg;
3115 struct msghdr msg;
3116 struct iovec iov;
3117 unsigned flags;
3118
3119 ret = import_single_range(WRITE, sr->buf, sr->len, &iov,
3120 &msg.msg_iter);
3121 if (ret)
3122 return ret;
3123
3124 msg.msg_name = NULL;
3125 msg.msg_control = NULL;
3126 msg.msg_controllen = 0;
3127 msg.msg_namelen = 0;
3128
3129 flags = req->sr_msg.msg_flags;
3130 if (flags & MSG_DONTWAIT)
3131 req->flags |= REQ_F_NOWAIT;
3132 else if (force_nonblock)
3133 flags |= MSG_DONTWAIT;
3134
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003135 msg.msg_flags = flags;
3136 ret = sock_sendmsg(sock, &msg);
Jens Axboefddafac2020-01-04 20:19:44 -07003137 if (force_nonblock && ret == -EAGAIN)
3138 return -EAGAIN;
3139 if (ret == -ERESTARTSYS)
3140 ret = -EINTR;
3141 }
3142
3143 io_cqring_add_event(req, ret);
3144 if (ret < 0)
3145 req_set_fail_links(req);
3146 io_put_req_find_next(req, nxt);
3147 return 0;
3148#else
3149 return -EOPNOTSUPP;
3150#endif
3151}
3152
Jens Axboe3529d8c2019-12-19 18:24:38 -07003153static int io_recvmsg_prep(struct io_kiocb *req,
3154 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07003155{
3156#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07003157 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003158 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003159 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07003160
Jens Axboe3529d8c2019-12-19 18:24:38 -07003161 sr->msg_flags = READ_ONCE(sqe->msg_flags);
3162 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003163 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003164
Jens Axboed8768362020-02-27 14:17:49 -07003165#ifdef CONFIG_COMPAT
3166 if (req->ctx->compat)
3167 sr->msg_flags |= MSG_CMSG_COMPAT;
3168#endif
3169
Jens Axboefddafac2020-01-04 20:19:44 -07003170 if (!io || req->opcode == IORING_OP_RECV)
Jens Axboe06b76d42019-12-19 14:44:26 -07003171 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003172 /* iovec is already imported */
3173 if (req->flags & REQ_F_NEED_CLEANUP)
3174 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07003175
Jens Axboed9688562019-12-09 19:35:20 -07003176 io->msg.iov = io->msg.fast_iov;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003177 ret = recvmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07003178 &io->msg.uaddr, &io->msg.iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003179 if (!ret)
3180 req->flags |= REQ_F_NEED_CLEANUP;
3181 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003182#else
Jens Axboee47293f2019-12-20 08:58:21 -07003183 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07003184#endif
3185}
3186
Jens Axboefc4df992019-12-10 14:38:45 -07003187static int io_recvmsg(struct io_kiocb *req, struct io_kiocb **nxt,
3188 bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07003189{
3190#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07003191 struct io_async_msghdr *kmsg = NULL;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003192 struct socket *sock;
3193 int ret;
3194
3195 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3196 return -EINVAL;
3197
3198 sock = sock_from_file(req->file, &ret);
3199 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003200 struct io_async_ctx io;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003201 unsigned flags;
3202
Jens Axboe03b12302019-12-02 18:50:25 -07003203 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07003204 kmsg = &req->io->msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003205 kmsg->msg.msg_name = &req->io->msg.addr;
Jens Axboe0b416c32019-12-15 10:57:46 -07003206 /* if iov is set, it's allocated already */
3207 if (!kmsg->iov)
3208 kmsg->iov = kmsg->fast_iov;
3209 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07003210 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003211 struct io_sr_msg *sr = &req->sr_msg;
3212
Jens Axboe0b416c32019-12-15 10:57:46 -07003213 kmsg = &io.msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003214 kmsg->msg.msg_name = &io.msg.addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003215
3216 io.msg.iov = io.msg.fast_iov;
3217 ret = recvmsg_copy_msghdr(&io.msg.msg, sr->msg,
3218 sr->msg_flags, &io.msg.uaddr,
3219 &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07003220 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003221 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003222 }
Jens Axboe0fa03c62019-04-19 13:34:07 -06003223
Jens Axboee47293f2019-12-20 08:58:21 -07003224 flags = req->sr_msg.msg_flags;
3225 if (flags & MSG_DONTWAIT)
3226 req->flags |= REQ_F_NOWAIT;
3227 else if (force_nonblock)
3228 flags |= MSG_DONTWAIT;
3229
3230 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.msg,
3231 kmsg->uaddr, flags);
Jens Axboe03b12302019-12-02 18:50:25 -07003232 if (force_nonblock && ret == -EAGAIN) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003233 if (req->io)
3234 return -EAGAIN;
Pavel Begunkov1e950812020-02-06 19:51:16 +03003235 if (io_alloc_async_ctx(req)) {
Dan Carpenter297a31e2020-02-17 17:39:45 +03003236 if (kmsg->iov != kmsg->fast_iov)
Pavel Begunkov1e950812020-02-06 19:51:16 +03003237 kfree(kmsg->iov);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003238 return -ENOMEM;
Pavel Begunkov1e950812020-02-06 19:51:16 +03003239 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003240 memcpy(&req->io->msg, &io.msg, sizeof(io.msg));
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003241 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe0b416c32019-12-15 10:57:46 -07003242 return -EAGAIN;
Jens Axboe03b12302019-12-02 18:50:25 -07003243 }
Jens Axboe441cdbd2019-12-02 18:49:10 -07003244 if (ret == -ERESTARTSYS)
3245 ret = -EINTR;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003246 }
3247
Pavel Begunkov1e950812020-02-06 19:51:16 +03003248 if (kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07003249 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003250 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe78e19bb2019-11-06 15:21:34 -07003251 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003252 if (ret < 0)
3253 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003254 io_put_req_find_next(req, nxt);
Jens Axboe0fa03c62019-04-19 13:34:07 -06003255 return 0;
3256#else
3257 return -EOPNOTSUPP;
3258#endif
3259}
3260
Jens Axboefddafac2020-01-04 20:19:44 -07003261static int io_recv(struct io_kiocb *req, struct io_kiocb **nxt,
3262 bool force_nonblock)
3263{
3264#if defined(CONFIG_NET)
3265 struct socket *sock;
3266 int ret;
3267
3268 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3269 return -EINVAL;
3270
3271 sock = sock_from_file(req->file, &ret);
3272 if (sock) {
3273 struct io_sr_msg *sr = &req->sr_msg;
3274 struct msghdr msg;
3275 struct iovec iov;
3276 unsigned flags;
3277
3278 ret = import_single_range(READ, sr->buf, sr->len, &iov,
3279 &msg.msg_iter);
3280 if (ret)
3281 return ret;
3282
3283 msg.msg_name = NULL;
3284 msg.msg_control = NULL;
3285 msg.msg_controllen = 0;
3286 msg.msg_namelen = 0;
3287 msg.msg_iocb = NULL;
3288 msg.msg_flags = 0;
3289
3290 flags = req->sr_msg.msg_flags;
3291 if (flags & MSG_DONTWAIT)
3292 req->flags |= REQ_F_NOWAIT;
3293 else if (force_nonblock)
3294 flags |= MSG_DONTWAIT;
3295
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003296 ret = sock_recvmsg(sock, &msg, flags);
Jens Axboefddafac2020-01-04 20:19:44 -07003297 if (force_nonblock && ret == -EAGAIN)
3298 return -EAGAIN;
3299 if (ret == -ERESTARTSYS)
3300 ret = -EINTR;
3301 }
3302
3303 io_cqring_add_event(req, ret);
3304 if (ret < 0)
3305 req_set_fail_links(req);
3306 io_put_req_find_next(req, nxt);
3307 return 0;
3308#else
3309 return -EOPNOTSUPP;
3310#endif
3311}
3312
3313
Jens Axboe3529d8c2019-12-19 18:24:38 -07003314static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003315{
3316#if defined(CONFIG_NET)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003317 struct io_accept *accept = &req->accept;
3318
Jens Axboe17f2fe32019-10-17 14:42:58 -06003319 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3320 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05003321 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003322 return -EINVAL;
3323
Jens Axboed55e5f52019-12-11 16:12:15 -07003324 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3325 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003326 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003327 return 0;
3328#else
3329 return -EOPNOTSUPP;
3330#endif
3331}
Jens Axboe17f2fe32019-10-17 14:42:58 -06003332
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003333#if defined(CONFIG_NET)
3334static int __io_accept(struct io_kiocb *req, struct io_kiocb **nxt,
3335 bool force_nonblock)
3336{
3337 struct io_accept *accept = &req->accept;
3338 unsigned file_flags;
3339 int ret;
3340
3341 file_flags = force_nonblock ? O_NONBLOCK : 0;
3342 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
3343 accept->addr_len, accept->flags);
3344 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003345 return -EAGAIN;
Jens Axboe8e3cca12019-11-09 19:52:33 -07003346 if (ret == -ERESTARTSYS)
3347 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003348 if (ret < 0)
3349 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07003350 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003351 io_put_req_find_next(req, nxt);
Jens Axboe17f2fe32019-10-17 14:42:58 -06003352 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003353}
3354
3355static void io_accept_finish(struct io_wq_work **workptr)
3356{
3357 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3358 struct io_kiocb *nxt = NULL;
3359
3360 if (io_req_cancelled(req))
3361 return;
3362 __io_accept(req, &nxt, false);
3363 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07003364 io_wq_assign_next(workptr, nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003365}
3366#endif
3367
3368static int io_accept(struct io_kiocb *req, struct io_kiocb **nxt,
3369 bool force_nonblock)
3370{
3371#if defined(CONFIG_NET)
3372 int ret;
3373
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003374 ret = __io_accept(req, nxt, force_nonblock);
3375 if (ret == -EAGAIN && force_nonblock) {
3376 req->work.func = io_accept_finish;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003377 io_put_req(req);
3378 return -EAGAIN;
3379 }
3380 return 0;
Jens Axboe17f2fe32019-10-17 14:42:58 -06003381#else
3382 return -EOPNOTSUPP;
3383#endif
3384}
3385
Jens Axboe3529d8c2019-12-19 18:24:38 -07003386static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07003387{
3388#if defined(CONFIG_NET)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003389 struct io_connect *conn = &req->connect;
3390 struct io_async_ctx *io = req->io;
Jens Axboef499a022019-12-02 16:28:46 -07003391
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003392 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3393 return -EINVAL;
3394 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
3395 return -EINVAL;
3396
Jens Axboe3529d8c2019-12-19 18:24:38 -07003397 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3398 conn->addr_len = READ_ONCE(sqe->addr2);
3399
3400 if (!io)
3401 return 0;
3402
3403 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003404 &io->connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07003405#else
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003406 return -EOPNOTSUPP;
Jens Axboef499a022019-12-02 16:28:46 -07003407#endif
3408}
3409
Jens Axboefc4df992019-12-10 14:38:45 -07003410static int io_connect(struct io_kiocb *req, struct io_kiocb **nxt,
3411 bool force_nonblock)
Jens Axboef8e85cf2019-11-23 14:24:24 -07003412{
3413#if defined(CONFIG_NET)
Jens Axboef499a022019-12-02 16:28:46 -07003414 struct io_async_ctx __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003415 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003416 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003417
Jens Axboef499a022019-12-02 16:28:46 -07003418 if (req->io) {
3419 io = req->io;
3420 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003421 ret = move_addr_to_kernel(req->connect.addr,
3422 req->connect.addr_len,
3423 &__io.connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07003424 if (ret)
3425 goto out;
3426 io = &__io;
3427 }
3428
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003429 file_flags = force_nonblock ? O_NONBLOCK : 0;
3430
3431 ret = __sys_connect_file(req->file, &io->connect.address,
3432 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07003433 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003434 if (req->io)
3435 return -EAGAIN;
3436 if (io_alloc_async_ctx(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07003437 ret = -ENOMEM;
3438 goto out;
3439 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003440 memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect));
Jens Axboef8e85cf2019-11-23 14:24:24 -07003441 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07003442 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07003443 if (ret == -ERESTARTSYS)
3444 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07003445out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003446 if (ret < 0)
3447 req_set_fail_links(req);
Jens Axboef8e85cf2019-11-23 14:24:24 -07003448 io_cqring_add_event(req, ret);
3449 io_put_req_find_next(req, nxt);
3450 return 0;
3451#else
3452 return -EOPNOTSUPP;
3453#endif
3454}
3455
Jens Axboe221c5eb2019-01-17 09:41:58 -07003456static void io_poll_remove_one(struct io_kiocb *req)
3457{
3458 struct io_poll_iocb *poll = &req->poll;
3459
3460 spin_lock(&poll->head->lock);
3461 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07003462 if (!list_empty(&poll->wait.entry)) {
3463 list_del_init(&poll->wait.entry);
Jackie Liua197f662019-11-08 08:09:12 -07003464 io_queue_async_work(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003465 }
3466 spin_unlock(&poll->head->lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07003467 hash_del(&req->hash_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003468}
3469
3470static void io_poll_remove_all(struct io_ring_ctx *ctx)
3471{
Jens Axboe78076bb2019-12-04 19:56:40 -07003472 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003473 struct io_kiocb *req;
Jens Axboe78076bb2019-12-04 19:56:40 -07003474 int i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003475
3476 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07003477 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
3478 struct hlist_head *list;
3479
3480 list = &ctx->cancel_hash[i];
3481 hlist_for_each_entry_safe(req, tmp, list, hash_node)
3482 io_poll_remove_one(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003483 }
3484 spin_unlock_irq(&ctx->completion_lock);
3485}
3486
Jens Axboe47f46762019-11-09 17:43:02 -07003487static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
3488{
Jens Axboe78076bb2019-12-04 19:56:40 -07003489 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07003490 struct io_kiocb *req;
3491
Jens Axboe78076bb2019-12-04 19:56:40 -07003492 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
3493 hlist_for_each_entry(req, list, hash_node) {
3494 if (sqe_addr == req->user_data) {
Jens Axboeeac406c2019-11-14 12:09:58 -07003495 io_poll_remove_one(req);
3496 return 0;
3497 }
Jens Axboe47f46762019-11-09 17:43:02 -07003498 }
3499
3500 return -ENOENT;
3501}
3502
Jens Axboe3529d8c2019-12-19 18:24:38 -07003503static int io_poll_remove_prep(struct io_kiocb *req,
3504 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003505{
Jens Axboe221c5eb2019-01-17 09:41:58 -07003506 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3507 return -EINVAL;
3508 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
3509 sqe->poll_events)
3510 return -EINVAL;
3511
Jens Axboe0969e782019-12-17 18:40:57 -07003512 req->poll.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07003513 return 0;
3514}
3515
3516/*
3517 * Find a running poll command that matches one specified in sqe->addr,
3518 * and remove it if found.
3519 */
3520static int io_poll_remove(struct io_kiocb *req)
3521{
3522 struct io_ring_ctx *ctx = req->ctx;
3523 u64 addr;
3524 int ret;
3525
Jens Axboe0969e782019-12-17 18:40:57 -07003526 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003527 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07003528 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003529 spin_unlock_irq(&ctx->completion_lock);
3530
Jens Axboe78e19bb2019-11-06 15:21:34 -07003531 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003532 if (ret < 0)
3533 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06003534 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003535 return 0;
3536}
3537
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003538static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003539{
Jackie Liua197f662019-11-08 08:09:12 -07003540 struct io_ring_ctx *ctx = req->ctx;
3541
Jens Axboe8c838782019-03-12 15:48:16 -06003542 req->poll.done = true;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003543 if (error)
3544 io_cqring_fill_event(req, error);
3545 else
3546 io_cqring_fill_event(req, mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06003547 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003548}
3549
Jens Axboe561fb042019-10-24 07:25:42 -06003550static void io_poll_complete_work(struct io_wq_work **workptr)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003551{
Jens Axboe561fb042019-10-24 07:25:42 -06003552 struct io_wq_work *work = *workptr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003553 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
3554 struct io_poll_iocb *poll = &req->poll;
3555 struct poll_table_struct pt = { ._key = poll->events };
3556 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe89723d02019-11-05 15:32:58 -07003557 struct io_kiocb *nxt = NULL;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003558 __poll_t mask = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003559 int ret = 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003560
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003561 if (work->flags & IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06003562 WRITE_ONCE(poll->canceled, true);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003563 ret = -ECANCELED;
3564 } else if (READ_ONCE(poll->canceled)) {
3565 ret = -ECANCELED;
3566 }
Jens Axboe561fb042019-10-24 07:25:42 -06003567
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003568 if (ret != -ECANCELED)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003569 mask = vfs_poll(poll->file, &pt) & poll->events;
3570
3571 /*
3572 * Note that ->ki_cancel callers also delete iocb from active_reqs after
3573 * calling ->ki_cancel. We need the ctx_lock roundtrip here to
3574 * synchronize with them. In the cancellation case the list_del_init
3575 * itself is not actually needed, but harmless so we keep it in to
3576 * avoid further branches in the fast path.
3577 */
3578 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003579 if (!mask && ret != -ECANCELED) {
Jens Axboe392edb42019-12-09 17:52:20 -07003580 add_wait_queue(poll->head, &poll->wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003581 spin_unlock_irq(&ctx->completion_lock);
3582 return;
3583 }
Jens Axboe78076bb2019-12-04 19:56:40 -07003584 hash_del(&req->hash_node);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003585 io_poll_complete(req, mask, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003586 spin_unlock_irq(&ctx->completion_lock);
3587
Jens Axboe8c838782019-03-12 15:48:16 -06003588 io_cqring_ev_posted(ctx);
Jens Axboe89723d02019-11-05 15:32:58 -07003589
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003590 if (ret < 0)
3591 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003592 io_put_req_find_next(req, &nxt);
Jens Axboe89723d02019-11-05 15:32:58 -07003593 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07003594 io_wq_assign_next(workptr, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003595}
3596
Jens Axboee94f1412019-12-19 12:06:02 -07003597static void __io_poll_flush(struct io_ring_ctx *ctx, struct llist_node *nodes)
3598{
Jens Axboee94f1412019-12-19 12:06:02 -07003599 struct io_kiocb *req, *tmp;
Jens Axboe8237e042019-12-28 10:48:22 -07003600 struct req_batch rb;
Jens Axboee94f1412019-12-19 12:06:02 -07003601
Jens Axboec6ca97b302019-12-28 12:11:08 -07003602 rb.to_free = rb.need_iter = 0;
Jens Axboee94f1412019-12-19 12:06:02 -07003603 spin_lock_irq(&ctx->completion_lock);
3604 llist_for_each_entry_safe(req, tmp, nodes, llist_node) {
3605 hash_del(&req->hash_node);
3606 io_poll_complete(req, req->result, 0);
3607
Jens Axboe8237e042019-12-28 10:48:22 -07003608 if (refcount_dec_and_test(&req->refs) &&
3609 !io_req_multi_free(&rb, req)) {
3610 req->flags |= REQ_F_COMP_LOCKED;
3611 io_free_req(req);
Jens Axboee94f1412019-12-19 12:06:02 -07003612 }
3613 }
3614 spin_unlock_irq(&ctx->completion_lock);
3615
3616 io_cqring_ev_posted(ctx);
Jens Axboe8237e042019-12-28 10:48:22 -07003617 io_free_req_many(ctx, &rb);
Jens Axboee94f1412019-12-19 12:06:02 -07003618}
3619
3620static void io_poll_flush(struct io_wq_work **workptr)
3621{
3622 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3623 struct llist_node *nodes;
3624
3625 nodes = llist_del_all(&req->ctx->poll_llist);
3626 if (nodes)
3627 __io_poll_flush(req->ctx, nodes);
3628}
3629
Jens Axboef0b493e2020-02-01 21:30:11 -07003630static void io_poll_trigger_evfd(struct io_wq_work **workptr)
3631{
3632 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3633
3634 eventfd_signal(req->ctx->cq_ev_fd, 1);
3635 io_put_req(req);
3636}
3637
Jens Axboe221c5eb2019-01-17 09:41:58 -07003638static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
3639 void *key)
3640{
Jens Axboee9444752019-11-26 15:02:04 -07003641 struct io_poll_iocb *poll = wait->private;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003642 struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
3643 struct io_ring_ctx *ctx = req->ctx;
3644 __poll_t mask = key_to_poll(key);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003645
3646 /* for instances that support it check for an event match first: */
Jens Axboe8c838782019-03-12 15:48:16 -06003647 if (mask && !(mask & poll->events))
3648 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003649
Jens Axboe392edb42019-12-09 17:52:20 -07003650 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06003651
Jens Axboe7c9e7f02019-11-12 08:15:53 -07003652 /*
3653 * Run completion inline if we can. We're using trylock here because
3654 * we are violating the completion_lock -> poll wq lock ordering.
3655 * If we have a link timeout we're going to need the completion_lock
3656 * for finalizing the request, mark us as having grabbed that already.
3657 */
Jens Axboee94f1412019-12-19 12:06:02 -07003658 if (mask) {
3659 unsigned long flags;
Jens Axboe8c838782019-03-12 15:48:16 -06003660
Jens Axboee94f1412019-12-19 12:06:02 -07003661 if (llist_empty(&ctx->poll_llist) &&
3662 spin_trylock_irqsave(&ctx->completion_lock, flags)) {
Jens Axboef0b493e2020-02-01 21:30:11 -07003663 bool trigger_ev;
3664
Jens Axboee94f1412019-12-19 12:06:02 -07003665 hash_del(&req->hash_node);
3666 io_poll_complete(req, mask, 0);
Jens Axboee94f1412019-12-19 12:06:02 -07003667
Jens Axboef0b493e2020-02-01 21:30:11 -07003668 trigger_ev = io_should_trigger_evfd(ctx);
3669 if (trigger_ev && eventfd_signal_count()) {
3670 trigger_ev = false;
3671 req->work.func = io_poll_trigger_evfd;
3672 } else {
3673 req->flags |= REQ_F_COMP_LOCKED;
3674 io_put_req(req);
3675 req = NULL;
3676 }
3677 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3678 __io_cqring_ev_posted(ctx, trigger_ev);
Jens Axboee94f1412019-12-19 12:06:02 -07003679 } else {
3680 req->result = mask;
3681 req->llist_node.next = NULL;
3682 /* if the list wasn't empty, we're done */
3683 if (!llist_add(&req->llist_node, &ctx->poll_llist))
3684 req = NULL;
3685 else
3686 req->work.func = io_poll_flush;
3687 }
Jens Axboe8c838782019-03-12 15:48:16 -06003688 }
Jens Axboee94f1412019-12-19 12:06:02 -07003689 if (req)
3690 io_queue_async_work(req);
Jens Axboe8c838782019-03-12 15:48:16 -06003691
Jens Axboe221c5eb2019-01-17 09:41:58 -07003692 return 1;
3693}
3694
3695struct io_poll_table {
3696 struct poll_table_struct pt;
3697 struct io_kiocb *req;
3698 int error;
3699};
3700
3701static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
3702 struct poll_table_struct *p)
3703{
3704 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
3705
3706 if (unlikely(pt->req->poll.head)) {
3707 pt->error = -EINVAL;
3708 return;
3709 }
3710
3711 pt->error = 0;
3712 pt->req->poll.head = head;
Jens Axboe392edb42019-12-09 17:52:20 -07003713 add_wait_queue(head, &pt->req->poll.wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003714}
3715
Jens Axboeeac406c2019-11-14 12:09:58 -07003716static void io_poll_req_insert(struct io_kiocb *req)
3717{
3718 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07003719 struct hlist_head *list;
Jens Axboeeac406c2019-11-14 12:09:58 -07003720
Jens Axboe78076bb2019-12-04 19:56:40 -07003721 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
3722 hlist_add_head(&req->hash_node, list);
Jens Axboeeac406c2019-11-14 12:09:58 -07003723}
3724
Jens Axboe3529d8c2019-12-19 18:24:38 -07003725static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003726{
3727 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003728 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003729
3730 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3731 return -EINVAL;
3732 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
3733 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06003734 if (!poll->file)
3735 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003736
Jens Axboe221c5eb2019-01-17 09:41:58 -07003737 events = READ_ONCE(sqe->poll_events);
3738 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
Jens Axboe0969e782019-12-17 18:40:57 -07003739 return 0;
3740}
3741
3742static int io_poll_add(struct io_kiocb *req, struct io_kiocb **nxt)
3743{
3744 struct io_poll_iocb *poll = &req->poll;
3745 struct io_ring_ctx *ctx = req->ctx;
3746 struct io_poll_table ipt;
3747 bool cancel = false;
3748 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07003749
3750 INIT_IO_WORK(&req->work, io_poll_complete_work);
Jens Axboe78076bb2019-12-04 19:56:40 -07003751 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003752
Jens Axboe221c5eb2019-01-17 09:41:58 -07003753 poll->head = NULL;
Jens Axboe8c838782019-03-12 15:48:16 -06003754 poll->done = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003755 poll->canceled = false;
3756
3757 ipt.pt._qproc = io_poll_queue_proc;
3758 ipt.pt._key = poll->events;
3759 ipt.req = req;
3760 ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
3761
3762 /* initialized the list so that we can do list_empty checks */
Jens Axboe392edb42019-12-09 17:52:20 -07003763 INIT_LIST_HEAD(&poll->wait.entry);
3764 init_waitqueue_func_entry(&poll->wait, io_poll_wake);
3765 poll->wait.private = poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003766
Jens Axboe36703242019-07-25 10:20:18 -06003767 INIT_LIST_HEAD(&req->list);
3768
Jens Axboe221c5eb2019-01-17 09:41:58 -07003769 mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003770
3771 spin_lock_irq(&ctx->completion_lock);
Jens Axboe8c838782019-03-12 15:48:16 -06003772 if (likely(poll->head)) {
3773 spin_lock(&poll->head->lock);
Jens Axboe392edb42019-12-09 17:52:20 -07003774 if (unlikely(list_empty(&poll->wait.entry))) {
Jens Axboe8c838782019-03-12 15:48:16 -06003775 if (ipt.error)
3776 cancel = true;
3777 ipt.error = 0;
3778 mask = 0;
3779 }
3780 if (mask || ipt.error)
Jens Axboe392edb42019-12-09 17:52:20 -07003781 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06003782 else if (cancel)
3783 WRITE_ONCE(poll->canceled, true);
3784 else if (!poll->done) /* actually waiting for an event */
Jens Axboeeac406c2019-11-14 12:09:58 -07003785 io_poll_req_insert(req);
Jens Axboe8c838782019-03-12 15:48:16 -06003786 spin_unlock(&poll->head->lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003787 }
Jens Axboe8c838782019-03-12 15:48:16 -06003788 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06003789 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003790 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06003791 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07003792 spin_unlock_irq(&ctx->completion_lock);
3793
Jens Axboe8c838782019-03-12 15:48:16 -06003794 if (mask) {
3795 io_cqring_ev_posted(ctx);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003796 io_put_req_find_next(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003797 }
Jens Axboe8c838782019-03-12 15:48:16 -06003798 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003799}
3800
Jens Axboe5262f562019-09-17 12:26:57 -06003801static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
3802{
Jens Axboead8a48a2019-11-15 08:49:11 -07003803 struct io_timeout_data *data = container_of(timer,
3804 struct io_timeout_data, timer);
3805 struct io_kiocb *req = data->req;
3806 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06003807 unsigned long flags;
3808
Jens Axboe5262f562019-09-17 12:26:57 -06003809 atomic_inc(&ctx->cq_timeouts);
3810
3811 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08003812 /*
Jens Axboe11365042019-10-16 09:08:32 -06003813 * We could be racing with timeout deletion. If the list is empty,
3814 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08003815 */
Jens Axboe842f9612019-10-29 12:34:10 -06003816 if (!list_empty(&req->list)) {
Jens Axboe11365042019-10-16 09:08:32 -06003817 struct io_kiocb *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06003818
Jens Axboe11365042019-10-16 09:08:32 -06003819 /*
3820 * Adjust the reqs sequence before the current one because it
Brian Gianforcarod195a662019-12-13 03:09:50 -08003821 * will consume a slot in the cq_ring and the cq_tail
Jens Axboe11365042019-10-16 09:08:32 -06003822 * pointer will be increased, otherwise other timeout reqs may
3823 * return in advance without waiting for enough wait_nr.
3824 */
3825 prev = req;
3826 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
3827 prev->sequence++;
Jens Axboe11365042019-10-16 09:08:32 -06003828 list_del_init(&req->list);
Jens Axboe11365042019-10-16 09:08:32 -06003829 }
Jens Axboe842f9612019-10-29 12:34:10 -06003830
Jens Axboe78e19bb2019-11-06 15:21:34 -07003831 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06003832 io_commit_cqring(ctx);
3833 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3834
3835 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003836 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06003837 io_put_req(req);
3838 return HRTIMER_NORESTART;
3839}
3840
Jens Axboe47f46762019-11-09 17:43:02 -07003841static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
3842{
3843 struct io_kiocb *req;
3844 int ret = -ENOENT;
3845
3846 list_for_each_entry(req, &ctx->timeout_list, list) {
3847 if (user_data == req->user_data) {
3848 list_del_init(&req->list);
3849 ret = 0;
3850 break;
3851 }
3852 }
3853
3854 if (ret == -ENOENT)
3855 return ret;
3856
Jens Axboe2d283902019-12-04 11:08:05 -07003857 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe47f46762019-11-09 17:43:02 -07003858 if (ret == -1)
3859 return -EALREADY;
3860
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003861 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07003862 io_cqring_fill_event(req, -ECANCELED);
3863 io_put_req(req);
3864 return 0;
3865}
3866
Jens Axboe3529d8c2019-12-19 18:24:38 -07003867static int io_timeout_remove_prep(struct io_kiocb *req,
3868 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07003869{
Jens Axboeb29472e2019-12-17 18:50:29 -07003870 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3871 return -EINVAL;
3872 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
3873 return -EINVAL;
3874
3875 req->timeout.addr = READ_ONCE(sqe->addr);
3876 req->timeout.flags = READ_ONCE(sqe->timeout_flags);
3877 if (req->timeout.flags)
3878 return -EINVAL;
3879
Jens Axboeb29472e2019-12-17 18:50:29 -07003880 return 0;
3881}
3882
Jens Axboe11365042019-10-16 09:08:32 -06003883/*
3884 * Remove or update an existing timeout command
3885 */
Jens Axboefc4df992019-12-10 14:38:45 -07003886static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06003887{
3888 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07003889 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06003890
Jens Axboe11365042019-10-16 09:08:32 -06003891 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb29472e2019-12-17 18:50:29 -07003892 ret = io_timeout_cancel(ctx, req->timeout.addr);
Jens Axboe11365042019-10-16 09:08:32 -06003893
Jens Axboe47f46762019-11-09 17:43:02 -07003894 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06003895 io_commit_cqring(ctx);
3896 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06003897 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003898 if (ret < 0)
3899 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003900 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06003901 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06003902}
3903
Jens Axboe3529d8c2019-12-19 18:24:38 -07003904static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07003905 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06003906{
Jens Axboead8a48a2019-11-15 08:49:11 -07003907 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06003908 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06003909
Jens Axboead8a48a2019-11-15 08:49:11 -07003910 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06003911 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07003912 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06003913 return -EINVAL;
Jens Axboe2d283902019-12-04 11:08:05 -07003914 if (sqe->off && is_timeout_link)
3915 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06003916 flags = READ_ONCE(sqe->timeout_flags);
3917 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06003918 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06003919
Jens Axboe26a61672019-12-20 09:02:01 -07003920 req->timeout.count = READ_ONCE(sqe->off);
3921
Jens Axboe3529d8c2019-12-19 18:24:38 -07003922 if (!req->io && io_alloc_async_ctx(req))
Jens Axboe26a61672019-12-20 09:02:01 -07003923 return -ENOMEM;
3924
3925 data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07003926 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07003927 req->flags |= REQ_F_TIMEOUT;
3928
3929 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06003930 return -EFAULT;
3931
Jens Axboe11365042019-10-16 09:08:32 -06003932 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07003933 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06003934 else
Jens Axboead8a48a2019-11-15 08:49:11 -07003935 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06003936
Jens Axboead8a48a2019-11-15 08:49:11 -07003937 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
3938 return 0;
3939}
3940
Jens Axboefc4df992019-12-10 14:38:45 -07003941static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07003942{
3943 unsigned count;
3944 struct io_ring_ctx *ctx = req->ctx;
3945 struct io_timeout_data *data;
3946 struct list_head *entry;
3947 unsigned span = 0;
Jens Axboead8a48a2019-11-15 08:49:11 -07003948
Jens Axboe2d283902019-12-04 11:08:05 -07003949 data = &req->io->timeout;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003950
Jens Axboe5262f562019-09-17 12:26:57 -06003951 /*
3952 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07003953 * timeout event to be satisfied. If it isn't set, then this is
3954 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06003955 */
Jens Axboe26a61672019-12-20 09:02:01 -07003956 count = req->timeout.count;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003957 if (!count) {
3958 req->flags |= REQ_F_TIMEOUT_NOSEQ;
3959 spin_lock_irq(&ctx->completion_lock);
3960 entry = ctx->timeout_list.prev;
3961 goto add;
3962 }
Jens Axboe5262f562019-09-17 12:26:57 -06003963
3964 req->sequence = ctx->cached_sq_head + count - 1;
Jens Axboe2d283902019-12-04 11:08:05 -07003965 data->seq_offset = count;
Jens Axboe5262f562019-09-17 12:26:57 -06003966
3967 /*
3968 * Insertion sort, ensuring the first entry in the list is always
3969 * the one we need first.
3970 */
Jens Axboe5262f562019-09-17 12:26:57 -06003971 spin_lock_irq(&ctx->completion_lock);
3972 list_for_each_prev(entry, &ctx->timeout_list) {
3973 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
yangerkun5da0fb12019-10-15 21:59:29 +08003974 unsigned nxt_sq_head;
3975 long long tmp, tmp_nxt;
Jens Axboe2d283902019-12-04 11:08:05 -07003976 u32 nxt_offset = nxt->io->timeout.seq_offset;
Jens Axboe5262f562019-09-17 12:26:57 -06003977
Jens Axboe93bd25b2019-11-11 23:34:31 -07003978 if (nxt->flags & REQ_F_TIMEOUT_NOSEQ)
3979 continue;
3980
yangerkun5da0fb12019-10-15 21:59:29 +08003981 /*
3982 * Since cached_sq_head + count - 1 can overflow, use type long
3983 * long to store it.
3984 */
3985 tmp = (long long)ctx->cached_sq_head + count - 1;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +03003986 nxt_sq_head = nxt->sequence - nxt_offset + 1;
3987 tmp_nxt = (long long)nxt_sq_head + nxt_offset - 1;
yangerkun5da0fb12019-10-15 21:59:29 +08003988
3989 /*
3990 * cached_sq_head may overflow, and it will never overflow twice
3991 * once there is some timeout req still be valid.
3992 */
3993 if (ctx->cached_sq_head < nxt_sq_head)
yangerkun8b07a652019-10-17 12:12:35 +08003994 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08003995
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003996 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06003997 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003998
3999 /*
4000 * Sequence of reqs after the insert one and itself should
4001 * be adjusted because each timeout req consumes a slot.
4002 */
4003 span++;
4004 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06004005 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08004006 req->sequence -= span;
Jens Axboe93bd25b2019-11-11 23:34:31 -07004007add:
Jens Axboe5262f562019-09-17 12:26:57 -06004008 list_add(&req->list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07004009 data->timer.function = io_timeout_fn;
4010 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06004011 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06004012 return 0;
4013}
4014
Jens Axboe62755e32019-10-28 21:49:21 -06004015static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06004016{
Jens Axboe62755e32019-10-28 21:49:21 -06004017 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06004018
Jens Axboe62755e32019-10-28 21:49:21 -06004019 return req->user_data == (unsigned long) data;
4020}
4021
Jens Axboee977d6d2019-11-05 12:39:45 -07004022static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06004023{
Jens Axboe62755e32019-10-28 21:49:21 -06004024 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06004025 int ret = 0;
4026
Jens Axboe62755e32019-10-28 21:49:21 -06004027 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
4028 switch (cancel_ret) {
4029 case IO_WQ_CANCEL_OK:
4030 ret = 0;
4031 break;
4032 case IO_WQ_CANCEL_RUNNING:
4033 ret = -EALREADY;
4034 break;
4035 case IO_WQ_CANCEL_NOTFOUND:
4036 ret = -ENOENT;
4037 break;
4038 }
4039
Jens Axboee977d6d2019-11-05 12:39:45 -07004040 return ret;
4041}
4042
Jens Axboe47f46762019-11-09 17:43:02 -07004043static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
4044 struct io_kiocb *req, __u64 sqe_addr,
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004045 struct io_kiocb **nxt, int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07004046{
4047 unsigned long flags;
4048 int ret;
4049
4050 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
4051 if (ret != -ENOENT) {
4052 spin_lock_irqsave(&ctx->completion_lock, flags);
4053 goto done;
4054 }
4055
4056 spin_lock_irqsave(&ctx->completion_lock, flags);
4057 ret = io_timeout_cancel(ctx, sqe_addr);
4058 if (ret != -ENOENT)
4059 goto done;
4060 ret = io_poll_cancel(ctx, sqe_addr);
4061done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004062 if (!ret)
4063 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07004064 io_cqring_fill_event(req, ret);
4065 io_commit_cqring(ctx);
4066 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4067 io_cqring_ev_posted(ctx);
4068
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004069 if (ret < 0)
4070 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07004071 io_put_req_find_next(req, nxt);
4072}
4073
Jens Axboe3529d8c2019-12-19 18:24:38 -07004074static int io_async_cancel_prep(struct io_kiocb *req,
4075 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07004076{
Jens Axboefbf23842019-12-17 18:45:56 -07004077 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07004078 return -EINVAL;
4079 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
4080 sqe->cancel_flags)
4081 return -EINVAL;
4082
Jens Axboefbf23842019-12-17 18:45:56 -07004083 req->cancel.addr = READ_ONCE(sqe->addr);
4084 return 0;
4085}
4086
4087static int io_async_cancel(struct io_kiocb *req, struct io_kiocb **nxt)
4088{
4089 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07004090
4091 io_async_find_and_cancel(ctx, req, req->cancel.addr, nxt, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06004092 return 0;
4093}
4094
Jens Axboe05f3fb32019-12-09 11:22:50 -07004095static int io_files_update_prep(struct io_kiocb *req,
4096 const struct io_uring_sqe *sqe)
4097{
4098 if (sqe->flags || sqe->ioprio || sqe->rw_flags)
4099 return -EINVAL;
4100
4101 req->files_update.offset = READ_ONCE(sqe->off);
4102 req->files_update.nr_args = READ_ONCE(sqe->len);
4103 if (!req->files_update.nr_args)
4104 return -EINVAL;
4105 req->files_update.arg = READ_ONCE(sqe->addr);
4106 return 0;
4107}
4108
4109static int io_files_update(struct io_kiocb *req, bool force_nonblock)
4110{
4111 struct io_ring_ctx *ctx = req->ctx;
4112 struct io_uring_files_update up;
4113 int ret;
4114
Jens Axboef86cd202020-01-29 13:46:44 -07004115 if (force_nonblock)
Jens Axboe05f3fb32019-12-09 11:22:50 -07004116 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004117
4118 up.offset = req->files_update.offset;
4119 up.fds = req->files_update.arg;
4120
4121 mutex_lock(&ctx->uring_lock);
4122 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
4123 mutex_unlock(&ctx->uring_lock);
4124
4125 if (ret < 0)
4126 req_set_fail_links(req);
4127 io_cqring_add_event(req, ret);
4128 io_put_req(req);
4129 return 0;
4130}
4131
Jens Axboe3529d8c2019-12-19 18:24:38 -07004132static int io_req_defer_prep(struct io_kiocb *req,
4133 const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07004134{
Jens Axboee7815732019-12-17 19:45:06 -07004135 ssize_t ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07004136
Pavel Begunkovf1d96a82020-03-13 22:29:14 +03004137 if (!sqe)
4138 return 0;
4139
Jens Axboef86cd202020-01-29 13:46:44 -07004140 if (io_op_defs[req->opcode].file_table) {
4141 ret = io_grab_files(req);
4142 if (unlikely(ret))
4143 return ret;
4144 }
4145
Jens Axboecccf0ee2020-01-27 16:34:48 -07004146 io_req_work_grab_env(req, &io_op_defs[req->opcode]);
4147
Jens Axboed625c6e2019-12-17 19:53:05 -07004148 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07004149 case IORING_OP_NOP:
4150 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004151 case IORING_OP_READV:
4152 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004153 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004154 ret = io_read_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07004155 break;
4156 case IORING_OP_WRITEV:
4157 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004158 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004159 ret = io_write_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07004160 break;
Jens Axboe0969e782019-12-17 18:40:57 -07004161 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004162 ret = io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07004163 break;
4164 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004165 ret = io_poll_remove_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07004166 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004167 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004168 ret = io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004169 break;
4170 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004171 ret = io_prep_sfr(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004172 break;
Jens Axboe03b12302019-12-02 18:50:25 -07004173 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004174 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004175 ret = io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07004176 break;
4177 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004178 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004179 ret = io_recvmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07004180 break;
Jens Axboef499a022019-12-02 16:28:46 -07004181 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004182 ret = io_connect_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07004183 break;
Jens Axboe2d283902019-12-04 11:08:05 -07004184 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004185 ret = io_timeout_prep(req, sqe, false);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004186 break;
Jens Axboeb29472e2019-12-17 18:50:29 -07004187 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004188 ret = io_timeout_remove_prep(req, sqe);
Jens Axboeb29472e2019-12-17 18:50:29 -07004189 break;
Jens Axboefbf23842019-12-17 18:45:56 -07004190 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004191 ret = io_async_cancel_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07004192 break;
Jens Axboe2d283902019-12-04 11:08:05 -07004193 case IORING_OP_LINK_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004194 ret = io_timeout_prep(req, sqe, true);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004195 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004196 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004197 ret = io_accept_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004198 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07004199 case IORING_OP_FALLOCATE:
4200 ret = io_fallocate_prep(req, sqe);
4201 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004202 case IORING_OP_OPENAT:
4203 ret = io_openat_prep(req, sqe);
4204 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07004205 case IORING_OP_CLOSE:
4206 ret = io_close_prep(req, sqe);
4207 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004208 case IORING_OP_FILES_UPDATE:
4209 ret = io_files_update_prep(req, sqe);
4210 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004211 case IORING_OP_STATX:
4212 ret = io_statx_prep(req, sqe);
4213 break;
Jens Axboe4840e412019-12-25 22:03:45 -07004214 case IORING_OP_FADVISE:
4215 ret = io_fadvise_prep(req, sqe);
4216 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07004217 case IORING_OP_MADVISE:
4218 ret = io_madvise_prep(req, sqe);
4219 break;
Jens Axboecebdb982020-01-08 17:59:24 -07004220 case IORING_OP_OPENAT2:
4221 ret = io_openat2_prep(req, sqe);
4222 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004223 case IORING_OP_EPOLL_CTL:
4224 ret = io_epoll_ctl_prep(req, sqe);
4225 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004226 default:
Jens Axboee7815732019-12-17 19:45:06 -07004227 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
4228 req->opcode);
4229 ret = -EINVAL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004230 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004231 }
4232
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004233 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07004234}
4235
Jens Axboe3529d8c2019-12-19 18:24:38 -07004236static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboede0617e2019-04-06 21:51:27 -06004237{
Jackie Liua197f662019-11-08 08:09:12 -07004238 struct io_ring_ctx *ctx = req->ctx;
Jens Axboef67676d2019-12-02 11:03:47 -07004239 int ret;
Jens Axboede0617e2019-04-06 21:51:27 -06004240
Bob Liu9d858b22019-11-13 18:06:25 +08004241 /* Still need defer if there is pending req in defer list. */
4242 if (!req_need_defer(req) && list_empty(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06004243 return 0;
4244
Jens Axboe3529d8c2019-12-19 18:24:38 -07004245 if (!req->io && io_alloc_async_ctx(req))
Jens Axboede0617e2019-04-06 21:51:27 -06004246 return -EAGAIN;
4247
Jens Axboe3529d8c2019-12-19 18:24:38 -07004248 ret = io_req_defer_prep(req, sqe);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004249 if (ret < 0)
Jens Axboe2d283902019-12-04 11:08:05 -07004250 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07004251
Jens Axboede0617e2019-04-06 21:51:27 -06004252 spin_lock_irq(&ctx->completion_lock);
Bob Liu9d858b22019-11-13 18:06:25 +08004253 if (!req_need_defer(req) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06004254 spin_unlock_irq(&ctx->completion_lock);
Jens Axboede0617e2019-04-06 21:51:27 -06004255 return 0;
4256 }
4257
Jens Axboe915967f2019-11-21 09:01:20 -07004258 trace_io_uring_defer(ctx, req, req->user_data);
Jens Axboede0617e2019-04-06 21:51:27 -06004259 list_add_tail(&req->list, &ctx->defer_list);
4260 spin_unlock_irq(&ctx->completion_lock);
4261 return -EIOCBQUEUED;
4262}
4263
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004264static void io_cleanup_req(struct io_kiocb *req)
4265{
4266 struct io_async_ctx *io = req->io;
4267
4268 switch (req->opcode) {
4269 case IORING_OP_READV:
4270 case IORING_OP_READ_FIXED:
4271 case IORING_OP_READ:
4272 case IORING_OP_WRITEV:
4273 case IORING_OP_WRITE_FIXED:
4274 case IORING_OP_WRITE:
4275 if (io->rw.iov != io->rw.fast_iov)
4276 kfree(io->rw.iov);
4277 break;
4278 case IORING_OP_SENDMSG:
4279 case IORING_OP_RECVMSG:
4280 if (io->msg.iov != io->msg.fast_iov)
4281 kfree(io->msg.iov);
4282 break;
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03004283 case IORING_OP_OPENAT:
4284 case IORING_OP_OPENAT2:
4285 case IORING_OP_STATX:
4286 putname(req->open.filename);
4287 break;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004288 }
4289
4290 req->flags &= ~REQ_F_NEED_CLEANUP;
4291}
4292
Jens Axboe3529d8c2019-12-19 18:24:38 -07004293static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
4294 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004295{
Jackie Liua197f662019-11-08 08:09:12 -07004296 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07004297 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004298
Jens Axboed625c6e2019-12-17 19:53:05 -07004299 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07004300 case IORING_OP_NOP:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004301 ret = io_nop(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004302 break;
4303 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004304 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004305 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004306 if (sqe) {
4307 ret = io_read_prep(req, sqe, force_nonblock);
4308 if (ret < 0)
4309 break;
4310 }
Pavel Begunkov267bc902019-11-07 01:41:08 +03004311 ret = io_read(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004312 break;
4313 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07004314 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004315 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004316 if (sqe) {
4317 ret = io_write_prep(req, sqe, force_nonblock);
4318 if (ret < 0)
4319 break;
4320 }
Pavel Begunkov267bc902019-11-07 01:41:08 +03004321 ret = io_write(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004322 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004323 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004324 if (sqe) {
4325 ret = io_prep_fsync(req, sqe);
4326 if (ret < 0)
4327 break;
4328 }
Jens Axboefc4df992019-12-10 14:38:45 -07004329 ret = io_fsync(req, nxt, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004330 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004331 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004332 if (sqe) {
4333 ret = io_poll_add_prep(req, sqe);
4334 if (ret)
4335 break;
4336 }
Jens Axboefc4df992019-12-10 14:38:45 -07004337 ret = io_poll_add(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004338 break;
4339 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004340 if (sqe) {
4341 ret = io_poll_remove_prep(req, sqe);
4342 if (ret < 0)
4343 break;
4344 }
Jens Axboefc4df992019-12-10 14:38:45 -07004345 ret = io_poll_remove(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004346 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004347 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004348 if (sqe) {
4349 ret = io_prep_sfr(req, sqe);
4350 if (ret < 0)
4351 break;
4352 }
Jens Axboefc4df992019-12-10 14:38:45 -07004353 ret = io_sync_file_range(req, nxt, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004354 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004355 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004356 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004357 if (sqe) {
4358 ret = io_sendmsg_prep(req, sqe);
4359 if (ret < 0)
4360 break;
4361 }
Jens Axboefddafac2020-01-04 20:19:44 -07004362 if (req->opcode == IORING_OP_SENDMSG)
4363 ret = io_sendmsg(req, nxt, force_nonblock);
4364 else
4365 ret = io_send(req, nxt, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004366 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06004367 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004368 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004369 if (sqe) {
4370 ret = io_recvmsg_prep(req, sqe);
4371 if (ret)
4372 break;
4373 }
Jens Axboefddafac2020-01-04 20:19:44 -07004374 if (req->opcode == IORING_OP_RECVMSG)
4375 ret = io_recvmsg(req, nxt, force_nonblock);
4376 else
4377 ret = io_recv(req, nxt, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06004378 break;
Jens Axboe5262f562019-09-17 12:26:57 -06004379 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004380 if (sqe) {
4381 ret = io_timeout_prep(req, sqe, false);
4382 if (ret)
4383 break;
4384 }
Jens Axboefc4df992019-12-10 14:38:45 -07004385 ret = io_timeout(req);
Jens Axboe5262f562019-09-17 12:26:57 -06004386 break;
Jens Axboe11365042019-10-16 09:08:32 -06004387 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004388 if (sqe) {
4389 ret = io_timeout_remove_prep(req, sqe);
4390 if (ret)
4391 break;
4392 }
Jens Axboefc4df992019-12-10 14:38:45 -07004393 ret = io_timeout_remove(req);
Jens Axboe11365042019-10-16 09:08:32 -06004394 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06004395 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004396 if (sqe) {
4397 ret = io_accept_prep(req, sqe);
4398 if (ret)
4399 break;
4400 }
Jens Axboefc4df992019-12-10 14:38:45 -07004401 ret = io_accept(req, nxt, force_nonblock);
Jens Axboe17f2fe32019-10-17 14:42:58 -06004402 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004403 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004404 if (sqe) {
4405 ret = io_connect_prep(req, sqe);
4406 if (ret)
4407 break;
4408 }
Jens Axboefc4df992019-12-10 14:38:45 -07004409 ret = io_connect(req, nxt, force_nonblock);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004410 break;
Jens Axboe62755e32019-10-28 21:49:21 -06004411 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004412 if (sqe) {
4413 ret = io_async_cancel_prep(req, sqe);
4414 if (ret)
4415 break;
4416 }
Jens Axboefc4df992019-12-10 14:38:45 -07004417 ret = io_async_cancel(req, nxt);
Jens Axboe62755e32019-10-28 21:49:21 -06004418 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07004419 case IORING_OP_FALLOCATE:
4420 if (sqe) {
4421 ret = io_fallocate_prep(req, sqe);
4422 if (ret)
4423 break;
4424 }
4425 ret = io_fallocate(req, nxt, force_nonblock);
4426 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004427 case IORING_OP_OPENAT:
4428 if (sqe) {
4429 ret = io_openat_prep(req, sqe);
4430 if (ret)
4431 break;
4432 }
4433 ret = io_openat(req, nxt, force_nonblock);
4434 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07004435 case IORING_OP_CLOSE:
4436 if (sqe) {
4437 ret = io_close_prep(req, sqe);
4438 if (ret)
4439 break;
4440 }
4441 ret = io_close(req, nxt, force_nonblock);
4442 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004443 case IORING_OP_FILES_UPDATE:
4444 if (sqe) {
4445 ret = io_files_update_prep(req, sqe);
4446 if (ret)
4447 break;
4448 }
4449 ret = io_files_update(req, force_nonblock);
4450 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004451 case IORING_OP_STATX:
4452 if (sqe) {
4453 ret = io_statx_prep(req, sqe);
4454 if (ret)
4455 break;
4456 }
4457 ret = io_statx(req, nxt, force_nonblock);
4458 break;
Jens Axboe4840e412019-12-25 22:03:45 -07004459 case IORING_OP_FADVISE:
4460 if (sqe) {
4461 ret = io_fadvise_prep(req, sqe);
4462 if (ret)
4463 break;
4464 }
4465 ret = io_fadvise(req, nxt, force_nonblock);
4466 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07004467 case IORING_OP_MADVISE:
4468 if (sqe) {
4469 ret = io_madvise_prep(req, sqe);
4470 if (ret)
4471 break;
4472 }
4473 ret = io_madvise(req, nxt, force_nonblock);
4474 break;
Jens Axboecebdb982020-01-08 17:59:24 -07004475 case IORING_OP_OPENAT2:
4476 if (sqe) {
4477 ret = io_openat2_prep(req, sqe);
4478 if (ret)
4479 break;
4480 }
4481 ret = io_openat2(req, nxt, force_nonblock);
4482 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004483 case IORING_OP_EPOLL_CTL:
4484 if (sqe) {
4485 ret = io_epoll_ctl_prep(req, sqe);
4486 if (ret)
4487 break;
4488 }
4489 ret = io_epoll_ctl(req, nxt, force_nonblock);
4490 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004491 default:
4492 ret = -EINVAL;
4493 break;
4494 }
4495
Jens Axboedef596e2019-01-09 08:59:42 -07004496 if (ret)
4497 return ret;
4498
4499 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe11ba8202020-01-15 21:51:17 -07004500 const bool in_async = io_wq_current_is_worker();
4501
Jens Axboe9e645e112019-05-10 16:07:28 -06004502 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07004503 return -EAGAIN;
4504
Jens Axboe11ba8202020-01-15 21:51:17 -07004505 /* workqueue context doesn't hold uring_lock, grab it now */
4506 if (in_async)
4507 mutex_lock(&ctx->uring_lock);
4508
Jens Axboedef596e2019-01-09 08:59:42 -07004509 io_iopoll_req_issued(req);
Jens Axboe11ba8202020-01-15 21:51:17 -07004510
4511 if (in_async)
4512 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07004513 }
4514
4515 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004516}
4517
Jens Axboe561fb042019-10-24 07:25:42 -06004518static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboe31b51512019-01-18 22:56:34 -07004519{
Jens Axboe561fb042019-10-24 07:25:42 -06004520 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004521 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe561fb042019-10-24 07:25:42 -06004522 struct io_kiocb *nxt = NULL;
4523 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004524
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07004525 /* if NO_CANCEL is set, we must still run the work */
4526 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
4527 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06004528 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07004529 }
Jens Axboe31b51512019-01-18 22:56:34 -07004530
Jens Axboe561fb042019-10-24 07:25:42 -06004531 if (!ret) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004532 req->in_async = true;
Jens Axboe561fb042019-10-24 07:25:42 -06004533 do {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004534 ret = io_issue_sqe(req, NULL, &nxt, false);
Jens Axboe561fb042019-10-24 07:25:42 -06004535 /*
4536 * We can get EAGAIN for polled IO even though we're
4537 * forcing a sync submission from here, since we can't
4538 * wait for request slots on the block side.
4539 */
4540 if (ret != -EAGAIN)
4541 break;
4542 cond_resched();
4543 } while (1);
4544 }
Jens Axboe31b51512019-01-18 22:56:34 -07004545
Jens Axboe561fb042019-10-24 07:25:42 -06004546 /* drop submission reference */
Jackie Liuec9c02a2019-11-08 23:50:36 +08004547 io_put_req(req);
Jens Axboe817869d2019-04-30 14:44:05 -06004548
Jens Axboe561fb042019-10-24 07:25:42 -06004549 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004550 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07004551 io_cqring_add_event(req, ret);
Jens Axboe817869d2019-04-30 14:44:05 -06004552 io_put_req(req);
Jens Axboeedafcce2019-01-09 09:16:05 -07004553 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004554
Jens Axboe561fb042019-10-24 07:25:42 -06004555 /* if a dependent link is ready, pass it back */
Jens Axboe78912932020-01-14 22:09:06 -07004556 if (!ret && nxt)
4557 io_wq_assign_next(workptr, nxt);
Jens Axboe31b51512019-01-18 22:56:34 -07004558}
Jens Axboe2b188cc2019-01-07 10:46:33 -07004559
Jens Axboe15b71ab2019-12-11 11:20:36 -07004560static int io_req_needs_file(struct io_kiocb *req, int fd)
Jens Axboe9e3aa612019-12-11 15:55:43 -07004561{
Jens Axboed3656342019-12-18 09:50:26 -07004562 if (!io_op_defs[req->opcode].needs_file)
Jens Axboe9e3aa612019-12-11 15:55:43 -07004563 return 0;
Jens Axboe0b5faf62020-02-06 21:42:51 -07004564 if ((fd == -1 || fd == AT_FDCWD) && io_op_defs[req->opcode].fd_non_neg)
Jens Axboed3656342019-12-18 09:50:26 -07004565 return 0;
4566 return 1;
Jens Axboe09bb8392019-03-13 12:39:28 -06004567}
4568
Jens Axboe65e19f52019-10-26 07:20:21 -06004569static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
4570 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06004571{
Jens Axboe65e19f52019-10-26 07:20:21 -06004572 struct fixed_file_table *table;
4573
Jens Axboe05f3fb32019-12-09 11:22:50 -07004574 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
4575 return table->files[index & IORING_FILE_TABLE_MASK];;
Jens Axboe65e19f52019-10-26 07:20:21 -06004576}
4577
Jens Axboe3529d8c2019-12-19 18:24:38 -07004578static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
4579 const struct io_uring_sqe *sqe)
Jens Axboe09bb8392019-03-13 12:39:28 -06004580{
Jackie Liua197f662019-11-08 08:09:12 -07004581 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe09bb8392019-03-13 12:39:28 -06004582 unsigned flags;
Jens Axboed3656342019-12-18 09:50:26 -07004583 int fd;
Jens Axboe09bb8392019-03-13 12:39:28 -06004584
Jens Axboe3529d8c2019-12-19 18:24:38 -07004585 flags = READ_ONCE(sqe->flags);
4586 fd = READ_ONCE(sqe->fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06004587
Jens Axboed3656342019-12-18 09:50:26 -07004588 if (!io_req_needs_file(req, fd))
4589 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06004590
4591 if (flags & IOSQE_FIXED_FILE) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07004592 if (unlikely(!ctx->file_data ||
Jens Axboe09bb8392019-03-13 12:39:28 -06004593 (unsigned) fd >= ctx->nr_user_files))
4594 return -EBADF;
Jens Axboeb7620122019-10-26 07:22:55 -06004595 fd = array_index_nospec(fd, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06004596 req->file = io_file_from_index(ctx, fd);
4597 if (!req->file)
Jens Axboe08a45172019-10-03 08:11:03 -06004598 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06004599 req->flags |= REQ_F_FIXED_FILE;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004600 percpu_ref_get(&ctx->file_data->refs);
Jens Axboe09bb8392019-03-13 12:39:28 -06004601 } else {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004602 if (req->needs_fixed_file)
Jens Axboe09bb8392019-03-13 12:39:28 -06004603 return -EBADF;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004604 trace_io_uring_file_get(ctx, fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06004605 req->file = io_file_get(state, fd);
4606 if (unlikely(!req->file))
4607 return -EBADF;
4608 }
4609
4610 return 0;
4611}
4612
Jackie Liua197f662019-11-08 08:09:12 -07004613static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004614{
Jens Axboefcb323c2019-10-24 12:39:47 -06004615 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07004616 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06004617
Jens Axboef86cd202020-01-29 13:46:44 -07004618 if (req->work.files)
4619 return 0;
Pavel Begunkovb14cca02020-01-17 04:45:59 +03004620 if (!ctx->ring_file)
Jens Axboeb5dba592019-12-11 14:02:38 -07004621 return -EBADF;
4622
Jens Axboefcb323c2019-10-24 12:39:47 -06004623 rcu_read_lock();
4624 spin_lock_irq(&ctx->inflight_lock);
4625 /*
4626 * We use the f_ops->flush() handler to ensure that we can flush
4627 * out work accessing these files if the fd is closed. Check if
4628 * the fd has changed since we started down this path, and disallow
4629 * this operation if it has.
4630 */
Pavel Begunkovb14cca02020-01-17 04:45:59 +03004631 if (fcheck(ctx->ring_fd) == ctx->ring_file) {
Jens Axboefcb323c2019-10-24 12:39:47 -06004632 list_add(&req->inflight_entry, &ctx->inflight_list);
4633 req->flags |= REQ_F_INFLIGHT;
4634 req->work.files = current->files;
4635 ret = 0;
4636 }
4637 spin_unlock_irq(&ctx->inflight_lock);
4638 rcu_read_unlock();
4639
4640 return ret;
4641}
4642
Jens Axboe2665abf2019-11-05 12:40:47 -07004643static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
4644{
Jens Axboead8a48a2019-11-15 08:49:11 -07004645 struct io_timeout_data *data = container_of(timer,
4646 struct io_timeout_data, timer);
4647 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07004648 struct io_ring_ctx *ctx = req->ctx;
4649 struct io_kiocb *prev = NULL;
4650 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07004651
4652 spin_lock_irqsave(&ctx->completion_lock, flags);
4653
4654 /*
4655 * We don't expect the list to be empty, that will only happen if we
4656 * race with the completion of the linked work.
4657 */
Pavel Begunkov44932332019-12-05 16:16:35 +03004658 if (!list_empty(&req->link_list)) {
4659 prev = list_entry(req->link_list.prev, struct io_kiocb,
4660 link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07004661 if (refcount_inc_not_zero(&prev->refs)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03004662 list_del_init(&req->link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07004663 prev->flags &= ~REQ_F_LINK_TIMEOUT;
4664 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07004665 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07004666 }
4667
4668 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4669
4670 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004671 req_set_fail_links(prev);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004672 io_async_find_and_cancel(ctx, req, prev->user_data, NULL,
4673 -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07004674 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07004675 } else {
4676 io_cqring_add_event(req, -ETIME);
4677 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07004678 }
Jens Axboe2665abf2019-11-05 12:40:47 -07004679 return HRTIMER_NORESTART;
4680}
4681
Jens Axboead8a48a2019-11-15 08:49:11 -07004682static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07004683{
Jens Axboe76a46e02019-11-10 23:34:16 -07004684 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07004685
Jens Axboe76a46e02019-11-10 23:34:16 -07004686 /*
4687 * If the list is now empty, then our linked request finished before
4688 * we got a chance to setup the timer
4689 */
4690 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov44932332019-12-05 16:16:35 +03004691 if (!list_empty(&req->link_list)) {
Jens Axboe2d283902019-12-04 11:08:05 -07004692 struct io_timeout_data *data = &req->io->timeout;
Jens Axboe94ae5e72019-11-14 19:39:52 -07004693
Jens Axboead8a48a2019-11-15 08:49:11 -07004694 data->timer.function = io_link_timeout_fn;
4695 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
4696 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07004697 }
Jens Axboe76a46e02019-11-10 23:34:16 -07004698 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07004699
Jens Axboe2665abf2019-11-05 12:40:47 -07004700 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07004701 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07004702}
4703
Jens Axboead8a48a2019-11-15 08:49:11 -07004704static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07004705{
4706 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004707
Jens Axboe2665abf2019-11-05 12:40:47 -07004708 if (!(req->flags & REQ_F_LINK))
4709 return NULL;
4710
Pavel Begunkov44932332019-12-05 16:16:35 +03004711 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
4712 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07004713 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07004714 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07004715
Jens Axboe76a46e02019-11-10 23:34:16 -07004716 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07004717 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07004718}
4719
Jens Axboe3529d8c2019-12-19 18:24:38 -07004720static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004721{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004722 struct io_kiocb *linked_timeout;
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004723 struct io_kiocb *nxt = NULL;
Jens Axboe193155c2020-02-22 23:22:19 -07004724 const struct cred *old_creds = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004725 int ret;
4726
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004727again:
4728 linked_timeout = io_prep_linked_timeout(req);
4729
Jens Axboe193155c2020-02-22 23:22:19 -07004730 if (req->work.creds && req->work.creds != current_cred()) {
4731 if (old_creds)
4732 revert_creds(old_creds);
4733 if (old_creds == req->work.creds)
4734 old_creds = NULL; /* restored original creds */
4735 else
4736 old_creds = override_creds(req->work.creds);
4737 }
4738
Jens Axboe3529d8c2019-12-19 18:24:38 -07004739 ret = io_issue_sqe(req, sqe, &nxt, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06004740
4741 /*
4742 * We async punt it if the file wasn't marked NOWAIT, or if the file
4743 * doesn't support non-blocking read/write attempts
4744 */
4745 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
4746 (req->flags & REQ_F_MUST_PUNT))) {
Pavel Begunkov86a761f2020-01-22 23:09:36 +03004747punt:
Jens Axboef86cd202020-01-29 13:46:44 -07004748 if (io_op_defs[req->opcode].file_table) {
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03004749 ret = io_grab_files(req);
4750 if (ret)
4751 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004752 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03004753
4754 /*
4755 * Queued up for async execution, worker will release
4756 * submit reference when the iocb is actually submitted.
4757 */
4758 io_queue_async_work(req);
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004759 goto done_req;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004760 }
Jens Axboee65ef562019-03-12 10:16:44 -06004761
Jens Axboefcb323c2019-10-24 12:39:47 -06004762err:
Jens Axboee65ef562019-03-12 10:16:44 -06004763 /* drop submission reference */
Jens Axboe2a44f462020-02-25 13:25:41 -07004764 io_put_req_find_next(req, &nxt);
Jens Axboee65ef562019-03-12 10:16:44 -06004765
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004766 if (linked_timeout) {
Jens Axboe76a46e02019-11-10 23:34:16 -07004767 if (!ret)
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004768 io_queue_linked_timeout(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07004769 else
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004770 io_put_req(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07004771 }
4772
Jens Axboee65ef562019-03-12 10:16:44 -06004773 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06004774 if (ret) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07004775 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004776 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06004777 io_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06004778 }
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004779done_req:
4780 if (nxt) {
4781 req = nxt;
4782 nxt = NULL;
Pavel Begunkov86a761f2020-01-22 23:09:36 +03004783
4784 if (req->flags & REQ_F_FORCE_ASYNC)
4785 goto punt;
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004786 goto again;
4787 }
Jens Axboe193155c2020-02-22 23:22:19 -07004788 if (old_creds)
4789 revert_creds(old_creds);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004790}
4791
Jens Axboe3529d8c2019-12-19 18:24:38 -07004792static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jackie Liu4fe2c962019-09-09 20:50:40 +08004793{
4794 int ret;
4795
Jens Axboe3529d8c2019-12-19 18:24:38 -07004796 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004797 if (ret) {
4798 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03004799fail_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004800 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004801 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07004802 io_double_put_req(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004803 }
Pavel Begunkov25508782019-12-30 21:24:47 +03004804 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Pavel Begunkov11185912020-01-22 23:09:35 +03004805 ret = io_req_defer_prep(req, sqe);
4806 if (unlikely(ret < 0))
4807 goto fail_req;
Jens Axboece35a472019-12-17 08:04:44 -07004808 /*
4809 * Never try inline submit of IOSQE_ASYNC is set, go straight
4810 * to async execution.
4811 */
4812 req->work.flags |= IO_WQ_WORK_CONCURRENT;
4813 io_queue_async_work(req);
4814 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004815 __io_queue_sqe(req, sqe);
Jens Axboece35a472019-12-17 08:04:44 -07004816 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08004817}
4818
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004819static inline void io_queue_link_head(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08004820{
Jens Axboe94ae5e72019-11-14 19:39:52 -07004821 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004822 io_cqring_add_event(req, -ECANCELED);
4823 io_double_put_req(req);
4824 } else
Jens Axboe3529d8c2019-12-19 18:24:38 -07004825 io_queue_sqe(req, NULL);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004826}
4827
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004828#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
Jens Axboece35a472019-12-17 08:04:44 -07004829 IOSQE_IO_HARDLINK | IOSQE_ASYNC)
Jens Axboe9e645e112019-05-10 16:07:28 -06004830
Jens Axboe3529d8c2019-12-19 18:24:38 -07004831static bool io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
4832 struct io_submit_state *state, struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06004833{
Jackie Liua197f662019-11-08 08:09:12 -07004834 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004835 unsigned int sqe_flags;
Jens Axboe75c6a032020-01-28 10:15:23 -07004836 int ret, id;
Jens Axboe9e645e112019-05-10 16:07:28 -06004837
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004838 sqe_flags = READ_ONCE(sqe->flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06004839
4840 /* enforce forwards compatibility on users */
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004841 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06004842 ret = -EINVAL;
Pavel Begunkov196be952019-11-07 01:41:06 +03004843 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06004844 }
4845
Jens Axboe75c6a032020-01-28 10:15:23 -07004846 id = READ_ONCE(sqe->personality);
4847 if (id) {
Jens Axboe193155c2020-02-22 23:22:19 -07004848 req->work.creds = idr_find(&ctx->personality_idr, id);
4849 if (unlikely(!req->work.creds)) {
Jens Axboe75c6a032020-01-28 10:15:23 -07004850 ret = -EINVAL;
4851 goto err_req;
4852 }
Jens Axboe193155c2020-02-22 23:22:19 -07004853 get_cred(req->work.creds);
Jens Axboe75c6a032020-01-28 10:15:23 -07004854 }
4855
Pavel Begunkov6b47ee62020-01-18 20:22:41 +03004856 /* same numerical values with corresponding REQ_F_*, safe to copy */
4857 req->flags |= sqe_flags & (IOSQE_IO_DRAIN|IOSQE_IO_HARDLINK|
4858 IOSQE_ASYNC);
Jens Axboe9e645e112019-05-10 16:07:28 -06004859
Jens Axboe3529d8c2019-12-19 18:24:38 -07004860 ret = io_req_set_file(state, req, sqe);
Jens Axboe9e645e112019-05-10 16:07:28 -06004861 if (unlikely(ret)) {
4862err_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004863 io_cqring_add_event(req, ret);
4864 io_double_put_req(req);
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004865 return false;
Jens Axboe9e645e112019-05-10 16:07:28 -06004866 }
4867
Jens Axboe9e645e112019-05-10 16:07:28 -06004868 /*
4869 * If we already have a head request, queue this one for async
4870 * submittal once the head completes. If we don't have a head but
4871 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
4872 * submitted sync once the chain is complete. If none of those
4873 * conditions are true (normal request), then just queue it.
4874 */
4875 if (*link) {
Pavel Begunkov9d763772019-12-17 02:22:07 +03004876 struct io_kiocb *head = *link;
Jens Axboe9e645e112019-05-10 16:07:28 -06004877
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03004878 /*
4879 * Taking sequential execution of a link, draining both sides
4880 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
4881 * requests in the link. So, it drains the head and the
4882 * next after the link request. The last one is done via
4883 * drain_next flag to persist the effect across calls.
4884 */
Pavel Begunkov711be032020-01-17 03:57:59 +03004885 if (sqe_flags & IOSQE_IO_DRAIN) {
4886 head->flags |= REQ_F_IO_DRAIN;
4887 ctx->drain_next = 1;
4888 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004889 if (io_alloc_async_ctx(req)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06004890 ret = -EAGAIN;
4891 goto err_req;
4892 }
4893
Jens Axboe3529d8c2019-12-19 18:24:38 -07004894 ret = io_req_defer_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07004895 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004896 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03004897 head->flags |= REQ_F_FAIL_LINK;
Jens Axboef67676d2019-12-02 11:03:47 -07004898 goto err_req;
Jens Axboe2d283902019-12-04 11:08:05 -07004899 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03004900 trace_io_uring_link(ctx, req, head);
4901 list_add_tail(&req->link_list, &head->link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06004902
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004903 /* last request of a link, enqueue the link */
4904 if (!(sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK))) {
4905 io_queue_link_head(head);
4906 *link = NULL;
4907 }
Jens Axboe9e645e112019-05-10 16:07:28 -06004908 } else {
Pavel Begunkov711be032020-01-17 03:57:59 +03004909 if (unlikely(ctx->drain_next)) {
4910 req->flags |= REQ_F_IO_DRAIN;
4911 req->ctx->drain_next = 0;
4912 }
4913 if (sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK)) {
4914 req->flags |= REQ_F_LINK;
Pavel Begunkov711be032020-01-17 03:57:59 +03004915 INIT_LIST_HEAD(&req->link_list);
Pavel Begunkovf1d96a82020-03-13 22:29:14 +03004916
4917 if (io_alloc_async_ctx(req)) {
4918 ret = -EAGAIN;
4919 goto err_req;
4920 }
Pavel Begunkov711be032020-01-17 03:57:59 +03004921 ret = io_req_defer_prep(req, sqe);
4922 if (ret)
4923 req->flags |= REQ_F_FAIL_LINK;
4924 *link = req;
4925 } else {
4926 io_queue_sqe(req, sqe);
4927 }
Jens Axboe9e645e112019-05-10 16:07:28 -06004928 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004929
4930 return true;
Jens Axboe9e645e112019-05-10 16:07:28 -06004931}
4932
Jens Axboe9a56a232019-01-09 09:06:50 -07004933/*
4934 * Batched submission is done, ensure local IO is flushed out.
4935 */
4936static void io_submit_state_end(struct io_submit_state *state)
4937{
4938 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06004939 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07004940 if (state->free_reqs)
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03004941 kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs);
Jens Axboe9a56a232019-01-09 09:06:50 -07004942}
4943
4944/*
4945 * Start submission side cache.
4946 */
4947static void io_submit_state_start(struct io_submit_state *state,
Jackie Liu22efde52019-12-02 17:14:52 +08004948 unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07004949{
4950 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07004951 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07004952 state->file = NULL;
4953 state->ios_left = max_ios;
4954}
4955
Jens Axboe2b188cc2019-01-07 10:46:33 -07004956static void io_commit_sqring(struct io_ring_ctx *ctx)
4957{
Hristo Venev75b28af2019-08-26 17:23:46 +00004958 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004959
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03004960 /*
4961 * Ensure any loads from the SQEs are done at this point,
4962 * since once we write the new head, the application could
4963 * write new data to them.
4964 */
4965 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004966}
4967
4968/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07004969 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07004970 * that is mapped by userspace. This means that care needs to be taken to
4971 * ensure that reads are stable, as we cannot rely on userspace always
4972 * being a good citizen. If members of the sqe are validated and then later
4973 * used, it's important that those reads are done through READ_ONCE() to
4974 * prevent a re-load down the line.
4975 */
Jens Axboe3529d8c2019-12-19 18:24:38 -07004976static bool io_get_sqring(struct io_ring_ctx *ctx, struct io_kiocb *req,
4977 const struct io_uring_sqe **sqe_ptr)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004978{
Hristo Venev75b28af2019-08-26 17:23:46 +00004979 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004980 unsigned head;
4981
4982 /*
4983 * The cached sq head (or cq tail) serves two purposes:
4984 *
4985 * 1) allows us to batch the cost of updating the user visible
4986 * head updates.
4987 * 2) allows the kernel side to track the head on its own, even
4988 * though the application is the one updating it.
4989 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03004990 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03004991 if (likely(head < ctx->sq_entries)) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004992 /*
4993 * All io need record the previous position, if LINK vs DARIN,
4994 * it can be used to mark the position of the first IO in the
4995 * link list.
4996 */
4997 req->sequence = ctx->cached_sq_head;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004998 *sqe_ptr = &ctx->sq_sqes[head];
4999 req->opcode = READ_ONCE((*sqe_ptr)->opcode);
5000 req->user_data = READ_ONCE((*sqe_ptr)->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005001 ctx->cached_sq_head++;
5002 return true;
5003 }
5004
5005 /* drop invalid entries */
5006 ctx->cached_sq_head++;
Jens Axboe498ccd92019-10-25 10:04:25 -06005007 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03005008 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005009 return false;
5010}
5011
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005012static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkovae9428c2019-11-06 00:22:14 +03005013 struct file *ring_file, int ring_fd,
5014 struct mm_struct **mm, bool async)
Jens Axboe6c271ce2019-01-10 11:22:30 -07005015{
5016 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06005017 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06005018 int i, submitted = 0;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03005019 bool mm_fault = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005020
Jens Axboec4a2ed72019-11-21 21:01:26 -07005021 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07005022 if (test_bit(0, &ctx->sq_check_overflow)) {
5023 if (!list_empty(&ctx->cq_overflow_list) &&
5024 !io_cqring_overflow_flush(ctx, false))
5025 return -EBUSY;
5026 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07005027
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03005028 /* make sure SQ entry isn't read before tail */
5029 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03005030
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03005031 if (!percpu_ref_tryget_many(&ctx->refs, nr))
5032 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005033
5034 if (nr > IO_PLUG_THRESHOLD) {
Jackie Liu22efde52019-12-02 17:14:52 +08005035 io_submit_state_start(&state, nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005036 statep = &state;
5037 }
5038
Pavel Begunkovb14cca02020-01-17 04:45:59 +03005039 ctx->ring_fd = ring_fd;
5040 ctx->ring_file = ring_file;
5041
Jens Axboe6c271ce2019-01-10 11:22:30 -07005042 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07005043 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03005044 struct io_kiocb *req;
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005045 int err;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005046
Pavel Begunkov196be952019-11-07 01:41:06 +03005047 req = io_get_req(ctx, statep);
5048 if (unlikely(!req)) {
5049 if (!submitted)
5050 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005051 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06005052 }
Jens Axboe3529d8c2019-12-19 18:24:38 -07005053 if (!io_get_sqring(ctx, req, &sqe)) {
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03005054 __io_req_do_free(req);
Pavel Begunkov196be952019-11-07 01:41:06 +03005055 break;
5056 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005057
Jens Axboed3656342019-12-18 09:50:26 -07005058 /* will complete beyond this point, count as submitted */
5059 submitted++;
5060
5061 if (unlikely(req->opcode >= IORING_OP_LAST)) {
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005062 err = -EINVAL;
5063fail_req:
5064 io_cqring_add_event(req, err);
Jens Axboed3656342019-12-18 09:50:26 -07005065 io_double_put_req(req);
5066 break;
5067 }
5068
5069 if (io_op_defs[req->opcode].needs_mm && !*mm) {
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03005070 mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm);
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005071 if (unlikely(mm_fault)) {
5072 err = -EFAULT;
5073 goto fail_req;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03005074 }
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005075 use_mm(ctx->sqo_mm);
5076 *mm = ctx->sqo_mm;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03005077 }
5078
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03005079 req->in_async = async;
5080 req->needs_fixed_file = async;
Jens Axboe354420f2020-01-08 18:55:15 -07005081 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
5082 true, async);
Jens Axboe3529d8c2019-12-19 18:24:38 -07005083 if (!io_submit_sqe(req, sqe, statep, &link))
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03005084 break;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005085 }
5086
Pavel Begunkov9466f432020-01-25 22:34:01 +03005087 if (unlikely(submitted != nr)) {
5088 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
5089
5090 percpu_ref_put_many(&ctx->refs, nr - ref_used);
5091 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005092 if (link)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03005093 io_queue_link_head(link);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005094 if (statep)
5095 io_submit_state_end(&state);
5096
Pavel Begunkovae9428c2019-11-06 00:22:14 +03005097 /* Commit SQ ring head once we've consumed and submitted all SQEs */
5098 io_commit_sqring(ctx);
5099
Jens Axboe6c271ce2019-01-10 11:22:30 -07005100 return submitted;
5101}
5102
5103static int io_sq_thread(void *data)
5104{
Jens Axboe6c271ce2019-01-10 11:22:30 -07005105 struct io_ring_ctx *ctx = data;
5106 struct mm_struct *cur_mm = NULL;
Jens Axboe181e4482019-11-25 08:52:30 -07005107 const struct cred *old_cred;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005108 mm_segment_t old_fs;
5109 DEFINE_WAIT(wait);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005110 unsigned long timeout;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005111 int ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005112
Jens Axboe206aefd2019-11-07 18:27:42 -07005113 complete(&ctx->completions[1]);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08005114
Jens Axboe6c271ce2019-01-10 11:22:30 -07005115 old_fs = get_fs();
5116 set_fs(USER_DS);
Jens Axboe181e4482019-11-25 08:52:30 -07005117 old_cred = override_creds(ctx->creds);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005118
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005119 timeout = jiffies + ctx->sq_thread_idle;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005120 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005121 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005122
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005123 if (!list_empty(&ctx->poll_list)) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07005124 unsigned nr_events = 0;
5125
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005126 mutex_lock(&ctx->uring_lock);
5127 if (!list_empty(&ctx->poll_list))
5128 io_iopoll_getevents(ctx, &nr_events, 0);
5129 else
Jens Axboe6c271ce2019-01-10 11:22:30 -07005130 timeout = jiffies + ctx->sq_thread_idle;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005131 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005132 }
5133
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005134 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07005135
5136 /*
5137 * If submit got -EBUSY, flag us as needing the application
5138 * to enter the kernel to reap and flush events.
5139 */
5140 if (!to_submit || ret == -EBUSY) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07005141 /*
Stefano Garzarella7143b5a2020-02-21 16:42:16 +01005142 * Drop cur_mm before scheduling, we can't hold it for
5143 * long periods (or over schedule()). Do this before
5144 * adding ourselves to the waitqueue, as the unuse/drop
5145 * may sleep.
5146 */
5147 if (cur_mm) {
5148 unuse_mm(cur_mm);
5149 mmput(cur_mm);
5150 cur_mm = NULL;
5151 }
5152
5153 /*
Jens Axboe6c271ce2019-01-10 11:22:30 -07005154 * We're polling. If we're within the defined idle
5155 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07005156 * to sleep. The exception is if we got EBUSY doing
5157 * more IO, we should wait for the application to
5158 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07005159 */
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005160 if (!list_empty(&ctx->poll_list) ||
Jens Axboedf069d82020-02-04 16:48:34 -07005161 (!time_after(jiffies, timeout) && ret != -EBUSY &&
5162 !percpu_ref_is_dying(&ctx->refs))) {
Jens Axboe9831a902019-09-19 09:48:55 -06005163 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07005164 continue;
5165 }
5166
Jens Axboe6c271ce2019-01-10 11:22:30 -07005167 prepare_to_wait(&ctx->sqo_wait, &wait,
5168 TASK_INTERRUPTIBLE);
5169
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005170 /*
5171 * While doing polled IO, before going to sleep, we need
5172 * to check if there are new reqs added to poll_list, it
5173 * is because reqs may have been punted to io worker and
5174 * will be added to poll_list later, hence check the
5175 * poll_list again.
5176 */
5177 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
5178 !list_empty_careful(&ctx->poll_list)) {
5179 finish_wait(&ctx->sqo_wait, &wait);
5180 continue;
5181 }
5182
Jens Axboe6c271ce2019-01-10 11:22:30 -07005183 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00005184 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02005185 /* make sure to read SQ tail after writing flags */
5186 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07005187
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005188 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07005189 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005190 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07005191 finish_wait(&ctx->sqo_wait, &wait);
5192 break;
5193 }
5194 if (signal_pending(current))
5195 flush_signals(current);
5196 schedule();
5197 finish_wait(&ctx->sqo_wait, &wait);
5198
Hristo Venev75b28af2019-08-26 17:23:46 +00005199 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005200 continue;
5201 }
5202 finish_wait(&ctx->sqo_wait, &wait);
5203
Hristo Venev75b28af2019-08-26 17:23:46 +00005204 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005205 }
5206
Jens Axboe8a4955f2019-12-09 14:52:35 -07005207 mutex_lock(&ctx->uring_lock);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005208 ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true);
Jens Axboe8a4955f2019-12-09 14:52:35 -07005209 mutex_unlock(&ctx->uring_lock);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005210 timeout = jiffies + ctx->sq_thread_idle;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005211 }
5212
5213 set_fs(old_fs);
5214 if (cur_mm) {
5215 unuse_mm(cur_mm);
5216 mmput(cur_mm);
5217 }
Jens Axboe181e4482019-11-25 08:52:30 -07005218 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06005219
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005220 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06005221
Jens Axboe6c271ce2019-01-10 11:22:30 -07005222 return 0;
5223}
5224
Jens Axboebda52162019-09-24 13:47:15 -06005225struct io_wait_queue {
5226 struct wait_queue_entry wq;
5227 struct io_ring_ctx *ctx;
5228 unsigned to_wait;
5229 unsigned nr_timeouts;
5230};
5231
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005232static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06005233{
5234 struct io_ring_ctx *ctx = iowq->ctx;
5235
5236 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08005237 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06005238 * started waiting. For timeouts, we always want to return to userspace,
5239 * regardless of event count.
5240 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005241 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06005242 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
5243}
5244
5245static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
5246 int wake_flags, void *key)
5247{
5248 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
5249 wq);
5250
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005251 /* use noflush == true, as we can't safely rely on locking context */
5252 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06005253 return -1;
5254
5255 return autoremove_wake_function(curr, mode, wake_flags, key);
5256}
5257
Jens Axboe2b188cc2019-01-07 10:46:33 -07005258/*
5259 * Wait until events become available, if we don't already have some. The
5260 * application must reap them itself, as they reside on the shared cq ring.
5261 */
5262static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
5263 const sigset_t __user *sig, size_t sigsz)
5264{
Jens Axboebda52162019-09-24 13:47:15 -06005265 struct io_wait_queue iowq = {
5266 .wq = {
5267 .private = current,
5268 .func = io_wake_function,
5269 .entry = LIST_HEAD_INIT(iowq.wq.entry),
5270 },
5271 .ctx = ctx,
5272 .to_wait = min_events,
5273 };
Hristo Venev75b28af2019-08-26 17:23:46 +00005274 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005275 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005276
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005277 if (io_cqring_events(ctx, false) >= min_events)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005278 return 0;
5279
5280 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005281#ifdef CONFIG_COMPAT
5282 if (in_compat_syscall())
5283 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07005284 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005285 else
5286#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07005287 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005288
Jens Axboe2b188cc2019-01-07 10:46:33 -07005289 if (ret)
5290 return ret;
5291 }
5292
Jens Axboebda52162019-09-24 13:47:15 -06005293 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02005294 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06005295 do {
5296 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
5297 TASK_INTERRUPTIBLE);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005298 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06005299 break;
5300 schedule();
5301 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005302 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06005303 break;
5304 }
5305 } while (1);
5306 finish_wait(&ctx->wait, &iowq.wq);
5307
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005308 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005309
Hristo Venev75b28af2019-08-26 17:23:46 +00005310 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005311}
5312
Jens Axboe6b063142019-01-10 22:13:58 -07005313static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
5314{
5315#if defined(CONFIG_UNIX)
5316 if (ctx->ring_sock) {
5317 struct sock *sock = ctx->ring_sock->sk;
5318 struct sk_buff *skb;
5319
5320 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
5321 kfree_skb(skb);
5322 }
5323#else
5324 int i;
5325
Jens Axboe65e19f52019-10-26 07:20:21 -06005326 for (i = 0; i < ctx->nr_user_files; i++) {
5327 struct file *file;
5328
5329 file = io_file_from_index(ctx, i);
5330 if (file)
5331 fput(file);
5332 }
Jens Axboe6b063142019-01-10 22:13:58 -07005333#endif
5334}
5335
Jens Axboe05f3fb32019-12-09 11:22:50 -07005336static void io_file_ref_kill(struct percpu_ref *ref)
5337{
5338 struct fixed_file_data *data;
5339
5340 data = container_of(ref, struct fixed_file_data, refs);
5341 complete(&data->done);
5342}
5343
Jens Axboe805b13a2020-03-08 20:07:28 -06005344static void io_file_ref_exit_and_free(struct work_struct *work)
Jens Axboec1e21482020-03-04 07:25:50 -07005345{
Jens Axboe805b13a2020-03-08 20:07:28 -06005346 struct fixed_file_data *data;
5347
5348 data = container_of(work, struct fixed_file_data, ref_work);
5349
5350 /*
5351 * Ensure any percpu-ref atomic switch callback has run, it could have
5352 * been in progress when the files were being unregistered. Once
5353 * that's done, we can safely exit and free the ref and containing
5354 * data structure.
5355 */
5356 rcu_barrier();
Jens Axboec1e21482020-03-04 07:25:50 -07005357 percpu_ref_exit(&data->refs);
5358 kfree(data);
5359}
5360
Jens Axboe6b063142019-01-10 22:13:58 -07005361static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
5362{
Jens Axboe05f3fb32019-12-09 11:22:50 -07005363 struct fixed_file_data *data = ctx->file_data;
Jens Axboe65e19f52019-10-26 07:20:21 -06005364 unsigned nr_tables, i;
5365
Jens Axboe05f3fb32019-12-09 11:22:50 -07005366 if (!data)
Jens Axboe6b063142019-01-10 22:13:58 -07005367 return -ENXIO;
5368
Jens Axboe05f3fb32019-12-09 11:22:50 -07005369 percpu_ref_kill_and_confirm(&data->refs, io_file_ref_kill);
Jens Axboee46a7952020-01-17 11:15:34 -07005370 flush_work(&data->ref_work);
Jens Axboe2faf8522020-02-04 19:54:55 -07005371 wait_for_completion(&data->done);
5372 io_ring_file_ref_flush(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005373
Jens Axboe6b063142019-01-10 22:13:58 -07005374 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06005375 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
5376 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005377 kfree(data->table[i].files);
5378 kfree(data->table);
Jens Axboe805b13a2020-03-08 20:07:28 -06005379 INIT_WORK(&data->ref_work, io_file_ref_exit_and_free);
5380 queue_work(system_wq, &data->ref_work);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005381 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07005382 ctx->nr_user_files = 0;
5383 return 0;
5384}
5385
Jens Axboe6c271ce2019-01-10 11:22:30 -07005386static void io_sq_thread_stop(struct io_ring_ctx *ctx)
5387{
5388 if (ctx->sqo_thread) {
Jens Axboe206aefd2019-11-07 18:27:42 -07005389 wait_for_completion(&ctx->completions[1]);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005390 /*
5391 * The park is a bit of a work-around, without it we get
5392 * warning spews on shutdown with SQPOLL set and affinity
5393 * set to a single CPU.
5394 */
Jens Axboe06058632019-04-13 09:26:03 -06005395 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005396 kthread_stop(ctx->sqo_thread);
5397 ctx->sqo_thread = NULL;
5398 }
5399}
5400
Jens Axboe6b063142019-01-10 22:13:58 -07005401static void io_finish_async(struct io_ring_ctx *ctx)
5402{
Jens Axboe6c271ce2019-01-10 11:22:30 -07005403 io_sq_thread_stop(ctx);
5404
Jens Axboe561fb042019-10-24 07:25:42 -06005405 if (ctx->io_wq) {
5406 io_wq_destroy(ctx->io_wq);
5407 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07005408 }
5409}
5410
5411#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07005412/*
5413 * Ensure the UNIX gc is aware of our file set, so we are certain that
5414 * the io_uring can be safely unregistered on process exit, even if we have
5415 * loops in the file referencing.
5416 */
5417static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
5418{
5419 struct sock *sk = ctx->ring_sock->sk;
5420 struct scm_fp_list *fpl;
5421 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06005422 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07005423
5424 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
5425 unsigned long inflight = ctx->user->unix_inflight + nr;
5426
5427 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
5428 return -EMFILE;
5429 }
5430
5431 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
5432 if (!fpl)
5433 return -ENOMEM;
5434
5435 skb = alloc_skb(0, GFP_KERNEL);
5436 if (!skb) {
5437 kfree(fpl);
5438 return -ENOMEM;
5439 }
5440
5441 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07005442
Jens Axboe08a45172019-10-03 08:11:03 -06005443 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07005444 fpl->user = get_uid(ctx->user);
5445 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005446 struct file *file = io_file_from_index(ctx, i + offset);
5447
5448 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06005449 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06005450 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06005451 unix_inflight(fpl->user, fpl->fp[nr_files]);
5452 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07005453 }
5454
Jens Axboe08a45172019-10-03 08:11:03 -06005455 if (nr_files) {
5456 fpl->max = SCM_MAX_FD;
5457 fpl->count = nr_files;
5458 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005459 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06005460 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
5461 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07005462
Jens Axboe08a45172019-10-03 08:11:03 -06005463 for (i = 0; i < nr_files; i++)
5464 fput(fpl->fp[i]);
5465 } else {
5466 kfree_skb(skb);
5467 kfree(fpl);
5468 }
Jens Axboe6b063142019-01-10 22:13:58 -07005469
5470 return 0;
5471}
5472
5473/*
5474 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
5475 * causes regular reference counting to break down. We rely on the UNIX
5476 * garbage collection to take care of this problem for us.
5477 */
5478static int io_sqe_files_scm(struct io_ring_ctx *ctx)
5479{
5480 unsigned left, total;
5481 int ret = 0;
5482
5483 total = 0;
5484 left = ctx->nr_user_files;
5485 while (left) {
5486 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07005487
5488 ret = __io_sqe_files_scm(ctx, this_files, total);
5489 if (ret)
5490 break;
5491 left -= this_files;
5492 total += this_files;
5493 }
5494
5495 if (!ret)
5496 return 0;
5497
5498 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005499 struct file *file = io_file_from_index(ctx, total);
5500
5501 if (file)
5502 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07005503 total++;
5504 }
5505
5506 return ret;
5507}
5508#else
5509static int io_sqe_files_scm(struct io_ring_ctx *ctx)
5510{
5511 return 0;
5512}
5513#endif
5514
Jens Axboe65e19f52019-10-26 07:20:21 -06005515static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
5516 unsigned nr_files)
5517{
5518 int i;
5519
5520 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005521 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06005522 unsigned this_files;
5523
5524 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
5525 table->files = kcalloc(this_files, sizeof(struct file *),
5526 GFP_KERNEL);
5527 if (!table->files)
5528 break;
5529 nr_files -= this_files;
5530 }
5531
5532 if (i == nr_tables)
5533 return 0;
5534
5535 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005536 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06005537 kfree(table->files);
5538 }
5539 return 1;
5540}
5541
Jens Axboe05f3fb32019-12-09 11:22:50 -07005542static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
Jens Axboec3a31e62019-10-03 13:59:56 -06005543{
5544#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06005545 struct sock *sock = ctx->ring_sock->sk;
5546 struct sk_buff_head list, *head = &sock->sk_receive_queue;
5547 struct sk_buff *skb;
5548 int i;
5549
5550 __skb_queue_head_init(&list);
5551
5552 /*
5553 * Find the skb that holds this file in its SCM_RIGHTS. When found,
5554 * remove this entry and rearrange the file array.
5555 */
5556 skb = skb_dequeue(head);
5557 while (skb) {
5558 struct scm_fp_list *fp;
5559
5560 fp = UNIXCB(skb).fp;
5561 for (i = 0; i < fp->count; i++) {
5562 int left;
5563
5564 if (fp->fp[i] != file)
5565 continue;
5566
5567 unix_notinflight(fp->user, fp->fp[i]);
5568 left = fp->count - 1 - i;
5569 if (left) {
5570 memmove(&fp->fp[i], &fp->fp[i + 1],
5571 left * sizeof(struct file *));
5572 }
5573 fp->count--;
5574 if (!fp->count) {
5575 kfree_skb(skb);
5576 skb = NULL;
5577 } else {
5578 __skb_queue_tail(&list, skb);
5579 }
5580 fput(file);
5581 file = NULL;
5582 break;
5583 }
5584
5585 if (!file)
5586 break;
5587
5588 __skb_queue_tail(&list, skb);
5589
5590 skb = skb_dequeue(head);
5591 }
5592
5593 if (skb_peek(&list)) {
5594 spin_lock_irq(&head->lock);
5595 while ((skb = __skb_dequeue(&list)) != NULL)
5596 __skb_queue_tail(head, skb);
5597 spin_unlock_irq(&head->lock);
5598 }
5599#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07005600 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06005601#endif
5602}
5603
Jens Axboe05f3fb32019-12-09 11:22:50 -07005604struct io_file_put {
5605 struct llist_node llist;
5606 struct file *file;
5607 struct completion *done;
5608};
5609
Jens Axboe2faf8522020-02-04 19:54:55 -07005610static void io_ring_file_ref_flush(struct fixed_file_data *data)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005611{
5612 struct io_file_put *pfile, *tmp;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005613 struct llist_node *node;
5614
Jens Axboe05f3fb32019-12-09 11:22:50 -07005615 while ((node = llist_del_all(&data->put_llist)) != NULL) {
5616 llist_for_each_entry_safe(pfile, tmp, node, llist) {
5617 io_ring_file_put(data->ctx, pfile->file);
5618 if (pfile->done)
5619 complete(pfile->done);
5620 else
5621 kfree(pfile);
5622 }
5623 }
Jens Axboe2faf8522020-02-04 19:54:55 -07005624}
Jens Axboe05f3fb32019-12-09 11:22:50 -07005625
Jens Axboe2faf8522020-02-04 19:54:55 -07005626static void io_ring_file_ref_switch(struct work_struct *work)
5627{
5628 struct fixed_file_data *data;
5629
5630 data = container_of(work, struct fixed_file_data, ref_work);
5631 io_ring_file_ref_flush(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005632 percpu_ref_switch_to_percpu(&data->refs);
5633}
5634
5635static void io_file_data_ref_zero(struct percpu_ref *ref)
5636{
5637 struct fixed_file_data *data;
5638
5639 data = container_of(ref, struct fixed_file_data, refs);
5640
Jens Axboe2faf8522020-02-04 19:54:55 -07005641 /*
5642 * We can't safely switch from inside this context, punt to wq. If
5643 * the table ref is going away, the table is being unregistered.
5644 * Don't queue up the async work for that case, the caller will
5645 * handle it.
5646 */
5647 if (!percpu_ref_is_dying(&data->refs))
5648 queue_work(system_wq, &data->ref_work);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005649}
5650
5651static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
5652 unsigned nr_args)
5653{
5654 __s32 __user *fds = (__s32 __user *) arg;
5655 unsigned nr_tables;
5656 struct file *file;
5657 int fd, ret = 0;
5658 unsigned i;
5659
5660 if (ctx->file_data)
5661 return -EBUSY;
5662 if (!nr_args)
5663 return -EINVAL;
5664 if (nr_args > IORING_MAX_FIXED_FILES)
5665 return -EMFILE;
5666
5667 ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
5668 if (!ctx->file_data)
5669 return -ENOMEM;
5670 ctx->file_data->ctx = ctx;
5671 init_completion(&ctx->file_data->done);
5672
5673 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
5674 ctx->file_data->table = kcalloc(nr_tables,
5675 sizeof(struct fixed_file_table),
5676 GFP_KERNEL);
5677 if (!ctx->file_data->table) {
5678 kfree(ctx->file_data);
5679 ctx->file_data = NULL;
5680 return -ENOMEM;
5681 }
5682
5683 if (percpu_ref_init(&ctx->file_data->refs, io_file_data_ref_zero,
5684 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
5685 kfree(ctx->file_data->table);
5686 kfree(ctx->file_data);
5687 ctx->file_data = NULL;
5688 return -ENOMEM;
5689 }
5690 ctx->file_data->put_llist.first = NULL;
5691 INIT_WORK(&ctx->file_data->ref_work, io_ring_file_ref_switch);
5692
5693 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
5694 percpu_ref_exit(&ctx->file_data->refs);
5695 kfree(ctx->file_data->table);
5696 kfree(ctx->file_data);
5697 ctx->file_data = NULL;
5698 return -ENOMEM;
5699 }
5700
5701 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
5702 struct fixed_file_table *table;
5703 unsigned index;
5704
5705 ret = -EFAULT;
5706 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
5707 break;
5708 /* allow sparse sets */
5709 if (fd == -1) {
5710 ret = 0;
5711 continue;
5712 }
5713
5714 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
5715 index = i & IORING_FILE_TABLE_MASK;
5716 file = fget(fd);
5717
5718 ret = -EBADF;
5719 if (!file)
5720 break;
5721
5722 /*
5723 * Don't allow io_uring instances to be registered. If UNIX
5724 * isn't enabled, then this causes a reference cycle and this
5725 * instance can never get freed. If UNIX is enabled we'll
5726 * handle it just fine, but there's still no point in allowing
5727 * a ring fd as it doesn't support regular read/write anyway.
5728 */
5729 if (file->f_op == &io_uring_fops) {
5730 fput(file);
5731 break;
5732 }
5733 ret = 0;
5734 table->files[index] = file;
5735 }
5736
5737 if (ret) {
5738 for (i = 0; i < ctx->nr_user_files; i++) {
5739 file = io_file_from_index(ctx, i);
5740 if (file)
5741 fput(file);
5742 }
5743 for (i = 0; i < nr_tables; i++)
5744 kfree(ctx->file_data->table[i].files);
5745
5746 kfree(ctx->file_data->table);
5747 kfree(ctx->file_data);
5748 ctx->file_data = NULL;
5749 ctx->nr_user_files = 0;
5750 return ret;
5751 }
5752
5753 ret = io_sqe_files_scm(ctx);
5754 if (ret)
5755 io_sqe_files_unregister(ctx);
5756
5757 return ret;
5758}
5759
Jens Axboec3a31e62019-10-03 13:59:56 -06005760static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
5761 int index)
5762{
5763#if defined(CONFIG_UNIX)
5764 struct sock *sock = ctx->ring_sock->sk;
5765 struct sk_buff_head *head = &sock->sk_receive_queue;
5766 struct sk_buff *skb;
5767
5768 /*
5769 * See if we can merge this file into an existing skb SCM_RIGHTS
5770 * file set. If there's no room, fall back to allocating a new skb
5771 * and filling it in.
5772 */
5773 spin_lock_irq(&head->lock);
5774 skb = skb_peek(head);
5775 if (skb) {
5776 struct scm_fp_list *fpl = UNIXCB(skb).fp;
5777
5778 if (fpl->count < SCM_MAX_FD) {
5779 __skb_unlink(skb, head);
5780 spin_unlock_irq(&head->lock);
5781 fpl->fp[fpl->count] = get_file(file);
5782 unix_inflight(fpl->user, fpl->fp[fpl->count]);
5783 fpl->count++;
5784 spin_lock_irq(&head->lock);
5785 __skb_queue_head(head, skb);
5786 } else {
5787 skb = NULL;
5788 }
5789 }
5790 spin_unlock_irq(&head->lock);
5791
5792 if (skb) {
5793 fput(file);
5794 return 0;
5795 }
5796
5797 return __io_sqe_files_scm(ctx, 1, index);
5798#else
5799 return 0;
5800#endif
5801}
5802
Jens Axboe05f3fb32019-12-09 11:22:50 -07005803static void io_atomic_switch(struct percpu_ref *ref)
Jens Axboec3a31e62019-10-03 13:59:56 -06005804{
Jens Axboe05f3fb32019-12-09 11:22:50 -07005805 struct fixed_file_data *data;
5806
Jens Axboedd3db2a2020-02-26 10:23:43 -07005807 /*
5808 * Juggle reference to ensure we hit zero, if needed, so we can
5809 * switch back to percpu mode
5810 */
Jens Axboe05f3fb32019-12-09 11:22:50 -07005811 data = container_of(ref, struct fixed_file_data, refs);
Jens Axboedd3db2a2020-02-26 10:23:43 -07005812 percpu_ref_put(&data->refs);
5813 percpu_ref_get(&data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005814}
5815
5816static bool io_queue_file_removal(struct fixed_file_data *data,
5817 struct file *file)
5818{
5819 struct io_file_put *pfile, pfile_stack;
5820 DECLARE_COMPLETION_ONSTACK(done);
5821
5822 /*
5823 * If we fail allocating the struct we need for doing async reomval
5824 * of this file, just punt to sync and wait for it.
5825 */
5826 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
5827 if (!pfile) {
5828 pfile = &pfile_stack;
5829 pfile->done = &done;
5830 }
5831
5832 pfile->file = file;
5833 llist_add(&pfile->llist, &data->put_llist);
5834
5835 if (pfile == &pfile_stack) {
Jens Axboedd3db2a2020-02-26 10:23:43 -07005836 percpu_ref_switch_to_atomic(&data->refs, io_atomic_switch);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005837 wait_for_completion(&done);
5838 flush_work(&data->ref_work);
5839 return false;
5840 }
5841
5842 return true;
5843}
5844
5845static int __io_sqe_files_update(struct io_ring_ctx *ctx,
5846 struct io_uring_files_update *up,
5847 unsigned nr_args)
5848{
5849 struct fixed_file_data *data = ctx->file_data;
5850 bool ref_switch = false;
5851 struct file *file;
Jens Axboec3a31e62019-10-03 13:59:56 -06005852 __s32 __user *fds;
5853 int fd, i, err;
5854 __u32 done;
5855
Jens Axboe05f3fb32019-12-09 11:22:50 -07005856 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06005857 return -EOVERFLOW;
5858 if (done > ctx->nr_user_files)
5859 return -EINVAL;
5860
5861 done = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005862 fds = u64_to_user_ptr(up->fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06005863 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005864 struct fixed_file_table *table;
5865 unsigned index;
5866
Jens Axboec3a31e62019-10-03 13:59:56 -06005867 err = 0;
5868 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
5869 err = -EFAULT;
5870 break;
5871 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07005872 i = array_index_nospec(up->offset, ctx->nr_user_files);
5873 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
Jens Axboe65e19f52019-10-26 07:20:21 -06005874 index = i & IORING_FILE_TABLE_MASK;
5875 if (table->files[index]) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005876 file = io_file_from_index(ctx, index);
Jens Axboe65e19f52019-10-26 07:20:21 -06005877 table->files[index] = NULL;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005878 if (io_queue_file_removal(data, file))
5879 ref_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06005880 }
5881 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06005882 file = fget(fd);
5883 if (!file) {
5884 err = -EBADF;
5885 break;
5886 }
5887 /*
5888 * Don't allow io_uring instances to be registered. If
5889 * UNIX isn't enabled, then this causes a reference
5890 * cycle and this instance can never get freed. If UNIX
5891 * is enabled we'll handle it just fine, but there's
5892 * still no point in allowing a ring fd as it doesn't
5893 * support regular read/write anyway.
5894 */
5895 if (file->f_op == &io_uring_fops) {
5896 fput(file);
5897 err = -EBADF;
5898 break;
5899 }
Jens Axboe65e19f52019-10-26 07:20:21 -06005900 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06005901 err = io_sqe_file_register(ctx, file, i);
5902 if (err)
5903 break;
5904 }
5905 nr_args--;
5906 done++;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005907 up->offset++;
5908 }
5909
Jens Axboedd3db2a2020-02-26 10:23:43 -07005910 if (ref_switch)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005911 percpu_ref_switch_to_atomic(&data->refs, io_atomic_switch);
Jens Axboec3a31e62019-10-03 13:59:56 -06005912
5913 return done ? done : err;
5914}
Jens Axboe05f3fb32019-12-09 11:22:50 -07005915static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
5916 unsigned nr_args)
5917{
5918 struct io_uring_files_update up;
5919
5920 if (!ctx->file_data)
5921 return -ENXIO;
5922 if (!nr_args)
5923 return -EINVAL;
5924 if (copy_from_user(&up, arg, sizeof(up)))
5925 return -EFAULT;
5926 if (up.resv)
5927 return -EINVAL;
5928
5929 return __io_sqe_files_update(ctx, &up, nr_args);
5930}
Jens Axboec3a31e62019-10-03 13:59:56 -06005931
Jens Axboe7d723062019-11-12 22:31:31 -07005932static void io_put_work(struct io_wq_work *work)
5933{
5934 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
5935
5936 io_put_req(req);
5937}
5938
5939static void io_get_work(struct io_wq_work *work)
5940{
5941 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
5942
5943 refcount_inc(&req->refs);
5944}
5945
Pavel Begunkov24369c22020-01-28 03:15:48 +03005946static int io_init_wq_offload(struct io_ring_ctx *ctx,
5947 struct io_uring_params *p)
5948{
5949 struct io_wq_data data;
5950 struct fd f;
5951 struct io_ring_ctx *ctx_attach;
5952 unsigned int concurrency;
5953 int ret = 0;
5954
5955 data.user = ctx->user;
5956 data.get_work = io_get_work;
5957 data.put_work = io_put_work;
5958
5959 if (!(p->flags & IORING_SETUP_ATTACH_WQ)) {
5960 /* Do QD, or 4 * CPUS, whatever is smallest */
5961 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
5962
5963 ctx->io_wq = io_wq_create(concurrency, &data);
5964 if (IS_ERR(ctx->io_wq)) {
5965 ret = PTR_ERR(ctx->io_wq);
5966 ctx->io_wq = NULL;
5967 }
5968 return ret;
5969 }
5970
5971 f = fdget(p->wq_fd);
5972 if (!f.file)
5973 return -EBADF;
5974
5975 if (f.file->f_op != &io_uring_fops) {
5976 ret = -EINVAL;
5977 goto out_fput;
5978 }
5979
5980 ctx_attach = f.file->private_data;
5981 /* @io_wq is protected by holding the fd */
5982 if (!io_wq_get(ctx_attach->io_wq, &data)) {
5983 ret = -EINVAL;
5984 goto out_fput;
5985 }
5986
5987 ctx->io_wq = ctx_attach->io_wq;
5988out_fput:
5989 fdput(f);
5990 return ret;
5991}
5992
Jens Axboe6c271ce2019-01-10 11:22:30 -07005993static int io_sq_offload_start(struct io_ring_ctx *ctx,
5994 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005995{
5996 int ret;
5997
Jens Axboe6c271ce2019-01-10 11:22:30 -07005998 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005999 mmgrab(current->mm);
6000 ctx->sqo_mm = current->mm;
6001
Jens Axboe6c271ce2019-01-10 11:22:30 -07006002 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06006003 ret = -EPERM;
6004 if (!capable(CAP_SYS_ADMIN))
6005 goto err;
6006
Jens Axboe917257d2019-04-13 09:28:55 -06006007 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
6008 if (!ctx->sq_thread_idle)
6009 ctx->sq_thread_idle = HZ;
6010
Jens Axboe6c271ce2019-01-10 11:22:30 -07006011 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06006012 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006013
Jens Axboe917257d2019-04-13 09:28:55 -06006014 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06006015 if (cpu >= nr_cpu_ids)
6016 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08006017 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06006018 goto err;
6019
Jens Axboe6c271ce2019-01-10 11:22:30 -07006020 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
6021 ctx, cpu,
6022 "io_uring-sq");
6023 } else {
6024 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
6025 "io_uring-sq");
6026 }
6027 if (IS_ERR(ctx->sqo_thread)) {
6028 ret = PTR_ERR(ctx->sqo_thread);
6029 ctx->sqo_thread = NULL;
6030 goto err;
6031 }
6032 wake_up_process(ctx->sqo_thread);
6033 } else if (p->flags & IORING_SETUP_SQ_AFF) {
6034 /* Can't have SQ_AFF without SQPOLL */
6035 ret = -EINVAL;
6036 goto err;
6037 }
6038
Pavel Begunkov24369c22020-01-28 03:15:48 +03006039 ret = io_init_wq_offload(ctx, p);
6040 if (ret)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006041 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006042
6043 return 0;
6044err:
Jens Axboe54a91f32019-09-10 09:15:04 -06006045 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006046 mmdrop(ctx->sqo_mm);
6047 ctx->sqo_mm = NULL;
6048 return ret;
6049}
6050
6051static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
6052{
6053 atomic_long_sub(nr_pages, &user->locked_vm);
6054}
6055
6056static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
6057{
6058 unsigned long page_limit, cur_pages, new_pages;
6059
6060 /* Don't allow more pages than we can safely lock */
6061 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
6062
6063 do {
6064 cur_pages = atomic_long_read(&user->locked_vm);
6065 new_pages = cur_pages + nr_pages;
6066 if (new_pages > page_limit)
6067 return -ENOMEM;
6068 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
6069 new_pages) != cur_pages);
6070
6071 return 0;
6072}
6073
6074static void io_mem_free(void *ptr)
6075{
Mark Rutland52e04ef2019-04-30 17:30:21 +01006076 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006077
Mark Rutland52e04ef2019-04-30 17:30:21 +01006078 if (!ptr)
6079 return;
6080
6081 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006082 if (put_page_testzero(page))
6083 free_compound_page(page);
6084}
6085
6086static void *io_mem_alloc(size_t size)
6087{
6088 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
6089 __GFP_NORETRY;
6090
6091 return (void *) __get_free_pages(gfp_flags, get_order(size));
6092}
6093
Hristo Venev75b28af2019-08-26 17:23:46 +00006094static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
6095 size_t *sq_offset)
6096{
6097 struct io_rings *rings;
6098 size_t off, sq_array_size;
6099
6100 off = struct_size(rings, cqes, cq_entries);
6101 if (off == SIZE_MAX)
6102 return SIZE_MAX;
6103
6104#ifdef CONFIG_SMP
6105 off = ALIGN(off, SMP_CACHE_BYTES);
6106 if (off == 0)
6107 return SIZE_MAX;
6108#endif
6109
6110 sq_array_size = array_size(sizeof(u32), sq_entries);
6111 if (sq_array_size == SIZE_MAX)
6112 return SIZE_MAX;
6113
6114 if (check_add_overflow(off, sq_array_size, &off))
6115 return SIZE_MAX;
6116
6117 if (sq_offset)
6118 *sq_offset = off;
6119
6120 return off;
6121}
6122
Jens Axboe2b188cc2019-01-07 10:46:33 -07006123static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
6124{
Hristo Venev75b28af2019-08-26 17:23:46 +00006125 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006126
Hristo Venev75b28af2019-08-26 17:23:46 +00006127 pages = (size_t)1 << get_order(
6128 rings_size(sq_entries, cq_entries, NULL));
6129 pages += (size_t)1 << get_order(
6130 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07006131
Hristo Venev75b28af2019-08-26 17:23:46 +00006132 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006133}
6134
Jens Axboeedafcce2019-01-09 09:16:05 -07006135static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
6136{
6137 int i, j;
6138
6139 if (!ctx->user_bufs)
6140 return -ENXIO;
6141
6142 for (i = 0; i < ctx->nr_user_bufs; i++) {
6143 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
6144
6145 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08006146 unpin_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07006147
6148 if (ctx->account_mem)
6149 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006150 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07006151 imu->nr_bvecs = 0;
6152 }
6153
6154 kfree(ctx->user_bufs);
6155 ctx->user_bufs = NULL;
6156 ctx->nr_user_bufs = 0;
6157 return 0;
6158}
6159
6160static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
6161 void __user *arg, unsigned index)
6162{
6163 struct iovec __user *src;
6164
6165#ifdef CONFIG_COMPAT
6166 if (ctx->compat) {
6167 struct compat_iovec __user *ciovs;
6168 struct compat_iovec ciov;
6169
6170 ciovs = (struct compat_iovec __user *) arg;
6171 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
6172 return -EFAULT;
6173
Jens Axboed55e5f52019-12-11 16:12:15 -07006174 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07006175 dst->iov_len = ciov.iov_len;
6176 return 0;
6177 }
6178#endif
6179 src = (struct iovec __user *) arg;
6180 if (copy_from_user(dst, &src[index], sizeof(*dst)))
6181 return -EFAULT;
6182 return 0;
6183}
6184
6185static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
6186 unsigned nr_args)
6187{
6188 struct vm_area_struct **vmas = NULL;
6189 struct page **pages = NULL;
6190 int i, j, got_pages = 0;
6191 int ret = -EINVAL;
6192
6193 if (ctx->user_bufs)
6194 return -EBUSY;
6195 if (!nr_args || nr_args > UIO_MAXIOV)
6196 return -EINVAL;
6197
6198 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
6199 GFP_KERNEL);
6200 if (!ctx->user_bufs)
6201 return -ENOMEM;
6202
6203 for (i = 0; i < nr_args; i++) {
6204 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
6205 unsigned long off, start, end, ubuf;
6206 int pret, nr_pages;
6207 struct iovec iov;
6208 size_t size;
6209
6210 ret = io_copy_iov(ctx, &iov, arg, i);
6211 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03006212 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07006213
6214 /*
6215 * Don't impose further limits on the size and buffer
6216 * constraints here, we'll -EINVAL later when IO is
6217 * submitted if they are wrong.
6218 */
6219 ret = -EFAULT;
6220 if (!iov.iov_base || !iov.iov_len)
6221 goto err;
6222
6223 /* arbitrary limit, but we need something */
6224 if (iov.iov_len > SZ_1G)
6225 goto err;
6226
6227 ubuf = (unsigned long) iov.iov_base;
6228 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
6229 start = ubuf >> PAGE_SHIFT;
6230 nr_pages = end - start;
6231
6232 if (ctx->account_mem) {
6233 ret = io_account_mem(ctx->user, nr_pages);
6234 if (ret)
6235 goto err;
6236 }
6237
6238 ret = 0;
6239 if (!pages || nr_pages > got_pages) {
6240 kfree(vmas);
6241 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006242 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07006243 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006244 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07006245 sizeof(struct vm_area_struct *),
6246 GFP_KERNEL);
6247 if (!pages || !vmas) {
6248 ret = -ENOMEM;
6249 if (ctx->account_mem)
6250 io_unaccount_mem(ctx->user, nr_pages);
6251 goto err;
6252 }
6253 got_pages = nr_pages;
6254 }
6255
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006256 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07006257 GFP_KERNEL);
6258 ret = -ENOMEM;
6259 if (!imu->bvec) {
6260 if (ctx->account_mem)
6261 io_unaccount_mem(ctx->user, nr_pages);
6262 goto err;
6263 }
6264
6265 ret = 0;
6266 down_read(&current->mm->mmap_sem);
John Hubbard2113b052020-01-30 22:13:13 -08006267 pret = pin_user_pages(ubuf, nr_pages,
Ira Weiny932f4a62019-05-13 17:17:03 -07006268 FOLL_WRITE | FOLL_LONGTERM,
6269 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07006270 if (pret == nr_pages) {
6271 /* don't support file backed memory */
6272 for (j = 0; j < nr_pages; j++) {
6273 struct vm_area_struct *vma = vmas[j];
6274
6275 if (vma->vm_file &&
6276 !is_file_hugepages(vma->vm_file)) {
6277 ret = -EOPNOTSUPP;
6278 break;
6279 }
6280 }
6281 } else {
6282 ret = pret < 0 ? pret : -EFAULT;
6283 }
6284 up_read(&current->mm->mmap_sem);
6285 if (ret) {
6286 /*
6287 * if we did partial map, or found file backed vmas,
6288 * release any pages we did get
6289 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07006290 if (pret > 0)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08006291 unpin_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07006292 if (ctx->account_mem)
6293 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006294 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07006295 goto err;
6296 }
6297
6298 off = ubuf & ~PAGE_MASK;
6299 size = iov.iov_len;
6300 for (j = 0; j < nr_pages; j++) {
6301 size_t vec_len;
6302
6303 vec_len = min_t(size_t, size, PAGE_SIZE - off);
6304 imu->bvec[j].bv_page = pages[j];
6305 imu->bvec[j].bv_len = vec_len;
6306 imu->bvec[j].bv_offset = off;
6307 off = 0;
6308 size -= vec_len;
6309 }
6310 /* store original address for later verification */
6311 imu->ubuf = ubuf;
6312 imu->len = iov.iov_len;
6313 imu->nr_bvecs = nr_pages;
6314
6315 ctx->nr_user_bufs++;
6316 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006317 kvfree(pages);
6318 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07006319 return 0;
6320err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006321 kvfree(pages);
6322 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07006323 io_sqe_buffer_unregister(ctx);
6324 return ret;
6325}
6326
Jens Axboe9b402842019-04-11 11:45:41 -06006327static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
6328{
6329 __s32 __user *fds = arg;
6330 int fd;
6331
6332 if (ctx->cq_ev_fd)
6333 return -EBUSY;
6334
6335 if (copy_from_user(&fd, fds, sizeof(*fds)))
6336 return -EFAULT;
6337
6338 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
6339 if (IS_ERR(ctx->cq_ev_fd)) {
6340 int ret = PTR_ERR(ctx->cq_ev_fd);
6341 ctx->cq_ev_fd = NULL;
6342 return ret;
6343 }
6344
6345 return 0;
6346}
6347
6348static int io_eventfd_unregister(struct io_ring_ctx *ctx)
6349{
6350 if (ctx->cq_ev_fd) {
6351 eventfd_ctx_put(ctx->cq_ev_fd);
6352 ctx->cq_ev_fd = NULL;
6353 return 0;
6354 }
6355
6356 return -ENXIO;
6357}
6358
Jens Axboe2b188cc2019-01-07 10:46:33 -07006359static void io_ring_ctx_free(struct io_ring_ctx *ctx)
6360{
Jens Axboe6b063142019-01-10 22:13:58 -07006361 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006362 if (ctx->sqo_mm)
6363 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07006364
6365 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07006366 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07006367 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06006368 io_eventfd_unregister(ctx);
Jens Axboe41726c92020-02-23 13:11:42 -07006369 idr_destroy(&ctx->personality_idr);
Jens Axboedef596e2019-01-09 08:59:42 -07006370
Jens Axboe2b188cc2019-01-07 10:46:33 -07006371#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07006372 if (ctx->ring_sock) {
6373 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07006374 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07006375 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006376#endif
6377
Hristo Venev75b28af2019-08-26 17:23:46 +00006378 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006379 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006380
6381 percpu_ref_exit(&ctx->refs);
6382 if (ctx->account_mem)
6383 io_unaccount_mem(ctx->user,
6384 ring_pages(ctx->sq_entries, ctx->cq_entries));
6385 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07006386 put_cred(ctx->creds);
Jens Axboe206aefd2019-11-07 18:27:42 -07006387 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -07006388 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07006389 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006390 kfree(ctx);
6391}
6392
6393static __poll_t io_uring_poll(struct file *file, poll_table *wait)
6394{
6395 struct io_ring_ctx *ctx = file->private_data;
6396 __poll_t mask = 0;
6397
6398 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02006399 /*
6400 * synchronizes with barrier from wq_has_sleeper call in
6401 * io_commit_cqring
6402 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07006403 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00006404 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
6405 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006406 mask |= EPOLLOUT | EPOLLWRNORM;
Stefano Garzarella63e5d812020-02-07 13:18:28 +01006407 if (io_cqring_events(ctx, false))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006408 mask |= EPOLLIN | EPOLLRDNORM;
6409
6410 return mask;
6411}
6412
6413static int io_uring_fasync(int fd, struct file *file, int on)
6414{
6415 struct io_ring_ctx *ctx = file->private_data;
6416
6417 return fasync_helper(fd, file, on, &ctx->cq_fasync);
6418}
6419
Jens Axboe071698e2020-01-28 10:04:42 -07006420static int io_remove_personalities(int id, void *p, void *data)
6421{
6422 struct io_ring_ctx *ctx = data;
6423 const struct cred *cred;
6424
6425 cred = idr_remove(&ctx->personality_idr, id);
6426 if (cred)
6427 put_cred(cred);
6428 return 0;
6429}
6430
Jens Axboe2b188cc2019-01-07 10:46:33 -07006431static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
6432{
6433 mutex_lock(&ctx->uring_lock);
6434 percpu_ref_kill(&ctx->refs);
6435 mutex_unlock(&ctx->uring_lock);
6436
Jens Axboedf069d82020-02-04 16:48:34 -07006437 /*
6438 * Wait for sq thread to idle, if we have one. It won't spin on new
6439 * work after we've killed the ctx ref above. This is important to do
6440 * before we cancel existing commands, as the thread could otherwise
6441 * be queueing new work post that. If that's work we need to cancel,
6442 * it could cause shutdown to hang.
6443 */
6444 while (ctx->sqo_thread && !wq_has_sleeper(&ctx->sqo_wait))
6445 cpu_relax();
6446
Jens Axboe5262f562019-09-17 12:26:57 -06006447 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07006448 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06006449
6450 if (ctx->io_wq)
6451 io_wq_cancel_all(ctx->io_wq);
6452
Jens Axboedef596e2019-01-09 08:59:42 -07006453 io_iopoll_reap_events(ctx);
Jens Axboe15dff282019-11-13 09:09:23 -07006454 /* if we failed setting up the ctx, we might not have any rings */
6455 if (ctx->rings)
6456 io_cqring_overflow_flush(ctx, true);
Jens Axboe071698e2020-01-28 10:04:42 -07006457 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
Jens Axboe206aefd2019-11-07 18:27:42 -07006458 wait_for_completion(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006459 io_ring_ctx_free(ctx);
6460}
6461
6462static int io_uring_release(struct inode *inode, struct file *file)
6463{
6464 struct io_ring_ctx *ctx = file->private_data;
6465
6466 file->private_data = NULL;
6467 io_ring_ctx_wait_and_kill(ctx);
6468 return 0;
6469}
6470
Jens Axboefcb323c2019-10-24 12:39:47 -06006471static void io_uring_cancel_files(struct io_ring_ctx *ctx,
6472 struct files_struct *files)
6473{
6474 struct io_kiocb *req;
6475 DEFINE_WAIT(wait);
6476
6477 while (!list_empty_careful(&ctx->inflight_list)) {
Jens Axboe768134d2019-11-10 20:30:53 -07006478 struct io_kiocb *cancel_req = NULL;
Jens Axboefcb323c2019-10-24 12:39:47 -06006479
6480 spin_lock_irq(&ctx->inflight_lock);
6481 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07006482 if (req->work.files != files)
6483 continue;
6484 /* req is being completed, ignore */
6485 if (!refcount_inc_not_zero(&req->refs))
6486 continue;
6487 cancel_req = req;
6488 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06006489 }
Jens Axboe768134d2019-11-10 20:30:53 -07006490 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06006491 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07006492 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06006493 spin_unlock_irq(&ctx->inflight_lock);
6494
Jens Axboe768134d2019-11-10 20:30:53 -07006495 /* We need to keep going until we don't find a matching req */
6496 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06006497 break;
Bob Liu2f6d9b92019-11-13 18:06:24 +08006498
Jens Axboe2ca10252020-02-13 17:17:35 -07006499 if (cancel_req->flags & REQ_F_OVERFLOW) {
6500 spin_lock_irq(&ctx->completion_lock);
6501 list_del(&cancel_req->list);
6502 cancel_req->flags &= ~REQ_F_OVERFLOW;
6503 if (list_empty(&ctx->cq_overflow_list)) {
6504 clear_bit(0, &ctx->sq_check_overflow);
6505 clear_bit(0, &ctx->cq_check_overflow);
6506 }
6507 spin_unlock_irq(&ctx->completion_lock);
6508
6509 WRITE_ONCE(ctx->rings->cq_overflow,
6510 atomic_inc_return(&ctx->cached_cq_overflow));
6511
6512 /*
6513 * Put inflight ref and overflow ref. If that's
6514 * all we had, then we're done with this request.
6515 */
6516 if (refcount_sub_and_test(2, &cancel_req->refs)) {
6517 io_put_req(cancel_req);
6518 continue;
6519 }
6520 }
6521
Bob Liu2f6d9b92019-11-13 18:06:24 +08006522 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
6523 io_put_req(cancel_req);
Jens Axboefcb323c2019-10-24 12:39:47 -06006524 schedule();
6525 }
Jens Axboe768134d2019-11-10 20:30:53 -07006526 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06006527}
6528
6529static int io_uring_flush(struct file *file, void *data)
6530{
6531 struct io_ring_ctx *ctx = file->private_data;
6532
6533 io_uring_cancel_files(ctx, data);
Jens Axboe6ab23142020-02-08 20:23:59 -07006534
6535 /*
6536 * If the task is going away, cancel work it may have pending
6537 */
6538 if (fatal_signal_pending(current) || (current->flags & PF_EXITING))
6539 io_wq_cancel_pid(ctx->io_wq, task_pid_vnr(current));
6540
Jens Axboefcb323c2019-10-24 12:39:47 -06006541 return 0;
6542}
6543
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006544static void *io_uring_validate_mmap_request(struct file *file,
6545 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006546{
Jens Axboe2b188cc2019-01-07 10:46:33 -07006547 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006548 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006549 struct page *page;
6550 void *ptr;
6551
6552 switch (offset) {
6553 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00006554 case IORING_OFF_CQ_RING:
6555 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006556 break;
6557 case IORING_OFF_SQES:
6558 ptr = ctx->sq_sqes;
6559 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006560 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006561 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006562 }
6563
6564 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07006565 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006566 return ERR_PTR(-EINVAL);
6567
6568 return ptr;
6569}
6570
6571#ifdef CONFIG_MMU
6572
6573static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
6574{
6575 size_t sz = vma->vm_end - vma->vm_start;
6576 unsigned long pfn;
6577 void *ptr;
6578
6579 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
6580 if (IS_ERR(ptr))
6581 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006582
6583 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
6584 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
6585}
6586
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006587#else /* !CONFIG_MMU */
6588
6589static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
6590{
6591 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
6592}
6593
6594static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
6595{
6596 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
6597}
6598
6599static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
6600 unsigned long addr, unsigned long len,
6601 unsigned long pgoff, unsigned long flags)
6602{
6603 void *ptr;
6604
6605 ptr = io_uring_validate_mmap_request(file, pgoff, len);
6606 if (IS_ERR(ptr))
6607 return PTR_ERR(ptr);
6608
6609 return (unsigned long) ptr;
6610}
6611
6612#endif /* !CONFIG_MMU */
6613
Jens Axboe2b188cc2019-01-07 10:46:33 -07006614SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
6615 u32, min_complete, u32, flags, const sigset_t __user *, sig,
6616 size_t, sigsz)
6617{
6618 struct io_ring_ctx *ctx;
6619 long ret = -EBADF;
6620 int submitted = 0;
6621 struct fd f;
6622
Jens Axboe6c271ce2019-01-10 11:22:30 -07006623 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006624 return -EINVAL;
6625
6626 f = fdget(fd);
6627 if (!f.file)
6628 return -EBADF;
6629
6630 ret = -EOPNOTSUPP;
6631 if (f.file->f_op != &io_uring_fops)
6632 goto out_fput;
6633
6634 ret = -ENXIO;
6635 ctx = f.file->private_data;
6636 if (!percpu_ref_tryget(&ctx->refs))
6637 goto out_fput;
6638
Jens Axboe6c271ce2019-01-10 11:22:30 -07006639 /*
6640 * For SQ polling, the thread will do all submissions and completions.
6641 * Just return the requested submit count, and wake the thread if
6642 * we were asked to.
6643 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06006644 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006645 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07006646 if (!list_empty_careful(&ctx->cq_overflow_list))
6647 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006648 if (flags & IORING_ENTER_SQ_WAKEUP)
6649 wake_up(&ctx->sqo_wait);
6650 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06006651 } else if (to_submit) {
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006652 struct mm_struct *cur_mm;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006653
6654 mutex_lock(&ctx->uring_lock);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006655 /* already have mm, so io_submit_sqes() won't try to grab it */
6656 cur_mm = ctx->sqo_mm;
6657 submitted = io_submit_sqes(ctx, to_submit, f.file, fd,
6658 &cur_mm, false);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006659 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03006660
6661 if (submitted != to_submit)
6662 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006663 }
6664 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07006665 unsigned nr_events = 0;
6666
Jens Axboe2b188cc2019-01-07 10:46:33 -07006667 min_complete = min(min_complete, ctx->cq_entries);
6668
Jens Axboedef596e2019-01-09 08:59:42 -07006669 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07006670 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07006671 } else {
6672 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
6673 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006674 }
6675
Pavel Begunkov7c504e652019-12-18 19:53:45 +03006676out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03006677 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006678out_fput:
6679 fdput(f);
6680 return submitted ? submitted : ret;
6681}
6682
Tobias Klauserbebdb652020-02-26 18:38:32 +01006683#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07006684static int io_uring_show_cred(int id, void *p, void *data)
6685{
6686 const struct cred *cred = p;
6687 struct seq_file *m = data;
6688 struct user_namespace *uns = seq_user_ns(m);
6689 struct group_info *gi;
6690 kernel_cap_t cap;
6691 unsigned __capi;
6692 int g;
6693
6694 seq_printf(m, "%5d\n", id);
6695 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
6696 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
6697 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
6698 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
6699 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
6700 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
6701 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
6702 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
6703 seq_puts(m, "\n\tGroups:\t");
6704 gi = cred->group_info;
6705 for (g = 0; g < gi->ngroups; g++) {
6706 seq_put_decimal_ull(m, g ? " " : "",
6707 from_kgid_munged(uns, gi->gid[g]));
6708 }
6709 seq_puts(m, "\n\tCapEff:\t");
6710 cap = cred->cap_effective;
6711 CAP_FOR_EACH_U32(__capi)
6712 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
6713 seq_putc(m, '\n');
6714 return 0;
6715}
6716
6717static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
6718{
6719 int i;
6720
6721 mutex_lock(&ctx->uring_lock);
6722 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
6723 for (i = 0; i < ctx->nr_user_files; i++) {
6724 struct fixed_file_table *table;
6725 struct file *f;
6726
6727 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
6728 f = table->files[i & IORING_FILE_TABLE_MASK];
6729 if (f)
6730 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
6731 else
6732 seq_printf(m, "%5u: <none>\n", i);
6733 }
6734 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
6735 for (i = 0; i < ctx->nr_user_bufs; i++) {
6736 struct io_mapped_ubuf *buf = &ctx->user_bufs[i];
6737
6738 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf,
6739 (unsigned int) buf->len);
6740 }
6741 if (!idr_is_empty(&ctx->personality_idr)) {
6742 seq_printf(m, "Personalities:\n");
6743 idr_for_each(&ctx->personality_idr, io_uring_show_cred, m);
6744 }
6745 mutex_unlock(&ctx->uring_lock);
6746}
6747
6748static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
6749{
6750 struct io_ring_ctx *ctx = f->private_data;
6751
6752 if (percpu_ref_tryget(&ctx->refs)) {
6753 __io_uring_show_fdinfo(ctx, m);
6754 percpu_ref_put(&ctx->refs);
6755 }
6756}
Tobias Klauserbebdb652020-02-26 18:38:32 +01006757#endif
Jens Axboe87ce9552020-01-30 08:25:34 -07006758
Jens Axboe2b188cc2019-01-07 10:46:33 -07006759static const struct file_operations io_uring_fops = {
6760 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06006761 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07006762 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006763#ifndef CONFIG_MMU
6764 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
6765 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
6766#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07006767 .poll = io_uring_poll,
6768 .fasync = io_uring_fasync,
Tobias Klauserbebdb652020-02-26 18:38:32 +01006769#ifdef CONFIG_PROC_FS
Jens Axboe87ce9552020-01-30 08:25:34 -07006770 .show_fdinfo = io_uring_show_fdinfo,
Tobias Klauserbebdb652020-02-26 18:38:32 +01006771#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07006772};
6773
6774static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
6775 struct io_uring_params *p)
6776{
Hristo Venev75b28af2019-08-26 17:23:46 +00006777 struct io_rings *rings;
6778 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006779
Hristo Venev75b28af2019-08-26 17:23:46 +00006780 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
6781 if (size == SIZE_MAX)
6782 return -EOVERFLOW;
6783
6784 rings = io_mem_alloc(size);
6785 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006786 return -ENOMEM;
6787
Hristo Venev75b28af2019-08-26 17:23:46 +00006788 ctx->rings = rings;
6789 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
6790 rings->sq_ring_mask = p->sq_entries - 1;
6791 rings->cq_ring_mask = p->cq_entries - 1;
6792 rings->sq_ring_entries = p->sq_entries;
6793 rings->cq_ring_entries = p->cq_entries;
6794 ctx->sq_mask = rings->sq_ring_mask;
6795 ctx->cq_mask = rings->cq_ring_mask;
6796 ctx->sq_entries = rings->sq_ring_entries;
6797 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006798
6799 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07006800 if (size == SIZE_MAX) {
6801 io_mem_free(ctx->rings);
6802 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006803 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07006804 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006805
6806 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07006807 if (!ctx->sq_sqes) {
6808 io_mem_free(ctx->rings);
6809 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006810 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07006811 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006812
Jens Axboe2b188cc2019-01-07 10:46:33 -07006813 return 0;
6814}
6815
6816/*
6817 * Allocate an anonymous fd, this is what constitutes the application
6818 * visible backing of an io_uring instance. The application mmaps this
6819 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
6820 * we have to tie this fd to a socket for file garbage collection purposes.
6821 */
6822static int io_uring_get_fd(struct io_ring_ctx *ctx)
6823{
6824 struct file *file;
6825 int ret;
6826
6827#if defined(CONFIG_UNIX)
6828 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
6829 &ctx->ring_sock);
6830 if (ret)
6831 return ret;
6832#endif
6833
6834 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
6835 if (ret < 0)
6836 goto err;
6837
6838 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
6839 O_RDWR | O_CLOEXEC);
6840 if (IS_ERR(file)) {
6841 put_unused_fd(ret);
6842 ret = PTR_ERR(file);
6843 goto err;
6844 }
6845
6846#if defined(CONFIG_UNIX)
6847 ctx->ring_sock->file = file;
6848#endif
6849 fd_install(ret, file);
6850 return ret;
6851err:
6852#if defined(CONFIG_UNIX)
6853 sock_release(ctx->ring_sock);
6854 ctx->ring_sock = NULL;
6855#endif
6856 return ret;
6857}
6858
6859static int io_uring_create(unsigned entries, struct io_uring_params *p)
6860{
6861 struct user_struct *user = NULL;
6862 struct io_ring_ctx *ctx;
6863 bool account_mem;
6864 int ret;
6865
Jens Axboe8110c1a2019-12-28 15:39:54 -07006866 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006867 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07006868 if (entries > IORING_MAX_ENTRIES) {
6869 if (!(p->flags & IORING_SETUP_CLAMP))
6870 return -EINVAL;
6871 entries = IORING_MAX_ENTRIES;
6872 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006873
6874 /*
6875 * Use twice as many entries for the CQ ring. It's possible for the
6876 * application to drive a higher depth than the size of the SQ ring,
6877 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06006878 * some flexibility in overcommitting a bit. If the application has
6879 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
6880 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07006881 */
6882 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06006883 if (p->flags & IORING_SETUP_CQSIZE) {
6884 /*
6885 * If IORING_SETUP_CQSIZE is set, we do the same roundup
6886 * to a power-of-two, if it isn't already. We do NOT impose
6887 * any cq vs sq ring sizing.
6888 */
Jens Axboe8110c1a2019-12-28 15:39:54 -07006889 if (p->cq_entries < p->sq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06006890 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07006891 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
6892 if (!(p->flags & IORING_SETUP_CLAMP))
6893 return -EINVAL;
6894 p->cq_entries = IORING_MAX_CQ_ENTRIES;
6895 }
Jens Axboe33a107f2019-10-04 12:10:03 -06006896 p->cq_entries = roundup_pow_of_two(p->cq_entries);
6897 } else {
6898 p->cq_entries = 2 * p->sq_entries;
6899 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006900
6901 user = get_uid(current_user());
6902 account_mem = !capable(CAP_IPC_LOCK);
6903
6904 if (account_mem) {
6905 ret = io_account_mem(user,
6906 ring_pages(p->sq_entries, p->cq_entries));
6907 if (ret) {
6908 free_uid(user);
6909 return ret;
6910 }
6911 }
6912
6913 ctx = io_ring_ctx_alloc(p);
6914 if (!ctx) {
6915 if (account_mem)
6916 io_unaccount_mem(user, ring_pages(p->sq_entries,
6917 p->cq_entries));
6918 free_uid(user);
6919 return -ENOMEM;
6920 }
6921 ctx->compat = in_compat_syscall();
6922 ctx->account_mem = account_mem;
6923 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07006924 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07006925
6926 ret = io_allocate_scq_urings(ctx, p);
6927 if (ret)
6928 goto err;
6929
Jens Axboe6c271ce2019-01-10 11:22:30 -07006930 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006931 if (ret)
6932 goto err;
6933
Jens Axboe2b188cc2019-01-07 10:46:33 -07006934 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00006935 p->sq_off.head = offsetof(struct io_rings, sq.head);
6936 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
6937 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
6938 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
6939 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
6940 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
6941 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006942
6943 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00006944 p->cq_off.head = offsetof(struct io_rings, cq.head);
6945 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
6946 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
6947 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
6948 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
6949 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06006950
Jens Axboe044c1ab2019-10-28 09:15:33 -06006951 /*
6952 * Install ring fd as the very last thing, so we don't risk someone
6953 * having closed it before we finish setup
6954 */
6955 ret = io_uring_get_fd(ctx);
6956 if (ret < 0)
6957 goto err;
6958
Jens Axboeda8c9692019-12-02 18:51:26 -07006959 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
Jens Axboecccf0ee2020-01-27 16:34:48 -07006960 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
6961 IORING_FEAT_CUR_PERSONALITY;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006962 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006963 return ret;
6964err:
6965 io_ring_ctx_wait_and_kill(ctx);
6966 return ret;
6967}
6968
6969/*
6970 * Sets up an aio uring context, and returns the fd. Applications asks for a
6971 * ring size, we return the actual sq/cq ring sizes (among other things) in the
6972 * params structure passed in.
6973 */
6974static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
6975{
6976 struct io_uring_params p;
6977 long ret;
6978 int i;
6979
6980 if (copy_from_user(&p, params, sizeof(p)))
6981 return -EFAULT;
6982 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
6983 if (p.resv[i])
6984 return -EINVAL;
6985 }
6986
Jens Axboe6c271ce2019-01-10 11:22:30 -07006987 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07006988 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Pavel Begunkov24369c22020-01-28 03:15:48 +03006989 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006990 return -EINVAL;
6991
6992 ret = io_uring_create(entries, &p);
6993 if (ret < 0)
6994 return ret;
6995
6996 if (copy_to_user(params, &p, sizeof(p)))
6997 return -EFAULT;
6998
6999 return ret;
7000}
7001
7002SYSCALL_DEFINE2(io_uring_setup, u32, entries,
7003 struct io_uring_params __user *, params)
7004{
7005 return io_uring_setup(entries, params);
7006}
7007
Jens Axboe66f4af92020-01-16 15:36:52 -07007008static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
7009{
7010 struct io_uring_probe *p;
7011 size_t size;
7012 int i, ret;
7013
7014 size = struct_size(p, ops, nr_args);
7015 if (size == SIZE_MAX)
7016 return -EOVERFLOW;
7017 p = kzalloc(size, GFP_KERNEL);
7018 if (!p)
7019 return -ENOMEM;
7020
7021 ret = -EFAULT;
7022 if (copy_from_user(p, arg, size))
7023 goto out;
7024 ret = -EINVAL;
7025 if (memchr_inv(p, 0, size))
7026 goto out;
7027
7028 p->last_op = IORING_OP_LAST - 1;
7029 if (nr_args > IORING_OP_LAST)
7030 nr_args = IORING_OP_LAST;
7031
7032 for (i = 0; i < nr_args; i++) {
7033 p->ops[i].op = i;
7034 if (!io_op_defs[i].not_supported)
7035 p->ops[i].flags = IO_URING_OP_SUPPORTED;
7036 }
7037 p->ops_len = i;
7038
7039 ret = 0;
7040 if (copy_to_user(arg, p, size))
7041 ret = -EFAULT;
7042out:
7043 kfree(p);
7044 return ret;
7045}
7046
Jens Axboe071698e2020-01-28 10:04:42 -07007047static int io_register_personality(struct io_ring_ctx *ctx)
7048{
7049 const struct cred *creds = get_current_cred();
7050 int id;
7051
7052 id = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1,
7053 USHRT_MAX, GFP_KERNEL);
7054 if (id < 0)
7055 put_cred(creds);
7056 return id;
7057}
7058
7059static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
7060{
7061 const struct cred *old_creds;
7062
7063 old_creds = idr_remove(&ctx->personality_idr, id);
7064 if (old_creds) {
7065 put_cred(old_creds);
7066 return 0;
7067 }
7068
7069 return -EINVAL;
7070}
7071
7072static bool io_register_op_must_quiesce(int op)
7073{
7074 switch (op) {
7075 case IORING_UNREGISTER_FILES:
7076 case IORING_REGISTER_FILES_UPDATE:
7077 case IORING_REGISTER_PROBE:
7078 case IORING_REGISTER_PERSONALITY:
7079 case IORING_UNREGISTER_PERSONALITY:
7080 return false;
7081 default:
7082 return true;
7083 }
7084}
7085
Jens Axboeedafcce2019-01-09 09:16:05 -07007086static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
7087 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06007088 __releases(ctx->uring_lock)
7089 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07007090{
7091 int ret;
7092
Jens Axboe35fa71a2019-04-22 10:23:23 -06007093 /*
7094 * We're inside the ring mutex, if the ref is already dying, then
7095 * someone else killed the ctx or is already going through
7096 * io_uring_register().
7097 */
7098 if (percpu_ref_is_dying(&ctx->refs))
7099 return -ENXIO;
7100
Jens Axboe071698e2020-01-28 10:04:42 -07007101 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007102 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06007103
Jens Axboe05f3fb32019-12-09 11:22:50 -07007104 /*
7105 * Drop uring mutex before waiting for references to exit. If
7106 * another thread is currently inside io_uring_enter() it might
7107 * need to grab the uring_lock to make progress. If we hold it
7108 * here across the drain wait, then we can deadlock. It's safe
7109 * to drop the mutex here, since no new references will come in
7110 * after we've killed the percpu ref.
7111 */
7112 mutex_unlock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07007113 ret = wait_for_completion_interruptible(&ctx->completions[0]);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007114 mutex_lock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07007115 if (ret) {
7116 percpu_ref_resurrect(&ctx->refs);
7117 ret = -EINTR;
7118 goto out;
7119 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007120 }
Jens Axboeedafcce2019-01-09 09:16:05 -07007121
7122 switch (opcode) {
7123 case IORING_REGISTER_BUFFERS:
7124 ret = io_sqe_buffer_register(ctx, arg, nr_args);
7125 break;
7126 case IORING_UNREGISTER_BUFFERS:
7127 ret = -EINVAL;
7128 if (arg || nr_args)
7129 break;
7130 ret = io_sqe_buffer_unregister(ctx);
7131 break;
Jens Axboe6b063142019-01-10 22:13:58 -07007132 case IORING_REGISTER_FILES:
7133 ret = io_sqe_files_register(ctx, arg, nr_args);
7134 break;
7135 case IORING_UNREGISTER_FILES:
7136 ret = -EINVAL;
7137 if (arg || nr_args)
7138 break;
7139 ret = io_sqe_files_unregister(ctx);
7140 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06007141 case IORING_REGISTER_FILES_UPDATE:
7142 ret = io_sqe_files_update(ctx, arg, nr_args);
7143 break;
Jens Axboe9b402842019-04-11 11:45:41 -06007144 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -07007145 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -06007146 ret = -EINVAL;
7147 if (nr_args != 1)
7148 break;
7149 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -07007150 if (ret)
7151 break;
7152 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
7153 ctx->eventfd_async = 1;
7154 else
7155 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -06007156 break;
7157 case IORING_UNREGISTER_EVENTFD:
7158 ret = -EINVAL;
7159 if (arg || nr_args)
7160 break;
7161 ret = io_eventfd_unregister(ctx);
7162 break;
Jens Axboe66f4af92020-01-16 15:36:52 -07007163 case IORING_REGISTER_PROBE:
7164 ret = -EINVAL;
7165 if (!arg || nr_args > 256)
7166 break;
7167 ret = io_probe(ctx, arg, nr_args);
7168 break;
Jens Axboe071698e2020-01-28 10:04:42 -07007169 case IORING_REGISTER_PERSONALITY:
7170 ret = -EINVAL;
7171 if (arg || nr_args)
7172 break;
7173 ret = io_register_personality(ctx);
7174 break;
7175 case IORING_UNREGISTER_PERSONALITY:
7176 ret = -EINVAL;
7177 if (arg)
7178 break;
7179 ret = io_unregister_personality(ctx, nr_args);
7180 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07007181 default:
7182 ret = -EINVAL;
7183 break;
7184 }
7185
Jens Axboe071698e2020-01-28 10:04:42 -07007186 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007187 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -07007188 percpu_ref_reinit(&ctx->refs);
Jens Axboec1503682020-01-08 08:26:07 -07007189out:
7190 reinit_completion(&ctx->completions[0]);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007191 }
Jens Axboeedafcce2019-01-09 09:16:05 -07007192 return ret;
7193}
7194
7195SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
7196 void __user *, arg, unsigned int, nr_args)
7197{
7198 struct io_ring_ctx *ctx;
7199 long ret = -EBADF;
7200 struct fd f;
7201
7202 f = fdget(fd);
7203 if (!f.file)
7204 return -EBADF;
7205
7206 ret = -EOPNOTSUPP;
7207 if (f.file->f_op != &io_uring_fops)
7208 goto out_fput;
7209
7210 ctx = f.file->private_data;
7211
7212 mutex_lock(&ctx->uring_lock);
7213 ret = __io_uring_register(ctx, opcode, arg, nr_args);
7214 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02007215 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
7216 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07007217out_fput:
7218 fdput(f);
7219 return ret;
7220}
7221
Jens Axboe2b188cc2019-01-07 10:46:33 -07007222static int __init io_uring_init(void)
7223{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01007224#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
7225 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
7226 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
7227} while (0)
7228
7229#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
7230 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
7231 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
7232 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
7233 BUILD_BUG_SQE_ELEM(1, __u8, flags);
7234 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
7235 BUILD_BUG_SQE_ELEM(4, __s32, fd);
7236 BUILD_BUG_SQE_ELEM(8, __u64, off);
7237 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
7238 BUILD_BUG_SQE_ELEM(16, __u64, addr);
7239 BUILD_BUG_SQE_ELEM(24, __u32, len);
7240 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
7241 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
7242 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
7243 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
7244 BUILD_BUG_SQE_ELEM(28, __u16, poll_events);
7245 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
7246 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
7247 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
7248 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
7249 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
7250 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
7251 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
7252 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
7253 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
7254 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
7255 BUILD_BUG_SQE_ELEM(42, __u16, personality);
7256
Jens Axboed3656342019-12-18 09:50:26 -07007257 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007258 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
7259 return 0;
7260};
7261__initcall(io_uring_init);