blob: e412a1761d9342c1b809819cb51273fda97b1687 [file] [log] [blame]
Jens Axboe2b188cc2019-01-07 10:46:33 -07001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Shared application/kernel submission and completion ring pairs, for
4 * supporting fast/efficient IO.
5 *
6 * A note on the read/write ordering memory barriers that are matched between
Stefan Bühler1e84b972019-04-24 23:54:16 +02007 * the application and kernel side.
8 *
9 * After the application reads the CQ ring tail, it must use an
10 * appropriate smp_rmb() to pair with the smp_wmb() the kernel uses
11 * before writing the tail (using smp_load_acquire to read the tail will
12 * do). It also needs a smp_mb() before updating CQ head (ordering the
13 * entry load(s) with the head store), pairing with an implicit barrier
14 * through a control-dependency in io_get_cqring (smp_store_release to
15 * store head will do). Failure to do so could lead to reading invalid
16 * CQ entries.
17 *
18 * Likewise, the application must use an appropriate smp_wmb() before
19 * writing the SQ tail (ordering SQ entry stores with the tail store),
20 * which pairs with smp_load_acquire in io_get_sqring (smp_store_release
21 * to store the tail will do). And it needs a barrier ordering the SQ
22 * head load before writing new SQ entries (smp_load_acquire to read
23 * head will do).
24 *
25 * When using the SQ poll thread (IORING_SETUP_SQPOLL), the application
26 * needs to check the SQ flags for IORING_SQ_NEED_WAKEUP *after*
27 * updating the SQ tail; a full memory barrier smp_mb() is needed
28 * between.
Jens Axboe2b188cc2019-01-07 10:46:33 -070029 *
30 * Also see the examples in the liburing library:
31 *
32 * git://git.kernel.dk/liburing
33 *
34 * io_uring also uses READ/WRITE_ONCE() for _any_ store or load that happens
35 * from data shared between the kernel and application. This is done both
36 * for ordering purposes, but also to ensure that once a value is loaded from
37 * data that the application could potentially modify, it remains stable.
38 *
39 * Copyright (C) 2018-2019 Jens Axboe
Christoph Hellwigc992fe22019-01-11 09:43:02 -070040 * Copyright (c) 2018-2019 Christoph Hellwig
Jens Axboe2b188cc2019-01-07 10:46:33 -070041 */
42#include <linux/kernel.h>
43#include <linux/init.h>
44#include <linux/errno.h>
45#include <linux/syscalls.h>
46#include <linux/compat.h>
47#include <linux/refcount.h>
48#include <linux/uio.h>
Pavel Begunkov6b47ee62020-01-18 20:22:41 +030049#include <linux/bits.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070050
51#include <linux/sched/signal.h>
52#include <linux/fs.h>
53#include <linux/file.h>
54#include <linux/fdtable.h>
55#include <linux/mm.h>
56#include <linux/mman.h>
57#include <linux/mmu_context.h>
58#include <linux/percpu.h>
59#include <linux/slab.h>
Jens Axboe6c271ce2019-01-10 11:22:30 -070060#include <linux/kthread.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070061#include <linux/blkdev.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070062#include <linux/bvec.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070063#include <linux/net.h>
64#include <net/sock.h>
65#include <net/af_unix.h>
Jens Axboe6b063142019-01-10 22:13:58 -070066#include <net/scm.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070067#include <linux/anon_inodes.h>
68#include <linux/sched/mm.h>
69#include <linux/uaccess.h>
70#include <linux/nospec.h>
Jens Axboeedafcce2019-01-09 09:16:05 -070071#include <linux/sizes.h>
72#include <linux/hugetlb.h>
Jens Axboeaa4c3962019-11-29 10:14:00 -070073#include <linux/highmem.h>
Jens Axboe15b71ab2019-12-11 11:20:36 -070074#include <linux/namei.h>
75#include <linux/fsnotify.h>
Jens Axboe4840e412019-12-25 22:03:45 -070076#include <linux/fadvise.h>
Jens Axboe3e4827b2020-01-08 15:18:09 -070077#include <linux/eventpoll.h>
Jens Axboeff002b32020-02-07 16:05:21 -070078#include <linux/fs_struct.h>
Jens Axboe2b188cc2019-01-07 10:46:33 -070079
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +020080#define CREATE_TRACE_POINTS
81#include <trace/events/io_uring.h>
82
Jens Axboe2b188cc2019-01-07 10:46:33 -070083#include <uapi/linux/io_uring.h>
84
85#include "internal.h"
Jens Axboe561fb042019-10-24 07:25:42 -060086#include "io-wq.h"
Jens Axboe2b188cc2019-01-07 10:46:33 -070087
Daniel Xu5277dea2019-09-14 14:23:45 -070088#define IORING_MAX_ENTRIES 32768
Jens Axboe33a107f2019-10-04 12:10:03 -060089#define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES)
Jens Axboe65e19f52019-10-26 07:20:21 -060090
91/*
92 * Shift of 9 is 512 entries, or exactly one page on 64-bit archs
93 */
94#define IORING_FILE_TABLE_SHIFT 9
95#define IORING_MAX_FILES_TABLE (1U << IORING_FILE_TABLE_SHIFT)
96#define IORING_FILE_TABLE_MASK (IORING_MAX_FILES_TABLE - 1)
97#define IORING_MAX_FIXED_FILES (64 * IORING_MAX_FILES_TABLE)
Jens Axboe2b188cc2019-01-07 10:46:33 -070098
99struct io_uring {
100 u32 head ____cacheline_aligned_in_smp;
101 u32 tail ____cacheline_aligned_in_smp;
102};
103
Stefan Bühler1e84b972019-04-24 23:54:16 +0200104/*
Hristo Venev75b28af2019-08-26 17:23:46 +0000105 * This data is shared with the application through the mmap at offsets
106 * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200107 *
108 * The offsets to the member fields are published through struct
109 * io_sqring_offsets when calling io_uring_setup.
110 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000111struct io_rings {
Stefan Bühler1e84b972019-04-24 23:54:16 +0200112 /*
113 * Head and tail offsets into the ring; the offsets need to be
114 * masked to get valid indices.
115 *
Hristo Venev75b28af2019-08-26 17:23:46 +0000116 * The kernel controls head of the sq ring and the tail of the cq ring,
117 * and the application controls tail of the sq ring and the head of the
118 * cq ring.
Stefan Bühler1e84b972019-04-24 23:54:16 +0200119 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000120 struct io_uring sq, cq;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200121 /*
Hristo Venev75b28af2019-08-26 17:23:46 +0000122 * Bitmasks to apply to head and tail offsets (constant, equals
Stefan Bühler1e84b972019-04-24 23:54:16 +0200123 * ring_entries - 1)
124 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000125 u32 sq_ring_mask, cq_ring_mask;
126 /* Ring sizes (constant, power of 2) */
127 u32 sq_ring_entries, cq_ring_entries;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200128 /*
129 * Number of invalid entries dropped by the kernel due to
130 * invalid index stored in array
131 *
132 * Written by the kernel, shouldn't be modified by the
133 * application (i.e. get number of "new events" by comparing to
134 * cached value).
135 *
136 * After a new SQ head value was read by the application this
137 * counter includes all submissions that were dropped reaching
138 * the new SQ head (and possibly more).
139 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000140 u32 sq_dropped;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200141 /*
142 * Runtime flags
143 *
144 * Written by the kernel, shouldn't be modified by the
145 * application.
146 *
147 * The application needs a full memory barrier before checking
148 * for IORING_SQ_NEED_WAKEUP after updating the sq tail.
149 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000150 u32 sq_flags;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200151 /*
152 * Number of completion events lost because the queue was full;
153 * this should be avoided by the application by making sure
LimingWu0b4295b2019-12-05 20:18:18 +0800154 * there are not more requests pending than there is space in
Stefan Bühler1e84b972019-04-24 23:54:16 +0200155 * the completion queue.
156 *
157 * Written by the kernel, shouldn't be modified by the
158 * application (i.e. get number of "new events" by comparing to
159 * cached value).
160 *
161 * As completion events come in out of order this counter is not
162 * ordered with any other data.
163 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000164 u32 cq_overflow;
Stefan Bühler1e84b972019-04-24 23:54:16 +0200165 /*
166 * Ring buffer of completion events.
167 *
168 * The kernel writes completion events fresh every time they are
169 * produced, so the application is allowed to modify pending
170 * entries.
171 */
Hristo Venev75b28af2019-08-26 17:23:46 +0000172 struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700173};
174
Jens Axboeedafcce2019-01-09 09:16:05 -0700175struct io_mapped_ubuf {
176 u64 ubuf;
177 size_t len;
178 struct bio_vec *bvec;
179 unsigned int nr_bvecs;
180};
181
Jens Axboe65e19f52019-10-26 07:20:21 -0600182struct fixed_file_table {
183 struct file **files;
Jens Axboe31b51512019-01-18 22:56:34 -0700184};
185
Jens Axboe05f3fb32019-12-09 11:22:50 -0700186struct fixed_file_data {
187 struct fixed_file_table *table;
188 struct io_ring_ctx *ctx;
189
190 struct percpu_ref refs;
191 struct llist_head put_llist;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700192 struct work_struct ref_work;
193 struct completion done;
194};
195
Jens Axboe2b188cc2019-01-07 10:46:33 -0700196struct io_ring_ctx {
197 struct {
198 struct percpu_ref refs;
199 } ____cacheline_aligned_in_smp;
200
201 struct {
202 unsigned int flags;
Randy Dunlape1d85332020-02-05 20:57:10 -0800203 unsigned int compat: 1;
204 unsigned int account_mem: 1;
205 unsigned int cq_overflow_flushed: 1;
206 unsigned int drain_next: 1;
207 unsigned int eventfd_async: 1;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700208
Hristo Venev75b28af2019-08-26 17:23:46 +0000209 /*
210 * Ring buffer of indices into array of io_uring_sqe, which is
211 * mmapped by the application using the IORING_OFF_SQES offset.
212 *
213 * This indirection could e.g. be used to assign fixed
214 * io_uring_sqe entries to operations and only submit them to
215 * the queue when needed.
216 *
217 * The kernel modifies neither the indices array nor the entries
218 * array.
219 */
220 u32 *sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700221 unsigned cached_sq_head;
222 unsigned sq_entries;
223 unsigned sq_mask;
Jens Axboe6c271ce2019-01-10 11:22:30 -0700224 unsigned sq_thread_idle;
Jens Axboe498ccd92019-10-25 10:04:25 -0600225 unsigned cached_sq_dropped;
Jens Axboe206aefd2019-11-07 18:27:42 -0700226 atomic_t cached_cq_overflow;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700227 unsigned long sq_check_overflow;
Jens Axboede0617e2019-04-06 21:51:27 -0600228
229 struct list_head defer_list;
Jens Axboe5262f562019-09-17 12:26:57 -0600230 struct list_head timeout_list;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700231 struct list_head cq_overflow_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700232
Jens Axboefcb323c2019-10-24 12:39:47 -0600233 wait_queue_head_t inflight_wait;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700234 struct io_uring_sqe *sq_sqes;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700235 } ____cacheline_aligned_in_smp;
236
Hristo Venev75b28af2019-08-26 17:23:46 +0000237 struct io_rings *rings;
238
Jens Axboe2b188cc2019-01-07 10:46:33 -0700239 /* IO offload */
Jens Axboe561fb042019-10-24 07:25:42 -0600240 struct io_wq *io_wq;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700241 struct task_struct *sqo_thread; /* if using sq thread polling */
242 struct mm_struct *sqo_mm;
243 wait_queue_head_t sqo_wait;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700244
Jens Axboe6b063142019-01-10 22:13:58 -0700245 /*
246 * If used, fixed file set. Writers must ensure that ->refs is dead,
247 * readers must ensure that ->refs is alive as long as the file* is
248 * used. Only updated through io_uring_register(2).
249 */
Jens Axboe05f3fb32019-12-09 11:22:50 -0700250 struct fixed_file_data *file_data;
Jens Axboe6b063142019-01-10 22:13:58 -0700251 unsigned nr_user_files;
Pavel Begunkovb14cca02020-01-17 04:45:59 +0300252 int ring_fd;
253 struct file *ring_file;
Jens Axboe6b063142019-01-10 22:13:58 -0700254
Jens Axboeedafcce2019-01-09 09:16:05 -0700255 /* if used, fixed mapped user buffers */
256 unsigned nr_user_bufs;
257 struct io_mapped_ubuf *user_bufs;
258
Jens Axboe2b188cc2019-01-07 10:46:33 -0700259 struct user_struct *user;
260
Jens Axboe0b8c0ec2019-12-02 08:50:00 -0700261 const struct cred *creds;
Jens Axboe181e4482019-11-25 08:52:30 -0700262
Jens Axboe206aefd2019-11-07 18:27:42 -0700263 /* 0 is for ctx quiesce/reinit/free, 1 is for sqo_thread started */
264 struct completion *completions;
265
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700266 /* if all else fails... */
267 struct io_kiocb *fallback_req;
268
Jens Axboe206aefd2019-11-07 18:27:42 -0700269#if defined(CONFIG_UNIX)
270 struct socket *ring_sock;
271#endif
272
Jens Axboe071698e2020-01-28 10:04:42 -0700273 struct idr personality_idr;
274
Jens Axboe206aefd2019-11-07 18:27:42 -0700275 struct {
276 unsigned cached_cq_tail;
277 unsigned cq_entries;
278 unsigned cq_mask;
279 atomic_t cq_timeouts;
Jens Axboead3eb2c2019-12-18 17:12:20 -0700280 unsigned long cq_check_overflow;
Jens Axboe206aefd2019-11-07 18:27:42 -0700281 struct wait_queue_head cq_wait;
282 struct fasync_struct *cq_fasync;
283 struct eventfd_ctx *cq_ev_fd;
284 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700285
286 struct {
287 struct mutex uring_lock;
288 wait_queue_head_t wait;
289 } ____cacheline_aligned_in_smp;
290
291 struct {
292 spinlock_t completion_lock;
Jens Axboee94f1412019-12-19 12:06:02 -0700293 struct llist_head poll_llist;
294
Jens Axboedef596e2019-01-09 08:59:42 -0700295 /*
296 * ->poll_list is protected by the ctx->uring_lock for
297 * io_uring instances that don't use IORING_SETUP_SQPOLL.
298 * For SQPOLL, only the single threaded io_sq_thread() will
299 * manipulate the list, hence no extra locking is needed there.
300 */
301 struct list_head poll_list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700302 struct hlist_head *cancel_hash;
303 unsigned cancel_hash_bits;
Jens Axboee94f1412019-12-19 12:06:02 -0700304 bool poll_multi_file;
Jens Axboefcb323c2019-10-24 12:39:47 -0600305
306 spinlock_t inflight_lock;
307 struct list_head inflight_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700308 } ____cacheline_aligned_in_smp;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700309};
310
Jens Axboe09bb8392019-03-13 12:39:28 -0600311/*
312 * First field must be the file pointer in all the
313 * iocb unions! See also 'struct kiocb' in <linux/fs.h>
314 */
Jens Axboe221c5eb2019-01-17 09:41:58 -0700315struct io_poll_iocb {
316 struct file *file;
Jens Axboe0969e782019-12-17 18:40:57 -0700317 union {
318 struct wait_queue_head *head;
319 u64 addr;
320 };
Jens Axboe221c5eb2019-01-17 09:41:58 -0700321 __poll_t events;
Jens Axboe8c838782019-03-12 15:48:16 -0600322 bool done;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700323 bool canceled;
Jens Axboe392edb42019-12-09 17:52:20 -0700324 struct wait_queue_entry wait;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700325};
326
Jens Axboeb5dba592019-12-11 14:02:38 -0700327struct io_close {
328 struct file *file;
329 struct file *put_file;
330 int fd;
331};
332
Jens Axboead8a48a2019-11-15 08:49:11 -0700333struct io_timeout_data {
334 struct io_kiocb *req;
335 struct hrtimer timer;
336 struct timespec64 ts;
337 enum hrtimer_mode mode;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +0300338 u32 seq_offset;
Jens Axboead8a48a2019-11-15 08:49:11 -0700339};
340
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700341struct io_accept {
342 struct file *file;
343 struct sockaddr __user *addr;
344 int __user *addr_len;
345 int flags;
346};
347
348struct io_sync {
349 struct file *file;
350 loff_t len;
351 loff_t off;
352 int flags;
Jens Axboed63d1b52019-12-10 10:38:56 -0700353 int mode;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700354};
355
Jens Axboefbf23842019-12-17 18:45:56 -0700356struct io_cancel {
357 struct file *file;
358 u64 addr;
359};
360
Jens Axboeb29472e2019-12-17 18:50:29 -0700361struct io_timeout {
362 struct file *file;
363 u64 addr;
364 int flags;
Jens Axboe26a61672019-12-20 09:02:01 -0700365 unsigned count;
Jens Axboeb29472e2019-12-17 18:50:29 -0700366};
367
Jens Axboe9adbd452019-12-20 08:45:55 -0700368struct io_rw {
369 /* NOTE: kiocb has the file as the first member, so don't do it here */
370 struct kiocb kiocb;
371 u64 addr;
372 u64 len;
373};
374
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700375struct io_connect {
376 struct file *file;
377 struct sockaddr __user *addr;
378 int addr_len;
379};
380
Jens Axboee47293f2019-12-20 08:58:21 -0700381struct io_sr_msg {
382 struct file *file;
Jens Axboefddafac2020-01-04 20:19:44 -0700383 union {
384 struct user_msghdr __user *msg;
385 void __user *buf;
386 };
Jens Axboee47293f2019-12-20 08:58:21 -0700387 int msg_flags;
Jens Axboefddafac2020-01-04 20:19:44 -0700388 size_t len;
Jens Axboee47293f2019-12-20 08:58:21 -0700389};
390
Jens Axboe15b71ab2019-12-11 11:20:36 -0700391struct io_open {
392 struct file *file;
393 int dfd;
Jens Axboeeddc7ef2019-12-13 21:18:10 -0700394 union {
Jens Axboeeddc7ef2019-12-13 21:18:10 -0700395 unsigned mask;
396 };
Jens Axboe15b71ab2019-12-11 11:20:36 -0700397 struct filename *filename;
Jens Axboeeddc7ef2019-12-13 21:18:10 -0700398 struct statx __user *buffer;
Jens Axboec12cedf2020-01-08 17:41:21 -0700399 struct open_how how;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700400};
401
Jens Axboe05f3fb32019-12-09 11:22:50 -0700402struct io_files_update {
403 struct file *file;
404 u64 arg;
405 u32 nr_args;
406 u32 offset;
407};
408
Jens Axboe4840e412019-12-25 22:03:45 -0700409struct io_fadvise {
410 struct file *file;
411 u64 offset;
412 u32 len;
413 u32 advice;
414};
415
Jens Axboec1ca7572019-12-25 22:18:28 -0700416struct io_madvise {
417 struct file *file;
418 u64 addr;
419 u32 len;
420 u32 advice;
421};
422
Jens Axboe3e4827b2020-01-08 15:18:09 -0700423struct io_epoll {
424 struct file *file;
425 int epfd;
426 int op;
427 int fd;
428 struct epoll_event event;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700429};
430
Jens Axboef499a022019-12-02 16:28:46 -0700431struct io_async_connect {
432 struct sockaddr_storage address;
433};
434
Jens Axboe03b12302019-12-02 18:50:25 -0700435struct io_async_msghdr {
436 struct iovec fast_iov[UIO_FASTIOV];
437 struct iovec *iov;
438 struct sockaddr __user *uaddr;
439 struct msghdr msg;
Jens Axboeb5379162020-02-09 11:29:15 -0700440 struct sockaddr_storage addr;
Jens Axboe03b12302019-12-02 18:50:25 -0700441};
442
Jens Axboef67676d2019-12-02 11:03:47 -0700443struct io_async_rw {
444 struct iovec fast_iov[UIO_FASTIOV];
445 struct iovec *iov;
446 ssize_t nr_segs;
447 ssize_t size;
448};
449
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700450struct io_async_ctx {
Jens Axboef67676d2019-12-02 11:03:47 -0700451 union {
452 struct io_async_rw rw;
Jens Axboe03b12302019-12-02 18:50:25 -0700453 struct io_async_msghdr msg;
Jens Axboef499a022019-12-02 16:28:46 -0700454 struct io_async_connect connect;
Jens Axboe2d283902019-12-04 11:08:05 -0700455 struct io_timeout_data timeout;
Jens Axboef67676d2019-12-02 11:03:47 -0700456 };
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700457};
458
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300459enum {
460 REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT,
461 REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT,
462 REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT,
463 REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT,
464 REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT,
465
466 REQ_F_LINK_NEXT_BIT,
467 REQ_F_FAIL_LINK_BIT,
468 REQ_F_INFLIGHT_BIT,
469 REQ_F_CUR_POS_BIT,
470 REQ_F_NOWAIT_BIT,
471 REQ_F_IOPOLL_COMPLETED_BIT,
472 REQ_F_LINK_TIMEOUT_BIT,
473 REQ_F_TIMEOUT_BIT,
474 REQ_F_ISREG_BIT,
475 REQ_F_MUST_PUNT_BIT,
476 REQ_F_TIMEOUT_NOSEQ_BIT,
477 REQ_F_COMP_LOCKED_BIT,
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300478 REQ_F_NEED_CLEANUP_BIT,
Jens Axboe2ca10252020-02-13 17:17:35 -0700479 REQ_F_OVERFLOW_BIT,
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300480};
481
482enum {
483 /* ctx owns file */
484 REQ_F_FIXED_FILE = BIT(REQ_F_FIXED_FILE_BIT),
485 /* drain existing IO first */
486 REQ_F_IO_DRAIN = BIT(REQ_F_IO_DRAIN_BIT),
487 /* linked sqes */
488 REQ_F_LINK = BIT(REQ_F_LINK_BIT),
489 /* doesn't sever on completion < 0 */
490 REQ_F_HARDLINK = BIT(REQ_F_HARDLINK_BIT),
491 /* IOSQE_ASYNC */
492 REQ_F_FORCE_ASYNC = BIT(REQ_F_FORCE_ASYNC_BIT),
493
494 /* already grabbed next link */
495 REQ_F_LINK_NEXT = BIT(REQ_F_LINK_NEXT_BIT),
496 /* fail rest of links */
497 REQ_F_FAIL_LINK = BIT(REQ_F_FAIL_LINK_BIT),
498 /* on inflight list */
499 REQ_F_INFLIGHT = BIT(REQ_F_INFLIGHT_BIT),
500 /* read/write uses file position */
501 REQ_F_CUR_POS = BIT(REQ_F_CUR_POS_BIT),
502 /* must not punt to workers */
503 REQ_F_NOWAIT = BIT(REQ_F_NOWAIT_BIT),
504 /* polled IO has completed */
505 REQ_F_IOPOLL_COMPLETED = BIT(REQ_F_IOPOLL_COMPLETED_BIT),
506 /* has linked timeout */
507 REQ_F_LINK_TIMEOUT = BIT(REQ_F_LINK_TIMEOUT_BIT),
508 /* timeout request */
509 REQ_F_TIMEOUT = BIT(REQ_F_TIMEOUT_BIT),
510 /* regular file */
511 REQ_F_ISREG = BIT(REQ_F_ISREG_BIT),
512 /* must be punted even for NONBLOCK */
513 REQ_F_MUST_PUNT = BIT(REQ_F_MUST_PUNT_BIT),
514 /* no timeout sequence */
515 REQ_F_TIMEOUT_NOSEQ = BIT(REQ_F_TIMEOUT_NOSEQ_BIT),
516 /* completion under lock */
517 REQ_F_COMP_LOCKED = BIT(REQ_F_COMP_LOCKED_BIT),
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300518 /* needs cleanup */
519 REQ_F_NEED_CLEANUP = BIT(REQ_F_NEED_CLEANUP_BIT),
Jens Axboe2ca10252020-02-13 17:17:35 -0700520 /* in overflow list */
521 REQ_F_OVERFLOW = BIT(REQ_F_OVERFLOW_BIT),
Pavel Begunkov6b47ee62020-01-18 20:22:41 +0300522};
523
Jens Axboe09bb8392019-03-13 12:39:28 -0600524/*
525 * NOTE! Each of the iocb union members has the file pointer
526 * as the first entry in their struct definition. So you can
527 * access the file pointer through any of the sub-structs,
528 * or directly as just 'ki_filp' in this struct.
529 */
Jens Axboe2b188cc2019-01-07 10:46:33 -0700530struct io_kiocb {
Jens Axboe221c5eb2019-01-17 09:41:58 -0700531 union {
Jens Axboe09bb8392019-03-13 12:39:28 -0600532 struct file *file;
Jens Axboe9adbd452019-12-20 08:45:55 -0700533 struct io_rw rw;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700534 struct io_poll_iocb poll;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -0700535 struct io_accept accept;
536 struct io_sync sync;
Jens Axboefbf23842019-12-17 18:45:56 -0700537 struct io_cancel cancel;
Jens Axboeb29472e2019-12-17 18:50:29 -0700538 struct io_timeout timeout;
Jens Axboe3fbb51c2019-12-20 08:51:52 -0700539 struct io_connect connect;
Jens Axboee47293f2019-12-20 08:58:21 -0700540 struct io_sr_msg sr_msg;
Jens Axboe15b71ab2019-12-11 11:20:36 -0700541 struct io_open open;
Jens Axboeb5dba592019-12-11 14:02:38 -0700542 struct io_close close;
Jens Axboe05f3fb32019-12-09 11:22:50 -0700543 struct io_files_update files_update;
Jens Axboe4840e412019-12-25 22:03:45 -0700544 struct io_fadvise fadvise;
Jens Axboec1ca7572019-12-25 22:18:28 -0700545 struct io_madvise madvise;
Jens Axboe3e4827b2020-01-08 15:18:09 -0700546 struct io_epoll epoll;
Jens Axboe221c5eb2019-01-17 09:41:58 -0700547 };
Jens Axboe2b188cc2019-01-07 10:46:33 -0700548
Jens Axboe1a6b74f2019-12-02 10:33:15 -0700549 struct io_async_ctx *io;
Pavel Begunkovb14cca02020-01-17 04:45:59 +0300550 /*
551 * llist_node is only used for poll deferred completions
552 */
553 struct llist_node llist_node;
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +0300554 bool in_async;
555 bool needs_fixed_file;
Jens Axboed625c6e2019-12-17 19:53:05 -0700556 u8 opcode;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700557
558 struct io_ring_ctx *ctx;
Jens Axboeeac406c2019-11-14 12:09:58 -0700559 union {
560 struct list_head list;
Jens Axboe78076bb2019-12-04 19:56:40 -0700561 struct hlist_node hash_node;
Jens Axboeeac406c2019-11-14 12:09:58 -0700562 };
Jens Axboe9e645e112019-05-10 16:07:28 -0600563 struct list_head link_list;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700564 unsigned int flags;
Jens Axboec16361c2019-01-17 08:39:48 -0700565 refcount_t refs;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700566 u64 user_data;
Jens Axboe9e645e112019-05-10 16:07:28 -0600567 u32 result;
Jens Axboede0617e2019-04-06 21:51:27 -0600568 u32 sequence;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700569
Jens Axboefcb323c2019-10-24 12:39:47 -0600570 struct list_head inflight_entry;
571
Jens Axboe561fb042019-10-24 07:25:42 -0600572 struct io_wq_work work;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700573};
574
575#define IO_PLUG_THRESHOLD 2
Jens Axboedef596e2019-01-09 08:59:42 -0700576#define IO_IOPOLL_BATCH 8
Jens Axboe2b188cc2019-01-07 10:46:33 -0700577
Jens Axboe9a56a232019-01-09 09:06:50 -0700578struct io_submit_state {
579 struct blk_plug plug;
580
581 /*
Jens Axboe2579f912019-01-09 09:10:43 -0700582 * io_kiocb alloc cache
583 */
584 void *reqs[IO_IOPOLL_BATCH];
Pavel Begunkov6c8a3132020-02-01 03:58:00 +0300585 unsigned int free_reqs;
Jens Axboe2579f912019-01-09 09:10:43 -0700586
587 /*
Jens Axboe9a56a232019-01-09 09:06:50 -0700588 * File reference cache
589 */
590 struct file *file;
591 unsigned int fd;
592 unsigned int has_refs;
593 unsigned int used_refs;
594 unsigned int ios_left;
595};
596
Jens Axboed3656342019-12-18 09:50:26 -0700597struct io_op_def {
598 /* needs req->io allocated for deferral/async */
599 unsigned async_ctx : 1;
600 /* needs current->mm setup, does mm access */
601 unsigned needs_mm : 1;
602 /* needs req->file assigned */
603 unsigned needs_file : 1;
604 /* needs req->file assigned IFF fd is >= 0 */
605 unsigned fd_non_neg : 1;
606 /* hash wq insertion if file is a regular file */
607 unsigned hash_reg_file : 1;
608 /* unbound wq insertion if file is a non-regular file */
609 unsigned unbound_nonreg_file : 1;
Jens Axboe66f4af92020-01-16 15:36:52 -0700610 /* opcode is not supported by this kernel */
611 unsigned not_supported : 1;
Jens Axboef86cd202020-01-29 13:46:44 -0700612 /* needs file table */
613 unsigned file_table : 1;
Jens Axboeff002b32020-02-07 16:05:21 -0700614 /* needs ->fs */
615 unsigned needs_fs : 1;
Jens Axboed3656342019-12-18 09:50:26 -0700616};
617
618static const struct io_op_def io_op_defs[] = {
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300619 [IORING_OP_NOP] = {},
620 [IORING_OP_READV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700621 .async_ctx = 1,
622 .needs_mm = 1,
623 .needs_file = 1,
624 .unbound_nonreg_file = 1,
625 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300626 [IORING_OP_WRITEV] = {
Jens Axboed3656342019-12-18 09:50:26 -0700627 .async_ctx = 1,
628 .needs_mm = 1,
629 .needs_file = 1,
630 .hash_reg_file = 1,
631 .unbound_nonreg_file = 1,
632 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300633 [IORING_OP_FSYNC] = {
Jens Axboed3656342019-12-18 09:50:26 -0700634 .needs_file = 1,
635 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300636 [IORING_OP_READ_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700637 .needs_file = 1,
638 .unbound_nonreg_file = 1,
639 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300640 [IORING_OP_WRITE_FIXED] = {
Jens Axboed3656342019-12-18 09:50:26 -0700641 .needs_file = 1,
642 .hash_reg_file = 1,
643 .unbound_nonreg_file = 1,
644 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300645 [IORING_OP_POLL_ADD] = {
Jens Axboed3656342019-12-18 09:50:26 -0700646 .needs_file = 1,
647 .unbound_nonreg_file = 1,
648 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300649 [IORING_OP_POLL_REMOVE] = {},
650 [IORING_OP_SYNC_FILE_RANGE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700651 .needs_file = 1,
652 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300653 [IORING_OP_SENDMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700654 .async_ctx = 1,
655 .needs_mm = 1,
656 .needs_file = 1,
657 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700658 .needs_fs = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700659 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300660 [IORING_OP_RECVMSG] = {
Jens Axboed3656342019-12-18 09:50:26 -0700661 .async_ctx = 1,
662 .needs_mm = 1,
663 .needs_file = 1,
664 .unbound_nonreg_file = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700665 .needs_fs = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700666 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300667 [IORING_OP_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700668 .async_ctx = 1,
669 .needs_mm = 1,
670 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300671 [IORING_OP_TIMEOUT_REMOVE] = {},
672 [IORING_OP_ACCEPT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700673 .needs_mm = 1,
674 .needs_file = 1,
675 .unbound_nonreg_file = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700676 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700677 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300678 [IORING_OP_ASYNC_CANCEL] = {},
679 [IORING_OP_LINK_TIMEOUT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700680 .async_ctx = 1,
681 .needs_mm = 1,
682 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300683 [IORING_OP_CONNECT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700684 .async_ctx = 1,
685 .needs_mm = 1,
686 .needs_file = 1,
687 .unbound_nonreg_file = 1,
688 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300689 [IORING_OP_FALLOCATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700690 .needs_file = 1,
691 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300692 [IORING_OP_OPENAT] = {
Jens Axboed3656342019-12-18 09:50:26 -0700693 .needs_file = 1,
694 .fd_non_neg = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700695 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700696 .needs_fs = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700697 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300698 [IORING_OP_CLOSE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700699 .needs_file = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700700 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700701 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300702 [IORING_OP_FILES_UPDATE] = {
Jens Axboed3656342019-12-18 09:50:26 -0700703 .needs_mm = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700704 .file_table = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700705 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300706 [IORING_OP_STATX] = {
Jens Axboed3656342019-12-18 09:50:26 -0700707 .needs_mm = 1,
708 .needs_file = 1,
709 .fd_non_neg = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700710 .needs_fs = 1,
Jens Axboed3656342019-12-18 09:50:26 -0700711 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300712 [IORING_OP_READ] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700713 .needs_mm = 1,
714 .needs_file = 1,
715 .unbound_nonreg_file = 1,
716 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300717 [IORING_OP_WRITE] = {
Jens Axboe3a6820f2019-12-22 15:19:35 -0700718 .needs_mm = 1,
719 .needs_file = 1,
720 .unbound_nonreg_file = 1,
721 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300722 [IORING_OP_FADVISE] = {
Jens Axboe4840e412019-12-25 22:03:45 -0700723 .needs_file = 1,
724 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300725 [IORING_OP_MADVISE] = {
Jens Axboec1ca7572019-12-25 22:18:28 -0700726 .needs_mm = 1,
727 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300728 [IORING_OP_SEND] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700729 .needs_mm = 1,
730 .needs_file = 1,
731 .unbound_nonreg_file = 1,
732 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300733 [IORING_OP_RECV] = {
Jens Axboefddafac2020-01-04 20:19:44 -0700734 .needs_mm = 1,
735 .needs_file = 1,
736 .unbound_nonreg_file = 1,
737 },
Pavel Begunkov0463b6c2020-01-18 21:35:38 +0300738 [IORING_OP_OPENAT2] = {
Jens Axboecebdb982020-01-08 17:59:24 -0700739 .needs_file = 1,
740 .fd_non_neg = 1,
Jens Axboef86cd202020-01-29 13:46:44 -0700741 .file_table = 1,
Jens Axboeff002b32020-02-07 16:05:21 -0700742 .needs_fs = 1,
Jens Axboecebdb982020-01-08 17:59:24 -0700743 },
Jens Axboe3e4827b2020-01-08 15:18:09 -0700744 [IORING_OP_EPOLL_CTL] = {
745 .unbound_nonreg_file = 1,
746 .file_table = 1,
747 },
Jens Axboed3656342019-12-18 09:50:26 -0700748};
749
Jens Axboe561fb042019-10-24 07:25:42 -0600750static void io_wq_submit_work(struct io_wq_work **workptr);
Jens Axboe78e19bb2019-11-06 15:21:34 -0700751static void io_cqring_fill_event(struct io_kiocb *req, long res);
Jackie Liuec9c02a2019-11-08 23:50:36 +0800752static void io_put_req(struct io_kiocb *req);
Jens Axboe978db572019-11-14 22:39:04 -0700753static void __io_double_put_req(struct io_kiocb *req);
Jens Axboe94ae5e72019-11-14 19:39:52 -0700754static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req);
755static void io_queue_linked_timeout(struct io_kiocb *req);
Jens Axboe05f3fb32019-12-09 11:22:50 -0700756static int __io_sqe_files_update(struct io_ring_ctx *ctx,
757 struct io_uring_files_update *ip,
758 unsigned nr_args);
Jens Axboef86cd202020-01-29 13:46:44 -0700759static int io_grab_files(struct io_kiocb *req);
Jens Axboe2faf8522020-02-04 19:54:55 -0700760static void io_ring_file_ref_flush(struct fixed_file_data *data);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +0300761static void io_cleanup_req(struct io_kiocb *req);
Jens Axboede0617e2019-04-06 21:51:27 -0600762
Jens Axboe2b188cc2019-01-07 10:46:33 -0700763static struct kmem_cache *req_cachep;
764
765static const struct file_operations io_uring_fops;
766
767struct sock *io_uring_get_socket(struct file *file)
768{
769#if defined(CONFIG_UNIX)
770 if (file->f_op == &io_uring_fops) {
771 struct io_ring_ctx *ctx = file->private_data;
772
773 return ctx->ring_sock->sk;
774 }
775#endif
776 return NULL;
777}
778EXPORT_SYMBOL(io_uring_get_socket);
779
780static void io_ring_ctx_ref_free(struct percpu_ref *ref)
781{
782 struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs);
783
Jens Axboe206aefd2019-11-07 18:27:42 -0700784 complete(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700785}
786
787static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
788{
789 struct io_ring_ctx *ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -0700790 int hash_bits;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700791
792 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
793 if (!ctx)
794 return NULL;
795
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700796 ctx->fallback_req = kmem_cache_alloc(req_cachep, GFP_KERNEL);
797 if (!ctx->fallback_req)
798 goto err;
799
Jens Axboe206aefd2019-11-07 18:27:42 -0700800 ctx->completions = kmalloc(2 * sizeof(struct completion), GFP_KERNEL);
801 if (!ctx->completions)
802 goto err;
803
Jens Axboe78076bb2019-12-04 19:56:40 -0700804 /*
805 * Use 5 bits less than the max cq entries, that should give us around
806 * 32 entries per hash list if totally full and uniformly spread.
807 */
808 hash_bits = ilog2(p->cq_entries);
809 hash_bits -= 5;
810 if (hash_bits <= 0)
811 hash_bits = 1;
812 ctx->cancel_hash_bits = hash_bits;
813 ctx->cancel_hash = kmalloc((1U << hash_bits) * sizeof(struct hlist_head),
814 GFP_KERNEL);
815 if (!ctx->cancel_hash)
816 goto err;
817 __hash_init(ctx->cancel_hash, 1U << hash_bits);
818
Roman Gushchin21482892019-05-07 10:01:48 -0700819 if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free,
Jens Axboe206aefd2019-11-07 18:27:42 -0700820 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL))
821 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700822
823 ctx->flags = p->flags;
824 init_waitqueue_head(&ctx->cq_wait);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -0700825 INIT_LIST_HEAD(&ctx->cq_overflow_list);
Jens Axboe206aefd2019-11-07 18:27:42 -0700826 init_completion(&ctx->completions[0]);
827 init_completion(&ctx->completions[1]);
Jens Axboe071698e2020-01-28 10:04:42 -0700828 idr_init(&ctx->personality_idr);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700829 mutex_init(&ctx->uring_lock);
830 init_waitqueue_head(&ctx->wait);
831 spin_lock_init(&ctx->completion_lock);
Jens Axboee94f1412019-12-19 12:06:02 -0700832 init_llist_head(&ctx->poll_llist);
Jens Axboedef596e2019-01-09 08:59:42 -0700833 INIT_LIST_HEAD(&ctx->poll_list);
Jens Axboede0617e2019-04-06 21:51:27 -0600834 INIT_LIST_HEAD(&ctx->defer_list);
Jens Axboe5262f562019-09-17 12:26:57 -0600835 INIT_LIST_HEAD(&ctx->timeout_list);
Jens Axboefcb323c2019-10-24 12:39:47 -0600836 init_waitqueue_head(&ctx->inflight_wait);
837 spin_lock_init(&ctx->inflight_lock);
838 INIT_LIST_HEAD(&ctx->inflight_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700839 return ctx;
Jens Axboe206aefd2019-11-07 18:27:42 -0700840err:
Jens Axboe0ddf92e2019-11-08 08:52:53 -0700841 if (ctx->fallback_req)
842 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe206aefd2019-11-07 18:27:42 -0700843 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -0700844 kfree(ctx->cancel_hash);
Jens Axboe206aefd2019-11-07 18:27:42 -0700845 kfree(ctx);
846 return NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700847}
848
Bob Liu9d858b22019-11-13 18:06:25 +0800849static inline bool __req_need_defer(struct io_kiocb *req)
Jens Axboede0617e2019-04-06 21:51:27 -0600850{
Jackie Liua197f662019-11-08 08:09:12 -0700851 struct io_ring_ctx *ctx = req->ctx;
852
Jens Axboe498ccd92019-10-25 10:04:25 -0600853 return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped
854 + atomic_read(&ctx->cached_cq_overflow);
Jens Axboede0617e2019-04-06 21:51:27 -0600855}
856
Bob Liu9d858b22019-11-13 18:06:25 +0800857static inline bool req_need_defer(struct io_kiocb *req)
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600858{
Pavel Begunkov87987892020-01-18 01:22:30 +0300859 if (unlikely(req->flags & REQ_F_IO_DRAIN))
Bob Liu9d858b22019-11-13 18:06:25 +0800860 return __req_need_defer(req);
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600861
Bob Liu9d858b22019-11-13 18:06:25 +0800862 return false;
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600863}
864
865static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx)
Jens Axboede0617e2019-04-06 21:51:27 -0600866{
867 struct io_kiocb *req;
868
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600869 req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list);
Bob Liu9d858b22019-11-13 18:06:25 +0800870 if (req && !req_need_defer(req)) {
Jens Axboede0617e2019-04-06 21:51:27 -0600871 list_del_init(&req->list);
872 return req;
873 }
874
875 return NULL;
876}
877
Jens Axboe5262f562019-09-17 12:26:57 -0600878static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx)
879{
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600880 struct io_kiocb *req;
881
882 req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list);
Jens Axboe93bd25b2019-11-11 23:34:31 -0700883 if (req) {
884 if (req->flags & REQ_F_TIMEOUT_NOSEQ)
885 return NULL;
Linus Torvaldsfb4b3d32019-11-25 10:40:27 -0800886 if (!__req_need_defer(req)) {
Jens Axboe93bd25b2019-11-11 23:34:31 -0700887 list_del_init(&req->list);
888 return req;
889 }
Jens Axboe7adf4ea2019-10-10 21:42:58 -0600890 }
891
892 return NULL;
Jens Axboe5262f562019-09-17 12:26:57 -0600893}
894
Jens Axboede0617e2019-04-06 21:51:27 -0600895static void __io_commit_cqring(struct io_ring_ctx *ctx)
Jens Axboe2b188cc2019-01-07 10:46:33 -0700896{
Hristo Venev75b28af2019-08-26 17:23:46 +0000897 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -0700898
Pavel Begunkov07910152020-01-17 03:52:46 +0300899 /* order cqe stores with ring update */
900 smp_store_release(&rings->cq.tail, ctx->cached_cq_tail);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700901
Pavel Begunkov07910152020-01-17 03:52:46 +0300902 if (wq_has_sleeper(&ctx->cq_wait)) {
903 wake_up_interruptible(&ctx->cq_wait);
904 kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN);
Jens Axboe2b188cc2019-01-07 10:46:33 -0700905 }
906}
907
Jens Axboecccf0ee2020-01-27 16:34:48 -0700908static inline void io_req_work_grab_env(struct io_kiocb *req,
909 const struct io_op_def *def)
Jens Axboe18d9be12019-09-10 09:13:05 -0600910{
Jens Axboecccf0ee2020-01-27 16:34:48 -0700911 if (!req->work.mm && def->needs_mm) {
912 mmgrab(current->mm);
913 req->work.mm = current->mm;
914 }
915 if (!req->work.creds)
916 req->work.creds = get_current_cred();
Jens Axboeff002b32020-02-07 16:05:21 -0700917 if (!req->work.fs && def->needs_fs) {
918 spin_lock(&current->fs->lock);
919 if (!current->fs->in_exec) {
920 req->work.fs = current->fs;
921 req->work.fs->users++;
922 } else {
923 req->work.flags |= IO_WQ_WORK_CANCEL;
924 }
925 spin_unlock(&current->fs->lock);
926 }
Jens Axboe6ab23142020-02-08 20:23:59 -0700927 if (!req->work.task_pid)
928 req->work.task_pid = task_pid_vnr(current);
Jens Axboecccf0ee2020-01-27 16:34:48 -0700929}
930
931static inline void io_req_work_drop_env(struct io_kiocb *req)
932{
933 if (req->work.mm) {
934 mmdrop(req->work.mm);
935 req->work.mm = NULL;
936 }
937 if (req->work.creds) {
938 put_cred(req->work.creds);
939 req->work.creds = NULL;
940 }
Jens Axboeff002b32020-02-07 16:05:21 -0700941 if (req->work.fs) {
942 struct fs_struct *fs = req->work.fs;
943
944 spin_lock(&req->work.fs->lock);
945 if (--fs->users)
946 fs = NULL;
947 spin_unlock(&req->work.fs->lock);
948 if (fs)
949 free_fs_struct(fs);
950 }
Jens Axboe561fb042019-10-24 07:25:42 -0600951}
952
Jens Axboe94ae5e72019-11-14 19:39:52 -0700953static inline bool io_prep_async_work(struct io_kiocb *req,
954 struct io_kiocb **link)
Jens Axboe561fb042019-10-24 07:25:42 -0600955{
Jens Axboed3656342019-12-18 09:50:26 -0700956 const struct io_op_def *def = &io_op_defs[req->opcode];
Jens Axboe561fb042019-10-24 07:25:42 -0600957 bool do_hashed = false;
Jens Axboe54a91f32019-09-10 09:15:04 -0600958
Jens Axboed3656342019-12-18 09:50:26 -0700959 if (req->flags & REQ_F_ISREG) {
960 if (def->hash_reg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -0700961 do_hashed = true;
Jens Axboed3656342019-12-18 09:50:26 -0700962 } else {
963 if (def->unbound_nonreg_file)
Jens Axboe3529d8c2019-12-19 18:24:38 -0700964 req->work.flags |= IO_WQ_WORK_UNBOUND;
Jens Axboe54a91f32019-09-10 09:15:04 -0600965 }
Jens Axboecccf0ee2020-01-27 16:34:48 -0700966
967 io_req_work_grab_env(req, def);
Jens Axboe54a91f32019-09-10 09:15:04 -0600968
Jens Axboe94ae5e72019-11-14 19:39:52 -0700969 *link = io_prep_linked_timeout(req);
Jens Axboe561fb042019-10-24 07:25:42 -0600970 return do_hashed;
971}
972
Jackie Liua197f662019-11-08 08:09:12 -0700973static inline void io_queue_async_work(struct io_kiocb *req)
Jens Axboe561fb042019-10-24 07:25:42 -0600974{
Jackie Liua197f662019-11-08 08:09:12 -0700975 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe94ae5e72019-11-14 19:39:52 -0700976 struct io_kiocb *link;
977 bool do_hashed;
978
979 do_hashed = io_prep_async_work(req, &link);
Jens Axboe561fb042019-10-24 07:25:42 -0600980
981 trace_io_uring_queue_async_work(ctx, do_hashed, req, &req->work,
982 req->flags);
983 if (!do_hashed) {
984 io_wq_enqueue(ctx->io_wq, &req->work);
985 } else {
986 io_wq_enqueue_hashed(ctx->io_wq, &req->work,
987 file_inode(req->file));
988 }
Jens Axboe94ae5e72019-11-14 19:39:52 -0700989
990 if (link)
991 io_queue_linked_timeout(link);
Jens Axboe18d9be12019-09-10 09:13:05 -0600992}
993
Jens Axboe5262f562019-09-17 12:26:57 -0600994static void io_kill_timeout(struct io_kiocb *req)
995{
996 int ret;
997
Jens Axboe2d283902019-12-04 11:08:05 -0700998 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe5262f562019-09-17 12:26:57 -0600999 if (ret != -1) {
1000 atomic_inc(&req->ctx->cq_timeouts);
Jens Axboe842f9612019-10-29 12:34:10 -06001001 list_del_init(&req->list);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001002 io_cqring_fill_event(req, 0);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001003 io_put_req(req);
Jens Axboe5262f562019-09-17 12:26:57 -06001004 }
1005}
1006
1007static void io_kill_timeouts(struct io_ring_ctx *ctx)
1008{
1009 struct io_kiocb *req, *tmp;
1010
1011 spin_lock_irq(&ctx->completion_lock);
1012 list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list)
1013 io_kill_timeout(req);
1014 spin_unlock_irq(&ctx->completion_lock);
1015}
1016
Jens Axboede0617e2019-04-06 21:51:27 -06001017static void io_commit_cqring(struct io_ring_ctx *ctx)
1018{
1019 struct io_kiocb *req;
1020
Jens Axboe5262f562019-09-17 12:26:57 -06001021 while ((req = io_get_timeout_req(ctx)) != NULL)
1022 io_kill_timeout(req);
1023
Jens Axboede0617e2019-04-06 21:51:27 -06001024 __io_commit_cqring(ctx);
1025
Pavel Begunkov87987892020-01-18 01:22:30 +03001026 while ((req = io_get_deferred_req(ctx)) != NULL)
Jackie Liua197f662019-11-08 08:09:12 -07001027 io_queue_async_work(req);
Jens Axboede0617e2019-04-06 21:51:27 -06001028}
1029
Jens Axboe2b188cc2019-01-07 10:46:33 -07001030static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
1031{
Hristo Venev75b28af2019-08-26 17:23:46 +00001032 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001033 unsigned tail;
1034
1035 tail = ctx->cached_cq_tail;
Stefan Bühler115e12e2019-04-24 23:54:18 +02001036 /*
1037 * writes to the cq entry need to come after reading head; the
1038 * control dependency is enough as we're using WRITE_ONCE to
1039 * fill the cq entry
1040 */
Hristo Venev75b28af2019-08-26 17:23:46 +00001041 if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001042 return NULL;
1043
1044 ctx->cached_cq_tail++;
Hristo Venev75b28af2019-08-26 17:23:46 +00001045 return &rings->cqes[tail & ctx->cq_mask];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001046}
1047
Jens Axboef2842ab2020-01-08 11:04:00 -07001048static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx)
1049{
Jens Axboef0b493e2020-02-01 21:30:11 -07001050 if (!ctx->cq_ev_fd)
1051 return false;
Jens Axboef2842ab2020-01-08 11:04:00 -07001052 if (!ctx->eventfd_async)
1053 return true;
1054 return io_wq_current_is_worker() || in_interrupt();
1055}
1056
Jens Axboef0b493e2020-02-01 21:30:11 -07001057static void __io_cqring_ev_posted(struct io_ring_ctx *ctx, bool trigger_ev)
Jens Axboe8c838782019-03-12 15:48:16 -06001058{
1059 if (waitqueue_active(&ctx->wait))
1060 wake_up(&ctx->wait);
1061 if (waitqueue_active(&ctx->sqo_wait))
1062 wake_up(&ctx->sqo_wait);
Jens Axboef0b493e2020-02-01 21:30:11 -07001063 if (trigger_ev)
Jens Axboe9b402842019-04-11 11:45:41 -06001064 eventfd_signal(ctx->cq_ev_fd, 1);
Jens Axboe8c838782019-03-12 15:48:16 -06001065}
1066
Jens Axboef0b493e2020-02-01 21:30:11 -07001067static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
1068{
1069 __io_cqring_ev_posted(ctx, io_should_trigger_evfd(ctx));
1070}
1071
Jens Axboec4a2ed72019-11-21 21:01:26 -07001072/* Returns true if there are no backlogged entries after the flush */
1073static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001074{
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001075 struct io_rings *rings = ctx->rings;
1076 struct io_uring_cqe *cqe;
1077 struct io_kiocb *req;
1078 unsigned long flags;
1079 LIST_HEAD(list);
1080
1081 if (!force) {
1082 if (list_empty_careful(&ctx->cq_overflow_list))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001083 return true;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001084 if ((ctx->cached_cq_tail - READ_ONCE(rings->cq.head) ==
1085 rings->cq_ring_entries))
Jens Axboec4a2ed72019-11-21 21:01:26 -07001086 return false;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001087 }
1088
1089 spin_lock_irqsave(&ctx->completion_lock, flags);
1090
1091 /* if force is set, the ring is going away. always drop after that */
1092 if (force)
Jens Axboe69b3e542020-01-08 11:01:46 -07001093 ctx->cq_overflow_flushed = 1;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001094
Jens Axboec4a2ed72019-11-21 21:01:26 -07001095 cqe = NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001096 while (!list_empty(&ctx->cq_overflow_list)) {
1097 cqe = io_get_cqring(ctx);
1098 if (!cqe && !force)
1099 break;
1100
1101 req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
1102 list);
1103 list_move(&req->list, &list);
Jens Axboe2ca10252020-02-13 17:17:35 -07001104 req->flags &= ~REQ_F_OVERFLOW;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001105 if (cqe) {
1106 WRITE_ONCE(cqe->user_data, req->user_data);
1107 WRITE_ONCE(cqe->res, req->result);
1108 WRITE_ONCE(cqe->flags, 0);
1109 } else {
1110 WRITE_ONCE(ctx->rings->cq_overflow,
1111 atomic_inc_return(&ctx->cached_cq_overflow));
1112 }
1113 }
1114
1115 io_commit_cqring(ctx);
Jens Axboead3eb2c2019-12-18 17:12:20 -07001116 if (cqe) {
1117 clear_bit(0, &ctx->sq_check_overflow);
1118 clear_bit(0, &ctx->cq_check_overflow);
1119 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001120 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1121 io_cqring_ev_posted(ctx);
1122
1123 while (!list_empty(&list)) {
1124 req = list_first_entry(&list, struct io_kiocb, list);
1125 list_del(&req->list);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001126 io_put_req(req);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001127 }
Jens Axboec4a2ed72019-11-21 21:01:26 -07001128
1129 return cqe != NULL;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001130}
1131
Jens Axboe78e19bb2019-11-06 15:21:34 -07001132static void io_cqring_fill_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001133{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001134 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001135 struct io_uring_cqe *cqe;
1136
Jens Axboe78e19bb2019-11-06 15:21:34 -07001137 trace_io_uring_complete(ctx, req->user_data, res);
Jens Axboe51c3ff62019-11-03 06:52:50 -07001138
Jens Axboe2b188cc2019-01-07 10:46:33 -07001139 /*
1140 * If we can't get a cq entry, userspace overflowed the
1141 * submission (by quite a lot). Increment the overflow count in
1142 * the ring.
1143 */
1144 cqe = io_get_cqring(ctx);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001145 if (likely(cqe)) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001146 WRITE_ONCE(cqe->user_data, req->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001147 WRITE_ONCE(cqe->res, res);
1148 WRITE_ONCE(cqe->flags, 0);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001149 } else if (ctx->cq_overflow_flushed) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07001150 WRITE_ONCE(ctx->rings->cq_overflow,
1151 atomic_inc_return(&ctx->cached_cq_overflow));
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001152 } else {
Jens Axboead3eb2c2019-12-18 17:12:20 -07001153 if (list_empty(&ctx->cq_overflow_list)) {
1154 set_bit(0, &ctx->sq_check_overflow);
1155 set_bit(0, &ctx->cq_check_overflow);
1156 }
Jens Axboe2ca10252020-02-13 17:17:35 -07001157 req->flags |= REQ_F_OVERFLOW;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001158 refcount_inc(&req->refs);
1159 req->result = res;
1160 list_add_tail(&req->list, &ctx->cq_overflow_list);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001161 }
1162}
1163
Jens Axboe78e19bb2019-11-06 15:21:34 -07001164static void io_cqring_add_event(struct io_kiocb *req, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001165{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001166 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001167 unsigned long flags;
1168
1169 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001170 io_cqring_fill_event(req, res);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001171 io_commit_cqring(ctx);
1172 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1173
Jens Axboe8c838782019-03-12 15:48:16 -06001174 io_cqring_ev_posted(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001175}
1176
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001177static inline bool io_is_fallback_req(struct io_kiocb *req)
1178{
1179 return req == (struct io_kiocb *)
1180 ((unsigned long) req->ctx->fallback_req & ~1UL);
1181}
1182
1183static struct io_kiocb *io_get_fallback_req(struct io_ring_ctx *ctx)
1184{
1185 struct io_kiocb *req;
1186
1187 req = ctx->fallback_req;
1188 if (!test_and_set_bit_lock(0, (unsigned long *) ctx->fallback_req))
1189 return req;
1190
1191 return NULL;
1192}
1193
Jens Axboe2579f912019-01-09 09:10:43 -07001194static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx,
1195 struct io_submit_state *state)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001196{
Jens Axboefd6fab22019-03-14 16:30:06 -06001197 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001198 struct io_kiocb *req;
1199
Jens Axboe2579f912019-01-09 09:10:43 -07001200 if (!state) {
Jens Axboefd6fab22019-03-14 16:30:06 -06001201 req = kmem_cache_alloc(req_cachep, gfp);
Jens Axboe2579f912019-01-09 09:10:43 -07001202 if (unlikely(!req))
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001203 goto fallback;
Jens Axboe2579f912019-01-09 09:10:43 -07001204 } else if (!state->free_reqs) {
1205 size_t sz;
1206 int ret;
1207
1208 sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs));
Jens Axboefd6fab22019-03-14 16:30:06 -06001209 ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs);
1210
1211 /*
1212 * Bulk alloc is all-or-nothing. If we fail to get a batch,
1213 * retry single alloc to be on the safe side.
1214 */
1215 if (unlikely(ret <= 0)) {
1216 state->reqs[0] = kmem_cache_alloc(req_cachep, gfp);
1217 if (!state->reqs[0])
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001218 goto fallback;
Jens Axboefd6fab22019-03-14 16:30:06 -06001219 ret = 1;
1220 }
Jens Axboe2579f912019-01-09 09:10:43 -07001221 state->free_reqs = ret - 1;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001222 req = state->reqs[ret - 1];
Jens Axboe2579f912019-01-09 09:10:43 -07001223 } else {
Jens Axboe2579f912019-01-09 09:10:43 -07001224 state->free_reqs--;
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03001225 req = state->reqs[state->free_reqs];
Jens Axboe2b188cc2019-01-07 10:46:33 -07001226 }
1227
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001228got_it:
Jens Axboe1a6b74f2019-12-02 10:33:15 -07001229 req->io = NULL;
Jens Axboe60c112b2019-06-21 10:20:18 -06001230 req->file = NULL;
Jens Axboe2579f912019-01-09 09:10:43 -07001231 req->ctx = ctx;
1232 req->flags = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06001233 /* one is dropped after submission, the other at completion */
1234 refcount_set(&req->refs, 2);
Jens Axboe9e645e112019-05-10 16:07:28 -06001235 req->result = 0;
Jens Axboe561fb042019-10-24 07:25:42 -06001236 INIT_IO_WORK(&req->work, io_wq_submit_work);
Jens Axboe2579f912019-01-09 09:10:43 -07001237 return req;
Jens Axboe0ddf92e2019-11-08 08:52:53 -07001238fallback:
1239 req = io_get_fallback_req(ctx);
1240 if (req)
1241 goto got_it;
Pavel Begunkov6805b322019-10-08 02:18:42 +03001242 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001243 return NULL;
1244}
1245
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001246static void __io_req_do_free(struct io_kiocb *req)
Jens Axboedef596e2019-01-09 08:59:42 -07001247{
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001248 if (likely(!io_is_fallback_req(req)))
1249 kmem_cache_free(req_cachep, req);
1250 else
1251 clear_bit_unlock(0, (unsigned long *) req->ctx->fallback_req);
1252}
1253
Jens Axboec6ca97b302019-12-28 12:11:08 -07001254static void __io_req_aux_free(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001255{
Jens Axboefcb323c2019-10-24 12:39:47 -06001256 struct io_ring_ctx *ctx = req->ctx;
1257
Pavel Begunkov929a3af2020-02-19 00:19:09 +03001258 if (req->flags & REQ_F_NEED_CLEANUP)
1259 io_cleanup_req(req);
1260
YueHaibing96fd84d2020-01-07 22:22:44 +08001261 kfree(req->io);
Jens Axboe05f3fb32019-12-09 11:22:50 -07001262 if (req->file) {
1263 if (req->flags & REQ_F_FIXED_FILE)
1264 percpu_ref_put(&ctx->file_data->refs);
1265 else
1266 fput(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001267 }
Jens Axboecccf0ee2020-01-27 16:34:48 -07001268
1269 io_req_work_drop_env(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001270}
1271
1272static void __io_free_req(struct io_kiocb *req)
1273{
Jens Axboec6ca97b302019-12-28 12:11:08 -07001274 __io_req_aux_free(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001275
Jens Axboefcb323c2019-10-24 12:39:47 -06001276 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001277 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06001278 unsigned long flags;
1279
1280 spin_lock_irqsave(&ctx->inflight_lock, flags);
1281 list_del(&req->inflight_entry);
1282 if (waitqueue_active(&ctx->inflight_wait))
1283 wake_up(&ctx->inflight_wait);
1284 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1285 }
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03001286
1287 percpu_ref_put(&req->ctx->refs);
1288 __io_req_do_free(req);
Jens Axboee65ef562019-03-12 10:16:44 -06001289}
1290
Jens Axboec6ca97b302019-12-28 12:11:08 -07001291struct req_batch {
1292 void *reqs[IO_IOPOLL_BATCH];
1293 int to_free;
1294 int need_iter;
1295};
1296
1297static void io_free_req_many(struct io_ring_ctx *ctx, struct req_batch *rb)
1298{
Jens Axboe10fef4b2020-01-09 07:52:28 -07001299 int fixed_refs = rb->to_free;
1300
Jens Axboec6ca97b302019-12-28 12:11:08 -07001301 if (!rb->to_free)
1302 return;
1303 if (rb->need_iter) {
1304 int i, inflight = 0;
1305 unsigned long flags;
1306
Jens Axboe10fef4b2020-01-09 07:52:28 -07001307 fixed_refs = 0;
Jens Axboec6ca97b302019-12-28 12:11:08 -07001308 for (i = 0; i < rb->to_free; i++) {
1309 struct io_kiocb *req = rb->reqs[i];
1310
Jens Axboe10fef4b2020-01-09 07:52:28 -07001311 if (req->flags & REQ_F_FIXED_FILE) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001312 req->file = NULL;
Jens Axboe10fef4b2020-01-09 07:52:28 -07001313 fixed_refs++;
1314 }
Jens Axboec6ca97b302019-12-28 12:11:08 -07001315 if (req->flags & REQ_F_INFLIGHT)
1316 inflight++;
Jens Axboec6ca97b302019-12-28 12:11:08 -07001317 __io_req_aux_free(req);
1318 }
1319 if (!inflight)
1320 goto do_free;
1321
1322 spin_lock_irqsave(&ctx->inflight_lock, flags);
1323 for (i = 0; i < rb->to_free; i++) {
1324 struct io_kiocb *req = rb->reqs[i];
1325
Jens Axboe10fef4b2020-01-09 07:52:28 -07001326 if (req->flags & REQ_F_INFLIGHT) {
Jens Axboec6ca97b302019-12-28 12:11:08 -07001327 list_del(&req->inflight_entry);
1328 if (!--inflight)
1329 break;
1330 }
1331 }
1332 spin_unlock_irqrestore(&ctx->inflight_lock, flags);
1333
1334 if (waitqueue_active(&ctx->inflight_wait))
1335 wake_up(&ctx->inflight_wait);
1336 }
1337do_free:
1338 kmem_cache_free_bulk(req_cachep, rb->to_free, rb->reqs);
Jens Axboe10fef4b2020-01-09 07:52:28 -07001339 if (fixed_refs)
1340 percpu_ref_put_many(&ctx->file_data->refs, fixed_refs);
Jens Axboec6ca97b302019-12-28 12:11:08 -07001341 percpu_ref_put_many(&ctx->refs, rb->to_free);
Jens Axboec6ca97b302019-12-28 12:11:08 -07001342 rb->to_free = rb->need_iter = 0;
Jens Axboee65ef562019-03-12 10:16:44 -06001343}
1344
Jackie Liua197f662019-11-08 08:09:12 -07001345static bool io_link_cancel_timeout(struct io_kiocb *req)
Jens Axboe9e645e112019-05-10 16:07:28 -06001346{
Jackie Liua197f662019-11-08 08:09:12 -07001347 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001348 int ret;
1349
Jens Axboe2d283902019-12-04 11:08:05 -07001350 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe2665abf2019-11-05 12:40:47 -07001351 if (ret != -1) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001352 io_cqring_fill_event(req, -ECANCELED);
Jens Axboe2665abf2019-11-05 12:40:47 -07001353 io_commit_cqring(ctx);
1354 req->flags &= ~REQ_F_LINK;
Jackie Liuec9c02a2019-11-08 23:50:36 +08001355 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07001356 return true;
1357 }
1358
1359 return false;
1360}
1361
Jens Axboeba816ad2019-09-28 11:36:45 -06001362static void io_req_link_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboe9e645e112019-05-10 16:07:28 -06001363{
Jens Axboe2665abf2019-11-05 12:40:47 -07001364 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001365 bool wake_ev = false;
Jens Axboe9e645e112019-05-10 16:07:28 -06001366
Jens Axboe4d7dd462019-11-20 13:03:52 -07001367 /* Already got next link */
1368 if (req->flags & REQ_F_LINK_NEXT)
1369 return;
1370
Jens Axboe9e645e112019-05-10 16:07:28 -06001371 /*
1372 * The list should never be empty when we are called here. But could
1373 * potentially happen if the chain is messed up, check to be on the
1374 * safe side.
1375 */
Pavel Begunkov44932332019-12-05 16:16:35 +03001376 while (!list_empty(&req->link_list)) {
1377 struct io_kiocb *nxt = list_first_entry(&req->link_list,
1378 struct io_kiocb, link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001379
Pavel Begunkov44932332019-12-05 16:16:35 +03001380 if (unlikely((req->flags & REQ_F_LINK_TIMEOUT) &&
1381 (nxt->flags & REQ_F_TIMEOUT))) {
1382 list_del_init(&nxt->link_list);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001383 wake_ev |= io_link_cancel_timeout(nxt);
Jens Axboe94ae5e72019-11-14 19:39:52 -07001384 req->flags &= ~REQ_F_LINK_TIMEOUT;
1385 continue;
1386 }
Jens Axboe9e645e112019-05-10 16:07:28 -06001387
Pavel Begunkov44932332019-12-05 16:16:35 +03001388 list_del_init(&req->link_list);
1389 if (!list_empty(&nxt->link_list))
1390 nxt->flags |= REQ_F_LINK;
Pavel Begunkovb18fdf72019-11-21 23:21:02 +03001391 *nxtptr = nxt;
Jens Axboe94ae5e72019-11-14 19:39:52 -07001392 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06001393 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001394
Jens Axboe4d7dd462019-11-20 13:03:52 -07001395 req->flags |= REQ_F_LINK_NEXT;
Jens Axboe2665abf2019-11-05 12:40:47 -07001396 if (wake_ev)
1397 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001398}
1399
1400/*
1401 * Called if REQ_F_LINK is set, and we fail the head request
1402 */
1403static void io_fail_links(struct io_kiocb *req)
1404{
Jens Axboe2665abf2019-11-05 12:40:47 -07001405 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07001406 unsigned long flags;
1407
1408 spin_lock_irqsave(&ctx->completion_lock, flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06001409
1410 while (!list_empty(&req->link_list)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03001411 struct io_kiocb *link = list_first_entry(&req->link_list,
1412 struct io_kiocb, link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06001413
Pavel Begunkov44932332019-12-05 16:16:35 +03001414 list_del_init(&link->link_list);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02001415 trace_io_uring_fail_link(req, link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001416
1417 if ((req->flags & REQ_F_LINK_TIMEOUT) &&
Jens Axboed625c6e2019-12-17 19:53:05 -07001418 link->opcode == IORING_OP_LINK_TIMEOUT) {
Jackie Liua197f662019-11-08 08:09:12 -07001419 io_link_cancel_timeout(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001420 } else {
Jens Axboe78e19bb2019-11-06 15:21:34 -07001421 io_cqring_fill_event(link, -ECANCELED);
Jens Axboe978db572019-11-14 22:39:04 -07001422 __io_double_put_req(link);
Jens Axboe2665abf2019-11-05 12:40:47 -07001423 }
Jens Axboe5d960722019-11-19 15:31:28 -07001424 req->flags &= ~REQ_F_LINK_TIMEOUT;
Jens Axboe9e645e112019-05-10 16:07:28 -06001425 }
Jens Axboe2665abf2019-11-05 12:40:47 -07001426
1427 io_commit_cqring(ctx);
1428 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1429 io_cqring_ev_posted(ctx);
Jens Axboe9e645e112019-05-10 16:07:28 -06001430}
1431
Jens Axboe4d7dd462019-11-20 13:03:52 -07001432static void io_req_find_next(struct io_kiocb *req, struct io_kiocb **nxt)
Jens Axboe9e645e112019-05-10 16:07:28 -06001433{
Jens Axboe4d7dd462019-11-20 13:03:52 -07001434 if (likely(!(req->flags & REQ_F_LINK)))
Jens Axboe2665abf2019-11-05 12:40:47 -07001435 return;
Jens Axboe2665abf2019-11-05 12:40:47 -07001436
Jens Axboe9e645e112019-05-10 16:07:28 -06001437 /*
1438 * If LINK is set, we have dependent requests in this chain. If we
1439 * didn't fail this request, queue the first one up, moving any other
1440 * dependencies to the next request. In case of failure, fail the rest
1441 * of the chain.
1442 */
Jens Axboe2665abf2019-11-05 12:40:47 -07001443 if (req->flags & REQ_F_FAIL_LINK) {
1444 io_fail_links(req);
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001445 } else if ((req->flags & (REQ_F_LINK_TIMEOUT | REQ_F_COMP_LOCKED)) ==
1446 REQ_F_LINK_TIMEOUT) {
Jens Axboe2665abf2019-11-05 12:40:47 -07001447 struct io_ring_ctx *ctx = req->ctx;
1448 unsigned long flags;
1449
1450 /*
1451 * If this is a timeout link, we could be racing with the
1452 * timeout timer. Grab the completion lock for this case to
Jens Axboe7c9e7f02019-11-12 08:15:53 -07001453 * protect against that.
Jens Axboe2665abf2019-11-05 12:40:47 -07001454 */
1455 spin_lock_irqsave(&ctx->completion_lock, flags);
1456 io_req_link_next(req, nxt);
1457 spin_unlock_irqrestore(&ctx->completion_lock, flags);
1458 } else {
1459 io_req_link_next(req, nxt);
Jens Axboe9e645e112019-05-10 16:07:28 -06001460 }
Jens Axboe4d7dd462019-11-20 13:03:52 -07001461}
Jens Axboe9e645e112019-05-10 16:07:28 -06001462
Jackie Liuc69f8db2019-11-09 11:00:08 +08001463static void io_free_req(struct io_kiocb *req)
1464{
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001465 struct io_kiocb *nxt = NULL;
1466
1467 io_req_find_next(req, &nxt);
Pavel Begunkov70cf9f32019-11-21 23:21:00 +03001468 __io_free_req(req);
Pavel Begunkov944e58b2019-11-21 23:21:01 +03001469
1470 if (nxt)
1471 io_queue_async_work(nxt);
Jackie Liuc69f8db2019-11-09 11:00:08 +08001472}
1473
Jens Axboeba816ad2019-09-28 11:36:45 -06001474/*
1475 * Drop reference to request, return next in chain (if there is one) if this
1476 * was the last reference to this request.
1477 */
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001478__attribute__((nonnull))
Jackie Liuec9c02a2019-11-08 23:50:36 +08001479static void io_put_req_find_next(struct io_kiocb *req, struct io_kiocb **nxtptr)
Jens Axboee65ef562019-03-12 10:16:44 -06001480{
Jens Axboe2a44f462020-02-25 13:25:41 -07001481 if (refcount_dec_and_test(&req->refs)) {
1482 io_req_find_next(req, nxtptr);
Jens Axboe4d7dd462019-11-20 13:03:52 -07001483 __io_free_req(req);
Jens Axboe2a44f462020-02-25 13:25:41 -07001484 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001485}
1486
Jens Axboe2b188cc2019-01-07 10:46:33 -07001487static void io_put_req(struct io_kiocb *req)
1488{
Jens Axboedef596e2019-01-09 08:59:42 -07001489 if (refcount_dec_and_test(&req->refs))
1490 io_free_req(req);
1491}
1492
Jens Axboe978db572019-11-14 22:39:04 -07001493/*
1494 * Must only be used if we don't need to care about links, usually from
1495 * within the completion handling itself.
1496 */
1497static void __io_double_put_req(struct io_kiocb *req)
Jens Axboea3a0e432019-08-20 11:03:11 -06001498{
Jens Axboe78e19bb2019-11-06 15:21:34 -07001499 /* drop both submit and complete references */
1500 if (refcount_sub_and_test(2, &req->refs))
1501 __io_free_req(req);
1502}
1503
Jens Axboe978db572019-11-14 22:39:04 -07001504static void io_double_put_req(struct io_kiocb *req)
1505{
1506 /* drop both submit and complete references */
1507 if (refcount_sub_and_test(2, &req->refs))
1508 io_free_req(req);
1509}
1510
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001511static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
Jens Axboea3a0e432019-08-20 11:03:11 -06001512{
Jens Axboe84f97dc2019-11-06 11:27:53 -07001513 struct io_rings *rings = ctx->rings;
1514
Jens Axboead3eb2c2019-12-18 17:12:20 -07001515 if (test_bit(0, &ctx->cq_check_overflow)) {
1516 /*
1517 * noflush == true is from the waitqueue handler, just ensure
1518 * we wake up the task, and the next invocation will flush the
1519 * entries. We cannot safely to it from here.
1520 */
1521 if (noflush && !list_empty(&ctx->cq_overflow_list))
1522 return -1U;
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001523
Jens Axboead3eb2c2019-12-18 17:12:20 -07001524 io_cqring_overflow_flush(ctx, false);
1525 }
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001526
Jens Axboea3a0e432019-08-20 11:03:11 -06001527 /* See comment at the top of this file */
1528 smp_rmb();
Jens Axboead3eb2c2019-12-18 17:12:20 -07001529 return ctx->cached_cq_tail - READ_ONCE(rings->cq.head);
Jens Axboea3a0e432019-08-20 11:03:11 -06001530}
1531
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03001532static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx)
1533{
1534 struct io_rings *rings = ctx->rings;
1535
1536 /* make sure SQ entry isn't read before tail */
1537 return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head;
1538}
1539
Jens Axboe8237e042019-12-28 10:48:22 -07001540static inline bool io_req_multi_free(struct req_batch *rb, struct io_kiocb *req)
Jens Axboee94f1412019-12-19 12:06:02 -07001541{
Jens Axboec6ca97b302019-12-28 12:11:08 -07001542 if ((req->flags & REQ_F_LINK) || io_is_fallback_req(req))
1543 return false;
Jens Axboee94f1412019-12-19 12:06:02 -07001544
Jens Axboec6ca97b302019-12-28 12:11:08 -07001545 if (!(req->flags & REQ_F_FIXED_FILE) || req->io)
1546 rb->need_iter++;
1547
1548 rb->reqs[rb->to_free++] = req;
1549 if (unlikely(rb->to_free == ARRAY_SIZE(rb->reqs)))
1550 io_free_req_many(req->ctx, rb);
1551 return true;
Jens Axboee94f1412019-12-19 12:06:02 -07001552}
1553
Jens Axboedef596e2019-01-09 08:59:42 -07001554/*
1555 * Find and free completed poll iocbs
1556 */
1557static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
1558 struct list_head *done)
1559{
Jens Axboe8237e042019-12-28 10:48:22 -07001560 struct req_batch rb;
Jens Axboedef596e2019-01-09 08:59:42 -07001561 struct io_kiocb *req;
Jens Axboedef596e2019-01-09 08:59:42 -07001562
Jens Axboec6ca97b302019-12-28 12:11:08 -07001563 rb.to_free = rb.need_iter = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001564 while (!list_empty(done)) {
1565 req = list_first_entry(done, struct io_kiocb, list);
1566 list_del(&req->list);
1567
Jens Axboe78e19bb2019-11-06 15:21:34 -07001568 io_cqring_fill_event(req, req->result);
Jens Axboedef596e2019-01-09 08:59:42 -07001569 (*nr_events)++;
1570
Jens Axboe8237e042019-12-28 10:48:22 -07001571 if (refcount_dec_and_test(&req->refs) &&
1572 !io_req_multi_free(&rb, req))
1573 io_free_req(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001574 }
Jens Axboedef596e2019-01-09 08:59:42 -07001575
Jens Axboe09bb8392019-03-13 12:39:28 -06001576 io_commit_cqring(ctx);
Jens Axboe8237e042019-12-28 10:48:22 -07001577 io_free_req_many(ctx, &rb);
Jens Axboedef596e2019-01-09 08:59:42 -07001578}
1579
1580static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events,
1581 long min)
1582{
1583 struct io_kiocb *req, *tmp;
1584 LIST_HEAD(done);
1585 bool spin;
1586 int ret;
1587
1588 /*
1589 * Only spin for completions if we don't have multiple devices hanging
1590 * off our complete list, and we're under the requested amount.
1591 */
1592 spin = !ctx->poll_multi_file && *nr_events < min;
1593
1594 ret = 0;
1595 list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) {
Jens Axboe9adbd452019-12-20 08:45:55 -07001596 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboedef596e2019-01-09 08:59:42 -07001597
1598 /*
1599 * Move completed entries to our local list. If we find a
1600 * request that requires polling, break out and complete
1601 * the done list first, if we have entries there.
1602 */
1603 if (req->flags & REQ_F_IOPOLL_COMPLETED) {
1604 list_move_tail(&req->list, &done);
1605 continue;
1606 }
1607 if (!list_empty(&done))
1608 break;
1609
1610 ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin);
1611 if (ret < 0)
1612 break;
1613
1614 if (ret && spin)
1615 spin = false;
1616 ret = 0;
1617 }
1618
1619 if (!list_empty(&done))
1620 io_iopoll_complete(ctx, nr_events, &done);
1621
1622 return ret;
1623}
1624
1625/*
Brian Gianforcarod195a662019-12-13 03:09:50 -08001626 * Poll for a minimum of 'min' events. Note that if min == 0 we consider that a
Jens Axboedef596e2019-01-09 08:59:42 -07001627 * non-spinning poll check - we'll still enter the driver poll loop, but only
1628 * as a non-spinning completion check.
1629 */
1630static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events,
1631 long min)
1632{
Jens Axboe08f54392019-08-21 22:19:11 -06001633 while (!list_empty(&ctx->poll_list) && !need_resched()) {
Jens Axboedef596e2019-01-09 08:59:42 -07001634 int ret;
1635
1636 ret = io_do_iopoll(ctx, nr_events, min);
1637 if (ret < 0)
1638 return ret;
1639 if (!min || *nr_events >= min)
1640 return 0;
1641 }
1642
1643 return 1;
1644}
1645
1646/*
1647 * We can't just wait for polled events to come to us, we have to actively
1648 * find and complete them.
1649 */
1650static void io_iopoll_reap_events(struct io_ring_ctx *ctx)
1651{
1652 if (!(ctx->flags & IORING_SETUP_IOPOLL))
1653 return;
1654
1655 mutex_lock(&ctx->uring_lock);
1656 while (!list_empty(&ctx->poll_list)) {
1657 unsigned int nr_events = 0;
1658
1659 io_iopoll_getevents(ctx, &nr_events, 1);
Jens Axboe08f54392019-08-21 22:19:11 -06001660
1661 /*
1662 * Ensure we allow local-to-the-cpu processing to take place,
1663 * in this case we need to ensure that we reap all events.
1664 */
1665 cond_resched();
Jens Axboedef596e2019-01-09 08:59:42 -07001666 }
1667 mutex_unlock(&ctx->uring_lock);
1668}
1669
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08001670static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events,
1671 long min)
Jens Axboedef596e2019-01-09 08:59:42 -07001672{
Jens Axboe2b2ed972019-10-25 10:06:15 -06001673 int iters = 0, ret = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001674
Xiaoguang Wangc7849be2020-02-22 14:46:05 +08001675 /*
1676 * We disallow the app entering submit/complete with polling, but we
1677 * still need to lock the ring to prevent racing with polled issue
1678 * that got punted to a workqueue.
1679 */
1680 mutex_lock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001681 do {
1682 int tmin = 0;
1683
Jens Axboe500f9fb2019-08-19 12:15:59 -06001684 /*
Jens Axboea3a0e432019-08-20 11:03:11 -06001685 * Don't enter poll loop if we already have events pending.
1686 * If we do, we can potentially be spinning for commands that
1687 * already triggered a CQE (eg in error).
1688 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07001689 if (io_cqring_events(ctx, false))
Jens Axboea3a0e432019-08-20 11:03:11 -06001690 break;
1691
1692 /*
Jens Axboe500f9fb2019-08-19 12:15:59 -06001693 * If a submit got punted to a workqueue, we can have the
1694 * application entering polling for a command before it gets
1695 * issued. That app will hold the uring_lock for the duration
1696 * of the poll right here, so we need to take a breather every
1697 * now and then to ensure that the issue has a chance to add
1698 * the poll to the issued list. Otherwise we can spin here
1699 * forever, while the workqueue is stuck trying to acquire the
1700 * very same mutex.
1701 */
1702 if (!(++iters & 7)) {
1703 mutex_unlock(&ctx->uring_lock);
1704 mutex_lock(&ctx->uring_lock);
1705 }
1706
Jens Axboedef596e2019-01-09 08:59:42 -07001707 if (*nr_events < min)
1708 tmin = min - *nr_events;
1709
1710 ret = io_iopoll_getevents(ctx, nr_events, tmin);
1711 if (ret <= 0)
1712 break;
1713 ret = 0;
1714 } while (min && !*nr_events && !need_resched());
1715
Jens Axboe500f9fb2019-08-19 12:15:59 -06001716 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07001717 return ret;
1718}
1719
Jens Axboe491381ce2019-10-17 09:20:46 -06001720static void kiocb_end_write(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001721{
Jens Axboe491381ce2019-10-17 09:20:46 -06001722 /*
1723 * Tell lockdep we inherited freeze protection from submission
1724 * thread.
1725 */
1726 if (req->flags & REQ_F_ISREG) {
1727 struct inode *inode = file_inode(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001728
Jens Axboe491381ce2019-10-17 09:20:46 -06001729 __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001730 }
Jens Axboe491381ce2019-10-17 09:20:46 -06001731 file_end_write(req->file);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001732}
1733
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001734static inline void req_set_fail_links(struct io_kiocb *req)
1735{
1736 if ((req->flags & (REQ_F_LINK | REQ_F_HARDLINK)) == REQ_F_LINK)
1737 req->flags |= REQ_F_FAIL_LINK;
1738}
1739
Jens Axboeba816ad2019-09-28 11:36:45 -06001740static void io_complete_rw_common(struct kiocb *kiocb, long res)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001741{
Jens Axboe9adbd452019-12-20 08:45:55 -07001742 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001743
Jens Axboe491381ce2019-10-17 09:20:46 -06001744 if (kiocb->ki_flags & IOCB_WRITE)
1745 kiocb_end_write(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001746
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001747 if (res != req->result)
1748 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07001749 io_cqring_add_event(req, res);
Jens Axboeba816ad2019-09-28 11:36:45 -06001750}
1751
1752static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
1753{
Jens Axboe9adbd452019-12-20 08:45:55 -07001754 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboeba816ad2019-09-28 11:36:45 -06001755
1756 io_complete_rw_common(kiocb, res);
Jens Axboee65ef562019-03-12 10:16:44 -06001757 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001758}
1759
Jens Axboeba816ad2019-09-28 11:36:45 -06001760static struct io_kiocb *__io_complete_rw(struct kiocb *kiocb, long res)
1761{
Jens Axboe9adbd452019-12-20 08:45:55 -07001762 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001763 struct io_kiocb *nxt = NULL;
Jens Axboeba816ad2019-09-28 11:36:45 -06001764
1765 io_complete_rw_common(kiocb, res);
Jackie Liuec9c02a2019-11-08 23:50:36 +08001766 io_put_req_find_next(req, &nxt);
1767
1768 return nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001769}
1770
Jens Axboedef596e2019-01-09 08:59:42 -07001771static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2)
1772{
Jens Axboe9adbd452019-12-20 08:45:55 -07001773 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
Jens Axboedef596e2019-01-09 08:59:42 -07001774
Jens Axboe491381ce2019-10-17 09:20:46 -06001775 if (kiocb->ki_flags & IOCB_WRITE)
1776 kiocb_end_write(req);
Jens Axboedef596e2019-01-09 08:59:42 -07001777
Jens Axboe4e88d6e2019-12-07 20:59:47 -07001778 if (res != req->result)
1779 req_set_fail_links(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06001780 req->result = res;
Jens Axboedef596e2019-01-09 08:59:42 -07001781 if (res != -EAGAIN)
1782 req->flags |= REQ_F_IOPOLL_COMPLETED;
1783}
1784
1785/*
1786 * After the iocb has been issued, it's safe to be found on the poll list.
1787 * Adding the kiocb to the list AFTER submission ensures that we don't
1788 * find it from a io_iopoll_getevents() thread before the issuer is done
1789 * accessing the kiocb cookie.
1790 */
1791static void io_iopoll_req_issued(struct io_kiocb *req)
1792{
1793 struct io_ring_ctx *ctx = req->ctx;
1794
1795 /*
1796 * Track whether we have multiple files in our lists. This will impact
1797 * how we do polling eventually, not spinning if we're on potentially
1798 * different devices.
1799 */
1800 if (list_empty(&ctx->poll_list)) {
1801 ctx->poll_multi_file = false;
1802 } else if (!ctx->poll_multi_file) {
1803 struct io_kiocb *list_req;
1804
1805 list_req = list_first_entry(&ctx->poll_list, struct io_kiocb,
1806 list);
Jens Axboe9adbd452019-12-20 08:45:55 -07001807 if (list_req->file != req->file)
Jens Axboedef596e2019-01-09 08:59:42 -07001808 ctx->poll_multi_file = true;
1809 }
1810
1811 /*
1812 * For fast devices, IO may have already completed. If it has, add
1813 * it to the front so we find it first.
1814 */
1815 if (req->flags & REQ_F_IOPOLL_COMPLETED)
1816 list_add(&req->list, &ctx->poll_list);
1817 else
1818 list_add_tail(&req->list, &ctx->poll_list);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08001819
1820 if ((ctx->flags & IORING_SETUP_SQPOLL) &&
1821 wq_has_sleeper(&ctx->sqo_wait))
1822 wake_up(&ctx->sqo_wait);
Jens Axboedef596e2019-01-09 08:59:42 -07001823}
1824
Jens Axboe3d6770f2019-04-13 11:50:54 -06001825static void io_file_put(struct io_submit_state *state)
Jens Axboe9a56a232019-01-09 09:06:50 -07001826{
Jens Axboe3d6770f2019-04-13 11:50:54 -06001827 if (state->file) {
Jens Axboe9a56a232019-01-09 09:06:50 -07001828 int diff = state->has_refs - state->used_refs;
1829
1830 if (diff)
1831 fput_many(state->file, diff);
1832 state->file = NULL;
1833 }
1834}
1835
1836/*
1837 * Get as many references to a file as we have IOs left in this submission,
1838 * assuming most submissions are for one file, or at least that each file
1839 * has more than one submission.
1840 */
1841static struct file *io_file_get(struct io_submit_state *state, int fd)
1842{
1843 if (!state)
1844 return fget(fd);
1845
1846 if (state->file) {
1847 if (state->fd == fd) {
1848 state->used_refs++;
1849 state->ios_left--;
1850 return state->file;
1851 }
Jens Axboe3d6770f2019-04-13 11:50:54 -06001852 io_file_put(state);
Jens Axboe9a56a232019-01-09 09:06:50 -07001853 }
1854 state->file = fget_many(fd, state->ios_left);
1855 if (!state->file)
1856 return NULL;
1857
1858 state->fd = fd;
1859 state->has_refs = state->ios_left;
1860 state->used_refs = 1;
1861 state->ios_left--;
1862 return state->file;
1863}
1864
Jens Axboe2b188cc2019-01-07 10:46:33 -07001865/*
1866 * If we tracked the file through the SCM inflight mechanism, we could support
1867 * any file. For now, just ensure that anything potentially problematic is done
1868 * inline.
1869 */
1870static bool io_file_supports_async(struct file *file)
1871{
1872 umode_t mode = file_inode(file)->i_mode;
1873
Jens Axboe10d59342019-12-09 20:16:22 -07001874 if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISSOCK(mode))
Jens Axboe2b188cc2019-01-07 10:46:33 -07001875 return true;
1876 if (S_ISREG(mode) && file->f_op != &io_uring_fops)
1877 return true;
1878
1879 return false;
1880}
1881
Jens Axboe3529d8c2019-12-19 18:24:38 -07001882static int io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
1883 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001884{
Jens Axboedef596e2019-01-09 08:59:42 -07001885 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe9adbd452019-12-20 08:45:55 -07001886 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe09bb8392019-03-13 12:39:28 -06001887 unsigned ioprio;
1888 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001889
Jens Axboe491381ce2019-10-17 09:20:46 -06001890 if (S_ISREG(file_inode(req->file)->i_mode))
1891 req->flags |= REQ_F_ISREG;
1892
Jens Axboe2b188cc2019-01-07 10:46:33 -07001893 kiocb->ki_pos = READ_ONCE(sqe->off);
Jens Axboeba042912019-12-25 16:33:42 -07001894 if (kiocb->ki_pos == -1 && !(req->file->f_mode & FMODE_STREAM)) {
1895 req->flags |= REQ_F_CUR_POS;
1896 kiocb->ki_pos = req->file->f_pos;
1897 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07001898 kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp));
Pavel Begunkov3e577dc2020-02-01 03:58:42 +03001899 kiocb->ki_flags = iocb_flags(kiocb->ki_filp);
1900 ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags));
1901 if (unlikely(ret))
1902 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001903
1904 ioprio = READ_ONCE(sqe->ioprio);
1905 if (ioprio) {
1906 ret = ioprio_check_cap(ioprio);
1907 if (ret)
Jens Axboe09bb8392019-03-13 12:39:28 -06001908 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001909
1910 kiocb->ki_ioprio = ioprio;
1911 } else
1912 kiocb->ki_ioprio = get_current_ioprio();
1913
Stefan Bühler8449eed2019-04-27 20:34:19 +02001914 /* don't allow async punt if RWF_NOWAIT was requested */
Jens Axboe491381ce2019-10-17 09:20:46 -06001915 if ((kiocb->ki_flags & IOCB_NOWAIT) ||
1916 (req->file->f_flags & O_NONBLOCK))
Stefan Bühler8449eed2019-04-27 20:34:19 +02001917 req->flags |= REQ_F_NOWAIT;
1918
1919 if (force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07001920 kiocb->ki_flags |= IOCB_NOWAIT;
Stefan Bühler8449eed2019-04-27 20:34:19 +02001921
Jens Axboedef596e2019-01-09 08:59:42 -07001922 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07001923 if (!(kiocb->ki_flags & IOCB_DIRECT) ||
1924 !kiocb->ki_filp->f_op->iopoll)
Jens Axboe09bb8392019-03-13 12:39:28 -06001925 return -EOPNOTSUPP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001926
Jens Axboedef596e2019-01-09 08:59:42 -07001927 kiocb->ki_flags |= IOCB_HIPRI;
1928 kiocb->ki_complete = io_complete_rw_iopoll;
Jens Axboe6873e0b2019-10-30 13:53:09 -06001929 req->result = 0;
Jens Axboedef596e2019-01-09 08:59:42 -07001930 } else {
Jens Axboe09bb8392019-03-13 12:39:28 -06001931 if (kiocb->ki_flags & IOCB_HIPRI)
1932 return -EINVAL;
Jens Axboedef596e2019-01-09 08:59:42 -07001933 kiocb->ki_complete = io_complete_rw;
1934 }
Jens Axboe9adbd452019-12-20 08:45:55 -07001935
Jens Axboe3529d8c2019-12-19 18:24:38 -07001936 req->rw.addr = READ_ONCE(sqe->addr);
1937 req->rw.len = READ_ONCE(sqe->len);
Jens Axboe9adbd452019-12-20 08:45:55 -07001938 /* we own ->private, reuse it for the buffer index */
1939 req->rw.kiocb.private = (void *) (unsigned long)
Jens Axboe3529d8c2019-12-19 18:24:38 -07001940 READ_ONCE(sqe->buf_index);
Jens Axboe2b188cc2019-01-07 10:46:33 -07001941 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07001942}
1943
1944static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret)
1945{
1946 switch (ret) {
1947 case -EIOCBQUEUED:
1948 break;
1949 case -ERESTARTSYS:
1950 case -ERESTARTNOINTR:
1951 case -ERESTARTNOHAND:
1952 case -ERESTART_RESTARTBLOCK:
1953 /*
1954 * We can't just restart the syscall, since previously
1955 * submitted sqes may already be in progress. Just fail this
1956 * IO with EINTR.
1957 */
1958 ret = -EINTR;
1959 /* fall through */
1960 default:
1961 kiocb->ki_complete(kiocb, ret, 0);
1962 }
1963}
1964
Jens Axboeba816ad2019-09-28 11:36:45 -06001965static void kiocb_done(struct kiocb *kiocb, ssize_t ret, struct io_kiocb **nxt,
1966 bool in_async)
1967{
Jens Axboeba042912019-12-25 16:33:42 -07001968 struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw.kiocb);
1969
1970 if (req->flags & REQ_F_CUR_POS)
1971 req->file->f_pos = kiocb->ki_pos;
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03001972 if (in_async && ret >= 0 && kiocb->ki_complete == io_complete_rw)
Jens Axboeba816ad2019-09-28 11:36:45 -06001973 *nxt = __io_complete_rw(kiocb, ret);
1974 else
1975 io_rw_done(kiocb, ret);
1976}
1977
Jens Axboe9adbd452019-12-20 08:45:55 -07001978static ssize_t io_import_fixed(struct io_kiocb *req, int rw,
Pavel Begunkov7d009162019-11-25 23:14:40 +03001979 struct iov_iter *iter)
Jens Axboeedafcce2019-01-09 09:16:05 -07001980{
Jens Axboe9adbd452019-12-20 08:45:55 -07001981 struct io_ring_ctx *ctx = req->ctx;
1982 size_t len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07001983 struct io_mapped_ubuf *imu;
1984 unsigned index, buf_index;
1985 size_t offset;
1986 u64 buf_addr;
1987
1988 /* attempt to use fixed buffers without having provided iovecs */
1989 if (unlikely(!ctx->user_bufs))
1990 return -EFAULT;
1991
Jens Axboe9adbd452019-12-20 08:45:55 -07001992 buf_index = (unsigned long) req->rw.kiocb.private;
Jens Axboeedafcce2019-01-09 09:16:05 -07001993 if (unlikely(buf_index >= ctx->nr_user_bufs))
1994 return -EFAULT;
1995
1996 index = array_index_nospec(buf_index, ctx->nr_user_bufs);
1997 imu = &ctx->user_bufs[index];
Jens Axboe9adbd452019-12-20 08:45:55 -07001998 buf_addr = req->rw.addr;
Jens Axboeedafcce2019-01-09 09:16:05 -07001999
2000 /* overflow */
2001 if (buf_addr + len < buf_addr)
2002 return -EFAULT;
2003 /* not inside the mapped region */
2004 if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len)
2005 return -EFAULT;
2006
2007 /*
2008 * May not be a start of buffer, set size appropriately
2009 * and advance us to the beginning.
2010 */
2011 offset = buf_addr - imu->ubuf;
2012 iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len);
Jens Axboebd11b3a2019-07-20 08:37:31 -06002013
2014 if (offset) {
2015 /*
2016 * Don't use iov_iter_advance() here, as it's really slow for
2017 * using the latter parts of a big fixed buffer - it iterates
2018 * over each segment manually. We can cheat a bit here, because
2019 * we know that:
2020 *
2021 * 1) it's a BVEC iter, we set it up
2022 * 2) all bvecs are PAGE_SIZE in size, except potentially the
2023 * first and last bvec
2024 *
2025 * So just find our index, and adjust the iterator afterwards.
2026 * If the offset is within the first bvec (or the whole first
2027 * bvec, just use iov_iter_advance(). This makes it easier
2028 * since we can just skip the first segment, which may not
2029 * be PAGE_SIZE aligned.
2030 */
2031 const struct bio_vec *bvec = imu->bvec;
2032
2033 if (offset <= bvec->bv_len) {
2034 iov_iter_advance(iter, offset);
2035 } else {
2036 unsigned long seg_skip;
2037
2038 /* skip first vec */
2039 offset -= bvec->bv_len;
2040 seg_skip = 1 + (offset >> PAGE_SHIFT);
2041
2042 iter->bvec = bvec + seg_skip;
2043 iter->nr_segs -= seg_skip;
Aleix Roca Nonell99c79f62019-08-15 14:03:22 +02002044 iter->count -= bvec->bv_len + offset;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002045 iter->iov_offset = offset & ~PAGE_MASK;
Jens Axboebd11b3a2019-07-20 08:37:31 -06002046 }
2047 }
2048
Jens Axboe5e559562019-11-13 16:12:46 -07002049 return len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002050}
2051
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002052static ssize_t io_import_iovec(int rw, struct io_kiocb *req,
2053 struct iovec **iovec, struct iov_iter *iter)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002054{
Jens Axboe9adbd452019-12-20 08:45:55 -07002055 void __user *buf = u64_to_user_ptr(req->rw.addr);
2056 size_t sqe_len = req->rw.len;
Jens Axboeedafcce2019-01-09 09:16:05 -07002057 u8 opcode;
2058
Jens Axboed625c6e2019-12-17 19:53:05 -07002059 opcode = req->opcode;
Pavel Begunkov7d009162019-11-25 23:14:40 +03002060 if (opcode == IORING_OP_READ_FIXED || opcode == IORING_OP_WRITE_FIXED) {
Jens Axboeedafcce2019-01-09 09:16:05 -07002061 *iovec = NULL;
Jens Axboe9adbd452019-12-20 08:45:55 -07002062 return io_import_fixed(req, rw, iter);
Jens Axboeedafcce2019-01-09 09:16:05 -07002063 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002064
Jens Axboe9adbd452019-12-20 08:45:55 -07002065 /* buffer index only valid with fixed read/write */
2066 if (req->rw.kiocb.private)
2067 return -EINVAL;
2068
Jens Axboe3a6820f2019-12-22 15:19:35 -07002069 if (opcode == IORING_OP_READ || opcode == IORING_OP_WRITE) {
2070 ssize_t ret;
2071 ret = import_single_range(rw, buf, sqe_len, *iovec, iter);
2072 *iovec = NULL;
Jens Axboe3a901592020-02-25 17:48:55 -07002073 return ret < 0 ? ret : sqe_len;
Jens Axboe3a6820f2019-12-22 15:19:35 -07002074 }
2075
Jens Axboef67676d2019-12-02 11:03:47 -07002076 if (req->io) {
2077 struct io_async_rw *iorw = &req->io->rw;
2078
2079 *iovec = iorw->iov;
2080 iov_iter_init(iter, rw, *iovec, iorw->nr_segs, iorw->size);
2081 if (iorw->iov == iorw->fast_iov)
2082 *iovec = NULL;
2083 return iorw->size;
2084 }
2085
Jens Axboe2b188cc2019-01-07 10:46:33 -07002086#ifdef CONFIG_COMPAT
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002087 if (req->ctx->compat)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002088 return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV,
2089 iovec, iter);
2090#endif
2091
2092 return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter);
2093}
2094
Jens Axboe32960612019-09-23 11:05:34 -06002095/*
2096 * For files that don't have ->read_iter() and ->write_iter(), handle them
2097 * by looping over ->read() or ->write() manually.
2098 */
2099static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb,
2100 struct iov_iter *iter)
2101{
2102 ssize_t ret = 0;
2103
2104 /*
2105 * Don't support polled IO through this interface, and we can't
2106 * support non-blocking either. For the latter, this just causes
2107 * the kiocb to be handled from an async context.
2108 */
2109 if (kiocb->ki_flags & IOCB_HIPRI)
2110 return -EOPNOTSUPP;
2111 if (kiocb->ki_flags & IOCB_NOWAIT)
2112 return -EAGAIN;
2113
2114 while (iov_iter_count(iter)) {
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002115 struct iovec iovec;
Jens Axboe32960612019-09-23 11:05:34 -06002116 ssize_t nr;
2117
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002118 if (!iov_iter_is_bvec(iter)) {
2119 iovec = iov_iter_iovec(iter);
2120 } else {
2121 /* fixed buffers import bvec */
2122 iovec.iov_base = kmap(iter->bvec->bv_page)
2123 + iter->iov_offset;
2124 iovec.iov_len = min(iter->count,
2125 iter->bvec->bv_len - iter->iov_offset);
2126 }
2127
Jens Axboe32960612019-09-23 11:05:34 -06002128 if (rw == READ) {
2129 nr = file->f_op->read(file, iovec.iov_base,
2130 iovec.iov_len, &kiocb->ki_pos);
2131 } else {
2132 nr = file->f_op->write(file, iovec.iov_base,
2133 iovec.iov_len, &kiocb->ki_pos);
2134 }
2135
Pavel Begunkov311ae9e2019-11-24 11:58:24 +03002136 if (iov_iter_is_bvec(iter))
2137 kunmap(iter->bvec->bv_page);
2138
Jens Axboe32960612019-09-23 11:05:34 -06002139 if (nr < 0) {
2140 if (!ret)
2141 ret = nr;
2142 break;
2143 }
2144 ret += nr;
2145 if (nr != iovec.iov_len)
2146 break;
2147 iov_iter_advance(iter, nr);
2148 }
2149
2150 return ret;
2151}
2152
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002153static void io_req_map_rw(struct io_kiocb *req, ssize_t io_size,
Jens Axboef67676d2019-12-02 11:03:47 -07002154 struct iovec *iovec, struct iovec *fast_iov,
2155 struct iov_iter *iter)
2156{
2157 req->io->rw.nr_segs = iter->nr_segs;
2158 req->io->rw.size = io_size;
2159 req->io->rw.iov = iovec;
2160 if (!req->io->rw.iov) {
2161 req->io->rw.iov = req->io->rw.fast_iov;
2162 memcpy(req->io->rw.iov, fast_iov,
2163 sizeof(struct iovec) * iter->nr_segs);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002164 } else {
2165 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboef67676d2019-12-02 11:03:47 -07002166 }
2167}
2168
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002169static int io_alloc_async_ctx(struct io_kiocb *req)
Jens Axboef67676d2019-12-02 11:03:47 -07002170{
Jens Axboed3656342019-12-18 09:50:26 -07002171 if (!io_op_defs[req->opcode].async_ctx)
2172 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002173 req->io = kmalloc(sizeof(*req->io), GFP_KERNEL);
Jens Axboe06b76d42019-12-19 14:44:26 -07002174 return req->io == NULL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002175}
2176
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002177static int io_setup_async_rw(struct io_kiocb *req, ssize_t io_size,
2178 struct iovec *iovec, struct iovec *fast_iov,
2179 struct iov_iter *iter)
2180{
Jens Axboe980ad262020-01-24 23:08:54 -07002181 if (!io_op_defs[req->opcode].async_ctx)
Jens Axboe74566df2020-01-13 19:23:24 -07002182 return 0;
Jens Axboe5d204bc2020-01-31 12:06:52 -07002183 if (!req->io) {
2184 if (io_alloc_async_ctx(req))
2185 return -ENOMEM;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002186
Jens Axboe5d204bc2020-01-31 12:06:52 -07002187 io_req_map_rw(req, io_size, iovec, fast_iov, iter);
2188 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002189 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002190}
2191
Jens Axboe3529d8c2019-12-19 18:24:38 -07002192static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2193 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002194{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002195 struct io_async_ctx *io;
2196 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002197 ssize_t ret;
2198
Jens Axboe3529d8c2019-12-19 18:24:38 -07002199 ret = io_prep_rw(req, sqe, force_nonblock);
2200 if (ret)
2201 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002202
Jens Axboe3529d8c2019-12-19 18:24:38 -07002203 if (unlikely(!(req->file->f_mode & FMODE_READ)))
2204 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002205
Pavel Begunkov5f798be2020-02-08 13:28:02 +03002206 /* either don't need iovec imported or already have it */
2207 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002208 return 0;
2209
2210 io = req->io;
2211 io->rw.iov = io->rw.fast_iov;
2212 req->io = NULL;
2213 ret = io_import_iovec(READ, req, &io->rw.iov, &iter);
2214 req->io = io;
2215 if (ret < 0)
2216 return ret;
2217
2218 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2219 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002220}
2221
Pavel Begunkov267bc902019-11-07 01:41:08 +03002222static int io_read(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06002223 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002224{
2225 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002226 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002227 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002228 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002229 ssize_t io_size, ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002230
Jens Axboe3529d8c2019-12-19 18:24:38 -07002231 ret = io_import_iovec(READ, req, &iovec, &iter);
Jens Axboe06b76d42019-12-19 14:44:26 -07002232 if (ret < 0)
2233 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002234
Jens Axboefd6c2e42019-12-18 12:19:41 -07002235 /* Ensure we clear previously set non-block flag */
2236 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07002237 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002238
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002239 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002240 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06002241 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07002242 req->result = io_size;
2243
2244 /*
2245 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2246 * we know to async punt it even if it was opened O_NONBLOCK
2247 */
Jens Axboe9adbd452019-12-20 08:45:55 -07002248 if (force_nonblock && !io_file_supports_async(req->file)) {
Jens Axboef67676d2019-12-02 11:03:47 -07002249 req->flags |= REQ_F_MUST_PUNT;
2250 goto copy_iov;
2251 }
Jens Axboe9e645e112019-05-10 16:07:28 -06002252
Jens Axboe31b51512019-01-18 22:56:34 -07002253 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002254 ret = rw_verify_area(READ, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002255 if (!ret) {
2256 ssize_t ret2;
2257
Jens Axboe9adbd452019-12-20 08:45:55 -07002258 if (req->file->f_op->read_iter)
2259 ret2 = call_read_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002260 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002261 ret2 = loop_rw_iter(READ, req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002262
Jens Axboe9d93a3f2019-05-15 13:53:07 -06002263 /* Catch -EAGAIN return for forced non-blocking submission */
Jens Axboef67676d2019-12-02 11:03:47 -07002264 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002265 kiocb_done(kiocb, ret2, nxt, req->in_async);
Jens Axboef67676d2019-12-02 11:03:47 -07002266 } else {
2267copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002268 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002269 inline_vecs, &iter);
2270 if (ret)
2271 goto out_free;
2272 return -EAGAIN;
2273 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002274 }
Jens Axboef67676d2019-12-02 11:03:47 -07002275out_free:
Pavel Begunkov1e950812020-02-06 19:51:16 +03002276 kfree(iovec);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002277 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002278 return ret;
2279}
2280
Jens Axboe3529d8c2019-12-19 18:24:38 -07002281static int io_write_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
2282 bool force_nonblock)
Jens Axboef67676d2019-12-02 11:03:47 -07002283{
Jens Axboe3529d8c2019-12-19 18:24:38 -07002284 struct io_async_ctx *io;
2285 struct iov_iter iter;
Jens Axboef67676d2019-12-02 11:03:47 -07002286 ssize_t ret;
2287
Jens Axboe3529d8c2019-12-19 18:24:38 -07002288 ret = io_prep_rw(req, sqe, force_nonblock);
2289 if (ret)
2290 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07002291
Jens Axboe3529d8c2019-12-19 18:24:38 -07002292 if (unlikely(!(req->file->f_mode & FMODE_WRITE)))
2293 return -EBADF;
Jens Axboef67676d2019-12-02 11:03:47 -07002294
Pavel Begunkov5f798be2020-02-08 13:28:02 +03002295 /* either don't need iovec imported or already have it */
2296 if (!req->io || req->flags & REQ_F_NEED_CLEANUP)
Jens Axboe3529d8c2019-12-19 18:24:38 -07002297 return 0;
2298
2299 io = req->io;
2300 io->rw.iov = io->rw.fast_iov;
2301 req->io = NULL;
2302 ret = io_import_iovec(WRITE, req, &io->rw.iov, &iter);
2303 req->io = io;
2304 if (ret < 0)
2305 return ret;
2306
2307 io_req_map_rw(req, ret, io->rw.iov, io->rw.fast_iov, &iter);
2308 return 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002309}
2310
Pavel Begunkov267bc902019-11-07 01:41:08 +03002311static int io_write(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe8358e3a2019-04-23 08:17:58 -06002312 bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002313{
2314 struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs;
Jens Axboe9adbd452019-12-20 08:45:55 -07002315 struct kiocb *kiocb = &req->rw.kiocb;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002316 struct iov_iter iter;
Jens Axboe31b51512019-01-18 22:56:34 -07002317 size_t iov_count;
Jens Axboef67676d2019-12-02 11:03:47 -07002318 ssize_t ret, io_size;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002319
Jens Axboe3529d8c2019-12-19 18:24:38 -07002320 ret = io_import_iovec(WRITE, req, &iovec, &iter);
Jens Axboe06b76d42019-12-19 14:44:26 -07002321 if (ret < 0)
2322 return ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002323
Jens Axboefd6c2e42019-12-18 12:19:41 -07002324 /* Ensure we clear previously set non-block flag */
2325 if (!force_nonblock)
Jens Axboe9adbd452019-12-20 08:45:55 -07002326 req->rw.kiocb.ki_flags &= ~IOCB_NOWAIT;
Jens Axboefd6c2e42019-12-18 12:19:41 -07002327
Bijan Mottahedeh797f3f52020-01-15 18:37:45 -08002328 req->result = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07002329 io_size = ret;
Jens Axboe9e645e112019-05-10 16:07:28 -06002330 if (req->flags & REQ_F_LINK)
Jens Axboef67676d2019-12-02 11:03:47 -07002331 req->result = io_size;
2332
2333 /*
2334 * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so
2335 * we know to async punt it even if it was opened O_NONBLOCK
2336 */
2337 if (force_nonblock && !io_file_supports_async(req->file)) {
2338 req->flags |= REQ_F_MUST_PUNT;
2339 goto copy_iov;
2340 }
2341
Jens Axboe10d59342019-12-09 20:16:22 -07002342 /* file path doesn't support NOWAIT for non-direct_IO */
2343 if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT) &&
2344 (req->flags & REQ_F_ISREG))
Jens Axboef67676d2019-12-02 11:03:47 -07002345 goto copy_iov;
Jens Axboe9e645e112019-05-10 16:07:28 -06002346
Jens Axboe31b51512019-01-18 22:56:34 -07002347 iov_count = iov_iter_count(&iter);
Jens Axboe9adbd452019-12-20 08:45:55 -07002348 ret = rw_verify_area(WRITE, req->file, &kiocb->ki_pos, iov_count);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002349 if (!ret) {
Roman Penyaev9bf79332019-03-25 20:09:24 +01002350 ssize_t ret2;
2351
Jens Axboe2b188cc2019-01-07 10:46:33 -07002352 /*
2353 * Open-code file_start_write here to grab freeze protection,
2354 * which will be released by another thread in
2355 * io_complete_rw(). Fool lockdep by telling it the lock got
2356 * released so that it doesn't complain about the held lock when
2357 * we return to userspace.
2358 */
Jens Axboe491381ce2019-10-17 09:20:46 -06002359 if (req->flags & REQ_F_ISREG) {
Jens Axboe9adbd452019-12-20 08:45:55 -07002360 __sb_start_write(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002361 SB_FREEZE_WRITE, true);
Jens Axboe9adbd452019-12-20 08:45:55 -07002362 __sb_writers_release(file_inode(req->file)->i_sb,
Jens Axboe2b188cc2019-01-07 10:46:33 -07002363 SB_FREEZE_WRITE);
2364 }
2365 kiocb->ki_flags |= IOCB_WRITE;
Roman Penyaev9bf79332019-03-25 20:09:24 +01002366
Jens Axboe9adbd452019-12-20 08:45:55 -07002367 if (req->file->f_op->write_iter)
2368 ret2 = call_write_iter(req->file, kiocb, &iter);
Jens Axboe32960612019-09-23 11:05:34 -06002369 else
Jens Axboe9adbd452019-12-20 08:45:55 -07002370 ret2 = loop_rw_iter(WRITE, req->file, kiocb, &iter);
Jens Axboefaac9962020-02-07 15:45:22 -07002371 /*
2372 * Raw bdev writes will -EOPNOTSUPP for IOCB_NOWAIT. Just
2373 * retry them without IOCB_NOWAIT.
2374 */
2375 if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT))
2376 ret2 = -EAGAIN;
Jens Axboef67676d2019-12-02 11:03:47 -07002377 if (!force_nonblock || ret2 != -EAGAIN) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03002378 kiocb_done(kiocb, ret2, nxt, req->in_async);
Jens Axboef67676d2019-12-02 11:03:47 -07002379 } else {
2380copy_iov:
Jens Axboeb7bb4f72019-12-15 22:13:43 -07002381 ret = io_setup_async_rw(req, io_size, iovec,
Jens Axboef67676d2019-12-02 11:03:47 -07002382 inline_vecs, &iter);
2383 if (ret)
2384 goto out_free;
2385 return -EAGAIN;
2386 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07002387 }
Jens Axboe31b51512019-01-18 22:56:34 -07002388out_free:
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002389 req->flags &= ~REQ_F_NEED_CLEANUP;
Pavel Begunkov1e950812020-02-06 19:51:16 +03002390 kfree(iovec);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002391 return ret;
2392}
2393
2394/*
2395 * IORING_OP_NOP just posts a completion event, nothing else.
2396 */
Jens Axboe78e19bb2019-11-06 15:21:34 -07002397static int io_nop(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07002398{
2399 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2b188cc2019-01-07 10:46:33 -07002400
Jens Axboedef596e2019-01-09 08:59:42 -07002401 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2402 return -EINVAL;
2403
Jens Axboe78e19bb2019-11-06 15:21:34 -07002404 io_cqring_add_event(req, 0);
Jens Axboee65ef562019-03-12 10:16:44 -06002405 io_put_req(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07002406 return 0;
2407}
2408
Jens Axboe3529d8c2019-12-19 18:24:38 -07002409static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002410{
Jens Axboe6b063142019-01-10 22:13:58 -07002411 struct io_ring_ctx *ctx = req->ctx;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002412
Jens Axboe09bb8392019-03-13 12:39:28 -06002413 if (!req->file)
2414 return -EBADF;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002415
Jens Axboe6b063142019-01-10 22:13:58 -07002416 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboedef596e2019-01-09 08:59:42 -07002417 return -EINVAL;
Jens Axboeedafcce2019-01-09 09:16:05 -07002418 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002419 return -EINVAL;
2420
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002421 req->sync.flags = READ_ONCE(sqe->fsync_flags);
2422 if (unlikely(req->sync.flags & ~IORING_FSYNC_DATASYNC))
2423 return -EINVAL;
2424
2425 req->sync.off = READ_ONCE(sqe->off);
2426 req->sync.len = READ_ONCE(sqe->len);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002427 return 0;
2428}
2429
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002430static bool io_req_cancelled(struct io_kiocb *req)
2431{
2432 if (req->work.flags & IO_WQ_WORK_CANCEL) {
2433 req_set_fail_links(req);
2434 io_cqring_add_event(req, -ECANCELED);
2435 io_put_req(req);
2436 return true;
2437 }
2438
2439 return false;
2440}
2441
Jens Axboe78912932020-01-14 22:09:06 -07002442static void io_link_work_cb(struct io_wq_work **workptr)
2443{
2444 struct io_wq_work *work = *workptr;
2445 struct io_kiocb *link = work->data;
2446
2447 io_queue_linked_timeout(link);
2448 work->func = io_wq_submit_work;
2449}
2450
2451static void io_wq_assign_next(struct io_wq_work **workptr, struct io_kiocb *nxt)
2452{
2453 struct io_kiocb *link;
2454
2455 io_prep_async_work(nxt, &link);
2456 *workptr = &nxt->work;
2457 if (link) {
2458 nxt->work.flags |= IO_WQ_WORK_CB;
2459 nxt->work.func = io_link_work_cb;
2460 nxt->work.data = link;
2461 }
2462}
2463
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002464static void io_fsync_finish(struct io_wq_work **workptr)
2465{
2466 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2467 loff_t end = req->sync.off + req->sync.len;
2468 struct io_kiocb *nxt = NULL;
2469 int ret;
2470
2471 if (io_req_cancelled(req))
2472 return;
2473
Jens Axboe9adbd452019-12-20 08:45:55 -07002474 ret = vfs_fsync_range(req->file, req->sync.off,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002475 end > 0 ? end : LLONG_MAX,
2476 req->sync.flags & IORING_FSYNC_DATASYNC);
2477 if (ret < 0)
2478 req_set_fail_links(req);
2479 io_cqring_add_event(req, ret);
2480 io_put_req_find_next(req, &nxt);
2481 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07002482 io_wq_assign_next(workptr, nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002483}
2484
Jens Axboefc4df992019-12-10 14:38:45 -07002485static int io_fsync(struct io_kiocb *req, struct io_kiocb **nxt,
2486 bool force_nonblock)
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002487{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002488 struct io_wq_work *work, *old_work;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002489
2490 /* fsync always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002491 if (force_nonblock) {
2492 io_put_req(req);
2493 req->work.func = io_fsync_finish;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002494 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002495 }
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002496
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002497 work = old_work = &req->work;
2498 io_fsync_finish(&work);
2499 if (work && work != old_work)
2500 *nxt = container_of(work, struct io_kiocb, work);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07002501 return 0;
2502}
2503
Jens Axboed63d1b52019-12-10 10:38:56 -07002504static void io_fallocate_finish(struct io_wq_work **workptr)
2505{
2506 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2507 struct io_kiocb *nxt = NULL;
2508 int ret;
2509
Pavel Begunkov7fbeb952020-02-16 01:01:18 +03002510 if (io_req_cancelled(req))
2511 return;
2512
Jens Axboed63d1b52019-12-10 10:38:56 -07002513 ret = vfs_fallocate(req->file, req->sync.mode, req->sync.off,
2514 req->sync.len);
2515 if (ret < 0)
2516 req_set_fail_links(req);
2517 io_cqring_add_event(req, ret);
2518 io_put_req_find_next(req, &nxt);
2519 if (nxt)
2520 io_wq_assign_next(workptr, nxt);
2521}
2522
2523static int io_fallocate_prep(struct io_kiocb *req,
2524 const struct io_uring_sqe *sqe)
2525{
2526 if (sqe->ioprio || sqe->buf_index || sqe->rw_flags)
2527 return -EINVAL;
2528
2529 req->sync.off = READ_ONCE(sqe->off);
2530 req->sync.len = READ_ONCE(sqe->addr);
2531 req->sync.mode = READ_ONCE(sqe->len);
2532 return 0;
2533}
2534
2535static int io_fallocate(struct io_kiocb *req, struct io_kiocb **nxt,
2536 bool force_nonblock)
2537{
2538 struct io_wq_work *work, *old_work;
2539
2540 /* fallocate always requiring blocking context */
2541 if (force_nonblock) {
2542 io_put_req(req);
2543 req->work.func = io_fallocate_finish;
2544 return -EAGAIN;
2545 }
2546
2547 work = old_work = &req->work;
2548 io_fallocate_finish(&work);
2549 if (work && work != old_work)
2550 *nxt = container_of(work, struct io_kiocb, work);
2551
2552 return 0;
2553}
2554
Jens Axboe15b71ab2019-12-11 11:20:36 -07002555static int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2556{
Jens Axboef8748882020-01-08 17:47:02 -07002557 const char __user *fname;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002558 int ret;
2559
2560 if (sqe->ioprio || sqe->buf_index)
2561 return -EINVAL;
Jens Axboecf3040c2020-02-06 21:31:40 -07002562 if (sqe->flags & IOSQE_FIXED_FILE)
2563 return -EBADF;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03002564 if (req->flags & REQ_F_NEED_CLEANUP)
2565 return 0;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002566
2567 req->open.dfd = READ_ONCE(sqe->fd);
Jens Axboec12cedf2020-01-08 17:41:21 -07002568 req->open.how.mode = READ_ONCE(sqe->len);
Jens Axboef8748882020-01-08 17:47:02 -07002569 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboec12cedf2020-01-08 17:41:21 -07002570 req->open.how.flags = READ_ONCE(sqe->open_flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002571
Jens Axboef8748882020-01-08 17:47:02 -07002572 req->open.filename = getname(fname);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002573 if (IS_ERR(req->open.filename)) {
2574 ret = PTR_ERR(req->open.filename);
2575 req->open.filename = NULL;
2576 return ret;
2577 }
2578
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002579 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002580 return 0;
2581}
2582
Jens Axboecebdb982020-01-08 17:59:24 -07002583static int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2584{
2585 struct open_how __user *how;
2586 const char __user *fname;
2587 size_t len;
2588 int ret;
2589
2590 if (sqe->ioprio || sqe->buf_index)
2591 return -EINVAL;
Jens Axboecf3040c2020-02-06 21:31:40 -07002592 if (sqe->flags & IOSQE_FIXED_FILE)
2593 return -EBADF;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03002594 if (req->flags & REQ_F_NEED_CLEANUP)
2595 return 0;
Jens Axboecebdb982020-01-08 17:59:24 -07002596
2597 req->open.dfd = READ_ONCE(sqe->fd);
2598 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
2599 how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
2600 len = READ_ONCE(sqe->len);
2601
2602 if (len < OPEN_HOW_SIZE_VER0)
2603 return -EINVAL;
2604
2605 ret = copy_struct_from_user(&req->open.how, sizeof(req->open.how), how,
2606 len);
2607 if (ret)
2608 return ret;
2609
2610 if (!(req->open.how.flags & O_PATH) && force_o_largefile())
2611 req->open.how.flags |= O_LARGEFILE;
2612
2613 req->open.filename = getname(fname);
2614 if (IS_ERR(req->open.filename)) {
2615 ret = PTR_ERR(req->open.filename);
2616 req->open.filename = NULL;
2617 return ret;
2618 }
2619
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002620 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboecebdb982020-01-08 17:59:24 -07002621 return 0;
2622}
2623
2624static int io_openat2(struct io_kiocb *req, struct io_kiocb **nxt,
2625 bool force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07002626{
2627 struct open_flags op;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002628 struct file *file;
2629 int ret;
2630
Jens Axboef86cd202020-01-29 13:46:44 -07002631 if (force_nonblock)
Jens Axboe15b71ab2019-12-11 11:20:36 -07002632 return -EAGAIN;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002633
Jens Axboecebdb982020-01-08 17:59:24 -07002634 ret = build_open_flags(&req->open.how, &op);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002635 if (ret)
2636 goto err;
2637
Jens Axboecebdb982020-01-08 17:59:24 -07002638 ret = get_unused_fd_flags(req->open.how.flags);
Jens Axboe15b71ab2019-12-11 11:20:36 -07002639 if (ret < 0)
2640 goto err;
2641
2642 file = do_filp_open(req->open.dfd, req->open.filename, &op);
2643 if (IS_ERR(file)) {
2644 put_unused_fd(ret);
2645 ret = PTR_ERR(file);
2646 } else {
2647 fsnotify_open(file);
2648 fd_install(ret, file);
2649 }
2650err:
2651 putname(req->open.filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002652 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe15b71ab2019-12-11 11:20:36 -07002653 if (ret < 0)
2654 req_set_fail_links(req);
2655 io_cqring_add_event(req, ret);
2656 io_put_req_find_next(req, nxt);
2657 return 0;
2658}
2659
Jens Axboecebdb982020-01-08 17:59:24 -07002660static int io_openat(struct io_kiocb *req, struct io_kiocb **nxt,
2661 bool force_nonblock)
2662{
2663 req->open.how = build_open_how(req->open.how.flags, req->open.how.mode);
2664 return io_openat2(req, nxt, force_nonblock);
2665}
2666
Jens Axboe3e4827b2020-01-08 15:18:09 -07002667static int io_epoll_ctl_prep(struct io_kiocb *req,
2668 const struct io_uring_sqe *sqe)
2669{
2670#if defined(CONFIG_EPOLL)
2671 if (sqe->ioprio || sqe->buf_index)
2672 return -EINVAL;
2673
2674 req->epoll.epfd = READ_ONCE(sqe->fd);
2675 req->epoll.op = READ_ONCE(sqe->len);
2676 req->epoll.fd = READ_ONCE(sqe->off);
2677
2678 if (ep_op_has_event(req->epoll.op)) {
2679 struct epoll_event __user *ev;
2680
2681 ev = u64_to_user_ptr(READ_ONCE(sqe->addr));
2682 if (copy_from_user(&req->epoll.event, ev, sizeof(*ev)))
2683 return -EFAULT;
2684 }
2685
2686 return 0;
2687#else
2688 return -EOPNOTSUPP;
2689#endif
2690}
2691
2692static int io_epoll_ctl(struct io_kiocb *req, struct io_kiocb **nxt,
2693 bool force_nonblock)
2694{
2695#if defined(CONFIG_EPOLL)
2696 struct io_epoll *ie = &req->epoll;
2697 int ret;
2698
2699 ret = do_epoll_ctl(ie->epfd, ie->op, ie->fd, &ie->event, force_nonblock);
2700 if (force_nonblock && ret == -EAGAIN)
2701 return -EAGAIN;
2702
2703 if (ret < 0)
2704 req_set_fail_links(req);
2705 io_cqring_add_event(req, ret);
2706 io_put_req_find_next(req, nxt);
2707 return 0;
2708#else
2709 return -EOPNOTSUPP;
2710#endif
2711}
2712
Jens Axboec1ca7572019-12-25 22:18:28 -07002713static int io_madvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2714{
2715#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
2716 if (sqe->ioprio || sqe->buf_index || sqe->off)
2717 return -EINVAL;
2718
2719 req->madvise.addr = READ_ONCE(sqe->addr);
2720 req->madvise.len = READ_ONCE(sqe->len);
2721 req->madvise.advice = READ_ONCE(sqe->fadvise_advice);
2722 return 0;
2723#else
2724 return -EOPNOTSUPP;
2725#endif
2726}
2727
2728static int io_madvise(struct io_kiocb *req, struct io_kiocb **nxt,
2729 bool force_nonblock)
2730{
2731#if defined(CONFIG_ADVISE_SYSCALLS) && defined(CONFIG_MMU)
2732 struct io_madvise *ma = &req->madvise;
2733 int ret;
2734
2735 if (force_nonblock)
2736 return -EAGAIN;
2737
2738 ret = do_madvise(ma->addr, ma->len, ma->advice);
2739 if (ret < 0)
2740 req_set_fail_links(req);
2741 io_cqring_add_event(req, ret);
2742 io_put_req_find_next(req, nxt);
2743 return 0;
2744#else
2745 return -EOPNOTSUPP;
2746#endif
2747}
2748
Jens Axboe4840e412019-12-25 22:03:45 -07002749static int io_fadvise_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2750{
2751 if (sqe->ioprio || sqe->buf_index || sqe->addr)
2752 return -EINVAL;
2753
2754 req->fadvise.offset = READ_ONCE(sqe->off);
2755 req->fadvise.len = READ_ONCE(sqe->len);
2756 req->fadvise.advice = READ_ONCE(sqe->fadvise_advice);
2757 return 0;
2758}
2759
2760static int io_fadvise(struct io_kiocb *req, struct io_kiocb **nxt,
2761 bool force_nonblock)
2762{
2763 struct io_fadvise *fa = &req->fadvise;
2764 int ret;
2765
Jens Axboe3e694262020-02-01 09:22:49 -07002766 if (force_nonblock) {
2767 switch (fa->advice) {
2768 case POSIX_FADV_NORMAL:
2769 case POSIX_FADV_RANDOM:
2770 case POSIX_FADV_SEQUENTIAL:
2771 break;
2772 default:
2773 return -EAGAIN;
2774 }
2775 }
Jens Axboe4840e412019-12-25 22:03:45 -07002776
2777 ret = vfs_fadvise(req->file, fa->offset, fa->len, fa->advice);
2778 if (ret < 0)
2779 req_set_fail_links(req);
2780 io_cqring_add_event(req, ret);
2781 io_put_req_find_next(req, nxt);
2782 return 0;
2783}
2784
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002785static int io_statx_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2786{
Jens Axboef8748882020-01-08 17:47:02 -07002787 const char __user *fname;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002788 unsigned lookup_flags;
2789 int ret;
2790
2791 if (sqe->ioprio || sqe->buf_index)
2792 return -EINVAL;
Jens Axboecf3040c2020-02-06 21:31:40 -07002793 if (sqe->flags & IOSQE_FIXED_FILE)
2794 return -EBADF;
Pavel Begunkov0bdbdd02020-02-08 13:28:03 +03002795 if (req->flags & REQ_F_NEED_CLEANUP)
2796 return 0;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002797
2798 req->open.dfd = READ_ONCE(sqe->fd);
2799 req->open.mask = READ_ONCE(sqe->len);
Jens Axboef8748882020-01-08 17:47:02 -07002800 fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002801 req->open.buffer = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboec12cedf2020-01-08 17:41:21 -07002802 req->open.how.flags = READ_ONCE(sqe->statx_flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002803
Jens Axboec12cedf2020-01-08 17:41:21 -07002804 if (vfs_stat_set_lookup_flags(&lookup_flags, req->open.how.flags))
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002805 return -EINVAL;
2806
Jens Axboef8748882020-01-08 17:47:02 -07002807 req->open.filename = getname_flags(fname, lookup_flags, NULL);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002808 if (IS_ERR(req->open.filename)) {
2809 ret = PTR_ERR(req->open.filename);
2810 req->open.filename = NULL;
2811 return ret;
2812 }
2813
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002814 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002815 return 0;
2816}
2817
2818static int io_statx(struct io_kiocb *req, struct io_kiocb **nxt,
2819 bool force_nonblock)
2820{
2821 struct io_open *ctx = &req->open;
2822 unsigned lookup_flags;
2823 struct path path;
2824 struct kstat stat;
2825 int ret;
2826
2827 if (force_nonblock)
2828 return -EAGAIN;
2829
Jens Axboec12cedf2020-01-08 17:41:21 -07002830 if (vfs_stat_set_lookup_flags(&lookup_flags, ctx->how.flags))
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002831 return -EINVAL;
2832
2833retry:
2834 /* filename_lookup() drops it, keep a reference */
2835 ctx->filename->refcnt++;
2836
2837 ret = filename_lookup(ctx->dfd, ctx->filename, lookup_flags, &path,
2838 NULL);
2839 if (ret)
2840 goto err;
2841
Jens Axboec12cedf2020-01-08 17:41:21 -07002842 ret = vfs_getattr(&path, &stat, ctx->mask, ctx->how.flags);
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002843 path_put(&path);
2844 if (retry_estale(ret, lookup_flags)) {
2845 lookup_flags |= LOOKUP_REVAL;
2846 goto retry;
2847 }
2848 if (!ret)
2849 ret = cp_statx(&stat, ctx->buffer);
2850err:
2851 putname(ctx->filename);
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03002852 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07002853 if (ret < 0)
2854 req_set_fail_links(req);
2855 io_cqring_add_event(req, ret);
2856 io_put_req_find_next(req, nxt);
2857 return 0;
2858}
2859
Jens Axboeb5dba592019-12-11 14:02:38 -07002860static int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
2861{
2862 /*
2863 * If we queue this for async, it must not be cancellable. That would
2864 * leave the 'file' in an undeterminate state.
2865 */
2866 req->work.flags |= IO_WQ_WORK_NO_CANCEL;
2867
2868 if (sqe->ioprio || sqe->off || sqe->addr || sqe->len ||
2869 sqe->rw_flags || sqe->buf_index)
2870 return -EINVAL;
2871 if (sqe->flags & IOSQE_FIXED_FILE)
Jens Axboecf3040c2020-02-06 21:31:40 -07002872 return -EBADF;
Jens Axboeb5dba592019-12-11 14:02:38 -07002873
2874 req->close.fd = READ_ONCE(sqe->fd);
2875 if (req->file->f_op == &io_uring_fops ||
Pavel Begunkovb14cca02020-01-17 04:45:59 +03002876 req->close.fd == req->ctx->ring_fd)
Jens Axboeb5dba592019-12-11 14:02:38 -07002877 return -EBADF;
2878
2879 return 0;
2880}
2881
Pavel Begunkova93b3332020-02-08 14:04:34 +03002882/* only called when __close_fd_get_file() is done */
2883static void __io_close_finish(struct io_kiocb *req, struct io_kiocb **nxt)
2884{
2885 int ret;
2886
2887 ret = filp_close(req->close.put_file, req->work.files);
2888 if (ret < 0)
2889 req_set_fail_links(req);
2890 io_cqring_add_event(req, ret);
2891 fput(req->close.put_file);
2892 io_put_req_find_next(req, nxt);
2893}
2894
Jens Axboeb5dba592019-12-11 14:02:38 -07002895static void io_close_finish(struct io_wq_work **workptr)
2896{
2897 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2898 struct io_kiocb *nxt = NULL;
2899
Pavel Begunkov7fbeb952020-02-16 01:01:18 +03002900 /* not cancellable, don't do io_req_cancelled() */
Pavel Begunkova93b3332020-02-08 14:04:34 +03002901 __io_close_finish(req, &nxt);
Jens Axboeb5dba592019-12-11 14:02:38 -07002902 if (nxt)
2903 io_wq_assign_next(workptr, nxt);
2904}
2905
2906static int io_close(struct io_kiocb *req, struct io_kiocb **nxt,
2907 bool force_nonblock)
2908{
2909 int ret;
2910
2911 req->close.put_file = NULL;
2912 ret = __close_fd_get_file(req->close.fd, &req->close.put_file);
2913 if (ret < 0)
2914 return ret;
2915
2916 /* if the file has a flush method, be safe and punt to async */
Jens Axboef86cd202020-01-29 13:46:44 -07002917 if (req->close.put_file->f_op->flush && !io_wq_current_is_worker())
Jens Axboeb5dba592019-12-11 14:02:38 -07002918 goto eagain;
Jens Axboeb5dba592019-12-11 14:02:38 -07002919
2920 /*
2921 * No ->flush(), safely close from here and just punt the
2922 * fput() to async context.
2923 */
Pavel Begunkova93b3332020-02-08 14:04:34 +03002924 __io_close_finish(req, nxt);
2925 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07002926eagain:
2927 req->work.func = io_close_finish;
Jens Axboe1a417f42020-01-31 17:16:48 -07002928 /*
2929 * Do manual async queue here to avoid grabbing files - we don't
2930 * need the files, and it'll cause io_close_finish() to close
2931 * the file again and cause a double CQE entry for this request
2932 */
2933 io_queue_async_work(req);
2934 return 0;
Jens Axboeb5dba592019-12-11 14:02:38 -07002935}
2936
Jens Axboe3529d8c2019-12-19 18:24:38 -07002937static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002938{
2939 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002940
2941 if (!req->file)
2942 return -EBADF;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002943
2944 if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
2945 return -EINVAL;
2946 if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
2947 return -EINVAL;
2948
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002949 req->sync.off = READ_ONCE(sqe->off);
2950 req->sync.len = READ_ONCE(sqe->len);
2951 req->sync.flags = READ_ONCE(sqe->sync_range_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002952 return 0;
2953}
2954
2955static void io_sync_file_range_finish(struct io_wq_work **workptr)
2956{
2957 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
2958 struct io_kiocb *nxt = NULL;
2959 int ret;
2960
2961 if (io_req_cancelled(req))
2962 return;
2963
Jens Axboe9adbd452019-12-20 08:45:55 -07002964 ret = sync_file_range(req->file, req->sync.off, req->sync.len,
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002965 req->sync.flags);
2966 if (ret < 0)
2967 req_set_fail_links(req);
2968 io_cqring_add_event(req, ret);
2969 io_put_req_find_next(req, &nxt);
2970 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07002971 io_wq_assign_next(workptr, nxt);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002972}
2973
Jens Axboefc4df992019-12-10 14:38:45 -07002974static int io_sync_file_range(struct io_kiocb *req, struct io_kiocb **nxt,
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002975 bool force_nonblock)
2976{
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002977 struct io_wq_work *work, *old_work;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002978
2979 /* sync_file_range always requires a blocking context */
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002980 if (force_nonblock) {
2981 io_put_req(req);
2982 req->work.func = io_sync_file_range_finish;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002983 return -EAGAIN;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002984 }
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002985
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07002986 work = old_work = &req->work;
2987 io_sync_file_range_finish(&work);
2988 if (work && work != old_work)
2989 *nxt = container_of(work, struct io_kiocb, work);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06002990 return 0;
2991}
2992
Jens Axboe3529d8c2019-12-19 18:24:38 -07002993static int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboeaa1fa282019-04-19 13:38:09 -06002994{
Jens Axboe03b12302019-12-02 18:50:25 -07002995#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07002996 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07002997 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03002998 int ret;
Jens Axboe03b12302019-12-02 18:50:25 -07002999
Jens Axboee47293f2019-12-20 08:58:21 -07003000 sr->msg_flags = READ_ONCE(sqe->msg_flags);
3001 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboefddafac2020-01-04 20:19:44 -07003002 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003003
Jens Axboefddafac2020-01-04 20:19:44 -07003004 if (!io || req->opcode == IORING_OP_SEND)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003005 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003006 /* iovec is already imported */
3007 if (req->flags & REQ_F_NEED_CLEANUP)
3008 return 0;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003009
Jens Axboed9688562019-12-09 19:35:20 -07003010 io->msg.iov = io->msg.fast_iov;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003011 ret = sendmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07003012 &io->msg.iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003013 if (!ret)
3014 req->flags |= REQ_F_NEED_CLEANUP;
3015 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003016#else
Jens Axboee47293f2019-12-20 08:58:21 -07003017 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07003018#endif
3019}
3020
Jens Axboefc4df992019-12-10 14:38:45 -07003021static int io_sendmsg(struct io_kiocb *req, struct io_kiocb **nxt,
3022 bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07003023{
3024#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07003025 struct io_async_msghdr *kmsg = NULL;
Jens Axboe03b12302019-12-02 18:50:25 -07003026 struct socket *sock;
3027 int ret;
3028
3029 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3030 return -EINVAL;
3031
3032 sock = sock_from_file(req->file, &ret);
3033 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003034 struct io_async_ctx io;
Jens Axboe03b12302019-12-02 18:50:25 -07003035 unsigned flags;
3036
Jens Axboe03b12302019-12-02 18:50:25 -07003037 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07003038 kmsg = &req->io->msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003039 kmsg->msg.msg_name = &req->io->msg.addr;
Jens Axboe0b416c32019-12-15 10:57:46 -07003040 /* if iov is set, it's allocated already */
3041 if (!kmsg->iov)
3042 kmsg->iov = kmsg->fast_iov;
3043 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07003044 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003045 struct io_sr_msg *sr = &req->sr_msg;
3046
Jens Axboe0b416c32019-12-15 10:57:46 -07003047 kmsg = &io.msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003048 kmsg->msg.msg_name = &io.msg.addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003049
3050 io.msg.iov = io.msg.fast_iov;
3051 ret = sendmsg_copy_msghdr(&io.msg.msg, sr->msg,
3052 sr->msg_flags, &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07003053 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003054 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003055 }
3056
Jens Axboee47293f2019-12-20 08:58:21 -07003057 flags = req->sr_msg.msg_flags;
3058 if (flags & MSG_DONTWAIT)
3059 req->flags |= REQ_F_NOWAIT;
3060 else if (force_nonblock)
3061 flags |= MSG_DONTWAIT;
3062
Jens Axboe0b416c32019-12-15 10:57:46 -07003063 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
Jens Axboe03b12302019-12-02 18:50:25 -07003064 if (force_nonblock && ret == -EAGAIN) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003065 if (req->io)
3066 return -EAGAIN;
Pavel Begunkov1e950812020-02-06 19:51:16 +03003067 if (io_alloc_async_ctx(req)) {
Dan Carpenter297a31e2020-02-17 17:39:45 +03003068 if (kmsg->iov != kmsg->fast_iov)
Pavel Begunkov1e950812020-02-06 19:51:16 +03003069 kfree(kmsg->iov);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003070 return -ENOMEM;
Pavel Begunkov1e950812020-02-06 19:51:16 +03003071 }
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003072 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003073 memcpy(&req->io->msg, &io.msg, sizeof(io.msg));
Jens Axboe0b416c32019-12-15 10:57:46 -07003074 return -EAGAIN;
Jens Axboe03b12302019-12-02 18:50:25 -07003075 }
3076 if (ret == -ERESTARTSYS)
3077 ret = -EINTR;
3078 }
3079
Pavel Begunkov1e950812020-02-06 19:51:16 +03003080 if (kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07003081 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003082 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe03b12302019-12-02 18:50:25 -07003083 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003084 if (ret < 0)
3085 req_set_fail_links(req);
Jens Axboe03b12302019-12-02 18:50:25 -07003086 io_put_req_find_next(req, nxt);
3087 return 0;
3088#else
3089 return -EOPNOTSUPP;
3090#endif
3091}
3092
Jens Axboefddafac2020-01-04 20:19:44 -07003093static int io_send(struct io_kiocb *req, struct io_kiocb **nxt,
3094 bool force_nonblock)
3095{
3096#if defined(CONFIG_NET)
3097 struct socket *sock;
3098 int ret;
3099
3100 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3101 return -EINVAL;
3102
3103 sock = sock_from_file(req->file, &ret);
3104 if (sock) {
3105 struct io_sr_msg *sr = &req->sr_msg;
3106 struct msghdr msg;
3107 struct iovec iov;
3108 unsigned flags;
3109
3110 ret = import_single_range(WRITE, sr->buf, sr->len, &iov,
3111 &msg.msg_iter);
3112 if (ret)
3113 return ret;
3114
3115 msg.msg_name = NULL;
3116 msg.msg_control = NULL;
3117 msg.msg_controllen = 0;
3118 msg.msg_namelen = 0;
3119
3120 flags = req->sr_msg.msg_flags;
3121 if (flags & MSG_DONTWAIT)
3122 req->flags |= REQ_F_NOWAIT;
3123 else if (force_nonblock)
3124 flags |= MSG_DONTWAIT;
3125
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003126 msg.msg_flags = flags;
3127 ret = sock_sendmsg(sock, &msg);
Jens Axboefddafac2020-01-04 20:19:44 -07003128 if (force_nonblock && ret == -EAGAIN)
3129 return -EAGAIN;
3130 if (ret == -ERESTARTSYS)
3131 ret = -EINTR;
3132 }
3133
3134 io_cqring_add_event(req, ret);
3135 if (ret < 0)
3136 req_set_fail_links(req);
3137 io_put_req_find_next(req, nxt);
3138 return 0;
3139#else
3140 return -EOPNOTSUPP;
3141#endif
3142}
3143
Jens Axboe3529d8c2019-12-19 18:24:38 -07003144static int io_recvmsg_prep(struct io_kiocb *req,
3145 const struct io_uring_sqe *sqe)
Jens Axboe03b12302019-12-02 18:50:25 -07003146{
3147#if defined(CONFIG_NET)
Jens Axboee47293f2019-12-20 08:58:21 -07003148 struct io_sr_msg *sr = &req->sr_msg;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003149 struct io_async_ctx *io = req->io;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003150 int ret;
Jens Axboe06b76d42019-12-19 14:44:26 -07003151
Jens Axboe3529d8c2019-12-19 18:24:38 -07003152 sr->msg_flags = READ_ONCE(sqe->msg_flags);
3153 sr->msg = u64_to_user_ptr(READ_ONCE(sqe->addr));
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003154 sr->len = READ_ONCE(sqe->len);
Jens Axboe3529d8c2019-12-19 18:24:38 -07003155
Jens Axboefddafac2020-01-04 20:19:44 -07003156 if (!io || req->opcode == IORING_OP_RECV)
Jens Axboe06b76d42019-12-19 14:44:26 -07003157 return 0;
Pavel Begunkov5f798be2020-02-08 13:28:02 +03003158 /* iovec is already imported */
3159 if (req->flags & REQ_F_NEED_CLEANUP)
3160 return 0;
Jens Axboe03b12302019-12-02 18:50:25 -07003161
Jens Axboed9688562019-12-09 19:35:20 -07003162 io->msg.iov = io->msg.fast_iov;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003163 ret = recvmsg_copy_msghdr(&io->msg.msg, sr->msg, sr->msg_flags,
Jens Axboee47293f2019-12-20 08:58:21 -07003164 &io->msg.uaddr, &io->msg.iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003165 if (!ret)
3166 req->flags |= REQ_F_NEED_CLEANUP;
3167 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003168#else
Jens Axboee47293f2019-12-20 08:58:21 -07003169 return -EOPNOTSUPP;
Jens Axboe03b12302019-12-02 18:50:25 -07003170#endif
3171}
3172
Jens Axboefc4df992019-12-10 14:38:45 -07003173static int io_recvmsg(struct io_kiocb *req, struct io_kiocb **nxt,
3174 bool force_nonblock)
Jens Axboe03b12302019-12-02 18:50:25 -07003175{
3176#if defined(CONFIG_NET)
Jens Axboe0b416c32019-12-15 10:57:46 -07003177 struct io_async_msghdr *kmsg = NULL;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003178 struct socket *sock;
3179 int ret;
3180
3181 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3182 return -EINVAL;
3183
3184 sock = sock_from_file(req->file, &ret);
3185 if (sock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003186 struct io_async_ctx io;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003187 unsigned flags;
3188
Jens Axboe03b12302019-12-02 18:50:25 -07003189 if (req->io) {
Jens Axboe0b416c32019-12-15 10:57:46 -07003190 kmsg = &req->io->msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003191 kmsg->msg.msg_name = &req->io->msg.addr;
Jens Axboe0b416c32019-12-15 10:57:46 -07003192 /* if iov is set, it's allocated already */
3193 if (!kmsg->iov)
3194 kmsg->iov = kmsg->fast_iov;
3195 kmsg->msg.msg_iter.iov = kmsg->iov;
Jens Axboe03b12302019-12-02 18:50:25 -07003196 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003197 struct io_sr_msg *sr = &req->sr_msg;
3198
Jens Axboe0b416c32019-12-15 10:57:46 -07003199 kmsg = &io.msg;
Jens Axboeb5379162020-02-09 11:29:15 -07003200 kmsg->msg.msg_name = &io.msg.addr;
Jens Axboe3529d8c2019-12-19 18:24:38 -07003201
3202 io.msg.iov = io.msg.fast_iov;
3203 ret = recvmsg_copy_msghdr(&io.msg.msg, sr->msg,
3204 sr->msg_flags, &io.msg.uaddr,
3205 &io.msg.iov);
Jens Axboe03b12302019-12-02 18:50:25 -07003206 if (ret)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003207 return ret;
Jens Axboe03b12302019-12-02 18:50:25 -07003208 }
Jens Axboe0fa03c62019-04-19 13:34:07 -06003209
Jens Axboee47293f2019-12-20 08:58:21 -07003210 flags = req->sr_msg.msg_flags;
3211 if (flags & MSG_DONTWAIT)
3212 req->flags |= REQ_F_NOWAIT;
3213 else if (force_nonblock)
3214 flags |= MSG_DONTWAIT;
3215
3216 ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.msg,
3217 kmsg->uaddr, flags);
Jens Axboe03b12302019-12-02 18:50:25 -07003218 if (force_nonblock && ret == -EAGAIN) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003219 if (req->io)
3220 return -EAGAIN;
Pavel Begunkov1e950812020-02-06 19:51:16 +03003221 if (io_alloc_async_ctx(req)) {
Dan Carpenter297a31e2020-02-17 17:39:45 +03003222 if (kmsg->iov != kmsg->fast_iov)
Pavel Begunkov1e950812020-02-06 19:51:16 +03003223 kfree(kmsg->iov);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003224 return -ENOMEM;
Pavel Begunkov1e950812020-02-06 19:51:16 +03003225 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003226 memcpy(&req->io->msg, &io.msg, sizeof(io.msg));
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003227 req->flags |= REQ_F_NEED_CLEANUP;
Jens Axboe0b416c32019-12-15 10:57:46 -07003228 return -EAGAIN;
Jens Axboe03b12302019-12-02 18:50:25 -07003229 }
Jens Axboe441cdbd2019-12-02 18:49:10 -07003230 if (ret == -ERESTARTSYS)
3231 ret = -EINTR;
Jens Axboe0fa03c62019-04-19 13:34:07 -06003232 }
3233
Pavel Begunkov1e950812020-02-06 19:51:16 +03003234 if (kmsg && kmsg->iov != kmsg->fast_iov)
Jens Axboe0b416c32019-12-15 10:57:46 -07003235 kfree(kmsg->iov);
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03003236 req->flags &= ~REQ_F_NEED_CLEANUP;
Jens Axboe78e19bb2019-11-06 15:21:34 -07003237 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003238 if (ret < 0)
3239 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003240 io_put_req_find_next(req, nxt);
Jens Axboe0fa03c62019-04-19 13:34:07 -06003241 return 0;
3242#else
3243 return -EOPNOTSUPP;
3244#endif
3245}
3246
Jens Axboefddafac2020-01-04 20:19:44 -07003247static int io_recv(struct io_kiocb *req, struct io_kiocb **nxt,
3248 bool force_nonblock)
3249{
3250#if defined(CONFIG_NET)
3251 struct socket *sock;
3252 int ret;
3253
3254 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3255 return -EINVAL;
3256
3257 sock = sock_from_file(req->file, &ret);
3258 if (sock) {
3259 struct io_sr_msg *sr = &req->sr_msg;
3260 struct msghdr msg;
3261 struct iovec iov;
3262 unsigned flags;
3263
3264 ret = import_single_range(READ, sr->buf, sr->len, &iov,
3265 &msg.msg_iter);
3266 if (ret)
3267 return ret;
3268
3269 msg.msg_name = NULL;
3270 msg.msg_control = NULL;
3271 msg.msg_controllen = 0;
3272 msg.msg_namelen = 0;
3273 msg.msg_iocb = NULL;
3274 msg.msg_flags = 0;
3275
3276 flags = req->sr_msg.msg_flags;
3277 if (flags & MSG_DONTWAIT)
3278 req->flags |= REQ_F_NOWAIT;
3279 else if (force_nonblock)
3280 flags |= MSG_DONTWAIT;
3281
Jens Axboe0b7b21e2020-01-31 08:34:59 -07003282 ret = sock_recvmsg(sock, &msg, flags);
Jens Axboefddafac2020-01-04 20:19:44 -07003283 if (force_nonblock && ret == -EAGAIN)
3284 return -EAGAIN;
3285 if (ret == -ERESTARTSYS)
3286 ret = -EINTR;
3287 }
3288
3289 io_cqring_add_event(req, ret);
3290 if (ret < 0)
3291 req_set_fail_links(req);
3292 io_put_req_find_next(req, nxt);
3293 return 0;
3294#else
3295 return -EOPNOTSUPP;
3296#endif
3297}
3298
3299
Jens Axboe3529d8c2019-12-19 18:24:38 -07003300static int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003301{
3302#if defined(CONFIG_NET)
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003303 struct io_accept *accept = &req->accept;
3304
Jens Axboe17f2fe32019-10-17 14:42:58 -06003305 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3306 return -EINVAL;
Hrvoje Zeba8042d6c2019-11-25 14:40:22 -05003307 if (sqe->ioprio || sqe->len || sqe->buf_index)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003308 return -EINVAL;
3309
Jens Axboed55e5f52019-12-11 16:12:15 -07003310 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3311 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003312 accept->flags = READ_ONCE(sqe->accept_flags);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003313 return 0;
3314#else
3315 return -EOPNOTSUPP;
3316#endif
3317}
Jens Axboe17f2fe32019-10-17 14:42:58 -06003318
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003319#if defined(CONFIG_NET)
3320static int __io_accept(struct io_kiocb *req, struct io_kiocb **nxt,
3321 bool force_nonblock)
3322{
3323 struct io_accept *accept = &req->accept;
3324 unsigned file_flags;
3325 int ret;
3326
3327 file_flags = force_nonblock ? O_NONBLOCK : 0;
3328 ret = __sys_accept4_file(req->file, file_flags, accept->addr,
3329 accept->addr_len, accept->flags);
3330 if (ret == -EAGAIN && force_nonblock)
Jens Axboe17f2fe32019-10-17 14:42:58 -06003331 return -EAGAIN;
Jens Axboe8e3cca12019-11-09 19:52:33 -07003332 if (ret == -ERESTARTSYS)
3333 ret = -EINTR;
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003334 if (ret < 0)
3335 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07003336 io_cqring_add_event(req, ret);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003337 io_put_req_find_next(req, nxt);
Jens Axboe17f2fe32019-10-17 14:42:58 -06003338 return 0;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003339}
3340
3341static void io_accept_finish(struct io_wq_work **workptr)
3342{
3343 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3344 struct io_kiocb *nxt = NULL;
3345
3346 if (io_req_cancelled(req))
3347 return;
3348 __io_accept(req, &nxt, false);
3349 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07003350 io_wq_assign_next(workptr, nxt);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003351}
3352#endif
3353
3354static int io_accept(struct io_kiocb *req, struct io_kiocb **nxt,
3355 bool force_nonblock)
3356{
3357#if defined(CONFIG_NET)
3358 int ret;
3359
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003360 ret = __io_accept(req, nxt, force_nonblock);
3361 if (ret == -EAGAIN && force_nonblock) {
3362 req->work.func = io_accept_finish;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07003363 io_put_req(req);
3364 return -EAGAIN;
3365 }
3366 return 0;
Jens Axboe17f2fe32019-10-17 14:42:58 -06003367#else
3368 return -EOPNOTSUPP;
3369#endif
3370}
3371
Jens Axboe3529d8c2019-12-19 18:24:38 -07003372static int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboef499a022019-12-02 16:28:46 -07003373{
3374#if defined(CONFIG_NET)
Jens Axboe3529d8c2019-12-19 18:24:38 -07003375 struct io_connect *conn = &req->connect;
3376 struct io_async_ctx *io = req->io;
Jens Axboef499a022019-12-02 16:28:46 -07003377
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003378 if (unlikely(req->ctx->flags & (IORING_SETUP_IOPOLL|IORING_SETUP_SQPOLL)))
3379 return -EINVAL;
3380 if (sqe->ioprio || sqe->len || sqe->buf_index || sqe->rw_flags)
3381 return -EINVAL;
3382
Jens Axboe3529d8c2019-12-19 18:24:38 -07003383 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
3384 conn->addr_len = READ_ONCE(sqe->addr2);
3385
3386 if (!io)
3387 return 0;
3388
3389 return move_addr_to_kernel(conn->addr, conn->addr_len,
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003390 &io->connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07003391#else
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003392 return -EOPNOTSUPP;
Jens Axboef499a022019-12-02 16:28:46 -07003393#endif
3394}
3395
Jens Axboefc4df992019-12-10 14:38:45 -07003396static int io_connect(struct io_kiocb *req, struct io_kiocb **nxt,
3397 bool force_nonblock)
Jens Axboef8e85cf2019-11-23 14:24:24 -07003398{
3399#if defined(CONFIG_NET)
Jens Axboef499a022019-12-02 16:28:46 -07003400 struct io_async_ctx __io, *io;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003401 unsigned file_flags;
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003402 int ret;
Jens Axboef8e85cf2019-11-23 14:24:24 -07003403
Jens Axboef499a022019-12-02 16:28:46 -07003404 if (req->io) {
3405 io = req->io;
3406 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07003407 ret = move_addr_to_kernel(req->connect.addr,
3408 req->connect.addr_len,
3409 &__io.connect.address);
Jens Axboef499a022019-12-02 16:28:46 -07003410 if (ret)
3411 goto out;
3412 io = &__io;
3413 }
3414
Jens Axboe3fbb51c2019-12-20 08:51:52 -07003415 file_flags = force_nonblock ? O_NONBLOCK : 0;
3416
3417 ret = __sys_connect_file(req->file, &io->connect.address,
3418 req->connect.addr_len, file_flags);
Jens Axboe87f80d62019-12-03 11:23:54 -07003419 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003420 if (req->io)
3421 return -EAGAIN;
3422 if (io_alloc_async_ctx(req)) {
Jens Axboef499a022019-12-02 16:28:46 -07003423 ret = -ENOMEM;
3424 goto out;
3425 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07003426 memcpy(&req->io->connect, &__io.connect, sizeof(__io.connect));
Jens Axboef8e85cf2019-11-23 14:24:24 -07003427 return -EAGAIN;
Jens Axboef499a022019-12-02 16:28:46 -07003428 }
Jens Axboef8e85cf2019-11-23 14:24:24 -07003429 if (ret == -ERESTARTSYS)
3430 ret = -EINTR;
Jens Axboef499a022019-12-02 16:28:46 -07003431out:
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003432 if (ret < 0)
3433 req_set_fail_links(req);
Jens Axboef8e85cf2019-11-23 14:24:24 -07003434 io_cqring_add_event(req, ret);
3435 io_put_req_find_next(req, nxt);
3436 return 0;
3437#else
3438 return -EOPNOTSUPP;
3439#endif
3440}
3441
Jens Axboe221c5eb2019-01-17 09:41:58 -07003442static void io_poll_remove_one(struct io_kiocb *req)
3443{
3444 struct io_poll_iocb *poll = &req->poll;
3445
3446 spin_lock(&poll->head->lock);
3447 WRITE_ONCE(poll->canceled, true);
Jens Axboe392edb42019-12-09 17:52:20 -07003448 if (!list_empty(&poll->wait.entry)) {
3449 list_del_init(&poll->wait.entry);
Jackie Liua197f662019-11-08 08:09:12 -07003450 io_queue_async_work(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003451 }
3452 spin_unlock(&poll->head->lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07003453 hash_del(&req->hash_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003454}
3455
3456static void io_poll_remove_all(struct io_ring_ctx *ctx)
3457{
Jens Axboe78076bb2019-12-04 19:56:40 -07003458 struct hlist_node *tmp;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003459 struct io_kiocb *req;
Jens Axboe78076bb2019-12-04 19:56:40 -07003460 int i;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003461
3462 spin_lock_irq(&ctx->completion_lock);
Jens Axboe78076bb2019-12-04 19:56:40 -07003463 for (i = 0; i < (1U << ctx->cancel_hash_bits); i++) {
3464 struct hlist_head *list;
3465
3466 list = &ctx->cancel_hash[i];
3467 hlist_for_each_entry_safe(req, tmp, list, hash_node)
3468 io_poll_remove_one(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003469 }
3470 spin_unlock_irq(&ctx->completion_lock);
3471}
3472
Jens Axboe47f46762019-11-09 17:43:02 -07003473static int io_poll_cancel(struct io_ring_ctx *ctx, __u64 sqe_addr)
3474{
Jens Axboe78076bb2019-12-04 19:56:40 -07003475 struct hlist_head *list;
Jens Axboe47f46762019-11-09 17:43:02 -07003476 struct io_kiocb *req;
3477
Jens Axboe78076bb2019-12-04 19:56:40 -07003478 list = &ctx->cancel_hash[hash_long(sqe_addr, ctx->cancel_hash_bits)];
3479 hlist_for_each_entry(req, list, hash_node) {
3480 if (sqe_addr == req->user_data) {
Jens Axboeeac406c2019-11-14 12:09:58 -07003481 io_poll_remove_one(req);
3482 return 0;
3483 }
Jens Axboe47f46762019-11-09 17:43:02 -07003484 }
3485
3486 return -ENOENT;
3487}
3488
Jens Axboe3529d8c2019-12-19 18:24:38 -07003489static int io_poll_remove_prep(struct io_kiocb *req,
3490 const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003491{
Jens Axboe221c5eb2019-01-17 09:41:58 -07003492 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3493 return -EINVAL;
3494 if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index ||
3495 sqe->poll_events)
3496 return -EINVAL;
3497
Jens Axboe0969e782019-12-17 18:40:57 -07003498 req->poll.addr = READ_ONCE(sqe->addr);
Jens Axboe0969e782019-12-17 18:40:57 -07003499 return 0;
3500}
3501
3502/*
3503 * Find a running poll command that matches one specified in sqe->addr,
3504 * and remove it if found.
3505 */
3506static int io_poll_remove(struct io_kiocb *req)
3507{
3508 struct io_ring_ctx *ctx = req->ctx;
3509 u64 addr;
3510 int ret;
3511
Jens Axboe0969e782019-12-17 18:40:57 -07003512 addr = req->poll.addr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003513 spin_lock_irq(&ctx->completion_lock);
Jens Axboe0969e782019-12-17 18:40:57 -07003514 ret = io_poll_cancel(ctx, addr);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003515 spin_unlock_irq(&ctx->completion_lock);
3516
Jens Axboe78e19bb2019-11-06 15:21:34 -07003517 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003518 if (ret < 0)
3519 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06003520 io_put_req(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003521 return 0;
3522}
3523
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003524static void io_poll_complete(struct io_kiocb *req, __poll_t mask, int error)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003525{
Jackie Liua197f662019-11-08 08:09:12 -07003526 struct io_ring_ctx *ctx = req->ctx;
3527
Jens Axboe8c838782019-03-12 15:48:16 -06003528 req->poll.done = true;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003529 if (error)
3530 io_cqring_fill_event(req, error);
3531 else
3532 io_cqring_fill_event(req, mangle_poll(mask));
Jens Axboe8c838782019-03-12 15:48:16 -06003533 io_commit_cqring(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003534}
3535
Jens Axboe561fb042019-10-24 07:25:42 -06003536static void io_poll_complete_work(struct io_wq_work **workptr)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003537{
Jens Axboe561fb042019-10-24 07:25:42 -06003538 struct io_wq_work *work = *workptr;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003539 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
3540 struct io_poll_iocb *poll = &req->poll;
3541 struct poll_table_struct pt = { ._key = poll->events };
3542 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe89723d02019-11-05 15:32:58 -07003543 struct io_kiocb *nxt = NULL;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003544 __poll_t mask = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003545 int ret = 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003546
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003547 if (work->flags & IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06003548 WRITE_ONCE(poll->canceled, true);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003549 ret = -ECANCELED;
3550 } else if (READ_ONCE(poll->canceled)) {
3551 ret = -ECANCELED;
3552 }
Jens Axboe561fb042019-10-24 07:25:42 -06003553
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003554 if (ret != -ECANCELED)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003555 mask = vfs_poll(poll->file, &pt) & poll->events;
3556
3557 /*
3558 * Note that ->ki_cancel callers also delete iocb from active_reqs after
3559 * calling ->ki_cancel. We need the ctx_lock roundtrip here to
3560 * synchronize with them. In the cancellation case the list_del_init
3561 * itself is not actually needed, but harmless so we keep it in to
3562 * avoid further branches in the fast path.
3563 */
3564 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003565 if (!mask && ret != -ECANCELED) {
Jens Axboe392edb42019-12-09 17:52:20 -07003566 add_wait_queue(poll->head, &poll->wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003567 spin_unlock_irq(&ctx->completion_lock);
3568 return;
3569 }
Jens Axboe78076bb2019-12-04 19:56:40 -07003570 hash_del(&req->hash_node);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003571 io_poll_complete(req, mask, ret);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003572 spin_unlock_irq(&ctx->completion_lock);
3573
Jens Axboe8c838782019-03-12 15:48:16 -06003574 io_cqring_ev_posted(ctx);
Jens Axboe89723d02019-11-05 15:32:58 -07003575
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003576 if (ret < 0)
3577 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003578 io_put_req_find_next(req, &nxt);
Jens Axboe89723d02019-11-05 15:32:58 -07003579 if (nxt)
Jens Axboe78912932020-01-14 22:09:06 -07003580 io_wq_assign_next(workptr, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003581}
3582
Jens Axboee94f1412019-12-19 12:06:02 -07003583static void __io_poll_flush(struct io_ring_ctx *ctx, struct llist_node *nodes)
3584{
Jens Axboee94f1412019-12-19 12:06:02 -07003585 struct io_kiocb *req, *tmp;
Jens Axboe8237e042019-12-28 10:48:22 -07003586 struct req_batch rb;
Jens Axboee94f1412019-12-19 12:06:02 -07003587
Jens Axboec6ca97b302019-12-28 12:11:08 -07003588 rb.to_free = rb.need_iter = 0;
Jens Axboee94f1412019-12-19 12:06:02 -07003589 spin_lock_irq(&ctx->completion_lock);
3590 llist_for_each_entry_safe(req, tmp, nodes, llist_node) {
3591 hash_del(&req->hash_node);
3592 io_poll_complete(req, req->result, 0);
3593
Jens Axboe8237e042019-12-28 10:48:22 -07003594 if (refcount_dec_and_test(&req->refs) &&
3595 !io_req_multi_free(&rb, req)) {
3596 req->flags |= REQ_F_COMP_LOCKED;
3597 io_free_req(req);
Jens Axboee94f1412019-12-19 12:06:02 -07003598 }
3599 }
3600 spin_unlock_irq(&ctx->completion_lock);
3601
3602 io_cqring_ev_posted(ctx);
Jens Axboe8237e042019-12-28 10:48:22 -07003603 io_free_req_many(ctx, &rb);
Jens Axboee94f1412019-12-19 12:06:02 -07003604}
3605
3606static void io_poll_flush(struct io_wq_work **workptr)
3607{
3608 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3609 struct llist_node *nodes;
3610
3611 nodes = llist_del_all(&req->ctx->poll_llist);
3612 if (nodes)
3613 __io_poll_flush(req->ctx, nodes);
3614}
3615
Jens Axboef0b493e2020-02-01 21:30:11 -07003616static void io_poll_trigger_evfd(struct io_wq_work **workptr)
3617{
3618 struct io_kiocb *req = container_of(*workptr, struct io_kiocb, work);
3619
3620 eventfd_signal(req->ctx->cq_ev_fd, 1);
3621 io_put_req(req);
3622}
3623
Jens Axboe221c5eb2019-01-17 09:41:58 -07003624static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
3625 void *key)
3626{
Jens Axboee9444752019-11-26 15:02:04 -07003627 struct io_poll_iocb *poll = wait->private;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003628 struct io_kiocb *req = container_of(poll, struct io_kiocb, poll);
3629 struct io_ring_ctx *ctx = req->ctx;
3630 __poll_t mask = key_to_poll(key);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003631
3632 /* for instances that support it check for an event match first: */
Jens Axboe8c838782019-03-12 15:48:16 -06003633 if (mask && !(mask & poll->events))
3634 return 0;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003635
Jens Axboe392edb42019-12-09 17:52:20 -07003636 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06003637
Jens Axboe7c9e7f02019-11-12 08:15:53 -07003638 /*
3639 * Run completion inline if we can. We're using trylock here because
3640 * we are violating the completion_lock -> poll wq lock ordering.
3641 * If we have a link timeout we're going to need the completion_lock
3642 * for finalizing the request, mark us as having grabbed that already.
3643 */
Jens Axboee94f1412019-12-19 12:06:02 -07003644 if (mask) {
3645 unsigned long flags;
Jens Axboe8c838782019-03-12 15:48:16 -06003646
Jens Axboee94f1412019-12-19 12:06:02 -07003647 if (llist_empty(&ctx->poll_llist) &&
3648 spin_trylock_irqsave(&ctx->completion_lock, flags)) {
Jens Axboef0b493e2020-02-01 21:30:11 -07003649 bool trigger_ev;
3650
Jens Axboee94f1412019-12-19 12:06:02 -07003651 hash_del(&req->hash_node);
3652 io_poll_complete(req, mask, 0);
Jens Axboee94f1412019-12-19 12:06:02 -07003653
Jens Axboef0b493e2020-02-01 21:30:11 -07003654 trigger_ev = io_should_trigger_evfd(ctx);
3655 if (trigger_ev && eventfd_signal_count()) {
3656 trigger_ev = false;
3657 req->work.func = io_poll_trigger_evfd;
3658 } else {
3659 req->flags |= REQ_F_COMP_LOCKED;
3660 io_put_req(req);
3661 req = NULL;
3662 }
3663 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3664 __io_cqring_ev_posted(ctx, trigger_ev);
Jens Axboee94f1412019-12-19 12:06:02 -07003665 } else {
3666 req->result = mask;
3667 req->llist_node.next = NULL;
3668 /* if the list wasn't empty, we're done */
3669 if (!llist_add(&req->llist_node, &ctx->poll_llist))
3670 req = NULL;
3671 else
3672 req->work.func = io_poll_flush;
3673 }
Jens Axboe8c838782019-03-12 15:48:16 -06003674 }
Jens Axboee94f1412019-12-19 12:06:02 -07003675 if (req)
3676 io_queue_async_work(req);
Jens Axboe8c838782019-03-12 15:48:16 -06003677
Jens Axboe221c5eb2019-01-17 09:41:58 -07003678 return 1;
3679}
3680
3681struct io_poll_table {
3682 struct poll_table_struct pt;
3683 struct io_kiocb *req;
3684 int error;
3685};
3686
3687static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head,
3688 struct poll_table_struct *p)
3689{
3690 struct io_poll_table *pt = container_of(p, struct io_poll_table, pt);
3691
3692 if (unlikely(pt->req->poll.head)) {
3693 pt->error = -EINVAL;
3694 return;
3695 }
3696
3697 pt->error = 0;
3698 pt->req->poll.head = head;
Jens Axboe392edb42019-12-09 17:52:20 -07003699 add_wait_queue(head, &pt->req->poll.wait);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003700}
3701
Jens Axboeeac406c2019-11-14 12:09:58 -07003702static void io_poll_req_insert(struct io_kiocb *req)
3703{
3704 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe78076bb2019-12-04 19:56:40 -07003705 struct hlist_head *list;
Jens Axboeeac406c2019-11-14 12:09:58 -07003706
Jens Axboe78076bb2019-12-04 19:56:40 -07003707 list = &ctx->cancel_hash[hash_long(req->user_data, ctx->cancel_hash_bits)];
3708 hlist_add_head(&req->hash_node, list);
Jens Axboeeac406c2019-11-14 12:09:58 -07003709}
3710
Jens Axboe3529d8c2019-12-19 18:24:38 -07003711static int io_poll_add_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe221c5eb2019-01-17 09:41:58 -07003712{
3713 struct io_poll_iocb *poll = &req->poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003714 u16 events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003715
3716 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3717 return -EINVAL;
3718 if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index)
3719 return -EINVAL;
Jens Axboe09bb8392019-03-13 12:39:28 -06003720 if (!poll->file)
3721 return -EBADF;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003722
Jens Axboe221c5eb2019-01-17 09:41:58 -07003723 events = READ_ONCE(sqe->poll_events);
3724 poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP;
Jens Axboe0969e782019-12-17 18:40:57 -07003725 return 0;
3726}
3727
3728static int io_poll_add(struct io_kiocb *req, struct io_kiocb **nxt)
3729{
3730 struct io_poll_iocb *poll = &req->poll;
3731 struct io_ring_ctx *ctx = req->ctx;
3732 struct io_poll_table ipt;
3733 bool cancel = false;
3734 __poll_t mask;
Jens Axboe0969e782019-12-17 18:40:57 -07003735
3736 INIT_IO_WORK(&req->work, io_poll_complete_work);
Jens Axboe78076bb2019-12-04 19:56:40 -07003737 INIT_HLIST_NODE(&req->hash_node);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003738
Jens Axboe221c5eb2019-01-17 09:41:58 -07003739 poll->head = NULL;
Jens Axboe8c838782019-03-12 15:48:16 -06003740 poll->done = false;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003741 poll->canceled = false;
3742
3743 ipt.pt._qproc = io_poll_queue_proc;
3744 ipt.pt._key = poll->events;
3745 ipt.req = req;
3746 ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
3747
3748 /* initialized the list so that we can do list_empty checks */
Jens Axboe392edb42019-12-09 17:52:20 -07003749 INIT_LIST_HEAD(&poll->wait.entry);
3750 init_waitqueue_func_entry(&poll->wait, io_poll_wake);
3751 poll->wait.private = poll;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003752
Jens Axboe36703242019-07-25 10:20:18 -06003753 INIT_LIST_HEAD(&req->list);
3754
Jens Axboe221c5eb2019-01-17 09:41:58 -07003755 mask = vfs_poll(poll->file, &ipt.pt) & poll->events;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003756
3757 spin_lock_irq(&ctx->completion_lock);
Jens Axboe8c838782019-03-12 15:48:16 -06003758 if (likely(poll->head)) {
3759 spin_lock(&poll->head->lock);
Jens Axboe392edb42019-12-09 17:52:20 -07003760 if (unlikely(list_empty(&poll->wait.entry))) {
Jens Axboe8c838782019-03-12 15:48:16 -06003761 if (ipt.error)
3762 cancel = true;
3763 ipt.error = 0;
3764 mask = 0;
3765 }
3766 if (mask || ipt.error)
Jens Axboe392edb42019-12-09 17:52:20 -07003767 list_del_init(&poll->wait.entry);
Jens Axboe8c838782019-03-12 15:48:16 -06003768 else if (cancel)
3769 WRITE_ONCE(poll->canceled, true);
3770 else if (!poll->done) /* actually waiting for an event */
Jens Axboeeac406c2019-11-14 12:09:58 -07003771 io_poll_req_insert(req);
Jens Axboe8c838782019-03-12 15:48:16 -06003772 spin_unlock(&poll->head->lock);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003773 }
Jens Axboe8c838782019-03-12 15:48:16 -06003774 if (mask) { /* no async, we'd stolen it */
Jens Axboe8c838782019-03-12 15:48:16 -06003775 ipt.error = 0;
Jens Axboeb0dd8a42019-11-18 12:14:54 -07003776 io_poll_complete(req, mask, 0);
Jens Axboe8c838782019-03-12 15:48:16 -06003777 }
Jens Axboe221c5eb2019-01-17 09:41:58 -07003778 spin_unlock_irq(&ctx->completion_lock);
3779
Jens Axboe8c838782019-03-12 15:48:16 -06003780 if (mask) {
3781 io_cqring_ev_posted(ctx);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003782 io_put_req_find_next(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07003783 }
Jens Axboe8c838782019-03-12 15:48:16 -06003784 return ipt.error;
Jens Axboe221c5eb2019-01-17 09:41:58 -07003785}
3786
Jens Axboe5262f562019-09-17 12:26:57 -06003787static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
3788{
Jens Axboead8a48a2019-11-15 08:49:11 -07003789 struct io_timeout_data *data = container_of(timer,
3790 struct io_timeout_data, timer);
3791 struct io_kiocb *req = data->req;
3792 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe5262f562019-09-17 12:26:57 -06003793 unsigned long flags;
3794
Jens Axboe5262f562019-09-17 12:26:57 -06003795 atomic_inc(&ctx->cq_timeouts);
3796
3797 spin_lock_irqsave(&ctx->completion_lock, flags);
zhangyi (F)ef036812019-10-23 15:10:08 +08003798 /*
Jens Axboe11365042019-10-16 09:08:32 -06003799 * We could be racing with timeout deletion. If the list is empty,
3800 * then timeout lookup already found it and will be handling it.
zhangyi (F)ef036812019-10-23 15:10:08 +08003801 */
Jens Axboe842f9612019-10-29 12:34:10 -06003802 if (!list_empty(&req->list)) {
Jens Axboe11365042019-10-16 09:08:32 -06003803 struct io_kiocb *prev;
Jens Axboe5262f562019-09-17 12:26:57 -06003804
Jens Axboe11365042019-10-16 09:08:32 -06003805 /*
3806 * Adjust the reqs sequence before the current one because it
Brian Gianforcarod195a662019-12-13 03:09:50 -08003807 * will consume a slot in the cq_ring and the cq_tail
Jens Axboe11365042019-10-16 09:08:32 -06003808 * pointer will be increased, otherwise other timeout reqs may
3809 * return in advance without waiting for enough wait_nr.
3810 */
3811 prev = req;
3812 list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list)
3813 prev->sequence++;
Jens Axboe11365042019-10-16 09:08:32 -06003814 list_del_init(&req->list);
Jens Axboe11365042019-10-16 09:08:32 -06003815 }
Jens Axboe842f9612019-10-29 12:34:10 -06003816
Jens Axboe78e19bb2019-11-06 15:21:34 -07003817 io_cqring_fill_event(req, -ETIME);
Jens Axboe5262f562019-09-17 12:26:57 -06003818 io_commit_cqring(ctx);
3819 spin_unlock_irqrestore(&ctx->completion_lock, flags);
3820
3821 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003822 req_set_fail_links(req);
Jens Axboe5262f562019-09-17 12:26:57 -06003823 io_put_req(req);
3824 return HRTIMER_NORESTART;
3825}
3826
Jens Axboe47f46762019-11-09 17:43:02 -07003827static int io_timeout_cancel(struct io_ring_ctx *ctx, __u64 user_data)
3828{
3829 struct io_kiocb *req;
3830 int ret = -ENOENT;
3831
3832 list_for_each_entry(req, &ctx->timeout_list, list) {
3833 if (user_data == req->user_data) {
3834 list_del_init(&req->list);
3835 ret = 0;
3836 break;
3837 }
3838 }
3839
3840 if (ret == -ENOENT)
3841 return ret;
3842
Jens Axboe2d283902019-12-04 11:08:05 -07003843 ret = hrtimer_try_to_cancel(&req->io->timeout.timer);
Jens Axboe47f46762019-11-09 17:43:02 -07003844 if (ret == -1)
3845 return -EALREADY;
3846
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003847 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07003848 io_cqring_fill_event(req, -ECANCELED);
3849 io_put_req(req);
3850 return 0;
3851}
3852
Jens Axboe3529d8c2019-12-19 18:24:38 -07003853static int io_timeout_remove_prep(struct io_kiocb *req,
3854 const struct io_uring_sqe *sqe)
Jens Axboeb29472e2019-12-17 18:50:29 -07003855{
Jens Axboeb29472e2019-12-17 18:50:29 -07003856 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
3857 return -EINVAL;
3858 if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->len)
3859 return -EINVAL;
3860
3861 req->timeout.addr = READ_ONCE(sqe->addr);
3862 req->timeout.flags = READ_ONCE(sqe->timeout_flags);
3863 if (req->timeout.flags)
3864 return -EINVAL;
3865
Jens Axboeb29472e2019-12-17 18:50:29 -07003866 return 0;
3867}
3868
Jens Axboe11365042019-10-16 09:08:32 -06003869/*
3870 * Remove or update an existing timeout command
3871 */
Jens Axboefc4df992019-12-10 14:38:45 -07003872static int io_timeout_remove(struct io_kiocb *req)
Jens Axboe11365042019-10-16 09:08:32 -06003873{
3874 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe47f46762019-11-09 17:43:02 -07003875 int ret;
Jens Axboe11365042019-10-16 09:08:32 -06003876
Jens Axboe11365042019-10-16 09:08:32 -06003877 spin_lock_irq(&ctx->completion_lock);
Jens Axboeb29472e2019-12-17 18:50:29 -07003878 ret = io_timeout_cancel(ctx, req->timeout.addr);
Jens Axboe11365042019-10-16 09:08:32 -06003879
Jens Axboe47f46762019-11-09 17:43:02 -07003880 io_cqring_fill_event(req, ret);
Jens Axboe11365042019-10-16 09:08:32 -06003881 io_commit_cqring(ctx);
3882 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06003883 io_cqring_ev_posted(ctx);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07003884 if (ret < 0)
3885 req_set_fail_links(req);
Jackie Liuec9c02a2019-11-08 23:50:36 +08003886 io_put_req(req);
Jens Axboe11365042019-10-16 09:08:32 -06003887 return 0;
Jens Axboe5262f562019-09-17 12:26:57 -06003888}
3889
Jens Axboe3529d8c2019-12-19 18:24:38 -07003890static int io_timeout_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
Jens Axboe2d283902019-12-04 11:08:05 -07003891 bool is_timeout_link)
Jens Axboe5262f562019-09-17 12:26:57 -06003892{
Jens Axboead8a48a2019-11-15 08:49:11 -07003893 struct io_timeout_data *data;
Jens Axboea41525a2019-10-15 16:48:15 -06003894 unsigned flags;
Jens Axboe5262f562019-09-17 12:26:57 -06003895
Jens Axboead8a48a2019-11-15 08:49:11 -07003896 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboe5262f562019-09-17 12:26:57 -06003897 return -EINVAL;
Jens Axboead8a48a2019-11-15 08:49:11 -07003898 if (sqe->ioprio || sqe->buf_index || sqe->len != 1)
Jens Axboea41525a2019-10-15 16:48:15 -06003899 return -EINVAL;
Jens Axboe2d283902019-12-04 11:08:05 -07003900 if (sqe->off && is_timeout_link)
3901 return -EINVAL;
Jens Axboea41525a2019-10-15 16:48:15 -06003902 flags = READ_ONCE(sqe->timeout_flags);
3903 if (flags & ~IORING_TIMEOUT_ABS)
Jens Axboe5262f562019-09-17 12:26:57 -06003904 return -EINVAL;
Arnd Bergmannbdf20072019-10-01 09:53:29 -06003905
Jens Axboe26a61672019-12-20 09:02:01 -07003906 req->timeout.count = READ_ONCE(sqe->off);
3907
Jens Axboe3529d8c2019-12-19 18:24:38 -07003908 if (!req->io && io_alloc_async_ctx(req))
Jens Axboe26a61672019-12-20 09:02:01 -07003909 return -ENOMEM;
3910
3911 data = &req->io->timeout;
Jens Axboead8a48a2019-11-15 08:49:11 -07003912 data->req = req;
Jens Axboead8a48a2019-11-15 08:49:11 -07003913 req->flags |= REQ_F_TIMEOUT;
3914
3915 if (get_timespec64(&data->ts, u64_to_user_ptr(sqe->addr)))
Jens Axboe5262f562019-09-17 12:26:57 -06003916 return -EFAULT;
3917
Jens Axboe11365042019-10-16 09:08:32 -06003918 if (flags & IORING_TIMEOUT_ABS)
Jens Axboead8a48a2019-11-15 08:49:11 -07003919 data->mode = HRTIMER_MODE_ABS;
Jens Axboe11365042019-10-16 09:08:32 -06003920 else
Jens Axboead8a48a2019-11-15 08:49:11 -07003921 data->mode = HRTIMER_MODE_REL;
Jens Axboe11365042019-10-16 09:08:32 -06003922
Jens Axboead8a48a2019-11-15 08:49:11 -07003923 hrtimer_init(&data->timer, CLOCK_MONOTONIC, data->mode);
3924 return 0;
3925}
3926
Jens Axboefc4df992019-12-10 14:38:45 -07003927static int io_timeout(struct io_kiocb *req)
Jens Axboead8a48a2019-11-15 08:49:11 -07003928{
3929 unsigned count;
3930 struct io_ring_ctx *ctx = req->ctx;
3931 struct io_timeout_data *data;
3932 struct list_head *entry;
3933 unsigned span = 0;
Jens Axboead8a48a2019-11-15 08:49:11 -07003934
Jens Axboe2d283902019-12-04 11:08:05 -07003935 data = &req->io->timeout;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003936
Jens Axboe5262f562019-09-17 12:26:57 -06003937 /*
3938 * sqe->off holds how many events that need to occur for this
Jens Axboe93bd25b2019-11-11 23:34:31 -07003939 * timeout event to be satisfied. If it isn't set, then this is
3940 * a pure timeout request, sequence isn't used.
Jens Axboe5262f562019-09-17 12:26:57 -06003941 */
Jens Axboe26a61672019-12-20 09:02:01 -07003942 count = req->timeout.count;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003943 if (!count) {
3944 req->flags |= REQ_F_TIMEOUT_NOSEQ;
3945 spin_lock_irq(&ctx->completion_lock);
3946 entry = ctx->timeout_list.prev;
3947 goto add;
3948 }
Jens Axboe5262f562019-09-17 12:26:57 -06003949
3950 req->sequence = ctx->cached_sq_head + count - 1;
Jens Axboe2d283902019-12-04 11:08:05 -07003951 data->seq_offset = count;
Jens Axboe5262f562019-09-17 12:26:57 -06003952
3953 /*
3954 * Insertion sort, ensuring the first entry in the list is always
3955 * the one we need first.
3956 */
Jens Axboe5262f562019-09-17 12:26:57 -06003957 spin_lock_irq(&ctx->completion_lock);
3958 list_for_each_prev(entry, &ctx->timeout_list) {
3959 struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
yangerkun5da0fb12019-10-15 21:59:29 +08003960 unsigned nxt_sq_head;
3961 long long tmp, tmp_nxt;
Jens Axboe2d283902019-12-04 11:08:05 -07003962 u32 nxt_offset = nxt->io->timeout.seq_offset;
Jens Axboe5262f562019-09-17 12:26:57 -06003963
Jens Axboe93bd25b2019-11-11 23:34:31 -07003964 if (nxt->flags & REQ_F_TIMEOUT_NOSEQ)
3965 continue;
3966
yangerkun5da0fb12019-10-15 21:59:29 +08003967 /*
3968 * Since cached_sq_head + count - 1 can overflow, use type long
3969 * long to store it.
3970 */
3971 tmp = (long long)ctx->cached_sq_head + count - 1;
Pavel Begunkovcc42e0a2019-11-25 23:14:38 +03003972 nxt_sq_head = nxt->sequence - nxt_offset + 1;
3973 tmp_nxt = (long long)nxt_sq_head + nxt_offset - 1;
yangerkun5da0fb12019-10-15 21:59:29 +08003974
3975 /*
3976 * cached_sq_head may overflow, and it will never overflow twice
3977 * once there is some timeout req still be valid.
3978 */
3979 if (ctx->cached_sq_head < nxt_sq_head)
yangerkun8b07a652019-10-17 12:12:35 +08003980 tmp += UINT_MAX;
yangerkun5da0fb12019-10-15 21:59:29 +08003981
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003982 if (tmp > tmp_nxt)
Jens Axboe5262f562019-09-17 12:26:57 -06003983 break;
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003984
3985 /*
3986 * Sequence of reqs after the insert one and itself should
3987 * be adjusted because each timeout req consumes a slot.
3988 */
3989 span++;
3990 nxt->sequence++;
Jens Axboe5262f562019-09-17 12:26:57 -06003991 }
zhangyi (F)a1f58ba2019-10-23 15:10:09 +08003992 req->sequence -= span;
Jens Axboe93bd25b2019-11-11 23:34:31 -07003993add:
Jens Axboe5262f562019-09-17 12:26:57 -06003994 list_add(&req->list, entry);
Jens Axboead8a48a2019-11-15 08:49:11 -07003995 data->timer.function = io_timeout_fn;
3996 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts), data->mode);
Jens Axboe842f9612019-10-29 12:34:10 -06003997 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe5262f562019-09-17 12:26:57 -06003998 return 0;
3999}
4000
Jens Axboe62755e32019-10-28 21:49:21 -06004001static bool io_cancel_cb(struct io_wq_work *work, void *data)
Jens Axboede0617e2019-04-06 21:51:27 -06004002{
Jens Axboe62755e32019-10-28 21:49:21 -06004003 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboede0617e2019-04-06 21:51:27 -06004004
Jens Axboe62755e32019-10-28 21:49:21 -06004005 return req->user_data == (unsigned long) data;
4006}
4007
Jens Axboee977d6d2019-11-05 12:39:45 -07004008static int io_async_cancel_one(struct io_ring_ctx *ctx, void *sqe_addr)
Jens Axboe62755e32019-10-28 21:49:21 -06004009{
Jens Axboe62755e32019-10-28 21:49:21 -06004010 enum io_wq_cancel cancel_ret;
Jens Axboe62755e32019-10-28 21:49:21 -06004011 int ret = 0;
4012
Jens Axboe62755e32019-10-28 21:49:21 -06004013 cancel_ret = io_wq_cancel_cb(ctx->io_wq, io_cancel_cb, sqe_addr);
4014 switch (cancel_ret) {
4015 case IO_WQ_CANCEL_OK:
4016 ret = 0;
4017 break;
4018 case IO_WQ_CANCEL_RUNNING:
4019 ret = -EALREADY;
4020 break;
4021 case IO_WQ_CANCEL_NOTFOUND:
4022 ret = -ENOENT;
4023 break;
4024 }
4025
Jens Axboee977d6d2019-11-05 12:39:45 -07004026 return ret;
4027}
4028
Jens Axboe47f46762019-11-09 17:43:02 -07004029static void io_async_find_and_cancel(struct io_ring_ctx *ctx,
4030 struct io_kiocb *req, __u64 sqe_addr,
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004031 struct io_kiocb **nxt, int success_ret)
Jens Axboe47f46762019-11-09 17:43:02 -07004032{
4033 unsigned long flags;
4034 int ret;
4035
4036 ret = io_async_cancel_one(ctx, (void *) (unsigned long) sqe_addr);
4037 if (ret != -ENOENT) {
4038 spin_lock_irqsave(&ctx->completion_lock, flags);
4039 goto done;
4040 }
4041
4042 spin_lock_irqsave(&ctx->completion_lock, flags);
4043 ret = io_timeout_cancel(ctx, sqe_addr);
4044 if (ret != -ENOENT)
4045 goto done;
4046 ret = io_poll_cancel(ctx, sqe_addr);
4047done:
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004048 if (!ret)
4049 ret = success_ret;
Jens Axboe47f46762019-11-09 17:43:02 -07004050 io_cqring_fill_event(req, ret);
4051 io_commit_cqring(ctx);
4052 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4053 io_cqring_ev_posted(ctx);
4054
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004055 if (ret < 0)
4056 req_set_fail_links(req);
Jens Axboe47f46762019-11-09 17:43:02 -07004057 io_put_req_find_next(req, nxt);
4058}
4059
Jens Axboe3529d8c2019-12-19 18:24:38 -07004060static int io_async_cancel_prep(struct io_kiocb *req,
4061 const struct io_uring_sqe *sqe)
Jens Axboee977d6d2019-11-05 12:39:45 -07004062{
Jens Axboefbf23842019-12-17 18:45:56 -07004063 if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
Jens Axboee977d6d2019-11-05 12:39:45 -07004064 return -EINVAL;
4065 if (sqe->flags || sqe->ioprio || sqe->off || sqe->len ||
4066 sqe->cancel_flags)
4067 return -EINVAL;
4068
Jens Axboefbf23842019-12-17 18:45:56 -07004069 req->cancel.addr = READ_ONCE(sqe->addr);
4070 return 0;
4071}
4072
4073static int io_async_cancel(struct io_kiocb *req, struct io_kiocb **nxt)
4074{
4075 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefbf23842019-12-17 18:45:56 -07004076
4077 io_async_find_and_cancel(ctx, req, req->cancel.addr, nxt, 0);
Jens Axboe62755e32019-10-28 21:49:21 -06004078 return 0;
4079}
4080
Jens Axboe05f3fb32019-12-09 11:22:50 -07004081static int io_files_update_prep(struct io_kiocb *req,
4082 const struct io_uring_sqe *sqe)
4083{
4084 if (sqe->flags || sqe->ioprio || sqe->rw_flags)
4085 return -EINVAL;
4086
4087 req->files_update.offset = READ_ONCE(sqe->off);
4088 req->files_update.nr_args = READ_ONCE(sqe->len);
4089 if (!req->files_update.nr_args)
4090 return -EINVAL;
4091 req->files_update.arg = READ_ONCE(sqe->addr);
4092 return 0;
4093}
4094
4095static int io_files_update(struct io_kiocb *req, bool force_nonblock)
4096{
4097 struct io_ring_ctx *ctx = req->ctx;
4098 struct io_uring_files_update up;
4099 int ret;
4100
Jens Axboef86cd202020-01-29 13:46:44 -07004101 if (force_nonblock)
Jens Axboe05f3fb32019-12-09 11:22:50 -07004102 return -EAGAIN;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004103
4104 up.offset = req->files_update.offset;
4105 up.fds = req->files_update.arg;
4106
4107 mutex_lock(&ctx->uring_lock);
4108 ret = __io_sqe_files_update(ctx, &up, req->files_update.nr_args);
4109 mutex_unlock(&ctx->uring_lock);
4110
4111 if (ret < 0)
4112 req_set_fail_links(req);
4113 io_cqring_add_event(req, ret);
4114 io_put_req(req);
4115 return 0;
4116}
4117
Jens Axboe3529d8c2019-12-19 18:24:38 -07004118static int io_req_defer_prep(struct io_kiocb *req,
4119 const struct io_uring_sqe *sqe)
Jens Axboef67676d2019-12-02 11:03:47 -07004120{
Jens Axboee7815732019-12-17 19:45:06 -07004121 ssize_t ret = 0;
Jens Axboef67676d2019-12-02 11:03:47 -07004122
Jens Axboef86cd202020-01-29 13:46:44 -07004123 if (io_op_defs[req->opcode].file_table) {
4124 ret = io_grab_files(req);
4125 if (unlikely(ret))
4126 return ret;
4127 }
4128
Jens Axboecccf0ee2020-01-27 16:34:48 -07004129 io_req_work_grab_env(req, &io_op_defs[req->opcode]);
4130
Jens Axboed625c6e2019-12-17 19:53:05 -07004131 switch (req->opcode) {
Jens Axboee7815732019-12-17 19:45:06 -07004132 case IORING_OP_NOP:
4133 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004134 case IORING_OP_READV:
4135 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004136 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004137 ret = io_read_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07004138 break;
4139 case IORING_OP_WRITEV:
4140 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004141 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004142 ret = io_write_prep(req, sqe, true);
Jens Axboef67676d2019-12-02 11:03:47 -07004143 break;
Jens Axboe0969e782019-12-17 18:40:57 -07004144 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004145 ret = io_poll_add_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07004146 break;
4147 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004148 ret = io_poll_remove_prep(req, sqe);
Jens Axboe0969e782019-12-17 18:40:57 -07004149 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004150 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004151 ret = io_prep_fsync(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004152 break;
4153 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004154 ret = io_prep_sfr(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004155 break;
Jens Axboe03b12302019-12-02 18:50:25 -07004156 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004157 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004158 ret = io_sendmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07004159 break;
4160 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004161 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004162 ret = io_recvmsg_prep(req, sqe);
Jens Axboe03b12302019-12-02 18:50:25 -07004163 break;
Jens Axboef499a022019-12-02 16:28:46 -07004164 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004165 ret = io_connect_prep(req, sqe);
Jens Axboef499a022019-12-02 16:28:46 -07004166 break;
Jens Axboe2d283902019-12-04 11:08:05 -07004167 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004168 ret = io_timeout_prep(req, sqe, false);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004169 break;
Jens Axboeb29472e2019-12-17 18:50:29 -07004170 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004171 ret = io_timeout_remove_prep(req, sqe);
Jens Axboeb29472e2019-12-17 18:50:29 -07004172 break;
Jens Axboefbf23842019-12-17 18:45:56 -07004173 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004174 ret = io_async_cancel_prep(req, sqe);
Jens Axboefbf23842019-12-17 18:45:56 -07004175 break;
Jens Axboe2d283902019-12-04 11:08:05 -07004176 case IORING_OP_LINK_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004177 ret = io_timeout_prep(req, sqe, true);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004178 break;
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004179 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004180 ret = io_accept_prep(req, sqe);
Jens Axboe8ed8d3c2019-12-16 11:55:28 -07004181 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07004182 case IORING_OP_FALLOCATE:
4183 ret = io_fallocate_prep(req, sqe);
4184 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004185 case IORING_OP_OPENAT:
4186 ret = io_openat_prep(req, sqe);
4187 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07004188 case IORING_OP_CLOSE:
4189 ret = io_close_prep(req, sqe);
4190 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004191 case IORING_OP_FILES_UPDATE:
4192 ret = io_files_update_prep(req, sqe);
4193 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004194 case IORING_OP_STATX:
4195 ret = io_statx_prep(req, sqe);
4196 break;
Jens Axboe4840e412019-12-25 22:03:45 -07004197 case IORING_OP_FADVISE:
4198 ret = io_fadvise_prep(req, sqe);
4199 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07004200 case IORING_OP_MADVISE:
4201 ret = io_madvise_prep(req, sqe);
4202 break;
Jens Axboecebdb982020-01-08 17:59:24 -07004203 case IORING_OP_OPENAT2:
4204 ret = io_openat2_prep(req, sqe);
4205 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004206 case IORING_OP_EPOLL_CTL:
4207 ret = io_epoll_ctl_prep(req, sqe);
4208 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004209 default:
Jens Axboee7815732019-12-17 19:45:06 -07004210 printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
4211 req->opcode);
4212 ret = -EINVAL;
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004213 break;
Jens Axboef67676d2019-12-02 11:03:47 -07004214 }
4215
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004216 return ret;
Jens Axboef67676d2019-12-02 11:03:47 -07004217}
4218
Jens Axboe3529d8c2019-12-19 18:24:38 -07004219static int io_req_defer(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboede0617e2019-04-06 21:51:27 -06004220{
Jackie Liua197f662019-11-08 08:09:12 -07004221 struct io_ring_ctx *ctx = req->ctx;
Jens Axboef67676d2019-12-02 11:03:47 -07004222 int ret;
Jens Axboede0617e2019-04-06 21:51:27 -06004223
Bob Liu9d858b22019-11-13 18:06:25 +08004224 /* Still need defer if there is pending req in defer list. */
4225 if (!req_need_defer(req) && list_empty(&ctx->defer_list))
Jens Axboede0617e2019-04-06 21:51:27 -06004226 return 0;
4227
Jens Axboe3529d8c2019-12-19 18:24:38 -07004228 if (!req->io && io_alloc_async_ctx(req))
Jens Axboede0617e2019-04-06 21:51:27 -06004229 return -EAGAIN;
4230
Jens Axboe3529d8c2019-12-19 18:24:38 -07004231 ret = io_req_defer_prep(req, sqe);
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004232 if (ret < 0)
Jens Axboe2d283902019-12-04 11:08:05 -07004233 return ret;
Jens Axboe2d283902019-12-04 11:08:05 -07004234
Jens Axboede0617e2019-04-06 21:51:27 -06004235 spin_lock_irq(&ctx->completion_lock);
Bob Liu9d858b22019-11-13 18:06:25 +08004236 if (!req_need_defer(req) && list_empty(&ctx->defer_list)) {
Jens Axboede0617e2019-04-06 21:51:27 -06004237 spin_unlock_irq(&ctx->completion_lock);
Jens Axboede0617e2019-04-06 21:51:27 -06004238 return 0;
4239 }
4240
Jens Axboe915967f2019-11-21 09:01:20 -07004241 trace_io_uring_defer(ctx, req, req->user_data);
Jens Axboede0617e2019-04-06 21:51:27 -06004242 list_add_tail(&req->list, &ctx->defer_list);
4243 spin_unlock_irq(&ctx->completion_lock);
4244 return -EIOCBQUEUED;
4245}
4246
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004247static void io_cleanup_req(struct io_kiocb *req)
4248{
4249 struct io_async_ctx *io = req->io;
4250
4251 switch (req->opcode) {
4252 case IORING_OP_READV:
4253 case IORING_OP_READ_FIXED:
4254 case IORING_OP_READ:
4255 case IORING_OP_WRITEV:
4256 case IORING_OP_WRITE_FIXED:
4257 case IORING_OP_WRITE:
4258 if (io->rw.iov != io->rw.fast_iov)
4259 kfree(io->rw.iov);
4260 break;
4261 case IORING_OP_SENDMSG:
4262 case IORING_OP_RECVMSG:
4263 if (io->msg.iov != io->msg.fast_iov)
4264 kfree(io->msg.iov);
4265 break;
Pavel Begunkov8fef80b2020-02-07 23:59:53 +03004266 case IORING_OP_OPENAT:
4267 case IORING_OP_OPENAT2:
4268 case IORING_OP_STATX:
4269 putname(req->open.filename);
4270 break;
Pavel Begunkov99bc4c32020-02-07 22:04:45 +03004271 }
4272
4273 req->flags &= ~REQ_F_NEED_CLEANUP;
4274}
4275
Jens Axboe3529d8c2019-12-19 18:24:38 -07004276static int io_issue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
4277 struct io_kiocb **nxt, bool force_nonblock)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004278{
Jackie Liua197f662019-11-08 08:09:12 -07004279 struct io_ring_ctx *ctx = req->ctx;
Jens Axboed625c6e2019-12-17 19:53:05 -07004280 int ret;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004281
Jens Axboed625c6e2019-12-17 19:53:05 -07004282 switch (req->opcode) {
Jens Axboe2b188cc2019-01-07 10:46:33 -07004283 case IORING_OP_NOP:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004284 ret = io_nop(req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004285 break;
4286 case IORING_OP_READV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004287 case IORING_OP_READ_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004288 case IORING_OP_READ:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004289 if (sqe) {
4290 ret = io_read_prep(req, sqe, force_nonblock);
4291 if (ret < 0)
4292 break;
4293 }
Pavel Begunkov267bc902019-11-07 01:41:08 +03004294 ret = io_read(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004295 break;
4296 case IORING_OP_WRITEV:
Jens Axboeedafcce2019-01-09 09:16:05 -07004297 case IORING_OP_WRITE_FIXED:
Jens Axboe3a6820f2019-12-22 15:19:35 -07004298 case IORING_OP_WRITE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004299 if (sqe) {
4300 ret = io_write_prep(req, sqe, force_nonblock);
4301 if (ret < 0)
4302 break;
4303 }
Pavel Begunkov267bc902019-11-07 01:41:08 +03004304 ret = io_write(req, nxt, force_nonblock);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004305 break;
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004306 case IORING_OP_FSYNC:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004307 if (sqe) {
4308 ret = io_prep_fsync(req, sqe);
4309 if (ret < 0)
4310 break;
4311 }
Jens Axboefc4df992019-12-10 14:38:45 -07004312 ret = io_fsync(req, nxt, force_nonblock);
Christoph Hellwigc992fe22019-01-11 09:43:02 -07004313 break;
Jens Axboe221c5eb2019-01-17 09:41:58 -07004314 case IORING_OP_POLL_ADD:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004315 if (sqe) {
4316 ret = io_poll_add_prep(req, sqe);
4317 if (ret)
4318 break;
4319 }
Jens Axboefc4df992019-12-10 14:38:45 -07004320 ret = io_poll_add(req, nxt);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004321 break;
4322 case IORING_OP_POLL_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004323 if (sqe) {
4324 ret = io_poll_remove_prep(req, sqe);
4325 if (ret < 0)
4326 break;
4327 }
Jens Axboefc4df992019-12-10 14:38:45 -07004328 ret = io_poll_remove(req);
Jens Axboe221c5eb2019-01-17 09:41:58 -07004329 break;
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004330 case IORING_OP_SYNC_FILE_RANGE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004331 if (sqe) {
4332 ret = io_prep_sfr(req, sqe);
4333 if (ret < 0)
4334 break;
4335 }
Jens Axboefc4df992019-12-10 14:38:45 -07004336 ret = io_sync_file_range(req, nxt, force_nonblock);
Jens Axboe5d17b4a2019-04-09 14:56:44 -06004337 break;
Jens Axboe0fa03c62019-04-19 13:34:07 -06004338 case IORING_OP_SENDMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004339 case IORING_OP_SEND:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004340 if (sqe) {
4341 ret = io_sendmsg_prep(req, sqe);
4342 if (ret < 0)
4343 break;
4344 }
Jens Axboefddafac2020-01-04 20:19:44 -07004345 if (req->opcode == IORING_OP_SENDMSG)
4346 ret = io_sendmsg(req, nxt, force_nonblock);
4347 else
4348 ret = io_send(req, nxt, force_nonblock);
Jens Axboe0fa03c62019-04-19 13:34:07 -06004349 break;
Jens Axboeaa1fa282019-04-19 13:38:09 -06004350 case IORING_OP_RECVMSG:
Jens Axboefddafac2020-01-04 20:19:44 -07004351 case IORING_OP_RECV:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004352 if (sqe) {
4353 ret = io_recvmsg_prep(req, sqe);
4354 if (ret)
4355 break;
4356 }
Jens Axboefddafac2020-01-04 20:19:44 -07004357 if (req->opcode == IORING_OP_RECVMSG)
4358 ret = io_recvmsg(req, nxt, force_nonblock);
4359 else
4360 ret = io_recv(req, nxt, force_nonblock);
Jens Axboeaa1fa282019-04-19 13:38:09 -06004361 break;
Jens Axboe5262f562019-09-17 12:26:57 -06004362 case IORING_OP_TIMEOUT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004363 if (sqe) {
4364 ret = io_timeout_prep(req, sqe, false);
4365 if (ret)
4366 break;
4367 }
Jens Axboefc4df992019-12-10 14:38:45 -07004368 ret = io_timeout(req);
Jens Axboe5262f562019-09-17 12:26:57 -06004369 break;
Jens Axboe11365042019-10-16 09:08:32 -06004370 case IORING_OP_TIMEOUT_REMOVE:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004371 if (sqe) {
4372 ret = io_timeout_remove_prep(req, sqe);
4373 if (ret)
4374 break;
4375 }
Jens Axboefc4df992019-12-10 14:38:45 -07004376 ret = io_timeout_remove(req);
Jens Axboe11365042019-10-16 09:08:32 -06004377 break;
Jens Axboe17f2fe32019-10-17 14:42:58 -06004378 case IORING_OP_ACCEPT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004379 if (sqe) {
4380 ret = io_accept_prep(req, sqe);
4381 if (ret)
4382 break;
4383 }
Jens Axboefc4df992019-12-10 14:38:45 -07004384 ret = io_accept(req, nxt, force_nonblock);
Jens Axboe17f2fe32019-10-17 14:42:58 -06004385 break;
Jens Axboef8e85cf2019-11-23 14:24:24 -07004386 case IORING_OP_CONNECT:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004387 if (sqe) {
4388 ret = io_connect_prep(req, sqe);
4389 if (ret)
4390 break;
4391 }
Jens Axboefc4df992019-12-10 14:38:45 -07004392 ret = io_connect(req, nxt, force_nonblock);
Jens Axboef8e85cf2019-11-23 14:24:24 -07004393 break;
Jens Axboe62755e32019-10-28 21:49:21 -06004394 case IORING_OP_ASYNC_CANCEL:
Jens Axboe3529d8c2019-12-19 18:24:38 -07004395 if (sqe) {
4396 ret = io_async_cancel_prep(req, sqe);
4397 if (ret)
4398 break;
4399 }
Jens Axboefc4df992019-12-10 14:38:45 -07004400 ret = io_async_cancel(req, nxt);
Jens Axboe62755e32019-10-28 21:49:21 -06004401 break;
Jens Axboed63d1b52019-12-10 10:38:56 -07004402 case IORING_OP_FALLOCATE:
4403 if (sqe) {
4404 ret = io_fallocate_prep(req, sqe);
4405 if (ret)
4406 break;
4407 }
4408 ret = io_fallocate(req, nxt, force_nonblock);
4409 break;
Jens Axboe15b71ab2019-12-11 11:20:36 -07004410 case IORING_OP_OPENAT:
4411 if (sqe) {
4412 ret = io_openat_prep(req, sqe);
4413 if (ret)
4414 break;
4415 }
4416 ret = io_openat(req, nxt, force_nonblock);
4417 break;
Jens Axboeb5dba592019-12-11 14:02:38 -07004418 case IORING_OP_CLOSE:
4419 if (sqe) {
4420 ret = io_close_prep(req, sqe);
4421 if (ret)
4422 break;
4423 }
4424 ret = io_close(req, nxt, force_nonblock);
4425 break;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004426 case IORING_OP_FILES_UPDATE:
4427 if (sqe) {
4428 ret = io_files_update_prep(req, sqe);
4429 if (ret)
4430 break;
4431 }
4432 ret = io_files_update(req, force_nonblock);
4433 break;
Jens Axboeeddc7ef2019-12-13 21:18:10 -07004434 case IORING_OP_STATX:
4435 if (sqe) {
4436 ret = io_statx_prep(req, sqe);
4437 if (ret)
4438 break;
4439 }
4440 ret = io_statx(req, nxt, force_nonblock);
4441 break;
Jens Axboe4840e412019-12-25 22:03:45 -07004442 case IORING_OP_FADVISE:
4443 if (sqe) {
4444 ret = io_fadvise_prep(req, sqe);
4445 if (ret)
4446 break;
4447 }
4448 ret = io_fadvise(req, nxt, force_nonblock);
4449 break;
Jens Axboec1ca7572019-12-25 22:18:28 -07004450 case IORING_OP_MADVISE:
4451 if (sqe) {
4452 ret = io_madvise_prep(req, sqe);
4453 if (ret)
4454 break;
4455 }
4456 ret = io_madvise(req, nxt, force_nonblock);
4457 break;
Jens Axboecebdb982020-01-08 17:59:24 -07004458 case IORING_OP_OPENAT2:
4459 if (sqe) {
4460 ret = io_openat2_prep(req, sqe);
4461 if (ret)
4462 break;
4463 }
4464 ret = io_openat2(req, nxt, force_nonblock);
4465 break;
Jens Axboe3e4827b2020-01-08 15:18:09 -07004466 case IORING_OP_EPOLL_CTL:
4467 if (sqe) {
4468 ret = io_epoll_ctl_prep(req, sqe);
4469 if (ret)
4470 break;
4471 }
4472 ret = io_epoll_ctl(req, nxt, force_nonblock);
4473 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004474 default:
4475 ret = -EINVAL;
4476 break;
4477 }
4478
Jens Axboedef596e2019-01-09 08:59:42 -07004479 if (ret)
4480 return ret;
4481
4482 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboe11ba8202020-01-15 21:51:17 -07004483 const bool in_async = io_wq_current_is_worker();
4484
Jens Axboe9e645e112019-05-10 16:07:28 -06004485 if (req->result == -EAGAIN)
Jens Axboedef596e2019-01-09 08:59:42 -07004486 return -EAGAIN;
4487
Jens Axboe11ba8202020-01-15 21:51:17 -07004488 /* workqueue context doesn't hold uring_lock, grab it now */
4489 if (in_async)
4490 mutex_lock(&ctx->uring_lock);
4491
Jens Axboedef596e2019-01-09 08:59:42 -07004492 io_iopoll_req_issued(req);
Jens Axboe11ba8202020-01-15 21:51:17 -07004493
4494 if (in_async)
4495 mutex_unlock(&ctx->uring_lock);
Jens Axboedef596e2019-01-09 08:59:42 -07004496 }
4497
4498 return 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004499}
4500
Jens Axboe561fb042019-10-24 07:25:42 -06004501static void io_wq_submit_work(struct io_wq_work **workptr)
Jens Axboe31b51512019-01-18 22:56:34 -07004502{
Jens Axboe561fb042019-10-24 07:25:42 -06004503 struct io_wq_work *work = *workptr;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004504 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
Jens Axboe561fb042019-10-24 07:25:42 -06004505 struct io_kiocb *nxt = NULL;
4506 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004507
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07004508 /* if NO_CANCEL is set, we must still run the work */
4509 if ((work->flags & (IO_WQ_WORK_CANCEL|IO_WQ_WORK_NO_CANCEL)) ==
4510 IO_WQ_WORK_CANCEL) {
Jens Axboe561fb042019-10-24 07:25:42 -06004511 ret = -ECANCELED;
Jens Axboe0c9d5cc2019-12-11 19:29:43 -07004512 }
Jens Axboe31b51512019-01-18 22:56:34 -07004513
Jens Axboe561fb042019-10-24 07:25:42 -06004514 if (!ret) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004515 req->in_async = true;
Jens Axboe561fb042019-10-24 07:25:42 -06004516 do {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004517 ret = io_issue_sqe(req, NULL, &nxt, false);
Jens Axboe561fb042019-10-24 07:25:42 -06004518 /*
4519 * We can get EAGAIN for polled IO even though we're
4520 * forcing a sync submission from here, since we can't
4521 * wait for request slots on the block side.
4522 */
4523 if (ret != -EAGAIN)
4524 break;
4525 cond_resched();
4526 } while (1);
4527 }
Jens Axboe31b51512019-01-18 22:56:34 -07004528
Jens Axboe561fb042019-10-24 07:25:42 -06004529 /* drop submission reference */
Jackie Liuec9c02a2019-11-08 23:50:36 +08004530 io_put_req(req);
Jens Axboe817869d2019-04-30 14:44:05 -06004531
Jens Axboe561fb042019-10-24 07:25:42 -06004532 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004533 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07004534 io_cqring_add_event(req, ret);
Jens Axboe817869d2019-04-30 14:44:05 -06004535 io_put_req(req);
Jens Axboeedafcce2019-01-09 09:16:05 -07004536 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07004537
Jens Axboe561fb042019-10-24 07:25:42 -06004538 /* if a dependent link is ready, pass it back */
Jens Axboe78912932020-01-14 22:09:06 -07004539 if (!ret && nxt)
4540 io_wq_assign_next(workptr, nxt);
Jens Axboe31b51512019-01-18 22:56:34 -07004541}
Jens Axboe2b188cc2019-01-07 10:46:33 -07004542
Jens Axboe15b71ab2019-12-11 11:20:36 -07004543static int io_req_needs_file(struct io_kiocb *req, int fd)
Jens Axboe9e3aa612019-12-11 15:55:43 -07004544{
Jens Axboed3656342019-12-18 09:50:26 -07004545 if (!io_op_defs[req->opcode].needs_file)
Jens Axboe9e3aa612019-12-11 15:55:43 -07004546 return 0;
Jens Axboe0b5faf62020-02-06 21:42:51 -07004547 if ((fd == -1 || fd == AT_FDCWD) && io_op_defs[req->opcode].fd_non_neg)
Jens Axboed3656342019-12-18 09:50:26 -07004548 return 0;
4549 return 1;
Jens Axboe09bb8392019-03-13 12:39:28 -06004550}
4551
Jens Axboe65e19f52019-10-26 07:20:21 -06004552static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,
4553 int index)
Jens Axboe09bb8392019-03-13 12:39:28 -06004554{
Jens Axboe65e19f52019-10-26 07:20:21 -06004555 struct fixed_file_table *table;
4556
Jens Axboe05f3fb32019-12-09 11:22:50 -07004557 table = &ctx->file_data->table[index >> IORING_FILE_TABLE_SHIFT];
4558 return table->files[index & IORING_FILE_TABLE_MASK];;
Jens Axboe65e19f52019-10-26 07:20:21 -06004559}
4560
Jens Axboe3529d8c2019-12-19 18:24:38 -07004561static int io_req_set_file(struct io_submit_state *state, struct io_kiocb *req,
4562 const struct io_uring_sqe *sqe)
Jens Axboe09bb8392019-03-13 12:39:28 -06004563{
Jackie Liua197f662019-11-08 08:09:12 -07004564 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe09bb8392019-03-13 12:39:28 -06004565 unsigned flags;
Jens Axboed3656342019-12-18 09:50:26 -07004566 int fd;
Jens Axboe09bb8392019-03-13 12:39:28 -06004567
Jens Axboe3529d8c2019-12-19 18:24:38 -07004568 flags = READ_ONCE(sqe->flags);
4569 fd = READ_ONCE(sqe->fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06004570
Jens Axboed3656342019-12-18 09:50:26 -07004571 if (!io_req_needs_file(req, fd))
4572 return 0;
Jens Axboe09bb8392019-03-13 12:39:28 -06004573
4574 if (flags & IOSQE_FIXED_FILE) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07004575 if (unlikely(!ctx->file_data ||
Jens Axboe09bb8392019-03-13 12:39:28 -06004576 (unsigned) fd >= ctx->nr_user_files))
4577 return -EBADF;
Jens Axboeb7620122019-10-26 07:22:55 -06004578 fd = array_index_nospec(fd, ctx->nr_user_files);
Jens Axboe65e19f52019-10-26 07:20:21 -06004579 req->file = io_file_from_index(ctx, fd);
4580 if (!req->file)
Jens Axboe08a45172019-10-03 08:11:03 -06004581 return -EBADF;
Jens Axboe09bb8392019-03-13 12:39:28 -06004582 req->flags |= REQ_F_FIXED_FILE;
Jens Axboe05f3fb32019-12-09 11:22:50 -07004583 percpu_ref_get(&ctx->file_data->refs);
Jens Axboe09bb8392019-03-13 12:39:28 -06004584 } else {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004585 if (req->needs_fixed_file)
Jens Axboe09bb8392019-03-13 12:39:28 -06004586 return -EBADF;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02004587 trace_io_uring_file_get(ctx, fd);
Jens Axboe09bb8392019-03-13 12:39:28 -06004588 req->file = io_file_get(state, fd);
4589 if (unlikely(!req->file))
4590 return -EBADF;
4591 }
4592
4593 return 0;
4594}
4595
Jackie Liua197f662019-11-08 08:09:12 -07004596static int io_grab_files(struct io_kiocb *req)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004597{
Jens Axboefcb323c2019-10-24 12:39:47 -06004598 int ret = -EBADF;
Jackie Liua197f662019-11-08 08:09:12 -07004599 struct io_ring_ctx *ctx = req->ctx;
Jens Axboefcb323c2019-10-24 12:39:47 -06004600
Jens Axboef86cd202020-01-29 13:46:44 -07004601 if (req->work.files)
4602 return 0;
Pavel Begunkovb14cca02020-01-17 04:45:59 +03004603 if (!ctx->ring_file)
Jens Axboeb5dba592019-12-11 14:02:38 -07004604 return -EBADF;
4605
Jens Axboefcb323c2019-10-24 12:39:47 -06004606 rcu_read_lock();
4607 spin_lock_irq(&ctx->inflight_lock);
4608 /*
4609 * We use the f_ops->flush() handler to ensure that we can flush
4610 * out work accessing these files if the fd is closed. Check if
4611 * the fd has changed since we started down this path, and disallow
4612 * this operation if it has.
4613 */
Pavel Begunkovb14cca02020-01-17 04:45:59 +03004614 if (fcheck(ctx->ring_fd) == ctx->ring_file) {
Jens Axboefcb323c2019-10-24 12:39:47 -06004615 list_add(&req->inflight_entry, &ctx->inflight_list);
4616 req->flags |= REQ_F_INFLIGHT;
4617 req->work.files = current->files;
4618 ret = 0;
4619 }
4620 spin_unlock_irq(&ctx->inflight_lock);
4621 rcu_read_unlock();
4622
4623 return ret;
4624}
4625
Jens Axboe2665abf2019-11-05 12:40:47 -07004626static enum hrtimer_restart io_link_timeout_fn(struct hrtimer *timer)
4627{
Jens Axboead8a48a2019-11-15 08:49:11 -07004628 struct io_timeout_data *data = container_of(timer,
4629 struct io_timeout_data, timer);
4630 struct io_kiocb *req = data->req;
Jens Axboe2665abf2019-11-05 12:40:47 -07004631 struct io_ring_ctx *ctx = req->ctx;
4632 struct io_kiocb *prev = NULL;
4633 unsigned long flags;
Jens Axboe2665abf2019-11-05 12:40:47 -07004634
4635 spin_lock_irqsave(&ctx->completion_lock, flags);
4636
4637 /*
4638 * We don't expect the list to be empty, that will only happen if we
4639 * race with the completion of the linked work.
4640 */
Pavel Begunkov44932332019-12-05 16:16:35 +03004641 if (!list_empty(&req->link_list)) {
4642 prev = list_entry(req->link_list.prev, struct io_kiocb,
4643 link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07004644 if (refcount_inc_not_zero(&prev->refs)) {
Pavel Begunkov44932332019-12-05 16:16:35 +03004645 list_del_init(&req->link_list);
Jens Axboe5d960722019-11-19 15:31:28 -07004646 prev->flags &= ~REQ_F_LINK_TIMEOUT;
4647 } else
Jens Axboe76a46e02019-11-10 23:34:16 -07004648 prev = NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07004649 }
4650
4651 spin_unlock_irqrestore(&ctx->completion_lock, flags);
4652
4653 if (prev) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004654 req_set_fail_links(prev);
Jens Axboeb0dd8a42019-11-18 12:14:54 -07004655 io_async_find_and_cancel(ctx, req, prev->user_data, NULL,
4656 -ETIME);
Jens Axboe76a46e02019-11-10 23:34:16 -07004657 io_put_req(prev);
Jens Axboe47f46762019-11-09 17:43:02 -07004658 } else {
4659 io_cqring_add_event(req, -ETIME);
4660 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07004661 }
Jens Axboe2665abf2019-11-05 12:40:47 -07004662 return HRTIMER_NORESTART;
4663}
4664
Jens Axboead8a48a2019-11-15 08:49:11 -07004665static void io_queue_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07004666{
Jens Axboe76a46e02019-11-10 23:34:16 -07004667 struct io_ring_ctx *ctx = req->ctx;
Jens Axboe2665abf2019-11-05 12:40:47 -07004668
Jens Axboe76a46e02019-11-10 23:34:16 -07004669 /*
4670 * If the list is now empty, then our linked request finished before
4671 * we got a chance to setup the timer
4672 */
4673 spin_lock_irq(&ctx->completion_lock);
Pavel Begunkov44932332019-12-05 16:16:35 +03004674 if (!list_empty(&req->link_list)) {
Jens Axboe2d283902019-12-04 11:08:05 -07004675 struct io_timeout_data *data = &req->io->timeout;
Jens Axboe94ae5e72019-11-14 19:39:52 -07004676
Jens Axboead8a48a2019-11-15 08:49:11 -07004677 data->timer.function = io_link_timeout_fn;
4678 hrtimer_start(&data->timer, timespec64_to_ktime(data->ts),
4679 data->mode);
Jens Axboe2665abf2019-11-05 12:40:47 -07004680 }
Jens Axboe76a46e02019-11-10 23:34:16 -07004681 spin_unlock_irq(&ctx->completion_lock);
Jens Axboe2665abf2019-11-05 12:40:47 -07004682
Jens Axboe2665abf2019-11-05 12:40:47 -07004683 /* drop submission reference */
Jens Axboe76a46e02019-11-10 23:34:16 -07004684 io_put_req(req);
Jens Axboe2665abf2019-11-05 12:40:47 -07004685}
4686
Jens Axboead8a48a2019-11-15 08:49:11 -07004687static struct io_kiocb *io_prep_linked_timeout(struct io_kiocb *req)
Jens Axboe2665abf2019-11-05 12:40:47 -07004688{
4689 struct io_kiocb *nxt;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004690
Jens Axboe2665abf2019-11-05 12:40:47 -07004691 if (!(req->flags & REQ_F_LINK))
4692 return NULL;
4693
Pavel Begunkov44932332019-12-05 16:16:35 +03004694 nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb,
4695 link_list);
Jens Axboed625c6e2019-12-17 19:53:05 -07004696 if (!nxt || nxt->opcode != IORING_OP_LINK_TIMEOUT)
Jens Axboe76a46e02019-11-10 23:34:16 -07004697 return NULL;
Jens Axboe2665abf2019-11-05 12:40:47 -07004698
Jens Axboe76a46e02019-11-10 23:34:16 -07004699 req->flags |= REQ_F_LINK_TIMEOUT;
Jens Axboe76a46e02019-11-10 23:34:16 -07004700 return nxt;
Jens Axboe2665abf2019-11-05 12:40:47 -07004701}
4702
Jens Axboe3529d8c2019-12-19 18:24:38 -07004703static void __io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004704{
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004705 struct io_kiocb *linked_timeout;
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004706 struct io_kiocb *nxt = NULL;
Jens Axboe193155c2020-02-22 23:22:19 -07004707 const struct cred *old_creds = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004708 int ret;
4709
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004710again:
4711 linked_timeout = io_prep_linked_timeout(req);
4712
Jens Axboe193155c2020-02-22 23:22:19 -07004713 if (req->work.creds && req->work.creds != current_cred()) {
4714 if (old_creds)
4715 revert_creds(old_creds);
4716 if (old_creds == req->work.creds)
4717 old_creds = NULL; /* restored original creds */
4718 else
4719 old_creds = override_creds(req->work.creds);
4720 }
4721
Jens Axboe3529d8c2019-12-19 18:24:38 -07004722 ret = io_issue_sqe(req, sqe, &nxt, true);
Jens Axboe491381ce2019-10-17 09:20:46 -06004723
4724 /*
4725 * We async punt it if the file wasn't marked NOWAIT, or if the file
4726 * doesn't support non-blocking read/write attempts
4727 */
4728 if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) ||
4729 (req->flags & REQ_F_MUST_PUNT))) {
Pavel Begunkov86a761f2020-01-22 23:09:36 +03004730punt:
Jens Axboef86cd202020-01-29 13:46:44 -07004731 if (io_op_defs[req->opcode].file_table) {
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03004732 ret = io_grab_files(req);
4733 if (ret)
4734 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004735 }
Pavel Begunkovbbad27b2019-11-19 23:32:47 +03004736
4737 /*
4738 * Queued up for async execution, worker will release
4739 * submit reference when the iocb is actually submitted.
4740 */
4741 io_queue_async_work(req);
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004742 goto done_req;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004743 }
Jens Axboee65ef562019-03-12 10:16:44 -06004744
Jens Axboefcb323c2019-10-24 12:39:47 -06004745err:
Jens Axboee65ef562019-03-12 10:16:44 -06004746 /* drop submission reference */
Jens Axboe2a44f462020-02-25 13:25:41 -07004747 io_put_req_find_next(req, &nxt);
Jens Axboee65ef562019-03-12 10:16:44 -06004748
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004749 if (linked_timeout) {
Jens Axboe76a46e02019-11-10 23:34:16 -07004750 if (!ret)
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004751 io_queue_linked_timeout(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07004752 else
Pavel Begunkovf9bd67f2019-11-21 23:21:03 +03004753 io_put_req(linked_timeout);
Jens Axboe76a46e02019-11-10 23:34:16 -07004754 }
4755
Jens Axboee65ef562019-03-12 10:16:44 -06004756 /* and drop final reference, if we failed */
Jens Axboe9e645e112019-05-10 16:07:28 -06004757 if (ret) {
Jens Axboe78e19bb2019-11-06 15:21:34 -07004758 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004759 req_set_fail_links(req);
Jens Axboee65ef562019-03-12 10:16:44 -06004760 io_put_req(req);
Jens Axboe9e645e112019-05-10 16:07:28 -06004761 }
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004762done_req:
4763 if (nxt) {
4764 req = nxt;
4765 nxt = NULL;
Pavel Begunkov86a761f2020-01-22 23:09:36 +03004766
4767 if (req->flags & REQ_F_FORCE_ASYNC)
4768 goto punt;
Jens Axboe4a0a7a12019-12-09 20:01:01 -07004769 goto again;
4770 }
Jens Axboe193155c2020-02-22 23:22:19 -07004771 if (old_creds)
4772 revert_creds(old_creds);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004773}
4774
Jens Axboe3529d8c2019-12-19 18:24:38 -07004775static void io_queue_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe)
Jackie Liu4fe2c962019-09-09 20:50:40 +08004776{
4777 int ret;
4778
Jens Axboe3529d8c2019-12-19 18:24:38 -07004779 ret = io_req_defer(req, sqe);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004780 if (ret) {
4781 if (ret != -EIOCBQUEUED) {
Pavel Begunkov11185912020-01-22 23:09:35 +03004782fail_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004783 io_cqring_add_event(req, ret);
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004784 req_set_fail_links(req);
Jens Axboe78e19bb2019-11-06 15:21:34 -07004785 io_double_put_req(req);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004786 }
Pavel Begunkov25508782019-12-30 21:24:47 +03004787 } else if (req->flags & REQ_F_FORCE_ASYNC) {
Pavel Begunkov11185912020-01-22 23:09:35 +03004788 ret = io_req_defer_prep(req, sqe);
4789 if (unlikely(ret < 0))
4790 goto fail_req;
Jens Axboece35a472019-12-17 08:04:44 -07004791 /*
4792 * Never try inline submit of IOSQE_ASYNC is set, go straight
4793 * to async execution.
4794 */
4795 req->work.flags |= IO_WQ_WORK_CONCURRENT;
4796 io_queue_async_work(req);
4797 } else {
Jens Axboe3529d8c2019-12-19 18:24:38 -07004798 __io_queue_sqe(req, sqe);
Jens Axboece35a472019-12-17 08:04:44 -07004799 }
Jackie Liu4fe2c962019-09-09 20:50:40 +08004800}
4801
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004802static inline void io_queue_link_head(struct io_kiocb *req)
Jackie Liu4fe2c962019-09-09 20:50:40 +08004803{
Jens Axboe94ae5e72019-11-14 19:39:52 -07004804 if (unlikely(req->flags & REQ_F_FAIL_LINK)) {
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03004805 io_cqring_add_event(req, -ECANCELED);
4806 io_double_put_req(req);
4807 } else
Jens Axboe3529d8c2019-12-19 18:24:38 -07004808 io_queue_sqe(req, NULL);
Jackie Liu4fe2c962019-09-09 20:50:40 +08004809}
4810
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004811#define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK| \
Jens Axboece35a472019-12-17 08:04:44 -07004812 IOSQE_IO_HARDLINK | IOSQE_ASYNC)
Jens Axboe9e645e112019-05-10 16:07:28 -06004813
Jens Axboe3529d8c2019-12-19 18:24:38 -07004814static bool io_submit_sqe(struct io_kiocb *req, const struct io_uring_sqe *sqe,
4815 struct io_submit_state *state, struct io_kiocb **link)
Jens Axboe9e645e112019-05-10 16:07:28 -06004816{
Jackie Liua197f662019-11-08 08:09:12 -07004817 struct io_ring_ctx *ctx = req->ctx;
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004818 unsigned int sqe_flags;
Jens Axboe75c6a032020-01-28 10:15:23 -07004819 int ret, id;
Jens Axboe9e645e112019-05-10 16:07:28 -06004820
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004821 sqe_flags = READ_ONCE(sqe->flags);
Jens Axboe9e645e112019-05-10 16:07:28 -06004822
4823 /* enforce forwards compatibility on users */
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004824 if (unlikely(sqe_flags & ~SQE_VALID_FLAGS)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06004825 ret = -EINVAL;
Pavel Begunkov196be952019-11-07 01:41:06 +03004826 goto err_req;
Jens Axboe9e645e112019-05-10 16:07:28 -06004827 }
4828
Jens Axboe75c6a032020-01-28 10:15:23 -07004829 id = READ_ONCE(sqe->personality);
4830 if (id) {
Jens Axboe193155c2020-02-22 23:22:19 -07004831 req->work.creds = idr_find(&ctx->personality_idr, id);
4832 if (unlikely(!req->work.creds)) {
Jens Axboe75c6a032020-01-28 10:15:23 -07004833 ret = -EINVAL;
4834 goto err_req;
4835 }
Jens Axboe193155c2020-02-22 23:22:19 -07004836 get_cred(req->work.creds);
Jens Axboe75c6a032020-01-28 10:15:23 -07004837 }
4838
Pavel Begunkov6b47ee62020-01-18 20:22:41 +03004839 /* same numerical values with corresponding REQ_F_*, safe to copy */
4840 req->flags |= sqe_flags & (IOSQE_IO_DRAIN|IOSQE_IO_HARDLINK|
4841 IOSQE_ASYNC);
Jens Axboe9e645e112019-05-10 16:07:28 -06004842
Jens Axboe3529d8c2019-12-19 18:24:38 -07004843 ret = io_req_set_file(state, req, sqe);
Jens Axboe9e645e112019-05-10 16:07:28 -06004844 if (unlikely(ret)) {
4845err_req:
Jens Axboe78e19bb2019-11-06 15:21:34 -07004846 io_cqring_add_event(req, ret);
4847 io_double_put_req(req);
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004848 return false;
Jens Axboe9e645e112019-05-10 16:07:28 -06004849 }
4850
Jens Axboe9e645e112019-05-10 16:07:28 -06004851 /*
4852 * If we already have a head request, queue this one for async
4853 * submittal once the head completes. If we don't have a head but
4854 * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be
4855 * submitted sync once the chain is complete. If none of those
4856 * conditions are true (normal request), then just queue it.
4857 */
4858 if (*link) {
Pavel Begunkov9d763772019-12-17 02:22:07 +03004859 struct io_kiocb *head = *link;
Jens Axboe9e645e112019-05-10 16:07:28 -06004860
Pavel Begunkov8cdf2192020-01-25 00:40:24 +03004861 /*
4862 * Taking sequential execution of a link, draining both sides
4863 * of the link also fullfils IOSQE_IO_DRAIN semantics for all
4864 * requests in the link. So, it drains the head and the
4865 * next after the link request. The last one is done via
4866 * drain_next flag to persist the effect across calls.
4867 */
Pavel Begunkov711be032020-01-17 03:57:59 +03004868 if (sqe_flags & IOSQE_IO_DRAIN) {
4869 head->flags |= REQ_F_IO_DRAIN;
4870 ctx->drain_next = 1;
4871 }
Jens Axboeb7bb4f72019-12-15 22:13:43 -07004872 if (io_alloc_async_ctx(req)) {
Jens Axboe9e645e112019-05-10 16:07:28 -06004873 ret = -EAGAIN;
4874 goto err_req;
4875 }
4876
Jens Axboe3529d8c2019-12-19 18:24:38 -07004877 ret = io_req_defer_prep(req, sqe);
Jens Axboe2d283902019-12-04 11:08:05 -07004878 if (ret) {
Jens Axboe4e88d6e2019-12-07 20:59:47 -07004879 /* fail even hard links since we don't submit */
Pavel Begunkov9d763772019-12-17 02:22:07 +03004880 head->flags |= REQ_F_FAIL_LINK;
Jens Axboef67676d2019-12-02 11:03:47 -07004881 goto err_req;
Jens Axboe2d283902019-12-04 11:08:05 -07004882 }
Pavel Begunkov9d763772019-12-17 02:22:07 +03004883 trace_io_uring_link(ctx, req, head);
4884 list_add_tail(&req->link_list, &head->link_list);
Jens Axboe9e645e112019-05-10 16:07:28 -06004885
Pavel Begunkov32fe5252019-12-17 22:26:58 +03004886 /* last request of a link, enqueue the link */
4887 if (!(sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK))) {
4888 io_queue_link_head(head);
4889 *link = NULL;
4890 }
Jens Axboe9e645e112019-05-10 16:07:28 -06004891 } else {
Pavel Begunkov711be032020-01-17 03:57:59 +03004892 if (unlikely(ctx->drain_next)) {
4893 req->flags |= REQ_F_IO_DRAIN;
4894 req->ctx->drain_next = 0;
4895 }
4896 if (sqe_flags & (IOSQE_IO_LINK|IOSQE_IO_HARDLINK)) {
4897 req->flags |= REQ_F_LINK;
Pavel Begunkov711be032020-01-17 03:57:59 +03004898 INIT_LIST_HEAD(&req->link_list);
4899 ret = io_req_defer_prep(req, sqe);
4900 if (ret)
4901 req->flags |= REQ_F_FAIL_LINK;
4902 *link = req;
4903 } else {
4904 io_queue_sqe(req, sqe);
4905 }
Jens Axboe9e645e112019-05-10 16:07:28 -06004906 }
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03004907
4908 return true;
Jens Axboe9e645e112019-05-10 16:07:28 -06004909}
4910
Jens Axboe9a56a232019-01-09 09:06:50 -07004911/*
4912 * Batched submission is done, ensure local IO is flushed out.
4913 */
4914static void io_submit_state_end(struct io_submit_state *state)
4915{
4916 blk_finish_plug(&state->plug);
Jens Axboe3d6770f2019-04-13 11:50:54 -06004917 io_file_put(state);
Jens Axboe2579f912019-01-09 09:10:43 -07004918 if (state->free_reqs)
Pavel Begunkov6c8a3132020-02-01 03:58:00 +03004919 kmem_cache_free_bulk(req_cachep, state->free_reqs, state->reqs);
Jens Axboe9a56a232019-01-09 09:06:50 -07004920}
4921
4922/*
4923 * Start submission side cache.
4924 */
4925static void io_submit_state_start(struct io_submit_state *state,
Jackie Liu22efde52019-12-02 17:14:52 +08004926 unsigned int max_ios)
Jens Axboe9a56a232019-01-09 09:06:50 -07004927{
4928 blk_start_plug(&state->plug);
Jens Axboe2579f912019-01-09 09:10:43 -07004929 state->free_reqs = 0;
Jens Axboe9a56a232019-01-09 09:06:50 -07004930 state->file = NULL;
4931 state->ios_left = max_ios;
4932}
4933
Jens Axboe2b188cc2019-01-07 10:46:33 -07004934static void io_commit_sqring(struct io_ring_ctx *ctx)
4935{
Hristo Venev75b28af2019-08-26 17:23:46 +00004936 struct io_rings *rings = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004937
Pavel Begunkovcaf582c2019-12-30 21:24:46 +03004938 /*
4939 * Ensure any loads from the SQEs are done at this point,
4940 * since once we write the new head, the application could
4941 * write new data to them.
4942 */
4943 smp_store_release(&rings->sq.head, ctx->cached_sq_head);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004944}
4945
4946/*
Jens Axboe3529d8c2019-12-19 18:24:38 -07004947 * Fetch an sqe, if one is available. Note that sqe_ptr will point to memory
Jens Axboe2b188cc2019-01-07 10:46:33 -07004948 * that is mapped by userspace. This means that care needs to be taken to
4949 * ensure that reads are stable, as we cannot rely on userspace always
4950 * being a good citizen. If members of the sqe are validated and then later
4951 * used, it's important that those reads are done through READ_ONCE() to
4952 * prevent a re-load down the line.
4953 */
Jens Axboe3529d8c2019-12-19 18:24:38 -07004954static bool io_get_sqring(struct io_ring_ctx *ctx, struct io_kiocb *req,
4955 const struct io_uring_sqe **sqe_ptr)
Jens Axboe2b188cc2019-01-07 10:46:33 -07004956{
Hristo Venev75b28af2019-08-26 17:23:46 +00004957 u32 *sq_array = ctx->sq_array;
Jens Axboe2b188cc2019-01-07 10:46:33 -07004958 unsigned head;
4959
4960 /*
4961 * The cached sq head (or cq tail) serves two purposes:
4962 *
4963 * 1) allows us to batch the cost of updating the user visible
4964 * head updates.
4965 * 2) allows the kernel side to track the head on its own, even
4966 * though the application is the one updating it.
4967 */
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03004968 head = READ_ONCE(sq_array[ctx->cached_sq_head & ctx->sq_mask]);
Pavel Begunkov9835d6f2019-11-21 21:24:56 +03004969 if (likely(head < ctx->sq_entries)) {
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03004970 /*
4971 * All io need record the previous position, if LINK vs DARIN,
4972 * it can be used to mark the position of the first IO in the
4973 * link list.
4974 */
4975 req->sequence = ctx->cached_sq_head;
Jens Axboe3529d8c2019-12-19 18:24:38 -07004976 *sqe_ptr = &ctx->sq_sqes[head];
4977 req->opcode = READ_ONCE((*sqe_ptr)->opcode);
4978 req->user_data = READ_ONCE((*sqe_ptr)->user_data);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004979 ctx->cached_sq_head++;
4980 return true;
4981 }
4982
4983 /* drop invalid entries */
4984 ctx->cached_sq_head++;
Jens Axboe498ccd92019-10-25 10:04:25 -06004985 ctx->cached_sq_dropped++;
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03004986 WRITE_ONCE(ctx->rings->sq_dropped, ctx->cached_sq_dropped);
Jens Axboe2b188cc2019-01-07 10:46:33 -07004987 return false;
4988}
4989
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03004990static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
Pavel Begunkovae9428c2019-11-06 00:22:14 +03004991 struct file *ring_file, int ring_fd,
4992 struct mm_struct **mm, bool async)
Jens Axboe6c271ce2019-01-10 11:22:30 -07004993{
4994 struct io_submit_state state, *statep = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06004995 struct io_kiocb *link = NULL;
Jens Axboe9e645e112019-05-10 16:07:28 -06004996 int i, submitted = 0;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03004997 bool mm_fault = false;
Jens Axboe6c271ce2019-01-10 11:22:30 -07004998
Jens Axboec4a2ed72019-11-21 21:01:26 -07004999 /* if we have a backlog and couldn't flush it all, return BUSY */
Jens Axboead3eb2c2019-12-18 17:12:20 -07005000 if (test_bit(0, &ctx->sq_check_overflow)) {
5001 if (!list_empty(&ctx->cq_overflow_list) &&
5002 !io_cqring_overflow_flush(ctx, false))
5003 return -EBUSY;
5004 }
Jens Axboe6c271ce2019-01-10 11:22:30 -07005005
Pavel Begunkovee7d46d2019-12-30 21:24:45 +03005006 /* make sure SQ entry isn't read before tail */
5007 nr = min3(nr, ctx->sq_entries, io_sqring_entries(ctx));
Pavel Begunkov9ef4f122019-12-30 21:24:44 +03005008
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03005009 if (!percpu_ref_tryget_many(&ctx->refs, nr))
5010 return -EAGAIN;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005011
5012 if (nr > IO_PLUG_THRESHOLD) {
Jackie Liu22efde52019-12-02 17:14:52 +08005013 io_submit_state_start(&state, nr);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005014 statep = &state;
5015 }
5016
Pavel Begunkovb14cca02020-01-17 04:45:59 +03005017 ctx->ring_fd = ring_fd;
5018 ctx->ring_file = ring_file;
5019
Jens Axboe6c271ce2019-01-10 11:22:30 -07005020 for (i = 0; i < nr; i++) {
Jens Axboe3529d8c2019-12-19 18:24:38 -07005021 const struct io_uring_sqe *sqe;
Pavel Begunkov196be952019-11-07 01:41:06 +03005022 struct io_kiocb *req;
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005023 int err;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005024
Pavel Begunkov196be952019-11-07 01:41:06 +03005025 req = io_get_req(ctx, statep);
5026 if (unlikely(!req)) {
5027 if (!submitted)
5028 submitted = -EAGAIN;
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005029 break;
Jens Axboe9e645e112019-05-10 16:07:28 -06005030 }
Jens Axboe3529d8c2019-12-19 18:24:38 -07005031 if (!io_get_sqring(ctx, req, &sqe)) {
Pavel Begunkov2b85edf2019-12-28 14:13:03 +03005032 __io_req_do_free(req);
Pavel Begunkov196be952019-11-07 01:41:06 +03005033 break;
5034 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005035
Jens Axboed3656342019-12-18 09:50:26 -07005036 /* will complete beyond this point, count as submitted */
5037 submitted++;
5038
5039 if (unlikely(req->opcode >= IORING_OP_LAST)) {
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005040 err = -EINVAL;
5041fail_req:
5042 io_cqring_add_event(req, err);
Jens Axboed3656342019-12-18 09:50:26 -07005043 io_double_put_req(req);
5044 break;
5045 }
5046
5047 if (io_op_defs[req->opcode].needs_mm && !*mm) {
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03005048 mm_fault = mm_fault || !mmget_not_zero(ctx->sqo_mm);
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005049 if (unlikely(mm_fault)) {
5050 err = -EFAULT;
5051 goto fail_req;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03005052 }
Pavel Begunkov1cb1edb2020-02-06 21:16:09 +03005053 use_mm(ctx->sqo_mm);
5054 *mm = ctx->sqo_mm;
Pavel Begunkov95a1b3ff2019-10-27 23:15:41 +03005055 }
5056
Pavel Begunkovcf6fd4b2019-11-25 23:14:39 +03005057 req->in_async = async;
5058 req->needs_fixed_file = async;
Jens Axboe354420f2020-01-08 18:55:15 -07005059 trace_io_uring_submit_sqe(ctx, req->opcode, req->user_data,
5060 true, async);
Jens Axboe3529d8c2019-12-19 18:24:38 -07005061 if (!io_submit_sqe(req, sqe, statep, &link))
Pavel Begunkov2e6e1fd2019-12-05 16:15:45 +03005062 break;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005063 }
5064
Pavel Begunkov9466f432020-01-25 22:34:01 +03005065 if (unlikely(submitted != nr)) {
5066 int ref_used = (submitted == -EAGAIN) ? 0 : submitted;
5067
5068 percpu_ref_put_many(&ctx->refs, nr - ref_used);
5069 }
Jens Axboe9e645e112019-05-10 16:07:28 -06005070 if (link)
Pavel Begunkov1b4a51b2019-11-21 11:54:28 +03005071 io_queue_link_head(link);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005072 if (statep)
5073 io_submit_state_end(&state);
5074
Pavel Begunkovae9428c2019-11-06 00:22:14 +03005075 /* Commit SQ ring head once we've consumed and submitted all SQEs */
5076 io_commit_sqring(ctx);
5077
Jens Axboe6c271ce2019-01-10 11:22:30 -07005078 return submitted;
5079}
5080
5081static int io_sq_thread(void *data)
5082{
Jens Axboe6c271ce2019-01-10 11:22:30 -07005083 struct io_ring_ctx *ctx = data;
5084 struct mm_struct *cur_mm = NULL;
Jens Axboe181e4482019-11-25 08:52:30 -07005085 const struct cred *old_cred;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005086 mm_segment_t old_fs;
5087 DEFINE_WAIT(wait);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005088 unsigned long timeout;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005089 int ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005090
Jens Axboe206aefd2019-11-07 18:27:42 -07005091 complete(&ctx->completions[1]);
Jackie Liua4c0b3d2019-07-08 13:41:12 +08005092
Jens Axboe6c271ce2019-01-10 11:22:30 -07005093 old_fs = get_fs();
5094 set_fs(USER_DS);
Jens Axboe181e4482019-11-25 08:52:30 -07005095 old_cred = override_creds(ctx->creds);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005096
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005097 timeout = jiffies + ctx->sq_thread_idle;
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005098 while (!kthread_should_park()) {
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005099 unsigned int to_submit;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005100
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005101 if (!list_empty(&ctx->poll_list)) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07005102 unsigned nr_events = 0;
5103
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005104 mutex_lock(&ctx->uring_lock);
5105 if (!list_empty(&ctx->poll_list))
5106 io_iopoll_getevents(ctx, &nr_events, 0);
5107 else
Jens Axboe6c271ce2019-01-10 11:22:30 -07005108 timeout = jiffies + ctx->sq_thread_idle;
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005109 mutex_unlock(&ctx->uring_lock);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005110 }
5111
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005112 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07005113
5114 /*
5115 * If submit got -EBUSY, flag us as needing the application
5116 * to enter the kernel to reap and flush events.
5117 */
5118 if (!to_submit || ret == -EBUSY) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07005119 /*
Stefano Garzarella7143b5a2020-02-21 16:42:16 +01005120 * Drop cur_mm before scheduling, we can't hold it for
5121 * long periods (or over schedule()). Do this before
5122 * adding ourselves to the waitqueue, as the unuse/drop
5123 * may sleep.
5124 */
5125 if (cur_mm) {
5126 unuse_mm(cur_mm);
5127 mmput(cur_mm);
5128 cur_mm = NULL;
5129 }
5130
5131 /*
Jens Axboe6c271ce2019-01-10 11:22:30 -07005132 * We're polling. If we're within the defined idle
5133 * period, then let us spin without work before going
Jens Axboec1edbf52019-11-10 16:56:04 -07005134 * to sleep. The exception is if we got EBUSY doing
5135 * more IO, we should wait for the application to
5136 * reap events and wake us up.
Jens Axboe6c271ce2019-01-10 11:22:30 -07005137 */
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005138 if (!list_empty(&ctx->poll_list) ||
Jens Axboedf069d82020-02-04 16:48:34 -07005139 (!time_after(jiffies, timeout) && ret != -EBUSY &&
5140 !percpu_ref_is_dying(&ctx->refs))) {
Jens Axboe9831a902019-09-19 09:48:55 -06005141 cond_resched();
Jens Axboe6c271ce2019-01-10 11:22:30 -07005142 continue;
5143 }
5144
Jens Axboe6c271ce2019-01-10 11:22:30 -07005145 prepare_to_wait(&ctx->sqo_wait, &wait,
5146 TASK_INTERRUPTIBLE);
5147
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005148 /*
5149 * While doing polled IO, before going to sleep, we need
5150 * to check if there are new reqs added to poll_list, it
5151 * is because reqs may have been punted to io worker and
5152 * will be added to poll_list later, hence check the
5153 * poll_list again.
5154 */
5155 if ((ctx->flags & IORING_SETUP_IOPOLL) &&
5156 !list_empty_careful(&ctx->poll_list)) {
5157 finish_wait(&ctx->sqo_wait, &wait);
5158 continue;
5159 }
5160
Jens Axboe6c271ce2019-01-10 11:22:30 -07005161 /* Tell userspace we may need a wakeup call */
Hristo Venev75b28af2019-08-26 17:23:46 +00005162 ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP;
Stefan Bühler0d7bae62019-04-19 11:57:45 +02005163 /* make sure to read SQ tail after writing flags */
5164 smp_mb();
Jens Axboe6c271ce2019-01-10 11:22:30 -07005165
Pavel Begunkovfb5ccc92019-10-25 12:31:30 +03005166 to_submit = io_sqring_entries(ctx);
Jens Axboec1edbf52019-11-10 16:56:04 -07005167 if (!to_submit || ret == -EBUSY) {
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005168 if (kthread_should_park()) {
Jens Axboe6c271ce2019-01-10 11:22:30 -07005169 finish_wait(&ctx->sqo_wait, &wait);
5170 break;
5171 }
5172 if (signal_pending(current))
5173 flush_signals(current);
5174 schedule();
5175 finish_wait(&ctx->sqo_wait, &wait);
5176
Hristo Venev75b28af2019-08-26 17:23:46 +00005177 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005178 continue;
5179 }
5180 finish_wait(&ctx->sqo_wait, &wait);
5181
Hristo Venev75b28af2019-08-26 17:23:46 +00005182 ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005183 }
5184
Jens Axboe8a4955f2019-12-09 14:52:35 -07005185 mutex_lock(&ctx->uring_lock);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005186 ret = io_submit_sqes(ctx, to_submit, NULL, -1, &cur_mm, true);
Jens Axboe8a4955f2019-12-09 14:52:35 -07005187 mutex_unlock(&ctx->uring_lock);
Xiaoguang Wangbdcd3ea2020-02-25 22:12:08 +08005188 timeout = jiffies + ctx->sq_thread_idle;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005189 }
5190
5191 set_fs(old_fs);
5192 if (cur_mm) {
5193 unuse_mm(cur_mm);
5194 mmput(cur_mm);
5195 }
Jens Axboe181e4482019-11-25 08:52:30 -07005196 revert_creds(old_cred);
Jens Axboe06058632019-04-13 09:26:03 -06005197
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005198 kthread_parkme();
Jens Axboe06058632019-04-13 09:26:03 -06005199
Jens Axboe6c271ce2019-01-10 11:22:30 -07005200 return 0;
5201}
5202
Jens Axboebda52162019-09-24 13:47:15 -06005203struct io_wait_queue {
5204 struct wait_queue_entry wq;
5205 struct io_ring_ctx *ctx;
5206 unsigned to_wait;
5207 unsigned nr_timeouts;
5208};
5209
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005210static inline bool io_should_wake(struct io_wait_queue *iowq, bool noflush)
Jens Axboebda52162019-09-24 13:47:15 -06005211{
5212 struct io_ring_ctx *ctx = iowq->ctx;
5213
5214 /*
Brian Gianforcarod195a662019-12-13 03:09:50 -08005215 * Wake up if we have enough events, or if a timeout occurred since we
Jens Axboebda52162019-09-24 13:47:15 -06005216 * started waiting. For timeouts, we always want to return to userspace,
5217 * regardless of event count.
5218 */
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005219 return io_cqring_events(ctx, noflush) >= iowq->to_wait ||
Jens Axboebda52162019-09-24 13:47:15 -06005220 atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts;
5221}
5222
5223static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode,
5224 int wake_flags, void *key)
5225{
5226 struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue,
5227 wq);
5228
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005229 /* use noflush == true, as we can't safely rely on locking context */
5230 if (!io_should_wake(iowq, true))
Jens Axboebda52162019-09-24 13:47:15 -06005231 return -1;
5232
5233 return autoremove_wake_function(curr, mode, wake_flags, key);
5234}
5235
Jens Axboe2b188cc2019-01-07 10:46:33 -07005236/*
5237 * Wait until events become available, if we don't already have some. The
5238 * application must reap them itself, as they reside on the shared cq ring.
5239 */
5240static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
5241 const sigset_t __user *sig, size_t sigsz)
5242{
Jens Axboebda52162019-09-24 13:47:15 -06005243 struct io_wait_queue iowq = {
5244 .wq = {
5245 .private = current,
5246 .func = io_wake_function,
5247 .entry = LIST_HEAD_INIT(iowq.wq.entry),
5248 },
5249 .ctx = ctx,
5250 .to_wait = min_events,
5251 };
Hristo Venev75b28af2019-08-26 17:23:46 +00005252 struct io_rings *rings = ctx->rings;
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005253 int ret = 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005254
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005255 if (io_cqring_events(ctx, false) >= min_events)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005256 return 0;
5257
5258 if (sig) {
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005259#ifdef CONFIG_COMPAT
5260 if (in_compat_syscall())
5261 ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig,
Oleg Nesterovb7724342019-07-16 16:29:53 -07005262 sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005263 else
5264#endif
Oleg Nesterovb7724342019-07-16 16:29:53 -07005265 ret = set_user_sigmask(sig, sigsz);
Arnd Bergmann9e75ad52019-03-25 15:34:53 +01005266
Jens Axboe2b188cc2019-01-07 10:46:33 -07005267 if (ret)
5268 return ret;
5269 }
5270
Jens Axboebda52162019-09-24 13:47:15 -06005271 iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02005272 trace_io_uring_cqring_wait(ctx, min_events);
Jens Axboebda52162019-09-24 13:47:15 -06005273 do {
5274 prepare_to_wait_exclusive(&ctx->wait, &iowq.wq,
5275 TASK_INTERRUPTIBLE);
Jens Axboe1d7bb1d2019-11-06 11:31:17 -07005276 if (io_should_wake(&iowq, false))
Jens Axboebda52162019-09-24 13:47:15 -06005277 break;
5278 schedule();
5279 if (signal_pending(current)) {
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005280 ret = -EINTR;
Jens Axboebda52162019-09-24 13:47:15 -06005281 break;
5282 }
5283 } while (1);
5284 finish_wait(&ctx->wait, &iowq.wq);
5285
Jackie Liue9ffa5c2019-10-29 11:16:42 +08005286 restore_saved_sigmask_unless(ret == -EINTR);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005287
Hristo Venev75b28af2019-08-26 17:23:46 +00005288 return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0;
Jens Axboe2b188cc2019-01-07 10:46:33 -07005289}
5290
Jens Axboe6b063142019-01-10 22:13:58 -07005291static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
5292{
5293#if defined(CONFIG_UNIX)
5294 if (ctx->ring_sock) {
5295 struct sock *sock = ctx->ring_sock->sk;
5296 struct sk_buff *skb;
5297
5298 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
5299 kfree_skb(skb);
5300 }
5301#else
5302 int i;
5303
Jens Axboe65e19f52019-10-26 07:20:21 -06005304 for (i = 0; i < ctx->nr_user_files; i++) {
5305 struct file *file;
5306
5307 file = io_file_from_index(ctx, i);
5308 if (file)
5309 fput(file);
5310 }
Jens Axboe6b063142019-01-10 22:13:58 -07005311#endif
5312}
5313
Jens Axboe05f3fb32019-12-09 11:22:50 -07005314static void io_file_ref_kill(struct percpu_ref *ref)
5315{
5316 struct fixed_file_data *data;
5317
5318 data = container_of(ref, struct fixed_file_data, refs);
5319 complete(&data->done);
5320}
5321
Jens Axboe6b063142019-01-10 22:13:58 -07005322static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
5323{
Jens Axboe05f3fb32019-12-09 11:22:50 -07005324 struct fixed_file_data *data = ctx->file_data;
Jens Axboe65e19f52019-10-26 07:20:21 -06005325 unsigned nr_tables, i;
5326
Jens Axboe05f3fb32019-12-09 11:22:50 -07005327 if (!data)
Jens Axboe6b063142019-01-10 22:13:58 -07005328 return -ENXIO;
5329
Jens Axboe05f3fb32019-12-09 11:22:50 -07005330 percpu_ref_kill_and_confirm(&data->refs, io_file_ref_kill);
Jens Axboee46a7952020-01-17 11:15:34 -07005331 flush_work(&data->ref_work);
Jens Axboe2faf8522020-02-04 19:54:55 -07005332 wait_for_completion(&data->done);
5333 io_ring_file_ref_flush(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005334 percpu_ref_exit(&data->refs);
5335
Jens Axboe6b063142019-01-10 22:13:58 -07005336 __io_sqe_files_unregister(ctx);
Jens Axboe65e19f52019-10-26 07:20:21 -06005337 nr_tables = DIV_ROUND_UP(ctx->nr_user_files, IORING_MAX_FILES_TABLE);
5338 for (i = 0; i < nr_tables; i++)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005339 kfree(data->table[i].files);
5340 kfree(data->table);
5341 kfree(data);
5342 ctx->file_data = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07005343 ctx->nr_user_files = 0;
5344 return 0;
5345}
5346
Jens Axboe6c271ce2019-01-10 11:22:30 -07005347static void io_sq_thread_stop(struct io_ring_ctx *ctx)
5348{
5349 if (ctx->sqo_thread) {
Jens Axboe206aefd2019-11-07 18:27:42 -07005350 wait_for_completion(&ctx->completions[1]);
Roman Penyaev2bbcd6d2019-05-16 10:53:57 +02005351 /*
5352 * The park is a bit of a work-around, without it we get
5353 * warning spews on shutdown with SQPOLL set and affinity
5354 * set to a single CPU.
5355 */
Jens Axboe06058632019-04-13 09:26:03 -06005356 kthread_park(ctx->sqo_thread);
Jens Axboe6c271ce2019-01-10 11:22:30 -07005357 kthread_stop(ctx->sqo_thread);
5358 ctx->sqo_thread = NULL;
5359 }
5360}
5361
Jens Axboe6b063142019-01-10 22:13:58 -07005362static void io_finish_async(struct io_ring_ctx *ctx)
5363{
Jens Axboe6c271ce2019-01-10 11:22:30 -07005364 io_sq_thread_stop(ctx);
5365
Jens Axboe561fb042019-10-24 07:25:42 -06005366 if (ctx->io_wq) {
5367 io_wq_destroy(ctx->io_wq);
5368 ctx->io_wq = NULL;
Jens Axboe6b063142019-01-10 22:13:58 -07005369 }
5370}
5371
5372#if defined(CONFIG_UNIX)
Jens Axboe6b063142019-01-10 22:13:58 -07005373/*
5374 * Ensure the UNIX gc is aware of our file set, so we are certain that
5375 * the io_uring can be safely unregistered on process exit, even if we have
5376 * loops in the file referencing.
5377 */
5378static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
5379{
5380 struct sock *sk = ctx->ring_sock->sk;
5381 struct scm_fp_list *fpl;
5382 struct sk_buff *skb;
Jens Axboe08a45172019-10-03 08:11:03 -06005383 int i, nr_files;
Jens Axboe6b063142019-01-10 22:13:58 -07005384
5385 if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) {
5386 unsigned long inflight = ctx->user->unix_inflight + nr;
5387
5388 if (inflight > task_rlimit(current, RLIMIT_NOFILE))
5389 return -EMFILE;
5390 }
5391
5392 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
5393 if (!fpl)
5394 return -ENOMEM;
5395
5396 skb = alloc_skb(0, GFP_KERNEL);
5397 if (!skb) {
5398 kfree(fpl);
5399 return -ENOMEM;
5400 }
5401
5402 skb->sk = sk;
Jens Axboe6b063142019-01-10 22:13:58 -07005403
Jens Axboe08a45172019-10-03 08:11:03 -06005404 nr_files = 0;
Jens Axboe6b063142019-01-10 22:13:58 -07005405 fpl->user = get_uid(ctx->user);
5406 for (i = 0; i < nr; i++) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005407 struct file *file = io_file_from_index(ctx, i + offset);
5408
5409 if (!file)
Jens Axboe08a45172019-10-03 08:11:03 -06005410 continue;
Jens Axboe65e19f52019-10-26 07:20:21 -06005411 fpl->fp[nr_files] = get_file(file);
Jens Axboe08a45172019-10-03 08:11:03 -06005412 unix_inflight(fpl->user, fpl->fp[nr_files]);
5413 nr_files++;
Jens Axboe6b063142019-01-10 22:13:58 -07005414 }
5415
Jens Axboe08a45172019-10-03 08:11:03 -06005416 if (nr_files) {
5417 fpl->max = SCM_MAX_FD;
5418 fpl->count = nr_files;
5419 UNIXCB(skb).fp = fpl;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005420 skb->destructor = unix_destruct_scm;
Jens Axboe08a45172019-10-03 08:11:03 -06005421 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
5422 skb_queue_head(&sk->sk_receive_queue, skb);
Jens Axboe6b063142019-01-10 22:13:58 -07005423
Jens Axboe08a45172019-10-03 08:11:03 -06005424 for (i = 0; i < nr_files; i++)
5425 fput(fpl->fp[i]);
5426 } else {
5427 kfree_skb(skb);
5428 kfree(fpl);
5429 }
Jens Axboe6b063142019-01-10 22:13:58 -07005430
5431 return 0;
5432}
5433
5434/*
5435 * If UNIX sockets are enabled, fd passing can cause a reference cycle which
5436 * causes regular reference counting to break down. We rely on the UNIX
5437 * garbage collection to take care of this problem for us.
5438 */
5439static int io_sqe_files_scm(struct io_ring_ctx *ctx)
5440{
5441 unsigned left, total;
5442 int ret = 0;
5443
5444 total = 0;
5445 left = ctx->nr_user_files;
5446 while (left) {
5447 unsigned this_files = min_t(unsigned, left, SCM_MAX_FD);
Jens Axboe6b063142019-01-10 22:13:58 -07005448
5449 ret = __io_sqe_files_scm(ctx, this_files, total);
5450 if (ret)
5451 break;
5452 left -= this_files;
5453 total += this_files;
5454 }
5455
5456 if (!ret)
5457 return 0;
5458
5459 while (total < ctx->nr_user_files) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005460 struct file *file = io_file_from_index(ctx, total);
5461
5462 if (file)
5463 fput(file);
Jens Axboe6b063142019-01-10 22:13:58 -07005464 total++;
5465 }
5466
5467 return ret;
5468}
5469#else
5470static int io_sqe_files_scm(struct io_ring_ctx *ctx)
5471{
5472 return 0;
5473}
5474#endif
5475
Jens Axboe65e19f52019-10-26 07:20:21 -06005476static int io_sqe_alloc_file_tables(struct io_ring_ctx *ctx, unsigned nr_tables,
5477 unsigned nr_files)
5478{
5479 int i;
5480
5481 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005482 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06005483 unsigned this_files;
5484
5485 this_files = min(nr_files, IORING_MAX_FILES_TABLE);
5486 table->files = kcalloc(this_files, sizeof(struct file *),
5487 GFP_KERNEL);
5488 if (!table->files)
5489 break;
5490 nr_files -= this_files;
5491 }
5492
5493 if (i == nr_tables)
5494 return 0;
5495
5496 for (i = 0; i < nr_tables; i++) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005497 struct fixed_file_table *table = &ctx->file_data->table[i];
Jens Axboe65e19f52019-10-26 07:20:21 -06005498 kfree(table->files);
5499 }
5500 return 1;
5501}
5502
Jens Axboe05f3fb32019-12-09 11:22:50 -07005503static void io_ring_file_put(struct io_ring_ctx *ctx, struct file *file)
Jens Axboec3a31e62019-10-03 13:59:56 -06005504{
5505#if defined(CONFIG_UNIX)
Jens Axboec3a31e62019-10-03 13:59:56 -06005506 struct sock *sock = ctx->ring_sock->sk;
5507 struct sk_buff_head list, *head = &sock->sk_receive_queue;
5508 struct sk_buff *skb;
5509 int i;
5510
5511 __skb_queue_head_init(&list);
5512
5513 /*
5514 * Find the skb that holds this file in its SCM_RIGHTS. When found,
5515 * remove this entry and rearrange the file array.
5516 */
5517 skb = skb_dequeue(head);
5518 while (skb) {
5519 struct scm_fp_list *fp;
5520
5521 fp = UNIXCB(skb).fp;
5522 for (i = 0; i < fp->count; i++) {
5523 int left;
5524
5525 if (fp->fp[i] != file)
5526 continue;
5527
5528 unix_notinflight(fp->user, fp->fp[i]);
5529 left = fp->count - 1 - i;
5530 if (left) {
5531 memmove(&fp->fp[i], &fp->fp[i + 1],
5532 left * sizeof(struct file *));
5533 }
5534 fp->count--;
5535 if (!fp->count) {
5536 kfree_skb(skb);
5537 skb = NULL;
5538 } else {
5539 __skb_queue_tail(&list, skb);
5540 }
5541 fput(file);
5542 file = NULL;
5543 break;
5544 }
5545
5546 if (!file)
5547 break;
5548
5549 __skb_queue_tail(&list, skb);
5550
5551 skb = skb_dequeue(head);
5552 }
5553
5554 if (skb_peek(&list)) {
5555 spin_lock_irq(&head->lock);
5556 while ((skb = __skb_dequeue(&list)) != NULL)
5557 __skb_queue_tail(head, skb);
5558 spin_unlock_irq(&head->lock);
5559 }
5560#else
Jens Axboe05f3fb32019-12-09 11:22:50 -07005561 fput(file);
Jens Axboec3a31e62019-10-03 13:59:56 -06005562#endif
5563}
5564
Jens Axboe05f3fb32019-12-09 11:22:50 -07005565struct io_file_put {
5566 struct llist_node llist;
5567 struct file *file;
5568 struct completion *done;
5569};
5570
Jens Axboe2faf8522020-02-04 19:54:55 -07005571static void io_ring_file_ref_flush(struct fixed_file_data *data)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005572{
5573 struct io_file_put *pfile, *tmp;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005574 struct llist_node *node;
5575
Jens Axboe05f3fb32019-12-09 11:22:50 -07005576 while ((node = llist_del_all(&data->put_llist)) != NULL) {
5577 llist_for_each_entry_safe(pfile, tmp, node, llist) {
5578 io_ring_file_put(data->ctx, pfile->file);
5579 if (pfile->done)
5580 complete(pfile->done);
5581 else
5582 kfree(pfile);
5583 }
5584 }
Jens Axboe2faf8522020-02-04 19:54:55 -07005585}
Jens Axboe05f3fb32019-12-09 11:22:50 -07005586
Jens Axboe2faf8522020-02-04 19:54:55 -07005587static void io_ring_file_ref_switch(struct work_struct *work)
5588{
5589 struct fixed_file_data *data;
5590
5591 data = container_of(work, struct fixed_file_data, ref_work);
5592 io_ring_file_ref_flush(data);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005593 percpu_ref_switch_to_percpu(&data->refs);
5594}
5595
5596static void io_file_data_ref_zero(struct percpu_ref *ref)
5597{
5598 struct fixed_file_data *data;
5599
5600 data = container_of(ref, struct fixed_file_data, refs);
5601
Jens Axboe2faf8522020-02-04 19:54:55 -07005602 /*
5603 * We can't safely switch from inside this context, punt to wq. If
5604 * the table ref is going away, the table is being unregistered.
5605 * Don't queue up the async work for that case, the caller will
5606 * handle it.
5607 */
5608 if (!percpu_ref_is_dying(&data->refs))
5609 queue_work(system_wq, &data->ref_work);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005610}
5611
5612static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
5613 unsigned nr_args)
5614{
5615 __s32 __user *fds = (__s32 __user *) arg;
5616 unsigned nr_tables;
5617 struct file *file;
5618 int fd, ret = 0;
5619 unsigned i;
5620
5621 if (ctx->file_data)
5622 return -EBUSY;
5623 if (!nr_args)
5624 return -EINVAL;
5625 if (nr_args > IORING_MAX_FIXED_FILES)
5626 return -EMFILE;
5627
5628 ctx->file_data = kzalloc(sizeof(*ctx->file_data), GFP_KERNEL);
5629 if (!ctx->file_data)
5630 return -ENOMEM;
5631 ctx->file_data->ctx = ctx;
5632 init_completion(&ctx->file_data->done);
5633
5634 nr_tables = DIV_ROUND_UP(nr_args, IORING_MAX_FILES_TABLE);
5635 ctx->file_data->table = kcalloc(nr_tables,
5636 sizeof(struct fixed_file_table),
5637 GFP_KERNEL);
5638 if (!ctx->file_data->table) {
5639 kfree(ctx->file_data);
5640 ctx->file_data = NULL;
5641 return -ENOMEM;
5642 }
5643
5644 if (percpu_ref_init(&ctx->file_data->refs, io_file_data_ref_zero,
5645 PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) {
5646 kfree(ctx->file_data->table);
5647 kfree(ctx->file_data);
5648 ctx->file_data = NULL;
5649 return -ENOMEM;
5650 }
5651 ctx->file_data->put_llist.first = NULL;
5652 INIT_WORK(&ctx->file_data->ref_work, io_ring_file_ref_switch);
5653
5654 if (io_sqe_alloc_file_tables(ctx, nr_tables, nr_args)) {
5655 percpu_ref_exit(&ctx->file_data->refs);
5656 kfree(ctx->file_data->table);
5657 kfree(ctx->file_data);
5658 ctx->file_data = NULL;
5659 return -ENOMEM;
5660 }
5661
5662 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
5663 struct fixed_file_table *table;
5664 unsigned index;
5665
5666 ret = -EFAULT;
5667 if (copy_from_user(&fd, &fds[i], sizeof(fd)))
5668 break;
5669 /* allow sparse sets */
5670 if (fd == -1) {
5671 ret = 0;
5672 continue;
5673 }
5674
5675 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
5676 index = i & IORING_FILE_TABLE_MASK;
5677 file = fget(fd);
5678
5679 ret = -EBADF;
5680 if (!file)
5681 break;
5682
5683 /*
5684 * Don't allow io_uring instances to be registered. If UNIX
5685 * isn't enabled, then this causes a reference cycle and this
5686 * instance can never get freed. If UNIX is enabled we'll
5687 * handle it just fine, but there's still no point in allowing
5688 * a ring fd as it doesn't support regular read/write anyway.
5689 */
5690 if (file->f_op == &io_uring_fops) {
5691 fput(file);
5692 break;
5693 }
5694 ret = 0;
5695 table->files[index] = file;
5696 }
5697
5698 if (ret) {
5699 for (i = 0; i < ctx->nr_user_files; i++) {
5700 file = io_file_from_index(ctx, i);
5701 if (file)
5702 fput(file);
5703 }
5704 for (i = 0; i < nr_tables; i++)
5705 kfree(ctx->file_data->table[i].files);
5706
5707 kfree(ctx->file_data->table);
5708 kfree(ctx->file_data);
5709 ctx->file_data = NULL;
5710 ctx->nr_user_files = 0;
5711 return ret;
5712 }
5713
5714 ret = io_sqe_files_scm(ctx);
5715 if (ret)
5716 io_sqe_files_unregister(ctx);
5717
5718 return ret;
5719}
5720
Jens Axboec3a31e62019-10-03 13:59:56 -06005721static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
5722 int index)
5723{
5724#if defined(CONFIG_UNIX)
5725 struct sock *sock = ctx->ring_sock->sk;
5726 struct sk_buff_head *head = &sock->sk_receive_queue;
5727 struct sk_buff *skb;
5728
5729 /*
5730 * See if we can merge this file into an existing skb SCM_RIGHTS
5731 * file set. If there's no room, fall back to allocating a new skb
5732 * and filling it in.
5733 */
5734 spin_lock_irq(&head->lock);
5735 skb = skb_peek(head);
5736 if (skb) {
5737 struct scm_fp_list *fpl = UNIXCB(skb).fp;
5738
5739 if (fpl->count < SCM_MAX_FD) {
5740 __skb_unlink(skb, head);
5741 spin_unlock_irq(&head->lock);
5742 fpl->fp[fpl->count] = get_file(file);
5743 unix_inflight(fpl->user, fpl->fp[fpl->count]);
5744 fpl->count++;
5745 spin_lock_irq(&head->lock);
5746 __skb_queue_head(head, skb);
5747 } else {
5748 skb = NULL;
5749 }
5750 }
5751 spin_unlock_irq(&head->lock);
5752
5753 if (skb) {
5754 fput(file);
5755 return 0;
5756 }
5757
5758 return __io_sqe_files_scm(ctx, 1, index);
5759#else
5760 return 0;
5761#endif
5762}
5763
Jens Axboe05f3fb32019-12-09 11:22:50 -07005764static void io_atomic_switch(struct percpu_ref *ref)
Jens Axboec3a31e62019-10-03 13:59:56 -06005765{
Jens Axboe05f3fb32019-12-09 11:22:50 -07005766 struct fixed_file_data *data;
5767
Jens Axboedd3db2a2020-02-26 10:23:43 -07005768 /*
5769 * Juggle reference to ensure we hit zero, if needed, so we can
5770 * switch back to percpu mode
5771 */
Jens Axboe05f3fb32019-12-09 11:22:50 -07005772 data = container_of(ref, struct fixed_file_data, refs);
Jens Axboedd3db2a2020-02-26 10:23:43 -07005773 percpu_ref_put(&data->refs);
5774 percpu_ref_get(&data->refs);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005775}
5776
5777static bool io_queue_file_removal(struct fixed_file_data *data,
5778 struct file *file)
5779{
5780 struct io_file_put *pfile, pfile_stack;
5781 DECLARE_COMPLETION_ONSTACK(done);
5782
5783 /*
5784 * If we fail allocating the struct we need for doing async reomval
5785 * of this file, just punt to sync and wait for it.
5786 */
5787 pfile = kzalloc(sizeof(*pfile), GFP_KERNEL);
5788 if (!pfile) {
5789 pfile = &pfile_stack;
5790 pfile->done = &done;
5791 }
5792
5793 pfile->file = file;
5794 llist_add(&pfile->llist, &data->put_llist);
5795
5796 if (pfile == &pfile_stack) {
Jens Axboedd3db2a2020-02-26 10:23:43 -07005797 percpu_ref_switch_to_atomic(&data->refs, io_atomic_switch);
Jens Axboe05f3fb32019-12-09 11:22:50 -07005798 wait_for_completion(&done);
5799 flush_work(&data->ref_work);
5800 return false;
5801 }
5802
5803 return true;
5804}
5805
5806static int __io_sqe_files_update(struct io_ring_ctx *ctx,
5807 struct io_uring_files_update *up,
5808 unsigned nr_args)
5809{
5810 struct fixed_file_data *data = ctx->file_data;
5811 bool ref_switch = false;
5812 struct file *file;
Jens Axboec3a31e62019-10-03 13:59:56 -06005813 __s32 __user *fds;
5814 int fd, i, err;
5815 __u32 done;
5816
Jens Axboe05f3fb32019-12-09 11:22:50 -07005817 if (check_add_overflow(up->offset, nr_args, &done))
Jens Axboec3a31e62019-10-03 13:59:56 -06005818 return -EOVERFLOW;
5819 if (done > ctx->nr_user_files)
5820 return -EINVAL;
5821
5822 done = 0;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005823 fds = u64_to_user_ptr(up->fds);
Jens Axboec3a31e62019-10-03 13:59:56 -06005824 while (nr_args) {
Jens Axboe65e19f52019-10-26 07:20:21 -06005825 struct fixed_file_table *table;
5826 unsigned index;
5827
Jens Axboec3a31e62019-10-03 13:59:56 -06005828 err = 0;
5829 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
5830 err = -EFAULT;
5831 break;
5832 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07005833 i = array_index_nospec(up->offset, ctx->nr_user_files);
5834 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
Jens Axboe65e19f52019-10-26 07:20:21 -06005835 index = i & IORING_FILE_TABLE_MASK;
5836 if (table->files[index]) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07005837 file = io_file_from_index(ctx, index);
Jens Axboe65e19f52019-10-26 07:20:21 -06005838 table->files[index] = NULL;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005839 if (io_queue_file_removal(data, file))
5840 ref_switch = true;
Jens Axboec3a31e62019-10-03 13:59:56 -06005841 }
5842 if (fd != -1) {
Jens Axboec3a31e62019-10-03 13:59:56 -06005843 file = fget(fd);
5844 if (!file) {
5845 err = -EBADF;
5846 break;
5847 }
5848 /*
5849 * Don't allow io_uring instances to be registered. If
5850 * UNIX isn't enabled, then this causes a reference
5851 * cycle and this instance can never get freed. If UNIX
5852 * is enabled we'll handle it just fine, but there's
5853 * still no point in allowing a ring fd as it doesn't
5854 * support regular read/write anyway.
5855 */
5856 if (file->f_op == &io_uring_fops) {
5857 fput(file);
5858 err = -EBADF;
5859 break;
5860 }
Jens Axboe65e19f52019-10-26 07:20:21 -06005861 table->files[index] = file;
Jens Axboec3a31e62019-10-03 13:59:56 -06005862 err = io_sqe_file_register(ctx, file, i);
5863 if (err)
5864 break;
5865 }
5866 nr_args--;
5867 done++;
Jens Axboe05f3fb32019-12-09 11:22:50 -07005868 up->offset++;
5869 }
5870
Jens Axboedd3db2a2020-02-26 10:23:43 -07005871 if (ref_switch)
Jens Axboe05f3fb32019-12-09 11:22:50 -07005872 percpu_ref_switch_to_atomic(&data->refs, io_atomic_switch);
Jens Axboec3a31e62019-10-03 13:59:56 -06005873
5874 return done ? done : err;
5875}
Jens Axboe05f3fb32019-12-09 11:22:50 -07005876static int io_sqe_files_update(struct io_ring_ctx *ctx, void __user *arg,
5877 unsigned nr_args)
5878{
5879 struct io_uring_files_update up;
5880
5881 if (!ctx->file_data)
5882 return -ENXIO;
5883 if (!nr_args)
5884 return -EINVAL;
5885 if (copy_from_user(&up, arg, sizeof(up)))
5886 return -EFAULT;
5887 if (up.resv)
5888 return -EINVAL;
5889
5890 return __io_sqe_files_update(ctx, &up, nr_args);
5891}
Jens Axboec3a31e62019-10-03 13:59:56 -06005892
Jens Axboe7d723062019-11-12 22:31:31 -07005893static void io_put_work(struct io_wq_work *work)
5894{
5895 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
5896
5897 io_put_req(req);
5898}
5899
5900static void io_get_work(struct io_wq_work *work)
5901{
5902 struct io_kiocb *req = container_of(work, struct io_kiocb, work);
5903
5904 refcount_inc(&req->refs);
5905}
5906
Pavel Begunkov24369c22020-01-28 03:15:48 +03005907static int io_init_wq_offload(struct io_ring_ctx *ctx,
5908 struct io_uring_params *p)
5909{
5910 struct io_wq_data data;
5911 struct fd f;
5912 struct io_ring_ctx *ctx_attach;
5913 unsigned int concurrency;
5914 int ret = 0;
5915
5916 data.user = ctx->user;
5917 data.get_work = io_get_work;
5918 data.put_work = io_put_work;
5919
5920 if (!(p->flags & IORING_SETUP_ATTACH_WQ)) {
5921 /* Do QD, or 4 * CPUS, whatever is smallest */
5922 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
5923
5924 ctx->io_wq = io_wq_create(concurrency, &data);
5925 if (IS_ERR(ctx->io_wq)) {
5926 ret = PTR_ERR(ctx->io_wq);
5927 ctx->io_wq = NULL;
5928 }
5929 return ret;
5930 }
5931
5932 f = fdget(p->wq_fd);
5933 if (!f.file)
5934 return -EBADF;
5935
5936 if (f.file->f_op != &io_uring_fops) {
5937 ret = -EINVAL;
5938 goto out_fput;
5939 }
5940
5941 ctx_attach = f.file->private_data;
5942 /* @io_wq is protected by holding the fd */
5943 if (!io_wq_get(ctx_attach->io_wq, &data)) {
5944 ret = -EINVAL;
5945 goto out_fput;
5946 }
5947
5948 ctx->io_wq = ctx_attach->io_wq;
5949out_fput:
5950 fdput(f);
5951 return ret;
5952}
5953
Jens Axboe6c271ce2019-01-10 11:22:30 -07005954static int io_sq_offload_start(struct io_ring_ctx *ctx,
5955 struct io_uring_params *p)
Jens Axboe2b188cc2019-01-07 10:46:33 -07005956{
5957 int ret;
5958
Jens Axboe6c271ce2019-01-10 11:22:30 -07005959 init_waitqueue_head(&ctx->sqo_wait);
Jens Axboe2b188cc2019-01-07 10:46:33 -07005960 mmgrab(current->mm);
5961 ctx->sqo_mm = current->mm;
5962
Jens Axboe6c271ce2019-01-10 11:22:30 -07005963 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboe3ec482d2019-04-08 10:51:01 -06005964 ret = -EPERM;
5965 if (!capable(CAP_SYS_ADMIN))
5966 goto err;
5967
Jens Axboe917257d2019-04-13 09:28:55 -06005968 ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
5969 if (!ctx->sq_thread_idle)
5970 ctx->sq_thread_idle = HZ;
5971
Jens Axboe6c271ce2019-01-10 11:22:30 -07005972 if (p->flags & IORING_SETUP_SQ_AFF) {
Jens Axboe44a9bd12019-05-14 20:00:30 -06005973 int cpu = p->sq_thread_cpu;
Jens Axboe6c271ce2019-01-10 11:22:30 -07005974
Jens Axboe917257d2019-04-13 09:28:55 -06005975 ret = -EINVAL;
Jens Axboe44a9bd12019-05-14 20:00:30 -06005976 if (cpu >= nr_cpu_ids)
5977 goto err;
Shenghui Wang7889f442019-05-07 16:03:19 +08005978 if (!cpu_online(cpu))
Jens Axboe917257d2019-04-13 09:28:55 -06005979 goto err;
5980
Jens Axboe6c271ce2019-01-10 11:22:30 -07005981 ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
5982 ctx, cpu,
5983 "io_uring-sq");
5984 } else {
5985 ctx->sqo_thread = kthread_create(io_sq_thread, ctx,
5986 "io_uring-sq");
5987 }
5988 if (IS_ERR(ctx->sqo_thread)) {
5989 ret = PTR_ERR(ctx->sqo_thread);
5990 ctx->sqo_thread = NULL;
5991 goto err;
5992 }
5993 wake_up_process(ctx->sqo_thread);
5994 } else if (p->flags & IORING_SETUP_SQ_AFF) {
5995 /* Can't have SQ_AFF without SQPOLL */
5996 ret = -EINVAL;
5997 goto err;
5998 }
5999
Pavel Begunkov24369c22020-01-28 03:15:48 +03006000 ret = io_init_wq_offload(ctx, p);
6001 if (ret)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006002 goto err;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006003
6004 return 0;
6005err:
Jens Axboe54a91f32019-09-10 09:15:04 -06006006 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006007 mmdrop(ctx->sqo_mm);
6008 ctx->sqo_mm = NULL;
6009 return ret;
6010}
6011
6012static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages)
6013{
6014 atomic_long_sub(nr_pages, &user->locked_vm);
6015}
6016
6017static int io_account_mem(struct user_struct *user, unsigned long nr_pages)
6018{
6019 unsigned long page_limit, cur_pages, new_pages;
6020
6021 /* Don't allow more pages than we can safely lock */
6022 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
6023
6024 do {
6025 cur_pages = atomic_long_read(&user->locked_vm);
6026 new_pages = cur_pages + nr_pages;
6027 if (new_pages > page_limit)
6028 return -ENOMEM;
6029 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
6030 new_pages) != cur_pages);
6031
6032 return 0;
6033}
6034
6035static void io_mem_free(void *ptr)
6036{
Mark Rutland52e04ef2019-04-30 17:30:21 +01006037 struct page *page;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006038
Mark Rutland52e04ef2019-04-30 17:30:21 +01006039 if (!ptr)
6040 return;
6041
6042 page = virt_to_head_page(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006043 if (put_page_testzero(page))
6044 free_compound_page(page);
6045}
6046
6047static void *io_mem_alloc(size_t size)
6048{
6049 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP |
6050 __GFP_NORETRY;
6051
6052 return (void *) __get_free_pages(gfp_flags, get_order(size));
6053}
6054
Hristo Venev75b28af2019-08-26 17:23:46 +00006055static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries,
6056 size_t *sq_offset)
6057{
6058 struct io_rings *rings;
6059 size_t off, sq_array_size;
6060
6061 off = struct_size(rings, cqes, cq_entries);
6062 if (off == SIZE_MAX)
6063 return SIZE_MAX;
6064
6065#ifdef CONFIG_SMP
6066 off = ALIGN(off, SMP_CACHE_BYTES);
6067 if (off == 0)
6068 return SIZE_MAX;
6069#endif
6070
6071 sq_array_size = array_size(sizeof(u32), sq_entries);
6072 if (sq_array_size == SIZE_MAX)
6073 return SIZE_MAX;
6074
6075 if (check_add_overflow(off, sq_array_size, &off))
6076 return SIZE_MAX;
6077
6078 if (sq_offset)
6079 *sq_offset = off;
6080
6081 return off;
6082}
6083
Jens Axboe2b188cc2019-01-07 10:46:33 -07006084static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries)
6085{
Hristo Venev75b28af2019-08-26 17:23:46 +00006086 size_t pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006087
Hristo Venev75b28af2019-08-26 17:23:46 +00006088 pages = (size_t)1 << get_order(
6089 rings_size(sq_entries, cq_entries, NULL));
6090 pages += (size_t)1 << get_order(
6091 array_size(sizeof(struct io_uring_sqe), sq_entries));
Jens Axboe2b188cc2019-01-07 10:46:33 -07006092
Hristo Venev75b28af2019-08-26 17:23:46 +00006093 return pages;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006094}
6095
Jens Axboeedafcce2019-01-09 09:16:05 -07006096static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx)
6097{
6098 int i, j;
6099
6100 if (!ctx->user_bufs)
6101 return -ENXIO;
6102
6103 for (i = 0; i < ctx->nr_user_bufs; i++) {
6104 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
6105
6106 for (j = 0; j < imu->nr_bvecs; j++)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08006107 unpin_user_page(imu->bvec[j].bv_page);
Jens Axboeedafcce2019-01-09 09:16:05 -07006108
6109 if (ctx->account_mem)
6110 io_unaccount_mem(ctx->user, imu->nr_bvecs);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006111 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07006112 imu->nr_bvecs = 0;
6113 }
6114
6115 kfree(ctx->user_bufs);
6116 ctx->user_bufs = NULL;
6117 ctx->nr_user_bufs = 0;
6118 return 0;
6119}
6120
6121static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
6122 void __user *arg, unsigned index)
6123{
6124 struct iovec __user *src;
6125
6126#ifdef CONFIG_COMPAT
6127 if (ctx->compat) {
6128 struct compat_iovec __user *ciovs;
6129 struct compat_iovec ciov;
6130
6131 ciovs = (struct compat_iovec __user *) arg;
6132 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
6133 return -EFAULT;
6134
Jens Axboed55e5f52019-12-11 16:12:15 -07006135 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
Jens Axboeedafcce2019-01-09 09:16:05 -07006136 dst->iov_len = ciov.iov_len;
6137 return 0;
6138 }
6139#endif
6140 src = (struct iovec __user *) arg;
6141 if (copy_from_user(dst, &src[index], sizeof(*dst)))
6142 return -EFAULT;
6143 return 0;
6144}
6145
6146static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg,
6147 unsigned nr_args)
6148{
6149 struct vm_area_struct **vmas = NULL;
6150 struct page **pages = NULL;
6151 int i, j, got_pages = 0;
6152 int ret = -EINVAL;
6153
6154 if (ctx->user_bufs)
6155 return -EBUSY;
6156 if (!nr_args || nr_args > UIO_MAXIOV)
6157 return -EINVAL;
6158
6159 ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf),
6160 GFP_KERNEL);
6161 if (!ctx->user_bufs)
6162 return -ENOMEM;
6163
6164 for (i = 0; i < nr_args; i++) {
6165 struct io_mapped_ubuf *imu = &ctx->user_bufs[i];
6166 unsigned long off, start, end, ubuf;
6167 int pret, nr_pages;
6168 struct iovec iov;
6169 size_t size;
6170
6171 ret = io_copy_iov(ctx, &iov, arg, i);
6172 if (ret)
Pavel Begunkova2786822019-05-26 12:35:47 +03006173 goto err;
Jens Axboeedafcce2019-01-09 09:16:05 -07006174
6175 /*
6176 * Don't impose further limits on the size and buffer
6177 * constraints here, we'll -EINVAL later when IO is
6178 * submitted if they are wrong.
6179 */
6180 ret = -EFAULT;
6181 if (!iov.iov_base || !iov.iov_len)
6182 goto err;
6183
6184 /* arbitrary limit, but we need something */
6185 if (iov.iov_len > SZ_1G)
6186 goto err;
6187
6188 ubuf = (unsigned long) iov.iov_base;
6189 end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT;
6190 start = ubuf >> PAGE_SHIFT;
6191 nr_pages = end - start;
6192
6193 if (ctx->account_mem) {
6194 ret = io_account_mem(ctx->user, nr_pages);
6195 if (ret)
6196 goto err;
6197 }
6198
6199 ret = 0;
6200 if (!pages || nr_pages > got_pages) {
6201 kfree(vmas);
6202 kfree(pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006203 pages = kvmalloc_array(nr_pages, sizeof(struct page *),
Jens Axboeedafcce2019-01-09 09:16:05 -07006204 GFP_KERNEL);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006205 vmas = kvmalloc_array(nr_pages,
Jens Axboeedafcce2019-01-09 09:16:05 -07006206 sizeof(struct vm_area_struct *),
6207 GFP_KERNEL);
6208 if (!pages || !vmas) {
6209 ret = -ENOMEM;
6210 if (ctx->account_mem)
6211 io_unaccount_mem(ctx->user, nr_pages);
6212 goto err;
6213 }
6214 got_pages = nr_pages;
6215 }
6216
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006217 imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec),
Jens Axboeedafcce2019-01-09 09:16:05 -07006218 GFP_KERNEL);
6219 ret = -ENOMEM;
6220 if (!imu->bvec) {
6221 if (ctx->account_mem)
6222 io_unaccount_mem(ctx->user, nr_pages);
6223 goto err;
6224 }
6225
6226 ret = 0;
6227 down_read(&current->mm->mmap_sem);
John Hubbard2113b052020-01-30 22:13:13 -08006228 pret = pin_user_pages(ubuf, nr_pages,
Ira Weiny932f4a62019-05-13 17:17:03 -07006229 FOLL_WRITE | FOLL_LONGTERM,
6230 pages, vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07006231 if (pret == nr_pages) {
6232 /* don't support file backed memory */
6233 for (j = 0; j < nr_pages; j++) {
6234 struct vm_area_struct *vma = vmas[j];
6235
6236 if (vma->vm_file &&
6237 !is_file_hugepages(vma->vm_file)) {
6238 ret = -EOPNOTSUPP;
6239 break;
6240 }
6241 }
6242 } else {
6243 ret = pret < 0 ? pret : -EFAULT;
6244 }
6245 up_read(&current->mm->mmap_sem);
6246 if (ret) {
6247 /*
6248 * if we did partial map, or found file backed vmas,
6249 * release any pages we did get
6250 */
John Hubbard27c4d3a2019-08-04 19:32:06 -07006251 if (pret > 0)
John Hubbardf1f6a7d2020-01-30 22:13:35 -08006252 unpin_user_pages(pages, pret);
Jens Axboeedafcce2019-01-09 09:16:05 -07006253 if (ctx->account_mem)
6254 io_unaccount_mem(ctx->user, nr_pages);
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006255 kvfree(imu->bvec);
Jens Axboeedafcce2019-01-09 09:16:05 -07006256 goto err;
6257 }
6258
6259 off = ubuf & ~PAGE_MASK;
6260 size = iov.iov_len;
6261 for (j = 0; j < nr_pages; j++) {
6262 size_t vec_len;
6263
6264 vec_len = min_t(size_t, size, PAGE_SIZE - off);
6265 imu->bvec[j].bv_page = pages[j];
6266 imu->bvec[j].bv_len = vec_len;
6267 imu->bvec[j].bv_offset = off;
6268 off = 0;
6269 size -= vec_len;
6270 }
6271 /* store original address for later verification */
6272 imu->ubuf = ubuf;
6273 imu->len = iov.iov_len;
6274 imu->nr_bvecs = nr_pages;
6275
6276 ctx->nr_user_bufs++;
6277 }
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006278 kvfree(pages);
6279 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07006280 return 0;
6281err:
Mark Rutlandd4ef6472019-05-01 16:59:16 +01006282 kvfree(pages);
6283 kvfree(vmas);
Jens Axboeedafcce2019-01-09 09:16:05 -07006284 io_sqe_buffer_unregister(ctx);
6285 return ret;
6286}
6287
Jens Axboe9b402842019-04-11 11:45:41 -06006288static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg)
6289{
6290 __s32 __user *fds = arg;
6291 int fd;
6292
6293 if (ctx->cq_ev_fd)
6294 return -EBUSY;
6295
6296 if (copy_from_user(&fd, fds, sizeof(*fds)))
6297 return -EFAULT;
6298
6299 ctx->cq_ev_fd = eventfd_ctx_fdget(fd);
6300 if (IS_ERR(ctx->cq_ev_fd)) {
6301 int ret = PTR_ERR(ctx->cq_ev_fd);
6302 ctx->cq_ev_fd = NULL;
6303 return ret;
6304 }
6305
6306 return 0;
6307}
6308
6309static int io_eventfd_unregister(struct io_ring_ctx *ctx)
6310{
6311 if (ctx->cq_ev_fd) {
6312 eventfd_ctx_put(ctx->cq_ev_fd);
6313 ctx->cq_ev_fd = NULL;
6314 return 0;
6315 }
6316
6317 return -ENXIO;
6318}
6319
Jens Axboe2b188cc2019-01-07 10:46:33 -07006320static void io_ring_ctx_free(struct io_ring_ctx *ctx)
6321{
Jens Axboe6b063142019-01-10 22:13:58 -07006322 io_finish_async(ctx);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006323 if (ctx->sqo_mm)
6324 mmdrop(ctx->sqo_mm);
Jens Axboedef596e2019-01-09 08:59:42 -07006325
6326 io_iopoll_reap_events(ctx);
Jens Axboeedafcce2019-01-09 09:16:05 -07006327 io_sqe_buffer_unregister(ctx);
Jens Axboe6b063142019-01-10 22:13:58 -07006328 io_sqe_files_unregister(ctx);
Jens Axboe9b402842019-04-11 11:45:41 -06006329 io_eventfd_unregister(ctx);
Jens Axboe41726c92020-02-23 13:11:42 -07006330 idr_destroy(&ctx->personality_idr);
Jens Axboedef596e2019-01-09 08:59:42 -07006331
Jens Axboe2b188cc2019-01-07 10:46:33 -07006332#if defined(CONFIG_UNIX)
Eric Biggers355e8d22019-06-12 14:58:43 -07006333 if (ctx->ring_sock) {
6334 ctx->ring_sock->file = NULL; /* so that iput() is called */
Jens Axboe2b188cc2019-01-07 10:46:33 -07006335 sock_release(ctx->ring_sock);
Eric Biggers355e8d22019-06-12 14:58:43 -07006336 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006337#endif
6338
Hristo Venev75b28af2019-08-26 17:23:46 +00006339 io_mem_free(ctx->rings);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006340 io_mem_free(ctx->sq_sqes);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006341
6342 percpu_ref_exit(&ctx->refs);
6343 if (ctx->account_mem)
6344 io_unaccount_mem(ctx->user,
6345 ring_pages(ctx->sq_entries, ctx->cq_entries));
6346 free_uid(ctx->user);
Jens Axboe181e4482019-11-25 08:52:30 -07006347 put_cred(ctx->creds);
Jens Axboe206aefd2019-11-07 18:27:42 -07006348 kfree(ctx->completions);
Jens Axboe78076bb2019-12-04 19:56:40 -07006349 kfree(ctx->cancel_hash);
Jens Axboe0ddf92e2019-11-08 08:52:53 -07006350 kmem_cache_free(req_cachep, ctx->fallback_req);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006351 kfree(ctx);
6352}
6353
6354static __poll_t io_uring_poll(struct file *file, poll_table *wait)
6355{
6356 struct io_ring_ctx *ctx = file->private_data;
6357 __poll_t mask = 0;
6358
6359 poll_wait(file, &ctx->cq_wait, wait);
Stefan Bühler4f7067c2019-04-24 23:54:17 +02006360 /*
6361 * synchronizes with barrier from wq_has_sleeper call in
6362 * io_commit_cqring
6363 */
Jens Axboe2b188cc2019-01-07 10:46:33 -07006364 smp_rmb();
Hristo Venev75b28af2019-08-26 17:23:46 +00006365 if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head !=
6366 ctx->rings->sq_ring_entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006367 mask |= EPOLLOUT | EPOLLWRNORM;
Stefano Garzarella63e5d812020-02-07 13:18:28 +01006368 if (io_cqring_events(ctx, false))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006369 mask |= EPOLLIN | EPOLLRDNORM;
6370
6371 return mask;
6372}
6373
6374static int io_uring_fasync(int fd, struct file *file, int on)
6375{
6376 struct io_ring_ctx *ctx = file->private_data;
6377
6378 return fasync_helper(fd, file, on, &ctx->cq_fasync);
6379}
6380
Jens Axboe071698e2020-01-28 10:04:42 -07006381static int io_remove_personalities(int id, void *p, void *data)
6382{
6383 struct io_ring_ctx *ctx = data;
6384 const struct cred *cred;
6385
6386 cred = idr_remove(&ctx->personality_idr, id);
6387 if (cred)
6388 put_cred(cred);
6389 return 0;
6390}
6391
Jens Axboe2b188cc2019-01-07 10:46:33 -07006392static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
6393{
6394 mutex_lock(&ctx->uring_lock);
6395 percpu_ref_kill(&ctx->refs);
6396 mutex_unlock(&ctx->uring_lock);
6397
Jens Axboedf069d82020-02-04 16:48:34 -07006398 /*
6399 * Wait for sq thread to idle, if we have one. It won't spin on new
6400 * work after we've killed the ctx ref above. This is important to do
6401 * before we cancel existing commands, as the thread could otherwise
6402 * be queueing new work post that. If that's work we need to cancel,
6403 * it could cause shutdown to hang.
6404 */
6405 while (ctx->sqo_thread && !wq_has_sleeper(&ctx->sqo_wait))
6406 cpu_relax();
6407
Jens Axboe5262f562019-09-17 12:26:57 -06006408 io_kill_timeouts(ctx);
Jens Axboe221c5eb2019-01-17 09:41:58 -07006409 io_poll_remove_all(ctx);
Jens Axboe561fb042019-10-24 07:25:42 -06006410
6411 if (ctx->io_wq)
6412 io_wq_cancel_all(ctx->io_wq);
6413
Jens Axboedef596e2019-01-09 08:59:42 -07006414 io_iopoll_reap_events(ctx);
Jens Axboe15dff282019-11-13 09:09:23 -07006415 /* if we failed setting up the ctx, we might not have any rings */
6416 if (ctx->rings)
6417 io_cqring_overflow_flush(ctx, true);
Jens Axboe071698e2020-01-28 10:04:42 -07006418 idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
Jens Axboe206aefd2019-11-07 18:27:42 -07006419 wait_for_completion(&ctx->completions[0]);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006420 io_ring_ctx_free(ctx);
6421}
6422
6423static int io_uring_release(struct inode *inode, struct file *file)
6424{
6425 struct io_ring_ctx *ctx = file->private_data;
6426
6427 file->private_data = NULL;
6428 io_ring_ctx_wait_and_kill(ctx);
6429 return 0;
6430}
6431
Jens Axboefcb323c2019-10-24 12:39:47 -06006432static void io_uring_cancel_files(struct io_ring_ctx *ctx,
6433 struct files_struct *files)
6434{
6435 struct io_kiocb *req;
6436 DEFINE_WAIT(wait);
6437
6438 while (!list_empty_careful(&ctx->inflight_list)) {
Jens Axboe768134d2019-11-10 20:30:53 -07006439 struct io_kiocb *cancel_req = NULL;
Jens Axboefcb323c2019-10-24 12:39:47 -06006440
6441 spin_lock_irq(&ctx->inflight_lock);
6442 list_for_each_entry(req, &ctx->inflight_list, inflight_entry) {
Jens Axboe768134d2019-11-10 20:30:53 -07006443 if (req->work.files != files)
6444 continue;
6445 /* req is being completed, ignore */
6446 if (!refcount_inc_not_zero(&req->refs))
6447 continue;
6448 cancel_req = req;
6449 break;
Jens Axboefcb323c2019-10-24 12:39:47 -06006450 }
Jens Axboe768134d2019-11-10 20:30:53 -07006451 if (cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06006452 prepare_to_wait(&ctx->inflight_wait, &wait,
Jens Axboe768134d2019-11-10 20:30:53 -07006453 TASK_UNINTERRUPTIBLE);
Jens Axboefcb323c2019-10-24 12:39:47 -06006454 spin_unlock_irq(&ctx->inflight_lock);
6455
Jens Axboe768134d2019-11-10 20:30:53 -07006456 /* We need to keep going until we don't find a matching req */
6457 if (!cancel_req)
Jens Axboefcb323c2019-10-24 12:39:47 -06006458 break;
Bob Liu2f6d9b92019-11-13 18:06:24 +08006459
Jens Axboe2ca10252020-02-13 17:17:35 -07006460 if (cancel_req->flags & REQ_F_OVERFLOW) {
6461 spin_lock_irq(&ctx->completion_lock);
6462 list_del(&cancel_req->list);
6463 cancel_req->flags &= ~REQ_F_OVERFLOW;
6464 if (list_empty(&ctx->cq_overflow_list)) {
6465 clear_bit(0, &ctx->sq_check_overflow);
6466 clear_bit(0, &ctx->cq_check_overflow);
6467 }
6468 spin_unlock_irq(&ctx->completion_lock);
6469
6470 WRITE_ONCE(ctx->rings->cq_overflow,
6471 atomic_inc_return(&ctx->cached_cq_overflow));
6472
6473 /*
6474 * Put inflight ref and overflow ref. If that's
6475 * all we had, then we're done with this request.
6476 */
6477 if (refcount_sub_and_test(2, &cancel_req->refs)) {
6478 io_put_req(cancel_req);
6479 continue;
6480 }
6481 }
6482
Bob Liu2f6d9b92019-11-13 18:06:24 +08006483 io_wq_cancel_work(ctx->io_wq, &cancel_req->work);
6484 io_put_req(cancel_req);
Jens Axboefcb323c2019-10-24 12:39:47 -06006485 schedule();
6486 }
Jens Axboe768134d2019-11-10 20:30:53 -07006487 finish_wait(&ctx->inflight_wait, &wait);
Jens Axboefcb323c2019-10-24 12:39:47 -06006488}
6489
6490static int io_uring_flush(struct file *file, void *data)
6491{
6492 struct io_ring_ctx *ctx = file->private_data;
6493
6494 io_uring_cancel_files(ctx, data);
Jens Axboe6ab23142020-02-08 20:23:59 -07006495
6496 /*
6497 * If the task is going away, cancel work it may have pending
6498 */
6499 if (fatal_signal_pending(current) || (current->flags & PF_EXITING))
6500 io_wq_cancel_pid(ctx->io_wq, task_pid_vnr(current));
6501
Jens Axboefcb323c2019-10-24 12:39:47 -06006502 return 0;
6503}
6504
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006505static void *io_uring_validate_mmap_request(struct file *file,
6506 loff_t pgoff, size_t sz)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006507{
Jens Axboe2b188cc2019-01-07 10:46:33 -07006508 struct io_ring_ctx *ctx = file->private_data;
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006509 loff_t offset = pgoff << PAGE_SHIFT;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006510 struct page *page;
6511 void *ptr;
6512
6513 switch (offset) {
6514 case IORING_OFF_SQ_RING:
Hristo Venev75b28af2019-08-26 17:23:46 +00006515 case IORING_OFF_CQ_RING:
6516 ptr = ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006517 break;
6518 case IORING_OFF_SQES:
6519 ptr = ctx->sq_sqes;
6520 break;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006521 default:
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006522 return ERR_PTR(-EINVAL);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006523 }
6524
6525 page = virt_to_head_page(ptr);
Matthew Wilcox (Oracle)a50b8542019-09-23 15:34:25 -07006526 if (sz > page_size(page))
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006527 return ERR_PTR(-EINVAL);
6528
6529 return ptr;
6530}
6531
6532#ifdef CONFIG_MMU
6533
6534static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
6535{
6536 size_t sz = vma->vm_end - vma->vm_start;
6537 unsigned long pfn;
6538 void *ptr;
6539
6540 ptr = io_uring_validate_mmap_request(file, vma->vm_pgoff, sz);
6541 if (IS_ERR(ptr))
6542 return PTR_ERR(ptr);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006543
6544 pfn = virt_to_phys(ptr) >> PAGE_SHIFT;
6545 return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot);
6546}
6547
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006548#else /* !CONFIG_MMU */
6549
6550static int io_uring_mmap(struct file *file, struct vm_area_struct *vma)
6551{
6552 return vma->vm_flags & (VM_SHARED | VM_MAYSHARE) ? 0 : -EINVAL;
6553}
6554
6555static unsigned int io_uring_nommu_mmap_capabilities(struct file *file)
6556{
6557 return NOMMU_MAP_DIRECT | NOMMU_MAP_READ | NOMMU_MAP_WRITE;
6558}
6559
6560static unsigned long io_uring_nommu_get_unmapped_area(struct file *file,
6561 unsigned long addr, unsigned long len,
6562 unsigned long pgoff, unsigned long flags)
6563{
6564 void *ptr;
6565
6566 ptr = io_uring_validate_mmap_request(file, pgoff, len);
6567 if (IS_ERR(ptr))
6568 return PTR_ERR(ptr);
6569
6570 return (unsigned long) ptr;
6571}
6572
6573#endif /* !CONFIG_MMU */
6574
Jens Axboe2b188cc2019-01-07 10:46:33 -07006575SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
6576 u32, min_complete, u32, flags, const sigset_t __user *, sig,
6577 size_t, sigsz)
6578{
6579 struct io_ring_ctx *ctx;
6580 long ret = -EBADF;
6581 int submitted = 0;
6582 struct fd f;
6583
Jens Axboe6c271ce2019-01-10 11:22:30 -07006584 if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006585 return -EINVAL;
6586
6587 f = fdget(fd);
6588 if (!f.file)
6589 return -EBADF;
6590
6591 ret = -EOPNOTSUPP;
6592 if (f.file->f_op != &io_uring_fops)
6593 goto out_fput;
6594
6595 ret = -ENXIO;
6596 ctx = f.file->private_data;
6597 if (!percpu_ref_tryget(&ctx->refs))
6598 goto out_fput;
6599
Jens Axboe6c271ce2019-01-10 11:22:30 -07006600 /*
6601 * For SQ polling, the thread will do all submissions and completions.
6602 * Just return the requested submit count, and wake the thread if
6603 * we were asked to.
6604 */
Jens Axboeb2a9ead2019-09-12 14:19:16 -06006605 ret = 0;
Jens Axboe6c271ce2019-01-10 11:22:30 -07006606 if (ctx->flags & IORING_SETUP_SQPOLL) {
Jens Axboec1edbf52019-11-10 16:56:04 -07006607 if (!list_empty_careful(&ctx->cq_overflow_list))
6608 io_cqring_overflow_flush(ctx, false);
Jens Axboe6c271ce2019-01-10 11:22:30 -07006609 if (flags & IORING_ENTER_SQ_WAKEUP)
6610 wake_up(&ctx->sqo_wait);
6611 submitted = to_submit;
Jens Axboeb2a9ead2019-09-12 14:19:16 -06006612 } else if (to_submit) {
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006613 struct mm_struct *cur_mm;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006614
6615 mutex_lock(&ctx->uring_lock);
Pavel Begunkovae9428c2019-11-06 00:22:14 +03006616 /* already have mm, so io_submit_sqes() won't try to grab it */
6617 cur_mm = ctx->sqo_mm;
6618 submitted = io_submit_sqes(ctx, to_submit, f.file, fd,
6619 &cur_mm, false);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006620 mutex_unlock(&ctx->uring_lock);
Pavel Begunkov7c504e652019-12-18 19:53:45 +03006621
6622 if (submitted != to_submit)
6623 goto out;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006624 }
6625 if (flags & IORING_ENTER_GETEVENTS) {
Jens Axboedef596e2019-01-09 08:59:42 -07006626 unsigned nr_events = 0;
6627
Jens Axboe2b188cc2019-01-07 10:46:33 -07006628 min_complete = min(min_complete, ctx->cq_entries);
6629
Jens Axboedef596e2019-01-09 08:59:42 -07006630 if (ctx->flags & IORING_SETUP_IOPOLL) {
Jens Axboedef596e2019-01-09 08:59:42 -07006631 ret = io_iopoll_check(ctx, &nr_events, min_complete);
Jens Axboedef596e2019-01-09 08:59:42 -07006632 } else {
6633 ret = io_cqring_wait(ctx, min_complete, sig, sigsz);
6634 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006635 }
6636
Pavel Begunkov7c504e652019-12-18 19:53:45 +03006637out:
Pavel Begunkov6805b322019-10-08 02:18:42 +03006638 percpu_ref_put(&ctx->refs);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006639out_fput:
6640 fdput(f);
6641 return submitted ? submitted : ret;
6642}
6643
Jens Axboe87ce9552020-01-30 08:25:34 -07006644static int io_uring_show_cred(int id, void *p, void *data)
6645{
6646 const struct cred *cred = p;
6647 struct seq_file *m = data;
6648 struct user_namespace *uns = seq_user_ns(m);
6649 struct group_info *gi;
6650 kernel_cap_t cap;
6651 unsigned __capi;
6652 int g;
6653
6654 seq_printf(m, "%5d\n", id);
6655 seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
6656 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
6657 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
6658 seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
6659 seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
6660 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
6661 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
6662 seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
6663 seq_puts(m, "\n\tGroups:\t");
6664 gi = cred->group_info;
6665 for (g = 0; g < gi->ngroups; g++) {
6666 seq_put_decimal_ull(m, g ? " " : "",
6667 from_kgid_munged(uns, gi->gid[g]));
6668 }
6669 seq_puts(m, "\n\tCapEff:\t");
6670 cap = cred->cap_effective;
6671 CAP_FOR_EACH_U32(__capi)
6672 seq_put_hex_ll(m, NULL, cap.cap[CAP_LAST_U32 - __capi], 8);
6673 seq_putc(m, '\n');
6674 return 0;
6675}
6676
6677static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
6678{
6679 int i;
6680
6681 mutex_lock(&ctx->uring_lock);
6682 seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
6683 for (i = 0; i < ctx->nr_user_files; i++) {
6684 struct fixed_file_table *table;
6685 struct file *f;
6686
6687 table = &ctx->file_data->table[i >> IORING_FILE_TABLE_SHIFT];
6688 f = table->files[i & IORING_FILE_TABLE_MASK];
6689 if (f)
6690 seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
6691 else
6692 seq_printf(m, "%5u: <none>\n", i);
6693 }
6694 seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
6695 for (i = 0; i < ctx->nr_user_bufs; i++) {
6696 struct io_mapped_ubuf *buf = &ctx->user_bufs[i];
6697
6698 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf,
6699 (unsigned int) buf->len);
6700 }
6701 if (!idr_is_empty(&ctx->personality_idr)) {
6702 seq_printf(m, "Personalities:\n");
6703 idr_for_each(&ctx->personality_idr, io_uring_show_cred, m);
6704 }
6705 mutex_unlock(&ctx->uring_lock);
6706}
6707
6708static void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
6709{
6710 struct io_ring_ctx *ctx = f->private_data;
6711
6712 if (percpu_ref_tryget(&ctx->refs)) {
6713 __io_uring_show_fdinfo(ctx, m);
6714 percpu_ref_put(&ctx->refs);
6715 }
6716}
6717
Jens Axboe2b188cc2019-01-07 10:46:33 -07006718static const struct file_operations io_uring_fops = {
6719 .release = io_uring_release,
Jens Axboefcb323c2019-10-24 12:39:47 -06006720 .flush = io_uring_flush,
Jens Axboe2b188cc2019-01-07 10:46:33 -07006721 .mmap = io_uring_mmap,
Roman Penyaev6c5c2402019-11-28 12:53:22 +01006722#ifndef CONFIG_MMU
6723 .get_unmapped_area = io_uring_nommu_get_unmapped_area,
6724 .mmap_capabilities = io_uring_nommu_mmap_capabilities,
6725#endif
Jens Axboe2b188cc2019-01-07 10:46:33 -07006726 .poll = io_uring_poll,
6727 .fasync = io_uring_fasync,
Jens Axboe87ce9552020-01-30 08:25:34 -07006728 .show_fdinfo = io_uring_show_fdinfo,
Jens Axboe2b188cc2019-01-07 10:46:33 -07006729};
6730
6731static int io_allocate_scq_urings(struct io_ring_ctx *ctx,
6732 struct io_uring_params *p)
6733{
Hristo Venev75b28af2019-08-26 17:23:46 +00006734 struct io_rings *rings;
6735 size_t size, sq_array_offset;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006736
Hristo Venev75b28af2019-08-26 17:23:46 +00006737 size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset);
6738 if (size == SIZE_MAX)
6739 return -EOVERFLOW;
6740
6741 rings = io_mem_alloc(size);
6742 if (!rings)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006743 return -ENOMEM;
6744
Hristo Venev75b28af2019-08-26 17:23:46 +00006745 ctx->rings = rings;
6746 ctx->sq_array = (u32 *)((char *)rings + sq_array_offset);
6747 rings->sq_ring_mask = p->sq_entries - 1;
6748 rings->cq_ring_mask = p->cq_entries - 1;
6749 rings->sq_ring_entries = p->sq_entries;
6750 rings->cq_ring_entries = p->cq_entries;
6751 ctx->sq_mask = rings->sq_ring_mask;
6752 ctx->cq_mask = rings->cq_ring_mask;
6753 ctx->sq_entries = rings->sq_ring_entries;
6754 ctx->cq_entries = rings->cq_ring_entries;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006755
6756 size = array_size(sizeof(struct io_uring_sqe), p->sq_entries);
Jens Axboeeb065d32019-11-20 09:26:29 -07006757 if (size == SIZE_MAX) {
6758 io_mem_free(ctx->rings);
6759 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006760 return -EOVERFLOW;
Jens Axboeeb065d32019-11-20 09:26:29 -07006761 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006762
6763 ctx->sq_sqes = io_mem_alloc(size);
Jens Axboeeb065d32019-11-20 09:26:29 -07006764 if (!ctx->sq_sqes) {
6765 io_mem_free(ctx->rings);
6766 ctx->rings = NULL;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006767 return -ENOMEM;
Jens Axboeeb065d32019-11-20 09:26:29 -07006768 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006769
Jens Axboe2b188cc2019-01-07 10:46:33 -07006770 return 0;
6771}
6772
6773/*
6774 * Allocate an anonymous fd, this is what constitutes the application
6775 * visible backing of an io_uring instance. The application mmaps this
6776 * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled,
6777 * we have to tie this fd to a socket for file garbage collection purposes.
6778 */
6779static int io_uring_get_fd(struct io_ring_ctx *ctx)
6780{
6781 struct file *file;
6782 int ret;
6783
6784#if defined(CONFIG_UNIX)
6785 ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP,
6786 &ctx->ring_sock);
6787 if (ret)
6788 return ret;
6789#endif
6790
6791 ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
6792 if (ret < 0)
6793 goto err;
6794
6795 file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx,
6796 O_RDWR | O_CLOEXEC);
6797 if (IS_ERR(file)) {
6798 put_unused_fd(ret);
6799 ret = PTR_ERR(file);
6800 goto err;
6801 }
6802
6803#if defined(CONFIG_UNIX)
6804 ctx->ring_sock->file = file;
6805#endif
6806 fd_install(ret, file);
6807 return ret;
6808err:
6809#if defined(CONFIG_UNIX)
6810 sock_release(ctx->ring_sock);
6811 ctx->ring_sock = NULL;
6812#endif
6813 return ret;
6814}
6815
6816static int io_uring_create(unsigned entries, struct io_uring_params *p)
6817{
6818 struct user_struct *user = NULL;
6819 struct io_ring_ctx *ctx;
6820 bool account_mem;
6821 int ret;
6822
Jens Axboe8110c1a2019-12-28 15:39:54 -07006823 if (!entries)
Jens Axboe2b188cc2019-01-07 10:46:33 -07006824 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07006825 if (entries > IORING_MAX_ENTRIES) {
6826 if (!(p->flags & IORING_SETUP_CLAMP))
6827 return -EINVAL;
6828 entries = IORING_MAX_ENTRIES;
6829 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006830
6831 /*
6832 * Use twice as many entries for the CQ ring. It's possible for the
6833 * application to drive a higher depth than the size of the SQ ring,
6834 * since the sqes are only used at submission time. This allows for
Jens Axboe33a107f2019-10-04 12:10:03 -06006835 * some flexibility in overcommitting a bit. If the application has
6836 * set IORING_SETUP_CQSIZE, it will have passed in the desired number
6837 * of CQ ring entries manually.
Jens Axboe2b188cc2019-01-07 10:46:33 -07006838 */
6839 p->sq_entries = roundup_pow_of_two(entries);
Jens Axboe33a107f2019-10-04 12:10:03 -06006840 if (p->flags & IORING_SETUP_CQSIZE) {
6841 /*
6842 * If IORING_SETUP_CQSIZE is set, we do the same roundup
6843 * to a power-of-two, if it isn't already. We do NOT impose
6844 * any cq vs sq ring sizing.
6845 */
Jens Axboe8110c1a2019-12-28 15:39:54 -07006846 if (p->cq_entries < p->sq_entries)
Jens Axboe33a107f2019-10-04 12:10:03 -06006847 return -EINVAL;
Jens Axboe8110c1a2019-12-28 15:39:54 -07006848 if (p->cq_entries > IORING_MAX_CQ_ENTRIES) {
6849 if (!(p->flags & IORING_SETUP_CLAMP))
6850 return -EINVAL;
6851 p->cq_entries = IORING_MAX_CQ_ENTRIES;
6852 }
Jens Axboe33a107f2019-10-04 12:10:03 -06006853 p->cq_entries = roundup_pow_of_two(p->cq_entries);
6854 } else {
6855 p->cq_entries = 2 * p->sq_entries;
6856 }
Jens Axboe2b188cc2019-01-07 10:46:33 -07006857
6858 user = get_uid(current_user());
6859 account_mem = !capable(CAP_IPC_LOCK);
6860
6861 if (account_mem) {
6862 ret = io_account_mem(user,
6863 ring_pages(p->sq_entries, p->cq_entries));
6864 if (ret) {
6865 free_uid(user);
6866 return ret;
6867 }
6868 }
6869
6870 ctx = io_ring_ctx_alloc(p);
6871 if (!ctx) {
6872 if (account_mem)
6873 io_unaccount_mem(user, ring_pages(p->sq_entries,
6874 p->cq_entries));
6875 free_uid(user);
6876 return -ENOMEM;
6877 }
6878 ctx->compat = in_compat_syscall();
6879 ctx->account_mem = account_mem;
6880 ctx->user = user;
Jens Axboe0b8c0ec2019-12-02 08:50:00 -07006881 ctx->creds = get_current_cred();
Jens Axboe2b188cc2019-01-07 10:46:33 -07006882
6883 ret = io_allocate_scq_urings(ctx, p);
6884 if (ret)
6885 goto err;
6886
Jens Axboe6c271ce2019-01-10 11:22:30 -07006887 ret = io_sq_offload_start(ctx, p);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006888 if (ret)
6889 goto err;
6890
Jens Axboe2b188cc2019-01-07 10:46:33 -07006891 memset(&p->sq_off, 0, sizeof(p->sq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00006892 p->sq_off.head = offsetof(struct io_rings, sq.head);
6893 p->sq_off.tail = offsetof(struct io_rings, sq.tail);
6894 p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask);
6895 p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries);
6896 p->sq_off.flags = offsetof(struct io_rings, sq_flags);
6897 p->sq_off.dropped = offsetof(struct io_rings, sq_dropped);
6898 p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings;
Jens Axboe2b188cc2019-01-07 10:46:33 -07006899
6900 memset(&p->cq_off, 0, sizeof(p->cq_off));
Hristo Venev75b28af2019-08-26 17:23:46 +00006901 p->cq_off.head = offsetof(struct io_rings, cq.head);
6902 p->cq_off.tail = offsetof(struct io_rings, cq.tail);
6903 p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask);
6904 p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries);
6905 p->cq_off.overflow = offsetof(struct io_rings, cq_overflow);
6906 p->cq_off.cqes = offsetof(struct io_rings, cqes);
Jens Axboeac90f242019-09-06 10:26:21 -06006907
Jens Axboe044c1ab2019-10-28 09:15:33 -06006908 /*
6909 * Install ring fd as the very last thing, so we don't risk someone
6910 * having closed it before we finish setup
6911 */
6912 ret = io_uring_get_fd(ctx);
6913 if (ret < 0)
6914 goto err;
6915
Jens Axboeda8c9692019-12-02 18:51:26 -07006916 p->features = IORING_FEAT_SINGLE_MMAP | IORING_FEAT_NODROP |
Jens Axboecccf0ee2020-01-27 16:34:48 -07006917 IORING_FEAT_SUBMIT_STABLE | IORING_FEAT_RW_CUR_POS |
6918 IORING_FEAT_CUR_PERSONALITY;
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02006919 trace_io_uring_create(ret, ctx, p->sq_entries, p->cq_entries, p->flags);
Jens Axboe2b188cc2019-01-07 10:46:33 -07006920 return ret;
6921err:
6922 io_ring_ctx_wait_and_kill(ctx);
6923 return ret;
6924}
6925
6926/*
6927 * Sets up an aio uring context, and returns the fd. Applications asks for a
6928 * ring size, we return the actual sq/cq ring sizes (among other things) in the
6929 * params structure passed in.
6930 */
6931static long io_uring_setup(u32 entries, struct io_uring_params __user *params)
6932{
6933 struct io_uring_params p;
6934 long ret;
6935 int i;
6936
6937 if (copy_from_user(&p, params, sizeof(p)))
6938 return -EFAULT;
6939 for (i = 0; i < ARRAY_SIZE(p.resv); i++) {
6940 if (p.resv[i])
6941 return -EINVAL;
6942 }
6943
Jens Axboe6c271ce2019-01-10 11:22:30 -07006944 if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL |
Jens Axboe8110c1a2019-12-28 15:39:54 -07006945 IORING_SETUP_SQ_AFF | IORING_SETUP_CQSIZE |
Pavel Begunkov24369c22020-01-28 03:15:48 +03006946 IORING_SETUP_CLAMP | IORING_SETUP_ATTACH_WQ))
Jens Axboe2b188cc2019-01-07 10:46:33 -07006947 return -EINVAL;
6948
6949 ret = io_uring_create(entries, &p);
6950 if (ret < 0)
6951 return ret;
6952
6953 if (copy_to_user(params, &p, sizeof(p)))
6954 return -EFAULT;
6955
6956 return ret;
6957}
6958
6959SYSCALL_DEFINE2(io_uring_setup, u32, entries,
6960 struct io_uring_params __user *, params)
6961{
6962 return io_uring_setup(entries, params);
6963}
6964
Jens Axboe66f4af92020-01-16 15:36:52 -07006965static int io_probe(struct io_ring_ctx *ctx, void __user *arg, unsigned nr_args)
6966{
6967 struct io_uring_probe *p;
6968 size_t size;
6969 int i, ret;
6970
6971 size = struct_size(p, ops, nr_args);
6972 if (size == SIZE_MAX)
6973 return -EOVERFLOW;
6974 p = kzalloc(size, GFP_KERNEL);
6975 if (!p)
6976 return -ENOMEM;
6977
6978 ret = -EFAULT;
6979 if (copy_from_user(p, arg, size))
6980 goto out;
6981 ret = -EINVAL;
6982 if (memchr_inv(p, 0, size))
6983 goto out;
6984
6985 p->last_op = IORING_OP_LAST - 1;
6986 if (nr_args > IORING_OP_LAST)
6987 nr_args = IORING_OP_LAST;
6988
6989 for (i = 0; i < nr_args; i++) {
6990 p->ops[i].op = i;
6991 if (!io_op_defs[i].not_supported)
6992 p->ops[i].flags = IO_URING_OP_SUPPORTED;
6993 }
6994 p->ops_len = i;
6995
6996 ret = 0;
6997 if (copy_to_user(arg, p, size))
6998 ret = -EFAULT;
6999out:
7000 kfree(p);
7001 return ret;
7002}
7003
Jens Axboe071698e2020-01-28 10:04:42 -07007004static int io_register_personality(struct io_ring_ctx *ctx)
7005{
7006 const struct cred *creds = get_current_cred();
7007 int id;
7008
7009 id = idr_alloc_cyclic(&ctx->personality_idr, (void *) creds, 1,
7010 USHRT_MAX, GFP_KERNEL);
7011 if (id < 0)
7012 put_cred(creds);
7013 return id;
7014}
7015
7016static int io_unregister_personality(struct io_ring_ctx *ctx, unsigned id)
7017{
7018 const struct cred *old_creds;
7019
7020 old_creds = idr_remove(&ctx->personality_idr, id);
7021 if (old_creds) {
7022 put_cred(old_creds);
7023 return 0;
7024 }
7025
7026 return -EINVAL;
7027}
7028
7029static bool io_register_op_must_quiesce(int op)
7030{
7031 switch (op) {
7032 case IORING_UNREGISTER_FILES:
7033 case IORING_REGISTER_FILES_UPDATE:
7034 case IORING_REGISTER_PROBE:
7035 case IORING_REGISTER_PERSONALITY:
7036 case IORING_UNREGISTER_PERSONALITY:
7037 return false;
7038 default:
7039 return true;
7040 }
7041}
7042
Jens Axboeedafcce2019-01-09 09:16:05 -07007043static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
7044 void __user *arg, unsigned nr_args)
Jens Axboeb19062a2019-04-15 10:49:38 -06007045 __releases(ctx->uring_lock)
7046 __acquires(ctx->uring_lock)
Jens Axboeedafcce2019-01-09 09:16:05 -07007047{
7048 int ret;
7049
Jens Axboe35fa71a2019-04-22 10:23:23 -06007050 /*
7051 * We're inside the ring mutex, if the ref is already dying, then
7052 * someone else killed the ctx or is already going through
7053 * io_uring_register().
7054 */
7055 if (percpu_ref_is_dying(&ctx->refs))
7056 return -ENXIO;
7057
Jens Axboe071698e2020-01-28 10:04:42 -07007058 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007059 percpu_ref_kill(&ctx->refs);
Jens Axboeb19062a2019-04-15 10:49:38 -06007060
Jens Axboe05f3fb32019-12-09 11:22:50 -07007061 /*
7062 * Drop uring mutex before waiting for references to exit. If
7063 * another thread is currently inside io_uring_enter() it might
7064 * need to grab the uring_lock to make progress. If we hold it
7065 * here across the drain wait, then we can deadlock. It's safe
7066 * to drop the mutex here, since no new references will come in
7067 * after we've killed the percpu ref.
7068 */
7069 mutex_unlock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07007070 ret = wait_for_completion_interruptible(&ctx->completions[0]);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007071 mutex_lock(&ctx->uring_lock);
Jens Axboec1503682020-01-08 08:26:07 -07007072 if (ret) {
7073 percpu_ref_resurrect(&ctx->refs);
7074 ret = -EINTR;
7075 goto out;
7076 }
Jens Axboe05f3fb32019-12-09 11:22:50 -07007077 }
Jens Axboeedafcce2019-01-09 09:16:05 -07007078
7079 switch (opcode) {
7080 case IORING_REGISTER_BUFFERS:
7081 ret = io_sqe_buffer_register(ctx, arg, nr_args);
7082 break;
7083 case IORING_UNREGISTER_BUFFERS:
7084 ret = -EINVAL;
7085 if (arg || nr_args)
7086 break;
7087 ret = io_sqe_buffer_unregister(ctx);
7088 break;
Jens Axboe6b063142019-01-10 22:13:58 -07007089 case IORING_REGISTER_FILES:
7090 ret = io_sqe_files_register(ctx, arg, nr_args);
7091 break;
7092 case IORING_UNREGISTER_FILES:
7093 ret = -EINVAL;
7094 if (arg || nr_args)
7095 break;
7096 ret = io_sqe_files_unregister(ctx);
7097 break;
Jens Axboec3a31e62019-10-03 13:59:56 -06007098 case IORING_REGISTER_FILES_UPDATE:
7099 ret = io_sqe_files_update(ctx, arg, nr_args);
7100 break;
Jens Axboe9b402842019-04-11 11:45:41 -06007101 case IORING_REGISTER_EVENTFD:
Jens Axboef2842ab2020-01-08 11:04:00 -07007102 case IORING_REGISTER_EVENTFD_ASYNC:
Jens Axboe9b402842019-04-11 11:45:41 -06007103 ret = -EINVAL;
7104 if (nr_args != 1)
7105 break;
7106 ret = io_eventfd_register(ctx, arg);
Jens Axboef2842ab2020-01-08 11:04:00 -07007107 if (ret)
7108 break;
7109 if (opcode == IORING_REGISTER_EVENTFD_ASYNC)
7110 ctx->eventfd_async = 1;
7111 else
7112 ctx->eventfd_async = 0;
Jens Axboe9b402842019-04-11 11:45:41 -06007113 break;
7114 case IORING_UNREGISTER_EVENTFD:
7115 ret = -EINVAL;
7116 if (arg || nr_args)
7117 break;
7118 ret = io_eventfd_unregister(ctx);
7119 break;
Jens Axboe66f4af92020-01-16 15:36:52 -07007120 case IORING_REGISTER_PROBE:
7121 ret = -EINVAL;
7122 if (!arg || nr_args > 256)
7123 break;
7124 ret = io_probe(ctx, arg, nr_args);
7125 break;
Jens Axboe071698e2020-01-28 10:04:42 -07007126 case IORING_REGISTER_PERSONALITY:
7127 ret = -EINVAL;
7128 if (arg || nr_args)
7129 break;
7130 ret = io_register_personality(ctx);
7131 break;
7132 case IORING_UNREGISTER_PERSONALITY:
7133 ret = -EINVAL;
7134 if (arg)
7135 break;
7136 ret = io_unregister_personality(ctx, nr_args);
7137 break;
Jens Axboeedafcce2019-01-09 09:16:05 -07007138 default:
7139 ret = -EINVAL;
7140 break;
7141 }
7142
Jens Axboe071698e2020-01-28 10:04:42 -07007143 if (io_register_op_must_quiesce(opcode)) {
Jens Axboe05f3fb32019-12-09 11:22:50 -07007144 /* bring the ctx back to life */
Jens Axboe05f3fb32019-12-09 11:22:50 -07007145 percpu_ref_reinit(&ctx->refs);
Jens Axboec1503682020-01-08 08:26:07 -07007146out:
7147 reinit_completion(&ctx->completions[0]);
Jens Axboe05f3fb32019-12-09 11:22:50 -07007148 }
Jens Axboeedafcce2019-01-09 09:16:05 -07007149 return ret;
7150}
7151
7152SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode,
7153 void __user *, arg, unsigned int, nr_args)
7154{
7155 struct io_ring_ctx *ctx;
7156 long ret = -EBADF;
7157 struct fd f;
7158
7159 f = fdget(fd);
7160 if (!f.file)
7161 return -EBADF;
7162
7163 ret = -EOPNOTSUPP;
7164 if (f.file->f_op != &io_uring_fops)
7165 goto out_fput;
7166
7167 ctx = f.file->private_data;
7168
7169 mutex_lock(&ctx->uring_lock);
7170 ret = __io_uring_register(ctx, opcode, arg, nr_args);
7171 mutex_unlock(&ctx->uring_lock);
Dmitrii Dolgovc826bd72019-10-15 19:02:01 +02007172 trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_bufs,
7173 ctx->cq_ev_fd != NULL, ret);
Jens Axboeedafcce2019-01-09 09:16:05 -07007174out_fput:
7175 fdput(f);
7176 return ret;
7177}
7178
Jens Axboe2b188cc2019-01-07 10:46:33 -07007179static int __init io_uring_init(void)
7180{
Stefan Metzmacherd7f62e82020-01-29 14:39:41 +01007181#define __BUILD_BUG_VERIFY_ELEMENT(stype, eoffset, etype, ename) do { \
7182 BUILD_BUG_ON(offsetof(stype, ename) != eoffset); \
7183 BUILD_BUG_ON(sizeof(etype) != sizeof_field(stype, ename)); \
7184} while (0)
7185
7186#define BUILD_BUG_SQE_ELEM(eoffset, etype, ename) \
7187 __BUILD_BUG_VERIFY_ELEMENT(struct io_uring_sqe, eoffset, etype, ename)
7188 BUILD_BUG_ON(sizeof(struct io_uring_sqe) != 64);
7189 BUILD_BUG_SQE_ELEM(0, __u8, opcode);
7190 BUILD_BUG_SQE_ELEM(1, __u8, flags);
7191 BUILD_BUG_SQE_ELEM(2, __u16, ioprio);
7192 BUILD_BUG_SQE_ELEM(4, __s32, fd);
7193 BUILD_BUG_SQE_ELEM(8, __u64, off);
7194 BUILD_BUG_SQE_ELEM(8, __u64, addr2);
7195 BUILD_BUG_SQE_ELEM(16, __u64, addr);
7196 BUILD_BUG_SQE_ELEM(24, __u32, len);
7197 BUILD_BUG_SQE_ELEM(28, __kernel_rwf_t, rw_flags);
7198 BUILD_BUG_SQE_ELEM(28, /* compat */ int, rw_flags);
7199 BUILD_BUG_SQE_ELEM(28, /* compat */ __u32, rw_flags);
7200 BUILD_BUG_SQE_ELEM(28, __u32, fsync_flags);
7201 BUILD_BUG_SQE_ELEM(28, __u16, poll_events);
7202 BUILD_BUG_SQE_ELEM(28, __u32, sync_range_flags);
7203 BUILD_BUG_SQE_ELEM(28, __u32, msg_flags);
7204 BUILD_BUG_SQE_ELEM(28, __u32, timeout_flags);
7205 BUILD_BUG_SQE_ELEM(28, __u32, accept_flags);
7206 BUILD_BUG_SQE_ELEM(28, __u32, cancel_flags);
7207 BUILD_BUG_SQE_ELEM(28, __u32, open_flags);
7208 BUILD_BUG_SQE_ELEM(28, __u32, statx_flags);
7209 BUILD_BUG_SQE_ELEM(28, __u32, fadvise_advice);
7210 BUILD_BUG_SQE_ELEM(32, __u64, user_data);
7211 BUILD_BUG_SQE_ELEM(40, __u16, buf_index);
7212 BUILD_BUG_SQE_ELEM(42, __u16, personality);
7213
Jens Axboed3656342019-12-18 09:50:26 -07007214 BUILD_BUG_ON(ARRAY_SIZE(io_op_defs) != IORING_OP_LAST);
Jens Axboe2b188cc2019-01-07 10:46:33 -07007215 req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
7216 return 0;
7217};
7218__initcall(io_uring_init);